Is Graph Bipartite?
Medium Graphs
graph describes an undirected graph of n nodes, numbered from 0 to n - 1, where n is the length of graph: graph[u] lists the nodes linked to node u, and each link appears in both lists of the two nodes it joins. The graph may be in several pieces, and a node may have no links at all.
The graph is bipartite when its nodes can be split into two groups so that every link joins a node of one group to a node of the other, and no link stays inside a group.
Return true if the graph is bipartite and false if it is not.
Example 1
Put 0, 2 and 4 in one group and 1, 3 and 5 in the other. Each of the six links, around the ring 0, 1, 2, 3 and on from 3 to 4 and from 4 to 5, joins the two groups.
Example 2
Example 3
Constraints
1 ≤ graph.length ≤ 10⁵0 ≤ graph[u][i] < graph.length, andgraph[u]does not holdu- No node appears twice in one list, and
vis ingraph[u]exactly whenuis ingraph[v] - The lists hold at most
2 × 10⁵numbers in all, so there are at most10⁵links
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundMore Graphs
- Number of IslandsAI round · Medium
- Rotting OrangesAI round · Medium
- Word LadderAI round · Hard
- Graph Valid TreeMedium
- Walls and GatesMedium