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  -  2  of  20
The preview of this document ends here! Please or to read the full document or to download it.
Document information
Embed this document:
COMS21102: Bellman-Ford

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 i ← 1 to |V | − 1 do for each edge (u, v) ∈ E do

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) ∈ E do 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

C D

E

-1

4

3

5

1 2

-3

2

0

8

8

88

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 4

Bellman-Ford - Example

Intermediate steps on blackboard.

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 5

Bellman-Ford - Example

Example graph at terminination.

B

A

C D

E

-1

4

3

5

1 2

-3

2

0

-1

1

-22

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 6

Bellman-Ford - Analysis

We analyse the time complexity and correctness of the Bellman-Ford algorithm

◮ The outer loop of Bellman-Ford is iterated |V | − 1 times. ◮ The inner loop is iterated |E | times for each iteration of the outer loop ◮ This makes O(|V ||E |) time in total

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 7

Bellman-Ford - Correctness

How do we know it actually works? We would like to prove

Theorem If G = (V ,E) contains no negatively weighted cycles, then after the Bellman-Ford algorithm terminates, d [v ] = δ(s, v) for all v ∈ V.

Proof. Let v ∈ V be any vertex and consider a shortest path p from s to v with the minimum number of edges. Call the vertices on this path v0, . . . , vk where the target vertex is vk = v . Since p is a shortest path we have

δ(s, vi) = δ(s, vi−1) + w(vi−1, vi)

[...]

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 8

Bellman-Ford - Correctness How do we know it actually works? We would like to prove

Theorem If G = (V ,E) contains no negatively weighted cycles, then after the Bellman-Ford algorithm terminates, d [v ] = δ(s, v) for all v ∈ V.

Proof. [...] Initially d [v0] = 0 = δ(s, v0) and d [v0] is unchanged by subsequent relaxations (as d [v ] ≥ δ(s, v) at all times).

◮ After 1 iteration of the outer loop, we are guaranteed that d [v1] = δ(s, v1)

◮ After 2 iterations of the outer loop, we are guaranteed that d [v2] = δ(s, v2)

◮ After k iterations of the outer loop, we are guaranteed that d [vk ] = δ(s, vk )

Since G contains no negatively weighted cycles, p has no loops and therefore the longest path has ≤ |V | − 1 edges.

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 8

Detection of negatively weighted cycles Theorem If G has a negatively weighted cycle reachable from the source s, then the algorithm will report it.

In other words, if d [v ] fails to converge after |V | − 1 iterations then there must be a negatively weighted cycle which is reachable from s.

Proof. Proof by contradiction.

◮ Assume Bellman-Ford doesn’t report a negatively weighted cycle. As there is a negatively weighted cycle we know that

∑k i=1 w(vi−1, vi) < 0

for some cycle v0, v1, . . . , vk with v0 = vk . ◮ As Bellman-Ford did not report a cycle we also have that

d [vi ] ≤ d [vi−1] + w(vi−1, vi) for all 1 ≤ i ≤ k . ◮ Summing the inequalities around the cycle gives us

∑k i=1 d [vi ] ≤

∑k i=1 d [vi−1] +

∑k i=1 w(vi−1, vi)

◮ Therefore 0 ≤ ∑k

i=1 w(vi−1, vi) giving us a contradiction. Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 9

Applications of Bellman-Ford

There are a number of applications of variants of Bellman-Ford ◮ ◮ Subroutine in other graph algorithms

◮ Route planning ◮ Difference constraints

◮ When route planning in a network we need to find the shortest path to a destination.

◮ We could use Dijkstra’s algorithm but it requires global knowledge which is not practical in a router

◮ Bellman-Ford only requires each node to update its own d [vi ] and can be modified to be even more efficient.

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 10

Difference constraints

Linear Programming is a very important and general way of expressing problems in optimisation. We consider a much simplified version involving difference constraints.

◮ Linear Programming - Let A be an m × n matrix, b be an m-vector and c be an n-vector. Find an n-vector that maximises cT x subject to Ax ≤ b, or determine that no such solution exists

◮ See blackboard for example. ◮ Linear Programming using the simplex method, this is fast and

practical but exponential in the worst case ◮ Interior point methods, polynomial time and in current competition with

