Professional Documents
Culture Documents
Introduction
Worst case and Best case Analysis
Asymptotic Notations
● 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.
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 s
m, a[i] Data-Structure
A = {5, 2, 4, 6, 1, 3}
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
}
}
InsertionSort(A, n) {
for i = 2 to n { How many times will
key = A[i] this loop execute?
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
f(n)
n0 n
c.g(n)
n0 n
● Theorem
■ f(n) is (g(n)) iff f(n) is both O(g(n)) and (g(n))
■ Proof:
c1.g(n)
n0 n
We say g(n) is an asymptotic tight bound for f(n)
Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 23
Practical Complexity
TP(n)
TB(n) = 2n2
TA(n) = 100n
5000
Input Size n
50
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
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
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
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
10000000
f(n) = n
1000
100
10
1
1 4 16 64 256 1024 4096 16384 65536
● Intuitively,
■ o( ) is like < ■ ( ) is like > ■ ( ) is like =
■ O( ) is like ■ ( ) is like