26
2004 SDU Lecture9- Single-Source Shortest Paths Related Notions and Variants of Shortest Paths Prob Properties of Shortest Paths and Relaxation The Bellman-Ford Algorithm Single-Source Shortest Paths in Directed Acyclic Gr

2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

Embed Size (px)

Citation preview

Page 1: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU

Lecture9-Single-Source Shortest

Paths

1. Related Notions and Variants of Shortest Paths Problems2. Properties of Shortest Paths and Relaxation3. The Bellman-Ford Algorithm4. Single-Source Shortest Paths in Directed Acyclic Graphs

Page 2: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 2

Shortest Paths—Notions Related

1. Given a weighted, directed graph G = (V, E; w), the weight of path p = <v0, v1, …, vk> is the sum of the weights of its constituent edges, that is,

2. Define the shortest-path weight from u to v by

3. A shortest path from vertex u to v is any path p with weight w(p) = (u, v).

11

( ) ( , )k

i ii

w p w v v

min{ ( ) : } if there is a path from to ,( , )

otherwise.

pw p u v u vu v

Page 3: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 3

Single-source shortest-paths problem

Input: A weighted directed graph G=(V, E, w), and a source vertex s.

Output: Shortest-path weight from s to each vertex v in V, and a shortest path from s to each vertex v in V if v is reachable from s.

Page 4: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 4

Notes on Shortest-Paths

For single-source shortest-paths problem Negative-weight edge and Negative-weight cycle reachable from s

Cycles Can a shortest path contain a cycle?

Page 5: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 5

Representing Shortest-Paths

Given a graph G = (V, E), maintain for each vertex v V a predecessor [v] that is either another vertex or NIL.

Given a vertex v for which [v] NIL, the procedure PRINT-PATH(G, s, v) prints a shortest path from s to v.

Page 6: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 6

Property of Shortest Paths

Lemma 24.1 (Sub-paths of shortest paths are shortest paths) Given a weighted, directed graph G = (V, E; w), let p

= <v1, v2, …, vk> be a shortest path from vertex v1 to vertex vk and, for any i and j such that 1 i j k, let pij = <vi, vi+1, …, vj> be the sub-path of p from vertex vi to vertex vj. Then, pij is a shortest path from vi to vj.

Why?

Page 7: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 7

Relaxation

Relaxation: In our shortest-paths problem, we maintain an attribute

d[v], which is a shortest-path estimate from source s to v. The term RELAXATION is used here for an operation

that tightens d[v], or the difference of d[v] and (s, v).

Page 8: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 8

Relaxation--Initialization

The initial estimate of (s, v) can be given by:

Page 9: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 9

Relaxation Process

Relaxing an edge (u, v) consists: Testing whether we can improve the shortest path to v

found so far by going through u, and if so, Updating d[v] and [v].

A relaxation step may either decrease the value of the shortest-path estimate d[v] and update v’s predecessor [v], or cause no change.

Page 10: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 10

A Relaxation Step

Page 11: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 11

Relaxation

Note:1. All algorithms in this chapter call INITIALIZE-

SINGLE-SOURCE and then repeatedly relax edges

2. Relaxation is the only means by which shortest-path estimates and predecessors change

3. The algorithms differ in how many times they relax each edge and the order in which they relax edges

4. Bellman-Ford relaxes each edge many times, while Dijkstra’s and the algorithm for directed acyclic graphs relax each edge exactly once.

Page 12: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 12

Properties of Shortest Paths and Relaxation

Suppose we have already initialized d[] and [], and the only way used to change their values is relaxation.

Triangle inequality (Lemma 24.10) For any edge (u, v) E, we have (s, v) (s, u) + w(u, v)

Upper-bound property (Lemma 24.11) We always have d[v] (s, v) for all vertices v V, and once d[v]

achieves the value (s, v), it never changes.

No-path property (Lemma 24.12) If there is no path from s to v, then we always have d[v] = (s, v) = .

Page 13: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 13

Properties of Shortest Paths and Relaxation

Convergence property (Lemma 24.14) If s ~ u v is a shortest path from s to v in G, and if

d[u] = (s, u) at any time prior to relaxing edge (u, v), then d[v] = (s, v) at all times afterward.

Path-relaxation property (Lemma 24.15) If p = <s, v1, …, vk> is a shortest path from s to vk, and

the edges of p are relaxed in the order (s, v1), (v1, v2), …, (vk-1, vk), then after relax all the edges, d[vk] = (s, vk).

