Presentation is loading. Please wait.

Presentation is loading. Please wait.

Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne COS 423: Theory of Algorithms Princeton University Spring, 2001 Kevin Wayne.

Similar presentations


Presentation on theme: "Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne COS 423: Theory of Algorithms Princeton University Spring, 2001 Kevin Wayne."— Presentation transcript:

1 Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne COS 423: Theory of Algorithms Princeton University Spring, 2001 Kevin Wayne

2 2 Algorithm. (webster.com) n A procedure for solving a mathematical problem (as of finding the greatest common divisor) in a finite number of steps that frequently involves repetition of an operation. n Broadly: a step-by-step procedure for solving a problem or accomplishing some end especially by a computer. "Great algorithms are the poetry of computation." Etymology. n "algos" = Greek word for pain. n "algor" = Latin word for to be cold. n Abu Ja'far al-Khwarizmi's = 9th century Arab scholar. – his book "Al-Jabr wa-al-Muqabilah" evolved into today's high school algebra text Theory of Algorithms

3 3 Imagine: A World With No Algorithms Fast arithmetic. n Cryptography. Quicksort. n Databases. FFT. n Signal processing. Huffman codes. n Data compression. Network flow. n Routing Internet packets. Linear programming. n Planning, decision-making.

4 4 What is COS 423? Introduction to design and analysis of computer algorithms. n Algorithmic paradigms. n Analyze running time of programs. n Data structures. n Understand fundamental algorithmic problems. n Intrinsic computational limitations. n Models of computation. n Critical thinking. Prerequisites. n COS 226 (array, linked list, search tree, graph, heap, quicksort). n COS 341 (proof, induction, recurrence, probability).

5 5 Administrative Stuff Lectures: (Kevin Wayne) n Monday, Wednesday 10:00 - 10:50, COS 104. TA's: (Edith Elkind, Sumeet Sobti) Textbook: Introduction to Algorithms (CLR). Grading: n Weekly problem sets. n Collaboration, no-collaboration. n Class participation, staff discretion. n Undergrad / grad. Course web site: courseinfo.princeton.edu/courses/COS423_S2001/courseinfo.princeton.edu/courses/COS423_S2001/ n Fill out questionnaire.

6 6 Approximate Lecture Outline Algorithmic paradigms. n Divide-and-conquer. n Greed. n Dynamic programming. n Reductions. Analysis of algorithms. n Amortized analysis. Data structures. n Union find. n Search trees and extensions. Graph algorithms. n Shortest path, MST. n Max flow, matching.

7 7 Approximate Lecture Outline NP completeness. n More reductions. n Approximation algorithms. Other models of computation. n Parallel algorithms. n Randomized algorithms. Miscellaneous. n Numerical algorithms. n Linear programming.

8 Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne College Admissions Sample problem. Algorithm. Analysis. References: The Stable Marriage Problem by Dan Gusfield and Robert Irving, MIT Press, 1989. Introduction to Algorithms by Jon Kleinberg and Éva Tardos.

9 9 College Admissions Goal: Design a self-reinforcing college admissions process. Given a set of preferences among colleges and applicants, can we assign applicants to colleges so that for every applicant X, and every college C that X is not attending, either: n C prefers every one of its admitted students to X; n X prefers her current situation to the situation in which she is attending college C. If this holds, the outcome is STABLE. n Individual self-interest prevents any applicant / college to undermine assignment by joint action.

10 Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Love, Marriage, and Lying Standard disclaimer.

11 11 Stable Matching Problem Problem: Given N men and N women, find a "suitable" matching between men and women. n Participants rate members of opposite sex. n Each man lists women in order of preference from best to worst. ZeusBerthaAmyDianeErikaClare YanceyAmyClareDianeBerthaErika XavierBerthaClareErikaDianeAmy WyattDianeAmyBerthaClareErika VictorBerthaDianeAmyErikaClare Man 1 st 2 nd 3 rd 4 th 5 th Men’s Preference List worstbest

12 12 Stable Matching Problem Problem: Given N men and N women, find a "suitable" matching between men and women. n Participants rate members of opposite sex. n Each man lists women in order of preference from best to worst. n Each woman lists men in order of preference. ErikaYanceyZeusWyattXavierVictor DianeVictorYanceyZeusXavierWyatt ClareWyattYanceyXavierZeusVictor BerthaXavierYanceyWyattVictorZeus AmyZeusWyattVictorYanceyXavier Woman 1 st 2 nd 3 rd 4 th 5 th Women’s Preference List worstbest

