Dijkstras Algorithm
Dijkstras 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 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 7: Find the vertex with the minimum distance that has not been visited yet.
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.
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];
dist[src] = 0;
OUTPUT:
RESULT:
Thus the program to perform Prims algorithm is executed and is verified.