# Optimized subset algorithm implementation

I was reading a paper from Mathematica Journal.It mentions a code for calculating subsets.

    subSets[s_List] := Variables /@ (Expand[Times @@ (1 + s)] /. Plus -> List) // Sort


Then it mentions the following code as optimized code,

subSetsV[s_List] :=  Distribute[{{}, {#}} & /@ s, List, List, List, Union] // Sort


My problem is I could not get both of these.Can someone please help me with bit of explanation of these snippets.

-
For the first one, why don't you assign s a concrete instance of a list (say {a,b,c} - assuming a, b, c haven't been assigned values) and work through the expression inside out? Start with (1+{a,b,c}), then Times@@(1+{a,b,c}) and so on and so forth. – Aky Jun 11 '13 at 16:54
The second one is clever, but to find the details of that form of Distribute you'll need to look in the details section of the docs. – rcollyer Jun 11 '13 at 17:08
Of course, if the entries of the input list are numeric, then the first snippet won't work. I'd have done Sort[Variables[List @@ Expand[Times @@ (1 + s)]]], tho. – J. M. Jun 11 '13 at 17:18
For example, in second what does continuous List means? This example is as it is in doc as well. – Rorschach Jun 11 '13 at 17:55
To help you learn how to catch fish, execute these in sequence: s = {1, 2, 3};, {{}, {#}} & /@ s, Distribute[{{}, {#}} & /@ s, List] (same as Distribute[{{}, {#}} & /@ s, List, List], why?), and then finally Distribute[{{}, {#}} & /@ s, List, List, g, f]. You should be able to see a few patterns. – J. M. Jun 11 '13 at 18:10