CSCI 3130: Formal languages and automata theory

39
CSCI 3130: Formal languages and automata theory Andrej Bogdanov http://www.cse.cuhk.edu.hk/ ~andrejb/csc3130 The Chinese University of Hong Kong Turing Machines Fall 2011

description

Fall 2011. The Chinese University of Hong Kong. CSCI 3130: Formal languages and automata theory. Turing Machines. Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130. What is a computer?. computer. program. output. input. - PowerPoint PPT Presentation

Transcript of CSCI 3130: Formal languages and automata theory

Page 1: CSCI 3130: Formal languages and automata theory

CSCI 3130: Formal languages and automata theory

Andrej Bogdanov

http://www.cse.cuhk.edu.hk/~andrejb/csc3130

The Chinese University of Hong Kong

Turing Machines

Fall 2011

Page 2: CSCI 3130: Formal languages and automata theory

What is a computer?

A computer is a machine that manipulates data according to a list of instructions.

computerinput

programoutput

Page 3: CSCI 3130: Formal languages and automata theory

What is a computer?

world

void hello( String name){ print( “Hello, “, name);} Hello, world

E2-E4

computer

deep blue

?

?

?

Page 4: CSCI 3130: Formal languages and automata theory

Turing Machines

…a b b ☐ ☐

input blanks

controlhead

Can both read from and write to the tape

Head can move both left and right

Tape is infinite

Has two special states accept and reject

Page 5: CSCI 3130: Formal languages and automata theory

Example

L1 = {w#w: w ∈{a, b}*}

Strategy:

Read and remember the first symbol

Cross it off (x)

Read the first symbol past #

If they don’t match, reject

abbaa#abbaa

xbbaa#abbaa

xbbaa#abbaa

If they do, cross it off xbbaa#xbbaa

Page 6: CSCI 3130: Formal languages and automata theory

Example

L1 = {w#w: w ∈ {a, b}*}

Strategy:

Look for the first uncrossed symbol

Cross it off (x)

Read the first uncrossed symbol past #

xbbaa#xbbaa

xxbaa#xbbaa

xxbaa#xbbaa

If they match, cross it off, else reject xxbaa#xxbaa

At the end, there should be only xs and #sxxxxx#xxxxx

If not, reject; otherwise, accept.

Page 7: CSCI 3130: Formal languages and automata theory

How Turing Machines operate

…a b a ☐q1 q2

a/bL

Replace a with b, and move head left

current state

…a b b ☐q1 q2

a/bL

new state

Page 8: CSCI 3130: Formal languages and automata theory

Formal Definition

A Turing Machine is (Q, , , , q0, qacc, qrej):– Q is a finite set of states; is the input alphabet not containing the blank

symbol ☐ is the tape alphabet ( ) including ☐

– q0 in Q is the start state;

– qacc, qrej in Q are the accepting and rejecting state is the transition function

: (Q – {qacc, qrej}) → Q {L, R}

Turing Machines are deterministic

Page 9: CSCI 3130: Formal languages and automata theory

Configurations

• A configuration consists of the current state, the head position, and tape contents

…a b a ☐q1 abq1a

q1 qacca/bR

abbqacc…a b b ☐

qacc

configuration

Page 10: CSCI 3130: Formal languages and automata theory

Configurations

• We say configuration C yields C ’ if the TM can go from C to C ’ in one step

• The start configuration of the TM on input w is q0w

• An accepting configuration is one that contains qacc;A rejecting configuration is one that contains qrej

abq1a abbqaccyields

Page 11: CSCI 3130: Formal languages and automata theory

The language of a Turing Machine

• We say M accepts x if there exists a sequence of configurations C0, C1, ..., Ck where

C0 is starting Ci yields Ci+1Ck is accepting

The language recognized by M is the set of all strings that M accepts

Page 12: CSCI 3130: Formal languages and automata theory

Looping

• Something strange can happen in a Turing Machine:

• Inputs can be divided into three types

q0

qacc

qrej

= {0, 1}

input: ☐/☐R

0/0R

1/1RThis machinenever halts

qacc qrej

accept reject loop

Page 13: CSCI 3130: Formal languages and automata theory

Halting

• We say M halts on x if there exists a sequence of configurations C0, C1, ..., Ck where

C0 is starting Ci yields Ci+1

Ck is acceptingor rejecting

A TM M is a decider if it halts on every inputLanguage L is decidable if it is recognized by a TM that halts on every input

Page 14: CSCI 3130: Formal languages and automata theory

Programming Turing Machines

L1 = {w#w: w ∈ {a, b}*}Description of Turing Machine:

Until you reach #

Read and remember entry

Move right past # and past all xsIf this entry is different, reject

Otherwise

Move left past # and to right of first x

If you see only xs followed by ☐, accept

xbbaa#xbbaa

xxbaa#xxbaa

Write x xxbaa#xbbaaxxbaa#xbbaa

Write x xxbaa#xxbaa

1

2

3

4

5

6

7

8

Page 15: CSCI 3130: Formal languages and automata theory

Programming Turing Machines

L1 = {w#w: w ∈ {a, b}*}

q0

qa1 qa2

q2

qb1 qb2

q3 qrejq1 qacc

a/xR

#/#R

#/#R

b/xL

b/xR

a/xL

a/aRb/bR

a/aRb/bR

x/xR

x/xR

x/xR

a/aLb/bLx/xL

☐/☐R#/#R #/#L

a/aLb/bL

x/xR

everything else

1

23

23

4

4

56

56

78

Page 16: CSCI 3130: Formal languages and automata theory

Programming Turing Machines

q0

qa1 qa2

q2

qb1 qb2

q3q1 qacc

a/xR

#/#R

#/#R

b/xL

b/xR

a/xL

a/aRb/bR

a/aRb/bR

x/xR

x/xR

x/xR

a/aLb/bLx/xL

☐/☐R#/#R #/#L

a/aLb/bL

x/xR

1

23

23

4

4

56

56

78

input: aab#aab

configurations:

q0aab#aabxqa1ab#aabxaqa1b#aabxabqa1#aabxab#qa2aabxabq2#xabxaq3b#xabxq3ab#xabq3xab#xabxq0ab#xab

Page 17: CSCI 3130: Formal languages and automata theory

Programming Turing Machines

L2 = {aibjck: i j = k and i, j, k > 0 }

High-level description of TM:

For every a:

Cross off the same number of bs and cs

Uncross the crossed bs (but not the cs)

Cross off this a

If all as and cs are crossed off, accept.

aabbcccc

aabbcccc

= {a, b, c}

Page 18: CSCI 3130: Formal languages and automata theory

Programming Turing Machines

L2 = {aibjck: i j = k and i, j, k > 0 }

Low-level description of TM:

Scan input from left to right to check it looks like aa*bb*cc*

Move the head to the first symbol of the tape

For every a:

Cross off the same number of bs and cs

Restore the crossed of bs (but not the cs)

Cross off this a

If all as and cs are crossed off, accept.

how do we know?

how to do this?

Page 19: CSCI 3130: Formal languages and automata theory

Programming Turing Machines

Implementation details:

Cross off the same number of bs and cs:

Move right until you see a c

If any bs are left, repeat

Put a special marker on top of first a

Move the head to the first symbol of the tape

aabbcccc

aabbcccc

aaqbbcccc

= {a, b, c}

Page 20: CSCI 3130: Formal languages and automata theory

Programming Turing Machines

L3 = {#x1#x2...#xl : xi ∈ {0, 1}* and xi ≠ xj for each i ≠ j}

#01#0011#1High-level description of TM:

On input w,

For every pair of blocks xi and xj in w,

Compare the blocks xi and xjIf they are the same, reject.Otherwise, accept.

Page 21: CSCI 3130: Formal languages and automata theory

Programming Turing Machines

L3 = {#x1#x2...#xl : xi ∈ {0, 1}* and xi ≠ xj for each i ≠ j}

#01#0011#1Low-level description:

Place a mark on the leftmost #(i.e. replace # by #) and move right

1.

0.If input is , or has exactly one #, accept.

#01#0011#1

Place another mark on next unmarked #(If there are no more #, accept)2. #01#0011#1

Page 22: CSCI 3130: Formal languages and automata theory

Programming Turing Machines

L3 = {#x1#x2...#xl : xi ∈ {0, 1}* and xi ≠ xj for each i ≠ j}

current state:

Repeat Step 35.

#01#0011#1

#01#0011#1

#01#0011#1

#01#0011#1

Compare the two strings to the rightof marked #. If there are same, reject3.

4.Move the right # to the right If not possible, move the left # to the next # and put the right # on the next If not possible, accept

Page 23: CSCI 3130: Formal languages and automata theory

Programming Turing Machines

L3 = {#x1#x2...#xl : xi ∈ {0, 1}* and xi ≠ xj for each i ≠ j}

#01#0011#1

#01#0011#1

accept4.Move the right # to the right If not possible, move the left # to the next # and put the right # on the next If not possible, accept

Compare the two strings to the rightof marked #. If there are same, reject3.

Page 24: CSCI 3130: Formal languages and automata theory

How to describe Turing Machines?

• Unlike for DFAs, NFAs, PDAs, we rarely give complete state diagrams of Turing Machines

• Usually we give a high-level description:A recipe about the workings of the Turing Machine

Just like in cooking, practice makes perfect!

Page 25: CSCI 3130: Formal languages and automata theory

Programming Turing Machines

L4 = { 〈 G 〉 : G is a connected undirected graph}

Q: How do we feed a graph into a Turing Machine?

A: We represent it by a string, e.g.

1 2

3 4

(1,2,3,4)((1,4),(2,3),(3,4)(4,2))

Convention for describing graphs:

(nodes)(edges)

no node must repeatedges are pairs (node1,node2)

Page 26: CSCI 3130: Formal languages and automata theory

Programming Turing Machines

L4 = { 〈 G 〉 : G is a connected undirected graph}

On input 〈 G 〉 ,

0.Verify that 〈 G 〉 is the description of a graph (no vertex repeats; edges only go between nodes)

1.Mark the first node of G

2.Repeat until no new nodes are marked:

For each node, mark it if it is attached to an already marked node

3.If all nodes are marked accept, otherwise reject.

1 2

3 4

x

x

x

x

Page 27: CSCI 3130: Formal languages and automata theory

Programming Turing Machines

L4 = { 〈 G 〉 : G is a connected undirected graph}

(1,2,3,4)((1,4)(2,3)(3,4)(4,2))

(1,2,3,4)((1,4)(2,3)(3,4)(4,2))

(1,2,3,4)((1,4)(2,3)(3,4)(4,2))

(1,2,3,4)((1,4)(2,3)(3,4)(4,2))

(1,2,3,4)((1,4)(2,3)(3,4)(4,2))

(1,2,3,4)((1,4)(2,3)(3,4)(4,2))

(1,2,3,4)((1,4)(2,3)(3,4)(4,2))

(1,2,3,4)((1,4)(2,3)(3,4)(4,2)) etc.

1 2

3 4

(1,2,3,4)((1,4)(2,3)(3,4)(4,2))

(1,2,3,4)((1,4)(2,3)(3,4)(4,2))

Page 28: CSCI 3130: Formal languages and automata theory

What’s so great aboutTuring Machines?

Page 29: CSCI 3130: Formal languages and automata theory

Hilbert’s list of 23 problems

• Leading mathematician in the 1900s

• At the 1900 International Congressof Mathematicians, proposed a list of23 problems for the 20th century

• Hilbert’s 10th problem:David Hilbert

(1862-1943)

Find a method to tell if an equation like xyz – 3xy + 6xz + 2 = 0 has integer solutions

Page 30: CSCI 3130: Formal languages and automata theory

A brief history of computing devices

Z3 (Germany, 1941) ENIAC (Pennsylvania, 1945)

PC (1980) MacBook Air (2008)

Page 31: CSCI 3130: Formal languages and automata theory

Computation is universal

In principle, all thesehave the same problem-solving ability

Page 32: CSCI 3130: Formal languages and automata theory

The Church-Turing Thesis

Turing Machine

If an algorithm can be implemented on any realistic computer, then it can be implemented on a Turing Machine.

Page 33: CSCI 3130: Formal languages and automata theory

The Church-Turing Thesis

Turing Machinequantum computing

DNA computing

cosmic computing

Page 34: CSCI 3130: Formal languages and automata theory

Alan Turing

• Invented the Turing Test to tellhumans and computers apart

• Broke German encryption machines in World War II

• The Turing Award is the “Nobel prize of Computer Science”

Alan Turing (1912-1954)

Turing’s motivation: By studying Turing Machines, we can understand the limitations of real computers.

Page 35: CSCI 3130: Formal languages and automata theory

Undecidability

• What Hilbert meant to ask was:

• Building on work of Gödel, Church, Turing, Davis, Putnam, Robinson, in 1970 Matijasievič showed:

Find a Turing Machine to tell if an equation like xyz – 3xy + 6xz + 2 = 0 has integer solutions

There is no such Turing Machine!

Page 36: CSCI 3130: Formal languages and automata theory

Computer program analysis

public static void main(String args[]) { System.out.println("Hello World!"); }

What does this program do?

How about this one?

public static void main(String args[]) { int i = 0; for (j = 1; j < 10; j++) { i += j; if (i == 28) { System.out.println("Hello World!"); } } }

Page 37: CSCI 3130: Formal languages and automata theory

Computers cannot analyze programs!

No Turing Machine can do this:

input: The code of a java program P

Accept if P prints “hello, world”

Reject if not

Significance: It is impossible for computer to predict what a computer program will do!

Page 38: CSCI 3130: Formal languages and automata theory

How do you argue things like that?

To argue what computers cannot do,we need to have a precise definitionof what a computer is.

“On Computable Numbers, with an Application to the Entscheidungsproblem”

1936:

Section 1. Computing Machines

Turing’s answer: A computer is just a Turing Machine.

Page 39: CSCI 3130: Formal languages and automata theory

The Church-Turing Thesis

“On Computable Numbers, with an Application to the Entscheidungsproblem”

1936:

Section 9. The extent of the computable numbers

All arguments [for the CT Thesis] which can be given are bound to be, fundamentally, appeals to intuition, and for this reason rather unsatisfactory mathematically. The arguments which I shall use are of three kinds: 1. A direct appeal to intuition 2. A proof of the equivalence of two definitions (In case the new definition has greater intuitive appeal) 3. Giving examples of large classes of numbers which are computable.