11package com .thealgorithms .datastructures .graphs ;
22
3- import java .io .BufferedReader ;
4- import java .io .IOException ;
5- import java .io .InputStreamReader ;
63import java .util .ArrayList ;
74import 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 */
1731public 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}
0 commit comments