|
| 1 | +import sys |
| 2 | + |
| 3 | +class Graph(): |
| 4 | + |
| 5 | + def __init__(self, vertices): |
| 6 | + self.V = vertices |
| 7 | + self.graph = [[0 for column in range(vertices)] |
| 8 | + for row in range(vertices)] |
| 9 | + |
| 10 | + def printSolution(self, dist): |
| 11 | + print ("Vertex \tDistance from Source") |
| 12 | + for node in range(self.V): |
| 13 | + print (node, "\t", dist[node]) |
| 14 | + |
| 15 | + # A utility function to find the vertex with |
| 16 | + # minimum distance value, from the set of vertices |
| 17 | + # not yet included in shortest path tree |
| 18 | + def minDistance(self, dist, sptSet): |
| 19 | + |
| 20 | + # Initialize minimum distance for next node |
| 21 | + min = sys.maxsize |
| 22 | + |
| 23 | + # Search not nearest vertex not in the |
| 24 | + # shortest path tree |
| 25 | + for u in range(self.V): |
| 26 | + if dist[u] < min and sptSet[u] == False: |
| 27 | + min = dist[u] |
| 28 | + min_index = u |
| 29 | + |
| 30 | + return min_index |
| 31 | + |
| 32 | + # Function that implements Dijkstra's single source |
| 33 | + # shortest path algorithm for a graph represented |
| 34 | + # using adjacency matrix representation |
| 35 | + def dijkstra(self, src): |
| 36 | + |
| 37 | + dist = [sys.maxsize] * self.V |
| 38 | + dist[src] = 0 |
| 39 | + sptSet = [False] * self.V |
| 40 | + |
| 41 | + for cout in range(self.V): |
| 42 | + |
| 43 | + # Pick the minimum distance vertex from |
| 44 | + # the set of vertices not yet processed. |
| 45 | + # x is always equal to src in first iteration |
| 46 | + x = self.minDistance(dist, sptSet) |
| 47 | + |
| 48 | + # Put the minimum distance vertex in the |
| 49 | + # shortest path tree |
| 50 | + sptSet[x] = True |
| 51 | + |
| 52 | + # Update dist value of the adjacent vertices |
| 53 | + # of the picked vertex only if the current |
| 54 | + # distance is greater than new distance and |
| 55 | + # the vertex in not in the shortest path tree |
| 56 | + for y in range(self.V): |
| 57 | + if self.graph[x][y] > 0 and sptSet[y] == False and \ |
| 58 | + dist[y] > dist[x] + self.graph[x][y]: |
| 59 | + dist[y] = dist[x] + self.graph[x][y] |
| 60 | + |
| 61 | + self.printSolution(dist) |
| 62 | + |
| 63 | +# Driver program |
| 64 | +v = int(input("Enter total vertices in weighted graph : ")) |
| 65 | +g = Graph(v) |
| 66 | +print("By default no vertex is connected to any vertex.If u want to connect any vertex to another then enter the index of both vertex one by one and then weight between them\nNote:Vertex index start from 0") |
| 67 | + |
| 68 | +while 1: |
| 69 | + a = int(input("Enter first vertex:")) |
| 70 | + b = int(input("Enter another vertex:")) |
| 71 | + w =int(input("Enter weight:")) |
| 72 | + g.graph[a][b] = g.graph[b][a] = w |
| 73 | + c = input("Wanna connect another Y/N :") |
| 74 | + if(c == "N" or c == "n"): |
| 75 | + break |
| 76 | + |
| 77 | +print("Graph :\n" , g.graph) |
| 78 | + |
| 79 | +""" |
| 80 | +g.graph = [[0, 4, 0, 0, 0, 0, 0, 8, 0], |
| 81 | + [4, 0, 8, 0, 0, 0, 0, 11, 0], |
| 82 | + [0, 8, 0, 7, 0, 4, 0, 0, 2], |
| 83 | + [0, 0, 7, 0, 9, 14, 0, 0, 0], |
| 84 | + [0, 0, 0, 9, 0, 10, 0, 0, 0], |
| 85 | + [0, 0, 4, 14, 10, 0, 2, 0, 0], |
| 86 | + [0, 0, 0, 0, 0, 2, 0, 1, 6], |
| 87 | + [8, 11, 0, 0, 0, 0, 1, 0, 7], |
| 88 | + [0, 0, 2, 0, 0, 0, 6, 7, 0] |
| 89 | + ]; |
| 90 | +""" |
| 91 | +print("above 2D array represent graph with row and columns index as connecting vertices and value as weight.0 value means that that vetices not connected.") |
| 92 | +s = int(input("Enter source vertex: ")) |
| 93 | + |
| 94 | +g.dijkstra(a) |
0 commit comments