Skip to content

Commit 245b998

Browse files
committed
Floyd warshall
1 parent 94a8017 commit 245b998

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

graph/.vscode/settings.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"files.associations": {
3+
"array": "cpp",
4+
"atomic": "cpp",
5+
"bit": "cpp",
6+
"*.tcc": "cpp",
7+
"bitset": "cpp",
8+
"cctype": "cpp",
9+
"clocale": "cpp",
10+
"cmath": "cpp",
11+
"compare": "cpp",
12+
"concepts": "cpp",
13+
"cstdarg": "cpp",
14+
"cstddef": "cpp",
15+
"cstdint": "cpp",
16+
"cstdio": "cpp",
17+
"cstdlib": "cpp",
18+
"cstring": "cpp",
19+
"cwchar": "cpp",
20+
"cwctype": "cpp",
21+
"deque": "cpp",
22+
"list": "cpp",
23+
"map": "cpp",
24+
"set": "cpp",
25+
"string": "cpp",
26+
"unordered_map": "cpp",
27+
"vector": "cpp",
28+
"exception": "cpp",
29+
"algorithm": "cpp",
30+
"functional": "cpp",
31+
"iterator": "cpp",
32+
"memory": "cpp",
33+
"memory_resource": "cpp",
34+
"numeric": "cpp",
35+
"random": "cpp",
36+
"string_view": "cpp",
37+
"system_error": "cpp",
38+
"tuple": "cpp",
39+
"type_traits": "cpp",
40+
"utility": "cpp",
41+
"initializer_list": "cpp",
42+
"iosfwd": "cpp",
43+
"iostream": "cpp",
44+
"istream": "cpp",
45+
"limits": "cpp",
46+
"new": "cpp",
47+
"numbers": "cpp",
48+
"ostream": "cpp",
49+
"stdexcept": "cpp",
50+
"streambuf": "cpp",
51+
"typeinfo": "cpp"
52+
}
53+
}

graph/floyd_warshall.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)