Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Interactive shortest path calculator
Finds shortest paths from source to all vertices in weighted graphs
Select an algorithm and generate steps to begin visualization
Dijkstra's algorithm computes the shortest path from a source node to every other node in a weighted graph with non-negative edge weights. Published by Edsger Dijkstra in 1959, it remains the standard single-source shortest path algorithm and the basis of most practical routing systems.
The algorithm maintains a tentative distance for every node, initially infinite except the source at zero. Using a priority queue it repeatedly extracts the unsettled node with the smallest tentative distance, marks it final, and relaxes each outgoing edge: if the path through the current node is shorter than the neighbor's recorded distance, the distance is updated. With a binary heap this runs in O((V + E) log V) time. Non-negative weights are essential; a negative edge can invalidate already settled nodes.
Dijkstra's algorithm drives GPS navigation, internet routing protocols such as OSPF, flight and transit planners, and network latency analysis. It also appears inside games for pathfinding when heuristics are unavailable. In interviews it is the canonical answer for weighted shortest path questions and the starting point for discussing A* and Bellman-Ford trade-offs.
Dijkstra is a greedy algorithm whose entire correctness rests on one claim: the closest unsettled node can never be improved later. A priority queue supplies that node in O(log V).
Dijkstra(graph, source):
for each vertex v: dist[v] = infinity
dist[source] = 0
pq = priority queue containing (0, source)
while pq is not empty:
(d, u) = pq.extractMin()
if d > dist[u]: continue // stale entry, skip
for each edge (u, v, w):
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
parent[v] = u
pq.insert((dist[v], v))The stale-entry check matters. Rather than decreasing a key inside the heap, which most standard libraries do not support, the usual implementation pushes a duplicate entry and ignores any entry whose recorded distance no longer matches. This is called lazy deletion, and it is why the queue can hold up to E entries rather than V.
Run Dijkstra from A on a weighted graph where the greedy choice pays off, watching the settled set grow.
Example graph: Undirected edges A-B (4), A-C (2), C-B (1), B-D (5), C-D (8).
Final distances are A 0, C 2, B 3, D 8, with the shortest path to D running A to C to B to D. Note that the direct A-B edge of weight 4 is never used: routing through C costs 3. Note also that nodes settle in distance order 0, 2, 3, 8, which is the property the greedy argument depends on.
Time: O((V + E) log V) · Space: O(V)
With a binary heap, each of the V vertices is extracted once at O(log V), and each of the E edges can trigger one insertion at O(log V), giving O((V + E) log V). With lazy deletion the heap holds up to E entries, so extraction is O(log E), but since E is at most V squared, log E is at most 2 log V and the bound is unchanged. A Fibonacci heap improves the theoretical bound to O(E + V log V) because decrease-key becomes O(1) amortised, though the constants are bad enough that binary heaps usually win in practice. On a dense graph, a simple array scan for the minimum gives O(V squared), which beats the heap when E approaches V squared.
Dijkstra is the default for weighted shortest paths. What replaces it depends on which of its assumptions your graph breaks.
| Alternative | Prefer it when | Cost |
|---|---|---|
| BFS | All edges have equal weight, so hop count is distance. Strictly faster. | O(V + E) |
| Bellman-Ford | Some edge weight is negative, which breaks the greedy settling argument. | O(VE) |
| A* search | You want one specific target and have an admissible heuristic, such as straight-line distance on a map. | O((V + E) log V) worst case |
| Floyd-Warshall | You need every pair of distances and the graph is small or dense. | O(V^3) |
| Bidirectional Dijkstra | One source, one target, a large graph, and edges you can traverse backwards. | roughly half the explored nodes |
Read the full article: Shortest Path Algorithms Explained
Related algorithms: Bellman-Ford Algorithm, Floyd-Warshall Algorithm, Breadth-First Search