dijkstra's algorithm demo

24
Lecture slides by Kevin Wayne Copyright © 2005 Pearson-Addison Wesley http://www.cs.princeton.edu/~wayne/kleinberg-tardos Last updated on 11/15/17 10:10 AM 4. G REEDY A LGORITHMS II Dijkstra’s algorithm demo Dijkstra’s algorithm demo (efficient implementation)

Upload: nguyendiep

Post on 14-Feb-2017

239 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Dijkstra's Algorithm Demo

Lecture slides by Kevin WayneCopyright © 2005 Pearson-Addison Wesley

http://www.cs.princeton.edu/~wayne/kleinberg-tardos

Last updated on 11/15/17 10:10 AM

4. GREEDY ALGORITHMS II

‣ Dijkstra’s algorithm demo

‣ Dijkstra’s algorithm demo(efficient implementation)

Page 2: Dijkstra's Algorithm Demo

4. GREEDY ALGORITHMS II

‣ Dijkstra’s algorithm demo

‣ Dijkstra’s algorithm demo(efficient implementation)

Page 3: Dijkstra's Algorithm Demo

・Initialize S ← { s } and d[s] ← 0.

・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.

Dijkstra’s algorithm demo

3

4 8

3

16 2

67 5

1

d[s] 0

S s

the length of a shortest path from s to some node u in explored part S, followed by a single edge e = (u, v)

�(v) = mine = (u,v) : u�S

d[u] + �e

Page 4: Dijkstra's Algorithm Demo

Dijkstra’s algorithm demo

・Initialize S ← { s } and d[s] ← 0.

・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.

4

4 8

16

0

0 + 4 = 4 0 + 8 = 8

0 + 16 = 16

sS

d[s]

the length of a shortest path from s to some node u in explored part S, followed by a single edge e = (u, v)

�(v) = mine = (u,v) : u�S

d[u] + �e

Page 5: Dijkstra's Algorithm Demo

Dijkstra’s algorithm demo

・Initialize S ← { s } and d[s] ← 0.

・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.

5

v

0

4

8

3

16

4 + 3 = 7 0 + 8 = 8

0 + 16 = 16

s

S

the length of a shortest path from s to some node u in explored part S, followed by a single edge e = (u, v)

�(v) = mine = (u,v) : u�S

d[u] + �e

pred[v]

Page 6: Dijkstra's Algorithm Demo

Dijkstra’s algorithm demo

・Initialize S ← { s } and d[s] ← 0.

・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.

6

0

4 7

16

7

1

7 + 1 = 8

0 + 16 = 16 7 + 7 = 14

s

S

the length of a shortest path from s to some node u in explored part S, followed by a single edge e = (u, v)

�(v) = mine = (u,v) : u�S

d[u] + �e

Page 7: Dijkstra's Algorithm Demo

Dijkstra’s algorithm demo

・Initialize S ← { s } and d[s] ← 0.

・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.

7

0

4 7 8

16

7 5 6

0 + 16 = 16 7 + 7 = 14 8 + 5 = 13 8 + 6 = 14

s

S

the length of a shortest path from s to some node u in explored part S, followed by a single edge e = (u, v)

�(v) = mine = (u,v) : u�S

d[u] + �e

Page 8: Dijkstra's Algorithm Demo

Dijkstra’s algorithm demo

・Initialize S ← { s } and d[s] ← 0.

・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.

8

0

4 7 8

13 + 2 = 15 8 + 6 = 14

6

2

13

s

S

the length of a shortest path from s to some node u in explored part S, followed by a single edge e = (u, v)

�(v) = mine = (u,v) : u�S

d[u] + �e

Page 9: Dijkstra's Algorithm Demo

Dijkstra’s algorithm demo

・Initialize S ← { s } and d[s] ← 0.

・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.

9

v

0

4 7 8

1413

s

S

�(v) = mine = (u,v) : u�S

d[u] + �e

Page 10: Dijkstra's Algorithm Demo

Dijkstra’s algorithm demo

・Initialize S ← { s } and d[s] ← 0.

・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.

10

v

0

4 7 8

1413

�(v) = mine = (u,v) : u�S

d[u] + �e

s

4π[v]

pred[v]

Page 11: Dijkstra's Algorithm Demo

4. GREEDY ALGORITHMS II

‣ Dijkstra’s algorithm demo

‣ Dijkstra’s algorithm demo(efficient implementation)

Page 12: Dijkstra's Algorithm Demo

Initialization.

