In a party of N people, only one person is known to everyone. Such a person may be present in the party, if yes, (s)he doesn’t know anyone in the party. We can only ask questions like “does A know B? “. Find the stranger (celebrity) in minimum number of questions.

We can describe the problem input as an array of numbers/characters representing persons in the party. We also have a hypothetical function HaveAcquaintance(A, B) which returns true if A knows B, false otherwise. How can we solve the problem.

We measure the complexity in terms of calls made to HaveAcquaintance().

Method 1 (Graph)
We can model the solution using graphs. Initialize indegree and outdegree of every vertex as 0. If A knows B, draw a directed edge from A to B, increase indegree of B and outdegree of A by 1. Construct all possible edges of the graph for every possible pair [i, j]. We have NC2 pairs. If celebrity is present in the party, we will have one sink node in the graph with outdegree of zero, and indegree of N-1. We can find the sink node in (N) time, but the overall complexity is O(N2) as we need to construct the graph first.

Method 2 (Recursion)
We can decompose the problem into combination of smaller instances. Say, if we know celebrity of N-1 persons, can we extend the solution to N? We have two possibilities, Celebrity(N-1) may know N, or N already knew Celebrity(N-1). In the former case, N will be celebrity if N doesn’t know anyone else. In the later case we need to check that Celebrity(N-1) doesn’t know N.

Solve the problem of smaller instance during divide step. On the way back, we find the celebrity (if present) from the smaller instance. During combine stage, check whether the returned celebrity is known to everyone and he doesn’t know anyone. The recurrence of the recursive decomposition is,

T(N) = T(N-1) + O(N)

T(N) = O(N2). You may try writing pseudo code to check your recursion skills.

Method 3 (Using Stack)

The graph construction takes O(N2) time, it is similar to brute force search. In case of recursion, we reduce the problem instance by not more than one, and also combine step may examine M-1 persons (M – instance size).

We have following observation based on elimination technique (Refer Polya’s How to Solve It book).

  • If A knows B, then A can’t be celebrity. Discard A, and B may be celebrity.
  • If A doesn’t know B, then B can’t be celebrity. Discard B, and A may be celebrity.
  • Repeat above two steps till we left with only one person.
  • Ensure the remained person is celebrity. (Why do we need this step?)

We can use stack to verity celebrity.

  1. Push all the celebrities into a stack.
  2. Pop off top two persons from the stack, discard one person based on return status of HaveAcquaintance(A, B).
  3. Push the remained person onto stack.
  4. Repeat step 2 and 3 until only one person remains in the stack.
  5. Check the remained person in stack doesn’t have acquaintance with anyone else.

We will discard N elements utmost (Why?). If the celebrity is present in the party, we will call HaveAcquaintance() 3(N-1) times. Here is code using stack.

C++ Programming:

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20to%20find%20celebrity%0A%23include%20%3Cbits%2Fstdc%2B%2B.h%3E%0A%23include%20%3Clist%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Max%20%23%20of%20persons%20in%20the%20party%0A%23define%20N%208%0A%20%0A%2F%2F%20Person%20with%202%20is%20celebrity%0Abool%20%20MATRIX%5BN%5D%5BN%5D%20%3D%20%7B%7B0%2C%200%2C%201%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%201%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%200%2C%201%2C%200%7D%7D%3B%0A%20%0Abool%20knows(int%20a%2C%20int%20b)%0A%7B%0A%20%20%20%20return%20MATRIX%5Ba%5D%5Bb%5D%3B%0A%7D%0A%20%0A%2F%2F%20Returns%20-1%20if%20celebrity%20is%20not%20present.%0A%2F%2F%20If%20present%2C%20returns%20id%20%20(value%20from%200%20to%20n-1).%0Aint%20findCelebrity(int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20Handle%20trivial%20case%20of%20size%20%3D%202%0A%20%0A%20%20%20%20stack%3Cint%3E%20s%3B%0A%20%0A%20%20%20%20int%20C%3B%20%2F%2F%20Celebrity%0A%20%0A%20%20%20%20%2F%2F%20Push%20everybody%20to%20stack%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20s.push(i)%3B%0A%20%0A%20%20%20%20%2F%2F%20Extract%20top%202%0A%20%20%20%20int%20A%20%3D%20s.top()%3B%0A%20%20%20%20s.pop()%3B%0A%20%20%20%20int%20B%20%3D%20s.top()%3B%0A%20%20%20%20s.pop()%3B%0A%20%0A%20%20%20%20%2F%2F%20Find%20a%20potential%20celevrity%0A%20%20%20%20while%20(s.size()%20%3E%201)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(knows(A%2C%20B))%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20A%20%3D%20s.top()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20s.pop()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20B%20%3D%20s.top()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20s.pop()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Potential%20candidate%3F%0A%20%20%20%20C%20%3D%20s.top()%3B%0A%20%20%20%20s.pop()%3B%0A%20%0A%20%20%20%20%2F%2F%20Last%20candidate%20was%20not%20examined%2C%20it%20leads%0A%20%20%20%20%2F%2F%20one%20excess%20comparison%20(optimize)%0A%20%20%20%20if%20(knows(C%2C%20B))%0A%20%20%20%20%20%20%20%20C%20%3D%20B%3B%0A%20%0A%20%20%20%20if%20(knows(C%2C%20A))%0A%20%20%20%20%20%20%20%20C%20%3D%20A%3B%0A%20%0A%20%20%20%20%2F%2F%20Check%20if%20C%20is%20actually%20a%20celebrity%20or%20not%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20any%20person%20doesn’t%20know%20’a’%20or%20’a’%0A%20%20%20%20%20%20%20%20%2F%2F%20doesn’t%20know%20any%20person%2C%20return%20-1%0A%20%20%20%20%20%20%20%20if%20(%20(i%20!%3D%20C)%20%26%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(knows(C%2C%20i)%20%7C%7C%20!knows(i%2C%20C))%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20C%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20code%0Aint%20main()%0A%7B%0A%20%20%20%20int%20n%20%3D%204%3B%0A%20%20%20%20int%20id%20%3D%20findCelebrity(n)%3B%0A%20%20%20%20id%20%3D%3D%20-1%20%3F%20cout%20%3C%3C%20%22No%20celebrity%22%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Celebrity%20ID%20%22%20%3C%3C%20id%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output :

Celebrity ID 2

Complexity O(N). Total comparisons 3(N-1). Try the above code for successful MATRIX {{0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}}.

Note: You may think that why do we need a new graph as we already have access to input matrix. Note that the matrix MATRIX used to help the hypothetical function HaveAcquaintance(A, B), but never accessed via usual notation MATRIX[i, j]. We have access to the input only through the function HaveAcquiantance(A, B). Matrix is just a way to code the solution. We can assume the cost of hypothetical function as O(1).

If still not clear, assume that the function HaveAcquiantance accessing information stored in a set of linked lists arranged in levels. List node will have next and nextLevel pointers. Every level will have N nodes i.e. an N element list, next points to next node in the current level list and the nextLevel pointer in last node of every list will point to head of next level list. For example the linked list representation of above matrix looks like,

L0 0->0->1->0
             |
L1           0->0->1->0
                       |
L2                     0->0->1->0
                                 |
L3                               0->0->1->0

The function HaveAcquanintance(i, j) will search in the list for j-th node in the i-th level. Out goal is to minimize calls to HaveAcquanintance function.

Method 4 (Using two Pointers)
The idea is to use two pointers, one from start and one from the end. Assume the start person is A, and the end person is B. If A knows B, then A must not be the celebrity. Else, B must not be the celebrity. We will find a celebrity candidate at the end of the loop. Go through each person again and check whether this is the celebrity. Below is C++ implementation.

C++ Programming:

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20to%20find%20celebrity%20in%20O(n)%20time%0A%2F%2F%20and%20O(1)%20extra%20space%0A%23include%20%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Max%20%23%20of%20persons%20in%20the%20party%0A%23define%20N%208%0A%20%0A%2F%2F%20Person%20with%202%20is%20celebrity%0Abool%20%20MATRIX%5BN%5D%5BN%5D%20%3D%20%7B%7B0%2C%200%2C%201%2C%200%7D%2C%0A%20%20%20%20%7B0%2C%200%2C%201%2C%200%7D%2C%0A%20%20%20%20%7B0%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%7B0%2C%200%2C%201%2C%200%7D%0A%7D%3B%0A%20%0Abool%20knows(int%20a%2C%20int%20b)%0A%7B%0A%20%20%20%20return%20MATRIX%5Ba%5D%5Bb%5D%3B%0A%7D%0A%20%0A%2F%2F%20Returns%20id%20of%20celebrity%0Aint%20findCelebrity(int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20Initialize%20two%20pointers%20as%20two%20corners%0A%20%20%20%20int%20a%20%3D%200%3B%0A%20%20%20%20int%20b%20%3D%20n%20-%201%3B%0A%20%0A%20%20%20%20%2F%2F%20Keep%20moving%20while%20the%20two%20pointers%0A%20%20%20%20%2F%2F%20don’t%20become%20same.%20%0A%20%20%20%20while%20(a%20%3C%20b)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(knows(a%2C%20b))%0A%20%20%20%20%20%20%20%20%20%20%20%20a%2B%2B%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20b–%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Check%20if%20a%20is%20actually%20a%20celebrity%20or%20not%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20any%20person%20doesn’t%20know%20’a’%20or%20’a’%0A%20%20%20%20%20%20%20%20%2F%2F%20doesn’t%20know%20any%20person%2C%20return%20-1%0A%20%20%20%20%20%20%20%20if%20(%20(i%20!%3D%20a)%20%26%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(knows(a%2C%20i)%20%7C%7C%20!knows(i%2C%20a))%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20a%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20code%0Aint%20main()%0A%7B%0A%20%20%20%20int%20n%20%3D%204%3B%0A%20%20%20%20int%20id%20%3D%20findCelebrity(n)%3B%0A%20%20%20%20id%20%3D%3D%20-1%20%3F%20cout%20%3C%3C%20%22No%20celebrity%22%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Celebrity%20ID%20%22%20%3C%3C%20id%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output :

Celebrity ID 2

Categorized in: