7
7
import java .util .Arrays ;
8
8
9
9
/**
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.
13
16
*
14
17
* 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)
17
33
*/
18
34
public final class BipartiteGraphDFS {
19
35
private BipartiteGraphDFS () {
@@ -22,11 +38,14 @@ private BipartiteGraphDFS() {
22
38
/**
23
39
* Helper method to perform DFS and check if the graph is bipartite.
24
40
*
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
+ *
25
44
* @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
30
49
*/
31
50
private static boolean bipartite (int v , ArrayList <ArrayList <Integer >> adj , int [] color , int node ) {
32
51
if (color [node ] == -1 ) {
@@ -53,7 +72,6 @@ private static boolean bipartite(int v, ArrayList<ArrayList<Integer>> adj, int[]
53
72
* @return True if the graph is bipartite, otherwise false
54
73
*/
55
74
public static boolean isBipartite (int v , ArrayList <ArrayList <Integer >> adj ) {
56
- // Code here
57
75
int [] color = new int [v + 1 ];
58
76
Arrays .fill (color , -1 );
59
77
for (int i = 0 ; i < v ; i ++) {
@@ -65,39 +83,4 @@ public static boolean isBipartite(int v, ArrayList<ArrayList<Integer>> adj) {
65
83
}
66
84
return true ;
67
85
}
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
- }
103
86
}
0 commit comments