44 demo dijk stra

34
Algorithms, 4 th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · November 11, 2011 5:56:51 AM Algorithms F O U R T H E D I T I O N R O B E R T S E D G E W I C K K E V I N W AY N E 4.4 DIJKSTRA'S ALGORITHM DEMO

Upload: ivanna-jolie

Post on 12-Dec-2015

221 views

Category:

Documents


0 download

DESCRIPTION

demo

TRANSCRIPT

Page 1: 44 Demo Dijk Stra

Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · November 11, 2011 5:56:51 AM

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

R O B E R T S E D G E W I C K K E V I N W A Y N E

4.4 DIJKSTRA'S ALGORITHM DEMO

Page 2: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

2

0

4

7

1 3

5

2

6

s

69

8

4

5

7

1

54

15

312

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

Page 3: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

3

0

4

7

1 3

5

2

6

choose source vertex 0

v distTo[] edgeTo[]

0 0.0 -

1

2

3

4

5

6

7

Page 4: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

4

4

7

1 3

5

2

6

relax all edges incident from 0

9

8

5

v distTo[] edgeTo[]

0 0.0 -

1

2

3

4

5

6

7

0

0

Page 5: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

5

4

7

1 3

5

2

6

relax all edges incident 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

0

5

0

8

9

Page 6: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

6

0

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

Page 7: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

7

0

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

Page 8: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

8

0

4

7

1 3

5

2

6

relax all edges incident 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

Page 9: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

9

0

4

7

1 3

5

2

6

relax all edges incident 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

Page 10: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

10

0

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

Page 11: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

11

0

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

Page 12: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

12

0

4

7

1 3

5

2

6

relax all edges incident 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

78

17

Page 13: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

13

0

4

7

1 3

5

2

6

relax all edges incident 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

78

17

∞ 14

15

Page 14: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

14

0

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

Page 15: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

15

0

4

7

1 3

5

2

6

select vertex 4

s

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

Page 16: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

16

0

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 incident from 4

4

5

20

8

14

9 ∞

Page 17: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

17

0

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 incident from 4

4

5

20

∞ 29

8

14

9

13

Page 18: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

18

0

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

Page 19: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

19

0

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

Page 20: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

20

0

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 incident from 5

1

13

29

13

15

Page 21: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

21

0

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 incident from 5

1

13

29

13

15 14

26

Page 22: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

22

0

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

Page 23: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

23

0

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

Page 24: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

24

0

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 incident from 2

3

11

26

14

20

Page 25: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

25

0

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 incident from 2

3

11

26

14

20 17

25

Page 26: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

26

0

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

Page 27: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

27

0

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

Page 28: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

28

0

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 incident from 3

9

3

25

20

Page 29: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

29

0

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 incident from 3

9

3

25

20

Page 30: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

30

0

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

Page 31: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

31

0

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

Page 32: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

32

0

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 incident from 6

Page 33: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

33

0

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

Page 34: 44 Demo Dijk Stra

• 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 incident from that vertex.

Dijkstra's algorithm

34

0

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