Algorithms R OBERT S EDGEWICK | K EVIN W AYNE D IJKSTRA ' S A - - PowerPoint PPT Presentation

algorithms
SMART_READER_LITE
LIVE PREVIEW

Algorithms R OBERT S EDGEWICK | K EVIN W AYNE D IJKSTRA ' S A - - PowerPoint PPT Presentation

Algorithms R OBERT S EDGEWICK | K EVIN W AYNE D IJKSTRA ' S A LGORITHM D EMO Algorithms F O U R T H E D I T I O N R OBERT S EDGEWICK | K EVIN W AYNE http://algs4.cs.princeton.edu Dijkstra's algorithm demo Consider vertices in increasing


slide-1
SLIDE 1

ROBERT SEDGEWICK | KEVIN WAYNE

F O U R T H E D I T I O N

Algorithms

http://algs4.cs.princeton.edu

Algorithms

ROBERT SEDGEWICK | KEVIN WAYNE

DIJKSTRA'S ALGORITHM DEMO

slide-2
SLIDE 2

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

2

4 7 1 3 5 2 6 s 6 9 8 4 5 7 1 5 4 15 3 12 20 13 11 9 an edge-weighted digraph 0→1 5.0 0→4 9.0 0→7 8.0 1→2 12.0 1→3 15.0 1→7 4.0 2→3 3.0 2→6 11.0 3→6 9.0 4→5 4.0 4→6 20.0 4→7 5.0 5→2 1.0 5→6 13.0 7→5 6.0 7→2 7.0

slide-3
SLIDE 3

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

3

4 7 1 3 5 2 6 choose source vertex 0 v distTo[] edgeTo[] 0 0.0 - 1 2 3 4 5 6 7

slide-4
SLIDE 4

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

4

4 7 1 3 5 2 6 relax all edges pointing from 0 9 8 5

∞ ∞ ∞

v distTo[] edgeTo[] 0 0.0 - 1 2 3 4 5 6 7

slide-5
SLIDE 5

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

5

4 7 1 3 5 2 6 relax all edges pointing from 0 9 8 5 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7

5

8 9

slide-6
SLIDE 6

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

6

4 7 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7 1

slide-7
SLIDE 7

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

7

4 7 1 3 5 2 6 choose vertex 1 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7

slide-8
SLIDE 8

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

8

4 7 1 3 5 2 6 relax all edges pointing from 1 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7 4 15 12 5

∞ ∞

8

slide-9
SLIDE 9

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

9

4 7 1 3 5 2 6 relax all edges pointing from 1 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 4 15 12 ✔

∞ ∞

5 17 20 8

slide-10
SLIDE 10

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

10

4 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 7

slide-11
SLIDE 11

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

11

4 7 1 3 5 2 6 choose vertex 7 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7

slide-12
SLIDE 12

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

12

4 7 1 3 5 2 6 relax all edges pointing from 7 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 6 7 8 17

slide-13
SLIDE 13

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

13

4 7 1 3 5 2 6 relax all edges pointing from 7 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 14.0 7→5 6 7 8.0 0→7 6 7 8 17

14 15

slide-14
SLIDE 14

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

14

4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 14.0 7→5 6 7 8.0 0→7

slide-15
SLIDE 15

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

15

4 7 1 3 5 2 6 select vertex 4 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 14.0 7→5 6 7 8.0 0→7

slide-16
SLIDE 16

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

16

4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 14.0 7→5 6 7 8.0 0→7 relax all edges pointing from 4 4 5 20 8 14 9

slide-17
SLIDE 17

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

17

4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 relax all edges pointing from 4 4 5 20 ✔

29 8 14 9 13

slide-18
SLIDE 18

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

18

4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7

slide-19
SLIDE 19

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

19

4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 select vertex 5

slide-20
SLIDE 20

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

20

4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 relax all edges pointing from 5 1 13 29 13 15

slide-21
SLIDE 21

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

21

4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 relax all edges pointing from 5 1 13 29 13 15 14 26

slide-22
SLIDE 22

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

22

4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7

slide-23
SLIDE 23

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

23

4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 select vertex 2

slide-24
SLIDE 24

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

24

4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 relax all edges pointing from 2 3 11 26 14 20

slide-25
SLIDE 25

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

25

4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 relax all edges pointing from 2 3 11 26 14 20 17 25

slide-26
SLIDE 26

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

26

4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7

slide-27
SLIDE 27

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

27

4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 select vertex 3

slide-28
SLIDE 28

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

28

4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 relax all edges pointing from 3 9 3 25 20

slide-29
SLIDE 29

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

29

4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 relax all edges pointing from 3 9 ✔ 3 25 20

slide-30
SLIDE 30

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

30

4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3

slide-31
SLIDE 31

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

31

4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3 select vertex 6

slide-32
SLIDE 32

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

32

4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3 relax all edges pointing from 6

slide-33
SLIDE 33

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

33

4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3

slide-34
SLIDE 34

・Consider vertices in increasing order of distance from s

(non-tree vertex with the lowest distTo[] value).

・Add vertex to tree and relax all edges pointing from that vertex.

Dijkstra's algorithm demo

34

4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3 shortest-paths tree from vertex s s