Docsity

Dynamic Programming: Solving Optimization Problems with Optimal Substructure - Prof. Thoma, Study notes of Algorithms and Programming

Toggle sidebar
bg1
1
1
CS 432 – Algorithms
– Dynamic programming
• Also, memoization
– Examples:
• Longest Common Subsequence
– Readings: 8.1, pp. 334-335, 8.4, p. 361
• Also, handout on 0/1 knapsack
• Wikipedia articles
2
Dynamic programming
Old “bad” name (see Wikipedia or Notes, p. 361)
It is used, when the solution can be recursively
described in terms of solutions to subproblems
(optimal substructure)
Algorithm finds solutions to subproblems and
stores them in memory for later use
More efficient than “brute-force methods”, which
solve the same subproblems over and over again
3
Optimal Substructure Property
Definition on p. 334
– If S is an optimal solution to a problem, then the
components of S are optimal solutions to
subproblems
Examples:
– True for knapsack
– True for coin-changing (p. 334)
– True for single-source shortest path
– Not true for longest-simple-path (p. 335)
4
Dynamic Programming
Works “bottom-up”
– Finds solutions to small sub-problems first
– Stores them
– Combines them somehow to find a solution
to a slightly larger subproblem
Compare to greedy approach
– Also requires optimal substructure
– But greedy makes choice first, then solves
5
Problems Solved with Dyn. Prog.
Coin changing (Section 8.2, we won’t do)
Multiplying a sequence of matrices (8.3, we
might do if we have time)
– Can do in various orders: (AB)C vs. A(BC)
– Pick order that does fewest number of scalar
multiplications
Longest common subsequence (8.4, we’ll do)
All-pairs shortest paths (Floyd’s algorithm)
– Remember from CS216?
Constructing optimal binary search trees
Knapsack problems (we’ll do 0/1)
6
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Dynamic Programming: Solving Optimization Problems with Optimal Substructure - Prof. Thoma and more Study notes Algorithms and Programming in PDF only on Docsity!

1

CS 432 – Algorithms

  • Dynamic programming
    • Also, memoization
  • Examples:
    • Longest Common Subsequence
  • Readings: 8.1, pp. 334-335, 8.4, p. 361
    • Also, handout on 0/1 knapsack
    • Wikipedia articles 2

Dynamic programming

 Old “bad” name (see Wikipedia or Notes, p. 361)

 It is used, when the solution can be recursively

described in terms of solutions to subproblems

( optimal substructure )

 Algorithm finds solutions to subproblems and

stores them in memory for later use

 More efficient than “ brute-force methods ”, which

solve the same subproblems over and over again

3

Optimal Substructure Property

 Definition on p. 334

  • If S is an optimal solution to a problem, then the components of S are optimal solutions to subproblems

 Examples:

  • True for knapsack
  • True for coin-changing (p. 334)
  • True for single-source shortest path
  • Not true for longest-simple-path (p. 335) 4

Dynamic Programming

 Works “bottom-up”

  • Finds solutions to small sub-problems first
  • Stores them
  • Combines them somehow to find a solution

to a slightly larger subproblem

 Compare to greedy approach

  • Also requires optimal substructure
  • But greedy makes choice first, then solves

5

Problems Solved with Dyn. Prog.

 Coin changing (Section 8.2, we won’t do)

 Multiplying a sequence of matrices (8.3, we

might do if we have time)

  • Can do in various orders: (AB)C vs. A(BC)
  • Pick order that does fewest number of scalarmultiplications

 Longest common subsequence (8.4, we’ll do)

 All-pairs shortest paths (Floyd’s algorithm)

  • Remember from CS216?

 Constructing optimal binary search trees

 Knapsack problems (we’ll do 0/1)

6

7

Remember Fibonacci numbers?

 Recursive code:

