0% found this document useful (0 votes)
2 views12 pages

CN FILE 2dj

The document contains code implementations for three algorithms: Distance Vector Algorithm, Flooding Algorithm, and Dijkstra Algorithm. Each section includes the aim, code, and sample output demonstrating the functionality of the algorithms. The code is written in C and includes user input for graph parameters and displays results based on the algorithms' computations.

Uploaded by

Deepanshi Jain
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 views12 pages

CN FILE 2dj

The document contains code implementations for three algorithms: Distance Vector Algorithm, Flooding Algorithm, and Dijkstra Algorithm. Each section includes the aim, code, and sample output demonstrating the functionality of the algorithms. The code is written in C and includes user input for graph parameters and displays results based on the algorithms' computations.

Uploaded by

Deepanshi Jain
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();

DAKSHIKA CHAUDHARY
2200910100051
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

DAKSHIKA CHAUDHARY
2200910100051
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

DAKSHIKA CHAUDHARY
2200910100051
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

DAKSHIKA CHAUDHARY
2200910100051
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
--------------------------------

DAKSHIKA CHAUDHARY
2200910100051
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: ");

DAKSHIKA CHAUDHARY
2200910100051
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

DAKSHIKA CHAUDHARY
2200910100051
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

DAKSHIKA CHAUDHARY
2200910100051
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];

DAKSHIKA CHAUDHARY
2200910100051
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");
}

DAKSHIKA CHAUDHARY
2200910100051
}
}

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;
}

DAKSHIKA CHAUDHARY
2200910100051
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

DAKSHIKA CHAUDHARY
2200910100051

You might also like