0% found this document useful (0 votes)
22 views

Dijkstras Algorithm

Uploaded by

abinayadev04
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)
22 views

Dijkstras Algorithm

Uploaded by

abinayadev04
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

EX NO:

DATE: DIJKSTRA’S ALGORITHM

AIM:
To write a program to find the shortest paths to other vertices from a given vertex in a
weighted connected graph using Dijkstra’s algorithm.

ALGORITHM:
Step 1: Start the program.

Step 2: Ask the user to input the number of vertices n.

Step 3: Create and input the adjacency matrix graph[MAX][MAX].

Step 4: Initialize arrays distance, visited, and parent, and the spanningTree matrix.

Step 5: Set the distance to the starting node as 0 and mark it as visited.

Step 6: Begin a loop until all vertices are visited.

Step 7: Find the vertex with the minimum distance that has not been visited yet.

Step 8: Mark this vertex as visited.

Step 9: Update distances and parents for adjacent unvisited vertices.

Step 10: Update the spanning tree matrix.

Step 11: Update the cost matrix by setting the row and column corresponding to the visited
vertex to infinity.

Step 12: Print the spanning tree matrix and the total minimum cost of the spanning tree.

Step 13: End the program.

SOURCE CODE:
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#define V 100
int min_distance(int dist[], bool spt_set[], int n) {
int min = INT_MAX, min_index;
for (int v = 0; v < n; v++)
if (spt_set[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
void print_solution(int dist[], int src, int n) {
printf("Vertex Distance from Source\n");
for (int i = 0; i < n; i++) {
if (dist[i] != INT_MAX)
printf("%d %d\n", i, dist[i]);
else
printf("%d Infinity\n", i);
}
}
void dijkstra(int graph[V][V], int src, int n) {
int dist[V];
bool spt_set[V];

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


dist[i] = INT_MAX, spt_set[i] = false;

dist[src] = 0;

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


int u = min_distance(dist, spt_set, n);
spt_set[u] = true;

for (int v = 0; v < n; 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];
}

print_solution(dist, src, n);


}
int main() {
int graph[V][V];
int n;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &n);
printf("Enter the adjacency matrix of the graph (%d x %d):\n", n, n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &graph[i][j]);
int src;
printf("Enter the source vertex: ");
scanf("%d", &src);
dijkstra(graph, src, n);
return 0;
}

OUTPUT:
RESULT:
Thus the program to perform Prims algorithm is executed and is verified.

You might also like