Skip to content

Commit 387707f

Browse files
Hardvanalxkm
andauthored
Refactor BipartiteGraphDFS.java, add Junit tests (#5606)
* Refactor `BipartiteGraphDFS.java`, add Junit tests * Update directory * Fix * Add suggested changes * Update BipartiteGraphDFS.java --------- Co-authored-by: Hardvan <[email protected]> Co-authored-by: Alex Klymenko <[email protected]>
1 parent 2001a09 commit 387707f

File tree

3 files changed

+114
-38
lines changed

3 files changed

+114
-38
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@
676676
* dynamicarray
677677
* [DynamicArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/dynamicarray/DynamicArrayTest.java)
678678
* graphs
679+
* [BipartiteGraphDFSTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/BipartiteGraphDFSTest.java)
679680
* [BoruvkaAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithmTest.java)
680681
* [DijkstraAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithmTest.java)
681682
* [EdmondsBlossomAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/EdmondsBlossomAlgorithmTest.java)
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
11
package com.thealgorithms.datastructures.graphs;
22

3-
import java.io.BufferedReader;
4-
import java.io.IOException;
5-
import java.io.InputStreamReader;
63
import java.util.ArrayList;
74
import java.util.Arrays;
85

96
/**
10-
* Given an adjacency list of a graph adj of V no. of vertices having 0 based
11-
* index. Check whether the graph is bipartite or not.
7+
* This class provides a method to check if a given undirected graph is bipartite using Depth-First Search (DFS).
8+
* A bipartite graph is a graph whose vertices can be divided into two disjoint sets such that no two vertices
9+
* within the same set are adjacent. In other words, all edges must go between the two sets.
1210
*
13-
* Input : {{0, 1, 0, 1}, {1, 0, 1, 0}, {0, 1, 0, 1}, {1, 0, 1, 0}}
11+
* The implementation leverages DFS to attempt to color the graph using two colors. If we can color the graph such
12+
* that no two adjacent vertices have the same color, the graph is bipartite.
1413
*
15-
* Output : YES
14+
* Example:
15+
* Input (Adjacency Matrix):
16+
* {{0, 1, 0, 1},
17+
* {1, 0, 1, 0},
18+
* {0, 1, 0, 1},
19+
* {1, 0, 1, 0}}
20+
*
21+
* Output: YES (This graph is bipartite)
22+
*
23+
* Input (Adjacency Matrix):
24+
* {{0, 1, 1, 0},
25+
* {1, 0, 1, 0},
26+
* {1, 1, 0, 1},
27+
* {0, 0, 1, 0}}
28+
*
29+
* Output: NO (This graph is not bipartite)
1630
*/
1731
public final class BipartiteGraphDFS {
1832
private BipartiteGraphDFS() {
1933
}
2034

35+
/**
36+
* Helper method to perform DFS and check if the graph is bipartite.
37+
*
38+
* During DFS traversal, this method attempts to color each vertex in such a way
39+
* that no two adjacent vertices share the same color.
40+
*
41+
* @param v Number of vertices in the graph
42+
* @param adj Adjacency list of the graph where each index i contains a list of adjacent vertices
43+
* @param color Array to store the color assigned to each vertex (-1 indicates uncolored)
44+
* @param node Current vertex being processed
45+
* @return True if the graph (or component of the graph) is bipartite, otherwise false
46+
*/
2147
private static boolean bipartite(int v, ArrayList<ArrayList<Integer>> adj, int[] color, int node) {
2248
if (color[node] == -1) {
2349
color[node] = 1;
@@ -35,11 +61,16 @@ private static boolean bipartite(int v, ArrayList<ArrayList<Integer>> adj, int[]
3561
return true;
3662
}
3763

64+
/**
65+
* Method to check if the graph is bipartite.
66+
*
67+
* @param v Number of vertices in the graph
68+
* @param adj Adjacency list of the graph
69+
* @return True if the graph is bipartite, otherwise false
70+
*/
3871
public static boolean isBipartite(int v, ArrayList<ArrayList<Integer>> adj) {
39-
// Code here
4072
int[] color = new int[v + 1];
4173
Arrays.fill(color, -1);
42-
4374
for (int i = 0; i < v; i++) {
4475
if (color[i] == -1) {
4576
if (!bipartite(v, adj, color, i)) {
@@ -49,33 +80,4 @@ public static boolean isBipartite(int v, ArrayList<ArrayList<Integer>> adj) {
4980
}
5081
return true;
5182
}
52-
53-
public static void main(String[] args) throws IOException {
54-
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
55-
int t = Integer.parseInt(read.readLine().trim());
56-
while (t-- > 0) {
57-
String[] str1 = read.readLine().trim().split(" ");
58-
int numVertices = Integer.parseInt(str1[0]);
59-
int numEdges = Integer.parseInt(str1[1]);
60-
61-
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
62-
for (int i = 0; i < numVertices; i++) {
63-
adj.add(new ArrayList<>());
64-
}
65-
for (int i = 0; i < numEdges; i++) {
66-
String[] str2 = read.readLine().trim().split(" ");
67-
int vertexU = Integer.parseInt(str2[0]);
68-
int vertexV = Integer.parseInt(str2[1]);
69-
adj.get(vertexU).add(vertexV);
70-
adj.get(vertexV).add(vertexU);
71-
}
72-
73-
boolean ans = isBipartite(numVertices, adj);
74-
if (ans) {
75-
System.out.println("YES");
76-
} else {
77-
System.out.println("NO");
78-
}
79-
}
80-
}
8183
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.util.ArrayList;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class BipartiteGraphDFSTest {
10+
11+
// Helper method to create an adjacency list from edges
12+
private ArrayList<ArrayList<Integer>> createAdjacencyList(int numVertices, int[][] edges) {
13+
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
14+
for (int i = 0; i < numVertices; i++) {
15+
adj.add(new ArrayList<>());
16+
}
17+
for (int[] edge : edges) {
18+
int vertexU = edge[0];
19+
int vertexV = edge[1];
20+
adj.get(vertexU).add(vertexV);
21+
adj.get(vertexV).add(vertexU);
22+
}
23+
return adj;
24+
}
25+
26+
@Test
27+
public void testBipartiteGraphEvenCycle() {
28+
int numVertices = 4;
29+
int[][] edges = {{0, 1}, {1, 2}, {2, 3}, {3, 0}}; // Even cycle
30+
ArrayList<ArrayList<Integer>> adj = createAdjacencyList(numVertices, edges);
31+
assertTrue(BipartiteGraphDFS.isBipartite(numVertices, adj), "Graph should be bipartite (even cycle)");
32+
}
33+
34+
@Test
35+
public void testBipartiteGraphOddCycle() {
36+
int numVertices = 5;
37+
int[][] edges = {{0, 1}, {1, 2}, {2, 0}, {1, 3}, {3, 4}}; // Odd cycle
38+
ArrayList<ArrayList<Integer>> adj = createAdjacencyList(numVertices, edges);
39+
assertFalse(BipartiteGraphDFS.isBipartite(numVertices, adj), "Graph should not be bipartite (odd cycle)");
40+
}
41+
42+
@Test
43+
public void testBipartiteGraphDisconnected() {
44+
int numVertices = 6;
45+
int[][] edges = {{0, 1}, {2, 3}, {4, 5}}; // Disconnected bipartite graphs
46+
ArrayList<ArrayList<Integer>> adj = createAdjacencyList(numVertices, edges);
47+
assertTrue(BipartiteGraphDFS.isBipartite(numVertices, adj), "Graph should be bipartite (disconnected)");
48+
}
49+
50+
@Test
51+
public void testBipartiteGraphSingleVertex() {
52+
int numVertices = 1;
53+
int[][] edges = {}; // Single vertex, no edges
54+
ArrayList<ArrayList<Integer>> adj = createAdjacencyList(numVertices, edges);
55+
assertTrue(BipartiteGraphDFS.isBipartite(numVertices, adj), "Graph should be bipartite (single vertex)");
56+
}
57+
58+
@Test
59+
public void testBipartiteGraphCompleteBipartite() {
60+
int numVertices = 4;
61+
int[][] edges = {{0, 2}, {0, 3}, {1, 2}, {1, 3}}; // K2,2 (Complete bipartite graph)
62+
ArrayList<ArrayList<Integer>> adj = createAdjacencyList(numVertices, edges);
63+
assertTrue(BipartiteGraphDFS.isBipartite(numVertices, adj), "Graph should be bipartite (complete bipartite)");
64+
}
65+
66+
@Test
67+
public void testBipartiteGraphNonBipartite() {
68+
int numVertices = 3;
69+
int[][] edges = {{0, 1}, {1, 2}, {2, 0}}; // Triangle (odd cycle)
70+
ArrayList<ArrayList<Integer>> adj = createAdjacencyList(numVertices, edges);
71+
assertFalse(BipartiteGraphDFS.isBipartite(numVertices, adj), "Graph should not be bipartite (triangle)");
72+
}
73+
}

0 commit comments

Comments
 (0)