Theory of Computation
Theory of Computation
UNIT-I
AUTOMATA
Introduction
Why do we study Theory of Computation ?
What is Computation ?
Sequence of mathematical operations ?
Which operations ?
Basis of
Algorithm analysis
Complexity theory
Analysis of algorithms
Complexity Theory
Cryptography
Compilers
Circuit design
1936 Alan Turing invented the Turing machine, and proved that there exists an
unsolvable problem.
1940s Stored-program computers were built.
1943 McCulloch and Pitts invented finite automata.
1956 Kleene invented regular expressions and proved the equivalence of regular
expression and finite automata.
1956 Chomsky defined Chomsky hierarchy, which organized languages
recognized by different automata into hierarchical classes.
1959 Rabin and Scott introduced nondeterministic finite automata and proved its
equivalence to (deterministic) finite automata.
1950s-1960s More works on languages, grammars, and compilers
1965 Hartmantis and Stearns defined time complexity, and Lewis, Hartmantis and
Stearns defined space complexity.
1971 Cook showed the first NP-complete problem, the satisfiability prooblem.
1972 Karp Showed many other NP-complete problems.
Let = {a, b, , z}
length(automata) = 8
length(computation) = 11
length() = 0
x(i), denotes the symbol in the ith position of a string x, for 1 i length(x).
String Operations
Concatenation
Substring
Reversal
Example
automatacomputation = automatacomputation
The concatenation of string x for n times, where n0, is denoted by xn
x0 =
x1 = x
x2 = x x
x3 = x x x
Substring
Let x and y be strings over an alphabet
The string x is a substring of y if there exist strings w and z over such that y = w
x z.
is a substring of every string.
For every string x, x is a substring of x itself.
Example
, comput and computation are substrings of computation.
Reversal
Let x be a string over an alphabet
The reversal of the string x, denoted by x r, is a string such that
if x is , then xr is .
If a is in , y is in * and x = a y, then xr = yr a.
(automata)r
= (utomata)r a
= (tomata)r ua
= (omata)r tua
= (mata)r otua
= (ata)r motua
= (ta)r amotua
= (a)r tamotua
= ()r atamotua
= i=0.. i - 0
= i=0.. i - {}
Let = {0, 1}. + = {0, 1, 00, 01, 10, 11, 000, 001, 010, 011, }.
* and + are infinite sets.
Operations on LanguagesComplementation
Union
Intersection
Concatenation
Reversal
Closure
Complementation
Let L be a language over an alphabet .
The complementation of L, denoted byL, is *L.
Example:
Let = {0, 1} be the alphabet.
Le = {* | the number of 1s in is even}.
Le= {* | the number of 1s in is not even}.
Le= {* | the number of 1s in is odd}.
Union
Let L1 and L2 be languages over an alphabet .
The union of L1 and L2, denoted by L1L2, is {x | x is in L1 or L2}.
Example:
{x{0,1}*|x begins with 0} {x{0,1}*|x ends with 0}
Concatenation
Let L1 and L2 be languages over an alphabet .
The concatenation of L1 and L2, denoted by L1L2, is {w1w2| w1 is in L1 and w2
is in L2}.
Example
{ x {0,1}*| x begins with 0}{x {0,1}*| x ends with 0}
= { x {0,1}*| x begins and ends with 0 and length(x) 2}
{ x {0,1}*| x ends with 0}{x {0,1}*| x begins with 0}
= { x {0,1}*| x has 00 as a substring}
Reversal
Let L be a language over an alphabet .
The reversal of L, denoted by Lr, is {wr| w is in L}.
Example
{x {0,1}*| x begins with 0} r
= {x {0,1}*| x ends with 0}
{x {0,1}*| x has 00 as a substring} r
= {x {0,1}*| x has 00 as a substring}
Closure
Let L be a language over an alphabet .
The closure of L, denoted by L+, is { x |for an integer n 1, x = x1x2xn and x1,
x2 , , xn are in L}
That is, L+ = i= 1 Li
Example:
Let = {0, 1} be the alphabet.
Le = {* | the number of 1s in is even}
Le+ = {* | the number of 1s in is even} = Le*
Observation about Closure
L+ = L* {} ?
Example:
L = {* | the number of 1s in is even}
L+ = {* | the number of 1s in is even} = Le*
Why?
L* = L+ {} ?
Decision problem
Problem with a YES/NO answer
Example: Given a positive integer n, is n a prime number > 20?
Language
Example: {n | n is a prime number > 20}
Finite Automata
At the beginning,
an FA is in the start state (initial state)
its tape head points at the first cell
For each move, FA
reads the symbol under its tape head
changes its state (according to the transition function) to the next state
determined by the symbol read from the tape and its current state
move its tape head to the right one cell
When does an FA stop working?
When it reads all symbols on the tape
Then, it gives an answer if the input is in the specific language:
Answer YES if its last state is a final state
Answer NO if its last state is not a final state
f
1
(q,a)
How an FA works
Definition
f
1
Using 2 states
Current
number
Current
remainder
3n
6n
3n
6n+1
3n+1
6n+2
3n+1
6n+3
3n+2
6n+4
3n+2
6n+5
Definition
Definition
Definition
Let M = (Q, , , s, F) be an NFA, and *. We say M accepts if (s, )
*M (f, ), when f F. Otherwise, we say M rejects .
For any DFA Md, there exists an NFA Mn such that Md and Mn are equivalent
Proof: Let Md be any DFA. We want to construct an NFA Mn such that L(Mn) = L(Md).
From the definitions of DFA and NFA, if M is a DFA then it is also an NFA.
Then, we let Mn = Md.
Thus, L(Md) = L(Mn).
For any NFA Mn, there exists a DFA Md such that Md and Mn are equivalent.
Proof: Let Mn = (Q, , , s, F) be any NFA. We want to construct a DFA Md such that
L(Md) = L(Mn).
First define the closure of q, denoted by E(q).
Second, construct a DFA Md=(2Q, , ', E(s), F')
Finally, prove
f F (s, ) |-*Mn (f, ) f 'F ' (E(s), ) |*Md (f ' , ).
Closure of state q
Let M = (Q, , , s, F) be an NFA, and qQ.
The closure of q, denoted by E(q), is
the set of states which can be reached from q without reading any symbol.
{pQ| (q, ) |-M* (p, )}
If an NFA is in a state q, it can also be in any state in the closure of q without
reading any input symbol.
Example of closure
Constructing the equivalent DFA
Let Mn = (Q, , , s, F) be any NFA. We construct a DFA Md =(2Q, , ', E(s), F'),
where :
'(q',a) = {rE(p)| p (q,a) } and
F' = {f Q | f F })
E(q0)
E(q1)
E(q2)
E(q3)
E(q4)
q1, q2, q3
q2
q3
q3,q4
Proof
Part I:
For any string in *, and states q and r in Q, there exists R Q such that
(q, ) *Mn (r, ) (E(q), ) *Md (R, ) and rR.
Basis:
Let be a string in *, q and r be states in Q, and (q, ) *Mn (r, ) in 0 step.
Because (q, ) *Mn (r, ) in 0 step, we know (1) q=r , and (2) = .
Then, (E(q), ) = (E(r), ).
Thus, (E(q), ) *Md (E(r), ) .
That is, there exists R=E(r) such that r R and (E(q), ) *Md (R, ).
Induction hypothesis:
For any non-negative integer k, string in *, and states q and r in Q, there exists R
Q:
(q, ) *Mn (r, ) in k steps -> (E(q), ) *Md (R, ) and rR.
Induction step:
Prove, for any non-negative integer k, string in *, and states q and r in Q, there
exists R Q:
(q, ) *Mn (r, ) in k+1 steps -> (E(q), ) *Md (R, ) and rR.
Let be a string in *, q and r be states in Q, and (q, ) *Mn (r, ) in k+1 steps.
Because (q, ) *Mn (r, ) in k+1 steps and k0, there exists a state p in Q and a string
* such that (q, ) *Mn (p, a) in k steps and (p, a) Mn (r, ) for some a {}.
From the induction hypothesis and (q, ) *Mn (p, a) in k steps, we know that there
exists PQ such that (E(q), ) *Md (P, a) and pP.
Since (p, a) Mn (r, ), r(p, a).
From the definition of of Md, E((p, a)) (P, a) because pP.
Because r(p, a) and E((p, a)) (P, a), r(P, a).
Then, for R=(P, a), (P, a) *Md (R, ) and rR.
Thus, (E(q), ) *Md (P, a) *Md (R, ) and rR.
Part II:
For any string in *, and states q and r in Q, there exists R Q such that rR and
(E(q), ) *Md (R, ) -> (q, ) *Mn (r, ).
Proof
Basis:
Let be a string in *, q and r be states in Q, R be a subset of Q such that r R and
(E(q), ) *Md (R, ) in 0 step.
Because (E(q), ) *Md (R, ) in 0 step, E(q)=R and =.
From the definition of E, (q, )=R because E(q)=R.
Then, for any rR, (q, ) *Mn (r, ).
That is, there exists R=E(q) such that r R and (q, ) *Mn (r, ).
Induction hypothesis:
For any non-negative integer k, string in *, and states q and r in Q, there exists R
Q such that rR and:
(E(q), ) *Md(R, ) in k steps ->(q, ) *Mn(r, ).
Induction step:
Prove, for any non-negative integer k, string in *, and states q and r in Q, there
exists R Q such that rR:
(E(q), )*Md(R, ) in k+1 steps ->(q, ) *Mn(r, ).
Let be a string in *, q and r be states in Q, and (E(q), ) *Md (R, ) in k+1 steps.
Because (E(q), ) *Md (R, ) in k+1 steps and k0, there exists P2Q (i.e. PQ) and
a string * such that =a, (E(q), ) *Md (P,) in k steps and (P, a) Md (R, ) for
some a.
From the induction hypothesis and (E(q), ) *Md (P, ) in k steps, we know that there
exists pP such that (q, )*Mn(p,) (i.e. (q, a) *Mn (p, a) ).
Since (P, a) Md (R, ), there exists rR such that r= (p, a).
Then, for some rR, (p, a) *Mn (r, ).
Thus, (q, ) *Mn (p, a) *Mn (r, ) for some rR.
Closure Properties
Basic
Check list
Advanced
Prove DFA/NFA accepting a language
Prove properties of DFA/NFA
Configuration change
Under some modification
etc.
Prove some properties of languages accepted by DFA/NFA
Under some modification
Surprise!
Regular expressions
Regular languages
Equivalence between languages accepted by FA and regular languages
Closure Properties
Regular Expressions
expression a.
If L1 and L2 are regular languages corresponding to the regular expression r1
and r2, then
L1L2, L1L2, and L1* are regular languages corresponding to (r1 + r2)
, (r1 r2), and (r1*).
Simple examples
Let = {0,1}.
{*| does not contain 1s}
(0*)
{*| contains 1s only}
(1(1*)) (which can can be denoted by (1+))
*
((0+1)*)
{*| contains only 0s or only 1s}
((00*)+(11*))
Notation
* =
* =
(r + )+ = r*
r* = r*(r + ) = r* r* = (r*)*
(r*s*)* = (r + s)*
To show that the languages accepted by FA and regular languages are equivalent, we
need to prove:
For any regular language L, there exists an FA M such that L = L(M).
For any FA M, L(M) is a regular language.
Proof:
Proof (contd)
Induction hypotheses: Let r1 and r2 be regular expressions with
less than n operations. And, there are NFAs M1 and M2 accepting
regular languages corresponding to L(r1) and L(r2).
Induction step: Let r be a regular expression with n operations.
We construct an NFA accepting L(r).
s
1
+ *
0 (10 )
(1+)
ql
ql
qj
'
Proof (contd)
We prove that L(M) is a regular language by showing that there is a regular
expression corresponding to L(M), by induction.
Basis: R(i, j, 0) corresponds to a regular expression a if i j and a + if i= j
for some a.
Induction hypotheses: Let R(i, j,k-1) correspond to a regular expression, for
any i, j, k n.
Induction step: R(i, j, k) = R(i, j, k-1) R(i, k, k-1) R(k, k, k-1)* R(k, j, k-1)
also corresponds to a regular expression because R(i, j, k-1), R(i, k, k-1), R(k,
k, k-1) and R(k, j, k-1) correspond to some regular expressions and union,
concatenation, and Kleenes star are allowed in regular expressions.
Therefore, L(M) is also a regular language because L(M) = + R(1, f, n) for all
qf in F.
Pumping Lemma
Let L be a regular language.
Then, there exists an integer n0 such that for every string x in L that |x|n, there are
strings u, v, and w such that
x = u v w,
v ,
|u v| n, and
for all k 0, u vk w is also in L
Any language L is not a regular language if for any integer n0 , there is a string x in L
such that |x|n, for any strings u, v, and w,
x u v w, or
v = , or
Not (|u v| n), or
there is k 0, u vk w is not in L
Given a language L.
Let n be any integer 0 .
Choose a string x in L that |x|n.
Consider all possible ways to chop x into u, v and w such that v , and |uv| n.
For all possible u, v, and w, show that there is k 0 such that u vk w is not in L.
Then, we can conclude that L is not regular.
k1.
k1.
Then, L is not regular.
Check list
Find the language described by a regular exp.
Construct regular exp. describing a given language
Convert a regular exp. into an FA
Convert an FA into a regular exp.
Prove a language is regular
By constructing a regular exp.
By constructing an FA
By using closure properties
Construct an FA or a regular exp. for the intersection,
union, concatenation, complementation, and Kleenes star
of regular languages
Prove other closure properties of the class of regular lang
UNIT-III
CONTEXT FREE GRAMMAR AND LANGUAGES
Pushdown automata
table.
Put together: Given an input signal, current state, and stack symbol, the automaton can
follow a transition to another state, and optionally manipulate (push or pop) the stack.
In general pushdown automata may have several computations on a given input string,
some of which may be halting in accepting configurations while others are not. Thus we
have a model which is technically known as a "nondeterministic pushdown automaton"
(NPDA). Nondeterminism means that there may be more than just one transition
available to follow, given an input signal, state, and stack symbol. If in every situation
only one transition is available as continuation of the computation, then the result is a
deterministic pushdown automaton (DPDA), a strictly weaker device.
If we allow a finite automaton access to two stacks instead of just one, we obtain a more
powerful device, equivalent in power to a Turing machine. A linear bounded automaton
is a device which is more powerful than a pushdown automaton but less so than a Turing
machine.
Pushdown automata are equivalent to context-free grammars: for every context-free
grammar, there exists a pushdown automaton such that the language generated by the
grammar is identical with the language generated by the automaton, which is easy to
prove. The reverse is true, though harder to prove: for every pushdown automaton there
exists a context-free grammar such that the language generated by the automaton is
identical with the language generated by the grammar.
Formal Definition
A PDA is formally defined as a 7-tuple:
where
An element is a transition of M. It has the intended meaning that M, in state , with on the
input and with as topmost stack symbol, may read a, change the state to q, pop A,
replacing it by pushing . The letter (epsilon) denotes the empty string and the
component of the transition relation is used to formalize that the PDA can either read a
letter from the input, or proceed leaving the input untouched.
In many texts the transition relation is replaced by an (equivalent) formalization, where
Here (p,a,A) contains all possible actions in state p with A on the stack, while reading
a on the input. One writes for the function precisely when for the relation. Note that finite
in this definition is essential.
Computations
can be chosen in a computation. With the above definition in each step always a single
symbol (top of the stack) is popped, replacing it with as many symbols as necessary. As a
consequence no step is defined when the stack is empty.
Computations of the pushdown automaton are sequences of steps. The computation starts
in the initial state q0 with the initial stack symbol Z on the stack, and a string w on the
input tape, thus with initial description (q0,w,Z). There are two modes of accepting. The
pushdown automaton either accepts by final state, which means after reading its input the
automaton reaches an accepting state (in F), or it accepts by empty stack (), which means
after reading its input the automaton empties its stack. The first acceptance mode uses the
internal memory (state), the second the external memory (stack).
Formally one defines
1. with and (final state)
2. with (empty stack)
Here represents the reflexive and transitive closure of the step relation meaning any
number of consecutive steps (zero, one or more).
For each single pushdown automaton these two languages need to have no relation: they
may be equal but usually this is not the case. A specification of the automaton should also
include the intended mode of acceptance. Taken over all pushdown automata both
acceptance conditions define the same family of languages.
Theorem. For each pushdown automaton M one may construct a pushdown automaton
M' such that L(M) = N(M'), and vice versa, for each pushdown automaton M one may
construct a pushdown automaton M' such that N(M) = L(M')
The following is the formal description of the PDA which recognizes the language by
final state:
Q = {p,q,r}
= {0,1}
= {A,Z}
F = {r}
consists of the following six instructions:
(p,0,Z,p,AZ), (p,0,A,p,AA), (p,,Z,q,Z), (p,,A,q,A), (q,1,A,q,), and
(q,,Z,r,Z).
In words, in state p for each symbol 0 read, one A is pushed onto the stack. Pushing
symbol A on top of another A is formalized as replacing top A by AA. In state q for each
symbol 1 read one A is popped. At any moment the automaton may move from state p to
state q, while it may move from state q to accepting state r only when the stack consists
of a single Z.
There seems to be no generally used representation for PDA. Here we have depicted the
instruction (p,a,A,q,) by an edge from state p to state q labelled by a;A / (read a;
replace A by ).
Pushdown Automata
As Fig. 5.1 indicates, a pushdown automaton consists of three components: 1) an input
tape, 2) a control unit and 3) a stack structure. The input tape consists of a linear
configuration of cells each of which contains a character from an alphabet. This tape can
be moved one cell at a time to the left. The stack is also a sequential structure that has a
first element and grows in either direction from the other end. Contrary to the tape head
associated with the input tape, the head positioned over the current stack element can
read and write special stack characters from that position. The current stack element is
always the top element of the stack, hence the name ``stack''. The control unit contains
both tape heads and finds itself at any moment in a particular state.
A (non-deterministic) finite state pushdown automaton (abbreviated PDA or, when the
context is clear, an automaton) is a 7-tuple = (X, Z, , R, zA, SA, ZF), where
X = {x1, ... , xm} is a finite set of input symbols. As above, it is also called an
alphabet. The empty symbol is not a member of this set. It does, however, carry
its usual meaning when encountered in the input.
Z = {z1, ... zn} is a finite set of states.
= {s1, ... , sp} is a finite set of stack symbols. In this case
ZF
Context-Free Languages
As will be recalled from the last chapter there were two basic ways to determine whether
a given string belongs to the language generated by some finite state automaton: One
could verify that the string brings the automaton to a final state or one could derive, or,
better, produce, the string in the regular grammar corresponding to the automaton. The
same option holds for PDAs.
Definition
X - T and
X*.
X* write u
X* and A
...
is {x
T*| S
x}.
Thus, the definition just articulates the reduction of A to in any context in which A
occurs. It is trivial that every regular language is context-free. The obverse, as will be
seen presently, is not true. Before proving the central theorem for this section two typical
examples are given.
Example 1
Consider
0}. From
E+T | E-T | T
T*F | T/F | F
(E) | id
id
a | b | c
Then the string (a + b)*c belongs to L( ). Indeed, it is easy to write down a derivation
of this string:
E
T*F
F*F
(E)*F
(E + T)*F
(T + T)*F
(F + T)*F
(id + T)*F
(a + T)*F
(a + F)*F
(a + id )*F
(a + b)*F
(a + b)*id
(a + b)*c
The derivation just adduced is leftmost in the sense that the leftmost nonterminal was
always substituted. Although derivations are in general by no means unique, the leftmost
one is. The entire derivation can also be nicely represented in a tree form, as Fig. 5.4
suggests.
For simplicity, assume that pushes at most two symbols and that it has a single
acceptance statei zF. A moment's reflection shows that these assumptions are not
restrictive; but they do eliminate some extra preprocessing. The nonterminals of G are
triples (z, A, z ) of states z, z and a stack symbol A. The basic idea is to imitate what
the machine undergoes in state z finally to pop symbol A and to wind up thereby in state
z , having processed some string of input characters. Thus the rules for the sought-after
context-free grammar are posited as follows:
1. For the (extra) start symbol put S
(z, B, z1)
a(z , C, z1)
Z
(z, B, z1)
4. For each z
Z put (z, , z)
It is important to notice the free choice of z1 and z1, z2 in rules 2. and 3. Consider, for
example, processing the string a2bc2 from the PDA from Section 5.1. Then posit the start
rule
S
(z1, SA, z3),
since there is only one final state. Now mechanically translate each of the transitions
from this PDA into their grammatical equivalents as shown in Table 5.1.
Table 5.1: Translation of the PDA Transition Rules into Grammatical Productions
Nr. Transition Function
Nr Production
2'
(zA, S, w')
3'
(zA, S, v')
b(z2, , v')
4'
(z2, S, u')
c(z2, , u')
5'
c(z3, , t')
It is important to note that states z', z'', w', w'', v', u', t' can be chosen at will.
Hopefully, a proper choice will lead to success in accordance with the philosophy of
nondeterminism.
Tree representations of derivations, also known as syntax trees, were briefly introduced in
the preceding section to promote intuition of derivations. Since these are such important
tools for the investigation of context-free languages, they will be dealt with a little more
systematically here.
Definition
Let = (X, T, R, S) be a context-free grammar. A syntax tree for this grammar consists
of one of the following
1. A single node x for an x
2. An edge
corresponding to a production A
R.
3. A tree
where the A1, A2, ... , An are the root nodes of syntax trees. Their yields are read
from left to right.
Ambiguity
Until now the syntax trees were uniquely determined-even if the sequence of direct
derivations were not. Separating the productions corresponding to the operator hierarchy,
from weakest to strongest, in the expression grammar + , - ,*,/,() preserves this natural
hierarchy. If this is not done, then syntax trees with a false evualation sequence are often
the result. Suppose, for instance, that the rules of the expression grammar were written E
E + E | E*E | id, then two different syntax trees are the result. If the first
production E
E + E were chosen then the result would be the tree
Thus this grammar is ambiguous, because it is possible to generate two different syntax
trees for the expression a + b*c.
Chomsky Normal Form
Work with a given context-free grammar is greatly facilitated by putting it into a socalled normal form. This provides some kind of regularity in the appearance of the righthand sides of grammar rules. One of the most important normal forms is the Chomsky
normal form.
Definition
for a
rule
T and B, C
(5.1)
a | BC,
L(
is permitted. If
1. vy
L(
(that is, v
or y
).
n.
L(
).
Proof
Assume that is in Chomsky normal form. For x L( ) consider the (binary) syntax
tree for the derivation of x. Assume the height of this tree is h as illustrated in Fig. 5.6.
L(
2h-2 + 2h-2 = 2h-1, i.e. the yield of the tree with height h is at
L(
) be a string
with | x| n. Thus the syntax tree for x has height at least k + 1, thus on the path from
the root downwards that defines the height of the tree there are at least k + 2 nodes, i.e. at
least k + 1 nonterminal symbols. It then follows that there is some nonterminal symbol A
that appears at least twice. Consulting Fig. 5.7, it is seen that the partial derivation S
uAz
uvAyz obtains.
uvAyz
Example 1
Proof
Assume L were context-free. Then let n be the n from the preceding theorem and put x =
anbncn. Ogden's lemma then provides the decomposition x = uvwyz with the stated
properties. There are several cases to consider.
Case 1 The string vy contains only a's. But then the string uwz L, which is
impossible, because it contains fewer a's than b's and c's. .
Case 2,3 vy contains only b's or c's. This case is similar to case 1.
Case 4,5 vy contains only a's and b's or only b's or c's. Then it follows that uwz contains
more c's than a's and b's or more a's than b's and c's. This is again a contradiction.
Since | vwy|
and
An algorithm is called polynomial in case there is an integer k 2 such that the number
of steps after which the algorithm halts is (nk). The argument n depends only on the
input.
Theorem 5..7 There is a polynomial algorithm that constructs to any given push down
automaton
with L(
a context-free grammar
) = L(
). Conversely, there is a
a push down
with L( ) = L( ).
Theorem 5..8 There is a polynomial algorithm that decides, given any context-free
automaton
T* whether x
L(
).
Proof
The proof of this theorem sometimes goes under the name CYK algorithm after their
discoverers Cocke, Younger and Kasami. It proceeds as follows:
1. Rewrite in Chomsky normal form. It is easily seen that this can be done in
polynomial time.
2. If x = x1x2 ... xn, then for 0
i, j
X - T for which A
X-T | A
Vij
xij}.
X-T | A
A
xi}.
for i : = 1 to n do
Vi1 : = {A
X-T | A
xi
R};
for j : = 2 to n do
for i : = 1 to n - j + 1 do begin
Vij : =
for k : = 1 to j - 1 do
Vij : = Vij
{A
X-T | A
BC, B
Vik, C
Vi+k, j-k};
end
L(
This technique of producing increasingly larger solutions from smaller ones is called
dynamic programming.
Example
AB | BC
BA | a
CC | b
AB | a
and the string x = baaba with n = 5. Then proceeding as above, the following triangular
matrix results:
b
a
a
b a
B
A, C A, C B A, C
S, A B
S, C S, A
B
S, A, C
S, A, C
Since S V15 it follows that x L( ). It is quite remarkable that the algorithm time is
(n3). It is also remarkable that the CYK algorithm actually shows how to construct the
derivation, which has great practical importance.
Then it is easy to derive the string abc:
aBC
abC
abc
aSBC
a2BCBC
a2B2C2
a2bBC2
a2b2C2
a2b2cC
a2b2c2
a2b2C2
a2b2cC
a2b2c2
T + b*c
F + b*c
id + b*c
a + b*c.
At each stage of the derivation the sentential form of the stage is of the form uv, where u
X* and v T*. Tracing this derivation backwards, now proceed as follows: Starting
from the leftmost input symbol reduce that symbol to a rule for which it is the right-hand
side, in this case id
a. Then reduce id to F, etc. until an E has been produced. All of
the previous symbols are handles or right-hand sides of rules that allow successful (in the
sense that the start symbol will eventually be produced). After E has been obtained, the
next input symbol `+' is kept, or better, appended to E. Thus the sentential form `E +' is
produced. This sentential form is called a viable prefix because there is a rule of the form
E
E + T (a trivial one). If it recognized that E + is a viable prefix, then, starting with
the next input symbol, continue this process from that point onwards until the rest of the
right-hand side has been produced, i.e. a handle has been found. Then reduce this handle
to the left-hand side of the ``correct'' rule until the start symbol alone has been produced.
This process can be nicely realized using a push-down automaton. Thus, proceeding from
left to right on the input string, shift or push one or more input symbols onto the stack
until a handle is found. The reduce or pop that handle from the stack and push the lefthand side of the associated rule onto the stack. On a successful parse, if no reduction is
presently forthcoming then the contents of the stack constitute a viable prefix for some
rule yet to be determined. Another way of saying the same thing is that the contents of the
stack, read from bottom up, are the prefix of a sentential form produced on the way back
to the start symbol during a rightmost derivation.
A correct parse of the string a + b*c as a sequence of shift/reduce actions is given in
Table 5.3. Notice the decision to handle multiplication before addition is governed by
``looking ahead'' one symbol.
Table 5.3: Predictive Parse of the expression a + b*c
Stack
Input
Action
a + b*c$ Shift
id$
+ b*c$ Reduce
F$
+ b*c$ Reduce
T$
+ b*c$ Reduce
E$
+ b*c$ Reduce
b*c$ Shift
b + E$
*c$ Shift
id + E$
*c$ Reduce
F + E$
*c$ Reduce
T + E$
*c$ Reduce
*T + E$
c$ Shift
c*T + E$
$ Reduce
id*T + E$
$ Reduce
F*T + E$
$ Reduce
T + E$
$ Reduce
E$
$ Accept
Stack
Input
a + b*c$ Shift
Action
id$
+ b*c$ Reduce
F$
+ b*c$ Reduce
T$
+ b*c$ Reduce
E$
+ b*c$ Reduce
+ E$
b*c$ Shift
b + E$
*c$ Shift
id + E$
*c$ Reduce
F + E$
*c$ Reduce
T + E$
*c$ Reduce
*T + E$
c$ Shift
c*T + E$
$ Reduce
id*T + E$
$ Reduce
F*T + E$
$ Reduce
T + E$
$ Reduce
E$
$ Accept
UNIT-IV
PROPERTIES OF CONTEXT FREE LANGUAGES
At the beginning,
A TM is in the start state (initial state)
its tape head points at the first cell
The tape contains , following by input string, and the rest of the tape
contains .
For each move, a TM
reads the symbol under its tape head
According to the transition function on the symbol read from the tape and
its current state, the TM:
write a symbol on the tape
move its tape head to the left or right one cell or not
changes its state to the next state
A TM stops working,
when it gets into the special state called halt state. (halts)
The output of the TM is on the tape.
when the tape head is on the leftmost cell and is moved to the left. (hangs)
when there is no next state. (hangs)
Example of a DTM
Definition
Let T = (Q, , , , s) be a DTM, and (q1, 1a11) and (q2, 2a22) be two
configurations of T.
We say
(q1, 1a11) yields (q2, 2a22) in one step,
denoted by (q1, 1a11) T (q2, 2a22), if
(q1, a1) = (q2,a2,s), 1=2 and 1=2,
(q1, a1) = (q2,b,R), 2=1b and 1=a22,
Definition
Let T=(Q, , , , s) be a DTM, and (q1, 1a11) and (q2, 2a22) be two
configurations of T.
We say
(q1, 1a11) yields (q2, 1a22) in zero step or
more, denoted by (q1, 1a11) -*T (q2, 1a22), if
q1=q2, 1 =2, a1= a2, and 1= 2, or
(q1,1a11)-T (q, a) and (q, a)-*T (q2,1a22) for some q in Q,
and in *, and a in .
s,0001000)
(p1,@0001000)
(p2,@001000)
(p2,@001000)
(p3,@001000)
(p4,@00100)
(p4,@00100)
(p1,@00100)
(p2,@0100) (p4,@010)
(p4,@010)
(p1,@010)
(p2,@10)
(p2,@10)
(p2,@10)
(p3,@10)
B.VIJAYAKUMAR B.E. M.Tech (PhD)
(p4,@1)
(p4,@1)
(p1,@1)
(q1,@)
(q1,@)
(q2,)
(h ,1)
(p2,@0100)
(p3,@0100)
TM accepting a language
Definition
Let T=(Q, , , , s) be a TM, and w*.
T accepts w if (s, , , w) |-T* (h, , , 1).
The language accepted by a TM T, denoted by L(T), is the
set of strings accepted by T.
L(T)={0n10n | n0}
T halts on 0n10n
T hangs on 0n+110n at p3
T hangs on 0n10n+1 at q1
T hangs on 0n 12 0n at q1
TM computing a function
Definition
Let T=(Q, , , , s) be a TM, and f be a function from * to *.T computes f if, for any
string w in *,
q2
/0,L
@/0,R
/,R
r2
Jaruloj Chongstitvatana
q1
0/0,R
p1
h
0/0,R
1/1,R
0/@,R
@/1,R
0/0,L
1/1,L
0/0,L
1/1,L
0/0,R
1/1,R
1/1,R
p2
1/@,R
/1,L
2301379
r1
/,S
/,L
p3
1/1,L
0/0,L
0/0,R
1/1,R
Turing Machines
19
Nondeterministic TM
An NTM starts working and stops working in the same way as a DTM.
Each move of an NTM can be nondeterministic.
Each Move in an NTM
Configuration of an NTM
Definition
Let T = (Q, , , , s) be an TM.
A configuration of
Can be written as
T is an element of Q
(q,l,a,r) or
(q,lar) Definition
Let T = (Q, , , , s) be an NTM, and (q1, 1a11) and (q2, 2a22) be
two configurations of T.
We say
(q1, 1a11) yields (q2, 2a22) in one step,
denoted by (q1, 1a11) T (q2, 2a22), if
(q2,a2,S) (q1, a1), 1=2 and 1=2,
(q2,b,R) (q1, a1), 2=1b and 1=a22,
(q2,b,L) (q1, a1), 1=2a2 and 2=b1.
, w) |-T*
(h, , , f(w)).
Example of NTM
Let L={ww| w{0,1}*}
0/0,L
1/1,L
/,L
s
/@,R
0/0,L
1/1,L
/@,L
,
0/
0/0,R
1/1,R
q0
1/
,
2301379
0/
,
,L
1/
q1
0/0,R
R 1/1,R
@/,L
t0
h
,R
@/
,R
r1
Turing Machines
0/@
,
,R
/
/,R
0/0,L
1/1,L
/,L
Jaruloj Chongstitvatana
r0
,R
@/
1
,R
1/@
0/0,R
1/1,R
28
Multitape TM
TM with more than one tape.
Each tape has its own tape head.
Each tape is independent.
CONTROL
UNIT
TAPE
TAPE
Jaruloj Chongstitvatana
2301379
Turing Machines
29
,/(,),(R,S)
p1
p2
0,/(0,0),(L,R)
1,/(1,1),(L,R)
,/(1,),(L,L)
,/(,),(R,S)
p3
0,/(0,),(R,S)
1,/(1,),(R,S)
q2
,/(,),(R,R)
,/(,),(L,L)
0,/(0,),(R,S)
1,/(1,),(R,S)
,/(,),(L,R)
p4
0,0/(,),(L, L)
1,1/(,),(L, L)
Jaruloj Chongstitvatana
2301379
Turing Machines
31
Theorem:
For any 2-tape TM T, there exists a single-tape TM M
such that for any string in *:
if T halts on with on its tape, then M halts on with on its tape, and
if T does not halt on , then M does not halt
Marking the position of each tape head in the content of the tape
Encode content of 2 tapes on 1 tape
When to convert 1-tape symbol into 2-tape symbol
0 1 1 1 0
0 1 0 1
2301379
34
Tape format
c(b,)
Jaruloj Chongstitvatana
2301379
Turing Machines
36
a1,a2/(b1,b2),(d1,d2 )
c(a1,a2)
Jaruloj Chongstitvatana
2301379
Turing Machines
37
Update
the first
cell symbol
T_tape1(0,1,d)
Convert
1-tape
,L
,x)
(
)/ c ),L
,x
c( (,
/c
c(?,x)/c(?,x),R
c(?,x)/c(?,x),R
It is
Remember
not possible
symbol
that
c(1,?)
under
is found
tape head
because
in
c(0,?) is wriiten
tape 1in cell 1.
Jaruloj Chongstitvatana
c(?,x)/c(1,x),R
2301379
#/#,L
,R
c(1,x)/c(1,x),L
#/#,L
1/c(1,),L
)/
c(
,
x)
c(0,?)/c(1,?),d
c(0,?)/c(1,?),d
c(
? ,x
),R
,x
(0
/c
#/#,R
c(?,?)/c(?,?),R
#/#,L
x)
?,
c(
? and x are 0, 1, or
c(
0/ 0,x
c( ) /c
0, (0
) ,x
,L
),L
38
Set WorkingTape
FindNewConfiguration
FindStateinCurrentConfiguration
EraseCurrentConfiguration
a,q
a,h
WriteAllPossibleNextConfiguration
2301379
Depend on Mn
Turing Machines
45
How Md works
WriteInitialConfiguration
Tape 1
FindNewConfiguration
FindStateinCurrentC onfiguration
a, q
a, h
EraseCurrentC onfiguration
@
0 1 1 0
Current
Current state:
state: ssq
Tape 2
WriteAllPossibleNex tC onfiguration
* s 0 1 1 0
- # *
0 q 1 1 0 #
*
# @ s 0 1 1 0 # 0 1 q 1 0 #
*
/@,S
Jaruloj Chongstitvatana
/,R
2301379
0/0,R
Turing Machines
46
Then, there is a positive integer n such that the initial configuration (s, ) of Mn
yeilds a halting configuration (h, ) in n steps.
From the construction of Md, the configuration (h, ) must appear on tape 2 at
some time.
Then, Md must halt with on tape 1.
Encoding function
Example of Encoded TM
e()=0 ,
e(a1)=00 , e(a2)=000
e(h)=0,
e(q1)=00,
e(q2)=000
e(S)=0,
e(L)=00,
e(R)=000
e(a1a1a2) = 1e()1e(a1)1e(a1)1e(a2)1e()1
= 101001001000101
e(m1) = (q1),e(a1),e(q2),e(a2),e(R)
= 00,00,000,000,000
e(m2) = e(q2),e(),e(h),e(),e(S)
= 000,0,0,0,0
e() = e(m1)#e(m2)#...#
= 00,00,000,000,000#000,0,0,0,0#...#
e(T) = e(s)#e()
= 00#00,00,000,000,000#000,0,0,0,0#...#
Input = e(Z)|e(T)|
=
101001001000101|00#00,00,000,000,000#000,0,0,0,0#...#|
CopyStartStateToTape3
UpdateTape2
FindRightMove
0
UpdateStateOnTape3
Jaruloj Chongstitvatana
(halt)
2301379
CopyTape2ToTape1
Turing Machines
54
1 0 0 1
0 0
1 1
0 1
Tape 3
UpdateTape2
FindRightMov e
alt
N ot h
UpdateStateOnTape3
0 0 0
Jaruloj Chongstitvatana
CopyStartStateToTape3
2301379
halt
Turing Machines
CopyTape2ToTape1
55
Church-Turing Thesis
Turing machines are formal versions of algorithms.
No computational procedure will be considered an algorithm unless it
can be presented as a Turing machine.
Checklist
Construct a DTM, multitape TM, NTM accepting languages
or computing function
Construct composite TM
Prove properties of languages accepted by specific TM
Prove the relationship between different types
Describe the relationship between TM and FA
Prove the relationship between TM and FA
UNIT -V
Decidability
Decidable/Undecidable problems
Accepting:
Definition
Characteristic function
For any language L*, the characteristic function of L is the function L(x)
such that
L(x) = 1
if x L
L(x) = 0
otherwise
Example
Let L = { {0,1}* | n1() <n0() <2n1() }, where nx() is the number of xs in
}.
L() = 1
if n1() <n0() <2n1()
L() = 0
otherwise
Deciding: Definition
Accepting/Deciding: Example
S
/@,R
/,L
R
,
/
0/0,L
1/1,L
0/0,R
p2 1/1,R
0/
,L
p4
1/,L
/,L
Jaruloj Chongstitvatana
p3
Hang when
input = 02n
q1
q2
/1,L
/0,L
1/,R
/,L
p1
@/,R
r2
/,L
/,L
0/,R
@/,R
r1
n10
n10
n |n0}
n |n0}
TM decidinging
accepting L={0
L={0
0/,L
1/,L
2301379
If the input x is in L,
T halts with output 1.
If the input x is not in L,
T hangs.
Hang when input
= 0n+m 0n
Decidability
Recursive languages
Example:
{0n10n|n0} is a recursive language.
Theorem:
Proof:
Let L1 and L2 be recursive languages over .
Then, there exist TMs T1 and T2 computing L1 and L2, respectively.
Construct a 2-tape TM M as follows:
TcopyTape1ToTape2 T1 TmoveRight 0 TcopyTape2ToTape1 T2
Proof:
Let L1 and L2 be recursive languages over .
Then, there exist TMs T1 and T2 computing L1 and L2, respectively.
Languages
Theorem: Let L1 and L2 be recursively enumerable languages over . Then, L1L2
is also recursively enumerable.
Proof:
Let L1 and L2 be recursively enumerable languages over .
Then, there exist TMs T1 and T2 accepting L1 and L2, respectively.
Construct an NTM M as follows.
Theorem:
Let L1 and L2 be recursively enumerable languages over . Then, L1L2 is also recursively
enumerable.
Proof:
Let L1 and L2 be recursively enumerable languages over .
Then, there exist DTMs T1 =(Q1, , , 1, s1) and T2 =(Q2, , , 2, s2) accepting L1 and L2,
respectively.
Construct a 2-tape TM M which simulates T1 and T2 simultaneously. Tape 1 represents T1s
tape and Tape 2 represents T2s tape.
Proof:
Let L andL be recursively-enumerable languages over .
Then, there are a TM T accepting L, and a TMT acceptingL.
For any string w in *, w is either in L or inL.
That is, either T orT must halt on w, for any w in *.
We construct an NTM M as follows:
If w is in L, T halts on w and thus, M accepts w.
If w is not in L,T halts on w and thus, M rejects w.
Then, M computes the characteristic function of L. Then, L is recursive.
Decision Problems
decidable.
Self-Accepting
SA (Self-accepting) = {w{0,1,#, ,}*| w=e(T) for some TM T and wL(T)}
NSA (Non-self-accepting) = {w {0,1,#, ,}*| w=e(T) for some TM T and
wL(T)}
E (Encoded-TM) = {w{0,1,#, ,}*| w=e(T) for some TM T}
E is recursive
Theorem: E is recursive.
Proof:
We can construct a regular expression for E according to the
definition of the encoding function as follows:
R = S 1 (M #)+
S=0
M = Q , A, Q , A, D
Q = 0+
A = 0+
D = 0 + 00 + 000
Then, E is regular, and thus recursive.
SA is recursively enumerable
Construct a TM S accepting SA
If w is not e(T) for some TM T, S rejects w.
If w is e(T) for some TM T, S accepts e(T) iff T
accepts e(T).
L(S) = {w| w=e(T) for some TM T accepting e(T) =
SA.
Then, SA is recursively enumerable.
Encode
UTM
Reject
Jaruloj Chongstitvatana
2301379
Decidability
32
SA is not recursive
NSA = E SA
NSA is not recursively enumerable (from previous theorem), and thus not recursive.
But E is recursive.
From the closure property, if L1 and L2 are recursive, then L1 - L2 is recursive.
Using its contrapositive, if L1 - L2 is not recursive, then L1 or L2 are not recursive.
Since NSA is not recursive and E is recursive, SA is not recursive.
Co-R.E.
Definition
A language L is co-R.E. if its complement L is R.E.
It does not mean L is not R.E.
Examples:
SA is R.E. SA=ENSA is not R.E.
SA is co-R.E., but not R.E.
NSA is not R.E. NSA=ESA is R.E.
NSA is co-R.E., but not R.E.
E is recursive, R.E., and co-R.E.
Relationship between R.E., co-R.E. and Recursive Languages
Observation
A language L is either
recursive
R.E., bot not recursive
co-R.E., but not recursive
Neither R.E. nor co-R.E.
R.E.
recursive co-R.E.
2301379
Decidability
36
Reduction Definition:
Let L1 and L2 be languages over 1 and 2, respectively.
L1 is (many-one) reducible to L2, denoted by L1L2, if there is a TM M computing a
function f: 1*2* such that wL1 f(w)L2.
Definition:
Let P1 and P2 be problems. P1 is (many-one) reducible to
P2 if there is a TM M computing a function f: 1*2* such that w is a yes-instance of P1
f(w) is a yes-instance of P2.
Reduction
Definition:
A function f: 1*2* is a Turing-computable function if there is a Turing
machine computing f.
Definition:
Let L1 and L2 be languages over 1 and 2,
respectively. L1 is (many-one) reducible to L2, denoted by L1L2, if there
is a Turing-computable function f: 1*2* such that wL1 f(w)L2.
Meaning of Reduction
P1 is reducible to P2 if TM M computing a function f: 1*2* such
that w is a yes-instance of P1 f(w) is a yes-instance of P2.
If you can map yes-instances of problem A to yes-instances of
problem B, then
we can solve A if we can solve B
it doesnt mean we can solve B if we can solve A
the decidability of B implies the decidability of A
Properties of reduction
Theorem:
Proof:
Properties of reduction
Theorem:
Proof:
Properties of reduction
Theorem:
Proof:
Theorem:
If L2 is co-R.E., and L1L2, then L1 is also co-R.E.
Proof:
Let L1 and L2 be languages over , L1L2, and L2 be co-R.E.
Because L2 is co-R.E,L2 is R.E.
Because L1L2,L1L2. Then,L1 is R.E.
Thus, L1 is co-R.E.
Theorem:
If L2 is co-R.E., and L1L2, then L1 is also co-R.E.
Proof:
Let L1 and L2 be languages over , L1L2, and L2 be co-R.E.
Because L2 is co-R.E,L2 is R.E.
Because L1L2,L1L2. Then,L1 is R.E.
Thus, L1 is co-R.E.
recursive co-R.E.
Let L1L2.
If L1 is not recursive /
R.E. /
co-R.E.,
then L2 is not recursive /
R.E. /
co-R.E.
2301379
Decidability
51
Proof:
(Guess Accept is in R.E., but not co-R.E.)
Show SA Accept
(We want a Turing-computable f n f(<T>)=<M> such that
T accepts e(T) M accepts
T does not accept e(T) M does not accept
Let f(T)=M is a TM that first writes e(T) after its input and then runs
T.
M writes e(T) after its input. If its input is , T has e(T) as input.
Halting problem
Problem
Given a Turing machine T and string z, does T halt on z?
Given a program P and input z, does P halt on z?
Language
Halt = {w*| w=e(T)e(z) for a Turing machine T halting on
z}.
Halt = {<T,z>| T is a Turing machine halting on z}.
Proof:
Let Halt = {<T,z>| T is a Turing machine halting on z}.
(Guess Halt is in R.E., but not co-R.E.)
Show SA Halt
(We want a Turing-computable f n f(<T1>)=<T2 ,z> such that
T1 accepts e(T1) T2 halts on z
T1 does not accept e(T1) T2 does not halt on z
Then, a possible function is f(<T>) = <T, e(T)> because T accepts e(T) T
halts on e(T).)
Checklist