Dijkstra Shortest Path Calculator
Find shortest paths from a start node over non-negative weighted directed edges given as [from, to, weight] rows.
Description
Find shortest distances and a minimum-cost path in a directed graph with non-negative edge weights.
The Dijkstra shortest path calculator is suited to routing, network latency, transport cost, and other graphs where every edge has a non-negative numeric cost. It can return the distance from one start node to every node and reconstruct a route to a selected destination.
When to use Dijkstra Shortest Path Calculator
- Compare route costs in a weighted directed network
- Build a shortest-path tree from one source
- Recover the node sequence for a reachable destination
How the calculation works
Nodes are numbered from zero. Each edge is entered as `[from, to, weight]`. A binary min-heap selects the unsettled node with the lowest known distance, then relaxes its outgoing edges. The algorithm records a predecessor whenever it discovers a cheaper route.
Interpreting the result
The `distances` array aligns with node indices; `-1` means unreachable. When an end node is supplied, `path` lists the selected route and `totalCost` is its summed edge weight. When no end is supplied, the path is empty and total cost is `-1` by design.
Important limitations
- Negative edge weights are rejected because Dijkstra's greedy guarantee does not apply to them.
- Edges are directed; add the reverse edge explicitly for an undirected connection.
- Equal-cost routes may have more than one valid path, and input order can determine which predecessor is retained.