43224315423ABCDEFGH
AlgoPlus//graph / dijkstra
Read the theory

Dijkstra's Shortest Path

Settle the nearest unvisited node, relax its edges.

Stability
In-Place
Space Complexity
Avg Time
Source
Target
Legend
Current
Frontier
Visited
Path / MST
AI Tutor Workspace
In a nutshell
Dijkstra's algorithm finds the shortest path from one start node to every other when edges carry costs (weights). It always settles the nearest unsettled node next — locking in its final distance — then relaxes its neighbours, checking whether routing through it is cheaper. A priority queue keeps the closest candidate handy. It assumes no edge weight is negative.
Ready
Press play to begin the cinematic walkthrough.
A wavefront expands from the source, always settling the closest unvisited node first.
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for Dijkstra's Shortest Path.
Code Simulator
1
def dijkstra(graph, start):
2
    pq = [(0, start)]
3
    dist = {n: inf for n in graph}
4
    dist[start] = 0
5
    while pq:
6
        d, u = heappop(pq)  # Settle node
7
        for v, w in graph[u]:
8
            if dist[u] + w < dist[v]:  # Relax
9
                dist[v] = dist[u] + w
10
                heappush(pq, (dist[v], v))
Why Python? · Readable first, fast second

Dynamically typed and interpreted — every comparison and swap is dispatched by the interpreter at run time, so tight loops run roughly 10–100× slower than compiled C/C++. Unbeatable for learning the idea with the least code; not what you reach for when the inner loop is the bottleneck.