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

Lab5 F

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)
7 views3 pages

Lab5 F

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

EXPERMINET-05

AIM : Write a C program to find the shortest path between the Node
among the given networks.
Apparatus : Scilab

THEORY :
CODE :
function dijkstra(graph, src)

V = size(graph, 1);

dist = ones(1, V) * %inf; // Initialize distances as infinite

sptSet = zeros(1, V); // Shortest path tree set

dist(src) = 0;

for count = 1:V-1

minDist = %inf; u = -1;

for v = 1:V

if sptSet(v) == 0 & dist(v) <= minDist then

minDist = dist(v);

u = v;

end

end

sptSet(u) = 1;

for v = 1:V

if sptSet(v) == 0 & graph(u,v) > 0 & dist(u) + graph(u,v) < dist(v)

then

dist(v) = dist(u) + graph(u,v);

end

end

end

printf("Vertex \t Distance from Source\n");

for i = 1:V

printf("%d \t\t %d\n", i-1,

dist(i));

end

endfunction
//Example usage

graph =[0 10 20 0 0;

10 0 30 50 10 ;

20 30 0 20 33;

0 50 20 0 2;

0 10 33 20;]

src = 1; // Source

node (1 in Scilab

corresponds to 0 in

C)

OUTPUT :

CONCLUSION :
Dijkstra's Algorithm is an effective and efficient way to find the shortest paths in a weighted graph
with non-negative edge weights. The implementation in Scilab confirms the correctness of the
algorithm as it provides the shortest path distances accurately.

You might also like