You are on page 1of 96

Theoretical Computer Science

Unit 1: Basic Concepts and Finite


Automata

Faculty Name : Dr. Vanita Mane


1
Index -

Lecture 4 – Finite Automata Design 3

Lecture 5 – DFA Minimization 24

Lecture 6 – – NFA to DFA Conversion 52

2
Lecture 4

Finite Automata Design


Finite automata

• Finite automata (FA) is the mathematical model (or formalism) of an FSM.


Mathematical models of machines are the abstractions and simplifications of
how certain machines actually work.
• They help us understand specific properties of these machines. An FA
portrays the FSM as a language acceptor.
• Finite automata involves states and transitions among states in response to
states .In FA internal states alters or changes when it receives input.
• It has finite number of states. There will be an initial state and one or more
final states. There will be transitions from one state to another for some
input.

4 Lecture 4: DFA Design


Working of Finite Automata

5 Lecture 4: DFA Design


Finite Automata Types

FA

FA Without
FA With Output
Output

Moore M/C Mealy M/C DFA NFA

6 Lecture 4: DFA Design


Deterministic Finite Automata (DFA)

• The transitions are deterministic i.e. there should be only one state to which
we can transit from current state on each input. Thus the automata cannot be
in more than one state at any time

7 Lecture 4: DFA Design


Non-Deterministic Finite Automata (NFA)

• The transitions are non deterministic so the automata can be in more than one
state at any time

8 Lecture 4: DFA Design


Formal definition of DFA

DFA can be mathematically expressed as a 5-


tuple as:
 M=(Q, ∑,δ,q0,F)
Where ,
Q is a finite set of states
∑ is a finite set of input symbols
q0 is a initial state
F is a finite set of final states
δ is a transition function as QX ∑🡪Q

9 Lecture 4: DFA Design


Example 1: Design DFA which accepts string with length exactly three over
∑ ={0,1}

▪ Input =∑ ={0, 1}
▪ Language
L= {000, 111, 101, 001, 110, 011, 010…………..}

▪ Possible States :
qs 🡪 length is 0
q0 🡪 length is 1
q1 🡪 length is 2
q2 🡪 length is 3
q3 🡪 length is greater than 3
▪ Q= {qs, q0, q1 ,q2 , q3}

▪ Initial State ={qs }


▪ Final State ={q2 }

10 Lecture 4: DFA Design


Transition Table


0 1
Q
-->qs (len=0) q0 q0

q0 (len=1) q1 q1

q1 (len=2) q2 q2

q2* (len=3) q3 q3

q3 (len>3) q3 q3
11 Lecture 4: DFA Design
Transition Diagram

Simulation: Simulation:
(qs, 001) (qs, 1011)
(q0,01) (q0,011)
(q1,1) (q1,11)
(q2,ε) (q2,1)
Accept (q3,ε)
Reject

12 Lecture 4: DFA Design


Example 2: Design DFA which accepts string with odd number of 0s and even
number of 1s over ∑ ={0,1}

▪ Input =∑ ={0, 1}
▪ Language
L= {0, 011, 110, 00011, 10100, 0011011..............}

▪ Possible States :
qs 🡪 no symbol accepting
q0 🡪 odd 0 and even 1
q1 🡪 even 0 and odd 1
q2 🡪 even 0 and even 1
q3 🡪 odd 0 and odd 1
▪ Q= {qs, q0, q1 ,q2, q3 }

▪ Initial State ={qs }


▪ Final State ={q0 }

13 Lecture 4: DFA Design


Transition Table

State/ Input 0 1

--> qs q0 q1

q0* (odd, even) q2 q3

q1 (even, odd) q3 q2

q2 (even, even) q0 q1

q3 (odd, odd) q1 q0

14 Lecture 4: DFA Design


Transition Diagram

15 Lecture 4: DFA Design


Simulation