long fib(int n) {

assert(n >= 0);

if ( n == 0 ) return 0;

if ( n == 1 ) return 1;

return fib(n-1) + fib(n-2);

 What’s the problem?

  • Repeatedly solves the same subproblems
  • “Obscenely” exponential (p. 326) (^8)

Memoization

 Before talking about dynamic

programming, another general

technique: Memoization

– AKA using a memory function

 Simple idea:

– Calculate and store solutions to

subproblems

– Before solving it (again), look to see if

you’ve remembered it

9

Memoization

 Use a Table abstract data type

  • Lookup key: whatever identifies a

subproblem

  • Value stored: the solution

 Could be an array/vector

  • E.g. for Fibonacci, store fib(n) using

index n

  • Need to initialize the array

 Could use a map / hash-table

10

Memoization and Fibonacci

 Before recursive code below called, mustinitialize results[] so all values are -

long fib_mem(int n, long results[]) {

if ( results[n] != -1 )return results[n]; // return stored value

long val;

if ( n == 0 || n ==1 ) val = n; else // odd but right

val = fib_mem(n-1, results)

+ fib_mem(n-2, results);

results[n] = val; return val; // store calculated value

11

Observations on fib_mem()

 Same elegant top-down, recursive

approach based on definition

– Without repeated subproblems

 Memory function: a function that

remembers

– Save time by using extra space

 Can show this runs in ΘΘΘΘ(n)

12

Memoization and Functional

Languages

 Languages like Lisp and Scheme are

functional languages

 How could memoization help?

 What could go wrong? Would this

always work?

  • Side effects
  • Haskell does this (call-by-need)

19

LCS recursive solution

 We start with i = j = 0 (empty substrings of x and y)

 Since X 0 and Y 0 are empty strings, their LCS is always empty (i.e. c[0,0] = 0 )

 LCS of empty string and any other string is empty, so for every i and j: c[0, j] = c[i,0] = 0

max([, 1 ],[ 1 , ]) otherwise

[ , ] [^1 ,^1 ]^1 if [] [],

cij ci j

cij ci j xi yj

20

LCS recursive solution

 When we calculate c[i,j], we consider two cases:  First case: x[i]=y[j] : one more symbol in strings X and Y matches, so the length of LCS Xi and Y j equals to the length of LCS of smaller strings X i-1 and Y i-1 , plus 1

max([, 1 ],[ 1 , ]) otherwise

[ , ] [^1 ,^1 ]^1 if [] [],

cij ci j

cij ci j xi yj

21

LCS recursive solution

 Second case: x[i] != y[j]

 As symbols don’t match, our solution is not improved, and the length of LCS(Xi , Yj) is the same as before (i.e. maximum of LCS(Xi, Yj-1) and LCS(Xi-1,Yj)

max([, 1 ],[ 1 , ]) otherwise

[ , ] [^1 ,^1 ]^1 if [] [],

cij ci j

cij ci j xi yj

Why not just take the length of LCS(Xi-1, Yj-1)? (^22)

LCS Length Algorithm

LCS-Length(X, Y)

1. m = length(X) // get the # of symbols in X

2. n = length(Y) // get the # of symbols in Y

3. for i = 1 to m c[i,0] = 0 // special case: Y 0

4. for j = 1 to n c[0,j] = 0 // special case: X 0

5. for i = 1 to m // for all Xi

6. for j = 1 to n // for all Yj

7. if ( Xi == Yj )

8. c[i,j] = c[i-1,j-1] + 1

9. else c[i,j] = max( c[i-1,j], c[i,j-1] )

10. return c[m,n] // return LCS length for X and Y

23

LCS Example We’ll see how LCS algorithm works on the following example:  X = ABCB  Y = BDCAB

LCS(X, Y) = BCB X = A B C B Y = B D C A B

What is the Longest Common Subsequence of X and Y?

24

LCS Example (0) j 0 1 2 3 4 5 0 1 2 3 4

i Xi A B C B

Yj B D C A B

X = ABCB; m = |X| = 4

Y = BDCAB; n = |Y| = 5

Allocate array c[5,4]

ABCB BDCAB

25

LCS Example (1) j 0 1 2 3 4 5

0

1 2 3 4

i Xi A B C B

Yj B D C A B 0 0

for i = 1 to m c[i,0] = 0

for j = 1 to n c[0,j] = 0

ABCB BDCAB

26

LCS Example (2) j 0 1 2 3 4 5 0 1 2 3 4

i Xi A B C B

Yj B D C A B 0 0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

ABCB BDCAB

27

LCS Example (3) j 0 1 2 3 4 5

0 1 2 3 4

i Xi A B C B

Yj B D C A B 0 0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

ABCB BDCAB

28

LCS Example (4) j 0 1 2 3 4 5

0 1 2 3 4

i Xi A B C B

Yj B D C A B 0 0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

ABCB BDCAB

29

LCS Example (5) j 0 1 2 3 4 5

0

1 2 3 4

i Xi A B C B

Yj B D C A B 0 0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

ABCB BDCAB

30

LCS Example (6) j 0 1 2 3 4 5 0 1 2 3 4

i Xi A B C B

Yj B D C A B 0 0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

ABCB BDCAB

37

LCS Example (14) j 0 1 2 3 4 5 0 1 2 3 4

i Xi A B C B

Yj B D C A B 0 0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

ABCB BDCAB

38

LCS Example (15) j 0 1 2 3 4 5 0 1 2 3 4

i Xi A B C B

Yj B D C A B 0 0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

1 1 2 2 3

ABCB BDCAB

39

LCS Algorithm Running Time

 LCS algorithm calculates the values of each entry of the array c[m,n]  So what is the running time?

O(mn) since each c[i,j] is calculated in constant time, and there are mn elements in the array 40

How to find actual LCS  So far, we have just found the length of LCS, but not LCS itself.  We want to modify this algorithm to make it output Longest Common Subsequence of X and Y Each c[i,j] depends on c[i-1,j] and c[i,j-1] or c[i-1, j-1] For each c[i,j] we can say how it was acquired: 2 2 3

2 For example, here c[i,j] = c[i-1,j-1] +1 = 2+1=

41

How to find actual LCS - continued  Remember that

max([, 1 ],[ 1 , ]) otherwise

[ , ] [^1 ,^1 ]^1 if [] [],

cij ci j

cij ci j xi yj

 So we can start from c[m,n] and go backwards  Look first to see if 2nd^ case above was true  If not, then c[i,j] = c[i-1, j-1]+1 , so remember x[i] (because x[i] is a part of LCS)  When i=0 or j=0 (i.e. we reached the beginning), output remembered letters in reverse order 42

Algorithm to find actual LCS  Here’s a recursive algorithm to do this:                  !" ""#$       "  %" & &

43

Finding LCS j 0 1 2 3 4 5 0 1 2 3 4

i Xi A B C

Yj B D C A B 0 0

B 1 1 2 2 3

44

Finding LCS (2) j 0 1 2 3 4 5 0 1 2 3 4

i Xi A B C

Yj B D C A B 0 0

B 1 1 2 2 3

LCS (reversed order):^ B^ C^ B LCS (straight order): B C B (this string turned out to be a palindrome)

45 46

Review: Dynamic programming  DP is a method for solving certain kind of problems  DP can be applied when the solution of a problem includes solutions to subproblems  We need to find a recursive formula for the solution  We can recursively solve subproblems, starting from the trivial case, and save their solutions in memory  In the end we’ll get the solution of the whole problem

47

Properties of a problem that can be solved with dynamic programming

 Simple Subproblems

  • We should be able to break the original

problem to smaller subproblems that have the

same structure

 Optimal Substructure of the problems

  • The solution to the problem must be a

composition of subproblem solutions

 Subproblem Overlap

  • Optimal subproblems to unrelated problems can

contain subproblems in common 48

Review: Longest Common Subsequence (LCS)  Problem: how to find the longest pattern of characters that is common to two text strings X and Y  Dynamic programming algorithm: solve subproblems until we get the final solution  Subproblem: first find the LCS of prefixes of X and Y.  this problem has optimal substructure : LCS of two prefixes is always a part of LCS of bigger strings