Data Structures and Algorithm Analysis in C++,3rd Edition by Mark Allen Weiss
Chapter 1-12
such as simulations, interactive case studies, or project-based assessments. These methods can better replicate the conditions of the modern business environment, where decision-making often
requires collaboration, creativity, and adaptability. Digital tools and platforms that offer real-time feedback and interactive
CONTENTS
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
, C H A P T E R 1
Introduction
such as simulations, interactive case studies, or project-based assessments. These methods can better replicate the conditions of the modern business environment, where decision-making often
requires collaboration, creativity, and adaptability. Digital tools and platforms that offer real-time feedback and interactive
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.
X B X B XB B
(b) Let 2 = A. Then A = (2 = 2 . Thus log A = 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 = 1 + 2 + 3 + . . .. 4S = 1 + 2 + 3 + .........Subtracting the first equation from the second
4 4
42 43 42
1 2
gives 3S = 1 + 4
+ 2
+ ........ By part (a), 3S = 4/3 so S = 4/9.
4
(c) S = 1
4 + 4
+ 9
+ . . .. 4S = 1 + 44 + 92 + 163 + .........Subtracting
the first equation from the
42 43 4 4
i + Σ 1 . Thus 3S =
∞ Σ
∞
second gives 3S = 1 + 43 + 452 7
3 + . . .. Rewriting, we get 3S = 2
4
i=0 i=0
2(4/9) + 4/3 = 20/9. Thus S = 20/27.
Σ
∞ iN
(d) Let SN = . Follow the same method as in parts (a)–(c) to obtain a formula for S in terms
i=0
of S ,S S and solve the recurrence. Solving the recurrence is very difficult.
N —1 N—2, ..., 0
Σ 1 Σ 1
LN/2—1]
1
1.9 = — ≈ ln N — ln N/2 ≈ ln 2.
N N N
, 1.10 (a) Σ(2i — 1) = 2 Σ i — Σ 1 = N(N + 1) — N = N 2.
(b) The easiest way to prove this is by induction. The case N = 1 is trivial. Otherwise,
N +1 N
Σ Σ
i3 = (N + 1)3 + i3
i=1 i=1
N 2(N + 1)2
3
= (N + 1) + 4
· 2 ¸
2 N
= (N + 1) + (N + 1)
4
· 2 ¸
= (N + 1)2 N + 4N + 4
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());}
};
such as simulations, interactive case studies, or project-based assessments. These methods can better replicate the conditions of the modern business environment, where decision-making often
requires collaboration, creativity, and adaptability. Digital tools and platforms that offer real-time feedback and interactive
, Solutions 3
string getLast( const string & name)
{
string last;
int blankPosition = name.find(" ");
last = name.substr(blankPosition+1, name.size());
return last;
}
int main()
{
vector<Employee> v(3);
v[0].setValue("George Bush", 400000.00);
v[1].setValue("Bill Gates", 2000000000.00);
v[2].setValue("Dr. Phil", 13000000.00);
cout<<findMax(v, EmployeeLastNameCompare())<<endl;
return 0;
}
such as simulations, interactive case studies, or project-based assessments. These methods can better replicate the conditions of the modern business environment, where decision-making often
requires collaboration, creativity, and adaptability. Digital tools and platforms that offer real-time feedback and interactive
C H A P T E R 2
Algorithm Analysis
√ N/2 N
2.1 2/N, 37, N , N , N log log N , N log N , N log(N2), N log2 N , N 1.5, N 2, N 2 log N , N 3, 2 , 2 .
2
N log N and N log(N ) grow at the same rate.
2.2 (a) True.
(b) False. A counterexample is T1(N) = 2N , T2(N) = N , and f(N) = N .
(c) False. A counterexample is T1(N) = N 2, T2(N) = N , and f(N) = N 2.
(d) False. The same counterexample as in part (c) applies.
2.3 c l a i m that N log N is the slower growing function. To see this, suppose otherwise. Then,
We √
‹/ N
N log would grow slower than log N . Taking logs of both sides, we find that, under this
assumption, ‹/ log N log N grows slower than log log N . But the first expression simplifies to
√
‹ log N . If L = log N , then we are claiming that ‹ L grows slower than log L, or equivalently,
that ‹2L grows slower than log2 L. But we know that log2 L = o(L), so the original assumption is
false, proving the claim.
k k
2.4 Clearly, log 1 N = o(log 2 N) if k1 < k2, so we need to worry only about positive integers. The claim
is clearly true for k = 0 and k = 1. Suppose it is true for k < i. Then, by L’Hospital’s rule,
i i
log N log —1 N
lim lim
N →∞
N N →∞
i
N
The second limit is zero by the inductive hypothesis, proving the claim.
2.5 Let f(N) = 1 when N is even, and N when N is odd. Likewise, let g(N) = 1 when N is odd, and
N when N is even. Then the ratio f (N)/g(N) oscillates between 0 and inf .