SlideShare una empresa de Scribd logo
1 de 75
Descargar para leer sin conexión
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               2

s       2       4       t
    3               3
        2       3
            2
Maximum Flow
                3/3
            1           4
    3/3                     2/2

s         0/2         1/4         t
    2/3                     3/3
            2           3
                2/2
Concept

Level Graph


Blocking Flow
Level Graph
                      0/4
            1               3
    0/10
                                0/10

S               0/8                           t
    0/10                               0/10
                      0/9
            2               4
Level Graph
                      0/4
            1               3
    0/10
                                0/10

S
1               0/8                           t
    0/10                               0/10
                      0/9
            2               4
Level Graph
                      0/4
            2
            1               3
    0/10
                                0/10

S
1               0/8                           t
    0/10                               0/10
                      0/9
            2               4
Level Graph
                      0/4
            2
            1               3
    0/10
                                0/10

S
1               0/8                           t
    0/10                               0/10
                      0/9
            2               3
                            4
Level Graph
                      0/4
            2
            1               3
    0/10
                                0/10

S
1               0/8                           t
                                              4
    0/10                               0/10
                      0/9
            2               3
                            4
Level Graph
                      0/4
            2
            1               3
    0/10
                                0/10

S
1
s               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/7
1                                 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/7
1                              3            4            5
        8/10       3/5               5/5
                                                10/10
               2                            4
                     5/5           5/6


                               3
Algorithm
1. build a level graph
2. find an augmenting path from source to sink
3. find the bottleneck on augmenting path
4. find the augmenting path from bottleneck to
   sink
5. repeat step 3 and step 4 to construct a
   blocking flow until no augmenting path found
Example
               0/15       0/10

0/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/10

0/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/7
1
           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/7
1
           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/7
1                                 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/7
1                                 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/7
1                                 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/7
1
1                                 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/7
1
1                                 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/7
1
1                                  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/7
1
1                              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/7
1                              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/7
1                              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/7
1                              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/7
1                              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/7
1                              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/7
1                              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/7
1                              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/7
1                              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/7
1                              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/7
1                              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/7
1                              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/7
1                              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/7
1                              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/7
1                              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/7
1
1                              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/7
1
1                              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/7
1
1                              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/7
1
1                              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/7
1                              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/7
1                              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/7
1                              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/7
1                              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/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
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/10

20/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/7
1
        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/7
1
        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/7
1                              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/7
1                              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/7
1                              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/10

20/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 to
b.
//   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 Code
bool 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 Code
int 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.

Más contenido relacionado

La actualidad más candente

指数時間アルゴリズムの最先端
指数時間アルゴリズムの最先端指数時間アルゴリズムの最先端
指数時間アルゴリズムの最先端
Yoichi Iwata
 

La actualidad más candente (20)

AtCoder Beginner Contest 015 解説
AtCoder Beginner Contest 015 解説AtCoder Beginner Contest 015 解説
AtCoder Beginner Contest 015 解説
 
AtCoder Regular Contest 037 解説
AtCoder Regular Contest 037 解説AtCoder Regular Contest 037 解説
AtCoder Regular Contest 037 解説
 
文字列アルゴリズム
文字列アルゴリズム文字列アルゴリズム
文字列アルゴリズム
 
プログラミングコンテストでのデータ構造 2 ~動的木編~
プログラミングコンテストでのデータ構造 2 ~動的木編~プログラミングコンテストでのデータ構造 2 ~動的木編~
プログラミングコンテストでのデータ構造 2 ~動的木編~
 
abc032
abc032abc032
abc032
 
AtCoder Regular Contest 048
AtCoder Regular Contest 048AtCoder Regular Contest 048
AtCoder Regular Contest 048
 
abc027
abc027abc027
abc027
 
3次元の凸包を求める
3次元の凸包を求める3次元の凸包を求める
3次元の凸包を求める
 
ウェーブレット木の世界
ウェーブレット木の世界ウェーブレット木の世界
ウェーブレット木の世界
 
ユークリッド最小全域木
ユークリッド最小全域木ユークリッド最小全域木
ユークリッド最小全域木
 
Union find(素集合データ構造)
Union find(素集合データ構造)Union find(素集合データ構造)
Union find(素集合データ構造)
 
最大流 (max flow)
最大流 (max flow)最大流 (max flow)
最大流 (max flow)
 
AtCoder Beginner Contest 017 解説
AtCoder Beginner Contest 017 解説AtCoder Beginner Contest 017 解説
AtCoder Beginner Contest 017 解説
 
指数時間アルゴリズムの最先端
指数時間アルゴリズムの最先端指数時間アルゴリズムの最先端
指数時間アルゴリズムの最先端
 
双対性
双対性双対性
双対性
 
プログラミングコンテストでのデータ構造 2 ~平衡二分探索木編~
プログラミングコンテストでのデータ構造 2 ~平衡二分探索木編~プログラミングコンテストでのデータ構造 2 ~平衡二分探索木編~
プログラミングコンテストでのデータ構造 2 ~平衡二分探索木編~
 
AtCoder Regular Contest 040 解説
AtCoder Regular Contest 040 解説AtCoder Regular Contest 040 解説
AtCoder Regular Contest 040 解説
 
AtCoder Regular Contest 036 解説
AtCoder Regular Contest 036 解説AtCoder Regular Contest 036 解説
AtCoder Regular Contest 036 解説
 
Bloom filter
Bloom filterBloom filter
Bloom filter
 
AtCoder Beginner Contest 023 解説
AtCoder Beginner Contest 023 解説AtCoder Beginner Contest 023 解説
AtCoder Beginner Contest 023 解説
 

Más de Chih-Hsuan Kuo

