The Wayback Machine - https://web.archive.org/web/20240607083331/https://www.geeksforgeeks.org/construct-turing-machine-language-l-wwr-w-0-1/
Open In App

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

Last Updated : 29 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 will be accepted by this language. The beginning and end of string is marked by $ sign. For example, if first part w = 1 1 0 0 1 then second part wr = 1 0 0 1 1. It is clearly visible that wr is the reverse of w, so the string 1 1 0 0 1 1 0 0 1 1 is a part of given language. It can also be said that the strings which are palindrome of even length will be accepted by this language.

Examples –

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

Basic Representation –  Approach 1: Two Pointers

Start from the beginning of the input tape. If the symbol is 0, replace it with Y and move right, or if it’s 1, replace it with X and move right.

Once at the end of the string, move back to the position next to the symbol replaced at the beginning and repeat the process.

In the new state, check if the symbol at the corresponding position from the end matches the one at the beginning. If they match, continue; otherwise, reject the string.

Move left and repeat the process until the entire string is processed.

If all symbols match as expected, accept the string.

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

Approach Used – First check the first symbol, if it’s 0 then replace it by Y and by X if it’s 1. Then go to the end of string. So last symbol is same as first. We replace it also by X or Y depending on it. Now again come back to the position next to the symbol replace from the starting and repeat the same process as told above. One important thing to note is that since wr is reverse of w of both of them will have equal number of symbols. Every time replace a nth symbol from beginning of string, replace a corresponding nth symbol from the end.

  • Step-1: If symbol is 0 replace it by Y and move right, Go to state Q2 If symbol is 1 replace it by X and move right, Go to state Q1
  • Step-2: 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 X replace it by X and move right, Go to state Q3 If symbol is Y replace it by Y and move right, Go to state Q3 If symbol is $ replace it by $ and move right, Go to state Q3
  • Step-3: If symbol is 1 replace it by X and move left, Go to state Q4 If symbol is 0 replace it by Y and move left, Go to state Q5
  • Step-4: If symbol is 1 replace it by 1 and move left If symbol is 0 replace it by 0 and move left Remain on same state
  • Step-5: If symbol is X replace it by X and move right If symbol is Y replace it by Y and move right Go to state Q0
  • Step-6: If symbol is X replace it by X and move right If symbol is Y replace it by Y and move right Go to state Q6 ELSE Go to step 1
  • Step-7: If symbol is X replace it by X and move right, Remain on same state If symbol is Y replace it by Y and move right, Remain on same state If symbol is $ replace it by $ and move left, STRING IS ACCEPTED, GO TO FINAL STATE Q7

Another solution for the given problem:

If we look at the problem ,it is nothing but an even palindrome so the following is an another solution for the given language. Here I have consider Blank as B. The following turing machine checks that the language L = {wwr | w ∈ {0, 1}} is a palindrome or not. 

  • Step 1- if first symbol is “0” then it replace by the blank symbol goes to the right till it finds the blank and by checking that the symbol is blank or not .
  • Step2- after getting the last symbol go to the one step left and check that symbol it is “0” or not and 
  • step3- if the last symbol is “0” then replace it with blank symbol and keep going left until it find the blank symbol 
  • step4- after finding the left most blank symbol go one step right and check it “0” or “1” 
  • Step 5- if we get “1” then replace it by the blank symbol and goes to the right until it find the blank symbol.
  • Step 6- if it find the blank symbol then take one step left and check that symbol is “1” or not .If it is “1” then replace it with the blank symbol.
  • step7- after replacing it goes to the left and find the blank symbol after finding the blank symbol take one step right and check if the symbol is “1” or “0”
  • Step 8- repeat the above steps until the whole string become blank .


Previous Article
Next Article

Similar Reads

Construct a Turing Machine for language L = {ww | w ∈ {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
NPDA for accepting the language L = {wwR | w ∈ (a,b)*}
Problem: Design a non deterministic PDA for accepting the language L = {wwR w ∈ (a, b)+}, i.e., L = {aa, bb, abba, aabbaa, abaaba, ......} Prerequisite - Pushdown Automata, Pushdown Automata Acceptance by Final State Explanation: In this type of input string, one input has more than one transition states, hence it is called non-deterministic PDA, a
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