-
Notifications
You must be signed in to change notification settings - Fork 0
/
prims.py
71 lines (57 loc) · 1.36 KB
/
prims.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
import heapq
def minSpanningTree(edges, n):
mSet = set()
for src, dest, wt in edges:
mSet.add(src)
mSet.add(dest)
adj = {}
for c in mSet:
adj[c] = []
for src, dest, wt in edges:
adj[src].append((dest, wt))
adj[dest].append((src, wt))
minHeap = []
for node, wt in adj['A']:
heapq.heappush(minHeap, (wt, 'A', node))
visited = set()
visited.add("A")
mst = []
mst_val = 0
while minHeap:
cost, src, dest = heapq.heappop(minHeap)
if dest in visited:
continue
visited.add(dest)
mst.append((src, dest))
mst_val += cost
for child, c in adj[dest]:
if child not in visited:
heapq.heappush(minHeap, (c, dest, child))
print("ORDER: ", mst)
print("COST: ", mst_val)
return mst
minSpanningTree(
[
('A', 'B', 3),
('A', 'D', 6),
('A', 'E', 9),
('B', 'C', 2),
('B', 'D', 4),
('B', 'E', 9),
('B', 'F', 9),
('C', 'D', 2),
('C', 'F', 8),
('C', 'G', 9),
('D', 'G', 9),
('E', 'F', 8),
('E', 'J', 18),
('F', 'G', 7),
('F', 'I', 9),
('F', 'J', 10),
('G', 'H', 4),
('G', 'I', 5),
('I', 'H', 1),
('I', 'J', 3),
('H', 'J', 4)],
10
)