简体   繁体   English

语言图灵机 L={a^mb^na^mb^n ∣ m,n≥0}

[英]Turing machine for language L={a^m b^n a^m b^n ∣ m,n≥0}

I am having trouble making a Turing machine for language L={a^mb^na^mb^n ∣ m,n≥0}我无法为语言 L={a^mb^na^mb^n ∣ m,n≥0} 制作图灵机

What I have thought so far is:到目前为止我的想法是:

If we start with a blank, the string is empty and it should accept, if not, start reading as and I thought to mark the a's with X and the b's with Y would be OK如果我们以空白开头,则字符串为空,它应该接受,如果不是,则开始阅读 as,我认为用 X 标记 a,用 Y 标记 b 就可以了

A high-level strategy for designing a TM for this is the following:为此设计 TM 的高级策略如下:

  1. Check to see whether you're looking at a string of the form a^2k or b^2k (including the empty string).检查您是否正在查看格式为 a^2k 或 b^2k 的字符串(包括空字符串)。 In any of these cases, halt-accept.在任何这些情况下,停止接受。 Otherwise, continue to step 2.否则,继续执行步骤 2。
  2. Cross off pairs of a , one each from the first and third sections, respectively, until one of the sections runs out of a .从第一部分和第三部分中分别划掉一对a ,直到其中一个部分用完a If one runs out while the other still has a , halt-reject.如果一个用完而另一个仍然a , 停止拒绝。 Otherwise, continue to step 3.否则,继续执行步骤 3。
  3. Cross off pairs of b , one each from the second and fourth sections, respectively, until one of the sections runs out of b .划掉成对的b ,分别来自第二和第四部分,直到其中一个部分用完b If one runs out while the other still has b , halt-reject.如果一个用完而另一个仍然有b ,则停止拒绝。 Otherwise, halt-accept.否则,停止接受。

There are 4 cases in this question:这个问题有4个案例:

  1. Blank String can be straight away accepted.可以立即接受空白字符串。
  2. List contains only a's so if their total number is even we accept the string.列表仅包含 a,因此如果它们的总数是偶数,我们接受该字符串。
  3. Like point 2 if the input contains only b's.如果输入仅包含 b,则类似于第 2 点。
  4. The input is a combination of both a and b's.输入是 a 和 b 的组合。

I will mark the first set of a's as X and second set of a's as Z and the first set of b's as U and second set of b's as V.我将第一组a标记为X,第二组a标记为Z,第一组b标记为U,第二组b标记为V。

The Turing machine designed is:设计的图灵机是:

图灵机

Here the states {q0,q10} deals with the first case, {q0, q1, q11, q12, q13, q14} deals with the second case, {q0, q4, q15, q16, q17, q18} deals with the third case and {q0, q1, q2, q3, q4, q5, q6, q7, q8, q9} deals with the final case.这里状态{q0,q10}处理第一种情况, {q0, q1, q11, q12, q13, q14}处理第二种情况, {q0, q4, q15, q16, q17, q18}处理第三种情况case 和{q0, q1, q2, q3, q4, q5, q6, q7, q8, q9}处理最后一种情况。

I have also designed the corresponding code in python for this Turing machine.我还为这个图灵机设计了python中对应的代码。

#function to perform action of states
def action(inp, rep, move):
    global tapehead
    if tape[tapehead] == inp:
        tape[tapehead] = rep
        if move == 'L':
            tapehead -= 1
        else:
            tapehead += 1
        return True
    return False

tape = ['B']*50 
string = input("Enter String: ")
i = 5
tapehead = 5
for s in string: #loop to place string in tape
    tape[i] = s
    i += 1

