View
0
Download
0
Category
Preview:
Citation preview
Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao
โช Homework 3 releasedโช Due on 12/13 (Thur) 14:20 (one week only)
โช Mini-HW 9 releasedโช Due on 12/13 (Thur) 14:20
โช Homework 4 releasedโช Due on 1/3 (Thur) 14:20 (four weeks later)
2
Frequently check the website for the updated information!
3
โช Single-Source Shortest Pathsโช Bellman-Ford Algorithm
โช Lawler Algorithm (SSSP in DAG)
โช Dijkstra Algorithm
4
Textbook Chapter 24 โ Single-Source Shortest Paths
5
โช Input: a weighted, directed graph ๐บ = ๐, ๐ธโช Weights can be arbitrary numbers, not necessarily distance
โช Weight function needs not satisfy triangle inequality
โช Output: a minimal-cost path from ๐ to ๐ก s.t. ๐ฟ ๐ , ๐ก is the minimum weight from ๐ to ๐ก
โช Problem Variantsโช Single-source shortest-path problemโช Single-destination shortest-path problemโช Single-pair shortest-path problemโช All-pair shortest path problem
6
โช Can a shortest path contain a negative-weight edge?
โช Can a shortest path contain a negative-weight cycle?
โช Can a shortest path contain a cycle?
7
Yes.
Doesnโt make sense.
No.
โช Input: a weighted, directed graph ๐บ = ๐, ๐ธ and a source vertex ๐
โช Output: a minimal-cost path from ๐ to ๐ก, where ๐ก โ ๐
8
โช Let ๐บ = ๐, ๐ธ be a weighted, directed graph with no negative-weight cycles reachable from ๐
โช A shortest path tree ๐บโฒ = ๐โฒ, ๐ธโฒ of ๐ is a subgraph of ๐บ s.t.โช ๐โฒ is the set of vertices reachable from ๐ in ๐บ
โช ๐บโฒ forms a rooted tree with root ๐
โช For all ๐ฃ โ ๐โฒ, the unique simple path from ๐ to ๐ฃ in ๐บโฒ is a shortest path from ๐ to ๐ฃ in ๐บ
9
โช Input: a weighted, directed graph ๐บ = ๐, ๐ธ and a vertex ๐
โช Output: a tree ๐ rooted at ๐ s.t. the path from ๐ to ๐ข of ๐ is a shortest path from ๐ to ๐ข in ๐บ
10
โช The shortest path tree problem is equivalent to finding the minimal cost ๐ฟ ๐ , ๐ข from ๐ to each node ๐ข in ๐บโช The minimal cost from ๐ to ๐ข in ๐บ is the length of any
shortest path from ๐ to ๐ข in ๐บ
11
โequivalenceโ: a solution to either problem can be obtained from a solution to the other problem in linear time
Shortest Path Tree Problem
Single-Source Shortest Path Problem
=
Textbook Chapter 24.1 โ The Bellman-Ford algorithm
12
Richard Bellman, 1920~1984
โช Norbert Wiener Prize in Applied Mathematics, 1970
โช Dickson Prize, Carnegie-Mellon University, 1970
โช John von Neumann Theory Award, 1976.
โช IEEE Medal of Honor, 1979,
โช Fellow of the American Academy of Arts and Sciences, 1975.
โช Membership in the National Academy of Engineering, 1977
Lester R. Ford, Jr. 1927~2017
โช Proved the algorithm before Bellman
โช An important contributor to the theory of network flow.
13
โช Idea: estimate the value of ๐ ๐ข to approximate ๐ฟ ๐ , ๐ข
โช Initializationโช Let ๐ ๐ข = โ for ๐ข โ ๐บ
โช Let ๐ ๐ = 0
โช Repeat the following step for sufficient number of phasesโช For each edge ๐ข, ๐ฃ โ ๐ธ, relax edge ๐ข, ๐ฃ
โช Relaxing: If ๐ ๐ฃ > ๐ ๐ข + ๐ค ๐ข, ๐ฃ , let ๐ ๐ฃ = ๐ ๐ข + ๐ค ๐ข, ๐ฃ
14
โ improve the estimation of ๐ ๐ข
15
0
โ โ6
3
2 14
2 7
3
6
5โ โ5
16
0
โ โ6
3
2 14
2 7
3
6
5โ5
6
17
0
โ6
3
2 14
2 7
3
6
5โ5
63
โช Observation: let ๐ be a shortest path from ๐ to ๐โช For any vertex ๐ข in ๐, the subpath of ๐ from ๐ to ๐ข has to be a
shortest path from ๐ to ๐ขโ optimal substructure
โช For any edge ๐ข, ๐ฃ in ๐, if ๐ ๐ข = ๐ฟ ๐ , ๐ข , then ๐ ๐ฃ = ๐ฟ ๐ , ๐ฃ also holds after relaxing edge ๐ข, ๐ฃ
โช If ๐บ contains no negative cycles, then each node ๐ข has a shortest path from ๐ to ๐ข that has at most n โ 1 edges
โช From observation, after the first ๐ phases of improvement via relaxation, the estimation of ๐ ๐ข for the first ๐ + 1 nodes ๐ข in the path is precise (= ๐ฟ ๐ , ๐ข )
18
sru
โ ๐ โ 1 phases
19
s u
โช Time complexity:
20
BELLMAN-FORD(G, w, s)
INITIALIZATION(G, s)
for i = 1 to |G.V| - 1
for (u, v) in G.E
RELAX(u, v, w)
INITIALIZATION(G, s)
for v in G.V
v.d = โ
v.ฯ = NIL
s.d = 0
RELAX(u, v, w)
if v.d > u.d + w(u, v)
// DECREASE-KEY
v.d = u.d + w(u, v)
v.ฯ = u
How to do if there is a negative cycle in the graph?
โช Q: How do we know ๐บ has negative cycles?
โช A: Using another phase of improvement via relaxationโช Run another phase of improving the estimation of ๐ ๐ข for
each vertex ๐ข โ ๐ via relaxing all edges ๐ธ
โช If in the ๐-th phase, there are still some ๐ ๐ข being modified, we know that ๐บ has negative cycles
21
โช Proof by contradictionโช Let ๐ถ be a negative cycle of ๐ nodes ๐ฃ1, ๐ฃ2, โฆ , ๐ฃ๐ (๐ฃ๐+1 = ๐ฃ1)
โช Assume ๐ ๐ฃ๐ for all 1 โค ๐ โค ๐ are not changed in a phase of improvement, then for 1 โค ๐ โค ๐
โช Summing all ๐ inequalities, the sum of edge weights of ๐ถ is nonnegative
22
If there exists a negative cycle in ๐บ, in the ๐-th phase, there are still some ๐ ๐ข being modified.
negative
โช Time complexity:
โช Finding a shortest-path tree of ๐บ:
23
BELLMAN-FORD(G, w, s)
INITIALIZATION(G, s)
for i = 1 to |G.V| - 1
for (u, v) in G.E
RELAX(u, v, w)
for (u, v) in G.E
if v.d > u.d + w(u, v)
return FALSE
return TRUE
INITIALIZATION(G, s)
for v in G.V
v.d = โ
v.ฯ = NIL
s.d = 0
RELAX(u, v, w)
if v.d > u.d + w(u, v)
// DECREASE-KEY
v.d = u.d + w(u, v)
v.ฯ = u
negative cycle detection
Textbook Chapter 24.2 โ Single-source shortest paths in directed acyclic graphs
24
โช Input: a weighted, directed, and acyclic graph ๐บ = ๐, ๐ธand a source vertex ๐
โช Output: a shortest-path distance from ๐ to ๐ก, where ๐ก โ ๐
25No negative cycle!
โช Idea: one phase relaxation
โช Perform a topological sort in linear time on the input DAG
โช For ๐ = 1 to ๐โช Let ๐ฃ๐ be the ๐-th node in the above order
โช Relax each outgoing edge (๐ฃ๐, ๐ข) from ๐ฃ๐
Time complexity:
26
s
โช Assume this is a shortest path from ๐ to ๐ข
โช If we follow the order from topological sort to relax the verticesโ edges, in this shortest path, the left edge must be relaxed before the right edge
โช One phase of improvement is enough
27
s u
Textbook Chapter 24.3 โ Dijkstraโs algorithm
28
โช Input: a non-negative weighted, directed, graph ๐บ = ๐, ๐ธand a source vertex ๐
โช Output: a shortest-path distance from ๐ to ๐ก, where ๐ก โ ๐
29No negative cycle!
โช Idea: BFS finds shortest paths on unweighted graph by expanding the search frontier
โช Initialization
โช Loops for ๐ iterations, where each iteration โช relax outgoing edges of an unprocessed node ๐ข with minimal ๐ ๐ข
โช marks ๐ข as processed
30
31
0
โ โ
โ โ
10
2
3
1
4 69
7
2
5
32
0
10 โ
5 โ
10
2
3
1
4 69
7
2
5
33
0
8 14
5 7
10
2
3
1
4 69
7
2
5
34
0
8 13
5 7
10
2
3
1
4 69
7
2
5
35
0
8 9
5 7
10
2
3
1
4 69
7
2
5
36
0
8 9
5 7
10
2
3
1
4 69
7
2
5
37
0
8 9
5 7
10
2
3
1
4 69
7
2
5
โช Prove by contradictionโช Assume ๐ข is the first vertex for
being processed (minimal distance)
โช Let a shortest path ๐ from ๐ to ๐ข,
โช ๐ฅ is the last vertex in ๐ from ๐
โช ๐ฆ is the first vertex in ๐ not from ๐
โช ๐ ๐ฆ = ๐ฟ ๐ , ๐ฆ because ๐ฅ, ๐ฆ is relaxed when putting ๐ฅ into ๐
โช ๐ฆ should be processed before ๐ข, contradiction. 38
s
x y
u
processed nodes ๐
The vertex selected by Dijkstraโs algorithm into the processed set must precise estimation of its shortest path distance.
The first node
a shortest path from ๐ to ๐ข
โช Min-priority queueโช INSERT:
โช EXTRACT-MIN:
โช DECREASE-KEY:
โช Total complexity:
39
DIJKSTRA(G, w, s)
INITIALIZATION(G, s)
S = empty
Q = G.v // INSERT
while Q โ empty
u = EXTRACT-MIN(Q)
S = Sโช{u}for v in G.adj[u]
RELAX(u, v, w)
INITIALIZATION(G, s)
for v in G.V
v.d = โ
v.ฯ = NIL
s.d = 0
RELAX(u, v, w)
if v.d > u.d + w(u, v)
// DECREASE-KEY
v.d = u.d + w(u, v)
v.ฯ = u
โช Fibonacci heap (Textbook Ch. 19)โช BUILD-MIN-HEAP:
โช EXTRACT-MIN: (amortized)
โช DECREASE-KEY: (amortized)
โช Total complexity:
40
DIJKSTRA(G, w, s)
INITIALIZATION(G, s)
S = empty
Q = G.v // INSERT
while Q โ empty
u = EXTRACT-MIN(Q)
S = Sโช{u}for v in G.adj[u]
RELAX(u, v, w)
INITIALIZATION(G, s)
for v in G.V
v.d = โ
v.ฯ = NIL
s.d = 0
RELAX(u, v, w)
if v.d > u.d + w(u, v)
// DECREASE-KEY
v.d = u.d + w(u, v)
v.ฯ = u
โช Single-Source Shortest Pathsโช Bellman-Ford Algorithm (general graph and weights)
โช and detecting negative cycles
โช Lawler Algorithm (acyclic graph)
โช
โช Dijkstra Algorithm (non-negative weights)
โช with Fibonacci heap
41
Course Website: http://ada.miulab.tw
Email: ada-ta@csie.ntu.edu.tw
42
Important announcement will be sent to @ntu.edu.tw mailbox & post to the course website
Recommended