hard problems
why it works
what it is
Some problems have no fast method that anyone knows. This page says what fast means, why a table can look fast and not be, and what P and NP mean.
Take some numbers and a target. Can some of the numbers add up to exactly the target? Say 2, 3, 4 and 6, and a target of 7. One method fills a table: a column for each number, a row for each total from 0 to the target. A cell says yes when its total can be reached with the numbers up to its column. That is 4 columns of 8 cells, 32 in all, one step each. 7 is reached: 3 + 4.
| total | with 2 | with 3 | with 4 | with 6 |
|---|---|---|---|---|
| 0 | yes | yes | yes | yes |
| 1 | ||||
| 2 | yes | yes | yes | yes |
| 3 | yes | yes | yes | |
| 4 | yes | yes | ||
| 5 | yes | yes | yes | |
| 6 | yes | yes | ||
| 7 | yes | yes |
A method is called fast when its steps grow no faster than a fixed power of the input's length: the length, its square, its cube. Books call that polynomial time. The length is the count of digits in the input. 2, 3, 4, 6 and 7 are 5 digits.
The table has a row for each total, so the target's value sets its cells, not how many digits the target has. With 4 numbers, a target of 100, 3 digits, takes 404 cells. One digit more, 1000, takes 4,004. The input grew by 1 digit, and the table about 10 times. Question 1 shows that this outgrows any fixed power of the target's digits. Books call that pseudo-polynomial time: fast while the numbers stay short.
Another method tries every set of the numbers, adding each one up. The size of the numbers does not matter now. But each extra number doubles the sets: 4 numbers make 16 sets, and 20 make 1,048,576. Now say someone hands you a set and says it adds to the target. To check, add it up: for 20 numbers, at most 19 additions. Nobody knows a fast way to find a set for every input. Checking one is fast.
| numbers | sets to try | additions to check one |
|---|---|---|
| 5 | 32 | 4 |
| 10 | 1,024 | 9 |
| 20 | 1,048,576 | 19 |
| 40 | 1,099,511,627,776 | 39 |
Problems whose answers can be found fast are called P. Problems where a proposed yes can be checked fast are called NP, and adding up to a target is one. Every problem in P is in NP: if you can solve it fast, solving it is a fast check. Is every problem in NP also in P? Nobody knows. The Clay Mathematics Institute has offered a million dollars for the answer since 2000.
One problem can turn into another. The books' volume four, chapter 43 turns a roster into boxes. Ana, Bo and Cass: at least one works on Saturday, and not all three. Draw a box for each way to meet a condition, such as A1 for Ana works and A0 for Ana is off. A1, B1 and C1 meet at least one works; A0, B0 and C0 meet not all three. Pick one box from each condition. Lines join boxes you cannot pick together: two of the same condition, and a person working and off. Two boxes with no line give a roster. A1 and B0 say Ana works and Bo is off; Cass can be either. Turning one problem into another quickly, with the same answer, is called a reduction.
In 1971 Cook showed that every problem in NP reduces to one question. Can a list of yes or no conditions, each saying at least one of a few things holds, all be met? Levin, in the Soviet Union, found the same kind of result on his own, published in 1973. A problem in NP that every problem in NP reduces to is called NP-complete. Garey and Johnson's book of 1979 lists over 300 of them. Given a fast method for one, turn any problem into it quickly, then solve that fast. So a fast method for one would give a fast method for all. But NP-complete is a claim about the worst input, not yours. The table above took 32 cells. Knapsack's page counts such a table, and the travelling salesperson's tries every order of its towns.
where the formulas come from
Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about , or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.
the move: the smallest target of d digits
the line:why it is allowed: Its first digit is at least 1, and every other digit at least 0. So the smallest is 1 and then zeros. For 3 digits, 100.
the move: as tens multiplied
the line:why it is allowed: 1 and then zeros is tens multiplied together. For 3 digits, 100.
guess it, thenthe move: count the cells
the line:why it is allowed: For the smallest target, each column has a cell for each total from 0 to the target, so the target plus 1 cells. There are 4 columns. For 3 digits, 4 × 101 = 404.
guess it, thenthe move: leave out the 4 extra cells
the line:digits smallest target cells floor 1 1 8 4 2 10 44 40 3 100 404 400 4 1000 4,004 4,000 The cells by line 3 and the floor by line 4. why it is allowed: Line 3, less the 4 cells for the total of 0. For 3 digits, 404 is at least 400.
guess it, thenthe move: one digit more
the line:digits sixth power of the digits floor 2 64 40 3 729 400 4 4,096 4,000 5 15,625 40,000 6 46,656 400,000 The sixth power starts above the floor. From 5 digits the floor is above, and each digit more widens its lead, as line 6 shows. why it is allowed: One digit more multiplies by one more 10. The input grows by 1 digit, and the floor by exactly 10 times. The cells come close, as 404 to 4,004 is about 9.9 times.
guess it, thenthe move: against any fixed power
the line:why it is allowed: Here is the fixed power. One digit more turns into , which is times . For the sixth power, from 4 digits to 5, 4,096 times about 3.8 is 15,625. As grows, shrinks, so falls toward 1. Once it is below 5, each digit more multiplies the floor by 10 and the power by less than 5, so the floor divided by the power at least doubles. Doubling again and again passes any number, so the floor passes the power and stays above.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: in or out
the line:why it is allowed: A set is fixed once you say, for each number, whether it is in. 1 number gives 2 sets, with it and without it.
the move: one more doubles
the line:set adds to none 0 2 2 3 3 2, 3 5 4 4 2, 4 6 3, 4 7 2, 3, 4 9 The 8 sets of 2, 3 and 4. The first 4 are the sets of 2 and 3, and the last 4 are the same with 4 put in. why it is allowed: Every set so far comes back twice, once without the new number and once with it.
guess it, thenthe move: double n times
the line:why it is allowed: Line 1 gives 2 sets for 1 number, and line 2 doubles them for each number after it. So numbers give 2s multiplied together. For 3 numbers, 8.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: the sets for n + 10
the line:why it is allowed: Question 2, for numbers.
the move: split the 2s
the line:why it is allowed: 2s multiplied are 2s, times 10 more.
guess it, thenthe move: work out 10 2s
the line:why it is allowed: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1,024. For 3 numbers, 8 sets become 8,192.
guess it, thenthe move: what it says
the line:why it is allowed: By line 3, 1,024 is more than 1,000. Count one step per set. A machine 1,000 times faster cannot try every set of 10 more numbers in the same time.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: add along the set
the line:why it is allowed: Here is how many numbers the set holds. Start from the first and add each other number once. Then compare the total with the target. For 3 and 4, 3 + 4 = 7, 1 addition.
the move: the biggest set
the line:why it is allowed: The biggest set holds all numbers, so by line 1 it takes the most. For 20 numbers, 19, against 1,048,576 sets to try.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
a worked example
Fill the table by hand for 3, 5 and 6, and a target of 11. Write each column as the totals it reaches.
- Before any number, only the total 0 is reached: take nothing.
- With 3: keep 0, and 0 + 3 = 3. Reached: 0 and 3.
- With 5: 0 + 5 = 5 and 3 + 5 = 8. Reached: 0, 3, 5 and 8.
- With 6: 0 + 6 = 6, 3 + 6 = 9, 5 + 6 = 11, and 8 + 6 = 14, past 11. 11 is reached: 5 + 6.
- The table has 3 columns of 12 cells, 36 in all. Checking the answer takes 1 addition, 5 + 6 = 11. Trying every set instead makes 8.
| with | totals reached | how many |
|---|---|---|
| 3 | 0, 3 | 2 |
| 5 | 0, 3, 5, 8 | 4 |
| 6 | 0, 3, 5, 6, 8, 9, 11 | 7 |
the trap
check yourself
The table has a column for each of 5 numbers and a cell for each total from 0 to the target 9,999. How many cells? And with the target 99,999?
the answer
5 × 10,000 = 50,000 cells, then 5 × 100,000 = 500,000. One digit more, 10 times the cells.
You have 30 numbers. How many sets does trying every set make? How many additions at most check one proposed set?
the answer
30 2s multiplied, 1,073,741,824 sets. Checking takes at most 29 additions, then one comparison.
Adapted from the books' volume four, chapter 43. Trying every set of 40 numbers takes exactly a day, one step per set. A machine 1,000 times faster runs for a day. How many numbers can it handle?
the answer
49. By question 3, 10 more numbers need 1,024 times the sets, more than 1,000. 9 more need 512 times, which fits.