Re: find subsets

• To: mathgroup at smc.vnet.net
• Subject: [mg104736] Re: find subsets
• From: Helen Read <hpr at together.net>
• Date: Sun, 8 Nov 2009 06:47:22 -0500 (EST)
• References: <hd3mn9\$9l4\$1@smc.vnet.net>

```r_poetic wrote:
> Hello,
> here is a quick question from a novice.
>
> First, generate a list of subsets using Subsets or KSubsets
> functions.
>
> Ns = KSubsets[{a,b,c,d,e},3] = {{a,b,c},{a,b,d},{a,b,e},{a,c,d}, etc.}

It's a good practice to use names that begin with a lower case letter,
to avoid conflict with the names of built-in symbols and functions.

> How can one infer from that list a list of its members that contain
> given elements, e.g. a list of members that contain b, or those that
> contain both b and c? For illustration:
>
> Some_function[Ns, b and c] = { {a,b,c},{b,c,d},{b,c,e}}
>
> I keep thinking there must be a direct way to do this using Select or
> some other function?

Select together with MemberQ will do the job. First, generate the subsets:

sets = Subsets[{a, b, c, d, e}, {3}]

This will select the ones that contain b:

Select[sets, MemberQ[#, b] &]

This will select the ones that contain both b and c:

Select[sets, (MemberQ[#, b]) && (MemberQ[#, c]) &]

And this will select the ones that contain b or c:

Select[sets, (MemberQ[#, b]) || (MemberQ[#, c]) &]

--