Program-05 (DIJKTRA'S ALGORITHM)
Program-05 (DIJKTRA'S ALGORITHM)
Subject Code:21CS42
Program-05
Dijkstra's Algorithm
Dijkstra algorithm is a single-source shortest path algorithm. Here, single-source means
that only one source is given, and we have to find the shortest path from the source to all
the nodes.
Let's understand the working of Dijkstra's algorithm. Consider the below graph.
First, we have to consider any vertex as a source vertex. Suppose we consider vertex 0 as
a source vertex.
Here we assume that 0 as a source vertex, and distance to all the other vertices is infinity.
Initially, we do not know the distances. First, we will find out the vertices which are directly
connected to the vertex 0. As we can observe in the above graph that two vertices are
directly connected to vertex 0.
Before proceeding the step by step process for implementing the algorithm,
let us consider some essential characteristics of Dijkstra’s algorithm;
In the above section, you have gained the step by step process of Dijkstra’s
algorithm, now let’s study the algorithm with an explained example.
We will calculate the shortest path between node C and the other nodes in the
graph.
1. During the execution of the algorithm, each node will be marked with its
minimum distance to node C as we have selected node C.
In this case, the minimum distance is 0 for node C. Also, for the rest of
the nodes, as we don’t know this distance, they will be marked as infinity
(∞), except node C (currently marked as red dot).
4. Now, we will select the new current node such that the node must be
unvisited with the lowest minimum distance, or the node with the least
number and no check mark. Here, node A is the unvisited with minimum
distance 1, marked as current node with red dot.
5. After this, node A marked as visited with a green check mark. The
current node is selected as node D, it is unvisited and has a smallest
recent distance. We repeat the algorithm and check for node B and E.
6. The current node is set as node B, here we need to check only node E as
it is unvisited and the node D is visited. We obtain 4+ 1=5, compare it
with the minimum distance of the node.
We mark D as visited node with a green check mark, and node E is set as
the current node.
So, we are done as no unvisited node is left. The minimum distance of each
node is now representing the minimum distance of that node from node C.
PROGRAM
package DAA_Programs;
import java.util.Scanner;
for(j=0;j<tot_nodes;j++) {
Dijkstra(tot_nodes,cost,i,dist);
if(dist[j]==999)
System.out.println("\n There is no Path to"+j);
else
{
display(i,j,dist);
}
}
}
public static void create(int tot_nodes,int cost[][]) {
int i,j,val,tot_edges,count=0;
for(i=0;i<tot_nodes;i++) {
for(j=0;j<tot_nodes;j++) {
if(i==j)
cost[i][j]=0;
else
cost[i][j]=999;
}
}
System.out.println("\n Total Number of Edges:");
Scanner in = new Scanner(System.in);
tot_edges=in.nextInt();
Search Creators… Page 14
21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS
while(count<tot_edges) {
System.out.println("\nEnter Vi and Vj:");
i=in.nextInt();
j=in.nextInt();
System.out.println("\nEnter the cost along this edge:");
val=in.nextInt();
cost[j][i]=val;
cost[i][j]=val;
count++;
}
}
public static void Dijkstra(int tot_nodes,int cost[][],int source,int dist[]) {
int i,j,v1,v2,min_dist;
int s[]=new int[10];
for(i=0;i<tot_nodes;i++) {
dist[i]=cost[source][i];
s[i]=0;
path[i]=source;
}
s[source]=1;
for(i=1;i<tot_nodes;i++) {
min_dist=999;
v1=-1;
for(j=0;j<tot_nodes;j++) {
if(s[j]==0) {
if(dist[j]<min_dist) {
Search Creators… Page 15
21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS
min_dist=dist[j];
v1=j;
}
}
}
s[v1]=1;
for(v2=0;v2<tot_nodes;v2++) {
if(s[v2]==0) {
if(dist[v1]+cost[v1][v2]<dist[v2]) {
dist[v2]=dist[v1]+cost[v1][v2];
path[v2]=v1;
}
}
}
}
}
public static void display(int source,int destination,int dist[]) {
int i;
System.out.println("\n Step By Step Shortest Path is...\n");
for(i=destination;i!=source;i=path[i]) {
System.out.println(i+"<-");
}
System.out.println(""+i);
System.out.println("The Length="+dist[destination]);
OUTPUT