Stable marriages Competitive programming and problem solving Yoram Meijaard
Today The stable marriage problem Problem statement Solution Proof Running time Reenactment Implementation Adaptations Applications Practice
Problem statement Imagine we have a village With people
Problem statement 100 boys 100 girls
Problem statement We are a matchmaker We would like to arrange 100 marriages Is this possible? Are they stable?
Solution - Classic Human inefficiency Solution? Computer science!
Problem statement (finally)
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
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
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
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.
Proof – Part 2: every marriage is stable
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
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)
Reenactment
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
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)); }
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; }}}}
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
Applications Server routing E-matching? Economics game theory
Practice && Misc Numberphile on stable marriages My “prerecording”, in case you want me to explain it again
Questions?