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

Post on 08-Mar-2018

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Algorithms Design & Analysis

Dynamic Programming

2

Recap

•  Divide-and-conquer design paradigm

3

Today’s topics •  Dynamic programming

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

4

Decision Problem

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

•  Example – Binary search – Reachability

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

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

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

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!

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.

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

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

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

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.

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

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)

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???

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!

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!

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!

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)

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?

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

#

$%

&%

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 >

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

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

26

It is totally frustrated!

We need to find another better way!

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

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)

29

Example •  Given matrices

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

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

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

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.

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.

34

Overlapping subproblems

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

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)

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]

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.

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.

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.

40

Next week

•  Greedy algorithm

top related