Graph Theory37 sections · 1633 units
Open in Course

LeetCode 886 Possible Bipartition - Walkthrough

Trace example

let's trace n=4n = 4, dislikes = [[1,2],[1,3],[2,4]]. Build graph: 121 \leftrightarrow 2, 131 \leftrightarrow 3, 242 \leftrightarrow 4. Start: color = [-1,-1,-1,-1,-1]. BFS from 11: color 11 as 00. Neighbors of 11: 22, 33. Color both as 11. Neighbors of 22: 11 (color 00, opposite, good), 44 (uncolored, color as 00).

Neighbors of 33: 11 (color 00, opposite, good). Neighbors of 44: 22 (color 11, opposite, good). No conflicts. Return true. Group A: {11, 44}. Group B: {22, 33}.