Skip to content

Commit adf21ab

Browse files
authored
feat: Add CelebrityFinder new algorithm with Junit tests (#5756)
1 parent 776946e commit adf21ab

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@
607607
* [WiggleSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/WiggleSort.java)
608608
* stacks
609609
* [BalancedBrackets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/BalancedBrackets.java)
610+
* [CelebrityFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/CelebrityFinder.java)
610611
* [DecimalToAnyUsingStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java)
611612
* [DuplicateBrackets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/DuplicateBrackets.java)
612613
* [InfixToPostfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/InfixToPostfix.java)
@@ -1163,6 +1164,7 @@
11631164
* [WiggleSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/WiggleSortTest.java)
11641165
* stacks
11651166
* [BalancedBracketsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/BalancedBracketsTest.java)
1167+
* [CelebrityFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/CelebrityFinderTest.java)
11661168
* [DecimalToAnyUsingStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/DecimalToAnyUsingStackTest.java)
11671169
* [DuplicateBracketsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/DuplicateBracketsTest.java)
11681170
* [InfixToPostfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/InfixToPostfixTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Solves the celebrity problem using a stack-based algorithm.
7+
*
8+
* <p>Celebrity is someone known by everyone but doesn't know anyone else.
9+
* <p>Applications: Graph theory and social network analysis.
10+
*
11+
* @author Hardvan
12+
*/
13+
public final class CelebrityFinder {
14+
private CelebrityFinder() {
15+
}
16+
17+
/**
18+
* Finds the celebrity in the given party matrix using a stack-based algorithm.
19+
*
20+
* @param party A 2D matrix where party[i][j] is 1 if i knows j, otherwise 0.
21+
* @return The index of the celebrity, or -1 if there is no celebrity.
22+
*/
23+
public static int findCelebrity(int[][] party) {
24+
25+
// Push all people onto the stack
26+
Stack<Integer> stack = new Stack<>();
27+
for (int i = 0; i < party.length; i++) {
28+
stack.push(i);
29+
}
30+
31+
// Find the potential celebrity by comparing pairs
32+
while (stack.size() > 1) {
33+
int person1 = stack.pop();
34+
int person2 = stack.pop();
35+
36+
if (party[person1][person2] == 1) {
37+
stack.push(person2); // person1 knows person2, so person2 might be the celebrity
38+
} else {
39+
stack.push(person1); // person1 doesn't know person2, so person1 might be the celebrity
40+
}
41+
}
42+
43+
// Verify the candidate
44+
int candidate = stack.pop();
45+
for (int i = 0; i < party.length; i++) {
46+
if (i != candidate && (party[candidate][i] == 1 || party[i][candidate] == 0)) {
47+
return -1;
48+
}
49+
}
50+
return candidate;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public class CelebrityFinderTest {
11+
12+
@ParameterizedTest
13+
@MethodSource("providePartyMatrices")
14+
public void testCelebrityFinder(int[][] party, int expected) {
15+
assertEquals(expected, CelebrityFinder.findCelebrity(party));
16+
}
17+
18+
private static Stream<Arguments> providePartyMatrices() {
19+
return Stream.of(
20+
// Test case 1: Celebrity exists
21+
Arguments.of(new int[][] {{0, 1, 1}, {0, 0, 1}, {0, 0, 0}}, 2),
22+
23+
// Test case 2: No celebrity
24+
Arguments.of(new int[][] {{0, 1, 0}, {1, 0, 1}, {1, 1, 0}}, -1),
25+
26+
// Test case 3: Everyone knows each other, no celebrity
27+
Arguments.of(new int[][] {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}}, -1),
28+
29+
// Test case 4: Single person, they are trivially a celebrity
30+
Arguments.of(new int[][] {{0}}, 0),
31+
32+
// Test case 5: All know the last person, and they know no one
33+
Arguments.of(new int[][] {{0, 1, 1, 1}, {0, 0, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 0}}, 3),
34+
35+
// Test case 6: Larger party with no celebrity
36+
Arguments.of(new int[][] {{0, 1, 1, 0}, {1, 0, 0, 1}, {0, 1, 0, 1}, {1, 1, 1, 0}}, -1),
37+
38+
// Test case 7: Celebrity at the start of the matrix
39+
Arguments.of(new int[][] {{0, 0, 0}, {1, 0, 1}, {1, 1, 0}}, 0));
40+
}
41+
}

0 commit comments

Comments
 (0)