All possible sample combinations from a list

Hello,

I would like your insight on a problem I have. It might be simple but I’m stuck. Let’s say I have a list of 10 items and I want to get all possible combinations of a sample of 5 items from them. Any ideas how to do this?

Thank you

Hi,

Here is a possibility with Cross Reference

The result is a tree with (10 choose 5) = 252 branches.

FiveChooseTen.gh (10.3 KB)

Thank you, it seems to work. Are you aware if there is a limit to the number of items grasshopper can calculate? For example, if I bump the list to 20 items and the sample to 10, that would generate 184756 branches of 10 items, 1847560 in total.

The amount of items is not a problem, but the Cross Reference component seems to be extremely slow, so I made a C# component to do the job - takes less than 10 seconds.

However it is absolutely not practical to modify the number of items chosen, I’m sure something better exists.

TenChooseTwenty.gh (15.0 KB)

There we (there we (there we (there we… go) go) go) go

(with recursion).

RecursiveNChoosek.gh (8.5 KB)

Get the general case.

Entry level: works on collections of Type int (*) for 6 results: permutations/combinations/kcombos/cats and dogs.

PermutationsAndCombinations_V1.gh (120.7 KB)

(*) but the Methods used accept any Type - see inside

Thank you everybody for your valuable help!