Toc M-1
Toc M-1
Toc M-1
MODULE-1
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|).
Example: If S = ‘cabcad’, |S|= 6
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,………..}
Prefix of a string
Definition: It is the string formed by taking any number of symbols of the string.
Example: String w= 0111
Prefix of a string for 0111 λ, 0, 01,011 0111
Substring
Definition: It is the string formed by taking any number of symbols of the string.
THEORY OF COMPUTATION
Problems:
1) Construct the Language generated from the given grammar SaS/ε.
Sol: SaS
SaaS (Replace S by S aS)
Saa (Replace S by S ε)
n
If we repeat this process by replacing with S with SaS and ε, we get a steps.
n
So, L(G) = a , n>=0.
2) Construct the Language generated from the grammar S aSb/ ε.
Sol: SaSb
SaaSbb
SaaaSbbb
Saaabbb
If we repeat this process, we get equal number of a’s followed by equal number of b’s.
n n
So, L(G) = a b , n ≥ 0
3) Construct the language generated by the grammar SaCa, CaCa/b.
Sol: SaCa
SaaCaa
SaaaCaaa
Saaabaaa
n n
If we repeat this process, the language generated by grammar is L(G)= a ba , n > 0
4) Construct the grammar for palindrome of binary numbers
Sol: G={V,T,P,S}
V={S}
T={0,1}
P: S0S0/1S1/1/0/ ε
S is start state
n n+1
5) Construct Grammar for the Language a b ,n > 0
Sol: V={S}
THEORY OF COMPUTATION
T={a,b}
P: SaSb/abb
S is start state
Chomsky Hierarchy of Languages
According to Chomsky, 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 −
Take a look at the following illustration. It shows the scope of each type of grammar −
THEORY OF COMPUTATION
Type 0:
Type-0 grammars generate recursively enumerable languages. The productions
have no restrictions. They are any phase structure grammar including all formal
grammars. They generate the languages that are recognized by a Turing machine.
The productions can be in the form of α → β where α is a string of terminals and non-
terminals with at least one non-terminal and α cannot be null. β is a string of terminals
and non-terminals.
Example: Sab –> ba
Finite automata:
A finite automaton is a mathematical model of a system with discrete inputs and
outputs. Finite means number of possible states and number of symbols in alphabet
are both finite and automaton becomes change of state.
Formal definition of a Finite Automaton
FA 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 of the automaton.
δ is the transition function.
q0 is the initial state
F is a set of final state/states of Q
Applications of finite automata:
Designing and checking the behavior of digital circuits using software.
Compiler design: During compilation, Lexical analyzers breaks the input text into
various lexical units such as identifiers, Keywords and punctuations.
Designing software for scanning large bodies of text such as collection of web
pages
Designing software for verifying systems having finite number of states
Software for natural language processing
Types of finite automata
Finite Automata can be classified into two types −
Deterministic Finite Automata (DFA)
Non-deterministic Finite Automata ( NFA)
Deterministic finite automata:
Deterministic Finite Automaton is a FA in which there is only one path for a
specific input from current state to next state.
Tuples: (Q,∑, 𝛿 , q0,F)
Q is a finite set of states.
∑ is a finite set of input alphabets
δ is the transition function. 𝛿 : Q * ∑ Q
THEORY OF COMPUTATION
M = (Q, Σ, δ, q0,f )
Q = {q0,q1,q2}
Σ = {0,1}
q0start state
f ={q2}
δ 0 1
q0 q1 q0
q1 q2 q0
q2 q2 q0
3) Design DFA to accept strings of 0’s and 1’s starting with string 01. Check whether the
string 01101 and 00011 is acceptable or not.
Sol:
5) Design DFA to accept strings of 0’s and 1’s having atleast one ‘0’.
Sol:
6) Design DFA to accept a) even no. of 0’s and even no. of 1’s
b) odd no. of 0’s and even no. of 1’s
c) even no. of 0’s and odd no. of 1’s
d) odd no. of 0’s and odd no. of 1’s
Sol:
a) Even no. of 0’s and even no. of 1’s
δ 0 1
q0 q2 q1
q1 q3 q0
q2 q0 q3
q3 q1 q2
Sol:
δ(q0,110101) = δ(δ(q0,1), 10101) = δ(q1,10101) = δ(δ(q1,1),0101) = δ(q0,0101) =
δ(δ(q0,0),101) = δ(q2,101) = δ(δ(q2,1),01) = δ(q3, 01) = δ(δ(q3,0),1) = δ(q1,1) = q0
(accept)
DFA NFA
1) Deterministic Finite Automaton 1) NFA or Non Deterministic Finite
is a FA in which there is only Automaton is the one in which
one path for a specific input there exists many paths for a
from current state to next specific input from current
state. state to next state.
Tuples: (Q,∑, 𝛿 , q0,F) Tuples: (Q,∑, 𝛿 , q0,F)
𝛿 : Q * ∑ Q 𝛿 :Q*∑2
Q
δ δ(q,0) δ(q,1)
a {a,b,c,d,e} {d,e}
b {c} {e}
c ∅ {b}
d {e} ∅
e ∅ ∅
Using the above algorithm, we find its equivalent DFA. The state table of the DFA is
shown in below.
δ δ(q,0) δ(q,1)
THEORY OF COMPUTATION
[d,e] [e] ∅
[e] ∅ ∅
[c, e] ∅ [b]
[c] ∅ [b]
ε -NFA
Nondeterministic finite automaton with ε-moves (NFA-ε) is a further
generalization to NFA. This automaton replaces the transition function with the
one that allows the empty string ε as a possible input. The transitions without
consuming an input symbol are called ε-transitions.
Formal definition NFA-ε
An NFA-ε is represented formally by a 5-tuple, (Q, Σ, 𝛿 , q0, F), consisting of
Q finite set of states
Σfinite set of input alphabets
Q
𝛿 transition function : Q × (Σ ∪ {ε}) → 2
q0 an start state
Ffinal state
Problems
1) Convert the following ε-NFA to its equivalent DFA
'
δ (q0, b) = ε- Closure (𝛿 (ε- Closure (q0),b))
= ε- Closure (𝛿 (q0, q1, q2),b)
THEORY OF COMPUTATION
'
δ (q0, c) = ε- Closure (𝛿 (ε- Closure (q0),c))
= ε- Closure (𝛿 (q0, q1, q2),c)
= ε- Closure (𝛿 (q0, c) ∪ 𝛿 (q1, c) ∪ 𝛿 (q2, c))
= ε- Closure (q2)
= { q2}
For state q1
'
δ (q1, a) = ε- Closure (𝛿 (ε- Closure (q1),a))
= ε- Closure (𝛿 (q1, q2),a)
= ε- Closure (𝛿 (q1, a) ∪ 𝛿 (q2, a))
= ε- Closure (ᴓ)
=ᴓ
'
δ (q1, b) = ε- Closure (𝛿 (ε- Closure (q1),b))
= ε- Closure (𝛿 (q1, q2),b)
= ε- Closure (𝛿 (q1, b) ∪ 𝛿 (q2, b))
= ε- Closure (q1)
= { q1, q2}
'
δ (q1, c) = ε- Closure (𝛿 (ε- Closure (q1),c))
= ε- Closure (𝛿 (q1, q2),c)
= ε- Closure (𝛿 (q1, c) ∪ 𝛿 (q2, c))
= ε- Closure (q2)
= { q2}
For state q2
THEORY OF COMPUTATION
'
δ (q2, a) = ε- Closure (𝛿 (ε- Closure (q2),a))
= ε- Closure (𝛿 (q2),a)
=ᴓ
'
δ (q2, b) = ε- Closure (𝛿 (ε- Closure (q2),b))
= ε- Closure (𝛿 (q2),b)
= ε- Closure (ᴓ)
=ᴓ
'
δ (q2, c) = ε- Closure (𝛿 (ε- Closure (q2),c))
= ε- Closure (𝛿 (q2),c)
= ε- Closure (q2)
= { q2}
Consider A = { q0, q1, q2}
B = { q1, q2}
C= { q2}
Transition table:
𝛿 A B C
*A A B C
*B ᴓ B B
*C ᴓ ᴓ C
Mealy machine:
A Mealy Machine is an FSM whose output depends on the present state as well
as the present input.
A Mealy machine can be described by a 6 tuple (Q, ∑, δ, q0, λ, Δ) where −
Q is a finite set of states.
∑ is a finite set of symbols called the input alphabet.
Δ is a finite set of symbols called the output alphabet.
δ is the input transition function where δ: Q × ∑ → Q
λ is the output transition function where Q x ∑→ Δ
q0 is the initial state.
Problems:
1)Design moore and mealy machine which determines the residue mod 3 for
each binary string
Sol: Moore machine
Mealy machine
THEORY OF COMPUTATION
2) Design moore and mealy machine for 1's complement of a binary number.
Mealy machine
3) Consider the following mealy machine and find the output for the input string
aaabb
THEORY OF COMPUTATION
Sol: Given the input string aaabb, the output string is 01110
Sol:
Q = {q0, q1, q2, q3}
∑ = {0, 1}
Δ ={0, 1}
For state q0
'
λ (q0,0) = λ (δ(q0,0)) = λ(q1) =0
'
λ (q0,1) = λ (δ(q0,1)) = λ(q2) =1
THEORY OF COMPUTATION
For state q1
'
λ (q1,0) = λ (δ(q1,0)) = λ(q3) =1
'
λ (q1,1) = λ (δ(q1,1)) = λ(q2) =1
For state q2
'
λ (q2,0) = λ (δ(q2,0)) = λ(q2) =1
'
λ (q2,1) = λ (δ(q2,1)) = λ(q1) =0
For state q3
'
λ (q3,0) = λ (δ(q3,0)) = λ(q0) =1
'
λ (q3,1) = λ (δ(q3,1)) = λ(q3) =1
Transition table:
q0 q3 0 q1 1
q1 q0 1 q3 0
q2 q2 1 q2 0
q3 q1 0 q0 1
Sol:
Step 1: If the output of the state in both λ1 and λ2 are same, then the state
remains same. For example, in the state q0, the output of λ1 and λ2 is 0. So, there
is no need to change q0.
Step 2: If the output of the state in both λ1 and λ2 are not same, then split the
state into two states. For example, in the state q2, the output of λ1 and λ2 is 1 and
0. So, split q2 as q20 and q21.
Step 3: The output of q1 and q2 are not same. So, split these states.
δ Input 0 Output Input 1 Output
q0 q3 0 q11 1
q10 q0 1 q3 0
q11 q0 1 q3 0
q3 q10 0 q0 1
Step 4:
Now, convert the above mealy machine to moore machine
q0 q3 q11 1
q10 q0 q3 0
q11 q0 q3 1
q3 q10 q0 0
Transition diagram
THEORY OF COMPUTATION
0 1 0
A B C D
1 0 1
0 1
1 1
E F G 0
0 1 0
Sol:
Step 1: D is unreachable state. So, remove D and proceed to step 2
Step 2: First, mark pairs of final and non-final states.
B
C X X
E X
F X
G X
H X
A B C E F G
Step 3: In the first iteration examine all unmarked pairs. For example, for
pair A,B we get δ(A,1)=F and δ(B,1)=C and the pair C,F is marked, so mark
A,B too. After doing this for all pairs we get
B X
C X X
E X X
THEORY OF COMPUTATION
F X X X X
G X X X
H X X X X X
A B C E F G
B X
C X X
E X X
F X X X X
G X X X X X
H X X X X X
A B C E F G
Step 5: Here, no new distinguishable pairs will be discovered and now group the
states to equivalence classes. Since, {A,E} and {B,H} are equivalent, the classes
are {A,E}, {B,H}, {C}, {F}, {G}. The minimized automaton is: