Shortest Path Calculator — Dijkstra’s Algorithm Visualizer

Find a minimum-weight route through a directed or undirected graph. Enter non-negative edge weights, calculate the shortest path, then move through each settled vertex and distance relaxation—all locally in your browser.

Enter a weighted graph

Each line joins both endpoints in undirected mode.
Separate labels with commas, semicolons, or lines. Use this field for isolated vertices.
Format: A,B,49 lines

Private by design: your graph is parsed and calculated on this device. It is not uploaded, stored, or included in the page URL.

Shortest path and iterations

The sample graph is calculated below.

UnreachedFrontierCurrentSettledFinal path
Step 0 of 0
ReadyCalculate a graph to inspect the algorithm.
Tentative distances and predecessors at the selected Dijkstra iteration
VertexDistancePredecessorStatus
No calculation yet.

Advertisement

How Dijkstra’s algorithm finds a shortest path

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.

Initialization

d(source) = 0

d(v) = ∞ for v ≠ source

Relax an edge

candidate = d(u) + w(u,v)

If the candidate is smaller than d(v), replace the distance and predecessor.

Finish

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.

Worked shortest-path example

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 settlingUseful updatesBest 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 1414
E (10)F improves to 1313
F (13)The destination is settled; stop.13

Input rules, assumptions, and limits

Weighted edge format

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.

Graph interpretation

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.

Duplicates and ties

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.

Numerical limits

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: .

Shortest path calculator FAQ

What graphs can Dijkstra’s algorithm solve?

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.

Why are negative edge weights rejected?

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.

How do I enter directed edges?

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.

Can weights be decimals or zero?

Yes. Finite decimal and zero weights are accepted. Negative values, infinities, and values greater than 1 trillion are rejected.

What happens when shortest routes tie?

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.

Why can a path with more edges be shorter?

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.

What does infinity mean?

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.

Does the calculator save my graph?

No. Parsing, shortest-path calculation, playback, copying, and CSV preparation run locally in your browser. The tool does not transmit or store your input.

Explore more tools