Contents

Dijkstra's Algorithm

The cover image was generated by ChatGPT.

Introduction

Dijkstra’s Algorithm is a method for finding the shortest path between nodes in a graph. It was originally designed to find the shortest path between two points, but was later extended to find the shortest paths from a fixed point to all other points, forming what is called a “shortest path tree”.

Example of Dijkstra's Algorithm, retrieved from Wikipedia on July 2, 2025, by Subh83

How It Works

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Dijkstra's%20Algorithm/example_0.jpg
Graph showing six locations.

As shown above, suppose there are six locations labeled $A$, $B$, $C$, $D$, $E$, and $F$. We set $A$ as the starting point and $F$ as the destination, and we aim to find the shortest path from $A$ to $F$.

First, we calculate the distances between connected nodes, as shown below.

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Dijkstra's%20Algorithm/example_1.jpg
Distances between each path.

Note
The distance between two points can also be seen as the weight of the edge. Dijkstra’s algorithm and most of its variants cannot handle negative weights.

To find the shortest paths from $A$, we create a table to record the previous node and the current distance. Initially, all distances are set to infinity ($\infty$) to represent an unknown or infinite distance.

NodeMarkedDistancePrevious Node
A$\infty$
B$\infty$
C$\infty$
D$\infty$
E$\infty$
F$\infty$

Since we start at point $A$, the distance from $A$ to itself is 0.

NodeMarkedDistancePrevious Node
A0
B$\infty$
C$\infty$
D$\infty$
E$\infty$
F$\infty$

Next, among all unmarked nodes, we select the one with the shortest distance and mark it as the optimal path.

NodeMarkedDistancePrevious Node
A0
B$\infty$
C$\infty$
D$\infty$
E$\infty$
F$\infty$

After marking $A$, update its adjacent nodes. From the diagram, we see that $A$ is connected to $B$ and $C$, both with distances of 4, which are smaller than $\infty$, so we update them.

NodeMarkedDistancePrevious Node
A0
B4A
C4A
D$\infty$
E$\infty$
F$\infty$

We again select the unmarked node with the shortest distance. Both $B$ and $C$ have a distance of 4. We mark $B$ first.

NodeMarkedDistancePrevious Node
A0
B4A
C4A
D$\infty$
E$\infty$
F$\infty$

Now update $B$’s neighbors, $D$ and $E$. From $A$ through $B$ to $D$ is $4 + 7 = 11$, which is less than $\infty$, so we update $D$’s distance and set its previous node to $B$. Similarly, $A \rightarrow B \rightarrow E = 4 + 5 = 9$; we also update $E$.

NodeMarkedDistancePrevious Node
A0
B4A
C4A
D11B
E9B
F$\infty$

Now we continue. Among the unmarked nodes, $C$ has the shortest distance. We mark it and attempt to update its neighbors $E$ and $F$.

NodeMarkedDistancePrevious Node
A0
B4A
C4A
D11B
E9B
F$\infty$

The path from $C$ to $F$ is 16, so total distance is $4 + 16 = 20$, which is smaller than $\infty$, so we update $F$. As for $E$, the path via $C$ is 12, which is greater than the current distance of 9, so we don’t update.

NodeMarkedDistancePrevious Node
A0
B4A
C4A
D11B
E9B
F20C

Repeat this process of marking and updating until all nodes are marked. The final table will look like this:

NodeMarkedDistancePrevious Node
A0
B4A
C4A
D11B
E9B
F18E

From this table, we can see that the shortest path from $A$ to $F$ is 18. We can backtrack from $F$ to $A$ to determine the shortest path: $$ A \Rightarrow B \Rightarrow E \Rightarrow F $$

Algorithm

Based on the above example, the general steps of Dijkstra’s Algorithm can be summarized as follows:

  1. Select the unmarked node with the smallest distance from the start.
  2. Mark that node as processed.
  3. For each neighboring node, calculate the total distance through the current node.
  4. If the total distance is less than the currently recorded distance, update the neighbor’s distance and record the current node as its previous node. If not, skip it.
  5. Repeat from step 1 until all nodes are marked.

Conclusion

When I first encountered this algorithm, I was amazed. It turns out that what we know as the “shortest path” can actually be computed with just a few simple steps. If I hadn’t seen this algorithm, I might have tried to brute-force every possible path!

References