blob: fcc6a0f0483befcd4799100e38d94659dc3406d6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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()
|