0% found this document useful (0 votes)
14 views

Dsa 9

The document describes Dijkstra's algorithm for finding the single source shortest path in a graph. It includes the algorithm, a C program to implement Dijkstra's algorithm, and sample input and output. The program takes a graph as an adjacency matrix, a starting node, and finds the shortest path from the starting node to all other nodes by iteratively finding the unvisited node with the minimum distance. It outputs the distance and path for each node from the starting node.

Uploaded by

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

Dsa 9

The document describes Dijkstra's algorithm for finding the single source shortest path in a graph. It includes the algorithm, a C program to implement Dijkstra's algorithm, and sample input and output. The program takes a graph as an adjacency matrix, a starting node, and finds the shortest path from the starting node to all other nodes by iteratively finding the unvisited node with the minimum distance. It outputs the distance and path for each node from the starting node.

Uploaded by

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

IT22311 –DATA STRUCTURES AND ALGORITHM LABORATORY

EX.NO:9
DATE:

QUESTION: Implementation of Dijkstra’s algorithm for single source shortest


path problem using c.

AIM:

ALGORITHM:

Roll Number:2127220801051 Page No.:


PROGRAM:
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10

void dijkstra(int G[MAX][MAX],int n,int startnode);

int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}

void dijkstra(int G[MAX][MAX],int n,int startnode)


{

int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;

for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];

Roll Number:2127220801051 Page No.:


for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;

for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}

visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}

for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);

Roll Number:2127220801051 Page No.:


}
}

SAMPLE INPUT AND OUTPUT:

RESULT:

Roll Number:2127220801051 Page No.:

You might also like