CS242 Module 9 Part-2
CS242 Module 9 Part-2
26/12/2021
Theory of Computing
https://people.eecs.berkeley.edu/~sseshia/172/lectures/Lecture12.pdf
This Presentation is mainly dependent on the textbook: Introduction to Automata Theory, Languages, and Computation: Global Edition, 3rd edition (2013) PHI
by John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman
• Problems That Computers Cannot Solve
Integers, Strings, and Other Things
7
Example: Text
8
Binary Strings to Integers
9
Example: Proofs
10
Proofs – (3)
11
Example: Encoding Proofs
1110100011111100000101110101…
A given
An ex- expression
pression follows
Notice this
1 could not
be part of
the “end”
12
Example: Programs
13
Finite and Infinite Sets
• Intuitively, a finite set is a set for which there is a
particular integer that is the count of the number of
members.
• Example: {a, b, c} is a finite set; its cardinality is 3.
• It is impossible to find a 1-1 mapping between a
finite set and a proper subset of itself.
• Formally, an infinite set is a set for which there is a 1-
1 correspondence between itself and a proper subset
of itself.
• Example: the positive integers {1, 2, 3,…} is an infinite set.
• There is a 1-1 correspondence 1<->2, 2<->4, 3<->6,…
between this set and a proper subset (the set of even
integers).
14
Countable Sets
• A countable set is a set with a 1-1 correspondence with
the positive integers.
• Hence, all countable sets are infinite.
• Example: All integers.
• 0<->1; -i <-> 2i; +i <-> 2i+1.
• Thus, order is 0, -1, 1, -2, 2, -3, 3,…
• Examples: set of binary strings, set of Java programs.
• Examples: Pairs of Integers
• Order the pairs of positive integers first by sum, then by first
component:
• [1,1], [2,1], [1,2], [3,1], [2,2], [1,3], [4,1], [3,2],…, [1,4], [5,1],…
• Interesting exercise: figure out the function f(i, j) such that the
pair [i, j] corresponds to the integer f(i, j) in this order.
15
Enumerations
16
How Many Languages?
17
Proof – Continued
Lj
Is x in L?
If so, x is not in L by definition of L. j-th
18
Diagonalization Picture
Strings
1 2 3 4 5 …
1 1 0 1 1 0 …
2 1
Languages 3 0
4 0
5 1
… …
19
Diagonalization Picture
Strings Can’t be
Flip each a row –
diagonal 1 2 3 4 5 … it disagrees
entry in an entry
1 0 0 1 1 0 … of each row.
2 0
Languages 3 1
4 1
5 0
… …
20
Proof – Concluded
• We have a contradiction: x is neither in L nor not in L, so
our sole assumption (that there was an enumeration of
the languages) is wrong.
• Comment: This is really bad; there are more languages
than programs.
• E.g., there are languages with no membership algorithm.
21
• The Turing Machine
Turing Machines
• These are very powerful (but abstract) machines
that could simulate any modern day computer.
• Why design such a machine?
• A problem is solved means, for every input,
it answers YES or NO.
• If a problem cannot be solved even using a TM, then it
implies that the problem is undecidable.
• Computability vs. Decidability
23
A Turing Machine (TM)
• M = (Q, ∑, , , q0, B, F)
Tape head
Infinite tape with tape symbols
… B B B X1 X2 X3 … Xi … Xn B B …
25
ID of a TM
• Instantaneous Description or ID :
• X1X2…Xi-1qXiXi+1…Xn
means:
• q is the current state
• Tape head is pointing to Xi
• X1X2…Xi-1XiXi+1…Xn are the current tape symbols
26
Way to check for Membership
• Initial condition:
• The (whole) input string w is present in TM,
preceded and followed by infinite blank symbols
• Final acceptance/rejection:
• Accept w if TM enters final state and halts
• If TM halts and not final state, then reject
27
Example: L = {0n1n | n ≥ 1}
• Strategy: w = 000111
…
B B 0 0 0 1 1 1 B B … B B X X 0 Y Y 1 B B …
… …
…
… B B X 0 0 1 1 1 B B … B B X X X Y Y 1 B B …
… …
… B B X 0 0 Y 1 1 B B … B B X X X Y Y Y B B …
…
… B B X X 0 Y 1 1 B B …
B B X X X Y Y Y B B …
Accept
28
TM for L = {0n1n | n ≥ 1}
29
TM for L = {0n1n | n ≥ 1} using State Diagram
Tabular representation of the state diagram
q3 - - - (q3,Y,R) (q4,B,R)
*q4 - -- - - -
30
TMs for calculations
31
Example 2: Monus subtraction
“m -- n” = max{m-n,0}
0m10n ➔ ...B 0m-n B.. (if m>n)
...BB…B.. (otherwise)
1. For every 0 on the left (mark X), mark off a 0 on the right
(mark Y)
2. Repeat process, until one of the following happens:
1. // No more 0s remaining on the left of 1
Answer is 0, so flip all excess 0s on the right of 1 to Bs
(and the 1 itself) and halt
2. //No more 0s remaining on the right of 1
Answer is m-n, so simply halt after making 1 to B
32
Example 3: Multiplication
• Pseudocode:
1. Move tape head back & forth such that for every 0
seen in 0m, write n 0s to the right of the last
delimiting 1
2. Once written, that zero is changed to B to get
marked as finished
3. After completing on all m 0s, make the remaining
n 0s and 1s also as Bs
33
• Programming Techniques for Turing Machines
Turing-Machine Theory
35
Picture of a Turing Machine
Action: based on
the state and the
tape symbol under
the head: change
state, rewrite the
symbol and move the
State head one square.
... A B C A D ...
36
Why Turing Machines?
37
Then Why Not Finite-State Machines to Model
Computers?
38
Turing-Machine Formalism
• A TM is described by:
1. A finite set of states Q.
2. An input alphabet Σ.
3. A tape alphabet Γ.
4. A transition function δ.
5. A start state q0, in Q.
6. A blank symbol B, in Γ- Σ.
All tape except for the input is blank initially.
7. A set of final states F ⊆ Q.
39
Conventions
40
The Transition Function
41
Actions of the PDA
42
Example: Turing Machine
43
Example: Turing Machine – (2)
44
Simulation of TM
δ(q, 0) = (q, 0, R)
δ(q, 1) = (f, 0, R)
δ(q, B) = (q, 1, L)
... B B 0 0 B B ...
45
Simulation of TM
δ(q, 0) = (q, 0, R)
δ(q, 1) = (f, 0, R)
δ(q, B) = (q, 1, L)
... B B 0 0 B B ...
46
Simulation of TM
δ(q, 0) = (q, 0, R)
δ(q, 1) = (f, 0, R)
δ(q, B) = (q, 1, L)
... B B 0 0 B B ...
47
Simulation of TM
δ(q, 0) = (q, 0, R)
δ(q, 1) = (f, 0, R)
δ(q, B) = (q, 1, L)
... B B 0 0 1 B ...
48
Simulation of TM
δ(q, 0) = (q, 0, R)
δ(q, 1) = (f, 0, R)
δ(q, B) = (q, 1, L)
... B B 0 0 1 B ...
49
Simulation of TM
δ(q, 0) = (q, 0, R)
δ(q, 1) = (f, 0, R)
δ(q, B) = (q, 1, L)
No move is possible.
The TM halts and
... B B 0 0 0 B ... accepts.
50
Instantaneous Descriptions of a Turing
Machine
• Initially, a TM has a tape consisting of a string of
input symbols surrounded by an infinity of blanks in
both directions.
• The TM is in the start state, and the head is at the
leftmost input symbol.
51
TM ID’s – (2)
52
TM ID’s – (3)
53
Formal Definition of Moves
54
Languages of a TM
55
Equivalence of Accepting and Halting
56
Proof of 1: Acceptance -> Halting
57
Proof of 2: Halting -> Acceptance
58
Recursively Enumerable Languages
59
Example: Recursive Languages
60
Calculations vs. Languages
61
Language of the Turing Machines
Regular Context-
sensitive
Enumerable
Context
Recursively
(DFA)
free
(PDA)
62
• Extensions to the Basic Turing Machine
TMs with storage
The standard TMs are equivalent to TMs with storage.
E.g., TM for 01* + 10*
64
Multi-track Turing Machines
• A TM with multiple tracks, but just one unified tape head.
control
One tape head to read
k symbols from the k tracks
at one step.
Track 1 … …
… …
Track 2
…
…
Track k … …
66
Multi-Track TMs
• E.g., TM for {wcw | w {0,1}* } but w/o modifying original input string
BEFORE AFTER
control control
… B B 0 1 0 c 0 1 0 B … Track 1 … B B 0 1 0 c 0 1 0 B … Track 1
… B B B B B B B B B B … Track 2 … B B X X X c Y Y Y B … Track 2
67
Multi-tape Turing Machines
control
k separate heads
Tape 1 … …
… …
Tape 2
…
Tape k … …
70
Non-deterministic TMs
• A TM can have non-deterministic moves:
• (q, X) = { (q1, Y1, D1), (q2, Y2, D2), … }
• Non-deterministic TMs are equivalent to
Deterministic TMs
• Simulation using a multitape deterministic TM:
Control
Input tape
ID1 ID2 ID3 ID4
Marker tape * * * *
Scratch tape
74
Main Reference
1. Problems That Computers Cannot Solve
2. The Turing Machine
3. Programming Techniques for Turing Machines
4. Extensions to the Basic Turing Machine
(Introduction to Automata Theory, Languages, and Computation
(2013) Global Edition 3rd Edition)
Additional References
https://people.eecs.berkeley.edu/~sseshia/172/lectures/Lecture12.pdf
This Presentation is mainly dependent on the textbook: Introduction to Automata Theory, Languages, and Computation: Global Edition, 3rd edition (2013) PHI
by John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman
Thank You