Computer Science and Engineering: Course Title: Algorithms Lab
Computer Science and Engineering: Course Title: Algorithms Lab
Objective:
we will discuss another representation of Graph, Adjacency Matrix and
use this representation to find the shortest path in a weighted graph using
Dijkstra’s algorithm.
Introduction:
Algorithm:
For example:
Create a set sptSet (shortest path tree set) that keeps
track of vertices included in shortest path tree, i.e.,
whose minimum distance from source is
calculatedand finalized. Initially, this set is empty.
2) Assign a distance value to all vertices in the input
graph. Initialize all distance values as INFINITE.
Assign distance value as 0 for the source vertex so that
it is picked first.
3) While sptSet doesn’t include all vertices
a) Pick a vertex u which is not there in sptSet and has
minimum distance value.
….b) Include u to sptSet.
….c) Update distance value of all adjacent vertices of
u. To update the distance values, iterate through all
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 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. 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.
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).
We repeat the above steps until sptSet does include all vertices of given
graph. Finally, we get the following Shortest Path Tree (SPT).
Implementation:
Remark:
Discussion: