Solution Manual For
Data Structures and Algorithm Analysis in C++3rd Edition by Mark Allen Weiss
Chapter 1-12
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
exams include:Multiple Choice Questions (MCQs): These are frequently used to assess students’ understanding of business terminology, theories, and principles.Case Studies: A
staple of business exams, case studies present students with real-world business scenarios and ask them to apply their knowledge to solve complex problems. Case studies evaluate students' ability to
think critically and make strategic decisions.Essay/Short Answer Questions: These types of questions test the student’s ability to explain and analyze business concepts in a detailed and coherent
manner.1.3. Skills Tested in Business ExamsCritical Thinking and Problem-Solving: Business exams often include case studies that challenge students to apply theoretical knowledge to real-life
situations. These tests assess decision-making skills, as well as the ability to evaluate various business alternatives.Quantitative Analysis: For subjects like finance or economics, business exams
often require students to perform calculations and interpret data. The ability to analyze financial statements, project revenues, and manage budgets is essential.Communication and Writing Skills:
Business exams may require students to present ideas clearly and concisely. Students must convey complex concepts in a way that demonstrates their unde
, 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.
exams include:Multiple Choice Questions (MCQs): These are frequently used to assess students’ understanding of business terminology, theories, and principles.Case Studies: A
staple of business exams, case studies present students with real-world business scenarios and ask them to apply their knowledge to solve complex problems. Case studies evaluate
students' ability to think critically and make strategic decisions.Essay/Short Answer Questions: These types of questions test the student’s ability to explain and analyze business
concepts in a detailed and coherent manner.1.3. Skills Tested in Business ExamsCritical Thinking and Problem-Solving: Business exams often include case studies that challenge
students to apply theoretical knowledge to real-life situations. These tests assess decision-making skills, as well as the ability to evaluate various business alternatives.Quantitative
Analysis: For subjects like finance or economics, business exams often require students to perform calculations and interpret data. The ability to analyze financial statements,
project revenues, and manage budgets is essential.Communication and Writing Skills: Business exams may require students to present ideas clearly and concisely. Students must
convey complex concepts in a way that demonstrates their unde
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
+ + ........ By part (a), 3S = 4/3 so S = 4/9.
42
(c) S = 1
+ 4
+ 9
+ . . . . 4S = 1 + 4 + 2 + 163 + .........Subtracting
4 9
the first equation from the
4 42 43 4 4
Σ
∞
i + Σ 1 . Thus 3S =
∞
second gives 3S = 1 + 43 + 452 7 . . .. Rewriting, we get 3S = 2
43 +
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 Σ 1
LN/2—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
= (N + 1)3 + 4
· ¸
2 N2
= (N + 1) + (N + 1)
4
· ¸
= (N + 1)2 N 2 + 4N + 4
4
2
(N 1) (N 2)2
=
22
· ¸2
(N + 1)(N + 2)
2
N +1
#2
= i
exams include:Multiple Choice Questions (MCQs): These are frequently used to assess i=1
students’ understanding of business terminology, theories, and principles.Case Studies:
A staple of business exams, case studies present students with real-world business
scenarios and ask them to apply their knowledge to solve complex problems. Case
studies evaluate students' ability to think critically and make strategic
decisions.Essay/Short Answer Questions: These types of questions test the student’s
ability to explain and analyze business concepts in a detailed and coherent manner.1.3.
Skills Tested in Business ExamsCritical Thinking and Problem-Solving: Business
exams often include case studies that challenge students to apply theoretical knowledge
to real-life situations. These tests assess decision-making skills, as well as the ability to
evaluate various business alternatives.Quantitative Analysis: For subjects like finance or
economics, business exams often require students to perform calculations and interpret
data. The ability to analyze financial statements, project revenues, and manage budgets
is essential.Communication and Writing Skills: Business exams may require students to
present ideas clearly and concisely. Students must convey complex concepts in a way
that demonstrates their unde
1.15 class EmployeeLastNameCompare
{
public:
bool operator () (const Employee & lhs, const Employee & rhs) const
{ return getLast(lhs.getName())< getLast(rhs.getName());}
};
, 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;
}
exams include:Multiple Choice Questions (MCQs): These are frequently used to assess students’ understanding of business terminology, theories, and principles.Case Studies: A staple of business
exams, case studies present students with real-world business scenarios and ask them to apply their knowledge to solve complex problems. Case studies evaluate students' ability to think critically
and make strategic decisions.Essay/Short Answer Questions: These types of questions test the student’s ability to explain and analyze business concepts in a detailed and coherent manner.1.3. Skills
Tested in Business ExamsCritical Thinking and Problem-Solving: Business exams often include case studies that challenge students to apply theoretical knowledge to real-life situations. These tests
assess decision-making skills, as well as the ability to evaluate various business alternatives.Quantitative Analysis: For subjects like finance or economics, business exams often require students to
perform calculations and interpret data. The ability to analyze financial statements, project revenues, and manage budgets is essential.Communication and Writing Skills: Business exams may require
students to present ideas clearly and concisely. Students must convey complex concepts in a way that demonstrates their unde
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 .
N log N and N log(N2) 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
Data Structures and Algorithm Analysis in C++3rd Edition by Mark Allen Weiss
Chapter 1-12
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
exams include:Multiple Choice Questions (MCQs): These are frequently used to assess students’ understanding of business terminology, theories, and principles.Case Studies: A
staple of business exams, case studies present students with real-world business scenarios and ask them to apply their knowledge to solve complex problems. Case studies evaluate students' ability to
think critically and make strategic decisions.Essay/Short Answer Questions: These types of questions test the student’s ability to explain and analyze business concepts in a detailed and coherent
manner.1.3. Skills Tested in Business ExamsCritical Thinking and Problem-Solving: Business exams often include case studies that challenge students to apply theoretical knowledge to real-life
situations. These tests assess decision-making skills, as well as the ability to evaluate various business alternatives.Quantitative Analysis: For subjects like finance or economics, business exams
often require students to perform calculations and interpret data. The ability to analyze financial statements, project revenues, and manage budgets is essential.Communication and Writing Skills:
Business exams may require students to present ideas clearly and concisely. Students must convey complex concepts in a way that demonstrates their unde
, 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.
exams include:Multiple Choice Questions (MCQs): These are frequently used to assess students’ understanding of business terminology, theories, and principles.Case Studies: A
staple of business exams, case studies present students with real-world business scenarios and ask them to apply their knowledge to solve complex problems. Case studies evaluate
students' ability to think critically and make strategic decisions.Essay/Short Answer Questions: These types of questions test the student’s ability to explain and analyze business
concepts in a detailed and coherent manner.1.3. Skills Tested in Business ExamsCritical Thinking and Problem-Solving: Business exams often include case studies that challenge
students to apply theoretical knowledge to real-life situations. These tests assess decision-making skills, as well as the ability to evaluate various business alternatives.Quantitative
Analysis: For subjects like finance or economics, business exams often require students to perform calculations and interpret data. The ability to analyze financial statements,
project revenues, and manage budgets is essential.Communication and Writing Skills: Business exams may require students to present ideas clearly and concisely. Students must
convey complex concepts in a way that demonstrates their unde
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
+ + ........ By part (a), 3S = 4/3 so S = 4/9.
42
(c) S = 1
+ 4
+ 9
+ . . . . 4S = 1 + 4 + 2 + 163 + .........Subtracting
4 9
the first equation from the
4 42 43 4 4
Σ
∞
i + Σ 1 . Thus 3S =
∞
second gives 3S = 1 + 43 + 452 7 . . .. Rewriting, we get 3S = 2
43 +
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 Σ 1
LN/2—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
= (N + 1)3 + 4
· ¸
2 N2
= (N + 1) + (N + 1)
4
· ¸
= (N + 1)2 N 2 + 4N + 4
4
2
(N 1) (N 2)2
=
22
· ¸2
(N + 1)(N + 2)
2
N +1
#2
= i
exams include:Multiple Choice Questions (MCQs): These are frequently used to assess i=1
students’ understanding of business terminology, theories, and principles.Case Studies:
A staple of business exams, case studies present students with real-world business
scenarios and ask them to apply their knowledge to solve complex problems. Case
studies evaluate students' ability to think critically and make strategic
decisions.Essay/Short Answer Questions: These types of questions test the student’s
ability to explain and analyze business concepts in a detailed and coherent manner.1.3.
Skills Tested in Business ExamsCritical Thinking and Problem-Solving: Business
exams often include case studies that challenge students to apply theoretical knowledge
to real-life situations. These tests assess decision-making skills, as well as the ability to
evaluate various business alternatives.Quantitative Analysis: For subjects like finance or
economics, business exams often require students to perform calculations and interpret
data. The ability to analyze financial statements, project revenues, and manage budgets
is essential.Communication and Writing Skills: Business exams may require students to
present ideas clearly and concisely. Students must convey complex concepts in a way
that demonstrates their unde
1.15 class EmployeeLastNameCompare
{
public:
bool operator () (const Employee & lhs, const Employee & rhs) const
{ return getLast(lhs.getName())< getLast(rhs.getName());}
};
, 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;
}
exams include:Multiple Choice Questions (MCQs): These are frequently used to assess students’ understanding of business terminology, theories, and principles.Case Studies: A staple of business
exams, case studies present students with real-world business scenarios and ask them to apply their knowledge to solve complex problems. Case studies evaluate students' ability to think critically
and make strategic decisions.Essay/Short Answer Questions: These types of questions test the student’s ability to explain and analyze business concepts in a detailed and coherent manner.1.3. Skills
Tested in Business ExamsCritical Thinking and Problem-Solving: Business exams often include case studies that challenge students to apply theoretical knowledge to real-life situations. These tests
assess decision-making skills, as well as the ability to evaluate various business alternatives.Quantitative Analysis: For subjects like finance or economics, business exams often require students to
perform calculations and interpret data. The ability to analyze financial statements, project revenues, and manage budgets is essential.Communication and Writing Skills: Business exams may require
students to present ideas clearly and concisely. Students must convey complex concepts in a way that demonstrates their unde
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 .
N log N and N log(N2) 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