1 - Algorithm Analysis
1 - Algorithm Analysis
Asymptotic Notations
8000
7000
Run the program with inputs of
6000
varying size and composition
Time (ms)
5000
1000
Difficult to compare
same hardware and software environments must be used
100
Running Time
80
determine. 60
40
20
while … do …
Return value
repeat … until …
return expression
for … do …
Indentation replaces braces Expressions
or := Assignment (like in
C++)
Method declaration Equality testing (like in
C++)
Algorithm method (arg [, arg…])
n2 Superscripts and other
Input … mathematical formatting
allowed
Output …
Analysis of Algorithms 15
Data Structures Department of Computer Science – University of Zakho 15
Estimating Running Time
Algorithm arrayMax executes 7n 2 primitive operations in the worst
case.
Define:
a = time taken by the fastest primitive operation
b = time taken by the slowest primitive operation
The linear growth rate of the running time T(n) is an intrinsic property
of algorithm arrayMax.
Exponential an (a ≥ 1)
O(g(n)) = { f(n) :
∃ positive constants c and n0,
such that ∀n ≥ n0
we have 0 f(n) cg(n) }
Intuitively: Set of all functions whose rate of growth is the Technically, f(n) ∈ O(g(n)).
same as or lower than that of g(n). Older usage, f(n) = O(g(n)).
Analysis of Algorithms 26
Data Structures Department of Computer Science – University of Zakho 26
Prefix Averages V2
O(n) - Linear!
The following algorithm computes prefix averages
by keeping a running sum
Algorithm prefixAverages2(X, n)
Input array X of n integers
rough # operations
Output array A of prefix averages of X
A new array of n integers n
s0 1
for i 0 to n 1 do n
s s + X[i] n
A[i] s / (i + 1) n
return A 1
Analysis of Algorithms 27
Data Structures Department of Computer Science – University of Zakho 27
Ω-notation
For functions g(n), we define
Ω(g(n)), big-Omega of n, as the
set:
Ω(g(n)) = { f(n) :
∃ positive constants c and n0,
such that ∀n ≥ n0
we have 0 cg(n) f(n)}
Analysis of Algorithms 28
Data Structures Department of Computer Science – University of Zakho 28
𝛩-notation
For functions g(n), we define
𝛩(g(n)), big-Theta of n, as the set:
𝛩(g(n)) = { f(n) :
∃ positive constants c1, c2, and n0,
such that ∀n ≥ n0
we have 0 c1g(n) f(n) c2g(n)}
Analysis of Algorithms 30
Data Structures Department of Computer Science – University of Zakho 30