single source shortest paths with negative weightstaoyf/course/3160/19-fall/lec/bellford.… · we...

28
1/28 Single Source Shortest Paths with Negative Weights Yufei Tao Department of Computer Science and Engineering Chinese University of Hong Kong Yufei Tao Single Source Shortest Paths with Negative Weights

Upload: others

Post on 13-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

1/28

Single Source Shortest Pathswith Negative Weights

Yufei Tao

Department of Computer Science and EngineeringChinese University of Hong Kong

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 2: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

2/28

In this lecture, we will continue our discussion on the single sourceshortest path (SSSP) problem, but this time we will allow the edges totake negative weights.

In this case, Dijkstra’s algorithm no longer works.

We will learn another algorithm — called Bellman-Ford’s algorithm —to compute the shortest paths correctly.

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 3: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

3/28

Let G = (V ,E ) be a directed graph. Let w be a function that maps eachedge in E to an integer, which can be positive, 0, or negative.

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 4: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

4/28

Weighted Graphs

Consider a path in G : (v1, v2), (v2, v3), ..., (v`, v`+1), for some integer` ≥ 1. We define the length of the path as

∑̀i=1

w(vi , vi+1).

Given two vertices u, v ∈ V , a shortest path from u to v is a path thathas the minimum length among all the paths from u to v . Denote byspdist(u, v) the length of the shortest path from u to v .

If v is unreachable from u, then spdist(u, v) =∞.

New: it is possible for spdist(u, v) to be negative.

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 5: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

5/28

Example

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

The path c → d → g has length −5.

What is the the shortest path from a to c? Counter-intuitively, it has aninfinite number of edges! Observe that spdist(a, c) = −∞!

Why? Because there is a negative cycle a→ b → c → d → a!

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 6: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

6/28

Negative cycle

A path (v1, v2), (v2, v3), ..., (v`, v`+1) is a cycle if v`+1 = v1.

It is a negative cycle if its length is negative, namely:

∑̀i=1

w(vi , vi+1) < 0

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 7: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

7/28

Problem (SSSP): Let G = (V ,E ) be a directed weighted graphwhere the weight of every edge can be a positive integer, 0, or anegative integer. It is guaranteed that G has no negative cy-cles. Given a vertex s in V , we want to find, for every other vertext ∈ V \ {s}, a shortest path from s to t, unless t is unreachablefrom s.

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 8: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

8/28

We will learn an algorithm called Bellman-Ford’s algorithm thatsolves both problems in O(|V ||E |) time.

We will focus on computing spdist(s, v), namely, the shortest pathdistance from the source vertex s to every other vertex v ∈ V \{s}.

Constructing the shortest paths is a bi-product of our algorithm, is easy,

and will be left to you.

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 9: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

9/28

Example

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

This graph has no negative cycles.

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 10: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

10/28

Lemma: For every vertex v ∈ V , there is a shortest path from s tov that is a simple path, namely, a path where no vertex appearstwice.

The proof is simple and left to you — note that you must use thecondition that no negative cycles are present.

Corollary: For every vertex v ∈ V , there is a shortest path from sto v that has at most |V | − 1 edges.

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 11: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

11/28

Edge Relaxation

At all times, we will remember, for every v ∈ V \ {s}, a value dist(v),which records the distance of the shortest path from the source vertex sto u we have found so far.

Relaxing an edge (u, v) means:

- If dist(v) < dist(u) + w(u, v), do nothing;

- Otherwise, reduce dist(v) to dist(u) + w(u, v).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 12: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

12/28

Bellman-Ford’s algorithm

1 Set dist(s) = 0, and dist(v) =∞ for all other vertices v ∈ V

2 Repeat the following |V | − 1 times

Relax all edges in E (the ordering by which the edges arerelaxed does not matter)

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 13: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

13/28

Example

Suppose that the source vertex is a.

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

vertex v dist(v)a 0b ∞c ∞d ∞e ∞f ∞g ∞

Although the edge-relaxation ordering does not matter, for illustrationpurposes we will relax the edges in alphabetic order, namely, (u1, v1)before (u2, v2) when

u1 < u2 or

u1 = u2 but v1 < v2.

Here is the alphabetic order of the edges in the graph:(a, b), (a, d), (b, c), (c , d), (c , e), (d , g), (e, d), (f , e), (g , f ).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 14: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

14/28

Example

Relaxing all edges the first time.

Here is what happens after relaxing (a, b):

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

vertex v dist(v)a 0b 1c ∞d ∞e ∞f ∞g ∞

Alphabetic order of the edges in the graph:(a, b), (a, d), (b, c), (c , d), (c , e), (d , g), (e, d), (f , e), (g , f ).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 15: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

15/28

Example

Relaxing all edges the first time.

Here is what happens after relaxing (a, d):

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

vertex v dist(v)a 0b 1c ∞d −6e ∞f ∞g ∞

Alphabetic order of the edges in the graph:(a, b), (a, d), (b, c), (c , d), (c , e), (d , g), (e, d), (f , e), (g , f ).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 16: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

