Graph Theory37 sections · 1633 units
Open in Course

BFS for Detection

Level-by-level coloring

BFS works naturally for bipartite detection. Start from a node, color it 00. BFS explores in levels: all neighbors (level 11), then neighbors of neighbors (level 22), and so on. Nodes at even levels get color 00. Nodes at odd levels get color 11. This matches BFS's natural behavior perfectly. When you visit a neighbor, check: is it uncolored?

Give it the opposite color. Already colored? Verify it has the opposite color. Same color means the graph is not bipartite.