Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Models of Computation Dale Roberts, Lecturer Computer Science,

Similar presentations


Presentation on theme: "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Models of Computation Dale Roberts, Lecturer Computer Science,"— Presentation transcript:

1 Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Models of Computation Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

2 Dale Roberts Algorithms Computer Science is a study of algorithms, which includes: their formal mathematical properties their hardware realizations their linguistic realizations their applications Therefore, our task is to: design and develop algorithms to solve problems study the algorithms to see if they are correct or efficient design and build computer systems which can execute these algorithms design programming languages and translate these algorithms into these languages

3 Dale Roberts “If we can specify an algorithm for a problem, then we can automate its solution” Definition of an Algorithm consists of unambiguous & computable operations produce a result halt in a finite amount of time

4 Dale Roberts Examples of Algorithm A good example of an algorithm A recipe for cherry pie step 1: mix 1 cup sugar, ¼ cup flour, ¼ tsp. salt step 2: stir in ½ cup juice from the cherries step 3: Cook & stir over medium heat until thick step 4: Add 3 cups canned, pitted red cherries step 5: Add 1 tblsp. butter & 4 drops almond extract step 6: Make pie crust & place in a 9 inch pie plate step 7: Fill crust with cherry mix & top with 2 nd crust step 8: Bake at 450 o F for 10 mins. then 350 for 45 mins.

5 Dale Roberts A bad example of an algorithm How to use shampoo XYX step 1: Wet hair step 2: Lather step 3: Rinse step 4: Repeat What is wrong with algorithm?

6 Dale Roberts Algorithmic problem Solving Approach Algorithm Discovery and Design Problem: Assume that we have a list of 10,000 names called N 1, N 2, N 3, …, N 10,000, along with their telephone numbers which we denotes as T 1, T 2, T 3, …, T 10,000. Assume that the numbers are not in alphabetical order. Design an algorithm which allows to input the name of any specific person, same NAME. The algorithm should look for NAME in the list of names and, if found, print the corresponding telephone number. If the name is not found, print a message “ sorry NAME not found on the list ” NamesNumbers N 1 T 1 N 2 T 2 N 3 T 3 … Name ip … …… N 10,000 T 10,000

7 Dale Roberts Method 1: LineOperation 1If NAME = N 1 then write out the value T 1 2If NAME = N 2 then write out the value T 2 3If NAME = N 3 then write out the value T 3 … 9,999If NAME = N 9,999 then write out the value T 9,999 10,000If NAME = N 10,000 then write out the value T 10,000 10,001Stop

8 Dale Roberts Method 2: The sequential Search Algorithm LineOperation 1Set the value of i to 1 2Repeat lines 3 through 5 until either NAME is found or the value of i exceeds 10,000 3 Check to see if NAME is equal to the i th name on the list, i.e., N i. 4 If they are equal then write the telephone number of that person, T i, and mark that NAME has been found 5 Add 1 to the value of i 6 If NAME was not found then write out the message “ I am sorry ….” 7 Stop, you are done.

9 Dale Roberts Method 3: 2 nd Attempt at Designing A sequential Search Algorithm LineOperation 1Set the value of i to 1 2Repeat lines 3 through 5 until NAME is found 3 Check to see if NAME is equal to the i th name on the list, i.e., N i. 4 If they are equal then write the telephone number of that person, T i, and mark that NAME has been found 5 Add 1 to the value of i 6 Stop, you are done.

10 Dale Roberts Algorithm Analysis Not only we are interested in finding a solution, we are also interested in finding an efficient one. In the previous problem, how many comparisons does it take to find a NAME ? Best case situation: 1 comparison Worst case situation: n comparisons, where n = 10,000 Average case situation: (n+1)/2, where n=10,000

11 Dale Roberts If N is a small number, ex. 100, we may not see much significant difference. However, consider the following example: N.Y. City telephone directory has 20,000,000 entries. A computer can do 50,000 comparisons per second. Therefore, the average time to locate NAME is (20,0000,000/50,000)/2  3½ minutes, or approximately 7 minutes to tell NAME is not in the list!!! (20,0000,000/50,000)/2  3½ minutes, or approximately 7 minutes to tell NAME is not in the list!!!

12 Dale Roberts Models of Computation What is a model? Capture the important properties of the real thing probably be different in scale from the real thing suppress details of the real thing lack full functionality of the real thing Example: A model to compute the distance traveled for a moving vehicle: d = r*t d = distance; r = rate of speed; t = time

13 Dale Roberts Why we need models if they are not the real thing? By changing some aspects, we can observe their effects Can provide an environment for learning They can be used as design tools without actually building the real thing – for economic reasons In summary, they can be predict, can be used for training, can be used as test beds.

14 Dale Roberts Model of a Computing Agent Q: Operations in an algorithm must be unambiguous and effectively computable. Therefore, could we design a model of an algorithm before we implement the algorithm in hardware and /or software? A: A computing agent (robot) is a thing/a person that carries out the operations described in an algorithm.

15 Dale Roberts Properties of a Computing Agent can accept an input can store & retrieve information from memory can take actions according to instructions – the actions may depend upon a present state and the current input can produce an output Agent So, what is a State? Environment Input Output motor agent ?

16 Dale Roberts Consider a simple scenario: If I am in my office: if the phone rings; I’ll answer the phone and leave my office if the phone does not ring; I’ll not answer the phone and will stay in the office If I am NOT in my office: if the phone rings; I’ll NOT answer the phone but I’ll come back to my office if the phone does not ring; I’ll not answer it and will stay away from the office A Concept of a State Phone ringI am in office AnswerI will be in office? 1110 0101 1001 1010 Office 0/1 Phone 0/1 Answer the Phone 0/1