16/28

Example

Relaxing all edges the first time.

Here is what happens after relaxing (b, c):

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

vertex v dist(v)a 0b 1c 2d −6e ∞f ∞g ∞

Alphabetic order of the edges in the graph:(a, b), (a, d), (b, c), (c , d), (c , e), (d , g), (e, d), (f , e), (g , f ).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 17: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

17/28

Example

Relaxing all edges the first time.

Here is what happens after relaxing (c , d):

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

vertex v dist(v)a 0b 1c 2d −6e ∞f ∞g ∞

Alphabetic order of the edges in the graph:(a, b), (a, d), (b, c), (c , d), (c , e), (d , g), (e, d), (f , e), (g , f ).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 18: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

18/28

Example

Relaxing all edges the first time.

Here is what happens after relaxing (c , e):

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

vertex v dist(v)a 0b 1c 2d −6e 1f ∞g ∞

Alphabetic order of the edges in the graph:(a, b), (a, d), (b, c), (c , d), (c , e), (d , g), (e, d), (f , e), (g , f ).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 19: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

19/28

Example

Relaxing all edges the first time.

Here is what happens after relaxing (d , g):

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

vertex v dist(v)a 0b 1c 2d −6e 1f ∞g −9

Alphabetic order of the edges in the graph:(a, b), (a, d), (b, c), (c , d), (c , e), (d , g), (e, d), (f , e), (g , f ).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 20: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

20/28

Example

Relaxing all edges the first time.

Here is what happens after relaxing (e, d):

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

vertex v dist(v)a 0b 1c 2d −6e 1f ∞g −9

Alphabetic order of the edges in the graph:(a, b), (a, d), (b, c), (c , d), (c , e), (d , g), (e, d), (f , e), (g , f ).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 21: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

21/28

Example

Relaxing all edges the first time.

Here is what happens after relaxing (f , e):

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

vertex v dist(v)a 0b 1c 2d −6e 1f ∞g −9

Alphabetic order of the edges in the graph:(a, b), (a, d), (b, c), (c , d), (c , e), (d , g), (e, d), (f , e), (g , f ).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 22: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

22/28

Example

Relaxing all edges the first time.

Here is what happens after relaxing (g , f ):

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

vertex v dist(v)a 0b 1c 2d −6e 1f −7g −9

Alphabetic order of the edges in the graph:(a, b), (a, d), (b, c), (c , d), (c , e), (d , g), (e, d), (f , e), (g , f ).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 23: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

23/28

Example

In the same fashion, relaxing all edges for a second time.

Here is the content of the table at the end of this relaxation round:

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

vertex v dist(v)a 0b 1c 2d −6e −6f −7g −9

Alphabetic order of the edges in the graph:(a, b), (a, d), (b, c), (c , d), (c , e), (d , g), (e, d), (f , e), (g , f ).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 24: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

24/28

Example

In the same fashion, relaxing all edges for a third time.

Here is the content of the table at the end of this relaxation round (nochanges from the previous round):

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

vertex v dist(v)a 0b 1c 2d −6e −6f −7g −9

Alphabetic order of the edges in the graph:(a, b), (a, d), (b, c), (c , d), (c , e), (d , g), (e, d), (f , e), (g , f ).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 25: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

25/28

Example

In the same fashion, relaxing all edges for a fourth time, fifth time, andthen a sixth time. No more changes to the table:

a

b

c

d

e

f

g

1 −6

1−2

−1

5

−3

1

2

vertex v dist(v)a 0b 1c 2d −6e −6f −7g −9

The algorithm then terminates here with the above values as the finalshortest path distances.

Remark: We did 6 rounds because the purpose is to follow thealgorithm description faithfully. In reality, we can stop as soon asno changes are made to the table after some round.

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 26: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

26/28

Time

The running time is clearly O(|V ||E |).

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 27: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

27/28

Correctness

Theorem: Consider any vertex v ; suppose that there is a shortestpath from s to v that has ` edges. Then, after ` rounds of edgerelaxations, it must hold that dist(v) = spdist(v).

Proof:We will prove the theorem by induction on `. If ` = 0, then v = s, inwhich case the theorem is obviously correct. Next, assuming thestatement’s correctness for ` < i where i is an integer at least 1, we willprove it holds for ` = i as well.

Yufei Tao Single Source Shortest Paths with Negative Weights

Page 28: Single Source Shortest Paths with Negative Weightstaoyf/course/3160/19-fall/lec/bellford.… · We will focus on computing spdist(s;v), namely, the shortest path distance from the

28/28

Denote by π the shortest path from s to v , namely, π has i edges. Let pbe the vertex right before v on π.

By the inductive assumption, we know that dist(p) was already equal tospdist(v) after the (i − 1)-th round of edge relaxations.

In the i-th round, by relaxing edge (p, v), we make sure:

dist(v) ≤ dist(p) + w(p, v)

= spdist(p) + w(p, v)

= spdist(v).

Yufei Tao Single Source Shortest Paths with Negative Weights