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 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 in a convex set (simple, with an efficient oracle) satisfies "hard" linear constraints . The oracle gets a probability vector over the constraints and must either
- return with (the weighted average constraint holds), or
- report that no such exists, which proves that the system is infeasible: any feasible would satisfy every non-negative combination of the constraints, in particular this one.
The width bounds how much any oracle answer can violate or satisfy a constraint: for all and all oracle outputs .
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 satisfies for every constraint , after oracle calls.
Proof sketch. Run MWU with the constraints as decisions and costs (a violated constraint has negative cost, so its weight grows). The oracle guarantees , so the algorithm's cost is every round. The regret theorem then says that, for every constraint , , that is, . With the stated and the right side is at most , and the left side is by linearity.
Optimization reduces to feasibility by binary search on the objective value (add " guess" to ). The running time depends on , not on the bit length: these are approximation schemes, fast when 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 units can be sent from to within capacities. Let be the set of flows of value that ignore the capacities: every such flow is a mix of units sent along single - paths. The hard constraints are the congestions , one per arc.
- Oracle. Given weights on the arcs, minimize over : send all units along a shortest path for the lengths . If even that path has , no flow of value fits, and the lengths are a fractional cut certificate.
- Width. A path flow has congestion at most on any arc, so .
The program runs this on the network of Max-Flow Min-Cut as LP Duality, whose maximum flow is 5, for and .
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 and the averaged flow overloads no arc by more than (at , the true maximum, the excess is under 1%). For 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 fixed the method is fast; the number of rounds grows with , 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.
#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 is large. Christiano, Kelner, Mądry, Spielman and Teng (2011) used a much better oracle for undirected graphs: the electrical flow. Give arc the resistance (current MWU weights over squared capacities), and inject units of current at , extracting them at . The resulting flow minimizes the energy , which is a weighted average of squared congestions, and it spreads over many paths, so its width is only about . It is computed by solving one Laplacian system , and Laplacian systems can be solved in nearly linear time (Spielman and Teng, 2004). The result was an -time -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.
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?
- Satisfy every constraint
- Return a point of the easy set satisfying the weighted average constraint , or prove that none exists
- Solve the LP exactly
- Find the width
How many oracle calls does the framework need for accuracy with width and constraints?
Which oracle does the max-flow version use, and what does its failure prove?
- Max flow; nothing
- A shortest path for lengths ; if times the distance exceeds 1, no flow of value fits the capacities
- A min cut; the flow is maximum
- Dijkstra on capacities; the graph is disconnected