Introduction
Dijkstra’s algorithm finds shortest paths from starting node to all nodes of the graph. It is more efficient and can be used for processing large graphs. However, the algorithm requires that there are no negative weight edges in the graph. Each edge is only processed once.
Implementation
Note
It is important to check
if w != distance[node]
to avoid processing the same node multiple times.
Complexity
- Time complexity: , where is the number of vertices and is the number of edges
- Each vertex is added/removed from the priority queue once:
- Each edge is processed once:
- Space complexity:
- Priority queue can contain at most vertices:
- Graph representation (adjacency list):