-
Notifications
You must be signed in to change notification settings - Fork 20.5k
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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4c1ba0f
feat: Add `CelebrityFinder` new algorithm with Junit tests
Hardvan 77f2e9c
Update directory
Hardvan c201cfd
Fix
Hardvan 6bb9819
Merge remote-tracking branch 'origin/celeb_new_algo' into celeb_new_algo
Hardvan 564bae0
Add suggested changes
Hardvan 6b18afa
Merge branch 'master' into celeb_new_algo
alxkm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/com/thealgorithms/stacks/CelebrityFinder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.thealgorithms.stacks; | ||
|
||
/** | ||
* 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. | ||
* | ||
* @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) { | ||
int n = party.length; | ||
int candidate = 0; | ||
|
||
// Find a potential celebrity | ||
for (int i = 1; i < n; i++) { | ||
if (party[candidate][i] == 1) { | ||
candidate = i; | ||
} | ||
} | ||
|
||
// Verify the candidate | ||
for (int i = 0; i < n; i++) { | ||
if (i != candidate && (party[candidate][i] == 1 || party[i][candidate] == 0)) { | ||
return -1; | ||
} | ||
} | ||
return candidate; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/java/com/thealgorithms/stacks/CelebrityFinderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.thealgorithms.stacks; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class CelebrityFinderTest { | ||
|
||
@Test | ||
Hardvan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
public void testCelebrityExists() { | ||
int[][] party = {{0, 1, 1}, {0, 0, 1}, {0, 0, 0}}; | ||
assertEquals(2, CelebrityFinder.findCelebrity(party)); | ||
} | ||
|
||
@Test | ||
public void testNoCelebrity() { | ||
int[][] party = {{0, 1, 0}, {1, 0, 1}, {1, 1, 0}}; | ||
assertEquals(-1, CelebrityFinder.findCelebrity(party)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.