Watch Dijkstra's algorithm find the shortest path from A to E on a weighted graph you can edit. Step-by-step example, and why negative edges break it.
About this algorithm
Dijkstra's algorithm finds the shortest path from one source to the other nodes of a graph whose edge weights are zero or more: road distances, travel times, network delays. It keeps a priority queue of nodes by distance and always expands the closest node it has not finished yet, relaxing that node's edges, so once a node is finished its distance can never improve.
See Dijkstra find a route in your city →Use Dijkstra when edges have different costs that are never negative and you want the lowest total cost rather than the fewest edges. If every edge costs the same, BFS gives the same answer with a plain queue and less work. If any edge is negative, Dijkstra can finish a node too early and report a wrong cost, so use Bellman-Ford there. DFS is not a shortest-path method at all.
Give the source distance 0 and every other node infinity, meaning not reached yet. Put the source into a min-priority queue keyed by distance.
Remove the entry with the smallest distance. If its distance is larger than the best already recorded for that node, it is a leftover from before an improvement: skip it. If the node is the target, stop.
For each neighbour, add the edge weight to the current node's distance. If that sum beats the neighbour's recorded distance, write the new distance, record the current node as its parent, and push the neighbour with the new distance. This check-and-improve step is called relaxing the edge.
A node popped with its best distance is settled: any other route to it would pass through a node at least as far away, and with no negative edges that route cannot be cheaper. Repeat until the target is popped, then follow parents back from it to get the path.
The page loads six nodes with source A and target E. The weights are A-B 4, A-D 2, B-C 5, B-D 1, B-E 3, C-E 1, D-E 8, D-F 10 and E-F 2, and every edge works in both directions. Pop A at 0: B becomes 4 and D becomes 2. Pop D at 2: through D, B costs 2 + 1 = 3, which beats 4, so B drops to 3; E becomes 10 and F becomes 12. Pop B at 3: C becomes 8, and E drops from 10 to 3 + 3 = 6. The old entry for B at 4 comes out next and is skipped. Pop E at 6: it is the target. The path is A → D → B → E with cost 2 + 1 + 3 = 6. BFS on the same graph picks A → B → E, which has fewer edges but costs 4 + 3 = 7.
Dijkstra treats a node as final once it is popped. A negative edge found later can make a settled node cheaper, and every distance already worked out from that node is then wrong. Bellman-Ford makes no such assumption, which is why it is the one to use with negative weights.
Time: every edge can push one entry into the binary heap, and each push or pop costs about log V, so the total is O((V + E) log V). Space: the distance and parent tables hold one entry per node, and this version leaves outdated entries in the heap instead of updating them, so the heap can hold up to one entry per edge.