// https://leetcode.com/problems/is-graph-bipartite/ import java.util.*; import java.io.*; class BipartiteGraphUsingBFSandDFS { // BFS public static boolean bfs(int node, int color, int colorArray[], ArrayList> adj){ Queue q = new LinkedList<>(); q.add(node); colorArray[node] = color; while(!q.isEmpty()){ Integer currNodeode = q.poll(); for(Integer it : adj.get(currNodeode)){ // if(!visited[it]){ // visited[it] = true; // q.add(it); // } if (colorArray[it]==-1){ // not colored yet (not visited basically) colorArray[it] = 1 - colorArray[currNodeode]; q.add(it); } else if (colorArray[it]==colorArray[currNodeode]){ return false; } } } return true; } // DFS public static boolean dfs(int node, int color, int colorArray[], ArrayList> adj){ colorArray[node] = color; for (Integer it: adj.get(node)){ if (colorArray[it]==-1){ // not colored yet (not visited basically) if (dfs(it,1-color, colorArray, adj)==false) return false; } else if (colorArray[it]==color){ return false; } } return true; } public static boolean isBipartite(int graph[][]){ int V = graph.length; ArrayList> adj = new ArrayList<>(); for(int i=0;i()); for(int u=0; u