HW2 solution
4.2-3
How would you modify Strassenβs algorithm to multiply n*n matrices in which n
is not an exact power of 2? Show that the resulting algorithm runs in time
π(ππ₯π π )
Suppose 2πβ1 < n < N = 2π
We could extend original n*n matrices to N*N matrices by appending zeros.
Then the Strassenβs algorithm could work on extended matrices. Finally,
eliminating the appended elements in the matrices needs O(π2 ).
Since 2πβ1 < n, it follows that N < 2n. Therefore, the runtime becomes
ΞΈ((2n)lg 7 ) = π(2lg 7 β πlg 7 ) = π(πlg 7 )
4.3-2
Show that the solution of T (n) = T(βπ/πβ) + 1 is O( lgn )
We will use substitution method to verify the given solution. We start by trying
to prove that T (n) β¦ clg(n).
T (n) β¦ clg(βn/2β) + 1
< clg(n/2 + 1) + 1
π+2
= clg(
2
)+1
= clg(n + 2) - c + 1
This is inconclusive, so we will modify the solution a bit. We will try to prove
that T (n) β¦clg(n - b). Note that this still would imply that T (n) is O(lgn).
T (n) β¦ clg(βn/2β β π) + 1
< clg(n/2- b + 1) + 1
πβπβ(πβ2)
= clg(
2
)+1
= clg(n - b - (b - 2)) - clg2 + 1
β¦clg(n - b)
The last inequality is true if b β§ 2 and c β§ 1. Note that this still does not work
for n = 1 as it involves computing clog(1-2). It also does not work for n = 2. So
the simple solution is to move up the base case.
4.3-6
Show that the solution to T(n) = 2T( ο«n / 2ο» ο« 17 ) + n is O( nlgn )
Let us assume that it holds β m β€βπ/2 + 17β
So, T(m) β€ cm lg m
β m β€βπ/2 + 17β
Hence we can write
T(n) = 2T( βπ/2 + 17β ) + n
β€ 2c ( βπ/2 + 17β )lg ( βπ/2 + 17β ) + n
β€ 2c (n/2+17)lg(n/2 +17) + n
β€ 2c(n/2+17) lg(n/2+n/4) + n
β n/4 β₯ 17
= (cn + 34c) lg(3n/4) + n
= cn lgn β cn lg 4/3 + 34c lg n β 34c lg 4/3 + n
β€ cn lgn β cn lg 4/3 + 34c lg n + n
As lg n = 0(n) therefore 34 c lgn β€ kn β n β₯ no hence
T(n) β€ cn lg n + n(1- c lg 4/3) + kn
β n β₯ max {n0 , 68}
= cn lg n + n(k+1-c lg 4/3)
now we can always choose c such that
k+1 β c lg 4/3 β€ 0
k+1 β€ c lg 4/3
c β₯ (k+1)/ lg(4/3) = k
T(n) β€ cn lg n
for some constant c β₯ k and n β₯ max {n0, 68}β¦β¦β¦β¦.(Hence proved)
4.4-5
Use a recursion tree to determine a good asymptotic upper bound on the
recurrence π(π§) = π(π§ β π) + π(π§/π) + π§. Use the substitution method to
verify your answer.
Let A(n) = T(n/2) + n, and we could know that
T(n) = T(n β 1) + A(n)
= T(n β 2) + A(n β 1) + A(n)
β¦.
= A(1) + A(2) + β― + A(n) = βππ=1 π΄(π)
T(n) = βππ=1 π(π/2) + βππ=1 π
Assume n/2 is integer division, T(n/2) = T((n + 1)/2) for even n, so the first
sum consists of two equal halves : T(1) + T(1) + T(2) + T(2) + β―
π/2
T(n) = 2 β βπ=1 π(π ) + π β (π + 1)/2
Since T(m) β€ T(n/2) for every m β€ n/2
T(n) β€ n β T(n/2) + π β (π + 1)/2
Since T(n/2) β₯ n/2
T(n) β€ n β T(n/2) + (π + 1) β π(π/2) β€ 2 β (π + 1) β π(π/2)
Letβs consider this for only n = 2π , since T is monotonous: n = 2π and
U(k) = T(2π ) = T(n)
U(k) β€ 2 β 2π+1 β π(π β 1)
ο since n + 1 β€ 2π+1
Let L(k) = lg π(π)
L(k) β€ 1 + k + 1 + L(k β 1)
Therefore, we could get that
L(k) = O(π 2 )
2
U(k) = O(2(π ) )
2
T(n) = O(2((lg π) ) )
4.5-1
Use the master method to give tight asymptotic bounds for the following
recurrences.
b. T(n) = 2T(n/4)+βπ
case2:
T(n) = aT(n/b)+f(n)
If f(n) = ΞΈ(nlogb a) thenT(n) =ΞΈ(nlogb a πππ)
a = 2, b = 4, f (n) =βπ
nlog4 2 = π1/2 = f(n)
So , T(n) = ΞΈ(βππππ)
4-3
Give asymptotic upper and lower bounds for T(n) in each of the following
recurrences.Assume that T(n) is constant for sufficiently small n. Make your
bounds as tight as possible, and justify your answers.
b. π(π§) = ππ»(π/π) + π/ π₯π π
Solution 1
Let g(n) = π(π)/π
Then the equation can be transformed
ο¨ n β g(n) = 3 β (π/3) β π(π/3) + π/ lg π
ο¨ g(n) = π(π/3) + 1/ lg π
ο¨ g(n) = 1/ lg π + 1/ lg(π/3) + 1/ lg(π/9) + β―
ο¨ g(n) = ΞΈ(1/ lg π + 1/ (lg π β 1) + 1/ (lg π β 2) + 1/ (lg π β 3) + β¦)
lg π
// harmonic sum
ο¨ g(n) = ΞΈ(βπ=1 1/π )
ο¨ g(n) = ΞΈ(lg lg π)
Thus T(n) = n β g(n) = ΞΈ(n lg lg π)
Solution 2
T(n) = 3T(n/3) + n/ lg π
= 3(3T(n/9) + (n/3)/(lg(n/3))) + n/lg n
= 9T(n/9) + n/ lg(n/3) + n/ lg(n)
β¦β¦
πβ1
= 3π T(n/3π ) + βπβ1
)
π=1 π/lg(π/3
When i = log 3 π the first term reduces to 3log3 π π(1), so we have
(log π)β1
π/(lg(π/3πβ1 ))
T(n) = nΞΈ(1) + βπ=1 3
(log π)β1
= ΞΈ(n) + n βπ=1 3
(1/(lg π β (π β 1) lg 2 3))
(log π)β1
= ΞΈ(n) + n(1/ log 2 3) βπ=1 3
(1/(log 3 π β(π β 1)))
log π
= ΞΈ(n) + n log 3 2 βπ=23 (1/π)
This is the harmonic sum, so we have T(n) = ΞΈ(n) + n β log 3 2 β ln( log 3 π)
= ΞΈ(n lg lg π)
d. π(π§) = ππ»(π/π β π) + π/π
assume T(n) = O(n lg π)
T(n) = 3T(n/3 - 2)+ n/2
β€ 3c(n/3 - 2) lg(n/3 - 2) + n/2
β€ 3c(n/3) lg(n/3) + n/2
= cn lg(n/3) + n/2
= cnlg(n) β cnlg(3) + n/2
1
β€ cnlg(n)
---------- for c β₯ 2 lg 3
So the upper bound is O(n lg π)
assume T(n) = Ξ©(n lg π)
T(n) = 3T(n/3 - 2)+ n/2
β₯ 3c(n/3 - 2) lg(n/3 - 2) + n/2
β₯ 3c(n/3 - 2) lg(n/3 β n/6) + n/2
for n β₯ 12
= (cn β 6c) lg(n/6) + n/2
= cnlgn β cnlg6 β 6clgn + 6clg6 + n/2
β₯ cnlgn β cnlg6 β 6clgn + n/2
As lg n = O(n) therefore 6clgn β€ kn β n β₯ n0 hence
T(n) β₯ cnlgn + n(1/2 - clg6) β kn
= cnlgn + n(1/2 - clg6 β k)
now we can always choose c such that
1/2 - clg6 β k β€ 0
1/2 β k β€ clg6
c β₯ 1/(2lg6) β k/lg6
------ we choose c = 1/(2lg6)
T(n) β₯ cn lg n
Therefore, the time complexity is T(n) = ΞΈ(n lg π)
© Copyright 2026 Paperzz