AiTechWorlds
AiTechWorlds
Visualize Dijkstra finding shortest paths with live distance labels, edge relaxation, and synced multi-language code.
import heapq
def dijkstra(adj, s, n):
dist = [float('inf')] * n; dist[s] = 0
pq = [(0, s)]
while pq:
d, u = heapq.heappop(pq)
if d > dist[u]: continue
for v, w in adj[u]:
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
heapq.heappush(pq, (dist[v], v))
return distDijkstra's algorithm finds the shortest path from a source to all nodes in a graph with non-negative edge weights by greedily expanding the closest unfinished node.
| Time | O((V + E) log V) |
| Space | O(V) |
| Graph | Undirected, weighted |
The Dijkstra Visualizer animates how Dijkstra's algorithm works: it finds the shortest path from a source to every node in a graph with non-negative edge weights by repeatedly expanding the closest unfinished node and relaxing its edges. Dijkstra runs in O((V + E) log V) with a priority queue and uses O(V) space. Live distance labels update on every relaxation.
Generate a graph
Click New Graph or set the node-count slider to create a random weighted graph.
Play the animation
Press Play to watch the closest node picked and its edges relaxed; distance labels update live.
Step and scrub
Use Step Forward/Back or the timeline to inspect each relaxation.
Read the synced code
The highlighted pseudocode line tracks the relaxation; switch tabs for C++, Java, Python, or JavaScript.
100% Private — No Server Required
All processing happens directly in your browser. No data is uploaded, stored, or transmitted to any server.
Last reviewed on June 14, 2026 by the AiTechWorlds Tools Team. All processing runs locally in your browser.