0% found this document useful (0 votes)
32 views3 pages

Assignment-8: 119CS0159 - Roshan Kumar Sahu

This document contains C++ code to implement Dijkstra's algorithm for finding the shortest path between nodes in a graph. It defines a Node struct with parent and distance fields, initializes a distance matrix, finds the minimum distance node, and uses Dijkstra's algorithm to calculate the routing path from a source node to all other nodes, storing the results in the Node parent and distance fields. It then prints the calculated paths.

Uploaded by

Roshan Sahu
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)
32 views3 pages

Assignment-8: 119CS0159 - Roshan Kumar Sahu

This document contains C++ code to implement Dijkstra's algorithm for finding the shortest path between nodes in a graph. It defines a Node struct with parent and distance fields, initializes a distance matrix, finds the minimum distance node, and uses Dijkstra's algorithm to calculate the routing path from a source node to all other nodes, storing the results in the Node parent and distance fields. It then prints the calculated paths.

Uploaded by

Roshan Sahu
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/ 3

Assignment-8

119CS0159 | Roshan Kumar Sahu

Code

//Roshan Kumar Sahu || 119CS0159


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

struct Node {

int parent, dist;


};

int distance_matrix[100][100];
int transmission_rt = pow(10, 6);

int minimunDistance(Node arr[], bool set[], int N) {

int mn = INT_MAX, min_idx;


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

if(set[i] == false && arr[i].dist <= mn)


mn = arr[i].dist, min_idx = i;
}
return min_idx;
}

Node* initializeNode(int& N) {

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


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

//function to create the distance matrix


void createDistanceMatrix(Node Arr[], int N){

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

int t_dist = rand()%50 + 1;


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

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


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

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

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

path.push_back(i);
i = Arr[i].parent;
}

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, \nTime to transmit : " << (double)(Arr[dest].dist * 1000)/transmissio
}

//function to find the routing path using djikstra


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

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

Arr[src].dist = 0;

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

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


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

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

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


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

int main() {

srand(time(NULL));
memset(distance_matrix, 0, 100*100*sizeof(int));
Node* Arr;
int size;
Arr = initializeNode(size);
cout << "\nCreating nodes...\n";
createDistanceMatrix(Arr, size);
cout << "Distance matrix:\n";
Routing(Arr, distance_matrix, size, 0);
cout << "Routing table:\n";

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


for(int i = 1; i < size; i++) {
printPath(Arr, i);
}

return 0;
}

Output

Assignment-8 2
Assignment-8 3

You might also like