fall 2016 comp-251 algorithms and data...

76
Fall 2016 COMP-251 Algorithms and Data Structures Prof. Claude Crépeau

Upload: dotruc

Post on 08-May-2018

238 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Fall 2016 COMP-251 Algorithms and Data Structures

Prof. Claude Crépeau

Page 2: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

ME

• Prof. Claude Crépeau

• McConnell 110N

• (514) 398-4716

[email protected]

• Office Hours:Tuesdays 10:30-13:30,

• For all class matters please use:[email protected]

Page 3: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

TAs

• Hossein Aboutalebi ([email protected])

• Omar Asad ([email protected])

• Yi Chang ([email protected])

• Yixin Chen ([email protected])

• Tianyu Li ([email protected])

Page 4: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

YOU

U-1

U-2

U-3

Page 5: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

YOU

• 63 Bachelor of Science

• 46 Bachelor of Software Engr

• 34 Bachelor of Engr

• 25 Bachelor of Arts

• 6 Bachelor of Arts & Science

• 4 Bachelor of Commerce

• 4 Bachelor of Music

• 1 B SC Agricu and Envir Sc

• 3 No degree

Page 6: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

USU-1

U-2

U-3

Page 7: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

• Description: (3 credits; 3 hours) Complexity of algorithms. Introduction to algorithm design and analysis. Graph algorithms, greedy algorithms, data structures, dynamic programming, maximum flows.

• Prerequisite: either COMP 250 or COMP 203.+ MATH 240 (Discrete Structures 1) is strongly recommended.

• Restrictions: Not open to students who have taken or are taking COMP-252.

• May not be enrolled in one of the following Levels/Faculties: Doctorate / Masters & Grad Dips & Certs       Faculty of Medicine / Dentistry      Centre for Continuing Education

COMP-251

Page 8: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

COMP-251

Page 9: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Your final grade will be calculated as follows:

• 40% for 4 assignments (10% each)

• 10% for the midterm

• 50% for the final exam

• The exams are open book, no electronic device will be allowed (this includes noise-cancelling headphones).

COMP-251 : EVALUATION

Page 10: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Policy on collaborations

• We greatly encourage you to discuss the assignment problems with each other.

• However, these discussions should not go so far that you are sharing code or giving away the answer.

• A rule of thumb is that your discussions should be considered public in the sense that anything you share with a friend should be sharable with any student in the class.

• We ask you to indicate on your assignments the names of the persons with whom you collaborated or discussed your assignments (including the TA’s and instructor).

COMP-251 : Collaborations

Page 11: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Policy on discussion Board

• We will use the forum system available at: https://www.facebook.com/groups/comp251/(i.e. we will not use the forum in MyCourse).

• You are invited to register to this forum.

• Please follow common sense rules and etiquette for discussion board postings: be polite, avoid texting shorthand ("ur" instead of "you are", ...), choose a suitable subject line for your posting and use multiple postings for multiple subjects, keep your postings brief, etc.

COMP-251 : Discussion

Page 12: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Policy on re-grading

• If you wish us to re-grade a question on an exam (or assignment), we will do so. However, to avoid grade ratcheting, we reserve us the right to re-grade other questions on as well.

Policy on final grades

• We will use the same rules and formula for calculating the final grade for everyone. We understand that your performances may be influenced by many factors, possibly out of your control. However, that is the only way we can be fair. The only exceptions will be medical exceptions. In that case, I will require a medical note, which has to be also reported to McGill, and to be informed as early as possible. Failure to comply to these rules, may results in the impossibility to invoke a medical exception.

COMP-251 : Grades

Page 13: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Policy on Assignments

• Due date/time, location/mode for returning your solutions, and accepted formats will be announced in class and indicated on the course web page.

• Failure to return your assignment in time will results in penalties or even absence of grading. Late submission of 24h or less will receive a penalty of 20%. In all other cases, your assignment will be refused and not graded.

• Importantly, solutions that do not follow the requested format will receive a penalty. By default, we only accept PDF or TEXT files. Images (if any) must be embedded in a PDF. Do not compress your files. All files must open on LINUX SOCS workstations.

• The quality of the presentation of your solution is very important. Unreadable material, cryptic notations, or bad organization of the material will results in penalties, and potentially even an absence of grading. If you scan your hand-written solutions, it is your responsibility to ensure that you submit a high-quality image (i.e. excellent luminosity, contrast, focus and resolution). The clarity of your explanations will also be an integral part of your final grade.

COMP-251 : Assignments

Page 14: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

McGill Policies

• In accordance with McGill University’s Charter of Students’ Rights, students in this course have the right to submit in French as well as in English any written work that is to be graded.

• McGill University values academic integrity. Therefore, all students must understand the meaning and consequences of cheating, plagiarism and other academic offenses under the Code of Student Conduct and Disciplinary Procedures.See this link: http://www.mcgill.ca/students/srr/

COMP-251 : @McGill

Page 15: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Fall 2016 COMP-251 Algorithms and Data Structures

Prof. Claude Crépeau

Page 16: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Chapter 1 Introduction:Some Representative Problems

Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.

Page 17: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

1.1 A First Problem: Stable Matching

Page 18: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

18

Matching Residents to Hospitals

Goal. Given a set of preferences among hospitals and medical students,design a self-reinforcing admissions process.

Unstable pair: applicant x and hospital y are unstable if:■ x prefers y to its assigned hospital.■ y prefers x to one of its admitted students.

Stable assignment. Assignment with no unstable pairs.■ Natural and desirable condition.■ Individual self-interest will prevent any applicant/hospital deal from

being made.

Page 19: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

19

Stable Matching Problem

Goal. Given n men and n women, find a "suitable" matching. ■ Participants rate members of opposite sex. ■ Each man lists women in order of preference from best to worst. ■ Each woman lists men in order of preference from best to worst.

Zeus Amy ClareBertha

Yancey Bertha ClareAmy

Xavier Amy ClareBertha

1st 2nd 3rd

Men’s Preference Profile

favorite least favorite

Clare Xavier ZeusYancey

Bertha Xavier ZeusYancey

Amy Yancey ZeusXavier

1st 2nd 3rd

Women’s Preference Profile

favorite least favorite

Page 20: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

20

Stable Matching Problem

Perfect matching: everyone is matched monogamously.■ Each man gets exactly one woman.■ Each woman gets exactly one man.

Stability: no incentive for some pair of participants to undermineassignment by joint action.■ In matching M, an unmatched pair m-w is unstable if man m and

woman w prefer each other to their current partner.■ Unstable pair m-w could each improve by eloping.

Stable matching: perfect matching with no unstable pairs.

Stable matching problem. Given the preference lists of n men andn women, find a stable matching if one exists.

Page 21: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

21

Stable Matching Problem

Q. Is assignment X-C, Y-B, Z-A stable?

Zeus Amy ClareBertha

Yancey Bertha ClareAmy

Xavier Amy ClareBertha

1st 2nd 3rd

Men’s Preference Profile

Clare Xavier ZeusYancey

Bertha Xavier ZeusYancey

Amy Yancey ZeusXavier

1st 2nd 3rd

Women’s Preference Profile

favorite favoriteleast favorite least favorite

Page 22: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

22

Stable Matching Problem

Q. Is assignment X-C, Y-B, Z-A stable? A. No. Bertha and Xavier will hook up.

Zeus Amy ClareBertha

Yancey Bertha ClareAmy

Xavier Amy ClareBertha

Clare Xavier ZeusYancey

Bertha Xavier ZeusYancey

Amy Yancey ZeusXavier

1st 2nd 3rd 1st 2nd 3rd

favorite favorite

Men’s Preference Profile Women’s Preference Profile

least favorite least favorite

Page 23: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

23

Stable Matching Problem

Q. Is assignment X-A, Y-B, Z-C stable? A. Yes.

Zeus Amy ClareBertha

Yancey Bertha ClareAmy

Xavier Amy ClareBertha

Clare Xavier ZeusYancey

Bertha Xavier ZeusYancey

Amy Yancey ZeusXavier

1st 2nd 3rd 1st 2nd 3rd

favorite favorite

Men’s Preference Profile Women’s Preference Profile

least favorite least favorite

Page 24: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

24

Stable Roommate Problem

Q. Do stable matchings always exist? A. Not obvious a priori.

Stable roommate problem. ■ 2n people; each person ranks others from 1 to 2n-1. ■ Assign roommate pairs so that no unstable pairs.

Observation. Stable matchings do not always exist for stable roommate problem.

BBob

Chris

Adam C

A

B

D

DDoofus A B C

D

C

A

1st 2nd 3rd

A-B, C-D ⇒ B-C unstable

A-C, B-D ⇒ A-B unstable

A-D, B-C ⇒ A-C unstable

