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

Daa Casestudy (Lab)

The document presents a case study on implementing Dijkstra's algorithm in C for finding the shortest path in a graph. It includes a code example that utilizes an adjacency matrix to represent the graph and calculates the shortest distance from a source node to a destination node. The output of the implementation indicates that the shortest distance from node 0 to node 4 is 28.

Uploaded by

pritish5903
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)
11 views5 pages

Daa Casestudy (Lab)

The document presents a case study on implementing Dijkstra's algorithm in C for finding the shortest path in a graph. It includes a code example that utilizes an adjacency matrix to represent the graph and calculates the shortest distance from a source node to a destination node. The output of the implementation indicates that the shortest distance from node 0 to node 4 is 28.

Uploaded by

pritish5903
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/ 5

C V RAMAN

GLOBAL
UNIVERSITY

CASE STUDY-(LAB)
Design And analysis of algorithm
SUBJECT CODE:-cse23307

TOPIC:-dEvelop a small prototype


implementation for real life application of
dijkstra’s algorithm

GUIDED BY:- dr mamata wagh


Submitted by:- pritish kumar Mohanty
DEVELOP A SMALL PROTOTYPE
IMPLEMENTATION FOR REAL LIFE
APPLICATION OF DIJKSTRA’S ALGORITHM

Implementation in c:-
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>

#define V 5 // Number of vertices in the graph

int min_distance(int dist[], bool spt_set[]) {


int min = INT_MAX, min_index;
for (int v = 0; v < V; v++) {
if (spt_set[v] == false && dist[v] <= min) {
min = dist[v];
min_index = v;
}
}
return min_index;
}

void dijkstra(int graph[V][V], int src, int dest) {


int dist[V];
bool spt_set[V];
for (int i = 0; i < V; i++) {
dist[i] = INT_MAX;
spt_set[i] = false;
}
dist[src] = 0;
for (int count = 0; count < V-1; count++) {
int u = min_distance(dist, spt_set);
spt_set[u] = true;
for (int v = 0; v < V; v++) {
if (!spt_set[v] && graph[u][v] && dist[u] !=
INT_MAX
&& dist[u]+graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
}
printf("Shortest distance from node %d to node %d is
%d\n", src, dest, dist[dest]);
}

int main() {
int graph[V][V] = {
{0, 4, 0, 0, 0},
{4, 0, 8, 0, 0},
{0, 8, 0, 7, 0},
{0, 0, 7, 0, 9},
{0, 0, 0, 9, 0}
};
int src = 0, dest = 4;
dijkstra(graph, src, dest);
return 0;
}

Output:-
Shortest distance from node 0 to node 4 is 28
Explanation for the code used:-
In this implementation, graph is a 2D array (an
adjacency matrix) representing the graph, where each
cell (i, j) represents the weight of the edge between
nodes i and j. src and dest are the source and
destination nodes respectively, for which we want to
find the shortest path.
The implementation maintains an array dist of shortest
distances from the source node to each node in the
graph, and a boolean array spt_set indicating whether
a node has been included in the shortest path tree
(SPT) yet. Initially, all distances are set to INT_MAX and
all nodes are marked as not included in the SPT. The
distance to the source node is set to 0.

The implementation then repeatedly selects the node


with the smallest distance to the source that has not
yet been included in the SPT, adds it to the SPT, and
updates the distances of its neighbors if a shorter path
through the selected node is found.

To use this implementation, you can create a graph as a


2D array and call the dijkstra function with the graph,
source node, and destination node as arguments. The
shortest distance between the source and destination
nodes will be printed.

In this example, the graph has five nodes labeled 0 to 4,


and edges with weights are represented by an
adjacency matrix. The shortest path from node 0 to
node 4.
Thank you

You might also like