Fact-checked by Grok 2 weeks ago

Pseudo-polynomial time

In , pseudo-polynomial time describes the running time of an algorithm for numeric problems that is bounded by a polynomial in both the length of the input instance and the maximum magnitude of the integers appearing in the input. This concept, introduced by Michael R. Garey and David S. Johnson in 1978, distinguishes such algorithms from those running in true time, as the dependence on the numeric values can lead to exponential behavior in the of the input when large integers are involved. Unlike strongly polynomial time algorithms, which bound the number of arithmetic operations by a polynomial in the number of variables or words in the input, independent of the numeric magnitudes (assuming unit cost for each arithmetic operation), pseudo-polynomial algorithms treat the input numbers in a way that scales with their unary representation size rather than binary. For instance, if the input size is n and the largest number is M, a pseudo-polynomial algorithm might run in O(n M) time, which is efficient when M is small relative to $2^n but inefficient for large M encoded in \Theta(\log M) bits. This intermediate complexity class is particularly relevant for optimization problems where inputs involve integers, as it allows practical solutions for instances with moderate numeric values despite underlying NP-hardness. Prominent examples include the dynamic programming solutions to the 0-1 and the , both of which achieve O(n W) time where W is the target capacity or sum, rendering them pseudo-polynomial. These algorithms highlight the term's utility in classifying "weakly" NP-complete problems, where pseudo-polynomial solvability indicates that the intractability arises primarily from large numeric values rather than the structural complexity of the input. In practice, pseudo-polynomial time enables efficient computation for real-world instances of such problems, such as or , provided the integers do not grow exponentially with the input size.

Definition and Fundamentals

Formal Definition

In , an algorithm runs in pseudo-polynomial time if its running time is bounded by a polynomial in the numeric values of the input parameters, which equates to polynomial time relative to the input size under unary encoding, but exponential time relative to the input size under the standard binary encoding. This concept, introduced in the seminal work on , distinguishes algorithms that are efficient for small numeric values but scale poorly with large ones due to the logarithmic compression in binary representation. Formally, consider an input instance of bit length n (the total number of bits needed to encode the input in ) and maximum numeric value M among the input numbers. An operates in pseudo-polynomial time if its worst-case running time is O(n^k \cdot M) for some constant k \geq 0. Here, M can be as large as $2^n - 1, making the time complexity exponential in n since M grows exponentially relative to the bit length. The distinction arises from input encoding schemes. In binary encoding, a positive integer x requires \lceil \log_2 (x+1) \rceil bits, so the input size is logarithmic in the numeric values. In contrast, unary encoding represents x as a string of x unary symbols (e.g., x ones separated by zeros), making the representation length exactly proportional to x. Thus, a pseudo-polynomial time bound, when analyzed under unary encoding, appears polynomial in the overall input size, but under binary encoding, the dependence on M renders it non-polynomial. For illustration, the number 1000 requires approximately 10 bits in binary (\log_2 1000 \approx 9.97) but 1000 symbols in unary, highlighting how unary encoding inflates the input size to match the numeric magnitude and thereby frames the algorithm's complexity as "pseudo-polynomial."

Distinction from Polynomial Time

Pseudo-polynomial time algorithms differ fundamentally from true polynomial time algorithms in how their running times are measured relative to the input representation. In polynomial time, the running time is bounded by a polynomial function of the input size n, where n typically denotes the number of bits required to encode the input; formally, the time complexity is T(n) = O(n^k) for some constant k. In contrast, pseudo-polynomial time allows the running time to depend polynomially on both the input size n and the numeric values present in the input, such as a maximum value M; a representative complexity is T(n, M) = O(n^k \cdot M^l) for constants k and l. This dependence on M—which can be exponentially larger than its bit length \log M—renders the algorithm exponential in the bit length when M is large, violating the strict polynomial bound. The term "pseudo-polynomial time" was introduced in 1978 by Michael R. Garey and David S. Johnson to describe algorithms for numeric problems that appear polynomial-like but fail to scale with large numeric inputs, particularly in the context of NP-complete problems like the knapsack and partition problems. Their work highlighted that such algorithms are efficient only when the numeric parameters are polynomially bounded relative to the input size, as the encoding of numbers would otherwise lead to infeasible runtimes. For instance, an algorithm with time O(n \cdot M) performs well if M = O(\mathrm{poly}(n)), but if M = 2^{\Omega(n)}, the time becomes exponential in n. This distinction has critical implications for efficiency in practice: pseudo-polynomial algorithms are viable for problems where input numbers remain small compared to the , enabling dynamic programming solutions that would otherwise be intractable. However, for inputs with large magnitudes—common in real-world scenarios involving high-precision numbers—they degrade to time, underscoring why pseudo-polynomial solvability does not imply membership in .

