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.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Coming to Transducers: In general, transducers are the devices used to convert one form of signal into another. The same can be told about Turing Machine Transducers.
A transducer is a type of Turing Machine that is used to convert the given input into the output after the machine performs various read-writes. It doesn’t accept or reject an input but performs series of operations to obtain the output right in the same tape and halts when finished.
Few examples of Turing Machine transducers are:
- Turing Machine for addition
- Turing Machine for subtraction
- Turing Machine for multiplication
- Turing Machine for 1’s and 2’s complement
Now we will be proposing a Java program that was written to simulate the construction and execution performed by turing machine transducers. There are two inputs that must be given while executing it: A .txt file to define the automaton (an example for unary multiplication machine is given after the code), and a string to be entered via the console window which will be the input on tape for the automaton to execute.
The .txt file’s path must be given as input. This was done so that the same program can be used for various types of machines instead of hard-coding the automaton. We just need to write a different txt file for generating a different automaton.
Java was chosen specifically because of the OOP structure, using which a class was defined for State, Transition, Machine, etc, to be able to encapsulate various aspects of an entity within an object. For example, a transition is defined as a class whose members are three characters – read, write and shift which store read symbol, write a symbol, and shift direction respectively, along with the index of the next state to which the machine should transition to. The same applies to a State object, which stores a list of possible outgoing transitions.
The input file that defines symbols, states and transitions for Automaton for Unary multiplication:
//lines starting with ‘//’ are ignored by the program while reading so that the input text can be commented
//title, will be printed on console
MACHINE FOR UNARY MULTIPLICATION
//input symbols – 1’s for defining unary-number, 0 as delimiter
//tape symbols – other than inputs
//number of states
//format – read,write,shift,next state
//for state q0, 2 transitions to be read for this state
1 B R 1
0 B R 6
0 0 R 2
1 1 R 1
0 0 L 5
1 Y R 3
0 0 R 3
1 1 R 3
B 1 L 4
0 0 L 4
1 1 L 4
Y Y R 2
0 0 L 5
1 1 L 5
Y 1 L 5
B B R 0
1 B R 6
0 B R 7
//state q7 – machine halts when it reaches this state as 0 transitions are defined
Note: The program must be executed on local machine or on an IDE that accepts path for I/O files.
Following is how the OUTPUT looks like for Unary Multiplication automaton with “1101110” as input (unary 2 * unary 3) :
(after several iterations)
With a bit of effort, the same program can be extended to also work for Acceptor-type Turing machines. Anyone interested is encouraged to improve the article.