Docsity

Dynamic Programming: Solving Recurrent Problems with Overlapping Subinstances - Prof. esku, Assignments of Compiler Design

Partial preview of the text

Download Dynamic Programming: Solving Recurrent Problems with Overlapping Subinstances - Prof. esku and more Assignments Compiler Design in PDF only on Docsity!

Chapter 4(b)

Dynamic Programming

and traversal techniques

Always remember answers to the sub-problems you've already solved.

Outline

 The general Method of dynamic programming  Fibonacci Series  0/1 Knapsack Problem  Finding the actual Knapsack

Dynamic Programming

D ynamic Programming is a general algorithm design technique for solving problems defined by or formulated as recurrences with overlapping sub- instances

  • (^) Main idea:
    • (^) set up a recurrence relating a solution to a larger instance to solutions of some smaller instances
    • solve smaller instances once
    • (^) record solutions in a table
    • (^) extract solution to the initial instance from that table

Dynamic programming

It is like a divide-and-conquer …

but store the solutions to sub-problems

for possible reuse.

Invented by American mathematician

Richard Bellman in the 1950s to solve

optimization problems and later

assimilated by CS

“Programming” here means “planning”

Example of Memorization:

Fibonacci series

 0, 1, 1, 2, 3, 5, 8, 13, 21, …  f(0) = 0.  f(1) = 1.  f(N) = f(N-1) + f(N-2) if N 2. int f(int n) { if n < 2 return n else return f(n-1) + f(n-2) } f(7) f(6) f(5) f(5) f(4) f(4) f(3) f(4) f(3)f(3)f(2)f(3)f(2) f(2)f(1) … …^ …^ …^ …^ …^ …^1

Reuse earlier results!

(“memoization” or “tabling”)

 0, 1, 1, 2, 3, 5, 8, 13, 21, …  f(0) = 0.  f(1) = 1.  f(N) = f(N-1) + f(N-2) if N 2. int f(int n) { if n < 2 return n else return f memo (n-1) + f memo (n-2) } f(7) f(6) f(5) f(4) …

Given some items, pack the knapsack to get the maximum total value. Each item has some weight and some value. Total weight that we can carry is no more than some fixed number W. So we must consider weights of items as well as their values. Item # Weight Value 1 1 8 2 3 6 3 5 5

Knapsack problem

Knapsack problem

There are two versions of the problem:

  1. “0-1 knapsack problem”  (^) Items are indivisible; you either take an item or not. each item must be entirely accepted or rejected
  2. “Fractional knapsack problem”  Items are divisible: you can take any fraction of an item

Let’s first solve this problem with a straightforward algorithm  Since there are n items, there are 2 n possible combinations of items.  We go through all combinations and find the one with maximum value and with total weight less or equal to W  Running time will be O( n )

0-1 Knapsack problem:

brute-force approach

 We can do better with an algorithm based on dynamic programming  We need to carefully identify the sub-problems

0-1 Knapsack problem:

dynamic programming approach

for w = 0 to W V[0,w] = 0 for i = 1 to n V[i,0] = 0 for i = 1 to n for w = 0 to W if w i <= w // item i can be part of the solution if b i

  • V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i
  • V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i

w

0-1 Knapsack Algorithm

for w = 0 to W V[0,w] = 0 for i = 1 to n V[i,0] = 0 for i = 1 to n for w = 0 to W < the rest of the code > What is the running time of this algorithm? O(W) O(W) Repeat n times O(W) + O(n*W) ~ O(n) Remember that the brute-force algorithm takes O( n )

Running time

for w = 0 to W V[0,w] = 0 0 0 0 0 0 0 0 1 2 3 0 1 2 3 4 5 4 i\W

Example (2)

for i = 1 to n V[i,0] = 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 0 1 2 3 4 5 4 i\W

Example (3)