-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmaxflow.rs
334 lines (312 loc) · 9.57 KB
/
maxflow.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#![allow(dead_code)]
use crate::internal_queue::SimpleQueue;
use crate::internal_type_traits::Integral;
use std::cmp::min;
use std::iter;
impl<Cap> MfGraph<Cap>
where
Cap: Integral,
{
pub fn new(n: usize) -> MfGraph<Cap> {
MfGraph {
_n: n,
pos: Vec::new(),
g: iter::repeat_with(Vec::new).take(n).collect(),
}
}
pub fn add_edge(&mut self, from: usize, to: usize, cap: Cap) -> usize {
assert!(from < self._n);
assert!(to < self._n);
assert!(Cap::zero() <= cap);
let m = self.pos.len();
self.pos.push((from, self.g[from].len()));
let rev = self.g[to].len() + usize::from(from == to);
self.g[from].push(_Edge { to, rev, cap });
let rev = self.g[from].len() - 1;
self.g[to].push(_Edge {
to: from,
rev,
cap: Cap::zero(),
});
m
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct Edge<Cap: Integral> {
pub from: usize,
pub to: usize,
pub cap: Cap,
pub flow: Cap,
}
impl<Cap> MfGraph<Cap>
where
Cap: Integral,
{
pub fn get_edge(&self, i: usize) -> Edge<Cap> {
let m = self.pos.len();
assert!(i < m);
let _e = &self.g[self.pos[i].0][self.pos[i].1];
let _re = &self.g[_e.to][_e.rev];
Edge {
from: self.pos[i].0,
to: _e.to,
cap: _e.cap + _re.cap,
flow: _re.cap,
}
}
pub fn edges(&self) -> Vec<Edge<Cap>> {
let m = self.pos.len();
(0..m).map(|i| self.get_edge(i)).collect()
}
pub fn change_edge(&mut self, i: usize, new_cap: Cap, new_flow: Cap) {
let m = self.pos.len();
assert!(i < m);
assert!(Cap::zero() <= new_flow && new_flow <= new_cap);
let (to, rev) = {
let _e = &mut self.g[self.pos[i].0][self.pos[i].1];
_e.cap = new_cap - new_flow;
(_e.to, _e.rev)
};
let _re = &mut self.g[to][rev];
_re.cap = new_flow;
}
/// `s != t` must hold, otherwise it panics.
pub fn flow(&mut self, s: usize, t: usize) -> Cap {
self.flow_with_capacity(s, t, Cap::max_value())
}
/// # Parameters
/// * `s != t` must hold, otherwise it panics.
/// * `flow_limit >= 0`
pub fn flow_with_capacity(&mut self, s: usize, t: usize, flow_limit: Cap) -> Cap {
let n_ = self._n;
assert!(s < n_);
assert!(t < n_);
// By the definition of max flow in appendix.html, this function should return 0
// when the same vertices are provided. On the other hand, it is reasonable to
// return infinity-like value too, which is what the original implementation
// (and this implementation without the following assertion) does.
// Since either return value is confusing, we'd rather deny the parameters
// of the two same vertices.
// For more details, see https://github.com/rust-lang-ja/ac-library-rs/pull/24#discussion_r485343451
// and https://github.com/atcoder/ac-library/issues/5 .
assert_ne!(s, t);
// Additional constraint
assert!(Cap::zero() <= flow_limit);
let mut calc = FlowCalculator {
graph: self,
s,
t,
flow_limit,
level: vec![0; n_],
iter: vec![0; n_],
que: SimpleQueue::default(),
};
let mut flow = Cap::zero();
while flow < flow_limit {
calc.bfs();
if calc.level[t] == -1 {
break;
}
calc.iter.iter_mut().for_each(|e| *e = 0);
while flow < flow_limit {
let f = calc.dfs(t, flow_limit - flow);
if f == Cap::zero() {
break;
}
flow += f;
}
}
flow
}
pub fn min_cut(&self, s: usize) -> Vec<bool> {
let mut visited = vec![false; self._n];
let mut que = SimpleQueue::default();
que.push(s);
while let Some(&p) = que.pop() {
visited[p] = true;
for e in &self.g[p] {
if e.cap != Cap::zero() && !visited[e.to] {
visited[e.to] = true;
que.push(e.to);
}
}
}
visited
}
}
struct FlowCalculator<'a, Cap> {
graph: &'a mut MfGraph<Cap>,
s: usize,
t: usize,
flow_limit: Cap,
level: Vec<i32>,
iter: Vec<usize>,
que: SimpleQueue<usize>,
}
impl<Cap> FlowCalculator<'_, Cap>
where
Cap: Integral,
{
fn bfs(&mut self) {
self.level.iter_mut().for_each(|e| *e = -1);
self.level[self.s] = 0;
self.que.clear();
self.que.push(self.s);
while !self.que.empty() {
let v = *self.que.front().unwrap();
self.que.pop();
for e in &self.graph.g[v] {
if e.cap == Cap::zero() || self.level[e.to] >= 0 {
continue;
}
self.level[e.to] = self.level[v] + 1;
if e.to == self.t {
return;
}
self.que.push(e.to);
}
}
}
fn dfs(&mut self, v: usize, up: Cap) -> Cap {
if v == self.s {
return up;
}
let mut res = Cap::zero();
let level_v = self.level[v];
for i in self.iter[v]..self.graph.g[v].len() {
self.iter[v] = i;
let &_Edge {
to: e_to,
rev: e_rev,
..
} = &self.graph.g[v][i];
if level_v <= self.level[e_to] || self.graph.g[e_to][e_rev].cap == Cap::zero() {
continue;
}
let d = self.dfs(e_to, min(up - res, self.graph.g[e_to][e_rev].cap));
if d <= Cap::zero() {
continue;
}
self.graph.g[v][i].cap += d;
self.graph.g[e_to][e_rev].cap -= d;
res += d;
if res == up {
return res;
}
}
self.iter[v] = self.graph.g[v].len();
res
}
}
#[derive(Default)]
pub struct MfGraph<Cap> {
_n: usize,
pos: Vec<(usize, usize)>,
g: Vec<Vec<_Edge<Cap>>>,
}
struct _Edge<Cap> {
to: usize,
rev: usize,
cap: Cap,
}
#[cfg(test)]
mod test {
use crate::{Edge, MfGraph};
#[test]
fn test_max_flow_wikipedia() {
// From https://commons.wikimedia.org/wiki/File:Min_cut.png
// Under CC BY-SA 3.0 https://creativecommons.org/licenses/by-sa/3.0/deed.en
let mut graph = MfGraph::new(6);
assert_eq!(graph.add_edge(0, 1, 3), 0);
assert_eq!(graph.add_edge(0, 2, 3), 1);
assert_eq!(graph.add_edge(1, 2, 2), 2);
assert_eq!(graph.add_edge(1, 3, 3), 3);
assert_eq!(graph.add_edge(2, 4, 2), 4);
assert_eq!(graph.add_edge(3, 4, 4), 5);
assert_eq!(graph.add_edge(3, 5, 2), 6);
assert_eq!(graph.add_edge(4, 5, 3), 7);
assert_eq!(graph.flow(0, 5), 5);
let edges = graph.edges();
{
#[rustfmt::skip]
assert_eq!(
edges,
vec![
Edge { from: 0, to: 1, cap: 3, flow: 3 },
Edge { from: 0, to: 2, cap: 3, flow: 2 },
Edge { from: 1, to: 2, cap: 2, flow: 0 },
Edge { from: 1, to: 3, cap: 3, flow: 3 },
Edge { from: 2, to: 4, cap: 2, flow: 2 },
Edge { from: 3, to: 4, cap: 4, flow: 1 },
Edge { from: 3, to: 5, cap: 2, flow: 2 },
Edge { from: 4, to: 5, cap: 3, flow: 3 },
]
);
}
assert_eq!(
graph.min_cut(0),
vec![true, false, true, false, false, false]
);
}
#[test]
fn test_max_flow_wikipedia_multiple_edges() {
// From https://commons.wikimedia.org/wiki/File:Min_cut.png
// Under CC BY-SA 3.0 https://creativecommons.org/licenses/by-sa/3.0/deed.en
let mut graph = MfGraph::new(6);
for &(u, v, c) in &[
(0, 1, 3),
(0, 2, 3),
(1, 2, 2),
(1, 3, 3),
(2, 4, 2),
(3, 4, 4),
(3, 5, 2),
(4, 5, 3),
] {
for _ in 0..c {
graph.add_edge(u, v, 1);
}
}
assert_eq!(graph.flow(0, 5), 5);
assert_eq!(
graph.min_cut(0),
vec![true, false, true, false, false, false]
);
}
#[test]
#[allow(clippy::many_single_char_names)]
fn test_max_flow_misawa() {
// Originally by @MiSawa
// From https://gist.github.com/MiSawa/47b1d99c372daffb6891662db1a2b686
let n = 100;
let mut graph = MfGraph::new((n + 1) * 2 + 5);
let (s, a, b, c, t) = (0, 1, 2, 3, 4);
graph.add_edge(s, a, 1);
graph.add_edge(s, b, 2);
graph.add_edge(b, a, 2);
graph.add_edge(c, t, 2);
for i in 0..n {
let i = 2 * i + 5;
for j in 0..2 {
for k in 2..4 {
graph.add_edge(i + j, i + k, 3);
}
}
}
for j in 0..2 {
graph.add_edge(a, 5 + j, 3);
graph.add_edge(2 * n + 5 + j, c, 3);
}
assert_eq!(graph.flow(s, t), 2);
}
#[test]
fn test_dont_repeat_same_phase() {
let n = 100_000;
let mut graph = MfGraph::new(3);
graph.add_edge(0, 1, n);
for _ in 0..n {
graph.add_edge(1, 2, 1);
}
assert_eq!(graph.flow(0, 2), n);
}
}