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

语言(111+11111)*的NFA转DFA分步转换请求

Converting NFA for (111+11111)* to DFA: Step-by-Step Guide

Got it, let's walk through converting the NFA for (111+11111)* to a DFA step by step using the subset construction method—this is the standard approach for NFA-to-DFA conversion, so I'll break it down clearly.

First, let's align on the NFA structure for this regex (it matches strings made of any number of 3-1 sequences or 5-1 sequences, including the empty string):

  • States: Q = {0, 1, 2, 3, 4, 5, 6, 7, 8}
  • Start state: 0 (also an accept state, since the empty string is allowed via *)
  • Accept states: F = {0}
  • Transitions:
    • ε-transitions: δ(0, ε) = {1, 4} (start can branch into either the 3-1 or 5-1 path)
    • 1-transitions:
      • δ(1,1)={2}, δ(2,1)={3}, δ(3,1)={0} (completes a 3-1 sequence and loops back to start)
      • δ(4,1)={5}, δ(5,1)={6}, δ(6,1)={7}, δ(7,1)={8}, δ(8,1)={0} (completes a 5-1 sequence and loops back to start)
    • All other transitions are empty sets.

We'll use the subset construction method: each DFA state represents a set of NFA states reachable via ε-transitions (called an ε-closure).


Step 1: Initialize the DFA start state

The DFA start state is the ε-closure of the NFA's start state (0):

  • ε-closure(0) = {0} ∪ δ(0, ε) = {0, 1, 4}
  • Name this DFA state S0 (accept state, since it includes NFA accept state 0)

Step 2: Compute transitions for S0 on input 1

First, collect all NFA states reachable from S0 via input 1:

  • δ(0,1)=∅, δ(1,1)={2}, δ(4,1)={5} → union is {2,5}
  • Compute ε-closure({2,5}) = {2,5} (no ε-transitions from these states)
  • Name this state S1 (not an accept state, no NFA accept state included)

Step 3: Compute transitions for S1 on input 1

  • δ(2,1)={3}, δ(5,1)={6} → union is {3,6}
  • ε-closure({3,6}) = {3,6}
  • Name this state S2 (not an accept state)

Step 4: Compute transitions for S2 on input 1

  • δ(3,1)={0}, δ(6,1)={7} → union is {0,7}
  • ε-closure({0,7}) = {0,1,4,7} (add ε-transitions from 0: {1,4})
  • Name this state S3 (accept state, includes 0)

Step 5: Compute transitions for S3 on input 1

  • δ(0,1)=∅, δ(1,1)={2}, δ(4,1)={5}, δ(7,1)={8} → union is {2,5,8}
  • ε-closure({2,5,8}) = {2,5,8}
  • Name this state S4 (not an accept state)

Step 6: Compute transitions for S4 on input 1

  • δ(2,1)={3}, δ(5,1)={6}, δ(8,1)={0} → union is {3,6,0}
  • ε-closure({3,6,0}) = {0,1,3,4,6} (add ε-transitions from 0: {1,4})
  • Name this state S5 (accept state)

Step 7: Compute transitions for S5 on input 1

  • δ(0,1)=∅, δ(1,1)={2}, δ(3,1)={0}, δ(4,1)={5}, δ(6,1)={7} → union is {2,0,5,7}
  • ε-closure({2,0,5,7}) = {0,1,2,4,5,7} (add ε-transitions from 0: {1,4})
  • Name this state S6 (accept state)

Step 8: Compute transitions for S6 on input 1

  • δ(0,1)=∅, δ(1,1)={2}, δ(2,1)={3}, δ(4,1)={5}, δ(5,1)={6}, δ(7,1)={8} → union is {2,3,5,6,8}
  • ε-closure({2,3,5,6,8}) = {2,3,5,6,8}
  • Name this state S7 (not an accept state)

Step 9: Compute transitions for S7 on input 1

  • δ(2,1)={3}, δ(3,1)={0}, δ(5,1)={6}, δ(6,1)={7}, δ(8,1)={0} → union is {3,0,6,7}
  • ε-closure({3,0,6,7}) = {0,1,3,4,6,7} (add ε-transitions from 0: {1,4})
  • Name this state S8 (accept state)

Step 10: Compute transitions for S8 on input 1

  • δ(0,1)=∅, δ(1,1)={2}, δ(3,1)={0}, δ(4,1)={5}, δ(6,1)={7}, δ(7,1)={8} → union is {2,0,5,7,8}
  • ε-closure({2,0,5,7,8}) = {0,1,2,4,5,7,8} (add ε-transitions from 0: {1,4})
  • Name this state S9 (accept state)

Step 11: Compute transitions for S9 on input 1

  • δ(0,1)=∅, δ(1,1)={2}, δ(2,1)={3}, δ(4,1)={5}, δ(5,1)={6}, δ(7,1)={8}, δ(8,1)={0} → union is {2,3,5,6,8,0}
  • ε-closure({2,3,5,6,8,0}) = {0,1,2,3,4,5,6,8} (add ε-transitions from 0: {1,4})
  • Name this state S10 (accept state)

Step 12: Compute transitions for S10 on input 1

  • δ(0,1)=∅, δ(1,1)={2}, δ(2,1)={3}, δ(3,1)={0}, δ(4,1)={5}, δ(5,1)={6}, δ(6,1)={7}, δ(8,1)={0} → union is {2,3,0,5,6,7}
  • ε-closure({2,3,0,5,6,7}) = {0,1,2,3,4,5,6,7} (add ε-transitions from 0: {1,4})
  • Name this state S11 (accept state)

Step 13: Check for new states

Compute transitions for S11 on input 1:

  • δ(0,1)=∅, δ(1,1)={2}, δ(2,1)={3}, δ(3,1)={0}, δ(4,1)={5}, δ(5,1)={6}, δ(6,1)={7}, δ(7,1)={8} → union is {2,3,0,5,6,8}
  • ε-closure of this set is {0,1,2,3,4,5,6,8} → which is S10 (already exists)

No new states are generated, so we're done with subset construction.


Final DFA Summary

DFA States & Acceptance

DFA StateNFA State SubsetAccept State?
S0{0, 1, 4}Yes
S1{2, 5}No
S2{3, 6}No
S3{0, 1, 4, 7}Yes
S4{2, 5, 8}No
S5{0, 1, 3, 4, 6}Yes
S6{0, 1, 2, 4, 5, 7}Yes
S7{2, 3, 5, 6, 8}No
S8{0, 1, 3, 4, 6, 7}Yes
S9{0, 1, 2, 4, 5, 7, 8}Yes
S10{0, 1, 2, 3, 4, 5, 6, 8}Yes
S11{0, 1, 2, 3, 4, 5, 6, 7}Yes

DFA Transition Function (Input: 1)

  • S0 → S1
  • S1 → S2
  • S2 → S3
  • S3 → S4
  • S4 → S5
  • S5 → S6
  • S6 → S7
  • S7 → S8
  • S8 → S9
  • S9 → S10
  • S10 → S11
  • S11 → S10

You can verify this DFA works for test cases:

  • Empty string: stays in S0 (accept)
  • "111": S0→S1→S2→S3 (accept)
  • "11111": S0→S1→S2→S3→S4→S5 (accept)
  • "111111" (3+3): S0→S1→S2→S3→S1→S2→S3 (accept)

内容的提问来源于stack exchange,提问作者Atul Kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:19:17