CC-7-UNIT-4
CC-7-UNIT-4
Related Terminologies
Alphabet
Definition − An alphabet is any finite set of symbols.
Example − ∑ = {a, b, c, d} is an alphabet set where ‘a’, ‘b’, ‘c’, and ‘d’
are symbols.
String
Definition − A string is a finite sequence of symbols taken from ∑.
Example − ‘cabcad’ is a valid string on the alphabet set ∑ = {a, b, c, d}
Length of a String
Definition − It is the number of symbols present in a string. (Denoted by |S|).
Examples −
If S = ‘cabcad’, |S|= 6
If |S|= 0, it is called an empty string (Denoted by λ or ε)
Kleene Star
Definition − The Kleene star, ∑*, is a unary operator on a set of symbols or
strings, ∑, that gives the infinite set of all possible strings of all possible
lengths over ∑ including λ.
Representation − ∑* = ∑0 ∪ ∑1 ∪ ∑2 ∪……. where ∑p is the set of all possible
strings of length p.
Example − If ∑ = {a, b}, ∑* = {λ, a, b, aa, ab, ba, bb,………..}
Kleene Closure / Plus
Definition − The set ∑+ is the infinite set of all possible strings of all possible
lengths over ∑ excluding λ.
Representation − ∑+ = ∑1 ∪ ∑2 ∪ ∑3 ∪…….
∑+ = ∑* − { λ }
Example − If ∑ = { a, b } , ∑+ = { a, b, aa, ab, ba, bb,………..}
Manas Ku Mishra, Asst. Prof. of Comp. Sc., FM (A) College, BLS. Page 1 of 15
Language
Definition − A language is a subset of ∑* for some alphabet ∑. It can be finite
or infinite.
Example − If the language takes all possible strings of length 2 over ∑ = {a,
b}, then L = { ab, aa, ba, bb }
Finite Automaton can be classified into two types −
Deterministic Finite Automaton (DFA)
Non-deterministic Finite Automaton (NFA)
Deterministic Finite Automaton (DFA): In DFA, for each input symbol, one can
determine the state to which the machine will move. Hence, it is called Deterministic
Automaton. As it has a finite number of states, the machine is called Deterministic
Finite Machine or Deterministic Finite Automaton.
Formal Definition of a DFA
A DFA can be represented by a 5-tuple (Q, ∑, δ, q0, F) where −
Q is a finite set of states.
∑ is a finite set of symbols called the alphabet.
δ is the transition function where δ: Q × ∑ → Q
q0 is the initial state from where any input is processed (q0 ∈ Q).
F is a set of final state/states of Q (F ⊆ Q).
Graphical Representation of a DFA:
A DFA is represented by digraphs called state diagram.
The vertices represent the states.
The arcs labeled with an input alphabet show the transitions.
The initial state is denoted by an empty single incoming arc.
The final state is indicated by double circles.
Manas Ku Mishra, Asst. Prof. of Comp. Sc., FM (A) College, BLS. Page 3 of 15
Difference between DFA and NFA
DFA NFA
only one state transition (next state) in can be multiple state transitions (next
DFA. states) in NFA.
DFA cannot use Empty String transition. NFA can use Empty String transition.
DFA is more difficult to construct. NFA is easier to construct.
Time needed for executing an input Time needed for executing an input
string is less. string is more.
DFA requires more space. NFA requires less space then DFA.
NFA to DFA Conversion: Let X = (Qx, ∑, δx, q0, Fx) be an NFA which accepts the
language L(X). We have to design an equivalent DFA Y = (Qy, ∑, δy, q0, Fy) such
that L(Y) = L(X). The following procedure converts the NFA to its equivalent DFA −
Algorithm
Input − An NFA
Output − An equivalent DFA
Step 1 − Create state table from the given NFA.
Step 2 − Create a blank state table under possible input alphabets for the
equivalent DFA.
Step 3 − Mark the start state of the DFA by q0 (Same as the NFA).
Step 4 − Find out the combination of States {Q0, Q1,... , Qn} for each possible input
alphabet.
Step 5 − Each time we generate a new DFA state under the input alphabet
columns, we have to apply step 4 again, otherwise go to step 6.
Step 6 − The states which contain any of the final states of the NFA are the final
states of the equivalent DFA.
Example: Let us consider the NFA shown in the figure below. Find its equivalent
DFA.
Manas Ku Mishra, Asst. Prof. of Comp. Sc., FM (A) College, BLS. Page 4 of 15
(State diagram) (Transition Table)
q δ(q,0) δ(q,1)
a {a,b,c,d,e} {d,e}
b {c} {e}
c ∅ {b}
d {e} ∅
e ∅ ∅
Solution:
(Transition Table) (State diagram)
q δ(q,0) δ(q,1)
[a] [a,b,c,d,e] [d,e]
[a,b,c,d,e] [a,b,c,d,e] [b,d,e]
[d,e] [e] ∅
[b,d,e] [c,e] [e]
[e] ∅ ∅
[c, e] ∅ [b]
[b] [c] [e]
[c] ∅ [b]
∅ ∅ ∅ 1 0,1 0
0,1 ∅
0
Algorithm:
Step 1 − All the states Q are divided in two partitions − final states and non-final
states and are denoted by P0. All the states in a partition are 0th equivalent. Take a
counter k and initialize it with 0.
Step 2 − Increment k by 1. For each partition in Pk, divide the states in Pk into two
partitions if they are k-distinguishable. Two states within this partition X and Y are k-
distinguishable if there is an input S such that δ(X, S) and δ(Y, S) are (k-1)-
distinguishable.
Step 3 − If Pk ≠ Pk-1, repeat Step 2, otherwise go to Step 4.
Step 4 − Combine kth equivalent sets and make them the new states of the
t reduced
DFA.
As the states c, d and e moves to same state on particular input, the states c, d and
e are equivalent states. Similarly, states a and b are also equivalent. So the
minimized DFA is as follows.
q δ(q,0) δ(q,1)
(a, b) (a, b) (c,d,e)
(c,d,e) (c,d,e) f
f f f
Example-1: Suppose, L (G) = {am bn | m ≥ 0 and n > 0}. Find out the
grammar G which produces L(G).
Solution: Since L(G) = {am bn | m ≥ 0 and n > 0}, the set of strings accepted can be
rewritten as −
L(G) = {b, ab,bb, aab, abb, …….}
Here, the start symbol has to take at least one ‘b’ preceded by any number of ‘a’
including null.
To accept the string set {b, ab, bb, aab, abb, …….}, we have taken the productions
S → aS, S → B, B → b, B → bB
S → B → b (Accepted)
S → B → bB → bb (Accepted)
S → aS → aB → ab (Accepted)
S → aS → aaS → aaB → aab(Accepted)
S → aS → aB → abB → abb (Accepted)
Thus, we can prove every single string in L(G) is accepted by the language
generated by the production set.
Hence the grammar, G: ({S, A, B}, {a, b}, S, { S → aS | B , B → b | bB })
Example-2: Suppose, L (G) = {am bn | m > 0 and n ≥ 0}. Find out the grammar G
which produces L(G).
Solution − Since L(G) = {am bn | m > 0 and n ≥ 0}, the set of strings accepted can
be rewritten as −
L(G) = {a, aa, ab, aaa, aab ,abb, …….}
Here, the start symbol has to take at least one ‘a’ followed by any number of ‘b’
including null.
To accept the string set {a, aa, ab, aaa, aab, abb, …….}, we have taken the
productions −
S → aA, A → aA, A → B, B → bB, B → λ
S → aA → aB → aλ → a (Accepted)
S → aA → aaA → aaB → aaλ → aa (Accepted)
S → aA → aB → abB → abλ → ab (Accepted)
S → aA → aaA → aaaA → aaaB → aaaλ → aaa (Accepted)
S → aA → aaA → aaB → aabB → aabλ → aab (Accepted)
S → aA → aB → abB → abbB → abbλ → abb (Accepted)
Manas Ku Mishra, Asst. Prof. of Comp. Sc., FM (A) College, BLS. Page 8 of 15
Thus, we can prove every single string in L(G) is accepted by the language
generated by the production set.
Hence the grammar, G: ({S, A, B}, {a, b}, S, {S → aA, A → aA | B, B → λ | bB })
Chomsky Classification of Grammars: According to Noam Chomosky, there are
four types of grammars − Type 0, Type 1, Type 2, and Type 3. The following table
shows how they differ from each other −
Grammar
Grammar Accepted Language Accepted Automaton
Type
Recursively enumerable
Type 0 Unrestricted grammar Turing Machine
language
Context-sensitive Context-sensitive Linear-bounded
Type 1
grammar language automaton
Context-free
Type 2 Context-free language Pushdown automaton
grammar
Finite state
Type 3 Regular grammar Regular language
automaton
The following figure shows the scope of each type of grammar –
Manas Ku Mishra, Asst. Prof. of Comp. Sc., FM (A) College, BLS. Page 9 of 15
Example
X → a | aY | ε
Y→b
Manas Ku Mishra, Asst. Prof. of Comp. Sc., FM (A) College, BLS. Page 10 of 15
X . Y is a Regular Expression corresponding to the language L(X) .
L(Y) where L(X.Y) = L(X) . L(Y)
R* is a Regular Expression corresponding to the
language L(R*)where L(R*) = (L(R))*
If we apply any of the rules several times from 1 to 4, they are Regular Expressions.
Examples of some Regular Expressions
Regular Regular Set
Expressions
(0 + 10*) L = { 0, 1, 10, 100, 1000, 10000, … }
(0*10*) L = {1, 01, 10, 010, 0010, …}
(0 + ε)(1 + ε) L = {ε, 0, 1, 01}
(a+b)* Set of strings of a’s and b’s of any length including the null string.
So L = { ε, a, b, aa , ab , bb , ba, aaa…….}
(a+b)*abb Set of strings of a’s and b’s ending with the string abb. So L =
{abb, aabb, babb, aaabb, ababb, …………..}
(11)* Set consisting of even number of 1’s including empty string, So L=
{ε, 11, 1111, 111111, ……….}
(aa)*(bb)*b Set of strings consisting of even number of a’s followed by odd
number of b’s , so L = {b, aab, aabbb, aabbbbb, aaaab, aaaabbb,
…………..}
(aa + ab + ba + String of a’s and b’s of even length can be obtained by
bb)* concatenating any combination of the strings aa, ab, ba and bb
including null, so L = {aa, ab, ba, bb, aaab, aaba, …………..}
Regular Sets: Any set that represents the value of the Regular Expression is called
a Regular Set.
Identities Related to Regular Expressions: Given P, Q, R, L as regular
expressions, the following identities hold −
∅* = ε
ε* = ε
RR* = R*R
R*R* = R*
(R*)* = R*
(PQ)*P =P(QP)*
(a+b)* = (a*b*)* = (a*+b*)* = (a+b*)* = a*(ba*)*
R + ∅ = ∅ + R = R (The identity for union)
R ε = ε R = R (The identity for concatenation)
∅L=L∅=∅
Manas Ku Mishra, Asst. Prof. of Comp. Sc., FM (A) College, BLS. Page 11 of 15
R + R = R (Idempotent law)
L (M + N) = LM + LN (Left distributive law)
(M + N) L = ML + NL (Right distributive law)
ε + RR* = ε + R*R = R*
Method
Step 1 Construct an NFA with Null moves from the given regular expression.
Step 2 Remove Null transition from the NFA and convert it into its equivalent DFA.
Problem: Convert the RE, 1 (0 + 1)* 0 into its equivalent DFA.
Solution: We will concatenate three expressions "1", "(0 + 1)*" and "0"
Manas Ku Mishra, Asst. Prof. of Comp. Sc., FM (A) College, BLS. Page 12 of 15
Finite Automata with Null Moves (NFA-ε):
(NFA A Finite Automaton with null moves
(FA-ε)
ε) does transit not only after giving input from the alphabet set but also without
any input symbol. This transition without input is called a null move.
An NFA-εε is represented formally by a 5-tuple (Q, ∑, δ, q0, F), consisting of
q0 − an initial state q0 ∈ Q
F − a set of final state/states of Q (F
(F⊆Q).
The above (FA-ε) accepts a string set − {0, 1, 01}
Removal of Null Moves from Finite Automata
Automata: If in an NFA, there is ϵ-move
between vertex X to vertex Y, we can remove it using the following steps −
Find all the outgoing edges from Y.
Copy all these edges starting from X without changing the edge labels.
If X is an initial state, make Y also an initial state.
If Y is a final state, make X also a final state.
Solution
Step 1 −
Here the ε transition is between q1 and qf, so let q1 is X and qf is Y.
Y
Here the outgoing edges from qf is to qf for inputs 0 and 1.
Step 2 −
Now we will Copy all these
edges from q1 without
changing the edges from
qf and get the FA as shown
in the figure.
Step 3 −
Here q1 is an initial state, so
we make qf also an initial
state.
So the FA becomes −
Step 4 −
Here qf is a final state, so we
make q1 also a final state.
So the FA becomes −
Pumping Lemma for Regular Language:
Theorem: Let L be a regular language. Then there exists a constant ‘n’ such that for
every string w in L, |w| ≥ n
We can break w into three strings, w = xyz, such that −
|y| ≥ 1
|xy| ≤ n
For all k ≥ 0, the string xykz is also in L.
Applications of Pumping Lemma: Pumping Lemma is to be applied to show
that certain languages are not regular. It should never be used to show a language
is regular.
If L is regular, it satisfies Pumping Lemma.
If L does not satisfy Pumping Lemma, it is non-regular.
Method to prove that a language L is not regular:
At first, we have to assume that L is regular.
So, the pumping lemma should hold for L.
Use the pumping lemma to obtain a contradiction −
Select w such that |w| ≥ n
Select y such that |y| ≥ 1
Select x such that |xy| ≤ n
Assign the remaining string to z.
Select k such that the resulting string is not in L.
Hence L is not regular.
Problem: Prove that L = {aibi | i ≥ 0} is not regular.
Solution −
At first, we assume that L is regular.
Let w = anbn. Thus |w| = 2n ≥ n.
By pumping lemma, let w = xyz, where |xy| ≤ n.
Let x = ap, y = aq, and z = bn, where p + q = n, p ≠ 0, q ≠ 0. Thus |y| ≠ 0.
Let k = 2. Then xy2z = apa2qbn.
Number of a = (p + 2q) = (p + q) + q = n + q
Hence, xy2z = an+q bn. Since q ≠ 0, xy2z is not of the form anbn.
Thus, xy2z is not in L. Hence L is not regular.
Manas Ku Mishra, Asst. Prof. of Comp. Sc., FM (A) College, BLS. Page 15 of 15