Dijkstra's Algorithm Calculator
Interactive Dijkstra's algorithm calculator. Build a weighted graph, run the shortest path algorithm step-by-step, and get distances, predecessor table, and the optimal path between two nodes.
Full original guide (expanded)
Dijkstra's Algorithm Calculator
Build a weighted graph, run Dijkstra from a source node, and read shortest-path distances and routes.
1. Define the graph
Nodes
Node labels are case-sensitive and must be unique.
Current nodes
Edges (weights must be non-negative)
If the graph is undirected, each edge is automatically mirrored. Dijkstra's algorithm requires all weights to be ≥ 0.
Current edges
| # | From | To | Weight | Direction | Actions |
|---|
2. Run Dijkstra's algorithm
Leave empty to compute distances to all nodes.
Dijkstra's algorithm in a nutshell
Dijkstra's algorithm solves the single-source shortest path problem on a weighted graph with non-negative edge weights. Given a source node \(s\), it computes the shortest distance \(d(s, v)\) to every other node \(v\), and a predecessor for each node so you can reconstruct the actual path.
Key assumptions
- Graph can be directed or undirected.
- Edge weights must be \(\ge 0\).
- The algorithm returns exact shortest paths under these assumptions.
High-level algorithm
- Initialize all distances to \(\infty\) except the source, set to 0.
- Mark all nodes as unvisited.
- Repeat:
- Pick the unvisited node with the smallest tentative distance.
- For each outgoing edge, try to relax it (update neighbor distance if a shorter path is found).
- Mark the current node as visited.
- Stop when all nodes are visited or when the target node has been extracted (if you only care about one destination).
Relaxation step
For an edge \(u \to v\) with weight \(w\), if \[ d(v) > d(u) + w \] then update \[ d(v) \leftarrow d(u) + w,\quad \text{pred}(v) \leftarrow u. \]
Time complexity
The complexity depends on how you select the node with minimum tentative distance:
- Simple array / linear scan: \(O(V^2)\)
- Binary heap: \(O((V + E)\log V)\)
- Fibonacci heap: roughly \(O(E + V\log V)\)
This calculator uses the simple array-based approach for transparency of the logic and is suitable for educational examples and small- to medium-sized graphs.
Interpreting the calculator output
- Distance table: Shows the final minimal distance from the source to every node. If a node is unreachable, the distance is reported as \(\infty\).
- Predecessor column: Indicates, for each node, which previous node lies on a shortest path from the source. By following predecessors from the target back to the source you reconstruct the route.
- Shortest path summary: When a target node is specified, the calculator reconstructs and prints the path as a sequence of nodes plus the total distance.
Common pitfalls and FAQs
Why can't I use negative weights?
Dijkstra's algorithm assumes that once a node is extracted with the smallest tentative distance, that distance is final. Negative edge weights break this property because a path that looks optimal at one stage may be improved later by a negative edge. For graphs with negative weights, use algorithms such as Bellman–Ford.
Directed vs undirected graphs
In an undirected graph, every edge \((u, v)\) is interpreted as two directed edges \(u \to v\) and \(v \to u\) with the same weight. The calculator automatically mirrors edges when the "Treat graph as directed" option is off.
How large can my graph be?
The implementation here is designed for teaching and quick problem solving, not for huge production instances. A few dozen nodes and up to a couple of hundred edges are perfectly fine in a browser environment; for very large graphs, use specialized libraries or offline tools.
Formula (LaTeX) + variables + units
d(v) > d(u) + w
d(v) \leftarrow d(u) + w,\quad \text{pred}(v) \leftarrow u.
','\
- No variables provided in audit spec.
- NIST — Weights and measures — nist.gov · Accessed 2026-01-19
https://www.nist.gov/pml/weights-and-measures - FTC — Consumer advice — consumer.ftc.gov · Accessed 2026-01-19
https://consumer.ftc.gov/
Last code update: 2026-01-19
- Initial audit spec draft generated from HTML extraction (review required).
- Verify formulas match the calculator engine and convert any text-only formulas to LaTeX.
- Confirm sources are authoritative and relevant to the calculator methodology.