Greedy Techniques-Dijkstra’s and Bellman Ford Algorithm

Dijkstra's algorithm and Bellman-Ford algorithm are two popular algorithms used to find the shortest path in a weighted graph. However, they differ in their approach and the scenarios they are suited for.

Dijkstra's Algorithm:

Dijkstra's algorithm is a greedy algorithm that finds the shortest path from a source vertex to all other vertices in a graph with non-negative edge weights. It maintains a priority queue (min-heap) of vertices and their tentative distances from the source. The algorithm iteratively selects the vertex with the smallest tentative distance and relaxes its neighboring vertices, updating their distances if a shorter path is found.

Here is a step-by-step explanation of Dijkstra's algorithm:

1. Initialize the distance from the source vertex to all other vertices as infinity, except the source itself, which is set to 0.

2. Initialize a priority queue (min-heap) and insert the source vertex with a distance of 0.

3. Repeat until the priority queue is empty:

a. Extract the vertex 'u' with the minimum distance from the priority queue.

b. For each neighbor 'v' of 'u':
- Calculate the tentative distance from the source to 'v' through 'u'.
- If the tentative distance is smaller than the current distance, update the distance of 'v'.
- If 'v' is not visited, insert it into the priority queue.

4. After the algorithm terminates, the distances from the source to all other vertices represent the shortest paths.


Note: Dijkstra's algorithm assumes non-negative edge weights and works only for connected graphs. It does not handle negative edge weights.


Bellman-Ford Algorithm:

The Bellman-Ford algorithm is a dynamic programming-based algorithm that finds the shortest path from a source vertex to all other vertices in a graph, even if it contains negative edge weights or negative cycles. The algorithm relaxes all edges repeatedly, gradually improving the distance estimates until it reaches the optimal solution.

Here is a step-by-step explanation of the Bellman-Ford algorithm:

1. Initialize the distance from the source vertex to all other vertices as infinity, except the source itself, which is set to 0.

2. Repeat the following steps for (V - 1) times, where V is the number of vertices in the graph:

a. For each edge (u, v) in the graph:
- Relax the edge: If the distance to 'v' can be improved by going through 'u', update the distance of 'v'.

3. Check for negative cycles:

a. Repeat the following steps for each edge (u, v) in the graph:
- If the distance to 'v' can still be improved by going through 'u', then there is a negative cycle in the graph.

4. After the algorithm terminates, the distances from the source to all other vertices represent the shortest paths.


The Bellman-Ford algorithm can handle graphs with negative edge weights but is less efficient than Dijkstra's algorithm. It has a time complexity of O(V * E), where V is the number of vertices and E is the number of edges.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext