1
1
package com .thealgorithms .datastructures .graphs ;
2
2
3
- import java .io .BufferedReader ;
4
- import java .io .IOException ;
5
- import java .io .InputStreamReader ;
6
3
import java .util .ArrayList ;
7
4
import java .util .Arrays ;
8
5
9
6
/**
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.
12
10
*
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.
14
13
*
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)
16
30
*/
17
31
public final class BipartiteGraphDFS {
18
32
private BipartiteGraphDFS () {
19
33
}
20
34
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
+ */
21
47
private static boolean bipartite (int v , ArrayList <ArrayList <Integer >> adj , int [] color , int node ) {
22
48
if (color [node ] == -1 ) {
23
49
color [node ] = 1 ;
@@ -35,11 +61,16 @@ private static boolean bipartite(int v, ArrayList<ArrayList<Integer>> adj, int[]
35
61
return true ;
36
62
}
37
63
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
+ */
38
71
public static boolean isBipartite (int v , ArrayList <ArrayList <Integer >> adj ) {
39
- // Code here
40
72
int [] color = new int [v + 1 ];
41
73
Arrays .fill (color , -1 );
42
-
43
74
for (int i = 0 ; i < v ; i ++) {
44
75
if (color [i ] == -1 ) {
45
76
if (!bipartite (v , adj , color , i )) {
@@ -49,33 +80,4 @@ public static boolean isBipartite(int v, ArrayList<ArrayList<Integer>> adj) {
49
80
}
50
81
return true ;
51
82
}
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
- }
81
83
}
0 commit comments