CSE 5311 Homework 1 Solution: Problem 2.2-1
CSE 5311 Homework 1 Solution: Problem 2.2-1
Problem 2.2-1
Express the function n3 /1000 − 100n2 − 100n + 3 in terms of Θ-notation
Answer
Θ(n3 ).
Problem 2.3-3
Use mathematical induction to show that when n is an exact power of 2, the
solution of the recurrence
(
2 if n = 2,
T (n) =
2T (n/2) + n if n = 2k , for k > 1.
is T (n) = n lg n
Answer
First, establish a function for induction:
F (k) = 2k lg 2k (2)
Base case:
Prove for k + 1:
2k+1
F (k + 1) = T (2k+1 ) = 2T ( ) + 2k+1 (4)
2
= 2T (2k ) + 2k+1 = 2 · 2k lg 2k + 2k+1 (5)
k+1 k+1
=2 lg 2 (6)
1
Problem 2-1 Insertion sort on small arrays in
merge sort
Although merge sort runs in Θ(lg n) worst-case time and insertion sort runs
in Θ(n2 ) worst-case time, the constant factors in insertion sort can make it
faster in practice for small problem sizes on many machines. Thus, it makes
sense to coarsen the leaves of the recursion by using insertion sort within merge
sort when subproblems become sufficiently small. Consider a modification to
merge sort in which n/k sublists of length k are sorted using insertion sort and
then merged using the standard merging mechanism, where k is a value to be
determined.
1. Show that insertion sort can sort the n/k sublists, each of length k, in
Θ(nk) worst-case time.
2. Show how to merge the sublists in Θ(n lg(n/k)) worst-case time.
Answer
1. Sorting sublists
Note that sorting each list takes ak 2 + bk + c for some constants a, b and c. We
have n/k of those, thus:
n cn
(ak 2 + bk + c) = ank + bn + = Θ(nk)
k k
2. Merging sublists
Sorting a sublists of length k each takes:
(
0 if a = 1,
T (a) =
2T (a/2) + ak if a = 2p , if p > 0.
This makes sense, since merging one sublist is trivial and merging a sublists
means splitting dividing them in two groups of a/2 lists, merging each group
recursively and then combining the results in ak steps, since have two arrays,
each of length a2 k.
Proof by induction:
Base. Simple as ever:
T (1) = 1k lg 1 = k · 0 = 0
Step. Assume that T (a) = ak lg a and we calculate T (2a):
2
T (2a) = 2T (a) + 2ak = 2(T (a) + ak) = 2(ak lg a + ak) = (7)
= 2ak(lg a + 1) = 2ak(lg a + lg 2) = 2ak lg(2a) (8)
This proves it. Now if we substitue the number of sublists n/k for a:
n n
T (n/k) = k lg = n lg(n/k)
k k
While this is exact only when n/k is a power of 2, it tells us that the overall
time complexity of the merge is Θ(n lg(n/k)).
3
Answer
Let’s see that p(n) = O(nd ). We need do pick c = ad + b, such that:
d
X
= ad nd + ad−1 nd−1 + . . . + a1 n + a0 ≤ cnd
i=0
Answer
A B O o Ω ω Θ
lgk n n yes yes no no no
nk cn yes yes no no no
√
n nsin n no no no no no
2n 2n/2 no no yes yes no
nlg c clg n yes no yes no yes
lg(n!) lg(nn ) yes no yes no yes
4
Problem 3-6 Iterated functions
We can apply the iteration operator ∗ used in the lg∗ function to any monoton-
ically increasing function f (n) over the reals. For a given constant c ∈ R, we
define the iterated function fc∗ by
Answer
As shown in the table:
Problem 4.5-1
Use the master method to give tight asymptotic bounds for the following recur-
rences:
1. T (n) = 2T (n/4) + 1
√
2. T (n) = 2T (n/4) + n
3. T (n) = 2T (n/4) + n
4. T (n) = 2T (n/4) + n2
Answer
√
1. Θ(nlog4 2 ) = Θ( n)
√
2. Θ(nlog4 2 lg n) = Θ( n lg n)
3. Θ(n)
4. Θ(n2 )
5
Problem 4.5-4
Can the master method be applied to the recurrence T (n) = 4T (n/2) + n2 lg n?
Why or why not? Give an asymptotic upper bound for this recurrence.
Answer
With a = 4, b = 2, we have f (n) = n2 lg n 6= O(n2− ) 6= Ω(n2− ), so no - we
cannot apply the master method.
Let’s guess Θ(n2 lg2 n):
1. T (n) = 2T (n/2) + n4
2. T (n) = T (7n/10) + n
3. T (n) = 16T (n/4) + n2
4. T (n) = 7T (n/3) + n2
5. T (n) = 7T (n/2) + n2
√
6. T (n) = 2T (n/4) + n
7. T (n) = T (n − 2) + n2
Problem 30-3
1. Θ(n4 ) (master method)
2. Θ(n) (master method, log10/7 1 = 0)
6
3. Θ(n2 lg n) (master method)
4. Θ(n2 ) (master method)
5. Θ(nlog2 7 ) (master method)
√
6. Θ( n lgn ) (master method)
Pn/2
7. T (n) = n2 + T (n − 2) = n2 + (n − 2)2 + T (n − 4) = i=0 (n − 2i)
2
= Θ(n3 )
Problem 30-3
We can generalize the 1-dimensional discrete Fourier transform defined by
equa- tion (30.8) to d dimensions. The input is a d -dimensional array A =
(aj1 ,j2 ,··· ,jd ) whose dimensions are n1 , n2 , · · · , nd , where n1 n2 · · · nd = n. We
define the d-dimensional discrete Fourier transform by the equation
1 −1 n
nX X 2 −1 d −1
nX
yk1 ,k2 ,··· ,kd = ... aj1 ,j2 ,...,jd ωnj11k1 ωnj22k2 · · · ωnjddkd (16)
j1 =0 j2 =0 jd =0
Answer
a.
Reorder the summation as follows
1 −1 n
nX X 2 −1 d −1
nX
yk1 ,k2 ,··· ,kd = ... aj1 ,j2 ,...,jd ωnj11k1 ωnj22k2 · · · ωnjddkd (17)
j1 =0 j2 =0 jd =0
d −1
nX nd−1 −1 2 −1
nX 1 −1
nX
X
ωnjd kd ωnjd−1 kd−1 . . . ωnj2 k2 j1 k1
= d d−1 2
aj1 ,j2 ,...,jd ωn1 .
jd =0 jd−1 =0 j2 =0 j1 =0
(18)
7
b.
With the result of a. and use the properties of summation and multiplication,
it can be proved that any two indexes s and t (1 ≤ s, t ≤ d) can be switch in
Equation 18. Hence, prove the hypothesis.
c.
The complexity of FFT on each dimension is