Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC)...

60
CS/ECE 374: Algorithms & Models of Computation Dynamic Programming Lecture 13 (UIUC) CS/ECE 374 1 March 10, 2021 1 / 28

Transcript of Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC)...

Page 1: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

CS/ECE 374: Algorithms & Models of

Computation

Dynamic Programming

Lecture 13

(UIUC) CS/ECE 374 1 March 10, 2021 1 / 28

Page 2: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursion types

1 Divide and Conquer: Problem reduced to multipleindependent sub-problems.

Examples: Merge sort, quick sort, multiplication, medianselection.

Each sub-problem is a fraction smaller.2 Backtracking: A sequence of decision problems. Recursion

tries all possibilities at each step.

Each subproblem is only a constant smaller, e.g. from n ton � 1.

3 Dynamic Programming: Smart recursion with memoization

(UIUC) CS/ECE 374 2 March 10, 2021 2 / 28

Page 3: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursion types

1 Divide and Conquer: Problem reduced to multipleindependent sub-problems.

Examples: Merge sort, quick sort, multiplication, medianselection.

Each sub-problem is a fraction smaller.2 Backtracking: A sequence of decision problems. Recursion

tries all possibilities at each step.

Each subproblem is only a constant smaller, e.g. from n ton � 1.

3 Dynamic Programming: Smart recursion with memoization

(UIUC) CS/ECE 374 2 March 10, 2021 2 / 28

Page 4: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Part I

Fibonacci Numbers

(UIUC) CS/ECE 374 3 March 10, 2021 3 / 28

Page 5: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Fibonacci Numbers

Fibonacci numbers defined by recurrence:

F (n) = F (n � 1) + F (n � 2) and F (0) = 0, F (1) = 1.

These numbers have many interesting and amazing properties.A journal The Fibonacci Quarterly!

1 F (n) = (�n � (1 � �)n)/p5 where � is the golden ratio

(1 +p5)/2 ' 1.618.

2 limn!1F (n + 1)/F (n) = �

(UIUC) CS/ECE 374 4 March 10, 2021 4 / 28

Toni Ocn 1 2 Tci

Page 6: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Algorithm for Fibonacci Numbers

Question: Given n, compute F (n).

Fib(n):if (n = 0)

return 0

else if (n = 1)

return 1

else

return Fib(n � 1) + Fib(n � 2)

Running time? Let T (n) be the number of additions in Fib(n).

T (n) = T (n � 1) + T (n � 2) + 1 and T (0) = T (1) = 0

(UIUC) CS/ECE 374 5 March 10, 2021 5 / 28

Page 7: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Algorithm for Fibonacci Numbers

Question: Given n, compute F (n).

Fib(n):if (n = 0)

return 0

else if (n = 1)

return 1

else

return Fib(n � 1) + Fib(n � 2)

Running time? Let T (n) be the number of additions in Fib(n).

T (n) = T (n � 1) + T (n � 2) + 1 and T (0) = T (1) = 0

(UIUC) CS/ECE 374 5 March 10, 2021 5 / 28

Page 8: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Algorithm for Fibonacci Numbers

Question: Given n, compute F (n).

Fib(n):if (n = 0)

return 0

else if (n = 1)

return 1

else

return Fib(n � 1) + Fib(n � 2)

Running time? Let T (n) be the number of additions in Fib(n).

T (n) = T (n � 1) + T (n � 2) + 1 and T (0) = T (1) = 0

(UIUC) CS/ECE 374 5 March 10, 2021 5 / 28

Page 9: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Algorithm for Fibonacci Numbers

Question: Given n, compute F (n).

Fib(n):if (n = 0)

return 0

else if (n = 1)

return 1

else

return Fib(n � 1) + Fib(n � 2)

Running time? Let T (n) be the number of additions in Fib(n).

T (n) = T (n � 1) + T (n � 2) + 1 and T (0) = T (1) = 0

Roughly same as F (n)

T (n) = ⇥(�n)

The number of additions is exponential in n. Can we do better?(UIUC) CS/ECE 374 5 March 10, 2021 5 / 28

Page 10: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursion Tree

(UIUC) CS/ECE 374 6 March 10, 2021 6 / 28

Page 11: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Memoization

The recursive algorithm is slow because it computes the sameFibonacci numbers over and over.

Memoization1 Write down the results of recursive calls and look them up later2 An array F (n), where F (i) stores the result of Fib(i)3 Evaluation order: From bottom up, i = 2 then i = 3 and so on

(UIUC) CS/ECE 374 7 March 10, 2021 7 / 28

Page 12: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Memoization

The recursive algorithm is slow because it computes the sameFibonacci numbers over and over.

Memoization1 Write down the results of recursive calls and look them up later

2 An array F (n), where F (i) stores the result of Fib(i)3 Evaluation order: From bottom up, i = 2 then i = 3 and so on

(UIUC) CS/ECE 374 7 March 10, 2021 7 / 28

Page 13: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Memoization

The recursive algorithm is slow because it computes the sameFibonacci numbers over and over.

Memoization1 Write down the results of recursive calls and look them up later2 An array F (n), where F (i) stores the result of Fib(i)

3 Evaluation order: From bottom up, i = 2 then i = 3 and so on

(UIUC) CS/ECE 374 7 March 10, 2021 7 / 28

Page 14: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Memoization

The recursive algorithm is slow because it computes the sameFibonacci numbers over and over.

Memoization1 Write down the results of recursive calls and look them up later2 An array F (n), where F (i) stores the result of Fib(i)3 Evaluation order: From bottom up, i = 2 then i = 3 and so on

(UIUC) CS/ECE 374 7 March 10, 2021 7 / 28

Page 15: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

An iterative algorithm for Fibonacci numbers

FibIter(n):if (n = 0) then

return 0

if (n = 1) then

return 1

F [0] = 0

F [1] = 1

for i = 2 to n do

F [i ] = F [i � 1] + F [i � 2]

return F [n]

What is the running time of the algorithm? O(n) additions.

(UIUC) CS/ECE 374 8 March 10, 2021 8 / 28

Page 16: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

An iterative algorithm for Fibonacci numbers

FibIter(n):if (n = 0) then

return 0

if (n = 1) then

return 1

F [0] = 0

F [1] = 1

for i = 2 to n do

F [i ] = F [i � 1] + F [i � 2]

return F [n]

What is the running time of the algorithm?

O(n) additions.

(UIUC) CS/ECE 374 8 March 10, 2021 8 / 28

Page 17: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

An iterative algorithm for Fibonacci numbers

FibIter(n):if (n = 0) then

return 0

if (n = 1) then

return 1

F [0] = 0

F [1] = 1

for i = 2 to n do

F [i ] = F [i � 1] + F [i � 2]

return F [n]

What is the running time of the algorithm? O(n) additions.

(UIUC) CS/ECE 374 8 March 10, 2021 8 / 28

Page 18: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

DP prunes recursion tree

(UIUC) CS/ECE 374 9 March 10, 2021 9 / 28

Page 19: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

What is the di↵erence?

Dynamic Programming:Finding a recursion that can be e↵ectively/e�ciently memoized.

Leads to polynomial time algorithm if number of distinctsub-problems is polynomial in input size.

(UIUC) CS/ECE 374 10 March 10, 2021 10 / 28

Page 20: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Saving space

Do we need an array of n numbers? Not really.

FibIter(n):if (n = 0) then

return 0

if (n = 1) then

return 1

prev2 = 0

prev1 = 1

for i = 2 to n do

temp = prev1 + prev2prev2 = prev1prev1 = temp

return prev1

(UIUC) CS/ECE 374 11 March 10, 2021 11 / 28

Page 21: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Dynamic Programming

Dynamic Programming: Smart recursion with memoization

Dynamic programming is not about filling tables. It is

about finding a smart recursion. First, find the correct

recursion.

Use memoization to avoid recomputation of common solutions,hence optimizing running time and space.

First, allocate a data structure (usually an array or amulti-dimensional array that can hold values for each of thesubproblems)Figure out a way to order the computation of the sub-problemsstarting from the base case.

Often an iterative algorithm with bottom up computation.

(UIUC) CS/ECE 374 12 March 10, 2021 12 / 28

Page 22: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Dynamic Programming

