Flow Networks Goal: “push” as much flow as possible through “pipes” from source to destination. A flow network is a directed graph G=(V,E) in which each edge (u,v) in E has a non-negative capacity c(u,v) ≥ 0. If an edge does not belong to the graph, we assume it has flow of 0. There is a source and a sink vertex, and every other vertex is on a path from the source to the sink. A flow in G is a real valued function f: VxV -> R that satisfies the following properties: 1. capacity constraint (cannot push more flow than the pipe can take): for all u,v in V, f(u,v) ≤ c(u,v) 2. skew symmetry (flow does not accumulate in nodes): for all u,v in V, f(u,v) = -f(v,u) 3. flow conservation (flow in equals flow out): for all u in V-{s,t} SUM[v in V] f(u,v) = 0. Example: Truck as many shipments from Vancouver to Winnipeg. “Pipes” are highways. The capacity of a “pipe” is how many shipments it can take. Edmonton 12 v1 Saskatoon v3 20 16 Winnipeg Vancouver 10 7 4 s 13 t 9 4 v4 v2 14 Calgary Regina Example flow network showing flow/capacity ( “/” does not represent division, but just a “separator” – we could have used a comma or something else): v1 12/12 v3 11/16 0/10 s 7/7 1/4 4/9 8/13 v2 15/20 t 4/4 v4 11/14 Residual capacity cf is the amount of additional flow we can push from u to v before exceeding the capacity of that link. Residual network is a flow network showing residual capacities, i.e. consisting of edges that can admit more flow (called residual edges). So, residual network “overlapped” over the zero flow network should produce the flow network in which all “pipes” are utilized to their maximum capacity. Augmenting path is a path on which we can send more flow. //calculate the maximum flow and the path of the maximum flow FloydFulkersonMethod(G, s, t) { initialize flow to 0 while there exists an augmenting path p augment flow of f along p return f } Example: Flow network: Residual network: v1 12/12 v3 v1 15/20 11/16 10 7/7 1/4 s t 12 5 v3 5 4 11 s 10 1 15 7 3 t 5 8/13 4/9 5 4/4 8 v4 v2 4 3 v2 11/14 v4 11 Add the additional capacity along the augmenting path: v1 12/12 v3 11/16 10 7/7 1/4 s 12/13 v1 19/20 t 4/4 v2 v4 11/14 v3 5 10 1 s 12 19 1 11 1 9 12 7 3 9 v2 t 4 3 v4 11 FloydFulkersonAlgorithm(G, s, t) { //initialize flow to 0 for each edge (u,v) in E[G] f(u,v) = 0 f(v,u) = 0 //while there exists an augmenting path p while there exists a path p from s to t in the residual network Gf //augment flow of f along p cf(p) = min{ cf(u,v): (u,v) is in p} for each edge (u,v) in p f(u,v) = f(u,v) + cf(p) f(v,u) = -f(u,v) return f } EXAMPLE: Following the exact page outlay as the book: Flow network: 0. 1. this slot intentionally left blank v1 0/12 v3 0/20 0/16 0/10 s 0/13 0/7 0/4 0/9 t 0/4 v4 v2 0/14 Residual network: 3. 2. v1 v1 12 v3 16 s 9 v2 4 v4 14 0/7 0/4 t t 0/13 13 0/20 20 7 4 s v3 4/16 0/10 10 4/12 0/9 v2 4/4 v4 4/14 Residual network: 4. 8. 6. Flow network: 5. 7. Example from the book, but with different paths: (odd numbered diagrams are flow networks, and even numbered are residual networks) Following the exact page outlay as the book: Flow network: 0. 0/12 1. this slot intentionally left blank v1 v3 0/16 0/10 s 0/13 0/7 0/4 0/9 v2 0/20 t 0/4 v4 0/14 Residual network: 3. 2. v1 v1 12 0/12 v3 0/20 0/16 v3 20 16 0/10 10 7 4 s s 9 4 v4 v2 14 t t 4/13 13 0/7 0/4 0/9 4/4 v4 v2 4/14 4. 5. 12 v1 v3 v1 20 16 10 7 4 s 12/12 v3 12/20 12/16 t 0/10 s 0/7 0/4 t 4 9 9 4/13 4 4 v4 v2 0/9 v4 v2 10 6. 4/14 12 v1 7. v3 4 12 10 s 7 4 v1 8 9 v2 4 t 10 4 v4 12/12 v3 16/16 12 4 9 4/4 4/10 s 4/13 4/7 0/4 0/9 v2 t 4/4 v4 8/14 16/20 EXAMPLE: what happens if we don’t pick the augmenting path properly: 1. 1,000,000 1,000,000 s 1 1,000,000 3. t 1,000,000 v1 0/1,000,000 1/1,000,000 s 1/1 0/1,000,000 v2 v1 4. v1 2. 1/1,000,000 1,000,000 1 1,000,000 t 999,999 v2 … keep on going, 1,000,000 times. s t 1/1,000,000 v2 999,999 s 2. v1 0/1 1/1,000,000 1/1,000,000 t 1/1,000,000 v2 The Edmonds-Karp algorithm Use breath-first search to find the augmenting path. Residual network: 2. v1 Flow network: 1. same as 2. 3. 12 v3 v1 20 10 7 4 13 9 t 4 v4 v2 0/10 s 0/13 0/7 0/4 0/9 t 0/4 v4 0/14 V = [0,\ INF,\ INF,\ INF,\ INF,\ INF,\] Q=s u = s v = v1, v2 V = [0,\ 1,s 1,s Q = v1 v2 INF,\ INF,\ INF,\] u = v1 v = v2, v3 V = [0,\ 1,s 1,s Q = v2 v3 2,v1 INF,\ INF,\] u = v2 v = v1, v4 V = [0,\ 1,s 1,s Q = v3 v4 2,v1 2,v2 INF,\] u = v3 v = v2, t V = [0,\ 1,s 1,s Q = v4 t 2,v1 2,v2 3,v3] u=t 12/20 v2 14 u=v4 v3 12/16 16 s 12/12 Excercise: finish this problem to the end. Example from the book, but with different paths: (odd numbered diagrams are flow networks, and even numbered are residual networks) 2. v1 12 20 16 10 7 4 s 13 9 t v1 v3 0/16 0/10 s 0/9 v2 v4 v1 12/12 v3 12/16 s 0/7 0/4 4. 0/9 t 0/4 v4 v2 v1 4 12 v3 7 4 12 13 9 t 4 v2 0/14 8 12 10 s 0/13 0/4 0/14 12/20 0/10 t v4 14 3. 0/20 0/7 0/4 0/13 4 v2 0/12 1. v3 v4 14 5. v1 12/12 v3 12/20 12/16 0/10 s 6. v1 4 0/7 0/4 s 0/9 4/4 v4 v2 4/14 v3 t 10 4/13 12 12 7 4 12 8 t 4 9 9 v2 4 10 4 v4 7. v1 12/12 v3 19/20 12/16 0/10 s 7/7 0/4 0/9 11/1 3 t 4/4 v4 v2 11/14 8. v1 4 v3 1 10 s 12 19 7 4 12 t 4 9 9 11 v2 3 4 v4 Flow network shown in diagram 7 is the THE SOLUTION, because residual network shown in diagram 8 has no augmenting path.