Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c69f42c

Browse files
committedOct 6, 2024·
Add suggested changes
1 parent cd38eff commit c69f42c

File tree

1 file changed

+28
-45
lines changed

1 file changed

+28
-45
lines changed
 

‎src/main/java/com/thealgorithms/datastructures/graphs/BipartiteGraphDFS.java

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,29 @@
77
import java.util.Arrays;
88

99
/**
10-
* This class provides a method to check if a given graph is bipartite using Depth-First Search (DFS).
11-
* A bipartite graph is a graph whose vertices can be divided into two disjoint sets such that no two graph vertices
12-
* within the same set are adjacent.
10+
* This class provides a method to check if a given undirected graph is bipartite using Depth-First Search (DFS).
11+
* A bipartite graph is a graph whose vertices can be divided into two disjoint sets such that no two vertices
12+
* within the same set are adjacent. In other words, all edges must go between the two sets.
13+
*
14+
* The implementation leverages DFS to attempt to color the graph using two colors. If we can color the graph such
15+
* that no two adjacent vertices have the same color, the graph is bipartite.
1316
*
1417
* Example:
15-
* Input : {{0, 1, 0, 1}, {1, 0, 1, 0}, {0, 1, 0, 1}, {1, 0, 1, 0}}
16-
* Output : YES
18+
* Input (Adjacency Matrix):
19+
* {{0, 1, 0, 1},
20+
* {1, 0, 1, 0},
21+
* {0, 1, 0, 1},
22+
* {1, 0, 1, 0}}
23+
*
24+
* Output: YES (This graph is bipartite)
25+
*
26+
* Input (Adjacency Matrix):
27+
* {{0, 1, 1, 0},
28+
* {1, 0, 1, 0},
29+
* {1, 1, 0, 1},
30+
* {0, 0, 1, 0}}
31+
*
32+
* Output: NO (This graph is not bipartite)
1733
*/
1834
public final class BipartiteGraphDFS {
1935
private BipartiteGraphDFS() {
@@ -22,11 +38,14 @@ private BipartiteGraphDFS() {
2238
/**
2339
* Helper method to perform DFS and check if the graph is bipartite.
2440
*
41+
* During DFS traversal, this method attempts to color each vertex in such a way
42+
* that no two adjacent vertices share the same color.
43+
*
2544
* @param v Number of vertices in the graph
26-
* @param adj Adjacency list of the graph
27-
* @param color Array to store colors assigned to vertices
28-
* @param node Current node being processed
29-
* @return True if the graph is bipartite, otherwise false
45+
* @param adj Adjacency list of the graph where each index i contains a list of adjacent vertices
46+
* @param color Array to store the color assigned to each vertex (-1 indicates uncolored)
47+
* @param node Current vertex being processed
48+
* @return True if the graph (or component of the graph) is bipartite, otherwise false
3049
*/
3150
private static boolean bipartite(int v, ArrayList<ArrayList<Integer>> adj, int[] color, int node) {
3251
if (color[node] == -1) {
@@ -53,7 +72,6 @@ private static boolean bipartite(int v, ArrayList<ArrayList<Integer>> adj, int[]
5372
* @return True if the graph is bipartite, otherwise false
5473
*/
5574
public static boolean isBipartite(int v, ArrayList<ArrayList<Integer>> adj) {
56-
// Code here
5775
int[] color = new int[v + 1];
5876
Arrays.fill(color, -1);
5977
for (int i = 0; i < v; i++) {
@@ -65,39 +83,4 @@ public static boolean isBipartite(int v, ArrayList<ArrayList<Integer>> adj) {
6583
}
6684
return true;
6785
}
68-
69-
/**
70-
* Main method to read input and check if the graph is bipartite.
71-
*
72-
* @param args Command line arguments
73-
* @throws IOException If an I/O error occurs
74-
*/
75-
public static void main(String[] args) throws IOException {
76-
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
77-
int t = Integer.parseInt(read.readLine().trim());
78-
while (t-- > 0) {
79-
String[] str1 = read.readLine().trim().split(" ");
80-
int numVertices = Integer.parseInt(str1[0]);
81-
int numEdges = Integer.parseInt(str1[1]);
82-
83-
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
84-
for (int i = 0; i < numVertices; i++) {
85-
adj.add(new ArrayList<>());
86-
}
87-
for (int i = 0; i < numEdges; i++) {
88-
String[] str2 = read.readLine().trim().split(" ");
89-
int vertexU = Integer.parseInt(str2[0]);
90-
int vertexV = Integer.parseInt(str2[1]);
91-
adj.get(vertexU).add(vertexV);
92-
adj.get(vertexV).add(vertexU);
93-
}
94-
95-
boolean ans = isBipartite(numVertices, adj);
96-
if (ans) {
97-
System.out.println("YES");
98-
} else {
99-
System.out.println("NO");
100-
}
101-
}
102-
}
10386
}

0 commit comments

Comments
 (0)
Please sign in to comment.