1.-Introduction-to-Asymptotic-analysis
1.-Introduction-to-Asymptotic-analysis
to
Asymptotic Analysis
CSE 2218 1
UMAMA RAHMAN
Today’s Goals
• Discuss Runtime of Programs.
• Compute and Classify growth of functions.
• Analyze complexity classes for algorithms.
2
DSA
Textbook: Introduction to Algorithms
3
What is an Algorithm?
An algorithm is a sequence of computational steps that solves a
4
What is a Program?
5
Define a Problem, and Solve It
Problem:
Description of Input-Output relationship
Algorithm:
A sequence of computational steps that transform the input into
the output.
Data Structure:
An organized method of storing and retrieving data.
Our Task:
Given a problem, design a correct and good algorithm that solves
it.
6
Define a Problem, and Solve It
Problem: Input is a sequence of integers stored in an array.
Output the minimum.
Algorithm
INPUT
m, a[i] Data-Structure
7
What do we Analyze?
o Correctness
o Does the input/output relation match algorithm requirement?
o Simplicity, clarity
o Verification and implementation.
o Optimality
o Is it impossible to do better?
8
Running Time
Number of primitive steps that are executed
Except for time of executing a function call most statements roughly
require the same amount of time
y = m * x + b
c = 5 / 9 * (t - 32 )
z = f(x) + g(y)
9
Asymptotic Performance
10
Asymptotic Analysis
Worst case
o Provides an upper bound on running time
o An absolute guarantee of required resources
Average case
o Provides the expected running time
o Very useful, but treat with care: what is “average”?
o Random (equally likely) inputs
o Real-life inputs
Best case
o Provides a lower bound on running time
In general a function
f(n) is O(g(n)) if there exist positive constants c and n
0
such that 0 f(n) c g(n) for all n n0
Formally
O(g(n)) = { f(n): positive constants c and n such that
0
0 f(n) c g(n) n n0 }
12
Upper Bound Notation
time
c.g(n)
f(n)
n0 n
13
Lower Bound Notation
We say InsertionSort’s run time is (n) (big-omega or just omega)
In general a function
f(n) is (g(n)) if positive constants c and n such that
0
0 cg(n) f(n) n n0
Proof:
Suppose run time is an + b
Assume a and b are positive
an an + b
14
Lower Bound Notation
time
f(n)
c.g(n)
n0 n
15
Asymptotic Tight Bound
A function f(n) is (g(n)) if positive constants c , c , and n
1 2 0
such that
Theorem
f(n) is (g(n)) iff f(n) is both O(g(n)) and (g(n))
16
Asymptotic Tight Bound
c2.g(n)
f(n)
time
c1.g(n
)
n0 n
18
Practical Complexity
Function Descriptor Big-Oh
c Constant O( 1 )
log n Logarithmic O( log n )
n Linear O( n )
n log n n log n O( n log n )
n2 Quadratic O( n2 )
n3 Cubic O( n3 )
nk Polynomial O( nk )
2n Exponential O( 2n )
n! Factorial O( n! )
Practical Complexity
For large input sizes, constant terms are insignificant
Program A with running time TA(n)= 100n
Program B with running time TB(n)= 2n2
TP(n)
TB(n) = 2n2
TA(n) = 100n
5000
Input Size n
50
20
Analysis of Algorithms
What is the goal of analysis of algorithms?
To compare algorithms mainly in terms of running time but also in
terms of other factors (e.g., memory requirements, programmer’s
effort etc.)
What do we mean by running time analysis?
Determine how running time increases as the size of the problem
increases.
Input Size
Input size (number of elements in the input)
size of an array
# of elements in a matrix
proportional to n2.
function.
Example
Associate a "cost" with each statement.
Find the "total cost“ by finding the total number of times each
statement is executed.
Algorithm 1 Algorithm 2
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
... ...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 = (c2 + c1) x N + c2
29
More Examples …
n4 + 100n2 + 10n + 50 is O(n4)
10n3 + 2n2 is O(n3)
n3 - n2 is O(n3)
2n2 = O(n3)
n2 = O(n2)
1000n2+1000n = O(n2)
n = O(n2)
constants
10 is O(1)
1273 is O(1)
Examples of big-omega
5n2 = (n)
c, n0 such that: 0 cn 5n2 cn 5n c = 5 and n0 = 1
2
100n + 5 ≠ (n2)
RR
O(f) (f)
(f)
32
Examples of theta notation
f(n) = n2/2 – n/2 = (n2)
logn ≠ (n)
base
log b x log a x
log a b
lg k n lg n
Other important formulas: k 1
n
1
k p 1p 2 p ... n p
k 1 p 1
n p 1
36
Other Asymptotic Notations
A function f(n) is o(g(n)) if positive constants c and n such
0
that
f(n) < c g(n) n n0
38
Thank You
39