1.2L Efficiency
1.2L Efficiency
Algorithmic Foundations
And Problem Solving
Algorithm efficiency: asymptotic analysis
Dr Yushi Li
Department of Computer Science
Learning outcomes
Understand the notations of asymptotic analysis.
Able to carry out simple asymptotic analysis of
algorithms
2
Time Complexity Analysis
How fast is the algorithm?
Code the algorithm and run the program,
then measure the running time
4
Finding the minimum...
Finding min among 3 numbers
Input: 3 numbers a, b, c
Output: the min value of these 3 numbers
Algorithm: Input a, b, c
Y ab? N
Y ac? Y bc? N
Y ac? Y bc? N
Y ac? Y bc? N
It takes 2 comparisons
9
Finding min among n numbers
Input a0, a1, ...
Any
Y a0 a1 ? N systematic
approach?
Y a0 a2 ? Y a1 a2 ?
N N
a0 a3 ? a1 a3 ?
a2 a3 ? a2 a3 ?
10
Finding min among n numbers
Input a[0], a[1], ..., a[n-1]
min = 0
i=1
i = i+1
i < n? N
Y
Output a[min]
Y
min = i a[i] < a[min]?
N
11
Finding min among n numbers
input a[0], a[1], ..., a[n-1]
min = 0
i = 1
while (i < n) do Example
begin
if (a[i] < a[min]) then a[]={50,30,40,20,10}
min = i
i = i + 1 iteration min a[min]
end before 0 50
output a[min]
1 1 30
2 1 30
3 3 20
4 412 10
Finding min among n numbers
Time complexity: ?? comparisons n-1
13
Finding min using for-loop
Rewrite the above while-loop into a for-loop
input a[0], a[1], ..., a[n-1]
min = 0
for i = 1 to n-1 do
begin
if (a[i] < a[min]) then
min = i
end
output a[min]
14
Why efficiency matters?
speed of computation by hardware has been
improved
efficiency still matters
ambition for computer applications grow with
computer power
demand a great increase in speed of computation
15
Amount of data handled matches
speed increase?
When computation speed vastly increased, can we
handle much more data?
Suppose
• an algorithm takes n2 comparisons to sort n numbers
• we need 1 sec to sort 5 numbers (25 comparisons)
• computing speed increases by factor of 100
Using 1 sec, we can now perform 100x25 comparisons,
i.e., to sort 50 numbers
With 100 times speedup, only sort 10 times more
numbers!
16
Time complexity
- Big O notation …
Which algorithm is the fastest?
Consider a problem that can be solved by 5 algorithms
A1, A2, A3, A4, A5 using different number of operations
(time complexity).
f1(n) = 50n + 20 f2(n) = 10 n log2 n + 100
f3(n) = n2 - 3n + 6 f4(n) = 2n2
f5(n) = 2n/8 - n/4 + 2
n 1 2 4 8 16 32 64 128 256 512 1024 2048
f1(n) = 50n + 20 70 120 220 420 820 1620 3220 6420 12820 25620 51220 102420
f2(n) = 10 n log2n + 100 100 120 180 340 740 1700 3940 9060 20580 46180 102500 225380
f3(n) = n2 - 3n + 6 4 4 10 46 214 934 3910 16006 64774 3E+05 1E+06 4E+06
2
f4(n) = 2n 2 8 32 128 512 2048 8192 32768 131072 5E+05 2E+06 8E+06
f5(n) = 2n/8 - n/4 + 2 2 2 3 32 8190 5E+08 2E+18
100000
10000
Time
1000
ff1(n)
1(n) == 50n
50n ++ 20
20
100 ff2(n)
2(n) == 10
10 nn log2n
log2n ++ 100
100
f3(n) = n2 - 3n + 6
f3(n) = n2 - 3n + 6
10 f4(n) = 2n2
f4(n) = 2n2
ff5(n) = 2n/8 - n/4 + 2
5(n) = 2 /8 - n/4 + 2
n
1
1 2 4 8 16 32 64 128 256 512 1024 2048 4096
19
What do we observe?
There is huge difference between
– functions involving powers of n (e.g., n, n log n, n2,
called polynomial functions) and
– functions involving powering by n (e.g., 2n, called
exponential functions)
Among polynomial functions, those with same
order of power are more comparable
– e.g., f3(n) = n2 - 3n + 6 and f4(n) = 2n2
20
Growth of functions
21
Relative growth rate
2n
n2
log n
c
n
22
Hierarchy of functions
We can define a hierarchy of functions each having a
greater order of magnitude than its predecessor:
24
Hierarchy of functions (3)
1 log n n n2 n3 ... nk ... 2n
25
Big-O notation
f(n) = O(g(n)) [read as f(n) is of order g(n)]
Roughly speaking, this means f(n) is at most a
constant times g(n) for all large n
Examples
– 2n3 = O(n3)
– 3n2 = O(n2)
– 2n log n = O(n log n)
– n3 + n2 = O(n3)
function on L.H.S and function on R.H.S are said to
have the same order of magnitude
26
Big-O notation - formal definition
27
f(n) = O(g(n) – Graphical illustration
7000
constant c & no c g(n)
6000
such that
5000 n>no, f(n) c g(n)
f(n)
4000
Time
3000
2000
1000
0
n0
100 300 500 700 900 1100
250 n + 60
Time
200
150 n
100
50 c=2, n0=60
0
n0
10 30 50 70 90 110 130 150 170 190
40000
f(n)
Time
30000
c=3, n0=600
n log n
20000
10000
0
n0
100 300 500 700 900 1100 1300 1500 1700 1900
400 f(n)
Time
300
200
0
10 30 n0 50 70 90 110
O(log n) < O(n) < O(n) < O(n log n) < O(n2) < O(2n)
32
Proof of order of magnitude
Show that 2n2 + 4n is O(n2)
Since
n n2 for all n>1,
we have 2n2 + 4n 2n2 + 4n2 6n2 for all n>1.
Therefore, by definition, 2n2 + 4n is O(n2).
34
Exercise (2)
Prove the order of magnitude
1. n3 + 3n2 + 3
35
Exercise (2) cont’d
Prove the order of magnitude
3. 2n2 + n2 log n
4. 6n2 + 2n
36
More Exercise
Are the followings correct?
1. n2log n + n3 + 3n2 + 3 O(n2)?
2. n + 1000 O(n)?
3. 6n20 + 2n O(n20)?
4. n3 + 5n2 log n + n O(n2 log n) ?
37
Some algorithms we learnt
input n
Computing sum of the first n sum = 0
for i = 1 to n do
numbers begin
sum = sum + i
input n end
sum = n*(n+1)/2 output sum
output sum
O(?) O(?)
38
More exercise
What is the time complexity of the following pseudo
code?
for i = 1 to 2n do
for j = 1 to n do
x = x + 1
O(?)
39