CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005,...

34
CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some by Iker Gondra

description

Propose-And-Reject Algorithm (Recall) Propose-and-reject algorithm [Gale-Shapley 1962]: Intuitive method that guarantees output which is a stable matching Initialize each person to be free while (some man is free and hasn't proposed to every woman) { Choose such a man m w = 1 st woman on m's list to whom m has not yet proposed if (w is free) assign m and w to be engaged else if (w prefers m to her fiancé m') assign m and w to be engaged, and m' to be free else w rejects m }

Transcript of CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005,...

Page 1: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

CSCI 256 Data Structures and Algorithm Analysis Lecture 2

Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some by Iker Gondra

Page 2: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Stable Matching Problem (Recall)

• Perfect matching• Preference lists (ranking is strictly increasing)• Stability

– Following is not stable

m1 w1

m2 w2

Page 3: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Propose-And-Reject Algorithm (Recall)

• Propose-and-reject algorithm [Gale-Shapley 1962]: Intuitive method that guarantees output which is a stable matching

Initialize each person to be freewhile (some man is free and hasn't proposed to every woman) { Choose such a man m w = 1st woman on m's list to whom m has not yet proposed if (w is free) assign m and w to be engaged else if (w prefers m to her fiancé m') assign m and w to be engaged, and m' to be free else w rejects m}

Page 4: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Does this Work?

• Does it terminate?• Is the result a stable matching?• Is there a unique solution?• What is the complexity

• Begin analysis by identifying measures of progress and invariants– Observation 1: Men propose to women in decreasing order of

preference– Observation 2: Once a woman is matched, she never

becomes unmatched; she only “trades up.”

Page 5: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Proof of Correctness: Termination

• Denote a Gale-Shapely marching by S*• Claim: Algorithm terminates after at most n2

iterations of while loop. Why?– Pf: Each time through the while loop a man proposes

to a new woman. There are only n2 possible proposals ▪

Page 6: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Proof of Correctness: Perfection

• Claim: All men and women get matched. Why?– Pf: (by contradiction)

• Suppose, for sake of contradiction, that Zeus is not matched upon termination of algorithm

• Then some woman, say Amy, is not matched upon termination

• By Observation 2, Amy was never proposed to• But, Zeus proposes to everyone – contradiction!▪

• Hence, the algorithm finds a perfect matching

Page 7: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Proof of Correctness: Stability

• Claim: There are no unstable pairs in S*• Pf: (by contradiction)

– Suppose A-Z is an unstable pair: each prefers each other to the partner in Gale-Shapley matching S*

– Case 1: Z never proposed to A Z prefers his GS partner to A A-Z is stable

– Case 2: Z proposed to A A rejected Z (right away or later) A prefers her GS partner to Z A-Z is stable

– In either case A-Z is stable, a contradiction ▪

Bertha-ZeusAmy-Yancey

S*

. . .

men propose in decreasingorder of preference –Obs 1

women only trade up

Page 8: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Result

• Simple, O(n2) algorithm to compute a stable matching

• Corollary– A stable matching always exists

Page 9: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Understanding the Solution

• For a given problem instance, there may be several stable matchings:

m1: w1 w2 w3

m2: w2 w3 w1

m3: w3 w1 w2

w1: m2 m3 m1

w2: m3 m1 m2

w3: m1 m2 m3

m1

m2

m3

w1

w2

w3

How many stable matchings can you find?

Page 10: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Understanding the Solution

• Q: Do all executions of Gale-Shapley yield the same stable matching? If so, which one?m1: w1 w2 w3

m2: w1 w3 w2

m3: w1 w2 w3

w1: m2 m3 m1

w2: m3 m1 m2

w3: m3 m1 m2

m1

m2 w2

w1

m3 w3

Page 11: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Understanding the Solution

• Stable matchings are not necessarily fair• Many different ways of picking m’s to propose• Surprising result:

– All executions (orderings of picking free m’s) in G-S approach give the same result

Page 12: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Understanding the Solution

• Proposal Algorithm (G-S algorithm) finds the best possible solution for M (i.e., there is no stable matching in which any of the men could have hoped to do better)

• Formalize the notion of best possible solution– (m, w) is valid if (m, w) is in some stable matching– best(m): the highest ranked w for m, such that (m, w) is

valid (i.e., there is no w ' such that w ' ranks higher than w and (m, w ') is valid)

– S* = {(m, best(m)}– Every execution of the proposal algorithm computes S*

(proof in page 11 of the book) • uniqueness

Page 13: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Understanding the Solution

• Related result: Proposal algorithm yields the worst valid partner for the W’s ( i.e., for each w, (w,m) is valid (i.e. there is a stable matching containing the pair (w,m) ) and no m’ that w ranks lower is a valid partner)

• Algorithm is the M-optimal algorithm • Proposal algorithms where w’s propose is W-

optimal

Page 14: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Let’s prove that the algorithm gives the worst valid partner for each w• Lemma: In the stable matching S*, each woman is

paired with her worst valid partner.• Proof in class

– Sketch -- do by contradiction; assume there is a pair (m,w) in S* such that m is not the worst valid partner

– Then there is a stable matching S’ in which w is paired with a man m’, whom she likes less than m

– What can we say about m in S’ ( he must be paired with a woman w’, (m,w’) is valid and since (m,w) is in S*, w is the best valid partner for m; consequently m prefers w to w’)

– We can see that S’ is unstable --- Why???– Contradiction --- Why??

Page 15: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Key Ideas/Steps in Algorithm Design as exemplified in this sample problem: • Formalize real world problem

– Model: graph and preference lists– Mechanism: stability condition

• Specify an algorithm with a natural operation– e.g. Proposal in G-S

• Establish termination of process through invariants and progress measure

• Underspecify an algorithm – e.g. in G-S as long as there is a free man one can

choose any free man)• Establish uniqueness of solution

Page 16: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

A Landscape of Representative Problems • Show the types of problems we will consider• We’ll see that similar looking problems can have

very different characteristics and require different methods

• Problems– Interval Scheduling– Weighted Interval Scheduling– Bipartite Matching– Maximum Independent Set– Competitive Facility Location

Page 17: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Interval Scheduling

• Input: Set of jobs with start and finish times• Goal: Find maximum cardinality subset of

mutually compatible jobs (nlogn greedy algorithm)

Time0 1 2 3 4 5 6 7 8 9 10 11

f

g

h

e

a

b

c

d

h

e

b

jobs don't overlap

Page 18: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Weighted Interval Scheduling

• Input: Set of jobs with start, finish, and weights• Goal: Find maximum weight subset of mutually

compatible jobs

Time0 1 2 3 4 5 6 7 8 9 10 11

20

11

16

13

23

12

20

26

n log n dynamic programming algorithm

Page 19: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Bipartite Graph

• A graph G = (V, E) is bipartite if V can be partitioned into sets X and Y such that every edge in E has one end in X and the other end in Y

Page 20: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Bipartite Graph

C

1

5

2

A

E

3

B

D 4

Page 21: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Bipartite Graph

• Many relationships are expressed by a bipartite graph

C

1

5

2

A

E

3

B

D 4

professors courses

can teach

Page 22: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Bipartite Matching

• Input: Bipartite graph• Goal: Find maximum cardinality matching

– A matching is M E with property that each node in V appears in at most one edge of M

Page 23: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Find a Maximum Matching

Solutions use max-flow based algorithms (Polynomial time)

Page 24: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Independent Set Problem

• Input: Graph• Goal: Find maximum cardinality independent set

6

2

5

1

7

34

6

5

1

4

subset of nodes such that no two joined by an edge

Page 25: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Find a Maximum Independent Set

NP-complete

No efficient algorithm is

known, and it is

conjectured that no such

algorithm exists

Page 26: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Key characteristics of the Independent Set Problem

• Hard to find a solution• Easy to verify a solution once you have one• Can encode interval scheduling and bipartite

matching problems as Independent Set problems• Other problems like this

– Hamiltonian cycle– Traveling salesman– 3-Sat– Graph coloring

Page 27: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

NP-Completeness

• Theory of Hard Problems (like the independent set problem)

• A large number of problems are known to be equivalent• Very elegant theory

Page 28: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Are There Even Harder Problems?

• Simple game:– Players alternating selecting nodes in a graph

• Score points associated with node• Once node is picked, remove node’s neighbors

– When neither can move, player with most points wins

Page 29: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Another Game: Competitive Facility Location

For a particular player Pi, is there a strategy so that, no

matter how the other players play,

Pi will be able to select a set of

nodes with a total value of at least X? Hard to convince someone of the answer to this

question (i.e., no short proof,

lengthy case-by-case analysis), compare with

Independent Set Problem

PSPACE-complete

Page 30: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Competitive Facility Location

• PSPACE-complete problems are believed to be strictly harder than NP-complete problems as there does not appear to be a short proof if there is a solution

• For many problems that involve game playing and planning, it seems that one must do a lengthy case-by-case analysis of the set of possible moves.

Page 31: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

Theorem: Output of G-S Algorithm is S* {(m,best(m)) for all m}• Proof Sketch:• By contradiction – (what do we say??)

Page 32: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

• Assume in output of G-S there is some man who is not paired with his best valid partner – (What does this imply?)

Page 33: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

• (Because men propose in decreasing order of preference) some man m is rejected by a valid partner;

• Consider the first time in execution this happens; let m be this man who is rejected by a valid partner, w

• Then w must be best(m) (Why – again because m proposes in order of preference.)

• Now focus on w

Page 34: CSCI 256 Data Structures and Algorithm Analysis Lecture 2 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.

• w now forms or continues an engagement with m’, who she prefers to m

• Also there is a stable matching S’ containing the pair (m,w) ( since w is a valid partner of m)

• Who is m’ paired with in this S’?• It can’t be w, so let it be w’ not equal to w• We can show that (m’,w’) is unstable

– m’ prefers w to w’ (m’ proposed in order of preference) – and w prefers m’ to m (see above)

• Conclude that S’ is not a stable matching• A contradiction!!!• Conclude in output of G-S: for all men m, m is paired with best(m)