0% found this document useful (0 votes)
77 views

Notes Compiled by Prof.: Ganesh Sir: Sem - V (COMP)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Notes Compiled by Prof.: Ganesh Sir: Sem - V (COMP)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 146

{ For Private Circulation-11 }

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

About GANESH SIR:


Sir has completed his B.E(Comp) from Mumbai
university, M.E (Comp-IT) from Pune University.
He was working for Infosys and Oracle for 3
years. Currently he is pursuing his Ph.D in
Theory of Computer Science (TCS). He has
expertise in Model checking and Formal Method,
which are the advance part of TCS.

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.

1 Finite Alphabets, Strings and Languages, automata and


Automata/Finite Grammars. Finite Automata (FA) −its behaviour; DFA −
State Machine Formal definition, simplified notations (state transition
diagram, transition table), Language of a DFA.
NFA−Formal de inition, Language of an NFA. An
Application: Text Search, FA with epsilon−transitions,
Eliminating epsilon−transitions, Eliminating
epsilon−transitions, Equivalence of DFAs and NFAs.

2 Regular Definition, FA and RE, RE to FA, FA to RE, algebraic laws


expressions (RE) for RE, applications of REs, Regular grammars and FA,
FA for regular grammar, Regular grammar for FA.

3 Proving languages Pumping Lemma, and its applications. Some closure


to be non−regular properties of

Regular languages − Closure under Boolean operations,


reversal homomorphism, inverse homomorphism, etc.
Mhill−Nerode Theorem.

4 DFA Minimization Some decision properties of Regular languages −


emptiness, finiteness, membership, equivalence of two
DFAs or REs, Finite automata with output.

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 Context−free Formal definition, sentential forms, leftmost and


Grammars (CFGs) rightmost derivations, the language of a CFG. Derivation
tree or Parse tree−Definition, Relationship between
parse trees and derivations.

Parsing and ambiguity, Applications of CFGs, Ambiguity


in grammars and Languages. Simplification of CFGs −
Removing useless symbols, epsilon−Productions, and
unit productions, Normal forms −CNF and GNF. Proving
that some languages are not context free −Pumping
lemma for CFLs, applications. Some closure

Properties of CFLs − Closure under union,


concatenation, Kleene closure, substitution, Inverse
homomorphism, reversal, intersection with regular set,
etc. Some more decision properties of CFLs, Review of
some undecidable CFL problems.

6 Pushdown Formal definition, behaviour and graphical notation,


Automata (PDA) Instantaneous descriptions (Ids), The language of PDA
(acceptance by final state and empty stack). Equivalence
of acceptance by final state and empty stack,
Equivalence of PDAs and CFGs, CFG to PDA, PDA to CFG.
DPDAs − De inition, DPDAs and Regular Languages,
DPDAs, Multistack DPDAs & NPDAs and CFLs.
Languages of DPDAs, NPDAs, and ambiguous grammars.

7 Turing Machines Formal definition and behaviour, Transition diagrams,


TM Language of a TM, TM as accepters deciding and
generators. TM as a computer of integer functions,
Design of TMs, Programming techniques for TMs −
Storage in state, multiple tracks, subroutines, etc.
Universal TMs, Variants of TMs − Multitape TMs,

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

Nondeterministic TMs. TMs with semi−in inite tapes,


Multistack machines, Simulating TM by computer,
Simulating a Computer by a TM, Equivalence of the
various variants with the basic model. Recursive and
recursively enumerable languages, Properties of
recursive and recursively enumerable languages, A
language that is non-recursively enumerable (the
diagonalization language). The universal language,

Undecidability of the universal language, The Halting


problem, Rice’s Theorem, Greibach Theorem, Post’s
Correspondence Problem (PCP) − De inition,
Undecidability of PCP. Context sensitive language and
linear bounded automata. Chomsky hierarchy.

8 Intractable The classes P and NP, an NP−complete problem, A


Problems Restricted Satisfiability problem, Additional
NP−complete problems, Complements of languages in
NP, Problems Solvable in polynomial space, A problem
that is complete for PS, Language Classes based on
randomization, The complexity of primality testing.

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

Subject Taken by Ganesh Sir:


Semester Subject Batch
SEM‐VI SPCC (System Vacation +
Programming and
(Computer) Compiler Construction)
Regular

ALL OCAJP 1.7 Vacation +


(Oracle Certified
Associate JAVA
Regular
Programmer)

ALL OCPJP 1.7 Vacation +


(Oracle Certified
Professional JAVA
Regular
Programmer)

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."

 Abstract machine ‐ A procedure for executing a set of instructions in some formal


language, possibly also taking in input data and producing output. Such abstract
machines are not intended to be constructed as hardware but are used in thought
experiments about computability.

 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.

 Turing's goal as to describe precisely the boundary between what a computing


machine could do and what it could not do.

 In the 1940's and 1950's, simpler kinds of machines, which we today call "finite
automata," were studied by a number of researchers.

 These automata, originally proposed to model brain function, turned out to be


extremely useful for a variety of other purposes.

 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.

 The latter class of problems is called "intractable," or "NP‐hard." It is highly unlikely


that even the exponential improvement in computing speed that computer hardware
has been following ("Moore's Law") will have significant impact our ability to solve
large instances of intractable problems.

 Any of these theoretical developments bear directly on what computer scientists do


today.

 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.

Why Study Automata Theory?


There are several reasons why the study of automata and complexity is an important
part of the core of Computer Science.

Introduction to Finite Automata:

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:

1. Software for designing and checking the behaviour of digital circuits.

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.

 For example, we could implement it in hardware as a circuit, or as a simple form of


program that can make decisions looking only at a limited amount of data or using
the position in the code itself to make the decision.

 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

Figure: A finite autom


maton modeeling an on/
/off switch

 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.

 Onee of the staates is dessignated th e "start sttate," the state


s in w
which the sy
ystem is
placced initially
y. In our ex
xample, thee start statte is off, and we conveentionally indicate
the start state by the worrd Start andd an arroww leading to that state..

 It iss often neccessary to indicate on ne or moree states as "final" or "accepting" states.


Enttering one of these states
s afterr a sequen nce of inputs indicattes that th he input
sequence is go ood in som
me way. Forr instance, we w could have
h regardded the statte on in
Fig.. as accepting, becausee in that sttate, the deevice being
g controlledd by the sw
witch will
opeerate. It is convention
c al to design nate accepting states by a doubble circle, although
a
we have not made
m any su
uch designaation in Fig g.

 Exaample 2: So ometimes, what


w is rem
membered by a state can be muuch more complex
c
thann an on/offf choice. Fig
gure, showws another finite
f autom
maton thatt could be part
p of a
lexiical analyzeer.

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

 Thee job of thiss automaton is to rec ognize the keyword then.


t It thuss needs fiv
ve states,
each h of which representss a differen
nt position in
i the word
d then thatt has been reached
so ffar.

 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.

Figure 1.2: A finite autom


maton mod
deling recogn
nition of the
en

 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.

 Thee start statte correspoonds to thee empty string, and each


e state hhas a transsition on
the next letterr of then too the state that correesponds to the next‐llarger prefix. The
statte named then
t is ente
ered when the input has spelled d the wordd then.

 Sincce it is the job of thiss automatoon to recogn


nize when then
t has beeen seen, we
w could
con
nsider that state the lone acceptting state.

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:

1. Whhat can a computerr do at alll? This sttudy is ca alled "deciddability," and


a the
problems thatt can be solved by com
mputer are called "deccidable."

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.

The Central Concepts of Automata Theory


In this section we shall introduce the most important definitions of terms that pervade
the theory of automata. These concepts include the "alphabet" (a set of symbols),
"strings" (a list of symbols from an alphabet), and "language" (a set of strings from the
same alphabet).

1. Alphabets

An alphabet is a finite, nonempty set of symbols. Conventionally, we use the


symbol ∑ for an alphabet. Common alphabets include:

a. ∑ = {0, 1}, the binary alphabet.


b. ∑ = {a, b, ... , z}, the set of all lower‐case letters.
c. The set of all ASCII characters or the set of all printable ASCII characters.

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}.

The string 111 is another string chosen from this alphabet.

3. The Empty String:

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,

|011| = 3 and |ε| = 0.

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.

If Σ = {0, 1}, then Σ1 = {0, 1}, Σ2 = {00,01, 10, 11},


Σ3 = {000,001,010,011,100,101,110,111} and so on.

 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

 If Σ is an alphabet, and L ⊆ Σ*, then L is a language over Σ.

 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.

 Another example is C, or any other programming language, where the legal


programs are a subset of the possible strings that can be formed from the alphabet
of the language. This alphabet is a subset of the ASCII characters. The exact
alphabet may differ slightly among different programming languages, but generally
includes the upper‐ and lower‐case letters, the digits, punctuation, and
mathematical symbols.

 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

Set‐Formers as a Way to Define Languages

 It is common to describe a language using a "set‐former":


{ω I something about ω}

 This expression is read "the set of words ω such that (whatever is said about ω to
the right of the vertical bar)."
Examples are:

1. {ω I ω consists of an equal number of 0's and 1's }.

2. {ω I ω is a binary integer that is prime }.

3. {ω I ω is a syntactically correct C program }.

 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:

Given a string ω in Σ*, decide whether or not ω is in 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

Desiign step
ps for Finite
F A
Automa
ata: Reffer classs notess.

Moddel of a Finitte Auttomata/ Finitte Statte Macchine


(FSM
M):
The Mo
odel consistts of three main partss, as followss:

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 ∑.

b. The en o the tape contain th


nd square of he endmark
ker ₵ at thhe left end and the
endmarker $ at th
he right end
d.

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.

Operation of Finite Automata/ Finite State Machine is


given below:
1. Input string is fed to the machine through a tape. Tape is divided into squares and
each square contains an input symbol.
2. The main machine is shown as a box called as the Finite Control. The finite control
has the mathematical logic of the model.
3. Initially the machine is in the starting state (q0). Reading head is placed at the
leftmost square of the tape.
4. At regular intervals, the machine reads one symbol from the tape and then enters a
new state. Transition to a state depends only on the current state and the input
symbol.

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’.

Types of Finite Automata:


 As was mentioned earlier, a finite automaton has a set of states, and its "control''
moves from state to state in response to external "inputs."

 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.

 Types of Finite Automata:


1. Deterministic Finite Automata (DFA)
2. Non‐Deterministic Finite Automata (NFA)

