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.
| item | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| none | 0 | 0 | 0 | 0 | 0 |
| A | - | 0 | 0 | - | 50 |
| B | - | 30 | - | - | 50 |
| C | - | - | - | - | 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.
| item | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| none | 0 | 0 | 0 | 0 | 0 |
| A | 0 | 0 | 0 | 50 | 50 |
| B | 0 | 30 | 30 | 50 | 50 |
| C | 0 | 30 | 40 | 50 | 70 |
or make one
race these, up to four
Choose two to four racers.
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.
No tick the race offers is counted by every one of these racers, so one clock cannot time them. Choose others.
Every lane packs a bag of 5.
knapsack from the top down
done in 6 cells0/1 knapsack
done in 15 cellswhat 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
- 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 down | 0/1 knapsack | |
|---|---|---|
| the starting items and room4 50, 2 30, 3 40; 5 | 6 | 15 |
| a bag of 1,000 | 15 | 4,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.
| item | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| none | 0 | 0 | 0 | 0 | 0 |
| A | - | - | - | - | 1 |
| B | - | - | - | - | 2 |
| C | - | - | - | - | 3 |
| D | - | - | - | - | 4 |
| item | weight | rooms asked for | cells |
|---|---|---|---|
| D | 4 | 1,000 | 1 |
| C | 3 | 1,000, 996 | 2 |
| B | 2 | 1,000, 997, 996, 993 | 4 |
| A | 1 | 1,000, 998, 997, 995, 996, 994, 993, 991 | 8 |
Their cells on the bag the race makes with each set of items.
| knapsack from the top down | 0/1 knapsack | |
|---|---|---|
| four of weight 11 1, 1 2, 1 3, 1 4; 5 | 10 | 20 |
| weights 1, 2, 3 and 41 1, 2 2, 3 3, 4 4; 5 | 10 | 20 |
| weights 1, 2, 4 and 81 1, 2 2, 4 3, 8 4; 5 | 7 | 20 |
| each as heavy as the bag5 1, 5 2, 5 3, 5 4; 5 | 4 | 20 |
| four weights at random6 1, 1 2, 5 3, 8 4; 5 | 5 | 20 |