I'm beginner in programming and I'm learning algorithms to find maximum flows.
Most of them are rather difficult such as Ford-Falkerson, Edmunds-Karp and Dinitz. The problem is here: https://cses.fi/problemset/task/1694
I found an algorithm that finds maximum flow just for one depth first search for O(n+m). What is the name or author of this algorithm? This solution uses just one DFS. All standard algorithms use many DFS or BFS searches, but not this. I'm a bit confused.
#include<bits/stdc++.h>
using namespace std;
vector<vector<pair<int, long long>>> adj;
vector<bool> visited;
long long dfs(int to) {
long long r = 0;
visited[to] = true;
for (const auto& [from, flow]: adj[to]) {
if (from == 1 || visited[from]) {
r += flow;
} else {
r += min(flow, dfs(from));
}
}
return r;
}
int main() {
int n, m;
cin >> n >> m;
adj.resize(n + 1);
visited.resize(n + 1, false);
for (int i = 0; i < m; i++) {
int from, to, flow;
cin >> from >> to >> flow;
adj[to].push_back({from, flow});
}
cout << dfs(n);
}
Can you help me to understand why Dinitz or Edmunds-Karp, with O(m2n) complexity, are needed?
Breaks on the following graph since the second visit to v will incorrectly assume that there is one unit of flow available to pull: