TExcelTAD
Home › Linear Programming & Duality for Algorithms › Approximately Solving LPs and Flows with MWU

Approximately Solving LPs and Flows with MWU

The minimax proof of the previous lesson had a remarkable side effect: it solved an LP (the game's) to accuracy ε\varepsilon using nothing but matrix-vector products and a best-response oracle, without simplex, ellipsoids or interior points. The same idea works for large classes of LPs. Keep one weight per constraint, ask an oracle for a point that satisfies the weighted average constraint, increase the weights of the constraints that point violates, and average the points. This is the framework of Plotkin, Shmoys and Tardos (1991; journal version 1995), Grigoriadis and Khachiyan (1994), Young (1995) and Garg and Könemann (1998), unified in Arora, Hazan and Kale's survey (2012). This lesson states the general theorem, implements it for maximum flow with a shortest-path oracle, and sketches how electrical flows took the idea to the fastest approximate max-flow algorithms.

The framework

We want to decide approximately whether some xx in a convex set PP (simple, with an efficient oracle) satisfies mm "hard" linear constraints Ax≤bAx \le b. The oracle gets a probability vector pp over the constraints and must either

  • return x∈Px \in P with pTAx≤pTbp^T A x \le p^T b (the weighted average constraint holds), or
  • report that no such xx exists, which proves that the system is infeasible: any feasible xx would satisfy every non-negative combination of the constraints, in particular this one.

The width ρ\rho bounds how much any oracle answer can violate or satisfy a constraint: ∣aiTx−bi∣≤ρ\lvert a_i^T x - b_i \rvert \le \rho for all ii and all oracle outputs xx.

text
MWU-FEASIBILITY(oracle, rho, eps)
1  w_i = 1 for every constraint i;   T = ceil(4 rho^2 ln(m) / eps^2);   eta = eps / (2 rho)
2  for t = 1 .. T
3      p = w / sum(w)
4      x(t) = ORACLE(p);  if the oracle fails: return INFEASIBLE (certificate p)
5      for every constraint i:  w_i = w_i * (1 + eta * (a_i . x(t) - b_i) / rho)    -- violated ones gain weight
6  return x_bar = average of x(1..T)

Theorem (in the form of Arora, Hazan and Kale). If the oracle never fails, the average xˉ∈P\bar x \in P satisfies aiTxˉ≤bi+εa_i^T \bar x \le b_i + \varepsilon for every constraint ii, after T=O(ρ2ln⁡m/ε2)T = O(\rho^2 \ln m / \varepsilon^2) oracle calls.

Proof sketch. Run MWU with the constraints as decisions and costs mi(t)=−(aiTx(t)−bi)/ρ∈[−1,1]m_i^{(t)} = -(a_i^T x^{(t)} - b_i)/\rho \in [-1, 1] (a violated constraint has negative cost, so its weight grows). The oracle guarantees ∑ipi(t)(aiTx(t)−bi)≤0\sum_i p_i^{(t)} (a_i^T x^{(t)} - b_i) \le 0, so the algorithm's cost is ≥0\ge 0 every round. The regret theorem then says that, for every constraint ii, 0≤∑tmi(t)+ηT+ln⁡mη0 \le \sum_t m_i^{(t)} + \eta T + \frac{\ln m}{\eta}, that is, 1T∑t(aiTx(t)−bi)≤ρ(η+ln⁡mηT)\frac{1}{T} \sum_t (a_i^T x^{(t)} - b_i) \le \rho (\eta + \frac{\ln m}{\eta T}). With the stated η\eta and TT the right side is at most ε\varepsilon, and the left side is aiTxˉ−bia_i^T \bar x - b_i by linearity. □\square

Optimization reduces to feasibility by binary search on the objective value (add "cTx≥c^T x \ge guess" to PP). The running time depends on ρ2/ε2\rho^2 / \varepsilon^2, not on the bit length: these are approximation schemes, fast when ε\varepsilon is moderate and the width is small. Much of the art (Garg-Könemann, Fleischer) consists of reducing the width, for instance by scaling or by restricting the oracle's answers.

Maximum flow with a shortest-path oracle

Decide whether FF units can be sent from ss to tt within capacities. Let PP be the set of flows of value FF that ignore the capacities: every such flow is a mix of FF units sent along single ss-tt paths. The hard constraints are the congestions fe/ue≤1f_e / u_e \le 1, one per arc.

  • Oracle. Given weights pep_e on the arcs, minimize ∑epefe/ue\sum_e p_e f_e / u_e over PP: send all FF units along a shortest path for the lengths pe/uep_e / u_e. If even that path has F⋅dist⁡(s,t)>1=∑epeF \cdot \operatorname{dist}(s, t) > 1 = \sum_e p_e, no flow of value FF fits, and the lengths pe/uep_e / u_e are a fractional cut certificate.
  • Width. A path flow has congestion at most F/umin⁡F / u_{\min} on any arc, so ρ=F/umin⁡\rho = F / u_{\min}.

The program runs this on the network of Max-Flow Min-Cut as LP Duality, whose maximum flow is 5, for F=4,5,6F = 4, 5, 6 and ε=0.1\varepsilon = 0.1.

python · try it yourself
import math

arcs = [("s", "a", 3), ("s", "b", 2), ("a", "b", 1), ("a", "t", 2), ("b", "t", 3)]
V = ["s", "a", "b", "t"]

def shortest_path(length):
    dist, pred = {v: math.inf for v in V}, {}
    dist["s"] = 0.0
    for _ in range(len(V) - 1):                       # Bellman-Ford, enough for 4 vertices
        for k, (u, v, _) in enumerate(arcs):
            if dist[u] + length[k] < dist[v] - 1e-15:
                dist[v], pred[v] = dist[u] + length[k], k
    path, x = [], "t"
    while x != "s":
        path.append(pred[x])
        x = arcs[pred[x]][0]
    return dist["t"], path

def mwu_flow(F, eps=0.1):
    m = len(arcs)
    rho = F / min(c for _, _, c in arcs)
    T = math.ceil(4 * rho * rho * math.log(m) / eps ** 2)
    eta = eps / (2 * rho)
    w, avg = [1.0] * m, [0.0] * m
    for t in range(T):
        W = sum(w)
        p = [x / W for x in w]
        dist, path = shortest_path([p[k] / arcs[k][2] for k in range(m)])
        if F * dist > 1 + 1e-12:
            return f"F = {F}: infeasible, certified in round {t + 1} (F * dist = {F * dist:.4f} > 1)"
        load = [F / arcs[k][2] if k in path else 0.0 for k in range(m)]
        avg = [a + l / T for a, l in zip(avg, load)]
        w = [w[k] * (1 + eta * (load[k] - 1) / rho) for k in range(m)]
    return f"F = {F}: {T} rounds, max congestion of the average flow {max(avg):.4f} (<= 1 + eps)"

for F in [4, 5, 6]:
    print(mwu_flow(F))
F = 4: 10301 rounds, max congestion of the average flow 0.8077 (<= 1 + eps)
F = 5: 16095 rounds, max congestion of the average flow 1.0076 (<= 1 + eps)
F = 6: infeasible, certified in round 3 (F * dist = 1.0008 > 1)

For F=4F = 4 and F=5F = 5 the averaged flow overloads no arc by more than ε\varepsilon (at F=5F = 5, the true maximum, the excess is under 1%). For F=6F = 6 the oracle fails in the third round: with nearly uniform weights, every path is too "long", which proves that no flow of value 6 exists. Each round costs one shortest-path computation, so for ε\varepsilon fixed the method is fast; the number of rounds grows with ρ2\rho^2, the reason practical versions (Garg and Könemann, 1998) route flow in many small steps instead.

In C++

The same loop with arcs as index arrays.

cpp · try it yourself
#include <cmath>
#include <cstdio>
#include <vector>

const int NV = 4, M = 5;                  // vertices s=0, a=1, b=2, t=3
const int from[M] = {0, 0, 1, 1, 2}, to[M] = {1, 2, 2, 3, 3};
const double cap[M] = {3, 2, 1, 2, 3};

double shortest(const std::vector<double>& len, std::vector<int>& path) {
    std::vector<double> d(NV, 1e18);
    std::vector<int> pred(NV, -1);
    d[0] = 0;
    for (int it = 0; it < NV - 1; it++)
        for (int k = 0; k < M; k++)
            if (d[from[k]] + len[k] < d[to[k]] - 1e-15) { d[to[k]] = d[from[k]] + len[k]; pred[to[k]] = k; }
    path.clear();
    for (int x = 3; x != 0; x = from[pred[x]]) path.push_back(pred[x]);
    return d[3];
}

void run(double F, double eps) {
    double rho = F / 1.0;                                          // the smallest capacity is 1
    int T = (int)std::ceil(4 * rho * rho * std::log(M) / (eps * eps));
    double eta = eps / (2 * rho);
    std::vector<double> w(M, 1.0), avg(M, 0.0), len(M);
    std::vector<int> path;
    for (int t = 0; t < T; t++) {
        double W = 0;
        for (double x : w) W += x;
        for (int k = 0; k < M; k++) len[k] = w[k] / W / cap[k];
        double dist = shortest(len, path);
        if (F * dist > 1 + 1e-12) { std::printf("F = %.0f: infeasible, certified in round %d\n", F, t + 1); return; }
        std::vector<double> load(M, 0.0);
        for (int k : path) load[k] = F / cap[k];
        for (int k = 0; k < M; k++) { avg[k] += load[k] / T; w[k] *= 1 + eta * (load[k] - 1) / rho; }
    }
    double worst = 0;
    for (double a : avg) if (a > worst) worst = a;
    std::printf("F = %.0f: %d rounds, max congestion %.4f\n", F, T, worst);
}

int main() { run(4, 0.1); run(5, 0.1); run(6, 0.1); }
F = 4: 10301 rounds, max congestion 0.8077
F = 5: 16095 rounds, max congestion 1.0076
F = 6: infeasible, certified in round 3

Electrical flows: the idea of Christiano et al.

The shortest-path oracle sends all flow along one path, which is why the width ρ\rho is large. Christiano, Kelner, Mądry, Spielman and Teng (2011) used a much better oracle for undirected graphs: the electrical flow. Give arc ee the resistance re=we/ue2r_e = w_e / u_e^2 (current MWU weights over squared capacities), and inject FF units of current at ss, extracting them at tt. The resulting flow minimizes the energy ∑erefe2\sum_e r_e f_e^2, which is a weighted average of squared congestions, and it spreads over many paths, so its width is only about m/ε\sqrt{m / \varepsilon}. It is computed by solving one Laplacian system Lϕ=F(es−et)L \phi = F(e_s - e_t), and Laplacian systems can be solved in nearly linear time (Spielman and Teng, 2004). The result was an O~(m4/3ε−3)\tilde O(m^{4/3} \varepsilon^{-3})-time (1−ε)(1 - \varepsilon)-approximate max flow, the start of a line of work that reached nearly linear time for approximate undirected max flow (Sherman, 2013; Kelner, Lee, Orecchia and Sidford, 2014) and, through interior-point methods, almost-linear exact max flow (Chen et al., 2022).

The program computes the first electrical flow (all weights 1) on the network above, treated as undirected.

python · try it yourself
import numpy as np

arcs = [("s", "a", 3), ("s", "b", 2), ("a", "b", 1), ("a", "t", 2), ("b", "t", 3)]
V = ["s", "a", "b", "t"]
F = 5.0
idx = {v: i for i, v in enumerate(V)}
L = np.zeros((4, 4))
for u, v, c in arcs:
    g = c * c                                       # conductance 1 / r_e with r_e = 1 / c^2
    i, j = idx[u], idx[v]
    L[i, i] += g; L[j, j] += g; L[i, j] -= g; L[j, i] -= g
rhs = np.zeros(4)
rhs[idx["s"]], rhs[idx["t"]] = F, -F
phi = np.zeros(4)
phi[:3] = np.linalg.solve(L[:3, :3], rhs[:3])       # ground the sink: phi_t = 0
for u, v, c in arcs:
    f = c * c * (phi[idx[u]] - phi[idx[v]])
    print(f"arc {u}-{v}: flow {f:7.4f}, congestion {abs(f) / c:.4f}")
arc s-a: flow  2.6471, congestion 0.8824
arc s-b: flow  2.3529, congestion 1.1765
arc a-b: flow  0.2941, congestion 0.2941
arc a-t: flow  2.3529, congestion 1.1765
arc b-t: flow  2.6471, congestion 0.8824

Even with uniform weights the electrical flow spreads the 5 units over all five arcs; the two most loaded arcs carry 18% more than their capacity. MWU then raises the resistance of the overloaded arcs and solves again. Each step is a linear system rather than a shortest path, and far fewer steps are needed.

Test yourself

What must the oracle in the MWU feasibility framework do?

  1. Satisfy every constraint
  2. Return a point of the easy set PP satisfying the weighted average constraint pTAx≤pTbp^T Ax \le p^T b, or prove that none exists
  3. Solve the LP exactly
  4. Find the width

How many oracle calls does the framework need for accuracy ε\varepsilon with width ρ\rho and mm constraints?

  1. O(log⁡(1/ε))O(\log(1/\varepsilon))
  2. O(ρ2ln⁡m/ε2)O(\rho^2 \ln m / \varepsilon^2)
  3. O(m)O(m)
  4. O(2m)O(2^m)

Which oracle does the max-flow version use, and what does its failure prove?

  1. Max flow; nothing
  2. A shortest path for lengths pe/uep_e / u_e; if FF times the distance exceeds 1, no flow of value FF fits the capacities
  3. A min cut; the flow is maximum
  4. Dijkstra on capacities; the graph is disconnected