Lec17 - prims vs Dijkstra PDF

Title Lec17 - prims vs Dijkstra
Author mohamed khaled
Course Algorithm
Institution جامعة القاهرة
Pages 59
File Size 2.1 MB
File Type PDF
Total Downloads 46
Total Views 136

Summary

prims vs Dijkstra ...


Description

Introduction to Algorithms 6.046J/18.401J LECTURE 17 Shortest Paths I • Properties of shortest paths • Dijkstra’s algorithm • Correctness • Analysis • Breadth-first search Prof. Erik Demaine November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.1

Paths in graphs Consider a digraph G = (V, E) with edge-weight function w : E → R. The weight of path p = v1 → v2 → L → vk is defined to be k −1

w( p) = ∑ w( vi , vi +1 ) . i =1

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.2

Paths in graphs Consider a digraph G = (V, E) with edge-weight function w : E → R. The weight of path p = v1 → v2 → L → vk is defined to be k −1

w( p) = ∑ w( vi , vi +1 ) . i =1

Example:

v1

4

–2

v2

November 14, 2005

v3

–5

1

v4

v5 w(p) = –2

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.3

Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortestpath 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.

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.4

Optimal substructure Theorem. A subpath of a shortest path is a shortest path.

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.5

Optimal substructure Theorem. A subpath of a shortest path is a shortest path.

Proof. Cut and paste:

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.6

Optimal substructure Theorem. A subpath of a shortest path is a shortest path.

Proof. Cut and paste:

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.7

Triangle inequality Theorem. For all u, v, x ∈ V, we have δ(u, v) ≤ δ(u, x) + δ(x, v).

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.8

Triangle inequality Theorem. For all u, v, x ∈ V, we have δ(u, v) ≤ δ(u, x) + δ(x, v).

Proof. δ(u, v)

u δ(u, x)

v δ(x, v)

x November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.9

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

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.10

Well-definedness of shortest paths If a graph G contains a negative-weight cycle, then some shortest paths may not exist. Example:

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

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.14

Dijkstra’s algorithm d[s] ← 0 for each v ∈ V – {s} do d[v] ← ∞ S←∅ Q←V ⊳ Q is a priority queue maintaining V – S while Q ≠ ∅ do u ← EXTRACT-MIN(Q) S ← S ∪ {u} for each v ∈ Adj[u] relaxation do if d[v] > d[u] + w(u, v) step then d[v] ← d[u] + w(u, v)

Implicit DECREASE-KEY November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.15

Example of Dijkstra’s algorithm Graph with nonnegative edge weights:

10

A

1 4 3

November 14, 2005

B

C

2 8

2

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

D 7 9

E

L17.16

Example of Dijkstra’s algorithm ∞ B

Initialize: 10

0 A Q: A B C D E 0







1 4 3



C

2 8

2



∞ D 7 9

E ∞

S: {} November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.17

Example of Dijkstra’s algorithm “A” ← EXTRACT-MIN(Q): 10

0 A Q: A B C D E 0









∞ B

1 4 3

C

2 8

2



∞ D 7 9

E ∞

S: { A } November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.18

Example of Dijkstra’s algorithm Relax all edges leaving A: 10

0 A Q: A B C D E 0

∞ 10

∞ 3

∞ ∞

∞ ∞

10 B

1 4 3

C

2 8

2

3

∞ D 7 9

E ∞

S: { A } November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.19

Example of Dijkstra’s algorithm “C” ← EXTRACT-MIN(Q): 10

0 A Q: A B C D E 0

∞ 10

∞ 3

∞ ∞

∞ ∞

10 B

1 4 3

C

2 8

2

3

∞ D 7 9

E ∞

S: { A, C } November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.20

Example of Dijkstra’s algorithm Relax all edges leaving C: 10

0 A Q: A B C D E 0

∞ 10 7

November 14, 2005

∞ 3

∞ ∞ 11

∞ ∞ 5

7 B

1 4 3

C

2 8

2

3

11 D 7 9

E 5

S: { A, C }

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.21

Example of Dijkstra’s algorithm “E” ← EXTRACT-MIN(Q): 10

0 A Q: A B C D E 0

∞ 10 7

November 14, 2005

∞ 3

∞ ∞ 11

∞ ∞ 5

7 B

1 4 3

C

2 8

2

3

11 D 7 9

E 5

S: { A, C, E }

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.22

Example of Dijkstra’s algorithm Relax all edges leaving E: 10

0 A Q: A B C D E 0

∞ 10 7 7

November 14, 2005

∞ 3

∞ ∞ 11 11

∞ ∞ 5

7 B

1 4 3

C

2 8

2

3

11 D 7 9

E 5

S: { A, C, E }

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.23

Example of Dijkstra’s algorithm “B” ← EXTRACT-MIN(Q): 10

0 A Q: A B C D E 0

∞ 10 7 7

November 14, 2005

∞ 3

∞ ∞ 11 11

∞ ∞ 5

7 B

1 4 3

C

2 8

11 D 7 9

2

3

E 5

S: { A, C, E, B }

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.24

Example of Dijkstra’s algorithm Relax all edges leaving B: 10

0 A Q: A B C D E 0

∞ 10 7 7

November 14, 2005

∞ 3

∞ ∞ 11 11 9

∞ ∞ 5

7 B

1 4 3

C

9 D

2 8

7 9

2

3

E 5

S: { A, C, E, B }

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.25

Example of Dijkstra’s algorithm “D” ← EXTRACT-MIN(Q): 10

0 A Q: A B C D E 0

∞ 10 7 7

November 14, 2005

∞ 3

∞ ∞ 11 11 9

∞ ∞ 5

7 B

1 4 3

C

2 8

2

3

9 D 7 9

E 5

S: { A, C, E, B, D }

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.26

Correctness — Part I Lemma. Initializing d[s] ← 0 and d[v] ← ∞ for all v ∈ V – {s} establishes d[v] ≥ δ(s, v) for all v ∈ V, and this invariant is maintained over any sequence of relaxation steps.

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.27

Correctness — Part I Lemma. Initializing d[s] ← 0 and d[v] ← ∞ for all v ∈ V – {s} establishes d[v] ≥ δ(s, v) for all v ∈ V, and this invariant is maintained over any sequence of relaxation steps. Proof. Suppose not. Let v be the first vertex for which d[v] < δ(s, v), and let u be the vertex that caused 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 violation Contradiction. November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.28

Correctness — Part II Lemma. Let u be v’s predecessor on a shortest path from s to v. Then, if d[u] = δ(s, u) and edge (u, v) is relaxed, we have d[v] = δ(s, v) after the relaxation.

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.29

Correctness — Part II Lemma. Let u be v’s predecessor on a shortest path from s to v. Then, if d[u] = δ(s, u) and edge (u, v) is relaxed, we have d[v] = δ(s, v) after the relaxation. Proof. Observe that δ(s, v) = δ(s, u) + w(u, v). Suppose that d[v] > δ(s, v) before the relaxation. (Otherwise, we’re done.) Then, the test d[v] > d[u] + w(u, v) succeeds, because d[v] > δ(s, v) = δ(s, u) + w(u, v) = d[u] + w(u, v), and the algorithm sets d[v] = d[u] + w(u, v) = δ(s, v). November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.30

Correctness — Part III Theorem. Dijkstra’s algorithm terminates with d[v] = δ(s, v) for all v ∈ V.

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.31

Correctness — Part III Theorem. Dijkstra’s algorithm terminates with d[v] = δ(s, v) for all v ∈ V. Proof. It suffices to show that d[v] = δ(s, v) for every v ∈ V when v is added to S. Suppose u is the first vertex added to S for which d[u] > δ(s, u). Let y be the first vertex in V – S along a shortest path from s to u, and let x be its predecessor:

u S, just before adding u. November 14, 2005

s

x

y

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.32

Correctness — Part III (continued) S s

u x

y

Since u is the first vertex violating the claimed invariant, we have d[x] = δ(s, x). When x was added to S, the edge (x, y) was relaxed, which implies that d[y] = δ(s, y) ≤ δ(s, u) < d[u]. But, d[u] ≤ d[y] by our choice of u. Contradiction. November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.33

Analysis of Dijkstra 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)

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.34

Analysis of Dijkstra |V | times

November 14, 2005

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)

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.35

Analysis of Dijkstra |V | times

