diff options
author | Vanshika Mishra <74042272+vanshika230@users.noreply.github.com> | 2023-05-02 16:05:20 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-02 13:35:20 +0300 |
commit | 6681b5fc1375f9c2c9b466bd2509b08b91348938 (patch) | |
tree | 436559f49bfe1b2df33b893ea75a45fc02a057ad | |
parent | 301b52b089a1d43f34976c0472521adc571e10d7 (diff) | |
download | networkx-6681b5fc1375f9c2c9b466bd2509b08b91348938.tar.gz |
Add Greedy Coloring Example to Gallery (#6647)
* plot_greedy_coloring_Example
* suggestions incorporated
* fix error
* fixed error
* Minor formatting tweaks.
---------
Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
-rw-r--r-- | examples/algorithms/plot_greedy_coloring.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/examples/algorithms/plot_greedy_coloring.py b/examples/algorithms/plot_greedy_coloring.py new file mode 100644 index 00000000..fcc6a0f0 --- /dev/null +++ b/examples/algorithms/plot_greedy_coloring.py @@ -0,0 +1,35 @@ +""" +=============== +Greedy Coloring +=============== + +We attempt to color a graph using as few colors as possible, where no neighbours +of a node can have same color as the node itself. +""" +import networkx as nx +import matplotlib.pyplot as plt +import matplotlib.colors as mpl + +G = nx.dodecahedral_graph() + +# Apply greedy coloring +graph_coloring = nx.greedy_color(G) +unique_colors = set(graph_coloring.values()) + +# Assign colors to nodes based on the greedy coloring +graph_color_to_mpl_color = dict(zip(unique_colors, mpl.TABLEAU_COLORS)) +node_colors = [graph_color_to_mpl_color[graph_coloring[n]] for n in G.nodes()] + +pos = nx.spring_layout(G, seed=14) +nx.draw( + G, + pos, + with_labels=True, + node_size=500, + node_color=node_colors, + edge_color="grey", + font_size=12, + font_color="#333333", + width=2, +) +plt.show() |