State/ Input 0 1
String = 10100
(qs,10100) --> qs q0 q1
(q1, 0100)
(q3, 100)
q0* (odd, even) q2 q3
(q0, 00)
(q2, 0)
q1 (even, odd) q3 q2
(q0,ε)

q2 (even, even) q0 q1
Accept
q3 (odd, odd) q1 q0

16 Lecture 4: DFA Design


Example 3: Design DFA that accepts string containing “baa” as a substring over ∑
={a, b}

▪ Input =∑ ={a, b}
▪ Language
L= {baa, abaa,abbaa,aabaab....................}

▪ Possible States :
qs 🡪 no symbol accepting
q0 🡪 substring as ‘a’
q1 🡪 substring as ‘b’
q2 🡪 substring as ‘ba’
q3 🡪 substring as ‘baa’
▪ Q= {qs, q0, q1 ,q2, q3 }

▪ Initial State ={qs }


▪ Final State ={q3 }

17 Lecture 4: DFA Design


Transition Table

delete possible prefix


State/ Input a b Processing
🡪qs (ε) q0 q1 ε.a ε.b
q0 (a) q0 q1 a. a a. b
q1 (b) q2 q1 b. a b. b
q2 (ba) q3 q1 baa bab
q3 * (baa) q3 q3

18 Lecture 4: DFA Design


Transition Diagram

19 Lecture 4: DFA Design


Simulation

Consider string= bbaa


-(qs, bbaa)
State/ Input a b
-(q1, baa)
🡪qs (ε) q0 q1
-(q1, aa)
-(q2,a) q0 (a) q0 q1
-(q3, ε) q1 (b) q2 q1
q2 (ba) q3 q1
Yes/Accept
q3 * (baa) q3 q3

20 Lecture 4: DFA Design


Example 4: Design DFA that accepts string containing “aba” as a substring over ∑
={a, b}

▪ Input =∑ ={a, b}

▪ Language
L= {aba, aaba,aabab,aabab....................}

▪ Possible States :
qs 🡪 no symbol accepting
q0 🡪 substring as ‘a’
q1 🡪 substring as ‘b’
q2 🡪 substring as ‘ab’
q3 🡪 substring as ‘aba’
▪ Q= {qs, q0, q1 ,q2, q3 }

▪ Initial State ={qs }


▪ Final State ={q3 }
21 Lecture 4: DFA Design
Transition Table

State/ Input a b

🡪qs q0 q1

q0 q0 q2

q1 q0 q1

q2 q3 q1

q3* q3 q3

22 Lecture 4: DFA Design


Transition Diagram

23 Lecture 4: DFA Design


Simulation

STF
Consider string= bbaa
State/ Input a b
-(qs, bbaa)
-(q1, baa) 🡪qs q0 q1
-(q1, aa) q0 q0 q2
-(q0,a) q1 q0 q1
-(q0, ε) q2 q3 q1
q3* q3 q3
No/Reject

24 Lecture 4: DFA Design


Example 5: Design DFA that accepts strings ending with “baa” over ∑ ={a, b}

▪ Input =∑ ={a, b}
▪ Language
L= {baa, abaa,abbaa,aabaa....................}

▪ Possible States :
qs 🡪 no symbol accepting
q0 🡪 substring as ‘a’
q1 🡪 substring as ‘b’
q2 🡪 substring as ‘ba’
q3 🡪 substring as ‘baa’
▪ Q= {qs, q0, q1 ,q2, q3 }

▪ Initial State ={qs }


▪ Final State ={q3 }

25 Lecture 4: DFA Design


Transition Table

delete possible prefix


State/ Input a b Processing
🡪qs (ε) q0 q1 ε.a ε.b
q0 (a) q0 q1 a. a a. b
q1 (b) q2 q1 b. a b. b
q2 (ba) q3 q1 baa bab
q3 * (baa) q0 q1 baa.a baa.b

26 Lecture 4: DFA Design


Transition Diagram

27 Lecture 4: DFA Design


Simulation

Consider string= bbaa


-(qs, bbaa)
State/ Input a b
-(q1, baa)
🡪qs (ε) q0 q1
-(q1, aa)
-(q2,a) q0 (a) q0 q1
-(q3, ε) q1 (b) q2 q1
q2 (ba) q3 q1
Yes/Accept
q3 * (baa) q0 q1

28 Lecture 4: DFA Design


Example 6: Design DFA that accepts strings ending with “abab”
over ∑ ={a, b}

▪ Input =∑ ={a, b}
▪ Language
L= {abab, aabab,aabab,baaabab....................}

▪ Possible States :
qs 🡪 no symbol accepting
q0 🡪 substring as ‘a’
q1 🡪 substring as ‘b’
q2 🡪 substring as ‘ab’
q3 🡪 substring as ‘aba’
q4 🡪 substring as ‘abab’
▪ Q= {qs, q0, q1 ,q2, q3, q4 }

▪ Initial State ={qs }


▪ Final State ={q4 }

29 Lecture 4: DFA Design


Transition Table

State/ Input a b
🡪qs q0 q1
q0 q0 q2
q1 q0 q1
q2 q3 q1
q3 q0 q4
q4* q3 q1

30 Lecture 4: DFA Design


Transition Diagram

31 Lecture 4: DFA Design


Simulation

Consider string= bbabab


-(qs, bbabab) State/ Input a b
-(q1, babab) 🡪qs q0 q1
-(q1, abab) q0 q0 q2
-(q0,bab) q1 q0 q1
-(q2,ab)
q2 q3 q1
-(q3,b)
-(q4, ε) q3 q0 q4
q4* q3 q1
Yes/Accept

32 Lecture 4: DFA Design


Example 7: Design DFA which accepts string that does not contain sequence
”bbb” over ∑ ={a, b}

▪ Input =∑ ={a, b}
▪ Language
L= {a, bba, bb, bbab,aa, ab, aba..............}

▪ Possible States :
qs 🡪 no symbol accepting
q0 🡪 substring “a”
q1 🡪 substring “b”
q2 🡪 substring “bb”
qϕ 🡪 substring “bbb
▪ Q= {qs, q0, q1 ,q2, qϕ }

▪ Initial State ={qs}


▪ Final State ={q0,q1,q2 }

33 Lecture 4: DFA Design


Transition Table

String 1= abbab
(qs,abbab) State/ a b
(q0, bbab) Input
(q1, bab) --> qs q0 q1
(q2,ab)
(q0, b)
q0* (a) q0 q1
(q1,ε)
Accept
String 2= abbba q1* (b) q0 q2
(qs,abbba)
(q0, bbba) q2* (bb) q0 qϕ
(q1, bba)
(q2,ba) qϕ (bbb) qϕ qϕ
(qϕ , a)
(qϕ ,ε)
Reject

34 Lecture 4: DFA Design


Transition Diagram

35 Lecture 4: DFA Design


Thank You
Lecture 5

Finite Automata Design


Examples and Minimization
Example 8: Design DFA that accepts strings starting with “abb” over ∑ ={a,b}

▪ Input =∑ ={a, b}

▪ Language
L= {abb, abbaa, abbab, ...................}

▪ Possible States :
qs 🡪 no symbol accepting
q0 🡪 substring as ‘a’
q1 🡪 substring as ‘ab’
q2 🡪 substring as ‘abb’
qr 🡪 reject state starts with ‘b’

▪ Q= {qs, q0, q1 , q2, qr }

▪ Initial State ={qs }


▪ Final State ={q2 }

38 Lecture 5: DFA Design and DFA Minimization


Transition Table

If no matching substring found for any state then transit to reject state

STF State/ Input a b Processing

🡪qs (ε) q0 qr ε.a ε.b

q0 (a) qr q1 aa ab

q1 (ab) qr q2 aba abb

q2 * (abb) q2 q2 (anything after “abb” can


be accepted)
qr qr qr (once reached to reject
(reject) state then no way to
accept any string )

39 Lecture 5: DFA Design and DFA Minimization


Transition Diagram

40 Lecture 5: DFA Design and DFA Minimization


Simulation

Consider string= abba State/ Input a b


-(qs, abba)
-(q0, bba) 🡪qs (ε) q0 qr
-(q1, ba)
-(q2, a)
q0 (a) qr q1
-(q2, ε)
Yes
q1 (ab) qr q2
Consider string= bba
-(qs, bba) q2 * (abb) q2 q2
-(qr, ba)
-(qr, a) qr qr qr
-(qr , ε) (reject)
No

41 Lecture 5: DFA Design and DFA Minimization


Example 9: Design DFA that accepts strings starting with “ab” or “ba” over ∑
={a,b}

▪ Input =∑ ={a, b}

▪ Language
L= {abb, abbaa, abbab, ba, baa, bab,...................}

▪ Possible States :
qs 🡪 no symbol accepting
q0 🡪 substring as ‘a’
q1 🡪 substring as ‘ab’
q2 🡪 substring as ‘b’
q3 🡪 substring as ‘ba’
qr🡪 reject state

▪ Q= {qs, q0, q1 , q2, q3, qr}

▪ Initial State ={qs }


▪ Final State ={q1, q3 }

42 Lecture 5: DFA Design and DFA Minimization


Transition Table

If no matching substring found for any state then transit to reject state

STF State/ Input a b Processing

🡪qs (ε) q0 q2 ε.a ε.b

q0 (a) qr q1 aa ab

q1 * (ab) q1 q1 (anything after “ab” can


be accepted)
q2 (b) q3 qr ba bb

q3* (ba) q3 q3 (anything after “ba” can


be accepted)
qr(reject) qr qr (once reached to reject
state then no way to
accept any string )
43 Lecture 5: DFA Design and DFA Minimization
Transition Diagram

44 Lecture 5: DFA Design and DFA Minimization


Simulation

State/ Input a b
Consider string= abba
-(qs, abba)
🡪qs (ε) q0 q2
-(q0, bba)
-(q1, ba)
-(q1, a) q0 (a) qr q1
-(q1, ε)
Yes q1 * (ab) q1 q1

Consider string= bba q2 (b) q3 qr


-(qs, bba)
-(q2, ba) q3* (ba) q3 q3
-(qr, a)
-(qr, , ε)
qr(reject) qr qr
No

45 Lecture 5: DFA Design and DFA Minimization


DFA Minimization

• Equivalent states:
• States are equivalent to each other if they have same transition for
the particular input.

• To minimize DFA equivalent states can be replaced.

• Rules for replacement: One of the state is replaced by other and


removed.
• If any state is equivalent to Initial state then replace that state with
Initial state. Don’t remove Initial State.
• Final state can be replaced by any other final state.
• Non-Final state can be replaced by any other non-final state.

46 Lecture 5: DFA Design and DFA Minimization


Example 1

• Equivalent states
{q1, q2}
• IF q1 and q2 are equivalent then
Delete entry for q2 from the transition table
In this modified table ,replace each occurrence of q2 by q1

State\ a b
State\ a b
Input
Input
--> q1 q1 q2
-->q1 q1
q2 q1
q2 q1 q2
q3 * q2 q1 q3
q3* q2 q3

47 Lecture 5: DFA Design and DFA Minimization


Example 2

• Equivalent states
{q1, q2}
• IF q1 and q2 are equivalent then
Delete entry for q2 from the transition table
In this modified table ,replace each occurrence of q2 by q1

State\ a b
State\ a b
Input
Input
--> q1 q1 q2
-->q1 q1 q1
q2 q1 q2
q3 * q1 q3
q3* q2 q3
q4 q1 q3

q4 q2 q3
Not Equivalent

48 Lecture 5: DFA Design and DFA Minimization


Example 3

• Equivalent states
{q2, q4}
• IF q2 and q4 are equivalent then
Delete entry for q4 from the transition table
In this modified table ,replace each occurrence of q4 by q2

State\ a b State\ a b
Input Input
--> q1 q3 q2
-->q1 q3 q2

q2 q3 q4
q2 q3 q2
q3* q2 q1 q3* q2 q1

q4 q3 q4

49 Lecture 5: DFA Design and DFA Minimization


Example 3 continued

• Equivalent states
{q1, q2}
• IF q1 and q2 are equivalent then
Delete entry for q2 from the transition table
In this modified table ,replace each occurrence of q2 by q1

State\ a b
State\ a b
Input
Input
-->q1 q3 q2
-->q1 q3 q1

q2 q3 q2
q3* q1 q1
q3* q2 q1

50 Lecture 5: DFA Design and DFA Minimization


Example 4

State\ 0 1 State\ 0 1
Input Input
🡪p x y 🡪p x y
q r q q r q
r s p r s p
s * s p s * s p
t r q t r q
x* t z x* t z
y r q y r q
z* t z z* t z

51 Lecture 5: DFA Design and DFA Minimization


• Equivalent states={q ,t, y} State\ 0 1
Input
🡪p x q
q r q
r s p
s * s p
x* q z
z* q z

52 Lecture 5: DFA Design and DFA Minimization


• Equivalent states={x, z} State\ 0 1
Input
🡪p x q
q r q
r s p
s * s p
x* q x

53 Lecture 5: DFA Design and DFA Minimization


Lecture 6

NFA to DFA Conversion


Non Deterministic Finite Automata (NFA)

• Non-determinism
Given a current state of the machine and input symbol to be read, the next
state is not uniquely determined.

• NFAs have 3 features when compared with DFAs.


1. Ability to take a step without reading any input symbol
2. A state may have no transition on a particular symbol
3. Ability to transit to more than one state on a given symbol

• NFA consist of finite set of states, one state is called start state and there
can be one/more final states.
• In NFA, from each state on each I/P symbol there can be 0,1 or more
transition.

55 Lecture 6: NFA to DFA Conversion


Non Deterministic Finite Automata (NFA) : Formal Definition

• NFA can be represented mathematically as follows:

M = (Q, Σ, δ, q0, F) where Q = Finite set of states

Σ = Finite set of Input symbols


δ : Q × Σ → 2Q transition function

q0 ∈ Q is start state

F = {q1, q2} ⊆ Q accept


states

56 Lecture 6: NFA to DFA Conversion


NFA with ε- Transitions

• Transitions without reading input symbols.

• Example 1. The British spelling of \color" is \colour". In a web search


application, you may want to recognize both variants.

57 Lecture 6: NFA to DFA Conversion


No Transtion

• In the following automaton, ∑ ={0, 1}


• But there is no transition over state qo for input 0

58 Lecture 6: NFA to DFA Conversion


Multiple Transitions

• In following automata there are multiple transitions on input 0 from state q0

59 Lecture 6: NFA to DFA Conversion


Why NFA?

• It becomes some times simpler to draw NFA for a particular problem than
drawing a DFA.
• E.g. Strings ending in 10
• The NFA is

• How does it knows which arrow to follow?


– It guesses and somehow makes the right choice.
– It tries multiple possibilities in parallel and one of them will turn out as
right choice.

60 Lecture 6: NFA to DFA Conversion


Accepting or rejecting an input

• E.g Input to NFA is 0110


• It can try following configurations:
q0🡪 q0 🡪 q0🡪 q0🡪 reject
q0🡪 q1 🡪 reject
q0🡪 q0 🡪q1🡪 q2 🡪 accept

• If any one path leads to accepting state and entire input is consumed then
machine accepts the string.
• NFA has same capacity as DFA.
• It just adds simplicity to DFA.

61 Lecture 6: NFA to DFA Conversion


Difference between DFA and NFA
DFA NFA
1. It is deterministic It is non-deterministic.
2. For each state for given input there is For each state for given input the
a unique next state next state is not uniquely
determined.
3. No epsilon transition. Epsilon transitions are allowed.
4. Conversion of RE to DFA is complex. Conversion of RE to NFA is easier.
5. More number of states compared to Less number of states.
NFA.
6. DFA requires more memory for storing NFA requires more computation to
the state info. match RE with input.
7. In DFA it is not possible to move to In NFA we can move to next state
next state without reading any symbol. without reading any symbol.
8. It is easy to check acceptance of It is difficult to check acceptance as
string. multiple paths are followed.

62 Lecture 6: NFA to DFA Conversion


NFA to DFA Conversion

1. Start with initial state of NFA


2. For each symbol
2.1 Find the transitions of current state on the set of states
for this symbol.
2.2 If any new state is encountered then add it to DFA.
2.3 Go to step 2
3. Repeat this till no new state is encountered

63 Lecture 6: NFA to DFA Conversion


Example 1: Convert given NFA to DFA

State\ 0 1
Stat 0 1 Input
e\
Inpu 🡪p q q
t q p q, r
🡪p q q q, r p, s p, q, r
q* p q,r p, s p, q p,q
r s p p,q,r p,q,s p,q,r
s * p p p,q p,q q,r
δ ({ q,r}, 0)= δ (q, 0) U δ (r, 0)= p,q,s p,q p,q,r
{p} U {s}= {p,s}

δ ({ q,r}, 1)= δ (q, 1) U δ (r, 1)=


{q, r} U {q}= {p,q,r}
64 Lecture 6: NFA to DFA Conversion
How to decide final state

• The original final state remains final in the converted DFA and also the states in which
original state is combined becomes final state

State\ 0 1
Input
🡪p q q
q* p q, r
q, r* p, s p, q, r
p, s* p, q p,q
p,q,r* p,q,s p,q,r
p,q* p,q q,r
p,q,s* p,q p,q,r

65 Lecture 6: NFA to DFA Conversion


Example 2: Convert given NFA to DFA

0 1 0 1
🡪p p,q p 🡪p p,q p
p, q p, q, r p, r
q r r
p,q,r p,q,r,s p,r
r s -
p, r p,q,s p
s * s s
p,q,r,s p,q,r,s p,r,s
*
p,q,s* p,q,r,s p,r,s
p,r,s* p,q,s p, s
p,s* p,q,s p, s

66 Lecture 6: NFA to DFA Conversion


Thank You
Practice Problems
Example 10: Design DFA which accepts string with length at-least three over
∑ ={0,1}

▪ Input =∑ ={0, 1}
▪ Language
L= {000, 111, 101, 001, 110, 011., 0011, 1100, 000111..................}

▪ Possible States :
qs 🡪 length is 0
q0 🡪 length is 1
q1 🡪 length is 2
q2 🡪 length is equal or greater than 3

▪ Q= {qs, q0, q1 ,q2 }

▪ Initial State ={qs }


▪ Final State ={q2}

69 Practice Problems
Transition Table


0 1
Q
-->qs (len=0) q0 q0

q0 (len=1) q1 q1

q1 (len=2) q2 q2

q2* (len=3) q2 q2

70 Practice Problems
Transition Diagram

Simulation: Simulation:
(qs, 001) (qs, 1011)
(q0,01) (q0,011)
(q1,1) (q1,11)
(q2,ε) (q2,1)
Accept (q2,ε)
Accept

71 Practice Problems
Example 11: Design DFA which accepts string with length at-most three over
∑ ={0,1}

▪ Input =∑ ={0, 1}
▪ Language
L= {0, 1, 00, 10, 000, 111, 101, 001, 110, 011...................}

▪ Possible States :
qs 🡪 length is 0
q0 🡪 length is 1
q1 🡪 length is 2
q2 🡪 length is 3
q3 🡪 length is greater than 3
▪ Q= {qs, q0, q1 ,q2 , q3}

▪ Initial State ={qs}


▪ Final State ={q0,q1,q2 }

72 Practice Problems
Transition Table


0 1
Q
-->qs (len=0) q0 q0

q0 * (len=1) q1 q1

q1 * (len=2) q2 q2

q2* (len=3) q3 q3

q3 (len>3) q3 q3
73 Practice Problems
Transition Diagram

Simulation: Simulation:
(qs, 01) (qs, 1011)
(q0,01) (q0,011)
(q1,ε) (q1,11)
Accept (q2,1)
(q3,ε)
Reject

74 Practice Problems
Example 12. Design a DFA which accepts strings containing odd number of a’s and
any number of b’s over ∑ = { a, b }

▪ Input =∑ ={a, b}
▪ Language
L= {a, ab, ba, abaa, aaab,bab,ababba....................}

▪ Possible States :
qs 🡪 not even or not odd
q0 🡪 odd number of ‘a’s and any number of ‘b’s
q1 🡪 even number of ‘a’s and any number of ‘b’s
Q= {qs, q0, q1}

▪ Initial State ={qs }


▪ Final State ={q0 }

75 Practice Problems
Transition Table

State/ Input a b

--> qs q0 q1

q0 * (odd ‘a’) q1 q0

q1 (even ‘a’) q0 q1

76 Practice Problems
Transition Diagram

77 Practice Problems
Simulation

String = bbabaa
State/ a b
(qs,bbabaa)
Input
(q1,babaa)
(q1,abaa) --> qs q0 q2
(q0,baa)
(q0,aa) q0* q1 q0
(q1,a)
(q0,ε) q1 q0 q1

Accept

78 Practice Problems
Example 13: Design DFA that accepts string containing “ab” as a substring over ∑
={a,b}

▪ Input =∑ ={a, b}

▪ Language
L= {ab, aab,bab,aabb....................}

▪ Possible States :
qs 🡪 no symbol accepting
q0 🡪 substring as ‘a’
q1 🡪 substring as ‘b’
q2 🡪 substring as ‘ab’
▪ Q= {qs, q0, q1 ,q2 }

▪ Initial State ={qs }


▪ Final State ={q2 }

79 Practice Problems
Transition Table

STF delete possible prefix


State/ Input a b Processing
🡪qs (ε) q0 q1 ε.a ε.b
q0 (a) q0 q2 a. a a. b
q1 (b) q0 q1 b. a b. b
q2* (ab) q2 q2

80 Practice Problems
Transition Diagram

81 Practice Problems
Simulation

Consider string= babba


-(qs, babba) STF
-(q1, abba)
-(q0, bba) State/ Input a b
-(q2,ba) 🡪qs (ε) q0 q1
-(q2,a) q0 (a) q0 q2
-(q2, ε)
q1 (b) q0 q1
q2* (ab) q2 q2
Yes

82 Practice Problems
Example 14: Design DFA that accepts strings starting with “ba” over ∑ ={a,b}

▪ Input =∑ ={a, b}

▪ Language
L= {ba, baa, bab,babaa....................}

▪ Possible States :
qs 🡪 no symbol accepting
q0 🡪 substring as ‘b’
q1 🡪 substring as ‘ba’
qr🡪 reject state starts with ‘a’

▪ Q= {qs, q0, q1 , qr}

▪ Initial State ={qs }


▪ Final State ={q1 }

83 Practice Problems
Transition Table

If no matching substring found for any state then transit to reject state

STF
State/ Input a b Processing

🡪qs (ε) qr q0 ε.a ε.b

q0 (b) q1 qr ba bb

q1* (ba) q1 q1 (anything after “ba” can


be accepted)
qr(reject) qr qr (once reached to reject
state then no way to
accept any string )

84 Practice Problems
Transition Diagram

85 Practice Problems
Simulation

Consider string= baa


-(qs, baa) State/ Input a b
-(q0, aa)
-(q1, a) 🡪qs (ε) qr q0
-(q1, ε)
Yes q0 (b) q1 qr

Consider string= aba q1* (ba) q1 q1


-(qs, aba)
-(qr, ba)
qr(reject) qr qr
-(qr, a)
-(qr, ε)
No

86 Practice Problems
Example 15: Design DFA that accepts strings ending with “ab” over ∑ ={a,b}

▪ Input =∑ ={a, b}
▪ Language
L= {ab, aab,bab,abab....................}

▪ Possible States :
qs 🡪 no symbol accepting
q0 🡪 substring as ‘a’
q1 🡪 substring as ‘b’
q2 🡪 substring as ‘ab’
▪ Q= {qs, q0, q1 ,q2 }

▪ Initial State ={qs }


▪ Final State ={q2 }

87 Practice Problems
Transition Table

STF delete possible prefix and apply for final state also
State/ Input a b Processing
🡪qs (ε) q0 q1 ε.a ε.b
q0 (a) q0 q2 a. a a. b
q1 (b) q0 q1 b. a b. b
q2* (ab) q0 q1 a b. a abb

88 Practice Problems
Transition Diagram

89 Practice Problems
Simulation

Consider string= babba


-(qs, babba) STF
-(q1, abba)
-(q0, bba) State/ Input a b
-(q2,ba) 🡪qs (ε) q0 q1
-(q1,a) q0 (a) q0 q2
-(q0, ε)
q1 (b) q0 q1
q2* (ab) q0 q1
No

90 Practice Problems
Example 16:
Q. Consider given transition diagram and convert that to minimized DFA.

Transition Table :
a b
🡪 q0 {q0, q1} q0
q1 - {q1, q2}
q2* {q2, q3} q3
q3 q3 -

91 Practice Problems
Example 16 : (cont..)
Converted DFA :

NFA DFA
a b a b
🡪 q0 {q0, q0 🡪 q0 {q0, q1} q0
q1}
{q0,q1} {q0, q1} {q0,q1, q2}
q1 - {q1,
q2} {q0,q1,q2}* {q0,q1,q2, {q0,q1,q2,
q2* {q2, q3} q3 q3} q3}
{q0,q1,q2,q {q0,q1,q2,q {q0,q1,q2,
q3 q3 - 3}* 3} q3}

92 Practice Problems
Example 16 : (cont..)
Equivalent States: a b
🡪 q0 {q0, q1} q0
{q0,q1} {q0, q1} {q0,q1, q2}
{q0,q1,q2}* {q0,q1,q2, {q0,q1,q2,
q3} q3}
{q0,q1,q2,q {q0,q1,q2,q {q0,q1,q2,
Minimized DFA:
3}* 3} q3}
a b
🡪 q0 {q0, q1} q0
{q0,q1} {q0, q1} {q0,q1, q2}
{q0,q1,q2}* {q0,q1,q2}* {q0,q1,q2}
*
93 Practice Problems
Example 17:Convert Given NFA to Minimized DFA
NFA DFA
a b
a b
🡪 q0 {q0, q1} q1
🡪 q0 { q0, q1
q1} q1* q2 q2
q1* q2 q2 {q0, q1}* {q0, q1, {q1, q2}
q2}
q2 q0 q2
q2 q0 q2
{q1, q2}* {q0, q2} q2
{q0, q2} {q0, q1} {q1, q2}
{q0, q1, {q0, q1, {q1, q2}
q2}* q2}
94 Practice Problems
Example 17: (cont..)
Minimized DFA:
a b
🡪 q0 {q0, q1} q1

q1* q2 q2
{q0, q1}* {q0, q1} {q1, q2}

q2 q0 q2
{q1, q2}* {q0, q2} q2
{q0, q2} {q0, q1} {q1, q2}

95 Practice Problems
Thank You

You might also like