Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

Representing Partitions

Program gif defines the Partition class. The abstract Partition class extends the abstract Set class defined in Program gif. Since a partition is a set of sets, it makes sense to derive Partition from Set. The two methods, find and join, correspond to the partition operations described above.

   program28158
Program: Abstract Partition class.

The elements of a partition are also sets. Consequently, the objects contained in a Partition are also derived from the Set class. The find method of the Partition class expects as its argument an int and returns the set which contains the specified item.

The join method takes two arguments, both of them references to sets. The two arguments are expected to be distinct elements of the partition. The effect of the join operation is to remove the specified sets from the partition and replace them with a set which represents the union of the two.


next up previous index

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