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/Graphs/Graph BFS / DFS

Technique

BFS/Dijkstra on an Augmented State Graph

Make each node (vertex, extra state) — keys held, stops used, coupon used, visited mask — and run BFS/Dijkstra on that larger graph.

Learn firstGraph BFS / DFSDijkstra (Weighted Shortest Path)UnlocksGames on Graphs (retrograde analysis)
0%
mastery
0/10 solved
Practise nextCat and Mouse
IdeaPracticePitfalls

Recognise it

The signal

shortest path with a resource (k stops, one discount, keys)

  1. 1Brute force

    DFS over all paths tracking the state.

  2. 2The observation

    Exponential paths: states with the same (vertex, extra) are interchangeable.

  3. 3Why it works

    Each augmented state is settled once with its true optimal cost.

Reach for it when

  • +visiting all nodes (vertex, mask)
  • +puzzle states (lock combinations)

Not when

  • ×extra state is too large (needs a different model)
  • ×state is monotone and can be pruned greedily

Practice

Next

Cat and Mouse

0/10 solved
  • Flight DiscountCSESMedium
  • Cheapest Flights Within K StopsAmazon, UberMedium
  • Swap GameCSESMedium
  • Cat and MouseHard
  • Cat and Mouse IIHard
  • Bus RoutesUber, GoogleHard
  • Shortest Path Visiting All NodesHard
  • Minimum PathCodeforces2400
  • Paired PaymentCodeforces2200
  • Fixed Length Walk QueriesCSESHard

Pitfalls

Misconceptions

  • !pruning by vertex alone loses states with different resources
Go deeper3 combinations · resources

Combinations

  • Dijkstra (Weighted Shortest Path)+BFS/Dijkstra on an Augmented State Graph

    one discount, at most k stops, fuel, keys

    Dijkstra (Weighted Shortest Path):
    weighted shortest path
    BFS/Dijkstra on an Augmented State Graph:
    duplicates vertices per resource state

    Flight DiscountCheapest Flights Within K Stops

  • 0-1 BFS+BFS/Dijkstra on an Augmented State Graph

    minimum modifications to make a path exist

    0-1 BFS:
    0-cost and 1-cost moves
    BFS/Dijkstra on an Augmented State Graph:
    grid cell plus direction/change state

    Minimum Cost to Make at Least One Valid Path in a GridMinimum Obstacle Removal to Reach Corner

  • 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

  • Graph Theory playlistWilliam Fiset · video
  • Graph TraversalUSACO Guide · course
  • Disjoint Set Unioncp-algorithms · article
  • Dijkstracp-algorithms · article
  • Topological sortingcp-algorithms · article
  • Shortest paths visualizerVisuAlgo · tool
  • Graph cheatsheetTech Interview Handbook · article
TodayReviewPatternsProblems