Lec02 Basics of Algorithm Analysis_Update
Lec02 Basics of Algorithm Analysis_Update
1
Pseudocode
2
Pseudocode
Programming Constructs:
Expressions (to express numeric and Boolean
expression)
Method declarations: Algorithm name(param1,
param2…)
Decision structures: if…then…[else…]
While loops: while…..do….
Repeat loops: Repeat…until….
For loop: for….do…..
Array indexing: A[i], A[i,j]
Functions:
Calls: return_type function_name(param1, param2…)
Returns: return value
3
Pseudocode example
Algorithm arrayMax(A,n)
Input array A of n integers
Output maximum element of A
currentMax A[0]
for i 1 to n 1 do
if A[i] currentMax then
currentMax A[i]
increment i
return currentMax
4
Primitive Operations
5
Primitive operation examples
6
Algorithm Analysis
7
Counting Primitive Operations
10
General Rules
Algorithm #operations
x, y, z, count = 0 1
for (x=n/2; x<=n; x++) n
for (y=1; y+(n/2)<=n; y++) n
for (z=1; z<= n; z=z*2)
count++
13
General Rules
Logarithm complexity
Algorithm No of Increment
for i n to (1) do Iteration pattern of i
i = i /2
1 n/20
2 n/21
3 n/22
( )
4 n/23
( )
k n/2(k-1)=1
Interestingly, we get the similar function as the
previous algorithm, i.e. k-1 = log2 n, k = log2 n + 1
15
General Rules
While loop
Algorithm #operations
i = 1, k = 1 1
k=k+i means that k=1+2;
while(k <= n) do √n k=(1+2)+3
i++ √n k=(1+2+3)+4……
Summation formula:
k = k+i √n z(z+1)/2
Thus, z2+z <=2n
Take the biggest operation: z2
<=2n
z = √2n=√2*√n
Ignore the constant, z = √n
k 1 3 6 10 15 x
i 1 2 3 4 5 z
16
Estimating Running Time
17
Growth Rate of Running Time
18
n log n n n log n n2 n3 2n
4 2 4 8 16 64 16
8 3 8 24 64 512 256
19
If the numbers below are in terms of micro seconds:
n log n n n log n n2 n3 2n
4 2 4 8 16 64 16
8 3 8 24 64 512 256
16 4 16 64 256 4,096 65,536
32 5 32 160 1,024 32,768 4,294,967,296
20
Growth Rates of Functions
21
22
Constant Factors
1E+26
1E+23 Quadratic Quadratic Linear Linear
1E+20
1E+17
T(n)
1E+14
1E+11
1E+8
1E+5
1E+2
1E-1
1E-1 1E+1 1E+3 n 1E+5 1E+7 1E+9
Our concern:
How running time/number of operations scales with
the size of the input (i.e. the runtime’s rate of
growth)
To measure the runtime independent from
hardware, programming language, etc
Use asymptotic notation to represent the growth of
the function after dropping the constant coefficients
and less significant terms
24
Asymptotic Notation
Commonly focus on this
Three forms: since it deals with any
Worse-case: Big O kind of input
The runtime on the worst possible input
Best-case: Big Omega
The runtime on the best possible input
Average-case: Big Theta If randomized algorithm,
The runtime on the average input then we prefer this
25
Big-Oh Defined
26
Big-Oh Defined
Using mathematic to explain:
“Given functions T(n) and f(n), we say that
T(n) is O(f(n)) if and only if there exist
positive constants
c and n0 such that for all n n0,T(n) c.f(n)”
c is the constant factor
n0 is the starting input value
T(n) is O(f(n))
It means that the growth rate of
T(n) is not more than O(f(n))
27
c*f(n)
c*f(n) is an upper bound for T(n)
T(n)
Asymptotic upper bound
n0 n
28
Big-Oh Defined
29
Proper proof procedures:
30
Big-Oh Notation
Example: 2n + 10 is O(n)
2n + 10 cn 10,000
(c 2) n 10 3n
2n+10
n 10/(c 2) n
1,000
Pick c = 3 and n0 = 10
100
10
1
1 10 100 1,000
n 31
Big-Oh Example
100
10
1
1 10 100 1,000
n 32
Big-Oh Rules
33
Exercise
Big-Oh examples
7n-2 is ____________
3 log n + 5 is ____________
10000n + 5 is ____________
34
Exercise
35
Asymptotic Algorithm Analysis
38
Big-Oh and Growth Rate
40
Computing Prefix Averages
Algorithm prefixAverages1(X, n)
Input array X of n integers
Output array A of prefix averages of X #operations
A new array of n integers 1
for i 0 to n 1 do n
s X[0] n
for j 1 to i do 1+2+…+(n1)
s s + X[j] 1+2+…+(n1)
A[i] s / (i + 1) n
return A 1
T(n) = O(n2)
42
Prefix Averages (Quadratic)
43
Arithmetic Progression
44
Arithmetic Progression
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
= (1+10) + (2+9) + (3+8) + (4+7) + (5+6)
= (1+10) × (10/2)
1 + 2 + 3 + 4 + 5 + ………..+ n
= (1+n) × (n/2)
= n(n+1)/2
= O(n2)
45
Prefix Averages (Linear)
Algorithm prefixAverages2(X,n)
Input array X of n integers
Output array A of prefix averages of X #operations
A new array of n integers 1
s 0 1
for i 0 to n 1 do n
s s + X[i] n
A[i] s / (i + 1) n
return A 1
T(n) = O(n)
46
Prefix Averages (Linear)
00 ≈93
≈26.5 m
O(2n) ≈18.9 Log(75,000,000)≈26 48
Relatives of Big-Oh
Big-Omega
f(n) is Ω(g(n)) if there is a constant c > 0 and an
integer constant n0 ≥ 1 such that f(n) ≥ c.g(n) for n ≥
n0. 3nlogn + 2n is Ω(nlogn)
Since: 3nlogn + 2 ≥ nlogn, for n ≥ 2
49
Relatives of Big-Oh
50
Intuition for Asymptotic Notation
51
Example Uses of the Relatives of Big-Oh
5n2 is (n2)
f(n) is (g(n)) if there is a constant c > 0 and an integer
constant n0 1 such that f(n) c•g(n) for n n0
let c = 5 and n0 = 1
5n2 is (n)
f(n) is (g(n)) if there is a constant c > 0 and an integer
constant n0 1 such that f(n) c•g(n) for n n0
let c = 1 and n0 = 1
5n2 is (n)
f(n) is (g(n)) if, for any constant c > 0, there is an
integer constant n0 0 such that f(n) c•g(n) for n n0
need 5n02 c•n0 given c, the n0 that satisfies this is
n0 c/5 0
52
Example Uses of the Relatives of Big-Oh
5n2+3nlogn+2n+5 is O(n2)
5n2+3nlogn+2n+5 ≤ (5+3+2+5)n2 = cn2 for c = 15, n0=2
3nlogn +2n is Ω(nlogn)
f(n) is Ω(g(n)) if there is a constant c > 0 and an integer
constant n0 1 such that f(n) c•g(n) for n n0
let c=3 and n0 = 2
5n2 is omega(n)
f(n) is Ω(g(n)) if there is a constant c > 0 and an integer
constant n0 1 such that f(n) c•g(n) for n n0
let c=1 and n0 = 1
3nlogn+4n+5 is Θ(nlogn)
3nlogn≤ 3nlogn+4n+5 ≤(3+4+5) nlogn for n 2
53
Example through Formal Proof
Show that
1. Get positive constant c1, c2 and no
for all
By eliminate , . If n is 6, we get 0, c1 is a
negative, but c1 must be positive. In order to fulfil the
condition, the minimum value of n must be 10
54
Time Complexity
55
Time Complexity Comparison
56
Exercise
N2 = O(N)
2N = O(N)
N = O(N)
57
Rules of Thumb to Find Complexity
58