introduction to algorithms

26
Introduction to Algorithms Single-Source Shortest Paths My T. Thai @ UF

Upload: chandler-ewing

Post on 01-Jan-2016

27 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Algorithms. Single-Source Shortest Paths My T. Thai @ UF. Single-Source Shortest Paths Problem. Input : A weighted, directed graph G = ( V , E ) and a source vertex s Weight (length) of a path is the total weight of edges on the path - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Algorithms

Introduction to Algorithms

Single-Source Shortest Paths

My T. Thai @ UF

Page 2: Introduction to Algorithms

Single-Source Shortest Paths Problem

Input: A weighted, directed graph G = (V, E) and a source vertex s Weight (length) of a path is the total weight of

edges on the path

Output: Shortest-path tree from s to each reachable destinations (similar to BFS tree)

My T. [email protected]

2

Value at each vertex: the length of shortest path from s to itShaded edges show shortest paths

Page 3: Introduction to Algorithms

Outline

Main properties

Bellman-Ford algorithm

DAG algorithm Input is directed acyclic graph

Dijkstra’s algorithm No negative weight edges

My T. [email protected]

3

Page 4: Introduction to Algorithms

Negative-weight edges and cycles

If we have a negative-weight cycle, we can just keep going around it and the total weight is -∞

If the negative-weight cycle is not reachable from the source, it is fine

Shortest paths cannot contain cycles Positive-weight we can get a shorter path by ⇒

omitting the cycle Zero-weight: no reason to use them assume that ⇒

our solutions won’t use them

My T. [email protected]

4

Page 5: Introduction to Algorithms

Optimal substructure

Proof: Decompose p into then:

Assume the path from vi to vj such that:

The path has weight less than My T. Thai

[email protected]

5

Page 6: Introduction to Algorithms

Corollary: Let p = Shortest path from s to v, where p = s u v. Then, δ(s, v) = δ(s, u) + w(u, v)

My T. [email protected]

6

p'

Page 7: Introduction to Algorithms

Relaxation v.d: upper bound (shortest-path estimate) on the weight of

a shortest path from source s to v v.: previous vertex of v on the path from s to v

These values are changed when an edge (u, v) is relaxed

My T. [email protected]

7

Page 8: Introduction to Algorithms

Properties of Relaxation

Proof: (by induction) Initially true. If a call to Relax(u, v, w) changes v.d , then :

v.d = v.u + w(u, v)

(s, u) + w(u, v) (by the inductive hypothesis)

(s, v) (by the triangle inequality)

My T. [email protected]

8

Page 9: Introduction to Algorithms

Proof: After relaxing edge (u, v):

v.d u.d + w(u, v) ; Relax algorithm

= (s, u) + w(u, v) ; u.d = (s, u) holds.

= (s, v) ; by Lemma 24.1.

By Lemma 24.11, v.d (s, v) => v.d = (s, v)

My T. [email protected]

9

Page 10: Introduction to Algorithms

Proof: Using induction to show that vi.d = δ(s, vi ) after

(vi−1, vi ) is relaxed

Basis: i = 0. v0.d = 0 = δ(s, v0) = δ(s, s)

Assume vi−1.d = δ(s, vi−1). Relax (vi−1, vi ) => vi.d = δ(s, vi ) and vi.d never changes.

My T. [email protected]

10

Page 11: Introduction to Algorithms

Bellman-Ford algorithm

Allows negative-weight edges Computes v.d and v.π for all v V∈ Returns TRUE if no negative-weight cycles

reachable from s, FALSE otherwise

My T. [email protected]

11

Page 12: Introduction to Algorithms

Bellman-Ford algorithm

Time: O(VE) V – 1 for loop in line 2, each takes O(E) time

My T. [email protected]

12

Page 13: Introduction to Algorithms

Example

My T. [email protected]

13

Page 14: Introduction to Algorithms

Proof of correctness Case 1: G contains no negative cycles reachable from s

Let v be reachable from s , and let p = <v0, v1, …, vk> be a shortest path from s to v, where v0 = s and vk = v

