Sets Set Identities Sets and propositional logic have some similar properties In fact both are forms of boolean algebra Compare Set Identities table (p. 130 in text) with Logical Equivalences table (p. 27 in text) We will look at the Wikipedia pages http://en.wikipedia.org/wiki/Algebra_of_sets https://en.wikipedia.org/wiki/Logical_equivalence These will be useful to remember See text for some proofs Let’s demonstrate just a few
A B = B A [Commutative Law] Sets A B = B A [Commutative Law] Greatly simplified / informalized proof using logic, previous known identities and set builder notation We will show equality by showing that each set is a subset of the other x, x (A B) x A v x B [ Def. of Union] x B v x A [Comm. Law Logic] x (B A) [ Def. of Union] (A B) (B A) [ Def. of Subset ] We can apply similar assertions to show that (B A) (A B) and thus the sets are equal Ex: A = { 1, 2, 3, 4, 5 } B = { 6, 7, 8, 9, 10} A B = B A = {1,2,3,4,5,6,7,8,9,10}
There is often more than one way to prove an assertion Sets There is often more than one way to prove an assertion For sets lets consider a membership table Same idea as a truth table for logic Instead of T and F for true and false We have 1 and 0 for member or not member Rows indicate the different ways elements may or may not be included Columns indicate the sub-expressions If the membership tables of two set expressions are identical then the expressions are equivalent Consider the Commutative Law that we just proved
Membership table for A B and B A Sets Membership table for A B and B A Note that the columns for A B and B A are identical Let's try another one! We know DeMorgan's Laws for logic Let's look at them for sets A B A B B A 1
[DeMorgan's Law 2] [DeMorgan’s Law 1] Sets [DeMorgan’s Law 1] [DeMorgan's Law 2] Let's prove the second law using same technique we did previously (detailed proof of first law is in the text) Definition of complement Definition of negation Definition of union DeMorgan’s for logic Definition of negation Definition of complement Definition of intersection Definition of subset
Ex: A = { x | x is a Pitt Student } B = { x | x is a CMU Student } Sets We (again) can apply similar logic to show that to complete the proof (by showing each is a subset of the other) Ex: A = { x | x is a Pitt Student } B = { x | x is a CMU Student } { x | x is not (a Pitt student or a CMU student) } = {x | x is not a Pitt student and x is not a CMU student }
Sets Membership table for Again the columns are identical so the expressions are equivalent See your course notes and text for more proofs / approaches for sets A B A B 1
Consider again the Cartesian product of two sets, A x B Functions Consider again the Cartesian product of two sets, A x B Recall that this produces a set of ordered pairs over all of the elements in A and B Two important subsets of the Cartesian product of two sets are: A relation, R from set A to set B Ordered pairs chosen for a relation can be an arbitrary subset of the Cartesian product Relations are very useful and you will cover them in detail later in the term
A function, F from set A to set B Functions A function, F from set A to set B F : A B y = F(x) where x A and y B Adds two constraints to a relation The domain of the function is the entire set A, or x A y B such that (x, y) F Each x A is mapped to only one y B, or x A, y, y’ B, if (x, y) F and (x, y’) F then y = y’ Another way to think of these rules is that, considering the ordered pairs in the Cartesian product, for a function, each value in set A will occur in exactly one ordered pair
Let’s look at an example: Functions Let’s look at an example: Let set Actors = {McGregor, Bale, Keaton, Downey, Guinness} Heroes = {Batman, Obi-Wan, IronMan, Holmes} Consider Function Plays : Actors Heroes = {(McGregor, Obi-Wan), (Bale, Batman), (Keaton, Batman), (Downey, IronMan), (Guinness, Obi-Wan)} We call Actors the Domain of function Plays We call Heroes the Codomain of function Plays Within Heroes we call the subset {Batman, Obi-Wan, Ironman} the Range of function Plays It is the set of values actually mapped to by the function
Functions If we add ordered pair (Downey, Holmes) to the set F is it still a function? No, since now we have (Downey, IronMan) and (Downey, Holmes) and constraint ii. does not hold Value in domain mapped to multiple values in range If we remove ordered pair (Guinness, Obi-Wan) from the set F is it still a function? No, since now we have a value x in A such that no (x, y) exists in F and constraint i. does not hold Domain is not the entire set A
Injections A function F: A B is an injection, or one to one, if no two elements in A map to the same element in B or y B, at most one x A such that (x, y) is in F Is the previous example (from Slide 11) an injection? No, Plays(McGregor) = Plays(Guinness) No, Plays(Bale) = Plays(Keaton) Ex: Given Domain and Codomain = Positive Integers F(n) = 2n is one to one F(n) = N mod 2 is not one to one (F(4) = F(2) = 0, for example)
Surjections A function, F: A B is a surjection, or onto, if the range of F is equal to the codomain (= B) or y B, an x A such that (x, y) is in F Is the previous example (from Slide 11) a surjection? No, there is no pair (x, Holmes) in F We could make it onto if we add Cumberbatch to A and add (Cumberbatch, Holmes) to F Ex: Given Domain and Codomain = real numbers F(x) = x is onto F(x) = abs(x) is not onto, since the abs(x) cannot be negative
Note the similarities: One to one and Onto Note the similarities: One to one: y B, at most one x A such that (x, y) is in F Onto: y B, an x A such that (x, y) is in F But note the differences: One to one: Entire codomain does not have to be in the range at most one means 0 or 1 Onto: Values in range can be in more than one pair an means 1 or more
If a function is both one to one and onto it is called a bijection Ex: Domain and codomain = real numbers, R F(x) = x is a bijection Note that the domain and codomain are significant in determining a bijection With domain and codomain = R F(x) = 2x is a bijection With domain and codomain = Z F(n) = 2n is NOT a bijection It is not onto [ex: there is no n such that F(n) = 3]
Arbitrary subset of the Cartesian product Functions Let’s visualize these different subsets of Cartesian products: Relation: Arbitrary subset of the Cartesian product Does not require all elements of domain to be mapped Same value in domain can map to multiple values in range Multiple values in domain can map to same value in range Does not require all elements of codomain to be in range
Functions Function All elements of domain must be mapped Each value in domain can map to only one value in range Multiple values in domain can map to same value in range Does not require all elements of codomain to be in range
Functions Injection (One to One Function) All elements of domain must be mapped Each value in domain can map to only one value in range Each value in range is mapped from a distinct value in domain Does not require all elements of codomain to be in range
Functions Surjection (Onto Function) All elements of domain must be mapped Each value in domain can map to only one value in range Multiple values in domain can map to same value in range All elements in codomain must be in range
(One to One and Onto Function) Functions Bijection (One to One and Onto Function) All elements of domain must be mapped Each value in domain can map to only one value in range Each value in range is mapped from a distinct value in domain All elements in codomain must be in range
Consider the Java hash code for a String: public int hashCode() { More Examples Consider the Java hash code for a String: public int hashCode() { int h = hash; if (h == 0 && count > 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; return h; Let’s talk about this a bit Note: This function is utilized by the Hashtable (and other classes) to insert and retrieve data from a Collection. Note that the hashCode function can be negative. You will see (either in CS 0445 or CS 1501) the the actual index for a hash function must be non-negative, so that it can map to a valid index in an array.
Is it actually a function? More Examples Domain Set of character strings of arbitrary length Codomain Set of integers representable by a Java int Is it actually a function? Yes All Java Strings can be hashed No String will hash to more than one int Is it one-to-one? We would need to show that for every String S there is a unique hash value h(S) To disprove this, we simply need to show that at least two different Strings, S1 and S2, hash to the same value Domain: Max length of a String is actually Integer.MAX_VALUE which is 2,147,483,647
Let’s try to disprove it in the following way: More Examples Let’s try to disprove it in the following way: How many possible unique values can a Java int have? 232 = 4294967296 ~ 4.295 x 109 How may possible String values are there? Consider just Strings of letters (upper and lower case) This gives 2 * 26 = 52 possibilities per character Consider now Strings of 10 characters or fewer 5210 ~ 1.446 x 1017 Thus there are MANY MORE possible Strings (in the domain) than there are integers (in the codomain) Based on this is is impossible for each String in the domain to map to a unique integer in the codomain Note: This “proof” relies on some things we will be discussing later on: counting techniques and the pigeonhole principle – stay tuned!!!
Is it onto? Is it a bijection? More Examples This is more difficult to verify We would have to show that for every possible int value, x, in Java, there is some String S such that S.hashCode() = x Could be done with some number theory Is it a bijection? Since it is not one to one, clearly no
More Examples Consider the function SSN(x) which returns the Social Security number for a US citizen (or resident) Domain? Set of people living in the US Codomain? Set of 9-digit integers Is it actually a function? This is not as simple as it would seem See: http://ssa.gov/pubs/10093.html#how The tricky part is whether both numbers would still be associated with the same person
Assuming SSN is a function More Examples Assuming SSN is a function Is it one to one? In theory it should be Two different people should not have the same SSN Is it onto? Not yet (thankfully) We probably do not ever want it to be onto, since that would mean there are no remaining SSNs for new people!!! Is it a bijection? No, since it is not onto
Inverse Functions A function which is a bijection also has a mapping from the codomain back to the domain This is also a function and is called the inverse of the original function, and is denoted F-1 for function F Note: Why must the function be a bijection? If it is not one to one then F(x) = F(y) for some x ≠ y This implies that the “inverse” F-1 would map the same value, F(x) [ = F(y) ], to two different values, x and y, which means it is not a function (Rule ii.) If it is not onto then the exists some y in the codomain such that x D, F(x) ≠ y This implies that there is at least one value in the codomain that would not be mapped back to the domain, so F-1 is not a function (Rule i.)
Ex: Let A = { 1, 2, 3, 4 } Let B = { a, b, c, d } Inverse Functions Ex: Let A = { 1, 2, 3, 4 } Let B = { a, b, c, d } if F : A B = { (1, a), (2, b), (3, c), (4, d) } then F-1 : B A = { (a, 1), (b, 2), (c, 3), (d, 4) } Note that, for all x in A, F-1(F(x)) = x We say that this is an identity function
Given functions: F : A B G : B C Inverse Functions Given functions: F : A B G : B C We call G(F(x)) the composition of functions F and G It is also shown as G o F Note that the range of F is the domain of G Ex: A = { a, b, c, d } B = { 1, 2, 3 } C = { #, $, &, @ } F : A B = {(a, 2), (b, 3), (c, 1), (d, 1)} G : B C = {(1, $), (2, @), (3, &)} Note that F is not one to one Note that G is not onto Bijections are not required for composition
Examples: Given domain and codomain = R F(x) = 2x F(x) = 2x2 Inverse Functions Examples: Given domain and codomain = R F(x) = 2x What is the inverse of F? y = 2x y/2 = x F-1(y) = y/2 F(x) = 2x2 y = 2x2 y/2 = x2 sqrt(y/2) = x So F-1(y) = sqrt(y/2)?????? 2x^2 is not one to one, since +- sqrt(y/2) = x NO!!! Why not??
Two functions of interest (among many) Floor and Ceiling Two functions of interest (among many) Floor: Domain: R Range: Z Floor(x) = x = greatest integer <= x Ceiling: Ceiling(x) = x = smallest integer >= x Can be confusing Floor is greatest? Ceiling is smallest? Important thing is the relational operator
Note that both floor and ceiling are Examples: x = 3.8 x = 3 x = 4 x = -3.8 x = -4 x = -3 x = 3.00000001 x = 3 x = 4 x = 3 x = 3 x = 3 Note that both floor and ceiling are Onto (surjections) Not one to one (not injections)
Floor and Ceiling functions are quite useful in CS Ex: I have 235 distinct values to represent in binary. How many bits do I need? lg2(235) = 8 You will see issues like this soon in counting (Ch. 6) Note: In programming, none of these things is ceiling or floor: (int) x or trunc(x) Not equal to floor when x is negative; ok for x >= 0 round(x) Depending on value of x could match one or other See Demo1.java
A bijective function is both one to one and onto Functions: Summary A function, F, is a subset of the Cartesian product of two sets, A and B such that The domain of F is all of A For all x in A, there exists exactly one pair (x, y) in F An injective function (one to one) contains at most one x in A for each y in B A surjective function (onto) contains at least one x in A for each y in B A bijective function is both one to one and onto
Functions have inverses iff they are bijections Functions: Summary Functions have inverses iff they are bijections Given F: A B and G: B C, the composition of functions F and G, G(F(x)): A C The composition of a function and its inverse is an identify: F-1(F(x)) = x The floor and ceiling functions are of interest in computer science Be careful to understand their meanings