You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为给定语言L = (a*b*) | (ab)*构造确定有限自动机(DFA)

构造正则语言 ( L = (a*b) | (ab)^ ) 的确定有限自动机(DFA)

Got it, let's work through building this DFA step by step. First, let's make sure we're clear on exactly what language we're trying to recognize:

( L = (a*b) | (ab)^ )
This language includes two sets of valid strings (plus the empty string, which falls into both):

  1. Any string made of zero or more as followed by zero or more bs (e.g., ε, a, aa, b, ab, aabbb)
  2. Any string made of the substring ab repeated zero or more times (e.g., ε, ab, abab, ababab)

Step 1: Define States

We need states that track our progress in matching either subset, plus a dead state for invalid inputs. Here's the breakdown:

  • ( q_0 ): Initial state, accepting state — corresponds to the empty string (ε), valid in both subsets.
  • ( q_{1a} ): Accepting state — corresponds to the single string a (could start either subset, so we split this from longer a sequences to handle ab correctly).
  • ( q_{1b} ): Accepting state — corresponds to strings with 2+ as (only valid in the first subset, since longer a runs can't be part of the (ab)^* pattern).
  • ( q_2 ): Accepting state — corresponds to strings with 1+ bs (only valid in the first subset).
  • ( q_3 ): Accepting state — corresponds to strings that are exactly (ab)^k where k ≥ 1 (valid in both subsets).
  • ( q_4 ): Non-accepting state — corresponds to strings like aba, ababa (i.e., (ab)^k a where k ≥1). These aren't valid on their own, but we can transition back to a valid state if we get a b next.
  • ( q_5 ): Accepting state — corresponds to strings like abb, aab, aaabbb (valid in the first subset, but not part of the (ab)^* pattern).
  • ( q_d ): Non-accepting dead state — corresponds to invalid strings (e.g., ba, abaa, abba). Once we enter this state, we never leave it.

Step 2: State Transition Table

This table defines which state we move to for each input character from every current state:

Current StateInput aInput b
( q_0 )( q_{1a} )( q_2 )
( q_{1a} )( q_{1b} )( q_3 )
( q_{1b} )( q_{1b} )( q_5 )
( q_2 )( q_d )( q_2 )
( q_3 )( q_4 )( q_5 )
( q_4 )( q_d )( q_3 )
( q_5 )( q_d )( q_5 )
( q_d )( q_d )( q_d )

Step 3: Key Validation Examples

Let's test some cases to confirm the DFA works as expected:

  • Empty string: Stays in ( q_0 ) → accepted (correct, since ε belongs to both subsets)
  • a: ( q_0 → q_{1a} ) → accepted (correct, part of ( a*b* ))
  • aa: ( q_0 → q_{1a} → q_{1b} ) → accepted (correct, part of ( a*b* ))
  • ab: ( q_0 → q_{1a} → q_3 ) → accepted (correct, part of both subsets)
  • abab: ( q_0 → q_{1a} → q_3 → q_4 → q_3 ) → accepted (correct, part of ( (ab)^* ))
  • abb: ( q_0 → q_{1a} → q_3 → q_5 ) → accepted (correct, part of ( a*b* ))
  • aba: ( q_0 → q_{1a} → q_3 → q_4 ) → rejected (correct, aba doesn't fit either subset)
  • ba: ( q_0 → q_2 → q_d ) → rejected (correct, ba violates the a^*b^* order and isn't part of (ab)^*)

内容的提问来源于stack exchange,提问作者Kuldeep Bera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 02:08:13