It is well known that the array of subsets of even small set is very big. This leads to problems with machine memory. Is there an effective way to generate subsets sequentially?

  • $\begingroup$ Have you seen this? $\endgroup$ Jun 16, 2016 at 13:41
  • $\begingroup$ Is this a Mathematica-specific question? $\endgroup$
    – Yves Klett
    Jun 16, 2016 at 13:41
  • $\begingroup$ Yes, it's about Mathematica. The answer is interesting, but unwieldy enough. May be in a new version there is a better way? $\endgroup$
    – lesobrod
    Jun 16, 2016 at 13:48
  • 4
    $\begingroup$ Related: Alternative to Subsets to generate k-combinations $\endgroup$
    – jkuczm
    Jun 16, 2016 at 22:52
  • $\begingroup$ @jkuczm That seems close enough to consider a duplicate. Do you disagree? $\endgroup$
    – Mr.Wizard
    Jul 11, 2016 at 16:45

1 Answer 1


The Subsets function has a third argument to return only some subsets. For example,

Subsets[set, All, {k1, k2}]

returns the k1th through the k2th subsets. This allows for easy sequential generation in blocks. Please check the documentation for details.

  • $\begingroup$ Great! I guess to use Nest and hope it will work. $\endgroup$
    – lesobrod
    Jun 16, 2016 at 14:38
  • $\begingroup$ @lesobrod I assume this won't use extra memory, but I haven't actually tested ... you better test this first. $\endgroup$
    – Szabolcs
    Jun 16, 2016 at 14:46
  • $\begingroup$ I assume that NextSubset is implemented carefully enough that it does not generate the entire set just to find the next element. Be sure to click on the Details in the documentation to understand the usage. $\endgroup$
    – Bill
    Jun 16, 2016 at 19:06
  • $\begingroup$ @Bill You mean NextSubset from Combinatorica? $\endgroup$
    – Szabolcs
    Jun 16, 2016 at 19:08
  • $\begingroup$ @Szabolcs Yes, which was why I included the hint to click on Details. $\endgroup$
    – Bill
    Jun 17, 2016 at 16:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.