MCPcopy Index your code
hub / github.com/subbarayudu-j/TheAlgorithms-Python / show_path

Method show_path

Graphs/dijkstra_algorithm.py:143–167  ·  view source on GitHub ↗
(self, src, dest)

Source from the content-addressed store, hash-verified

141 print('Node {} has distance: {}'.format(u, self.dist[u]))
142
143 def show_path(self, src, dest):
144 # To show the shortest path from src to dest
145 # WARNING: Use it *after* calling dijkstra
146 path = []
147 cost = 0
148 temp = dest
149 # Backtracking from dest to src
150 while self.par[temp] != -1:
151 path.append(temp)
152 if temp != src:
153 for v, w in self.adjList[temp]:
154 if v == self.par[temp]:
155 cost += w
156 break
157 temp = self.par[temp]
158 path.append(src)
159 path.reverse()
160
161 print('----Path to reach {} from {}----'.format(dest, src))
162 for u in path:
163 print('{}'.format(u), end=' ')
164 if u != dest:
165 print('-> ', end='')
166
167 print('\nTotal cost of path: ', cost)
168
169
170if __name__ == '__main__':

Callers 1

Calls 1

reverseMethod · 0.80

Tested by

no test coverage detected