Mechanism
Maintain tentative distances and visited nodes. Always pick the unvisited node with the smallest distance to explore next.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
The Dijkstra algorithm finds the shortest path from a source vertex to all other vertices in a graph with non‑negative edge weights. It greedily selects the closest unvisited node at each step, guaranteeing optimal distances.
Maintain tentative distances and visited nodes. Always pick the unvisited node with the smallest distance to explore next.
A Min-Priority Queue efficiently provides the next node. Without it, finding the next node would take O(V) instead of O(log V).
When checking edge (u, v), if dist[u] + weight < dist[v], we update dist[v]. This is the core of path finding.
Dijkstra fails with negative edge weights. For graphs with negative costs, use Bellman-Ford instead.