Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Graphs/Graph BFS / DFS

Technique

Bipartite Check / 2-Colouring

BFS/DFS colouring with alternating colours; a conflict means an odd cycle and the graph is not bipartite.

Learn firstGraph BFS / DFSUnlocksBipartite Matching
0%
mastery
0/5 solved
Practise nextBipartite Checking
IdeaPracticePitfalls

Recognise it

The signal

split into two groups with no conflicts inside a group

  1. 1Brute force

    Try all 2n splits.

  2. 2The observation

    Exponential splits: colouring is forced along each edge.

  3. 3Why it works

    Adjacent coloured vertices always have different colours.

Reach for it when

  • +possible bipartition / dislike pairs

Not when

  • ×more than two groups (k-colouring is NP-hard in general)
  • ×parity constraints with weights (DSU with parity)

Practice

Next

Bipartite Checking

0/5 solved
  • Building TeamsCSESEasy
  • Is Graph Bipartite?Meta, AmazonMedium
  • Split Into Two SetsCodeforces1600
  • Graph Without Long Directed PathsCodeforces1700
  • Bipartite CheckingCodeforces2500

Pitfalls

Misconceptions

  • !only checking the component of vertex 0
Go deeperresources

Resources

  • Graph Theory playlistWilliam Fiset · video
  • Graph TraversalUSACO Guide · course
  • Disjoint Set Unioncp-algorithms · article
  • Dijkstracp-algorithms · article
  • Topological sortingcp-algorithms · article
  • Shortest paths visualizerVisuAlgo · tool
  • Graph cheatsheetTech Interview Handbook · article
TodayReviewPatternsProblems