Note that other relaxations may take place among these relaxations.

Page 14: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 14

Properties of Shortest Paths and Relaxation

Predecessor-subgraph property (Lemma 24.17) Once d[v] = (s, v) for all v V, the predecessor sub-

graph is a shortest-paths tree rooted at s. Predecessor subgraph G = (V, E), where

– V = {v V : [v] Nil} {s}

– E = {([v], v) E : v V-{s}}

Note: Proofs for all these properties can be found in your textbook, Page 230-236.

Page 15: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 15

Shortest-Paths Tree

Let G = (V, E) be a weighted, directed graph with source s and weight function w: ER, and assume that G contains no negative cycles that are reachable from s. A shortest-paths tree rooted at s is a directed sub-graph G’=(V’, E’), where V’V and E’E, such that V’ is the set of vertices reachable from s in G, G’ forms a rooted tree with root s, and For all v V’, the unique simple path from s to v in G’ is a shortest path from

s to v in G.

The shortest-path tree may not be unique.

Page 16: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 16

The Bellman-Ford Algorithm

Solve the single-source shortest-paths problem The algorithm returns a boolean value indicating

whether or not there is a negative-weight cycle that is reachable from the source.

If there is a negative-weight cycle reachable from the source s, the algorithm indicates that no solution exists.

If there are no such cycles, the algorithm produces the shortest paths and their weights.

Exercise 24.1-4

Page 17: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 17

The Bellman-Ford O(VE)-time Algorithm

Page 18: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 18

Edges are relaxed in the order: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x),(z, s), (s, t), (s, y)

Page 19: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 19

Correctness of The Bellman-Ford

Lemma 24.2 Let G = (V, E) be a weighted, directed graph with

source s and weight function w: ER, and assume that G contains no negative cycles that are reachable from s. Then, after the | V | - 1 iterations of the for loop of lines 2-4 of BELLMAN-FORD, we have d[v] = (s, v) for all vertices v reachable from s.

Page 20: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 20

Proof of Lemma 24.2

Proof: Consider any vertex v that is reachable from s, and let p = <v0, v1, …, vk>, where v0 = s and vk = v, be any acyclic shortest path from s to v. Path p has at most | V |-1 edges, and so k | V |-1. Each of the | V |-1 iterations of the for loop of lines 2-4 relaxes all E edges. Among the edges relaxed in the ith iteration, the edge in P is (vi-1, vi), for i = 1, 2, …, k. By the path-relaxation property, d[v] = d[vk] = (s, vk) = (s, v).

Page 21: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 21

Correctness of The Bellman-Ford

Corollary 24.3 Let G = (V, E) be a weighted, directed graph with

source s and weight function w: ER. Then for each vertex v V, there is a path from s to v if and only if BELLMAN-FORD terminates with d[v] < ∞ when it is run on G.

Exercise 24.1-2.

Page 22: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 22

Correctness of The Bellman-Ford

Theorem 24.4 Let BELLMAN-FORD be run on a weighted, directed

graph G = (V, E) with source s and weight function w: ER. If G contains no negative cycles that are reachable from s, then the algorithm returns TRUE, we have d[v] = (s, v) for all vertices v V, and the predecessor sub-graph G is a shortest-paths tree rooted at s. If G does contain a negative weight cycle reachable from s, then the algorithm returns FALSE.

Page 23: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 23

Proof of Theorem 24.4

1. Suppose G contains no negative-weight cycles that are reachable from s.

Prove d[v] = (s, v) for all vertices in V.Prove that the algorithm returns TRUE

For each edge (u, v), d[v] = (s, v) (s, u) + w(u, v) = d[u] + w(u, v).

2. Suppose that G contains a negative-weight cycle that is reachable from s.

Prove that the algorithm returns FALSEOtherwise, for each edge on a negative-weight cycle C=<v1,…,vk>,

d[vi] d[vi+1] + w(vi,vi+1), 1 i k.Summing both sides of equations yields 0 w(C), contradiction.

Page 24: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 24

Single-Source Shortest Paths in Directed Acyclic Graphs

Time Complexity: O(V + E)

Page 25: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 25

Page 26: 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation

2004 SDU 26

Correctness of the Algorithm

Lemma 24.5 If a weighted, directed graph G = (V, E) has source

vertex s and no cycles, then at the termination of the DAG-SHORTEST-PATHS procedure, d[v] = (s, v) for all vertices v V, and the predecessor sub-graph G is a shortest-paths tree rooted at s.

Proof. According to path relaxation property.