Exercise: graphs, cuts and maximum flow

Goal

By the end of this exercise you should be able to:

  • compute the cost of an s–t cut,
  • find a maximum flow by hand with augmenting paths, using the residual graph,
  • read a minimum cut off the final residual graph, and check that flow = cut,
  • explain why backward arcs are needed and which edges limit the flow.

Do the pen-and-paper part first, then the interactive part.

Background

Idea What it means
Capacity c(u, v) \ge 0 on every directed edge u \to v
s–t cut nodes split into S \ni s and T \ni t; cost c(S, T) = sum of capacities of edges from S to T (edges from T to S do not count)
Flow 0 \le f(u, v) \le c(u, v), and flow in = flow out at every node except s and t
Residual graph forward arc u \to v with capacity c - f; backward arc v \to u with capacity f
Augmenting path a path from s to t in the residual graph; push its smallest residual capacity (the bottleneck)
Max-flow = min-cut at the end, S = nodes reachable from s in the residual graph is a minimum cut, and its cost equals the flow

By hand

The graph for B1–B4 has nodes s, a, b, c, d, t and edges

edge s\!\to\!a s\!\to\!b a\!\to\!b a\!\to\!c b\!\to\!d c\!\to\!d c\!\to\!t d\!\to\!t
capacity 8 4 8 5 4 7 4 7

Draw it first: s on the left, a and c on top, b and d below, t on the right.

B1. Compute the cost of the cuts S = \{s\}, \{s, a\}, \{s, b\}, \{s, a, b\} and \{s, a, b, c, d\}. Which is the smallest of these five? What does that tell you about the maximum flow?

B2. Find a maximum flow with augmenting paths. Always take a shortest path (fewest edges) in the residual graph. For each step, write the path, its bottleneck and the total flow so far. Give the final flow on every edge.

B3. Check the flow of B2: is the capacity constraint respected on every edge, and is flow in = flow out at a, b, c and d?

B4. Draw the final residual graph. Which nodes are reachable from s? Which edges cross from this set to the rest, and what is the cost of this cut? Compare with B1 and B2.

B5. Which single edge’s capacity would you raise to increase the maximum flow? What is the maximum flow if you raise c(a, c) from 5 to 6? From 5 to 8? Why does raising c(c, t) not help?

B6. Four nodes s, a, b, t, all capacities 1: s\!\to\!a, s\!\to\!b, a\!\to\!b, a\!\to\!t, b\!\to\!t. (a) What is the maximum flow? Give a minimum cut. (b) Suppose the first augmenting path is s \to a \to b \to t. Draw the residual graph. Is there a path from s to t using forward arcs only? Using backward arcs too? Push along it and give the final flow.

B1. \{s\}: s\!\to\!a + s\!\to\!b = 8 + 4 = 12. \{s, a\}: s\!\to\!b + a\!\to\!b + a\!\to\!c = 4 + 8 + 5 = 17. \{s, b\}: s\!\to\!a + b\!\to\!d = 8 + 4 = 12. \{s, a, b\}: a\!\to\!c + b\!\to\!d = 5 + 4 = 9. \{s, a, b, c, d\}: c\!\to\!t + d\!\to\!t = 4 + 7 = 11. The smallest is \{s, a, b\} with 9, so the maximum flow is at most 9.

B2. There are two shortest paths (3 edges): s \to a \to c \to t, bottleneck \min(8, 5, 4) = 4, flow 4; and s \to b \to d \to t, bottleneck \min(4, 4, 7) = 4, flow 8 (either order). Now s\!\to\!b, b\!\to\!d and c\!\to\!t are full. The next shortest path is s \to a \to c \to d \to t, bottleneck \min(8 - 4, 5 - 4, 7, 7 - 4) = 1: flow 9. Then a\!\to\!c is full as well, and no path is left. Final flow: s\!\to\!a 5, s\!\to\!b 4, a\!\to\!b 0, a\!\to\!c 5, b\!\to\!d 4, c\!\to\!d 1, c\!\to\!t 4, d\!\to\!t 5.

B3. Every flow is between 0 and the capacity. At a: in 5 (s\!\to\!a), out 0 + 5 (a\!\to\!b, a\!\to\!c). At b: in 4 + 0, out 4. At c: in 5, out 1 + 4. At d: in 4 + 1, out 5. Flow out of s: 5 + 4 = 9 = flow into t: 4 + 5 = 9.

B4. Residual arcs leaving s: s\!\to\!a (3 left); s\!\to\!b is full. From a: a\!\to\!b (8 left), a\!\to\!c is full, backward a\!\to\!s. From b: b\!\to\!d is full; only backward arcs to s (and none to a, since a\!\to\!b carries no flow). So the reachable set is S = \{s, a, b\}. Edges from S to T = \{c, d, t\}: a\!\to\!c (5) and b\!\to\!d (4), cost 9, both full. Flow 9 = cut 9: this is the minimum cut of B1.

