Dijikstra Algorithm
Dijikstra Algorithm
paths from the source to all the other vertices in the given graph.
Note: The given graph does not contain any negative edge.
Examples:
Input: src = 0, the graph is shown below.
Output: 0 4 12 19 21 11 9 8 14
Explanation: The distance from 0 to 1 = 4.
The minimum distance from 0 to 2 = 12. 0->1->2
The minimum distance from 0 to 3 = 19. 0->1->2->3
The minimum distance from 0 to 4 = 21. 0->7->6->5->4
The minimum distance from 0 to 5 = 11. 0->7->6->5
The minimum distance from 0 to 6 = 9. 0->7->6
The minimum distance from 0 to 7 = 8. 0->7
The minimum distance from 0 to 8 = 14. 0->1->2->8
Step 1:
The set sptSet is initially empty and distances assigned to vertices are {0,
INF, INF, INF, INF, INF, INF, INF} where INF indicates infinite.
Now pick the vertex with a minimum distance value. The vertex 0 is
picked, include it in sptSet . So sptSet becomes {0}. After including 0
to sptSet , update distance values of its adjacent vertices.
Adjacent vertices of 0 are 1 and 7. The distance values of 1 and 7 are
updated as 4 and 8.
The following subgraph shows vertices and their distance values, only the
vertices with finite distance values are shown. The vertices included
in SPT are shown in green colour.
Step 2:
Pick the vertex with minimum distance value and not already included
in SPT (not in sptSET ). The vertex 1 is picked and added to sptSet .
So sptSet now becomes {0, 1}. Update the distance values of adjacent
vertices of 1.
The distance value of vertex 2 becomes 12 .
Step 3:
Pick the vertex with minimum distance value and not already included
in SPT (not in sptSET ). Vertex 7 is picked. So sptSet now becomes {0, 1,
7}.
Update the distance values of adjacent vertices of 7. The distance value
of vertex 6 and 8 becomes finite ( 15 and 9 respectively).
Step 4:
Pick the vertex with minimum distance value and not already included
in SPT (not in sptSET ). Vertex 6 is picked. So sptSet now becomes {0, 1,
7, 6} .
Update the distance values of adjacent vertices of 6. The distance value
of vertex 5 and 8 are updated.
We repeat the above steps until sptSet includes all vertices of the given
graph. Finally, we get the following S hortest Path Tree (SPT).
return min_index;
}
// driver's code
int main()
{
// Function call
dijkstra(graph, 0);
return 0;
}
Output
Vertex Distance from Source
0 0
1 4
2 12
3 19
4 21
5 11
6 9
7 8
8 14
Time Complexity: O(V 2 )
Auxiliary Space: O(V)
Notes:
The code calculates the shortest distance but doesn’t calculate the path
information. Create a parent array, update the parent array when distance
is updated and use it to show the shortest path from source to different
vertices.
The time Complexity of the implementation is O(V 2 ) . If the input graph is
represented using adjacency list , it can be reduced to O(E * log V) with
the help of a binary heap. Please see Dijkstra’s Algorithm for Adjacency
List Representation for more details.
Dijkstra’s algorithm doesn’t work for graphs with negative weight cycles