Data Structures, Algorithms, & Applications in C++
Chapter 1, Exercise 29
We may represent a subset of n elements
by the one-dimensional array x[1:n],
where
x[j] is one if element
j is included in the subset and
x[j] is zero if element
j is not included in the subset.
To output the subsets recursively, we define a method
subsets(int i)
which outputs all
x[1:n] with preset values for
x[1:i-1] and
x[i:n] taking on all possible 0 and 1
values. The invocation
subsets(1)
will output all subsets.
The code is
given below.
/** generate all subsets of n elements */
package applications;
public class AllSubsets
{
// class data member
static int [] x; // subset vector
/** define array x and invoke private method subsets */
public static void allSubsets(int n)
{
x = new int [n + 1];
// output all subsets of x[1:n]
subsets(1);
}
/** output x[1:i-1] followed by all subsets of x[i:x.length-1] */
private static void subsets(int i)
{
if (i == x.length - 1)
{// x[x.length - 1] can be 0 or 1
// output subset without last element
x[x.length - 1] = 0;
for (int j = 1; j <= x.length - 1; j++)
System.out.print(x[j] + " ");
System.out.println();
// output subset with last element
x[x.length - 1] = 1;
for (int j = 1; j <= x.length - 1; j++)
System.out.print(x[j] + " ");
System.out.println();
return;
}
// leave element i out
x[i] = 0;
// generate all subsets with i excluded
subsets(i + 1);
// put element i into subset
x[i] = 1;
// generate all subsets with i included
subsets(i + 1);
}
/** test program */
public static void main(String [] args)
{
allSubsets(4);
}
}
The above code may be modified if we are to output element identifiers
for the selected elements rather than 0/1 vectors.