












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Single Source Shortest path Analysis Correctness Negative Weighted cycle Difference constraints
Typology: Slides
1 / 20
This page cannot be seen from the preview
Don't miss anything!













Raphaël Clifford [email protected]
Bristol University, Department of Computer Science Bristol BS8 1UB, UK
December 3, 2009
Raphaël Clifford [email protected]
◮ (^) 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 [email protected]
Example graph after initialisation.
B
A
C D
E
4
3
5
2 1
2 0
8
8
8 8
Raphaël Clifford [email protected]
Intermediate steps on blackboard.
Raphaël Clifford [email protected]
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 [email protected]
How do we know it actually works? We would like to prove
If G = ( V , E ) contains no negatively weighted cycles, then after the Bellman-Ford algorithm terminates, d [ v ] = δ( s , v ) for all v ∈ V.
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 v 0 ,... , 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 [email protected]
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 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 v 0 , v 1 ,... , vk with v 0 = 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 [email protected]
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 [email protected]
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.
x 1 − x 2 ≤ 3 x 2 − x 3 ≤ − 2 x 1 − x 3 ≤ 2
Solution: x 1 = 3 x 2 = 0 x 3 = 2
Raphaël Clifford [email protected]
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 ◮ (^) x 2 ≥ x 1 + 2 ⇒ x 1 − x 2 ≤ −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. x 2 ≥ x 1 and x 2 ≤ x 1 + 1 which is equivalent to x 1 − x 2 ≤ 0 and x 2 − x 1 ≤ 1
Raphaël Clifford [email protected]
If the constraint graph contains a negatively weighted cycle, then the system of differences is unsatisfiable
Suppose the negatively weighted cycle is v 1 → v 2 →... → vk → v 1. Then
x 2 − x 1 ≤ w 12 x 3 − x 2 ≤ w 23 .. . xk − xk − 1 ≤ wk − 1 , k x 1 − xk ≤ wk 1 Therefore, 0 ≤ weight of cycle ⇒ 0 < 0
Raphaël Clifford [email protected]
Suppose no negatively weighted cycles exist in the constraint graph. Then, the constraints are satisfiable.
Let the source s be the vertex v 0 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 [email protected]
◮ (^) 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 [email protected]
◮ (^) 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 [email protected]