0% found this document useful (0 votes)
18 views5 pages

CNCS LAB - Part A - Distance Vector Routing and Dijkastra's Algorithm Experminets

Cncs lab manual

Uploaded by

kolnadharish258
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

CNCS LAB - Part A - Distance Vector Routing and Dijkastra's Algorithm Experminets

Cncs lab manual

Uploaded by

kolnadharish258
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No.

Distance Vector Algorithm

Aim: To write a program for Distance Vector algorithm to find suitable path for transmission.

#include<stdio.h>
#include<stdlib.h>
int main ()
{
int d [10][10], t[10][10], a[10][10], i, j, n, k,
count; printf ("Enter the number of nodes\n");
scanf ("%d", &n);
printf ("Enter the initial cost matrix\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
scanf("%d", &d[i][j]);
d[i][i] = 0;
t[i][j] = d[i][j];
a[i][j] = j;
}
}
do
{
count = 0;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
for(k=1; k<=n; k++)
{
if(t[i][j]>(d[i][k]+t[k][j]))
{
t[i][j] = (t[i][k] + t[k][j]);
a[i][j] = k;
count++;
}
}
}
}
}while(count!=0);

for(i=1; i<=n; i++)


{
printf("\n\n For router %d\n", i);

Department of Electronics and Communication


Engineering Computer Networks and Cyber Security Lab
(EC3002-1)
for(j=1; j<=n; j++)
{
printf("\t\n to node %d, via %d, Distance -> %d ",j, a[i][j], t[i][j]);
}
}
printf("\n\n");
return 0;
}

Initially:
Node 1 Node 2 Node 3 Node 4 Node 5

Node 1 0 7 999 999 1

Node 2 7 0 1 999 8

Node 3 999 1 0 2 999

Node 4 999 999 2 0 2

Node 5 1 8 999 2 0

Finally:
Node 1 Node 2 Node 3 Node 4 Node 5

Node 1 0 6 5 3 1

Node 2 6 0 1 3 5

Node 3 5 1 0 2 4

Node 4 3 3 2 0 2

Node 5 1 5 4 2 0

Output:
Enter the number of nodes : 5

Department of Electronics and Communication


Engineering Computer Networks and Cyber Security Lab
(EC3002-1)
Enter the initial cost matrix:
0 7 999 999 1
7 0 1 999 8
999 1 0 2 999
999 999 2 0 2
1 8 999 2 0

For router 1
to node 1, via 1, Distance -> 0
to node 2, via 5, Distance -> 6
to node 3, via 5, Distance -> 5
to node 4, via 5, Distance -> 3
to node 5, via 5, Distance -> 1

For router 2
to node 1, via 3, Distance -> 6
to node 2, via 2, Distance -> 0
to node 3, via 3, Distance -> 1
to node 4, via 3, Distance -> 3
to node 5, via 3, Distance -> 5

For router 3
to node 1, via 4, Distance -> 5
to node 2, via 2, Distance -> 1
to node 3, via 3, Distance -> 0
to node 4, via 4, Distance -> 2
to node 5, via 4, Distance -> 4

For router 4
to node 1, via 5, Distance -> 3
to node 2, via 3, Distance -> 3
to node 3, via 3, Distance -> 2
to node 4, via 4, Distance -> 0
to node 5, via 5, Distance -> 2

For router 5
to node 1, via 1, Distance -> 1
to node 2, via 4, Distance -> 5
to node 3, via 4, Distance -> 4
to node 4, via 4, Distance -> 2
to node 5, via 5, Distance -> 0

Department of Electronics and Communication


Engineering Computer Networks and Cyber Security Lab
(EC3002-1)
Experiment No. 6

Dijkstra Algorithm

Aim: To implement Dijkstra’s algorithm to compute the shortest routing path


#include<stdio.h>
#include<conio.h>
void dijkstra(int n, int v, int cost[10][10],int dist[10])
{
int count, u, i, w, visited[10], min;
for(i=0;i<n;i++)
{
visited[i]=0;
dist[i]=cost[v][i];
}
visited[v]=1;
dist[v]=0;
count=2;
while(count<=n)
{
min=999;
for(w=0;w<n;w++)
if((dist[w]<min) && (visited[w]!=1))
{
min=dist[w];
u=w;
}
visited[u]=1;
count++;
for(w=0;w<n;w++)
if((dist[u]+cost[u][w]<dist[w]) && (visited[w]!=1))
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n, v, cost[10][10], dist[10], i, j;
printf("Enter number of vertices:");
scanf("%d",&n);
printf("\nEnter cost matrix (for infinity, enter 999):\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
printf("\nEnter source vertex:");
scanf("%d",&v);
dijkstra(n,v,cost,dist);

Department of Electronics and Communication


Engineering Computer Networks and Cyber Security Lab
(EC3002-1)
printf("\nShortest path from \n");
for(i=0;i<n;i++)
if(i!=v)
printf("\n%d -> %d = %d", v, i, dist[i]);
getch();
}

Output:

Department of Electronics and Communication


Engineering Computer Networks and Cyber Security Lab
(EC3002-1)

You might also like