Dijkstra's algorithm finds shortest paths from one start node in a weighted graph, as long as every edge weight is nonnegative. Reach for it whenever you can model a problem as moving through a graph with nonnegative costs and you want the cheapest route. If you only care about one destination, you can stop as soon as that destination is settled.

The cost might represent distance, travel time, energy, or anything else you want to minimize, and the algorithm works on directed or undirected graphs. The one condition you cannot skip: every edge weight must be at least 00. The core move is greedy, always continuing from the unsettled node with the smallest known distance, and that move is only safe under the nonnegative-weight condition.

The Procedure, Step By Step

  1. Set the starting distances. Give the start node distance 00 and every other node an initial distance of infinity.
  2. Pick the smallest tentative distance. Among the unvisited nodes, choose the one with the smallest current distance.
  3. Relax its edges. Update each neighbor if going through the chosen node gives a shorter path.
  4. Settle the node. Mark the chosen node as settled and repeat until the destination or all reachable nodes are done.

In compact form, the full loop is: choose a start node; set its distance to 00 and every other distance to \infty; select the unsettled node with the smallest tentative distance; relax its outgoing edges; mark that node as settled; repeat until all reachable nodes are settled, or stop once your target node is settled.

Step 3 is called relaxation. A settled node is finished: its shortest distance is final, provided the graph has no negative edge weights. If you also store which previous node caused each update, you can recover the actual path, not just the final distance.

Running The Procedure On A Full Graph

Suppose the graph has these edge weights:

  • AB=4A \to B = 4
  • AC=1A \to C = 1
  • CB=2C \to B = 2
  • BD=1B \to D = 1
  • CD=5C \to D = 5

We want the shortest path from AA to DD. Start with:

dist(A)=0,dist(B)=,dist(C)=,dist(D)=\text{dist}(A)=0,\quad \text{dist}(B)=\infty,\quad \text{dist}(C)=\infty,\quad \text{dist}(D)=\infty

Settle AA first and relax its edges:

dist(B)=4,dist(C)=1\text{dist}(B)=4,\quad \text{dist}(C)=1

The smallest unsettled distance is now at CC, so settle CC next. Going through CC:

  • Through CC, the path to BB costs 1+2=31+2=3, which improves on 44, so update dist(B)=3\text{dist}(B)=3.
  • Through CC, the path to DD costs 1+5=61+5=6, so set dist(D)=6\text{dist}(D)=6.

Now BB has the smallest unsettled distance, so settle BB. Going through BB, the path to DD costs:

3+1=43+1=4

That improves on 66, so update dist(D)\text{dist}(D) from 66 to 44. Now DD is the smallest unsettled node. Settle it, and stop.

The shortest path is:

ACBDA \to C \to B \to D

with total cost:

1+2+1=41+2+1=4

This example shows the key pattern: a route that first looked worse, ABA \to B, became better later through CC. Dijkstra's algorithm allows that while BB is still unsettled. Once a node is settled, its distance will not change.

Why The Greedy Step Is Safe

The greedy choice in step 2 deserves a justification, because it is exactly what breaks without the nonnegative condition.

Suppose the current cheapest unsettled node has distance dd. Any new path to that node found later would have to come from another unsettled node whose tentative distance is at least dd, and then add an edge of weight at least 00. So that later route cannot beat dd. That is why settling the smallest tentative distance is safe when all weights are nonnegative.

If a negative edge exists, that argument breaks. A path that looks expensive now might become cheaper later, which means settling a node too early can give the wrong answer.

Where Students Slip, And How To Check Each Step

Using it with a negative edge

Even one negative edge can break the greedy logic. Self-check: scan every weight for a negative value before you start; if one exists, switch to a different shortest-path method.

Treating "seen" as "finished"

A node can already have a tentative distance and still be unfinished. Self-check: only a settled node has a final shortest distance, so do not lock in a value until you settle it in step 4.

Forgetting the previous-node table

Distances tell you the cost but not the route. Self-check: if you need the path, confirm you stored which node last improved each distance.

Where Dijkstra's Algorithm Is Used

Dijkstra's algorithm is used when a problem can be modeled as moving through a graph with nonnegative costs:

  • Route planning on maps
  • Network routing
  • Robot motion on weighted grids
  • Game pathfinding when movement costs vary

It is also a standard example of a greedy algorithm whose correctness depends on a precise condition. To internalize the procedure, draw a graph with five nodes and positive edge weights, run the algorithm by hand with a distance table, and watch exactly when each node becomes safe to settle.

Frequently Asked Questions

What problem does Dijkstra's algorithm solve?
Dijkstra's algorithm finds shortest paths from one start node in a weighted graph, where edge costs might represent distance, travel time, or energy. It works on directed or undirected graphs, with one key condition: every edge weight must be nonnegative. If you only care about one destination, you can stop as soon as that destination is settled.
Why does Dijkstra's algorithm require nonnegative edge weights?
The algorithm is greedy: it always continues from the unsettled node with the smallest known distance and treats settled nodes as final. That works only if no later route can become cheaper, which is guaranteed when weights are at least zero. With negative edges, a settled node's distance could still be improved, breaking the algorithm's core assumption.
What does relaxation mean in Dijkstra's algorithm?
Relaxation is the update step. When a node is processed, the algorithm checks each neighbor and asks whether the route through the current node is cheaper than the neighbor's stored tentative distance. If it is, the distance is updated. In a worked example, a path that first looked worse can become better later through relaxation, as long as the node is not yet settled.
How do you recover the actual shortest path, not just the distance?
Store a parent pointer alongside each distance: whenever a relaxation step improves a node's distance, record which previous node caused that update. After the search, walk the parent pointers backward from the destination to the start to reconstruct the route. Without this bookkeeping, Dijkstra's algorithm only reports final distances.

Need help with a problem?

Upload your question and get a verified, step-by-step solution in seconds.

Open GPAI Solver →