NFA到DFA的转化(保证能讲明白)

本文详细介绍了如何将非确定有限状态自动机(NFA)转换为确定有限状态自动机(DFA),通过具体实例展示了ε-closure操作和状态转移过程。首先计算每个状态的ε-closure,接着根据NFA的状态转移,逐步构造DFA的转换表。最终形成DFA状态转换图,用于理解NFA到DFA的转换原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何将下图的NFA转化为DFA呢?


图1

解答如下:

  1. 求出 ε _ c l o s u r e ( s ) ε\_closure(s) ε_closure(s)
    ε _ c l o s u r e ( s ) ε\_closure(s) ε_closure(s)表示由状态 s s s 经由条件 ε ε ε 可以到达的所有状态的集合

ε _ c l o s u r e ( 0 ) = { 0 , 1 , 2 , 4 , 7 } ε\_closure(0)=\{0,1,2,4,7\} ε_closure(0)={0,1,2,4,7}
ε _ c l o s u r e ( 1 ) = { 1 , 2 , 4 } ε\_closure(1)=\{1,2,4\} ε_closure(1)={1,2,4}
ε _ c l o s u r e ( 2 ) = { 2 } ε\_closure(2)=\{2\} ε_closure(2)={2}
ε _ c l o s u r e ( 3 ) = { 1 , 2 , 3 , 4 , 6 , 7 } ε\_closure(3)=\{1,2,3,4,6,7\} ε_closure(3)={1,2,3,4,6,7}
ε _ c l o s u r e ( 4 ) = { 4 } ε\_closure(4)=\{4\} ε_closure(4)={4}
ε _ c l o s u r e ( 5 ) = { 1 , 2 , 4 , 5 , 6 , 7 } ε\_closure(5)=\{1,2,4,5,6,7\} ε_closure(5)={1,2,4,5,6,7}
ε _ c l o s u r e ( 6 ) = { 1 , 2 , 4 , 6 , 7 } ε\_closure(6)=\{1,2,4,6,7\} ε_closure(6)={1,2,4,6,7}
ε _ c l o s u r e ( 7 ) = { 7 } ε\_closure(7)=\{7\} ε_closure(7)={7}
ε _ c l o s u r e ( 8 ) = { 8 } ε\_closure(8)=\{8\} ε_closure(8)={8}
ε _ c l o s u r e ( 9 ) = { 9 } ε\_closure(9)=\{9\} ε_closure(9)={9}

  1. 下面的A,B,C…表示一种状态。如A表示 { 0 , 1 , 2 , 4 , 7 } \{0,1,2,4,7\} {0,1,2,4,7},其实就是 ε _ c l o s u r e ( 0 ) ε\_closure(0) ε_closure(0)

为了读懂下面的内容,在这里先解释几个东西。

  • ε _ c l o s u r e ( m o v e ( A , a ) ) ε\_closure(move(A,a)) ε_closure(move(A,a)) 中的 m o v e ( A , a ) move(A,a) move(A,a)是什么意思呢?
    它代表从状态A,经过a到达的状态。
    A状态是 ε _ c l o s u r e ( 0 ) = { 0 , 1 , 2 , 4 , 7 } ε\_closure(0) = \{0,1,2,4,7\} ε_closure(0)={0,1,2,4,7},在 { 0 , 1 , 2 , 4 , 7 } \{0,1,2,4,7\} {0,1,2,4,7}里面,对每一个元素都考虑其是否通过a到达了某个状态,观察图1,只有状态2和7经过了a,分别到达了3和8,所以 m o v e ( A , a ) move(A,a) move(A,a) = { 3 , 8 } \{3,8\} {3,8},从而有 ε _ c l o s u r e ( m o v e ( A , a ) ) = ε _ c l o s u r e ( { 3 , 8 } ) = { 1 , 2 , 3 , 4 , 6 , 7 , 8 } ε\_closure(move(A,a)) = ε\_closure(\{3,8\}) = \{1,2,3,4,6,7,8\} ε_closure(move(A,a))=ε_closure({3,8})={1,2,3,4,6,7,8} { 1 , 2 , 3 , 4 , 6 , 7 , 8 } \{1,2,3,4,6,7,8\} {1,2,3,4,6,7,8}是未出现过的状态,所以可把它取名为B状态(其它名字也行).
  • ε _ c l o s u r e ( { 3 , 8 } ) = { 1 , 2 , 3 , 4 , 6 , 7 , 8 } ε\_closure(\{3,8\}) = \{1,2,3,4,6,7,8\} ε_closure({3,8})={1,2,3,4,6,7,8}是怎么来的呢?
    因为在步骤1我们已经知道, ε _ c l o s u r e ( 3 ) = { 1 , 2 , 3 , 4 , 6 , 7 } ε\_closure(3)=\{1,2,3,4,6,7\} ε_closure(3)={1,2,3,4,6,7} ε _ c l o s u r e ( 8 ) = { 8 } ε\_closure(8)=\{8\} ε_closure(8)={8} ,然后取它们的并集就得到了 { 1 , 2 , 3 , 4 , 6 , 7 , 8 } \{1,2,3,4,6,7,8\} {1,2,3,4,6,7,8}.

    这样应该解释明白了。继续往下看,然后不懂的话再倒回来看看。

