Dijkstra Algorithm
Dijkstra Algorithm
Topic:
Dijkstra Algorithm
Group Members:
Sabtain Ali (FA20-BCS-076)
Saifullah (FA20-BCS-077)
Muhammad Zain (FA20-BCS-069)
Muhammad Kamran (FA20-BCS-056)
What is Dijkstra Algorithm?
Dijkstra algorithm is a single-source shortest
path algorithm. Single-source means that only one source is given, and
we have to find the shortest path from the source to all the nodes.
Example:
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 color.
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 Shortest Path Tree (SPT).
Algorithm:
1. Mark the ending vertex with a distance of zero.
Designate this vertex as current.
2. Find all vertices leading to the current vertex. Calculate
their distances to the end. Since we already know the distance the
current vertex is from the end, this will just require adding the most
recent edge. Don’t record this distance if it is longer than a previously
recorded distance.
3. Mark the current vertex as visited. We will never look at
this vertex again.
4. Mark the vertex with the smallest distance as current,
and repeat from step 2.
Pseudo Code:
1) Create vertex set Q
2) for each vertex Q in graph
3) dist[v]= ∞
4) Add v to Q
5) dist[source]=O
6) while Q is not empty
7) u=Extract-min[Q]
8) for each neighbor v of u
9) Relax (u,v)
Advantages:-
Disadvantages:-