Knapsack Problem Algorithm Analysis
Knapsack Problem Algorithm Analysis
Columbia University
1. Introduction
This paperpresentsa branchand boundalgorithmfor the solutionof a special
type of combinatorialproblem. The problem which is sometimes called the
knapsack problem arises in various cargo loading situations and consists of select-
ing from a finite collection of objects that subcollection which maximizes a
linear function of the objects selected while obeying a single linear inequality
constraint.
This problem may be formulated and solved in a variety of ways. The method
proposed here is of interest in that it provides initially a solution which is ap-
proximately optimal and also an upper bound on the value of the optimal solu-
tion. Thus, computation may be terminated at any point where it is felt that
further improvements in the solution are not worth the additional computational
effort. The nature of the algorithm is such that the approximation tends to be
good for problems involving a large number of items where each item makes a
small contribution to the objective function and to the restriction. Further, it
appears that the efficiency of the algorithm increases as the size of the problem
increases. Nonetheless, as with other branch and bound [8] algorithms, the
memory and time requirements are quite large for problems involving many
items.
As will be shown in Part 2 of this paper, the problem may be formulated as an
integer linear programming problem and may be solved using either the all in-
teger cutting plane method of Gomory [5] or the more specialized algorithm of
Balas [7] for linear programs with 0, 1 variables. Still another approach to the
problem is offered by dynamic programming, and Bellman and Dreyfus [2]
have treated slightly different versions of the problem in this way. Fulkerson has
formulated the problem as a network flow problem. The solution is obtained by
finding the longest chain of an acyclic network. Although this is a quite straight-
* Received March 1966 and revised November 1966.
t Research supported in part by the Office of Naval Research under contract Nonr 266
(55), and the National Science Foundation under grant GP 2202. Reproduction in whole or
in part is permitted for any purpose of the United States Government.
723
724 PETER J. KOLESAR
3. The Algorithm
We call the algorithm which will be proposed here a branch and bound al-
gorithm in the sense of Little, et al. [13]. In the following paragraphs we introduce
some terminology and notation, discuss generally the concepts on which the
branch and bound algorithm is based, and then present the details of the specific
branching and bounding procedure proposed.
General
We adopt the following terminology. A collection of xi satisfying xi _ 1,
i = 1, 2, * , N and (2) will be called a solution or a loading. Notice that this
permits violation of the assumption that each item is indivisible. A collection of
xi satisfying (2) and (3), that is, obeying the indivisibility assumption, will be
called a feasible solution.
A branch and bound algorithm proceeds by repeatedly partitioning the class
of all feasible solutions into smaller and smaller subclasses in such a way that
ultimately an optimal solution is obtained. The partitioning, or branching as it
is called, is carried out so that at each partitioning the subclasses of solutions
are mutually exclusive and all inclusive, that is, each feasible solution belongs
to exactly one subclass. For each of the subclasses of solutions one computes an
upper bound to the maximum value of the objective function attained by solu-
tions belonging to the subclass. On the basis of these upper bounds, a further
stage of branching is carried out, new upper bounds are computed, and so on,
repeatedly, until ultimately a feasible solution is obtained which has a value of
the objective function greater than the upper bounds of all the subclasses and
also higher than the values of the objective function for all previously obtained
solutions. The algorithm is constructive in nature. It repeats the branching
process, successively generating smaller and smaller subclasses some of which
ultimately will contain only one feasible solution, one of which will be optimal.
The operation of a branch and bound algorithm may be visualized in terms of
the construction of a tree whose nodes represent the subclasses of solutions.
Such a tree is shown in Figure 1. We index the nodes by n 1, 2, *. in the
ALL
SOLUTIONS NODE L
FIG. 1. AtypicalNODE 3
X NODE 2 ND 5
( im NODE 4
/ ( j~,m*r) NODE7
ANODE 6
order in which they are generated by the algorithm. We denote by B(n) the
upper bound associated with node n. A node that has not been branched from
is a terminal node. Branching takes place at the terminal node which has the
highest value of B(n) and is accomplished by creating two new descendent
nodes.
We describe a node, that is a subclass of feasible solutions, by listing the items
which are explicitly included in the solutions (loadings) contained in the sub-
class, and the items which are explicitly excluded from the solutions which are
contained in the subclass. Thus at a given node n, we speak of three categories
of items:
Included Items
The set of items which are explicitly included in the solutions contained in
node n will be denoted by In~. In Figure 1 we indicate the members of I,"at each
node by listing their indices.
Excluded Items
The set of items which are explicitly excluded from the solutions contained
in node n will. be denoted by E,,. In Figure 1 we indicate the members of E,, at
each node by listing their indices with star superscripts.
Unassigned Items
The items which belong to neither I,, or to E. have not yet been specifically
assigned. When the set of unassigned items at any node 'is empty, the node con-
tains only one solution and further branching from that node is impossible.
Branching at a node takes place in the following way: One of the previously
unassigned items is selected; in one descendent node it becomes included and in
the other it becomes excluded. Upon performing the branching, the algorithm
checks the feasibility of the solutions contained in the two new nodes. If the sub-
class of solutions contained in a given node is infeasible, further branching from
that node is eliminated; if the subclass of solutions is feasible, the upper bound
is computed and the process continues. The details of the branching and bound-
ing processes are given below.
In the same tree shown in Figure 1, node 1 represents the class of all feasible
solutions. Node 2 represents all feasible solutions which do not include item j,
while node 3 represents all feasible solutions which do include item j. Node 7
represents all feasible solutions containing j and r but not m. The sets I, , E,,
for the nodes shown in Figure 1 are:
I2 (O E2=(j)
14(j) E4 (M)
15 =(j,rm) E5 =0
A BRANCH AND BOUND ALGORITHM FOR THE KNAPSACK PROBLEM 727
16 = (i) E6 = (m, r)
17 = (j, r) E7 = (m)
(8) X = 1, iEIn
(9) Xi = 0, iEEn
(10) 0 < Xi < 1 iE(InUEn)
The solution of this system is an upper bound at node n by application of the
first observation since in (10) we have relaxed restriction (3) for the unassigned
items at node n. The second observation gives a direct solution to this system
since by a simple transformation it can be put in the form of (4), (5), (6). The
solution is given by:
728 PETER J. KOLESAR
(a) Order all items in (IrzUEn)cby decreasing vl/wi and starting with the
first item in this list load as much as possible of each item in sequence
until the total weight of the items so loaded exactly equals W - E win , .
(b) The upper bound at node n, B(n), is equal to the total value loaded in
(a) aboveplus EZa, v .
In order to perform the branching operation, two decisions must be made.
First, one must select the terminal node at which to make the next branch, and
second one must select the item which will be added to the list of included and
excluded items in the two resulting descendent nodes. We call this the "pivot"
item. The selection of the node at which to make the next branch is clearly
dictated by the branch and bound method to be the terminal node with the
highest value of B(n). On the other hand, selection of the pivot element is arbi-
trary. Clearly, one would like to make this selection in a manner that would
enable the algorithm to reach an optimum solution as soon as possible. The
solution to (4), (5), (6) suggests an heuristically good choice. Assuming that
the original items have been arranged according to decreasing order of magni-
tude of vl/wi , we select as the pivot element the unassigned variable with largest
vl/w . In the case of a tie, select that item which appears first in the sequence.
Stage 1 (Preliminary)
(a) Test the nontrivial feasibility of the problem by verifying at least one
indexi = 1,2,... ,N,
Wi < W
Example
The operation of the algorithm is illustrated with the following example. Con-
sider a problem with seven items whose weight and values are given below.
Item No. Weight Value
1 40 40
2 50 60
3 30 10
4 10 10
5 10 3
6 40 20
7 30 60
The total allowable weight in the load W = 100. A preliminary test reveals
that the problem possesses a nonempty feasible solution and is not trivial, and
Ewi > 100. We compute the ratios v/lwi and reorder the items. They are given
below with the new indexing.
New 1hdex ItemnNo. Weight Value Ratio
1 7 30 60 2
2 2 50 60 6/5
3 1 40 40 1
4 4 10 10 1
5 6 40 20 1/2
6 3 30 10 1/3
7 5 10 3 3/10
The first node shown in Figure 2 is that including all possible solutions. The
first branching uses index 1 as the first pivot and at node 2 where this index is
excluded from the solution the upper bound is computed by
B(2) = V2 + V3 + V4 = 110.
14 2 NODE 2,3
( 1( l) O-
NODE 2 i _2 < \NODE 3
1silo1
B(2)X10 B(3) 140
NODE
NODEj4
z < NODE 7T ,.
NODE6 for ea (7 999
B(8)6= 135(ND
NQDE
12 /NODE
NOD I
3,
For purposes of comparison, we present the network flow solution to this ex-
ample. The problem is that of finding the largest chain in the following acyclic
network. The nodes of the network are denoted by the couple (i, w) where i
denotes the index of the i2 item i = 0, 1, *--, N and w takes on all integers
w =0, 1, *- W. We assume that the item weights are normalized to integers.
Each node (i, w) has two arcs leading into it. One arc leads from node (i -1, w)
and has length zero; the second arc leads from node (i -1, w - w) and has
length vi. We add to the network a starting node which is Joined to all nodes
(0, w) with arcs of length zero. Thus a chain from the starting node to node
(i, w) corresponds to a subset of the first items whose total weight is w and the
length of the chain is the total value of the subset. The network for this example
is given in Figure 3.
(00
90
0 ~~~~~~~~~~~~
0~~~~~~~~~~~~~~~~~~~7
0 7~~~~~~~~~~~
30
20
0~~~~~~~~~~~~~~~~~~~~~~I
0 0~~~~~~/ 4
0 0 ~~~~~~~~~~~~~~~~~~~~~~~~30
i=0 I 2 3 4 5 6 7
ITEM NUMBERS
accounting data or the like, where the loading problem is itself merely a sub-
optimization of a much larger problem etc., the word "optimal" loses an opera-
tional meaning. With this in mind, we propose a method of generating nearly
optimal solutions accompanied by an upper bound on the difference between
the value from the optimal policy and the nearly optimal policy.
The procedure is as follows:
1. Carry out preliminary computation by always branching to the right (see
Figure 2) in the algorithm until an infeasible loading is obtained. The last fea-
sible loading obtained in this branching consists of loading as much as possible
of each item as ordered by the ratio of vi/ws. Let this be the "candidate nearly
optimal solution".
2. Compare the value of the "candidate nearly optimal solution" to the cur-
rent upper bound on the value of all solutions. If the difference is less than some
present tolerance, terminate the algorithm and use the candidate solution;
otherwise go to Stage 3.
3. Employ the algorithm in the ordinary manner until a new feasible loading
is obtained; which has a value greater than that of the "candidate nearly opti-
mal solution". If this solution is optimal, the algorithm terminates; if not, make
this solution the new candidate solution, and go to Stage 2.
The dominance ideas employed by Ignall and Schrage [10] in their branch
and bound algorithm for job shop scheduling may also be employed in the al-
gorithm to eliminate nodes from further consideration if the following conditions
exist:
If for nodes n and m
(i) (ImUEm)CC (InuEn)c,
732 PETER J. KOLESAR
yi = 0, 1, 2, *--
whereWi(yi) is a monotonically
increasing
functionof yi andvi(yi) is an arbi-
trary function of yi. The problemmay be formulatedas a kind of knapsack
problem by representing
yi = Ej xij
wherexij = 0, 1. However,the additionalconstraintxij > xij+l may be neces-
sary to assurethat the jth of a given item type is loadedif the j + lt is loaded.
In cases in which the objectivefunctionis concaveand the constraintsdefine a
convex set, the constraintmay not need to be explicitly employed.In cases in
which the constraintmust be explicitly employed,one can define for node n a
set Cn of unassignedbut unassignableitems and then computeupperboundsby
solving ordinarylinearprogrammingproblemsof the form:
maximize
i= Xtii
subject to
zjX=1x1 i < W
where
xi = 1 for icIn
i= 0 for icEn
0 ? XL ? 1 for i(InUEn)cUCn .
The next pivot item must be selectedfrom the set IcflnEncflncz.
In [11] Kolesar
treats a problemof optimaldesignof redundancyin networkswhich are subject
to two types of failureby formulatingthe problemas a knapsackproblemhaving
orderrelationshipsbetweenthe variables.
A BRANCH AND BOUND ALGORITHM FOR THE KNAPSACK PROBLEM 733
7. Computational Experience
The version of the algorithm presented in Section 4 has been coded in Fortran
IV for the IBM 7094 computer. Branch and bound algorithms involve a signifi-
cant amount of list processing for which Fortran is not designed. Consequently,
our code is relatively unsophisticated and becomes inefficient as the number of
nodes generated becomes large. It is possible to construct a code which would
solve larger problems more efficiently.
Approximately 500 sample problems have been generated and solved on the
IBM 7094 computer. The number of items ranged from 3 to 100. Item weights
and values were generated randomly as approximately independent observations
on a normally distributed random variable with expected value of 5.00 and
standard deviation of 0.91. The algorithm was run until either an optimal solu-
tion was obtained or until the node list reached 3000.
Solution times depend strongly on the number of nodes generated by the al-
gorithm, increasing approximately exponentially with the number of nodes
generated. See Figure 4.
The number of nodes generated, and hence the solution time, depends both
on the number of items and on the maximum permissible weight of the knapsack.
Another way to say this is that the difficulty of the problem depends on the num-
ber of items to be selected and on the number of items to be selected from.
Table 1 below gives the median number of nodes generated in solving ten prob-
lems for each of the combinations of the number of items N, and the total per-
missible weight W indicated.
A set of 160 problems were solved for which the number of items selected from
was 25 and the maximum permissible weight was 50. The problem was roughly to
1.00
0.90 /
0.80 _
W 0.70 -
Z 0.60 -
z 0.50 -
0
D 0.40-
0
0.30 -
0.20 -
0.10 _
TABLE 1
Median Number of Nodes Generated
W= 25 W= 50 W= 75 W= 100 W= 125
50
45
40-
35-
0>- 30-
z
0 =>25-
2 20-.
15-
10-
5-
0A
0 400 800 1200 I600 2000 2400 2800 +
NODES GENERATED
FIG. 5. Histogram of number of nodes generated in solving 160 knapsack problems with
N = 25, W = 50.
pick about half of the 25 items for inclusion in the knapsack. Figure 5 gives a
histogram of the number of nodes generated to solve these problems. Since com-
putation was terminated when the node list reached 3000, this data is a trun-
cated sample. The number of nodes generated appears to have an exponential
type distribution with a mean of approximately 484 nodes. The average time to
generate and solve these problems was 0.07 minutes each. The algorithm termi-
nated before an optimal solution was reached in 3.1 % of the problems solved.
Preliminary experience using an algorithm incorporating node dominance
indicates that solution times are longer than when node dominance is not em-
ployed, but larger problems may be solved.
References
1. BALAS, E., "Un algorithm additif pour la resolution des programmes lineaires in varia-
bles bivalentes, ComptesRendus de l'Academic des Sciences, Paris, Vol. 258, 1964, pp.
3817-3820.
2. BELLMAN,R. E. AND DREYFUS, S. E., Applied Dynamic Programming,Princeton Uni-
versity Press, 1962, pp. 27-31.
3. CORD, J., "A Method for Allocating Funds to Investment Projects when Returns Are
Subject to Uncertainty," ManagementScience, Vol. 10, No. 2 (1964), pp. 335-341.
A BRANCHAND BOUND ALGORITHM
FOR THE KNAPSACKPROBLEM 735