Practice Discrete Math

Algorithms / Shortest Paths Weight Signs

Least You Need to Know: Dijkstra, Bellman-Ford, and Edge-Weight Assumptions

Shortest-path algorithms are chosen by the structure of the edge weights. Dijkstra relies on nonnegative weights so finalized distances never need revision, while Bellman-Ford handles negative edges and can detect reachable negative cycles through repeated relaxation.

The least you need to know

Key notation

relaxation replace a distance estimate with a smaller one when an edge offers improvement
negative cycle a cycle whose total weight is negative
finalized vertex a vertex whose shortest-path distance is settled in Dijkstra

Tiny worked example

  • If all weights are nonnegative, Dijkstra can safely finalize the nearest not-yet-final vertex.
  • If negative edges exist, that monotonicity can fail.
  • Bellman-Ford instead performs repeated relaxation rounds.
  • A further improvement after `|V|-1` rounds reveals a reachable negative cycle.

Common mistakes

How to recognize this kind of problem

Start practice