823

Introduction CSCE423/823 Introduction Rod Cutting Matrix-Chain Multiplication CSCE423/823 Computer Science & Engineering 423/823 Design and Analysi...
Author: Byron Fowler
7 downloads 0 Views 686KB Size
Introduction CSCE423/823

Introduction Rod Cutting Matrix-Chain Multiplication

CSCE423/823

Computer Science & Engineering 423/823 Design and Analysis of Algorithms Lecture 09 — Dynamic Programming (Chapter 15)

Introduction Rod Cutting Matrix-Chain Multiplication Longest Common Subsequence

Longest Common Subsequence Optimal Binary Search Trees

Stephen Scott (Adapted from Vinodchandran N. Variyam)

Optimal Binary Search Trees

Dynamic programming is a technique for solving optimization problems Key element: Decompose a problem into subproblems, solve them recursively, and then combine the solutions into a final (optimal) solution Important component: There are typically an exponential number of subproblems to solve, but many of them overlap ⇒ Can re-use the solutions rather than re-solving them Number of distinct subproblems is polynomial

Spring 2010 1 / 41

2 / 41

[email protected]

Rod Cutting

Example: Rod Cutting (2)

CSCE423/823

Introduction Rod Cutting Recursive Algorithm Dynamic Programming Algorithm Reconstructing a Solution

Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees

CSCE423/823

A company has a rod of length n and wants to cut it into smaller rods to maximize profit Have a table telling how much they get for rods of various lengths: A rod of length i has price pi The cuts themselves are free, so profit is based solely on the prices charged for of the rods If cuts only occur at integral boundaries 1, 2, . . . , n − 1, then can make or not make a cut at each of n − 1 positions, so total number of possible solutions is 2n−1

3 / 41

Introduction

Introduction Rod Cutting Recursive Algorithm Dynamic Programming Algorithm Reconstructing a Solution

Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees

2 5

3 8

4 9

5 10

6 17

7 17

8 20

9 24

10 30

Recursive Algorithm Dynamic Programming Algorithm Reconstructing a Solution

Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees

4 / 41

Recursive Algorithm

Given a rod of length n, want to find a set of cuts into lengths i1 , . . . , ik (where i1 + · · · + ik = n) and rn = pi1 + · · · + pik is maximized For a specific value of n, can either make no cuts (revenue = pn ) or make a cut at some position i, then optimally solve the problem for lengths i and n − i: rn = max (pn , r1 + rn−1 , r2 + rn−2 , . . . , ri + rn−i , . . . , rn−1 + r1 )

Notice that this problem has the optimal substructure property, in that an optimal solution is made up of optimal solutions to subproblems Can find optimal solution if we consider all possible subproblems

Alternative formulation: Don’t further cut the first segment:

CSCE423/823

Introduction

1

Rod Cutting

2

Recursive Algorithm Dynamic Programming Algorithm Reconstructing a Solution

Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees

rn = max (pi + rn−i ) 5 / 41

1 1

Rod Cutting

Example: Rod Cutting (3) CSCE423/823

i pi

1≤i≤n

3 4 5 6 7

if n == 0 then return 0 q = −∞ for i = 1 to n do q = max (q, p[i] + Cut-Rod(p, n − i)) end return q Algorithm 1: Cut-Rod(p, n)

What is the time complexity? 6 / 41

Time Complexity CSCE423/823

Time Complexity (2) CSCE423/823

Let T (n) be number of calls to Cut-Rod Thus T (0) = 1 and, based on the for loop,

Introduction

Introduction

Rod Cutting Recursive Algorithm Dynamic Programming Algorithm Reconstructing a Solution

Matrix-Chain Multiplication Longest Common Subsequence

T (n) = 1 +

n−1 X

Rod Cutting

T (j) = 2

n

j=0

Why exponential? Cut-Rod exploits the optimal substructure property, but repeats work on these subproblems E.g. if the first call is for n = 4, then there will be: 1 1 2 4 8

Optimal Binary Search Trees

call to Cut-Rod(4) call to Cut-Rod(3) calls to Cut-Rod(2) calls to Cut-Rod(1) calls to Cut-Rod(0)

7 / 41

Recursive Algorithm Dynamic Programming Algorithm Reconstructing a Solution

Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees

8 / 41

Dynamic Programming Algorithm CSCE423/823

Introduction Rod Cutting Recursive Algorithm Dynamic Programming Algorithm Reconstructing a Solution

Matrix-Chain Multiplication Longest Common Subsequence

Top-Down with Memoization CSCE423/823

Can save time dramatically by remembering results from prior calls Two general approaches: 1

2

Top-down with memoization: Run the recursive algorithm as defined earlier, but before recursive call, check to see if the calculation has already been done and memoized Bottom-up: Fill in results for “small” subproblems first, then use these to fill in table for “larger” ones

Typically have the same asymptotic running time

Optimal Binary Search Trees

Introduction Rod Cutting Recursive Algorithm Dynamic Programming Algorithm Reconstructing a Solution

Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees

9 / 41

CSCE423/823

Rod Cutting Recursive Algorithm Dynamic Programming Algorithm Reconstructing a Solution

Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees

11 / 41

1 if r[n] ≥ 0 then 2 return r[n] // r initialized to all −∞ 3 if n == 0 then 4 q=0 5 else 6 q = −∞ 7 for i = 1 to n do 8 q= max (q, p[i] + Memoized-Cut-Rod-Aux(p, n − i, r)) 9 end 10 r[n] = q 11 return q

Algorithm 2: Memoized-Cut-Rod-Aux(p, n, r)

10 / 41

Bottom-Up

Introduction

Recursion Tree for n = 4

Time Complexity CSCE423/823

1 Allocate r[0 . . . n] 2 r[0] = 0 3 for j = 1 to n do 4 q = −∞ 5 for i = 1 to j do 6 q = max (q, p[i] + r[j − i]) 7 end 8 r[j] = q 9 end 10 return r[n]

Algorithm 3: Bottom-Up-Cut-Rod(p, n) First solves for n = 0, then for n = 1 in terms of r[0], then for n = 2 in terms of r[0] and r[1], etc.

Subproblem graph for n = 4 Introduction Rod Cutting Recursive Algorithm Dynamic Programming Algorithm Reconstructing a Solution

Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees

12 / 41

Both algorithms take linear time to solve for each value of n, so total time complexity is Θ(n2 )

Reconstructing a Solution

Reconstructing a Solution (2)

CSCE423/823

CSCE423/823

Introduction

Introduction

Rod Cutting Recursive Algorithm Dynamic Programming Algorithm Reconstructing a Solution

Matrix-Chain Multiplication Longest Common Subsequence

Rod Cutting

If interested in the set of cuts for an optimal solution as well as the revenue it generates, just keep track of the choice made to optimize each subproblem Will add a second array s, which keeps track of the optimal size of the first piece cut in each subproblem

Optimal Binary Search Trees

Recursive Algorithm Dynamic Programming Algorithm Reconstructing a Solution

Matrix-Chain Multiplication

CSCE423/823

1 (r, s) = Extended-Bottom-Up-Cut-Rod(p, n) 2 while n > 0 do 3 print s[n] 4 n = n − s[n]

Introduction Rod Cutting Recursive Algorithm Dynamic Programming Algorithm Reconstructing a Solution

5 end

Algorithm 5: Print-Cut-Rod-Solution(p, n)

Matrix-Chain Multiplication

i 0 1 2 Example: r[i] 0 1 5 s[i] 0 1 2 If n = 10, optimal solution segments of sizes 1 and 6

3 4 8 10 3 2 is no cut;

5 6 13 17 2 6 if n = 7,

7 18 1 then

8 9 10 22 25 30 2 3 10 cut once to get

15 / 41

Introduction Rod Cutting Matrix-Chain Multiplication Characterizing Structure Recursive Definition Computing Optimal Value Constructing Optimal Solution Overalapping Subproblems

Optimal Binary Search Trees 16 / 41

CSCE423/823

Matrix-Chain Multiplication Characterizing Structure Recursive Definition Computing Optimal Value Constructing Optimal Solution Overalapping Subproblems

Longest Common Subsequence Optimal Binary Search Trees 17 / 41

Algorithm Rod(p, n)

4:

Extended-Bottom-Up-Cut-

Brute forcesolution is infeasible, since its time complexity is Ω 4n /n3/2 Will follow 4-step procedure for dynamic programming: 1 2 3 4

Characterize the structure of an optimal solution Recursively define the value of an optimal solution Compute the value of an optimal solution Construct an optimal solution from computed information

1

2

Computing ((A1 A2 )A3 ) requires 10 · 100 · 5 = 5000 steps to compute (A1 A2 ) (yielding a 10 × 5), and then 10 · 5 · 50 = 2500 steps to finish, for a total of 7500 Computing (A1 (A2 A3 )) requires 100 · 5 · 50 = 25000 steps to compute (A2 A3 ) (yielding a 100 × 50), and then 10 · 100 · 50 = 50000 steps to finish, for a total of 75000

Characterizing the Structure of an Optimal Solution CSCE423/823

The matrix-chain multiplication problem is to take a chain hA1 , . . . , An i of n matrices, where matrix i has dimension pi−1 × pi , and fully parenthesize the product A1 · · · An so that the number of scalar multiplications is minimized

Given a chain of matrices hA1 , . . . , An i, goal is to compute their product A1 · · · An This operation is associative, so can sequence the multiplications in multiple ways and get the same result Can cause dramatic changes in number of operations required Multiplying a p × q matrix by a q × r matrix requires pqr steps and yields a p × r matrix for future multiplications E.g. Let A1 be 10 × 100, A2 be 100 × 5, and A3 be 5 × 50

Longest Common Subsequence

Matrix-Chain Multiplication (2)

Rod Cutting

11 end 12 return r, s

Matrix-Chain Multiplication

CSCE423/823

Introduction

end r[j] = q

14 / 41

Reconstructing a Solution (3)

Optimal Binary Search Trees

9 10

Longest Common Subsequence Optimal Binary Search Trees

13 / 41

Longest Common Subsequence

1 Allocate r[0 . . . n] and s[0 . . . n] 2 r[0] = 0 3 for j = 1 to n do 4 q = −∞ 5 for i = 1 to j do 6 if q < p[i] + r[j − i] then 7 q = p[i] + r[j − i] 8 s[j] = i

Introduction Rod Cutting Matrix-Chain Multiplication Characterizing Structure Recursive Definition Computing Optimal Value Constructing Optimal Solution Overalapping Subproblems

Longest Common Subsequence Optimal Binary Search Trees 18 / 41

Let Ai...j be the matrix from the product Ai Ai+1 · · · Aj To compute Ai...j , must split the product and compute Ai...k and Ak+1...j for some integer k, then multiply the two together Cost is the cost of computing each subproduct plus cost of multiplying the two results Say that in an optimal parenthesization, the optimal split for Ai Ai+1 · · · Aj is at k Then in an optimal solution for Ai Ai+1 · · · Aj , the parenthisization of Ai · · · Ak is itself optimal for the subchain Ai · · · Ak (if not, then we could do better for the larger chain) Similar argument for Ak+1 · · · Aj Thus if we make the right choice for k and then optimally solve the subproblems recursively, we’ll end up with an optimal solution Since we don’t know optimal k, we’ll try them all

Recursively Defining the Value of an Optimal Solution CSCE423/823

Introduction Rod Cutting Matrix-Chain Multiplication Characterizing Structure Recursive Definition Computing Optimal Value Constructing Optimal Solution Overalapping Subproblems

Longest Common Subsequence Optimal Binary Search Trees 19 / 41

Define m[i, j] as minimum number of scalar multiplications needed to compute Ai...j (What entry in the m table will be our final answer?) Computing m[i, j]: 1 2

If i = j, then no operations needed and m[i, i] = 0 for all i If i < j and we split at k, then optimal number of operations needed is the optimal number for computing Ai...k and Ak+1...j , plus the number to multiply them: m[i, j] = m[i, k] + m[k + 1, j] + pi−1 pk pj

3

Since we don’t know k, we’ll try all possible values:  0 m[i, j] = mini≤k 0 and xi = yj c[i, j] =  max (c[i, j − 1], c[i − 1, j]) if i, j > 0 and xi 6= yj

