INSTITUTE OF PHYSICS PUBLISHING

JOURNAL OF PHYSICS A: MATHEMATICAL AND GENERAL

J. Phys. A: Math. Gen. 34 (2001) 9555–9567

PII: S0305-4470(01)25711-4

Statistical mechanics of an NP-complete problem:
subset sum
Tomohiro Sasamoto1,3 , Taro Toyoizumi2 and Hidetoshi Nishimori1
1 Department of Physics, Tokyo Institute of Technology, Oh-okayama 2-12-1, Meguro-ku, Tokyo
152-8551, Japan
2 Department of Complexity Science and Engineering, University of Tokyo, Hongo 7-3-1,
Bunkyo-ku, Tokyo 113-8656, Japan

E-mail: sasamoto@stat.phys.titech.ac.jp, taro@sat.t.u-tokyo.ac.jp andnishi@stat.phys.titech.ac.jp

Received 6 June 2001
Published 26 October 2001
Online at stacks.iop.org/JPhysA/34/9555
Abstract
We study the statistical properties of an NP-complete problem, the subset
sum, using the methods and concepts of statistical mechanics. The problem
is a generalization of the number partitioning problem, which is also an NPcomplete problem and has been studied in the physics literature. The asymptotic
expressions for the number of solutions are obtained. These results, applied
to the number partitioning problem as a special case, are compared with thosewhich were previously obtained by a different method. We discuss the limit ofapplicability of the techniques of statistical mechanics to the present problem.
PACS numbers: 05.20.-y, 02.10.De, 02.60.-x, 75.10-b

1. Introduction
The methods and concepts of statistical mechanics have turned out to be quite useful in the
study of problems in computer science and related fields. In particular, techniques which
had originally been developed in spin glass theory have been successfully applied to theinvestigation of the properties of NP-complete problems in the theory of computational
complexity [1]. Some of them include the travelling salesman [2], graph partitioning [3],K-SAT [4], knapsack [5], vertex cover [6] and other problems [7, 8]. Roughly speaking, NPcomplete problems are a class of problems which are difficult to solve, in the sense that, sofar, no one has succeeded in devising (and in fact it is believed to be impossible to design) an
algorithm to determine in polynomial time whether or not there is a solution to given input data.
NP-complete problems have been extensively studied, but still pose many open questions [1,9].
3

Corresponding author.

0305-4470/01/449555+13$30.00© 2001 IOP Publishing Ltd

Printed in the UK

9555

9556

T Sasamoto et al

The issue of primary interest to computer scientists is to find an algorithm which efficiently
finds a solution to given input data, for which purpose statistical mechanics may not be of direct
use because the latter is more suitable to reveal typical properties of many-body systems.
Recently, however, statistical properties of these problems have been receiving increased
attention since it has gradually been recognized that a generally hard problem can sometimes
be solved relatively easily under certain criteria with the assistance of statistical mechanics
ideas [10].
For a wide class of NP-complete problems, the following situation occurs. A problem has
a parameter and, when the size of the problem becomes large, there appears a ‘critical’ valueof the parameter such that below it an algorithm can efficiently find a solution (easy region) but
above it the same algorithm no longer works effectively (hard region). This happens because
the definition of NP-completeness is based on the worst case analysis. A problem can beclassified as a difficult one if there are only a few difficult instances. The sudden change in thestatistical properties of a problem is in many respects similar to a phase transition, a conceptfrom statistical mechanics. In fact, the methods for studying phase transitions have turned out
to be powerful tools to understand the properties of the above-mentioned phenomena. Theseobservations suggest that the typical case study will play an increasingly important role incomputer science and accordingly the methods from statistical mechanics will provide usefultools.
In typical case studies, one usually considers a randomized version of a problem. In otherwords, our main interest is in the properties of the problem averaged over possible realizationsof input data. The randomized problems share many features with spin glass systems andhave often been studied using the techniques of spin glass theory. In particular the replica
method has allowed us to analyse the problems, many of which would have been impossible
to deal with without it. Nonetheless the resulting saddle point analysis, known as the problem
of replica-symmetry breaking, is often so hard that it is usually difficult to obtain a complete
understanding of the problem. Hence, to gain more insights, it is important to study problems
which are solvable without using replicas.
The number partitioning problem seems to be an ideal example from this point ofview [11–14]. Suppose that one is given a set of positive integers A = {a1 , a2 , . . . , aN }and asked to divide this into two subsets with the same value of their sums. In other words,
one tries to find a subset A ⊂ A which minimizes the partition difference


 


(1.1)
aj −
aj .
A

A \A 

A subset A with zero partition difference is called a perfect partition, whereas a subset witha positive partition difference is termed an imperfect partition. It has been argued that thisproblem shows a sharp change of state, reminiscent of a phase transition, between easy andhard regions [12–15]. In addition, the problem has a lot of practical applications such as
multiprocessor scheduling and minimization of VLSI circuit size.
The analysis in [13, 14] starts from taking the partition difference to be the Hamiltonian.
Then a perfect partition corresponds to a ground state of the Hamiltonian and an imperfectpartition to a configuration with positive energy. By applying statistical mechanics methods
and a saddle point approximation in the large-N limit, several results have been obtained
without using replicas. The phase transition behaviour of the problem, found numerically [15],was understandable through those results. But the expressions obtained in his analysis show
peculiar high temperature behaviour, as will be shown below. In particular, the partition
function does not give the correct entropy in the limit of high temperature. Hence his results
are not expected to give reliable predictions for imperfect partitions.

Statistical mechanics of an NP-complete problem: subset sum

9557

The main purpose of this paper is to propose an alternative approach to the number
partitioning problem applicable to imperfect partitions as well. We study a generalized versionof the number partitioning problem: the subset sum [1]. By using some basic concepts andmethods of statistical mechanics, the asymptotic expressions of the number of solutions are
obtained. Our results specialized to the number partitioning problem are compared with thepreviously obtained predictions. It is shown that our results are applicable to those cases where
the predictions of the previous analysis do not agree with an exactly solvable example. Our
discussions are mainly restricted to the easy region, although the hard region could also be
considered by similar arguments using the ideas in [16].
The rest of the paper is organized as follows. In the next section, we introduce the subset
sum and reformulate it in terms of a Hamiltonian. By using the canonical ensemble, theasymptotic number of solutions is estimated in section 3. Based on these results, we discussa crossover between easy and hard regions of the subset sum in section 4. In section 5, theanalysis is generalized to the case with constraints. In section 6, we apply the results to thenumber partitioning problem and compare the results with those in [13]. Our conclusion is
given in the last section.
2. Subset sum
Let us denote N+ = {1, 2, . . .}, the set of positive integers. The subset sum is an example
of NP-complete problems in which one asks, for a given set of A = {a1 , a2 , . . . , aN } withaj ∈ N+ (j = 1, 2, . . . , N) and E ∈ N+ , whether or not there exists a subset A ⊂ A such thatthe sum of the elements of A is E [1]. To formulate the problem, we introduce a Hamiltonian(or energy)
H =

N


aj nj

(2.1)

j =1

where nj ∈ {0, 1} (j = 1, 2, . . . , N ), and the subset sum is equivalent to asking whether or
not there exists a configuration {n1 , n2 , . . . , nN } such that H = E. A configuration which
satisfies H = E is called a solution in the following.
There are several versions of the problem. The original one is the decision problem; one
only asks whether there exists a solution or not. Once one learns that the answer to the decision
problem is yes, however, it would be quite natural next to ask how many solutions there are.
This is called the counting (or enumeration) version of the problem. On the other hand, if there
is no solution, one might try to find the best possible configuration which minimizes the energy
difference from the given E. This is the optimization version of the problem. Of course, these
versions are closely related to each other. In the following treatments, we focus on the counting
version of the problem, for which statistical mechanics provide powerful analytical tools. The
number of solutions for a given energy E will be denoted by W (E).
3. Statistical mechanical analysis of subset sum
Evaluation of the exact value of W (E) for given A and E is still a question of complicatedcombinatorics and is very hard. In particular, fixing the value of E is a very strong constraint
which renders the counting almost intractable. In the terminology of statistical mechanics,
considering the problem with a fixed value of E corresponds to working in the microcanonicalensemble. For many purposes in practice, however, one is interested in the asymptotic
behaviour for a large N and is satisfied with approximate expressions of W (E); an exact

9558

T Sasamoto et al

