#785

Is Graph Bipartite?

Medium 2-colouring with BFS

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether an undirected graph's nodes can be split into two groups so that every edge connects nodes from different groups.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: can nodes be split into two teams with every edge between teams?

Why this works: colour with two colours using BFS; a neighbour with your own colour means it's impossible.

Example: square 0-1-2-3-0

0 red, 1 blue, 2 red, 3 blue → no conflict → true

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Only starting from node 0 and missing other components.

Solve on LeetCode ↗
Complexity
  • Time: O(V + E).
  • Space: O(V).
Topic: Graphs
🎬 Episodes 74–85: video coming soon