Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a name for this set/array operation?

Given the input array

[a,b,c,d,e]

and a 'join' function (a,b) => (a+b)

my code returns the following array of arrays, containing each possible variation obtained by applying the join function to various pairs of elements whilst maintaining the order:

[
  [a,b,c,d,e],
  [a,b+c,d,e],
  [a,b+c+d,e],
  [a,b,c+d,e],
  [a+b,c,d,e],
  [a+b,c+d,e],
  [a+b+c,d,e],
  [a+b+c+d,e],
  [a,b,c,d+e],
  [a,b+c,d+e],
  [a,b+c+d+e],
  [a,b,c+d+e],
  [a+b,c,d+e],
  [a+b,c+d+e],
  [a+b+c,d+e],
  [a+b+c+d+e],
]

Visually, what I'm trying to do is this:

diagram of ordered partitions

The code works but I have no idea what to call it - and would like to use a name that other developers familiar with this operation will understand, should such a name exist. It's not a power set but it is something similar... does this particular set/array operation have a name?

EDIT: OK. They're not permutations; permutations would all be 5-element arrays in different orders [[a,b,c,d,e], [e,d,c,b,a], [a,d,b,c,e], ...]

They're not partitions, since any subset can only contain adjacent elements of the input. - in other words, partitions would allow this:

diagram depicting partitions of non-adjacent elements

(This probably arises from pure set theory having no notion of an ordered set.)

They're not combinations since every element of the output uses every member of the input set exactly once.

I think myArray.OrderedPartitions((a,b) => (a+b)) is probably a suitably succinct and explanatory.

like image 265
Dylan Beattie Avatar asked Apr 16 '13 11:04

Dylan Beattie


1 Answers

As mbeckish said in a comment, those sets are (once an order on the original set is fixed) isomorphic to order-dependent integer partitions, which apparently are commonly referred to as compositions. There are exactly 2n-1 compositions of every set. For every 1kn, there are exactly (n-1) choose (k-1) compositions of n elements into k sets, preserving the order of the set you started out with. To visualize it, think of the elements of your set put in order and a space between elements that are neighbours in that order; think of your example as A|B|C|D|E. You will notice that there are exactly n-1 possible borders. To create a k-composition, you need only choose k-1 of those possible borders, which may or may not be the way you generated your sets. Summing all (n-1) choose (k-1) for k from 1 to n then gives us 2n-1 as the number of possible compositions.

like image 54
G. Bach Avatar answered Sep 18 '22 20:09

G. Bach