Dynamic Programming Problem.. Array Partitioning..

微笑、不失礼 提交于 2019-12-11 03:15:51

问题


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..


回答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




回答2:


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?



回答3:


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)



来源:https://stackoverflow.com/questions/6493120/dynamic-programming-problem-array-partitioning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!