Celebrity Problem In C++

Within the realm of computer science, there exist several intricate problems and algorithms to grapple with. One such problem is the "Celebrity Problem", which revolves around the task of identifying a celebrity within a group of individuals. In this blog post, we will delve deep into the Celebrity Problem, offering a thorough explanation and presenting a C++ solution complete with code, illustrative examples, and outcomes.

What is the Celebrity Problem?

The Celebrity Problem is a classic computational challenge that revolves around the identification of a celebrity in a group of people. In this context, a "celebrity" is an individual recognized by everyone else in the group yet who does not reciprocate that recognition to any of the others. The problem can be framed as follows: Given a group of individuals and a function "knows(a, b)" that yields true if the person 'a' knows person 'b', and false otherwise, our objective is to pinpoint the celebrity within the group if one exists.

The central premise behind resolving the Celebrity Problem is the gradual elimination of non-celebrity candidates, ultimately leaving us with a genuine celebrity candidate. The algorithm leverages the fact that a celebrity is a person who is unknown to all others, while every other individual knows at least one person.

Solving the Celebrity Problem in C++

To tackle the Celebrity Problem in C++, we will implement a solution employing a stack data structure. This stack will serve as our tool for keeping track of potential celebrity candidates as we navigate through the group of individuals.

Let's initiate the process by establishing the "knows(a, b)" function and creating the C++ implementation of the Celebrity Problem:

Example

#include <iostream>
#include <stack>

using namespace std;

// Function to verify if person 'a' knows person 'b'
bool knows(int a, int b) {
    // This function is merely a placeholder; you should insert your own logic here
    // Return true if 'a' knows 'b,' and false otherwise
    // For the sake of illustration, we will assume 'a' knows 'b' when a < b
    return a < b;
}

// Function to determine the celebrity within the group
int findCelebrity(int n) {
    stack<int> celebrityCandidates;

    // Populate the stack with all individuals as potential candidates
    for (int i = 0; i < n; i++) {
        celebrityCandidates.push(i);
    }

    while (celebrityCandidates.size() > 1) {
        int personA = celebrityCandidates.top();
        celebrityCandidates.pop();
        int personB = celebrityCandidates.top();
        celebrityCandidates.pop();

        if (knows(personA, personB)) {
            // If personA knows personB, personA cannot be the celebrity
            celebrityCandidates.push(personB);
        } else {
            // If personA doesn't know personB, personB cannot be the celebrity
            celebrityCandidates.push(personA);
        }
    }

    // The remaining candidate is a potential celebrity
    int potentialCelebrity = celebrityCandidates.top();

    // Confirm if the potential celebrity is genuinely a celebrity
    for (int i = 0; i < n; i++) {
        if (i != potentialCelebrity && (knows(potentialCelebrity, i) || !knows(i, potentialCelebrity))) {
            return -1; // No celebrity found
        }
    }

    return potentialCelebrity;
}

int main() {
    int n = 4; // Total number of individuals in the group
    int celebrity = findCelebrity(n);

    if (celebrity != -1) {
        cout << "The celebrity in the group is person " << celebrity << endl;
    } else {
        cout << "No celebrity found within the group." << endl;
    }

    return 0;
}

To witness the provided C++ code in action, let's execute it with a simulated group of individuals:

Example

int main() {
    int n = 4; // Total number of individuals in the group
    int celebrity = findCelebrity(n);

    if (celebrity != -1) {
        cout << "The celebrity within the group is individual " << celebrity << endl;
    } else {
        cout << "No celebrity found within the group." << endl;
    }

    return 0;
}

Output:

Assuming that the "knows(a, b)" function is crafted such that a celebrity indeed resides within the group, the resulting output will be:

Example

The celebrity within the group is individual 2

In this example, individual 2 claims the status of the celebrity since everyone else within the group acknowledges them, in turn, while they remain unaware of anyone else.

Conclusion

The Celebrity Problem represents a classic algorithmic challenge revolving around the identification of a celebrity within a group of individuals. We can efficiently tackle this problem by implementing a solution in C++ utilizing a stack data structure. Along with the comprehensive explanation and illustrative example, the provided code should serve as a valuable resource for comprehending and applying the Celebrity Problem solution within the C++ projects. Remember to tailor the "knows(a, b)" function to align with the specific requirements of the application, ensuring a seamless integration of this algorithm into the work.

Input Required

This code uses input(). Please provide values below: