简体   繁体   English

用于二进制数加法和比较的图灵机

[英]Turing machine for addition and comparison of binary numbers

Good Day everyone!今天是个好日子!

I am trying to solve this Exercise for learning purpose.我正在尝试解决此练习以进行学习。 Can someone guide me in solving these 3 questions?有人可以指导我解决这三个问题吗?

Like I tried the 1st question for addition of 2 binary numbers separated by '+'.就像我尝试了第一个问题,以“+”分隔的 2 个二进制数相加。 where I tried 2 numbers addition by representing each number with respective number of 1's or zeros eg 5 = 1 1 1 1 1 or 0 0 0 0 0 and then add them and the result will also be in the same format as represented but how to add or represent 2 binaries and separating them by +, not getting any clue.在那里我通过用相应数量的 1 或零表示每个数字来尝试 2 个数字加法,例如 5 = 1 1 1 1 1 或 0 0 0 0 0 然后将它们相加,结果也将与所表示的格式相同,但如何添加或表示 2 个二进制文件并用 + 分隔它们,没有得到任何线索。 Will be head of Turing machine move from left and reach plus sign and then move left and right of + sign?图灵机的头部会从左边移动到加号然后左右移动+号吗? But how will the addition be performed.但是将如何执行添加。 As far as my little knowledge is concerned TM can not simply add binaries we have to make some logic to represent its binaries like in the case of simple addition of 2 numbers.就我的一点知识而言,TM 不能简单地添加二进制文件,我们必须制定一些逻辑来表示它的二进制文件,就像简单地将 2 个数字相加一样。 Similar is the case with comparison of 2 binaries?比较2个二进制文件的情况类似吗? Regards问候

I'll start with problems 2 and 3 since they are actually easier than problem 1.我将从问题 2 和 3 开始,因为它们实际上比问题 1 容易。

We'll assume we have valid input (non-empty binary strings on both sides with no leading zeroes), so we don't need to do any input validation.我们假设我们有有效的输入(两边的非空二进制字符串,没有前导零),所以我们不需要做任何输入验证。 To check whether the numbers are equal, we can simply bounce back and forth across the = symbol and cross off one digit at a time.要检查数字是否相等,我们可以简单地在 = 符号上来回弹跳并一次划掉一位数字。 If we find a mismatch at any point, we reject.如果我们在任何时候发现不匹配,我们就会拒绝。 If we have a digit remaining on the left and can't find one on the right, we reject.如果我们在左边有一个数字,而在右边找不到一个,我们就拒绝。 If we run out of digits on the left and still have some on the right, we reject.如果左边的数字用完而右边还有一些,我们拒绝。 Otherwise, we accept.否则,我们接受。

Q    T    Q'    T'    D

q0   0    q1    X     right    // read the next (or first) symbol
q0   1    q2    X     right    // of the first binary number, or
q0   =    q7    =     right    // recognize no next is available

q1   0    q1    0     right    // skip ahead to the = symbol while 
q1   1    q1    1     right    // using state to remember which
q1   =    q3    =     right    // symbol we need to look for
q2   0    q2    0     right
q2   1    q2    1     right
q2   =    q4    =     right

q3   X    q3    X     right    // skip any crossed-out symbols
q3   0    q5    X     left     // in the second binary number
q3   1,b  rej   1     left     // then, make sure the next
q4   X    q4    X,b   right    // available digit exists and
q4   0,b  rej   0,b   left     // matches the one remembered
q4   1    q5    X     left     // otherwise, reject

q5   X    q5    X     left     // find the = while ignoring
q5   =    q6    =     left     // any crossed-out symbols

q6   0    q6    0     left     // find the last crossed-out
q6   1    q6    1     left     // symbol in the first binary
q6   X    q0    X     right    // number, then move right
                               // and start over

q7   X    q7    X     right    // we ran out of symbols
q7   b    acc   b     left     // in the first binary number,
q7   0,1  rej   0,1   left     // make sure we already ran out
                               // in the second as well

