Notes Compiled by Prof.: Ganesh Sir: Sem - V (COMP)
Notes Compiled by Prof.: Ganesh Sir: Sem - V (COMP)
TCS
Notes Compiled By Prof.:
GANESH SIR
Notes SET-I
Sem – V
(COMP)
CHOPRA ACADEMY
Degree & Diploma
Bandra (W) Thane (W) Vashi
201 & 305, Off No 1 & 2Gnd Flr, 350 C- Wing
Hill Road, Gangadhar Apt, Sector – 17
Rizvi Chambers, RS Rd,Data Mandir Vashi Plaza,
Mumbai :400050 Thane - 400601 Navi-Mumbai
Call : 2655 9366. Call : 6579 4001. Call : 26421927
9967 843 854 9967 843 853 9967 843 852
Office Timing : 10.30a.m to 7.30p.m (Monday to Saturday)
10.30a.m to 3.30pm (Sunday)
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Dear Students,
Welcome to the world of Theory in
computation. For any doubts you can contact me
at 83 84 82 05 75 between 9:00 pm to 1:00 am. I
will also be posting important question and
exam solution on my blog before the
examination.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Syllabus:
Chapter Chapter Name Contents
No.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Chapter 1
Finite Automata/Finite State Machine
History of Automata:
Automata theory is the study of abstract computing devices, or "machines."
Before there were computers, in the 1930's, A. Turing studied an abstract machine
that had all the capabilities of today's computers, at least as far as in what they could
compute.
In the 1940's and 1950's, simpler kinds of machines, which we today call "finite
automata," were studied by a number of researchers.
Also in the late 1950's, the linguist N. Chomsky began the study of formal
"grammars."
While not strictly machines, these grammars have close relationships to abstract
automata and serve today as the basis of some important software components,
including parts of compilers.
In 1969, S. Cook extended Turing's study of what could and what could not be
computed.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Cook was able to Separate those problems that cannot be solved efficiently by
computer from those problems that can in principle be solved, but in practice take
so much time that computers are useless for all but very small instances of the
problem.
Some of the concepts, like finite automata and certain kinds of formal grammars, are
used in the design and construction of important kinds of software.
Other concepts, like the Turing machine, help us understand what we can expect
from our software.
Especially, the theory of intractable problems lets us deduce whether we are likely to
be able to meet a problem "head‐on" and write a program to solve i t (because it is
not in the intractable class), or whether we have to find some way to work around
the intractable problem: find an approximation, use a heuristic, or use some other
method to limit the amount of time the program will spend solving the problem.
Definition: A finite automaton has a set of states, and its "control'' moves from state
to state in response to external "inputs".
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Finite automata are a useful model for many important kinds of hardware and
software. We shall see some examples of how the concepts are used. For the
moment, let us just list some of the most important kinds:
2. The "lexical analyzer” of a typical compiler, that is, the compiler component
that breaks the input text into logical units, such as identifiers, keywords, and
punctuation.
3. Software for scanning large bodies of text, such as collections of Web pages, to
find occurrences of words, phrases, or other patterns.
4. Software for verifying systems of all types that have a finite number of
distinct states, such as communications protocols or protocols for secure
exchange of information.
There are many systems or components, such as those enumerated above, that may
be viewed as being at all times in one of a finite number of "states."
The purpose of a state is to remember the relevant portion of the system's history.
Since there are only a finite number of states, the entire history generally cannot be
remembered, so the system must be designed carefully, to remember what is
important and forget what is not.
The advantage of having only a finite number of states is that we can implement the
system with a fixed set of resources.
Example 1: Perhaps the simplest nontrivial finite automaton is an on/off switch. The
device remembers whether it is in the "on" state or the "off" state, and it allows the
user to press a button whose effect is different, depending on the state of the switch.
That is, if the switch is in the off state, then pressing the button changes it to the on
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
statte, and if th
he switch iss in the on sstate, then pressing th
he same buutton turns it to the
off sstate.
Push
h
Push
h
Thee finite‐auto
omaton mo
odel for thee switch is shown
s in Fig.
As ffor all finitte automataa, the statees are reprresented byy circles; inn this exam
mple, we
havve named th he states on
n and off. A
Arcs betweeen states are
a labeled by "inputs," which
represent exteernal influe ences on thhe system. Here,
H both arcs are laabeled by th he input
Pussh, which represents a user push hing the buutton. The intent of thhe two arcs is that
whiichever staate the systtem is in, wwhen the Pu ush input iss received iit goes to th
he other
statte.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For
F Doub bts Conta
act Ganes
sh Sir@833 84 82 05
0 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Theese positio ons corresp pond to thhe prefixess of the woord, ranginng from the
e empty
string (i.e., notthing of the
e word has been seen so far) to the
t compleete word.
In F
Fig, the fivee states are
e named byy the prefixx of then seeen so far. IInputs corrrespond
to letters. Wee may imag gine that th he lexical analyzer
a ex
xamines onne characte er of the
program thatt it is comp piling at a ttime, and th
he next cha
aracter to bbe examine ed is the
inpuut to the automaton.
Auto
omata
a and Comple
C exity:
Auttomata are essential for
f the stud dy of the liimits of com
mputation.. As we me
entioned
in th
he introducction to the
e chapter, tthere are tw
wo importa ant issues:
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For
F Doub bts Conta
act Ganes
sh Sir@833 84 82 05
0 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
2. What can a computer do efficiently? This study is called "intractability," and the
problems that can be solved by a computer using no more time than some slowly
growing function of the size of the input are called "tractable." Often, we take all
polynomial functions to be "slowly growing," while functions that grow faster than
any polynomial are deemed to grow too fast.
1. Alphabets
2. Strings
A string (or sometimes word) is a finite sequence of symbols chosen from some
alphabet. For example, 01101 is a string from the binary alphabet ∑ = {0, 1}.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
The empty string is the string with zero occurrences of symbols. This string, denoted ε
is a string that may be chosen from any alphabet whatsoever.
4. Length of a String
It is often useful to classify strings by their length, that is, the number of
positions for symbols in the string.
For instance, 01101 has length 5. It is common to say that the length of a
string is "the number of symbols" in the string; this statement is colloquially
accepted but not strictly correct.
Thus, there arc only two symbols, 0 and 1, in the string 01101, but there are
five positions for symbols, and its length is 5.
However, you should generally expect that "the number of symbols" can be used
when "number of positions" is meant.
The standard notation for the length of a string w is lwl. For example,
5. Powers of an Alphabet
If Σ is an alphabet, we can express the set of all strings of a certain length from
that alphabet by using an exponential notation. We define Σk to be the set of
strings of length k, each of whose symbols is in E.
Example: Note that Σ0 = {ε}, regardless of what alphabet Σ is. That is, ε is the only
string whose length is 0.
Note that there is a slight confusion between Σ and Σ1. The former is an alphabet;
its members 0 and 1 are symbols. The latter is a set of strings; its members are
the strings 0 and 1, each of which is of length 1.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
The set off all stringgs over an alphabet Σ is conve entionally denoted Σ*. For
instance, {0, 1}* = {ε, 0, 1, 00, 01
1, 10, 11,00
00, ...}. Put another
a waay,
Sometimess, we wish to excludee the empty y string from the set oof strings. The set
of nonemp pty stringss from alp
phabet Σ iss denoted Σ+. Thus,, two apprropriate
equivalencces are:
Σ + = Σ 1 ∪ Σ 2 ∪ Σ3 …
Σ* = Σ+ U {εε}
6. Con
ncatenatio
on of String
gs
Let x and y be string gs. Then xy denotess the conccatenation of x and y, that is,
the string formed by y making a copy of x and
a follow
wing it by a copy of y.
More precisely, if x iss the stringg composed
d of i symbbols x = a1a 2… ai and y is the
string com
mposed of j symbols y = b1b2… bj, then xy is i the strinng of length
h i+j and
xy = a1a2… aib1b2… bj.
Example: Let x = 01101 an d y = 110. Then xy = 011101110 an nd yx =
11001101.
For any strring w, thee equationss εw = wε = w hold. That is, ε iss the identtity for·
concatenattion, since when con ncatenated
d with anyy string itt yields th he other
string as a result (aanalogouslyy to the way
w 0, the identity
i foor addition, can be
added to any
a number x and yieelds x as a result).
r
Lang
guages::
A sset of strin ngs all of which
w are chosen frrom some Σ*, where Σ is a pa
articular
alphhabet, is caalled a langu
uage.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For
F Doub bts Conta
act Ganes
sh Sir@833 84 82 05
0 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Notice that a language over Σ need not include strings with all the symbols of Σ, so
once we have established that L is a language over Σ, we also know it is a language
over any alphabet that is a superset of Σ.
The choice of the term "language" may seem strange. However, common languages
can be viewed as sets of strings.
An example is English, where the collection of legal English words is a set of strings
over the alphabet that consists of all the letters.
However, there are also many other languages that appear when we study automata.
Some are abstract examples, such as:
1. The language of all strings consisting of n 0's followed by n 1's, for some n ≥ 0
L = { ε, 01, 0011, 000111, ...}.
2. The set of strings of 0's and 1's with an equal number of each:
L = {ε, 01, 10,0011, 0101, 1001, . . .}
3. The set of binary numbers whose value is a prime:
L = {10,11,101,111, 1011, . . }
4. L* is a language for any alphabet Σ.
5. ∅, the empty language, is a language over any alphabet.
6. {ε}, the language consisting of only the empty string, is also a language over
any alphabet. Notice that ∅ ≠ { ε }; the former has no strings and the latter
has one string.
The only important constraint on what can be a language is that all alphabets are
finite. Thus languages, although they can have an infinite number of strings, are
restricted to consist of strings drawn from one fixed, finite alphabet.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
This expression is read "the set of words ω such that (whatever is said about ω to
the right of the vertical bar)."
Examples are:
It is also common to replace ω by some expression with parameters and describe the
strings in the language by stating conditions on the parameters. Here are some
examples; the first with parameter n, the second with parameters I and j:
1. {0n1nl n ≥ 1}. Read "the set of 0 to then 1 to then such that n is greater than or
equal to 1," this language consists of the strings {01, 0011,000111, .. .}.
2. {0i1j I 0 ≤ i ≤ j}. This language consists of strings with some 0's (possibly none)
followed by at least as many 1's.
Problems:
In automata theory, a problem is the question of deciding whether a given string is a
member of some particular language.
It turns out, as we shall see, that anything we more colloquially call a "problem" can
be expressed as membership in a language. More precisely, if Σ is an alphabet, and L
is a language over Σ, then the problem L is:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Desiign step
ps for Finite
F A
Automa
ata: Reffer classs notess.
1. Inp
put Tape
2. Reaading Head
d
3. Finite Control
Block Diagram
D of Fi nite State Ma
achine
1. Inp
put Tape:
a. The input tape is divided into squarres, each square
s conntaining th
he input
symboll from the input
i alphaabet ∑.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For
F Doub bts Conta
act Ganes
sh Sir@833 84 82 05
0 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
c. The absence of endmarkers indicates that the tape is of infinite length. The
left to right sequence of symbol between the two endmarkers is the input
string to be processed.
2. Reading Head:
a. The head examines only one square at a time and can move one square either
to left or to the right.
b. We restrict the movement of the Reading head only to the right side. The
head read the input data from the input tape and give it to the Finite Control.
3. Finite Control:
a. Finite Control has the actual logic of the model.
b. The input to the finite control will usually be the symbol under the Reading
Head, say ‘∑i’ and the present state of the machine say ‘Qi’ then the next state
of the machine will be given by δ(Qi, ∑i) Qj.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
δ(Qi, ∑i) Qj
Machine transits from Qi to Qj on input ∑i.
5. After reading an input symbol, the reading head moves right to the next square.
6. This process is repeated again and again, i.e.
a. A symbol is read.
b. State of the machine (finite control) changes.
c. Reading head moves to the right.
7. Eventually, the reading head reaches the end of the input string. The last cell in the
input tape is represented by $.
8. Now, the automaton has to say ‘yes’ or ‘no’. If the machine ends up in one of a set of
finial states(q1) then the answer is ‘yes’ otherwise the answer is ‘no’.
One of the crucial distinctions among classes of finite automata is whether that
control is "deterministic," meaning that the automaton cannot be in more than one
state at any one time, or "nondeterministic," meaning that it may be in several
states at once.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
The term "finite automaton or finite state machine" will refer to the deterministic
variety, although we shall use "deterministic" or the abbreviation DFA normally.
Note: If you ask to design FSM or FA, then you should design DFA.
3. A transition function that takes as arguments a state and an input symbol and
returns a state. The transition function will commonly be denoted δ. In our
informal graph representation of automata, δ was represented by arcs
between states and the labels on the arcs. If q is a state, and a is an input
symbol, then δ(q,a) is that state p such that there is an arc labelled a from q
to p.
The most brief representation of a DFA is a listing of the five components above. In
proofs we often talk about a DFA in "five‐tuple" notation:
A= (Q, Σ, δ, qo, F)
where A is the name of the DFA, Q is its set of states, Σ its input symbols, δ its
transition function, q0 its start state, and F its set of accepting states.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
1. Transition Diagrams
A transition diagram for a DFA A = (Q, Σ, δ, qo, F) is a graph defined as follows:
2. For each state q in Q and each input symbol a in Σ, let δ(q,a) = p, then the
transition diagram has an arc from node q to node p, labelled a. If there are
several input symbols that cause transitions from q to p, then the transition
diagram can have one arc, labelled by the list of these symbols.
3. There is an arrow into the start state qo, labelled Start. This arrow does not
originate at any node.
0
0, 1
Figure: The transition diagram for the DFA accepting all strings with a
substring 01
2. Transition Tables
A transition table is a conventional, tabular representation of a function like δ
that takes two arguments and returns a value.
The rows of the table correspond to the states, and the columns correspond to
the inputs.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
F
Figure: Tra
ansition ta
able for the DFA accep
pting all strings with a substring
0
01
Exten
nding the
e Transiition Fun
nction to
o Stringss: Refer class
notes
Nond
determ
ministicc Finite
e Autom
mata
A "n
nondeterm ministic" finiite automaaton (NFA) has the pow wer to be iin several states
s at
oncce. This abillity is often
n expressed
d as an ability to "guesss' somethiing about itts input.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For
F Doub bts Conta
act Ganes
sh Sir@833 84 82 05
0 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Forr the NFA, δ is a functiion that taakes a statee and inputt symbol aas argumen
nts {like
the DFA 's trransition fu unction), b but return ns a set of zero, onne, or morre states
{ratther than returning exactly
e onee state, as the
t DFA mu ust).
Exaample: Figu ure, shows a nondeterrministic fiinite autom maton, whoose job is to o accept
all aand only th
he strings of 0's and 1''s that end in 01. Statte q0 is the start state,, and we
can think off the autom maton as b being in state q0 (pe erhaps am mong otherr states)
wheenever it has
h not yet "guessed" that the fin nal 01 has begun. It is always possible
p
thatt the next symbol
s doees not begi n the final 01, even iff that symbbol is 0. Thuus, state
q0 mmay transittion to itsellf on both 0 and 1.
Howwever, if th
he next sym
mbol is 0, th
his NFA alsso guesses that the ffinal 01 hass begun.
An arc labelleed 0 thus leads fromm q0 to staate q1. Nottice that thhere are tw wo arcs
labeelled 0 out of q0 .
Nottice that th
here is no arc
a out of q 1 labeled 0,
0 and there
e are no arrcs at all ou
ut of q2.
In tthese situattions, the thread
t of th
he NFA's existence
e co
orrespondiing to thosse states
sim
mply "dies,"" although other threeads may continue
c to
o exist.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For
F Doub bts Conta
act Ganes
sh Sir@833 84 82 05
0 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Figure: Th
he states an NFA iss in durin
ng the pro
ocessing off input se
equence
001
101
Thuus, after reaading 001, the NFA iss in states q0 and q2. Since
S q2 is aan acceptin
ng state,
the NFA accep pts 001.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For
F Doub bts Conta
act Ganes
sh Sir@833 84 82 05
0 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
However, the input is not finished. The fourth input, a 0, causes q2's thread to die,
while q0 goes to both q0 and q1. The last input, a 1, sends q0 to q0 and q1, to q2. Since
we are again in an accepting state, 00101 is accepted.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Thee fact that other choiices using the input symbols of o ω lead tto a non‐acccepting
statte, or do no
ot lead to any
a state aat all (i.e., the
t sequence of state s "dies"), does
d not
prevent ω from m being acccepted by tthe NFA as a whole.
Forrmally, if A=
= (Q, Σ, δ, qo, F) is an N
NFA, then
Refer Class No
otes for Exampless.
Equiivalencce of Determ
D ministic and Nondet
N terministic:
Finitte Auto
omata: Refer C
Class Notes
1. Fin
nding Strings in Text
T
A coommon problem in th he age of th
he Web an nd other onn‐line text repositorie
es is the
follo
owing.
1. Given a set of wo ords, find aall documeents that contain onne (or all) of those
words. A search h engine iss a popular example of this prrocess. The e search
engine uses a parrticular tecchnology, called
c inverrted indexees, where for
f each
word appearing
a on
o the We b (there are a 100,000 0,000 differrent word
ds), a list
of all th
he places where
w that word occu urs is store
ed. Machinnes with ve
ery large
amounts of main n memory keep the most comm mon of theese lists av
vailable,
allowin ng many peeople to seaarch for doccuments att once.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For
F Doub bts Conta
act Ganes
sh Sir@833 84 82 05
0 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
2. Inverted‐index techniques do not make use of finite automata, but they also
take very large amounts of time for crawlers to copy the Web and set up the
indexes. There are a number of related applications that are unsuited for in‐
vetted indexes, but are good applications for automaton‐based techniques.
The characteristics that make an application suitable for searches that use
automata are:
A "shopping robot" wants to search for the current prices charged for
the items that its clients request. The robot will retrieve current
catalog pages from the Web and then search those pages for words
that suggest a price for a particular item.
The text of a document is fed, one character at a time to this NFA, which then
recognizes occurrences of the keywords in this text. There is a simple form to an
NFA that recognizes a set of key words.
1. There is a start state with a transition to itself on every input symbol, e.g.
every printable ASCII character if we are examining text. Intuitively, the start
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
state represents
r a "guess" that we have
h not yet
y begun tto see onee of the
keyworrds, even iff we have s een some letters of on
ne of these words.
2. For eacch keyword a1a2…ak, there are k states, say q1, q2, ... , qk· Th here is a
transitiion from th
he start staate to q1 on
n symbol a1, a transittion from q1 to q2
on sym
mbol a2 , and so on. Th he state qk is an accep
pting state and indica
ates that
the key
yword a1a2…ak has beeen found.
Exaample: Supp pose we want to desiign an NFA A to recognize occurreences of the words
web b and ebayy. The transsition diagrram for thee NFA desig
gned usingg the rules above
a is
in bbelow Fig. State 1 iss the start state, and d we use Σ to stand for the se et of all
prin
ntable ASC CII characte
ers. States 2 through 4 have the job
j of recoggnizing we eb, while
stattes 5 througgh 8 recog
gnize ebay.
Figu
ure: An NFA that search
hes for the words
w web and
a ebay
Somme text‐proocessing programs, su uch as advaanced forms of the UNNIX grep coommand
(egrrep and fgrrep) actuallly use a m
mixture of th
hese two approaches . However,, for our
purrposes, conversion to a DFA is eaasy and is guaranteed d not to inccrease the number
of states.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For
F Doub bts Conta
act Ganes
sh Sir@833 84 82 05
0 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
FSM properties:
1. Periodicity:
a. The limitations of FSM is that it does not have the capacity to remember
arbitrarily large amount of information, because it has only a fixed number of
states and this set a limit to the length of the sequence it can remember.
b. Also, we have seen a finite control representation of FSM, where read head
moves always one position to the right after reading an input symbol.
c. Head can never move in reverse direction, therefore, FSM cannot retrieve
what it read previously, before coming to current position of tape.
2. State Determination:
a. Since, the initial states of an FSM and the input sequence given to it,
determines the output sequence.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
b. It is always possible to discover the unknown state, in which the FSM resides
at a particular instance.
3. Impossibility of Multiplication:
a. An FSM cannot remember arbitrarily long sequences.
As FSM has no capability to remember all the earlier inputs to it, cannot compare with
the remaining to check well‐formedness. It is an impossible task for any FSM.
Divisibility:
1. Design a machine which checks whether a given decimal number is divisible by 3.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
8. Design a machine which checks whether the given ternary number is divisible by 5 or not.
(May‐03)
Ending with:
9. Design FSM that accepts set of all strings ending with 101.
10. Design FSM that accepts set of all strings ending with ‘aab’.
11. Design FSM that accepts set of all strings ending with ‘110’ or ‘101’.
12. Design FSM that accepts set of all strings ending with ‘abb’ or ‘bba’.
13. Design FSM that accepts set of all strings with second last symbol is ‘a’ over {a, b}.
Contains:
14. Design FSM in which input is valid if it contains ‘1101’ over {0, 1}.
15. Design FSM that accepts set of all strings, if it contains atlest one occurrence of substring
‘bba’ over {a, b}.
16. Design an FSM in which input is valid if it does not contain any occurrence of 3 consecutive
b’s over {a, b}.
Adder:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Extra Problems:
18. Design FSM, which has odd number of 0’s and any number of 1’s.
19. Design FSM which accepts a string if it contains even number of 0’s and odd number of 1’s.
20. Design FSM which accepts all string which end with 00.
21. Design FSM that accepts set of all strings containing with 1001.
22. Design FSM which accepts the string if it is ending with ‘aa’.
23. Design FSM for the language in which the string is acceptable if the second last symbol is ‘a’
over the ∑={a, b}
24. Design a machine which checks whether a given decimal number is even.
25. Design FSM for the language over the ∑={ a, b} and the string is acceptable if it contains
a. At most 3 a’s
b. At least 3 a’s
c. Exactly 3 a’s
26. Design FSM for the language over the ∑={ a, b} and the string is acceptable if it does not
contain ‘bbb’.
27. Design FSM to recognize sub string CAT from the set ∑={ C, H, A, R, I, O, T}.
28. Design FSM to add 2 binary numbers of equal lengths. (May‐00, Dec‐01)
29. Design FSM which generates the remainder when the given decimal number is divided by 3.
(Dec‐01)
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
30. Design FSM which generates the remainder when the given binary number is divided by 4.
31. Design FSM which generates the remainder when the given ternary number is divided by 3.
Problems:
Refer class Notes for below problems:
3. Design a DFA for a set of strings over ∑ = {0, 1} such that the number of 0’s is divisible by 5,
and number of 1’s divisible by 3.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
7. Design the DFA which accepts set of strings such that every string containing ‘00’ as a
substring but not ‘000’ as a substring. (Dec‐2007)
8. Design the DFA for the language, containing strings in which leftmost symbol differ from
rightmost symbol. ∑ = {0, 1}. (May‐2008, Dec‐2008)
9. Design a DFA for set of strings over ∑ = {0, 1} in which there are at least two occurrences of
‘b’ between any two occurrences of ‘a’.
10. Design a DFA for set of all strings over ∑ = {0, 1} ending in either ‘ab’ or ‘ba’.
11. Design a DFA for set of all strings over ∑ = {0, 1} containing both ‘ab’ and ‘ba’ as substrings.
12. Design a FA that reads string defined over ∑ = {0, 1} and accepts only those strings which
end up in either ‘aa’ or ‘bb’.
13. Design a DFA for set of all strings over ∑ = {0, 1} containing neither ‘aa’ nor ‘bb’ as a
substring.
14. Design a DFA for set of all strings over ∑ = {0, 1} such that each ‘a’ in ω is immediately
preceded and immediately followed by a ‘b’.
15. Design a DFA for set of all strings over ∑ = {0, 1} such that every block of five consecutive
symbols contains at least two 0’s.
16. Design a DFA for set of all strings over ∑ = {0, 1} such that strings either begin or end with
‘01’.
17. Design a DFA for set of all strings over ∑ = {0, 1} such that the third symbol from the right
end is ‘1’. (Dec‐2008)
18. Construct a DFA for set of strings containing either the substring ‘aaa’ or ‘bbb’.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
19. Construct a DFA for accepting a set of strings over ∑ = {0, 1} not ending in ‘010’.
20. Design a DFA that reads strings made up of letters in the word ‘CHARIOT’ and recognizes
these strings that contain the word ‘CAT’ as a substring.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Cha
apter 2
Reg
gular pressio
Exp ons
Regu
ular Ex
xpress
sions
Now from machine‐like de
w, we swittch our atttention fr escriptions of langua
ages ‐
detterministicc and nondeterm
n ministic fin
nite autom
mata ‐ to an algebraic
desscription: the
t "regula
ar express ion."
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
2. Concatenation:
Say L1 and L2 are the two Language, then L1.L2 = {ab | a Є L1 and b Є L2}
Example: L1 = {a, b} over ∑ = {a, b} and L2 = {aa, bb} over ∑ = {a, b}
Then L1.L2 = {aaa, abb, baa, bbb}
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
For instance, the familiar arithmetic algebra starts with constants such as
integers and real numbers, plus variables, and builds more complex
expressions with arithmetic operators such as + and x .
The algebra of regular expressions follows this pattern, using constants and
variaibles that denote languages, and operators for the three operations
union, dot, and star.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Prece
edence of Regu
ular‐Expression Operattors
Likee other alggebras, th
he regular‐‐expression
n operators have aan assumed
d
ord
der of "preecedence," which meeans that operators are assocciated withh
theiir operand
ds in a partticular ord
der.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
3. Finally, all unions ( + operators) are grouped with their operands. Since
union is also associative, it again matters li ttle in which order consecutive
unions are grouped, but we shall assume grouping from the left.
We have already shown that deterministic finite automata, and the two
kinds of nondeterministic finite automata ‐ with and without ε‐
transitions‐ accept the same class of languages. In order to show that
the regular expressions define the same class, we must show that:
Figure: shows all the equivalences we have proved or will prove. An arc from
class X to class Y means that we prove every language defined by class X is
also defined by classY. Since the graph is strongly connected (i.e., we can get
from each of the four nodes to any other node) we see that all four classes are
really the same.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
From
m DFA's
s tto Regullar
Expressio
ons:
Howwever, the paths are e allowed to pass th hrough only a limitedd subset ofo
the states. In
n an inducctive definiition of theese expressions, we start with h
the simplest expression
e ns that de‐‐ scribe patths that arre not allow
wed to passs
through any states (i.e.,
( they are single e nodes or single arcs), and d
indu
uctively build the expressioons that let the e paths ggo through h
progressively larger setss of statess.
Refe
er Classs Notes for Pro
oof.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
In ppart (a) we
w see how w to hand dle the exp pression E. The langguage of thhe
autoomaton iss easily see
en to be {ε}}, since thee only path
h from thee start statte
to aan acceptin
ng state is labeled E .
Finaally, part (c) gives the autom maton forr a regula ar expresssion a. Thhe
langguage of thhis automaaton evideently consissts of the one string a, which is
also
o L(a). It is
i easy to check
c that these automata all satisfy connditions (1),
(2),, and (3).
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
The fo
our cases are:
a
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Figure: Automata
A cconstructed for (0 + 1)*1(0 + 1)
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Lawss for Re
egular Expresssion:
Followiing are the algebraic laws
l for Reegular exprressions:
1. Asssociativity: Associativ
vity is the p
property off an operator that alloows to regrroup the
opeerands wheen the operator is app plied twice.
Exaample:
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
3. Identity: An identity for an operator is a value such that when the operator is
applied to the identity and some other value, the result is the other value.
Example:
0 is the identity for addition:
0 + X = X + 0 = X, here 0 is the identity.
4. Annihilators: An annihilator for an operator is a value such that when the operator
is applied to the annihilator and some other value, the result is the annihilator.
Example:
0 is the annihilator for multiplication.
0 * X = X * 0 = 0, here 0 is the annihilator.
5. Distributive Law: A distributive law involves two operators and asserts that one
operator can be pushed down to be applied to each argument of other operator
individually.
Example:
X*(Y+Z) = X*Y + Y*Z
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Problems:
Refer class Notes for below problems:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
6. Find a regular expression corresponding to each of the following subset of {0, 1}:
a. The language of all strings containing exactly two 0’s
b. The language of all strings that begin or end with 00 or 11.
c. The language of all strings containing both 11 and 010 as substrings.
(Dec‐06)
7. What is the language represented by the regular expression L((a U b)*a).
9. Write a regular expression for the set of all strings of 0’s and 1’s containing no
more than 2 consecutive 1’s.
11. Write a regular expression corresponding to each of the following subset of {a,
b}*:
a. Set of all strings having even number of a’s and no b’s.
b. Set of all strings that contain even number of a’s and b’s.
c. Set of all strings that contain odd number of a’s and b’s.
14. Define language such that it could contain atleast one double letter.
15. Define language such that it could contain no occurrence of a double letter.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Chapter 3
Proving languages to be non−regular
We have established that the class of languages known as the regular
languages has at least four different descriptions. They are the
languages accepted by DFA's, by NFA's, and by ε‐NFA's; they are also the
languages defined by regular expressions.
Let us consider the language L01 = {0n1nI n >= 1}. This language
contains all strings 01, 0011, 000111, and so on, that consist of one or
more 0's followed by an equal number of 1's. We claim that L01 is not
a regular language.
The intuitive argument is that if L01 were regular, then L01 would be the
language of some DFA A. This automaton has some particular number
of states, say k states. Imagine this automaton receiving k 0's as input. It
is in some state after consuming each of the k + 1 prefixes of the input:
0,0, 00, ... , 0k.
Since there are only k different states, the pigeonhole principle tells us
that after reading two different prefixes, say 0i and 0j, A must be in the
same state, say state q.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
That is, we can always find a nonempty stringy not too far from the
beginning of w that can be "pumped"; that is, repeating y any number of
times, or deleting it {the case k = 0) , keeps the resulting string in the
language L.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Applications
s of the Pumpiing Lem
mma:
Lett us See some
s exam how the pumping lemma iss used. In
mples of h
eacch case. we
w shall propose a langua age and use the pumpinng
lem
mma to prrove that the langu
uage is nott regular.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Closu
ure Prop
perties of
o Regullar Lang
guages:
In this sectioon, we sha all prove sseveral theorems off the form m "if certaiin
langguages aree regular, and
a a langguage L is formed from them m by certaiin
opeerations (ee.g., L is th
he union oof two reguular languaages), thenn L is also
reguular." Theese theore ems are offten calledd closure properties of the regula
ar
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
langguages, sin
nce they show that the class of regular languagees is close
ed
undder the opeeration mentioned.
Closu
ure of Regula
ar Lang
guages Under Boollean
Operations
Our ffirst closurre propertties are the threee boolean
n operatio
ons: union
n,
interseection, and
d complem
mentation:
1. Let Land M be b languag ges over alpphabet. Then
T L U M is the laanguage
thatt contains all strings
s that arc in either or
o both of L and M.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Hom
momorp
phism:
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Inve
erse Homomorrphism
m:
For Ex
xamples Refer cla
ass Notess.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Chapter 4
DFA Minimization
Decision Properties of Regular Languages:
In this section we consider how one answers important questions about
regular languages.
The typical language is infinite, so you cannot present the strings of the
language to someone and ask a question that requires them to inspect
the infinite set of strings.
However, for many of the questions we ask, algorithms exist only for the
class of regular languages. The same questions become "undecidable" (no
algorithm to answer them exists) when posed using more "expressive"
notations (i.e., notations that can be used to express a larger set of
languages) than the representations we have developed for the regular
languages. W
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
If so, the language is nonempty, while if the accepting states are all
separated from the start state, then the language is empty.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Testin
ng Memb
bership in a Reg
gular Lan
nguage
Thee next qu
uestion of importa nce is, giv
ven a string w and
d a regula
ar
lan guage L, is w in L. Whilee w is represente ed expliicitly, L is
i
rep
presented by an automaton o r regular expression.
If L is repressented by
y a DFA , tthe algorithm is siimple. Sim
mulate the
DFA A processing the sttring of in
nput symb
bols w, beg
ginning i n the starrt
statte.
If th
he DFA ennds in an acceptingg state, thhe answer is "yes" ; otherwise
thee answer is
i "no." Th
his algoritthm is ex xtremely fast. If |ww| = n, and
thee DFA is represente
r ed by a su uitable daata structure, such
h as a twoo‐
dimmensional array tha at is the ttransition
n table, then each transition
req me, and thee entire test takes O(n)
quires constant tim O time..
How
wever, if the
t repressentation is an NFA or ε‐ NF
FA, it is siimpler and
more efficient to sim
mulate thee NFA directly. Th
hat is, w we processs
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub bts Contact Ganesh Sir@83
3 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
symbols of w one at a time, maintaining the set of states the NFA can
be in after following any path labeled with that prefix of w.
If w is of length n, and the NFA has s states, then the running time of
this algorithm is O(ns2 ) . Each input symbol can be processed by
taking the previous set of states, which numbers at most s states, and
looking at the successors of each of these states.
First, we take the previous set of states and find their successors on
input symbol a.
Next, we compute the ε ‐ closure of this set of states. The initial set of
states for the simulation is the ε‐closure of the initial state of the NFA.
Minimization of DFA's
Another important consequence of the test for equivalence of states is that
we can "minimize" DFA 's. That is, for each DFA we can find an equivalent
DFA that has as few states as any DFA accepting the same language.
Moreover, except for our ability to call the states by whatever names we
choose, this minimum‐state DFA is unique for the language. The algorithm is
as follows:
1. First, eliminate any state that cannot be reached from the start state.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
2. Then , partition the remaining states into blocks, so tha.t all states in the
same block are equivalent, and no pair of states from different blocks are
equivalent. Theorem, below, shows that we can always make such a
partition.
After reading a string if FA resides in final state, it says that, the string is “accepted”
by FA else it says that, the string is “rejected”.
But if we need to produce some more precise information and not only ‘accept’ or
‘reject’; there are two different types of machines, which can be formulated as FA
with output, viz.
1. Moore Machine.
2. Mealy Machine.
1. Moore Machine:
It is the machine with finite number of states and for which, the output symbol at a
given time depends only upon the present state of the machine.
In Moore Machine, an output symbol is associated with each state. When the
machine is in a particular state, it produces the output, irrespective of what the input
on which the transition is made.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
For Moore Machine, If the length of the input sequence is n, then the length of the
output sequence is n+1.
2. Mealy Machine:
It is the machine with finite number of states and for which, the output symbol at a
given time is a function of the present input symbol as well as the present state of
the machine.
Thus, for this type of machine output depends on both current state and the current
input symbol.
For Mealy Machine, if the length of the input sequence is n, then the length of the
output sequence is n.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Problems:
2. Design Moore and Mealy machine for binary input sequence which produces an
output A if 101 are recognized otherwise output B.
3. Construct a Mealy Machine which can output EVEN and ODD according as the total
number of 1’s encountered is even or odd. The input symbols are 0 and 1.
(Nov‐2004)
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
4. Design Moore and Mealy Machine to find 1’s complement of given binary number.
(May‐2003)
6. Design Moore and Mealy Machine to find 2’s complement of given binary number.
(May‐2003, Dec‐2006, Dec‐2007,
June‐2008)
7. Design Moore and Mealy machine for a binary input string giving output as the
remainder when divided by 3.
10. Design a Moore Machine that will read sequences made up of letters a, e, I, o, u and
will give as output, same characters except when an ‘i’ is followed by ‘e’, it will be
changed to ‘u’.
13. Design Mealy Machine to convert each occurrence of substring add by aba over
∑={a,B}.
(Dec‐2006, Dec‐2000)
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
18. Design Moore and Mealy machines to convert substring 121 to 122 for strings of
languages having ∑= {0, 1, 2}
(Dec‐2002)
23. Explain the equivalence of Moore and Mealy Machine. Design a Mealy Machine for
the language (0+1)*(00+11) and convert this Mealy machine to Moore Machine.
(May‐2005)
24. Design a Moore Machine which counts the occurrence of substring aab in a long
input string over {a,b}
(Nov‐2004)
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Chapter 5
Context−free Grammars
For building any model, we should consider two aspects of the given grammar:
1. The generative capacity of the grammar i.e., the grammar used should
generate all and only the sentence of the language for which it is written.
2. Grammatical constituents like terminals and non‐terminals.
Definition: Grammar is used for specifying the syntax of a language and for
representing a non‐regular language.
1. A parse structure grammar is denoted by a quadruple of the form
G = (V, T, P, S)
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Where,
V – is a set of Variables.
T – is a set of Terminals.
P – is a set of Productions.
S – is a special variable called that start symbol S Є V.
Notations:
Variables: Variables are those symbols that take part in the derivation of a sentence,
but are not the part of derived sentence.
Terminals: Terminals are those symbols that are the part of derived sentence.
A string can be derived from start symbol of the grammar, using the productions of
the grammar.
1. Sentential Form OR
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Sentential Form:
Example:
S A1B (Start Variable)
S 0A1B (using A 0A)
S 00A1B (using A 0A)
S 001B (using A ɛ)
S 0010B (using B 0B)
S 00101B (using B 1B)
S 00101 (using B ɛ)
Example:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
A set of derivations applied to generate a word can be represented using a tree. Such a
tree is known as a parse tree. A parse tree is constructed with the following condition:
Problems:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
A SbA | SS | ba
Derive ‘aabaaabbaaa’ using the leftmost derivation and rightmost derivation.
Derive ‘aabbaa’ using the leftmost derivation and rightmost derivation.
OR
Definition: A Grammar is said to be CFG if all the production are of the form:
Aα
Where,
A ‐ is a variable and
α – is sentential form. (Sentential forms means, the combination of Variables and
Terminals).
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Problems:
University questions will be solved in Class:
1. Let G = (V, T, P, S) be the CFG having following set of productions. Derive the string
‘aabbaa’ using leftmost derivation and rightmost derivation.
S aAS | a
A SbA | SS |ba
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Example:
Let the language L1 and L2 are given as below:
L1 = {an | n>0}
L2 = {bn | n>0}
S S1S2
Example:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Problems:
University questions will be solved in Class:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
9. Give CFG for all strings with at least two 0’s, ∑ = {0, 1}
10. Give CFG for set of odd length strings in {0, 1}* with middle symbol ‘1’.
11. Give CFG for set of even length strings in {a, b, c, d}* with two middle symbol equal.
12. Give CFG for L = { x | x contains equal number of a’s and b’s}
Ambiguous Grammar:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
EE+E|a|b
E E
E + E E + E
a E E
+ a
E + E
a
b
a b
(a)
(b)
Fig: Parse Trees considered for ambiguity
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
The grammar generates (a + b + a) in two different ways. The two derivations are
shown in Fig (a) and (b).
The first derivation Fig (a) says that (b + a) is evaluated first and then the evaluated
value is added to a. Thus the right side + operator gets a precedence over the left
side + operator. The expression (a + b + a) is treated as (a + (b + a)).
The second derivation Fig (b) says that (a + b) is evaluated first and then the
evaluated value is added to a. Thus the left side + operator gets a precedence over
the right side + operator. The expression (a + b + a) is treated as ((a +b )+ a).
Removing ambiguity:
There is no general rule for removing ambiguity from CFG. Removing ambiguity
from a grammar involves rewriting of grammar so that there is only one derivation
tree for every string belonging to L(G) i.e., language generated by grammar G.
EE+E|a|b
Can be removed by strictly assigning higher precedence to left side + operator over
right side + operator. This will mean evaluation of an expression of the form (a + b +
a), from left to right.
This property can be incorporated in the grammar itself by suitably modifying the
grammar.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Parse tree in Fig. given below is based on unambiguous grammar for the string (a + b
+ a).
E + T
a
T + T
a b
Problems:
University questions will be solved in Class:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
2. Show that the CFG given below. Which generates all strings of balanced parentheses
is ambiguous. Give an equivalent unambiguous grammar.
S SS | (S) | ɛ
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
8. In each case, show that the grammar is ambiguous, and find the equivalent
unambiguous grammar:
i. S SS| a|b
ii. S ABA, A aA |ɛ , B bB |ɛ
iii. S aSb | aaSb |ɛ
Simplification of CFG:
A grammar written in a simple form is easy to analyse. Certain restrictions are
imposed on simplified grammar.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
S Aa | Bb | a | b
A Aa | a
B bB
B bB
and it cannot generate a string of terminals. The Grammar can be simplified by deleting
every production containing the useless symbol B. A simplified grammar is given as:
S Aa | a | b
A Aa | a
There are two rules for finding a set of generating symbols for the given grammar.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Non‐reachable Symbols:
A symbol X is reachable if it can be reached from the start symbol S. i.e. if:
*
S α
G
and α contains a variable X then X is reachable.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Non‐reachable symbols can be located with the help of a dependently graph. A variable
X is said to be dependent on S if there is a production:
S α1 X α2
We must draw a dependency graph for all productions. If there is no path from the start
symbol S to a variable X, then X is non‐reachable.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
S aS | AB
A bA
B AA
2. Elimination of ɛ‐productions:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Bɛ
A production of the form A B is known as the unit production where A and B are
variables.
For every context free grammar G with unit productions, we can find a context free
grammar G1 having no unit productions such that
L(G1) = L(G)
Ai * Aj
G
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
STEP 3: From pairs constructed in step 2, we can construct a chain like A1 A2…
Aj α is a non‐unit production.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
DE
Ea
Productions in G, satisfying certain restrictions are said to be in normal form. There are
two normal forms for CFG.
1. Chomsky Normal Form (CNF)
2. Greibach Normal Form (GNF)
1. A BC, where A, B, C Є V.
2. A a, where A Є V and a Є T.
The grammar should have no useless symbols. Every CFG without ɛ‐productions can be
converted into an equivalent CNF form.
1. Eliminate ɛ‐productions, unit productions and useless symbols from the grammar.
2. Every variable deriving a string of length 2 or more should consist only of variables.
i.e. every production of the form A α with | α| ≥ 2, α should consist only of
variables.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Ca a and
Cb b
A X1C1
C1 X2C2
C2 X3C3
:
Cn‐2 Xn‐1Xn
each with two variables on the right.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
S bA | aB
A bAA | aS | a
B aBB | bS | b
S Aba
S aab
B Ac
S ASB | ɛ
A AaS | a
B SbS | A | bb
A aα
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
A Aα …[Left recursive ]
A Aα
A Aαα … using A Aα
A Aααα … using A Aα
A Aαn … using A Aα
A βαn … using A β
Right recursive grammar for βαn: A right recursive grammar for βαn can be
written as:
A βB | β
B αB | α
A Aα | β
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
A βB | β
B αB | α
1. A Aa | b
2. A Aa | b | c
3. A ABC | BC
4. A ABC | DA | EC
5. S S10 | 0
1. Eliminate ɛ‐productions, unit productions and useless symbols from the grammar.
2. In production of the form A X1X2…Xi…Xn, other than X1, every other symbol should
be a variable. X1 could be a terminal.
Example: consider a production
A V1V2aV3bV4 as
A V1V2CaV3CbV4 and adding two productions
Ca a and
Cb b
Aα
Aa
A aα
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
S aXSY | YSX | b
The variables S, X and Y can be renamed as A1, A2 and A3 respectively. Then the becomes
A1 aA1A2A3 | A1A2A3| b
4. Modify the productions to ensure that if there is a production Ai > Ajα then I should
be ≤j. If there is a production Ai Ajα with i>j, then we must generate productions
substituting for Aj.
5. Repeating step 4, several times will guarantee that for every production Ai Ajα,
i≤j.
6. Remove left recursion from every production of the form Ak Akα. B‐productions
should be added to remove left recursion.
7. Modify Ai‐production to the form Ai aα, where ‘a’ is a terminal and α is a string of
non‐terminals.
8. Modify Bi‐productions to the form Bi aα, where ‘a’ is a terminal l and α is a string
of non‐terminals.
S AA | a
A SS | b
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
S AB
A BS | b
B SA | a
S BA | ab
B AB | a
A Bb | BB
S SS | aSb | ab
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Y 1X1
11. Convert the following CFG to GNF (Dec‐
2005)
S aSa | bSb | c
Regular Grammar
Definition: The language accepted by finite automata can be described using a set of
productions known as regular grammar. The productions of a regular grammar are of
the following form:
Aa
A aB
A Ba
Aɛ
Where, a Є T and A, B Є V.
1. Right‐linear form
2. Left‐linear form
Right‐Linear Form: A right linear regular grammar will have production of the given
form.
Aa
A aB
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Aɛ
Left‐Linear Form: A left linear regular grammar will have productions of the following
form:
Aa
A Ba
Aɛ
Every DFA can be described using a set of production using the following steps:
a
B C
a
B C
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Following steps are required to write a left linear grammar corresponding to a DFA.
1. Interchange starting state and the final state.
2. Reverse the direction of all the transitions.
3. Write the grammar from the transition graph in left‐linear form.
a
A B
a
A B
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
b
A F
Every left linear grammar can be represented using an equivalent DFA. Following steps
are required to draw a DFA for a given left linear grammar.
Transition Graph
Every right linear grammar can be represented by an equivalent left linear grammar.
The conversion process involves drawing of an intermediate transition graph. Following
steps are required:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
1. Represent the right grammar using a transition graph. Mark the final state as
2. Interchange the start and the final state.
3. Reverse the direction of all transitions.
4. Write left – linear grammar from the transition graph.
Transition Graph
Every left linear grammar can be represented by an equivalent right linear grammar.
The conversion process involves drawing of an intermediate transition graph. Following
steps are required:
1. Represent the left grammar using a transition graph. Mark the final state as ɛ
2. Interchange the start and the final state.
3. Reverse the direction of all transitions.
4. Write right – linear grammar from the transition graph.
Problems:
University questions will be solved in Class:
1. Construct right linear grammar and left linear grammar for the language (ba*).
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
B aB
Ca
Bb
4. Final the equivalent DFA accepting the regular language defined by the right linear
grammar given as:
S aA | bB
A aA | bc | a
B aB | b
C bB
5. Construct DFA accepting the regular language generated by the left linear grammar
given below:
S Ca | Bb
C Bb
B Ba | b
6. Construct DFA accepting the language generated by the left linear grammar given
below:
S B1 | A0 | C0
B B1 | 1
A A1 | B1 | C0 | 0
C A0
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
8. Write an equivalent left linear grammar from the given right linear grammar:
S 0A | 1B
A 0C | 1A | 0
B 1B | 1A | 1
C 0 | 0A
9. For right linear grammar given below, obtain an equivalent left linear grammar:
S 10A | 01
A 00A | 1
10. Write an equivalent right linear grammar from the given left linear grammar:
S C0 |A0 | B1
A A1 | C0 | B1 | 0
B B1 | 1
C A0
11. Construct the right linear grammar corresponding to the regular expression:
R = (0+1)1*(1+(01)*)
12. Write an equivalent right recursive grammar for the given left recursive grammar:
S S10 | 0
13. Construct the right linear grammar corresponding to the regular expression:
R = (1+(01)*)1*(0+1)
14. Draw NFA accepting the language generated by grammar with productions:
S abA | bB | aba
A b | aB | bA
B aB | aA
15. Construct right linear and left linear grammar for the language:
L = {anbm, n≥2, m≥3}
16. Construct left linear and right linear grammar for the language:
0*(1(0+1))*
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
17. Construct left linear and right linear grammar for the language:
(0+1)*00(0+1)*
18. Construct left linear and right linear grammar for the language:
(((01 + 10)*11)*00)*
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Chapter 6
Pushdown Automata (PDA)
The context‐free languages have a type of automaton that defines them. This
automaton, called a "pushdown automaton,'' is an extension of the
nondeterministic finite automaton with ε‐transitions, which is one of the ways to
define the regular languages.
The pushdown automaton is essentially an ε‐NFA with the addition of a stack. The
stack can be read, pushed, and popped only at the top, just like the "stack" data
structure.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
PDA is more powerful than FA. A context‐free language (CFL) can be recognized by a
PDA. Only a subset of CFL that are regular can be recognized by finite automata.
Example: A string of the form anbn cannot be handled by a finite automaton. But the
same can be handled by a PDA.
1. Any machine recognizing a string of the form anbn, must keep track of a’s as number
of b’s must be equal to the number of a’s.
2. First half of the string can be remembered through a stack.
a a a a a b b b b b Input
Finite State
a
Control
a
a
a
a
3. As the machine reads the first half of anbn, it remembers it by pushing it on top of the
stack. As shown in Fig. after reading first 5 a’s, the stack contains 5 b’s.
4. While reading the second half of the input string consisting of b’s, the machine pops
out an ‘a’ from the stack for every ‘b’ as input.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
5. After reading 5 b’s, input will finish and the stack will become empty. This will
indicate that the input string is of the form anbn.
7. While in state q0 an input ‘a’ is not allowed and hence there is a need for two states.
9. A transition in PDA can be shown as a directed edge from the state qi to qj. While
moving to state qj, the machine can also perform stack operation. A transition edge
from qi to qj should be marked with current input, current stack symbol and the
stack operation. It is shown in below fig:
11. The language { anbn | n >= 1} can be accepted by the PDA of Fig:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
(b, a)
a, any Pop(a)
Pop(a) (b, a)
Pop(a)
q0 q1
12. The state q0 will keep track of the number of a’s in an input string, by pushing
symbol ‘a’ onto the stack for each input ‘a’. A second state q1 is used to pop an ‘a’
from the stack for each input symbol ‘b’. Finally, after consuming the entire input the
will become empty.
PDA consists of finite set of states, one initial and one or more final states.
After the input symbol is read the machine can remain in the same state or
change the state, at the same time it can push a symbol on the stack or pop the
top most symbol or perform neither push nor pop.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
A PDA is a 7‐tuple
P =(Q, ∑, Γ, δ, q₀, Z₀, F)
where,
Γ: A finite stack alphabet. This component, which has no finite‐automaton analog, is the
set of symbols that we are allowed to push onto the stack.
δ: The transition function. As for a finite automaton, δ governs the behavior of the
automaton. Formally, δ takes as argument a triple δ (q,a, X), where:
1. q is a state in Q.
2. a is either an input symbol in Σ or a = ε, the empty string, which is assumed not
to be an input symbol.
3. X is a stack symbol, that is, a member of Γ.
q0: The start state. The PDA is in this state before making any transitions.
Z0: The start symbol. Initially, the PDA's stack consists of one instance of this
symbol, and nothing else.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CH
HOP
PRA
A AC
CAD
DEM
MY
@Ban
ndra:986
67 843 85
54 @Thanne:9867 843
8 853 @Vashi
@ :9
9867 8433 852
For Doub
bts Conta
act Ganes
sh Sir@83
3 84 82 05
0 75
Thee only thingg that the diagram d hich stack ssymbol is the start
does not teell us is wh
sym
mbol. Conventionally, it is Z0 , un
nless we ind
dicate otheerwise.
CH
HO
OPR
RA ACA
A ADE
EMY
Y
@Ban
ndra:986
67 843 85
54 @Than
ne:9867 843
8 853 @Vashi
@ :9
9867 843
3 852
For
F Doub ts Contac
ct Ganesh Sir@83
3 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
An ID is (q, x, α)
q Є Q, x Є ∑* α Є Γ*
say (q, a₁a₂…an, z₁z₂…zm) is an ID
This describes the PDA when the current state is q, the input string to be processed is
a₁a₂…an and the pds has z₁z₂…zm with z₁ at the top and zm lowest.
Definition:
Let P be a PDA
A move relation (denoted by |‐) between ID’s is defined as
(q, a₁a₂…an, z₁z₂…zm) |‐ (q’, a₂, … an β z₂ …zn)
i .e. PDA in state of with z₁z₂…zm in PDS (z₁ is at the top) reads the input symbol a₁, the
PDA moves to state q’ and write β on the top of z₂, …zm. After this transition, the input
string to be processed a₂a₃,…an.
Example:
P =({q₀, q₁, q₂}, {a, b, c}, {a, b, z₀}, δ, q₀, z₀, {q₂})
δ:
δ(q₀, a, z₀) = {(q₀, az₀)} δ(q₀, a, q) = {(q₀, aa)}
δ(q₀, a, b) = {(q₀, ab)} δ(q₀, b, z₀) = {(q₀, bz₀)}
δ(q₀, b, a) = {(q₀, ba)} δ(q₀, b, b) = {(q₀, bb)}
δ(q₀, c, a) = {(q₁, a)} δ(q₀, c, b) = {(q₁, b)}
δ(q₀, c, z₀) = {(q₁, z₀)} δ(q₁, a, a) = {(q₁, Є)}
δ(q₁, b, b) = {(q₁, Є)} δ(q₁, Є, z₀) = {(q₂, z₀)}
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Acceptance by PDA:
W Є ∑* | ‐ (q₀,w,z₀) |‐ (qf, Є, α)
qf Є F α Є Γ*
Problems:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
8. Design a PDA which accepts the strings containing equal no. of a’s and b’s.
(Dec‐2002, May‐2003, May‐2004)
15. Design DPDA to accept strings with more a’s than b’s.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
18. Design the PDA to accept the language containing all odd length palindromes over ∑
= {0, 1} (Dec‐2007)
There is a general relation between Context – free Languages and NPDA. In the
following section we see that for every context – free language there is a NPDA that
accepts it and conversely, that the language accepted by any NPDA is context – free.
Theorem:
For any context – free language L, there exists an NPDA M such that
L = L(M)
Proof:
If L is Є – free context free language, there exists a CFG in Greibach Normal Form for it.
Let G = (V, T, S, P) be such a grammar. We then construct an NPDA which simulates
leftmost derivations in this grammar. As suggested, the simulation will be done so that
the unprocessed part of the sentential form is in the stack, while the terminal prefix of
any sentential form matches the corresponding prefix of the input string. Specifically,
the NPDA will be
where z Є V. Note that the input alphabet of M is identical with the set of terminals of G
and that the stack alphabet contains the set of variables of the grammar.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
so that after the first move of M, the stack contains the start symbol S of the derivation.
(the stack start symbol z is a marker to allow us to detect the end of the derivation.). In
addition, the set of transition rules is such that
(q₁, u) Є (q₁, a, A)
Whenever
A → au
is in p. This reads input a and remaining variable A from the stack replacing it with u. In
this way it generates the transitions that allow the PDA to simulate all derivations.
Finally, we have
δ(q₁, Є, z) = {(q, z)}
to get M into a final state.
To show that M accepts any w Є L(G), consider the partial leftmost derivation
S=> a₁a₂…anA₁A₂…Am
=> a₁a₂… anbB₁B2… BkA2… Am.
If M is to simulate this derivation, then after reading a₁a₂…an, the stack must contain
A₁A₂…Am. To take the next step in the derivation, G must have a production
A₁ → bB₁B₂…Bk
But the construction is such that then M has a transition rule in which
δ(q₁,B₁…Bk) Є δ(q₁, b, A₁),
so that the stack now contains B₁…BkA2…Am after having read a1a2…an.
A simple induction argument on the number of steps in the derivation shows that if
S => w,
Then (q₁, w, Sz) |‐(q₁, Є, z),
Now we have,
(q₀, w, z) |‐ (q₁,w, Sz) |‐ (q₁, Є, z) |‐ (qf, Є, z),
So that L(G) C_ L(M).
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
But there is only one way to get from q₀ to q₁ and only one way from q₁ to qf. Therefore
must have
(q₁, w, Sz) |‐ (q₁, Є, z),
But then the grammar has a rule of the form S → a₁u₁, so that
S => a₁u₁.
Repeating this, writing u₁ = Au₂z) |‐ (q₁ ,a3…an,u3u2z)
(q₁,a₂a3…an, Au₂z) |‐ (q₁, a3…an, u3u2z).
Implying that A a₂u3 is in the grammar and that
S a₁a₂u3u2.
This makes it quite clear at any point the stack contents are identical with the
unmatched part of the sentential form, so that
S a₁a₂a3... an.
In the consequence, L(M) subset of L(G), completing the proof if the language does not
contain ɛ.
Problems:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
B 0S | 1S | 0
Test whether 010000 is in the language.
7. Write CFG for language having number of a’s greater than number of b’s and Design
a PDA for the same.
(Dec‐2009)
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Chapter 7
Turing machine
Turing Machine:
Turing Machine is a simple mathematical model of a general purpose computer.
Turing machine models the computing power of a computer i.e. the Turing machine
is capable of performing any calculation which can be performed by any computing
machine.
It has one tape which is divided into number of cells. Each cell can store one symbol.
The input to and the output from the Finite Automata are affected by the read/write
head which can examine one cell at a time.
In one move, the machine examines the present symbol under the read/write head
on the tape and the present state of an automation to determine:
- A new symbol to be written on the tape in the cell under the read/write head.
- A motion of the read/write head along the tape i.e. either the head moves one
cell left(L), one cell right (R) or stay at the same cell (S)
- The next state of machine.
M = (Q, Σ, Ѓ, δ, qo, B, F)
Where,
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
δ‐ transition function mapping the state of finite automaton and tape symbols to
states, tape symbols and movement of head
δ : Q × Г →Q × Ѓ × { L, R, S}
Instantaneous Descriptions:
Snapshots of a Turing machine in action can be used to describe a Turing Machine.
These give instantaneous descriptions of a Turing Machine.
An ID of Turing Machine is defined in terms entire input string and the current state.
Definition:
The first symbol of γ is the current symbol a under read/write head and γ has
all the subsequent symbols of the input string.
The substring α of the input string formed by all the symbols to the left of a.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Note: The description of moves by IDs is very much useful to represent the processing
of input strings.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Problems:
University questions will be solved in Class:
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
7. Design a Turing machine which recognizes the language having equal number of a’s
and b’s
OR
(Dec‐2006, Jun‐2008)
8. Design a TM to accept a language over {a, b} such that the number of a’s > number of
b’s.
(May‐2007)
9. Construct Turing Machine that will accept the language L over Σ = { a, b} where L = {
w: │w│is even }
10. Construct Turing Machine that will accept the language L over Σ = { a, b} where L = {
w: │w│is a multiple of 3 }
(May‐2006)
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
12. Design TM to recognized string containing even number of a’s and odd number of b’s
over Σ = {a, b}
14. Design TM that can accept set of all even palindromes over alphabet (0, 1).
(May‐2006)
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
m–n m >n
m≤n 0
5. Design T.M. to perform division and find the quotient and remainder.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
o/p “C”
19. Construct a Turing Machine that compares 2 numbers m and n and leaves at the end
x on the tape where x = g/l/e depending on whether m>n / m<n/ m=n respectively.
0/p “C”
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
22. Design a TM for generating 2n where n is binary. The result should also be binary.
(May‐2004, Jun‐2007, Dec‐2007)
In other words, the UTM should have the capability of imitating any Turing machine
‘T’ given the following information in its tape:
The description of ‘T’ in terms if its operation or program area of the tape (i.e. the
transaction table).
The initial configuration of the TM i.e. starting state or the current state and the
symbol scanned. The processing data to be fed to ‘T’ (data area of the tape).
This obviously means that the UTM should have an algorithm to interpret correctly
the rules of operation given about the TM ‘T’.
The behavior of the UTM is simple, namely, simulating ‘T’ one step at a time as
follows:
- A marker to indicate the point at which the description of ‘T’ begins, and it
keeps a complete account of how the tape of ‘T’ looks likes at every instant
guides it.
- Also, it remembers the state ‘T’ is in, and the symbol ‘T’ is reading. Then it
simply looks at the description of ‘T; to carry out what ‘T’ is supposed to do.
- In order to exhibit this behavior, the UTM should have a lookup facility and
should perform the following steps:
Step 1: Scan the square on the state area of the tape and read the symbol that ‘T’ reads
and initial state of ‘T’.
Step 2: Find the triplet which corresponds to the initial state and the input symbol
read in step1.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Step 3: Move the tape to reach the appropriate square in the data area, replace the
symbol, move the tape in the required direction, read the next symbol and finally reach
state area and replace the state and scanned symbols. Goto step 1.
- As it name implies, the tape is infinite to the left as well as to the right.
- We imagine that there is infinity of blank cells to the left and the right of the
current nonblank portion of the tape.
- L is recognized by a Turing Machine with a two‐ way infinite tape if and only if it
is recognized by a Turing Machine with a one‐way infinite tape.
A multitape Turing machine consists of a finite control with k tape heads and k
tapes; each tape is infinite in both directions.
On a single move, depending on the state of the finite control and the symbol by each
of the tape heads, the machine can:
- Change State
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
- Print a new symbol on each of the cells scanned by its tape heads.
- Move each of its tape head, independently, one cell to the left ir right, or keep it
stationary.
A nondeterministic Turing Machine is a device with a finite control and a single one‐
way infinite tape.
For a given state and tape symbol scanned by the tape head, the machine has a finite
number of choices for the next move. Each choice consists of a
- New state
- Tape Symbol to print
- Direction of head motion
It is a device having a finite control and the tape consists of a k‐dimensional array of
cells infinite in all 2k directions for some fixed k. Depending on the state and the
symbol scanned, the device
- Changes state
- Prints a new symbol
- Move the tape head in one of 2k directions either positively or negatively along
with one of the k axes.
A k‐head Turing Machine has fixed number, k, of heads. The heads are numbered 1
through k, and a move of the TM depends on the state and on the symbol scanned by
each head.
In one move, the heads may each move independently left, right or remain
stationary.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
6. Composite T.M.:
The idea of composite TM give rise to the concept of breaking the complicated job
into number of jobs implementing each separately and then combining them
together to get answer for the job required to be done.
7. Iterated T.M.:
Till now we have allowed the tape head of TM to move either left or right from its
initial position.
It is only necessary that the TM’s head be allowed to move within the positions at
and to the right of the initial head position.
In TM with Semi‐infinite tapes there are no cells to the left of the initial position.
The machine starting at this configuration will halt after a finite number of steps.
The machine starting at this configuration never no matter how loge it runs.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Given any TM, problem of determining whether it halts ever or not, is called as
halting problem.
To solve the halting problem, we should have some mechanism to which given any
functional matrix, input data type and initial configuration of the TM for which we want
to detect, determines whether the process will ever halt or not.
Note: In reality, one cannot solve the halting problem. The halting problem is
unsolvable. That means there exists no TM, which can determine whether a given
program including itself, will ever halt, or not.
Proof:‐
1. Let us prove the halting problem by contradiction. Suppose that there exists a TM ‘A’
which decides whether or not any computation by a TM ‘T’ will ever halt, given the
description ‘dT’ of ‘T’ and the tape ‘t’ of ‘T’. Then for every input (t, dT) to ‘A’, if ‘T’
halts for the input ‘t’, ‘A’ reaches an “accept halt”;
2. If ‘T’ does not halt for the input ‘t’, then ‘A’ reaches an “reject halt”. We can now
construct another TM ‘B’ which takes ‘dT’ as the input and proceed as follows:
- First it copies the input ‘dT’ and duplicates ‘dT’ on its input tape and then
takes this duplicated information tape as the input to ‘A’ with one
modification namely, whenever ‘A’ is supposed to reach an “accept halt”, ‘B’
will loop forever.
3. Considering the original behavior of ‘A’, we find that ‘B’ acts as follows. It loops if ‘T’
halts for input t = dT and halts if ‘T’ does not halt for the input t = dT.
4. Since ‘B’ itself is a TM, let us set T=B. Thus replacing ‘T’ by ‘B’ we get that, ‘B’ halts for
the input ‘dB’ if and only if ‘B’ halts for the input ‘dB’. This is a contradiction.
5. Hence we conclude that machine ‘A’ which can decide whether any other TM will
ever halt, does not exist. Therefore, halting problem is unsolvable.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
1. We cannot decide whether a TM ever prints a given symbol of its alphabet. This is
also unsolvable.
2. Two TM’s with the same alphabet cannot be checked for equivalence or
inequivalence by an algorithm; i.e. there is no effective general way to decide
whether a given computational process will ever terminate or whether two given
processes are equivalent. This is also another unsolvable problem.
3. Blank‐tape theorem: There exists a TM which when started on a Blank tape, can
write its own description. This is of interest in constructing self‐reproducing
machine.
Turing Machine and Computers:
Simulating a TM by Computer:
2. Finite Control: since there are only a finite number of states as character strings and
use a table of transition, which it looks up to determine each move.
3. Machine Tape:
Zip disks or removable hard disks can simulate infinite machine tape.
We can arrange the disks placed in two stacks: One stack holds the data in
cells of the left of the tape head, and other stack holds the data significantly to
the right of the tape head.
The following diagram shows how the TM would be designed to simulate a computer.
2. The second tape holds the memory locations on tape1. The value stored in this
location will be interpreted as the next computer instruction to be executed.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
3. The third tape holds the memory address or the contents of that address after that
address has been located on tape1. To execute an instruction, the TM must find the
contents of memory address that hold data involved in the computation.
4. The fourth tape holds the simulated input to the computer, since the computer must
read its input from a file.
CHOPRA ACADEMY
@Bandra:9867 843 854 @Thane:9867 843 853 @Vashi :9867 843 852
For Doubts Contact Ganesh Sir@83 84 82 05 75
Chapter 8
Intractable Problems
We now bring our discussion of what can or cannot be computed down to the level
of efficient versus inefficient computation.
We focus on problems that are decidable, and ask which of them can be computed by
Turing machines that run in an amount of time that is polynomial in the size of the
input.
1. The problems solvable in polynomial time on a typical computer are exactly the
same as the problems solvable in polynomial time on a Turing machine.
2. Experience has shown that the dividing line between problems that can be
solved in polynomial time and those that require exponential time or more is
quite fundamental.
In this chapter we introduce the theory of "intractability," that is, techniques for
showing problems not to be solvable in polynomial time.
Since we are dealing with whether problems can be solved in polynomial time, our
notion of a reduction must change.
The algorithm itself must take at most polynomial time, or the reduction does not let
us conclude that the target problem is intractable, even if the source problem is.
There is another important distinction between the kinds of conclusions we drew in
the theory of undecidability and those that intractability theory lets us draw.
1. P denotes the class of problems, for each of which, there is at least one known
polynomial time deterministic TM solving it.
2. NP denotes the class of all problems, for each of which, there is at least one
known non‐deterministic polynomial time solution. However, this solution may
not be reducible to a polynomial time deterministic TM.
7. A problem which does not have any polynomial time algorithm is called an
intractable problem, otherwise it is called tractable.
NP‐Complete Problems:
1. A problem is NP‐complete if it is in NP and for which no polynomial time
deterministic TM solution is known so far.
4. Each of these problems can be solved by at least on Non‐deterministic TM, the time
complexity of which is a polynomial function of the size of the problem.
5. A problem from the class NP, can be defined as one for which a potential solution, if
given, can be verified in polynomial time whether the potential solution is actually a
solution or not.
4. The truth value of a Boolean expression depends on the truth values of its variables.
NP‐Completeness:
1. Polynomial‐time reduction plays an important role in defining NP‐completeness. A
polynomial‐time reduction is a polynomial‐time algorithm which constructs
instances of a problem P2 from the instances of some other problem P1.
4. NP‐Hard Problem:
a. A problem L is said to be NP‐Hard if for any problem L1 in NP, there is a polynomial‐
time reduction of L1 to L.
b. In other words, a problem is NP‐Hard if:
i. Establishing L as an NP‐class problem is so far not possible.
ii. For any problem L1 in NP, there is polynomial time reduction of L1 to L.
c. Every NP‐Complete problem must be NP‐Hard problem.
2. A Turing machine can use random numbers in its calculation. A Turing machine with such a
capability is known as randomized Turing machine.
3. There are some similarities between a randomized TM and non‐deterministic TM. The non‐
deterministic choice of a NDTM could be based on random number.
4. The class of languages accepted by a randomized TM can be divided into two categories.
a. The class RP
b. The class ZPP
3. Algorithms for primality testing are based on the following two theorems:
a. If ‘n’ is a prime, then a(n‐1) = 1 modulo P ‐‐‐‐‐‐‐‐‐‐‐‐‐ Fermat’s theorem
b. The equation X2 = 1 (modulo n) has exactly two solutions namely 1 and n‐1, if n is a
prime.
4. An algorithm based on Fermat’s theorem can be written with time complexity of O(n3).
Questions: