0% found this document useful (0 votes)
11 views66 pages

Automata Chapter 2 Exercises

Uploaded by

Sarafat Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views66 pages

Automata Chapter 2 Exercises

Uploaded by

Sarafat Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

Finite Automata Exercises

Course: Theory of Computation (CSE 2205)

Prepared By
Noshin Tahsin
Lecturer, Department of Computer Science and Engineering
BSMRU, Kishoreganj
Exercise 1

Construct the transition table for the following DFA:

Q = {q0, q1, q2}

∑ = {0, 1}
Transition Diagram:
q0 = {q0}

F = {q2}

2
Exercise 1 (Solution)

Construct the transition table for the following DFA:

Q = {q0, q1, q2} Transition Diagram:


∑ = {0, 1}

q0 = {q0}

F = {q2}
Present Next state Next State
State for Input 0 of Input 1

→q0 q0 q1

q1 q2 q1

*q2 q2 q2

3
Exercise 2

Draw the state diagram and transition table for a DFA with ∑ = {0, 1} accepts all
starting with 0.

4
Exercise 2 (Solution)

Draw the state diagram and transition table for a DFA with ∑ = {0, 1} accepts all
starting with 0.

5
Exercise 3

Draw the state diagram and transition table for a DFA with ∑ = {0, 1} accepts all
ending with 0.

6
Exercise 3 (Solution)

Draw the state diagram and transition table for a DFA with ∑ = {0, 1} accepts all
ending with 0.

7
Exercise 4
Design a FA with ∑ = {0, 1} accepts those string which starts with 1 and ends with 0.

8
Exercise 4 (Solution)
Design a FA with ∑ = {0, 1} accepts those string which starts with 1 and ends with 0.

9
Exercise 5
Design a FA with ∑ = {0, 1} accepts the only input 101.

10
Exercise 5 (Solution)
Design a FA with ∑ = {0, 1} accepts the only input 101.

11
Exercise 6
Design a FA with ∑ = {0, 1} accepts the strings with an even number of 0's followed by
single 1.

12
Exercise 6 (Solution)
Design a FA with ∑ = {0, 1} accepts the strings with an even number of 0's followed by
single 1.

13
Exercise 7
Construct the transition table for the following NFA.

Q = {q0, q1, q2}


∑ = {0, 1}
q0 = {q0}
F = {q2}

14
Exercise 7 (Solution)
Construct the transition table for the following NFA.

Q = {q0, q1, q2}


∑ = {0, 1}
q0 = {q0}
F = {q2}

Present Next state Next State


State for Input 0 of Input 1

→q0 q0, q1 q1

q1 q2 q0

*q2 q2 q1, q2

15
Exercise 8
Construct the NFA with ∑ = {0, 1} that accepts all strings of length at least 2.

16
Exercise 8 (Solution)
Construct the NFA with ∑ = {0, 1} that accepts all strings of length at least 2.

Present Next Next


State state for State of
Input 0 Input 1

→q0 q1 q1

q1 q2 q2

*q2 ε ε

17
Exercise 9
Design an NFA for the transition table as given below:

Present 0 1
State

→q0 q0, q1 q0, q2

q1 q3 ε

q2 q2, q3 q3

→q3 q3 q3

18
Exercise 9 (Solution)
Design an NFA for the transition table as given below:

Present 0 1
State

→q0 q0, q1 q0, q2

q1 q3 ε

q2 q2, q3 q3

→q3 q3 q3

19
Exercise 10
Design an NFA with ∑ = {0, 1} accepts all strings in which the third symbol from the right end
is always 0.

20
Exercise 10 (Solution)
Design an NFA with ∑ = {0, 1} accepts all strings in which the third symbol from the right end
is always 0.

21
Exercise 11

Draw a non-deterministic finite automaton that accepts 00 and 11 at the end of a


string containing 0, 1 in it; e.g., it accepts 01010100 and 01010111, but not 000111010.

22
Exercise 11 (Solution)

Draw a non-deterministic finite automaton that accepts 00 and 11 at the end of a


string containing 0, 1 in it; e.g., it accepts 01010100 and 01010111, but not 000111010.

23
Exercise 12
Convert the given NFA to DFA.

24
Solution
State 0 1

→q0 q0 q1

q1 {q1, q2} q1

*q2 q2 {q1, q2}

Step 1: For the given transition


diagram, we will first construct the
transition table.

25
Solution Step 2: Now we will obtain δ'
transition for all the states
State 0 1
The δ' transition for state q0 is
obtained as:
→q0 q0 q1
δ'([q0], 0) = [q0]
δ'([q0], 1) = [q1]
q1 {q1, q2} q1
The δ' transition for state q1 is
obtained as:
*q2 q2 {q1, q2}
δ'([q1], 0) = [q1, q2] (new state generated)
δ'([q1], 1) = [q1]
Table: The transition table for the
NFA

26
Solution Step 2: Now we will obtain δ'
transition for all the states
State 0 1
The δ' transition for state q0 is
obtained as:
→q0 q0 q1
δ'([q0], 0) = [q0]
δ'([q0], 1) = [q1]
q1 {q1, q2} q1
The δ' transition for state q1 is
obtained as:
*q2 q2 {q1, q2}
δ'([q1], 0) = [q1, q2] (new state generated)
δ'([q1], 1) = [q1]
Table: The transition table for the
NFA The δ' transition for state q2 is
obtained as:

δ'([q2], 0) = [q2]
δ'([q2], 1) = [q1, q2]

27
Solution Step 2: Now we will obtain δ'
transition for all the states
State 0 1
Now we will obtain δ' transition on [q1, q2].

δ'([q1, q2], 0) = δ(q1, 0) ∪ δ(q2,


→q0 q0 q1
0)

= {q1, q2} ∪ {q2}


q1 {q1, q2} q1
= [q1, q2]
*q2 q2 {q1, q2} δ'([q1, q2], 1) = δ(q1, 1) ∪ δ(q2, 1)

= {q1} ∪ {q1, q2}


Table: The transition table for the = {q1, q2}
NFA
= [q1, q2]

28
Solution Step 3: Construct the transition table for the DFA

State 0 1 State 0 1

→q0 q0 q1 →[q0] [q0] [q1]

q1 {q1, q2} q1 [q1] [q1, q2] [q1]

*q2 q2 {q1, q2} *[q2] [q2] [q1, q2]

*[q1, q2] [q1, q2] [q1, q2]


Table: The transition table for the
NFA

29
Solution
Step 4: Draw the transition diagram

State 0 1

→[q0] [q0] [q1]

[q1] [q1, q2] [q1]

*[q2] [q2] [q1, q2]

*[q1, q2] [q1, q2] [q1, q2]

The state q2 can be eliminated


Table: The transition table for the DFA because q2 is an unreachable state.

30
Solution
State 0 1

→[q0] [q0] [q1]

[q1] [q1, q2] [q1]

*[q2] [q2] [q1, q2]


The state q2 can be eliminated because q2 is an
unreachable state. You can also construct the
*[q1, q2] [q1, q2] [q1, q2]
whole table according to the complete subset
construction approach. However, it will end up
creating unreachable states that can be
Table: The transition table for the DFA discarded.

31
Exercise 13
Convert the given NFA to DFA.

32
Solution
Step 1: For the given transition diagram,
we will first construct the transition
table.

State 0 1

→q0 {q0, q1} {q1}

*q1 ϕ {q0, q1}

33
Step 2: Obtain transition states
Solution
Now we will obtain δ' transition for state q0.
Step 1: For the given transition diagram,
we will first construct the transition
δ'([q0], 0) = {q0, q1}
table.
= [q0, q1] (new state generated)
State 0 1
δ'([q0], 1) = {q1} = [q1]

→q0 {q0, q1} {q1} The δ' transition for state q1 is obtained as:

δ'([q1], 0) = ϕ
*q1 ϕ {q0, q1}
δ'([q1], 1) = [q0, q1]

34
Step 2: Obtain transition states
Solution
Now we will obtain δ' transition on [q0, q1].
Step 1: For the given transition diagram, δ'([q0, q1], 0) = δ(q0, 0) ∪ δ(q1,
we will first construct the transition
0)
table.
= {q0, q1} ∪ ϕ
State 0 1
= {q0, q1}

