Constraint Patterns

Download Report

Transcript Constraint Patterns

Computability Theory
Turing Machines
Professor MSc. Ivan A. Escobar
[email protected]
S
Recalling
S A finite automata is a computational model that has a
limited quantity of memory available.
S They are good models for devices that have a small amount of
memory.
S A pushdown automata is a computational model that has a
unlimited quantity of memory available but possesses some
restrictions in they way it can use it.
S LIFO memory management.
Important Observation
S The previous models don’t have the sufficient properties to
represent a general purpose computational model.
TMs
S We introduce the most powerful of the automata we will
study: Turing Machines (TMs)
S Invented by Alan Turing in 1936.
S TMs can compute any function normally considered
computable
S Indeed we can define computable to mean computable by a
TM
Turing Machines
S A Turing machine (TM) can compute everything a usual computer
program can compute
S may be less efficient
S But the computation allows a mathematical analysis of questions
like :
S What is computable?
S What is decidable?
S What is complexity?
input
State
control
a
a
b
a
-
-
-
A Turing Machine is a theoretical computer consisting of a tape
of infinite length and a read-write head which can move left and
right across the tape.
Informal definition
S TMs uses an infinite memory (tape)
S Tape initially contain input string followed by blanks
S When started, a TM executes a series of discrete transitions,
as determined by its transition function and by the initial
characters on the tape
Informal definition
S For each transition, the machine checks
S what state it is in and what character is written on the tape
below the head.
S Based on those, it then changes to a new state, writes a new
character on the tape, and moves the head one space left or
right.
S The machine stops after transferring to the special HALT
(accept or reject) state.
S If TM doesn’t enter a HALT state, it will go on forever
Differences between FA and
TMs
S A TM can both write on the tape and read from it
S The read-write head can move both to the left and to the
right
S The tape is infinite
S The special states of rejecting and accepting take immediate
effect
Examples
aa,R
ba,R
aa,R means TM reads the symbol a, it replaces it with a
and moves head to right
This TM when reading an a will move right one square stays in the
same start state. When it scans a b, it will change this symbol to an
a and go into the other state (accept state).
Examples
S A TM (M1) that tests for memberships in the language
S B= {w#w | w belongs to {0,1}*}
S We want to design M1 to accept if its input is a member of
B.
S Idea:
S Zig-zag across tape, crossing off matching symbols
Machine M1 Design
S M1 makes multiple passes over the input string with the
read-write head.
S On each pass it matches one of the characters on each side
of the # symbol.
S To keep track of which symbols have been checked already,
M1 crosses off each symbol as it is examined.
Machine M1 Design
S If it crosses off all the symbols, that means that everything
matched successfully, and M1 goes into an accept state.
S If it discovers a mismatch, it enters a reject state.
M1: Algorithm
Examples: M1
a b b # a b b
X b b # a b b
X
b b b # a b b
S Tape head starts over leftmost symbol
S Record symbol in control and overwrite X
S Scan right: reject if blank encountered before #
S When # encountered, move right one space
S If symbols don’t match, reject
Examples
X
b b b # X b b
X X b # X b b
S Overwrite X
S Scan left, past # to X
S Move one space right
S Record symbol and overwrite X
S When # encountered, move right one space
S If symbols don’t match, reject
X
b X b # X b b
Examples
X
b X X # X X X
S Finally scan left
S If a or b encountered, reject
S When blank encountered, accept
Formal Definition
S A Turing Machine is a 7-tuple (Q,∑,T, ξ,q0,qaccept, qreject),
where
S Q is a finite set of states
S ∑ is a finite set of symbols called the alphabet
S T is the tape alphabet, where – belongs to T, and ∑⊆ T
S ξ : Q x T  Q x T x {L,R} is the transition function
S q0 Є Q is the start state
qaccept ⊆ Q is the accept state
S qreject ⊆ Q is the reject state, where qaccept ≠ qreject
S
Transition function
S ξ : Q x T  Q x T x {L,R} is the transition function
ξ(q,a) = (r,b,L) means
in state q where head reads tape symbol a
the machine overwrites a with b
enters state r
move the head left
Workings of a TM
S M = (Q,∑,T, ξ,q0,qaccept, qreject) computes as follows
S Input w=w1w2…wn is on leftmost n tape squares
S Rest of tape is blank –
S Head is on leftmost square of tape
Workings of a TM
S M = (Q,∑,T, ξ,q0,qaccept, qaccept)
S When computation starts
S M Proceeds according to transition function ξ
S If M tries to move head beyond left-hand-end of tape, it doesn’t
move
S Computation continues until qaccept or qaccept is reached
S Otherwise M runs forever
Configurations
S Computation changes
S Current state
S Current head position
S Tape contents
Configurations
S Configuration
S 1011r0111
S Means
S State is r
S Left-Hand-Side (LHS) is 1011
S Right-Hand-Side (RHS) is 0111
S Head is on RHS 0
Configurations
S uarbv yields upacv if
S ξ(r,b) = (p,c,L)
S uarbv yields uacpv if
S ξ(r,b) = (p,c,R)
S Special cases: rbv yields pcv if
S ξ(r,b) = (p,c,L)
S The head moves (tries) to the left, so it only overwrites if
it is in the extreme left end.
S wr is the same as wr– (right hand end)
More configurations
S We have
S starting configuration q0w
S
Indicates machine is in a start state q0 with its head at the leftmost position.
S accepting configuration w0qacceptw1
S rejecting configuration w0qrejectw1
S halting configurations
S w0qacceptw1
S w0qrejectw1
Observations
S The accept and reject configurations do not derive in further
configurations (or states).
Accepting a language
S TM M accepts input w if a sequence of configurations
C1,C2,…,Ck exist
S C1 is start configuration of M on w
S Each Ci yields Ci+1
S Ck is an accepting configuration
TM Language
S The collection of strings that M accepts is the language of
M, denoted by L(M).
Detailed example
Detailed example
Detailed Example
S Do the following test runs:
S q10.
S q100.
S q1000.
S q10000.
Detailed Example
S Sample run for q10000.
Software:
S JFLAP (http://jflap.org)
S DEMO:
Exercise 2:
S B= {w#w | w belongs to {0,1}*}
Exercise 2
S Design the state transition diagram based on the previous
informal definition of the turing machine M3.
S Test the diagram with 001101#001101
S Transfer the diagram to JFLAP
State Diagram
Enumerable languages
S Definition: A language is (recursively) enumerable if some
TM accepts it
S In some other textbooks Turing-recognizable
Enumerable languages
S On an input to a TM we may
S accept
S reject
S loop (run for ever)
S Not very practical: never know if TM will halt
Enumerable languages
S Definition: A TM decides a language if it always halts in an
accept or reject state. Such a TM is called a decider
S A decider that recognizes some language is said to decide
that language.
S Definition: A language is decidable if someTM decides it
S Some textbooks use recursive instead of decidable
S Therefore, every decidable language is enumerable, but not
the reverse!
Example of decidable language
S Here is a decidable language
S L={aibjck | ix j = k, I,j,k > 0}
S Because there exist a TM that decides it
S How?
Example of decidable language
S
How?
Scan from left to right to check that input is of the form a*b*c*
Return to the start
Cross off an a and
scan right till you see a b
Mark each b and scan right till you see a c
Cross off that c
Move left to the next b, and repeat previous two steps until all b’s are
marked
8. Unmark all b’s and go to the start of the tape
9. Goto step 1 until all a’s are crossed off
10. Check if all c’s are crossed off; if so accept; otherwise reject
1.
2.
3.
4.
5.
6.
7.
TM: Variants
S Turing Machine head stays put
S Does not add any power
S Three possible states for the tape {L,R,S}
S Nondetermisim added to TMs
S Does not add any power
S Halting states may produce transitions.
Remarks
S Many models have been proposed for general-purpose
computation
S Remarkably all “reasonable” models are equivalent to
Turing Machines
S All ”reasonable” programming languages like C, C++, Java,
Prolog, etc are equivalent
S The notion of an algorithm is model-independent!