12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 16.1 Dynamic Programming 533dynamic control systems, which got its start before what we think of as computerprogramming. The act of s<strong>to</strong>ring precomputed values in a table for later reuse isreferred <strong>to</strong> as “programming” in that field.Dynamic programming is a powerful alternative <strong>to</strong> the st<strong>and</strong>ard principle ofdivide <strong>and</strong> conquer. In divide <strong>and</strong> conquer, a problem is split in<strong>to</strong> subproblems,the subproblems are solved (independently), <strong>and</strong> then recombined in<strong>to</strong> a solutionfor the problem being solved. Dynamic programming is appropriate whenever thesubproblems <strong>to</strong> be solved are overlapping in some way <strong>and</strong> if we can find a suitableway of doing the necessary bookkeeping. Dynamic programming algorithms areusually not implemented by simply using a table <strong>to</strong> s<strong>to</strong>re subproblems for recursivecalls (i.e., going backwards as is done by Fibrt). Instead, such algorithms typicallyimplemented by building the table of subproblems from the bot<strong>to</strong>m up. Thus,Fibi better represents the most common form of dynamic programming than doesFibrt, even though it doesn’t need the actual table.16.1.1 The Knapsack ProblemWe will next consider a problem that appears with many variations in a varietyof industrial settings. Many businesses need <strong>to</strong> package items with the greatestefficiency. One way <strong>to</strong> describe this basic idea is in terms of packing items in<strong>to</strong>a knapsack, <strong>and</strong> so we will refer <strong>to</strong> this as the Knapsack Problem. First definea particular form of the problem, <strong>and</strong> then discuss an algorithm for it based ondynamic programming. We will see other versions of the problem in the exercises<strong>and</strong> in Chapter 17.Assume that we have a knapsack with a certain amount of space that we willdefine using integer value K. We also have n items each with a certain size suchthat that item i has integer size k i . The problem is <strong>to</strong> find a subset of the n itemswhose sizes exactly sum <strong>to</strong> K, if one exists. For example, if our knapsack hascapacity K = 5 <strong>and</strong> the two items are of size k 1 = 2 <strong>and</strong> k 2 = 4, then no suchsubset exists. But if we have add a third item of size k 3 = 1, then we can fill theknapsack exactly with the first <strong>and</strong> third items. We can define the problem moreformally as: Find S ⊂ {1, 2, ..., n} such that∑k i = K.i∈SExample 16.1 Assume that we are given a knapsack of size K = 163<strong>and</strong> 10 items of sizes 4, 9, 15, 19, 27, 44, 54, 68, 73, 101. Can we find asubset of the items that exactly fills the knapsack? You should take a fewminutes <strong>and</strong> try <strong>to</strong> do this before reading on <strong>and</strong> looking at the answer.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!