Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee,...

41
Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution- NonCommercial - ShareAlike 3.0 Unported License . Based on a work at www.peerinstruction4cs.org .

Transcript of Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee,...

Page 2: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

2

INFINITE LOOPS“ARE WE THERE YET?”

Flashback from CSE 8A/B:

Page 3: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

3

If there exists a TM M that accepts every string in L, and rejects all other strings, then L is:

a) Turing decidableb) Turing recognizable, but not decidablec) Not enough information

Page 4: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

4

If there exists a TM M that accepts every string in L, and loops for all strings not in L, then L is:

a) Turing decidableb) Turing recognizable, but not decidablec) Not enough information

Page 5: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

5

If there exists a for all TM M such that M recognizes L, M is not a

decider, then L is:

a) Turing decidableb) Turing recognizable, but not decidablec) Not enough information

Page 6: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

6

DECIDABILITYIMPORTANT DECIDABLE LANGUAGES

More Proofs

Page 7: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

7

String Encoding Notation < >

• Turing Machines always take a single input string of finite length– M(w)

• But sometimes we want them to take as input several strings, or an object, or several objects

• We use < > to denote “encode this as a single string”– Several strings can be combined into one string by using a

delimiter character not in the main alphabet• <s1,s2,s3,s4>

– Object(s) can be encoded as a finite string • A video V can be encoded as a (very long) string of 0’s and 1’s <V>• The diagram for TM M can be encoded as a string <M>• Three TM’s M1, M2 and M3 <M1,M2,M3>

Page 8: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

String Encoding Notation < >

• Since we already know these encodings can be done, we just put triangle brackets around them to denote “encode this as a string”– For standard objects (TMs, DFAs, etc), no further explanation is

necessary—use book as your guide

• NOTE! Languages CANNOT generally be encoded as a string because if there are infinitely many strings, there is no way to concatenate all the strings together into a single finite string– DFAs, NFAs, REs, PDAs, CFGs and TMs can all be encoded as strings

(<D> <N> <M> etc) – However their languages cannot

Page 9: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

Prove that the class of Turing-decidable languages is closed under Union

• Given: Two decidable languages A and B, and TMs that decide them, MA and MB.

• Want to Show: A TM MU that decides A U B.• Construction:– MU(w) = //w is a string

1. Simulate running MA(w) – If it accepts, accept. If it rejects, go to step 2:

2. Simulate running MB(w)– If it accepts, accept. If it rejects, reject.

• Correctness: //this is for you to debate!• Conclusion: MU is a TM that decides AUB, therefore AUB is

decidable, and decidable languages are closed under union. QED.

This proof is:(a) Correct(b) Incorrect

Page 10: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

Prove that the class of Turing-recognizable languages is closed under Union

• Given: Two Turing-recognizable languages A and B, and TMs that recognize them, MA and MB.

• Want to Show: A TM MU that recognizes A U B.• Construction:– MU(w) = //w is a string

1. Simulate running MA(w) – If it accepts, accept. If it rejects, go to step 2:

2. Simulate running MB(w)– If it accepts, accept. If it rejects, reject.

• Correctness: //this is for you to debate!• Conclusion: MU is a TM that recognizes AUB, therefore AUB is Turing-

recognizable, and Turing-recognizable languages are closed under union. QED.

This proof is:(a) Correct(b) Incorrect

Page 11: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

Prove that the class of Turing-recognizable languages is closed under Intersection

• Given: Two Turing-recognizable languages A and B, and TMs that recognize them, MA and MB.

• Want to Show: A TM MI that recognizes A intersect B.• Construction:– MI(w) = //w is a string

1. Simulate running MA(w) – If it rejects, reject. If it accepts, go to step 2:

2. Simulate running MB(w)– If it accepts, accept. If it rejects, reject.

• Correctness: //this is for you to debate!• Conclusion: MI is a TM that recognizes A intersect B, therefore A

intersect B is Turing-recognizable, and Turing-recognizable languages are closed under intersection. QED.

This proof is:(a) Correct(b) Incorrect

Page 12: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

Prove that the class of Turing-decidable languages is closed under Intersection

• Given: Two decidable languages A and B, and TMs that decide them, MA and MB.

• Want to Show: A TM MI that decides A intersect B.• Construction:– MI(w) = //w is a string

1. Simulate running MA(w) – If it rejects, reject. If it accepts, go to step 2:

2. Simulate running MB(w)– If it accepts, accept. If it rejects, reject.

• Correctness: //this is for you to debate!• Conclusion: MI is a TM that decides A intersect B, therefore A

intersect B is decidable, and decidable languages are closed under intersection. QED.

This proof is:(a) Correct(b) Incorrect

Page 13: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

Prove that the class of Turing-decidable languages is closed under Complement

• Given: A decidable language A, and a TM that decides it, MA.

• Want to Show: A TM MC that decides the complement of A.• Construction:– MC(w) = //w is a string

1. Simulate running MA(w) – If it accepts, reject. If it rejects, accept.

