|
| 1 | +/** |
| 2 | + * @file Dijkstra.cpp |
| 3 | + * @brief Dijkstra's Shortest Path Algorithm Implementation |
| 4 | + * |
| 5 | + * Dijkstra's algorithm finds the shortest path from a source vertex to all |
| 6 | + * other vertices in a weighted graph with non-negative edge weights. |
| 7 | + * |
| 8 | + * Time Complexity: O(V²) where V is the number of vertices |
| 9 | + * (Can be improved to O(E + V log V) with a min-heap) |
| 10 | + * |
| 11 | + * Space Complexity: O(V²) for the adjacency matrix representation |
| 12 | + */ |
| 13 | + |
| 14 | +#include <iostream> |
| 15 | +#include <climits> |
| 16 | +#include <algorithm> |
| 17 | + |
| 18 | +/* Constants */ |
| 19 | +#define MAX_VERTICES 10 |
| 20 | +#define INFINITE_DISTANCE INT_MAX / 2 /* Use large value to avoid overflow */ |
| 21 | + |
| 22 | +/* Global arrays for Dijkstra's algorithm */ |
| 23 | +int distance_arr[MAX_VERTICES]; /**< Shortest distance from source to each vertex */ |
| 24 | +int predecessor_arr[MAX_VERTICES]; /**< Predecessor of each vertex in shortest path */ |
| 25 | +int adjacency_matrix[MAX_VERTICES][MAX_VERTICES]; /**< Graph represented as adjacency matrix */ |
| 26 | + |
| 27 | +/** |
| 28 | + * @brief Initializes the adjacency matrix with sample data |
| 29 | + * |
| 30 | + * This is a placeholder - in practice, the graph would be loaded |
| 31 | + * from input or file. |
| 32 | + */ |
| 33 | +void init_graph() |
| 34 | +{ |
| 35 | + /* Initialize with zeros (no edges) */ |
| 36 | + for (int i = 0; i < MAX_VERTICES; i++) { |
| 37 | + for (int j = 0; j < MAX_VERTICES; j++) { |
| 38 | + adjacency_matrix[i][j] = (i == j) ? 0 : INFINITE_DISTANCE; |
| 39 | + } |
| 40 | + } |
| 41 | + /* Add sample edges here if needed for testing */ |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * @brief Dijkstra's shortest path algorithm |
| 46 | + * |
| 47 | + * Finds the shortest path from the source vertex to all other vertices. |
| 48 | + * Uses a greedy approach, always selecting the unvisited vertex with |
| 49 | + * the smallest tentative distance. |
| 50 | + * |
| 51 | + * @param source_vertex The starting vertex for path calculations |
| 52 | + */ |
| 53 | +void dijkstra_shortest_path(int source_vertex) |
| 54 | +{ |
| 55 | + bool visited[MAX_VERTICES] = {false}; /* Track visited vertices */ |
| 56 | + int num_vertices = MAX_VERTICES; |
| 57 | + |
| 58 | + /* Initialize distances and predecessors */ |
| 59 | + for (int i = 0; i < num_vertices; ++i) { |
| 60 | + distance_arr[i] = adjacency_matrix[source_vertex][i]; |
| 61 | + visited[i] = false; |
| 62 | + |
| 63 | + if (distance_arr[i] == INFINITE_DISTANCE) { |
| 64 | + predecessor_arr[i] = -1; /* No path from source */ |
| 65 | + } else { |
| 66 | + predecessor_arr[i] = source_vertex; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /* Distance to source is 0 */ |
| 71 | + distance_arr[source_vertex] = 0; |
| 72 | + visited[source_vertex] = true; |
| 73 | + |
| 74 | + /* Find shortest path for all remaining vertices */ |
| 75 | + for (int i = 1; i < num_vertices; i++) { |
| 76 | + /* Find the unvisited vertex with minimum distance */ |
| 77 | + int min_distance = INFINITE_DISTANCE; |
| 78 | + int min_vertex = source_vertex; |
| 79 | + |
| 80 | + for (int v = 0; v < num_vertices; ++v) { |
| 81 | + if (!visited[v] && distance_arr[v] < min_distance) { |
| 82 | + min_vertex = v; |
| 83 | + min_distance = distance_arr[v]; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /* Mark the selected vertex as visited */ |
| 88 | + visited[min_vertex] = true; |
| 89 | + |
| 90 | + /* Update distances to neighbors of the selected vertex */ |
| 91 | + for (int v = 0; v < num_vertices; v++) { |
| 92 | + if (!visited[v] && adjacency_matrix[min_vertex][v] != INFINITE_DISTANCE) { |
| 93 | + int new_distance = distance_arr[min_vertex] + adjacency_matrix[min_vertex][v]; |
| 94 | + |
| 95 | + /* If found a shorter path, update distance and predecessor */ |
| 96 | + if (new_distance < distance_arr[v]) { |
| 97 | + distance_arr[v] = new_distance; |
| 98 | + predecessor_arr[v] = min_vertex; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * @brief Prints the shortest path from source to a target vertex |
| 107 | + * |
| 108 | + * @param target_vertex The destination vertex |
| 109 | + * @param source_vertex The source vertex (for stopping condition) |
| 110 | + */ |
| 111 | +void print_path(int target_vertex, int source_vertex) |
| 112 | +{ |
| 113 | + if (target_vertex == source_vertex) { |
| 114 | + std::cout << source_vertex; |
| 115 | + return; |
| 116 | + } |
| 117 | + if (predecessor_arr[target_vertex] == -1) { |
| 118 | + std::cout << "No path exists"; |
| 119 | + return; |
| 120 | + } |
| 121 | + print_path(predecessor_arr[target_vertex], source_vertex); |
| 122 | + std::cout << " -> " << target_vertex; |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * @brief Main function demonstrating Dijkstra's algorithm |
| 127 | + * |
| 128 | + * Note: This implementation requires manual initialization of the |
| 129 | + * adjacency matrix before running the algorithm. |
| 130 | + */ |
| 131 | +int main() |
| 132 | +{ |
| 133 | + /* Initialize graph - add your edge weights here */ |
| 134 | + init_graph(); |
| 135 | + |
| 136 | + /* Example: Create a simple graph */ |
| 137 | + /* Vertex 0 to 1 with weight 4 */ |
| 138 | + adjacency_matrix[0][1] = 4; |
| 139 | + adjacency_matrix[1][0] = 4; |
| 140 | + |
| 141 | + /* Vertex 0 to 2 with weight 2 */ |
| 142 | + adjacency_matrix[0][2] = 2; |
| 143 | + adjacency_matrix[2][0] = 2; |
| 144 | + |
| 145 | + /* Vertex 1 to 2 with weight 1 */ |
| 146 | + adjacency_matrix[1][2] = 1; |
| 147 | + adjacency_matrix[2][1] = 1; |
| 148 | + |
| 149 | + /* Vertex 1 to 3 with weight 5 */ |
| 150 | + adjacency_matrix[1][3] = 5; |
| 151 | + adjacency_matrix[3][1] = 5; |
| 152 | + |
| 153 | + /* Vertex 2 to 3 with weight 8 */ |
| 154 | + adjacency_matrix[2][3] = 8; |
| 155 | + adjacency_matrix[3][2] = 8; |
| 156 | + |
| 157 | + int source = 0; |
| 158 | + dijkstra_shortest_path(source); |
| 159 | + |
| 160 | + /* Print results */ |
| 161 | + std::cout << "Shortest distances from vertex " << source << ":" << std::endl; |
| 162 | + for (int i = 0; i < MAX_VERTICES; i++) { |
| 163 | + if (distance_arr[i] != INFINITE_DISTANCE) { |
| 164 | + std::cout << "To vertex " << i << ": distance = " << distance_arr[i]; |
| 165 | + std::cout << ", path: "; |
| 166 | + print_path(i, source); |
| 167 | + std::cout << std::endl; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return 0; |
| 172 | +} |
0 commit comments