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

CN FILE 2

The document contains three experiments implementing different routing algorithms: Distance Vector Algorithm, Flooding Algorithm, and Dijkstra Algorithm. Each section includes the aim, code, and output for the respective algorithm, demonstrating how to manage and calculate routing paths in a network. The code is written in C and includes user input for nodes and edges, as well as the resulting paths and distances for each algorithm.

Uploaded by

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

CN FILE 2

The document contains three experiments implementing different routing algorithms: Distance Vector Algorithm, Flooding Algorithm, and Dijkstra Algorithm. Each section includes the aim, code, and output for the respective algorithm, demonstrating how to manage and calculate routing paths in a network. The code is written in C and includes user input for nodes and edges, as well as the resulting paths and distances for each algorithm.

Uploaded by

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

EXPERIEMENT- 07

AIM: Write a code to implement Distance Vector Algorithm.


CODE:
#include<stdio.h>
int dist[50][50],temp[50][50],n,i,j,k,x;
void dvr();
int main()
{
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
printf("\nEnter the distance matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&dist[i][j]);
dist[i][i]=0;
temp[i][j]=j;
}
printf("\n");
}
dvr();
printf("enter value of i &j:");
scanf("%d",&i);
scanf("%d",&j);
printf("enter the new cost");
scanf("%d",&x);
dist[i][j]=x;
printf("After update\n\n");
dvr();

DHRUVA GUPTA 2200910100060


return 0;
}
void dvr()
{
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
for (k = 0; k < n; k++)
if (dist[i][k] + dist[k][j] < dist[i][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
temp[i][j] = k;
}

for(i=0;i<n;i++)
{
printf("\n\nState value for router %d is \n",i+1);
for(j=0;j<n;j++)
printf("\t\nnode %d via %d Distance%d",j+1,temp[i][j]+1,dist[i][j]);
}
printf("\n\n");

OUTPUT:
Enter the number of nodes : 4
Enter the distance matrix :
0
12
9
16

DHRUVA GUPTA 2200910100060


20
0
18
4

15
8
0
32

41
24
51
0
State value for router 1 is

node 1 via 1 Distance0


node 2 via 2 Distance12
node 3 via 3 Distance9
node 4 via 4 Distance16

State value for router 2 is

node 1 via 1 Distance20


node 2 via 2 Distance0
node 3 via 3 Distance18
node 4 via 4 Distance4

State value for router 3 is

DHRUVA GUPTA 2200910100060


node 1 via 1 Distance15
node 2 via 2 Distance8
node 3 via 3 Distance0
node 4 via 2 Distance12

State value for router 4 is

node 1 via 1 Distance41


node 2 via 2 Distance24
node 3 via 2 Distance42
node 4 via 4 Distance0

enter value of i &j:


1
3
enter the new cost
68
After update

State value for router 1 is

node 1 via 1 Distance0


node 2 via 2 Distance12
node 3 via 3 Distance9
node 4 via 4 Distance16

State value for router 2 is

node 1 via 1 Distance20

DHRUVA GUPTA 2200910100060


node 2 via 2 Distance0
node 3 via 3 Distance18
node 4 via 3 Distance30

State value for router 3 is

node 1 via 1 Distance15


node 2 via 2 Distance8
node 3 via 3 Distance0
node 4 via 2 Distance12

State value for router 4 is

node 1 via 1 Distance41


node 2 via 2 Distance24
node 3 via 2 Distance42
node 4 via 4 Distance0
--------------------------------

DHRUVA GUPTA 2200910100060


EXPERIEMENT- 08
AIM: Write a code to implement Flooding Algorithm.
CODE:

#include <stdio.h>
#include <stdlib.h>

#define MAX 100

int graph[MAX][MAX];
int visited[MAX];
int nodes;

void flood(int current, int cameFrom) {


visited[current] = 1;
printf("Packet reached node %d from node %d\n", current, cameFrom);

for (int i = 0; i < nodes; i++) {


if (graph[current][i] && i != cameFrom) {
flood(i, current);
}
}
}

int main() {
int edges;
printf("Enter number of nodes: ");
scanf("%d", &nodes);

printf("Enter number of edges: ");

DHRUVA GUPTA 2200910100060


scanf("%d", &edges);

printf("Enter edges (format: source destination):\n");


for (int i = 0; i < edges; i++) {
int u, v;
scanf("%d %d", &u, &v);
graph[u][v] = 1;
graph[v][u] = 1;
}

int source;
printf("Enter source node to start flooding: ");
scanf("%d", &source);

printf("\nFlooding started:\n");
flood(source, -1);

return 0;
}

OUTPUT:
Enter number of nodes: 4
Enter number of edges: 4
Enter edges (format: source destination):
01
02
13
23
Enter source node to start flooding: 0

DHRUVA GUPTA 2200910100060


Flooding started:
Packet reached node 0 from node -1
Packet reached node 1 from node 0
Packet reached node 3 from node 1
Packet reached node 2 from node 0

DHRUVA GUPTA 2200910100060


EXPERIEMENT- 09
AIM: Write a code to implement Dijkstra Algorithm in Routing.
CODE:
#include <stdio.h>
#include <limits.h>

#define MAX 100


#define INF 99999

int graph[MAX][MAX];
int distance[MAX];
int visited[MAX];
int parent[MAX];
int nodes;

void dijkstra(int start) {


for (int i = 0; i < nodes; i++) {
distance[i] = INF;
visited[i] = 0;
parent[i] = -1;
}

distance[start] = 0;

for (int count = 0; count < nodes - 1; count++) {


int min = INF, u = -1;

for (int i = 0; i < nodes; i++) {


if (!visited[i] && distance[i] <= min) {
min = distance[i];

DHRUVA GUPTA 2200910100060


u = i;
}
}

visited[u] = 1;

for (int v = 0; v < nodes; v++) {


if (!visited[v] && graph[u][v] && distance[u] + graph[u][v] < distance[v]) {
distance[v] = distance[u] + graph[u][v];
parent[v] = u;
}
}
}
}

void printPath(int j) {
if (parent[j] == -1)
return;
printPath(parent[j]);
printf(" -> %d", j);
}

void displayRoutes(int start) {


printf("\nShortest paths from node %d:\n", start);
for (int i = 0; i < nodes; i++) {
if (i != start) {
printf("To node %d: Distance = %d | Path = %d", i, distance[i], start);
printPath(i);
printf("\n");
}

DHRUVA GUPTA 2200910100060


}
}

int main() {
int edges;
printf("Enter number of nodes: ");
scanf("%d", &nodes);

printf("Enter number of edges: ");


scanf("%d", &edges);

for (int i = 0; i < nodes; i++)


for (int j = 0; j < nodes; j++)
graph[i][j] = 0;

printf("Enter edges and weights (format: source destination weight):\n");


for (int i = 0; i < edges; i++) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
graph[u][v] = w;
graph[v][u] = w;
}
int start;
printf("Enter starting node: ");
scanf("%d", &start);
dijkstra(start);
displayRoutes(start);

return 0;
}

DHRUVA GUPTA 2200910100060


OUTPUT:
Enter number of nodes: 5
Enter number of edges: 6
Enter edges and weights:
012
024
121
137
243
341
Enter starting node: 0
Shortest paths from node 0:
To node 1: Distance = 2 | Path = 0 -> 1
To node 2: Distance = 3 | Path = 0 -> 1 -> 2
To node 3: Distance = 8 | Path = 0 -> 1 -> 3
To node 4: Distance = 6 | Path = 0 -> 1 -> 2 -> 4

DHRUVA GUPTA 2200910100060

You might also like