Automata Questions With Solution
Automata Questions With Solution
TECHNOLOGY
DEPARTMENT OF CSE
Faculty Name: Mr. Vivek Kumar, Dr. Satendra Kumar,Mr. Ashu Singh
Solution:
Solution:
● Q = {q0, q1}
● q0 = initial state
● F = {q1}
● δ:
δ(q0,0) = q0 δ(q0,1) = q1
δ(q1,0) = q0 δ(q1,1) = q1
Solution:
Solution
● Every NFA can be converted to an equivalent DFA using the subset construction
method:
o Each DFA state represents a subset of NFA states.
o DFA simulates all possible moves of the NFA simultaneously.
Solution:
Solution:
Solution:
Steps:
1-Remove unreachable states.
2-Partition states into groups: {final states} and {non-final states}.
3-Refine partitions until no more changes occur (distinguish states).
Example:
Given DFA:
mathematica
CopyEdit
States = {A,B,C,D}, Final={C}
Transitions:
A-0B, A-1C
B-0A, B-1D
C-0C, C-1C
D-0C, D-1C
Solution:
Q = {q0, q1}
q0 = initial, final
q1 = non-final
δ:
δ(q0,0)=q1 δ(q0,1)=q0
δ(q1,0)=q0 δ(q1,1)=q1
Question 11.
Question 12.
Question 13
Question 14:
Question 15:
Question 16:
Question 17:
Draw a deterministic and non-deterministic finite automate which accept
00 and 11 at the end of a string containing 0, 1 in it, e.g., 01010100 but
not 000111010. Explanation - Design a DFA and NFA of a same string if
input value reaches the final state then it is acceptable otherwise it is not
acceptable. NFA of the given string is as follows:
Question 18:
Step-by-step Breakdown:
DFA Design:
● Q: Set of states
● Σ: Input alphabet = {0, 1}
● δ: Transition function
● q₀: Start state
● F: Set of accepting states
States:
Transitions:
● F = {q₁}
● 1 q q ✅
● 01 q q q ✅
● 0011 q q q q q ✅
● 0 q (no 1s at all) ❌
● 10 q q q_dead ❌
Question 19:
Construct a Moore and Melay Machine to accept modulo 5 counter for binary inputs.
Sol: In a Moore machine, the output depends solely on the current state, not on the input. For
a modulo-5 counter, we need to design a 3-bit counter that resets after counting up to 4
(binary 100).
States and Outputs
● States (S): 5 states representing counts 0 to 4.
● Output (O): The output can be the binary representation of the current count.
State Diagram
markdown
Copy code
S0 (000) S1 (001) S2 (010) S3 (011) S4 (100)
________________________________________________
● Transitions: On each clock pulse, the counter increments by 1.
● Reset: After reaching state S4 (100), the counter resets to state S0 (000).
State Diagram
markdown
Copy code
S0 (000) S1 (001) S2 (010) S3 (011) S4 (100)
________________________________________________
● Transitions: On each clock pulse, the counter increments by 1.
● Outputs: The output is the binary representation of the current state.
Question:20
🧠 Formal Definition
An ε-NFA is a 5-tuple:
M = (Q, Σ, δ, q₀, F)
Where:
● Q = finite set of states
● Σ = input alphabet
● δ = transition function:
δ: Q × (Σ ∪
{}) P(Q)
set of states)
● q₀ ∈ Q = start state
● F ⊆ Q = set of accepting states
💡 States:
Let’s define the states:
● Q = {q0, q1, q2, q3}
● Σ = {a, b}
● Start state: q0
● Accepting states: F = {q1, q3}
🔁 Transition function δ:
Fro Inp T
m ut o
q
q0 ε
1
q
q0 ε
2
Fro Inp T
m ut o
q1 a —
q
q2 a
3
q3 b —
🧾 Explanation:
● From q0, the machine can:
o Jump to q1 with ε and accept a directly
o Or jump to q2 with ε, then read a, go to q3, then read b and accept ab
So this ε-NFA accepts both:
● "a" (via q0 q1 accept)
✅ Accepting Strings:
● Input a
:
⇒ accept ✔️
● Input ab
:
⇒ accept ✔️
● Input b: no path ⇒ reject ❌
● Input aa: no full path ⇒ reject ❌
Question:21
Construct a DFA with reduced states equivalent to the regular expression 10 + (0 + 11)0* 1
● "01" 0+ +1
● "001" 0+0+1
● "0001" 0 + 00 + 1
● "1101" 11 + 0 + 1
● "11001"
11 + 00 + 1
🔹 State Definitions:
Let’s name states informally first:
● q0: Start
● For "10":
o q0 1 q1 0 q_accept
● For (0 + 11) 0* 1:
o Case 0...1:
▪ q0 0 q2 0* q3 1 q_accept
o Case 11...1:
▪ q0 1 q4 1 q2 (same q2 as above) 0* q3 1
q_accept
💡 DFA Construction
Let’s define DFA states as sets of possibilities (subset construction):
We'll walk through all possibilities manually to build a DFA.
🧾 State List and Transitions:
Let’s define the following DFA states:
Sta
Description
te
A Start state
B Seen 1 (possible start of "10" or "11")
C Seen 10 ⇒ Accept (for "10")
D Seen 0 (start of (0+11)0*1)
E Seen 11 ⇒ move to 0* state
F In 0* (after 0 or 11)
Seen final 1 ⇒ Accept (from
G
(0+11)0*1)
Z Dead state (for invalid strings)
📋 Transition Table:
Curre Inp Ne
Notes
nt ut xt
A 0 D Start 0 path
A 1 B Start 1 path
B 0 C 10 ⇒ Accept
B 1 E Seen 11
C 0 Z Dead after 10
C 1 Z Dead after 10
D 0 F Start 0*
D 1 G Accept 01
E 0 F Start 0*
E 1 Z No valid transition from 111
F 0 F Looping in 0*
F 1 G Accepting final 1
Final accept; no further input
G 0 Z
allowed
Final accept; no further input
G 1 Z
allowed
Z 0 Z Dead state
Z 1 Z Dead state
✅ Accepting States:
● C (for string "10")
● G (for strings matching (0 + 11)0*1)
🏁 Final Answer:
✅ DFA with reduced states that accepts the regular expression:
10+(0+11)0∗110 + (0 + 11)0^*110+(0+11)0∗1
States:
A, B, C, D, E, F, G, Z
Alphabet: {0, 1}
Start state: A
Accepting states: {C, G}
Question:22
Draw the transition table, transition diagram, transition function of DFA a) Which accepts
strings which have odd number of a’s and b’s over the alphabet {a,b} b) Which accepts string
which have even number of a’s and b’s over the alphabet {a,b} c)Which accepts all strings
ending in 00 over alphabet {0, 1} d)Which accepts all strings having 3 consecutive zeros? e)
Which accepts all strings having 5 consecutive ones? f) Which accepts all strings having even
number of symbols.
Sol: Certainly! Let's design Deterministic Finite Automata (DFA) for each of the specified
languages over the given alphabets. For each case, I'll provide:
● Transition Table
● Transition Diagram
● Transition Function
a) DFA that accepts strings with an odd number of 'a's and 'b's over the
alphabet {a, b}
Transition Table:
Transition Diagram:
● δ(q0, a) = q1
● δ(q0, b) = q2
● δ(q1, a) = q0
● δ(q1, b) = q3
● δ(q2, a) = q3
● δ(q2, b) = q0
● δ(q3, a) = q2
● δ(q3, b) = q1
b) DFA that accepts strings with an even number of 'a's and 'b's over the
alphabet {a, b}
Transition Table:
Transition Diagram:
Transition Function:
● δ(q0, a) = q1
● δ(q0, b) = q2
● δ(q1, a) = q0
● δ(q1, b) = q3
● δ(q2, a) = q3
● δ(q2, b) = q0
● δ(q3, a) = q2
● δ(q3, b) = q1
c) DFA that accepts strings ending in "00" over the alphabet {0, 1}
Transition Table:
Transition Diagram:
q0 --0--> q1 --0--> q2
| | |
+--------+--------+
Transition Function:
● δ(q0, 0) = q1
● δ(q0, 1) = q0
● δ(q1, 0) = q2
● δ(q1, 1) = q0
● δ(q2, 0) = q2
● δ(q2, 1) = q0
d) DFA that accepts strings containing "000" (3 consecutive zeros) over the
alphabet {0, 1}
Transition Table:
Transition Function:
● δ(q0, 0) = q1
● δ(q0, 1) = q0
● δ(q1, 0) = q2
● δ(q1, 1) = q0
● δ(q2, 0) = q3
● δ(q2, 1) = q0
● δ(q3, 0) = q3
● δ(q3, 1) = q0
e) DFA that accepts strings containing "11111" (5 consecutive ones) over the
alphabet {0, 1}
Transition Table:
Transition Diagram:
Transition Function:
● δ(q0, 0) = q0
● δ(q0, 1) = q1
● δ(q1, 0) = q0
● δ(q1, 1) = q2
● δ(q2, 0) = q0
● δ(q2, 1) = q3
● δ(q3, 0) = q0
● δ(q3, 1) = q4
● δ(q4, 0) = q0
● δ(q4, 1) = q5
● δ(q5, 0) = q5
● δ(q5, 1) = q5
f) DFA that accepts strings with an even number of symbols (i.e., even length)
over the alphabet {0, 1}
Transition Table:
Transition Diagram:
q0 --0--> q1 --0--> q0
| | |
+--------+--------+
Transition Function:
● δ(q0, 0) = q1
● δ(q0, 1) = q1
● δ(q1, 0) = q
● δ(q1, 1) = q0
Question:23
Construct NFA which accepts the set of all strings over f0; 1g in which there are at least two
occurrences of 1 between any two occurrences of 0. Construct DFA for the same set.
Sol: We are asked to construct an NFA and then a DFA for the language:
L = { w ∈ {0,1} | there are at least two 1s between any two 0s }*
✅ Step 1: Understanding the Language
The strings must follow the rule:
Between any two 0s, there must be at least two 1s.
✔ Valid Examples:
● 1, 11, 0, 0110, 01110, 101110, 1101101
❌ Invalid Examples:
● 00 0 followed immediately by 0
● 010 only 1 one between 0s
● 01 only one 1 after 0, next is 0 or end (unless last 0)
🔹 States:
Let’s define the states:
Sta
Meaning
te
q0 Start state (no recent 0)
q1 Just saw a 0, need first 1
q2 Saw one 1 after 0, need second 1
Saw two 1s after 0, can now see another
q3
0
🔁 NFA Transitions:
Fro Inp T
Explanation
m ut o
q
q0 1 Stay on 1
0
q
q0 0 Saw a 0 must enforce 2 1s next
1
q
q1 1 First 1 after 0
2
q
q2 1 Second 1 after 0
3
q3 1 q Additional 1s
Fro Inp T
Explanation
m ut o
3
q
q3 0 Next 0 allowed re-enter 0 state
1
🟩 Start State: q0
🟦 Accepting States: {q0, q3}
● q0: Valid start and all-1 strings
● q3: After valid spacing between 0s
📘 Final Summary
✅ NFA:
● States: q0, q1, q2, q3
● Start: q0
● Accept: q0, q3
● Logic: Require 2 1s between 0s
✅ DFA:
● States: A, B, C, D
● Start: A
● Accept: A, D
● Transitions:
A --0--> B
A --1--> A
B --1--> C
C --1--> D
D --1--> D
D --0--> B
Question:24
Design a Moore machine to determine the residue mod 5 for each binary string treated as
integer.
🔢 Key Concepts:
● A Moore machine is a 6-tuple:
Where:
o Q: Set of states
o Σ: Input alphabet (here: {0,1})
o Δ: Output alphabet (here: {0,1,2,3,4})
o δ : Transition function Q×QQ \times \Sigma \rightarrow QQ×Q
o λ : Output function QQ \rightarrow \DeltaQ
o q₀: Initial state
💡 Idea:
We simulate the binary number being built left to right. After each bit, compute the residue
mod 5 of the number seen so far.
Let:
● q₀ = state where the value so far is 0 mod 5
● q₁ = 1 mod 5
● q₂ = 2 mod 5
● q₃ = 3 mod 5
● q₄ = 4 mod 5
On reading bit b, and in current state qᵢ, the new value is:
new value=(2×i+b)mod 5\text{new value} = (2 × i + b) \mod 5new value=(2×i+b)mod5
🔁 Transition Table:
Let’s construct the transition function (δ) and the output function (λ):
📘 δ: State transitions for each bit
Current Input Input
State '0' '1'
q₀ (0) q₀ q₁
q₁ (1) q₂ q₃
q₂ (2) q₄ q₀
q₃ (3) q₁ q₂
q₄ (4) q₃ q₄
📗 λ: Output at each state
Sta Outp
te ut
q₀ 0
q₁ 1
q₂ 2
q₃ 3
q₄ 4
🧪 Example:
Input: 1011 (which is 11 in decimal)
Let's simulate step-by-step:
● Start at q₀ (value = 0) Output: 0
📘 Formal Definition
An ε-NFA is a 5-tuple:
M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F)M=(Q,Σ,δ,q0,F)
Where:
● Q: A finite set of states
δ:Q×(Σ∪
{})P(Q)\delta: Q \times (\Sigma \cup \{\varepsilon\}) \rightarrow P(Q):Q×(
∪ {})P(Q)
Meaning: Given a state and a symbol (or ε), return a set of states
● q₀: Start state
🔁 Example
Let’s say we want to build an automaton for the regular expression:
L={a,ab}L = \{ a, ab \}L={a,ab}
An ε-NFA for this might look like:
rust
Copy code
q0 --ε--> q1 --a--> q3
\
ε--> q2 --a--> q4 --b--> q5
● Accepting states: q3 and q5
● Input "a" : q0 q1 a q3 ✅
● Input "ab" : q0 q2 a q4 b q5 ✅
📌 ε-Closure
To process input in an ε-NFA, we often compute the ε-closure of a state — the set of all
states reachable from that state using only ε-transitions.
This helps simulate an ε-NFA as an NFA or DFA.