-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGREEDY ALGORITHM:
119 lines (60 loc) · 2.21 KB
/
GREEDY ALGORITHM:
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import networkx as nx
import matplotlib.pyplot as plt
import time
def color_nodes(G):
color_map = {}
colormap = []
for node in sorted(G, key=lambda x: len(G[x]), reverse=True):
neighbor_colors = set(color_map.get(neigh) for neigh in G[node])
#Nodes sorted first, descending based on degree for node in sorted(G, key=lambda x:
len(G[x]), reverse=True): neighbor_colors = set(color_map.get(neigh) for neigh in
G[node])
color_map[node] = next(color for color in range(len(G))if color not in neighbor_colors)
i=0
#print(color_map)
while i<color_map.__len__():
if color_map[i]==0:
colormap.append('yellow')
elif color_map[i]==1:
colormap.append('purple')
elif color_map[i]==2:
colormap.append('lightseagreen')
elif color_map[i]==3:
colormap.append('blue')
elif color_map[i]==4:
colormap.append('magenta')
elif color_map[i]==5:
colormap.append('cyan')
elif color_map[i]==6:
colormap.append('red')
elif color_map[i]==7:
colormap.append('blue_violet')
elif color_map[i]==8:
colormap.append('orange')
elif color_map[i]==9:
colormap.append('lightsalmon')
elif color_map[i]==10:
colormap.append('darkcyan')
elif color_map[i]==11:
colormap.append('green')
elif color_map[i]==12:
colormap.append('lightpink')
elif color_map[i]==13:
colormap.append('y')
i=i+1
plt.subplot(212)
plt.title("Result of Greedy Algorithm")
nx.draw(G, with_labels=True, font_weight='bold',node_color=colormap)
# if _name_ == '_main_':
t = time.process_time()
# Graph drawing
G = nx.Graph()
G.add_nodes_from([0,1,2,3,4,5,6,7,8,9,10,11,12])#13 nodes
G.add_edges_from([(0,1),(0,2),(1,4),(2,4),(2,3),(3,5),(4,5),(5,7),(5,6),(7,9),(6,8),(8,9),(9,11),(
8,10),(10,12),(11,12)])
plt.subplot(211)
plt.title("Initial Graph")
nx.draw_shell(G, with_labels=True, font_weight='bold', node_color='lightgray')
color_nodes(G)
print("Time elapsed:",time.process_time() - t)
plt.show()