forked from EbTech/rust-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow.rs
224 lines (204 loc) · 7.21 KB
/
flow.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
//! Maximum flows, matchings, and minimum cuts.
use super::{AdjListIterator, Graph};
/// Representation of a network flow problem with (optional) costs.
pub struct FlowGraph {
/// Owned graph, managed by this FlowGraph object.
pub graph: Graph,
/// Edge capacities.
pub cap: Vec<i64>,
/// Edge cost per unit flow.
pub cost: Vec<i64>,
}
impl FlowGraph {
/// An upper limit to the flow.
const INF: i64 = 0x3f3f_3f3f_3f3f_3f3f;
/// Initializes an flow network with vmax vertices and no edges.
pub fn new(vmax: usize, emax_hint: usize) -> Self {
Self {
graph: Graph::new(vmax, 2 * emax_hint),
cap: Vec::with_capacity(2 * emax_hint),
cost: Vec::with_capacity(2 * emax_hint),
}
}
/// Adds an edge with specified capacity and cost. The reverse edge is also
/// added for residual graph computation, but has zero capacity.
pub fn add_edge(&mut self, u: usize, v: usize, cap: i64, cost: i64) {
self.cap.push(cap);
self.cap.push(0);
self.cost.push(cost);
self.cost.push(-cost);
self.graph.add_undirected_edge(u, v);
}
/// Dinic's algorithm to find the maximum flow from s to t where s != t.
/// Generalizes the Hopcroft-Karp maximum bipartite matching algorithm.
/// V^2E in general, min(V^(2/3),sqrt(E))E when all edges are unit capacity,
/// sqrt(V)E when all vertices are unit capacity as in bipartite graphs.
///
/// # Panics
///
/// Panics if the maximum flow is 2^63 or larger.
pub fn dinic(&self, s: usize, t: usize) -> i64 {
let mut flow = vec![0; self.graph.num_e()];
let mut max_flow = 0;
loop {
let dist = self.dinic_search(s, &flow);
if dist[t] == Self::INF {
break;
}
// Keep track of adjacency lists to avoid revisiting blocked edges.
let mut adj_iters = (0..self.graph.num_v())
.map(|u| self.graph.adj_list(u).peekable())
.collect::<Vec<_>>();
max_flow += self.dinic_augment(s, t, Self::INF, &dist, &mut adj_iters, &mut flow);
}
max_flow
}
// Compute BFS distances to restrict attention to shortest path edges.
fn dinic_search(&self, s: usize, flow: &[i64]) -> Vec<i64> {
let mut dist = vec![Self::INF; self.graph.num_v()];
let mut q = ::std::collections::VecDeque::new();
dist[s] = 0;
q.push_back(s);
while let Some(u) = q.pop_front() {
for (e, v) in self.graph.adj_list(u) {
if dist[v] == Self::INF && flow[e] < self.cap[e] {
dist[v] = dist[u] + 1;
q.push_back(v);
}
}
}
dist
}
// Pushes a blocking flow that increases the residual's s-t distance.
fn dinic_augment(
&self,
u: usize,
t: usize,
f: i64,
dist: &[i64],
adj: &mut [::std::iter::Peekable<AdjListIterator>],
flow: &mut [i64],
) -> i64 {
if u == t {
return f;
}
let mut df = 0;
while let Some(&(e, v)) = adj[u].peek() {
let rem_cap = (self.cap[e] - flow[e]).min(f - df);
if rem_cap > 0 && dist[v] == dist[u] + 1 {
let cf = self.dinic_augment(v, t, rem_cap, dist, adj, flow);
flow[e] += cf;
flow[e ^ 1] -= cf;
df += cf;
if df == f {
break;
}
}
// The current edge is either saturated or blocked.
adj[u].next();
}
df
}
/// After running maximum flow, use this to recover the dual minimum cut.
pub fn min_cut(&self, dist: &[i64]) -> Vec<usize> {
(0..self.graph.num_e())
.filter(|&e| {
let u = self.graph.endp[e ^ 1];
let v = self.graph.endp[e];
dist[u] < Self::INF && dist[v] == Self::INF
}).collect()
}
/// Among all s-t maximum flows, finds one with minimum cost, assuming
/// s != t and no negative-cost cycles.
///
/// # Panics
///
/// Panics if the flow or cost overflow a 64-bit signed integer.
pub fn mcf(&self, s: usize, t: usize) -> (i64, i64) {
let mut pot = vec![0; self.graph.num_v()];
// Bellman-Ford deals with negative-cost edges at initialization.
for _ in 1..self.graph.num_v() {
for e in 0..self.graph.num_e() {
if self.cap[e] > 0 {
let u = self.graph.endp[e ^ 1];
let v = self.graph.endp[e];
pot[v] = pot[v].min(pot[u] + self.cost[e]);
}
}
}
let mut flow = vec![0; self.graph.num_e()];
let (mut min_cost, mut max_flow) = (0, 0);
loop {
let par = self.mcf_search(s, &flow, &mut pot);
if par[t] == None {
break;
}
let (dc, df) = self.mcf_augment(t, &par, &mut flow);
min_cost += dc;
max_flow += df;
}
(min_cost, max_flow)
}
// Maintains Johnson's potentials to prevent negative-cost residual edges.
// This allows running Dijkstra instead of the slower Bellman-Ford.
fn mcf_search(&self, s: usize, flow: &[i64], pot: &mut [i64]) -> Vec<Option<usize>> {
let mut vis = vec![false; self.graph.num_v()];
let mut dist = vec![Self::INF; self.graph.num_v()];
let mut par = vec![None; self.graph.num_v()];
dist[s] = 0;
while let Some(u) = (0..self.graph.num_v())
.filter(|&u| !vis[u] && dist[u] < Self::INF)
.min_by_key(|&u| dist[u] - pot[u])
{
vis[u] = true;
pot[u] = dist[u];
for (e, v) in self.graph.adj_list(u) {
if dist[v] > dist[u] + self.cost[e] && flow[e] < self.cap[e] {
dist[v] = dist[u] + self.cost[e];
par[v] = Some(e);
}
}
}
par
}
// Pushes flow along an augmenting path of minimum cost.
fn mcf_augment(&self, t: usize, par: &[Option<usize>], flow: &mut [i64]) -> (i64, i64) {
let (mut dc, mut df) = (0, Self::INF);
let mut u = t;
while let Some(e) = par[u] {
df = df.min(self.cap[e] - flow[e]);
u = self.graph.endp[e ^ 1];
}
u = t;
while let Some(e) = par[u] {
flow[e] += df;
flow[e ^ 1] -= df;
dc += df * self.cost[e];
u = self.graph.endp[e ^ 1];
}
(dc, df)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_basic_flow() {
let mut graph = FlowGraph::new(3, 2);
graph.add_edge(0, 1, 4, 1);
graph.add_edge(1, 2, 3, 1);
let flow = graph.dinic(0, 2);
assert_eq!(flow, 3);
}
#[test]
fn test_min_cost_flow() {
let mut graph = FlowGraph::new(4, 4);
graph.add_edge(0, 1, 10, -10);
graph.add_edge(1, 2, 7, 8);
graph.add_edge(2, 3, 7, 8);
graph.add_edge(1, 3, 7, 10);
let (cost, flow) = graph.mcf(0, 3);
assert_eq!(cost, 18);
assert_eq!(flow, 10);
}
}