From 4a7234d4592166a1a13bc6b8e8b3b201019df23b Mon Sep 17 00:00:00 2001 From: Himanshu Airan <62210670+Himanshu-77@users.noreply.github.com> Date: Wed, 13 May 2020 18:30:57 +0530 Subject: [PATCH 1/6] Create prims_minimum_spanning.py code implemented for Prim's Algorithm to find minimum spanning tree. Although there was file named minimum_spanning_tree.py in graph section but contain only Kruskal Algorithm . Prim's Algo. was still missing --- algorithms/graph/prims_minimum_spanning.py | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 algorithms/graph/prims_minimum_spanning.py diff --git a/algorithms/graph/prims_minimum_spanning.py b/algorithms/graph/prims_minimum_spanning.py new file mode 100644 index 000000000..adb31910c --- /dev/null +++ b/algorithms/graph/prims_minimum_spanning.py @@ -0,0 +1,36 @@ +import heapq # for priority queue + +# input number of nodes and edges in graph +n, e = map (int,input().split()) + +# initializing empty graph as a dictionary (of the form {int:list}) +g = dict (zip ([i for i in range(1,n+1)],[[] for i in range(n)])) + +# input graph data +for i in range(e): + a, b, c = map (int,input().split()) + g[a].append([c,b]) + g[b].append([c,a]) + +vis = [] +s = [[0,1]] +prim = [] +mincost = 0 + +# prim's algo. to find weight of minimum spanning tree +while (len(s)>0): + v = heapq.heappop(s) + x = v[1] + if (x in vis): + continue + + mincost += v[0] + prim.append(x) + vis.append(x) + + for j in g[x]: + i = j[-1] + if(i not in vis): + heapq.heappush(s,j) + +print(mincost) From d7ceafc0497022276521f42e5686b0361f73f7fe Mon Sep 17 00:00:00 2001 From: Himanshu Airan <62210670+Himanshu-77@users.noreply.github.com> Date: Wed, 13 May 2020 22:29:53 +0530 Subject: [PATCH 2/6] Update prims_minimum_spanning.py function and sample test cases added --- algorithms/graph/prims_minimum_spanning.py | 90 ++++++++++++++-------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/algorithms/graph/prims_minimum_spanning.py b/algorithms/graph/prims_minimum_spanning.py index adb31910c..060235eac 100644 --- a/algorithms/graph/prims_minimum_spanning.py +++ b/algorithms/graph/prims_minimum_spanning.py @@ -1,36 +1,64 @@ import heapq # for priority queue -# input number of nodes and edges in graph -n, e = map (int,input().split()) +# prim's algo. to find weight of minimum spanning tree +def prims(graph): + vis=[] + s=[[0,1]] + prim = [] + mincost=0 + + while(len(s)>0): + v=heapq.heappop(s) + x=v[1] + if(x in vis): + continue -# initializing empty graph as a dictionary (of the form {int:list}) -g = dict (zip ([i for i in range(1,n+1)],[[] for i in range(n)])) + mincost += v[0] + prim.append(x) + vis.append(x) -# input graph data -for i in range(e): - a, b, c = map (int,input().split()) - g[a].append([c,b]) - g[b].append([c,a]) - -vis = [] -s = [[0,1]] -prim = [] -mincost = 0 + for j in g[x]: + i=j[-1] + if(i not in vis): + heapq.heappush(s,j) -# prim's algo. to find weight of minimum spanning tree -while (len(s)>0): - v = heapq.heappop(s) - x = v[1] - if (x in vis): - continue - - mincost += v[0] - prim.append(x) - vis.append(x) - - for j in g[x]: - i = j[-1] - if(i not in vis): - heapq.heappush(s,j) - -print(mincost) + return mincost + + + +if __name__=="__main__": + + # input number of nodes and edges in graph + n,e = map(int,input().split()) + + # initializing empty graph as a dictionary (of the form {int:list}) + g=dict(zip([i for i in range(1,n+1)],[[] for i in range(n)])) + + # input graph data + for i in range(e): + a,b,c=map(int,input().split()) + g[a].append([c,b]) + g[b].append([c,a]) + + # print weight of minimum spanning tree + print(prims(g)) + + ''' tests- + Input : 4 5 + 1 2 7 + 1 4 6 + 2 4 9 + 4 3 8 + 2 3 6 + Output : 19 + + + Input : 5 6 + 1 2 3 + 1 3 8 + 2 4 5 + 3 4 2 + 3 5 4 + 4 5 6 + Output : 14 + ''' From 52d835ec8a3dfec53c3cab23598be6f63da9addc Mon Sep 17 00:00:00 2001 From: Himanshu Airan <62210670+Himanshu-77@users.noreply.github.com> Date: Thu, 14 May 2020 15:24:30 +0530 Subject: [PATCH 3/6] Update prims_minimum_spanning.py function created --- algorithms/graph/prims_minimum_spanning.py | 60 +++++++--------------- 1 file changed, 19 insertions(+), 41 deletions(-) diff --git a/algorithms/graph/prims_minimum_spanning.py b/algorithms/graph/prims_minimum_spanning.py index 060235eac..7e290d81a 100644 --- a/algorithms/graph/prims_minimum_spanning.py +++ b/algorithms/graph/prims_minimum_spanning.py @@ -1,7 +1,24 @@ +''' +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 # prim's algo. to find weight of minimum spanning tree -def prims(graph): +def prims(graph_used): vis=[] s=[[0,1]] prim = [] @@ -17,48 +34,9 @@ def prims(graph): prim.append(x) vis.append(x) - for j in g[x]: + for j in graph_used[x]: i=j[-1] if(i not in vis): heapq.heappush(s,j) return mincost - - - -if __name__=="__main__": - - # input number of nodes and edges in graph - n,e = map(int,input().split()) - - # initializing empty graph as a dictionary (of the form {int:list}) - g=dict(zip([i for i in range(1,n+1)],[[] for i in range(n)])) - - # input graph data - for i in range(e): - a,b,c=map(int,input().split()) - g[a].append([c,b]) - g[b].append([c,a]) - - # print weight of minimum spanning tree - print(prims(g)) - - ''' tests- - Input : 4 5 - 1 2 7 - 1 4 6 - 2 4 9 - 4 3 8 - 2 3 6 - Output : 19 - - - Input : 5 6 - 1 2 3 - 1 3 8 - 2 4 5 - 3 4 2 - 3 5 4 - 4 5 6 - Output : 14 - ''' From 581be000530d67b38038d140c457308ee0afd319 Mon Sep 17 00:00:00 2001 From: Himanshu Airan <62210670+Himanshu-77@users.noreply.github.com> Date: Thu, 14 May 2020 15:42:16 +0530 Subject: [PATCH 4/6] Update test_graph.py unittest added for prims_minimum_spanning.py --- tests/test_graph.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_graph.py b/tests/test_graph.py index 83d35293b..325b5d896 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -10,6 +10,7 @@ from algorithms.graph import bellman_ford from algorithms.graph import bellman_ford from algorithms.graph import count_connected_number_of_component +from algorithms.graph import prims_minimum_spanning import unittest @@ -264,3 +265,23 @@ def test_connected_components_without_edges_graph(self): expected_result = 4 result = count_connected_number_of_component.count_components(l,size) self.assertEqual(result,expected_result) + + +class PrimsMinimumSpanning(unittest.TestCase): + def test_prim_spanning(self): + graph1 = { + 1 : [ [3, 2], [8, 3] ], + 2 : [ [3, 1], [5, 4] ], + 3 : [ [8, 1], [2, 4], [4, 5] ], + 4 : [ [5, 2], [2, 3], [6, 5] ], + 5 : [ [4, 3], [6, 4] ] + } + self.assertEqual(14, prims_minimum_spanning(graph1)) + + graph2 = { + 1 : [ [7, 2], [6, 4] ], + 2 : [ [7, 1], [9, 4], [6, 3] ], + 3 : [ [8, 4], [6, 2] ], + 4 : [ [6, 1], [9, 2], [8, 3] ] + } + self.assertEqual(19, prims_minimum_spanning(graph2)) From 2f8be626a187724076ab7b603dd33d6f3f7273b9 Mon Sep 17 00:00:00 2001 From: Himanshu Airan <62210670+Himanshu-77@users.noreply.github.com> Date: Thu, 14 May 2020 15:43:45 +0530 Subject: [PATCH 5/6] Update prims_minimum_spanning.py function name changed --- algorithms/graph/prims_minimum_spanning.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/algorithms/graph/prims_minimum_spanning.py b/algorithms/graph/prims_minimum_spanning.py index 7e290d81a..02295c4c2 100644 --- a/algorithms/graph/prims_minimum_spanning.py +++ b/algorithms/graph/prims_minimum_spanning.py @@ -18,7 +18,7 @@ import heapq # for priority queue # prim's algo. to find weight of minimum spanning tree -def prims(graph_used): +def prims_minimum_spanning(graph_used): vis=[] s=[[0,1]] prim = [] From 3e7bd31d8b5106addf7e638aca8b0147a6a1c7b2 Mon Sep 17 00:00:00 2001 From: Himanshu Airan <62210670+Himanshu-77@users.noreply.github.com> Date: Fri, 15 May 2020 00:39:12 +0530 Subject: [PATCH 6/6] Update README.md link added for new file prims_minimum_spanning.py --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 06fb2d4c4..1fed65ab4 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,7 @@ If you want to uninstall algorithms, it is as simple as: - [markov_chain](algorithms/graph/markov_chain.py) - [minimum_spanning_tree](algorithms/graph/minimum_spanning_tree.py) - [satisfiability](algorithms/graph/satisfiability.py) + - [minimum_spanning_tree_prims](algorithms/graph/prims_minimum_spanning.py) - [tarjan](algorithms/graph/tarjan.py) - [traversal](algorithms/graph/traversal.py) - [maximum_flow](algorithms/graph/maximum_flow.py)