29 selected ideas from cs 1 the stored program concept
play

29: Selected Ideas from CS 1. The stored program Concept Computers - PDF document

29: Selected Ideas from CS 1. The stored program Concept Computers Breaks away from the idea of a particular Everything an actual computer can do is the result of extremely simple arithmetic and logical machine that does a particular


  1. 29: Selected Ideas from CS 1. The stored program Concept Computers • Breaks away from the idea of a particular • Everything an actual computer can do is the result of extremely simple arithmetic and logical machine that does a particular task operations • Precursor: Jacquard loom – patterns • At the ground level, even these simple embedded in punched cards operations must be specified algorithmically • Machine executes simple instructions (e.g. “how to add two numbers” which together build a complex task • In this respect computers are exceptionally stupid – but they make up for it by being extremely fast and accurate Programs • Most of the time we’re concerned with problems and their procedural solutions at a much higher level of analysis 2. The Algorithm • In such contexts, the solution might first be expressed as a set of step-by-step instructions in a natural language such as English • Then the algorithm must be refined as a set of instructions appropriate to some high level programming language – • This gives the context relative to which the instructions must be ‘ mechanical ’ 1

  2. What is an Algorithm? Pancake Recipe • Quite generally, an algorithm is a set of 1. Mix 1 egg and 1 1 / 2 cups of milk instructions that lead to some desired 2. Add to 2 cups of sifted flower result 3. Stir until smooth • Must first decompose a problem, and 4. Mix in 2 tablespoons of melted butter then express it’s method of solution as a 5. Pour some of the mixture into a hot pan series of well defined steps to be followed. and fry • So an algorithm is a recipe for achieving a 6. Serve with maple syrup, sugar or jam certain goal It’s All Relative Where to begin • Whether or not a list of instructions counts • For example, the Pancake recipe doesn’t as an ‘algorithm’ is relative to a set of include an initial step: background assumptions about what kind 0. place an adequately sized mixing bowl of device will be following the instructions, on a flat surface what its basic capacities are, etc. And it doesn’t tell you how to sift the flower • Can fill in steps between steps as the level or melt the butter, etc. of analysis becomes more and more fine - these steps between steps are simply grained assumed Effective Procedures World Knowledge In this most precise sense of an effective In the most rigorous context, algorithms procedure, there can be no missing steps or are the recipes that a computer can follow ambiguities • In this sense they can be thought of as - ‘ Common sense ’ or ‘ world knowledge ’ is a central effective procedures (we’ve seen these aspect of human intelligence not shared by computers before): • One of the most difficult aspects of designing – a finitary set of instructions that can be algorithms is to break down a problem in such a followed ‘mechanically’, i.e. with no way that the solution really is mechanical, relative to its level of execution understanding or interpretation required 2

  3. Pancake Bugs More Bugs • So even though the average Step 2. Add to 2 cups of sifted flower (contemporary western?) human could But add what – more flower, red follow the pancake recipe, it assumes too wine, marmite? much world knowledge for a computer Step 3. Stir until smooth • Has a number of bugs as a potential But with what, and exactly how smooth program: e.g. – can the stirring go on for 3 hours? step 1. says to mix 1 egg with milk, Step 5. Pour some of the mixture into a hot but it doesn’t say not to include pan and fry - until when? the shell The Finite State Automaton End stop Up Control panel Top station (limit switch) Down 3. The automaton Builder’s Hoist End stop Up (limit switch) Bottom station Down Things can happen Automaton has states • Workman at top calls lift Up • Lift is on the way up • Workman at top sends lift Down • Lift is on the way down • Workman at bottom sends lift Up • Lift is at the top station • Workman at bottom calls lift Down • Lift is at the bottom station • Lift hits top limit switch • Lift hits bottom limit switch 3

  4. State Machine More complete state machine nodes At top At top Up pressed edges Down pressed Down pressed Hits top limit Hits top limit Down pressed Going up Going down Going up Going down Up pressed Up pressed Hits bottom limit Up pressed Hits bottom limit Down pressed At bottom At bottom State transition diagram State transition diagram State transition table Cellular Auomata At top Going At Going • Collections of finite-state automata down bottom up • Very simple rules, applied repeatedly ‘Down’ Going - - Going down down • Result is complex behaviour Bottom limit ! At - - • John Horton Conway’s ‘game of life’: switch bottom – http://www.bitstorm.org/gameoflife/ ‘Up’ - Going Going - • Other cellular automata: up up – http://math.hws.edu/xJava/CA/EdgeOfChaos Top limit - - ! At top CA.html switch The two cases ‘!’ should not occur but neglecting them will cause system to fail If someone kicks the limit switch Recursion • Factorial n, written n! • = (n) x (n-1) x (n-2) … 1 4. Recursion • Factorial 1 = 1! = 1 (end point) • Factorial 2 = 2! = 2 x 1 = 2 x 1! • Factorial 3 = 3! = 3 x 2 x 1 = 3 x 2! • Factorial n = n! = n x (n-1)! • Recursion: recurrence relation + end point 4

  5. Many recursive solutions Divide-and-conquer • I want to find the RAC in the phone book. • Traverse a tree • Business directory is 257 pages • Take the mid point. Have I found it? • No. Am I above or below it? • Take whichever half contains the RAC • Recurse with a directory of 128 pages • Then 64 pages .. Then 32 .. • Keep going till I find it or have 1 page left Visit the left subtree Visit the node • For n pages this will find it in ≤ Log 2 n steps Visit the right subtree Quicksort • Pick an element, called a pivot , from the list. • Reorder the list so that all elements which are less than the pivot come before the pivot and so 5. Computational Complexity that all elements greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. • Recursively sort the sub-list of lesser elements and the sub-list of greater elements. (Wikipedia) • This can finish in n log n steps Computational Complexity • “Find the RAC” example: • I could have started at page 1 and kept going. Would expect to find it in n/2 steps. • Complexity of order ‘n’ • For n = 1,000,000 a linear search takes n/2 steps. A divide-and-conquer sort takes 20. • Intuitive ‘Insertion sort’ has complexity of order n 2 compared with n log n • For n=1,000 that is 1,000,000 versus 20,000 5

Recommend


More recommend