・For all v ≠ s : π [v] ← ∞.

・For all v ≠ s : pred [v] ← null.

・S ← ∅ and π [s] ← 0.

s

Dijkstra’s algorithm demo (efficient implementation)

12

4 8

3

16 2

67 5

1

0π[s]

Page 13: Dijkstra's Algorithm Demo

Basic step. Choose unexplored node u ∉ S with minimum π [u].

・Add u to S.

・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:

- π [v] ← π [u] + ℓe

- pred[v] ← e

s

Dijkstra’s algorithm demo (efficient implementation)

13

4 8

16

0

0 + 4 = 4 0 + 8 = 8

0 + 16 = 16

Page 14: Dijkstra's Algorithm Demo

Basic step. Choose unexplored node u ∉ S with minimum π [u].

・Add u to S.

・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:

- π [v] ← π [u] + ℓe

- pred[v] ← e

v

s

Dijkstra’s algorithm demo (efficient implementation)

14

4 8

16

0

0 + 4 = 4 0 + 8 = 8

0 + 16 = 16

pred[v]

Page 15: Dijkstra's Algorithm Demo

Basic step. Choose unexplored node u ∉ S with minimum π [u].

・Add u to S.

・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:

- π [v] ← π [u] + ℓe

- pred[v] ← e

Dijkstra’s algorithm demo (efficient implementation)

15

0

4 8

16

3

4 + 3 = 7

s

Page 16: Dijkstra's Algorithm Demo

Basic step. Choose unexplored node u ∉ S with minimum π [u].

・Add u to S.

・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:

- π [v] ← π [u] + ℓe

- pred[v] ← e

Dijkstra’s algorithm demo (efficient implementation)

16

0

4 8

16

3

4 + 3 = 7

s

Page 17: Dijkstra's Algorithm Demo

Basic step. Choose unexplored node u ∉ S with minimum π [u].

・Add u to S.

・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:

- π [v] ← π [u] + ℓe

- pred[v] ← e

Dijkstra’s algorithm demo (efficient implementation)

17

0

4 7

16

7

1

7 + 1 = 8

7 + 7 = 14

s

Page 18: Dijkstra's Algorithm Demo

Basic step. Choose unexplored node u ∉ S with minimum π [u].

・Add u to S.

・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:

- π [v] ← π [u] + ℓe

- pred[v] ← e

Dijkstra’s algorithm demo (efficient implementation)

18

0

4 7

16

7

1

7 + 1 = 8

7 + 7 = 14

s

Page 19: Dijkstra's Algorithm Demo

Basic step. Choose unexplored node u ∉ S with minimum π [u].

・Add u to S.

・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:

- π [v] ← π [u] + ℓe

- pred[v] ← e

Dijkstra’s algorithm demo (efficient implementation)

19

0

4 7

14

8

5 6

8 + 5 = 138 + 6 = 14

s

Page 20: Dijkstra's Algorithm Demo

Basic step. Choose unexplored node u ∉ S with minimum π [u].

・Add u to S.

・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:

- π [v] ← π [u] + ℓe

- pred[v] ← e

Dijkstra’s algorithm demo (efficient implementation)

20

0

4 7

14

8

5 6

8 + 5 = 138 + 6 = 14

s

Page 21: Dijkstra's Algorithm Demo

Basic step. Choose unexplored node u ∉ S with minimum π [u].

・Add u to S.

・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:

- π [v] ← π [u] + ℓe

- pred[v] ← e

Dijkstra’s algorithm demo (efficient implementation)

21

0

4 7

13

8

1413 + 2 = 15

2s

Page 22: Dijkstra's Algorithm Demo

Basic step. Choose unexplored node u ∉ S with minimum π [u].

・Add u to S.

・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:

- π [v] ← π [u] + ℓe

- pred[v] ← e

Dijkstra’s algorithm demo (efficient implementation)

22

0

4 7

13

8

14

s

Page 23: Dijkstra's Algorithm Demo

Basic step. Choose unexplored node u ∉ S with minimum π [u].

・Add u to S.

・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:

- π [v] ← π [u] + ℓe

- pred[v] ← e

Dijkstra’s algorithm demo (efficient implementation)

23

0

4 7

13

8

14

s

Page 24: Dijkstra's Algorithm Demo

Dijkstra’s algorithm demo (efficient implementation)

Termination.

・π [v] = length of a shortest s↝v path.

・ pred[v] = last edge on a shortest s↝v path.

24

v

s

0

4 7

13

8

14

π[v]

pred[v]