assignment and matching - ucsb computer sciencesuri/cs231/matching.pdf · assignment and matching...

30
Subhash Suri UC Santa Barbara Assignment and Matching 1. A matching is a pairing of nodes—collection of disjoint edges. Worker Job 1 2 4 A B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. An edge (i, j ) means worker i can do job j . 4. If weighted, then c(i, j ) is the proficiency of i at job j . (In unweighted case, c(i, j )=1.) 5. Workers to jobs assignment for maximizing total proficiency. 6. Each worker assigned to at most one job and vice versa, so this is a matching.

Upload: buidat

Post on 17-Mar-2018

233 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Assignment and Matching

1. A matching is a pairing of nodes—collectionof disjoint edges.

Worker Job

1

2

4

A

B

C

D

3

2. Bipartite graph. Two node classes,workers and jobs.

3. An edge (i, j) means worker i can do job j.

4. If weighted, then c(i, j) is the proficiency of iat job j. (In unweighted case, c(i, j) = 1.)

5. Workers to jobs assignment for maximizingtotal proficiency.

6. Each worker assigned to at most one joband vice versa, so this is a matching.

Page 2: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Applications

1. [Rooming Problem.] Dorm roomassignment. Graph G with students asnodes. Weight cij is compatibility of pair(i, j).

2. [Airline Pilot Assignment.]

• Airlines need to form teams of captainand first officer.

• αi is effectiveness of i as captain.• βi is effectiveness of i as 1st officer.• Seniority Rule: captain more senior.• Make edge weight

