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

Applications

One of the most important applications of partitions involves the processing of equivalence relations. Equivalence relations arise in many interesting contexts. For example, two nodes in an electric circuit are electrically equivalent if there is a conducting path (a wire) connecting the two nodes. In effect, the wires establish an electrical equivalence relation over the nodes of a circuit.

A similar relation arises among the classes in a Python program. Consider the following Python code fragment:

class A(object): pass
class B(A): pass
class C(A): pass
class D(B): pass
class E(C): pass
The classes A, B, C, D and E are equivalent in the sense that they are all subclasses of the class A. (By definition, a class is a subsclass of itself.) In effect, the class declarations establish an equivalence relation over the classes in a Python program.

Definition (Equivalence Relation)  An equivalence relation   over a universal set U is a relation tex2html_wrap_inline67245 with the following properties:
  1. The relation tex2html_wrap_inline67245 is reflexive . That is, for every tex2html_wrap_inline67249, tex2html_wrap_inline67251.
  2. The relation tex2html_wrap_inline67245 is symmetric . That is, for every pair tex2html_wrap_inline67249 and tex2html_wrap_inline67257, if tex2html_wrap_inline67259 then tex2html_wrap_inline67261.
  3. The relation tex2html_wrap_inline67245 is transitive . That is, for every triple tex2html_wrap_inline67249, tex2html_wrap_inline67257 and tex2html_wrap_inline67269, if tex2html_wrap_inline67259 and tex2html_wrap_inline67273 then tex2html_wrap_inline67275.

An important characteristic of an equivalence relation is that it partitions the elements of the universal set U into a set of equivalence classes . That is, U is partitioned into tex2html_wrap_inline66961, such that for every pair tex2html_wrap_inline67249 and tex2html_wrap_inline67257, tex2html_wrap_inline67259 if and only if x and y are in the same element of the partition. That is, tex2html_wrap_inline67259 if there exists a value of i such that tex2html_wrap_inline67297.

For example, consider the universe tex2html_wrap_inline67299. and the equivalence relation tex2html_wrap_inline67245 defined over U defines as follows:

  multline29538

This relation results in the following partition of U:

displaymath67239

The list of equivalences in Equation gif contains many redundancies. Since we know that the relation tex2html_wrap_inline67245 is reflexive, symmetric and transitive, it is possible to infer the complete relation from the following list

displaymath67240

The problem of finding the set of equivalence classes from a list of equivalence pairs is easily solved using a partition. Program gif shows how it can be done using the PartitionAsForest class defined in Section gif.

   program29545
Program: Application of disjoint sets--finding equivalence classes.

The algorithm first gets a positive integer n from the input and creates a partition, p, of the universe tex2html_wrap_inline67309 (lines 4-5). As explained in Section gif, the initial partition comprises n disjoint sets of size one. That is, each element of the universal set is in a separate element of the partition.

Each iteration of the main loop processes one equivalence pair (lines 6-15). An equivalence pair consists of two numbers, i and j, such that tex2html_wrap_inline67311 and tex2html_wrap_inline67313. The find operation is used to determine the sets s and t in partition p that contain elements i and j, respectively (lines 10-11).

If s and t are not the same set, then the disjoint sets are united using the join operation (lines 12-13). Otherwise, i and j are already in the same set and the equivalence pair is redundant (line 15). After all the pairs have been processed, the final partition is printed (line 16).


next up previous index

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