lec 7
lec 7
CSE308
LECTURE 6: TIME COMPLEXITY
It would seem that the most obvious way to measure the efficiency of an
algorithm is to run it and measure how much processor time is needed
Is it correct ?
2
Factors
Hardware
Operating System
Compiler
Size of input
Nature of Input
Algorithm
4
Analysis of algorithms
Issues:
◦ correctness
◦ time efficiency
◦ space efficiency
◦ optimality
Approaches:
◦ empirical analysis – less useful
◦ theoretical analysis – most important
5
Empirical analysis of time
efficiency
Select a specific sample of inputs
Problems:
6
Empirical analysis of time
efficiency
Select a specific sample of inputs
Problem - Inefficient:
◦ Must implement algorithm
◦ Must run on many data sets to see effects of scaling
◦ Hard to see patterns in actual data
7
Theoretical analysis of time
efficiency
Time efficiency is analyzed by determining the number of repetitions of the
basic operation as a function of input size
Basic operation: the operation that contributes most towards the running time
of the algorithm
input size
T(n) ≈ copC(n)
running time execution time Number of times
for basic operation basic operation is
executed
8
RUNNING TIME OF AN ALGORITHM
Depends upon
◦ Input Size
◦ Nature of Input
9
RUNNING TIME OF AN ALGORITHM
10
Input size and basic operation
examples
Problem Input size measure Basic operation
Matrix dimensions or
Multiplication of two Multiplication of
total number of
matrices two numbers
elements
n’size = number of
Checking primality
digits (in binary Division
of a given integer n
representation)
11
Simple Example
12
Simple Example
13
Simple Example Growth of 5n+3
N = 10 => 53 steps
N = 100 => 503 steps
N = 1,000 => 5003 steps
N = 1,000,000 => 5,000,003 steps
As N grows, the number of steps grow in linear proportion to N for this function
“Sum”
14
What Dominates in Previous Example?
15
Asymptotic Complexity
16
COMPARING FUNCTIONS: ASYMPTOTIC NOTATION
17
Big Oh Notation [1]
f(N) = O(g(N))
18
Big Oh Notation [2]
19
t(n) O(g(n)) iff t(n) <=cg(n) for n > n0
t(n) = 10n3 in O(n3) and in O(n5). What c and n0? More later.
20
O(f(n))
21
Example (2): Comparing Functions
4000
10 n2 Vs n3 3000
2500
10 n^2
2000
n^3
1500
1000
500
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
22
Comparing Functions
0.05 N2 = O(N2)
Time (steps)
3N = O(N)
Input (size)
N = 60
23
Big-Oh Notation
Simple Rule:
Drop lower order terms and constant factors
7n-3 is O(n)
8n2log n + 5n2 + n is O(n2log n)
24
Big Omega Notation
25
t(n) Ω(g(n)) iff t(n) >=cg(n) for n > n0
26
Big Theta Notation
27
t(n) Θ(g(n)) iff t(n)O(g(n)) and
Ω(g(n))
If f(n) = Θ(g(n)) we say that f(n) and g(n) grow at the same rate,
asymptotically
If f(n) = O(g(n)) and f(n) ≠ Ω(g(n)), then we say that f(n) is asymptotically
slower growing than g(n).
If f(n) = Ω(g(n)) and f(n) ≠ O(g(n)), then we say that f(n) is asymptotically
faster growing than g(n).
29
Which Notation do we use?
Why?
If we know the worse case then we can aim to improve it and/or avoid
it.
30
Size does matter[1]
N log2N 5N N log2N N2 2N
8 3 40 24 64 256
16 4 80 64 256 65536
32 5 160 160 1024 ~109
64 6 320 384 4096 ~1019
128 7 640 896 16384 ~1038
256 8 1280 2048 65536 ~1076
31
Complexity Classes
Time (steps)
32
Size does matter[2]
Suppose a program has run time O(n!) and the run time for
n = 10 is 1 second
33
Standard Analysis Techniques
Analyzing Loops
34
Constant time statements
35
Analyzing Loops[1]
36
Analyzing Loops[2]
Treat just like a single loop and evaluate each level of nesting as needed:
int j,k;
for (j=0; j<N; j++)
for (k=N; k>0; k--)
sum += k+j;
Start with outer loop:
◦ How many iterations? N
◦ How much time per iteration? Need to evaluate inner loop
39
Analyzing Nested Loops[2]
What if the number of iterations of one loop depends on the counter of the other?
int j,k;
for (j=0; j < N; j++)
for (k=0; k < j; k++)
sum += k+j;
40
How Did We Get This Answer?
Gauss figured out that the sum of the first n numbers is always:
41
Sequence of Statements
42
Conditional Statements
if (condition)
statement1;
else
statement2;
where statement1 runs in O(n) time and statement2 runs in O(n2) time?
We use "worst case" complexity: among all inputs of size n, what is the maximum
running time?
The analysis for the example above is O(n2)
43
Deriving A Recurrence Equation
So far, all algorithms that we have been analyzing have been non recursive
Example : Recursive power method
44
Iteration Method
46
Answer these questions
Some algs are same for all three (eg all case performance)
53
Example: Sequential search
Worst case
Best case
Average case: depends on assumputions about input: ?
54
Example: Sequential search
Worst case
Best case
Average case: depends on assumputions about input: proportion of found vs not-
found keys
55
Critical factor for analysis: Growth rate
56
Order of growth: Upper, tight, lower bounds
More formally:
- Θ(g(n)): class of functions f(n) that grow at same rate as g(n)
57
Basic asymptotic efficiency classes
1 constant Best case
log n logarithmic Divide
Ignore part
n-log-n or Divide
n log n
linearithmic Use all parts
58