Is Graph Bipartite?
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. Return true if and only if it is bipartite.

30:00

Is Graph Bipartite?
medium
Topics
Companies

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. Return true if and only if it is bipartite.

Example 1:
Input: [[1,3],[0,2],[1,3],[0,2]]
Output: true
Constraints:
  • graph.length==n\text{graph.length} == n

  • 1n1001 \leq n \leq 100

  • 0graph[u].length<n0 \leq \text{graph}[u].\text{length} < n

  • 0graph[u][i]n10 \leq \text{graph}[u][i] \leq n - 1

  • graph[u]\text{graph}[u] does not contain uu.

  • All values of graph[u] are unique.

  • If graph[u] contains vv, then graph[v] contains uu.

Input
arr =[[1,3],[0,2],[1,3],[0,2]]

Check if graph is bipartite using BFS coloring

0

1

2

3

Graph is Bipartite

Color 0 (Blue)

Color 1 (Red)

Current

Uncolored

Variables
No variables to display
DepthFunction Call
Stack empty
0/5