expression of W (E) is unnecessary and can even obscure the essential aspects of the problem.
Experience in the study of statistical mechanics tells us that, in order to know the asymptotic
behaviour of W (E), it is much easier to work in the canonical ensemble. This is a superpositionof the microcanonical ensembles for all possible values of the energy with the Boltzmann factor
e−βE , where β is the inverse temperature. In this section, the set A is still fixed; statistics overmany A is not considered.
Simplicity of the analysis of the subset sum compared with other problems stems from acompact expression for the partition function. For a given A, the partition function Z is simply
given by Z=e−βH =
···
e−βH
{nj }

n1 =0,1 n2 =0,1

= (1 + e−βa1)(1 + e−βa2

nN =0,1) · · · (1 + e−βaN ).

(3.1)

From this one can calculate the average values of various physical quantities. The average
here means the thermal average and is denoted by · · · . The average energy E for a given
value of β is given by
E =−

N

aj
∂
.
log Z =
∂β
1 + eβaj
j =1

(3.2)

Note that the value of E can be controlled by changing β. As β is increased from −∞ to ∞,the average energy E decreases from N
j =1 aj to 0. In the usual statistical mechanics, thetemperature and hence β should be positive. For our present problem, however, the temperature
is introduced only as a parameter to control the average energy. A negative value of β is also
allowed in our problem. The fluctuation of the energy is similarly calculated as(E − E )2 =

N

aj2
∂2
log
Z
=
.
∂β 2
(1 + eβaj )(1 + e−βaj )
j =1

(3.3)

Here we go back to (3.1) and observe that W (E), the number of solutions to the condition
H = E, appears as the coefficient of the Eth power of q (= e−β ) in Z; the expansion of Z interms of q gives
Z=

E
max


W (E) q E

(3.4)

E=0

with Emax = a1 + a2 + · · · + aN . Moreover, since Z is a polynomial in q, (3.4) can be inverted
easily: W (E) has an integral representationdq
W (E) =
Zq −E−1(3.5)
2πiCwith C being a contour enclosing the origin anticlockwise on the complex q plane. It is
important to notice that the contour C in (3.5) can be deformed arbitrarily as far as it encloses
the origin anticlockwise.
Now we consider the asymptotics of W (E) as N → ∞. One should specify how this limit
is taken since changing N also implies changing A simultaneously. To avoid this difficulty, let
us suppose for the moment that one first has an infinite set A∞ = {a1 , a2 , . . .}, each elementof which is taken from a finite set of {1, 2, . . . , L}, with L ∈ N+ . Then the set A can be
regarded as a collection of the first N elements of A∞ . The limit N → ∞ is defined without
ambiguities in this way.
Since aj satisfies 1  aj  L for all j , a simple estimation of (3.3) shows that thefluctuation of the energy is of the order of N when β is finite. Hence the fluctuation of theStatistical mechanics of an NP-complete problem: subset sum

9559

energy per system size N , i.e. (E/N − E /N )2 , tends to zero as N → ∞. Let β0 be
the value of β such that the average energy E is equal to E. Then one takes the contour Cin (3.5) to be a circle with radius e−β0 and uses the phase variable θ defined by q = e−β0 +iθ to
find
 π
1
W (E) =dθ exp (log Z + β0 E − iEθ).
(3.6)
2π −π
It is not difficult to verify that the saddle point of this integrand is at θ = 0. (When the g.c.d.
of A is not one, there appear other saddle points with the same order of contributions. But,
when N is large enough, it is almost sure that the g.c.d. of A is one, which we assume in what
follows.)
The quantities in the exponent on the right-hand side of (3.6) are all of order N . Therefore
we can use the method of steepest descent to evaluate the asymptotic behaviour of the integral.
Expanding log Z around β = β0 to second order leads to

 2 2
 π
1
θ ∂
W (E) = exp (log Z|β=β0 + β0 E)dθ exp −log Z|β=β0 .(3.7)
2π −π2 ∂β 2Since the second derivative of log Z is the fluctuation of the energy (3.3) and is of order N ,the integration range may be extended to ±∞. The result is
exp log Z|β=β0 + β0 E
W (E) ≈
.
(3.8)
∂2
2π ∂β
log
Z|
β=β0
2
Here and in the following the symbol ≈ means that the ratio of the right- and left-hand sides
tends to unity as N → ∞. After rewriting β0 back to β, we finally obtain the asymptotic
expression of W (E) for a given value of E via a common parameter β as
 N