Dynamic Programming: Smart recursion with memoization

Dynamic programming is not about filling tables. It is

about finding a smart recursion. First, find the correct

recursion.

Use memoization to avoid recomputation of common solutions,hence optimizing running time and space.

First, allocate a data structure (usually an array or amulti-dimensional array that can hold values for each of thesubproblems)Figure out a way to order the computation of the sub-problemsstarting from the base case.

Often an iterative algorithm with bottom up computation.

(UIUC) CS/ECE 374 12 March 10, 2021 12 / 28

Page 23: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Dynamic Programming

Dynamic Programming: Smart recursion with memoization

Dynamic programming is not about filling tables. It is

about finding a smart recursion. First, find the correct

recursion.

Use memoization to avoid recomputation of common solutions,hence optimizing running time and space.

First, allocate a data structure (usually an array or amulti-dimensional array that can hold values for each of thesubproblems)Figure out a way to order the computation of the sub-problemsstarting from the base case.

Often an iterative algorithm with bottom up computation.

(UIUC) CS/ECE 374 12 March 10, 2021 12 / 28

Page 24: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Dynamic Programming

Dynamic Programming: Smart recursion with memoization

Dynamic programming is not about filling tables. It is

about finding a smart recursion. First, find the correct

recursion.

Use memoization to avoid recomputation of common solutions,hence optimizing running time and space.

First, allocate a data structure (usually an array or amulti-dimensional array that can hold values for each of thesubproblems)Figure out a way to order the computation of the sub-problemsstarting from the base case.

Often an iterative algorithm with bottom up computation.

(UIUC) CS/ECE 374 12 March 10, 2021 12 / 28

Page 25: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Part II

Text Segmentation

(UIUC) CS/ECE 374 13 March 10, 2021 13 / 28

Page 26: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Problem

Input A string w 2 ⌃⇤ and access to a language L ✓ ⌃

⇤ viafunction IsStrInL(string x) that decides whether x isin L

Goal Decide if w 2 L⇤ using IsStrInL(string x) as a black

box sub-routine

ExampleSuppose L is English and we have a procedure to check whether astring/word is in the English dictionary.

Is the string “isthisanenglishsentence” in English⇤?

Is “stampstamp” in English⇤?

Is “zibzzzad” in English⇤?

(UIUC) CS/ECE 374 14 March 10, 2021 14 / 28

Page 27: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Text Segmentation

Backtracking

Changes the problem into a sequence of decision problems

Each tries all possibilities for the current decision

Let the recursion fairy make all remaining decisions

Only the su�x matters.

Base case

zero-length string

(UIUC) CS/ECE 374 15 March 10, 2021 15 / 28

Page 28: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Text Segmentation

Backtracking

Changes the problem into a sequence of decision problems

Each tries all possibilities for the current decision

Let the recursion fairy make all remaining decisions

Only the su�x matters.

Base case

zero-length string

(UIUC) CS/ECE 374 15 March 10, 2021 15 / 28

Page 29: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Text Segmentation

Backtracking

Changes the problem into a sequence of decision problems

Each tries all possibilities for the current decision

Let the recursion fairy make all remaining decisions

Only the su�x matters.

Base case

zero-length string

(UIUC) CS/ECE 374 15 March 10, 2021 15 / 28

Page 30: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Solution

Assume w is stored in array A[1..n]

IsStringinLstar(A[1..n]):If (n = 0) Output YES

If (IsStrInL(A[1..n]))Output YES

Else

For (i = 1 to n � 1) do

If (IsStrInL(A[1..i ]) and IsStrInLstar(A[i + 1..n]))Output YES

Output NO

Question: How many distinct sub-problems doesIsStrInLstar(A[1..n]) generate? O(n)

(UIUC) CS/ECE 374 16 March 10, 2021 16 / 28

Page 31: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Solution

Assume w is stored in array A[1..n]

IsStringinLstar(A[1..n]):If (n = 0) Output YES

If (IsStrInL(A[1..n]))Output YES

Else

For (i = 1 to n � 1) do

If (IsStrInL(A[1..i ]) and IsStrInLstar(A[i + 1..n]))Output YES

