Assignment 1

Assignment 1
February 9, 2014
Author
Email:
Henry He (0810831)
[email protected]
1 Excercise 1:
Rank the following functions ofn by order of growth (starting with the slowest
growing). Functions with the same order of growth should be ranked equal:
First we have: Ω(1) = 1, Ω(n) = n, Ω(n−2 ) = n−2 ,Ω(log 3) = 1,
2
2
log n
Ω(n
) = 16log n = 24 log n = n4 ,
√ + log n) =√n, Ω(n ) = n , Ω(16
Ω( n log n) = n log n, Ω(log n) = log n, Ω(nlog 16 ) = n4 ; And based
on the knowledge from class we know that logarithm grows slower than
polynomial. So there we go for the order:
Solutions:
Odering: n−2 < 1 = log 3 < log n <
√
n log n < n = n + log n < n2 < nlog 16 =
16log n
2 Excercise 2:
(a).
Suppose we are comparing implementations of insertion sort and merge
sort on the same machine. For inputs of size n insertion sort runs in n2
steps, while merge sort runs in 64n log n steps. For which values of n does
insertion sort beat merge sort?
If the insertion sort in this case runs faster than the merge sort,
that means it took less steps than merge sort does, therefore we have the
inequality:64n log n > n2 (n > 0 obviously), by solving that equation (via
Wolframalpha), it shows it have 2 solutions: 1.001101 < n < 588.924,
hence it is a praticle computer science problem so n is an interger, therefore: when 1 < n < 589, the insertion sort beats the merge sort.
Solution:
(b).
What is the smallest value of n such that an algorithm whose running time
is 100n2 runs faster than an algorithm whose running time is 2n on the
same machine?
1
In this case if the rst algorithm runs faster than the second one,
that means the total steps it take is less than the total steps the second
algorithm take, So similarly we have the inequality 100n2 < 2n , Similarly
we have: n > 14.3247 or −0.0967 < n < 0.1037, asn needs to be a positive
integer, the nal anwser would be n > 15.
Solution:
3 Excercise 3:
Algorithm FindMax:
1.max = A[1]
2. for i = 2 to n
3.
do if A[i] > max
4.
then max = A[i]
5. return max
I listed the code from the original excercise above and following are
my prove of correctness for the loop from line 2 to line 5.
Solution:
For the for loop from line 2 to line 5, before the start of ecch
iteration the result max is the largest number in the array A[1...i] checked
previously.
Loop Invariant:
Before the rst loop interation, we only have 1 value checked
which is A[1], and according to the rst line max = A[1], so max is the
largest number in the A[1...1], trivial.
Initialization:
Assume the loop invariant holds before ith iteration, max is the
largest number in A[1...i − 1]. Now increment i, there are 2 cases:
Maintenance:
Case 1: A[i] > max, then max = A[i]. This means A[i] is
larger than all of A[1...i − 1], so A[i] is the largest number in the checked
array A[1...i], in this case loop invariant trivial.
Case 2: A[i] ≤ max, then max remain it's value. This means
when A[i] is no larger than the maximum number, max is the largest
number in the checked array A[1...i], the loop invariant holds in this case.
When loop terminates, we have i = n + 1, the for loop breaks,
before that happens, max is the largest number from A[1...n], so the entire
array has been examined and max is the largest value in the A, trivial.
Thus this algorithm is correct.
Termination:
line 1, took O(1) time, for giving a value, the loop in line
2 take O(n) as it go through the elements from 2 to n and line 3 and
4 only took O(1) time, line 5 took O(1) time, so the running time is
T (n) = O(1) + O(n) ∗ (O(1) + O(1)) + O(1) = O(n).
Running time:
2
4 Excercise 4:
Solution:
In the excercise it strictly specify S as a set, then mathematically speaking, the
elements of S are distinct , that means they are all dierent. also say S have n
elements.
1
Store the S into an array A.
2
Create an new array B , such that for i from 1 to n B[i] = x − A[i].
3
Merge sort A and B into a new array C. New array should have 2n elements.
4
Construct a for loop here, initially we say duplication = F ALSE , that means
we do not have a duplicate in the array C since we have not start checking
yet. For j from 2 to 2n, in each iteration, we compare the value of C[j − 1]
and C[j], if they are the same, then we let duplication = T RU E which
means we have a duplication in the array C , however if they are not the
same then we go to the next iteration, the duplication remain the same
value as before. At the end of the iteration return the value of duplication.
5
If dupilcation = T RU E , then there are at least a pair of number in S such
that the sum of the pair is x, if duplication = F ALSE then such pair
does not exist in S.
Correctness:
If array C have a pair of elements with the same value, then
array A countains a pair (a, b) such that a + b = x.
Statement 1:
Proof:
Now let us prove this statement, assume we have a pair of elements in
C , namely (C[k], C[l]), because the elements in S is distinct as it is strictly
dened as a set, and array A is the array storing the set S , so elements
from A is distinct, that means C[k] and C[l] came from dierent sets (from
A or B ), let's say C[k] is from B , that means there is an element A[m]
in A, such that A[m] + C[k] = x, (because of what we dened in line 2),
then as C[k] = C[l], that means A[m] + C[l] = x , because C[l] and A[m]
are both from A, therefore there are pair of elements (a, b) in A, such that
the a + b = x. So the Statement 1 is trivial.
Let's check the line 4.
Before the start of each iteration,if the array cheked in the
previous iterations (namelyC[1...j]) have a pair of duplicates then duplication =
T RU E , if the array cheked in the previous iterations (namelyC[1...j]) does
not have a pair of duplicates then duplication = F ALSE .
Loop Invariant:
Before the rst iteration, duplication = F ALSE in this case,
non of the elements are checked, so checked array is an empty array therefore there are no duplicates pairs, so duplication = F ALSE is trivial.
Initialization:
3
Maintenance:
Assume loop invariant is true before thej th iteration.
Case 1: duplication = F ALSE , then we start the next itereation,
C[j − 1] = C[j] , the duplication = T RU E , indeed C[j − 1] = C[j] means
there is a duplicate in the array, so in this case loop invariant trivial.
Case 2: duplication = T RU E , then we start the next itereation,
C[j − 1] = C[j] , the duplication = T RU E , indeed C[j − 1] = C[j] means
there is a duplicate in the array, so in this case loop invariant trivial.
Case 3: duplication = F ALSE , then we start the next itereation,
C[j − 1] 6= C[j], the loop does nothing, the value of the duplication stays
F ALSE , because the array C has already been sorted and C[j −1] < C[j],
it is not nessasary to check the whether C[1...j−2] is equal to C[j], because
all of the are smaller or equal to C[j−1] and C[j−1] 6= C[j], so there are no
duplication in the array and the array is sorted, so duplication = F ALSE
as it should be, in this case the loop invariant trivial.
Case 4: duplication = T RU E , then we start the next itereation,
C[j − 1] 6= C[j], then duplication value remains the same as the loop does
nothing and goes to next iteration which is T RU E , as C[1...j] already
coutains a duplicate the duplication = T RU E is trivial, so the loop in-
variant trivial in this case.
After the last iteration, whichj = 2n + 1, the loop ends, it returns duplication = T RU E if C[1...2n] have at least 1 duplicates, returns
duplication = F ALSE otherwise. The entire array is checked so it is
trivial.
Termination:
As for line(or step) 5, I already proved the statement in the statement 1 at the
beginning of the proof, as loop in line 4 does its duty which I also proved, the
algorithm is correct and does its duty.
Solution2:
5 Excercise 5:
(a).
Solution:
when n ≥ 2 and it's an exact power of 2,
the solution for T (n) mentioned is n log n.
Induction Hypothesis: P (n) :=
Base case: P (2) := T (2) = 2, 2 log 2 = 2, 2 log 2 = T (2).
Trivial.
Assume P ( 2i ) is true ( 2i > 2 and it is a power of 2)), so
i > 4, T ( 2i ) = 2i log 2i . Then
Induction steps:
4
T (i) = 2( 2i ) + n, then T (i) = 2 ∗ 2i log 2i + i = i log
i ∗ (log 2i + log 2) = i log( 2i ∗ 2) = i log i.
i
2
+ i = i ∗ (log
i
2
+ 1) =
So we have T (i) = i log i. So P ( 2i ) does impy P (i).
From base case and the induction steps, we can now safely say
the indunction hypothesis is true, when n ≥ 2 and it's an exact power of
2, the solution for T (n) mentioned is n log n.
Conclusion:
(b).
Let us look through this proof, the base case is correct, it does show
if the rst element works. However the induction step is really a mess, it
rst assume 6 ∗ n = 0 for n > 0, the next thing he needs to do is proving
from 6 ∗ n = 0 he can imply 6 ∗ (n + 1) is 0, and he did not do that, so
that is not correct.
Solution:
(c).
Solution:
Before I even started, I rst do some transformation, because some of the existing mathematical is not in the0 book, and I used them in my prove, I think
I have to justify them here by giving a short prove:
a From page 54 of the book we have for any real number u:u−1 < xuy ≤ u.
So −1 < xuy − u ≤ 0.
b And we have 0 ≤ u − xuy < 1.
c From u = u we haveu = xuy + (u − xuy),
d As xuy is always an interger and 0 ≤ u − xuy < 1(from b).
e We can callxuy the interger part of u and u − xuyfractional part of u.
(from c and d)
1. From page 54, denition 3,3 we know: x − 1 < xxy ≤ x, (x + 16 ) − 1 <
x(x + 16 )y ≤ (x + 16 ) and (x + 12 ) − 1 < x(x + 12 )y ≤ (x + 12 ) .
2. So x − 56 < xx + 16 y ≤ x + 16 and x − 12 < xx + 12 y ≤ x + 12
3. To satisfy the equation, we must have x −
x−
1
2
< xx +
1
2y
≤x+
1
6
1
2
< xx + 16 y ≤ x +
1
6
and
4. We then have x − 12 − (x + 16 ) < xx + 16 y − (x + 16 ) ≤ x + 16 − (x + 16 ) and
x−
1
2
− (x + 12 ) < xx + 12 y − (x + 12 ) ≤ x +
1
6
− (x + 12 )
5. To simplify this we have − 56 < xx + 16 y − (x + 16 ) ≤ 0 and −1 < xx + 12 y −
(x + 12 ) ≤ − 13 .
6. By multiply -1 on the inequalities from step 5 we have 0 ≤ (x + 16 ) − xx +
1
5
1
1
1
6 y < 6 and 3 6 (x + 2 ) − xx + 2 y < 1.
7. From step 6 we learned the fractional part of (x + 16 ) have to be in the
interval [0, 56 ), the fractional part of (x+ 12 ) have to be in the interval [ 13 , 1)
5
8. We now we assume y is the fractional part of x, then 0 ≤ y < 1. (from e)
9. For y + 16 it is between 0 to (1 + 16 ), as for y + 12 it is between 0 to (1 + 12 )
because y is a fraction. (from e)
10. So from 7, 8 and 9 we know, for y +
y + 16 − 1 < 56 .
11. From 7, 8 and 9 for y + 12 we have,
1
3
1
6
we have:0 ≤ y +
≤y+
1
2
1
6
< 1 or 0 ≤ y +
<
1
2
5
6
or 0 ≤
− 1 < 56 .
12. Combine 10 and 8 we have y ∈ [0, 23 ) ∪ [ 56 , 1).
13. Combine 11 and 8 we have y ∈ [0, 12 ) ∪ [ 12 , 1).
14. Combine 13 and 12, we have y ∈ [0, 12 ) ∪ [ 56 , 1).
15. As the fractional part y ∈ [0, 12 ) ∪ [ 56 , 1), we thus adding the interger part,
we have x ∈ [k, k + 12 ) ∪ [k + 56 , k + 1) for all k ∈ N0 .
6 Excercise 6:
(a)
If this is true, the it means there exisit 2 positive constant c and
n0 , such that 0 ≤ cn3 ≤ n2 for n ≥ n0 . Though it is already pretty
Solution:
obvious in this case it's not true, let us nevertheless use the proof by
contridiction. Let's assume you can nd 2 positive constant c and n0 ,
such that 0 ≤ cn3 ≤ n2 or n ≥ n0 . Then since n ≥ n0 that means n is
positive so n2 is t possitive, multiply both side of cn3 ≤ n2 by n12 , we have
cn ≤ 1, further more as c is a positive constant we have n ≤ 1c , since c is a
constant so is 1c , there's no way n is smaller than 1c , just try n = 2c , then
it is not trivial, so the statement is not true.
(b)
According to the denition ofΘin the page 44,n + log n = Θ(n) is
equivalent to say, there exisit positive sonstants c1 , c2 and n0 , such that
0 ≤ c1 n ≤ n + log n ≤ c2 n for all n ≥ n0 .
Solution:
1. Because of c1 , c2 and n0 are all positive, and n ≥ n0 , so obiviously c1 n
and c2 n are all larger than 0, let n0 = 2 then obivously, so log n > 0, hence
n + log n > 0 for n ≥ n0 .
2. Since n1 > 0, c1 n ∗ n1 ≤ (n + log n) ∗ n1 that is: c1 ≤ 1 + logn n because
n0 = 2 (as we dined in step 1), let c1 = 12 then obviously c1 < 1 + logn n
as logn n > 0 and so 1 + log n > 1 > c1 .
3. for n + log n ≤ c2 n let's transform it into log n ≤ c2 n − n = (c2 − 1)n let
c2 = 10:
6
(a) We have to prove log n ≤ 9n for n0 = 2 and n ≥ n0 .
i. The derivative of f (n) = log n is f 0 (n) = n ln1 2 , as n ≥ n0 , it
is always smaller than 2 ln1 2 which is smaller than 9, so it grows
slower than 9n.
ii. when n = 2, log n = 1 and 9n = 18, log n ≤ 9n.
iii. from i and ii, and the denition from calculus, we know log n ≤
9n for n0 = 2 and n ≥ n0 .
4. When c2 = 10 c1 = 12 , and n0 = 2, the 0 ≤ c1 n ≤ n + log n ≤ c2 n hold for
all n ≥ n0 .(1, 2, and 3)
5. So n + log n = Θ(n).
(c)
If this is true, the it means there exisit 2 positive constant c and
n0 , such that 0 ≤ n2 log n ≤ cn2 for n ≥ n0 . Let's assume that is true,
because n0 is positive therefore we can divide both side of n2 log n ≤ cn2
by n12 , then we have log n ≤ c, let n = 2c+1 then log n = c + 1 > c. That's
Solution:
not true, so we have a contridiction. The statement is not trivial.
(d)
√
From the denition on the text book n log n = O(n n) means
√ there
exisit 2 positive constant c and n0 , such that 0 ≤ n log n ≤ cn n for all
n ≥ n0 .
Solution:
√
1. Say n0 = 2 as c > 0, it is obivous in this case 0 ≤ n log n and 0 ≤ cn n.
√
√
2. Forn log n ≤ cn n we can transform it into 0 ≤ log n ≤ c n because
n ≥ n0 andn0 = 2.
(a) We can then further transform it into
3. Say c = 16, as log n grows slower than
polynomials,
4.
log
√n
n
≤
log
√2
2
√
log
√n
n
≤c
n because it grows slower than
< 16.
5. So when c = 16, n0 = 2, the statement hold. (4 and 5)
(e)
According to the denition ofΘin the page 44,n2 − 3n − 18 = Θ(n2 )
is equivalent to say, there exisit positive sonstants c1 , c2 and n0 , such that
0 ≤ c1 n2 ≤ n2 − 3n − 18 ≤ c2 n2 for all n ≥ n0 .
Solution:
1. Because of c1 , c2 and n0 are all positive, and n ≥ n0 , let n0 = 10 then
obivously, obivously .then c1 n2 > 0, n2 − 3n − 18 > 0, c2 n2 > 0
7
2. We can transform c1 n2 ≤ n2 − 3n − 18 into (1 − c1 )n2 − 3n − 18 ≥ 0
1
(a) let c1 = 1000
= 0.001, f (n) = 0.999n2 − 3n − 18 it is a in crease
0
function as f (n) = 1.998n − 3 > 0 for all n ≥ n0 .
(b) f (10) ≤ f (n) (from a)
(c) we now have f (10) = 51.9 > 0 so f (n) > 0.
(d) so when c1 = 0.001, we have c1 n2 ≤ n2 − 3n − 18 for all n ≥ n0 .
3. We can transform n2 − 3n − 18 ≤ c2 n2 into 1 − n3 − n182 ≤ c2 for or all
n ≥ n0 . it is obvious 1 − n3 − n182 ≤ 1 so let c2 = 1 we always have
n2 − 3n − 18 ≤ c2 n2 for all n ≥ n0 .
1
4. So from 1 2 and 3, we learned if c1 = 1000
, c2 = 1 and n0 = 10, we always
2
2
2
have 0 ≤ c1 n ≤ n − 3n − 18 ≤ c2 n for all n ≥ n0 .
5. from 4 we can conclude n2 − 3n − 18 = Θ(n2 )
(f )
Solution:
Assume it is not true, we have a funtion f (n) = Ω(n2 ) ∩ O(n log n)
1. We have positive numbersc1 c2 , and n0 and n1 such that, 0 ≤ f (n) ≤
c1 n log n for all n ≥ n0 .
2. and 0 ≤ c2 n2 ≤ f (n)for n ≥ n1
3. So we have c2 n2 ≤ c1 n log n.
c1
n
n
4. Thus we have n log
n = log n ≤ c2 , this is absurd as
function as n ≥ n0 . with no upper bond.
2
n
log n
is a increasing
√
(a) to prove it have no upper bond we rst have 0 < log n < n for
for somen ≥ k, k is a positive number. because it grows slower than
polynomial
√
(b) So logn n > √nn = n for all n ≥ k ,
(c) Since
√
n do not have a upper bond so does
n
log n
5. So we have a contridiction.
6. So the statement is true Ω(n2 ) ∩ O(n log n) is an empty set.
8