What is Dijkstra’s algorithm?
Dijkstra’s algorithm is a method for
finding the shortest path from one starting point to all other points in a
weighted graph, as long as the edge weights are non-negative. It is commonly
used for route finding, like calculating the fastest path between cities in a
road network.
Quick Scoop
It works by repeatedly choosing the unvisited node with
the smallest known distance from the source, then updating the distances of
its neighbors if a shorter route is found. This process continues until every
node has been processed, or until the destination node’s shortest path is
confirmed.
How it works
- Start with distance 0 for the source node and infinity for all others.
- Pick the unvisited node with the smallest current distance.
- “Relax” its neighbors by checking whether going through that node gives a shorter route.
- Mark the node as visited and repeat until done.
Why it matters
Dijkstra’s algorithm is popular because it is simple,
reliable, and efficient for graphs with non-negative weights. It is widely
used in GPS navigation, network routing, and other pathfinding problems.
Important limit
It does not work correctly when graphs contain
negative edge weights. For those cases, another algorithm is needed.
Example
If cities are connected by roads with distances, Dijkstra’s
algorithm can find the shortest route from one city to every other city by
always expanding the nearest not-yet-checked city first.
TL;DR
Dijkstra’s algorithm finds shortest paths from one source in a
graph with non-negative edge weights by greedily expanding the nearest
unvisited node.