Skip to content

pack a bag

Pack a bag with the most value it can hold. Race two methods. The knapsack from the top down works out just the cells it is asked for. The knapsack table fills a cell for every item at every room.

The room is how much weight the bag has space for. A cell is the best value the items up to its row can pack into that much room, worked out once. Fewest cells filled wins the race. Beside the verdict, the race says the value each method packed.

In the race below each method has a lane. At each tick of the clock, every lane still running fills one more cell.

Both race on one bag, which holds a weight of 5. A weighs 4 and is worth 50, B weighs 2 and is worth 30, and C weighs 3 and is worth 40. The top-down fills 6 cells, the table 15. Both pack B and C, worth 70.

The knapsack from the top down asks the whole question first: the best of A, B and C in room 5, the last cell. It asks the row above, the same items without C, two questions. Leave C: the best in room 5. Take C: 40, plus the best in room 2, 5 less C's weight 3. Each of those asks the row above in turn, asking to take its item only if it fits. A weighs 4, so A at room 3 and A at room 2 only leave it, 0. No items, or room 0, is 0 at once: no cell is filled. Each answer is kept, so a cell asked twice is filled once.

item12345
none00000
A-00-50
B-30--50
C----70
The cells it fills, rooms 1 to 5. The none row is 0 from the start. A dash is a cell never asked for. 6 of the table's 15, and the last holds 70.

The table, 0/1 knapsack, takes each item whole or leaves it. It fills every cell, row by row: A at rooms 1 to 5, then B, then C. Each cell holds the more of two. Leave its item: the cell above. Take it, if it fits: its value, plus the cell above at the room it leaves.

item12345
none00000
A0005050
B030305050
C030405070
Every cell filled: 3 items times 5 rooms, 15 cells. C at room 5: leave C, 50. Take C: 40 plus B at room 2, 30, which is 70. C at room 5 keeps the larger, 70.

or make one

race these, up to four

one tick is

Without scripts the race cannot run, so each lane shows where its racer ends.

Tick 15: no lane has made more than 15 cells. Knapsack from the top down and 0/1 knapsack have finished.

Every lane packs a bag of 5.

knapsack from the top down

done in 6 cells
012345ABC0000000005003050070

0/1 knapsack

done in 15 cells
012345ABC0000000000505000303050500030405070

what the marks mean

  • a cell a call asks for
  • a cell being filled
  • a cell whose value is final
  • a cell where the best packing takes its item
  • any other cell
  • how much of its race a lane has run

which suits this bag

In a bag of 5, knapsack from the top down filled the fewest cells: 6, against 15 for 0/1 knapsack.

What each lane packed: knapsack from the top down a value of 70 and 0/1 knapsack a value of 70.

each racer's own page

The same racers packing bigger bags with the same items (weights 1, 2, 3 and 4): cells up, the room across, both scales in powers of ten. Each point is counted on that bag.
102104101102103104105106107101102103104101102103104105106107
  • knapsack from the top down
  • 0/1 knapsack

the starting racers, then bigger bags

In a bag of 5, the top-down fills 6 cells to the table's 15. The table fills every room for every item, so a bigger room means more cells. Pack a bag of 1,000 with four items, A to D, weighing 1, 2, 3 and 4. The table fills 4 rows of 1,000 rooms: 4,000 cells. The top-down starts with one cell: D at room 1,000. Each cell asks the row above for at most 2 rooms, so its rows hold at most 1, 2, 4 and 8 cells: 15, whatever the room.

The second row packs a bag of 1,000 with items weighing 1, 2, 3 and 4. The fewest in each row is in bold.

knapsack from the top down0/1 knapsack
the starting items and room4 50, 2 30, 3 40; 5615
a bag of 1,000154,000

the starting racers packing a bag of 5 with each set of items

Each button under the race's box picks a set of items. The race's table below gives both methods' cells in a bag of 5 with each set. The top-down fills the fewest with all five. The table fills every cell, 4 items times 5 rooms: 20. With these five sets the top-down fills 4 to 10 cells. Every cell the top-down fills is one of the table's, filled once, so it never fills more. In a bag of 1 they tie, 4 cells each: every cell is asked for. When each item is as heavy as the bag, the top-down fills 4, as the first picture shows. With 4 items it never fills more than 15, whatever the room, as the second shows. Both always pack the same value. So with a big room and few items, use the top-down. The table fills every cell, but simply: row by row, with no questions to keep track of.

item12345
none00000
A----1
B----2
C----3
D----4
The top-down in a bag of 5, each item weighing 5. Taking an item leaves room 0, which is 0 at once, so each row is asked at room 5 alone: 4 cells, where the table fills 20.
itemweightrooms asked forcells
D41,0001
C31,000, 9962
B21,000, 997, 996, 9934
A11,000, 998, 997, 995, 996, 994, 993, 9918
The top-down in a bag of 1,000, the last item first. Each room asked of an item asks the next row down this list at two rooms: leave the item, the same room; take it, the room less its weight. D weighs 4, so C is asked at 1,000 and 996. C weighs 3, so B is asked at 1,000 and 997, 996 and 993. B weighs 2, so each of B's rooms gives two of A's: 1,000 and 998, 997 and 995, and so on. Each row has at most twice the rooms of the row before: 1 + 2 + 4 + 8 = 15 cells, against the table's 4,000.

Their cells on the bag the race makes with each set of items.

knapsack from the top down0/1 knapsack
four of weight 11 1, 1 2, 1 3, 1 4; 51020
weights 1, 2, 3 and 41 1, 2 2, 3 3, 4 4; 51020
weights 1, 2, 4 and 81 1, 2 2, 4 3, 8 4; 5720
each as heavy as the bag5 1, 5 2, 5 3, 5 4; 5420
four weights at random6 1, 1 2, 5 3, 8 4; 5520