Dijkstra's Shortest Path Calculator

Weighted graph · Shortest distances from source · Step-by-step algorithm trace · Path visualization

Input:
Graph Type:

Example: A B 5 = edge from A to B with weight 5. Node names can be letters or numbers.

Quick Examples

Simple Network

5 nodes · 7 edges · undirected

City Roads

8 nodes · directed · S → T

Classic Graph

6 nodes · undirected · source 1

What is Dijkstra's Algorithm?

Dijkstra's algorithm is one of the most celebrated algorithms in computer science. Invented by Dutch computer scientist Edsger W. Dijkstra in 1956 and published in 1959, it solves the single-source shortest path problem for a weighted graph with non-negative edge weights. Given a starting (source) vertex, the algorithm computes the minimum-cost path from that source to every other vertex in the graph.

The algorithm is a classic example of a greedy strategy: at each step it selects the unvisited vertex with the currently smallest known distance, then uses that vertex to try to improve the known distances to its neighbours. This process is known as relaxation.

How Dijkstra's Algorithm Works

  1. Initialise all distances to Infinity, except the source which is set to 0.
  2. Mark all vertices as unvisited. Place all vertices in an unvisited set.
  3. Select the unvisited vertex u with the smallest tentative distance.
  4. For each unvisited neighbour v of u, compute alt = dist[u] + weight(u, v).
  5. If alt < dist[v], update dist[v] = alt and record prev[v] = u.
  6. Mark u as visited (remove from unvisited set).
  7. Repeat from step 3 until all vertices are visited or the target is reached.

To reconstruct the path to any vertex, trace back through the prev[] array from the target to the source.

Dijkstra's Algorithm Example

Consider the Simple Network example with 5 nodes (A–E) and the following edges: A-B=4, A-C=2, B-C=1, B-D=5, C-D=8, C-E=10, D-E=2. Starting from A:

StepVisitingdist[A]dist[B]dist[C]dist[D]dist[E]
Init0
1A042
2C0321012
3B032812
4D032810
5E032810

Final shortest distances from A: B=3 (A→C→B), C=2 (A→C), D=8 (A→C→B→D), E=10 (A→C→B→D→E).

Time Complexity Analysis

ImplementationTime ComplexitySpaceBest For
Array / naiveO(V²)O(V)Dense graphs
Binary heapO((V + E) log V)O(V + E)Sparse graphs
Fibonacci heapO(E + V log V)O(V + E)Theoretical optimum

This calculator uses the O(V²) array implementation, which is perfectly efficient for graphs with up to 15 nodes.

When to Use Dijkstra's vs Other Algorithms

AlgorithmNegative WeightsSingle SourceAll PairsComplexity
Dijkstra'sNoYesNoO(V² ) or O(E log V)
Bellman-FordYesYesNoO(VE)
Floyd-WarshallYesNoYesO(V³)
A* SearchNoYes (single target)NoO(E log V) with heuristic
BFSN/A (unweighted)YesNoO(V + E)

Real-World Applications

  • GPS Navigation: Finding the fastest or shortest route between two locations on a road network.
  • Network Routing: Internet routers use variants of Dijkstra's (OSPF protocol) to determine the best path for data packets.
  • Game Pathfinding: NPC characters in video games navigate terrain using Dijkstra's or its heuristic variant A*.
  • Logistics & Supply Chain: Optimising delivery routes to minimise distance or cost.
  • Social Networks: Finding the shortest chain of connections between two people (degrees of separation).
  • Airline Route Planning: Computing the minimum-cost itinerary between two airports.

Frequently Asked Questions

What is Dijkstra's algorithm?
Dijkstra's algorithm is a greedy shortest-path algorithm invented by Edsger W. Dijkstra in 1956. It finds the shortest (minimum-weight) path from a single source vertex to all other vertices in a weighted graph with non-negative edge weights. It works by repeatedly selecting the unvisited vertex with the smallest tentative distance, then updating its neighbours (relaxation).
What are the limitations of Dijkstra's algorithm?
Dijkstra's algorithm does not work correctly with negative edge weights — it can produce incorrect results. If your graph has negative weights, use the Bellman-Ford algorithm instead. Dijkstra's works on both directed and undirected graphs as long as all weights are ≥ 0.
What is the time complexity of Dijkstra's algorithm?
The naive array-based implementation runs in O(V²) where V is the number of vertices. Using a binary min-heap (priority queue) improves this to O((V + E) log V). The theoretical best is O(E + V log V) with a Fibonacci heap. For small graphs (≤15 nodes) the O(V²) approach is perfectly fast.
How do I enter a graph for the calculator?
Use the Edge List tab and enter one edge per line in the format From To Weight. For example, A B 5 creates an edge between A and B with weight 5. Node names can be any letters or numbers. Alternatively, use the Adjacency Matrix tab for a visual grid input.
What is the difference between directed and undirected graphs?
In a directed graph, an edge from A to B allows travel only from A to B. In an undirected graph, every edge works both ways — A-B allows travel A→B and B→A. Dijkstra's algorithm works on both types, but the resulting shortest paths can differ significantly depending on edge direction.
Can Dijkstra's algorithm handle disconnected graphs?
Yes. If a vertex cannot be reached from the source, its shortest distance remains ∞ (Infinity). Dijkstra's algorithm handles disconnected graphs gracefully — the algorithm simply stops updating unreachable nodes, and they appear with distance ∞ in the results table.
What is the difference between Dijkstra's and BFS?
BFS (Breadth-First Search) finds the shortest path in an unweighted graph, minimising the number of edges (hop count). It runs in O(V+E). Dijkstra's generalises BFS to weighted graphs, minimising total edge weight. BFS ignores weights entirely — use it only when all edges are equal. Use Dijkstra's whenever edges have different costs.