DAA - Shortest Paths - Tutorialspoint
DAA - Shortest Paths - Tutorialspoint
Dijkstra’s Algorithm
Dijkstra’s algorithm solves the single-source shortest-paths problem on a directed weighted graph G = (V,
E), where all the edges are non-negative (i.e., w(u, v) ≥ 0 for each edge (u, v) Є E).
In the following algorithm, we will use one function Extract-Min(), which extracts the node with the
smallest key.
Analysis
The complexity of this algorithm is fully dependent on the implementation of Extract-Min function. If extract
min function is implemented using linear search, the complexity of this algorithm is O(V2 + E).
In this algorithm, if we use min-heap on which Extract-Min() function works to return the node from Q with
the smallest key, the complexity of this algorithm can be reduced further.
Example
Let us consider vertex 1 and 9 as the start and destination vertex respectively. Initially, all the vertices
except the start vertex are marked by ∞ and the start vertex is marked by 0.
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_shortest_paths.htm 1/5
9/23/2019 DAA - Shortest Paths - Tutorialspoint
Vertex Initial Step1 V11 Step2 V33 Step3 V22 Step4 V44 Step5 V55 Step6 V77 Step7 V88 Step8 V66
1 0 0 0 0 0 0 0 0 0
2 ∞ 5 4 4 4 4 4 4 4
3 ∞ 2 2 2 2 2 2 2 2
4 ∞ ∞ ∞ 7 7 7 7 7 7
5 ∞ ∞ ∞ 11 9 9 9 9 9
6 ∞ ∞ ∞ ∞ ∞ 17 17 16 16
7 ∞ ∞ 11 11 11 11 11 11 11
8 ∞ ∞ ∞ ∞ ∞ 16 13 13 13
9 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 20
Hence, the minimum distance of vertex 9 from vertex 1 is 20. And the path is
1→ 3→ 7→ 8→ 6→ 9
This algorithm solves the single source shortest path problem of a directed graph G = (V, E) in which the
edge weights may be negative. Moreover, this algorithm can be applied to find the shortest path, if there
does not exist any negative weighted cycle.
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_shortest_paths.htm 2/5
9/23/2019 DAA - Shortest Paths - Tutorialspoint
Analysis
The first for loop is used for initialization, which runs in O(V) times. The next for loop runs |V - 1| passes
over the edges, which takes O(E) times.
Hence, Bellman-Ford algorithm runs in O(V, E) time.
Example
The following example shows how Bellman-Ford algorithm works step by step. This graph has a negative
edge but does not have any negative cycle, hence the problem can be solved using this technique.
At the time of initialization, all the vertices except the source are marked by ∞ and the source is marked
by 0.
In the first step, all the vertices which are reachable from the source are updated by minimum cost.
Hence, vertices a and h are updated.
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_shortest_paths.htm 3/5
9/23/2019 DAA - Shortest Paths - Tutorialspoint
Following the same logic, in this step vertices b, f, c and g are updated.
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_shortest_paths.htm 4/5
9/23/2019 DAA - Shortest Paths - Tutorialspoint
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_shortest_paths.htm 5/5