simplex methods in practice

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 11

Difference constraints

Linear Programming where each row of A contains exactly one 1, one −1 and the rest 0’s gives us a system of difference constraints.

Example

x1 − x2 ≤ 3

x2 − x3 ≤ −2

x1 − x3 ≤ 2

Solution: x1 = 3

x2 = 0

x3 = 2

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 12

Difference constraints

Systems of difference constraints occur in many different applications. ◮ Consider the unknown xi to be times at which events are to occur ◮ Each constraint tells us the minimum or maximum time between

events ◮ x2 ≥ x1 + 2⇒ x1 − x2 ≤ −2 might mean that event 2 must start at

least 2 minutes after event 1 does ◮ Or event 2 must happen after event 1 but no later than one minute

after. I.e. x2 ≥ x1 and x2 ≤ x1 + 1 which is equivalent to x1 − x2 ≤ 0 and x2 − x1 ≤ 1

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 13

Difference constraints

We interpret the constraints in a graph theoretic way. ◮ If A is an m × n matrix then we represent it by a graph with n vertices

and m edges ◮ Each vertex vi in the graph corresponds to one of the n unknown

variables xi ◮ Each directed edge in the graph corresponds to one of the m

inequalities involving two unknowns ◮ We also add a vertex v0 and edges of weight 0 from it to every other

vertex to guarantee that every other vertex is reachable

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 14

Difference constraints

Theorem If the constraint graph contains a negatively weighted cycle, then the system of differences is unsatisfiable

Proof. Suppose the negatively weighted cycle is v1 → v2 → . . .→ vk → v1. Then

x2 − x1 ≤ w12

x3 − x2 ≤ w23 ...

xk − xk−1 ≤ wk−1,k

x1 − xk ≤ wk1

Therefore, 0 ≤ weight of cycle⇒ 0 < 0

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 15

Difference constraints

Theorem Suppose no negatively weighted cycles exist in the constraint graph. Then, the constraints are satisfiable.

Proof. Let the source s be the vertex v0 which was added with a zero weighted edge to every over vertex. No negatively weighted cycle can have been introduced by this extra vertex. Therefore, a shortest path exists from this vertex to each other vertex. The claim is that if we let xi = δ(s, vi) then the constraints will be solved.

◮ Consider any constraint xj − xi ≤ wij , and consider the shortest paths from s to vj and vi

◮ The triangle inequality gives us δ(s, vj) ≤ δ(s, vi) + wij . Since xi = δ(s, vi) and xj = δ(s, vj), the constraint xj − xi ≤ wij is satisfied.

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 16

Difference constraints

Theorem The Bellman-Ford algorithm can solve a system of m difference constraints on n variables in O((n + 1)(n + m)) = O(n2 + nm) time.

A system of difference constraints with m constraints and n unknowns produces a graph with n + 1 vertices and n + m edges. Therefore Bellman-Ford will run in O((n + 1)(n + m)) = O(n2 + nm) time. Exercise 24.4-5 CLRS - show how to reduce the complexity to O(nm) time.

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 17

Conclusions

◮ We have looked at the Single-Source Shortest Path problem in graphs that may contain edges with negative weights.

◮ We have seen that Dijkstra’s algorithm will no longer work ◮ The Bellman-Ford algorithms runs in O(|V ||E |) time and will detect

any negatively weighted cycles as well ◮ Further, Bellman-Ford has a number of important applications

including solving difference constraints, which is a limited form of Linear Programming.

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 18

Further Reading

◮ Introduction to Algorithms T.H. Cormen, C.E. Leiserson, R.L. Rivest and C. Stein. MIT Press/McGraw-Hill, ISBN: 0-262-03293-7.

◮ Chapter 24 – Single-Source Shortest Paths ◮ Algorithms

S. Dasgupta, C.H. Papadimitriou and U.V. Vazirani http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/

◮ Chapter 4, Section 4.6 – Shortest paths in the presence of negative edges

Raphaël Clifford

clifford@cs.bris.ac.uk

COMS21102: Bellman-Ford Slide 19

Docsity is not optimized for the browser you're using. In order to have a better experience please switch to Google Chrome, Firefox, Internet Explorer 9+ or Safari! Download Google Chrome