Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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
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.
Algorithm Psuedo-code
The pseudo-code for the Bellman-Ford algorithm is quite short.
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.
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):
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":
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: 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:
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:
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/