Fewest edges and least weight differ
A two-edge route of weight 20 loses to a five-edge route of weight 8. BFS answers the wrong question once weights exist.
With weights, breadth-first search no longer works. Dijkstra settles vertices in order of distance from the source, which is correct precisely because the weights are non-negative.
Core ideas
Two different questions — fewest edges and least total weight — and the second needs a different algorithm.
A two-edge route of weight 20 loses to a five-edge route of weight 8. BFS answers the wrong question once weights exist.
Once settled, a vertex's distance is final, because any other route would have to pass through a vertex already known to be further away.
A negative edge could make a longer detour cheaper, so a settled vertex might be improvable — and Dijkstra never revisits it.
The material
The algorithm, why it is correct, and where it fails.
A weighted graph assigns each edge a number. The weight of a path is the sum of its edge weights, and a shortest path is one of minimum weight.
With non-negative weights, when Dijkstra settles a vertex u, dist[u] is the true shortest distance from s to u.
By induction on the order of settling. Suppose every previously settled vertex has its true distance, and consider the moment u is chosen.
Take any path P from s to u, and let x be its first vertex that is not yet settled, with predecessor y (settled). Then the length of P is at least dist[y] + w(y,x) ≥ dist[x] ≥ dist[u], the last step because u was chosen as the smallest.
The first inequality uses that the remaining part of P has non-negative weight — which is exactly where the hypothesis enters. So no path beats dist[u].
□
The proof names the place non-negativity is used, which is the useful way to read it: the argument is fine until a negative edge lets the tail of a path reduce its total.
Vertices s, a, b with w(s,a) = 1, w(s,b) = 2, w(b,a) = −2. Dijkstra settles a at distance 1 and never reconsiders, but s → b → a has weight 0. The answer is wrong and the algorithm never notices.
Bellman–Ford handles negative weights by relaxing every edge n−1 times, at cost Θ(n|E|), and detects negative cycles — for which no shortest path exists at all, since going round the cycle again always reduces the total.
For sparse graphs the heap version is much faster; for dense ones the simple scan is competitive. The choice is exactly “Graph Representations”'s representation trade-off appearing again.
With all weights equal to 1, Dijkstra settles vertices in the order BFS visits them, and the priority queue degenerates into a plain queue. BFS is Dijkstra's special case, which is why “Breadth-First and Depth-First Search” came first.
Interactive
Your graph, the course's algorithms
Method
Settle the nearest, relax its edges, repeat.
Source at 0, everything else at infinity, nothing settled.
Its distance is now final. This is the step the correctness proof justifies.
For each neighbour, if going through the settled vertex is cheaper, update the distance and the parent.
Vertices still at infinity are unreachable. Follow the parent pointers backwards to recover a route.
s → a weight 10; s → b weight 3; b → a weight 2; a → t weight 1; b → t weight 9.
The relaxation of a from 10 down to 5 is the step BFS cannot perform: it settles a vertex on first contact and never improves it. That is exactly right when every edge costs 1 and wrong otherwise.
Build a small graph with one negative edge and run Dijkstra by hand. Watch it settle a vertex whose distance later turns out to be improvable, and locate the step of the proof that fails.
Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.
No recommendations yet for this lesson.