对所有 1≤i,j≤n,0≤k≤n 迭代计算 Rij(k),表示一个正规表达式,从 i 到 j 有一条标记为 w 的路径,且路径上所有点除 i 和 j 外不大于 k
k=0 时
若 i=j,若不存在从 i 到 j 的弧,Rij(0)=∅,否则 Rij(0)=a1+a2+⋯+am
若 i=j,若不存在自环,Rij(0)=ϵ,否则 Rij(0)=ϵ+a1+a2+⋯+am
Rij(k)=Rij(k−1)+Rik(k−1)(Rkk(k−1))∗Rkj(k−1)
迭代计算得到 Rij(n)
将所有 R1j(n) 相加(j 为终态)
Example
\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] (q1) {$q_1$}; \node [state, accepting, right of = q1] (q2) {$q_2$}; \draw (q1) edge[loop above, above] node{1} (q1) (q1) edge[above] node{0} (q2) (q2) edge[loop above, above] node{0,1} (q2); \end{tikzpicture}\end{document}
状态消去法
思路:
扩展自动机的概念,允许转移弧是正规表达式
消去某一中间状态时,更新每一个前驱状态到每一个后继状态转移弧
对每一终态 q,消去除 q 和 q0 外的所有状态
若 q=q0,可得到以下自动机,对应的正规表达式可表示为 (R+SU∗T)∗SU∗
\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, accepting, right of = q0] (q) {$q$}; \draw (q0) edge[loop above, above] node{R} (q0) (q0) edge[above, bend left] node{S} (q) (q) edge[below, bend left] node{T} (q0) (q) edge[loop above, above] node{U} (q); \end{tikzpicture}\end{document}
若 q=q0 ,可得到以下自动机,对应的正规表达式可表示为 R∗
\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$}; \draw (q0) edge[loop above, above] node{R} (q0); \end{tikzpicture}\end{document}