270 Handout 1
270 Handout 1
Definition: Person p is famous iff (if and only if) everyone in this classroom knows p, but p does
not know anyone else in this classroom.
Definition: A single query consists of taking a pair of people (p, q) in the classroom, asking if p
knows q, and receiving a response.
Problem Formulation: Determine all the famous people in a classroom with n people, using the
minimum possible number of queries.
A First Attempt:
1: For all people in the class p:
2: For all people in the class q, where p 6= q:
3: Check if p knows q. If so, p is not famous.
4: Check if q knows p. If not, p is not famous.
5: If p is famous, add p to the list of famous people.
6: Return our list of famous people.
Follow-up Questions:
1. How many queries does this algorithm use in the worst case?
2. Is this better than our old algorithm?
3. Is this always better than our old algorithm?
Important Points
1. When writing algorithms, pseudocode is acceptable (in fact, it is recommended). You can
also write in code, or in English, as long as you provide enough detail.
2. If a reasonably competent programmer could take your answer and code it up in a language
of their choice, then your answer is acceptable.
3. Algorithms is learned with practice. If you think that you must have a “Eureka” moment
to answer an Algorithms problem on a test, that is merely a sign that you need to practice
more problems.
Stable Matching
Every user has ranked all of the members of the opposite sex from 1 to n.
The site needs to create male/female pairs so that everyone is in exactly one pair.
Suppose n = 2.
Suppose both women ranked m1 higher, and both men ranked w1 higher.
If we try to match m1 with w2 and m2 with w1 , there is something innately wrong with our
solution.
m1 and w1 will dump their respective partners and match together.
If this situation doesn’t happen, we say that this is a stable matching.
More generally, if mi is matched with wa , and mj is matched with wb , at least one of these
statements should be false:
mi prefers wb to wa .
wb prefers mi to mj .
2. m asks out the first woman w on his list whom he has not yet asked.
4. if w is already matched, but prefers m, she says yes and breaks up with her current match.
Every woman will be matched, since a woman always says yes if they are unmatched, and
men will eventually ask everyone out until someone says yes.
Assume there is an unstable pairing h Alice, Bob i and h Charlie, Debra i, where Bob and
Debra prefer each other to their current matches.
Bob would have asked out Debra before Alice, so Debra either said no, or broke up with Bob
prior to this.
Women only ever improve their match, so it is impossible that Debra prefers Bob to her
current match.
The Gale-Shapley algorithm produces the best possible stable matching for all men simultane-
ously, and the worst possible stable matching for all women simultaneously.
The algorithm becomes best for women and worst for men if you have women do the asking.
If you were implementing this algorithm for your job, which version would you make?
As Computer Scientists, our algorithms directly affect the lives of many people.
Simple decisions about implementation, and where we get our data, have a profound impact.
Be aware of this, consider things carefully, and sometimes consult experts on fairness.
What are some examples of this?
Machine Learning algorithms, when trained on biased data, can produce biased/racist results.
2. Given a problem and an algorithm, can we prove that the algorithm correctly solves the
problem?
Extra Problems
1. Improve the algorithm for the Famous Person Problem to require only 3(n−1)−log n queries.
2. Informally argue why the Famous Person Problem cannot be solved in less than θ(n) queries.