\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node[state, initial] (q0) {$q_0$}; \node[state, right of=q0] (q2) {$q_2$}; \node[state, accepting, right of=q2] (q1) {$q_1$}; \draw (q0) edge[loop above] node{1} (q0) (q0) edge[above] node{0} (q2) (q1) edge[loop above] node{0,1} (q1) (q2) edge[loop above] node{0} (q2) (q2) edge[above] node{1} (q1); \end{tikzpicture}\end{document}
转移表
0
1
q0
q2
q0
q1
q1
q1
q2
q2
q1
转移函数扩展到串
扩展转移函数δ^。
接收状态q和串w,返回状态p,p是q开始处理输入序列w后达到的状态。
DFA的语言
L(A)={w∣δ^(q0,w)属于F}
如果某个语言 L 是某个DFA A的 L(A),则可以证明 L 是一个正规语言。
Example
σ={0,1}∗ 上的语言 L={w∣w中0和1的数目的奇偶性相同} 是一个正规语言,可证 L 是以下 DFA 的语言。
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial, accepting] (q0) {$q_0$}; \node [state, right of=q0] (q1) {$q_1$}; \node [state, below of=q0] (q2) {$q_2$}; \node [state, accepting, right of=q2] (q3) {$q_3$}; \draw (q0) edge[bend left, above] node{1} (q1) (q0) edge[bend left, right] node{0} (q2) (q1) edge[bend left, below] node{1} (q0) (q1) edge[bend left, right] node{0} (q3) (q2) edge[bend left, left] node{0} (q0) (q2) edge[bend left, above] node{1} (q3) (q3) edge[bend left, below] node{1} (q2) (q3) edge[bend left, left] node{0} (q1); \end{tikzpicture}\end{document}
互归纳法:P0(n):δ′(q0,w)=q0 当且仅当 w 有偶数个 0 和偶数个 1,P1(n),P2(n),P3(n) 类似。
DFA 的设计
以下例题默认 Σ={0,1}∗。
例 1
Σ={0,1}∗,所有以 00 结尾的串。
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial] (q0) {$q_0$}; \node [state, right of=q0] (q1) {$q_1$}; \node [state, right of=q1] (q2) {$q_2$}; \draw (q0) edge[bend left, above] node{0} (q1) (q1) edge[bend left, below] node{1} (q0) (q1) edge[above] node{0} (q2) (q0) edge[loop above, above] node{1} (q0) (q2) edge[bend left, below] node{1} (q0) (q2) edge[loop above, above] node{0} (q2); \end{tikzpicture}\end{document}
例 2
L={x∈{0,1}∗∧x中至少包含一个子串010}
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial] (q0) {$q_0$}; \node [state, right of=q0] (q1) {$q_1$}; \node [state, right of=q1] (q2) {$q_2$}; \node [state, right of=q2, accepting] (q3) {$q_3$}; \draw (q0) edge[bend left, above] node{0} (q1) (q1) edge[above] node{1} (q2) (q2) edge[above] node{0} (q3) (q3) edge[loop above, above] node{0,1} (q3) (q0) edge[loop above, above] node{1} (q0) (q1) edge[loop above, above] node{0} (q1) (q2) edge[bend left, below] node{1} (q0); \end{tikzpicture}\end{document}
例 3
L={x∈{0,1}∗∧x中不包含子串010}
正难则反!可以从补集入手考虑
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial, accepting] (q0) {$q_0$}; \node [state, right of=q0, accepting] (q1) {$q_1$}; \node [state, right of=q1, accepting] (q2) {$q_2$}; \node [state, right of=q2] (q3) {$q_3$}; \draw (q0) edge[bend left, above] node{0} (q1) (q1) edge[above] node{1} (q2) (q2) edge[above] node{0} (q3) (q3) edge[loop above, above] node{0,1} (q3) (q0) edge[loop above, above] node{1} (q0) (q1) edge[loop above, below] node{0} (q1) (q2) edge[bend left, below] node{1} (q0); \end{tikzpicture}\end{document}
例 4
长度至少为 2 且头两个字符不同。
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial] (q0) {$q_0$}; \node [state, right of=q0, yshift=2cm] (q1) {$q_1$}; \node [state, right of=q0, yshift=-2cm] (q2) {$q_2$}; \node [state, right of=q1, yshift=-2cm, accepting] (q3) {$q_3$}; \node [state, right of=q0] (q4) {$q_4$}; \draw (q0) edge[left] node{0} (q1) (q0) edge[left] node{1} (q2) (q1) edge[left] node{0} (q4) (q2) edge[left] node{1} (q4) (q1) edge[right] node{1} (q3) (q2) edge[right] node{0} (q3) (q3) edge[loop right, right] node{0,1} (q3) (q4) edge[loop right, right] node{0,1} (q4); \end{tikzpicture}\end{document}
例 5
子串 01 出现的次数为奇数。
初始状态为子串 01 出现次数为偶数。
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial] (q0) {$q_0$}; \node [state, right of=q0] (q1) {$q_1$}; \node [state, right of=q1, accepting] (q2) {$q_2$}; \node [state, right of=q2, accepting] (q3) {$q_3$}; \draw (q0) edge[above] node{0} (q1) (q1) edge[above] node{1} (q2) (q2) edge[above] node{0} (q3) (q3) edge[bend left, below] node{1} (q0) (q0) edge[loop above, above] node{1} (q0) (q1) edge[loop above, above] node{0} (q1) (q2) edge[loop above, above] node{1} (q2) (q3) edge[loop above, above] node{0} (q3); \end{tikzpicture}\end{document}
例 6
包含相同个数的 0 和 1 且每个前缀的 0 和 1 个数之差不超过 1
初始状态 0 和 1 个数相等。
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial, accepting] (q0) {$q_0$}; \node [state, right of=q0, yshift=2cm] (q1) {$q_1$}; \node [state, right of=q0, yshift=-2cm] (q2) {$q_2$}; \node [state, right of=q1, yshift=-2cm] (q3) {$q_3$}; \draw (q0) edge[bend left, left] node{0} (q1) (q0) edge[bend left, right] node{1} (q2) (q1) edge[bend left, right] node{1} (q0) (q2) edge[bend left, left] node{0} (q0) (q1) edge[right, bend left] node{0} (q3) (q2) edge[right, bend right] node{1} (q3) (q3) edge[loop right, right] node{0,1} (q3); \end{tikzpicture}\end{document}
例 7
包含子串 01 但不包含子串 11
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial] (q0) {$q_0$}; \node [state, right of=q0, yshift=1cm] (q1) {$q_1$}; \node [state, right of=q0, yshift=-1cm] (q2) {$q_2$}; \node [state, right of=q1, accepting] (q3) {$q_3$}; \node [state, right of=q2] (q4) {$q_4$}; \node [state, right of=q3, accepting] (q5) {$q_5$}; \draw (q0) edge[left] node{0} (q1) (q0) edge[left] node{1} (q2) (q1) edge[above] node{1} (q3) (q1) edge[loop above, above] node{0} (q1) (q2) edge[left] node{0} (q1) (q2) edge[above] node{1} (q4) (q4) edge[loop right, right] node{0,1} (q4) (q3) edge[bend left, above] node{0} (q5) (q5) edge[bend left, below] node{1} (q3) (q3) edge[right] node{1} (q4) (q5) edge[loop right, right] node{0} (q5); \end{tikzpicture}\end{document}
注意不要漏掉 q5 的状态
例 8
所有能被 3 整除的二进制数
设 3 个节点分别表示当前的串模 3 余 0,1,2.
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial] (s) {$s$}; \node [state, accepting, right of=s] (q0) {$q_0$}; \node [state, below of=q0] (q1) {$q_1$}; \node [state, below of=s] (q2) {$q_2$}; \draw (s) edge[above] node{0} (q0) (s) edge[above] node{1} (q1) (q0) edge[bend left, right] node{1} (q1) (q0) edge[loop above, above] node{0} (q0) (q1) edge[left, bend left] node{1} (q0) (q1) edge[below, bend left] node{0} (q2) (q2) edge[above, bend left] node{0} (q1) (q2) edge[loop left, left] node{1} (q2); \end{tikzpicture}\end{document}
非确定型有穷自动机
定义
有穷状态集合 Q
有穷的输入符号集合Σ
转移函数 δ。以一个状态和一个输入符号作为变量,返回一个状态集合(可以为空)。
初始状态。
接受状态的集合F。F为Q的子集。
A=(Q,Σ,δ,q0,F)
NFA 与 DFA 的区别
DFA 中的 δ 的返回值是恰好的一个状态,而 NFA 中的 δ 是一个状态集合。
扩展转移函数
扩展转移函数 δ^ 接收状态 q 和输入符号串 w,返回一个状态集合。
NFA 的语言
设一个 NFA A=(Q,Σ,δ,q0,F) ,定义 A 的语言为
L(A)={w∣w∈Σ∗∧δ′(q0,w)∩F=∅}
设 L 是 Σ 上的语言,若存在 NFA A 满足 L=L(A),则可以证明 L 是一个正规语言。
NFA 的设计
以下默认 Σ={0,1}
例 1
所有长度至少为 2 且头两个字符不同的串
NFA 不要求每个状态对每个不同的输入都有一个对应的状态。
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial] (q0) {$q_0$}; \node [state, right of=q0, yshift=2cm] (q1) {$q_1$}; \node [state, right of=q0, yshift=-2cm] (q2) {$q_2$}; \node [state, right of=q1, yshift=-2cm, accepting] (q3) {$q_3$}; \draw (q0) edge[left] node{0} (q1) (q0) edge[left] node{1} (q2) (q1) edge[right] node{1} (q3) (q3) edge[loop right, right] node{0,1} (q3) (q2) edge[right] node{0} (q3); \end{tikzpicture}\end{document}
例 2
所有长度至少为 3 且第二和第三个字符不同的串
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial] (q0) {$q_0$}; \node [state, right of = q0] (q1) {$q_1$}; \node [state, right of = q1, yshift = 2cm] (q2) {$q_2$}; \node [state, right of = q1, yshift = -2cm] (q3) {$q_3$}; \node [state, right of = q2, yshift = -2cm, accepting] (q4) {$q_4$}; \draw (q0) edge[above] node{0,1} (q1) (q1) edge[left] node{0} (q2) (q1) edge[left] node{1} (q3) (q2) edge[right] node{1} (q4) (q3) edge[right] node{0} (q4) (q4) edge[loop right, right] node{0,1} (q4); \end{tikzpicture}\end{document}
例 3
长度至少为 2 且后两个字符不同且不含 2 的 0、1 和 2 组成的串
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial] (q0) {$q_0$}; \node [state, right of = q0, yshift = 2cm] (q1) {$q_1$}; \node [state, right of = q0, yshift = -2cm] (q2) {$q_2$}; \node [state, right of = q1, yshift = -2cm, accepting] (q3) {$q_3$}; \draw (q0) edge[loop above, above] node{0,1,2} (q0) (q0) edge[left] node{0} (q1) (q0) edge[left] node{1} (q2) (q1) edge[right] node{1} (q3) (q2) edge[right] node{0} (q3); \end{tikzpicture}\end{document}
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial] (q0) {$q_0$}; \node [state, right of = q0] (q1) {$q_1$}; \node [state, right of = q1] (q2) {$q_2$}; \node [state, right of = q2, accepting] (q3) {$q_3$}; \draw (q0) edge[above] node{$\epsilon$,0,1} (q1) (q1) edge[above] node{$\epsilon$,0,1} (q2) (q2) edge[above] node{0} (q3) (q3) edge[loop above] node{0,1} (); \end{tikzpicture}\end{document}
例 2
长度至少为 1,后三个符号里至少有一个不是 2 的 0、1 和 2 组成的串
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial] (q0) {$q_0$}; \node [state, right of = q0] (q1) {$q_1$}; \node [state, right of = q1] (q2) {$q_2$}; \node [state, right of = q2, accepting] (q3) {$q_3$}; \draw (q0) edge[loop above, above] node{0,1,2} () (q0) edge[above] node{0,1} (q1) (q1) edge[above] node{$\epsilon$,0,1,2} (q2) (q2) edge[above] node{$\epsilon$,0,1,2} (q3); \end{tikzpicture}\end{document}
即后三个符号中至少有一个是 0 或 1
例 3
长度至少为 2 且第 2 到第 4 个符号中至少有一个不是 2 的 0、1、2 组成的串
\usetikzlibrary{automata, positioning, arrows}\tikzset{ ->, % makes the edges directed >=stealth, % makes the arrow heads bold node distance=2.5cm, % specifies the minimum distance between two nodes. Change if necessary. every state/.style={thick}, % sets the properties for each ’state’ node initial text=$Start$, % sets the text that appears on the start arrow}\begin{document} \begin{tikzpicture} \node [state, initial] (q0) {$q_0$}; \node [state, right of = q0] (q1) {$q_1$}; \node [state, right of = q1] (q2) {$q_2$}; \node [state, right of = q2] (q3) {$q_3$}; \node [state, right of = q3, accepting] (q4) {$q_4$}; \draw (q0) edge[above] node{0,1,2} (q1) (q1) edge[above] node{$\epsilon$,0,1,2} (q2) (q2) edge[above] node{$\epsilon$,0,1,2} (q3) (q3) edge[above] node{0} (q4) (q4) edge[loop above, above] node{0,1,2} (q4); \end{tikzpicture}\end{document}