13 13 Stable Matching Problem Problem: Given N men and N women, find a "suitable" matching between men and women. n PERFECT MATCHING: everyone is matched monogamously. – each man gets exactly one woman – each woman gets exactly one man n STABILITY: no incentive for some pair of participants to undermine assignment by joint action. – in matching M, an unmatched pair (m,w) is UNSTABLE if man m and woman w prefer each other to current partners – unstable pair could each improve by dumping spouses and eloping STABLE MATCHING = perfect matching with no unstable pairs. (Gale and Shapley, 1962)

14 14 Lavender assignment is a perfect matching. Are there any unstable pairs? Men’s Preference List Women’s Preference List Zeus Yancey Xavier Man A B A 1 st B A B 2 nd C C C 3 rd Clare Bertha Amy Woman X X Y 1 st Y Y X 2 nd Z Z Z 3 rd  Yes. Bertha and Xavier form an unstable pair. They would prefer each other to current partners. B X Example

15 15 Example Green assignment is a stable matching. A B A CX X Y Y Y X Z Z Z Men’s Preference List Women’s Preference List Zeus Yancey Xavier Man1 st B A B 2 nd C C 3 rd Clare Bertha Amy Woman1 st 2 nd 3 rd

16 16 Example Orange assignment is also a stable matching. AX X Y Y Z Z Men’s Preference List Women’s Preference List Zeus Yancey Xavier Man1 st A B 2 nd C C 3 rd Clare Bertha Amy Woman1 st 2 nd 3 rd B A B C X Y Z

17 17 B Preference List Bob Chris Adam C A B D D DoofusABC D C A Stable Roommate Problem Not obvious that any stable matching exists. Consider related "stable roommate problem." n 2N people. n Each person ranks others from 1 to 2N-1. n Assign roommate pairs so that no unstable pairs. C A B D No stable matching. All 3 perfect matchings have unstable pair. E.g., A-C forms unstable pair in lavender matching. C A 1 st 2 nd 3 rd

