COL 106: Data-Structures: Course Coordinator: Amit Kumar
COL 106: Data-Structures: Course Coordinator: Amit Kumar
Course coordinator :
Amit Kumar ([email protected])
Course web-page:
www.cse.iitd.ac.in/~amitk/
Course Information
50
How should we measure the 40
20
10
Methods:
calls:object method(args)
returns: return value
Analysis of Algorithms
Primitive
Operation: Low-level operation
independent of programming language.
Can be identified in pseudo-code. For eg:
Data movement (assign)
Control (branch, subroutine call, return)
arithmetic an logical operations (e.g. addition,
comparison)
Byinspecting the pseudo-code, we can
count the number of primitive operations
executed by an algorithm.
Example: Sorting
INPUT OUTPUT
sequence of numbers a permutation of the
sequence of numbers
2 5 4 10 7 2 4 5 7 10
(t
n
c5 1)
do A[i+1]A[i] j 2 j
(t
n
c6 n-1 1)
i-- j 2 j
A[i+1] Ã key c7
worst-case
6n average-case
Running time
5n
best-case
4n
3n
2n
1n
1 2 3 4 5 6 7 8 9 10 11 12 …..
Input instance size
Best/Worst/Average Case (4)
Running Time
f(n) and g(n) are
functions over non-
negative integers
Usedfor worst-case n0 Input Size
analysis
Example
For functions f(n) and g(n) there are positive
constants c and n0 such that: f(n) ≤ c g(n) for n ≥ n0
f(n) = 2n + 6
conclusion:
2n+6 is O(n).
Another Example
Running Time
f(n) is (g(n)) if there exists
constants c and n0, s.t. c g ( n)
c g(n) f(n) for n n0
Used to describe best-
case running times or n0 Input Size
lower bounds for
algorithmic problems
E.g.,lower-bound for
searching in an unsorted
array is (n).
Asymptotic Notation
The “big-Theta” Notation
asymptotically tight bound
f(n) = (g(n)) if there exists
constants c1, c2, and n0, s.t.
c1 g(n) f(n) c2 g(n) for n
n0
c 2 g (n )
f(n) is (g(n)) if and only if f (n )
Running Time
f(n) is O(g(n)) and f(n) is c 1 g (n )
(g(n))
O(f(n)) is often misused
n0
instead of (f(n))
Input Size
Asymptotic Notation
Two more asymptotic notations
"Little-Oh" notation f(n) is o(g(n))
non-tight analogue of Big-Oh
For every c, there should exist n0 , s.t. f(n)
c g(n) for n n0
Used for comparisons of running times.
If f(n)=o(g(n)), it is said that g(n)
dominates f(n).
"Little-omega" notation f(n) is w(g(n))
non-tight analogue of Big-Omega
Asymptotic Notation
Analogy with real numbers
f(n) = O(g(n)) @ f g
f(n) = (g(n)) @ fg
f(n) = (g(n)) @ f g
f(n) = o(g(n)) @ f <g
f(n) = w(g(n)) @ f >g
Abuse
of notation: f(n) = O(g(n)) actually
means f(n) O(g(n))
Comparison of Running Times