Graph Theory37 sections · 1633 units
Open in Course

The Three Colors

State Management

For every node, you track its state: State 00 (Unvisited) means you have not touched this node yet. State 11 (Visiting) means you are currently processing this node or its neighbors. It is in the recursion stack. State 22 (Visited) means you are completely done with this node and all its descendants.

The cycle rule is simple: if you move from a node to a neighbor that is State 11 (Visiting), you have found a cycle. That neighbor is already in your current path, which forms a loop.