-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKruskal.py
82 lines (64 loc) · 1.9 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
81
82
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed May 3 15:24:56 2017
@author: ska
Kruskal MST: time complexity ElogV
"""
#==============================================================================
# Implemention of Union-Find Data Structure
#==============================================================================
def find(parent, i):
if parent[i] == i:
return i
return find(parent, parent[i])
def union(parent, rank, x,y):
xroot = find(parent, x)
yroot = find(parent, y)
#Assign parent according to rank
if rank[xroot] > rank[yroot]:
parent[yroot] = xroot
elif rank[xroot] < rank[yroot]:
parent[xroot] = yroot
else:
parent[xroot] = yroot
rank[yroot] += 1
#==============================================================================
#Kruskal Minimum Spanning Tree |||| graph is of form [u,v,w], where u and v ar virtices
# and w weight|| V = total no. of virtices
#==============================================================================
def KruskalMST():
result = []
graphs = sorted(graph , key = lambda item:item[2])
parent=[]
rank =[0]*V
for i in range(V):
parent.append(i)
i = 0 # index for sorted edges
e = 0 # index for result
while e < V-1:
u,v,w = graphs[i]
i += 1
x = find(parent, u)
y = find(parent, v)
if x != y:
union(parent, rank, x,y)
e += 1
result.append([u,v,w])
print(result)
#==============================================================================
# Graph Implementation
#==============================================================================
def addEdge(u,v,w):
graph.append([u,v,w])
graph = []
#==============================================================================
# Implementation by example
#==============================================================================
V =4
addEdge(0, 1, 10)
addEdge(0, 2, 6)
addEdge(0, 3, 5)
addEdge(1, 3, 15)
addEdge(2, 3, 4)
KruskalMST()