s -> v (16) and s -> w (13). Let's say we go to v first.
Then from v: to s (0, skip), to w (10), to t (12), to u (0). So we can go to w and t.
Suppose we go to w: then from w: to s (0), to u (9), to v (0). So we can go to u.
Then from u: to w (0), to v (4), to t (14). So we can go to v (4) and t (14).
Now, if we go to t from u, then we have a path: s->v->w->u->t.
But wait, we could also go from v to t directly? That's shorter. The BFS will find the shortest path in terms of edges. So the first time we see t, that's the shortest.
Actually, from s we go to v and w. Then from v we see t (12). So we have a path s->v->t with 2 edges.
So BFS will find s->v->t.
Then the bottleneck is min(16, 12) = 12.
Then we update:
residual[s][v] = 16-12 = 4
residual[v][s] = 0+12 = 12
residual[v][t] = 12-12 = 0
residual[t][v] = 0+12 = 12
Then max_flow = 12.
Next BFS:
Now residual:
s: {v:4, w:13}
v: {s:12, w:10, t:0, u:0}
w: {s:0, u:9, v:0}
u: {w:0, v:4, t:14}
t: {v:12, u:0}
BFS from s:
s -> v (4) and s -> w (13).
From v: to s (12), to w (10), to t (0), to u (0). So we can go to w (10) and s (12, but s is visited). So w.
From w: to s (0), to u (9), to v (0). So u.
From u: to w (0), to v (4), to t (14). So we can go to v (4) and t (14).
Now, from u we can go to t? That's s->v->w->u->t. But we also have from v we could go to u? But v->u is 0.
So the shortest path to t? We have:
Option 1: s->w->u->t: 3 edges.
Option 2: s->v->w->u->t: 4 edges.
But note: from s we also have s->w (13) and then w->u (9) and then u->t (14). So that's 3 edges.
BFS will find s->w->u->t.
Bottleneck: min(13,9,14) = 9.
Update:
s->w: 13-9=4, w->s: 0+9=9
w->u: 9-9=0, u->w: 0+9=9
u->t: 14-9=5, t->u: 0+9=9
max_flow = 12+9=21.
Next BFS:
residual:
s: {v:4, w:4}
v: {s:12, w:10, t:0, u:0}
w: {s:9, u:0, v:0}
u: {w:9, v:4, t:5}
t: {v:12, u:9}
BFS from s:
s -> v (4) and s -> w (4).
From v: to s (12), to w (10), to t (0), to u (0). So we can go to w (10).
From w: to s (9), to u (0), to v (0). So only s (but visited) and u (0). So no.
But wait, from v we can go to w (10). Then from w? w has no outgoing with positive capacity? w->u is 0, w->v is 0, w->s is 9 (but s visited). So dead end.
So from s->v->w is a dead end? But we have an edge from v to t? That's 0.
What about from s->w? w has no outgoing.
So we need to use reverse edges? For example, from v we have an edge to s (12) but s is visited. But we can go from v to s? That's a reverse edge, but we are allowed to use it if it has positive capacity? Yes, the residual graph includes reverse edges.
But note: from v to s is 12, but s is already visited. So we don't go back to s.
What about from u? We haven't reached u yet.
Let's try another path: s->v (4) and then from v we can go to s (12) but that's back. Or from v we can go to w (10). Then from w we can go to u? That's 0. But we have an edge from u to w? That's a reverse edge? Actually, we have residual[u][w] = 9. But we are at w, and we are looking at w's outgoing. w has an edge to u? No, w has an edge to u with capacity 0 (from the original we had w->u, but now it's 0). But we do have an edge from u to w? That's a reverse edge? In the residual graph, we have both directions. So at w, we have an edge to u? No, we have an edge from w to u with capacity 0? Actually, in the residual graph, the edge from w to u is the forward edge (which we used) and now it's 0. But we also have an edge from u to w (the reverse) which is 9. But that edge is in the residual graph as residual[u][w]=9. But we are at w, and we are iterating over residual[w]? We are looking at the outgoing edges from w. So we only see:
w: {s:9, u:0, v:0}
So we cannot go from w to u because the residual capacity is 0.
But wait, we have an edge from u to w? That's an edge from u to w, not from w to u. So we cannot traverse from w to u on that edge because it's directed.
So we are stuck.
What about going from s->w? Then from w we can only go to s (9) but s is visited.
So we need to find a path that uses reverse edges? For example, from v we can go to s (12) and then from s to w? But that's two forward edges? Actually, we are at v, and we can go to s (reverse edge) and then from s to w? But s to w is 4. So we have:
s->v (4) then v->s (12) then s->w (4) then w->? w has no outgoing? Then from w we can go to u? That's 0.
Alternatively, we can go from s->v (4) then v->w (10) then w->s (9) then s->w (4) again? That doesn't help.
Actually, we have an edge from u to t (5) and from t to u (9). But we haven't reached u or t.
Let me list the positive residual capacities:
s: {v:4, w:4}
v: {s:12, w:10}
w: {s:9}
u: {v:4, t:5}
t: {u:9, v:12}
So from s we can go to v and w.
From v: to s (12) and w (10). But s and w are already visited? Actually, in BFS we mark visited when we first see them.
Let's run BFS:
Start: s -> v (4) and s -> w (4). Mark v and w visited.
Then from v: to s (12) -> s is visited, skip. To w (10) -> w is visited? We already visited w from s. So we don't add w again.
Then from w: to s (9) -> s visited.
So we haven't reached t.
But wait, we have an edge from u to t (5) and from t to v (12) and t to u (9). But we haven't reached u or t.
How can we reach u? We have an edge from v to u? That's 0. And from w to u? That's 0. But we have an edge from u to v? That's 4. But we are at v and we want to go to u? We can't because v->u is 0.
What about from s->v->? We can't go to u.
What about from s->w? Then from w we can't go to u.
So we are stuck.
But wait, we have an edge from u to v (4) and from v to s (12). So if we could get to u, then we could go to v and then to s? But we are at s and we want to get to t.
Actually, we might have missed a path: s->v (4) then v->s (12) then s->w (4) then w->? nothing. Or s->w (4) then w->s (9) then s->v (4) then v->w (10) then w->? nothing.
What about using the reverse edge from t to u? But we haven't reached t.
It seems we are stuck. But the maximum flow is 23. We have 21, so there is 2 more.
Let me check: we have residual capacities:
s->v: 4
s->w: 4
v->w: 10
v->t: 0
w->u: 0
u->v: 4
u->t: 5
and reverse edges:
v->s: 12
w->s: 9
u->w: 9
t->u: 9
t->v: 12
Now, consider the path: s->v->w->u->t? But w->u is 0.
How about: s->v->w->? We can go from v to w (10) and then from w we have no outgoing? But we have an edge from u to w? That's a reverse edge? We are at w, and we want to go to u? We have an edge from w to u? That's the forward edge and it's 0. But we have an edge from u to w? That's an edge from u to w, so we cannot go from w to u on that.
Alternatively, we can go from v to u? That's 0.
What about: s->w->s->v->t? That doesn't make sense because s->w and then w->s? That's going back.
Actually, we can have a path that uses the reverse edge from u to w? But we are at w and we want to go to u? We cannot because the edge is from u to w.
Wait, the residual graph has:
u->w: 9 (reverse edge of w->u)
But that means we can go from u to w, not from w to u.
So how can we get to u? We have an edge from v to u? That's 0. And from w to u? That's 0.
But we have an edge from t to u? That's 9. But we haven't reached t.
So it seems we cannot reach u or t from s.
But that can't be because the maximum flow is 23 and we have 21.
Let me reexamine the residual graph after two augmentations:
After first augmentation (path s->v->t, flow 12):
s->v: 4
v->t: 0
v->s: 12
t->v: 12
After second augmentation (path s->w->u->t, flow 9):
s->w: 4
w->u: 0
u->t: 5
w->s: 9
u->w: 9
t->u: 9
Now, what about the edge from v to w? It's still 10. And from u to v? It's 4.
So we have:
s: {v:4, w:4}
v: {s:12, w:10, t:0, u:0}
w: {s:9, u:0, v:0}
u: {w:9, v:4, t:5}
t: {v:12, u:9}
Now, consider the path: s->v->w->u->t? But w->u is 0.
How about: s->v->? We can go from v to w (10). Then from w we can go to s (9) but that's back. Or from w we can go to v? That's 0. Or from w we can go to u? That's 0.
But wait, we have an edge from u to w (9). That's an edge from u to w. So if we are at w, we cannot go to u.
What about: s->v->u->t? But v->u is 0.
What about: s->w->v->t? But w->v is 0? We have an edge from v to w, but not from w to v? Actually, we have residual[w][v]? We set it when we processed v->w? Yes, we set residual[w][v]=0. So from w to v is 0.
Alternatively, we can use the reverse edge from v to s? Then from s to w? That would be s->v->s->w? That's not a simple path.
Actually, the Edmonds-Karp algorithm uses simple paths? The BFS will not go to a visited node. So we cannot go back to s.
So how do we get the remaining 2?
Let me think about the original graph:
We have:
s->v (16)
s->w (13)
v->w (10)
v->t (12)
w->u (9)
u->v (4)
u->t (14)
The maximum flow is 23.
We have sent 12 on s->v->t and 9 on s->w->u->t.
Now, we can send 2 more on s->v->w->u->t? But w->u is already at capacity? We used 9 out of 9? Actually, in the original, w->u has capacity 9, and we used 9, so it's full.
But we can also send flow on s->w->v->t? But we don't have an edge from w to v? We have an edge from v to w, so we can use the reverse edge? That is, we can send flow from w to v by using the reverse edge of v->w? That would be like reducing the flow on v->w? But we haven't sent any flow on v->w yet? In the two augmentations, we didn't use v->w.
Actually, in the first augmentation we used s->v->t, so we didn't use v->w.
In the second, we used s->w->u->t, so we didn't use v->w.
So v->w still has 10.
But to use v->w in a path from s to t, we would need to go from s to v and then to w and then to u and then to t? But w->u is full.
Alternatively, we can go from s to w and then to v? But we don't have w->v. We have v->w, so we can go from w to v by using the reverse edge? That would be reducing the flow on v->w? But we haven't sent any flow on v->w, so the reverse edge is 0? Actually, the reverse edge of v->w is w->v, which initially was 0. And we haven't used v->w, so it's still 0? Wait, in the residual graph, the reverse edge of v->w is stored as residual[w][v]. Initially it was 0. And we haven't sent any flow on v->w, so it remains 0.
So we cannot go from w to v.
Then how do we get 2 more?
Let me check the standard solution for this example:
The maximum flow is 23. The augmenting paths found by Edmonds-Karp are:
1. s->v->t (12)
2. s->w->u->t (9)
3. s->v->w->u->t (2) [but wait, w->u is already at 9?]
Actually, in the third augmentation, we use the path s->v->w->u->t, but note that w->u has capacity 9, but we have already used 9? So we cannot use it again.
But wait, in the second augmentation we used w->u? Yes, we used 9 on w->u, so it's full.
Then how?
Let me double-check the residual after two augmentations:
After first (s->v->t, 12):
s->v: 16-12=4
v->t: 12-12=0
reverse:
v->s: 0+12=12
t->v: 0+12=12
After second (s->w->u->t, 9):
s->w: 13-9=4
w->u: 9-9=0
u->t: 14-9=5
reverse:
w->s: 0+9=9
u->w: 0+9=9
t->u: 0+9=9
Now, note that we also have the edge u->v (4) and v->w (10) that we haven't used.
Consider the path: s->v->w->u->t? But w->u is 0.
How about: s->v->u->t? But v->u is 0.
How about: s->w->v->t? But w->v is 0.
What if we use the reverse edge from u to w? That would be going from w to u? No, we are at w and we want to go to u? We have an edge from w to u? It's 0. But we have an edge from u to w? That's 9, but that's from u to w, so we cannot go from w to u on that.
Actually, we can go from w to u by using the reverse edge of u->w? That would be w->u? But the reverse edge of u->w is w->u? No, the reverse edge of u->w is w->u? Actually, if we have an edge u->w, then the reverse edge is w->u. But in the residual graph, we have:
When we have an edge u->w with capacity c and flow f, we have:
residual[u][w] = c - f
residual[w][u] = f
So if we have an edge from u to w (in the original) and we haven't used it, then residual[u][w] = c and residual[w][u] = 0.
In our case, the original has an edge from w to u, not from u to w. So the residual for w->u is the forward edge, and u->w is the reverse edge.
So after using w->u, we have:
residual[w][u] = 0 (forward)
residual[u][w] = 9 (reverse)
So from u, we can go to w (9) but from w, we cannot go to u (0).
So the path s->v->w->u->t is not possible because w->u is 0.
Then how do we get 2 more?
Let me look at the standard solution:
After two augmenting paths (12 and 9), the third augmenting path is s->v->w->u->t with flow 2.
But wait, in the residual graph, we have:
s->v:4
v->w:10
w->u:0 -> but we are going to use w->u? How?
Ah, but note: in the residual graph, we have an edge from u to w (9). That means we can send flow from u to w? But we want to go from w to u? That would be using the reverse edge? Actually, the edge from u to w is a reverse edge of the original w->u. So if we are at w and we want to go to u, we need an edge from w to u. But we have an edge from u to w, not from w to u.
So how can we go from w to u? We cannot.
Unless we consider the path s->v->w->? and then from w we go to s? That doesn't help.
Wait, maybe I made a mistake in the residual update for the second augmentation.
In the second augmentation, we used the path s->w->u->t.
So we updated:
s->w: 13 -> 4
w->u: 9 -> 0
u->t: 14 -> 5
and the reverse edges:
w->s: 0 -> 9
u->w: 0 -> 9
t->u: 0 -> 9
So now, we have:
residual[w][u] = 0 (forward)
residual[u][w] = 9 (reverse)
So if we want to send flow from w to u, we need residual[w][u] > 0, but it's 0.
So we cannot.
Then how do we get 2 more?
Let me consider the possibility of using the edge v->w and then from w to v? But we don't have w->v.
Wait, we have an edge from v to w (10) and we haven't used it. So we can use it in a path? But then from w, we need to go to t. The only way from w to t is through u? But w->u is 0.
What about going from w to s? Then from s to v? That would be s->w->s->v? That's not allowed because we don't want to visit s twice.
Alternatively, we can send flow from s to v, then from v to w, then from w to u? But w->u is 0.
But note: we have an edge from u to w (9). So if we are at w, we cannot go to u, but if we are at u, we can go to w.
So what if we send flow from s to w, then from w to s? That's not helpful.
I think I found the issue: in the second augmentation, we used s->w->u->t. But we also have an edge from v to w. So after that, we have:
We can send flow from s to v (4) and then from v to w (10) and then from w to u? But w->u is 0.
However, we can use the reverse edge from u to w? That would be going from w to u? No, because the reverse edge is u->w.
Wait, the residual graph has:
u->w: 9 (reverse of w->u)
So if we are at u, we can go to w. But we are at w and we want to go to u.
So we need an edge from w to u. That would be the forward edge of w->u, which is 0.
So we cannot.
Then how?
Let me check the actual maximum flow for this graph:
The maximum flow is 23. The min cut is {s, v, w, u} and {t}? Or {s, v} and {w, u, t}? Let me compute the min cut.
The cut: S = {s, v, w, u}, T = {t}. The edges from S to T are u->t (14). So capacity 14? That's not 23.
Another cut: S = {s, v}, T = {w, u, t}. Edges from S to T: s->w (13), v->w (10), v->t (12). That's 13+10+12=35.
Another: S = {s}, T = {v, w, u, t}. Edges: s->v (16), s->w (13) = 29.
Another: S = {s, w}, T = {v, u, t}. Edges: s->v (16), w->u (9), w->v? (0) -> 16+9=25.
Another: S = {s, v, w}, T = {u, t}. Edges: v->t (12), w->u (9) = 21.
Another: S = {s, v, u}, T = {w, t}. Edges: s->w (13), v->w (10), u->t (14) = 13+10+14=37.
So the min cut is 21? But the maximum flow is 23? That violates the max-flow min-cut theorem.
Wait, I think I have the graph wrong. Let me check the original graph from CLRS:
The graph is:
s -> v (16)
s -> w (13)
v -> w (10)
v -> t (12)
w -> u (9)
u -> v (4)
u -> t (14)
The min cut is actually {s, v, w} and {u, t}? Then edges from S to T: w->u (9) and v->t (12) -> 21.
But the maximum flow is 23? That can't be.
Actually, the maximum flow in this graph is 23? Let me compute manually:
We can try:
Send 16 from s->v, then from v: send 12 to t and 4 to w? But v->w is 10, so we can send 4 to w. Then at w: receives 4 from v and also s->w? We haven't used s->w yet. Then from w we can send 9 to u? But we only have 4 from v and we can also get from s->w.
Alternatively:
Send 13 on s->w, then w->u (9) and then u->t (14) -> we can send 9 on that path.
Then we have s->v (16) and v->t (12) -> send 12.
Then we have s->w remaining 4, and v->w remaining 10? But we haven't used v->w? Actually, we used s->w for 9, so s->w has 4 left. And v->w has 10.
Now, we can send 4 from s->w to w, then from w we have no outgoing because w->u is full? But we can send from w to v? We don't have an edge from w to v.
Alternatively, we can send from s->v (16) and then from v to w (10) and then from w to u? But w->u is full.
But note: we have an edge from u to v (4). So we can send from u to v? That would be reducing the flow on v->w? Actually, we haven't sent any flow on v->w.
Wait, let's try:
After sending 12 on s->v->t and 9 on s->w->u->t, we have:
s->v: 4 left
s->w: 4 left
v->w: 10 (unused)
w->u: 0 (used 9)
u->t: 5 left (14-9)
u->v: 4 (unused)
Now, we can send 4 from s->v->w->u->t? But w->u is 0.
However, note that we have an edge from u to v (4). So we can send from s->v->? But we want to get to t.
What if we send 4 from s->v->w? Then from w we can't go to u. But we can go from w to s? That's reverse edge? Actually, we have residual[w][s] = 9 (from s->w). So we can send from w to s? That would be undoing part of the flow on s->w.
Alternatively, we can send 4 from s->w->? Then from w we can go to s? That would be undoing.
Actually, we can use the following path: s->v->w->s->w->u->t? That would be invalid because we visit s twice.
I think I found the third augmenting path:
s->v->w->u->v->t? That would be:
s->v (4)
v->w (10) -> but then at w, we want to go to u? w->u is 0.
But we can go from w to u by using the reverse edge? No.
Wait, we have an edge from u to w (9). That is a reverse edge of w->u. So if we are at w, we cannot go to u. But if we are at u, we can go to w.
So the path might be: s->v->? then to u? We don't have v->u.
How about: s->v->? then to t? We have v->t but it's 0.
I'm confused.
Let me look up the standard Edmonds-Karp steps for this graph:
Step 1: s->v->t (12)
Step 2: s->w->u->t (9)
Step 3: s->v->w->u->t (2)
But in step 3, we use w->u which is already used? How?
Actually, after step 2, the residual graph has:
s->v:4
s->w:4
v->w:10
w->u:0
u->t:5
and reverse edges:
v->s:12
w->s:9
u->w:9
t->u:9
t->v:12
Now, consider the path s->v->w->u->t:
s->v:4 -> ok
v->w:10 -> ok
w->u:0 -> not ok.
But wait, we have an edge from u to w (9). That is not from w to u.
So how can we go from w to u?
The key is: in the residual graph, we have an edge from u to w (9). That means we can send flow from u to w. But we are at w and we want to go to u. We need an edge from w to u.
However, note that the edge from w to u in the original is a forward edge, and we have used it 9, so the residual is 0. But the reverse edge of that is u->w, which is 9.
So to go from w to u, we would need to use the forward edge w->u, but it's 0.
Therefore, the path s->v->w->u->t is not available.
Then how do we get 2 more?
Let me try to find an augmenting path:
From s, we can go to v (4) and w (4).
From v, we can go to w (10) and s (12). So we can go to w.
From w, we can go to s (9). So we have a cycle? But BFS won't go to visited nodes.
From v, we can also go to t? That's 0.
What about from u? We haven't reached u.
How to reach u? We have an edge from w to u? That's 0. But we have an edge from u to w? That's 9. So if we are at u, we can go to w, but we are not at u.
What about from t? We haven't reached t.
So it seems we cannot reach t.
But wait, we have an edge from u to t (5). So if we can reach u, then we can go to t.
How to reach u? We have an edge from w to u? That's 0. And from v to u? That's 0.
But we have an edge from u to v (4). So if we are at v, we can go to u? No, because the edge is from u to v, not v to u.
So we cannot reach u.
Then how is the maximum flow 23?
I think I have a mistake in the example. Let me check the capacities:
s->v:16
s->w:13
v->w:10
v->t:12
w->u:9
u->v:4
u->t:14
The maximum flow is indeed 23? Let me try to construct a flow:
Let f(s,v)=13, f(s,w)=10? Then total from s is 23.
Now, at v: incoming 13, outgoing? v->t must be <=12, and v->w? We have 13 coming in, so we must send 13 out. We can send 12 to t and 1 to w? Then at w: incoming 10 from s and 1 from v, total 11. Then w->u must be <=9, so we send 9 to u. Then at u: incoming 9, and u->t can take 14, so we send 9 to t. Then at t: from v 12 and from u 9, total 21. But we have 2 more from s? We sent 13 from s->v and 10 from s->w, total 23. But we only have 21 at t.
So we need to send 2 more from s->v->? to t.
How about: f(s,v)=16, f(s,w)=7? Then at v: 16 in, send 12 to t and 4 to w. Then at w: 7 from s and 4 from v, total 11. Then w->u:9, so 9 to u. Then at u: 9 in, and then u->t:9? But u->t can take 14, so we send 9. Then at t: 12+9=21. Still 21.
What if we use u->v? We haven't used that.
Let me try:
f(s,v)=12, f(s,w)=11? Then at v:12 in, send 12 to t? Then v->w:0. At w:11 in, then w->u:9, then u->t:9? Then t gets 12+9=21.
Still 21.
How to get 23?
I think the maximum flow is 21? But the literature says 23.
Wait, I found an error: in the graph, there is an edge from u to v (4). So we can use that.
Let me try:
f(s,v)=16, f(s,w)=7.
Then at v:16 in. Send 12 to t, and 4 to w? Then at w:7+4=11. Then w->u:9, so 9 to u. Then at u:9 in. Then we can send 9 to t? But then we have u->v? We haven't used that.
What if we send from u to v? Then at v we have extra 4? But we already sent 12 to t and 4 to w, so v has 16 in and 16 out? That's balanced.
But if we send from u to v, then we are sending flow from u to v, which means we are reducing the net flow at v? Actually, if we send 4 from u to v, then at v we have 16 in (from s) and 4 in from u, total 20. Then we must send 20 out. We can send 12 to t and 8 to w? But v->w is only 10, so we can send 8 to w. Then at w:7 from s and 8 from v, total 15. Then w->u:9, so we send 9 to u. Then at u:9 from w and 4? Wait, we sent 4 from u to v? That's outflow from u. So at u: inflow from w:9, outflow to v:4 and to t:? We haven't sent to t yet.
This is getting messy.
Let me set up equations:
Let x = f(s,v)
y = f(s,w)
a = f(v,w)
b = f(v,t)
c = f(w,u)
d = f(u,v)
e = f(u,t)
Constraints:
0<=x<=16
0<=y<=13
0<=a<=10
0<=b<=12
0<=c<=9
0<=d<=4
0<=e<=14
Flow conservation:
At v: x = a + b + d (inflow from s, outflow to w, t, and u? Actually, outflow from v: to w, to t, and to u? But we have an edge from v to u? No, we have an edge from u to v. So at v, inflow: from s (x) and from u (d). Outflow: to w (a) and to t (b). So:
x + d = a + b
At w: y + a = c (inflow: from s and from v; outflow: to u)
At u: c + d = e (inflow: from w and from v? Actually, from w (c) and from v? We have an edge from u to v? That's outflow. So inflow at u: from w (c). Outflow: to v (d) and to t (e). So:
c = d + e
At t: b + e = ?
The value of the flow is: x + y.
We want to maximize x+y.
From w: y + a = c => c = y+a.
From u: c = d+e => y+a = d+e.
From v: x+d = a+b => x = a+b-d.
Then x+y = (a+b-d) + y = a+b-d+y.
But from y+a = d+e, we have y = d+e - a.
So x+y = a+b-d + (d+e-a) = b+e.
So the flow value is b+e.
We want to maximize b+e.
Constraints:
b <= 12
e <= 14
and from y = d+e-a >=0 and y<=13, and a<=10, d<=4.
Also, from x = a+b-d >=0 and x<=16.
And from c = y+a <=9? Actually, c<=9.
So we have:
c = y+a <=9
but y = d+e-a, so c = d+e <=9.
So d+e <=9.
Then b+e is maximized when b=12 and e as large as possible, but e <=14 and d+e<=9, so e<=9-d<=9.
So if we set d=0, then e<=9, so b+e <=12+9=21.
If we set d=4, then e<=5, so b+e<=12+5=17.
So the maximum is 21.
Therefore, the maximum flow is 21, not 23.
I think the example I recalled is wrong. The correct maximum flow for that graph is 21.
So let me adjust: after two augmentations we have 21, and then we cannot find another augmenting path, so we stop at 21.
So the algorithm works.
But wait, the example in CLRS (I think) has a different graph? Let me check:
In CLRS, the graph is:
s -> v (16)
s -> w (13)
v -> w (10)
v -> t (12)
w -> u (9)
u -> v (4)
u -> t (14)
And the maximum flow is 23? I must be missing something.
Actually, I found online that the maximum flow for this graph is 23. How?
Let me try to find a flow of 23:
We need x+y=23.
From above, x+y = b+e.
So we need b+e=23.
But b<=12, e<=14, so maximum b+e=26, but we have the constraint d+e<=9.
If b=12, then e=11? But e<=14, and d+e<=9, so if e=11, then d<=-2, impossible.
If b=10, e=13? Then d+e<=9 -> d<=-4.
So it's impossible to have b+e=23.
Therefore, the maximum flow cannot be 23.
I think the graph in CLRS is different. Let me check the book.
Actually, in the third edition of CLRS, the graph is:
Figure 26.1 (a) in Chapter 26:
s -> v (16)
s -> w (13)
v -> w (10)
v -> t (12)
w -> x (9)
x -> v (4)
x -> t (14)
So there is an extra node x? And the edge w->x (9) and x->v (4) and x->t (14).
And the maximum flow is 23.
So my example was missing the node x.
So the correct example has:
nodes: s, v, w, x, t
edges:
s->v:16
s->w:13
v->w:10
v->t:12
w->x:9
x->v:4
x->t:14
Then the maximum flow is 23.
Let me adjust our algorithm to this graph.
We'll represent:
graph = {
's': {'v':16, 'w':13},
'v': {'w':10, 't':12},
'w': {'x':9},
'x': {'v':4, 't':14},
't': {}
}
Then we expect max flow 23.
Let's run through the algorithm:
Step 1: BFS finds s->v->t (12) -> max_flow=12.
After update:
residual:
s: {v:4, w:13}
v: {s:12, w:10, t:0}
w: {s:0, x:9}
x: {v:4, t:14}
t: {v:12}
Step 2: BFS finds s->w->x->t? Let's see:
s->w (13) and s->v (4). BFS will go to w and v.
From w: to s (0) and to x (9). So x.
From x: to v (4) and to t (14). So t.
Path: s->w->x->t, bottleneck = min(13,9,14)=9.
Update:
s->w:13-9=4
w->x:9-9=0
x->t:14-9=5
reverse:
w->s:0+9=9
x->w:0+9=9
t->x:0+9=9
max_flow=12+9=21.
Step 3: BFS:
residual:
s: {v:4, w:4}
v: {s:12, w:10, t:0}
w: {s:9, x:0}
x: {w:9, v:4, t:5}
t: {v:12, x:9}
BFS:
s -> v (4) and s -> w (4).
From v: to s (12), to w (10), to t (0). So we can go to w (10) and s (12, but s visited). So w.
From w: to s (9) and to x (0). So s (9) but s visited, and x (0). So no new node from w? Wait, w has x with capacity 0? But we have residual[w][x]=0? Yes.
From v we also have t (0) so no.
But we also have from s to w? We already did.
What about from x? We haven't reached x.
How to reach x? From w to x is 0. But we have an edge from x to w (9). That's reverse.
But we are at w and we want to go to x? We need w->x, which is 0.
Alternatively, from v we can go to w (10) and then from w we can go to x? No.
What about from v to s? That's 12.
So we haven't reached t or x.
But wait, from s we also went to v. From v we went to w. Then from w we can go to s? That's back.
So we need to find another path.
What about: s->v->? from v we can go to t? That's 0.
Or s->w->? from w we can go to s? That's back.
So we are stuck? But we have 21, and we expect 23.
Let me check the residual graph again:
s: {v:4, w:4}
v: {s:12, w:10, t:0}
w: {s:9, x:0}
x: {w:9, v:4, t:5}
t: {v:12, x:9}
Now, consider the path: s->v->w->x->t? But w->x is 0.
How about: s->v->? then to x? We don't have v->x.
How about: s->w->x->v->t? But w->x is 0.
Wait, we have an edge from x to v (4). So if we can get to x, then we can go to v and then to t? But v->t is 0.
But we have an edge from t to v (12). That's reverse.
So if we are at v, we can go to t? That's 0.
But we can go from v to s (12) and then from s to w? That's a cycle.
Let me try BFS again, but carefully:
Start at s. Neighbors: v (4) and w (4). Mark v and w.
Then from v: neighbors: s (12, visited), w (10, visited? w is visited because we came from s to w? But we also came from s to v. So w is visited from s? Actually, in BFS, we mark nodes when we first see them. So when we process s, we add v and w to the queue and mark them. Then when we process v, we look at its neighbors: s (visited), w (visited), t (0). So no new node.
Then from w: neighbors: s (9, visited), x (0). So no new node.
So BFS ends without reaching t.
But we have 21 and we expect 23.
What's wrong?
I think I missed the path: s->v->w->x->v->t? That would be:
s->v (4)
v->w (10)
w->x (0) -> not possible.
Alternatively, s->w->x->v->t? But w->x is 0.
Wait, we have an edge from x to w (9). That is a reverse edge of w->x. So if we are at w, we cannot go to x on that. But if we are at x, we can go to w.
So to get to x, we must come from w? But w->x is 0.
How about: s->v->? and then from v we can go to x? No.
So we cannot reach x.
Then how do we get 2 more?
Let me reconsider the residual graph after two augmentations:
After first: path s->v->t (12)
After second: path s->w->x->t (9)
Then the residual:
s->v:4
s->w:4
v->w:10
w->x:0
x->v:4
x->t:5
and reverse edges:
v->s:12
w->s:9
x->w:9
t->x:9
t->v:12
Now, note that we have an edge from x to v (4). So we can go from x to v.
How to get to x? We have an edge from w to x? That's 0. But we have an edge from x to w? That's 9.
So if we are at w, we cannot go to x. But if we are at x, we can go to w.
How to get to x? We must have an edge from some node to x. The only edge to x is from w? In the original, only w->x. So in the residual, we have w->x (0) and x->w (9). So to get to x, we need an edge from w to x? That's 0.
So we cannot get to x.
Then how do we use the edge x->v?
We must get to x first.
But we cannot.
So the maximum flow is 21.
But the example says 23.
Wait, in the example, there is an edge from x to v (4). So we can use that in a path that goes from s to v, then from v to w? But then from w to x? That's 0.
Alternatively, we can go from s to w, then from w to x? That's 0.
So we cannot.
I think I found the issue: in the second augmentation, we used s->w->x->t. But note that we have an edge from x to v (4). So after that, we have:
residual[x][v] = 4 (unchanged? Actually, we didn't use x->v in that path, so it's still 4).
Now, consider the path: s->v->? We have s->v (4). Then from v we can go to w (10). Then from w we can go to x? That's 0. But we have an edge from x to w? That's 9, but that's from x to w.
What if we go: s->v->w->? then from w we can go to s? That's 9. Then from s to w? That would be a cycle.
Alternatively, we can go: s->v->w->x? Not possible.
How about: s->w->x->v->t? But w->x is 0.
Wait, we have an edge from x to v (4). So if we can get to x, then we can go to v and then to t? But v->t is 0.
But we have an edge from t to v (12). So if we are at v, we can go to t? That's 0.
But we can go from v to s (12) and then from s to v? That's a cycle.
I'm stuck.
Let me look for the standard Edmonds-Karp steps for this graph:
Step 1: s->v->t (12)
Step 2: s->w->x->t (9)
Step 3: s->v->w->x->v->t? That would be:
s->v (4)
v->w (10)
w->x (0) -> not possible.
Step 3 in some explanations: s->v->w->x->t? But w->x is 0.
Actually, I found a step-by-step:
After first two, the residual graph:
s->v:4
s->w:4
v->w:10
w->x:0
x->v:4
x->t:5
Then the third augmenting path is: s->v->w->x->v->t? That uses v twice.
That's not allowed.
Wait, I think I have a mistake: after the first augmentation, we have:
residual[s][v]=4, residual[v][s]=12, residual[v][t]=0, residual[t][v]=12.
After the second:
residual[s][w]=4, residual[w][s]=9, residual[w][x]=0, residual[x][w]=9, residual[x][t]=5, residual[t][x]=9.
Now, we have residual[x][v]=4 (unchanged).
Now, consider the path: s->v->w->x->v->t? That would be:
s->v:4
v->w:10
w->x:0 -> cannot.
What about: s->v->? then to x? We don't have v->x.
How about: s->w->x->v->t? But w->x is 0.
So we cannot.
Then how to get 2 more?
Let me try to find any path from s to t in the residual:
s can go to v and w.
From v: to w (10) and to s (12). So we can go to w.
From w: to s (9) and to x (0). So we can only go to s (which is visited).
From s we already did.
So we cannot reach t.
Therefore, the maximum flow is 21.
But the example says 23.
I must have the wrong example. Let me double-check the graph from CLRS:
In the book (3rd edition), Figure 26.1 (a) has:
s -> v (16)
s -> w (13)
v -> w (10)
v -> t (12)
w -> x (9)
x -> v (4)
x -> t (14)
And the maximum flow is 23.
How?
Let me try to compute the flow manually:
We want to send 23 from s.
Let f(s,v)=x, f(s,w)=y, with x+y=23.
At v: x = f(v,w) + f(v,t) + f(v,?) but we have an edge from x to v? That's inflow to v? Actually, we have an edge from x to v, so at v, inflow: from s (x) and from x (f(x,v)). Outflow: to w (f(v,w)), to t (f(v,t)).
So: x + f(x,v) = f(v,w) + f(v,t) ...(1)
At w: y + f(v,w) = f(w,x) ...(2)
At x: f(w,x) = f(x,v) + f(x,t) ...(3)
At t: f(v,t) + f(x,t) = ?
The flow value is x+y.
We want to maximize x+y.
From (2): f(w,x) = y + f(v,w) <=9.
From (3): f(w,x) = f(x,v) + f(x,t) <= 4+14=18, but also <=9 from (2).
From (1): x + f(x,v) = f(v,w) + f(v,t) <= 10+12=22.
And x<=16, y<=13.
We want x+y=23, so x>=10, y>=10? Actually, x+y=23, and x<=16, y<=13, so x>=10, y>=10.
Let me try x=16, y=7.
Then from (2): f(w,x) = 7 + f(v,w) <=9 -> f(v,w) <=2.
From (1): 16 + f(x,v) = f(v,w) + f(v,t). Since f(v,w)<=2, then f(v,t) >=14+f(x,v)? But f(v,t)<=12, so impossible.
Try x=15, y=8.
Then from (2): f(w,x)=8+f(v,w)<=9 -> f(v,w)<=1.
From (1): 15+f(x,v)=f(v,w)+f(v,t) <=1+12=13 -> f(x,v) <= -2, impossible.
Try x=14, y=9.
Then from (2): f(w,x)=9+f(v,w)<=9 -> f(v,w)=0.
Then from (1): 14+f(x,v)=f(v,t) <=12 -> f(x,v) <= -2, impossible.
Try x=13, y=10.
Then from (2): f(w,x)=10+f(v,w)<=9 -> f(v,w)<=-1, impossible.
So it's impossible to have x+y=23.
Therefore, the maximum flow must be less than 23.
Let me find the maximum:
We have from (2): f(w,x) = y + f(v,w) <=9.
So y <=9 - f(v,w) <=9.
And from (1): x + f(x,v) = f(v,w) + f(v,t) <=10+12=22.
And from (3): f(w,x) = f(x,v) + f(x,t) <=4+14=18, but also <=9.
So f(w,x) <=9.
Now, x+y = x + (f(w,x) - f(v,w)) from (2) -> y = f(w,x) - f(v,w).
So x+y = x + f(w,x) - f(v,w).
But from (1): x = f(v,w) + f(v,t) - f(x,v).
So x+y = (f(v,w) + f(v,t) - f(x,v)) + f(w,x) - f(v,w) = f(v,t) + f(w,x) - f(x,v).
And we have f(w,x) <=9, f(v,t)<=12, f(x,v)>=0.
So x+y <=12+9=21.
And we can achieve 21 by setting:
f(v,t)=12, f(w,x)=9, f(x,v)=0.
Then from (1): x = f(v,w) + 12.
From (2): y = 9 - f(v,w).
Then x+y = f(v,w)+12 + 9 - f(v,w) =21.
And we need x<=16 and y<=13.
So f(v,w) can be any value? But from (3): f(w,x)=9 = f(x,v)+f(x,t)=0+f(x,t) -> f(x,t)=9.
Then from (2): y = 9 - f(v,w). So if f(v,w)=0, then y=9, x=12.
Check: x=12<=16, y=9<=13.
So flow:
f(s,v)=12, f(s,w)=9.
f(v,w)=0, f(v,t)=12.
f(w,x)=9, f(x,t)=9.
f(x,v)=0.
Then at v: in:12, out:12 (to t) -> balanced.
At w: in:9, out:9 -> balanced.
At x: in:9, out:9 -> balanced.
At t: in:12+9=21.
So indeed maximum flow is 21.
Therefore, the example with 5 nodes (s,v,w,x,t) has maximum flow 21, not 23.
I think I have a persistent wrong memory. The example in CLRS must be different.
Let me check the actual figure in CLRS:
In the 3rd edition, Chapter 26, Figure 26.1 (a) has:
s -> v (16)
s -> w (13)
v -> w (10)
v -> t (12)
w -> x (9)
x -> v (4)
x -> t (14)
And the maximum flow is reported as 23? Actually, in the text, they say the maximum flow is 23. But my calculation shows 21.
Wait, I see: in the figure, the capacities are:
s->v:16
s->w:13
v->w:10
v->t:12
w->x:9
x->v:4
x->t:14
And they show a flow of 23? How?
Let me try to construct a flow of 23:
We need to send 23 from s.
Suppose we send 13 from s->w and 10 from s->v? Then total 23.
Then at w: in 13, out to x:9, so we have 4 left? But we don't have an edge from w to anywhere else? We have only w->x.
So we must send 13 from s->w to x? But x can only take 9 from w? Then we have 4 stuck at w.
Alternatively, send 16 from s->v and 7 from s->w.
Then at v: in 16, out: to w? to t? and from x? We have an edge from x to v, so that's inflow? Actually, the edge is from x to v, so at v, we have inflow from s (16) and from x (some amount), and outflow to w and t.
Let me set:
f(s,v)=16
f(s,w)=7
Then at w: in 7, out to x: must be 7? But w->x has capacity 9, so we can send 7.
Then at x: in 7 from w, and then we can send to v and t.
Let f(x,v)=4 (max) and f(x,t)=3? Then at x: in 7, out 4+3=7.
Then at v: in from s:16, from x:4, total 20. Out to w? We haven't used v->w yet. And to t: we can send 12 to t.
So at v: we need to send 20 out. We can send 12 to t and 8 to w? But v->w has capacity 10, so we can send 8.
Then at w: in from s:7 and from v:8, total 15. But we already sent 7 from w to x? Wait, we sent 7 from w to x? Actually, we set f(w,x)=7? But then at w, we have inflow 7 (from s) and 8 (from v) total 15, and outflow to x:7? That doesn't balance.
We need to have outflow equal inflow at w.
So at w: inflow: f(s,w)+f(v,w) = 7 + f(v,w)
outflow: f(w,x)
So f(w,x) = 7+f(v,w).
But f(w,x)<=9.
So f(v,w) <=2.
Then at v: inflow: f(s,v)+f(x,v)=16+4=20
outflow: f(v,w)+f(v,t) = f(v,w)+12.
So 20 = f(v,w)+12 -> f(v,w)=8.
But then f(v,w)=8, which is more than 2? Contradiction.
So it's impossible.
Therefore, the maximum flow is 21.
I think the example in CLRS might have a different capacity on w->x? Or maybe I misread.
Actually, I found the solution manual for CLRS:
In the solution manual for problem 26.1-2, they have the same graph and they say the maximum flow is 23.
How?
Let me try to set:
f(s,v)=13
f(s,w)=10
Then at v: in 13, out: to w? to t? and from x? We have x->v.
Let f(v,t)=12 (max)
Then at v: we need to send 13-12=1 to somewhere else? We can send to w? But then at w: in from s:10 and from v:1, total 11. Then w->x:9, so we send 9 to x, and then we have 2 left at w? But w has no other outlet.
Alternatively, we can send from v to w and then from w to x, but w->x is 9.
Let me set:
f(v,w)=1
f(v,t)=12
Then at v: in 13, out 1+12=13.
At w: in from s:10 and from v:1, total 11. Out to x:9, so we have 2 left? But w has no other outlet.
So we need to send the 2 from w to x? But w->x is already at 9, capacity 9.
So we cannot.
What if we use x->v? Then we can send from x to v? But that would be sending flow back to v.
Suppose we send from w to x:9, then at x: in 9, out to v? and to t.
Let f(x,v)=4 (max) and f(x,t)=5.
Then at x: in 9, out 4+5=9.
Then at v: in from s:13 and from x:4, total 17. Out to w:1 and to t:12? That's 13, but we have 17 in, so we need to send 4 more? We can send to w? But we already set f(v,w)=1. We can increase f(v,w) to 5? But then at w: in from s:10 and from v:5, total 15, and out to x:9, so we have 6 left? Not balanced.
This is complicated.
Let me use the equations:
We have:
(1) x + f(x,v) = f(v,w) + f(v,t)
(2) y + f(v,w) = f(w,x)
(3) f(w,x) = f(x,v) + f(x,t)
and x<=16, y<=13, f(v,w)<=10, f(v,t)<=12, f(w,x)<=9, f(x,v)<=4, f(x,t)<=14.
And we want to maximize x+y.
From (2) and (3): y + f(v,w) = f(x,v) + f(x,t) <=4+14=18.
So y <= 18 - f(v,w).
But also from (2): f(w,x) = y+f(v,w) <=9.
So y <= 9 - f(v,w).
Therefore, y <= min(13, 9-f(v,w)).
And from (1): x = f(v,w) + f(v,t) - f(x,v) <= f(v,w) +12.
So x+y <= f(v,w)+12 + 9 - f(v,w) =21.
So the maximum is 21.
Therefore, the maximum flow is 21.
So the example in CLRS must be wrong? Or I have the wrong capacities.
Let me check the figure again: in the book, the capacity on w->x is 9, on x->v is 4, on x->t is 14.
So my calculation is correct.
But the solution manual says 23? I must be missing something.
Wait, in the solution manual for problem 26.1-2, they have a different graph? Actually, problem 26.1-2 is about the Ford-Fulkerson method, and they have a graph with capacities:
s->v:16
s->w:13
v->w:10
v->t:12
w->x:9
x->v:4
x->t:14
and they say the maximum flow is 23.
How?
Let me try to set:
f(s,v)=13
f(s,w)=10
Then at v: in 13, out: to w? to t? and from x? We have x->v.
Set f(v,t)=12.
Then we need to send 1 more from v? We can send to w.
Set f(v,w)=1.
Then at w: in from s:10 and from v:1, total 11. Out to x:9, so we have 2 left.
Then at x: in 9 from w, and we can send to v and t.
Set f(x,v)=4 and f(x,t)=5? Then at x: out 4+5=9, balanced.
Then at v: in from s:13 and from x:4, total 17. Out to w:1 and to t:12, total 13. So we have 4 more at v? We need to send them out? But we already used all edges from v? v->w is only 1? We can increase f(v,w) to 5? But then at w: in from s:10 and from v:5, total 15, and out to x:9, so we have 6 left.
Alternatively, we can send from v to w more? But then at w we have more inflow.
This is not working.
What if we send from x to v more? But x->v is only 4.
What if we send from s to v more? But we set f(s,v)=13, and capacity is 16, so we can send more.
Let me try:
f(s,v)=14
f(s,w)=9
Then at v: in 14, out: to t:12, so 2 left? Send to w:2.
Then at w: in from s:9 and from v:2, total 11. Out to x:9, so 2 left.
Then at x: in 9, out to v:4 and to t:5? Then at v: in from x:4, so total in at v:14+4=18, out: to w:2 and to t:12, total 14? Not balanced.
We need to adjust.
From the equations, we have x+y = f(v,t) + f(w,x) - f(x,v).
To maximize, we want f(v,t)=12, f(w,x)=9, f(x,v)=0 -> 21.
Or f(v,t)=12, f(w,x)=9, f(x,v)=4 -> 17.
So maximum is 21.
Therefore, I think the example in CLRS is misreported? Or I have a different edition.
Given the confusion, I will trust the algorithm and test with a known simple example.
Simple example:
graph = {
's': {'a': 10, 'b': 10},
'a': {'b': 2, 't': 10},
'b': {'t': 10},
't': {}
}
Expected max flow: 20? Let's see.
Path1: s->a->t (10)
Then residual:
s->a:0, a->t:0, reverse: a->s:10, t->a:10.
Path2: s->b->t (10)
Then residual:
s->b:0, b->t:0, reverse: b->s:10, t->b:10.
Now, we have s->a:0, s->b:0, so no more.
But we also have a->b (2) and b->a? We don't have b->a initially.
Actually, after first path, we have:
residual:
s: {'a':0, 'b':10}
a: {'s':10, 'b':2, 't':0}
b: {'s':0, 't':10}
t: {'a':10, 'b':0}
Then BFS for second path: s->b->t (10).
Then after second, we have:
s: {'a':0, 'b':0}
a: {'s':10, 'b':2, 't':0}
b: {'s':10, 't':0}
t: {'a':10, 'b':10}
Now, can we find a third path? s has no outgoing positive capacity? s->a:0, s->b:0. But we have reverse edges from a and b to s? That would be going back to s.
So we stop at 20.
But wait, we have an edge from a to b (2). We haven't used that.
What if we send 2 from s->a->b->t? But s->a is 0. However, we have an edge from a to s? That's 10. So we could send from s to b? That's 0. But we have an edge from b to s? That's 10.
So we can't.
So maximum flow is 20.
But the min cut is {s} and {a,b,t} with capacity 10+10=20.
So it's correct.
Now, if we change the graph to:
graph = {
's': {'a': 10, 'b': 10},
'a': {'b': 2, 't': 10},
'b': {'a': 2, 't': 10},
't': {}
}
Then we have two edges between a and b? Actually, we have a->b and b->a.
Then the maximum flow might be more? Let's see.
We can send 10 on s->a->t and 10 on s->b->t, then we have 2 on a->b and 2 on b->a? But that cancels.
Actually, we can send 10 on s->a->t, 10 on s->b->t, and then 2 on s->a->b->t? But s->a is already at capacity? After sending 10 on s->a->t, s->a has 0.
But we can use the reverse edge? We have residual[a][s]=10.
So we can send from s to b (10) and then from b to a (2) and then from a to t? But a->t is 0.
Alternatively, we can send from s to a (10) and then from a to b (2) and then from b to t (10) -> but b->t is already used 10? After sending 10 on s->b->t, b->t is 0.
So no.
So maximum flow is still 20.
Given the complexity, I think the algorithm is correct.
So I will code the Edmonds-Karp algorithm as described.
Let's code with the example that we know works: the simple one with max flow 20.
We'll test with that.
But note: in the example with the node x, we got 21, which is the correct maximum flow for that graph.
So the algorithm works.
Now, let's code.</think>