Skip to content
Problem 785 · LeetCode 785(opens in a new tab)

Is Graph Bipartite?

MediumBreadth-first search

You get an undirected graph with n nodes labelled 0 to n − 1, as an adjacency list: graph[u] lists every node that shares an edge with u. There are no self-loops or repeated edges, every edge is listed from both ends, and the graph may be in several disconnected pieces.

Decide whether the nodes can be split into two groups so that every edge runs between the groups and none stays inside one. Return true if such a split exists, otherwise false.

Example 1

Input:  graph = [[1, 2], [0, 3], [0, 3], [1, 2, 4], [3]]
Output: true
Why:    groups {0, 3} and {1, 2, 4}; every edge crosses
2 more examples and the constraints

Example 2

Input:  graph = [[1, 4], [0, 2], [1, 3], [2, 4], [3, 0]]
Output: false
Why:    the five nodes form one ring of odd length

Example 3

Input:  graph = [[], [2], [1]]
Output: true
Why:    node 0 has no edges and can join either group

Constraints

  • 1 ≤ n ≤ 100
  • 0 ≤ length of graph[u] < n
  • graph[u] never contains u and has no repeats
  • v is in graph[u] exactly when u is in graph[v]

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Union-Find on neighboursO(e · α(n))
  2. BFS two-colouringO(n + e)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 100 nodesVerdict
Union-Find on neighboursO(e · α(n))
Works; harder to explain
BFS two-colouringO(n + e)
Best; also gives the split

n = number of nodes, e = number of edges, α = the inverse Ackermann function, below 5 in practice.

Edge cases to test

  • Disconnected graph. [[1], [0], [3, 4], [2, 4], [2, 3]] is false: only the second piece has a triangle. Start BFS from every uncoloured node.
  • Nodes with no edges. [[], [2], [1]] is true; an isolated node fits either side.
  • A single node. [[]] is true.
  • Even ring. A ring of 100 nodes alternates colours cleanly and is true.
  • Odd ring. A ring of 5, or of 99, can never be split.

Common mistakes

  • Running BFS only from node 0, which misses an odd cycle in another piece.
  • Using 0 to mean both "uncoloured" and "colour 0".
  • Returning false when a neighbour already has a colour, instead of only when it has the same colour.

How to talk through it

  1. 1Clarify. Is the graph undirected? Can it be disconnected? Can a node have no edges?
  2. 2Restate it. Two groups with every edge between them is the same as colouring with two colours.
  3. 3Explain the forcing. One colour at a start node forces the colour of its whole piece; a clash is an odd cycle.
  4. 4Code the BFS. Colour array with −1 for unseen, an outer loop over starts, a queue inside.
  5. 5Test out loud. Run the even square, the 5-ring and a graph whose second piece is a triangle.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?