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

Post on 16-Dec-2020

7 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

黄 潤和

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

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

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

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

CS473 LectureX 6

Bellman-Ford Algorithm Example

5

∞ ∞

∞ ∞

0 s

z y

6

7

8 -3

7 2

9

-2 x t

-4

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

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

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

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

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:

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

13

14

Compute matrix multiplication (1)

similar?

15

Standard algorithm for multiplication

16

Improved algorithm for multiplication

17

All-pairs shortest path problem description

18

Compute matrix multiplication (2)

Is it good?

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

?

19

Proof of Claim

Notice that

akj

20

Compute matrix multiplication (1)

similar?

21

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

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!

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.

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

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 .

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

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

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

28 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) = 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

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

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

32 CMSC 251

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

33 CMSC 251

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

5 2

12

34

35

The Floyd-Warshall Algorithm – Pseudo-code

36

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

37

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

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

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

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

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

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

top related