You are on page 1of 8

Brilliant

TODAYCOURSES
PRACTICE
Sign up Log in

Bellman-Ford Algorithm
Recommended Course


Advanced Algorithms
Complexity theory, randomized algorithms, graphs, and more.

Relevant For...
 Computer Science>
Graph Algorithms

Alex Chumbley, Karleigh Moore, Eli Ross, and 


1 other 
contributed
The Bellman-Ford algorithm is a graph search algorithm that finds the shortest path
between a given source vertex and all other vertices in the graph. This algorithm can be
used on both weighted and unweighted graphs.

Like Dijkstra's shortest path algorithm, the Bellman-Ford algorithm is guaranteed to find


the shortest path in a graph. Though it is slower than Dijkstra's algorithm, Bellman-Ford
is capable of handling graphs that contain negative edge weights, so it is more versatile.
It is worth noting that if there exists a negative cycle in the graph, then there is no
shortest path. Going around the negative cycle an infinite number of times would
continue to decrease the cost of the path (even though the path length is increasing).
Because of this, Bellman-Ford can also detect negative cycles which is a useful feature.

Imagine a scenario where you need to get to a baseball game from your house. Along
the way, on each road, one of two things can happen. First, sometimes the road you're
using is a toll road, and you have to pay a certain amount of money. Second, sometimes
someone you know lives on that street (like a family member or a friend). Those people
can give you money to help you restock your wallet. You need to get across town, and
you want to arrive across town with as much money as possible so you can buy hot
dogs. Given that you know which roads are toll roads and which roads have people who
can give you money, you can use Bellman-Ford to help plan the optimal route.
Graphical representation of routes to a baseball game
Instead of your home, a baseball game, and streets that either take money away from
you or give money to you, Bellman-Ford looks at a weighted graph. The graph is a
collection of edges that connect different vertices in the graph, just like roads. The edges
have a cost to them. Either it is a positive cost (like a toll) or a negative cost (like a friend
who will give you money). So, in the above graphic, a red arrow means you have to pay
money to use that road, and a green arrow means you get paid money to use that road.
In the graph, the source vertex is your home, and the target vertex is the baseball
stadium. On your way there, you want to maximize the number and absolute value of the
negatively weighted edges you take. Conversely, you want to minimize the number and
value of the positively weighted edges you take. Bellman-Ford does just this.

Contents

 Overview

 Algorithm Psuedo-code

 Relaxation Equation

 Detecting Negative Cycles

 Algorithm Proof

 Complexity

 Applications

Overview
The Bellman-Ford algorithm operates on an input graph, GG, with |V|∣V∣ vertices and |
E|∣E∣ edges. A single source vertex, ss, must be provided as well, as the Bellman-Ford
algorithm is a single-source shortest path algorithm. No destination vertex needs to be
supplied, however, because Bellman-Ford calculates the shortest distance to all vertices
in the graph from the source vertex.
The Bellman-Ford algorithm, like Dijkstra's algorithm, uses the principle of relaxation to
find increasingly accurate path length. Bellman-Ford, though, tackles two main issues
with this process:

1. If there are negative weight cycles, the search for a shortest path will go on
forever.

2. Choosing a bad ordering for relaxations leads to exponential relaxations.


The detection of negative cycles is important, but the main contribution of this algorithm
is in its ordering of relaxations. Dijkstra's algorithm is a greedy algorithm that selects the
nearest vertex that has not been processed. Bellman-Ford, on the other hand,
relaxes all of the edges.

Bellman-Ford labels the edges for a graph GG as

e_1, e_2, ... , e_m,e1,e2,...,em,


and that set of edges is relaxed exactly |V| - 1∣V∣−1 times, where |V|∣V∣ is the number
of vertices in the graph.

Algorithm Psuedo-code
The pseudo-code for the Bellman-Ford algorithm is quite short.

This is high level description of Bellman-Ford written with pseudo-code, not an


implementation.

