Solution Review: Implementing Dijkstra's
Explore the implementation of Dijkstra's algorithm for calculating shortest paths in network routing. Understand how to set up variables, update distances, and construct shortest paths efficiently. Learn two solution approaches including an optimized one that stops when the destination route is found. This lesson helps you grasp network layer routing techniques essential for software engineers.
We'll cover the following...
Solution #1: Calculating All Paths
Explanation
Let’s go through this code line by line.
- Lines 1-9: we set up a few variables that are important for the implementation.
- The
number_of_nodesis the number of nodes in the graph. It’s equivalent to the number of rows/columns of the givengraph. This variable is not necessary for the algorithm itself, but makes calculating other variables clear and easy. - The
parentlist will map each node to its ‘parent’ or the previous node in the shortest path to the source node. Initialized to-1. - The
visitedlist is initially empty. - The
unvisitedlist contains all the nodes in the graph. Since the nodes in our graph are labeled by numbers, this list is simple to generate. - The
distancelist has all the current distances of all the nodes from thesrcnode. Note that all the distances besides the distance of thesrcnode from itself are set to infinity, i.e.,16. - The
shortest_pathis initialized to an empty list. - The
currentnode is set to thesrcnode since we are calculating the shortest paths fromsrcto the rest of the nodes.
- The
- Lines 11-24: The
whileloop.- We iterate through all of the neighbors of every
currentnode. - If the current distance for a node is greater than the distance of the
currentnode + the cost of the link between them, its distance is updated and itsparentis changed tocurrent. - Once all of the neighbors of a node have been exhausted, the next
currentnode is chosen to be an unvisited node with the smallestdistance. - Steps 1-3 are repeated until the while loop exits when no more nodes are left to visit.
- We iterate through all of the neighbors of every
- Lines 26-34: Calculating the shortest path and the cost. We traverse the
parentlist link by link until a path is generated. Since we start from the destination, the path has to be reversed. While we calculate the path, we also sum up the cost of each link that is traversed.- Example: Assume the source node is
0. IFparent[2]has the value1, the previous node from2is1as part of the shortest path to a source. Then, supposeparent[1]is0. So the shortest path will turn out to be[2, 1, 0].
- Example: Assume the source node is
Solution #2: Stopping When Route to dst is Calculated
Explanation
This solution differs from the previous one because it exits the while loop as soon as the destination node is visited via the if condition on lines 18 and 19. Since subsequent calculations for the rest of the graph do not change the shortest path to the destination node, there is no need to visit all of them.
In the next lesson, we’ll learn about the Internet protocol!