problem - question we seek an answer for, e.g., "what is the largest item in an array?"
parameters - variables with unspecified values
instance - assignment of values to parameters, i.e., the specific input to the problem
algorithm - step-by-step procedure for producing a solution
Text Notation: text uses a C++ pseudo code
Data Type | Meaning |
keytype | items from an ordered set (we can compare two items for =, <, >, etc.) |
index | integer used as an array index |
number | either integer or real numbers |
bool | a Boolean that can be assigned either "true" or "false" |
Operator | C++ symbol |
and | && |
or | || |
not | ! |
Comparison Operator |
C++ syntax |
x = y | (x == y) |
x y | (x != y) |
x y | (x <= y) |
x y | (x >= y) |
C++ syntax | Meaning |
x = 4; | assignment |
x++ | increment: x = x + 1 |
x-- | decrement: x = x - 1 |
x += 4 | x = x + 4 |
Algorithm 1.1 Sequential Search
Problem: Is the key x in the array S of n keys?
Inputs (parameters): positive integer n, array of keys S indexed from 1 to n, and a key x.
Outputs: location, the location of x in S (0 if x is not in S).
void seqsearch ( int n,
const keytype S[ ],
keytype x,
index & location )
{
location = 1;
while (location <= n && S[location] != x)
location++;
if (location > n)
location = 0;
}
Algorithm 1.2 Add Array Members
Problem: Add all the numbers in the array S of n numbers.
Inputs: positive integer n, array of numbers S indexed from 1 to n.
Outputs: sum, the sum of the numbers in S.
number sum ( int n, const keytype S[ ] )
{
index i;
number result;
result = 0;
for (i = 1; i <= n; i++)
result = result + S [ i ];
return result;
}
basic operation - fundamental operation in the algorithm (i.e., operation done the most)
We want to derive a function for the number of times that the basic operation is performed related to the problem size.
problem size - input size. For algorithms involving arrays, the problem size is the number of elements.
For Add Array Members algorithm, what is the basic operation?
What is the problem size?
Every-case time complexity analysis, T(n) = c1 + c2 n = 100 + 10 n
What determines the length of c1 and c2?
When n is "sufficiently large," the 10 n term dominates. For what value of n, does 10 n = 100?
Big-oh Definition - asymptotic upper bound
For a given complexity function f(n), O( f(n) ) is the set of complexity functions g(n) for which there exists some positive real constant c and some nonnegative integer N such that for all n N,
T(n) = c1 + c2 n = 100 + 10 n is O( n ).
"Proof": Pick c = 110 and N = 1, then 100 + 10 n 110 n for all n 1.
100 + 10 n 110 n
100 100 n
1 n
Problem with big-oh:
If T(n) is O(n), then it is also O(n2), O(n3), O(n3), O(2n), .... since these are also upper bounds.
Omega Definition - asymptotic lower bound
For a given complexity function f(n), ( f(n) ) is the set of complexity functions g(n) for which there exists some positive real constant c and some nonnegative integer N such that for all n N,
T(n) = c1 + c2 n = 100 + 10 n is ( n ).
"Proof": We need to find a c and N so that the definition is satisfied, i.e.,
100 + 10 n c n for all n N.
What c and N will work?
Theta Definition - asymptotic upper and lower bound, i.e., a "tight" bound or "best" big-oh
For a given complexity function f(n), ( f(n) ) is the set of complexity functions g(n) for which there exists some positive real constants c and d and some nonnegative integer N such that for all n N,
T(n) = c1 + c2 n = 100 + 10 n is ( n ).