+ All Categories
Home > Documents > Fall 2004COMP 3351 Turing Machines. Fall 2004COMP 3352 The Language Hierarchy Regular Languages...

Fall 2004COMP 3351 Turing Machines. Fall 2004COMP 3352 The Language Hierarchy Regular Languages...

Date post: 20-Dec-2015
Category:
View: 226 times
Download: 2 times
Share this document with a friend
99
Fall 2004 COMP 335 1 Turing Machines
Transcript

Fall 2004 COMP 335 1

Turing Machines

Fall 2004 COMP 335 2

The Language Hierarchy

*aRegular Languages

Context-Free Languagesnnba Rww

nnn cba ww?

**ba

?

Fall 2004 COMP 335 3

*aRegular Languages

Context-Free Languagesnnba Rww

nnn cba ww

**ba

Languages accepted byTuring Machines

Fall 2004 COMP 335 4

A Turing Machine

............Tape

Read-Write head

Control Unit

Fall 2004 COMP 335 5

The Tape

............

Read-Write head

No boundaries -- infinite length

The head moves Left or Right

Fall 2004 COMP 335 6

............

Read-Write head

The head at each time step:

1. Reads a symbol 2. Writes a symbol 3. Moves Left or Right

Fall 2004 COMP 335 7

............

Example:Time 0

............Time 1

1. Reads

2. Writes

a a cb

a b k c

a

k3. Moves Left

Fall 2004 COMP 335 8

............Time 1

a b k c

............Time 2

a k cf

1. Reads

2. Writes

bf

3. Moves Right

Fall 2004 COMP 335 9

The Input String

............

Blank symbol

head

a b ca

Head starts at the leftmost positionof the input string

Input string

Fall 2004 COMP 335 10

............

Blank symbol

head

a b ca

Input string

Remark: the input string is never empty

Fall 2004 COMP 335 11

States & Transitions

1q 2qLba ,

Read Write Move Left

1q 2qRba ,

Move Right

Fall 2004 COMP 335 12

Example:

1q 2qRba ,

............ a b ca

Time 1

1qcurrent state

Fall 2004 COMP 335 13

............ a b caTime 1

1q 2qRba ,

............ a b cbTime 2

1q

2q

Fall 2004 COMP 335 14

............ a b caTime 1

1q 2qLba ,

............ a b cbTime 2

1q

2q

Example:

Fall 2004 COMP 335 15

............ a b caTime 1

1q 2qRg,

............ ga b cbTime 2

1q

2q

Example:

Fall 2004 COMP 335 16

Determinism

1q

2qRba ,

Allowed Not Allowed

3qLdb ,

1q

2qRba ,

3qLda ,

No lambda transitions allowed

Turing Machines are deterministic

Fall 2004 COMP 335 17

Partial Transition Function

1q

2qRba ,

3qLdb ,

............ a b ca

1q

Example:

No transitionfor input symbol c

Allowed:

Fall 2004 COMP 335 18

Halting

The machine halts if there areno possible transitions to follow

Fall 2004 COMP 335 19

Example:

............ a b ca

1q

1q

2qRba ,

3qLdb ,

No possible transition

HALT!!!

Fall 2004 COMP 335 20

Final States

1q 2q Allowed

1q 2q Not Allowed

• Final states have no outgoing transitions

• In a final state the machine halts

Fall 2004 COMP 335 21

Acceptance

Accept Input If machine halts in a final state

Reject Input

If machine halts in a non-final state or If machine enters an infinite loop

Fall 2004 COMP 335 22

Turing Machine Example

A Turing machine that accepts the language:

*aa

0q

Raa ,

L,1q

Fall 2004 COMP 335 23

aaTime 0

0q

a

0q

Raa ,

L,1q

Fall 2004 COMP 335 24

aaTime 1

0q

a

0q

Raa ,

L,1q

Fall 2004 COMP 335 25

aaTime 2

0q

a

0q

Raa ,

L,1q

Fall 2004 COMP 335 26

aaTime 3

0q

a

0q

Raa ,

L,1q

Fall 2004 COMP 335 27

aaTime 4

1q

a

0q

Raa ,

L,1q

Halt & Accept

Fall 2004 COMP 335 28

Rejection Example

0q

Raa ,

L,1q

baTime 0

0q

a

Fall 2004 COMP 335 29

0q

Raa ,

L,1q

baTime 1

0q

a