18 18 Propose-And-Reject Algorithm Intuitive method that guarantees to find a stable matching. Initialize each person to be free. while (some man m is free and hasn't proposed to every woman) w = first 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 Gale-Shapley Algorithm (men propose)

19 19 Implementation and Running Time Analysis Engagements. n Maintain two arrays wife[m], and husband[w]; set equal to 0 if participant is free. n Store list of free men on a stack (queue). Preference lists. n For each man, create a linked list of women, ordered from favorite to worst. – men propose to women at top of list, if rejected goto next n For each woman, create a "ranking array" such that m th entry in array is woman's ranking of man m. – allows for queries of the form: does woman w prefer m to m' ? Resource consumption. n Time =  (N 2 ). n Space =  (N 2 ). n Optimal.

20 20 Men's Preference List Wyatt Victor Man1 st A B 2 nd C D 3 rd C B AZeus Yancey XavierC D A B B A D C 4 th E E 5 th A D E E D C B E Women's Preference List Bertha Amy Man1 st W X 2 nd Y Z 3 rd Y X VErika Diane ClareY Z V W W V Z X 4 th V W 5 th V Z X Y Y X W Z A Worst Case Instance Number of proposals  n(n-1) + 1. n Algorithm terminates when last woman gets first proposal. Number of proposals = n(n-1) + 1 for following family of instances.

21 21 Proof of Correctness Observation 1. Men propose to their favorite women first. Observation 2. Once a woman is matched, she never becomes unmatched. She only "trades up." Fact 1. All men and women get matched (perfect). n Suppose upon termination Zeus is not matched. n Then some woman, say Amy, is not matched upon termination. n By Observation 2, Amy was never proposed to. n But, Zeus proposes to everyone, since he ends up unmatched. (contradiction)

22 22 Proof of Correctness Observation 1. Men propose to their favorite women first. Observation 2. Once a woman is matched, she never becomes unmatched. She only "trades up." Fact 2. No unstable pairs. n Suppose (Amy, Zeus) is an unstable pair: each prefers each other to partner in Gale-Shapley matching S*. n Case 1. Zeus never proposed to Amy.  Zeus must prefer Bertha to Amy (Observation 1)  (Amy, Zeus) is stable. (contradiction) n Case 2. Zeus proposed to Amy.  Amy rejected Zeus (right away or later)  Amy prefers Yancey to Zeus (women only trade up)  (Amy, Zeus) is stable (contradiction) Bertha-Zeus Amy-Yancey S*

23 23 Understanding the Solution For a given problem instance, there may be several stable matchings. n Do all executions of Gale-Shapley yield the same stable matching? If so, which one? Fact 3. Yes. Gale-Shapley finds MAN-OPTIMAL stable matching! n Man m is a valid partner of woman w if there exists some stable matching in which they are married. n Man-optimal assignment: every man receives best valid partner. – simultaneously best for each and every man – there is no stable matching in which any single man individually does better

24 24 Proof of Fact 3 Proof. n Suppose, for sake of contradiction, some man is paired with someone other than best partner. – since men propose in decreasing order of preference, some man is rejected by valid partner n Let Yancey be first such man, and let Amy be first valid partner that rejects him. n When Yancey is rejected, Amy forms (or reaffirms) engagement with man, say Zeus, whom she prefers to Yancey. n Let Bertha be Zeus' partner in S. n Zeus not rejected by any valid partner at the point when Yancey is rejected by Amy (since Yancey is first to be rejected by valid partner). Thus, Zeus prefers Amy to Bertha. n But Amy prefers Zeus to Yancey. n Thus (Amy, Zeus) is unstable pair in S. Bertha-Zeus Amy-Yancey S

25 25 Understanding the Solution Fact 4. Gale-Shapley finds WOMAN-PESSIMAL matching. n Each woman married to worst valid partner. – simultaneously worst for each and every woman. – there is no stable matching in which any single woman individually does worse Proof. n Suppose (Amy, Zeus) matched in S*, but Zeus is not worst valid partner for Amy. n There exists stable matching S in which Amy is paired with man, say Yancey, whom she likes less than Zeus. n Let Bertha be Zeus' partner in S. n Zeus prefers Amy to Bertha (man optimality). n (Amy, Zeus) form unstable pair in S. Bertha-Zeus Amy-Yancey S

26 26 Understanding the Solution Fact 5. The man-optimal stable matching is weakly Pareto optimal. n There is no other perfect matching (stable or unstable), where every man does strictly better. Proof. n Let Amy be last woman in some execution of Gale-Shapley (men propose) algorithm to receive a proposal. n No man is rejected by Amy since algorithm terminates when last woman receives first proposal. n No man matched to Amy will be strictly better off than in man- optimal stable matching.

27 27 Extensions: Unacceptable Partners Yeah, but in real-world every woman is not willing to marry every man, and vice versa? n Some participants declare others as "unacceptable." (prefer to be alone than with given partner) n Algorithm extends to handle partial preference lists. Matching S unstable if there exists man m and woman w such that: n m is either unmatched in S, or strictly prefers w to his partner in S n w is either unmatched in S, or strictly prefers m to her partner in S. Fact 6. Men and women are each partitioned into two sets: n those that have partners in all stable matchings; n those that have partners in none.

28 28 Extensions: Sets of Unequal Size Also, there may be an unequal number of men and women. n E.g., |M| = 100 men, |W| = 90 women. n Algorithm extends. n WLOG, assume |W| < |M|. Matching S unstable if there exists man m and woman w such that: n m is either unmatched in S, or strictly prefers w to his partner in S; n w is either unmatched in S, or strictly prefers m to her partner in S. Fact 7. All women are matched in every stable matching. Men are partitioned into two subsets: n men who are matched in every stable matching; n men who are matched in none.

29 29 Extensions: Limited Polygamy What about limited polygamy? n E.g., Bill wants 3 women. n Algorithm extends. Matching S unstable if there exists man m and woman w such that: n either w is unmatched, or w strictly prefers m to her partner; n either m does not have all its "places" filled in the matching, or m strictly prefers w to at least one of its assigned residents.

30 30 Application: Matching Residents to Hospitals Sets of unequal size, unacceptable partners, limited polygamy. Matching S unstable if there exists hospital h and resident r such that: n h and r are acceptable to each other; n either r is unmatched, or r prefers h to her assigned hospital; n either h does not have all its places filled in the matching, or h prefers r to at least one of its assigned residents.

31 31 Application: Matching Residents to Hospitals Matching medical school residents to hospitals. (NRMP) n Hospitals ~ Men (limited polygamy allowed). n Residents ~ Women. n Original use just after WWII (predates computer usage). n Ides of March, 13,000+ residents. Rural hospital dilemma. n Certain hospitals (mainly in rural areas) were unpopular and declared unacceptable by many residents. n Rural hospitals were under-subscribed in NRMP matching. n How can we find stable matching that benefits "rural hospitals"? Rural Hospital Theorem:  Rural hospitals get exactly same residents in every stable matching!

32 32 Deceit: Machiavelli Meets Gale-Shapley Is there any incentive for a participant to misrepresent his/her preferences? n Assume you know men’s propose-and-reject algorithm will be run. n Assume that you know the preference lists of all other participants. Fact 8. No, for any man yes, for some women! A X X Y Y Z Z Men’s Preference ListWomen’s True Preference List Zeus Yancey Xavier Man1 st A B 2 nd C C 3 rd Clare Bertha Amy Woman1 st 2 nd 3 rd B A B C X Y Z X Z Y Y Z X Amy Lies Clare Bertha Amy Woman1 st 2 nd 3 rd X Y Z X Y Z X Y Z

33 33 Lessons Learned Powerful ideas learned in COS 423. n Isolate underlying structure of problem. n Create useful and efficient algorithms. n Sometimes deep social ramifications.  Historically, men propose to women. Why not vice versa?  Men: propose early and often.  Men: be more honest.  Women: ask out the guys.  Theory can be socially enriching and fun!  CS majors get the best partners!!!

34 Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Love, Marriage, and Lying: Extra Slides

35 35 Example With a Unique Stable Matching Red matching is unique stable matching. CY Z X X Y X Men’s Preference List Women’s Preference List Zeus Yancey Xavier Man1 st B B 2 nd C A 3 rd Clare Bertha Amy Woman1 st 2 nd 3 rd B C A A Z Y Z

36 36 How to Represent Men and Women Represent men and women as integers between 0 and N-1. n 0 through N-1 since C array indices start at 0. Could use struct if we want to carry around more information, e.g., name, age, astrological sign.

37 37 How to Represent Marriages Use array to keep track of marriages. int wife[N]; int husb[N]; for (m = 0; m < N; m++) wife[m] = -1; for (w = 0; w < N; w++) husb[w] = -1;

38 38 Filling in Some of the Code while (marriages < N) for (m = 0; wife[m] != -1; m++) ; while (-1 == wife[m]) let w be man m’s favorite women to whom he has not yet proposed if (-1 == husb[w]) husb[m] = w; wife[w] = m; marriages++; else if (w prefers m to current fiancé) m' = husb[w]; wife[m'] = -1; husb[m] = w; wife[w] = m; if (w unmatched) m and w get engaged while (m unmatched) m' = current fiancé of w m' now unmatched m and w get engaged find unmatched man

