A negative road breaks Dijkstra.
transcript
One road gives couriers a credit. Its cost is minus three. Watch what Dijkstra does. It starts at junction zero. Then it picks the smallest distance each time.
Junction one gets four. Junction two gets five. Four is smaller. Junction one is final. Five minus three is two. That beats four. But junction one is final. Dijkstra never looks back.
It relaxes every road, in the listed order. Then it does it again, round after round. In round two, five minus three is two. Junction one becomes two. The right answer.
Dijkstra assumes a road never lowers a distance. A negative road breaks that. And there is no warning. The answer looks fine. It is wrong. The true cheapest way to one costs two.
Costs never negative? Use Dijkstra. A cost can be negative? Use Bellman-Ford.
Negative cost? Not Dijkstra. Relax every road, round by round. A cost can be negative there. Full breakdown in the lecture.