-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKruskal's.py
60 lines (52 loc) · 1.2 KB
/
Kruskal's.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
class Node:
def __init__(self,nameofnode):
self.name=nameofnode
a=Node("a")
b=Node("b")
c=Node("c")
d=Node("d")
e=Node("e")
f=Node("f")
g=Node("g")
h=Node("h")
i=Node("i")
edges=dict()
#given graph
listofsets=list()
graph={a:[(b,4),(h,8)],b:[(h,11),(c,8)],c:[(i,2),(f,4),(d,7)],d:[(f,14),(e,9)],e:[(f,10)],f:[(g,2)],g:[(i,6),(h,1)],h:[(i,7)],i:[]}
for x in graph: #for forming the edges
a=set()
a.add(x)
listofsets.append(a)
neighbours=graph[x];
for y in neighbours:
edges[(x,y[0])]=y[1]
'''
for x in edges: #for displaying the edges
v=edges[x]
print(x[0].name+" "+x[1].name+" "+str(v))
'''
sortededges=sorted(edges.items(),key=lambda x:x[1])
print("============================")
cost=0
for y in sortededges: #For displaying the sorted edges
p1=y[0]
u=p1[0]
v=p1[1]
p2=y[1]
for i in range(len(listofsets)):
if u in listofsets[i]:
ii=i
break
for i in range(len(listofsets)):
if v in listofsets[i]:
jj=i
break
if ii==jj:
continue
else:
listofsets[ii]=listofsets[ii]|listofsets[jj]
listofsets[jj]=listofsets[ii]|listofsets[jj]
cost+=p2
print(u.name,v.name)
print("Minimum cost of the spanning tree: "+str(cost))