.BG
.FN napsack
.TL
Solve Knapsack Problems
.CS
napsack(x, target, best=10)
.AG x
vector of generators for knapsack problem.  The function
attempts to find subsets of `x' whose sums are equal (or
close to) `target'.
.AG target
scalar target value.
.AG best
the desired number of solutions (or approximate solutions)
to the problem.
.RT
matrix of logical values, of size `length(x)' by `best'.  Each
column tells which elements of `x' are contained in one of
the `best' subsets.  Thus, a column of the result can be
used to subscript `x' to obtain a subset.
.PP
The knapsack problem is NP-complete, and the algorithm is exponential
in time and space complexity.  If `n' is the length of `x', then the
algorithm requires time O( 2^(n/2) ) and space O( 2^(n/4) ).  Problems
with `n'<30 can be readily solved by this function.  Remember that
both time and space requirements increase very rapidly with problem
size!
.PP
The solutions produced may not include all subsets that can generate
solutions with a certain error.  It is guaranteed to produce an exact
solution if one is possible, but may not find all of a number of exact
solutions.
.SP
.SH REFERENCE
Richard Schroeppel and Adi Shamir,
"A T*S^2 = O(2^n) Time/Space Tradeoff for Certain NP-Complete Problems",
.ul
Twentieth Symposium in Foundations of Computer Science,
October 1979.
.EX
# given areas of counties of Nevada, find subsets of the
# counties with approximately 1/2 the total state area
subsets <- napsack(nevada,sum(nevada)/2)
crossprod(subsets, nevada)   # areas of the subsets
.KW algebra
.WR
