SlideShare a Scribd company logo
Turing Machine
Implementation
by Sina Rostami
1. Turing Machine
Re-introduction to the Turing Machine.
2. Implementation
concepts
Basis for Implementing the TM.
3. Implementation
Implementing the TM in C++ programming lang.
Turing Machine
Tip
We use _ (underscore) to
show TM's blank symbol in
this lecture.
δ : Q × Γ → Q × Γ × {L, R}
Members of δ AKA Rules.
The heart of a Turing machine is its transition
function δ, as it tells us how the machine gets from
one configuration to another.
A Turing machine configuration is described by its
current state, the current tape contents, and the
current head location.
TM Configuration example
For a state q and two strings u and v,
over the tape alphabet Γ,
we write uqv :
for the configuration where the current state is q,
the current tape contents is uv,
and the current head location is the first symbol of v.
Turing Machine Configuration
For example
1011 q7 0111
represents the configuration when the tape is 10110111,
the current state is q7,
and the head is currently on the first 0 after q7.
Turing Machine Configuration
An accepting configuration is a configuration in which
the state is the accept state.
A rejecting configuration is a configuration in which
the state is the reject state.
Accepting and rejecting configurations are halting
configurations.
Turing Machine example
For L = {01*0}
q_0 q_1 q_2
q_aq_r
0 -> 0, R 0 -> 0, R
_ -> _, R
1 -> 1, R
_ -> _, R
0 -> 0, R
1 -> 1, R
1 -> 1, R
_ -> _, R
Implementation
Basis
We need following concepts to implement TM.
➔ TM ‘s 7-Tuple
For implementing we need the
7-tuple which we discussed
earlier.
➔ I/O
We need some input and output
feature to give the initializing
fields like 7-tuple and input string,
and also see the result of the TM.
But How to Implement our 7-Tuple in code !!!???
Abstraction!
Let’s Start
We start from abstracting some basis,
and then go for some coding and test our idea.
Concepts
➔ Direction
Left, Right
➔ Rule
a -> b, R
➔ State
q_0, q_1, .., q_a, q_r
➔ Result
ongoing, accepted, rejected
➔ TM!
Direction
Only can have 2 values, LEFT and RIGHT
So we Use a simple enum to abstract the direction.
enum class Direction
{
LEFT,
RIGHT,
};
Rule
a -> b , R
In a single rule we must have 2 characters:
● The character that TM reads from tape
● The character that TM writes in tape
And also a Direction, which we already defined.
struct Rule
{
char read_symbol, write_symbol;
Direction direction;
};
State
Every state has a name ( like q_0, q_1, …, q_a, q_r)
and also some transitions to other states with specified Rule!
So we need rules that’ve been mapped to states.
struct State
{
string name;
map<Rule, State> transitions;
};
Result
Like the Direction, Result also can have only 3 values:
1. ON_GOING
2. ACCEPTED
3. REJECTED
enum class Result
{
ACCEPTED,
REJECTED,
ON_GOING,
};
TM
Turing should have followings:
Some states and also q_0, q_a, q_r
Some chars as input alphabet
Some chars as tape alphabet
struct TuringMachine
{
vector<State> states;
State q_0, q_a, q_r;
vector<char> input_alphabet;
vector<char> tape_alphabet;
};
Tip
We use std::vector as
container
Milestone
Turing’s work Our work
TM intro.
Implementation basis
Abstract concepts
Implement required
and auxiliary
functions.
Run and test codes
Turing’s work is much greater than our’s!
What is left ?
Test our TM with
some examples.
And try to break it
Write auxiliary
functions for
connecting
structures
together and
checking given
string to the TM
parse the TM
inputs.
In other word
handle to make an
instance of our TM.
Check input string func.
Result check_input_string(string input_string)
{
size_t current_index = 0;
State current_state = q_0;
Result result = Result::ON_GOING;
print_current_config(input_string, current_index, current_state);
while (result == Result::ON_GOING)
{
handle_current_char(input_string, current_index, current_state);
print_current_config(input_string, current_index, current_state);
if (current_state == q_a)
result = Result::ACCEPTED;
else if (current_state == q_r)
result = Result::REJECTED;
}
return result;
}
Handle current char func.
void handle_current_char(string &input_string, size_t &current_index,
State &current_state)
{
char &head_symbol = input_string[current_index];
for (auto &pair : current_state.transitions)
{
if (pair.first.read_symbol == head_symbol)
{
head_symbol = pair.first.write_symbol; // a -> b
current_index = (pair.first.direction == Direction::LEFT) // R, L
? current_index - 1
: current_index + 1;
current_state = pair.second;
return;
}
}
}
We’re Almost Done!
Now let’s parse and test the
example we just checked out in TM
intro.
Parsing and testing an example func.
Rule r1('0', '0', Direction::RIGHT);
Rule r2('1', '1', Direction::RIGHT);
Rule r3('_', '_', Direction::RIGHT);
Parse Rules
Parsing and testing an example func.
State q_0("q_0");
State q_1("q_1");
State q_2("q_2");
State q_a("q_a");
State q_r("q_r");
Parse States
Parsing and testing an example func.
q_0.transitions.emplace(r1, q_1);
q_0.transitions.emplace(r2, q_r);
q_0.transitions.emplace(r3, q_r);
q_1.transitions.emplace(r2, q_1);
q_1.transitions.emplace(r1, q_2);
q_1.transitions.emplace(r3, q_r);
q_2.transitions.emplace(r3, q_a);
q_2.transitions.emplace(r1, q_r);
q_2.transitions.emplace(r2, q_r);
Parse Transitions(connect States to each other)
Parsing and testing an example func.
TuringMachine tm({q_0, q_1, q_2, q_a, q_r},
{'0', '1'},
{'0', '1', '_'},
q_0, q_a, q_r);
Result result = tm.check_input_string("0111110___");
switch (result)
{
case Result::ACCEPTED:
cout << "ACCEPTED" << endl;
break;
case Result::REJECTED:
cout << "REJECTED" << endl;
break;
}
Create a TM, Call the chek func. on a string and see the output
The result !
"0111110___"
The result on wrong input!
"01111101___"
Thanks!
I hope this gave you a newer and deeper perspective
to the TM.
You can findthis presentation
and the source files
inmygithubprofilewith linkbellow:
https://github.com/sina-rostami
Course : Theory of Languages and Automata
Instructor : Dr. Khasteh
Fall 2020

More Related Content

What's hot

What's hot (20)

Turing machine
Turing machineTuring machine
Turing machine
 
Turing machine
Turing machineTuring machine
Turing machine
 
Turing machine
Turing machineTuring machine
Turing machine
 
Turing machine-TOC
Turing machine-TOCTuring machine-TOC
Turing machine-TOC
 
Turing machine
Turing machineTuring machine
Turing machine
 
Automata Theory - Turing machine
Automata Theory - Turing machineAutomata Theory - Turing machine
Automata Theory - Turing machine
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Introduction to Turing Machine
Introduction to Turing MachineIntroduction to Turing Machine
Introduction to Turing Machine
 
4.6 halting problem
4.6 halting problem4.6 halting problem
4.6 halting problem
 
LINEAR BOUNDED AUTOMATA (LBA).pptx
LINEAR BOUNDED AUTOMATA (LBA).pptxLINEAR BOUNDED AUTOMATA (LBA).pptx
LINEAR BOUNDED AUTOMATA (LBA).pptx
 
Automata theory
Automata theoryAutomata theory
Automata theory
 
Variants of Turing Machine
Variants of Turing MachineVariants of Turing Machine
Variants of Turing Machine
 
Turing machine - theory of computation
Turing machine - theory of computationTuring machine - theory of computation
Turing machine - theory of computation
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Turing machine
Turing machineTuring machine
Turing machine
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
Mealy and moore machine
Mealy and moore machineMealy and moore machine
Mealy and moore machine
 
Multi Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing MachineMulti Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing Machine
 

Similar to Turing machine implementation

Preparatory_questions_final_exam_DigitalElectronics1 (1).pdf
Preparatory_questions_final_exam_DigitalElectronics1 (1).pdfPreparatory_questions_final_exam_DigitalElectronics1 (1).pdf
Preparatory_questions_final_exam_DigitalElectronics1 (1).pdf
rdjo
 
@vtucode.in-module-1-21CS51-5th-semester (1).pdf
@vtucode.in-module-1-21CS51-5th-semester (1).pdf@vtucode.in-module-1-21CS51-5th-semester (1).pdf
@vtucode.in-module-1-21CS51-5th-semester (1).pdf
FariyaTasneem1
 
