### Shortest Path Algorithm: Dijkstra algorithm

Quote of the day: Simplicity is prerequisite for reliability.

I previously touched upon the shortest path problem which in summary is a problem involving finding shortest distance between nodes in a graph, provided that the distance between adjacent nodes exists and expressed in numbers. The problem is among many different graph problems defined in Computer Science and there are several algorithms or solutions provided to the shortest path problem.

In this article, I am going to look at a particular algorithm or solution called the Dijkstra algorithm (one of my favourite algorithms) by Edsger Wybe Dijkstra.

Pseudocode

The Dijkstra algorithm is detailed below in a format called a Pseudocode:

Given a graph such as the image above, and a node could be node 0, we follow these steps

- Define a set of nodes which we call Q
- For each node in the graph do the following:
- Initialize the array of distance with all values being infinity
- Initialize the adjacent array with unknown
- Add the node to the set Q
- Set distance from source to itself to be 0.
- While our set Q is not empty repeat the following:
- Select a node in Q with the minimum distance from source to that node. Call that node U.
- Delete that node U from set Q
- For each of the neighboring nodes of node U do the following:
- Calculate the distance to reach the node U from its neighbor node S. now let alt = distance[U] + length(u,v)
- If alt is less than distance[S] then set distance[S] = alt and previous[S] = U
- Return distance and previous arrays

Below is a quick video demonstrating how the algorithm works using an example:

Thank you for reading and watching.