Guides

Stuffle · Guide

The packing problem: what a suitcase teaches computer scientists

You have a bag, a pile of things and not enough room. Which things go in? It sounds like a two-minute chore, but it's one of the most studied problems in mathematics and computer science, with a name of its own: the knapsack problem. Stuffle turns it into a daily puzzle, and the ideas behind it can make you a better packer.

The problem in one sentence

Each item has a size and a value, the bag has a limited size, and you want the most value that fits. In Stuffle, the value is how many stars an item is worth, meaning how much you'd miss it on the trip. The size is its shape on the bag's grid. The same problem turns up wherever space or money is short: loading lorries and shipping containers, cutting shapes out of a sheet of metal or cloth, fitting programs onto servers, or choosing projects for a limited budget.

Why the obvious method fails

Most people pack the same way: the best value for its size first, then keep going while things fit. Computer scientists call this a greedy method. It's quick and often good, but it can miss the best answer. A small example makes it clear:

ItemSizeStarsStars per square
A6 squares71.17
B5 squares51.00
C5 squares51.00

With a bag of 10 squares, the greedy packer takes A first, because it's the best value for its size. Then only 4 squares are left, so neither B nor C fits: 7 stars. Leaving A at home and packing B and C fills the bag exactly: 10 stars. The best bag isn't made of the individually best items. It's the set that fits together best.

Why it gets hard so fast

To be sure you have the best bag, you'd have to consider every possible set of items. Each item is either in or out, so the number of sets doubles with every item: 10 items give 1,024 sets, 20 give over a million, and 40 give more than a trillion. In 1972, Richard Karp included the knapsack problem in a famous list of 21 problems that are “NP-complete”. Roughly speaking, nobody knows a method that stays fast as these problems grow, and finding one would be among the biggest discoveries in computer science.

In practice, that's less scary than it sounds. A technique called dynamic programming, developed by Richard Bellman in the 1950s, solves the classic knapsack problem quickly whenever the bag's size is a modest whole number, by building up the best answer for every smaller bag first. Many real problems are solved exactly this way, and good shortcuts get close enough for the rest.

Shapes make it harder still

The textbook knapsack only cares about total size. A real bag also cares about shape: a laptop and an umbrella might have the same area and still not fit in the same gap. Fitting shapes onto a grid, with the option to turn them, belongs to a family called packing problems, which are even harder than the knapsack. Stuffle adds a third layer with the day's house rule, such as two items that mustn't touch or one that must sit against a side of the bag.

How Stuffle knows the best bag

Each day's trip is checked by a solver before it goes live. It considers sets of optional items from the most stars downwards. For each set, it tries the shapes in the bag's positions and turns, with the must-haves in and the house rule kept, until it finds a set that really fits. That's the “best bag” shown on the results screen. The bag is small enough that this exhaustive search finishes in moments, so the target you aim for is always reachable, and no day is a trick where everything fits.

Packing tips from the theory

Play today's Stuffle

Sources and further reading