Member-only story

30 Days of Algorithms: From Zero to Intermediate (23/30) —Dijkstra’s Algorithm

Tomas Svojanovsky
5 min readMay 31, 2024

--

Dijkstra’s algorithm is famous for finding the shortest paths between nodes in a weighted graph.

Imagine a map where cities are represented by nodes and roads connecting them are edges. The weights on the edges could represent distance, travel time, or even cost. Dijkstra’s algorithm finds the shortest path between a starting point and any node in the graph.

Example

Here’s the graph where we will find the shortest path from A to E.

Graph example

Algorithm

We will create a table to store information about all nodes. This table will include the shortest distance to each node and the node that came before it in the path.

Initial table

Now we will prepare two lists — visited and unvisited nodes.

visited = []
unvisited [A, B, C, E, F, G]

Step 1

--

--

Tomas Svojanovsky
Tomas Svojanovsky

Written by Tomas Svojanovsky

I'm a full-stack developer. Programming isn't just my job but also my hobby. I like developing seamless user experiences and working on server-side complexities

No responses yet