Papers
Topics
Authors
Recent
Search
2000 character limit reached

An O($nlogn$) approximate knapsack algorithm

Published 24 Dec 2025 in cs.DS | (2512.21195v1)

Abstract: A modified dynamic programming algorithm rapidly and accurately solves large 0/1 knapsack problems. It has computational O($nlogn$), space O($nlogn$) and predictable maximum error. Experimentally it's accuracy increases faster than linearly with the solution size $k$. Problems with $k=103$ are solved with an average maximum fractional error of $10{-4}$ and problems with $k=105$ with an average maximum fractional error of $10{-7}$. The algorithm runs in constant time for all problems with a given $n$. On a common desktop computer the algorithm processes $n=103$ problems in $10{-3}$ seconds and $n=106$ problems in 2 seconds.

Authors (1)

Summary

  • The paper introduces the XDP algorithm, a modified dynamic programming approach achieving O(n log n) complexity via logarithmic bin reduction.
  • It employs exact weight indexing and a backtracking mechanism with a greedy upper bound to ensure near-optimal accuracy.
  • Empirical results confirm sublinear runtime scaling and diminishing error rates, making it effective even for large and adversarial knapsack instances.

Efficient Approximate Solution to 0/1 Knapsack: The XDP Algorithm

Introduction

The 0/1 knapsack problem remains a prototypical NP-complete problem with significance across resource allocation, logistics, and finance. While exact algorithms typically exhibit pseudopolynomial or exponential complexity, the focus has shifted to developing high-performance approximation algorithms that guarantee solution quality within measurable bounds. The paper "An O(nlognnlogn) approximate knapsack algorithm" (2512.21195) introduces XDP, a modified dynamic programming approach achieving both computational and memory complexities of O(nlogn)O(n\log n) and predictable, empirically tight maximum error bounds. This marks a notable advancement facilitating practical resolution of large-scale problem instances.

Algorithmic Innovations

XDP transforms classic dynamic programming by several methodological changes:

  1. Bin Reduction: The number of bins TT is scaled logarithmically as T=ln(n)gT=\ln(n)\cdot g (gg constant, default 12), cutting computational effort to O(nlogn)O(n\log n).
  2. Exact Weight Indexing: Each bin's index directly reflects the exact subset weight, mapped as k=subset weightT/ck = \text{subset weight} \cdot T/c, avoiding quantization errors from scaling and rounding incurred in legacy approaches.
  3. Backtracking for Solution Recovery: The back[i][j]back[i][j] structure records object indices through which optimal subset formation can be retrieved efficiently, maintaining O(nlogn)O(n\log n) space.
  4. Comprehensive Object Processing: All objects, even the lowest-weight, are considered in the update sequence, countering biases toward greedy inclusion.
  5. Error Calculation via Greedy Upper Bound: The paper employs a modified greedy procedure to compute PmaxP_{\text{max}}, enabling direct calculation of maximum fractional error ee as (PmaxS)/Pmax(P_{\text{max}} - S)/P_{\text{max}}, where SS is the achieved profit.

XDP is implemented in C++ targeting single-core desktop CPUs, using double-precision arithmetic to avoid numerical error accumulation, and optimizing performance without parallelism.

Empirical Results

Extensive experimentation corroborates both theoretical and practical claims:

  • Fixed k=103k=10^3 yields average fractional error 104\approx 10^{-4}, and k=105k=10^5 yields 107\approx 10^{-7}.
  • Runtime scales sublinearly; n=103n=10^3 solves in 10310^{-3}s, n=106n=10^6 in $2$s.
  • Error ee diminishes faster than $1/k$, evidenced by decreasing ekek product as kk grows. For example, at k=5×104k=5\times10^4, e=1.19×107e=1.19 \times 10^{-7} and ek=0.006ek=0.006.
  • Performance is robust across randomly generated and "hard" instances per Jooken et al. [JO22], with average errors on hard files at 2.08×1042.08\times10^{-4}, closely matched to corresponding kk regimes for random instances.

The independence of error ee from nn for fixed kk consolidates the theoretical position that approximation quality is a function of the selected subset size more than instance cardinality. Benchmarks against GreedyPlus confirm that the greedy algorithm's average error scales as $0.5/k$, a property shown experimentally and leveraged in the analysis.

Theoretical Implications

The XDP methodology demonstrates that the most intensive phase of classic dynamic programming for knapsack—bin enumeration—can be logarithmically compressed without significant loss in solution fidelity, provided bins are accurately indexed. The backtracking mechanism ensures solutions can be reconstructed efficiently post-optimization. Greedy-sourced upper bounds for PmaxP_{\text{max}} are validated to be universally admissible across both random and adversarial instance types.

The author’s empirical observations confirm existing theoretical predictions (e.g., Calvin and Leung [CL03]), and extend them by showing that error reduction outpaces $1/k$, suggesting underlying structural regularity in the knapsack instance landscape which can be exploited algorithmically. The implementation prescribes a practical route to scale approximate knapsack solvers to problems several orders of magnitude larger than those feasible with prior FPTAS and DP methods.

Practical Impact and Future Directions

XDP's minimalistic runtime, sublinear scaling, and tight error bounds make it highly competitive for integration within real-world systems in logistics, capital allocation, and combinatorial optimization platforms requiring rapid, near-optimal subset selection. The strategy of logarithmic binning and greedy-based error estimation may inspire similar compressions in related combinatorial problems.

Further research directions include parallelization for multicore or distributed processing, adaptive bin scaling based on problem instance characteristics, and extension to multi-dimensional knapsack, where constraint coupling may manifest new error behaviors. Analytical exploration into the observed superlinear error decay in kk will yield deeper insights into solution landscapes, possibly refining complexity bounds for other NP-complete settings.

Conclusion

The XDP algorithm furnishes an O(nlogn)O(n\log n) approach to the 0/1 knapsack problem, achieving high empirical accuracy with rigorous worst-case error bounds. Its binning innovations and backtrack solution recovery set a practical benchmark for scalable, predictable knapsack approximation, facilitating application in large data domains and informing future algorithmic refinements for discrete optimization.

Paper to Video (Beta)

No one has generated a video about this paper yet.

Whiteboard

No one has generated a whiteboard explanation for this paper yet.

Open Problems

We found no open problems mentioned in this paper.

Collections

Sign up for free to add this paper to one or more collections.

Tweets

Sign up for free to view the 1 tweet with 61 likes about this paper.