Dijkstra vs Bellman–Ford — Side-by-Side, Animated

Watch two shortest-path algorithms solve similar problems differently. Left: Dijkstra (non-negative edges). Right: Bellman–Ford (handles negative edges, no negative cycles). Start node A.

Dijkstra’s Algorithm Greedy
5 2 1 A dist=0 B dist=∞ C dist=∞
Ready. Start at A. All edges ≥ 0.
Graph: A→B (5), A→C (2), C→B (1). Dijkstra always picks the closest unvisited node and relaxes its outgoing edges. Once a node is visited, its distance is final.
edge   active edge   visited node
Bellman–Ford Dynamic Programming
5 2 -2 A dist=0 B dist=∞ C dist=∞
Ready. Start at A. Negative edge allowed (no negative cycle).
Graph: A→B (5), A→C (2), C→B (−2). Bellman–Ford relaxes all edges for V−1 rounds. If a further pass still improves a distance, that would indicate a negative cycle (not present here).
Shortest A→B will end up 0 via A→C→B.