Shortest-Path Calculator

Find the shortest path in a weighted graph using Dijkstra’s algorithm for nonnegative edge weights.

Graph TheoryAlgorithmsComputer Science Math

Calculate

Start vertex
End vertex
Weighted edges

Enter one edge per line: from to weight. Weights must be nonnegative. This calculator treats edges as undirected by default.

Please enter valid values.

What this calculator teaches

Shortest-path algorithms find the lowest-cost route through a weighted graph. Dijkstra’s algorithm is the classic method when all edge weights are nonnegative.

This appears in maps, networking, routing, scheduling, games, and optimization.

Formula & Symbols

ConceptFormulaMeaning
Path costsum of edge weightsThe total cost of traveling along a path.
Dijkstra updatedist[v] = min(dist[v], dist[u]+w)Relax edges from the current closest vertex.

Step-by-Step Examples

Example 1
Route problem
  • A-C costs 2 and C-B costs 1.
  • A-B directly costs 4.
  • The algorithm keeps the smaller cost A-C-B = 3.
✓ Shortest partial route to B is A → C → B.

Where students use this

🎓
High school and college
Use the tool to check homework and understand each step.
📊
Data and modeling
Connect formulas to tables, graphs, and real values.
🧪
Science and engineering
Use math results inside physics, chemistry, and engineering problems.
💻
Computer science
Apply the same logic to algorithms, systems, and numerical work.

Common Mistakes to Avoid

⚠️
Using negative weights

Dijkstra’s algorithm does not support negative edge weights.

⚠️
Stopping too early

A node is final only when it is selected as the current minimum unvisited node.

⚠️
Confusing fewest edges with shortest weight

The shortest path has the smallest total weight, not always the fewest edges.

Frequently Asked Questions

What is Dijkstra’s algorithm? ▾
It repeatedly chooses the unvisited vertex with the smallest known distance and relaxes its edges.
Can weights be negative? ▾
No. Use Bellman-Ford for graphs with negative weights.
Is the graph directed? ▾
This simple calculator treats edges as undirected to keep classroom examples easy.
What is path cost? ▾
It is the sum of all edge weights along the chosen path.

Related Math Calculators

Formula Explorer connections

Interpretation: This formula or algorithm measures graph structure, traversal, reachability, path cost or network connectivity. Assumption: Specify whether the graph is directed or weighted and whether negative edges, cycles or disconnected components are allowed by the chosen algorithm.

Strongly Connected Components Calculator →Tree Traversal Calculator →A* Search Calculator →Math Formula Explorer →