Skip to content

feat: Add CelebrityFinder new algorithm with Junit tests #5756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@
* [WiggleSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/WiggleSort.java)
* stacks
* [BalancedBrackets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/BalancedBrackets.java)
* [CelebrityFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/CelebrityFinder.java)
* [DecimalToAnyUsingStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java)
* [DuplicateBrackets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/DuplicateBrackets.java)
* [InfixToPostfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/InfixToPostfix.java)
Expand Down Expand Up @@ -1163,6 +1164,7 @@
* [WiggleSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/WiggleSortTest.java)
* stacks
* [BalancedBracketsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/BalancedBracketsTest.java)
* [CelebrityFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/CelebrityFinderTest.java)
* [DecimalToAnyUsingStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/DecimalToAnyUsingStackTest.java)
* [DuplicateBracketsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/DuplicateBracketsTest.java)
* [InfixToPostfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/InfixToPostfixTest.java)
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/com/thealgorithms/stacks/CelebrityFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.thealgorithms.stacks;

import java.util.Stack;

/**
* Solves the celebrity problem using a stack-based algorithm.
*
* <p>Celebrity is someone known by everyone but doesn't know anyone else.
* <p>Applications: Graph theory and social network analysis.
*
* @author Hardvan
*/
public final class CelebrityFinder {
private CelebrityFinder() {
}

/**
* Finds the celebrity in the given party matrix using a stack-based algorithm.
*
* @param party A 2D matrix where party[i][j] is 1 if i knows j, otherwise 0.
* @return The index of the celebrity, or -1 if there is no celebrity.
*/
public static int findCelebrity(int[][] party) {

// Push all people onto the stack
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < party.length; i++) {
stack.push(i);
}

// Find the potential celebrity by comparing pairs
while (stack.size() > 1) {
int person1 = stack.pop();
int person2 = stack.pop();

if (party[person1][person2] == 1) {
stack.push(person2); // person1 knows person2, so person2 might be the celebrity
} else {
stack.push(person1); // person1 doesn't know person2, so person1 might be the celebrity
}
}

// Verify the candidate
int candidate = stack.pop();
for (int i = 0; i < party.length; i++) {
if (i != candidate && (party[candidate][i] == 1 || party[i][candidate] == 0)) {
return -1;
}
}
return candidate;
}
}
41 changes: 41 additions & 0 deletions src/test/java/com/thealgorithms/stacks/CelebrityFinderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.thealgorithms.stacks;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class CelebrityFinderTest {

@ParameterizedTest
@MethodSource("providePartyMatrices")
public void testCelebrityFinder(int[][] party, int expected) {
assertEquals(expected, CelebrityFinder.findCelebrity(party));
}

private static Stream<Arguments> providePartyMatrices() {
return Stream.of(
// Test case 1: Celebrity exists
Arguments.of(new int[][] {{0, 1, 1}, {0, 0, 1}, {0, 0, 0}}, 2),

// Test case 2: No celebrity
Arguments.of(new int[][] {{0, 1, 0}, {1, 0, 1}, {1, 1, 0}}, -1),

// Test case 3: Everyone knows each other, no celebrity
Arguments.of(new int[][] {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}}, -1),

// Test case 4: Single person, they are trivially a celebrity
Arguments.of(new int[][] {{0}}, 0),

// Test case 5: All know the last person, and they know no one
Arguments.of(new int[][] {{0, 1, 1, 1}, {0, 0, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 0}}, 3),

// Test case 6: Larger party with no celebrity
Arguments.of(new int[][] {{0, 1, 1, 0}, {1, 0, 0, 1}, {0, 1, 0, 1}, {1, 1, 1, 0}}, -1),

// Test case 7: Celebrity at the start of the matrix
Arguments.of(new int[][] {{0, 0, 0}, {1, 0, 1}, {1, 1, 0}}, 0));
}
}