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

29 selected ideas from cs 1 the stored program concept
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

1

29: Selected Ideas from CS

  • 1. The stored program

Concept

  • Breaks away from the idea of a particular

machine that does a particular task

  • Precursor: Jacquard loom – patterns

embedded in punched cards

  • Machine executes simple instructions

which together build a complex task

Computers

  • Everything an actual computer can do is the

result of extremely simple arithmetic and logical

  • perations
  • At the ground level, even these simple
  • perations must be specified algorithmically

(e.g. “how to add two numbers”

  • 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

  • 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’

  • 2. The Algorithm
slide-2
SLIDE 2

2

What is an Algorithm?

  • Quite generally, an algorithm is a set of

instructions that lead to some desired result

  • Must first decompose a problem, and

then express it’s method of solution as a series of well defined steps to be followed.

  • So an algorithm is a recipe for achieving a

certain goal

Pancake Recipe

  • 1. Mix 1 egg and 11/2 cups of milk
  • 2. Add to 2 cups of sifted flower
  • 3. Stir until smooth
  • 4. Mix in 2 tablespoons of melted butter
  • 5. Pour some of the mixture into a hot pan

and fry

  • 6. Serve with maple syrup, sugar or jam

It’s All Relative

  • Whether or not a list of instructions counts

as an ‘algorithm’ is relative to a set of background assumptions about what kind

  • f device will be following the instructions,

what its basic capacities are, etc.

  • Can fill in steps between steps as the level
  • f analysis becomes more and more fine

grained

Where to begin

  • For example, the Pancake recipe doesn’t

include an initial step:

  • 0. place an adequately sized mixing bowl
  • n a flat surface

And it doesn’t tell you how to sift the flower

  • r melt the butter, etc.
  • these steps between steps are simply

assumed

Effective Procedures

In the most rigorous context, algorithms are the recipes that a computer can follow

  • In this sense they can be thought of as

effective procedures (we’ve seen these before): – a finitary set of instructions that can be followed ‘mechanically’, i.e. with no understanding or interpretation required

World Knowledge

In this most precise sense of an effective procedure, there can be no missing steps or ambiguities

  • ‘Common sense’ or ‘world knowledge’ is a central

aspect of human intelligence not shared by computers

  • One of the most difficult aspects of designing

algorithms is to break down a problem in such a way that the solution really is mechanical, relative to its level of execution

slide-3
SLIDE 3

3

Pancake Bugs

  • So even though the average

(contemporary western?) human could follow the pancake recipe, it assumes too much world knowledge for a computer

  • Has a number of bugs as a potential

program: e.g. step 1. says to mix 1 egg with milk, but it doesn’t say not to include the shell

More Bugs

Step 2. Add to 2 cups of sifted flower But add what – more flower, red wine, marmite? Step 3. Stir until smooth But with what, and exactly how smooth – can the stirring go on for 3 hours? Step 5. Pour some of the mixture into a hot pan and fry - until when?

  • 3. The automaton

The Finite State Automaton

Up Down Up Down Builder’s Hoist Top station Bottom station

End stop (limit switch) End stop (limit switch)

Control panel

Things can happen

  • Workman at top calls lift Up
  • Workman at top sends lift Down
  • Workman at bottom sends lift Up
  • Workman at bottom calls lift Down
  • Lift hits top limit switch
  • Lift hits bottom limit switch

Automaton has states

  • Lift is on the way up
  • Lift is on the way down
  • Lift is at the top station
  • Lift is at the bottom station
slide-4
SLIDE 4

4

State Machine

At top At bottom Going up Going down Down pressed Hits top limit Hits bottom limit Up pressed State transition diagram nodes edges

More complete state machine

At top At bottom Going up Going down Down pressed Hits top limit Hits bottom limit Up pressed Up pressed Down pressed Down pressed Up pressed State transition diagram

State transition table

At top !

  • Top limit

switch

  • Going

up Going up

  • ‘Up’
  • At

bottom ! Bottom limit switch Going down

  • Going

down ‘Down’ Going up At bottom Going down At top

The two cases ‘!’ should not occur but neglecting them will cause system to fail If someone kicks the limit switch

Cellular Auomata

  • Collections of finite-state automata
  • Very simple rules, applied repeatedly
  • Result is complex behaviour
  • John Horton Conway’s ‘game of life’:

– http://www.bitstorm.org/gameoflife/

  • Other cellular automata:

– http://math.hws.edu/xJava/CA/EdgeOfChaos CA.html

  • 4. Recursion

Recursion

  • Factorial n, written n!
  • = (n) x (n-1) x (n-2) … 1
  • 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
slide-5
SLIDE 5

5

Many recursive solutions

  • Traverse a tree

Visit the left subtree Visit the node Visit the right subtree

Divide-and-conquer

  • I want to find the RAC in the phone book.
  • 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
  • For n pages this will find it in ≤ Log2 n steps

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 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
  • 5. Computational Complexity

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

n2 compared with n log n

  • For n=1,000 that is 1,000,000 versus 20,000