B5. Only raising an edge of the minimum cut can help: a\!\to\!c or b\!\to\!d. With c(a, c) = 6 the cut \{s, a, b\} costs 10 and that is the new maximum flow (all other cuts cost at least 11). With c(a, c) = 8 the cut \{s, a, b\} costs 12, but now \{s, a, b, c, d\} (11) is the smallest: maximum flow 11. c\!\to\!t is not in the minimum cut, so raising it leaves the cut of 9, and the flow, unchanged.

B6. (a) Maximum flow 2, for example s \to a \to t and s \to b \to t. A minimum cut: S = \{s\}, cost 1 + 1 = 2 (also \{s, b\}: s\!\to\!a + b\!\to\!t = 2). (b) After pushing 1 along s \to a \to b \to t, the arcs s\!\to\!a, a\!\to\!b, b\!\to\!t are full. Forward arcs with room: s\!\to\!b and a\!\to\!t only; from s we reach b, and from b no forward arc leads anywhere. Stuck at 1. With backward arcs: b \to a (capacity 1, undoing the flow on a\!\to\!b). Path s \to b \to a \to t, bottleneck 1. Pushing it sets s\!\to\!b = 1, a\!\to\!b = 0, a\!\to\!t = 1. Final flow: s\!\to\!a 1, s\!\to\!b 1, a\!\to\!b 0, a\!\to\!t 1, b\!\to\!t 1: value 2.

For any cut (S, T), the value of a flow equals the flow from S to T minus the flow from T to S. For a node u \ne s in S, flow out minus flow in is 0 (conservation); for s it is |f|. Add these over all u \in S: |f| = \sum_{u \in S} \big(\sum_v f(u,v) - \sum_v f(v,u)\big). An edge with both ends in S appears once with + and once with - and cancels. What is left are the edges leaving S (with +) and the edges entering S from T (with -): |f| = \sum_{S\to T} f - \sum_{T \to S} f. Since f \le c on the first sum and f \ge 0 on the second, |f| \le \sum_{S \to T} c = c(S, T).

Explore

The demo shows a small graph with flow / capacity on every edge. Next augmenting path finds a shortest path from s to t in the residual graph (orange) and pushes its bottleneck; run to end does all steps; flow = 0 starts again. Show the residual graph draws the residual arcs instead: grey for capacity left, red for flow that can be undone. When no path is left, the nodes reachable from s are shaded blue, the rest orange, and the edges of the cut are red and dashed. Cut nearest t shows the other extreme minimum cut. Click an edge to change its capacity.

E1. With the example, step through with next augmenting path. Write down each path and its bottleneck. When no path is left, which nodes are on the source side, and which edges form the cut? Edge a \to b goes from the sink side to the source side: why does it not count, and what flow does it carry?

E2. Step through again with show the residual graph ticked. After the second step, which arcs leave s? Why is there a red arc from t to d?

E3. Choose needs a back-edge and step through. Which edge does the second path use backwards, and what happens to the flow on that edge? What would happen without backward arcs?

E4. Choose two minimum cuts and run to end. Which cut does the demo show? Tick cut nearest t: which cut is it now, and what does it cost? Now raise c(c, t) from 4 to 5: does the maximum flow change? Then reset and raise c(s, b) from 2 to 3 instead: what changes?

E5. Back to the example. Raise c(b, d) from 9 to 12. What is the new maximum flow, and where is the new minimum cut? Reset and raise c(c, t) to 12 instead. Explain the difference.

E1. Paths: s\,a\,c\,t (bottleneck 4), s\,a\,d\,t (6), s\,b\,d\,t (4), s\,b\,d\,c\,t (5): total 19. The source side is S = \{s, b\}; the cut edges are s\!\to\!a (10) and b\!\to\!d (9): 19 = flow. a\!\to\!b goes from T to S, so it is not part of the cost; at the end it carries 0 flow, as the max-flow / min-cut argument requires (if it carried flow, its backward arc b \to a would make a reachable from s).

E2. After two steps (s\,a\,c\,t with 4 and s\,a\,d\,t with 6), s\!\to\!a is full: from s there is only the forward arc s \to b (10 left); s\!\to\!a now appears only as a red backward arc a \to s with capacity 10. The red arc t \to d (capacity 6) is the backward arc of d\!\to\!t, which carries 6: flow that could be sent back.

E3. The second path is s\,c\,g\,b\,a\,d\,e\,t. It uses a \to b backwards (as b \to a): the 4 units the first path sent along a \to b are cancelled, and the flow on a\!\to\!b drops to 0. In effect the first path is rerouted to s\,a\,d\,e\,t, and the second path takes over b\!\to\!t. Without backward arcs, after the first path (s\,a\,b\,t), b\!\to\!t and s\!\to\!a are full and t cannot be reached from s: the algorithm would stop at 4 instead of 8.

