0% found this document useful (0 votes)
240 views

C Program / Source Code For The Distance Vector Routing Algorithm Using Bellman Ford's Algorithm

This C program implements the distance vector routing algorithm using Bellman-Ford's algorithm to calculate the shortest paths between nodes in a network. It takes user input for the number of nodes and cost matrix, initializes the distance and from values, then iteratively calculates the minimum distances and next hops until route changes stop. It outputs the routing tables for each node showing the destination, next hop, and distance. A second part of the code implements link state routing by having each node broadcast its own link state packet with distances to other nodes.

Uploaded by

FatiLily
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views

C Program / Source Code For The Distance Vector Routing Algorithm Using Bellman Ford's Algorithm

This C program implements the distance vector routing algorithm using Bellman-Ford's algorithm to calculate the shortest paths between nodes in a network. It takes user input for the number of nodes and cost matrix, initializes the distance and from values, then iteratively calculates the minimum distances and next hops until route changes stop. It outputs the routing tables for each node showing the destination, next hop, and distance. A second part of the code implements link state routing by having each node broadcast its own link state packet with distances to other nodes.

Uploaded by

FatiLily
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

C Program / source code for the Distance Vector

Routing Algorithm using Bellman Ford's Algorithm


/*
Distance Vector Routing in this program is implemented using
Bellman Ford Algorithm:*/
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int costmat[20][20];
int nodes,i,j,k,count=0;
printf("\nEnter the number of nodes : ");
scanf("%d",&nodes);//Enter the nodes
printf("\nEnter the cost matrix :\n");
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[i][j];//initialise the distance
equal to cost matrix
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)//We choose arbitary vertex k and
we calculate the direct distance from the node i to k using the
cost matrix
//and add the distance from k to node j
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
{//We calculate the minimum distance
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<nodes;i++)
{
printf("\n\n For router %d\n",i+1);
for(j=0;j<nodes;j++)
{
printf("\t\nnode %d via %d Distance %d
",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n\n");
getch();
}
/*
A sample run of the program works as:Enter the number of nodes :
3
Enter the cost matrix :
0 2 7
2 0 1
7 1 0
For router 1
node 1 via 1 Distance 0
node 2 via 2 Distance 2
node 3 via 3 Distance 3
For router 2
node 1 via 1 Distance 2
node 2 via 2 Distance 0
node 3 via 3 Distance 1
For router 3
node 1 via 1 Distance 3
node 2 via 2 Distance 1
node 3 via 3 Distance 0
*/
>>>>>>>>>>>>>>>>>>>
/*************************************************************************************/
/* C Program to implement link state routing.*/
/* <span id="IL_AD2" class="IL_AD">Download</span> more programs at http://sourcecode4u.com/ */
/*************************************************************************************/
#include<stdio.h>
main()

{
int n,a[10][10],i,j,k;
printf("\n ENTER THE NO.OF NODES: ");
scanf("%d",&n);
printf("\n ENTER THE MATRIX ELEMENTS: ");
for(i=0;i<n;i++)
{
printf("\nENTER THE DISTANCE FOR NODE:%d\n",i+1);
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
printf("THE LINK STATE STATE PACKETS FOR NODE:%d\n",i+1);
printf("\n NODE\tDISTANCE\n");
for(j=0;j<n;j++)
{
if(a[i][j]!=0&&a[i][j]!=-1)
{
printf("%d\t%d\n",j+1,a[i][j]);
}
}
printf("\n\n");
}

Out Put

You might also like