Dijkstra's Algorithm
Dijkstra's algorithm allows us to find the shortest path between any two vertices
of a graph.
It differs from the minimum spanning tree because the shortest distance
between two vertices might not include all the vertices of the graph.
How Dijkstra's Algorithm works
Dijkstra's Algorithm works on the basis that any subpath B -> D of the shortest
path A -> D between vertices A and D is also the shortest path between vertices
B and D.
Each subpath is the shortest path
Djikstra used this property in the opposite direction i.e we overestimate the
distance of each vertex from the starting vertex. Then we visit each node and its
neighbors to find the shortest subpath to those neighbors.
The algorithm uses a greedy approach in the sense that we find the next best
solution hoping that the end result is the best solution for the whole problem.
Example of Dijkstra's algorithm
It is easier to start with an example and then think about the algorithm.
Start with a weighted graph
Choose a starting vertex and assign infinity path values to all other devices
Go to each vertex and update its path length
If the path length of the adjacent vertex is lesser than new path length, don't update it
Avoid updating path lengths of already visited vertices
After each iteration, we pick the unvisited vertex with the least path length. So we choose
5 before 7
Notice how the rightmost vertex has its path length updated twice
Repeat until all the vertices have been visited
Djikstra's algorithm pseudocode
We need to maintain the path distance of every vertex. We can store that in an
array of size v, where v is the number of vertices.
We also want to be able to get the shortest path, not only know the length of the
shortest path. For this, we map each vertex to the vertex that last updated its
path length.
Once the algorithm is over, we can backtrack from the destination vertex to the
source vertex to find the path.
A minimum priority queue can be used to efficiently receive the vertex with least
path distance.
function dijkstra(G, S)
for each vertex V in G
distance[V] <- infinite
previous[V] <- NULL
If V != S, add V to Priority Queue Q
distance[S] <- 0
while Q IS NOT EMPTY
U <- Extract MIN from Q
for each unvisited neighbour V of U
tempDistance <- distance[U] + edge_weight(U, V)
if tempDistance < distance[V]
distance[V] <- tempDistance
previous[V] <- U
return distance[], previous[]
Reverse Polish Notation
Reverse Polish Notation is a way of expressing arithmetic expressions that avoids
the use of brackets to define priorities for evaluation of operators. In ordinary
notation, one might write
(3 + 5) * (7 – 2)
and the brackets tell us that we have to add 3 to 5, then subtract 2 from 7, and
multiply the two results together. In RPN, the numbers and operators are listed one
after another, and an operator always acts on the most recent numbers in the list.
The numbers can be thought of as forming a stack, like a pile of plates. The most
recent number goes on the top of the stack. An operator takes the appropriate
number of arguments from the top of the stack and replaces them by the result of
the operation.
In this notation the above expression would be
35+72–*
Reading from left to right, this is interpreted as follows:
• Push 3 onto the stack.
• Push 5 onto the stack. Reading from the top, the stack now contains (5, 3).
• Apply the + operation: take the top two numbers off the stack, add them
together, and put the result back on the stack. The stack now contains just
the number 8.
• Push 7 onto the stack.
• Push 2 onto the stack. It now contains (2, 7, 8).
• Apply the – operation: take the top two numbers off the stack, subtract the
top one from the one below, and put the result back on the stack. The stack
now contains (5, 8).
• Apply the * operation: take the top two numbers off the stack, multiply them
together, and put the result back on the stack. The stack now contains just
the number 40.