3.1 For each of the five expressions of Figure 3.1, give the range of values ofn for which that expression is most efficient.
3.2 Graph the following expressions. For each expression, state the range of values ofnfor which that expression is the most efficient.
4n2 log3n 3n 20n 2 log2n n2/3 3.3 Arrange the following expressions by growth rate from slowest to fastest.
4n2 log3n n! 3n 20n 2 log2n n2/3 See Stirling’s approximation in Section 2.2 for help in classifyingn!. 3.4 (a) Suppose that a particular algorithm has time complexityT(n) = 3×
2n, and that executing an implementation of it on a particular machine takestseconds forninputs. Now suppose that we are presented with a machine that is 64 times as fast. How many inputs could we process on the new machine intseconds?
(b) Suppose that another algorithm has time complexityT(n) = n2, and that executing an implementation of it on a particular machine takes tseconds forninputs. Now suppose that we are presented with a ma- chine that is 64 times as fast. How many inputs could we process on the new machine intseconds?
(c) A third algorithm has time complexityT(n) = 8n. Executing an im- plementation of it on a particular machine takestseconds forninputs.
Given a new machine that is 64 times as fast, how many inputs could we process intseconds?
3.5 Hardware vendor XYZ Corp. claims that their latest computer will run 100 times faster than that of their competitor, Prunes, Inc. If the Prunes, Inc.
computer can execute a program on input of sizen in one hour, what size
input can XYZ’s computer execute in one hour for each algorithm with the following growth rate equations?
n n2 n3 2n
3.6 (a) Find a growth rate that squares the run time when we double the input size. That is, ifT(n) =X, thenT(2n) =x2
(b) Find a growth rate that cubes the run time when we double the input size. That is, ifT(n) =X, thenT(2n) =x3
3.7 Using the definition of big-Oh, show that 1 is inO(1)and that 1 is inO(n).
3.8 Using the definitions of big-Oh andΩ, find the upper and lower bounds for the following expressions. Be sure to state appropriate values forcandn0.
(a) c1n (b) c2n3+c3
(c) c4nlogn+c5n (d) c62n+c7n6
3.9 (a) What is the smallest integerksuch that√
n=O(nk)?
(b) What is the smallest integerksuch thatnlogn=O(nk)?
3.10 (a) Is2n= Θ(3n)? Explain why or why not.
(b) Is2n= Θ(3n)? Explain why or why not.
3.11 For each of the following pairs of functions, eitherf(n)is inO(g(n)),f(n) is inΩ(g(n)), orf(n) = Θ(g(n)). For each pair, determine which relation- ship is correct. Justify your answer, using the method of limits discussed in Section 3.4.5.
(a) f(n) = logn2; g(n) = logn+ 5. (b) f(n) =√
n; g(n) = logn2. (c) f(n) = log2n; g(n) = logn. (d) f(n) =n; g(n) =log2n.
(e) f(n) =nlogn+n; g(n) = logn. (f) f(n) = logn2; g(n) = (logn)2. (g) f(n) = 10; g(n) = log 10. (h) f(n) = 2n; g(n) = 10n2.
(i) f(n) = 2n; g(n) =nlogn.
(j) f(n) = 2n; g(n) = 3n. (k) f(n) = 2n; g(n) =nn.
3.12 DetermineΘfor the following code fragments in the average case. Assume that all variables are of typeint.
(a) a = b + c;
d = a + e;
(b) sum = 0;
for (i=0; i<3; i++) for (j=0; j<n; j++)
sum++;
(c) sum=0;
for (i=0; i<n*n; i++) sum++;
(d) for (i=0; i < n-1; i++) for (j=i+1; j < n; j++) {
tmp = AA[i][j];
AA[i][j] = AA[j][i];
AA[j][i] = tmp;
} (e) sum = 0;
for (i=1; i<=n; i++) for (j=1; j<=n; j*=2)
sum++;
(f) sum = 0;
for (i=1; i<=n; i*=2) for (j=1; j<=n; j++)
sum++;
(g) Assume that arrayAcontainsnvalues, Randomtakes constant time, andsorttakesnlognsteps.
for (i=0; i<n; i++) { for (j=0; j<n; j++)
A[i] = DSutil.random(n);
sort(A);
}
(h) Assume arrayAcontains a random permutation of the values from 0 to n−1.
sum = 0;
for (i=0; i<n; i++)
for (j=0; A[j]!=i; j++) sum++;
(i) sum = 0;
if (EVEN(n))
for (i=0; i<n; i++) sum++;
else
sum = sum + n;
3.13 Show that big-Theta notation (Θ) defines an equivalence relation on the set of functions.
3.14 Give the bestlowerbound that you can for the following code fragment, as a function of the initial value ofn.
while (n > 1) if (ODD(n))
n = 3 * n + 1;
else
n = n / 2;
Do you think that the upper bound is likely to be the same as the answer you gave for the lower bound?
3.15 Does every algorithm have aΘrunning-time equation? In other words, are the upper and lower bounds for the running time (on any specified class of inputs) always the same?
3.16 Does every problem for which there exists some algorithm have aΘrunning- time equation? In other words, for every problem, and for any specified class of inputs, is there some algorithm whose upper bound is equal to the problem’s lower bound?
3.17 Given an array storing integers ordered by value, modify the binary search routine to return the position of the first integer with valueKin the situation whereKcan appear multiple times in the array. Be sure that your algorithm isΘ(logn), that is, donotresort to sequential search once an occurrence of Kis found.
3.18 Given an array storing integers ordered by value, modify the binary search routine to return the position of the integer with the greatest value less than K when K itself does not appear in the array. Return ERRORif the least value in the array is greater thanK.
3.19 Modify the binary search routine to support search in an array of infinite size. In particular, you are given as input a sorted array and a key value K to search for. Callnthe position of the smallest value in the array that is equal to or larger thanX. Provide an algorithm that can determinenin O(logn)comparisons in the worst case. Explain why your algorithm meets the required time bound.
3.20 It is possible to change the way that we pick the dividing point in a binary search, and still get a working search routine. However, where we pick the dividing point could affect the performance of the algorithm.
(a) If we change the dividing point computation in functionbinaryfrom i= (l+r)/2toi = (l+ ((r−l)/3)), what will the worst-case run- ning time be in asymptotic terms? If the difference is only a constant time factor, how much slower or faster will the modified program be compared to the original version ofbinary?
(b) If we change the dividing point computation in functionbinaryfrom i= (l+r)/2toi=r−2, what will the worst-case running time be in asymptotic terms? If the difference is only a constant time factor, how much slower or faster will the modified program be compared to the original version ofbinary?
3.21 Design an algorithm to assemble a jigsaw puzzle. Assume that each piece has four sides, and that each piece’s final orientation is known (top, bottom, etc.). Assume that you have available a function
bool compare(Piece a, Piece b, Side ad)
that can tell, in constant time, whether piecea connects to piece b on a’s sideadandb’s opposite sidebd. The input to your algorithm should consist of ann×marray of random pieces, along with dimensions nandm. The algorithm should put the pieces in their correct positions in the array. Your algorithm should be as efficient as possible in the asymptotic sense. Write a summation for the running time of your algorithm onnpieces, and then derive a closed-form solution for the summation.
3.22 Can the average case cost for an algorithm be worse than the worst case cost?
Can it be better than the best case cost? Explain why or why not.
3.23 Prove that if an algorithm isΘ(f(n))in the average case, then it isΩ(f(n)) in the worst case.
3.24 Prove that if an algorithm isΘ(f(n))in the average case, then it isO(f(n)) in the best case.