analysis of algorithms - hosei...16/7/17 18時5 分 shortest path 5 • the bellman–ford algorithm...

43
アルゴリズムの設計と解析 潤和

Upload: others

Post on 16-Dec-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

アルゴリズムの設計と解析

黄 潤和

Page 2: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

Contents (L14 –15 All-pairs Shortest Path) • All-pairs shortest paths • Floyd-Warshall algorithm • Matrix-multiplication algorithm • Final review MIT video lecture http://videolectures.net/mit6046jf05_demaine_lec19/

2

Page 3: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

3

All-pairs shortest paths Run Dijkstra’s algorithm for each vertex

• Nonnegative edge weights � Dijkstra’s algorithm? O(E + V lg2 V) |V| times: O(VE + V2 lg2 V)

in the case of all pairs

Page 4: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

4

• General (including negative weight) � Input: Digraph G = (V, E), where V = {1, 2, …, n}, with edge-weight function w : E → R. Output: n × n matrix of shortest-path lengths δ(i, j) for all i, j ∈ V. • Time Complexity?

• Single source shortest paths in general � Bellman-Ford: O(VE)

• all-pairs shortest paths? • IDEA: run Bellman-Ford once from each vertex. Time = O(V2E).

All-pairs shortest paths

Page 5: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

16/7/17 18時5分 Shortest Path 5

• The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph.

• It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers.

Bellman-Ford algorithm Versus Dijkstra’s algorithm

Page 6: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

CS473 LectureX 6

Bellman-Ford Algorithm Example

5

∞ ∞

∞ ∞

0 s

z y

6

7

8 -3

7 2

9

-2 x t

-4

Page 7: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

CS473 LectureX 7

Bellman-Ford Algorithm Example

6 ∞

7 ∞

0 s

z y

6

7

8 -3

7 2

9

-2 x t

-4

5

Page 8: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

CS473 LectureX 8

Bellman-Ford Algorithm Example

6 4

7 2

0 s

z y

6

7

8 -3

7 2

9

-2 x t

-4

5

Page 9: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

CS473 LectureX 9

Bellman-Ford Algorithm Example

2 4

7 2

0 s

z y

6

7

8 -3

7 2

9

-2 x t

-4

5

Page 10: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

CS473 LectureX 10

Bellman-Ford Algorithm Example

2 4

7 −2

0 s

z y

6

7

8 -3

7 2

9

-2 x t

-4

5

Page 11: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

11

Dijkstra, from node 1 1->2, 1->4->2, (8 3) 1->3, 14->2->3, (-, 4) 1->4, (1) 3

4 Dijkstra, from node 2 2->1, 2->3->1, (-, 5) 2->3, (1) 2->4, 2->1->4, (-, 6)

5

6

Dijkstra, from node 3 3->1, (4) 3->2, 3->1->2, (-, 7) 3->4, 3->1->4, (-, 5)

7 5

Dijkstra, from node 4 4->1, 4->2->1 (-, 7) 4->2, (2) 4->3, 4->2->3, (93)

7

3

4 4 4

方法1:

Page 12: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

12

cij = aik + bkj ikj 7(3,2) =5 (3,4) + 2(4,2) (行、列)

方法2: 観察から

312 (4+8=12) 314(4+1=5) 1経由

123 (8+1=9) 2経由

231(1+4=5) 431(3+4=7) 234(1+5=6) 3経由

行列の操作で All-pair shortest path

Page 13: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

13

Page 14: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

14

Compute matrix multiplication (1)

similar?

Page 15: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

15

Standard algorithm for multiplication

Page 16: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

16

Improved algorithm for multiplication

Page 17: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

17

All-pairs shortest path problem description

Page 18: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

18

Compute matrix multiplication (2)

Is it good?

n times of matrix multiplication (θ(n3)), right?

?

Page 19: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

19

Proof of Claim

Notice that

akj

Page 20: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

20

Compute matrix multiplication (1)

similar?

Page 21: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

21

The Floyd-Warshall Algorithm in Java (1) http://www.seas.gwu.edu/~simhaweb/cs151/lectures/module9/examples/FloydWarshall.java

Page 22: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

22

dynamic programming is a method for solving complex problems by breaking them down into simpler sub-problems. It is applicable to problems exhibiting the properties of overlapping sub-problems which are only slightly smaller and optimal substructure (described below). When applicable, the method takes far less time than naive methods. e. g. Recursive algorithm Divide and conquer Dijkstra algorithm

Floyd-Warshall algorithm Also dynamic programming, but faster!

Page 23: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

23

Input representation: We assume that we have a weight matrix W= (wij) (i,j) in E wij= 0 if i=j wij= w(i,j) if i≠j and (i,j) in E (has edge from i to j)

wij= ∞ or ? if i≠j and (i,j) not in E (no edge from i to j)

Input and output

By Andreas Klappenecker

Output representation: If the graph has n vertices, we return a distance matrix (dij), Where, dij the length of the path from i to j.

Page 24: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

Intermediate Vertices Without loss of generality, we will assume that V={1,2,…,n}, i.e., that the vertices of the graph are numbered from 1 to n. Given a path p=(v1, v2,…, vm) in the graph, we will call the vertices vk with index k in {2,…,m-1} the intermediate vertices of p.

24

Page 25: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

Key Definition The key to the Floyd-Warshall algorithm is the following definition: Let dij

(k) denote the length of the shortest path from i to j such that all intermediate vertices are contained in the set {1,…,k}. We have the following remark

25

dij(1) dij

(2) dij(k) dij

(…)

Thus, the shortest path δ(i, j) = dij

(n) Also, dij

(0) = aij .

Page 26: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

More remark Consider a shortest path p from i to j such that the intermediate vertices are from the set {1,…,k}. • If the vertex k is not an intermediate vertex

on p, then dij(k) = dij

(k-1)

• If the vertex k is an intermediate vertex on p, then dij

(k) = dik(k-1) + dkj

(k-1) Interestingly, in either case, the sub-paths contain merely nodes from {1,…,k-1}.

26 By Andreas Klappenecker

Page 27: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

Conclusion Therefore, we can conclude that dij

(k) = min{dij(k-1) , dik

(k-1) + dkj(k-1)}

dik(k-1) dkj

(k-1)

dij(k-1)

Page 28: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

Recursive Formulation

If we do not use intermediate nodes, i.e., when k=0, then dij

(0) = wij If k>0, then dij

(k) = mink{dij(k-1) , dik

(k-1) + dkj(k-1)}

28 By Andreas Klappenecker

Page 29: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

The Floyd-Warshall Algorithm Floyd-Warshall(W) n = # of rows of W; D(0) = W; for k = 1 to n do for i = 1 to n do for j = 1 to n do dij

(k) = mink{dij(k-1) , dik

(k-1) + dkj(k-1)};

end-do; end-do; end-do; return D(n);

29

By Andreas Klappenecker

do if dij(k-1)>dik

(k-1)+djk(k-1)

then dij

(k) dik(k-1)+djk

(k-1)

relaxation

Page 30: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

Time and Space Requirements • The running time is obviously O(n3). • However, in this version, the space

requirements are high. • One can reduce the space from O(n3) to

O(n2) by using a single array d.

30 By Andreas Klappenecker

Page 31: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

31 CMSC 251

An example: http://www.cs.umd.edu/~meesh/351/mount/lectures/lect24-floyd-warshall.pdf

Work in class: Please continue to get the final updated graph and matrix without see the previous page (p24).

Page 32: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

32 CMSC 251

An example: http://www.cs.umd.edu/~meesh/351/mount/lectures/lect24-floyd-warshall.pdf

Page 33: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

33 CMSC 251

An example: take a look d3,2(k)

5 2

12

Page 34: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

34

Page 35: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

35

The Floyd-Warshall Algorithm – Pseudo-code

Page 36: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

36

The Floyd-Warshall Algorithm in Java (1) http://www.seas.gwu.edu/~simhaweb/cs151/lectures/module9/examples/FloydWarshall.java

Page 37: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

37

The Floyd-Warshall Algorithm in Java (2) http://algs4.cs.princeton.edu/44sp/FloydWarshall.java.html

Page 38: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

16/7/17 20時17分 Shortest Path 38

0 1 -3 2 -4 3 0 -4 1 -1 7 4 0 5 3 2 -1 -5 0 -2 8 5 1 6 0

0 3 8 ? -4 ? 0 ? 1 7 ? 4 0 ? ? 2 ? -5 0 ? ? ? ? 6 0

FloydWarshall in Java

Page 39: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

16/7/17 18時55分 Shortest Path 39

Page 40: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

40

Visualization of the Floyd-Warshall Algorithm http://www.pms.ifi.lmu.de/lehre/compgeometry/Gosper/shortest_path/shortest_path.html#visualization https://www.cs.usfca.edu/~galles/visualization/Floyd.html

Page 41: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

16/7/17 19時1分 Shortest Path 41

Page 43: Analysis of Algorithms - Hosei...16/7/17 18時5 分 Shortest Path 5 • The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all

16/7/17 18時5分 Shortest Path 43

Ex14-1 Solve the all-pairs shortest-path problem using Floyd’s algorithm for the following digraph