Output NO

Question: How many distinct sub-problems doesIsStrInLstar(A[1..n]) generate?

O(n)

(UIUC) CS/ECE 374 16 March 10, 2021 16 / 28

Page 32: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Solution

Assume w is stored in array A[1..n]

IsStringinLstar(A[1..n]):If (n = 0) Output YES

If (IsStrInL(A[1..n]))Output YES

Else

For (i = 1 to n � 1) do

If (IsStrInL(A[1..i ]) and IsStrInLstar(A[i + 1..n]))Output YES

Output NO

Question: How many distinct sub-problems doesIsStrInLstar(A[1..n]) generate? O(n)

(UIUC) CS/ECE 374 16 March 10, 2021 16 / 28

Page 33: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Naming subproblems

After seeing that number of subproblems is O(n) we name them tohelp us understand the structure better.

ISL(i): a boolean which is 1 if A[i ..n] is in L⇤, 0 otherwise

Base case: ISL(n + 1) = 1 interpreting A[n + 1..n] as ✏

(UIUC) CS/ECE 374 17 March 10, 2021 17 / 28

Page 34: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Evaluate subproblems

Recursive relation:

ISL(i) = 1 if 9i < j n + 1 such that (ISL(j) = 1 andIsStrInL(A[i ..(j � 1)]) = 1)

ISL(i) = 0 otherwise

Alternatively: ISL(i) = maxi<jn+1 ISL(j)IsStrInL(A[i ..(j � 1]))

Output: ISL(1)

(UIUC) CS/ECE 374 18 March 10, 2021 18 / 28

I 1 1

Page 35: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Evaluate subproblems

Recursive relation:

ISL(i) = 1 if 9i < j n + 1 such that (ISL(j) = 1 andIsStrInL(A[i ..(j � 1)]) = 1)

ISL(i) = 0 otherwise

Alternatively: ISL(i) = maxi<jn+1 ISL(j)IsStrInL(A[i ..(j � 1]))

Output: ISL(1)

(UIUC) CS/ECE 374 18 March 10, 2021 18 / 28

Page 36: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Iterative Algorithm

IsStringinLstar-Iterative(A[1..n]):boolean ISL[1..(n + 1)]

ISL[n + 1] = TRUEfor (i = n down to 1)

ISL[i ] = FALSEfor (j = i + 1 to n + 1)

If (ISL[j ] and IsStrInL(A[i ..j � 1]))

ISL[i ] = TRUEBreak

If (ISL[1] = 1) Output YES

Else Output NO

Running time: O(n2) (assuming call to IsStrInL is O(1)

time)

Space: O(n)

(UIUC) CS/ECE 374 19 March 10, 2021 19 / 28

je

Page 37: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Iterative Algorithm

IsStringinLstar-Iterative(A[1..n]):boolean ISL[1..(n + 1)]

ISL[n + 1] = TRUEfor (i = n down to 1)

ISL[i ] = FALSEfor (j = i + 1 to n + 1)

If (ISL[j ] and IsStrInL(A[i ..j � 1]))

ISL[i ] = TRUEBreak

If (ISL[1] = 1) Output YES

Else Output NO

Running time:

O(n2) (assuming call to IsStrInL is O(1)

time)

Space: O(n)

(UIUC) CS/ECE 374 19 March 10, 2021 19 / 28

Page 38: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Iterative Algorithm

IsStringinLstar-Iterative(A[1..n]):boolean ISL[1..(n + 1)]

ISL[n + 1] = TRUEfor (i = n down to 1)

ISL[i ] = FALSEfor (j = i + 1 to n + 1)

If (ISL[j ] and IsStrInL(A[i ..j � 1]))

ISL[i ] = TRUEBreak

If (ISL[1] = 1) Output YES

Else Output NO

Running time: O(n2) (assuming call to IsStrInL is O(1)

time)

Space: O(n)

(UIUC) CS/ECE 374 19 March 10, 2021 19 / 28

I 12T 1 n 1 0 NZ

Page 39: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Iterative Algorithm

IsStringinLstar-Iterative(A[1..n]):boolean ISL[1..(n + 1)]

ISL[n + 1] = TRUEfor (i = n down to 1)

