Analysis of Algorithms
Analysis of Algorithms
ALGORITHMS
Introducing Some Limited Concepts about Algorithms
Outline
Efficiency of Algorithms
Examples
Order of Growth
Worst, Average and Best Case Analysis
Efficiency of Algorithms
Two fundamental questions:
How fast will my program run? (Time Complexity)
How much memory it will take?
Examples
Fahrenheit to Celsius Converter (code)
Examples
Fahrenheit to Celsius Converter (analysis)
Three arithmetic operations
One subtraction, once multiplication, and one division
Examples
Adding Array Elements (code)
Examples
Adding Array Elements (analysis)
For an array of size N, how many operations are roughly executed?
Statement
Frequency of Execution
int sum = 0
int i= 0 1
i < list.length
i++
sum=sum+list(i)
Total
=3N+2
Examples
Adding Square Matrices(code)
For an N N matrix, how many operations are
roughly executed?
Examples
Adding Square Matrices(analysis)
Statement
Frequency of Execution
dbouble[][] sum...
int i= 0 1
i < list.length
i++
int j = 0
j< list.length
N2
j++
N2
N2
Total
=3N2+ 3N + 2
Order of Growth
Effect of Problem Size
Only large problem sizes are worth studying!
Modern computers are very fast.
Operations on an input size of a few hundreds will
usually complete in under a minute
Order of Growth
Growth Rate of Functions
Consider: f (n) = n2 + 100n + log (n) + 1000
Let us consider how this function behaves for small
and large values of n
Order of Growth
Growth Rate of Functions(cont'd)
For the previous function, f (n) = n2 + 100n + log (n) +
1000, we can see
For large n, f (n)
n2
This enables us to use the approximation: f (n) n2, for a
sufficiently large n
This simplification worked despite the large coefficient in front of
n
Order of Growth
Big-O Notation
Instead of saying an algorithm takes 3n2 + 4n + 2
steps, its simpler to leave out lower order terms such
as 4n and 2, and even the coefficient 3 and say the
growth rate is O(n2) (pronounced as big-oh of n2)
Big-O is the most widely used asymptotic notation in
algorithm analysis
Will be used throughout the course
Order of Growth
Big-O Notation - Common Sense Approach
Big-O enables us to specify how an algorithm's performance
scales with input size
We will discard lower order terms since they are dominated
na dominates nb if a> b: for example n3 dominates n2
any exponential dominates any polynomial: 3n dominates 5n
any polynomial dominates any logarithm: n dominates (log (n))3
Order of Growth
Big-O Notation - Formal Definition
Denition Let T(n) and f (n) be functions from positive
integers to positive reals. We say T(N) = O(f (N))
(which means T(N) grows no faster than f (n)) if
there are positive constants c and n0 such that T(N)
= cf(N) when N n0.
Order of Growth
Growth Rate Comparisons
If we assume a given operation takes one
nanosecond(10-9) on some computer, we can
construct the following table
Order of Growth
What do we mean by a fast algorithm?
We cannot simply say a fast algorithm is one that runs
quickly when given a real input instance
This definition depends on the speed of the computer algorithms run fast on some machines and slowly on
other machines
Different algorithms behave differently on different
input sizes
Definition
An algorithm if fast if it is growth rate is O(n) or less.
Average Case
Hard to determine analytically unless we know the type of input we are
processing
Reflects the actual situation
Average Case
Hard to determine analytically unless we know the type of input we are
processing
Reflects the actual situation