1 introduction to algorithms 6.046j/18.401j/sma5503 lecture 17 prof. erik demaine

38
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

Upload: rosaline-marshall

Post on 16-Dec-2015

246 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

1

Introduction to Algorithms6.046J/18.401J/SMA5503

Lecture 17Prof. Erik Demaine

Page 2: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

2

Paths in graphs

Consider a digraph G = (V, E) with edge-weightfunction w : E → . The weight of path p = v1 →v2 → → vk is defined to be

Example :

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Page 3: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

3

Shortest paths

A shortest path from u to v is a path ofminimum weight from u to v. The shortest-path weight from u to v is defined as

(u, v) = min{w(p) : p is a path from u to v}.

Note: (u, v) = if no path from u to v exists.

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Page 4: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

4

Optimal substructure

Theorem. A subpath of a shortest path is ashortest path.

Proof. Cut and paste:

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Page 5: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

5

Triangle inequality

Theorem. For all u, v, x V, we have(u, v) (u, x) + (x, v).

Proof.

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Page 6: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

6

Well-definedness of shortestpaths

If a graph G contains a negative-weight cycle,then some shortest paths may not exist.

Example:

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Page 7: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

7

Single-source shortest pathsProblem. From a given source vertex s V, findthe shortest-path weights (s, v) for all v V.

If all edge weights w(u, v) are nonnegative, allshortest-path weights must exist.

IDEA: Greedy. 1. Maintain a set S of vertices whose shortest-path distances from s are known.2. At each step add to S the vertex v V – S whose distance estimate from s is minimal.3. Update the distance estimates of vertices adjacent to v.

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Page 8: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

8

d[s] ← 0for each v V – {s} do d[v] ← S ← Q ← V ⊳ Q is a priority queue maintaining V – Swhile Q ≠ do u ← Extract-Min(Q) S ← S {u} for each v Adj[u] do

Dijkstra’s algorithm

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Implicit DECREASE-KEY

relaxationstepif d[v] > d[u] + w(u, v)

then d[v] ← d[u] + w(u, v)

Page 9: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

9

Example of Dijkstra’salgorithm

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Graph withnonnegativeedge weights:

Page 10: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

10

Example of Dijkstra’salgorithm

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

S : {}

Page 11: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

11

Example of Dijkstra’salgorithm

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

S : {A}

Page 12: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

12

Example of Dijkstra’salgorithm

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

S : {A}

Page 13: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

13

Example of Dijkstra’salgorithm

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

S : {A,C}

Page 14: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

14

Example of Dijkstra’salgorithm

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Page 15: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

15

Example of Dijkstra’salgorithm

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Page 16: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

16

Example of Dijkstra’salgorithm

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Page 17: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

17

Example of Dijkstra’salgorithm

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Page 18: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

18

Example of Dijkstra’salgorithm

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Page 19: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

19

Example of Dijkstra’salgorithm

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Page 20: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

20

Correctness — Part I

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Lemma. Initializing d[s] ← 0 and d[v] ← for allv V – {s} establishes d[v] ≥ (s, v) for all v V,and this invariant is maintained over any sequenceof relaxation steps.Proof. Suppose not. Let v be the first vertex forwhich d[v] < (s, v), and let u be the vertex thatcaused d[v] to change: d[v] = d[u] + w(u, v). Then, d[v] < (s, v) supposition ≤ (s, u) + (u, v) triangle inequality ≤ (s,u) + w(u, v) sh. path ≤ specific path ≤ d[u] + w(u, v) v is first violationContradiction.

Page 21: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

21

Correctness — Part II

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Theorem. Dijkstra’s algorithm terminates withd[v] = (s, v) for all v V.Proof. It suffices to show that d[v] = (s, v) for everyv V when v is added to S. Suppose u is the firstvertex added to S for which d[u] ≠ (s, u). Let y be thefirst vertex in V – S along a shortest path from s to u,and let x be its predecessor:

Page 22: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

22

Correctness — Part II(continued)

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Since u is the first vertex violating the claimed invariant,we have d[x] = (s, x). Since subpaths of shortest pathsare shortest paths, it follows that d[y] was set to (s, x) +w(x, y) = (s, y) when (x, y) was relaxed just after x wasadded to S. Consequently, we have d[y] = (s, y) ≤ (s, u)≤ d[u]. But, d[u] ≤ d[y] by our choice of u, and hence d[y]= (s, y) = (s, u) = d[u]. Contradiction.

Page 23: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

23

Analysis of Dijkstra

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

|V|times degree(u

)times

while Q ≠ do u ← EXTRACT-MIN(Q) S ← S {u} for each v Adj[u]

do if d[v] > d[u] + w(u, v) then d[v] ← d[u] + w(u, v)

Handshaking Lemma (E) implicit DECREASE-KEY’s.

Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY

Note: Same formula as in the analysis of Prim’sminimum spanning tree algorithm.

Page 24: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

24

Analysis of Dijkstra(continued)

Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Q TEXTRACT-MIN TDECREASE-KEY Total

array O(V) O(1) O(V2)

binaryheap

O(lg V) O(lg V) O(E lg V)

Fibonacciheap

O(lg V)amortized

O(1)amortized

O(E + V lg V)worst case

Page 25: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

25

Unweighted graphsSuppose w(u, v) = 1 for all (u, v) E. Can thecode for Dijkstra be improved?

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

• Use a simple FIFO queue instead of a priority queue.• Breadth-first search

while Q ≠ do u ← DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] ← d[u] + 1 ENQUEUE(Q, v)

Analysis: Time = O(V + E).

Page 26: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

26

Example of breadth-firstsearch

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Q:

Page 27: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

27

Example of breadth-firstsearch

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Q: a

Page 28: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

28

Example of breadth-firstsearch

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Q: a b d

Page 29: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

29

Example of breadth-firstsearch

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Q: a b d c e

Page 30: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

30

Example of breadth-firstsearch

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Q: a b d c e

Page 31: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

31

Example of breadth-firstsearch

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Q: a b d c e

Page 32: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

32

Example of breadth-firstsearch

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Q: a b d c e g i

Page 33: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

33

Example of breadth-firstsearch

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Q: a b d c e g i f

Page 34: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

34

Example of breadth-firstsearch

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Q: a b d c e g i f h

Page 35: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

35

Example of breadth-firstsearch

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Q: a b d c e g i f h

Page 36: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

36

Example of breadth-firstsearch

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Q: a b d c e g i f h

Page 37: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

37

Example of breadth-firstsearch

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Q: a b d c e g i f h

Page 38: 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 17 Prof. Erik Demaine

38

Correctness of BFSwhile Q ≠ do u ← DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] ← d[u] + 1 ENQUEUE(Q, v)

© 2001 by Charles E. Leiserson Introduction to Algorithms Day 29 L17.

Key idea:The FIFO Q in breadth-first search mimicsthe priority queue Q in Dijkstra.• Invariant: v comes after u in Q implies that d[v] = d[u] or d[v] = d[u] + 1.