240 likes | 267 Views
Relations on a Set. Lecture 46 Section 10.1, 10.2, 10.5 Mon, Apr 30, 2007. Relations on a Set. A relation from a set A to a set B is a subset of A B . A relation on a set A is a relation from A to A , i.e., a subset of A A. Examples: Relations on a Set.
E N D
Relations on a Set Lecture 46 Section 10.1, 10.2, 10.5 Mon, Apr 30, 2007
Relations on a Set • A relation from a set A to a set B is a subset of AB. • A relation on a set A is a relation from A to A, i.e., a subset of AA.
Examples: Relations on a Set • Define the relation < on the real numbers as x < y if x is less than y. • Define the relation on the integers as ab if a divides b. • Define the relation on Z as m n if m – n is even. • Define the relation on the set of all statements as p q if p q p.
Inverse Relations • Let R be a relation from A to B. • The inverse relation, denoted R–1, is defined by (a, b) R–1 if and only if (b, a) R.
Examples: Inverse Relations • What is the inverse of the relation < on the real numbers? • What is the inverse of the relation on the integers? • What is the inverse of the relation on Z? What is the inverse of the relation on statements, defined by p q p?
Inverse of p q p • Theorem: If p q p, then p q q. • Proof: • Suppose that p q p. • Then • (p q) p p or p. • (p p) (q p) p p. • T (q p) T. • q p T.
Inverse of p q p • (q p) q T q. • (q q) (p q) q. • F (p q) q. • p qq. • p qq.
Reflexivity, Symmetry, and Transitivity • Let R be a relation on a set A. • R is reflexive if xA,(x, x) R. • R is symmetric if x, yA, (x, y) R (y, x) R. • R is transitive if x, y, zA, (x, y) R and (y, z) R (x, z) R.
Examples • Which of the following relations are reflexive? symmetric? transitive? • ab, on Z. • A B, on (U). • pq, on a set of statements. • a b (mod 10), on Z. • gcd(a, b) > 1, on Z. • pq = p, on a set of statements.
Examples • Which of the following relations are reflexive? symmetric? transitive? • RR, on R. • , on R.
Antisymmetry • A relation R on a set A is antisymmetric if for all a, bA, (a, b) R and (b, a) Ra = b. • This is equivalent to If a b, then (a, b) R (b, a) R.
Examples: Antisymmetry • The following relations are antisymmetric. • a b, on Z+. • A B, on (U). • x y, on R. • A B = A, on (U). • f(x)g(x) = f(x) on the set of all functions from R to R.
Partial Order Relations • A relation R on a set A is a partial order relation if • R is reflexive. • R is antisymmetric. • R is transitive. • We use as the generic symbol for a partial order relation.
Examples: Partial Order Relations • The following relations are partial order relations. • a b, on Z+. • A B, on (U). • x y, on R. • Are the following relations partial order relations? • A B = A, on (U) • f(x)g(x) = f(x) on functions
Comparable Elements and Total Orders • Given a partial order on a set A, two elements a, b A are comparable if a b or b a. • A partial order relation is a total order relation if any two elements are comparable under that relation.
Total Order Relations • Which of the following partial orders are total orders? • x y, on R. • A B, on (U). • a b, on Z+.
Total Order Relations • Which are total orders on R R? • (a, b) (c, d) if a c and b d. • (a, b) (c, d) if a c or b d. • (a, b) (c, d) if a + b c + d. • (a, b) (c, d) if a < c or (a = c and b d).
Example: Total Ordering and Sorting • In order to sort the list {(2, 3), (3, 3), (3, 2), (2, 2)}. it is necessary that be a total ordering of the Point class. • If is not a total order relation, then the results of any sorting algorithm are unpredictable. • Why?
Example: Total Ordering and Sorting • Define a Point object to consist of two doubles. class Point { double x; double y; };
Example: Total Ordering and Sorting • Define operator<() on the Point class as follows. • Then define operator<=() to mean that a < b or a == b. booloperator<(const Point& p, const Point& q) { if (p.x != q.x) return p.x < q.x; else return p.y < q.y; }
C Library Functions • There are two standard library functions, bsearch() and qsort(), that perform a binary search and a quicksort on an array, respectively. • For each function, one parameter is a function compare(a, b) that returns • < 0 if a < b • = 0 if a == b • > 0 if a > b
C Library Functions • Those three requirements are not sufficient to guarantee that operator<=() is a total order relation. • It is the programmer’s responsibility to be sure that it is. • Otherwise, neither bsearch() nor qsort() is guaranteed to work properly.
C Library Functions • SortDemo.cpp