0% found this document useful (0 votes)
40 views7 pages

Chapter1 Odd

2020111522256195Foundations of Algorithms - Richard E. Neapolitan Chapter1_odd Solution

Uploaded by

히마가나
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views7 pages

Chapter1 Odd

2020111522256195Foundations of Algorithms - Richard E. Neapolitan Chapter1_odd Solution

Uploaded by

히마가나
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 1: Algorithms: Efficiency, Analysis, and Order Solutions

Section 1.1

1) Write an algorithm that finds the largest number in a list (an array) of n numbers.

Input: positive integer n, list of numbers S indexed from 1 to n

Output: the maximum element in the list S


number findMax(int n, const keytype S[ ]) {
index i;
number max = S[1];
for(i =2; i<=n; i++)
if(S[i] > max)
max = S[i];
return max;
}

3) Write an algorithm that prints out all the subsets of three elements of a set of n
elements. The elements of this set are stored in a list that is the input to the algorithm.

Input: positive integer n, list of numbers S indexed from 1 to n

Output: none (the results are simply printed)

void nC3(int n, const keytype S[ ]) {


index i, j, k;
for (i =1; i<=n; i++)
for (j=i+1; j<=n;, j++)
for (k=j+1; k<=n; k++)
cout <<S[i] <<S[j] <<S[k] <<endl;
Chapter 1: Algorithms: Efficiency, Analysis, and Order Solutions

5) Write an algorithm that finds the greatest common divisor of two integers.

Input: two integers a and b

Output: the greatest common divisor of a and b

int gcd(int a, int b) {

if(b == 0)

return a;

return gcd(b, a%b) ;

7) Write an algorithm that determines whether or not an almost complete binary tree
is a heap.

Input: a pointer tree to an almost complete binary tree

Output: true if the input tree is a heap, false otherwise

bool isHeap(tree_pointer tree) {

if(tree->left == NULL && tree-> right == NULL)

return true;

if(tree->key < tree->left->key)

return false;

if(tree->right != NULL && tree->key < tree->right->key)

return false;

if(tree->right == null)

return isHeap(tree->left);

return isHeap(tree->left) && isHeap(tree->right);

}
Chapter 1: Algorithms: Efficiency, Analysis, and Order Solutions

Section 1.2

9) Give a practical example in which you would not use Exchange Sort (Algorithm 1.3)
to do a sorting task.

Is we had to sort an array of 10 million elements, the number of comparisons in


Exchange Sort would be  107 x 107 = 1014. Even performing one comparison every
nanosecond, the program would still run for 105 seconds, which is more than one day.
A more efficient algorithm like Mergesort would run in less than half a second.
If we tried Exchange Sort algorithm for 100M elements, it would take almost 4 months,
whereas Mergesort would finish in about 5 seconds!

Section 1.3

11) Determine the worst-case, average-case, and best-case time complexities for the
basic Insertion Sort and for the version given in Exercise 4, which uses Binary Search.

In this solution, we only count comparisons:

Basic Insertion Sort: B(n) = n (array already sorted), A(n) = n2/4, W(n) = n2/2 (array
reverse sorted). Proofs can be found in Section 7.2.

Insertion Sort with Binary Search: B(n) = 2lg n (each new key is always inserted in the
middle of the array, so only 2 comparisons are needed to find its location … if repeated
keys are allowed, than an array of identical elements only requires 1 comparison per
key!), A(n) = nlg n, W(n) = nlg n.
Chapter 1: Algorithms: Efficiency, Analysis, and Order Solutions

13) Algorithm A performs 10n2 basic operations, and algorithm B performs 300ln n
basic operations. For what value of n does algorithm B start to show its better
performance?

For n = 7, we have 490 operations in A, and 584 in B, so A is better, but for for n = 8, we
have 640 operations in A and 624 in B, so B is better (and stays that way for all n >= 8).

Section 1.4

15) Show directly that f(n) = n2 + 3n3  (n3). That is, use the definitions of O and  to
show that f(n) is in both O(n3) and (n3).

We show that n2 + 3n3  O(n3) because for n0,


n2 + 3n3  4n3 ,
we can take c = 4, N = 0 to obtain our result.
We show that n2 + 3n3  (n3) because for n0,
n2 + 3n3  3n3 ,
we can take c = 3, N = 0 to obtain our result.
Thus, since n2 + 3n3  O(n3) and n2 + 3n3  (n3), n2 + 3n3  (n3).

17) Using the Properties of Order in Section 1.4.2, show that

5n5 + 4n4 + 6n3 + 2n2 + n + 7  (n5).

Apply Property 7, with g(n) = n4 + 6n3/4 + 2n2/4 + n/4 + 7/4, h(n) = n5 , c = 4, and d =
5.
A direct proof is also possible: take f(n)= 5n5 + 4n4 + 6n3 + 2n2 + n + 7, and we have
5n5 + 4n4 + 6n3 + 2n2 + n + 7 <=5n5 + 4n4 + 6n3 + 2n2 + n + 7 <= 5n5 + 4 n5 + 6 n5 + 2
n5 + n5 + 7 n5
5n5<=5n5 + 4n4 + 6n3 + 2n2 + n + 7 <=25n5
Therefore the order will be Θ(n5), with c1=5 and c2=25, where n>=1.

