6

Click here to load reader

Knapsack Backtrack

Embed Size (px)

Citation preview

Page 1: Knapsack Backtrack

8/9/2019 Knapsack Backtrack

http://slidepdf.com/reader/full/knapsack-backtrack 1/6

Backtracking Algorithms for the Knapsack Problem

CS 341 Notes by D.R. Stinson

1 Introduction

A  backtracking algorithm  is a recursive method of building up feasible solutions to a combi-natorial optimization problem one step at a time. A backtracking algorithm is an exhaustive 

search , that is, all feasible solutions are considered and it will thus always find the optimalsolution.   Pruning  methods can be used to avoid considering some feasible solutions that arenot optimal.

To illustrate the basic principles of backtracking, we consider the   Knapsack   problem.Recall that a problem instance consists of a list of profits,  P   = [ p1, . . . , pn]; a list of weights,W   = [w1, . . . , wn]; and a capacity,  M . These are all positive integers. It is required to findthe maximum value of 

  pixi   subject to

 wixi  ≤ M   and  xi  ∈ {0, 1}  for all   i. An  n-tuple

[x1, x2, . . . , xn] of 0’s and 1’s is a feasible solution if  wixi ≤  M.One naive way to solve this problem is to try all 2n possible n-tuples of 0’s and 1’s. We can

build up an n-tuple one coordinate at a time by first choosing a value for x1, then choosing avalue for x2, etc. Backtracking provides a simple method for generating all possible n-tuples.After each   n-tuple is generated it is checked for feasibility. If it is feasible, then its profitis compared to the current best solution found to that point. The current best solution isupdated whenever a better feasible solution is found.

We will denote by   X   = [x1, . . . , xn] the current   n-tuple being constructed, and   CurP 

will denote its profit.   OptX  will denote the current optimal solution and  OptP   is its profit.A recursive backtracking algorithm for the   Knapsack   problem is presented now. In thisalgorithm,   denotes the coordinate being chosen at any given time. When   =  n + 1, all  n

coordinates have been chosen.

Algorithm 1.1:   Knapsack1()global   X, OptX , OptP 

if   =  n + 1

then

if n

i=1

wixi ≤ M 

then

CurP  ←n

i=1

 pixi

if   CurP  > OptP 

then OptP  ← CurP 

OptX   ← [x1, . . . , xn]

else

x ← 1Knapsack1( + 1)x ← 0Knapsack1( + 1)

1

Page 2: Knapsack Backtrack

8/9/2019 Knapsack Backtrack

http://slidepdf.com/reader/full/knapsack-backtrack 2/6

Page 3: Knapsack Backtrack

8/9/2019 Knapsack Backtrack

http://slidepdf.com/reader/full/knapsack-backtrack 3/6

Algorithm 2.1:   Knapsack2(, CurW )global   X, OptX , OptP 

if   =  n + 1

then

if n

i=1

 pixi  > OptP 

then

OptP  ←n

i=1

 pixi

OptX   ← [x1, . . . , xn]

else

if  CurW  + w ≤  M 

then

x ←  1Knapsack2( + 1, CurW  + w)x ←  0Knapsack2( + 1, CurW )

else

x ←  0Knapsack2( + 1, CurW )

Whenever  CurW  + w > M , we have a situation known as pruning . This means that oneof the two recursive calls (the one corresponding to  x  = 1) is not performed. Because wedo not traverse the corresponding subtree of the state space tree, we think of this subtree ashaving been pruned.

3 Bounding Functions

A more sophisticated method of pruning is to use a bounding function. We require a fewpreliminary definitions, which will apply to any backtracking algorithm for a maximizationproblem. Let profit(X ) denote the profit for any feasible solution  X . For a partial feasible

solution, say   X   = [x1, . . . , x−1], define   P(X ) to be the maximum profit of any feasiblesolution which is a descendant of  X   in the state space tree. In other words,   P(X ) is themaximum value of   profit(X ) taken over all feasible solutions   X  = [x

1, . . . , x

n] such that

xi  =  x

i  for 1  ≤  i  ≤   − 1. It follows from the definition that, if  X  = [ ], then   P(X ) is the

optimal profit of the given problem instance.In general,  P(X ) could be computed exactly by traversing the subtree with root node  X .

In order to avoid doing this, if possible, we will employ a bounding function. A   bounding 

 function   is a real-valued function, denoted  B, defined on the set of nodes in the state spacetree and satisfying the following condition:

For any feasible partial solution  X ,  B(X ) ≥  P(X ).

This property says that  B(X ) is an upper bound on the profit of any feasible solution thatis a descendant of  X  in the state space tree. (For a minimization problem, the definition isthe same, except that the inequality is reversed.)

Once a bounding function   B(X ) has been specified, it can be used to prune the statetree, as follows. Suppose that at some stage of the backtracking algorithm, we have a current

3

Page 4: Knapsack Backtrack

8/9/2019 Knapsack Backtrack

http://slidepdf.com/reader/full/knapsack-backtrack 4/6

partial solution   X   = [x1, . . . , x−1], and   OptP   is the current optimal profit. WheneverB(X ) ≤  OptP , then we have that

P(X ) ≤  B(X ) ≤  OptP .

This implies that no descendants of  X  in the state space tree can improve the current optimalprofit. Hence we can prune this entire subtree, i.e., we perform no recursive calls from thispartial solution X .

