18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Constructing All Subsets<br />

Next: Constructing All Permutations Up: Backtracking Previous: Backtracking<br />

Constructing All Subsets<br />

To design a suitable state space for representing a collection of combinatorial objects, it is important to<br />

know how many objects you will need to represent. How many subsets are there of an n-element set, say<br />

the integers ? <strong>The</strong>re are exactly two subsets for n=1 namely and , four subsets for n=2,<br />

and eight subsets for n=3. Since each new element doubles the number of possibilities, there are<br />

subsets of n elements.<br />

Each subset is described by stating which elements are in it. <strong>The</strong>refore, to construct all subsets, we can<br />

set up an array/vector of n cells, where the value of is either true or false, signifying whether the ith<br />

item is or is not in the given subset. To use the notation of the general backtrack algorithm,<br />

, and A is a solution whenever .<br />

Using this state space representation, the backtracking algorithm constructs the following sequence of<br />

partial solutions in finding the subsets of . Final solutions, i.e. complete subsets, are marked with<br />

a *. False choices correspond to dashes in the partial solution, while true in position i is denoted by i<br />

itself:<br />

Trace through this example carefully to make sure you understand the backtracking procedure. <strong>The</strong><br />

problem of generating subsets is more thoroughly discussed in Section .<br />

<strong>Algorithm</strong>s<br />

Mon Jun 2 23:33:50 EDT 1997<br />

file:///E|/BOOK/BOOK2/NODE85.HTM [19/1/2003 1:29:25]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!