CISC 1100: Structures of Computer Sciencecschweikert/cisc1100/ch02h.pdf · CISC 1100: Structures of...

Preview:

Citation preview

CISC 1100: Structures of Computer ScienceChapter 2

Sets and Sequences

Fordham UniversityDepartment of Computer and Information Sciences

Fall, 2010

CISC 1100/Fall, 2010/Chapter 2 1 / 49

Outline

Sets

Basic defnitionsNaming and describing setsComparison relations on setsSet operationsPrinciple of Inclusion/Exclusion

Sequences

Finding patternsNotation

Closed formRecursive formConverting between them

Summations

CISC 1100/Fall, 2010/Chapter 2 2 / 49

Sets

Set: a collection of objects (the members or elements of theset)

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 wemight want to ever consider (restrict our attention to whatmatters)

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 6∈ A means “x is not an element of A”

So

e ∈ {a, e, i , o, u}f 6∈ {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 : 13x ∈ Z} = {0, 3, 6, 9, . . . }

{ x ∈ R≥0 : x2 = 2} = {√

2}{ x ∈ Q≥0 : x2 = 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 isalso 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} 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 andA 6= B.

{1, 3, 5} ⊂ {1, 2, 3, 4, 5}{1, 2, 3, 4, 5} 6⊂ {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”

A ⊆ B means if x ∈ A then x ∈ B

Examples: Let

A = {purple, blue, orange, red} and B = {blue}.Fill in the missing symbol from the set {∈, /∈,⊆,⊂, *, =, 6=}to correctly complete each of the following statements:

B A ⊂,⊆, 6=blue A ∈

green A /∈A A ⊆, =

{blue, purple} B *, 6=A B *, 6=

CISC 1100/Fall, 2010/Chapter 2 10 / 49

Venn Diagram

Diagram for visualizing sets and set operations

A

B

C

When necessary, indicate universal set via rectangle surroundingthe set circles.

CISC 1100/Fall, 2010/Chapter 2 11 / 49

Venn Diagram (cont’d)

A

B

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)

LetA = {1, 2, 3, 4, 5}B = {0, 2, 4, 6, 8}C = {0, 5, 10, 15}

ThenA ∪ 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}

LetL = {e, g , b, d , f }S = {f , a, c , e}

ThenL ∪ 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 isempty.

CISC 1100/Fall, 2010/Chapter 2 16 / 49

Set operations: Intersection (examples)

LetA = {1, 2, 3, 4, 5}B = {0, 2, 4, 6, 8}C = {0, 5, 10, 15}

ThenA ∩ B = {2, 4}B ∩ A = {2, 4}B ∩ C = {0}

(A ∩ B) ∩ C = ∅

LetL = {e, g , b, d , f }S = {f , a, c , e}

ThenL ∩ 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)