Matrix-Chain Multiplication

Characterizing Structure Recursive Definition Computing Optimal Value Constructing Optimal Solution

Optimal Binary Search Trees

30 / 41

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

allocate b[1 . . . m, 1 . . . n] and c[0 . . . m, 0 . . . n] initialize c[i, 0] = 0 and c[0, j] = 0 ∀ 0 ≤ i ≤ m and 0 ≤ j ≤ n for i = 1 to m do for j = 1 to n do if xi == yj then c[i, j] = c[i − 1, j − 1] + 1 b[i, j] = “ - ” else if c[i − 1, j] ≥ c[i, j − 1] then c[i, j] = c[i − 1, j] b[i, j] = “ ↑ ” else

end

c[i, j] = c[i, j − 1] b[i, j] = “ ← ”

end return (c, b)

Algorithm 8: LCS-Length(X, Y, m, n) What is the time complexity?

Constructing an Optimal Solution from Computed Information

Computing the Value of an Optimal Solution (2) CSCE423/823

X = hA, B, C, B, D, A, Bi, Y = hB, D, C, A, B, Ai

CSCE423/823

Introduction

Introduction

Rod Cutting

Rod Cutting

Length of LCS is stored in c[m, n]

Matrix-Chain Multiplication

Matrix-Chain Multiplication

Longest Common Subsequence

Longest Common Subsequence

To print LCS, start at b[m, n] and follow arrows until in row or column 0

Characterizing Structure Recursive Definition Computing Optimal Value Constructing Optimal Solution

Characterizing Structure Recursive Definition Computing Optimal Value Constructing Optimal Solution

Optimal Binary Search Trees

32 / 41

Constructing an Optimal Solution from Computed Information (2) CSCE423/823

Rod Cutting Matrix-Chain Multiplication Longest Common Subsequence Characterizing Structure Recursive Definition Computing Optimal Value Constructing Optimal Solution

Optimal Binary Search Trees

Constructing an Optimal Solution from Computed Information (3) CSCE423/823

if i == 0 or j == 0 then return 3 if b[i, j] == “ - ” then 4 Print-LCS(b, X, i − 1, j − 1) 5 print xi 1

Introduction

2

Rod Cutting

else if b[i, j] == “ ↑ ” then Print-LCS(b, X, i − 1, j) 8 else Print-LCS(b, X, i, j − 1) 6 7

Algorithm 9: Print-LCS(b, X, i, j) What is the time complexity?

33 / 41

Introduction Rod Cutting Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees Characterizing Structure Recursive Definition Computing Optimal Value

35 / 41

X = hA, B, C, B, D, A, Bi, Y = hB, D, C, A, B, Ai, prints “BCBA”

Matrix-Chain Multiplication Longest Common Subsequence Characterizing Structure Recursive Definition Computing Optimal Value Constructing Optimal Solution

Optimal Binary Search Trees

34 / 41

Optimal Binary Search Trees CSCE423/823

This will print LCS backwards

Optimal Binary Search Trees

31 / 41

Introduction

If in cell (i, j) on this path, when xi = yj (i.e. when arrow is “ - ”), print xi as part of the LCS

Optimal Binary Search Trees (2)

Goal is to construct binary search trees such that most frequently sought values are near the root, thus minimizing expected search time Given a sequence K = hk1 , . . . , kn i of n distinct keys in sorted order Key ki has probability pi that it will be sought on a particular search To handle searches for values not in K, have n + 1 dummy keys d0 , d1 , . . . , dn to serve as the tree’s leaves Dummy key di will be reached with probability qi If depthT (ki ) is distance from root of ki in tree T , then expected search cost of T is n n X X 1+ pi depthT (ki ) + qi depthT (di ) i=1

i=0

An optimal binary search tree is one with minimum expected search cost

CSCE423/823

Introduction

i pi qi

0 0.05

1 0.15 0.10

2 0.10 0.05

3 0.05 0.05

4 0.10 0.05

5 0.20 0.10

Rod Cutting Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees Characterizing Structure Recursive Definition Computing Optimal Value

expected cost = 2.80 36 / 41

