## ~ Searching Algorithms - Dijkstra's Algorithm

Sorting Algorithms - Python Code Sorted

### Dijkstra's shortest path Algorithm

Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. It was conceived by computer scientist Edsger W. Dijkstra in 1958 and published three years later. The algorithm exists in many variants; Dijkstra's original variant found the shortest path between two nodes, but a more 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

### Challenge

#1 Create your own code to illustrate the functionality of this algorithm

#2 Comment each line of the python solution below to show your understanding of the algorithm

### Solution

```nodes = ('A', 'B', 'C', 'D', 'E', 'F', 'G')
distances = {
'B': {'A': 5, 'D': 1, 'G': 2},
'A': {'B': 5, 'D': 3, 'E': 12, 'F' :5},
'D': {'B': 1, 'G': 1, 'E': 1, 'A': 3},
'G': {'B': 2, 'D': 1, 'C': 2},
'C': {'G': 2, 'E': 1, 'F': 16},
'E': {'A': 12, 'D': 1, 'C': 1, 'F': 2},
'F': {'A': 5, 'E': 2, 'C': 16}}

unvisited = {node: None for node in nodes} #using None as +inf
visited = {}
current = 'B'
currentDistance = 0
unvisited[current] = currentDistance

while True:
for neighbour, distance in distances[current].items():
if neighbour not in unvisited: continue
newDistance = currentDistance + distance
if unvisited[neighbour] is None or unvisited[neighbour] > newDistance:
unvisited[neighbour] = newDistance
visited[current] = currentDistance
del unvisited[current]
if not unvisited: break
candidates = [node for node in unvisited.items() if node]
current, currentDistance = sorted(candidates, key = lambda x: x)

print(visited)
```

### Explanation with Example

For a given source node in the graph, the algorithm finds the shortest path between that node and every other. It can also be used for finding the shortest 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. For example, if the nodes of the graph represent cities and edge path costs represent driving distances between pairs of cities connected by a direct road, Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. As a result, the shortest path algorithm is widely used in network routing protocols, most notably IS-IS (Intermediate System to Intermediate System) and Open Shortest Path First (OSPF). It is also employed as a subroutine in other algorithms such as Johnson's.

### Wiki animation and illustration

#1: Dijkstra's algorithm to find the shortest path between a and b. It picks the unvisited vertex with the lowest distance, calculates the distance through it to each unvisited neighbor, and updates the neighbor's distance if smaller. Mark visited (set to red) when done with neighbors. #2: Illustration of Dijkstra's algorithm finding a path from a start node (lower left, red) to a goal node (upper right, green) in a robot motion planning problem. Open nodes represent the "tentative" set (aka set of "unvisited" nodes). Filled nodes are visited ones, with color representing the distance: the greener, the farther. Nodes in all the different directions are explored uniformly, appearing more-or-less as a circular wavefront as Dijkstra's algorithm uses a heuristic identically equal to 0. #2: A demo of Dijkstra's algorithm based on Euclidean distance. Red lines are the shortest path covering, i.e., connecting u and prev[u]. Blue lines indicate where relaxing happens, i.e., connecting v with a node u in Q, which gives a shorter path from the source to v.

## Pseudocode

In the following algorithm, the code u ← vertex in Q with min dist[u], searches for the vertex u in the vertex set Q that has the least dist[u] value. length(uv) returns the length of the edge joining (i.e. the distance between) the two neighbor-nodes u and v. The variable alt on line 17 is the length of the path from the root node to the neighbor node v if it were to go through u. If this path is shorter than the current shortest path recorded for v, that current path is replaced with this alt path. The prev array is populated with a pointer to the "next-hop" node on the source graph to get the shortest route to the source.

``` 1  function Dijkstra(Graph, source):
2
3      create vertex set Q
4
5      for each vertex v in Graph:             // Initialization
6          dist[v] ← INFINITY                  // Unknown distance from source to v
7          prev[v] ← UNDEFINED                 // Previous node in optimal path from source
8          add v to Q                          // All nodes initially in Q (unvisited nodes)
9
10      dist[source] ← 0                        // Distance from source to source
11
12      while Q is not empty:
13          u ← vertex in Q with min dist[u]    // Node with the least distance will be selected first
14          remove u from Q
15
16          for each neighbor v of u:           // where v is still in Q.
17              alt ← dist[u] + length(u, v)
18              if alt < dist[v]:               // A shorter path to v has been found
19                  dist[v] ← alt
20                  prev[v] ← u
21
22      return dist[], prev[]```

The following two examples have been taken from: algocoding.wordpress.com

### Solution #2 with explanation

