-2

When getting a list of X elements, how can I get all doubles, triples, ... ( Y ) combinations of these elements ?
Y being the size of the required combinations. Ex : if Y = 2, I need to get all of the possible pairs.
I must not give the same combinations twice ( ex : [a, b] and [b, a] are the same combination )

  • 1
    Look at combination in [implementation-of-permutation-combinations-and-powerset-in-c++](https://stackoverflow.com/a/25556248/2684539) – Jarod42 Nov 07 '17 at 08:55
  • Please [edit] your question to show [the code you have so far](http://whathaveyoutried.com). You should include at least an outline (but preferably a [mcve]) of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Nov 07 '17 at 15:23

2 Answers2

2

Take a copy of the list.

If the list is empty, there are no combinations.

To get all combinations of size one, look at each element in turn.

To get all combinations of size n+1, first remove the first element. Then get all combinations of size n of the rest of the list, plus that first element. Then get all combinations of size n+1 of the rest of the list, and don't add the first element.

And then you are done.

You can get fancy and merely pretend to copy/remove elements for optimization sake.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • should I use recursivity is the size of the array is equal or greater than 2 ? And how does it allow me to avoid having duplicates ? Must I delete all of the elements one by one in order to avoid them ? – Matthieu Raynaud de Fitte Nov 07 '17 at 04:00
1

You can iterate t from 2 to Y, and create an array A with the size X fill with X-t 0s in the front and t 1s in the back, then with the code below:

do{
    //1s in array A now correspond to a valid combination
}while(std::next_permutation(A,A+X));

The loop will stop when all combination with size t are iterated

next_permutation is in header algorithm, it will reorder the array to the next lexicographically greater permutation or return false if the array is already in the lexicographically greatest permutation. Its complexity is O(n), since you also need to iterate through the array once, so it wouldn't be a problem. Total complexity for the whole process will be bounded by O(2^n*n).

So here is an example pseudo code

D[X] = {1,2,3,4} Y = 3 //the input
For t = 2,3,..,Y
    A[X] = {0,...,0,1,...,1} // X - t 0s and t 1s 
    Do
        For j = 0,1,...,X-1
            if A[j] == 1
                output D[j]
            end if
        end for
        output newline
    While next_permutation(A,A+X)
end for

The output will looks like

3 4
2 4
2 3
1 4
1 3
1 2
2 3 4
1 3 4
1 2 4
1 2 3
Brian Su
  • 11
  • 2
  • Will this work with structures ? And does this allow me to have only unique combinations ? ( and not have [a, b] and [b, a] in the results ) – Matthieu Raynaud de Fitte Nov 07 '17 at 04:16
  • Because it will genarate lexicographically greater permutation, so it wouldn't give a same permutation. – Brian Su Nov 07 '17 at 04:23
  • And yes, it will work with structure since only 0s and 1s are permutated. – Brian Su Nov 07 '17 at 04:27
  • but how do I use it ? How do I choose the size of the wished combinations ? And how can I access them in the loop ? If I understand ( pretty sure I do not ), I would get all of the combinations, no matter the size and have to ignore the one that do not fit the required size witch does not seem very efficient – Matthieu Raynaud de Fitte Nov 07 '17 at 04:33
  • 1
    First, a outer for loop will iterate through all combination size you want (eg. 2,3,...,Y), then you fill same amount of 1s in the back of another arry. With the do loop above, you will get all possible combination with this size, once for each of them. Let's say if the array looks like {0,1,0,1,1}, then this mean a combination formed by the second, the fourth, and the fifth element. You won't have to ignore any of them, since all of them fulfill your need. – Brian Su Nov 07 '17 at 04:45
  • Could you please edit your reply to show me what you mean ? I am struggling to understand. If possible with an array of unique numbers ( not just 0 or 1 ). – Matthieu Raynaud de Fitte Nov 07 '17 at 05:04
  • The 01 array is a new array you created. I'm still in school so I couldn't write more now, sorry. I can give you an implementation example later. – Brian Su Nov 07 '17 at 05:08