We now show how to define a useful bounding function for the  Knapsack problem using theRational Knapsack   problem. Recall that the  Rational Knapsack  problem is exactly the sameas the   Knapsack  problem we have been considering, except that the condition   xi   ∈ {0, 1}is replaced by the weaker condition 0   ≤   xi   ≤   1 (i.e., the   xi’s are allowed to be arbitraryrational numbers in the interval [0, 1]). Recall also that the Rational Knapsack problem can beefficiently solved by a greedy algorithm, in which we consider the objects in non-decreasingorder of their profit-to-weight ratios.

Assume we have a greedy algorithm   GreedyRKnap(k; p1, . . . , pk, w1, . . . , wk, M ) to find

the optimal solution to the  Rational Knapsack   problem for an arbitrary problem instance p1, . . . , pk, w0, w1, . . . , wk, M  which consists of  k  objects. We use the algorithm  GreedyRKnap

to define a bounding function for the Knapsack problem as follows. Given a (feasible) partialsolution X  = [x1, . . . , x−1] for  Knapsack, define

B(X ) =−1i=1

 pixi + GreedyRKnap(n − + 1; p, . . . , pn, w, . . . , wn, M  −−1i=1

wixi)

=−1i=0

 pixi + GreedyRKnap(n − + 1; p, . . . , pn, w, . . . , wn, M  − CurW )

Thus,  B(X ) is equal to the sum of:

1. the profit already obtained from objects 1, . . . , − 1,

plus

2. the profit from the remaining n − + 1 objects, using the remaining capacity

M  − CurW ,

but allowing rational xi’s.

If we restricted each  xi  to be 0 or 1 in part 2 above, then we would obtain  P(X ). Allowingthe   xi’s with    ≤   i  ≤  n  to be rational may yield a higher profit, so   B(X )  ≥   P(X ) and   B

is indeed a bounding function. It is also easy to compute, and thus this may be useful forpruning.

Suppose we want to solve an instance of the  Knapsack problem. It will be useful to sort theobjects in decreasing order of profit/weight ahead of time, before we begin the backtracking

4

Page 5: Knapsack Backtrack

8/9/2019 Knapsack Backtrack

http://slidepdf.com/reader/full/knapsack-backtrack 5/6

algorithm. Then, when we wish to evaluate our bounding function for a subset of objects , . . . , n, they are already given the appropriate order. Thus, we will assume that

 p1w1

≥ · · · ≥  pn

wn

.

The improved algorithm is given as Algorithm 3.1.

Algorithm 3.1:   Knapsack3(, CurW )external   GreedyRKnap()global   X, OptX , OptP 

if   =  n + 1

then

if n

i=1

 pixi  > OptP 

then

OptP  ←ni

 pixi

OptX   ← [x1, . . . , xn]

else

B ←−1i=1

 pixi + GreedyRKnap(n − + 1; p, . . . , pn, w, . . . , wn, M  − CurW )

if  B ≤ OptP    then return

if  CurW  + w ≤  M 

then

x ← 1Knapsack3( + 1, CurW  + w)

if  B ≤ OptP    then return

x ← 0Knapsack3( + 1, CurW )

It is very important to note that we check to see if the pruning condition,   B   ≤  OptP ,is true before every recursive recursive call made during the algorithm. This is because thevalue of   OptP  can increase as the algorithm progresses, and so we check to see if we canprune every time we are preparing to choose a new value for  x.

4 An Example

Suppose we have five objects, having weights 11, 12, 8, 7, 9; profits 23, 24, 15, 13 and 16 (re-spectively); and capacity M  = 26. Note that the objects are already arranged in decreasingorder of profit/weight. We draw in Figure 2 the space tree traversed in the course of the

backtrack algorithm Knapsack3. At each node, we record the current values of  X ,  B(X ) andCurW .Observe that pruning using the bounding function happens three times in this example:

1. When  X   = [1, 0] and [1, 0, 1], we have   OptP   = 51 after the first recursive call. SinceB(X ) = 51, the second recursive call is not performed. Therefore the subtrees withroot nodes [1, 0, 0] and [1, 0, 1, 0] are pruned.

5

Page 6: Knapsack Backtrack

8/9/2019 Knapsack Backtrack

http://slidepdf.com/reader/full/knapsack-backtrack 6/6

2. When  X  = [0], we have  OptP  = 51 and  B(X ) = 50.14 <  51, so no recursive calls areperformed. The subtree with root node [0] is pruned.

Also, pruning due to the capacity being exceeded occurs at nodes [1 , 1], [1, 1, 0], [1, 1, 0, 0]and [1, 0, 1, 1]. At each of these nodes, the first recursive call, with  x = 1, is not performed.

CurW   = 11

X  = [1, 0, 1]B = 51

B = 51

X  = [ ]B = 52.625CurW   = 0

X  = [1]B = 52.625CurW   = 11

X  = [1, 1]B = 52.625CurW   = 23

X  = [1, 1, 0]B = 52.57

X  = [1, 1, 0, 0]B = 52.33CurW   = 23

CurW   = 23

P = 47, so set  OptP  = 47X  = [1, 1, 0, 0, 0]

CurW   = 23

X  = [0]B = 50.14 < 51CurW   = 0

X  = [1, 0]B = 51

CurW   = 19

X  = [1, 0, 1, 1]

CurW   = 26

X  = [1, 0, 1, 1, 0]P = 51 >  OptP ,so set  OptP  = 51

CurW   = 26

Figure 2: The state space tree traversed by  Knapsack3.

6