Skip to content

Commit ef055e6

Browse files
committed
Prim's Algorithm using Adjacency Matrix
1 parent 8e6bb22 commit ef055e6

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

Graph/P06_Prim's-Algorithm.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Author: OMKAR PATHAK
2+
3+
# Algorithm:
4+
# 1) Create a set mstSet that keeps track of vertices already included in MST.
5+
# 2) Assign a key value to all vertices in the input graph. Initialize all key values as INFINITE.
6+
# Assign key value as 0 for the first vertex so that it is picked first.
7+
# 3) While mstSet doesn’t include all vertices
8+
# a) Pick a vertex u which is not there in mstSet and has minimum key value.
9+
# b) Include u to mstSet.
10+
# c) Update key value of all adjacent vertices of u. To update the key values, iterate through all adjacent
11+
# vertices. For every adjacent vertex v, if weight of edge u-v is less than the previous key value of v,
12+
# update the key value as weight of u-v
13+
14+
# Properties of Prim's Algorithm:
15+
# 1) The edges in the subset of some minimum spanning tree always form a single tree.
16+
# 2) It grows the tree until it spans all the vertices of the graph.
17+
# 3) An edge is added to the tree, at every step, that crosses a cut if its weight is the minimum of any edge
18+
# crossing the cut, connecting it to a vertex of the graph.
19+
20+
import sys
21+
22+
class Graph(object):
23+
def __init__(self, vertices):
24+
self.vertices = vertices # Total number of Vertices in the graph
25+
self.graph = [[0 for column in range(vertices)]
26+
for row in range(vertices)] # Initialize the adjacency matrix with zeros
27+
28+
def getMinimumKey(self, weight, visited):
29+
# initialize the min by infinite number
30+
min = 9999
31+
32+
for i in range(self.vertices):
33+
# Find the edge with minimum weight if it not visited
34+
if weight[i] < min and visited[i] == False:
35+
min = weight[i]
36+
minIndex = i
37+
38+
# Return the index of the found edge with minimum weight
39+
return minIndex
40+
41+
def primsAlgo(self):
42+
weight = [9999] * self.vertices # This list will be used for storing the weights of all vertices
43+
MST = [None] * self.vertices # This will contain our minimum spanning tree
44+
weight[0] = 0 # Weight of the root node will be 0
45+
visited = [False] * self.vertices
46+
MST[0] = -1 # Choosing first node as root vertex
47+
48+
for _ in range(self.vertices):
49+
minIndex = self.getMinimumKey(weight, visited)
50+
# mark the index as visited
51+
visited[minIndex] = True
52+
53+
for vertex in range(self.vertices):
54+
if self.graph[minIndex][vertex] > 0 and visited[vertex] == False and \
55+
weight[vertex] > self.graph[minIndex][vertex]:
56+
weight[vertex] = self.graph[minIndex][vertex]
57+
MST[vertex] = minIndex
58+
59+
self.printMST(MST)
60+
61+
def printMST(self, MST):
62+
print ("Edge \tWeight")
63+
for i in range(1, self.vertices):
64+
print (MST[i],"-",i,"\t",self.graph[i][ MST[i] ])
65+
66+
if __name__ == '__main__':
67+
g = Graph(6)
68+
69+
g.graph = [[0, 3, 2, 5, 7, 3],
70+
[3, 0, 4, 8, 6, 6],
71+
[2, 4, 0, 7, 1, 3],
72+
[5, 8, 7, 0, 2, 4],
73+
[7, 6, 1, 2, 0, 3],
74+
[3, 6, 3, 4, 3, 0]]
75+
76+
g.primsAlgo()

0 commit comments

Comments
 (0)