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

Sodapdf

The document implements Dijkstra's algorithm to find the shortest path between nodes in a graph. It includes a dijkstra function that takes a source node and adjacency matrix as input and calculates the shortest distances from the source to all other nodes.

Uploaded by

Balaji Koushik S
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)
35 views3 pages

Sodapdf

The document implements Dijkstra's algorithm to find the shortest path between nodes in a graph. It includes a dijkstra function that takes a source node and adjacency matrix as input and calculates the shortest distances from the source to all other nodes.

Uploaded by

Balaji Koushik S
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

3.

Implement Dijkstra’s algorithm to compute the shortest


routing path.

#include<stdio.h>
#include<conio.h>
void dijkstra(int src,int cost[][10],int dist[],int n)
{
int visited[10],min,i,j,u;
for(i=1;i<=n;i++)
{
visited[i]=0;
dist[i]=cost[src][i];

}
visited[src]=1,dist[src]=0;
for(i=1;i<=n;i++)
{
if(i==src)continue;
min=999;
for(j=1;j<=n;j++)
if((visited[j]==0) && (min>dist[j]))
{
min=dist[j];
u=j;
}
visited[u]=1;
for(j=1;j<=n;j++)
if(visited[j]==0)
{
if(dist[j]>dist[u]+cost[u][j])
dist[j]=dist[u]+cost[u][j];
}
}
}

void main()
{
int n,cost[10][10],dist[10]={0},i,j,src;
//clrscr();
printf("enter the no. of vertices\n");
scanf("%d",&n);
printf("enter the adjacency matrix of the graph with 999 for infinity \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
printf("enter the source vertex");
scanf("%d",&src);
dijkstra(src,cost,dist,n);
printf("the shortest paths from src %d to all other vertices are
\n",src);
for(i=1;i<=n;i++)
printf("%d to %d = %d\n",src,i,dist[i]);
//getch();
}

You might also like