wiki 伪代码看上去一直以为怪。发现葡萄牙语和俄罗斯语那里的 if 推断都还缺少一个条件。



国内的资料比較少。这几份学习资料不错。比我稀里糊涂的思路要好,分享下:

http://www.liafa.univ-paris-diderot.fr/~carton/Enseignement/Complexite/

ENS/Redaction/2008-2009/yingjie.xu.pdf

http://www8.cs.umu.se/kurser/TDBC92/VT06/final/1.pdf

http://arxiv.org/pdf/1010.5318.pdf





对于一个确定型自己主动机 D = (Q, Σ, δ, q0, F)。Q 的一系列恒等关系 ρi (i ≥ 0) 被定义为:

ρ0 = {(p, q)|p, q ∈ F} ∪ {(p, q)|p, q ∈ Q − F},

ρi+1 = {(p, q) ∈ ρi|(∀a ∈ Σ)(δ(p, a), δ(q, a)) ∈ ρi}.



ρi有例如以下关系:

ρ0 ⊇ ρ1 ⊇ · · · .

若 ρi = ρi+1 则对于 ρi = ρj (j > i).

存在 0 ≤ k ≤ |Q| 满足 ρk = ρk+1.



对于 ρi ≠ ρi+1,存在下面性质Equation 1

ρi ≠ ρi+1    ⇔ (∃p, q ∈ Q, a ∈ Σ) (p, q) ∈ ρi and (δ(p, a), δ(q, a)) ∉ ρi

⇔ (∃U ∈ Q/ρi , a ∈ Σ) p, q ∈ U and (δ(p, a), δ(q, a)) ∉ ρi

⇔ (∃U, V ∈ Q/ρi , a ∈ Σ) p, q ∈ U and δ(p, a) ∈ V and δ(q, a) ∉ V

⇔ (∃U, V ∈ Q/ρi , a ∈ Σ) δ(U, a) ∩ V ≠ ∅ and δ(U, a) ∉ V

算法抽象:

1: Q/θ ← {F, Q − F}

2: while (∃U, V ∈ Q/θ, a ∈ Σ) s.t. Equation 1 holds do

3: Q/θ ← (Q/θ − {U}) ∪ {U ∩ δ^-1(V, a), U − U ∩ δ^-1(V, a)}

4: end while



算法细化:

1:W ← {F, Q − F}    # 有些版本号上仅仅是 W ← {F }

2: P ← {F, Q − F}

3: while W is not empty do

4:     select and remove S from W

5:     for all a ∈ Σ do

6:         la ← δ^-1(S, a)

7:         for all R in P such that R ∩ la ≠ ∅ and R ∉ la do

8:             partition R into R1 and R2: R1 ← R ∩ la and R2 ← R − R1

9:             replace R in P with R1 and R2

10:           if R ∈ W then

11:               replace R in W with R1 and R2

12:           else

13:                 if |R1| ≤ |R2| then

14:                     add R1 to W

15:                 else

16:                     add R2 to W

17:                 end if

18:             end if

19:         end for

20:     end for

21: end while



复杂度:

O(n log n)



另一个优化的代码:

1: P = {F, Q − F}

2:     for all a ∈ A do

3:         Add((min(F, Q − F), a), S)

4:     while S ≠ ∅ do

5:         get (C, a) from S (we extract (C, a) according to the

strategy associated with S: FIFO/LIFO/...)

6:         for each B ∈ P split by (C, a) do

7:             B′, B′′ are the sets resulting from splitting of B w.r.t. (C, a)

8:             Replace B in P with both B′ and B′′

9:             for all b ∈ A do

10:                if (B, b) ∈ S then

11:                    Replace (B, b) by (B′, b) and (B′′, b) in S

12:                else

13:                    Add((min(B′,B′′), b), S)





找出无用状态:

state_graph1 = {
'total_states': [ 'A', 'B', 'C', 'D', 'E' ],
'initial_states': [ 'A' ],
'termination_states': [ 'D' ],
'state_transition_map': {
'A': { 'a': 'B', 'b': 'C' },
'B': { 'a': 'B', 'b': 'D' },
'C': { 'a': 'B' },
'E': { 'a': 'E', 'b': 'E', },
'D': { 'a': 'B' },
},
'cins': [ 'a', 'b' ],
} def get_unreachable_states( G ):
reachable_states = set( G['initial_states'] )
new_states = set( G['initial_states'] )
total_states = set( G['total_states'] )
cins = G['cins']
state_transition_map = G['state_transition_map'] while True:
temp_set = set()
for state in new_states:
for char in cins:
try:
next_state = state_transition_map[state][char]
temp_set.update( next_state )
except KeyError:
pass new_states = temp_set - reachable_states
reachable_states.update( temp_set )
if new_states == set():
break unreachable_states = total_states - reachable_states
return unreachable_states print get_unreachable_states( state_graph1 )

