-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkruskal.py
More file actions
57 lines (40 loc) · 1.36 KB
/
kruskal.py
File metadata and controls
57 lines (40 loc) · 1.36 KB
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
class Grafo:
def __init__(self,vertices):
self.numVertices = vertices
self.grafo = []
def inserir(self,u,v,w):
self.grafo.append([u,v,w])
def encontrar(self, pai, i):
if pai[i] == i:
return i
return self.encontrar(pai, pai[i])
def unir(self, pai, rank, x, y):
x = self.encontrar(pai, x)
y = self.encontrar(pai, y)
if rank[x] < rank[y]:
pai[x] = y
elif rank[x] > rank[y]:
pai[y] = x
else:
pai[y] = x
rank[x] += 1
def KruskalMST(self):
resultado =[]
i = 0
e = 0
#aqui tem que sortear usando o w, que é o peso, por isso ele usa a key como item[2]
self.grafo = sorted(self.grafo,key=lambda item: item[2])
pai = [] ; rank = []
for node in range(self.numVertices):
pai.append(node)
rank.append(0)
while e < self.numVertices -1 :
u,v,w = self.grafo[i]
i += 1
x = self.encontrar(pai, u)
y = self.encontrar(pai ,v)
if x != y:
e = e + 1
resultado.append([u,v,w])
self.unir(pai, rank, x, y)
return resultado