snick snack cpsc 121: models of computation 2008/9 winter term 2 sets steve wolfman, based on notes...

42
snick snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Post on 22-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

snick

snack

CPSC 121: Models of Computation2008/9 Winter Term 2

Sets

Steve Wolfman, based on notes by Patrice Belleville and others

Page 2: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Lecture Prerequisites

Read Section 5.1.

Solve problems like Exercise Set 5.1, #1-21.

(Note: the notion of a “partition” is important in CS, but we won’t discuss it in CPSC 121. We’ll talk more about “power sets” very soon.)

Complete the open-book, untimed quiz on WebCT that will be due Tuesday evening March 17th.

Page 3: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Learning Goals: Pre-Class

By the start of class, you should be able to:– Define the set operations union, intersection, complement,

and set difference and the logical operations subset and set equality in terms of predicate logic and set membership ().

– Translate between sets represented explicitly (possibly using ellipses “…”, e.g., {4, 6, 8, …}) and using “set builder” notation (e.g., {x Z+ | x2 > 10 x is even}).

– Execute the union, intersection, complement, set difference, subset, and set equality operations on sets expressed explicitly, using set builder notation, or a combination of these and set operators.

– Interpret the empty set symbol , including the fact that the empty set has no members and that it is a subset of any set.

Page 4: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Learning Goals: In-Class

By the end of this unit, you should be able to:– Define the power set and cartesian product

operations in terms of predicate logic and set membership/subset relations.

– Execute the power set, cartesian product, and cardinality operations on sets expressed through any of the notations discussed so far.

– Apply your proof skills to proofs involving sets.– Relate DFAs to sets.

Page 5: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Quiz 10 Notes

Generally excellent results. Only the subset questions caused many problems, because of the empty set.

Set A is a subset of set B exactly when every element of A is an element of B. Is the empty set a subset of B?

Page 6: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Outline

• Out-of-class notes on set definitions• What’s the Use of Sets

– Historical note– Application to DFAs

• More set operations– Cardinality (size)– Power set (and an induction proof)– Cartesian products (and application to DFAs)

• Set proofs

Page 7: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

What Are Sets Good For?

Historically:

Mathematicians attempted to formalize set theory to create a foundation for all of mathematics. Essentially all mathematical constructs can be defined in terms of sets.

Good news: this means sets are a powerful way to communicate many types of ideas.

Page 8: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Bad News: Russell’s Paradox(and other problems)

Page 9: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

What Are Sets Good For?

Applications for us:

Codifying and communicating ideas. For example, formalizing DFAs...

Page 10: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

What is a DFA?

The states are: ______

The start state is: ____

The accepting states are: _____________

The input language is: ________________

What’s left?

a

b

b

a,b

a,ba

Page 11: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

The Transition Function

The arrows in our DFA have the following properties:

• An arrow starts from every state, for every letter in the input alphabet.

• The arrows lead to other states.

We’ll formalize this as the function soon, which will complete our DFA!

a

b

b

a,b

a,ba

Page 12: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

What is a DFA?(Deterministic Finite Automaton)Q is the (finite) set of

states.

q0 is the start state, and q0 Q.

F is the set of accepting states, and F Q.

Is the (finite) set of letters in the input language.

is the transition function, to be described.

These are just standard names we use,but the constraints make sense regardless of names.

a

b

b

a,b

a,ba

Page 13: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Problem: Formalizing an Example DFA

For this DFA:

Q = { }

q0 =

F = { }

= { }

: ( , )

( , )

( , )

( , )

( , )

( , )

( , )

( , )

a

b

b

a,b

a,ba

Page 14: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Problem: Testing the DFA Formalism

Q is the set of states

q0 is the start state, and q0 Q.

F is the set of accepting states, and F Q.

Is the set of letters in the input language.

is the transition function, to be described.

Must a DFA have a start state?

Can a DFA have more than one start state?

Must a DFA have an accepting state?

Can all states in a DFA be accepting?

Can the start state be accepting?

Page 15: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Testing the DFA Formalism

Must a DFA have a start state?

Can a DFA have more than one start state?

Must a DFA have an accepting state?

Can all states in a DFA be accepting?

Can the start state be accepting?

q0 is the start state; so, yes.

q0 is the only start state; so, no.

F Q. Is Q?

F Q. Is Q Q?

q0 Q and F Q. Can q0 F?

Page 16: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Must a DFA Have an Accepting State?

What does a DFA with no accepting states look like? What language does it accept?

Page 17: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Can All States in a DFA Be Accepting?

What does a DFA with no rejecting states look like? What language does it accept?

Page 18: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Outline

• Out-of-class notes on set definitions• What’s the Use of Sets

– Historical note– Application to DFAs

• More set operations– Cardinality (size)– Power set (and an induction proof)– Cartesian products (and application to DFAs)

• Set proofs

Page 19: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

How Big Is a Set?

How big are these sets?

A = {1, 3}B = {1, 3, 27, snow}C = { {1, 3}, {1, 3, 27, snow} }D = E = NF = { …, -4, -2, 0, 2, 4, … }

Page 20: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Cardinality

The cardinality of a set A — denoted |A| — is the set’s size.

For finite sets, the cardinality of the set is the number of elements it contains…

|A| = |{1, 3}| = 2|B| = |{1, 3, 27, snow}| = 4|C| = |{ {1, 3}, {1, 3, 27, snow} }| = ??|D| = || = ??

Page 21: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Cardinality of Infinite Sets?

For now, we won’t worry about the cardinality of infinite sets. Why?

Well, we can’t count the members of such sets.

Side note: Depending on our definition, however, the results can be surprising. For example, under the standard definition of cardinality, these have the same cardinality:E = N = {1, 2, 3, … }F = { …, -4, -2, 0, 2, 4, … }

Why? Try “folding” the set F over creatively.

Page 22: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Problem: Cardinality Exercises

Given the definitions:

A = {1, 2, 3}

B = {2, 4, 6, 8}

What are:

|A| = _________

|B| = _________

|A B| = _________

|A B| = _________

|A – B| = _________

|B – A| = _________

|{{}}| = _________

|{}| = _________

|{{}}| = _________

Page 23: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Outline

• Out-of-class notes on set definitions• What’s the Use of Sets

– Historical note– Application to DFAs

• More set operations– Cardinality (size)– Power set (and an induction proof)– Cartesian products (and application to DFAs)

• Set proofs

Page 24: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

A Touch of Logic

Consider the four propositional logic variables p, q, r, and s.

What are all the different ways can we assign truth values to each of these?

Now, imagine a set S that contains exactly the true variables among p, q, r, and s.

What are the possible “Ss” (depending on p, q, r, and s’s truth values)?

Page 25: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

A Touch of Logic: Rephrased

Consider the set {p, q, r, s}.

What are all the subsets of this set?

Equivalently, what is: {S U | S {p,q,r,s}}?

Page 26: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Power Sets

The power set of a set T — denoted P(T) — is the set of all subsets of T:{ S U | S T }

Page 27: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Using Logic to Build Power Sets

p q r s Set from P(…)

F F F F {}

F F F T {s}

F F T F {r}

F F T T {r,s}

F T F F {q}

F T F T {q,s}

F T T F {q,r}

F T T T {q,r,s}

p q r s Set from P(…)

T F F F {p}

T F F T {p,s}

T F T F {p,r}

T F T T {p,r,s}

T T F F {p,q}

T T F T {p,q,s}

T T T F {p,q,r}

T T T T {p,q,r,s}

So, how big is the power set of a set?

P({p,q,r,s}) = …

Page 28: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Problem: Power Set Exercises

Given the definitions: A = {1, 2} B = {2, 4, 6}