state_machines1.pdf
state_machines1.pdfstate_machines1.pdf
state_machines1.pdf
rdjo
 

Similar to Turing machine implementation (20)

Theory of computation and automata
Theory of computation and automataTheory of computation and automata
Theory of computation and automata
 
Theory of computation and automata
Theory of computation and automataTheory of computation and automata
Theory of computation and automata
 
Preparatory_questions_final_exam_DigitalElectronics1 (1).pdf
Preparatory_questions_final_exam_DigitalElectronics1 (1).pdfPreparatory_questions_final_exam_DigitalElectronics1 (1).pdf
Preparatory_questions_final_exam_DigitalElectronics1 (1).pdf
 
@vtucode.in-module-1-21CS51-5th-semester (1).pdf
@vtucode.in-module-1-21CS51-5th-semester (1).pdf@vtucode.in-module-1-21CS51-5th-semester (1).pdf
@vtucode.in-module-1-21CS51-5th-semester (1).pdf
 
Unit iv
Unit ivUnit iv
Unit iv
 
Automata based programming
Automata based programmingAutomata based programming
Automata based programming
 
state_machines1.pdf
state_machines1.pdfstate_machines1.pdf
state_machines1.pdf
 
Mba ebooks ! Edhole
Mba ebooks ! EdholeMba ebooks ! Edhole
Mba ebooks ! Edhole
 
Free Ebooks Download ! Edhole
Free Ebooks Download ! EdholeFree Ebooks Download ! Edhole
Free Ebooks Download ! Edhole
 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3
 
Moore and mealy machines
Moore and mealy machinesMoore and mealy machines
Moore and mealy machines
 
Ch2 finite automaton
Ch2 finite automatonCh2 finite automaton
Ch2 finite automaton
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Assignment8
Assignment8Assignment8
Assignment8
 
Iteration
IterationIteration
Iteration
 
Theory of automata
Theory of automataTheory of automata
Theory of automata
 
Ladder Intro Tutorial
Ladder Intro TutorialLadder Intro Tutorial
Ladder Intro Tutorial
 
人力
人力人力
人力
 
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
 
flat unit1
flat unit1flat unit1
flat unit1
 

Recently uploaded

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
parmarsneha2
 

Recently uploaded (20)

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 

