Mawcsol Solution Manual
C%2B%2B%20Solution%20manual
User Manual: Pdf
Open the PDF directly: View PDF
.
Page Count: 97
Publisher Greg Tobin
Senior Acquisitions Editor Michael Hirsch
Editorial Assistant Lindsey Triebel
Marketing Manager Michelle Brown
Marketing Assistant Dana Lopreato
Digital Asset Manager Marianne Groth
Composition Windfall Software, using ZzTEX
Proofreaders Melanie Aswell, Debbie Sidman
Access the latest information about Addison-Wesley titles from our World Wide Web site: http://www.awbc.com/computing
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and Addison-Wesley was aware of a trademark
claim, the designations have been printed in initial caps or all caps.
Copyright © 2006 by Pearson Education, Inc.
For information on obtaining permission for use of material in this work, please submit a written request
to Pearson Education, Inc., Rights and Contract Department, 75 Arlington Street, Suite 300, Boston, MA
02116 or fax your request to (617) 848-7047.
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or
transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or any other
media embodiments now known or hereafter to become known, without the prior written permission of
the publisher. Printed in the United States of America.
1 2 3 4 5 6 7 8 9 10—PDF—08 07 06 05
CONTENTS
Preface
v
Chapter 1 Introduction
1
Chapter 2 Algorithm Analysis
5
Chapter 3 Lists, Stacks, and Queues
9
Chapter 4 Trees
29
Chapter 5 Hashing
41
Chapter 6 Priority Queues (Heaps)
45
Chapter 7 Sorting
53
Chapter 8 The Disjoint Set
59
Chapter 9 Graph Algorithms
63
Chapter 10 Algorithm Design Techniques
77
Chapter 11 Amortized Analysis
87
Chapter 12 Advanced Data Structures and Implementation
91
iii
PREFACE
Included in this manual are answers to many of the exercises in the textbook Data Structures and
Algorithm Analysis in C++, third edition, published by Addison-Wesley. These answers reflect the
state of the book in the first printing of the third edition.
Specifically omitted are general programming questions and any question whose solution is
pointed to by a reference at the end of the chapter. Solutions vary in degree of completeness; generally,
minor details are left to the reader. For clarity, the few code segments that are present are meant to
be pseudo-C++ rather than completely perfect code.
Errors can be reported to weiss@fiu.edu.
v
C H A P T E R
1
Introduction
1.4
The general way to do this is to write a procedure with heading
void processFile( String fileName );
which opens fileName, does whatever processing is needed, and then closes it. If a line of the form
#include SomeFile
is detected, then the call
processFile( SomeFile );
is made recursively. Self-referential includes can be detected by keeping a list of files for which a call
to processFile has not yet terminated, and checking this list before making a new call to processFile.
1.5
int ones( int n )
{
if( n < 2 )
return n;
return n % 2 + ones( n / 2 );
}
1.7
(a) The proof is by induction. The theorem is clearly true for 0 < X ≤ 1, since it is true for X = 1,
and for X < 1, log X is negative. It is also easy to see that the theorem holds for 1 < X ≤ 2, since it
is true for X = 2, and for X < 2, log X is at most 1. Suppose the theorem is true for p < X ≤ 2p
(where p is a positive integer), and consider any 2p < Y ≤ 4p (p ≥ 1). Then log Y = 1 + log(Y /2) <
1 + Y /2 < Y /2 + Y /2 ≤ Y , where the first inequality follows by the inductive hypothesis.
B
(b) Let 2X = A. Then AB = (2X ) = 2XB . Thus log AB = XB. Since X = log A, the theorem is
proved.
1.8
(a) The sum is 4/3 and follows directly from the formula.
(b) S = 41 + 422 + 433 + . . .. 4S = 1 + 42 + 432 + . . .. Subtracting the first equation from the second
gives 3S = 1 + 41 + 422 + . . .. By part (a), 3S = 4/3 so S = 4/9.
+ . . .. Subtracting the first equation from the
(c) S = 41 + 442 + 493 + . . .. 4S = 1 + 44 + 492 + 16
43
∞
∞
1
i
. Thus 3S =
+
second gives 3S = 1 + 43 + 452 + 473 + . . .. Rewriting, we get 3S = 2
i
4i
4
i=0
i=0
2(4/9) + 4/3 = 20/9. Thus S = 20/27.
∞ N
i
. Follow the same method as in parts (a)–(c) to obtain a formula for SN in terms
(d) Let SN =
4i
i=0
of SN −1, SN −2, . . . , S0 and solve the recurrence. Solving the recurrence is very difficult.
1.9
N
i=N/2
1
i
=
N
i=1
1
i
−
N/2−1
i=1
1
i
≈ ln N − ln N/2 ≈ ln 2.
1
2
Chapter 1
1.10
24 = 16 ≡ 1 (mod 5). (24) ≡ 125 (mod 5). Thus 2100 ≡ 1 (mod 5).
1.11
(a) Proof is by induction. The statement is clearly true for N = 1 and N = 2. Assume true for
k+1
k
N = 1, 2, . . . , k. Then
Fi =
Fi + Fk+1. By the induction hypothesis, the value of the sum
Introduction
25
i=1
i=1
on the right is Fk+2 − 2 + Fk+1 = Fk+3 − 2, where the latter equality follows from the definition of
the Fibonacci numbers. This proves the claim for N = k + 1, and hence for all N .
(b) As in the text, the proof is by induction. Observe that φ + 1 = φ 2. This implies that φ −1 + φ −2 =
1. For N = 1 and N = 2, the statement is true. Assume the claim is true for N = 1, 2, . . . , k.
Fk+1 = Fk + Fk−1
by the definition and we can use the inductive hypothesis on the right-hand side, obtaining
Fk+1 < φ k + φ k−1
< φ −1φ k+1 + φ −2φ k+1
Fk+1 < (φ −1 + φ −2)φ k+1 < φ k+1
and proving the theorem.
(c) See any of the advanced math references at the end of the chapter. The derivation involves the
use of generating functions.
1.12
(a)
N
(2i − 1) = 2
i=1
N
i=1
i−
N
1 = N (N + 1) − N = N 2.
i=1
(b) The easiest way to prove this is by induction. The case N = 1 is trivial. Otherwise,
N
+1
i 3 = (N + 1)3 +
N
i3
i=1
N 2(N
i=1
+ 1)2
= (N + 1)3 +
4
2
N
= (N + 1)2
+ (N + 1)
4
2
2 N + 4N + 4
= (N + 1)
4
(N + 1)2(N + 2)2
22
2
(N + 1)(N + 2)
=
2
N +1 2
=
i
=
i=1
1.15
class EmployeeLastNameCompare
{
public:
bool operator () (const Employee & lhs, const Employee & rhs) const
{ return
};
getLast(lhs.getName())< getLast(rhs.getName());}
Solutions
string getLast( const string & name)
{
string last;
int blankPosition = name.find(" ");
last = name.substr(blankPosition+1, name.size());
return last;
}
int main()
{
vector
v(3);
v[0].setValue("George Bush", 400000.00);
v[1].setValue("Bill Gates", 2000000000.00);
v[2].setValue("Dr. Phil", 13000000.00);
cout< 1, the number of multiplies used is
log N
log N + b(N ) − 1
2.25
(a) A.
(b) B.
(c) The information given is not sufficient to determine an answer. We have only worst-case bounds.
(d) Yes.
2.26
(a) Recursion is unnecessary if there are two or fewer elements.
(b) One way to do this is to note that if the first N − 1 elements have a majority, then the last element
cannot change this. Otherwise, the last element could be a majority. Thus if N is odd, ignore the last
element. Run the algorithm as before. If no majority element emerges, then return the N th element
as a candidate.
(c) The running time is O(N ), and satisfies T (N ) = T (N/2) + O(N ).
(d) One copy of the original needs to be saved. After this, the B array, and indeed the recursion can
be avoided by placing each Bi in the A array. The difference is that the original recursive strategy
implies that O(log N ) arrays are used; this guarantees only two copies.
2.27
Start from the top-right corner. With a comparison, either a match is found, we go left, or we go
down. Therefore, the number of comparisons is linear.
2.28
(a,c) Find the two largest numbers in the array.
(b,d) Similar solutions; (b) is described here. The maximum difference is at least zero (i ≡ j ), so
that can be the initial value of the answer to beat. At any point in the algorithm, we have the current
value j , and the current low point i. If a[j ] − a[i] is larger than the current best, update the best
7
8
Chapter 2
Algorithm Analysis
difference. If a[j ] is less than a[i], reset the current low point to i. Start with i at index 0, j at index
0. j just scans the array, so the running time is O(N ).
2.29
Otherwise, we could perform operations in parallel by cleverly encoding several integers into one.
For instance, if A = 001, B = 101, C = 111, D = 100, we could add A and B at the same time as C
and D by adding 00A00C + 00B00D. We could extend this to add N pairs of numbers at once in
unit cost.
2.31
No. If low = 1, high = 2, then mid = 1, and the recursive call does not make progress.
2.33
No. As in Exercise 2.31, no progress is made.
2.34
See my textbook Algorithms, Data Structures and Problem Solving with C++ for an explanation.
C H A P T E R
3
Lists, Stacks,
and Queues
3.1
template
void printLots(list