Initialization
d(source) = 0
d(v) = ∞ for v ≠ source
Private by design: your graph is parsed and calculated on this device. It is not uploaded, stored, or included in the page URL.
| Vertex | Distance | Predecessor | Status |
|---|---|---|---|
| No calculation yet. | |||
Dijkstra’s algorithm keeps a tentative distance from the source to every vertex. It repeatedly selects the unsettled vertex with the smallest finite distance, settles that distance, and tests whether travelling through that vertex improves any neighbor’s distance. Testing an edge is called relaxation.
d(source) = 0
d(v) = ∞ for v ≠ source
candidate = d(u) + w(u,v)
If the candidate is smaller than d(v), replace the distance and predecessor.
Stop once the destination is settled, or once no reachable unsettled vertex remains. Follow predecessors backward to reconstruct the route.
With the array-based implementation used here, the running time is O(V² + E) and memory use is O(V + E). That is well suited to the small and medium graphs a browser visualizer can display clearly.
In the sample undirected graph, the route from A to F is A → C → B → D → E → F. Its total weight is 2 + 1 + 5 + 2 + 3 = 13. The apparently more direct route A → C → D → F costs 2 + 8 + 6 = 16, so fewer edges do not necessarily mean a shorter weighted path.
| After settling | Useful updates | Best known distance to F |
|---|---|---|
| A (0) | C becomes 2; B becomes 4 | ∞ |
| C (2) | B improves to 3; D becomes 10; E becomes 12 | ∞ |
| B (3) | D improves to 8 | ∞ |
| D (8) | E improves to 10; F becomes 14 | 14 |
| E (10) | F improves to 13 | 13 |
| F (13) | The destination is settled; stop. | 13 |
Enter one edge per line as start,end,weight, for example A,B,4.5. In directed mode, A -> B,4.5 is also accepted. Endpoint labels are case-sensitive.
Undirected edges can be travelled both ways. Directed edges can only be travelled from the first endpoint to the second. Edge endpoints are added automatically; list isolated vertices separately.
If the same directed edge or undirected pair appears more than once, the smallest supplied weight is used. When tentative distances tie, input order determines which vertex is settled first.
Weights must be finite numbers from 0 through 1 trillion. The tool accepts up to 50 vertices and 2,000 unique edges. Results use JavaScript floating-point arithmetic and display up to six decimal places.
Important: Dijkstra’s algorithm requires non-negative edge weights. A graph with a negative weight needs a different method, such as Bellman–Ford. Zero-weight edges are valid.
The algorithm and non-negative-weight requirement follow the treatment of shortest paths in Princeton University’s Algorithms, 4th Edition materials. The visualizer stops when the destination is settled because its distance cannot later improve under the non-negative-weight assumption.
Calculation note: directed, undirected, decimal-weight, zero-weight, unreachable, duplicate-edge, identical-endpoint, and invalid-input cases checked by the Starlight Tools editorial team. Last reviewed: .
It finds shortest paths in directed or undirected weighted graphs when every edge weight is zero or positive. The graph may be disconnected; the tool reports when the chosen destination cannot be reached.
Dijkstra’s algorithm assumes that settling the smallest tentative distance makes that value final. A later negative edge could break that assumption. Use a negative-weight-capable method such as Bellman–Ford instead.
Choose Directed weighted graph, then enter A,B,4 or A -> B,4. Both mean an edge from A to B with weight 4. The reverse trip requires a separate B-to-A edge.
Yes. Finite decimal and zero weights are accepted. Negative values, infinities, and values greater than 1 trillion are rejected.
The tool returns one of the minimum-weight routes. It breaks equal-distance processing ties by the order in which vertices first appear, so the same input produces the same displayed route.
Dijkstra’s algorithm minimizes total edge weight, not the number of edges. Several inexpensive edges can have a lower sum than one or two expensive edges.
Infinity means no route from the source to that vertex has been discovered at the displayed step. If it remains infinity when the algorithm ends, the vertex is unreachable from the source.
No. Parsing, shortest-path calculation, playback, copying, and CSV preparation run locally in your browser. The tool does not transmit or store your input.