Skip to content

Commit e498d4e

Browse files
authored
Merge pull request #654 from Himanshu-77/master
Create prims_minimum_spanning.py
2 parents 6d1515d + 3e7bd31 commit e498d4e

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ If you want to uninstall algorithms, it is as simple as:
158158
- [markov_chain](algorithms/graph/markov_chain.py)
159159
- [minimum_spanning_tree](algorithms/graph/minimum_spanning_tree.py)
160160
- [satisfiability](algorithms/graph/satisfiability.py)
161+
- [minimum_spanning_tree_prims](algorithms/graph/prims_minimum_spanning.py)
161162
- [tarjan](algorithms/graph/tarjan.py)
162163
- [traversal](algorithms/graph/traversal.py)
163164
- [maximum_flow](algorithms/graph/maximum_flow.py)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
This Prim's Algorithm Code is for finding weight of minimum spanning tree
3+
of a connected graph.
4+
For argument graph, it should be a dictionary type
5+
such as
6+
graph = {
7+
'a': [ [3, 'b'], [8,'c'] ],
8+
'b': [ [3, 'a'], [5, 'd'] ],
9+
'c': [ [8, 'a'], [2, 'd'], [4, 'e'] ],
10+
'd': [ [5, 'b'], [2, 'c'], [6, 'e'] ],
11+
'e': [ [4, 'c'], [6, 'd'] ]
12+
}
13+
14+
where 'a','b','c','d','e' are nodes (these can be 1,2,3,4,5 as well)
15+
'''
16+
17+
18+
import heapq # for priority queue
19+
20+
# prim's algo. to find weight of minimum spanning tree
21+
def prims_minimum_spanning(graph_used):
22+
vis=[]
23+
s=[[0,1]]
24+
prim = []
25+
mincost=0
26+
27+
while(len(s)>0):
28+
v=heapq.heappop(s)
29+
x=v[1]
30+
if(x in vis):
31+
continue
32+
33+
mincost += v[0]
34+
prim.append(x)
35+
vis.append(x)
36+
37+
for j in graph_used[x]:
38+
i=j[-1]
39+
if(i not in vis):
40+
heapq.heappush(s,j)
41+
42+
return mincost

tests/test_graph.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from algorithms.graph import bellman_ford
1111
from algorithms.graph import bellman_ford
1212
from algorithms.graph import count_connected_number_of_component
13+
from algorithms.graph import prims_minimum_spanning
1314

1415
import unittest
1516

@@ -264,3 +265,23 @@ def test_connected_components_without_edges_graph(self):
264265
expected_result = 4
265266
result = count_connected_number_of_component.count_components(l,size)
266267
self.assertEqual(result,expected_result)
268+
269+
270+
class PrimsMinimumSpanning(unittest.TestCase):
271+
def test_prim_spanning(self):
272+
graph1 = {
273+
1 : [ [3, 2], [8, 3] ],
274+
2 : [ [3, 1], [5, 4] ],
275+
3 : [ [8, 1], [2, 4], [4, 5] ],
276+
4 : [ [5, 2], [2, 3], [6, 5] ],
277+
5 : [ [4, 3], [6, 4] ]
278+
}
279+
self.assertEqual(14, prims_minimum_spanning(graph1))
280+
281+
graph2 = {
282+
1 : [ [7, 2], [6, 4] ],
283+
2 : [ [7, 1], [9, 4], [6, 3] ],
284+
3 : [ [8, 4], [6, 2] ],
285+
4 : [ [6, 1], [9, 2], [8, 3] ]
286+
}
287+
self.assertEqual(19, prims_minimum_spanning(graph2))

0 commit comments

Comments
 (0)