如何将有限自动机转换为正则表达式？

115

2

Grijesh Chauhan 2014年

94

状态删除方法

例

$\left(ab+\left(b+aa\right)\left(ba{\right)}^{\ast }\left(a+bb\right){\right)}^{\ast }$

算法

L[i,j]是从到的语言的正则表达式。首先，我们删除所有多边：${q}_{i}$$q_i$${q}_{j}$$q_j$

for i = 1 to n:
for j = 1 to n:
if i == j then:
L[i,j] := ε
else:
L[i,j] := ∅
for a in Σ:
if trans(i, a, j):
L[i,j] := L[i,j] + a


remove(k):
for i = 1 to n:
for j = 1 to n:
L[i,i] += L[i,k] . star(L[k,k]) . L[k,i]
L[j,j] += L[j,k] . star(L[k,k]) . L[k,j]
L[i,j] += L[i,k] . star(L[k,k]) . L[k,j]
L[j,i] += L[j,k] . star(L[k,k]) . L[k,i]


for i = 1 to n:
if not(final(i)) and not(initial(i)):
remove(i)


e := star(L[s,s]) . L[s,f] . star(L[f,s] . star(L[s,s]) . L[s,f] + L[f,f])


缺点

1

@Kabamaru：固定。但是现在我认为第三张图像中的也应该是，并且类似地在最终的正则表达式中也是如此。$\epsilon$$\varepsilon$ab
Wandering Logic

1

wvxvw

50

方法

$\qquad \displaystyle Q_i = \bigcup\limits_{q_i \overset{a}{\to} q_j} aQ_j \cup \begin{cases} \{\varepsilon\} &,\ q_i \in F \\ \emptyset &, \text{ else}\end{cases}$

$\phantom{\rule{2em}{0ex}}L=UL\cup V\phantom{\rule{1em}{0ex}}⟺\phantom{\rule{1em}{0ex}}L={U}^{\ast }V$$\qquad \displaystyle L = UL \cup V \quad \Longleftrightarrow \quad L = U^*V$

例

[ 来源 ]

$\phantom{\rule{2em}{0ex}}\begin{array}{rl}{Q}_{0}& =a{Q}_{1}\cup b{Q}_{2}\cup \epsilon \\ {Q}_{1}& =b{Q}_{0}\cup a{Q}_{2}\\ {Q}_{2}& =a{Q}_{0}\cup b{Q}_{1}\end{array}$\qquad \begin{align} Q_0 &= aQ_1 \cup bQ_2 \cup \varepsilon \\ Q_1 &= bQ_0 \cup aQ_2 \\ Q_2 &= aQ_0 \cup bQ_1 \end{align}

$\phantom{\rule{2em}{0ex}}\begin{array}{rl}{Q}_{1}& =b{Q}_{0}\cup a\left(a{Q}_{0}\cup b{Q}_{1}\right)\\ & =ab{Q}_{1}\cup \left(b\cup aa\right){Q}_{0}\\ & =\left(ab{\right)}^{\ast }\left(b\cup aa\right){Q}_{0}\end{array}$\qquad \begin{align} Q_1 &= bQ_0 \cup a(aQ_0 \cup bQ_1) \\ &= abQ_1 \cup (b \cup aa)Q_0 \\ &= (ab)^*(b \cup aa)Q_0 \end{align}

$\phantom{\rule{2em}{0ex}}\begin{array}{rl}{Q}_{0}& =a\left(ab{\right)}^{\ast }\left(b\cup aa\right){Q}_{0}\cup ba{Q}_{0}\cup bb\left(ab{\right)}^{\ast }\left(b\cup aa\right){Q}_{0}\cup \epsilon \\ & =\left(\left(a\cup bb\right)\left(ab{\right)}^{\ast }\left(b\cup aa\right)\cup ba\right){Q}_{0}\cup \epsilon \\ & =\left(\left(a\cup bb\right)\left(ab{\right)}^{\ast }\left(b\cup aa\right)\cup ba{\right)}^{\ast }\phantom{\rule{2em}{0ex}}\text{(by Arden's Lemma)}\end{array}$\qquad \begin{align} Q_0 &= a(ab)^*(b \cup aa)Q_0 \cup baQ_0 \cup bb(ab)^*(b \cup aa)Q_0 \cup \varepsilon \\ &= ((a \cup bb)(ab)^*(b \cup aa) \cup ba)Q_0 \cup \varepsilon \\ &= ((a \cup bb)(ab)^*(b \cup aa) \cup ba)^* \qquad \text{(by Arden's Lemma)} \end{align}

$\phantom{\rule{2em}{0ex}}\left(\left(a+bb\right)\left(ab{\right)}^{\ast }\left(b+aa\right)+ba{\right)}^{\ast }.$$\qquad \displaystyle ((a + bb)(ab)^*(b + aa) + ba)^*.$

1. 有关Arden引理的证明，请参见此处

1

jmite 2014年

@jmite：我不知道。我认为我不会尝试实现这一点（在这方面其他方法似乎更可行），但可以将其用作笔-纸方法。

1

wvxvw

Georgio3 '16

@PAOK检查的定义上述（行）; 这是因为是最终状态。${Q}_{i}$$Q_i$${q}_{0}$$q_0$

28

Brzozowski代数方法

算法的思想

Arden规则的巧妙解到语言方程我们可以将自动机视为以下形式的一组方程：$X={A}^{\ast }B$$X=A^*B$$X=AX\cup B$$X=AX∪B$

${X}_{i}={B}_{i}+{A}_{i,1}{X}_{1}+\dots +{A}_{i,n}{X}_{n}$

${X}_{n}={B}_{n}+{A}_{n,1}{X}_{1}+\dots +{A}_{n,n}{X}_{n}$

${X}_{n}={A}_{n,n}^{\ast }\left({B}_{n}+{A}_{n,1}{X}_{1}+\dots +{A}_{n,n-1}{X}_{n-1}\right)$

${X}_{n}={B}_{n}^{\prime }+{A}_{n,1}^{\prime }{X}_{1}+\dots +{A}_{n,n-1}^{\prime }{X}_{n-1}$

${B}_{i}^{\prime }={B}_{i}+{A}_{i,n}{B}_{n}^{\prime }$
${A}_{i,j}^{\prime }={A}_{i,j}+{A}_{i,n}{A}_{n,j}^{\prime }$

${X}_{1}={B}_{1}^{\prime }$

算法

for i = 1 to m:
if final(i):
B[i] := ε
else:
B[i] := ∅


for i = 1 to m:
for j = 1 to m:
for a in Σ:
if trans(i, a, j):
A[i,j] := a
else:
A[i,j] := ∅


for n = m decreasing to 1:
B[n] := star(A[n,n]) . B[n]
for j = 1 to n:
A[n,j] := star(A[n,n]) . A[n,j];
for i = 1 to n:
B[i] += A[i,n] . B[n]
for j = 1 to n:
A[i,j] += A[i,n] . A[n,j]


e := B[1]


4

Columbo

24

传递闭合法

关键思想

${R}_{i,j}^{\prime }={R}_{i,j}+{R}_{i,k}.{R}_{k,k}^{\ast }.{R}_{k,j}$

（为，为。）$R$$R$${R}^{k-1}$$R^{k-1}$${R}^{\prime }$$R'$${R}^{k}$$R^k$

例

${R}^{0}=\left[\begin{array}{ccc}\epsilon & a& b\\ b& \epsilon & a\\ a& b& \epsilon \end{array}\right]$

${R}^{1}=\left[\begin{array}{ccc}\epsilon & a& b\\ b& \epsilon +ba& a+bb\\ a& b+aa& \epsilon +ab\end{array}\right]$

算法

for i = 1 to n:
for j = 1 to n:
if i == j:
R[i,j,0] := ε
else:
R[i,j,0] := ∅
for a in Σ:
if trans(i, a, j):
R[i,j,0] := R[i,j,0] + a


for k = 1 to n:
for i = 1 to n:
for j = 1 to n:
R[i,j,k] := R[i,j,k-1] + R[i,k,k-1] . star(R[k,k,k-1]) . R(k,j,k-1)


e := ∅
for i = 1 to n:
if final(i):
e := e + R[s,i,n]