Bipartite - BFS and DFS


Link : is graph Bipartite?


Definition

A graph is bipartite if you can split its set of vertices into two disjoint sets such that:

Formally, a graph G(V, E) is bipartite if its vertices can be colored with 2 colors such that no two adjacent vertices share the same color.


Intuition

Imagine you're trying to seat people at two tables (Set A and Set B) such that no pair of friends sits at the same table.
If this is possible for all people (nodes), the graph is bipartite.

This is essentially a graph coloring problem:


Applications


Idea


Java Code: Bipartite Check using BFS

class Solution {
    public boolean isBipartite(int[][] graph) {
        int n = graph.length;
        int[] color = new int[n];
        Arrays.fill(color, -1); // -1 = unvisited

        for (int i = 0; i < n; i++) {
            if (color[i] == -1) {
                if (!bfsCheck(i, graph, color)) return false;
            }
        }
        return true;
    }

    private boolean bfsCheck(int node, int[][] graph, int[] color) {
        Queue<Integer> q = new LinkedList<>();
        q.offer(node);
        color[node] = 0;

        while (!q.isEmpty()) {
            int curr = q.poll();
            for (int neighbor : graph[curr]) {
                if (color[neighbor] == -1) {
                    color[neighbor] = 1 - color[curr];
                    q.offer(neighbor);
                } else if (color[neighbor] == color[curr]) {
                    return false;
                }
            }
        }
        return true;
    }
}

Dry Run

Graph (adjacency list):

0 → 1, 3  
1 → 0, 2  
2 → 1, 3  
3 → 0, 2

BFS from node 0:

All neighbors colored correctly → Bipartite: True


Time Complexity

Space Complexity


Idea


Java Code: Bipartite Check using DFS

class Solution {
    public boolean isBipartite(int[][] graph) {
        int n = graph.length;
        int[] color = new int[n];
        Arrays.fill(color, -1); // uncolored

        for (int i = 0; i < n; i++) {
            if (color[i] == -1) {
                if (!dfsCheck(i, 0, color, graph)) return false;
            }
        }
        return true;
    }

    private boolean dfsCheck(int node, int col, int[] color, int[][] graph) {
        color[node] = col;
        for (int neighbor : graph[node]) {
            if (color[neighbor] == -1) {
                if (!dfsCheck(neighbor, 1 - col, color, graph)) return false;
            } else if (color[neighbor] == col) {
                return false;
            }
        }
        return true;
    }
}

Dry Run

Graph:

0 → 1, 3  
1 → 0, 2  
2 → 1, 3  
3 → 0, 2

DFS:

No color conflict → Bipartite: True


Time Complexity

Space Complexity


How to Convert From Edge List to Adjacency List

If input is like:

int[][] edges = {
  {0, 1},
  {1, 2},
  {2, 3},
  {3, 0}
};

Convert to adjacency list:

List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
for (int[] edge : edges) {
    adj.get(edge[0]).add(edge[1]);
    adj.get(edge[1]).add(edge[0]); // undirected
}

Detecting Non-Bipartite Graph

A graph is not bipartite if:

Example:

0 — 1
 \  |
   2

Cycle: 0 → 1 → 2 → 0 (length 3) ⇒ odd ⇒ Not Bipartite


Final Notes

Aspect BFS DFS
Coloring method Queue-based Recursion
Easier to visualize Yes -
Stack overflow risk - Yes (for deep recursion)
Detects bipartite Yes Yes