//pgm no-05 BATCH NO-A4-50 USN-2BA11CS409
//program for distance vector algorithm to find shortest path
#include<stdio.h>
#include<stdlib.h>
struct node
{
unsigned int dist[20];
unsigned int from[20];
}rt[10];
int main()
{
int c[20][20],s,d,arr[20];
int n,i,j,k,count=0;
printf("Enter the number of nodes\n");
scanf("%d",&n);
printf("Enter the number for each nodes\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&c[i][j]);
rt[i].dist[j]=c[i][j];
rt[i].from[j]=j;;
}
for(i=0;i<n;i++)
{
printf("Routing information of router %d\n",i);
printf("source\tdest\tvia\tcost\n");
for(j=0;j<n;j++)
printf("%d\t%d\t%d\t%d\n",i,j,rt[i].from[j],rt[i].dist[j]);
}
do
{
count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>c[i][k]+rt[k].dist[j])
{
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}
while(count!=0);
printf("\n***** After updating*****\n");
for(i=0;i<n;i++)
{
printf("routing information of router %d\n",i);
printf("source\tdestination\tvia\tcost\n");
for(j=0;j<n;j++)
printf("\n%d\t%d\t%d\t%d\n",i,j,rt[i].from[j],rt[i].dist[j]);
}
printf("enter the source node\n");
scanf("%d",&s);
printf("enter the destination node\n");
scanf("%d",&d);
printf("The minimum cost from s to d is \n");
printf("%d",rt[s].dist[d]);
arr[0]=s,i=1;
while(s!=d)
{
arr[i]=rt[s].from[d];
s=rt[s].from[d];
i++;
}
printf("\nThe shortest path from s to d \n");
for(j=0;j<i-1;j++)
printf("%d -->",arr[j]);
printf("%d\n",arr[j]);
}
/************************OUTPUT**************************
[root123@localhost ~]$ cc DVA.c
[root123@localhost ~]$ ./a.out
Enter the number of nodes
5
Enter the number for each nodes
0 5 2 3 999
5 0 4 999 3
2 4 0 999 4
3 999 999 0 999
999 3 4 999 0
Routing information of router 0
source dest via cost
0 0 0 0
0 1 1 5
0 2 2 2
0 3 3 3
0 4 4 999
Routing information of router 1
source dest via cost
1 0 0 5
1 1 1 0
1 2 2 4
1 3 3 999
1 4 4 3
Routing information of router 2
source dest via cost
2 0 0 2
2 1 1 4
2 2 2 0
2 3 3 999
2 4 4 4
Routing information of router 3
source dest via cost
3 0 0 3
3 1 1 999
3 2 2 999
3 3 3 0
3 4 4 999
Routing information of router 4
source dest via cost
4 0 0 999
4 1 1 3
4 2 2 4
4 3 3 999
4 4 4 0
***** After updating*****
routing information of router 0
source destination via cost
0 0 0 0
0 1 1 5
0 2 2 2
0 3 3 3
0 4 2 6
routing information of router 1
source destination via cost
1 0 0 5
1 1 1 0
1 2 2 4
1 3 0 8
1 4 4 3
routing information of router 2
source destination via cost
2 0 0 2
2 1 1 4
2 2 2 0
2 3 0 5
2 4 4 4
routing information of router 3
source destination via cost
3 0 0 3
3 1 0 8
3 2 0 5
3 3 3 0
3 4 0 9
routing information of router 4
source destination via cost
4 0 2 6
4 1 1 3
4 2 2 4
4 3 2 9
4 4 4 0
enter the source node
0
enter the destination node
4
The minimum cost from s to d is
6
The shortest path from s to d
0 -->2 -->4 */
DVA.c.zip_distance
版权申诉
170 浏览量
2022-09-14
20:46:55
上传
评论
收藏 1KB ZIP 举报
alvarocfc
- 粉丝: 105
- 资源: 1万+
最新资源
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- 【ERP标准流程-标准流程-进货管理】(DOC 17页).doc
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- Java爬虫项目【项目开发计划】(共12页).docx
- 11111111111
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