next up previous contents index CD CD Algorithms
Next: Generating Partitions Up: Combinatorial Problems Previous: Generating Permutations

Generating Subsets

  

  figure12120

Input description: An integer n.

Problem description: Generate (1) all or (2) a random or (3) the next subset of the integers tex2html_wrap_inline28161 .

Discussion: A subset describes a selection of objects, where the order among them does not matter. Many of the algorithmic problems in this catalog seek the best subset of a group of things: vertex cover seeks the smallest subset of vertices to touch each edge in a graph; knapsack seeks the most profitable subset of items of bounded total size; and set packing seeks the smallest subset of subsets that together cover each item exactly once.         

There are tex2html_wrap_inline28163 distinct subsets of an n-element set, including the empty set as well as the set itself. This grows exponentially, but at a considerably smaller rate than the n! permutations of n items. Indeed, since tex2html_wrap_inline28167 1,048,576, a brute-force search through all subsets of   20 elements is easily manageable, although by n=30, tex2html_wrap_inline28171 1,073,741,824, so you will certainly be pushing things.

By definition, the relative order among the elements does not distinguish different subsets. Thus tex2html_wrap_inline28173 is the same as tex2html_wrap_inline28175 .   However, it is a very good idea to maintain your subsets in a sorted or canonical order, in order to speed up such operations as testing whether two subsets are identical or making them look right when printed.

As with permutations (see Section gif), the key to subset generation problems is establishing a numerical sequence among all tex2html_wrap_inline28177 subsets. There are three primary alternatives:

Generation problems for two closely related problems arise often in practice:

Implementations: Nijenhuis and Wilf [NW78] provide efficient Fortran   implementations of algorithms to construct random subsets and to sequence subsets in Gray code and lexicographic order. They also provide routines to construct random k-subsets and sequence them in lexicographic order. See Section gif for details on ftp-ing these programs. Algorithm 515 [BL77] of the Collected Algorithms of the ACM is another Fortran implementation of lexicographic k-subsets, available from Netlib     (see Section gif).

An exciting WWW site developed by Frank Ruskey of the University of Victoria contains a wealth of material on generating combinatorial objects of different types, including permutations, subsets, partitions, and certain graphs. Specifically, it provides an interactive interface that lets you specify which type of objects you would like to construct and then returns the objects to you. Check this out at http://sue.csc.uvic.ca/ tex2html_wrap_inline28229 cos/.

Combinatorica [Ski90] provides Mathematica implementations     of algorithms to construct random subsets and to sequence subsets in Gray code, binary, and lexicographic order. They also provide routines to construct random k-subsets and strings, and sequence them lexicographically. See Section gif for further information on Combinatorica.

Notes: The primary expositions on subset generation include [NW78, RND77, Rus97]. Wilf [Wil89] provides an update of [NW78], including a thorough discussion of modern Gray code generation problems.

Gray codes were first developed [Gra53] to transmit digital   information in a robust manner over an analog channel.   By assigning the code words in Gray code order, the ith word differs only slightly from the (i+1)st, so minor fluctuations in analog signal strength corrupts only a few bits. Gray codes have a particularly nice correspondence to Hamiltonian cycles on the hypercube.     See any of the references above for details. An exposition on the more general problem of constructing Gray codes for k items (instead of tex2html_wrap_inline28233 subsets) appears in [Man89].

The popular puzzle Spinout, manufactured by Binary Arts Corporation,   can be solved using Gray codes.

Related Problems: Generating permutations (see page gif), generating partitions (see page gif).    


next up previous contents index CD CD Algorithms
Next: Generating Partitions Up: Combinatorial Problems Previous: Generating Permutations

Algorithms
Mon Jun 2 23:33:50 EDT 1997