Deterministic Finite Automata


 The term "deterministic" refers to the fact that on each input there is one and only
one state to which the automaton can transition from its current 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

 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.

Definition of a Deterministic Finite Automaton


A deterministic finite automaton consists of:

1. A finite set of states, often denoted Q.

2. A finite set of input symbols, often denoted Σ.

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.

4. A start state, one of the states in Q.

5. A set of final or accepting states F. The set F is a subset of Q.

 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.

Simpler Notations for DFA's:


There are two preferred notations for representing transition function for automata:

1. A transition diagram, which is a graph.

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. A transition table, which is a tabular listing of the δ function, which by


implication tells us the set of states and the input alphabet.

1. Transition Diagrams
A transition diagram for a DFA A = (Q, Σ, δ, qo, F) is a graph defined as follows:

1. For each state in Q there is a node.

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.

4. Nodes corresponding to accepting states (those in F) are marked by a double


circle. States not in F have a single circle.

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

 The entry for


f the row
w correspon
nding to staate q and the
t columnn correspon
nding to
input a is the
t state δ((q, a).

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

The Language of a DFA


A:
 Now, we can
c define the languaage of a DF
FA A= (Q, Σ, δ, qo, F)). This lang
guage is
denoted L(A),
L and is defined byy

 That is, thee language of A is the set of strin


ngs ω that take the sttart state q0 to one
of the acceepting state
es.

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

 Likee the DFA, an NFA has a finite seet of states, a finite se


et of input symbols, one
o start
statte and a sett of accepting states.

 It aalso has a transition function, w


which we shall comm
monly call δ. The difference
betw ween the DFA
D and the e NFA is in the type off δ.

 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.

Figure: An NFA accep


pting all strrings that end
e in 01

 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 .

 Thee NFA has the


t option of going eitther to q0 or o to q1, an
nd in fact itt does both
h, as we
shall see when n we make the definittions preciise. In state e Q1, the N FA checks that the
nexxt symbol iss 1, and if so, it goes too state q2 and
a acceptss.

 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

 While a DFA has


h exactly one arc ou ut of each sttate for each input syymbol, an NFA
N has
no ssuch constrraint; we have
h seen i n Fig. casess where the
e number oof arcs is ze
ero, one,
and
d two, for exxample.

 Beloow figure suggests


s how an NFAA processess inputs. We have shoown what happens
h
wheen the auto omaton of above Fig.. receives the t input sequence 000101. It starts
s in
onlyy its start state,
s q0. When
W the firrst 0 is reaad, the NFA
A may go too either state q0 or
statte q1 , so itt does both
h. These tw
wo threads are suggessted by thee second co olumn in
beloow Fig.

Theen, the seccond 0 is read.


r State q0 may again go to o both q0 and q1. However,
statte q1 has no
n transitio
on on 0, so it "dies." When
W the third
t inputt, a 1, occu
urs, we
musst consider transitioons from booth q0 and d q1· We fin
nd that q0 goes only to q0 on
1, w
while q1 goes only to q2.

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.

Definition of Nondeterministic Finite Automata


 Now, let us introduce the formal notions associated with nondeterministic finite
automata. The differences between DFA's and NFA's will be pointed out as we do. An
NFA is represented essentially like a DFA:
A = (Q, Σ, δ, q0, F)
where:
1. Q is a finite set of states.

2. Σ is a finite set of input symbols.

3. q0 , a member of Q, is the start state.

4. F, a subset of Q, is the set of final (or accepting) states.

5. δ, the transition function that takes a state in Q and an input symbol in Σ as


arguments and returns a subset of Q. Notice that the only difference between
an NFA and a DFA is in the type of value that δ returns: a set of states in the
case of an NFA and a single state in the case of a DFA.

The Extended Transition Function: Refer Class Notes.

The Language of an NFA:


 As we have suggested, an NFA accepts a string ω if it is possible to make any
sequence of choices of next state, while reading the characters of ω, and go from the
start state to any accepting 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
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

 Thaat is, L(A) is the set of strings ω in Σ* su


uch that δ((q0 ,ω) conntains at le
east one
acceepting state.

Refer Class No
otes for Exampless.

Equiivalencce of Determ
D ministic and Nondet
N terministic:
Finitte Auto
omata: Refer C
Class Notes

Application: Text Sea


An A arch

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:

1. The repository on which the search is conducted is rapidly changing. For


example:
 Every day, news analysts want to search the day's on‐line news
articles for relevant topics. For example, a financial analyst might
search for certain stock ticker symbols or names of companies.

 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.

2. The documents to be searched cannot be cataloged. For example,


Amazon.com does not make it easy for crawlers to find all the pages for all the
books that the company sells. Rather, these pages are generated "on the fly"
in response to queries. However, we could send a query for books on a certain
topic, say "finite automata," and then search the pages retrieved for certain
words, e.g., “excellent" in a review portion.

2. Nondeterministic Finite Automata for Text Search


 Suppose we are given a set of words, which we shall call the keywords , and we want
to find occurrences of any of these words. In applications such as these, a useful way
to proceed is to design a nondeterministic finite automaton, which signals, by
entering an accepting state, that it has seen one of the keywords.

 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.

 We have two major


m choicces for an iimplementtation of this NFA.
1. Write a program thatt simulaates this NF
FA by comp puting the sset of state
es it is in
after reeading each
h input symmbol. The siimulation was
w suggessted in belo ow Fig.

FA using the subset cconstructio


2. Converrt the NFA to an equiivalent DF on. Then
simulatte the DFA directly.

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

Finite Automata With Epsilon‐Transitions: Refer Class


Notes

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.

d. As it cannot retrieve we cannot say, it can remember something. This also


means that FSM eventually will always repeat a state produce a periodic
sequence of states.

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.

b. Hence for multiplication operation it is required to remember two full


sequences corresponding to multiplier and multiplicand, while multiplying, it
is also required to store the partial sums that we obtain normally at
intermediate stages of multiplication.

c. Therefore, no FSM can multiply two given arbitrarily large numbers.

4. Impossibility of palindrome recognition: FSM can recognizing a palindrome,


because it does not have that capability to remember all the symbols it reads until
half the way point of input sequence, in order to match them in reverse order, with
the symbols in second half of the sequence.

5. Impossibility to check well‐formed ness of parenthesis:

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.

Problems on Finite Automata / Finite State Machine:

Refer class Notes for below problems:

Divisibility:
1. Design a machine which checks whether a given decimal number is divisible by 3.

2. Design a machine which checks whether a given decimal number is divisible by 4.


3. Design a machine which checks whether the given binary number is divisible by 3. (May‐01,
May‐02, Dec‐02, June‐07)

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 a FSM to check whether a given unary number is divisible by 3. (May‐03)


5. Design a FSM to check whether a given unary number is divisible by 4. (Dec‐05)

6. Design divisibility by four testers FSM for binary numbers. (Dec‐05)


7. Design a machine which checks whether the given ternary number is divisible by 4. (Dec‐
02)

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

17. Design FSM to implement Binary Adder. (Jun‐08)

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:

1. Give DFA accepting the following language over ∑ = {0, 1}


a. Number of 1’s is multiple of 3.
b. Number of 1’s is not multiple of 3.

2. Give DFA accepting the following language over ∑ = {0, 1}


a. Number of 1’s is even and number of 0’s is even.
b. Number of 1’s is odd and number of 0’s is odd.

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.

4. Draw DFA for the following language over ∑ = {0, 1}


a. All strings of length at most 5.
b. All strings with exactly two 1’s.
c. All strings containing at least two 0’s.
d. All strings containing at most two 0’s.
e. All strings starting with 1 and length of the string is divisible by 3.

5. Draw DFA for the following language over ∑ = {0, 1}


a. All strings starting with ‘abb’.
b. All strings with ‘abb’ as a substring i.e., ‘abb’ anywhere in the string.
c. All strings ending in ‘abb’.

6. Design DFA for a language of string 0 and 1, such that:


a. Ending with ‘10’.
b. Ending with ‘11’.
c. Ending with ‘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

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.

Subjects Taken by Ganesh Sir:


Semester Subject Batch
SEM‐VI SPCC (System Vacation +
Programming and
(Computer) Compiler Construction)
Regular
ALL OCAJP 1.7 Vacation +
(Oracle Certified
Associate JAVA
Regular
Programmer)
ALL OCPJP 1.7 Vacation +
(Oracle Certified
Professional JAVA
Regular
Programmer)

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

 We begin thiss chapter by introduucing the notation


n called
c "reguular expre
essions."
Theese expresssions are another tyype of language‐defin
ning notattion.

 Reggular expreessions alsso may bee thought of as a "p


programmiing language," in
whiich we express some importantt applicatiions, such as text‐seaarch appliications
or ccompiler componentss.

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."

 Howwever, reggular expressions offfer sometthing that automaata do not:


n a
decclarative way
w to exprress the sttrings we want to accept.
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

Operators of the Regular Expression:


1. Union:
Say L1 and L2 are the two Language, then L1 U L2 = {a, b | a Є L1 and b Є L2}
Example: L1 = {a, b} over ∑ = {a, b} and L2 = {aa, bb} over ∑ = {a, b}
Then L1 U L2 = {a, b, aa, bb}

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}

3. Closure (zero or more):


Say L is a Language, then language closure (L*) is denoted as:
L* = L0 U L1 U L2 U L3…………………..
Example:
If ∑ = {0, 1}
Then L* over ∑ = {ɛ, 0, 1, 00, 01, 10, 11, 000, 001 …}

4. Positive Closure (one or more):


Say L is a Language, then Language positive closure (L+) is denoted as:
L+ = L1 U L2 U L3…………………..
Example:
If ∑ = {0, 1}
Then L+ over ∑= {0, 1, 00, 01, 10, 11, 000, 001 …}
L+ = L.L*

Building Regular Expressions:


 Algebras of all kinds start with some elementary expressions, usually
constants and/or 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

 Algebras then allow us to construct more expressions by applying a


certain set of operators to these elementary expressions and to previously
constructed expressions.

 Usually, some method of grouping operators with their operands, such as


parentheses, is required as well.

 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.

 We can describe the regular expressions recursively, as follows. In this


definition , we not only describe what the legal regular expressions are, but
for each regular expression E, we describe the language it represents, which
we denote L(E).

The basis consists of three parts:


1. The constants ε and ϕ are regular expressions, denoting the languages {ε}
and ϕ, respectively. That is, L(ε) = {ε}, and L(ϕ) = ϕ.
2. If a is any symbol, then a is a regular expression. This expression denotes
the language {a}. That is, L(a) = {a}. Note that we use boldface font to
denote an expression corresponding to a symbol. The correspondence, e.g.
that a refers to cl, should be obvious.
3. A variable, usually capitalized and italic such as L, is a variable,
representing any language.

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.

 We are familiiar with th he notion of precedeence from ordinary arithmetic


pressions. For
exp F instan nce, we kn now that xy+z groups the pproduct xy y
befo
ore the summ, so it is equivalentt to the paarenthesize
ed expresssion (xy)+ z
and
d not to th he expresssion x(y+zz). Similarlly, we grouup two off the same
ope
erators froom the leftt in arithm
metic, so x‐ y ‐ z is equivallent to (x x‐
y)‐‐ z, and not to x‐ (y ‐ z).

 Forr regular expressions


e s, the folloowing is th
he order of
o precedennce for the
opeerators:

1. The star operator is of higheest preced


dence. That is, it aapplies only
to the sm
mallest sequence of ssymbols to
o its left that is a w
well‐formed
d
regular exxpression.
2. Next in precedencce comes the conca atenation or "dot" operatorr.
After grouuping all stars
s to th
heir operaands, we group
g con
ncatenation n
operatorss to their operand ds. Thatt is, all ex xpressions that arre
jjuxtaposed (adjacentt, with n no interve ening ope erator) arre grouped
together. Since concatenation n is an asssociative operator itt does no ot
matter in what order we gro oup consecu utive conca
atenations, although if
there is a choice to be
b made, yyou shouldd group the em from thhe left. Fo
or
instance, 012
0 is grou
uped (01)22.

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.

Finite Automata and Regular Expressions


 While the regular‐expression approach to describing languages is
fundamentally different from the finite‐automaton approach, these two
notations turn out to represent exactly the same set of languages, which
we have termed the "regular languages."

 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:

1. Every language defined by one of these automata is also


defined by a regular expression. For this proof, we can assume the
language is accepted by some DFA.

2. Every language defined by a regular expression is defined by one


of these automata. For this part of the proof, the easiest is to
show that there is an NFA with :‐transitions accepting the same
language.

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

Figure: Plan forr showingg the equ


uivalence of four differen
nt
notations for regu
ular langua
ages

From
m DFA's
s tto Regullar
Expressio
ons:

 Thee construcction of a regular eexpression to define the languuage of any


DFAA is surprrisingly triicky. Rougghly, we bu uild expressions thaat describe
setss of stringss that labe
el certain p
paths in th
he DFA 's transition
t diagram.

 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.

 Finaally, the paaths are alllowed to ggo through any state


e; i.e., the eexpression
ns
we generate at a the end representt all possibble paths. These ideaas appear in n
the proof of th he followin
ng theoremm.

Theoreem: If L = L(A) for some


s DFA
A A, then th
here is a regular
r exxpression R
such th
hat L = L(R
R).

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

Converting DFA's to Regular


Expressions by Eliminating States:
Refer Class Notes.

Converting Regular Expressions to


Automata:
Theorem: Every language defined by a regular expression is also defined by
a finite automaton.
PROOF: Suppose L = L(R) for a regular expression R. We show that L = L(E) for some
ε‐NFA E with:

1. Exactly one accepting state.


2. No arcs into the initial state.
3. No arcs out of the accepting state.
The proof is by structural induction on R, following the recursive definition of
regular expressions that we had before.

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 1: The bassis of the constructiion of an automatoon from a


rregular exp
pression

 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 .

 Parrt (b) show


ws the consstruction foor ϕ. Clearly there arre no pathss from starrt
statte to accep
pting state, so ϕ is tthe languagge of this automaton n.

 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

Figure: The indu


uctive step
p in the regular‐e
expressionn‐to‐ε‐FA
A
constructtion

The fo
our cases are:
a

1. The expression is R + S for som


me smallerr expressio
ons R an
nd S. Then
the autommaton of Fig.
F (a) serrves. That is, startiing at the new star rt
state, we can
c go to the
t start sstate of eitther the au
utomaton for R or th he
automaton n for S. We
W then reeach the accepting
a state
s of onne of t.hese
automata, followingg a path l abeled by y some striing in L(RR) or L( S)),

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

respectively. Once we reach the accepting state of the automaton for R


or S , we can follow one of the ε‐arcs to the accepting state of the
new automaton.

Thus, the language of the automaton in Fig. {a) is L( R) U L(S).

2. The expression is RS for some smaller expressions R and S. The


automaton for the concatenation is shown in Fig. (b). Note that the
start state of the first automaton becomes the start state of the
whole, and the accepting state of the second automaton becomes the
accepting state of the whole. The idea is that the only paths from start
to accepting state go first through the automaton for R , where it must
follow a path labeled by a string in L( R ), and t hen through the
automaton for S, where it follows a path labeled by a string in L(S ).
Thus, the paths in the automaton of Fig. (b) are all and only those
labeled by strings in L(R)L(S).

3. The expression is R* for some smaller expression R. Then we use


the automaton of Fig. (c). That automaton allows us to go either:
(a) Directly from the start state to the accepting state along
a path labeled ε. That path lets us accept ε, which is in L(
R*) no matter what expression R is.
(b) To the start state of the automaton for R , through that
automaton one or more times, and then to the accepting
state. This set of paths allows us to accept strings in L( R),
L(R)L(R), L( )L( R)L(R), and so on , thus covering all strings in
L(R* ) except perhaps ε, which was covered by the direct arc
to the accepting state mentioned in (1a).

4. The expression is (R) for some smaller expression R. The


automaton for R also serves as the automaton for (R), since the
parentheses do not change the language defined by the expression.

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

Associativity law of multiplication:


(A*B)*C = A*(B*C)
Associativity law of Union:
(A+B)+C = A+ (B+C)

2. Commutative: Commutative is the property of an operator that says we can switch


the order of operands and get the same result.
Example:
Commutative law of union:
A+B = B+A

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.

1 is the identity for Multiplication:


1 * X = X *1 = X, here 1 is the identity.

ᶲ is the identity for union


ᶲ U L = L U ᶲ = L, here ᶲ is the identity.

ɛ is the identity for concatenation


ɛ.L = L.ɛ = L, here ɛ 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.

ᶲ is the annihilator for concatenation.


ᶲ.L = L.ᶲ = L, here ᶲ 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

(X+Y)*Z = X*Z + Y*Z

6. Idempotent Law: An operator is said to be idempotent if the result of applying it to


two of the same values as arguments is that value.
Example:
Law of idempotent for union:
LUL=L

7. Laws involving closures:


(L*)* = L*
ᶲ* = ɛ
ɛ* = ɛ
L+ = LL*
L* = L+ + ɛ

Problems:
Refer class Notes for below problems:

1. Describe the following sets by Regular expression.


a. { abb}
b. { 1010}
c. { ab, ba}
d. { ɛ, aa}
e. { 011, 0, 1, 110}

2. Write the regular expression for the following:


a. Set of all strings on {a, b} terminated by either an ‘a’ or a ‘bb’.
b. Set of all strings on {0, 1} starting with 10 and ending with ‘111’.
c. Set of all strings on {a, b} with an even number of a’s followed by an odd
number of b’s.
(Dec‐00)
3. Write regular expressions for the following languages:
a. L = {anbm| n>=4, m<=3 }
b. L = {w | (|w|mod 3 = 0), w Є {a, b} }
(June‐08)
4. Write regular expressions for the following languages:
a. L = {anbm| (n+m) is even }

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. L = {w Є {a, b}* | (| number of a’s in w|mod 3 = 0) }


(Dec‐07)
5. Write the regular expression to generate strings of length 6 or less over {0, 1}

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).

8. Express in words the language represented by the following regular expression


(a*bc)*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.

10. Write regular expression for the following:


a. Set of all strings on {a, b} which end in a ‘a’ or ‘bb’
b. Set of all strings on {0, 1} starting with 10 and ending with 111.
c. Set of all strings on {0, 1} 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.

12. Write regular expression for the following:


a. Set of all strings 0’s and 1’s such that 10th symbol from the right end is 1.
b. Set of all string in (0+1)* such that some of two 0’s are separated by a
string whose length is 4i, for some i>=0.
13. Define the language such that all words begin and end with ‘a’ and in between
any word using ‘b’.

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

16. Prove the following identities for regular expressions r, s and t.


a. r + s = s + r
b. ( r + s ) + t = r + ( s + t )
c. (rs)t = r(st) (Dec‐03)
d. r ( s + t ) = rt + st (Dec‐03)
e. ( r*)* = r*
f. ( ɛ + r )* = r*
g. (r*s*)* = (r + s)*

17. Verify the following identities involving regular expressions:


a. (r + s) + t = r + (s + t)
b. (rs)t = r(st)
(May‐06)

18. Prove or disprove the following regular expressions r, s and t:


a. (rs + r)*r = r (sr + r)*
b. s(rs + s)*r = rr*s(r*s)*
c. (r + s)* = r* + s*

19. Prove the formula: (111*)* = (11 + 111)*

20. Show that (1 + 00*1) + (1+ 00*1)(0+10*1)*(0+10*1) = 0*1(0+ 10*1)*. (May‐06)

21. Show that 0(0+1)* + (0 + 1)*00(0+1)* = (1*0)*(01*)* (Dec‐07)

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

Subjects Taken by Ganesh Sir:


Semester Subject Batch
SEM‐VI SPCC (System Vacation +
Programming and
(Computer) Compiler Construction)
Regular
ALL OCAJP 1.7 Vacation +
(Oracle Certified
Associate JAVA
Regular
Programmer)
ALL OCPJP 1.7 Vacation +
(Oracle Certified
Professional JAVA
Regular
Programmer)

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.

 Not every language is a regular language. We shall introduce a powerful


technique, known as the "pumping lemma," for showing certain
languages not to be regular. We then give several examples of non‐
regular languages.

The Pumping Lemma for Regular Languages

 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.

 However, suppose instead that after reading i or j 0's, the automaton


A starts receiving 1's as input. After receiving i 1's, it must accept if it
previously received i 0's, but not if it received j 0's. Since it was in 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

q when the 1's started, it cannot "remember" whether it received i or j


0's, so we can "fool" A and make it do the wrong thing ‐ accept if it
should not, or fail to accept when it should.

 The above argument is informal, but can be made precise. However,


the same conclusion, that the language L01 is not regular, can be
reached using a general result, as follows.
 Theorem: {The pumping lemma for regular languages) Let L be a
regular language. Then there exists a constant n {which depends on L)
such that for every string ω in L such that lωl >= n, we can break ω
into three strings, ω = xyz, such that:
1. y != ε
2. lxyl <= n.
3. For all k >= 0, the string xykz is also in L.

 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

Figuree 4.1: Eveery string


g longer tthan the number of
o states m
must cause
state to repeat.

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.

 Clossure prope erties exprress the id


dea that when
w one (or
( several)) language es
are regular, then certa ain relate d languagees arc also regular. They alsso
servve as an in
nteresting illustratio
on of how the equiva alent repreesentationns
of tthe regularr languagees (autom mata and regular ex xpressions ) reinforcce
eachh other inn our undeerstandingg of the claass of langu
uages, sincce often on
ne
rep
presentatio on is far better than n the others in sup pporting a proof of a
clossure propeerty. Heree is a summ mary of the
t princip
pal closuree propertie
es
for regular lannguages:
1. The union
n of two reg
gular langu
uages is re
egular.
2. The interssection of two
t regularr languagees is regula
ar.
3. The compllement of a regular llanguage iss regular.
4. The differeence of two
o regular l anguages is
i regular.
5. The reverssal of a reg
gular langu
uage is reg
gular.
6. The closurre (star) of a regularr language is regular.
7. The conca o regular llanguages is regular..
atenation of
8. A homomo orphism (ssubstitutio
on of string
gs for sym
mbols) of a regular la
anguage
is regular..
9. The inversse homomo
orphism off a regular language is regular..

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.

2. Let L and M be languag ges over allphabet. Then L n M is the laanguage


thatt contains all stringss that are in both L and M.
3. Lett L be a language
l over
o alphaabet . The
en ,the complemeent of L, iss
the
e set of striings in ·tha
at are not in L.

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

It tturns out that the regular llanguages are closed


d under aall three of
o
the
e boolean operations
o .

Hom
momorp
phism:

xamples Refer cla


For Ex 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
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:

Figure 4.5: A homomor


h rphism app plied in th
he forward
d and
inveerse direc
ction

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

Subjects Taken by Ganesh Sir:


Semester Subject Batch
SEM‐VI SPCC (System Vacation +
Programming and
(Computer) Compiler Construction)
Regular
ALL OCAJP 1.7 Vacation +
(Oracle Certified
Associate JAVA
Regular
Programmer)
ALL OCPJP 1.7 Vacation +
(Oracle Certified
Professional JAVA
Regular
Programmer)

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.

 First, we must consider what it means to ask a question about a


language.

 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.

 Rather, we present a language by giving one of the finite representations


for it that we have developed: a DFA, an NFA, an €‐NFA, or a regular
expression.

 Of course the language so described will be regular, and in fact there is no


way at all to represent completely arbitrary languages.

 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

 We begin our study of algorithms for questions about regular languages


by reviewing the ways we can convert one representation into another for
the same language.

 In particular, we want to observe the time complexity of the algorithms


that perform the conversions. We then consider some of the fundamental
questions about languages:

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. Is the language described empty?

2. Is a particular string w in the described language?

3. Do two descriptions of a language actually describe the same language?

This question is often called "equivalence" of languages.


Testing Emptiness of Regular Languages

 At first glance the answer to the question "is regular language L


empty?" is obvious: ϕ is empty, and all other regular languages are
not.

 The problem is not stated with an explicit list of the strings in L.


Rather, we are given some representation for L and need to decide
whether that representation denotes the language ϕ.

 If our representation is any kind of finite automaton, the emptiness


question is whether there is any path whatsoever from the start state
to some accepting state.

 If so, the language is nonempty, while if the accepting states are all
separated from the start state, then the language is empty.

 Deciding whether we can reach an accepting state from the start


state is a simple instance of graph‐reachability, similar in spirit to the
calculation of the ε‐closure.

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..

 If L has any other representatio on besides a DFA , we could convert to


aD DFA and run
r the test abovee. That approch
a could takee time tha at
is e
exponentiaal in the size of the represen
ntation, altthough it iis linear in
|w|.

 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.

 We take the union of at most s sets of at most s states each , which


requires O(s2 ) time.

 If the NFA has €‐transitions, then we must compute the €‐closure


before starting the simulation. Then the processing of each input
symbol a has two stages, each of which requires O( s2 ) time.

 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.

 Lastly, if the representation of L ) is a regular expression of size. We


can convert to an NFA with at most 2s states, in O(s) time. We then
perform the simulation above, taking O(ns2) time on an input w of
length n.

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.

Refer Class Notes for Examples.

Moore and Mealy Machine


 FA is the mathematical model of a machine and is defined by a 5‐tuple (Q, ∑, δ, q₀, F)
which does not include the information about output.

 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.

Moore Machine is a six‐tuple:


M=(Q, ∑, Δ, δ, λ, q₀)
Where,
Q ‐ Finite set of states
∑ ‐ Finite input alphabet
Δ ‐ Finite output alphabet
δ ‐ State function, δ: Q X ∑ → Q
λ ‐ Machine function, λ : Q → Δ
q₀ ‐ initial state of the machine. q₀ Є Q

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.

Mealy machine is denoted by a six‐tuple:


M=(Q, ∑, Δ, δ, λ, q₀)
Where,
Q ‐ finite set of states
∑ ‐ finite input alphabet
Δ ‐ finite output alphabet
δ ‐ state function, δ: Q X ∑ → Q
λ ‐ machine function, λ : Q → Δ
q₀ ‐ initial state of the machine. q₀ Є 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

Procedure for transforming Moore Machine to Mealy Machine:


If M₁ =(Q,∑,Δ,δ,λ,q₀) is Moore Macchine, then equivalent Mealy Machine is
M₂=(Q,∑,Δ,δ,λ’,q₀)
where ,

λ’(q,a)= λ(δ(q,a)) for all states Q and input symbols a.

Procedure for transforming Mealy Machine to Moore Machine: (Dec‐2003, May –


2004, Nov – 2004)
If given Mealy Machine is, M₁ =(Q,∑,Δ,δ,λ,q₀) then, an equivalent Moore machine is:
M₂=([Q X Δ],∑,Δ,δ’,Δ’,[q₀,b₀]),

Where, ‘b₀’is an arbitrarily selected member of Δ and δ’([q,b],a) = [δ(q,a),λ(q,a)] and


([q,b]) = b

Problems:

University questions will be solved in Class:

1. Give Mealy and Moore machine for the following process:


For input from (0 + 1)*,
If input ends in 101 output = A
If input ends in 110 output = B
Otherwise output = C
(Dec‐2003, May‐2004, May‐2005)

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)

5. Design Moore and Mealy Machine to increment binary number by 1

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.

8. Design Moore Machine for the following process:


For input from binary (0+1)* print the residue modulo 3 of the input.
(Dec‐1999)

9. Give Mealy and Moore machine for the following:


For input from ∑*, where∑, print the residue modulo 5 of the input treated as a ternary
(base 3, with digits 0,1 and 2 number.
(May‐2006)

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’.

11. Design the Mealy machine for the above.

12. Design Moore Machine to convert each occurrence of 100 to 101.


(Dec‐2000)

13. Design Mealy Machine to convert each occurrence of substring add by aba over
∑={a,B}.
(Dec‐2006, Dec‐2000)

14. Design Moore Machine to convert each occurrence of 101 to 100.

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

15. Design Moore Machine to convert each occurrence of 1000 to 1001.


(Dec‐2002, Jun‐2008)
16. Design Moore Machine to convert each occurrence of 121 to 120.

17. Design Moore Machine to convert each occurrence of 121 to021.

18. Design Moore and Mealy machines to convert substring 121 to 122 for strings of
languages having ∑= {0, 1, 2}
(Dec‐2002)

19. Design Mealy Machine to convert HEX numbers to OCTAL numbers.


(Dec‐2005)

20. Design Moore Machine to convert HEX numbers to OCTAL numbers.


(Dec‐2005)
21. Construct the Mealy Machine to accept the language (0+1)*(00+11).
(Dec‐2003)

22. Convert this Mealy Machine to Moore Machine.


(Dec‐2003)

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

Subjects Taken by Ganesh Sir:


Semester Subject Batch
SEM‐VI SPCC (System Vacation +
Programming and
(Computer) Compiler Construction)
Regular
ALL OCAJP 1.7 Vacation +
(Oracle Certified
Associate JAVA
Regular
Programmer)
ALL OCPJP 1.7 Vacation +
(Oracle Certified
Professional JAVA
Regular
Programmer)

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

 We have seen that every finite automata M accepts a language L, which is


represented by L(M).

 We have seen that a regular language can be described by a regular expression.


 We have seen that there are several languages which are not regular.
1. L1 = {ap | p is a prime} is not regular.
2. L2 = {anbn | n>= 0} is not regular.

 We have seen two ways of representing a language.


1. Using finite automata.
2. Using a regular expression.

 If a language is not‐regular, it cannot be represented either using a FA or using a


regular expression. Hence there was a need for representing such languages.

 Grammar is another approach for representing a language:


1. In this approach, a language is represented using a set of equations.
2. Equations are recursive in nature.
3. Finite Automata have a set of states; a grammar has set of variables.
4. Finite Automata are defined over an alphabet; a grammar is defined over a
set of terminals.
5. Finite Automata have a set of transitions; a grammar has a set of equations
(Productions).

 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:

1. Terminals are denoted by lower case letters a, b, c … or digits 0, 1, 2 … etc.


2. Variables (Non‐Terminals) are denoted by capital letters A, B … V, W, X ….
3. A string of terminals or a word ω Є L is represented using u, v, w, x, y, z.
4. A sentential form is a string of terminals and variables and it is denoted by α, β, ϒ…
etc.

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.

The Language of A Grammar:


 Every grammar generates a language. A word of a language is generated by applying
productions a finite number of times.
 Derivation of a string should start from the start symbol and the final string should
consist of terminals.
 If G is a grammar with start symbol S and set of terminals T, then the language of G is
the set:
*
L(G) = { ω | ω Є T* and S  ω }
G

The * represent that, production can be applied multiple time.

 A string can be derived from start symbol of the grammar, using the productions of
the grammar.

Derivations are represented either in the:

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

2. Parse Tree Form

Sentential Form:

Let us consider a grammar given below:


S  A1B
A  0A | ɛ
B  0B | 1B | ɛ
Where Grammar G is given by (V, T, P, S)
V = {S, A, B}
T = {0, 1}
P = {S  A1B, A  0A | ɛ, B  0B | 1B | ɛ}
S = {S}
Let us try to generate the string ‘00101’ with above grammar.

There are two different derivations possible:


1. Leftmost Derivation.
2. Rightmost Derivation.

Leftmost Derivation: If at each step in a derivation, a production is applied to the


leftmost variable (non‐terminal), then the derivation is called as leftmost derivation.

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  ɛ)

