-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkruskalFinalSol.py
62 lines (54 loc) · 2.27 KB
/
kruskalFinalSol.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
class graph:
#initialize graph with N vertices, and no edges
def __init__(self, N):
self.vertices = N #no. of vertices
self.edges = [] #dictionary to keep edges etc.
#edge is represented by (src,dst,weight)
def addEdge(self,src,dst,weight):
self.edges.append([src,dst,weight])
#find the "representative" of the group node i belongs to
def find(self, representatives, i):
#print(representatives, i)
if representatives[i-1] == i:
return i
return self.find(representatives, representatives[i-1])
#unite two groups and decide their new representative by rank
def unite(self, representatives, rank, x, y):
xRep = self.find(representatives, x)
yRep = self.find(representatives, y)
if rank[xRep-1] < rank[yRep-1]:
representatives[xRep-1] = yRep
elif rank[xRep-1] > rank[yRep-1]:
representatives[yRep-1] = xRep
else :
representatives[yRep-1] = xRep
rank[xRep-1] += 1
class Solution:
#run kruskal algorithm to find weight of MST
def minimumCost(N, connections):
g = graph(N)
for item in connections:
g.addEdge(item[0],item[1],item[2])
sum = 0; i = 0; e = 0;
edges = sorted(g.edges, key = lambda item:item[2])
representatives =[]; rank = [];
#build a list where every element represents the group of the node in the matching index
for nodeInd in range(N):
representatives.append(nodeInd+1)
rank.append(0)
#haven't built a tree yet, and there are more edges to go through
while e < N-1 and i < len(edges):
u,v,w = edges[i]
i += 1
rep1 = g.find(representatives, u)
rep2 = g.find(representatives ,v)
#different groups -> unite them into one
if rep1 != rep2:
e = e + 1
sum += w
g.unite(representatives, rank, rep1, rep2)
if(e < N-1): #no tree can be built
return -1
else:
return sum
print("Minimal cost for MST:" ,Solution.minimumCost(N = 3, connections = [[1,2,5],[1,3,6],[2,3,1]]))