简体   繁体   English

设计图灵机的状态表

[英]Designing a Turing Machine's state table

Are they any helpful guidelines to describing what a Turing machine does if you already have the pseudo code for the algorithm? 如果您已经拥有算法的伪代码,它们是否有助于描述图灵机的功能?

I'm taking a course on complexity theory and it takes me a while to describe a Turing machine that decides or accepts some language (states, transitions, etc.) even though I know how I could code it in something like C or even assembly. 我正在学习复杂性理论课程,我花了一些时间来描述一个决定或接受某种语言(状态,转换等)的图灵机器,即使我知道如何用C或甚至汇编这样的东西来编码它。 I guess I just haven't had enough practice with Turing machines (working on it), but I appreciate any suggestions. 我想我只是没有足够的图灵机练习(工作),但我很感激任何建议。

edit 编辑

I don't want to make a Turing Machine simulator, I want to describe a Turing Machine on paper (alphabet, states, transitions) for deciding some language. 我不想制作图灵机模拟器,我想在纸上描述图灵机(字母,状态,过渡)来决定某种语言。

Here's a trivial example of what I mean, say I need to write a Turing Machine that goes over a string of 0s and 1s and changes all the 0s in it to 1s. 这是一个简单的例子,我的意思是,我需要编写一个超过0和1的字符串的图灵机,并将其中的所有0更改为1。 For example, if you start with 11010 on the tape (input) it halts with 11111 on the tape (output). 例如,如果从磁带(输入)上的11010开始,它将在磁带(输出)上以11111停止。 Now in a high level language you know it's something like: 现在用高级语言,你知道它是这样的:

Go over every character on tape
    If character is 0 change it to 1

The Turing machine description is informally something like: 图灵机描述非正式地类似于:

You have two states, q and halt. 你有两个状态,q和停止。 When you are on state q and you see a 1, go to the right without changing it. 当您处于状态q并且您看到1时,请在不更改的情况下向右转。 If you see a 0, change it to 1 and go to the right. 如果看到0,则将其更改为1并向右移动。 If you see the blank symbol (end of tape) then go to the halt state. 如果看到空白符号(磁带末尾),则转到暂停状态。

Formally you will have something like {q, halt} for states. 在形式上你会有类似{q,halt}的状态。 {((q, 1) -> (q, 1, R)), ((q, 0) -> (q, 1, R)), ((q, #) -> (halt, 0, L))} for transitions. {((q,1) - >(q,1,R)),((q,0) - >(q,1,R)),((q,#) - >(halt,0,L) )}用于过渡。

Now this problem is trivial, but there are others which are more involving (add unary numbers or recognize a language with equal number of a's, b's, and c's). 现在这个问题是微不足道的,但还有其他更多涉及(添加一元数或识别具有相同数量的a,b和c的语言)。 I could easily write the pseudocode for them, but writing the Turing Machine is far more challenging (takes me a long time) and I was wondering if there were some tips, resources, or guidelines that help me become better at solving problems like that. 我可以轻松地为他们编写伪代码,但编写图灵机更具挑战性(需要我很长时间),我想知道是否有一些技巧,资源或指导方针可以帮助我更好地解决这类问题。

Disclaimer: Your question is very general, hence so is this answer. 免责声明:您的问题很一般,因此答案也是如此。 Note that I'm anything but an expert on TMs, and this approach will usually not be very efficient (I can't even promise it will always be effective). 请注意,我不是TM的专家,这种方法通常效率不高(我甚至不能保证它总是有效的)。 I'm just jotting down some thoughts here. 我只是在这里记下一些想法。

I would suggest trying an approach like this: Take your pseudo-code and reduce it so that it only consists of a) boolean variables and b) if -statements. 我建议尝试这样的方法:取你的伪代码并减少它,使它只包含a)布尔变量和b) if -statements。 For example: 例如:

