






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solutions to various problems related to algorithm analysis, including proofs of big o and omega notations for polynomials, the behavior of sin(n)n and log(n!), the generating function for fibonacci numbers, and the correctness and runspace analysis of selectionsort.
Typology: Assignments
1 / 10
This page cannot be seen from the preview
Don't miss anything!







1
Problem 1: Suppose a polynomial in n of degree d has the form
p(n) =
∑^ d
i=
aini
with leading coefficient ad > 0, and let k ≥ 1 be a constant (not necessarily an integer).
Prove the following:
(a) If k ≥ d then p(n) ≤ O(nk). (b) If k ≤ d then p(n) ≥ Ω(nk^ ). (c) If k = d then p(n) = Θ(nk).
Proof of (c). First we do some algebra:
p(n) =
∑^ d
i=
aini^ = nd
∑d
i=
aini−d^ = nd(
∑^ d−^1
i=
aini−d^ + ad) = nd(ad + q(n))
where
q(n) =
∑^ d−^1
i=
aini−d.
Note that q(n) is a sum of terms each of which is a constant multiplied by a power of n, the power being less than or equal to −1. By calculus, the limit of q(n) is zero as n → ∞. So there is an integer n 0 such that
|q(n)| < 0. 5 ad
for all n ≥ n 0. Using more elementary algebra, and assuming n ≥ n 0 , we can see that
nd(ad − 0. 5 ad) ≤ nd(ad + q(n)) ≤ nd(ad + 0. 5 ad).
Letting c 1 = 0. 5 ad and c 2 = 1. 5 ad, we have:
c 1 nd^ ≤ p(n) ≤ c 2 nd
for all n ≥ n 0. From this last inequality we see that p(n) = Θ(nd). QED
We have shown part (c) first, which makes (a) and (b) very straightforward:
Proof of (a). Since k − d ≥ 0, we have nd^ ≤ ndnk−d^ = nk, so that O(nd) ≤ O(nk). Using part (c), we have p(n) ≤ O(nd) ≤ O(nk)
as required. QED
Problem 3: This problem develops properties of the Fibonacci numbers, which are given by the recurrence fn = fn− 1 +fn− 2 with initial conditions f 0 = 0, f 1 = 1. Define the generating function for the Fibonacci sequence as the formal power series
F (z) =
i=
fizi
.
(a). Show that F (z) = z + zF (z) + z^2 F (z).
Proof: Expand the right hand side RHS:
z + zF (z) + z^2 F (z) = z + z(
i=
fizi) + z^2 (
i=
fizi) = (1 + f 0 )z +
i=
(fi− 1 + fi− 2 )zi
Now plug in the initial conditions and recurrence relation, to obtain
RHS = z +
i=
fizi^ =
i=
fizi
which is identical to the left hand side. QED
(b). Show that
F (z) =
z 1 − z − z^2
z (1 − φz)(1 − φzˆ)
(1 − φz)
(1 − φzˆ)
where
φ =
and
φˆ =^1 −
Proof: The first equality is proved by solving the equation proved in (a) for F (z). The other two equalities are proved by calculation, for example:
(1 − φz)(1 − φzˆ ) = 1 − (φ + φˆ)z + (φ φˆ)z^2 = 1 − (1/2 + 1/2)z + ((1 − 5)/4)z^2 = 1 − z − z^2
(c). Show that
F (z) =
i=
(φi^ − φˆi)zi
Proof: Recall the fact: 1 1 − az
i=
aizi
(verify by multiplying both sides by 1 − az). Thus applying part (b) we obtain:
F (z) =
(1 − φz)
(1 − φzˆ)
i=
φizi^ −
i=
φ^ ˆizi) =
i=
(φi^ − φˆi)zi
which proves the result.
(d). Prove that fi = φi/
5, rounded to the nearest integer.
Proof: Note that the absolute value of the second base is | φˆ| = |(1 −
and hence | φˆi| < 1 for all i. Applying part (c), we have fi = φi/
5 − φˆi/
5, where the second term is smaller than 1/2 in absolute value. QED
(e). Prove that fi+2 ≥ φi^ for i ≥ 0.
Proof: First note that φ and φˆ are the roots of the quadratic P (z) = z^2 − z − 1. In particular, φ^2 = φ + 1, so that
φi−^1 + φi−^2 = φi−^2 (φ + 1) = φi−^2 φ^2 = φi,
which shows that the sequence φi^ satisfies the Fibonacci recursion (but not the initial conditions defining fi). We prove the assertion by induction on i ≥ 2.
For the base cases, note that f 2 = 1 = φ^0 and f 3 = 2 = (1+
φ^1. The inductive step uses the calculation:
fi+2 = fi+1 + fi ≥ φi−^1 + φi−^2 = φi
where the inequality follows from the inductive hypothesis and the equality follows from the fact that φ satisfies the recursion. QED
Problem 5: This problem is about the stack depth (which adds to the runspace requirements of the algorithm) of QuickSort. Here are two versions of QuickSort, for an array A with range [p,r). (Note that the notation here is the standard C interpretation of range, that includes the begin index and excludes the end index. This differs from the text.)
void QuickSort(A,p,r) void QuickSort2(A,p,r) { { if (r - p > 1) while (r - p > 1) { { q = partition(A,p,r); q = partition(A,p,r); QuickSort(A,p,q); QuickSort2(A,p,q); QuickSort(A,q+1,r); p = q + 1; } } } }
These each call the same version of Partition (below). (QuickSort2 is obtained from QuickSort by eliminating tail recursion, a process that can be formalized and accomplished by optimizing compilers.)
size_t partition(A,p,r) { i = p; for (j = p; j < r-1; ++j) { if (A[j] <= A[r-1]) { swap(A[i],A[j]); ++i; } } swap(A[i],A[r-1]); return i; }
(a) Give an informal argument that QuickSort2 is a sort.
Note that by setting p = q+1 at the end of the loop body of QuickSort2, the effect is that the next execution of the loop body results in the same process as a call to QuickSort(q+1,r). Thus the two algorithm bodies perform the same sequence of
tasks. Since we have already shown that QuickSort is a sort, so also must QuickSort be a sort.
(b) Describe a scenario in which the stack depth of QuickSort2 is Θ(n) on an n-element array (n = r − p).
If the input array is sorted, the partition index will always be the largest index in the range, resulting in recursive calls to QuickSort2(A,p,i) for i = r ... p, a total on n recursive calls.
(c) Modify the code for QuickSort2 so that the worst-case stack depth is Θ(log n), while maintaining O(n log n) expected runtime of the algoriithm.
Using a randomized version of Partition will at least make the expected stack usage O(log n), but we would still have the worst case Ω(n). To ensure that stack space does not grow worse than log n we can modify the algorithm so that the recursive call is made on the smaller of the two ranges:
void QuickSort3(A,p,r) { while (r - p > 1) { q = partition(A,p,r); if (q - p < r - q) { QuickSort3(A,p,q); p = q + 1; } else { QuickSort3(A, q+1, r) r = q; } } }
This modification ensures that the recursive call is made on a range that is no larger than 1/2 the size of the previous call and terminates when the range is 1, so there are at most log n recursive calls.
Inductive step part 1: P 1 (i) implies P 4 (i)
By P 3 (i − 1), A[i-1] is a smallest element of A[i-1..n), which implies that A[i-1] <= A[i]. P 4 (i) follows because A[0..i) is sorted.
Inductive step part 2: P 1 (i) and P 4 (i) imply P 1 (i + 1)
We are given that A[0..i) is sorted and must prove that A[0..i+1) is sorted after the next iteration of the loop. Invoking P 1 (i) and P 4 (i) we see that A[0..i) is sorted and that A[i] is at least as large as any element in A[0..i). Therefore A[0..i] = A[0..i+1) is sorted.
Problem 7: Provide worst and average case runtime analysis of SelectionSort.
The algorithm body runs exactly the same independent of data, because the lengths of the outer and inner loops are not data dependent. This runtime is
Θ(
∑^ n
i=
∑^ n
j=i
∑^ n
i=
(n − i)) = Θ(
∑^ n
i=
i) = Θ(n^2 )
by Equation (1) of Formulas.
Problem 8: Provide runspace analysis of SelectionSort.
There are four local variables used in the algorithm body, independent of n. There- fore the runspace is +Θ(1).