LetA = {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 agiven set:

A{ = A′ = A = U − A.

Venn diagrams dealing with complements generally use asurrounding 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}

ThenP{ = {orange, yellow, indigo, violet}

Let E and O respectively denote the sets of even and oddintegers. 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?

|P(A)| = 2|A|,

i.e.,

if |A| = n, then |P(A)| = 2n.

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 allordered 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)

LetA = {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 6= 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, youfind 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) ontheir hamburgers?Let K = {people who like ketchup} andP = {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:

|A ∪ B| = |A|+ |B| − |A ∩ B|

If A and B are disjoint, then

|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:

A

B

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

|K | = 20 |P| = 30 |T | = 45

|K ∩ P| = 10 |K ∩ T | = 12 |P ∩ T | = 13

|K ∩ P ∩ T | = 8.

Then

|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 asubscript denoting position number of the term (e.g., a1, b7,zk).

For the sequence 2, 4, 6, 8, 10, . . . :

a1 = 2.a2 = 4.an is nth term in the sequence.

CISC 1100/Fall, 2010/Chapter 2 34 / 49

2, 4, 6, 8 , 10, . . .

What is a3? 6

What is a5? 10

What is a6? 12

What is an if n = 5? 10

What is an+1 if n = 5? 12

CISC 1100/Fall, 2010/Chapter 2 35 / 49

Recursive formula

Recursive formula for a sequence: each term is described inrelation to previous term(s). For example:

an = 2an−1

Soa3 = 2a2 = 2 · (2 a1) = 4a1 = 4 · (2a0) = 8a0

= 2 · (8a−1) = 16a−1 = . . .

Problem: Need a starting point (initial condition) such as

a1 = 1

So:an = 2an−1 for n ≥ 2

a1 = 2

Example:

a3 = 2a2 = 2 · (2 a1) = 4a1 = 4 · 2 = 8

CISC 1100/Fall, 2010/Chapter 2 36 / 49

Fibonacci sequence

1, 1, 2, 3, 5, 8, 13, . . .

Recursive formula:

an = an−1 + an−2 for n ≥ 3

a2 = 1

a1 = 1

What’s a10? Top-down solution:

a10 = a9 + a8 = (a8 + a7) + (a7 + a6) = a8 + 2a7 + a6 . . .

Too hard!

Better way? Work bottom-up via a grid.

n 1 2 3 4 5 6 7 8 9 10

an 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 aprogramming language.

Fibonacci formula

an = an−1 + an−2 for n ≥ 3

a2 = 1

a1 = 1

Recursive function

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, . . .

an = an−1 + 2 for n ≥ 2

a1 = 2

1, 3, 6, 10, 15, . . .

an = an−1 + n for n ≥ 2

a1 = 1

2, 2, 4, 6, 10, 16, . . .

an = an−1 + an−2 for n ≥ 3

a2 = 2

a1 = 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, . . .

a1 = 2 = 2 · 1a2 = 4 = 2 · 2a3 = 6 = 2 · 3More generally, an = 2n.

CISC 1100/Fall, 2010/Chapter 2 40 / 49

Find the closed formulas

1, 3, 5, 7, 9, . . . an = 2n − 1

3, 6, 9, 12, 15, . . . bn = 3n

8, 13, 18, 23, 28, . . . cn = 5n + 3

3, 9, 27, 81, 243, . . . dn = 3n

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 givensequence.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 previousterms.

Example: rn = 3n + 4.

n 1 2 3 4 5 . . .

rn 7 10 13 16 19 . . .

We findrn = rn−1 + 3 for n ≥ 2

r1 = 7

CISC 1100/Fall, 2010/Chapter 2 43 / 49

Closed formula ⇒ recursive formula

Can also use algebraic manipulation. Let’s try

rn = 3n + 4

again.

Initial condition is easiest—substitute n = 1 into closed form:

r1 = 3 · 1 + 4 = 7

Recursive formula: Try to describe rn in terms of rn−1:

rn = 3n + 4

rn−1 = 3(n − 1) + 4 = 3n − 3 + 4 = 3n + 1

Sorn − rn−1 = (3n + 4)− (3n + 1) = 3,

i.e.,rn = rn−1 + 3

CISC 1100/Fall, 2010/Chapter 2 44 / 49

Another example

sn = 2n − 2

Initial condition: s1 = 21 − 2 = 0.

Recursive formula: We have

sn = 2n − 2

andsn−1 = 2n−1 − 2

Sosn = 2n − 2 = 2 · 2n−1 − 2 = 2 · 2n−1 − 4 + 2

= 2 · (2n−1 − 2) + 2

= 2sn−1 + 2

CISC 1100/Fall, 2010/Chapter 2 45 / 49

Exercise

Find the recursive formulas for the following sequences:

an = 2n + 7

a1 = 9an = an−1 + 2 for n ≥ 2.

bn = 2n − 1

b1 = 1bn = 2bn−2 + 1 for n ≥ 2.

CISC 1100/Fall, 2010/Chapter 2 46 / 49

Summations

Summing the terms in a sequence: important enough to have itsown notation (“sigma notation”):

n∑i=1

ai = a1 + a2 + · · ·+ an

Parts of speech?

Large Σ: “summation”

i = 1 at bottom: We want to start summation at term #1 ofthe sequence.

n at the top: We want to stop summation at the nth term ofthe sequence

Portion to the right of the Σni=1: Closed form of sequence we

want to sum.

CISC 1100/Fall, 2010/Chapter 2 47 / 49

Examples of Σ-notation:

5∑i=1

(3i + 7) = (3 · 1 + 7) + (3 · 2 + 7) + (3 · 3 + 7) + (3 · 4 + 7)

+ (3 · 5 + 7)

= 10 + 13 + 16 + 19 + 22 = 80

6∑j=2

(j2 − 2) = (22 − 2) + (32 − 2) + (42 − 2) + (52 − 2) + (62 − 2)

= 2 + 7 + 14 + 23 + 34 = 80

Note: Parentheses are important!

5∑i=1

3i + 7 = (3 · 1 + 3 · 2 + 3 · 3 + 3 · 4 + 3 · 5) + 7 = 52

CISC 1100/Fall, 2010/Chapter 2 48 / 49

Converting a sum into Σ-notation

3 + 7 + 11 + 15 + 19 =5∑

i=1

(4i − 1)

=5∑

j=1

(4j − 1)

0 + 3 + 8 + 15 + 24 =5∑

k=1

(k2 − 1)

CISC 1100/Fall, 2010/Chapter 2 49 / 49