Skip to content
AI360Xpert
Beta
Difficulty: MediumGraphs

Is Graph Bipartite?

Problem in Plain English

Given an adjacency list of an undirected graph, decide whether its nodes split into two groups with every edge crossing between groups. Odd-length cycles make this impossible.

Problem Statement

You are given an undirected graph with n nodes (0 to n - 1) as an adjacency list graph, where graph[u] lists the neighbours of u.

Return true exactly when the graph is bipartite: its nodes split into two sets A and B so that every edge joins a node in A to a node in B.

Constraints

  • graph.length equals n.
  • 1≤n≤1001 \le n \le 100
  • 0≤graph[u].length<n0 \le \text{graph[u].length} < n
  • 0≤graph[u][i]<n0 \le \text{graph[u][i]} < n
  • No self-edges: graph[u] does not contain u.
  • No parallel edges: every value in graph[u] is unique.
  • If graph[u] contains v, then graph[v] contains u; the graph is undirected.

Examples

Example 1

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

Nodes 0, 1 and 2 form a triangle, and three mutually adjacent nodes cannot fit into two groups.

Example 2

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

Split into {0, 2} and {1, 3}: every edge runs from one group to the other.

Example 3

Input
graph = [[]]
Output
true

There are no edges to violate the rule. The isolated node may be assigned to either group; the other group may be empty.

Example 4

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

The component containing 0 and 1 splits cleanly, but nodes 2, 3 and 4 form a triangle. Every component must be bipartite, so checking only node 0's component would miss the failure.

Example 5

Input
graph = [[],[2],[1],[]]
Output
true

The single edge 1-2 puts its endpoints in opposite groups. Isolated nodes 0 and 3 impose no additional constraints.

Intuition

Place one node in group 0 and every neighbor in group 1; repeat this forced alternation along paths. A triangle creates a contradiction because two adjacent neighbors of 0 must both receive group 1. Example 1 exposes exactly that conflict. Different connected components can choose their starting group independently, so the traversal must restart at every unassigned node. The numbers 0 and 1 are group labels, and -1 means unassigned.

Approaches

BFS Coloring

Solution Details

Reveal BFS Coloring: intuition, complexity, and code

Hints

Hint 1
Think of group 0 and group 1 as two labels. If a node uses one label, what must every neighbor use?
Hint 2
A triangle makes the forced assignments collide. Detect that collision by comparing the labels on every already assigned edge.
Hint 3
Start another traversal whenever the outer scan finds an unassigned vertex. The graph may be disconnected, and every component must pass.

Edge Cases

  • A single isolated node and an entirely edgeless graph return true; empty groups are allowed.
  • A disconnected graph fails if any component contains an odd cycle, even when node 0 belongs to a valid component. Example 4 illustrates this.
  • An even cycle such as Example 2 is bipartite: a cycle does not automatically mean failure. An odd cycle forces a same-group closing edge.
  • A 100-node path reaches the maximum recursive depth; a complete bipartite graph with 50 vertices per group checks many edges without conflict.
  • Self-edges and repeated neighbors are excluded by the input contract. A defensive same-group check would reject a self-edge, but authored examples should use valid inputs.

Common Mistakes and Interview Tips

  • Checking only the component containing node 0; a later triangle can make the whole graph fail.
  • Treating any cycle as failure. Bipartiteness rejects odd cycles, while a four-node cycle has a valid alternating partition.
  • Using 0 for both unassigned and group 0. Keep -1 as the unassigned sentinel so a legitimate group is never mistaken for unseen.
  • Marking a node only when it leaves the BFS queue can enqueue it many times. Assign the group before enqueueing, and use a head index in JavaScript instead of shift().
  • Ignoring the false result of a recursive child, which allows a detected contradiction to disappear on the way back to the caller.
  • Interview tip: explain why flipping all groups in one connected component cannot resolve a same-group edge, and why separate components may choose their starting labels independently.

Key Takeaway

Bipartiteness is two-colouring by force: propagate opposite colours and read the first conflict as proof of an odd cycle.