|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Find the shortest paths between all pairs of vertices in a graph using |
| 4 | + * the [Floyd-Warshall Algorithm](https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm). |
| 5 | + * @details |
| 6 | + * The Floyd-Warshall algorithm is an algorithm for finding shortest paths in a |
| 7 | + * weighted graph with positive or negative edge weights (but with no negative cycles). |
| 8 | + * The algorithm works for both directed and undirected graphs. |
| 9 | + * It uses dynamic programming to iteratively improve the estimate of the shortest path |
| 10 | + * between two vertices by considering each vertex as an intermediate point. |
| 11 | + * |
| 12 | + * Time Complexity: O(n^3) |
| 13 | + * where n is the number of vertices in the graph. |
| 14 | + * |
| 15 | + * Space Complexity: O(n^2) |
| 16 | + * |
| 17 | + * @author [Naman Jain](https://github.com/namanmodi65) |
| 18 | + */ |
| 19 | + |
| 20 | +#include <cassert> /// for std::assert |
| 21 | +#include <iostream> /// for IO operations |
| 22 | +#include <vector> /// for std::vector |
| 23 | +#include <limits> /// for std::numeric_limits |
| 24 | + |
| 25 | +/** |
| 26 | + * @brief Function to implement Floyd-Warshall Algorithm |
| 27 | + * @param graph The input adjacency matrix of the graph, where graph[i][j] |
| 28 | + * represents the weight of the edge from vertex i to vertex j. |
| 29 | + * If there is no edge, it should be set to infinity. |
| 30 | + * @return A matrix of shortest distances between all pairs of vertices. |
| 31 | + */ |
| 32 | + |
| 33 | +std::vector<std::vector<int>> floyd_warshall(std::vector<std::vector<int>>& graph) { |
| 34 | + int n = graph.size(); |
| 35 | + std::vector<std::vector<int>> dist = graph; |
| 36 | + |
| 37 | + for (int k = 0; k < n; ++k) { |
| 38 | + for (int i = 0; i < n; ++i) { |
| 39 | + for (int j = 0; j < n; ++j) { |
| 40 | + if (dist[i][k] != std::numeric_limits<int>::max() && |
| 41 | + dist[k][j] != std::numeric_limits<int>::max()) { |
| 42 | + dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + for (int i = 0; i < n; ++i) { |
| 49 | + if (dist[i][i] < 0) { |
| 50 | + std::cerr << "Graph contains a negative cycle\n"; |
| 51 | + return {}; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return dist; |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +/** |
| 60 | + * @brief Test cases for Floyd-Warshall algorithm |
| 61 | + * @returns void |
| 62 | + */ |
| 63 | +static void tests() { |
| 64 | + const int INF = std::numeric_limits<int>::max(); |
| 65 | + |
| 66 | + std::vector<std::vector<int>> graph1 = { |
| 67 | + {0, 5, INF, 10}, |
| 68 | + {INF, 0, 3, INF}, |
| 69 | + {INF, INF, 0, 1}, |
| 70 | + {INF, INF, INF, 0} |
| 71 | + }; |
| 72 | + |
| 73 | + std::vector<std::vector<int>> expected1 = { |
| 74 | + {0, 5, 8, 9}, |
| 75 | + {INF, 0, 3, 4}, |
| 76 | + {INF, INF, 0, 1}, |
| 77 | + {INF, INF, INF, 0} |
| 78 | + }; |
| 79 | + |
| 80 | + assert(floyd_warshall(graph1) == expected1); |
| 81 | + |
| 82 | + std::vector<std::vector<int>> graph2 = { |
| 83 | + {0, 1, INF, INF}, |
| 84 | + {INF, 0, -1, INF}, |
| 85 | + {INF, INF, 0, -1}, |
| 86 | + {-1, INF, INF, 0} |
| 87 | + }; |
| 88 | + |
| 89 | + std::vector<std::vector<int>> expected2 = { |
| 90 | + {0, 1, 0, -1}, |
| 91 | + {-1, 0, -1, -2}, |
| 92 | + {-2, -1, 0, -1}, |
| 93 | + {-1, 0, -1, 0} |
| 94 | + }; |
| 95 | + |
| 96 | + assert(floyd_warshall(graph2) == expected2); |
| 97 | + |
| 98 | + std::vector<std::vector<int>> graph3 = {{0}}; |
| 99 | + std::vector<std::vector<int>> expected3 = {{0}}; |
| 100 | + assert(floyd_warshall(graph3) == expected3); |
| 101 | + |
| 102 | + std::cout << "All tests have successfully passed!\n"; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * @brief Main function to run tests |
| 107 | + * @returns 0 on exit |
| 108 | + */ |
| 109 | +int main() { |
| 110 | + tests(); |
| 111 | + return 0; |
| 112 | +} |
0 commit comments