Celebrity Problem

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 155:-Celebrity Problem

The Celebrity Problem is a classic problem often used to illustrate the concept of a directed graph and its applications. In this problem, you are given a group of people numbered from 0 to N-1, where each person knows some other people. However, there is one person (the celebrity) who is known by everyone else but doesn't know anyone.

The task is to find the celebrity, if one exists, or determine that there is no celebrity in the group.

Here's a common approach to solving the Celebrity Problem:

Create a two-dimensional matrix or an array to represent the relationships between people. The element matrix[i][j] (or relation[i][j]) will be 1 if person i knows person j, and 0 otherwise.

Initialize two pointers, A and B, to 0 and N-1, respectively. These pointers will be used to keep track of potential celebrities.

Iterate through the group while A is less than B: a. Check if person A knows person B (i.e., matrix[A][B] is 1). If true, person A cannot be the celebrity, so increment A. b. Otherwise, person A doesn't know person B, so person B cannot be the celebrity, so decrement B.

After the loop, check if person A is the celebrity by verifying that: a. Person A doesn't know anyone else (i.e., matrix[A][i] is 0 for all i except A). b. Everyone else knows person A (i.e., matrix[i][A] is 1 for all i except A).

If the conditions in step 4 are satisfied, person A is the celebrity. Otherwise, there is no celebrity in the group.

Here's a simple C++ implementation of the Celebrity Problem:

cppCopy code

#include <iostream> #include <vector> bool knows(int a, int b) { // Placeholder function that returns true if person 'a' knows person 'b' // You can implement this function according to your input data return true; // Replace with actual implementation } int findCelebrity(int N) { int A = 0; int B = N - 1; while (A < B) { if (knows(A, B)) { A++; } else { B--; } } for (int i = 0; i < N; ++i) { if (i != A && (knows(A, i) || !knows(i, A))) { return -1; // No celebrity found } } return A; // A is the celebrity } int main() { int N = 4; // Number of people int celebrity = findCelebrity(N); if (celebrity != -1) { std::cout << "Celebrity found: Person " << celebrity << std::endl; } else { std::cout << "No celebrity found." << std::endl; } return 0; }

In this implementation, the knows function represents the relationship between two people (i.e., whether one person knows the other). You should replace the placeholder knows function with the actual implementation based on your input data. The findCelebrity function finds the celebrity using the approach described earlier.

Keep in mind that the celebrity problem assumes there is exactly one celebrity, or none at all. If multiple celebrities are possible, this approach won't work as intended.

21. Stacks - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?