Dijkstra shortest path calculator
Find shortest paths and route costs from one start using non-negative weights. Your inputs stay on your device.
Set up your task
Starting local processing
Ready. Processing stays in this browser.
2 Your result
Your result updates in this browser. Check your input if the result looks unexpected.
How to use Dijkstra shortest path calculator
Find shortest paths and route costs from one start using non-negative weights. The pre-filled problem gives you a worked starting point. Compare the response with your own working before trying the self-check below.
- Check the example inputs: Vertices (space-separated), Edges (from, to, weight, one per line), Start vertex, Edges are.
- Change the inputs for your problem. Results update as you type, so finish entering the values before copying an answer.
- Use the working to check an intermediate value or assumption. Then change the example as suggested in the self-check and compare the result.
Supported inputs and limits
Use up to 20 vertices and 80 weighted edges. Each edge needs a numeric weight of zero or greater.
How it works
Dijkstra’s algorithm tracks the cheapest known distance from a source. At each round it selects the unvisited vertex with the smallest tentative distance and checks whether travelling through it improves a neighbour. A predecessor records which edge supplied a cheaper path so the final route can be reconstructed.
Non-negative weights are essential to the greedy selection. Once a vertex is picked, taking extra non-negative edges cannot create a route shorter than its already minimal distance. A negative edge breaks that argument. For a directed graph with negative edges, use an algorithm that accounts for them explicitly.
Example
Run the pre-filled example. Its first result is “Shortest paths from A”.
- Vertices (space-separated)
- A B C D
- Edges (from, to, weight, one per line)
- A B 2
A C 8
B C 3
C D 2 - Start vertex
- A
- Edges are
- Directed
Shortest paths from A A: 0 via A B: 2 via A → B C: 5 via A → B → C D: 7 via A → B → C → D
Questions about Dijkstra shortest path calculator
Can two edges connect the same vertices?
Yes. Each candidate edge is relaxed, so a higher-cost parallel edge cannot replace a cheaper route.
Is a zero-weight edge allowed?
Yes. It can create equally cheap alternatives. The calculator keeps one route and reports the total cost.
In the example, work out the cheapest route from A to C.
Expect A → B → C at cost 5. The single edge from A to C costs 8, showing why the fewest hops can be more expensive.
Related tools
Last updated