Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Alg Lec25 Spt2 Print

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

24.

1 The Bellman-Ford Algorithm

24.1 The Bellman-Ford Algorithm (continued)


Bellman Ford(G, w, s) 1 s. d = 0 2 s. = NIL 3 for all v V {s} 4 v. d = 5 v. = NIL 6 for i = 1 to |V | 1
7 8 9 10 11 12

The Bellman-Ford algorithm solves the single-source shortest paths problem. Unlike Dijkstras algorithm, it allows edges of negative length. The bad news is that it runs slower. Like Dijkstras algorithm, the Bellman-Ford algorithm uses the technique of relaxation, progressively decreasing estimates v. d of the distance from s until the estimates are precise. The algorithm returns true iff the graph contains no negative cycles that are reachable from the source.

for each edge (u, v) E Relax(u, v, w) if v. d > u. d + w(u, v) return false /* i.e., (u, v) can still be relaxed */

for each edge (u, v) E return true

The running time is O(V E ). COMP3600/6466: Lecture 25 2012 1 COMP3600/6466: Lecture 25 2012 2

24.1 The Bellman-Ford Algorithm (continued)

24.1 The Bellman-Ford Algorithm (continued)


Proof of correctness. As usual with relaxation, we always have v. d (s, v) and v. d can only decrease. A phase is one iteration of the for loop of lines 68, where each edge is relaxed once. (1) Suppose there is no negative cycle reachable from s. We can assume k |V | 1. Consider some shortest path s v1 v2 vk1 vk .


COMP3600/6466: Lecture 25 2012 3

When (s, v1) is relaxed in the 1st phase, v1. d is set to (s, v1) if it isnt already. When (v1, v2) is relaxed in the 2nd phase, v2. d is set to (s, v2) if it isnt already.

When (vk1, vk ) is relaxed in the kth phase, vk . d is set to (s, vk ) if it isnt already.

So v. d = (s, v) for all v after |V | 1 phases, and no edges are still relaxable. COMP3600/6466: Lecture 25 2012 4

24.1 The Bellman-Ford Algorithm (continued)


(2) Suppose there is a negative cycle reachable from s. Say the cycle is v0 v1 v2 vk = v0. After |V | 1 phases, all the v. d values on the cycle are nite. Suppose that none of the edges on the cycle are relaxable. That is,

24.2 Single-Source Shortest Paths in DAGs


For a directed graph without cycles (a DAG), we can get by with fewer relaxations. We relax the edges according to the increasing topological order of their start vertices.

Summing this inequality over i = 1, . . . , k, we nd a contradiction since the sum of w(vi1, vi) is negative by assumption. Therefore, some edge of the negative cycle is still relaxable.

vi. d vi1. d + w(vi1, vi)

for i = 1, . . . , k.

DAG Shortest Paths(G, w, s) 1 s. d = 0 2 s. = NIL 3 for all v V {s} 4 v. d = 5 v. = NIL 6 for each vertex u in topological order 7 for v G. Adj [u] 8 Relax(u, v, w)
The time complexity is O(V + E ). 5 COMP3600/6466: Lecture 25 2012 6

Note that fewer than |V | 1 phases may not be enough. COMP3600/6466: Lecture 25 2012

24.2 Shortest Paths in DAGs (continued)

24.2 Shortest Paths in DAGs (continued)

Proof of correctness. Consider any shortest path s v1 v2 vk1 vk . The algorithm relaxes the edges from left to right.

When (s, v1) is relaxed, v1. d is set to (s, v1) if it isnt already. When (v1, v2) is relaxed, v2. d is set to (s, v2) if it isnt already.

When (vk1, vk ) is relaxed, vk . d is set to (s, vk ) if it isnt already.

So v. d = (s, v) for all v by the time we are nished.

COMP3600/6466: Lecture 25

2012

COMP3600/6466: Lecture 25

2012

24.4 Difference constraints


Suppose we have to schedule n tasks T1, T2, . . . , Tn, and we have a set of constraints like these:

24.4 Difference constraints (continued)

T3 must be done at least 15 minutes after T7 T2 must be done before T9 T2 must be done at least 5 minutes before T4 T5 must be done at most 10 minutes after T1
and so on. We wish to know if this is possible and if so nd a schedule. If Ti is scheduled at time xi, the above constraints can be written as:

In general, we have real variables x1, x2, . . . , xn, and some number of constraints of the form x j xi bk . We will dene a weighted graph G = (V , E , w), called the constraint graph. There are n + 1 vertices V = {v0, v1, v2, . . . , vn}. There is an edge (v0, vi) of length 0 for i = 1, 2, . . . , n. For each constraint x j xi bk , there is an edge (vi, v j ) with length bk . Interesting fact: If the constraint graph has a negative cycle, there is no solution. Otherwise, an example of a solution is

x7 x3 15 x2 x9 0 x2 x4 5 x5 x1 10
COMP3600/6466: Lecture 25 2012 9

xi = (v0, vi) for i = 1, 2, . . . , n.

COMP3600/6466: Lecture 25

2012

10

24.4 Example system of difference constraints

24.4 Example constraint graph and solution

x1 x2 0, x1 x5 1, x2 x5 1, x3 x1 5, x4 x1 4, x4 x3 1, x5 x3 3, x5 x4 3.

(1) (2) (3) (4) (5) (6) (7) (8)

COMP3600/6466: Lecture 25

2012

11

COMP3600/6466: Lecture 25

2012

12

You might also like