# Bellman Ford Algorithm, Lecture Slides - Computer Science, Slides for Introduction to Computers. University of Bristol

## Introduction to Computers

Description: Single Source Shortest path Analysis Correctness Negative Weighted cycle Difference constraints
Showing pages  1  -  4  of  20
COMS21102: Bellman-Ford
Raphaël Clifford
clifford@cs.bris.ac.uk
Bristol University, Department of Computer Science
Bristol BS8 1UB, UK
December 3, 2009
Raphaël Clifford
clifford@cs.bris.ac.uk
COMS21102: Bellman-Ford Slide 1
Single-Source Shortest Paths
We have previously looked at this problem in two cases
When there are no negative weight edges, Dijkstra’s shortest path
algorithm uses |V|EXTRACT-MIN and INSERT operations plus |E|
DECREASE-KEY operations. This gives a total worst case of
O(|E|log |V|)or O(|V|log(|V|) + |E|)time depending on how we
implement the priority queue.
Where the graph has no cycles at all (it is a DAG) then we can solve the
problem in O(|V|+|E|)time
In this lecture we consider the case where there are negatively weighted
edges. The algorithm is known as Bellman-Ford
If the graph has a negatively weighted cycle, the algorithm will tell us.
Otherwise, it will give us the shortest path from the source to every node.
The good news is that Bellman-Ford is simpler than the previous
graph algorithms.
Raphaël Clifford
clifford@cs.bris.ac.uk
COMS21102: Bellman-Ford Slide 2
Bellman-Ford
First set the distance to the source to 0 and all other distances to .d[v]
is the current estimate of the distance from the source.
for i1to |V| − 1do
for each edge (u,v)Edo
if d[v]>d[u] + w(u,v)then
d[v]d[u] + w(u,v);
end
end
end
Now we need only check to see if all the distances make sense.
for each edge (u,v)Edo
if d[v]>d[u] + w(u,v)then
report a negatively weighted cycle exists
end
end
Raphaël Clifford
clifford@cs.bris.ac.uk
COMS21102: Bellman-Ford Slide 3
Bellman-Ford - Example
Example graph after initialisation.
B
A
CD
E
-1
4
3
5
1
2
-3
2
0
8
8
8
8
Raphaël Clifford
clifford@cs.bris.ac.uk
COMS21102: Bellman-Ford Slide 4