Dijkstra Shortest Path Calculator

JJ Ben-Joseph headshot JJ Ben-Joseph

Introduction: what this Dijkstra shortest path calculator computes

This Dijkstra shortest path calculator lets you test a small weighted graph by typing its adjacency matrix, picking a start node and an end node, and reading back the minimum-cost route that Dijkstra’s algorithm discovers.

Nodes are numbered from 0 through N−1. Enter weights as zero or positive numbers, use a blank or dash for no edge, and remember that a zero entry is a real edge with no cost rather than an absent connection.

Dijkstra’s algorithm in a shortest path calculator

Inside this Dijkstra shortest path calculator, the algorithm starts from your chosen source node and keeps a table of the cheapest known distance to every other node in the graph.

At each step it locks in the currently closest unvisited node, then checks whether any outgoing edge from that node creates a lower total cost for one of its neighbors.

Core distance update formula

The key operation in Dijkstra shortest path calculations is called relaxation. Suppose the current node is u and a neighbor is v with edge weight wuv. If the best known distance to u is d(u) and the best known distance to v is d(v), then we update using:

d ( v ) = min ( d ( v ) , d ( u ) + w u v ) .

In this calculator, that update is what lets a cheaper route replace an earlier estimate. Because all edge weights must be nonnegative for Dijkstra’s method to stay valid, once a node is chosen as the current minimum its distance will not be improved later.

How to use the Dijkstra shortest path calculator

Follow these steps to set up a Dijkstra shortest path problem in the calculator and read the route back clearly.

  1. Choose the graph size for your Dijkstra shortest path problem.

    Enter an integer between 2 and 6 in the Nodes field. The calculator then builds a square N × N adjacency matrix with one row and one column for each node.

  2. Fill in the weighted adjacency matrix.

    For each ordered pair of nodes (i, j):

    • Enter a nonnegative number for the edge weight from node i to node j.
    • Leave the cell blank or enter a dash (-) if there is no edge from i to j.
    • Zero means a real zero-cost edge, not “no edge”.

    The graph is treated as directed unless you explicitly mirror weights. If you want an undirected edge between i and j, enter the same weight in both (i, j) and (j, i).

  3. Set the start and end nodes for the route.

    Use zero-based labels. For example, with N = 4, valid nodes are 0, 1, 2, and 3. Enter the indices of the start node and the end node in their respective fields so the calculator can compare one source-to-target route.

  4. Run the Dijkstra shortest path calculation.

    Click the button to compute the shortest path. The tool runs Dijkstra’s algorithm from the chosen start node and reports the total distance to the end node together with the path as an ordered list of nodes.

Interpreting the Dijkstra shortest path result

When this Dijkstra shortest path calculator finishes, the output tells you two things: the total cost of the route and the exact node sequence used to reach the destination.

If there is no way to reach the end node from the start node following the edges you entered, the calculator will indicate that the end node is unreachable. In that case, the most useful checks are usually to confirm the edge direction, make sure a connection was not left blank by mistake, and verify that the start and end indices are inside the valid range 0 … N−1.

Worked example: shortest path from node 0 to node 3

To see the Dijkstra shortest path calculator in action, use a graph with four nodes, labeled 0 through 3, and the following directed edges:

The adjacency matrix (rows are sources, columns are destinations) looks like this, where a dash means no edge:

      0   1   2   3
    ----------------
  0 |  -   2   5   -
  1 |  -   -   1   7
  2 |  -   -   -   2
  3 |  -   -   -   -
  

Suppose you set the start node to 0 and the end node to 3. Dijkstra’s algorithm proceeds roughly as follows:

  1. Initialize distances: d(0) = 0, and d(1) = d(2) = d(3) = ∞. Mark all nodes as unvisited.
  2. Pick the unvisited node with smallest tentative distance: node 0.
    • Relax edge 0 → 1: d(1) becomes 2.
    • Relax edge 0 → 2: d(2) becomes 5.
    Mark node 0 as visited.
  3. Next smallest tentative distance is node 1 with d(1) = 2.
    • Relax edge 1 → 2: d(2) can improve to 2 + 1 = 3, so update d(2) = 3.
    • Relax edge 1 → 3: d(3) becomes 2 + 7 = 9.
    Mark node 1 as visited.
  4. Next is node 2 with d(2) = 3.
    • Relax edge 2 → 3: d(3) improves from 9 to 3 + 2 = 5.
    Mark node 2 as visited.
  5. Finally, node 3 has d(3) = 5. It has no outgoing edges, so the process ends.

For this graph, the shortest path from 0 to 3 is 0 → 1 → 2 → 3 with total distance 2 + 1 + 2 = 5. That is the route the calculator will display after you enter the matrix and press the compute button.

When to use Dijkstra vs. other algorithms

Dijkstra’s algorithm is a strong choice for shortest-path work on weighted graphs when every edge cost is nonnegative. The table below shows where it fits alongside a few related algorithms.

Algorithm Supports negative weights? Uses a heuristic? Typical use case
Dijkstra No — requires nonnegative edge weights No Exact shortest paths on small or large graphs with nonnegative costs
Bellman–Ford Yes — handles negative edges (but not negative cycles) No Graphs where some edges have negative weights; detecting negative cycles
A* Usually no negative weights Yes — uses a heuristic estimate to the goal Pathfinding on large maps when you have a good heuristic (e.g., straight-line distance)
Breadth-first search (BFS) Not applicable (assumes all edges equal) No Unweighted graphs where every edge has the same cost or distance

This calculator specifically implements standard Dijkstra for small graphs with nonnegative weights. If you need to handle negative edges or a more specialized routing rule, a different algorithm or a more specialized tool will be a better fit.

Assumptions and limitations for Dijkstra shortest path calculations

These are the practical boundaries this Dijkstra shortest path calculator is built around.

Common input mistakes in Dijkstra shortest path graphs

When you use this Dijkstra shortest path calculator, the mistakes below are the ones most likely to change the route or make the destination look unreachable.

Within those limits, this Dijkstra shortest path calculator is useful for checking hand calculations, building intuition before you code your own implementation, or demonstrating how the algorithm updates tentative distances in a classroom or study session.

Enter a 4×4 adjacency matrix for the current graph. Leave blanks or enter - for no edge.

Interactive details will appear here after you run the calculator.
Enter the graph and choose the start and end nodes.

Arcade Mini-Game: Dijkstra Shortest Path Calculator Calibration Run

Use this quick arcade run to practice separating valid graph inputs from mistakes that would throw off a Dijkstra shortest path calculation before you rely on the calculator output.

Score: 0 Timer: 30s Best: 0

Start the game, then use your pointer or arrow keys to catch useful inputs and avoid bad assumptions.