ISL[i ] = FALSEfor (j = i + 1 to n + 1)

If (ISL[j ] and IsStrInL(A[i ..j � 1]))

ISL[i ] = TRUEBreak

If (ISL[1] = 1) Output YES

Else Output NO

Running time: O(n2) (assuming call to IsStrInL is O(1)

time)

Space:

O(n)

(UIUC) CS/ECE 374 19 March 10, 2021 19 / 28

Page 40: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Iterative Algorithm

IsStringinLstar-Iterative(A[1..n]):boolean ISL[1..(n + 1)]

ISL[n + 1] = TRUEfor (i = n down to 1)

ISL[i ] = FALSEfor (j = i + 1 to n + 1)

If (ISL[j ] and IsStrInL(A[i ..j � 1]))

ISL[i ] = TRUEBreak

If (ISL[1] = 1) Output YES

Else Output NO

Running time: O(n2) (assuming call to IsStrInL is O(1)

time)

Space: O(n)

(UIUC) CS/ECE 374 19 March 10, 2021 19 / 28

Page 41: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

How to design DP algorithms

1 Find a “smart” recursion (The hard part)1 Formulate the sub-problem2 so that the number of distinct subproblems is small; polynomial

in the original problem size.

2 Memoization1 Identify distinct subproblems2 Choose a memoization data structure3 Identify dependencies and find a good evaluation order4 An iterative algorithm replacing recursive calls with array

lookups

(UIUC) CS/ECE 374 20 March 10, 2021 20 / 28

Page 42: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

How to design DP algorithms

1 Find a “smart” recursion (The hard part)1 Formulate the sub-problem2 so that the number of distinct subproblems is small; polynomial

in the original problem size.

2 Memoization1 Identify distinct subproblems2 Choose a memoization data structure3 Identify dependencies and find a good evaluation order4 An iterative algorithm replacing recursive calls with array

lookups

(UIUC) CS/ECE 374 20 March 10, 2021 20 / 28

Page 43: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Part III

Longest Increasing Subsequence

(UIUC) CS/ECE 374 21 March 10, 2021 21 / 28

Page 44: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Longest Increasing Subsequence Problem

Input A sequence of numbers a1, a2, . . . , an

Goal Find an increasing subsequence ai1, ai2, . . . , aik ofmaximum length

Example1 Sequence: 6, 3, 5, 2, 7, 8, 12 Increasing subsequences: 6, 7, 8 and 3, 5, 7, 8 and 2, 7 etc3 Longest increasing subsequence: 3, 5, 7, 8

(UIUC) CS/ECE 374 22 March 10, 2021 22 / 28

Page 45: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Longest Increasing Subsequence Problem

Input A sequence of numbers a1, a2, . . . , an

Goal Find an increasing subsequence ai1, ai2, . . . , aik ofmaximum length

Example1 Sequence: 6, 3, 5, 2, 7, 8, 12 Increasing subsequences: 6, 7, 8 and 3, 5, 7, 8 and 2, 7 etc3 Longest increasing subsequence: 3, 5, 7, 8

(UIUC) CS/ECE 374 22 March 10, 2021 22 / 28

Page 46: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Approach: Take 1

LIS: Longest increasing subsequence

Can we find a recursive algorithm for LIS?

LIS(A[1..n]):1 Case 1: Does not contain A[n] in which case

LIS(A[1..n]) = LIS(A[1..(n � 1)])2 Case 2: contains A[n] in which case LIS(A[1..n]) is not so

clear.

ObservationFor second case we want to find a subsequence in A[1..(n � 1)]

that is restricted to numbers less than A[n]. This suggests that amore general problem is LIS smaller(A[1..n], x) which gives thelongest increasing subsequence in A where each number in thesequence is less than x .

(UIUC) CS/ECE 374 23 March 10, 2021 23 / 28

Page 47: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Approach: Take 1

LIS: Longest increasing subsequence

Can we find a recursive algorithm for LIS?

LIS(A[1..n]):1 Case 1: Does not contain A[n] in which case

LIS(A[1..n]) = LIS(A[1..(n � 1)])2 Case 2: contains A[n] in which case LIS(A[1..n]) is not so

