Problem. In a party of people, a celebrity is someone whom everyone knows and who knows nobody. Given only the query knows(a, b), find the celebrity or report that none exists.
The elimination
Each query eliminates one candidate:
int findCelebrity(int n) {
int cand = 0;
for (int i = 1; i < n; i++)
if (knows(cand, i)) cand = i; // cand knows i -> cand is out
// else i knows... no: i is out, keep cand
for (int i = 0; i < n; i++) { // VERIFY
if (i == cand) continue;
if (knows(cand, i) || !knows(i, cand)) return -1;
}
return cand;
}queries to find the candidate, then to verify — total, or in the worst case.
Verification is mandatory
The elimination phase finds the only possible celebrity, not a confirmed one. If no celebrity exists, it still returns someone. Skipping the second phase is the standard bug.
Why one query eliminates one person
- If
knows(a,b)is true, then knows someone, so is not a celebrity. - If it is false, then is not known by everyone, so is not a celebrity.
Either way one candidate dies, so queries leave exactly one survivor. And since a celebrity, if it exists, is never eliminated, the survivor is the only possibility.
The lower bound
queries are necessary in the worst case, so the simple algorithm is optimal up to lower-order terms. The verification phase can be trimmed slightly (some pairs are already known from phase 1), which is where the saving comes from.
The graph view
Model “knows” as a directed graph. A celebrity is a vertex with
- At most one can exist (two celebrities would have to know each other).
- With the full adjacency matrix, finding it is by summing rows and columns — but the algorithm never reads most of the matrix.
That gap is the interesting part: the answer is determined by of the entries.
The general technique: elimination by pairwise comparison
Each comparison removes one candidate; comparisons leave one; then verify.
| Problem | Comparison | Verification |
|---|---|---|
| Celebrity | knows(a,b) | check in/out degrees |
| Majority element | equal or not | count occurrences |
| Maximum of | none needed | |
| Tournament winner | who beats whom | check it beats everyone |
| The “heavier coin” | balance | weigh against a known-good |
| Finding a sink in a DAG | edge direction | check out-degree 0 |
Majority and celebrity share the same shape: a linear elimination pass that finds the only candidate, followed by a verification pass. Recognising that pattern makes both algorithms easy to reconstruct.
Variants
| Variant | Note |
|---|---|
| Celebrity may not exist | the verification handles it |
| Several celebrities | impossible — at most one |
| ”Almost celebrity” (knows at most ) | needs more queries |
| Adjacency matrix given | with the same algorithm, or by summing |
| Find a sink in a tournament | every tournament has a Hamiltonian path; a sink may not exist |
| Interactive version with a query limit | the bound matters |
Why it is worth knowing
It is the cleanest demonstration that an algorithm can beat the input size: with possible relationships, only of them need to be examined. Whenever a problem gives you an relation and asks for something with a strong uniqueness property, look for an elimination argument before reading the whole matrix.
See also: Majority Element · Graph Fundamentals · Extremal Principle