Problems on algorithms 1st Edition Ian Parberry instant download
Problems on algorithms 1st Edition Ian Parberry instant download
pdf download
https://ebookgate.com/product/problems-on-algorithms-1st-edition-
ian-parberry/
https://ebookgate.com/product/algorithms-illuminated-
part-4-algorithms-for-np-hard-problems-1st-edition-tim-roughgarden/
ebookgate.com
https://ebookgate.com/product/mathematical-finance-core-theory-
problems-and-statistical-algorithms-nikolai-dokuchaev/
ebookgate.com
https://ebookgate.com/product/algorithms-on-strings-1st-edition-
maxime-crochemore/
ebookgate.com
https://ebookgate.com/product/optimization-algorithms-on-matrix-
manifolds-p-a-absil/
ebookgate.com
Hume on Causation Problems of Philosophy 1st Edition Helen
Beebee
https://ebookgate.com/product/hume-on-causation-problems-of-
philosophy-1st-edition-helen-beebee/
ebookgate.com
https://ebookgate.com/product/geometric-problems-on-maxima-and-
minima-1st-edition-titu-andreescu/
ebookgate.com
https://ebookgate.com/product/algorithms-1st-edition-umesh-vazirani/
ebookgate.com
https://ebookgate.com/product/essential-algorithms-1st-edition-rod-
stephens/
ebookgate.com
https://ebookgate.com/product/approximate-iterative-algorithms-1st-
edition-almudevar/
ebookgate.com
Problems on Algorithms
Second Edition
Consisting of
http://hercule.csci.unt.edu/ian/books/free/license.html.
Faculty, if you wish to use this work in your classroom, you are requested to:
• encourage your students to make individual donations, or
• make a lump-sum donation on behalf of your class.
If you have a credit card, you may place your donation online at:
https://www.nationalmssociety.org/donate/donate.asp.
If you restrict your donation to the National MS Society's targeted research campaign, 100% of
your money will be directed to fund the latest research to find a cure for MS. For the story of Ian
Parberry's experience with Multiple Sclerosis, see http:// multiplesclerosissucks.com.
Problems on Algorithms
by
Ian Parberry
([email protected])
Dept. of Computer Science
University of North Texas
Denton, TX 76203
August, 1994
Contents
Preface ix
1 Introduction 1
1.1 How to Use This Book 1
1.2 Overview of Chapters 1
1.3 Pseudocode 3
1.4 Useful Texts 4
2 Mathematical Induction 8
2.1 Summations 8
2.2 Inequalities 10
2.3 Floors and Ceilings 10
2.4 Divisibility 11
2.5 Postage Stamps 11
2.6 Chessboard Problems 12
2.7 Fibonacci Numbers 14
2.8 Binomial Coefficients 14
2.9 What is Wrong? 15
2.10 Graphs 16
2.11 Trees 17
2.12 Geometry 18
2.13 Miscellaneous 19
2.14 Hints 20
2.15 Solutions 21
2.16 Comments 23
iii
iv Contents
4 Recurrence Relations 37
4.1 Simple Recurrences 37
4.2 More Difficult Recurrences 38
4.3 General Formulae 39
4.4 Recurrences with Full History 39
4.5 Recurrences with Floors and Ceilings 40
4.6 Hints 41
4.7 Solutions 41
4.8 Comments 44
5 Correctness Proofs 46
5.1 Iterative Algorithms 46
5.1.1 Straight-Line Programs 47
5.1.2 Arithmetic 47
5.1.3 Arrays 49
5.1.4 Fibonacci Numbers 51
5.2 Recursive Algorithms 51
5.2.1 Arithmetic 52
5.2.2 Arrays 53
5.2.3 Fibonacci Numbers 54
5.3 Combined Iteration and Recursion 54
5.4 Hints 55
5.5 Solutions 56
5.6 Comments 58
6 Algorithm Analysis 59
6.1 Iterative Algorithms 59
Contents v
7 Divide-and-Conquer 67
7.1 Maximum and Minimum 67
7.2 Integer Multiplication 68
7.3 Strassen’s Algorithm 73
7.4 Binary Search 74
7.5 Quicksort 75
7.6 Towers of Hanoi 77
7.7 Depth-First Search 78
7.8 Applications 79
7.9 Hints 81
7.10 Solutions 83
7.11 Comments 85
8 Dynamic Programming 87
8.1 Iterated Matrix Product 87
8.2 The Knapsack Problem 89
8.3 Optimal Binary Search Trees 90
8.4 Floyd’s Algorithm 91
8.5 Applications 92
8.6 Finding the Solutions 97
8.7 Hints 99
8.8 Solutions 99
8.9 Comments 100
vi Contents
12 N P-completeness 147
12.1 General 147
12.2 Cook Reductions 148
12.3 What is Wrong? 150
12.4 Circuits 152
12.5 One-in-Three 3SAT 153
12.6 Factorization 154
Contents vii
13 Miscellaneous 156
13.1 Sorting and Order Statistics 156
13.2 Lower Bounds 157
13.3 Graph Algorithms 158
13.4 Maximum Flow 160
13.5 Matrix Reductions 161
13.6 General 162
13.7 Hints 164
13.8 Solutions 166
13.9 Comments 166
Bibliography 168
Index 174
viii Contents
Preface
The ability to devise effective and efficient algorithms in new situations is a skill
that separates the master programmer from the merely adequate coder. The best
way to develop that skill is to solve problems. To be effective problem solvers,
master-programmers-in-training must do more than memorize a collection of stan-
dard techniques and applications — they must in addition be able to internalize and
integrate what they have learned and apply it in new circumstances. This book is a
collection of problems on the design, analysis, and verification of algorithms for use
by practicing programmers who wish to hone and expand their skills, as a supple-
mentary text for students enrolled in an undergraduate or beginning graduate class
on algorithms, and as a self-study text for graduate students who are preparing for
the qualifying (often called “breadth” or “comprehensive”) examination on algo-
rithms for a Ph.D. program in Computer Science or Computer Engineering. It is
intended to augment the problem sets found in any standard algorithms textbook.
Recognizing that a supplementary text must be cost-effective if it is to be useful,
I have made two important and perhaps controversial decisions in order to keep its
length within reasonable bounds. The first is to cover only what I consider to be
the most important areas of algorithm design and analysis. Although most instruc-
tors throw in a “fun” advanced topic such as amortized analysis, computational
geometry, approximation algorithms, number-theoretic algorithms, randomized al-
gorithms, or parallel algorithms, I have chosen not to cover these areas. The second
decision is not to search for the origin of the problems that I have used. A lengthy
discussion of the provenance of each problem would help make this book more schol-
arly, but would not make it more attractive for its intended audience — students
and practicing programmers.
To make this book suitable for self-instruction, I have provided at the end of
each chapter a small collection of hints, solutions, and comments. The solutions are
necessarily few for reasons of brevity, and also to avoid hindering instructors in their
selection of homework problems. I have included various preambles that summarize
the background knowledge needed to solve the problems so that students who are
familiar with the notation and style of their textbook and instructor can become
more familiar with mine.
ix
x Preface
The organization of this book is a little unusual and requires a few words of
explanation. After a chapter of introduction, it begins with five chapters on back-
ground material that most algorithms instructors would like their students to have
mastered before setting foot in an algorithms class. This will be the case at some
universities, but even then most students would profit from brushing up on this
material by attempting a few of the problems. The introductory chapters include
mathematical induction, big-O notation, recurrence relations, correctness proofs,
and basic algorithm analysis methods. The correctness proof chapter goes beyond
what is normally taught, but I believe that students profit from it once they over-
come their initial aversion to mathematical formalism.
The next four chapters are organized by algorithm design technique: divide-and-
conquer, dynamic programming, greedy algorithms, and exhaustive search. This
organization is somewhat nonstandard, but I believe that the pedagogical benefits
outweigh the drawbacks (the most significant of which seems to be the scattering of
problems on graph algorithms over several chapters, mainly in Sections 2.10, 2.11,
7.7, 8.4, 9.2, 9.3, 9.4, 13.3, and 13.4). Standard textbooks usually devote little time
to exhaustive search because it usually requires exponential time, which is a pity
when it contains many rich and interesting opportunities to practice the application
of algorithm design, analysis, and verification methods. The manuscript is rounded
out with chapters on advanced data structures and N P-completeness. The final
chapter contains miscellaneous problems that do not necessarily fit into the earlier
chapters, and those for which part of the problem is to determine the algorithmic
technique or techniques to be used.
The algorithms in this book are expressed in a Pascal-like pseudocode. Two
problems (Problems 326 and 327) require the reader to examine some code written
in Pascal, but the details of the programming language are not a major factor in
the solutions. In any case, I believe that students of Computer Science should be
literate in many different programming languages.
For those who are interested in technical details, this manuscript was produced
from camera-ready copy provided by the author using LATEX version 2.09 (which
used TEX C Version 3.14t3), using the Prentice Hall macros phstyle.sty writ-
ten by J. Kenneth Shultis, and the twoside, epsf, and makeidx style files. The
Bibliography was created by BibTEX. The index was produced using the program
makeindex. The figures were prepared in encapsulated Postscript form using idraw,
and the graphs using xgraph. The dvi file produced by LATEX was translated into
Postscript using dvips.
A list of errata for this book is available by anonymous ftp from ftp.unt.edu
(IP address 129.120.1.1), in the directory ian/poa. I will be grateful to receive
feedback, suggestions, errata, and in particular any new and interesting problems
(including those from areas not presently covered), which can be sent by electronic
mail to [email protected].
Chapter 1
Introduction
1
2 Chap. 1. Introduction
Symbol Meaning
easy
medium
difficult
hint
solution
comment
that you review this material and attempt some of the problems from Chapters 2–4
before attending the first class. Some kind instructors may spend a few lectures
“reminding” you of this material, but not all of them can afford the time to do so.
In any case, you will be ahead of the game if you devote some time to look over
the material again. If you ignored the material on mathematical induction in your
discrete mathematics class, thinking that it is useless to a programmer, then think
again. If you can master induction, you have mastered recursion — they are two
sides of the same coin.
You may also meet the material from Chapters 5 and 6 in other classes. Chap-
ter 5 contains problems on correctness proofs. Not all instructors will emphasize
correctness proofs in their algorithms class, but mastering them will teach you im-
portant skills in formalization, expression, and design. The approach to correctness
proofs taken in Chapter 5 is closer to that taken by working researchers than the
formal-logic approach expounded by others such as Dijkstra [21]. Chapter 6 con-
tains some problems involving the analysis of some naive algorithms. You should
have met this material in earlier classes, but you probably don’t have the depth of
knowledge and facility that you can get from working the problems in this chapter.
Chapters 7–10 consist of problems on various algorithm design techniques: re-
spectively, divide-and-conquer, dynamic programming, greedy algorithms, and ex-
haustive search. The problems address some of the standard applications of these
techniques, and ask you to demonstrate your knowledge by applying the techniques
to new problems. Divide-and-conquer has already been foreshadowed in earlier
chapters, but the problems in Chapter 7 will test your knowledge further. The
treatment of exhaustive search in Chapter 10 goes beyond what is normally covered
in algorithms courses. The excuse for skimming over exhaustive search is usually a
natural distate for exponential time algorithms. However, extensive problems are
provided here because exhaustive search contains many rich and interesting oppor-
tunities to practice the application of algorithm design, analysis, and verification
methods.
Chapter 11 contains problems on advanced data structures, and Chapter 12
Sec. 1.3. Pseudocode 3
1.3 PSEUDOCODE
The algorithms in this text are described in a Pascal-like pseudocode. Here is a
quick overview of the conventions used:
Data Types: Most variables are either integers or one- or two-dimensional arrays
of integers. The notation P [i..j] is shorthand for an array P , whose elements
are P [i], P [i + 1], . . . , P [j]. Occasionally, variables will be other mathematical
objects such as sets or lists.
Selection: The selection command uses Pascal’s if-then-else syntax, which has
the form
if condition
then S1
else S2
while condition do
S
repeat
S
until condition
4 Chap. 1. Introduction
and is equivalent to
S
while not condition do
S
i := s i := s
while i ≤ f do while i ≥ f do
S S
i := i + 1 i := i − 1
Aho, Hopcroft, and Ullman [1] This was the standard graduate text on algo-
rithms and data structures for many years. It is written quite tersely, with
some sentences requiring a paragraph of explanation for some students. If you
want the plain facts with no singing or dancing, this is the book for you. It is
a little dated in presentation and material, but not particularly so.
Aho, Hopcroft, and Ullman [2] This is the undergraduate version of the pre-
vious book, with more emphasis on programming. It is full of minor errors.
A large number of the Pascal program fragments don’t work, even when you
correctly implement their C-like return statement.
Aho and Ullman [3] This textbook is redefining the undergraduate computer sci-
ence curriculum at many leading institutions. It is a good place to go to brush
up on your discrete mathematics, data structures, and problem solving skills.
Bentley [9, 10] This delightful pair of books are a collection of Jon Bentley’s Pro-
gramming Pearls column from Communications of the ACM. They should be
recommended reading for all undergraduate computer science majors. Bent-
ley explores the problems and pitfalls of algorithm design and analysis, and
pays careful attention to implementation and experimentation. The subjects
chosen are too idiosyncratic and anecdotal to be a textbook, but nonetheless
a useful pair of books.
Brassard and Bratley [14] This is another good algorithms text with a strong
emphasis on design techniques. The title comes from the French word algo-
rithmique, which is what they (perhaps more aptly) call Computer Science.
Cormen, Leiserson, and Rivest [19] This is an excellent text for those who can
handle it. In the spirit of Aho, Hopcroft, and Ullman [1], it does not mess
around. A massive tome, it contains enough material for both a graduate and
undergraduate course in algorithms. It is the definitive text for those who
want to get right down to business, but it is not for the faint of heart.
Even [26] This is the canonical book on graph algorithms covering most of the
graph algorithm material taught in the typical algorithms course, plus more.
It is a little dated, but still very useful.
Gibbons [29] A more modern and carefully presented text on graph algorithms,
superior in many ways to Even [26].
Greene and Knuth [33] A highly mathematical text for an advanced course on
algorithm analysis at Stanford. Recommended for the serious graduate stu-
dent.
Harel [34] This is another good text along the lines of Brassard and Bratley. It
contains excellent high-level descriptions of subjects that tend to confuse the
beginning student if they are presented in all their detailed glory. It is noted
for its biblical quotes.
Horowitz and Sahni [37] This is a reasonable text for many of the topics found
in the typical algorithms class, but it is weak on analysis and but its approach
to backtracking is somewhat idiosyncratic and hard to follow.
Knuth [45, 46, 47] The Knuth three-volume series is the standard reference text
for researchers and students. The material in it has aged well, except for
Knuth’s penchant for an obscure fictitious assembly-level code instead of the
more popular and easy to understand pseudocode.
Kozen [50] A text for graduate students containing 40 lectures on algorithms. The
terse presentation is not for everybody. It contains enough material for a single
graduate course, as opposed to the normal text that allows the instructor
to pick and choose the material to be covered. The choice of material is
strong but not to everybody’s taste. The homework problems (all of which
have solutions) and miscellaneous exercises (some of which have solutions) are
particularly useful.
Lewis and Denenberg [52] A strong data structures text that also covers some
of the material in the typical algorithms course.
Manber [56] Some students find the approach taken in this book, which is algo-
rithm design by successive refinement of intuitive but flawed version of the
algorithm, very helpful. Others, who do not think this way, hate it. It is
certainly more realistic in its approach to algorithm design than more formal
Sec. 1.4. Useful Texts 7
texts, but perhaps it encourages the wrong habits. Some important topics are
not covered in very great detail.
Moret and Shapiro [58] This is planned as the first volume of a two-volume set.
This volume covers tractable problems, and the second volume (unsighted
as yet) will cover intractable problems. It has a definite combinatorial opti-
mization flavor. Dynamic programming is a notable omission from the first
volume.
Papadimitriou and Steiglitz [59] A reasonable text for an algorithms course,
but one which is very heavy with the jargon and mind-set of the combinatorial
optimization and numerical analysis community, which makes it a difficult
book to just dip into at random for the parts of it that are useful.
Purdom and Brown [64] A mathematically rigorous text that focuses on anal-
ysis almost to the exclusion of design principles.
Rawlins [67] An entertaining text that nonetheless is technically rigorous and
detailed.
Sedgewick [72] This book studies a large array of standard algorithms, but it is
largely descriptive in nature and does not spend much time on verification,
analysis, or design techniques. It is excellent for the elementary undergraduate
algorithms curriculum, but not sufficiently rigorous for upper-division courses.
Smith [74] This is another strong algorithms text with an awesome collection of
problems.
Solow [75] This is a good text for the student who has trouble with mathematical
formalism. The chapter on induction may be particularly useful for those who
have trouble getting started.
Weiss [82] A strong data structures text that also covers quite a bit of the material
in the typical algorithms course.
Wilf [83] A book that does a good job on analysis, but does not cover algorithm
design techniques. The selection of topics is limited to chapters on mathe-
matical preliminaries, recursion, network flow, number-theoretic algorithms,
and N P-completeness.
Chapter 2
Mathematical Induction
At first sight, this chapter may seem out of place because it doesn’t seem to be
about algorithms at all. Although the problems in this chapter are mainly mathe-
matical in nature and have very little to do with algorithms, they are good practice
in a technique that is fundamental to algorithm design, verification, and analysis.
Unfortunately, many students come to my algorithms class poorly versed in the use
of induction. Either it was badly taught or they discarded it as useless information
(or both). If you are one of these students, you should start by attempting at least
some of the following problems. In addition to mastering the technique of mathe-
matical induction, you should pay particular attention to the results in Sections 2.1,
2.2, 2.3, 2.7, 2.8, 2.10, and 2.11. Some of them are quite useful in algorithm design
and analysis, as we shall see later.
2.1 SUMMATIONS
1. Prove by induction on n ≥ 0 that ni=1 i = n(n + 1)/2.
n
2. Prove by induction on n ≥ 0 that i=1 i2 = n(n + 1)(2n + 1)/6.
n
3. Prove by induction on n ≥ 0 that i=1 i3 = n2 (n + 1)2 /4.
n
5. Prove by induction on n ≥ 0 that i=1 i(i + 1) = n(n + 1)(n + 2)/3.
8
Sec. 2.1. Summations 9
n
7. Prove by induction on n ≥ 0 that i · i! = (n + 1)! − 1.
i=1
n
8. Prove by induction on n ≥ 1 that i=1 1/2i = 1 − 1/2n .
n
11. Prove by induction on n ≥ 0 that i=0 2i = 2n+1 − 1.
n
13. Prove by induction on n ≥ 0 that i=1 i2i = (n − 1)2n+1 + 2.
2.2 INEQUALITIES
18. Prove by induction on n ≥ 1 that if x > −1, then (1 + x)n ≥ 1 + nx.
n
1 1
2
<2− .
i=1
i n
n n+m−1
= .
m m
Sec. 2.4. Divisibility 11
2.4 DIVISIBILITY
27. Prove by induction on n ≥ 0 that n3 + 2n is divisible by 3.
28. Prove by induction on n ≥ 0 that n5 − n is divisible by 5.
29. Prove by induction on n ≥ 0 that 5n+1 + 2 · 3n + 1 is divisible by 8.
30. Prove by induction on n ≥ 0 that 8n+2 + 92n+1 is divisible by 73.
31. Prove by induction that for all n ≥ 0, 11n+2 + 122n+1 is divisible by 133.
32. Define S ⊆ IN × IN as follows. (0, 0) ∈ S. If (m, n) ∈ S, then
(m + 2, n + 3) ∈ S. Prove by induction on n ≥ 0 that for all (m, n) ∈ S, m + n
is divisible by 5.
35. Prove by induction that the sum of the cubes of three successive natural
numbers is divisible by 9.
36. Let Sn = {1, 2, . . . , 2n} be the set of integers from 1 to 2n. Let
T ⊂ Sn be any subset containing exactly n + 1 elements of Sn . Prove by
induction on n that there exists x, y ∈ T , x = y, such that x divides evenly
into y with no remainder.
38. Show that any integer postage greater than 34 cents can be formed by
using only 5-cent and 9-cent stamps.
39. Show that any integer postage greater than 5 cents can be formed by
using only 2-cent and 7-cent stamps.
40. Show that any integer postage greater than 59 cents can be formed by
using only 7-cent and 11-cent stamps.
41. What is the smallest value of k such that any integer postage greater
than k cents can be formed by using only 4-cent and 9-cent stamps? Prove
your answer correct.
12 Chap. 2. Mathematical Induction
42. What is the smallest value of k such that any integer postage greater
than k cents can be formed by using only 6-cent and 11-cent stamps? Prove
your answer correct.
43. Show that for all n ≥ 1, any positive integer amount of postage that
is at least n(n − 1) cents can be formed by using only n-cent and (n + 1)-cent
stamps.
44. Show that for all m, n ≥ 1 such that gcd(m, n) = 1, there exists k ∈ IN
such that any positive integer amount of postage that is at least k cents can
be formed by using only m-cent and n-cent stamps.
48. Recall that a knight can make one of eight legal moves de-
picted in Figure 2.1. Figure 2.2 shows a closed knight’s tour on an 8 × 8
chessboard, that is, a circular tour of knight’s moves that visits every square
of the chessboard exactly once before returning to the first square. Prove by
induction that a closed knight’s tour exists for any 2k × 2k chessboard for all
k ≥ 3.
2 3
1 4
8 5
7 6
65. What is wrong with the following proof? We claim that 6n = 0 for all
n ∈ IN. The proof is by induction on n ≥ 0. Clearly, if n = 0, then 6n = 0.
Now, suppose that n > 0. Let n = a + b. By the induction hypothesis, 6a = 0
and 6b = 0. Therefore,
6n = 6(a + b) = 6a + 6b = 0 + 0 = 0.
16 Chap. 2. Mathematical Induction
66. What is wrong with the following reasoning? We show that for every
n ≥ 3, Fn is even. The base of the induction is trivial, since F3 = 2. Now
suppose that n ≥ 4 and that Fm is even for all m < n. Then Fn = Fn−1 +Fn−2 ,
and by the induction hypothesis Fn−1 and Fn−2 are even. Thus, Fn is the
sum of two even numbers, and must therefore be even.
2.10 GRAPHS
A graph is an ordered pair G = (V, E), where V is a finite set and E ⊆ V × V . The
elements of V are called nodes or vertices, and the elements of E are called edges.
We will follow the normal conventions of using n for the number of nodes and e for
the number of edges. Where convenient we will take V = {v2 , v2 , . . . , vn }.
67. Prove by induction that a graph with n vertices can have at most
n(n − 1)/2 edges.
72. Prove by induction that every hypercube has a Hamiltonian cycle, that
is, a cycle that visits every vertex exactly once.
73. Prove by induction that the vertices of a hypercube can be colored using
two colors so that no pair of adjacent vertices have the same color.
Sec. 2.11. Trees 17
Dimension 3 Dimension 4
2.11 TREES
A tree is a special kind of graph defined as follows1 . A single vertex v is a tree
with root v. Suppose Ti = (Vi , Ei ) are disjoint trees with roots ri respectively, for
1 ≤ i ≤ k. Suppose r ∈ V1 , V2 , . . . , Vk . Then, T = (V, E) is a tree, where
V = V1 ∪ V2 ∪ · · · ∪ Vk ∪ {r},
E = E1 ∪ E2 ∪ · · · ∪ Ek ∪ {(r, r1 ), (r, r2 ), . . . , (r, rk )}.
1 Technically, this is actually called a rooted tree, but the distinction is not of prime importance
here.
18 Chap. 2. Mathematical Induction
The root of a tree is said to be at level 1. For all i ≥ 1, a child of a level i node
is said to be at level i + 1. The number of levels in a tree is the largest level number
of any node in the tree. A binary tree is a tree in which all nodes have at most two
children. A complete binary tree is a binary tree in which all leaves are at the same
level.
76. Prove by induction that a tree with n vertices has exactly n − 1 edges.
77. Prove by induction that a complete binary tree with n levels has 2n − 1
vertices.
78. Prove by induction that if there exists an n-node tree in which all the
nonleaf nodes have k children, then n mod k = 1.
79. Prove by induction that for every n such that n mod k = 1, there exists
an n-node tree in which all of the nonleaf nodes have k children.
80. Prove by induction that between any pair of vertices in a tree there is a
unique path.
2.12 GEOMETRY
84. Prove that any set of regions defined by n lines in the plane can be
colored with two colors so that no two regions that share an edge have the
same color.
85. Prove by induction that n circles divide the plane into n2 − n + 2
regions if every pair of circles intersect in exactly two points, and no three
circles intersect in a common point. Does this hold for closed shapes other
than circles?
86. A polygon is convex if every pair of points in the polygon can be
joined by a straight line that lies in the polygon. Prove by induction on n ≥ 3
that the sum of the angles of an n-vertex convex polygon is 180(n−2) degrees.
Sec. 2.13. Miscellaneous 19
89. Prove that n straight lines in the plane, all passing through a single
point, divide the plane into 2n regions.
90. Prove that n planes in space, all passing through a single point, no
three of which meet in a straight line, divide space into n(n − 2) + 2 regions.
2.13 MISCELLANEOUS
91. Suppose Mi is an ri−1 × ri matrix, for 1 ≤ i ≤ n. Prove by induction on
n ≥ 1 that the matrix product M1 · M2 · · · Mn is an r0 × rn matrix.
A Gray code is a list of the 2n n-bit strings in which each string differs from the
previous one in exactly one bit. Consider the following algorithm for listing the
20 Chap. 2. Mathematical Induction
n-bit strings. If n = 1, the list is 0, 1. If n > 1, first take the list of (n − 1)-bit
strings, and place a 0 in front of each string. Then, take a second copy of the list of
(n − 1)-bit strings, place a 1 in front of each string, reverse the order of the strings,
and place it after the first list. So, for example, for n = 2 the list is 00, 01, 11, 10,
and for n = 3 the list is 000, 001, 011, 010, 110, 111, 101, 100.
96. Show that every n-bit string appears exactly once in the list generated
by the algorithm.
97. Show that the list has the Gray code property, that is, that each string
differs from the previous one in exactly one bit.
2.14 HINTS
37. You will need to use strong induction. And your base case may not be what
you expect at first,
48. Try combining four n/2 × n/2 tours into one. Your induction hypothesis must
be stronger than what you really need to prove — the tours you build must
have some extra structure to allow them to be joined at the corners.
49. Look carefully at the example given before the problem. It illustrates the
technique that you will need. Also, consider the hint to Problem 48.
69. First, prove that any graph in which all vertices have even degree must have a
cycle C (try constructing one). Delete C, leaving a smaller graph in which all
vertices have even degree. Apply the induction hypothesis (careful: there is
something I am not telling you). Put together C and the results of applying
the induction hypothesis to give the Eulerian cycle.
86. The hardest part of this is the base case, proving that the sum of the three
angles of a triangle is 180 degrees. It can be proved as follows. First, prove
Sec. 2.15. Solutions 21
92. You’ll need to make two cases in your inductive step, according to whether n
is a power of 2 (or something like that) or not.
2.15 SOLUTIONS
n
1. We are required to prove that for all n ≥ 0, i=1 i = n(n + 1)/2. The claim
is certainly true for n = 0,
in which case both sides of the equation are zero.
Suppose that n ≥ 0 and ni=1 i = n(n + 1)/2. We are required to prove that
n+1
i=1 i = (n + 1)(n + 2)/2. Now,
n+1
n
i = i + (n + 1)
i=1 i=1
= n(n + 1)/2 + (n + 1) (by the induction hypothesis)
= (n + 1)(n + 2)/2,
as required.
n
8. We are required to prove that for all n ≥ 1, i=1 1/2i = 1 − 1/2n . The claim
is true for n = 1, since in this case
both sides of the equation are equal to
1/2. Now suppose that n ≥ 1, and ni=1 1/2i = 1 − 1/2n . It remains to prove
that n+1 i
i=1 1/2 = 1 − 1/2
n+1
.
n+1
1 1 1 1
1/2i = + + + · · · + n+1
i=1
2 4 8 2
1 1 1 1 1 1
= + + + + ··· + n
2 2 2 4 8 2
n
1 1 1
= +
2 2 i=1 2i
1 1 1
− + · (1 − n ) (by the induction hypothesis)
2 2 2
= 1 − 1/2n+1 ,
as required.
n
11. We are required to prove that for all n ≥ 0, i=0 2i = 2n+1 − 1. The
0 i
i=0 2 = 1 = 2 − 1.
1
claim is certainly true for
nn =i 0, in which case
n+1
Suppose that n ≥ 0 and i=0 2 = 2 − 1. We are required to prove that
22 Chap. 2. Mathematical Induction
n+1
i=0 2i = 2n+2 − 1. Now,
n+1
n
2i = 2i + 2n+1
i=0 i=0
= (2n+1 − 1) + 2n+1 (by the induction hypothesis)
= 2n+2 − 1,
as required.
37. We are required to show that any amount of postage that is a positive integer
number of cents n > 7 can be formed by using only 3-cent and 5-cent stamps.
The claim is certainly true for n = 8 (one 3-cent and one 5-cent stamp), n = 9
(three 3-cent stamps), and n = 10 (two 5-cent stamps). Now suppose that
n ≥ 11 and all values up to n − 1 cents can be made with 3-cent and 5-cent
stamps. Since n ≥ 11, n − 3 ≥ 8, and hence by hypothesis, n − 3 cents can
be made with 3-cent and 5-cent stamps. Simply add a 3-cent stamp to this
to make n cents. Notice that we can prove something stronger with just a
little more work. The required postage can always be made using at most
two 5-cent stamps. Can you prove this?
n
52. We are required to prove that for all n ≥ 0, i=0 Fi = Fn+2 − 1. The claim
is certainly true for n = 0, since the left-hand side of the equation is F0 = 0,
and the right-hand side is F2 − 1 = 1 − 1 = 0. Now suppose that n ≥ 1, and
n−1
that i=0 Fi = Fn+1 − 1. Then,
n
n−1
Fi = Fi + Fn
i=0 i=0
= (Fn+1 − 1) + Fn (by the induction hypothesis)
= Fn+2 − 1,
as required.
Sec. 2.16. Comments 23
84. We are required to prove that any set of regions defined by n lines in the
plane can be colored with only two colors so that no two regions that share
an edge have the same color. The hypothesis is true for n = 1 (color one side
light, the other side dark). Now suppose that the hypothesis is true for n
lines. Suppose we are given n + 1 lines in the plane. Remove one of the lines
L, and color the remaining regions with two colors (which can be done, by
the induction hypothesis). Replace L. Reverse all of the colors on one side
of the line. Consider two regions that have a line in common. If that line is
not L, then by the induction hypothesis, the two regions have different colors
(either the same as before or reversed). If that line is L, then the two regions
formed a single region before L was replaced. Since we reversed colors on one
side of L only, they now have different colors.
2.16 COMMENTS
1. This identity was apparently discovered by Gauss at age 9 in 1786. There is
an apocryphal story attached to this discovery. Gauss’ teacher set the class
to add the numbers 1 + 2 + · · · + 100. One account I have heard said that the
teacher was lazy, and wanted to occupy the students for a long time so that
he could read a book. Another said that it was punishment for the class being
unruly and disruptive. Gauss came to the teacher within a few minutes with
an answer of 5050. The teacher was dismayed, and started to add. Some time
later, after some fretting and fuming, he came up with the wrong answer. He
refused to believe that he was wrong until Gauss explained:
24 Chap. 2. Mathematical Induction
1 + 2 + ··· + 50
+ 100 + 99 + · · · + 51
101 + 101 + · · · + 101 = 50 × 101 = 5050.
Can you prove this result without using induction? The case for even n can
easily be inferred from Gauss’ example. The case for odd n is similar.
3. I could go on asking for more identities like this. I can never remember the
exact form of the solution. Fortunately, there is not much need to. In general,
if you are asked to solve
n
ik
i=1
for some k ≥ 1, just remember that the solution is a polynomial in n with
largest exponent k + 1 (see Problem 21). Simply hypothesize that the sum is
ak+1 nk+1 + ak nk + · · · + a1 n + a0
for some choice of ai ∈ IR, 0 ≤ i ≤ k + 1, and try to prove it by induction.
The right values for the constants will drop right out of the proof. It gets a
bit tricky as k becomes larger, though. If you want to practice this technique,
start with k = 2. If you want a challenge to your ability to do arithmetic, try
k = 4.
4. Yes, I realize that you can solve this without induction by applying some
elementary algebra and the identities of Problems 1 and 2. What I want you
to do is practice your induction by proving this identity from first principles.
10. This can also be solved by applying the identity of Problem 9 twice.
11. This is a special case of the identity in Problem 9. There is an easy noninduc-
tive proof of this fact that appeals more to computer
n science students than
math students. The binary representation of i=0 2i is a string of n + 1 ones,
that is,
· · · 1 .
11
n+1
Sec. 2.16. Comments 25
2 n-1 2 n-1
n-1
2n
Figure
n 2.7. Using the method of areas to show that
n−1
i n i
i=1 i2 = (2n − 1)2 − i=1 2 .
· · · 0,
1 00
n+1
n n
that is, 2n+1 . Therefore, i=0 2i + 1 = 2n+1 or, equivalently, i=0 2i =
2n+1 − 1.
13. There is an elegant noninductive proof of this using the method of areas. This
technique works well for sums of products: Simply make a rectangle with area
equal to each term of the sum, fit them together to “nearly” make a rectangle,
and analyze the missing area.
n
n−1
i2i = (2n − 1)2n − 2i (see Figure 2.7)
i=1 i=1
= (2n − 1)2n − (2 − 2) n
(by Problem 1)
= (n − 1)2n+1 + 2.
15. This can also be solved using the method of areas sketched in the comment
to Problem 13.
16. This can also be solved using algebra and the identity from Problem 15.
19. And, therefore, 3n = O(n!). But we’re getting ahead of ourselves here. See
Chapter 3.
20. And, therefore, 2n = Ω(n2 ). But we’re getting ahead of ourselves here. See
Chapter 3.
48. There are actually closed knight’s tours on n × n chessboards for all even
n ≥ 6 (see Problem 369). Note that Problem 47 implies that there can be no
such tour for odd n. The formal study of the knight’s tour problem is said
26 Chap. 2. Mathematical Induction
to have begun with Euler [25] in 1759, who considered the standard 8 × 8
chessboard. Rouse Ball and Coxeter [8] give an interesting bibliography of
the history of the problem from this point.
51. This problem was suggested to the author by Nainan Kovoor in 1990.
ones. But since you have written exactly as many ones as zeros, and you have
written n2n bits in all, there must be n2n−1 ones.
64. Of course it is obvious that the proof is wrong, since all horses are not of the
same color. What I want you to do is point out where in the proof is there a
false statement made?
67. This can be proved using Problem 1, but what I’m looking for is a straight-
forward inductive proof.
69. An Eulerian cycle is not possible if the graph has at least one vertex of odd
degree. (Can you prove this?) Therefore, we conclude that a graph has a
Eulerian cycle iff it is connected and all vertices have even degree. The Swiss
mathematician Leonhard Euler first proved this in 1736 [24].
81. This problem together with Problem 82 show that “a connected graph with
no cycles” is an alternate definition of a tree. You’ll see this in the litera-
ture as often as you’ll see the recursive definition given at the beginning of
Section 2.11.
94. Yes, this is a little contrived. It’s easier to prove by contradiction. But it’s
good practice in using induction.
Big-O notation is useful for the analysis of algorithms since it captures the asymp-
totic growth pattern of functions and ignores the constant multiple (which is out
of our control anyway when algorithms are translated into programs). We will use
the following definitions (although alternatives exist; see Section 3.5). Suppose that
f, g : IN → IN.
• f (n) is O(g(n)) if there exists c, n0 ∈ IR+ such that for all n ≥ n0 , f (n) ≤
c · g(n).
• f (n) is Ω(g(n)) if there exists c, n0 ∈ IR+ such that for all n ≥ n0 , f (n) ≥
c · g(n).
• f (n) is Θ(g(n)) if f (n) is O(g(n)) and f (n) is Ω(g(n)).
• f (n) is o(g(n)) if limn→∞ f (n)/g(n) = 0.
• f (n) is ω(g(n)) if limn→∞ g(n)/f (n) = 0.
• f (n) ∼ g(n) if limn→∞ f (n)/g(n) = 1.
We will follow the normal convention of writing, for example, “f (n) = O(g(n))”
instead of “f (n) is O(g(n))”, even though the equality sign does not indicate true
equality. The proper definition of “O(g(n))” is as a set:
O(g(n)) = {f (n) | there exists c, n0 ∈ IR+ such that for all n ≥ n0 , f (n) ≤ c · g(n)}.
28
Sec. 3.1. Rank the Functions 29
Group these functions so that f (n) and g(n) are in the same group if and
only if f (n) = O(g(n)) and g(n) = O(f (n)), and list the groups in increasing
order.
99. Draw a line from each of the five functions in the center to the best
big-Ω value on the left, and the best big-O value on the right.
Ω(1/n) O(1/n)
Ω(1) O(1)
Ω(log log n) O(log log n)
Ω(log n) O(log n)
2 2
Ω(log√ n) O(log
√ n)
3 3
Ω( n) O( n)
Ω(n/ log n) 1/(log n) O(n/ log n)
Ω(n) 7n5 − 3n + 2 O(n)
Ω(n1.00001 ) (n2 + n)/(log2 n + log n) O(n1.00001 )
2
Ω(n2 / log2 n) 2log n O(n2 / log2 n)
Ω(n2 / log n) 3n O(n2 / log n)
Ω(n2 ) O(n2 )
Ω(n3/2 ) O(n3/2 )
Ω(2n ) O(2n )
Ω(5n ) O(5n )
Ω(nn ) O(nn )
2 2
Ω(nn ) O(nn )
For each of the following pairs of functions f (n) and g(n), either f (n) = O(g(n))
or g(n) = O(f (n)), but not both. Determine which is the case.
For each of the following pairs of functions f (n) and g(n), state whether f (n) =
O(g(n)), f (n) = Ω(g(n)), f (n) = Θ(g(n)), or none of the above.
127. f (n) = n2 + 3n + 4, g(n) = 6n + 7.
√
128. f (n) = n, g(n) = log(n + 3).
√
129. f (n) = n n, g(n) = n2 − n.
√
130. f (n) = n + n n, g(n) = 4n log(n2 + 1).
131. f (n) = (n2 + 2)/(1 + 2−n ), g(n) = n + 3.
132. f (n) = 2n − n2 , g(n) = n4 + n2 .
Sec. 3.3. Proving Big-O 31
2
156. Prove or disprove: 2(1+O(1/n)) = 2 + O(1/n).
Compare the following pairs of functions f , g. In each case, say whether f = o(g),
f = ω(g), or f = Θ(g), and prove your claim.
For each of the following pairs of functions f (n) and g(n), find c ∈ IR+ such that
f (n) ≤ c · g(n) for all n > 1.
173. Prove that if f1 (n) = O(g1 (n)) and f2 (n) = O(g2 (n)), then f1 (n) +
f2 (n) = O(max{g1 (n), g2 (n)}).
Sec. 3.5. Alternative Definitions 33
174. Prove that if f1 (n) = Ω(g1 (n)) and f2 (n) = Ω(g2 (n)), then f1 (n) +
f2 (n) = Ω(min{g1 (n), g2 (n)}).
175. Suppose that f1 (n) = Θ(g1 (n)) and f2 (n) = Θ(g2 (n)). Is it true
that f1 (n) + f2 (n) = Θ(g1 (n) + g2 (n))? Is it true that f1 (n) + f2 (n) =
Θ(max{g1 (n), g2 (n)})? Is it true that f1 (n) + f2 (n) = Θ(min{g1 (n), g2 (n)})?
Justify your answer.
176. Prove that if f1 (n) = O(g1 (n)) and f2 (n) = O(g2 (n)), then f1 (n) ·
f2 (n) = O(g1 (n) · g2 (n)).
177. Prove that if f1 (n) = Ω(g1 (n)) and f2 (n) = Ω(g2 (n)), then f1 (n)·f2 (n) =
Ω(g1 (n) · g2 (n)).
178. Prove or disprove: For all functions f (n) and g(n), either f (n) =
O(g(n)) or g(n) = O(f (n)).
179. Prove or disprove: If f (n) > 0 and g(n) > 0 for all n, then O(f (n) +
g(n)) = f (n) + O(g(n)).
180. Prove or disprove: O(f (n)α ) = O(f (n))α for all α ∈ IR+ .
181. Prove or disprove: O(x + y)2 = O(x2 ) + O(y 2 ).
√
182. Multiply log n + 6 + O(1/n) by n + O( n) and simplify your answer as
much as possible.
183. Show that big-O is transitive. That is, if f (n) = O(g(n)) and g(n) =
O(h(n)), then f (n) = O(h(n)).
184. Prove that if f (n) = O(g(n)), then f (n)k = O(g(n)k ).
185. Prove or disprove: If f (n) = O(g(n)), then 2f (n) = O(2g(n) ).
186. Prove or disprove: If f (n) = O(g(n)), then log f (n) = O(log g(n)).
187. Suppose f (n) = Θ(g(n)). Prove that h(n) = O(f (n)) iff h(n) = O(g(n)).
188. Prove or disprove: If f (n) = O(g(n)), then f (n)/h(n) = O(g(n)/h(n)).
189. Prove that if f (n) = O(g(n)), then f (n) = O1 (g(n)), or find a counterex-
ample to this claim.
34 Chap. 3. Big-O and Big-Ω
190. Prove that if f (n) = O1 (g(n)), then f (n) = O(g(n)), or find a counterex-
ample to this claim.
191. Prove that if f (n) = Ω(g(n)), then f (n) = Ω2 (g(n)), or find a counterex-
ample to this claim.
192. Prove that if f (n) = Ω2 (g(n)), then f (n) = Ω(g(n)), or find a coun-
terexample to this claim.
193. Prove that if f (n) = Ω1 (g(n)), then f (n) = Ω2 (g(n)), or find a coun-
terexample to this claim.
194. Prove that if f (n) = Ω2 (g(n)), then f (n) = Ω1 (g(n)), or find a coun-
terexample to this claim.
195. Prove or disprove: If f (n) = O(g(n)), then f (n) = Ω(g(n)). If
f (n) = O(g(n)), then f (n) = Ω2 (g(n)).
196. Define the relation ≡ by f (n) ≡ g(n) iff f (n) = Ω(g(n)) and g(n) =
Ω(f (n)). Similarly, define the relation ≡2 by f (n) ≡ g(n) iff f (n) = Ω2 (g(n))
and g(n) = Ω2 (f (n)). Show that ≡ is an equivalence relation, but ≡2 is not
an equivalence relation.
Language: English
All designs in this book may be used to make sock toys for gifts or
for sale by individuals or organizations. It is not necessary to secure
permission from the publisher.
To a Monkey....
Whereby This Tale Is Hung
This book is dedicated to a monkey, a whimsical little fellow whose
body is a sock and whose heart is hidden somewhere in cotton
stuffing. During the summer and early fall of 1955 several of our
friends and readers of Pack-O-Fun magazine wrote to us about him.
We learned that instructions on how to make him could be had from
the Nelson Knitting Company of Rockford, Illinois. We received
permission to reprint the instructions and thus our new friend, the
Red Heel Sock Monkey, made his appearance in the November 1955
issue of Pack-O-Fun.
Several months ago Michelle Graff joined our staff as an artist. Much
to our delight we learned that she was quite adept at creating and
making all kinds of stuffed toys. Within a short time the Red Heel
Sock Monkey was no longer lonely. He had several friends to play
with after we had turned off the lights and left the office.
We have used the Nelson Red Heel socks for most of the animals,
dolls, puppets and novelties in this book for many reasons. One very
good reason is that the red heel lends itself to the creation of lovable
toys. The brown body gives the feeling of warmth that a stuffed toy
must have and the white ribbing and toes provide material for faces,
feet, hands, etc.
Another good reason is that the Nelson Red Heel socks are quite
inexpensive, selling for about 30c a pair and even less in lots of one
dozen or more. These prices vary somewhat throughout the country.
That brings us to our third good reason. They are nationally sold and
can be found in many variety, dry goods and chain stores. They can
also be ordered through many mail order houses.
All you need are some socks, scrap materials for stuffing and
clothing plus a steady eye to thread a needle. The instructions have
been written as simply and made as complete as possible. Pick out
your toys, thread your needle and start. And, to make it even more
fun, vary the features, the trimmings and clothes to bring out your
own creative abilities and personal touches.
Edna N. Clapper
3
TABLE OF CONTENTS
To a Monkey 2
SOCK ANIMALS
Red Heel Monkey 5
Elephant 7
Teddy Bear and Daschund 8
Polar Bear and Piggy 9
Donkey and Pony 10
Kitten and Puppy 11
Bunny 13
Squirrel, Chipmunk and Begging Dog 14
Kangaroo and Baby Puppet Kangaroo 15
SOCK DOLLS
Scrappy 17
Doll Instructions 19
Girl Doll and Pixie 20
Soldier and Engineer 21
Dutch Girl and Scotch Lass 22
Mexican Boy and Chinese Boy 23
Eskimo Boy and Hula Girl 24
Cowboy and Indian 25
Red Riding Hood and Robin Hood 26
Santa Claus and Clown 27
CLOTHING AND SPECIAL EFFECTS
Jackets and Aprons 28
Pants and Skirts 29
Shoes and Boots 30
Hats, Caps and Hoods 31
Hair and Facial Expressions 32
Tips and Tricks 33
FINGER PUPPETS
Witch 35
Katie and Jamie 36
Pirate and Indian 37
Lion and Billy Goat 38
ARM PUPPETS
Crocodile and Variations 39
Clown and Ringmaster 40
PUPPET SKIT AND CHARACTERS
Skit—Phineas Rides Again 41-42
Susie Starfish and Phineas Fish 42
Clem Clam, Sheriff Sam Sponge, Oily Oliver Octopus and Louie
the Lobster
43
SOCK NOVELTIES
Penguin and Duck 45
Double Dolls 46
Owl Pillow and Humpty Dumpty 47
Hobby Horse and Weenie Doll 48
4
5
BASIC INSTRUCTIONS FOR MONKEY
Step 1
Turn sock inside out. Start 3″ from white heel and sew two seams
½″ apart and across end of the rib.
Step 2
Cut sock between seams to within 1½″ of white heel. This leaves
opening for stuffing.
Step 3
Turn sock right side out. Stuff with one hand and mold shape to suit
with other.
Step 4
Sew up opening from top to bottom so that legs are spread apart
and not pulled together.
Step 5
To shape head, tie loosely at neck with ribbon. For a more floppy
head tie ribbon tighter.
Step 6
For feet, bend legs into L-shape for desired foot length and sew
crease with strong white thread.
RED HEEL MONKEY
SOCK NO. 2
CAP: Cut off toe of the sock, leaving ½″ of brown to roll for a trim.
TAIL: Cut a 1″ strip the entire length of the front of the sock. Seam
and stuff.
MOUTH: Cut the heel from the sock, leaving a brown edge around
the white. Fasten on lower part of face, whipping around the
bottom; stuff and finish sewing around top. The mouth can be
improved by a running stitch of either black or white across the
middle.
ARMS: Cut the remaining upper part of the sock into two pieces.
Seam and stuff.
EARS: Cut the ears from the remaining brown part of the sole of the
sock.
There are many variations from this basic pattern. Decorations can
be either pom poms, yarn or bells. Sometimes a fez is used for a
cap. Jackets and skirts are also used for clothing the monkey.
6
uncaptioned
7
BASIC INSTRUCTIONS FOR PAGES 7-15
Step 1
Turn sock inside out. Cut off most of the rib, leaving about 1″ for
white feet.
Step 2
Start 3″ from heel and sew two seams, ⅛″ each side of center line
and across end of rib.
Step 3
Cut between seams and about 1″ into body to make opening for
stuffing. Turn sock right side out.
Step 4
Insert ovals of cardboard or wood to shape feet. Stuff legs and rest
of sock nice and plump.
Step 5
To shape head, tie a ribbon loosely around neck. Do not tie tightly as
head will wobble.
Welcome to Our Bookstore - The Ultimate Destination for Book Lovers
Are you passionate about books and eager to explore new worlds of
knowledge? At our website, we offer a vast collection of books that
cater to every interest and age group. From classic literature to
specialized publications, self-help books, and children’s stories, we
have it all! Each book is a gateway to new adventures, helping you
expand your knowledge and nourish your soul
Experience Convenient and Enjoyable Book Shopping Our website is more
than just an online bookstore—it’s a bridge connecting readers to the
timeless values of culture and wisdom. With a sleek and user-friendly
interface and a smart search system, you can find your favorite books
quickly and easily. Enjoy special promotions, fast home delivery, and
a seamless shopping experience that saves you time and enhances your
love for reading.
Let us accompany you on the journey of exploring knowledge and
personal growth!
ebookgate.com