Re: array of integers




On Thu, 28 Apr 2005, Kemal Oral CANSIZLAR wrote:

There is an array of integers, without any restriction. How can I come up with an algorithm to find all combinations of elements that add up to the numbers in the array. If array is {1, 3, 2, 5, 4}, result would be {1, 2} = 3 is in array; {1, 3} = 4 is in array; {2, 3} = 5 is in array; {1, 4} = 5 is in array.

That depends in what format your input and output need to be. I think (but I'm no expert) that if you're given a set (say {1,3,6,7}) and some value (say 8) by the user, and asked "does any subset of {1,3,6,7} sum to 8?", then that's a variant of the subset-sum problem, which is hard. (For the definition of "hard" in this context, Google "NP-complete".)

If, as I suspect, you just want a new array consisting of all the subset-sums of the input array --- {0,1,3,4,6,7,8,9,10,11,13,14,16,17}
--- then that seems like an easier problem. (I mean, you can do all the
work once and then just do a search of the output array to determine
whether any input number is in it or not.) You can do that in O(2^n)
time by just generating all the combinations of the input array's elements.


Sure, it's slow for big inputs, but I don't think you can do any better
/without any restrictions/, as you said. If you can find out some interesting properties of the input array and target, then maybe you can
solve some special cases. (E.g., if the target is less than the minimum
element of the array; if the target is odd and all the array elements are
even; that sort of thing.)


  Maybe someone here knows the state of the art in heuristics for this
kind of problem; I don't.

HTH,
-Arthur
.



Relevant Pages

  • Fwd: Please Forward: Ruby Quiz Submission
    ... string then back into a count array. ... beyond the range of the target and source, I would munge one or more ... the target contains a direct representation of the pangram search ... def to_counts ...
    (comp.lang.ruby)
  • Re: readdir formulated badly? gives wrong count
    ... Sometimes there might be some numbered files already in the target ... I'll probably try to turn the perl script into something more general ... You are doing boolean tests on the contents of array elements when you ... sub usage { ...
    (perl.beginners)
  • Re: Efficient Searching
    ... by creating an array from the ... Alternatively, I create a hash with integer_1 as key and as the value, ... Thus for 200 target values, ... cannot program a binary search correctly, ...
    (comp.lang.perl.misc)
  • Fwd: Please Forward: Ruby Quiz Submission (Part 1)
    ... array by initializing it with complete information on what was knowable ... string then back into a count array. ... beyond the range of the target and source, I would munge one or more ... the target contains a direct representation of the pangram search ...
    (comp.lang.ruby)
  • Re: memory mapped file question
    ... source copies its data to a special address and a target can read out ... data from this special memory address and has to copy it into its ... It allows any number of processes to share a block of data, overlaying a VB array of any type. ... dim MyArrayas Type MyArrayType ...
    (microsoft.public.vb.winapi)