最大流与最小割
Maxflow & Mincut
在了解最大流最小割定理前,我们先对最大流问题与最小割问题进行一个简单的介绍。
Mincut Problem
对于一个边权为正的 edge-weighted digraph,给出一个 source vertex
Def. A
关于 cut 的定义在之前 MST 的部分也介绍过。对于最小割问题我们引入一个 cut 的容量(capacity):即从一个点集到另一个点集间所有边的边权之和。
据此我们定义最小割问题:Minumum st-cut (mincut) problem. Find a cut of minimum capacity.
冷战期间,对于苏联-东欧的铁路网,我们假设破坏一个铁路线的代价是该铁路线的容量。西方世界想要以最小的代价切断两地的物资供应:这个问题的模型就是最小割问题。
Maxflow Problem
同样,对于一个有 source vertex 与 target vertex 的正边权 edge-weighted digraph 我们定义流:
Def. An
- Capacity constraint: 0
edge's flow edge's capacity. - Local equilibrium: inflow = outflow at every vertex (except
and ).
The value of a flow is the inflow at
流是一种分配方式(assignment of values),而流的值即最后流入 target vertex 的值之和。据此我们定义最大流问题:Maximum st-flow (maxflow) problem. Find a flow of maximum value.
冷战期间,对于苏联-东欧的铁路网,每条铁路都有其容量上限。苏联方想要找到一种运输方式,使得由苏联运送到东欧的物资总量最大化,这个问题的模型即为最大流问题。
Ford-Fulkerson Algorithm
Ford-Fulkerson 算法是求解最大流问题的经典算法,它通过不断寻找图中可能的 augmenting paths(增广路)试图增加 flow 的值。本质上它是一个贪心算法,但通过 backward edges 引入了一个类似后悔的机制。
Initialization. Start with 0 flow.
在初始化之后,Ford-Fulkerson 算法开始不断寻找图中的 augmenting paths(增广路)。
Augmenting Path. An undirected path from
- Can increase flow on forward edges (not full).
- Can decrease flow on backward edges (not empty).
注意增广路是 undirected 的:因此一条增广路可能包含若干条 forward edges 与若干条 backward edges。forward edges 是增广路中指向终点
In an augmenting path, we greedily increase the flow in all forward edges that are not full and decrease the flow in all backward edges that are not empty.
如图所示,在两次增广过后,Ford-Fulkerson 算法找到了第三条增广路,它包括四条 forward edges 与一条 backward edge。在 forward edges 上我们贪心的增加流量,在 backward edge 上我们贪心的减少流量。
在 local equilibrium 条件下我们得以计算出这条增广路的 bottleneck 是 5;因此它将为最大流贡献 5 流量。
Termination. All paths from
- Full forward edge.
- Empty backward edge.
如图,当图中没有增广路时,Ford-Fulkerson 算法将中止。而没有增广路的表现是,从
Running Time Analysis
Integrity theorem: There exists an integer-valued maxflow (and FF finds one).
一个图中的增广路数量
此外,对于一些特殊的构造,FF 算法可能显得比较低效。例如:
对于该图,朴素的 FF 算法在 100 次增广后才能计算出最大流(其不断以 S 型与倒 S 型这两种 bottleneck capacity 为 1 的增广路进行增广)。解决的方法是采用特殊的增广路选取方式。
- shortest path: augmenting path with fewest number of edges. number of paths
. - fattest path: augmenting path with maximum bottleneck capacity. number of paths
.
Java Implementation
定义一系列 flow network representation。在 flow edge data type 中保存 flow
定义一条边的 residual capacity(残留容量)为:
- Forward edge: residual capacity =
. - Backward edge: residual capacity =
.
如此一来,对于一条增广路,其 Augment flow 的计算方式为:
- Forward edge: add
(residual capacity). - Backward edge: substract
(residual capacity).
在引入了 residual capacity 的概念之后,我们可以基于此构造 residual network(残留网络)。在原始网络中, augmenting paths 是 undirected 的,而在 residual network 中 augmenting paths 是 directed 的。
首先是 FlowEdge datatype 的实现:
public class FlowEdge {
private final int v, w; // from and to
private final double capacity;
private double flow; // initialized as 0
public FlowEdge(int v, int w, double capacity) {
this.v = v;
this.w = w;
this.capacity = capacity;
}
public int from() { return v; }
public int to() { return w; }
public double capacity() { return capacity; }
public int other(int vertex) {
if (vertex == v) return w;
else if (vertex == w) return v;
else throw new IllegalArgumentException();
}
public double residualCapacityTo(int vertex) {
if (vertex == v) return flow; // backward edge
else if (vertex == w) return capacity - flow; // forward edge
else throw new IllegalArgumentException();
}
public void addResidualFlowTo(int vertex, double delta) {
if (vertex == v) flow -= delta; // backward edge
else if (vertex == w) flow += delta; // forward edge
else throw new IllegalArgumentException();
}
}
接下来是 FlowNetwork datatype:其实现与 EdgeWeightedGraph datatype 的实现基本相同。注意,虽然边是 directed 的,但我们却需要将其同时加入
public class FlowNetwork {
private final int V;
private Bag<FlowEdge>[] adj;
public FlowNetwork(int V) {
this.V = V;
adj = (Bag<FlowEdge>[]) new Bag[V];
for (int v = 0; v < V; ++v)
adj[v] = new Bag<FlowEdge>();
}
public void addEdge(FlowEdge e) {
int v = e.from();
int w = e.to();
adj[v].add(e); // add forward edge
adj[w].add(e); // add backward edge
}
public Iterable<FlowEdge> adj(int v) {
return adj[v];
}
}
最后是 FordFulkerson datatype:并且我们采用 shortest path 策略来寻找增广路。在网络流中边权的意义是容量(capacity)而非距离。在距离意义上,每条边的长度都是 1;因此使用 BFS 即可保证找到的增广路最短。
public class FordFulkerson {
private boolean[] marked; // true if s->v path in residual network
private FlowEdge[] edgeTo; // last edge on s->v path
private double value; // value of the flow
public FordFulkerson(FlowNetwork G, int s, int t) {
value = 0.0;
while (hasAugmentingPath(G, s, t)) {
double bottleneck = Double.POSITIVE_INFINITY;
// 遍历找到的增广路并计算 bottleneck capacity
for (int v = t; v != s; v = edgeTo[v].other(v))
bottle = Math.min(bottleneck, edgeTo[v].residualCapacityTo(v));
// 遍历找到的增广路并对每一条边进行增广 (更新 flow)
for (int v = t; v != s; v = edgeTo[v].other(v))
edgeTo[v].addResidualFlowTo(v, bottleneck);
value += bottleneck;
}
}
private boolean hasAugmentingPath(FlowNetwork G, int s, int t) {
edgeTo = new FlowEdge[G.V()];
marked = new boolean[G.V()];
Queue<Integer> queue = new Queue<Integer>(); // 采用 BFS 寻找增广路
queue.enqueue(s);
marked[s] = true;
while (!queue.isEmpty()) {
int v = queue.dequeue();
for (FlowEdge e : G.adj(v)) {
int w = e.other(v);
if (e.residualCapacity(w) > 0 && !marked[w]) {
edgeTo[w] = e;
marked[w] = true;
queue.enqueue(w);
}
}
}
// if t reachable from s in residual network, an augmenting path found
return marked[t];
}
public double value() { return value; }
// is v reachable from s in residual network?
public boolean inCut(int v) { return marked[v]; }
}
注意 inCut 函数:该函数在 FF 算法结束后被调用,判断在 residual network 中
之前我们讲过,FF 算法结束后,网络中的点将会被分为两个点集,这两个点集被一系列 full forward edges 与 empty backward edges 分隔开来,因此在该 residual network 中不存在任何新的增广路。此外,源点
inCut 函数的作用就是描述这两个点集的划分,即 cut:若 inCut(v) 为真,说明
更重要的是,该 cut 恰恰是这个网络的 mincut(最小割)!这绝非偶然:事实上,maxflow(最大流)与 mincut(最小割)问题是等价的(dual problems)。我们将通过介绍 maxflow-mincut theorem 来说明这一点。
Maxflow-Mincut Theorem
Flow-value Lemma
首先我们引入 flow-value lemma:这一 lemma 将 flows 与 cuts 这两个概念联系起来。
Flow-value lemma. Let
Def. The net flow across a cut
Flow-value lemma 的证明很符合直觉:它是由流的 local equilibrium 保证的。通过 flow-value lemma 我们还能得出一个有用的结论:Outflow from
Weak Duality
Weak duality. Let
Flow-value lemma 决定了第一个等式成立;flow bounded by capacity 决定了第二个不等式成立。这一性质称为 weak duality:true duality 由 maxflow-mincut theorem 决定。
True Duality
Augmenting path theorem. A flow
Maxflow-mincut theorem. Value of the maxflow = Capacity of mincut.
Pf. The following three conditions are equivalent for any flow
- There exists a cut whose capacity equals the value of the flow
. is a maxflow. - There is no augmenting path with repect to
.
其中 ii
- Suppose that
is a cut with capacity equal to the value of . - Then, the value of any flow
capacity of = value of . (weak duality + assumption) - Thus,
is a maxflow.
据此,通过 FF 算法求最小割的正确性得以证明(具体实现见 Ford-Fulkerson 部分):
- By augmenting path theorem, no augmenting paths with respect to
. - Compute
set of vertices connected to by an undirected path with no full forward or empty backward edges.



