- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Construct Turing machine for L = {an bm a(n+m) - n,m≥1} in C++

**Turing Machine** − A Turing machine is a device used to accept words of a language generated by type 0 grammars. A Turing Machine (TM) is a mathematical model which consists of an infinite length tape divided into cells on which input is given. It consists of a head which reads the input tape. A state register stores the state of the Turing machine. After reading an input symbol, it is replaced with another symbol, its internal state is changed, and it moves from one cell to the right or left. If the TM reaches the final state, the input string is accepted, otherwise rejected.

A TM can be formally described as a 7-tuple (Q, X, Σ, δ, q0, B, F) where −

- Q is a finite set of states
- X is the tape alphabet
- Σ is the input alphabet
- δ is a transition function; δ : Q × X → Q × X × {Left_shift, Right_shift}.
- q0 is the initial state
- B is the blank symbol
- F is the set of final states

Our goal is to construct a Turing machine TM which accepts the language

L= anbma(n+m) where n,m>=1

Let us take examples of words that TM can accept,

- abaa, n=1,m=1
- aabaaa, n=2,m=1
- abbaaa, n=1,m=2
- aaabaaaa, n=3,m=1

That is n times a followed by m times b followed by n+m times a again. n,m>=1

The least no. of a’s will always be 3 and and b be 1. When both n=m=1.

The approach is summarized below −

The machine first accepts all n no. of a’s followed by all m no. of b’s. Then as more a’s are encountered, it starts deleting previous input b’s and a’s. In the end when no new a’s are coming and the head reaches to the first input character, it means all characters are processed correctly. Let us follow step by step for input string −

**Transitions from state q0**

δ (q0, a) → ( q1, x, R ). At state q0 if character read is a then transit to state q1, make it x and move right to point the next character in the string.

Ex − aabaaa → xabaaa ( first character became x head moved right to next a )

δ ( q0, b ) → ( q3, x, R ). At state q0 if character read is a then transit to state q3, make it x and move right to point the next character in string.

Ex − babaaa…. → xabaaa…. (first character became x head moved right to next a )

Here x is used to represent the first character.

**Transitions from state q1**

δ ( q1, a ) → ( q1, a, R ). At state q1 if character read is a then remain at state q1, and move right to point the next character in the string.

Ex: xaabaaa… → xaabaaa… (for rest a’s do nothing and move right)

δ ( q1, b ) → ( q2, b, R ). At state q1 if character read is b then remain at state q1, and move right to point the next character in string.

Ex − xaabaaa… → xaabaaa… (for rest b’s do nothing and move right)

**Transitions from state q2**

δ ( q2, b ) → ( q2, b, R ). At state q2 if character read is b then remain at state q2, and move right to point the next character in the string.

Ex − xaabbbaaa… → xaabbbaaa… (for rest b’s do nothing and move right )

δ ( q2, z ) → ( q2, z, R ). At state q2 if character read is z then remain at state q2, and move right to point the next character in string.

Ex − xaabaazz… → xaabaazz… ( for rest z’s do nothing and move right )

δ ( q2, a ) → ( q3, z, L ). At state q2 if character read is a, make it z then transit to state q3, and move left to point the previous character in string.

Ex − xaabaazz… → xaabazzz… ( for a’s replace with z and move left )

**Transitions from state q3**

δ ( q3, z ) → ( q3, z, L ). At state q3 if character read is z then remain at state q3, and move left to point the previous character in the string.

Ex − xaabzzzz… → xaabzzzzz… ( for z’s do nothing and move left )

δ ( q3, b ) → ( q2, z, R ). At state q2 if character read is b then make it z and transit at state q2, and move right to point the next character in string. Replace all b’s

Ex − xaabzzzz… → xaazzzzz… ( for b’s replace with z and move right )

δ ( q3, a ) → ( q2, z, R ). At state q2 if character read is a then make it z and transit to state q3, and move right to point the next character in string. Replace all a’s

Ex − xaazzzz… → xaazzzzz… ( for a’s replace with z and move right )

δ ( q3, x ) → ( q4, z, R ). At state q2 if character read is x make it z then transit to state q3, and move right to point the next character in string. The first symbol is reached.

Ex − xzzzzzzz… → zzzzzzzz… ( for x replace with z and move right )

**Transitions from state q4**

δ ( q4, z ) → ( q4 z, R ). At state q4 if character read is z then remain at state q4, and move right to point the next character in the string. All characters are z now.

Ex − zzzzzzzz… → zzzzzzzz… ( for all z’s do nothing and move right)

δ ( q4, $ ) → ( qf, $ , R ). At state q4 if no characters remain, end reached. Transit to final state qf. Which means string is accepted.

Ex − zzzzzzzz$ → zzzzzzzz ( for end of string, $ do nothing and move to final state )

Diagram shows the turing machine −

## Input

aabaaa q0: aabaaa → q1: xabaaa → q1: xabaaa → q2: xabaaa → q3: xabzaa → q2: xazzaa q2: xazzaa → q3: xazzza → q3: xazzza → q3: xazzza → q2: xzzzzza → q2: xzzzzza q2: xzzzzza → q2: xzzzzza → q2: xzzzzza → q2: xzzzzzz → q3: xzzzzzz…….. q3: xzzzzzz → q3: xzzzzzz → q4: zzzzzzz → q4: zzzzzzz…….q4: xzzzzzz$ → qf: xzzzzzz$

Reached qf means aabaaa is accepted

- Related Articles
- Construct a Turing Machine for L = {a^n b^n | n>=1}
- Construct a Turing Machine for language L = {0n1n2n | n≥1}
- Construct PDA for L = {0n1m2(n+m) | m,n >=1}
- Construct Pushdown automata for L = {0n1m2(n+m) | m,n = 0} in C++
- Construct Pushdown automata for L = {0(n+m)1m2n | m, n = 0} in C++
- Construct Pushdown automata for L = {0m1(n+m)2n | m,n = 0} in C++
- If ( a=x^{m+n} y^{l}, b=x^{n+l} y^{m} ) and ( c=x^{l+m} y^{n} ), prove that ( a^{m-n} b^{n-1} c^{l-m}=1 . )
- Construct Pushdown automata for L = {a(2*m)c(4*n)dnbm | m,n = 0} in C++
- Construct a PDA for language L = {0n 1m2m3n | n>=1, m>=1}
- Construct DPDA for a(n+m)bmcn n,m≥1 in TOC
- Construct DPDA for anbmc(n+m) n,m≥1 in TOC
- Construct Pushdown automata for L = {0n1m2m3n | m,n = 0} in C++
- Construct a Turing Machine for language L = {wwr | w ∈ {0, 1}}
- If ( x=a^{m+n}, y=a^{n+1} ) and ( z=a^{l+m} ), prove that ( x^{m} y^{n} z^{l}=x^{n} y^{l} z^{m} )
- Construct a Turing machine for L = {aibjck | i< j< k; i ≥ 1}