Key Examples

Knapsack Problem

The 0-1 is an where one is given n items, each with a positive integer weight w_i and positive integer value v_i for i = 1, \dots, n, along with an integer knapsack capacity W, and the goal is to select a of the items such that the of their weights is at most W and the of their values is maximized. This problem admits an exact solution via dynamic programming. The approach builds a table dp, where dp denotes the maximum value obtainable using the first i items with total weight at most w. The is: dp = \begin{cases} \max\left(dp[i-1], \, dp[i-1][w - w_i] + v_i\right) & \text{if } w \geq w_i, \\ dp[i-1] & \text{otherwise}, \end{cases} with base cases dp{{grok:render&&&type=render_inline_citation&&&citation_id=0&&&citation_type=wikipedia}} = 0 for all w and dp{{grok:render&&&type=render_inline_citation&&&citation_id=0&&&citation_type=wikipedia}} = 0 for all i. The final answer is dp[W], and the algorithm requires O(nW) time and O(nW) space to fill the table. The O(nW) time complexity renders this algorithm pseudo-polynomial, as it is polynomial when W is represented in unary (where the input size is \Theta(W)) but exponential in the input size when W is encoded in binary (where the input size is \Theta(\log W)). The space usage can be reduced to O(W) while preserving the O(nW) time by employing a one-dimensional array and iterating over the items and weights in reverse order, ensuring that each entry is updated using previously computed values without overlap. Although the exact dynamic programming solution runs in pseudo-polynomial time, the 0-1 knapsack problem also possesses a fully polynomial-time approximation scheme (FPTAS) that computes a solution within (1 - \epsilon) of the optimal value in time polynomial in n and $1/\epsilon, for any fixed \epsilon > 0.

Subset Sum Problem

The Subset Sum problem is a fundamental decision problem in computer science, where, given a finite set of positive integers S = \{s_1, s_2, \dots, s_n\} and a target integer T, the task is to determine whether there exists a subset S' \subseteq S such that the sum of the elements in S' equals exactly T. This problem admits a pseudo-polynomial time solution via dynamic programming. The algorithm maintains a boolean array dp[0 \dots T], initialized with dp{{grok:render&&&type=render_inline_citation&&&citation_id=0&&&citation_type=wikipedia}} = \true and all other entries \false, indicating whether a subset sums to each possible value up to T. For each s_i in S, the array is updated in reverse order from T down to s_i: dp \leftarrow dp \lor dp[w - s_i] for w = T, T-1, \dots, s_i. At the end, dp[T] = \true if and only if a valid subset exists. This approach runs in O(nT) time and O(T) space, which is pseudo-polynomial because the runtime is polynomial in the numeric value of T but exponential in the bit length \log T of the input. Subset Sum is NP-complete, as established by a polynomial-time reduction from the 3-SAT problem. Due to the existence of the above pseudo-polynomial algorithm, it is specifically weakly NP-complete, meaning it remains NP-complete under encoding of numbers but becomes solvable in polynomial time if numbers are encoded in . The , which asks whether a of integers can be divided into two subsets with equal sums, reduces directly to Subset Sum by setting T to half the total sum of the multiset (assuming the total sum is even); this underscores Subset Sum's foundational role in proving for various partitioning and summation problems. In practice, the dynamic programming algorithm's efficiency degrades when T is large relative to n, such as T \approx 2^n, rendering the O(nT) runtime infeasible as it equates to exponential time in the input size. Subset Sum serves as a special case of the more general , where item values equal their weights.

Applications and Generalizations

Primality Testing

