Graph Theory37 sections · 1633 units
Open in Course

LeetCode 785 Is Graph Bipartite? - Conflict Example

When it fails

let's trace a triangle: graph = [[1,2],[0,2],[0,1]]. Start: color = [-1,-1,-1]. BFS from 00. Color 00 as 00. Queue: [0]. Pop 00. Neighbors: 11, 22. Color 11 as 11, color 22 as 11. Queue: [1, 2]. Pop 11. Neighbors: 00, 22.

Node 00 has color 00 (opposite of 11, good). Node 22 has color 11 (same as 11). Conflict! Return false. The triangle is an odd cycle. You caught it the moment two neighbors tried to have the same color.