본문 바로가기
Network

[C Network Programming] Distance Vector Routing Algorithm C code

by TYB 2024. 3. 10.
반응형
//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");

}

 

반응형