Function Repository Resource:

SubsetsWithComplements

Source Notebook

Get a list of subsets of a list along with their complements

Contributed by: Seth J. Chandler

ResourceFunction["SubsetsWithComplements"][list]

gives a List of all possible subsets of list along with their complements.

ResourceFunction["SubsetsWithComplements"][list,n]

gives all subsets containing at most n elements along with their corresponding complements.

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

gives all subsets containing exactly n elements.

ResourceFunction["SubsetsWithComplements"][list,{nmin,nmax}]

gives all subsets containing between nmin and nmax elements.

ResourceFunction["SubsetsWithComplements"][list,nspec,s]

limits the result to the first s subsets.

ResourceFunction["SubsetsWithComplements"][list,nspec,{s}]

gives, if possible, the sth subset and complement.

Details and Options

The permissible argument specifications are exactly the same as for Subsets.

Examples

Basic Examples (2) 

Get all the subsets of a List of characters, paired with their complements:

In[1]:=
ResourceFunction["SubsetsWithComplements"][CharacterRange["a", "e"]]
Out[1]=

Take a List of three characters and get all the one-element subsets thereof and their complements:

In[2]:=
ResourceFunction["SubsetsWithComplements"][
 CharacterRange["a", "c"], {1}]
Out[2]=

Scope (4) 

Get all subsets from a List of five characters with at most two elements, along with their complements:

In[3]:=
ResourceFunction["SubsetsWithComplements"][CharacterRange["a", "e"],
  2]
Out[3]=

Take a List of five characters and get all subsets with between two and three elements and their complements:

In[4]:=
ResourceFunction["SubsetsWithComplements"][
 CharacterRange["a", "e"], {2, 3}]
Out[4]=

Get the first three two-element subsets and complements from a list with six elements:

In[5]:=
ResourceFunction[
 "SubsetsWithComplements"][{"Houston", "Austin", "Dallas", "Fort Worth", "San Antonio", "El Paso"}, {2}, 3]
Out[5]=

Get the seventh two-element subset and complements of a list with six elements:

In[6]:=
ResourceFunction[
 "SubsetsWithComplements"][{"Houston", "Austin", "Dallas", "Fort Worth", "San Antonio", "El Paso"}, {2}, {7}]
Out[6]=

Applications (2) 

Find and show conditions under which each expression in a List will have a value greater than that of any other expression in the list:

In[7]:=
expressions = {a + b, a - b, a*b};
In[8]:=
s = ResourceFunction["SubsetsWithComplements"][expressions, {1}]
Out[8]=
In[9]:=
conditions[op_, opts : OptionsPattern[Query]] := Query[All, MapAt[First, {1}]/*Apply[op]/*Thread/*
   Apply[And]/*(Reduce[#, {a, b}] &), opts]
In[10]:=
dominance = conditions[Greater][s]
Out[10]=
In[11]:=
RegionPlot[dominance, {a, -2, 2}, {b, -2, 2}, PlotLegends -> expressions]
Out[11]=

Do similarly for the lowest value of any expression in the List:

In[12]:=
conditions[Less, FailureAction -> None]
In[13]:=
RegionPlot[
 Evaluate[conditions[Less, FailureAction -> None][s]], {a, -2, 2}, {b, -2, 2}, PlotLegends -> expressions]
Out[13]=

Neat Examples (1) 

A certain classifier predicts the probabilities that a person will be in each of three possible classes. A misclassification results in a utility of –1, except when a person is really in the first class but is misclassified as being a member of the second class. This latter case results in a utility of –u, where u is some non-negative number. For each value of u, find and show the set of classifier probabilities {p1,(1–p1)p2,1–(p1+(1–p1)p2)} that result in each prediction being the one that maximizes expected utility:

In[14]:=
expectedUtilities = {p1, p2, p3} . {{0, -u, -1}, {-1, 0, -1}, {-1, -1, 0}}
Out[14]=
In[15]:=
bestClassificationFunction = Function[u, Evaluate@Function[{p1, p2, p3}, Evaluate[
     Query[Simplify, MapAt[First, {1}]/*Apply[Greater]/*Thread/*
        Apply[And]/*(Reduce[
           And[#, Total[{p1, p2, p3}] == 1, And @@ Map[NonNegative, {p1, p2, p3}]], {p1, p2, p3}] &)][
      ResourceFunction["SubsetsWithComplements"][
       expectedUtilities, {1}]]]]]
Out[15]=
In[16]:=
RegionPlot3D[{bestClassificationFunction[u][p1, p2 (1 - p1), 1 - (p1 + p2 (1 - p1))][[1]], bestClassificationFunction[u][p1, p2 (1 - p1), 1 - (p1 + p2 (1 - p1))][[2]], bestClassificationFunction[u][p1, p2 (1 - p1), 1 - (p1 + p2 (1 - p1))][[3]]}, {p1, 0, 1}, {p2, 0, 1}, {u, 0.1, 8}, PlotStyle -> Opacity[0.25], Mesh -> None, AxesLabel -> {"p1", "p2", "u"}]
Out[16]=

Publisher

Seth J. Chandler

Version History

  • 1.0.0 – 19 October 2020

License Information