Operation of Algorithm
The following sequence of diagrams shows the operation of Dijkstra's Algorithm. The bold vertices show the vertex to which shortest path has been find out.
Step 1:
Initialize the graph, all of the vertices have infinite costs except the source vertex that has zero cost
![1831_Operation of Algorithm.png](https://www.expertsmind.com/CMSImages/1831_Operation%20of%20Algorithm.png)
Step 2
From all the adjacent vertices, select the closest vertex to the source s.
As we initialized d[s] through 0, it's s. (illustrated in bold circle)
Add it to S
Relax all vertices adjacent to s, that means u & x
Update vertices u & x by 10 & 5 as the distance from s.
![1932_Operation of Algorithm1.png](https://www.expertsmind.com/CMSImages/1932_Operation%20of%20Algorithm1.png)
Step 3:
Select the nearest vertex, x. Relax all of vertices adjacent to x
Update predecessors for u, v & y. Predecessor of x = s
Predecessor of v = x ,s
Predecessor of y = x ,s
add x to S
![248_Operation of Algorithm2.png](https://www.expertsmind.com/CMSImages/248_Operation%20of%20Algorithm2.png)
Step 4:
Now y is the closest vertex. Add it to S.
Relax v and adjust its predecessor.
![651_Operation of Algorithm3.png](https://www.expertsmind.com/CMSImages/651_Operation%20of%20Algorithm3.png)
Step 5:
u is now closest, add it to S and adjust its adjacent vertex, v.
![724_Operation of Algorithm3.png](https://www.expertsmind.com/CMSImages/724_Operation%20of%20Algorithm3.png)
Step 6:
Finally, add v to S.
The predecessor list now defines the shortest path from each node to s.