Function Repository Resource:

NextSubset

Source Notebook

Get the next subset of a given set in canonical order

Contributed by: Wolfram Staff (original content by Sriram V. Pemmaraju and Steven S. Skiena)

ResourceFunction["NextSubset"][l,s]

gives the subset of l following the subset s when the subsets are sorted in canonical order.

Details and Options

ResourceFunction["NextSubset"] is limited to sets of unique elements.

Examples

Basic Examples (1) 

Get the subset of {1,2,3} following the subset {1,2} in canonical order:

In[1]:=
ResourceFunction["NextSubset"][{1, 2, 3}, {1, 2}]
Out[1]=

Properties and Relations (2) 

Get the subset following another subset in canonical ordering:

In[2]:=
ResourceFunction["NextSubset"][{1, 2, 3}, {1, 2}]
Out[2]=

This is not the same as the next subset in lexicographic order, as can be obtained with the resource function NextLexicographicSubset:

In[3]:=
ResourceFunction["NextLexicographicSubset"][{1, 2, 3}, {1, 2}]
Out[3]=

The full list of lexicographically-ordered subsets can be obtained using the resource function LexicographicSubsets:

In[4]:=
ResourceFunction["LexicographicSubsets"][{1, 2, 3}]
Out[5]=

Subsets of {1,2,3} in canonical order generated sequentially using NextSubset:

In[6]:=
NestList[ResourceFunction["NextSubset"][{1, 2, 3}, #] &, {}, 7]
Out[6]=

Compare:

In[7]:=
Subsets[{1, 2, 3}]
Out[7]=
In[8]:=
% === %%
Out[8]=

Version History

  • 1.0.0 – 15 July 2020

Related Resources

License Information