1 Lecture 12 Computations –Formal Definition –Important Terms Computational Models –Formal...
-
date post
21-Dec-2015 -
Category
Documents
-
view
220 -
download
0
Transcript of 1 Lecture 12 Computations –Formal Definition –Important Terms Computational Models –Formal...
1
Lecture 12
• Computations– Formal Definition– Important Terms
• Computational Models– Formal Definition– Sequential access memory (tape)– Graphical representation
3
Computations
• What is a computation?– Execution of a program P on an input x
• Is the computation just the output produced by running P on x?– No, we define a computation to be a trace of the
entire execution
4
Exercise1 int x,y,r;
2 cin >> x;
3 cin >> y;
4 r = x % y;
5 If r = 0 goto 10;
6 x = y;
7 y = r;
8 r = x % y;
9 goto 5;
10 return y;
Execute the above program on input 657 282
5
Definitions
• Configuration– Intuitive Definition
• Given the original program and the current configuration of a computation, someone should be able to complete the computation
– Contents of a configuration• current instruction to be executed• current value of all variables
• Computation– Complete sequence of configurations
6
Computation 1
1 int x,y,r;
2 cin >> x;
3 cin >> y;
4 r = x % y;
5 If r = 0 goto 10;
6 x = y;
7 y = r;
8 r = x % y;
9 goto 5;
10 return y;
Input: 10 3
• Line 1, x=?,y=?,r=?
• Line 2, x=?, y=?,r=?
• Line 3, x=10, y=?, r=?
• Line 4, x=10, y=3, r=?
• Line 5, x= 10, y=3, r=1
• Line 6, x=10, y=3, r=1
• Line 7, x=3, y=3, r=1
• Line 8, x=3, y=1, r=1
• Line 9, x=3, y=1, r=0
• Line 5, x=3, y=1, r=0
• Line 10, x=3, y=1, r=0
• Output is 1
7
Computation 2
1 int x,y,r;
2 cin >> x;
3 cin >> y;
4 r = x % y;
5 If r = 0 goto 10;
6 x = y;
7 y = r;
8 r = x % y;
9 goto 5;
10 return y;
Input: 53 10
• Line 1, x=?,y=?,r=?
• Line 2, x=?, y=?,r=?
• Line 3, x=53, y=?, r=?
• Line 4, x=53, y=10, r=?
• Line 5, x= 53, y=10, r=3
• Line 6, x=53, y=10, r=3
• Line 7, x=10, y=10, r=3
• Line 8, x=10, y=3, r=3
• Line 9, x=10, y=3, r=1
• Line 5, x=10, y=3, r=1
• ...
8
Computations 1 and 2
• Line 1, x=?,y=?,r=?
• Line 2, x=?, y=?,r=?
• Line 3, x=53, y=?, r=?
• Line 4, x=53, y=10, r=?
• Line 5, x= 53, y=10, r=3
• Line 6, x=53, y=10, r=3
• Line 7, x=10, y=10, r=3
• Line 8, x=10, y=3, r=3
• Line 9, x=10, y=3, r=1
• Line 5, x=10, y=3, r=1
• ...
• Line 1, x=?,y=?,r=?
• Line 2, x=?, y=?,r=?
• Line 3, x=10, y=?, r=?
• Line 4, x=10, y=3, r=?
• Line 5, x= 10, y=3, r=1
• Line 6, x=10, y=3, r=1
• Line 7, x=3, y=3, r=1
• Line 8, x=3, y=1, r=1
• Line 9, x=3, y=1, r=0
• Line 5, x=3, y=1, r=0
• Line 10, x=3, y=1, r=0
• return 1
9
Observation
1 int x,y,r;
2 cin >> x;
3 cin >> y;
4 r = x % y;
5 If r = 0 goto 10;
6 x = y;
7 y = r;
8 r = x % y;
9 goto 5;
10 return y;
• Line 5, x= 10, y=3, r=1– Enough information to
complete the computation
– What was the original input?• Uncertain
• Both previous inputs, 10 3 as well as 53 10 eventually reached above configuration
11
Computational Models
• So far, we have worked with C++ as our computational model
• We now define some new computational models• We first restrict the computational model to
simplify their formalization– Note, these models will be just as powerful as C++
• We then restrict the power of the computational models in ways that limit their power
12
Language Recognition Problems
• We define our computational models with language recognition problems as our focus– Decision problems are general– Decision problems can be formulated as
languages with language recognition problems
13
Components of Computational Models
• Memory
• Data Types
• Operations
• Graphical Representation
• Formal Definition
14
Memory
• Random Access Memory Model– We have been assuming a random access memory
computational model for C++
– Time to access next memory location is independent of previously accessed memory location
• Sequential Access Memory Model– Memory is stored on a tape broken up into cells– Tape head scans currently accessed cell
15
Illustration
TapeHead
Time to access a particular cell is dependent on currenttape head location
......
16
Data Types
• C++ has many data types– integers– reals– characters– allows definition of new data types
• Our models– just characters– Alphabet will typically be just {a,b} or {0,1}
18
Operations
• C++ has a complex operation set– some operations change memory
– other operations control flow of execution
• Our operation set– Memory operations
• read a character
• write a character
• tape head movement
– simple control flow • switch statements
• goto statements
19
Illustration
...... a b a a b a b
TapeHead
1 switch(current tape cell) {case a: write b; move tapehead left 1 cell; goto 7;case b: write a; move tapehead right 1 cell; goto 4;case EOF: return YES;
}2 switch(current tape cell) {...
20
Sample Program1 switch(current tape cell) {
case a: write a; move tapehead right 1 cell; goto 2;
case b: write b; move tapehead right 1 cell; goto 2;
case EOF: return YES;
}
2 switch(current tape cell) {case a: write a; move tapehead right 1 cell; goto 1;
case b: write b; move tapehead right 1 cell; goto 1;
case EOF: return NO;
}
• Claim– This computational model is as powerful as C++
– Any language which can be decided by a C++ program can be decided by one of these programs
21
Example1 switch(current tape cell) {
case a: write a; move tapehead right 1 cell; goto 2;
case b: write b; move tapehead right 1 cell; goto 2;
case EOF: return YES;
}
2 switch(current tape cell) {case a: write a; move tapehead right 1 cell; goto 1;
case b: write b; move tapehead right 1 cell; goto 1;
case EOF: return NO;
}
• Run this program on input aabbaa
22
Computation
1 switch(current tape cell) {case a: write a; move tapehead right 1
cell; goto 2;
case b: write b; move tapehead right 1 cell; goto 2;
case EOF: return YES;
}
2 switch(current tape cell) {case a: write a; move tapehead right 1
cell; goto 1;
case b: write b; move tapehead right 1 cell; goto 1;
case EOF: return NO;
}
Input: aabbaa
• Line 1, tape=aabbaa$
• Line 2, tape=aabbaa$
• Line 1, tape=aabbaa$
• Line 2, tape=aabbaa$
• Line 1, tape=aabbaa$
• Line 2, tape=aabbaa$
• Line 1, tape=aabbaa$
• Output yes– $ is EOF
23
Configurations
• What needs to be recorded?– Current instruction
– Tape contents
– Tape head location
• Line 1, tape=aabbaa$
• Line 2, tape=aabbaa$
• Line 1, tape=aabbaa$
• Line 2, tape=aabbaa$
• Line 1, tape=aabbaa$
• Line 2, tape=aabbaa$
• Line 1, tape=aabbaa$
• Return yes– $ is EOF
24
Graphical Representation
1 switch(current tape cell) {case a: write a; move tapehead right 1
cell; goto 2;
case b: write b; move tapehead right 1 cell; goto 2;
case EOF: return YES;
}
2 switch(current tape cell) {case a: write a; move tapehead right 1
cell; goto 1;
case b: write b; move tapehead right 1 cell; goto 1;
case EOF: return NO;
}
1 2
a;a;Rb;b;R
a;a;Rb;b;R
Color coded.Still missing one thing.