Re: generating subsets
- To: mathgroup at smc.vnet.net
- Subject: [mg21604] Re: [mg21588] generating subsets
- From: BobHanlon at aol.com
- Date: Sun, 16 Jan 2000 22:43:50 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Needs["DiscreteMath`Combinatorica`"]
subSets[elements_List] :=
Module[{k},
Flatten[Table[KSubsets[elements, k], {k, 0, Length[elements]}], 1]]
subSets[{a, b, c}]
{{}, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}}
Length[subSets[{a, b, c, d, e, f, g, h, i}]] // Timing
{0.25*Second, 512}
Bob Hanlon
In a message dated 1/16/2000 5:22:27 AM, mgass at bach.math.csbsju.edu writes:
>I need an _efficient_ procedure for generating all the subsets
>of a given set. The procedure should produce output as follows:
>
>subSets[{a,b,c}]
>produces
>{{}, {a}, {b}, {c}, {a,b}, {a,c}, {b,c}, {a,b,c}}
>
>I have written a recursive procedure to do this, but
>it takes quite a while to generate, say, the 512 subsets
>of a 9 element set and I need to work with somewhat larger
>sets.
>