Analysis and Design of Algorithm-2
Analysis and Design of Algorithm-2
Last Time
Steps in problem solving Algorithm analysis
Space complexity Time complexity Pseudo-code
Algorithm Analysis
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
Goal: to simplify analysis by getting rid of unneeded information (like rounding 1,000,0011,000,000) We want to say in a formal way 3n2 n2 The Big-Oh Notation:
given functions f(n) and g(n), we say that f(n) is O(g(n)) if and only if there are positive constants c and n0 such that f(n) c g(n) for n n0
Graphic Illustration
f(n) = 2n + 6
g(n) = n n
More examples
What about f(n) = 4n2 ? Is it O(n)?
Find a c such that 4n2 < cn for any n > n0
3log(n) + log (log (n)) = O( ? ) Simple Rule: Drop lower order terms and constant factors
Properties of Big-Oh
If f(n) is O(g(n)) then af(n) is O(g(n)) for any a. If f(n) is O(g(n)) and h(n) is O(g(n)) then f(n)+h(n) is O(g(n)+g(n)) If f(n) is O(g(n)) and h(n) is O(g(n)) then f(n)h(n) is O(g(n)g(n)) If f(n) is O(g(n)) and g(n) is O(h(n)) then f(n) is O(h(n)) If f(n) is a polynomial of degree d , then f(n) is O(nd) nx = O(an), for any fixed x > 0 and a > 1
An algorithm of order n to a certain power is better than an algorithm of order a ( > 1) to the power of n
log nx is O(log n), fox x > 0 how? log x n is O(ny) for x > 0 and y > 0
An algorithm of order log n (to a certain power) is better than an algorithm of n raised to a power y.
Some Numbers
log n 0 1 2 3 4 5
n 1 2 4 8 16 32
n log n 0 2 8 24 64 160
n2 1 4 16 64 256 1024
n3
2n
Relatives of Big-Oh
Relatives of the Big-Oh
(f(n)): Big Omega asymptotic lower bound (f(n)): Big Theta asymptotic tight bound Big-Omega think of it as the inverse of O(n) g(n) is (f(n)) if f(n) is O(g(n)) Big-Theta combine both Big-Oh and Big-Omega f(n) is (g(n)) if f(n) is O(g(n)) and g(n) is (f(n)) Make the difference: 3n+3 is O(n) and is (n) 3n+3 is O(n2) but is not (n2)
More relatives
Little-oh f(n) is o(g(n)) if for any c>0 there is n0 such that f(n) < c(g(n)) for n > n0. Little-omega Little-theta 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
this
Example (contd)
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
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
S ( N ) = 1 2 N = i = N (1 N ) / 2
i =1
2
A N 1 1 A = A 1 i =0
N i
20 + 21 + 22 + + 2N = 2N+1 - 1
foo(A, B);
T(n)
= T(n/2logn) + clogn choose k = logn = T(n/n) + clogn = T(1) + clogn = b + clogn = (logn)
= (logn)
Problem
Running time for finding a number in a sorted array [binary search]
Pseudo-code Running time analysis
ADT
ADT = Abstract Data Types A logical view of the data objects together with specifications of the operations required to create and manipulate them. Describe an algorithm pseudo-code Describe a data structure ADT
What is a representation?
A specific encoding of an instance This encoding MUST be known to implementors of the data type but NEED NOT be known to users of the data type Terminology: "we implement data types using data structures
Assign
isEqual
id1 == id2;
id1<id2
-id1 id1+id2
Negative Sum
Operation Signatures Create: identifier Integer Assign: Integer Identifier IsEqual: (Integer,Integer) Boolean LessThan: (Integer,Integer) Boolean Negative: Integer Integer Sum: (Integer,Integer) Integer