0% found this document useful (0 votes)
63 views75 pages

Dinic'S Algorithm: 郭至軒( Kuoe0) Kuoe0.Ch

Dinic's algorithm is used to find the maximum flow in a flow network. It works by building a level graph of the network and finding augmenting paths from the source to the sink. It then determines the bottleneck capacity on each path and constructs a blocking flow by pushing flow along paths until no more augmenting paths exist.

Uploaded by

Satyam Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views75 pages

Dinic'S Algorithm: 郭至軒( Kuoe0) Kuoe0.Ch

Dinic's algorithm is used to find the maximum flow in a flow network. It works by building a level graph of the network and finding augmenting paths from the source to the sink. It then determines the bottleneck capacity on each path and constructs a blocking flow by pushing flow along paths until no more augmenting paths exist.

Uploaded by

Satyam Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

Dinics Algorithm

KuoE0
[email protected]
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
1
2 3
0/10
0/10

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

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

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

S
1
s 0/8 4t
0/10 0/10
0/9
2 4
3
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

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

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

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
11 3 4 5
0/10 0/5 0/5
0/10
2 4
0/5 0/6

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
11 3 4 5
0/10 0/5 0/5
0/10
2 4
0/5 0/6

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
11 3 4 5
0/10 0/5 0/5
0/10
2 4
0/5 0/6

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
11 3 4 5
0/10 0/5 0/5
0/10
2 4
0/5 0/6

current flow: 10
0
construct a blocking flow

10/15 10/10

10/30
2 33 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

current flow: 10
construct a blocking flow

10/15 10/10

10/30
2 33 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

current flow: 10
construct a blocking flow
bottleneck: 5
10/15 10/10

10/30
2 33 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

current flow: 10
construct a blocking flow

15/15 10/10

15/30
2 33 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

current flow: 10
15
construct a blocking flow

15/15 10/10

15/30
22 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

current flow: 10
15
construct a blocking flow

15/15 10/10

15/30
22 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

current flow: 10
15
construct a blocking flow
bottleneck: 2
15/15 10/10

15/30
22 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

current flow: 10
15
construct a blocking flow

15/15 10/10

17/30
22 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

current flow: 10
15
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 44 5
0/10 0/5 0/5
0/10
2 4
0/5 0/6

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 33 4 5
0/10 0/5 0/5
0/10
2 4
0/5 0/6

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 33 4 5
0/10 0/5 0/5
0/10
2 4
0/5 0/6

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 33 4 5
0/10 0/5 0/5
0/10
2 4
0/5 0/6

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 33 4 5
0/10 0/5 3/5
3/10
2 4
0/5 0/6

current flow: 17
20
construct a blocking flow

15/15 10/10

20/30
22 3 4
10/15
5/5 5/10
2/5 7/7
1 33 4 5
0/10 0/5 3/5
3/10
2 4
0/5 0/6

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
11 33 4 5
0/10 0/5 3/5
3/10
2 4
0/5 0/6

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
11 33 4 5
0/10 0/5 3/5
3/10
2 4
0/5 0/6

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
11 33 4 5
0/10 0/5 3/5
3/10
2 4
0/5 0/6

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
11 33 4 5
2/10 2/5 5/5
5/10
2 4
0/5 0/6

current flow: 20
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 33 4 5
2/10 2/5 5/5
5/10
2 4
0/5 0/6

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 33 4 5
2/10 2/5 5/5
5/10
22 4
0/5 0/6

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 33 4 5
2/10 2/5 5/5
5/10
22 4
0/5 0/6

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 33 4 5
2/10 2/5 5/5
5/10
22 4
0/5 0/6

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 33 4 5
7/10 2/5 5/5
10/10
22 4
5/5 5/6

current flow: 22
27
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 33 4 5
7/10 2/5 5/5
10/10
22 4
5/5 5/6

current flow: 22
27
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: 22
27
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: 22
27
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: 22
27
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: 22
27
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: 22
27
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: 22
27
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: 22
27
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.

You might also like