Graph Theory37 sections · 1633 units
Open in Course

LeetCode 785 Is Graph Bipartite? - The Color Array

Tracking state

You need to track each node's color. Create an array color of size nn. What values should you use?

A common choice: 1-1 means uncolored (not yet visited), 00 means first color (say, red), 11 means second color (say, blue). Initialize all entries to 1-1. When you visit a node, set its color to 00 or 11. Check if it is 1-1 to know if you have seen it before. This array serves as both the color assignment and the visited tracker.