Primality testing involves determining whether a given positive N > 1 is prime or composite using a that guarantees a correct output without relying on . A classic example of a deterministic is trial division, which verifies whether N has any divisors from 2 up to \lfloor \sqrt{N} \rfloor. The of this method is given by T(N) = O(\sqrt{N}), which is polynomial in the representation of N (of length N) but in the input size \log_2 N, rendering it pseudo-polynomial. This approach exemplifies how algorithms for numeric problems can exhibit pseudo-polynomial behavior when their runtime depends on the magnitude of the input value rather than its . Prior to 2002, unconditional deterministic primality tests like trial division were limited to pseudo-polynomial time, as no general -time was known. The , developed by , , and Nitin Saxena, marked a pivotal historical shift by providing the first unconditional running in time, specifically \tilde{O}(\log^{6} N). This advancement firmly established that the PRIMES lies in the complexity class , underscoring the distinction between pseudo-polynomial and truly algorithms in number-theoretic computations. The Miller-Rabin primality test, originally proposed by Gary L. Miller and , offers a probabilistic alternative with expected polynomial running time of O(k \log^3 N) for k iterations, where the probability of error for composite numbers decreases exponentially with k (at most $4^{-k}). Deterministic variants of Miller-Rabin, which employ a fixed set of witnesses tailored to specific ranges (e.g., up to 7 witnesses for N < 3,317,044,064,679,887,385,961,981), achieve unconditional deterministic primality testing in polynomial time for those bounded ranges. However, for arbitrary N, unconditional deterministic performance without assumptions like the Extended reverts to the polynomial-time guarantee provided by AKS, while broader deterministic extensions under GRH run in O(\log^5 N) time.

Non-Numeric Problems

The concept of pseudo-polynomial time extends beyond problems with numeric inputs to those involving structural or parameterized measures, where a parameter k (such as treewidth) plays the role analogous to a numeric value. In this , an is considered pseudo-polynomial if its running time is O(f(k) \cdot n^c) for some f (possibly in k) and constant c \geq 1, making it polynomial in the input size n but potentially super-polynomial in the bit length of k. This mirrors the original numeric case, where time is polynomial in the encoding of input values but in their length (\log k). A prominent example is on graphs with bounded w, where dynamic programming over a yields an running in O(q^w \cdot w^{O(1)} \cdot n) time for q-, with w as the . For determining the chromatic number, improved techniques achieve O( (2^w \cdot w^{O(1)} \cdot n) ) time, treating w as the unary-like for tractability when small. These approaches exploit the structural sparsity captured by to reduce the problem to local computations on decomposition bags. This extension aligns closely with fixed-parameter tractable (FPT) algorithms in , where pseudo-polynomial time emerges as an FPT outcome when the parameter k is small relative to n. Specifically, FPT algorithms of the form O(f(k) \cdot n^c) provide practical solvability for instances with bounded k, much like how pseudo-polynomial algorithms handle numeric inputs with modest magnitudes. Seminal work in formalizes this framework, emphasizing parameters like for NP-hard graph problems. However, not all NP-hard problems admit such pseudo-polynomial generalizations; tractability depends on the problem's structure and the choice of parameter, with some remaining W-hard and lacking FPT algorithms unless FPT = W. For instance, while many graph problems on bounded are FPT, others like certain coloring variants may resist efficient parameterization without additional assumptions. This selectivity underscores the need for problem-specific parameter selection in extending pseudo-polynomial techniques.

Complexity Classifications

Strong vs. Weak NP-Hardness

