|
| 1 | +/** |
| 2 | + * @file floyd_warshall.cpp |
| 3 | + * @brief Implementation of the Floyd-Warshall algorithm in C++ |
| 4 | + * |
| 5 | + * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of vertices |
| 6 | + * in a weighted graph. It is particularly useful for dense graphs where the number of edges is |
| 7 | + * close to the number of vertices squared. The algorithm can handle graphs with positive or negative |
| 8 | + * edge weights but will not correctly handle graphs with negative weight cycles. |
| 9 | + * |
| 10 | + * The algorithm works by initializing a distance matrix with the direct distances between vertices. |
| 11 | + * Then, it iteratively updates the matrix by considering each vertex as an intermediate point and |
| 12 | + * checks if a shorter path exists through that vertex. The time complexity of the algorithm is O(V^3), |
| 13 | + * where V is the number of vertices in the graph, and the space complexity is O(V^2). |
| 14 | + * |
| 15 | + * Limitations: |
| 16 | + * 1. High time complexity: O(V^3), which may be impractical for very large graphs. |
| 17 | + * 2. High space complexity: O(V^2), requiring significant memory for large graphs. |
| 18 | + * 3. Cannot handle negative weight cycles: If such cycles exist, the algorithm cannot provide correct shortest paths. |
| 19 | + * |
| 20 | + * Usage: |
| 21 | + * This implementation reads the number of vertices and the adjacency matrix of the graph from standard input. |
| 22 | + * The resulting shortest path distances between all pairs of vertices are printed to the standard output. |
| 23 | + * |
| 24 | + * Example input: |
| 25 | + * 4 |
| 26 | + * 0 3 INF 5 |
| 27 | + * 2 0 INF 4 |
| 28 | + * INF 1 0 INF |
| 29 | + * INF INF 2 0 |
| 30 | + * |
| 31 | + * Example output: |
| 32 | + * 0 3 7 5 |
| 33 | + * 2 0 6 4 |
| 34 | + * 3 1 0 5 |
| 35 | + * 5 3 2 0 |
| 36 | + */ |
| 37 | + |
| 38 | +#include <iostream> |
| 39 | +#include <vector> |
| 40 | +#include <algorithm> |
| 41 | + |
| 42 | +const int inf = 1e8; |
| 43 | + |
| 44 | +void display(const std::vector<std::vector<int>>& graph) { |
| 45 | + int n = graph.size(); |
| 46 | + for (int i = 0; i < n; i++) { |
| 47 | + for (int j = 0; j < n; j++) { |
| 48 | + if (graph[i][j] == inf) { |
| 49 | + std::cout << "INF "; |
| 50 | + } else { |
| 51 | + std::cout << graph[i][j] << " "; |
| 52 | + } |
| 53 | + } |
| 54 | + std::cout << std::endl; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void floyd(const std::vector<std::vector<int>>& graph, std::vector<std::vector<int>>& dist) { |
| 59 | + int n = graph.size(); |
| 60 | + for (int k = 0; k < n; k++) { |
| 61 | + for (int i = 0; i < n; i++) { |
| 62 | + for (int j = 0; j < n; j++) { |
| 63 | + if (dist[i][k] < inf && dist[k][j] < inf) { |
| 64 | + dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +int main() { |
| 72 | + int N, M; |
| 73 | + std::cin >> N >> M; // N - number of vertices; M - number of edges. |
| 74 | + std::vector<std::vector<int>> graph(N, std::vector<int>(N, inf)); |
| 75 | + for (int i = 0; i < N; i++) { |
| 76 | + graph[i][i] = 0; |
| 77 | + } |
| 78 | + for (int i = 0; i < M; i++) { |
| 79 | + int from, to, length; |
| 80 | + std::cin >> from >> to >> length; |
| 81 | + graph[from][to] = length; |
| 82 | + } |
| 83 | + |
| 84 | + // min_len[a][b] : the shortest distance from a to b. |
| 85 | + std::vector<std::vector<int>> min_len = graph; |
| 86 | + |
| 87 | + floyd(graph, min_len); |
| 88 | + display(min_len); |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
0 commit comments