1 for v in V:
2 v.distance = infinity
3 v.p = None
4 source.distance = 0
5 for i from 1 to |V| - 1:
6 for (u, v) in E:
7 relax(u, v)

The first  for  loop sets the distance to each vertex in the graph to infinity. This is later
changed for the source vertex to equal zero. Also in that first  for  loop, the  p  value for
each vertex is set to nothing. This value is a pointer to a predecessor vertex so that we
can create a path later.

The next  for  loop simply goes through each edge  (u, v)  in  E  and relaxes it. This
process is done  |V| - 1  times.

Relaxation Equation
Relaxation is the most important step in Bellman-Ford. It is what increases the accuracy
of the distance to any given vertex. Relaxation works by continuously shortening the
calculated distance between vertices comparing that distance with other known
distances.

Take the baseball example from earlier. Let's say I think the distance to the baseball
stadium is 20 miles. However, I know that the distance to the corner right before the
stadium is 10 miles, and I know that from the corner to the stadium, the distance is 1
mile. Clearly, the distance from me to the stadium is at most 11 miles. So, I can update
my belief to reflect that. That is one cycle of relaxation, and it's done over and over until
the shortest paths are found.

This is relaxation equation.

Relax Equation

1 relax(u, v):
2 if v.distance > u.distance + weight(u, v):
3 v.distance = u.distance + weight(u, v)
4 v.p = u

Remember that the distance to every vertex besides the source starts at infinity, so a
clear starting point for this algorithm is an edge out of the source vertex. Imagine that
there is an edge coming out of the source vertex, SS, to another vertex, AA. This edge
has a weight of 5. So, the  if  statement in the relax function would look like this for the
edge (S, A):(S,A):

\text{if }A.distance > S.distance + weight(S,


A),if A.distance>S.distance+weight(S,A),
which is the same as

\text{if }\infty > 0 + 5 .if ∞>0+5.


Since this is of course true, the rest of the function is executed.  A.distance  is set to 5,
and the predecessor of  A  is set to  S , the source vertex.

Relaxation is safe to do because it obeys the "triangle inequality." Another way of saying
that is "the shortest distance to go from AA to BB to CC should be less than or equal to
the shortest distance to go from AA to BB plus the shortest distance to go
from BB to CC":

distance(A, C) \leq distance(A, B) + distance(B,


C).distance(A,C)≤distance(A,B)+distance(B,C).
Detecting Negative Cycles
A very short and simple addition to the Bellman-Ford algorithm can allow it to detect
negative cycles, something that is very important because it disallows shortest-path
finding altogether. After the Bellman-Ford algorithm shown above has been run, one
more short loop is required to check for negative weight cycles.

This pseudo-code is written as a high-level description of the algorithm, not an


implementation.

1 for v in V:
2 v.distance = infinity
3 v.p = None
4 source.distance = 0
5 for i from 1 to |V| - 1:
6 for (u, v) in E:
7 relax(u, v)
8 for (u, v) in E:
9 if v.distance > u.distance + weight(u, v):
10 print "A negative weight cycle exists"

Algorithm Proof
There are a few short steps to proving Bellman-Ford. The first step shows that each
iteration of Bellman-Ford reduces the distance of each vertex in the appropriate way. The
second step shows that, once the algorithm has terminated, if there are no negative
weight cycles, the resulting distances are perfectly correct. The final step shows that if
that is not the case, then there is indeed a negative weight cycle, which proves the
Bellman-Ford negative cycle detection.

Step 1:

Claim: After interation ii, for all vv in VV, v.dv.d is at most the weight of every path


from ss to vv using at most ii edges.

Before iteration ii, the value of v.dv.d is constrained by the following equation

v.d \leq min{w(p): |p| \leq i - 1},v.d≤minw(p):∣p∣≤i−1,


where w(p)w(p) is the weight of a given path and |p|∣p∣ is the number of edges in that
path. Subsequent relaxation will only decrease v.dv.d, so this will always remain true.
The i^\text{th}ith iteration will consider all incoming edges to vv for paths with \leq
i≤i edges.
On
the  i^\text{th}ith  iteration
Step 2:

