




























































































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!





























































































Contained herein are the solutions to all exercises from the textbook A Practical Introduction to Data Structures and Algorithm Analysis , 2nd edition. For most of the problems requiring an algorithm I have given actual code. In a few cases I have presented pseudocode. Please be aware that the code presented in this manual has not actually been compiled and tested. While I believe the algo- rithms to be essentially correct, there may be errors in syntax as well as semantics. Most importantly, these solutions provide a guide to the instructor as to the intended answer, rather than usable programs.
iii
2 Chap. 1 Data Structures and Algorithms
// Concatenate two strings String strcat(String s1, String s2);
// Return the length of a string int length(String s1);
// Extract a substring, starting at ‘start’, // and of length ‘length’ String extract(String s1, int start, int length);
// Get the first character char first(String s1);
// Compare two strings: the normal C++ strcmp func- tion. Some // convention should be indicated for how to inter- pret the // return value. In C++, this is - 1 for s1<s2; 0 for s1=s2; // and 1 for s1>s2. int strcmp(String s1, String s2)
// Copy a string int strcpy(String source, String destination)
1.4 The answer to this question is provided by the ADT for lists given in Chap- ter 4. 1.5 One’s compliment stores the binary representation of positive numbers, and stores the binary representation of a negative number with the bits inverted. Two’s compliment is the same, except that a negative number has its bits inverted and then one is added (for reasons of efficiency in hardware imple- mentation). This representation is the physical implementation of an ADT defined by the normal arithmetic operations, declarations, and other support given by the programming language for integers. 1.6 An ADT for two-dimensional arrays might look as follows.
Matrix add(Matrix M1, Matrix M2); Matrix multiply(Matrix M1, Matrix M2); Matrix transpose(Matrix M1); void setvalue(Matrix M1, int row, int col, int val); int getvalue(Matrix M1, int row, int col); List getrow(Matrix M1, int row);
One implementation for the sparse matrix is described in Section 12.3 Another im- plementation is a hash table whose search key is a concatenation of the matrix coor- dinates. 1.7 Every problem certainly does not have an algorithm. As discussed in Chapter 15, there are a number of reasons why this might be the case. Some problems don’t have a sufficiently clear definition. Some problems, such as the halting problem, are non-computable. For some problems, such as one typically studied by artificial intelligence researchers, we simply don’t know a solution. 1.8 We must assume that by “algorithm” we mean something composed of steps are of a nature that they can be performed by a computer. If so, than any algorithm can be expressed in C ++. In particular, if an algorithm can be expressed in any other computer programming language, then it can be expressed in C ++, since all (sufficiently general) computer programming languages compute the same set of functions. 1.9 The primitive operations are (1) adding new words to the dictionary and (2) search- ing the dictionary for a given word. Typically, dictionary access involves some sort of pre-processing of the word to arrive at the “root” of the word. A twenty page document (single spaced) is likely to contain about 20,000 words. A user may be willing to wait a few seconds between individual “hits” of mis-spelled words, or perhaps up to a minute for the whole document to be processed. This means that a check for an individual word can take about 10-20 ms. Users will typically insert individual words into the dictionary interactively, so this process can take a couple of seconds. Thus, search must be much more efficient than insertion. 1.10 The user should be able to find a city based on a variety of attributes (name, location, perhaps characteristics such as population size). The user should also be able to in- sert and delete cities. These are the fundamental operations of any database system: search, insertion and deletion. A reasonable database has a time constraint that will satisfy the patience of a typical user. For an insert, delete, or exact match query, a few seconds is satisfactory. If the database is meant to support range queries and mass deletions, the entire operation may be allowed to take longer, perhaps on the order of a minute. However, the time spent to process individual cities within the range must be appropriately reduced. In practice, the data representation will need to be such that it accommodates efficient processing to meet these time constraints. In particular, it may be necessary to sup- port operations that process range queries efficiently by processing all cities in the range as a batch, rather than as a series of operations on individual cities. 1.11 Students at this level are likely already familiar with binary search. Thus, they should typically respond with sequential search and binary search. Binary search should be described as better since it typically needs to make fewer comparisons (and thus is likely to be much faster). 1.12 The answer to this question is discussed in Chapter 8. Typical measures of cost will be number of comparisons and number of swaps. Tests should include running timings on sorted, reverse sorted, and random lists of various sizes.
2.1 (a) Not reflexive if the set has any members. One could argue it is sym- metric, antisymmetric, and transitive, since no element violate any of the rules. (b) Not reflexive (for any female). Not symmetric (consider a brother and sister). Not antisymmetric (consider two brothers). Transitive (for any 3 brothers). (c) Not reflexive. Not symmetric, and is antisymmetric. Not transitive (only goes one level). (d) Not reflexive (for nearly all numbers). Symmetric since a + b = b + a, so not antisymmetric. Transitive, but vacuously so (there can be no distinct a, b, and c where aRb and bRc). (e) Reflexive. Symmetric, so not antisymmetric. Transitive (but sort of vacuous). (f) Reflexive – check all the cases. Since it is only true when x = y, it is technically symmetric and antisymmetric, but rather vacuous. Like- wise, it is technically transitive, but vacuous. 2.2 In general, prove that something is an equivalence relation by proving that it is reflexive, symmetric, and transitive. (a) This is an equivalence that effectively splits the integers into odd and even sets. It is reflexive (x + x is even for any integer x), symmetric (since x + y = y + x) and transitive (since you are always adding two odd or even numbers for any satisfactory a, b, and c). (b) This is not an equivalence. To begin with, it is not reflexive for any integer. (c) This is an equivalence that divides the non-zero rational numbers into positive and negative. It is reflexive since x x >˙ 0. It is symmetric since x y˙ = y x˙. It is transitive since any two members of the given class satisfy the relationship.
6 Chap. 2 Mathematical Preliminaries
(d) This is not an equivalance relation since it is not symmetric. For exam- ple, a = 1 and b = 2. (e) This is an eqivalance relation that divides the rationals based on their fractional values. It is reflexive since for all a, a−a = 0. It is symmetric since if a−b = x then b−a = −x. It is transitive since any two rationals with the same fractional value will yeild an integer. (f) This is not an equivalance relation since it is not transitive. For exam- ple, 4 − 2 = 2 and 2 − 0 = 2, but 4 − 0 = 4. 2.3 A relation is a partial ordering if it is antisymmetric and transitive. (a) Not a partial ordering because it is not transitive. (b) Is a partial ordering bacause it is antisymmetric (if a is an ancestor of b, then b cannot be an ancestor of a) and transitive (since the ancestor of an ancestor is an ancestor). (c) Is a partial ordering bacause it is antisymmetric (if a is older than b, then b cannot be older than a) and transitive (since if a is older than b and b is older than c, a is older than c). (d) Not a partial ordering, since it is not antisymmetric for any pair of sis- ters. (e) Not a partial ordering because it is not antisymmetric. (f) This is a partial ordering. It is antisymmetric (no violations exist) and transitive (no violations exist). 2.4 A total ordering can be viewed as a permuation of the elements. Since there are n! permuations of n elements, there must be n! total orderings. 2.5 This proposed ADT is inspired by the list ADT of Chapter 4. void clear(); void insert(int); void remove(int); void sizeof(); bool isEmpty(); bool isInSet(int); 2.6 This proposed ADT is inspired by the list ADT of Chapter 4. Note that while it is similiar to the operations proposed for Question 2.5, the behaviour is somewhat different. void clear(); void insert(int); void remove(int); void sizeof();
8 Chap. 2 Mathematical Preliminaries
2.13 void allpermute(int array[], int n, int currpos) { if (currpos == (n-1)} { printout(array); return; } for (int i=currpos; i<n; i++) { swap(array, currpos, i); allpermute(array, n, currpos+1); swap(array, currpos, i); // Put back for next pass } } 2.14 In the following, function bitposition(n, i) returns the value (0 or
2 is rational. By defini- tion, there exist integers p and q such that √ 2 = p q
where p and q have no common factors (that is, the fraction p/q is in lowest terms). By squaring both sides and doing some simple algebraic manipula- tion, we get
2 = p^2 q^2 2 q^2 = p^2
Since p^2 must be even, p must be even. Thus,
2 q^2 = 4( p 2
q^2 = 2(
p 2
This implies that q^2 is also even. Thus, p and q are both even, which contra- dicts the requirement that p and q have no common factors. Thus,
2 must be irrational. ✷ 2.17 The leftmost summation sums the integers from 1 to n. The second summa- tion merely reverses this order, summing the numbers from n − 1 + 1 = n down to n − n + 1 = 1. The third summation has a variable substitution of i− 1 for i, with a corresponding substitution in the summation bounds. Thus, it is also the summation of n − 0 = n to n − (n − 1) = 1. 2.18 Proof : (a) Base case. For n = 1, 12 = [2(1)^3 + 3(1)^2 + 1]/6 = 1. Thus, the formula is correct for the base case. (b) Induction Hypothesis. n∑− 1
i=
i^2 =
2(n − 1)^3 + 3(n − 1)^2 + (n − 1) 6
(c) Induction Step. ∑^ n
i=
i^2 =
n∑− 1
i=
i^2 + n^2
2(n − 1)^3 + 3(n − 1)^2 + (n − 1) 6
= 2 n^3 − 6 n^2 + 6n − 2 + 3n^2 − 6 n + 3 + n − 1 6
= 2 n^3 + 3n^2 + n 6
Thus, the theorem is proved by mathematical induction. ✷ 2.19 Proof : (a) Base case. For n = 1, 1 /2 = 1 − 1 /2 = 1/ 2. Thus, the formula is correct for the base case. (b) Induction Hypothesis. n∑− 1
i=
2 i^
2 n−^1
2.22 Theorem 2.
∑n i=1(2i) =^ n (^2) + n_._
(a) Proof : We know from Example 2.3 that the sum of the first n odd numbers is n^2. The ith even number is simply one greater than the ith odd number. Since we are adding n such numbers, the sum must be n greater, or n^2 + n. ✷ (b) Proof : Base case : n = 1 yields 2 = 1^2 + 1, which is true. Induction Hypothesis : n∑− 1
i=
2 i = (n − 1)^2 + (n − 1).
Induction Step : The sum of the first n even numbers is simply the sum of the first n − 1 even numbers plus the nth even number.
∑^ n
i=
2 i = (
n∑− 1
i=
2 i) + 2n
= (n − 1)^2 + (n − 1) + 2n = (n^2 − 2 n + 1) + (n − 1) + 2n = n^2 − n + 2n = n^2 + n.
Thus, by mathematical induction,
∑n i=1 2 i^ =^ n (^2) + n. ✷
2.23 Proof : Base case. For n = 1, Fib(1) = 1 < 53. For n = 2, Fib(2) = 1 < ( 53 )^2. Thus, the formula is correct for the base case. Induction Hypothesis. For all positive integers i < n,
Fib(i) < (
)i.
Induction Step. Fib(n) = Fib(n − 1) + Fib(n − 2) and, by the Induction Hypothesis, Fib(n − 1) < ( 53 )n−^1 and Fib(n − 2) < ( 53 )n−^2. So,
Fib(n) < (
)n−^1 + (
)n−^2
<
)n−^2 + (
)n−^2
12 Chap. 2 Mathematical Preliminaries
)n−^2
< (
)n−^2
=
n .
Thus, the theorem is proved by mathematical induction. ✷ 2.24 Proof : (a) Base case. For n = 1, 13 = 1
(^2) (1+1) 2 4 = 1. Thus, the formula is correct for the base case. (b) Induction Hypothesis. n∑− 1
i=
i^3 = (n − 1)^2 n^2 4
(c) Induction Step.
∑^ n
i=
i^3 = (n − 1)^2 n^2 4
n^4 − 2 n^3 + n^2 4
= n^4 + 2n^3 + n^2 4 = n^2 (n^2 + 2n + 2) 4 = n^2 (n + 1)^2 4 Thus, the theorem is proved by mathematical induction. ✷ 2.25 (a) Proof : By contradiction. Assume that the theorem is false. Then, each pigeonhole contains at most 1 pigeon. Since there are n holes, there is room for only n pigeons. This contradicts the fact that a total of n + 1 pigeons are within the n holes. Thus, the theorem must be correct. ✷ (b) Proof : i. Base case. For one pigeon hole and two pigeons, there must be two pigeons in the hole. ii. Induction Hypothesis. For n pigeons in n − 1 holes, some hole must contain at least two pigeons.
14 Chap. 2 Mathematical Preliminaries
Induction hypothesis : T (n − 1) = 2n−^1 − 1. Induction step :
T (n) = 2 T (n − 1) + 1 = 2(2n−^1 − 1) + 1 = 2 n^ − 1.
Thus, as proved by mathematical induction, this formula is indeed the correct closed form solution for the recurrence. 2.29 (a) The probability is 0.5 for each choice. (b) The average number of “1” bits is n/ 2 , since each position has 0. probability of being “1.” (c) The leftmost “1” will be the leftmost bit (call it position 0) with prob- ability 0.5; in position 1 with probability 0.25, and so on. The number of positions we must examine is 1 in the case where the leftmost “1” is in position 0; 2 when it is in position 1, and so on. Thus, the expected cost is the value of the summation ∑^ n
i=
i 2 i^
The closed form for this summation is 2 − n 2 +2n , or just less than two. Thus, we expect to visit on average just less than two positions. (Stu- dents at this point will probably not be able to solve this summation, and it is not given in the book.) 2.30 There are at least two ways to approach this problem. One is to estimate the volume directly. The second is to generate volume as a function of weight. This is especially easy if using the metric system, assuming that the human body is roughly the density of water. So a 50 Kilo person has a volume slightly less than 50 liters; a 160 pound person has a volume slightly less than 20 gallons. 2.31 (a) Image representations vary considerably, so the answer will vary as a result. One example answer is: Consider VGA standard size, full-color (24 bit) images, which is 3 × 640 × 480 , or just less than 1 Mbyte per image. The full database requires some 30-35 CDs. (b) Since we needed 30-35 CDs before, compressing by a factor of 10 is not sufficient to get the database onto one CD. [Note that if the student picked a smaller format, such as estimating the size of a “typical” gif image, the result might well fit onto a single CD.]
2.32 (I saw this problem in John Bentley’s Programming Pearls.) Approach 1: The model is Depth X Width X Flow where Depth and Width are in miles and Flow is in miles/day. The Mississippi river at its mouth is about 1/4 mile wide and 100 feet (1/50 mile) deep, with a flow of around 15 miles/hour = 360 miles/day. Thus, the flow is about 2 cubic miles/day. Approach 2: What goes out must equal what goes in. The model is Area X Rainfall where Area is in square miles and Rainfall is in (linear) miles/day. The Mississipi watershed is about 1000 X 1000 miles, and the average rainfal is about 40 inches/year ≈ .1 inches/day ≈ .000002 miles/day (2 X 10 −^6 ). Thus, the flow is about 2 cubic miles/day.
2.33 Note that the student should NOT be providing answers that look like they were done using a calculator. This is supposed to be an exercise in estima- tion! The amount of the mortgage is irrelevant, since this is a question about rates. However, to give some numbers to help you visualize the problem, pick a $100,000 mortgage. The up-front charge would be $1,000, and the savings would be 1/4% each payment over the life of the mortgage. The monthly charge will be on the remaining principle, being the highest at first and grad- ually reducing over time. But, that has little effect for the first few years. At the grossest approximation, you paid 1% to start and will save 1/4% each year, requiring 4 years. To be more precise, 8% of $100,000 is $8,000, while 7 3/4% is $7,750 (for the first year), with a little less interest paid (and there- fore saved) in following years. This will require a payback period of slightly over 4 years to save $1000. If the money had been invested, then in 5 years the investment would be worth about $1300 (at 5would be close to 5 1/ years.
2.34 Disk drive seek time is somewhere around 10 milliseconds or a little less in 2000. RAM memory requires around 50 nanoseconds – much less than a microsecond. Given that there are about 30 million seconds in a year, a machine capable of executing at 100 MIPS would execute about 3 billion billion ( 3 ∗ 1018 ) instructions in a year.
2.35 Typical books have around 500 pages/inch of thickness, so one million pages requires 2000 inches or 150-200 feet of bookshelf. This would be in excess of 50 typical shelves, or 10-20 bookshelves. It is within the realm of possibility that an individual home has this many books, but it is rather unusual.
2.36 A typical page has around 400 words (best way to derive this is to estimate the number of words/line and lines/page), and the book has around 500 pages, so the total is around 200,000 words.