• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
[ACM-ICPC] Dinic's Algorithm
 

[ACM-ICPC] Dinic's Algorithm

on

  • 2,055 views

 

Statistics

Views

Total Views
2,055
Views on SlideShare
2,049
Embed Views
6

Actions

Likes
1
Downloads
75
Comments
0

1 Embed 6

http://0.0.0.0 6

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    [ACM-ICPC] Dinic's Algorithm [ACM-ICPC] Dinic's Algorithm Presentation Transcript

    • Dinic’s Algorithm 郭至軒(KuoE0) KuoE0.tw@gmail.com KuoE0.ch
    • Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/ Latest update: Mar 27, 2013
    • Maximum Flow 3 1 4 3 2s 2 4 t 3 3 2 3 2
    • Maximum Flow 3/3 1 4 3/3 2/2s 0/2 1/4 t 2/3 3/3 2 3 2/2
    • ConceptLevel GraphBlocking Flow
    • Level Graph 0/4 1 3 0/10 0/10S 0/8 t 0/10 0/10 0/9 2 4
    • Level Graph 0/4 1 3 0/10 0/10S1 0/8 t 0/10 0/10 0/9 2 4
    • Level Graph 0/4 2 1 3 0/10 0/10S1 0/8 t 0/10 0/10 0/9 2 4
    • Level Graph 0/4 2 1 3 0/10 0/10S1 0/8 t 0/10 0/10 0/9 2 3 4
    • Level Graph 0/4 2 1 3 0/10 0/10S1 0/8 t 4 0/10 0/10 0/9 2 3 4
    • Level Graph 0/4 2 1 3 0/10 0/10S1s 0/8 t 4 0/10 0/10 0/9 2 3 4
    • Blocking Flow 0/4 0/5 0/20 0/5 0/10... 0/3 0/8
    • Blocking Flow 0/4 0/5 0/20 0/5 0/10... 0/3 0/8
    • Blocking Flow 0/4 0/5 0/20 0/5 0/10... 0/3 0/8 bottleneck: 4
    • Blocking Flow 4/4 4/5 4/20 0/5 0/10... 0/3 0/8
    • Blocking Flow 4/4 4/5 4/20 0/5 0/10... 0/3 0/8
    • Blocking Flow 4/4 4/5 4/20 0/5 0/10... 0/3 0/8 bottleneck: 5
    • Blocking Flow 4/4 4/5 9/20 5/5 5/10... 0/3 0/8
    • Blocking Flow 4/4 4/5 9/20 5/5 5/10... 0/3 0/8
    • Blocking Flow 4/4 4/5 9/20 5/5 5/10... 0/3 0/8 bottleneck: 3
    • Blocking Flow 4/4 4/5 12/20 5/5 5/10... 3/3 3/8
    • Blocking Flow 0/15 0/10 2 3 4 0/30 0/5 0/10 0/15 0/5 0/71 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3
    • Blocking Flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/71 3 4 5 8/10 3/5 5/5 10/10 2 4 5/5 5/6 3
    • Algorithm1. build a level graph2. find an augmenting path from source to sink3. find the bottleneck on augmenting path4. find the augmenting path from bottleneck to sink5. repeat step 3 and step 4 to construct a blocking flow until no augmenting path found
    • Example 0/15 0/100/30 0/5 0/10 0/15 0/5 0/7 0/10 0/5 0/5 0/10 0/5 0/6
    • build a level graph 0/15 0/100/30 0/5 0/10 0/15 0/5 0/7 0/10 0/5 0/5 0/10 0/5 0/6 current flow: 0
    • build a level graph 0/15 0/10 0/30 0/5 0/10 0/15 0/5 0/71 0/10 0/5 0/5 0/10 0/5 0/6 current flow: 0
    • build a level graph 0/15 0/10 2 0/30 0/5 0/10 0/15 0/5 0/71 0/10 0/5 0/5 0/10 2 0/5 0/6 current flow: 0
    • build a level graph 0/15 0/10 2 3 0/30 0/5 0/10 0/15 0/5 0/71 3 0/10 0/5 0/5 0/10 2 0/5 0/6 3 current flow: 0
    • build a level graph 0/15 0/10 2 3 4 0/30 0/5 0/10 0/15 0/5 0/71 3 4 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 0
    • build a level graph 0/15 0/10 2 3 4 0/30 0/5 0/10 0/15 0/5 0/71 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 0
    • construct a blocking flow 0/15 0/10 2 3 4 0/30 0/5 0/10 0/15 0/5 0/711 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 0
    • construct a blocking flow 0/15 0/10 2 3 4 0/30 0/5 0/10 0/15 0/5 0/711 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 0
    • construct a blocking flow bottleneck: 10 0/15 0/10 2 3 4 0/30 0/5 0/10 0/15 0/5 0/711 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 0
    • construct a blocking flow 10/15 10/10 2 3 4 10/30 0/5 0/10 10/15 0/5 0/711 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 10 0
    • construct a blocking flow 10/15 10/10 10/30 2 3 3 4 10/15 0/5 0/10 0/5 0/71 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 10
    • construct a blocking flow 10/15 10/10 10/30 2 3 3 4 10/15 0/5 0/10 0/5 0/71 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 10
    • construct a blocking flow bottleneck: 5 10/15 10/10 10/30 2 3 3 4 10/15 0/5 0/10 0/5 0/71 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 10
    • construct a blocking flow 15/15 10/10 15/30 2 3 3 4 10/15 0/5 5/10 0/5 5/71 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 15 10
    • construct a blocking flow 15/15 10/10 15/30 2 2 3 4 10/15 0/5 5/10 0/5 5/71 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 15 10
    • construct a blocking flow 15/15 10/10 15/30 2 2 3 4 10/15 0/5 5/10 0/5 5/71 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 15 10
    • construct a blocking flow bottleneck: 2 15/15 10/10 15/30 2 2 3 4 10/15 0/5 5/10 0/5 5/71 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 15 10
    • construct a blocking flow 15/15 10/10 17/30 2 2 3 4 10/15 2/5 5/10 2/5 7/71 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 17 15 10
    • construct a blocking flow 15/15 10/10 2 3 4 17/30 2/5 5/10 10/15 2/5 7/71 3 4 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 17
    • construct a blocking flow 15/15 10/10 2 3 4 17/30 2/5 5/10 10/15 2/5 7/71 3 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 17
    • construct a blocking flow 15/15 10/10 2 3 4 17/30 2/5 5/10 10/15 2/5 7/71 3 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 17
    • construct a blocking flow bottleneck: 3 15/15 10/10 2 3 4 17/30 2/5 5/10 10/15 2/5 7/71 3 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 17
    • construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/71 3 3 4 5 0/10 0/5 3/5 3/10 2 4 0/5 0/6 3 current flow: 20 17
    • construct a blocking flow 15/15 10/10 20/30 2 2 3 4 10/15 5/5 5/10 2/5 7/71 3 3 4 5 0/10 0/5 3/5 3/10 2 4 0/5 0/6 3 current flow: 20
    • construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/711 3 3 4 5 0/10 0/5 3/5 3/10 2 4 0/5 0/6 3 current flow: 20
    • construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/711 3 3 4 5 0/10 0/5 3/5 3/10 2 4 0/5 0/6 3 current flow: 20
    • construct a blocking flow bottleneck: 2 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/711 3 3 4 5 0/10 0/5 3/5 3/10 2 4 0/5 0/6 3 current flow: 20
    • construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/711 3 3 4 5 2/10 2/5 5/5 5/10 2 4 0/5 0/6 3 current flow: 22 20
    • construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/71 3 3 4 5 2/10 2/5 5/5 5/10 2 4 0/5 0/6 3 current flow: 22
    • construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/71 3 3 4 5 2/10 2/5 5/5 5/10 2 2 4 0/5 0/6 3 current flow: 22
    • construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/71 3 3 4 5 2/10 2/5 5/5 5/10 2 2 4 0/5 0/6 3 current flow: 22
    • construct a blocking flow bottleneck: 5 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/71 3 3 4 5 2/10 2/5 5/5 5/10 2 2 4 0/5 0/6 3 current flow: 22
    • construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/71 3 3 4 5 7/10 2/5 5/5 10/10 2 2 4 5/5 5/6 3 current flow: 27 22
    • no augmenting path from source to sink 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/7 1 3 3 4 5 7/10 2/5 5/5 10/10 2 2 4 5/5 5/6 3 current flow: 27 22
    • build a level graph 15/15 10/1020/30 5/5 5/10 10/15 2/5 7/7 7/10 2/5 5/5 10/10 4 5/5 5/6 current flow: 27 22
    • build a level graph 15/15 10/10 20/30 5/5 5/10 10/15 2/5 7/71 7/10 2/5 5/5 10/10 4 5/5 5/6 current flow: 27 22
    • build a level graph 15/15 10/10 2 20/30 5/5 5/10 10/15 2/5 7/71 7/10 2/5 5/5 10/10 2 4 5/5 5/6 current flow: 27 22
    • build a level graph 15/15 10/10 2 20/30 5/5 5/10 10/15 2/5 7/71 3 7/10 2/5 5/5 10/10 2 4 5/5 5/6 current flow: 27 22
    • build a level graph 15/15 10/10 2 20/30 5/5 5/10 10/15 2/5 7/71 3 4 7/10 2/5 5/5 10/10 2 4 5/5 5/6 current flow: 27 22
    • build a level graph 15/15 10/10 2 5 20/30 5/5 5/10 10/15 2/5 7/71 3 4 7/10 2/5 5/5 10/10 2 4 5/5 5/6 current flow: 27 22
    • no any path from source to sink exists 15/15 10/10 2 5 20/30 5/5 5/10 10/15 2/5 7/7 1 3 4 7/10 2/5 5/5 10/10 2 4 5/5 5/6 current flow: 27 22
    • Maximum Flow: 27 15/15 10/1020/30 5/5 5/10 10/15 2/5 7/7 7/10 2/5 5/5 10/10 4 5/5 5/6
    • Source Code// vertex[ a ] is the adjacent list of a.// cap[ a ][ b ] is the capacity from a to b.// flow[ a ][ b ] is the occupied flow from a tob.// level[ a ] is the level in level graph of a.// path[ a ] is the previous node of a.int Dinic( int source, int sink ) { int ret = 0; while ( buildLevelGraph( source, sink ) ) ret += constructBlockingFlow( source, sink ); return ret;}
    • Source Codebool buildLevelGraph( int source, int sink ) { queue< int > que; memset( level, 0, sizeof( level ) ); que.push( source ); level[ source ] = 1; while ( que.empty() != true ) { int now = que.front(); que.pop(); for ( int i = 0; i < vertex[ now ].size(); ++i ) { int next = vertex[ now ][ i ]; if ( ( cap[ now ][ next ] - flow[ now ][ next ] > 0 || flow[ next ][ now ] > 0 ) && level[ next ] == 0 ) { que.push( next ); level[ next ] = level[ now ] + 1; } } } return level[ sink ] != 0;}
    • Source Codeint constructBlockingFlow( int source, int sink ) { int ret = 0; stack< int > stk; memset( visit, 0, sizeof( visit ) ); stk.push( source ); while ( stk.empty() != true ) { int now = stk.top(); if ( now != sink ) { for ( int i = 0; i < vertex[ now ].size() && stk.top() != now; ++i ) { int next = vertex[ now ][ i ]; if ( visit[ next ] || level[ next ] != level [ now ] + 1 ) continue; if ( cap[ now ][ next ] - flow[ now ][ next ] > 0 ) stk.push( next ), path[ next ] = now; else if ( flow[ now ][ next ] > 0 ) stk.push( next ), path[ next ] = -now; }
    • Source Code if ( stk.top() == now ) stk.pop(), visit[ now ] = 1;}else { int F = 1e9, bottleneck; for ( int cur = sink; cur != source; cur = abs( path[ cur ] ) ) F = min( F, path[ cur ] > 0 ? cap[ path[ cur ] ] [ cur ] - flow[ path[ cur ] ][ cur ] : flow[ cur ][ -path[ cur ] ] ); for ( int cur = sink; cur != source; cur = abs( path[ cur ] ) ) { if ( path[ cur ] > 0 ) { flow[ path[ cur ] ][ cur ] += F; if ( cap[ path[ cur ] ][ cur ] - flow[ path[ cur ] ][ cur ] == 0 ) bottleneck = path[ cur ]; }
    • Source Code else { flow[ cur ][ -path[ cur ] ] -= F; if ( flow[ cur ][ -path[ cur ] ] == 0 ) bottleneck = -path[ cur ]; } } while ( stk.empty() != true && stk.top() != bottleneck ) stk.pop(); ret += F; } } return ret;}
    • Practice Now[POJ] 1459 - Power Network
    • Reference• 演算法筆記- Flow• Maximum flow problem - Wikipedia, the free encyclopedia
    • Thank You for Your Listening.