Slides credited from Hsueh-I Lu & Hsu-Chun Hsiaoyvchen/f107-ada/doc/...ย ยท Input: a weighted,...

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