while Q ≠ ∅ do u ← EXTRACT-MIN(Q) S ← S ∪ {u} for each v ∈ Adj[u] degree(u) do if d[v] > d[u] + w(u, v) times then d[v] ← d[u] + w(u, v)

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.36

Analysis of Dijkstra |V | times

while Q ≠ ∅ do u ← EXTRACT-MIN(Q) S ← S ∪ {u} for each v ∈ Adj[u] degree(u) do if d[v] > d[u] + w(u, v) times then d[v] ← d[u] + w(u, v)

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

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.37

Analysis of Dijkstra |V | times

while Q ≠ ∅ do u ← EXTRACT-MIN(Q) S ← S ∪ {u} for each v ∈ Adj[u] degree(u) do if d[v] > d[u] + w(u, v) times 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’s minimum spanning tree algorithm. November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.38

Analysis of Dijkstra (continued) Time = Θ(V)·TEXTRACT-MIN + Θ(E)·TDECREASE-KEY Q

TEXTRACT-MIN TDECREASE-KEY

November 14, 2005

Total

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.39

Analysis of Dijkstra (continued) Time = Θ(V)·TEXTRACT-MIN + Θ(E)·TDECREASE-KEY Q

TEXTRACT-MIN TDECREASE-KEY

array

November 14, 2005

O(V)

O(1)

Total O(V2)

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.40

Analysis of Dijkstra (continued) Time = Θ(V)·TEXTRACT-MIN + Θ(E)·TDECREASE-KEY Q

TEXTRACT-MIN TDECREASE-KEY

Total

array

O(V)

O(1)

O(V2)

binary heap

O(lg V)

O(lg V)

O(E lg V)

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.41

Analysis of Dijkstra (continued) Time = Θ(V)·TEXTRACT-MIN + Θ(E)·TDECREASE-KEY Q

TEXTRACT-MIN TDECREASE-KEY

Total

array

O(V)

O(1)

O(V2)

binary heap

O(lg V)

O(lg V)

O(E lg V)

Fibonacci O(lg V) heap amortized November 14, 2005

O(1) O(E + V lg V) amortized worst case

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.42

Unweighted graphs Suppose that w(u, v) = 1 for all (u, v) ∈ E. Can Dijkstra’s algorithm be improved?

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.43

Unweighted graphs Suppose that w(u, v) = 1 for all (u, v) ∈ E. Can Dijkstra’s algorithm be improved? • Use a simple FIFO queue instead of a priority queue.

November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.44

Unweighted graphs Suppose that w(u, v) = 1 for all (u, v) ∈ E. Can Dijkstra’s algorithm be improved? • 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) November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.45

Unweighted graphs Suppose that w(u, v) = 1 for all (u, v) ∈ E. Can Dijkstra’s algorithm be improved? • 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). November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.46

Example of breadth-first search f

a

h

d b

g e

i

c Q: November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.47

Example of breadth-first search 0

f

a

h

d b

g e

i

c 0

Q: a November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.48

Example of breadth-first search 0

a

1

f

h

d 1

b

g e

i

c 1 1

Q: a b d November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.49

Example of breadth-first search 0

a

1

f

h

d 1

b

g e

2

c

i

2 1 2 2

Q: a b d c e November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.50

Example of breadth-first search 0

a

f

1

h

d 1

b

g e

2

c

i

2 2 2

Q: a b d c e November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.51

Example of breadth-first search 0

a

f

1

h

d 1

b

g e

2

c

i

2 2

Q: a b d c e November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.52

Example of breadth-first search 0

a

f

1

h

d 1

2

3

b c

g

e

i

2

3 3 3

Q: a b d c e g i November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.53

Example of breadth-first search 4 0

a

f

1

h

d 1

2

3

b c

g

e

i

2

3 3 4

Q: a b d c e g i f November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.54

Example of breadth-first search 0

a

1

4

4

f

h

d 1

2

3

b c

g

e

i

2

3 4 4

Q: a b d c e g i f h November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.55

Example of breadth-first search 0

a

1

4

4

f

h

d 1

2

3

b c

g

e

i

2

3 4

Q: a b d c e g i f h November 14, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.56

Example of breadth-first search 0

a

1

4

4

f

h
...


Similar Free PDFs