ε _ c l o s u r e ( 0 ) = { 0 , 1 , 2 , 4 , 7 } = A ε\_closure(0) = \{0,1,2,4,7\} = A ε_closure(0)={0,1,2,4,7}=A
ε _ c l o s u r e ( m o v e ( A , a ) ) = ε _ c l o s u r e ( { 3 , 8 } ) = { 1 , 2 , 3 , 4 , 6 , 7 , 8 } = B ε\_closure(move(A,a)) = ε\_closure(\{3,8\}) = \{1,2,3,4,6,7,8\} = B ε_closure(move(A,a))=ε_closure({3,8})={1,2,3,4,6,7,8}=B
ε _ c l o s u r e ( m o v e ( A , b ) ) = ε _ c l o s u r e ( 5 ) = { 1 , 2 , 4 , 5 , 6 , 7 } = C ε\_closure(move(A,b)) = ε\_closure(5) = \{1,2,4,5,6,7\} = C ε_closure(move(A,b))=ε_closure(5)={1,2,4,5,6,7}=C
ε _ c l o s u r e ( m o v e ( B , a ) ) = ε _ c l o s u r e ( { 3 , 8 } ) = { 1 , 2 , 3 , 4 , 6 , 7 , 8 } = B ε\_closure(move(B,a)) = ε\_closure(\{3,8\}) = \{1,2,3,4,6,7,8\} = B ε_closure(move(B,a))=ε_closure({3,8})={1,2,3,4,6,7,8}=B
ε _ c l o s u r e ( m o v e ( B , b ) ) = ε _ c l o s u r e ( { 5 , 9 } ) = { 1 , 2 , 4 , 5 , 6 , 7 , 9 } = D ε\_closure(move(B,b)) = ε\_closure(\{5,9\}) =\{1,2,4,5,6,7,9\} = D ε_closure(move(B,b))=ε_closure({5,9})={1,2,4,5,6,7,9}=D
ε _ c l o s u r e ( m o v e ( C , a ) ) = ε _ c l o s u r e ( { 3 , 8 } ) = { 1 , 2 , 3 , 4 , 6 , 7 , 8 } = B ε\_closure(move(C,a)) = ε\_closure(\{3,8\}) = \{1,2,3,4,6,7,8\} = B ε_closure(move(C,a))=ε_closure({3,8})={1,2,3,4,6,7,8}=B
ε _ c l o s u r e ( m o v e ( C , b ) ) = ε _ c l o s u r e ( 5 ) = { 1 , 2 , 4 , 5 , 6 , 7 } = C ε\_closure(move(C,b)) = ε\_closure(5) = \{1,2,4,5,6,7\} = C ε_closure(move(C,b))=ε_closure(5)={1,2,4,5,6,7}=C
ε _ c l o s u r e ( m o v e ( D , a ) ) = ε _ c l o s u r e ( { 3 , 8 } ) = { 1 , 2 , 3 , 4 , 6 , 7 , 8 } = B ε\_closure(move(D,a)) = ε\_closure(\{3,8\}) = \{1,2,3,4,6,7,8\} = B ε_closure(move(D,a))=ε_closure({3,8})={1,2,3,4,6,7,8}=B
ε _ c l o s u r e ( m o v e ( D , b ) ) = ε _ c l o s u r e ( 5 ) = { 1 , 2 , 4 , 5 , 6 , 7 } = C ε\_closure(move(D,b)) = ε\_closure(5) = \{1,2,4,5,6,7\} = C ε_closure(move(D,b))=ε_closure(5)={1,2,4,5,6,7}=C

  1. 画出DFA状态转换表
    步骤2中有A,B,C,D四种情况,其中A是初始情况( ε _ c l o s u r e ( 0 ) = { 0 , 1 , 2 , 4 , 7 } ε\_closure(0)=\{0,1,2,4,7\} ε_closure(0)={0,1,2,4,7}. 多说几句,在NFA图1中,是从0出发的,所以初始情况是 ε _ c l o s u r e ( 0 ) ε\_closure(0) ε_closure(0),那假设如果是从X开始,则初始情况是 ε _ c l o s u r e ( X ) ε\_closure(X) ε_closure(X)

看步骤2, ε _ c l o s u r e ( m o v e ( A , a ) ) = ε _ c l o s u r e ( { 3 , 8 } ) = { 1 , 2 , 3 , 4 , 6 , 7 , 8 } = B ε\_closure(move(A,a)) = ε\_closure(\{3,8\}) = \{1,2,3,4,6,7,8\} = B ε_closure(move(A,a))=ε_closure({3,8})={1,2,3,4,6,7,8}=B,所以A行a列填B,其他同理。

ab
ABC
BBD
CBC
DBC
  1. 根据DFA状态转换表画出DFA状态转换图

看步骤3的表格,A经过a到B,A经过b到C,B经过a到B,以此类推…

参考:
NFA到DFA的转化🔗

NFA(非确定性有限自动机)转化为DFA(确定性有限自动机)是通过子集构造法实现的。以下是使用Python代码实现NFA转化为DFA的过程: ```python class NFA: def __init__(self, states, alphabet, transitions, start_state, final_states): self.states = states self.alphabet = alphabet self.transitions = transitions self.start_state = start_state self.final_states = final_states def epsilon_closure(self, states): closure = set(states) stack = list(states) while stack: current_state = stack.pop() if current_state in self.transitions and 'ε' in self.transitions[current_state]: next_states = self.transitions[current_state]['ε'] new_states = [state for state in next_states if state not in closure] closure.update(new_states) stack.extend(new_states) return closure def move(self, states, symbol): result = set() for state in states: if state in self.transitions and symbol in self.transitions[state]: result.update(self.transitions[state][symbol]) return result def convert_to_dfa(self): dfa_states = [] dfa_transitions = {} start_state = frozenset(self.epsilon_closure([self.start_state])) dfa_states.append(start_state) stack = [start_state] while stack: current_state = stack.pop() for symbol in self.alphabet: next_state = frozenset(self.epsilon_closure(self.move(current_state, symbol))) if next_state not in dfa_states: dfa_states.append(next_state) stack.append(next_state) if current_state not in dfa_transitions: dfa_transitions[current_state] = {} dfa_transitions[current_state][symbol] = next_state dfa_final_states = [state for state in dfa_states if any(final_state in state for final_state in self.final_states)] return DFA(dfa_states, self.alphabet, dfa_transitions, start_state, dfa_final_states) class DFA: def __init__(self, states, alphabet, transitions, start_state, final_states): self.states = states self.alphabet = alphabet self.transitions = transitions self.start_state = start_state self.final_states = final_states def accept(self, input_string): current_state = self.start_state for symbol in input_string: if symbol in self.transitions[current_state]: current_state = self.transitions[current_state][symbol] else: return False return current_state in self.final_states # 示例用法 nfa = NFA( states={'A', 'B', 'C', 'D'}, alphabet={'0', '1'}, transitions={ 'A': {'ε': {'B', 'C'}}, 'B': {'0': {'B'}, '1': {'B', 'D'}}, 'C': {'0': {'C', 'D'}, '1': {'C'}}, }, start_state='A', final_states={'D'} ) dfa = nfa.convert_to_dfa() print(dfa.accept('000')) # 输出 True print(dfa.accept('111')) # 输出 False ``` 上述代码实现了NFA转化为DFA的过程。首先定义了NFA类和DFA类,通过epsilon_closure()方法计算ε-closure,move()方法根据给定的symbol计算下一个状态,然后使用子集构造法将NFA转化为DFA,最后通过DFA类的accept()方法来判断给定的输入字符串是否被DFA接受。
评论 40
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫咪钓鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值