Knapsack

Knapsack: Given a number B and a collection of n items
A = {x[0], x[1], x[2],..., x[n]}, each with some positive integer
value v(x[i]) and some positive integer size s(x[i]), find a subset
A' of A such that the sum of the values of items in A' is greatest
over all subsets of A whose sum of sizes is no greater than B.
Example: B=27
A = { x[0]...x[4] }
v(x[0]) = 23, s(x[0]) = 11
v(x[1]) = 17, s(x[1]) = 7
v(x[2]) = 21, s(x[2]) = 16
v(x[3]) = 26, s(x[3]) = 10
v(x[4]) = 19, s(x[4]) = 9
Solution: { x[1], x[3], x[4] }
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] is in the knapsack
40 : <21,6>, <26,10>, <19,9> : 9
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] is in the knapsack
40 : <21,6>, <26,10>, <19,9> : 9
. x[2] is in the knapsack
61 : <26,10>, <19,9> : 3
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] is in the knapsack
40 : <21,6>, <26,10>, <19,9> : 9
. x[2]
61 : <26,10>, <19,9> : 3
Best value so far: 61
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] is in the knapsack
40 : <21,6>, <26,10>, <19,9> : 9
. x[2]
61 : <26,10>, <19,9> : 3
Best value so far: 61
. x[2] not in the knapsack
40 : <26,10>, <19,9> : 9
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] is in the knapsack
40 : <21,6>, <26,10>, <19,9> : 9
. x[2]
61 : <26,10>, <19,9> : 3
Best value so far: 61
. x[2] not in the knapsack
40 : <26,10>, <19,9> : 9
. x[3] not in the knapsack
40 : <19,9> : 9
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] is in the knapsack
40 : <21,6>, <26,10>, <19,9> : 9
. x[2]
61 : <26,10>, <19,9> : 3
Best value so far: 61
. x[2] not in the knapsack
40 : <26,10>, <19,9> : 9
. x[3] not in the knapsack
40 : <19,9> : 9
. x[4] in the knapsack
59 : : 0
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1]
40 : <21,6>, <26,10>, <19,9> : 9
. x[2]
61 : <26,10>, <19,9> : 3
Best value so far: 61
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1]
40 : <21,6>, <26,10>, <19,9> : 9
. x[2]
61 : <26,10>, <19,9> : 3
Best value so far: 61
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
. x[1]
40 : <21,6>, <26,10>, <19,9> : 9
. x[2]
61 : <26,10>, <19,9> : 3
Best value so far: 61
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
. x[1]
40 : <21,6>, <26,10>, <19,9> : 9
. x[2]
61 : <26,10>, <19,9> : 3
Best value so far: 61
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
. x[1]
40 : <21,6>, <26,10>, <19,9> : 9
. x[2]
61 : <26,10>, <19,9> : 3
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
. x[3] not in the knapsack
44 : <19,9> : 10
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
. x[3] not in the knapsack
44 : <19,9> : 10
Cannot beat 70
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
. x[2] is not in the knapsack
23 : <26,10>, <19,9> : 16
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
. x[2] is not in the knapsack
23 : <26,10>, <19,9> : 16
Cannot beat 70
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
. x[0] not in knapsack
0 : <17,7>, ..., <19,9> : 16
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
. x[0] not in knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
0 : <17,7>, ..., <19,9> : 27
. x[1] in knapsack
17 : <21,6>, <26,10>, <19,9> : 20
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
. x[0] not in knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
. x[1] in knapsack
17 : <21,6>, <26,10>, <19,9> : 20
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
0 : <17,7>, ..., <19,9> : 27
. x[2] in the knapsack
38 : <26,10>, <19,9> : 14
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
. x[0] not in knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
0 : <17,7>, ..., <19,9> : 27
. x[1] in knapsack
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
17 : <21,6>, <26,10>, <19,9> : 20
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
. x[2] in the knapsack
38 : <26,10>, <19,9> : 14
. x[3] in the knapsack
64 : <19,9> : 4
Cannot beat 70
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
. x[0] not in knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
. x[1] in knapsack
17 : <21,6>, <26,10>, <19,9> : 20
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
0 : <17,7>, ..., <19,9> : 27
. x[2] in the knapsack
38 : <26,10>, <19,9> : 14
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
. x[0] not in knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
0 : <17,7>, ..., <19,9> : 27
. x[1] in knapsack
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
17 : <21,6>, <26,10>, <19,9> : 20
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
. x[2] in the knapsack
38 : <26,10>, <19,9> : 14
. x[3] not in the knapsack
38 : <19,9> : 14
Cannot beat 70
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
. x[0] not in knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
0 : <17,7>, ..., <19,9> : 27
. x[1] in knapsack
17 : <21,6>, <26,10>, <19,9> : 20
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
. x[0] not in knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
0 : <17,7>, ..., <19,9> : 27
. x[1] in knapsack
17 : <21,6>, <26,10>, <19,9> : 20
. x[2] in the knapsack
. x[2] not in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
17 : <26,10>, <19,9> : 20
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
. x[0] not in knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
0 : <17,7>, ..., <19,9> : 27
. x[1] in knapsack
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
17 : <21,6>, <26,10>, <19,9> : 20
. x[2] in the knapsack
. x[2] not in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
17 : <26,10>, <19,9> : 20
Cannot beat 70
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
. x[0] not in knapsack
0 : <17,7>, ..., <19,9> : 27
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
. x[0] not in knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
0 : <17,7>, ..., <19,9> : 27
. x[1] not in knapsack
0 : <21,6>, <26,10>, <19,9> : 27
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
. x[0] not in knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Best value so far: 70
0 : <17,7>, ..., <19,9> : 27
. x[1] not in knapsack
0 : <21,6>, <26,10>, <19,9> : 27
Cannot beat 70
0 : <23,11>, <17,7>, <21,16>, <26,10>, <19,9> : 27
Assume x[0] is in the knapsack
. x[0] not in knapsack
23 : <17,7>, <21,6>, <26,10>, <19,9> : 16
. x[1] not in the knapsack
23 : <21,6>, <26,10>, <19,9> : 16
. x[2] in the knapsack
44 : <26,10>, <19,9> : 10
. x[3] in the knapsack
70 : <19,9> : 0
Solution: 70
0 : <17,7>, ..., <19,9> : 27
. x[1] not in knapsack
0 : <21,6>, <26,10>, <19,9> : 27