data structures - Optimal solution for the "celebrity" algorithm -
among n
persons,a "celebrity" defined who known not know anyone. problem identify celebrity, if 1 exists, asking question of form, "excuse me, know person on there?" (the assumption answers correct, , celebrity answer.) goal minimize number of questions.
is there solution of order less obvious o(n^2)
here?
using analysis of celebrity problem here
brute-force solution. graph has @
n(n-1)
edges, , can compute asking question each potential edge. @ point, can check whether vertex sink computing indegree , outdegree. brute-force solution asksn(n-1)
questions. next show how to @3(n-1)
questions , linear place.an elegant solution. our algorithm consists of 2 phases: in elimination phase, eliminate 1 person being celebrity; in verification phase check whether 1 remaining person indeed celebrity. elimination phase maintains list of possible celebrities. contains
n
people. in each iteration, delete 1 person list. exploit following key observation: if person1
knows person2
, person1
not celebrity; if person1
not know person2
, person2
not celebrity. thus, asking person1
if knows person2
, can eliminate either person1
or person2
list of possible celebrities. can use idea repeatedly eliminate people one, personp
. verify brute force whetherp
celebrity: every other personi
, ask personp
whether knows personi
, , ask personsi
whether know personp
. if personp
answers no, , other people answer yes, declare personp
celebrity. otherwise, conclude there no celebrity in group.