Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Example-0/1 Knapsack Problem Again

Consider again the 0/1 knapsack problem described in Section gif. We are given a set of n items from which we are to select some number of items to be carried in a knapsack. The solution to the problem has the form tex2html_wrap_inline68635, where tex2html_wrap_inline68477 is one if the tex2html_wrap_inline58387 item is placed in the knapsack and zero otherwise. Each item has both a weight, tex2html_wrap_inline68467, and a profit, tex2html_wrap_inline58415. The goal is to maximize the total profit,

displaymath68629

subject to the knapsack capacity constraint

displaymath68462

A partial solution to the problem is one in which only the first k items have been considered. I.e., the solution has the form tex2html_wrap_inline68647, where tex2html_wrap_inline68649. The partial solution tex2html_wrap_inline68651 is feasible if and only if

  equation32825

Clearly if tex2html_wrap_inline68651 is infeasible, then every possible complete solution containing tex2html_wrap_inline68651 is also infeasible.

If tex2html_wrap_inline68651 is feasible, the total profit of any solution containing tex2html_wrap_inline68651 is bounded by

  equation32830

I.e., the bound is equal the actual profit accrued from the k items already considered plus the sum of the profits of the remaining items.

Clearly, the 0/1 knapsack problem can be solved using a backtracking algorithm. Furthermore, by using Equations gif and gif a branch-and-bound solver can potentially prune the solution space, thereby arriving at the solution more quickly.

For example, consider the 0/1 knapsack problem with n=6 items given in Table gif. There are tex2html_wrap_inline68665 possible solutions and the solution space contains tex2html_wrap_inline68667 nodes. The simple DepthFirstSolver given in Program gif visits all 127 nodes and generates all 64 solutions because it does a complete traversal of the solution tree. The BreadthFirstSolver of Program gif behaves similarly. On the other hand, the DepthFirstBranchAndBoundSolver shown in Program gif visits only 67 nodes and generates only 27 complete solutions. In this case, the branch-and-bound technique prunes almost half the nodes from the solution space!


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.