Is Graph Bipartite?
Given an undirected graph as an adjacency list (graph[u] lists the neighbors of node u), decide whether it is bipartite — whether the nodes can be split into two sets so that every edge connects a node in one set to a node in the other. Return true if such a split exists.
Open official problem prompt ↗Determine whether the graph's nodes admit a red/blue labeling in which no edge joins two nodes of the same color.
Seat rivals at a two-sided table: each person must sit across from everyone they conflict with. If you can seat everyone with all rivalries spanning the table, it works; if two rivals are forced onto the same side, it is impossible.
- Input
- graph = [[1,3],[0,2],[1,3],[0,2]]
- Output
- true
- Why
- Coloring nodes {0,2} one color and {1,3} the other leaves every edge between the two colors, so the graph is bipartite.
graph.length == n1 <= n <= 1000 <= graph[u].length < n0 <= graph[u][i] <= n - 1The graph is undirected and has no self-edges or parallel edgesIt may be disconnected