Dijkstra Shortest Path Calculator

Enter your graph's edges as a list of connections with weights, then specify a source node and destination node to find the shortest path using Dijkstra's Algorithm. You'll get back the minimum path cost, the exact node sequence to travel, and a breakdown of each edge traversed along the optimal route.

Enter each edge on a new line in the format: SourceNode DestinationNode Weight. Example: A B 4

The starting node for the shortest path search.

The target node you want to reach.

Results

Shortest Path Cost

--

Optimal Path

--

Number of Hops

--

Total Nodes in Graph

--

Total Edges in Graph

--

Edge Weights Along Shortest Path

Results Table

Frequently Asked Questions

What is Dijkstra's algorithm and how does it work?

Dijkstra's algorithm finds the shortest path between a source node and all other nodes in a weighted graph with non-negative edge weights. It works by repeatedly selecting the unvisited node with the smallest known distance, then relaxing (updating) the distances to its neighbors. This process continues until the destination node is reached or all nodes are visited.

What format should I use to enter edges?

Enter each edge on its own line in the format: SourceNode DestinationNode Weight — for example, 'A B 4' means there is an edge between node A and node B with a weight (cost) of 4. Node names can be letters, numbers, or short strings. Weights must be positive numbers.

What is the difference between a directed and undirected graph?

In an undirected graph, an edge between A and B allows travel in both directions — A to B and B to A. In a directed graph, an edge A B only allows travel from A to B, not the reverse. Choose the type that matches your real-world problem (e.g. one-way roads are directed, two-way roads are undirected).

Can Dijkstra's algorithm handle negative edge weights?

No. Dijkstra's algorithm requires all edge weights to be non-negative (zero or positive). If your graph contains negative weights, you should use the Bellman-Ford algorithm instead, which handles negative edges correctly.

What does 'No path found' mean?

This means there is no connected sequence of edges linking your source node to your destination node. In an undirected graph this happens if the two nodes are in separate disconnected components. In a directed graph, it can happen even if the nodes are connected, if the edge directions don't allow a route from source to destination.

How is the shortest path cost calculated?

The shortest path cost is the sum of all edge weights along the optimal route from the source node to the destination node. Dijkstra's algorithm guarantees this is the minimum possible total weight among all possible routes between the two nodes.

Can I use numbers as node names?

Yes. Node names can be any alphanumeric label — single letters like A, B, C, numbers like 1, 2, 3, or short strings like 'NYC' or 'Hub1'. Just make sure the names in your edge list exactly match what you enter in the Source and Destination fields (case-sensitive).

What happens if I enter duplicate edges?

If you enter two edges between the same pair of nodes, the calculator will keep only the one with the lower weight for undirected graphs, or process both directions independently for directed graphs. It is best practice to enter each edge only once to avoid confusion.

More Math Tools