Bellman Ford Algorithm

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

PRESENTED BY:

BELLMAN – FORD PARAM BATAVIA: 1714004


SHAKSHI GANDHI: 1714018
INTRODUCTION

• The Bellman-Ford algorithm solves the single-source shortest-paths problem in the general
case in which edge weights may be negative. Bellman-Ford algorithm returns a boolean
value indicating whether or not there is a negative-weight cycle that is reachable from the
source. If there is such a cycle ,the algorithm indicates that no solution exists. If there is no
such cycle, the algorithm produces the shortest paths and their weights.

• The algorithm was first proposed by Alfonso Shimbel (1955), but is instead named
after Richard Bellman and Lester Ford, Jr., who published it in 1958 and 1956.
Which type of Algorithm?

• The algorithm is Dynamic.


• In the first iteration of the loops it builds one possible path between two
vertex and then at each iteration it improves the path by at least one
edge. As the longest path can use maximum n-1 edges, the iteration of
the loop continues that much to find the shortest path.
• For negative cycle, algorithm at nth iteration checks one more time to
see if an edge exists to decrease the weight of the shortest path having
n-1 edges. If yes, then that edge must be negative as the shortest path
with all positive edges should be consisted of n-1 not n edges.
ALGORITHM
d[s] <- 0
for each v ∈ V – {s} \\Initialization
do d[v] <- ∞
for I <- 1 to [V] – 1
do for each edge(u,v) ∈ E \\Relaxation
do if d[v] > d[u] + w(u,v)
then d[v] <- d[u] + w(u,v)
for each edge(u,v) ∈ E \\ Check for Negative cycle
do if d[v] > d[u] + w(u,v)
then report that negative weight cycle exists
EXAMPLE
EXAMPLE
K VERTICES
1 TO N-1 1 2 3 4 5 6 7

Initializat 0 ∞ ∞ ∞ ∞ ∞ ∞
ion
1 0 6 5 5 ∞ ∞ ∞
2 0 3 3 5 5 4 ∞
3 0 1 3 5 2 4 7
4 0 1 3 5 0 4 5
5 0 1 3 5 0 4 3
6 0 1 3 5 0 4 3
TIME COMPLEXITY
• All the loops included in the algorithm:
• The initialization loop runs |V| times.
• The outer for loop runs |V| – 1 times.
• The inner loop runs |E| times for each iteration of the outer loop.
• The last loop runs for |E| times.

Step 1 takes O(|V|) time


Step 2 takes O(|V| . |E|) times
Step 3 takes O(|E|) times.
Hence the running time would be dominated by the term O(|V| . |E|).
On a complete graph of n vertices, there are around n2 edges, for a total running time of n3.
APPLICATIONS
• A distributed variant of the Bellman–Ford algorithm is used in distance-vector routing protocols, for
example the Routing Information Protocol (RIP). The algorithm is distributed because it involves a
number of nodes (routers) within an Autonomous system, a collection of IP networks typically owned by
an ISP. It consists of the following steps:
• Each node calculates the distances between itself and all other nodes within the AS and stores this
information as a table.
• Each node sends its table to all neighboring nodes.
• When a node receives distance tables from its neighbors, it calculates the shortest routes to all
other nodes and updates its own table to reflect any changes.

You might also like