/// Greedily color the graph with a largest-first strategy.
///
/// Colors are numbered starting at 0, with smaller colors being
/// the ones that contain the largest number of associated vertices.
Greedily color the graph with a largest-first strategy.
Colors are numbered starting at 0, with smaller colors being
the ones that contain the largest number of associated vertices.
Greedily color the graph with a largest - first strategy . Colors are numbered starting at 0 with smaller colors being the ones that contain the largest number of associated vertices .
fn greedy_color(graph: &AdjacencyList) -> (u32, Vec<u32>) {
    let nvertices = graph.nvertices();
    let mut vertices: Vec<_> = (0..nvertices).map(|v| v as u32).collect();
    vertices.sort_unstable_by_key(|&v| graph.degree(v));
    let mut colors: Vec<u32> = vec![std::u32::MAX; nvertices];
    let mut has_been_colored = vec![false; nvertices];
    let mut adjacent_colors: Vec<bool> = Vec::new();
    for vertex in vertices.into_iter().rev() {
        // loop invariant is that none of adjacent_colors elements are true
        // what color are our neighbors?
        let mut nadjacent_colors = 0;
        for &n in graph.neighbors(vertex) {
            let n = n as usize;
            if !has_been_colored[n] {
                continue;
            }
            let c = colors[n] as usize;
            if !adjacent_colors[c] {
                adjacent_colors[c] = true;
                nadjacent_colors += 1;
                if nadjacent_colors == colors.len() {
                    break;
                }
            }
        }
        // what's the smallest color not in our neighbors?
        let chosen = if nadjacent_colors == adjacent_colors.len() {
            adjacent_colors.push(false);
            adjacent_colors.len() - 1
        } else {
            adjacent_colors.iter().copied().position(|x| !x).unwrap()
        };
        colors[vertex as usize] = chosen as u32;
        has_been_colored[vertex as usize] = true;
        // retain loop invariant, unset neighbor colors
        if graph.degree(vertex) >= adjacent_colors.len() {
            graph
                .neighbors(vertex)
                .iter()
                .copied()
                .flat_map(|n| {
                    let n = n as usize;
                    if has_been_colored[n] {
                        Some(colors[n])
                    } else {
                        None
                    }
                })
                .for_each(|c| {
                    adjacent_colors[c as usize] = false;
                });
        } else {
            for c in adjacent_colors.iter_mut() {
                *c = false;
            }
        }
    }
    let ncolors = adjacent_colors.len();
    let mut color_counts = vec![0u32; ncolors];
    colors.iter().copied().for_each(|c| {
        color_counts[c as usize] += 1;
    });
    let code: Vec<_> = (0..ncolors)
        .sorted_by_key(|i| -(color_counts[*i] as i64))
        .collect();
    let mut recode = vec![0u32; ncolors];
    for i in 0..ncolors {
        recode[code[i]] = i as u32;
    }
    for color in colors.iter_mut() {
        *color = recode[*color as usize];
    }
    (ncolors as u32, colors)
}
fn greedy_color ( graph : & AdjacencyList ) -> ( u32 , Vec < u32 > ) { let nvertices = graph . nvertices ( ) ; let mut vertices : Vec < _ > = ( 0 .. nvertices ) . map ( | v | v as u32 ) . collect ( ) ; vertices . sort_unstable_by_key ( | & v | graph . degree ( v ) ) ; let mut colors : Vec < u32 > = vec ! [ std u32 MAX nvertices ] ; let mut has_been_colored = vec ! [ false nvertices ] ; let mut adjacent_colors : Vec < bool > = Vec :: new ( ) ; for vertex in vertices . into_iter ( ) . rev ( ) { // loop invariant is that none of adjacent_colors elements are true // what color are our neighbors? let mut nadjacent_colors = 0 ; for & n in graph . neighbors ( vertex ) { let n = n as usize ; if ! has_been_colored [ n ] { continue ; } let c = colors [ n ] as usize ; if ! adjacent_colors [ c ] { adjacent_colors [ c ] = true ; nadjacent_colors += 1 ; if nadjacent_colors == colors . len ( ) { break ; } } } // what's the smallest color not in our neighbors? let chosen = if nadjacent_colors == adjacent_colors . len ( ) { adjacent_colors . push ( false ) ; adjacent_colors . len ( ) - 1 } else { adjacent_colors . iter ( ) . copied ( ) . position ( | x | ! x ) . unwrap ( ) } ; colors [ vertex as usize ] = chosen as u32 ; has_been_colored [ vertex as usize ] = true ; // retain loop invariant, unset neighbor colors if graph . degree ( vertex ) >= adjacent_colors . len ( ) { graph . neighbors ( vertex ) . iter ( ) . copied ( ) . flat_map ( | n | { let n = n as usize ; if has_been_colored [ n ] { Some ( colors [ n ] ) } else { None } } ) . for_each ( | c | { adjacent_colors [ c as usize ] = false ; } ) ; } else { for c in adjacent_colors . iter_mut ( ) { * c = false ; } } } let ncolors = adjacent_colors . len ( ) ; let mut color_counts = vec ! [ 0u32 ncolors ] ; colors . iter ( ) . copied ( ) . for_each ( | c | { color_counts [ c as usize ] += 1 ; } ) ; let code : Vec < _ > = ( 0 .. ncolors ) . sorted_by_key ( | i | - ( color_counts [ * i ] as i64 ) ) . collect ( ) ; let mut recode = vec ! [ 0u32 ncolors ] ; for i in 0 .. ncolors { recode [ code [ i ] ] = i as u32 ; } for color in colors . iter_mut ( ) { * color = recode [ * color as usize ] ; } ( ncolors as u32 , colors ) }
Greedily color the graph with a largest-first strategy.
Greedily color the graph with a largest - first strategy .
graph
&AdjacencyList