Lably
Skip to content

Pseudo-polynomial time

Concept in complexity theory

  • 3 min read
  • 2 chapters

Related on Lably

Through Polynomial, Positional notation and Primality test

In general, using a positional number system, the numeric value of the input is exponential in the input length, which is why a pseudo-polynomial time algorithm does not necessarily run in polynomial time with respect to the input length. The distinction between the value of a number and its length is due to positional encoding; if numeric inputs are instead encoded in unary then the length and value are the same.

An NP-complete problem with known pseudo-polynomial time algorithms is called weakly NP-complete. An NP-complete problem is called strongly NP-complete if it is proven that it cannot be solved by a pseudo-polynomial time algorithm unless P = NP. The strong/weak kinds of NP-hardness are defined analogously. Strong NP-completeness of a problem can be established by a pseudo-polynomial transformation from a known strongly NP-complete problem.

Examples

Primality testing

Consider solving the problem of testing whether a number n is prime by naively checking whether a number in {2,3,…,n} divides n evenly. This approach can take up to n−1 divisions, which is sub-linear in the value of n but exponential in the length of n (which is about log⁡(n)). For example, a number n slightly less than 10,000,000,000 would require up to approximately 100,000 divisions, even though the length of n is only 11 digits. Moreover one can easily write down an input (say, a 300-digit number) for which this algorithm is impractical. Since computational complexity measures difficulty with respect to the length of the (encoded) input, this naive algorithm is actually exponential. It is, however, pseudo-polynomial time.

Contrast this algorithm with a true polynomial numeric algorithm—say, the straightforward algorithm for addition: Adding two 9-digit numbers takes around 9 simple steps, and in general the algorithm is truly linear in the length of the input. Compared with the actual numbers being added (in the billions), the algorithm could be called "pseudo-logarithmic time", though such a term is not standard. Thus, adding 300-digit numbers is not impractical. Similarly, long division is quadratic: an m-digit number can be divided by a n-digit number in O(mn) steps (see Big O notation.)

In the case of primality, it turns out there is a different algorithm for testing whether n is prime (discovered in 2002) that runs in time O((log⁡n)6).

Knapsack problem

In the knapsack problem, we are given n items with weight wi and value vi, along with a maximum weight capacity of a knapsack W. The goal is to solve the following optimization problem; informally, what's the best way to fit the items into the knapsack to maximize value?

maximize ∑i=1nvixi
subject to ∑i=1nwixi≤W and xi∈{0,1}.

Solving this problem is NP-hard, so a polynomial time algorithm is impossible unless P = NP. However, an O(nW) time algorithm is possible using dynamic programming; since the number W only needs log⁡W bits to describe, this algorithm runs in pseudo-polynomial time.

Sign in to keep reading

One more chapter is free with a Lably account.

More in Computing and communication