What is an algorithm?
- An algorithm is a clear step-by-step procedure to solve a given problem (by computers).
- Each step must be computer-implementable.
- Must be clear enough that anyone reading it cannot misunderstand any of the steps.
- After figuring out an algorithm for a problem, we can then implement it as a computer program.
The importance of proofs for this course
- As you will soon see, many algorithms do not make sense at first glance yet they are actually correct.
- For such algorithms, studying the proofs of their correctness is the only way to understand why they are correct.
Other benefits of studying proofs
-
You will unlikely to misremember and/or misuse the algorithms.
-
You will know whether the same algorithm can be used for other similar problem.
-
Sometimes, just a few slight tweaks to the proofs you study can result in a modified algorithm for a new problem you encounter!
-
You will be able to distinguish between proper and flawed arguments. Useful for real life.
-
You will likely find proofs to be difficult to understand in the beginning as they are new and different to you.
-
Unfortunately, they are crucial for this course (and only a few other courses in your curriculum, i.e., CSS321, CSS322).
About the exams in this course
- Because proofs are difficult, there will not be any proof question in the midterm exam, and there will be at most one light “kind-of proof” question in the final exam.
- That is, you can easily get A for this course even if you cannot answer the proof question (as long as you can do the other non-proof ones better than your classmates).
- Caveat: For ambitious students who want to study Master/PhD in top universities or work in top companies abroad, they do expect you to be comfortable at understanding proofs and proving new theorems.
Intro problem: Stable Matching
- There are n men and n women.
- We want to arrange marriages between them.
- n marriages. So everyone is married.
- But, suppose Somchai prefers Somsri to his wife and Somsri also prefers Somchai to her husband...
- Somchai and Somsri may both leave their respective marriages and run off together.
- I.e., ==Somchai’s and Somsri’s marriages are unstable.==
- So we want to arrange marriages such that all marriages are stable.
- The problem also applies to matching employees to employers, subordinates to bosses, students to universities, residents to hospitals, and so on.
Stable Matching: Problem Formulation
- Let be a set on men.
- Let be a set on women.
- A matching S is a set of ordered pairs where each member of and each member of appears in at most one pair in S.
- A perfect matching S’ is a matching with the property that each member of and each member of appears in exactly one pair in S’.
- I.e., everyone is married and no one is married to two or more people.
Preferences
- Each man ranks all the women according to his preference.
- Call this ranking his “preference list”.
- prefers to if ranks higher than . → like more than (different gir)
- Ties in the ranking are not allowed.
- Each woman ranks all the men in the same way.
Instability
- There are two pairs and in S where prefers to and prefers to
- So and may abandon their current partners and head off together.
- Such a pair is said to be an instability with respect to S.
Goal
- A matching S is stable if
- It is perfect, and → (No one is single)
- There is no instability with respect to S.
Question
Does there exist a stable matching for every set of preference lists?
Question
Given a set of preference lists, can we efficiently construct a stable matching if there is one?
Example 1
- Two men: = {,}. Two women: = {,}.
- prefers to .
- prefers to .
- prefers to .
- prefers to .
- Any stable matching in this example? If so, what is it? Is it unique?

