#include <stdio.h>
#include <limits.h>
#define V 9 // Number of vertices in the graph
// Function to find the vertex with the minimum distance value
int minDistance(int dist[], int sptSet[]) {
int min = INT_MAX, 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;
}
// Function to implement Dijkstra's algorithm
void dijkstra(int graph[V][V], int src) {
int dist[V]; // Output array. dist[i] holds the shortest distance from src to i
int sptSet[V]; // Shortest Path Tree Set
// Initialize all distances as infinite and sptSet as false
for (int i = 0; i < V; i++) {
dist[i] = INT_MAX;
sptSet[i] = 0;
}
// Distance from source to itself is always 0
dist[src] = 0;
// Find the shortest path for all vertices
for (int count = 0; count < V - 1; count++) {
// Pick the minimum distance vertex from the set of vertices not yet processed
int u = minDistance(dist, sptSet);
sptSet[u] = 1; // Mark the picked vertex as processed
// Update dist value of the neighboring vertices of the picked vertex
for (int v = 0; v < V; v++) {
// Update dist[v] if and only if it is not in sptSet, there is an edge from u to v,
// and the total weight of path from src to v through u is smaller than the current value of dist[v]
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
// Print the constructed distance array
printf("Vertex \t Distance from Source\n");
for (int i = 0; i < V; i++) {
printf("%d \t %d\n", i, dist[i]);
}
}
int main() {
// Example graph represented as an adjacency matrix
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); // Starting from vertex 0
return 0;
}
Explanation of the Code
- Constants and Variables:
- We define
V
as the number of vertices in the graph. - The
minDistance
function finds the vertex with the minimum distance that hasn’t been processed yet.
- We define
- Dijkstra Function:
- The
dijkstra
function initializes thedist
array to store the shortest distances and thesptSet
array to track processed vertices. - The main loop runs until all vertices have been processed. Inside the loop:
- It selects the vertex with the smallest distance (not yet processed).
- Updates the distances of its adjacent vertices.
- The
- Graph Representation:
- The graph is represented as an adjacency matrix, where
graph[i][j]
holds the weight of the edge from vertexi
to vertexj
. A value of0
means no edge exists.
- The graph is represented as an adjacency matrix, where
- Output:
- The program prints the shortest distance from the starting vertex (in this case, vertex 0) to all other vertices.
Running the Program
To run this program:
- Copy the code into a C file, say
dijkstra.c
. - Compile it using a C compiler. For example:
gcc dijkstra.c -o dijkstra
Execute the program:
./dijkstra
Sample Output
When you run the program, you will see output similar to this:
Vertex Distance from Source
0 0
1 4
2 12
3 19
4 21
5 11
6 9
7 8
8 14
Explanation of Output
- The output shows the shortest distance from the starting vertex (0) to each vertex in the graph.
- For instance, the shortest distance from vertex 0 to vertex 1 is 4, while the distance to vertex 2 is 12, and so on.