Intro Algo
Intro Algo
Introduction
What is an Algorithm?
May be specified
In English
As a computer program
As a pseudo-code
Data structures
Introduction
Introduction
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
Algorithm Analysis
Incorrect algorithms
If, for every input instance, it halts with the correct output
Might not halt at all on some input instances
Might halt with other than the desired answer
Analyzing an algorithm
Algorithm Analysis
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
E.g. multiply two matrices together the total number of
elements in the two matrices
Asymptotic notations
Big-oh(O)
Omega()
Theta()
Little-oh(o)
Growth Rate
Big-Oh: example
f(N) = O(N4)
f(N) = O(N3)
f(N) = O(N2) (best answer, asymptotically tight)
Some rules
Big-Omega
f(N) = (g(N))
There are positive constants c and n 0 such
that
f(N) c g(N) when N n0
Big-Omega
Big-Omega: examples
f(N) = (N)
f(N) = (N2)
(best answer)
Big-Theta
f(N) = (g(N)) iff
f(N) = O(g(N)) and f(N) = (g(N))
The growth rate of f(N) equals the growth rate
of g(N)
Example: Let f(N)=N2 , g(N)=2N2
f(N) = (g(N))
Small - oh (o)