Function Repository Resource:

OrderlessCombinations

Source Notebook

Generate all possible orderless lists of a given length with a given set of elements

Contributed by: Jon McLoone

ResourceFunction["OrderlessCombinations"][list,n]

gives all possible orderless sets comprised of the elements of list up to length n.

ResourceFunction["OrderlessCombinations"][list,{n}]

gives sets of exactly length n.

ResourceFunction["OrderlessCombinations"][list,{n,m}]

gives sets containing between n and m elements.

Examples

Basic Examples (2) 

There are nine possible orderless sets of up to two elements chosen from three possible elements:

In[1]:=
ResourceFunction["OrderlessCombinations"][{a, b, c}, 2]
Out[1]=

There are six possible orderless sets of exactly two elements chosen from three possible elements:

In[2]:=
ResourceFunction["OrderlessCombinations"][{a, b, c}, {2}]
Out[2]=

Scope (2) 

Get length four sets from a list of length two:

In[3]:=
ResourceFunction["OrderlessCombinations"][Range[2], {4}]
Out[3]=

Elements need not be integers:

In[4]:=
ResourceFunction["OrderlessCombinations"][{"dog", "cat", "bird"}, 3]
Out[4]=

Applications (1) 

A company makes three different heating units. The customer needs a row of five heating units for a large room. How many different purchase combinations are available to the customer:

In[5]:=
Length[ResourceFunction["OrderlessCombinations"][Range[3], {5}]]
Out[5]=

Properties and Relations (1) 

The same sets can be generated by sorting and deduplicating the output of Tuples, but OrderlessCombinations is faster, and uses much less memory for larger problems:

In[6]:=
AbsoluteTiming@
 Length[ResourceFunction["OrderlessCombinations"][Range[4], {12}]]
Out[6]=
In[7]:=
AbsoluteTiming@Length[DeleteDuplicates[Sort /@ Tuples[Range[4], 12]]]
Out[7]=

Publisher

Jon McLoone

Version History

  • 1.0.0 – 25 October 2022

Related Resources

License Information