Search code examples
binaryautomatacomputation-theoryturing-machinesturing-complete

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 '+'. where I tried 2 numbers addition by representing each number with respective number of 1's or zeros e.g 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. 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. Similar is the case with comparison of 2 binaries? Regards


Solution

  • I'll start with problems 2 and 3 since they are actually easier than problem 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).

    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. 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. 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.
    2. Go to least significant digit of first number. Go to state (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)
    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#