Graph Theory37 sections · 1633 units
Open in Course

Computing Grundy Numbers

(MEX computation)

The Grundy number of a position is the minimum excludant (MEX) of the Grundy numbers of all positions you can move to. MEX of a set is the smallest non-negative integer not in the set. For example, MEX of {00, 11, 33} is 22. Terminal positions have Grundy number 00. For other positions, compute MEX of successor Grundy numbers.

This takes O(V+E)O(V + E) time on DAGs. You compute Grundy numbers recursively: the Grundy number of a position is the minimum excludant of all positions reachable in one move. For combined games, XOR the Grundy numbers.