S02 PsAlgorithmComplexity
S02 PsAlgorithmComplexity
S02 PsAlgorithmComplexity
Thanh-Hai Tran
2020 2
Analysis of algorithms
2020 3
Analysis of algorithms
2020 4
Analyses of computational times
n Experimental method
u Setting algorithm in programming language
u Run the program with different input data
u Measure program execution time and evaluate the increase
u Size relative to the size of the input data
n Limitations:
u Limitation in the quantity and quality of test samples
u Requires testing environment (hardware and software) should
be uniform, stable
2020 5
Analyses of computational times
n Theoretical methods
u Able to review any input data
u Used to evaluate algorithms independent of testing
environment
u Used with high-level descriptions of the algorithm
n Implementing this method must take care of
u Language describing algorithm
u Determination of the time measurement
u An approach to generalizing time complexity
2020 6
Analyses of computational times
2020 7
An example
n Line 1: 2 typical operations
n Line 2:
u Assignment i= 1 (1)
2020 10
Estimation of algorithm complexity
n Objective:
u Given an algorithm A with n representing its data size. We
need to find a formula (function) TA(n) that expresses the
running time of A in computers (we usually use T(n) for short).
n Normally, there are 3 cases for T(n):
u Example:
« Given an array
« Please find out the value x in the array
u Best case Tb(n): the case that algorithm can be run the most
quickly
u Worse case Tw(n): the case that algorithm can be run the
most slowly
u Average case Ta(n): the average of all cases.
2020 11
Estimation of algorithm complexity
2020 12
An example
n Worst case: n
n Best case: 1
n Average case: T(n) = ∑ 𝑖𝑝! where is pi is the probability that
value of interest appears at a[i]. When pi = 1/n the total
number will be (n+1)/2
2020 13
Analysis of algorithms
2020 14
Big O concept (ô lớn)
n Definition: Given an integer n that is not negative, and two
functions f(n) và g(n) defined in non-negative domains. We
say that:
f(n) = O (g(n)) (f(n) is big O of g(n))
if and only if there exists a constant C and n0, so that:
" n ³ n0 then f(n) £ C.g(n)
n Meaning of big O: f(n) = O (g(n)) means that for " n ³ n0
g(n) is above asymptote of f(n) (For large amounts of data,
f(n) will grow no more than a constant factor than g(n))
n Hence, g provides an upper bound.
2020 15
Big O concept (ô lớn)
2020 16
Categories of algorithms
2020 17
Big O concept (ô lớn)
2020 18
Example
2020 19
Rules to determine complexity
2020 20
Limitations of Big O Notation
2020 21
Example
2020