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/Dijkstra (Weighted Shortest Path)

Technique

0-1 BFS

Shortest paths when edges cost 0 or 1: push 0-edges to the deque front and 1-edges to the back for O(V + E).

Learn firstGraph BFS / DFSDijkstra (Weighted Shortest Path)
0%
mastery
0/2 solved
Practise nextMinimum Cost to Make at Least One Valid Path in a Grid
IdeaPracticePitfalls

Recognise it

The signal

minimum number of changes/removals along a path

  1. 1Brute force

    Dijkstra O(E log V).

  2. 2The observation

    Heap overhead: with only two weights the deque keeps distances sorted.

  3. 3Why it works

    The deque contains vertices with distance d or d+1 only, in order.

Reach for it when

  • +some moves are free and others cost 1

Not when

  • ×more than two distinct weights (dijkstra or dial's buckets)

Practice

Next

Minimum Cost to Make at Least One Valid Path in a Grid

0/2 solved
  • Minimum Cost to Make at Least One Valid Path in a GridHard
  • Minimum Obstacle Removal to Reach CornerHard

Pitfalls

Misconceptions

  • !marking visited on push (must allow later improvement as in dijkstra)
Go deeper1 combinations · resources

Combinations

  • 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

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