Page 25: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Propose-and-reject algorithm. [Gale-Shapley 1962] Intuitive method that guarantees to find a stable matching.

25

Propose-And-Reject Algorithm

Initialize each person to be free. while (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 26: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Initialize each person to be free. while (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 27: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Victor proposes to Bertha.

Initialize each person to be free. while (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 28: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Victor proposes to Bertha.

Victor

Bertha

Initialize each person to be free. while (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 29: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Victor proposes to Bertha. - Bertha accepts since previously unmatched.

Victor

Bertha

Initialize each person to be free. while (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 30: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Wyatt proposes to Diane.

Victor

Bertha

Initialize each person to be free. while (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 31: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Wyatt proposes to Diane.

Victor

BerthaDiane

Wyatt

Initialize each person to be free. while (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 32: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Wyatt proposes to Diane. - Diane accepts since previously unmatched.

Victor

BerthaDiane

Wyatt

Initialize each person to be free. while (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 33: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Xavier proposes to Bertha.

Victor

BerthaDiane

Wyatt

Initialize each person to be free. while (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 34: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Xavier proposes to Bertha.

Victor

BerthaDiane

Wyatt

Bertha

Xavier

Initialize each person to be free. while (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 35: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Xavier proposes to Bertha. - Bertha dumps Victor and accepts Xavier.

Victor

BerthaDiane

Wyatt

Bertha

Xavier

Initialize each person to be free. while (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 36: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Diane

Wyatt

Bertha

Xavier

Victor proposes to Amy.

Victor

Bertha

Initialize each person to be free. while (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 37: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Diane

Wyatt

Bertha

Xavier

Victor proposes to Amy.

Amy

VictorVictor

Bertha

Initialize each person to be free. while (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 38: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Diane

Wyatt

Bertha

Xavier

Amy

Victor

Victor proposes to Amy. - Amy accepts since previously unmatched.

Victor

Bertha

Initialize each person to be free. while (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 39: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Diane

Wyatt

Bertha

Xavier

Amy

Victor

Yancey proposes to Amy.

Victor

Bertha

Initialize each person to be free. while (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 40: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Diane

Wyatt

Bertha

Xavier

Amy

Victor

Yancey proposes to Amy.

Amy

YanceyVictor

Bertha

Initialize each person to be free. while (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 41: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Diane

Wyatt

Bertha

Xavier

Amy

Victor

Amy

Yancey

Yancey proposes to Amy. - Amy rejects since she prefers Victor.

Victor

Bertha

Initialize each person to be free. while (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 42: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Diane

Wyatt

Bertha

Xavier

Amy

Victor

Yancey proposes to Diane.

Amy

YanceyVictor

Bertha

Initialize each person to be free. while (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 43: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Diane

Wyatt

Bertha

Xavier

Amy

Victor

Yancey proposes to Diane.

Diane

Yancey

Amy

YanceyVictor

Bertha

Initialize each person to be free. while (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 44: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Diane

Wyatt

Bertha

Xavier

Amy

Victor

Diane

Yancey

Yancey proposes to Diane. - Diane dumps Wyatt and accepts Yancey.

Amy

YanceyVictor

Bertha

Initialize each person to be free. while (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 45: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Diane

Yancey

Wyatt proposes to Bertha.

Diane

Wyatt

Amy

YanceyVictor

Bertha

Initialize each person to be free. while (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 46: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Diane

Yancey

Wyatt proposes to Bertha.

Bertha

Wyatt

Diane

Wyatt

Amy

YanceyVictor

Bertha

Initialize each person to be free. while (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 47: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Victor

BerthaDiane

Wyatt

Bertha

Xavier

Amy

Victor

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Wyatt proposes to Bertha. - Bertha rejects since she prefers Xavier.

Initialize each person to be free. while (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 48: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Diane

Yancey

Wyatt proposes to Amy.

Victor

BerthaDiane

Wyatt

Amy

Yancey

Bertha

Wyatt

Initialize each person to be free. while (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 49: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Diane

Yancey

Wyatt proposes to Amy.

Amy

WyattVictor

BerthaDiane

Wyatt

Amy

Yancey

Bertha

Wyatt

Initialize each person to be free. while (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 50: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Diane

Yancey

Wyatt proposes to Amy. - Amy rejects since she prefers Victor.

Victor

BerthaDiane

Wyatt

Amy

Yancey

Bertha

Wyatt

Amy

Wyatt

Initialize each person to be free. while (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 51: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Victor

BerthaDiane

Wyatt

Bertha

Xavier

Amy

Victor

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Amy

Wyatt

Wyatt proposes to Clare.

Initialize each person to be free. while (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 52: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Victor

BerthaDiane

Wyatt

Bertha

Xavier

Amy

Victor

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Amy

Wyatt

Wyatt proposes to Clare.

Clare

Wyatt

Initialize each person to be free. while (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 53: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Diane

Yancey

Clare

Wyatt

Wyatt proposes to Clare. - Clare accepts since previously unmatched.

Victor

BerthaDiane

Wyatt

Amy

Yancey

Bertha

Wyatt

Amy

Wyatt

Initialize each person to be free. while (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 54: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Diane

Yancey

Clare

Wyatt

Zeus proposes to Bertha.

Victor

BerthaDiane

Wyatt

Amy

Yancey

Bertha

Wyatt

Amy

Wyatt

Initialize each person to be free. while (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 55: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Diane

Yancey

Clare

Wyatt

Zeus proposes to Bertha.

Bertha

ZeusVictor

BerthaDiane

Wyatt

Amy

Yancey

Bertha

Wyatt

Amy

Wyatt

Initialize each person to be free. while (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 56: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Diane

Yancey

Clare

Wyatt

Zeus proposes to Bertha. - Bertha rejects since she prefers Xavier.

Victor

BerthaDiane

Wyatt

Amy

Yancey

Bertha

Wyatt

Amy

Wyatt

Bertha

Initialize each person to be free. while (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 57: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Diane

Yancey

Clare

Wyatt

Zeus proposes to Diane.

Victor

BerthaDiane

Wyatt

Amy

Yancey

Bertha

Wyatt

Amy

Wyatt

Bertha

Initialize each person to be free. while (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 58: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Diane

Yancey

Clare

Wyatt

Zeus proposes to Diane.

Diane

Zeus

Victor

BerthaDiane

Wyatt

Amy

Yancey

Bertha

Wyatt

Amy

Wyatt

Bertha

Initialize each person to be free. while (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 59: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Victor

BerthaDiane

Wyatt

Bertha

Xavier

Amy

Victor

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Amy

Wyatt

Clare

Wyatt

Bertha

Zeus

Diane

Zeus

Zeus proposes to Diane. - Diane dumps Yancey and accepts Zeus.

Initialize each person to be free. while (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 60: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Clare

Wyatt

Diane

Zeus

Yancey proposes to Clare.

Victor

BerthaDiane

Wyatt

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Amy

Wyatt

Bertha

Zeus

Initialize each person to be free. while (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 61: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Clare

Wyatt

Diane

Zeus

Yancey proposes to Clare.

Clare

YanceyVictor

BerthaDiane

Wyatt

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Amy

Wyatt

Bertha

Zeus

Initialize each person to be free. while (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 62: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Victor

BerthaDiane

Wyatt

Bertha

Xavier

Amy

Victor

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Amy

Wyatt

Clare

Wyatt

Bertha

Zeus

Diane

Zeus

Clare

Yancey

Yancey proposes to Clare. - Clare rejects since she prefers Wyatt.

Initialize each person to be free. while (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 63: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Clare

Wyatt

Diane

Zeus

Yancey proposes to Bertha.

Victor

BerthaDiane

Wyatt

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Amy

Wyatt

Bertha

Zeus

Clare

Yancey

Initialize each person to be free. while (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 64: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Clare

Wyatt

Diane

Zeus

Yancey proposes to Bertha.

Bertha

Yancey Victor

BerthaDiane

Wyatt

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Amy

Wyatt

Bertha

Zeus

Clare

Yancey

Initialize each person to be free. while (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 65: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Victor

BerthaDiane

Wyatt

Bertha

Xavier

Amy

Victor

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Amy

Wyatt

Clare

Wyatt

Bertha

Zeus

Diane

Zeus

Clare

Yancey

Bertha

Yancey

Yancey proposes to Bertha. - Bertha rejects since she prefers Xavier.

Initialize each person to be free. while (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 66: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Clare

Wyatt

Diane

Zeus

Yancey proposes to Erika.

Victor

BerthaDiane

Wyatt

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Amy

Wyatt

Bertha

Zeus

Clare

Yancey

Bertha

Yancey

Initialize each person to be free. while (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 67: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Clare

Wyatt

Diane

Zeus

Yancey proposes to Erika.

Erika

Yancey

Victor

BerthaDiane

Wyatt

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Amy

Wyatt

Bertha

Zeus

Clare

Yancey

Bertha

Yancey

Initialize each person to be free. while (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 68: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Clare

Wyatt

Diane

Zeus

Erika

Yancey

Yancey proposes to Erika. - Erika accepts since previously unmatched.

Victor

BerthaDiane

Wyatt

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Amy

Wyatt

Bertha

Zeus

Clare

Yancey

Bertha

Yancey

Initialize each person to be free. while (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 69: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Zeus Bertha AmyDiane Erika ClareYancey Amy ClareDiane Bertha ErikaXavier Bertha ClareErika Diane AmyWyatt Diane AmyBertha Clare ErikaVictor Bertha DianeAmy Erika Clare

0th 1st 2nd 3rd 4th

Men’s Preference Profile

Erika Yancey ZeusWyatt Xavier VictorDiane Victor YanceyZeus Xavier WyattClare Wyatt YanceyXavier Zeus Victor

Bertha Xavier YanceyWyatt Victor ZeusAmy Zeus WyattVictor Yancey Xavier

0th 1st 2nd 3rd 4th

Women’s Preference Profile

Bertha

Xavier

Amy

Victor

Clare

Wyatt

Diane

Zeus

Erika

Yancey

STOP - Everyone matched. - Stable matching!

Victor

BerthaDiane

Wyatt

Amy

Yancey

Diane

Yancey

Bertha

Wyatt

Amy

Wyatt

Bertha

Zeus

Clare

Yancey

Bertha

Yancey

Page 70: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

56

Proof of Correctness: Termination

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."

Claim. Algorithm terminates after at most n2 iterations of while loop.Pf. Each time through the while loop a man proposes to a new woman.There are only n2 possible proposals. ▪

Wyatt

Victor

1st

A

B

2nd

C

D

3rd

C

B

AZeus

Yancey

Xavier C

D

A

B

B

A

D

C

4th

E

E

5th

A

D

E

E

D

C

B

E

Bertha

Amy

1st

W

X

2nd

Y

Z

3rd

Y

X

VErika

Diane

Clare Y

Z

V

W

W

V

Z

X

4th

V

W

5th

V

Z

X

Y

Y

X

W

Z

n(n-1) + 1 proposals required

Page 71: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

57

Proof of Correctness: Perfection

Claim. All men and women get matched.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, since he ends up unmatched. ▪

Page 72: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

58

Proof of Correctness: Stability

Claim. No unstable pairs. Pf. (by contradiction) ■ Suppose A-Z is an unstable pair: each prefers each other to

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-Zeus

Amy-Yancey

S*

. . .

men propose in decreasingorder of preference

women only trade up

Page 73: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

59

Summary

Stable matching problem. Given n men and n women, and theirpreferences, find a stable matching if one exists.

Gale-Shapley algorithm. Guarantees to find a stable matching for anyproblem instance.

Q. How to implement GS algorithm efficiently?

Q. If there are multiple stable matchings, which one does GS find?

Page 74: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

60

Efficient Implementation

Efficient implementation. We describe O(n2) time implementation.

Representing men and women.■ Assume men are named 1, …, n.■ Assume women are named 1', …, n'.

Engagements.■ Maintain a list of free men, e.g., in a queue.■ Maintain two arrays wife[m], and husband[w].

– set entry to 0 if unmatched– if m matched to w then wife[m]=w and husband[w]=m

Men proposing.■ For each man, maintain a list of women, ordered by preference.■ Maintain an array count[m] that counts the number of proposals

made by man m.

Page 75: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

61

Efficient Implementation

Women rejecting/accepting. ■ Does woman w prefer man m to man m'? ■ For each woman, create inverse of preference list of men. ■ Constant time access for each query after O(n) preprocessing.

for i = 1 to n inverse[pref[i]] = i

Pref

1st

8

2nd

7

3rd

3

4th

4

5th

1 5 26

6th 7th 8th

Inverse 4th 2nd8th 6th5th 7th 1st3rd

1 2 3 4 5 6 7 8

Amy

Amy

Amy prefers man 3 to 6 since inverse[3] < inverse[6]

2 7

Page 76: Fall 2016 COMP-251 Algorithms and Data Structurescrypto.cs.mcgill.ca/~crepeau/COMP251/01stable-matching-2016.pdf•Description: (3 credits; 3 hours) Complexity of algorithms. Introduction

Chapter 1 Introduction:Some Representative Problems

Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.