Analysis of Algorithms
Analysis of Algorithms
Introduction
What is Algorithm?
a clearly specified set of simple instructions to be followed to
solve a problem
Takes a set of values, as input and
produces a value, or set of values, as output
May be specified
InEnglish
As a computer program
As a pseudo-code
Data structures
Methods of organizing data
Program = algorithms + data structures
Introduction
Why need algorithm analysis ?
writing a working program is not good enough
The program may be inefficient!
If the program is run on a large data set, then the
running time becomes an issue
Example: Selection Problem
Given a list of N numbers, determine the kth
largest, where k N.
Algorithm 1:
(1) Read N numbers into an array
(2) Sort the array in decreasing order by some
simple algorithm
(3) Return the element in position k
Example: Selection Problem…
Algorithm 2:
(1) Read the first k elements into an array and sort
them in decreasing order
(2) Each remaining element is read one by one
Ifsmaller than the kth element, then it is ignored
Otherwise, it is placed in its correct spot in the array,
bumping one element out of the array.
(3) The element in the kth position is returned as
the answer.
Example: Selection Problem…
Which algorithm is better when
N =100 and k = 100?
N =100 and k = 1?
What happens when N = 1,000,000 and k =
500,000?
There exist better algorithms
Algorithm Analysis
We only analyze correct algorithms
An algorithm is correct
If, for every input instance, it halts with the correct output
Incorrect algorithms
Might not halt at all on some input instances
Might halt with other than the desired answer
Analyzing an algorithm
Predicting the resources that the algorithm requires
Resources include
Memory
Communication bandwidth
Computational time (usually most important)
Algorithm Analysis…
Factors affecting the running time
computer
compiler
algorithm used
input to the algorithm
The content of the input affects the running time
typically, the input size (number of items in the input) is the main
consideration
E.g. sorting problem the number of items to be sorted
1
1
2 2N+2
3 4N
4 1
N
i 1
i N N O( N 2 )
i 1
N
i 2
N N 2
O ( N 3
)
log N + N = O(N)
logk N = O(N) for any constant k
N = O(2N), but 2N is not O(N)
210N is not O(2N)
Math Review: logarithmic functions
x b iff
a
log x b a
log ab log a log b
log m b
log a b
log m a
log a b log a
b
a log n n log a
log a (log a ) log a
b b b
d log e x 1
dx x
Some rules
When considering the growth rate of a function using
Big-Oh
Ignore the lower order terms and the coefficients of
the highest-order term
No need to specify the base of logarithm
Changing the base from one constant to another changes the
value of the logarithm by only a constant factor
f(N) = (g(N))
There are positive constants c and n0 such
that
f(N) c g(N) when N n0
f (N ) f ( N )
then lim = lim
n g ( N ) n g ( N )
O(N3)
Algorithm 2: Divide-and-conquer
Divide-and-conquer
split the problem into two roughly equal subproblems, which
are then solved recursively
patch together the two solutions of the subproblems to arrive
at a solution for the whole problem
O(1)
T(m/2)
T(m/2)
O(m)
O(1)
Algorithm 2 (cont’d)
Recurrence equation
T (1) 1
N
T (N ) 2T ( ) N
2
2 T(N/2): two subproblems, each of size N/2
N: for “patching” two solutions to find solution to
whole problem
Algorithm 2 (cont’d)
N
Solving the recurrence: T (N ) 2T ( ) N
2
N
4T ( ) 2N
4
N
8 T ( ) 3N
8
N
2 k T ( k ) kN
2
With k=log N (i.e. 2k = N), we have
T ( N ) N T (1) N log N
N log N N
Thus, the running time is O(N log N)
faster than Algorithm 1 for large data sets