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

Exp-3 1

The document outlines an experiment for a laboratory course on Design and Analysis of Algorithms, focusing on implementing Dijkstra's algorithm to find the shortest paths in a weighted graph. It includes pseudocode and C source code for the algorithm, along with a sample graph and expected output. The time complexity of the algorithm is noted as O(V^2), where V is the number of vertices.

Uploaded by

717823l316
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)
13 views3 pages

Exp-3 1

The document outlines an experiment for a laboratory course on Design and Analysis of Algorithms, focusing on implementing Dijkstra's algorithm to find the shortest paths in a weighted graph. It includes pseudocode and C source code for the algorithm, along with a sample graph and expected output. The time complexity of the algorithm is noted as O(V^2), where V is the number of vertices.

Uploaded by

717823l316
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/ 3

23CSR304 Design and Analysis of Algorithms Laboratory

Exp. No : 3.1
FINDING THE SHORTEST PATH FROM THE SOURCE TO
Date :
OTHER VERTICES

AIM:
To write a C program to return the shortest paths from the source to all the other vertices in
the given weighted graph.
Data Structure: Array
Datatype: Integer

PSEUDOCODE:

BEGIN
int minDistance(int dist[], int sptSet[]) {
int min = INF, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == 0 && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}

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


int dist[V];
int sptSet[V];
for (int i = 0; i < V; i++) {
dist[i] = INF;
sptSet[i] = 0;
}

dist[src] = 0;

for (int count = 0; count < V - 1; count++) {


int u = minDistance(dist, sptSet);
sptSet[u] = 1;

for (int v = 0; v < V; v++)


if (!sptSet[v] && graph[u][v] && dist[u] != INF
&& dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}

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


printf("%d ", dist[i]);
printf("\n");

int main()

717823L316
23CSR304 Design and Analysis of Algorithms Laboratory

int graph[V][V] = { {0, 4, 0, 0, 0, 0, 0, 8, 0},


{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0} };

dijkstra(graph, 0);
return 0;
END

Time Complexity: O(V^2) // V = No of vertices

SOURCE CODE:

#include <stdio.h>

#define V 9
#define INF 99999

int minDistance(int dist[], int sptSet[]) {


int min = INF, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == 0 && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}

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


int dist[V];
int sptSet[V];
for (int i = 0; i < V; i++) {
dist[i] = INF;
sptSet[i] = 0;
}

dist[src] = 0;

for (int count = 0; count < V - 1; count++) {


int u = minDistance(dist, sptSet);
sptSet[u] = 1;

for (int v = 0; v < V; v++)


if (!sptSet[v] && graph[u][v] && dist[u] != INF
&& dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];

717823L316
23CSR304 Design and Analysis of Algorithms Laboratory

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


printf("%d ", dist[i]);
printf("\n");
}

int main() {

int graph[V][V] = { {0, 4, 0, 0, 0, 0, 0, 8, 0},


{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0} };

dijkstra(graph, 0);
return 0;
}

OUTPUT:

RESULT:
Thus the program for finding the shortest paths from the source to all the other
vertices in the given weighted graph is successfully executed an the out has been verified.

717823L316

You might also like