SlideShare a Scribd company logo
1 of 54
Download to read offline
http://introcs.cs.princeton.edu
R O B E R T S E D G E W I C K
K E V I N W A Y N E
Computer
Science
Computer
Science
An Interdisciplinary Approach
15. Turing Machines
C O M P U T E R S C I E N C E
S E D G E W I C K / W A Y N E
PART II: ALGORITHMS, MAC HINES, and THEORY
Section 7.4
15. Turing Machines
•Context
•A simple model of computation
•Universality
•Computability
•Implications
C O M P U T E R S C I E N C E
S E D G E W I C K / W A Y N E
PART II: ALGORITHMS, MAC HINES, and THEORY
CS.15.A.Turing.Context
Universality and computability
3
Fundamental questions
• What is a general-purpose computer?
• Are there limits on the power of digital computers?
• Are there limits on the power of machines we can build?
Pioneering work at Princeton in the 1930s.
David Hilbert
1862−1943
Asked the questions
Kurt Gödel
1906−1978
Solved the math
problem
Alonzo Church
1903−1995
Solved the decision
problem
Alan Turing
1912−1954
Provided THE answers
Context: Mathematics and logic
4
Mathematics. Any formal system powerful enough to express arithmetic.
Complete. Can prove truth or falsity of any arithmetic statement.
Consistent. Cannot prove contradictions like 2 + 2 = 5.
Decidable. An algorithm exists to determine truth of every statement.
Q. (Hilbert, 1900) Is mathematics complete and consistent?
A. (Gödel's Incompleteness Theorem, 1931) NO (!!!)
Q. (Hilbert's Entscheidungsproblem) Is mathematics decidable?
A. (Church 1936, Turing 1936) NO (!!)
Principia Mathematics
Peano arithmetic
Zermelo-Fraenkel set theory
.
.
.
C O M P U T E R S C I E N C E
S E D G E W I C K / W A Y N E
CS.15.A.Turing.Context
Image sources
http://en.wikipedia.org/wiki/David_Hilbert#/media/File:Hilbert.jpg
http://en.wikipedia.org/wiki/Kurt_Gödel#/media/File:Kurt_gödel.jpg
http://en.wikipedia.org/wiki/Alonzo_Church#/media/File:Alonzo_Church.jpg
http://en.wikipedia.org/wiki/Alan_Turing#/media/File:Alan_Turing_photo.jpg
15. Turing Machines
•Context
•A simple model of computation
•Universality
•Computability
•Implications
C O M P U T E R S C I E N C E
S E D G E W I C K / W A Y N E
PART II: ALGORITHMS, MAC HINES, and THEORY
CS.15.B.Turing.Machine
Starting point
7
Goals
• Develop a model of computation that encompasses all known computational processes.
• Make the model as simple as possible.
Characteristics
• Discrete.
• Local.
• States.
Example: A familiar computational process.
3 1 4 2
7 1 8 2
4
0
2
1
3
0
0
1
1
Previous lecture: DFAs
A DFA is an abstract machine that solves a pattern matching problem.
• A string is specified on an input tape (no limit on its length).
• The DFA reads each character on input tape once, moving left to right.
• The DFA lights "YES" if it recognizes the string, "NO" otherwise.
Each DFA defines a set of strings (all the strings that it recognizes).
8
YES
b b a a b b a b b
YES
NO
Previous lecture: DFA details and example
A DFA is an abstract machine with a finite number of states, each labelled Y or N and
transitions between states, each labelled with a symbol. One state is the start state.
• Begin in the start state.
• Read an input symbol and move to the indicated state.
• Repeat until the last input symbol has been read.
• Turn on the "YES" or "NO" light according to the label on the final state.
9
YES
b b a a b b a b b
YES
NO
YES
Y N N
b b
a a a
b
Does this DFA recognize
this string?
This lecture: Turing machines
A Turing machine (TM) is an abstract model of computation.
• A string is specified on a tape (no limit on its length).
• The TM reads and writes characters on the tape, moving left or right.
• The TM lights "YES" if it recognizes the string, "NO" otherwise.
• The TM may halt, leaving the result of the computation on the tape.
10
YES
YES
NO
. . . . . .
HALT
This lecture: Turing Machine details and example
A Turing Machine is an abstract machine with a finite number of states, each labeled Y, N,
H, L, or R and transitions between states, each labeled with a read/write pair of symbols.
• Begin in the start state.
• Read an input symbol, move to the indicated state and write the indicated output.
• Move tape head left if new state is labeled L, right if it is labeled R.
• Repeat until entering a state labeled Y, N, or H ( and turn on associated light).
11
YES
# # # 1 0 1 1 0 0 1 1 1 # # #
YES
NO
HALT
HALT
H
L
1:0 #:1
R #:#
0:1
DFAs vs TMs
12
DFAs
• Can read input symbols from the tape.
• Can only move tape head to the right.
• Tape is finite (a string).
• One step per input symbol.
• Can recognize (turn on "YES" or "NO").
TMs
• Can read from or write onto the tape.
• Can move tape head either direction.
• Tape does not end (either direction).
• No limit on number of steps.
• Can also compute (with output on tape).
Similarities
• Simple model of computation.
• Input on tape is a finite string with symbols from a finite alphabet.
• Finite number of states.
• State transitions determined by current state and input symbol.
Differences
TM example 1: Binary decrementer
13
YES
# # # 1 0 1 0 1 0 0 0 0 # # #
YES
NO
HALT
HALT
H
L
0:1
R #:#
1
1
1
1
0
1 0 1 0 1 0 0 0 0
Input
Output 1 0 1 0 0 1 1 1 1
1:0
Note:
x:x self-loops are omitted.
"Scan right until reading #"
TM example 1: Binary decrementer
14
YES
# # # # # # # # 0 0 0 0 # # #
YES
NO
HALT
H
L
0:1 #:#
R #:#
1
1
1
1
Q. What happens when we try to decrement 0?
A. Doesn't halt! TMs can have bugs, too.
Fix to avoid infinite loop. Check for #.
1:0
"Scan left until
reading 0 or 1"
TM example 2: Binary incrementer
15
YES
# # # 1 0 1 0 0 1 1 1 1 # # #
YES
NO
HALT
HALT
H
L
1:0 #:1
R #:#
0:1
0
0
0
0
1
1 0 1 0 0 1 1 1 1
Input
Output 1 0 1 0 1 0 0 0 0
Note: This adds a 1 at the left as the
last step when incrementing 111...1
TM example 3: Binary adder (method)
16
To compute x + y
• Move right to right end of y.
• Decrement y.
• Move left to right end of x (left of +) .
• Increment x.
• Continue until y = 0 is decremented.
• Clean up by erasing + and 1s.
# # 1 0 1 1 + 1 0 1 0 # #
. . . . . .
# # 1 0 1 1 + 1 0 0 1 # #
. . . . . .
# # 1 0 1 1 + 1 0 0 1 # #
. . . . . .
# # 1 1 0 0 + 1 0 0 1 # #
. . . . . .
# 1 0 1 0 1 + 1 1 1 1 # #
. . . . . .
Found + when seeking 1? Just decremented 0.
# 1 0 1 0 1 # # # # # # #
. . . . . .
Clean up
TM example 3: Binary adder
17
YES
# # # 1 0 1 1 + 1 0 1 0 # # #
YES
NO
HALT
HALT
L
L
0:1
R
#:#
1:0 L
R H
1:#
#:#
+:+
#:1
0:1
+:#
Halt
Clean Up
Find +
Increment x
1:0
Decrement y
Find right end
# 1 0 1 1 + 1 0 0 1 #
# 1 1 0 0 + 1 0 0 1 #
1 0 1 0 1 + 1 1 1 1 #
. . .
# 1 0 1 1 + 1 0 1 0 #
# 1 0 1 1 + 1 0 0 1 #
1 0 1 0 1 # # # # # #
Simulating an infinite tape with two stacks
18
Q. How can we simulate a tape that is infinite on both ends?
A. Use two stacks, one for each end.
private Stack<Character> left;
private Stack<Character> right;
private char read()
{
if (right.isEmpty()) return '#';
else return right.pop();
}
private char write(char c)
{ right.push(c); }
private void moveRight()
{
if (right.isEmpty())
left.push('#')
else left.push(right.pop());
}
private void moveLeft()
{
if (left.isEmpty())
right.push('#')
else right.push(left.pop());
}
# # # 1 0 1 1 + 1 0 1 0 # # #
to simulate TM tape,
need to call write()
exactly once after
each call on read()
1
0
1
1
+
1
0
1
0
# #
move
right
move
left
0
1
1
1
+
1
0
0
1
# #
"tape head" is top of right stack
empty? assume # is there
Simulating the operation of a Turing machine
19
public class TuringMachine
{
private int state;
private int start;
private char[] action;
private ST<Character, Integer>[] next;
private ST<Character, Character>[] out;
/* Stack code from previous slide */
public TM(String filename)
{ /* Fill in data structures */ }
public String simulate(String input)
{
state = start;
for (int i = input.length()-1; i >= 0; i--)
right.push(input.charAt(i));
while (action[state] != 'H')
{
char c = read();
write(out[state].get(c));
state = next[state].get(c);
if (action[state] == 'R') moveRight();
if (action[state] == 'L') moveLeft();
}
return action[state];
}
public static void main(String[] args)
{ /* Similar to DFA's main() */ }
}
% more dec.txt
3 01# 0
R 0 0 1 0 1 #
L 1 2 2 1 0 #
H 2 2 2 0 1 #
% java TM dec.txt
000111
000110
010000
001111
000000
111111
0 R
1 L
2 H
action[]
0 1 #
0 0 0 1
1 1 2 2
2 2 2 2
next[]
H
L
0:1
R #:# 1:0
0 1 #
0 0 1 #
1 1 0 #
2 0 1 #
out[]
0 1 2
#:#
entries in gray are implicit in graphical representation
fixes bug
# states, alphabet, start
type, transitions, output
for each state
C O M P U T E R S C I E N C E
S E D G E W I C K / W A Y N E
CS.15.B.Turing.Machine
15. Turing Machines
•Context
•A simple model of computation
•Universality
•Computability
•Implications
C O M P U T E R S C I E N C E
S E D G E W I C K / W A Y N E
PART II: ALGORITHMS, MAC HINES, and THEORY
CS.15.C.Turing.Universality
Representing a Turing machine
22
Turing's key insight. A TM is nothing more than a finite sequence of symbols.
Implication. Can put a TM and its input on a TM tape.
Profound implication. We can use a TM to simulate the operation of any TM.
H
L
0:1
R #:# 1:0
0 1 2
#:#
decrementer TM
3 01# 0
R 0 0 1 0 1 #
L 1 2 2 1 0 #
H 2 2 2 0 1 #
dec.txt
1 1 0 0 0 3 0 1 # 0 R 0 0 1 0 1 # L 1 2 1 1 0 # H 2 2 2 0 1 #
input Turing machine
Universal Turing machine (UTM)
23
Universal Turing machine. A TM that takes as
input any TM and input for that TM on a TM tape.
Result. Whatever would happen if that TM were to
run with that input (could loop or end in Y, N, or H).
Turing. Simulating a TM is a simple computational task, so there exists a TM to do it: A UTM.
Easier for us to think about. Implement Java simulator as a TM.
# # # 1 1 0 0 0 3 0 1 # 0 R 0 0 1
. . .
YES
NO
HALT
UTM
# # # 1 0 1 1 1 # # #
. . .
YES
NO
HALT
UTM
result that decrementer TM would produce
input to decrementer TM decrementer TM
Implementing a universal Turing machine
Java simulator gives a roadmap
• No need for a constructor as in Java because
everything is already on the tape.
• Simulating the infinite tape is a bit easier
than in Java because TM has an infinite tape.
• Critical part of the calculation is to update
state as indicated.
24
Want to see the details or build your own TM?
Use the booksite's TM development environment.
A 24-state UTM
Warning. TM development may be addictive. Amazed that it's only 24 states?
The record is 4 states, 6 symbols.
Universality
Definition. A task is computable if a Turing machine exists that computes it.
25
Theorem (Turing, 1936). It is possible to invent a single
machine which can be used to do any computable task.
Profound implications
• Any machine that can simulate a TM can simulate a universal Turing machine (UTM).
• Any machine that can simulate a TM can do any computable task.
• Don't need separate devices for solving scientific problems, playing music, email, . . .
UTM: A simple and universal model of computation.
YES
NO
. . . . . .
HALT
UTM
26
Conway's game of life
A cellular automaton
• Cells live and die in an infinite square grid.
• Time proceeds in discrete steps.
Survival. Each cell lives or dies depending on its 8 neighbors:
• Too few neighbors alive? (0 or 1) Die of loneliness.
• Number of living neighbors just right (2 or 3) Survive to next generation.
• Too many neighbors alive? (more than 3) Die of overcrowding.
Birth. Cell born when it has exactly 3 living neighbors.
time t
0 1 1 1 0 0
0 1 1 2 1 0
1 3 5 3 2 0
1 1 3 2 2 0
1 2 3 2 1 0
0 0 0 0 0 0
living neighbors time t+1
John Horton Conway
27
Conway's Game of Life
Lesson. Simple rules can lead to complicated behavior
Example 1. Glider
Example 3. Glider gun breeder (generates glider guns)
Example 2. Glider gun (generates gliders)
Note. YOU can write a program for the game of life (might have been an assignment).
A complex initial configuration for the game of life
Q. What happens with this starting configuration?
28
A. Anything we can compute!
(It is a UTM).
A profound connection to the real world
29
Church-Turing thesis. Turing machines can do anything that can be described by any
physically harnessable process of this universe: All computational devices are equivalent.
Remarks
• A thesis, not a theorem.
• Not subject to proof.
• Is subject to falsification.
New model of computation or new physical process?
• Use simulation to prove equivalence.
• Example: TOY simulator in Java.
• Example: Java compiler in TOY.
Implications
• No need to seek more powerful machines or languages.
• Enables rigorous study of computation (in this universe).
=
= YES
NO
. . . . . .
HALT
30
Evidence in favor of the Church-Turing thesis
Evidence. Many, many models of computation have turned out to be equivalent (universal).
model of computation description
enhanced Turing machines multiple heads, multiple tapes, 2D tape, nondeterminism
untyped lambda calculus method to define and manipulate functions
recursive functions functions dealing with computation on integers
unrestricted grammars iterative string replacement rules used by linguists
extended Lindenmayer systems parallel string replacement rules that model plant growth
programming languages Java, C, C++, Perl, Python, PHP, Lisp, PostScript, Excel
random access machines registers plus main memory, e.g., TOY, Pentium
cellular automata cells which change state based on local interactions
quantum computer compute using superposition of quantum states
DNA computer compute using biological operations on DNA
PCP systems string matching puzzles (stay tuned)
8 decades without a counterexample, and counting.
31
Example of a universal model: Extended Lindenmayer systems for synthetic plants
C O M P U T E R S C I E N C E
S E D G E W I C K / W A Y N E
CS.15.C.Turing.Universality
Image sources
http://astronomy.swin.edu.au/~pbourke/modelling/plants
15. Turing Machines
•Context
•A simple model of computation
•Universality
•Computability
•Implications
C O M P U T E R S C I E N C E
S E D G E W I C K / W A Y N E
PART II: ALGORITHMS, MAC HINES, and THEORY
CS.15.D.Turing.Computability
Post's correspondence problem (PCP)
PCP. A family of puzzles, each based on a set of cards.
• N types of cards.
• No limit on the number of cards of each type.
• Each card has a top string and bottom string.
Does there exist an arrangement of cards with matching top and bottom strings?
34
Example 1 (N = 4).
BAB
A
0
A
ABA
1
AB
B
2
BA
B
3
Solution 1 (easy): YES.
A
ABA
1
BA
B
3
BAB
A
0
AB
B
2
A
ABA
1
Post's correspondence problem (PCP)
PCP. A family of puzzles, each based on a set of cards.
• N types of cards.
• No limit on the number of cards of each type.
• Each card has a top string and bottom string.
Does there exist an arrangement of cards with matching top and bottom strings?
35
Example 2 (N = 4).
BAB
A
0
A
BAB
1
AB
B
2
BA
A
3
Solution 2 (easy): NO. No way to match even the first character!
BAB
A
2
Challenge for the bored: Find a solution that starts with a card of type 0.
Post's correspondence problem (PCP)
PCP. A family of puzzles, each based on a set of cards.
• N types of cards.
• No limit on the number of cards of each type.
• Each card has a top string and bottom string.
Does there exist an arrangement of cards with matching top and bottom strings?
36
Example 3 (created by Andrew Appel).
X
1X
1
11A
A1
3
1
1
4
[A
[B
5
]
]
6
[
[
7
B1
1B
8
B]
A]
9
S[
S[11111X][
0
[1A]E
E
10
Post's correspondence problem (PCP)
PCP. A family of puzzles, each based on a set of cards.
• N types of cards.
• No limit on the number of cards of each type.
• Each card has a top string and bottom string.
Does there exist an arrangement of cards with matching top and bottom strings?
37
0 1 2 3
A reasonable idea. Write a program to take N card types as input and solve PCP.
4
. . .
N
A surprising fact. It is not possible to write such a program.
Another impossible problem
Halting problem. Write a Java program that reads in code for a Java static method f()
and an input x, and decides whether or not f(x) results in an infinite loop.
38
Next. A proof that it is not possible to write such a program.
Example 1 (easy). Example 2 (difficulty unknown).
public void f(int x)
{
while (x != 1)
{
if (x % 2 == 0) x = x / 2;
else x = 3*x + 1;
}
}
public void f(int x)
{
while (x != 1)
{
if (x % 2 == 0) x = x / 2;
else x = 2*x + 1;
}
}
f(7): 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
f(-17): -17 -50 -25 -74 -37 -110 -55 -164 -82 -41 -122 ... -17 ...
Involves Collatz conjecture
(see Recursion lecture)
Halts only if x is a positive power of 2
Undecidability of the halting problem
Definition. A yes-no problem is undecidable if no Turing machine exists to solve it.
(A problem is computable if a Turing machine does exist that solves it.)
39
Theorem (Turing, 1936). The halting problem is undecidable.
Profound implications
• There exists a problem that no Turing machine can solve.
• There exists a problem that no computer can solve.
• There exist many problems that no computer can solve (stay tuned).
Warmup: self-referential statements
Liar paradox (dates back to ancient Greek philosophers).
• Divide all statements into two categories: true and false.
• Consider the statement "This statement is false."
• Is it true? If so, then it is false, a contradiction.
• Is it false? If so, then it is true, a contradiction.
40
Source of the difficulty: Self-reference.
2 + 2 = 4
The earth is round.
Starfish have no brains.
Venus rotates clockwise.
...
2 + 2 = 99
The earth is flat.
Earthworms have 3 hearts.
Saturn rotates clockwise.
...
true false
This statement is false. This statement is false.
Logical conclusion. Cannot label all statements as true or false.
✗ ✗
Proof of the undecidability of the halting problem
41
Theorem (Turing, 1936). The halting problem is undecidable.
Proof outline.
• Assume the existence of a function halt(f, x) that solves the problem.
• Arguments: A function f and input x, encoded as strings.
• Return value: true if f(x) halts and false if f(x) does not halt.
• halt(f, x) always halts.
• Proof idea: Reductio ad absurdum: if any logical argument based on an
assumption leads to an absurd statement, then the assumption is false.
public boolean halt(String f, String x)
{
if ( /* something terribly clever */ ) return true;
else return false;
}
By universality, may as well use Java.
(If this exists, we could simulate it on a TM.)
Proof of the undecidability of the halting problem
42
Theorem (Turing, 1936). The halting problem is undecidable.
Proof.
• Assume the existence of a function halt(f, x)
that solves the problem.
• Create a function strange(f) that goes into an
infinite loop if f(f) halts and halts otherwise.
• Call strange() with itself as argument.
• If strange(strange) halts, then
strange(strange) goes into an infinite loop.
• If strange(strange) does not halt, then
strange(strange) halts.
• Reductio ad absurdum.
• halt(f, x) cannot exist.
public boolean halt(String f, String x)
{
if ( /* f(x) halts */ ) return true;
else return false;
}
Solution to the problem
public void strange(String f)
{
if (halt(f, f))
while (true) { } // infinite loop
}
A client
strange(strange)
A contradiction
halts?
does not halt?
C O M P U T E R S C I E N C E
S E D G E W I C K / W A Y N E
CS.15.D.Turing.Computability
15. Turing Machines
•Context
•A simple model of computation
•Universality
•Computability
•Implications
C O M P U T E R S C I E N C E
S E D G E W I C K / W A Y N E
PART II: ALGORITHMS, MAC HINES, and THEORY
CS.15.E.Turing.Implications
Implications of undecidability
45
Primary implication. If you know that a problem is undecidable...
...don't try to solve it!
What's the idea?
Hey, Alice. We came up with a
great idea at our hackathon.
We're going for startup funding.
An app that you can use to
make sure that any app you
download won't hang your phone!
Umm. I think that's
undecidable.
?
???
Will your app
work on itself ?
46
Implications for programming systems
Halting problem. Give a function f, does it halt on a given input x?
Totality problem. Give a function f, does it halt on every input x?
No-input halting problem. Give a function f with no input, does it halt?
Program equivalence. Do two functions f and g always return same value?
Uninitialized variables. Is the variable x initialized before it's used?
Dead-code elimination. Does this statement ever get executed?
Q. Why is debugging difficult?
A. All of the following are undecidable.
Q. Why are program development environments complicated?
A. They are programs that manipulate programs.
Prove each by reduction from the halting problem: A solution would solve the halting problem.
UNDECIDABLE
Another undecidable problem
47
Theorem (Church and Turing, 1936). The Entscheidungsproblem is undecidable.
The Entscheidungsproblem (Hilbert, 1928)
• Given a first-order logic with a finite number of additional axioms.
• Is the statement provable from the axioms using the rules of logic?
David Hilbert
1862−1943
Lambda calculus
• Formulated by Church in the 1930s to address the Entscheidungsproblem.
• Also the basis of modern functional languages.
Alonso Church
1903−1995
UNDECIDABLE
"Decision problem"
Another undecidable problem
48
Theorem (Post, 1946). Post's correspondence problem is undecidable.
UNDECIDABLE
Examples of undecidable problems from computational mathematics
49
Hilbert's 10th problem
• Given a multivariate polynomial f (x, y, z, ...).
• Does f have integral roots ? (Do there exist
integers x, y, z, such that f (x, y, z, ...) = 0 ? )
Definite integration
• Given a rational function f (x ) composed of
polynomial and trigonometric functions.
• Does exist?
( , , ) = +
Ex. 1
( , , ) =
YES
( , ) = +
Ex. 2 NO
NO
( )
YES
cos( )
+
=
Ex. 1
cos( )
+
Ex. 2
cos( )
UNDECIDABLE
UNDECIDABLE
Examples of undecidable problems from computer science
50
Optimal data compression
• Find the shortest program to produce a given string.
• Find the shortest program to produce a given picture.
Virus identification
• Is this code equivalent to this known virus?
• Does this code contain a virus?
UNDECIDABLE
UNDECIDABLE
produced by a 34-line Java program
Private Sub AutoOpen()
On Error Resume Next
If System.PrivateProfileString("", CURRENT_USERSoftware
MicrosoftOffice9.0WordSecurity",
"Level") <> "" Then
CommandBars("Macro").Controls("Security...").Enabled = False
. . .
For oo = 1 To AddyBook.AddressEntries.Count
Peep = AddyBook.AddressEntries(x)
BreakUmOffASlice.Recipients.Add Peep
x = x + 1
If x > 50 Then oo = AddyBook.AddressEntries.Count
Next oo
. . .
BreakUmOffASlice.Subject = "Important Message From " &
Application.UserName
BreakUmOffASlice.Body = "Here is that document you asked for
... don't show anyone else ;-)"
. . .
Melissa virus (1999)
Turing's key ideas
51
The Turing machine. A formal model of computation.
Equivalence of programs and data. Encode both as strings and compute with both.
Universality. Concept of general-purpose programmable computers.
Church-Turing thesis. If it is computable at all, it is computable with a Turing machine.
Computability. There exist inherent limits to computation.
Turing's paper was published in 1936, ten years before Eckert and Mauchly worked on ENIAC (!)
John von Neumann read the paper...
Turing's paper in the Proceedings of the London Mathematical Society
"On Computable Numbers, With an Application to the Entscheidungsproblem"
was one of the most impactful scientific papers of the 20th century.
Alan Turing
1912−1954
Stay tuned
Alan Turing: the father of computer science
52
It was not only a matter of abstract mathematics, not only a play of
symbols, for it involved thinking about what people did in the physical
world…. It was a play of imagination like that of Einstein or von
Neumann, doubting the axioms rather than measuring effects…. What
he had done was to combine such a naïve mechanistic picture of the
mind with the precise logic of pure mathematics. His machines – soon
— John Hodges, in Alan Turing, the Enigma
= YES
NO
. . . . . .
HALT
UTM
A Google data center
A Universal Turing Machine
C O M P U T E R S C I E N C E
S E D G E W I C K / W A Y N E
CS.15.E.Turing.Implications
http://introcs.cs.princeton.edu
R O B E R T S E D G E W I C K
K E V I N W A Y N E
Computer
Science
Computer
Science
An Interdisciplinary Approach
15. Turing Machines
C O M P U T E R S C I E N C E
S E D G E W I C K / W A Y N E
PART II: ALGORITHMS, MAC HINES, and THEORY
Section 7.4

More Related Content

Similar to CS.15.Turing.pdf

Winter 8 TM.pptx
Winter 8 TM.pptxWinter 8 TM.pptx
Winter 8 TM.pptxHarisPrince
 
Model of Computation-Turing Machine
Model of Computation-Turing MachineModel of Computation-Turing Machine
Model of Computation-Turing MachineIOSR Journals
 
Computability, turing machines and lambda calculus
Computability, turing machines and lambda calculusComputability, turing machines and lambda calculus
Computability, turing machines and lambda calculusEdward Blurock
 
Turing machine Introduction
Turing machine IntroductionTuring machine Introduction
Turing machine IntroductionAram Rafeq
 
Free Ebooks Download ! Edhole
Free Ebooks Download ! EdholeFree Ebooks Download ! Edhole
Free Ebooks Download ! EdholeEdhole.com
 
churchturingthesis-160906170827.pptx
churchturingthesis-160906170827.pptxchurchturingthesis-160906170827.pptx
churchturingthesis-160906170827.pptxmukul narayana
 
Class 36: Halting Problem
Class 36: Halting ProblemClass 36: Halting Problem
Class 36: Halting ProblemDavid Evans
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template LibraryIlio Catallo
 
Turing machines 1
Turing machines 1Turing machines 1
Turing machines 1grahamwell
 
C Programming Language
C Programming LanguageC Programming Language
C Programming LanguageRTS Tech
 
Church Turing Thesis
Church Turing ThesisChurch Turing Thesis
Church Turing ThesisHemant Sharma
 
CP4151 Advanced data structures and algorithms
CP4151 Advanced data structures and algorithmsCP4151 Advanced data structures and algorithms
CP4151 Advanced data structures and algorithmsSheba41
 
The TclQuadcode Compiler
The TclQuadcode CompilerThe TclQuadcode Compiler
The TclQuadcode CompilerDonal Fellows
 
G044053060
G044053060G044053060
G044053060inventy
 
Class 16: Making Loops
Class 16: Making LoopsClass 16: Making Loops
Class 16: Making LoopsDavid Evans
 

Similar to CS.15.Turing.pdf (20)

universality
universalityuniversality
universality
 
Winter 8 TM.pptx
Winter 8 TM.pptxWinter 8 TM.pptx
Winter 8 TM.pptx
 
Model of Computation-Turing Machine
Model of Computation-Turing MachineModel of Computation-Turing Machine
Model of Computation-Turing Machine
 
Computability, turing machines and lambda calculus
Computability, turing machines and lambda calculusComputability, turing machines and lambda calculus
Computability, turing machines and lambda calculus
 
Turing machine Introduction
Turing machine IntroductionTuring machine Introduction
Turing machine Introduction
 
Free Ebooks Download ! Edhole
Free Ebooks Download ! EdholeFree Ebooks Download ! Edhole
Free Ebooks Download ! Edhole
 
churchturingthesis-160906170827.pptx
churchturingthesis-160906170827.pptxchurchturingthesis-160906170827.pptx
churchturingthesis-160906170827.pptx
 
Class 36: Halting Problem
Class 36: Halting ProblemClass 36: Halting Problem
Class 36: Halting Problem
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
 
C notes for exam preparation
C notes for exam preparationC notes for exam preparation
C notes for exam preparation
 
Turing machines 1
Turing machines 1Turing machines 1
Turing machines 1
 
C Programming Language
C Programming LanguageC Programming Language
C Programming Language
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Church Turing Thesis
Church Turing ThesisChurch Turing Thesis
Church Turing Thesis
 
CP4151 Advanced data structures and algorithms
CP4151 Advanced data structures and algorithmsCP4151 Advanced data structures and algorithms
CP4151 Advanced data structures and algorithms
 
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
 
The TclQuadcode Compiler
The TclQuadcode CompilerThe TclQuadcode Compiler
The TclQuadcode Compiler
 
Left factor put
Left factor putLeft factor put
Left factor put
 
G044053060
G044053060G044053060
G044053060
 
Class 16: Making Loops
Class 16: Making LoopsClass 16: Making Loops
Class 16: Making Loops
 

Recently uploaded

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

CS.15.Turing.pdf

  • 1. http://introcs.cs.princeton.edu R O B E R T S E D G E W I C K K E V I N W A Y N E Computer Science Computer Science An Interdisciplinary Approach 15. Turing Machines C O M P U T E R S C I E N C E S E D G E W I C K / W A Y N E PART II: ALGORITHMS, MAC HINES, and THEORY Section 7.4
  • 2. 15. Turing Machines •Context •A simple model of computation •Universality •Computability •Implications C O M P U T E R S C I E N C E S E D G E W I C K / W A Y N E PART II: ALGORITHMS, MAC HINES, and THEORY CS.15.A.Turing.Context
  • 3. Universality and computability 3 Fundamental questions • What is a general-purpose computer? • Are there limits on the power of digital computers? • Are there limits on the power of machines we can build? Pioneering work at Princeton in the 1930s. David Hilbert 1862−1943 Asked the questions Kurt Gödel 1906−1978 Solved the math problem Alonzo Church 1903−1995 Solved the decision problem Alan Turing 1912−1954 Provided THE answers
  • 4. Context: Mathematics and logic 4 Mathematics. Any formal system powerful enough to express arithmetic. Complete. Can prove truth or falsity of any arithmetic statement. Consistent. Cannot prove contradictions like 2 + 2 = 5. Decidable. An algorithm exists to determine truth of every statement. Q. (Hilbert, 1900) Is mathematics complete and consistent? A. (Gödel's Incompleteness Theorem, 1931) NO (!!!) Q. (Hilbert's Entscheidungsproblem) Is mathematics decidable? A. (Church 1936, Turing 1936) NO (!!) Principia Mathematics Peano arithmetic Zermelo-Fraenkel set theory . . .
  • 5. C O M P U T E R S C I E N C E S E D G E W I C K / W A Y N E CS.15.A.Turing.Context Image sources http://en.wikipedia.org/wiki/David_Hilbert#/media/File:Hilbert.jpg http://en.wikipedia.org/wiki/Kurt_Gödel#/media/File:Kurt_gödel.jpg http://en.wikipedia.org/wiki/Alonzo_Church#/media/File:Alonzo_Church.jpg http://en.wikipedia.org/wiki/Alan_Turing#/media/File:Alan_Turing_photo.jpg
  • 6. 15. Turing Machines •Context •A simple model of computation •Universality •Computability •Implications C O M P U T E R S C I E N C E S E D G E W I C K / W A Y N E PART II: ALGORITHMS, MAC HINES, and THEORY CS.15.B.Turing.Machine
  • 7. Starting point 7 Goals • Develop a model of computation that encompasses all known computational processes. • Make the model as simple as possible. Characteristics • Discrete. • Local. • States. Example: A familiar computational process. 3 1 4 2 7 1 8 2 4 0 2 1 3 0 0 1 1
  • 8. Previous lecture: DFAs A DFA is an abstract machine that solves a pattern matching problem. • A string is specified on an input tape (no limit on its length). • The DFA reads each character on input tape once, moving left to right. • The DFA lights "YES" if it recognizes the string, "NO" otherwise. Each DFA defines a set of strings (all the strings that it recognizes). 8 YES b b a a b b a b b YES NO
  • 9. Previous lecture: DFA details and example A DFA is an abstract machine with a finite number of states, each labelled Y or N and transitions between states, each labelled with a symbol. One state is the start state. • Begin in the start state. • Read an input symbol and move to the indicated state. • Repeat until the last input symbol has been read. • Turn on the "YES" or "NO" light according to the label on the final state. 9 YES b b a a b b a b b YES NO YES Y N N b b a a a b Does this DFA recognize this string?
  • 10. This lecture: Turing machines A Turing machine (TM) is an abstract model of computation. • A string is specified on a tape (no limit on its length). • The TM reads and writes characters on the tape, moving left or right. • The TM lights "YES" if it recognizes the string, "NO" otherwise. • The TM may halt, leaving the result of the computation on the tape. 10 YES YES NO . . . . . . HALT
  • 11. This lecture: Turing Machine details and example A Turing Machine is an abstract machine with a finite number of states, each labeled Y, N, H, L, or R and transitions between states, each labeled with a read/write pair of symbols. • Begin in the start state. • Read an input symbol, move to the indicated state and write the indicated output. • Move tape head left if new state is labeled L, right if it is labeled R. • Repeat until entering a state labeled Y, N, or H ( and turn on associated light). 11 YES # # # 1 0 1 1 0 0 1 1 1 # # # YES NO HALT HALT H L 1:0 #:1 R #:# 0:1
  • 12. DFAs vs TMs 12 DFAs • Can read input symbols from the tape. • Can only move tape head to the right. • Tape is finite (a string). • One step per input symbol. • Can recognize (turn on "YES" or "NO"). TMs • Can read from or write onto the tape. • Can move tape head either direction. • Tape does not end (either direction). • No limit on number of steps. • Can also compute (with output on tape). Similarities • Simple model of computation. • Input on tape is a finite string with symbols from a finite alphabet. • Finite number of states. • State transitions determined by current state and input symbol. Differences
  • 13. TM example 1: Binary decrementer 13 YES # # # 1 0 1 0 1 0 0 0 0 # # # YES NO HALT HALT H L 0:1 R #:# 1 1 1 1 0 1 0 1 0 1 0 0 0 0 Input Output 1 0 1 0 0 1 1 1 1 1:0 Note: x:x self-loops are omitted. "Scan right until reading #"
  • 14. TM example 1: Binary decrementer 14 YES # # # # # # # # 0 0 0 0 # # # YES NO HALT H L 0:1 #:# R #:# 1 1 1 1 Q. What happens when we try to decrement 0? A. Doesn't halt! TMs can have bugs, too. Fix to avoid infinite loop. Check for #. 1:0 "Scan left until reading 0 or 1"
  • 15. TM example 2: Binary incrementer 15 YES # # # 1 0 1 0 0 1 1 1 1 # # # YES NO HALT HALT H L 1:0 #:1 R #:# 0:1 0 0 0 0 1 1 0 1 0 0 1 1 1 1 Input Output 1 0 1 0 1 0 0 0 0 Note: This adds a 1 at the left as the last step when incrementing 111...1
  • 16. TM example 3: Binary adder (method) 16 To compute x + y • Move right to right end of y. • Decrement y. • Move left to right end of x (left of +) . • Increment x. • Continue until y = 0 is decremented. • Clean up by erasing + and 1s. # # 1 0 1 1 + 1 0 1 0 # # . . . . . . # # 1 0 1 1 + 1 0 0 1 # # . . . . . . # # 1 0 1 1 + 1 0 0 1 # # . . . . . . # # 1 1 0 0 + 1 0 0 1 # # . . . . . . # 1 0 1 0 1 + 1 1 1 1 # # . . . . . . Found + when seeking 1? Just decremented 0. # 1 0 1 0 1 # # # # # # # . . . . . . Clean up
  • 17. TM example 3: Binary adder 17 YES # # # 1 0 1 1 + 1 0 1 0 # # # YES NO HALT HALT L L 0:1 R #:# 1:0 L R H 1:# #:# +:+ #:1 0:1 +:# Halt Clean Up Find + Increment x 1:0 Decrement y Find right end # 1 0 1 1 + 1 0 0 1 # # 1 1 0 0 + 1 0 0 1 # 1 0 1 0 1 + 1 1 1 1 # . . . # 1 0 1 1 + 1 0 1 0 # # 1 0 1 1 + 1 0 0 1 # 1 0 1 0 1 # # # # # #
  • 18. Simulating an infinite tape with two stacks 18 Q. How can we simulate a tape that is infinite on both ends? A. Use two stacks, one for each end. private Stack<Character> left; private Stack<Character> right; private char read() { if (right.isEmpty()) return '#'; else return right.pop(); } private char write(char c) { right.push(c); } private void moveRight() { if (right.isEmpty()) left.push('#') else left.push(right.pop()); } private void moveLeft() { if (left.isEmpty()) right.push('#') else right.push(left.pop()); } # # # 1 0 1 1 + 1 0 1 0 # # # to simulate TM tape, need to call write() exactly once after each call on read() 1 0 1 1 + 1 0 1 0 # # move right move left 0 1 1 1 + 1 0 0 1 # # "tape head" is top of right stack empty? assume # is there
  • 19. Simulating the operation of a Turing machine 19 public class TuringMachine { private int state; private int start; private char[] action; private ST<Character, Integer>[] next; private ST<Character, Character>[] out; /* Stack code from previous slide */ public TM(String filename) { /* Fill in data structures */ } public String simulate(String input) { state = start; for (int i = input.length()-1; i >= 0; i--) right.push(input.charAt(i)); while (action[state] != 'H') { char c = read(); write(out[state].get(c)); state = next[state].get(c); if (action[state] == 'R') moveRight(); if (action[state] == 'L') moveLeft(); } return action[state]; } public static void main(String[] args) { /* Similar to DFA's main() */ } } % more dec.txt 3 01# 0 R 0 0 1 0 1 # L 1 2 2 1 0 # H 2 2 2 0 1 # % java TM dec.txt 000111 000110 010000 001111 000000 111111 0 R 1 L 2 H action[] 0 1 # 0 0 0 1 1 1 2 2 2 2 2 2 next[] H L 0:1 R #:# 1:0 0 1 # 0 0 1 # 1 1 0 # 2 0 1 # out[] 0 1 2 #:# entries in gray are implicit in graphical representation fixes bug # states, alphabet, start type, transitions, output for each state
  • 20. C O M P U T E R S C I E N C E S E D G E W I C K / W A Y N E CS.15.B.Turing.Machine
  • 21. 15. Turing Machines •Context •A simple model of computation •Universality •Computability •Implications C O M P U T E R S C I E N C E S E D G E W I C K / W A Y N E PART II: ALGORITHMS, MAC HINES, and THEORY CS.15.C.Turing.Universality
  • 22. Representing a Turing machine 22 Turing's key insight. A TM is nothing more than a finite sequence of symbols. Implication. Can put a TM and its input on a TM tape. Profound implication. We can use a TM to simulate the operation of any TM. H L 0:1 R #:# 1:0 0 1 2 #:# decrementer TM 3 01# 0 R 0 0 1 0 1 # L 1 2 2 1 0 # H 2 2 2 0 1 # dec.txt 1 1 0 0 0 3 0 1 # 0 R 0 0 1 0 1 # L 1 2 1 1 0 # H 2 2 2 0 1 # input Turing machine
  • 23. Universal Turing machine (UTM) 23 Universal Turing machine. A TM that takes as input any TM and input for that TM on a TM tape. Result. Whatever would happen if that TM were to run with that input (could loop or end in Y, N, or H). Turing. Simulating a TM is a simple computational task, so there exists a TM to do it: A UTM. Easier for us to think about. Implement Java simulator as a TM. # # # 1 1 0 0 0 3 0 1 # 0 R 0 0 1 . . . YES NO HALT UTM # # # 1 0 1 1 1 # # # . . . YES NO HALT UTM result that decrementer TM would produce input to decrementer TM decrementer TM
  • 24. Implementing a universal Turing machine Java simulator gives a roadmap • No need for a constructor as in Java because everything is already on the tape. • Simulating the infinite tape is a bit easier than in Java because TM has an infinite tape. • Critical part of the calculation is to update state as indicated. 24 Want to see the details or build your own TM? Use the booksite's TM development environment. A 24-state UTM Warning. TM development may be addictive. Amazed that it's only 24 states? The record is 4 states, 6 symbols.
  • 25. Universality Definition. A task is computable if a Turing machine exists that computes it. 25 Theorem (Turing, 1936). It is possible to invent a single machine which can be used to do any computable task. Profound implications • Any machine that can simulate a TM can simulate a universal Turing machine (UTM). • Any machine that can simulate a TM can do any computable task. • Don't need separate devices for solving scientific problems, playing music, email, . . . UTM: A simple and universal model of computation. YES NO . . . . . . HALT UTM
  • 26. 26 Conway's game of life A cellular automaton • Cells live and die in an infinite square grid. • Time proceeds in discrete steps. Survival. Each cell lives or dies depending on its 8 neighbors: • Too few neighbors alive? (0 or 1) Die of loneliness. • Number of living neighbors just right (2 or 3) Survive to next generation. • Too many neighbors alive? (more than 3) Die of overcrowding. Birth. Cell born when it has exactly 3 living neighbors. time t 0 1 1 1 0 0 0 1 1 2 1 0 1 3 5 3 2 0 1 1 3 2 2 0 1 2 3 2 1 0 0 0 0 0 0 0 living neighbors time t+1 John Horton Conway
  • 27. 27 Conway's Game of Life Lesson. Simple rules can lead to complicated behavior Example 1. Glider Example 3. Glider gun breeder (generates glider guns) Example 2. Glider gun (generates gliders) Note. YOU can write a program for the game of life (might have been an assignment).
  • 28. A complex initial configuration for the game of life Q. What happens with this starting configuration? 28 A. Anything we can compute! (It is a UTM).
  • 29. A profound connection to the real world 29 Church-Turing thesis. Turing machines can do anything that can be described by any physically harnessable process of this universe: All computational devices are equivalent. Remarks • A thesis, not a theorem. • Not subject to proof. • Is subject to falsification. New model of computation or new physical process? • Use simulation to prove equivalence. • Example: TOY simulator in Java. • Example: Java compiler in TOY. Implications • No need to seek more powerful machines or languages. • Enables rigorous study of computation (in this universe). = = YES NO . . . . . . HALT
  • 30. 30 Evidence in favor of the Church-Turing thesis Evidence. Many, many models of computation have turned out to be equivalent (universal). model of computation description enhanced Turing machines multiple heads, multiple tapes, 2D tape, nondeterminism untyped lambda calculus method to define and manipulate functions recursive functions functions dealing with computation on integers unrestricted grammars iterative string replacement rules used by linguists extended Lindenmayer systems parallel string replacement rules that model plant growth programming languages Java, C, C++, Perl, Python, PHP, Lisp, PostScript, Excel random access machines registers plus main memory, e.g., TOY, Pentium cellular automata cells which change state based on local interactions quantum computer compute using superposition of quantum states DNA computer compute using biological operations on DNA PCP systems string matching puzzles (stay tuned) 8 decades without a counterexample, and counting.
  • 31. 31 Example of a universal model: Extended Lindenmayer systems for synthetic plants
  • 32. C O M P U T E R S C I E N C E S E D G E W I C K / W A Y N E CS.15.C.Turing.Universality Image sources http://astronomy.swin.edu.au/~pbourke/modelling/plants
  • 33. 15. Turing Machines •Context •A simple model of computation •Universality •Computability •Implications C O M P U T E R S C I E N C E S E D G E W I C K / W A Y N E PART II: ALGORITHMS, MAC HINES, and THEORY CS.15.D.Turing.Computability
  • 34. Post's correspondence problem (PCP) PCP. A family of puzzles, each based on a set of cards. • N types of cards. • No limit on the number of cards of each type. • Each card has a top string and bottom string. Does there exist an arrangement of cards with matching top and bottom strings? 34 Example 1 (N = 4). BAB A 0 A ABA 1 AB B 2 BA B 3 Solution 1 (easy): YES. A ABA 1 BA B 3 BAB A 0 AB B 2 A ABA 1
  • 35. Post's correspondence problem (PCP) PCP. A family of puzzles, each based on a set of cards. • N types of cards. • No limit on the number of cards of each type. • Each card has a top string and bottom string. Does there exist an arrangement of cards with matching top and bottom strings? 35 Example 2 (N = 4). BAB A 0 A BAB 1 AB B 2 BA A 3 Solution 2 (easy): NO. No way to match even the first character!
  • 36. BAB A 2 Challenge for the bored: Find a solution that starts with a card of type 0. Post's correspondence problem (PCP) PCP. A family of puzzles, each based on a set of cards. • N types of cards. • No limit on the number of cards of each type. • Each card has a top string and bottom string. Does there exist an arrangement of cards with matching top and bottom strings? 36 Example 3 (created by Andrew Appel). X 1X 1 11A A1 3 1 1 4 [A [B 5 ] ] 6 [ [ 7 B1 1B 8 B] A] 9 S[ S[11111X][ 0 [1A]E E 10
  • 37. Post's correspondence problem (PCP) PCP. A family of puzzles, each based on a set of cards. • N types of cards. • No limit on the number of cards of each type. • Each card has a top string and bottom string. Does there exist an arrangement of cards with matching top and bottom strings? 37 0 1 2 3 A reasonable idea. Write a program to take N card types as input and solve PCP. 4 . . . N A surprising fact. It is not possible to write such a program.
  • 38. Another impossible problem Halting problem. Write a Java program that reads in code for a Java static method f() and an input x, and decides whether or not f(x) results in an infinite loop. 38 Next. A proof that it is not possible to write such a program. Example 1 (easy). Example 2 (difficulty unknown). public void f(int x) { while (x != 1) { if (x % 2 == 0) x = x / 2; else x = 3*x + 1; } } public void f(int x) { while (x != 1) { if (x % 2 == 0) x = x / 2; else x = 2*x + 1; } } f(7): 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 f(-17): -17 -50 -25 -74 -37 -110 -55 -164 -82 -41 -122 ... -17 ... Involves Collatz conjecture (see Recursion lecture) Halts only if x is a positive power of 2
  • 39. Undecidability of the halting problem Definition. A yes-no problem is undecidable if no Turing machine exists to solve it. (A problem is computable if a Turing machine does exist that solves it.) 39 Theorem (Turing, 1936). The halting problem is undecidable. Profound implications • There exists a problem that no Turing machine can solve. • There exists a problem that no computer can solve. • There exist many problems that no computer can solve (stay tuned).
  • 40. Warmup: self-referential statements Liar paradox (dates back to ancient Greek philosophers). • Divide all statements into two categories: true and false. • Consider the statement "This statement is false." • Is it true? If so, then it is false, a contradiction. • Is it false? If so, then it is true, a contradiction. 40 Source of the difficulty: Self-reference. 2 + 2 = 4 The earth is round. Starfish have no brains. Venus rotates clockwise. ... 2 + 2 = 99 The earth is flat. Earthworms have 3 hearts. Saturn rotates clockwise. ... true false This statement is false. This statement is false. Logical conclusion. Cannot label all statements as true or false. ✗ ✗
  • 41. Proof of the undecidability of the halting problem 41 Theorem (Turing, 1936). The halting problem is undecidable. Proof outline. • Assume the existence of a function halt(f, x) that solves the problem. • Arguments: A function f and input x, encoded as strings. • Return value: true if f(x) halts and false if f(x) does not halt. • halt(f, x) always halts. • Proof idea: Reductio ad absurdum: if any logical argument based on an assumption leads to an absurd statement, then the assumption is false. public boolean halt(String f, String x) { if ( /* something terribly clever */ ) return true; else return false; } By universality, may as well use Java. (If this exists, we could simulate it on a TM.)
  • 42. Proof of the undecidability of the halting problem 42 Theorem (Turing, 1936). The halting problem is undecidable. Proof. • Assume the existence of a function halt(f, x) that solves the problem. • Create a function strange(f) that goes into an infinite loop if f(f) halts and halts otherwise. • Call strange() with itself as argument. • If strange(strange) halts, then strange(strange) goes into an infinite loop. • If strange(strange) does not halt, then strange(strange) halts. • Reductio ad absurdum. • halt(f, x) cannot exist. public boolean halt(String f, String x) { if ( /* f(x) halts */ ) return true; else return false; } Solution to the problem public void strange(String f) { if (halt(f, f)) while (true) { } // infinite loop } A client strange(strange) A contradiction halts? does not halt?
  • 43. C O M P U T E R S C I E N C E S E D G E W I C K / W A Y N E CS.15.D.Turing.Computability
  • 44. 15. Turing Machines •Context •A simple model of computation •Universality •Computability •Implications C O M P U T E R S C I E N C E S E D G E W I C K / W A Y N E PART II: ALGORITHMS, MAC HINES, and THEORY CS.15.E.Turing.Implications
  • 45. Implications of undecidability 45 Primary implication. If you know that a problem is undecidable... ...don't try to solve it! What's the idea? Hey, Alice. We came up with a great idea at our hackathon. We're going for startup funding. An app that you can use to make sure that any app you download won't hang your phone! Umm. I think that's undecidable. ? ??? Will your app work on itself ?
  • 46. 46 Implications for programming systems Halting problem. Give a function f, does it halt on a given input x? Totality problem. Give a function f, does it halt on every input x? No-input halting problem. Give a function f with no input, does it halt? Program equivalence. Do two functions f and g always return same value? Uninitialized variables. Is the variable x initialized before it's used? Dead-code elimination. Does this statement ever get executed? Q. Why is debugging difficult? A. All of the following are undecidable. Q. Why are program development environments complicated? A. They are programs that manipulate programs. Prove each by reduction from the halting problem: A solution would solve the halting problem. UNDECIDABLE
  • 47. Another undecidable problem 47 Theorem (Church and Turing, 1936). The Entscheidungsproblem is undecidable. The Entscheidungsproblem (Hilbert, 1928) • Given a first-order logic with a finite number of additional axioms. • Is the statement provable from the axioms using the rules of logic? David Hilbert 1862−1943 Lambda calculus • Formulated by Church in the 1930s to address the Entscheidungsproblem. • Also the basis of modern functional languages. Alonso Church 1903−1995 UNDECIDABLE "Decision problem"
  • 48. Another undecidable problem 48 Theorem (Post, 1946). Post's correspondence problem is undecidable. UNDECIDABLE
  • 49. Examples of undecidable problems from computational mathematics 49 Hilbert's 10th problem • Given a multivariate polynomial f (x, y, z, ...). • Does f have integral roots ? (Do there exist integers x, y, z, such that f (x, y, z, ...) = 0 ? ) Definite integration • Given a rational function f (x ) composed of polynomial and trigonometric functions. • Does exist? ( , , ) = + Ex. 1 ( , , ) = YES ( , ) = + Ex. 2 NO NO ( ) YES cos( ) + = Ex. 1 cos( ) + Ex. 2 cos( ) UNDECIDABLE UNDECIDABLE
  • 50. Examples of undecidable problems from computer science 50 Optimal data compression • Find the shortest program to produce a given string. • Find the shortest program to produce a given picture. Virus identification • Is this code equivalent to this known virus? • Does this code contain a virus? UNDECIDABLE UNDECIDABLE produced by a 34-line Java program Private Sub AutoOpen() On Error Resume Next If System.PrivateProfileString("", CURRENT_USERSoftware MicrosoftOffice9.0WordSecurity", "Level") <> "" Then CommandBars("Macro").Controls("Security...").Enabled = False . . . For oo = 1 To AddyBook.AddressEntries.Count Peep = AddyBook.AddressEntries(x) BreakUmOffASlice.Recipients.Add Peep x = x + 1 If x > 50 Then oo = AddyBook.AddressEntries.Count Next oo . . . BreakUmOffASlice.Subject = "Important Message From " & Application.UserName BreakUmOffASlice.Body = "Here is that document you asked for ... don't show anyone else ;-)" . . . Melissa virus (1999)
  • 51. Turing's key ideas 51 The Turing machine. A formal model of computation. Equivalence of programs and data. Encode both as strings and compute with both. Universality. Concept of general-purpose programmable computers. Church-Turing thesis. If it is computable at all, it is computable with a Turing machine. Computability. There exist inherent limits to computation. Turing's paper was published in 1936, ten years before Eckert and Mauchly worked on ENIAC (!) John von Neumann read the paper... Turing's paper in the Proceedings of the London Mathematical Society "On Computable Numbers, With an Application to the Entscheidungsproblem" was one of the most impactful scientific papers of the 20th century. Alan Turing 1912−1954 Stay tuned
  • 52. Alan Turing: the father of computer science 52 It was not only a matter of abstract mathematics, not only a play of symbols, for it involved thinking about what people did in the physical world…. It was a play of imagination like that of Einstein or von Neumann, doubting the axioms rather than measuring effects…. What he had done was to combine such a naïve mechanistic picture of the mind with the precise logic of pure mathematics. His machines – soon — John Hodges, in Alan Turing, the Enigma = YES NO . . . . . . HALT UTM A Google data center A Universal Turing Machine
  • 53. C O M P U T E R S C I E N C E S E D G E W I C K / W A Y N E CS.15.E.Turing.Implications
  • 54. http://introcs.cs.princeton.edu R O B E R T S E D G E W I C K K E V I N W A Y N E Computer Science Computer Science An Interdisciplinary Approach 15. Turing Machines C O M P U T E R S C I E N C E S E D G E W I C K / W A Y N E PART II: ALGORITHMS, MAC HINES, and THEORY Section 7.4