Sub topic 14
Modeling Tricks: Super Sources, Undirected Edges, Vertex Capacities
This chapter assumes you know the flow network model with a single source s and a single sink t, and the paired-edge MaxFlow class from the implementation chapter. Real problems often don't fit that model directly. They may have several sources, roads that carry traffic both ways, or junctions with a throughput limit. You don't need a new algorithm for any of these. You change the graph, then run Edmonds-Karp on the result.
Multiple Sources and Sinks
Add a new super source S with an edge to every original source. Then add a new super sink T with an edge from every original sink. A maximum flow from S to T equals the best total flow from all the sources to all the sinks.
The capacities of these new edges matter:
- If sources can supply any amount, give each edge
S -> s_ia capacity that can never be the bottleneck. Total edge capacity plus one is enough. - If source
s_ihas a supply limitb_i, set the capacity ofS -> s_itob_i. Sink demands work the same way on the edgest_j -> T.
Undirected Edges
An undirected edge {u, v} with capacity c lets up to c units travel in either direction. Model it as two directed edges, u -> v and v -> u, each with capacity c. You never need flow in both directions at once, because flow going opposite ways on the same edge cancels out.
With the paired-edge representation, there is a shortcut. Store the forward edge with capacity c and give its reverse edge capacity c too, instead of 0. Both versions give the same maximum flow.
Vertex Capacities
Sometimes a vertex v, such as a router or a warehouse, can pass at most c(v) units. Max-flow algorithms only limit edges, so split v into two nodes:
v_inreceives every edge that used to enterv.v_outsends every edge that used to leavev.- One edge
v_in -> v_outwith capacityc(v)carries all the flow throughv.
A common mistake is to redirect only some of the edges. If any incoming edge still reaches v_out, that flow skips the limit.
Putting It Together
The function below applies all three transformations. It splits every vertex: node v becomes v (in) and v + n (out), and vertices with no capacity limit get a large bound. It returns an edge list that you can pass to MaxFlow.add_edge.
def build_network(n, edges, undirected, sources, sinks, vertex_cap):
big = sum(c for _, _, c in edges) + sum(c for _, _, c in undirected) + 1
S, T = 2 * n, 2 * n + 1
out = []
for v in range(n): # v_in -> v_out
out.append((v, v + n, vertex_cap.get(v, big)))
for u, v, c in edges: # u_out -> v_in
out.append((u + n, v, c))
for u, v, c in undirected: # both directions
out.append((u + n, v, c))
out.append((v + n, u, c))
for s in sources:
out.append((S, s, big))
for t in sinks:
out.append((t + n, T, big))
return 2 * n + 2, S, T, out
edges = [(0, 2, 4), (1, 2, 6), (1, 3, 3), (3, 4, 7), (2, 4, 8)]
undirected = [(2, 3, 2)]
nodes, S, T, net = build_network(5, edges, undirected,
sources=[0, 1], sinks=[4], vertex_cap={2: 5})
print(nodes, S, T, len(net)) # 12 10 11 15To solve it, create MaxFlow(nodes), call add_edge(u, v, c) for each tuple in net, and run max_flow(S, T).
Exercise
In the example above, what is the maximum flow, and which constraint limits it?
Show answer
8. Vertex 2 can receive 10 units (4 from vertex 0 and 6 from vertex 1), but its capacity caps it at 5, and those 5 units go to vertex 4. Vertex 1 sends another 3 through 3 -> 4. Vertex 3 could forward more over the undirected edge, but its only extra input would have to come from vertex 2, which is already full. Total: 5 + 3 = 8.
Exercise
Why can't you enforce a vertex capacity c(v) by capping each edge that enters v at c(v)?
Show answer
Each incoming edge would be capped separately, so k incoming edges could still carry up to k * c(v) in total. Only the single vin -> vout edge limits the combined flow.