You are on page 1of 21

SURESH GYAN VIHAR UNIVERSITY

DEPARTMENT OF COMPUTER ENGINEERING AND


INFORMATION TECHNOLOGY

PROJECT REPORT ON
Implementing Turing Machine using Java

Submitted to: Submitted by:

Name: Mrs, Priyanka Gupta Shailja Mishra-66879

Tejashvi Sharma

B.tech (CSE) VIII

ACA Project

1
ACKNOWLEDGEMENT

Working in a good environment and motivation enhance the


quality of the work and i get it from my college through my
ACA Project. I have been permitted to take this golden
opportunity under the expert guidance of Mrs. Priyanka Gupta
from SGVU, Jaipur. I am heartily thankful to her for completing
my project successfully. She has given me her full experience
and extra knowledge in practical fields. I am also thankful to my
head of department Mr. Sohit Agrawal and all IT staff to guide
me. Finally, I thank all the people who have directly or
indirectly helped me to complete my project.

2
Index

● Introduction

● Type of Turing Machine

● Why java was choosen?

● Algorithm

● Conclusion

3
INTRODUCTION

Turing Machine was invented by Alan Turing in 1936 and


it is used to accept Recursive Enumerable Languages
(generated by Type-0 Grammar). 

A turing machine consists of a tape of infinite length on


which read and writes operation can be performed. The
tape consists of infinite cells on which each cell either
contains input symbol or a special symbol called blank. It
also consists of a head pointer which points to cell
currently being read and it can move in both directions.

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.

4
A TM is expressed as a 7-tuple (Q, T, B, ∑, δ, q0, F)
where:
 
● Q is a finite set of states
● T is the tape alphabet (symbols which can be written
on Tape)
● B is blank symbol (every cell is filled with B except
input alphabet initially)
● ∑ is the input alphabet (symbols which are part of
input alphabet)
● δ is a transition function which maps Q × T → Q × T ×
{L,R}. Depending on its present state and present tape
alphabet (pointed by head pointer), it will move to new
state, change the tape symbol (may or may not) and
move head pointer to either left or right.
● q0 is the initial state
● F is the set of final states. If any state of F is reached,
input string is accepted.

-Example :- L={0^n1^n|n>=1}

5
Types of 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.

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.

6
Implementation:

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.

7
Why java was choosen?

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.

8
Construct a Turing Machine for language L
= {02n1n | n>=0}

Prerequisite – Turing Machine

The language L = {0 2n1n | n >= 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.
Input : 001
Output : YES

Input : 00001
Output : NO

Input : or empty string


Output : YES

9
Basic Representation –

10
Algorithm

Our Java project package contain 3 classes :-

Main.java, TuringMachine.java and


MachinesLibrary.java.

In our TuringMachine.java Class we have defined State


and Transition Space, along with Tape, Pointer to the
Tape, Start state, Accept state and Reject state.

TuringMachine.java Class has Transition Class which


declares Read/Write symbols and states and continues the
transaction in a specified direction(left/right).

11
Rules/Constraints set for the Input String :-

1)For State Q1 when Tape reads ‘1’, ‘0’ and ‘#’ move
right(‘true’ argument for addTransaction() method in
JAVA code) to State Q3, Q2, Q8 and Tape writes ‘x’, ‘x’
and ‘#’ respectively.

2)For State Q2 when Tape reads ‘0’, ‘1’ and ‘#’ move
right to State Q2(self-loop), Q2, Q4 and Tape writes ‘0’,
‘1’ and ‘#’ respectively.

3)For State Q3 when Tape reads ‘0’, ‘1’ and ‘#’ move
right to State Q3, Q3, Q5 and Tape writes ‘0’, ‘1’ and ‘#’
respectively.

4)For State Q4 when Tape reads ‘x’ and ‘0’move


right(‘true’ argument for addTransaction() method in
JAVA code) to State Q4 and Q6 and Tape writes ‘x’ and
‘x’ respectively

5)For State Q5 when Tape reads ‘x’ and ‘1’ move to State
Q5(right/true) and Q6(left/false) and Tape writes ‘x’ and
‘x’ respectively.

12
6)For State Q6 when Tape reads ‘0’, ‘1’ , ‘x’ and ‘#’
move left to State Q6(self-loop), Q6, Q6, Q7 and Tape
writes ‘0’, ‘1’, ‘x’ and ‘#’ respectively.

7)For State Q7 when Tape reads ‘0’ , ‘1’ and ‘x’ move left
to State Q7, Q7, Q1(right/true) and Tape writes ‘0’ , ‘1’
and ‘x’ respectively.

8)For State Q8 when Tape reads ‘x’ and ‘_’ move right to
State Q8 and Qa(Accept State) and Tape writes ‘x’ and
‘_’ respectively.

String Validity Check :- If input string follows above


steps then it is valid and accepted!!!

13
Main.java
packag
e main;

import tm.*;

public class Main


{

public static void main(String[] args)


{
TuringMachine TM1 =
MachinesLibrary.EqualBinaryWords();

boolean done =
TM1.Run("010000110101#010000110101",
false);
if (done==true)
{
System.out.println("The input
was accepted.");

14
}
else
{
System.out.println("The input
was rejected.");
}
}

15
16
17
18
19
20
Conclusion | Benefits

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.

Through our JAVA program we have simulated the


construction and operations produced by a Turing
Machine.

21

You might also like