Contents:
Introduction to Bellman Ford
Which data structure is used?
Bellman Ford Algorithm
Bellman Ford Working
Example
Time-complexity
Applications
Limitations
2. Contents
• Introduction to Bellman Ford
• Which data structure is used?
• Bellman Ford Algorithm
• Bellman Ford Working
• Example
• Time-complexity
• Applications
• Limitations
3. Introduction to Bellman Ford Algorithm
• In a directed graph, we have to select one vertex as source vertex
and find out the shortest path to all other vertex.
• Before doing this, we already have Dijkstra algorithm, that will
solve the shortest path problem, but Dijkstra algorithm will not
work if there are negative edges. It may or may not give correct
result
• So we use Bellman ford Algorithm.
4. Introduction to Bellman Ford Algorithm
• Solve the single-source shortest-paths problem
• The algorithm returns a boolean value indicating whether or
not there is a negative-weight cycle that is reachable from the
source.
• If there are no negative weight cycles, then the algorithm
produces the shortest paths and their weights.
5. What data structure is used?
• Graph Data Structure is Used Because It Solve the Problem of
Weighted Graph
6. Bellman-Ford algorithm
d[s] ← 0
for each v ∈ V – {s}
do d[v] ← ∞
for i ← 1 to |V| – 1
initialization
do for each edge (u, v) ∈ E
do if d[v] > d[u] + w(u, v)
then d[v] ← d[u] + w(u, v)
for each edge (u, v) ∈ E
do if d[v] > d[u] + w(u, v)
relaxation
step
then report that a negative-weight cycle exists.
7. Bellman Ford Working
• The algorithm says to prepare list of all edges,
• This algorithm says to relax all edges
• We repeatedly relax them n-1 times, n is the number of vertices
|v| = n
So we relax them |v|-1
8. 1 3
4
5
6
7
6
-1
5
5 -2
1
-1
3
3
2
-2
• There are total 10 edges, edge-list is
(1,2), (1,3), (1,4), (2,5),(3,2), (3,5), (4,3), (4,6),(5,7),(6,7)
• Now to relax all edges for 6 times. As, |v|-1 =6
• Initially we mark the distance of all edges
• Mark 1st as 0 and others as infinity
0
Example
15. • We see that after third time it
had stopped changing
• It will run 6 times, when we are
doing it in algorithm
• So finally, these are the shortest
path
1 2 3 4 5 6 7
0 0
1 0 3 3 5 5 4 7
2 0 1 3 5 2 4 5
3 0 1 3 5 0 4 3
4 0 1 3 5 0 4 3
5 0 1 3 5 0 4 3
6 0 1 3 5 0 4 3
16. Time-Complexity
• It relaxes all edges, edges are |E|
• How many time it is relaxing|v-1|
• So this is order of v into e
O(|V| |E|)
If we say V and E are n,n
O(n2 )
This depends on number of edges.
17. REAL LIFE APPLICATIONS
• A Google Map may uses Bellman-Ford for finding Shortest Path
b/w to locations :-
• consider map as a GRAPH
• locations in the map are our VERTICES
• roads and road lengths between locations are EDGES and
WEIGHTS
18. REAL LIFE APPLICATIONS
• In a telephone network the lines have bandwidth(BW) We want to
route the phone call via the highest BW and consider the
transmition lines with highest BW as Shortest Path
• consider telephone network as a GRAPH
• switching station in the network are our VERTICES
• transmission lines in network are EDGES
• Bandwidths in network are WEIGHTS
19. Limitations of Bellman Ford’s Algorithm
• If there is a negative weight cycle then the graph cannot be
solved.
• Negative cycles. A negative cycle is a directed cycle whose total
weight (sum of the weights of its edges) is negative. The concept
of a shortest path is meaningless if there is a negative cycle.
-10
5
3
1
2 3
3+5-10 = -2
20. Limitations of Bellman Ford’s Algorithm
• Does not scale well
• Changes in network topology are not reflected quickly since
updates are spread node-by-node.
Editor's Notes
Input: Graph and a source vertex srcOutput: Shortest distance to all vertices from src. If there is a negative weight cycle, then shortest distances are not calculated, negative weight cycle is reported.
1) This step initializes distances from source to all vertices as infinite and distance to source itself as 0. Create an array dist[] of size |V| with all values as infinite except dist[src] where src is source vertex.
2) This step calculates shortest distances. Do following |V|-1 times where |V| is the number of vertices in given graph.…..a) Do following for each edge u-v………………If dist[v] > dist[u] + weight of edge uv, then update dist[v]………………….dist[v] = dist[u] + weight of edge uv
) This step reports if there is a negative weight cycle in graph. Do following for each edge u-v……If dist[v] > dist[u] + weight of edge uv, then “Graph contains negative weight cycle”The idea of step 3 is, step 2 guarantees shortest distances if graph doesn’t contain negative weight cycle. If we iterate through all edges one more time and get a shorter path for any vertex, then there is a negative weight cycle
Some graphs may get relaxation in one time and some may go till the last relaxation to get the shortest path,
If it’s a complete graph(meaning there is edge between every pair of vertex
So, total edges will be |E|= 𝑛(𝑛−1) 2
Then , time will be Total edges into number of vertices= 𝑛(𝑛−1) 2 * n-1 = O(n3 ) time
Min it its n2 and max n3