38
Shortest-Paths Trees Kun-Mao Chao ( 趙趙趙 ) Department of Computer Scienc e and Information Engineering National Taiwan University, T aiwan E-mail: [email protected] WWW: http://www.csie.ntu.edu.tw/~k mchao

Shortest-Paths Trees

Embed Size (px)

DESCRIPTION

Shortest-Paths Trees. Kun-Mao Chao ( 趙坤茂 ) Department of Computer Science and Information Engineering National Taiwan University, Taiwan E-mail: [email protected] WWW: http://www.csie.ntu.edu.tw/~kmchao. Shortest-Paths Trees. - PowerPoint PPT Presentation

Citation preview

Page 1: Shortest-Paths Trees

Shortest-Paths Trees

Kun-Mao Chao (趙坤茂 )Department of Computer Science an

d Information EngineeringNational Taiwan University, Taiwan

E-mail: [email protected]

WWW: http://www.csie.ntu.edu.tw/~kmchao

Page 2: Shortest-Paths Trees

2

Shortest-Paths Trees

• The objective is to find the set of edges connecting all nodes such that the sum of the edge lengths from the source to each node is minimized.

• In order to minimize the total path lengths, the path from the source to each node must be a shortest path connecting them.

Page 3: Shortest-Paths Trees

3

Shortest-Paths Trees

4 3

4

1

1

5

2

1 1

2

a b c

d

e f

g h

2

4 3

4

1

1

5

2

1 1

2

a b c

d

e f

g h

2

(a) (b)

Page 4: Shortest-Paths Trees

4

Negative edges in an undirected graph

4 3

4

1

1

5

2

-1 1

2

a b c

d

e f

g h

2

Page 5: Shortest-Paths Trees

5

Directed graphs

4 3

4

1

1

5

2

1 1

1

a b c

d

e f

g h

2

Page 6: Shortest-Paths Trees

6

Dijkstra's Algorithm

Page 7: Shortest-Paths Trees

7

Choose a

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 ∞ ∞

∞ ∞

∞∞

Page 8: Shortest-Paths Trees

8

Relax (a, b) and (a, g)

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

∞ ∞

Page 9: Shortest-Paths Trees

9

Choose b; Add (a, b) to T

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

∞ ∞

Page 10: Shortest-Paths Trees

10

Relax (b, c) and (b, d)

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

7

5

∞ ∞

Page 11: Shortest-Paths Trees

11

Choose g; Add (a, g) to T

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

7

5

∞ ∞

Page 12: Shortest-Paths Trees

12

Relax (g, e) and (g, h)

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

7

5

7 ∞

6

Page 13: Shortest-Paths Trees

13

Choose d; Add (b, d) to T

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

7

5

7 ∞

6

Page 14: Shortest-Paths Trees

14

Relax (d, e)

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

7

5

6 ∞

6

Page 15: Shortest-Paths Trees

15

Choose h; Add (g, h) to T

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

7

5

6 ∞

6

Page 16: Shortest-Paths Trees

16

Choose e; Add (d, e) to T

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

7

5

6 ∞

6

Page 17: Shortest-Paths Trees

17

Relax (e, f)

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

7

5

6 7

6

Page 18: Shortest-Paths Trees

18

Choose c; Add (b, c) to T

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

7

5

6 7

6

Page 19: Shortest-Paths Trees

19

Relax (c, h)

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

7

5

6 7

6

Page 20: Shortest-Paths Trees

20

Choose f; Add (e, f) to T

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

7

5

6 7

6

Page 21: Shortest-Paths Trees

21

Relax (f, d) and (f, h)

4 3

4

1

15

2

1

1

1

a b c

d

e f

g h

2

0 4

5

7

5

6 7

6

Page 22: Shortest-Paths Trees

22

The resulting SPT

4 3

1

1

5 1

1

a b c

d

e f

g h

Page 23: Shortest-Paths Trees

23

Negative edge

2 2

4

1

1

5

-3

1 1

1

a b c

d

e f

g h

2

Page 24: Shortest-Paths Trees

24

Choose a

2 2

4

1

15

-3

1

1

1

a b c

d

e f

g h

2

0 2 ∞

∞ ∞

∞5

Page 25: Shortest-Paths Trees

25

Choose b; Add (a, b) to T

2 2

4

1

15

-3

1

1

1

a b c

d

e f

g h

2

0 2 4

3

∞ ∞

∞5

Page 26: Shortest-Paths Trees

26

Choose d; Add (b, d) to T

2 2

4

1

15

-3

1

1

1

a b c

d

e f

g h

2

0 2 4

3

4 ∞

∞5

Page 27: Shortest-Paths Trees

27

Choose c; Add (b, c) to T

2 2

4

1

15

-3

1

1

1

a b c

d

e f

g h

2

0 2 4

3

4 ∞

85

Page 28: Shortest-Paths Trees

28

Choose e; Add (d, e) to T

2 2

4

1

15

-3

1

1

1

a b c

d

e f

g h

2

0 2 4

3

4 5

85

Page 29: Shortest-Paths Trees

29

Something went wrong

2 2

4

1

15

-3

1

1

1

a b c

d

e f

g h

2

0 2 4

3

4 5

65

Page 30: Shortest-Paths Trees

30

A wrong SPT

2 2

1

1

5

1

a b c

d

e f

g h

1

Page 31: Shortest-Paths Trees

31

A correct SPT

2 2

1

5

-3

1

a b c

d

e f

g h

2

Page 32: Shortest-Paths Trees

32

The Bellman-Ford Algorithm

Page 33: Shortest-Paths Trees

33

δ[b] and δ[g] modified

2 2

4

1

15

-3

1

1

1

a b c

d

e f

g h

2

0 2 ∞

∞ ∞

∞5

Page 34: Shortest-Paths Trees

34

δ[c], δ[d], δ[e] and δ[h] modified

2 2

4

1

15

-3

1

1

1

a b c

d

e f

g h

2

0 2 4

3

2 ∞

65

Page 35: Shortest-Paths Trees

35

δ[f] modified

2 2

4

1

15

-3

1

1

1

a b c

d

e f

g h

2

0 2 4

3

2 3

65

Page 36: Shortest-Paths Trees

36

δ[h] modified

2 2

4

1

15

-3

1

1

1

a b c

d

e f

g h

2

0 2 4

3

2 3

55

Page 37: Shortest-Paths Trees

37

A correct SPT

2 2

4

1

15

-3

1

1

1

a b c

d

e f

g h

2

0 2 4

3

2 3

55

Page 38: Shortest-Paths Trees

38

Try this in class

4 3

4

1

1

5

2

1 -3

1

a b c

d

e f

g h

2