2
public boolean isBipartite(int[][] graph) {4
int[] colors = new int[n];6
// take two colors and color adjacent neighbours differently, if any of the neighbour is already7
// colored and is not a different color, but same color, then return false else true.8
for (int i = 0; i < n; i++) {9
if (colors[i] == 0) { // if not yet coloured.10
Queue<Integer> q = new LinkedList<>();12
colors[i] = 1; // color it with blue14
while (!q.isEmpty()) {15
int cur = q.poll(); // start polling the current node17
for (int x : graph[cur]) {18
if (colors[x] == 0) { // if not yet colored19
colors[x] = -colors[cur]; // Use a different color for the adjacent node23
cur]) // if the adjacent node/neighbour is already coloured and not a different24
// colour, we return false, as no two adjacent vertices must have same