TCS Lect 6-7-8 DFA
TCS Lect 6-7-8 DFA
Lecture 6
• Examples on DFA
• E.g.
a
So S1 a
b
S2
• From state S0 for input 'a' there is only one path going to S1
• Similarly all the transitions can be described
M = ( Q , Σ , δ , q0 , F )
Where
q0 is an initial state q0 ε Q
• It is described as δ : Q X Σ → Q
• For example:
1 0 1
q0 q1 q2 q3
F = { q3 } δ ( q2 , 1 ) = q3 q3 By: Mr.
Prepared -- Vaibhav
--
Examples on DFA
0
Ex. 2 Design FA which checks whether the given binary
number is even. q2
0
Solution:
Binary no is made up of 1's and 0's. Hence Σ = { 0 , 1 } 1
q0 0
When binary number is ended with 1 → Odd Number
When binary number is ended with 0 → Even Number q1
1
Above DFA can be represented as Transition Function 1
M = ( Q , Σ , δ , q0 , F ) ( δ ):
Transition Table:
Where Q = { q0, q1, q2 } δ ( q0 , 0 ) = q2
Input/ 0 1
Σ = { 0, 1 } δ ( q0 , 1 ) = q1 States
q0 = q0 δ ( q1 , 0 ) = q2 qo q2 q1
q1 q2 q1
F = { q2 } δ ( q1 , 1 ) = q1
q2 q2 q1
δ ( q2 , 0 ) = q2 Prepared By: Mr. Vaibhav
Examples on DFA
Ex. 2 Design FA which checks whether the given binary number
is even.
Solution:
Transition Table:
The simulation to check whether given binary number is even or
not.
Input/ 0 1
Suppose input number is 11010 States
δ ( q0 , 1 1 0 1 0 ) |-- δ ( q1 , 1 0 1 0 ) qo q2 q1
|-- δ ( q1 , 0 1 0 ) q1 q2 q1
|-- δ ( q2 , 1 0 ) q2 q2 q1
|-- δ ( q1 , 0 )
|-- δ ( q2 , ε )
= q2
q2 is a final state.
Hence given number 11010 is accepted by given DFA
Prepared By: Mr. Vaibhav
Examples on DFA
Ex. 2 Design FA which checks whether the given binary number is
even.
Solution:
The simulation to check whether given binary number is even or
Transition Table:
not.
Input/ 0 1
Suppose input number is 10101
States
δ ( q0 , 1 0 1 0 1 ) |-- δ ( q1 , 0 1 0 1 ) qo q2 q1
|-- δ ( q2 , 1 0 1 ) q1 q2 q1
|-- δ ( q1 , 0 1 ) q2 q2 q1
|-- δ ( q2 , 1 )
|-- δ ( q1 , ε )
= q1
q1 is not a final state.
Hence given number 10101 is not accepted by given DFA
Prepared By: Mr. Vaibhav
Examples on DFA
Ex. 3 Design DFA which accepts only those strings which starts
with 1 and ends with 0 for Σ = { 0, 1 }.
Solution:
Above DFA can be
Regular expression = 1 . ( 0 + 1 )* . 0
Here, q1 represents strings start with 1 and end with 1 represented as
State q2 which represents strings start with 1 and end with zero M = ( Q , Σ , δ , q0 , F )
Transition Diagram: Where Q = { q0, q1, q2 }
1 0
1 Σ = { 0, 1 }
q0 q1 q2
q0 = q0
0
1 F = { q2 }
Transition Table:
Inputs
Transition Function ( δ ): 0 1
/States
δ ( q0 , 1 ) = q1 δ ( q2 , 0 ) q0 -- q1
= q2 q1 q2 q1
q2 q2 q1
δ ( q1 , 0 ) = q2 δ ( q2 , 1 ) Prepared By: Mr. Vaibhav
Examples on DFA
Ex. 3 Design DFA which accepts only those strings which
starts with 1 and ends with 0 for Σ = { 0, 1 }.
1
Prepared
δ ( q1 , By:
1)= Mr.q0
Vaibhav
Examples on DFA
Ex. 5 Design DFA to accept the string which always ends
with 00 for Σ = { 0, 1 }.
Solution:
Transition Table:
Simulation for the string 10100
δ ( q0 , 1 0 1 0 0 ) |-- δ ( q0 , 0 1 0 0 ) Input/ 0 1
|-- δ ( q1 , 1 0 0 ) States
qo q1 q0
|-- δ ( q0 , 0 0 )
q1 q2 q0
|-- δ ( q1 , 0 )
q2 q2 q0
|-- δ ( q2 , ε )
= q2
q2 is a final state.
Hence given string 10100 is accepted by given DFA
b b ) = q2
b
δ ( q2 , a ) = q1 δ ( q2 , b )
= q3
Prepared By: Mr. Vaibhav
Examples on DFA
Ex. 6 Design DFA to accept the strings of a's and b's ending
with abb over Σ = { a, b }
Solution:
Transition Table:
Simulation for the string baabb
δ ( q0 , b a a b b ) |-- δ ( q0 , a a b b ) Input/ a b
|-- δ ( q1 , a b b ) States
qo q1 q0
|-- δ ( q1 , b b )
q1 q1 q2
|-- δ ( q2 , b )
q2 q1 q3
|-- δ ( q3 , ε ) q3 q1 q0
= q3
q3 is a final state.
Hence given string baabb is accepted by given DFA
0
0
Solution: as
Invert all the Non Final states into Final states and Final state into non final state. M = ( Q , Σ , δ , q0 , F )
Required transition diagram is as follows: Where Q = { q0, q1, q2, q3 }
Transition Σ = { 0, 1 }
Diagram: q0 = q0
0
F = { q0, q1, q2 }
1
Transition Function ( δ ) :
1 0 1
qo q1 q2 q3 δ ( q0 , 0 ) = q0 δ ( q0 ,
1 ) = q1
1 δ ( q1 , 0 ) = q2 δ ( q1 , 1 )
0 = q1
0
δ ( q2 , 0 ) = q0 δ ( q2 ,
1 ) Prepared
= q3 By: Mr. Vaibhav
Examples on DFA
Ex. 7 Construct DFA that accepts all the strings on Σ = { 0,
1 } except those containing the substring 101
Transition Table:
Solution:
Simulation for the string 11001 Input/ 0 1
δ ( q0 , 1 1 0 0 1 ) |-- δ ( q1 , 1 0 0 1 ) States
qo q0 q1
|-- δ ( q1 , 0 0 1 )
q1 q2 q1
|-- δ ( q2 , 0 1 )
q2 q0 q3
|-- δ ( q0 , 1 ) q3 q3 q3
|-- δ ( q1 , ε )
= q1
q1 is a final state.
Hence given string 11001 is accepted by given DFA
Prepared By: Mr. Vaibhav
Examples on DFA
Ex. 8 Design DFA to accept all the string in Language L such that Above DFA can be
total number of a's in them are divisible by three on Σ = { a, b }.
Solution: represented as
Regular expression: (b*ab*ab*ab*)* M = ( Q , Σ , δ , q0 , F )
Here, q0 represents strings with number of a's are divisible by 3
Where Q = { q0, q1, q2 }
q1 represents strings with number of a's are divisible by 3 plus one
q2 represents strings with number of a's are divisible by 3 plus two Σ = { a, b }
Transition Diagram: q0 = q0
b Transition
F Function
= { q0 }
b
a a (δ):
q0 q1 q2
δ ( q0 , a ) = q1
δ ( q0 , b ) = q0
b δ ( q1 , a ) = q2
a
Prepared
δ ( q1 , By:
b)= Mr.q1
Vaibhav
Examples on DFA
Ex. 8 Design DFA to accept all the string in Language L
such that total number of a's in them are divisible by three
on Σ = { a, b }.
Solution: Transition Table:
Simulation for the string baaba
Input/ a b
δ ( q0 , b a a b a ) |-- δ ( q0 , a a b a ) States
|-- δ ( q1 , a b a ) qo q1 q0
|-- δ ( q2 , b a ) q1 q2 q1
|-- δ ( q2 , a ) q2 q0 q2
|-- δ ( q0 , ε )
= q0
q0 is a final state.
Hence given string baaba is accepted by given DFA
Prepared By: Mr. Vaibhav
Examples on DFA
Ex. 9 Design DFA which accepts even number of 0's and even number of Above DFA can be
1's over Σ = { 0, 1 }.
represented as
Solution:
Here, q0 represents strings with even number of 0's and even number of
M = ( Q , Σ , δ , q0 , F )
1's Where Q = { q0, q1, q2,
q1 represents strings with odd number of 0's and even number of 1's
q3 }
q2 represents strings with odd number of 0's and odd number of 1's
Σ = { 0, 1 }
q3 represents strings with even number of 0's and odd number of 1's
q0 = q0
Transition Diagram:
Transition Function ( δ ) : F = { q0 }
0
q0 q1
δ ( q0 , 0 ) = q1 δ ( q0 , 1 ) =
q3
0
1 1 1 1 δ ( q1 , 0 ) = q0 δ ( q1 , 1 ) =
0 q2
q3 q2 δ ( q2 , 0 ) = q3 δ ( q2 , 1 ) =
0 q1 Prepared By: Mr. Vaibhav
Examples on DFA
Ex. 9 Design DFA which accepts even number of 0's and
even number of 1's over Σ = { 0, 1 }.
Solution:
Transition Table:
Simulation for the string 100010
δ ( q0 , 1 0 0 0 1 0 ) |-- δ ( q3 , 0 0 0 1 0 ) Input/ 0 1
|-- δ ( q2 , 0 0 1 0 ) States
qo q1 q3
|-- δ ( q3 , 0 1 0 )
q1 q0 q2
|-- δ ( q2 , 1 0 )
q2 q3 q1
|-- δ ( q1 , 0 ) q3 q2 q0
|-- δ ( q0 , ε )
= q0
q0 is a final state.
Hence given string 100010 is accepted by given DFA
Prepared By: Mr. Vaibhav
Examples on DFA
Ex. 10 Design DFA to check whether given decimal number is divisible by 3 Above DFA can be
Solution: represented as
Logic is Divisibility test of 3
M = ( Q , Σ , δ , q0 , F )
Here, q3 represents remainder 0 state
q1 represents remainder 1 state Where Q = { q0, q1, q2,
q2 represents remainder 2 state q3 }
Transition Diagram: Σ = { 0, 1, 2, 3,
…,9}
2, 5, 8
q2 q0make
Here, we will = q0group
q0
of inputs toFrepresent
= { q3 }
0, 3, 6, 9 transitions as they have
2, 5, 8
similar next state on
0, 3, 6, current Input.
9
2, 5, 8 1, 4, 1, 4,
1, 4, 7 7 7
1, 4,7 Group 1: 0, 3, 6, 9
q3 Group 2: 1, 4, 7
q1 Group 3: 2, 5, 8
2, 5,8 0, 3, 6,
0, 3, 6, Prepared By: Mr. Vaibhav
9
Examples on DFA
Ex. 10 Design DFA to check whether given decimal number is divisible by 3 Transition Function:
Solution:
Logic is Divisibility test of 3
δ ( q0 , ( 0, 3, 6, 9 ) ) = q3
Here, q3 represents remainder 0 state
q1 represents remainder 1 state
δ ( q1 , ( 0, 3, 6, 9 ) ) = q1
q2 represents remainder 2 state δ ( q2 , ( 0, 3, 6, 9 ) ) = q2
Transition Diagram: δ ( q3 , ( 0, 3, 6, 9 ) ) = q3
δ ( q0 , ( 1, 4, 7 ) ) = q1
2, 5, 8 δ ( q1 , ( 1, 4, 7 ) ) = q2
q0 q2
δ ( q2 , ( 1, 4, 7 ) ) = q3
0, 3, 6, 9
2, 5, 8 δ ( q3, ( 1, 4, 7 ) ) = q1
0, 3, 6, δ ( q0 , ( 2, 5, 8 ) ) = q2
9
2, 5, 8 1, 4, 1, 4,
1, 4, 7 7 7 δ ( q1 , ( 2, 5, 8 ) ) = q3
1, 4,7
q3 δ ( q2 , ( 2, 5, 8 ) ) = q1
q1
2, 5,8 0, 3, 6, δ ( q3 , ( 2, 5, 8 ) ) = q2
0, 3, 6, Prepared By: Mr. Vaibhav
9
Examples on DFA
Ex. 10 Design DFA to check whether given decimal number is divisible by 3 Transition Table:
Solution:
Logic is Divisibility test of 3
Inputs/
Here, q3 represents remainder 0 state 0, 3, 6, 9 1, 4, 7 2, 5, 8
States
q1 represents remainder 1 state
q0 q3 q1 q2
q2 represents remainder 2 state
q1 q1 q2 q3
Transition Diagram:
q2 q2 q3 q1
q3 q3 q1 q2
2, 5, 8
q0 q2
0, 3, 6, 9
2, 5, 8
0, 3, 6,
9
2, 5, 8 1, 4, 1, 4,
1, 4, 7 7 7
1, 4,7
q3 q1
2, 5,8 0, 3, 6,
0, 3, 6, Prepared By: Mr. Vaibhav
9
Examples on DFA
Ex. 10 Design DFA to check whether given decimal number
is divisible by 3
Solution:
Transition Table:
Simulation for the string 532
δ ( q0 , 5 3 2 ) |-- δ ( q2 , 3 2 ) Inputs/ 0, 3, 1, 4, 2, 5, 8
|-- δ ( q2 , 2 ) States 6, 9 7
q0 q3 q1 q2
|-- δ ( q1 , ε )
q1 q1 q2 q3
= q1
q2 q2 q3 q1
q3 is not a final state. q3 q3 q1 q2
Hence given string 532 is not accepted by given DFA