



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This is solution manual provided by Muhammad Uzair at University of Engineering and Technology Lahore. It related to Data Structures and Algorithms course. Its main points are: Algorithm, Analysis, Growing, Function, Inductive, Hupothesis, Running, Time, Swap, Duplicate
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




By:
Muhammad Uzair
05-E-
U.E.T. Lahore
Pakistan
Preface
Included in this manual are answers to most of the exercises in the textbook Data Structures and Algorithm Analysis in C, second edition, published by Addison-Wesley. These answers reflect the state of the book in the first printing.
Specifically omitted are likely programming assignments and any question whose solu- tion is pointed to by a reference at the end of the chapter. Solutions vary in degree of complete- ness; generally, minor details are left to the reader. For clarity, programs are meant to be pseudo-C rather than completely perfect code.
Errors can be reported to [email protected]. Thanks to Grigori Schwarz and Brian Harvey for pointing out errors in previous incarnations of this manual.
2.1 2 /N O, 37, √MM N OO, N O, N Olog log N O, N Olog N O, N Olog ( N O^2 ), N Olog^2 N O, N O1.5, N O^2 , N O^2 log N O, N O^3 , 2 N O /^^2 , 2 N O. N Olog N O and N Olog ( N O^2 ) grow at the same rate.
2.2 (a) True.
(b) False. A counterexample is T O 1 ( N O) = 2 N O, T O 2 ( N O) = N O, and P f OO( N O) = N O. (c) False. A counterexample is T O 1 ( N O) = N O^2 , T O 2 ( N O) = N O, and P f OO( N O) = N O^2. (d) False. The same counterexample as in part (c) applies.
2.3 We claim that N Olog N O is the slower growing function. To see this, suppose otherwise. Then, N Oε /^ √MMMMMlog^ N OO^ would grow slower than log N O. Taking logs of both sides, we find that, under this assumption, ε / √MlogMMMMM N OOlog N O grows slower than log log N O. But the first expres- sion simplifies to ε √MlogMMMMM N OO. If L O = log N O, then we are claiming that ε √MM L OO grows slower than log L O, or equivalently, that ε^2 L O grows slower than log^2 L O. But we know that log^2 L O = ο ( L O), so the original assumption is false, proving the claim.
2.4 Clearly, log k O^1 N O = ο(log k O^2 N O) if k O 1 < k O 2 , so we need to worry only about positive integers. The claim is clearly true for k O = 0 and k O = 1. Suppose it is true for k O < i O. Then, by L’Hospital’s rule,
N O→∞
lim N
N O→∞
lim i N
_________^ log i O−^1 N
The second limit is zero by the inductive hypothesis, proving the claim.
2.5 Let P f OO( N O) = 1 when N O is even, and N O when N O is odd. Likewise, let g O( N O) = 1 when N O is odd, and N O when N O is even. Then the ratio P f OO( N O) / g O( N O) oscillates between 0 and ∞.
2.6 For all these programs, the following analysis will agree with a simulation:
(I) The running time is O O( N O). (II) The running time is O O( N O^2 ). (III) The running time is O O( N O^3 ). (IV) The running time is O O( N O^2 ). (V) P j O can be as large as i O^2 , which could be as large as N O^2. k O can be as large as P j O, which is N O^2. The running time is thus proportional to N O .N O^2 .N O^2 , which is O O( N O^5 ). (VI) The if O statement is executed at most N O^3 times, by previous arguments, but it is true only O O( N O^2 ) times (because it is true exactly i O times for each i O). Thus the innermost loop is only executed O O( N O^2 ) times. Each time through, it takes O O(P j O^2 ) = O O( N O^2 ) time, for a total of O O( N O^4 ). This is an example where multiplying loop sizes can occasionally give an overesti- mate.
2.7 (a) It should be clear that all algorithms generate only legal permutations. The first two algorithms have tests to guarantee no duplicates; the third algorithm works by shuffling an array that initially has no duplicates, so none can occur. It is also clear that the first two algorithms are completely random, and that each permutation is equally likely. The third algorithm, due to R. Floyd, is not as obvious; the correctness can be proved by induction.
See J. Bentley, "Programming Pearls," Communications of the ACM 30 (1987), 754-757. Note that if the second line of algorithm 3 is replaced with the statement Swap( A[i], A[ RandInt( 0, N-1 ) ] ); then not all permutations are equally likely. To see this, notice that for N O = 3, there are 27 equally likely ways of performing the three swaps, depending on the three random integers. Since there are only 6 permutations, and 6 does not evenly divide 27, each permutation cannot possibly be equally represented. (b) For the first algorithm, the time to decide if a random number to be placed in A O[ i O] has not been used earlier is O O( i O). The expected number of random numbers that need to be tried is N O / ( N O − i O). This is obtained as follows: i O of the N O numbers would be duplicates. Thus the probability of success is ( N O − i O) / N O. Thus the expected number of independent trials is N O / ( N O − i O). The time bound is thus
i O= 0
N O− 1 N O− i
_____Ni_ < i O= 0
N O− 1 N O− i
i O= 0
N O− 1 N O− i
P j O= 1
N P j
= O O( N O^2 log N O)
The second algorithm saves a factor of i O for each random number, and thus reduces the time bound to O O( N Olog N O) on average. The third algorithm is clearly linear. (c, d) The running times should agree with the preceding analysis if the machine has enough memory. If not, the third algorithm will not seem linear because of a drastic increase for large N O. (e) The worst-case running time of algorithms I and II cannot be bounded because there is always a finite probability that the program will not terminate by some given time T O. The algorithm does, however, terminate with probability 1. The worst-case running time of the third algorithm is linear - its running time does not depend on the sequence of random numbers.
2.8 Algorithm 1 would take about 5 days for N O = 10,000, 14.2 years for N O = 100,000 and 140 centuries for N O = 1,000,000. Algorithm 2 would take about 3 hours for N O = 100,000 and about 2 weeks for N O = 1,000,000. Algorithm 3 would use 1 ⁄^1 2 minutes for N O = 1,000,000. These calculations assume a machine with enough memory to hold the array. Algorithm 4 solves a problem of size 1,000,000 in 3 seconds.
2.9 (a) O O( N O^2 ).
(b) O O( N Olog N O).
2.10 (c) The algorithm is linear.
2.11 Use a variation of binary search to get an O O(log N O) solution (assuming the array is preread).
2.13 (a) Test to see if N O is an odd number (or 2) and is not divisible by 3, 5, 7, ..., √MM N OO.
(b) O O(√MM N OO), assuming that all divisions count for one unit of time. (c) B O = O O(log N O). (d) O O(2 B O /^^2 ). (e) If a 20-bit number can be tested in time T O, then a 40-bit number would require about T O^2 time. (f) B O is the better measure because it more accurately represents the size O of the input.