```# Dijktra's shortest path algorithm. Prints the path from source to target.

INF = ((1<<63) - 1)//2
pred = { x:x for x in adj }
dist = { x:INF for x in adj }
dist[source] = 0
PQ = []
heapq.heappush(PQ, [dist[source], source])

while(PQ):
u = heapq.heappop(PQ)  # u is a tuple [u_dist, u_id]
u_dist = u
u_id = u
if u_dist == dist[u_id]:
#if u_id == target:
#    break
v_id = v
w_uv = v
if dist[u_id] +  w_uv < dist[v_id]:
dist[v_id] = dist[u_id] + w_uv
heapq.heappush(PQ, [dist[v_id], v_id])
pred[v_id] = u_id

if dist[target]==INF:
stdout.write("There is no path between " + source + " and " + target)
else:
st = []
node = target
while(True):
st.append(str(node))
if(node==pred[node]):
break
node = pred[node]
path = st[::-1]
stdout.write("The shortest path is: " + " ".join(path))```

### Explanation

• Step 0 (initialization): Assign to all nodes a distance value of ‘inf’, except for the source node which has a distance of zero. Initialize an empty set S. Initialize a predecessor dictionary.
• Repeat loop:
• Step 1: add to S the node with the smallest distance (among the nodes that touch S via an edge). Call that node u.
• Step 2: for u consider all new outgoing edges {u, v1}, {u, v2}, …, {u, vk} (i.e. edges that lead to nodes which are not yet in S). Check if these edges lead to a smaller distance value for the nodes v1, v2, …, vk. If it does for vj, update the distance for vj to the smaller value and set the predecessor of vj to u.
• If there are no nodes left that touch S via an edge, then break the loop.
• Lines 5 to 9 show the initialization. INF is initialized to half of the maximum value of a signed 64 bit integer (a signed 64 bit integer can display integers from $-2^{63}$ to $(2^{63} - 1)$.
• In lines 9 and 10 we initialize a priority queue and insert the tuple (dist[source], source) since we start with the source node.
• Lines 12 to 25 implement the repeating loop. In line 13 we try to find the next node with the smallest distance value. We do this with a priority queue. In line 16 we discard “wrong tuples”, see also Part 3a.
• Lines 17 and 18 are optional. They make the algorithm stop once the minimum distance for the target node has been found. Note though that if you uncomment these lines the distances to other nodes beside the target node are not necessarily found, so uncomment these lines only if you are interested in a particular target node.
• In line 27 we check whether a path has been found.
• In lines 30 to 38 we reconstruct the shortest path with the help of the predecessor dictionary.
• In particular this is done in lines 32 to 36 where we follow the shortest path backwards from the target to the source. We recognize the source by checking if `pred[node] == node`, see also the way we initialized the predecessor dictionary in line 6.

Notes:
– Dijkstra’s algorithm only works if the edge weights are positive.
– We’ve used a priority queue that does not support a decrease-key operation as described in Part 3a. This differs from the description in the literature where a priority queue with a decrease-key operation is used.
– You have probably noticed that in our implementation we did not use a set S that contains the nodes for which we have found the minimum distance. The priority queue holds those nodes that “touch” our set S via an edge, see also part 1. When a node gets extracted from the priority queue it means we are adding it to our set S.
– Line 22 automatically ignores edges that go to nodes v within S because `dist[u_id] + w_uv < dist[v_id]` can never be true due to the fact that `dist[v_id]` is the minimum distance for v once it has been added to S.

### Python Graph Implementation

```import heapq
from sys import stdin, stdout

# Dijktra's shortest path algorithm. Prints the path from source to target.

INF = ((1<<63) - 1)//2
pred = { x:x for x in adj }
dist = { x:INF for x in adj }
dist[ source ] = 0
PQ = []
heapq.heappush(PQ, [dist[ source ], source])

while(PQ):
u = heapq.heappop(PQ)  # u is a tuple [u_dist, u_id]
u_dist = u
u_id = u
if u_dist == dist[u_id]:
#if u_id == target:
#    break
v_id = v
w_uv = v
if dist[u_id] +  w_uv < dist[v_id]:
dist[v_id] = dist[u_id] + w_uv
heapq.heappush(PQ, [dist[v_id], v_id])
pred[v_id] = u_id

if dist[target]==INF:
stdout.write("There is no path between ", source, "and", target)
else:
st = []
node = target
while(True):
st.append(str(node))
if(node==pred[node]):
break
node = pred[node]
path = st[::-1]
stdout.write("The shortest path is: " + " ".join(path) + "\n\n")
stdout.write("The distance from 'a' to 'i' is: " + str(dist['i']) + "\n\n")
stdout.write("distance dictionary: " + str(dist) + "\n\n")
stdout.write("predecessor dictionary: " + str(pred))

#----------------------------------------------------------

def main():

adj = {'c': [('b', 0.32), ('e', 0.17), ('f', 0.91)],
'g': [('d', 0.17), ('e', 0.27), ('h', 0.92)],
'i': [('e', 1.98), ('f', 0.13), ('h', 0.22)],
'f': [('c', 0.91), ('e', 0.33), ('i', 0.13)],
'h': [('e', 0.18), ('g', 0.92), ('i', 0.22)],
'd': [('a', 0.72), ('e', 0.29), ('g', 0.17)],
'a': [('b', 0.95), ('d', 0.72), ('e', 1.75)],
'e': [('a', 1.75), ('b', 0.82), ('c', 0.17), ('d', 0.29), ('f', 0.33), ('g', 0.27), ('h', 0.18), ('i', 1.98)],
'b': [('a', 0.95), ('c', 0.32), ('e', 0.82)]}

#----------------------------------------------------------

if __name__ == "__main__":
main()
```

### History, Description and some interesting facts

Dijkstra thought about the shortest path problem when working at the Mathematical Center in Amsterdam in 1956 as a programmer to demonstrate the capabilities of a new computer called ARMAC.His objective was to choose both a problem as well as an answer (that would be produced by computer) that non-computing people could understand. He designed the shortest path algorithm and later implemented it for ARMAC for a slightly simplified transportation map of 64 cities in the Netherlands (64, so that 6 bits would be sufficient to encode the city number). A year later, he came across another problem from hardware engineers working on the institute's next computer: minimize the amount of wire needed to connect the pins on the back panel of the machine. As a solution, he re-discovered the algorithm known as Prim's minimal spanning tree algorithm (known earlier to Jarník, and also rediscovered by Prim). Dijkstra published the algorithm in 1959, two years after Prim and 29 years after Jarník.

### Refer to Animation #2

Illustration of Dijkstra's algorithm finding a path from a start node (lower left, red) to a goal node (upper right, green) in a robot motion planning problem. Open nodes represent the "tentative" set (aka set of "unvisited" nodes). Filled nodes are visited ones, with color representing the distance: the greener, the farther. Nodes in all the different directions are explored uniformly, appearing more-or-less as a circular wavefront as Dijkstra's algorithm uses a heuristic identically equal to 0.

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.

1. Assign to every node a tentative distance value: set it to zero for our initial node and to infinity for all other nodes.
2. Set the initial node as current. Mark all other nodes unvisited. Create a set of all the unvisited nodes called the unvisited set.
3. 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.
4. 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.
5. 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.
6. 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.

### Description

Note: For ease of understanding, this discussion uses the terms intersectionroad and map — however, in formal terminology these terms are vertexedge and graph, respectively.

Suppose you would like to find the shortest path between two intersections on a city map: a starting point and a destination. Dijkstra's algorithm initially marks the distance (from the starting point) to every other intersection on the map with infinity. This is done not to imply there is an infinite distance, but to note that those intersections have not yet been visited; some variants of this method simply leave the intersections' distances unlabeled. Now, at each iteration, select the current intersection. For the first iteration, the current intersection will be the starting point, and the distance to it (the intersection's label) will be zero. For subsequent iterations (after the first), the current intersection will be a closest unvisited intersection to the starting point (this will be easy to find).

From the current intersection, update the distance to every unvisited intersection that is directly connected to it. This is done by determining the sum of the distance between an unvisited intersection and the value of the current intersection, and relabeling the unvisited intersection with this value (the sum), if it is less than its current value. In effect, the intersection is relabeled if the path to it through the current intersection is shorter than the previously known paths. To facilitate shortest path identification, in pencil, mark the road with an arrow pointing to the relabeled intersection if you label/relabel it, and erase all others pointing to it. After you have updated the distances to each neighboring intersection, mark the current intersection as visited, and select an unvisited intersection with minimal distance (from the starting point) – or the lowest label—as the current intersection. Nodes marked as visited are labeled with the shortest path from the starting point to it and will not be revisited or returned to.

Continue this process of updating the neighboring intersections with the shortest distances, then marking the current intersection as visited and moving onto a closest unvisited intersection until you have marked the destination as visited. Once you have marked the destination as visited (as is the case with any visited intersection) you have determined the shortest path to it, from the starting point, and can trace your way back, following the arrows in reverse; in the algorithm's implementations, this is usually done (after the algorithm has reached the destination node) by following the nodes' parents from the destination node up to the starting node; that's why we also keep track of each node's parent.

This algorithm makes no attempt to direct "exploration" towards the destination as one might expect. Rather, the sole consideration in determining the next "current" intersection is its distance from the starting point. This algorithm therefore expands outward from the starting point, interactively considering every node that is closer in terms of shortest path distance until it reaches the destination. When understood in this way, it is clear how the algorithm necessarily finds the shortest path. However, it may also reveal one of the algorithm's weaknesses: its relative slowness in some topologies.