Turing machine implementation

  • 2. 1. Turing Machine Re-introduction to the Turing Machine. 2. Implementation concepts Basis for Implementing the TM. 3. Implementation Implementing the TM in C++ programming lang.
  • 3. Turing Machine Tip We use _ (underscore) to show TM's blank symbol in this lecture. δ : Q × Γ → Q × Γ × {L, R} Members of δ AKA Rules. The heart of a Turing machine is its transition function δ, as it tells us how the machine gets from one configuration to another. A Turing machine configuration is described by its current state, the current tape contents, and the current head location.
  • 4. TM Configuration example For a state q and two strings u and v, over the tape alphabet Γ, we write uqv : for the configuration where the current state is q, the current tape contents is uv, and the current head location is the first symbol of v.
  • 5. Turing Machine Configuration For example 1011 q7 0111 represents the configuration when the tape is 10110111, the current state is q7, and the head is currently on the first 0 after q7.
  • 6. Turing Machine Configuration An accepting configuration is a configuration in which the state is the accept state. A rejecting configuration is a configuration in which the state is the reject state. Accepting and rejecting configurations are halting configurations.
  • 7. Turing Machine example For L = {01*0} q_0 q_1 q_2 q_aq_r 0 -> 0, R 0 -> 0, R _ -> _, R 1 -> 1, R _ -> _, R 0 -> 0, R 1 -> 1, R 1 -> 1, R _ -> _, R
  • 8. Implementation Basis We need following concepts to implement TM. ➔ TM ‘s 7-Tuple For implementing we need the 7-tuple which we discussed earlier. ➔ I/O We need some input and output feature to give the initializing fields like 7-tuple and input string, and also see the result of the TM.
  • 9. But How to Implement our 7-Tuple in code !!!???
  • 11. Let’s Start We start from abstracting some basis, and then go for some coding and test our idea.
  • 12. Concepts ➔ Direction Left, Right ➔ Rule a -> b, R ➔ State q_0, q_1, .., q_a, q_r ➔ Result ongoing, accepted, rejected ➔ TM!
  • 13. Direction Only can have 2 values, LEFT and RIGHT So we Use a simple enum to abstract the direction. enum class Direction { LEFT, RIGHT, };
  • 14. Rule a -> b , R In a single rule we must have 2 characters: ● The character that TM reads from tape ● The character that TM writes in tape And also a Direction, which we already defined. struct Rule { char read_symbol, write_symbol; Direction direction; };
  • 15. State Every state has a name ( like q_0, q_1, …, q_a, q_r) and also some transitions to other states with specified Rule! So we need rules that’ve been mapped to states. struct State { string name; map<Rule, State> transitions; };
  • 16. Result Like the Direction, Result also can have only 3 values: 1. ON_GOING 2. ACCEPTED 3. REJECTED enum class Result { ACCEPTED, REJECTED, ON_GOING, };
  • 17. TM Turing should have followings: Some states and also q_0, q_a, q_r Some chars as input alphabet Some chars as tape alphabet struct TuringMachine { vector<State> states; State q_0, q_a, q_r; vector<char> input_alphabet; vector<char> tape_alphabet; }; Tip We use std::vector as container
  • 18. Milestone Turing’s work Our work TM intro. Implementation basis Abstract concepts Implement required and auxiliary functions. Run and test codes Turing’s work is much greater than our’s!
  • 19. What is left ? Test our TM with some examples. And try to break it Write auxiliary functions for connecting structures together and checking given string to the TM parse the TM inputs. In other word handle to make an instance of our TM.
  • 20. Check input string func. Result check_input_string(string input_string) { size_t current_index = 0; State current_state = q_0; Result result = Result::ON_GOING; print_current_config(input_string, current_index, current_state); while (result == Result::ON_GOING) { handle_current_char(input_string, current_index, current_state); print_current_config(input_string, current_index, current_state); if (current_state == q_a) result = Result::ACCEPTED; else if (current_state == q_r) result = Result::REJECTED; } return result; }
  • 21. Handle current char func. void handle_current_char(string &input_string, size_t &current_index, State &current_state) { char &head_symbol = input_string[current_index]; for (auto &pair : current_state.transitions) { if (pair.first.read_symbol == head_symbol) { head_symbol = pair.first.write_symbol; // a -> b current_index = (pair.first.direction == Direction::LEFT) // R, L ? current_index - 1 : current_index + 1; current_state = pair.second; return; } } }
  • 22. We’re Almost Done! Now let’s parse and test the example we just checked out in TM intro.
  • 23. Parsing and testing an example func. Rule r1('0', '0', Direction::RIGHT); Rule r2('1', '1', Direction::RIGHT); Rule r3('_', '_', Direction::RIGHT); Parse Rules
  • 24. Parsing and testing an example func. State q_0("q_0"); State q_1("q_1"); State q_2("q_2"); State q_a("q_a"); State q_r("q_r"); Parse States
  • 25. Parsing and testing an example func. q_0.transitions.emplace(r1, q_1); q_0.transitions.emplace(r2, q_r); q_0.transitions.emplace(r3, q_r); q_1.transitions.emplace(r2, q_1); q_1.transitions.emplace(r1, q_2); q_1.transitions.emplace(r3, q_r); q_2.transitions.emplace(r3, q_a); q_2.transitions.emplace(r1, q_r); q_2.transitions.emplace(r2, q_r); Parse Transitions(connect States to each other)
  • 26. Parsing and testing an example func. TuringMachine tm({q_0, q_1, q_2, q_a, q_r}, {'0', '1'}, {'0', '1', '_'}, q_0, q_a, q_r); Result result = tm.check_input_string("0111110___"); switch (result) { case Result::ACCEPTED: cout << "ACCEPTED" << endl; break; case Result::REJECTED: cout << "REJECTED" << endl; break; } Create a TM, Call the chek func. on a string and see the output
  • 28. The result on wrong input! "01111101___"
  • 29. Thanks! I hope this gave you a newer and deeper perspective to the TM. You can findthis presentation and the source files inmygithubprofilewith linkbellow: https://github.com/sina-rostami Course : Theory of Languages and Automata Instructor : Dr. Khasteh Fall 2020