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
npeople. in each iteration, delete 1 person list. exploit following key observation: if person1knows person2, person1not celebrity; if person1not know person2, person2not celebrity. thus, asking person1if knows person2, can eliminate either person1or person2list of possible celebrities. can use idea repeatedly eliminate people one, personp. verify brute force whetherpcelebrity: every other personi, ask personpwhether knows personi, , ask personsiwhether know personp. if personpanswers no, , other people answer yes, declare personpcelebrity. otherwise, conclude there no celebrity in group.