-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkruskal.py
80 lines (64 loc) · 1.8 KB
/
kruskal.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
from collections import OrderedDict, defaultdict
def find_path_compression(v, parent):
"""
Find the root of the vertex
using path compression technique
"""
if v != parent[v]:
parent[v] = find_path_compression(parent[v], parent)
return parent[v]
def find(v, parent):
"""
Find the root of the tree
"""
if parent[v] is None:
return v
return find(parent[v], parent)
def union(x, y, parent, rank):
"""
Union by rank
"""
root_x = find(x,parent)
root_y = find(y,parent)
if root_x == root_y:
return
if rank[root_x] > rank[root_y]:
parent[root_y] = root_x
else:
parent[root_x] = root_y
if rank[root_x] == rank[root_y]:
rank[root_y] += 1
def kruskal(G):
"""
Kruskal's algorithm for finding minimum spanning tree
Input : Graph G(V,E)
Output: Minimum spanning tree
"""
mst = defaultdict(dict)
total_cost = 0
edges = [(G[v][z],v,z) for v in G for z in G[v]]
edges = sorted(edges, key = lambda x: x[0])
parent = {v:None for v in G}
rank = {v:0 for v in G}
for edge in edges:
cost,u,v = edge
if find(u,parent) != find(v,parent):
mst[u][v] = cost
total_cost += cost
union(u,v,parent,rank)
return mst, total_cost
if __name__ == "__main__":
G = {
'A': {'B':5, 'C':6, 'D':4},
'B': {'A':5, 'C':1, 'D':2},
'C': {'A':6, 'B':1, 'D':2, 'E':5, 'F':3},
'D': {'A':4, 'B':2, 'C':2, 'F':4},
'E': {'C':5, 'F':4},
'F': {'C':3, 'D':4, 'E':4}
}
G = OrderedDict(G)
mst, total_cost = kruskal(G)
print("Total cost of MST:", total_cost)
print("Minimum Spanning Tree:", end='\n')
for u in mst:
print(f"{u} : {mst[u]}")