Dijkstra's Shortest Path Algorithm With Examples
Dijkstra's Shortest Path Algorithm With Examples
Dijkstra's Algorithm
The following tutorial will teach us about Dijkstra's Shortest Path Algorithm. We will understand the
working of Dijkstra's Algorithm with a stepwise graphical explanation.
Components of a Graph
1. Vertices:Vertices are the basic units of the graph used to represent real-life objects, persons,
or entities. Sometimes, vertices are also known as Nodes.
2. Edges:Edges are drawn or used to connect two vertices of the graph. Sometimes, edges are
also known as Arcs.
https://www.javatpoint.com/dijkstras-algorithm 2/37
4/25/24, 12:33 AM Dijkstra's Shortest Path Algorithm with Examples - javatpoint
In the above figure, the Vertices/Nodes are denoted with Colored Circles, and the Edges are
denoted with the lines connecting the nodes.
Graphs are used to solve many real-life problems. Graphs are utilized to represent the networks.
These networks may include telephone or circuit networks or paths in a city.
For example, we could use Graphs to design a transportation network model where the vertices
display the facilities that send or receive the products, and the edges represent roads or paths
connecting them. The following is a pictorial representation of the same:
https://www.javatpoint.com/dijkstras-algorithm 3/37
4/25/24, 12:33 AM Dijkstra's Shortest Path Algorithm with Examples - javatpoint
Graphs are also utilized in different Social Media Platforms like LinkedIn, Facebook, Twitter, and
more. For example, Platforms like Facebook use Graphs to store the data of their users where every
person is indicated with a vertex, and each of them is a structure containing information like Person
ID, Name, Gender, Address, etc.
Types of Graphs
1. Undirected Graph
https://www.javatpoint.com/dijkstras-algorithm 4/37
4/25/24, 12:33 AM Dijkstra's Shortest Path Algorithm with Examples - javatpoint
2. Directed Graph
Undirected Graph: A Graph with edges that do not have a direction is termed an Undirected
Graph. The edges of this graph imply a two-way relationship in which each edge can be traversed in
both directions. The following figure displays a simple undirected graph with four nodes and five
edges.
Directed Graph: A Graph with edges with direction is termed a Directed Graph. The edges of this
graph imply a one-way relationship in which each edge can only be traversed in a single direction.
The following figure displays a simple directed graph with four nodes and five edges.
https://www.javatpoint.com/dijkstras-algorithm 5/37
4/25/24, 12:33 AM Dijkstra's Shortest Path Algorithm with Examples - javatpoint
The absolute length, position, or orientation of the edges in a graph illustration characteristically
does not have meaning. In other words, we can visualize the same graph in different ways by
rearranging the vertices or distorting the edges if the underlying structure of the graph does not
alter.
A Graph is said to be Weighted if each edge is assigned a 'weight'. The weight of an edge can
denote distance, time, or anything that models the 'connection' between the pair of vertices it
connects.
For instance, we can observe a blue number next to each edge in the following figure of the
Weighted Graph. This number is utilized to signify the weight of the corresponding edge.
https://www.javatpoint.com/dijkstras-algorithm 6/37
4/25/24, 12:33 AM Dijkstra's Shortest Path Algorithm with Examples - javatpoint
Ever wondered how does Google Maps finds the shortest and fastest route between two places?
Well, the answer is Dijkstra's Algorithm. Dijkstra's Algorithm is a Graph algorithm that finds the
shortest path from a source vertex to all other vertices in the Graph (single source shortest path). It
is a type of Greedy Algorithm that only works on Weighted Graphs having positive weights. The
time complexity of Dijkstra's Algorithm is O(V2) with the help of the adjacency matrix
representation of the graph. This time complexity can be reduced to O((V + E) log V) with the help
of an adjacency list representation of the graph, where V is the number of vertices and E is the
number of edges in the graph.
https://www.javatpoint.com/dijkstras-algorithm 7/37
4/25/24, 12:33 AM Dijkstra's Shortest Path Algorithm with Examples - javatpoint
Dijkstra's Algorithm was designed and published by Dr. Edsger W. Dijkstra, a Dutch Computer
Scientist, Software Engineer, Programmer, Science Essayist, and Systems Scientist.
During an Interview with Philip L. Frana for the Communications of the ACM journal in the
year 2001, Dr. Edsger W. Dijkstra revealed:
"What is the shortest way to travel from Rotterdam to Groningen, in general: from given city to
given city? It is the algorithm for the shortest path, which I designed in about twenty minutes. One
morning I was shopping in Amsterdam with my young fiancée, and tired, we sat down on the café
terrace to drink a cup of coffee and I was just thinking about whether I could do this, and I then
designed the algorithm for the shortest path. As I said, it was a twenty-minute invention. In fact, it
was published in '59, three years later. The publication is still readable, it is, in fact, quite nice. One of
the reasons that it is so nice was that I designed it without pencil and paper. I learned later that one
of the advantages of designing without pencil and paper is that you are almost forced to avoid all
avoidable complexities. Eventually, that algorithm became to my great amazement, one of the
cornerstones of my fame."
Dijkstra thought about the shortest path problem while working as a programmer at the
Mathematical Centre in Amsterdam in 1956 to illustrate the capabilities of a new computer known
as ARMAC. His goal was to select both a problem and a solution (produced by the computer) that
people with no computer background could comprehend. He developed the shortest path
algorithm and later executed it for ARMAC for a vaguely shortened transportation map of 64 cities
in the Netherlands (64 cities, so 6 bits would be sufficient to encode the city number). A year later,
he came across another issue from hardware engineers operating the next computer of the institute:
Minimize the amount of wire required to connect the pins on the machine's back panel. As a
solution, he re-discovered the algorithm called Prim's minimal spanning tree algorithm and
published it in the year 1959.
https://www.javatpoint.com/dijkstras-algorithm 8/37
4/25/24, 12:33 AM Dijkstra's Shortest Path Algorithm with Examples - javatpoint
1. Dijkstra's Algorithm begins at the node we select (the source node), and it examines the
graph to find the shortest path between that node and all the other nodes in the graph.
2. The Algorithm keeps records of the presently acknowledged shortest distance from each
node to the source node, and it updates these values if it finds any shorter path.
3. Once the Algorithm has retrieved the shortest path between the source and another node,
that node is marked as 'visited' and included in the path.
4. The procedure continues until all the nodes in the graph have been included in the path. In
this manner, we have a path connecting the source node to all other nodes, following the
shortest possible path to reach each node.
Each Vertex in this Algorithm will have two properties defined for it:
1. Visited Property
2. Path Property
Visited Property:
1. The 'visited' property signifies whether or not the node has been visited.
https://www.javatpoint.com/dijkstras-algorithm 9/37
4/25/24, 12:33 AM Dijkstra's Shortest Path Algorithm with Examples - javatpoint
3. A node is marked visited only when the shortest path has been found.
Path Property:
1. The 'path' property stores the value of the current minimum path to the node.
2. The current minimum path implies the shortest way we have reached this node till now.
4. This property is significant because it will store the final answer for each node.
Initially, we mark all the vertices, or nodes, unvisited as they have yet to be visited. The path to all
the nodes is also set to infinity apart from the source node. Moreover, the path to the source node
is set to zero (0).
We then select the source node and mark it as visited. After that, we access all the neighboring
nodes of the source node and perform relaxation on every node. Relaxation is the process of
lowering the cost of reaching a node with the help of another node.
In the process of relaxation, the path of each node is revised to the minimum value amongst the
node's current path, the sum of the path to the previous node, and the path from the previous node
to the current node.
Let us suppose that p[n] is the value of the current path for node n, p[m] is the value of the path up
to the previously visited node m, and w is the weight of the edge between the current node and
previously visited one (edge weight between n and m).
Bluetooth Gamepads
BROODIO Compatible Nintendo Switch Controller
AilExpress
https://www.javatpoint.com/dijkstras-algorithm 10/37
4/25/24, 12:33 AM Dijkstra's Shortest Path Algorithm with Examples - javatpoint
We then mark an unvisited node with the least path as visited in every subsequent step and update
its neighbor's paths.
We repeat this procedure until all the nodes in the graph are marked visited.
Whenever we add a node to the visited set, the path to all its neighboring nodes also changes
accordingly.
If any node is left unreachable (disconnected component), its path remains 'infinity'. In case the
source itself is a separate component, then the path to all other nodes remains 'infinity'.
Step 1: First, we will mark the source node with a current distance of 0 and set the rest of the nodes
to INFINITY.
Step 2: We will then set the unvisited node with the smallest current distance as the current node,
suppose X.
Step 3: For each neighbor N of the current node X: We will then add the current distance of X with
the weight of the edge joining X-N. If it is smaller than the current distance of N, set it as the new
current distance of N.
Step 5: We will repeat the process from 'Step 2' if there is any node unvisited left in the graph.
Let us now understand the implementation of the algorithm with the help of an example:
https://www.javatpoint.com/dijkstras-algorithm 11/37
4/25/24, 12:33 AM Dijkstra's Shortest Path Algorithm with Examples - javatpoint
1. We will use the above graph as the input, with node A as the source.
3. We will set the path to 0 at node A and INFINITY for all the other nodes.
4. We will now mark source node A as visited and access its neighboring nodes.
Note: We have only accessed the neighboring nodes, not visited them.
5. We will now update the path to node B by 4 with the help of relaxation because the path to
node A is 0 and the path from node A to B is 4, and the minimum((0 + 4), INFINITY) is 4.
6. We will also update the path to node C by 5 with the help of relaxation because the path to
node A is 0 and the path from node A to C is 5, and the minimum((0 + 5), INFINITY) is 5.
Both the neighbors of node A are now relaxed; therefore, we can move ahead.
7. We will now select the next unvisited node with the least path and visit it. Hence, we will visit
node B and perform relaxation on its unvisited neighbors. After performing relaxation, the
path to node C will remain 5, whereas the path to node E will become 11, and the path to
node D will become 13.
8. We will now visit node E and perform relaxation on its neighboring nodes B, D, and F. Since
only node F is unvisited, it will be relaxed. Thus, the path to node B will remain as it is, i.e., 4,
the path to node D will also remain 13, and the path to node F will become 14 (8 + 6).
9. Now we will visit node D, and only node F will be relaxed. However, the path to node F will
remain unchanged, i.e., 14.
10. Since only node F is remaining, we will visit it but not perform any relaxation as all its
neighboring nodes are already visited.
https://www.javatpoint.com/dijkstras-algorithm 12/37
4/25/24, 12:33 AM Dijkstra's Shortest Path Algorithm with Examples - javatpoint
11. Once all the nodes of the graphs are visited, the program will end.
A=0
B = 4 (A -> B)
C = 5 (A -> C)
D = 4 + 9 = 13 (A -> B -> D)
E = 5 + 3 = 8 (A -> C -> E)
F = 5 + 3 + 6 = 14 (A -> C -> E -> F)
We have to maintain a record of the path distance of every node. Therefore, we can store the
path distance of each node in an array of size n, where n is the total number of nodes.
Moreover, we want to retrieve the shortest path along with the length of that path. To
overcome this problem, we will map each node to the node that last updated its path length.
Once the algorithm is complete, we can backtrack the destination node to the source node to
retrieve the path.
We can use a minimum Priority Queue to retrieve the node with the least path distance in an
efficient way.
Pseudocode:
https://www.javatpoint.com/dijkstras-algorithm 13/37