formal language and automata theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4...

29
0 0 1 1 1 1 0 e0-e1 o0-e1 e0-o1 o0-o1 Finite State Automata Example 1 All strings over {0,1} that contain at least two 0s. Example 2 Even number of 0s and 1s Hint: We give some names to the states which will reflect the nature of input to reach there. Solution: State: e0-e1 (meaning even 0s, even 1s) This state is reached with even number of 0s and even number of 1s. State: o0-e1 (meaning odd 0s, even 1s) This state is reached with odd number of 0s and even number of 1s State: o0-o1 (meaning odd 0s, odd 1s); This state is reached with odd number of 0s and odd number of 1s. State: e0-o1 (meaning even 0s, odd 1s); This state is reached with even number of 0s and odd number of 1s Example 3 a n b:n>=0 Solution: 1 0 0 Q0 Q01 Q02 0 1 a b a,b a,b Trap state

Upload: others

Post on 18-Jan-2020

71 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

0

0

1 1 1 1

0

e0-e1 o0-e1

e0-o1 o0-o1

Finite State Automata

Example 1

All strings over {0,1} that contain at least two 0s.

Example 2

Even number of 0s and 1s

Hint: We give some names to the states

which will reflect the nature of input to reach

there.

Solution:

State: e0-e1 (meaning even 0s, even 1s)

This state is reached with even number of 0s and

even number of 1s.

State: o0-e1 (meaning odd 0s, even 1s)

This state is reached with odd number of 0s and even number of 1s

State: o0-o1 (meaning odd 0s, odd 1s); This state is reached with odd number of 0s and odd

number of 1s.

State: e0-o1 (meaning even 0s, odd 1s); This state is reached with even number of 0s and odd

number of 1s

Example 3

anb:n>=0

Solution:

1

0 0

Q0 Q01 Q02

0 1

a

b a,b

a,b

Trap state

Page 2: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

a

a

b

b

a

a,b

Trap state

b

Example 4

All strings of {a,b} with prefix ab

Solution:

Example 5

Automaton over {a,b} accepting all strings not containing aab

Solution:

Example 6

Finite automaton over alphabet {a,b} that start with “a” and

ends with “a”

{awa : w ∈ {a,b}*}

Solution:

a,b

a b

b a

a,b

Trap state

a

a

a

b

a,b

Trap state

b

b

Page 3: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

0

0

1 1 1 1

0

e0-e1 o0-e1

e0-o1 o0-o1

Example 7

DFA for integers divisible by 3.

Solution:

Since the DFA reads integers, they will consist of digit 0-9.

i.e., Σ = {0,1,2,3,4,5,6,7,8,9}

Now we draw the transition diagram.

Note:

State I is reached when remainder = 0

State II is reached when remainder = 1

State III is reached when remainder = 2

Example 8 (WBUT 2010)

2. Draw the transition diagram of a finite state automaton that accepts all strings over {0,1}

a) Having odd number of 0’s b) Having even number of 0’s and even number of 1’s

Solution:

a) b)

1,4,7

2,5,8

2,5,8

1,4,7

1,4,7

0,3,6,9

0,3,6,9 0,3,6,9

I II III

0

0

1 1

Even 0 Odd 0

0

0

1 1

Even 0 Odd 0

Page 4: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

1

1

1

0

0

0

0

ε

ε

ε

ε

ε

ε

ε

Hint: The names of the states have been so given that they indicate the nature of the string seen so

far.

a) Even 0: The machine is in this state means it has seen even number of “0”s.

b) e0-o1: The machine is in this state means it has seen even number of “0”s and odd number

of “1”s.

Example 9 (WBUT 2010)

Design an NFA which accepts set of all binary strings containing 1100 or 1010 as substrings

Solution:

Example 10 (WBUT 2010)

Convert the following from NFA to DFA

0

0, 1 0, 1

1

q0 q1 q2

Page 5: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

0

1 0, 1

1

q0 q1 q2

q01 0

q012

q12 1

0

1

q122

0

1

0

1

Solution:

Transition table of the NFA

We introduce a new state q01, replacing the set {q0,q1}

Now define transitions for q01.

δ (q01, 0) = δ (q0, 0) + δ (q1, 0) = q01 + q2 = {q01,q2} = q012

