ATC Notes Module 4
ATC Notes Module 4
Module 4
Algorithms and Decision Procedures for CFLs: Decidable questions, Un-decidable
questions. Turing Machine: Turing machine model, Representation, Language acceptability
by TM, design of TM, Techniques for TM construction. Variants of Turing Machines (TM),
The model of Linear Bounded automata.
Textbook 1: Ch 14: 14.1, 14.2,
Textbook 2: Ch 9.1 to 9.8
RBT: L1, L2, L3
Use a PDA:
Create PDA M that is guaranteed to halt on all inputs and that accepts all strings in L and rejects all strings that are not
in L.
Proof: The following algorithm, decideCFL, uses decideCFLusingGrammar to answer the question:
decideCFL(L: CFL, w: string) =
1. If decideCFLusingGrammar(L, w) accepts, return True else return False.
Proof: The proof is by a construction that begins by converting G to Greibach normal form. Recall that, in any
grammar in Greibach normal form, all rules are of the form X → a A, where a ∈ Ʃ and A ∈ (V - Ʃ)*.
Algorithm cfgtoPDAtopdown, builds PDA M from any context-free grammar G, that on input w, simulates G
deriving w, starting from S.
M = ({p, q}, Ʃ, V, ∆, p, {q}), where ∆ contains:
1. The start-up transition ((p, ε, ε), (q, S)), which pushes the start symbol onto the stack and goes to
state q.
2. For each rule X → s1s2…sn in R, the transition ((q, ε, X), (q, s1s2…sn)), which replaces X by
s1s2…sn. If n = 0 (i.e., the right-hand side of the rule is ), then the transition ((q, ε, X), (q, ε)).
3. For each character c ∈ Ʃ, the transition ((q, c, c), (q, ε)), which compares an expected character
from the stack against the next input character and continues if they match.
The start-up transition, plus all the transitions generated in step 2, are ε -transitions.
If G is in Greibach normal form. If G contains the rule X → c s2…sn (where c ∈ Ʃ and s2 through sn are elements of
V - Ʃ), it is not necessary to push c onto the stack, only to pop it with a rule from step 3. Instead, we collapse the push
and the pop into a single transition. So we create a transition that can be taken only if the next input character is c. In
that case, the string s2…sn is pushed onto the stack.
Now we need only find a way to get rid of the start-up transition, whose job is to push S onto the stack so that the
derivation process can begin. Since G is in Greibach normal form, any rules with S on the left-hand side must have the
form S → cs2…sn. So instead of reading no input and just pushing S, M will skip pushing S and instead, if the first
input character is c, read it and push the string s2…sn.
Since terminal symbols are no longer pushed onto the stack, we no longer need the transitions created in step 3 of the
original algorithm.
Proof:
a) Since each computation of M must consume one character of w at each step and M will halt when
it runs out of input, each computation must halt within n steps.
b) M may split into at most b branches at each step in a computation. The number of steps in a
computation is less than or equal to n. So the total number of computations must be less than or
equal to bn.
c) Since the maximum number of computations is bn and the maximum length of each is n, the
maximum number of steps that can be executed before all computations of M halt is nbn.
Second way to answer the question, “Given a context-free language L and a string w, is w in L?” is to execute the
following algorithm:
Since we have proven that there exists a grammar that generates L iff there exists a PDA that accepts it, these
questions will have the same answers whether we ask them about grammars or about PDAs.
Proof:
(1) Let G = (V, Ʃ, R, S) be a context-free grammar that generates L. L(G) = ∅ iff S is unproductive (i.e., not able to
generate any terminal strings). The following algorithm exploits the procedure removeunproductive, defined in
Section 11.4, to remove all unproductive nonterminals from G. It answers the question, “Given a context-free
language L, is L = ∅?”.
decideCFLempty(G: context-free grammar) =
1. Let G’ = removeunproductive(G).
2. If S is not present in G’ then return True else return False.
Proof:
14.2 The Undecidable Questions
There exists no decision procedure for many other questions
Given a context free language L, is L = *?
Given a context free language L, is the complement of L context-free?
Given a context free language L, is L regular?
Given a context free language L1 and L2,, is L1 = L2?
Given a context free language L1 and L2, is L1 L2?
Given a context free language L1 and L2, is L1 L2 = ?
Given a context free language L, is L inherently ambiguous?
Given a context free grammar G, is G ambiguous?
TURING MACHINE
The Turing machine provides an ideal theoretical model of a computer. Turing machines are useful
in several ways:
• Turing machines are also used for determining the undecidability of certain languages and
• As an automaton, the Turing machine is the most general model, It accepts type-0
languages.
• It can also be used for computing functions. It turns out to be a mathematical model of
partial recursive functions.
• Measuring the space and time complexity of problems.
Turing assumed that while computing, a person writes symbols on a one-dimensional paper (instead
of a two dimensional paper as is usually done) which can be viewed as a tape divided into cells. In
Turing machine one scans the cells one at a time and usually performs one of the three simple
operations, namely:
(i) Writing a new symbol in the cell being currently scanned,
(ii) Moving to the cell left of the present cell, and
(iii) Moving to the cell right of the present cell.
REPRESENTATION OF TURINGMACHINES
We can describe a Turing machine employing
(i) Instantaneous descriptions using move-relations.
(ii) Transition table, and
(iii) Transition diagram (Transition graph).
REPRESENTATION BY INSTANTANEOUS DESCRIPTIONS
Definition: An ID of a Turing machine M is a string 𝛼𝛽𝛾, where 𝛽 is the present state of M, the
entire input string is split as 𝛼𝛾, the first symbol of 𝛾 is the current symbol a under the R/W head and
𝛾 has all the subsequent symbols of the input string, and the string 𝛼 is the substring of the input
string formed by all the symbols to the left of a.
EXAMPLE: A snapshot of Turing machine is shown in below Fig. Obtain the instantaneous
description.
We give the definition of in the form of a table called the transition table If (q, a)=(𝛾,𝛼,𝛽). We write
𝛼𝛽𝛾 under the 𝛼-column and in the q-row. So if we get 𝛼𝛽𝛾 in the table, it means that 𝛼 is written in
the current cell, 𝛽 gives the movement of the head (L or R) and 𝛾 denotes the new state into which
the Turing machine enters.
EXAMPLE:
Consider, for example, a Turing machine with five states q1,...,q5 where q1 is the initial state and q5 is
the (only) final state. The tape symbols are 0,1and b. The transition table given below describes :
As (q5,1) is not defined, M halts; so the input string 011 is not accepted
The Turing machine M has the initial state q0. The initial ID for M is q00m10n. On seeing 0,the
following moves take place
q1 is the initial state of COPY. The following moves take place for M1:
After exhausting 0s, q1 encounters 1. M1 moves to state q4. All 2's are converted back to 0's
and M1 halts in q5. The TM M picks up the computation by starting from q 5 The q0 and q6 are the
states of M. Additional states are created to check whether reach 0 in 0m gives rise to 0m at the end of
the rightmost 1 in the input string. Once this is over, M erases 10n1 and finds 0mn in the input tape.
M can be defined by M=({q0,q1,....q12}{0,1},{0,,2,b},,q0,b,{q12}) where is defined by table given
below:
9.7 VARIANTS OF TURING MACHINES
A multitape TM has a
Q: finite set of states
q0: an initial state,
F ⊆ Q: the set of final states,
Ʃ ⊆ Γ* and b ∉ Ʃ
δ transition function, Q x Γk in to Q x Γk x {L,R,S}k
There are k tapes, each divided into cells. The first tape holds the input string w. Initially, all the
other tapes hold the blank symbol.
Initially the head of the first tape (input tape) is at the left end of the input w. All the other heads
can be placed at any cell initially.
A move depends on the current state and k tape symbols under k tape heads.
In a typical move:
(i) M enters a new state.
(ii) On each tape. a new symbol is written in the cell under the head.
(iii) Each tape head moves to the left or right or remains stationary.
The heads move independently.
The initial ID has the initial state q0, the input string w in the first tape (input tape),
empty strings of b's in the remaining k - 1 tapes.
An accepting ID has a final state and some strings in each of the k tapes.
Theorem 9.1 Every language accepted by a multitape TM is acceptable by some single-tape TM (that
is, the standard TM).
Initially the contents of tapes 1 and 2 of M are stored in the second and fourth tracks of M1.
The headmarkers of the first and third tracks are at the cells containing the first symbol.
To simulate a move of M, the 2k-track TM M1 has to visit the two headmarkers and store the scanned
symbols in its control.
Keeping track of the headmarkers visited and those to be visited is achieved by keeping a count and
storing it in the finite control of M1
After visiting both head markers, M1 knows the tape symbols being scanned by the two heads of M.
M1 accepts a string w if the new state of M, as recorded in its control at the end of the processing of w.
is a final state of M.
Definition 9.4 The time complexity of TM M is the function T(n), n being the input size, where T(n) is
defined as the maximum of the running time of M over all inputs w of size n.
Theorem 9.2 If M1 is the single-tape TM simulating multitape TM M, then the time taken by M1 to
simulate n moves of M is O(n2).
9.7.2 Nondeterministic Turing Machines
Definition 9.5 A nondeterministic Turing machine is a 7-tuple (Q, Ʃ,Γ, δ, q0, b, F) where
1. Q is a finite nonempty set of states
2. Ʃ is a finite nonempty set of tape symbols
3. b ∈ Γ is called the blank symbol
4. Ʃ is a nonempty subset of Γ called the set of input symbols. We assume that b ∉ Ʃ.
5. q0 is the initial state
6. F ⊆ Q is the set of final states
7. δ is a partial function from Q x Γ into the power set of Q x Γ x {L,R}
Definition 9.6 w ∈ Ʃ * is accepted by a nondeterministic TM M if q0w |-* xqfy for some final state qf.
Theorem 9.3 If M is a nondeterministic TM, there is a deterministic TM M1 such that T(M) = T(M1)
Proof:
We construct M1 as a multitape TM.
Each symbol in the input string leads to a change in ID. M1 should be able to reach all IDs and stop
when an ID containing a final state is reached.
So the first tape is used to store IDs of M as a sequence and also the state of M. These IDs are
separated by the symbol * (included as a tape symbol). The current ID is known by marking an x
along with the ID-separator * (The symbol * marked with x is a new tape symbol.)
All IDs to the left of the current one have been explored already and so can be ignored subsequently.
Note that the current ID is decided by the current input symbol of w.
1. M1 examines the state and the scanned symbol of the current ID. Using the knowledge of
moves of M stored in the finite control of M1, it checks whether the state in the current ID is
an accepting state of M. In this case M1 accepts and stops simulating M.
2. If the state q say in the current ID xqay, is not an accepting state of M1 and δ(q, a) has k
triples, M1 copies the ID xqay in the second tape and makes k copies of this ID at the end of
the sequence of IDs in tape 2.
3. M1 modifies these k IDs in tape 2 according to the k choices given by δ(q, a).
4. M1 returns to the marked current ID. erases the mark x and marks the next ID- separator *
with x. Then M1 goes back to step 1.
A linear bounded automaton is a non deterministic Turing machine which has a single tape whose
length is not infinite but bounded by a linear function of the length of the input string.
Definition
M = (Q, Ʃ, Γ, δ, q0, b, ¢ $, F)
¢ is called the left-end marker which is entered in the leftmost cell of the input tape and prevents the
R/W head from getting off the left end of the tape.
$ is called the right-end marker which is entered in the rightmost cell of the input tape and prevents the
R/W head from getting off the right end of the tape.
The R/W head should not print any other symbol over both the end markers.
The transition of IDs is similar except that k changes to k - 1 if the R/W head moves to the left and to k
+ 1 if the head moves to the right.
ADDITIONAL PROBLEMS
1. Design a Turing machine to obtain complement of a binary number.
IDEA OF CONSTRUCTION:
1) If symbol is 0 change it to 1, move read write head to RIGHT
2) If symbol is 1 change it to 0, move read write head to RIGHT
3) Symbol is b (blank) don’t change, move read write head to RIGHT,
and HALT.The construction is made by defining moves in the following
manner:
(a) ql is the initial state. On scanning 1, no change in state and write 0 and move head to RIGHT.
(c) If M is in state q1and scans blank, it enters q2 and writes b move to right.
(d) q2 is the only accepting state.
Symbolically, M=({ql,q2},{1,0,b},{1,0,b},,ql,b,{q2}) Where is defined by:
The computation sequence of 1010:
2. Design a TM that converts binary number into its 2’s complement representation.
IDEA OF CONSTRUCTION:
Read input from left to right until right end blank is scanned.
Begin scan from right to left keep symbols as it is until 1 found on input file.
If 1 found on input file, move head to left one cell without changing input.
Now until left end blank is scanned, change al 1’s to 0 and 0’s to 1.
We require the following moves:
(a) Let q1 be initial state, until blank is scanned, move head to RIGHT without changing
anything. On scanning blank, move head to RIGHT change state to q 2 without changing the
content of input.
(b) If q2 is the state, until 1 is scanned, move head to LEFT without changing anything. On
reading 1, change state to q3, move head to LEFT without changing input.
(c) If q3 is the state, until blank is scanned, move head to LEFT, if symbol is 0 change to 1,
otherwise if symbol is 1 change to 0.On finding blank change state to q4, move head to LEFT
without Changing input.
(d) q4 is the only accepting state.
We construct a TM M as follows:
M = (Q, Σ,, , q0,b, F)
Q = {q1,q2,q3,q4}
F = {q4 }
Σ = {0,1}
𝚪= {0,1,b}
PRACTICE PROBLEMS
1. Design a Turing machine to replace al a’s with X and al b’s with Y.
2. Design a Turing machine to accept anbm n>m.
3. Design a Turing machine to accept anbn n<m.
4. Design a Turing machine to accept (0+1)*00(0+1)* .
5. Design a Turing machine to increment a given input.
6. Design a Turing machine to decrement a given input.
7. Design a Turing machine to subtract two unary numbers.
8. Design a Turing machine to multiply two unary numbers.
9. Design a Turing machine to accept a string 0’s followed by a 1.
10. Design a Turing machine to verify if the given binary number is an even number or not.
11. Design a Turing machine to shift the given input by one cell to left.
12. Design a Turing machine to shift the given input to the right by one cell .
13. Design a Turing machine to rotate a given input by one cell.
14. Design a Turing machine to erase the tape.
15. Design a Turing machine to accept anbncn .
16. Design a Turing machine to accept any string of a’s & b’s with equal number of a’s & b’s.
17. Design a Turing machine to accept anb2n.
18. Design a Turing machine to accept anbkcm: where n=m+k.
19. Design a Turing machine to accept anbkcm: where m=n+k.
20. Design a Turing machine to accept anbkcm: where k=m+n.