SlideShare une entreprise Scribd logo
1  sur  75
Télécharger pour lire hors ligne
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.

Contenu connexe

Tendances

指数時間アルゴリズム入門
指数時間アルゴリズム入門指数時間アルゴリズム入門
指数時間アルゴリズム入門Yoichi Iwata
 
様々な全域木問題
様々な全域木問題様々な全域木問題
様々な全域木問題tmaehara
 
プログラミングコンテストでのデータ構造
プログラミングコンテストでのデータ構造プログラミングコンテストでのデータ構造
プログラミングコンテストでのデータ構造Takuya Akiba
 
LCA and RMQ ~簡潔もあるよ!~
LCA and RMQ ~簡潔もあるよ!~LCA and RMQ ~簡潔もあるよ!~
LCA and RMQ ~簡潔もあるよ!~Yuma Inoue
 
プログラミングコンテストでのデータ構造 2 ~動的木編~
プログラミングコンテストでのデータ構造 2 ~動的木編~プログラミングコンテストでのデータ構造 2 ~動的木編~
プログラミングコンテストでのデータ構造 2 ~動的木編~Takuya Akiba
 
最小カットを使って「燃やす埋める問題」を解く
最小カットを使って「燃やす埋める問題」を解く最小カットを使って「燃やす埋める問題」を解く
最小カットを使って「燃やす埋める問題」を解くshindannin
 
AtCoder Beginner Contest 034 解説
AtCoder Beginner Contest 034 解説AtCoder Beginner Contest 034 解説
AtCoder Beginner Contest 034 解説AtCoder Inc.
 
AtCoder Beginner Contest 023 解説
AtCoder Beginner Contest 023 解説AtCoder Beginner Contest 023 解説
AtCoder Beginner Contest 023 解説AtCoder Inc.
 
プログラミングコンテストでのデータ構造 2 ~平衡二分探索木編~
プログラミングコンテストでのデータ構造 2 ~平衡二分探索木編~プログラミングコンテストでのデータ構造 2 ~平衡二分探索木編~
プログラミングコンテストでのデータ構造 2 ~平衡二分探索木編~Takuya Akiba
 
二部グラフの最小点被覆と最大安定集合と最小辺被覆の求め方
二部グラフの最小点被覆と最大安定集合と最小辺被覆の求め方二部グラフの最小点被覆と最大安定集合と最小辺被覆の求め方
二部グラフの最小点被覆と最大安定集合と最小辺被覆の求め方Kensuke Otsuki
 
AtCoder Beginner Contest 020 解説
AtCoder Beginner Contest 020 解説AtCoder Beginner Contest 020 解説
AtCoder Beginner Contest 020 解説AtCoder Inc.
 
AtCoder Beginner Contest 015 解説
AtCoder Beginner Contest 015 解説AtCoder Beginner Contest 015 解説
AtCoder Beginner Contest 015 解説AtCoder Inc.
 
Union find(素集合データ構造)
Union find(素集合データ構造)Union find(素集合データ構造)
Union find(素集合データ構造)AtCoder Inc.
 

Tendances (20)

Rolling hash
Rolling hashRolling hash
Rolling hash
 
指数時間アルゴリズム入門
指数時間アルゴリズム入門指数時間アルゴリズム入門
指数時間アルゴリズム入門
 
様々な全域木問題
様々な全域木問題様々な全域木問題
様々な全域木問題
 
プログラミングコンテストでのデータ構造
プログラミングコンテストでのデータ構造プログラミングコンテストでのデータ構造
プログラミングコンテストでのデータ構造
 
目指せグラフマスター
目指せグラフマスター目指せグラフマスター
目指せグラフマスター
 
LCA and RMQ ~簡潔もあるよ!~
LCA and RMQ ~簡潔もあるよ!~LCA and RMQ ~簡潔もあるよ!~
LCA and RMQ ~簡潔もあるよ!~
 
プログラミングコンテストでのデータ構造 2 ~動的木編~
プログラミングコンテストでのデータ構造 2 ~動的木編~プログラミングコンテストでのデータ構造 2 ~動的木編~
プログラミングコンテストでのデータ構造 2 ~動的木編~
 
双対性
双対性双対性
双対性
 
動的計画法を極める!
動的計画法を極める!動的計画法を極める!
動的計画法を極める!
 
最小カットを使って「燃やす埋める問題」を解く
最小カットを使って「燃やす埋める問題」を解く最小カットを使って「燃やす埋める問題」を解く
最小カットを使って「燃やす埋める問題」を解く
 
AtCoder Beginner Contest 034 解説
AtCoder Beginner Contest 034 解説AtCoder Beginner Contest 034 解説
AtCoder Beginner Contest 034 解説
 
最大流 (max flow)
最大流 (max flow)最大流 (max flow)
最大流 (max flow)
 
AtCoder Beginner Contest 023 解説
AtCoder Beginner Contest 023 解説AtCoder Beginner Contest 023 解説
AtCoder Beginner Contest 023 解説
 
abc032
abc032abc032
abc032
 
arc047
arc047arc047
arc047
 
プログラミングコンテストでのデータ構造 2 ~平衡二分探索木編~
プログラミングコンテストでのデータ構造 2 ~平衡二分探索木編~プログラミングコンテストでのデータ構造 2 ~平衡二分探索木編~
プログラミングコンテストでのデータ構造 2 ~平衡二分探索木編~
 
二部グラフの最小点被覆と最大安定集合と最小辺被覆の求め方
二部グラフの最小点被覆と最大安定集合と最小辺被覆の求め方二部グラフの最小点被覆と最大安定集合と最小辺被覆の求め方
二部グラフの最小点被覆と最大安定集合と最小辺被覆の求め方
 
AtCoder Beginner Contest 020 解説
AtCoder Beginner Contest 020 解説AtCoder Beginner Contest 020 解説
AtCoder Beginner Contest 020 解説
 
AtCoder Beginner Contest 015 解説
AtCoder Beginner Contest 015 解説AtCoder Beginner Contest 015 解説
AtCoder Beginner Contest 015 解説
 
Union find(素集合データ構造)
Union find(素集合データ構造)Union find(素集合データ構造)
Union find(素集合データ構造)
 

Plus de Chih-Hsuan Kuo

[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-selectChih-Hsuan Kuo
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in RustChih-Hsuan Kuo
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。Chih-Hsuan Kuo
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Chih-Hsuan Kuo
 
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 GeckoChih-Hsuan Kuo
 
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 OSChih-Hsuan Kuo
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in GeckoChih-Hsuan Kuo
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!Chih-Hsuan Kuo
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...Chih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree IsomorphismChih-Hsuan Kuo
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint SetChih-Hsuan Kuo
 

Plus 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.