The Wayback Machine - https://web.archive.org/web/20240607081828/https://www.geeksforgeeks.org/turing-machine-as-comparator/
Open In App

Turing Machine as Comparator

Last Updated : 23 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite – Turing Machine

Problem : Draw a turing machine which compare two numbers. Using unary format to represent the number. For example, 4 is represented by 
 

4 = 1 1 1 1  or 0 0 0 0 

Lets use one’s for representation. 

Example: 

 

Approach: 
 

  1. Comparing two numbers by comparing number of ‘1’s.
  2. Comparing ‘1’s by marking them ‘X’.
  3. If ‘1’s are remaining in left of ‘0’, then first number is greater.
  4. If ‘1’s are remaining in right of ‘0’, then second number is greater.
  5. If both ‘1’ are finished then both numbers are equal.

Steps: 
 

  • Step-1: Convert 1 into X and move right and goto step 2. If symbol is 0 ignore it and move right and goto step-6. 
     
  • Step-2: Keep ignoring 1 and move towards right. Ignore 0 move right and goto step-3.
  • Step-3: Keep ignoring X and move towards right. Ignore 1 move left and goto step-4. If B is found ignore it and move left and goto step-9.
  • Step-4: Keep ignoring X and move towards left. Ignore 0 move left and goto step-5.
  • Step-5: Keep ignoring 1 and move towards left. Ignore X move right and goto step-1.
  • Step-6: Keep ignoring X and move towards right. If B is found ignore it and move left and goto step-8. If 1 is found ignore it and move right and goto step-7.
  • Step-7: Stop the Machine (A < B)
  • Step-8: Stop the Machine (A > B)
  • Step-9: Stop the Machine (A = B)

State transition diagram : 
 

  • Comparator for A < B
  • Comparator for A = B
  • Comparator for A > B
  • Universal Comparator for A < B, A = B, A > B 
     

Here, Q0 shows the initial state and Q2, Q3, Q4, Q5 shows the transition state and (A < B), (A = B)and (A > B) shows the final state. 0, 1 are the variables used and R, L shows right and left. 

Explanation: 
 

  • Using Q0, when 1 is found make it X and go to leftand to state Q1. and if 0 found move to right to Q5 state.
  • On the state Q1, ignore all 1 and goto right.If 0 found ignore it and goto right into next state Q2.
  • In Q2, ignore all X and move right.If B found stop the execution and you will goto the state showing A > B, If 1 found make it X move left and to Q3.
  • In Q3 state, ignore all X and move left.If 0 found ignore it move left to Q4.
  • In Q4, ignore all 1 and move left.If X found ignore it move right.
  • In Q5 state, ignore all X and move right.If 1 found ignore it move right and stop the machine it will give the result that A < B.And if, B found move left to stop the machine it will give the result that A = B

 


Previous Article
Next Article

Similar Reads

Turing Machine Construction (Transducers Turing Machine) in Java
Prerequisite - Turing Machine Turing Machines can broadly be classified into two types, the Acceptors and the Transducers. Acceptor Turing Machine is an automaton used to define Turing-acceptable languages. Such a machine can be used to check whether a given string belongs to a language or not. It is defined as a 7-tuple machine. Coming to Transduc
10 min read
Difference between Turing machine and Universal Turing machine
Turing Machine was first described by Alan Turing in the year 1936. It was primarily invented to investigate the computability of a given problem. It accepts type-0 grammar which is Recursively Enumerable language. The Turing machine has a tape of infinite length where we can perform read and write operations. The infinite cells of the Turing machi
3 min read
Construct Turing machine for L = {a<sup>n</sup> b<sup>m</sup> a<sup>(n+m)</sup> | n,m≥1}
L = {an bm a(n+m) | n,m≥1} represents a kind of language where we use only 2 character, i.e., a and b. The first part of language can be any number of "a" (at least 1). The second part be any number of "b" (at least 1). The third part of language is a number of "a" whose count is sum of count of a's in first part of string and count of b's in secon
3 min read
Construct a Turing Machine for language L = {0<sup>n</sup>1<sup>n</sup>2<sup>n</sup> | n≥1}
Prerequisite - Turing Machine The language L = {0n1n2n | n≥1} represents a kind of language where we use only 3 character, i.e., 0, 1 and 2. In the beginning language has some number of 0's followed by equal number of 1's and then followed by equal number of 2's. Any such string which falls in this category will be accepted by this language. The be
3 min read
Construct a Turing Machine for language L = {ww | w &isin; {0,1}}
Prerequisite - Turing Machine The language L = {ww | w ∈ {0, 1}} tells that every string of 0's and 1's which is followed by itself falls under this language. The logic for solving this problem can be divided into 2 parts: Finding the mid point of the string After we have found the mid point we match the symbols Example - Lets understand it with th
7 min read
Construct a Turing machine for L = {aibjck | i&gt;j&gt;k; k &ge; 1}
Prerequisite – Turing MachineIn given language L = {aibjck | i&gt;j&gt;k; k ≥ 1}, every string of ‘a’, ‘b’ and ‘c’ have certain number of a’s, then certain number of b’s and then certain number of c’s. The condition is that count of 3rd symbols should be atleast 1. ‘a’ and ‘b’ can have thereafter be as many but count of c is less than count of ‘b’
3 min read
Turing Machine for addition
Prerequisite - Turing Machine A number is represented in binary format in different finite automata. For example, 5 is represented as 101. However, in the case of addition using a Turing machine, unary format is followed. In unary format, a number is represented by either all ones or all zeroes. For example, 5 will be represented by a sequence of f
3 min read
Construct a Turing machine for L = {a<sup>i</sup>b<sup>j</sup>c<sup>k</sup> | i&lt; j&lt; k; i &ge; 1}
Prerequisite – Turing Machine In given language L = {aibjck | i&lt; j&lt; k; i&ge; 1}, every string of ‘a’, ‘b’ and ‘c’ have certain number of a’s, then certain number of b’s and then certain number of c’s. The condition is that count of 1st symbols should be atleast 1. ‘b’ and ‘c’ can have thereafter be as many but count of a is less than count of
3 min read
Construct a Turing machine for L = {a<sup>i</sup>b<sup>j</sup>c<sup>k</sup> | i &lt; j &lt; k or i &gt; j &gt; k}
Prerequisite – Turing Machine The language L = {aibjck | i &lt; j &lt; k or i &gt; j &gt; k} is same as the union of two languages L1={aibjck | i &lt; j &lt; k } and L2={aibjck | i &gt; j &gt; k } In this language, every string of ‘a’, ‘b’ and ‘c’ have certain number of a’s, then certain number of b’s and then certain number of c’s. The condition i
5 min read
Construct a Turing Machine for a language L = {aibjck | i&lt;j&lt;k or i&gt;j&gt;k} ∩ {aibjck | i&gt;j&gt;k or i&gt;j&gt;k}
Prerequisite – Turing MachineThe language L = {aibjck | i &lt; j &lt; k or i &gt; j &gt; k} ∩ {aibjck | i &gt; j &gt; k or i &gt; j &gt; k} is same as the languages L={aibjck | j &gt; max(i, k)} In this language, every string of ‘a’, ‘b’ and ‘c’ have certain number of a’s, then certain number of b’s and then certain number of c’s. Count of 1st symb
4 min read