expected cost = 2.75 (optimal)

Characterizing the Structure of an Optimal Solution CSCE423/823

Introduction Rod Cutting Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees Characterizing Structure Recursive Definition Computing Optimal Value

37 / 41

Observation: Since K is sorted and dummy keys interspersed in order, any subtree of a BST must contain keys in a contiguous range ki , . . . , kj and have leaves di−1 , . . . , dj Thus, if an optimal BST T has a subtree T 0 over keys ki , . . . , kj , then T 0 is optimal for the subproblem consisting of only the keys ki , . . . , k j If T 0 weren’t optimal, then a lower-cost subtree could replace T 0 in T , ⇒ contradiction

Given keys ki , . . . , kj , say that its optimal BST roots at kr for some i≤r≤j Thus if we make right choice for kr and optimally solve the problem for ki , . . . , kr−1 (with dummy keys di−1 , . . . , dr−1 ) and the problem for kr+1 , . . . , kj (with dummy keys dr , . . . , dj ), we’ll end up with an optimal solution Since we don’t know optimal kr , we’ll try them all

Recursively Defining the Value of an Optimal Solution CSCE423/823

Introduction Rod Cutting Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees Characterizing Structure Recursive Definition Computing Optimal Value

38 / 41

Rod Cutting Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees Characterizing Structure Recursive Definition Computing Optimal Value

Thus we can condense the equation to e[i, j] = e[i, r − 1] + e[r + 1, j] + w(i, j)

Rod Cutting Matrix-Chain Multiplication Longest Common Subsequence Optimal Binary Search Trees Characterizing Structure Recursive Definition Computing Optimal Value

41 / 41

Introduction Rod Cutting Matrix-Chain Multiplication

Finally, since we don’t know what kr should be, we try them all:  qi−1 if j = i − 1 e[i, j] = mini≤r≤j {e[i, r − 1] + e[r + 1, j] + w(i, j)} if i ≤ j Will also maintain table root[i, j] = index r for which kr is root of an optimal BST on keys ki , . . . , kj

Longest Common Subsequence Optimal Binary Search Trees Characterizing Structure Recursive Definition Computing Optimal Value

e[i, j] = pr + (e[i, r − 1] + w(i, r − 1)) + (e[r + 1, j] + w(r + 1, j))

i pi qi

0 0.05

1 0.15 0.10

2 0.10 0.05

3 0.05 0.05

4 0.10 0.05

5 0.20 0.10

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

allocate e[1 . . . n + 1, 0 . . . n], w[1 . . . n + 1, 0 . . . n], and root[1 . . . n, 1 . . . n] initialize e[i, i − 1] = w[i, i − 1] = qi−1 ∀ 1 ≤ i ≤ n + 1 for ` = 1 to n do for i = 1 to n − ` + 1 do j =i+`−1 e[i, j] = ∞ w[i, j] = w[i, j − 1] + pj + qj

for r = i to j do t = e[i, r − 1] + e[r + 1, j] + w[i, j] if t < e[i, j] then e[i, j] = t root[i, j] = r end

end end return (e, root)

Algorithm 10: Optimal-BST(p, q, n) 40 / 41

Computing the Value of an Optimal Solution (2)

Introduction

When combining the optimal trees from subproblems and making them children of kr , we increase their depth by 1, which increases the cost of each by the sum of the probabilities of its nodes P P Define w(i, j) = j`=i p` + j`=i−1 q` as the sum of probabilities of the nodes in the subtree built on ki , . . . , kj , and get

1

w(i, j) = w(i, r − 1) + pr + w(r + 1, j)

39 / 41

CSCE423/823

If j ≥ i, then choose root kr from ki , . . . , kj and optimally solve subproblems ki , . . . , kr−1 and kr+1 , . . . , kj

CSCE423/823

Note that Introduction

If j = i − 1, then there is only the dummy key di−1 , so e[i, i − 1] = qi−1

Computing the Value of an Optimal Solution

Recursively Defining the Value of an Optimal Solution (2) CSCE423/823

Define e[i, j] as the expected cost of searching an optimal BST built on keys ki , . . . , kj

What is the time complexity?

Suggest Documents