The Control Plane: Route Calculation - Dijkstra's
In this lesson, we'll study Dijkstra's shortest path algorithm!
Phase II: Route Calculation
Each router then computes the spanning tree rooted at itself and calculates the entries in the routing table by using Dijkstra’s shortest path algorithm. Dijkstra’s is a common algorithm that is usually taught in Algorithms or Data Structures classes. Let’s get a quick refresher of it.
Dijkstra’s Algorithm
The goal is to find the shortest path from an initial node to all other nodes in the graph.
We first need to set up some data structures for us to use throughout the algorithm.
- Create a set called the unvisited set. All the nodes are initially unvisited.
- Create a set called the visited set. It’s initially empty.
- Create a list called the parent list. It will contain mappings of nodes to their parents.
- Lastly, every node has a distance of it from the initial node. Initially, all the nodes besides the initial node itself have a starting distance of infinity. We call this
d_node_n
, - Every link between two nodes in the graph has a certain weight. We call this
w_node_n_node_m
.
Algorithm
- Start with the initial node in the graph. Mark it as the current node.
- Consider each of its neighbor’s that are NOT in the visited set.
- If the sum of the distance of the current node and the distance to the neighbor from the current node is lower than the current distance of the neighbor, replace it with the new distance.
- In other words, if
w_node_curr_node_n
+d_node_curr
<d_node_n
, setd_node_n
tow_node_curr_node_n
+d_node_curr
. - Also, set the parent of this neighbor,
n
, to the current node.
- In other words, if
- Repeat step 3 for all unvisited neighbors. After that, add the current node to the visited set.
- Repeat steps 2-4 for the neighbor with the lowest
d_node_n
. Continue until the entire graph is visited.
Finding the Shortest Path
To find the shortest path from a given node n to the initial node:
- Find the parent of the current node. Initially the current node is n.
- Set the current node to the new parent node.
- Store each ‘current node’ in a stack.
- Repeat steps 1-3 until the initial node is reached.
- Pop and print the contents of the stack until it is empty.
Visual Example
Have a look at the following example to see how Dijkstra’s would apply to a graph.
Feel free to ask any questions related to the lesson in the following widget. Our AI will answer them and help you better understand the topic
Quick Quiz!
What is the aim of Dijkstra’s Algorithm?
To find the shortest paths from every node to every other node in a given graph
To find the most efficient paths from every node to every other node in a given graph
To find the most efficient paths from a node in a given graph to every other node in the graph
In the next lesson, you’ll implement Dijkstra’s Algorithm!
Get hands-on with 1400+ tech skills courses.