- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
例子
a abb a*b+
例子(续1)
输入aaba的执行过程
对应的DFA
模式 none a a*b+ none a*b+ abb 状态 A {0,1,3,7} B {2,4,7} C {8} D {7} E {5,8} F {6,8} a B D --D ----b C E C C F C
最小化一个DFA的状态数
不确定的有穷自动机(NFA)
NFA(Non-Deterministic Finite Automata)是一个五元组, 它包括: 有穷状态集合S 输入符号集合 转换函数move • 一个开始状态 s0S 状态集 字母表 转移函数 开始状态(初态)
• move(state, symbol) set of states
ε-closure(s) : s S
ε-closure of T : T S move(T,a)
状态s的ε闭包
状态集T的ε闭包
从状态s出发,仅通过ε边就能达到的状态集合
从状态集T中任一状态出发,仅通过ε边就能达到的状态集
: T S, a 转移函数
对于状态集T,在输入a时,可以到达的状态集(不考虑ε边)
DFA最小化(续)
思考题二
将(a|b)*abb(a|b)*转换成NFA,并将所得NFA确定 化,然后最小化所得DFA。
P92 练习3.5.1
P109 练习3.8.2
最小化一个DFA的状态数
最小化一个DFA的状态数
工作原理:将状态集分划成多个组,每个组中的各个状态相 互不可区分。然后,将每个组中的状态合并成一个状态。 1. Start with an initial partition Π with two groups, F and S - F. 2. Construct a new partition as follows: initially, let Πnew = Π; for ( each group G of Π ) { partition G into subgroups so that two states s and t are in the same subgroup if and only if for all input symbols a, states s and t have transitions on a to states in the same group of Π; replace G in Πnew by the set of all subgroups formed; }
由NFA构造DFA的子集构造算法
initially, ε-closure(s0) is the only state in Dstates, it is unmarked while ( there is an unmarked state T in Dstates ) { mark T; for ( each input symbol a ) { U = ε-closure(move(T,a)); if ( U is not in Dstates ) add U as an unmarked state to Dstates; Dtran[T, a] = U; } }
ε-closure(T)的计算(例)
ε-closure({0}):经过0条ε弧{0} 条ε弧{2,6,5} 经过3条ε弧{7}
经过1条ε弧{1,4} 经过4条ε弧Ø
经过2
ε-closure({0})={0,1,4,2,6,5,7}
move(ε-closure({0}),a)=move({0,1,4,2,6,5,7},a)={3,6} ε-closure(move(ε-closure({0}),a))= ε-closure({3,6})={3,6,7}
s = s0; c = nextchar( ); while ( c != eof ) { s = move(s, c); c = nextchar( ); } if ( s is in F ) return "yes"; else return "no";
接受(a|b)*abb的DFA
NFA状态集上的操作
NFA状态集上的操作(例)
ε-closure(1)={1,2,4}
ε-closure({1,2,3})={1,2,3,4,6}
move({2,4},a)={3}
ε-closure(T)的计算
push all states of T onto stack; initialize ε-closure(T) to T; while ( stack is not empty ) { pop t, the top element, off stack; for ( each state u with an edge from t to u labeled ε ) if ( u is not in ε-closure(T) ) { add u to ε-closure(T); push u onto stack } }
最小化一个DFA的状态数
3. If Πnew=Π, let Πfinal = Π and continue with step (4). Otherwise, repeat step (2) with Πnew in place of Π. 4. Construct the minimum-state DFA D' as follows: (a) Choose one state in each group of Πfinal as the representative. The representatives will be the states of D'. (b) The accepting states of D' are the representatives of those groups that contain an accepting state of D. (c) Let s be the representative of some group G of Πfinal, and let the transition of D from s on input a be to state t. Let r be the representative of t's group H. Then in D', there is a transition from s to r on input a.
自动机的等价:对于同一个语言,可以存在多个识 别此语言的DFA,这些DFA等价。
同构:如果仅需变换状态的名字就可以将一个DFA变成 另一个DFA,则这两个DFA同构。
两个状态的等价
同时是或不是接受状态 对任意的输入,总是转向同一状态或等价状态
任何正则语言都有唯一的状态数最少的DFA 从任意一个接受相同语言的DFA,通过合并等价状 态,可以得到状态数最少的DFA。
• 一个接受状态集合FS 终止状态集(终态)
NFA的两种表示
状态转换图:包括状态(圆圈)、弧、初态、终态 状态转换表:描述各个状态下,对于各个输入的状
态迁移 a a
0
1
b
2
b
3
a b ε
b
0 1 2 3
{0,1} Ø Ø Ø
{0} {2} {3} Ø
Ø Ø Ø Ø
自动机中输入字符串的接受
一个NFA接受(accept)输入字符串x, 当且仅当 状态转换图中存在一条从开始状态到某个接受 状态的路径,使得该路径上各条边的符号组成 符号串x。
由正则表达式构建NFA
正则式ε对应 NFA
正则式a, 对应NFA
由正则表达式构建NFA(II)
若s,t是正则式, N(s)和N(t)是对应的NFA,则s|t对 应之NFA为:
由正则表达式构建NFA(III)
若s, t为正则式,N(s)和N(t)是对应的NFA,则st对
应之NFA为:
例:
将a(a|b)*a转换成NFA,并将所得NFA确定化,然 后最小化所得DFA。
a(a|b)*a对应的NFA
将NFA转换成DFA
DFA状态 A B C D NFA状态 ε_closure(0)={0} ε_closure(1)={1,2,3,5,8} ε_closure({4,9})={2,3,4,5,7,8,9} ε_closure(6)={2,3,5,6,7,8} a B C C C b --D D D
DFA最小化
解:首先将DFA中状态划为终态组g1={C}和非终态组 g2={A,B,D}。 (1)g1中只有一个元素,无需继续划分。 g2中A没有定义输入b时的状态转换,而B,D在输入b时均转 向g2,故将g2继续划分成g3={A}, g4={B,D}。 (2)g3只有一个元素,无需继续划分。 g4中的状态在输入为a时,均转向g1,在输入为b时,均转 向g4。故无需划分。 本阶段状态分组无变化,故已经最小化,所得状态组为: g1={C}, g3={A}, g4={B,D}。合并状态B和D,得最小化后的 DFA如下:
由一个NFA定义(接受)的语言是其接受的 所有符号串的集合。
自动机中输入字符串的接受(例)
确定的有穷自动机(DFA)
NFA的一个特例
没有输入ε之上的转换动作 对每个状态s和每个输入符号a,有且仅有一条 标号为a的边离开。
DFA的运行
INPUT: An input string x terminated by an end-of-file character eof. A DFA D with start state s0, accepting states F, and transition function move. OUTPUT: Answer ''yes" if D accepts x; "no" otherwise.