【发布时间】:2020-08-01 12:21:18
【问题描述】:
我正在使用 python 构建一个 DFA 最小化程序,但我被卡住了。我正在使用我在网上找到的算法,但它没有给我正确的结果,我不知道为什么。
functions = {'p1,c': 'p2', 'p1,d': 'p6', 'p2,c': 'p7', 'p2,d': 'p3', 'p3,c': 'p1', 'p3,d': 'p3', 'p4,c': 'p3',
'p4,d': 'p7', 'p5,c': 'p8', 'p5,d': 'p6', 'p6,c': 'p3', 'p6,d': 'p7', 'p7,c': 'p7', 'p7,d': 'p5',
'p8,c': 'p7', 'p8,d': 'p3'}
DS = ['p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8']
acceptedStates = ['p3']
symbols = ['c', 'd']
# ---- some more code here that's currently not important ----
dist = []
for pair in pairs: #pairs is a list of lists with all possible pairs of states
if (pair[0] in acceptableStates and par[1] not in prihvatljivaStanja) or (pair[0] not in acceptableStates and par[1] in acceptableStates):
dist.append(pair)
for pair in pairs:
for symbol in symbols:
priv1 = functions[par[0] + ',' + symbol]
priv2 = functions[par[1] + ',' + symbol]
if (priv1 in acceptableStates and priv2 not in acceptableStates) or (priv1 not in acceptableStates and priv2 in acceptableStates):
dist.append(pair)
for i in pairs:
if i not in dist:
print(i)
基本上我要做的是在最后打印出所有无法区分的状态。正确的结果是(对于我正在使用的示例)是:
dist = [['p1', 'p5'], ['p2', p8'], ['p4', 'p6']]
我得到的结果是:
dist = [['p1', 'p5'] #correct
['p1', 'p7']
['p2', 'p8'] #correct
['p4', 'p6'] #correct
['p5', 'p7']]
如您所见,所有当前的都在其中,但它有一些不应该在这里的额外内容。在浏览代码时,我可以看到为什么会发生这种情况,但我不确定如何修复它。有任何想法吗?
【问题讨论】:
-
您能链接到您尝试实现的算法吗?
-
@PatrickHaugh 当然。在这里:cs.gettysburg.edu/~ilinkin/courses/Spring-2013/cs301
标签: python python-3.x algorithm computer-science dfa