HW2 solution - ShareCourse

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 𝑛)