Introduction To Asymptotic Analysis
Introduction To Asymptotic Analysis
Analysis
CSE 2215 - Lecture 2
Instructor : Fahmid Al Rifat, Lecturer, Dept. of CSE , UIU
[email protected]
1
Today’s Goals
• Discuss Runtime of Programs.
• Compute and Classify growth of functions.
• Analyze complexity classes for algorithms.
2
What is an Algorithm?
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
INPUT Algorithm
OUTPUT
instance m:= a[1];
for I:=2 to size of input
25, 90, 53, 23, 11, 34 if m > a[i] then 11
m:=a[I];
return m
m, a[i] Data-Structure
7
What do we Analyze?
o Correctness
o Does the input/output relation match algorithm
requirement?
o Amount of work done (complexity)
o Basic operations to do task
o Amount of space used
o Memory used
o Simplicity, clarity
o Verification and implementation.
o Optimality
o Is it impossible to do better?
8
Running Time
9
An Example: Insertion Sort
10
An Example: Insertion Sort
A = {5, 2, 4, 6, 1, 3}
11
An Example: Insertion Sort
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
12
An Example: Insertion Sort
13
Analyzing Insertion Sort
T(n) = c1n + c2(n-1) + c3(n-1) + c4T + c5(T - (n-1)) + c6(T - (n-1)) + c7(n-1)
= c8T + c9n + c10
15
Asymptotic Performance
16
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
17
Upper Bound Notation
18
Upper Bound Notation
time
c.g(n)
f(n)
n0 n
19
Insertion Sort is O(n2)
Proof
The run-time is an2 + bn + c
If any of a, b, and c are less than 0, replace the constant with
its absolute value
an2 + bn + c (a + b + c)n2 + (a + b + c)n + (a + b + c)
3(a + b + c)n2 for n 1
Let c’ = 3(a + b + c) and let n0 = 1. Then
an2 + bn + c c’ n2 for n 1
Thus an2 + bn + c = O(n2).
Question
Is InsertionSort O(n3) ?
Is InsertionSort O(n) ?
20
Lower Bound Notation
21
Lower Bound Notation
time
f(n)
c.g(n)
n0 n
22
Asymptotic Tight Bound
Theorem
f(n) is (g(n)) iff f(n) is both O(g(n)) and (g(n))
Proof:
23
Asymptotic Tight Bound
c2.g(n) f(n)
time
c1.g(n)
n0 n
We say g(n) is an asymptotic tight bound for f(n)
24
Practical Complexity
TB(n) = 2n2
TA(n) = 100n
5000
Input Size n
50
25
Practical Complexity
250
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
26
Practical Complexity
500
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
27
Practical Complexity
1000
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 3 5 7 9 11 13 15 17 19
28
Practical Complexity
5000
4000
f(n) = n
f(n) = log(n)
3000
f(n) = n log(n)
f(n) = n^2
2000 f(n) = n^3
f(n) = 2^n
1000
0
1 3 5 7 9 11 13 15 17 19
29
Practical Complexity
f(n) = n
10000000 f(n) = log(n)
f(n) = n log(n)
1000000
f(n) = n^2
f(n) = n^3
100000
f(n) = 2^n
10000
1000
100
10
1
1 4 16 64 256 1024 4096 16384 65536
30
Practical Complexity
Intuitively,
■ o( ) is like < ■ ( ) is like > ■ ( ) is like =
■ O( ) is like ■ ( ) is like
32
Thank You
33