DAA Unit - 1
DAA Unit - 1
of Algorithms
L1.7
Algorithm - Characteristics
The criteria for any set of instruction for an algorithm is as
follows:
f(n) = 3n + 3
When n ≥ 3, 3n + 3 ≤ 3n + n = 4n
Hence f(n) = O(n), here c = 4 and n 0 = 3
Example
f(n) = 100n + 6
When n ≥ 6, 100n + 6 ≤ 100n + n = 101n
Hence f(n) = O(n), here c = 101 and n 0 = 6
Quadratic Functions
Example
f(n) = 10n2 + 4n + 2
When n ≥ 2, 10n2 + 4n + 2 ≤ 10n2 + 5n
When n ≥ 5, 5n ≤ n2, 10n2 + 4n + 2 ≤
10n2 + n2 = 11n2
Hence f(n) = O(n2), here c = 11 and n0 = 5
Example:
f(n) = 1000n2 + 100n - 6
f(n) ≤ 1000n2 + 100n for all values of n.
When n ≥ 100, 5n ≤ n2, f(n) ≤ 1000n2 +
n2 = 1001n2
Hence f(n) = O(n2), here c = 1001 and n0 =
100
Exponential Functions
Example
f(n) = 6*2n + n2
When n ≥ 4, n2 ≤ 2n
So f(n) ≤ 6*2n + 2n = 7*2n
Hence f(n) = O(2n), here c = 7 and n0 = 4
Constant Functions
Example
f(n) = 10
f(n) = O(1), because f(n) ≤ 10*1
Omega Notation (Ω)
Ω (g(n)) = { f(n) : there exist positive constants c
and n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }
It is the lower bound of any function. Hence it
denotes the best case complexity of any
algorithm. We can represent it graphically as
Example:
f(n) = 3n + 2
3n + 2 > 3n for all n.
Hence f(n) = Ω(n)
Similarly we can solve all the examples
specified under Big ‘Oh’.
Theta Notation (Θ)
Θ(g(n)) = {f(n) : there exist positive constants c 1,c2
and n0 such that c1g(n) ≤f(n) ≤c2g(n) for all n ≥ n0 }
If f(n) = Θ(g(n)), all values of n right to n 0 f(n) lies
on or above c1g(n) and on or below c2g(n). Hence
it is asymptotic tight bound for f(n).
Example 1.14
f(n) = 3n + 2
f(n) = Θ(n) because f(n) = O(n) , n ≥ 2.
In a linear search,
– Best-case complexity is O(1) and
– Worst-case complexity is O(10000).
In a binary search,
– Best-case complexity is O(1) and
– Worst-case complexity is O(log210000)=
O(13.287).
Data Type
Data Type
A data type is a collection of objects and a set of operations that act on those
objects.
Abstract Data Type
An abstract data type(ADT) is a data type that is organized in such a way that the
specification of the objects and the operations on the objects is separated from the
representation of the objects and the implementation of the operations.
Specification vs. Implementation
Operation specification
– function name
– the types of arguments
– the type of the results
Implementation independent
*Structure 1.1:Abstract data type Natural_Number (p.17)
structure Natural_Number is
objects: an ordered subrange of the integers starting at zero and ending
Assumptions:
*Figure 1.1: Space needed for one recursive call of Program 1.11 (p.21)
Total 2rows‧cols+2rows+1
Exercise 1
nlogn
logn
*Figure 1.9:Times on a 1 billion instruction per second computer(p.40)