[英]Deterministic Finite State Automata for Modulo Comparison
I'm working on creating a deterministic finite state automata for the following problem:我正在为以下问题创建确定性有限状态自动机:
You can create strings made of x's and y's.您可以创建由 x 和 y 组成的字符串。 How do you create a diagram that only accepts the language when the number of (x's mod 4) is greater than the number of (y's mod 4)?当(x's mod 4)的数量大于(y's mod 4)的数量时,如何创建仅接受语言的图表?
What I am currently able to understand is that I need to create a diagram similar to below:我目前能够理解的是,我需要创建一个类似于下图的图表:
>(0,0) -b-> (0,1) -b-> (0,2) -b-> (0,3) -b-> (0,4)
a a a a a
(1,0) -b-> (1,1) -b-> (1,2) -b-> (1,3) -b-> (1,4)
a a a a a
(2,0) -b-> (2,1) -b-> (2,2) -b-> (2,3) -b-> (2,4)
a a a a a
(3,0) -b-> (3,1) -b-> (3,2) -b-> (3,3) -b-> (3,4)
a a a a a
(4,0) -b-> (4,1) -b-> (4,2) -b-> (4,3) -b-> (4,4)
But what I don't understand is how to compare the number of times x and y occur relative to one another.但我不明白的是如何比较 x 和 y 相对于彼此出现的次数。
I try to answer your question by defining the DFA behaviour through a transition table instead of using a transition diagram - a table is easier to type here and it's functionally equivalent to the corresponding transition diagram.我尝试通过转换表而不是使用转换图来定义 DFA 行为来回答您的问题 - 表更容易在此处键入,并且在功能上等同于相应的转换图。
In the table below在下表中
x
's and the number of y
's read so far每个状态都由一个有序对表示,其元素分别表示到目前为止读取的x
的数量和y
的数量x
or an y
while the DFA is in the state shown in the first column of the table第二列和第三列分别包含当 DFA 处于表格第一列所示状态时读取x
或y
时达到的状态Looking at this table, you should easily be able to adapt your diagram so as to make things work properly.查看此表,您应该能够轻松地调整图表以使事情正常工作。 For example, the first line in the table corresponds to the following portion of your diagram例如,表中的第一行对应于图表的以下部分
>(0,0) -y-> (0,1)
x
(1,0)
You have pretty much arrived at the answer in the question itself.您几乎已经得出了问题本身的答案。
First you see which states you need.首先,您会看到需要哪些状态。 You need to calculate n(x) mod 4. This requires 4 states.您需要计算 n(x) mod 4。这需要 4 个状态。 Then you need to calculate n(y) mod 4. This also requires 4 states.然后你需要计算 n(y) mod 4。这也需要 4 个状态。 Now, since you need to calculate both together, you have to intersect them.现在,由于您需要同时计算两者,因此您必须将它们相交。 This will result in 4 x 4 = 16 states.这将导致 4 x 4 = 16 个状态。 Let us name those states as (a, b); a = n(x) mod 4, b = n(y) mod 4
让我们将这些状态命名为(a, b); a = n(x) mod 4, b = n(y) mod 4
(a, b); a = n(x) mod 4, b = n(y) mod 4
. (a, b); a = n(x) mod 4, b = n(y) mod 4
。
Then, you decide what is the initial state and what are the final states.然后,您决定什么是初始状态,什么是最终状态。 In this case, initial state is (0,0)
and final states are { (a,b) : a > b }
.在这种情况下,初始状态为(0,0)
,最终状态为{ (a,b) : a > b }
。
So, this is what you get finally:所以,这就是你最终得到的: DFA drawer link DFA 抽屉链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.