Chap 1
Chap 1
Contents
Hanoi University of Science and Technology
TRƯỜNG ĐẠI HỌCSchool
BÁCH of KHOA
Information and Communications Technology
HÀ NỘI
VIỆN CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
1.1. Introductory Example
1.2. Algorithm and Complexity
1.3. Asymptotic notation
1.4. Running time calculation
Chapter 1. Fundamentals
1. Introductory example: the max subarray problem 1. Introductory example: the max subarray problem
• The first simple algorithm that one could think about is: Index i 0 1 2 3 4 5
a[i] -2 11 -4 13 -5 2
browse all possible sub-arrays:
i = 0: (-2), (-2, 11), (-2,11, -4), (-2,11,-4,13), (-2,11,-
4,13,-5), (-2,11,-4,13,-5,2)
ai, ai+1 , …, aj với 1 ≤ i ≤ j ≤ n,
i = 1: (11), (11, -4), (11, -4, 13), (11, -4, 13, -5), (11, -4,
13, -5, 2)
then calculate the value of each sub-array in order to find i = 2: (-4), (-4, 13), (-4, 13, -5), (-4,13,-5,2)
i = 3: (13), (13,-5), (13, -5,2)
the maximum value. i = 4: (-5), (-5, 2) int maxSum = a[0];
i = 5: (2) for (int i=0; i<n; i++) {
• The number of all possible sub-arrays: for (int j=i; j<n; j++) {
int sum = 0;
for (int k=i; k<=j; k++)
C(n, 1) + C(n, 2) = n2/2 + n/2 sum += a[k];
if (sum > maxSum)
maxSum = sum;
NGUYỄN KHÁNH PHƯƠNG }
SOICT– HUST
}
Brute force algorithm: browse all possible sub-array 1. Introductory example: the max subarray problem
• Analyzing time complexity: we count the number of additions that the algorithm need to
perform, it means we count the statement
1.1.1. Brute force
sum += a[k] 1.1.2. Brute force with better implement
must perform how many times.
The number of additions: 1.1.3. Dynamic programming
n 1 n 1 n 1 n 1
( n i )(n i 1)
( j i 1) (1 2 ... (n i ))
i 0 j i i0 i 0 2
1 n 1 n 2 n 1 n(n 1)(2n 1) n( n 1)
k (k 1) 2
2 k 1 k 1
k k
k 1 2 6
2
3 2 int maxSum = a[0];
n n n
for (int i=0; i<n; i++) {
6 2 3
for (int j=i; j<n; j++) {
int sum = 0;
for (int k=i; k<=j; k++)
sum += a[k];
if (sum > maxSum)
maxSum = sum;
} NGUYỄN KHÁNH PHƯƠNG
SOICT– HUST
}
1.1.2. A better implementation 1.1.2. A better implementation
Brute force algorithm: browse all possible sub-array Brute force algorithm: browse all possible sub-array
Index i 0 1 2 3 4 5 Index i 0 1 2 3 4 5
a[i] -2 11 -4 13 -5 2 a[i] -2 11 -4 13 -5 2
i = 0: 18 + (-5)=13 13
i = 0: (-2), (-2, 11), (-2,11, -4), (-2,11,-4,13), (-2,11,-4,13,-5), (-2,11,-
9 + (-4)=5 5 4,13,-5,2)
i = 1: (11), (11, -4), (11, -4, 13), (11, -4, 13, -5), (11, -4, 13, -5, 2)
(-2),(-2, 11), (-2,11, -4), (-2,11,-4,13), (-2,11,-4,13,-5), (-2,11,-4,13,-5,2)
i = 2: (-4), (-4, 13), (-4, 13, -5), (-4,13,-5,2)
-2+11 = 9 9 5+13=18 18 i = 3: (13), (13,-5), (13, -5,2)
i = 4: (-5), (-5, 2)
i = 5: (2)
1.1.1. Brute force n3 103 10-5 106 10-2 sec 1012 2.7 hours 1018 115 days
n2 100 10-6 10000 10-4 sec 108 1 sec 1012 2.7 hours
For the same problem (max subarray), we propose 2 algorithms that requires • With small n, the calculation time is negligible.
different number of addition operations, and therefore, they will require different • The problem becomes more serious when n > 106. At that time, only
computation time. the third algorithm is applicable in real time.
The following tables show the computation time of these 2 algorithms with the • Can we do better?
assumption: the computer could do 108 addition operation per second Yes! It is possible to propose an algorithm that requires only n additions!
Complexity n=10 Time (sec) n=100 Time (sec) n=104 Time n=106 Time
n3 103 10-5 106 10-2 sec 1012 2.7 hours 1018 115 days
n2 100 10-6 10000 10-4 sec 108 1 sec 1012 2.7 hours
1. Introductory example: the max subarray problem 1.1.3. Dynamic programming to solve max subarray problem
1.1.1. Brute force The primary steps of dynamic programming:
1. Divide: Partition the given problem into sub problems
(Sub problem: have the same structure as the given problem but with smaller size)
1.1.2. Brute force with better implement
2. Note the solution: store the solutions of sub problems in a table
3. Construct the final solution: from the solutions of smaller size problems, try to
1.1.3. Dynamic programming find the way to construct the solutions of the larger size problems until get the
solution of the given problem (the sub problem with largest size)
n
• Example: The problem of finding the largest integer among a number of positive
integers
• Input: the array of n positive integers a1, a2, …, an
• Output: the largest
• Example: Input 12 8 13 9 11 Output: 12 NGUYỄN KHÁNH PHƯƠNG
• Question: Design the algorithm to solve this problem SOICT– HUST
worst case
NGUYỄN KHÁNH PHƯƠNG NGUYỄN KHÁNH PHƯƠNG
SOICT– HUST average case SOICT– HUST
Kind of analyses Experimental Evaluation of Running Time
Best-case: best case • Write a program implementing the algorithm
average case
• T(n) = minimum time of algorithm on any input of size n. worst case • Run the program with inputs of varying size and composition
120
• Cheat with a slow algorithm that works fast on some input. 100
• Use a method like clock( ) to get an accurate measure of the actual running time
Average-case:
Running Time
80
20
• Very useful but often difficult to determine 0
• Plot the results
1000 2000 3000 4000
Worst-case: Input Size
9000
7000
• Easier to analyze 6000
Time (ms)
5000
4000
To evaluate the running time: 2 ways:
3000
• Experimental evaluation of running time 2000
0
0 50 100
NGUYỄN KHÁNH PHƯƠNG Input Size
SOICT– HUST
Limitations of Experiments when evaluating the running time of an algorithm Theoretical Analysis of Running Time
• Experimental evaluation of running time is very useful but • Uses a pseudo-code description of the algorithm instead of an
– It is necessary to implement the algorithm, which may be difficult implementation
• Characterizes running time as a function of the input size, n
– Results may not be indicative of the running time on other inputs
not included in the experiment • Takes into account all possible inputs
• Allows us to evaluate the speed of an algorithm independent of the
– In order to compare two algorithms, the same hardware and hardware/software environment (Changing the hardware/software
software environments must be used environment affects the running time by a constant factor, but does not
We need: Theoretical Analysis of Running Time alter the growth rate of the running time)
• For a given function g(n), we denote by 𝚯(g(n)) the set of functions Example 1: Show that 10n2 - 3n = 𝚯(n2)
f (n) : there exist positive constants c1 , c2 , and n0 s.t.
( g (n)) • With which values of the constants n0, c1, c2 then the inequality in the
0 c1 g ( n) f ( n) c2 g (n) for all n n0
Intuitively: Set of all functions that have the same rate of growth as g(n). definition of the theta notation is correct:
• A function f(n) belongs to the set 𝚯(g(n)) if there exist positive constants c1
and c2 such that it can be “sand- wiched” between c1g(n) and c2g(n) for 𝑐 𝑛 ≤ 𝑓(𝑛) = 10𝑛 − 3𝑛 ≤ 𝑐 𝑛 ∀n ≥ n0
sufficienly large n • Suggestion: Make c1 a little smaller than the leading (the highest)
• 𝑓 𝑛 = 𝚯(g(n)) means that there exists some constant c1 and c2 s.t.
coefficient, and c2 a little bigger.
c1g(n) ≤ f(n) ≤ c2g(n) for large enough n.
• When we say that one function is theta of Select: c1 = 1, c2 = 11, n0 = 1 then we have
another, we mean that neither function goes
n2 ≤ 10n2 – 3n ≤ 11n2, with n ≥ 1.
to infinity faster than the other.
∀n ≥ 1: 10n2 - 3n = 𝚯(n2)
• Note: For polynomial functions: To compare the growth rate, it is necessary
to look at the term with the highest coefficient
f(n) = (g(n)) c1, c2 , n0 >0 : n n0 , c1g(n) f(n) c2g(n) f(n) = (g(n)) c1, c2 , n0 >0 : n n0 , c1g(n) f(n) c2g(n)
Example 2: Show that 𝑓 𝑛 = 𝑛 − 3𝑛 = 𝚯(𝑛 ) Example 3: Show that f(n) = 23n3 – 10 n2 log2n + 7n + 6 = 𝚯(𝑛 )
We must find n0, c1 and c2 such that
We must find n0, c1 and c2 such that
𝑐 𝑛 ≤ 𝑓(𝑛) = 𝑛 − 3𝑛 ≤ 𝑐 𝑛 ∀n ≥ n0 𝑐 n3 ≤ 𝑓(𝑛) = 23n3 – 10 n2 log2n + 7n + 6 ≤ 𝑐 n3n n0
g(n)=n
n
Big-Oh Examples Note
• The values of positive constants n0 and c are not unique when proof the
O(g(n)) = {f(n) : positive constants c and n0, such that
n n0, we have 0 f(n) cg(n) } asymptotic formulas
O(g(n)) = {f(n) : positive constants c and n0, such that O(g(n)) = {f(n) : positive constants c and n0, such that
n n0, we have 0 f(n) cg(n) } n n0, we have 0 f(n) cg(n) }
1,000,000
• Example 3: Show that 3n3 + 20n2 + 5 is O(n3) n^2
• Example 5: the function n2 is 100n
Need constants c and n0 such that 3n3 + 20n2 + 5 cn3 for n n0 not O(n)
100,000
10n
…… – n2 cn 10,000 n
1
1 10 100 1,000
NGUYỄN KHÁNH PHƯƠNG n
SOICT– HUST
Big-Oh and Growth Rate Inappropriate Expressions
• The big-Oh notation gives an upper bound on the growth rate of a
function
• The statement “f(n) is O(g(n))” means that the growth rate of f(n) is
O(g(n))
no more than the growth rate of g(n)
• We can use the big-Oh notation to rank functions according to their
growth rate
f (n)X
f(n) is O(g(n)) g(n) is O(f(n))
f (n)X
O(g(n))
g(n) grows more Yes No
f(n) grows more No Yes
Same growth Yes Yes
Theorem: For any two functions f(n) and g(n), we have 𝑓 𝑛 = 𝚯(g(n)) Theorem: For any two functions f(n) and g(n), we have 𝑓 𝑛 = 𝚯(g(n))
if and only if 𝑓 𝑛 = 𝑂(g(n)) and 𝑓 𝑛 = Ω(g(n)) if and only if 𝑓 𝑛 = 𝑂(g(n)) and 𝑓 𝑛 = Ω(g(n))
Example 1: Show that f(n) = 5n2 = 𝚯(𝑛 ) Example 2: Show that f(n) = 3n2 – 2n + 5 = 𝚯(𝑛 )
Because: Because:
• 5n2 = O 𝑛2 3n2 – 2n + 5 = O 𝑛2
f(n) is O(g(n)) if there is a constant c > 0 and an integer constant n0 1 such that f(n) is O(g(n)) if there is a constant c > 0 and an integer constant n0 1 such that
f(n) ≤ cg(n) for n n0 f(n) ≤ cg(n) for n n0
let c = 5 and n0 = 1 pick c = ? and n0 = ?
• 5n2 = Ω(n2) 3n2 – 2n + 5 = Ω(n2)
f(n) is (g(n)) if there is a constant c > 0 and an integer constant n0 1 such that f(n) is (g(n)) if there is a constant c > 0 and an integer constant n0 1 such that
f(n) cg(n) for n n0 f(n) cg(n) for n n0
let c = 5 and n0 = 1 pick c = ? and n0 = ?
Therefore: f(n) = (n2) Therefore: f(n) = (n2)
𝑓 𝑛 = 𝚯(g(n)) f(n) = O(g(n)) f(n) = Ω(g(n)) 𝑓 𝑛 = 𝚯(g(n)) f(n) = O(g(n)) f(n) = Ω(g(n))
Exercise 1 Exercise 2
Show that: 100n + 5 ≠ (n2) Show that: n ≠ (n2)
Exercise 3:Show that The way to talk about the running time
a) 6n3 ≠ (n2) • When people say “The running time for this algorithm is O(f(n))”, it means
Ans: Contradiction that the worst case running time is O(f(n)) (that is, no worse than c*f(n)
for large n, since big Oh notation gives an upper bound).
– Assume: 6n3 = (n2)
• It means the worst case running time could be determined by some
function g(n) O(f(n))
b) n ≠ (log2n) • When people say “The running time for this algorithm is (f(n))”, it means
that the best case running time is (f(n)) (that is, no better than c*f(n) for
Ans: Contradiction
large n, since big Omega notation gives a lower bound).
– Assume: n = (log2n)
• It means the best case running time could be determined by some
function g(n) (f(n))
g (n) :there exist positive constants c and n0 s.t.
NGUYỄN KHÁNH PHƯƠNG ( f (n))
SOICT– HUST 0 cf (n) g (n) for all n n0
o- Little oh notation - Little omega notation
• For a given function g(n), we denote by o(g(n)) the set of functions • For a given function g(n), we denote by o(g(n)) the set of functions
f (n) :there exist positive constants c and n0 s.t. f (n) :there exist positive constants c and n0 s.t.
o( g (n)) ( g (n))
0 f (n) cg (n) for all n n0 0 cg (n) f (n) for all n n0
f(n) becomes insignificant relative to g(n) as n approaches infinity: f(n) becomes arbitrarily large relative to g(n) as n approaches infinity:
lim [f(n) / g(n)] = 0 lim [f(n) / g(n)] =
n n
g(n) is an upper bound for f(n) that is not asymptotically tight. g(n) is a lower bound for f(n) that is not asymptotically tight.
n logn n nlogn n2 n3 2n
Which are more alike ? 4 2 4 8 16 64 16
8 3 8 24 64 512 256
Limits Exercise
• lim [f(n) / g(n)] = 0 f(n) o(g(n)) Show that
n
1) 3n2 – 100n + 6 = O(n2)
• lim [f(n) / g(n)] < f(n) O(g(n))
n 2) 3n2 – 100n + 6 = O(n3)
• 0 < lim [f(n) / g(n)] < f(n) (g(n)) 3) 3n2 – 100n + 6 ≠ O(n)
n
n 10 20 30 50 100
Fibrec 8ms 1sec 2min 21days 109years
O(n2)
sum += a[j];
if (sum > maxSum)
maxSum = sum;
Select the statement sum+=a[k] as the characteristic statement }
Running time of the algorithm: O(n3) }
Exercise 3 Exercise 4
• Give asymptotic big-Oh notation for the running time T(n) • Give asymptotic big-Oh notation for the running time T(n) of the following
statement segment:
of the following statement segment:
for (int i = 1; i<=n; i++) a) int x = 0;
for (int j = 1; j<= i*i*i; j++) for (int i = 1; i <=n; i *= 2)
for (int k = 1; k<=n; k++)
x=x+1;
x = x + 1;
• Ans:
• Ans: int x = 0;
for (int i = n; i > 0; i /= 2)
x=x+1;
• Ans:
Ans:
• T(n) is the constant when n<1000. T(n) = O(n2).