From 0526be9c23ae8d948daa4a233f2f05678ee10a58 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 12 Apr 2023 09:40:40 -0400 Subject: [PATCH 1/4] Improve documentation for graph_greedy_color This commit improves the documentation for the graph_greedy_color function. Previously, the details on the function and the algorithm it implemented where a bit sparse. This commit expands it by explaining the source for the algorithm, making it clear it's not always going to return an optimal solution, and also adding an example. --- src/coloring.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/coloring.rs b/src/coloring.rs index 329eb8385..ca89884cb 100644 --- a/src/coloring.rs +++ b/src/coloring.rs @@ -26,13 +26,36 @@ use petgraph::visit::NodeCount; use rayon::prelude::*; -/// Color a PyGraph using a largest_first strategy greedy graph coloring. +/// Color a :class:`~.PyGraph` object using a greedy graph coloring algorithm +/// +/// This function uses a `largest-first` strategy as described in [1]_ and colors +/// the nodes with higher degree first. As the coloring problem is NP-hard +/// this is a heuristic algorithm and may not return an optimal solution. /// /// :param PyGraph: The input PyGraph object to color /// /// :returns: A dictionary where keys are node indices and the value is /// the color /// :rtype: dict +/// +/// .. jupyter-execute:: +/// +/// import matplotlib.pyplot as plt +/// +/// import rustworkx as rx +/// from rustworkx.visualization import mpl_draw +/// +/// graph = rx.generators.generalized_petersen_graph(5, 2) +/// coloring = rx.graph_greedy_color(graph) +/// colors = [coloring[node] for node in graph.node_indices()] +/// +/// # Draw colored graph +/// layout = rx.shell_layout(graph, nlist=[[0, 1, 2, 3, 4],[6, 7, 8, 9, 5]]) +/// mpl_draw(graph, node_color=colors, pos=layout) +/// +/// +/// .. [1] Adrian Kosowski, and Krzysztof Manuszewski, Classical Coloring of Graphs, +/// Graph Colorings, 2-19, 2004. ISBN 0-8218-3458-4. #[pyfunction] #[pyo3(text_signature = "(graph, /)")] pub fn graph_greedy_color(py: Python, graph: &graph::PyGraph) -> PyResult { From b0346c3a1c0a9bcde91c2419abc19b2abb503af5 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 12 Apr 2023 10:04:23 -0400 Subject: [PATCH 2/4] Update src/coloring.rs --- src/coloring.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/coloring.rs b/src/coloring.rs index ca89884cb..c45155512 100644 --- a/src/coloring.rs +++ b/src/coloring.rs @@ -40,8 +40,6 @@ use rayon::prelude::*; /// /// .. jupyter-execute:: /// -/// import matplotlib.pyplot as plt -/// /// import rustworkx as rx /// from rustworkx.visualization import mpl_draw /// From 9243086e21e2d9556a40aa17dcd7d0efacfe4595 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 10 May 2023 15:10:59 -0400 Subject: [PATCH 3/4] Change to use note directive --- src/coloring.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/coloring.rs b/src/coloring.rs index c45155512..683f048a9 100644 --- a/src/coloring.rs +++ b/src/coloring.rs @@ -29,8 +29,12 @@ use rayon::prelude::*; /// Color a :class:`~.PyGraph` object using a greedy graph coloring algorithm /// /// This function uses a `largest-first` strategy as described in [1]_ and colors -/// the nodes with higher degree first. As the coloring problem is NP-hard -/// this is a heuristic algorithm and may not return an optimal solution. +/// the nodes with higher degree first. +/// +/// .. note:: +/// +/// The coloring problem is NP-hard and this is a heuristic algorithm which +/// may not return an optimal solution. /// /// :param PyGraph: The input PyGraph object to color /// From fc5278f69beaf581db7602d35f4ca64f7cfa83e4 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Tue, 16 May 2023 09:17:26 -0400 Subject: [PATCH 4/4] Update src/coloring.rs Co-authored-by: Julien Gacon --- src/coloring.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coloring.rs b/src/coloring.rs index 683f048a9..b432ee1d3 100644 --- a/src/coloring.rs +++ b/src/coloring.rs @@ -26,7 +26,7 @@ use petgraph::visit::NodeCount; use rayon::prelude::*; -/// Color a :class:`~.PyGraph` object using a greedy graph coloring algorithm +/// Color a :class:`~.PyGraph` object using a greedy graph coloring algorithm. /// /// This function uses a `largest-first` strategy as described in [1]_ and colors /// the nodes with higher degree first.