• Correctness: //this is for you to debate!• Conclusion: MC is a TM that decides the complement of A,

therefore the complement of A is decidable, and decidable languages are closed under complement. QED.

This proof is:(a) Correct(b) Incorrect

Page 14: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

Prove that the class of Turing-recognizable languages is closed under Complement

• Given: A Turing-recognizable language A, and a TM that recognizes it, MA.

• Want to Show: A TM MC that recognizes the complement of A.• Construction:– MC(w) = //w is a string

1. Simulate running MA(w) – If it accepts, reject. If it rejects, accept.

• Correctness: //this is for you to debate!• Conclusion: MC is a TM that recognizes the complement of A,

therefore the complement of A is Turing-recognizable, and Turing-recognizable languages are closed under complement. QED.

This proof is:(a) Correct(b) Incorrect

Page 15: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

15

Co-Turing-Recognizable Languages

• Our current classes of langauges:– Regular– Context-free– Turing-decidable (AKA just “decidable”)– Turing-recognizable (or r.e.)– co-Turing-recognizable (or co-r.e.)

• We now have the new class “co-Turing-recognizable” (or “co-r.e.” for short)– Language A is co-r.e. if the complement of A is r.e.

Page 16: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

16

CARDINALITYINFINITY AND INFINITIES

To infinity, and beyond! (really)

Page 17: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

17

Set Theory and Sizes of Sets

• How can we say that two sets are the same size?

• Easy for finite sets--what about infinite sets?• Georg Cantor (1845-1918), who invented Set

Theory, proposed a way of comparing the sizes of two sets that does not involve counting how many things are in each– Works for both finite and infinite

• SET SIZE EQUALITY:– Two sets are the same size if there is a bijective

(one-to-one and onto) function mapping from one to the other

– Intuition: neither set has any element “left over” in the mapping

Page 18: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

18

One-to-one and Onto

f is one-to-one but NOT onto.• Does this prove that |N| ≠ |N|?Draw a function that is onto but not one-to-one.

Page 19: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

19

One-to-one and Onto

f is:a) One-to-Oneb) Ontoc) Correspondence (both (a) and (b))d) Neither

Page 20: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

20

One-to-one and Onto

f is:a) One-to-Oneb) Ontoc) Correspondence (both (a) and (b))d) Neither

Page 21: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

21

It gets even weirder:Rational Numbers

1/1 1/2 1/3 1/4 1/5 1/6 …

2/1 2/2 2/3 2/4 2/5 2/6 …

3/1 3/2 3/3 3/4 3/5 3/6 …

4/1 4/2 4/3 4/4 4/5 4/6 …

5/1 5/2 5/3 5/4 5/5 5/6 ...

6/1 6/2 6/3 6/4 6/5 6/6

… … … … … … …

Q = {m/n | m,n are in N}

Page 22: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

22

Sizes of Infinite Sets

• The number of Natural Numbers is equal to the number of positive Even Numbers, even though one is a proper subset of the other!– |N| = |E+|, not |N| = 2|E+|

• The number of Rational Numbers is equal to the number of Natural Numbers– |N| = |Q|, not |Q| ≈ |N|2

• But it gets even weirder than that:• It might seem like Cantor’s definition of “same size” for sets is

so overly broad, that any two sets of infinite size could be proven to be the “same size”– Not so!!!!

Page 23: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

23

Thm. |R| != |N|Proof by contradiction: Assume |R| = |N|, so a correspondence f exists

between N and R.

• Want to show: no matter how f is designed (we don’t know how it is designed so we can’t assume anything about that), it cannot work correctly.

• Specifically, we will show a number z in R that can never be f(n) for any n, no matter how f is designed.

• Therefore f is not onto, a contradiction.

Page 24: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

24

Thm. |R| != |N|Proof by contradiction: Assume a correspondence f exists between N and

R.

n f(n)

1 .100000…

2 .333333…

3 .314159…

… …

• We construct z as follows:• z’s nth digit is the nth digit of f(n), PLUS ONE*

(*wrap to 1 if the digit is 9)

• Below is an example f

What is z in this example? a) .244…b) .134…c) .031…d) .245…

Page 25: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

25

Thm. |R| != |N|Proof by contradiction: Assume a correspondence f exists between N and

R.

n f(n)

1 .d11d12

d13d14

2 .d21d22

d23d24

3 .d31d32

d33d34

… …

• We construct z as follows:• z’s nth digit is the nth digit of f(n), PLUS ONE*

(*wrap to 1 if the digit is 9)

• Below generalized version of f(n)

What is z?a) .d11

d12d13

b) .d11d22

d33 …

c) .[d11+1] [d22

+1] [d33+1]

d) .[d11+1] [d21

+1] [d31+1]

Page 26: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

26

Thm. |R| != |N|Proof by contradiction: Assume a correspondence f exists between N and

R.

n f(n)

1 .d11d12

d13d14

2 .d21d22

d23d24

3 .d31d32

d33d34

… …

• How do we reach a contradiction?• Must show that z cannot be f(n) for any n• How do we know that z ≠ f(n) for any n?

a) We can’t know if z = f(n) without knowing what f is and what n is

b) Because z’s nth digit differs from n‘s nth digit

c) Because z’s nth digit differs from f(i)’s nth digit

Page 27: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

Thm. |R| != |N|

27

• Proof by contradiction: Assume |R| = |N|, so a correspondence f exists between N and R.

• Want to show: f cannot work correctly. • Let z = [z’s nth digit = (nth digit of f(n)) + 1]. • Note that z is in R, but for all n in N, z != f(n).

• Therefore f is not onto, a contradiction. • So |R| ≠ |N|• |R| > |N|

Page 28: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

Diagonalization

28

n f(n)1 .d11

d12d13

d14d15

d16d17

d18d19

2 .d21d22

d23d24

d25d26

d27d28

d29…

3 .d31d32

d33d34

d35d36

d37d38

d39…

4 .d41d42

d43d44

d45d46

d47d48

d49…

5 .d51d52

d53d54

d55d56

d57d58

d59…

6 .d61d62

d63d64

d65d66

d67d68

d69…

7 .d71d72

d73d74

d75d76

d77d78

d79…

8 .d81d82

d83d84

d85d86

d87d88

d89…

9 .d91d92

d93d94

d95d96

d97d98

d99…

… …

Page 29: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

29

Some infinities are more infinite than other infinities

• Natural numbers are called countable• Any set that can be put in correspondence with N

is called countable (ex: E+, Q)• Real numbers are called uncountable• Any set that can be put in correspondence with R

is called “uncountable”

• But it gets even weirder…• There are more than two categories!

Page 30: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

30

Some infinities are more infinite than other infinities

• |N| is called 0א

• |E+| = |Q| = 0א

• |R| is maybe 1א

• Although we just proved that |N| < |R|, and nobody has ever found a different infinity between |N| and |R|, mathematicians haven’t proved that there are not other infinities between |N| and |R|, making |R| = 2א or greater

• Sets exist whose size is 3א, 2א, 1א, 0א …• An infinite number of aleph numbers!• An infinite number of different infinities

Page 31: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

31

Famous People: Georg Cantor (1845-1918)

• His theory of set size, in particular transfinite numbers (different infinities) was so strange that many of his contemporaries hated it – Just like many CSE 105 students!

• “scientific charlatan” “renegade” “corrupter of youth”• “utter nonsense” “laughable” “wrong”• “disease”• “I see it, but I don't believe it!” –Georg Cantor

“The finest product of mathematical genius and one of the supreme achievements of purely intellectual human activity.” –David Hilbert

Page 32: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

32

What does this have to do with computer science? Or Turing Machines?

• The set of all possible Turing Machines is countable• The set of all possible languages is uncountable• Therefore….

a) The language NATURAL = {<w> | w is a natural number} is not Turing decidable

b) Some languages are not Turing decidablec) Some languages are not Turing recognizabled) Some Turing Machines don’t recognize any languagee) None or more than one of these

• That’s a start, but more diagonalization gets us a more specific result

Page 33: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

33

PARADOXXODARAPPreparing for DIAGONALIZATION!

XODARAPPARADOX

Page 34: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

34

Warm-upYou can prove a language is Regular by drawing a DFA that recognizes the language.

a) TRUEb) FALSEc) Not enough information to

decide between (a) and (b)d) Other

Dr. Lee wrote down number 5.

a) TRUEb) FALSEc) Not enough information to

decide between (a) and (b)d) Other

Page 35: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

35

This sentence is false.

a) TRUEb) FALSEc) Not enough information to decide between

(a) and (b)d) Other

Page 36: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

36

Liar’s Paradox

• “This sentence is false.”– This has been perplexing people since at least the

Greeks in 4th century BCE (2300 years!)

• What are some key features of this that make it a paradox?

Page 37: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

37

Two more examples

This sentence is 36 characters long.a) TRUEb) FALSEc) Not enough information to

decide between (a) and (b)d) Other

This sentence is in French.

a) TRUEb) FALSEc) Not enough infod) Other

Page 38: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

38

The Barber

• A certain town has only one barber (a man). Every man in the town is clean-shaven. For each man m in the town, the barber shaves m if and only if m does not shave himself.

• Question: Does the barber shave himself?a) YESb) NOc) Not enough information d) Other

Page 39: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

39

Making Lists

• Suppose you have many, many lists.

• So many, in fact, that some of your lists are lists of lists (to help you organize your lists), and some of them even include themselves.

Page 40: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

40

List Organization Question• Since some of your lists include themselves, and

you know that self-reference is dangerous business, you make a list of all lists that do not include themselves.

• Call this list NON-DANGER-LIST.• Question:– Should NON-DANGER-LIST include itself?a) YESb) NOc) Not enough informationd) Other

Page 41: Theory of Computation 1 Theory of Computation Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike.

41

Grandparent Paradox(Time Travel Paradox)

• You travel back in time and prevent one pair of your biological grandparents from ever meeting each other (assume this prevents your birth).

• Pop culture version: