SlideShare uma empresa Scribd logo
1 de 29
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Minterm and maxterm
Minterm and maxtermMinterm and maxterm
Minterm and maxterm
parsa.khan64
 

Mais procurados (20)

Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
Counters In Digital Logic Design
Counters In Digital Logic DesignCounters In Digital Logic Design
Counters In Digital Logic Design
 
Bitwise operators
Bitwise operatorsBitwise operators
Bitwise operators
 
Moore and mealy machines
Moore and mealy machinesMoore and mealy machines
Moore and mealy machines
 
Network flows
Network flowsNetwork flows
Network flows
 
Turing machine - theory of computation
Turing machine - theory of computationTuring machine - theory of computation
Turing machine - theory of computation
 
Automata theory
Automata theoryAutomata theory
Automata theory
 
Multiplexer and DeMultiplexer
Multiplexer and DeMultiplexerMultiplexer and DeMultiplexer
Multiplexer and DeMultiplexer
 
COMPILER DESIGN
COMPILER DESIGNCOMPILER DESIGN
COMPILER DESIGN
 
Flipflops and Excitation tables of flipflops
Flipflops and Excitation tables of flipflopsFlipflops and Excitation tables of flipflops
Flipflops and Excitation tables of flipflops
 
D and T Flip Flop
D and T Flip FlopD and T Flip Flop
D and T Flip Flop
 
1.9. minimization of dfa
1.9. minimization of dfa1.9. minimization of dfa
1.9. minimization of dfa
 
Minterm and maxterm
Minterm and maxtermMinterm and maxterm
Minterm and maxterm
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
 
POST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMPOST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEM
 
Formal Languages and Automata Theory unit 5
Formal Languages and Automata Theory unit 5Formal Languages and Automata Theory unit 5
Formal Languages and Automata Theory unit 5
 

Semelhante a 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
 

Semelhante a 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
 
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
 
Automata Theory - Turing machine
Automata Theory - Turing machineAutomata Theory - Turing machine
Automata Theory - Turing machine
 
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
 

Último

Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Último (20)

Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

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