Interactive Dijkstra Calculator
Enter a weighted graph and compute shortest paths from a start node. All edge weights must be non-negative.
from,to,weight or from to weightWhat This Dijkstra Algorithm Calculator Does
This calculator runs Dijkstra’s algorithm on your graph and returns the shortest known distance from your selected start node to every other node. If you provide an end node, it also reconstructs the exact shortest path.
It is useful for route planning, network optimization, graph theory homework, interview practice, and validating hand calculations quickly.
How to Use the Tool
1) Define nodes and edges
You can explicitly list nodes like A, B, C, D, or leave that field blank and let the calculator infer nodes from your edge list.
Then add edges one per line with a non-negative weight:
A,B,4 A,C,2 C,B,1 B,D,5
2) Pick start (and optional end) node
The start node is required. The end node is optional. If end is given, you get a focused shortest path summary from start to end.
3) Choose directed or undirected graph
- Undirected means each edge works both ways.
- Directed means edge direction matters (from → to only).
How Dijkstra’s Algorithm Works (Quick Intuition)
Dijkstra’s algorithm starts from one source node and grows a set of nodes with finalized shortest distances. At every step, it picks the unvisited node with the lowest tentative distance, then relaxes its outgoing edges. This process continues until no better updates are possible.
- Initialize all distances to infinity except start node = 0
- Repeatedly choose the unvisited node with smallest distance
- Try to improve distances to neighbors through that node
- Track predecessors to reconstruct shortest paths
When You Should Use Dijkstra
Dijkstra is ideal when:
- Edge weights are non-negative
- You need shortest paths from one source to many destinations
- You want deterministic, explainable behavior
Important Limitation
Dijkstra does not support negative edge weights. If your graph includes negative costs, use Bellman-Ford or Johnson’s algorithm instead.
Example Interpretation
Suppose the calculator reports path A → C → B → D → E with distance 10. That means this sequence has the smallest total cost among all possible routes from A to E in your graph configuration.
Complexity
This page uses a straightforward JavaScript implementation that is great for small and medium graphs:
- Time complexity (this implementation): approximately
O(V² + E) - Space complexity:
O(V + E)
With a binary heap priority queue, classical Dijkstra can reach O((V + E) log V).
Tips for Accurate Inputs
- Use consistent node labels (e.g., don’t mix
Aandaunless intentional). - Avoid negative weights.
- If no path exists, the distance remains infinity.
- For directed graphs, verify arrow direction before calculating.