Uary Subset-Sum is i Logspace arxiv:1012.1336v2 [cs.cc] 8 Dec 2010 1 Itroductio Daiel M. Kae December 9, 2010 I this paper we cosider the Uary Subset-Sum problem which is defied as follows: Give itegers m 1,...,m ad B (writte i uary), we defie the subset sum problem to be that of determiig whether or ot there exists a S [] so that i S m i = B (ote that for this problem the m i are ofte assumed to be o-egative). Let C = B + x i + 1. This problem ca be solved usig a stadard dyamic program usig space O(C) ad time O(C). The dyamic program makes fudametal use of this large space ad it is iterestig to ask whether this requiremet ca be removed. Uary Subset- Sum has bee studied i small-space models of computatio as early as 1980 i [4], where they showed that it was i NL. Sice the the problem was studied i [2], where Cho ad Huyh devised a complexity class betwee L ad NL that cotaied Uary Subset-Sum as supportig evidece that it is ot N L-complete. This problem was listed agai i [1] claimig it to be a ope problem as to whether or ot it is i L. It was recetly show i [3] that this problem was i Logspace. Ufortuately, their algorithm is somewhat complicated. We provide a simple algorithm solvig this problem i Logspace. 2 Our Algorithm The basic idea of our algorithm will be to make use of the geeratig fuctio (1+xmi ) = S [] x i S mi to compute the umber of solutios to our problem modulo p for a umber of differet primes p (we show how to do this i Lemma 1). Pseudocode for our algorithm is follows: 1
c := 0 p := NextPrime(C) While(c ) If x B ) 0 (mod p) (1+xmi Retur True c := c+ log 2 (p) p := NextPrime(p) Ed While Retur False 2.1 Space Complexity There are several thigs that must be oted to show that this algorithm rus i logspace. First we claim that p is ever more tha polyomial i size. This is because stadard facts about prime umbers imply that there are at least primes betwee C ad poly(c,), ad each of these primes causes c to icrease by at least 1. We also ote that x B (1 + xmi ) ca be computed modulo p i Logspace. This is because we ca just keep track of the value of x ad the curret ruig total (modulo p) alog with the space ecessary to compute the ext term. The product is computed agai by keepig track of i ad the curret ruig product (modulo p) ad whatever is ecessary to compute the ext term. The expoets are computed i the obvious way. Fially primality testig of poly-sized umbers ca be doe by repeated trial divisios i Logspace, ad hece the NextPrime fuctio ca also be computed i Logspace. 2.2 Correctess We ow have to prove correctess of the algorithm. Let A be the umber of subsets S [] so that i S m i = B. Lemma 1. For p a prime umber, p > C. The x B (1+x mi ) A (mod p). Where agai A is the umber of subsets S [] so that i S m i = B. Proof. Note that x B (1+x mi ) = x i S mi B. S [] The idea of our proof will be to iterchage the order of summatio ad show that the terms for which i S m i B cacel out. 2
Notice that each expoet i this sum has absolute value less tha p 1. Iterchagig the sums o the right had side, we fid that We ote that: x B (1+x mi ) = x i S mi B. x k (mod p) S [] { 1 if k 0 (mod ). 0 else If k is a multiple of p 1, the all terms i the sum are 1 modulo p ad the result follows. Otherwise, we let g be a primitive root mod p ad ote that istead of summig over x = 1 to p 1 we may sum over x = g l for l = 0 to p 2. The Hece p 2 x k l=0 g kl 1 gk() 1 g k x B (1+x mi ) = x i S mi B S [] 1 1 1 g k 0. S [] i S xi B (mod ) SiceislargerthaC, i S x i B (mod )ifadolyif i S x i = B. Hece this sum cotributes -1 for each such S ad so the fial sum is A. We are ow ready to prove correctess. If (1 + ) 0 xmi (mod p) for some p > C, the by our Lemma, this meas that A 0 (mod p). I particular, this meas that A 0, ad that therefore there is some such S. Cosider a iteger d which is equal to the product of the primes p that have bee checked so far. The d is a product of distict primes p so that A x B ) 0 (mod p). Therefore d A. Furthermore it (1+xmi is the case that d 2 c. It is clear from the defiitio of A that 0 A 2. Thereforeif c >, d > 2 ad d A, which implies that A = 0, ad that therefore there are o solutios. Hece our algorithm always outputs correctly. 3 Extesios x B There are some relatively simple extesios of this algorithm. For oe thig, our algorithm does more tha tell us whether or ot A is equal to 0, but also tells us cogruetial iformatio about A. We ca i fact obtai more refied cogruetial iformatio tha is apparet from our Lemma. We ca also use this alog with the Chiese Remaider Theorem to compute a umerical approximatio of A. Fially a slight geeralizatio of these techiques allows us to work with m i vector valued rather tha iteger-valued. 1. 3
3.1 Computig Cogrueces We show above how to compute A modulo p for p a prime larger tha C. But i fact if p is ay prime ad k > 1 ay iteger, A ca be computed modulo p k i O(log(p k )) space. If p > C, the we have that A 1 x B (1+x mi ) (mod p). O the other had if p C, the above expressio will oly cout the umber of subsets that give the correct sum modulo p 1. We ca fix this by lettig q = p l for some iteger l so that q > C. The for the same reasos that the above is true, it will be the case that A 1 q 1 x B (1+x mi ) (mod p). x F q Where F q is the fiite field of order q. If we have k > 1 ad p > C we ote that agai for the same reasos A 1 x µ x B (1+x mi ) (mod p k ). Where µ is the set of () st roots of uity i Z/p k. This computatio ca be performed without difficulty i Z/p k. We agai ru ito difficulty if p < C. We ca let r = ()p l for some iteger l so that r > C. It will the be the case that ra x B (1+x mi ) (mod p k+l ). x µ r The right had side ca easily be computed i Z/p k+l, ad dividig by r gives A (mod p k ). 3.2 Approximatig the Number of Solutios It is also possible i Logspace to approximate the umber of solutios, A, computig logarithmically may sigificat bits. This ca be doe usig the Chiese Remaider Theorem. Suppose that p 1,...,p k are distict primes. By the above we ca compute A modulo p i for each i. Let N = k p i, ad N i = N p i. The Chiese Remaider Theorem tells us that A k N i (A (mod p i )) ( N 1 i (mod p i ) ) (mod N). 4
Or i other words, A k ( 1 N p i ) (A (mod p i )) ( N 1 i (mod p i ) ) (mod 1). Now we ca compute A modulo p i by the above. We ca also compute N 1 i j i j (mod p i ). Hece we ca compute each term i the sum to logarithmically may bits. Hece i logspace we ca compute A N (mod 1) to logarithmically may bits of precisio. If 2A > N > A, this allows us to compute logarithmically may sigificat bits of A. We ca fid such a N by startig with a N > 2 A ad repeatedly tryig N at least half as big as the previous N util N < 2A (we ca fid our ext N by either removig the prime 2 from N or replacig the smallest prime dividig N by oe at least half as big (which exists by Bertrad s postulate)). It should also be oted that this ability to approximately cout solutios i Logspace allows us to approximately uiformly sample from the space of solutios i Radomized Logspace. This is doe by decidig whether or ot each elemet is i S oe-by-oe ad puttig it i with probability early equal to the proportio of the remaiig solutios that have that elemet i S. 3.3 Vector-Valued Iputs We cosider the slightly modified subset sum problem where ow m i ad B lie i Z k, ad agai we wish to determie whether or ot there exists ad S so that i S m i = B. If we let C be oe more tha the sum of the absolute values of the coefficiets of the m i plus the absolute values of the coefficiets of B, a slight modificatio of our algorithm allows us to solve this problem i O(klog(C)) space ad C O(k) time (i particular if k = O(1), this rus i O(log(C)) space ad C O(1) time). There are two ways to do this. Oe is simply to treat our vectors as base C-expasios of itegers ad reduce this to our previous algorithm. Aother techique ivolves a slight geeralizatio of our Lemma. I either case we let m i = (m i,1,...,m i,k ), B = (B 1,...,B k ). For the firstalgorithm, welet m i = k j=1 Cj 1 m i,j ad B = k j=1 Cj 1 B j. We claim that for ay S [] that i S m i = B if ad oly if i S m i = B, thus reducig this to a istace of our origial problem. The claim holds because m i B = i S k j=1c j 1( i S m i,j B j ) = k C j 1 e j. Sice the e j are all itegers of absolute value less tha C, this sum is 0 if ad oly if, each of the e j are 0. Hece i S m i = B if ad oly if i S m i = B. j=1 5
Aother way to do this is by geeralizig our Lemma. I particular it ca be show usig similar techiques that if A is the umber of subsets S that work, ad if p is a prime bigger tha C that A x 1,...,x k =1 ( k x Bi i ) 1+ k j=1 x mi,j j (mod p). Give this, there is a atural geeralizatio of our algorithm. It should also be oted that both of these techiques allow us to use the above-stated geeralizatios to our algorithm i the vector-valued cotext. This geeralizatio also allows us to solve some related problems, such as the Uary 0-1 Kapsack problem. This problem is defied as follows: You are give a list of iteger weights w 1,...,w, a list of iteger values, v 1,...,v, ad a iteger boud B. The objective is to fid a subset S [] so that i S v i is as large as possible subject to the restrictio that i S w i B. We do this by determiig all possible pairs of ( i S w i, i S v i) by applyig our algorithm to m i = (w i,v i ) ad B = (w,v) for all w w i, v v i. Of the pairs (w,v) for which there is a solutio, we keep track of the largest v that correspods to a w B. From this pair it is also ot hard to use our algorithm to fid a subset S which achieves this boud. Refereces [1] Alvarez, C. ad Greelaw, R. A Compedium of Problems Complete for Symmetric Logarithmic Space, Computatioal Complexity, Vol. 9(2), 2000. [2] S. Cho ad D. Huyh O a complexity hierarchy betwee L ad NL Iform. Process. Lett. 29, 1988, 177182. [3] Michael Elberfeld, Adreas Jakoby, Till Tatau Logspace Versios of the Theorems of Bodlaeder ad Courcelle, Foudatios of Computer Sciece, 2010. [4] B. Moie ad I. H. Sudborough Formal laguage theory I Formal Laguage Theory, ed. R. V. Book. Academic Press, 1980. 6