Lec2, Algorithm Analysis & Design, Asympototic Notation
Lec2, Algorithm Analysis & Design, Asympototic Notation
1
ASYMPTOTIC ANALYSIS
It is much more convenient to have simple measures for the efficiency of an algorithm than to implement the
algorithm and test the efficiency every time a certain parameter in the underlying computer system changes.
It is impossible to predict the exact behavior of an algorithm. There are too many influencing factors.
More importantly, by analyzing different algorithms, we can compare them to determine the best one for our
purpose.
2
ASYMPTOTIC NOTATION
Definition: The notations we use to describe the asymptotic running time of an algorithm are defined in terms of
functions whose domains are the set of natural numbers {0, 1, 2, ⋯ }.
Big-O Notation
Big-Ω Notation
Big-Θ Notation
3
WHY ASYMPTOTIC NOTATION?
Abstraction of Complexity
Scalability Analysis
4
COMMONLY USED RATES OF GROWTH
5
COMMONLY USED RATES OF GROWTH
𝒏
𝟐𝟐
𝒏!
𝟒𝒏
𝟐𝒏
𝒏𝟐
𝒏𝒍𝒐𝒈𝒏
log (𝒏!)
𝟐𝐥𝐨𝐠 𝒏
l𝒐𝒈𝟐 𝒏
√𝒍𝒐𝒈𝒏
Log log n
6
BIG-O NOTATION
Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constants
c and n0 such that
f(n) cg(n) for n n0
We use O-notation to give an upper bound on a function, to within a constant factor.
Big-O notation represents the upper bound of the running time of an algorithm. Therefore, it gives the worst-case complexity of an
algorithm.
It is the most widely used notation for Asymptotic analysis.
It specifies the upper bound of a function.
The maximum time required by an algorithm or the worst-case time complexity.
It returns the highest possible output value(big-O) for a given input..
Big-Oh(Worst Case) It is defined as the condition that allows an algorithm to complete statement execution in the longest amount
of time possible.
7
BIG-O NOTATION
8
BIG-O NOTATION
9
BIG-O NOTATION
10
BIG-Ω NOTATION
f(n) is (g(n)) if there is a constant c > 0 and an integer constant n0 1 such that
𝑓(𝑛) 𝑐 • 𝑔(𝑛) 𝑓𝑜𝑟 𝑛 𝑛0
Omega notation represents the lower bound of the running time of an algorithm. Thus, it provides the best case
complexity of an algorithm.
The execution time serves as a lower bound on the algorithm’s time complexity.
It is defined as the condition that allows an algorithm to complete statement execution in the shortest amount of
time.
11
BIG-Ω NOTATION
12
BIG-Ω NOTATION
13
BIG-Θ NOTATION
f(n) is (g(n)) if there are constants 𝑐1 > 0 and 𝑐2 > 0 and an integer constant n0 1 such that:
𝑐1 • 𝑔(𝑛) 𝑓(𝑛) 𝑐2 ’ • 𝑔(𝑛) 𝑓𝑜𝑟 𝑛 𝑛0
Theta notation encloses the function from above and below.
Since it represents the upper and the lower bound of the running time of an algorithm, it is used for analyzing the
average-case complexity of an algorithm.
14
BIG-Θ NOTATION
15
BIG-Θ NOTATION
16
LINEAR TIME COMPLEXITY O(N)
17
WHICH OF THE FOLLOWING IS ASYMPTOTICALLY TIGHT
18
QUADRATIC TIME COMPLEXITY
19
LOGARITHMIC TIME COMPLEXITY O(LOG N)
20
LOGARITHMIC TIME COMPLEXITY O(LOG N)
// Recursive function
void recurse(int n)
{
if (n <= 0)
return;
else {
// some O(1) expressions
}
recurse(n/c);
// Here c is positive integer constant greater than 1
}
21
LOGARITHMIC TIME COMPLEXITY O(LOG LOG N)
22
BINARY SEARCH
23
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
Ex. Binary search for 33. hi
mid
BINARY SEARCH
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo
hi
mid
THANKS
33