Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ford-Fulkerson.

Similar presentations


Presentation on theme: "Ford-Fulkerson."— Presentation transcript:

1 Ford-Fulkerson

2 Max-flow Maximize the total amount of flow from s to t subject to two constraints Flow on edge e doesn’t exceed capacity(e) For every node v ≠ {s, t}, incoming flow is equal to outgoing flow

3 Ford-Fulkerson The general idea is simple while (hasAugmentingPath) {
public FordFulkerson(FlowNetwork G, int s, int t) { V = G.V(); value = excess(G, t); while (hasAugmentingPath(G, s, t)) { double bottle = Double.POSITIVE_INFINITY; for (int v = t; v != s; v = edgeTo[v].other(v)) { bottle = Math.min(bottle, edgeTo[v].residualCapacityTo(v)); } edgeTo[v].addResidualFlowTo(v, bottle); value += bottle; The general idea is simple while (hasAugmentingPath) { augment the the current max flow by the minimum residual capacity on the augmenting path. }

4 Ford-Fulkerson The general idea is simple while (hasAugmentingPath) {
public FordFulkerson(FlowNetwork G, int s, int t) { V = G.V(); value = excess(G, t); while (hasAugmentingPath(G, s, t)) { double bottle = Double.POSITIVE_INFINITY; for (int v = t; v != s; v = edgeTo[v].other(v)) { bottle = Math.min(bottle, edgeTo[v].residualCapacityTo(v)); } edgeTo[v].addResidualFlowTo(v, bottle); value += bottle; The general idea is simple while (hasAugmentingPath) { augment the the current max flow by the minimum residual capacity on the augmenting path. } To find minimum residual capacity edgeTo: backtrack trace. edgeTo[t] is the last edge on shortest residual s->t path.

5 Ford-Fulkerson The general idea is simple while (hasAugmentingPath) {
public FordFulkerson(FlowNetwork G, int s, int t) { V = G.V(); value = excess(G, t); while (hasAugmentingPath(G, s, t)) { double bottle = Double.POSITIVE_INFINITY; for (int v = t; v != s; v = edgeTo[v].other(v)) { bottle = Math.min(bottle, edgeTo[v].residualCapacityTo(v)); } edgeTo[v].addResidualFlowTo(v, bottle); value += bottle; The general idea is simple while (hasAugmentingPath) { augment the the current max flow by the minimum residual capacity on the augmenting path. } Two remaining problems How to find augmenting path Adjust the residual graph after upating the max flow

6 Augmenting path Find augmenting path hasAugmentingPath ? t s B C A D
Flow = = 17 ? Find augmenting path s->A->t [4] s->B->C->t [7] s->D->t [6] hasAugmentingPath ? No: max-flow = 17 Yes: max-flow > 17 t s 4|4 B C A D 7|7 6|10 4|10 0|2 7|10 6|6 G while (hasAugmentingPath) { augment the the current max flow by the minimum residual capacity on the augmenting path. }

7 Augmenting path Find augmenting path hasAugmentingPath ? t s B C A D
Flow = = 17 ? Find augmenting path s->A->t [4] s->B->C->t [7] s->D->t [6] s->…->t hasAugmentingPath ? Yes: max-flow > 17 But why cannot we find it ? t s 4|4 B C A D 7|7 6|10 4|10 0|2 7|10 6|6 G while (hasAugmentingPath) { augment the the current max flow by the minimum residual capacity on the augmenting path. }

8 Residual graph Residual graph t s B C A D
We look for augmenting path on the residual graph G’ At the same time we also update the residual graph G’ At initial time, G’== G t s 4|4 B C A D 7|7 6|10 4|10 0|2 7|10 6|6 G public FordFulkerson(FlowNetwork G, int s, int t) { V = G.V(); value = excess(G, t); while (hasAugmentingPath(G, s, t)) { double bottle = Double.POSITIVE_INFINITY; for (int v = t; v != s; v = edgeTo[v].other(v)) { bottle = Math.min(bottle, edgeTo[v].residualCapacityTo(v)); } edgeTo[v].addResidualFlowTo(v, bottle); value += bottle;

9 Residual graph Method addResidualFlowTo(v, bottle) t s B C A D t s B C
s->A->t, bottle=4 For s->A, A->t: decrease capacity of forward edge by 4 while increase capacity of backward edge by 4 s->B->C->t, bottle=7 For s->B, B->C, c->t decrease capacity of forward edge by 7 while increase capacity of backward edge by 7 s->D->t, bottle=6 For s->D, D->t, decrease capacity of forward edge by 7 while increase capacity of backward edge by 7 t s 4|4 B C A D 7|7 6|10 4|10 0|2 7|10 6|6 G t s 4 B C A D 6 3 2 7 G’ We show both forward and backward edge here, yet you only store one edge in the implementation.

10 Residual graph Method addResidualFlowTo(v, bottle) t s B C A D t s B C
We found a new augmenting path! Method addResidualFlowTo(v, bottle) s->D->C->B->A->t is an augmenting path, bottle=2 For s->D, D->C, B->A, A->t, decrease capacity of forward edge by 2 while increase capacity of backward edge by 2 For C->B, increase capacity of forward edge by 2 while decrease capacity of backward edge by 2 Forward edge, backward edge On the path of s->D->C->B->A->t, C->B is a backward edge, while all others are forward edges t s 4|4 B C A D 7|7 6|10 4|10 0|2 7|10 6|6 G t s 4 B C A D 6 3 2 7 G’ We show both forward and backward edge here, yet you only store one edge in the implementation.

11 Residual graph Method addResidualFlowTo(v, bottle) t s B C A D A s B C
We found a new augmenting path! MaxFlow=17+2=19 Method addResidualFlowTo(v, bottle) s->D->C->B->A->t is an augmenting path, bottle=2 For s->D, D->C, B->A, A->t, decrease capacity of forward edge by 2 while increase capacity of backward edge by 2 For C->B, increase capacity of forward edge by 2 while decrease capacity of backward edge by 2 Forward edge, backward edge On the path of s->D->C->B->A->t, C->B is a backward edge, while all others are forward edges t s 4 B C A D 6 3 2 7 G’ A 4 6 2 2 4 5 s B C t 7 5 2 2 6 2 8 D G’ We show both forward and backward edge here, yet you only store one edge in the implementation.

12 Residual graph Method addResidualFlowTo(v, bottle) A t s B C A D s B C
We found a new augmenting path! MaxFlow=17+2=19 Method addResidualFlowTo(v, bottle) s->D->C->B->A->t is an augmenting path, bottle=2 Understand backward edge C->B Backward edge C->B tries to `fork` the previous path S->B->C->t, thus pump extra flows into the graph A t s 4|4 B C A D 7|7 8|10 6|10 0|2 2|2 5|10 6|6 G 4 6 2 2 4 5 s B C t 7 5 2 2 6 2 8 D G’ We show both forward and backward edge here, yet you only store one edge in the implementation.

13 Ford-Fulkerson The general idea is simple while (hasAugmentingPath) {
public FordFulkerson(FlowNetwork G, int s, int t) { V = G.V(); value = excess(G, t); while (hasAugmentingPath(G, s, t)) { double bottle = Double.POSITIVE_INFINITY; for (int v = t; v != s; v = edgeTo[v].other(v)) { bottle = Math.min(bottle, edgeTo[v].residualCapacityTo(v)); } edgeTo[v].addResidualFlowTo(v, bottle); value += bottle; The general idea is simple while (hasAugmentingPath) { augment the the current max flow by the minimum residual capacity on the augmenting path. } Two remaining problems How to find augmenting path Adjust the residual graph after upating the max flow

14 Ford-Fulkerson Method hasAugmentingPath(FlowNetwork G, int s, int t) t
For each edge in the residual graph G’, the summation of backward and forward edges capacity equals to the capacity of the edge in G Thus we need to store only one copy, the capacity of the other could be calculated using subtraction E.g., cap(D->S)=10-cap(S->D) t s 4|4 B C A D 7|7 6|10 4|10 0|2 7|10 6|6 G t s 4 B C A D 6 3 2 7 G’ FlowEdge.java public double residualCapacityTo(int vertex) { if (vertex == v) return flow; // backward edge else if (vertex == w) return capacity - flow; // forward edge }

15 Ford-Fulkerson Method hasAugmentingPath(FlowNetwork G, int s, int t) t
FlowNetwork.java public void addEdge(FlowEdge e) { int v = e.from(); int w = e.to(); adj[v].add(e); adj[w].add(e); E++; } Explaination: Vertex B is connected to edge B->C Vertex C is also connected to edge B->C This ensures that vertices are connected by both forward edge and backward edge t s 4|4 B C A D 7|7 6|10 4|10 0|2 7|10 6|6 G t s 4 B C A D 6 3 2 7 G’ FlowEdge e is defined to be from v to w

16 Ford-Fulkerson Method hasAugmentingPath(FlowNetwork G, int s, int t) t
private boolean hasAugmentingPath(FlowNetwork G, int s, int t) { edgeTo = new FlowEdge[G.V()]; marked = new boolean[G.V()]; Queue<Integer> queue = new Queue<Integer>(); queue.enqueue(s); marked[s] = true; while (!queue.isEmpty() && !marked[t]) { int v = queue.dequeue(); for (FlowEdge e : G.adj(v)) { int w = e.other(v); if (e.residualCapacityTo(w) > 0) { if (!marked[w]) { edgeTo[w] = e; marked[w] = true; queue.enqueue(w); } return marked[t]; t s 4 B C A D 6 3 2 7 G’ For example, residualCapacity from B to C is 3, yet from C to B is 7 From s, we could not visit A since there is no residual Capacity

17 Ford-Fulkerson The general idea is simple while (hasAugmentingPath) {
public FordFulkerson(FlowNetwork G, int s, int t) { V = G.V(); value = excess(G, t); while (hasAugmentingPath(G, s, t)) { double bottle = Double.POSITIVE_INFINITY; for (int v = t; v != s; v = edgeTo[v].other(v)) { bottle = Math.min(bottle, edgeTo[v].residualCapacityTo(v)); } edgeTo[v].addResidualFlowTo(v, bottle); value += bottle; The general idea is simple while (hasAugmentingPath) { augment the the current max flow by the minimum residual capacity on the augmenting path. } Two remaining problems How to find augmenting path Adjust the residual graph after upating the max flow

18 Ford-Fulkerson Method addResidualFlowTo(v, bottle) t s B C A D t s B C
s->D->C->B->A->t is an augmenting path, bottle=2 For s->D, D->C, B->A, A->t, decrease capacity of forward edge by 2 while increase capacity of backward edge by 2 For C->B, increase capacity of forward edge by 2 while decrease capacity of backward edge by 2 Forward edge, backward edge On the path of s->D->C->B->A->t, C->B is a backward edge, while all others are forward edges Ford-Fulkerson Method addResidualFlowTo(v, bottle) t s 4 B C A D 8 6 5 2 7 t s 4 B C A D 6 3 2 7 G’ public void addResidualFlowTo(int vertex, double delta) { if (vertex == v) flow -= delta; // backward edge else if (vertex == w) flow += delta; // forward edge } Edge are defined to be from v to w


Download ppt "Ford-Fulkerson."

Similar presentations


Ads by Google