Assignment 1
Assignment 1
Assignment 1
1 Problem 1.1
1. f (n) = O(f (n)2 )
is sometimes true since for f (n) = n, it’s true. But for f (n) = 1/n, it’s not
true.
2. f (n) + g(n) = Θ(max(f (n), g(n))
is always true since we have that:
max(f (n), g(n)) 6 f (n) + g(n) 6 cmax(f (n), g(n)), c > 1
Therefore, we have:
f (n) + g(n) = Θ(max(f (n), g(n))
→ f (n)>cg(n)
f (n)
→ g(n)<
c
→ g(n) = O(f (n))(Contradict)
1
2 Problem 1.2
1. T (n) = 2T (n/3) + nlgn
Apply Master Theorem with a = 2 and b = 3, we have:
nlogb a = nlog3 2 .
We are in case 3 since the function f (n) is larger.
Check the regularity condition for some c<1:
n n
→ 2 lg 6 cnlgn
3 3
2 n
→ lg 6 clgn
3 3
2
If we choose c = then it’s true that:
3
→ 23 lg n3 < 23 lgn
Thus T (n) = Θ(nlgn)
2. T (n) = 3T (n/5) + lg 2 n
Apply Master Theorem with a = 3 and b = 5, we have:
nlogb a = nlog5 3 .
We are in case 1 since the function nlogb a is larger.
Thus T (n) = Θ(nlog5 3 ).
3. T (n) = T (n/2)+2n
Apply Master Theorem with a = 1 and b = 2, we have:
nlogb a = nlog2 1 .
We are in case 3 since the function f (n) = 2n is larger than nlog2 1 = n0 = 1.
Check the regularity condition for some c<1:
2n
→ 6 c2n
2
→ 2n−1 6 c2n
1
→ 2n−1 6 2n
2
→ 2n−1 6 2n−1
2
We choose S(m) = T (2m ) so that:
S(m) = S(m/2) + Θ(lgm)
Apply Master Theorem with a = 1 and b = 2, we have:
mlogb a = mlog2 1 = m0
We are in case 2 since the two functions f (m) = mlogb a = 1.
Thus S(m) = Θ(lg 2 m) →T(n) = Θ(lg 2 lgn).
6. T (n) = 7T (n/2) + n3
Apply Master Theorem with a = 7 and b = 2, we have:
nlogb a = nlog2 7 .
We are in case 3 since the function f (n) = n3 is larger than nlog2 7 .
Check the regularity condition for some c<1:
3
→ 7 n8 6 cn3
If we choose c = 78 , it’s true that:
3 3
→ 7 n8 6 7n8
Thus T (n) = Θ(n3 ).
√ √
7. T (n) = T (n/2 + n) + 6046
- For large enough n, we have:
√
T (n/2) 6 T (n/2 + n) 6 T (3n/4)
- Thus T (n) = Θ(lgn)
8. T (n) = T (n − 2) + lgn
- Guess: T (n) = Θ(nlgn).
- Prove: T (n) 6 cnlgn, for some c>0.
- We have:
3
- We have:
n n 4n 4n
T (n) 6 d log + d log + cn
5 5 5 5
n 1 4n 4
6 d (logn − log ) + d (logn − log ) + cn
5 5 5 5
n 1 4n 4
= dnlogn − d( log + log ) + cn
5 5 5 5
n n 4n 4n
= dnlogn − d( log1 − log5 + log4 − log5) + cn
5 5 5 5
1 4
= dnlogn − dn( log1 + log4 − log5) + cn
5 5
c
6 dnlogn, d > 1 4
5 log1 + 5 log4 − log5
n n 4n 4n
dnlogn 6 d log + d log + cn
5 5 5 5
1 4
dnlogn 6 dnlogn − dn( log1 + log4 − log5) + cn
5 5
c
- It’s true if d 6 1
log1+ 45 log4−log5
5
-Thus T (n) = Ω(nlogn).
- Therefore, T (n) = Θ(nlogn)
√ √
10. T (n) = nT ( n) + 100n
- Let S(n) = T (n)/n, we have:
√
S(n) = S( n) + 100
4
- Let m = lgn → n = 2m
S(2m ) = S(2m/2 ) + 100
- Rename S(2m ) by F (m), we have:
F (m) = F (m/2) + 100
- Appy Master Theorem with a = 1 and b = 2, we have:
mlog2 1 = 1
- We are in case 2, then the solution of F (m) is Θ(lgm)
- Thus, the solution of S(m) is Θ(lglgn)
- Finally, we have T (n) = Θ(nlglgn).
3 Problem 1.3
Summary of maximum-subarray problem:
- Suppose we want to find the nonempty, contiguous subarray of an array
A[low..high] whose values have the largest sum. To solve this problem, we
divide A into 2 subarrays of as equal size as possible. By using the mid-
point of array mid = high/2, then we have two subarray A[low..mid] and
A[mid + 1..high]. Thus, the maximum-subarray must be in one of the fol-
lowing parts:
1. entirely in A[low..mid]
2. entirely in A[mid + 1..high]
3. crossing the midpoint low 6 i 6 mid<j 6 high
- We can find the maximum-subarrays of A[low..mid] and A[mid + 1..high]
recursively. For finding the maximum-subarray that crossing the midpoint,
that is we need to find maximum-subarray of the form A[i..mid] and A[mid+
1..j] and then combine them.
5
Find-Maximum-Subarray(A)
1 max -sum := -∞
2 sum := 0
3 left := 0
4 right := 0
5 for i = 1 to A.length-1
6 sum := A[i]
7 for j = i + 1 to A.length
8 sum := sum + A[j]
9 if (sum > max -sum)
10 max -sum := sum
11 left := i
12 right := j
13 return (left, right, max -sum)
Prove-of-correctness
- The first for loop scans each element in A.
- For 2 6 k 6 n then the (k − 1)th iteration of the for loop j = k assigns
A[k] to sum. Then entering the second for loop to find the sum of A[k −1..j]
and compare with the maximum sum. The result of this iteration is the left
index, right index and the max -sum (*).
- For the base case: k = 1 then clearly (*) is true. (A has only 1 element)
- Assume that when k 6 n − 1 (*) is true. - Next we enter the next for loop
of computing sum i = k + 1. - Then sum is assigned to A[k + 1] and enter
the second for loop to compute the sum of A[i + 1..j] to compare with the
maximum sum max -sum. - It’s clearly that after exit the second for loop
the indices and the maximum sum will only be updated if the if condition
is satisfied.
Analysis
- We denote by T (n) the running time of Find-Maximum-Subarray on a
subarray of n elements.
- Lines 1-4 initialize the variables max -sum, sum, left, right where max -sum
holds the greatest sum of a subarray[left..right], sum keeps track the current
sum of an arbitrary subarray of A. Therefore, each line takes a constant time
T (1) = Θ(1). The running time for initializing all variables is 4Θ(1).
- Lines 5-12 is the for loop for finding maximum subbarry. Since we use the
first for loop to scan through the length of A and the second for loop to
compute the sum of each possible subarray of A to find the greatest sum.
Therefore, it takes the running time is T (n) = Θ(n2 )
- Combining two equations gives us the total running time T (n) of finding
Find-Maximum-Subarray:
T (n) = 4Θ(1) + Θ(n2 )
- Thus T (n) = Θ(n ). 2
6
Instance Test.
- Apply the algorithm for the array A[1, −2, 4, 9, −3].