E4. The demo shows S = \{s\}: cut s\!\to\!a + s\!\to\!b = 5 + 2 = 7. Cut nearest t: S = \{s, a, b, c, d\}, cut c\!\to\!t + d\!\to\!t = 4 + 3 = 7. Raising c(c, t) to 5: the flow stays 7, because the cut \{s\} still costs 7. Raising c(s, b) to 3 instead: the flow also stays 7, but now the cut nearest s is \{s, a, b, c, d\} as well, because \{s\} costs 8 and only the cut on the t side is minimal. To increase the flow, every minimum cut must get more capacity.

E5. With c(b, d) = 12 the maximum flow is 20, and the minimum cut is S = \{s\} (10 + 10): the old cut \{s, b\} now costs 10 + 12 = 22. With c(c, t) = 12 instead the flow stays 19: c\!\to\!t is not in the minimum cut, so more capacity there is never used.

Code

Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in graphcuts/src/maxflow/exercise.rs. A graph of n nodes is its n \times n capacity matrix, cap[u][v] (0 where there is no edge); s is node 0 and t node 1.

C1. cut_cost(cap, source_side): the cost of an s–t cut:

cost = 0
for every pair of nodes (u, v):
    if u is in S and v is not:  cost = cost + c(u, v)

The check is your answer to B1.

C2. residual(cap, flow) and shortest_path(r, s, t): the residual graph, r(u, v) = c(u, v) - f(u, v) + f(v, u) (room left, plus flow on v \to u that can be undone), and a shortest path in it by breadth-first search:

queue = [s];  mark s as seen
while the queue is not empty:
    u = take the first node from the queue
    for every node v not seen with r(u, v) > 0:
        mark v as seen;  came_from(v) = u;  add v to the end of the queue
if t was seen: follow came_from back from t to s

C3. max_flow(cap, s, t): maximum flow by shortest augmenting paths:

f = 0 on every edge
repeat:
    r = the residual graph of f
    path = a shortest path from s to t in r;  stop if there is none
    b = the smallest r(u, v) along the path               # the bottleneck
    for every arc u → v of the path:
        undo = min(b, f(v, u));   f(v, u) = f(v, u) − undo   # backward arc
        f(u, v) = f(u, v) + (b − undo)                       # forward arc

The checks are your answers to B2 (flow 9, and the flow on every edge), B5 and B6, and the lecture’s “needs a back-edge” graph (flow 8, with the flow on a → b undone).

C4. source_side(cap, flow, s): the nodes that can still be reached from s in the residual graph of the maximum flow. The checks are B4 (S = \{s, a, b\}, cut 9), the lecture’s example (S = \{s, b\}, cut 19), and flow = cut on random graphs.

Then cargo run -p graphcuts -- maxflow. It prints every augmenting path with its bottleneck, the source side and the cut edges, for the lecture’s example, the graph of B1–B5 and “needs a back-edge”. Then it times max_flow on grids of k \times k nodes with random capacities to the four neighbours, the left column joined to s and the right column to t: an image graph in miniature.

C5. Compare the printed paths with the slide “Ford–Fulkerson” and with your B2. In “needs a back-edge”, which arc does the second path use backwards?

C6. Count the cost. On our computer the grids of 102, 402 and 902 nodes took 0.002, 0.08 and 0.5 s, with 21, 69 and 94 augmenting paths. Each breadth-first search looks at every entry of the residual matrix. Estimate the cost of one search for a 128 \times 128 image (16 386 nodes), and how much it would save to store, for every node, only its few edges.

The model solution is graphcuts/src/maxflow/solution.rs in the download.

C5. Lecture example: s a c t (4), s a d t (6), s b d t (4), s b d c t (5): flow 19, then S = \{s, b\} with the full cut edges s→a (10/10) and b→d (9/9), as on the slides. B1–B5: s a c t (4), s b d t (4), s a c d t (1): flow 9, S = \{s, a, b\}, cut a→c (5/5) and b→d (4/4). “Needs a back-edge”: s a b t (4), then s c g b a d e t (4): the second path goes from b to a, the backward arc of a → b, and takes the 4 units of the first path off a → b. Flow 8, cut s→a and s→c.

C6. One search on a full matrix looks at n^2 entries: 16\,386^2 \approx 2.7 \cdot 10^8 for a 128 \times 128 image, about 2 seconds at the speed of the 902-node grid (which did 94 \cdot 902^2 \approx 7.6 \cdot 10^7 in 0.5 s), and an image needs hundreds or thousands of augmenting paths: minutes to hours. A pixel has only about 6 edges (4 neighbours, s and t), so with a list of edges per node a search costs about 6n instead of n^2: n/6 \approx 2700 times less. The graph-cut unit’s code keeps such lists (and a faster way of choosing paths).

Where this leads

The algorithm finds a minimum cut in a graph with thousands or millions of nodes. In the next unit we build a graph from an image, so that the cost of every cut is the energy of a segmentation. One max-flow then gives the best segmentation, exactly.