What are: P(A) = _____________________________ P(B) = _____________________________ |P(A)| = _____________________________ |P(B)| = _____________________________ |P(P(A))|= _____________________________ P(P(A)) = _____________________________ P(A B) = _____________________________

Page 29: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Cardinality of a Finite Power Set

Theorem: |P(S)| = 2|S|

Base case: When |S| = 0, what is S?

What is P(S)? How do we know?

Page 30: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Cardinality of a Finite Power Set

Induction Hypothesis: Assume for all sets S of cardinality n 0, |P(S)| = 2|S|.

Inductive step:

To prove: Given the induction hypothesis, for all sets T, |P(T)| = 2|T| when |T| = n+1.

The key is to drop an element from a set of size n+1.Then, take the power set.

Then, consider how to put the element back in.

Page 31: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Outline

• Out-of-class notes on set definitions• What’s the Use of Sets

– Historical note– Application to DFAs

• More set operations– Cardinality (size)– Power set (and an induction proof)– Cartesian products (and application to DFAs)

• Set proofs

Page 32: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Tuples

A tuple is an ordered collection of elements. (An n-tuple is a tuple with n elements.)

Two tuples are equal when each pair of corresponding elements is the same:

(a, 1, ) = (a, 5 – 4, A A)(a, b, c) (a, c, b)

Actual fact: database people love the word “tuple”.And… why shouldn’t they? Say “two-tuple”. Isn’t that great?

Page 33: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Formalizing DFAs: Where the Arrows Come From

The arrows in our DFA have the following properties:

• An arrow starts from every state, for every letter in the input alphabet.

• The arrows lead to other states.

We’ll formalize this as a function soon.

What is the set of “things” for which there is an arrow?

a

b

b

a,b

a,ba

t

m

b

g

Page 34: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Cartesian Products

The Cartesian product of the sets A and B — denoted A B — is the set of all tuples whose first element is drawn from A and whose second element is drawn from B.

In other words, A B = {(a,b) | a A b B}

Page 35: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Cartesian Products andthe Cartesian Plane

Let’s visualize N N:

1

2

3

4

5

1 2 3 4 5 6 7 8

Page 36: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Calculating Cartesian Products

What is {1,2,3} {a,b}:

1

2

3

a b

( , )

( , )

( , )

( , )

( , )

( , )

Page 37: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Formalizing DFAs: Where the Arrows Come From

The arrows in our DFA have the following properties:

• An arrow starts from every state, for every letter in the input alphabet.

• The arrows lead to other states.

We’ll formalize this as a function soon.

Each tuple of a state and a letter has an arrow.There’s an arrow for every member of Q

a

b

b

a,b

a,ba

t

m

b

g

Page 38: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Example DFA, Revisited

For this DFA:

Q = { t, m, b, g}

q0 = t

F = { t, b }

= { a, b }

: (t, a) m

(t, b) b

(m, a) g

(m, b) b

(b, a) b

(b, b) b

(g, a) g

(g, b) g

a

b

b

a,b

a,ba

t

m

b

g

Page 39: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Outline

• Out-of-class notes on set definitions• What’s the Use of Sets

– Historical note– Application to DFAs

• More set operations– Cardinality (size)– Power set (and an induction proof)– Cartesian products (and application to DFAs)

• Set proofs

Page 40: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Problem: Proofs with Sets (1/2)

Prove by contradiction that for all sets A and B, if A B then A B = { }.

(Note: B contains all elements in the universe that are not in B.)

Page 41: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Problem: Proofs with Sets (2/2)

Prove for all sets A and B, if A B then P(A) P(B).

(Note: if A B and B C, then A C.)

Page 42: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Sets Steve Wolfman, based on notes by Patrice Belleville and others

Learning Goals: In-Class

By the end of this unit, you should be able to:– Define the power set and cartesian product

operations in terms of predicate logic and set membership/subset relations.

– Execute the power set, cartesian product, and cardinality operations on sets expressed through any of the notations discussed so far.

– Apply your proof skills to proofs involving sets.– Relate DFAs to sets.