-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_coloring.py
80 lines (65 loc) · 2.31 KB
/
test_coloring.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import PCST
import networkx as nx
import matplotlib.pyplot as plt
def visualize_graph(nodes, edges, colors):
G = nx.Graph()
for edge in edges:
G.add_edge(edge.node1, edge.node2, weight=edge.cost)
pos = nx.spring_layout(G) # positions for all nodes
# nodes
nx.draw_networkx_nodes(G, pos, node_size=700, node_color=colors, cmap=plt.cm.rainbow)
# edges
nx.draw_networkx_edges(G, pos, width=6)
# labels
labels = {node.id: str(node.id) + '\n' + str(node.prize) for node in nodes}
nx.draw_networkx_labels(G, pos, labels=labels, font_size=16, font_family='sans-serif')
plt.axis('off')
plt.show()
def visualize_tree(tree, colors):
G = nx.Graph()
for edge in tree:
G.add_edge(edge.node1, edge.node2, weight=edge.cost)
pos = nx.spring_layout(G) # positions for all nodes
# nodes
nx.draw_networkx_nodes(G, pos, node_size=700, node_color=colors, cmap=plt.cm.rainbow)
# edges
nx.draw_networkx_edges(G, pos, width=6)
# labels
labels = {node.id: str(node.id) + '\n' + str(node.prize) for node in nodes}
nx.draw_networkx_labels(G, pos, font_size=20, font_family='sans-serif')
plt.axis('off')
plt.show()
# ... [Your nodes and edges definition]
nodes = [
PCST.Node(id=0, prize=1),
PCST.Node(id=1, prize=2),
PCST.Node(id=2, prize=3),
PCST.Node(id=3, prize=4),
PCST.Node(id=4, prize=5),
PCST.Node(id=5, prize=6),
PCST.Node(id=6, prize=7),
PCST.Node(id=7, prize=8),
PCST.Node(id=8, prize=9),
PCST.Node(id=9, prize=10)
]
edges = [
PCST.Edge(node1=0, node2=1, cost=1),
PCST.Edge(node1=1, node2=2, cost=2),
PCST.Edge(node1=2, node2=3, cost=3),
PCST.Edge(node1=3, node2=4, cost=4),
PCST.Edge(node1=4, node2=5, cost=5),
PCST.Edge(node1=5, node2=6, cost=6),
PCST.Edge(node1=6, node2=7, cost=7),
PCST.Edge(node1=7, node2=8, cost=8),
PCST.Edge(node1=8, node2=9, cost=9),
PCST.Edge(node1=0, node2=9, cost=10),
PCST.Edge(node1=1, node2=8, cost=11),
PCST.Edge(node1=2, node2=7, cost=12),
PCST.Edge(node1=3, node2=6, cost=13),
PCST.Edge(node1=4, node2=5, cost=14),
PCST.Edge(node1=0, node2=5, cost=15)
]
colors = PCST.greedyVertexColoring(edges, len(nodes))
# PCST.greedyVertexColoring(edges, len(nodes))
visualize_graph(nodes, edges, colors)
#visualize_tree(tree, colors)