Dijkstra\'s Algorithm: Explanation and Implementation with C++ program
belongs to collection: Data Structure programs using C and C++ (Sorting Programs)
All Answers
total answers (1)
belongs to collection: Data Structure programs using C and C++ (Sorting Programs)
total answers (1)
Algorithm:
1. Initially Dset contains src
dist[s]=0 dist[v]= ∞
2. Set Dset to initially empty
3. While all the elements in the graph are not added to 'Dset'
How about we understand this with the help of an example:
Initially Dset is empty and the distance of all the vertices is set to infinity except the source which is set to zero. First we find the vertex with minimum distance. The vertex ‘A’ got picked as it is the source so update Dset for A.
Now update the distance of its adjacent vertices which B and C. Now find the vertex whose distance is minimum which is C.
So update Dset for C and update the distance of its adjacent vertices. Find the vertex with minimum distance which is B.
Update Dset for B and update distance of its adjacent vertices and then find the vertex with minimum distance which is G.
Update Dset for G and update distance of its adjacent vertices and find the vertex with minimum distance which is E.
Update Dset for E and update distance of its adjacent vertices and find the vertex with minimum distance which is F.
Update Dset for F and update distance of its adjacent vertices and find the vertex with minimum distance which is D.
Update Dset for D.
As all the vertices are part of Dset so we got the shortest path which contains all the vertices. So the graph for shortest path from vertex A is
Consider the program:
Output