17 Dale Roberts Output depends NOT ONLY on the INPUT, but also depends on the internal (current) state of the office (0/1). Let A: is a state when I’m in the office B: is a state when I’m NOT in the office State TableState Transition Diagram Present StateNext State (Output) phone = 0phone = 1 AA, 0B, 1 BB, 0A, 0 AB 1 / 1 1 / 0 0 / 0 in officeout of office

18 Dale Roberts The Turing Machine (TM) Turing Machine is a model of computing agent. It is a pencil-and-paper type model that captures the essential features of a computing agent. A Turing machine consists of 1. a tape that extends infinitely in both directions 2. the tape is divided into cells, each cell can carry a symbol. 3. the symbols comes from a finite set of symbols called the alphabet 4. the alphabet consists of symbols : b (blank), 0, 1, and special symbols X & Y 5. the tape serve as memory 6. has a finite number of k states, 1, …k..bb011bb. 1

19 Dale Roberts A Turing machine can do only one operation at a time. Each time an operation is done, three actions may take place: 1. write a symbol to cell 2. go into a new state 3. move one cell left or right The above actions depends on: The current state of the machine content of cell currently being read (input)

20 Dale Roberts current state next state output input action (direction) The Turing Machine (TM)..bb011bb. 1..bb111bb. 1212 12 0/1 R State: where you are now. R W Your movement output input State Transition: Example: Assume a Turing machine (TM) instruction: if you are in state 1 if you are in state 1and you are reading symbol 0 you are reading symbol 0then write symbol 1 onto tape write symbol 1 onto tape go into state 2 go into state 2 move right move right Written as: (1,0,1,2,R)

21 Dale Roberts The Turing Machine (TM) Example: Design a Turing Machine which will invert the string of binary digits if the input string is 10110 then the output string should be 01001 let us draw a state diagram The TM instruction sets will be (1,0,1,1,R)(1,1,0,1,R) Let the initial configuration be :... b 1 0 1 1 0 b...... b 1 0 1 1 0 b...... b 0 0 1 1 0 b...... b 0 1 1 1 0 b...... b 0 1 0 1 0 b...... b 0 1 0 0 0 b...... b 0 1 0 0 1 b... 1 1/0 0/1

22 Dale Roberts Unary representation is used in order to do any arithmetic operations on a TM. Unary representation looks as follows 0  1 1  11 2  111 3  1111 4  11111 Example: Design a Turing Machine to add 1 to any number start in state 1 if the state is 1 and current input is 1, write 1 and move right and stay in state 1 if the current state is 1 and current input is b, write 1 and move to state 2 and move right and HALT TM instructions: (1,1,1,1,R)(1,b,1,2,R) (2,b,-,-,-) not allowed! Let the initial configuration be:... b 1 1 1 b...... b 1 1 1 b...... b 1 1 1 1 b... Unary Representation for TM 12 b/1 R 1/1 Halt s1 2 in unary representation 3 in unary representation s1 s2

23 Dale Roberts Example: Adding of two non-zero numbers (unary representation) Initial Setup.. b 1 1 1 b 1 1 b.. Initial Setup.. b 1 1 1 b 1 1 b.. Answer should be:.. b b b 1 1 1 1 b.. (1,1,b,2,R): Erase leftmost 1 and move right (2,1,b,3,R): Erase second 1 and move right (3,1,1,3,R): pass over any 1’s until a blank is full (3,b,1,4,R): write 1 over the blank (4,1,1,4,R): pass over remaining 1’s (4,b,b,5,R): halt 12 1/b R 3 R 4 b/1 R 5 b/b R 1/1 Halt S1... b 1 1 1 b 1 1 b... S2... b b 1 1 b 1 1 b... S3... b b b 1 b 1 1 b... S4... b b b 1 1 1 1 b...

24 Dale Roberts Example: Add two numbers, 2 + 3 initial setup would: a ‘ b ’ separate the two numbers.... b 1 1 1 b 1 1 1 1 b... 2 3 2 3 and the expected answer is... b 1 1 1 1 1 1 b... 5 The TM instruction sets will be (1,1,1,1,R)(1,b,1,2,R)(2,1,1,2,R)(2,b,b,3,L)(3,1,b,4,L)(4,1,b,5,L) Let the initial configuration be: S1... b 1 1 1 b 1 1 1 1 b... S2... b 1 1 1 1 1 1 1 1 b... S3... b 1 1 1 1 1 1 1 1 b... S4... b 1 1 1 1 1 1 1 b b... S5... b 1 1 1 1 1 1 b b b... 12 b/1 R 3 b/b L 4 1/b L 5 L 1/1 RR

25 Dale Roberts Example: Add two numbers, 2 + 3 try(1,1,b,2,R)(2,1,b,3,R)(3,1,1,3,R)(3,b,1,4,R) if an initial configuration is b b 1 1 1 b 1 1 1 1 b b.. 12 1/b R 3 R 4 b/1 R 1/1 R S1... b b 1 1 1 b 1 1 1 1 b b... S2... b b b 1 1 b 1 1 1 1 b b... S3... b b b b 1 b 1 1 1 1 b b... S3... b b b b 1 1 1 1 1 1 b b... S4... b b b b 1 1 1 1 1 1 b b...


Download ppt "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Models of Computation Dale Roberts, Lecturer Computer Science,"

Similar presentations


Ads by Google