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/             L...
Maximum Flow            3        1       4    3               2s       2       4       t    3               3        2    ...
Maximum Flow                3/3            1           4    3/3                     2/2s         0/2         1/4         t...
ConceptLevel GraphBlocking Flow
Level Graph                      0/4            1               3    0/10                                0/10S            ...
Level Graph                      0/4            1               3    0/10                                0/10S1           ...
Level Graph                      0/4            2            1               3    0/10                                0/10...
Level Graph                      0/4            2            1               3    0/10                                0/10...
Level Graph                      0/4            2            1               3    0/10                                0/10...
Level Graph                      0/4            2            1               3    0/10                                0/10...
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       ...
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       ...
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       ...
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           ...
Blocking Flow                   15/15           10/10               2               3            4    20/30               ...
Algorithm1. build a level graph2. find an augmenting path from source to sink3. find the bottleneck on augmenting path4. find...
Example               0/15       0/100/30                0/5     0/10    0/15                          0/5      0/7       ...
build a level graph                  0/15       0/100/30                   0/5     0/10        0/15                       ...
build a level graph                      0/15       0/10    0/30                   0/5     0/10        0/15               ...
build a level graph                       0/15       0/10                  2    0/30                    0/5     0/10      ...
build a level graph                       0/15           0/10                  2               3    0/30                  ...
build a level graph                       0/15           0/10                  2               3            4    0/30     ...
build a level graph                       0/15           0/10                  2               3            4    0/30     ...
construct a blocking flow                       0/15           0/10                  2               3            4    0/30...
construct a blocking flow                       0/15           0/10                  2               3            4    0/30...
construct a blocking flow                                  bottleneck: 10                       0/15            0/10       ...
construct a blocking flow                   10/15           10/10               2               3            4    10/30    ...
construct a blocking flow                   10/15           10/10    10/30               2               3                 ...
construct a blocking flow                   10/15           10/10    10/30               2               3                 ...
construct a blocking flow            bottleneck: 5                   10/15           10/10    10/30               2        ...
construct a blocking flow                   15/15           10/10    15/30               2               3                 ...
construct a blocking flow                   15/15           10/10    15/30               2               2               3 ...
construct a blocking flow                   15/15           10/10    15/30               2               2               3 ...
construct a blocking flow                                           bottleneck: 2                   15/15           10/10  ...
construct a blocking flow                   15/15           10/10    17/30               2               2               3 ...
construct a blocking flow                   15/15           10/10               2               3            4    17/30    ...
construct a blocking flow                   15/15           10/10               2               3            4    17/30    ...
construct a blocking flow                   15/15           10/10               2               3            4    17/30    ...
construct a blocking flow               bottleneck: 3                   15/15           10/10               2              ...
construct a blocking flow                   15/15           10/10               2               3            4    20/30    ...
construct a blocking flow                   15/15           10/10    20/30               2               2               3 ...
construct a blocking flow                   15/15           10/10               2               3            4    20/30    ...
construct a blocking flow                   15/15           10/10               2               3            4    20/30    ...
construct a blocking flow                               bottleneck: 2                   15/15           10/10              ...
construct a blocking flow                   15/15           10/10               2               3            4    20/30    ...
construct a blocking flow                   15/15           10/10               2               3            4    20/30    ...
construct a blocking flow                   15/15           10/10               2               3            4    20/30    ...
construct a blocking flow                   15/15           10/10               2               3            4    20/30    ...
construct a blocking flow                                            bottleneck: 5                   15/15           10/10 ...
construct a blocking flow                   15/15           10/10               2               3            4    20/30    ...
no augmenting path from source to sink                        15/15           10/10                    2               3  ...
build a level graph              15/15       10/1020/30               5/5     5/10        10/15                          2...
build a level graph                  15/15       10/10    20/30               5/5     5/10        10/15                   ...
build a level graph                   15/15       10/10               2    20/30                5/5     5/10        10/15 ...
build a level graph                   15/15           10/10               2    20/30                5/5         5/10      ...
build a level graph                   15/15           10/10               2    20/30                5/5         5/10      ...
build a level graph                   15/15           10/10               2               5    20/30                5/5   ...
no any path from source to sink exists                       15/15           10/10                   2               5    ...
Maximum Flow: 27           15/15       10/1020/30            5/5     5/10        10/15                       2/5          ...
Source Code//   vertex[ a ] is the adjacent list of a.//   cap[ a ][ b ] is the capacity from a to b.//   flow[ a ][ b ] i...
Source Codebool buildLevelGraph( int source, int sink ) {   queue< int > que;   memset( level, 0, sizeof( level ) );   que...
Source Codeint constructBlockingFlow( int source, int sink ) {   int ret = 0;   stack< int > stk;   memset( visit, 0, size...
Source Code  if ( stk.top() == now )    stk.pop(), visit[ now ] = 1;}else {  int F = 1e9, bottleneck;  for ( int cur = sin...
Source Code            else {              flow[ cur ][ -path[ cur ] ] -= F;              if ( flow[ cur ][ -path[ cur ] ]...
Practice Now[POJ] 1459 - Power Network
Reference• 演算法筆記- Flow• Maximum flow problem - Wikipedia, the  free encyclopedia
Thank You for Your Listening.
Upcoming SlideShare
Loading in …5
×

[ACM-ICPC] Dinic's Algorithm

9,479 views

Published on

[ACM-ICPC] Dinic's Algorithm

  1. 1. Dinic’s Algorithm 郭至軒(KuoE0) KuoE0.tw@gmail.com KuoE0.ch
  2. 2. Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/ Latest update: Mar 27, 2013
  3. 3. Maximum Flow 3 1 4 3 2s 2 4 t 3 3 2 3 2
  4. 4. Maximum Flow 3/3 1 4 3/3 2/2s 0/2 1/4 t 2/3 3/3 2 3 2/2
  5. 5. ConceptLevel GraphBlocking Flow
  6. 6. Level Graph 0/4 1 3 0/10 0/10S 0/8 t 0/10 0/10 0/9 2 4
  7. 7. Level Graph 0/4 1 3 0/10 0/10S1 0/8 t 0/10 0/10 0/9 2 4
  8. 8. Level Graph 0/4 2 1 3 0/10 0/10S1 0/8 t 0/10 0/10 0/9 2 4
  9. 9. Level Graph 0/4 2 1 3 0/10 0/10S1 0/8 t 0/10 0/10 0/9 2 3 4
  10. 10. Level Graph 0/4 2 1 3 0/10 0/10S1 0/8 t 4 0/10 0/10 0/9 2 3 4
  11. 11. Level Graph 0/4 2 1 3 0/10 0/10S1s 0/8 t 4 0/10 0/10 0/9 2 3 4
  12. 12. Blocking Flow 0/4 0/5 0/20 0/5 0/10... 0/3 0/8
  13. 13. Blocking Flow 0/4 0/5 0/20 0/5 0/10... 0/3 0/8
  14. 14. Blocking Flow 0/4 0/5 0/20 0/5 0/10... 0/3 0/8 bottleneck: 4
  15. 15. Blocking Flow 4/4 4/5 4/20 0/5 0/10... 0/3 0/8
  16. 16. Blocking Flow 4/4 4/5 4/20 0/5 0/10... 0/3 0/8
  17. 17. Blocking Flow 4/4 4/5 4/20 0/5 0/10... 0/3 0/8 bottleneck: 5
  18. 18. Blocking Flow 4/4 4/5 9/20 5/5 5/10... 0/3 0/8
  19. 19. Blocking Flow 4/4 4/5 9/20 5/5 5/10... 0/3 0/8
  20. 20. Blocking Flow 4/4 4/5 9/20 5/5 5/10... 0/3 0/8 bottleneck: 3
  21. 21. Blocking Flow 4/4 4/5 12/20 5/5 5/10... 3/3 3/8
  22. 22. 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
  23. 23. 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
  24. 24. 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
  25. 25. 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
  26. 26. 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
  27. 27. 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
  28. 28. 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
  29. 29. 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
  30. 30. 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
  31. 31. 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
  32. 32. 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
  33. 33. 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
  34. 34. 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
  35. 35. 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
  36. 36. 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
  37. 37. 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
  38. 38. 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
  39. 39. 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
  40. 40. 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
  41. 41. 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
  42. 42. 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
  43. 43. 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
  44. 44. 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
  45. 45. 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
  46. 46. 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
  47. 47. 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
  48. 48. 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
  49. 49. 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
  50. 50. 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
  51. 51. 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
  52. 52. 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
  53. 53. 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
  54. 54. 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
  55. 55. 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
  56. 56. 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
  57. 57. 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
  58. 58. 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
  59. 59. 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
  60. 60. 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
  61. 61. 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
  62. 62. 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
  63. 63. 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
  64. 64. 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
  65. 65. 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
  66. 66. 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
  67. 67. 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
  68. 68. 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;}
  69. 69. 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;}
  70. 70. 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; }
  71. 71. 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 ]; }
  72. 72. 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;}
  73. 73. Practice Now[POJ] 1459 - Power Network
  74. 74. Reference• 演算法筆記- Flow• Maximum flow problem - Wikipedia, the free encyclopedia
  75. 75. Thank You for Your Listening.

×