This TM could first sanitize input by ensuring both binary strings are non-empty and contain no leading zeroes (crossing off any it finds).这个 TM 可以首先通过确保两个二进制字符串都是非空的并且不包含前导零(划掉它找到的任何东西)来清理输入。

Do to "greater than", you could easily do the following:做“大于”,你可以很容易地做到以下几点:

  1. check to see if the length of the first binary number (after removing leading zeroes) is greater than, equal to, or less than the length of the second binary number (after removing leading zeroes).检查第一个二进制数的长度(去除前导零后)是否大于、等于或小于第二个二进制数(去除前导零后)的长度。 If the first one is longer than the second, accept.如果第一个比第二个长,请接受。 If the first one is shorter than the second, reject.如果第一个比第二个短,则拒绝。 Otherwise, continue to step 2.否则,继续执行步骤 2。

  2. check for equality as in the other problem, but accept if at any point you have a 1 in the first number and find a 0 in the second.像在另一个问题中一样检查相等性,但是如果在任何时候第一个数字中有 1 并且在第二个数字中有 0,则接受。 This works because we know there are no leading zeroes, the numbers have the same number of digits, and we are checking digits in descending order of significance.这是有效的,因为我们知道没有前导零,数字具有相同的位数,并且我们正在按重要性的降序检查数字。 Reject if you find the other mismatch or if you determine the numbers are equal.如果您发现其他不匹配或您确定数字相等,则拒绝。

To add numbers, the problem says to increment and decrement, but I feel like just adding with carry is going to be not significantly harder.要添加数字,问题说要递增和递减,但我觉得只添加进位不会太难。 An outline of the procedure is this:该程序的概要是这样的:

  1. Begin with carry = 0.以进位 = 0 开始。
  2. Go to least significant digit of first number.转到第一个数字的最低有效位。 Go to state (dig=X, carry=0)进入状态(dig=X,carry=0)
  3. Go to least significant digit of second number.转到第二个数字的最低有效位。 Go to state (sum=(X+Y+carry)%2, carry=(X+Y+carry)/2)转到状态(和=(X+Y+进位)%2,进位=(X+Y+进位)/2)
  4. Go after the second number and write down the sum digit.跟随第二个数字并写下总和数字。
  5. Go back and continue the process until one of the numbers runs out of digits.返回并继续该过程,直到其中一个数字用完数字。
  6. Then, continue with whatever number still has digits, adding just those digits and the carry.然后,继续使用仍然有数字的任何数字,只添加这些数字和进位。
  7. Finally, erase the original input and copy the sum backwards to the beginning of the tape.最后,擦除原始输入并将总和向后复制到磁带的开头。

An example of the distinct steps the tape might go through:磁带可能经历的不同步骤的示例:

#1011+101#
#101X+101#
#101X+10X#
#101X+10X=#
#101X+10X=0#
#10XX+10X=0#
#10XX+1XX=0#
#10XX+1XX=00#
#1XXX+1XX=00#
#1XXX+XXX=00#
#1XXX+XXX=000#
#XXXX+XXX=000#
#XXXX+XXX=0000#
#XXXX+XXX=00001#
#XXXX+XXX=0000#
#1XXX+XXX=0000#
#1XXX+XXX=000#
#10XX+XXX=000#
#10XX+XXX=00#
#100X+XXX=00#
#100X+XXX=0#
#1000+XXX=0#
#1000+XXX=#
#10000XXX=#
#10000XXX#
#10000XX#
#10000X#
#10000#

There are two ways to solve the addition problem.有两种方法可以解决加法问题。 Assume your input tape is in the form ^a+b$ , where ^ and $ are symbols telling you you've reached the front and back of the input.假设您的输入磁带采用^a+b$形式,其中^$是告诉您已经到达输入的正面和背面的符号。

  1. You can increment b and decrement a by 1 each step until a is 0, at which point b will be your answer.您可以每一步增加b并减少a 1直到a为 0,此时b将是您的答案。 This is assuming you're comfortable writing a TM that can increment and decrement.这是假设您可以轻松编写可以递增和递减的 TM。
  2. You can implement a full adding TM, using carries as you would if you were adding binary numbers on paper.您可以使用进位实现全加 TM,就像在纸上添加二进制数一样。

