From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
Hasnain_Khalid_DAA_ppt.pptx
1. Click to edit Master title style
1
Design & Analysis of
Algorithm
2. Click to edit Master title style
2
Bellman Ford’s Algorithm
S u b m i t t e d B y :
H a s n a i n K h a l i d .
M u s t a n s a r G u l .
2
3. Click to edit Master title style
3
Table of contents
• Introduction
• Comparison
• Working Mechanism
• Example Problem
• Algorithm
• Pseudocode
• Algorithm Complexity
• Applications
• Conclusion
3
4. Click to edit Master title style
4
Introduction
4
What is Bellman Ford’s Algorithm?
• Bellman Ford is an algorithm that is used to find the shortest path from a vertex to
all other vertices of a weighted graph.
• It is similar to Dijkstra's algorithm but it can work with graphs in which edges can
have negative weights.
• If it has not converged after V(G) -1 iterations, then there cannot be a shortest path
tree.
• So, there must be a negative weight cycle
5. Click to edit Master title style
5
Dijkstras Vs Bellman Ford
5
• Dijkstra doesn’t work for Graphs with negative weight edges.
• Bellman-Ford works for such graphs.
• Both Algorithms are quite similar but the time complexity of bellman ford algorithm is more
than Dijkstra.
6. Click to edit Master title style
6
Working Mechanism
6
• It works by overestimating the length of the path from the starting vertex to all other
vertices.
• Then it iteratively relaxes those estimates by finding new paths that are shorter than the
previously overestimated paths.
• By doing this repeatedly for all vertices, we can guarantee that the result is optimized.
7. Click to edit Master title style
7
Example Problem
7
• Step No. 1: Starting with the weighted graph.
8. Click to edit Master title style
8
Cont..
8
• Step No. 02: Choose a starting vertex and assign infinity path values to all other
vertices.
9. Click to edit Master title style
9
Cont..
9
• Step No. 03: Visit each edge and relax path distances if they are inaccurate.
10. Click to edit Master title style
10
Cont..
10
• Step No. 04: We need to do this V times because in the worst case, a vertex’s path length
might need to be readjusted V times.
11. Click to edit Master title style
11
Cont..
11
• Step No. 05: Notice how the vertex at the top right corner had its path length
adjusted.
12. Click to edit Master title style
12
Cont..
12
• Step No. 06: After all the vertices have their path lengths, we check if a negative cycle is
present.
13. Click to edit Master title style
13
Algorithm
13
• For Bellman Ford’s Algorithm, We need to maintain the path distance of every
vertex.
• We can store that in an array of size v, where v is the number of vertices.
• We also want to be able to get the shortest path, not only know the length of the
shortest path
• For this, we map each vertex to the vertex that last updated its path length.
• Once the algorithm is over, we can backtrack from the destination vertex to the
source vertex to find the path.
14. Click to edit Master title style
14
Pseudocode
14
function bellmanFord(G, S)
for each vertex V in G
distance[V] <- infinite
previous[V] <- NULL
distance[S] <- 0
for each vertex V in G
for each edge (U,V) in G
tempDistance <- distance[U] +
edge_weight(U, V)
if tempDistance < distance[V]
distance[V] <- tempDistance
previous[V] <- U
for each edge (U,V) in G
If distance[U] + edge_weight(U, V) <
distance[V}
Error: Negative Cycle Exists
return distance[ ], previous[ ]
15. Click to edit Master title style
15
Bellman Ford's Complexity
15
Time Complexity Space Complexity
• Best Case Complexity is O(E)
• Average Case Complexity is O(VE)
• Worst Case Complexity is O(VE)
• And, the space complexity is O(V).
16. Click to edit Master title style
16
Applications
16
• A version of Bellman-Ford is used in the distance-vector routing protocol. This protocol
decides how to route packets of data on a network.
• And for finding the shortest path.
17. Click to edit Master title style
17
Conclusion
17
• After analyzing the both algorithm we have concluded that the main advantage of the Bellman-
Ford algorithm is its capability to handle negative weights. However, it has a considerably larger
complexity than Dijkstra's algorithm.