summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGloria Mundi <gloria@gloria-mundi.eu>2025-02-13 21:53:53 +0100
committerGloria Mundi <gloria@gloria-mundi.eu>2025-02-13 21:55:17 +0100
commit8d707127988c2a96d0e182ba8e1520a2b466fc18 (patch)
treef6cd869b1566c8921fd1a36b4370662c2f75b716
parent85150d345a5b2b32ca6dc11e56ea514d4f34a71a (diff)
dinic/capacity scaling: remove dinic.cpp, add comment to dinicScaling.cpp
-rw-r--r--content/graph/dinic.cpp55
-rw-r--r--content/graph/dinicScaling.cpp1
-rw-r--r--test/graph/dinic.cpp62
3 files changed, 1 insertions, 117 deletions
diff --git a/content/graph/dinic.cpp b/content/graph/dinic.cpp
deleted file mode 100644
index c8c34a8..0000000
--- a/content/graph/dinic.cpp
+++ /dev/null
@@ -1,55 +0,0 @@
-struct Edge {
- int to, rev;
- ll f, c;
-};
-
-vector<vector<Edge>> adj;
-int s, t;
-vector<int> pt, dist;
-
-void addEdge(int u, int v, ll c) {
- adj[u].push_back({v, (int)ssize(adj[v]), 0, c});
- adj[v].push_back({u, (int)ssize(adj[u]) - 1, 0, 0});
-}
-
-bool bfs() {
- dist.assign(ssize(adj), -1);
- dist[s] = 0;
- queue<int> q({s});
- while (!q.empty() && dist[t] < 0) {
- int v = q.front(); q.pop();
- for (Edge& e : adj[v]) {
- if (dist[e.to] < 0 && e.c - e.f > 0) {
- dist[e.to] = dist[v] + 1;
- q.push(e.to);
- }}}
- return dist[t] >= 0;
-}
-
-ll dfs(int v, ll flow = INF) {
- if (v == t || flow == 0) return flow;
- for (; pt[v] < ssize(adj[v]); pt[v]++) {
- Edge& e = adj[v][pt[v]];
- if (dist[e.to] != dist[v] + 1) continue;
- ll cur = dfs(e.to, min(e.c - e.f, flow));
- if (cur > 0) {
- e.f += cur;
- adj[e.to][e.rev].f -= cur;
- return cur;
- }}
- return 0;
-}
-
-ll maxFlow(int source, int target) {
- s = source, t = target;
- ll flow = 0;
- while (bfs()) {
- pt.assign(ssize(adj), 0);
- ll cur;
- do {
- cur = dfs(s);
- flow += cur;
- } while (cur > 0);
- }
- return flow;
-}
diff --git a/content/graph/dinicScaling.cpp b/content/graph/dinicScaling.cpp
index 0082c05..fd82296 100644
--- a/content/graph/dinicScaling.cpp
+++ b/content/graph/dinicScaling.cpp
@@ -43,6 +43,7 @@ ll dfs(int v, ll flow) {
ll maxFlow(int source, int target) {
s = source, t = target;
ll flow = 0;
+ // lim = 1 may be faster if capacities are small
for (ll lim = (1LL << 62); lim >= 1; lim /= 2) {
while (bfs(lim)) {
pt.assign(ssize(adj), 0);
diff --git a/test/graph/dinic.cpp b/test/graph/dinic.cpp
deleted file mode 100644
index 5af7c6f..0000000
--- a/test/graph/dinic.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-#include "../util.h"
-constexpr ll INF = LL::INF;
-namespace dinic {
-#include <graph/dinic.cpp>
-}
-
-namespace pushRelabel {
-#include <graph/pushRelabel.cpp>
-}
-
-void stress_test() {
- ll queries = 0;
- for (int tries = 0; tries < 20'000; tries++) {
- int n = Random::integer<int>(2, 30);
- int m = Random::integer<int>(n-1, max<int>(n, min<int>(500, n*(n-1) / 2 + 1)));
-
- dinic::adj.assign(n, {});
- pushRelabel::adj.assign(n, {});
-
- Graph<NoData, true> g(n);
- g.erdosRenyi(m);
- g.forEdges([](int a, int b){
- ll w = Random::integer<ll>(1, 1'000'000'000'000ll);
- dinic::addEdge(a, b, w);
- pushRelabel::addEdge(a, b, w);
- });
-
- ll got = dinic::maxFlow(0, n - 1);
- ll expected = pushRelabel::maxFlow(0, n - 1);
-
- if (got != expected) cerr << "got: " << got << ", expected: " << expected << FAIL;
- queries += n;
- }
- cerr << "tested random queries: " << queries << endl;
-}
-
-constexpr int N = 50000;
-constexpr int M = 200000;
-void performance_test() {
- using namespace dinic;
- timer t;
- Graph<NoData> g(N);
- g.erdosRenyi(M);
- adj.assign(N, {});
- g.forEdges([](int a, int b){
- ll w1 = Random::integer<ll>(1, 1'000'000'000'000ll);
- ll w2 = Random::integer<ll>(1, 1'000'000'000'000ll);
- addEdge(a, b, w1);
- addEdge(b, a, w2);
- });
-
- t.start();
- hash_t hash = maxFlow(0, N - 1);
- t.stop();
- if (t.time > 2000) cerr << "too slow: " << t.time << FAIL;
- cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
-}
-
-int main() {
- stress_test();
- performance_test();
-}