Professional Documents
Culture Documents
Algorithm Efficiency
Keun-Liang Sue
Information Management
NCU
2023/3/9 1
Analysis Framework
Time efficiency
How fast an algorithm in question runs.
Primarily concentrated.
Space efficiency
The extra space the algorithm required.
b log 2 n 1
2023/3/9 Algorithms by KLSue/NCU 3
Define :
cop:Execution time of an algorithm’s basic
operation.
Note
Answer it without knowing the value of cop
Orders of Growth
logarithmic function(log2n).
Function growing the slowest among these.
Orders of Growth
Why log2n? Can be log n?
Sure!!
Can omit a logarithm’s base and write simply logn.
See the formula
Bad functions
Exponential function 2n and factorial function n!
Grow so fast
The value become large even for small value
Both referred to as “exponential-growth functions”
Remember :
• “Algorithms that require an exponential number of
operations are practical for solving only problems of
very small sizes”
(0 p 1)
prob. of first match occurring in ith position of the list
is the same for every i.
For examples:
1
n O(n 2 ), 100n 5 O(n 2 ), n(n 1) O(n 2 )
2
n 3 O(n 2 ), 0.00001n 3 O(n 2 ), n 4 n 1 O(n 2 )
2023/3/9 Algorithms by KLSue/NCU 22
Asymptotic Notations and Basic Efficiency
Classes
Informal Introduction
Ω(g(n)):
set of all functions with a larger or same order of
growth as g(n)
For examples:
1
n 3 (n 2 ), n(n 1) (n 2 ), but 100n 5 (n 2 )
2
2023/3/9 Algorithms by KLSue/NCU 23
t (n) O( g (n))
• if t(n) is bounded above by some constant multiple of g(n)
for all large n.
i.e. exist positive constant c and nonnegative integer
n0 such that
n 3 n 2 for all n 0
i,e., select c =1 and n0=0
1 1 1 1
n(n 1) n 2 n n 2 for all n 0
2 2 2 2
1 1 1 1 1 1 1
n(n 1) n 2 n n 2 n n (for all n 2) n 2
2 2 2 2 2 2 4
Select 1 1
c2 , c1 and n0 2
4 2
Question:
First two case means ?
Last two case means ?
The second case means ?
2023/3/9 Algorithms by KLSue/NCU 38
Asymptotic Notations and Basic Efficiency
Classes
Useful technique
L’Hôpital’s rule:
t ( n) t ' ( n)
lim lim
n g ( n) n g ' ( n)
Stirling’s formula:
n
n
n! 2n for large values of n.
e
1
(log 2 e)
log 2 n (log 2 n) ' n 2 log e lim n 0
lim lim lim 2
n n n ( n)' n 1 n n
2 n
n
2 n ( ) n
n! e nn n n
lim n lim n
lim 2 n n n
lim 2 n ( )
n 2 n 2 n 2 e n 2e
Thus,
n 1
C ( n ) 1 n 1 ( n )
i 1
Accordingly,
n 2 n 1 n2 n2
Cworst(n) 1 [(n 1) (i 1) 1] (n 1 i)
i 0 j i 1 i 0 i 0
n2 n2 n2
(n 2)(n 1)
(n 1) i (n 1)1
i 0 i 0 i 0 2
(n 2)(n 1) (n 1)n 1 2
(n 1) 2 n (n 2 )
2 2 2
M(n):
sum for the total number of multiplication executed.
n 1
1
k 0
n 1 n 1 n 1 n 1 n 1 n 1
M (n) 1 n n 2 n 3
i 0 j 0 k 0 i 0 j 0 i 0
log 2 n 1 times
2023/3/9 Algorithms by KLSue/NCU 58
Mathematical Analysis of Recursive
Algorithms
Compute factorial function
M (n) M (n 1) 1 for n 0
to compute F(n -1) to multiply F(n -1) by n
M ( 0) 0
Thus,
M (n) M (n 1) 1 M (n i ) i M (n n) n n.
Definition
What is Tower of Hanoi ?
Analysis
Input size: the number of disks n
Tower of Hanoi
General Case
Base Case
General Case
Tower of Hanoi
Thus,
M (n) M (n 1) 1 M (n 1) for n 1
Recurrence relation:
M (n) 2 M (n 1) 1 for n 1
M (1) 1
M (n) 2M (n 1) 1 sub. M (n 1) 2M (n 2) 1
2[2M (n 2) 1] 1 2 2 M (n 2) 2 1 sub. M (n 2) 2 M (n 3) 1
2 2 [2M (n 3) 1] 2 1 23 M (n 3) 2 2 2 1
After i substitutions:
M (n) 2i M (n i ) 2i 1 2i 2 2 1 2i M (n i ) 2i 1
Tower of Hanoi
Because initial condition is n=1
We let i = n-1
So,
M (n) 2 n 1 M (n (n 1)) 2 n 1 1
2 n 1 M (1) 2 n 1 1 2 n 1 2 n 1 1 2 n 1
A(n) log 2 n
Fibonacci numbers:
0,1,1,2,3,5,8,13,21,34,…
F (n) F (n 1) F (n 2) for n 1
F (0) 0, F (1) 1
aX (n) bX (n 1) cX (n 2) 0
a, b and c: coefficients of the recurrence
X(n): unknown sequence to be found
ar 2 br c 0
Apply this theorem to the case of Fibonacci
numbers:
F (n) F (n 1) F (n 2) 0
r 2 r 1 0
With the roots:
1 1 4(1) 1 5
r1, 2
2 2
5
Hence,
A(n) ( n )
2023/3/9 Algorithms by KLSue/NCU 87
recommended parameters
seed: set to the current date and time
m: 2w
w: the computer’s word size
a: integer between 0.01m and 0.99m
b: can be chosen as 1
Scatterplot
Empirical data from experiment must be recorded
Then, presented for an analysis
Data Can be presented in a graph
Called scatterplot
Observation of scatterplot
Help to ascertain the efficiency class of a algorithm
Typical scatterplots
Concave shape: for a logarithmic algorithm.
Straight line
Algorithm Visualization