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

Updated on: 03-Aug-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements