Theory of Computation: Sathyabama
Theory of Computation: Sathyabama
SATHYABAMA
Institute of Science and Technology
Deemed to be University
Chennai.
Empty Set : Ø
NULL String :ε or λ
Finding Language using Conditions
• Find L with 0’S and 1’s with odd no. of 1’s
– ∑ ={ 0,1}
– w ={ 1,01,10,100,010, 111,1011……..}
– w ={ 1,01,10,100,010, 110 ,111,1011……..} --invalid
– L = {w/w consists of odd no. of 1’s}
• Find L with 0’s and 1’s with even no. of 1’s
– ∑ ={ 0,1}
– w ={Λ ,11,011,101,110,0110, 1010, ……..}
– w ={Λ ,11,011,101,100, 110,0110, 1010, …..} --invalid
– L = {w/w consists of even no. of 1’s}
Finding Language using Conditions
• Find L with a’s and b’s with even no. of a’s
– ∑ ={ a,b}
– w ={Λ ,aa,baa,aba,aab,baab, abba,abab ……..}
– w ={Λ ,aa,baa,aaa,aab,baab, abba,abab ……..} --invalid
– L = {w/w consists of even no. of a’s}
• Find L with a’s and b’s with even no of a’s and b’s.
– ∑ ={ a,b}
– w ={Λ ,aa,bb,aabb,abab,baba,bbaa ……..}
– L = {w/w consists of even no. of a’s and b’s}
• Find L with a’s and b’s having a substring aa
– ∑ ={ a,b}
– w ={aa,baa,aab,baab, abaa,aabaa ……..}
– L = {w/w consists of a substring aa}
Define Regular language and
Regular Expression over ∑.
• RL:
– A language is regular if there exits a DFA for that
language
– The language accepted by DFA is RL.
• RE:
– A Mathematical notation used to describe the regular
language.
– This is formed by using 3 Symbols:
(i) . [dot operator] – for concatenation
(ii) + [Union operator] –at least 1 occurrence
(iii) {*} [Closure Operator ] – Zero or more occurrences
Basic Regular Expressions?
• Ø is a RE and denotes the empty set.
• ε is a RE and denotes the set { ε }
• For each a in ∑, a is a RE and denotes the set
{a}
• If r and s are RE that denoting the languages R
and S respectively, then,
– (r+s) is a RE that denotes the set (RUS)
– (r.s) is a RE that denotes the set RS
– (r)* is a RE that denotes the set R*
Writing Regular Expressions
1. Write RE for the language of even no of 1’s
∑ ={ 1}
w ={Λ , 11,1111, ……..}
RE = (11)*
2. Write RE for the language of odd no of 1’s
∑ ={ 1}
w ={1,111,11111 ……..}
RE = (11)*.1
3. Write RE with a string starting with a
∑ ={ a,b}
w ={a,ab,aba,aab ……..}
RE = (a) (a+b)*
Writing Regular Expressions
4. Write RE with a string starting with either a or ab
∑ ={ a,b}
w ={a,ab,aba,aab ……..}
RE = (a+ab) (a+b)*
5. Write RE with a string consist of a’s and b’s any length include
λ
RE= (a+b)*
6. Write RE with a string a’s and b’s ending with abb
RE= (a+b)*abb
7. Write RE with a string a’s and b’s starting with abb
RE= abb (a+b)*
Writing Regular Expressions
8. Write RE with a string having a substring aa
RE= (a+b)*.aa.(a+b)*
9. Write RE with a string that begins and ends
with double consecutive letters
RE = (aa+bb).(a+b)*.(aa+bb)
10. Strings of a’s and b’s of even length
RE= (aa+bb+ab+ba)*
11. Strings of a’s and b’s in which 3rd symbolfrom right
end is a.
RE = (a+b)*.a.(a+b).(a+b)
What is Finite Automata?
• Finite automata are used to recognize
patterns.
• A machine that accepts Regular Language
Applications:
– Compliers
– Text processing
– Hardware design.
Types of Automata
DFA (Deterministic Finite Automata)
• Only one path for specific input from the
current state to the next state.
• DFA does not accept the null move
• It is used in Lexical Analysis phase in
Compilers.
• Example: RE=(a+b)+
Formal Definition of DFA
A DFA or finite state machine is a 5 tuple
M=(Q, ∑, q0, δ, A) machine,
where,
Q -is a finite set of states
∑ - finite set of input symbols
q0 Q - Initial states
A ⊆ Q - Set of accepting states
δ - transition function
δ : Q x ∑ -> Q
Construction of DFA
Problems:
1. Design DFA to accept the strings having exactly one a.
∑ ={ a,b}
w ={a,ab, ba, abb,bab ……..} RE = b*.a. b*
δ : Q x ∑ -> Q
2. Design DFA to accept strings having at least one a.
Tuples:
Q={q0,q1}
∑={a,b}
q0 -> start state
A= {q1}
δ - transition function
δ (q0,a)->q1
δ (q0,b)->q0
δ (q1,a)-> q1
δ (q1,b)-> q1
3. Design DFA to accept
Case 1: strings of even a’s and even b’s
Case 2: strings of even a’s and odd b’s
Case 3: strings of odd a’s and even b’s
Case 4: strings of odd a’s and odd b’s
Tuples:
Q={q0,q2,q3,q4}
Σ ={a,b}
q0= start state, A={q3}
Transition Function:
δ(q0,a)={q2}, δ(q0,b)={q4}
δ(q2,b)={q3}, δ(q2,a)={q0}
δ(q3,a}={q4}, δ(q3,b)={q2}
Extended Transition Function (δ*)
• δ*
– Describes what happens when we start in a any state
and follow any sequence of inputs.
– Definition of δ*:
Let M= (Q,Σ,δ,q0,A) be a FA.
We define the function δ* : Q x ∑ *->Q as follows:
1. For any state q ∈ Q , δ* (q, Λ ) = q
2. for any state q ∈ Q , y ∈ ∑ *, a ∈ ∑
δ*(q, ya) = δ(δ*( q,y),a)
Note:
δ* - to read a string
δ – to read a symbol
Problems - (δ*)
• Find δ*(q0,abc) ?
δ* ( q0,abc) = δ(δ*(q0,ab),c)
= δ(δ(δ* (q0,a),b),c)
= δ(δ(δ (q0,a),b),c)
= δ(δ(q1,b),c)
=δ(q2,c)
Ans= q3
Definitions
• String acceptance by Finite Automata
• Let M=(Q,∑,q0,A, δ) be an FA.
• A string x∈ ∑* is accepted by M, if δ*( q0,x) ∈ A
• Language acceptance by Finite Automata
– The language accepted by M or the language
recognized by M is the set,
– L(M)= {x | x ∈ ∑* and δ*( q0,x) ∈ A}
Non-Deterministic Finite Automata
• When there exist many paths for specific input from the
current state to the next state.
• Every NFA is not DFA, but each NFA can be translated into
DFA.
• Types
1. NFA without Λ 2.
2. NFA with Λ
Advantages of NFA over DFA:
– DFAs are faster but more complex
– Build a FA representing the language that is a
union, intersection, concatenation etc. of two (or more)
languages easily by using NFA's.
• Design NFA to accept the Language
L= {w/w = ababn /aban}
•
Conversion of NFA to DFA
• Problem 1: convert the given NFA to DFA
Step 3: Find the possible set of states for each input symbol
3.1 From A: DFA- Transition
State 0 Table 1
δ'([q0, q1], 0) = δ(q0, 0) ∪ δ(q1, 0) q0 A B
= {q0, q1} ∪ {q1,q2} A C B
= {q0, q1, q2}
B D B
= [q0, q1, q2] --------(C)
Step 3: find the possible set of states for each input symbol
3.1 From A
δ'([q1], 0) = δ(q1, 0)
= [q1,q2 ] -------------(B)
δ'([q1], 1) = δ(q1, 1)
= [q1] --------------(A)
3.2 From B:
δ'([q1,q2], 0) = δ(q1,0) U δ(q2,0)
={q1,q2} U {q2}
={q1,q2} --------------(B)
Find:
i. δ*(q0,11):
δ*(q0,11)=
= δ(q0,1) U δ(q1,1)
={q0,q1} U {q2}
ii. Find δ*(q0,01)
δ*(q0,01)=
= δ(q0,1)
={q0,q1}
iii. Find δ*(q0,011)
δ*(q0,011)=
=δ(q0,1) U δ(q1,1)
={q0,q1} U{q2} ={q0,q1,q2}
iv.Find: δ*(q0,111)
δ*(q0,111)=
∴111 is accepted.
Theorem 1
• Statement:
For any NFA machine M=(Q, ∑, q0, δ , A)
accepting a language L ⊆ ∑*, there is a DFA
machine M1= (Q, ∑, q0, δ , A) that accepts L.
Proof by Structural Induction
• Proof:
DFA- M1is defined as follows:
– Q1=2Q
– q1=q0
– A1={qϵQ1 |A⊆Q}
– For,qϵQ1 and aϵ∑,
Ʌ(q0)={ }
Ʌ(q1) ={ }
Ʌ(q2)={ }
Problems on Ʌ Closure
2.
Ʌ(q0)={ }
Ʌ(q1) ={ }
Ʌ(q2)={ }
Extended Transition Function of
NFA- Λ (δ*)
• δ*
– Describes what happens when we start in a any state and follow any
sequence of inputs.
– Definition of δ*:
Let M= (Q,Σ,δ,q0,A) be a NFA with Λ .
We define the function δ* : Q x ∑ * {Λ }→2Q as follows:
1. For any state q 𝞮 Q , δ* (q, Λ ) = Λ (q)
2. for any state q 𝞮 Q , y 𝞮∑ *, a 𝞮 ∑
δ*(q, ya)
δ* - to read a string
δ – to read a symbol
Extended Transition Function (δ*)
Problem on δ*
=Ʌ(δ(p,1) U δ(u,1))
=Ʌ({r}U∅)
= Ʌ({r})
δ*(q0,01) ={r}
iii.Find δ*(q0,010)
δ*(q0,010)
=Ʌ(δ(r,0))
= Ʌ({s})
={s,w,q0,p,t}
Accepted
Conversion of NFA-Ʌ to an NFA
=Ʌ(δ(B,0) U δ(D,0))
= Ʌ(C,D)
={C,D}
• iv. δ*(B,1)=δ*(B,Ʌ1)
=Ʌ(δ(B,1) U δ(D,1))
= Ʌ(∅)
=∅
v.δ*(C,0)=δ*(C,Ʌ0)
=Ʌ(δ(C,0))= Ʌ(∅)=∅
vi. δ*(C,1)=δ*(C,Ʌ1)
=Ʌ(δ(D,0))=Ʌ(D)={D}
viii.δ*(D,1)= δ*(D,Ʌ1)
=Ʌ(δ(D,1)) = Ʌ(∅) =∅
Final state of NFA
Ʌ -Closure (A)=
{A,B,D}
Ʌ -Closure (B)=
State
{B} CONVERTED NFA0 1
A {A,B,C,D,E} {D,E}
Ʌ -Closure (C)=
B {C} {E}
{C}
C - {B}
Ʌ -Closure (D)=
D {E} {D}
{D}
E - -
Ʌ -Closure (E)=
{E}
Theorem 2
• Equivalence of NFA-ꓥ and NFA
• If L is accepted by an NFA with ꓥ transitions then
L is accepted by NFA without ꓥ transitions
• Statement:
For any NFA NULL machine M=(Q, ∑, q0, δ , A)
accepting a language L ⊆ ∑*, there is a NFA
machine M1= (Q, ∑, q0, δ , A) that accepts L.
Given:
NFA-ꓥ, M=(Q,∑,δ,q0,A)
NFA, M1=(Q1,∑,δ1,q1,A1)
Given:
NFA-ꓥ, M=(Q,∑,δ,q0,A) NFA-ꓥ:
NFA, M1=(Q1,∑,δ1,q1,A1)
Proof: NFA:
• By Definitions:
• For, aϵ∑ :
(By definition it is true if we
pass a single length string)
• To Prove:
• Proof: By Structural Induction
• Basis Step:
Take |x|=0 => x= Ʌ
LHS:
RHS:
LHS: δ1*(q1,xa)
= δ*(q, xa)
=RHS Hence the theorem is proved.
Kleene’s Theorem Part -I
Step 3: RE=a |b a
1 2
ε
ε
start
0
5
ε ε
3 4
The NFA recognizes {a,b} b
Step 4: RE=ab
start a b OR start a ε b
0 1 2 0 1 2 3
start ε a ε
0 1 2 3
Step 6: RE= a+
= a.a*
Follow step 4 for construction.
a. (a|b)*abb
b. (a|b)*
c. (a*|b*)* =a*,b* , (a*+b*)
d. ((ε|a)b*)*
e. (0|123)*=0,123,0+123,
ε
r9
r10
r7 a
r8 2 3
r5 b ε
ε ε
start
r6 b 0 1 ε
r4 * 6 7
a ε ε
4 5
( r3 ) b
r1 r2 ε
|
a b
ε
r9
r10
r7 a
r8 2 3
r5 b ε
ε ε
start a
r6 b 0 1 ε
r4 6 7 8
*
a ε ε
4 5
( r3 ) b
r1 r2 ε
|
a b
a
2 3
ε
ε
start ε
0 1 ε a b b
6 7 8 9 10
ε ε
4 5
b
a. (a|b)*abb
b. (a|b)*
c. (a*|b*)*
d. ((ε) |a)b*)*= ε,a, ε+a,b*, , (ε+a)b*,
e. (0|123)*
• Induction Hypothesis:
Assume that theorem is true for ‘r’ with fewer than
‘i’ operators, i>=1 =>1<=n<i
• Induction Step:
Let ‘r’ have ‘i’ operators.
Case 1: r=r1+r2
Let NFA-Ʌ Machine
M1=(Q1,∑1,δ1,q1, {f1})
L(M1)=L(r1)
Let NFA- Ʌ’s machine
M2=(Q2,∑2,δ2,q2,{f2})
L(M2)=L(r2)
Where Q1 and Q2 are disjoint.
q0->new initial state
f0->new final state
Construct M= (Q1UQ2U{q0,f0}, ∑1U∑2,δ,q0,{f0})
Where δ is defined by,
δ(q0,Ʌ)={q1,q2}
δ(f1,Ʌ)=δ(f2,Ʌ)={f0}
Thus all the moves of M1 and M2 are in M.
There is a path labelled x in M from q1
to f1 or a path in M2 from q2 to f2.
Hence L(M)=L(M1) U L(M2)
L(M)={x|x is in L(M1) or x is in L(M2)}
Case 2: r=r1 . r2
Let M1 and M2 be as in case 1
• Formula:
• Where
– i= Start state
– j = Final state
– k = No.of states
Problem 1: Obtain the regular expression for the
finite automata shown below:
Formula for RE:
Find:
In:
When K=0:
When K=0:
Substituting in 1:
Substituting in 1:
2. Obtain the RE for the following DFA