Upload
rebecca-odom
View
33
Download
0
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
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
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.
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)
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
5
Directed graphs
4 3
4
1
1
5
2
1 1
1
a b c
d
e f
g h
2
6
Dijkstra's Algorithm
7
Choose a
4 3
4
1
15
2
1
1
1
a b c
d
e f
g h
2
0 ∞ ∞
∞
∞ ∞
∞∞
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
∞
∞
∞ ∞
∞
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
∞
∞
∞ ∞
∞
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
∞ ∞
∞
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
∞ ∞
∞
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
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
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
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
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
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
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
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
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
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
22
The resulting SPT
4 3
1
1
5 1
1
a b c
d
e f
g h
23
Negative edge
2 2
4
1
1
5
-3
1 1
1
a b c
d
e f
g h
2
24
Choose a
2 2
4
1
15
-3
1
1
1
a b c
d
e f
g h
2
0 2 ∞
∞
∞ ∞
∞5
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
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
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
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
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
30
A wrong SPT
2 2
1
1
5
1
a b c
d
e f
g h
1
31
A correct SPT
2 2
1
5
-3
1
a b c
d
e f
g h
2
32
The Bellman-Ford Algorithm
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
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
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
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
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
38
Try this in class
4 3
4
1
1
5
2
1 -3
1
a b c
d
e f
g h
2