27
アルゴリズムの設計と解析 潤和、佐藤 温(TA2012.4

Analysis of Algorithms...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:

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Analysis of Algorithms...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:

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

黄 潤和、佐藤 温(TA) 2012.4~

Page 2: Analysis of Algorithms...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:

Contents (L10 – All-pairs Shortest Path) • All-pairs shortest paths • Matrix-multiplication algorithm • Floyd-Warshall algorithm

2

Page 3: Analysis of Algorithms...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:

3

All-pairs shortest paths • Nonnegative edge weights � Dijkstra’s algorithm? |V| times: O(VE + V2 lg V)

二分ヒープとは、二分木を使って作られるヒープ(データ構造)の特に単純な種類のひとつである

フィボナッチヒープはminimum-heap propertyを満足する木の集まりである

Page 4: Analysis of Algorithms...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:

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 in general

Page 5: Analysis of Algorithms...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:

5

Problem description

Page 6: Analysis of Algorithms...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:

6

Proof of Claim

Notice that

Page 7: Analysis of Algorithms...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:

7

Compute matrix multiplication (1)

?

similar?

Page 8: Analysis of Algorithms...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:

8

Compute matrix multiplication (2)

Is it good?

n times of matrix multiplication, right?

?

Page 9: Analysis of Algorithms...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:

9

Review D&C algorithm for multiplication

Improved multiplication

Page 10: Analysis of Algorithms...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:

10

How to detect negative-weight cycle?

the diagonal: its value is 0, ? its is negative value, ?

negative-weight cycle

Page 11: Analysis of Algorithms...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:

11

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 12: Analysis of Algorithms...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:

12

Input repreentation: 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= ∞ 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 13: Analysis of Algorithms...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:

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.

13

Page 14: Analysis of Algorithms...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:

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

14

dij(1) dij

(2) dij(k) dij

(…)

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

(n) Also, dij

(0) = aij .

Page 15: Analysis of Algorithms...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:

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}.

15 By Andreas Klappenecker

Page 16: Analysis of Algorithms...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:

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 17: Analysis of Algorithms...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:

Recursive Formulation

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

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

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

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

17 By Andreas Klappenecker

Page 18: Analysis of Algorithms...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:

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) = min{dij(k-1) , dik

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

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

18

By Andreas Klappenecker

do if dij(k-1)>dik

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

then dij

(k-1)dij dik(k-1)+djk

(k-1)

relaxation

Page 19: Analysis of Algorithms...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:

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.

19 By Andreas Klappenecker

Page 20: Analysis of Algorithms...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:

20 CMSC 251

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

Page 21: Analysis of Algorithms...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:

21 CMSC 251

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

Page 22: Analysis of Algorithms...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:

22

The Floyd-Warshall Algorithm – Pseudo-code

Page 23: Analysis of Algorithms...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:

23

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

Page 24: Analysis of Algorithms...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:

24

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

Page 25: Analysis of Algorithms...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:

25

Page 26: Analysis of Algorithms...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:

26

Visualization of the Floyd-Warshall Algorithm http://www.pms.ifi.lmu.de/lehre/compgeometry/Gosper/shortest_path/shortest_path.html#visualization

Page 27: Analysis of Algorithms...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:

27

Exercises 10

EX 10.2 The Floyd-Warshall algorithm requires O(n3) space, since we compute dij

(k) for i, j, k = 1, 2, …, n (in slide p18). Show that the procedure in the right side, which simply drops all the superscripts (dij

(k) dij), is correct, and thus only O(n2) space is required.

EX 10.1 What does the matrix below used in the shortest-paths algorithms correspond to in regular matrix multiplication? Floyd-Warshall(W)

n = # of rows of W; D = W; for k = 1 to n do for i = 1 to n do for j = 1 to n do dij = min{dij, dik + dkj}; end-do; end-do; end-do; return D;