0% found this document useful (0 votes)
5 views20 pages

Program-05 (DIJKTRA'S ALGORITHM)

The document provides an overview of Dijkstra's algorithm, which is used to find the shortest paths from a single source vertex to all other vertices in a weighted connected graph. It outlines the step-by-step process for implementing the algorithm, including marking nodes as visited and updating distances. Additionally, it includes a Java program that demonstrates the algorithm's functionality through user input for graph creation and pathfinding.

Uploaded by

raviba1998
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)
5 views20 pages

Program-05 (DIJKTRA'S ALGORITHM)

The document provides an overview of Dijkstra's algorithm, which is used to find the shortest paths from a single source vertex to all other vertices in a weighted connected graph. It outlines the step-by-step process for implementing the algorithm, including marking nodes as visited and updating distances. Additionally, it includes a Java program that demonstrates the algorithm's functionality through user input for graph creation and pathfinding.

Uploaded by

raviba1998
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/ 20

21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Subject Code:21CS42

Subject :Design and Analysis of Algorithm

Program-05

5. To Find shortest paths to other vertices from a given vertex in a weighted


connected graph, using Dijkstra's algorithm.

Theory Concept : Dijikstra’s Algorithm

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.

Search Creators… Page 1


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

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.

How to Implement the Dijkstra Algorithm?

Before proceeding the step by step process for implementing the algorithm,
let us consider some essential characteristics of Dijkstra’s algorithm;

• Basically, the Dijkstra’s algorithm begins from the node to be selected,


the source node, and it examines the entire graph to determine the
shortest path among that node and all the other nodes in the graph.
• The algorithm maintains the track of the currently recognized shortest
distance from each node to the source code and updates these values if
it identifies another shortest path.
• Once the algorithm has determined the shortest path amid the source
code to another node, the node is marked as “visited” and can be added
to the path.
• This process is being continued till all the nodes in the graph have been
added to the path, as this way, a path gets created that connects the
source node to all the other nodes following the plausible shortest path
to reach each node.

Search Creators… Page 2


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

step by step process of algorithm implementation;

1. The very first step is to mark all nodes as unvisited,


2. Mark the picked starting node with a current distance of 0 and the rest
nodes with infinity,
3. Now, fix the starting node as the current node,
4. For the current node, analyse all of its unvisited neighbours and
measure their distances by adding the current distance of the current
node to the weight of the edge that connects the neighbour node and
current node,
5. Compare the recently measured distance with the current distance
assigned to the neighbouring node and make it as the new current
distance of the neighbouring node,
6. After that, consider all of the unvisited neighbours of the current node,
mark the current node as visited,
7. If the destination node has been marked visited then stop, an algorithm
has ended, and
8. Else, choose the unvisited node that is marked with the least distance,
fix it as the new current node, and repeat the process again from step
4.

Search Creators… Page 3


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Working Example 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.

Example of Dijkstra's Algorithm

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

Search Creators… Page 4


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Graphical Representation of Node C as Current Node

2. Now the neighbours of node C will be checked, i.e, node A, B, and D. We


start with B, here we will add the minimum distance of current node (0)
with the weight of the edge (7) that linked the node C to node B and get
0+ 7= 7.

Now, this value will be compared with the minimum distance of B


(infinity), the least value is the one that remains the minimum distance of
B, like in this case, 7 is less than infinity, and marks the least value to
node B.

Search Creators… Page 5


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Assign Node B a minimum distance value

3. Now, the same process is checked with neighbour A. We add 0 with 1


(weight of edge that connects node C to A), and get 1. Again, 1 is
compared with the minimum distance of A (infinity), and marks the
lowest value.

Search Creators… Page 6


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Assign Node A a minimum distance value

The same is repeated with node D, and marked 2 as lowest value at D.

Assign Node D a minimum distance value

Since, all the neighbours of node C have checked, so node C is marked


as visited with a green check mark.

Search Creators… Page 7


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Marked Node C as visited

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.

Graphical Representation of Node A as Current Node


Search Creators… Page 8
21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

We repeat the algorithm, checking the neighbour of the current node


while ignoring the visited node, so only node B will be checked.

For node B, we add 1 with 3 (weight of the edge connecting node A to B)


and obtain 4. This value, 4, will be compared with the minimum distance
of B, 7, and mark the lowest value at B as 4.

Assign Node B a minimum distance value

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.

Search Creators… Page 9


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Graphical Representation of Node D as Current Node

For node B, we add 2 to 5, get 7 and compare it with the minimum


distance value of B, since 7>4, so leave the smallest distance value at
node B as 4.

For node E, we obtain 2+ 7= 9, and compare it with the minimum


distance of E which is infinity, and mark the smallest value as node E as
9. The node D is marked as visited with a green check mark.

Search Creators… Page 10


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Marked Node D as visited

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.

As 9 > 5, leave the smallest value at node node E as 5.

We mark D as visited node with a green check mark, and node E is set as
the current node.

Search Creators… Page 11


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Marked Node B as visited

7. Since it doesn’t have any unvisited neighbours, so there is not any


requirement to check anything. Node E is marked as a visited node with
a green mark.

Marked Node E as visited

Search Creators… Page 12


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

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;

public class Dijkstras_Algorithm {


public static int path[] = new int[10];

public static void main(String[] args) {


int tot_nodes,i,j;
int cost[][]=new int[10][10];
int dist[]=new int[10];
int s[]=new int[10];
System.out.println("\n\t\t-------Creation of Graph-----");
System.out.println("\nEnter Total Number of Nodes:");
Scanner in = new Scanner(System.in);
tot_nodes=in.nextInt();
create(tot_nodes,cost);
for(i=0;i<tot_nodes;i++) {
System.out.println("\n\t\tPress any Key To Continue...");
System.out.println("\n\t\tWhen Source="+i);

Search Creators… Page 13


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

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]);

Search Creators… Page 16


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

OUTPUT

Search Creators… Page 17


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Search Creators… Page 18


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Search Creators… Page 19


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Search Creators… Page 20

You might also like