2012 ACM Subject Classification Theory of computation → \to → Design and analysis of algorithms
Funding Anubhav Dhar: Supported by the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) grant number 559177164.
Pranjal Dutta: Supported by Nanyang Assistant Professorship as well as by the SUG Grant (#025774-00001), funded by Nanyang Technological University Singapore.
Mahesh Sreekumar Rajasree: Funded by the European Union (ERC, LACONIC, 101041207). Views and opinions expressed are those of the authors only and do not necessarily reflect those of the European Union or the European Research Council. Neither the European Union nor the granting authority can be held responsible for them.
Karol Węgrzycki: Supported by the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) grant number 559177164.
Introduction
The Subset Sum problem asks whether, given nonnegative integers a 1 , … , a n a_1,\ldots,a_n a 1 , … , a n and a target t t t , there exists a subset of the input items that sums exactly to t t t . It is a basic example of an NP-complete problem that admits a pseudopolynomial algorithm: Bellman’s dynamic program takes O ( n t ) O(nt) O ( n t ) time [3 ] . Subsequent work has substantially improved the dependence on n n n and t t t . Koiliaris and Xu gave a deterministic O ~ ( n t ) \widetilde{O}(\sqrt{nt}) O ( n t ) -time algorithm [15 ] , and Bringmann [4 ] and, later, Jin and Wu [14 ] gave randomized O ~ ( n + t ) \widetilde{O}(n+t) O ( n + t ) -time algorithms. Recently, Chan derandomized these results and gave a deterministic O ~ ( t ) \widetilde{O}(t) O ( t ) -time algorithm for Subset sum [6 ] . These algorithms determine which sums in { 0 , … , t } \{0,\ldots,t\} { 0 , … , t } are attainable , i.e. appear as a sum of some subset of input items. A natural next question is what can be learned about the subsets attaining a given target. We study the maximum size of such a subset. Items are distinguished by their indices, even when their values are equal.
Definition 1 (Largest Cardinality Subset Sum (LCSS)). Given nonnegative integers a 1 , … , a n a_1,\ldots,a_n a 1 , … , a n and a positive target t t t , LCSS asks for max { ∣ S ∣ : S ⊆ { 1 , … , n } , ∑ i ∈ S a i = t } \max\{|S|:S\subseteq\{1,\ldots,n\},\sum_{i\in S}a_i=t\} max { ∣ S ∣ : S ⊆ { 1 , … , n } , ∑ i ∈ S a i = t } , else report if t t t is unattainable.
Thus LCSS refines the usual decision problem: it distinguishes two attainable representations of the same target by their cardinalities. It also has a direct interpretation as a structured 0-1 knapsack problem: we give item i i i weight a i a_i a i and profit M a i + 1 Ma_i+1 M a i + 1 , where M > n M>n M > n , and set the knapsack capacity to t t t . Every subset of weight exactly t t t has profit M t + ∣ S ∣ Mt+|S| M t + ∣ S ∣ , whereas every subset of weight at most t − 1 t-1 t − 1 has profit at most M ( t − 1 ) + n < M t M(t-1)+n<Mt M ( t − 1 ) + n < M t . Consequently, if t t t is attainable, an optimal knapsack solution is a subset that sums to t t t and has maximum cardinality among all the subsets that sum to t t t ; if it is unattainable, the optimal profit is below M t Mt M t . This embedding explains why recording only attainable sums, as in ordinary subset sum, does not immediately give the desired optimum. In fact, at the fine-grained level, this places LCSS in between the subset sum and the knapsack problem; LCSS is at least as hard as subset sum, and knapsack is at least as hard as LCSS. These observations make LCSS an interesting problem to study.
0-1 knapsack for single capacity: Jin [12 ] gave a deterministic algorithm that runs in time O ~ ( n + a max 2 ) \widetilde{O}(n+a_{\max}^2) O ( n + a m a x 2 ) , whereas Bringman [5 ] also (independently) gave a deterministic O ~ ( n + a max 2 ) \widetilde{O}(n+a_{\max}^2) O ( n + a m a x 2 ) -time algorithm for the bounded knapsack problem (where each item comes with a multiplicity). Here a max a_{\max} a m a x denotes the maximum weight item. Using the above reduction and these two algorithms, we get O ~ ( n + a max 2 ) \widetilde{O}(n+a_{\max}^2) O ( n + a m a x 2 ) -time algorithm for LCSS.
There is also a useful analogy with change-making . The change-making problem minimizes the number of coins summing to a target, with unlimited use of each denomination. In LCSS, each input item can be used at most once, and the number of selected items is maximized. Both problems invite a distinction between a single specified target and all targets up to a bound. This distinction has proved consequential for change-making: Chan and He obtained near-linear-in-t t t algorithms for the single-target problem and an O ~ ( t 4 / 3 ) \widetilde{O}(t^{4/3}) O ( t 4/3 ) -time algorithm for its all-targets version [7 ] . We likewise study both versions of LCSS.
Definition 2 (All-Targets Largest Cardinality Subset Sum (AT-LCSS)). Given nonnegative integers a 1 , … , a n a_1,\ldots,a_n a 1 , … , a n and a positive integer t t t , AT-LCSS asks, for every s ∈ { 1 , … , t } s\in\{1,\ldots,t\} s ∈ { 1 , … , t } , for the maximum cardinality of a subset S ⊆ { 1 , … , n } S\subseteq\{1,\ldots,n\} S ⊆ { 1 , … , n } with ∑ ( S ) = s \sum(S)=s ∑ ( S ) = s , else report if s s s is unattainable.
A straightforward dynamic program computes the optimum for every sum up to t t t in O ( n t ) O(nt) O ( n t ) time: when item a i a_i a i is processed, the best cardinality at sum s s s is compared with one plus the previous best cardinality at s − a i s-a_i s − a i . Koiliaris and Xu [15 ] also discuss the problem of subset sums with cardinality information. They further provide an O ~ ( n t ) \widetilde{O}(nt) O ( n t ) -time algorithm that gives all the attainable subset sums with complete cardinality information, not just the maximum. However, their improvement of the complexity from O ~ ( n t ) \widetilde{O}(nt) O ( n t ) to O ~ ( n t ) \widetilde{O}(\sqrt{nt}) O ( n t ) loses information regarding cardinalities. This suggests that solving LCSS in O ~ ( n t ) \widetilde{O}(\sqrt{nt}) O ( n t ) time is an interesting problem. On the other hand, Axiotis and Tzamos [2 ] give an O ( D t ) O(Dt) O ( D t ) -time algorithm for the knapsack problem, with all weights being less than or equal to t t t , where D D D denotes the number of distinct items in the instance. This algorithm, with the aforementioned reduction of LCSS to knapsack, gives a O ( D t ) O(Dt) O ( D t ) -time algorithm for AT-LCSS, and consequently for LCSS too.
The subset-sum algorithms in [4 , 14 , 6 ] do not, on their own, preserve the largest cardinality for each sum. Related work has examined other forms of cardinality information: Dutta and Rajasree [9 ] study the Hamming weights of all solutions when the number of solutions is bounded, while Antonopoulos, Pagourtzis, Petsalakis, and Vasilakis [1 ] discuss algorithms for subset-sum variants with target-cardinality constraints.
Projection Subset Sum and related work
AT-LCSS has a direct application to Projection Subset Sum , introduced by Dutta and Rajasree [10 ] . Given a nonnegative integer vector Z = ( a 1 , … , a n ) Z=(a_1,\ldots,a_n) Z = ( a 1 , … , a n ) and a nonnegative integer target t t t , the search problem asks for an integer vector X = ( x 1 , … , x n ) X=(x_1,\ldots,x_n) X = ( x 1 , … , x n ) and a subset S ⊆ [ n ] S\subseteq[n] S ⊆ [ n ] satisfying ∑ i ∈ S x i = t \sum_{i\in S}x_i=t ∑ i ∈ S x i = t that minimize ∥ Z − X ∥ p \lVert Z-X\rVert_p ∥ Z − X ∥ p . For t > 0 t>0 t > 0 , the set S S S is necessarily nonempty. Their near-linear randomized algorithm for the ℓ 1 \ell_1 ℓ 1 case assumes that every attainable sum has a unique representation; they also give an O ( n t ) O(nt) O ( n t ) algorithm under that assumption [10 ] .
For a fixed nonempty subset S S S , the amount by which its original sum misses t t t is d = t − Σ ( S ) d=t-\Sigma(S) d = t − Σ ( S ) . In the ℓ ∞ \ell_\infty ℓ ∞ case, if integer-valued coordinates may be changed without sign restrictions, the smallest possible maximum coordinate change is ⌈ ∣ d ∣ / ∣ S ∣ ⌉ \lceil|d|/|S|\rceil ⌈ ∣ d ∣/∣ S ∣ ⌉ . More selected items allow the discrepancy to be spread over more coordinates. For general p ≥ 1 p\ge1 p ≥ 1 , the corresponding exact integer cost further depends on Σ ( S ) \Sigma(S) Σ ( S ) and ∣ S ∣ |S| ∣ S ∣ ; see § 6 . Among nonempty subsets with the same sum, a largest subset minimizes the projection cost. Therefore, an AT-LCSS solution supplies exactly the cardinality information needed to compare the best projection costs for different attainable sums, including sums above t t t .
A related motivation for Projection Subset Sum comes from neural-network pruning. The strong lottery ticket hypothesis asks whether a randomly initialized network contains a subnetwork that approximates a given target network [20 , 18 ] . Pensia et al. [19 ] study this question using random subset sums, while Xiong, Liao, and Kyrillidis [22 ] additionally allow bounded perturbations of the weights. This combines selecting existing weights with slightly modifying them to improve approximation. Projection Subset Sum captures a related question for a list of numbers: how little must the numbers be changed so that some subset sums exactly to a prescribed target?
Our results
The previous discussion already gives us O ~ ( n + d max 2 ) \widetilde{O}(n+d_{\max}^2) O ( n + d m a x 2 ) -time and O ( n + D t ) O(n+Dt) O ( n + D t ) -time algorithms for LCSS. Our main result is a faster pseudopolynomial-time algorithm for the regime when a max = Θ ~ ( n 1 / 4 t ) a_{\max}=\widetilde{\Theta}(n^{1/4}\sqrt{t}) a m a x = Θ ( n 1/4 t ) and D = O ~ ( n ) D=\widetilde{O}(\sqrt{n}) D = O ( n ) . Note that O ~ ( ⋅ ) \widetilde{O}(\cdot) O ( ⋅ ) hides polylogarithmic factors.
Theorem 3. For any fixed integer constant c ≥ 10 c\ge10 c ≥ 10 , there exists a randomized algorithm that solves LCSS on n n n nonnegative integers with target t t t in time O ~ ( n + n t ) \widetilde{O}(n+\sqrt{nt}) O ( n + n t ) , with error probability at most n − c n^{-c} n − c .
Thus, up to polylogarithmic factors, our algorithm reduces the coefficient of t t t from n n n to n \sqrt{n} n in the running time complexity. It computes the exact maximum cardinality of a subset summing to t t t , or reports that t t t is unattainable, with high probability.
Very recently, Jin and Mao [13, Proposition D.3] give a conditional result for LCSS on positive inputs. If there is an algorithm running in T ( n , Δ ) T(n,\Delta) T ( n , Δ ) that decomposes all subset sums of n n n positive integers bounded by Δ \Delta Δ into an arithmetic progression with a common [difference], then they managed to show that LCSS can be solved in O ~ ( n + n ⋅ t + T ( n , 3 t n ) ) \widetilde{O}(n+\sqrt{n}\cdot t+T(n,3t\sqrt{n})) O ( n + n ⋅ t + T ( n , 3 t n )) time. Their result would match the running time of our result, if T ( n , Δ ) ≤ O ~ ( n + Δ ) T(n,\Delta)\le\widetilde{O}(n+\Delta) T ( n , Δ ) ≤ O ( n + Δ ) , however the existance of such a fast algorithm is open [13 ] . Theorem 3 avoids this unresolved assumption and achieves the same running-time bound unconditionally .
Our second result focuses on the all-targets version, in which the maximum cardinality must be computed for every target s ∈ { 1 , … , t } s\in\{1,\ldots,t\} s ∈ { 1 , … , t } .
Theorem 4. For any fixed integer constant c ≥ 10 c\ge10 c ≥ 10 , there exists a randomized algorithm that solves AT-LCSS on n n n nonnegative integers with target t t t in time O ~ ( n + t 3 / 2 ) \widetilde{O}(n+t^{3/2}) O ( n + t 3/2 ) , with failure probability at most n − c n^{-c} n − c .
This algorithm improves the discussed O ( n + D t ) O(n+Dt) O ( n + D t ) algorithm when D = ω ~ ( t 1 / 2 ) D=\widetilde{\omega}(t^{1/2}) D = ω ( t 1/2 ) . The probability guarantee holds simultaneously for all targets. Beyond the time needed to read the input, the running time depends only on t t t , rather than on the number of items. The two bounds are complementary. Since the all-targets algorithm also solves any single-target instance, together they yield an O ~ ( n + t min { n , t } ) \widetilde{O}(n+t\min\{\sqrt{n},\sqrt{t}\}) O ( n + t min { n , t }) -time algorithm for LCSS.
By exploiting the cardinality information provided by AT-LCSS, we also give a randomized O ~ ( n + t 3 / 2 ) \widetilde{O}(n+t^{3/2}) O ( n + t 3/2 ) -time algorithm for Projection Subset Sum in the ℓ p \ell_p ℓ p norm (PSSp _p p ) for all fixed p ≥ 1 p\ge1 p ≥ 1 .
Theorem 5. For fixed p ≥ 1 p\ge1 p ≥ 1 , given nonnegative integers Z = ( a 1 , … , a n ) Z=(a_1,\ldots,a_n) Z = ( a 1 , … , a n ) and an integer t ≥ 0 t\ge0 t ≥ 0 , there is a randomized algorithm that outputs an optimal solution ( X , S ) (X,S) ( X , S ) to PSSp _p p in time O ~ ( n + t 3 / 2 ) \widetilde{O}(n+t^{3/2}) O ( n + t 3/2 ) , with failure probability at most 1 / 3 1/3 1/3 .
And, for the ℓ 1 \ell_1 ℓ 1 norm, we give a randomized O ~ ( n + t ) \widetilde{O}(n+t) O ( n + t ) -time algorithm using Bringmann’s subset-sum algorithm; see Theorem 27 . Both the PSS algorithms output an optimal vector X X X and a set S S S witnessing its feasibility, with failure probability at most 1 / 3 1/3 1/3 , without the unique-representation assumption.
Organization of the paper. § 2 gives an overview of our algorithms. § 3 introduces the notation and tools used. § 4 presents the algorithm for LCSS via solving TCSS. The algorithm for AT-LCSS can be found in § 5 . The algorithms for Projection Subset Sum can be found in § 6 . Appendix § 7 contains deferred lemmas and proofs.
Declaration on the Use of AI Tools. Generative AI tools, including ChatGPT and Claude, were used solely for proofreading, language editing, improving presentation, and identifying potential inconsistencies. All technical contributions are original to the authors, who independently verified relevant AI-generated suggestions and take full responsibility for the manuscript.
Technical Overview
Previously, Bringmann [4 ] gave an O ~ ( n + t ) \widetilde{O}(n+t) O ( n + t ) -time algorithm to output all possible subset sums capped by an integer t t t , in an application of n n n elements. This was later simplified by Jin and Wu [14 ] , and then derandomized by Chan [6 ] . For this paper, we give a bivariate equivalent result of [14 ] , which computes both cardinalities and sums of all possible subsets with caps in both cardinality and the sum.
Lemma 6. Given nonnegative integers a 1 , … , a n a_1,\ldots,a_n a 1 , … , a n , positive integers k k k and t t t , and a parameter δ ∈ ( 0 , 1 ) \delta\in(0,1) δ ∈ ( 0 , 1 ) , there is a randomized algorithm with one-sided error probability at most δ \delta δ that computes the set { ( Σ ( S ) , ∣ S ∣ ) : S ⊆ { 1 , … , n } , ∣ S ∣ ≤ k , Σ ( S ) ≤ t } \{(\Sigma(S), |S|) : S \subseteq\{1,\ldots,n\}, |S| \le k, \Sigma(S) \le t\} {( Σ ( S ) , ∣ S ∣ ) : S ⊆ { 1 , … , n } , ∣ S ∣ ≤ k , Σ ( S ) ≤ t } in time
O ( ( n + k t ) polylog ( n + k t + 1 / δ ) ) . O((n+kt)\operatorname{polylog}(n+kt+1/\delta)). O (( n + k t ) polylog ( n + k t + 1/ δ )) . The returned set is always a subset of { ( Σ ( S ) , ∣ S ∣ ) : S ⊆ { 1 , … , n } , ∣ S ∣ ≤ k , Σ ( S ) ≤ t } \{(\Sigma(S), |S|) : S \subseteq\{1,\ldots,n\}, |S| \le k, \Sigma(S) \le t\} {( Σ ( S ) , ∣ S ∣ ) : S ⊆ { 1 , … , n } , ∣ S ∣ ≤ k , Σ ( S ) ≤ t } .
A proof idea is as follows: Consider Bringmann’s algorithm [4 ] . Essentially, the algorithm consists of a collection of convolutions to keep track of sums. Whenever a sum exceeds t t t , this sum is dropped from further consideration. One can modify Bringmann’s algorithm to simply consider pairs of cardinalities and sums, then convolve these sets of pairs, and drop pairs which exceed either the cardinality bound or the sum cap. The resulting algorithm has the required running time. Similarly, Jin and Wu’s algorithm [14 ] can also be modified accordingly to prove Lemma 6; for completeness, we provide a complete proof in Appendix A.2.
Solving Largest Cardinality Subset Sum
We start by focusing on the intermediate problem of finding whether a subset of a given sum and a given cardinality exists.
Definition 7 (Target Cardinality Subset Sum (TCSS)). Let a 1 , … , a n , k , t a_1,\ldots,a_n,k,t a 1 , … , a n , k , t be nonnegative integers. TCSS asks whether there is a subset S ⊆ { 1 , … , n } S \subseteq\{1,\ldots,n\} S ⊆ { 1 , … , n } with ∣ S ∣ = k |S| = k ∣ S ∣ = k and Σ ( S ) = t \Sigma(S) = t Σ ( S ) = t .
We first provide an O ~ ( n + n t ) \widetilde{O}(n+\sqrt{nt}) O ( n + n t ) algorithm for TCSS, and then use it to solve LCSS.
Algorithm for TCSS. Our approach can be informally described as follows. We process items smaller than t / n t/\sqrt{n} t / n and items larger than t / n t/\sqrt{n} t / n separately. At most n \sqrt{n} n many large items can contribute to a solution; we use Lemma 6 to essentially list all cardinalities and sums of all relevant subsets with at most n \sqrt{n} n large items. Next, we color the indices randomly into n \sqrt{n} n colors. For each color class, we compute all cardinality–sum pairs of the small items (using Lemma 6) with a sum cap of O ~ ( t / n ) \widetilde{O}(t/\sqrt{n}) O ( t / n ) . We merge these cardinality–sum pairs in a merge-sort fashion: at each step, merge two sets of cardinality–sum pairs to get a larger one. However, doing this naively would blow up the sizes of these sets. We overcome this issue by keeping only the pairs whose cardinality deviates from the expected cardinality of any solution by at most O ~ ( n ) \widetilde{O}(\sqrt{n}) O ( n ) , while discarding others. This crucial step aids in making the algorithm efficient (O ~ ( n t ) \widetilde{O}(\sqrt{nt}) O ( n t ) time spent on each level), while the Chernoff bound guarantees that with high probability, a solution is not discarded. Finally, our algorithm decides if a solution exists by checking the existence of a compatible subset among the small items for each cardinality–sum pair of the large items. An overview of the details and the techniques is as follows.
Assume that the number of items n n n is a power of 4, and all items are bounded by Θ ( t ) \Theta(t) Θ ( t ) . We start by color coding the indices into n \sqrt{n} n color classes. Formally, construct a random partition Z 1 ⊎ ⋯ ⊎ Z n = { 1 , … , n } Z_1 \uplus\cdots\uplus Z_{\sqrt{n}} = \{1,\ldots,n\} Z 1 ⊎ ⋯ ⊎ Z n = { 1 , … , n } of the indices. We define Z j ( ℓ ) = Z 2 ℓ ( j − 1 ) + 1 ∪ Z 2 ℓ ( j − 1 ) + 2 ∪ ⋯ ∪ Z 2 ℓ ( j − 1 ) + 2 ℓ Z_j^{(\ell)} = Z_{2^\ell(j-1)+1} \cup Z_{2^\ell(j-1)+2} \cup\cdots\cup Z_{2^\ell(j-1)+2^\ell} Z j ( ℓ ) = Z 2 ℓ ( j − 1 ) + 1 ∪ Z 2 ℓ ( j − 1 ) + 2 ∪ ⋯ ∪ Z 2 ℓ ( j − 1 ) + 2 ℓ , for ℓ ≤ 1 2 log n \ell\le\frac{1}{2}\log n ℓ ≤ 2 1 log n .
Assume that a solution exists, i.e. let X ⊆ { 1 , … , n } X \subseteq\{1,\ldots,n\} X ⊆ { 1 , … , n } be such that Σ ( X ) = t \Sigma(X) = t Σ ( X ) = t and ∣ X ∣ = k |X| = k ∣ X ∣ = k . Consider the following breakup of the input elements into heavy elements B B B and light elements A A A , with their respective intersection with the solutions being X B X_B X B and X A X_A X A .
B : = { i ∈ { 1 , … , n } : a i ∈ ( t n , t + 1 ] } , X B : = X ∩ B , A : = { 1 , … , n } ∖ B , X A : = X ∩ A . \begin{aligned}
B &:= \left\{i \in\{1,\ldots,n\} : a_i \in\left(\frac{t}{\sqrt{n}},t+1\right]\right\}, & X_B &:= X \cap B,\\
A &:= \{1,\ldots,n\}\setminus B, & X_A &:= X \cap A.
\end{aligned} B A := { i ∈ { 1 , … , n } : a i ∈ ( n t , t + 1 ] } , := { 1 , … , n } ∖ B , X B X A := X ∩ B , := X ∩ A .
It is easy to see that X B X_B X B has size at most n \sqrt{n} n , as all elements of B B B have a value of at least t / n t/\sqrt{n} t / n . Using Lemma 6 on the indices in B B B , we compute
J ← { ( ∣ S ∣ , Σ ( S ) ) : S ⊆ B , ∣ S ∣ ≤ n , Σ ( S ) ≤ t } \mathcal{J} \gets\{(|S|,\Sigma(S)) : S \subseteq B, |S| \le\sqrt{n}, \Sigma(S) \le t\} J ← {( ∣ S ∣ , Σ ( S )) : S ⊆ B , ∣ S ∣ ≤ n , Σ ( S ) ≤ t }
in time O ~ ( n + n t ) \widetilde{O}(n+\sqrt{n}t) O ( n + n t ) . We must have ( ∣ X B ∣ , Σ ( X B ) ) ∈ J (|X_B|,\Sigma(X_B)) \in\mathcal{J} ( ∣ X B ∣ , Σ ( X B )) ∈ J .
We now focus on the light items. Using the Chernoff bound, we show that (i) each color class is of size O ( n ) O(\sqrt{n}) O ( n ) , (ii) the size of intersection of X A X_A X A with a given subset of color classes deviates by at most O ~ ( n ) \widetilde{O}(\sqrt{n}) O ( n ) from its expected intersection size, and (iii) the sum of elements in X A X_A X A from any fixed color class is at most O ~ ( t / n ) \widetilde{O}(t/\sqrt{n}) O ( t / n ) , i.e. the color classes split the sum somewhat evenly. More formally, we prove the following lemma.
Lemma 16 (Simplified). With probability at least 1 − n − 3 c 1-n^{-3c} 1 − n − 3 c , we have all of the following.
∣ Z j ∣ ≤ O ( n ) |Z_j| \le O(\sqrt{n}) ∣ Z j ∣ ≤ O ( n ) , for j ∈ { 1 , … , n } j \in\{1,\ldots,\sqrt{n}\} j ∈ { 1 , … , n } ,
∣ ∣ X A ∩ Z j ( ℓ ) ∣ − 2 ℓ k n ∣ ≤ O ( n log n ) \left||X_A \cap Z_j^{(\ell)}|-\frac{2^{\ell}k}{\sqrt{n}}\right| \le O(\sqrt{n\log n}) ∣ X A ∩ Z j ( ℓ ) ∣ − n 2 ℓ k ≤ O ( n log n ) , for ℓ ∈ { 0 , 1 , … , 1 2 log n } \ell\in\{0,1,\ldots,\frac{1}{2}\log n\} ℓ ∈ { 0 , 1 , … , 2 1 log n } , j ∈ { 1 , … , n / 2 ℓ } j \in\{1,\ldots,\sqrt{n}/2^\ell\} j ∈ { 1 , … , n / 2 ℓ } , and
Σ ( X A ∩ Z j ) ≤ O ( ( t / n ) log 2 n ) \Sigma(X_A \cap Z_j) \le O((t/\sqrt{n})\log^2 n) Σ ( X A ∩ Z j ) ≤ O (( t / n ) log 2 n ) , for j ∈ { 1 , … , n } j \in\{1,\ldots,\sqrt{n}\} j ∈ { 1 , … , n } .
We now use these guarantees to compute the cardinalities and sums of relevant subsets of the light items. For each j ∈ { 1 , … , n } j \in\{1,\ldots,\sqrt{n}\} j ∈ { 1 , … , n } , we use Lemma 6 on ( a i ) i ∈ Z j ∩ A (a_i)_{i\in Z_j\cap A} ( a i ) i ∈ Z j ∩ A , with cardinality bound O ( n ) O(\sqrt{n}) O ( n ) and a cap of O ( ( t / n ) log 2 n ) O((t/\sqrt{n})\log^2 n) O (( t / n ) log 2 n ) on the sum, to compute
I j ( 0 ) = { ( ∣ S ∣ − ⌊ k n ⌋ , Σ ( S ) ) : S ⊆ Z j ∩ A , Σ ( S ) ≤ O ( ( t / n ) log 2 n ) , ∣ ∣ S ∣ − ⌊ k n ⌋ ∣ ≤ O ( n log n ) } . \mathcal{I}_j^{(0)}=\left\{\left(|S|-\left\lfloor\frac{k}{\sqrt{n}}\right\rfloor,\Sigma(S)\right):S\subseteq Z_j\cap A,\ \Sigma(S)\le O((t/\sqrt{n})\log^2 n),\ \left||S|-\left\lfloor\frac{k}{\sqrt{n}}\right\rfloor\right|\le O(\sqrt{n}\log n)\right\}. I j ( 0 ) = { ( ∣ S ∣ − ⌊ n k ⌋ , Σ ( S ) ) : S ⊆ Z j ∩ A , Σ ( S ) ≤ O (( t / n ) log 2 n ) , ∣ S ∣ − ⌊ n k ⌋ ≤ O ( n log n ) } .
An intuition behind I j ( 0 ) \mathcal{I}_j^{(0)} I j ( 0 ) is as follows. Notice that the expected value of ∣ X ∩ Z j ∣ |X\cap Z_j| ∣ X ∩ Z j ∣ over the randomness of color coding is exactly k / n k/\sqrt{n} k / n ; we only consider ⌊ k / n ⌋ \left\lfloor k/\sqrt{n}\right\rfloor ⌊ k / n ⌋ simply to work with integers, while staying close to the actual expected value. Moreover, since ∣ X ∖ X A ∣ = ∣ X B ∣ ≤ n |X\setminus X_A|=|X_B|\le\sqrt{n} ∣ X ∖ X A ∣ = ∣ X B ∣ ≤ n , it can be shown that the expected value of ∣ X A ∩ Z j ∣ |X_A\cap Z_j| ∣ X A ∩ Z j ∣ is in ( k / n − O ( log n ) , k / n + O ( log n ) ) (k/\sqrt{n}-O(\log n),k/\sqrt{n}+O(\log n)) ( k / n − O ( log n ) , k / n + O ( log n )) . Therefore, a pair ( d , w ) ∈ I j ( 0 ) (d,w)\in\mathcal{I}_j^{(0)} ( d , w ) ∈ I j ( 0 ) corresponds to the existence of (at least) one subset S ⊆ Z j ∩ A S\subseteq Z_j\cap A S ⊆ Z j ∩ A which sums up to w w w and has deviation of d ± O ( log n ) d\pm O(\log n) d ± O ( log n ) from the expected cardinality of ∣ X A ∩ Z j ∣ |X_A\cap Z_j| ∣ X A ∩ Z j ∣ . In fact, another application of the Chernoff bound shows that a pair corresponding to X A ∩ Z j X_A\cap Z_j X A ∩ Z j is in the set I j ( 0 ) \mathcal{I}_j^{(0)} I j ( 0 ) with high probability. Note that computing I j ( 0 ) \mathcal{I}_j^{(0)} I j ( 0 ) for all color classes takes time O ~ ( n + n t ) \widetilde{O}(n+\sqrt{n}t) O ( n + n t ) .
Continuing in the same spirit of working with pairs representing (i) deviation from expected size, and (ii) sum of the subset, our next aim is to obtain such pairs for subsets S ⊆ Z j ( ℓ ) ∩ A S\subseteq Z_j^{(\ell)}\cap A S ⊆ Z j ( ℓ ) ∩ A , even for ℓ > 0 \ell>0 ℓ > 0 . Observe that Z j ( ℓ ) = Z 2 j − 1 ( ℓ − 1 ) ∪ Z 2 j ( ℓ − 1 ) Z_j^{(\ell)}=Z_{2j-1}^{(\ell-1)}\cup Z_{2j}^{(\ell-1)} Z j ( ℓ ) = Z 2 j − 1 ( ℓ − 1 ) ∪ Z 2 j ( ℓ − 1 ) . If a subset S 1 ⊆ Z 2 j − 1 ( ℓ − 1 ) S_1\subseteq Z_{2j-1}^{(\ell-1)} S 1 ⊆ Z 2 j − 1 ( ℓ − 1 ) deviates from the expected size ∣ X ∩ Z 2 j − 1 ( ℓ − 1 ) ∣ |X\cap Z_{2j-1}^{(\ell-1)}| ∣ X ∩ Z 2 j − 1 ( ℓ − 1 ) ∣ (i.e. 2 ℓ − 1 k / n 2^{\ell-1}k/\sqrt{n} 2 ℓ − 1 k / n ) by d 1 ′ d'_1 d 1 ′ , and a subset S 2 ⊆ Z 2 j ( ℓ − 1 ) S_2\subseteq Z_{2j}^{(\ell-1)} S 2 ⊆ Z 2 j ( ℓ − 1 ) deviates from the expected size ∣ X ∩ Z 2 j ( ℓ − 1 ) ∣ |X\cap Z_{2j}^{(\ell-1)}| ∣ X ∩ Z 2 j ( ℓ − 1 ) ∣ (which is again 2 ℓ − 1 k / n 2^{\ell-1}k/\sqrt{n} 2 ℓ − 1 k / n ) by d 2 ′ d'_2 d 2 ′ , then their union S 1 ∪ S 2 S_1\cup S_2 S 1 ∪ S 2 deviates from the expected size of ∣ X ∩ Z j ( ℓ ) ∣ |X\cap Z_j^{(\ell)}| ∣ X ∩ Z j ( ℓ ) ∣ by d 1 ′ + d 2 ′ d'_1+d'_2 d 1 ′ + d 2 ′ . Moreover, the sum of S 1 ∪ S 2 S_1\cup S_2 S 1 ∪ S 2 is the sum of S 1 S_1 S 1 plus the sum of S 2 S_2 S 2 . This allows us to define a set I j ( ℓ ) \mathcal{I}_j^{(\ell)} I j ( ℓ ) of pairs and compute them recursively by taking the sum sets of the two lower-level sets I 2 j − 1 ( ℓ − 1 ) \mathcal{I}_{2j-1}^{(\ell-1)} I 2 j − 1 ( ℓ − 1 ) and I 2 j ( ℓ − 1 ) \mathcal{I}_{2j}^{(\ell-1)} I 2 j ( ℓ − 1 ) . Since we are working with the floor of the expectation, we must be slightly careful in the recursion. computation. Formally, for 1 ≤ ℓ ≤ 1 2 log n 1 \le\ell\le\frac{1}{2}\log n 1 ≤ ℓ ≤ 2 1 log n and 1 ≤ j ≤ n / 2 ℓ 1 \le j \le\sqrt{n}/2^\ell 1 ≤ j ≤ n / 2 ℓ , we recursively compute
T j ( ℓ ) = { ( d , w ) : ( d 1 , w 1 ) ∈ T 2 j − 1 ( ℓ − 1 ) , ( d 2 , w 2 ) ∈ T 2 j ( ℓ − 1 ) , w = w 1 + w 2 \mathcal{T}^{(\ell)}_j = \left\{(d,w) : (d_1,w_1) \in\mathcal{T}^{(\ell-1)}_{2j-1},\ (d_2,w_2) \in\mathcal{T}^{(\ell-1)}_{2j},\ w = w_1+w_2
\right. T j ( ℓ ) = { ( d , w ) : ( d 1 , w 1 ) ∈ T 2 j − 1 ( ℓ − 1 ) , ( d 2 , w 2 ) ∈ T 2 j ( ℓ − 1 ) , w = w 1 + w 2
d = ( d 1 + ⌊ 2 ℓ − 1 k n ⌋ ) + ( d 2 + ⌊ 2 ℓ − 1 k n ⌋ ) − ⌊ 2 ℓ k n ⌋ , ∣ d ∣ ≤ O ( n log n ) } . \left.
d = \left(d_1+\left\lfloor\frac{2^{\ell-1}k}{\sqrt{n}}\right\rfloor\right)+\left(d_2+\left\lfloor\frac{2^{\ell-1}k}{\sqrt{n}}\right\rfloor\right)-\left\lfloor\frac{2^\ell k}{\sqrt{n}}\right\rfloor,\ |d| \le O(\sqrt{n}\log n)\right\}. d = ( d 1 + ⌊ n 2 ℓ − 1 k ⌋ ) + ( d 2 + ⌊ n 2 ℓ − 1 k ⌋ ) − ⌊ n 2 ℓ k ⌋ , ∣ d ∣ ≤ O ( n log n ) } .
Note that we further trim the sets with the condition ∣ d ∣ ≤ O ( n log n ) |d| \le O(\sqrt{n}\log n) ∣ d ∣ ≤ O ( n log n ) . This ensures that the sizes of these sets are bounded by O ~ ( 2 ℓ ⋅ t + n ) \widetilde{O}(2^\ell\cdot t+\sqrt{n}) O ( 2 ℓ ⋅ t + n ) . Further, it is enough to guarantee (from the Chernoff bound) that with high probability, the pair corresponding to deviation and sum for X A ∩ Z j ( ℓ ) X_A \cap Z^{(\ell)}_j X A ∩ Z j ( ℓ ) survives this trimming, (in this and all other lower levels of the recursion) and stays in the set T j ( ℓ ) \mathcal{T}^{(\ell)}_j T j ( ℓ ) . In particular, with high probability, the pair ( ∣ X A ∣ − k , Σ ( X A ) ) = ( − ∣ X B ∣ , t − Σ ( X B ) ) (|X_A|-k,\Sigma(X_A))=(-|X_B|,t-\Sigma(X_B)) ( ∣ X A ∣ − k , Σ ( X A )) = ( − ∣ X B ∣ , t − Σ ( X B )) is in the set T 1 ( 1 2 log n ) \mathcal{T}^{(\frac{1}{2}\log n)}_1 T 1 ( 2 1 l o g n ) . We therefore return YES if some ( s , w ) ∈ J (s,w)\in\mathcal{J} ( s , w ) ∈ J satisfies ( − s , t − w ) ∈ T 1 ( 1 2 log n ) (-s,t-w)\in\mathcal{T}^{(\frac{1}{2}\log n)}_1 ( − s , t − w ) ∈ T 1 ( 2 1 l o g n ) , and NO otherwise. Such a pair gives disjoint subsets of B B B and A A A whose union has cardinality k k k and sum t t t .
It remains to bound the time for computing the sets T j ( ℓ ) \mathcal{T}^{(\ell)}_j T j ( ℓ ) . Every ( d , w ) ∈ T j ( ℓ ) (d,w)\in\mathcal{T}^{(\ell)}_j ( d , w ) ∈ T j ( ℓ ) satisfies ∣ d ∣ ≤ O ( n log n ) |d|\le O(\sqrt{n}\log n) ∣ d ∣ ≤ O ( n log n ) and it can be shown that w ≤ O ~ ( 2 ℓ t / n + 1 ) w\le\widetilde{O}(2^\ell t/\sqrt{n}+1) w ≤ O ( 2 ℓ t / n + 1 ) . Hence computing T j ( ℓ ) \mathcal{T}^{(\ell)}_j T j ( ℓ ) takes time O ~ ( n ⋅ ( 2 ℓ − 1 t / n + 1 ) ) = O ~ ( 2 ℓ t + n ) \widetilde{O}(\sqrt{n}\cdot(2^{\ell-1}t/\sqrt{n}+1))=\widetilde{O}(2^\ell t+\sqrt{n}) O ( n ⋅ ( 2 ℓ − 1 t / n + 1 )) = O ( 2 ℓ t + n ) using a fast convolution algorithm (see Lemma 28). There are n / 2 ℓ \sqrt{n}/2^\ell n / 2 ℓ such computations for a fixed value of ℓ \ell ℓ , and 1 2 log n \frac{1}{2}\log n 2 1 log n many values of ℓ \ell ℓ . Hence, the total running time amounts to ∑ ℓ = 1 1 2 log n n 2 ℓ O ~ ( 2 ℓ t + n ) = O ~ ( n + n t ) \sum_{\ell=1}^{\frac{1}{2}\log n}\frac{\sqrt{n}}{2^\ell}\widetilde{O}(2^\ell t+\sqrt{n})=\widetilde{O}(n+\sqrt{n}t) ∑ ℓ = 1 2 1 l o g n 2 ℓ n O ( 2 ℓ t + n ) = O ( n + n t ) . Together with the other steps, the total running time is O ~ ( n + n t ) \widetilde{O}(n+\sqrt{n}t) O ( n + n t ) . The details can be found in Theorem 17.
From TCSS to LCSS. To solve LCSS, we first decide whether there exists a subset with sum t t t and cardinality in an interval [ ℓ , h ] [\ell,h] [ ℓ , h ] : This can be done by appending h − ℓ h-\ell h − ℓ zero-valued items and calling a TCSS algorithm with target sum t t t and target cardinality h h h . With this, we can binary search for the largest cardinality of a subset summing up to t t t .
We first check if t t t is a possible subset sum; if not, we terminate. Henceforth, assume that there exists a subset summing up to t t t . Let k ∗ k^* k ∗ be the largest cardinality of a subset summing to t t t . We use binary search, starting with ℓ = 0 \ell=0 ℓ = 0 and h = n + 1 h=n+1 h = n + 1 , and maintain the invariant of k ∗ ∈ [ ℓ , h ) k^*\in[\ell,h) k ∗ ∈ [ ℓ , h ) throughout. At each iteration, we set m = ⌊ ( ℓ + h ) / 2 ⌋ m=\lfloor(\ell+h)/2\rfloor m = ⌊( ℓ + h ) /2 ⌋ , and check if there exists a subset with sum t t t and cardinality in [ m , h ) [m,h) [ m , h ) . Following the binary search framework, we set ℓ = m \ell=m ℓ = m if such a subset exists; otherwise we set h = m h=m h = m . When h = ℓ + 1 h=\ell+1 h = ℓ + 1 , we output ℓ = k ∗ \ell=k^* ℓ = k ∗ .
There are O ( log n ) O(\log n) O ( log n ) calls, each on O ( n ) O(n) O ( n ) items, giving a total running time of O ~ ( n + n t ) \widetilde{O}(n+\sqrt{n}t) O ( n + n t ) . More details can be found in Section 4.
It still remains open whether one can get better algorithms for LCSS, possibly with a running time of O ~ ( n + t ) \widetilde{O}(n+t) O ( n + t ) .
Solving All-targets Largest Cardinality Subset Sum
For AT-LCSS, we assume without loss of generality that all items are positive (otherwise we simply delete all zero items and add back their count in all outputs). Bellman’s dynamic program for knapsack [11 , 3 ] can be adapted to give us an O ( n t ) O(nt) O ( n t ) algorithm for AT-LCSS. We provide an algorithm with running time O ~ ( n + t 3 / 2 ) \widetilde{O}(n+t^{3/2}) O ( n + t 3/2 ) , which is an improvement in the regime t ≤ n 2 − ε t\le n^{2-\varepsilon} t ≤ n 2 − ε , for some ε > 0 \varepsilon>0 ε > 0 .
We follow the framework by Bringmann and Kociumaka as discussed in [17 ] (also see [7 Section 3] ). They consider the all-targets change-making problem; recall that given a set of denominations, the problem asks for the smallest number of coins which sum up to 1 , 2 , … , t 1, 2, \ldots, t 1 , 2 , … , t (as discussed in § 1 ). The framework can be broadly described as follows.
Set a threshold τ \tau τ . Classify denominations larger than τ \tau τ as heavy and the others light .
Compute relevant information from the heavy denominations in time O ~ ( t 2 / τ ) \widetilde{O}(t^{2}/\tau) O ( t 2 / τ ) . The crucial observation is to notice that in any solution, at most t / τ t/\tau t / τ many coins appear from a heavy denomination.
Incorporate on top all light denominations using a classical dynamic programming approach, which takes a total time of O ~ ( t τ ) \widetilde{O}(t\tau) O ( t τ ) . The crucial observation here is to notice that there are at most τ \tau τ many light denominations.
This framework produces an algorithm with a running-time of O ~ ( n + t 2 / τ + t τ ) \widetilde{O}(n+t^{2}/\tau+t\tau) O ( n + t 2 / τ + t τ ) . Setting τ = Θ ( t ) \tau=\Theta(\sqrt{t}) τ = Θ ( t ) minimizes this to O ~ ( n + t 3 / 2 ) \widetilde{O}(n+t^{3/2}) O ( n + t 3/2 ) .
Although AT-LCSS can be viewed as a 0-1 variant of the all-targets change-making problem, the specific algorithm for change-making does not directly give us an algorithm for AT-LCSS. We follow this broad framework of splitting into heavy and light items, but modify the specifics as follows.
Handling heavy items. Firstly, the algorithm for handling heavy denominations does not directly work for AT-LCSS. We overcome this obstacle using Lemma 6 instead: We compute the cardinalities and sums of all subsets of the heavy items with sum at most t t t and cardinality at most t / τ t/\tau t / τ . This takes a total time of O ~ ( n + t 2 / τ ) \widetilde{O}(n+t^{2}/\tau) O ( n + t 2 / τ ) and allows us to extract the necessary information regarding the heavy items.
Reducing the count of light items. Secondly, the algorithm for light denomination crucially requires the number of light denominations to be bounded by τ \tau τ . This is not the case for AT-LCSS; in fact, it is quite possible that all input items in an AT-LCSS are light, for some τ \tau τ . Moreover, it is important to keep track of the exact multiplicities of items. We resolve this issue using a multiplicity reduction trick by Lawler [16 ] (see also the discussion on preprocessing multisets in [4 Section 2] ) as follows. Consider each item initially having a profit of 1 1 1 , then maximizing cardinality is equivalent to maximizing profit. Whenever there are at least three copies of items with value w w w and profit p p p , we delete two such copies and add a new element with value 2 w 2w 2 w and profit 2 p 2p 2 p . Such an operation is safe as the maximum profits for all target sums remain unchanged. Moreover, this additionally guarantees the number of items is at most O ( τ log t ) O(\tau\log t) O ( τ log t ) . Moreover, this can be done in time O ( n + τ log t ) O(n+\tau\log t) O ( n + τ log t ) .
Solving All-Targets Largest Cardinality Subset Sum. We present an overview of the algorithm solving AT-LCSS following the above framework. First, we count the number of zeros z z z appearing in our input, and delete all such items. We add back z z z to every output at the final step of our algorithm. Now, fix a threshold τ ∈ { 1 , … , t } \tau\in\{1,\ldots,t\} τ ∈ { 1 , … , t } . Let H = { i ∈ { 1 , … , n } : a i > τ } H=\{i\in\{1,\ldots,n\}:a_i>\tau\} H = { i ∈ { 1 , … , n } : a i > τ } be the indices of the heavy elements, and let L = { i ∈ { 1 , … , n } : a i ≤ τ } L=\{i\in\{1,\ldots,n\}:a_i\le\tau\} L = { i ∈ { 1 , … , n } : a i ≤ τ } be the indices of the light elements.
We start by computing the output of AT-LCSS for just the heavy items. For s ∈ { 0 , … , t } s\in\{0,\ldots,t\} s ∈ { 0 , … , t } , let largest ( s ) \operatorname{largest}(s) largest ( s ) denote the largest cardinality of a subset of the heavy items summing up to exactly s s s . Since all heavy items have value at least τ \tau τ , we must have largest ( s ) ≤ s / τ ≤ t / τ \operatorname{largest}(s)\le s/\tau\le t/\tau largest ( s ) ≤ s / τ ≤ t / τ . Therefore to compute largest ( s ) \operatorname{largest}(s) largest ( s ) for all s ∈ { 0 , … , t } s\in\{0,\ldots,t\} s ∈ { 0 , … , t } , we use Lemma 6 on ( a i ) i ∈ H (a_i)_{i\in H} ( a i ) i ∈ H with cardinality bound t / τ t/\tau t / τ and a cap of t t t on the sum. That is, we compute
{ ( Σ ( S ) , ∣ S ∣ ) : S ⊆ H , ∣ S ∣ ≤ t / τ , Σ ( S ) ≤ t } ; \{(\Sigma(S), |S|): S\subseteq H, |S|\le t/\tau, \Sigma(S)\le t\}; {( Σ ( S ) , ∣ S ∣ ) : S ⊆ H , ∣ S ∣ ≤ t / τ , Σ ( S ) ≤ t } ;
this can be done in time O ~ ( n + t 2 / τ ) \widetilde{O}(n+t^2/\tau) O ( n + t 2 / τ ) . Then we iterate over all pairs ( Σ ( S ) , ∣ S ∣ ) (\Sigma(S), |S|) ( Σ ( S ) , ∣ S ∣ ) , and update largest ( Σ ( S ) ) \operatorname{largest}(\Sigma(S)) largest ( Σ ( S )) to ∣ S ∣ |S| ∣ S ∣ , if this causes an increase in its value.
To incorporate the light items, we use the multiplicity reduction trick by Lawler, i.e. in time O ( n + τ log t ) O(n+\tau\log t) O ( n + τ log t ) , we transform the light items into a multiset L \mathcal{L} L of at most O ( τ log t ) O(\tau\log t) O ( τ log t ) pairs of value and profit.
This allows us to use a dynamic programming approach as follows. First, let D P j [ s ] \mathrm{DP}_j[s] DP j [ s ] denote the maximum of the number of some selected heavy items plus the profit of some selected pairs among the first j j j pairs of L \mathcal{L} L , such that their total weight is s s s . Formally, if L \mathcal{L} L contains the elements ( w 1 , p 1 ) , … , ( w m , p m ) (w_1,p_1),\ldots,(w_m,p_m) ( w 1 , p 1 ) , … , ( w m , p m ) , where w 1 , … , w m w_1,\ldots,w_m w 1 , … , w m are the values and p 1 , … , p m p_1,\ldots,p_m p 1 , … , p m are the respective profits, then for 0 ≤ j ≤ m 0\le j\le m 0 ≤ j ≤ m and 0 ≤ s ≤ t 0\le s\le t 0 ≤ s ≤ t ,
D P j [ s ] : = max { ∣ S ∣ + ∑ i ∈ I p i : S ⊆ H , I ⊆ { 1 , … , j } , Σ ( S ) + ∑ i ∈ I w i = s } . \mathrm{DP}_j[s] := \max\left\{|S|+\sum_{i\in I}p_i:S\subseteq H,\ I\subseteq\{1,\ldots,j\},\ \Sigma(S)+\sum_{i\in I}w_i=s\right\}. DP j [ s ] := max { ∣ S ∣ + i ∈ I ∑ p i : S ⊆ H , I ⊆ { 1 , … , j } , Σ ( S ) + i ∈ I ∑ w i = s } .
For the base case, we set D P 0 [ s ] = largest ( s ) \mathrm{DP}_0[s]=\operatorname{largest}(s) DP 0 [ s ] = largest ( s ) for all s ∈ { 0 , … , t } s\in\{0,\ldots,t\} s ∈ { 0 , … , t } ; these are the largest cardinalities of subsets summing up to s s s , when no elements from L \mathcal{L} L are considered. For transitions, notice that D P j [ s ] \mathrm{DP}_j[s] DP j [ s ] can either contain the j j j -th pair or not contain it. We take the maximum of the two. Formally,
D P j [ s ] = { D P j − 1 [ s ] , s < w j , max { D P j − 1 [ s ] , D P j − 1 [ s − w j ] + p j } , s ≥ w j \mathrm{DP}_j[s]=
\begin{cases}
\mathrm{DP}_{j-1}[s], & s<w_j,\\
\max\{\mathrm{DP}_{j-1}[s],\mathrm{DP}_{j-1}[s-w_j]+p_j\}, & s\ge w_j
\end{cases} DP j [ s ] = { DP j − 1 [ s ] , max { DP j − 1 [ s ] , DP j − 1 [ s − w j ] + p j } , s < w j , s ≥ w j
After processing all items in L \mathcal{L} L , the answer is stored in D P m [ s ] \mathrm{DP}_m[s] DP m [ s ] for all s ∈ { 0 , … , t } s\in\{0,\ldots,t\} s ∈ { 0 , … , t } . We finally add back the count of the deleted zero-valued items giving us the output
( D P m [ 0 ] + z ) , ( D P m [ 1 ] + z ) , … , ( D P m [ t ] + z ) . (\mathrm{DP}_m[0]+z),(\mathrm{DP}_m[1]+z),\ldots,(\mathrm{DP}_m[t]+z). ( DP m [ 0 ] + z ) , ( DP m [ 1 ] + z ) , … , ( DP m [ t ] + z ) .
The dynamic program takes time O ( ∣ L ∣ ⋅ t ) = O ~ ( t τ ) O(|\mathcal{L}|\cdot t)=\widetilde{O}(t\tau) O ( ∣ L ∣ ⋅ t ) = O ( t τ ) . The total running time of the algorithm is therefore O ~ ( n + t 2 / τ + t τ ) \widetilde{O}(n+t^2/\tau+t\tau) O ( n + t 2 / τ + t τ ) . Selecting τ = Θ ( t ) \tau=\Theta(\sqrt{t}) τ = Θ ( t ) minimizes this running time to O ~ ( n + t 3 / 2 ) \widetilde{O}(n+t^{3/2}) O ( n + t 3/2 ) . The details can be found in § 5 .
It is interesting to note that although it is possible to modify Bringmann and Kociumaka’s framework for AT-LCSS, the improvements by Chan and He [7 ] do not extend directly. For the all targets change-making problem, they handle the heavy items identically, however for incorporating the light items a faster approach using binary (min, +)-convolution is used, making this step take merely O ~ ( t τ ) \widetilde{O}(t\sqrt{\tau}) O ( t τ ) time. Their algorithm hence has a total running time of O ~ ( n + t 2 / τ + t τ ) \widetilde{O}(n+t^2/\tau+t\sqrt{\tau}) O ( n + t 2 / τ + t τ ) . This is minimized by setting τ = Θ ( t 2 / 3 ) \tau=\Theta(t^{2/3}) τ = Θ ( t 2/3 ) to O ~ ( n + t 4 / 3 ) \widetilde{O}(n+t^{4/3}) O ( n + t 4/3 ) . This approach crucially uses the fact that coins of a particular denomination can be selected an arbitrary number of times as opposed to the 0-1 nature of AT-LCSS. It remains open to get O ~ ( n + t 4 / 3 ) \widetilde{O}(n+t^{4/3}) O ( n + t 4/3 ) -time algorithms for AT-LCSS, possibly using a smarter modification of the framework by Chan and He.
Preliminaries
All logarithms are base 2. For a positive integer m m m , we define [ m ] : = { 1 , … , m } [m]:=\{1,\ldots,m\} [ m ] := { 1 , … , m } and [ m ] 0 : = { 0 , … , m } [m]_0:=\{0,\ldots,m\} [ m ] 0 := { 0 , … , m } . We consider a multiset Z Z Z containing n n n non-negative integers a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a 1 , a 2 , … , a n . We denote Σ ( Z ) = ∑ i ∈ Z a i \Sigma(Z)=\sum_{i\in Z}a_i Σ ( Z ) = ∑ i ∈ Z a i . For sets P , Q ⊆ Z 2 P,Q\subseteq\mathbb{Z}^2 P , Q ⊆ Z 2 of pairs, we define their convolution P ⊕ Q P\oplus Q P ⊕ Q as { ( x 1 + x 2 , y 1 + y 2 ) : ( x 1 , y 1 ) ∈ P , ( x 2 , y 2 ) ∈ Q } \{(x_1+x_2,y_1+y_2):(x_1,y_1)\in P,\ (x_2,y_2)\in Q\} {( x 1 + x 2 , y 1 + y 2 ) : ( x 1 , y 1 ) ∈ P , ( x 2 , y 2 ) ∈ Q } . This can be computed efficiently (see Lemma 28 in Appendix § 7 .§ 1 ). For completeness, we also state the well-known Chernoff bound.
Lemma 8 (Chernoff Bound). Let Y 1 , Y 2 , … , Y n Y_1, Y_2, \ldots, Y_n Y 1 , Y 2 , … , Y n be independent Bernoulli random variables. Let Y = Y 1 + Y 2 + ⋯ + Y n Y = Y_1 + Y_2 + \cdots+ Y_n Y = Y 1 + Y 2 + ⋯ + Y n and let μ = E [ Y ] \mu= \mathbb{E}[Y] μ = E [ Y ] . Then, for all δ ∈ ( 0 , 1 ) \delta\in(0,1) δ ∈ ( 0 , 1 ) ,
Pr [ ∣ Y − μ ∣ ≥ δ μ ] ≤ 2 exp ( − δ 2 μ 3 ) . \Pr[|Y-\mu| \ge\delta\mu] \le2\exp\left(-\frac{\delta^2\mu}{3}\right). Pr [ ∣ Y − μ ∣ ≥ δ μ ] ≤ 2 exp ( − 3 δ 2 μ ) . Moreover, for any λ ≥ 2 e μ \lambda\ge2e\mu λ ≥ 2 e μ , we have Pr [ ∣ Y ∣ ≥ λ ] ≤ 2 − λ \Pr[|Y| \ge\lambda] \le2^{-\lambda} Pr [ ∣ Y ∣ ≥ λ ] ≤ 2 − λ .
Largest Cardinality Subset Sum
In this section we present a randomized algorithm for LCSS on nonnegative integers a 1 , … , a n a_1,\ldots,a_n a 1 , … , a n and target t t t (recall Definition 1). We first describe an algorithm for TCSS with target cardinality k k k (recall Definition 7), and then use it as a subroutine to solve LCSS. The running time of both algorithms is O ~ ( n + n t ) \widetilde{O}(n+\sqrt{n}t) O ( n + n t ) .
Algorithm for Target Cardinality Subset Sum
We may assume the following:
a i ∈ [ t + 1 ] 0 a_i \in[t+1]_0 a i ∈ [ t + 1 ] 0 for every i ∈ [ n ] i \in[n] i ∈ [ n ] (if not, we replace values greater than t t t by t + 1 t+1 t + 1 ), and
n n n is a power of four (if not, we simply add dummy items of value t + 1 t+1 t + 1 ).
The last assumption makes sure that n \sqrt{n} n , 1 2 log n \frac{1}{2}\log n 2 1 log n , etc are integers.
Color coding.
We perform a random color coding of the indices [ n ] [n] [ n ] into n \sqrt{n} n colors. Formally, let Z 1 ⊎ Z 2 ⊎ ⋯ ⊎ Z n Z_1 \uplus Z_2 \uplus\cdots\uplus Z_{\sqrt{n}} Z 1 ⊎ Z 2 ⊎ ⋯ ⊎ Z n be a random partition of [ n ] [n] [ n ] . That is, for each index i ∈ [ n ] i \in[n] i ∈ [ n ] , an integer j ∈ [ n ] j \in[\sqrt{n}] j ∈ [ n ] is chosen uniformly at random, and i i i is added to Z j Z_j Z j . We refer to these sets Z j Z_j Z j as color classes .
For ℓ ∈ [ 1 2 log n ] 0 \ell\in[\frac{1}{2}\log n]_0 ℓ ∈ [ 2 1 log n ] 0 and j ∈ [ n / 2 ℓ ] j \in[\sqrt{n}/2^\ell] j ∈ [ n / 2 ℓ ] , let Z j ( ℓ ) = Z 2 ℓ ( j − 1 ) + 1 ∪ Z 2 ℓ ( j − 1 ) + 2 ∪ ⋯ ∪ Z 2 ℓ ( j − 1 ) + 2 ℓ Z_j^{(\ell)} = Z_{2^\ell(j-1)+1} \cup Z_{2^\ell(j-1)+2} \cup\cdots\cup Z_{2^\ell(j-1)+2^\ell} Z j ( ℓ ) = Z 2 ℓ ( j − 1 ) + 1 ∪ Z 2 ℓ ( j − 1 ) + 2 ∪ ⋯ ∪ Z 2 ℓ ( j − 1 ) + 2 ℓ .
Segregating the input and the solution.
Assuming there exists a solution, we fix a solution X ⊆ [ n ] X \subseteq[n] X ⊆ [ n ] , i.e. with Σ ( X ) = t \Sigma(X)=t Σ ( X ) = t and ∣ X ∣ = k |X|=k ∣ X ∣ = k . First, we segregate the items into heavy and light weights as follows.
B : = { i ∈ [ n ] : a i ∈ ( t n , t + 1 ] } , X B : = X ∩ B , B := \left\{i \in[n] : a_i \in\left(\frac{t}{\sqrt{n}},t+1\right]\right\}, \qquad X_B := X \cap B, B := { i ∈ [ n ] : a i ∈ ( n t , t + 1 ] } , X B := X ∩ B ,
A : = [ n ] ∖ B , X A : = X ∩ A . A := [n] \setminus B, \qquad X_A := X \cap A. A := [ n ] ∖ B , X A := X ∩ A .
We also define the following sets.
C : = { i ∈ [ n ] : a i ∈ [ 0 , t n n ] } , X C : = X ∩ C , C := \left\{i \in[n] : a_i \in\left[0,\frac{t}{n\sqrt{n}}\right]\right\}, \qquad X_C := X \cap C, C := { i ∈ [ n ] : a i ∈ [ 0 , n n t ] } , X C := X ∩ C ,
L ℓ : = { i ∈ [ n ] : a i ∈ [ t 2 ℓ , t 2 ℓ − 1 ) } , X ℓ : = X ∩ L ℓ . L_\ell:= \left\{i \in[n] : a_i \in\left[\frac{t}{2^\ell},\frac{t}{2^{\ell-1}}\right)\right\}, \qquad X_\ell:= X \cap L_\ell. L ℓ := { i ∈ [ n ] : a i ∈ [ 2 ℓ t , 2 ℓ − 1 t ) } , X ℓ := X ∩ L ℓ .
Here L ℓ L_\ell L ℓ and X ℓ X_\ell X ℓ are defined for every integer ℓ ∈ { 1 2 log n + 1 , … , 3 2 log n } \ell\in\{\frac{1}{2}\log n+1,\ldots,\frac{3}{2}\log n\} ℓ ∈ { 2 1 log n + 1 , … , 2 3 log n } . We refer to the sets L ℓ L_\ell L ℓ as layers . We observe that these sets partition the sets [ n ] [n] [ n ] and X X X respectively;
[ n ] = B ⊎ C ⊎ ( ⋃ ℓ = 1 2 log n + 1 3 2 log n L ℓ ) , and X = X B ⊎ X C ⊎ ( ⋃ ℓ = 1 2 log n + 1 3 2 log n X ℓ ) . [n] = B \uplus C \uplus\left(\bigcup_{\ell=\frac{1}{2}\log n+1}^{\frac{3}{2}\log n} L_\ell\right), \qquad\text{and} \qquad X = X_B \uplus X_C \uplus\left(\bigcup_{\ell=\frac{1}{2}\log n+1}^{\frac{3}{2}\log n} X_\ell\right). [ n ] = B ⊎ C ⊎ ℓ = 2 1 l o g n + 1 ⋃ 2 3 l o g n L ℓ , and X = X B ⊎ X C ⊎ ℓ = 2 1 l o g n + 1 ⋃ 2 3 l o g n X ℓ .
These sets will help us define and derive various probabilistic guarantees. These, in turn, culminate in useful structural properties that we leverage to design our algorithm.
Probabilistic guarantees.
The goal of this section is to prove that the randomness in the color coding step guarantees that the input would be somewhat well structured. We prove various guarantees and unify them later in Lemma 16. All the probabilities in this section are over the random choice of the partition, i.e. the color coding. Moreover, recall that c ≥ 10 c \ge10 c ≥ 10 is a fixed constant.
We begin by showing that each color class has O ( n ) O(\sqrt{n}) O ( n ) items with high probability. This bounds the size of the sub-instances processed by the algorithm. For this bound, we define
Γ Z ( s i z e ) : = 2 e n + 4 c log n ≤ O ( n ) . (1) \Gamma_Z^{(\mathrm{size})} := 2e\sqrt{n} + 4c\log n \le O(\sqrt{n}). \tag*{(1)} Γ Z ( size ) := 2 e n + 4 c log n ≤ O ( n ) . (1)
Lemma 9. For every j ∈ [ n ] j \in[\sqrt{n}] j ∈ [ n ] , Pr [ ∣ Z j ∣ ≥ Γ Z ( s i z e ) ] ≤ n − 4 c \Pr[|Z_j| \ge\Gamma_Z^{(\mathrm{size})}] \le n^{-4c} Pr [ ∣ Z j ∣ ≥ Γ Z ( size ) ] ≤ n − 4 c .
Proof. We write ∣ Z j ∣ = ∑ i = 1 n Y i |Z_j| = \sum_{i=1}^{n} Y_i ∣ Z j ∣ = ∑ i = 1 n Y i , where Y i Y_i Y i is a random variable indicating whether i ∈ Z j i \in Z_j i ∈ Z j . These variables are independent and E [ ∣ Z j ∣ ] = n \mathbb{E}[|Z_j|] = \sqrt{n} E [ ∣ Z j ∣ ] = n . Therefore Γ Z ( s i z e ) ≥ 2 e n ≥ 2 e E [ ∣ Z j ∣ ] \Gamma_Z^{(\mathrm{size})} \ge2e\sqrt{n} \ge2e\mathbb{E}[|Z_j|] Γ Z ( size ) ≥ 2 e n ≥ 2 e E [ ∣ Z j ∣ ] making Lemma 8 applicable. This gives Pr [ ∣ Z j ∣ ≥ Γ Z ( s i z e ) ] ≤ 2 − Γ Z ( s i z e ) ≤ 2 − 4 c log n = n − 4 c \Pr[|Z_j| \ge\Gamma_Z^{(\mathrm{size})}] \le2^{-\Gamma_Z^{(\mathrm{size})}} \le2^{-4c\log n} = n^{-4c} Pr [ ∣ Z j ∣ ≥ Γ Z ( size ) ] ≤ 2 − Γ Z ( size ) ≤ 2 − 4 c l o g n = n − 4 c .
We next bound the deviation of ∣ X ∩ Z j ( ℓ ) ∣ |X \cap Z_j^{(\ell)}| ∣ X ∩ Z j ( ℓ ) ∣ from its expectation 2 ℓ k / n 2^\ell k/\sqrt{n} 2 ℓ k / n . At each level, this deviation is O ( n log n ) O(\sqrt{n}\log n) O ( n log n ) with high probability. To state the bound, we define
Γ X , Z ( s i z e ) : = 16 c n log n ≤ O ( n log n ) . (2) \Gamma_{X,Z}^{(\mathrm{size})} := 16c\sqrt{n}\log n \le O(\sqrt{n}\log n). \tag*{(2)} Γ X , Z ( size ) := 16 c n log n ≤ O ( n log n ) . (2)
Lemma 10. For all ℓ ∈ [ 1 2 log n ] 0 \ell\in[\frac{1}{2}\log n]_0 ℓ ∈ [ 2 1 log n ] 0 , j ∈ [ n / 2 ℓ ] j \in[\sqrt{n}/2^\ell] j ∈ [ n / 2 ℓ ] ,
Pr [ ∣ ∣ X ∩ Z j ( ℓ ) ∣ − 2 ℓ k n ∣ ≥ Γ X , Z ( s i z e ) ] ≤ n − 4 c . \Pr\left[\left||X \cap Z_j^{(\ell)}| - \frac{2^\ell k}{\sqrt{n}}\right| \ge\Gamma_{X,Z}^{(\mathrm{size})}\right] \le n^{-4c}. Pr [ ∣ X ∩ Z j ( ℓ ) ∣ − n 2 ℓ k ≥ Γ X , Z ( size ) ] ≤ n − 4 c . Proof. If k < Γ X , Z ( s i z e ) k < \Gamma_{X,Z}^{(\mathrm{size})} k < Γ X , Z ( size ) , both ∣ X ∩ Z j ( ℓ ) ∣ |X \cap Z_j^{(\ell)}| ∣ X ∩ Z j ( ℓ ) ∣ and 2 ℓ k / n 2^\ell k/\sqrt{n} 2 ℓ k / n lie in [ 0 , k ] [0,k] [ 0 , k ] . Their difference in absolute value is therefore at most k < Γ X , Z ( s i z e ) k < \Gamma_{X,Z}^{(\mathrm{size})} k < Γ X , Z ( size ) .
Otherwise, ∣ X ∩ Z j ( ℓ ) ∣ |X \cap Z_j^{(\ell)}| ∣ X ∩ Z j ( ℓ ) ∣ is a sum of k k k independent Bernoulli variables with mean μ = 2 ℓ k / n \mu= 2^\ell k/\sqrt{n} μ = 2 ℓ k / n . We set δ = 3 ( 4 c + 1 ) log n / μ \delta= \sqrt{3(4c+1)\log n/\mu} δ = 3 ( 4 c + 1 ) log n / μ . Since μ ≥ 16 c ⋅ 2 ℓ log n \mu\ge16c\cdot2^\ell\log n μ ≥ 16 c ⋅ 2 ℓ log n and 3 ( 4 c + 1 ) < 16 c 3(4c+1) < 16c 3 ( 4 c + 1 ) < 16 c , we have 0 < δ < 1 0 < \delta< 1 0 < δ < 1 . Lemma 8 gives
Pr [ ∣ ∣ X ∩ Z j ( ℓ ) ∣ − μ ∣ ≥ 3 ( 4 c + 1 ) μ log n ] ≤ 2 exp ( − ( 4 c + 1 ) log n ) ≤ n − 4 c . \Pr\left[\left||X \cap Z_j^{(\ell)}|-\mu\right| \ge\sqrt{3(4c+1)\mu\log n}\right] \le2\exp(-(4c+1)\log n) \le n^{-4c}. Pr [ ∣ X ∩ Z j ( ℓ ) ∣ − μ ≥ 3 ( 4 c + 1 ) μ log n ] ≤ 2 exp ( − ( 4 c + 1 ) log n ) ≤ n − 4 c . The last inequality follows from 2 exp ( − ( 4 c + 1 ) log n ) ≤ 2 n − ( 4 c + 1 ) ≤ n − 4 c 2\exp(-(4c+1)\log n) \le2n^{-(4c+1)} \le n^{-4c} 2 exp ( − ( 4 c + 1 ) log n ) ≤ 2 n − ( 4 c + 1 ) ≤ n − 4 c , since logarithms are base two and n ≥ 4 n \ge4 n ≥ 4 . Finally, μ ≤ k ≤ n \mu\le k \le n μ ≤ k ≤ n , 3 ( 4 c + 1 ) < 16 c 3(4c+1) < 16c 3 ( 4 c + 1 ) < 16 c , and log n ≥ 1 \log n \ge1 log n ≥ 1 imply 3 ( 4 c + 1 ) μ log n < 4 c n log n < Γ X , Z ( s i z e ) \sqrt{3(4c+1)\mu\log n} < 4\sqrt{c}\sqrt{n}\log n < \Gamma_{X,Z}^{(\mathrm{size})} 3 ( 4 c + 1 ) μ log n < 4 c n log n < Γ X , Z ( size ) by Equation (2), proving the claim.
Note that ∣ X B ∣ ≤ n |X_B| \le\sqrt{n} ∣ X B ∣ ≤ n , as every item of X B X_B X B has value greater than t / n t/\sqrt{n} t / n ; if ∣ X B ∣ > n |X_B| > \sqrt{n} ∣ X B ∣ > n , then Σ ( X ) ≥ Σ ( X B ) > t \Sigma(X) \ge\Sigma(X_B) > t Σ ( X ) ≥ Σ ( X B ) > t , a contradiction. Removing these items changes the preceding cardinality bound by at most n \sqrt{n} n , giving the following bound for X A X_A X A . For this, we define
Γ A , Z ( s i z e ) : = 20 c n log n ≤ O ( n log n ) . (3) \Gamma_{A,Z}^{(\mathrm{size})} := 20c\sqrt{n}\log n \le O(\sqrt{n}\log n). \tag*{(3)} Γ A , Z ( size ) := 20 c n log n ≤ O ( n log n ) . (3)
Lemma 11. For all ℓ ∈ [ 1 2 log n ] 0 \ell\in[\frac{1}{2}\log n]_0 ℓ ∈ [ 2 1 log n ] 0 and j ∈ [ n / 2 ℓ ] j \in[\sqrt{n}/2^\ell] j ∈ [ n / 2 ℓ ] ,
Pr [ ∣ ∣ X A ∩ Z j ( ℓ ) ∣ − 2 ℓ k n ∣ ≥ Γ A , Z ( s i z e ) ] ≤ n − 4 c . \Pr\left[\left||X_A \cap Z_j^{(\ell)}| - \frac{2^\ell k}{\sqrt{n}}\right| \ge\Gamma_{A,Z}^{(\mathrm{size})}\right] \le n^{-4c}. Pr [ ∣ X A ∩ Z j ( ℓ ) ∣ − n 2 ℓ k ≥ Γ A , Z ( size ) ] ≤ n − 4 c . Proof. Using X = X A ⊎ X B X = X_A \uplus X_B X = X A ⊎ X B and ∣ X B ∣ ≤ n |X_B| \le\sqrt{n} ∣ X B ∣ ≤ n , we obtain
∣ ∣ X A ∩ Z j ( ℓ ) ∣ − 2 ℓ k n ∣ ≤ ∣ ∣ X ∩ Z j ( ℓ ) ∣ − 2 ℓ k n ∣ + ∣ X B ∩ Z j ( ℓ ) ∣ ≤ ∣ ∣ X ∩ Z j ( ℓ ) ∣ − 2 ℓ k n ∣ + n . \left||X_A \cap Z_j^{(\ell)}| - \frac{2^\ell k}{\sqrt{n}}\right| \le\left||X \cap Z_j^{(\ell)}| - \frac{2^\ell k}{\sqrt{n}}\right| + |X_B \cap Z_j^{(\ell)}| \le\left||X \cap Z_j^{(\ell)}| - \frac{2^\ell k}{\sqrt{n}}\right| + \sqrt{n}. ∣ X A ∩ Z j ( ℓ ) ∣ − n 2 ℓ k ≤ ∣ X ∩ Z j ( ℓ ) ∣ − n 2 ℓ k + ∣ X B ∩ Z j ( ℓ ) ∣ ≤ ∣ X ∩ Z j ( ℓ ) ∣ − n 2 ℓ k + n . By Equations (2) and (3), since 4 c log n ≥ 1 4c\log n \ge1 4 c log n ≥ 1 , a deviation of at least Γ A , Z ( s i z e ) \Gamma_{A,Z}^{(\mathrm{size})} Γ A , Z ( size ) on the left implies a deviation of at least Γ X , Z ( s i z e ) \Gamma_{X,Z}^{(\mathrm{size})} Γ X , Z ( size ) for X X X . Lemma 10 now gives the desired bound.
the next lemma states that each color class Z j Z_j Z j contains at most O ( log n ) O(\log n) O ( log n ) items from X B X_B X B , the large items of the fixed solution, with high probability. For this, we first define
Γ B , Z ( s i z e ) : = 4 c log n ≤ O ( log n ) . (4) \Gamma^{(\mathrm{size})}_{B,Z} := 4c\log n \le O(\log n). \tag*{(4)} Γ B , Z ( size ) := 4 c log n ≤ O ( log n ) . (4)
Lemma 12. For all j ∈ [ n ] j \in[\sqrt{n}] j ∈ [ n ] , we have Pr [ ∣ X B ∩ Z j ∣ ≥ Γ B , Z ( s i z e ) ] ≤ n − 4 c \Pr[|X_B \cap Z_j| \ge\Gamma^{(\mathrm{size})}_{B,Z}] \le n^{-4c} Pr [ ∣ X B ∩ Z j ∣ ≥ Γ B , Z ( size ) ] ≤ n − 4 c .
Proof. We observe that E [ ∣ X B ∩ Z j ∣ ] = ∣ X B ∣ / n ≤ 1 \mathbb{E}[|X_B \cap Z_j|] = |X_B|/\sqrt{n} \le1 E [ ∣ X B ∩ Z j ∣ ] = ∣ X B ∣/ n ≤ 1 , where the inequality is because ∣ X B ∣ ≤ n |X_B| \le\sqrt{n} ∣ X B ∣ ≤ n . Moreover, ∣ X B ∩ Z j ∣ = ∑ i ′ ∈ X B Y i ′ |X_B \cap Z_j| = \sum_{i' \in X_B} Y_{i'} ∣ X B ∩ Z j ∣ = ∑ i ′ ∈ X B Y i ′ , where Y i ′ Y_{i'} Y i ′ is the indicator variable of whether i ′ ∈ Z j i' \in Z_j i ′ ∈ Z j . Since 2 e ⋅ E [ ∣ X B ∩ Z j ∣ ] ≤ Γ B , Z ( s i z e ) 2e \cdot\mathbb{E}[|X_B \cap Z_j|] \le\Gamma^{(\mathrm{size})}_{B,Z} 2 e ⋅ E [ ∣ X B ∩ Z j ∣ ] ≤ Γ B , Z ( size ) , by Lemma 8 and Equation (4) we have Pr [ ∣ X B ∩ Z j ∣ ≥ Γ B , Z ( s i z e ) ] ≤ 2 − Γ B , Z ( s i z e ) = n − 4 c \Pr[|X_B \cap Z_j| \ge\Gamma^{(\mathrm{size})}_{B,Z}] \le2^{-\Gamma^{(\mathrm{size})}_{B,Z}} = n^{-4c} Pr [ ∣ X B ∩ Z j ∣ ≥ Γ B , Z ( size ) ] ≤ 2 − Γ B , Z ( size ) = n − 4 c .
For each layer L ℓ L_\ell L ℓ , we similarly bound the number of solution items in a color class. The bound is O ( 2 ℓ / n + log n ) O(2^\ell/\sqrt{n}+\log n) O ( 2 ℓ / n + log n ) with high probability. To state it, we define
Γ ℓ , Z ( s i z e ) : = 2 e ⋅ 2 ℓ n + Γ B , Z ( s i z e ) ≤ O ( 2 ℓ / n + log n ) . (5) \Gamma^{(\mathrm{size})}_{\ell,Z} := \frac{2e\cdot2^\ell}{\sqrt{n}}+\Gamma^{(\mathrm{size})}_{B,Z} \le O(2^\ell/\sqrt{n}+\log n). \tag*{(5)} Γ ℓ , Z ( size ) := n 2 e ⋅ 2 ℓ + Γ B , Z ( size ) ≤ O ( 2 ℓ / n + log n ) . (5)
Lemma 13. Pr [ ∣ X ℓ ∩ Z j ∣ ≥ Γ ℓ , Z ( s i z e ) ] ≤ n − 4 c \Pr[|X_\ell\cap Z_j| \ge\Gamma^{(\mathrm{size})}_{\ell,Z}] \le n^{-4c} Pr [ ∣ X ℓ ∩ Z j ∣ ≥ Γ ℓ , Z ( size ) ] ≤ n − 4 c for all j ∈ [ n ] j \in[\sqrt{n}] j ∈ [ n ] , ℓ ∈ { 1 2 log n + 1 , … , 3 2 log n } \ell\in\{\frac{1}{2}\log n+1,\ldots,\frac{3}{2}\log n\} ℓ ∈ { 2 1 log n + 1 , … , 2 3 log n } .
Proof. We observe that ∣ X ℓ ∣ ≤ 2 ℓ |X_\ell| \le2^\ell ∣ X ℓ ∣ ≤ 2 ℓ , as a i ′ ∈ ( t 2 ℓ , t 2 ℓ − 1 ] a_{i'} \in\left(\frac{t}{2^\ell},\frac{t}{2^{\ell-1}}\right] a i ′ ∈ ( 2 ℓ t , 2 ℓ − 1 t ] for every i ′ ∈ X ℓ i' \in X_\ell i ′ ∈ X ℓ and Σ ( X ℓ ) ≤ Σ ( X ) = t \Sigma(X_\ell) \le\Sigma(X) = t Σ ( X ℓ ) ≤ Σ ( X ) = t . Therefore, E [ ∣ X ℓ ∩ Z j ∣ ] = ∣ X ℓ ∣ / n ≤ 2 ℓ / n \mathbb{E}[|X_\ell\cap Z_j|] = |X_\ell|/\sqrt{n} \le2^\ell/\sqrt{n} E [ ∣ X ℓ ∩ Z j ∣ ] = ∣ X ℓ ∣/ n ≤ 2 ℓ / n . Moreover, ∣ X ℓ ∩ Z j ∣ = ∑ i ′ ∈ X ℓ Y i ′ |X_\ell\cap Z_j| = \sum_{i' \in X_\ell} Y_{i'} ∣ X ℓ ∩ Z j ∣ = ∑ i ′ ∈ X ℓ Y i ′ , where Y i ′ Y_{i'} Y i ′ is the indicator variable of whether i ′ ∈ Z j i' \in Z_j i ′ ∈ Z j . Since 2 e ⋅ E [ ∣ X ℓ ∩ Z j ∣ ] ≤ 2 ℓ + 1 e / n ≤ Γ ℓ , Z ( s i z e ) 2e\cdot\mathbb{E}[|X_\ell\cap Z_j|] \le2^{\ell+1}e/\sqrt{n} \le\Gamma^{(\mathrm{size})}_{\ell,Z} 2 e ⋅ E [ ∣ X ℓ ∩ Z j ∣ ] ≤ 2 ℓ + 1 e / n ≤ Γ ℓ , Z ( size ) by Equation (5), Lemma 8 gives Pr [ ∣ X ℓ ∩ Z j ∣ ≥ Γ ℓ , Z ( s i z e ) ] ≤ 2 − Γ ℓ , Z ( s i z e ) ≤ 2 − Γ B , Z ( s i z e ) = n − 4 c \Pr[|X_\ell\cap Z_j| \ge\Gamma^{(\mathrm{size})}_{\ell,Z}] \le2^{-\Gamma^{(\mathrm{size})}_{\ell,Z}} \le2^{-\Gamma^{(\mathrm{size})}_{B,Z}} = n^{-4c} Pr [ ∣ X ℓ ∩ Z j ∣ ≥ Γ ℓ , Z ( size ) ] ≤ 2 − Γ ℓ , Z ( size ) ≤ 2 − Γ B , Z ( size ) = n − 4 c . The last equality uses Equation (4). This proves the lemma.
We next convert this cardinality bound into a weight bound: for fixed ℓ \ell ℓ and j j j , the items in X ℓ ∩ Z j X_\ell\cap Z_j X ℓ ∩ Z j have total weight O ( ( t / n ) log n ) O((t/\sqrt{n})\log n) O (( t / n ) log n ) with high probability. For this bound, define
Γ ℓ , Z ( s u m ) : = 6 c t n log n ≤ O ( ( t / n ) log n ) . (6) \Gamma^{(\mathrm{sum})}_{\ell,Z} := 6c\frac{t}{\sqrt{n}}\log n \le O((t/\sqrt{n})\log n). \tag*{(6)} Γ ℓ , Z ( sum ) := 6 c n t log n ≤ O (( t / n ) log n ) . (6)
Lemma 14. Pr [ Σ ( X ℓ ∩ Z j ) ≥ Γ ℓ , Z ( s u m ) ] ≤ n − 4 c \Pr[\Sigma(X_\ell\cap Z_j) \ge\Gamma^{(\mathrm{sum})}_{\ell,Z}] \le n^{-4c} Pr [ Σ ( X ℓ ∩ Z j ) ≥ Γ ℓ , Z ( sum ) ] ≤ n − 4 c for all j ∈ [ n ] j \in[\sqrt{n}] j ∈ [ n ] , ℓ ∈ { 1 2 log n + 1 , … , 3 2 log n } \ell\in\{\frac{1}{2}\log n+1,\ldots,\frac{3}{2}\log n\} ℓ ∈ { 2 1 log n + 1 , … , 2 3 log n } .
Proof. Note that 2 ℓ ≥ 2 n 2^\ell\ge2\sqrt{n} 2 ℓ ≥ 2 n and a i ′ ∈ ( t 2 ℓ , t 2 ℓ − 1 ] a_{i'} \in\left(\frac{t}{2^\ell},\frac{t}{2^{\ell-1}}\right] a i ′ ∈ ( 2 ℓ t , 2 ℓ − 1 t ] for every i ′ ∈ X ℓ i' \in X_\ell i ′ ∈ X ℓ . Assume the event ∣ X ℓ ∩ Z j ∣ < Γ ℓ , Z ( s i z e ) |X_\ell\cap Z_j| < \Gamma^{(\mathrm{size})}_{\ell,Z} ∣ X ℓ ∩ Z j ∣ < Γ ℓ , Z ( size ) . This implies Σ ( X ℓ ∩ Z j ) < Γ ℓ , Z ( s i z e ) ⋅ t 2 ℓ − 1 \Sigma(X_\ell\cap Z_j) < \Gamma^{(\mathrm{size})}_{\ell,Z} \cdot\frac{t}{2^{\ell-1}} Σ ( X ℓ ∩ Z j ) < Γ ℓ , Z ( size ) ⋅ 2 ℓ − 1 t . Substituting the values of Γ ℓ , Z ( s i z e ) \Gamma^{(\mathrm{size})}_{\ell,Z} Γ ℓ , Z ( size ) , Γ B , Z ( s i z e ) \Gamma^{(\mathrm{size})}_{B,Z} Γ B , Z ( size ) and Γ ℓ , Z ( s u m ) \Gamma^{(\mathrm{sum})}_{\ell,Z} Γ ℓ , Z ( sum ) (see Equations (4)--(6)), we get
Σ ( X ℓ ∩ Z j ) < Γ ℓ , Z ( s i z e ) ⋅ t 2 ℓ − 1 = 4 e t n + Γ B , Z ( s i z e ) t 2 ℓ − 1 ≤ 4 e t n + Γ B , Z ( s i z e ) t n ≤ Γ ℓ , Z ( s u m ) . \Sigma(X_\ell\cap Z_j) < \Gamma^{(\mathrm{size})}_{\ell,Z} \cdot\frac{t}{2^{\ell-1}} = \frac{4et}{\sqrt{n}}+\frac{\Gamma^{(\mathrm{size})}_{B,Z}t}{2^{\ell-1}} \le\frac{4et}{\sqrt{n}}+\frac{\Gamma^{(\mathrm{size})}_{B,Z}t}{\sqrt{n}} \le\Gamma^{(\mathrm{sum})}_{\ell,Z}. Σ ( X ℓ ∩ Z j ) < Γ ℓ , Z ( size ) ⋅ 2 ℓ − 1 t = n 4 e t + 2 ℓ − 1 Γ B , Z ( size ) t ≤ n 4 e t + n Γ B , Z ( size ) t ≤ Γ ℓ , Z ( sum ) .
Lemma 13 therefore implies Pr [ Σ ( X ℓ ∩ Z j ) ≥ Γ ℓ , Z ( s u m ) ] ≤ Pr [ ∣ X ℓ ∩ Z j ∣ ≥ Γ ℓ , Z ( s i z e ) ] ≤ n − 4 c \Pr[\Sigma(X_\ell\cap Z_j) \ge\Gamma^{(\mathrm{sum})}_{\ell,Z}] \le\Pr[|X_\ell\cap Z_j| \ge\Gamma^{(\mathrm{size})}_{\ell,Z}] \le n^{-4c} Pr [ Σ ( X ℓ ∩ Z j ) ≥ Γ ℓ , Z ( sum ) ] ≤ Pr [ ∣ X ℓ ∩ Z j ∣ ≥ Γ ℓ , Z ( size ) ] ≤ n − 4 c .
Summing over the layers and including the smallest items in X C X_C X C gives a bound of O ( ( t / n ) log 2 n ) O((t/\sqrt{n})\log^2 n) O (( t / n ) log 2 n ) on the total weight of X A ∩ Z j X_A \cap Z_j X A ∩ Z j , with high probability. We define
Γ A , Z ( s u m ) : = 7 c ( t / n ) log 2 n ≤ O ( ( t / n ) log 2 n ) . (7) \Gamma^{(\mathrm{sum})}_{A,Z} := 7c(t/\sqrt{n})\log^2 n \le O((t/\sqrt{n})\log^2 n). \tag*{(7)} Γ A , Z ( sum ) := 7 c ( t / n ) log 2 n ≤ O (( t / n ) log 2 n ) . (7)
Lemma 15. For j ∈ [ n ] j \in[\sqrt{n}] j ∈ [ n ] , we have Pr [ Σ ( X A ∩ Z j ) ≥ Γ A , Z ( s u m ) ] ≤ n − 3 c \Pr[\Sigma(X_A \cap Z_j) \ge\Gamma^{(\mathrm{sum})}_{A,Z}] \le n^{-3c} Pr [ Σ ( X A ∩ Z j ) ≥ Γ A , Z ( sum ) ] ≤ n − 3 c .
Proof. There are exactly log n \log n log n layers L ℓ L_\ell L ℓ . By Lemma 14 and a union bound, with probability at least 1 − ( log n ) n − 4 c 1-(\log n)n^{-4c} 1 − ( log n ) n − 4 c , every one of them satisfies Σ ( X ℓ ∩ Z j ) < Γ ℓ , Z ( s u m ) \Sigma(X_\ell\cap Z_j)<\Gamma_{\ell,Z}^{(\mathrm{sum})} Σ ( X ℓ ∩ Z j ) < Γ ℓ , Z ( sum ) . Also, deterministically, Σ ( X C ∩ Z j ) ≤ ∣ X C ∣ t n n ≤ t n \Sigma(X_C\cap Z_j)\leq|X_C|\frac{t}{n\sqrt{n}}\leq\frac{t}{\sqrt{n}} Σ ( X C ∩ Z j ) ≤ ∣ X C ∣ n n t ≤ n t . Since X A X_A X A is the disjoint union of X C X_C X C and the sets X ℓ X_\ell X ℓ , these inequalities imply Σ ( X A ∩ Z j ) < log n ⋅ Γ ℓ , Z ( s u m ) + t n ≤ Γ A , Z ( s u m ) \Sigma(X_A\cap Z_j)<\log n\cdot\Gamma_{\ell,Z}^{(\mathrm{sum})}+\frac{t}{\sqrt{n}}\leq\Gamma_{A,Z}^{(\mathrm{sum})} Σ ( X A ∩ Z j ) < log n ⋅ Γ ℓ , Z ( sum ) + n t ≤ Γ A , Z ( sum ) . The last inequality uses Equations (6) and (7) . The probability of this is therefore at least 1 − ( log n ) n − 4 c ≥ 1 − n ⋅ n − 4 c ≥ 1 − n − 3 c 1-(\log n)n^{-4c}\geq1-n\cdot n^{-4c}\geq1-n^{-3c} 1 − ( log n ) n − 4 c ≥ 1 − n ⋅ n − 4 c ≥ 1 − n − 3 c .
The thresholds used in the preceding bounds are collected in Table 1 for reference.
Table 1. Thresholds in the color-class analysis.
The next lemma aggregates all proven guarantees into one single statement. It follows by a union bound from Lemmas 9 , 11 , and 15 .
Lemma 16 (List of probabilistic guarantees). Over the random choice of the partition of the set [ n ] [n] [ n ] , all the events below happen with probability at least 1 − n − 3 c 1-n^{-3c} 1 − n − 3 c .
∣ Z j ∣ ≤ Γ Z ( s i z e ) ≤ O ( n ) |Z_j|\leq\Gamma_Z^{(\mathrm{size})}\leq O(\sqrt{n}) ∣ Z j ∣ ≤ Γ Z ( size ) ≤ O ( n ) , for j ∈ [ n ] j\in[\sqrt{n}] j ∈ [ n ] ,
∣ X A ∩ Z j ( ℓ ) ∣ − 2 ℓ k n ≤ Γ A , Z ( s i z e ) ≤ O ( n log n ) \left|X_A\cap Z_j^{(\ell)}\right|-\frac{2^{\ell k}}{\sqrt{n}}\leq\Gamma_{A,Z}^{(\mathrm{size})}\leq O(\sqrt{n}\log n) X A ∩ Z j ( ℓ ) − n 2 ℓ k ≤ Γ A , Z ( size ) ≤ O ( n log n ) , for ℓ ∈ [ 1 2 log n ] 0 \ell\in[\frac{1}{2}\log n]_0 ℓ ∈ [ 2 1 log n ] 0 , j ∈ [ n / 2 ℓ ] j\in[\sqrt{n/2^\ell}] j ∈ [ n / 2 ℓ ] , and
Σ ( X A ∩ Z j ) ≤ Γ A , Z ( s u m ) ≤ O ( ( t / n ) log 2 n ) \Sigma(X_A\cap Z_j)\leq\Gamma_{A,Z}^{(\mathrm{sum})}\leq O((t/\sqrt{n})\log^2 n) Σ ( X A ∩ Z j ) ≤ Γ A , Z ( sum ) ≤ O (( t / n ) log 2 n ) , for j ∈ [ n ] j\in[\sqrt{n}] j ∈ [ n ] .
Proof. There are n \sqrt{n} n color classes and 2 n − 1 2\sqrt{n}-1 2 n − 1 sets Z j ( ℓ ) Z_j^{(\ell)} Z j ( ℓ ) . The first two properties have total failure probability at most ( 3 n − 1 ) n − 4 c (3\sqrt{n}-1)n^{-4c} ( 3 n − 1 ) n − 4 c . The proof of Lemma 15 bounds the failure probability of the third property, over all color classes, by n ( log n ) n − 4 c \sqrt{n}(\log n)n^{-4c} n ( log n ) n − 4 c . Thus the total is at most ( 3 + log n ) n n − 4 c ≤ n 2 n − 4 c ≤ n − 3 c (3+\log n)\sqrt{n}n^{-4c}\leq n^2n^{-4c}\leq n^{-3c} ( 3 + log n ) n n − 4 c ≤ n 2 n − 4 c ≤ n − 3 c , since ( 3 + log n ) n ≤ n 2 (3+\log n)\sqrt{n}\leq n^2 ( 3 + log n ) n ≤ n 2 for n ≥ 4 n\geq4 n ≥ 4 . ◀
Algorithm steps.
We now have the ingredients to describe our O ~ ( n + n t ) \widetilde{O}(n+\sqrt{n}t) O ( n + n t ) -time algorithm for TCSS. We use Lemma 16 to guarantee certain probabilistic structures. Then, with repeated usage of Lemma 6, we compute the cardinalities and sums of relevant subsets, while pruning out subsets which are ‘far from expectation’ and hence are ruled out as irrelevant by Lemma 8 .
Theorem 17. For any fixed integer constant c ≥ 10 c\geq10 c ≥ 10 , there exists a randomized algorithm solving TCSS on n n n nonnegative integers with target t t t in time O ~ ( n + n t ) \widetilde{O}(n+\sqrt{n}t) O ( n + n t ) , with error probability at most n − 2 c n^{-2c} n − 2 c .
Proof. We first describe the algorithm, prove its correctness, bound its failure probability, and analyze its running time.
Description of the algorithm. We form the random color classes and the sets A , B A,B A , B as described in Sections 4.1.1 and 4.1.2. If some color class Z j Z_j Z j has size more than Γ Z j ( s i z e ) \Gamma_{Z_j}^{(\mathrm{size})} Γ Z j ( size ) , we return NO. In every call to Lemma 6 below, we set δ = n − 4 c \delta=n^{-4c} δ = n − 4 c and use fresh randomness.
We apply Lemma 6 to ( a i ) i ∈ B (a_i)_{i\in B} ( a i ) i ∈ B with cardinality bound n \sqrt{n} n and target t t t , to obtain
J = { ( ∣ S ∣ , Σ ( S ) ) ∣ S ⊆ B , Σ ( S ) ≤ t , ∣ S ∣ ≤ n } . \mathcal{J}=\{(|S|,\Sigma(S))\mid S\subseteq B,\Sigma(S)\leq t,|S|\leq\sqrt{n}\}. J = {( ∣ S ∣ , Σ ( S )) ∣ S ⊆ B , Σ ( S ) ≤ t , ∣ S ∣ ≤ n } .
Every subset of B B B of sum at most t t t has fewer than n \sqrt{n} n items.
For each j ∈ [ n ] j\in[\sqrt{n}] j ∈ [ n ] , we apply Lemma 6 to ( a i ) i ∈ Z j ∩ A (a_i)_{i\in Z_j\cap A} ( a i ) i ∈ Z j ∩ A with cardinality bound ∣ Z j ∩ A ∣ |Z_j\cap A| ∣ Z j ∩ A ∣ and target W = Γ A , Z ( s u m ) W=\Gamma_{A,Z}^{(\mathrm{sum})} W = Γ A , Z ( sum ) . This gives us
I j ′ = { ( ∣ S ∣ , Σ ( S ) ) : S ⊆ Z j ∩ A , Σ ( S ) ≤ Γ A , Z ( s u m ) } . \mathcal{I}'_j=\{(|S|,\Sigma(S)):S\subseteq Z_j\cap A,\Sigma(S)\leq\Gamma_{A,Z}^{(\mathrm{sum})}\}. I j ′ = {( ∣ S ∣ , Σ ( S )) : S ⊆ Z j ∩ A , Σ ( S ) ≤ Γ A , Z ( sum ) } .
We define
I j ( 0 ) = { ( d , w ) : ( s , w ) ∈ I j ′ , d = s − ⌊ k n ⌋ , ∣ d ∣ ≤ Γ A , Z ( s i z e ) } . \mathcal{I}^{(0)}_j=\left\{(d,w):(s,w)\in\mathcal{I}'_j,d=s-\left\lfloor\frac{k}{\sqrt{n}}\right\rfloor,|d|\leq\Gamma_{A,Z}^{(\mathrm{size})}\right\}. I j ( 0 ) = { ( d , w ) : ( s , w ) ∈ I j ′ , d = s − ⌊ n k ⌋ , ∣ d ∣ ≤ Γ A , Z ( size ) } .
For 1 ≤ ℓ ≤ 1 2 log n 1\leq\ell\leq\frac{1}{2}\log n 1 ≤ ℓ ≤ 2 1 log n , and 1 ≤ j ≤ n / 2 ℓ 1\leq j\leq\sqrt{n}/2^\ell 1 ≤ j ≤ n / 2 ℓ , we define
I j ( ℓ ) = { ( d , w ) : ( d ′ , w ) ∈ I 2 j − 1 ( ℓ − 1 ) ⊕ I 2 j ( ℓ − 1 ) , d = d ′ + 2 ⌊ 2 ℓ − 1 k n ⌋ − ⌊ 2 ℓ k n ⌋ , ∣ d ∣ ≤ Γ A , Z ( s i z e ) } . \mathcal{I}^{(\ell)}_j=\left\{(d,w):(d',w)\in\mathcal{I}^{(\ell-1)}_{2j-1}\oplus\mathcal{I}^{(\ell-1)}_{2j},d=d'+2\left\lfloor\frac{2^{\ell-1}k}{\sqrt{n}}\right\rfloor-\left\lfloor\frac{2^\ell k}{\sqrt{n}}\right\rfloor,|d|\leq\Gamma_{A,Z}^{(\mathrm{size})}\right\}. I j ( ℓ ) = { ( d , w ) : ( d ′ , w ) ∈ I 2 j − 1 ( ℓ − 1 ) ⊕ I 2 j ( ℓ − 1 ) , d = d ′ + 2 ⌊ n 2 ℓ − 1 k ⌋ − ⌊ n 2 ℓ k ⌋ , ∣ d ∣ ≤ Γ A , Z ( size ) } .
For every ( d , w ) ∈ I j ( ℓ ) (d,w)\in\mathcal{I}^{(\ell)}_j ( d , w ) ∈ I j ( ℓ ) , there is a subset of Z j ( ℓ ) ∩ A Z^{(\ell)}_j\cap A Z j ( ℓ ) ∩ A summing up to w w w and having size d + ⌊ 2 ℓ k / n ⌋ d+\left\lfloor2^\ell k/\sqrt{n}\right\rfloor d + ⌊ 2 ℓ k / n ⌋ . We return YES if some ( s , w ) ∈ J (s,w)\in\mathcal{J} ( s , w ) ∈ J satisfies ( − s , t − w ) ∈ I 1 ( 1 2 log n ) (-s,t-w)\in\mathcal{I}^{(\frac{1}{2}\log n)}_1 ( − s , t − w ) ∈ I 1 ( 2 1 l o g n ) , NO otherwise.
Correctness. We first show that the pairs in J \mathcal{J} J and I j ( ℓ ) \mathcal{I}^{(\ell)}_j I j ( ℓ ) represent cardinalities and sums of existing subsets. Lemma 6 implies that for every ( s , w ) ∈ J (s,w)\in\mathcal{J} ( s , w ) ∈ J there is S ⊆ B S\subseteq B S ⊆ B with ∣ S ∣ = s |S|=s ∣ S ∣ = s and Σ ( S ) = w \Sigma(S)=w Σ ( S ) = w . Likewise, for every ( s , w ) ∈ I j ′ (s,w)\in\mathcal{I}'_j ( s , w ) ∈ I j ′ there is S ⊆ Z j ∩ A S\subseteq Z_j\cap A S ⊆ Z j ∩ A with ∣ S ∣ = s |S|=s ∣ S ∣ = s and Σ ( S ) = w \Sigma(S)=w Σ ( S ) = w .
Claim. For every ℓ ∈ [ 1 2 log n ] 0 \ell\in[\frac{1}{2}\log n]_0 ℓ ∈ [ 2 1 log n ] 0 , j ∈ [ n / 2 ℓ ] j\in[\sqrt{n}/2^\ell] j ∈ [ n / 2 ℓ ] , and ( d , w ) ∈ I j ( ℓ ) (d,w)\in\mathcal{I}^{(\ell)}_j ( d , w ) ∈ I j ( ℓ ) , there is a subset S ⊆ Z j ( ℓ ) ∩ A S\subseteq Z^{(\ell)}_j\cap A S ⊆ Z j ( ℓ ) ∩ A such that Σ ( S ) = w \Sigma(S)=w Σ ( S ) = w and ∣ S ∣ = d + ⌊ 2 ℓ k / n ⌋ |S|=d+\left\lfloor2^\ell k/\sqrt{n}\right\rfloor ∣ S ∣ = d + ⌊ 2 ℓ k / n ⌋ .
Proof. We proceed by induction on ℓ \ell ℓ . If ℓ = 0 \ell=0 ℓ = 0 , the definition of I j ( 0 ) \mathcal{I}^{(0)}_j I j ( 0 ) gives ( s , w ) ∈ I j ′ (s,w)\in\mathcal{I}'_j ( s , w ) ∈ I j ′ with d = s − ⌊ k / n ⌋ d=s-\left\lfloor k/\sqrt{n}\right\rfloor d = s − ⌊ k / n ⌋ . Therefore, there is S ⊆ Z j ∩ A = Z j ( 0 ) ∩ A S\subseteq Z_j\cap A=Z^{(0)}_j\cap A S ⊆ Z j ∩ A = Z j ( 0 ) ∩ A with Σ ( S ) = w \Sigma(S)=w Σ ( S ) = w and ∣ S ∣ = s = d + ⌊ k / n ⌋ |S|=s=d+\left\lfloor k/\sqrt{n}\right\rfloor ∣ S ∣ = s = d + ⌊ k / n ⌋ . Now, suppose ℓ ≥ 1 \ell\geq1 ℓ ≥ 1 and ( d , w ) ∈ I j ( ℓ ) (d,w)\in\mathcal{I}^{(\ell)}_j ( d , w ) ∈ I j ( ℓ ) . By construction, there are ( d 1 , w 1 ) ∈ I 2 j − 1 ( ℓ − 1 ) (d_1,w_1)\in\mathcal{I}^{(\ell-1)}_{2j-1} ( d 1 , w 1 ) ∈ I 2 j − 1 ( ℓ − 1 ) and ( d 2 , w 2 ) ∈ I 2 j ( ℓ − 1 ) (d_2,w_2)\in\mathcal{I}^{(\ell-1)}_{2j} ( d 2 , w 2 ) ∈ I 2 j ( ℓ − 1 ) with w = w 1 + w 2 w=w_1+w_2 w = w 1 + w 2 , and d = d 1 + d 2 + 2 ⌊ 2 ℓ − 1 k / n ⌋ − ⌊ 2 ℓ k / n ⌋ d=d_1+d_2+2\left\lfloor2^{\ell-1}k/\sqrt{n}\right\rfloor-\left\lfloor2^\ell k/\sqrt{n}\right\rfloor d = d 1 + d 2 + 2 ⌊ 2 ℓ − 1 k / n ⌋ − ⌊ 2 ℓ k / n ⌋ .
By the induction hypothesis, there are subsets S 1 ⊆ Z 2 j − 1 ( ℓ − 1 ) ∩ A S_1\subseteq Z^{(\ell-1)}_{2j-1}\cap A S 1 ⊆ Z 2 j − 1 ( ℓ − 1 ) ∩ A and S 2 ⊆ Z 2 j ( ℓ − 1 ) ∩ A S_2\subseteq Z^{(\ell-1)}_{2j}\cap A S 2 ⊆ Z 2 j ( ℓ − 1 ) ∩ A such that, for i ∈ { 1 , 2 } i\in\{1,2\} i ∈ { 1 , 2 } , Σ ( S i ) = w i \Sigma(S_i)=w_i Σ ( S i ) = w i and ∣ S i ∣ = d i + ⌊ 2 ℓ − 1 k / n ⌋ |S_i|=d_i+\left\lfloor2^{\ell-1}k/\sqrt{n}\right\rfloor ∣ S i ∣ = d i + ⌊ 2 ℓ − 1 k / n ⌋ . The sets Z 2 j − 1 ( ℓ − 1 ) Z^{(\ell-1)}_{2j-1} Z 2 j − 1 ( ℓ − 1 ) and Z 2 j ( ℓ − 1 ) Z^{(\ell-1)}_{2j} Z 2 j ( ℓ − 1 ) are disjoint and their union is Z j ( ℓ ) Z^{(\ell)}_j Z j ( ℓ ) . Hence S 1 ∪ S 2 ⊆ Z j ( ℓ ) ∩ A S_1\cup S_2\subseteq Z^{(\ell)}_j\cap A S 1 ∪ S 2 ⊆ Z j ( ℓ ) ∩ A has sum w w w and cardinality d + ⌊ 2 ℓ k / n ⌋ d+\left\lfloor2^\ell k/\sqrt{n}\right\rfloor d + ⌊ 2 ℓ k / n ⌋ , as required. ◁
If the algorithm returns YES, there are ( s , w ) ∈ J (s,w)\in\mathcal{J} ( s , w ) ∈ J and ( − s , t − w ) ∈ I 1 ( 1 2 log n ) (-s,t-w)\in\mathcal{I}^{(\frac{1}{2}\log n)}_1 ( − s , t − w ) ∈ I 1 ( 2 1 l o g n ) . The first pair gives a subset S B ⊆ B S_B\subseteq B S B ⊆ B with ∣ S B ∣ = s |S_B|=s ∣ S B ∣ = s and Σ ( S B ) = w \Sigma(S_B)=w Σ ( S B ) = w . Since Z 1 ( 1 2 log n ) = [ n ] Z^{(\frac{1}{2}\log n)}_1=[n] Z 1 ( 2 1 l o g n ) = [ n ] , the claim gives a subset S A ⊆ A S_A\subseteq A S A ⊆ A with ∣ S A ∣ = − s + k |S_A|=-s+k ∣ S A ∣ = − s + k and Σ ( S A ) = t − w \Sigma(S_A)=t-w Σ ( S A ) = t − w . The sets S A S_A S A and S B S_B S B are disjoint because A A A and B B B are disjoint. Thus S A ∪ S B S_A\cup S_B S A ∪ S B has cardinality k k k and sum t t t , so the algorithm cannot return YES on a NO-instance.
Now fix a subset X ⊆ [ n ] X\subseteq[n] X ⊆ [ n ] with ∣ X ∣ = k |X|=k ∣ X ∣ = k and Σ ( X ) = t \Sigma(X)=t Σ ( X ) = t , independently of the random choices. Let E E E be the event that all three properties of Lemma 16 hold. Let E 1 E_1 E 1 be the event that J = { ( ∣ S ∣ , Σ ( S ) ) : S ⊆ B , Σ ( S ) ≤ t } \mathcal{J}=\{(|S|,\Sigma(S)):S\subseteq B,\ \Sigma(S)\leq t\} J = {( ∣ S ∣ , Σ ( S )) : S ⊆ B , Σ ( S ) ≤ t } , and let E 2 E_2 E 2 be the event that ( ∣ X A ∩ Z j ∣ , Σ ( X A ∩ Z j ) ) ∈ I j ′ (|X_A\cap Z_j|,\Sigma(X_A\cap Z_j))\in\mathcal{I}'_j ( ∣ X A ∩ Z j ∣ , Σ ( X A ∩ Z j )) ∈ I j ′ for every j ∈ [ n ] j\in[\sqrt{n}] j ∈ [ n ] . On E E E , the algorithm does not return NO at its initial check, and Σ ( X A ∩ Z j ) ≤ Γ A , Z ( s u m ) \Sigma(X_A\cap Z_j)\leq\Gamma_{A,Z}^{(\mathrm{sum})} Σ ( X A ∩ Z j ) ≤ Γ A , Z ( sum ) for every j ∈ [ n ] j\in[\sqrt{n}] j ∈ [ n ] .
We next bound the shifted cardinality of X A ∩ Z j ( ℓ ) X_A \cap Z_j^{(\ell)} X A ∩ Z j ( ℓ ) used in the definition of I j ( ℓ ) \mathcal{I}_j^{(\ell)} I j ( ℓ ) . For every ℓ ∈ [ 1 2 log n ] 0 \ell\in[\frac{1}{2}\log n]_0 ℓ ∈ [ 2 1 log n ] 0 and j ∈ [ n / 2 ℓ ] j \in[\sqrt{n}/2^\ell] j ∈ [ n / 2 ℓ ] , the event E E E implies
∣ ∣ X A ∩ Z j ( ℓ ) ∣ − ⌊ 2 ℓ k n ⌋ ∣ ≤ ∣ ∣ X A ∩ Z j ( ℓ ) ∣ − 2 ℓ k n ∣ + ( 2 ℓ k n − ⌊ 2 ℓ k n ⌋ ) ≤ Γ A , Z ( s i z e ) + 1. (8) \left| |X_A \cap Z_j^{(\ell)}| - \left\lfloor\frac{2^\ell k}{\sqrt{n}}\right\rfloor\right| \le\left| |X_A \cap Z_j^{(\ell)}| - \frac{2^\ell k}{\sqrt{n}} \right| + \left(\frac{2^\ell k}{\sqrt{n}} - \left\lfloor\frac{2^\ell k}{\sqrt{n}}\right\rfloor\right) \le\Gamma_{A,Z}^{(\mathrm{size})} + 1. \tag*{(8)} ∣ X A ∩ Z j ( ℓ ) ∣ − ⌊ n 2 ℓ k ⌋ ≤ ∣ X A ∩ Z j ( ℓ ) ∣ − n 2 ℓ k + ( n 2 ℓ k − ⌊ n 2 ℓ k ⌋ ) ≤ Γ A , Z ( size ) + 1. (8)
Since c c c , n \sqrt{n} n , and log n \log n log n are integers, Γ A , Z ( s i z e ) \Gamma_{A,Z}^{(\mathrm{size})} Γ A , Z ( size ) is an integer. The expression on the left is also an integer, and hence it is at most Γ A , Z ( s i z e ) \Gamma_{A,Z}^{(\mathrm{size})} Γ A , Z ( size ) .
Claim. If E E E and E 2 E_2 E 2 both occur, then for every ℓ ∈ [ 1 2 log n ] 0 \ell\in[\frac{1}{2}\log n]_0 ℓ ∈ [ 2 1 log n ] 0 and j ∈ [ n / 2 ℓ ] j \in[\sqrt{n}/2^\ell] j ∈ [ n / 2 ℓ ] , we have
( ∣ X A ∩ Z j ( ℓ ) ∣ − ⌊ 2 ℓ k n ⌋ , Σ ( X A ∩ Z j ( ℓ ) ) ) ∈ I j ( ℓ ) . \left(\left|X_A \cap Z_j^{(\ell)}\right|-\left\lfloor\frac{2^\ell k}{\sqrt{n}}\right\rfloor,\ \Sigma(X_A \cap Z_j^{(\ell)})\right) \in\mathcal{I}_j^{(\ell)}. ( X A ∩ Z j ( ℓ ) − ⌊ n 2 ℓ k ⌋ , Σ ( X A ∩ Z j ( ℓ ) ) ) ∈ I j ( ℓ ) .
Proof. We proceed by induction on ℓ \ell ℓ . If ℓ = 0 \ell=0 ℓ = 0 , the pair ( ∣ X A ∩ Z j ∣ , Σ ( X A ∩ Z j ) ) (|X_A \cap Z_j|,\Sigma(X_A \cap Z_j)) ( ∣ X A ∩ Z j ∣ , Σ ( X A ∩ Z j )) belongs to I j ′ \mathcal{I}_j^{\prime} I j ′ by E 2 E_2 E 2 . Assuming E E E , Equation (8) gives ∣ ∣ X A ∩ Z j ∣ − ⌊ k / n ⌋ ∣ ≤ Γ A , Z ( s i z e ) \left||X_A \cap Z_j|-\lfloor k/\sqrt{n}\rfloor\right| \le\Gamma_{A,Z}^{(\mathrm{size})} ∣ ∣ X A ∩ Z j ∣ − ⌊ k / n ⌋ ∣ ≤ Γ A , Z ( size ) . Thus the I j ( 0 ) \mathcal{I}_j^{(0)} I j ( 0 ) contains ( ∣ X A ∩ Z j ∣ − ⌊ k / n ⌋ , Σ ( X A ∩ Z j ) ) (|X_A \cap Z_j|-\lfloor k/\sqrt{n}\rfloor,\Sigma(X_A \cap Z_j)) ( ∣ X A ∩ Z j ∣ − ⌊ k / n ⌋ , Σ ( X A ∩ Z j )) . Now, suppose ℓ ≥ 1 \ell\ge1 ℓ ≥ 1 . By induction hypothesis,
( ∣ X A ∩ Z 2 j − 1 ( ℓ − 1 ) ∣ − ⌊ 2 ℓ − 1 k n ⌋ , Σ ( X A ∩ Z 2 j − 1 ( ℓ − 1 ) ) ) ∈ I 2 j − 1 ( ℓ − 1 ) , \left(\left|X_A \cap Z_{2j-1}^{(\ell-1)}\right|-\left\lfloor\frac{2^{\ell-1}k}{\sqrt{n}}\right\rfloor,\ \Sigma(X_A \cap Z_{2j-1}^{(\ell-1)})\right) \in\mathcal{I}_{2j-1}^{(\ell-1)}, ( X A ∩ Z 2 j − 1 ( ℓ − 1 ) − ⌊ n 2 ℓ − 1 k ⌋ , Σ ( X A ∩ Z 2 j − 1 ( ℓ − 1 ) ) ) ∈ I 2 j − 1 ( ℓ − 1 ) ,
( ∣ X A ∩ Z 2 j ( ℓ − 1 ) ∣ − ⌊ 2 ℓ − 1 k n ⌋ , Σ ( X A ∩ Z 2 j ( ℓ − 1 ) ) ) ∈ I 2 j ( ℓ − 1 ) . \left(\left|X_A \cap Z_{2j}^{(\ell-1)}\right|-\left\lfloor\frac{2^{\ell-1}k}{\sqrt{n}}\right\rfloor,\ \Sigma(X_A \cap Z_{2j}^{(\ell-1)})\right) \in\mathcal{I}_{2j}^{(\ell-1)}. ( X A ∩ Z 2 j ( ℓ − 1 ) − ⌊ n 2 ℓ − 1 k ⌋ , Σ ( X A ∩ Z 2 j ( ℓ − 1 ) ) ) ∈ I 2 j ( ℓ − 1 ) .
The componentwise sum of these two pairs is ( d ′ , w ) (d',w) ( d ′ , w ) , where
d ′ = ∣ X A ∩ Z j ( ℓ ) ∣ − 2 ⌊ 2 ℓ − 1 k n ⌋ , w = Σ ( X A ∩ Z j ( ℓ ) ) . d' = |X_A \cap Z_j^{(\ell)}| - 2\left\lfloor\frac{2^{\ell-1}k}{\sqrt{n}}\right\rfloor,\qquad w = \Sigma(X_A \cap Z_j^{(\ell)}). d ′ = ∣ X A ∩ Z j ( ℓ ) ∣ − 2 ⌊ n 2 ℓ − 1 k ⌋ , w = Σ ( X A ∩ Z j ( ℓ ) ) .
Substituting this value of d ′ d' d ′ into the defining relation for I j ( ℓ ) \mathcal{I}_j^{(\ell)} I j ( ℓ ) gives
( ∣ X A ∩ Z j ( ℓ ) ∣ − ⌊ 2 ℓ k n ⌋ , Σ ( X A ∩ Z j ( ℓ ) ) ) . (9) \left(\left|X_A \cap Z_j^{(\ell)}\right|-\left\lfloor\frac{2^\ell k}{\sqrt{n}}\right\rfloor,\ \Sigma(X_A \cap Z_j^{(\ell)})\right). \tag*{(9)} ( X A ∩ Z j ( ℓ ) − ⌊ n 2 ℓ k ⌋ , Σ ( X A ∩ Z j ( ℓ ) ) ) . (9)
Again, Equation (8) shows that ∣ ∣ X A ∩ Z j ( ℓ ) ∣ − ⌊ 2 ℓ k / n ⌋ ∣ ≤ Γ A , Z ( s i z e ) \left||X_A \cap Z_j^{(\ell)}|-\lfloor2^\ell k/\sqrt{n}\rfloor\right| \le\Gamma_{A,Z}^{(\mathrm{size})} ∣ X A ∩ Z j ( ℓ ) ∣ − ⌊ 2 ℓ k / n ⌋ ≤ Γ A , Z ( size ) , implying that the pair in Equation (9) belongs to I j ( ℓ ) \mathcal{I}_j^{(\ell)} I j ( ℓ ) .
At ℓ = 1 2 log n \ell=\frac{1}{2}\log n ℓ = 2 1 log n and j = 1 j=1 j = 1 , we have Z 1 ( ℓ ) = [ n ] Z_1^{(\ell)}=[n] Z 1 ( ℓ ) = [ n ] and ⌊ 2 ℓ k / n ⌋ = k \lfloor2^\ell k/\sqrt{n}\rfloor=k ⌊ 2 ℓ k / n ⌋ = k . Thus, on E ∩ E 2 E \cap E_2 E ∩ E 2 , ( ∣ X A ∣ − k , Σ ( X A ) ) ∈ I 1 ( 1 2 log n ) (|X_A|-k,\Sigma(X_A))\in\mathcal{I}_1^{(\frac{1}{2}\log n)} ( ∣ X A ∣ − k , Σ ( X A )) ∈ I 1 ( 2 1 l o g n ) . On E 1 E_1 E 1 , the pair ( ∣ X B ∣ , Σ ( X B ) ) (|X_B|,\Sigma(X_B)) ( ∣ X B ∣ , Σ ( X B )) belongs to J \mathcal{J} J . Because ∣ X A ∣ − k = − ∣ X B ∣ |X_A|-k=-|X_B| ∣ X A ∣ − k = − ∣ X B ∣ and Σ ( X A ) = t − Σ ( X B ) \Sigma(X_A)=t-\Sigma(X_B) Σ ( X A ) = t − Σ ( X B ) , these two pairs satisfy the acceptance condition, and the algorithm returns YES.
Failure probability. By Lemma 16, Pr [ ¬ E ] ≤ n − 3 c \Pr[\neg E]\le n^{-3c} Pr [ ¬ E ] ≤ n − 3 c . By Lemma 6, each call fails with probability at most δ = n − 4 c \delta=n^{-4c} δ = n − 4 c . Thus Pr [ ¬ E 1 ] ≤ n − 4 c \Pr[\neg E_1]\le n^{-4c} Pr [ ¬ E 1 ] ≤ n − 4 c . For every partition satisfying E E E , each of the n \sqrt{n} n calls for I j ′ \mathcal{I}_j' I j ′ contains its eligible pair with failure probability at most n − 4 c n^{-4c} n − 4 c . A union bound gives Pr [ ¬ E 2 ∣ E ] ≤ n n − 4 c ≤ n − 3 c \Pr[\neg E_2\mid E]\le\sqrt{n}n^{-4c}\le n^{-3c} Pr [ ¬ E 2 ∣ E ] ≤ n n − 4 c ≤ n − 3 c . Hence the total failure probability is at most n − 3 c + n − 4 c + n − 3 c ≤ 3 n − 3 c ≤ n − 2 c n^{-3c}+n^{-4c}+n^{-3c}\le3n^{-3c}\le n^{-2c} n − 3 c + n − 4 c + n − 3 c ≤ 3 n − 3 c ≤ n − 2 c .
Running time. The initial partition into color classes and preprocessing take O ( n ) O(n) O ( n ) time. Since log ( 1 / δ ) = 4 c log n \log(1/\delta)=4c\log n log ( 1/ δ ) = 4 c log n , the dependence on δ \delta δ in Lemma 6 contributes only polylogarithmic factors. Our initial check based on sizes of color classes ensures that every color class has at most Γ Z ( s i z e ) \Gamma_Z^{(\mathrm{size})} Γ Z ( size ) items whenever we perform the remaining computations. The call to Lemma 6 for computing I \mathcal{I} I takes O ~ ( ∣ B ∣ + n t ) \widetilde{O}(|B|+\sqrt{n}t) O ( ∣ B ∣ + n t ) time. By Equation (7), the call to Lemma 6 for computing I j ′ \mathcal{I}'_j I j ′ takes O ~ ( ∣ Z j ∣ + ∣ Z j ∣ t / n ) \widetilde{O}(|Z_j|+|Z_j|t/\sqrt{n}) O ( ∣ Z j ∣ + ∣ Z j ∣ t / n ) time. In total, computing I j ( 0 ) \mathcal{I}^{(0)}_j I j ( 0 ) for all j ∈ [ n ] j\in[\sqrt{n}] j ∈ [ n ] takes
∑ j = 1 n O ~ ( ∣ Z j ∣ + ∣ Z j ∣ t / n ) = O ~ ( n + n t ) \sum_{j=1}^{\sqrt{n}} \widetilde{O}(|Z_j|+|Z_j|t/\sqrt{n})=\widetilde{O}(n+\sqrt{n}t) j = 1 ∑ n O ( ∣ Z j ∣ + ∣ Z j ∣ t / n ) = O ( n + n t )
time. Next we compute I j ( ℓ ) \mathcal{I}^{(\ell)}_j I j ( ℓ ) for ℓ ≥ 1 \ell\ge1 ℓ ≥ 1 using Lemma 28. We bound w w w for all ( d , w ) ∈ I j ( ℓ ) (d,w)\in\mathcal{I}^{(\ell)}_j ( d , w ) ∈ I j ( ℓ ) .
Claim 18. For ℓ ∈ [ 1 2 log n ] 0 \ell\in[\frac{1}{2}\log n]_0 ℓ ∈ [ 2 1 log n ] 0 , j ∈ [ n / 2 ℓ ] j\in[\sqrt{n}/2^\ell] j ∈ [ n / 2 ℓ ] , and ( d , w ) ∈ I j ( ℓ ) (d,w)\in\mathcal{I}^{(\ell)}_j ( d , w ) ∈ I j ( ℓ ) , we have 0 ≤ w ≤ 2 ℓ Γ A , Z ( s u m ) 0\le w\le2^\ell\Gamma^{(\mathrm{sum})}_{A,Z} 0 ≤ w ≤ 2 ℓ Γ A , Z ( sum ) .
Proof. We proceed by induction on ℓ \ell ℓ . If ℓ = 0 \ell=0 ℓ = 0 , the definition of I j ( 0 ) \mathcal{I}^{(0)}_j I j ( 0 ) gives ( s , w ) ∈ I j ′ (s,w)\in\mathcal{I}'_j ( s , w ) ∈ I j ′ for some s s s . Every pair in I j ( 0 ) \mathcal{I}^{(0)}_j I j ( 0 ) is realized by a subset of Z j ∩ A Z_j\cap A Z j ∩ A with nonnegative sum at most Γ A , Z ( s u m ) \Gamma^{(\mathrm{sum})}_{A,Z} Γ A , Z ( sum ) , so 0 ≤ w ≤ Γ A , Z ( s u m ) 0\le w\le\Gamma^{(\mathrm{sum})}_{A,Z} 0 ≤ w ≤ Γ A , Z ( sum ) . Now, suppose ℓ ≥ 1 \ell\ge1 ℓ ≥ 1 . By the definition of I j ( ℓ ) \mathcal{I}^{(\ell)}_j I j ( ℓ ) , some ( d ′ , w ) (d',w) ( d ′ , w ) belongs to I 2 j − 1 ( ℓ − 1 ) ⊕ I 2 j ( ℓ − 1 ) \mathcal{I}^{(\ell-1)}_{2j-1}\oplus\mathcal{I}^{(\ell-1)}_{2j} I 2 j − 1 ( ℓ − 1 ) ⊕ I 2 j ( ℓ − 1 ) . Thus there are ( d 1 , w 1 ) ∈ I 2 j − 1 ( ℓ − 1 ) (d_1,w_1)\in\mathcal{I}^{(\ell-1)}_{2j-1} ( d 1 , w 1 ) ∈ I 2 j − 1 ( ℓ − 1 ) and ( d 2 , w 2 ) ∈ I 2 j ( ℓ − 1 ) (d_2,w_2)\in\mathcal{I}^{(\ell-1)}_{2j} ( d 2 , w 2 ) ∈ I 2 j ( ℓ − 1 ) with w = w 1 + w 2 w=w_1+w_2 w = w 1 + w 2 . By the induction hypothesis, 0 ≤ w 1 , w 2 ≤ 2 ℓ − 1 Γ A , Z ( s u m ) 0\le w_1,w_2\le2^{\ell-1}\Gamma^{(\mathrm{sum})}_{A,Z} 0 ≤ w 1 , w 2 ≤ 2 ℓ − 1 Γ A , Z ( sum ) . Hence 0 ≤ w ≤ 2 ℓ Γ A , Z ( s u m ) 0\le w\le2^\ell\Gamma^{(\mathrm{sum})}_{A,Z} 0 ≤ w ≤ 2 ℓ Γ A , Z ( sum ) .
Fix ℓ ∈ { 1 , … , 1 2 log n } \ell\in\{1,\ldots,\frac{1}{2}\log n\} ℓ ∈ { 1 , … , 2 1 log n } and j ∈ [ n / 2 ℓ ] j\in[\sqrt{n}/2^\ell] j ∈ [ n / 2 ℓ ] . Every ( d , w ) (d,w) ( d , w ) in either I 2 j − 1 ( ℓ − 1 ) \mathcal{I}^{(\ell-1)}_{2j-1} I 2 j − 1 ( ℓ − 1 ) or I 2 j ( ℓ − 1 ) \mathcal{I}^{(\ell-1)}_{2j} I 2 j ( ℓ − 1 ) satisfies ∣ d ∣ ≤ Γ A , Z ( s i z e ) |d|\le\Gamma^{(\mathrm{size})}_{A,Z} ∣ d ∣ ≤ Γ A , Z ( size ) by definition and 0 ≤ w ≤ 2 ℓ − 1 Γ A , Z ( s u m ) 0\le w\le2^{\ell-1}\Gamma^{(\mathrm{sum})}_{A,Z} 0 ≤ w ≤ 2 ℓ − 1 Γ A , Z ( sum ) by the claim. Therefore, Lemma 28 computes their sumset in time O ~ ( Γ A , Z ( s i z e ) ( 2 ℓ − 1 Γ A , Z ( s u m ) + 1 ) ) = O ~ ( 2 ℓ t + n ) \widetilde{O}(\Gamma^{(\mathrm{size})}_{A,Z}(2^{\ell-1}\Gamma^{(\mathrm{sum})}_{A,Z}+1))=\widetilde{O}(2^\ell t+\sqrt{n}) O ( Γ A , Z ( size ) ( 2 ℓ − 1 Γ A , Z ( sum ) + 1 )) = O ( 2 ℓ t + n ) by Equations (3) and (7). Every pair ( d ′ , w ) (d',w) ( d ′ , w ) in this sumset has ∣ d ′ ∣ ≤ 2 Γ A , Z ( s i z e ) |d'|\le2\Gamma^{(\mathrm{size})}_{A,Z} ∣ d ′ ∣ ≤ 2 Γ A , Z ( size ) and 0 ≤ w ≤ 2 ℓ Γ A , Z ( s u m ) 0\le w\le2^\ell\Gamma^{(\mathrm{sum})}_{A,Z} 0 ≤ w ≤ 2 ℓ Γ A , Z ( sum ) , so the sumset contains O ( Γ A , Z ( s i z e ) ( 2 ℓ Γ A , Z ( s u m ) + 1 ) ) O(\Gamma^{(\mathrm{size})}_{A,Z}(2^\ell\Gamma^{(\mathrm{sum})}_{A,Z}+1)) O ( Γ A , Z ( size ) ( 2 ℓ Γ A , Z ( sum ) + 1 )) pairs. Scanning them to form I j ( ℓ ) \mathcal{I}^{(\ell)}_j I j ( ℓ ) also takes O ~ ( 2 ℓ t + n ) \widetilde{O}(2^\ell t+\sqrt{n}) O ( 2 ℓ t + n ) time. There are n / 2 ℓ \sqrt{n}/2^\ell n / 2 ℓ such computations for this value of ℓ \ell ℓ , so their total time over all ℓ \ell ℓ is
∑ ℓ = 1 1 2 log n n 2 ℓ O ~ ( 2 ℓ t + n ) = O ~ ( n + n t ) . \sum_{\ell=1}^{\frac{1}{2}\log n}\frac{\sqrt{n}}{2^\ell}\widetilde{O}(2^\ell t+\sqrt{n})=\widetilde{O}(n+\sqrt{n}t). ℓ = 1 ∑ 2 1 l o g n 2 ℓ n O ( 2 ℓ t + n ) = O ( n + n t ) .
Finally, we store I 1 ( 1 2 log n ) \mathcal{I}^{(\frac{1}{2}\log n)}_1 I 1 ( 2 1 l o g n ) in a Boolean array indexed by ( d , w ) (d,w) ( d , w ) . Its definition gives ∣ d ∣ ≤ Γ A , Z ( s i z e ) |d|\le\Gamma^{(\mathrm{size})}_{A,Z} ∣ d ∣ ≤ Γ A , Z ( size ) , and the claim gives 0 ≤ w ≤ n Γ A , Z ( s u m ) 0\le w\le\sqrt{n}\Gamma^{(\mathrm{sum})}_{A,Z} 0 ≤ w ≤ n Γ A , Z ( sum ) ; thus initializing and populating the array takes O ~ ( Γ A , Z ( s i z e ) ( n Γ A , Z ( s u m ) + 1 ) ) = O ~ ( n t ) \widetilde{O}(\Gamma^{(\mathrm{size})}_{A,Z}(\sqrt{n}\Gamma^{(\mathrm{sum})}_{A,Z}+1))=\widetilde{O}(\sqrt{n}t) O ( Γ A , Z ( size ) ( n Γ A , Z ( sum ) + 1 )) = O ( n t ) time. The set J \mathcal{J} J has at most ( n + 1 ) ( t + 1 ) (\sqrt{n}+1)(t+1) ( n + 1 ) ( t + 1 ) pairs, so scanning it and checking the acceptance condition takes O ~ ( n t ) \widetilde{O}(\sqrt{n}t) O ( n t ) time. This proves the theorem. Algorithm 1 summarizes the construction.
Algorithm for Largest Cardinality Subset Sum
TCSS asks to decide if there exists a subset with sum t t t and cardinality k k k . Our next step to solve LCSS is to decide if there exists a subset with sum t t t and cardinality in an interval [ ℓ , h ] [\ell,h] [ ℓ , h ] . We show that this can be done by simply adding h − ℓ h-\ell h − ℓ zeros to the instance and asking whether a subset exists with sum t t t and cardinality h h h .
Lemma 19 (Target cardinality to cardinality in an interval). Let a 1 , … , a n a_1,\ldots,a_n a 1 , … , a n and t t t be nonnegative integers, and let 0 ≤ ℓ ≤ h 0\le\ell\le h 0 ≤ ℓ ≤ h be integers. Then the following are equivalent:
There is a subset S ⊆ [ n ] S\subseteq[n] S ⊆ [ n ] with Σ ( S ) = t \Sigma(S)=t Σ ( S ) = t and ∣ S ∣ ∈ [ ℓ , h ] |S|\in[\ell,h] ∣ S ∣ ∈ [ ℓ , h ] .
The TCSS instance obtained by appending h − ℓ h-\ell h − ℓ zero-valued items, with target cardinality h h h and target t t t , is a YES-instance.
Algorithm 1 FasterTCSS((a 1 , … , a n a_1,\ldots,a_n a 1 , … , a n ), t t t , k k k ); see Theorem 17
Replace values greater than t t t by t + 1 t+1 t + 1 and add values of t + 1 t+1 t + 1 to get a power-of-four size.
Define A A A , B B B as above. ▷ see § 4.1.2
Γ Z ( s i z e ) ← 2 e n + 4 c log n \Gamma_Z^{(\mathrm{size})} \leftarrow2e\sqrt{n}+4c\log n Γ Z ( size ) ← 2 e n + 4 c log n , Γ A , Z ( s i z e ) ← 20 c n log n \Gamma_{A,Z}^{(\mathrm{size})} \leftarrow20c\sqrt{n}\log n Γ A , Z ( size ) ← 20 c n log n , Γ A , Z ( s u m ) ← 7 c ( t / n ) log 2 n \Gamma_{A,Z}^{(\mathrm{sum})} \leftarrow7c(t/\sqrt{n})\log^2 n Γ A , Z ( sum ) ← 7 c ( t / n ) log 2 n , δ ← n − 4 c \delta\leftarrow n^{-4c} δ ← n − 4 c
Z 1 , … , Z n ← ∅ Z_1,\ldots,Z_{\sqrt{n}} \leftarrow\varnothing Z 1 , … , Z n ← ∅ ▷ Color classes; see § 4.1.1
for i = 1 , … , n i=1,\ldots,n i = 1 , … , n do
Choose j j j independently and uniformly from [ n ] [\sqrt{n}] [ n ] .
Z j ← Z j ∪ { i } Z_j \leftarrow Z_j \cup\{i\} Z j ← Z j ∪ { i }
end for
if some ∣ Z j ∣ > Γ Z ( s i z e ) |Z_j|>\Gamma_Z^{(\mathrm{size})} ∣ Z j ∣ > Γ Z ( size ) then
return NO
end if
for j = 1 , … , n j=1,\ldots,\sqrt{n} j = 1 , … , n do
I j ′ ← { ( ∣ S ∣ , Σ ( S ) ) : S ⊆ Z j ∩ A , Σ ( S ) ≤ Γ A , Z ( s u m ) } \mathcal{I}'_j \leftarrow\{(|S|,\Sigma(S)):S\subseteq Z_j\cap A,\ \Sigma(S)\leq\Gamma_{A,Z}^{(\mathrm{sum})}\} I j ′ ← {( ∣ S ∣ , Σ ( S )) : S ⊆ Z j ∩ A , Σ ( S ) ≤ Γ A , Z ( sum ) } ▷ Lemma 6
I j ( 0 ) ← { ( s − ⌊ k / n ⌋ , w ) : ( s , w ) ∈ I j ′ , ∣ s − ⌊ k / n ⌋ ∣ ≤ Γ A , Z ( s i z e ) } \mathcal{I}^{(0)}_j \leftarrow\{(s-\lfloor k/\sqrt{n}\rfloor,w):(s,w)\in\mathcal{I}'_j,\ |s-\lfloor k/\sqrt{n}\rfloor|\leq\Gamma_{A,Z}^{(\mathrm{size})}\} I j ( 0 ) ← {( s − ⌊ k / n ⌋ , w ) : ( s , w ) ∈ I j ′ , ∣ s − ⌊ k / n ⌋ ∣ ≤ Γ A , Z ( size ) }
end for
J ← { ( ∣ S ∣ , Σ ( S ) ) : S ⊆ B , Σ ( S ) ≤ t } \mathcal{J}\leftarrow\{(|S|,\Sigma(S)):S\subseteq B,\ \Sigma(S)\leq t\} J ← {( ∣ S ∣ , Σ ( S )) : S ⊆ B , Σ ( S ) ≤ t } ▷ Lemma 6
for ℓ = 1 , … , 1 2 log n \ell=1,\ldots,\frac{1}{2}\log n ℓ = 1 , … , 2 1 log n do
for j = 1 , … , n / 2 ℓ j=1,\ldots,\sqrt{n}/2^\ell j = 1 , … , n / 2 ℓ do
I ′ ← I 2 j − 1 ( ℓ − 1 ) ⊕ I 2 j ( ℓ − 1 ) \mathcal{I}'\leftarrow\mathcal{I}^{(\ell-1)}_{2j-1}\oplus\mathcal{I}^{(\ell-1)}_{2j} I ′ ← I 2 j − 1 ( ℓ − 1 ) ⊕ I 2 j ( ℓ − 1 )
I j ( ℓ ) ← { ( d , w ) : ( d ′ , w ) ∈ I ′ , ∣ d ∣ ≤ Γ A , Z ( s i z e ) , d = d ′ + 2 ⌊ 2 ℓ − 1 k / n ⌋ − ⌊ 2 ℓ k / n ⌋ } \mathcal{I}^{(\ell)}_j\leftarrow\{(d,w):(d',w)\in\mathcal{I}',\ |d|\leq\Gamma_{A,Z}^{(\mathrm{size})},\ d=d'+2\lfloor2^{\ell-1}k/\sqrt{n}\rfloor-\lfloor2^\ell k/\sqrt{n}\rfloor\} I j ( ℓ ) ← {( d , w ) : ( d ′ , w ) ∈ I ′ , ∣ d ∣ ≤ Γ A , Z ( size ) , d = d ′ + 2 ⌊ 2 ℓ − 1 k / n ⌋ − ⌊ 2 ℓ k / n ⌋}
end for
end for
for ( s , w ) ∈ J (s,w)\in\mathcal{J} ( s , w ) ∈ J do
if ( − s , t − w ) ∈ I 1 ( 1 2 log n ) (-s,t-w)\in\mathcal{I}^{(\frac{1}{2}\log n)}_1 ( − s , t − w ) ∈ I 1 ( 2 1 l o g n ) then
return YES
end if
end for
return NO
Proof. Suppose the first statement holds, and let S ⊆ [ n ] S \subseteq[n] S ⊆ [ n ] have Σ ( S ) = t \Sigma(S) = t Σ ( S ) = t and ∣ S ∣ ∈ [ ℓ , h ] |S| \in[\ell,h] ∣ S ∣ ∈ [ ℓ , h ] . Since h − ∣ S ∣ ≤ h − ℓ h-|S| \le h-\ell h − ∣ S ∣ ≤ h − ℓ , we can add h − ∣ S ∣ h-|S| h − ∣ S ∣ of the appended zeros to S S S . The resulting subset has cardinality h h h and sum t t t , proving the second statement. Conversely, suppose the second statement holds, and let S ′ ⊆ [ n + h − ℓ ] S' \subseteq[n+h-\ell] S ′ ⊆ [ n + h − ℓ ] have cardinality h h h and sum t t t in the augmented instance. The subset S = S ′ ∩ [ n ] S=S'\cap[n] S = S ′ ∩ [ n ] has sum t t t , since all appended items have value zero. Moreover, ∣ S ∣ = h − ∣ S ′ ∩ { n + 1 , … , n + h − ℓ } ∣ ≥ h − ( h − ℓ ) = ℓ |S|=h-|S'\cap\{n+1,\ldots,n+h-\ell\}|\ge h-(h-\ell)=\ell ∣ S ∣ = h − ∣ S ′ ∩ { n + 1 , … , n + h − ℓ } ∣ ≥ h − ( h − ℓ ) = ℓ , while ∣ S ∣ ≤ ∣ S ′ ∣ = h |S|\le|S'|=h ∣ S ∣ ≤ ∣ S ′ ∣ = h . Thus S S S satisfies the first statement.
This allows us to solve LCSS using a binary-search approach.
Theorem 3. For any fixed integer constant c ≥ 10 c\ge10 c ≥ 10 , there exists a randomized algorithm that solves LCSS on n n n nonnegative integers with target t t t in time O ~ ( n + n t ) \widetilde{O}(n+\sqrt{nt}) O ( n + n t ) , with error probability at most n − c n^{-c} n − c .
Proof. First we test feasibility by calling Theorem 17 with target t t t and target cardinality n n n on the original items augmented by n n n zeros. A correct call returns YES exactly when the original instance has a subset of sum t t t and cardinality in [ 1 , n ] [1,n] [ 1 , n ] . Since t > 0 t>0 t > 0 , this is equivalent to the existence of a subset of sum t t t . If the call returns NO, we output that no solutions exist. The test takes O ~ ( n + n t ) \widetilde{O}(n+\sqrt{nt}) O ( n + n t ) time and never declares an infeasible instance feasible.
If the feasibility test succeeds, set ℓ = 0 \ell=0 ℓ = 0 , h = n + 1 h=n+1 h = n + 1 . Let k ∗ k^* k ∗ be the cardinality of the largest subset summing up to t t t . Our invariant throughout will be k ∗ ∈ [ ℓ , h ) k^*\in[\ell,h) k ∗ ∈ [ ℓ , h ) ; this holds at initialization. At each iteration, we set m = ⌊ ( ℓ + h ) / 2 ⌋ m=\lfloor(\ell+h)/2\rfloor m = ⌊( ℓ + h ) /2 ⌋ and apply Theorem 17 with target cardinality h h h to the original items augmented by h − m h-m h − m zeros. By Lemma 19 , a correct YES answer occurs exactly when the original instance has a subset of sum t t t and cardinality in [ m , h ] [m,h] [ m , h ] . From the invariant in the previous iteration, we had k ∗ < h k^*<h k ∗ < h . Thus a correct YES answer implies k ∗ ∈ [ m , h ) k^*\in[m,h) k ∗ ∈ [ m , h ) , and a correct NO answer implies k ∗ ∈ [ ℓ , m ) k^*\in[\ell,m) k ∗ ∈ [ ℓ , m ) . We update ℓ ← m \ell\gets m ℓ ← m and h ← m h\gets m h ← m respectively for these cases, preserving the invariant. When h = ℓ + 1 h=\ell+1 h = ℓ + 1 , the invariant gives k ∗ = ℓ k^*=\ell k ∗ = ℓ . Including the feasibility test, there are at most ( 1 + ⌈ log ( n + 1 ) ⌉ ) (1+\lceil\log(n+1)\rceil) ( 1 + ⌈ log ( n + 1 )⌉) calls to Theorem 17 , each on at most 2 n 2n 2 n items. Their total time is O ~ ( n + n t ) \widetilde{O}(n+\sqrt{nt}) O ( n + n t ) . Conditioning on the history before any call fixes its input, Theorem 17 bounds its conditional error probability of any specific call by n − 2 c n^{-2c} n − 2 c . A union bound gives total error at most ( 1 + ⌈ log ( n + 1 ) ⌉ ) n − 2 c ≤ n − c (1+\lceil\log(n+1)\rceil)n^{-2c}\le n^{-c} ( 1 + ⌈ log ( n + 1 )⌉) n − 2 c ≤ n − c . Algorithm 2 summarizes this.
Algorithm 2 FasterLCSS((a 1 , … , a n a_1,\ldots,a_n a 1 , … , a n ), t t t ); see Theorem 3
if FasterTCSS((a 1 , … , a n , 0 , … , 0 a_1,\ldots,a_n,0,\ldots,0 a 1 , … , a n , 0 , … , 0 ), t , n t,n t , n ) = NO then Algorithm 1
return “No Subset with sum t t t ”
end if
ℓ ← 0 \ell\gets0 ℓ ← 0 , h ← n + 1 h\gets n+1 h ← n + 1
while ℓ < h − 1 \ell<h-1 ℓ < h − 1 do
m ← ⌊ ( ℓ + h ) / 2 ⌋ m\gets\lfloor(\ell+h)/2\rfloor m ← ⌊( ℓ + h ) /2 ⌋
if FasterTCSS((a 1 , … , a n , 0 , … , 0 a_1,\ldots,a_n,0,\ldots,0 a 1 , … , a n , 0 , … , 0 ), t , h t,h t , h ) = YES then
ℓ ← m \ell\gets m ℓ ← m
else
h ← m h\gets m h ← m
end if
end while
return ℓ \ell ℓ
All-Targets Largest Cardinality Subset Sum
We recall the problem of AT-LCSS (Definition 2 ): Given nonnegative integers a 1 , … , a n a_1,\ldots,a_n a 1 , … , a n and a positive integer t t t , the task is to compute, for every s ∈ [ t ] s \in[t] s ∈ [ t ] , the largest cardinality of a subset of indices whose sum is s s s . For this section, we assume without loss of generality that all items are positive (otherwise we simply delete all zero items and add back their count in all outputs, see proof of Theorem 4).
We fix an integer threshold 1 ≤ τ ≤ t 1 \le\tau\le t 1 ≤ τ ≤ t . We define heavy and light elements as follows.
Definition 20 (Heavy elements H H H and light elements L L L ). Let H = { i ∈ [ n ] : a i > τ } H = \{i \in[n] : a_i > \tau\} H = { i ∈ [ n ] : a i > τ } be heavy elements, and let L = { i ∈ [ n ] : a i ≤ τ } L = \{i \in[n] : a_i \le\tau\} L = { i ∈ [ n ] : a i ≤ τ } be light elements.
We handle heavy items and light items separately, and then incorporate them to get an efficient algorithm for AT-LCSS.
Handling heavy items
A simple application of Lemma 6 allows us to efficiently compute and store for every s ∈ [ t ] 0 s \in[t]_0 s ∈ [ t ] 0 the largest cardinality of a subset of the heavy element indices which sum up to s s s .
Lemma 21. There exists a randomized algorithm computing largest ( s ) = max { ∣ S ∣ : S ⊆ H , Σ ( S ) = s } \operatorname{largest}(s) = \max\{|S| : S \subseteq H, \Sigma(S) = s\} largest ( s ) = max { ∣ S ∣ : S ⊆ H , Σ ( S ) = s } for every s ∈ [ t ] 0 s \in[t]_0 s ∈ [ t ] 0 in time O ~ ( ∣ H ∣ + t 2 / τ ) \widetilde{O}(|H| + t^2/\tau) O ( ∣ H ∣ + t 2 / τ ) with failure probability ≤ n − c \le n^{-c} ≤ n − c .
Proof. Each item in H H H is greater than τ \tau τ . Hence every subset with sum at most t t t has cardinality less than t / τ t/\tau t / τ . We apply Lemma 6 once to ( a i ) i ∈ H (a_i)_{i\in H} ( a i ) i ∈ H with k = ⌊ t / τ ⌋ k = \lfloor t/\tau\rfloor k = ⌊ t / τ ⌋ and δ = n − c \delta= n^{-c} δ = n − c to obtain A = { ( Σ ( S ) , ∣ S ∣ ) ∣ S ⊆ H , ∣ S ∣ ≤ k , Σ ( S ) ≤ t } \mathcal{A} = \{(\Sigma(S), |S|) \mid S \subseteq H, |S| \le k, \Sigma(S) \le t\} A = {( Σ ( S ) , ∣ S ∣ ) ∣ S ⊆ H , ∣ S ∣ ≤ k , Σ ( S ) ≤ t } with probability at least 1 − n − c 1 - n^{-c} 1 − n − c . Every returned pair is realized by a subset of H H H . Since log ( 1 / δ ) = c log n \log(1/\delta) = c\log n log ( 1/ δ ) = c log n , the call takes O ~ ( ∣ H ∣ + k t ) = O ~ ( ∣ H ∣ + t 2 / τ ) \widetilde{O}(|H| + kt) = \widetilde{O}(|H| + t^2/\tau) O ( ∣ H ∣ + k t ) = O ( ∣ H ∣ + t 2 / τ ) time.
We initialize the array largest ( ⋅ ) \operatorname{largest}(\cdot) largest ( ⋅ ) to − ∞ -\infty − ∞ and, for each returned pair ( s , r ) ∈ A (s,r) \in\mathcal{A} ( s , r ) ∈ A , we replace largest ( s ) \operatorname{largest}(s) largest ( s ) by the maximum of its current value and r r r ; the resulting entry is precisely the largest cardinality of a subset S ⊆ H S \subseteq H S ⊆ H with sum s s s .
For each sum s ∈ [ t ] 0 s \in[t]_0 s ∈ [ t ] 0 , it is sufficient to store only this maximum cardinality largest ( s ) \operatorname{largest}(s) largest ( s ) . All items subsequently added come from the light items in the input.
Processing light items
We consider all items in the input to have an associated profit of 1. Then AT-LCSS asks for every s ∈ [ t ] s \in[t] s ∈ [ t ] , the maximum profit subset with values summing up to s s s .
We now represent items by a pair ( w , p ) (w,p) ( w , p ) , where w w w is its value and p p p is its profit. We consider the light items in the input. Initially, each light item a i a_i a i is represented by ( a i , 1 ) (a_i,1) ( a i , 1 ) . We now use the multiplicity reduction trick from Lawler [16 ] , which deletes some of the existing items and introduces new items (possibly with profit more than 1) such that the maximum profit of subsets summing up to some s ∈ [ t ] s \in[t] s ∈ [ t ] does not change. This leads to a reduced total number of light items. We formally state it in the following lemma.
Lemma 22 (Small number of (weight, profit) pairs from light items). In time O ( ∣ L ∣ + τ log t ) O(|L|+\tau\log t) O ( ∣ L ∣ + τ log t ) , one can construct a multiset L \mathcal{L} L of size at most O ( τ log t ) O(\tau\log t) O ( τ log t ) , which contains pairs of positive integers, such that for every s ∈ [ t ] 0 s \in[t]_0 s ∈ [ t ] 0
max { ∣ S ∣ : S ⊆ L , Σ ( S ) = s } = max { ∑ ( w , p ) ∈ S ′ p : S ′ ⊆ L , ∑ ( w , p ) ∈ S ′ w = s } . (10) \max\{|S| : S \subseteq L, \Sigma(S) = s\} = \max\left\{\sum_{(w,p)\in S'} p : S' \subseteq\mathcal{L}, \sum_{(w,p)\in S'} w = s\right\}. \tag*{(10)} max { ∣ S ∣ : S ⊆ L , Σ ( S ) = s } = max ⎩ ⎨ ⎧ ( w , p ) ∈ S ′ ∑ p : S ′ ⊆ L , ( w , p ) ∈ S ′ ∑ w = s ⎭ ⎬ ⎫ . (10)
Proof. For each pair ( w , p ) ∈ L (w,p) \in\mathcal{L} ( w , p ) ∈ L , we refer to w w w as its weight and to p p p as its profit . We first describe the construction of L \mathcal{L} L , prove that Equation (10) holds, then bound the size of L \mathcal{L} L and finally argue about the time complexity to compute L \mathcal{L} L .
Construction of L \mathcal{L} L . Initially, for each index i ∈ L i \in L i ∈ L we add to L \mathcal{L} L the pair ( a i , 1 ) (a_i,1) ( a i , 1 ) . We repeatedly perform the following operations. We stop when neither applies.
If a pair ( w , p ) (w,p) ( w , p ) occurs at least three times, we remove two copies and insert one copy of ( 2 w , 2 p ) (2w,2p) ( 2 w , 2 p ) .
We delete every pair ( w , p ) (w,p) ( w , p ) with w > t w > t w > t .
Proof of Equation (10). We prove that for every s ∈ [ t ] 0 s \in[t]_0 s ∈ [ t ] 0 , the value of
max { ∑ ( w , p ) ∈ S ′ p : S ′ ⊆ L , ∑ ( w , p ) ∈ S ′ w = s } \max\left\{\sum_{(w,p)\in S'} p : S' \subseteq\mathcal{L}, \sum_{(w,p)\in S'} w = s\right\} max ⎩ ⎨ ⎧ ( w , p ) ∈ S ′ ∑ p : S ′ ⊆ L , ( w , p ) ∈ S ′ ∑ w = s ⎭ ⎬ ⎫
remains unchanged after each iteration, where L \mathcal{L} L denotes the current multiset. Fix s ∈ [ t ] 0 s \in[t]_0 s ∈ [ t ] 0 . We suppose that in some iteration we replace two copies of a pair ( w , p ) (w,p) ( w , p ) occurring at least three times by one copy of ( 2 w , 2 p ) (2w,2p) ( 2 w , 2 p ) . We compare the value of this maximum on the multisets L b e f o r e \mathcal{L}_{\mathrm{before}} L before and L a f t e r \mathcal{L}_{\mathrm{after}} L after immediately before and after this operation, respectively. We refer to all copies of ( w , p ) (w,p) ( w , p ) which were not removed in this iteration as surviving copies .
The maximum cannot increase: given any submultiset of L a f t e r \mathcal{L}_{\mathrm{after}} L after containing the newly inserted copy of ( 2 w , 2 p ) (2w,2p) ( 2 w , 2 p ) , we replace that copy by the two copies of ( w , p ) (w,p) ( w , p ) removed from L b e f o r e \mathcal{L}_{\mathrm{before}} L before . These two copies are distinct from all surviving copies, so this replacement respects multiplicities and preserves both total weight and total profit. On the other hand, a submultiset that does not contain the newly inserted copy is already available in L b e f o r e \mathcal{L}_{\mathrm{before}} L before .
The maximum cannot decrease either. Given any submultiset of L b e f o r e \mathcal{L}_{\mathrm{before}} L before containing at least two copies of ( w , p ) (w,p) ( w , p ) , we replace two of its copies by the newly inserted copy of ( 2 w , 2 p ) (2w,2p) ( 2 w , 2 p ) . We can select its remaining copies of ( w , p ) (w,p) ( w , p ) from those surviving in L a f t e r \mathcal{L}_{\mathrm{after}} L after . If the submultiset contains exactly one copy of ( w , p ) (w,p) ( w , p ) , we select a surviving copy; such a copy exists because there were at least three copies before the operation. If it contains no copies of ( w , p ) (w,p) ( w , p ) , we leave it unchanged. In each case, we retain all other selected pairs and preserve total weight and total profit.
Each iteration decreases the number of pairs by at least one. Hence the construction terminates after at most ∣ L ∣ |L| ∣ L ∣ such iterations. Initially, we have one pair ( a i , 1 ) (a_i,1) ( a i , 1 ) for each index i ∈ L i \in L i ∈ L . Thus subsets of L L L correspond to submultisets of the initial pairs with the same total weight and with total profit equal to their cardinality, establishing Equation (10) at initialization. Since each iteration preserves the maximum in Equation (10), the equality holds for the final multiset L \mathcal{L} L as well.
Size of L \mathcal{L} L . We first observe that every pair occurring during the construction has the form ( 2 j v , 2 j ) (2^jv,2^j) ( 2 j v , 2 j ) for some v ∈ [ τ ] v \in[\tau] v ∈ [ τ ] , j ≥ 0 j \ge0 j ≥ 0 . This is because initially this holds with j = 0 j=0 j = 0 and subsequently if we replace two copies of ( 2 j v , 2 j ) (2^jv,2^j) ( 2 j v , 2 j ) , the inserted pair is ( 2 j + 1 v , 2 j + 1 ) (2^{j+1}v,2^{j+1}) ( 2 j + 1 v , 2 j + 1 ) .
At termination, every pair in L \mathcal{L} L has weight at most t t t , and multiplicity at most two. For a pair ( 2 j v , 2 j ) (2^jv,2^j) ( 2 j v , 2 j ) in L \mathcal{L} L , its weight is at most t t t , so 0 ≤ j ≤ ⌊ log ( t / v ) ⌋ 0 \le j \le\lfloor\log(t/v)\rfloor 0 ≤ j ≤ ⌊ log ( t / v )⌋ . For each v ∈ [ τ ] v \in[\tau] v ∈ [ τ ] , there are thus ⌊ log ( t / v ) ⌋ + 1 \lfloor\log(t/v)\rfloor+ 1 ⌊ log ( t / v )⌋ + 1 possible pairs, each of multiplicity at most two. This gives us ∣ L ∣ = O ( τ log t ) |\mathcal{L}| = O(\tau\log t) ∣ L ∣ = O ( τ log t ) .
Running time. For completeness, we describe an efficient implementation of the construction of L \mathcal{L} L . We start by storing the multiplicity of each possible pair ( 2 j v , 2 j ) (2^jv,2^j) ( 2 j v , 2 j ) in a two-dimensional array indexed by v ∈ [ τ ] v \in[\tau] v ∈ [ τ ] and 0 ≤ j ≤ ⌊ log ( t / v ) ⌋ 0 \le j \le\lfloor\log(t/v)\rfloor 0 ≤ j ≤ ⌊ log ( t / v )⌋ . Distinct indices represent distinct pairs: the profit determines j j j , and the weight then determines v v v . We initialize the array to zero and count the input occurrences in the entries with j = 0 j=0 j = 0 , in time O ( ∣ L ∣ + τ log t ) O(|L|+\tau\log t) O ( ∣ L ∣ + τ log t ) .
For each v v v , we scan its entries in increasing order of j j j . While the current multiplicity is at least three, we (i) decrease it by two and, (ii) if 2 j + 1 v ≤ t 2^{j+1}v \le t 2 j + 1 v ≤ t , increase the next entry (i.e. indexed by v v v and j + 1 j+1 j + 1 ) by one. Each such step decreases the total number of items by at least one, so there are at most ∣ L ∣ |L| ∣ L ∣ steps in total. Since updates only affect the entries with a larger value of j j j , each entry which has already been processed has a multiplicity of at most two. We output these remaining copies. Scanning the array and writing the output take O ( τ log t ) O(\tau\log t) O ( τ log t ) time. Hence the total running time is bounded by O ( ∣ L ∣ + τ log t ) O(|L|+\tau\log t) O ( ∣ L ∣ + τ log t ) .
Algorithm for All-Targets Largest Cardinality Subset Sum
We now have all ingredients to prove Theorem 4.
Theorem 4. For any fixed integer constant c ≥ 10 c \ge10 c ≥ 10 , there exists a randomized algorithm that solves AT-LCSS on n n n nonnegative integers with target t t t in time O ~ ( n + t 3 / 2 ) \widetilde{O}(n+t^{3/2}) O ( n + t 3/2 ) , with failure probability at most n − c n^{-c} n − c .
Proof. Let z = ∣ { i ∈ [ n ] : a i = 0 } ∣ z=|\{i\in[n]:a_i=0\}| z = ∣ { i ∈ [ n ] : a i = 0 } ∣ be the number of items with value 0. We now discard all items of value 0 (we simply add z z z to every output in the end) and all items of value greater than t t t . We set τ = ⌈ t ⌉ \tau=\lceil\sqrt{t}\rceil τ = ⌈ t ⌉ . Let L L L and H H H be the sets of indices of remaining items with values at most τ \tau τ and greater than τ \tau τ , respectively (recall Definition 20).
By Lemma 21, we compute largest ( s ) \operatorname{largest}(s) largest ( s ) for every s ∈ [ t ] 0 s\in[t]_0 s ∈ [ t ] 0 , where largest ( s ) \operatorname{largest}(s) largest ( s ) is the maximum cardinality of a subset of H H H whose sum is s s s . By Lemma 22, we construct a multiset L \mathcal{L} L of weight–profit pairs such that, for every s ∈ [ t ] s\in[t] s ∈ [ t ] , the maximum profit of a submultiset of L \mathcal{L} L of weight s s s equals the maximum cardinality of a subset of L L L of sum s s s . We enumerate the occurrences in L \mathcal{L} L as ( w 1 , p 1 ) , … , ( w m , p m ) (w_1,p_1),\ldots,(w_m,p_m) ( w 1 , p 1 ) , … , ( w m , p m ) , where m = ∣ L ∣ = O ( τ log t ) m=|\mathcal{L}|=O(\tau\log t) m = ∣ L ∣ = O ( τ log t ) . For the correctness proof, we condition on the event that the call to Lemma 21 was successful. This happens with probability at least 1 − n − c 1-n^{-c} 1 − n − c .
We approach using dynamic programming. The entry D P j [ s ] \mathrm{DP}_j[s] DP j [ s ] stores the maximum of the number of selected heavy items plus the profit of selected pairs among the first j j j pairs of L \mathcal{L} L , subject to their total weight being s s s . Formally, for 0 ≤ j ≤ m 0\le j\le m 0 ≤ j ≤ m and 0 ≤ s ≤ t 0\le s\le t 0 ≤ s ≤ t , we define
D P j [ s ] : = max { ∣ S ∣ + ∑ i ∈ I p i : S ⊆ H , I ⊆ [ j ] , ∑ ( S ) + ∑ i ∈ I w i = s } . \mathrm{DP}_j[s] := \max\left\{|S|+\sum_{i\in I}p_i : S\subseteq H,\ I\subseteq[j],\ \sum(S)+\sum_{i\in I}w_i=s\right\}. DP j [ s ] := max { ∣ S ∣ + i ∈ I ∑ p i : S ⊆ H , I ⊆ [ j ] , ∑ ( S ) + i ∈ I ∑ w i = s } .
We initialize D P 0 [ s ] = largest ( s ) \mathrm{DP}_0[s]=\operatorname{largest}(s) DP 0 [ s ] = largest ( s ) and, for j = 1 , … , m j=1,\ldots,m j = 1 , … , m , we compute
D P j [ s ] = { D P j − 1 [ s ] , s < w j , max { D P j − 1 [ s ] , D P j − 1 [ s − w j ] + p j } , s ≥ w j . \mathrm{DP}_j[s] =
\begin{cases}
\mathrm{DP}_{j-1}[s], & s<w_j,\\
\max\{\mathrm{DP}_{j-1}[s],\mathrm{DP}_{j-1}[s-w_j]+p_j\}, & s\ge w_j.
\end{cases} DP j [ s ] = { DP j − 1 [ s ] , max { DP j − 1 [ s ] , DP j − 1 [ s − w j ] + p j } , s < w j , s ≥ w j .
We prove by induction on j j j that the recurrence computes the stated value of D P j [ s ] \mathrm{DP}_j[s] DP j [ s ] for every s ∈ [ t ] 0 s\in[t]_0 s ∈ [ t ] 0 . For j = 0 j=0 j = 0 , no pair from L \mathcal{L} L is available, so D P 0 [ s ] = largest ( s ) \mathrm{DP}_0[s]=\operatorname{largest}(s) DP 0 [ s ] = largest ( s ) by their definitions. Now let j ≥ 1 j\ge1 j ≥ 1 and assume the claim for j − 1 j-1 j − 1 . If s < w j s<w_j s < w j , then a subset of the first j j j pairs having total weight at most s s s cannot contain the j j j -th pair ( w j , p j ) (w_j,p_j) ( w j , p j ) . Consequently, D P j [ s ] = D P j − 1 [ s ] \mathrm{DP}_j[s]=\mathrm{DP}_{j-1}[s] DP j [ s ] = DP j − 1 [ s ] . From now, we suppose that s ≥ w j s\ge w_j s ≥ w j .
For the upper bound, we consider S ∗ ⊆ H S^*\subseteq H S ∗ ⊆ H and I ∗ ⊆ [ j ] I^*\subseteq[j] I ∗ ⊆ [ j ] satisfying ∑ ( S ∗ ) + ∑ i ∈ I ∗ w i = s \sum(S^*)+\sum_{i\in I^*}w_i=s ∑ ( S ∗ ) + ∑ i ∈ I ∗ w i = s such that D P j [ s ] = ∣ S ∗ ∣ + ∑ i ∈ I ∗ p i \mathrm{DP}_j[s]=|S^*|+\sum_{i\in I^*}p_i DP j [ s ] = ∣ S ∗ ∣ + ∑ i ∈ I ∗ p i . If j ∉ I ∗ j\notin I^* j ∈ / I ∗ , then ∣ S ∗ ∣ + ∑ i ∈ I ∗ p i ≤ D P j − 1 [ s ] |S^*|+\sum_{i\in I^*}p_i\le\mathrm{DP}_{j-1}[s] ∣ S ∗ ∣ + ∑ i ∈ I ∗ p i ≤ DP j − 1 [ s ] . If j ∈ I ∗ j\in I^* j ∈ I ∗ , then Σ ( S ∗ ) + ∑ i ∈ I ∗ ∖ { j } w i = s − w j \Sigma(S^*)+\sum_{i\in I^*\setminus\{j\}}w_i=s-w_j Σ ( S ∗ ) + ∑ i ∈ I ∗ ∖ { j } w i = s − w j , and hence ∣ S ∗ ∣ + ∑ i ∈ I ∗ p i ≤ D P j − 1 [ s − w j ] + p j |S^*|+\sum_{i\in I^*}p_i\leq\mathrm{DP}_{j-1}[s-w_j]+p_j ∣ S ∗ ∣ + ∑ i ∈ I ∗ p i ≤ DP j − 1 [ s − w j ] + p j . As at least one of these inequalities holds, we get
D P j [ s ] = ∣ S ∗ ∣ + ∑ i ∈ I ∗ p i ≤ max { D P j − 1 [ s ] , D P j − 1 [ s − w j ] + p j } . (11) \mathrm{DP}_j[s]=|S^*|+\sum_{i\in I^*}p_i\leq\max\{\mathrm{DP}_{j-1}[s],\mathrm{DP}_{j-1}[s-w_j]+p_j\}. \tag*{(11)} DP j [ s ] = ∣ S ∗ ∣ + i ∈ I ∗ ∑ p i ≤ max { DP j − 1 [ s ] , DP j − 1 [ s − w j ] + p j } . (11)
For the reverse inequality, we consider S 1 ∗ ⊆ H S_1^*\subseteq H S 1 ∗ ⊆ H and I 1 ∗ ⊆ [ j ] ∖ { j } I_1^*\subseteq[j]\setminus\{j\} I 1 ∗ ⊆ [ j ] ∖ { j } satisfying Σ ( S 1 ∗ ) + ∑ i ∈ I 1 ∗ w i = s \Sigma(S_1^*)+\sum_{i\in I_1^*}w_i=s Σ ( S 1 ∗ ) + ∑ i ∈ I 1 ∗ w i = s such that D P j − 1 [ s ] = ∣ S 1 ∗ ∣ + ∑ i ∈ I 1 ∗ p i \mathrm{DP}_{j-1}[s]=|S_1^*|+\sum_{i\in I_1^*}p_i DP j − 1 [ s ] = ∣ S 1 ∗ ∣ + ∑ i ∈ I 1 ∗ p i . Since these subsets are also considered in the definition of D P j [ s ] \mathrm{DP}_j[s] DP j [ s ] , we get D P j [ s ] ≥ ∣ S 1 ∗ ∣ + ∑ i ∈ I 1 ∗ p i = D P j − 1 [ s ] \mathrm{DP}_j[s]\geq|S_1^*|+\sum_{i\in I_1^*}p_i=\mathrm{DP}_{j-1}[s] DP j [ s ] ≥ ∣ S 1 ∗ ∣ + ∑ i ∈ I 1 ∗ p i = DP j − 1 [ s ] . Similarly, we consider S 2 ∗ ⊆ H S_2^*\subseteq H S 2 ∗ ⊆ H and I 2 ∗ ⊆ [ j ] ∖ { j } I_2^*\subseteq[j]\setminus\{j\} I 2 ∗ ⊆ [ j ] ∖ { j } satisfying Σ ( S 2 ∗ ) + ∑ i ∈ I 2 ∗ w i = s − w j \Sigma(S_2^*)+\sum_{i\in I_2^*}w_i=s-w_j Σ ( S 2 ∗ ) + ∑ i ∈ I 2 ∗ w i = s − w j such that D P j − 1 [ s − w j ] = ∣ S 2 ∗ ∣ + ∑ i ∈ I 2 ∗ p i \mathrm{DP}_{j-1}[s-w_j]=|S_2^*|+\sum_{i\in I_2^*}p_i DP j − 1 [ s − w j ] = ∣ S 2 ∗ ∣ + ∑ i ∈ I 2 ∗ p i . We retain S 2 ∗ S_2^* S 2 ∗ and replace I 2 ∗ I_2^* I 2 ∗ by I 2 ∗ ∪ { j } I_2^*\cup\{j\} I 2 ∗ ∪ { j } , thereby selecting the j j j -th pair ( w j , p j ) (w_j,p_j) ( w j , p j ) . The total weight becomes s s s , so D P j [ s ] ≥ ∣ S 2 ∗ ∣ + ∑ i ∈ I 2 ∗ p i + p j = D P j − 1 [ s − w j ] + p j \mathrm{DP}_j[s]\geq|S_2^*|+\sum_{i\in I_2^*}p_i+p_j=\mathrm{DP}_{j-1}[s-w_j]+p_j DP j [ s ] ≥ ∣ S 2 ∗ ∣ + ∑ i ∈ I 2 ∗ p i + p j = DP j − 1 [ s − w j ] + p j . Together, these inequalities give
D P j [ s ] ≥ max { D P j − 1 [ s ] , D P j − 1 [ s − w j ] + p j } . (12) \mathrm{DP}_j[s]\geq\max\{\mathrm{DP}_{j-1}[s],\mathrm{DP}_{j-1}[s-w_j]+p_j\}. \tag*{(12)} DP j [ s ] ≥ max { DP j − 1 [ s ] , DP j − 1 [ s − w j ] + p j } . (12)
The two inequalities Equations (11) and (12) give the desired equality and complete the induction.
It remains to relate D P m \mathrm{DP}_m DP m to the original instance. For every s ∈ [ t ] s\in[t] s ∈ [ t ] , the definition of D P m [ s ] \mathrm{DP}_m[s] DP m [ s ] gives
D P m [ s ] = max { ∣ S ∣ + ∑ ( w , p ) ∈ S ′ p : S ⊆ H , S ′ ⊆ L , Σ ( S ) + ∑ ( w , p ) ∈ S ′ w = s } = max r ∈ [ s ] 0 { largest ( r ) + max { ∑ ( w , p ) ∈ S ′ p : S ′ ⊆ L , ∑ ( w , p ) ∈ S ′ w = s − r } } = max r ∈ [ s ] 0 { max { ∣ S 1 ∣ : S 1 ⊆ H , Σ ( S 1 ) = r } + max { ∣ S 2 ∣ : S 2 ⊆ L , Σ ( S 2 ) = s − r } } = max { ∣ S ∣ : S ⊆ H ∪ L , Σ ( S ) = s } . \begin{aligned}
\mathrm{DP}_m[s]
&=\max\left\{|S|+\sum_{(w,p)\in S'}p:S\subseteq H,\ S'\subseteq L,\ \Sigma(S)+\sum_{(w,p)\in S'}w=s\right\}\\
&=\max_{r\in[s]_0}\left\{\operatorname{largest}(r)+\max\left\{\sum_{(w,p)\in S'}p:S'\subseteq L,\ \sum_{(w,p)\in S'}w=s-r\right\}\right\}\\
&=\max_{r\in[s]_0}\left\{\max\{|S_1|:S_1\subseteq H,\ \Sigma(S_1)=r\}+\max\{|S_2|:S_2\subseteq L,\ \Sigma(S_2)=s-r\}\right\}\\
&=\max\{|S|:S\subseteq H\cup L,\ \Sigma(S)=s\}.
\end{aligned} DP m [ s ] = max ⎩ ⎨ ⎧ ∣ S ∣ + ( w , p ) ∈ S ′ ∑ p : S ⊆ H , S ′ ⊆ L , Σ ( S ) + ( w , p ) ∈ S ′ ∑ w = s ⎭ ⎬ ⎫ = r ∈ [ s ] 0 max ⎩ ⎨ ⎧ largest ( r ) + max ⎩ ⎨ ⎧ ( w , p ) ∈ S ′ ∑ p : S ′ ⊆ L , ( w , p ) ∈ S ′ ∑ w = s − r ⎭ ⎬ ⎫ ⎭ ⎬ ⎫ = r ∈ [ s ] 0 max { max { ∣ S 1 ∣ : S 1 ⊆ H , Σ ( S 1 ) = r } + max { ∣ S 2 ∣ : S 2 ⊆ L , Σ ( S 2 ) = s − r } } = max { ∣ S ∣ : S ⊆ H ∪ L , Σ ( S ) = s } .
The first equality is the definition of D P m [ s ] \mathrm{DP}_m[s] DP m [ s ] . For the second equality, we group the subsets by r = Σ ( S ) r=\Sigma(S) r = Σ ( S ) and maximize the heavy cardinality and light profit independently for each r r r . The third equality follows from the definition of largest ( r ) \operatorname{largest}(r) largest ( r ) and Equation (10) . The fourth equality follows from observing that subsets S 1 ⊆ H S_1\subseteq H S 1 ⊆ H and S 2 ⊆ L S_2\subseteq L S 2 ⊆ L form precisely the subsets of H ∪ L H\cup L H ∪ L which sum up to s s s . Every subset of H ∪ L H\cup L H ∪ L of sum s s s can be enlarged by all z z z zero-valued items. Conversely, deleting zero-valued items from any original solution of sum s s s leaves a subset of H ∪ L H\cup L H ∪ L of the same sum. Thus the required answer is D P m [ s ] + z \mathrm{DP}_m[s]+z DP m [ s ] + z if D P m [ s ] \mathrm{DP}_m[s] DP m [ s ] .
Preprocessing takes O ( n ) O(n) O ( n ) time. The two lemmas take O ~ ( ∣ H ∣ + t 2 / τ ) \widetilde{O}(|H|+t^2/\tau) O ( ∣ H ∣ + t 2 / τ ) and O ( ∣ L ∣ + τ log t ) O(|L|+\tau\log t) O ( ∣ L ∣ + τ log t ) time, respectively. The dynamic program takes O ( m t ) = O ( τ t log t ) O(mt)=O(\tau t\log t) O ( m t ) = O ( τ t log t ) time, and output takes O ( t ) O(t) O ( t ) time. Since ∣ L ∣ + ∣ H ∣ ≤ n |L|+|H|\leq n ∣ L ∣ + ∣ H ∣ ≤ n and τ = O ( t ) \tau=O(\sqrt{t}) τ = O ( t ) , the total is
O ~ ( n + t 2 τ + τ t ) = O ~ ( n + t 3 / 2 ) . \widetilde{O}\left(n+\frac{t^2}{\tau}+\tau t\right)=\widetilde{O}(n+t^{3/2}). O ( n + τ t 2 + τ t ) = O ( n + t 3/2 ) .
The only randomized step is the heavy-item computation, which is correct for all targets simultaneously with probability at least 1 − n − c 1-n^{-c} 1 − n − c by Lemma 21.
Projection Subset Sum
We consider the search version of Projection Subset Sum, introduced by Dutta and Rajasree [10 ] .
Throughout this section, Z Z Z and X X X denote the vectors ( z 1 , … , z n ) (z_1,\ldots,z_n) ( z 1 , … , z n ) and ( x 1 , … , x n ) (x_1,\ldots,x_n) ( x 1 , … , x n ) , respectively. For an input a 1 , … , a n a_1,\ldots,a_n a 1 , … , a n , we set z i = a i z_i=a_i z i = a i for every i ∈ [ n ] i\in[n] i ∈ [ n ] .
Definition 23 (Projection Subset Sum (PSSp _p p )). Fix p ≥ 1 p\ge1 p ≥ 1 . Given nonnegative integers Z = ( a 1 , … , a n ) Z=(a_1,\ldots,a_n) Z = ( a 1 , … , a n ) and a nonnegative integer t t t , the PSSp _p p problem asks for a vector X = ( x 1 , … , x n ) ∈ Z n X=(x_1,\ldots,x_n)\in\mathbb{Z}^n X = ( x 1 , … , x n ) ∈ Z n and a set S ⊆ [ n ] S\subseteq[n] S ⊆ [ n ] such that ∑ i ∈ S x i = t \sum_{i\in S}x_i=t ∑ i ∈ S x i = t and ∥ Z − X ∥ p \lVert Z-X\rVert_p ∥ Z − X ∥ p is minimized.
We assume n ≥ 1 n\ge1 n ≥ 1 and write Σ ( S ) = ∑ i ∈ S a i \Sigma(S)=\sum_{i\in S}a_i Σ ( S ) = ∑ i ∈ S a i . A sum s s s is attainable if Σ ( S ) = s \Sigma(S)=s Σ ( S ) = s for some S ⊆ [ n ] S\subseteq[n] S ⊆ [ n ] . When t = 0 t=0 t = 0 , we return X = Z X=Z X = Z and S = ∅ S=\varnothing S = ∅ . When n = 1 n=1 n = 1 and t > 0 t>0 t > 0 , we return X = ( t ) X=(t) X = ( t ) and S = { 1 } S=\{1\} S = { 1 } . The proofs of the two PSS theorems below therefore assume n ≥ 2 n\ge2 n ≥ 2 and t > 0 t>0 t > 0 , so every feasible set is nonempty.
Projection Subset Sum for all norms
Before presenting our algorithm for PSSp _p p , we first show how to use the AT-LCSS algorithm of Theorem 4 to find a largest subset whose elements sum to a prescribed target. When applying this theorem to items indexed by I ⊆ [ n ] I\subseteq[n] I ⊆ [ n ] , we denote its output by L L L and include L ( 0 ) = ∣ { i ∈ I : a i = 0 } ∣ L(0)=|\{i\in I:a_i=0\}| L ( 0 ) = ∣ { i ∈ I : a i = 0 } ∣ .
Lemma 24. Given nonnegative integers a 1 , … , a n a_1,\ldots,a_n a 1 , … , a n and an integer t ≥ 0 t\ge0 t ≥ 0 , there is a randomized algorithm that, with probability at least 5 / 6 5/6 5/6 , outputs a largest subset S ⊆ [ n ] S\subseteq[n] S ⊆ [ n ] with Σ ( S ) = t \Sigma(S)=t Σ ( S ) = t , or fail if no such subset exists, in time O ~ ( n + t 3 / 2 ) \widetilde{O}(n+t^{3/2}) O ( n + t 3/2 ) .
Proof. We first describe the algorithm, prove its correctness, bound its failure probability, and analyze its running time.
Description of the algorithm. Start with I = [ n ] I=[n] I = [ n ] and u = t u=t u = t . On a recursive instance ( I , u ) (I,u) ( I , u ) , return { i ∈ I : a i = 0 } \{i\in I:a_i=0\} { i ∈ I : a i = 0 } if u = 0 u=0 u = 0 . For I = { i } I=\{i\} I = { i } and u > 0 u>0 u > 0 , return { i } \{i\} { i } if a i = u a_i=u a i = u , and fail otherwise.
For ∣ I ∣ ≥ 2 |I|\ge2 ∣ I ∣ ≥ 2 and u > 0 u>0 u > 0 , split I = I 1 ⊎ I 2 I=I_1\uplus I_2 I = I 1 ⊎ I 2 with ∣ ∣ I 1 ∣ − ∣ I 2 ∣ ∣ ≤ 1 \lvert|I_1|-|I_2|\rvert\le1 ∣ ∣ I 1 ∣ − ∣ I 2 ∣ ∣ ≤ 1 . For each r ∈ { 1 , 2 } r\in\{1,2\} r ∈ { 1 , 2 } , append one item of value u + 1 u+1 u + 1 to ( a i ) i ∈ I r (a_i)_{i\in I_r} ( a i ) i ∈ I r and solve AT-LCSS with sum cap u u u using Theorem 4 with c = 10 c=10 c = 10 (the addition of the item simply ensures that the number of items in any recursive call is at least 2, hence the failure probability is at most 2 − 10 2^{-10} 2 − 10 ). The added item cannot occur in a subset of sum at most u u u . Execute this algorithm independently R = 2 ⌈ log 2 ( 12 n ) ⌉ + 1 R=2\lceil\log_2(12n)\rceil+1 R = 2 ⌈ log 2 ( 12 n )⌉ + 1 times and let L r ( s ) L_r(s) L r ( s ) be the median of its R R R outputs (across the R R R independent runs) for each s ∈ [ u ] 0 s\in[u]_0 s ∈ [ u ] 0 . Scan s ∈ [ u ] 0 s\in[u]_0 s ∈ [ u ] 0 with L 1 ( s ) ≥ 0 L_1(s)\ge0 L 1 ( s ) ≥ 0 and L 2 ( u − s ) ≥ 0 L_2(u-s)\ge0 L 2 ( u − s ) ≥ 0 , and choose one maximizing L 1 ( s ) + L 2 ( u − s ) L_1(s)+L_2(u-s) L 1 ( s ) + L 2 ( u − s ) . If no such s s s exists, return fail. Apply the same procedure to ( I 1 , s ) (I_1,s) ( I 1 , s ) and ( I 2 , u − s ) (I_2,u-s) ( I 2 , u − s ) . If either invocation returns fail, return fail; otherwise, return the union of the two subsets it outputs.
Correctness. For each half, when a majority of the executions of Theorem 4 return the required cardinalities, the medians equal these cardinalities at every sum. When this holds for both halves, the chosen split gives the largest possible cardinality of a subset of I I I with sum u u u : every such subset splits between the two halves, and conversely any two subsets with these sums have union of sum u u u . Induction on ∣ I ∣ |I| ∣ I ∣ therefore proves the asserted output guarantee when this majority condition holds at every recursive split.
Failure probability. Each execution has at least two input items, so its failure probability is at most 2 − 10 2^{-10} 2 − 10 by Theorem 4. For either half, independence bounds the probability that at least R / 2 R/2 R /2 executions fail by
2 R ( 2 − 10 ) R / 2 = 2 − 4 R ≤ 1 12 n . 2^R(2^{-10})^{R/2}=2^{-4R}\le\frac{1}{12n}. 2 R ( 2 − 10 ) R /2 = 2 − 4 R ≤ 12 n 1 .
This bound holds conditioned on all preceding computations. There are at most 2 ( n − 1 ) 2(n-1) 2 ( n − 1 ) halves on which we compute the medians. A union bound gives total failure probability at most 2 ( n − 1 ) / ( 12 n ) < 1 / 6 2(n-1)/(12n)<1/6 2 ( n − 1 ) / ( 12 n ) < 1/6 .
Running time. At a fixed depth of the recursion, let h h h be the number of recursive instances and let n i n_i n i and t i t_i t i be the size and target of instance i ∈ [ h ] i\in[h] i ∈ [ h ] . Their index sets are disjoint and their targets sum to at most t t t . Handling the base cases, computing the two AT-LCSS arrays for each remaining instance, and scanning its t i + 1 t_i+1 t i + 1 possible splits take a total time at that depth of
O ( ( ∑ i = 1 h n i + ∑ i = 1 h t i 3 / 2 ) polylog ( n + t ) ) ≤ O ( ( n + t 3 / 2 ) polylog ( n + t ) ) . O\left(\left(\sum_{i=1}^{h}n_i+\sum_{i=1}^{h}t_i^{3/2}\right)\operatorname{polylog}(n+t)\right)\le O\left((n+t^{3/2})\operatorname{polylog}(n+t)\right). O ( ( i = 1 ∑ h n i + i = 1 ∑ h t i 3/2 ) polylog ( n + t ) ) ≤ O ( ( n + t 3/2 ) polylog ( n + t ) ) .
Here ∑ i = 1 h n i ≤ n \sum_{i=1}^{h}n_i\le n ∑ i = 1 h n i ≤ n and ∑ i = 1 h t i 3 / 2 ≤ ( ∑ i = 1 h t i ) 3 / 2 ≤ t 3 / 2 \sum_{i=1}^{h}t_i^{3/2}\le\left(\sum_{i=1}^{h}t_i\right)^{3/2}\le t^{3/2} ∑ i = 1 h t i 3/2 ≤ ( ∑ i = 1 h t i ) 3/2 ≤ t 3/2 . There are at most ⌈ log 2 n ⌉ + 1 \lceil\log_2 n\rceil+1 ⌈ log 2 n ⌉ + 1 depths. The R = O ( log ( n + 1 ) ) R=O(\log(n+1)) R = O ( log ( n + 1 )) executions per half and the computation of their medians contribute only to the displayed logarithmic factors. Summing over all depths gives O ~ ( n + t 3 / 2 ) \widetilde{O}(n+t^{3/2}) O ( n + t 3/2 ) total time. ◁
For a nonempty S ⊆ [ n ] S\subseteq[n] S ⊆ [ n ] , define
q S = ⌊ ∣ t − Σ ( S ) ∣ ∣ S ∣ ⌋ , r S = ∣ t − Σ ( S ) ∣ − ∣ S ∣ q S , F p ( S ) = ( ∣ S ∣ − r S ) q S p + r S ( q S + 1 ) p . q_S=\left\lfloor\frac{|t-\Sigma(S)|}{|S|}\right\rfloor,\qquad r_S=|t-\Sigma(S)|-|S|q_S,\qquad F_p(S)=(|S|-r_S)q_S^p+r_S(q_S+1)^p. q S = ⌊ ∣ S ∣ ∣ t − Σ ( S ) ∣ ⌋ , r S = ∣ t − Σ ( S ) ∣ − ∣ S ∣ q S , F p ( S ) = ( ∣ S ∣ − r S ) q S p + r S ( q S + 1 ) p .
For fixed p ≥ 1 p\ge1 p ≥ 1 , evaluating and comparing values of F p F_p F p take constant time in our running-time bounds. The following lemma shows that F p ( S ) F_p(S) F p ( S ) is the minimum value of ∥ Z − X ∥ p p \lVert Z-X\rVert_p^p ∥ Z − X ∥ p p subject to ∑ i ∈ S x i = t \sum_{i\in S}x_i=t ∑ i ∈ S x i = t .
Lemma 25. Fix p ≥ 1 p\ge1 p ≥ 1 . For every nonempty S ⊆ [ n ] S\subseteq[n] S ⊆ [ n ] , the minimum of ∥ Z − X ∥ p p \lVert Z-X\rVert_p^p ∥ Z − X ∥ p p over X ∈ Z n X\in\mathbb{Z}^n X ∈ Z n satisfying ∑ i ∈ S x i = t \sum_{i\in S}x_i=t ∑ i ∈ S x i = t is F p ( S ) F_p(S) F p ( S ) . Let S ⋆ S^\star S ⋆ minimize F p ( S ) F_p(S) F p ( S ) over nonempty S ⊆ [ n ] S\subseteq[n] S ⊆ [ n ] , and choose any R ⊆ S ⋆ R\subseteq S^\star R ⊆ S ⋆ with ∣ R ∣ = r S ⋆ |R|=r_{S^\star} ∣ R ∣ = r S ⋆ . For an integer z z z , sgn ( z ) \operatorname{sgn}(z) sgn ( z ) is 1 1 1 if z > 0 z>0 z > 0 , 0 0 0 if z = 0 z=0 z = 0 , and − 1 -1 − 1 if z < 0 z<0 z < 0 . The vector X = ( x 1 , … , x n ) X=(x_1,\ldots,x_n) X = ( x 1 , … , x n ) defined by
x i = { a i + sgn ( t − Σ ( S ⋆ ) ) ( q S ⋆ + 1 ) , i ∈ R , a i + sgn ( t − Σ ( S ⋆ ) ) q S ⋆ , i ∈ S ⋆ ∖ R , a i , i ∈ [ n ] ∖ S ⋆ x_i=
\begin{cases}
a_i+\operatorname{sgn}(t-\Sigma(S^\star))(q_{S^\star}+1), & i\in R,\\
a_i+\operatorname{sgn}(t-\Sigma(S^\star))q_{S^\star}, & i\in S^\star\setminus R,\\
a_i, & i\in[n]\setminus S^\star
\end{cases} x i = ⎩ ⎨ ⎧ a i + sgn ( t − Σ ( S ⋆ )) ( q S ⋆ + 1 ) , a i + sgn ( t − Σ ( S ⋆ )) q S ⋆ , a i , i ∈ R , i ∈ S ⋆ ∖ R , i ∈ [ n ] ∖ S ⋆ together with S ⋆ S^\star S ⋆ is an optimal solution to P S S p \mathrm{PSS}_p PSS p , of cost F p ( S ⋆ ) 1 / p F_p(S^\star)^{1/p} F p ( S ⋆ ) 1/ p . For a fixed sum Σ ( S ) \Sigma(S) Σ ( S ) , a subset of largest cardinality minimizes F p ( S ) F_p(S) F p ( S ) .
Proof. Fix a nonempty S ⊆ [ n ] S\subseteq[n] S ⊆ [ n ] . Setting x i = a i x_i=a_i x i = a i for i ∈ [ n ] ∖ S i\in[n]\setminus S i ∈ [ n ] ∖ S preserves ∑ i ∈ S x i = t \sum_{i\in S}x_i=t ∑ i ∈ S x i = t and cannot increase ∥ Z − X ∥ p p \lVert Z-X\rVert_p^p ∥ Z − X ∥ p p . Writing b i = x i − a i b_i=x_i-a_i b i = x i − a i for i ∈ S i\in S i ∈ S , we minimize ∑ i ∈ S ∣ b i ∣ p \sum_{i\in S}|b_i|^p ∑ i ∈ S ∣ b i ∣ p over integers satisfying ∑ i ∈ S b i = t − Σ ( S ) \sum_{i\in S}b_i=t-\Sigma(S) ∑ i ∈ S b i = t − Σ ( S ) . For i , j ∈ S i,j\in S i , j ∈ S with b i ≥ b j + 2 b_i\ge b_j+2 b i ≥ b j + 2 , replacing ( b i , b j ) (b_i,b_j) ( b i , b j ) by ( b i − 1 , b j + 1 ) (b_i-1,b_j+1) ( b i − 1 , b j + 1 ) does not increase ∑ i ∈ S ∣ b i ∣ p \sum_{i\in S}|b_i|^p ∑ i ∈ S ∣ b i ∣ p , by convexity of ∣ x ∣ p |x|^p ∣ x ∣ p . Repeating these operations terminates because ∑ i ∈ S b i 2 \sum_{i\in S}b_i^2 ∑ i ∈ S b i 2 strictly decreases. At termination, the integers b i b_i b i differ by at most one. Since their sum is t − Σ ( S ) t-\Sigma(S) t − Σ ( S ) , there are ∣ S ∣ − r S |S|-r_S ∣ S ∣ − r S copies of sgn ( t − Σ ( S ) ) q S \operatorname{sgn}(t-\Sigma(S))q_S sgn ( t − Σ ( S )) q S and r S r_S r S copies of sgn ( t − Σ ( S ) ) ( q S + 1 ) \operatorname{sgn}(t-\Sigma(S))(q_S+1) sgn ( t − Σ ( S )) ( q S + 1 ) . Thus the minimum of ∑ i ∈ S ∣ b i ∣ p \sum_{i\in S}|b_i|^p ∑ i ∈ S ∣ b i ∣ p is F p ( S ) F_p(S) F p ( S ) . The vector X = ( x 1 , … , x n ) X=(x_1,\ldots,x_n) X = ( x 1 , … , x n ) satisfies ∥ Z − X ∥ p p = F p ( S ⋆ ) \lVert Z-X\rVert_p^p=F_p(S^\star) ∥ Z − X ∥ p p = F p ( S ⋆ ) , and the choice of S ⋆ S^\star S ⋆ makes X X X optimal for P S S p \mathrm{PSS}_p PSS p .
For nonempty S 1 , S 2 ⊆ [ n ] S_1,S_2\subseteq[n] S 1 , S 2 ⊆ [ n ] with Σ ( S 1 ) = Σ ( S 2 ) \Sigma(S_1)=\Sigma(S_2) Σ ( S 1 ) = Σ ( S 2 ) and ∣ S 1 ∣ ≤ ∣ S 2 ∣ |S_1|\leq|S_2| ∣ S 1 ∣ ≤ ∣ S 2 ∣ , choose integers b i b_i b i , for i ∈ S 1 i\in S_1 i ∈ S 1 , satisfying ∑ i ∈ S 1 b i = t − Σ ( S 1 ) \sum_{i\in S_1}b_i=t-\Sigma(S_1) ∑ i ∈ S 1 b i = t − Σ ( S 1 ) and ∑ i ∈ S 1 ∣ b i ∣ p = F p ( S 1 ) \sum_{i\in S_1}|b_i|^p=F_p(S_1) ∑ i ∈ S 1 ∣ b i ∣ p = F p ( S 1 ) . Assign these integers to distinct indices of S 2 S_2 S 2 , and assign zero to the remaining indices. Denote the resulting integers by ( c i ) i ∈ S 2 (c_i)_{i\in S_2} ( c i ) i ∈ S 2 . Then ∑ i ∈ S 2 c i = t − Σ ( S 2 ) \sum_{i\in S_2}c_i=t-\Sigma(S_2) ∑ i ∈ S 2 c i = t − Σ ( S 2 ) and ∑ i ∈ S 2 ∣ c i ∣ p = F p ( S 1 ) \sum_{i\in S_2}|c_i|^p=F_p(S_1) ∑ i ∈ S 2 ∣ c i ∣ p = F p ( S 1 ) . Because F p ( S 2 ) F_p(S_2) F p ( S 2 ) is the minimum value of ∥ Z − X ∥ p p \lVert Z-X\rVert_p^p ∥ Z − X ∥ p p among vectors satisfying ∑ i ∈ S 2 x i = t \sum_{i\in S_2}x_i=t ∑ i ∈ S 2 x i = t , we have F p ( S 2 ) ≤ F p ( S 1 ) F_p(S_2)\leq F_p(S_1) F p ( S 2 ) ≤ F p ( S 1 ) , proving the largest-cardinality assertion.
We can now compute an optimal solution by choosing a sum and recovering a largest subset with that sum.
Theorem 5. For fixed p ≥ 1 p\geq1 p ≥ 1 , given nonnegative integers Z = ( a 1 , … , a n ) Z=(a_1,\ldots,a_n) Z = ( a 1 , … , a n ) and an integer t ≥ 0 t\geq0 t ≥ 0 , there is a randomized algorithm that outputs an optimal solution ( X , S ) (X,S) ( X , S ) to P S S p \mathrm{PSS}_p PSS p in time O ~ ( n + t 3 / 2 ) \widetilde{O}(n+t^{3/2}) O ( n + t 3/2 ) , with failure probability at most 1 / 3 1/3 1/3 .
Proof. We first describe the algorithm, prove its correctness, bound its failure probability, and analyze its running time.
Description of the algorithm. Let j 0 ∈ [ n ] j_0\in[n] j 0 ∈ [ n ] satisfy a j 0 = min i = 1 n a i a_{j_0}=\min_{i=1}^n a_i a j 0 = min i = 1 n a i . If a j 0 ≥ t a_{j_0}\geq t a j 0 ≥ t , set x j 0 = t x_{j_0}=t x j 0 = t and x i = a i x_i=a_i x i = a i for i ∈ [ n ] ∖ { j 0 } i\in[n]\setminus\{j_0\} i ∈ [ n ] ∖ { j 0 } , and return ( X , { j 0 } ) (X,\{j_0\}) ( X , { j 0 }) . Otherwise, a j 0 < t a_{j_0}<t a j 0 < t , and we proceed as follows. We solve AT-LCSS on Z Z Z with sum cap 3 t 3t 3 t using Theorem 4 with c = 10 c=10 c = 10 , and denote its output by L L L . Let D = { s ∈ [ 3 t ] 0 : L ( s ) > 0 } D=\{s\in[3t]_0:L(s)>0\} D = { s ∈ [ 3 t ] 0 : L ( s ) > 0 } . For each s ∈ D s\in D s ∈ D , compute
q s = ⌊ ∣ t − s ∣ L ( s ) ⌋ , r s = ∣ t − s ∣ − L ( s ) q s , q_s=\left\lfloor\frac{|t-s|}{L(s)}\right\rfloor,\qquad r_s=|t-s|-L(s)q_s, q s = ⌊ L ( s ) ∣ t − s ∣ ⌋ , r s = ∣ t − s ∣ − L ( s ) q s ,
C ( s ) = ( L ( s ) − r s ) q s p + r s ( q s + 1 ) p . C(s)=(L(s)-r_s)q_s^p+r_s(q_s+1)^p. C ( s ) = ( L ( s ) − r s ) q s p + r s ( q s + 1 ) p .
Choose t ⋆ ∈ D t^\star\in D t ⋆ ∈ D minimizing C ( t ⋆ ) C(t^\star) C ( t ⋆ ) , or return fail if D D D is empty. Apply the algorithm of Lemma 24 to Z Z Z with target t ⋆ t^\star t ⋆ , using independent randomness. If this algorithm returns fail or an empty subset, return fail. Otherwise, let S S S be its output, construct X X X according to Lemma 25, and return ( X , S ) (X,S) ( X , S ) .
Correctness. If a j 0 ≥ t a_{j_0}\geq t a j 0 ≥ t , fix a nonempty S ⊆ [ n ] S\subseteq[n] S ⊆ [ n ] . Then Σ ( S ) ≥ ∣ S ∣ a j 0 ≥ t \Sigma(S)\geq|S|a_{j_0}\geq t Σ ( S ) ≥ ∣ S ∣ a j 0 ≥ t . Applying Jensen’s inequality [21 Appendix B] to the convex function x ↦ ∣ x ∣ p x\mapsto|x|^p x ↦ ∣ x ∣ p , with weights 1 − r S / ∣ S ∣ 1-r_S/|S| 1 − r S /∣ S ∣ and r S / ∣ S ∣ r_S/|S| r S /∣ S ∣ , gives
F p ( S ) ∣ S ∣ = ( 1 − r S ∣ S ∣ ) q S p + r S ∣ S ∣ ( q S + 1 ) p ≥ ( q S + r S ∣ S ∣ ) p = ( Σ ( S ) − t ∣ S ∣ ) p . \frac{F_p(S)}{|S|}=\left(1-\frac{r_S}{|S|}\right)q_S^p+\frac{r_S}{|S|}(q_S+1)^p\geq\left(q_S+\frac{r_S}{|S|}\right)^p=\left(\frac{\Sigma(S)-t}{|S|}\right)^p. ∣ S ∣ F p ( S ) = ( 1 − ∣ S ∣ r S ) q S p + ∣ S ∣ r S ( q S + 1 ) p ≥ ( q S + ∣ S ∣ r S ) p = ( ∣ S ∣ Σ ( S ) − t ) p .
Moreover, since t ≥ 0 t\geq0 t ≥ 0 and ∣ S ∣ ≥ 1 |S|\geq1 ∣ S ∣ ≥ 1 ,
Σ ( S ) − t ∣ S ∣ ≥ a j 0 − t ∣ S ∣ ≥ a j 0 − t ≥ 0. \frac{\Sigma(S)-t}{|S|}\geq a_{j_0}-\frac{t}{|S|}\geq a_{j_0}-t\geq0. ∣ S ∣ Σ ( S ) − t ≥ a j 0 − ∣ S ∣ t ≥ a j 0 − t ≥ 0.
Consequently,
F p ( S ) ≥ ∣ S ∣ ( Σ ( S ) − t ∣ S ∣ ) p ≥ ( a j 0 − t ) p = F p ( { j 0 } ) . F_p(S)\geq|S|\left(\frac{\Sigma(S)-t}{|S|}\right)^p\geq(a_{j_0}-t)^p=F_p(\{j_0\}). F p ( S ) ≥ ∣ S ∣ ( ∣ S ∣ Σ ( S ) − t ) p ≥ ( a j 0 − t ) p = F p ({ j 0 }) .
Thus the singleton { j 0 } \{j_0\} { j 0 } is optimal. It remains to consider a j 0 < t a_{j_0}<t a j 0 < t . We first justify the sum cap 3 t 3t 3 t .
Claim. There is a nonempty subset S ⋆ S^\star S ⋆ minimizing F p F_p F p with Σ ( S ⋆ ) ≤ 3 t \Sigma(S^\star) \le3t Σ ( S ⋆ ) ≤ 3 t .
Proof. Fix S ⊆ [ n ] S \subseteq[n] S ⊆ [ n ] with Σ ( S ) > 3 t \Sigma(S) > 3t Σ ( S ) > 3 t and ∣ S ∣ ≥ 2 |S| \ge2 ∣ S ∣ ≥ 2 . Choose j ∈ S j \in S j ∈ S with a j = max i ∈ S a i a_j = \max_{i \in S} a_i a j = max i ∈ S a i . Choose nonnegative integers b i ∈ { q S , q S + 1 } b_i \in\{q_S, q_S+1\} b i ∈ { q S , q S + 1 } for i ∈ S i \in S i ∈ S , with exactly r S r_S r S indices satisfying b i = q S + 1 b_i = q_S+1 b i = q S + 1 , and with b j = max i ∈ S b i b_j = \max_{i \in S} b_i b j = max i ∈ S b i . By the definitions of q S q_S q S , r S r_S r S , and F p F_p F p ,
∑ i ∈ S b i = Σ ( S ) − t , ∑ i ∈ S b i p = F p ( S ) , b j = ⌈ Σ ( S ) − t ∣ S ∣ ⌉ . \sum_{i \in S} b_i = \Sigma(S)-t,\qquad\sum_{i \in S} b_i^p = F_p(S),\qquad b_j = \left\lceil\frac{\Sigma(S)-t}{|S|} \right\rceil. i ∈ S ∑ b i = Σ ( S ) − t , i ∈ S ∑ b i p = F p ( S ) , b j = ⌈ ∣ S ∣ Σ ( S ) − t ⌉ . Let B = ∑ i ∈ S ∖ { j } b i B = \sum_{i \in S\setminus\{j\}} b_i B = ∑ i ∈ S ∖ { j } b i . Then
B = ∑ i ∈ S ∖ { j } b i = Σ ( S ) − t − ⌈ Σ ( S ) − t ∣ S ∣ ⌉ ≥ ⌊ Σ ( S ) − t 2 ⌋ ≥ t . B = \sum_{i \in S\setminus\{j\}} b_i = \Sigma(S)-t-\left\lceil\frac{\Sigma(S)-t}{|S|}\right\rceil
\ge\left\lfloor\frac{\Sigma(S)-t}{2}\right\rfloor\ge t. B = i ∈ S ∖ { j } ∑ b i = Σ ( S ) − t − ⌈ ∣ S ∣ Σ ( S ) − t ⌉ ≥ ⌊ 2 Σ ( S ) − t ⌋ ≥ t . If Σ ( S ∖ { j } ) ≥ t \Sigma(S\setminus\{j\}) \ge t Σ ( S ∖ { j }) ≥ t , then ∣ t − Σ ( S ∖ { j } ) ∣ = Σ ( S ) − t − a j ≤ B |t-\Sigma(S\setminus\{j\})| = \Sigma(S)-t-a_j \le B ∣ t − Σ ( S ∖ { j }) ∣ = Σ ( S ) − t − a j ≤ B , since a j ≥ ⌈ Σ ( S ) / ∣ S ∣ ⌉ a_j \ge\lceil\Sigma(S)/|S|\rceil a j ≥ ⌈ Σ ( S ) /∣ S ∣ ⌉ . Otherwise, ∣ t − Σ ( S ∖ { j } ) ∣ = t − Σ ( S ∖ { j } ) ≤ t ≤ B |t-\Sigma(S\setminus\{j\})| = t-\Sigma(S\setminus\{j\}) \le t \le B ∣ t − Σ ( S ∖ { j }) ∣ = t − Σ ( S ∖ { j }) ≤ t ≤ B . We can therefore choose integers 0 ≤ c i ≤ b i 0 \le c_i \le b_i 0 ≤ c i ≤ b i for i ∈ S ∖ { j } i \in S\setminus\{j\} i ∈ S ∖ { j } with ∑ i ∈ S ∖ { j } c i = ∣ t − Σ ( S ∖ { j } ) ∣ \sum_{i \in S\setminus\{j\}} c_i = |t-\Sigma(S\setminus\{j\})| ∑ i ∈ S ∖ { j } c i = ∣ t − Σ ( S ∖ { j }) ∣ . Setting x i = a i + sgn ( t − Σ ( S ∖ { j } ) ) c i x_i = a_i+\operatorname{sgn}(t-\Sigma(S\setminus\{j\}))c_i x i = a i + sgn ( t − Σ ( S ∖ { j })) c i for i ∈ S ∖ { j } i \in S\setminus\{j\} i ∈ S ∖ { j } and x i = a i x_i=a_i x i = a i for i ∈ [ n ] ∖ ( S ∖ { j } ) i \in[n]\setminus(S\setminus\{j\}) i ∈ [ n ] ∖ ( S ∖ { j }) gives a feasible pair ( X , S ∖ { j } ) (X,S\setminus\{j\}) ( X , S ∖ { j }) . Since b j > 0 b_j>0 b j > 0 ,
F p ( S ∖ { j } ) ≤ ∑ i ∈ S ∖ { j } c i p < ∑ i ∈ S b i p = F p ( S ) . F_p(S\setminus\{j\}) \le\sum_{i \in S\setminus\{j\}} c_i^p < \sum_{i \in S} b_i^p = F_p(S). F p ( S ∖ { j }) ≤ i ∈ S ∖ { j } ∑ c i p < i ∈ S ∑ b i p = F p ( S ) . For i ∈ [ n ] i \in[n] i ∈ [ n ] with a i > 3 t a_i > 3t a i > 3 t , the singleton { i } \{i\} { i } satisfies F p ( { i } ) = ( a i − t ) p > ( t − a j 0 ) p = F p ( { j 0 } ) F_p(\{i\})=(a_i-t)^p>(t-a_{j_0})^p=F_p(\{j_0\}) F p ({ i }) = ( a i − t ) p > ( t − a j 0 ) p = F p ({ j 0 }) . Thus no subset minimizing F p F_p F p has sum greater than 3 t 3t 3 t .
Now suppose the algorithm of Theorem 4 returns the required cardinalities. Then D D D consists exactly of the sums at most 3 t 3t 3 t attainable by nonempty subsets, and L ( s ) L(s) L ( s ) is the largest cardinality of a subset of sum s s s for every s ∈ D s \in D s ∈ D . Let S ⋆ S^\star S ⋆ minimize F p F_p F p with Σ ( S ⋆ ) ≤ 3 t \Sigma(S^\star) \le3t Σ ( S ⋆ ) ≤ 3 t , as guaranteed by the claim, and write s ⋆ = Σ ( S ⋆ ) s^\star=\Sigma(S^\star) s ⋆ = Σ ( S ⋆ ) . For each s ∈ D s \in D s ∈ D , C ( s ) C(s) C ( s ) is the value of F p F_p F p for any largest subset of sum s s s . By Lemma 25, C ( s ⋆ ) ≤ F p ( S ⋆ ) C(s^\star) \le F_p(S^\star) C ( s ⋆ ) ≤ F p ( S ⋆ ) . Hence
C ( t ⋆ ) ≤ C ( s ⋆ ) ≤ F p ( S ⋆ ) . C(t^\star) \le C(s^\star) \le F_p(S^\star). C ( t ⋆ ) ≤ C ( s ⋆ ) ≤ F p ( S ⋆ ) .
If the algorithm of Lemma 24 also returns a largest subset S S S of sum t ⋆ t^\star t ⋆ , then F p ( S ) = C ( t ⋆ ) F_p(S)=C(t^\star) F p ( S ) = C ( t ⋆ ) . Since S ⋆ S^\star S ⋆ minimizes F p F_p F p over all nonempty subsets, the inequalities above must be equalities. Thus S S S minimizes F p F_p F p , and Lemma 25 guarantees that ( X , S ) (X,S) ( X , S ) is feasible and optimal for P S S p \mathrm{PSS}_p PSS p .
Failure probability. The execution of Theorem 4 fails with probability at most n − 10 n^{-10} n − 10 . Conditioned on its successful return and the selected t ⋆ t^\star t ⋆ , the independent algorithm of Lemma 24 fails with probability at most 1 / 6 1/6 1/6 . The total failure probability is therefore at most n − 10 + 1 / 6 < 1 / 3 n^{-10}+1/6<1/3 n − 10 + 1/6 < 1/3 , since n ≥ 2 n\ge2 n ≥ 2 .
Running time. The algorithm of Theorem 4 with sum cap 3 t 3t 3 t and the algorithm of Lemma 24 with target t ⋆ ≤ 3 t t^\star\le3t t ⋆ ≤ 3 t each take O ~ ( n + t 3 / 2 ) \widetilde{O}(n+t^{3/2}) O ( n + t 3/2 ) time. Computing C ( s ) C(s) C ( s ) for s ∈ D s\in D s ∈ D and writing X X X and S S S take O ( n + t ) O(n+t) O ( n + t ) additional time.
Faster algorithm for Projection Subset Sum on the ℓ 1 \ell_1 ℓ 1 norm
For P S S 1 \mathrm{PSS}_1 PSS 1 , it is in fact enough to find a sum closest to t t t among sums of nonempty subsets. This allows us to solve P S S 1 \mathrm{PSS}_1 PSS 1 in near-linear pseudopolynomial time. First, we recover a subset with that sum using Bringmann’s algorithm [4 ] .
Lemma 26. Given nonnegative integers a 1 , … , a n a_1,\ldots,a_n a 1 , … , a n , an attainable integer sum t ≥ 0 t \ge0 t ≥ 0 , there is a randomized algorithm that returns a subset S ⊆ [ n ] S \subseteq[n] S ⊆ [ n ] with Σ ( S ) = t \Sigma(S)=t Σ ( S ) = t , or fail. It returns fail with probability at most 1 / 6 1/6 1/6 and runs in time O ~ ( n + t ) \widetilde{O}(n+t) O ( n + t ) .
Proof. We first describe the algorithm, prove its correctness, bound its failure probability, and analyze its running time.
Description of the algorithm. We use Bringmann’s algorithm [4 Theorem 1.1] : on positive items with sum cap u > 0 u>0 u > 0 and error parameter η ∈ ( 0 , 1 ) \eta\in(0,1) η ∈ ( 0 , 1 ) , it outputs only attainable sums at most u u u and omits any fixed attainable sum with probability at most η \eta η , in time O ( ( ∣ I ∣ + u ) polylog ( ∣ I ∣ + u + 1 / η ) ) O((|I|+u)\operatorname{polylog}(|I|+u+1/\eta)) O (( ∣ I ∣ + u ) polylog ( ∣ I ∣ + u + 1/ η )) for an input indexed by I I I .
Set η = 1 / ( 12 n ) \eta=1/(12n) η = 1/ ( 12 n ) and start with I = [ n ] I=[n] I = [ n ] and u = t u=t u = t . For u = 0 u=0 u = 0 , return the empty set. If I = { i } I=\{i\} I = { i } and u > 0 u>0 u > 0 , we return { i } \{i\} { i } when a i = u a_i=u a i = u , and fail otherwise. For ∣ I ∣ ≥ 2 |I|\ge2 ∣ I ∣ ≥ 2 and u > 0 u>0 u > 0 , we split I = I 1 ⊎ I 2 I=I_1\uplus I_2 I = I 1 ⊎ I 2 with ∣ ∣ I 1 ∣ − ∣ I 2 ∣ ∣ ≤ 1 ||I_1|-|I_2||\le1 ∣∣ I 1 ∣ − ∣ I 2 ∣∣ ≤ 1 . For each r ∈ { 1 , 2 } r\in\{1,2\} r ∈ { 1 , 2 } , apply Bringmann’s algorithm to ( a i ) i ∈ I r (a_i)_{i\in I_r} ( a i ) i ∈ I r with sum cap u u u and error parameter η \eta η , using randomness independent of all previous executions. Discard items of value zero or greater than u u u before this execution, and let A r A_r A r be its output together with 0 0 0 . If no item remains, set A r = { 0 } A_r=\{0\} A r = { 0 } ; if one item remains, compute its subset sums directly. We find s ∈ [ u ] 0 s\in[u]_0 s ∈ [ u ] 0 with s ∈ A 1 s\in A_1 s ∈ A 1 and u − s ∈ A 2 u-s\in A_2 u − s ∈ A 2 , returning fail if none exists. Apply the same procedure to ( I 1 , s ) (I_1,s) ( I 1 , s ) and ( I 2 , u − s ) (I_2,u-s) ( I 2 , u − s ) . If either invocation returns fail, return fail; otherwise, return the union of the two subsets it outputs.
Correctness. Since A r A_r A r contains only sums of subsets of I r I_r I r , every recursive target is attained by a subset of its recursive index set. Induction on ∣ I ∣ |I| ∣ I ∣ shows that the returned set has sum u u u , since I 1 I_1 I 1 and I 2 I_2 I 2 are disjoint.
Failure probability. Conditioned on the recursion history at ( I , u ) (I,u) ( I , u ) , fix a subset S ⊆ I S\subseteq I S ⊆ I of sum u u u . Bringmann’s algorithm reports both Σ ( S ∩ I 1 ) \Sigma(S\cap I_1) Σ ( S ∩ I 1 ) in A 1 A_1 A 1 and Σ ( S ∩ I 2 ) \Sigma(S\cap I_2) Σ ( S ∩ I 2 ) in A 2 A_2 A 2 with probability at least 1 − 2 η 1-2\eta 1 − 2 η , so the split search succeeds with at least this probability. There are at most n − 1 n-1 n − 1 recursive splits. A union bound over their split searches gives failure probability at most 2 ( n − 1 ) η < 1 / 6 2(n-1)\eta<1/6 2 ( n − 1 ) η < 1/6 .
Running time. At each depth of the recursion, the recursive index sets are disjoint and their targets sum to at most t t t . Executing Bringmann’s algorithm on both halves of each index set and scanning the possible split sums take O ( ( n + t ) polylog ( n + t + 1 / η ) ) O((n+t)\operatorname{polylog}(n+t+1/\eta)) O (( n + t ) polylog ( n + t + 1/ η )) time per depth. There are O ( log ( n + 1 ) ) O(\log(n+1)) O ( log ( n + 1 )) depths. Summing over the depths with 1 / η = 12 n 1/\eta=12n 1/ η = 12 n gives O ~ ( n + t ) \widetilde{O}(n+t) O ( n + t ) total time.
We now use Lemma 26 to output the modified vector and a set whose modified values sum to t t t .
Theorem 27. Given nonnegative integers Z = ( a 1 , … , a n ) Z=(a_1,\ldots,a_n) Z = ( a 1 , … , a n ) and an integer t ≥ 0 t\ge0 t ≥ 0 , there is a randomized algorithm that outputs an optimal solution ( X , S ) (X,S) ( X , S ) to P S S 1 \mathrm{PSS}_1 PSS 1 in time O ~ ( n + t ) \widetilde{O}(n+t) O ( n + t ) , with failure probability at most 1 / 3 1/3 1/3 .
Proof. We first describe the algorithm, prove its correctness, bound its failure probability, and analyze its running time.
Description of the algorithm. Let j 0 ∈ [ n ] j_0 \in[n] j 0 ∈ [ n ] satisfy a j 0 = min i = 1 n a i a_{j_0} = \min_{i=1}^n a_i a j 0 = min i = 1 n a i . If a j 0 ≥ t a_{j_0} \ge t a j 0 ≥ t , set x j 0 = t x_{j_0} = t x j 0 = t and x i = a i x_i = a_i x i = a i for i ∈ [ n ] ∖ { j 0 } i \in[n] \setminus\{j_0\} i ∈ [ n ] ∖ { j 0 } , and return ( X , { j 0 } ) (X, \{j_0\}) ( X , { j 0 }) .
For a j 0 < t a_{j_0} < t a j 0 < t , proceed as follows. Apply Bringmann’s algorithm [4 Theorem 1.1] to the items of Z Z Z with values in [ 2 t ] [2t] [ 2 t ] , with sum cap 2 t 2t 2 t and error parameter 1 / 6 1/6 1/6 . If at most one such item exists, compute its subset sums directly. Let A A A be the resulting set of sums together with a j 0 a_{j_0} a j 0 , delete 0 0 0 if a j 0 > 0 a_{j_0} > 0 a j 0 > 0 , and choose t ⋆ ∈ A t^\star\in A t ⋆ ∈ A minimizing ∣ t − t ⋆ ∣ |t-t^\star| ∣ t − t ⋆ ∣ .
If t ⋆ = 0 t^\star= 0 t ⋆ = 0 , set S = { j 0 } S = \{j_0\} S = { j 0 } ; otherwise, apply the algorithm of Lemma 26 to Z Z Z with target t ⋆ t^\star t ⋆ , using randomness independent of the preceding Bringmann computation. If this algorithm returns fail, return fail; otherwise, let S S S be its output. Choose any j ∈ S j \in S j ∈ S , set x j = a j + t − t ⋆ x_j = a_j + t - t^\star x j = a j + t − t ⋆ and x i = a i x_i = a_i x i = a i for i ∈ [ n ] ∖ { j } i \in[n] \setminus\{j\} i ∈ [ n ] ∖ { j } , and return ( X , S ) (X,S) ( X , S ) .
Correctness. For t > 0 t > 0 t > 0 , any feasible vector X ′ = ( x 1 ′ , … , x n ′ ) X' = (x'_1,\ldots,x'_n) X ′ = ( x 1 ′ , … , x n ′ ) has a nonempty S ′ ⊆ [ n ] S' \subseteq[n] S ′ ⊆ [ n ] with ∑ i ∈ S ′ x i ′ = t \sum_{i \in S'} x'_i = t ∑ i ∈ S ′ x i ′ = t , so
∥ Z − X ′ ∥ 1 ≥ ∣ ∑ i ∈ S ′ ( a i − x i ′ ) ∣ = ∣ Σ ( S ′ ) − t ∣ . \lVert Z-X'\rVert_1 \ge\left|\sum_{i \in S'}(a_i-x'_i)\right| = |\Sigma(S')-t|. ∥ Z − X ′ ∥ 1 ≥ i ∈ S ′ ∑ ( a i − x i ′ ) = ∣Σ ( S ′ ) − t ∣.
If a j 0 ≥ t a_{j_0} \ge t a j 0 ≥ t , every nonempty S ′ S' S ′ has Σ ( S ′ ) ≥ a j 0 \Sigma(S') \ge a_{j_0} Σ ( S ′ ) ≥ a j 0 , so a j 0 − t a_{j_0}-t a j 0 − t is a lower bound on the optimal cost. The returned pair attains this lower bound.
For a j 0 < t a_{j_0} < t a j 0 < t , fix a sum s ⋆ s^\star s ⋆ of a nonempty subset minimizing ∣ t − s ⋆ ∣ |t-s^\star| ∣ t − s ⋆ ∣ , independently of the algorithm’s randomness. Since Σ ( { j 0 } ) = a j 0 \Sigma(\{j_0\})=a_{j_0} Σ ({ j 0 }) = a j 0 and t − a j 0 ≤ t t-a_{j_0} \le t t − a j 0 ≤ t , whereas ∣ t − s ∣ > t |t-s|>t ∣ t − s ∣ > t for s > 2 t s>2t s > 2 t , we have s ⋆ ≤ 2 t s^\star\le2t s ⋆ ≤ 2 t . The set A A A is nonempty and contains only sums attained by nonempty subsets. If s ⋆ ∈ A s^\star\in A s ⋆ ∈ A , the chosen t ⋆ t^\star t ⋆ is closest to t t t among sums of nonempty subsets. When the algorithm of Lemma 26 returns a subset S S S of sum t ⋆ t^\star t ⋆ , or t ⋆ = 0 t^\star=0 t ⋆ = 0 and S = { j 0 } S=\{j_0\} S = { j 0 } , the returned vector and S S S are feasible and have cost ∣ t − t ⋆ ∣ |t-t^\star| ∣ t − t ⋆ ∣ . Whenever t ⋆ t^\star t ⋆ is closest to t t t among sums of nonempty subsets, this cost attains the lower bound on ∥ Z − X ′ ∥ 1 \lVert Z-X'\rVert_1 ∥ Z − X ′ ∥ 1 for every feasible vector X ′ X' X ′ .
Failure probability. The probability that s ⋆ s^\star s ⋆ is omitted is at most 1 / 6 1/6 1/6 . For t ⋆ > 0 t^\star>0 t ⋆ > 0 , conditioned on the selected t ⋆ t^\star t ⋆ , the independent algorithm of Lemma 26 returns fail with probability at most 1 / 6 1/6 1/6 . Hence an optimal pair ( X , S ) (X,S) ( X , S ) is returned with probability at least 2 / 3 2/3 2/3 .
Running time. Since t ⋆ ≤ 2 t t^\star\le2t t ⋆ ≤ 2 t , Bringmann’s algorithm with sum cap 2 t 2t 2 t and the algorithm of Lemma 26 with target t ⋆ t^\star t ⋆ each take O ~ ( n + t ) \widetilde{O}(n+t) O ( n + t ) time, and the scan of A A A and construction of X X X and S S S take O ( n + t ) O(n+t) O ( n + t ) time. ◀
Deferred Lemmas and Proofs
Fast convolution of sets of pairs
For finite sets P , Q ⊆ Z 2 P,Q \subseteq\mathbb{Z}^{2} P , Q ⊆ Z 2 , we show how to compute their convolution P ⊕ Q P \oplus Q P ⊕ Q efficiently.
Lemma 28. Let P , Q ⊆ { − M 1 , … , M 1 } × { − M 2 , … , M 2 } P,Q \subseteq\{-M_1,\ldots,M_1\}\times\{-M_2,\ldots,M_2\} P , Q ⊆ { − M 1 , … , M 1 } × { − M 2 , … , M 2 } , then P ⊕ Q P\oplus Q P ⊕ Q can be computed in time O ~ ( M 1 M 2 ) \widetilde{O}(M_1M_2) O ( M 1 M 2 ) .
Proof. Set b = 4 M 1 + 1 b=4M_1+1 b = 4 M 1 + 1 and represent a pair ( x , y ) (x,y) ( x , y ) in either input set by the exponent ( x + M 1 ) + b ( y + M 2 ) (x+M_1)+b(y+M_2) ( x + M 1 ) + b ( y + M 2 ) . We form a polynomial for each set whose coefficients indicate the represented pairs, then compute their product by exact integer convolution (for example, using fast Fourier transform; see [8 ] ). The exponent contributed by a pair ( x , y ) ∈ P ⊕ Q (x,y)\in P\oplus Q ( x , y ) ∈ P ⊕ Q is ( x + 2 M 1 ) + b ( y + 2 M 2 ) (x+2M_1)+b(y+2M_2) ( x + 2 M 1 ) + b ( y + 2 M 2 ) . Since 0 ≤ x + 2 M 1 ≤ 4 M 1 < b 0\leq x+2M_1\leq4M_1<b 0 ≤ x + 2 M 1 ≤ 4 M 1 < b , there is no carry between the coordinates. Thus a coefficient of the product is positive exactly when its exponent represents a pair in P ⊕ Q P\oplus Q P ⊕ Q . The polynomials and their product have degree O ( M 1 M 2 ) O(M_1M_2) O ( M 1 M 2 ) , and each coefficient of the product is at most ∣ P ∣ ∣ Q ∣ = O ( M 1 2 M 2 2 ) |P||Q|=O(M_1^2M_2^2) ∣ P ∣∣ Q ∣ = O ( M 1 2 M 2 2 ) . Exact fast convolution and a scan of the coefficients therefore take O ~ ( M 1 M 2 ) \widetilde{O}(M_1M_2) O ( M 1 M 2 ) time. ◀
Proof of Lemma 6
We recall Lemma 6 from [2 ] and prove it.
Lemma 6. Given nonnegative integers a 1 , … , a n a_1,\ldots,a_n a 1 , … , a n , positive integers k k k and t t t , and a parameter δ ∈ ( 0 , 1 ) \delta\in(0,1) δ ∈ ( 0 , 1 ) , there is a randomized algorithm with one-sided error probability at most δ \delta δ that computes the set { ( Σ ( S ) , ∣ S ∣ ) : S ⊆ { 1 , … , n } , ∣ S ∣ ≤ k , Σ ( S ) ≤ t } \{(\Sigma(S),|S|):S\subseteq\{1,\ldots,n\},\ |S|\leq k,\ \Sigma(S)\leq t\} {( Σ ( S ) , ∣ S ∣ ) : S ⊆ { 1 , … , n } , ∣ S ∣ ≤ k , Σ ( S ) ≤ t } in time
O ( ( n + k t ) polylog ( n + k t + 1 / δ ) ) . O((n+kt)\operatorname{polylog}(n+kt+1/\delta)). O (( n + k t ) polylog ( n + k t + 1/ δ )) . The returned set is always a subset of { ( Σ ( S ) , ∣ S ∣ ) : S ⊆ { 1 , … , n } , ∣ S ∣ ≤ k , Σ ( S ) ≤ t } \{(\Sigma(S),|S|):S\subseteq\{1,\ldots,n\},\ |S|\leq k,\ \Sigma(S)\leq t\} {( Σ ( S ) , ∣ S ∣ ) : S ⊆ { 1 , … , n } , ∣ S ∣ ≤ k , Σ ( S ) ≤ t } .
Proof. The proof follows the generating-function approach of Jin and Wu [14 ] , with an additional variable recording the cardinality. The algorithm computes coefficients modulo a random prime. A zero integer coefficient is never reported as nonzero, but a positive coefficient may vanish modulo the chosen prime. We bound this one-sided error below.
Since the a i a_i a i are nonnegative, an index i i i with a i > t a_i>t a i > t cannot belong to any subset S S S with Σ ( S ) ≤ t \Sigma(S)\leq t Σ ( S ) ≤ t , and we henceforth disregard such indices. Let z : = ∣ { i ∈ [ n ] : a i = 0 } ∣ z:=|\{i\in[n]:a_i=0\}| z := ∣ { i ∈ [ n ] : a i = 0 } ∣ .
For a bivariate formal power series F ( x , y ) F(x,y) F ( x , y ) , let
[ x s y r ] F ( x , y ) [x^s y^r]F(x,y) [ x s y r ] F ( x , y )
denote the coefficient of the monomial x s y r x^s y^r x s y r . We also define its ( t , k ) (t,k) ( t , k ) -truncation by
tr t , k ( F ) : = ∑ s = 0 t ∑ r = 0 k [ x s y r ] F ( x , y ) x s y r . \operatorname{tr}_{t,k}(F):=\sum_{s=0}^{t}\sum_{r=0}^{k}[x^s y^r]F(x,y)x^s y^r. tr t , k ( F ) := s = 0 ∑ t r = 0 ∑ k [ x s y r ] F ( x , y ) x s y r .
Thus, after every polynomial operation, terms of x x x -degree larger than t t t or y y y -degree larger than k k k may be discarded.
Consider the following bivariate generating function defined by
A ( x , y ) : = ∏ i = 1 n ( 1 + y x a i ) . A(x,y):=\prod_{i=1}^{n}(1+yx^{a_i}). A ( x , y ) := i = 1 ∏ n ( 1 + y x a i ) .
The subset sum instance has a solution for sum s s s of cardinality k k k if and only if [ x s y r ] A ( x , y ) ≠ 0 [x^s y^r]A(x,y)\ne0 [ x s y r ] A ( x , y ) = 0 . Formally, we have for every 0 ≤ s ≤ t 0\leq s\leq t 0 ≤ s ≤ t and 0 ≤ r ≤ k 0\leq r\leq k 0 ≤ r ≤ k ,
[ x s y r ] A ( x , y ) = ∣ { S ⊆ [ n ] : Σ ( S ) = s , ∣ S ∣ = r } ∣ . [x^s y^r]A(x,y)=|\{S\subseteq[n]:\Sigma(S)=s,\ |S|=r\}|. [ x s y r ] A ( x , y ) = ∣ { S ⊆ [ n ] : Σ ( S ) = s , ∣ S ∣ = r } ∣.
We first explain how to compute these coefficients modulo a prime. Let p > max { t , k } p > \max\{t,k\} p > max { t , k } be a prime, and let F p \mathbb{F}_p F p denote the residue ring modulo p p p . All computations in the remainder of this paragraph are over F p \mathbb{F}_p F p . Let
B ( x , y ) : = ln ( ∏ i : a i > 0 ( 1 + y x a i ) ) ∈ Q [ [ x , y ] ] . B(x,y) := \ln\left(\prod_{i:a_i>0}(1+yx^{a_i})\right) \in\mathbb{Q}[[x,y]]. B ( x , y ) := ln ( i : a i > 0 ∏ ( 1 + y x a i ) ) ∈ Q [[ x , y ]] .
Proposition 29. For prime p > max { k , t } p > \max\{k,t\} p > max { k , t } , in O ~ ( n + t ) \widetilde{O}(n+t) O ( n + t ) time one can compute tr t , k ( B ) \operatorname{tr}_{t,k}(B) tr t , k ( B ) .
Proof. The zero-valued items contribute the factor ( 1 + y ) z (1+y)^z ( 1 + y ) z to A ( x , y ) A(x,y) A ( x , y ) . Over the rational formal power series, using
ln ( 1 + v ) = ∑ j ≥ 1 ( − 1 ) j − 1 j v j , \ln(1+v)=\sum_{j\geq1}\frac{(-1)^{j-1}}{j}v^j, ln ( 1 + v ) = j ≥ 1 ∑ j ( − 1 ) j − 1 v j , we have
B ( x , y ) = ∑ i : a i > 0 ln ( 1 + y x a i ) = ∑ i : a i > 0 ∑ j ≥ 1 ( − 1 ) j − 1 j y j x j a i . B(x,y)=\sum_{i:a_i>0}\ln(1+yx^{a_i})
=\sum_{i:a_i>0}\sum_{j\geq1}\frac{(-1)^{j-1}}{j}y^jx^{ja_i}. B ( x , y ) = i : a i > 0 ∑ ln ( 1 + y x a i ) = i : a i > 0 ∑ j ≥ 1 ∑ j ( − 1 ) j − 1 y j x j a i . For every a ∈ [ t ] a\in[t] a ∈ [ t ] , let
m a : = ∣ { i ∈ [ n ] : a i = a } ∣ m_a:=|\{i\in[n]:a_i=a\}| m a := ∣ { i ∈ [ n ] : a i = a } ∣ be the multiplicity of a a a among the input integers. Truncating (2) gives
tr t , k ( B ) = ∑ a = 1 t ∑ j = 1 min { k , ⌊ t / a ⌋ } m a ( − 1 ) j − 1 j x a j y j . \operatorname{tr}_{t,k}(B)=\sum_{a=1}^{t}\sum_{j=1}^{\min\{k,\lfloor t/a\rfloor\}}m_a\frac{(-1)^{j-1}}{j}x^{aj}y^j. tr t , k ( B ) = a = 1 ∑ t j = 1 ∑ m i n { k , ⌊ t / a ⌋} m a j ( − 1 ) j − 1 x aj y j . Every denominator occurring in (3) satisfies 1 ≤ j ≤ t 1\leq j\leq t 1 ≤ j ≤ t , and is therefore invertible modulo p p p . Hence (3) also unambiguously defines tr t , k ( B ) \operatorname{tr}_{t,k}(B) tr t , k ( B ) over F p \mathbb{F}_p F p .
The multiplicities m 1 , … , m t m_1,\ldots,m_t m 1 , … , m t can be computed in O ( n + t ) O(n+t) O ( n + t ) time. Moreover, the number of pairs ( a , j ) (a,j) ( a , j ) occurring in (3) is at most
∑ a = 1 t ⌊ t a ⌋ = O ( t ln t ) . \sum_{a=1}^{t}\left\lfloor\frac{t}{a}\right\rfloor=O(t\ln t). a = 1 ∑ t ⌊ a t ⌋ = O ( t ln t ) . Thus all coefficients of tr t , k ( B ) \operatorname{tr}_{t,k}(B) tr t , k ( B ) can be computed in O ~ ( n + t ) \widetilde{O}(n+t) O ( n + t ) time.
It remains to recover
tr t , k ( A ) = tr t , k ( ( 1 + y ) z exp ( B ) ) . \operatorname{tr}_{t,k}(A)=\operatorname{tr}_{t,k}\left((1+y)^z\exp(B)\right). tr t , k ( A ) = tr t , k ( ( 1 + y ) z exp ( B ) ) .
We use the divide-and-conquer computation of the exponential formal power series of Jin and Wu [14 Lemma 2] ; for completeness, we verify that adding the cardinality variable increases its running time by only a factor k k k .
Proposition 30. Given a polynomial f ( x , y ) ∈ x F p [ x , y ] f(x,y)\in x\mathbb{F}_p[x,y] f ( x , y ) ∈ x F p [ x , y ] of degree at most t t t (< p <p < p ), one can compute tr t , k ( ( 1 + y ) z exp ( f ) ) \operatorname{tr}_{t,k}((1+y)^z\exp(f)) tr t , k (( 1 + y ) z exp ( f )) in time O ~ ( t k ) \widetilde{O}(tk) O ( t k ) .
Proof. We write
f ( x , y ) = ∑ d = 1 t f d ( y ) x d f(x,y)=\sum_{d=1}^{t} f_d(y)x^d f ( x , y ) = d = 1 ∑ t f d ( y ) x d
for the polynomial tr t , k ( B ) \operatorname{tr}_{t,k}(B) tr t , k ( B ) , where every f d ( y ) f_d(y) f d ( y ) has degree at most k k k , and we write
g ( x , y ) = tr t , k ( ( 1 + y ) z exp ( f ( x , y ) ) ) = ∑ i = 0 t g i ( y ) x i g(x,y)=\operatorname{tr}_{t,k}\left((1+y)^z\exp(f(x,y))\right)=\sum_{i=0}^{t}g_i(y)x^i g ( x , y ) = tr t , k ( ( 1 + y ) z exp ( f ( x , y )) ) = i = 0 ∑ t g i ( y ) x i
where every g i ( y ) g_i(y) g i ( y ) is taken modulo y k + 1 y^{k+1} y k + 1 . Since f f f has zero x x x -constant term, g 0 ( y ) = ( 1 + y ) z m o d y k + 1 g_0(y)=(1+y)^z\bmod y^{k+1} g 0 ( y ) = ( 1 + y ) z mod y k + 1 . Its coefficients ( z j ) \binom{z}{j} ( j z ) , for 0 ≤ j ≤ k 0\leq j\leq k 0 ≤ j ≤ k , are computed in O ( k ) O(k) O ( k ) field operations using p > k p>k p > k . The identity
∂ g ∂ x = g ∂ f ∂ x \frac{\partial g}{\partial x}=g\frac{\partial f}{\partial x} ∂ x ∂ g = g ∂ x ∂ f
implies, by comparing coefficients of x i − 1 x^{i-1} x i − 1 , that for every 1 ≤ i ≤ t 1\leq i\leq t 1 ≤ i ≤ t ,
i g i ( y ) = ∑ j = 0 i − 1 ( i − j ) f i − j ( y ) g j ( y ) ( m o d y k + 1 ) . i g_i(y)=\sum_{j=0}^{i-1}(i-j)f_{i-j}(y)g_j(y)\pmod{y^{k+1}}. i g i ( y ) = j = 0 ∑ i − 1 ( i − j ) f i − j ( y ) g j ( y ) ( mod y k + 1 ) .
Since p > t p>t p > t , the integer i i i is invertible in F p \mathbb{F}_p F p for every i ∈ [ t ] i\in[t] i ∈ [ t ] , so (4), together with g 0 ( y ) = ( 1 + y ) z m o d y k + 1 g_0(y)=(1+y)^z\bmod y^{k+1} g 0 ( y ) = ( 1 + y ) z mod y k + 1 , uniquely determines $g_1(y),\ldots,g_t(y).
We compute the recurrence (4) by divide and conquer on the index interval [ 0 , t ] [0,t] [ 0 , t ] , exactly as in the univariate algorithm of Jin and Wu [14 Lemma 2] . We consider one recursive interval [ ℓ , r ] [\ell,r] [ ℓ , r ] , and let
q : = ⌊ ℓ + r 2 ⌋ . q:=\left\lfloor\frac{\ell+r}{2}\right\rfloor. q := ⌊ 2 ℓ + r ⌋ .
and we suppose that g ℓ ( y ) , … , g q ( y ) g_\ell(y),\ldots,g_q(y) g ℓ ( y ) , … , g q ( y ) have already been computed. Their total contribution to the right-hand coefficients g q + 1 ( y ) , … , g r ( y ) g_{q+1}(y),\ldots,g_r(y) g q + 1 ( y ) , … , g r ( y ) in (4) is obtained from the product
( ∑ d = 1 r − ℓ d f d ( y ) z d ) ( ∑ j = ℓ q g j ( y ) z j − ℓ ) . \left(\sum_{d=1}^{r-\ell}d f_d(y)z^d\right)\left(\sum_{j=\ell}^{q}g_j(y)z^{j-\ell}\right). ( d = 1 ∑ r − ℓ d f d ( y ) z d ) j = ℓ ∑ q g j ( y ) z j − ℓ .
Indeed, for every q < i ≤ r q<i\leq r q < i ≤ r , the coefficient of z i − ℓ z^{i-\ell} z i − ℓ in (5) is
∑ j = ℓ q ( i − j ) f i − j ( y ) g j ( y ) , \sum_{j=\ell}^{q}(i-j)f_{i-j}(y)g_j(y), j = ℓ ∑ q ( i − j ) f i − j ( y ) g j ( y ) ,
which is exactly the part of the right-hand side of (4) contributed by indices j ∈ [ ℓ , q ] j\in[\ell,q] j ∈ [ ℓ , q ] .
We next bound the time needed to compute (5). We suppose the length of the recursive interval is
L : = r − ℓ + 1. L:=r-\ell+1. L := r − ℓ + 1.
Both factors in (5) have z z z -degree O ( L ) O(L) O ( L ) and y y y -degree at most k k k . Their product, truncated modulo y k + 1 y^{k+1} y k + 1 , can be computed in O ~ ( L k ) \widetilde{O}(Lk) O ( L k ) field operations. To see this using only univariate polynomial multiplication, substitute
z ⟼ Z 2 k + 1 , y ⟼ Z . z\longmapsto Z^{2k+1},\qquad y\longmapsto Z. z ⟼ Z 2 k + 1 , y ⟼ Z .
Before truncation, the product of the two factors has y y y -degree at most 2 k 2k 2 k . Therefore different monomials z a y b z^a y^b z a y b , with 0 ≤ b ≤ 2 k 0 \le b \le2k 0 ≤ b ≤ 2 k , are mapped to different powers
Z ( 2 k + 1 ) a + b . Z^{(2k+1)a+b}. Z ( 2 k + 1 ) a + b .
Hence one multiplication of two univariate polynomials of degree O ( L k ) O(Lk) O ( L k ) suffices to compute (5), after which the coefficients of y y y -degree larger than k k k are discarded. Fast univariate polynomial multiplication takes O ~ ( L k ) \widetilde{O}(Lk) O ( L k ) time (cf. [14 Lemma 1] ).
At every fixed depth of the divide-and-conquer recursion, the recursive intervals are disjoint, and their lengths sum to O ( t ) O(t) O ( t ) . Consequently, the total cost of all polynomial multiplications at one recursion depth is O ~ ( t k ) \widetilde{O}(tk) O ( t k ) . There are O ( ln t ) O(\ln t) O ( ln t ) recursion depths, and all other operations require at most the same amount of time. Thus
tr t , k ( ( 1 + y ) z exp ( f ) ) \operatorname{tr}_{t,k}((1+y)^z\exp(f)) tr t , k (( 1 + y ) z exp ( f ))
is computed in
O ~ ( t k ) \widetilde{O}(tk) O ( t k )
time.
Proposition 31. For prime p > max { k , t } p > \max\{k,t\} p > max { k , t } , one can compute [ x s y r ] A ( x , y ) m o d p [x^s y^r]A(x,y)\bmod p [ x s y r ] A ( x , y ) mod p for all 0 ≤ s ≤ t 0 \le s \le t 0 ≤ s ≤ t and 0 ≤ r ≤ k 0 \le r \le k 0 ≤ r ≤ k in time O ~ ( n + t k ) \widetilde{O}(n+tk) O ( n + t k ) time.
Proof. Firstly, we use Proposition 29 to compute [ x s y r ] B ( x , y ) m o d p [x^s y^r]B(x,y)\bmod p [ x s y r ] B ( x , y ) mod p for all 0 ≤ s ≤ t 0 \le s \le t 0 ≤ s ≤ t and 0 ≤ r ≤ k 0 \le r \le k 0 ≤ r ≤ k in time O ~ ( n + t ) \widetilde{O}(n+t) O ( n + t ) time, and then we use Proposition 30 to compute tr t , k ( ( 1 + y ) z exp ( B ) ) \operatorname{tr}_{t,k}((1+y)^z\exp(B)) tr t , k (( 1 + y ) z exp ( B )) in O ~ ( t k ) \widetilde{O}(tk) O ( t k ) time. And, over the rational formal power series, we have
A ( x , y ) = ( 1 + y ) z exp ( B ( x , y ) ) . A(x,y)=(1+y)^z\exp(B(x,y)). A ( x , y ) = ( 1 + y ) z exp ( B ( x , y )) . This completes the proof.
It remains only to ensure that reduction modulo p p p does not turn a positive coefficient in (1) into zero. We use the same random-prime argument as Jin and Wu [14 Lemma 3,Theorem 6] , with a union bound over all pairs ( s , r ) (s,r) ( s , r ) we have the following
Corollary 32. Let M = n + ( k + 1 ) t + ⌈ 1 / η ⌉ M=n+(k+1)t+\lceil1/\eta\rceil M = n + ( k + 1 ) t + ⌈ 1/ η ⌉ where η ∈ { 0 , 1 } \eta\in\{0,1\} η ∈ { 0 , 1 } and p p p be a uniform random prime from [ M 4 , 2 M 4 ] [M^4,2M^4] [ M 4 , 2 M 4 ] . Then
Pr [ p ∣ [ x s y r ] A ( x , y ) for some ( s , r ) with [ x s y r ] A ( x , y ) ≠ 0 ] ≤ η . \Pr[p\mid[x^s y^r]A(x,y)\text{ for some }(s,r)\text{ with }[x^s y^r]A(x,y)\ne0]\le\eta. Pr [ p ∣ [ x s y r ] A ( x , y ) for some ( s , r ) with [ x s y r ] A ( x , y ) = 0 ] ≤ η .
By sampling and using Miller-Rabin primality testing [8 ] , we can pick a uniform random prime p p p from interval [ M 4 , 2 M 4 ] [M^4,2M^4] [ M 4 , 2 M 4 ] in poly log ( M ) \operatorname{poly}\log(M) poly log ( M ) time with O ( M − 1 ) O(M^{-1}) O ( M − 1 ) failure probability. Then the lemma immediately follows from Corollary 32 and Proposition 31.