cij ={

αi + βj if i more seniorαj + βj otherwise

3. In these applications, the graph is notbipartite. We will only study the bipartitecase.

Page 3: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

More Applications

4. [Stable Marriage.]

• Men {A,B, . . . , Z}, women {a, b, . . . , z}.• Their preference tables.

A

B

C

Men’s Preferences

b c a

b a c

c a b

C B

C B

a

b

c

C B A

A

A

Women’s Prefereces

• A matching M .

• M is unstable if ∃ pair (Bob, Sally) who likeeach other more than their spouses.

• Is stable marriage always possible?

• Medical schools use this protocol.

• Gale-Shapely Theorem: A stable marriagealways possible, and found in O(n2) time.

Page 4: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Stereo Vision

1. Stereo matching to locate objects in space.

2. Infrared sensors at two different locations.

3. Each sensor gives the angle of sight (line)on which the object lies.

. .. .

. . ...

.. .

O1 O2

l1ln h1

hn

4. If p objects, we get two sets of lines:{L1, L2, . . . , Lp} and {L′1, L′2, . . . , L′p}.

Page 5: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Stereo Vision

. .. .

. . ...

.. .

O1 O2

l1ln h1

hn

1. Two problems: (1) a line from one sensormight intersect multiple lines from theother; (2) due to noise, the lines for thesame object may not intersect.

2. Solve the problem using assingment.Nodes are lines. Cost cij is the distancebetween Li and L′j.

3. Distance between lines of the same objectshould be close to zero.

4. Optimal assingment should give excellentmatching of line.

Page 6: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Definitions

1. A matching M ⊆ E, in graph G = (V, E), is aset of edges no two sharing a vertex.

A matching M

non−matching edges

matching edges

2. |M | is the cardinality of M .

3. In unweighted graphs, find maxcardinality matching.

4. In weighted graphs, find max weightmatching.

5. A matching is perfect if all vertices arematched.

Page 7: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Perfect Matching

1. Consider a bipartite graph G = (U, V,E).

2. When does G have a perfect matching?

U V

u1

v2

v4

v3

u4

u3

u2

v1

Page 8: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Neighborhoods

1. A subset S ∈ U .

2. Neighborhood N(S) is vertices of Vadjacent to any vertex in S.

3. For example, N(u1) = {v2, v4}.

Hall’s Theorem: G has a perfect matchingiff |N(S)| ≥ |S|, for all S ⊆ U .

U V

u1

v2

v4

v3

u4

u3

u2

v1

Page 9: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Alternating Paths

1. A matching M has some matched and someunmatched (free) vertices.

2. Alternating path has edges alternatingbetween M and E −M .

U V

u1

v2

v4

v3

u4

u3

u2

v1

3. u2, u4 are free, while u1, u3 are matched.

4. Path u4, v4, u3, v2, u1, v3 is alternating.

5. An alternating path is augmenting if both ofits endpoints are free vertices.

6. Path u4, v4, u3, v2, u1, v3 is also augmenting.

Page 10: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Augmenting Matching

1. If a matching M has an augmenting path,then we get a larger matching M ′ byswapping the edges on the augmentingpath.

U V

u1

v2

v4

v3

u4

u3

u2

v1U V

u1

v2

v4

v3

u4

u3

u2

v1

Page 11: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Hall’s Theorem

Hall’s Theorem: G has a perfect matching iff|N(S)| ≥ |S|, for all S ⊆ U .

1. The direction PM ⇒ |N(S)| ≥ |S| is easy.

2. Consider any set S ⊆ U .

3. Let mate(u) be the vertex matched with u.

4. Since mate(ui) 6= mate(uj), it follows that| ∪u∈S mate(u)| ≥ |S|.

5. Since mate(u) ∈ N(u) ⊆ V , we must have|N(S)| ≥ |S|.

U V

u1

v2

v4

v3

u4

u3

u2

v1

Page 12: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Hall’s Proof

1. Now, suppose |N(S)| ≥ |S| holds, for allS ⊆ U .

2. Consider a max matching M , and let u bea free vertex in it.

3. Let Z be the set of all vertices reachablefrom u with an alternating path.

4. There is no free vertex in Z (exceptu)–otherwise, an augmenting path, whichcontradicts M ’s max size.

5. Let L = Z ∩ U , and R = Z ∩ V .

U V

u1

v2

v4

v3

u4

u3

u2

v1

Page 13: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Hall’s Proof

U V

u1

v2

v4

v3

u4

u3

u2

v1

1. Observe that N(L) = R.

2. Each vertex in L (except u) is matchedwith someone in R, and the mates aredistinct.

3. So, |R| = |L| − 1.

4. But then |N(L)| < |L|, a contradiction!

Page 14: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Unweighted BipartiteMatching

1. Unweighted matching via maxflow.U V

S T

1

1

1

1

All capacities 1

2. Maximum flow value equals |M |.3. With integral flow, matching ⇔ flows.

4. With integer capacities, there alwaysexists an integral flow. (Why?)

5. Think Ford-Fulkerson algorithm.

6. Max cardinality matching solved in O(n3)time.

Page 15: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Remarks

1. No such transformation for non-bipartitematching.

A matching M

non−matching edges

matching edges

2. Actually, Hall’s Theorem also invalid forgeneral graphs. (Example: a triangle.)

Tutte’s Theorem: G has a perfect matching ifffor all S ⊆ V , oc(G− s) ≤ |S|, where oc is numberof odd-cardinality components.

3. For bipartite matching, specializedmaxflow algorithm for unit networks runsin O(

√nm). (Read Section 8.2.)

Page 16: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Hungarian Method forAssignment

• Maxflow method does not work forweighted matching.

• G = (X,Y,E), with edge weights w(e),which is weight or benefit of e.

• Find optimal (max weight) assignment.

X

Y

10

02

4 3

0

2

5

• Assume complete graph—missing edgesgiven w(e) = 0. So, want a max weightperfect matching in G.

Page 17: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Feasible Vertex Labeling

• Real-valued labels `() such that

`(x) + `(y) ≥ w(x, y), ∀x ∈ X, y ∈ Y.

• Initial feasible labeling:

`(x) = maxy∈Y

{w(x, y)} for x ∈ X

`(y) = 0 for y ∈ Y

X

Y

10

02

4 3

0

2

5

4 3 5

0 0 0

X

Y

4 35

4 3 5

0 0 0

Vertex Labeling Equality Graph

• [Equality Graph] G` = (X, Y, E`), where

E` = {(x, y) | `(x) + `(y) = w(x, y)}.

Page 18: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Main Theorem

[Kuhn-Munkres.] For any feasible labeling `, if G`

contains a perfect matching M , then M is an optimalassignment.

3

2 2

3

X

Y

10

02

4 3

0

2

5

X

Y

Vertex Labeling Equality Graph

2 3

00

2 3

00

1. In a PM, each vertex is covered exactlyonce, so w(M) =

∑e∈M w(e) =

∑v∈V `(v)

2. Any other assingment M ′ in G satisfiesw(M ′) =

∑e∈M ′ w(e) ≤ ∑

v∈V `(v)

3. Thus, w(M ′) ≤ w(M), and M must beoptimal.

Page 19: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Hungarian Method

1. Initialize vertex labeling `. Determine G`.

2. Pick any matching M in G`.

3. If M perfect, stop. Otherwise, pick a freevertex u ∈ X. Set S = {u}, and T = ∅.

4. If N(S) = T , update labels:α` = minx∈S, y 6∈T {`(x) + `(y)− w(x, y)}

`′(v) =

`(v)− α` if v ∈ S`(v) + α` if v ∈ T`(v) otherwise

(Now N(S) 6= T .)

5. If N(S) 6= T , pick y ∈ N(S)− T .

• If y free, u–y is augmenting path;augment M and go to 3.

• If y matched, say, to z, then extendalternating tree: S = S ∪ {z}, T = T ∪ {y}.Go to 4.

Page 20: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Example of Hungarian

x1 x2 x3

y1 y2 y3

3

15

2

6

x1 x2 x3

y1 y2 y3

3 6 5

0 0 0

x3

y2

x2

Initial Graph Equality Graph Alternating Tree

Matching M wrt M

• A 3× 3 assignment problem.

• Initial labels and equality graph.

• Initial matching (x1, y1), (x2, y2).

• S = {x3}, T = ∅.• Since N(S) 6= T , we do step 5. Choose

y2 ∈ N(S)− T .

• y2 matched, add y2x2 (grow tree).

• Since N(S) = T , do step 4.

Page 21: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Example contd.

x1 x2 x3

y1 y2 y3

x1 x2 x3

y1 y2 y3

3 6

1 25

3 2 1

0 4 00 0 0

563

36

1 52

New Eq. Graph

• S = {x2, x3}, and T = {y2}.• Calculate α:

α` = minx∈S,y 6∈T

6 + 0− 0 x2y1

6 + 0− 2 x2y3

5 + 0− 1 x3y1

5 + 0− 0 x3y3

= 4

• Reduce labels for S, increase for T , by 4.

• New equality graph has a perfectmatching.

Page 22: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Analysis

x1 x2 x3

y1 y2 y3

x1 x2 x3

y1 y2 y3

3 6

1 25

3 2 1

0 4 00 0 0

563

36

1 52

New Eq. Graph

• Relabeling ensures that at least one newedge added to G`.

• Relabeling ensures no edge of G` removed.

• In a worst-case, all edges of G wouldeventually appear in G`.

• Thus, a perfect matching guaranteed tobe found.

Page 23: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Complexity

x1 x2 x3

y1 y2 y3

3

15

2

6

x1 x2 x3

y1 y2 y3

3 6 5

0 0 0

x3

y2

x2

Initial Graph Equality Graph Alternating Tree

Matching M wrt M

• Algorithm has n phases, in each phasematching size grows by 1.

• Keep track of edge with smallest slack:`(x) + `(y)− w(x, y), where x ∈ S, y 6∈ T .

• Initial slack calculation takes O(n2) time.

• When a vertex moves from S̄ to S, wecompute slacks for all y 6∈ T .

• In each phase, at most n vertices go fromS̄ to S, so n slack re-calculations, each atO(n) time, for a total of O(n2).

• Total algorithm takes O(n3).

Page 24: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Other Results on Matching

• [Bipartite Cardinality Matching:]O(√

nm) time. Maxflow on unit capacitynetworks. [Even-Tarjan]

• [Non-Bipartite Cardinality Matching:]First polynomial time, O(n4), algorithm in1957 by Edmonds.Current best O(

√nm) [Micali-Vazirani].

• [Bipartite Weighted Matching:]O(nm + n2 log n) strongly poly.O(√

nm log(nC)) scaling algorithm.

• [Non-Bipartite Weighted Matching:]O(n3) by Edmonds+Gabow ’75.Current best O(nm + n2 log n).

Page 25: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Stable Matching Problem

• Society of n men (A,B, . . . , Z) and nwomen (a, b, . . . , z).

• Each man (woman) ranks all women(man), in descending order of preference.

C

B

A c a b

c b a

a b c

a

b

c AB

C B A

B A C

Women’s PreferencesMen’s Preferences

C

• A matching is a 1-to-1 correspondence(monogamous, heterosexual marriage).

• A pair (M, w) is unstable if M and w likeeach other more their assigned partners.

• A matching is called unstable if it has aunstable pair (risks elopement).

• Determine a stable matching.

Page 26: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Stable Matching

• The matching {(A, c), (B, b), (C, a)} leads toan unstable pair (B, c).

C

B

A c a b

c b a

a b c

a

b

c AB

C B A

B A C

Women’s PreferencesMen’s Preferences

C

• The matching {(A, b), (B, c), (C, a)} is stable.

Applications:

• The method used by Medical Schools forselecting residents.

• Hong Kong state universities use stablematching for admissions.

• Several books just on stable marriage.

Page 27: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Stable Matching Theorem

Theorem: Stable matching is alwayspossible. [Gale Shapely 1955]

• We will prove this theorem by presentingan algorithm that always returns a stablematching.

• Basic principle: Man proposes, woman disposes.

• Each unattached man proposes to thehighest-ranked woman in his list, who hasnot already rejected him.

• If the man proposing to her is better thanher current mate, the woman dumps hercurrent partner, and becomes engaged tothe new proposer.

• Since no man proposes to the same womantwice, the algorithm terminates, and weprove the result is a stable matching.

Page 28: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Stable Matching Algorithm

• LIST : list of unattached men.

• cur(m): highest ranked woman in m’s list,who has not rejected him.

• Initialize LIST = {1, 2, . . . , n} andcur(i) = M(i, 1).

• Choose a man, say, Bob, from LIST . Bobproposes to Alice, where Alice = cur(Bob).

• If Alice unattached, Bob and Alice areengaged.

• If Alice is engaged to, say, John, butprefers Bob, she dumps John, and Boband Alice are engaged. Otherwise, sherejects Bob.

• The rejected man rejoins LIST , andupdates his cur.

• Output the engaged pairs when LIST = ∅.

Page 29: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Illustration of theAlgorithm

C

B

A c a b

c b a

a b c

a

b

c AB

C B A

B A C

Women’s PreferencesMen’s Preferences

C

• Final matching {(A, b), (B, c), (C, a)}.• The algorithm terminates in O(n2) steps,

since each step moves one cur pointer, andthere are at most n2 preferences.

• Remains to prove the matching is alwaysstable.

Page 30: Assignment and Matching - UCSB Computer Sciencesuri/cs231/Matching.pdf · Assignment and Matching 1. ... B C D 3 2. Bipartite graph. Two node classes, workers and jobs. 3. ... Optimal

Subhash Suri UC Santa Barbara

Correctness

• Suppose the resulting matching has aunstable pair (Dick, Laura).

• Dick must have proposed to Laura atsome point.

• During the algorithm, Laura also rejectedDick in favor of some she prefers more.

• Since no woman ever switches to a manless desirable than her current partner,Laura’s current partner must be moredesirable than Dick.

• Thus, the pair (Dick, Laura) is notunstable.