if THIS_LETTER_IS("A"):
    found_an_even_number_of_A = not found_an_even_number_of_A

if THIS_LETTER_IS("Q") and previous_letter_was_X and found_an_even_number_of_A
        and not checking_for_alternative_2:
    # can't be a word of alternative 1, so check for alternative 2
    going_back_to_start_to_check_for_alternative_2 = True

if going_back_to_start_to_check_for_alternative_2:
    MOVE_TO_PREVIOUS
else:
    MOVE_TO_NEXT

if going_back_to_start_to_check_for_alternative_2 and THIS_LETTER_IS(EMPTY):
    # reached the beginning, so let's check for alternative 2
    going_back_to_start_to_check_for_alternative_2 = False
    checking_for_alternative_2 = True

When you have nested if s, replace them with and s; 当你嵌套if S,以取代他们and S; remove else blocks by using not : 使用not删除else块:

if something:
    if something_else:
        do_a
    else:
        do_b

becomes

if something and something_else:
    do_a

if something and not something_else:
    do_b

Each if block should then only contain one MOVE_TO_PREVIOUS or MOVE_TO_NEXT , possibly a WRITE command and any number of variable assignments. 每个if块应该只包含一个MOVE_TO_PREVIOUSMOVE_TO_NEXT ,可能是WRITE命令和任意数量的变量赋值。

Complete all if clauses such that every single one of your booleans AND the current letter is always checked, duplicating the blocks where neccessary. 完成所有if子句,以便始终检查每一个布尔值和当前字母,并在必要时复制块。 Example: 例:

if something and something_else:
    do_a

becomes

if THIS_LETTER_IS("A") and something and something_else and something_i_dont_care_about_here:
    do_a

if THIS_LETTER_IS("A") and something and something_else and not something_i_dont_care_about_here:
    do_a

if THIS_LETTER_IS("Q") and something and something_else and something_i_dont_care_about_here:
    do_a

if THIS_LETTER_IS("Q") and something and something_else and not something_i_dont_care_about_here:
    do_a

Now, if you have n booleans and m letters, you should have m * 2 n if s. 现在,如果你有n个布尔和m个字母,你应该有m * 2 n, if s。 Just imagine you have stored the booleans in a bitfield, so each possible combination of booleans represents an integer. 想象一下你已经将布尔值存储在一个位域中,因此布尔值的每个可能组合都代表一个整数。 Hence the above becomes 因此,上述变为

if THIS_LETTER_IS("A") and bitfield[0] and bitfield[1] and bitfield[2]:
    do_a

if THIS_LETTER_IS("A") and bitfield[0] and bitfield[1] and not bitfield[2]:
    do_a

# ...

which then becomes 然后变成

if THIS_LETTER_IS("A") and bitfield == 7:
    do_a

if THIS_LETTER_IS("A") and bitfield == 3:
    do_a

# ...

This integer value for the bitfield is the Turing machine state. 位域的该整数值是图灵机状态。 The do_a part is just an assignment to the booleans (ie the bitfield, so it's your new state), a write command (if there's none, just write the letter that was already there) and a movement command, hence explicitly a Turing Machine transition. do_a部分只是布尔值的赋值( do_a域,所以它是你的新状态),写命令(如果没有,只写下已经存在的字母)和移动命令,因此明确地表示图灵机转换。

I hope any of the above makes sense. 我希望上述任何一点都有道理。

Do you need a ready-to-use tool, Turing Machine simulator? 您需要一个即用型工具,图灵机模拟器吗? There are quite many available . 很多可用的 Or actually you want to make your own? 或者实际上你想制作自己的? This seems to be a valid implementation in JavaScript: http://klickfamily.com/david/school/cis119/TuringSim.html you could analyze the code and translate it into C or C++ quite easily. 这似乎是JavaScript中的有效实现: http//klickfamily.com/david/school/cis119/TuringSim.html您可以很容易地分析代码并将其转换为C或C ++。

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

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