-
Notifications
You must be signed in to change notification settings - Fork 0
/
mst_prim.py
47 lines (38 loc) · 1.05 KB
/
mst_prim.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
from queue import PriorityQueue
def prim(G, s):
n = len(G)
visited = [False for v in range(n)]
parent = [None for v in range(n)]
weight = [float('inf') for v in range(n)]
Q = PriorityQueue()
visited[s] = True
Q.put((0, s))
weight[s] = 0
while not Q.empty():
_, k = Q.get()
visited[k] = True
for v, w in G[k]:
if weight[v] > w and not visited[v]:
parent[v] = k
weight[v] = w
Q.put((weight[v], v))
print(parent)
G = [[(1, 1), (3, 5), (5, 8)], [(0, 1), (2, 3)], [(1, 3), (3, 4), (4, 6)], [(0,5), (4, 2), (2, 4), (5, 7)], [(3, 2), (2, 6)], [(0, 8), (3, 7)]]
prim(G, 2)
# n = 6
# B = [(0, 1, 1),
# (0, 3, 8),
# (0, 4, 5),
# (1, 2, 3),
# (2, 4, 4),
# (2, 5, 6),
# (3, 4, 7),
# (4, 5, 2)
# ]
# def graph(B, n):
# G = [[] for _ in range(n)]
# for kraw in B:
# G[kraw[0]].append([kraw[1], kraw[2]])
# G[kraw[1]].append([kraw[0], kraw[2]])
# return G
# G = graph(B, n)