clear.

ObservationFor second case we want to find a subsequence in A[1..(n � 1)]

that is restricted to numbers less than A[n]. This suggests that amore general problem is LIS smaller(A[1..n], x) which gives thelongest increasing subsequence in A where each number in thesequence is less than x .

(UIUC) CS/ECE 374 23 March 10, 2021 23 / 28

Page 48: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Approach

LIS(A[1..n]): the length of longest increasing subsequence in A

LIS smaller(A[1..n], x): length of longest increasing subsequencein A[1..n] with all numbers in subsequence less than x

LIS smaller(A[1..n], x):if (n = 0) then return 0

m = LIS smaller(A[1..(n � 1)], x)if (A[n] < x) then

m = max(m, 1 + LIS smaller(A[1..(n � 1)],A[n]))Output m

LIS(A[1..n]):return LIS smaller(A[1..n],1)

(UIUC) CS/ECE 374 24 March 10, 2021 24 / 28

Page 49: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Approach

LIS smaller(A[1..n], x):if (n = 0) then return 0

m = LIS smaller(A[1..(n � 1)], x)if (A[n] < x) then

m = max(m, 1 + LIS smaller(A[1..(n � 1)],A[n]))Output m

LIS(A[1..n]):return LIS smaller(A[1..n],1)

How many distinct sub-problems will LIS smaller(A[1..n],1)

generate?

O(n2)

What is the running time if we memoize recursion? O(n2) since

each call takes O(1) time to assemble the answers from tworecursive calls and no other computation.

How much space for memoization? O(n2)

(UIUC) CS/ECE 374 25 March 10, 2021 25 / 28

Page 50: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Approach

LIS smaller(A[1..n], x):if (n = 0) then return 0

m = LIS smaller(A[1..(n � 1)], x)if (A[n] < x) then

m = max(m, 1 + LIS smaller(A[1..(n � 1)],A[n]))Output m

LIS(A[1..n]):return LIS smaller(A[1..n],1)

How many distinct sub-problems will LIS smaller(A[1..n],1)

generate? O(n2)

What is the running time if we memoize recursion? O(n2) since

each call takes O(1) time to assemble the answers from tworecursive calls and no other computation.

How much space for memoization? O(n2)

(UIUC) CS/ECE 374 25 March 10, 2021 25 / 28

Page 51: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Approach

LIS smaller(A[1..n], x):if (n = 0) then return 0

m = LIS smaller(A[1..(n � 1)], x)if (A[n] < x) then

m = max(m, 1 + LIS smaller(A[1..(n � 1)],A[n]))Output m

LIS(A[1..n]):return LIS smaller(A[1..n],1)

How many distinct sub-problems will LIS smaller(A[1..n],1)

generate? O(n2)

What is the running time if we memoize recursion?

O(n2) since

each call takes O(1) time to assemble the answers from tworecursive calls and no other computation.

How much space for memoization? O(n2)

(UIUC) CS/ECE 374 25 March 10, 2021 25 / 28

Page 52: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Approach

LIS smaller(A[1..n], x):if (n = 0) then return 0

m = LIS smaller(A[1..(n � 1)], x)if (A[n] < x) then

m = max(m, 1 + LIS smaller(A[1..(n � 1)],A[n]))Output m

LIS(A[1..n]):return LIS smaller(A[1..n],1)

How many distinct sub-problems will LIS smaller(A[1..n],1)

generate? O(n2)

What is the running time if we memoize recursion? O(n2) since

each call takes O(1) time to assemble the answers from tworecursive calls and no other computation.

How much space for memoization? O(n2)

(UIUC) CS/ECE 374 25 March 10, 2021 25 / 28

Page 53: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Approach

LIS smaller(A[1..n], x):if (n = 0) then return 0

m = LIS smaller(A[1..(n � 1)], x)if (A[n] < x) then

m = max(m, 1 + LIS smaller(A[1..(n � 1)],A[n]))Output m

LIS(A[1..n]):return LIS smaller(A[1..n],1)

How many distinct sub-problems will LIS smaller(A[1..n],1)

generate? O(n2)

What is the running time if we memoize recursion? O(n2) since

