Daa Casestudy (Lab)
Daa Casestudy (Lab)
GLOBAL
UNIVERSITY
CASE STUDY-(LAB)
Design And analysis of algorithm
SUBJECT CODE:-cse23307
Implementation in c:-
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
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.