Rightmost Derivation: If at each step in a derivation, a production is applied to the


rightmost variable (non‐terminal), then the derivation is called as rightmost derivation.

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

S  A1B (Start Variable)


S  A10B (using B  0B)
S  A101B (using B  1B)
S  A101 (using B  ɛ)
S  0A101 (using A  0A)
S  00A101 (using A  0A)
S  00101 (using A  ɛ)

Parse Tree Form:

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:

1. Root of the tree is represented by start symbol.


2. Each interior mode is represented by a variable belonging to V.
3. Each leaf node is represented by a terminal or ɛ.

The parse tree is also created in two ways:

1. Using leftmost derivations.


2. Using rightmost derivations.

Problems:

University questions will be solved in Class:

1. For the grammar given below:


S  A1B
A  0A | ɛ
B  0B | 1B | ɛ
Give parse tree for leftmost and rightmost derivation of the string ‘1001’ and ‘00101’.

2. For the grammar given below:


S  0S1 | 01
Give derivation of ‘000111’.

3. Consider the grammar given as:


G = ({S, A}, {a, b}, P, S)
Where P consists of –
S  aAS | 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  SbA | SS | ba
Derive ‘aabaaabbaaa’ using the leftmost derivation and rightmost derivation.
Derive ‘aabbaa’ using the leftmost derivation and rightmost derivation.

4. Consider the following grammar:


S  aB | bA
A  a | aS | bAA
B  b | bS | aBB
Find the leftmost and rightmost derivation for the string: ‘bbaaba’, ‘aaabbabbba’,
‘aaabbb’ and ‘aaaba’.

5. Consider the following grammar:


S  XbbaaX | aX
X  Xa | Xb | ɛ
Construct leftmost derivation and rightmost derivation for the string ‘abaabb’.

Context Free Grammar (CFG):

A context free grammar G is a quadruple (V, T, P, S)


Where,
V – is a set of variable.
T ‐ is a set of terminals.
P – is a set of productions.
S – is a start symbol S Є V.
A production is of the form
Vi  α where Vi Є V and α is a string of terminals and variables.

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

2. For the grammar given below:


EE+T|T
TT*F|F
F  (E) | a | b

Give the derivation of (a + b) * a + b.

3. Write CFG to generate:


i. Set of all strings that start with ‘a’ over ∑ = {a, b}
ii. Set of all strings that start and end with different symbol over {0, 1}
iii. Set of all strings that start and end with same symbol over {0, 1}
iv. Set of all strings that contain over ∑ = {a, b}:
i. Atleast 3 a’s
ii. Exactly 2 a’s
iii. Atmost 1 a

4. Drive the grammar for the given languages:


i. L = {ɛ, a, aa, aaa …}
ii. L = {a, aa, aaa, aaaa …}
iii. L = {b, ab, aab, aaab …}

5. Drive the grammar for the given languages:


i. L = { ω Є {a, b}*}
ii. L = {ɛ, ab, aabb, …, anbn}
iii. L = {ab, aabb, …, anbn}
iv. L = {ω Є {a, b}* | ω is a palindrome of odd length}
v. L = {ω Є {a, b}* | ω is a palindrome of even length with |ω|>0}
vi. L = {ω Є {a, b}* | ω is a palindrome of even and odd length with |ω|>0}

Rules for 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

1. Union rule for Grammar:


If a language L1 is generated by a grammar with start symbol S1 and L2 is generated by
a grammar with start symbol S2 then the union of the languages L1 U L2 can be
generated with start symbol S, where
S S1 | S2

Example:
Let the language L1 and L2 are given as below:
L1 = {an | n>0}
L2 = {bn | n>0}

Productions for L1 are:


S1 aS | a
Productions for L2 are:
S2 bS | b

Then the productions for L = L1 U L2 can be written as:


S  S1 | S2
S1  aS | a
S2  bS | b

2. Concatenation rule for Grammar:

If a language L1 is generated by a grammar with start symbol S1 and L2 is generated by


a grammar with start symbol S2 then the concatenation (product) of the languages
L1.L2 can be generated with start symbol S, where

S  S1S2

Example:

Let the language L1 and L2 are given as below:


L1 = {an | n>0}
L2 = {bn | n>0}

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

Productions for L1 are:


S1 aS | a
Productions for L2 are:
S2 bS | b

Then the productions for L = L1.L2 can be written as:


S  S1 | S2
S1  aS | a
S2  bS | b

Problems:
University questions will be solved in Class:

1. Give a context free grammar for the following language:


0(0+1)*01(0+1)*1

2. Construct the context free grammar corresponding to the regular expression:


R = (0+1)1*(1+(01)*)

3. Give the CFG for L = { aibj, i<= j <= 2i, i>=1 }

4. Give the CFG for


L = { aibjcq, i+j= q;(i, j)>=1 }

5. Find context free grammars generating each of these languages:


a. L = { aibjck | i=j+k }
b. L = { aibjck | j=i+k }
c. L = { aibjck | i=j or j=k }

6. Give the context free grammar for the following languages:


a. (011+1)*(01)*
b. 0i1i+k0k where i, k>=0

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. Show that the languages:


a. L = { aibicj | i, j >= 1 } and
b. L = { aibjcj | i, j >= 1 } are context free languages.

8. Give CFG for matching parenthesis.

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}

13. Give CFG for strings in ab*.

14. Give CFG for strings in a*b*.

15. Find CFG for generating: (Dec‐06, May‐09, Dec‐09)


a. String containing alternate sequence of 0’s and 1’s, ∑ = {0, 1}
b. The string containing no consecutive b’s but a’s can be consecutive
c. The set of all string over alphabet {a, b} with exactly twice as many a’s as b’s.
d. Language having number of a’s greater then number of b’s

16. Write CFG for the language


∑ = {a, b} number of a’s is a multiple of 3.

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

Definition: A grammar is said to be ambiguous if the language generated by the


grammar contains some string that has two different parse trees.

Example: Let us consider the grammar given below:

EE+E|a|b

A string (a + b + a) is generated by the given grammar.

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.

Ambiguity from the grammar

EE+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.

 Parse tree of Fig (b) is based on left to right evaluation.

 Left to right evaluation in Grammar can be enforced by introducing one more


variable T. Variable T cannot be broken by + operator.
 An unambiguous grammar for the grammar is given as:
EE+T|T
T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

 A production of the form E  E + T provides a binding. E + T, implies that E must be


evaluated first before an atomic T can be added to it. E can be broken down in E + T
but T cannot be broken further. This ensures higher precedence to left side +
operator over right side + operator.

 Parse tree in Fig. given below is based on unambiguous grammar for the string (a + b
+ a).

E + T

a
T + T

a b

Fig: A Parse tree for (a+ b +c) using an unambiguous grammar

Problems:
University questions will be solved in Class:

1. Consider the grammar:


E  E + E | E * E | (E) | I
Ia|b
Show that the grammar is ambiguous.
Remove 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

2. Show that the CFG given below. Which generates all strings of balanced parentheses
is ambiguous. Give an equivalent unambiguous grammar.
S  SS | (S) | ɛ

3. Write an unambiguous CFG for arithmetic expressions with operators:


+, *, /, ^, unary minus and operand a, b, c, d, e and f.
Also, if should be possible to generate brackets with your grammar.
Derive (a+b)^d/e+(‐f) from your grammar. (Dec 2005)

4. Is the following CFG ambiguous?


S  aB | ab
A  aAB | a
B  ABb | b
If so, show multiple derivation trees for the same string.

5. Is the following CFG ambiguous?


G = ( {S, A}, {a, b}, P, S)
Where, P consists of
S  aAS | a
A  SbA | SS | ba

6. Consider the grammar having productions:


S  aS | ɛ
S  aSbS
The grammar is ambiguous.
i. Show in particular that the string ‘aab’ has two parse trees.
ii. Find an unambiguous grammar for the same.

7. Let G be the grammar


S  aB | bA
A  a | aS | bAA
B  b | bS |aBB
For string ‘aaabbabbba’ find
i. Left most derivation
ii. Right most derivation
iii. Parse Tree

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

iv. Is the grammar unambiguous? (Dec‐2009)

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 |ɛ

9. Consider the grammar:


G = ( {V = {E , F}}, {T ={a, b, ‐}}, E, P)
Where P consists of rules:
EF–E
Fa
EE–F
Fb
EF
i. Show that G is ambiguous
ii. Remove the ambiguity.

10. Test whether the following grammars are ambiguous:


i. S  0S1S | 1S0S | ɛ
ii. S  AA, A aAb | bAa | ɛ

Simplification of CFG:
 A grammar written in a simple form is easy to analyse. Certain restrictions are
imposed on simplified grammar.

 Simplification of CFG involves transforming CFG into an equivalent form that


satisfies certain restrictions on its form. A CFG can be simplified by eliminating:
1. Useless symbols.
2. ɛ ‐ Productions.
3. Unit 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

1. Elimination of useless symbols: A grammar may contain symbols and productions


which are not useful for derivation of strings. Two types of symbols are useless.
a. Non – generating symbols
b. Non reachable symbol.

Non – generating symbols: A symbol X Є V (Set of variables) is a generating symbol if :


*
X w
G
Where, w Є T* i.e. every variable must generate a string of terminals.

Example: Consider the following Grammar:

S  Aa | Bb | a | b

A  Aa | a

B  bB

Requires simplification as the symbol B is non‐generating. Only production for B is

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

A grammar containing a non‐generating symbol Vi should be simplified by deleting


every production containing the non‐generating symbol Vi.

Finding non‐generating symbols:

There are two rules for finding a set of generating symbols for the given grammar.

1. Every symbol in T (terminal) is generating.


2. If there is a production A  α and every symbol in α is generating, then A is
generating. Where, α Є (V+T)*

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 symbol not in a set of generating symbols is said to be non‐generating.

Problems on Non‐generating symbols:

University questions will be solved in Class:

1. Find non‐generating symbols in the grammar given below


S  AB | CA
B  BC | AB
Aa
C  aB | b

2. Find non‐generating symbols in the grammar given below


S  aAa
A  Sb | bCC
C  abb
E  aC

3. Find non‐generating symbols in the grammar given below


S  aAa
A  Sb | bCC |DaA
C  abb | DD
E  aC
D  aDa

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.

A grammar containing a non‐reachable symbol Vi should be simplified by deleting every


production containing the non‐reachable symbol Vi.

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

Finding non‐reachable symbols:

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.

Problems on Non‐reachable symbols:


University questions will be solved in Class:

1. Eliminate non‐reachable symbols from the given grammar:


S  aAa
A  Sb | bCC
C  abb
E  aC

2. Eliminate non‐reachable symbols from the given grammar:


S  aBa | BC
A  aC | BCC
Ca
B  bCC
DE
Ed

3. Eliminate non‐reachable symbols from the given grammar:


S  aAa
A  bBB
B  ab
C  aB

4. Eliminate non‐reachable symbols from 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

S  aS | AB
A  bA
B  AA

2. Elimination of ɛ‐productions:

A production of the form A  ɛ, is called a null productions or ɛ‐ production. For every


context free grammar G with ɛ‐productions, we can find a context‐free grammar G1
having no ɛ‐productions such that

L(G1) = L(G) – {ɛ}

The procedure for finding G1 is as follows:

Step 1: Find nullable variables.

Step 2: Addition of productions with nullable variables removed.

Step 3: Remove ɛ‐productions.

Problems on elimination of ɛ‐productions:


University questions will be solved in Class:

1. Eliminate the ɛ‐production from the grammar given below:


S  aS
Sɛ

2. Eliminate the ɛ‐production from the grammar given below:


S  ABA
Aɛ
Bɛ

3. Eliminate the ɛ‐production from the grammar given below:


S  aS | AB
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

Bɛ

4. Eliminate the ɛ‐production from the grammar given below:


S  ABA
A  aA | ɛ
B  bB | ɛ

5. Eliminate the ɛ‐production from the grammar given below:


S  AB
A  aAA | ɛ
B  bBB | ɛ

3. Elimination of Unit Productions:

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)

The procedure for finding G1 is as follows:

 The technique is based on expansion of unit production until it disappears. This


technique works in most of the cases. This technique does not work if there is a
cycle of unit productions such as
A1  A2
A2  A3
A3  A4
A4  A1
 The steps for elimination of unit productions are as follows:

STEP 1: Add all non‐unit production of G to G1.

STEP 2: Locate every pair of variables (Ai, Aj) such that

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.

Each variable Ai to Aj will derive α.

Problems on Elimination of Unit Productions:


University questions will be solved in Class:

1. Eliminate unit productions form:


S  ABA | BA | AA | AB | A | B
A  aA | a
B  bB |b

2. Eliminate unit productions from the grammar:


EE+T|T
TT*F|F
F  (E) | I
I  a | b | Ia | Ib | I0 | I1

3. Simplify the following grammar:


S  ASB | ɛ
A  aAS | a
B  SbS | A | bb

4. Simplify the following grammar:


S  0A0 | 1B1 | BB
AC
B  S |A
CS|ɛ

5. Simplify the following grammar:


S  Ab
Aa
BC|b
CD

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

DE
Ea

6. Find a reduced grammar equivalent to:


S  aC | SB
A  bSCa
B  aSB | bBC
C  aBC | ad

Normal forms for CFG:

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. Chomsky Normal Form (CNF): (Dec‐


2009)

A context free grammar (CFG) without ɛ‐ production is said to be in CNF if every


production is of the form:

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.

Algorithm for CFG to CNF Conversion:

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

Examples: Consider a production A  V1V2aV3bV4.


Terminal symbols ‘a’ and ‘b’ can be removed by rewriting the production
A  V1V2aV3bV4 as
A  V1V2CaV3CbV4

And adding two productions

Ca  a and

Cb  b

3. Every production deriving 2 or more variables (A  α with | α | ≥ 3) can be broken


down into a cascade of productions with each deriving a string of two variables.
Examples: Consider a production A  X1X2…Xn where n≥ 3 and as Xi’s are variables.
The production A  X1X2…Xn should be broken down as given below:

A  X1C1
C1  X2C2
C2  X3C3
:
Cn‐2  Xn‐1Xn
each with two variables on the right.

Problems on CFG to CNF conversion:

University questions will be solved in Class:

1. Find the CNF equivalent to:


S  aAbB
A  aA
B  bB | b

2. Convert the grammar given below to its equivalent CNF:


S  PQP
P  0P | ɛ
Q  1Q | ɛ

3. Check whether the given grammar is in CNF:

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

If it is not in CNF, find the equivalent CNF.

4. Design a CNF grammar for the set of strings of balanced parenthesis.

5. Convert the following grammar to CNF:

S  Aba
S  aab
B  Ac

6. Convert the following grammar to CNF:


S  AACD
A  aAb | ɛ
C  aC | a
D  aDa | bDb | ɛ

7. Given a CFG G, find G’ in CNF generating L(G) ‐ ɛ (May‐2006,


May‐2009)

S  ASB | ɛ

A  AaS | a

B  SbS | A | bb

8. Convert the given grammar to CNF


S  aSB | aA
A  Aa | Sa | a

2. Greibach Normal Form (GNF): (Dec‐2005, May‐2007, Dec‐2008, Dec‐2009)

A Context free grammar G = (V, T, P, S) is said to be in GNF if every production is of


the form:

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

Where, a Є T is a terminal and α is a string of zero or more variables. The language


L(G) should be without ɛ. Right hand side of each production should start with a
terminal followed by a string of non‐terminals of length zero or more.

Removing Left Recursion:

Elimination of left recursion is an important step in algorithm used in conversion of


a CFG into GNF form.

Left recursive grammar: A production of the form A  Aα is called left recursive as


the left hand side variable appears as the first symbol on the right hand side.

Language generated by left recursive grammar: Let us consider a CFG containing


productions of the form

A  Aα …[Left recursive ]

Aβ …[For termination of recursion ]

The language generated by above productions is:

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 | α

Thus a left recursive grammar

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

can be written using a right recursive grammar as:

A  βB | β

B  αB | α

Problems on conversion of Left Recursive grammar to Right Recursive grammar:

1. A  Aa | b
2. A  Aa | b | c
3. A  ABC | BC
4. A  ABC | DA | EC
5. S  S10 | 0

Algorithm for conversion from CFG to GNF:

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

Thus, at the end of step 2 all productions must be of the forms:

Aα

Aa

A  aα

Where, ‘a’ is a terminal and α is a string of non‐terminals.

3. Rename variables as A1, A2, A3 … An to create A‐productions.


Example: Consider a grammar given below

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.

Problems on CFG to GNF conversion:


University questions will be solved in Class:

1. Construct a grammar in GNF which is equivalent to the grammar

S  AA | a

A  SS | b

2. Find the grammar in GNF for the given CFG


EE+T|T
TT*F|F
F  (E) | a

3. Give the GNF for following CFG

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

4. Reduce the following grammar to GNF


S  AB
A  BSB | BB | b
B  aAb | a

5. Convert the following grammar to Greibach Normal Form (GNF)


S  BS
S  Aa
A  bc
B  Ac

6. Find GNF of the grammar given below (Dec‐2008)


S  ABAb | ab
B  ABA | a
Aa|b

7. Find the GNF equivalent to the CFG


S  AB
A  aA | bB | b
Bb

8. Find a GNF grammar equivalent to the following CFG (May‐2009)

S  BA | ab

B  AB | a

A  Bb | BB

9. Convert the given grammar to GNF (Dec‐


2009)

S  SS | aSb | ab

10. Convert the following grammar into GNF (May‐


2006)
S  XY1 | 0
X  00X | Y

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:

Aa

A  aB

A  Ba

Aɛ

Where, a Є T and A, B Є V.

A language generated by a regular grammar is known as regular language. A regular


grammar could be written in two forms:

1. Right‐linear form
2. Left‐linear form

Right‐Linear Form: A right linear regular grammar will have production of the given
form.

Aa

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ɛ

Note: Variable B in A  aB is the second symbol on the right.

Left‐Linear Form: A left linear regular grammar will have productions of the following
form:

Aa

A  Ba

Aɛ

Note: Variable B in A  Ba is the first symbol on the left.

DFA to Right Linear Regular Grammar:

Every DFA can be described using a set of production using the following steps:

1. Let the DFA, M = (Q, ∑, δ, q0, F)


Let the corresponding right linear grammar be G = (V, T, P, S).

2. Rename q0 Є Q as S Є V, relating start state of M with starting symbol of G.

3. Rena me states of Q as A, B, C, D … where, A, B, C, D … Є V.


4. Creating a set of production P.
a. If q0 Є F then add a production
S  ɛ to P.
b. For every transition of the form,

a
B C

Add a production B  aC, where C is a non‐accepting state.

c. For every transition of the form,

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

Add two productions B  aC, B  a, where C is an accepting state.

DFA to Left Linear Regular Grammar:

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.

Right Linear Grammar to DFA:

Every right linear grammar can be represented using a DFA:

1. A production of the form A  aB will generate a transition

a
A B

for the DFA.

2. A production of the form A  aB | a will generate a transition

a
A B

provided every transition entering B terminates in B.

3. A Production of the form A  ɛ will make A a final 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

4. An independent production of the form A  b, will generate a transition

b
A F

Where, F is a new state and it should be a final state.

Left Linear Grammar to DFA:

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.

1. Draw a transition graph from the given left linear grammar.


2. Reverse the direction of all the transitions.
3. Interchange starting state and the final state.
4. Carry out conversion from FA to DFA.

Right Linear Grammar to Left Linear Grammar:

Right Linear Grammar

Transition Graph

Left Linear Grammar

Fig: From Right Linear Grammar to Left Linear Grammar.

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.

Left Linear Grammar to Right Linear Grammar:

Left Linear Grammar

Transition Graph

Right Linear Grammar

Fig: From Left Linear Grammar to Right Linear Grammar.

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*).

2. Convert the following right‐linear grammar to an equivalent DFA:


S  bB
B  bC

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
Ca
Bb

3. Convert following RG to DFA:


S  0A | 1B
A  0C | 1A | 0
B  1B | 1A | 1
C  0 | 0A

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

7. Convert the following right linear grammar to an equivalent left‐linear grammar:


S  bB | b
B  bC
B  aB
Ca
B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

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)*

19. Describe the language generated by the following grammar:


S  bS | aA | ɛ
A  aA | bB | b
B  bS

20. Find the CFL associated with CFG:


S  0Q | 1P
P  0 | 0S | 1PP
Q  1 | 1S | 0QQ

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

Subjects Taken by Ganesh Sir:


Semester Subject Batch
SEM‐VI SPCC (System Vacation +
Programming and
(Computer) Compiler Construction)
Regular
ALL OCAJP 1.7 Vacation +
(Oracle Certified
Associate JAVA
Regular
Programmer)
ALL OCPJP 1.7 Vacation +
(Oracle Certified
Professional JAVA
Regular
Programmer)

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)

Introduction to Pushdown Automata (PDA): (Dec‐2005)

 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.

 In this chapter, we define two different versions of the pushdown automaton:


1. one that accepts by entering an accepting state, like finite automata do, and
2. another version that accepts by emptying its stack, regardless of the state it is
in.

 Informally, pushdown automata can be viewed as finite automata with stack. An


added stack provides memory and increases the capability of the machine.

 A pushdown automata can do the followings:


1. Read input symbol [as in case of FA].
2. Perform stack operations:
 Push operation
 Pop operation
 Check empty condition of a stack through an initial stack symbol.
 Read top symbol of stack without a pop.
3. Make state changes.

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.

1. A context free language can be recognized by PDA.


2. For every context‐free language, there exists a PDA.
3. The language of PDA is a context‐free language.

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

Fig: Structure of PDA

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.

6. The machine will have two states q0 and q1:


a. State q0 – while the machine is reading a’s.
b. State q1 – while the machine is reading b’s

7. While in state q0 an input ‘a’ is not allowed and hence there is a need for two states.

8. A transition in PDA depends on:


a. Current state
b. Current input
c. Top symbol of the stack

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:

Input symbol, Stack symbol (top most)


Stack operation
qi qj

Fig: A transition from qi to qj

10. A PDA uses three stack operations:


a. Pop operation, it removes the top symbol from the stack.
b. Push operation, it inserts a symbol onto the top of the stack.
c. Nop operation, it does nothing to stack.

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

Fig: PDA for anbn

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.

The Formal Definition of PDA:

Push Down Automata:


 PDA consists of read‐only input tape. In addition it has a stack called Push down
Store (PDS). It is a read‐write push down store as we add elements from PDS.

 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 set of input symbols, also analogous to the corresponding component of a


finite automaton.

Γ: 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.

F: The set of accepting states, or final states.

A Graphical Notation for PDA's


 Sometime, a diagram, generalizing the transition diagram of a finite automaton, will
make aspects of the behavior of a given PDA clearer.

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

 We shall therrefore intrroduce and subsequen


ntly use a transition
t ddiagram fo
or PDA's
in w
which:

1. The nodess correspon


nd to the sttates of thee PDA.

2. An arrow labeled Sttart indicattes the start state, an


nd doubly circled sta
ates are
accepting, as for finitte automataa.

3. The arcs correspond


c d to transiitions of th
he PDA in thet followiing sense. An arc
labeled a, X/α:
X from state q to state p means
m that δ(q , a, X) contains th
he pair
(p, α), perhaps among other p pairs. Thaat is, the arc
a label teells what in nput is
used, and also
a gives the
t old and new tops of the stack k.

Figure: Representting a PDA as a generralized tran


nsition diaggram

 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

Instantaneous Description of a PDA:

Let P = (Q, ∑, Γ, δ, q₀, Z₀, F) be a PDA.

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)

If δ (q, a₁, z₁) contains (q’, β)

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

Input sequence “bacab”


(q₀, bacab, z₀)
(q₀, acab, bz₀)
(q₀, cab, ab z₀)
(q₁, ab, abz₀)
(q₁, b, bz₀)
(q₁, Є, z₀)
(q₁, Є, z₀)
Thus (q₀, bacab, z₀) |‐* (q₂, z₀)

Acceptance by PDA:

PDA by Final State Method:


Definition:
Let P = (Q, ∑, Γ, δ, q₀, Z₀, F) be PDA.
The language accepted by final state is defined as

W Є ∑* | ‐ (q₀,w,z₀) |‐ (qf, Є, α)
qf Є F α Є Γ*

PDA by Null Store Method:


Definition:
Let P = (Q, ∑, Γ, δ, q₀, Z₀, F) be a PDA.
The set accepted by null store (or empty store) is defined as

W Є ∑* | ‐ (q₀,w,z₀) |‐ (qf, Є, Є) for some q Є Q.

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

University questions will be solved in Class:

1. Design a PDA to accept strings of type an bn. (May‐2004)

2. Design a PDA to accept strings of type 0n 1n. (Dec‐2006)

3. Design a PDA to accept strings of type an b2n.

4. Design a PDA to accept strings of type a2n bn.

5. Design a PDA to accept strings of type 0n 12n+1. (Dec‐2002)

6. Design a PDA to accept (bdb)n. (Dec‐2005)

7. Design a PDA to accept (bdb)n cn. (Dec‐2005)

8. Design a PDA which accepts the strings containing equal no. of a’s and b’s.
(Dec‐2002, May‐2003, May‐2004)

9. Design a PDA to accept strings of type (ab)n cn.

10. Design a PDA to accept (ab)n (cd)n. (Jun‐2007)

11. Construct a PDA that accepts the language – {an bm an+m| m, n ≥ 1}

12. Construct a PDA M accepting {an bm an| m, n ≥ 1} by null store.


(Dec‐2002, Dec‐2006)

13. Design a PDA to check for well‐formed ness of parenthesis. (Dec‐2006)

14. Design DPDA accepting balanced string of brackets. (Nov‐2004)

15. Design DPDA to accept strings with more a’s than b’s.

16. Construct a PDA that will recognize the language

L={WCWR | W Є {a, b} * WR – reverse of W }


(Dec‐2003, May‐2005, May‐2007, Jun‐2008)

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 a PDA that will recognize the language

L={ WWR | W Є {a, b}* WR – reverse of W } (even palindrome)


(Dec‐2005, May‐2005, May‐2006, Jun‐2007)

18. Design the PDA to accept the language containing all odd length palindromes over ∑
= {0, 1} (Dec‐2007)

Pushdown Automata and Context – free Languages:

 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

M = ({q₀, q₁, qf}, T, V U {z}, δ, q₀, z, {qf}),

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.

The transition function will include


δ(q₀, Є, z) = {(q₁,Sz)}

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).

To prove that L(M) C_ L(G), let w Є L(M). then by definition.


(q₀, w, z) |‐ (qf, Є, u).

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),

Now let us write w = a₁a₂a3…an. Then the first step in


(q₁, a₁a₂a3…an, Sz) |‐ (q₁, a₂a3…an, u₁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:

University questions will be solved in Class:

1. Construct a PDA equivalent to the following grammar: (Nov‐2004, May‐


2005)
S aAA
A  aS | bS | a

2. Construct the PDA equivalent to the following CFG: (May‐2006, Dec‐


2007)
S  0BB

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.

3. Construct a PDA equivalent to the following grammar”


S  aA
A  aABC | bB | a
Bb
Cc

4. Construct a PDA equivalent to the following grammar: (Dec‐


2003)
S  aSa | bSb | c

5. Construct a PDA equivalent the following grammar:


E  E + E | E * E | (E) | id
(Dec‐2003, May‐2004, Nov‐2004)
6. Design PDA for the following CFG: (Dec‐2009)
S  (S) | SS | ^

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

Subjects Taken by Ganesh Sir:


Semester Subject Batch
SEM‐VI SPCC (System Vacation +
Programming and
(Computer) Compiler Construction)
Regular
ALL OCAJP 1.7 Vacation +
(Oracle Certified
Associate JAVA
Regular
Programmer)
ALL OCPJP 1.7 Vacation +
(Oracle Certified
Professional JAVA
Regular
Programmer)

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.

 The Turing Machine can be thought of as a finite automata connected to read/write


head.

 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.

Turing Machine is a 7‐tuple

M = (Q, Σ, Ѓ, δ, qo, B, F)

Where,

Q‐ finite nonempty set of 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

Σ‐ set of input symbols

Ѓ‐ finite nonempty set of tape symbols

δ‐ transition function mapping the state of finite automaton and tape symbols to
states, tape symbols and movement of head

δ : Q × Г →Q × Ѓ × { L, R, S}

qo‐ initial state of

B‐ special tape symbol representing blank

F‐ set of final states F

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:

An ID of Turing Machine M is a string γβα


Where,
 β is the present state of M.

 The input string is split as γα.

 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

Moves in a Turing Machine

Say δ(q, xi) = (p, y, L)


The input string to be processed is x1 x2…..xn and the present symbol under read/write
head is xi.
So the ID before processing xi is x1x2……….xi‐1q xi…………xn
After processing of xi, the resulting ID is
X1 …….xi‐2pxi‐1 y xi+1……xn

This is represented by,


X1 x2 …..xi‐1 q xi……xn │xi……xi‐2 pxi‐1yxi+1 …….xn

Say δ(q, xi) = (p, y, R)


X1x2 …….xi‐1q xi …..xn │─x1x2 …..xi‐1 xiypxi+1…..xn

Note: The description of moves by IDs is very much useful to represent the processing
of input strings.

Language Acceptability by Turing Machine:

Let us consider the Turing Machine, M= (Q,Σ, Ѓ, δ, qo, B, F)

A string is said to be accepted by M


If qow │‐ α1p α2
Where
P is the final state and
α1 and α2 are tape symbols.

The TM M does not accept w


If the machine M either halts in a non‐accepting state or does not halts.

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:

Turing Machine as language recognizer:

1. Design a Turing machine which recognizes the language


{anbn │ n ≥ 1}

2. Design a Turing machine which recognizes the language


{0n1n │ n ≥ 1}

(May‐2002, May‐2003, Nov‐2004, May‐2005, Dec‐


2009)

3. Design a Turing machine which recognizes the language


{0n1n2n │n ≥ 1}

(Dec‐2002, May‐2003, May‐2006, Jun‐2008)

4. Design a Turing machine which recognizes the language (Dec‐


2002)
{02n1n │n ≥ 1}

5. Design a Turing machine which recognizes the language (Dec‐


2002)
{0n1n0n │n ≥ 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

6. Design a Turing machine which recognizes the language


{anbnan │n ≥ 1}

7. Design a Turing machine which recognizes the language having equal number of a’s
and b’s
OR

Design TM to accept the language –

L = { x Є { 0, 1} *│x contains equal number of 0’s and 1’s }

Simulate the operation for the string 110100.

(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 }

11. Design a Turing machine to check for well‐formed ness of parenthesis


OR

Design a Turing Machine that checks whether a string of left and

right parenthesis is well formed or not.

(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}

13. Design TM to recognize palindromes over Σ = {a, b}

14. Design TM that can accept set of all even palindromes over alphabet (0, 1).
(May‐2006)

15. Design a Turing machine which recognizes the language


L= {WCWR│ W Є {a, b}* WR – reverse of W.

16. Design a Turing machine which recognizes the language


L= {WWR │ W Є {0, 1}* WR – reverse of W.

University questions will be solved in Class:

Turing Machine to compute functions:

1. Design a T.M. to perform addition of two numbers.


(Dec‐2003)

2. Design a .M. to perform multiplication of two numbers.


(May‐2003, May‐2004, Nov‐2004, Jun‐2007, Jun‐2008)

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. Design a T.M. to perform subtraction m – n


Which is defined as

m–n m >n

m≤n 0

4. Construct Turing Machine to subtract two numbers assume m>n


(May‐2006, Dec‐2009)

5. Design T.M. to perform division and find the quotient and remainder.

6. Design T.M. to find n2 (Nov‐


2004)

7. Design T.M. to find n! (Dec‐2003, May‐2004, Dec‐


2008)

8. Design T.M. to find log24 (Dec‐


2003)

9. Design T.M. to find log2n (Dec‐2003, May‐2004, Dec‐


2006)

10. Design TM to perform C = A – B given “$A#B$”.


(May‐2005)

11. Design T.M. to find 2’c of given binary number.

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 detect whether a unary number is divisible by 3.


(May‐2005)

13. Design T.M. to increment binary number by 1.

14. Design TM to covert Unary number to Binary Number

15. Design TM to convert Binary Number to Unary Number

16. Design TM to perform C = A + B


i/p “A + B”

o/p “C”

17. Design TM to decrement binary number by 1.

18. Design TM to add Binary numbers

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.

20. Design TM to perform C = A* B


i/p “A * B”

0/p “C”

21. Design TM to recognize palindromes over the input Σ = {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

22. Design a TM for generating 2n where n is binary. The result should also be binary.
(May‐2004, Jun‐2007, Dec‐2007)

Universal Turing Machine:


 A universal Turing Machine (UTM) is a Turing Machine ™ which is al powerful or
universal in the sense that it is capable of doing anything that any other TM can do.

 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

(Triplet: new state, new symbol to be replaced and direction of move).

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.

The UTM laid the foundation for:

 Stored program computers and


 Interpretative implementation of programming languages.

Variations of Turing Machine:


1. Turing Machine with Two Way Infinite Tape:

A Turing Machine with a two infinite tape is denoted by

M = (Q, Σ, Г, δ, qo, B, F) as in the original model.

- 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.

2. Multitape Turing Machine:

 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.

3. Nondeterministic Turing Machine:

 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

4. Multidimensional Turing Machine:

 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.

5. Multihead Turing Machine:

 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.:

 Two or more Turing Machine can be combined to solve a collection of simpler


problem, so that the output of one TM forms the input to the next TM and so on. This
is called as Composition.

 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.:

In Iterated TM the output it applied to the input repetitively.

8. TM with Semi‐infinite tapes:

 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 Halting Problem:


For a give configuration of a TM case can arise:

 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

Consequences of Halting Problem:

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:

1. Given a particular TM M, we must write a program that acts like M.

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.

Simulating a Computer by a TM:

The following diagram shows how the TM would be designed to simulate a computer.

1. The first tape represents memory of the 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.

5. The scratch tape would be used to compute mathematical operations efficiently.

Refer Class Notes for Examples.

Subjects Taken by Ganesh Sir:


Semester Subject Batch
SEM‐VI SPCC (System Vacation +
Programming and
(Computer) Compiler Construction)
Regular
ALL OCAJP 1.7 Vacation +
(Oracle Certified
Associate JAVA
Regular
Programmer)
ALL OCPJP 1.7 Vacation +
(Oracle Certified
Professional JAVA
Regular
Programmer)

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.

3. Practical problems requiring polynomial time are almost always solvable in an


amount of time that we can tolerate, while those that require exponential time
generally cannot be solved except for small instances.

 In this chapter we introduce the theory of "intractability," that is, techniques for
showing problems not to be solvable in polynomial time.

 We start with a particular problem ‐ the question of whether a boolean expression


can be satisfied, that is, made true for some assignment of the truth values TRUE
and FALSE to its variables.

 Since we are dealing with whether problems can be solved in polynomial time, our
notion of a reduction must change.

 It is no longer sufficient that there be an algorithm to transform instances of one


problem to instances of another.

 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.

 We assume the class of problems that can be solved by nondeterministic TM's


operating in polynomial time includes at least some problems that cannot be solved
by deterministic TM's operating in polynomial time (even if we allow a higher
degree polynomial for the deterministic TM).

The Classes P and NP: (Dec‐2009)

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.

3. Time complexity of an algorithm is defined as a function of the size of the


problem.

4. For comparative study of algorithms, growth rate is considered to be very


important.

5. Size of a problem is often measured in terms of the size of the input.

6. An algorithm with time complexity which can be expressed as a polynomial of


the size of the problem is considered to have an efficient solution.

7. A problem which does not have any polynomial time algorithm is called an
intractable problem, otherwise it is called tractable.

8. A solution by deterministic TM is called an algorithm. A solution by a non‐


deterministic TM may not be an algorithm.

9. For every non‐deterministic TM solution, there is a deterministic TM solution of


a problem. But there is no computation equivalence between deterministic TM
and non‐deterministic TM.
10. If a problem is solvable in polynomial time by non‐deterministic TM then there is
no guarantee that there exists a deterministic TM that can solve it in polynomial
time.
a. If P is set of tractable problem then P is subset of NP. It follows from the
fact that every deterministic TM is a special case of non‐deterministic TM.
b. It is still not known whether P = NP.

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.

2. An interesting aspect of NP‐complete problem is that for each of these problems:


 It has not been possible to design a deterministic TM, so far.
 It has not been possible to establish that a deterministic TM does not exist.

3. There is a large number of problems, for which it is not known whether it is in P or


not in P. However, for each of these problems, it is known that it is in NP.

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.

6. Some of the NP‐complete problems include:


 Satisfiability Problem (SAT)
 Travelling Salesman Problem (TSP)
 Hamiltonian circuit Problem (HCP)
 The Vertex cover Problem (VCP)
 K‐Colourability Problem
 The complete subgraph problem

A Restricted Satisfiability Problem:


1. The satisfiability problem states: Given a Boolean expression, is it satisfiable?
2. A Boolean expression said to be satisfiable if at least one truth assignment makes the
Boolean expression ‘true’.
Example, the Boolean expression (X1 Ʌ X2) ν X3 is true for X1= 1, X2 = 1 and X3 = 0.
Hence it is satisfiable.

3. A Boolean expression involves:


a. Boolean variables X1, X2… Xn, each of these can assume a value either TRUE
or FALSE.
b. Boolean operators:

4. The truth value of a Boolean expression depends on the truth values of its variables.

5. Satisfiability problem is NP‐Complete.

6. SAT is NP‐Complete and it is also known as Cook’s theorem.

7. A Boolean expression in conjunctive normal form is NP‐complete. A Boolean


expression is said to be in CNF, if it is expressed as C1 Ʌ C2 Ʌ C3 Ʌ … Ʌ Ck where
each Ci is a disjunction of the form
Xi1 ν Xi2 ν … ν Xim

Xij is a literal. A literal is either a variable Xi or negation Xi.

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.

2. If P1 be a problem which is already known to be NP‐complete. We want to check whether a


problem P2 is NP‐complete or not. If we can design an algorithm which transforms or
constructs an instance of P2 for each instance of P2, then P2 is also NP‐complete.

3. A method of establishing NP‐completeness of a problem P2 requires designing a polynomial


time reduction algorithm that constructs an instance of P2 for each instance of P1, where P1
is already known to be NP‐complete.

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.

5. Complements of Languages in NP:


a. It is not known whether NP is closed under complementation. The class of languages
P is closed under complementation. It is believed that whenever language L is NP‐
complete, its complement is not in NP.
b. There is a set of languages whose complements are in NP. Such languages are called
Co‐NP.
c. We believe that complement of NP‐complete problem is not in NP.
d. No NP‐complete problem is in Co‐NP.
e. We believe that the complements of NP‐complete problems, which are in Co‐NP are
not in NP.

Language Classes Based on Randomization:


1. A randomized algorithm uses a random number generator. Decisions made in such
algorithms depend on the output of random number generator. The output of a randomized
algorithm is unpredictable and it may differ from run to run for the same input.

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

5. RP stands for randomized polynomial‐time algorithm. ZPP stands for zero‐error


probabilistic probabilistic polynomial algorithm. The class ZPP is based on a randomized TM
that always halts in polynomial time.

Complexity of Primality Testing:


1. An integer number is prime if it is divisible only by 1 and itself. An algorithm can be written
to test whether a number is prime or not. These algorithms are found to be in following
classes:
a. Np
b. Co‐NP
c. RP
2. Given in integer n, the problem of deciding whether n is prime is known as primality testing.

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).

5. A randomized algorithm based on Fermat’s theorem will have a time complexity of


O(log2n).

Questions:

1. Explain classes of complexity with example. (Dec‐2009)


2. Write notes on complements of languages in P. (Dec‐2009)

Subjects Taken by Ganesh Sir:


Semester Subject Batch
SEM‐VI SPCC (System Vacation +
Programming and
(Computer) Compiler Construction)
Regular
ALL OCAJP 1.7 Vacation +
(Oracle Certified
Associate JAVA
Regular
Programmer)
ALL OCPJP 1.7 Vacation +
(Oracle Certified
Professional JAVA
Regular
Programmer)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy