Program Explanation
This program implements Dijkstra’s Algorithm to find the shortest path from a source vertex to all other vertices in a weighted graph.
The graph is represented using an adjacency matrix, and the algorithm efficiently computes the shortest distances.
Program Structure
- Constants and Includes: Standard libraries are included for input/output and maximum values.
- Function Prototypes: Functions for the main algorithm and for printing results are declared.
- Main Function: Initializes the graph, sets the source vertex, and calls the Dijkstra function.
- Dijkstra Function: Implements the algorithm to calculate the shortest paths.
- Print Function: Displays the shortest path from the source to each vertex.
Code
#include #include #include #define V 5 // Number of vertices in the graph // Function prototypes int minDistance(int dist[], bool sptSet[]); void dijkstra(int graph[V][V], int src); void printSolution(int dist[], int n); int main() { // Example graph represented as an adjacency matrix int graph[V][V] = { {0, 10, 0, 30, 100}, {10, 0, 50, 0, 0}, {0, 50, 0, 20, 10}, {30, 0, 20, 0, 60}, {100, 0, 10, 60, 0} }; dijkstra(graph, 0); // Call Dijkstra's algorithm from source vertex 0 return 0; } // Function to find the vertex with the minimum distance value int minDistance(int dist[], bool sptSet[]) { int min = INT_MAX, min_index; for (int v = 0; v < V; v++) { if (sptSet[v] == false && dist[v] <= min) { min = dist[v]; min_index = v; } } return min_index; } // Function that implements 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 j bool sptSet[V]; // sptSet[i] will be true if vertex i is included in shortest path tree // Initialize all distances as infinite and sptSet[] as false for (int i = 0; i < V; i++) { dist[i] = INT_MAX; sptSet[i] = false; } // Distance from source to itself is always 0 dist[src] = 0; // Find 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); // Mark the picked vertex as processed sptSet[u] = true; // Update dist value of the adjacent vertices of the picked vertex for (int v = 0; v < V; v++) { // Update dist[v] if and only if it's 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 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 printSolution(dist, V); } // Function to print the constructed distance array void printSolution(int dist[], int n) { printf("Vertex Distance from Source\n"); for (int i = 0; i < n; i++) { printf("%d \t\t %d\n", i, dist[i]); } }
How to Compile and Run
- Copy the code into a file named
dijkstra.c
. - Open a terminal and navigate to the directory containing the file.
- Compile the code using
gcc dijkstra.c -o dijkstra
. - Run the program with
./dijkstra
.
Conclusion
Dijkstra’s Algorithm is a powerful tool for finding the shortest paths in a graph.
This C implementation provides a clear understanding of how the algorithm works, and can be extended for larger graphs or modified for different use cases.