Docsity

Dynamic Programming - Algorithms and Applications in Java - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Dynamic Programming - Algorithms and Applications in Java - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Dynamic Programming

  • Sequence of decisions.
  • Problem state.
  • Principle of optimality.
  • Dynamic Programming Recurrence Equations.
  • Solution of recurrence equations.

Sequence Of Decisions

  • As in the greedy method, the solution to a problem is viewed as the result of a sequence of decisions.
  • Unlike the greedy method, decisions are not made in a greedy and binding manner.

Sequence Of Decisions

  • Decide the x (^) i values in the order x 1 , x 2 , x 3 , …, x (^) n.
  • Decide the x (^) i values in the order x (^) n, x (^) n-1 , x (^) n-2 , …, x 1.
  • Decide the x (^) i values in the order x 1 , x (^) n, x 2 , x (^) n-1 , …
  • Or any other order.

Problem State

  • The state of the 0/1 knapsack problem is given by

 the weights and profits of the available items  the capacity of the knapsack

  • When a decision on one of the x (^) i values is made, the problem state changes.  item i is no longer available  the remaining knapsack capacity may be less

Problem State

  • Suppose that decisions are made in the order x (^) n , x (^) n-1 , x (^) n-2 , …, x 1.
  • The initial state of the problem is described by the pair (n, c).  Items 1 through n are available (the weights, profits and first item index are implicit).  The available knapsack capacity is c.
  • Following the first decision the state becomes one of the following:  (n-1, c) … when the decision is to set xn = 0.  (n-1, c-w (^) n ) … when the decision is to set xn = 1.

Principle Of Optimality

  • An optimal solution satisfies the following property:  No matter what the first decision, the remaining decisions are optimal with respect to the state that results from this decision.
  • Dynamic programming may be used only when the principle of optimality holds.

x 1 = a 1 = 0

  • If not, this instance has a better solution b 2 , b 3 , …, b (^) n.

i = 2

n maximize p^ i^ x^ i

i = 2

n subject to w^ i^ x^ i^ <= c

and x (^) i = 0 or 1 for all i

i = 2

n p (^) i b (^) i > i = 2

n p (^) i a (^) i

x 1 = a 1 = 1

  • If not, this instance has a better solution b 2 , b 3 , …, b (^) n.

i = 2

n maximize p^ i^ x^ i

i = 2

n subject to w^ i^ x^ i^ <= c- w^1

and x (^) i = 0 or 1 for all i

i = 2

n p (^) i b (^) i > i = 2

n p (^) i a (^) i

Dynamic Programming Recurrence

  • Let f(i,y) be the profit value of the optimal solution to the knapsack instance defined by the state (i,y).  Items i through n are available.  Available capacity is y.
  • For the time being assume that we wish to determine only the value of the best solution.  Later we will worry about determining the xis that yield this maximum value.
  • Under this assumption, our task is to determine f(1,c).

Dynamic Programming Recurrence

  • f(n,y) is the value of the optimal solution to the knapsack instance defined by the state (n,y).  Only item n is available.  Available capacity is y.
  • If wn <= y, f(n,y) = p (^) n.
  • If wn > y, f(n,y) = 0.

Dynamic Programming Recurrence

  • The only other possibility for the first decision is x (^) i = 1.
  • The case x (^) i = 1 can arise only when y >= wi.
  • From the principle of optimality, it follows that f(i,y) = f(i+1,y-w (^) i ) + p (^) i.
  • Combining the two cases, we get

 f(i,y) = f(i+1,y) whenever y < w (^) i.  f(i,y) = max{f(i+1,y), f(i+1,y-wi) + p (^) i}, y >= w (^) i.

Recursive Code

/** @return f(i,y) */

private static int f(int i, int y)

{

if (i == n) return (y < w[n])? 0 : p[n]; if (y < w[i]) return f(i + 1, y); return Math.max(f(i + 1, y), f(i + 1, y - w[i]) + p[i]);

}

Time Complexity

  • Let t(n) be the time required when n items are available.
  • t(0) = t(1) = a, where a is a constant.
  • When t > 1,

t(n) <= 2t(n-1) + b, where b is a constant.

  • t(n) = O(2 n).

Solving dynamic programming recurrences recursively can be hazardous to run time.

Reducing Run Time

f(1,c)

f(2,c ) f(2,c-w 1 )

f(3,c) (^) f(3,c-w 2 ) f(3,c-w 1 ) f(3,c-w 1 –w 2 )

f(4,c) f(4,c-w 3 ) f(4,c-w 2 )

f(5,c)

f(4,c-w1 –w 3 )

f(5,c-w 1 –w 3 –w 4 )