Post · 4 min read· pp. 29–30

Fast Exponential Algorithms

An exponential algorithm for knapsack

images10

Alan Perlis won the first Turing Award for his pioneering work on computer languages. He helped create the now famous department of computer science at Carnegie-Mellon University, then later in his career he created another department at Yale University. Alan was always smiling even through he spent much of his adult life in a wheel chair. He delighted in all things, especially anything about computing. You could walk in his office anytime and chat about just about anything. My connection with Alan was as a graduate student at Carnegie-Mellon, and later as a junior faculty at Yale when he was the chair there.

Perlis was famous for many “sayings”. He talked about “one man’s constant is another’s variable,” he coined the term “Turing Tar-pit”. The latter referred, of course, to the fact that just about anything interesting that one wanted to do with computers was undecidable. He talked about the “IBM fog descending over all”–recall this was when IBM was the dominate player in computers.

Polynomial vs. Exponential Algorithms

Alan had one saying that is quite relevant to P=NP: he once said, “for every polynomial-time algorithm you have, there is an exponential algorithm that I would rather run”. His point is simple: if your algorithm runs in n4n^{4} than an algorithm that runs in n2n/10n2^{n/10} is faster for n=100n=100. While this is a simple point I think that it is a profound one. When we talk about P=NP we must not forget that the actually running time of the algorithm is the key. For example, if there is a factoring algorithm that runs in time n2n/100n2^{n/100} such an algorithm would easily break all today’s factor based crypto-systems. Yet it’s asymptotic performance would pale when compared to the best known factoring methods.

An Exponential Algorithm for Knapsack

One of my favorite algorithms is one for the knapsack problem that runs in time exponential in nn, but the exponential term is 2n/22^{n/2} instead of the obvious 2n2^{n}. This is a huge improvement over the obvious method. I have no idea who this algorithm is due to. I have known about it for decades–I would be greatly interested in any reference to it. But please to be the “first” the reference has to be before 1974, at least.

Recall the knapsack problem is the question of whether or not given the positive integers a1,…ana_{1}, \dots a_{n}, there is a subset II of the indices 11 to nn so that             ∑i∈Iai=b\sum_{i \in I} a_{i} = b where bb is another given positive integer. The obvious algorithm would try all subsets II. Since there are 2n−12^{n}-1 non-empty subsets the algorithm takes a long time.

The cool algorithm makes one small observation. The above equation can be re-written as follows:            ∑i∈Iai=b−∑j∈Jaj\sum_{i \in I} a_{i} = b - \sum_{j \in J} a_{j} where I⊆{1,…,n/2}I \subseteq \{1, \dots, n/2\} and J⊆{n/2+1,…,n}J \subseteq \{n/2+1, \dots, n\}. (Yes, we assume that nn is even, but if nn is odd the method can easily be fixed.) Then, the algorithm computes two sets. The first consists of all the values of            ∑i∈Iai\sum_{i \in I} a_{i} where I⊆{1,…,n/2}I \subseteq \{1, \dots, n/2\}; and the second consists of all values of            b−∑j∈Jajb - \sum_{j \in J} a_{j} where J⊆{n/2+1,…,n}J \subseteq \{n/2+1, \dots, n\}. We then simply check to see if these two have a value in common. The point is that they have a value in common if and only if the knapsack problem has a solution. The punch-line is that the cost for this is now dominated by the number of values in each set: 2n/22^{n/2}. Note, checking if they have a common value can be done either by hashing or by sorting. But, in either case, this takes time just slightly larger than the sum size of the sets. Very neat, I think.

Open Problems

The above algorithm relies on the following idea. Suppose that AA and BB are finite sets of integers, and define A+B={a+b∣a∈A and b∈B}A+B = \{a+b \mid a \in A \text{ and } b \in B\}. Then, determining whether or not xx is in A+BA+B can be done in time ∣A∣+∣B∣|A|+|B| if hashing is used. (If you use sorting the time goes up by a log-term.) Note, this is potentially much faster than a brute force search of the set A+BA+B.

A natural problem is what happens with a set like A+B+CA+B+C? Is there a way to tell if xx is in this set in time ∣A∣+∣B∣+∣C∣|A|+|B|+|C|, for example? If this is possible, then the knapsack problem has an algorithm whose exponential term is 2n/32^{n/3}. I have thought about this and related approaches quite often. I have yet to discover anything, I hope you have better luck.