#include<stdio.h>
#include<conio.h>
#define IN 9999
#define MAX 10
void dj(int G[MAX][MAX],int n,int startnode,int dst);
void main()
{
int j,i,node,con[10][10],s,d;
clrscr();
printf("Enter No of Nodes::");
scanf("%d",&node);
for(i=0;i<node;i++)
{
for(j=0;j<node;j++)
{
printf("Node %d is conneted with node %d::",i,j);
a:
scanf("%d",&con[i][j]);
if(con[i][j]!=0 && con[i][j]!=1)
{
goto a;
}
}
}
printf("MATRIX:");
for(i=0;i<node;i++)
{
printf("\n");
for(j=0;j<node;j++)
{
printf("%d ",con[i][j]);
}
}
printf("\nENTER SOURCE & DESTINATION::");
scanf("%d%d",&s,&d);
dj(con,node,s,d);
}
void dj(int G[MAX][MAX],int n,int startnode,int dst)
{
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]=IN;
else
cost[i][j]=G[i][j];
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=IN;
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 && i==dst)
{
printf("\nShortest path of node %d from %d:::",i,startnode,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
#include<conio.h>
#define IN 9999
#define MAX 10
void dj(int G[MAX][MAX],int n,int startnode,int dst);
void main()
{
int j,i,node,con[10][10],s,d;
clrscr();
printf("Enter No of Nodes::");
scanf("%d",&node);
for(i=0;i<node;i++)
{
for(j=0;j<node;j++)
{
printf("Node %d is conneted with node %d::",i,j);
a:
scanf("%d",&con[i][j]);
if(con[i][j]!=0 && con[i][j]!=1)
{
goto a;
}
}
}
printf("MATRIX:");
for(i=0;i<node;i++)
{
printf("\n");
for(j=0;j<node;j++)
{
printf("%d ",con[i][j]);
}
}
printf("\nENTER SOURCE & DESTINATION::");
scanf("%d%d",&s,&d);
dj(con,node,s,d);
}
void dj(int G[MAX][MAX],int n,int startnode,int dst)
{
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]=IN;
else
cost[i][j]=G[i][j];
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=IN;
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 && i==dst)
{
printf("\nShortest path of node %d from %d:::",i,startnode,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
Comments
Post a Comment
Thank you