dijkstra calculator

Interactive Dijkstra Shortest Path Calculator

Enter a weighted graph and compute the shortest path from a source node to a target node using Dijkstra's algorithm.

Use non-negative weights only. Node names can be letters or words (e.g., A, Hub1, Office).
Leave blank to calculate shortest distances from source to all nodes.

What This Dijkstra Calculator Does

This tool computes shortest routes in a weighted graph using Dijkstra's algorithm, one of the most practical and widely used algorithms in computer science. You provide nodes and weighted edges, choose a starting node, and the calculator returns the minimum total cost path to a destination (or to all nodes if no destination is set).

In plain language: if your network has roads, links, routes, or transitions with costs, this calculator helps you find the cheapest path.

How to Use the Calculator

1) Enter your graph edges

Add one edge per line in either format:

  • Space format: A B 7
  • Comma format: A,B,7

Each line means there is an edge from the first node to the second node with the specified weight. If your graph is undirected, the reverse link is automatically included.

2) Choose graph type

  • Undirected: edge A-B works both ways.
  • Directed: edge A-B only works from A to B unless you also add B-A.

3) Set source and target

Source is required. Target is optional. If target is blank, you get all shortest distances from the source.

4) Click calculate

The result includes:

  • Total shortest cost
  • Node-by-node shortest path
  • Distance table for all reachable and unreachable nodes

Why Dijkstra Is Useful

Dijkstra's algorithm appears everywhere:

  • Navigation and maps
  • Network routing
  • Game AI pathfinding
  • Workflow and dependency optimization
  • Transportation planning and logistics

Important Constraints

Dijkstra's algorithm assumes all edge weights are non-negative. If your graph includes negative weights, the shortest-path result can be incorrect. In that case, use Bellman-Ford or another algorithm designed for negative edges.

Example Scenario

Suppose each node is a warehouse and each edge weight is shipping time. Set your source to the main hub and your target to a destination city. The calculator reveals the fastest route and total travel cost, helping you make operational decisions quickly.

Complexity (Quick Note)

Conceptually, Dijkstra can be implemented with a priority queue for strong performance on large graphs. This page uses a straightforward priority selection approach suitable for learning and moderate-sized inputs. For very large graphs, a binary heap implementation is recommended.

Final Tip

Keep your node names clean and consistent (for example, use NYC and not both NYC and nyc). Small formatting mistakes can split one logical node into two different nodes.

🔗 Related Calculators