Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Math & Number Theory/Combinatorial Game Theory (minimax, Nim, Sprague-Grundy)

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.

Learn firstCombinatorial Game Theory (minimax, Nim, Sprague-Grundy)Topological SortBFS/Dijkstra on an Augmented State Graph
0%
mastery
0/2 solved
Practise nextCat and Mouse
IdeaPracticePitfalls

Recognise it

The signal

two-player game on a graph where positions can repeat

  1. 1Brute force

    Minimax DFS with a move cap (fragile, and slow or wrong).

  2. 2The observation

    Cycles leave the recursion undefined.

  3. 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

Next

Cat and Mouse

0/2 solved
  • 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)

    Cat and MouseCat and Mouse II

Resources

  • Binary exponentiationcp-algorithms · article
  • Sieve of Eratosthenescp-algorithms · article
  • Modular inversecp-algorithms · article
  • DivisibilityUSACO Guide · course
  • CombinatoricsUSACO Guide · course
  • Math cheatsheetTech Interview Handbook · article
TodayReviewPatternsProblems