For either option, you need code to find the least significant bit of both a and b .对于任一选项,您都需要代码来查找ab的最低有效位。 The problem specifies that the most significant bit is first, so you'll want to start at + for a and $ for b .该问题指定最重要的位是第一位,因此您需要从+开始a$开始b

For example, let's say we want to increment 1011$ .例如,假设我们要增加1011$ The algorithm we'll use is find the least significant unmarked digit.我们将使用的算法是找到最不重要的未标记数字。 If it's a 0 , replace it with a 1 .如果它是一个0 ,用一个1替换它。 If it's a 1 , move left.如果是1 ,则向左移动。

  1. Start by finding $, moving the read head there.首先找到 $,将读取头移到那里。 Move the read head to the left.将读取头向左移动。
  2. You see a 1 .你看到一个1 Move the read head to the left.将读取头向左移动。
  3. You see a 1 .你看到一个1 Move the read head to the left.将读取头向左移动。
  4. You see a 0 .你看到一个0 write 1 .1 .
  5. Return the read head to $.将读取头返回到 $。 The binary number is now 1111$ .二进制数现在是1111$

To compare two numbers, you need to keep track of which values you've already looked at.要比较两个数字,您需要跟踪已查看过的值。 This is done by extending the alphabet with "marked" characters.这是通过使用“标记”字符扩展字母表来完成的。 0 could be marked as X , 1 as Y , for example. 0可以标记为X1标记为Y X means "there's a 0 here, but I've seen it already. X意思是“这里有一个 0,但我已经看到了。

So, for equality, we can start at ^ for a and = for b .因此,对于平等,我们可以开始^a=b (Assuming the input looks like ^a=b$ .) The algorithm is to find the start of a and b , comparing the first unmarked bit of each. (假设输入看起来像^a=b$ 。)算法是找到ab的开始,比较每个的第一个未标记位。 The first time you get to a different value, halt and reject.第一次达到不同的值时,停止并拒绝。 If you get to = and $ , halt and reject.如果您到达=$ ,请停止并拒绝。

Let's look at input ^11=10$ :让我们看看输入^11=10$

  1. Read head starts at ^.读取头从 ^ 开始。
  2. Move the head right until we find an unmarked bit.向右移动头部,直到我们找到一个未标记的位。
  3. Read a 1 .阅读1 Write Y .Y Tape reads ^Y1=10$ .磁带读取^Y1=10$ We're in a state that represents having read a 1 .我们处于表示已读取1
  4. Move the head right until we find = .向右移动头部,直到找到=
  5. Move the head right until we find an unmarked bit.向右移动头部,直到我们找到一个未标记的位。
  6. Read a 1 .阅读1 This matches the bit we read before.这与我们之前阅读的位相匹配。 Write a Y .写一个Y
  7. Move the head left until we find ^ .将头向左移动,直到找到^
  8. Go to step 2.转到步骤 2。
  9. This time, we'll read a 1 in a and read the 0 in b .这一次,我们要读1a与阅读0b We'll halt and reject.我们会停下来拒绝。

Hope this helps to get you started.希望这有助于您入门。

The following program, inspired by the edX / MITx course Paradox and Infinity , shows how to perform binary addition with a Turing machine, where the numbers to be added are input to the Turing machine and are separated by a blank.以下程序受edX / MITx 课程 Paradox 和 Infinity 的启发,展示了如何使用图灵机执行二进制加法,其中将要相加的数字输入到图灵机中并用空格分隔。

The Turing Machine图灵机

  • uses the second number as a counter使用第二个数字作为计数器
  • decrements the second number by one将第二个数字减一
  • increments the first number by one将第一个数字加一

till the second number becomes 0.直到第二个数字变为 0。

在此处输入图片说明

The following animation of the simulation of the Turing machine shows how 13 (binary 1101 ) and 5 (binary 101 ) are added to yield 18 (binary 10010 ).以下图灵机模拟动画显示了如何将 13(二进制1101 )和 5(二进制101 )相加以产生 18(二进制10010 )。

在此处输入图片说明

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

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