-
Notifications
You must be signed in to change notification settings - Fork 20k
Refactor BipartiteGraphDFS.java, add Junit tests #5606
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
19c492d
Refactor `BipartiteGraphDFS.java`, add Junit tests
Hardvan 635975f
Update directory
Hardvan 8545d78
Fix
Hardvan cd38eff
Merge remote-tracking branch 'origin/bipartite_add_tests' into bipart…
Hardvan c69f42c
Add suggested changes
Hardvan a11e629
Merge branch 'master' into bipartite_add_tests
alxkm 1776580
Update BipartiteGraphDFS.java
Hardvan da1edad
Merge branch 'master' into bipartite_add_tests
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
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
73 changes: 73 additions & 0 deletions
73
src/test/java/com/thealgorithms/datastructures/graphs/BipartiteGraphDFSTest.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,73 @@ | ||
package com.thealgorithms.datastructures.graphs; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.ArrayList; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class BipartiteGraphDFSTest { | ||
|
||
// Helper method to create an adjacency list from edges | ||
private ArrayList<ArrayList<Integer>> createAdjacencyList(int numVertices, int[][] edges) { | ||
ArrayList<ArrayList<Integer>> adj = new ArrayList<>(); | ||
for (int i = 0; i < numVertices; i++) { | ||
adj.add(new ArrayList<>()); | ||
} | ||
for (int[] edge : edges) { | ||
int vertexU = edge[0]; | ||
int vertexV = edge[1]; | ||
adj.get(vertexU).add(vertexV); | ||
adj.get(vertexV).add(vertexU); | ||
} | ||
return adj; | ||
} | ||
|
||
@Test | ||
public void testBipartiteGraphEvenCycle() { | ||
int numVertices = 4; | ||
int[][] edges = {{0, 1}, {1, 2}, {2, 3}, {3, 0}}; // Even cycle | ||
ArrayList<ArrayList<Integer>> adj = createAdjacencyList(numVertices, edges); | ||
assertTrue(BipartiteGraphDFS.isBipartite(numVertices, adj), "Graph should be bipartite (even cycle)"); | ||
} | ||
|
||
@Test | ||
public void testBipartiteGraphOddCycle() { | ||
int numVertices = 5; | ||
int[][] edges = {{0, 1}, {1, 2}, {2, 0}, {1, 3}, {3, 4}}; // Odd cycle | ||
ArrayList<ArrayList<Integer>> adj = createAdjacencyList(numVertices, edges); | ||
assertFalse(BipartiteGraphDFS.isBipartite(numVertices, adj), "Graph should not be bipartite (odd cycle)"); | ||
} | ||
|
||
@Test | ||
public void testBipartiteGraphDisconnected() { | ||
int numVertices = 6; | ||
int[][] edges = {{0, 1}, {2, 3}, {4, 5}}; // Disconnected bipartite graphs | ||
ArrayList<ArrayList<Integer>> adj = createAdjacencyList(numVertices, edges); | ||
assertTrue(BipartiteGraphDFS.isBipartite(numVertices, adj), "Graph should be bipartite (disconnected)"); | ||
} | ||
|
||
@Test | ||
public void testBipartiteGraphSingleVertex() { | ||
int numVertices = 1; | ||
int[][] edges = {}; // Single vertex, no edges | ||
ArrayList<ArrayList<Integer>> adj = createAdjacencyList(numVertices, edges); | ||
assertTrue(BipartiteGraphDFS.isBipartite(numVertices, adj), "Graph should be bipartite (single vertex)"); | ||
} | ||
|
||
@Test | ||
public void testBipartiteGraphCompleteBipartite() { | ||
int numVertices = 4; | ||
int[][] edges = {{0, 2}, {0, 3}, {1, 2}, {1, 3}}; // K2,2 (Complete bipartite graph) | ||
ArrayList<ArrayList<Integer>> adj = createAdjacencyList(numVertices, edges); | ||
assertTrue(BipartiteGraphDFS.isBipartite(numVertices, adj), "Graph should be bipartite (complete bipartite)"); | ||
} | ||
|
||
@Test | ||
public void testBipartiteGraphNonBipartite() { | ||
int numVertices = 3; | ||
int[][] edges = {{0, 1}, {1, 2}, {2, 0}}; // Triangle (odd cycle) | ||
ArrayList<ArrayList<Integer>> adj = createAdjacencyList(numVertices, edges); | ||
assertFalse(BipartiteGraphDFS.isBipartite(numVertices, adj), "Graph should not be bipartite (triangle)"); | ||
} | ||
} |
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.