Toc M-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

THEORY OF COMPUTATION

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

 Example: String w= 0111


Prefix of a string for 0111 λ, 0, 01,011 0111

Proper Prefix of a string


 Definition: Any Prefix of the string other than the string itself.
 Example: String w= 0111
Proper Prefix of a string for 0111 λ, 0, 01,011
Suffix of a string
 Definition: It is the string formed by taking any number of symbols from the end
of the string.
 Example: String w= 0110
Prefix of a string for 0111 λ, 0, 10, 110, 0110.
Proper Suffix of a string
 Definition: Any Suffix of the string other than the string itself.
 Example: String w= 0110
Prefix of a string for 0111 λ, 0, 10,110.
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, bb, ba, bb}
 Operations on Languages:
 Union
 Concatenation
 Intersection
 Difference
Grammar
 Definition: Grammar is a set of rules in any language. It consists of 4 tuples
G={V,T,P,S}. V is set of non-terminals, T is set of terminals, P is production
rules and s is start state.
 Example: AaA. Here, a is terminal and A is non-terminal.
THEORY OF COMPUTATION

Problems:
1) Construct the Language generated from the given grammar SaS/ε.
Sol: SaS
SaaS (Replace S by S aS)
Saa (Replace S by S ε)
n
If we repeat this process by replacing with S with SaS and ε, we get a steps.
n
So, L(G) = a , n>=0.
2) Construct the Language generated from the grammar S aSb/ ε.
Sol: SaSb
SaaSbb
SaaaSbbb
Saaabbb
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 SaCa, CaCa/b.
Sol: SaCa
SaaCaa
SaaaCaaa
Saaabaaa
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: S0S0/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: SaSb/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 −

Grammar Grammar Language Accepted Automaton


Type Accepted

Type 0 Unrestricted Recursively Turing Machine


grammar enumerable
language

Type 1 Context-sensitive Context-sensitive Linear-bounded


grammar language automaton

Type 2 Context-free Context-free Pushdown


grammar language automaton

Type 3 Regular grammar Regular language Finite state


automaton

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

Type 1: Type-1 grammars generate the context-sensitive languages. The language


generated by the grammar is recognized by the Linear Bound Automata. In Type 1, a)
first of all Type 1 grammar should be type 0. b) Grammar production should be in the
form αβ and |α|<=|β| i.e., count of symbol in α is less than or equal to β.
Example: SAB
ABabc
Bb
Type 2: Type-2 grammars generate the context-free languages. In Type 2, a) first of all
it should be in type. b) LHS should have only one variable |α| = 1 and There is no
restriction on β.
Example: S AB
Aa
Bb
Type 3: Type-3 grammars generate regular languages. Type-3 grammars must have a
single non-terminal on the left-hand side and a right-hand side consisting of a single
terminal or single terminal followed by a single non-terminal.
Example: AaA
Aa
THEORY OF COMPUTATION

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

 q0 is the initial state


 F is a set of final state/states of Q
Problems

1) Design a DFA that string end with 00?


Sol:

M = (Q, Σ, δ, q0,f )
Q = {q0,q1,q2}
Σ = {0,1}
q0start state
f ={q2}
δ 0 1
q0 q1 q0
q1 q2 q0
q2 q2 q0

2) Design DFA with substring 00?


THEORY OF COMPUTATION

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:

δ(q0, 01101) = δ(δ(q0,0),1101) = δ(q1,1101) = δ(δ(q1,1),101) = δ(q2,101) =


δ(δ(q2,1),01) = δ(q2,01) = δ(δ(q2,0),1) = δ(q2,1) = q2 (01101 is accepted).
4) Design DFA to accept string of 0’s and 1’s having exactly one ‘0’.
Sol:
THEORY OF COMPUTATION

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

b) odd no. of 0’s and even no. of 1’s


THEORY OF COMPUTATION

c) even no. of 0’s and odd no. of 1’s

d) odd no. of 0’s and odd no. of 1’s


THEORY OF COMPUTATION

7) Check whether the strings 110101 is accepted by the following FSM.

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

Non-deterministic finite automata:


NFA or Non Deterministic Finite Automaton is the one in which there exists many
paths 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
Q
 δ is the transition function. 𝛿 : Q * ∑  2
 q0 is the initial state
THEORY OF COMPUTATION

 F is a set of final state/states of Q


Problems
1) Design NFA accepting all strings ending with 01.
Sol:

2) Construct NFA in which double ‘1’ is followed by double ‘0’


Sol:

3) Construct NFA for the language L = {0101n}


Sol:
THEORY OF COMPUTATION

Difference between DFA and NFA

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

2) DFA is difficult to construct. There 2) NFA can be used in theory of


is a unique transition on each input computation because they are more
symbol. flexible and easier to use than DFA.
3) Backtracking is allowed in DFA 3) In NFA, backtracking is not always
possible.
4) Requires more space. 4) Requires less space.

Conversion of NFA to DFA:

1) Let us consider the NFA shown in the figure below.


THEORY OF COMPUTATION

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

[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]

The state diagram of the DFA is as follows −


THEORY OF COMPUTATION

ε -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
 Ffinal state
Problems
1) Convert the following ε-NFA to its equivalent DFA

Sol: ε- Closure (q0) = { q0, q1, q2}


ε- Closure (q1) = { q1, q2}
ε- Closure (q2) = { q2}
For state q0
'
δ (q0, a) = ε- Closure (𝛿 (ε- Closure (q0),a))
= ε- Closure (𝛿 (q0, q1, q2),a)
= ε- Closure (𝛿 (q0, a) ∪ 𝛿 (q1, a) ∪ 𝛿 (q2, a))
= ε- Closure (q0)
= { q0, q1, q2}

'
δ (q0, b) = ε- Closure (𝛿 (ε- Closure (q0),b))
= ε- Closure (𝛿 (q0, q1, q2),b)
THEORY OF COMPUTATION

= ε- Closure (𝛿 (q0, b) ∪ 𝛿 (q1, b) ∪ 𝛿 (q2, b))


= ε- Closure (q1)
= { q1, q2}

'
δ (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

Finite automata with Output


There are two types of finite automata with output. They are a) Moore
machine and 2) Mealy machine.
Moore machine: Moore machine is an FSM whose outputs depend on only the
present state. A Moore 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
THEORY OF COMPUTATION

 λ is the output transition function where Q → Δ


 q0 is the initial state.

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.

Sol: Moore machine

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

4) Convert the moore machine to mealy machine


Present Input 0 Input 1 Output
state
q0 q1 q2 1
q1 q3 q2 0
q2 q2 q1 1
q3 q0 q3 1

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:

Δ Input 0 Output Input 1 Output


q0 q1 0 q2 1
q1 q3 1 q2 1
q2 q2 1 q1 0
q3 q0 1 q3 1
Transition diagram:
THEORY OF COMPUTATION

5) Convert the following mealy machine to moore machine


δ Input 0 Output Input 1 Output

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

q20 q21 1 q20 0


THEORY OF COMPUTATION

q21 q21 1 q20 0

q3 q10 0 q0 1

Step 4:
Now, convert the above mealy machine to moore machine

δ Input 0 Input 1 Output

q0 q3 q11 1

q10 q0 q3 0

q11 q0 q3 1

q20 q21 q20 0

q21 q21 q20 1

q3 q10 q0 0

6) Convert the following mealy machine to moore machine


THEORY OF COMPUTATION

Sol: Q = {[q0,n], [q0,y], [q1,n], [q1,y], [q2,n], [q2,y]}


Δ = {n, y}
∑ = {0, 1}
Consider state [q0,n]
δ' ([q0, n], 0) = (δ[q0, 0], λ[q0,0]
= [q1, N]
δ' ([q0, n], 1) = (δ[q0, 1], λ[q0,1]
= [q2, N]
'
λ [(q0, n)] = n

Consider state [q0,y]


δ' ([q0, y], 0) = (δ[q0, 0], λ[q0,0]
= [q1, n]
δ' ([q0, y], 1) = (δ[q0, 1], λ[q0,1]
= [q2, n]
'
λ [(q0, y)] = y
Consider state [q1,n]
δ' ([q1, n], 0) = (δ[q1, 0], λ[q1,0]
= [q1, y]
δ' ([q1, n], 1) = (δ[q1, 1], λ[q1,1]
= [q2, n]
'
λ [(q1, n)] = n
Consider state [q1,y]
δ' ([q1, y], 0) = (δ[q1, 0], λ[q1,0]
= [q1, y]
δ' ([q1, y], 1) = (δ[q1, 1], λ[q1,1]
= [q2, n]
'
λ [(q1, y)] = y
Consider state [q2,n]
δ' ([q2, n], 0) = (δ[q2, 0], λ[q2,0]
= [q1, n]
THEORY OF COMPUTATION

δ' ([q2, n], 1) = (δ[q2, 1], λ[q2,1]


= [q2, y]
'
λ [(q2, n)] = n
Consider state [q2,y]
δ' ([q2, y], 0) = (δ[q2, 0], λ[q2,0]
= [q1, n]
δ' ([q2, y], 1) = (δ[q2, 1], λ[q2,1]
= [q2, y]
'
λ [(q0, n)] = n
Transition table:

δ Input 0 Input 1 Output


[q0, n] [q1,n] [q2,n] n
[q0, y] [q1,n] [q2,n] y
[q1,n] [q1,y] [q2,n] n
[q1,y] [q1,y] [q2,n] y
[q2,n] [q1,n] [q2,y] n
[q2,y] [q1,n] [q2,y] y

Transition diagram
THEORY OF COMPUTATION

7) Minimize the following Finite automata by using Myhill-nerode theorem


0 1

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

Step 4: In the next iteration we examine the remaining pairs

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:

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