Dijkstra Algorithm Simulator

Dijkstra's algorithm finds shortest paths from one source vertex in a graph with non-negative edge weights. This simulator uses an undirected weighted graph, tentative distances, predecessors, and a min-priority queue.

Binary-heap implementationO((V + E) log V)
Edge requirementNon-negative
OutputDistance + path
Graph typeUndirected

Graph editing and algorithm controls

400 ms

Example weighted graph loaded. Drag vertices or run Dijkstra.

Vertices0
Edges0
Settled vertices0
Relaxations0

Current step

—

Selected shortest path

—

Min-priority queue

Distance and predecessor table

VertexDistancePreviousState

Relaxation rule

candidate =
  distance[current] + edgeWeight

if candidate < distance[neighbor]:
  distance[neighbor] = candidate
  previous[neighbor] = current
  push updated entry
  into the min-priority queue

Stale queue entries are ignored.

Dijkstra concepts

Greedy settlement

The unsettled vertex with the smallest tentative distance becomes settled. With non-negative weights, its shortest distance can no longer improve.

Relaxation

Each outgoing edge tests whether reaching a neighbor through the current vertex gives a shorter route.

Path reconstruction

The predecessor table stores the previous vertex on each best-known route. Following those links backward reconstructs the shortest path.

Unreachable vertices

A vertex outside the source's connected component keeps distance infinity and has no shortest path from that source.

When Dijkstra applies

ConditionResult
All edge weights are non-negativeDijkstra is correct.
A negative edge existsUse Bellman–Ford or another suitable algorithm.
All weights are equalBFS is sufficient.
Need all-pairs shortest pathsRun from each source or use an all-pairs algorithm.