Order relations, equivalance relations; basic definitions and properties.

A number of useful algorithmic problems concern an order relation on a set of data objects. Examples of such problems are sorting, maximum, and median. Algorithms for these problems can be expressed quite generally in terms of a comparison function applicable to the data. Thus in one application of an algorithm for, say, sorting, numbers may be ordered by size (by < ) while in another application words are ordered lexicographically, and in a third application customer records are ordered by some complicated function involving size of purchases weighted by recency of purchase. To effectively use such ordering abstraction in our algorithms we need precise expression of the assumptions we are making about the order relation. The following definitions serve this need.

The ordering concepts for the algorithms we have in mind are strict weak order and total order. Intuitively the deal is this: A strict weak order defines an equivalency among the data items, wherein two data items are equivalent if the order relation cannot distinguish between them. In a total order, no two distinct data items are equivalent in this sense. Of the three examples just mentioned, < on numbers and lexicographic order on strings are total orders. The third example might well be be strict weak order which is not total. For instance if the order relation is made by comparing the sum of the two most recent purchases, distinct customers {A 10 10}, {B 5 15}, and {C 10 10} would all be equivalent with respect to this ordering.

A binary function on a type T is a function taking two arguments of type T, f(T a, T b).
A predicate is a function whose return type is boolean, bool f(...).
A relation on a type T is a binary predicate on T, bool f(T a, T b)..

Properties of relations: Let R(T, T) be a relation on T.
R is reflexive if for all a in T, R(a,a) is true.
R is anti-reflexive if for all a in T, R(a,a) is false.
R is symmetric if for all a,b in T, if whenever R(a,b) is true, then R(b,a) is also true.
R is anti-symmetric if for all a,b in T, if whenever R(a,b) is true, then R(b,a) is false.
R is transitive if for all a,b,c in T, if when R(a,b) and R(b,c) are true, then R(a,c) is also true.

Relation R is a partial order if R is anti-reflexive, anti-symmetric, and transitive. Example: R could be < on numbers.
Relation S is an equivalence relation if S is reflexive, symmetric, and transitive. Example: S could be equality of numbers.

Given a partial order R, define relation E_R by bool E_R(T a, T b) { return ! R(a, b) && ! R(b, a); } , in other words, a and b are related by E_R if and only if they are not related in either direction by R. If E_R(a, b) is true, a and b are said to be equivalent with respect to R. A set of members of T is said to be distinct with respect to R if no two elements of the set are equivalent with respect to R.
Relation R is a strict weak order if R is a partial order and E_R is an equivalence relation.
Relation R is a total order on T if R is a strict weak order and for all a, b in T, E_R(a, b) implies a and b are the same element of T (the equivalence classes of E_R are singletons). An (algorithmic) problem is a specification of a function. An algorithm is an implementation of the function.

Problem Max:
template<class T, class SWO>
T& Max(vector<T> A, SWO isLess);
/* A must be a non-empty vector.
* isLess must be a function object implementng a strict weak order.
* Max returns a reference to a largest element of A.
* If A contains multiple largest elements (equivalent with respect to isLess),
* we don't specify which of them is returned.
*/

Question: How many calls to isLess are necessary in an algorithm solving Max?

Problem maxmin:
template<class T, class SWO>
max_min(T& M, T& m, vector<T> A, SWO isLess);
/* A must be a non-empty vector.
* isLess must be a function object implementng a strict weak order.
* M is set to a largest element of A with respect to isLess.
* m is set to a least element of A with respect to isLess.
*/

Question: How many calls to isLess are necessary in an algorithm implementing max_min?

Problem max2:
template<class T, class SWO>
max2(T& M1, T& M2, vector<T> A, SWO isLess);
/* A must be a vector of size at least 2.
* isLess must be a function object implementng a strict weak order.
* M1 is set to a largest element of A with respect to isLess.
* M2 is set to a second largest element of A with respect to isLess.
* M1 and M2 will be references to elements of A in distinct positions
* but their values may be the same or be equivalent with respect to isLess.
*/

Question: How many calls to isLess are necessary in an algorithm implementing max2?