p is acyclic => has ≤ |V| − 1 edges=> k ≤ |V| − 1 Each iteration of the for loop relaxes all edges:

First iteration relaxes (v0, v1)

Second iteration relaxes (v1, v2)

kth iteration relaxes (vk−1, vk)

By path-relaxation property, v.d = vk.d = δ(s, vk ) = δ(s, v)

For all (u, v) E:∈

v.d = δ(s, v) δ(s, u) + w(u, v) = u.d + w(u, v)

return True My T. [email protected]

14

Page 15: Introduction to Algorithms

Proof of correctness Case 2: G contains negative cycles reachable from s

Suppose there exists a cycle c = ‹v0, v1, …, vk›, where v0 = vk reachable from s

Suppose (for contradiction) that BELLMAN-FORD returns TRUE

v0 = vk Contradiction

My T. [email protected]

15

Page 16: Introduction to Algorithms

Shortest paths in directed acyclic graphs

Time: Correctness:

Order of edges on any path is an order of vertices in topologically sorted order

Edges on any shortest path are relaxed in order

By path-relaxation property, correctMy T. Thai

[email protected]

16

Page 17: Introduction to Algorithms

Example

My T. [email protected]

17

Page 18: Introduction to Algorithms

My T. [email protected]

18

Page 19: Introduction to Algorithms

Dijkstra’s algorithm

Assumption: No negative-weight edges a weighted version of breadth-first search, such

as Prim’s algorithm Have two sets of vertices:

S = vertices whose final shortest-path weights are determined

Q = priority queue = V − S (key is v.d for each v ∈Q)

Repeatedly selects u in V–S with minimum shortest path estimate (greedy choice)

My T. [email protected]

19

Page 20: Introduction to Algorithms

Dijkstra’s algorithm

Time: O(V2) using linear array for priority queue O((V + E) lg V) using binary heap O(V lg V + E) using Fibonacci heap

My T. [email protected]

20

Page 21: Introduction to Algorithms

Example

My T. [email protected]

21

Page 22: Introduction to Algorithms

Correctness

Loop invariant: At the start of each iteration of the while loop, v.d =δ(s, v) for all v S∈

Initialization: Initially, S = ∅, so trivially true. Termination: At the end, Q = S = V ∅⇒ ⇒

v.d = δ(s, v) for all v V∈

My T. [email protected]

22

Page 23: Introduction to Algorithms

Maintenance: we show that u.d = δ(s, u) when u is added to S in each iteration

Suppose not, let u be the first vertex such that u.d (s, u) when inserted in S s.d = (s, s) = 0 when s is inserted, so u s

S before u is inserted

There must be some path from s to u, otherwise u.d = δ(s, u) = ∞ by no-path property

My T. [email protected]

23

Page 24: Introduction to Algorithms

The shortest path from s to u shown in the figure y the first vertex after the path leaves S x S ∈ x.d = (s, x) (x, y) was relaxed before y.d = (s, y)

But u is inserted before y. Thus:

u.d = y.d = (s, u). Contradiction

My T. [email protected]

24

Page 25: Introduction to Algorithms

Variants of shortest paths problem

Single-source: Find shortest paths from a given source vertex s V ∈ to every vertex v V∈

Single-destination: Find shortest paths to a given destination vertex (use single source algorithm on reversed graph)

Single-pair: Find shortest path from u to v. No way known that is better in worst case than solving single-source

All-pairs: Find shortest path from u to v for all u, v V ∈ (Next lecture)

My T. [email protected]

25

Page 26: Introduction to Algorithms

Summary Shortest path problem has optimal substructure Bellman-Ford algorithm uses dynamic programming

approach Can detect negative weight cycle reachable from the source Time: O(VE)

On Directed Acyclic Graphs: use topologically sorted order to reduce time to

Dijkstra’s algorithm is a weighted version of breadth-first search with a greedy choice Require all edges to have nonnegative weight Time: O((V + E) lg V) using binary heap

My T. [email protected]

26