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 asks n(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 person 1 knows person 2, person 1 not celebrity; if person 1 not know person 2, person 2 not celebrity. thus, asking person 1 if knows person 2, can eliminate either person 1 or person 2 list of possible celebrities. can use idea repeatedly eliminate people one, person p. verify brute force whether p celebrity: every other person i , ask person p whether knows person i , , ask persons i whether know person p . if person p answers no, , other people answer yes, declare person p celebrity. otherwise, conclude there no celebrity in group.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -