Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Comparing Sets

There is a special family of operators for comparing sets. Consider two sets, say S and T. We say that S is a subset  of T, written tex2html_wrap_inline62250, if every element of S is also an element of T. If there is at least one element of T that is not also an element of S, we say that S is a proper subset   of T, written tex2html_wrap_inline66234. We can also reverse the order in which the expressions are written to get tex2html_wrap_inline66236 or tex2html_wrap_inline66238, which indicates that T is a (proper) superset    of S.

The set comparison operators follow the rule that if tex2html_wrap_inline62250 and tex2html_wrap_inline62252 then tex2html_wrap_inline66248, which is analogous to a similar property of numbers: tex2html_wrap_inline66250. However, set comparison is unlike numeric comparison in that there exist sets S and T for which neither tex2html_wrap_inline62250 nor tex2html_wrap_inline62252! For example, clearly this is the case for tex2html_wrap_inline66260 and tex2html_wrap_inline66262. Mathematically, the relation tex2html_wrap_inline66264 is called a partial order  because there exist some pairs of sets for which neither tex2html_wrap_inline62250 nor tex2html_wrap_inline62252 holds; whereas the relation tex2html_wrap_inline58790 (among integers, say) is a total order.

Program gif defines the methods isEQ and isSubset each of which take argument that is assumed to be a SetAsArray instance. The former tests for equality and the latter determines whether the relation tex2html_wrap_inline66264 holds between this and set. Both operators return a boolean result. The worst-case running time of each of these operations is clearly O(N).

   program27537
Program: SetAsArray class isEQ and isSubset methods.

A complete repertoire of comparison methods would also include methods to compute tex2html_wrap_inline66276, tex2html_wrap_inline66278, tex2html_wrap_inline66280, and tex2html_wrap_inline60646. These operations follow directly from the implementation shown in Program gif (Exercise gif).


next up previous contents index

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