Creation: January 10 2018

Modified: November 26 2019

Dijkstra algorithm is an algorithm for finding the shortes paths between nodes in a graph.

A very common variant fixes a single node as the "source" node and finds
shortest paths from the source to all other nodes in the graph, producing a
**shortest-path tree**.

It can also be used for finding the shortes paths from a single node to a single destination node by stopping the algorithm once the shortest path to the destination node has been determined.

Let the node at which we are starting be called the initial node. Let the distance of node Y be the distance from the initial node to Y. Dijkstra's algorithm will assign some initial distance values and will try to improve them step by step.

- Assign to every node a tentative distance value: set it to zero for our initial node and to infinity for all other nodes.
- Set the initial node as current. Mark all other nodes unvisited. Create a set of all the unvisited nodes called the unvisited set.
- For the current node, consider all of its neighbors and calculate their tentative distances. Compare the newly calculated tentative distance to the current assigned value and assign the smaller one. For example, if the current node A is marked with a distance of 6, and the edge connecting it with a neighbor B has length 2, then the distance to B (through A) will be 6 + 2 = 8. If B was previously marked with a distance greater than 8 then change it to 8. Otherwise, keep the current value.
- When we are done considering all of the neighbors of the current node, mark the current node as visited and remove it from the unvisited set. A visited node will never be checked again.
- If the destination node has been marked visited (when planning a route between two specific nodes) or if the smallest tentative distance among the nodes in the unvisited set is infinity (when planning a complete traversal; occurs when there is no connection between the initial node and remaining unvisited nodes), then stop. The algorithm has finished.
- Otherwise, select the unvisited node that is marked with the smallest tentative distance, set it as the new "current node", and go back to step 3.

```
function Dijkstra(graph, source):
create vertex set Q
for each vertex v in graph:
add v to Q // Add the node to the set Q
dist[v] = INFINITY // Unknown distance from source to v
prev[v] = UNDEFINED // Previous node in optimal path from source
dist[source] = 0 // Distance from source to source
while Q is not empty:
u = vertex in Q with min dist[u]
remove u from Q
for each neighbor v of u
alt = dist + length(u, v)
if alt < dist[v]
dist[v] = alt
prev[v] = u
return dist[], prev[] // return distances and path links
```

The shortest path algorithm is widely used in network routing protocols, most notably IS-IS and OSPF.

Both the protocols are link-state routing protocol used for routing data within an autonomous system (interior gateway protocols), operating by reliably flooding link state information throughout a network of routers.