each call takes O(1) time to assemble the answers from tworecursive calls and no other computation.

How much space for memoization?

O(n2)

(UIUC) CS/ECE 374 25 March 10, 2021 25 / 28

Page 54: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Recursive Approach

LIS smaller(A[1..n], x):if (n = 0) then return 0

m = LIS smaller(A[1..(n � 1)], x)if (A[n] < x) then

m = max(m, 1 + LIS smaller(A[1..(n � 1)],A[n]))Output m

LIS(A[1..n]):return LIS smaller(A[1..n],1)

How many distinct sub-problems will LIS smaller(A[1..n],1)

generate? O(n2)

What is the running time if we memoize recursion? O(n2) since

each call takes O(1) time to assemble the answers from tworecursive calls and no other computation.

How much space for memoization? O(n2)

(UIUC) CS/ECE 374 25 March 10, 2021 25 / 28

Page 55: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Naming subproblems and recursive equation

After seeing that number of subproblems is O(n2) we name them to

help us understand the structure better. For notational ease we add1 at end of array (in position n + 1)

LIS(i , j): length of longest increasing sequence in A[1..i ] amongnumbers less than A[j ] (defined only for i < j)

(UIUC) CS/ECE 374 26 March 10, 2021 26 / 28

Page 56: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

How to order bottom up computation?

CS

374

For i<j

1 2 3 4 n+1

0

1

2

3

n

i

j

Base case: LIS(0, j) = 0 for 1 j n + 1

Recursive relation:

LIS(i , j) = LIS(i � 1, j) if A[i ] > A[j ]

LIS(i , j) = max{LIS(i � 1, j), 1 + LIS(i � 1, i)} if A[i ] A[j ]

(UIUC) CS/ECE 374 27 March 10, 2021 27 / 28

Page 57: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

How to order bottom up computation?

CS

374

For i<j

1 2 3 4 n+1

0

1

2

3

n

i

j

Base case: LIS(0, j) = 0 for 1 j n + 1

Recursive relation:

LIS(i , j) = LIS(i � 1, j) if A[i ] > A[j ]

LIS(i , j) = max{LIS(i � 1, j), 1 + LIS(i � 1, i)} if A[i ] A[j ]

(UIUC) CS/ECE 374 27 March 10, 2021 27 / 28

O U O O O O O O

Page 58: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

How to order bottom up computation?

CS

374

For i<j

1 2 3 4 n+1

0

1

2

3

n

i

j

Base case: LIS(0, j) = 0 for 1 j n + 1

Recursive relation:

LIS(i , j) = LIS(i � 1, j) if A[i ] > A[j ]

LIS(i , j) = max{LIS(i � 1, j), 1 + LIS(i � 1, i)} if A[i ] A[j ]

(UIUC) CS/ECE 374 27 March 10, 2021 27 / 28

k

Page 59: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

How to order bottom up computation?

Sequence: A[1..7] = 6, 3, 5, 2, 7, 8, 1

CS

374

For i<j

1 2 3 4 n+1

0

1

2

3

n

i

j

(UIUC) CS/ECE 374 28 March 10, 2021 28 / 28

63 5 2 78 I

O O O O O O O O

6 O O O 1 I O l3 1 01 I O l5 02 20 227 3 038 04I 4

Page 60: Dynamic Programming · 2021. 3. 11. · 3 Dynamic Programming:Smartrecursionwithmemoization (UIUC) CS/ECE 374 2 March 10, 2021 2/28. Recursion types 1 Divide and Conquer:Problemreducedtomultiple

Iterative algorithm

LIS-Iterative(A[1..n]):A[n + 1] = 1int LIS[0..n, 1..n + 1]

for (j = 1 to n + 1) do

LIS[0, j ] = 0

for (i = 1 to n) do

for (j = i + 1 to n)If (A[i ] > A[j ]) LIS[i , j ] = LIS[i � 1, j ]Else LIS[i , j ] = max{LIS[i � 1, j ], 1 + LIS[i � 1, i ]}

Return LIS[n, n + 1]

Running time: O(n2)

Space: O(n2)

(UIUC) CS/ECE 374 29 March 10, 2021 29 / 28