简体   繁体   English

如何确定有限状态自动机是否具有确定性?

[英]How to determine whether finite-state automata is deterministic?

I have a automata coursework that is really bothering me by its difficulty. 我有一个自动化课程,真的困扰我的困难。 I have spent almost 5 hours today doing without actually knowing whether it's right or not. 我今天花了将近5个小时,却没有真正知道它是否正确。 The task is to write a Java code that will determine whether the given automaton is deterministic. 任务是编写一个Java代码,用于确定给定的自动机是否具有确定性。 What I have is a class that represents automata and it has the following five variables: int alphabet_size; 我所拥有的是一个代表自动机的类,它有以下五个变量:int alphabet_size; int n_states; int n_states; int delta[][]; int delta [] []; int initial_state; int initial_state; int accepting_states[]; int accepting_states [];

Delta represents following eg in real automata q1, 1, q2 in program would look like {1, 1, 2}. Delta代表跟随例如真实自动机q1,1,q2在程序中将看起来像{1,1,2}。 I know that in a non-deterministic automaton there could be more than one possible state to go to, but I don't know how to compute that. 我知道在非确定性自动机中可能存在多个可能的状态,但我不知道如何计算它。

Thanks in advance. 提前致谢。

To solve it I would go this way: 为了解决这个问题,我会这样做:

  1. Create a Map (String, String or integer), I will use the key to check what actual state>symbol is and the value to track which state the transaction goes to (null or -1 if the actual state do not have a transaction with that symbol); 创建一个Map(字符串,字符串或整数),我将使用该键来检查实际状态>符号是什么以及跟踪事务进入哪个状态的值(如果实际状态没有事务,则为null或-1)那个符号);
  2. Populate the map keys and values, ex: map.put("1>1", null) means from state 1 with symbol 1 goes to null. 填充映射键和值,例如:map.put(“1> 1”,null)表示从状态1开始,符号1变为null。 For now, put null or -1 for every possibility; 现在,为每种可能性设置null或-1;
  3. Iterate over your transaction possibilities, build the key string, retrieve fom the map the corresponding value. 迭代您的交易可能性,构建密钥字符串,从地图中检索相应的值。 If it is null include the number of the state that goes to, else means there is another transaction coming from the same state with the same symbol that goes to a different state, so it is not deterministic; 如果它为null,则包含进入的状态的数量,否则表示存在来自相同状态的另一个事务,该状态具有相同的符号,进入不同的状态,因此它不是确定性的;

If you can iterate over the whole array it is deterministic! 如果你可以迭代整个数组,那就是确定性的!

With the member variables that you have now, you can only represent a deterministic FSA. 使用您现在拥有的成员变量,您只能表示确定性FSA。 Why? 为什么? Because a NDFSA is one where there is a state q 1 and input a such that both 因为一个NDFSA是其中有一个状态q 1输入 ,使得两个

  • (q 1 , a) -> q 2 (q 1 ,a) - > q 2
  • (q 1 , a) -> q 3 (q 1 ,a) - > q 3

are in delta. 三角洲。 You implement this (q from , input) -> q to mapping as int[][] , which means that for each instate-input pair, you can only store one outstate. 你实现了这个(q from ,input) - > q to mapping as int[][] ,这意味着对于每个instate-input对,你只能存储一个outstate。

One very quick fix would be to allow a list of outstates for all instate-input pair, like so: 一个非常快速的解决方法是允许所有instate-input对的outstates列表,如下所示:

ArrayList[][] delta;

However, this is ugly. 但是,这很难看。 We can arrive at a nicer solution* if you realize that delta is actually the mapping q from -> input -> q to , and it can be bracketed in two ways: 如果你意识到delta实际上是 - > input - > q q的映射q 我们可以得到一个更好的解决方案*,它可以用两种方式括起来:

  • (q from -> input) -> q to : this is what you had, with a little different notation (q from - > input) - > q to :这就是你所拥有的,带有一点不同的符号
  • q from -> (input -> q to ) q from - >(输入 - > q

Another thing to realize is that int[] is conceptually the same as Map<Integer, Integer> . 要实现的另一件事是int[]在概念上与Map<Integer, Integer> With that, the second mapping can implemented in Java as: 有了它,第二个映射可以在Java中实现为:

Map<Integer, Map<Integer, List<Integer>>> delta;

Whichever implementation you choose, the test that decides if the FSA is deterministic or not is very simple, and comes from the definition: A is a DFSA iff the lengths of all lists in delta are <= 1; 无论您选择哪种实现,决定FSA是否具有确定性的测试都非常简单,并且来自定义: 如果delta中所有列表的长度<= 1,则A是DFSA; otherwise, it is a NDFSA . 否则,它是NDFSA

*Note that real FSA libraries usually use a different approach. *请注意,真正的FSA库通常使用不同的方法。 For instance, foma uses an array of transition object with fields instate, outstate and the input character. 例如, foma使用具有字段instate,outstate和输入字符的转换对象数组。 However, for starters, the ones above are probably easier to handle. 但是,对于初学者来说,上面的那些可能更容易处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM