【问题标题】:How to represent transitions of a NFA in Python?如何在 Python 中表示 NFA 的转换?
【发布时间】:2018-08-27 09:16:38
【问题描述】:

我需要实现以下 NFA:

我用函数表示每个状态,但是在给定输入的情况下,我很难获得所有可能的路径。

例如,输入“bb”我应该有下一个输出:

Path 1: 1, 5, 1
Path 2: 1, 5, 3
Path 3: 1, 5, 7
Path 4: 1, 5, 9

我试图用列表列表或字典来表示转换,但我似乎找不到获取所有可能路径的方法。

【问题讨论】:

标签: python state transition automata nfa


【解决方案1】:

您可以通过多种方式实现矩阵表示或邻接表 这里我有一个例子说明你如何代表你的 NFA

start_node = 1  # keep start nodes
final_states = [9]  # there may be multiple final states so keep them all in list

# Now you can construct a dictionary having key as transition and key as possible states can be reached
# Here key of tuple is (<current-state>, 'input symbol')  and corresponding value is of list containing all the possible states can be reached.
paths = {
(1, 'b') : [5],
(1, 'n') : [2, 4],
(2, 'b') : [3, 5],
(2, 'n') : [4, 6],
(3, 'b') : [5],
(3, 'n') : [2, 6],
}

要对其进行迭代,您可以找到图中的所有状态。如果您将其视为一个图表,您的问题是找到所有可能的路径。

你需要做的是寻找开始状态

  • 如果键中存在带有输入符号的开始状态条目(开始节点),那么您需要探索所有节点(列表元素),否则会中断循环,结果将是字符串不被接受。

    即如果输入字符串是'bb',那么您需要探索[5],因为(1, 'b') 存在

  • 直到字符串结束,您需要探索从起始状态探索的所有节点以及从这些节点探索的节点。

请参阅Python Graph Representation doc 了解基本概述 更多详细信息请参考this线程

【讨论】:

    猜你喜欢
    • 2015-09-16
    • 2019-03-14
    • 1970-01-01
    • 2012-03-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    相关资源
    最近更新 更多