Celebrity problem:
You have a room with n people. A celebrity walks in. Everyone knows the celebrity, the celebrity knows no one. Non-celebrities may/may not know anyone in the room. Give an algorithm to find the celebrity. Discuss the complexity.
Mathematical formulation. Let G = (V, E) be a directed graph (represented by, say, its incident matrix). There is a vertex for each of the n people, and an edge from u to v if person u knows person v. We define a sink of a directed graph to be a vertex with indegree n − 1 and outdegree 0. A celebrity corresponds to a sink of the graph. We note that a graph can have at most one sink.
Brute-force solution. The graph has at most n(n − 1) edges, and we can compute it by asking a question for each potential edge. At this point, we can check whether a vertex is a sink by computing its indegree and its outdegree. This brute-force solution asks n(n − 1) questions. Next we show how to to do this with at most 3(n − 1) questions and linear place.
An elegant solution. Our algorithm consists of two phases: in the elimination phase, we eliminate all but one person from being the celebrity; in the verification phase we check whether this one remaining person is indeed a celebrity. The elimination phase maintains a list of possible celebrities. Initially it contains all n people. In each iteration, we delete one person from the list. We exploit the following key observation: if person 1 knows person 2, then person 1 is not a celebrity; if person 1 does not know person 2, then person 2 is not a celebrity. Thus, by asking person 1 if he knows person 2, we can eliminate either person 1 or person 2 from the list of possible celebrities. We can use this idea repeatedly to eliminate all people but one, say person L. We now verify by brute force whether L is a celebrity: for every other person i, we ask person L whether he knows person i, and we ask persons i whether they know person L. If person L always answers no, and the other people always answer yes, the we declare person L as the celebrity. Otherwise, we conclude there is no celebrity in this group.
Correctness. During the elimination phase, we maintain the invariant that is there exists a celebrity, then the celebrity is on the list. We can prove this by induction on the number of iterations. Thus, when elimination phase ends, either person L is a celebrity or there is no celebrity.
Analysis. The elimination phase requires exactly n − 1 questions, since each question reduces the size on the list by 1. In the verification phase, we ask L n − 1 questions, and we we ask the other n − 1 people one question. This phase requires at most 2(n − 1) questions, possibly fewer is L is not a celebrity. So the total number of questions is 3(n − 1). To efficiently implement the elimination phase, we maintain a queue that contains the remaining celebrities. Initially, we insert all n people to the queue. At each iteration we remove the top two elements off the queue, say v and w, and ask v whether he (or she) knows w. Depending on the outcome, we either insert v or w at the end of the queue. Each queue operation rakes Θ(1) time, so the whole process takes Θ(n) time.
public int findCelebrity(int[] persons) { Queue<Integer> queue = new LinkedList<>(); for(int p:persons) { queue.offer(p); } while(queue.size()>1) { int a = queue.poll(); int b = queue.poll(); if(hasAcquiantance(a, b)) { queue.offer(b); } else { queue.offer(a); } } int c = queue.poll(); // possible celebrity for(int p:persons) { if(p == c) continue; if(hasAcquiantance(c, p) || !hasAcquiantance(p, c)) return -1; } return c; }
我们还可以使用Stack来实现:
public int findCelebrity(int[] persons) { Stack<Integer> stack = new Stack<Integer>(); // 或者可以使用队列Queue for(int person: persons) { stack.push(person); } Stack<Integer> verifyStack = (Stack<Integer>) stack.clone(); //供最后验证用 int A = stack.pop(); int B = stack.pop(); while(stack.size() > 1) { // 直到剩余一个人为止 if(hasAcquiantance(A, B)) { // A认识B,说明A不是名人 A = stack.pop(); // 舍弃A,取出新的人代替A继续查找 } else { B = stack.pop(); } } // 因为最后一个人还没被检查,所以要把C和A,B比较,有可能为名人 int C = stack.pop(); // 保存名人 if(hasAcquiantance(C, A)) { C = A; } if(hasAcquiantance(C, B)) { C = B; } while(!verifyStack.isEmpty()) { int E = verifyStack.pop(); if(E != C) { // 如果C认识E,说明C不是名人,因为名人不可能认识别人 if(hasAcquiantance(C, E)) { return -1; } // 如果E不认识C,说明C不是名人,因为大家都认识名人 if(!hasAcquiantance(E, C)) { return -1; } } } return C; }
Reference:
http://webcourse.cs.technion.ac.il/234247/Spring2006/ho/WCFiles/Celebrity.pdf
http://www.cs.princeton.edu/courses/archive/spring13/cos423/problem0-1.pdf
相关推荐
本项目“quarrying-celebrity-id-master”旨在通过Python实现高效、精准的人脸识别系统,帮助开发者和研究人员深入理解这一技术。 一、Python在人脸识别中的应用 Python作为一种简洁易用的编程语言,深受数据科学...
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
React中的幻想名人设置 git clone https://github.com/gmassanek/fantasy-celebrity-react.gitcd fantasy-celebrity-reactnpm install跑步 gulp open index.html
《Guess-the-Celebrity》是一款基于Java开发的娱乐游戏,旨在带给玩家猜名人的乐趣。这款游戏通过一系列提示,让玩家猜测所描绘的名人身份,从而增加玩家对明星的了解和娱乐体验。以下是对游戏核心知识点的详细解析...
本文将深入探讨一款名为“Guess-the-celebrity”的Android游戏应用,它旨在测试用户对好莱坞明星的认知程度。这款应用以其独特的娱乐性和教育性吸引了大量用户,而它的背后则蕴含着丰富的编程技术和设计思路。 首先...
你是什么名人 ♪简介: 在本笔记本中,我们将尝试使用模式识别算法和卷积神经网络的有趣构想,以便预测可能与外层装饰图片中... -环境:Python-Jupyter(Google合作实验室)“ GPU” -框架:keras,scikit-learn
Django-名人-Wiki-App 当用户输入名人姓名并单击“提交”按钮时 使用simple_image_download模块显示5张名人照片 ...摘要后,显示一个“了解更多”按钮,该按钮会将用户带到Google页面(以名人作为查询)
Infocheck.in - 让您了解最新的传记,最新的新闻报道和来自印度的图片。 Infocheck.in通过传记,最新新闻报道和来自印度的图片,帮助您随时了解名人世界。获得电影,电视和音乐行业中您最喜欢的名人和名人面Kong的...
语言:English Infocheck.in - 让您了解与传记的名人,最新的新闻报道和来自印度的图片。 Infocheck.in有助于您对名人世界的最新信息 与传记,最新的新闻报道和印度图片。 在电影,电视和音乐行业获得您最喜爱的...
"Celebrity"可能是指一种特定的字体或者与名人相关的设计项目,但根据提供的信息,我们将主要围绕字体设计这一主题进行讨论。 字体设计不仅仅是文字的外观,它涉及到线条、空间、比例和风格等多方面的元素。设计师...
所有图片均已从Google抓取,并且没有重复的图片。 每个名人类(文件夹)包含大约700-800张图像,而Unknown类则包含100k张图像。 数据集的总大小-172 GB。 压缩文件总数-12 数据集的链接: : usp 2021年-更新: ...
"Celebrity"Magento模板是为电商网站设计的一种主题,旨在提升网站的视觉吸引力和用户体验,尤其适用于那些希望创建明星、时尚或娱乐相关的在线商店。 首先,我们要理解"Magento模板"这一概念。Magento模板是平台...
神经知识扩散的数据库。 文件为json,编码格式为UTF8。kb.experiment 电影和名人的KB电影属性--id :每部电影的唯一ID --...sentence中,用于基于类似知识的对话--celebrity :名人列表和相应的celebrityID出现在raw_se