Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Array and Bit-Vector Sets

In this section we consider finite sets over a finite universe. Specifically, the universe we consider is tex2html_wrap_inline67264, the set of integers in the range from zero to N-1, for some fixed and relatively small value of N.

Let tex2html_wrap_inline67278 be the universe. Every set which we wish to represent is a subset of U. The set of all subsets of U is called the power set  of U and is written tex2html_wrap_inline67286. Thus, the sets which we wish to represent are the elements of tex2html_wrap_inline67286. The number of elements in the set U, written |U|, is N. Similarly, tex2html_wrap_inline67296. This observation should be obvious: For each element of the universal set U there are only two possibilities: Either it is, or it is not, a member of the given set.

This suggests a relatively straightforward representation of the elements of tex2html_wrap_inline67286--an array of Boolean values, one for each element of the universal set. By using array subscripts in U, we can represent the set implicitly. I.e., i is a member of the set if the tex2html_wrap_inline58387 array element is true.

Program gif declares the class SetAsArray. This class uses an array of length tex2html_wrap_inline67308 to represent the elements of tex2html_wrap_inline67286 where tex2html_wrap_inline67278. A SetAsArray is a Set. Therefore, it supports the basic operations of searchable containers including Insert, IsMember, and Withdraw.

   program28238
Program: SetAsArray Class Definition

In addition, Program gif overloads the operators +, *, -, ==, and <=. The first three operators correspond to set union, set intersection, and set difference (respectively). The last two are used to compare two sets and to determine whether one set is a subset of another.




next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.