Celebrity Algorithm
Optimal Solution
Graph Theory
Computer Science
Algorithm Optimization

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.

Practice algorithms

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

  1. Initialize: Set the first person as the candidate, `C`.
  2. 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

  1. Confirm `C` is Known by Everyone Else:
    • Ensure every person either knows `C` or it’s the same person.
  2. 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 APerson Bknows(A, B)
01False
02True
03False
10True
12True
13True
20False
21False
23False
30False
31False
32True

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

PhaseDescriptionComplexityOutcome
Candidate SelectionIdentify a single candidate by eliminationO(n)A potential celebrity candidate is found
VerificationConfirm candidate's celebrity statusO(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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.