advanced practice · coding problem

Dijkstra shortest path

Weighted graph: 0—1—2 with weights 1 and 4, and 0—2 weight 10. Shortest 0→2?

The mechanism

Pick the algorithm by the graph: BFS for unweighted, Dijkstra when weights are non-negative, Bellman-Ford if weights can be negative, A* when you have a heuristic estimating distance to the goal. Using the wrong one gives wrong answers or wasted time.

Study Shortest Path

Expected evidence

Cost 5 via node 1.

Try this drill in the practice workspace →

Hints when you need them
  • Min-heap on tentative distance.
  • Skip stale heap entries.

Check the sources

Browse all practice briefs