Usually there is more than one way to solve a problem.
This means there is more than one solution...How do we judge an algorithm? I like these criteria from the book:
There are other criteria that have a more direct relationship to performance.
Compare
Express complexity as a function of the "size" of a problem
Space Complexity: amount of memory a program needs to run to completion
(in
book see page 15; I will focus on time but sometimes bring in space issues)
Time Complexity: amount of computer time a program needs to run to completion
When we measure time and space complexity of a program, we need to consider what happens as the number or size of data (for input or output) increases...i.e., what are the characteristics of the data for this instance which will affect the values for time and space?
Time analysis is based on the size of the data set.
One Example: the length of a string affects the time it takes to search for a character in a string
Basically, the measure of the size of a problem depends on the number of data items.
T (n) = n reads + n additions
anything else?
To do time complexity, we do not want to simply measure the run of a
program, but to determine mathematically how changes in data (for the given
procedure) will change the time.
What happens as the size increases ?
Asymptotic (worst case) complexity:
Algorithm ADD
ACCUM = 0
while (more data items) do
read data item
ACCUM = ACCUM + data item
write ACCUM
n = number of data items
Algorithm ADDPOS
/* add positive data items */
ACCUM = 0
while (more data items) do
read data_item
if data_item > 0
then ACCUM = ACCUM + data_item
T (n) = n reads + n comparisons + (<) n additions
| Code | What it counts as | |
| comments | 0 | |
| declarative statements | 0 | |
| assignments | 1 (unless function calls) | |
| iterative statements | 0 or 1 (checking the <expr>) |
step counts are useful since they tell us how the run time changes with instance characteristics (e.g., for i = 1 to n ... if n doubles, time doubles)
Note: Step counts do not necessarily reflect the complexity of the statement. For example some books count a function call as one step count
Steps/execution (s/e) is the amount of which the count changes as a result of execution (see page 19-24 and Sum(a,n))
The time complexity of a program is given by the number of steps taken by the program to compute the function it is written for. (i.e., s/e)
[Do some T(n) ]
Use "Big O" to describe time (or space) complexity. "O" for order of the algorithm.
Once an expression is obtained based on the data set, the order of the algorithm is given by the dominating term.
For example:
Thus, it is easy to see that the coefficients or constants are insignificant.
Asymptotic Notation
look at Example 1.11 on page 29
e.g. Suppose T (0) = 1
Verify:
T(n) is O(n2 ) for some program means there are positive constants c
and no such that for n > no we have T(n) < cn2
verify T(n) = 3n3 + 2n2 is O(n3)
(what does that mean?)
is T(n) also O(n4) ?
* Prove Thm 1.2 (page 30)... makes polynomials easy
(can always get a big enough c, no )
Theorem: If f(n)=amnm + ... + a1n + a0 , then f(n) = O(nm).
Proof:
So, f(n) = O(nm) (assuming that m is fixed)
DONE
WHY?
So, given:
One can always "play" for a proper no and c. You only need one to work to show the definition holds, so pick one that is easy to allow you to verify.
f(n) = O(g(n)) says g(n) is an upper bound ... does not say how good.
To be informative, want smallest (least upper bound)
another example:
What about
Omega
page 30
examples page 30
verify
Theta
verify
Examples 1.13
if upper = lower --> algorithm for this function is "best possible"
only way to make faster is faster hardware
Compare rates of growth for n, n2, n3, log n, 2n, n log n
See pages 38, 39, 40
MIT slides
Practice: calculate T(n)
Now get O(n) and verify
T(1) = 4
in general
T(2) = 9
T(n) = (n + 1) 2
then
T(n) is O(n2)
let no = 1 and c = 4
(why not n3 ) (lub)
for n > 1,
(n +1)2 < 4n2
T(n) = 3n3 + 2n2 < cn3 let
c= 5, no = 1, then for all n > 1
Discuss
3n3 + 2n2 < 5n3
2n2<2n3
n2< n3
1 < n
hints for proof:
then
n * fraction < n* 1.
i.e.,
6n3 + 5n2 +4n + 2 < 6n3 +
5n3 + 4n3 +2n3 = 17n3
Remember:
f(n) = O(g(n)) iff there exists positive constants c and no such that
f(n) < cg(n) for all n , n > no
Our g(n) is nm, let 6n3 + 5n2 +4n + 2 O(?) and what might c be?
Often, we use no = 1 since it makes things easy to see.
4n + 2 O(?) and what might c be?f(n) = 1000n2 + 100n - 6 < 1001n2 for
n > 100
(what is no and what is c?)f(n) = 6 * 2n + n
f(n) = 6 * 2n + n is O(2n) since
f(n) = 6 * 2n + n < 7 * 2n for n
> 4 (what is c, no?)
Additional materials to demonstrate handout betterrun in time independent of inputs
time proportional to length of input
if double length => double timeSee handout for calculations of recursive algorithms (Data Structures and Algorithms, Aho, Hopcroft, and Ullman, Addison-Wesley, 1983, ISBN 0-201-00023-7, Chapter 9)
iff there exists positive constants c and no such that
f(n) > cg(n) for all n , n > no
(g is lower bound)
(what does that mean?)
iff there exists positive constants c1, c2 and no
such that c1g(n) < f(n) < c2g(n) for all n , n
> no
(what does that mean?)