Hopcroft:

import random
from copy import deepcopy state_graph1 = {
'total_states': [ '1', '2', '3', '4', '5', '6', '7' ],
'initial_states': [ '1' ],
'termination_states': [ '6', '7' ],
'state_transition_map': {
'1': { 'a': '3', 'b': '2' },
'2': { 'a': '4', 'b': '2' },
'3': { 'c': '3', 'b': '6', 'd': '5' },
'4': { 'b': '7', 'd': '5', 'c': '3' },
'5': { 'a': '4' },
'6': { 'b': '6' },
'7': { 'b': '6' },
},
'cins': [ 'a', 'b', 'c', 'd' ],
} state_graph2 = {
'total_states': [ 'A', 'B', 'C', 'D', 'E', 'F', 'S' ],
'initial_states': [ 'A' ],
'termination_states': [ 'C', 'D', 'E', 'F' ],
'state_transition_map': {
'S': { 'a': 'A', 'b': 'B' },
'A': { 'a': 'C', 'b': 'B' },
'B': { 'a': 'A', 'b': 'D' },
'C': { 'a': 'C', 'b': 'E' },
'D': { 'a': 'F', 'b': 'D' },
'E': { 'a': 'F', 'b': 'D' },
'F': { 'a': 'C', 'b': 'E' },
},
'cins': [ 'a', 'b' ],
} state_graph3 = {
'total_states': [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' ],
'initial_states': [ 'A' ],
'termination_states': [ 'C' ],
'state_transition_map': {
'A': { '0': 'B', '1': 'F' },
'B': { '0': 'G', '1': 'C' },
'C': { '0': 'A', '1': 'C' },
'D': { '0': 'C', '1': 'G' },
'E': { '0': 'H', '1': 'F' },
'F': { '0': 'C', '1': 'G' },
'G': { '0': 'G', '1': 'E' },
'H': { '0': 'G', '1': 'C' }
},
'cins': [ '0', '1' ],
} def hopcroft_algorithm( G ):
cins = set( G['cins'] )
termination_states = set( G['termination_states'] )
total_states = set( G['total_states'] )
state_transition_map = G['state_transition_map']
not_termination_states = total_states - termination_states def get_source_set( target_set, char ):
source_set = set()
for state in total_states:
try:
if state_transition_map[state][char] in target_set:
source_set.update( state )
except KeyError:
pass
return source_set P = [ termination_states, not_termination_states ]
W = [ termination_states, not_termination_states ] while W: A = random.choice( W )
W.remove( A ) for char in cins:
X = get_source_set( A, char )
P_temp = [] for Y in P:
S = X & Y
S1 = Y - X if len( S ) and len( S1 ):
P_temp.append( S )
P_temp.append( S1 ) if Y in W:
W.remove( Y )
W.append( S )
W.append( S1 )
else:
if len( S ) <= len( S1 ):
W.append( S )
else:
W.append( S1 )
else:
P_temp.append( Y )
P = deepcopy( P_temp )
return P print hopcroft_algorithm( state_graph1 )
print hopcroft_algorithm( state_graph2 )
print hopcroft_algorithm( state_graph3 )

岛津义弘:

“真田幸村,这片 ‘ 战国 ’ 的土地上有太多的冷漠和争斗。

一个人想要在这种 ‘ 乱世 ’ 中心存温和。他前进的道路定然会非常痛苦,

可是最后能走到 ‘ 武 ’ 之巅峰的人,却往往又都是那样内心温和的人。

由于这份温和可以让人变得非常强壮。

希望你即便面对的是你的敌人,挥舞自己的 ‘ 双枪 ’ 时,也不要失去这份温和。”







版权声明:本文博客原创文章,博客,未经同意,不得转载。

DFA最小化 -- Hopcroft算法 Python实现的更多相关文章

  1. DFA 最小化

    NDFA.εNDFA 确定化的细节这里就不总结了,这里说一说DFA最小化的算法. 关于DFA最小化,

  2. dfa最小化,修正了上个版本的一些错误。

    上个版本测试的时候,只用了两个非常简单的测试用例,所以好多情况有问题却没有测试出来 bug1:在生成diff_matrix的时候,循环变量少循环了一次,导致最后一个节点在如果无法与其他点合并的情况下, ...

  3. 编译原理中DFA最小化

    关于编译原理最小化的操作,专业术语请移步至:http://www.360doc.com/content/18/0601/21/11962419_758841916.shtml 这里只是记录一下个人的理 ...

  4. 第九次作业——DFA最小化,语法分析初步

    老师:MissDu 提交作业 1.将DFA最小化:教材P65 第9题 答: 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 3.自上而下语法分析,回溯产生的原因是 ...

  5. DFA最小化,语法分析初步

    1.将DFA最小化:教材P65 第9题 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 语言:(01 | 10)*(01 | 10) 自动机图: DFA状态转换矩阵 ...

  6. 编译原理之DFA最小化,语法分析初步

    1.将DFA最小化: 状态转换图: 识别语言:b*ac*(da)*bb* 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 (1)正规式: S -> 0(1S+ ...

  7. 第九次作业 DFA最小化,语法分析初步

    1.将DFA最小化:教材P65 第9题 Ⅰ {1,2,3,4,5} {6,7} {1,2}b={1,2,3,4,5} 3,4}b={5} {6,7} Ⅱ {1,2}{3,4}{5} {6,7} 2.构 ...

  8. 作业九——DFA最小化

    1.将DFA最小化:教材P65 第9题 I {1, 2, 3, 4, 5} {6, 7} {1, 2}b->{1, 2, 3, 4, 5} {3, 4}b->{6, 7} {5}b-> ...

  9. 编译原理:DFA最小化,语法分析初步

    1.将DFA最小化:教材P65 第9题   解析: 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 解析: S→ 0A|1B →S → 0(1S|1)|1(0S|0 ...

随机推荐

  1. Android常用动画Animation的使用

    Andriod中有几种常用的Animation AlphaAnimation  淡入淡出效果 RotateAnimation 旋转效果 ScaleAnimation 缩放动画 TranslaAnima ...

  2. Lua学习笔记6:C++和Lua的相互调用

        曾经一直用C++写代码.话说近期刚换工作.项目组中的是cocos2dx-lua,各种被虐的非常慘啊有木有.     新建cocos2dx-lua项目.打开class能够发现,事实上就是C++项 ...

  3. Winform TabControl控件使用

    运行效果: 代码: /// <summary> /// 添加选项卡 /// </summary> /// <param name="sender"&g ...

  4. 05-IOSCore - 单例模式、KVO

    单例模式 是设计模式之一,使用频率高,让数据或对象在程序的各个地方都能访问,保持唯一 要素: 各个地方都能访问方法 + 静态消息 只要导入类 就能访问 保持唯一 1.在静态消息内限制对象的创建 2.外 ...

  5. C#利用lambda实现委托事件的挂接

    转自:http://www.cdtarena.com/cpx/201307/9287.html在写一个小程序的时候,碰到了这样的问题,需要用委托来挂接事件,但是又想在这事件中使用局部的变量,而委托一旦 ...

  6. 判断程序是否在VMWare内运行

    现在有许多用户都喜欢用虚拟机来测试他们的软件,以避免对真实机器环境造成损害.但是在虚拟机中,有些功能是受限,甚至不可能完成的,因此,需要在程序中判断虚拟机的环境,如果程序在虚拟机内运行,则就要把虚拟机 ...

  7. [WPF源代码]QQ空间相册下载工具

    放一个WPF源代码,源代码地址 http://download.csdn.net/detail/witch_soya/6195987 代码没多少技术含量,就是用WPF做的一个QQ空间相册下载工具,效果 ...

  8. ConcurrentModificationException异常解决办法

    今天在写一个带缓存功能的访问代理程序时出现了java.util.ConcurrentModificationException异常,因为该异常是非捕获型异常而且很少见,所以费了些时间才找到问题所在,原 ...

  9. MyEclipse弹出提示窗体

    MyEclipse弹出提示窗体 1.弹窗例如以下

  10. HDU 3966 Aragorn&#39;s Story(树链剖分)

    HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...