2

The question says,

That given an array of size n, we have to output/partition the array into subsets which sum to N.

For E,g, 
    I/p  arr{2,4,5,7}, n=4, N(sum) = 7(given)
    O/p = {2,5}, {7}

I saw similar kind of problem/explanation in the url Dynamic Programming3

And I have the following queries in the pdf:-

  1. How could we find the subsets which sum to N, as the logic only tells whether the subset exist or not?
  2. Also, if we change the question a bit, can we find two subsets which has equal average using the same ideology?

Can anybody thrown some light on this Dynamic Programming problem.. :)

Thanks in Advance..

Peter G.
  • 13,888
  • 6
  • 51
  • 75
AlgoGeek
  • 91
  • 2
  • 8
  • 2
    http://en.wikipedia.org/wiki/Bin_packing_problem – Peter G. Jun 27 '11 at 13:03
  • Hi Peter - I din't got anything from this wiki page.. Not enough information provided... – AlgoGeek Jun 27 '11 at 13:17
  • 1
    Closer to http://en.wikipedia.org/wiki/Cutting_stock_problem, actually. In the bin packing problem it's sufficient if Σai ≤ V, but here the condition is Σai = N. – MSalters Jun 27 '11 at 13:54

3 Answers3

1

You can try to process recursively:

Given a SORTED array X={x1 ... xn} xi !=0 and an intger N.

First find all the possibilities "made" with just one element:

here if N=xp, eliminate all xi s.t i>=p

second find all the possibilities made with 2 elements:

{ (x1,x2) .... (xp-2,xp-1)}

Sort by sum and elminate all the sums >=N and you had the rules: xi cannot go with xj when xi+xj >= N

Third with 3 elments: You create all the part that respect the above rule. And idem step 2 etc...

Example:

X={1,2,4,7,9,10} N=9

step one:
{9}
X'={1,2,4,7,9}

step 2: cannot chose 9 and 10
X={(1,2) (1,4) (2,4) (1,7) (2,7) (4,7)}
{2,7}
X'={(1,2) (1,4) (2,4) (1,7)}

step 3: 4 and 2 cannot go with 7:
X={(1,2,4)}
no sol

{9} {2,7} are the only solutions

This diminishes the total number of comparaison (that would be 2^n = 2^6=64) you only did : 12 comparaisons

hope it helps

Ricky Bobby
  • 7,282
  • 7
  • 40
  • 59
  • The only problem with this method is that with a larger array of values, when do you stop trying with larger and larger amounts of numbers? Until the subsets you are trying to make are equal or larger to the values left over you have to keep running comparisons. Also this runs a lot of comparisons over and over for smaller sets when the values will be used later in a larger set. For example if you have numbers that equal N in a set of 15 integers, but smaller than that they do not create a set you are doing a large number of comparisons from 0-15 until you hit the one case that works. – SomeoneRandom Jun 27 '11 at 14:12
  • For larger sets comparaison you can imagine (i say imagine because i'm not sure it works), combining the results you had for smaller sets and using the "rules " I defined above. for example for 6 elements sets: you look at X2' and X4' (the X' I defined with 2 and 4 elements)or X1' and X5' or X3' and X3'. So it diminishes the number of reappearance for sets made of small integers. – Ricky Bobby Jun 27 '11 at 14:34
1

Unfortunately, this is a very difficult problem. Even determining if there exists a single subset summing to your target value is NP-Complete.

If the problem is more restricted, you might be able to find a good algorithm. For example:

  • Do the subsets have to be contiguous?
  • Can you ignore subsets with more than K values?
  • Are the array values guaranteed to be positive?
  • Are the array values guaranteed to be distinct? What about differing from the other values by at least some constant factor?
  • Is there some bound on the difference between the smallest and largest value?
Craig Gidney
  • 16,378
  • 4
  • 62
  • 124
0

The proposed algorithm stores only a single bit of information in the temporary array T[N], namely whether it's reachable at all. Obviously, you can store more information at each index [N], such as the values C[i] used to get there. (It's a variation of the "Dealing with Unlimited Copies" chapter in the PDF)

MSalters
  • 159,923
  • 8
  • 140
  • 320