Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stable marriages Competitive programming and problem solving Yoram Meijaard.

Similar presentations


Presentation on theme: "Stable marriages Competitive programming and problem solving Yoram Meijaard."— Presentation transcript:

1 Stable marriages Competitive programming and problem solving Yoram Meijaard

2 Today The stable marriage problem  Problem statement  Solution  Proof  Running time  Reenactment  Implementation  Adaptations  Applications  Practice

3 Problem statement Imagine we have a village With people

4 Problem statement 100 boys 100 girls

5 Problem statement We are a matchmaker We would like to arrange 100 marriages Is this possible? Are they stable?

6 Solution - Classic Human inefficiency Solution? Computer science!

7 Problem statement (finally)

8 Solution 1.Every boy ranks the girls in a strict order of preference 2.Every girl ranks the boys in a strict order of preference 3.Repeat, until everyone is engaged: a)Every unengaged boy proposes to highest ranked girl that he hasn't proposed to on his list b)All girls accept the highest ranked boy who has proposed to her All marriages are stable All people are married

9 Example ABC XZZ YYX ZXY XYZ BCB CAC ABA RoundProposalsXYZ 1A-X, B-Z,C-ZAB 2C-XCB 3A-YCAB A proposes to X B proposes to Z C proposes to Z X accepts A Z accepts B C proposes to X X accepts C (A now single) A proposes to Y Y accepts A

10 Algorithm 1.Every boy ranks the girls in a strict order of preference 2.Every girl ranks the boys in a strict order of preference 3.Repeat, until everyone is engaged: a)Every unengaged boy proposes to highest ranked girl that he hasn't proposed to on his list b)All girls accept the highest ranked boy who has proposed to her

11 Proof – Part 1: does everyone get married? Property : #engaged boys = #engaged girls Property : if a girl is proposed to, she will be engaged for the rest of the algorithm. Proof by contradiction: Assume the loop ends Assume that there exist a boy A and girl B that are not engaged Then no one has proposed to B Thus A has not proposed to B Thus the loop cannot have ended.

12 Proof – Part 2: every marriage is stable

13 Algorithm 1.Every boy ranks the girls in a strict order of preference 2.Every girl ranks the boys in a strict order of preference 3.Repeat, until everyone is engaged: a)Every unengaged boy proposes to highest ranked girl that he hasn't proposed to on his list b)All girls accept the highest ranked boy who has proposed to her

14 Runtime Runtime equivalent to the amount of proposals made How many proposals are possible? All boys x all girls = n^2 Every round the supply of proposals drops by at least one Runtime= O(n^2)

15 Reenactment

16 Algorithm 1.Every boy ranks the girls in a strict order of preference 2.Every girl ranks the boys in a strict order of preference 3.Repeat, until everyone is engaged: a)Every unengaged boy proposes to highest ranked girl that he hasn't proposed to on his list b)All girls accept the highest ranked boy who has proposed to her

17 Implementation Input: Idea: represent all boys and girls as index int[] boy_engagedTo = new int[n]; // status of boys, filled with -1’s int[] girl_engagedTo = new int[n]; // status of girls, filled with -1’s int[] proposals_made = new int[n]; // amount of proposals made int[][] boy_pref = new int[n][n]; // filled with preferences of boys int[][] girl_pref = new int[n][n]; // filled with preferences of girls Queue q = new LinkedList<>();// available boys for(int I = 0; i < n; i++){// add all boys to queue q.add(i); } Output: for(int i = 0; i < n;i++){// output all boys and their matches System.out.println((i+1) + " " + (boy_engagedTo[i] + 1)); }

18 Implementation while(!q.isEmpty()){ int boy = q.poll();// get an unengaged guy int girl = boy_pref[boy][proposals_made[boy]++];// girl he wants to propose to if(girl_engagedTo[girl] == -1){// she is unengaged girl_engagedTo[girl] = boy; boy_engagedTo[boy] = girl; } else {// she is engaged for(int i=0;i<n;i++){// find out if boy is more preferred int b = girl_pref[girl][i]; if(b==boy){ boy_engagedTo[girl] =-1; //unengage the guy q.add(girl_engagedTo[girl]); // add him to the list of sad losers girl_engagedTo[girl] = boy; boy_engagedTo[boy] = girl; break; } else if(b==girl_engagedTo[girl]) { q.add(boy); break; }}}}

19 Adaptations College admission problem Matches x Colleges with q x spots to n Students Extreme case: x = n  stable marriage problem! In general: take students as Boys, take colleges spots as Girls Run stable marriage

20 Applications Server routing E-matching? Economics  game theory

21 Practice && Misc https://www.codechef.com/problems/STABLEMP Numberphile on stable marriages https://www.youtube.com/watch?v=Qcv1IqHWAzg https://www.youtube.com/watch?v=LtTV6rIxhdo My “prerecording”, in case you want me to explain it again https://www.youtube.com/watch?v=5zr-N369kuU

22 Questions?


Download ppt "Stable marriages Competitive programming and problem solving Yoram Meijaard."

Similar presentations


Ads by Google