There is an undirected graph with n nodes, where each node is numbered between 0 and n - 1. You are given a 2D array graph, where graph[u] is an array of nodes that node u is adjacent to.

More formally, for each v in graph[u], there is an undirected edge between node u and node v.

The graph has the following properties:

  • There are no self-edges (graph[u] does not contain u)
  • There are no parallel edges (graph[u] does not contain duplicate values)
  • If v is in graph[u], then u is in graph[v] (the graph is undirected)
  • The graph may not be connected

A graph is bipartite if the nodes can be partitioned into two independent sets A and B such that every edge in the graph connects a node in set A and a node in set B.

Return true if and only if it is bipartite.

Input & Output

Example 1 — Simple Bipartite Graph
$ Input: graph = [[1,2,3],[0,2],[0,1,3],[0,2]]
Output: false
💡 Note: This graph has edges: 0-1, 0-2, 0-3, 1-2, 2-3. It contains a triangle (3-cycle): 0-1-2-0. Since triangles are odd cycles, the graph cannot be bipartite. If we try to color 0 as blue, then 1 and 2 must be red, but since 1 and 2 are connected, they cannot both be red.
Example 2 — Tree Structure
$ Input: graph = [[1,3],[0,2],[1,3],[0,2]]
Output: true
💡 Note: This forms a 4-cycle: 0-1-2-3-0. We can color it as: Set A = {0,2}, Set B = {1,3}. All edges connect different sets, so it's bipartite.
Example 3 — Odd Cycle
$ Input: graph = [[1,2],[0,2],[0,1]]
Output: false
💡 Note: This is a triangle (3-cycle). Triangles are odd cycles and cannot be bipartite. If we color 0 as blue, then 1 and 2 must be red, but 1 and 2 are connected.

Constraints

  • 1 ≤ graph.length ≤ 100
  • 0 ≤ graph[u].length < graph.length
  • 0 ≤ graph[u][i] ≤ graph.length - 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

Visualization

Tap to expand
Is Graph Bipartite? - DFS Graph Coloring INPUT Graph Structure (n=4 nodes) 0 1 2 3 Adjacency List: graph[0] = [1,2,3] graph[1] = [0,2] graph[2] = [0,1,3] graph[3] = [0,2] ALGORITHM STEPS 1 Start DFS from node 0 Color node 0 with RED 2 Color neighbors BLUE Nodes 1,2,3 get BLUE 3 Check node 1's neighbors Node 2 is BLUE (same!) 4 Conflict detected! Edge 1-2 has same color Coloring Attempt: 0 1 2 3 CONFLICT! FINAL RESULT Cannot split into 2 sets Set A (RED) 0 Set B (BLUE) 1 2 3 Edge 1-2 connects nodes within same set! Output: false Key Insight: A graph is bipartite if and only if it contains no odd-length cycles. Use DFS to 2-color the graph: assign alternating colors to neighbors. If any adjacent nodes have the same color, return false. Time: O(V+E) | Space: O(V) for color array and recursion stack. TutorialsPoint - Is Graph Bipartite? | DFS Graph Coloring Approach
Asked in
Google 15 Amazon 12 Microsoft 10 Facebook 8
78.0K Views
Medium Frequency
~25 min Avg. Time
2.1K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen