Week 5 Intro - Algo
Week 5 Intro - Algo
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
In English
As a computer program
As a pseudo-code
Data structures
Methods of organizing data
Program = algorithms + data structures
Analysis of Algorithms / Slide 3
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
Analysis of Algorithms / Slide 4
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)
Analysis of Algorithms / Slide 8
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
E.g. multiply two matrices together the total number of
Example
N
Calculate i 3
i 1
1
1
2 2N+2
3 4N
4 1
Running-time of algorithms
Bounds are for the algorithms, rather than
programs
programs are just implementations of an algorithm,
and almost always the details of the program do
not affect the bounds
Growth Rate
Big-Oh: example
Let f(N) = 2N2. Then
f(N) = O(N4)
f(N) = O(N3)
f(N) = O(N2) (best answer, asymptotically tight)
i 1
N 2 2 3
i N N O ( N )
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)
Analysis of Algorithms / Slide 16
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
Big-Omega
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: examples
Let f(N) = 2N2. Then
f(N) = (N)
f(N) = (N2) (best answer)
Analysis of Algorithms / Slide 21
f(N) = (g(N))
Big-Theta
Growth rates …
Doubling the input size
f(N) = c f(2N) = f(N) = c
f(N) = log N f(2N) = f(N) + log 2
f(N) = N f(2N) = 2 f(N)
f(N) = N2 f(2N) = 4 f(N)
f(N) = N3 f(2N) = 8 f(N)
f(N) = 2N f(2N) = f2(N)
Advantages of algorithm analysis
To eliminate bad algorithms early
pinpoints the bottlenecks, which are worth coding
carefully
Analysis of Algorithms / Slide 25
General Rules
For loops
at most the running time of the statements inside
the for-loop (including tests) times the number of
iterations.
Nested for loops