Dijkstra’s Algorithm
Learn how Dijkstra’s algorithm works.
In this lesson, we look at another algorithm for solving the single-source shortest-paths problem called Dijkstra’s algorithm. Dijkstra’s algorithm is the preferred algorithm for finding the shortest paths from a single source because it also works for digraphs that have cycles. However, it does fail to work for digraphs with negative edge weights. The principal idea behind Dijkstra’s algorithm appears, in its rudimentary form, in a 1959
Main idea
Given a weighted digraph , the algorithm works so that throughout its execution, vertices of remain conceptually partitioned into two sets and its complement , as shown in the following figure.
Initially, the set is empty so that all vertices of are in its complement . In each round, we repeat the following steps:
-
We pick a vertex in with the smallest value (amongst all vertices in ) and add it to .
Note: The first vertex added to M is the source vertex since it’s the only vertex whose attribute is initialized to instead of .
-
With each vertex added to , we perform an edge-relaxation for all the edges originating at that vertex.
Once all vertices are added to the set , we consider the lengths of the shortest paths and the parent pointers correctly computed in the and attributes for each vertex .
Visualization
We give a simulation of how Dijkstra’s algorithm would work on a digraph. In the following slides, attributes of each vertex added to
Choice of data structure
To implement the above description, we can keep the vertices of