Thanks to visit codestin.com
Credit goes to github.com

Skip to content

dsatur_coloring reports one used color for the empty graph #975

@meng-xu-cs

Description

@meng-xu-cs

Summary

dsatur_coloring tracks the highest assigned color in max_color and returns max_color + 1 as the number of used colors.

When the input graph is empty, no node is ever colored, max_color stays at its sentinel value, and the function still returns 1. And yet, an empty graph uses zero colors.

Expected behavior

The color count should be 0 when no node was colored.

Concrete regression test

use petgraph::{Graph, Undirected, algo::dsatur_coloring};

#[test]
fn dsatur_coloring_empty_graph_uses_zero_colors() {
    let graph: Graph<(), (), Undirected> = Graph::new_undirected();

    let (coloring, nb_colors) = dsatur_coloring(&graph);

    assert!(coloring.is_empty());
    assert_eq!(nb_colors, 0);
}

This currently fails because dsatur_coloring(&graph) returns (empty_map, 1) for an empty graph.

Suggested fix

Special-case the empty graph or derive the count from whether any color assignment actually happened.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions