analysis of algorithms...output: n × n matrix of shortest-path lengths δ(i, j) for all i, j ∈ v....
TRANSCRIPT
アルゴリズムの設計と解析
黄 潤和、佐藤 温(TA) 2012.4~
Contents (L10 – All-pairs Shortest Path) • All-pairs shortest paths • Matrix-multiplication algorithm • Floyd-Warshall algorithm
2
3
All-pairs shortest paths • Nonnegative edge weights � Dijkstra’s algorithm? |V| times: O(VE + V2 lg V)
二分ヒープとは、二分木を使って作られるヒープ(データ構造)の特に単純な種類のひとつである
フィボナッチヒープはminimum-heap propertyを満足する木の集まりである
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
5
Problem description
6
Proof of Claim
Notice that
7
Compute matrix multiplication (1)
?
similar?
8
Compute matrix multiplication (2)
Is it good?
n times of matrix multiplication, right?
?
9
Review D&C algorithm for multiplication
Improved multiplication
10
How to detect negative-weight cycle?
the diagonal: its value is 0, ? its is negative value, ?
negative-weight cycle
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!
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.
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
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 .
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
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)
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
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
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
20 CMSC 251
An example: http://www.cs.umd.edu/~meesh/351/mount/lectures/lect24-floyd-warshall.pdf
21 CMSC 251
An example: take a look d3,2(k)
22
The Floyd-Warshall Algorithm – Pseudo-code
23
The Floyd-Warshall Algorithm in Java (1) http://www.seas.gwu.edu/~simhaweb/cs151/lectures/module9/examples/FloydWarshall.java
24
The Floyd-Warshall Algorithm in Java (2) http://algs4.cs.princeton.edu/44sp/FloydWarshall.java.html
25
26
Visualization of the Floyd-Warshall Algorithm http://www.pms.ifi.lmu.de/lehre/compgeometry/Gosper/shortest_path/shortest_path.html#visualization
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;