Professional Documents
Culture Documents
Asymptotic Analysis
Asymptotic Analysis
Asymptotic Analysis
6
Big-Oh Examples
Example 1: [Sequential Search] Finding value X in an array
(average cost). For T(n) a non-negatively valued
function, T(n) is in the set O(f(n))
Then T(n) = csn/2.
if there exist two positive
For all values of n > 1, csn/2 <= csn. constants c and n0 such that T(n)
Therefore, the definition is satisfied for: <= cf(n) for all n > n0.
f(n)=n,
n0 = 1, and
c = cs.
Hence, T(n) is in O(n).
7
Big-Oh Examples (2)
Example 2: Suppose T(n) = c1n2 + c2n, where c1 For T(n) a non-negatively valued
and c2 are positive. function, T(n) is in the set O(f(n))
if there exist two positive
c1n + c2n <= c1n + c2n <= (c1 + c2)n for all n > 1.
2 2 2 2
constants c and n0 such that T(n)
Then T(n) <= cn2 whenever n > n0, for c = c1 + c2 <= cf(n) for all n > n0.
and n0 = 1.
9
Big-Omega
Definition: For T(n) a non-negatively valued
function, T(n) is in the set (g(n)) if there exist
two positive constants c and n0 such that T(n)
>= cg(n) for all n > n0.
Lower bound.
10
Big-Omega Example
For T(n) a non-negatively
valued function, T(n) is in the
T(n) = c1n2 + c2n. set (g(n)) if there exist two
positive constants c and n0
c1n2 + c2n >= c1n2 for all n > 1. such that T(n) >= cg(n) for all
T(n) >= cn2 for c = c1 and n0 = 1. n > n0.
12
Theta Notation From previous analysis:
For sequential search, we have
When big-Oh and coincide, we indicate this by
T(n) is in O(n)
using (big-Theta) notation.
&
T(n) is in (n).
Definition: An algorithm is said to be in (h(n)) if it
is in O(h(n)) and it is in (h(n)). So, T(n) is in (n).
sum = 0;
Repeat n times (n)
(1) (n)
for (i=1; i<=n; i++)
sum += n;
15
Time Complexity Examples (2)
Example 3.11:
(1)
sum = 0;
for (j=1; j<=n; j++)
for (i=1; i<=j; i++)
(1)
sum++;
Repeat n times
for (k=0; k<n; k++) (1) (n)
A[k] = k;
16
Time Complexity Examples (2)
1 for (j=1; j<=n; j++) Outer loop (1) Executes n times
2 for (i=1; i<=j; i++) Inner loop (2) Executes j times depending on
outer loop counter, j.
3 sum++; c j changes (increases) at each iteration
Initially j is 1
How many times Line 3 is executed?
Then j is 2
…
1+2+ … + n = n(n+1)/2. Finally, j is n
So, cost for Line 3 is c n(n+1)/2
= cn^2/2 + cn/2,
which is in (n^2).
17
Time Complexity Examples (2)
Example 3.11:
(1) (1)
sum = 0;
for (j=1; j<=n; j++)
for (i=1; i<=j; i++) (n^2) (n^2)
(1)
sum++;
Repeat n times
for (k=0; k<n; k++) (1) (n)
A[k] = k;
18
Time Complexity Examples (3)
Example 3.12:
sum1 = 0;
for (i=1; i<=n; i++)
for (j=1; j<=n; j++) (n^2)
sum1++;
sum2 = 0; (n^2)
for (i=1; i<=n; i++)
for (j=1; j<=i; j++) (n^2)
sum2++;
19
Time Complexity Examples (4)
Example 3.13:
sum1 = 0;
for (k=1; k<=n; k*=2)
for (j=1; j<=n; j++)
sum1++;
sum2 = 0;
for (k=1; k<=n; k*=2)
for (j=1; j<=k; j++)
sum2++;
20
Time Complexity Examples (4)
Example 3.13: How many times this loop iterates?
Assume n is a power of 2.
Suppose we reach n at the mth iteration
sum1 = 0; n = 2^m
for (k=1; k<=n; k*=2) log_2 n = m.
for (j=1; j<=n; j++) Since we start at 1 = 2^0. total iteration is
log n + 1 [we will usually assume the base is 2],
sum1++; which is in: (log n)
sum2 = 0;
for (k=1; k<=n; k*=2)
for (j=1; j<=k; j++)
sum2++;
21
Time Complexity Examples (4)
sum1 = 0; (1)
Repeats n times
es
for (j=1; j<=n; j++)
m
) ti
+1
sum1++; (1)
gn
lo
n(
sum2 = 0;
for (k=1; k<=n; k*=2)
for (j=1; j<=k; j++)
(1)
sum2++;
22
Inner loop (2)
1 for (k=1; k<=n; k*=2) Iteration 1: k = 1; executes 1 = 2^0 times
Iteration 2: k = 2; executes 2 = 2^1 times
2 for (j=1; j<=k; j++) Iteration 3: k = 4: executes 4 = 2^2 times
Iteration 4: k = 8: executes 8 = 2^3 times
…
3 sum2++; c Iteration log n + 1: k = n; executes n = 2^{log n} times
which is (n).
Time Complexity Examples (4)
sum1 = 0; (1)
es
for (j=1; j<=n; j++)
m
) ti
+1
sum1++; (1)
gn
lo
(n log n)
n(
sum2 = 0;
for (k=1; k<=n; k*=2)
Repeats (n) times
(n)
for (j=1; j<=k; j++)
(1)
sum2++;
24
Binary Search
28
Problems
• Problem: a task to be performed.
• Best thought of as inputs and matching outputs.
• Problem definition should include constraints on the
resources that may be consumed by any acceptable
solution.
29
Problems (cont)
• Problems mathematical functions
• A function is a matching between inputs (the domain)
and outputs (the range).
• An input to a function may be single number, or a
collection of information.
• The values making up an input are called the
parameters of the function.
• A particular input must always result in the same
output every time the function is computed.
30
Algorithms and Programs
Algorithm: a method or a process followed to solve
a problem.
• A recipe.
31
Analyzing Problems
Upper bound: Upper bound of best known algorithm.
32
Analyzing Problems: Example
May or may not be able to obtain matching upper
and lower bounds.
Consider Sorting:
34
Multiple Parameters
Compute the rank ordering for all C pixel values in a
picture of P pixels.
for (i=0; i<C; i++) // Initialize count
count[i] = 0;
for (i=0; i<P; i++) // Look at all pixels
count[value(i)]++; // Increment count
sort(count); // Sort pixel counts
35
Space Complexity
Space complexity can also be analyzed with
asymptotic complexity analysis.
Time: Algorithm
Space: Data Structure
36
Common Misunderstandings
“The best case for my algorithm is n=1 because
that is the fastest.” WRONG!
Big-oh refers to a growth rate as n grows to .
Best case is defined for the input of size n that is
cheapest among all inputs of size n.
37
Common Misunderstandings
Confusing worst case with upper bound.
38
Intuition for Asymptotic Notation
big-Oh
f(n) is in O(g(n)) if f(n) is asymptotically less than or equal to g(n)
big-Omega
f(n) is in (g(n)) if f(n) is asymptotically greater than or equal to g(n)
big-Theta
f(n) is in (g(n)) if f(n) is asymptotically equal to g(n)