Designing the Algorithm
-
Initially, everyone is unmarried.
-
Suppose an unmarried man chooses the woman who ranks highest on his preference list and proposes to her.
-
Question: Should accept and marry ?
-
Will be one of the pairs in a stable matching?
- Not necessarily. We cannot be sure at this point.
- Another man whom prefers to may propose to her in the future. If is already married to , this may lead to instability.
-
On the other hand, should reject right away?
- No, because it is possible that no other men whom prefers to will propose to her in the future.
If that woman is free, she just accept the proposal
-
So let’s have engaged to .
-
Let’s keep doing this.
-
Suppose, at the current state, some men and women are free (not engaged).
-
An arbitrary free man chooses the highest-ranked woman to whom he has not yet proposed, and he proposes to her.
-
If is also free, have and engaged.
-
But what if is already engaged to some other man ?
-
If she prefers to , she keeps her current engagement to and reject .
-
Otherwise, then breaks off her engagement with and becomes engaged to instead.
-
Repeat until no one is free.
-
At this point, have every woman marry her current fiancé.
-
This algorithm is known as the Gale-Shapley (G-S) algorithm.
- Proved in 1962 by David Gale & Lloyd Shapley
- Well recognized in 1980 from Alvin Roth’s work
- Roth and Shapley won Nobel prize in 2012.
Analyzing the algorithm
- Some part of the step of the algorithm is called lemma.
Lemma (1.1)
A woman w remains engaged from the point at which she receives her first proposal; and the sequence of partners to which she is engaged gets better and better (in terms of her preference list).
- Proof:
- When a woman receives her first proposal, she accepts it and becomes engaged right away (because she is free).
- The only way for a woman to break off her engagement is to switch to be engaged to another man whom she prefers.
Lemma (1.2)
The sequence of women to whom a man m proposes get worse and worse (in terms of his preference list).
- Proof:
- Because a man starts by proposing the woman highest on his list and works his way lower his preference list.
Theorem (1.3)
The G-S algorithm terminates after at most iterations of the While loop.
- Proof:
- Each man proposes at most n times because he never proposes to the same woman more than once.
- There are n men. So at most _n_2 proposals before the algorithm terminates.
Lemma (1.4)
If m is free at some point in the execution of the algorithm, then there is a woman to whom he has not yet proposed.
- Proof:
- Let’s prove by contradiction.
- Suppose there comes a point where is free but has already proposed to every woman.
- By (1.1), each of the n women is engaged at this point in time.
- All n women are engaged means all n men are engaged.
- This contradicts the assumption that is free.
Lemma (1.5)
The set S returned at termination is a perfect matching.
- Proof:
- First, the set of engaged pairs always forms a matching.
- By (1.4), if is free, there must be at least one woman to whom he has not yet proposed.
- Therefore, the algorithm does not leave the While loop and continues.
- This means that when the algorithm does terminate, no men are free.
- Since engagement is one-to-one, no women are free either (same number of men and women).
- Therefore, the returned S is a perfect matching.
Theorem (1.6)
The G-S algorithm returns a stable matching.
- Proof:
- We already know from (1.5) that S is a perfect matching.
- To prove that S is a stable matching, let’s assume there is an instability with respect to S and obtain a contradiction.
- Assume there is an instability involves two pairs (,)and (0_,_0) in S with the property that
- prefers to , and
- prefers to .
- Since is married to , it means’s last proposal was to .
- Question: Did propose to 0at some earlier point in this execution?
- Since m prefers _w_0to w (our assumed instability), he did (as he
proposes in order of his preference).
Since m does not end up married to _w_0,itmeansthat_w_0must have
rejected m (either right away or later on) in favor of some other man
_m_00, whom _w_0prefers to m.
_m_0is the final partner of _w_0,soeither_m_00= _m_0or _w_0prefers her final
partner _m_0to _m_00.
Either way, this contradicts our assumption that _w_0prefers m to _m_0.
It follows that S is a stable matching.
Running time of G-S
-
With suitable data structure and implementation, G-S is .
-
What about the running time of a straightforward brute-force search?
- The brute-force search would try every possible perfect matching and check if each one is stable until it finds a stable one.
- There are perfect matchings.
- Checking if a perfect matching is stable takes operations (with suitable data structure).
- So total running time is = .
-
Note how the G-S algorithm is unfair, favoring men...
-
Ex: If the men all list di↵erent women as their first choice, then in all runs of the G-S algorithm, all men end up matched with their first choice, independent of the preferences of the women.
-
Also, G-S is underspecified: when there is more than one free man, we can choose any free man to make the next proposal.
-
Difference choices lead to di↵erent executions of the algorithm.
-
Question: Do all executions of G-S yield the same matching?
Overall Themes of Algorithm Design
- Many problems have “easy” and straightforward but highly-inefficient algorithms for solving them.
- Usually brute-force search.
- High-polynomial time or exponential time for some problems.
- But by designing more sophisticated algorithms, we can solve the same problems more efficiently.
- Exponential time → Polynomial time.
- High-polynomial time → Low-polynomial time.
- It is usually not intuitive if/why these sophisticated algorithms are correct.
- So the proofs of their correctness are required.
- (People won’t use your algorithms unless you can show them that the algorithms are correct).