Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Assignment-9

119CS0159 | Roshan Kumar Sahu

Q1
Code

/* Program to implement Distance Vector Routing */


#include <bits/stdc++.h>
using namespace std;

int dist_mat[100][100];
map<int, int> working;

// Node definition
struct Node
{

int par_node[50], dist[50];


};

// function to initalise node


Node *NodeInitialisation(int &N)
{

cout << "Enter the no of nodes for the network: ";


cin >> N;
Node *Arr;
Arr = (Node *)malloc((N + 1) * sizeof(Node));
return Arr;
}

// function to create the weights in the distance matrix


void distanceMatrixCreation(int N)
{

cout << "\nDistance Matrix creation:\n";


for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= i; j++)
{

if (i == j)
{
dist_mat[i][j] = 0;
continue;
}

int wt = rand() % 100;


if (wt == 0)
{

dist_mat[i][j] = -1;
dist_mat[j][i] = -1;
continue;
}

dist_mat[i][j] = wt;
dist_mat[j][i] = wt;
}
}

for (int i = 1; i <= N; i++)


{
for (int j = 1; j <= N; j++)
{
cout << dist_mat[i][j] << " ";
}
cout << endl;
}
cout << endl;
}

// function to select working nodes


void selectWorkingNodes(int N)
{

cout << "Probability: 50% for the no. of working nodes\n";


cout << "No. of working nodes is " << N / 2 << endl;

Assignment-9 1
cout << "Working set nodes : ";
int x = 1;
for (int i = 1; i <= N / 2; i++)
{
working[x] = 1;
cout << x << " ";
x += 2;
}

cout << "\nWorking node distance matrix is \n";

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


{

if (i == 0)
{

for (int j = 0; j <= N; j++)


{
if (j == 0)
cout << "Node-" << j << " ";

if (working[j])
cout << "Node-" << j << " ";
}
cout << endl;
continue;
}

if (working[i])
{

cout << "Node-" << i << " ";


for (int j = 1; j <= N; j++)
{

if (!working[j])
continue;

cout << dist_mat[i][j] << " ";


}
cout << endl;
}
}
cout << endl;
}

// function to implement distance vector routing


void distanceVectorRouting(Node Arr[], int dist_mat[100][100], int N)
{

int ct = 0;
for (int i = 1; i <= N; i++)
{
if (working[i])
{
for (int j = 1; j <= N; j++)
{
if (working[j])
{
Arr[i].dist[j] = dist_mat[i][j];
Arr[i].par_node[j] = j;
}
}
}
}

do
{
ct = 0;
for (int i = 1; i <= N; i++)
{
if (working[i])
{
for (int j = 1; j <= N; j++)
{
if (working[j])
{
for (int k = 1; k <= N; k++)
{
if (working[k])
{
if (Arr[i].dist[j] > dist_mat[i][k] + Arr[k].dist[j])
{
Arr[i].dist[j] = Arr[i].dist[k] + Arr[k].dist[j];
Arr[i].par_node[j] = k;

Assignment-9 2
ct++;
}
}
}
}
}
}
}
} while (ct != 0);

cout << "\n";


for (int i = 1; i <= N; i++)
{
if (working[i])
{
printf("\n\nFor router %d\n", i);
for (int j = 1; j <= N; j++)
{
if (working[j])
printf("\t\nNode %d via %d distance: %d ", j, Arr[i].par_node[j], Arr[i].dist[j]);
}
}
}
printf("\n\n");
}

// main function
int main()
{

srand(time(0));
memset(dist_mat, 0, 100 * 100 * sizeof(int));
Node *Arr;
int size;
Arr = NodeInitialisation(size);
distanceMatrixCreation(size);
selectWorkingNodes(size);
distanceVectorRouting(Arr, dist_mat, size);

int src, dest;


cout << "\nFor finding end-to-end delay : ";
cout << "\nEnter start: ";
cin >> src;

cout << "\nEnter end : ";


cin >> dest;

cout << "\nShortest distance and corresponding shortest time: \n";


cout << "\tTo Node\tDistance(in km)\tTime(in s)";
cout << "\n\t" << dest << "\t\t" << Arr[src].dist[dest] << "\t\t" << (float(Arr[src].dist[dest]) / 1000);

cout << "\n";


cout << endl;
return 0;
}

Output

Assignment-9 3
Q2
Code

#include <bits/stdc++.h>
using namespace std;

int dist_mat[100][100];
int trans_rate = pow(10, 6); // transmission rate in m/sec

// Node structure definition


struct Node
{

int par_node, dist;


};

// utility function
int minDist(Node Arr[], bool set[], int N)
{

int mn = INT_MAX, min_idx;


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

Assignment-9 4
if (set[i] == false && Arr[i].dist <= mn)
mn = Arr[i].dist, min_idx = i;
}
return min_idx;
}

// function to implement node initialisation


Node *nodeInitialisation(int &N)
{

cout << "Enter the no of nodes for the network: ";


cin >> N;
Node *Arr;
Arr = (Node *)malloc(N * sizeof(Node));
return Arr;
}

// function to create the distance matrix


// Here the distance is considered in kilometres
void createDistanceMatrix(Node Arr[], int N)
{

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


{

int t_dist = rand() % 50 + 1;


dist_mat[i][(i + 1) % N] = t_dist;
dist_mat[(i + 1) % N][i] = t_dist;
}

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


{
for (int j = 0; j < N; j++)
{
if (dist_mat[i][j])
continue;
int wt = rand() % 50 + 1;
dist_mat[i][j] = wt;
dist_mat[j][i] = wt;
}
}
}

// function to find the routing path using djikstra


void Routing(Node Arr[], int dist_mat[100][100], int N, int src)
{

bool set[N];
for (int i = 0; i < N; i++)
{
Arr[i].dist = INT_MAX;
Arr[i].par_node = -1;
set[i] = false;
}

Arr[src].dist = 0;

for (int ct = 0; ct < N - 1; ct++)


{

int u = minDist(Arr, set, N);


set[u] = true;
for (int v = 0; v < N; v++)
{

if (!set[v] && dist_mat[u][v] && Arr[u].dist != INT_MAX && (Arr[u].dist + dist_mat[u][v] < Arr[v].dist))
{

Arr[v].dist = Arr[u].dist + dist_mat[u][v];


Arr[v].par_node = u;
}
}
}
}

// function to print the shortest path from source to destination


void shortestPathFromSrcToDest(Node Arr[], int dest)
{

vector<int> path;
int i = dest;
while (i != -1)
{

path.push_back(i);
i = Arr[i].par_node;

Assignment-9 5
}

reverse(path.begin(), path.end());
cout << "Path followed : ";
for (i = 0; i < path.size() - 1; i++)
{
cout << path[i] << " --> ";
}
cout << path[i] << "\nDistance : " << Arr[dest].dist << " kms, Time to transmit : " << (double)(Arr[dest].dist * 1000) / trans_rate
}

// main function
int main()
{

srand(time(NULL));
memset(dist_mat, 0, 100 * 100 * sizeof(int));
Node *Arr;
int size;
Arr = nodeInitialisation(size);
cout << "\nFollowing shortest path routing algorithm,\nCreating nodes...\n";
createDistanceMatrix(Arr, size);
cout << "Computing distance matrix...\n";
Routing(Arr, dist_mat, size, 0);
cout << "Computing routing table...\n";

cout << "\nPaths followed:\n";


for (int i = 1; i < size; i++)
{
shortestPathFromSrcToDest(Arr, i);
}
return 0;
}
// end of main

Output

Assignment-9 6

You might also like