Skip to main content

greedy_coloring

Function greedy_coloring 

Source
pub fn greedy_coloring(graph: &Graph) -> IgraphResult<Vec<u32>>
Expand description

Greedy vertex coloring in natural vertex order.

Assigns each vertex the smallest colour (0-based) not used by any of its already-coloured neighbours. Returns the colour assignment as a Vec<u32> indexed by vertex id.

Works for both directed and undirected graphs (for directed graphs, both in- and out-neighbours are considered as “adjacent”).

§Examples

use rust_igraph::{Graph, greedy_coloring};

// Path 0-1-2: greedy gives {0, 1, 0}
let g = Graph::from_edges(&[(0,1),(1,2)], false, Some(3)).unwrap();
let c = greedy_coloring(&g).unwrap();
assert_eq!(c, vec![0, 1, 0]);