Docsity

Asymptotic Analysis of f(n) Function and Merge Sort Algorithm, Study notes of Digital Systems Design

Toggle sidebar
bg1
Lecture No.7
We have established that f(n) ∈ n2. Let’s show why f(n) is not in some other asymptotic
class. First, let’s show that f(n) ∉ Θ(n). Show that f(n) ∉ Θ(n). If this were true, we
would have had to satisfy both the upper and lower bounds. The lower bound is satisfied
because f(n) = 8n2 + 2n - 3 does grow at least as fast asymptotically as n. But the upper
bound is false. Upper bounds requires that there exist positive constants c2 and n0 such
that f(n) ≤ c2n for all n ≥ n0.
Informally we know that f(n) = 8n2 + 2n - 3 will eventually exceed c2n no matter how
large we make c2. To see this, suppose we assume that constants c2 and n0 did exist such
that 8n2 + 2n - 3 _ c2n for all n ≥ n0 since this is true for all sufficiently large n then it
must be true in the limit as n tends to infinity. If we divide both sides by n, we have
It is easy to see that in the limit, the left side tends to 1. So, no matter how large c2 is, the
statement is violated. Thus f(n) ∉ Θ(n).
Let’s show that f(n) ∉ Θ(n3). The idea would be to show that the lower bound
f(n) ≥ c1n3 for all n ≥ n0 is violated. (c1 and n0 are positive constants). Informally we
know this to be true because any cubic function will overtake a quadratic.
If we divide both sides by n3:
The left side tends to 0. The only way to satisfy this is to set c1 = 0. But by hypothesis, c1
is positive. This means that f(n) ∉ Θ(n3).
The definition of Θ-notation relies on proving both a lower and upper asymptotic bound.
Sometimes we only interested in proving one bound or the other. The O-notation is used
to state only the asymptotic upper bounds.
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Asymptotic Analysis of f(n) Function and Merge Sort Algorithm and more Study notes Digital Systems Design in PDF only on Docsity!

Lecture No.

We have established that f(n) ∈ n 2

. Let’s show why f(n) is not in some other asymptotic class. First, let’s show that f(n) ∉ Θ(n). Show that f(n) ∉ Θ(n). If this were true, we would have had to satisfy both the upper and lower bounds. The lower bound is satisfied because f(n) = 8n^2 + 2n - 3 does grow at least as fast asymptotically as n. But the upper

bound is false. Upper bounds requires that there exist positive constants c 2 and n 0 such

that f(n) ≤ c 2 n for all n ≥ n 0.

Informally we know that f(n) = 8n 2 + 2n - 3 will eventually exceed c 2 n no matter how large we make c 2. To see this, suppose we assume that constants c 2 and n 0 did exist such

that 8n^2 + 2n - 3 _ c 2 n for all n ≥ n 0 since this is true for all sufficiently large n then it must be true in the limit as n tends to infinity. If we divide both sides by n, we have

It is easy to see that in the limit, the left side tends to 1. So, no matter how large c 2 is, the statement is violated. Thus f(n) ∉ Θ(n).

Let’s show that f(n) ∉ Θ(n^3 ). The idea would be to show that the lower bound

f(n) ≥ c 1 n 3 for all n ≥ n 0 is violated. (c 1 and n 0 are positive constants). Informally we know this to be true because any cubic function will overtake a quadratic.

If we divide both sides by n^3 :

The left side tends to 0. The only way to satisfy this is to set c1 = 0. But by hypothesis, c 1

is positive. This means that f(n) ∉ Θ(n 3 ).

The definition of Θ-notation relies on proving both a lower and upper asymptotic bound. Sometimes we only interested in proving one bound or the other. The O-notation is used to state only the asymptotic upper bounds.

The Ω−notation allows us to state only the asymptotic lower bounds.

The three notations:

These definitions suggest an alternative way of showing the asymptotic behavior. We can use limits for define the asymptotic behavior. Limit rule for Θ-notation:

for some constant c > 0 (strictly positive but not infinity) then f(n) ∈ Θ(g(n)). Similarly, the limit rule for O-notation is

for some constant c ≥ 0 (nonnegative but not infinite) then f(n) ∈ O(g(n)) and limit rule for Ω-notation:

(either a strictly positive constant or infinity) then f(n) ∈ Ω(g(n)). Here is a list of common asymptotic running times:

  • Θ (1): Constant time; can’t beat it!
  • Θ (log n): Inserting into a balanced binary tree; time to find an item in a sorted array of length n using binary search.
  • Θ(n): About the fastest that an algorithm can run.
  • Θ(n log n): Best sorting algorithms.
  • Θ(n 2 ), Θ(n 3 ): Polynomial time. These running times are acceptable when the exponent of n is small or n is not to large, e.g., n ≤ 1000.
  • Θ( n ), Θ( n ): Exponential time. Acceptable only if n is small, e.g., n ≤ 50.
  • Θ(n!), Θ(n n ): Acceptable only for really small n, e.g. n ≤ 20.

MERGE - SORT( array A, int p, int r) 1 if (p < r) 2 then 3 q (p + r)/ 4 MERGE - SORT(A, p, q) // sort A[p..q] 5 MERGE - SORT(A, q + 1, r) // sort A[q + 1..r] 6 MERGE(A, p, q, r) // merge the two pieces

The following figure illustrates the dividing (splitting) procedure. 7 5 2 4 1 6 3 0 1 6 3 0 7 5 2 4

4 1 6 3 07 5 2 4 1 6 3 0 Figure 3.1: Merge sort divide phase

Merging: All that is left is to describe the procedure that merges two sorted lists. Merge(A, p, q, r) assumes that the left sub-array, A[p : q], and the right sub-array, A[q + 1 : r], have already been sorted. We merge these two sub-arrays by copying the elements to a temporary working array called B. For convenience, we will assume that the array B has the same index range A, that is, B[p : r]. (One nice thing about pseudo- code, is that we can make these assumptions, and leave them up to the programmer to figure out how to implement it.) We have to indices i and j, that point to the current elements of each sub-array. We move the smaller element into the next position of B (indicated by index k) and then increment the corresponding index (either i or j). When we run out of elements in one array, then we just copy the rest of the other array into B. Finally, we copy the entire contents of B back into A. (The use of the temporary array is a bit unpleasant, but this is impossible to overcome entirely. It is one of the shortcomings of MergeSort, compared to some of the other efficient sorting algorithms.)

Here is the merge algorithm:

1 6 3 0 merg eFigure 3.2: Merge sort: combine phase