file: 05.2.ordersAndComparability.txt author: Bob Muller date: February 13, 2014 CS102 Computer Science II Lecture Notes for Meeting 2 of week 5 Tuesday October 15, 2013 ---------------------------------------------------------------------- Sets & Orders We will often be interested in maintaining ordered structures of various kinds. These notes cover the background ideas. Study them and stash them away for later reference. ----------------------- Def: A SET is a collection of elements with no duplicates. For example: States = {MA, CT, NY, ... } denotes a set of 50 unique symbols denoting states. We use "States" as a name of the set for convenience. We will sometimes use a single capital letter A, B, C or X, Y, Z, as names for sets; we'll use lowercase letters near the front of the alphabet (a, b, c, ...) to denote particular elements of sets and lowercase letters near the end of the alphabet (x, y, z, ...) as VARIABLES that may represent elements of a set. Notes: 1. There are no constraints on the elements other than being able to identify them. 2. The elipsis "..." in the example above is pronounced "and so on". 3. Sets can be empty, finite or infinite. Def: An ASSERTION is simply a statement that is either true or false. Element/membership Notation: We can assert that an item is an element of a set using the \in notation: MA \in States --- this assertion happens to be true. BB \in States --- this assertion happens to be false. We often wish to make assertions about elements of sets: MA is in New England. MA is populated. We'll often use variables in assertions: Example: The assertion x \in States is true if x = MA. It is false if x = BB. Quantification: We can use the QUANTIFIERS \forall and \exists to make assertions about ALL elements of a set or SOME element of a set. \forall x \in X . Assertion(x) means Assertion(x) for every element of X. \exists x \in X . Assertion(x) means that there is some element of X such that Assertion(x) Examples: exists x \in States . x is in New England --- true forall x \in States . x is populated --- true forall x \in States . x is in New England --- false Comprehension Notation: { x | Assertion(x) } --- the vertical bar is pronounced "such that" Example: Natural Numbers: N = { n | n is a non-negative number } --- an infinite set. Subset and Set Equality Notation: X \subseteq Y means \forall x \in X . x \in Y, X = Y means X \subseteq Y and Y \subseteq X. Operations on Sets: Let X and Y be sets. Union: X \union Y = { x | x \in X or x \in Y } Intersection: X \inter Y = { x | x \in X and x \in Y } Examples: {MA, NY, IN} \union {GA, FL, MA} = {MA, NY, IN, GA, FL} {MA, NY, IN} \inter {GA, FL, MA} = {MA} Sequences over a set A Let e denote an empty sequence and let w denote a sequence. Sequence: A^* = { w | w = e or w = aw' where a in A & w' in A^* } Example: {a, b}^* = {e, be, bbe, bbbe, bbae, ...} Convention: We usually omit the e at the end! So: {a, b}^* is written {e, b, bb, bbb, bba, ... } Powerset: P(X) = { Y | Y \subseteq X } Example: P({MA, NY, IN}) = {{}, {MA}, {NY}, {IN}, {MA, NY}, {MA, IN}, {NY, IN}, {MA, NY, IN}} Note: If X has N elements then P(X) has 2^N elements. Tuples k-tuples: (a1, a2, ..., ak) is a k-tuple (a pair if k=2, a triple if k=3, ...). We're going to stick to 2-tuples or pairs. Let X and Y be sets. Product: X x Y = { (x, y) | x \in X and y \in Y } Example: Capitals = {Boston, Albany, Atlanta} {MA, NY, GA} x Capitals = {(MA, Boston), (MA, Albany), (MA, Atlanta), (NY, Boston), (NY, Albany), (NY, Atlanta), (GA, Boston), (GA, Albany), (GA, Atlanta)} Binary Relations Let X and Y be sets. A BINARY RELATION R on X and Y is R any subset of X x Y. Examples: R0 = {} --- the empty relation R1 = {MA, NY, GA} x Capitals --- the everywhere defined relation R2 = {(MA, Boston), (NY, Albany), (NY, Atlanta), (GA, Atlanta)} R3 = {(MA, Boston), (NY, Albany), (GA, Atlanta)} Sometimes we say "R is a binary relation on X" to mean that R is a binary relation on X and X, i.e., R \subseteq of X x X. Relations are extremely general and useful. They are the basis of of a widely used form a databases (called a -relational- database, naturally enough), they are the standard way to represent GRAPHS, they are the basis of orders and they are the basis of maps (aka functions). Properties of Relations Let A be a set and let R be a binary relation on A. I.e., R \subseteq of A x A. - R is REFLEXIVE iff for all a in A . (a, a) in R, - R is TRANSITIVE iff for all a, b, c in A . if (a, b) in R and (b, c) in R then (a, c) in R. A relation R on A that is reflexive and transitive is called a PREORDER of A. Example: Let A = {a, b, c}. Then R0 = {(a, a), (b, b), (c, c)} is a reflexive and transitive relation on A. I.e., R0 is a preorder of A. Notation: Let A be a set and let R be a preorder of A. Instead of writing (a, b) in R, we usually write: a <=_R b or, when R is implicit: a <= b. The Symmetry Property - R is SYMMETRIC iff for all a, b in A . if (a, b) in R then (b, a) in R, - R is ANTISYMMETRIC iff for all a, b in A . if (a, b) in R and (b, a) in R then a = b. Example: Let A = {a, b, c}. Then - R4 = {(a, b)} --- is antisymmetric - R5 = {(a, b), (b, a)} --- is symmetric - R6 = {(a, b), (b, a), (a, c)} --- is neither - R7 = {(a, a), (b, b), (c, c), (d, d)} --- is both! Depending on symmetry, we get two important categories of relations. Partial Orders Let A be a set and let R be a binary relation on A. R is a PARTIAL ORDER ("on A", "of A") if R is reflexive, antisymmetric and transitive. Sometimes we say: A is partially ordered by R. Examples: Let A = {a, b, c}. Then 1. R0 = {(a, a), (b, b), (c, c)} and 2. R2 = {(a, a), (b, b), (c, c), (c, a), (c, b), (b, a)} are -different- partial orders on A. (Confirm that all 3 conditions hold.) Note: - In the simple partial order {(a, a), (b, b), (c, c)}, we can write a <= a, b <= b, and c <= c but we cannot write a <= b e.g., because in this order they are INCOMPARABLE. Total Orders Let A be a set and let R be a partial order on A. R is a TOTAL ORDER on A iff for all a, b in A either (a, b) in R or (b, a) in R. Example: Let Letters = {J, B, Z, U, S, G, I, P, N, ... } the (unordered) set of 26 letters and let alphabeticalOrder = {(Z, Z), (Y, Y), (Y, Z), // pronounced: Y less than or equal to Y, Y less // than or equal to Z (X, X), (X, Y), (X, Z), (W, W), (W, X), (W, Y), (W, Z), ..., (A, A), (A, B), (A, C), ..., (A, Z)} is the total order that we learned by singing the familiar song as 4 year-olds. Notice that this is a total order because every letter can be compared to every other letter. Notes: 1. When a set {a, b, c, ...} is totally ordered the ordering is sometimes called a chain: a <= b <= c <= ... In Java, a total order on a class is called the class's "NATURAL ORDER". The interface java.lang.Comparable specifies a single method: int compareTo(T o); When creating a class T, we will often want to implement the Comparable interface. When doing so we should be careful to ensure that our implementation of compareTo: 1. defines a total order on the objects of the class T. This means that for every pair of instances e1 and e2 of T, both e1.compareTo(e2) and e2.compareTo(e1) are defined, and 2. compareTo is CONSISTENT with equals. This means that for every pair of instances e1 and e2 of the class T, that e1.compareTo(e2) == 0 gives the same boolean result as e1.equals(e2). Read the description of the Java interface Comparable on the Oracle web site and Bloch Item 12 (p. 62) for good discussions. Lexicographic Orders When we look up a word in the dictionary we are actually making use of a total order on words that is built on the underlying order on the alphabet. This is the most widely known example of a more general way to order sequences of things called lexicographic ordering. Let A be a set partially ordered by R. We will now define a partial order on the set of sequences of elements of A. You'll remember from above that A^* denotes the set of sequences of elements of A. We'll call the new ordering S. Let u, v, w be elements of A^*. S = { (u, v) | either u = e (i.e., u is empty) or u = aw, v = a'w' and either (a, a') in R or (w, w') in S} Note: that the partial ordering on A^* is "derived from" the underlying ordering on A. Example: Let A = {a, 8, %} and let R = {(a, a), (8, 8), (%, %), (a, 8), (a, %), (8, %)}. The set A^* = {e, a, 8, %, 8a, %%, %a8, aaa, %a%a, %a%8, ... }. The derived lexicographic ordering is: S = {(e, e), (e, a), ..., (e, %a%a), ..., (a, 8), (a, %), ..., (8a, %), ..., (%a%, %a%a), (%a%a, %a%8), ...} Notes: 1. The derived relation S will inherit the properties of the underlying relation R. If R is a partial order then S will be a partial order. If R is a total order then S will be one too. 2. Lexicographic orders can be derived from any collection of elements of a partially ordered set, not just sequences. See the example below. Example: Define a lexicographic total order on Points based on the total order on integers. public class Point implements Comparable { private int x, y; public getX() { return x; } public getY() { return y; } ... // A simple lexicographic ordering on Points. // public int compareTo(Point other) { int testX = this.x - other.getX(); if (testX != 0) return testX; else return this.y - other.getY(); } } Equivalence Let A be a set and let R be a relation on A. R is an EQUIVALENCE on A if R is reflexive, SYMMETRIC and transitive. Example: Let A = {a, b, c}. Then R1 = {(a, a), (b, b), (c, c), (a, c), (c, a)} is an equivalence relation on A. (Confirm that the three conditions hold.) In Java, the root class Object defines an equals method so every Java class has access to an equals method. When creating a new class C (which by definition is a subclass of Object) you will often want to override the Object version of equals by defining a version that is customized for your new class. See Item 8 of Bloch (p. 33) for a discusion of when to override equals. When defining your own version of equals, make sure that it has the required properties!