The Wayback Machine - https://web.archive.org/web/20240607084833/https://www.geeksforgeeks.org/construct-turing-machine-language-l-ww-w-01/
Open In App

Construct a Turing Machine for language L = {ww | w ∈ {0,1}}

Last Updated : 29 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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: 
 

  1. Finding the mid point of the string 
     
  2. After we have found the mid point we match the symbols 
     

Example – Lets understand it with the help of an example. Lets string 1 0 1 1 0 1, so w = 1 0 1 and string is of form (ww). 
The first thing that we do is to find the midpoint. For this, we convert 1 in the beginning into Y and move right till the end of the string. Here we convert 1 into y. 

Now our string would look like Y 0 1 1 0 Y. Now move left till, find a X or Y. When we do so, convert the 0 or 1 right of it to X or Y respectively and then do the same on the right end. Now our string would look like Y X 1 1 X Y. Thereafter, convert these 1’s also and finally it would look like Y X Y Y X Y. 

At this point, you have achieved the first objective which was to find the midpoint. Now convert all X and Y on the left of midpoint into 0 and 1 respectively, so string becomes 1 0 1 Y X Y. Now, convert the 1 into Y and move right till, find Y in the beginning of the right part of the string and convert this Y into a blank (denoted by B). Now string looks like Y 0 1 B X Y. 

Similarly, apply this on 0 and x followed by 1 and Y. After this string looks like Y X Y B B B. Now that you have no 0 and 1 and all X and Y on the right part of the string are converted into blanks so our string will be accepted. 

Assumption: We will replace 0 by X and 1 by Y. 

Approach Used – 
The first thing is to find the midpoint of the string, convert a 0 or 1 from the beginning of the string into X or Y respectively and a corresponding 0 or 1 into X or Y from the end of the string. After continuously doing it a point is reached when all 0’s and 1’s have been converted into X and Y respectively. At this point, you are on the midpoint of the string. So, our first objective is fulfilled. 

Now, convert all X’s and Y’s on the left of the midpoint into 0’s and 1’s. At this point the first half the string is in the form of 0 and 1. The second half of the string is in the form of X and Y. 

Now, start from the beginning of the string. If you have a 0 then convert it into X and move right till reaching the second half, here if we find X then convert it into a blank(B). Then traverse back till find an X or a Y. We convert the 0 or 1 on the right of it into X or Y respectively and correspondingly, convert its X or Y in the second half of string into a blank(B). 

Keep doing this till converted all symbols on the left part of the string into X and Y and all symbols on the right of string into blanks. If any one part is completely converted but still some symbols in the other half are left unchanged then the string will not be accepted. If you did not find an X or Y in the second half for a corresponding 0 or 1 respectively in the first half. Then also string will not be accepted. 

Examples: 
 

Input  : 1 1 0 0 1 1 0 0 
Output : Accepted

Input  : 1 0 1 1 1 0 1
Output : Not accepted

 

  • Step-1: 
    If the symbol is 0 replace it by X and move right 
    If the symbol is 1 replace it by Y and move right, 
    Go to state Q1 and step 2 
    ——————————————— 
    If the symbol is X replace it by X and move left or 
    If the symbol is Y replace it by Y and move left, 
    Go to state Q4 and step 5 

     

  • Step-2: 
    If the symbol is 0 replace it by 0 and move right, remain on the same state 
    If the symbol is 1 replace it by 1 and move right, remain on the same state 
    ——————————————— 
    If the symbol is X replace it by X and move left or 
    If the symbol is Y replace it by Y and move left or 
    If the symbol is $ replace it by $ and move left, Go to state Q2 and step 3 

     

  • Step-3: 
    If symbol is 0 replace it by X and move left, or 
    If symbol is 1 replace it by Y and move left, 
    Go to state Q3 and step 4 

     

  • Step-4: 
    If the symbol is 0 replace it by 0 and move left, remain on the same state 
    If the symbol is 1 replace it by 1 and move left, remain on the same state 
    ——————————————— 
    If the symbol is X replace it by X and move right or 
    If the symbol is Y replace it by Y and move right, 
    Go to state Q0 and step 1 

     

  • Step-5: 
    If symbol is X replace it by X and move left or 
    If symbol is Y replace it by Y and move left 
    Go to state Q4 and step 6 

     

  • Step-6: 
    If symbol is X replace it by 0 and move left, remain on same state 
    If symbol is Y replace it by 1 and move left, remain on same state 
    – – – – – – – – – — – – – – – – – – – — 
    If symbol is $ replace it by $ and move right 
    Go to state Q4 and step 7 

     

  • Step-7: 
    If symbol is 0 replace it by X and move right, go to state Q6 and step 8 
    If symbol is 1 replace it by Y and move right, go to state Q7 and step 9 
    – – – – – – – – – — – – – – – – – – – — 
    If symbol is B replace it by B and move left, STRING ACCEPTED, GO TO FINAL STATE Q9 

     

  • Step-8: 
    If symbol is 0 replace it by 0 and move right, remain on same state 
    If symbol is 1 replace it by 1 and move right, remain on same state 
    If symbol is B replace it by B and move right, remain on same state 
    – — – – – – – – – – – – – – – – – – – – 
    If symbol is X replace it by B and move left 
    Go to state Q8 and step 10 

     

  • Step-9: 
     

