Data Structures and Algorithms Network Flows - dgraf.chdgraf.ch/da16/files/slides12.pdfData...
-
Upload
truongthuy -
Category
Documents
-
view
244 -
download
0
Transcript of Data Structures and Algorithms Network Flows - dgraf.chdgraf.ch/da16/files/slides12.pdfData...
Network Flow: Problem Statement
Input: A flow network consisting ofdirected graph G = (V ,E )source and sink s, t ∈ Vedge capacity c : E → N.
s
0
2
t
1
3
1
1
1
1
2
1
2
1
Output: A flow function f : E → N suchthat:
all capacity constraints are satisfied:∀u, v ∈ V : 0 ≤ f (u, v) ≤ c(u, v)(no pipe is overflowed)flow is conserved at every vertex:∀u ∈ V \ {s, t} :∑
(v ,u)∈E f (v , u) =∑
(u,v)∈E f (u, v)(no vertex is leaking)the total flow is maximal:|f | =
∑v∈V f (s, v)−
∑v∈V f (v , s) =∑
u∈V f (u, t)−∑
u∈V f (t, u)
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Take any s-t-path and increase the flow along it.Update capacities and repeat as long as we can.Problem: We can get stuck at a local optimum.
s t
c
a b
d
2 2
2
24
31
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Take any s-t-path and increase the flow along it.Update capacities and repeat as long as we can.Problem: We can get stuck at a local optimum.
s t
c
a b
d
2 2
2
24
31
1
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Take any s-t-path and increase the flow along it.Update capacities and repeat as long as we can.Problem: We can get stuck at a local optimum.
s t
c
a b
d
2 2
1
24
20
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Take any s-t-path and increase the flow along it.Update capacities and repeat as long as we can.Problem: We can get stuck at a local optimum.
s t
c
a b
d
2 2
1
24
201
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Take any s-t-path and increase the flow along it.Update capacities and repeat as long as we can.Problem: We can get stuck at a local optimum.
s t
c
a b
d
1 1
0
24
20
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Solution: Keep track of the flow and allow paths that reroute units of flow.These are called augmenting paths in the residual network.Ford-Fulkerson: Repeatedly take any augmenting path: running time O(m|f |).Edmonds-Karp: Repeatedly take the shortest augmenting path:running time: best of O(m|f |), O(nmmax c) and O(nm2). [BGL-Doc].
s t
c
a b
d
1/2 1/2
2/2
0/20/4
1/31/1
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Solution: Keep track of the flow and allow paths that reroute units of flow.These are called augmenting paths in the residual network.Ford-Fulkerson: Repeatedly take any augmenting path: running time O(m|f |).Edmonds-Karp: Repeatedly take the shortest augmenting path:running time: best of O(m|f |), O(nmmax c) and O(nm2). [BGL-Doc].
s t
c
a b
d
1
1 1
1
2
0
4
1 1
0
20
20
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Solution: Keep track of the flow and allow paths that reroute units of flow.These are called augmenting paths in the residual network.Ford-Fulkerson: Repeatedly take any augmenting path: running time O(m|f |).Edmonds-Karp: Repeatedly take the shortest augmenting path:running time: best of O(m|f |), O(nmmax c) and O(nm2). [BGL-Doc].
s t
c
a b
d
1
1 1
1
2
0
4
1 1
0
20
20
1
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Solution: Keep track of the flow and allow paths that reroute units of flow.These are called augmenting paths in the residual network.Ford-Fulkerson: Repeatedly take any augmenting path: running time O(m|f |).Edmonds-Karp: Repeatedly take the shortest augmenting path:running time: best of O(m|f |), O(nmmax c) and O(nm2). [BGL-Doc].
s t
c
a b
d
2
1 0
2
2
1
3
0 0
0
11
30
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Solution: Keep track of the flow and allow paths that reroute units of flow.These are called augmenting paths in the residual network.Ford-Fulkerson: Repeatedly take any augmenting path: running time O(m|f |).Edmonds-Karp: Repeatedly take the shortest augmenting path:running time: best of O(m|f |), O(nmmax c) and O(nm2). [BGL-Doc].
s
v
w
t
C C
C
1
C
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Solution: Keep track of the flow and allow paths that reroute units of flow.These are called augmenting paths in the residual network.Ford-Fulkerson: Repeatedly take any augmenting path: running time O(m|f |).Edmonds-Karp: Repeatedly take the shortest augmenting path:running time: best of O(m|f |), O(nmmax c) and O(nm2). [BGL-Doc].
s
v
w
t
C C
C
1
C
1
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Solution: Keep track of the flow and allow paths that reroute units of flow.These are called augmenting paths in the residual network.Ford-Fulkerson: Repeatedly take any augmenting path: running time O(m|f |).Edmonds-Karp: Repeatedly take the shortest augmenting path:running time: best of O(m|f |), O(nmmax c) and O(nm2). [BGL-Doc].
s
v
w
t
C C-1
C
1
C-1
11
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Solution: Keep track of the flow and allow paths that reroute units of flow.These are called augmenting paths in the residual network.Ford-Fulkerson: Repeatedly take any augmenting path: running time O(m|f |).Edmonds-Karp: Repeatedly take the shortest augmenting path:running time: best of O(m|f |), O(nmmax c) and O(nm2). [BGL-Doc].
s
v
w
t
C C-1
C
1
C-1
111
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Solution: Keep track of the flow and allow paths that reroute units of flow.These are called augmenting paths in the residual network.Ford-Fulkerson: Repeatedly take any augmenting path: running time O(m|f |).Edmonds-Karp: Repeatedly take the shortest augmenting path:running time: best of O(m|f |), O(nmmax c) and O(nm2). [BGL-Doc].
s
v
w
t
C C
C
1
C
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Solution: Keep track of the flow and allow paths that reroute units of flow.These are called augmenting paths in the residual network.Ford-Fulkerson: Repeatedly take any augmenting path: running time O(m|f |).Edmonds-Karp: Repeatedly take the shortest augmenting path:running time: best of O(m|f |), O(nmmax c) and O(nm2). [BGL-Doc].
s
v
w
t
C C
C
1
CC
Network Flow Algorithms: Ford-Fulkerson and Edmonds-Karp
Solution: Keep track of the flow and allow paths that reroute units of flow.These are called augmenting paths in the residual network.Ford-Fulkerson: Repeatedly take any augmenting path: running time O(m|f |).Edmonds-Karp: Repeatedly take the shortest augmenting path:running time: best of O(m|f |), O(nmmax c) and O(nm2). [BGL-Doc].
s
v
w
t
C C
C
1
CC
C
Edmonds-Karp Maximum Flow Algorithm
The headers and edge struct:1 // Sample implementation of the Edmonds Karp Algorithm2 // Daniel Graf , [email protected], 7.11.20153 #i n c l u d e <ios t r eam >4 #i n c l u d e <vec to r >5 #i n c l u d e <c a s s e r t >6 #i n c l u d e <queue>7
8 #d e f i n e INF 10000000009 typedef long long int i n ;
10 using namespace s t d ;11
12 struct Edge {13 i n from , to , f low , cap , r e v ;14 i n r e s i d u a l _ c a p a c i t y ( ) {15 return cap−f l ow ;16 }17 } ;18 . . .
Edmonds-Karp Maximum Flow Algorithm
Graph struct, add_edge function:1 . . .2 struct Graph {3 i n s , t ;4 vec to r <vec to r <Edge> > E ; // adjacency -list of edges5 vec to r <Edge∗> P ; // predecessor map for the BFS6
7 Graph ( i n N) {8 E = vec to r <vec to r <Edge> >(N) ;9 }
10
11 void add_edge ( i n from , i n to , i n cap ) {12 if ( from==to ) return ;13 E [ from ] . push_back ({ from , to , 0 , cap , ( i n )E [ to ] . s i z e ( ) } ) ;14 E [ to ] . push_back ({ to , from , 0 , 0 , ( i n )E [ from ] . s i z e () −1}) ;15 }16 . . .
Edmonds-Karp Maximum Flow Algorithm
Reset all flow values:1 . . .2 void r e s e t _ f l o w ( ) {3 for ( i n v=0; v<E . s i z e ( ) ; v++) {4 for ( i n e =0; e<E [ v ] . s i z e ( ) ; e++) {5 E [ v ] [ e ] . f l ow = 0 ;6 }7 }8 }9 . . .
Edmonds-Karp Maximum Flow Algorithm
Can we find a path from s to t?1 bool f i n d _ f l o w ( ) {2 P = vec to r <Edge∗>(E . s i z e ( ) ,NULL ) ;3 // Breadth First Search through the edges with remaining capacity4 queue<in> Q; Q. push ( s ) ;5 while ( !Q. empty ( ) && P[ t ]==NULL) {6 i n v = Q. f r o n t ( ) ; Q. pop ( ) ;7 for ( i n e =0; e<E [ v ] . s i z e ( ) ; e++) {8 if (E [ v ] [ e ] . r e s i d u a l _ c a p a c i t y ()==0) {9 continue ;
10 }11 i n w = E [ v ] [ e ] . to ;12 if (P [w]==NULL) {13 P[w] = &(E [ v ] [ e ] ) ;14 Q. push (w) ;15 }16 }17 } . . .
Edmonds-Karp Maximum Flow Algorithm
How much can we fit through that path from s to t?1 . . .2 // Check if there is a path to t3 if (P [ t ] == NULL) {4 return 0 ;5 }6 // Check the minimum capacity7 i n f l ow = INF ;8 i n pos = t ;9 while ( pos != s ) {
10 f l ow = min ( f low , P [ pos]−> r e s i d u a l _ c a p a c i t y ( ) ) ;11 pos = P[ pos]−>from ;12 }13 return f l ow ;14 }
Edmonds-Karp Maximum Flow Algorithm
Increase the flow along the path and reduce the reverse edges.1 void update_f low ( i n f l ow ) {2 i n pos = t ;3 while ( pos != s ) {4 // cout << "update at vertex " << pos << endl;5 P[ pos]−>f l ow += f l ow ;6 E [P [ pos]−>to ] [ P [ pos]−>r e v ] . f l ow −= f l ow ;7 pos = P[ pos]−>from ;8 }9 }
Edmonds-Karp Maximum Flow Algorithm
Repeatedly search for the shortest augmenting path while one exists.1 i n edmonds_karp_max_flow ( i n _s , i n _t ) {2 s = _s ;3 t = _t ;4 r e s e t _ f l o w ( ) ;5 i n f l ow = 0 ;6 i n new_flow ;7 do {8 new_flow = f i n d _ f l o w ( ) ;9 f l ow += new_flow ;
10 if ( new_flow > 0) {11 update_f low ( new_flow ) ;12 }13 } while ( new_flow > 0 ) ;14 return f l ow ;15 }16 } ; // end of the Graph struct
Edmonds-Karp Maximum Flow Algorithm
Read graph from input and call the algorithm.1 void read_graph_from_std in ( Graph &G) {2 i n N,M;3 c i n >> N >> M;4 G = Graph (N) ;5 for ( i n m=0; m<M; m++) {6 i n a , b , c ;7 c i n >> a >> b >> c ;8 G. add_edge ( a , b , c ) ;9 }
10 }11 int main ( ) {12 Graph G( 0 ) ;13 read_graph_from_std in (G ) ;14 i n s , t ; c i n >> s >> t ;15 i n r e s = G. edmonds_karp_max_flow ( s , t ) ;16 cout << "max␣flow:␣" << r e s << en d l ;17 }
Tutorial Problem: Soccer Prediction
Swiss Soccer Championship, two rounds before the end.2 points awarded per game, split 1-1 if game ends in a tie.Goal difference used for tie breaking in the final standings.
Team Points RemainingGames
FC St. Gallen (FCSG) 37 FCB, FCWBSC Young Boys (YB) 36 FCW, FCBFC Basel (FCB) 35 FCSG, YBFC Luzern (FCL) 33 FCZ, GCZFC Winterthur (FCW) 31 YB, FCSG
Can FC Luzern still win the Championship? 37 points still possible, so yes?Is this a flow problem?
Tutorial Problem: Soccer Prediction
Swiss Soccer Championship, two rounds before the end.2 points awarded per game, split 1-1 if game ends in a tie.Goal difference used for tie breaking in the final standings.
Team Points RemainingGames
FC St. Gallen (FCSG) 37 FCB, FCWBSC Young Boys (YB) 36 FCW, FCBFC Basel (FCB) 35 FCSG, YBFC Luzern (FCL) 33 FCZ, GCZFC Winterthur (FCW) 31 YB, FCSG
Can FC Luzern still win the Championship? 37 points still possible, so yes?Is this a flow problem?
Tutorial Problem: Soccer Prediction
Swiss Soccer Championship, two rounds before the end.2 points awarded per game, split 1-1 if game ends in a tie.Goal difference used for tie breaking in the final standings.
Team Points RemainingGames
FC St. Gallen (FCSG) 37 FCB, FCWBSC Young Boys (YB) 36 FCW, FCBFC Basel (FCB) 35 FCSG, YBFC Luzern (FCL) 33 FCZ, GCZFC Winterthur (FCW) 31 YB, FCSG
Can FC Luzern still win the Championship? 37 points still possible, so yes?Is this a flow problem?
Tutorial Problem: Modelling
Can we let the points flow from the games to the teams so that all the teams end upwith at most 37 points?
FCSG-FCB
YB-FCW
FCW-FCSG
FCB-YB
FCSG
YB
FCB
FCW
ts
2
2
2
2
0
1
2
6
Games Teams
all 2
Tutorial Problem: Analysis
Is this a flow problem? Yes.What does a unit of flow stand for? A point in the soccer ranking.How large is this flow graph?For N teams, M games, we have n = 2 + N + M nodes and m = N + 3M edges.Flow is at most 2M, edge capacity is at most 2M.What algorithm should we use?Edmonds-Karp Max-Flow runs in O(m|f |) = O(n2).
Tutorial Problem: Analysis
Is this a flow problem? Yes.What does a unit of flow stand for? A point in the soccer ranking.How large is this flow graph?For N teams, M games, we have n = 2 + N + M nodes and m = N + 3M edges.Flow is at most 2M, edge capacity is at most 2M.What algorithm should we use?Edmonds-Karp Max-Flow runs in O(m|f |) = O(n2).
Tutorial Problem: Analysis
Is this a flow problem? Yes.What does a unit of flow stand for? A point in the soccer ranking.How large is this flow graph?For N teams, M games, we have n = 2 + N + M nodes and m = N + 3M edges.Flow is at most 2M, edge capacity is at most 2M.What algorithm should we use?Edmonds-Karp Max-Flow runs in O(m|f |) = O(n2).
Tutorial Problem: Analysis
Is this a flow problem? Yes.What does a unit of flow stand for? A point in the soccer ranking.How large is this flow graph?For N teams, M games, we have n = 2 + N + M nodes and m = N + 3M edges.Flow is at most 2M, edge capacity is at most 2M.What algorithm should we use?Edmonds-Karp Max-Flow runs in O(m|f |) = O(n2).
Common tricks
Multiple sources/sinks:
s1
s2
t2
t1
Undirected Graphs
v w4
Vertex Capacities
v
Minimum Flow per Edge
[Exercise]
Common tricks
Multiple sources/sinks:
s1
s2
t2
t1
s t
∑
!∈"#!
∑
!∈"#!
Undirected Graphs
v w4
Vertex Capacities
v
Minimum Flow per Edge
[Exercise]
Common tricks
Multiple sources/sinks:
s1
s2
t2
t1
s t
∑
!∈"#!
∑
!∈"#!
Undirected Graphs
v w4
v w
4
4
Vertex Capacities
v
Minimum Flow per Edge
[Exercise]
Common tricks
Multiple sources/sinks:
s1
s2
t2
t1
s t
∑
!∈"#!
∑
!∈"#!
Undirected Graphs
v w4
v w
4
4
v w
3/4
2/4
Vertex Capacities
v
Minimum Flow per Edge
[Exercise]
Common tricks
Multiple sources/sinks:
s1
s2
t2
t1
s t
∑
!∈"#!
∑
!∈"#!
Undirected Graphs
v w4
v w
4
4
v w
3/4
2/4
v w
1/4
0/4
Vertex Capacities
v
Minimum Flow per Edge
[Exercise]
Common tricks
Multiple sources/sinks:
s1
s2
t2
t1
s t
∑
!∈"#!
∑
!∈"#!
Undirected Graphs
v w4
v w
4
4
v w
3/4
2/4
v w
1/4
0/4
Vertex Capacities
v v v1
Minimum Flow per Edge
[Exercise]
Common tricks
Multiple sources/sinks:
s1
s2
t2
t1
s t
∑
!∈"#!
∑
!∈"#!
Undirected Graphs
v w4
v w
4
4
v w
3/4
2/4
v w
1/4
0/4
Vertex Capacities
v v v1
Minimum Flow per Edge
[Exercise]
Flow Application: Edge Disjoint Paths
How many ways are there to get from HB to CAB without using the same street twice?
t1
2
6
s t
Map:search.ch,TomTom,swisstopo,OSM
Flow Application: Edge Disjoint Paths
How many ways are there to get from HB to CAB without using the same street twice?
t1
2
6
s t
Map:search.ch,TomTom,swisstopo,OSM
Flow Application: Edge Disjoint Paths
How many ways are there to get from HB to CAB without using the same street twice?
t1
2
6
s t
Map:search.ch,TomTom,swisstopo,OSM
Flow Application: Edge Disjoint Paths
How many ways are there to get from HB to CAB without using the same street twice?
t1
2
6
s t
Map:search.ch,TomTom,swisstopo,OSM
Flow Application: Edge Disjoint Paths
How many ways are there to get from HB to CAB without using the same streettwice?
Is this a flow problem? No.Can it be turned into a flow problem? Maybe.Build directed street graph by adding edges in both directions.Set all capacities to 1.
LemmaIn a directed graph with unit capacities, the maximum number of edge-disjoints-t-paths is equal to the maximum flow from s to t.
Flow Application: Edge Disjoint Paths
How many ways are there to get from HB to CAB without using the same streettwice?
Is this a flow problem? No.Can it be turned into a flow problem? Maybe.Build directed street graph by adding edges in both directions.Set all capacities to 1.
LemmaIn a directed graph with unit capacities, the maximum number of edge-disjoints-t-paths is equal to the maximum flow from s to t.
Flow Application: Edge Disjoint Paths
How many ways are there to get from HB to CAB without using the same streettwice?
Is this a flow problem? No.Can it be turned into a flow problem? Maybe.Build directed street graph by adding edges in both directions.Set all capacities to 1.
LemmaIn a directed graph with unit capacities, the maximum number of edge-disjoints-t-paths is equal to the maximum flow from s to t.
Flow Application: Edge Disjoint Paths
How many ways are there to get from HB to CAB without using the same streettwice?
Is this a flow problem? No.Can it be turned into a flow problem? Maybe.Build directed street graph by adding edges in both directions.Set all capacities to 1.
LemmaIn a directed graph with unit capacities, the maximum number of edge-disjoints-t-paths is equal to the maximum flow from s to t.