Theory of Computer Science
Theory of Computer Science
Theory of Computer Science
MODULE-1
Alphabet
Language
Language of a Machine
Grammar
Finite Automata
Finite Automata is generally of two types :(i)Deterministic Finite Automata (DFA)(ii)Non Deterministic
Finite Automata(NFA)
DFA
1
0
A B
S State/Input 0 1
1
A B A
0 B C A
1 *
C C A
C 0
2. Construct a DFA that accepts set of all strings over ∑={0,1}, not containing 101 as a substring ?
0 1
1
A B State/Input 0 1
S *A A B
0
0 *B C B
0,1
*C A R
C R R R R
1
NFA
1. Construct a NFA that accepts set of all strings over ∑={0,1}, ending with 00 ?
State/Input 0 1
0,1
A A,B A
A C B C _
B
S 0 0 *C _ _
2. Construct a NFA that accepts set of all strings over ∑={0,1}, containing 10 as substring ?
0,1 0,1
State/Input 0 1
A C A A A,B
B
S 1 0 B C _
*C C C
Every DFA is an NFA, it is clear that the class of languages accepted by NFA’s includes the
languages accepted by DFA’s.
1- begin with {q0}, start state, and calculate ({q0},a) for all a in this gives a number of new states
Q-.
2- For each new states Q-, we again calculate (Q-,a) for all a in and introduce new states if
necessary.
4- Final states of new DFA are the states that contain any final state of the previous NFA.
NFA State Transition Diagram
1,0
A B C
S 1 0
State/Input 0 1
A A [A,B](New)
[A,B] [A,C](New) [A,B]
* [A,C] A [A,B]
0
1 1
A A,B
S
0
0 1
A,C
C
Construct a DFA that accepts set of all strings over ∑={0,1}, not containing 11 as substring ?
0,1 0,1
A B C
S 1 1
State Transition Table of DFA containing 11 as substring.
State/Input 0 1
A A [A,B](New)
[A,B] A [A,B,C](New)
* [A,B,C] [A,C](New) [A,B,C]
*[A,C] [A,C] [A,B,C]
State/Input 0 1
*A A [A,B](New)
* [A,B] A [A,B,C](New)
[A,B,C] [A,C](New) [A,B,C]
[A,C] [A,C] [A,B,C]
0 1
A A,B
S
0
1
0 0
A,C A,B,C 1
1
Conversion of FA with є-moves to NFA with out є-moves
0 1 2
є є
A B C
S
Find є* (Epsilon Closure) of all states
є*(A)={A,B,C}
є*(B)={B,C}
є*(C)={C}
Find δ(A,0)
є*(A)={A,B,C}
Input(є*(A),0)={A}
є*( Input(є*(A),0))={A,B,C}
Like that δ(A,1)={B,C}, δ(A,2)={C}, δ(B,0)={}, δ(B,1)={A,B}, δ(B,2)={C}, δ(C,0)={}, δ(C,1)= {},
δ(C,2)={C}.
State/Input 0 1 2
*A A,B,C B,C C
*B _ B,C C
*C _ _ C
0 1
0,1
A B
S
1,2
0,1,2
2
C
(00+11)
(0+1)* (0+1)*
A C D
B
0,1
0 0
0,1 F
є
є
є є
A E C D I B
1 G
1
0,1
0 0
C
0,1
B
A
1
D 1
CLOSURE PROPERTY OF REGULAR SET
The closure property of a set signifies the containment of the elements of the set under a particular
operation is in the same set. So it defines the set of operations on the regular set that results to an element
of regular set.
If L1 is regular and L2 is regular then L1UL2 ,L1L2 and L1∩L2 are also regular.
The class of regular set is closed under homomorphism and inverse homomorphism.
0 1 0,1
A B C
S 1 0
A=є+A0-------------------(1)
B=A1+B1---------------------(2)
C=B0+C(0+1)-----------------(3)
A= є0*=0*-------------------(4)
B=0*1+B1------------------(5)
B=0*11*=0*1+--------------(6)
A B
S 1
1 0
0
C 0,1
D
1
A=є+B1+D0--------------(1)
B=A0---------------------(2)
C=B0+D1+C(0+1)-------------(3)
D=A1------------------(4)
A= є+A01+A10
= є+A(01+10)
= є(01+10)*
=(01+10)*
Φ+R=R
ΦR=RΦ= Φ
єR=Rє=R
є*=є
φ*=є
R+R=R
R*R*=R*
RR*=R*R
(R*)*=R*
Є+RR*=R*=є+R*R
(PQ)*P=P(QP)*
(P+Q)*=(P*Q*)*=(P*+Q*)*
(P+Q)R=PR+QR
R(P+Q)=RP+RQ
Q. Prove (1+00*1)+(1+00*1)(0+10*1)*(0+10*1)=0*1(0+10*1)*
LHS=(1+00*1)+(1+00*1)(0+10*1)*(0+10*1)
=(1+00*1)(є+(0+10*1)*(0+10*1))
=(1+00*1)(0+10*1)*
=(є+00*)1(0+10*1)*
=0*1(0+10*1)*=RHS.
Regular Grammar
0,1
A B C
S 0 0
RLG
A0A|0B|1A
B0C|0
Updating RLG
A0A|0B|1A
B0
Constructing LLG
1. Construct the reverse machine of the Given FA(Covert initial state to final state and vice versa).
2. Find RLG.
3. Reverse the position of Nonterminal and terminal at the RHS of the productions.
0,1
A B C
0 0 S
C0B
B0A|0
A0A|1A|0|1
LLG
CB0
BA0|0
AA0|A1|0|1
MINIMIZATION OF FA
It is desirable to reduce the number of states as far as possible because it requires less space and is easily
processed. Here we are using partitioning method for minimization.
Partitioning Method.
1. At first states are divided into final states and non-final states.
3. Again the groups are converted into groups by looking at their transition to same group
of different group. If two states are transiting to same groups by the same input then they are
kept in same group or separate groups.
State Minimization of FA
0
A B
S
0
1 1
C D
State/Input 0 1
A B *C
B A *D
*C *E F
*D *E F
*E *E F
F F F
By Partitioning Algorithm
0 0, 1
[A, B] [F]
S
1
1
[C,D,E]
a. |y|≥1
b. |xy|≤n
To prove a language as non regular we have to find out a I value such that condition (c) is not satisfied.
Solution:
A CFG denoted G(V,T,P,S) where V is a finite set of nonterminals,T is a finite set of terminals,P is the
finite set of production rules and S is a starting symbol.
Ans. SaSa
SbSb
Sє|a|b
Deviation Tree is a tree representation of derivation steps required to derive a string from the starting
symbol S of the CFG G.
.
S
S
a a S
A
b a a
The step wise derivation shown in the above tree.
SSS
aaAS
aabaS
aabaa
If at each step in a derivation a production is applied to the left most nonterminal, then the derivation is
called LMD, if it is applied to right most nonterminal then the derivation is called RMD.
LMD
SaAS
abaS
abaa
RMD
SSS
Sa
aASa
aAaa
abaa
AMIGUOUS GRAMMAR
A CFG G is called ambiguous iff there exist some words wєL(G), that can be expressed by more than one
parse tree. Also there exist more than one LMD or RMD.
Two LMDs
SS+Sa+Sa+S*Sa+a*Sa+a*b
SS*SS+S*Sa+S*Sa+a*Sa+a*b
SIMPLIFICATION OF CFG
A CFG G can be simplified by eliminating not useful symbols from G. This can be achieved by
eliminating useless symbols, Null productions and unit productions.
SAB/a
Aa
Sa
Aa
SaS|bA,AaA|є
SaS|bA|b,AaA|a
SA|bb
AB|b
BS|a
Aa|bb|b
Bbb|b|a
NORMAL FORMS
According to context free grammar the right hand side of the production can be any combination of
terminals and nonterminals. The Normal Forms are used to restrict the right hand side of a production
rule.
A context free grammar G is inCNF if all the production obey the form ABC or Aa.
A context free grammar G is inCNF if all the production obey the form Aaα , where αє(V)*.
SXM
Xa
MAD
AXB
AYN
Yb
NAB
Bb
Dd
M(Q,∑,┌,δ,q0,z0,F)
δ =Qx{∑U{є}}x┌Qx┌*
A PDA is said to be deterministic if all derivations(ID) in the design has to give only single move.
A PDA is said to be nondeterministic ,if derivation generates more than one move in the designing of
particular task.
Instantaneous Description
a,Z0|xZ0
b,x|є
q0 q1
S b,x|є
є,Z0|є
a,x|xx
q2
State Transition Table
a b є
x Z0 x Z0 x Z0
q0 (q0,xx) (q0,xZ0) (q1,є) _______ ________ _______
q1 _________ _______ (q1,є) _______ ________ (qf ,__)
q2 _________ _______ _________ _______ _________ ________
a,x|є
q0 q1
S
b,y|є
qf
Cocke,Younger & Kasami Algorithm
This algorithm is useful for testing membership in a CFL(Context Free Language). This is based on the
idea of Dynamic programming. The CFG should be in CNF. The table is filled row-by-row in bottom to
top. The bottom row correspond to the substring of length 1. The second row from bottom row
corresponds to the substring of length 2, like on. It takes O(n) time to compute any one entry of the table
and here n(n+1)/2 entries are there so T.C. is O(n3) .
X15
X14 X25
X13 X24 X35
X12 X23 X34 X45
X11 X22 X33 X44 X55
a1 a2 a3 a4 a5
In this above figure the word of length 5 say a1a2 a3 a4 a5 is tested for its membership under a CFG say G.
If we find the starting symbol of the CFG in X15 then we can say a1a2 a3 a4 a5 is a member of L(G).
SAB|BC
ABA|a
BCC|b
CAB|a
Xij=Ui≤k<j XikXk+1j
X12=X11X22={B}{A,C}={BA,BC}={A,S}
X23=X22X33={A,C}{A,C}={AA,AC,CA,CC}={B}
X34=X33X44={A,C}{B}={AB,CB}={S,C}
X45=X44X55={B}{A,C}={BA,BC}={A,S}
={BB,SA,SC,AA,AC}={}
X24=X22X34UX23X44
={A,C}{S,C}U{B}{B}
={AS,AC,CS,CC,BB}
={B}
X35=X33X45UX34X55
={A,C}(A,S)U{S,C}{A,C}
={AA,AS,CA,CS,SA,SC,CA,CC}
={B}
X14=X11X24UX12X34UX13X44
={B}{B}U{A,C}{S,C}U{}{B}
={BB,AS,AC,CS,CC,B}
={}
X25=X22X35 UX23X45UX24X55
={A,C}{B}U{B}{S,A}U{B}{A,C}
={AB,CB,BS,BA,BA,BC}
={S,A,C}
X15=X11X25UX12X35UX13X45UX14X55
={B}{S,A,C}U{S,A}{B}U{}{S,A}U{}{A,C}
={BS,BA,BC,SB,AB,S,A,C}
={A,S,C}
{S,A,C}
--------- {S,A,C}
--------- {B} {B}
{S,A} {B} {S,C} {S,A}
{B} {A,C} {A,C} {B}
{A,C}
b a a b a
Parikh’s Theorem
To understand Parikh’s Theorem there is a need of some basic definitions of Linear subset, Semilinear
subset and Parikh’s Mapping.
Linear subset: A linear subset M, of Nn is given by tuples t0,t1,……,tm є Nn, where m єN and n єN+.
M={t0+l1t1+….+lmtm|l1,l2,…..,lm є N }
=t0+{t1,…..tm}*
Semilinear subset: A semilinear subset,M΄, of Nn, is a union of finite number of linear subsets M1,…..Mk,
where k єN+.
Parikh Mapping: if a word w is defined over ∑={a1,a2,…..,an}, where n єN+ , Parikh image is
Parikhs Theorem
For every context-free language L. Ψ(L) is effectively semilinear. The tuples specifying Ψ(L) can be
constructed effectively from a context-free grammar generating L.
Pumping Lemma for CFL is generally used to prove a language is not context free.
Let L be any CFL. Then there is a constant n, depending only on L, such that if z is in L and |z|≥n. Then
we may write w=uvwxy such that
1) |vx|≥1
2) |vwx|≤n
Solution:-
u=a,v=a,w=a,x=a,y=a,
The TM can be thought of as a finite state automaton connected to a R/W head. It has one tape which is
divided into number of cells.
……………… a1 a2 a3 a4 ……………
Reading/Writing head
Each cell can store only one symbol. R/W examines the tape symbols. In one move, the machine
examines the present symbol under R/W head on the tape and the present state of an automation to
determine:
1) A new symbol to be written in the tape in the cells under R/W head.
2) A motion of the R/W head along the tape either the head moves one cell left on one cell
right.
A TM is a 7-tuple notation
M=(Q,∑,┌,δ,q0,B,F) where
δ =Qx┌=Qx┌x{L,R}
q0=Initial state
B=Blank symbol.
Instanteneous Description
a|a,R
State Transition Diagram
Y|Y,R
a|X,R
q1
b|b,R
q0
S b|Y,L
X|Y,R
Y|Y,R q2 Z|Z,R
Y|Y,R c|Z,L
q4 q3
Z|Z,R
UNIVERSAL TURING a|a,L MACHINE Z|Z,L
B|B,R
A general-purpose Turing machine is usually called universal Turing machine which is powerful enough
to simulate the behavior of any computer. Universal Turing machine can simulate the behavior of an
qf Y|Y,L b|b,L
arbitrary Turing machine.
A Turing machine is called deterministic if at most one description can be possible from any description.
OR
There exists at most one entry in any cell of state transition table representation of Turing machine.
OR
There exists more than one entry in at least one cell of state transition table representation of Turing
machine.
A language L over the alphabet ∑ is called recursive if there is a TM say T, that accepts every word in L
and rejects every word in L΄, that is
A language L over the alphabet ∑ is called recursively enumerable if there is a TM say T, that accepts
any word in L and either reject or loops forever for every word in L΄, that is
Accept(T)=L
Reject(T) +Loop(T)= L΄
This model accepts the set of context-sensitive languages, the length of the tape is restricted by a linear
function.
A LBA is a nondeterministic Turing machine which has a single tape whose length is not infinite but
bounded by a linear function.
δ =Qx┌=2Qx┌x{L,R}
q0=Initial state
B=Blank symbol.
A context sensitive grammar can never generate a language containing the empty string i.e. Xє is not
allowed.
Basic Functions
Composition and Primitive Recursion are used to build more complicated functions from the basic
functions.
1. Composition f(x,y)=h(g1(x,y),g2(x,y))
2. Primitive Recursion
f(x,0)=g1(x)
f(x,y+1)=h(g2(x,y),f(x,y))
A function is called Primitive Recursive if and only if it can be constructed from the basic
functions Z,S,Pk by successive composition and primitive recursion.
Ackermanns Function
A(0,y)=y+1
A(x,0)=A(x-1,1)
A(x,y)=A(m-1,A(m,n-1))
Addition, Multiplication functions are the Primitive Recursive function but Ackermanns function
is not Primitive Recursive.
So for the construction of Ackermanns Function and some other functions μ-recursive function is
required.
In mathematical logic, a Gödel numbering is a function that assigns to each symbol and well-
formed formula of some formal language a unique natural number, called its Gödel number.
Gödel used a system based on prime factorization. He first assigned a unique natural number to
each basic symbol in the formal language of arithmetic with which he was dealing.
To encode an entire formula, which is a sequence of symbols, Gödel used the following system.
Given a sequence of positive integers, the Gödel encoding of the sequence
is the product of the first n primes raised to their corresponding values in the sequence:
According to the fundamental theorem of arithmetic, any number obtained in this way can be
uniquely factored into prime factors, so it is possible to recover the original sequence from its
Gödel number.
Example:-
Suppose the Gödel number for the symbol "&" is 2 and the Gödel number for the symbol "=" is
5. Thus, the Gödel number of the formula "& = " is 22.35=972.
Halting problem is an undecidable problem. So it is not possible to work with halting problem directly.
And it is convenient to establish some intermediate results that bridge the gap between halting problem
and other problems.
The intermediate result follow from the undecidability of the halting problem. But much more closer to
the problem that we want to study. One such intermediate result is Post Correspondence Problem.
PCP Statement
Given two sequence of n strings on some alphabet ∑, say A=w1,w2,….,wn and B=v1,v2,……,vn we say
there exists a post correspondence solution for pair( A,B)if there exist a nonempty sequence of integers
i,j,….,k such that wi wj…..wk=vi vj……vk.
Example:- Let ∑={0,1} and take A and B as
A w1=00, w2=101,w3=110
Bv1=001,v2=011,v3=10
For integer sequence <1,2,3> there exist a PCP solution because w1 w2 w3=v1 v2 v3 .
Suppose
A w1=00, w2=101,w3=110
Bv1=001,v2=011,v3=101
Decidable Problem
P is the set of all decision problems solvable by deterministic Turing machine in polynomial time.NP is
the set of all decision problems solvable by nondeterministic Turing machine in polynomial time.
Disclaimer
This above material may contain some mistakes. So the reader can read carefully.