= [q0, q1]
→q0 {q0, q1} {q1}
Similarly,
δ'([q0, q1], 1) = δ(q0, 1) ∪ δ(q1, 1)
*q1 ϕ {q0, q1}
= {q1} ∪ {q0, q1}

= {q0, q1}

= [q0, q1]

35
Step 3: Construct the transition table from
Solution the transition states

Step 1: For the given transition diagram, As in the given NFA, q1 is a final state, then in DFA
we will first construct the transition wherever, q1 exists that state becomes a final state.
table. Hence in the DFA, final states are [q1] and [q0, q1].
Therefore set of final states F = {[q1], [q0, q1]}.

State 0 1 The transition table for the constructed DFA will be:

State 0 1
→q0 {q0, q1} {q1}

→[q0] [q0, q1] [q1]


*q1 ϕ {q0, q1}

*[q1] ϕ [q0, q1]

*[q0, q1] [q0, q1] [q0, q1]

36
Solution Step 4: Construct the transition diagram
from the transition table
The transition table for the constructed DFA will
be:

State 0 1

→[q0] [q0, q1] [q1]

*[q1] ϕ [q0, q1]

*[q0, q1] [q0, q1] [q0, q1]

37
Solution Step 4: Construct the transition diagram
Even we can change the name of the from the transition table
states of DFA.

A = [q0]
B = [q1]
C = [q0, q1]

With these new names, the DFA will


be as follows:

38
Minimization of DFA

● Minimization of DFA means reducing the number of states from a given FA.
Thus, we get the FSM (finite state machine) with redundant states after
minimizing the FSM.

● We have to follow the various steps to minimize the DFA. These are as follows:

● Step 1: Remove all the states that are unreachable from the initial state via any
set of the transition of DFA.

● Step 2: Draw the transition table for all pairs of states.

39
Exercise 14
Minimize the following DFA.

40
Solution
Minimize the following DFA.

Fig. Minimized DFA

41
Exercise 16: Eliminating ε Transitions
Convert the following NFA with ε to NFA without ε.

42
Solution
Convert the following NFA with ε to NFA without ε.

We will first obtain ε-closures of q0, q1 and q2 as follows:

ε-closure(q0) = {q0}
ε-closure(q1) = {q1, q2}
ε-closure(q2) = {q2}

43
Solution
Convert the following NFA with ε to NFA without ε.
Now the δ' transition on each input symbol is
obtained as:

δ'(q0, a) = ε-closure(δ(δ^(q0, ε),a))


= ε-closure(δ(ε-closure(q0),a))
= ε-closure(δ(q0, a))
= ε-closure(q1)
We will first obtain ε-closures of q0, q1 and q2 as follows: = {q1, q2}

ε-closure(q0) = {q0} δ'(q0, b) = ε-closure(δ(δ^(q0, ε),b))


ε-closure(q1) = {q1, q2} = ε-closure(δ(ε-closure(q0),b))
ε-closure(q2) = {q2} = ε-closure(δ(q0, b))

44
Solution
Convert the following NFA with ε to NFA without ε.
Now the δ' transition on q1 is obtained as:

δ'(q1, a) = ε-closure(δ(δ^(q1, ε),a))


= ε-closure(δ(ε-closure(q1),a))
= ε-closure(δ(q1, q2), a)
= ε-closure(δ(q1, a) ∪ δ(q2, a))
= ε-closure(Ф ∪ Ф)
We will first obtain ε-closures of q0, q1 and q2 as follows: =Ф

ε-closure(q0) = {q0} δ'(q1, b) = ε-closure(δ(δ^(q1, ε),b))


ε-closure(q1) = {q1, q2} = ε-closure(δ(ε-closure(q1),b))
ε-closure(q2) = {q2} = ε-closure(δ(q1, q2), b)
= ε-closure(δ(q1, b) ∪ δ(q2, b))
= ε-closure(Ф ∪ q2)
= {q2}

45
Solution
Convert the following NFA with ε to NFA without ε.
The δ' transition on q2 is obtained as:

δ'(q2, a) = ε-closure(δ(δ^(q2, ε),a))


= ε-closure(δ(ε-closure(q2),a))
= ε-closure(δ(q2, a))
= ε-closure(Ф)

We will first obtain ε-closures of q0, q1 and q2 as follows:
δ'(q2, b) = ε-closure(δ(δ^(q2, ε),b))
ε-closure(q0) = {q0} = ε-closure(δ(ε-closure(q2),b))
ε-closure(q1) = {q1, q2} = ε-closure(δ(q2, b))
ε-closure(q2) = {q2} = ε-closure(q2)
= {q2}

46
Solution
Convert the following NFA with ε to NFA without ε.
Now we will summarize all the
computed δ' transitions:

δ'(q0, a) = {q1, q2}


δ'(q0, b) = Ф
δ'(q1, a) = Ф
δ'(q1, b) = {q2}
We will first obtain ε-closures of q0, q1 and q2 as follows: δ'(q2, a) = Ф
δ'(q2, b) = {q2}
ε-closure(q0) = {q0}
ε-closure(q1) = {q1, q2}
ε-closure(q2) = {q2}

47
Solution
ε-closures of q0, q1 and q2:
States a b
ε-closure(q0) = {q0}
ε-closure(q1) = {q1, q2}
→q0 {q1, q2} Ф
ε-closure(q2) = {q2}

All the computed δ' transitions: *q1 Ф {q2}

δ'(q0, a) = {q1, q2}


δ'(q0, b) = Ф *q2 Ф {q2}
δ'(q1, a) = Ф
δ'(q1, b) = {q2}
δ'(q2, a) = Ф State q1 and q2 become the final state as ε-closure
δ'(q2, b) = {q2} of q1 and q2 contain the final state q2.

48
Solution The NFA can be shown by the following transition
diagram:
States a b

→q0 {q1, q2} Ф

*q1 Ф {q2}

*q2 Ф {q2}

49
Conversion from NFA with ε to DFA

Step 1: We will take the ε-closure for the starting state of NFA as a starting state of
DFA.

Step 2: Find the states for each input symbol that can be traversed from the present.
That means the union of transition value and their closures for each state of NFA
present in the current state of DFA.

Step 3: If we found a new state, take it as current state and repeat step 2.

Step 4: Repeat Step 2 and Step 3 until there is no new state present in the transition
table of DFA.

Step 5: Mark the states of DFA as a final state which contains the final state of NFA.

50
Conversion from NFA with ε to DFA

ε-closure {q0} = {q0, q1, q2}


ε-closure {q1} = {q1}
ε-closure {q2} = {q2}
ε-closure {q3} = {q3}
ε-closure {q4} = {q4}

Now, let ε-closure {q0} = {q0, q1, q2} be


state A.

51
Conversion from NFA with ε to DFA

ε-closure {q0} = {q0, q1, q2}


ε-closure {q1} = {q1}
ε-closure {q2} = {q2}
ε-closure {q3} = {q3}
ε-closure {q4} = {q4}

Now, let ε-closure {q0} = {q0, q1, q2} be


δ'(A, 0) = ε-closure {δ((q0, q1, q2), 0) } state A.
= ε-closure {δ(q0, 0) ∪ δ(q1, 0) ∪ δ(q2, 0) }
= ε-closure {q3}
= {q3} call it as state B.

δ'(A, 1) = ε-closure {δ((q0, q1, q2), 1) }


= ε-closure {δ((q0, 1) ∪ δ(q1, 1) ∪ δ(q2, 1) }
= ε-closure {q3}
= {q3} = B.

52
Conversion from NFA with ε to DFA

ε-closure {q0} = {q0, q1, q2}


ε-closure {q1} = {q1}
ε-closure {q2} = {q2}
ε-closure {q3} = {q3}
ε-closure {q4} = {q4}

Now, let ε-closure {q0} = {q0, q1, q2} be


δ'(A, 0) = ε-closure {δ((q0, q1, q2), 0) } state A.
= ε-closure {δ(q0, 0) ∪ δ(q1, 0) ∪ δ(q2, 0) }
= ε-closure {q3}
The partial DFA will be
= {q3} call it as state B.

δ'(A, 1) = ε-closure {δ((q0, q1, q2), 1) }


= ε-closure {δ((q0, 1) ∪ δ(q1, 1) ∪ δ(q2, 1) }
= ε-closure {q3}
= {q3} = B.

53
Conversion from NFA with ε to DFA
ε-closure {q0} = {q0, q1, q2} δ'(B, 0) = ε-closure {δ(q3, 0) }
ε-closure {q1} = {q1} =ϕ
ε-closure {q2} = {q2} δ'(B, 1) = ε-closure {δ(q3, 1) }
ε-closure {q3} = {q3}
= ε-closure {q4}
ε-closure {q4} = {q4}
= {q4} i.e. state C
Now, let ε-closure {q0} = {q0, q1, q2} be
state A.

δ'(C, 0) = ε-closure {δ(q4, 0) }


The partial DFA will be

δ'(C, 1) = ε-closure {δ(q4, 1) }

54
Conversion from NFA with ε to DFA
δ'(B, 0) = ε-closure {δ(q3, 0) }
The partial DFA will be =ϕ
δ'(B, 1) = ε-closure {δ(q3, 1) }
= ε-closure {q4}
= {q4} i.e. state C

The final DFA will be


δ'(C, 0) = ε-closure {δ(q4, 0) }

δ'(C, 1) = ε-closure {δ(q4, 1) }

55
Conversion from NFA with ε to DFA
Convert the given NFA into its equivalent DFA.

ε-closure(q0) = {q0, q1, q2} Now we will obtain δ' transition.


ε-closure(q1) = {q1, q2}
ε-closure(q2) = {q2} Let ε-closure(q0) = {q0, q1, q2} call it as
state A.

56
Conversion from NFA with ε to DFA
Convert the given NFA into its equivalent DFA.

ε-closure(q0) = {q0, q1, q2}


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

57
Conversion from NFA with ε to DFA
Convert the given NFA into its equivalent DFA.

ε-closure(q0) = {q0, q1, q2}


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

Thus we have obtained

δ'(A, 0) = A
δ'(A, 1) = B
δ'(A, 2) = C

58
Conversion from NFA with ε to DFA
Convert the given NFA into its equivalent DFA. The partial DFA will be:

ε-closure(q0) = {q0, q1, q2}


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

Thus we have obtained

δ'(A, 0) = A
δ'(A, 1) = B
δ'(A, 2) = C

59
Conversion from NFA with ε to DFA
Convert the given NFA into its equivalent DFA.

The partial DFA will be:

60
Conversion from NFA with ε to DFA
Convert the given NFA into its equivalent DFA.

Thus we have obtained

δ'(B, 0) = ϕ
δ'(B, 1) = B
δ'(B, 2) = C

61
Conversion from NFA with ε to DFA
Convert the given NFA into its equivalent DFA.
The partial transition diagram will be

Thus we have obtained

δ'(B, 0) = ϕ
δ'(B, 1) = B
δ'(B, 2) = C

62
Conversion from NFA with ε to DFA
Convert the given NFA into its equivalent DFA.

ε-closure(q0) = {q0, q1, q2}


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

63
Conversion from NFA with ε to DFA
Convert the given NFA into its equivalent DFA.
Hence the final DFA is:

64
Conversion from NFA with ε to DFA
Convert the given NFA into its equivalent DFA.
Hence the final DFA is:

As A = {q0, q1, q2} in which final state q2


lies, hence A is a final state.

B = {q1, q2} in which the state q2 lies,


hence B is also a final state.

C = {q2} in which the state q2 lies, hence


C is also a final state.

65
Resource List
1. DFA | Deterministic Finite Automata - Javatpoint

2. Examples of DFA

3. NFA | Non-Deterministic Finite Automata - Javatpoint

4. Examples of NFA - Javatpoint

5. Automata Conversion from NFA to DFA - Javatpoint

6. Minimization of DFA - Javatpoint

7. Neso Academy Playlist:


https://youtu.be/40i4PKpM0cI?si=1ddTHOJYCJGMpKLG

66

You might also like