Return the route used to reach a target end node.
Implementation
This is implemented on the basis of a graph traversal algorithm (commonly BFS/DFS), or a shortest distance algorithm (such as Dijkstra’s algorithm), but using an extra variable to track the paths. Examples of two such implementations are listed below.
Dictionary of lists (intuitive)
Construct a dictionary
paths
where the key represents a node within the graph, andpaths[n]
represents the shortest path from the start node to noden
. You are responsible for updating the dictionary within the algorithm.
Dictionary of numbers (Dijkstras)
This approach is harder to understand but easier to implement.
Construct a dictionary
previous
where a key represents a node within the graph, and the corresponding value represents the previous node on the shortest path from the start node to node .Intuitive example:
previous[5]=2
means:
- the previous node of 5 is 2
- within the shortest path, it will always include .
We calculate the shortest path through a process called reconstruction. We follow the
previous
values backwards, from the end node back to the start node.For example, to reconstruct the path from node
0
to node7
: (made up values are used)
previous[7]=4
- shortest path:previous[4]=2
- shortest path:previous[2]=0
- shortest path: Start node0
is reached, hence the shortest path is .This is extremely easy to implement, especially within Dijkstras.