最大流与最小割

Maxflow & Mincut

在了解最大流最小割定理前,我们先对最大流问题与最小割问题进行一个简单的介绍。

Mincut Problem

对于一个边权为正的 edge-weighted digraph,给出一个 source vertex ss 与 target vertex tt.

Def. A stst-cut (cut) is a partition of the vertices into two disjoint sets, with ss in one set AA and tt in the other set BB. Its capacity is the sum of the capacities of the edges from AA to BB.

关于 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 stst-flow (flow) is an assignment of values to the edges such that:

  • Capacity constraint: 0 ≤\leq edge's flow ≤\leq edge's capacity.
  • Local equilibrium: inflow = outflow at every vertex (except ss and tt).

The value of a flow is the inflow at tt.

流是一种分配方式(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 ss to tt such that:

  • Can increase flow on forward edges (not full).
  • Can decrease flow on backward edges (not empty).

注意增广路是 undirected 的:因此一条增广路可能包含若干条 forward edges 与若干条 backward edges。forward edges 是增广路中指向终点 tt 的边;而 backward edges 是增广路中指向源点 ss 的边。

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 流量。

Algorithm ternimation.

Termination. All paths from ss to tt are blocked by either a

  • Full forward edge.
  • Empty backward edge.

如图,当图中没有增广路时,Ford-Fulkerson 算法将中止。而没有增广路的表现是,从 ss 到 tt 的所有无向路径都将被以上两种边所阻隔。换句话讲,所有存在 full forward edges 与 empty backward edges 的增广路的 bottleneck capacity 都是 0,无法为最大流做出任何贡献。

Running Time Analysis

Integrity theorem: There exists an integer-valued maxflow (and FF finds one).

一个图中的增广路数量 ≤\leq 最大流的值:这是因为 Each augmentation increases the value by at least 1.

此外,对于一些特殊的构造,FF 算法可能显得比较低效。例如:

对于该图,朴素的 FF 算法在 100 次增广后才能计算出最大流(其不断以 S 型与倒 S 型这两种 bottleneck capacity 为 1 的增广路进行增广)。解决的方法是采用特殊的增广路选取方式。VV vertices, EE edges, integer capacities between 11 and UU.

  • shortest path: augmenting path with fewest number of edges. number of paths ≤12EV\leq \frac{1}{2}EV.
  • fattest path: augmenting path with maximum bottleneck capacity. number of paths ≤Eln⁡EU\leq E\ln{EU}.

Java Implementation

定义一系列 flow network representation。在 flow edge data type 中保存 flow fef_e 与 capacity cec_e。

定义一条边的 residual capacity(残留容量)为:

  • Forward edge: residual capacity = ce−fec_e-f_e.
  • Backward edge: residual capacity = fef_e.

如此一来,对于一条增广路,其 Augment flow 的计算方式为:

  • Forward edge: add Δ\Delta (residual capacity).
  • Backward edge: substract Δ\Delta (residual capacity).

在引入了 residual capacity 的概念之后,我们可以基于此构造 residual network(残留网络)。在原始网络中, augmenting paths 是 undirected 的,而在 residual network 中 augmenting paths 是 directed 的。

首先是 FlowEdge datatype 的实现:

java
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 的,但我们却需要将其同时加入 vv 与 ww 的邻接表中(这是 undirected edge 的加边方式);这是由增广路的 undirected 性质决定的。因此 vv 中的边为 forward edge,ww 中的边为 backward edge。

java
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 即可保证找到的增广路最短。

java
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 中 ss 是否能到达 vv。

之前我们讲过,FF 算法结束后,网络中的点将会被分为两个点集,这两个点集被一系列 full forward edges 与 empty backward edges 分隔开来,因此在该 residual network 中不存在任何新的增广路。此外,源点 ss 与终点 tt 在 FF 算法结束后的 residual network 中一定分属不同的点集;如果它们仍然属于同一个点集,那么一定存在新的增广路。

inCut 函数的作用就是描述这两个点集的划分,即 cut:若 inCut(v) 为真,说明 vv 处于源点 ss 所在的点集内;反之说明 vv 处于终点 tt 所在的点集内。

更重要的是,该 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 ff be any flow and let (A,B)(A,B) be any cut. Then, the net flow across (A,B)(A,B) equals the value of ff.

Def. The net flow across a cut (A,B)(A,B) is the sum of the flows on its edges from AA to BB minus the flows on its edges from BB to AA.

Flow-value lemma 的证明很符合直觉:它是由流的 local equilibrium 保证的。通过 flow-value lemma 我们还能得出一个有用的结论:Outflow from ss = inflow to tt = value of flow ff.

Weak Duality

Weak duality. Let ff be any flow and let (A,B)(A,B) be any cut. Then the value of the flow == net flow across cut (A,B)(A,B) ≤\leq capacity of cut (A,B)(A,B).

Flow-value lemma 决定了第一个等式成立;flow bounded by capacity 决定了第二个不等式成立。这一性质称为 weak duality:true duality 由 maxflow-mincut theorem 决定。

True Duality

Augmenting path theorem. A flow ff is a maxflow iff no augmenting paths.

Maxflow-mincut theorem. Value of the maxflow = Capacity of mincut.

Pf. The following three conditions are equivalent for any flow ff:

  1. There exists a cut whose capacity equals the value of the flow ff.
  2. ff is a maxflow.
  3. There is no augmenting path with repect to ff.

其中 ii →\to iii 与 iii →\to i 的证明比较 trivial,联想 FF 算法的原理即可。需要稍作说明的是 i →\to ii:

  • Suppose that (A,B)(A,B) is a cut with capacity equal to the value of ff.
  • Then, the value of any flow f′≤f'\leq capacity of (A,B)(A,B) = value of ff. (weak duality + assumption)
  • Thus, ff is a maxflow.

据此,通过 FF 算法求最小割的正确性得以证明(具体实现见 Ford-Fulkerson 部分):

  • By augmenting path theorem, no augmenting paths with respect to ff.
  • Compute A=A= set of vertices connected to ss by an undirected path with no full forward or empty backward edges.
CC BY-NC-SA 4.0
正在加载评论…