Chapter 3 – Set Theory
What is a Set? A set is a collection of distinguishable objects called elements. When writing out the elements of a set we use { }. Two kinds of set definitions – list-based and rule-based. The above are examples of list-based definitions of sets. We list all the elements (or enough so that the pattern is obvious). A rule-based definition of a set involves a predicate. { 1, 2, 3,... } -- set of positive integers (Natural Numbers) {red, green, blue} -- primary colors set P = { x : P(x) } E = { x : x is an even integer } = { 0, +2, -2, +4, -4,... } We say “the set of all x such that...”.
Alternative Definitions: Give an alternative definition. A = {x : x is an integer and x 2 + 4x = 12 } B = {x : x is a day of the week not containing the letter 'u'} C = { n 2 : n is an integer }
Well-known Sets: Empty Set: Ø Natural Numbers: N -- { 1, 2, 3,... } Integers: Z -- { 0, ±1, ±2, ±3,... } Rational Numbers: Q -- { a/b : a, b Є Z and b != 0 } Real Numbers: R -- { all decimals } The above sets correspond very closely to the data types of many programming languages.
Data Types: A set of objects (values) together with a well-defined set of operations. When we declare a variable to be of a certain type we are saying that any value it takes on must come from a known set of values. Note that in perl there is a numeric value you can assign to a scalar variable and this numeric value is not a number – NaN.
Sets and Their Operations: A = {1, 2, 3} then we say that A is a subset of N. A set is called a subset of another set if every element of the first set is also an element of the second. We denote this as We use a Venn Diagram to represent this: A B U A B Universe of Discourse or Universal Set
Set Equality: Two sets are defined to be equal if each is a subset of the other. Symbolically we write: Exercise: Define A = {n : n 2 is an odd integer} and B = {n: n is an odd integer}. Prove A = B. A = B if (x A => x B) and (x B => x A) are both true єєє є If x A, then x 2 = 2m + 1. If x is even then x = 2n and so x 2 = 4n 2 so x 2 is even, a contradiction. So by contraposition, x can not be even so x is odd. Hence x B. Now suppose x B. Then x = 2m +1 and x 2 = (2m + 1) 2 = 2m 2 + 4m + 1 = 2(m 2 + 2m) + 1 and so x 2 is odd. Therefore x A. This shows A = B. є є є є
Union: The Union of two sets, A and B, is the set A B = { x: x A or x B} U єє AB
Intersection: The Union of two sets, A and B, is the set A B = { x: x A and x B} U єє B A
Complement of A Relative to B: The Complement of the set A relative to a set B B, is the set A \ B = { x: x A and x B} єє AB
Complement Set: If no “relative” set is mentioned then the complement of a set A is with respect to a Universal Set (represented as the box in our Venn Diagrams). It is everything that is not in A. A Complement of A
Symmetric Difference of A and B: The Complement of the set A relative to a set B B, is the set A B = { x: x A and x B} { x: x B and x A} є AB є єєє U ∆
Examples: A = { 1, 3, 5, 7 }, B = { 2, 4, 6, 8 }, C = {1, 2, 3, 4,5 } A B = { 1, 2, 3, 4, 5, 6, 7, 8 } B C = { 2, 4 } A \ C = { 7 } B C = { 6, 8 } { 1, 3, 5 } = { 1, 3, 5, 6, 8 } U U U ∆
Exercise: Suppose A = { x : 1 <= x <= 12 and x is an even integer } and B = { x : 1 <= x <= 12 and x is an integer multiple of 3 } Verify ~(A B) = ~A ~B. U U Calculate: ~(A B) = Calculate ~A ~B = U U A = { 2, 4, 6, 8, 10, 12 } B = {3, 6, 9, 12 } U = { 1, 2,..., 12 } ~ { 6, 12 } = { 1, 2, 3, 4, 5, 7, 8, 9, 10, 11 } = {1, 3, 5, 7, 9, 11 } {1, 2, 3, 5, 7, 8, 10, 11 } = { 1, 2, 3, 4, 5, 7, 8, 9, 10, 11 } U
Algebra of Sets: There is a nice correspondence between the boolean operators – and, or, not and => on predicates and the set operators intersection, union, complement and subset.
Example (de Morgan's Law): Prove that for any two sets, A and B, ~(A B) = ~A ~B. So we reduced a claim about sets to two compound logical propositions - ~(P and Q) and ~P or ~Q. U U ~(A B) = {x: x (A B)} = {x: not ( x (A B))} = { x: not ( x A and x B )} U Є U Є U ЄЄ ~A ~B = {x: (x A) or (x B)} = { x: (not ( x A)) or (not( x B )} Є ЄЄ UЄ Exercise: Prove the above are logically equivalent
Algebra of Sets: The algebra of sets is governed by certain laws just as basic arithmetic is governed by laws of arithmetic such as association, commutativity, etc. Associative Laws: A (B C) = (A B) C A (B C) = (A B) C U U UUU UUU Commutative Laws: B C = C B B C = C B UU UU Identity Laws: B = B B = B B = B = U U O U OO U U UU
Algebra of Sets (cont): Finally Idempotent Laws: B B = B B B = B U U Distributive Laws: A (B C) = (A B) (A C) A (B C) = (A B) (A C) UUU U U UU UU U Complement Laws: A ~A = A ~A = ~ = ~ = ~(~A) = A O U O O UU U U de Morgan's Laws: ~(B C) = ~C ~B ~(B C) = ~C ~B UU UU
Example: Use the laws of algebra of sets to prove: A B = (A B) ~(A B) Δ Proof: A B is symmetric difference. It is equal to (A ~B) (B ~A) However U U U (A B) ~(B A) = (A B) ((~A) (~B)) -- deM = ((A B) ~A) ((A B) ~B) UU U ∆ U U U U UU U U UU U = ((~A) (A B)) ((~B) (A B)) -- Comm Law = (((~A) A)) ((~A) B)) (((~B) A) ((~B) B)) --Dist Law = (((~A) A)) (B (~A))) ((A (~B)) ((~B) B)) --Comm Law =( (B (~A))) ((A (~B)) ) --Comp Law = (A ~B) (B ~A) -- Comm and Ident Law -- Dist Law U U UUU U U U UUU UUU UU UU UUU U U U U ОО the same UU
Further Properties of Sets: The cardinality of a finite set is the number of elements in the set (written |S|). If A and B are finite sets then |A B| = |A| + |B| - |A B|. Proof: A B = (A\B) (A B) (B\A) so |A B| = |A\B| + |A B| + |B\A| Suppose |A\B| = m, |A B| = n, |B\A| = p Then |A B| = m + n + p = (m + n) + (n + p) – n = |A| + |B| - |A B| AB A \ B A B B \ A U U U U U U U U U U U U
Exercise: 63 students study CS – 16 with the Accounting option, 37 with the business option and 5 studied both. How many studied neither ? Ans: 15
Cartesian Products: An ordered pair is a tuple (a,b) where a A and b B. A cartesian product A x B = {(a,b): a A, b B}. Cartesian products are fundamentally important because they are the basis of all functions (math) and relations (dbms). R 2, the real plane, is an example of a cartesian product. If A 1, A 2, A 3,..., A n are a collection of sets then A 1 x A 2 x A 3 x... x A n = { (a 1,a 2,a 3,..., a n ) : a 1 A 1,..., a n A n } If A 1 = A 2 = A 3 =... = A n = A we express this as A n. єє єє єє
Relational Database Example: The rows in the Employee table correspond in a 1-1 fashion to employees in a company. They also belong to the following cartesian product EmployeeID EName DOB Gender Employee {EmployeeID} x {EName} x {DOB} x {Gender} You can refer to all the rows of the cartesian product that lie in Employee as information rows and the rows outside Employee, the rows that don't correspond to reality as the noise rows.
Example: If B = {0,1} describe B 8. Suppose S is a finite ordered set with elements {s 1, s 2,..., s n }. Suppose A S. We define a characteristic function that corresponds to A as: So f A corresponds to an ordered tuple of n 0s and 1s. f A : S --> {0,1} where f A (s i ) = 1 if s i A = 0 if s i A є є
Example (cont): S = {1,2,3,4,5,6,7,8}, A = {2,3,4,6,7}, B = {1,3,4,5} f A = , f B = Find A B. This is just the bit operation f A or f B = This is the characteristic function of the set {1,2,3,4,5,6,7} which is A B. U Exercise: Find A B. U U and is the characteristic function of the set {3,4} so A B = {3,4}. U
Bit Map Example: Go back to the Employee table. Suppose BD = {birth dates}, G = {male, female}. For each element of G, create a sequence of 0s and 1s. These sequences are called bit maps. Repeat the process for every date in BD. Convince yourself that finding all females born on August 4, 1975 is just a matter or ANDing two bit maps. EmployeeID EName DOB Gender Employee male = where i th digit is 1 iff i th employee is male female = where i th digit is 1 iff i th employee is female
Homework: Do the Exercises at the end of the chapter.
Prolog: Prolog is a logic programming language. Prolog uses facts and rules to deduce new facts. % prolog facts % parent(x,y) means x is the parent of y parent(george_1, george_2). parent(george_3, george_4). parent(george_3,edward). parent(edward,victoria). parent(victoria,edward_7). parent(edward_7,george_5). parent(george_5,edward_8). parent(george_5,george_6). parent(george_6,elizabeth_2). parent(victoria,alice). parent(alice,victoria_alberta). parent(victoria_alberta,alice_mountbatten). parent(alice_mountbatten,philip). % wife(x,y) means x is the wife of y wife(sophia,george_1). wife(wilemina,george_2). wife(charlotte,george_3). wife(caroline,george_4). wife(adelaide,william_4). wife(victoria,albert). wife(victoria_mary,george_5). wife(elizabeth_qm,george_6). wife(elizabeth_2,philip).
Prolog Rules and Questions: % rules: female(X) :- wife(X,Y). % if X is wife of Y then X is female ancestor(X,Y) :- parent(X,Y). % if X is parent of Y then X is ancestor of Y ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). % is X is parent of Z and Z is ancestor of Y then X is ancestor of Y %questions female(victoria). yes female(george_3). no
How Do Questions Get Answered: Logically, prolog rules are expressed as quantified predicates: female(X) :- wife(X,Y) can be expressed as X Y wife(X,Y) --> female(X) this is the same as X Y ~wife(X,Y) or female(x) Since this rule is part of the program it is TRUE. If I ask the question female(victoria) then logically I am asking Y ~wife(victoria,Y) or female(victoria) Since wife(victoria,albert) is TRUE, ~wife(victoria,Y) can not be TRUE for all Y. Hence the only way this rule can be true is if female(victoria) is TRUE. AA AA A
How Do Questions Get Answered: Logically, prolog rules are expressed as quantified predicates: female(X) :- wife(X,Y) can be expressed as X Y wife(X,Y) --> female(X) this is the same as X Y ~wife(X,Y) or female(x) Since this rule is part of the program it is assumed TRUE. If I ask the question female(albert) then logically I am asking Y ~wife(albert,Y) or female(albert) Since wife(albert,Y) is never TRUE, ~wife(albert,Y) is TRUE for all Y. Hence the rule can be true even if female(albert) is FALSE. This leads us to note that in prolog, FALSE means “no evidence it is TRUE”. AA AA A
Exercise Understand the logic of finding There are two ancestor(x,y) rules X Y parent(X,Y) -> ancestor(X,Y) X Y Z parent(X,Z) and ancestor(Z,Y) -> ancestor(X,Y) Which can be rewritten ( X Y ~parent(X,Y) or ancestor(X,Y)) or ( X Y Z ~(parent(X,Z) and ancestor(Z,Y)) or ancestor(X,Y)) Which can be rewritten ( X Y ~parent(X,Y) or ancestor(X,Y)) or ( X Y Z ~parent(X,Z) or ~ancestor(Z,Y)) or ancestor(X,Y)) Now we ask the question: ancestor(alice,X). We first try to find as many solutions as possible to ( Y ~parent(alice,Y) or ancestor(alice,Y)) Since ~parent(alice,Y) fails for Y == victoria_alberta we can conclude that ancestor(alice,victoria_alberta) is TRUE. AA AAA AA AAA AA AAA A
Exercise Now we work with ( Y ~parent(alice,victoria_alberta) or ~ancestor(victoria_alberta,Y)) or ancestor(alice,Y)) ~parent(alice,victoria_alberta) is FALSE and we already have alice as an ancestor of victoria_alberta so we look for a Y that makes ~ancestor(victoria_alberta,Y) fail; Y == alice_mountbatten does this This makes the only way the above quantified pedicate can be TRUE is if ancestor(alice,alice_mountbatten) is TRUE. Continuing in this way we get all royals for whom alice is an ancestor. A
Monty Python: girls are witches. View the video.
Example: Monty Python A witch is a female who burns. Witches burn - because they're made of wood. Wood floats. What else floats on water? A duck; if something has the same weight as a duck it must float. A duck and scales are fetched. The girl and the duck balance perfectly. “Burn the witch!” witch(X) :- female(X), burns(X). burns(X) :- floats(X). floats(wood). floats(duck). floats(X) :- sameWeight(duck,X). sameWeight(duck,girl). female(girl). :- witch(girl)? yes
Exercise: Give as many examples of faulty logic from the skit as you can find. Are any of them inverse or converse reasoning?