Prerequisite – Turing Machine
Draw Turing machine to accept maximum of two unary numbers n1 and n2 given as B0n1C0n2B. If two numbers are equal, then accept either oftwo numbers.
Logic Used :
- For every 0 on left side, replace it with X and search for another 0 on right side. If found, replace it with X and move searching for 0 on left side.
- If not found, it means that left side has more number of 0s that right side. That is n1 > n2. Hence change all Xs to Os and stop on leftmost side.
- Keep doing step 1. If it happens that you point to C, after rightmost conversion (Replacing value of 0 with X on right side), then it implies that n2 >= n1. Hence convert all Xs to Os on right-hand side and stop.
Steps to be followed :
- From initial state q0, on receiving 0, replace with X and move rightwards and change state q1. On receiving C, move rightwards and change state to q7.
- From state q1, on receiving 0 keep moving right. On receiving C move rightwards and change state to q2.
- From state q2 on receiving X remains in same state and keep moving right. On receiving 0, replace it with X, move leftwards and change state to q5. On receiving blank(B) move left and change state to q3.
- From state q3, keep moving left on receiving 0 or C and on receiving X, replace X with 0 and move leftwards. Finally, on receiving blank space move right and change state to q4, accepting state for n1>n2.
- From state q5, on receiving C move leftwards and change state to q6. But on receiving X or 0 stay in the same state and keep moving left.
- From state q6, on receiving 0 keep moving left without changing state. But on receiving X replace it with 0 and move rightwards and change state back to q0.
- From state q7, keep moving rightwards on receiving 0 and change every X with 0 while moving rightwards. On receiving B move leftward and change state to q8.
- On receiving 0 in state q8, keep moving left and on receiving C move rightwards and change state to q9, accepting state for n2 >= n1.
Here, q0 shows initial state and q1, q2, ….., q9 are transition states and q4, q9 shows final state.
And 0, 1 is data inside machine and X, Y, C are variables used for finding maximum, i.e, comparing data and R, L shows right and left.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.