−βaj
βaj
exp
)+β N
)
j =1 log(1 + e
j =1 aj /(1 + e
W (E) ≈
(3.9)
2βaj )(1 + e−βaj )2π N
j =1 aj /(1 + e
E=

N


aj
.
1
+
eβaj
j =1

(3.10)

A numerical check of (3.9) and (3.10) is shown in figure 1. As far as one sees on this scale,the agreement of our predictions and simulation data is satisfactory for the entire range of theenergy.
One may notice that the obtained expressions, (3.9) and (3.10), do not depend on L. Thisis plausible since the number of solutions depends only on the elements of A, not directlyon the set from which elements of A have been taken. One should remember in this relationthat the validity of the saddle point analysis depends on L. The expressions (3.9) and (3.10)
become better approximations as N → ∞ for a fixed value of L. It would not be surprising if
the expressions do not agree very well with simulational data when L is sufficiently large sothat W (E) is of O(1). In particular, one should not use (3.9) in the parameter region which
gives W (E) < 1, as will be discussed below.
4. Easy/hard regions
Numerical simulations in [15] suggest that there exist easy and hard regions for a randomized
version of the subset sum, in which one considers statistics over many samples of A with eachaj drawn from {1, 2, . . . , L} uniformly. In simulations, one checks if there exists a solution for9560

T Sasamoto et al

W(E )
1200
1000
800
600
400
200
1000

2000

3000

4000

5000

E

Figure 1. The number of solutions W (E) as a function of the energy E for an example with N = 20,
L = 256, and A = {218, 13, 227, 193, 70, 134, 89, 198, 205, 147, 227, 190, 27, 239, 192, 131}.
The theoretical prediction is indistinguishable from the numerical results plotted as dots.

many samples of A with given N, L and E. Then, for each N and E, one plots the probability
that there is at least one solution as a function of κ = log2 L/N . Then it is observed that theprobability decreases fairly sharply from 1 to 0 as κ increases from zero to ∞. As N becomeslarger, the decrease of the probability occurs in a narrower range of κ. In fact, the system appears
to have a sharp transition at a critical value κc in the limit of N, L → ∞ [15]. In this section, we
estimate the critical value κc of the randomized subset sum by using the results of the last section.
The analysis in the last section gives us the asymptotic formula for a fixed A. To apply the
results to the randomized version of the problem, one has to notice that, as N becomes large,
the sample dependence of (3.9) and (3.10) is increasingly suppressed. In fact, in the limit
N → ∞ with L fixed, there would be no sample dependence so that the average properties ofthese quantities coincide with those of a typical sample. To see this, let us define the density ofyj = aj /L (1  j  N ) to be ρN (y) = N1 N
j =1 δ(y − yj ). Since we draw the aj uniformly,
we have lim ρN (y) = ρ(y), where ρ(y) = 1 for 0  y  1 and ρ(y) = 0 otherwise. In
N →∞addition, in this limit, summations in (3.9) and (3.10) are replaced by integrals, resulting in1
exp N 0 dy log(1 + e−αy ) + αy/(1 + eαy )W (E) ≈
(4.1)
1
2πN L2 0 dy y 2 /(1 + eαy )(1 + e−αy )
 1
y
E
dy
(4.2)
≈
x=
N ·L
1
+
eαy
0
where we have introduced a scaled inverse temperature α = β/L. The parameter α controls
x, the energy divided by N · L; as α is increased from −∞ to ∞, x decreases from 1/2 to 0.
The validity of these expressions is determined only by the values of N and L. For a given N ,they are valid for sufficiently small L. Even though L changes, however, these expressions are
expected to be good approximations as long as N is relatively large or when κ is smaller thanκc . On the other hand, the reliability of these expressions is unclear for κ  κc . In fact, thereis evidence that the average minimal cost is not self-averaging in this region [12]. The value ofκ below which the above formulas are valid increases as N and L increase, and finally reaches
κc in the limit N, L → ∞. It is important to notice that the value of κc can be determined by
the condition W (E) = 1 in the limit N, L → ∞ because W (E) is the expectation value ofthe number of configurations. We therefore find
 1
1
αy 
κc =
(4.3)
dy log(1 + e−αy ) +log 2 01 + eαyStatistical mechanics of an NP-complete problem: subset sum

9561

x
0.6
0.5

hard

0.4
0.3
easy

0.2
0.1

0.2 0.4 0.6 0.81

1.2 1.4

κFigure 2. The easy/hard regions of the randomized subset sum.

with α determined by (4.2) for a given value of x. For κ < κc , exponentially many solutions
are expected to exist and one of them can be found fairly easily. On the other hand, for κ > κc ,there is practically no solution and hence it is virtually impossible to find one. The easy/hardregions of the randomized subset sum are shown in figure 2.
5. Constrained caseIn some applications, one might encounter a situation where the number of aj is given. In
this section, our previous analysis is generalized to the constrained case where the number ofchosen aj is fixed to M. Instead of considering directly the system with constraint, we again
take a superposition of the problems with various values of M. In the language of statisticalmechanics, we work in the grand canonical ensemble. Let us define a Hamiltonian
N
N

µ
Hc =
aj nj −
nj .
(5.1)
β j =1
j =1
The first term is nothing other than the Hamiltonian (2.1) for the unconstrained subset sum.
The second term is introduced to control the number of aj by changing the parameter µ, thechemical potential. The grand partition function is evaluated as

e−βHc
#=
{nj }

= (1 + eµ e−βa1 )(1 + eµ e−βa2 ) · · · (1 + eµ e−βaN )
N E
max


W (M, E)eµM e−βE
=

(5.2)

M=0 E=0

a1 + a2 + · · · + aN as
before. Here W (M, E) is the number of configurations
with Emax = 
N
a
n
=
E
and
which satisfy N
jjj =1
j =1 nj = M simultaneously.
For given values of µ and β, the average number, energy and second moments of thesequantities are expressed as
N

1
∂
M =
(5.3)
log # =∂µ
1 + eβaj −µ
j =1
E =−

N

aj
∂
log # =
βaj −µ
∂β
1
+
e
j =1

(5.4)

9562

T Sasamoto et al(M − M )2 =

N

1
∂2
log
#
=
βaj −µ )(1 + e−βaj +µ )
∂µ2
(1
+
e
j =1

(M − M )(E − E ) = −(E − E )2 =

N

aj
∂2
log # =
βaj −µ )(1 + e−βaj +µ )
∂β∂µ
(1
+
e
j =1

N

aj2
∂2
log
#
=
.
∂β 2
(1 + eβaj −µ )(1 + e−βaj +µ )
j =1

(5.5)

(5.6)

(5.7)

Similarly to the unconstrained case, one can show that the fluctuations of the number andenergy divided by the system size vanish as N → ∞ so that one can apply the saddle point
method. The resulting asymptotic expression for W (M, E) is
exp[log # + βE − µM]
√
2π D

(5.8)

 ∂2

∂2
 ∂µ2 log #− ∂β∂µlog # 

.
D=
2
∂2
− ∂ log #log # 

(5.9)

W (M, E) ≈with D being

∂β∂µ

∂β 2

The values of M and E are given by (5.3) and (5.4), respectively. Using these expressions,
one can discuss the easy/hard regions of the constrained subset sum. The analysis is almost
the same as that in the last section and is omitted here.
6. Number partitioning problem
As already mentioned in the introduction, the subset sum is regarded as a generalization of
the number partitioning problem. In this section, we apply our previous discussions to thenumber partitioning problem. Our results are compared with those in [13, 14], which arebriefly reviewed in the appendix, with some remarks.
Let us first establish an explicit relationship between the subset sum and the number
partitioning problem. If one introduces the spin variables by
sj = 2nj − 1(6.1)

it is not difficult to see

  N

N


 

H̃ := 2H −aj  = aj sj .
j =1

(6.2)

j =1

This is exactly the Hamiltonian of the number partitioning problem studied in [13, 14].
The number of solutions of H̃ = Ẽ, which we denote by W̃ (Ẽ), is related to W (E) by
  N
 

1


W
(Ẽ = 0)
aj

2

j =1
W̃ (Ẽ) =
(6.3)




N
N




1
1
1
1

(Ẽ > 0).
aj + W − 2 Ẽ + 2
aj W 2 Ẽ + 2
j =1

j =1

Of special interest is the case of Ẽ = 0, a solution of which is called a perfect solution
in the number
partitioning problem. In the subset sum,N this corresponds to theenergyN
E = 21 N
j =1 aj . Clearly there is no perfect solution if
j =1 aj is odd; we assume
j =1 ajStatistical mechanics of an NP-complete problem: subset sum

9563

is even in the following. In terms of β, considering perfect solutions corresponds to β = 0
from (3.10). Setting β = 0 in (3.9) leads to2N
π NW̃ (0) ≈2

2
j =1 aj(6.4)

which is expected to be the number of perfect solutions to the number partitioning problem. In
fact (6.4) agrees with the previously obtained result in [13, 14]. Hence, as far as the number ofperfect solutions for the number partitioning problem is concerned, our method gives exactly
the same answer as in [13, 14].
The difference between our formula and that in [13,14] becomes manifest for a finite valueof Ẽ. We demonstrate this by considering the number partitioning problem for a special
 casewhere a1 = a2 = · · · = aN = 1 with N even. In this case, the Hamiltonian is H̃ = | N
j =1 sj |,−β H̃
and it is possible to write down the partition function Z̃ = {sj } eexplicitly:


N

N
e−β|N −2j |
Z̃ =
j
j =1


=

N
N
2




N/2 

N
+2
e−2βj .
N
+j
2
j =1

(6.5)

This formula indicates that there are solutions for even Ẽ and that W̃ (Ẽ) is


2N

N


≈ π


N/2
N

2



 (Ẽ = 0)W̃ (Ẽ) =2 exp N − ( 21 − Nj ) log( 21 − Nj ) − ( 21 + Nj ) log( 21 + Nj ) (6.6)
N


2
≈


N/2 + j
2π N ( 21 + Nj )( 21 − Nj )(Ẽ = 2j )where the asymptotics are also indicated. For the present case with aj = 1 (1  j  N ), (3.10)
is simply reverted as β = log(N/E − 1). Then, using Stirling’s formula, one can confirm that
our formula (3.9) gives correct asymptotics in (6.6) for the entire range of energy. In contrast,
the partition function in [13, 14], which we denote by Z̃  , for the present case, can be written
as


∞

2N

−2βj
.
(6.7)
Z̃ =  π
e
1+2
N
j =1
2

This indicates that there are solutions for even Ẽ and that W̃ (Ẽ) is asymptotically 2N / π2 N for
Ẽ = 0 and 2N +1 / π2 N for Ẽ = 2j . As is clear from (6.6), the correct asymptotic is predicted
only for Ẽ = 0. One may notice that the arguments in [13, 14] are somewhat different fromours. There, the energy Ẽ and the entropy S̃ are calculated from the partition function (6.7),
following the usual prescriptions of statistical mechanics. It is assumed that exp(S̃) gives thenumber of solutions. The obtained expressions again do not give the correct asymptotics ofW̃ (Ẽ) when Ẽ > 0. Our conclusion is that the results of [13, 14] give the correct asymptotic
value for Ẽ = 0 but not for Ẽ > 0.
The reason for this difficulty is traced back to the application of statistical mechanics
techniques to the systems for which the number of solutions of H̃ = Ẽ decreases as Ẽ
increases. It can be seen from figure 1 that the number partitioning problem is indeed an

9564

T Sasamoto et al

example with this anomalous property, if one notes that Ẽ = 0 corresponds to the peak of
the curve. The problem is that, for such systems, the usual prescriptions of the canonicalensemble do not work. For normal physical systems, the number of states W (E) increases
as a function of the energy E. The number of states multiplied by the Boltzmann factor
W (E)e−βE takes a maximum at some value of E. The peak around this point becomes
drastically sharp as the system size increases. Then the equivalence of microcanonical andcanonical ensembles holds so that we can study the thermodynamic behaviour of the systemin either ensemble. For systems with decreasing W (E), however, W (E)e−βE is a monotone
decreasing function. The fluctuation of the energy does not tend to zero even when the systemsize increases indefinitely, and consequently one cannot control the energy by changing thetemperature. As a result, the equivalence of ensembles does not hold. The exponential of
the entropy calculated in the canonical ensemble and the coefficient of the expansion of Z in
powers of e−βE do not agree, even in the thermodynamic limit; in addition, neither of thesequantities give the correct asymptotics of the number of configurations W (E). This problem
may be overcome by considering a negative temperature, as we did for the subset problem, but
a direct analysis of the number partitioning problem described by (6.2) would then be much
more difficult.
Before closing this section, we discuss the randomized number partitioningproblem witha constraint in which one asks whether or not there exists a perfect solution with N
sj fixed.
N j =1s=(2n
In the language of subset sums, this corresponds to fixing M since Nj −1) =
j =1 jj =1
2M − N . In [12, 14], it has been found that there is a phase transition in the limit M, N → ∞with m = 2M/N − 1 fixed. We can reproduce this phenomenon from the results of section 5.
In the limit N, L and M → ∞, summations in (5.3) and (5.4) are replaced by integrals. They
are written as, for a uniform distribution,
 11eα + eµ11(1 + m) =
log
dy
=
1
−
(6.8)
2
1 + eαy−µα1 + eµ
0
 1
y
x=
dy
(6.9)
1 + eαy−µ0where α = β/L and x = E/N · L as before. Since (6.8) is easily reverted aseα − eα(1−m)/2(6.10)eα(1−m)/2 − 1
one can regard x as a function of α and m. Then, for a fixed m (−1  m  1), one sees that x
decreases from (1 + m)(3 − m)/8 to (1 + m)2 /8 as α is increased from −∞ to ∞. There are few
or no configurations for an energy corresponding to x outside this range. If we note that theperfect solution corresponds
to x = 1/4, we find that there are extensive numbers of√perfect√solutions when |m| < 2 − 1 and there is practically no perfect√ solution when |m| > 2 − 1.
Hence we conclude that there is a phase transition at mc = 2 − 1, in agreement with theprevious analysis [12, 14].
µ = log

7. Conclusion
We have studied the statistical properties of the subset sum, which is a generalization of thenumber partitioning problem. The basic ideas and methods of statistical mechanics enabled
us to study the asymptotic behaviour of the number of solutions for a given set of input data.
The expressions (3.9) and (3.10) represent the main results of this paper. The agreement of
the predictions with simulation data have been found to be satisfactory. Our results have been
compared with those which were previously obtained by a different method. They agreed with

Statistical mechanics of an NP-complete problem: subset sum

9565

each other for the number of perfect solutions of the number partitioning problem. On theother hand, in the case of the subset sum, only our analysis gave the correct asymptotics overthe entire range of energy. The reason why the validity of the results in [13, 14] is restricted
to perfect solutions has been argued to be that the entropy calculated in a canonical ensemble
does not necessarily give the logarithm of the number of configurations for systems with adecreasing number of states as the energy increases. In such anomalous systems, one should
be extremely careful in using the equivalence between microcanonical, canonical and grandcanonical ensembles.
Acknowledgments
The work of HN was supported by the Grant-in-Aid for Scientific Research by the Ministryof Education, Culture, Sports, Science and Technology, the Sumitomo Foundation and theAnglo-Japanese Collaboration Programme between the Japan Society for the Promotion of
Science and the Royal Society. TS acknowledges the Grant-in-Aid for Encouragement ofYoung Scientists from the Ministry of Education, Culture, Sports, Science and Technology.
Appendix. Some remarks on the analysis in [13, 14]
In this appendix, we briefly review the results in [13,14] and give a few remarks on the analysis.
Our notation is slightly different from the original for consistency with the main text. The g.c.d.
of A is assumed to be unity in the following. After some manipulation, the partition function
Z̃ for the Hamiltonian (6.2) is rewritten asZ̃ =e−β H̃ = 2N

{sj }

 π/2

dy N G(y)e−π/2 π(A.1)

where β  0 is the inverse temperature andG(y) =N1 
log cos(βaj tan y).
N j =1(A.2)

Then it has been argued that, for a large N , the integral in (A.1) can be evaluated using the
Laplace method. There exist an infinite number of points which give the maximum value of
Re {G(y)}. The main contributions are expected to come from the points πyk = arctankk = 0, ±1, ±2, . . . .
(A.3)
β
It is not difficult to confirm Re {G(y)}  0 for general y and Re {G(yk )} = G (yk ) = 0 andG (yk ) < 0, so that yk of (A.3) certainly give the maximum of Re {G(y)}. The contributions
from these yk can be summed up explicitly, and the result is ∞∞
dy N G (yk )y 2e2eN G(yk )Z̃ ≈ 2N
−∞ π
k=−∞
=

β

2N
π N
2

j =1

∞


(−1)kλπ2a 2 k=−∞ 1 + ( β k)
j

9566

T Sasamoto et al

=









2N
π N








2N
π N

N

2

2

2
j =1 aj

j =1 ajcoth β(λ : even)cosechβ(λ : odd)

(A.4)

2

where λ = j =1 aj . Here we remark that in [13, 14] the formula for the case of odd λ is
missing. (Also, in the arguments of the constrained case in [14], the results are presented only
for the case of even N , but one has to consider the case of odd N separately.) Nevertheless,we only consider the case where λ is even in the following, because the odd λ case can be
discussed similarly.
One notes that the expression (A.4) diverges as β → 0 while the correct limiting value isclearly 2N from the definition (A.1). Hence, as mentioned in the main text, his result (A.4) is
not valid, at least for small β (or large T ).
In [13, 14], the hard region was also discussed using (A.4). In particular, the averageminimum cost was estimated. One should use the finite-temperature expression of the partition
function (A.4) to analyse the non-vanishing value of the average minimum cost in the hardregion. However, since (A.4) is not reliable for large T , the formulae given in [13, 14] should
be taken with special caution. The principal source of trouble is in the anomalous propertiesof the systems with decreasing numbers of configurations as the energy increases. There isanother type of problem in his analysis, as we discuss in the following.
A sign of difficulty is seen from the negative value of the entropy in the hard region. The
entropy calculated from the partition function (A.4) is
2NβS̃ = log
coth β +
.
(A.5)
sinh
β
· cosh β
π N
a 2
2

j =1

j

The ground state entropy S̃0 = limβ→∞ S̃ is found to beS̃0 = {N − Nc (A)} log 2

(A.6)

with
Nc (A) =Nπ1
aj 2 .
log2
2
2 j =1(A.7)

This is equivalent to (6.4). One notices that, when N < Nc (A), the ground state entropy isnegative. In [13, 14], the easy (resp. hard) region is characterized by a positive S̃0 (resp. anegative S̃0 ), i.e. by N > Nc (A) (resp. N < Nc (A)). In an appropriate limit, this coincides
with κ < κc (resp. κ > κc ). To avoid the difficulty of negative entropy in the hard region, theauthor of [13, 14] proposed not to take the β → ∞ limit but to use (A.4) only down to thetemperature where S̃  log 2. This is an arbitrary process which would not be necessary if we
use the exact expression of the entropy.
To identify the problem within this formalism, let us remember that (A.4) was obtainedby summing up only the contributions from around the extreme points {yk } of (A.3). In
the easy region (N  Nc (A)) the peaks around these points are very sharp and hence
the Laplace method gives a good approximation. On the other hand, in the hard region(N  Nc (A)), there appear a large number of other local maxima, with values not so far
from zero and at points located fairly close to the points {yk } of (A.3). One will be easily
convinced that this happens by checking a very simple example of N = 2. In this case G(y)is
G(y) = 21 log cos(βa1 tan y) + 21 log cos(βa2 tan y)(A.8)

Statistical mechanics of an NP-complete problem: subset sum

9567

where a1 , a2 (a1 < a2 ) are coprime natural numbers with a2 sufficiently large, corresponding
to the hard region.
References[1] Garey M R and Johnson D S 1979 Computers and Intractability: A Guide to the Theory of NP-Completeness(New York: Freeman)[2] Mézard M and Parisi G 1986 J. Physique 47 1285
[3] Fu Y and Anderson P W 1986 J. Phys. A: Math. Gen. 19 1605
[4] Monasson R and Zecchina R 1997 Phys. Rev. E 56 1357
[5] Inoue J 1997 J. Phys. A: Math. Gen. 30 1407
[6] Weigt M and Hartmann A K 2001 Phys. Rev. Lett. 19 1658
[7] Mézard M, Parisi G and Virasoro M 1987 Spin Glass Theory and Beyond (Singapore: World Scientific)[8] Nishimori H 2001 Statistical Physics of Spin Glasses and Information Processing: An Introduction (Oxford:
Oxford University Press)[9] Papadimitriou C H 1994 Computational Complexity (Reading, MA: Addison-Wesley)[10] Hogg T, Huberman B A and Williams C (ed) 1996 Frontiers in problem solving: phase transitions and complexity
Artificial Intelligence vol 81 part I and II (Amsterdam: Elsevier)[11] Fu Y 1989 Lectures in the Science of Complexity vol 1, ed D L Stein (Reading, MA: Addison-Wesley) p 815[12] Ferreira F F and Fontanari J F 1998 J. Phys. A: Math. Gen. 31 3417
[13] Mertens S 1998 Phys. Rev. Lett. 81 4281
[14] Mertens S 2000 Preprint cond-mat/0009230[15] Gent I P and Walsh T 1996 Proceedings of ECAI-99 ed W Wahlster (New York: Wiley) p 170[16] Mertens S 2000 Phys. Rev. Lett. 84 1347