0% found this document useful (0 votes)
7 views3 pages

AOA Exp6

The document contains a C program implementing Dijkstra's algorithm to find the shortest path in a graph represented by an adjacency matrix. It prompts the user to input the number of vertices, the adjacency matrix, and the starting node, then calculates and displays the shortest distance and path from the starting node to all other nodes. The program uses arrays to store distances, predecessors, and visited nodes during the computation.

Uploaded by

imtiyazk78694
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)
7 views3 pages

AOA Exp6

The document contains a C program implementing Dijkstra's algorithm to find the shortest path in a graph represented by an adjacency matrix. It prompts the user to input the number of vertices, the adjacency matrix, and the starting node, then calculates and displays the shortest distance and path from the starting node to all other nodes. The program uses arrays to store distances, predecessors, and visited nodes during the computation.

Uploaded by

imtiyazk78694
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/ 3

Name: Behera Archita Kumari Prasanta

Roll no.: 04 Div: A


Batch: A1

Program:

#include<stdio.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;

//pred[] stores the predecessor of each node


//count gives the number of nodes seen so far
//create the cost matrix
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];
Name: Behera Archita Kumari Prasanta
Roll no.: 04 Div: A
Batch: A1

//initialize pred[],distance[] and visited[]


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;

//nextnode gives the node at minimum distance


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

//check if a better path exists through nextnode


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++;
}

//print the path and distance of each node


for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
Name: Behera Archita Kumari Prasanta
Roll no.: 04 Div: A
Batch: A1

j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}

Output:
Enter no. of vertices:4

Enter the adjacency matrix:


0 5 0 10
5030
0301
10 0 1 0

Enter the starting node:0

Distance of node1=5
Path=1<-0
Distance of node2=8
Path=2<-1<-0
Distance of node3=9
Path=3<-2<-1<-0

You might also like