CSCE 3110 Data Structures & Algorithm Analysis: Rada Mihalcea
CSCE 3110 Data Structures & Algorithm Analysis: Rada Mihalcea
Rada Mihalcea
http://www.cs.unt.edu/~rada/CSCE3110
Algorithm Analysis II
Reading: Weiss, chap. 2
Last Time
Last time:
Experimental approach – problems
Low level analysis – count operations
Abstract even further
Characterize an algorithm as a function of the
“problem size”
E.g.
Input data = array problem size is N (length of
array)
Input data = matrix problem size is N x M
Asymptotic Notation
f(n) = 2n+6
Conf. def:
Need to find a c g(n) 4n
function g(n) and
a const. c such as
f(n) < cg(n)
g(n) = n and c = 4
f(n) is O(n)
g(n) n
The order of f(n)
is n
n
More examples
2 3 n
log n n n log n n n 2
0 1 0 1 1 2
1 2 2 4 8 4
2 4 8 16 64 16
3 8 24 64 512 256
4 16 64 256 4096 65536
5 32 160 1024 32768 4294967296
“Relatives” of Big-Oh
2n+3 is o(n2)
2n + 3 is o(n) ?
Example
Remember the algorithm for computing prefix averages
- compute an array A starting with an array X
- every element A[i] is the average of all elements X[j] with j < i
Algorithm prefixAverages2(X):
Input: An n-element array X of numbers.
Output: An n -element array A of numbers such that
A[i] is the average of elements X[0], ... , X[i].
Let A be an array of n numbers.
s 0
for i 0 to n do
s s + X[i]
A[i] s/(i+ 1)
return array A
Back to the original question
Which solution would you choose?
O(n2) vs. O(n)
Some math …
properties of logarithms:
logb(xy) = logbx + logby
logb (x/y) = logbx - logby
logbxa = alogbx
logba= logxa/logxb
properties of exponentials:
a(b+c) = aba c
abc = (ab)c
ab /ac = a(b-c)
b = a logab
bc = a c*logab
Important Series
N
S ( N ) 1 2 N i N (1 N ) / 2
i 1
N
N ( N 1)(2 N 1) N 3
Sum of squares:
i 1
i
2
6
3
for large N
N
N k 1
Sum of exponents: i
k
for large N and k -1
i 1 | k 1|
Geometric series: N
A N 1 1
Special case when A = 2
i 0
A
i
A 1
• 20 + 21 + 22 + … + 2N = 2N+1 - 1
Analyzing recursive algorithms
Operation Signatures
Create: identifier Integer
Assign: Integer Identifier
IsEqual: (Integer,Integer) Boolean
LessThan: (Integer,Integer) Boolean
Negative: Integer Integer
Sum: (Integer,Integer) Integer
More examples