|
| 1 | +# Copyright (C) Deepali Srivastava - All Rights Reserved |
| 2 | +# This code is part of DSA course available on CourseGalaxy.com |
| 3 | + |
| 4 | +from sys import maxsize as INFINITY |
| 5 | + |
| 6 | +TEMPORARY = 1 |
| 7 | +PERMANENT = 2 |
| 8 | + |
| 9 | +class Vertex: |
| 10 | + def __init__(self, name): |
| 11 | + self.name = name |
| 12 | + |
| 13 | + |
| 14 | +class DirectedWeightedGraph: |
| 15 | + |
| 16 | + def __init__(self, size=20): |
| 17 | + self._adj = [ [0 for column in range(size)] for row in range(size) ] |
| 18 | + self._n = 0 |
| 19 | + self._vertexList = [] |
| 20 | + |
| 21 | + |
| 22 | + def display(self): |
| 23 | + for i in range(self._n): |
| 24 | + for j in range(self._n): |
| 25 | + print( self._adj[i][j], end =' ') |
| 26 | + print() |
| 27 | + |
| 28 | + |
| 29 | + def numVertices(self): |
| 30 | + return self._n |
| 31 | + |
| 32 | + |
| 33 | + def numEdges(self): |
| 34 | + e = 0 |
| 35 | + for i in range(self._n): |
| 36 | + for j in range(self._n): |
| 37 | + if self._adj[i][j] !=0 : |
| 38 | + e+=1 |
| 39 | + return e |
| 40 | + |
| 41 | + |
| 42 | + def vertices(self): |
| 43 | + return [vertex.name for vertex in self._vertexList] |
| 44 | + |
| 45 | + |
| 46 | + def edges(self): |
| 47 | + edges = [] |
| 48 | + for i in range(self._n): |
| 49 | + for j in range(self._n): |
| 50 | + if self._adj[i][j] != 0: |
| 51 | + edges.append( (self._vertexList[i].name, self._vertexList[j].name, self._adj[i][j]) ) |
| 52 | + return edges |
| 53 | + |
| 54 | + |
| 55 | + def _getIndex(self,s): |
| 56 | + index = 0 |
| 57 | + for name in (vertex.name for vertex in self._vertexList): |
| 58 | + if s == name: |
| 59 | + return index |
| 60 | + index += 1 |
| 61 | + return None |
| 62 | + |
| 63 | + |
| 64 | + def insertVertex(self,name): |
| 65 | + if name in (vertex.name for vertex in self._vertexList): ######## |
| 66 | + print("Vertex with this name already present in the graph") |
| 67 | + return |
| 68 | + |
| 69 | + self._vertexList.append( Vertex(name) ) |
| 70 | + self._n += 1 |
| 71 | + |
| 72 | + |
| 73 | + def removeVertex(self,name): |
| 74 | + u = self._getIndex(name) |
| 75 | + if u is None: |
| 76 | + print("Vertex not present in the graph") |
| 77 | + return |
| 78 | + |
| 79 | + self._adj.pop(u) |
| 80 | + |
| 81 | + for i in range(self._n): |
| 82 | + self._adj[i].pop(u) |
| 83 | + |
| 84 | + self._vertexList.pop(u) |
| 85 | + self._n -= 1 |
| 86 | + |
| 87 | + |
| 88 | + def insertEdge(self, s1, s2, w): |
| 89 | + u = self._getIndex(s1) |
| 90 | + v = self._getIndex(s2) |
| 91 | + if u is None: |
| 92 | + print("Start vertex not present in the graph, first insert the start vertex") |
| 93 | + elif v is None: |
| 94 | + print("End vertex not present in the graph, first insert the end vertex") |
| 95 | + elif u == v: |
| 96 | + print("Not a valid edge") |
| 97 | + elif self._adj[u][v] != 0 : |
| 98 | + print("Edge already present in the graph") |
| 99 | + else: |
| 100 | + self._adj[u][v] = w |
| 101 | + |
| 102 | + |
| 103 | + def removeEdge(self, s1, s2): |
| 104 | + u = self._getIndex(s1) |
| 105 | + v = self._getIndex(s2) |
| 106 | + |
| 107 | + if u is None: |
| 108 | + print("Start vertex not present in the graph ") |
| 109 | + elif v is None: |
| 110 | + print("End vertex not present in the graph") |
| 111 | + elif self._adj[u][v] == 0: |
| 112 | + print("Edge not present in the graph") |
| 113 | + else: |
| 114 | + self._adj[u][v] = 0 |
| 115 | + |
| 116 | + |
| 117 | + def isAdjacent(self, s1, s2): |
| 118 | + u = self._getIndex(s1) |
| 119 | + v = self._getIndex(s2) |
| 120 | + if u is None: |
| 121 | + print("Start vertex not present in the graph") |
| 122 | + return False |
| 123 | + elif v is None: |
| 124 | + print("End vertex not present in the graph") |
| 125 | + return False |
| 126 | + return False if self._adj[u][v] == 0 else True |
| 127 | + |
| 128 | + |
| 129 | + def outdegree(self, s): |
| 130 | + u = self._getIndex(s) |
| 131 | + if u is None: |
| 132 | + print("Vertex not present in the graph") |
| 133 | + return |
| 134 | + outd = 0 |
| 135 | + for v in range(self._n): |
| 136 | + if self._adj[u][v] != 0 : |
| 137 | + outd+=1 |
| 138 | + return outd |
| 139 | + |
| 140 | + |
| 141 | + def indegree(self, s): |
| 142 | + u = self._getIndex(s) |
| 143 | + if u is None: |
| 144 | + print("Vertex not present in the graph") |
| 145 | + return |
| 146 | + ind = 0 |
| 147 | + |
| 148 | + for v in range(self._n): |
| 149 | + if self._adj[v][u] !=0 : |
| 150 | + ind+=1 |
| 151 | + return ind |
| 152 | + |
| 153 | + |
| 154 | + def _dijkstra(self, s): |
| 155 | + |
| 156 | + for v in range(self._n): |
| 157 | + self._vertexList[v].status = TEMPORARY |
| 158 | + self._vertexList[v].pathLength = INFINITY |
| 159 | + self._vertexList[v].predecessor = None |
| 160 | + |
| 161 | + |
| 162 | + self._vertexList[s].pathLength = 0 |
| 163 | + |
| 164 | + while True: |
| 165 | + c = self._tempVertexMinPL() |
| 166 | + |
| 167 | + if c == None: |
| 168 | + return |
| 169 | + |
| 170 | + self._vertexList[c].status = PERMANENT |
| 171 | + |
| 172 | + for v in range(self._n): |
| 173 | + if self._adj[c][v]!=0 and self._vertexList[v].status == TEMPORARY: |
| 174 | + if self._vertexList[c].pathLength + self._adj[c][v] < self._vertexList[v].pathLength: |
| 175 | + self._vertexList[v].predecessor = c |
| 176 | + self._vertexList[v].pathLength = self._vertexList[c].pathLength + self._adj[c][v] |
| 177 | + |
| 178 | + |
| 179 | + def _tempVertexMinPL(self): |
| 180 | + min = INFINITY |
| 181 | + x = None |
| 182 | + for v in range(self._n): |
| 183 | + if self._vertexList[v].status == TEMPORARY and self._vertexList[v].pathLength < min: |
| 184 | + min = self._vertexList[v].pathLength |
| 185 | + x = v |
| 186 | + return x |
| 187 | + |
| 188 | + |
| 189 | + def findPaths(self, source): |
| 190 | + |
| 191 | + s = self._getIndex(source) |
| 192 | + if s is None: |
| 193 | + print("Vertex not present in the graph") |
| 194 | + return |
| 195 | + |
| 196 | + self._dijkstra(s) |
| 197 | + |
| 198 | + print("Source Vertex :", source) |
| 199 | + |
| 200 | + for v in range(self._n): |
| 201 | + print("Destination Vertex :", self._vertexList[v].name) |
| 202 | + if self._vertexList[v].pathLength == INFINITY : |
| 203 | + print("There is no path from ", source , "to vertex", self._vertexList[v].name, "\n") |
| 204 | + else: |
| 205 | + self._findPath(s,v) |
| 206 | + |
| 207 | + |
| 208 | + def _findPath(self, s, v): |
| 209 | + path = [] |
| 210 | + sd = 0 |
| 211 | + count = 0 |
| 212 | + |
| 213 | + while v != s: |
| 214 | + count += 1 |
| 215 | + path.append(v) |
| 216 | + u = self._vertexList[v].predecessor |
| 217 | + sd += self._adj[u][v] |
| 218 | + v = u |
| 219 | + path.append(s) |
| 220 | + |
| 221 | + print("Shortest Path is : ", end = " ") |
| 222 | + for u in reversed(path): |
| 223 | + print(u, end = " ") |
| 224 | + print("\nShortest distance is :", sd, "\n") |
| 225 | + |
| 226 | + |
| 227 | +if __name__ == '__main__': |
| 228 | + |
| 229 | + g = DirectedWeightedGraph() |
| 230 | + |
| 231 | + g.insertVertex("Zero") |
| 232 | + g.insertVertex("One") |
| 233 | + g.insertVertex("Two") |
| 234 | + g.insertVertex("Three") |
| 235 | + g.insertVertex("Four") |
| 236 | + g.insertVertex("Five") |
| 237 | + g.insertVertex("Six") |
| 238 | + g.insertVertex("Seven") |
| 239 | + g.insertVertex("Eight") |
| 240 | + |
| 241 | + g.insertEdge("Zero", "Three", 2) |
| 242 | + g.insertEdge("Zero", "One", 5) |
| 243 | + g.insertEdge("Zero", "Four", 8) |
| 244 | + g.insertEdge("One", "Four", 2) |
| 245 | + g.insertEdge("Two", "One", 3) |
| 246 | + g.insertEdge("Two", "Five", 4) |
| 247 | + g.insertEdge("Three", "Four", 7) |
| 248 | + g.insertEdge("Three", "Six", 8) |
| 249 | + g.insertEdge("Four", "Five", 9) |
| 250 | + g.insertEdge("Four", "Seven", 4) |
| 251 | + g.insertEdge("Five", "One", 6) |
| 252 | + g.insertEdge("Six", "Seven", 9) |
| 253 | + g.insertEdge("Seven", "Three", 5) |
| 254 | + g.insertEdge("Seven", "Five", 3) |
| 255 | + g.insertEdge("Seven", "Eight", 5) |
| 256 | + g.insertEdge("Eight", "Five", 3) |
| 257 | + |
| 258 | + g.findPaths("Zero") |
| 259 | + |
| 260 | + |
0 commit comments