**Dijkstra’s algorithm**

# What is Dijkstra’s Algorithm?

Dijkstra’s algorithm is likewise known as the shortest path algorithm. it’s miles an algorithm used to discover the shortest direction among nodes of the graph. The set of rules creates the tree of the shortest paths from the beginning supply vertex from all different factors in the graph. It differs from the minimal spanning tree because the shortest distance among vertices might not be covered in all of the vertices of the graph. The algorithm works by means of building a hard and fast of nodes that have a minimal distance from the supply. right here, Dijkstra’s set of rules uses a greedy technique to clear up the problem and locate the nice solution.

# When Does Dijkstra’s Algorithm Fail

The graph with positive weights is the only one that Dijkstra’s algorithm works with. In order to discover the shortest path between the nodes, the weights of the edges must be added while executing an algorithm. The method will fail if there is a negative weight in the network. Remember that after you’ve marked a node as “visited,” the current path to that node is the shortest. As a result, if the overall weight is reduced and you have negative weights, this step may change.

Furthermore, the question of whether Dijkstra’s algorithm is BFS or DFS emerges while comprehending it. That isn’t the case. Priority first is Dijkstra’s algorithm. However, when choosing between BFS and DFS algorithms, BFS will take precedence over DFS. Dijkstra’s algorithm has some key BFS structure, but it is far more than the BFS algorithm.

**Algorithm for Dijkstra’s in C++**

To know how Dijkstra’s algorithm works behind the scene, look at the below steps to understand it in detail:

- First of all, we will mark all vertex as unvisited vertex.
- Then, we will mark the source vertex as 0 and all other vertices as infinity.
- Consider source vertex as current vertex.
- Calculate the path length of all the neighboring vertex from the current vertex by adding the weight of the edge in the current vertex.
- Now, if the new path length is smaller than the previous path length then replace it otherwise ignore it.
- Mark the current vertex as visited after visiting the neighbor vertex of the current vertex.
- Select the vertex with the smallest path length as the new current vertex and go back to step 4.
- Repeat this process until all the vertex are marked as visited.

# Pseudocode of Dijkstra’s Algorithm in C++

# Time Complexity

The time complexity of Dijkstra’s algorithm is O(V²), where V is the number of vertices in the graph. However, if the input graph is represented using an adjacency list (method of representation of graph), then the time complexity can be reduced to O(E log V) using a binary heap.

The space complexity of Dijkstra’s algorithm is O(V), where V is the total number of vertices of the graph. This is because we have to store all these vertices in the list as an output.

# Applications

- Dijkstra’s algorithm is used as a routing protocol required by the routers to update their forwarding table.
- It is used to find the shortest distance between two locations along the path on google maps.
- It is used in telephone networking to find the shortest path for finding the nearest switching station for transmission.
- Dijkstra’s algorithm is used to find the shortest path between users measured through handshakes or connections among them.
- Dijkstra’s algorithm is used to minimize the number of hops in computer networks.

# Conclusion

Graphs are used as a connection between objects, people, or entities, and Dijkstra’s algorithm will help you find the shortest distance between two points in a graph. As Dijkstra’s algorithm has its own importance in the real world, it is most recommended to learn and understand it in detail.