반응형
//Node4 = Boston
//Which node is not neighbor cost = 9999
#include <stdio.h>
#pragma warning (disable:4996)
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int dmat[20][20];
int n, i, j, k, count = 0;
printf("\nEnter the number of nodes : ");
scanf("%d", &n);
printf("\nEnter the cost matrix :\n");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{
scanf("%d", &dmat[i][j]);
dmat[i][i] = 0;
rt[i].dist[j] = dmat[i][j];
rt[i].from[j] = 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] > dmat[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);
for (i = 0; i < n; i++)
{
printf("\n\nDistance Vector for node %d is \n", i );
for (j = 0; j < n; j++)
{
printf("\t\nnode %d via node %d need Distance: %d", j, rt[i].from[j], rt[i].dist[j]);
}
}
printf("\n\n");
}
반응형
'Network' 카테고리의 다른 글
[C Network Programming] Parity Code C코드 (0) | 2024.03.13 |
---|---|
[Network]ICMPv6 (0) | 2024.03.12 |
[C Network Programming] Dijkstra's Routing Algorithm C code (0) | 2024.03.11 |
[C Network Programming] Stop&Wait ARQ Protocol (0) | 2024.03.10 |