Is Graph Bipartite?

There is an undirected graph with n nodes, numbered between 0 and n-1. You are provided with a 2D array graph, where graph[u] is an array of nodes adjacent to node u. The graph is undirected, has no self-edges, and no parallel edges. It may not be connected. A graph is bipartite if its nodes can be divided into two independent sets A and B such that every edge connects one node in set A and one node in set B. Return true if and only if the graph is bipartite.

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.
Saving… · 2 lines
class Solution:
def isGraphBipartite(self, graph: List[List[int]]) -> bool:
Console

Click Run to execute sample tests.