Sei sulla pagina 1di 7

# CS350 - Assignment 1

## June 13, 2010

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

## 3. f (n) + O(f (n)) = Θ(f (n))

Let g(n) = O(f (n)), then 0 6 g(n) 6 cf (n), c>0.
We have:
f (n) 6 f (n) + g(n) 6 f (n) + cf (n) .
f (n) 6 f (n) + O(f (n)) 6 (1 + c)f (n) .
Thus, f (n) + O(f (n)) = Θ(f (n)) is always true.

## 4. f (n) = Ω(g(n)) and f (n) = o(g(n)).

never true since according to the definition f (n) = o(g(n)) means that
f (n) = O(g(n)) and f (n) 6= Ω(g(n)).

## 5. f (n) 6= O(g(n)) and g(n) 6= O(f (n))

For f (n) 6= O(g(n)), we have:

→ f (n)>cg(n)
f (n)
→ g(n)<
c

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

## Thus T (n) = Θ(2n ).

4. T (n) = T ( n) + Θ(lglgn)
Let m = lgn →n = 2m .
Then: T (2m ) = T (2m/2 ) + Θ(lgm).

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

## 5. T (n) = 10T (n/3) + 17n1.2

Apply Master Theorem with a = 10 and b = 3, we have:
nlogb a = nlog3 10 .
We are in case 1 since the function nlogb a is larger.
Thus T (n) = Θ(nlog3 10 ).

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:

## T (n) 6 c(n − 2)lg(n − 2) + lgn

= cnlg(n − 2) − 2clg(n − 2) + lgn
6 cnlgn − clgn + lgn
6 cnlgn − clgn
6 cnlgn

## - Thus T (n) = O(nlgn).

- Similarly, prove: T (n) > cnlgn, for some c>0.

3
- We have:

## cnlgn 6 c(n − 2)lg(n − 2) + lgn

= cnlg(n − 2) − 2clg(n − 2) + lgn
6 cnlgn − clgn + lgn

## - Since c>0 so we choose c = 1.

- Thus T (n) = Ω(nlgn).
- Therefore, T (n) = Θ(nlgn).

## 9. T (n) = T (n/5) + T (4n/5) + Θ(n)

- Guess: T (n) = Θ(nlogn).
- Prove: T (n) 6 dnlogn, for some d>0.
- We have for some c>0.

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

## - Thus T (n) = O(nlogn).

-Similarly, prove: T (n) > dnlogn, for some d>0.
- We have for some c>0.

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.

## 4.1-1 If all elements of array A are negative, Find-Maximum-Subarray

returns the index of the smallest negative element of A. For example: A =
[−34, −23, −24, −32, −35, −54, −13, −10, −56] then the result will be the in-
dex of −10.

## 4.1-2 Write Pseudocode fro brute-force method of solving the maximum-

subarray problem.

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].

## 1 -2 4 9 -3 max -sum = -∞, left = 0 , right = 0

1 -2 4 9 -3 max -sum = -1 , left = 1 , right = 2
1 -2 4 9 -3 max -sum = 3 , left = 1 , right = 3
1 -2 4 9 -3 max -sum = 12 , left = 1 , right = 4
1 -2 4 9 -3 max -sum = 12 , left = 1 , right = 4
1 -2 4 9 -3 max -sum = 12 , left = 1 , right = 4
1 -2 4 9 -3 max -sum = 12 , left = 1 , right = 4
1 -2 4 9 -3 max -sum = 12 , left = 1 , right = 4
1 -2 4 9 -3 max -sum = 12 , left = 1 , right = 4
1 -2 4 9 -3 max -sum = 13 , left = 3 , right = 4
1 -2 4 9 -3 max -sum = 13 , left = 3 , right = 4
- The maximum-subarray is [4, 9].

## 4.1-5 Linear-algorithm for maximum-subarray problem.

- Since we know the maximum-subarray of A[1..j] and by observation a
maximum-subarray of A[1..j + 1] is either a maximum-subarray of A[1..j] or
a subbarray A[i..j + 1], for some 1 6 i 6 j + 1.
- Therefore, we need to check from the index j + 1 down to 1 to see if a
maximum sum is changed or not. If we find the new maximum sum differs
from the maximum sum of maximum-subarray of A[1..j], then maximum-
subarray is A[i..j + 1].
Pseudocode
Find-Maximum-Subarray(A)
1 max -sum ← A[1..j]
2 sum := 0
3 i := 0
4 for j := j + 1 downto 1
5 sum := sum + A[j]
6 if (sum > max -sum)
7 max -sum := sum
8 i := j
Analysis
- Lines 1-3 initilize variables, thus, it takes the constant time T (1) = Θ(1).
- Lines 4-8 use for loop to find the new sum from j + 1 downto 1, it takes
the running time is T (j) = Θ(j), j > 0. - Thus, the algorithm takes a linear-
time running for the maximum-subarray problem.