LeetCode Problem

How to Solve Is Graph Bipartite?

To solve "Is Graph Bipartite?", traverse the graph using DFS, BFS, or Union Find while assigning colors to nodes. Track conflicts where adjacent nodes share the same color. Return true if all nodes can be colored without conflict, ensuring the graph forms two independent sets connected only across sets.

GhostInterview Help

Need help with Is Graph Bipartite? without spending extra time grinding it?

GhostInterview can read Is Graph Bipartite? from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #785Graph traversal with depth-first searchReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Graph traversal with depth-first search
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve "Is Graph Bipartite?", traverse the graph using DFS, BFS, or Union Find while assigning colors to nodes. Track conflicts where adjacent nodes share the same color. Return true if all nodes can be colored without conflict, ensuring the graph forms two independent sets connected only across sets.

Problem Statement

You are given an undirected graph with n nodes labeled from 0 to n - 1. The graph is represented as a 2D array graph where graph[u] contains all nodes adjacent to node u. Each edge is bidirectional and appears in both nodes' adjacency lists. Your task is to determine whether this graph can be partitioned into two sets where no two nodes in the same set are connected.

A graph is bipartite if its nodes can be divided into two independent sets A and B such that every edge connects a node from set A to a node from set B. Return true if such a partition exists and false otherwise. Consider the constraints that the graph length is between 1 and 100, edges are unique, and no node contains itself as a neighbor.

Examples

Example 1

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

Output: false

There is no way to partition the nodes into two independent sets such that every edge connects a node in one and a node in the other.

Example 2

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

Output: true

We can partition the nodes into two sets: {0, 2} and {1, 3}.

Constraints

  • graph.length == n
  • 1 <= n <= 100
  • 0 <= graph[u].length < n
  • 0 <= graph[u][i] <= n - 1
  • graph[u] does not contain u.
  • All the values of graph[u] are unique.
  • If graph[u] contains v, then graph[v] contains u.

Solution Approach

Depth-First Search Coloring

Perform a DFS traversal and assign alternating colors to nodes. If a neighbor has the same color during traversal, return false immediately. Continue recursively until all connected components are verified.

Breadth-First Search Leveling

Use BFS to traverse each component, assigning levels as pseudo-colors. Nodes at the same level represent one set, and adjacent levels represent the other. Detect conflicts if a neighbor is on the same level.

Union Find for Set Separation

Use a Union Find structure to group nodes into two sets. For each edge, union nodes into separate groups. If two connected nodes end up in the same group, the graph is not bipartite.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(V + E) for DFS or BFS traversal, as each node and edge is visited once. Union Find adds near O(1) operations per edge, so overall complexity remains O(V + E). Space complexity is O(V) for coloring or parent arrays, and O(V + E) for adjacency representation.

What Interviewers Usually Probe

  • Ask to explain why DFS or BFS coloring ensures bipartiteness.
  • Probe for handling disconnected components in the graph.
  • Check understanding of how Union Find can represent two disjoint sets for bipartite checking.

Common Pitfalls or Variants

Common pitfalls

  • Failing to initialize all nodes before DFS/BFS leading to missed components.
  • Overlooking that an edge might connect two nodes already in the same set, causing incorrect true result.
  • Confusing directed and undirected graph behavior when checking adjacency relationships.

Follow-up variants

  • Check bipartiteness in a weighted undirected graph while ignoring weights.
  • Determine if a graph with additional constraints like forbidden node pairs is bipartite.
  • Extend bipartite check to k-partite graph verification for multiple independent sets.

How GhostInterview Helps

  • Provides automated DFS, BFS, and Union Find templates tailored for Is Graph Bipartite?
  • Highlights coloring conflicts and traversal order errors that commonly cause false results.
  • Tracks multiple components and ensures edge validation across sets for correct bipartite determination.

Topic Pages

FAQ

What is the main pattern used in the Is Graph Bipartite? problem?

The primary pattern is graph traversal with DFS or BFS combined with coloring to detect conflicts between connected nodes.

Can Union Find efficiently check bipartiteness for disconnected graphs?

Yes, Union Find can manage multiple components by grouping nodes and checking for conflicts within each disjoint set.

How do I handle graphs with isolated nodes?

Isolated nodes do not affect bipartiteness and can be colored arbitrarily without causing conflicts.

Why might DFS fail if implemented incorrectly for bipartite checking?

DFS may miss a component or incorrectly assign colors if the recursion does not cover all nodes, leading to false positives.

What is the difference between BFS leveling and DFS coloring in this problem?

BFS uses levels to assign pseudo-colors iteratively, while DFS assigns colors recursively; both detect conflicts but traversal order differs.

GhostInterview Solver

Need direct help with Is Graph Bipartite? instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Is Graph Bipartite? from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.