确定性有穷自动机

定义

  1. 有穷状态集合
  2. 有穷的输入符号集合
  3. 转移函数 。以一个状态和一个输入符号作为变量,返回一个状态。
  4. 初始状态
  5. 接受状态的集合的子集。

DFA处理串

输入序列,从出事状态开始,逐个处理每个输入符号,得到状态,如果属于则接受输入,否则拒绝。

转移图

例:接受所有带子串01的串的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] (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}

转移表

01

转移函数扩展到串

扩展转移函数。 接收状态和串,返回状态开始处理输入序列后达到的状态。

DFA的语言

如果某个语言 是某个DFA A的 ,则可以证明 是一个正规语言。

Example

上的语言 是一个正规语言,可证 是以下 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}

互归纳法: 当且仅当 有偶数个 0 和偶数个 1, 类似。

DFA 的设计

以下例题默认

例 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


\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


正难则反!可以从补集入手考虑

\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}

注意不要漏掉 的状态

例 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}

非确定型有穷自动机

定义

  1. 有穷状态集合
  2. 有穷的输入符号集合
  3. 转移函数 。以一个状态和一个输入符号作为变量,返回一个状态集合(可以为空)。
  4. 初始状态。
  5. 接受状态的集合的子集。

NFA 与 DFA 的区别

DFA 中的 的返回值是恰好的一个状态,而 NFA 中的 是一个状态集合。

扩展转移函数

扩展转移函数 接收状态 和输入符号串 ,返回一个状态集合。

NFA 的语言

设一个 NFA ,定义 A 的语言为

上的语言,若存在 NFA A 满足 ,则可以证明 L 是一个正规语言。

NFA 的设计

以下默认

例 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}

DFA 与 NFA 对比

DFANFA
分析歧义
设计难度

DFA 和 NFA 的等价性

Theorem

是一个 DFA 的语言 是一个 NFA 的语言

Proof

从 DFA 构造等价的 NFA,若 则令 ,易证

从 NFA 构造等价的 DFA(子集构造法) 定义 ,其中

  • 易证

例 1

01
转换后可能有多个状态不可达,可以删去
01

例 2

01
01

最坏情况下,由 NFA 转化为 DFA 的状态数为指数级增长。

转移的 NFA

定义

五元组 ,转移函数 可以接受 作为输入。

-闭包

状态 -闭包记为 ,表示从 开始经所有的 路径可以到达的状态(包括 自身)的集合。

扩展转移函数

  • ,其中 ,设 ,则

的设计

例 1

长度至少为 1,前三个符号中至少有一个 0


\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}

例 4

长度至少为 4 且前五位至少有一个子串 102,后五位至少有一个子串 021 的 0、1、2 组成的串


需考虑全部情况

  • 102 出现在 021 前且没有重叠
  • 102 出现在 021 前且重叠:1021
  • 102 出现在 021 后且整个串长度为 5!02102

例 5

长度至少为 3 且每个连续的 3 位必为 012、120 或 201


容易漏掉长度至少为 3 的条件

DFA 和 的等价性

Theorem

是一个 DFA 的语言 是一个 的语言

Proof

从 DFA 构造等价的 :容易构造 构造等价的 DFA(修改的子集构造法) 定义

  • ,定义

DFA 的最小化

设 DFA ,定义 上的一个二元关系

可验证该关系为二元关系。 若

  • 可区别 可区别
  • 不可区别 不可区别
  • 可由 区别 可由 区别

计算状态集划分:填表法

递归标记可区别的状态偶对。

  • 基础:若 为终态, 为非终态,标记 可区别
  • 归纳:若 可区别,,则标记 可区别 通过合并等价状态优化 DFA:
  • 删去所有从初始状态不可达的节点
  • 使用填表算法
  • 仅保留可区别的点,将所有不可区别的点合并

例 1

有穷自动机 2025-11-03 16.15.31有穷自动机 2025-11-03 16.15.31
有穷自动机 2025-11-03 16.26.36有穷自动机 2025-11-03 16.26.36