0% found this document useful (0 votes)
43 views9 pages

Computer Science and Engineering: Course Title: Algorithms Lab

The document discusses implementing Dijkstra's algorithm to find the shortest paths in a weighted graph using Java. It explains Dijkstra's algorithm, provides pseudocode, and includes an example to illustrate the steps of the algorithm.

Uploaded by

pubji gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views9 pages

Computer Science and Engineering: Course Title: Algorithms Lab

The document discusses implementing Dijkstra's algorithm to find the shortest paths in a weighted graph using Java. It explains Dijkstra's algorithm, provides pseudocode, and includes an example to illustrate the steps of the algorithm.

Uploaded by

pubji gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Computer Science and Engineering

Course Title: Algorithms Lab


Course Code: CSE 206
Experiment No: 04
Experiment Name: Graphs and Graphs Algorithms

1. Implementation of Dijkstra Algorithm.

Submitted by: Submitted to:

Name: Ujjal Kumar Roy TANPIA TASNIM


ID: 191002326 Lecturer
Section: DF

Department of CSE Department of CSE


Green University of Bangladesh Green University of Bangladesh

Date of Submission: 21-12-2020

©Ujjal Kumar Roy Page. 1


Implementation of Dijkstra Algorithm

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:

Dijkstra’s algorithm is a one of the most known algorithms for finding


the shortest paths between nodes in a graph. This algorithm is applied in
a lot of domains. For example, once you have represented road networks
in a graph, it becomes easy to calculate shortest paths inside this graph
by applying Dijkstra’s algorithm.

Algorithm:

©Ujjal Kumar Roy Page. 2


Dijkstra's algorithm works like this:

• We have a weighted graph G with a set of vertices (nodes) V and a


set of edges E
• We also have a starting node called s, and we set the distance
between s and s to 0
• Mark the distance between s and every other node as infinite, i.e.
start the algorithm as if no node was reachable from node s
• Mark all nodes (other than s) as unvisited, or mark s as visited if all
other nodes are already marked as unvisited (which is the approach
we'll use)
• As long as there is an unvisited node, do the following:
o Find the node n that has the shortest distance from the starting
node s
o Mark n as visited
o For every edge between n and m, where m is unvisited:
▪ If cheapestPath(s,n) + cheapestPath(n,m) < cheapestPath
(s,m), update the cheapest path between s and m to
equal cheapestPath(s,n) + cheapestPath(n,m)

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

©Ujjal Kumar Roy Page. 3


adjacent vertices. For every adjacent vertex v, if sum
of distance value of u (from source) and weight of
edge u-v, is less than the distance value of v, then
update the distance value of v.

Let us understand with the following example:

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.

©Ujjal Kumar Roy Page. 4


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.

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).

©Ujjal Kumar Roy Page. 5


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 does include all vertices of given
graph. Finally, we get the following Shortest Path Tree (SPT).

Implementation:

©Ujjal Kumar Roy Page. 6


©Ujjal Kumar Roy Page. 7
Output:

Remark:

I am successfully implementing this problem in my java compiler. I


have nothing to face any problem when I coded this problem in my java
compiler.

Discussion:

we discussed about how we can implement Dijkstra algorithm in Java to


find the shortest paths between nodes in a graph.

©Ujjal Kumar Roy Page. 8


©Ujjal Kumar Roy Page. 9

You might also like