CISC 1100: Structures of Computer Science Chapter 2 Sets and Sequences Fordham University Department of Computer and Information Sciences Fall, 2010 CISC 1100/Fall, 2010/Chapter 2 1 / 49
Outline Sets Basic defnitions Naming and describing sets Comparison relations on sets Set operations Principle of Inclusion/Exclusion Sequences Finding patterns Notation Closed form Recursive form Converting between them Summations CISC 1100/Fall, 2010/Chapter 2 2 / 49
Sets Set: a collection of objects (the members or elements of the set) Set-lister notation: curly braces around a list of the elements {a, b, c, d, e, f } {Arizona, California, Massachusetts, 42, 47} A set may contain other sets as elements: { 1, 2, {1, 2} } The empty set = {} contains no elements Can use variables (usually upper case letters) to denote sets C = {b, c, d, f, g, h, j, k, l, m, n, p, q, r, s, t, v, w, x, y, z} Universal set (generally denoted U): contains all elements we might want to ever consider (restrict our attention to what matters) CISC 1100/Fall, 2010/Chapter 2 3 / 49
Enumerating the elements of a set Order doesn t matter {1, 2, 3} = {3, 1, 2} Repetitions don t count {a, b, b} = {a, b} (better yet: don t repeat items in a listing of elements) CISC 1100/Fall, 2010/Chapter 2 4 / 49
Element notation If A is a set, then x A means x is an element of A x A means x is not an element of A So e {a, e, i, o, u} f {a, e, i, o, u} CISC 1100/Fall, 2010/Chapter 2 5 / 49
Some well-known sets Pretty much standard notations: N = {0, 1, 2, 3, 4, 5,... }: the set of natural numbers (non-negative integers). Z = {... 3, 2, 1, 0, 1, 2,...}: the set of all integers. Q: the set of all rational numbers (fractions). R: the set of all real numbers. Less standard (but useful) notations: Z + is the set of positive integers. Z is the set of negative integers. Z 0 is the same as N. R >7 is the set of all real numbers greater than seven. CISC 1100/Fall, 2010/Chapter 2 6 / 49
Set builder notation Rather than listing all the elements N = {0, 1, 2, 3, 4, 5,... } (inconvenient or essentially impossible), describe sets via a property A = { x : p(x) is true } Examples: N = { x x Z and x 0 } N = { x : x Z and x 0 } N = { x Z x 0 } N = { x Z : x 0 } CISC 1100/Fall, 2010/Chapter 2 7 / 49
Set builder notation (cont d) More examples: { x Q : 2x = 7} = {3.5} { x Z : 2x = 7} = {2x x Z} = {..., 4, 2, 0, 2, 4,... } { x N : 1 3x Z} = {0, 3, 6, 9,... } { x R 0 : x 2 = 2} = { 2} { x Q 0 : x 2 = 2} = CISC 1100/Fall, 2010/Chapter 2 8 / 49
Comparison relations on sets A is a subset of B (written A B ) if each element of A is also an element of B. {1, 3, 5} {1, 2, 3, 4, 5} {1, 2, 3, 4, 5} {1, 2, 3, 4, 5} {1, 3, 6} {1, 2, 3, 4, 5} A A for any set A A for any set A A is a proper subset of B (written A B ) if A B and A B. {1, 3, 5} {1, 2, 3, 4, 5} {1, 2, 3, 4, 5} {1, 2, 3, 4, 5} vs. is somewhat like < vs. CISC 1100/Fall, 2010/Chapter 2 9 / 49
Element vs. subset means is an element of means is a subset of Examples: Let A B means if x A then x B A = {purple, blue, orange, red} and B = {blue}. Fill in the missing symbol from the set {, /,,,, =, } to correctly complete each of the following statements: B A,, blue A green A / A A, = {blue, purple} B, A B, CISC 1100/Fall, 2010/Chapter 2 10 / 49
Venn Diagram Diagram for visualizing sets and set operations B A C When necessary, indicate universal set via rectangle surrounding the set circles. CISC 1100/Fall, 2010/Chapter 2 11 / 49
Venn Diagram (cont d) B A C For example, might have A = {Fordham students who ve taken CISC 1100} B = {Fordham students who ve taken CISC 1600} C = {Fordham students who ve taken ECON 1100} CISC 1100/Fall, 2010/Chapter 2 12 / 49
Set operations: Cardinality The number of elements in a set is called its cardinality. We denote the cardinality of S by S. Let A = {a, b, c, d, e, z}. Then A = 6. {a, e, i, o, u} = 5. = 0. {a, {b, c}, d, {e, f, g}, h} = 5. CISC 1100/Fall, 2010/Chapter 2 13 / 49
Set operations: Union Set of all elements belonging to either of two given sets: A B = {x : x A or x B} A B CISC 1100/Fall, 2010/Chapter 2 14 / 49
Set operations: Union (examples) Let A = {1, 2, 3, 4, 5} B = {0, 2, 4, 6, 8} C = {0, 5, 10, 15} Then A B = {0, 1, 2, 3, 4, 5, 6, 8} B A = {0, 1, 2, 3, 4, 5, 6, 8} B C = {0, 2, 4, 5, 6, 8, 10, 15} (A B) C = {0, 1, 2, 3, 4, 5, 6, 8, 10, 15} Let L = {e, g, b, d, f } S = {f, a, c, e} Then L S = {a, b, c, d, e, f, g} CISC 1100/Fall, 2010/Chapter 2 15 / 49
Set operations: Intersection Set of all elements belonging to both of two given sets: A B = {x : x A and x B} A B Note: We say that two sets are disjoint if their intersection is empty. CISC 1100/Fall, 2010/Chapter 2 16 / 49
Set operations: Intersection (examples) Let A = {1, 2, 3, 4, 5} B = {0, 2, 4, 6, 8} C = {0, 5, 10, 15} Then A B = {2, 4} B A = {2, 4} B C = {0} (A B) C = Let L = {e, g, b, d, f } S = {f, a, c, e} Then L S = {e, f } CISC 1100/Fall, 2010/Chapter 2 17 / 49
Set operations: Difference Set of all elements belonging to one set, but not another: A B = {x : x A and x / B} A B Note that A B = A A B CISC 1100/Fall, 2010/Chapter 2 18 / 49
Set operations: Difference (examples) Let A = {1, 2, 3, 4, 5} B = {0, 2, 4, 6, 8} C = {0, 5, 10, 15} Then A B = {1, 3, 5} B A = {0, 6, 8} B C = {2, 4, 6, 8} C B = {5, 10, 15} u(a B) (B A) = (Are you surprised by this?) CISC 1100/Fall, 2010/Chapter 2 19 / 49
Set operations: Complement Set of all elements (of the universal set) that do not belong to a given set: A = A = A = U A. Venn diagrams dealing with complements generally use a surrounding rectangle to indicate the universal set U: A If U and A are finite sets, then A = U A = U A. CISC 1100/Fall, 2010/Chapter 2 20 / 49
Set operations: Complement (examples) Let U = {red, orange, yellow, green, blue, indigo, violet} P = {red, green, blue} Then P = {orange, yellow, indigo, violet} Let E and O respectively denote the sets of even and odd integers. Suppose that our universal set is Z. Then E = O O = E CISC 1100/Fall, 2010/Chapter 2 21 / 49
Set operations: Power Set Set of all subsets of a given set B P(A) if and only if B A P( ) = { } P({a}) = {, {a} } P({a, b}) = {, {a}, {b}, {a, b} } P({a, b, c}) = {, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c} } How many elements does P(A) have? i.e., P(A) = 2 A, if A = n, then P(A) = 2 n. CISC 1100/Fall, 2010/Chapter 2 22 / 49
Set operations: Cartesian Product Ordered pair: Pair of items, in which order matters. (1, 2)... not the same thing as (2, 1) (red, blue) (1, green) Cartesian product (also known as set product): Set of all ordered pairs from two given sets, i.e., A B = {(x, y) x A and y B} CISC 1100/Fall, 2010/Chapter 2 23 / 49
Set operations: Cartesian Product (examples) Let A = {1, 2, 3} B = {a, b, c} C = { 1, 5}, Then... A B = {(1, a), (1, b), (1, c), (2, a), (2, b), (2, c), (3, a), (3, b), (3, c)} B A = {(a, 1), (a, 2), (a, 3), (b, 1), (b, 2), (b, 3), (c, 1), (c, 2), (c, 3)} C A = {( 1, 1), ( 1, 2), ( 1, 3), (5, 1), (5, 2), (5, 3)} B C = {(a, 1), (a, 5), (b, 1), (b, 5), (c, 1), (c, 5)} Note the following: A B B A (unless A = B) A B = A B (that s why it s called product ). CISC 1100/Fall, 2010/Chapter 2 24 / 49
Principle of Inclusion/Exclusion Suppose you run a fast-food restaurant. After doing a survey, you find that 25 people like ketchup on their hamburgers, 35 people like pickles on their hamburgers, 15 people like both ketchup and pickles on their hamburgers. How many people like either ketchup or pickles (maybe both) on their hamburgers? Let K = {people who like ketchup} and P = {people who like pickles}. Then K = 25 P = 35 K P = 15. CISC 1100/Fall, 2010/Chapter 2 25 / 49
Principle of Inclusion/Exclusion K P Since we don t want to count K P twice, we have K P = K + P K P = 25 + 35 15 = 45. CISC 1100/Fall, 2010/Chapter 2 26 / 49
Set operations: cardinalities of union and intersection A B Inclusion/exclusion principle: If A and B are disjoint, then A B = A + B A B A B = A + B See the example at http://tinyurl.com/kr4s3k What about three sets (hamburger eaters who like ketchup, pickles, and tomatoes)? CISC 1100/Fall, 2010/Chapter 2 27 / 49
Principle of Inclusion/Exclusion For three sets: B A C A B C = A + B + C A B A C B C + A B C. CISC 1100/Fall, 2010/Chapter 2 28 / 49
Principle of Inclusion/Exclusion Let K, P, T represent the sets of people who like ketchup, pickles, and tomatoes on their hamburgers. Suppose that Then K = 20 P = 30 T = 45 K P = 10 K T = 12 P T = 13 K P T = 8. K P T = K + P + T K P K T P T + K P T = 20 + 30 + 45 10 12 13 + 8 = 68 CISC 1100/Fall, 2010/Chapter 2 29 / 49
Sequences: Finding patterns What number comes next? 1, 2, 3, 4, 5, 6 2, 6, 10, 14, 18, 22 1, 2, 4, 8, 16, 32 1, 3, 6, 10, 15, 21 1, 2, 6, 24, 120, 720 1, 1, 2, 3, 5, 8, 13, 21 CISC 1100/Fall, 2010/Chapter 2 30 / 49
Discovering the pattern Each term might be related to previous terms Each term might depend on its position number (1st, 2nd, 3rd,... ) Well-known sequences (even numbers, odd numbers) Some (or all) of the above CISC 1100/Fall, 2010/Chapter 2 31 / 49
2, 4, 6, 8, 10,... Can we relate a term to previous terms? Second term is 2 more than first term. Third term is 2 more than second term.. Any given term is 2 more than previous term. CISC 1100/Fall, 2010/Chapter 2 32 / 49
2, 4, 6, 8, 10,... Can we describe each term by its position in the sequence? Term at position 1 is 2. Term at position 2 is 4. Term at position 3 is 6.. Term at position n is 2n. CISC 1100/Fall, 2010/Chapter 2 33 / 49
Mathematical notation Write term in a sequence as a lower case letter, followed by a subscript denoting position number of the term (e.g., a 1, b 7, z k ). For the sequence 2, 4, 6, 8, 10,... : a 1 = 2. a 2 = 4. a n is nth term in the sequence. CISC 1100/Fall, 2010/Chapter 2 34 / 49
2, 4, 6, 8, 10,... What is a 3? 6 What is a 5? 10 What is a 6? 12 What is a n if n = 5? 10 What is a n+1 if n = 5? 12 CISC 1100/Fall, 2010/Chapter 2 35 / 49
Recursive formula Recursive formula for a sequence: each term is described in relation to previous term(s). For example: a n = 2a n 1 So a 3 = 2a 2 = 2 (2 a 1 ) = 4a 1 = 4 (2a 0 ) = 8a 0 = 2 (8a 1 ) = 16a 1 =... Problem: Need a starting point (initial condition) such as a 1 = 1 So: Example: a n = 2a n 1 for n 2 a 1 = 2 a 3 = 2a 2 = 2 (2 a 1 ) = 4a 1 = 4 2 = 8 CISC 1100/Fall, 2010/Chapter 2 36 / 49
Fibonacci sequence 1, 1, 2, 3, 5, 8, 13,... Recursive formula: a n = a n 1 + a n 2 for n 3 a 2 = 1 a 1 = 1 What s a 10? Top-down solution: a 10 = a 9 + a 8 = (a 8 + a 7 ) + (a 7 + a 6 ) = a 8 + 2a 7 + a 6... Too hard! Better way? Work bottom-up via a grid. n 1 2 3 4 5 6 7 8 9 10 a n 1 1 2 3 5 8 13 21 34 55 CISC 1100/Fall, 2010/Chapter 2 37 / 49
Recursion Recursive formula corresponds to recursive function in a programming language. Fibonacci formula Recursive function a n = a n 1 + a n 2 for n 3 a 2 = 1 a 1 = 1 int a(n) { if (n == 1) return 1; if (n == 2) return 1; return a(n-1) + a(n-2); } CISC 1100/Fall, 2010/Chapter 2 38 / 49
Exercise: Find recursive formula 2, 4, 6, 8, 10,... a n = a n 1 + 2 for n 2 a 1 = 2 1, 3, 6, 10, 15,... a n = a n 1 + n for n 2 a 1 = 1 2, 2, 4, 6, 10, 16,... a n = a n 1 + a n 2 for n 3 a 2 = 2 a 1 = 2 CISC 1100/Fall, 2010/Chapter 2 39 / 49
Finding a closed formula Write each term in relation to its position Example: 2, 4, 6, 8, 10,... a 1 = 2 = 2 1 a 2 = 4 = 2 2 a 3 = 6 = 2 3 More generally, a n = 2n. CISC 1100/Fall, 2010/Chapter 2 40 / 49
Find the closed formulas 1, 3, 5, 7, 9,... a n = 2n 1 3, 6, 9, 12, 15,... b n = 3n 8, 13, 18, 23, 28,... c n = 5n + 3 3, 9, 27, 81, 243,... d n = 3 n CISC 1100/Fall, 2010/Chapter 2 41 / 49
Recursive formulas vs. closed formulas Recursive formula It s often easier to find a recursive formula for a given sequence. It s often harder to evaluate a given term. Closed formula It s often harder to find a closed formula for a given sequence. It s often easier to evaluate a given term. CISC 1100/Fall, 2010/Chapter 2 42 / 49
Closed formula recursive formula Write out a few terms. See if you can figure out how a given term relates to previous terms. Example: r n = 3n + 4. We find n 1 2 3 4 5... r n 7 10 13 16 19... r n = r n 1 + 3 for n 2 r 1 = 7 CISC 1100/Fall, 2010/Chapter 2 43 / 49
Closed formula recursive formula Can also use algebraic manipulation. Let s try r n = 3n + 4 again. Initial condition is easiest substitute n = 1 into closed form: r 1 = 3 1 + 4 = 7 Recursive formula: Try to describe r n in terms of r n 1 : So i.e., r n = 3n + 4 r n 1 = 3(n 1) + 4 = 3n 3 + 4 = 3n + 1 r n r n 1 = (3n + 4) (3n + 1) = 3, r n = r n 1 + 3 CISC 1100/Fall, 2010/Chapter 2 44 / 49
Another example s n = 2 n 2 Initial condition: s 1 = 2 1 2 = 0. Recursive formula: We have s n = 2 n 2 and So s n 1 = 2 n 1 2 s n = 2 n 2 = 2 2 n 1 2 = 2 2 n 1 4 + 2 = 2 (2 n 1 2) + 2 = 2s n 1 + 2 CISC 1100/Fall, 2010/Chapter 2 45 / 49
Exercise Find the recursive formulas for the following sequences: a n = 2n + 7 a 1 = 9 a n = a n 1 + 2 for n 2. b n = 2 n 1 b 1 = 1 b n = 2b n 2 + 1 for n 2. CISC 1100/Fall, 2010/Chapter 2 46 / 49
Summations Summing the terms in a sequence: important enough to have its own notation ( sigma notation ): Parts of speech? n a i = a 1 + a 2 + + a n i=1 Large Σ: summation i = 1 at bottom: We want to start summation at term #1 of the sequence. n at the top: We want to stop summation at the nth term of the sequence Portion to the right of the Σ n i=1 : Closed form of sequence we want to sum. CISC 1100/Fall, 2010/Chapter 2 47 / 49
Examples of Σ-notation: 5 (3i + 7) = (3 1 + 7) + (3 2 + 7) + (3 3 + 7) + (3 4 + 7) i=1 + (3 5 + 7) = 10 + 13 + 16 + 19 + 22 = 80 6 (j 2 2) = (2 2 2) + (3 2 2) + (4 2 2) + (5 2 2) + (6 2 2) j=2 = 2 + 7 + 14 + 23 + 34 = 80 Note: Parentheses are important! 5 3i + 7 = (3 1 + 3 2 + 3 3 + 3 4 + 3 5) + 7 = 52 i=1 CISC 1100/Fall, 2010/Chapter 2 48 / 49
Converting a sum into Σ-notation 3 + 7 + 11 + 15 + 19 = = 0 + 3 + 8 + 15 + 24 = 5 (4i 1) i=1 5 (4j 1) j=1 5 (k 2 1) k=1 CISC 1100/Fall, 2010/Chapter 2 49 / 49