algorithms design & analysis - fudan universityliy/courses/alg/slides/alg-08.pdf · • example...

40
Algorithms Design & Analysis Dynamic Programming

Upload: hakhanh

Post on 08-Mar-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

Algorithms Design & Analysis

Dynamic Programming

Page 2: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

2

Recap

•  Divide-and-conquer design paradigm

Page 3: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

3

Today’s topics •  Dynamic programming

– Design paradigm – Assembly-line scheduling – Matrix-chain multiplication – Elements

Page 4: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

4

Decision Problem

•  Definition The answer of the problem is simply “yes” or “no”.

•  Example – Binary search – Reachability

Page 5: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

5

Optimization Problem

•  Definition Each feasible solution has an associated value, and we wish to find the feasible solution with the best value.

•  Example – Shortest path – Closest-pair problem

Page 6: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

6

a1,1 a1,2 a1,3 a1,4 a1,n-1 a1,n

a2,1 a2,2 a2,3 a2,4 a2,n-1 a2,n

t1,1 t1,n-1 t1,2 t1,3 e1

t2,1 t2,n-1 t2,2 t2,3 e2

x1

x2 …

chassis enters

completed auto exits

line 2

line 1

station S2,1

station S2,2

station S2,3

station S2,4

station S2,n-1

station S2,n

station S1,1

station S1,2

station S1,3

station S1,4

station S1,n-1

station S1,n

Assembly-line scheduling •  Manufacturing problem

ai,j: assembly time ti,j: transfer time ei: enter time xi: exit time

Page 7: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

7

Example

7 9 3 4 8 4

8 5 6 4 5 7

2 4 3 1 2

2 1 1 2 4

3

2

chassis enters

completed auto exits

line 2

line 1

station S2,1

station S2,2

station S2,3

station S2,4

station S2,5

station S2,6

station S1,1

station S1,2

station S1,3

station S1,4

station S1,5

station S1,6

3

2

37 30 25 22 16 12 f2[j]

35 32 24 20 18 9 f1[j]

6 5 4 3 2 1 j

2 2 1 2 1 l2[j]

2 1 1 2 1 l1[j]

6 5 4 3 2 j

f * = 38 l * = 1

Page 8: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

8

Brute-force algorithm

Brute-force algorithm: Exhaustively checking all possible lists.

running time = Ω(2n) It is too unlucky!

IDEA: If a list is given of which stations to use in line 1 and which to use in line 2, it is easy to compute in Θ(n) time how long it takes to pass through the factory.

There are 2 choices for each station!

Page 9: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

9

Optimal structure

Observation: The fastest way through station S1,j is either 1.  The fastest way through station S1,j-1 and then

directly through station S1,j. 2.  The fastest way through station S2,j-1, a transfer

from line 2 to line 1, and then through station S1,j.

•  To find the fastest way through station j of either line, we solve the subproblem of finding the fastest ways through station j - 1 on both lines.

Page 10: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

10

Recursive solution

•  For the first station, f1[1] = e1 + a1,1, f2[1] = e2 + a2,1

•  For the final station, f* = min(f1[n] + x1, f2[n] + x2)

•  The recurrence

