|
| 1 | +# A Python program for Prim's Minimum Spanning Tree (MST) algorithm. |
| 2 | + |
| 3 | +# The program is for adjacency matrix representation of the graph |
| 4 | + |
| 5 | +import sys # Library for INT_MAX |
| 6 | + |
| 7 | +class Graph(): |
| 8 | + |
| 9 | + def __init__(self, vertices): |
| 10 | + self.V = vertices |
| 11 | + self.graph = [[0 for column in range(vertices)] |
| 12 | + for row in range(vertices)] |
| 13 | + |
| 14 | + # A utility function to print the constructed MST stored in parent[] |
| 15 | + def printMST(self, parent): |
| 16 | + print "Edge \tWeight" |
| 17 | + for i in range(1, self.V): |
| 18 | + print parent[i], "-", i, "\t", self.graph[i][ parent[i] ] |
| 19 | + |
| 20 | + # A utility function to find the vertex with |
| 21 | + # minimum distance value, from the set of vertices |
| 22 | + # not yet included in shortest path tree |
| 23 | + def minKey(self, key, mstSet): |
| 24 | + |
| 25 | + # Initilaize min value |
| 26 | + min = sys.maxint |
| 27 | + |
| 28 | + for v in range(self.V): |
| 29 | + if key[v] < min and mstSet[v] == False: |
| 30 | + min = key[v] |
| 31 | + min_index = v |
| 32 | + |
| 33 | + return min_index |
| 34 | + |
| 35 | + # Function to construct and print MST for a graph |
| 36 | + # represented using adjacency matrix representation |
| 37 | + def primMST(self): |
| 38 | + |
| 39 | + # Key values used to pick minimum weight edge in cut |
| 40 | + key = [sys.maxint] * self.V |
| 41 | + parent = [None] * self.V # Array to store constructed MST |
| 42 | + # Make key 0 so that this vertex is picked as first vertex |
| 43 | + key[0] = 0 |
| 44 | + mstSet = [False] * self.V |
| 45 | + |
| 46 | + parent[0] = -1 # First node is always the root of |
| 47 | + |
| 48 | + for cout in range(self.V): |
| 49 | + |
| 50 | + # Pick the minimum distance vertex from |
| 51 | + # the set of vertices not yet processed. |
| 52 | + # u is always equal to src in first iteration |
| 53 | + u = self.minKey(key, mstSet) |
| 54 | + |
| 55 | + # Put the minimum distance vertex in |
| 56 | + # the shortest path tree |
| 57 | + mstSet[u] = True |
| 58 | + |
| 59 | + # Update dist value of the adjacent vertices |
| 60 | + # of the picked vertex only if the current |
| 61 | + # distance is greater than new distance and |
| 62 | + # the vertex in not in the shotest path tree |
| 63 | + for v in range(self.V): |
| 64 | + |
| 65 | + # graph[u][v] is non zero only for adjacent vertices of m |
| 66 | + # mstSet[v] is false for vertices not yet included in MST |
| 67 | + # Update the key only if graph[u][v] is smaller than key[v] |
| 68 | + if self.graph[u][v] > 0 and mstSet[v] == False and key[v] > self.graph[u][v]: |
| 69 | + key[v] = self.graph[u][v] |
| 70 | + parent[v] = u |
| 71 | + |
| 72 | + self.printMST(parent) |
| 73 | + |
| 74 | +g = Graph(5) |
| 75 | +g.graph = [ [0, 2, 0, 6, 0], |
| 76 | + [2, 0, 3, 8, 5], |
| 77 | + [0, 3, 0, 0, 7], |
| 78 | + [6, 8, 0, 0, 9], |
| 79 | + [0, 5, 7, 9, 0]] |
| 80 | + |
| 81 | +g.primMST(); |
0 commit comments