Dijikrstra's Algo  

Posted in

#include"stdio.h"
#define INFINITY 999

void dijkstra(int wrt[10][10],int n,int source,int distance[10])
{
int visited[10],min,u,i,j;
for(i=1;i<=n;i++)
{
distance[i]=wrt[source][i];
visited[i]=0;
}
visited[source]=1;
for(i=1;i<=n;i++)
{
min=INFINITY;
u=-1;
for(j=1;j<=n;j++)
if(visited[j]==0 && distance[j] {
min=distance[j];
u=j;
}
visited[u]=1;
for(j=1;j<=n;j++)
if(visited[j]==0 && distance[u]+wrt[u][j] {
distance[j]=distance[u]+wrt[u][j];
}
}
}

int main()
{
int wrt[10][10],n,source,distance[10];
int i,j,sum;
printf("enter the nodes\n");
scanf("%d",&n);
printf("write matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&wrt[i][j]);
printf("enter the source\n");
scanf("%d\n",&source);
dijkstra(wrt,n,source,distance);
for(i=1;i<=n;i++)
if(distance[i]!=999)
printf("the shortest distance from %d to %d is %d\n",source,i,distance[i]);
else
printf("not reachable\n");
}

This entry was posted at 10:22 AM and is filed under . You can follow any responses to this entry through the comments feed .

0 comments

Post a Comment

Reverse Engineering :Subscribe Now

Zts - ZTS

DISCLAIMER

This Blog is ONLY for educational purposes,
and any such codes/snippets provided are to be executed on
your sole discretion. The author is not responsible for the codes.