(new state)

δ (q01, 1) = δ (q0, 1) + δ (q1, 1) = q1 + q2 = {q1,q2} = q12 (new

state)

We have to specify transitions for

q012 and q12.

δ (q012, 0) = δ (q01, 0) + δ (q2, 0) = q012 + φ = q012

δ (q012, 1) = δ (q01, 1) + δ (q2, 1) = q12 + q2 = q122 (new state)

δ (q12, 0) = δ (q1, 0) + δ (q2, 0) = q01 + φ = q01

δ (q12, 1) = δ (q1, 1) + δ (q2, 1) = q2 + q2 =q2

We have to specify transitions for q122.

δ (q122, 0) = δ (q12, 0) + δ (q2, 0) = q01 + φ = q01

δ (q122, 1) = δ (q12, 1) + δ (q2, 1) = q2 + q2 = q2

Add these transitions to the table

And the final or accepting state will be, all states containing q1 = q1,

q01, q12, q122, q0122

Current

State

Input Next

State

q0 0 {q0,q1}

q0 1 q1

q1 0 q2

q1 1 q2

q2 1 q2 Current

State

Input Next

State

q0 0 q01

q0 1 q1

q1 0 q2

q1 1 q2

q2 1 q2

q01 0 q012

q01 1 q12

q012 0 q012

q012 1 q122

q12 0 q01

q12 1 q2

q122 0 q01

q122 1 q2

Page 6: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

Example 11 (WBUT 2010)

Design a 2-input 2-0utput Mealy machine, which takes as input a binary stream and generates an

output of 1 only when a sequence of pattern 01011 is found in the input stream.

Solution:

Mealy machine: output depends on the state.

0

0

0

Output = 1

1

1

1

0

1

1

0

0 1

0

Page 7: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

Finite Automata with output

Example 1:

Convert Mealy to Moore

Present State Next State

Input=0 Output Input=1 Output

q1 q3 1 q2 0

q2 q1 1 q4 1

q3 q2 0 q1 1

q4 q4 1 q3 0

Only state q3 is associated with two outputs “0” and “1”.

Therefore, we split q3 into two new states q30 and q31 for outputs “0” and “1”, respectively.

Modify the Mealy machine in the following way:

Replace q3 by q31 where output is “1”

Replace q3 by q30 where output is “0”

The row with present state = q3 will be duplicated by replacing q3 by q30 and q31

Changes have been highlighted:

Present State Next State

Input=0 Output Input=1 Output

q1 q31 1 q2 0

q2 q1 1 q4 1

q31 q2 0 q1 1

q30 q2 0 q1 1

q4 q4 1 q30 0

Simply, split the state if there are multiple incoming transitions with different outputs.

Page 8: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

Example 2 Moore machine representation of SR Flip Flop

Current

State

Input Next State Output

S1 S=0, R=0 S1 0

S1 S=1, R=0 S2 1

S1 S=0, R=1 S1 0

S2 S=0, R=0 S2 1

S2 S=0, R=1 S1 0

S=0, R=0

S=0, R=0

S=1, R=0

S=0, R=1

S=1, R=1

S=1, R=1

ERROR

S2

Q=1

S1

Q=0

S=0, R=1

S=1, R=0

Page 9: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

Example 3

(1) Construct Moore machine which accepts strings of {1,0} and produces its complement.

Example 4

Design a Mealy machine which reads and accepts strings over {a,b} and outputs “1” when double

occurrence of “a” is found. Else, outputs “0”.

Answer:

Σ = {a,b} Δ = {0,1}

Example Input: a b a b a a b a b a a a b b

Output produced: 0 0 0 0 0 1 0 0 0 0 110 0

Example 5

0

0

1

S2

0

S2

1

S0

ε

0

1

1

a / 1

a / 0

b / 0

b / 0

a /0

b / 0

Page 10: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

Draw a Mealy machine which accepts {a,b} and outputs “0” for first occurrence of “a” and “b”

then 1 for each subsequent “a” and “b”.

Solution:

Convert the above to Moore machine

Current State Input Next State Output

q0 a q1 0

q0 b q2 0

q1 a q1 1

q1 b q2 0

q2 a q1 0

q2 b q2 1 b/1

a/0

a/0

a/1

b/0 b/0

q1 q0

q2

b

a

a

b

q1/0 q0/ε

q3/0

a b

b

q2/1

Page 11: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

Regular Expressions

1. Write a regular expression for each of the following sets of binary strings. Use only the basic

operations.

1. 0 or 11 or 101

2. only 0s

Answers:

a. 0 | 11 | 101

b. 0*

2. Write a regular expression for each of the following sets of binary strings. Use only the basic

operations.

a. all binary strings including empty string

b. all binary strings except empty string

c. begins with 1, ends with 1

d. ends with 00

e. contains at least three 1s

Answers:

a. (0|1)*

b. (0|1) (0|1)*

c. 1(0|1)*1

d. (0|1)*00

e. (0|1)*1(0|1)*1(0|1)*1(0|1)* or 0*10*10*1(0|1)*

3. Write a regular expression to describe inputs over the alphabet {a, b, c} that are in sorted

order.

Answer: a*b*c*.

4. Write a regular expression for each of the following sets of binary strings. Use only the

basic operations.

a. contains at least three consecutive 1s

Page 12: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

b. contains the substring 110

c. contains the substring 1101100

d. doesn't contain the substring 110

Answers:

a. (0|1)*111(0|1)*

b. (0|1)*110(0|1)*

c. (0|1)*1101100(0|1)*

d. (0|10)*1*

5. Write a regular expression for each of the following sets of binary strings. Use only the

basic operations.

a. has at least 3 characters, and the third character is 0

b. number of 0s is a multiple of 3

c. starts and ends with the same character

d. odd length

e. starts with 0 and has odd length, or starts with 1 and has even length

f. length is at least 1 and at most 3

Answers:

a. (0|1)(0|1)0(0|1)*

b. 1* | (1*01*01*01*)*

c. 1(0|1)*1 | 0(0|1)*0 | 0 | 1

d. (0|1)((0|1)(0|1))*

e. 0((0|1)(0|1))* | 1(0|1)((0|1)(0|1))*

f. (0|1) | (0|1)(0|1) | (0|1)(0|1)(0|1)

6. Write a regular expression that matches all strings over the alphabet {a, b, c} that contain:

a. starts and ends with a

b. at most one a

c. at least two a's

d. an even number of a's

Page 13: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

e. number of a's plus number of b's is even

Answers

a. a (a|b|c)* a

b. (b|c)*(a|b|c)(b|c)*

c. (a|b|c)*a(a|b|c)*a(a|b|c)

7. Write a regular expression that matches all strings over the alphabet {x,y} beginning with y and

ending with xx.

Answer

y(x|y)*xx

8. Find regular expression

(b|(ab)*a (aa*ba)*aa* bb(a|b)*

Answer:

(b|(ab)*)*a(aa*ba)*aa*bb(a|b)*

Hint: Solve part by part

9. Regular expressions for the following strings over {0,1}

a. 0 comes before 1 or 1 comes before 0

b. Even length

c. At least one 0 and one 1

d. At most one pair of 0 or at most one pair of 1.

e. At the most two 0

a,b a

b b

b b

a

a

a

Page 14: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

Answers:

a. (01)*+(10)*

b. (00+01+10+11)*

c. 0(0+1)*1(0+1)* +1(0+1)*0(0+1)*

d. 1*001* + 0*110*

e. 1* + 1*01* + 1*01*01*

10. Regular expressions for the following

a. {0, 1, 2}

b. {12n+1 | n>=0}

c. {a2, a5, a8,....} or {a2+3n | n>=0}

Answers:

a. (0+1+2)

b. 1(11)*

c. aa(aaa)*

11. Regular expressions for the following

a. Set of all strings that have no pair of consecutive “0”s

b. Set of all strings ending with either double “b” or single “a”

c. Set of all strings with even number of “a”s followed by an odd number of “b”s

Answers:

a. R =(1+01)*(0+1)

b. R=(a+b)*(a+bb)

c. R=(aa)*(bb)*b

12. Represent the following sets by regular expression

a. {^,ab}

b. {1,11,111,......}

c. {ab,a,b,bb}

Answers:

a. R = ^+ab

Page 15: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

b. R= 1(1)*

c. R= ab + a + b +bb

13. Find regular expressions over {a, b} for the languages defined as follows:

a. L1 = {ambm | m>0}

b. L2 = {a2nb 2m+1 | m,n>=0}

c. L3 = {bmabn | m,n>0}

Answer:

a. aa*bb*

b. (aa)*b(bb)*

c. bb*abb*

14. Find regular expression for the following transition graph:

Answer: 0*(11*0(10*)0)*

0

0

0

1

1

1

Page 16: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

15. a*(a+b)* is equivalent to

a) a* + b* b) (ab)*

c) a* b* d) None of these

Answer: d) None of these

Explanation:

The given expression represents strings which

might have both “a” and “b”

might start with “a” or “b”

Option (a): string is either null, only contains “a” or “b”. So, this is not the one.

Option (b): string is either null, or it must start with “a”. So, this is not the one.

Option (c): string is either null or it contains only “a” or “b”. So, this is not the one.

16. Which of the following denotes the set of all strings not containing 100 as sub-string?

a) 0*(1*0)* b) 0*1010*

c) 0*1*01* d) 0*(10+1)*

Answer: d) 0*(10+1)*

Explanation: There are two optional parts: 0* and (10+1)*

0* cannot definitely have 100 in it.

(10+1)* = NULLL or (10+1)(10+1)(10+1)(10+1)……

No matter whatever way you arrange 10 and 1, the pattern “100” will never

appear.

17. Construct a regular grammar G generating the regular set represented by P = a*b (a+b)*

Solution:

May or may not start with “a”

Must contain a single “b

Ends with any combination of “a” and “b” of any length

So, the language is,

Set of strings over {a,b} containing at least a single “b”.

Page 17: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

Non Deterministic Finite State Machines

Example 1

Find a deterministic finite automaton equivalent to the NFA

M=({q0,q1,q2},{0,1},δ, qo,{q1})

Where δ is given by

Solution:

Consider [q0,q1] as a new state and define

transitions from the same. You get new

combined states [q0,q1,q2] and [q1,q2]. So,

define transitions for them as well to arrive at

the following:

Final states: q1, [q0, q1], [q1, q2] and [q0, q1,

q2] because they all contain the previous final

state q1.

Initial state remains as q0.

Example 2

Construct DFA equivalent to the following NFA:

State \ Input => 0 1

q0 q0,q1 q1

q1 q2 q2

q2 - q2

State \ Input 0 1

q0 [q0,q1] q1

q1 q2 q2

q2 - q2

[q0,q1] [q0,q1,q2] [q1,q2]

[q1,q2] [q2] [q2]

[q0,q1,q2] [q0,q1,q2] [q1,q2]

State / Input a b c

q0 q1,q4 q4 q2,q3

q1 - q4 -

q2 - - q2, q3

q3 - q4 -

q4 - - -

c

a

b a, b c

c

c

b

q0 q1

q2

q3

q4

Page 18: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

Solution:

The transition table:

State / Input a b c

q0 q1,q4 q4 q2,q3

q1 - q4 -

q2 - - q2, q3

q3 - q4 -

q4 - - -

Derive the transition functions for the new states [q1, q2] and [q2, q3]

State / Input a b c

q0 q1,q4 q4 q2,q3

q1 - q4 -

q2 - - q2, q3

q3 - q4 -

q4 - - -

q2,q3 - q4 q2,q3

q1,q4 - q4 -

Note that q2 and q3 as separate states are unreachable. So, they are removed, though present in

combination with others, viz., [q2,q3].

Therefore, the final state table becomes:

Example 3

Obtain an NFA which should accept a language La, given by La = {x ∈ {a, b}* ||x|>=3 and third

symbol of x from the right is ‘a”

Solution:

The conditions are

(a) Third symbol from the right is “a”.

(b) Symbol in any other position can be “a” or “b”.

State / Input a b c

q0 q1,q4 q4 q2,q3

q1 - q4 -

q4 - - -

q2,q3 - q4 q2,q3

q1,q4 - q4 -

Page 19: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

Example 4:

Convert the NFA with ε-move (also termed NFA- ε for convenience) to an equivalent NFA

without ε-move.

Solution:

M = (q0,q1,q2,q3,q4,q5,q6, {a,b}, δ, q0, {q6})

Now, let us determine the ε-Closures.

ε-Closure (q0) = {q0}

ε-Closure (q1) = {q1}

ε-Closure (q2) = {q2}

ε-Closure (q3) = {q3,q4,q5}

ε-Closure (q4) = {q4,q5}

ε-Closure (q5) = {q5}

ε-Closure (q6) = {q6}

So, NFA without ε-moves:

N’ = ({{q0},{q1},{q2}, {q3,q4,q5}, {q4,q5},{q5},{q6}}, {a,b}, δ’,q0’,F’)

We will have to determine

δ’ the transition function

q0’ the new initial state

F’ the new final state or set of states

Let us define the new transition function δ’

δ’({q0},a) = {q3,q4,q5}

δ’({q0},b)={q1,q2}

δ’({q1},a) = {q6}

δ’({q1},b) = φ

δ’({q2},a) = φ

δ’({q2},b)={q3,q4,q5}

δ’({q4,q5},a) = {q4,q5}

δ’({q4,q5},b) = {q6}

δ’({q5},a) = φ

δ’({q5},b)={q0}

δ’({q6},a) = φ

b

b

b

b a a

a

ε ε

q0 q1 q6

q2 q3 q4 q5

Page 20: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

δ’({q6},b)= φ

δ’({q3,q4,q5},b) = {q6}

δ’({q3,q4,q5},a) = {q4,q5}

Once again, reminding about the technique of the trasformed transition function:

δ’({q2},b) = ε-Closure (δ({q2},b) = ε-Closure (q3,b) = {q3,q4,q5}

Draw the diagram using the E-Closure states only.

Remove {q5} because it is unreachable.

b

b

b

b a

a

a

a

{q0} {q1} {q6}

{q2} {q3, q4,

q5} {q4,q5}

b

{q5} b

b

b

b

b a

a

a

a

{q0} {q1} {q6}

{q2} {q3, q4,

q5} {q4,q5}

b

Page 21: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

1

0

1 0

Q0 Q1 Q2

Example 5

Design a Finite State Automaton which reads binary string and accepts only those that end with 01.

Solution:

Since we are in the topic of Non Deterministic Finite Automata (NFA), we will draw one.

State Transition Table

Note: In the last two entries, though the next state or to state is a single and not a set, the

convention is to represent is a set containing one element.

Note: There is no entry where From State = Q1 and Input Symbol = 0

i.e., δ (Q1, 0) = {}; It is a null state.

Example 6

Convert the above NFA to a DFA

Solution:

We take up the transition table and replace each set

of states by a single new state.

In the example, we combine replace {Q0,Q1} by

Q01

From State Input Symbol To State

Q0 0 {Q0, Q1}

Q0 1 {Q0}

Q1 1 {Q2}

From State Input Symbol To State

Q0 0 Q01

Q0 1 Q0

Q1 1 Q2

Initial State = Q0

Final State = Q2

Alphabet = {0,1}

Page 22: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

Now we need to define transition rules for this new

state Q01.

δ (Q01, 0) = δ (Q0, 0) + δ (Q1, 0) = Q01 + Φ = Q01

δ (Q01, 1) = δ (Q0, 1) + δ (Q1, 1) = Q0 + Q2 = Q02

(again a new state)

Define transition rules for this new state Q02.

δ (Q02, 0) = δ (Q0, 0) + δ (Q2, 0) = Q01 + Φ = Q01

δ (Q02, 1) = δ (Q0, 1) + δ (Q2, 1) = Q0 + Φ = Q0

Incorporating the above entries, the transition table

for the resultant DFA is:

Some action is still remaining.

1. We have to identify the final state. The final

state in the NFA was Q2. Therefore, the final state in

the equivalent DFA will be Q02 as it contains Q2.

2. There is no transition from the state Q2.

Hence, Q2 becomes redundant.

The final transition table is shown above.

Drawing the diagram:

Example 7

All strings over {0,1} that do not end with 01.

Solution:

First draw the NFA for strings that end with 01 and then take complement.

Then convert each non final state to final state and each final state to a non final state.

From State Input Symbol To State

Q0 0 Q01

Q0 1 Q0

Q1 1 Q2

Q01 0 Q01

Q01 1 Q02

Q02 0 Q01

Q02 1 Q0

From State Input Symbol To State

Q0 0 Q01

Q0 1 Q0

Q01 0 Q01

Q01 1 Q02

Q02 0 Q01

Q02 1 Q0

1

1 0

Q0 Q01 Q02

0

0

1

1

1 0

Q0 Q1 Q2

0

Page 23: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

0

1

Q0 Q1

0

Alternative: Directly. This way the number of states

is reduced.

Example 8

Draw NFA for (0+1)*(010(0+1)*101 + 101(0+1)*010)(0+1)*

Solution

Example 9

DFA converted from an NFA with n states can have maximum

a) n states b) n! states