{⎩⎨⎧

≥++−+−

=+=

− .2 if }]1[,]1[min.1 if

][,11,22,11

1,111 jatjfajf

jaejf

jjj

{⎩⎨⎧

≥++−+−

=+=

− .2 if }]1[,]1[min.1 if

][,21,11,22

1,222 jatjfajf

jaejf

jjj

Page 11: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

11

Find the fastest way FASTEST-WAY(a, t, e, x, n) f1[1] ← e1 + a1,1, f2[1] ← e2 + a2,1

for j ← 2 to n do if f1[j-1] + a1,j ≤ f1[j-1] +t2,j-1 + a1,j

then f1[j] ← f1[j-1] + a1,j; l1[j] ← 1 else f1[j] ← f2[j-1] +t2,j-1 + a1,j; l1[j] ← 2 if f2[j-1] + a2,j ≤ f2[j-1] +t1,j-1 + a2,j

then f2[j] ← f2[j-1] + a2,j ; l2[j] ← 2 else f2[j] ← f1[j-1] +t1,j-1 + a2,j; l2[j] ← 1

if f1[n] + x1, ≤ f2[n] + x2 then f * ← f1[n] + x1 l* ← 1 else f* ← f2[n] + x2

l * ← 2

Page 12: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

12

Example

7 9 3 4 8 4

8 5 6 4 5 7

2 4 3 1 2

2 1 1 2 4

3

2

chassis enters

completed auto exits

line 2

line 1

station S2,1

station S2,2

station S2,3

station S2,4

station S2,5

station S2,6

station S1,1

station S1,2

station S1,3

station S1,4

station S1,5

station S1,6

3

2

37 30 25 22 16 12 f2[j]

35 32 24 20 18 9 f1[j]

6 5 4 3 2 1 j

2 2 1 2 1 l2[j]

2 1 1 2 1 l1[j]

6 5 4 3 2 j

f * = 38 l * = 1

Page 13: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

13

Design paradigm

•  Characterize the structure of an optimal solution.

•  Define the value of an optimal solution recursively.

•  Compute the value of an optimal solution in a bottom-up fashion.

•  Construct an optimal solution from computed information.

Page 14: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

14

Matrix-chain multiplication

Input: a sequence of n matrices A1, A2, …, An

Output: a matrix B = A1×A2×…×An

Example: Bm,s = Am,n×An,o×…×Ar,s

Page 15: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

15

Multiplication of two matrices MATRIX-MULTIPLY(A,B)

if columns[A] ≠ rows[B] then error "incompatible dimensions" else for i ← 1 to rows[A] do for j ← 1 to columns[B] do C[i, j] ← 0

for k ← 1 to columns[A] do C[i ,j] ← C[i ,j] + A [i, k] · B[k, j]

return C

Suppose A is a p×q matrix, B is a q×r matrix Running time = Θ(p×q×r)

Page 16: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

16

Naïve algorithm

Naïve Algorithm: Multiply the matrices from left to right.

•  Suppose the size of the n matrice are p1×p2, p2×p3 ,…, pn-1×pn

•  Generalization of two matrice’s case Running time = Ο(p1×p2×…×pn)

Any better way to reduce the numbers of scalar multiplication???

Page 17: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

17

Matrix-chain: Example •  4 matrices: A1×A2×A3×A4

A1: 15×5 A2: 5×10 A3: 10×20 A4: 20×25

Associative law: (A1×A2)×A3 = A1×(A2×A3)

((A1×A2)×A3)×A4 : 15·5·10+15·10·20+15·20·25=11250 (A1×A2)×(A3×A4) : 15·5·10+10·20·15+15·10·25=13250 (A1×(A2×A3))×A4 : 5·10·20+15·5·20+15·20·25=10000 A1×((A2×A3)×A4) : 5·10·20+5·20·25+15·5·25=5375 A1×(A2×(A3×A4)) : 10·20·25+15·10·25+15·5·25=8125

Minimal number of multiplications!

Page 18: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

18

Brute-force algorithm

Brute-force algorithm: Exhausitively checking all possible parenthesization.

What’s the running time?

IDEA: Find a parenthesization that minimizes the number of scalar multiplications for n matrices < A1, …, An> with dimensions pi-1×pi, for 1 ≤ i ≤ n.

Depends on the number of parenthesization!

Page 19: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

19

Number of parenthesization •  The recurrence of the number of parenthesization

–  For a single matrix, we have only one. –  For a sequence of n matrices, split it between the kth and

(k+1)st matrices and parenthesize the subsequences recursively.

⎪⎩

⎪⎨⎧

≥−

== ∑

=

1

1.2 if )()(.1 if 1

)( n

knknPkPn

nP

)4(2

11)(),1()( 2/3nn

nn

nCnCnPn

Ω=⎟⎟⎠

⎞⎜⎜⎝

+=−=

Unlucky! It is exponential in n…

C(n) is the Catalan number!

Page 20: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

20

More clever way… Notation: Ai…j means Ai Ai+1…Aj , where i ≤ j

•  Given Ai…j with an optimal parenthesization There must be a i ≤ k < j such that Ai…j = (Ai…k)·(Ak+1…j).

•  Cost of computing Ai…j – Cost of computing Ai…k and Ak+1…j

– Cost of computing (Ai…k)·(Ak+1…j)

Page 21: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

21

More clever way…(Cont.)

Assertion: Suppose that an optimal parethesization of Ai…j is split at k. Then the parethesization subchain Ai…k within this optimization of Ai…j must be an optimal parenthesization of Ai…k.

Why?

Page 22: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

22

Recursive solution Notation: m[i,j] be the minimum number of scalar multiplications to compute Ai…j

•  If there is one matrix Ai: m[i,i] = 0 •  Suppose optimal parenthesization of Ai…j is k

m[i,j] = m[i,k] + m[k+1,j] + pi-1pkpj

•  Finally,

m[i, j]=0 if i = j.mini≤k< j

m[i, j]=m[i,k]+m[k +1, j]+ pi−1pk pj} if i < j.{

#

$%

&%

Page 23: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

23

Recursive Algorithm RECURSIVE-MATRIX-CHAIN(p,i,j)

if i = j then return 0 m[i, j] ← ∞ for k ← i to j - 1 //computing the minimum cost do q ← RECURSIVE-MATRIX-CHAIN(p, i, k) + RECURSIVE-MATRIX-CHAIN(p, k + 1, j)

+ pi-1pkpj if q < m[i, j] then m[i, j] ← q return m[i, j]

p = <p1, p2,.., pn >

Page 24: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

24

The great moment…

•  The recurrence

Theorem. The running time satisfies

T(n) ≥ 2n-1

Exponetial time, it is too bad!

∑−

=

>+−++≥

≥1

1.1for )1)()((1)(

,1)1(n

knknTkTnT

T

Page 25: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

25

The great moment…(Cont.) Proof.(Induction)

The base, T(1)≥1 = 20. Suppose T(n-1) ≥ 2n-2.

when i = n, we have

∑−

=

+≥1

1

)(2)(n

iniTnT

∑−

=

− +=1

1

122n

i

i n

∑−

=

+=2

0

22n

i

i n

11 222)12(2 −− ≥−+=+−= nnn nn

Page 26: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

26

It is totally frustrated!

We need to find another better way!

Page 27: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

27

Bottom-up method MATRIX-CHAIN-ORDER(p)

n ← length[p] – 1 for i ← 1 to n do m[i, i] ← 0 // initialize the entries for l ← 2 to n // l is the chain length do for i ← 1 to n – l + 1

do j ← i + l – 1 // j is i + length of chain -1 m[i, j] ← ∞ for k ← i to j – 1 //based on previous result

do q ← m[i, k] + m[k + 1, j] + pi–1 pk pj if q < m[i, j] // choose the minimal

then m[i, j] ← q s[i, j] ← k

return m and s

Page 28: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

28

The truly great moment…

•  Correctness? •  Space complexity

T(n) = Θ(n2) – m[1…n, 1…n]: store the cost m[i,j] –  s[1…n,1…n]: records the index k achieved

optimal cost in computing m[i,j] •  Time complexity

T(n) = Θ(n3)

Page 29: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

29

Example •  Given matrices

A1: 30×35 A2: 35×15 A3: 15×5 A4: 5×10 A5: 10×20 A6: 20×25

Page 30: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

30

Example(Cont.) •  Computing of m[2,5]

7125

.1137520103504375]5,5[]4,2[.71252053510002625]5,4[]3,2[

.1300020153525000]5,3[]2,2[min]5,2[

541

531

521

=

⎪⎩

⎪⎨

=⋅⋅++=++

=⋅⋅++=++

=⋅⋅++=++

=

pppmmpppmmpppmm

m

Page 31: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

31

Constructing the optimal solution

MATRIX-CHAIN-MULTIPLY(A, s, i, j) if j >i

then X ← MATRIX-CHAIN-MULTIPLY(A, s, i, s[i, j])

Y ← MATRIX-CHAIN-MULTIPLY(A, s, s[i, j] + 1, j) return MATRIX-MULTIPLY(X, Y)

else return A[i] •  It is a recursive algorithm

Page 32: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

32

Elements of dynamic programming

•  Optimal substructure – The optimal solution for a problem consists of

optimal solutions of subproblems. •  Overlapping subproblems

– Solving a subproblem leads to same subproblems over and over.

Page 33: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

33

Pattern to discover optimal substructure

•  Solution to the problem depends on one or more subproblems to be solved.

•  Assume that the optimal solution has been given. •  Determine ensued subproblems and characterize its

resulting space best. •  Show the solutions to the subproblems used within

the optimal solution must themselves be optimal by using a "cut-and-paste" technique.

Page 34: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

34

Overlapping subproblems

•  The recursion tree for the computation of RECURSIVE-MATRIX-CHAIN(p)

Page 35: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

35

Memoized recursive algorithm •  Bottom-up v.s. top-down •  Memoize the intermediate result encountered first. •  Memoized method

MEMOIZED-MATRIX-CHAIN(p) n ← length[p] - 1

for i ← 1 to n

do for j ← i to n

do m[i, j] ← ∞ return LOOKUP-CHAIN(p, 1, n)

Page 36: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

36

Memoized recursive algorithm(Cont.)

LOOKUP-CHAIN(p, i, j) //lookup while computing if m[i,j] < ∞ then return m[i, j] if i = j then m[i, j] ← 0 else for k ← i to j - 1 do q ← LOOKUP-CHAIN(p, i, k)

+ LOOKUP-CHAIN(p, k+1, j) + pi - 1pkpj if q < m[i, j] then m[i, j] ← q return m[i, j]

Page 37: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

37

Memoized recursive algorithm(Cont.)

•  Running time T(n) = O(n3)

•  Memoization v.s. bottom-up – Bottom-up wins: subproblem must be solved at

least once. – Top-down wins: if some subproblems need not

be solved.

Page 38: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

38

Dynamic v.s. Divide-and-conquer

•  Divide-and-conquer algorithms partition the problem into independent subproblems, solve the subproblems recursively, and the combine the solutions to solve the original problem.

•  Dynamic programming is applicable when the subproblems are not independent. Every subproblem is solved only once and the result sorted in a table for avoiding the work of recomputing it.

Page 39: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

39

Dynamic v.s. Divide-and-conquer

•  A consequence is that there must be only relatively few subproblems for the table to be efficiently computable. Under such circumstances dynamic programming allows an exponential-time algorithm to be transformed to a polynomial-time algorithm.

•  The name dynamic programming is historic. It refers to computing the table.

Page 40: Algorithms Design & Analysis - Fudan Universityliy/courses/alg/slides/alg-08.pdf · • Example – Shortest path ... Assembly-line scheduling • Manufacturing problem a i,j:

40

Next week

•  Greedy algorithm