Example of Turing Machine



In the previous chapter, we presented the concept of Turing machine (TM) and how we can form a TM for a problem. In this chapter, we will see some further examples of Turing machines with which it will be clear for us how the TM can be made using instantaneous description and state diagrams for a better view.

Example 1: Turing Machine for WWR

Let us look at a Turing Machine that accepts the language L = {WWR, where W ∈ (a, b)+}. Here, WR is the reverse of W.

Transition Table

Take a look at its Transition Table −

State a b B X Y
q0 (q1, X, R) (q4, Y, R) - (qf, X, H) (qf, Y, H)
q1 (q1, a, R) (q1, b, R) (q2, B, L) (q2, X, L) (q2, Y, L)
q2 (q3, X, L) - - - -
q3 (q3, a, L) (q3, b, L) - (q0, X, R) -
q4 (q4, a, R) (q4, b, R) (q5, B, L) (q5, X, L) (q5, Y, L)
q5 - (q6, Y, L) - - -
q6 (q6, a, L) (q6, b, L) - - (q0, Y, R)

Note: "-" indicates no transition for that state-symbol pair.

Transition Diagram

Here is its Transition Diagram −

Example of Turing Machine1

Explanation of the Turing Machine

This Turing Machine works as follows −

  • It replaces the first symbol (a or b) with X or Y.
  • It moves to the end of the string.
  • It checks if the last symbol matches the first one.
  • It moves back to the start and repeats the process for the next symbol.
  • If all symbols match, it accepts the string.

Processing "abaaaaba"

Let's see how this Turing Machine processes the string "abaaaaba" −

(q0, abaaaaba) → (q1, Xbaaaaba) → (q1, Xbaaaaba) → (q1, XbaaaabB) → 
(q2, XbaaaabB) → (q3, XbaaaabX) → (q3, XbaaaabX) → (q0, XbaaaabX) → 
(q4, XYaaaabX) → (q4, XYaaaabX) → (q5, XYaaaabX) → (q6, XYaaaaYX) → 
(q0, XYaaaaYX) → (q1, XYXaaaYX) → (q1, XYXaaaYX) → (q2, XYXaaaYX) → 
(q3, XYXaaXYX) → (q0, XYXaaXYX) → (q1, XYXXAXYX) → (q2, XYXXAXYX) → 
(q3, XYXXXXYX) → (q0, XYXXXXYX) → (qf, XYXXXXYX) [Halt]

The Turing Machine accepts the string "abaaaaba" as it is of the form WWR.

Example 2: Turing Machine for Palindromes

Last one is similar to the palindrome but in real case, a palindrome could be of odd length or even length. Let us see the Turing Machine that accepts the language L = {set of all palindromes over a, b}.

Transition Table

Here is its Transition Table −

State a b B
q1 (q2, B, R) (q5, B, R) (q7, B, H)
q2 (q2, a, R) (q2, b, R) (q3, B, L)
q3 (q4, B, L) - (q7, B, H)
q4 (q4, a, L) (q4, b, L) (q1, B, R)
q5 (q5, a, R) (q5, b, R) (q6, B, L)
q6 - (q4, B, L) (q7, B, H)

Transition Diagram

Take a look at tits Transition diagram −

Example of Turing Machine2

Explanation of the Turing Machine

This Turing Machine operates as follows −

  • It replaces the first symbol with a blank (B).
  • It moves to the end of the string.
  • It checks if the last symbol matches the first one.
  • It replaces the last symbol with a blank and moves back to the start.
  • It repeats this process until all symbols are checked.
  • If all symbols match, it accepts the string as a palindrome.

Let's see how this Turing Machine processes different strings −

Null String

(q1,B)(q7,B)[Halt]

The null string is accepted as a palindrome.

String "a"

(q1,aB)(q2,BB)(q3,BB)(q7,BB)[Halt]

The string "a" is accepted as a palindrome.

String "aba"

(q1,abaB)(q2,BbaB)(q2,BbaB)(q3,BbaB)(q4,BBBB)(q1,BBBB)(q7,BBBB)[Halt]

The string "aba" is accepted as a palindrome.

String "baab"

(q1,baabB)(q5,BaabB)(q5,BaabB)(q6,BaaB)(q4,BaBB)(q1,BaBB)(q2,BBaBB)(q3,BBABB)(q7,BBBBB)[Halt]

The string "baab" is accepted as a palindrome.

Example 3: Turing Machine for an bn cn

Finally, let's look at a Turing Machine that accepts the language L = an bn c+, where n ≥ 1.

This language is context-sensitive. It consists of strings with equal numbers of 'a', 'b', and 'c' in that order. The number of each symbol must be at least 1.

Transition Table

Here is its Transition Table −

State a b c B X Y Z
q1 (q2, X, R) - - - - (q5, Y, R) -
q2 (q2, a, R) (q3, Y, R) - - - (q2, Y, R) -
q3 - (q3, b, R) (q4, Z, L) - - - (q3, Z, R)
q4 (q4, a, L) (q4, b, L) - - (q1, X, R) (q4, Y, L) (q4, Z, L)
q5 - - - - - (q5, Y, R) (q6, Z, R)
q6 - - - (qf, B, H) - - (q6, Z, R)

Transition Diagram

Here is its Transition Diagram −

Example of Turing Machine3

Conclusion

In this chapter, we presented detailed examples of three different machines including string with its reverse, then palindrome and finally an bn cn. There are many such examples that can be solved using Turing Machine.

Advertisements