Shortest Path Problem in Graph
Last updated
Was this helpful?
Last updated
Was this helpful?
Given a graph and start/end point, finding the shortest path(evaluating the edge's weight as distance) is called Shortest Path Problem
.
Dijkstran algorithm is used when graph doesn't have any negative edges.
We use two hashtables:
"shortest-length" hashtable to store the shortest distance from starting point to each nodes.
"is-visited" hashtable to store if the node is visited.
Pseudo code for Dijkstra algorithm is as follows:
Bellman-Ford algorithm can be applied to graphs that has negative edges, and also verify if there are negative-loop.
We use a single hashtable:
"shortest-length" hash that stores the shortest distance from starting point to each nodes.
Pseudo code for Dijkstra algorithm is as follows: