如何绘制可识别语言L(n)={a^n b^n : n=0,1,2}的DFA状态图?
How to Build a DFA for L(n) = {aⁿbⁿ | n=0,1,2}
Alright, let's walk through constructing this DFA step by step. The target language only includes three types of strings: the empty string, ab, and aabb—so our automaton needs to strictly accept these and reject everything else (like ba, aaa, abb, etc.).
Step 1: Define State Meanings
First, we need states that track our progress through valid strings. Here's what each state represents:
- S0: Initial state (no characters read yet). This is an accepting state because the empty string is part of the language.
- S1: We've read exactly 1
aand nobs. - S2: We've read exactly 2
as and nobs. - S3: We've read 1
afollowed by 1b(this matches the stringab, so it's an accepting state). - S4: We've read 2
as followed by 1b. - S5: We've read 2
as followed by 2bs (this matchesaabb, so it's an accepting state). - D: Dead state (error state). Any string that enters this state is invalid, and we'll never leave this state no matter what input comes next.
Step 2: Define State Transitions
Now let's map out what happens for each state when we read an a or b:
- From S0:
- On input
a: Move to S1 (we've started a valid string with onea) - On input
b: Move to D (a string starting withbcan never be in our language)
- On input
- From S1:
- On input
a: Move to S2 (now we have twoas, which is allowed for n=2) - On input
b: Move to S3 (we've completed the valid stringab)
- On input
- From S2:
- On input
a: Move to D (threeas exceed our max n=2) - On input
b: Move to S4 (we've got twoas and oneb, halfway toaabb)
- On input
- From S3:
- On input
aorb: Move to D (adding any character toabmakes it invalid—we can't have moreas afterbs, and an extrabwould makeabbwhich isn't in the language)
- On input
- From S4:
- On input
a: Move to D (we can't have anaafter abin valid strings) - On input
b: Move to S5 (we've completed the valid stringaabb)
- On input
- From S5:
- On input
aorb: Move to D (adding any character toaabbmakes it invalid)
- On input
- From D:
- On input
aorb: Stay in D (once we hit an invalid string, we stay in the error state forever)
- On input
Step 3: Draw the State Transition Diagram
To visualize this:
- Draw a circle for each state. Use a double circle for accepting states (S0, S3, S5).
- Draw an arrow pointing to S0 to mark it as the initial state.
- Add arrows between states labeled with the input character (
aorb) based on the transitions above. - Include the dead state D to make invalid paths explicit (you can omit it if you want, but it makes the automaton clearer).
Step 4: Test the DFA with Example Strings
Let's verify a few cases to make sure it works:
- Empty string: Start and stay in S0 → Accepted
ab: S0 → S1 → S3 → Acceptedaabb: S0 → S1 → S2 → S4 → S5 → Acceptedba: S0 → D → Rejectedaaa: S0 → S1 → S2 → D → Rejectedabb: S0 → S1 → S3 → D → Rejected
内容的提问来源于stack exchange,提问作者JIsam
相关产品推荐
相关产品推荐