39 39 Representing the Preference Lists Use 2D-array to represent preference lists. n 2D-array is array of arrays. mp[m][i] = w if man m’s i th favorite woman is w. wp[w][i] = m if woman w’s i th favorite man is m. int mp[N][N]; int wp[N][N]; mp[1][0] = 3 man 1 likes woman 3 the best

40 40 Initializing the Preference Lists Could read from stdin. We’ll assign random lists for each man and woman. Use randomPermutation function from Lecture P2. n Need N random permutations for men, and N for women. int mp[N][N]; int wp[N][N]; for (m = 0; m < N; m++) randomPermutation(mp[m], N); for (w = 0; w < N; w++) randomPermutation(wp[w], N); mp[m] is man m’s preference list array.

41 41 Dumping Does woman w=2 prefer man m 1 =3 to man m 2 =0?  Yes, m 1 appears on woman w’s preference list before m 2. for (i = 0; i < N; i++) { if (wp[w][i] == m1) YES if (wp[w][i] == m2) NO } search preference list sequentially until m1 or m2 found

42 42 Keeping Track of Men’s Proposals Unmatched man proposes to most favorable woman to whom he hasn’t already proposed. How do we keep track of which woman a man has proposed to? n Men propose in decreasing order of preference. n Suffices to keep track of number of proposals in array. propose[m] = i if man m has proposed to i woman already. initialize array int props[N]; for (i = 0; i < N; i++) props[i] = 0;

43 43 Keeping Track of Men’s Proposals Unmatched man proposes to most favorable woman to whom he hasn’t already proposed. How do we keep track of which woman a man has proposed to? n Men propose in decreasing order of preference. n Suffices to keep track of number of proposals in array. propose[m] = i if man m has proposed to i woman already. while (-1 == wife[n]) { w = mp[m][props[m]]; props[m]++;... } props[m] is ranking of next woman on preference list make next proposal to woman mp[m][props[m]] find next woman to propose to

44 44 Try Out The Code #include #define N 500 int main(void) { int mp[N][N]; /* mp[m][i] = w if man m's ith favorite woman is w */ int wp[N][N]; /* wp[w][i] = m if woman w's ith favorite man is m */ int wife[N]; /* wife[m] = w if m married to w */ int husb[N]; /* husb[w] = m if m married to w */ int props[N]; /* props[m] = i if man m has proposed to i women */ int marriages = 0; /* number of couples matched so far */ int m, w; /* initialize men */ for (m = 0; m < N; m++) { props[m] = 0; wife[m] = -1; randomPermutation(mp[m], N); } /* initialize women */ for (w = 0; w < N; w++) { husb[w] = -1; randomPermutation(wp[w], N); } marriage.c

45 45 Try Out The Code while (marriages < N) { /* find first unmatched man */ for (m = 0; wife[m] != -1; m++) ; printf("man %d proposing:\n", m); /* propose to next women on list until successful */ while (-1 == wife[m]) { w = mp[m][props[m]]; printf(" to woman %d", w); props[m]++; /* woman w unmatched */ if (-1 == husb[w]) { printf(" accepted\t(woman %d previously unmatched)\n", w); husb[w] = m; wife[m] = w; marriages++; } /* woman w prefers m to current mate */ else if (wr[w][m] < wr[w][husb[w]]) { printf(" accepted\t(woman %d dumps man %d)\n", w, husb[w]); wife[husb[w]] = -1; husb[w] = m; wife[m] = w; } /* otherwise m rejected by w */ else printf(" rejected\t(woman %d prefers %d)\n", w, husb[w]); } marriage.c

46 46 Try Out The Code Observation: code is REALLY slow for large N. printf("Stable matching\n"); for (m = 0; m < N; m++) printf("%5d %5d\n", m, wife[m]); return 0; } marriage.c % gcc marriage.c % a.out man 0 proposing: to woman 4 accepted (woman 4 previously unmatched) man 1 proposing: to woman 0 accepted (woman 0 previously unmatched) man 2 proposing: to woman 2 accepted (woman 2 previously unmatched) man 3 proposing: to woman 2 rejected (woman 2 prefers 2) to woman 3 accepted (woman 3 previously unmatched) man 4 proposing: to woman 2 accepted (woman 2 dumps man 2) man 2 proposing: to woman 3 accepted (woman 3 dumps man 3) man 3 proposing: to woman 0 rejected (woman 0 prefers 1) to woman 4 rejected (woman 4 prefers 0) to woman 1 accepted (woman 1 previously unmatched) Stable matching 0 4 1 0 2 3 3 1 4 2 Unix

47 47 Why So Slow? Does woman w=2 prefer man m 1 =3 to man m 2 =0?  Need to search through row 2 to find answer. This is repeated many times. If N is large, this can be very expensive.

48 48 mp[1][0] = 3 man 1 likes woman 3 best mr[1][3] = 0 man 1 likes woman 3 best An Auxiliary Data Structure Create a 2D array that stores men’s ranking of women. n mr[m][w] = i if man m’s ranking of woman w is i. n wr[w][m] = i if woman w’s ranking of man m is i.

49 49 mr[m][w1] = 2 mr[m][w2] = 4 if (mr[m][w1] < mr[m][w2]) YES else NO An Auxiliary Data Structure Create a 2D array that stores men’s ranking of women. n mr[m][w] = i if man m’s ranking of woman w is i. n wr[w][m] = i if woman w’s ranking of man m is i. Does man m = 3 prefer woman w 1 = 2 to woman w 2 = 4?

50 50 Check if Marriage is Stable Check if husb[N] and wife[N] correspond to a stable marriage. n Good warmup and useful for debugging. n Check every man-woman pair to see if they’re unstable. n Use ranking arrays. int isStable(int husb[], int wife[], int mr[N][N], int wr[N][N]) { int m, w; for (m = 0; m < N; m++) for (w = 0; w < N; w++) if (mr[m][w] < mr[m][wife[m]]) && (wr[w][m] < wr[w][husb[w]]) return 0; return 1; } isStable m prefers w to current wife w prefers m to current husband

51 51 Check if Marriage is Stable Check if husb[N] and wife[N] correspond to a stable marriage. n Good warmup and useful for debugging. n Check every man-woman pair to see if they’re unstable. n Use ranking arrays. Time/space tradeoff for using auxiliary ranking arrays. n Disadvantage: requires twice as much memory (storage). n Advantage: dramatic speedup in running time (using 400 MHz Pentium II with N = 10,000).  1 second using ranking arrays.  2 hours by searching preference list sequentially!


Download ppt "Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne COS 423: Theory of Algorithms Princeton University Spring, 2001 Kevin Wayne."

Similar presentations


Ads by Google