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

时间:2019-11-10 21:57:12

标签: automata turing-machines

我在制作图灵机时遇到麻烦,该图灵机的语言为L = {a ^ m b ^ n a ^ m b ^ n ∣ m,n≥0}

到目前为止我想的是:

如果我们以空格开头,则该字符串为空,并且应该接受,如果不是,则开始读取as,我认为将a标记为X并将b标记为Y即可

2 个答案:

答案 0 :(得分:2)

为此设计TM的高级策略如下:

  1. 检查是否要查看a ^ 2k或b ^ 2k形式的字符串(包括空字符串)。在任何这些情况下,均应接受暂停。否则,请继续执行步骤2。
  2. 切断a对,分别从第一节和第三节开始,直到其中一个节用完a。如果一个用完了,而另一个仍然有a,则暂停。否则,请继续执行步骤3。
  3. 切断成对的b,分别从第二部分和第四部分开始,直到其中一个部分用完b。如果一个用完了,而另一个仍然有b,则暂停。否则,请停止接受。

答案 1 :(得分:0)

此问题有4种情况:

  1. 可以直接接受空白字符串。
  2. 列表仅包含一个,因此,即使它们的总数是偶数,我们也接受该字符串。
  3. 如果输入仅包含b,则类似于第2点。
  4. 输入是a和b的组合。

我将第一组a标记为X,将第二组a标记为Z,将第一组b标记为U,将第二组b标记为V。

设计的图灵机是:

Turing Machine

这里,状态{q0,q10}处理第一种情况,{q0, q1, q11, q12, q13, q14}处理第二种情况,{q0, q4, q15, q16, q17, q18}处理第三种情况,{q0, q1, q2, q3, q4, q5, q6, q7, q8, q9}处理最后一种情况。

我还为此Turing机器设计了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)

您可以检查图中未明确显示的任何状态,也可以针对任何输入对其进行测试。某些输入的输出:

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