A problem involving numeric parameters is classified as weakly NP-hard if it is NP-hard under encoding of the numbers but possesses a pseudo- time , meaning the runs in time in the representation of the input values. In contrast, a problem is strongly NP-hard if it remains NP-hard even when the numeric parameters are encoded in , which implies that no pseudo- time exists for it unless P=. This distinction arises because encoding expands the input size proportionally to the magnitude of the numbers, eliminating the efficiency gains that pseudo- s exploit in encoding. The exemplifies weak NP-hardness, as it admits a pseudo-polynomial dynamic programming solution that runs in O(nW) time, where n is the number of elements and W is the target sum. On the other hand, the 3-partition problem—given a set of 3m positive integers, determine if they can be partitioned into m disjoint subsets each summing to the same value B—is strongly NP-hard, with no pseudo-polynomial algorithm possible unless P=NP. To preserve these hardness distinctions in reductions, two types are defined: weak reductions, which are ordinary polynomial-time transformations under encoding and thus maintain pseudo-polynomial solvability; and strong reductions, which are polynomial-time even under encoding but do not preserve pseudo-polynomial solvability, allowing proofs of strong . Garey and Johnson formalized this in their 1979 book Computers and Intractability: A to the NP-Completeness, systematically categorizing over 300 NP-complete problems based on the presence of numeric parameters and the strength of their hardness. A key theorem states that if any strongly NP-hard problem admits a pseudo-polynomial time , then P=, underscoring the theoretical barrier strong NP-hardness imposes on algorithmic solutions.

Implications for Algorithm Design

Understanding pseudo-polynomial time algorithms is crucial for designing efficient solutions to weakly NP-hard problems, where the numeric parameters in the input are reasonably small relative to the bit length of the input. In such cases, dynamic programming approaches can yield practical exact solutions, as the runtime is polynomial in the magnitude of these parameters rather than their logarithmic size. For instance, in scenarios like the 0-1 , where capacities or weights are not excessively large, pseudo-polynomial algorithms enable optimal decisions without resorting to exponential-time methods, making them preferable over fully exponential alternatives when parameter values permit feasible computation. In contrast, for strongly NP-hard problems, which remain intractable even under encoding of inputs, pseudo-polynomial methods are unavailable, necessitating alternatives such as approximation algorithms, metaheuristics like genetic algorithms, or exact branch-and-bound techniques that may incur exponential time in the worst case. These choices involve evaluating the desired solution quality against computational resources; for example, fully polynomial-time approximation schemes (FPTAS) can provide near-optimal solutions with guaranteed error bounds in polynomial time for problems like knapsack, trading optimality for scalability. Designers must assess problem instances to select between exact pseudo-polynomial solutions for weak hardness and these approximations for strong cases, ensuring robustness across varying input scales. A key trade-off in implementing pseudo-polynomial dynamic programming lies in balancing time and , particularly for problems with large but manageable parameters. Standard table-based dynamic programming for knapsack requires O(nW) time and , where n is the number of items and W the capacity, but optimized versions can reduce to O(W) by processing rows sequentially, at the cost of increased constant factors or auxiliary computations. However, when parameters like W grow large (e.g., beyond 10^6), even pseudo-polynomial runtimes become prohibitive due to memory constraints or sheer execution time, prompting hybrid approaches that combine DP with pruning or bounding to mitigate scaling issues. In modern applications, pseudo-polynomial algorithms underpin solutions in , such as single-machine scheduling to minimize total tardiness (1||ΣT_j), where dynamic programming achieves O(n^4 Σp_j) time for processing times p_j, facilitating just-in-time and optimization. In , pre-AKS primality testing relied on pseudo-polynomial methods like trial division up to √n (O(√n) time), essential for before deterministic polynomial-time tests became available in 2002. Similarly, in AI for problems (CSPs), parameterized variants like Max-r-Lin2-AA admit fixed-parameter tractable (FPT) algorithms and kernels (e.g., 2^{O(k)} n^{O(1)} time for parameter k), enabling exact solving of instances with bounded solution deviations in and tasks. Looking ahead, pseudo-polynomial time intersects with , where small parameters (e.g., solution size k in CSPs) allow fixed-parameter tractable (FPT) algorithms that extend pseudo-polynomial techniques, offering quadratic kernels for problems like Max-r-Sat-AA and improving solvability for real-world constraints—as shown in 2012 results. In , these algorithms hold promise for speedup; for example, quantum enhances knapsack solving, outperforming classical pseudo-polynomial dynamic programming on hard instances with n ≥ 100 items by reducing required cycles and memory, potentially revolutionizing optimization in resource-limited quantum settings.

References

  1. [1]
    "Strong" NP-Completeness Results
    Definition 2. A pseudo-polynomial time algorithm is an algorithm that runs in time bounded by a polynomial in the two variables Length[l] and Max[I]. Note that ...
  2. [2]
    [PDF] 6.006 Introduction to Algorithms, Lecture 18: Pseudopolynomial
    Is This Polynomial Time? • (Strongly) polynomial time means that the running time is bounded above by a constant- degree polynomial in the input size ...
  3. [3]
    [PDF] Computational Complexity 1 Polynomial Time Algorithms
    An algorithm which runs in time polynomial of the data when the data is represented in unary is called a pseudo-polynomial time algorithm. Pseudo-polynomial ...<|control11|><|separator|>
  4. [4]
    [PDF] Lecture 3 1 Polynomial-time algorithms for the maximum flow prob
    Aug 30, 2007 · Definition 2 An algorithm runs in pseudo polynomial time if the number of operations can be bounded by a polynomial in the size of the input ...
  5. [5]
    Polynomial Time - an overview | ScienceDirect Topics
    A pseudo-polynomial time algorithm is an algorithm that runs in polynomial time when the input is encoded in a unary fashion, i.e., it runs in polynomial time ...
  6. [6]
    [PDF] NP-hardness and pseudo-polynomial time algorithms R. Inkulu
    And, both the pseudo-polynomial time and exponential time algorithms are exponential in time complexity since the input complexity is in the exponent of time.<|separator|>
  7. [7]
    Strong '' NP-Completeness Results: Motivation, Examples, and ...
    `` Strong '' NP-Completeness Results: Motivation, Examples, and Implications ... GAREY, M R, JOHNSON, D S, AND SETHI, R The complexRy of flowshop and ...
  8. [8]
    Fast Approximation Algorithms for the Knapsack and Sum of Subset ...
    Fast Approximation Algorithms for the Knapsack and Sum of Subset Problems. Authors: Oscar H. Ibarra.<|control11|><|separator|>
  9. [9]
    [PDF] Primality Testing - Stanford CS Theory
    Rabin's algorithm is always correct when the input is a prime, and has a (small) probability of error when the input is a composite. Miller described ...
  10. [10]
    [PDF] PRIMES is in P - Microsoft
    Algorithm for Primality Testing. Page 5. PRIMES IS IN P. 785. Theorem 4.1. The algorithm above returns PRIME if and only if n is prime. In the remainder of the ...Missing: original | Show results with:original
  11. [11]
    [PDF] The Miller-Rabin Randomized Primality Test
    In 1980, Michael Rabin discovered a randomized polynomial-time algorithm to test whether a number is prime. It is called the Miller-Rabin primality test because.
  12. [12]
    [PDF] COMPUTERS AND INTRACTABILITY A Guide to the Theory of NP ...
    4.2.2 Proving Strong NP-Completeness Results. 4.3 Time Complexity as a ... Chapter 3 is devoted to methods for proving a problem NP-complete. A number ...
  13. [13]
    [PDF] NP-Hard Problems - Jeff Erickson
    Algorithms like this are said to run in pseudo-polynomial time, and any. NP-hard problem with such an algorithm is called weakly NP-hard. Equivalently, a ...
  14. [14]
    [PDF] Dynamic Programming - cs.Princeton
    Knapsack algorithm runs in time O(NW). □. Not polynomial in input size ... Straightforward dynamic programming takes Θ(MN) time and space. □. English ...
  15. [15]
    [PDF] Scheduling Problems and Solutions - NYU Stern
    There is a pseudo polynomial time algorithm to solve the problem. Properties of the solution: 1. If pj ≤ pk and dj ≤ dk holds then there exists an optimal.
  16. [16]
    [PDF] Testing Primality in Polynomial Time
    Mar 7, 2016 · A naïve primality test. 11. If (X + 1)n. ⌘ Xn + 1 (mod n), then n is prime, otherwise n is composite. Pseudo-polynomial! Input size: logn.
  17. [17]
    [PDF] Parameterized Constraint Satisfaction Problems: a Survey - DROPS
    It is well-known that, in polynomial time, we can find an assignment to the variables that satisfies equations of total weight at least W/2, but, for any > 0 it ...
  18. [18]
    A quantum algorithm for solving 0-1 Knapsack problems - Nature
    Aug 26, 2025 · Known for its stable behaviour due to its pseudo-polynomial time complexity bound, it stands as the current leading solver for this problem.