Title: The basic question of Computer Science
1The basic question of Computer Science
- What is computable?
- By asking this we're trying to answer the
question of what computers - can and cannot do. What are their strength?
What are their limits? - Shortest Answer
- a. Have to be able to write an algorithm for the
problem solution. - b. Algorithm cost cannot be too large o(logn),
O(n), O(n logn), O(nk).
2CS Concepts Computational Power
- Computational power refers to the types of
problems a computer can solve. - It has nothing to do with how fast a computer is
nor how nice the graphics are. - It turns out very simple computers are just as
powerful than very expensive ones (although
slower). - So in terms of computational power, all computers
are equal. - This allows generalized solutions - solve a
problem for one and we solve it for all. - Since all computers are equal, we tend to focus
on types of problems rather than types of
computers. - Is the problem computable?
3Be able to Identify Patterns of Doubling/Halving
- Consider two math operations taking a number to
a power, and taking the log of a number. - We will focus on the number 2 (binary no
surprise)
4Example Color Depth
- How many colors are available if each pixel uses
n bits? - How many bits are required for 256 colors?
- 8 bits, because log2 256 8
- Thats one byte coincidence???
5 A Computer Model
Processor Model A black box that performs
specific instructions representing discrete
arithmetic and logical operations.
Memory Model Memory is composed of a single row
of storage locations, each referenced by a
numerical address.
6 Algorithm Basics
- Definition A step by step procedure written so
precisely that there is essentially no
possibility for variation in its performance. - Written in pseudo-code
- Part English, part computer code
- Generic enough to be easily translated into most
computer languages. - Five parts, each must be detailed
- A list of inputs and their types
- A list of other variables and their types
- Initialization
- Computation
- A list of outputs and end conditions
- If given a simple algorithm be able to discuss it
and how it operates.
7Why Algorithms are Important
- Algorithms have no mention of a computer.
- Remember that CS focuses on the computation not
the machine. - Thus, an algorithmic solution to a problem
means the problem is solved for every computer - Well find a fast machine if we have to.
- Each step in the algorithm was precise
- Therefore each step can be described and measured
in terms of cost. - This helps us answer the two big questions Can
it be computed and at what cost? - Lastly Algorithms can be abstracted into
functions.
84 Properties of Good Algorithms
- Correctness
- The algorithm works correctly for all possible
inputs.. - Precision
- The algorithms procedures are specified in such
a way that the same actions are taken no matter
who is performing it (Notice what the book says
about that on p 32) - Incremental Operation
- Each step specified should consist of a single
logical step. - Abstraction
- Abstraction allows for many incremental steps to
be logically grouped into single step.
9Example Algorithm Problems
- What inputs are required?
- If the program written from this algorithm was
executed with x 12, y 10 as input, what would
the output be? - Pretend the algorithm was turned into a function,
foo(x, y). If used in a program like below, what
is the type of "myvar?" - myvar foo( 12, 10)
Algorithm foo(x, y) Inputs Integer x
Number y Others Number z Initialization
Assume x, y, given to us. Computation IF ( y gt
x ) THEN z y / 2 ELSE z (x y) /
2 ENDIF Output return z
10Definition of Function
Function A function is a black box that takes
input, operates on it, and produces output. A
function is fully defined by three things
input, output, and a description relating the
output to the input.
Example The Add1 function Input - a number,
x. Output - a number, F(x) Operation - F(x) x
1
11Function Composition
Add2
- Function composition when simple functions are
combined to form more complex functions. - Definition of Add2
- Input A number, X
- Output A number, F(x)
- Operation F(x) x 2
12Why are Functions Important?
- Simple functions are the building blocks of all
computation. - Remember our model of a Processor allows only
simple operations. - Real Processors allow only simple operations.
- Simple functions are easily described.
- Iteration One function repeatedly calling
another. - Recursion One function repeatedly calling
itself. - We can build large programs by composing small
functions. - We can describe large programs by describing the
functions that make them up and how they are
composed.
13Functions are Abstract Algorithms
- Algorithms take in input, operate on it, and
produce output. - Functions take in input, operate on it, and
produce output. - When we speak of functions we focus on WHAT is
being done. - When we speak of algorithms we focus on HOW
things are done. - Algorithms can specify how functions produce
their results. - Many different algorithms may specify a single
function. - Functions can be thought of as abstract
algorithms. - Many algorithms can map to the same function.
14Order Notation
- ORDER NOTATION allows us to relate computational
cost to problem size. - Most input data can have a size associated with
it, like the size of a list. - Be able to compare relative sizes of different
orders - O(k) lt O(log n) lt O(n) lt O(nk) lt O(kn) lt O(n!)
15Why Use Order Notation
- The order (or cost) of a computation tells us
whether it is computable or not - All O(k) and O(n) problems are computable.
- O(nk) problems are computable ( usually k 1,
2, or 3) - O(kn) are not computable (except for very small
n). - Order notation allows us to compare the
efficiency of different algorithms that have been
developed to solve the same problems. - Order notation allows us to compare the
efficiency of our algorithms with known costs for
problems. - You dont want the cost of your algorithm to be
larger than the known best costs for a problem. - You cannot make an algorithm lower than the
proven best cost.
16Program Control Introduction
- A list of program statements that always execute
in sequence from top to bottom is called a BASIC
BLOCK. - Very few interesting programs can be written
using only basic blocks. - Interesting programs have the ability to react to
different input data. - PROGRAM CONTROL statements allow us to change how
the program behaves based on decisions made about
the input data. - Two basic program control structures are
- If-Then-Else structures which act like forks in
the road during program execution. The program
either takes the True branch or False branch. - Do-Loops which allow the same program statements
to be executed over and over in a looping style.
17Program Control If-Then -Else
- The If-Then-Else structure has three parts
- A predicate that must evaluate to True or False
- Something to do when the predicate is True
- Something to do when the predicate is False
- (Note Doing nothing counts as something to do)
- An example in our algorithmic pseudo-code
- IF ( X gt Y) THEN
- print(x)
- ELSE
- print(y)
- ENDIF
- Notice We needed a way to tell when we were
done ENDIF.
18Program Control Do-Loop
- Do-Loops in JavaScript
- Each loop requires three things an initial
value, a stopping condition, and a way to change
the looping variable. - The format for this is for(initial value stop
condition increment rule) - Example
- sum 0
- For ( j 1 j lt 5 j j 1)
- sum sum arrayj
- In our example
- The initial value was set by j 1
- The stopping condition was j lt 5 (so j would be
1, 2, 3, and 4) - The increment rule increased the value of j by 1
each tie through the loop ( JavaScript note j
is shorthand for j j 1)
19 Variables
- Why use variables? It would be almost impossible
to try to program by referring to the numerical
addresses. - Too complicated for the human. ( add memory
location 1 and memory location 8 ) - Different machines have different memory
structures. - Three things describe a variable name, type and
its structure. - The act of specifying a new variable is called
declaring the variable.
20 Variable Scope
- Scope refers to the portions of a program that
may legally reference a given memory location
referred to by a variable. - Global scope indicates the memory location is
always accessible. - Local scope means the memory location is only
available during certain portions of the program
execution. - Memory reserved within functions is only
available while the function is executing. - This means that variable names defined within a
function may only be used while the function is
executing. - If a single variable name is repeated, the memory
location being accessed by using the variable
name is the most local definition.
21Cost of Typical Problems