Académique Documents
Professionnel Documents
Culture Documents
Introduction
Time Complexity and Space Complexity
Order of complexities
2012-2013
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Outline
1
Reference Books
Introduction
Algorithm
Order of complexities
Big-Oh (O) notation
Big-Omega () notation
Theta () notation
Small-Oh (o) notation
Small-Omega () notation
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Books on Algorithm
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Algorithm
Outline
1
Reference Books
Introduction
Algorithm
Order of complexities
Big-Oh (O) notation
Big-Omega () notation
Theta () notation
Small-Oh (o) notation
Small-Omega () notation
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Algorithm
What is Algorithm
Algorithm
Definition
An algorithm is a procedure that consists of a finite set of
instructions which, given an input from some set of possible
inputs, enables us to obtain an output if such an output exists or
else obtain nothing at all if there is no output for that particular
input through a systematic execution of the instructions.
The algorithm is deterministic in the sense that the same set of
instructions are executed when the algorithm is initiated on a
particular input more than once. However, we will relax this
condition for randomized algorithm.
Instructor: Sanjib Sadhu
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Algorithm
What is Algorithm
Algorithm
Definition
An algorithm is a procedure that consists of a finite set of
instructions which, given an input from some set of possible
inputs, enables us to obtain an output if such an output exists or
else obtain nothing at all if there is no output for that particular
input through a systematic execution of the instructions.
The algorithm is deterministic in the sense that the same set of
instructions are executed when the algorithm is initiated on a
particular input more than once. However, we will relax this
condition for randomized algorithm.
Instructor: Sanjib Sadhu
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Algorithm
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Algorithm
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Outline
1
Reference Books
Introduction
Algorithm
Order of complexities
Big-Oh (O) notation
Big-Omega () notation
Theta () notation
Small-Oh (o) notation
Small-Omega () notation
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
RAM model
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
RAM model
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
RAM model
In the RAM model, instructions are executed one after
another, with no concurrent operations.
The RAM model contains instructions commonly found in
real computers:
Arithmetic(add, subtract, multiply, divide, remainder, floor,
ceiling)
Data movement(load, store, copy)
Control(conditional and unconditional branch, subroutione
call and return)
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
RAM model
In the RAM model, instructions are executed one after
another, with no concurrent operations.
The RAM model contains instructions commonly found in
real computers:
Arithmetic(add, subtract, multiply, divide, remainder, floor,
ceiling)
Data movement(load, store, copy)
Control(conditional and unconditional branch, subroutione
call and return)
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
RAM model
In the RAM model, instructions are executed one after
another, with no concurrent operations.
The RAM model contains instructions commonly found in
real computers:
Arithmetic(add, subtract, multiply, divide, remainder, floor,
ceiling)
Data movement(load, store, copy)
Control(conditional and unconditional branch, subroutione
call and return)
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
RAM model
In the RAM model, instructions are executed one after
another, with no concurrent operations.
The RAM model contains instructions commonly found in
real computers:
Arithmetic(add, subtract, multiply, divide, remainder, floor,
ceiling)
Data movement(load, store, copy)
Control(conditional and unconditional branch, subroutione
call and return)
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Outline
1
Reference Books
Introduction
Algorithm
Order of complexities
Big-Oh (O) notation
Big-Omega () notation
Theta () notation
Small-Oh (o) notation
Small-Omega () notation
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Insertion Sort
Assumption: Each execution of the i th line takes time ci , where
ci is a constant. Length of array A, denoted by length[A] is n.
cost
INSERTION-SORT(A)
times
c1
1 for j 2 to length[A]
n
c2
2
do key A[j]
n1
3
/* Insert A[j] into the sorted
0
sequence A[1..j-1]*/
n1
c
4
i j-1
n1
4
P
n
c5
5
while ((i>0) and (A[i]>key))
t
Pnj=2 j
c6
6
do A[i+1] A[i]
(t 1)
Pnj=2 j
c7
7
i i-1
j=2 (tj 1)
c8
8
A[i+1] key
n1
Here, tj is the number of times the while loop test in line 5 is
executed for that value of j.
Instructor: Sanjib Sadhu
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Insertion Sort
Assumption: Each execution of the i th line takes time ci , where
ci is a constant. Length of array A, denoted by length[A] is n.
cost
times
INSERTION-SORT(A)
c1
1 for j 2 to length[A]
n
c2
2
do key A[j]
n1
3
/* Insert A[j] into the sorted
0
sequence A[1..j-1]*/
n1
c
4
i j-1
n1
4
P
n
c5
5
while ((i>0) and (A[i]>key))
t
Pnj=2 j
c6
6
do A[i+1] A[i]
(t 1)
Pnj=2 j
c7
7
i i-1
j=2 (tj 1)
c8
8
A[i+1] key
n1
Here, tj is the number of times the while loop test in line 5 is
executed for that value of j.
Instructor: Sanjib Sadhu
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Insertion Sort
Assumption: Each execution of the i th line takes time ci , where
ci is a constant. Length of array A, denoted by length[A] is n.
cost
times
INSERTION-SORT(A)
c1
1 for j 2 to length[A]
n
c2
2
do key A[j]
n1
3
/* Insert A[j] into the sorted
0
sequence A[1..j-1]*/
n1
c
4
i j-1
n1
4
P
n
c5
5
while ((i>0) and (A[i]>key))
t
Pnj=2 j
c6
6
do A[i+1] A[i]
(t 1)
Pnj=2 j
c7
7
i i-1
j=2 (tj 1)
c8
8
A[i+1] key
n1
Here, tj is the number of times the while loop test in line 5 is
executed for that value of j.
Instructor: Sanjib Sadhu
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Insertion Sort
Assumption: Each execution of the i th line takes time ci , where
ci is a constant. Length of array A, denoted by length[A] is n.
cost
times
INSERTION-SORT(A)
c1
1 for j 2 to length[A]
n
c2
2
do key A[j]
n1
3
/* Insert A[j] into the sorted
0
sequence A[1..j-1]*/
n1
c
4
i j-1
n1
4
P
n
c5
5
while ((i>0) and (A[i]>key))
t
Pnj=2 j
c6
6
do A[i+1] A[i]
(t 1)
Pnj=2 j
c7
7
i i-1
j=2 (tj 1)
c8
8
A[i+1] key
n1
Here, tj is the number of times the while loop test in line 5 is
executed for that value of j.
Instructor: Sanjib Sadhu
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Insertion Sort
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Insertion Sort
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Insertion Sort
Worst case cost occurs when the given numbers are in
reverse order
P
P
Here, tj = j. Now, nj=2 tj = nj=2 j = n(n+1)
1
and
2
Pn
Pn
n(n1)
j=2 (tj 1) =
j=2 (j 1) =
2
T (n) = c1 n + c2 (n 1) + c4 (n 1) + c5 n(n+1)
1
2
n(n1)
n(n1)
1 + c7
+ c8 (n 1)
+c6
2
2
= (c5 +c26 +c7 ) n2 + c1 + c2 + c4 + c25 c26 c27 + c8 n
(c2 + c4 + c5 + c8 ) = an2 + bn + c
Average case cost occurs when the input is in random
order. In average, tj = 2j . T (n) will still be in the order of n2 ,
same as the worst case.
Instructor: Sanjib Sadhu
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Insertion Sort
Worst case cost occurs when the given numbers are in
reverse order
P
P
Here, tj = j. Now, nj=2 tj = nj=2 j = n(n+1)
1
and
2
Pn
Pn
n(n1)
j=2 (tj 1) =
j=2 (j 1) =
2
T (n) = c1 n + c2 (n 1) + c4 (n 1) + c5 n(n+1)
1
2
n(n1)
n(n1)
1 + c7
+ c8 (n 1)
+c6
2
2
= (c5 +c26 +c7 ) n2 + c1 + c2 + c4 + c25 c26 c27 + c8 n
(c2 + c4 + c5 + c8 ) = an2 + bn + c
Average case cost occurs when the input is in random
order. In average, tj = 2j . T (n) will still be in the order of n2 ,
same as the worst case.
Instructor: Sanjib Sadhu
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Big-Oh O
The O-notation provides an upper bound on the running time.
f (n), g(n)
Definition
A function f (n) is said to be of the order
O(g (n)) (written as f (n) = O(g(n)), if
there exists two positive constants c and
n0 such that n n0 , f (n) c g (n).
Alternately,
f (n)
f (n)
exists and lim
6= ,
n g(n)
n g(n)
if lim
CSE-603
c g(n)
f (n)
n0
f (n)=O(g(n))
implies that f (n)
grows no faster
than some
constant time g(n).
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Big-Oh O
The O-notation provides an upper bound on the running time.
f (n), g(n)
Definition
A function f (n) is said to be of the order
O(g (n)) (written as f (n) = O(g(n)), if
there exists two positive constants c and
n0 such that n n0 , f (n) c g (n).
Alternately,
f (n)
f (n)
exists and lim
6= ,
n g(n)
n g(n)
if lim
CSE-603
c g(n)
f (n)
n0
f (n)=O(g(n))
implies that f (n)
grows no faster
than some
constant time g(n).
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Big-Oh O
Example
If
f (n) = 2n3 + 5n + 7,
Here, f (n) 14 n3 , n 1.
Therefore,
f (n) = O n3
Instead of writing f (n) = 2n3 + 8n2 + 5n + 7, we may write
f (n) = 2n3 + O(n2 ). This is helpful if we are not interested
in the details of the lower order terms.
Instructor: Sanjib Sadhu
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Big-Omega
provides a lower bound within a constant factor of the running
time.
Definition
A function f (n) is said to be of the order (g(n)) (written as
f (n) = (g(n))), if there exists two positive constants c and n0
such that n n0 , f (n) c g (n).
Alternately,
if lim
f (n)
f (n)
exists and lim
6= 0,
n g(n)
g(n)
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Big-Omega
f (n), g(n)
Example
f (n)
If
3
f (n) = 2n + 5n + 7,
f (n) = n3
c g(n)
n0
f (n)=(g(n))
implies that f (n)
grows at least as fast
as some constant
times g(n).
Instructor: Sanjib Sadhu
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Theta
The provides an assymptotic tight bound on the running time.
Definition
A function f (n) is said to be of the order (g (n)) (written as
f (n) = (g(n)) ), if there exists three positive constants c1 , c2
and n0 such that n n0 , c1 g (n) f (n) c2 g (n).
Alternately, if
lim
f (n)
f (n)
exists and lim
= K , where 0 < K <
n
g(n)
g(n)
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Theta
Example
If
f (n) = 2n3 + 5n + 7,
f (n) = n3
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Small-Oh o I
Definition
A function f (n) is said to be of the order o (g (n)) (written as
f (n) = o(g(n)) ), if for every constant c > 0 there exists a
positive integer n0 such that n n0 , f (n) < c g (n).
Alternately,
f (n)
f (n)
exists and lim
= 0,
n g(n)
n g(n)
if lim
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Small-Oh o II
Example
If
f (n) = 2n3 + 5n + 7,
f (n) = o n3.1
but
f (n) 6= o n3
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Small-Omega I
Definition
A function f (n) is said to be of the order (g (n)) (written as
f (n) = (g(n)) ), if for any constant c > 0 there exists a positive
integer n0 such that n n0 , f (n) > c g (n).
Alternately,
g(n)
g(n)
exists and lim
= 0,
n f (n)
n f (n)
if lim
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Small-Omega II
Example
If
f (n) = 2n3 + 5n + 7,
f (n) = n2
but
f (n) 6= n3
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Properties of O, , , o, I
Transitivity
f (n) = O(g(n)) and g(n) = O(h(n)), f (n) = O(h(n)).
f (n) = (g(n)) and g(n) = (h(n)), f (n) = (h(n)).
f (n) = (g(n)) and g(n) = (h(n)), f (n) = (h(n)).
f (n) = o(g(n)) and g(n) = o(h(n)), f (n) = o(h(n)).
f (n) = (g(n)) and g(n) = (h(n)), f (n) = (h(n)).
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Properties of O, , , o, II
Reflexivity
f (n) = O(f (n)).
f (n) = (f (n)).
f (n) = (f (n)).
Symmetry
f (n) = (g(n)) if and only if g(n) = (f (n)).
Transpose Symmetry
f (n) = O(g(n)) if and only if g(n) = (f (n)).
f (n) = o(g(n)) if and only if g(n) = (f (n)).
Instructor: Sanjib Sadhu
CSE-603
Reference Books
Introduction
Time Complexity and Space Complexity
Order of complexities
Properties of O, , , o, III
Theorem
If f1 (n) = O(g1 (n)) and f2 (n) = O(g2 (n)), then
1. f1 (n) + f2 (n) = O(max(g1 (n), g2 (n))) // Rule of Sum
2. f1 (n) f2 (n) = O(g1 (n) g2 (n)) // Rule of Product
Questions
Prove the above theorems
Is f1 (n) f2 (n) = O(g1 (n) g2 (n))?
Is
f1 (n)
f2 (n)
(n)
= O( gg21 (n)
)?
CSE-603