Relations a Relation (R) Between Sets a and B Is a Subset of a × B. Often We Write Xry to Mean (X, Y) ∈ R and Say “X Is
Total Page:16
File Type:pdf, Size:1020Kb
Relations A relation (R) between sets A and B is a subset of A × B. Often we write xRy to mean (x, y) ∈ R and say “x is related to y”. Example: A = B is the set of all people and xRy means x is the father of y. Example: A = B is the set of all real numbers and xRy means x ≥ y Example: A is the set of students at U. Iowa, B is the set of courses at U. Iowa, and xRy means x is taking course y this semester 1 Relational databases We can generalize the idea of a relation to being a subset of a Cartesian product: R ⊆ A × B × C × · · · × G × H A relational database explicitly records each sequence (a, b, c, . , g, h) contained in R: A B C ··· G H a1 b1 c1 ... g1 h1 a2 b2 c2 ... g2 h2 a3 b3 c3 ... g3 h3 . There may be several relations in a complete database, but each relation is represented like this. Example: A database of students at the U. Iowa should enable us to find a student by their name &/or address, and find out what courses they are taking. 2 Relation #1: ID First Name(s) Last Name Address1 Address2 State Zip 37752198 John Albert Smith 11 Albrecht Iowa City IA 57340 11209432 Jane E. Jones 1050 Williams Waterloo IA 50103 70842165 Mohommad Hussein 715 George Lne Mosstown IL 49712 Relation #2: ID Course ID 37752198 22M:712 11209432 22C:077 37752198 22C:100 70842165 22C:315 70842165 22M:111 11209432 22C:055 Types of relations Relations R between A and A can have special structural properties: Reflexive xRx Symmetric xRy ⇒ yRx Transitive xRy ∧ yRz ⇒ xRz for all x, y and z. Here are some “negative” properties: Irreflexive xRx Antisymmetric xRy ∧ x 6= y ⇒ yRx for all x and y. 3 Examples of relations Let’s fill out this table ... Relation Refl? Symm? Trans? Irrefl? Antisymm? parent sister sibling > ≥ ancestor = 4 Partial Order Relations Consider a project which involves many tasks: task1 −→ task2 means that task1 must be completed before task2 is started: build internal finish walls paint walls & walls ceilings lay install roof tiles get foundation lay roof frame clean up bricks install approvals install bathroom & kitchen plumbing dig sewerage fittings access lines lay water & install sewerage lines electricity supply connect water & sewerage 5 The relation R where sRt means task s must be completed before task t is begun is irreflexive, antisymmetric, and transitive. Note: Partial order relations should be reflexive, antisymmetric and transitive. Equivalence relations The “=” relation is reflexive, symmetric, and transitive. A relation R on a set A that is reflexive, symmetric and transitive is an equivalence relation. Example: If A is a set of colored balls, then the relation xRy iff x and y have the same color is an equivalence relation. If R is an equivalence relation on A, then the equivalence class [a]R of an element a ∈ A is [a]R = { x ∈ A | xRa } Any b ∈ [a]R is a representative of the equivalence class. 6 Theorem: The set of equivalence classes S = { [a]R | a ∈ A } is a partition of A. Proof: We just have to show that for any a, b ∈ A, either [a]R ∩ [b]R = ∅ or [a]R = [b]R. S Note that a ∈ [a]R since R is reflexive. So S = A. Suppose [a]R ∩ [b]R 6= ∅. Then pick c ∈ [a]R ∩ [b]R. By definition of the equivalence classes, aRc and bRc. By symmetry, cRb. By transitivity, aRb and b ∈ [a]R. If x ∈ [b]R then bRx; but aRb so by transitivity aRx and x ∈ [a]R. So [b]R ⊆ [a]R. We can argue in the same way that [a]R ⊆ [b]R and so [a]R = [b]R as we wanted. 7 Rational numbers Rational numbers can be represented as fractions a/b with b 6= 0. But this representation is not unique: 1 2 3 37 = = = etc. 2 4 6 74 We can think of rational numbers as equivalence classes of pairs of integers (a, b) with b 6= 0 where a c (a, b)R(c, d) iff = b d i.e. (a, b)R(c, d) iff ad = bc 8 Divisibility and modular arithmetic Amongst natural numbers the “divides” relation is very useful: a | b means there is a natural number k where b = k a Examples: 2 | 6 and 3 | 6, but 2 6 | 7 Fact: ”|” is reflexive and transitive, but antisymmetric. • reflexive: a | a because a = 1 a • transitive: if a | b and b | c then there are k and l where b = ka and c = lb = l(ka) = (lk)a so a | c • antisymmetric: if a | b and b | a then b = ka and a = lb, so a = (kl)a and as a 6= 0, kl = 1. Thus k = l = 1 and b = a. 9 More on divisibility Division algorithm: For any two whole numbers a and b > 0, there are whole numbers q and 0 ≤ r < b where a r = q + , or equivalently b b a = qb + r. q is called the quotient, and r the remainder. These are unique! Can compute q and r using long division ... Note: b | a ≡ r = 0 10 Modular arithmetic a ≡ b (mod n) (“a and b are equivalent modulo n”) means n | (b − a). Examples: 2 ≡ 7 (mod 5); −3 ≡ 7 (mod 10); 15 ≡ 0 (mod 5); 34 ≡ −1 (mod 7) Exercise: Check that ≡ (mod n) is a reflexive, symmetric, and transitive relation. Fact: If a ≡ b (mod n) and c ≡ d (mod n) then a + c ≡ b + d (mod n) and a · c ≡ b · d (mod n). Examples: Since 2 ≡ 7 and −3 ≡ 7 (mod 5), 2 − 3 ≡ 7 + 7 (mod 5) and (2 × −3) ≡ (7 × 7) (mod 5). 11 Casting out nines This is an old trick to check some long calculations: 3732 6 315 0 7123 4 1138 4 5012 8 +6613 7 23933 2 The column on the right is the sum of the digits of the column on the left, repeated until the result has no more than one digit. Nines are replaced by zeros. Summing the column on the right, we get the digit-sum of the correct answer. Why does it work? 12 The greatest common divisor The greatest common divisor (gcd) of two numbers a and b (gcd(a, b)) is the largest whole number that divides both a and b. Examples: gcd(12, 14) = 2; gcd(24, 9) = 3; gcd(90, 24) = 6 (Note: 2 and 3 both divide both 90 and 24, but 6 is the greatest common divisor.) 13 Functions A function f takes an input x from a set A (its domain) and from it computes a value f(x) in a set B (its range): 17 x f f(x) f(17) In CS: functions represent computation. In Math: functions are special kinds of relations. R = { (x, f(x)) | x ∈ A } R can be thought of as a giant look-up table: 14 x f(x) x1 f(x1) x2 f(x2) x3 f(x3) . For this to work, each x ∈ A should appear exactly once in the left-hand column. (If it’s not there, there is no output; if it appears more than once, how can we choose which output?) In formal logic: ∀x(∃y(xRy) ∧ ∀y1∀y2(xRy1 ∧ xRy2 ⇒ y1 = y2)) The relation R can be thought of as the graph of f. Composition of functions If f : A → B and g : B → C then the composition of f and g is g ◦ f : A → C given by (g ◦ f)(x) = g(f(x)) x f f(x) g g(f(x)) = g f(x) Example: If f(x) = log(x) and g(x) = x2 + 1 then g ◦ f(x) = g(log(x)) = (log(x))2 + 1 and f ◦ g(x) = f(x2 + 1) = log(x2 + 1) 15 One-to-one and onto functions A function f : A → B is one-to-one if f(a1) = f(a2) ⇒ a1 = a2 1.5 1 0.5 Example: f : R → R given by 0 f(x) = arctan(x) is one-to-one. −0.5 −1 −1.5 −2 −1.5 −1 −0.5 0 0.5 1 1.5 2 A function f : A → B is onto if for every b ∈ B there is an a ∈ A where b = f(a). 2 1.5 1 0.5 Example: f : R → R given by 0 f(x) = x3 − x is onto. −0.5 −1 −1.5 −2 −1.5 −1 −0.5 0 0.5 1 1.5 16 A function f : A → B is a bijection if it is one-to-one and onto. 4 3 2 1 Example: f : R → R given by 0 f(x) = x3 is a bijection. −1 −2 −3 −4 −1.5 −1 −0.5 0 0.5 1 1.5 The identity function id : A → A is the function id(x) = x for all x ∈ A. Bijections have inverse functions: f : A → B has an inverse function g = f −1 : B → A where for all x ∈ A and y ∈ B, f(x) = y ⇔ x = g(y) Equivalently, g ◦ f = id, and f ◦ g = id. Example: f(x) = x3: The inverse function is g(y) = y1/3. Some special functions ... Some functions you might not have come across yet ... ceiling dxe smallest integer ≥ x floor bxc largest integer ≤ x mod/rem a mod b remainder when a is divided by b Properties: If a, b, and k are integers, and x a real number • dx + ke = dxe + k and bx + kc = bxc + k • k = bk/2c + dk/2e • (a + kb) mod b = a mod b • 0 ≤ (a mod b) < |b| provided b 6= 0 17.