Optimal solution for the celebrity algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The "celebrity problem" is a well-known puzzle in computer science that is often used to illustrate graph theory and algorithmic reasoning. This problem is typically posed as follows: given a group of `n` people, determine if there is a "celebrity" amongst them. A celebrity is defined as someone who is known by everyone else but does not know anyone else themselves. The challenge is to efficiently ascertain if a celebrity exists and, if so, identify them with minimal questioning among the individuals.
Problem Definition
Assumptions
- Each person knows some subset of the other members in the group.
- There is at most one celebrity.
- The problem can be represented using a directed graph, where an edge from `A` to `B` indicates that `A` knows `B`.
Input and Output
- Input: A function `knows(A, B)` which returns `True` if person `A` knows person `B`, otherwise `False`.
- Output: Identify the celebrity or return that there is no celebrity.
Optimal Solution
The optimal solution leverages a two-step method to efficiently find the celebrity if they exist. This involves 2 main phases:
Phase 1: Candidate Identification
The strategy is to identify a single candidate through a linear pass. If there exists a celebrity, this candidate is a potential celebrity.
Algorithm
- Initialize: Set the first person as the candidate, `C`.
- Iterate Through Each Person:
- For each person `i` in the group, check if `C` knows `i`.
- If `C` knows `i`, then `C` cannot be a celebrity, and update `C` to `i`.
- If `C` does not know `i`, then `i` cannot be a celebrity.
The completion of this phase leaves `C` as a possible celebrity.
Phase 2: Verification
Once a candidate is identified, verify whether they indeed meet the celebrity criteria.
Verification Steps
- Confirm `C` is Known by Everyone Else:
- Ensure every person either knows `C` or it’s the same person.
- Ensure `C` Knows None of the Others:
- Verify that `C` does not know any other person.
If both these conditions are satisfied, `C` is the celebrity; otherwise, there is no celebrity in the group.
Complexity Analysis
- Time Complexity: O(n)
- The candidate identification requires O(n) checks.
- Verification also requires O(n) checks.
- Space Complexity: O(1)
- Only a constant amount of extra space is needed.
The solution, thus, beats the naive O(n^2) approach by traversing each edge in the graph representing `knows(A, B)` relation at most once.
Example Walkthrough
Given four people: `0, 1, 2, 3`. The function `knows(A, B)` is defined with the following truth table:
| Person A | Person B | knows(A, B) |
| 0 | 1 | False |
| 0 | 2 | True |
| 0 | 3 | False |
| 1 | 0 | True |
| 1 | 2 | True |
| 1 | 3 | True |
| 2 | 0 | False |
| 2 | 1 | False |
| 2 | 3 | False |
| 3 | 0 | False |
| 3 | 1 | False |
| 3 | 2 | True |
Phase 1: Candidate Identification
- Start with candidate `C = 0`.
- `C` knows `2`, so update `C` to `2`.
- `C = 2` knows no one further (`knows(2, 1)`, `knows(2, 3)` are `False`), so `2` remains a candidate.
Phase 2: Verification
- Check if `2` is known by everyone: only `3` knows `2`.
- Therefore, `2` is not a celebrity as they are not known by everyone.
The result is that there is no celebrity in this setup.
Conclusion
The "celebrity" problem demonstrates how a seemingly complex problem can be broken down into simpler steps and solved in linear time. Understanding this approach not only provides a solution to this specific problem but also offers insights into handling graph-based algorithmic challenges more broadly. This methodology showcases the power of reducing problem space with a clever approach and careful verification methods.
Summary Table
| Phase | Description | Complexity | Outcome |
| Candidate Selection | Identify a single candidate by elimination | O(n) | A potential celebrity candidate is found |
| Verification | Confirm candidate's celebrity status | O(n) | Validate if the candidate is indeed a celebrity or not |
This optimal solution can be utilized in various scenarios requiring graph theory, particularly in social network analysis and similar domains where understanding directional connections is crucial.
Related reading
- optimal way to calculate all nodes at distance less than k from m given nodes
- Optimal way to sort a list by reversing sublists
- Optimal weights subset sum using backtracking
- Optimisation of recursive algorithm in Java
- Optimize Divide an array into continuous subsequences of length no greater than k such that sum of maximum value of each subsequence is minimum
- Optimize finding index of nearest point in 2d arrays
- Optimal variable initialization and learning rate in Tensorflow for matrix factorization
- Optimising accuracy for OneClassSVM

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.