forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prims_minimum_spanning.py
42 lines (33 loc) · 1 KB
/
prims_minimum_spanning.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
'''
This Prim's Algorithm Code is for finding weight of minimum spanning tree
of a connected graph.
For argument graph, it should be a dictionary type such as:
graph = {
'a': [ [3, 'b'], [8,'c'] ],
'b': [ [3, 'a'], [5, 'd'] ],
'c': [ [8, 'a'], [2, 'd'], [4, 'e'] ],
'd': [ [5, 'b'], [2, 'c'], [6, 'e'] ],
'e': [ [4, 'c'], [6, 'd'] ]
}
where 'a','b','c','d','e' are nodes (these can be 1,2,3,4,5 as well)
'''
import heapq # for priority queue
def prims_minimum_spanning(graph_used):
"""
Prim's algorithm to find weight of minimum spanning tree
"""
vis=[]
heap=[[0,1]]
prim = set()
mincost=0
while len(heap) > 0:
cost, node = heapq.heappop(heap)
if node in vis:
continue
mincost += cost
prim.add(node)
vis.append(node)
for distance, adjacent in graph_used[node]:
if adjacent not in vis:
heapq.heappush(heap, [distance, adjacent])
return mincost