If symbol is 0 replace it by 0 and move right, remain on same state 
If symbol is 1 replace it by 1 and move right, remain on same state 
If symbol is B replace it by B and move right, remain on same state 
– — – – – – – – – – – – – – – – – – – – 
If symbol is Y replace it by B and move left 
Go to state Q8 and step 10 

 

  • Step-10: 
    If symbol is 0 replace it by 0 and move left, remain on same state 
    If symbol is 1 replace it by 1 and move left, remain on same state 
    If symbol is B replace it by B and move left, remain on same state 
    – — – – – – – – – – – – – – – – – – – – 
    If symbol is Y replace it by Y and move right or 
    If symbol is X replace it by X and move right 
    Go to state Q5 and step 7 
     

 

 


Previous Article
Next Article

Similar Reads

Construct a Turing Machine for language L = {wwr | w ∈ {0, 1}}
Prerequisite - Turing Machine  The language L = {wwres | w ∈ {0, 1}} reprents a kind of language where you use only 2 character, i.e., 0 and 1. The first part of language can be any string of 0 and 1. The second part is the reverse of the first part. Combining both these parts out string will be formed. Any such string which falls in this category
5 min read
Turing Machine for language { www | w ∈ {a, b} }
Prerequisite - Turing Machine Design a Turing Machine for a string which contains exactly 3 repetitions of w consecutively. Approach Used : First, we will find the position of separation of the first w from the second w. Now, we will match the first and second w. If both get, matched then the second w string will be converted to a string of $. Then
7 min read
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 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 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
Construct a Turing Machine for language L = {0<sup>2n</sup>1<sup>n</sup> | n&gt;=0}
Prerequisite - Turing Machine The language L = {02n1n | n &gt;= 0} represents a kind of language where we use only 2 symbols, i.e., 0 and 1. In the beginning language has some number of 0's followed by exactly half number of 1's . Any such string which falls in this category will be accepted by this language. Examples : Input : 001 Output : YES Inp
2 min read
Construct a Turing Machine for language L = {a^n b^m c^nm where n &gt;=0 and m &gt;= 0}
Prerequisite - Turing Machine The language L = {anbmcnm | n &gt;= 0 and m&gt;=0} represents a kind of language where we use only 3 symbols, i.e., a, b and c. In the beginning, language has n number of a's followed by m number of b's and then n*m number of c's. Any such string which falls in this category will be accepted by this language. Examples:
3 min read
Construct a DFA which accept the language L = {w | w &isin; {a,b}* and Na(w) mod 3 = Nb (w) mod 3}
Problem: Construct a deterministic finite automata (DFA) for accepting the language L = {w | w ∈ {a,b}* and Na(w) mod 3 = Nb (w) mod 3}. Language L={w | Na(w) = Nb(w)mod 3} which means all string contain modulus of count of a's equal to modulus of count of b's by 3. Examples: Input: a a b b b Output: NOT ACCEPTED // n = 2, m=3 (2 mod 3! = 3 mod 3)
14 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