Dijkstra's Algorithm is a graph search algorithm that finds the shortest paths from a single source node to all other nodes in a graph with non-negative edge weights. It operates as a greedy, best-first search using a priority queue (often a min-heap) to iteratively select the unvisited node with the smallest known tentative distance.
The algorithm works in these steps:
- Assign a tentative distance of zero to the source node and infinity to all others.
- Mark all nodes as unvisited and add the source node to the priority queue.
- While the queue is not empty, extract the node
u with the smallest distance.
- For each neighbor
v of u, calculate a new distance: distance[u] + weight(u, v).
- If this new distance is less than the current
distance[v], update distance[v] and set u as its predecessor. Add/update v in the priority queue.
- Mark
u as visited. The algorithm terminates when the queue is empty, at which point distance[] holds the shortest path cost from the source to every node.
Its time complexity is O((V + E) log V) with a binary heap, where V is vertices and E is edges.