19) The function f(x) = 3n2+10n log n+1000n+4log n+9999 belongs in which of the
following complexity categories:
Chapter 1: Algorithms: Efficiency, Analysis, and Order Solutions

f(n)  (n2), by repeatedly applying Properties 6 and 7 (or “throwing out” all the
lower-order terms).
21) The function f(x) = n + n2 + 2n + n4 belongs in which of the following complexity
categories:

Solution: None of these; it is actually (2n), according to Property 6.

23) Establish Properties 1, 2, 6, and 7 of the Properties of Order in Section 1.4.2.

Property 1: g(n)∈O(f(n)) if and only if f(n)∈ Ω(g(n)).


Proof: “If and only if” means double implication.
We prove first that g(n)∈O(f(n)) implies f(n)∈ Ω(g(n)). According to the definition of
O, there exist c, N > 0 such that, for all n ≥ N, g(n) ≤ cf(n). This means f(n) ≥ c-1g(n),
which means f(n)∈ Ω(g(n)).
The opposite implication is proved in the same manner.

Property 2: g(n) ∈ Θ(f(n)) if and only if f(n)∈ Θ(g(n)).


Proof: We prove first that g(n)∈(f(n)) implies f(n)∈ (g(n)). According to the
definition of , there exist c, d, N > 0 such that, for all n ≥ N, cf(n) ≤ g(n) ≤ df(n) ⇨
⇨ d-1g(n) ≤ f(n) ≤ c-1g(n) ⇨ f(n)∈ (g(n)).
The opposite implication is proved in the same manner.

Property 6: Here we have to compare each category with the next in the sequence.
As an example, we prove bn ∈ o(n!):
Proof: According to the definition of “little-oh”, we have to show that, for every positive
c, there exists N such than bn ≤ cn!, for all n ≥ N. Let n0 be the first integer with the
property n0 > 2b. We rewrite the desired inequality thus:
b b n 0 n0 (n0 + 1) ... n
bn0∙bn-n0 ≤ c(1∙2∙…∙n0∙ … ∙n) ⇔   , where the left-hand side
c n0 ! b  b ... b
is a constant C, and each ratio on the right-hand-side is > 2. Therefore we simply make
N large enough so that C < 2N-n0+1, or N =
Chapter 1: Algorithms: Efficiency, Analysis, and Order Solutions

Property 7: If c>=0, d>0, g(n) ∈ O(f(n)), and h(n) ∈ (f(n)), then c*g(n)+d*h(n) ∈
Θ(f(n))
Proof: According to the definitions of O and , there are positive constants c1, N1, c2,
d2, N2 such that
g(n) ≤ c1∙f(n) for all n ≥ N1
c2∙f(n) ≤ h(n) ≤ d2∙f(n) for all n ≥ N2
We multiply the first inequality by c and the second one by d, and denote by N the
maximum of N1 and N2:
cg(n) ≤ cc1∙f(n) (*)
dc2∙f(n) ≤ dh(n) ≤ dd2∙f(n) (**) for all n ≥ N
Adding (*) and the right inequality in (**), we obtain
cg(n) + dh(n) ≤ (cc1 + dd2)∙f(n) for all n ≥ N, which is the right-hand-side
inequality in the definition of .
From the left-hand-side inequality of (**), we have dc2∙f(n) ≤ dh(n), and we add cg(n)
to obtain
dc2∙f(n) ≤ cg(n) + dh(n) for all n ≥ N, which is the left-hand-side
inequality in the definition of .
Chapter 1: Algorithms: Efficiency, Analysis, and Order Solutions

25) Suppose you have a computer that requires 1 minute to solve problem instances of
size n =1,000. Suppose you buy a new computer that runs 1,000 times faster than the
old one. What instance sizes can be run in 1 minute, assuming the following time
complexities T(n) for our algorithm?
a) T(n) = n Answer: 106
b) T(n) = n3 Answer: 104
c) T(n) = 10 n Answer: 1003

27) Show the correctness of the following statements.

For all these results, we use Theorem 1.3 and Theorem 1.4 ( L’Hôpital’s rule):

a) lim[(lg n)/n] = lim[(lg n)’/n’] = lim[1/n]/ln2 = 0, so lg n is in o(n), which, according


to Theorem 1.2, is included in O(n), so lg n is in O(n).

b) lim[n/(nlg n)] = lim[1/(nlg’n + lg n)] = 0, and continue as in (a).

c) lim[nlg n/n2] = lim[(lg n)/n] same as (a).

d) With algebraic manipulations we have 5lg n = 5log5(n)/log5(2) = (5log5(n))1/log5(2) ≈ n0.43 , ao


lim[5lg n/2n] = lim[n0.43/2n] = 0.43∙ln2∙lim[1/n0.572n] = 0, so as above we have that 5lgn 
O(2n), which, by Property 1, implies 2n  (5lgn).
e) lim[lg3n/n0.5] = lim[(3lg2n/(nln2)) / (0.5/n0.5)] = C1lim[lg2n/n0.5] = … = C2lim[lg
n/n0.5]= … = C3lim[1/n0.5] = 0, and from Theorem 1.3 this implies that lg3n  o(n0.5).

You might also like