Claim: If the input graph does not have any negative weight cycles, then Bellman-Ford
will accurately give the distance to every vertex vv in the graph from the source.

An important thing to note is that without negative weight cycles, the shortest paths will
always be simple. There will not be any repetition of edges. So, each shortest path has |
V^{*}|∣V∗∣ vertices and |V^{*} - 1|∣V∗−1∣ edges (depending on which vertex we are
calculating the distance for). More generally, |V^{*}| \leq |V|∣V∗∣≤∣V∣, so each path
has \leq |V|≤∣V∣ vertices and \leq |V^{*} - 1|≤∣V∗−1∣ edges.

Consider the shortest path from ss to uu, where vv is the predecessor of uu. On the (i
- 1)^\text{th}(i−1)th iteration, we've found the shortest path from ss to vv using at
most i - 1i−1 edges. v.distancev.distance is at most the weight of this path.
So, v.distance + weight(u, v)v.distance+weight(u,v) is at most the distance
from ss to uu. On the i^\text{th}ith iteration, all we're doing is comparing v.distance +
weight(u, v)v.distance+weight(u,v) to u.distanceu.distance. All that can
possibly happen is that u.distanceu.distance gets smaller. So, after
the i^\text{th}ith iteration, u.distanceu.distance is at most the distance
from ss to uu. And because it can't actually be smaller than the shortest path
from ss to uu, it is exactly equal.

Step 3:

Claim: Bellman-Ford can report negative weight cycles.

Using our Step 2, if we go back through all of the edges, we should see that for
all vv in VV, v.distance = distance(s, v)v.distance=distance(s,v). Bellman-
Ford will only report a negative cycle if v.distance \gt u.distance + weight(u,
v)v.distance>u.distance+weight(u,v), so there cannot be any false reporting
of a negative weight cycle.

If there is a negative weight cycle, then one of the edges of that cycle can always be
relaxed (because it can keep on being reduced as we go around the cycle). Imagining
that the edge in question is the edge (u, v),(u,v), that means that u.distance +
weight(u, v)u.distance+weight(u,v) will actually be less
than v.distancev.distance, which will trigger a negative cycle report.

Complexity
As described above, Bellman-Ford makes |E|∣E∣ relaxations for every iteration, and there
are |V| - 1∣V∣−1 iterations. Therefore, the worst-case scenario is that Bellman-Ford runs
in O\big(|V| \cdot |E|\big)O(∣V∣⋅∣E∣) time.

However, in some scenarios, the number of iterations can be much lower. For certain
graphs, only one iteration is needed, and hence in the best case scenario, only O\big(|
E|\big)O(∣E∣) time is needed. An example of a graph that would only need one round of
relaxation is a graph where each vertex only connects to the next one in a linear fashion,
like the graphic below:

This graph only needs one round of relaxation.


See also:  big O notation.

Worst Best
Bellman- O\big(|V| \cdot | O\big(|
Ford\hspace{12 E|\big)O(∣V∣⋅∣E∣)\hspace{ E|\big)O(∣E∣)\hspace{12mm
mm} 12mm} }
Applications
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. The distance equation (to decide
weights in the network) is the number of routers a certain path must go through to reach
its destination.

For the Internet specifically, there are many protocols that use Bellman-Ford. One
example is the routing Information protocol. This is one of the oldest Internet protocols,
and it prevents loops by limiting the number of hops a packet can make on its way to the
destination. A second example is the interior gateway routing protocol. This proprietary
protocol is used to help machines exchange routing data within a system.
Cite as: Bellman-Ford Algorithm. Brilliant.org. Retrieved 21:31, March 15,
2020, from https://brilliant.org/wiki/bellman-ford-algorithm/

Master concepts like these


 Get started
Learn more in our Advanced Algorithms course, built by experts for you.

You might also like