# Finding the Shortest Route: Exploring Dijkstra's Algorithm

# **What is Dijkstra's Algorithm?**

Dijkstra's Algorithm is a renowned algorithm used to find the shortest paths between nodes in a graph. It is widely used in routing and as a foundation for various network-related applications. Whether you're navigating through a map or optimizing network traffic, Dijkstra's Algorithm provides an efficient way to determine the shortest path.

---

# **Steps of Dijkstra's Algorithm:**

1. **Initialization**:
    
    * Set the distance to the source node as 0 and to all other nodes as infinity.
        
    * Add all nodes to a priority queue with their initial distances to keep track of the nodes with the shortest known distance..
        
2. **Relaxation**:
    
    * Extract the node with the smallest distance from the priority queue.
        
    * For each neighboring node, calculate the potential new distance and update it if it's shorter than the current known distance.
        
    * Repeat until all nodes have been processed.
        
3. **Path Reconstruction**:
    
    * Once the shortest paths are determined, reconstruct the shortest path from the source node to the target node using the recorded distances and predecessors.
        

---

## **Let's understand the journey to the shortest path!**

The algorithm iteratively selects the closest unvisited node and updates the distances of its neighbors. This process continues until the shortest path to each node is found.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725541631380/5e65e87b-649c-4d20-a4d4-765122b8bb7f.png align="center")

<mark>The time complexity is O((V + E) log V), where V is the number of vertices and E is the number of edges.</mark>

### STEP-1:

As we consider '**Vertex-A'** as our source, its distance from itself is zero. Now, as we travel to the closest unvisited nodes, we see that the adjacent vertices to **Vertex-A** are **D** and **B**.

By relaxing both **B** and **D**, we see that the potential new distance is less than infinity. So, we update the value from infinity to the potential new distance. Also, we should remember to update the parent identifier. In both cases, the parent is **A**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725541719371/fd22f1e7-24b5-4408-b912-aa2db65fafe2.png align="center")

### STEP-2:

Now, we choose the next vertex to visit based on its minimum weighted edge value. So, we select "Vertex-B" as our next current visiting point. We then repeat the same process as we did in Step-1.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725541757634/b41a66a4-f312-413e-a58e-aac107d3d940.png align="center")

### STEP-3

While currently at "**Vertex-D**," we see there are two outgoing edges. For the edge from D to B, the new potential distance was supposed to be 11, which is less than the current known distance. Because of this, we don't update its value and keep it as it is. Then, we move forward.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725541800520/09e2006e-1b2c-4bc8-9429-4336acd7f56c.png align="center")

### STEP-4:

We stick to our understanding of the algo and carry out our same approach for "**Vertex-E**".

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725541827156/fc43d158-4a27-4f69-abc0-1b182ee2bb42.png align="center")

### STEP-5:

Now, we reach our last vertex and see no adjacent vertices. So, we mark that node as visited.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725541854161/b98368a4-babc-46f5-913e-b89980833f98.png align="center")

### STEP-6:

Yahoo! Our stimulation is done as all the vertices are visited !

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725541877077/08fc5dd2-b8c6-4933-9371-7b3729516f8e.png align="center")

### STEP-7: Verification of Dijkstra's Algorithm:

Let's verify our work!

**<mark>Edge Weights and Path Calculation:</mark>**

As we go over from A to A path, we already know its distance from itself will be zero.

1. **From A to B:** Direct edge with weight 1.
    
2. **From A to C:** The shortest path is A → B → C, with the total distance being 1 (A to B) + 4 (B to C) = 5.
    
3. **From A to D:** Direct edge with weight 3.
    
4. **From A to E:** The shortest path is A → D → E, with the total distance being 3 (A to D) + 1 (D to E) = 4.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725541899556/6480fcd8-5c42-4d4a-91b6-00442c98f3fc.png align="center")

---

# **Advantages of Dijkstra's Algorithm**

* **Efficiency**: With a time complexity of O((V + E) log V), Dijkstra’s Algorithm is efficient for large graphs.
    
* **Correctness**: It guarantees the shortest path in graphs with non-negative weights.
    
* **Versatility**: Can be used in various applications, from network routing to geographical mapping.
    

---

# **Disadvantages of Dijkstra's Algorithm**

* Dijkstra's Algorithm does not work correctly with graphs that have negative weight edges.
    
* Although efficient, the performance can degrade with extremely large graphs or dense graphs.
    

---

# **Conclusion**

Dijkstra's Algorithm is a powerful tool for finding the shortest paths in graphs with non-negative weights. Its efficiency and correctness make it suitable for a variety of applications, from routing in navigation systems to optimizing network paths. Thus, understanding Dijkstra’s Algorithm and its implementation can enhance your problem-solving skills and open doors to more advanced topics in graph theory and algorithm design.