[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
Chih-Hsuan Kuo
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set
Chih-Hsuan Kuo
 

Más de Chih-Hsuan Kuo (20)

Rust
RustRust
Rust
 
[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-select
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in Rust
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OS
 
Necko walkthrough
Necko walkthroughNecko walkthrough
Necko walkthrough
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in Gecko
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!
 
應徵軟體工程師
應徵軟體工程師應徵軟體工程師
應徵軟體工程師
 
面試心得分享
面試心得分享面試心得分享
面試心得分享
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...
 
Python @Wheel Lab
Python @Wheel LabPython @Wheel Lab
Python @Wheel Lab
 
Introduction to VP8
Introduction to VP8Introduction to VP8
Introduction to VP8
 
Python @NCKU CSIE
Python @NCKU CSIEPython @NCKU CSIE
Python @NCKU CSIE
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set
 
[ACM-ICPC] Traversal
[ACM-ICPC] Traversal[ACM-ICPC] Traversal
[ACM-ICPC] Traversal
 
[ACM-ICPC] UVa-10245
[ACM-ICPC] UVa-10245[ACM-ICPC] UVa-10245
[ACM-ICPC] UVa-10245
 

[ACM-ICPC] Dinic's Algorithm

  • 1. Dinic’s Algorithm 郭至軒(KuoE0) KuoE0.tw@gmail.com KuoE0.ch
  • 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. Maximum Flow 3 1 4 3 2 s 2 4 t 3 3 2 3 2
  • 4. Maximum Flow 3/3 1 4 3/3 2/2 s 0/2 1/4 t 2/3 3/3 2 3 2/2
  • 6. Level Graph 0/4 1 3 0/10 0/10 S 0/8 t 0/10 0/10 0/9 2 4
  • 7. Level Graph 0/4 1 3 0/10 0/10 S 1 0/8 t 0/10 0/10 0/9 2 4
  • 8. Level Graph 0/4 2 1 3 0/10 0/10 S 1 0/8 t 0/10 0/10 0/9 2 4
  • 9. Level Graph 0/4 2 1 3 0/10 0/10 S 1 0/8 t 0/10 0/10 0/9 2 3 4
  • 10. Level Graph 0/4 2 1 3 0/10 0/10 S 1 0/8 t 4 0/10 0/10 0/9 2 3 4
  • 11. Level Graph 0/4 2 1 3 0/10 0/10 S 1 s 0/8 t 4 0/10 0/10 0/9 2 3 4
  • 12. Blocking Flow 0/4 0/5 0/20 0/5 0/10 ... 0/3 0/8
  • 13. Blocking Flow 0/4 0/5 0/20 0/5 0/10 ... 0/3 0/8
  • 14. Blocking Flow 0/4 0/5 0/20 0/5 0/10 ... 0/3 0/8 bottleneck: 4
  • 15. Blocking Flow 4/4 4/5 4/20 0/5 0/10 ... 0/3 0/8
  • 16. Blocking Flow 4/4 4/5 4/20 0/5 0/10 ... 0/3 0/8
  • 17. Blocking Flow 4/4 4/5 4/20 0/5 0/10 ... 0/3 0/8 bottleneck: 5
  • 18. Blocking Flow 4/4 4/5 9/20 5/5 5/10 ... 0/3 0/8
  • 19. Blocking Flow 4/4 4/5 9/20 5/5 5/10 ... 0/3 0/8
  • 20. Blocking Flow 4/4 4/5 9/20 5/5 5/10 ... 0/3 0/8 bottleneck: 3
  • 21. Blocking Flow 4/4 4/5 12/20 5/5 5/10 ... 3/3 3/8
  • 22. Blocking Flow 0/15 0/10 2 3 4 0/30 0/5 0/10 0/15 0/5 0/7 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3
  • 23. Blocking Flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/7 1 3 4 5 8/10 3/5 5/5 10/10 2 4 5/5 5/6 3
  • 24. Algorithm 1. build a level graph 2. find an augmenting path from source to sink 3. find the bottleneck on augmenting path 4. find the augmenting path from bottleneck to sink 5. repeat step 3 and step 4 to construct a blocking flow until no augmenting path found
  • 25. Example 0/15 0/10 0/30 0/5 0/10 0/15 0/5 0/7 0/10 0/5 0/5 0/10 0/5 0/6
  • 26. build a level graph 0/15 0/10 0/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. build a level graph 0/15 0/10 0/30 0/5 0/10 0/15 0/5 0/7 1 0/10 0/5 0/5 0/10 0/5 0/6 current flow: 0
  • 28. build a level graph 0/15 0/10 2 0/30 0/5 0/10 0/15 0/5 0/7 1 0/10 0/5 0/5 0/10 2 0/5 0/6 current flow: 0
  • 29. build a level graph 0/15 0/10 2 3 0/30 0/5 0/10 0/15 0/5 0/7 1 3 0/10 0/5 0/5 0/10 2 0/5 0/6 3 current flow: 0
  • 30. build a level graph 0/15 0/10 2 3 4 0/30 0/5 0/10 0/15 0/5 0/7 1 3 4 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 0
  • 31. build a level graph 0/15 0/10 2 3 4 0/30 0/5 0/10 0/15 0/5 0/7 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 0
  • 32. construct a blocking flow 0/15 0/10 2 3 4 0/30 0/5 0/10 0/15 0/5 0/7 1 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 0
  • 33. construct a blocking flow 0/15 0/10 2 3 4 0/30 0/5 0/10 0/15 0/5 0/7 1 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 0
  • 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/7 1 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 0
  • 35. construct a blocking flow 10/15 10/10 2 3 4 10/30 0/5 0/10 10/15 0/5 0/7 1 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 10 0
  • 36. construct a blocking flow 10/15 10/10 10/30 2 3 3 4 10/15 0/5 0/10 0/5 0/7 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 10
  • 37. construct a blocking flow 10/15 10/10 10/30 2 3 3 4 10/15 0/5 0/10 0/5 0/7 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 10
  • 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/7 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 10
  • 39. construct a blocking flow 15/15 10/10 15/30 2 3 3 4 10/15 0/5 5/10 0/5 5/7 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 15 10
  • 40. construct a blocking flow 15/15 10/10 15/30 2 2 3 4 10/15 0/5 5/10 0/5 5/7 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 15 10
  • 41. construct a blocking flow 15/15 10/10 15/30 2 2 3 4 10/15 0/5 5/10 0/5 5/7 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 15 10
  • 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/7 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 15 10
  • 43. construct a blocking flow 15/15 10/10 17/30 2 2 3 4 10/15 2/5 5/10 2/5 7/7 1 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 17 15 10
  • 44. construct a blocking flow 15/15 10/10 2 3 4 17/30 2/5 5/10 10/15 2/5 7/7 1 3 4 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 17
  • 45. construct a blocking flow 15/15 10/10 2 3 4 17/30 2/5 5/10 10/15 2/5 7/7 1 3 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 17
  • 46. construct a blocking flow 15/15 10/10 2 3 4 17/30 2/5 5/10 10/15 2/5 7/7 1 3 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 17
  • 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/7 1 3 3 4 5 0/10 0/5 0/5 0/10 2 4 0/5 0/6 3 current flow: 17
  • 48. construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/7 1 3 3 4 5 0/10 0/5 3/5 3/10 2 4 0/5 0/6 3 current flow: 20 17
  • 49. construct a blocking flow 15/15 10/10 20/30 2 2 3 4 10/15 5/5 5/10 2/5 7/7 1 3 3 4 5 0/10 0/5 3/5 3/10 2 4 0/5 0/6 3 current flow: 20
  • 50. construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/7 1 1 3 3 4 5 0/10 0/5 3/5 3/10 2 4 0/5 0/6 3 current flow: 20
  • 51. construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/7 1 1 3 3 4 5 0/10 0/5 3/5 3/10 2 4 0/5 0/6 3 current flow: 20
  • 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/7 1 1 3 3 4 5 0/10 0/5 3/5 3/10 2 4 0/5 0/6 3 current flow: 20
  • 53. construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/7 1 1 3 3 4 5 2/10 2/5 5/5 5/10 2 4 0/5 0/6 3 current flow: 22 20
  • 54. construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/7 1 3 3 4 5 2/10 2/5 5/5 5/10 2 4 0/5 0/6 3 current flow: 22
  • 55. construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/7 1 3 3 4 5 2/10 2/5 5/5 5/10 2 2 4 0/5 0/6 3 current flow: 22
  • 56. construct a blocking flow 15/15 10/10 2 3 4 20/30 5/5 5/10 10/15 2/5 7/7 1 3 3 4 5 2/10 2/5 5/5 5/10 2 2 4 0/5 0/6 3 current flow: 22
  • 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/7 1 3 3 4 5 2/10 2/5 5/5 5/10 2 2 4 0/5 0/6 3 current flow: 22
  • 58. construct a blocking flow 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
  • 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. build a level graph 15/15 10/10 20/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. build a level graph 15/15 10/10 20/30 5/5 5/10 10/15 2/5 7/7 1 7/10 2/5 5/5 10/10 4 5/5 5/6 current flow: 27 22
  • 62. build a level graph 15/15 10/10 2 20/30 5/5 5/10 10/15 2/5 7/7 1 7/10 2/5 5/5 10/10 2 4 5/5 5/6 current flow: 27 22
  • 63. build a level graph 15/15 10/10 2 20/30 5/5 5/10 10/15 2/5 7/7 1 3 7/10 2/5 5/5 10/10 2 4 5/5 5/6 current flow: 27 22
  • 64. build a level graph 15/15 10/10 2 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
  • 65. build a level graph 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
  • 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. Maximum Flow: 27 15/15 10/10 20/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. 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 to b. // 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. Source Code bool 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. Source Code int 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. 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. 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. Practice Now [POJ] 1459 - Power Network
  • 74. Reference • 演算法筆記- Flow • Maximum flow problem - Wikipedia, the free encyclopedia
  • 75. Thank You for Your Listening.