## Dijkstra's AlgorithmThe 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. We will cover the following: - A Brief Overview of the Fundamental Concepts of Graph
- Understand the Use of Dijkstra's Algorithm
- Understand the Working of the Algorithm with a Step-by-Step Example
So, let's get started. ## A Brief Introduction to Graphs
## Components of a Graph**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.**Edges:**Edges are drawn or used to connect two vertices of the graph. Sometimes, edges are also known as Arcs.
The following figure shows a graphical representation of a Graph:
In the above figure, the Vertices/Nodes are denoted with Colored Circles, and the Edges are denoted with the lines connecting the nodes. ## Applications of the GraphsGraphs 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:
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 GraphsThe Graphs can be categorized into two types: - Undirected Graph
- Directed Graph
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. ## What are Weighted Graphs?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.
## An Introduction to Dijkstra's AlgorithmNow that we know some basic Graphs concepts let's dive into understanding the concept of Dijkstra's Algorithm. Ever wondered how does Google Maps finds the shortest and fastest route between two places? Well, the answer is 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.## History of Dijkstra's AlgorithmDijkstra's Algorithm was designed and published by
"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. ## Fundamentals of Dijkstra's AlgorithmThe following are the basic concepts of Dijkstra's Algorithm: - 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.
- 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.
- 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.
- 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.
## Understanding the Working of Dijkstra's AlgorithmA
- Visited Property
- Path Property
Let us understand these properties in brief. ## Visited Property:- The 'visited' property signifies whether or not the node has been visited.
- We are using this property so that we do not revisit any node.
- A node is marked visited only when the shortest path has been found.
## Path Property:- The 'path' property stores the value of the current minimum path to the node.
- The current minimum path implies the shortest way we have reached this node till now.
- This property is revised when any neighbor of the node is visited.
- 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). In the mathematical sense, relaxation can be exemplified as:
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'. ## Understanding Dijkstra's Algorithm with an Example
- We will use the above graph as the input, with node
**A**as the source. - First, we will mark all the nodes as unvisited.
- We will set the path to
**0**at node**A**and**INFINITY**for all the other nodes. - 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. - 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**. - 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. - 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**. - 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)**. - 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**. - Since only node
**F**is remaining, we will visit it but not perform any relaxation as all its neighboring nodes are already visited. - Once all the nodes of the graphs are visited, the program will end.
## Pseudocode for Dijkstra's AlgorithmWe will now understand a pseudocode for Dijkstra's Algorithm. - 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.
Let us now implement a pseudocode of the above illustration:
In the above pseudocode, we have defined a function that accepts multiple parameters - the Graph consisting of the nodes and the source node. Inside this function, we have iterated through each node in the Graph, set their initial distance to ## Implementation of Dijkstra's Algorithm in Different Programming LanguagesNow that we have successfully understood the pseudocode of Dijkstra's Algorithm, it is time to see its implementation in different programming languages like C, C++, Java, and Python. ## Code for Dijkstra's Algorithm in CThe following is the implementation of Dijkstra's Algorithm in the C Programming Language:
Distance from the Source Node to 1: 4 Distance from the Source Node to 2: 12 Distance from the Source Node to 3: 19 Distance from the Source Node to 4: 12 Distance from the Source Node to 5: 8 Distance from the Source Node to 6: 10
In the above snippet of code, we have included the In the As a result, the required shortest possible paths for every node from the source node are printed for the users. ## Code for Dijkstra's Algorithm in C++The following is the implementation of Dijkstra's Algorithm in the C++ Programming Language:
Distance from start: 4 F G D A
In the above code snippet, we included the As a result, the required shortest path for the vertex ## Code for Dijkstra's Algorithm in JavaThe following is the implementation of Dijkstra's Algorithm in the Java Programming Language:
Distance from Vertex 0 to Vertex 0 is 0 Distance from Vertex 0 to Vertex 1 is 1 Distance from Vertex 0 to Vertex 2 is 1 Distance from Vertex 0 to Vertex 3 is 2 Distance from Vertex 0 to Vertex 4 is 4 Distance from Vertex 0 to Vertex 5 is 4 Distance from Vertex 0 to Vertex 6 is 3
In the above snippet of code, we have defined a public class as As a result, the required shortest possible paths for every node from the source node are printed for the users. ## Code for Dijkstra's Algorithm in PythonThe following is the implementation of Dijkstra's Algorithm in the Python Programming Language:
Distance of A from source node: 0 Distance of B from source node: 3 Distance of C from source node: 1 Distance of D from source node: 2 Distance of E from source node: 2 Distance of F from source node: 4 Distance of G from source node: 3
In the above snippet of code, we have imported the As a result, the required shortest possible paths for every node from the source node are printed for the users. ## Time and Space Complexity of Dijkstra's Algorithm- The Time Complexity of Dijkstra's Algorithm is
**O(E log V)**, where E is the number of edges and V is the number of vertices. - The Space Complexity of Dijkstra's Algorithm is O(V), where V is the number of vertices.
## Advantages and Disadvantages of Dijkstra's Algorithm
- One primary advantage of using Dijkstra's Algorithm is that it has an almost linear time and space complexity.
- We can use this algorithm to calculate the shortest path from a single vertex to all other vertices and a single source vertex to a single destination vertex by stopping the algorithm once we get the shortest distance for the destination vertex.
- This algorithm only works for directed weighted graphs, and all the edges of this graph should be non-negative.
- Dijkstra's Algorithm performs a concealed exploration that utilizes a lot of time during the process.
- This algorithm is impotent to handle negative edges.
- Since this algorithm heads to the acyclic graph, it cannot calculate the exact shortest path.
- It also requires maintenance to keep a record of vertices that have been visited.
## Some Applications of Dijkstra's Algorithm
**Digital Mapping Services in Google Maps:**There are various times when we have tried to find the distance in Google Maps either from our location to the nearest preferred location or from one city to another, which comprises multiple routes/paths connecting them; however, the application must display the minimum distance. This is only possible because Dijkstra's algorithm helps the application find the shortest between two given locations along the path. Let us consider the USA as a graph wherein the cities/places are represented as vertices, and the routes between two cities/places are represented as edges. Then with the help of Dijkstra's Algorithm, we can calculate the shortest routes between any two cities/places.**Social Networking Applications:**In many applications like Facebook, Twitter, Instagram, and more, many of us might have observed that these apps suggest the list of friends that a specific user may know. How do many social media companies implement this type of feature in an efficient and effective way, specifically when the system has over a billion users? The answer to this question is Dijkstra's Algorithm. The standard Dijkstra's Algorithm is generally used to estimate the shortest distance between the users measured through the connections or mutuality among them. When social networking is very small, it uses the standard Dijkstra's Algorithm in addition to some other features in order to determine the shortest paths. However, when the graph is much bigger, the standard algorithm takes several seconds to count, and thus, some advanced algorithms are used as the alternative.**Telephone Network:**As some of us might know, in a telephone network, each transmission line has a bandwidth, 'b'. The bandwidth is the highest frequency that the transmission line can support. In general, if the frequency of the signal is higher in a specific line, the signal is reduced by that line. Bandwidth represents the amount of information that can be transmitted by the line. Let us consider a city a graph wherein the switching stations are represented using the vertices, the transmission lines are represented as the edges, and the bandwidth, 'b', is represented using the weight of the edges. Thus, as we can observe, the telephone network can also fall into the category of the shortest distance problem and can be solved using Dijkstra's Algorithm.**Flight Program:**Suppose that a person requires software to prepare an agenda of flights for customers. The agent has access to a database with all flights and airports. In addition to the flight number, origin airport, and destination, the flights also have departure and arrival times. So, in order to determine the earliest arrival time for the selected destination from the original airport and given start time, the agents make use of Dijkstra's Algorithm.**IP routing to find Open Shortest Path First:**Open Shortest Path First (abbreviated as OSPF) is a link-state routing protocol used to find the best path between the source and destination router with the help of its own Shortest Path First. Dijkstra's Algorithm is extensively utilized in the routing protocols required by the routers in order to update their forwarding table. The algorithm gives the shortest cost path from the source router to the other routers present in the network.**Robotic Path:**These days, drones and robots have come into existence, some operated manually and some automatically. The drones and robots which are operated automatically and used to deliver the packages to a given location or used for any certain task are configured with Dijkstra's Algorithm module so that whenever the source and destination are known, the drone and robot will move in the ordered direction by following the shortest path keeping the time taken to a minimum in order to deliver the packages.**Designate the File Server:**Dijkstra's Algorithm is also used to designate a file server in a Local Area Network (LAN). Suppose that an infinite period of time is needed for the transmission of the files from one computer to another. So, to minimize the number of 'hops' from the file server to every other computer on the network, we will use Dijkstra's Algorithm. This algorithm will return the shortest path between the networks resulting in the minimum number of hops.
## The Conclusion- In the above tutorial, firstly, we have understood the basic concepts of Graph along with its types and applications.
- We then learned about Dijkstra's Algorithm and its history.
- We have also understood the fundamental working of Dijkstra's Algorithm with the help of an example.
- After that, we studied how to write code for Dijkstra's Algorithm with the help of Pseudocode.
- We observed its implementation in programming languages like C, C++, Java, and Python with proper outputs and explanations.
- We have also understood the Time and Space Complexity of Dijkstra's Algorithm.
- Finally, we have discussed the advantages and disadvantages of Dijkstra's algorithm and some of its real-life applications.
Next TopicHuffman Coding Algorithm |