Dynamic Programming-Lecture 7
Dynamic Programming-Lecture 7
Algorithms
• Dynamic programming, like the divide-and-conquer method, solves problems by combining the solutions to
subproblems.
• Divide-and-conquer algorithms partition the problem into disjoint subproblems, solve the subproblems recursively, and
then combine their solutions to solve the original problem
• In contrast, dynamic programming applies when the subproblems overlap—that is, when subproblems share sub-sub-
problems. In this context, a divide-and-conquer algorithm does more work than necessary, repeatedly solving the
common sub-sub-problems.
Dynamic Programming
• A dynamic-programming algorithm solves each sub-sub-problem just once and then saves its answer in a table, thereby
avoiding the work of recomputing the answer every time it solves each sub-sub-problem.
• We typically apply dynamic programming to optimization problems. Such problems can have many possible solutions.
Each solution has a value, and we wish to f ind a solution with the optimal (minimum or maximum) value. We call such
a solution an optimal solution to the problem
Rod Cutting
• Use dynamic programming to solve a simple problem in deciding where to cut steel rods.
• Serling Enterprises buys long steel rods and cuts them into shorter rods, which it then sells. The management of
Serling Enterprises wants to know the best way to cut up the rods.
• The rod-cutting problem is the following. Given a rod of length n inches and a table of prices pi for i = 1,2……..n,
determine the maximum revenue rn obtain able by cutting up the rod and selling the pieces. Note that if the price p n
for a rod of length n is large enough, an optimal solution may require no cutting at all.
Rod Cutting
• We can cut up a rod of length n in different ways, since we have an in
dependent option of cutting, or not cutting, at distance i inches from the left
end.
Rod Cutting
Recursive top-down implementation
Dynamic programming for optimal rod cutting
• If we need to refer to this subproblem’s solution again later, we can just look it.
• Bottom-up method
Top-Down with Memoization
Top-Down with Memoization
Thank you