Discrete Structures for Computing Notes 4 ------------------------------------------------------------------------ Chapter 2: Basic Structures: Sets, Functions, Sequences, and Sums ------------------------------------------------------------------------ These are mathematical notions used to represent, manipulate and reason about discrete objects, especially *collections* of discrete objects. ------------------------------------------------------------------------ 2.1 Sets ------------------------------------------------------------------------ We will use the following intuitive (informal) definition of a set: A SET is an unordered collection of objects (called the ELEMENTS of the set). (It is possible to define a set more rigorously using axioms. This is necessary to avoid some nasty problems, but is not needed for what we will do in this course.) Two sets A and B are EQUAL iff they have the same elements: i.e., a in A iff a in B ("in" should really be the "element-of" sign). * ordering does not matter: {3,5,1} = {1,3,5} * repetitions do not matter: {3,5,1} = {3,1,1,5,3} We can represent sets pictorially with VENN DIAGRAMS << draw example: rectangle is UNIVERSAL SET >> EMPTY SET: set that that has no elements, denoted 0/ (zero with a slash through it). WARNING: Do not confuse 0/ with {0/}. The latter is a set that has one element, the empty set. (Yes, we can have a set whose elements are sets.) Set A is a SUBSET of set B iff every element of A is also in B; denoted \subseteq. << draw Venn diagram example >> THEOREM: For every set S, (i) the empty set is a subset of S and (ii) S is a subset of S. A set A is a PROPER SUBSET of set B iff A \subseteq B but A != B. PROOF TECHNIQUE: To prove that A = B, (1) prove that A \subseteq B, and then (2) prove that B \subseteq A. Suppose set S contains a finite number of elements. The number of elements in S is called the CARDINALITY of S and is denoted |S|. The POWER SET of set S is the set of all subsets of S, denoted P(S). Example: the power set of {0,1,2} is { 0/, {0}, {1}, {2}, {0,1}, {0,2}, {1,2}, {1,2,3} } Example: the power set of 0/ is { 0/ } Example: the power set of { 0/ } is { 0/, {0/} } FACT: If |S| = n, then P(S) has 2^n elements. When the order of elements in a collection of n objects is important, we use a concept called ORDERED n-TUPLE: (a_1, a_2, ..., a_n). Two ordered n-tuples are EQUAL iff each element in one tuple is equal to the corresponding element in the other one. Example: (0,1,2) = (0,1,2), but (0,1,2) != (0,2,1). 2-tuples are called ORDERED PAIRS. Given two *sets* A and B, the CARTESIAN PRODUCT of A and B, denoted A x B, is the *set* of *ordered pairs* (a,b,), where a in A and b in B. Example: A = {1, 2} and B = {a, b, c}. Then A x B = { {1,a}, {1,b}, {1,c}, {2,a}, {2,b}, {2,c} } FACT: If |A| = n and |B| = m, then |A x B| = n*m. Can extend the definition of Cartesian product to more than 2 sets: A_1 x A_2 x ... x A_n = {(a_1, a_2, ..., a_n) : a_i in A_i for i = 1,2,...n} ------------------------------------------------------------------------ 2.2 Set Operations ------------------------------------------------------------------------ Different ways to combine sets A and B: * Union: A U B = {x : x in A or x @ B} << Venn diagram >> * Intersection: A /\ B = {x : x @ A and x @ B} << Venn diagram >> FACT: |A U B| = |A| + |B| - |A /\ B| * Difference: A - B = {x : x in A and x not-in B} << Venn diagram >> Example: {1, 3, 5} - {1, 2, 3} = {5} {1, 2, 3} - {1, 3, 5} = {2} * Complement (have to know the universal set U): A-bar = U - A i.e., A-bar = {x : x not-in A} << Venn diagram >> Table 1 on page 124 lists important identities about sets and these operations. Example: For any sets A and B, the complement of A /\ B equals A-bar U B-bar. (cf. DeMorgan's law) Proof: complement of A /\ B = {x : x not-in A /\ B} by def. of complement = {x : not(x in A /\ B) } by def. of not-in = {x : not(x in A and x in B)} by def. of intersection = {x : not(x in A) or not(x in B)} by DeMorgan's law *** = {x : x not-in A or x not-in B} by def. of not-in = {x : x in A-bar or x in B-bar} by def. of complement = {x : x in A-bar U B-bar} by def. of union = A-bar U B-bar Be familiar with the identities (identify, commutativity, associativity, distributivity, DeMorgan's for sets, etc.). Generalized definitions of union and intersection for more than two sets: Union: A_1 U A_2 U ... U A_n = U_{i=1}^n A_i Contains every element that is in at least one of the A_i's. By associativity, order in which the unions are done doesn't matter, so no need to use parentheses. Intersection: A_1 /\ A_2 /\ ... /\ A_n = /\_{i=1}^n A_i Contains every element that is in every one of the A_i's. By associativity, order in which the intersections are done doesn't matter, so no need to use parentheses. CS APPLICATION: An efficient way to represent sets in a computer is to use a bit string and set the i-th bit in the string equal to 1 if the i-th element of the universal set is in the set, and 0 if not. This makes it quick and easy to do many set operations: compare and/or change the appropriate bits. ------------------------------------------------------------------------ 2.3 Functions ------------------------------------------------------------------------ Functions are a basic concept in mathematics, including discrete mathematics, and its applications to computer science. Let's review some basic facts. Let A and B be nonempty sets. A FUNCTION f from A to B, denoted f:A -> B, is an assignment of one element of B to each element of A. If b is the element of B assigned to element a of A, we write it f(a) = b. Terminology: * A is the DOMAIN * B is the CODOMAIN * b is the IMAGE of a * a is a PREIMAGE of b (might be more than one, if several different elements of A are mapped to b) * RANGE of f is the subset of B consisting of images of all elements of A (might be smaller than B if f does not "use" all of B) Example: f:Z -> Z is f(x) = x^2. * Domain is Z (set of integers) * Codomain is Z * Range is all integers that are perfect squares * image of 3 is 9 * preimages of 16 are 4 and -4 Different ways to specify functions: * explicitly list the assignment mapping (only works for finite A) * give a formula * give an algorithm (computer program) to compute the function If f and g are both functions from A to R (real numbers): * (f+g)(x) = f(x) + g(x) * (fg)(x) = f(x)*g(x) A function is ONE-TO-ONE is f(a) = f(b) implies a = b. Example: f(x) = x+1, with domain and range Z, is one-to-one. f(x) = x^2, with domain and range Z, is not one-to-one A function is ONTO if the range equals the codomain, i.e., every element in the codomain is the image of some element of the range. Example: f(x) = x+1, with domain and range Z, is onto f(x) = x^2, with domain and range Z, is not onto A function is a ONE-TO-ONE CORRESPONDENCE, or BIJECTION, if it is one-to-one and onto. Example: f(x) = x+1, with domain and range Z, is a bijection. f(x) = x^2, with domain and range Z, is not a bijection. Some pictorial examples: << Figure 5, page 139 >> Given a bijection f:A -> B, the INVERSE of f, denoted f^{-1}, is the function from B to A defined by f^{-1}(b) = a iff f(a) = b. << Figure 6, page 139 >> Example: f(x) = x+1, with domain and range Z. f^{-1}(y) = y-1 Consider g:A->B and f:B->C. COMPOSITION of f and g, denoted f o g,is f(g(a)). I.e., first apply g to a, to get b, then apply f to b to get final answer. << Figure 7, page 141 >> Example: Suppose f(x) = 2x + 3 and g(x) = 3x + 2, both have Z as range and codomain. * f o g is defined by f(g(x)) = f(3x + 2) = 2(3x + 2) + 3 = 6x + 7 * g o f is defined by g(f(x)) = g(2x + 3) = 3(2x + 3) + 2 = 6x + 11. Notice that f composed with its inverse gives the identity function (returns its argument). *** assume you know how to graph functions *** In discrete mathematics, we often only want to deal with integers. To convert an arbitrary real number into an integer, two functions are commonly used: * FLOOR: returns the largest integer less than or equal to its argument * CEILING: returns the smallest integer greater than or equal to its argument Example: * floor(1/2) = 0 * ceil(1/2) = 1 * floor(-1/2) = -1 * ceil(-1/2) = 0 * floor(3.1) = 3 * ceil(3.1) = 4 * floor(7) = 7 * ceil(7) = 7 Example: Data is usually stored in units of BYTES (8 bits) for computer applications. How many bytes are required to encode 100 bits of data? ceil(100/8) = ceil(12.5) = 13 bytes. Table 1 on page 144 lists some useful properties of floor and ceiling. *** become familiar with them! *** Be careful with floor and ceiling! For instance, at first glance you might think that ceil(x+y) should equal ceil(x) + ceil(y). But a counter-example is x = y = 1/2: ceil(1/2 + 1/2) = ceil(1) = 1, which is not equal to ceil(1/2) + ceil(1/2) = 0 + 0 = 0. COMMON FUNCTIONS: * exponential: f(x) = b^x for some base b. FACTS: (1) b^{x+y} = b^x * b^y (2) (b^x)^y) = b^{x*y} * logarithmic: f(x) = log_b(x) (b raised to the power f(x) gives x) FACTS: (1) log_b(x*y) = log_b(x) + log_b(y) if x and y are pos reals (2) log_b(x^y) = y*log_b(x) if x is a pos real (3) log_a(x) = log_b(x) / log_b(a) NOTATION: * ln (x) is natural log (base e = 2.7...) * log (x) is log base 2 * polynomial: f(x) = a_0 + a_1*x + a_2*x^2 + ... + a_n*x^n * factorial: domain is N, codomain is Z+, f(n) = 1*2*3*...*n, denoted n! . By definition, let 0! = 1. ------------------------------------------------------------------------ 2.4 Sequences and Summations ------------------------------------------------------------------------ A SEQUENCE is a function from a subset of the integers (usually the natural numbers) to a set S. Example: 1, 1/2, 1/3, 1/4, ..., 1/n, ... A GEOMETRIC PROGRESSION is a sequence of the form a, a*r, a*r^2, ..., a*r^n, ... Example: 1, -1, 1, -1, ..., (-1)^n, ... [a = 1, r = -1] Example: 2, 10, 50, 250, 1250, ..., 2*5^n, ... [a = 2, r = 5] Example: 6, 2, 2/3, 2/9, 2/27, ..., 6*(1/3)^n, ... [a = 6, r = 1/3] An ARITHMETIC PROGRESSION is a sequence of the form a, a+d, a+2d, ..., a+nd,... Example: -1, 3, 7, 11, ... [a = -1, d = 4] Example: 7, 4, 1, -2, ... [a = 7, d = -3] Suppose you know some of the initial terms of a sequence. How can you figure out the general formula? * see if each term follows from the previous by adding the same amount, or an amount that depends on the position in the sequence * see if each term follows from the previous by multiplying by the same amount * are there repetitions of the pattern? Integer sequences occur in many application areas: biology, engineering, chemistry, physics, puzzles, ... Cf. http://www.research.att.com/~njas/sequences/ Summation notation: a_m + a_{m+1} + ... + a_{n-1} + a_n is denoted Sigma_{j=m}^n a_j. Example: Sigma_{j=1}^5 j^2 = 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55. Example: Sigma_{k=4}^8 (-1)^k = (-1)^4 + (-1)^5 + (-1)^6 + (-1)^7 + (-1)^8 = 1 + (-1) + 1 + (-1) + 1 = 1. IMPORTANT TRICK: *** shifting the index of summation *** If you change the values over which the index runs, you must be sure to make the corresponding change to the summand (the expression being summed). Example: Sigma_{j=1}^5 j^2 = Sigma_{k=0}^4(k+1)^2. In this example, k = j-1, i.e., j = k+1. Why would you want to do this? In order to do various algebraic manipulations. We'll see some examples. A useful fact in lots of applications is the formula for the SUM OF A GEOMETRIC PROGRESSION (called GEOMETRIC SERIES): THEOREM: If a and r are real numbers, r != 0, then Sigma_{j=0}^n a*r^j equals * (n+1)a if r = 1 and * (a*r^{n+1} - a)/(r-1) if r != 1 PROOF: We use a trick, which is to set a variable equal to the formula, do some algebraic manipulations to get another "copy" of the formula, and then solve for the formula. << p. 155 of text >> QED (end of proof) DOUBLE SUMMATIONS: (used, for instance, in analysis of time taken by nested loops in a program) << Example 13 >> TABLE 2 on page 157 lists some very useful formulas for commonly occurring sums. Become familiar with them (at least know where to look them up). << Example 15 >> Sometimes it's useful in discrete math and its CS applications to know formulas for infinite series (for instance, might be easier to approximate a finite sum with an infinite one): << Example 16 >> Let's go back to earlier discussion about size, or CARDINALITY, of sets. For finite sets, it is easily defined as the number of elements in the set. What about for infinite sets? Sets A and B have the same CARDINALITY iff there is a one-to-one correspondence from A to B. A set is called COUNTABLE if it is either finite or has the same cardinality as the set of positive integers Z+. This cardinality is called aleph-null. Example: The set of odd positive integers is countable. Reason: Show there is a one-to-one correspondence between the set S of odd positive integers and Z+. First, let's think with pictures. << Fig 1 on p. 159 >> Now let's come up with the mathematical formalism capturing the intuition in the figure. Let's try f(n) = 2n - 1. Why is f(n) one-to-one? If f(n) = f(m), then 2n - 1 = 2m - 1, so n = m. Why is f(n) onto? Consider any odd positive integer t. We have to show there is some positive integer n such that f(n) = t. By definition of odd, t = 2k - 1 for some positive integer k. So f(k) = t. --------------- Alternative way to think of this: To show a set is countable, we need to show a way to list its elements so that we don't leave out any. The first element in the list is the image of 1, the second is the image of 2, etc. Example: The set of *all* integers is countable. Reason: Come up with some way to list all the integers. First try: list all the positive integers, then list 0, then list all the negative integers. What's wrong with this? How about this instead? 0, 1, -1, 2, -2, 3, -3, ... This way works. The mathematical formula is f(n) = n/2 if n is even and f(n) = -(n-1)/2 when n is odd. Check that it is one-to-one and onto. Example: The set of all positive *rational* numbers is countable. Reason: << Fig. 2 on p. 159 >> The rule is: first list the positive rational numbers p/q with p+q = 2, then those with p+q = 3, then those with p+q = 4, etc. An important point is to avoid duplicates (i.e., 1/2 = 2/4 so we don't want to list both of them). I.e., include p/q in the list iff p and q have no factors in common. What about the real numbers? THEOREM (Example 21): The set of real numbers is uncountable. PROOF: By contradiction. Assume that the set of real numbers *is* countable. Then the set of real numbers between 0 and 1 is countable. Let's list those numbers, using their decimal expansion: r_1 = r_2 = r_3 = r_4 = . . . If we can come up with a new real number between 0 and 1 that is not in the list, we will have contradicted the assumption that all such reals *are* in the list, which means the list was not complete in the first place, and thus this set of reals is uncountable. How do we come up with a new real number not in the list? Let's make a number that differs from each number in the list in at least one place in the decimal expansion. More specifically, let r be the number whose i-th digit after the decimal point is * 4 if the i-th digit of r_i is *not* 4, and is * 5 if the i-th digit of r_i *is* 4 For instance, suppose r_1 = .23794102... r_2 = .44590138... r_3 = .09118764... r_4 = .80553900 etc. Then r would equal .4544... Thus we've made sure that r differs from r_1 in decimal place 1, r differs from r_2 in decimal place 2, r differs from r_3 in decimal place 3, etc. Since each real number has a unique decimal expansion, r is not equal to any of the r_i's. So r is not in the list. This is called the CANTOR DIAGONALIZATION ARGUMENT.