This Dijkstra’s algorithm calculator finds the shortest path between two nodes in a weighted graph. List the edges with their weights, choose a start and an end node, and it returns the shortest distance and the route, using Dijkstra’s algorithm.
Dijkstra’s Algorithm Calculator
Shortest path on a weighted graph: enter the edges, a start and an end node, get the distance, route, every node’s distance and the step-by-step run.
Starting values: the calculator opens with seven edges on six nodes (A to F), treated as undirected. The shortest path from A to F is A, C, E, D, F, with a total distance of 20.
How to enter a graph
Enter one edge per line as two node names followed by a weight. The default graph is:
- A B 4
- A C 2
- B C 5
- B D 10
- C E 3
- E D 4
- D F 11
Weights must not be negative, because Dijkstra’s algorithm gives wrong answers when an edge can lower a distance that is already final. The calculator rejects negative weights, and it handles graphs of up to 200 nodes.
How the algorithm works
- Set the distance to the start node to 0, and every other distance to infinity.
- Pick the unvisited node with the smallest known distance. Mark it as visited.
- For each neighbor, check whether going through this node gives a shorter distance. If so, update it and remember the previous node.
- Repeat until the end node is visited, or until there are no more reachable nodes.
The path is then read backwards from the end node, using the stored previous nodes.
Worked examples
Example 1: the default graph from A to F
Start with A at 0. Its neighbors are C at 2 and B at 4. The smallest unvisited node is C, which reaches E at 2 + 3 = 5, and B via C would cost 7, which is worse than 4, so B stays at 4. Next is B at 4, which reaches D at 14. Then E at 5 reaches D at 5 + 4 = 9, which beats 14. Then D at 9 reaches F at 9 + 11 = 20.
The final distances are A 0, C 2, B 4, E 5, D 9 and F 20. The route is A, C, E, D, F, and its edges add up to 2 + 3 + 4 + 11 = 20.
Example 2: why the direct-looking route loses
The route A, B, D, F has only three edges but costs 4 + 10 + 11 = 25. The route with four edges costs 20. Dijkstra compares total weight, not the number of edges, which is why it finds the cheaper one.
Example 3: an unreachable node
If a node has no edge that connects it to the rest of the graph, no path exists, and the distance stays at infinity. The calculator reports that the end node cannot be reached.
Directed and undirected graphs
In an undirected graph, an edge can be used in both directions, which is the default here. A directed graph treats A B 4 as a one-way link from A to B, so the shortest path from A to F can differ from the shortest path from F to A. Check that the graph type you are modeling matches how the calculator reads your edges, which is undirected by default.
Where the algorithm is used
- Route planning, where nodes are junctions and weights are distances or travel times.
- Network routing protocols such as OSPF, which compute shortest paths over link costs.
- Games and logistics, wherever you need the cheapest way between two places.
Limits
With a binary heap, the running time is roughly proportional to (V + E) log V, where V is the number of nodes and E the number of edges. The calculator is meant for teaching and checking small graphs. For negative weights, use the Bellman-Ford algorithm instead, and for a heuristic search on maps, A-star is often faster.
Tracing the algorithm in a table
On paper, keep a table with one row per node and two columns: the best known distance and the previous node. For the default graph the final table reads: A (0, none), C (2, A), B (4, A), E (5, C), D (9, E), F (20, D). To rebuild the route to F, follow the previous-node column backwards: F came from D, D from E, E from C and C from A. Reversing that list gives A, C, E, D, F.
Common mistakes
- Declaring a node final too early. A node is only final when it is the smallest unvisited one.
- Entering the same edge twice with different weights, which makes the result depend on which one is read last.
- Using a weight of 0 for a missing link. A missing link means no edge at all, and a 0 means the two nodes are free to cross.
Frequently asked questions
What does Dijkstra’s algorithm do?
It finds the shortest path from one node to every other node in a graph with non-negative edge weights.
Why can’t weights be negative?
Dijkstra’s algorithm assumes a node’s distance is final once it is visited. A negative edge could later lower it, so the result would be wrong.
Is the shortest path always the one with the fewest edges?
No. It is the one with the lowest total weight. A path with more edges can be cheaper.
What if there is no path between two nodes?
The distance stays at infinity and the calculator reports that the end node is unreachable.
How many nodes can the calculator handle?
Up to 200.
