Is Graph Bipartite?

Difficulty: Medium

You're given an undirected graph as an adjacency list: graph[i] lists the nodes connected to node i.

A graph is bipartite if you can split its nodes into two groups so that every edge connects the two groups, never two nodes in the same group.

Return true if the graph can be split this way, and false otherwise. The graph may be disconnected (several separate pieces).

Examples

Input: graph = [[1,3],[0,2],[1,3],[0,2]]
Output: true

This graph is a simple cycle 0-1-2-3-0. Put nodes 0 and 2 in group A, and nodes 1 and 3 in group B - every edge connects an A to a B.

Input: graph = [[1,2,3],[0,2],[0,1,3],[0,2]]
Output: false

Nodes 0, 1, and 2 are all directly connected to each other (a triangle). In any two-group split, at least two of those three nodes must land in the same group, which breaks the rule.

Input: graph = [[],[]]
Output: true

No edges at all means the rule is never violated, no matter how you split the nodes.

Constraints

  • 1 <= graph.length <= 100

  • The graph has no self-loops and no repeated edges.

Approach

This is a traversal problem disguised as coloring. Walk the graph (DFS or BFS), giving each node one of two colors when you first reach it. Every neighbor must get the opposite color.

Before visiting a neighbor, check it:

  • Uncolored: give it the opposite color and continue from it.
  • Already colored the same as the current node: two connected nodes are in the same group, so return false.

The graph can be disconnected, so restart the coloring from every node not yet visited. Each piece must pass the check.

Solutions

DFS - Two-Coloring

Color each node 1 or -1 when first visited. For each neighbor: if it has the same color, the graph isn't bipartite; if it's uncolored, give it the opposite color and recurse. Restart from any uncolored node to cover disconnected pieces.

function isBipartite(graph) {
  const n = graph.length;
  const color = new Array(n).fill(0); // 0 = uncolored, otherwise 1 or -1

  function dfs(node, c) {
    color[node] = c;

    for (const neighbor of graph[node]) {
      if (color[neighbor] === c) {
        return false;
      }
      if (color[neighbor] === 0 && !dfs(neighbor, -c)) {
        return false;
      }
    }

    return true;
  }

  for (let i = 0; i < n; i++) {
    if (color[i] === 0 && !dfs(i, 1)) {
      return false;
    }
  }

  return true;
}

Time: O(V + E) · Space: O(V)

BFS - Two-Coloring

Same coloring rule, layer by layer with a queue instead of recursion: color a starting node, then pop nodes and check or color their neighbors.

function isBipartite(graph) {
  const n = graph.length;
  const color = new Array(n).fill(0);

  for (let i = 0; i < n; i++) {
    if (color[i] !== 0) continue;

    color[i] = 1;
    const queue = [i];

    while (queue.length > 0) {
      const node = queue.shift();

      for (const neighbor of graph[node]) {
        if (color[neighbor] === color[node]) {
          return false;
        }
        if (color[neighbor] === 0) {
          color[neighbor] = -color[node];
          queue.push(neighbor);
        }
      }
    }
  }

  return true;
}

Time: O(V + E) · Space: O(V)