-2

I have a question that I think is related to this one:

Algorithm wanted: Enumerate all subsets of a set in order of increasing sums

but I couldn't adapt it to my problem, which is:

Given a series $S = \pm x_1 \pm x_2...\pm x_n$, where $x_i > 0$, enumerate all possible sign configurations in order of the increasing value of $S$.

For instance, consider the series $[1,1,2,2]$:

\begin{align} &-1-1-2-2 = -6\\ &+1-1-2-2 = -4\\ &-1+1-2-2 = -4\\ &+1+1-2-2 = -2\\ &-1-1+2-2 = -2\\ &-1-1-2+2 = -2\\ &+1-1+2-2 = 0\\ &+1-1-2+2 = 0\\ &-1+1+2-2 = 0\\ &-1+1-2+2 = 0\\ &-1-1+2+2 = 2\\ &+1+1+2-2 = 2\\ &+1+1-2+2 = 2\\ &+1-1+2+2 = 4\\ &-1+1+2+2 = 4\\ &+1+1+2+2 = 6\\ \end{align}

$n$ can be large, so it's infeasible to enumerate and sort all possible combinations. I've tried a few things but couldn't get around it.

Any ideas on what to search for? I'd greatly appreciate it!

Olexandr Konovalov
  • 7,186
  • 2
  • 35
  • 73

1 Answers1

2

I doubt that there's anything better in general than enumerating and sorting. You'll have $2^n$ terms to enumerate, and sorting them will take something like $n \; 2^n$ steps.

The alternative would be some way to generate the terms already sorted. Of course you could do that if, say, lexicographic order (with $- < +$) turns out to work (which would be the case if $0 < x_{n+1} < x_n/2$). But in general, I think it's unlikely that you could predict the next term in much less than $n$ steps (which is what it would take to beat enumerate-and-sort).

Robert Israel
  • 470,583