Professional Documents
Culture Documents
Analysis
n Algorithm Analysis:
q How to predict an algorithm’s performance
n Data Structures:
q How to efficiently store, access, manage data
COMP2015 HKBU/CS/JF/2023 1
Machine Independent Analysis
n A program segment to calculate
int Sum(int N)
{
int i, PartialSum;
PartialSum = 0;
for (i = 1; i <= N; i++)
PartialSum += i*i*i;
return PartialSum;
}
COMP2015 HKBU/CS/JF/2023 2
Machine Independent Analysis
n In fact, we will not worry about the exact values, but will
look at “broad classes” of values, or the growth rates.
COMP2015 HKBU/CS/JF/2023 3
Asymptotic Analysis
n Asymptotic analysis
q A way to describe behavior of functions in the limit, for
example, upper bound or lower bound of functions.
q Look at growth of T(N) as N infinity
q Establish a relative order among functions for large N
COMP2015 HKBU/CS/JF/2023 4
Asymptotic Notations
n Common notations:
q O » ≤
q W » ³
q Q » =
q o » <
q w » >
COMP2015 HKBU/CS/JF/2023 6
Asymptotic Notation: Big-Oh
n T(N) = O(f(N))
n There exist positive constants c and n0
such that
0 ≤ T(N) ≤ cf(N) for all N ≥ n0
COMP2015 HKBU/CS/JF/2023 7
Asymptotic Notation: Big-Omega
n T(N) = W(g(N))
COMP2015 HKBU/CS/JF/2023 8
Asymptotic Notation: Big-Theta
n T(N) = Q(h(N))
Ø T(N) = o(p(N))
for all constants c > 0, there exists a constant n0 > 0 such that
0 ≤ T(N) < c p(N) for all N ≥ n0
n o»<
COMP2015 HKBU/CS/JF/2023 10
Asymptotic Notation
n w Notation: Asymptotically bounds a function strictly
from below only
n T(N) = w(q(N))
for all constants c > 0, there exists a constant n0 > 0 such
that
0 ≤ c q(N) < T(N) for all N ≥ n0
n w»>
COMP2015 HKBU/CS/JF/2023 11
Asymptotic Notations
n Intuitive meaning:
q T(N) = O(f(N)): the growth rate of T(N) £ that of f(N);
COMP2015 HKBU/CS/JF/2023 12
Asymptotic Notations
n Examples:
if g(N) = 2N2, are g(N) = O(N4), g(N) = O(N3), and g(N) = O(N2)
all correct?
COMP2015 HKBU/CS/JF/2023 13
Growth of Functions
COMP2015 HKBU/CS/JF/2023 14
Complexity and Tractability
COMP2015 HKBU/CS/JF/2023 15
Growth Rate of Different Functions
(log N) < (N) < (N logN) < (N2) < (N3) < (2N) < (N!)
COMP2015 HKBU/CS/JF/2023 16
Simplifying the Bound
How to derive f(N) from T(N)?
q too complicated
q too many terms
q Difficult to compare two expressions, each with 10 or 20
terms
COMP2015 HKBU/CS/JF/2023 17
Simplifications
n Keep just one term!
q The fastest growing term (dominates the running time)
q Machine-dependent constants
q Example:
n N2 + 3N + 4 = O(N2)
n 3N + 5 = O(N)
COMP2015 HKBU/CS/JF/2023 19
Relative Growth Rates of Two
Functions
n To compare the growth rates of two functions f(N) and g(N)
q use L’Hopital’s rule to evaluate:
COMP2015 HKBU/CS/JF/2023 20
Example
COMP2015 HKBU/CS/JF/2023 21
Question
COMP2015 HKBU/CS/JF/2023 22
Asymptotic Algorithm Analysis
COMP2015 HKBU/CS/JF/2023 23
Asymptotic Analysis: Review
n n: Problem size
n Big-O: upper bound over all inputs of size n
n Highlight those dominant terms
n “Ignore constant factor”
n “as n grows large”
COMP2015 HKBU/CS/JF/2023 24
Asymptotic Analysis: Review
n Big-O Rules
COMP2015 HKBU/CS/JF/2023 25
Asymptotic Analysis: Review
n Big-O Rules
• Examples:
if T1(N) = O(N2) and T2(N)= O(N) then
(a) T1(N) + T2(N) = O(N2)
(b) T1(N)*T2(N) = O(N3)
COMP2015 HKBU/CS/JF/2023 26
Questions
COMP2015 HKBU/CS/JF/2023 27
Algorithm Complexity Analysis
Complexity of a loop:
diff = sum = 0;
for (k=0; k < N; k++) Each iteration of the first loop takes 2
basic steps.
sum ® sum + 1;
First loop runs N times.
diff ® diff - 1;
Each iteration of the second loop takes 1
for (k=0; k < 3N; k++) basic step.
sum ® sum - 1; Second loop runs for 3N times.
Overall, 2N + 3N steps
This is O(N)
COMP2015 HKBU/CS/JF/2023 28
Running Time Calculations: General
Rules
COMP2015 HKBU/CS/JF/2023 29
Running Time Calculations: General
Rules
n Nested for loops:
sum = 0;
Inner loop: O(N)
for (i=0; i < N; i++)
for (j=0; j < N; j++) Outer loop: N iterations
COMP2015 HKBU/CS/JF/2023 30
Running Time Calculations: General
Rules
COMP2015 HKBU/CS/JF/2023 31
Running Time Calculations: General
Rules
COMP2015 HKBU/CS/JF/2023 32
Running Time Calculations: General
Rules
if (condition)
S1
else
S2
COMP2015 HKBU/CS/JF/2023 33
Example
Sum=0
for (j=0; j<N; j++)
for (k=0; k<N*N; k++) O(N3)
Sum++;
Sum=0
for (j=0; j<N; j++)
for (k=0; k<j; k++) O(N2)
Sum++;
COMP2015 HKBU/CS/JF/2023 34
Example: Binary Search
n Analysis of Binary Search Problem:
Given an integer X and integers A0,
while (low <= high) A1, …, AN-1, which are presorted
{ mid = (low + high) / 2; and already in memory, find i such
if (x == a [mid]) that Ai = X, or return i = -1 if X is
not in the input.
return (mid);
else if (x < a [mid])
high = mid - 1;
else O(logN)
low = mid + 1;
}
COMP2015 HKBU/CS/JF/2023 35
Question
COMP2015 HKBU/CS/JF/2023 36