No possible Transition

Halt & Reject

Fall 2004 COMP 335 30

Infinite Loop Example

0q

Raa ,

L,1q

Lbb ,

Fall 2004 COMP 335 31

baTime 0

0q

a

0q

Raa ,

L,1q

Lbb ,

Fall 2004 COMP 335 32

baTime 1

0q

a

0q

Raa ,

L,1q

Lbb ,

Fall 2004 COMP 335 33

baTime 2

0q

a

0q

Raa ,

L,1q

Lbb ,

Fall 2004 COMP 335 34

baTime 2

0q

a

baTime 3

0q

a

baTime 4

0q

a

baTime 5

0q

a

Infinite

loop

Fall 2004 COMP 335 35

Because of the infinite loop:

•The final state cannot be reached

•The machine never halts

•The input is not accepted

Fall 2004 COMP 335 36

Another Turing Machine Example

Turing machine for the language }1:{ nba nn

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

Fall 2004 COMP 335 37

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

ba

0q

a bTime 0

Fall 2004 COMP 335 38

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

bx

1q

a b Time 1

Fall 2004 COMP 335 39

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

bx

1q

a b Time 2

Fall 2004 COMP 335 40

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

yx

2q

a b Time 3

Fall 2004 COMP 335 41

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

yx

2q

a b Time 4

Fall 2004 COMP 335 42

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

yx

0q

a b Time 5

Fall 2004 COMP 335 43

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

yx

1q

x b Time 6

Fall 2004 COMP 335 44

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

yx

1q

x b Time 7

Fall 2004 COMP 335 45

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

yx x y

2q

Time 8

Fall 2004 COMP 335 46

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

yx x y

2q

Time 9

Fall 2004 COMP 335 47

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

yx

0q

x y Time 10

Fall 2004 COMP 335 48

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

yx

3q

x y Time 11

Fall 2004 COMP 335 49

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

yx

3q

x y Time 12

Fall 2004 COMP 335 50

0q 1q 2q3qRxa ,

Raa ,Ryy ,

Lyb ,

Laa ,Lyy ,

Rxx ,

Ryy ,

Ryy ,4q

L,

yx

4q

x y

Halt & Accept

Time 13

Fall 2004 COMP 335 51

We easily modify the machine for the language }1:{ nba nn

to construct a TM for the language }1:{ ncba nnn

Observation:

Fall 2004 COMP 335 52

Formal Definitionsfor

Turing Machines

Fall 2004 COMP 335 53

Transition Function

1q 2qRba ,

),,(),( 21 Rbqaq

Fall 2004 COMP 335 54

1q 2qLdc ,

),,(),( 21 Ldqcq

Transition Function

Fall 2004 COMP 335 55

Turing Machine:

),,,,,,( 0 FqQM

States

Inputalphabet

Tapealphabet

Transitionfunction

Initialstate

blank

Finalstates

Fall 2004 COMP 335 56

Configuration

ba

1q

a

Instantaneous description:

c

baqca 1

Fall 2004 COMP 335 57

yx

2q

a b

Time 4

yx

0q

a b

Time 5

A Move: aybqxxaybq 02

Fall 2004 COMP 335 58

yx

2q

a b

Time 4

yx

0q

a b

Time 5

bqxxyybqxxaybqxxaybq 1102

yx

1q

x b

Time 6

yx

1q

x b

Time 7

Fall 2004 COMP 335 59

bqxxyybqxxaybqxxaybq 1102

bqxxyxaybq 12

Equivalent notation:

Fall 2004 COMP 335 60

Initial configuration: wq0

ba

0q

a b

w

Input string

Fall 2004 COMP 335 61

The Accepted Language

For any Turing Machine M

}:{)( 210 xqxwqwML f

Initial state Final state

Fall 2004 COMP 335 62

Standard Turing Machine

• Deterministic

• Infinite tape in both directions

•Tape is the input/output file

The machine we described is the standard:

Fall 2004 COMP 335 63

Computing Functionswith

Turing Machines

Fall 2004 COMP 335 64

A function )(wf

Domain: Result Region:

has:

D

Dw

S

Swf )()(wf

Fall 2004 COMP 335 65

A function may have several parameters / arguments:

yxyxf ),(

Example: The Addition function

Fall 2004 COMP 335 66

Integer Domain

Unary:

Binary:

Decimal:

11111

101

5

We prefer unary representation:

easier to manipulate with Turing machines

Fall 2004 COMP 335 67

Definition:

A function is computable ifthere is a Turing Machine such that:

fM

Initial configuration Final configuration

Dw Domain

0q

w

fq

)(wf

final stateinitial state

For all

Fall 2004 COMP 335 68

)(0 wfqwq f

Initial Configuratio

n

FinalConfiguration

A function is computable ifthere is a Turing Machine such that:

fM

In other words:

Dw (the domain)

Fall 2004 COMP 335 69

Example

The function yxyxf ),( is computable

A Turing Machine M for f:

Input string: yx0 unary

Output string: 0xy unary

yx, are integers

Fall 2004 COMP 335 70

0

0q

1 1 1 1

x y

1 Start

initial state

The 0 is the delimiter that separates the two numbers

Fall 2004 COMP 335 71

0

0q

1 1 1 1

x y

1

0

fq

1 1

yx

11

Start

Finish

final state

initial state

Fall 2004 COMP 335 72

0

fq

1 1

yx

11Finish

final state

The 0 helps when we usethe result for other operations

Fall 2004 COMP 335 73

0q

Turing machine M for the function:

1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

yxyxf ),(

Fall 2004 COMP 335 74

Execution Example:

11x

11y 0

0q

1 1 1 1

Input (time 0)

x y

Output (final result)

0

4q

1 1 1 1

yx

(2)

(2)

Fall 2004 COMP 335 75

0

0q

1 1Time 0

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

1 1

Fall 2004 COMP 335 76

0

0q

1 1Time 1

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

1 1

Fall 2004 COMP 335 77

0q

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

01 11 1Time 2

Fall 2004 COMP 335 78

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

0

0q

1 1 1 1Time 3

Fall 2004 COMP 335 79

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

1q

1 11 11Time 4

Fall 2004 COMP 335 80

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

1q

1 1 1 11Time 5

Fall 2004 COMP 335 81

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

1q

1 11 11Time 6

Fall 2004 COMP 335 82

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

2q

1 1 1 11Time 7

Fall 2004 COMP 335 83

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

3q

1 11 01Time 8

Fall 2004 COMP 335 84

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

3q

1 1 1 01Time 9

Fall 2004 COMP 335 85

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

3q

1 11 01Time 10

Fall 2004 COMP 335 86

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

3q

1 1 1 01Time 11

Fall 2004 COMP 335 87

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

3q

1 11 01Time 12

Fall 2004 COMP 335 88

0q 1q 2q 3qL, L,01

L,11

R,

R,10

R,11

4q

R,11

4q

1 1 1 01

HALT & accept

Time 13

Fall 2004 COMP 335 89

Another Example

The function xxf 2)( is computable

Turing Machine:

Input string: x unary

Output string: xx unary

x is integer

Fall 2004 COMP 335 90

0q

1 1

x

1

1

fq

1 1

x2

11

Start

Finish

final state

initial state

Fall 2004 COMP 335 91

Turing Machine Pseudocode for xxf 2)(

• Replace every 1 with $

• Repeat:

• Find rightmost $, replace it with 1

• Go to right end, then insert 1

Until no more $ remains

Fall 2004 COMP 335 92

0q 1q 2q

3q

R,1$

L,1

L,

R$,1 L,11 R,11

R,

A Turing Machine for: xxf 2)(

Fall 2004 COMP 335 93

0q 1q 2q

3q

R,1$

L,1

L,

R$,1 L,11 R,11

R,

Example

0q

1 1

3q

1 11 1

Start Finish

Fall 2004 COMP 335 94

Another Example

The function ),( yxf

is computable0

1 yx

yx

if

if

Fall 2004 COMP 335 95

A Turing Machine for

Input: yx0

Output: 1 0or

),( yxf0

1 yx

yx

if

if

Fall 2004 COMP 335 96

Pseudocode for our Turing Machine:

Match a 1 from with a 1 from x y

• Repeat

Until all of or is matchedx y

• If a 1 from is not matched erase tape, then write 1 else erase tape, then write 0

x)( yx

)( yx

Fall 2004 COMP 335 97

Combining Turing Machines

Fall 2004 COMP 335 98

Block Diagram

TuringMachine

input output

Fall 2004 COMP 335 99

Example:

),( yxf0

yx yx

yx

if

if

Comparer

Adder

Eraser

yx,

yx,

yx

yx

yx

0


Recommended