Turing Machine - E-STUDy...•Finite Automata –good for devices with small amounts of memory,...

55
Turing Machine UNIT V Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Transcript of Turing Machine - E-STUDy...•Finite Automata –good for devices with small amounts of memory,...

Turing Machine

UNIT V

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Devices of Increasing Computational Power

• So far:• Finite Automata – good for devices with small amounts of memory,

relatively simple control

• Pushdown Automata – stack-based automata

• Limitations:1. Amount of Memory(Limited)

2. The way of accessing the memory (PDA can access only top of thestack )

3. Input Tape (Finite)

4. Tape head is read only.

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Who was Alan Turing?

• Founder of computer science, mathematician, philosopher, code breaker.

• Introduced TM in 1936!

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Turing machine !

- models a “human computer”

(human writes/rewrites symbols on a sheet of paper, the human’s state of mindchanges based on what s/he has seen)

- The Turing machine is a mathematical model not ofcomputers, but of computation.

• His computer model –the Turing Machine– wasinspiration of the electronic computer that cametwo decades later

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Turing Machines

• TM’s described in 1936 by Alen Turing

• Abstract model for today’s computers

Components of TM :

• Finite Control : A TM consists of a finite control (i.e. a finitestate automaton).

• Infinite tape : FC is connected to an infinite tape.The tapeconsists of cells where each cell holds a symbol from the tapealphabet. Initially each cell has blanks.

• Tape Head : Reads one cell at a time and moves to L/ R / N

• States : Initial State , Final State

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

TM

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

States & Transitions

1q 2q

ReadWrite

Move Left

1q 2q

Move Right

a / b / L

a / b / R

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Turing Machine Details

• In one move the TM will:1. Change state, which may be the same as

the current state2. Write a tape symbol in the current cell,

which may be the same as the currentsymbol

3. Move the tape head left or right one cell4. The special states for rejecting and

accepting take effect immediately

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Formal definition of TM

• Formally, the TM is denoted by the 7-tuple: • M = (Q, , Γ, δ, q0, B, qa)

Q = finite states of the control

= finite set of input symbols, which is a subset of Γ below

Γ = finite set of tape symbols

δ = transition function. δ(q,X)=(p,Y,D)

p,q ϵ Q and X,Y ϵ Γ and D = ,L/R/N

q0= start state for finite control

B = blank symbol. This symbol is in Γ but not in .

qaccept = set of final or accepting states of Q.Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Turing Machine:

),,,,,,( 0 FBqQM

States

Input

alphabetTape

alphabet

Transition

function

Initial

stateblank

Final

states

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

q pX/Y/D

Transition function

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Instantaneous description

• processing of a string by TM can be shown usingthe instantaneous description.

• The ID of TM includes :1. All non-blank cells in the tape

2. Position of head.

3. Current state of machine

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Instantaneous Description (ID)

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

1q

2qRba ,

Allowed Not Allowed

3qLdb ,

1q

2qRba ,

3qLda ,

No empty move is allowed

Turing Machines are deterministic

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Halting and Acceptance

• The machine halts if there are no possible transitions to follow.

• A i/p string is accepted by TM if scanning of the string is completed from left to right and machine is in the final State.

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Transition Table

I/P

State

a B

q0 (q0,a,R) (q1,B,N)

q1 - -

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Handrun

• Let us trace “aa”

1. a a B

2. a a B

3. a a B

Accept

q0

q0

q1

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

TM for calculations

• TMs can also be used for calculating values • Like arithmetic computations

• Eg., addition, subtraction, multiplication, etc.

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Equivalence of TM’s and Computers

• In one sense, a real computer has a finite amountof memory, and thus is weaker than a TM.

• But, we can postulate an infinite supply of tapes,disks, or some peripheral storage device tosimulate an infinite TM tape. Additionally, we canassume there is a human operator to mount disks,keep them stacked neatly on the sides of thecomputer, etc.

• Need to show both directions, a TM can simulate acomputer and that a computer can simulate a TM

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Computer Simulate a TM

• This direction is fairly easy - Given a computer witha modern programming language, certainly, we canwrite a computer program that emulates the finitecontrol of the TM.

• The only issue remains the infinite tape. Ourprogram must map cells in the tape to storagelocations in a disk. When the disk becomes full, wemust be able to map to a different disk in the stackof disks mounted by the human operator.

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

TM Simulate a Computer

• In this exercise the simulation is performed at the level of stored instructions and accessing words of main memory.• TM has one tape that holds all the used memory locations and

their contents.

• Other TM tapes hold the instruction counter, memory address, computer input file, and scratch data.

• The computer’s instruction cycle is simulated by:

1. Find the word indicated by the instruction counter on the memory tape.

2. Examine the instruction code (a finite set of options), and get the contents of any memory words mentioned in the instruction, using the scratch tape.

3. Perform the instruction, changing any words' values as needed, and adding new address-value pairs to the memory tape, if needed.

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Turing Machine Variants

• There are many variations we can make to the basic TM• Extensions can make it useful to prove a theorem or

perform some task

• However, these extensions do not add anything extra the basic TM.

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

TM variants

1. Multitape TM

2. Multitrack TM

3. Non deterministic TM

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Multitape Turing Machines

• A multitape Turing machine has several tapesinstead of one tape and each tape with a separatehead.

• Each head can move independently of the others

• The transition function is changed to allow forreading, writing, and moving the heads on all thetapes simultaneously.• This means we could read on multiples tape and move

in different directions on each tape as well as write adifferent symbol on each tape, all in one move.

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Continue..

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Multitape Machine

… 0 1 0 1 0 B …

… a a a B …

… b a B …

M

Equivalent Single Tape Machine:

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

How a multitape TM operates?

• Initially: • The input is in tape #1 surrounded by blanks

• All other tapes contain only blanks

• The tape head for tape #1 points to the 1st symbol of the input

• The heads for all other tapes point at an arbitrary cell (doesn’t matter because they are all blanks anyway)

• A move: • Is a function (current state, the symbols pointed by all the

heads)

• After each move, each tape head can move independently (left or right) of one another

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Multitrack TM

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

continue…

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Deterministic and Nondeterministic TM

• Deterministic : when the transition rules arerepresented as five-tuples (s, x, s’, x’, d),where s, x are the current state and tape symbol, s’,x’ are the next state and tape symbol, and d is themove direction, then no two transition rules beginwith the same pair (s, x), i.e., the mapping(s,x) → (s’, x’, d) is a function.

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

NTM

• In a nondeterministic Turing machine there maybe more that one transition rule beginning with the same pair eg (q, X).

• A non-Deterministic Turing Machine allows morethan one possible action per given state-tapesymbol pair.

• A string w is accepted by TM if after being put onthe tape and letting TM run, TM eventually entersqacc on some computation branch.

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

A Universal Turing Machine

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

Turing Machines are “hardwired”

they execute

only one program

A limitation of Turing Machines:

Real Computers are re-programmable

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

Solution: Universal Turing Machine

A Universal Turing Machine is a TM that cansimulate the behavior of any other TuringMachine

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

Continue…• A universal Turing machine, a machine that can do

any computation if the appropriate program isprovided, was the first description of a moderncomputer. It can be proved that a very powerfulcomputer and a universal Turing machine cancompute the same thing. We need only providethe data and the program—the description ofhow to do the computation—to either machine.

• In fact, a universal Turing machine is capable ofcomputing anything that is computable.

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

Universal Turing Machine

simulates any Turing Machine M

Input of Universal Turing Machine:

Description of transitions of M

Input string of MPrepared by Prof. T.H.Gurav

,SKNCOE, Pune

Universal

Turing

MachineM

Description of

Tape Contents of

M

State of M

Three tapes

Tape 2

Tape 3

Tape 1

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

We describe Turing machine

as a string of symbols:

We encode as a string of symbols

M

M

Description of M

Tape 1

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

Alphabet Encoding

Symbols: a b c d

Encoding: 1 11 111 1111

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

State Encoding

States: 1q 2q 3q 4q

Encoding: 1 11 111 1111

Head Move Encoding

Move:

Encoding:

L R

1 11Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

Transition Encoding

Transition: ),,(),( 21 Lbqaq

Encoding: 10110110101

separator

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

Turing Machine Encoding

Transitions:

),,(),( 21 Lbqaq

Encoding:

10110110101

),,(),( 32 Rcqbq

110111011110101100

separatorPrepared by Prof. T.H.Gurav

,SKNCOE, Pune

Tape 1 contents of Universal Turing Machine:

binary encoding

of the simulated machine M

1100011101111010100110110110101

Tape 1

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

A Turing Machine is described with a binary string of 0’s and 1’s

The set of Turing machines forms a language:

each string of this language is the binaryencoding of a Turing Machine

Therefore:

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

Language of Turing Machines

L = { 010100101,

00100100101111,

111010011110010101,

…… }

(Turing Machine 1)

(Turing Machine 2)

……

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

Halting Problem

Given a program and an input to the program,determine if the program will eventually stopwhen it is given that input.

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

proof that the Halting Problem is unsolvable

This proof was devised by Alan Turing, 1936.

Suppose you have a solution to the halting problem called H. H takes two inputs: 1. a program P and

2. an input I for the program P.

H generates an output "halt" if H determines that P stops on input I or it outputs "loop" otherwise.

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

Continue..

So now H can be revised to take P as both inputs (the

program and its input) and H should be able to determine if

P will halt on P as its input.

Let us construct a new, simple algorithm K that takes H's output as its input and does the following :1. if H outputs "loop" then K halts,

2. otherwise H's output of "halt" causes K to loop forever.

That is, K will do the opposite of H's output.

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

Continue..function K() {

if (H()=="loop"){

return;

} else {

while(true); //loop forever

}

}

Since K is a program, let us use K as the input to H.

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

Continue..

• If H says that K halts then K itself would loop (that's how

we constructed it).

• If H says that K loops then K will halt.

In either case H gives the wrong answer for K. Thus H

cannot work in all cases.

We've shown that it is possible to construct an input that

causes any solution H to fail.

Prepared by Prof. T.H.Gurav

,SKNCOE, Pune

Turing Languages

• Three classes of languages:• Turing-decidable or recursive: TM can accept the strings

in the language and tell if a string is not in the language.Sometimes these are called decidable problems.

• Turing-recognizable or recursively enumerable : TM canaccept the strings in the language but cannot tell forcertain that a string is not in the language. Sometimesthese are called partially-decidable.

• Undecidable : no TM can even recognize ALL membersof the language.

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Recursively Enumerable Languages

• A TM accepts a string w if the TM halts in a final state. ATM rejects a string w if the TM halts in a non final state orthe TM never halts.

• A language L is recursively enumerable if some TM acceptsit. Hence they are also called as Turing Acceptable L .

• Recursively Enumerable Languages are also calledRecognizable

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Recursive Language

• Recursive Language : A language L is recursive if some TMaccepts it and halts on every input.

• Recursive languages are also called Decidable Languagesbecause a Turing Machine can decide membership in thoselanguages (it can either accept or reject a string).

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Undecidable Languages

• undecidable language = not decidable language

• If there is no Turing Machine which accepts thelanguage and makes a decision (halts) for everyinput string.

• Note : (machine may make decision for some inputstrings)

• For an undecidable language, the correspondingproblem is undecidable (unsolvable):

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune

Comparison with Previous Models

DeviceSeparate Input?

Read/Write Data Structure

Deterministic by default?

FA Yes None Yes

PDA Yes LIFO Stack No

TM No1-way infinite tape. 1 cell

access per step.

Yes

(but will also allow crashes)

Prepared by Prof. T.H.Gurav ,SKNCOE, Pune