c) 2n states d) nC2 states

Answer: c) 2n states

Example 10

Solution:

0

0, 1 0, 1

1

q0 q1 q2

Page 24: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

Transition table of the NFA

We introduce a new state q01, replacing the set {q0,q1}

Now define transitions for q01.

δ (q01, 0) = δ (q0, 0) + δ (q1, 0) = q01 + q2 = {q01,q2} = q012

(new state)

δ (q01, 1) = δ (q0, 1) + δ (q1, 1) = q1 + q2 = {q1,q2} = q12 (new

state)

We have to specify transitions for

q012 and q12.

δ (q012, 0) = δ (q01, 0) + δ (q2, 0) = q012 + φ = q012

δ (q012, 1) = δ (q01, 1) + δ (q2, 1) = q12 + q2 = q122 (new state)

δ (q12, 0) = δ (q1, 0) + δ (q2, 0) = q01 + φ = q01

δ (q12, 1) = δ (q1, 1) + δ (q2, 1) = q2 + q2 =q2

We have to specify transitions for q122.

δ (q122, 0) = δ (q12, 0) + δ (q2, 0) = q01 + φ = q01

δ (q122, 1) = δ (q12, 1) + δ (q2, 1) = q2 + q2 = q2

Add these transitions to the table

And the final or accepting state will be, all states containing q1 = q1,

q01, q12, q122, q0122

Current

State

Input Next

State

q0 0 {q0,q1}

q0 1 q1

q1 0 q2

q1 1 q2

q2 1 q2 Current

State

Input Next

State

q0 0 q01

q0 1 q1

q1 0 q2

q1 1 q2

q2 1 q2

q01 0 q012

q01 1 q12

q012 0 q012

q012 1 q122

q12 0 q01

q12 1 q2

q122 0 q01

q122 1 q2

Page 25: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

As such, the nature of the automaton is like this:

It accepts any string containing a single symbol, be it 0 or 1

It accepts any longer string ending with single 1 and optionally with any number of leading 0s.

So, the regular expression will be: 0*1 + 0

0

1 0, 1

1

q0 q1 q2

q01 0

q012

q12 1

0

1

q122

0

1

0

1

Page 26: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

Context Free Grammar

Example 1: L = { ww | w ∈{0, 1} } is not context free.

Proof (by contradiction): Assume L is CF, let n be the constant asserted by the pumping lemma.

Consider z = 0n+1 1n+1 0n+1 1n+1 = uvwxy. Using k = 0, the lemma asserts z0 = uwy ∈L, but we

show that z0 cannot have the form t t, for any string t, and thus that z0 ∈L, leading to a

contradiction. Recall that |v w x| ≤ n, and thus, when we delete v and x, we delete symbols that are

within a distance of at most n from each other. By analyzing three cases we show that, under this

restriction, it is impossible to delete symbols in such a way as to retain the property that the

shortened string z0 = u w x has the form t t. We illustrate this using the example n = 3, but the

argument holds for any n.

Given z = 0000111100001111, slide a window of length n = 3 across z, and delete any characters

you want from within the window. Observe that the blocks of 0s and of 1s within z are so long that

the truncated z, call it z’, still has the form “0s 1s 0s 1s”. This implies that if z’ can be written as z’

= t t, then t must have the form t = “0s 1s”. Checking the three cases: the window of length 3 lies

entirely within the left half of z; the window straddles the center of z; and the window lies entirely

within the right half of z, we observe that in none of these cases z’ has the form z’ = t t, and thus

that z0 = u w y ∉L.

QED

Example 2: L = {an! | n>=0} is not context free.

Proof (by contradiction): Assume L is CF, let n be the constant asserted by the pumping lemma.

We let z = uvwxy;

Since z∈L then, aaccording to pumping lemma, uviwxiy ∈L

Let’s denote uviwxiy as zi

So, z2= uv2wx2y, z3= uv3wx3y, etc.

Thus, z0 = uv3wx3y = uv0wx0y = wxz

If z ∈L, then also z0∈L

Given the opponent’s choice for m, we pick am!

Obviously, whatever the decomposition is, it must be of the form v= ak, x= al

The difference between z and z0 is vx, i.e., ak al = ak+l

Page 27: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

So, z0 = uv0wx0y = uwy = uvwxy / vx = am! / ak+l = am!-(k+l)

Then z0 = uwy has length m!-(k+l).

Since, according to our assumption, z0∈L, hence z0 will be of the form aj!

This string is in L only if m!-(k+1) = j! for some j

But it may not be true. So, this violates pumping lemma.

Hence L is not context free.

Example 3: L = {ambmambm | m>=0} is not context free.

Solution:

am bm am bm

We choose m such that |z| >=m.

Now, we decompose z=uvwxy;

For the grammar to be context free, there should exist some vwx, such that |vwx|<=m which will

make uviwxiy ∈L

But, in whichever way we choose vxy, since the length is limited to m, following are only

possible:

B D F

aa……..aabb……..bbaa……..aabb……..bb

A C E G

The double headed arrowed line shows the possible layouts of vxy whose length is limited by m.

A,B,C,D,E,F and G denote various possible positions of vwx.

Therefore, following cases are possible:

(A) vwx = am

(B) vwx = akbm-k

(C) vwx = bm

(D) vwx = bkam-k

(E) vwx = am

(F) vwx = akbm-k

(G) vwx = bm

Cases A, C, E and G are simpler to analyze. Pumping v and x will increase only the number of

“a”s or “b”s somewhere on the string. Therefore, uviwxiy will cause the increase of “a”s or “b”s

only at one place.

aa…………abb……....baa……aaaabbb……bb

Page 28: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

So, we can only have:

Case A: zi = anbmambm

Case C: zi = ambnambm

Case E: zi = ambmanbm

Case G: zi = ambmambn

For some n > m.

Cases B, D and F, it is possible to pump v and x equally such that the counts of both “a”s and “b”s

are incremented by equal number of times, but that will be only in one series of “a”s and one series

of “b”s.

So, we can only have:

Case B: zi = anbn ambm

Case D: zi = ambn anbm

Case F: zi = anbn ambm

For some n >m.

Hence, zi, i.e., uviwxiy is not in L

Example 4: L = {anaj | n=j2} is not context free.

Solution:

We pick our string as z = am^2bm.

So, |z| > m.

am^2 bm

aaa……………………….aabbbb…bb…….b

We decompose the string as z=uvwxy.

For the grammar to be context free, we should be able to choose a vxy anywhere along the string

such that zi = uviwxiy is in L

Length of vxy being limited by m, following cases are possible:

(A) vxy = only ”a”s = am

Pumping v and y will result in am^2+kbm

Obviously, m2+k < m2

Hence zi is not in L

u v x y z

=ak1 =bk2

Page 29: Formal Language and Automata Theory · 2017-08-10 · q1 q3 1 q2 0 q2 q1 1 q4 1 q3 q2 0 q1 1 q4 q4 1 q3 0 Only state q3 is associated with two outputs “0” and “1”. Therefore,

(B) vxy = series of “a”s followed by series of “b”s= akbm-k

aaa……………………….aabbbb…bb…….b

vxy

If we write z as am^2-kakbm-kbk

Pumping v and y will result in zi = am^2-kak+ibm-k+ibk = am^2-k+k+ibm-k+i+k

= am^2+ibm+i

Obviously, m2+i < (m + i)2

Hence zi is not in L

So, in whatever way we choose vxy, the pumped up string does not belong to L.

That means L is not context free.