Technique · Interview · hard
Games on Graphs (retrograde analysis)
When a game's state graph has cycles, memoised DFS can loop forever. Start from the known terminal states and propagate backwards instead. A state is WIN if some move reaches a LOSE state, and LOSE once every move is known to reach a WIN state (count the remaining moves, like Kahn in-degrees). Whatever is left unresolved is a DRAW.
0/2 solved
Recognise it
The signal
two-player game on a graph where positions can repeat
1Brute force
Minimax DFS with a move cap (fragile, and slow or wrong).
2The observation
Cycles leave the recursion undefined.
3Why it works
A label never changes once assigned. An unresolved state's counter is the number of its moves not yet known to win for the opponent.
Reach for it when
- +the outcome can be a draw
- +cat-and-mouse / chase games
Not when
- ×the state graph is a DAG (plain memo DFS)
- ×a sum of impartial games (Sprague-Grundy)
Practice
- Cat and MouseHard
- Cat and Mouse IIHard
Pitfalls
Misconceptions
- !cutting recursion at an arbitrary depth
Go deeper1 combinations · resources
Combinations
Games on Graphs (retrograde analysis)+Topological Sort+BFS/Dijkstra on an Augmented State Graph
chase game on a graph or grid with possible draws
- Games on Graphs (retrograde analysis):
- labels propagate back from terminal states
- Topological Sort:
- Kahn-style degree counters decide LOSE once every move is known to lose
- BFS/Dijkstra on an Augmented State Graph:
- states are (mouse, cat, turn)