0

I have total partitions of an integer and I want only those partitions which have all the values unequal. For ex.-Partitions of 3 are {1,1,1,1},{2,2},{3,1},{1,1,2} and {4}. So, the required unequal partitions are {3,1} and {4} because they contain no equal elements. The code that I have used for finding all partitions is provided below. I can filter the partitions to get the desired result, but I want some efficient way to find all the partitions, which have no equal terms in them, without finding all the partitions. I have searched the net and stackoverflow but nothing states exactly the problem that I am facing. Every idea is appreciated. Thanks.

function total_partitions_of_a_number($n) {# base case of recursion: zero is the sum of the empty list
if(!$n) return array(array()); # return empty array

# modify partitions of n-1 to form partitions of n
foreach(total_partitions_of_a_number($n-1) as $p) { # recursive call
 $a[] = array_merge(array(1), $p); # "yield" array [1, p...]
 if($p && (count($p) < 2 || $p[1] > $p[0])) { # p not empty, and length < 2 or p[1] > p[0]
   ++$p[0]; # increment first item of p
   $a[] = $p; # "yield" p
 }
}
return $a; # return all "yielded" values at once
}
Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
Sushant
  • 66
  • 1
  • 9

1 Answers1

2

So you want only partitions where any given component appears no more than once? The recursion is simple.

Reduce it to the problem of solving for the partitions of N, such that no element in the set is larger than some value a (a will initially be N.) Now, a either does or does not appear in the partition. Depending on this, then you will both recursively solve for the partitions of (N-a), such that no element is larger than a-1, and for the partitions of N such that no member is larger than a-1.

In either case, the recursion is well posed, and will terminate when it is no longer possible to solve the problem, thus, when a*(a+1)/2 < N. Of course, when a*(a+1)/2 = N, you can also quickly terminate the recursion as the solution is then unique.

  • Just to know, how did you think of this algorithm? And I am not getting the logic behind it. I mean what makes us to take this way. How can this approach strike someone's mind? – Sushant Apr 07 '12 at 12:58
  • What made me think of it is the code I wrote to do it quite a few years ago. I've had code to do essentially exactly what you want (as MATLAB code), posted online in 2006: http://www.mathworks.com/matlabcentral/fileexchange/12009-partitions-of-an-integer –  Apr 07 '12 at 13:34
  • As for not getting it, it seems clear to me. Consider a partition of N. Is "a" a member of that partition, where no member of that partition is larger than a? If it is, then a can appear no more than once. If any event, then you now need to find the partitions of both N-a and of N, in both cases, where the largest element is no larger than a-1. Surely you see that this is a recursive scheme that MUST terminate and MUST yield all solutions in the end. –  Apr 07 '12 at 14:48
  • Hey. Thanks for all the knowledge. Actually I am trying to implement your algorithm but I couldn't come up with the right implementation. I think I might be missing some point. I know it sounds almost like "code this for me", but as you have done the same problem before, can you please extend my provided function to find the unique partitions by your algorithm? If not, can you either provide me your code in PHP( because I have no idea about MATLAB) or at least some pseudocode? I am sorry for asking for code but my work is stuck because I am not able to implement properly. – Sushant Apr 11 '12 at 07:09
  • I am waiting for your kind reply. Thanks. – Sushant Apr 11 '12 at 07:10
  • Sorry, but no. I won't learn PHP so that I can write/debug your code for you, or go to the extent of writing out a highly detailed pseudocode/flowchart that you can follow. I gave you a very clear algorithm that PROVABLY will provide the result you desire, and will do so fairly efficiently. If you use a memoized scheme in the recursion it will be quite efficient, although that will often be unnecessary unless your problem is large. If you can't figure out how to code it, then you are effectively asking me to learn PHP so that you don't need to bother to learn to read MATLAB? –  Apr 11 '12 at 10:48
  • I didn't know that you don't have knowledge of PHP. – Sushant Apr 11 '12 at 10:58