GraphsAnimated
Dijkstra's Algorithm
Always settle the closest unsettled node.
Time
O((V + E) log V)
Extra space
O(V)
Use it for
Shortest paths with non-negative weights.
How it works
- Give every node a tentative distance: 0 for the start, ∞ for the rest.
- Repeatedly settle the unsettled node with the smallest distance: it can no longer be improved (all weights are non-negative).
- Relax each of its edges: if going through this node is shorter, update the neighbour’s distance.
- When every reachable node is settled, the distances are final.
Watch it run
Loading animation…