[acm-icpc] bipartite matching

70
Bipartite Matching 郭至軒(KuoE0[email protected] KuoE0.ch

Upload: chih-hsuan-kuo

Post on 15-May-2015

940 views

Category:

Education


3 download

TRANSCRIPT

Page 2: [ACM-ICPC] Bipartite Matching

Bipartite Graphtwo disjoint sets

every edge connects between them

Page 3: [ACM-ICPC] Bipartite Matching

Matching

A subset of edges, no two of which share an endpoint.

Page 4: [ACM-ICPC] Bipartite Matching

Valid Matching

bachelor

not matching edgematching edge

Page 5: [ACM-ICPC] Bipartite Matching

Invalid Matching

shared

not matching edgematching edge

Page 6: [ACM-ICPC] Bipartite Matching

Bipartite Matching

objective: more matching

Page 7: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

Page 8: [ACM-ICPC] Bipartite Matching

Alternating Path

A path in which the edges belong alternatively to the matching and not to the matching.

Page 9: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edge

[1 - 5 - 2 - 9] is an alternating path.

matching edge

Page 10: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edge

[1 - 5 - 2 - 9] is an alternating path.

matching edge

Page 11: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

[1 - 5 - 2 - 6 - 3] is not an alternating path.

Page 12: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

[1 - 5 - 2 - 6 - 3] is not an alternating path.

Page 13: [ACM-ICPC] Bipartite Matching

Alternating Cycle

A cycle in which the edges belong alternatively to the matching and not to the matching.

Page 14: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edge

[1 - 5 - 2 - 9 - 1] is an alternating cycle.

matching edge

Page 15: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edge

[1 - 5 - 2 - 9 - 1] is an alternating cycle.

matching edge

Page 16: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

Reverse the alternating cycle, cardinality won’t change.

Page 17: [ACM-ICPC] Bipartite Matching

Augmenting Path AlgorithmMaximum Matching

Page 18: [ACM-ICPC] Bipartite Matching

Augmenting Path

An alternating path that starts from and ends on unmatched vertices.

Page 19: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

[2 - 5 - 1 - 9] is an augmenting path.

Page 20: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

[2 - 5 - 1 - 9] is an augmenting path.

Page 21: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

Reverse the augmenting path, cardinality will increase.

Page 22: [ACM-ICPC] Bipartite Matching

Augmenting Path• The length of an augmenting path

always is odd.

• Reversing an augmenting path will increase the cardinality.

• If there is no augmenting path, the cardinality is maximum.

Page 23: [ACM-ICPC] Bipartite Matching

1.Try to build augmenting paths from all vertices in one side.

2.Travel on graph.

3. If an augmenting path exists, reverse the augmenting path to increase cardinality.

4. If no augmenting path exists, ignore this vertex.

5.Repeat above step until there no augmenting path exists.

Algorithm

Page 24: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

[1 - 5] is an augmenting path.

current cardinality: 0

Page 25: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

[1 - 5] is an augmenting path.

current cardinality: 0

Page 26: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

Reverse it!

not matching edgematching edge

current cardinality: 1

Page 27: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

[2 - 5 - 1- 9] is an augmenting path.

not matching edgematching edge

current cardinality: 1

Page 28: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

[2 - 5 - 1- 9] is an augmenting path.

not matching edgematching edge

current cardinality: 1

Page 29: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

current cardinality: 2

Reverse it!

Page 30: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

current cardinality: 2

[3 - 6] is an augmenting path.

Page 31: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

current cardinality: 2

[3 - 6] is an augmenting path.

Page 32: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

current cardinality: 3

Reverse it!

Page 33: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

current cardinality: 3

[4 - 5 - 2 - 6 - 3 - 7] is an augmenting path.

Page 34: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

current cardinality: 3

[4 - 5 - 2 - 6 - 3 - 7] is an augmenting path.

Page 35: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

current cardinality: 4

Reverse it!

Page 36: [ACM-ICPC] Bipartite Matching

1

2

84

5

7

9

3

6

not matching edgematching edge

Max cardinality is 4.

Page 37: [ACM-ICPC] Bipartite Matching

Time Complexity

O(V × E)

V: number of verticesE: number of edges

Page 38: [ACM-ICPC] Bipartite Matching

// find an augmenting pathbool find_aug_path( int x ) { for ( int i = 0; i < ( int )vertex[ x ].size(); ++i ) { int next = vertex[ x ][ i ]; // not in augmenting path if ( !visit[ next ] ) { // setup this vertex in augmenting path visit[ next ] = 1; /* * If this vertex is a unmatched vertex, reverse augmenting path and return. * If this vector is a matched vertex, try to reverse augmenting path and continue find an unmatched vertex. */ if ( lnk2[ next ] == -1 || find_aug_path( lnk2[ next ] ) ) { lnk1[ x ] = next, lnk2[ next ] = x; return 1; } } } return 0;}

Source Code

Page 40: [ACM-ICPC] Bipartite Matching

Hungarian Algorithm(Kuhn-Munkres Algorithm)

Maximum Weight Matching

Page 41: [ACM-ICPC] Bipartite Matching

1 4

2

3

5

6

532

1

-25

Page 42: [ACM-ICPC] Bipartite Matching

weight adjustment

If add (subtract) some value on all edges connected with vertex X, the maximum matching won’t be effected.

1 4

2

3

5

6

abc

not matching edgematching edge

1 4

2

3

5

6

a+db+dc+d

Page 43: [ACM-ICPC] Bipartite Matching

vertex labeling

For convenient, add a variable on vertices to denote the value add (subtract) on edges connected with them.

1 4

2

3

5

6

abc

not matching edgematching edge

1 4

2

3

5

6

a+db+dc+d

d ≡

Page 44: [ACM-ICPC] Bipartite Matching

vertex labeling

l(x)+l(y)≥w(x,y), for each edge(x,y)

1 4

2

3

5

6

3

532

1

-25

5

5

0

0

0

Page 45: [ACM-ICPC] Bipartite Matching

1 4

2

3

5

6

0

532

1

-25

2

4

1

3

0

minimize vertex labeling

Admissible Edge: l(x)+l(y)=w(x,y)

Page 46: [ACM-ICPC] Bipartite Matching

convertmaximum weight problem

tominimum vertex labeling problem

Page 47: [ACM-ICPC] Bipartite Matching

Augmenting Path with Admissible Edge

[1 - 4] is an augment path with admissible edges.

1 4

2

3

5

6

3

532

1

-25

5

5

0

0

0

Page 48: [ACM-ICPC] Bipartite Matching

Augmenting Path with Admissible Edge

[1 - 4] is an augment path with admissible edges.

1 4

2

3

5

6

3

532

1

-25

5

5

0

0

0

Page 49: [ACM-ICPC] Bipartite Matching

Augmenting Path with Admissible Edge

Reverse it!

1 4

2

3

5

6

3

532

1

-25

5

5

0

0

0

Current Weight = 5

Page 50: [ACM-ICPC] Bipartite Matching

Augmenting Path with Admissible Edge

No augmenting path found start from vertex 2.

1 4

2

3

5

6

3

532

1

-25

5

5

0

0

0

Page 51: [ACM-ICPC] Bipartite Matching

Augmenting Path with Admissible Edge

No augmenting path found start from vertex 2.

1 4

2

3

5

6

3

532

1

-25

5

5

0

0

0

Page 52: [ACM-ICPC] Bipartite Matching

Adjust Vertex Labeling

1 4

2

3

5

6

3

532

1

-25

5

5

0

0

0

relax value d = min(l(x)+l(y)-w(x,y))x: vertex in alternating pathy: vertex y not in alternating path

Page 53: [ACM-ICPC] Bipartite Matching

Adjust Vertex Labeling

1 4

2

3

5

6

3

532

1

-25

5

5

0

0

0

R(1,6)=3R(2,5)=2R(2,6)=5

relax d = 2

Page 54: [ACM-ICPC] Bipartite Matching

Adjust Vertex Labeling

l(x) subtract value d.l(y) add value d.

1 4

2

3

5

6

1

532

1

-25

3

5

0

2

0

Page 55: [ACM-ICPC] Bipartite Matching

Continue to Find Augmenting Path

1 4

2

3

5

6

1

532

1

-25

3

5

0

2

0

[2 - 5] is an augment path with admissible edges.

Page 56: [ACM-ICPC] Bipartite Matching

Continue to Find Augmenting Path

1 4

2

3

5

6

1

532

1

-25

3

5

0

2

0

[2 - 5] is an augment path with admissible edges.

Page 57: [ACM-ICPC] Bipartite Matching

Continue to Find Augmenting Path

1 4

2

3

5

6

1

532

1

-25

3

5

0

2

0

Reverse it!

Current Weight = 6

Page 58: [ACM-ICPC] Bipartite Matching

Continue to Find Augmenting Path

1 4

2

3

5

6

1

532

1

-25

3

5

0

2

0

No augmenting path found start from vertex 3.

Page 59: [ACM-ICPC] Bipartite Matching

Continue to Find Augmenting Path

1 4

2

3

5

6

1

532

1

-25

3

5

0

2

0

No augmenting path found start from vertex 3.

Page 60: [ACM-ICPC] Bipartite Matching

Adjust Vertex Labeling

1 4

2

3

5

6

1

532

1

-25

3

5

0

2

0

R(1,6)=1R(2,6)=3 relax d = 1

Page 61: [ACM-ICPC] Bipartite Matching

Adjust Vertex Labeling

1 4

2

3

5

6

0

532

1

-25

2

4

1

3

0

l(x) subtract value d.l(y) add value d.

Page 62: [ACM-ICPC] Bipartite Matching

Continue to Find Augmenting Path

1 4

2

3

5

6

0

532

1

-25

2

4

1

3

0

[3 - 5 - 2 - 4 - 1 - 6] is an augment path with admissible edges.

Page 63: [ACM-ICPC] Bipartite Matching

Continue to Find Augmenting Path

1 4

2

3

5

6

0

532

1

-25

2

4

1

3

0

[3 - 5 - 2 - 4 - 1 - 6] is an augment path with admissible edges.

Page 64: [ACM-ICPC] Bipartite Matching

Continue to Find Augmenting Path

1 4

2

3

5

6

0

532

1

-25

2

4

1

3

0

Reverse it!

Current Weight = 10

Page 65: [ACM-ICPC] Bipartite Matching

Maximum Weight Matching = 10

1 4

2

3

5

6

0

532

1

-25

2

4

1

3

0

Page 66: [ACM-ICPC] Bipartite Matching

Algorithm1. Initial vertex labeling to fit l(x)+l(y)≥w(x,y)2.Find augmenting paths composed with

admissible edges from all vertices in one side.

3. If no augmenting path exists, adjust vertex labeling until the augmenting path found.

4. If augmenting path exists, continue to find next augmenting path.

5.Repeat above step until there no augmenting path exists.

6.Calculate the sum of weight on matching edges.

Page 67: [ACM-ICPC] Bipartite Matching

POJ 2195 - Going Home

Practice Now

Page 70: [ACM-ICPC] Bipartite Matching

Thank You for Your Listening.