state = 0
a, b, X, Z, U, V, R, L, B = 'a', 'b', 'X', 'Z', 'U', 'V', 'R', 'L', 'B'
oldtapehead = -1
accept = False
while(oldtapehead != tapehead): #if tapehead not moving that means terminate Turing machine
    oldtapehead = tapehead

    if state == 0:
        if action(a, X, R):
            state = 1
        elif action(B, B, R):
            state = 10
        elif action(Z, Z, R):
            state = 7
        elif action(b, U, R):
            state = 4

    elif state == 1:
        if action(a, a, R):
            state = 1
        elif action(b, b, R):
            state = 2
        elif action(B, B, L):
            state = 11

    elif state == 2:
        if action(b, b, R) or action(Z, Z, R):
            state = 2
        elif action(a, Z, L):
            state = 3

    elif state == 3:
        if action(b, b, L) or action(Z, Z, L) or action(a, a, L):
            state = 3
        elif action(X, X, R):
            state = 0

    elif state == 4:
        if action(b, b, R):
            state = 4
        elif action(Z, Z, R):
            state = 5
        elif action(B, B, L):
            state = 15

    elif state == 5:
        if action(Z, Z, R) or action(V, V, R):
            state = 5
        elif action(b, V, L):
            state = 6

    elif state == 6:
        if action(Z, Z, L) or action(V, V, L) or action(b, b, L):
            state = 6
        elif action(U, U, R):
            state = 0

    elif state == 7:
        if action(Z, Z, R):
            state = 7
        elif action(V, V, R):
            state = 8

    elif state == 8:
        if action(V, V, R):
            state = 8
        elif action(B, B, R):
            state = 9

    elif state == 11:
        if action(a, a, L):
            state = 11
        elif action(X, X, R):
            state = 12

    elif state == 12:
        if action(a, Z, R):
            state = 13

    elif state == 13:
        if action(a, X, R):
            state = 12
        elif action(B, B, R):
            state = 14

    elif state == 15:
        if action(b, b, L):
            state = 15
        elif action(U, U, R):
            state = 16

    elif state == 16:
        if action(b, V, R):
            state = 17

    elif state == 17:
        if action(b, U, R):
            state = 16
        elif action(B, B, R):
            state = 18

    else:
        accept = True


if accept:
    print("String accepted on state = ", state)
else:
    print("String not accepted on state = ", state)

You can check any states which are not clear from the figure or test it against any inputs.您可以检查图中不清楚的任何状态或针对任何输入进行测试。 Output for some inputs: Output 对于某些输入:

Enter String: aaaaabbaaaaabb
String accepted on state =  9

Enter String: aaaaaa
String accepted on state =  14

Enter String: 
String accepted on state =  10

Enter String: aaabaaa
String not accepted on state =  5

Enter String: bbb
String not accepted on state =  16

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

相关问题 正则表达式 L(r) = {a^nb^m : n + m 是偶数}, r =? - REGEX L(r) = {a^n b^m : n + m is even}, r =? 设计一种接受语言L = {a ^ n + 1 b ^ 2n c ^ 3n:n&gt; = 0}的图灵机 - Design a turing machine that accepts the language L= {a^n+1 b^2n c^3n: n>=0} L = {a ^ nb ^ nc ^ md ^ m:n&gt; = 1,m&gt; = 1} U {a ^ nb ^ mc ^ md ^ n:n&gt; = 1,m&gt; = 1} isRegular? - L = { a^n b^n c^m d^m : n >= 1, m >= 1 } U { a^n b^m c^m d^n : n >= 1, m >= 1 } isRegular? L = {a^nb^m | n,m&gt;=1, n?= 3m} 不规则? - How L = {a^n b^m | n,m>=1, n != 3m} is not regular? 构造一个生成L = {a ^ pb ^ mc ^ n | n&gt; = 0,m&gt; = 0,p = m + n}的语法 - Construct a grammar that generates L = {a^p b^m c^n|n>=0, m>=0, p=m+n} PDA {a^nb^m | n&lt;=m&lt;=2n} - PDA for {a^n b^m | n<=m<=2n} L = {a^nb^m : m ≥ n, mn 为偶数的 PDA - PDA for L = {a^nb^m : m ≥ n, m-n is even} 如果 M 是图灵机,那么问题是否 L(M) = A 正则语言,可判定? - If M is a turing machine, the question if L(M) = A Regular Language, Decidable? 如何构造 L={a^nb^m where n&lt;=m&lt;=2n} 的下推自动机? - How to construct a pushdown automata for L={a^nb^m where n<=m<=2n}? 为 L={a^mb^n 其中 n&lt;=m&lt;=2n} 构造一个下推自动机? - construct a pushdown automata for L={a^mb^n where n<=m<=2n}?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM