



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS 200, Written Homework 2 1
Assuming that log 2 = 1, then it follows that { x^3 log x ≥ 2 x^2
∀x ≥ 2 ∴ let k = 2 After we have established this, it becomes clear that both of the terms in f (x) are ≤ x^3 log x when x > k. ∴
f (x) ≤ 2 x^3 log x
∀x > k ∴ let C = 2, g(x) = x^3 log x We can then conclude that ∀x > k, |f (x)| ≤ C|g(x)| and that f (x) is O(g(x)) = O(x^3 log x).
To address the problem which smallest integer value of n for O(xn) suits f (x) the best, we see that ∀x > k, x^3 < x^3 log x < x^4. Therefore, we can conclude that the best such n = 4 and that f (x) is also O(x^4 ).
(b) let f (x) =
x^4 + x^2 + 1 x^4 + 1
We can see that ∀x ≥ 1,
x^2 ≤ x^4 1 ≤ x^4 1 x^4 + 1
x^4
∴ let k = 1 We can then see that ∀x > k
f (x) ≤ 3 x^4 x^4
→ f (x) ≤ 3 ∴ let C = 3, g(x) = 1. From this, we can conclude that ∀x > k, |f (x)| ≤ C|g(x)| and that f (x) is O(g(x)) = O(1).
Since we have shown that f (x) is O(x^0 ) = O(1) above, the smallest such n = 0.
CS 200, Written Homework 2 2
(c) let f (x) = 3x^2 + 8x^5 + 1
We can see that ∀x ≥ 2, { x^5 ≥ 3 x^2 x^5 ≥ 1
∴ let k = 2 We can now see that ∀x > k, f (x) ≤ 10 x^5. ∴ let C = 10, g(x) = x^5. It then follows that ∀x > k, |f (x)| ≤ C|g(x)|. From this we can conclude that f (x) is O(g(x)) = O(x^5 ), which also answers the other part of the question. The smallest n such that f (x) is O(xn) is n = 5.
Algorithm 1 Sort Last Four 1: procedure sortLastFour(A :: Array) 2: n ← sizeof(A) 3: changed ← true 4: while changed = true do 5: changed ← false 6: for i = (n − 5); i < (n − 1); i++ do 7: if A[i] > A[i + 1] then 8: buf f er ← A[i] 9: A[i] ← A[i + 1] 10: A[i + 1] ← buf f er 11: changed ← true 12: end if 13: end for 14: end while 15: end procedure
Since the only nodes this method ever looks at in the array are the last four, the number of com- parisons for any given array is bounded. That is, the number of comparisons done has no relation to n. Therefore, this algorithm is O(1).
CS 200, Written Homework 2 4
For this example, the sort would first swap 5 and 1, requiring one comparison to verify that 1 < 5. It would then ”insert” 2 in the correct position, requiring two comparisons to verify that 1 < 2 < 5. This would continue until all of the elements to the right of 5 have been inserted to the left of it. In general, this requires a number of comparisons equal to
(n − 1) + (n − 2) + (n − 3) + · · · + 1 =
n∑− 1
i=
n − i (4.1)
It was shown in problem 3 that this can be simplified to
n^2 − n 2
If n were to double in size such that the input given to insertion sort is now of size 2n and is arranged in the same way as the worst case input shown above is, the number of comparisons would increase to (2n)^2 − 2 n 2
= 2n^2 − n (4.3)
More generally, in terms of order of magnitude, this algorithm requires O(n^2 ) comparisons for input arranged in the way earlier described, so doubling n effectively quadruples the number of comparisons that need to be made. (c) Selection sort requires the greatest number of comparisons when an array is in descending order. Here’s an example. 5 4 3 2 1 Selection sort would first verify that 5 is the maximum integer in the array, requiring 5 compar- isons (one to see if 5 < MAX INT or if this is the first element it needs to check and four more to compare 5 to each of the raining elements in the array). It would then require 4, then 3 and finally 2. Generally, the number of comparisons required by selection sort for input regained in this way is
n + (n − 1) + (n − 2) + · · · + 2 =
n∑− 2
i=
n − i =
n∑− 1
i=
n −
n∑− 2
i=
i (4.4)
It was shown in problem 3 that this can be simplified using (3.2). n∑− 1
i=
n = (n − 1)n = n^2 − n (4.5)
CS 200, Written Homework 2 5
n∑− 2
i=
i =
(n − 2)(n + 2) 2
n^2 2
Combining results (4.5) and (4.6) to yield a simplified version of (4.4)
( n^2 − n
n^2 2
n^2 2
− n + 2 (4.7)
Therefore, doubling the input size n would result in the number of comparisons required to increase by a number of times proportional to n.
More importantly, as n → ∞, the terms < n^2 in the numerator and denominator become ir- relevant and we can see that as n grows very large, the number of comparisons required for 2n data points comes closer and closer to quadruple the number required for the same operation with n data points.
More generally, since (4.7) is O(n^2 ), doubling the input effectively quadruples the number of comparisons needed.
(a) As a preliminary step, I first generated an for 0 ≤ n ≤ 4. Intuitively, we can see that since each successive bit string of length n > 3 has at least 2an− 1 bit strings containing three consecutive 0s (since a bit string of length n > 3 can be prefixed with either a 0 or a 1 and suffixed with each of the combinations present in a bit string of length n − 1). However, we can also see that there are more than 2an− 1. For example, a 3 = 1 (000), but a 4 = 3 (0000, 1000, 0001). I therefore concluded that an = 2an− 1 + f (n) (5.1) Where f (n) is some increasing function dependent on n. To further examine this pattern, I wrote a Java program that generated a more complete table (up to n = 10). I then examined an − 2 an− 1 and saw that it seemed to be growing pseudo-exponentially with a base of 2 and an exponent of n − 4. I then looked at 2n−^4 − (an − (2an− 1 )) and saw some recurrence! I saw that an− 4 = 2n−^4 − an + 2an− 1 (5.2) With some algebra, we can then see that
an = 2an− 1 + 2n−^4 − an− 4 (5.3)
CS 200, Written Homework 2 7
(a) Recursively divide the list of vote pairs into two sub-problems to determine if there are one or two candidates that have at least n/2 votes, where n is the size of the current sub-problem. If there is a tie, we should throw out the results and return some value indicating that there is no candidate in this half with a majority of the votes. If a candidate has n/2 of the votes, then at least one of the halves has to consist mostly of votes for that candidate.
Base-Case: If the size of the problem is one pair of votes, then each of the candidates represented by the pair should be returned to the call’s parent. For example, if the size of the parent is n = 2, then it will receive two arrays (Ai, Aj ) containing each of the votes contained by its sub-problems. It will then determine if there is one or two candidates within them that has the majority of the votes for Ai ∪ Aj. If so, it should return an array containing the candidate(s) that meet these conditions. (b) let f (n) be the total number of comparisons needed by this algorithm where n is the size of the problem and is even.
We can see that this algorithm divides the problem into two sub-problems, both of size n/2. Therefore, we can see that in
f (n) = af
( (^) n b
a = 2 and b = 2. We can also see that the conquer part of the algorithm steps over n elements, doing n comparisons to count the number of votes that a candidate received. It does this up to twice, requiring a total of 2n comparisons. From here, we can see that g(n) = 2n. Therefore, we have all the pieces of f (n).
f (n) = 2f
( (^) n 2
Now, by the master theorem, f (n) is O(n log n).