Preface A journey of a thousand miles begins with a single step.   A Chinese proverb
p
eople often ask: What is discrete mathematics? It's the mathematics of discrete (distinct and disconnected) objects. In other words, it is the study of discrete objects and relationships that bind them. The geometric representations of discrete objects have gaps in them. For example, integers are discrete objects, therefore (elementary) number theory, for instance, is part of discrete mathematics; so are linear algebra and abstract algebra. On the other hand, calculus deals with sets of connected (without any gaps) objects. The set of real numbers and the set of points on a plane are two such sets; they have continuous pictorial representations. Therefore, calculus does not belong to discrete mathematics, but to continuous mathematics. However, calculus is relevant in the study of discrete mathematics. The sets in discrete mathematics are often finite or countable, whereas those in continuous mathematics are often uncountable. Interestingly, an analogous situation exists in the field of computers. Just as mathematics can be divided into discrete and continuous mathematics, computers can be divided into digital and analog. Digital computers process the discrete objects 0 and 1, whereas analog computers process continuous d a t a ~ t h a t is, data obtained through measurement. Thus the terms discrete and continuous are analogous to the terms digital and analog, respectively. The advent of modern digital computers has increased the need for understanding discrete mathematics. The tools and techniques of discrete mathematics enable us to appreciate the power and beauty of mathematics in designing problemsolving strategies in everyday life, especially in computer science, and to communicate with ease in the language of discrete mathematics.
The Realization of a Dream
This book is the fruit of many years of many dreams; it is the endproduct of my fascination for the myriad applications of discrete mathematics to a variety of courses, such as Data Structures, Analysis of Algorithms, Programming Languages, Theory of Compilers, and Databases. Data structures and Discrete Mathematics compliment each other. The information in this book is applicable to quite a few areas in mathematics; discrete xiii
Preface
xiv
mathematics is also an excellent preparation for number theory and abstract
algebra. A logically conceived, selfcontained, wellorganized, and a userfriendly book, it is suitable for students and amateurs as well; so the language employed is, hopefully, fairly simple and accessible. Although the book features a wellbalanced mix of conversational and formal writing style, mathematical rigor has not been sacrificed. Also great care has been taken to be attentive to even minute details.
Audience The book has been designed for students in computer science, electrical engineering, and mathematics as a one or twosemester course in discrete mathematics at the sophomore/junior level. Several earlier versions of the text were classtested at two different institutions, with positive responses from students.
Prerequisites No formal prerequisites are needed to enjoy the material or to employ its power, except a very strong background in college algebra. A good background in precalculus mathematics is desirable, but not essential. Perhaps the most important requirement is a bit of sophisticated mathematical maturity: a combination of patience, logical and analytical thinking, motivation, systematism, decisionmaking, and the willingness to persevere through failure until success is achieved. Although no programming background is required to enjoy the discrete mathematics, knowledge of a structured programming language, such as Java or C + +, can make the study of discrete mathematics more rewarding.
Coverage The text contains indepth coverage of all major topics proposed by professional associations for a discrete mathematics course. It emphasizes problemsolving techniques, pattern recognition, conjecturing, induction, applications of varying nature, proof techniques, algorithm development, algorithm correctness, and numeric computations. Recursion, a powerful problemsolving strategy, is used heavily in both mathematics and computer science. Initially, for some students, it can be a bittersweet and demanding experience, so the strategy is presented with great care to help amateurs feel at home with this fascinating and frequently used technique for program development. This book also includes discussions on Fibonacci and Lucas numbers, Fermat numbers, and figurate numbers and their geometric representations, all excellent tools for exploring and understanding recursion.
Preface
xv
A sufficient amount of theory is included for those who enjoy the beauty in the development of the subject, and a wealth of applications as well for those who enjoy the power of problemsolving techniques. Hopefully, the student will benefit from the nice balance between theory and applications. Optional sections in the book are identified with an asterisk (.) in the left margin. Most of these sections deal with interesting applications or discussions. They can be omitted without negatively affecting the logical development of the topic. However, students are strongly encouraged to pursue the optional sections to maximize their learning.
Historical Anecdotes and Biographies Biographical sketches of about 60 mathematicians and computer scientists who have played a significant role in the development of the field are threaded into the text. Hopefully, they provide a h u m a n dimension and attach a h u m a n face to major discoveries. A biographical index, keyed to page, appears on the inside of the back cover for easy access.
Examples and Exercises Each section in the book contains a generous selection of carefully tailored examples to clarify and illuminate various concepts and facts. The backbone of the book is the 560 examples worked out in detail for easy understanding. Every section ends with a large collection of carefully prepared and wellgraded exercises (more than 3700 in total), including thoughtprovoking truefalse questions. Some exercises enhance routine computational skills; some reinforce facts, formulas, and techniques; and some require mastery of various proof techniques coupled with algebraic manipulation. Often exercises of the latter category require a mathematically sophisticated mind and hence are meant to challenge the mathematically curious. Most of the exercise sets contain optional exercises, identified by the letter o in the left margin. These are intended for more mathematically sophisticated students. Exercises marked with one asterisk (.) are slightly more advanced than the ones that precede them. Doublestarred (**) exercises are more challenging than the singlestarred; they require a higher level of mathematical maturity. Exercises identified with the letter c in the left margin require a calculus background; they can be omitted by those with no or minimal calculus. Answers or partial solutions to all oddnumbered exercises are given at the end of the book.
Foundation Theorems are the backbones of mathematics. Consequently, this book contains the various proof techniques, explained and illustrated in detail.
Preface
xvl
They provide a strong foundation in problemsolving techniques, algorithmic approach, verification and analysis of algorithms, as well as in every discrete mathematics topic needed to pursue computer science courses such as Data Structures, Analysis of Algorithms, Programming Languages, Theory of Compilers, Databases, and Theory of Computation.
Proofs Most of the concepts, definitions, and theorems in the book are illustrated with appropriate examples. Proofs shed additional light on the topic and enable students to sharpen their problemsolving skills. The various proof techniques appear throughout the text.
Applications Numerous current and relevant applications are woven into the text, taken from computer science, chemistry, genetics, sports, coding theory, banking, casino games, electronics, decisionmaking, and gambling. They enhance understanding and show the relevance of discrete mathematics to everyday life. A detailed index of applications, keyed to pages, is given at the end of the book. Algorithms Clearly written algorithms are presented throughout the text as problemsolving tools. Some standard algorithms used in computer science are developed in a straightforward fashion; they are analyzed and proved to enhance problemsolving techniques. The computational complexities of a number of standard algorithms are investigated for comparison. Algorithms are written in a simpletounderstand pseudocode that can easily be translated into any programming language. In this pseudocode: 9 Explanatory comments are enclosed within the delimeters (* and *). 9 The body of the algorithm begins with a B e g i n and ends in an E n d ; they serve as the outermost parentheses. 9 Every compound statement begins with a b e g i n and ends in an end; again, they serve as parentheses. In particular, for easy readability, a while (for) loop with a compound statement ends in e n d w h i l e (endfor).
Chapter Summaries Each chapter ends with a summary of important vocabulary, formulas, and properties developed in the chapter. All the terms are keyed to the text pages for easy reference and a quick review.
Preface
xvii
Review and Supplementary Exercises Each chapter summary is followed by an extensive set of wellconstructed review exercises. Used along with the summary, these provide a comprehensive review of the chapter. Chapterend supplementary exercises provide additional challenging opportunities for the mathematically sophisticated and curiousminded for further experimentation and exploration. The book contains about 950 review and supplementary exercises.
Computer Assignments Over 150 relevant computer assignments are given at the end of chapters. They provide handson experience with concepts and an opportunity to enhance programming skills. A computer algebra system, such as Maple, Derive, or Mathematica, or a programming language of choice can be used.
Exploratory Writing Projects Each chapter contains a set of wellconceived writing projects, for a total of about 600. These expository projects allow students to explore areas not pursued in the book, as well as to enhance research techniques and to practice writing skills. They can lead to original research, and can be assigned as group projects in a real world environment. For convenience, a comprehensive list of references for the writing projects, compiled from various sources, is provided in the S t u d e n t ' s Solutions Manual.
Enrichment Readings Each chapter ends with a list of selected references for further exploration and enrichment. Most expand the themes studied in this book.
Numbering System A concise numbering system is used to label each item, where an item can be an algorithm, figure, example, exercises, section, table, or theorem. Item m . n refers to item n in Chapter "m". For example, Section 3.4 is Section 4 in Chapter 3.
Special Symbols Colored boxes are used to highlight items that may need special attention. The letter o in the left margin of an exercise indicates that it is optional, whereas a c indicates that it requires the knowledge of calculus. Besides, every theorem is easily identifiable, and the end of every proof and example
Preface
xvill
is marked with a solid square (l l). An asterisk (.) next to an exercise indicates that it is challenging, whereas a doublestar (**) indicates that it is even more challenging. While "  " stands for equality, the closely related symbol "~" means is approximately equal to: 0 C
II
optional exercises requires a knowledge of calculus end of a proof or a solution a challenging exercise a more challenging exercise is equal to is approximately equal to
Abbreviations For the sake of brevity, four useful abbreviations are used throughout the text: LHS, RHS, PMI, and IH: LHS RHS PMI IH
LeftHand Side RightHand Side Principle of Mathematical Induction Inductive Hypothesis
Symbols Index An index of symbols used in the text and the page numbers where they occur can be found inside the front and back covers.
Web Links The World Wide Web can be a useful resource for collecting information about the various topics and algorithms. Web links also provide biographies and discuss the discoveries of major mathematical contributors. Some Web sites for specific topics are listed in the Appendix.
Student's Solutions Manual The Student's Solutions Manual contains detailed solutions of all oddnumbered exercises. It also includes suggestions for studying mathematics, and for preparing to take an math exam. The Manual also contains a comprehensive list of references for the various writing projects and assignments.
Preface
xix
Instructor's Manual
The Instructor's Manual contains detailed solutions to all evennumbered exercises, two sample tests and their keys for each chapter, and two sample final examinations and their keys. Acknowledgments
A number of people, including many students, have played a major role in substantially improving the quality of the manuscript through its development. I am truly grateful to every one of them for their unfailing encouragement, cooperation, and support. To begin with, I am sincerely indebted to the following reviewers for their unblemished enthusiasm and constructive suggestions: Gerald Alexanderson Stephen Brick Neil Calkin Andre Chapuis Luis E. Cuellar H. K. Dai Michael Daven Henry Etlinger Jerrold R. Griggs John Harding Nan Jiang Warren McGovern Tim O'Neil Michael O'Sullivan Stanley Selkow
Santa Clara University University of South Alabama Clemson University Indiana University McNeese State University Oklahoma State University Mt. St. Mary College Rochester Institute of Technology University of South Carolina New Mexico State University University of South Dakota Bowling Green State University University of Notre Dame San Diego State University Worcester Polytechnic Institute
Thanks also go to Henry Etlinger of Rochester Institute of Technology and Jerrold R. Griggs of the University of South Carolina for reading the entire manuscript for accuracy; to Michael Dillencourt of the University of California at Irvine, and Thomas E. Moore of Bridgewater State College for preparing the solutions to the exercises; and to Margarite Roumas for her excellent editorial assistance. My sincere thanks also go to Senior Editor, Barbara Holland, Production Editor, Marcy BarnesHenrie, Copy Editor, Kristin Landon, and Associate Editor, Thomas Singer for their devotion, cooperation, promptness, and patience, and for their unwavering support for the project. Finally, I must accept responsibility for any errors that may still remain. I would certainly appreciate receiving comments about any unwelcome surprises, alternate or better solutions, and exercises, puzzles, and applications you have enjoyed.
Framingham, Massachusetts September 19, 2003
Thomas Koshy
[email protected] A W o r d to t h e S t u d e n t Tell me a n d I will forget. S h o w me a n d I will remember. Involve me a n d I will u n d e r s t a n d . n
Confucius
The SALT of Life Mathematics is a science; it is an art; it is a precise and concise language; and it is a great problemsolving tool. Thus mathematics is the SALT of life. To learn a language, such as Greek or Russian, first you have to learn its alphabet, grammar, and syntax; you also have to build up a decent vocabulary to speak, read, or write. Each takes a lot of time and practice.
The Language of Mathematics Because mathematics is a concise language with its own symbolism, vocabulary, and properties (or rules), to be successful in mathematics, you must know them well and be able to apply them. For example, it is important to know that there is a difference between perimeter and area, area and volume, factor and multiple, divisor and dividend, hypothesis and hypotenuse, algorithm and logarithm, reminder and remainder, computing and solving, disjunction and destruction, conjunction and construction, and negation and negative. So you must be fluent in the language of mathematics, just like you need to be fluent in any foreign language. So keep speaking the language of mathematics. Although mathematics is itself an unambiguous language, algebra is the language of mathematics. Studying algebra develops confidence, improves logical and critical thinking, and enhances what is called mathematical maturity, all needed for developing and establishing mathematical facts, and for solving problems. This book is written in a clear and concise language that is easy to understand and easy to build on. It presents the essential (discrete) mathematical tools needed to succeed in all undergraduate computer science courses.
Theory and Applications This book features a perfect blend of both theory and applications. Mathematics does not exist without its logically developed theory; in fact, theorems are like the steel beams of mathematics. So study the various xxi
xxii
A Wordto the Student proof techniques, follow the various proofs presented, and try to reproduce them in your own words. Whenever possible, create your own proofs. Try to feel at home with the various methods and proofs. Besides developing a working vocabulary, pay close attention to facts, properties, and formulas, and enjoy the beautiful development of each topic. This book also draws on a vast array of interesting and practical applications to several disciplines, especially to computer science. These applications are spread throughout the book. Enjoy them, and appreciate the power of mathematics that can be applied to a variety of situations, many of which are found in business, industry, and scientific discovery in today's workplace.
ProblemSolving Strategies To master mathematics, you must practice it; that is, you must apply and do mathematics. You must be able to apply previously developed facts to solve problems. For this reason, this book emphasizes problemsolving techniques. You will encounter two types of exercises in the exercise sets: The first type is computational, and the second type is algebraic and theoretical. Being able to do computational exercises does not automatically imply that you are able to do algebraic and theoretical exercises. So do not get discouraged, but keep trying until you succeed. Of course, before you attempt the exercises in any section, you will need to first master the section; know the definitions, symbols, and facts, and redo the examples using your own steps. Since the exercises are graded in ascending order of difficulty, always do them in order; save the solutions and refine them as you become mathematically more sophisticated. The chapterend review exercises give you a chance to revisit the chapter. They can be used as a quick review of important concepts.
Recursion Recursion is an extremely powerful problemsolving strategy, used often in mathematics and computer science. Although some students may need a lot of practice to get used to it, once you know how to approach problems recursively, you will certainly appreciate its great power.
Stay Actively Involved Professional basketball players Magic Johnson, Larry Bird, and Michael Jordan didn't become superstars overnight by reading about basketball or by watching others play on television. Besides knowing the rules and the skills needed to play, they underwent countless hours of practice, hard work, a lot of patience and perseverance, willingness to meet failures, and determination to achieve their goal.
A Word to the Student
xxiii
Likewise, you cannot master mathematics by reading about it or by simply watching your professor do it in class; you have to get involved and stay involved by doing it every day, just as skill is acquired in a sport. You can learn mathematics only in small, progressive steps, building on skills you have already mastered. Remember the saying: Rome wasn't built in a day. Keep using the vocabulary and facts you have already studied. They must be fresh in your mind; review them every week.
A Few Suggestions for Learning Mathematics 9 Read a few sections before each class. You might not fully understand the material, but you'll follow it far better when your professor discusses it in class. In addition, you will be able to ask more questions in class and answer more questions. 9 Whenever you study the book, make sure you have a pencil and enough paper to write down definitions, theorems, and proofs, and to do the exercises. 9 Return to review the material taught in class later in the same day. Read actively; do not just read as if it was a novel or a newspaper. Write down the definitions, theorems, and properties in your own words, without looking in your notes or the book. Good notetaking and writing aid retention. Rewrite the examples, proofs, and exercises done in class, all in your own words. If you find them too challenging, study them again and try again; continue until you succeed. 9 Always study the relevant section in the text and do the examples there; then do the exercises at the end of the section. Since the exercises are graded in order of difficulty, do them in order. Don't skip steps or write over previous steps; this way you'll progress logically, and you can locate and correct your errors. If you can't solve a problem because it involves a new term, formula, or some property, then restudy the relevant portion of the section and try again. Don't assume that you'll be able to do every problem the first time you try it. Remember, practice is the only way to Success.
Solutions Manual The Student's Solutions Manual contains additional helpful tips for studying mathematics, and preparing for and taking an examination in mathematics. It also gives detailed solutions to all oddnumbered exercises and a comprehensive list of references for the various exploratory writing projects.
A Final Word Mathematics is no more difficult than any other subject. If you have the motivation, and patience to learn and do the work, then you will enjoy
xxiv
A Word to the Student
the beauty and power of discrete mathematics; you will see that discrete mathematics is really fun. Keep in mind that learning mathematics is a stepbystep process. Practice regularly and systematically; review earlier chapters every week, since things must be fresh in your mind to apply and build on them. In this way, you will enjoy the subject, feel confident, and to explore more. The name of the game is practice, so practice, practice, practice. I look forward to hearing from you with your comments and suggestions. In the meantime, enjoy the beauty and power of mathematics. Thomas Koshy
Chapter 1
The L a n g u a g e of Logic Symbolic logic has been disowned by many logicians on the plea that its interest is mathematical and by many mathematicians on the plea that its interest is logical.   A . N. W H I T E H E A D
L
ogic is the study of the principles and techniques of reasoning. It originated with the ancient Greeks, led by the philosopher Aristotle, who is often called the father of logic. However, it was not until the 17th century that symbols were used in the development of logic. German philosopher and mathematician Gottfried Leibniz introduced symbolism into logic. Nevertheless, no significant contributions in symbolic logic were made until those of George Boole, an English mathematician. At the age of 39, Boole published his outstanding work in symbolic logic, A n Investigation of the L a w s of Thought. Logic plays a central role in the development of every area of learning, especially in mathematics and computer science. Computer scientists, for example, employ logic to develop programming languages and to establish the correctness of programs. Electronics engineers apply logic in the design of computer chips. This chapter presents the fundamentals of logic, its symbols, and rules to help you to think systematically, to express yourself in precise and concise terms, and to make valid arguments. Here are a few interesting problems we shall pursue in this chapter: 9 Consider the following two sentences, both There are more residents in New York City head of any resident. No resident is totally sion: Is it true that at least two residents hairs? (R. M. Smullyan, 1978)
true: than there are hairs on the bald. What is your concluhave the same number of
9 There are two kinds of inhabitants, "knights" and "knaves," on an island. Knights always tell the truth, whereas knaves always lie. Every inhabitant is either a knight or a knave. Tom and Dick are two residents. Tom says, "At least one of us is a knave." What are Tom and Dick?
Chapter I The Language of Logic
A r i s t o t l e (384322 B.C.), o n e of the greatest philosophers in Western culture, was born in Stagira, a small town in northern Greece. His father was the personal physician of the king of Macedonia. Orphaned young, Aristotle was . ~,; ~" i. ~'' % ~ ,,. "":. ;'i ' . ' ~ ' raised by a guardian. At the age of 18, Aristotle entered Plato's Academy in Athens. He was the "brightest and most learned student" at the Academy which he left when Plato died in 34 7 B.C. About 342 B.C., the king of Macedonia invited him to supervise the education of his young son, Alexander, who later became Alexander the Great. Aristotle taught him until 336 B.C., when the youth became ruler following the assassination of his father. ...*:4 ......}"~:~i:~:.r Around 334 B.C., Aristotle returned to Athens and founded a school called the Lyceum. His philosophy and followers were called peripatetic, a Greek word meaning "walking around," since Aristotle taught his students while walking with them. The Athenians, perhaps resenting his relationship with Alexander the Great, who had conquered them, accused him of impiety soon after the Emperor's death in 323 B.C. Aristotle, knowing the fate of Socrates, who had been condemned to death on a similar charge, fled to Chalcis, so the Athenians would not "sin twice against philosophy." He died there the following year.
W h a t are t h e y if T o m says, " E i t h e r I ' m a k n a v e or Dick is a k n i g h t " ? (R. M. S m u l l y a n , 1978) 9 Are t h e r e positive i n t e g e r s t h a t can be e x p r e s s e d as t h e s u m of t w o d i f f e r e n t cubes in two d i f f e r e n t ways? 9 Does t h e f o r m u l a E ( n ) = n 2  n + 41 yield a p r i m e n u m b e r for e v e r y positive i n t e g e r n?
A d e c l a r a t i v e s e n t e n c e t h a t is e i t h e r t r u e or false, b u t not both, is a p r o p o s i t i o n (or a s t a t e m e n t ) , w h i c h we will d e n o t e by t h e l o w e r c a s e l e t t e r p, q, r, s, or t. T h e v a r i a b l e s p, q, r, s, or t are b o o l e a n v a r i a b l e s (or l o g i c variables). T h e following s e n t e n c e s are propositions: (1) (2) (3) (4)
S o c r a t e s w a s a G r e e k philosopher. 3+4=5. 1 + 1 = 0 a n d t h e m o o n is m a d e of g r e e n cheese. If i = 2, t h e n roses are red.
T h e following s e n t e n c e s a r e not propositions: 9 Let m e go!
(exclamation)
9 x+3=5
(x is an u n k n o w n . )
1.1
Propositions
B a r o n Gottfried Wilhelm Leibniz (16461716), an outstanding German mathematician, philosopher, physicist, diplomat, and linguist, was born into a Lutheran family. The son of a professor of philosophy, he "grew up to be a genius with encylopedic knowledge." He had taught himself Latin, Greek, and philosophy before entering the University of Leipzig at age 15 as a law student. There he read the works of great scientists and philosophers such as Galileo, Francis Bacon, and Rend Descartes. Because of his youth, Leipzig refused to award him the degree of the doctor of laws, so he left his native city forever. During 16631666, he attended the universities of Jena and Altdorf, and receiving his doctorate from the latter in 1666, he began legal services for the 7 Elector of Mainz. After the Elector's death, Leibniz pursued scientific studies. In 1672, he built a calculating machine that could multiply and divide and presented it to the Royal Society in London the following year. In late 1675, Leibniz laid the foundations of calculus, an honor he shares with Sir Isaac Newton. He discovered the fundamental theorem of calculus, and invented the popular notationsd/dx for differentiation and f for integration. He also introduced such modern notations as dot for multiplication, the decimal point, the equal sign, and the colon for ratio. From 1676, until his death, Leibniz worked for the Duke of Brunswick at Hanover and his estate after the duke's death in 1680. He played a key role in the founding of the Berlin Academy of Sciences in 1700. Twelve years later, Leibniz was appointed councilor of the Russian Empire and was given the title of baron by Peter the Great. Suffering greatly from gout, Leibniz died in Hanover. He was never married. His works influenced such diverse disciplines as theology, philosophy, mathematics, the natural sciences, history, and technology. , ..........
Lb~,
9 Close the door!
(command)
9 Kennedy was a great president of the United States.
(opinion)
9 What is my line?
(interrogation)
1
Truth Value
The truthfulness or falsity of a proposition is called its t r u t h v a l u e , denoted by T(true) and F(false), respectively. (These values are often denoted by 1 and 0 by computer scientists.) For example, the t r u t h value of statement (1) in Example 1.1 is T and that of statement (2) is F. Consider the sentence, This sentence is false. It is certainly a valid declarative sentence, but is it a proposition? To answer this, assume the sentence is true. But the sentence says it is false. This contradicts our assumption. On the other hand, suppose the sentence is false. This implies the sentence
Chapter I The Language of Logic
George Boole (18151864), the son of a cobbler whose main interests were mathematics and the making of optical instruments, was born in Lincoln, England. Beyond attending a local elementary school and briefly a commercial school, Boole was selftaught in mathematics and the classics. When his father's business failed, he started working to support the family. At 16, he began his teaching career, opening a school of his own four years later in Lincoln. In his leisure time, Boole read mathematical journals at the Mechanics Institute. There he grappled with the works of English physicist and mathematician Sir Isaac Newton and French mathematicians PierreSimon Laplace and JosephLouis Lagrange. In 1839, Boole began contributing original papers on differential equations to The Cambridge Mathematics Journal and on analysis to the Royal Society. In 1844, he was awarded a Royal Medal by the Society for his contributions to analysis; he was elected a fellow of the Society in 1857. Developing novel ideas in logic and symbolic reasoning, he published his first contribution to symbolic logic, The Mathematical Analysis of Logic, in 184 7. His publications played a key role in his appointment as professor of mathematics at Queen's College, Cork, Ireland, in 1849, although he lacked a university education. In 1854, he published his most important work, An Investigation to the Laws of Thought, in which he presented the algebra of logic now known as boolean algebra (see Chapter 12). The next year he married Mary Everest, the niece of Sir George Everest, for whom the mountain is named. In addition to writing about 50 papers, Boole published two textbooks, Treatise on Differential Equations (1859) and Treatise on the Calculus of Finite Differences; both were used as texts in the United Kingdom for many years. A conscientious and devoted teacher, Boole died of pneumonia in Cork. .....
I
1
is true, which again contradicts our assumption. Thus, if we assume t h a t the sentence is true, it is false; and if we assume t h a t it is false, it is true. It is a meaningless and selfcontradictory sentence, so it is not a proposition, but a p a r a d o x . The t r u t h value of a proposition may not be known for some reason, b u t t h a t does not prevent it from being a proposition. For example, around 1637, the F r e n c h m a t h e m a t i c a l genius PierreSimon de F e r m a t conjectured t h a t the equation x n + yn = z n has no positive integer solutions, where n >_ 3. His conjecture, known as F e r m a t ' s L a s t " T h e o r e m , " was one of the celebrated unsolved problems in n u m b e r theory, until it was proved in 1993 by the English m a t h e m a t i c i a n Andrew J. Wiles (1953) of Princeton University. Although the t r u t h value of the conjecture eluded m a t h e m a t i c i a n s for over three centuries, it was still a proposition! Here is a n o t h e r example of such a proposition. In 1742 the P r u s s i a n m a t h e m a t i c i a n Christian Goldbach conjectured t h a t every even integer greater t h a n 2 is the sum of two primes, not necessarily distinct. For example, 4  2 + 2, 6  3 + 3, and 18 = 7 + 11. It has been shown true for every
1.1 Propositions
F e r m a t (16011665) was born near Toulouse as the son of a leather merchant. A lawyer by profession, he devoted his leisure time to mathematics. Although he published almost none of his discoveries, he did correspond with contemporary mathematicians. Fermat contributed to several branches of mathematics, but he is best known for his work in number theory. Many of his results appear in margins of his copy of the works of the Greek mathematician Diophantus (250 A.D. ?). He wrote the following about his famous conjecture: "I have discovered a truly wonderful proof, but the margin is too small to contain it."
C h r i s t i a n Goldbach (16901764) was born in K6nigsberg, Prussia. He studied medicine and mathematics at the University of K6nigsberg and became professor of mathematics at the Imperial Academy of Sciences in St. Petersburg in 1725. In 1728, he moved to Moscow to tutor Tsarevich Peter H and his cousin Anna of Courland. From 1729 to 1763, he corresponded with Euler on number theory. He returned to the Imperial Academy in 1732, when Peter's successor Anna moved the imperial court to St. Petersburg. In 1742, Goldbach joined the Russian Ministry of Foreign Affairs, and later became privy councilor and established guidelines for the education of royal children. Noted for his conjectures in number theory and work in analysis, Goldbach died in Moscow.
even integer less than 4 • 1014, but no one has been able to prove or disprove his conjecture. Nonetheless, the Goldbach conjecture is a proposition. Propositions (1) and (2) in Example 1.1 are s i m p l e p r o p o s i t i o n s . A compound proposition is formed by combining two or more simple propositions called c o m p o n e n t s . For instance, propositions (3) and (4) in Example 1.1 are compound. The components of proposition (4) are I = 2 and Roses are red. The truth value of a compound proposition depends on the truth values of its components. Compound propositions can be formed in several ways, and they are presented in the rest of this section.
Conjunction The conjunction of two arbitrary propositions p and q, denoted by p A q, is the proposition p a n d q. It is formed by combining the propositions using the word and, called a connective.
Chapter I The Language of Logic Consider the s t a t e m e n t s p: S o c r a t e s w a s a G r e e k p h i l o s o p h e r q: E u c l i d w a s a C h i n e s e m u s i c i a n .
and
Their conjunction is given by p A q: S o c r a t e s w a s a G r e e k p h i l o s o p h e r a n d E u c l i d w a s a Chinese musician.
m
To define the t r u t h value of p A q, where p and q are a r b i t r a r y propositions, we need to consider four possible cases: 9 p is true, q is true. 9 p is true, q is false. 9 p is false, q is true. 9 p is false, q is false. (See the t r e e d i a g r a m in Figure 1.1 and Table 1.1.) If both p and q are true, t h e n p A q is true; i f p is t r u e and q is false, t h e n p A q is false; i f p is fhlse and q is true, t h e n p A q is false; and if both p and q are false, t h e n p A q is also false.
F i g u r e 1.1
Truth value ofp
Truth value ofq T
T F F
T a b l e 1.1
P
q
T T F F
T F T F
P^q
This information can be s u m m a r i z e d in a table. In the third column of Table 1.1, enter the t r u t h value ofp A q c o r r e s p o n d i n g to each pair of t r u t h values of p and q. The resulting table, Table 1.2, is the t r u t h t a b l e for pAq.
1.1 Propositions Table 1.2
p
q
pAq
T r u t h table for p A q
T T F F
T F T F
T F F F
Expressions t h a t yield the value t r u e or false are b o o l e a n e x p r e s s i o n s , and they often occur in both m a t h e m a t i c s and computer science. For instance, 3 < 5 and 5 < 5 are boolean expressions. Ifstatements and whileloops in computer programs often use such expressions, and their values determine w h e t h e r or not ifstatements and whileloops will be executed, as the next example illustrates. Determine w h e t h e r the assignment s t a t e m e n t , sum __ 5)]
then
SOLUTION: The s t a t e m e n t x ~ x + 1 will be executed if the value of the boolean expression ~ l ( a < b) v (b >__5)1 is true. By De M o r g a n ' s law, ~ l ( a < b) v (b >__5)1  ~ ( a < b) A ~(b >__5)  (a > b) A (b < 5) Sincea7andb4, botha>_bandb < 5 are true; so, (a >__b ) A(b < 5) is true. Therefore, the a s s i g n m e n t s t a t e m e n t will be executed, m One of the elegant applications of the laws of logic is employing t h e m to simplify complex boolean expressions, as the next example illustrates. Using the laws of logic simplify the boolean expression (p A ~q) v q v (~p Aq). SOLUTION" [The justification for every step is given on its r i g h t  h a n d  s i d e (RHS). ] (p A ~q) v q v (~p A q)  [(p A ~q) v q] v (p A q) [qV(pA~q)]V(~pAq)
assoc, law comm. law
25
1.2 Logical Equivalences  [(q v p) A (q v ~q)] v (~p A q)
dist. law
 [(q V p) A t] V (~p A q)
qv~qt
= (q V p) V (~p A q)
rAtr
= (~p
comm. law
A
q)
v
(p
v
q)
 [~p v (p v q)]
A
[q v (p v q)]
dist. law
= [(~p v p) v q]
A
[q v (p v q)]
assoc, law
_ (t
v
q)
A
[q
v
(p
v
q)]
~p vpt
= t A [q V (p Vq)]
tvqt
 q V (p V q)
tArr
=_qV(qVp)
comm. law
=(qVq)
assoc, law
Vp
=_qvp
idem. law
pVq
comm. law
For any propositions p, q, and r, it can be shown t h a t p ~ (q v r) (p A~q) ~ r (see Exercise 12). We shall employ this result in Section 1.5. Here are two e l e m e n t a r y but elegant applications of this equivalence. Suppose a and b are any two real n u m b e r s , and we would like to prove the following theorem: I f a . b = O, then either a = 0 or b = 0. By virtue of the above logical equivalence, we need only prove the following proposition: I f a . b = 0 a n d a V: O, t h e n b = 0 (see Exercise 43 in Section 1.5). Second, suppose a and b are two a r b i t r a r y positive integers, and p a prime number. Suppose we would like to prove the following fact: Ifp[ab,* then either p la or p lb. Using the above equivalence, it suffices to prove the following equivalent s t a t e m e n t : I f plab a n d p Xa, t h e n p]b (see Exercise 37 in Section 4.2). We shall now show how useful symbolic logic is in the design of switching networks.
Equivalent Switching Networks (optional) Two switching n e t w o r k s A and B are equivalent if they have the same electrical behavior, either b o t h open or both closed, symbolically described by A  B. One of the i m p o r t a n t applications of symbolic logic is to replace an electrical network, w h e n e v e r possible, by an equivalent simpler network to minimize cost, as illustrated in the following example. To this end,
*xly m e a n s
"x is a f a c t o r o f y . "
Chapter 1 The Language of Logic
26
let A be any circuit, T a closed circuit, and F an open circuit. T h e n A A T A, A A A'  F, A v T = T, and A v A'  T (see laws 3 t h r o u g h 8). Likewise, laws 1 t h r o u g h 11 can also be extended to circuits in an obvious way. ~
Replace the switching n e t w o r k in Figure 1.5 by an equivalent simpler network.

F i g u r e 1.5
@ 9
SOLUTION: The given network is r e p r e s e n t e d by (A A B') v [(A A B) v C]. Let us simplify this expression using the laws of logic. (The reason for each step is given on its RHS.) (A A B') v I(A A B) v CI = I(A A B') v (A A B)I v C
assoc, law
 [A A (B' v B)I v C
dist. law
 (A A T) v C
B'vBT
_=AvC
AAT=A
Consequently, the given circuit can be replaced by the simpler circuit in Figure 1.6.

F i g u r e 1.6
9 We close this section with a brief introduction to fuzzy logic.
Fuzzy Logic (optional) "The binary logic of m o d e r n computers," wrote Bart Kosko and Satoru Isaka, two pioneers in the development of fuzzy logic systems, "often falls short when describing the vagueness of the real world. Fuzzy logic offers more graceful alternatives." Fuzzy logic, a b r a n c h of artificial intelligence, incorporates the vagueness or value j u d g e m e n t s t h a t exist in everyday life, such as "young," "smart," "hot," and "cold." The first company to use a fuzzy system was F. L. Smidth and Co., a cont r a c t i n g company in Copenhagen, Denmark, which in 1980 used it to r u n a
27
1.2 LogicalEquivalences
B a r t Kosko holds degrees in philosophy and economics from the Universityof Southern California, an
M.S. in applied mathematics, and a Ph.D. in electrical engineering from the University of California, Irvine. Currently, he is on the faculty in electrical engineering at the University of Southern California. S a t o r u I s a k a received his M.S. and Ph.D. in systems science from the University of California, San Diego. He specializes in fuzzy information processing at Omron Advanced Systems at Santa Clara, and in the application of machine learning and adaptive control systems to biomedical systems and factory automation.
cement kiln. Eight years later, Hitachi used a fuzzy system to r u n the subway system in Sendai, Japan. Since then Japanese and American companies have employed fuzzy logic to control hundreds of household appliances, such as microwave ovens and washing machines, and electronic devices, such as cameras and camcorders. (See Figure 1.7.) It is generally believed t h a t fuzzy, commonsense models are far more useful and accurate t h a n standard mathematical ones.
F i g u r e 1.7
JuST TeEw~y (;INA LIKESIT
JUST THE WHY TEl) LIKES ir dUSTTHEWAY THE FEDEX 6UT LIKES IT
In fuzzy logic, the t r u t h value t(p) of a proposition p varies from 0 to 1, depending on the degree of its truth; so 0 _< t(p) _< 1. For example, the s t a t e m e n t "The room is cool" may be assigned a t r u t h value of 0.4; and the s t a t e m e n t "Sarah is smart" may be assigned a t r u t h value of 0.7.
(~hapter 1 The Language of Logic
28
Let 0 < x , y _< 1. T h e n t h e o p e r a t i o n s A, V, a n d ' a r e defined as follows" x A y  min{x,y} x v y  max{x,y} !
x 1x w h e r e min{x,y} d e n o t e s t h e m i n i m u m o f x a n d y, a n d max{x,y} d e n o t e s t h e m a x i m u m of x a n d y. N o t all p r o p e r t i e s in p r o p o s i t i o n a l logic are valid in fuzzy logic. F o r i n s t a n c e , t h e l a w o f e x c l u d e d m i d d l e , p v ~ p is true, does n o t hold in fuzzy logic. To see this, let p be a simple p r o p o s i t i o n w i t h t(p) = 0.3. T h e n t(p') = 1  0 . 3 = 0. 7; so t ( p v p ' ) = t ( p ) v t ( p ' ) = 0 . 3 v 0 . 7 = m a x { 0 . 3 , 0.7} = 0 . 7 # 1. T h u s p v p ' is not a t a u t o l o g y in fuzzy logic, l In p r o p o s i t i o n a l logic, t(p v p') = 1; so p v p' is a t a u t o l o g y . T h i n k of 1 r e p r e s e n t i n g a T a n d 0 r e p r e s e n t i n g an F. I Likewise, t(p A p') = t(p) A t(p') = 0.3 A 0. 7 = min{0.3, 0.7} = 0 . 3 # 0; so p A p ' is not a c o n t r a d i c t i o n , u n l i k e in p r o p o s i t i o n a l logic. N e x t we p r e s e n t briefly an i n t e r e s t i n g application* of fuzzy logic to decision m a k i n g . It is based on t h e Y a g e r m e t h o d , developed in 1981 by R o n a l d R. Yager of Iona College, a n d e m p l o y s fuzzy i n t e r s e c t i o n a n d i m p l i c a t i o n ~, defined by p ~ q = u p v q.
Fuzzy Decisions S u p p o s e t h a t from a m o n g five U.S. c i t i e s   B o s t o n , Cleveland, M i a m i , N e w York, a n d San D i e g o   w e would like to select t h e b e s t city to live in. We will use seven c a t e g o r i e s C I t h r o u g h C7 to m a k e t h e decision; t h e y are climate, cost o f h o u s i n g , cost o f living, o u t d o o r activities, e m p l o y m e n t , crime, a n d culture, respectively, a n d are j u d g e d on a scale 0  6 : 0 = terrible, 1 = bad, 2 = pool', 3 = average, 4 = fairly good, 5 = very good, a n d 6 = excellent. T a b l e 1.15 shows t h e relative i m p o r t a n c e of each c r i t e r i o n on a scale 0  6 a n d t h e r a t i n g for e a c h city in each category. T a b l e 1.15
Category
Importance
Boston
Cleveland
Miami
New York
San Diego
C1 C2 C3 C4 C5 C6 C7
6 3 2 4 4 5 4
3 1 3 5 4 2 6
2 5 4 3 3 4 3
5 4 3 6 3 0 3
1 0 1 2 4 1 6
6 1 5 6 3 3 5
*Based on M. Caudill, "Using Neural Nets: Fuzzy Decisions," A/Expert, Vol. 5 (April 1990), pp. 5964.
1.2 Logical Equivalences
29
The ideal city to live in will score high in the categories considered m o s t i m p o r t a n t . In order to choose the finest city, we need to evaluate each city by each criterion, weighing the relative i m p o r t a n c e of each category. Thus, given a p a r t i c u l a r category's i m p o r t a n c e , we m u s t check the city's score in t h a t category; in o t h e r words, we m u s t c o m p u t e the t r u t h value of i + s  ~ i v s for each city, w h e r e i denotes t h e i m p o r t a n c e r a n k i n g for a p a r t i c u l a r category and s the city's score for t h a t category. Table 1.16 shows the r e s u l t i n g data. Now we take the conjunction of all scores for each city, using the m i n function (see Table 1.16). The lowest combined score d e t e r m i n e s t h e city's overall ranking. It follows from the table t h a t San Diego is clearly t h e winner.
T a b l e 1.16
Category
~i
C1 C2 C3 C4 C5 C6 C7
0 3 4 2 2 1 2
Intersection
N e w York
San D i e g o
s
Boston ~ivs
s
Cleveland ,~ivs
s
,.~ivs
s
s
~ivs
3 1 3 5 4 2 6
3 3 4 5 4 2 6
2 5 4 3 3 4 3
2 5 4 3 3 4 3
5 4 3 6 3 0 3
5 4 4 6 3 1 3
1 0 1 2 4 1 6
6 1 5 6 3 3 5
6 3 5 6 3 3 5
2
Miami
2
1
,~ivs
1 3 4 2 4 1 6 1
next best choices
3 winner
Finally, suppose we add a sixth city, say, Atlanta, for consideration. T h e n the Yager m e t h o d e n s u r e s t h a t the revised choice will be the existing choice (San Diego) or Atlanta; it c a n ' t be any of the others. T h u s the p r o c e d u r e allows i n c r e m e n t a l decision making, so m a n a g e a b l e subdecisions can be combined into an overall final choice.
Exercises 1.2 Give the t r u t h value o f p in each case. 1. p  q, and q is not true.
2. p  q, q  r, and r is true.
Verify each, w h e r e f denotes a contradiction. (See Table 1.14.)
3. ~ ( ~ p ) =p
4. p Ap p
5. p Vp = p
6. p A q =   q A p
7. p V q = q V p
8. " ~ ( p V q ) =   ~ p A '  q
9. ~ ( p > q)  p
A ~q
30
Chapter I
The Language of Logic
10. p ~ q = ( p A ~ q )   ~ f
11. p A ( q A r ) =   ( p A q ) A r
12. p ~ ( q V r )   ( p A ' ~ q ) ~ r
13.
(p v q) ~
r  (p ~ r) A (q ~
r)
U s e De M o r g a n ' s laws to e v a l u a t e e a c h b o o l e a n e x p r e s s i o n , w h e r e x = 2, y = 5, a n d z = 3. 14. ~ [ ( x < z ) 16.
A(y 1 + n x . Since (1 + x) ~ > 1 + 0. x always, the proposition P(0) is true. Thus the theorem is trivially true when n = 0. In this trivial proof we did not use the premise t h a t x > 0. m
Next we pursue a n o t h e r proof method.
Direct Proof In the d i r e c t p r o o f of the t h e o r e m H1 A H2 A . . . A Hn ~ C, assume the given hypotheses Hi are true. Using the laws of logic or previously known facts, establish the desired conclusion C as the final step of a chain of implications: H ~ C1, C1 ~ C2,... ,Cn * C. Then, by the repeated application of the hypothetical syllogism, it follows t h a t H ~ C. The next example illustrates this method. Often, theorems are stated in terms of sentences, so we need to first rewrite t h e m symbolically and t h e n work with the symbols, as the next example demonstrates. Prove directly t h a t the product of any two odd integers is an odd integer.
PROOF: Let x and y be any two odd integers. Then there exist integers m and n such t h a t x = 2m + 1 and y = 2n + 1. Thus, x . y  (2m + 1). (2n + 1) = 4mn
+ 2m + 2n + 1
= 2(2mn + m + n) + 1 =2k+1 where k  2 m n + m + n is an integer. Therefore, x y is an odd integer. This concludes the proof. (Can you rewrite this proof as a chain of implications?) m
1.5 ProofMethods
51
Indirect Proof T h e r e are two kinds of i n d i r e c t p r o o f s for the t h e o r e m H1 A H2 A . .  A Hn > C: p r o o f o f t h e c o n t r a p o s i t i v e and p r o o f b y c o n t r a d i c t i o n . The first m e t h o d is based on the law of the contrapositive, H1 A H2 A  . . A Hn > C  ~ C + ~(H1 A H2 A.. A Hn). [You m a y recall, by De M o r g a n ' s law, t h a t ~(H1 A H2 A . . . A Hn) = ~H1 v ~ H 2 v . . . v ~Hn.] In this method, a s s u m e the desired conclusion C is false; t h e n using the laws of logic, establish t h a t some hypothesis Hi is also false. Once you have done this, the t h e o r e m is proved. The next example enlightens this method. Prove indirectly: If the square of an integer is odd, t h e n the integer is odd.
PROOF OF THE CONTRAPOSITIVE Let x be any integer such t h a t X 2 is odd. We would like to prove t h a t x m u s t be an odd integer. In the indirect method, we a s s u m e the conclusion is false; t h a t is, x is not odd; in o t h e r words, a s s u m e x is an even integer. Let x  2k for some integer k. T h e n x 2  (2k) 2  4k 2  2(2k2), which is an even integer. This makes our hypothesis t h a t x 2 is an odd integer false. Therefore, by the law of the contrapositive, our a s s u m p t i o n m u s t be wrong; in other words, x m u s t be an odd integer. Thus, ifx 2 is an odd integer, t h e n x is also an odd integer, m P r o o f b y c o n t r a d i c t i o n , the other variation of indirect proof, is based on the law of reductio ad absurdum: H1 A H2 A . . . A H,~ ~ C ~ IH1 A H2 A . . . A H,~ A (~C) ~ F. In this method, a s s u m e the given hypotheses Hi are true, but the conclusion C is false. T h e n argue logically and reach a contradiction F. The next example illustrates this method, where a p r i m e n u m b e r p is a positive integer with exactly two positive factors, 1 and p. Prove by contradiction: T h e r e is no largest prime n u m b e r ; t h a t is, there are infinitely m a n y prime n u m b e r s . P R O O F BY C O N T R A D I C T I O N (Notice that the theorem has no explicit hypothesis.) Suppose the given conclusion is false; t h a t is, t h e r e is a largest prime n u m b e r p. So the prime n u m b e r s we have are 2, 3, 5 , . . . ,p; a s s u m e t h e r e are k such primes, p l , p 2 , . . . , and Pk. Let x denote the product of all of these prime n u m b e r s plus one: x = ( 2 . 3  5 . . . p ) + 1. Clearly, x > p. W h e n x is divided by each of the primes 2, 3, 5 , . . . ,p, we get 1 as the remainder. So x is not divisible by any of the primes. Hence either x m u s t be a prime, or if x is composite t h e n x is divisible by a prime q # Pi. In either case, there are more t h a n k primes. But this contradicts the a s s u m p t i o n t h a t there are k primes, so our a s s u m p t i o n is false. In other words, t h e r e is no largest prime number, m Now we t u r n to yet a n o t h e r proof technique.
Chapter I
52
The Language of Logic
Proof by Cases Suppose we would like to prove a t h e o r e m of the form H1 v H2 v . . . v Hn + C. Since H I v H2 v . . . v H a + C  ( H I > C) A (H2 > C) A . .  A (Hn ~ C), the s t a t e m e n t H1 v H2 v . . . v Hn ~ C is t r u e if a n d only if each i m p l i c a t i o n Hi > C is true. Consequently, we need only prove t h a t each
implication is true. Such a proof is a p r o o f b y c a s e s , as i l l u s t r a t e d in the following example, due to R. M. Smullyan. Let A, B, and C be three i n h a b i t a n t s of the island described in Example 1.32. Two i n h a b i t a n t s are of the s a m e type if they are b o t h k n i g h t s or both knaves. Suppose A says, "B is a knave," and B says, "A a n d C are of the same type." Prove t h a t C is a knave.
PROOF BY CASES Although this t h e o r e m is not explicitly of the form II1 v H2 v . . . v IIn ~ C, we artificially create two cases, namely, A is a k n i g h t and A is a knave.
Case 1
Suppose A is a knight. Since k n i g h t s always tell the t r u t h , his s t a t e m e n t t h a t B is a knave is true. So B is a knave and hence B's s t a t e m e n t is false. Therefore, A and C are of different types; t h u s C is a knave.
Case 2
Suppose A is a knave. T h e n his s t a t e m e n t is false, so B is a knight. Since k n i g h t s always tell the t r u t h , B's s t a t e m e n t is true. So A and C are of the same type; t h u s C is a knave. T h u s in both cases, C is a knave,
m
Existence Proof Finally, t h e o r e m s of the form (~x)P(x) also occur in m a t h e m a t i c s . To prove such a theorem, we m u s t establish the existence of an object a for which P(a) is true. Accordingly, such a proof is an e x i s t e n c e p r o o f . T h e r e are two kinds of existence proofs: the c o n s t r u c t i v e e x i s t e n c e proof and the n o n c o n s t r u c t i v e e x i s t e n c e p r o o f . If we are able to find a m a t h e m a t i c a l object b such t h a t P(b) is true, such an existence proof is a c o n s t r u c t i v e p r o o f . The following example elucidates this method. Prove t h a t there is a positive integer t h a t can be expressed in two different ways as the sum of two cubes.
CONSTRUCTIVE PROOF By the discussion above, all we need is to produce a positive integer b t h a t has the required properties. Choose b  1729. Since 1729  13 + 123 = 93 + 103, 1729 is such an integer.* m
*A fascinating anecdote is told about the number 1729. In 1919, when the Indian mathematical genius Srinivasa Ramanujan (18871920) was sick in a nursing home in England, the eminent
1.5 ProofMethods
53
A n o n c o n s t r u c t i v e existence proof of the t h e o r e m (3x)P(x) does not provide us with an e l e m e n t a for which P(a) is true, b u t r a t h e r establishes its existence by an indirect m e t h o d , usually contradiction, as i l l u s t r a t e d by the next example. Prove t h a t t h e r e is a p r i m e n u m b e r > 3.
NONCONSTRUCTIVE PROOF Suppose t h e r e are no primes > 3. T h e n 2 and 3 are the only primes. Since every integer >_ 2 can be expressed as a p r o d u c t of powers of primes, 25 m u s t be expressible as a p r o d u c t of powers of 2 and 3, t h a t is, 25  2 i 3 j for some integers i and j. But n e i t h e r 2 nor 3 is a factor of 25, so 25 c a n n o t be w r i t t e n in the form 2 i 3 j , a contradiction. Consequently, t h e r e m u s t be a p r i m e > 3. II We invite you to give a constructive proof of the s t a t e m e n t in the example. We conclude this section with a brief discussion of counterexamples.
Counterexample Is the s t a t e m e n t E v e r y g i r l is a b r u n e t t e t r u e or false? Since we can find at least one girl who is not a b r u n e t t e , it is false! More generally, suppose you would like to show t h a t the s t a t e m e n t (Vx)P(x) is false. Since ~[(Vx)P(x)] _= (3x)[~P(x)] by De M o r g a n ' s law, the s t a t e m e n t (Vx)P(x) is false if t h e r e exists an item x in the UD for which the predicate P(x) is false. Such an object x is a c o u n t e r e x a m p l e . Thus, to disprove the proposition (Vx)P(x), all we need is to produce a c o u n t e r e x a m p l e c for which P(c) is false, as the next two examples d e m o n s t r a t e . N u m b e r theorists d r e a m of finding formulas t h a t g e n e r a t e p r i m e n u m b e r s . One such f o r m u l a was found by the Swiss m a t h e m a t i c i a n L e o n h a r d E u l e r (see C h a p t e r 8), namely, E ( n )  n 2  n + 41. It yields a p r i m e for n = 1, 2, . . . , 40. Suppose we claim t h a t the f o r m u l a g e n e r a t e s a p r i m e for every positive integer n. Since E(41) = 412  41 + 41 = 412 is not a prime, 41 is a counterexample, t h u s disproving the claim. II A r o u n d 1640, F e r m a t conjectured t h a t n u m b e r s of the form f ( n )  22'' + 1 are prime n u m b e r s for all n o n n e g a t i v e integers n. For instance, f(0) = 3, f(1) = 5, f(2) = 17, f(3) = 257, and f(4) = 65,537 are all primes. In 1732, however, E u l e r established the falsity of F e r m a t ' s conjecture by producing a counterexample. He showed t h a t f(5)  22'~ + 1  641 • 6700417, a composite n u m b e r . (Prime n u m b e r s of the form 22'' + I are called F e r m a t primes.) I1 English mathematician Godfrey Harold Hardy (18771947) visited him. He told Ramanujan that the number of the cab he came in, 1729, was "a rather dull number" and hoped that it wasn't a bad omen. "No, Hardy," Ramanujan responded, "It is a very interesting number. It is the smallest number expressible as the sum of two cubes in two different ways."
Chapter I The Language of Logic
54
Exercises 1.5 D e t e r m i n e if each implication is vacuously t r u e for the indicated value of n. 1. If n > 1, t h e n 2 n > n; n  0 2. If n > 4, t h e n 2 n >_ n2"n,  O, 1, 2, 3 D e t e r m i n e if each implication is trivially true. 3. If n is a p r i m e n u m b e r , t h e n n 2 + n is an even integer. 4. If n > 41, t h e n n 3  n is divisible by 3. Prove each directly. 5. The s u m of a n y two even i n t e g e r s is even. 6. The s u m of a n y two odd i n t e g e r s is even. 7. The s q u a r e of an even i n t e g e r is even. 8. The product of any two even i n t e g e r s is even. 9. The s q u a r e of an odd integer is odd. 10. The product of any two odd i n t e g e r s is odd. 11. The product of any even i n t e g e r a n d any odd integer is even. 12. The s q u a r e of every integer of t h e form 3k + 1 is also of t h e s a m e form, w h e r e k is an a r b i t r a r y integer. 13. The s q u a r e of every integer of t h e form 4k + 1 is also of t h e s a m e form, w h e r e k is an a r b i t r a r y integer. 14. T h e a r i t h m e t i c
mean
~~ of a n y two n o n n e g a t i v e real n u m b e r s a
and b is g r e a t e r t h a n or equal to t h e i r g e o m e t r i c m e a n j~ab. IHint" consider (v/a v/b)2 > 0.] Prove each u s i n g the law of the contrapositive. 15. If the s q u a r e of an integer is even, t h e n the integer is even. 16. If the s q u a r e of an integer is odd, t h e n the i n t e g e r is odd. 17. If the product of two integers is even, t h e n at least one of t h e m m u s t be an even integer. 18. If the product of two integers is odd, t h e n both m u s t be odd integers. Prove by contradiction, w h e r e p is a p r i m e n u m b e r . 19. j ~ is an irrational n u m b e r . 21. v/~ is an irrational n u m b e r .
20. v ~ is an i r r a t i o n a l n u m b e r . *22. log102 is an i r r a t i o n a l n u m b e r .
Prove by cases, w h e r e n is an a r b i t r a r y integer and Ixl denotes t h e absolute value of x.
1.5 ProofMethods
55
23. n 2 + n is a n e v e n i n t e g e r .
25. n 3  n is divisible by 3.
24. 2n 3 + 3n 2 + n is a n e v e n i n t e g e r .
( H i n t : A s s u m e t h a t e v e r y i n t e g e r is of t h e f o r m
3k, 3k + 1, or 3k + 2.)
26. ]  x [ = [ x [
27. [ x . y [ = [ x [ . [ y ]
28. [x +y] _< [x[ + [y[
P r o v e by t h e e x i s t e n c e m e t h o d . 29. T h e r e a r e i n t e g e r s x s u c h t h a t x 2 = x. 30. T h e r e a r e i n t e g e r s x s u c h t h a t [x] = x. 31. T h e r e a r e i n f i n i t e l y m a n y i n t e g e r s t h a t c a n be e x p r e s s e d as t h e s u m o f t w o c u b e s in t w o d i f f e r e n t ways. 32. T h e e q u a t i o n x 2 + y2 = z 2 h a s infinitely m a n y i n t e g e r s o l u t i o n s . Give a c o u n t e r e x a m p l e to d i s p r o v e e a c h s t a t e m e n t , w h e r e P(x) d e n o t e s a n arbitrary predicate. 33. T h e a b s o l u t e v a l u e of e v e r y real n u m b e r is positive. 34. T h e s q u a r e of e v e r y real n u m b e r is positive. 35. E v e r y p r i m e n u m b e r is odd. 36. E v e r y m o n t h h a s exactly 30 days. 37. (3x)P(x) ~
(3!x)P(x)
38. (~x)P(x) ~ (Vx)P(x) 39. F i n d t h e flaw in t h e following "proof": L e t a a n d b be real n u m b e r s s u c h t h a t a = b. T h e n a b = b 2. Therefore, a 2  ab = a 2  b 2 F a c t o r i n g , a ( a  b)  ( a + b ) ( a  b) C a n c e l a  b f r o m b o t h sides: a=a+b
Since a  b, t h i s yields a  2a. C a n c e l a f r o m b o t h sides. T h e n we get 1 = 2. L e t a, b, a n d c b e a n y real n u m b e r s . T h e n a < b if a n d only if t h e r e is a positive real n u m b e r x s u c h t h a t a + x  b. U s e t h i s fact to p r o v e each. 40. If a < b a n d b < c, t h e n a < c. ( t r a n s i t i v e 41. I f a < b ,
thena+c_ 3. D e t e r m i n e the t r u t h value of each. 57. (Vx)lP(x) A Q(x)l
58. (Vx)[P(x) v Q(x)]
59. (3y)[P(y) A Q(y)]
60. (Sz)lP(z)vQ(z)l
61. (Vx)[~P(x)]
62. (3z)[~Q(z)]
Prove each, where a, b, c, d, and n are any integers. 63. The product of two consecutive integers is even. 64. n 3 + n is divisible by 2. 65. n 4  n 2 is divisible by 3. 66. I f a < b a n d c < d , 67. I f a + b
thena+c 12, t h e n e i t h e r a > 6 o r b
>6.
68. I f a b  ac, t h e n either a  0 or b  c. [ H i n t : p > (qvr)  (p A~q) ~ r.] 69. If a 2  b 2, t h e n either a  b or a   b . [ H i n t : p > (q v r) 
( p A ~ q ) + r.]
70. Give a c o u n t e r e x a m p l e to disprove the following s t a t e m e n t : If n is a positive integer, t h e n n 2 + n + 41 is a prime n u m b e r . [Note: In 1798 the e m i n e n t F r e n c h m a t h e m a t i c i a n AdrienMarie Legendre (17521833) discovered t h a t the formula L ( n )  n 2 Zr n + 41 yields distinct primes for 40 consecutive values of n. Notice t h a t L ( n )  E (  n ) ; see Example 1.45. ] The propositions
Let p , q , t(r)0.5.
in Exercises
7181 are fuzzy logic.
and r be simple propositions with t ( p )

1, t ( q ) 
0.3,
and
C o m p u t e the t r u t h value of each, where s' denotes the negation of the s t a t e m e n t s. 71. p A ( q v r )
72. p V ( q A r )
73. ( p A q ) V ( p A r )
74. ( p V q ) A ( p V r )
75. p'
76. (p
A
q'
77. (p v q')' v q
v
q')
v
(p
A
q)
78. (p A q)' A (p V q)
79. Let p be a simple proposition with t ( p ) = x and p' its negation. Show t h a t t ( p v p') = 1 if and only if t ( p ) = 0 or 1. Let p and q be simple propositions with t ( p ) 0 a , t h e n x <  a o r x > a .
1. Iflxl < a , t h e n  a < x < a . Simplify each b o o l e a n expression.
3. (p
v
~q)
A
*5. (p
A
~q)
v
~(p (~p
A A
*4. [p v q v (~p A ~q)] v (p A ~q)
q)
q)
v
(~p
A
*6. (p V q) A "~(p A q) A (~p V q)
~q)
7. Let p  q a n d r  s. D e t e r m i n e i f p + (p A r)  q + (q A S). N e g a t e each proposition, w h e r e U D = set of real n u m b e r s .
9. (Vx)(Vy)(xy = yx)
8. (VX)(3y)(xy >__1)
11. (Vx)(3y)(3z)(x + y = z)
10. (Vx)(Vy)(3z)(x + y = z) P r o v e each.
12. T h e e q u a t i o n x 3 + y3 = z 3 h a s infinitely m a n y i n t e g e r solutions. "13. Let n be a positive integer. T h e n n(3n 4 + 7n 2 + 2) is divisible by 12. "14. Let n be a positive integer. T h e n n(3n 4 + 13n 2 + 8) is divisible by 24. "15. In 1981 O. H i g g i n s discovered t h a t t h e f o r m u l a h(x) = 9x 2 471x + 6203 g e n e r a t e s a p r i m e for 40 c o n s e c u t i v e v a l u e s of x. Give a c o u n t e r e x a m p l e to show t h a t not every value of h(x) is a p r i m e . "16. T h e f o r m u l a g ( x ) = x 2  2999x + 2248541 yields a p r i m e for 80 consecutive v a l u e s ofx. Give a c o u n t e r e x a m p l e to disprove t h a t every value o f g ( x ) is a prime. In a t h r e e  v a l u e d l o g i c , developed by t h e Polish logician J a n L u k a s i e w i c z (18781956), t h e possible t r u t h values of a p r o p o s i t i o n a r e 0, u, a n d 1, w h e r e 0 r e p r e s e n t s F, u r e p r e s e n t s u n d e c i d e d , a n d 1 r e p r e s e n t s T. T h e logical c o n n e c t i v e s A, V, ', ~, a n d o are defined as follows: A
0
v
U
0
!
u
0
0
0
0
0
u
0
1
u
0
u
u
u
u
u
u
1
0
u
1
1
1
1
0
+
0
u
0 u 1
1 u 0
1 1 u
0 u 1
0
u
1 u 0
u 1 u
Let p a n d q be a r b i t r a r y p r o p o s i t i o n s in a t h r e e  v a l u e d logic, w h e r e r' d e n o t e s t h e n e g a t i o n of s t a t e m e n t r a n d t(r) d e n o t e s t h e t r u t h value of r.
Chapter Summary
63
17. If t(p v p') = 1, show t h a t t(p) = 0 or 1. 18. Show t h a t p A q ~ p v q is a threevalued tautology. 19. Show t h a t (p + q) ~ (p' v q) is not a threevalued tautology. 20. Show t h a t (t9 ~ q) ~ (~q ~ ~p) is a threevalued tautology. 21. D e t e r m i n e if [p A (p ~ q)] ~ q is a threevalued tautology. Verify each. 22. (p A q)' =_p' v q'
[Hint: Show t h a t t((p A q)') = t(p') V t(q').]
23. (p V q)'  p' A q'
[Hint: Show t h a t t((p v q)') = t(p') A t(q').]
Computer Exercises Write a p r o g r a m to perform each task. C o n s t r u c t a t r u t h table for each proposition. 1. ( p v q ) A ~ q
2. p N A N D q
3. p N O R q
4. (p ~ q) ~ ( ~ p v q)
5. (p + q) ~ r
6. (p + q) a} is denoted by [a, oc) using the i n f i n i t y s y m b o l o~. Likewise, the set {x ~ R Ix < a} is denoted by (  ~ , a]. Next we present two interesting paradoxes related to infinite sets and proposed in the 1920s by the G e r m a n m a t h e m a t i c i a n David Hilbert.
The Hilbert Hotel Paradoxes Imagine a grand hotel in a major city with an infinite n u m b e r of rooms, all occupied. One m o r n i n g a visitor arrives at the registration desk looking for a room. "I'm sorry, we are full," replies the manager, "but we can certainly accommodate you." How is this possible? Is she contradicting herself?. To give a room to the new guest, Hilbert suggested moving the guest in Room 1 to Room 2, the guest in Room 2 to Room 3, the one in Room 3 to Room 4, and so on; Room 1 is now vacant and can be given to the new guest. The clerk is happy t h a t she can accommodate him by moving each guest one room down the hall. The second paradox involves an infinite n u m b e r of conventioneers arriving at the hotel, each looking for a room. The clerk realizes t h a t the hotel can make a fortune if she can somehow accommodate them. She knows she can give each a room one at a time as above, but t h a t will involve moving each guest constantly from one room to the next, resulting in total chaos and frustration. So Hilbert proposed the following solution: move the guest in Room 1 to Room 2, the guest in Room 2 to Room 4, the one in Room 3 to Room 6, and so on. This puts the old guests in evennumbered rooms, so the new guests can be checked into the oddnumbered rooms. Notice t h a t in both cases the hotel could accommodate the guests only because it has infinitely m a n y rooms.
2.1 The Concept of a Set
75
A third paradox: Infinitely m a n y hotels with infinitely m a n y rooms are leveled by an earthquake. All guests survive and come to Hilbert Hotel. How can they be accommodated? See Example 3.23 for a solution. We close this section by introducing a special set used in the study of formal languages. Every word in the English language is an a r r a n g e m e n t of the letters of the alphabet {A, B , . . . ,Z, a, b , . . . , z}. The alphabet is finite and not every a r r a n g e m e n t of the letters need make any sense. These ideas can be generalized as follows.
Alphabet A finite set Z of symbols is an alphabet. (E is the uppercase Greek letter sigma.) A w o r d (or s t r i n g ) o v e r E is a finite a r r a n g e m e n t of symbols from E. For instance, the only alphabet understood by a computer is the b i n a r y a l p h a b e t {0,1 }; every word is a finite and unique a r r a n g e m e n t of O's and l's. Every zip code is a word over the alphabet {0,... ,9}. Sets such as {a, b, c, ab, bc} are not considered alphabets since the string ab, for instance, can be obtained by juxtaposing, that is, placing next to each other, the symbols a and b.
Length of a Word The l e n g t h of a word w, denoted by llwli, is the n u m b e r of symbols in it. A word of length zero is the e m p t y w o r d (or the null w o r d ) , denoted by the lowercase Greek letter ~ ( l a m b d a ) ; It contains no symbols. For example, llabll = 2, llaabba]l  5, and IIs = 0. The set of words over an alphabet E is denoted by Z*. The empty word belongs to Z* for every alphabet Z. In particular, if Z denotes the English alphabet, then Z* consists of all words, both meaningful and meaningless. Consequently, the English language is a subset of Z*. More generally, we make the following definition.
Language A l a n g u a g e over an alphabet Z is a subset of E*. The following two examples illustrate this definition. The set of zip codes is a finite language over the alphabet E  { 0 , . . . , 9}. m Let E  {a, b}. Then E*  {~, a, b, aa, ab, ba, bb, aaa, aab, aba, abb, b a a , . . . }, an infinite set. Notice that {aa, ab, ba, bb} is a finite language over E, whereas {a, aa, aba, bab, aaaa, a b b a , . . . } is an infinite language, m Words can be combined to create new words, as defined below.
Chapter2 The Language of Sets
76 Concatenation
The c o n c a t e n a t i o n of two words x a n d y over an a l p h a b e t , d e n o t e d by x y , is obtained by a p p e n d i n g t h e word y at t h e end of x. T h u s if x = x l . . . X m a n d y = Yl...Yn, xy  Xl . . .XmYl . . .Yn. For example, let E be t h e E n g l i s h alphabet, x = CAN, a n d y = ADA; t h e n x y = CANADA. Notice t h a t c o n c a t e n a t i o n is n o t a c o m m u t a t i v e operation; t h a t is, x y 7/= y x . It is, however, associative; t h a t is, x ( y z ) = ( x y ) z = x y z . Two i n t e r e s t i n g p r o p e r t i e s are satisfied by t h e c o n c a t e n a t i o n operation: 9 The c o n c a t e n a t i o n of a n y word x with )~ is itself; t h a t is, ~x  x  x~ for every x e E*. 9 Let
x,y
~
E*. T h e n
[]xY]l 
][xll +
ilYi]. (See Section 5.1 for a proof.)
For example, let E = {a,b }, x = aba, and y  bbaab. T h e n x y  a b a b b a a b and [[xyl[  8  3 + 5  IIxl[ + IlyJl. A useful notation: As in algebra, t h e e x p o n e n t i a l n o t a t i o n can be employed to e l i m i n a t e the r e p e a t i n g of symbols in a word. Let x be a symbol and n an i n t e g e r >_ 2; t h e n x n denotes the c o n c a t e n a t i o n x x . . . x to n  1 times. U s i n g this compact notation, the words a a a b b a n d a b a b a b can be a b b r e v i a t e d as a3b 2 a n d (ab) 3, respectively. Notice, however, t h a t (ab) 3 = ababab ~: a3b 3  aaabbb.
Exercises 2.1 Rewrite each set using the listing method. 1. The set of m o n t h s t h a t begin with the l e t t e r A. 2. The set of letters of the word GOOGOL. 3. The set of m o n t h s with exactly 31 days. 4. The set of solutions of t h e e q u a t i o n x 2  5x + 6  0. Rewrite each set u s i n g the setbuilder notation. 5. The set of integers b e t w e e n 0 and 5. 6. The set of J a n u a r y , F e b r u a r y , May, a n d July. 7. The set of all m e m b e r s of t h e U n i t e d Nations. 8. {Asia, Australia, Antarctica} D e t e r m i n e if t h e given sets are equal. 9.
{x,y,z},
{x,z,y}
11.
{xlx 2 
x},
10.
{xlx 2
= 1}, {xlx 2  x}
12. {x, {y}}, {{x},y}
{0, 1}
M a r k each as t r u e or false.
13. a E {alfa}
14. b _ {a,b,c}
15. {x} _ {x,y, z}
2.1 The Concept of a Set
77
16. { 0 }  0
17. 0 ~ 0
18. { 0 }  0
19. {0} = 0
20. 0 __ 0
21. 0 ~ {0}
22. {xlx ~ x }  0
23. {x,y}  {y,x}
24. {x} ~ {{x},y}
25. 0 is a subset of every set.
26. E v e r y set is a subset of itself.
27. Every n o n e m p t y set has at least two subsets. 28. The set of people in the world is infinite. 29. The set of words in a dictionary is infinite. Find the power set of each set. 30. 0
31. {a}
32. {a,b,c}
33. Using Exercises 3032, predict the n u m b e r of subsets of a set with n elements. In Exercises 3437, n denotes a positive integer less t h a n 10. Rewrite each set using the listing method. 34. {nln is divisible by 2}
35. {nln is divisible by 3}
36. {nln is divisible by 2 and 3}
37. {nln is divisible by 2 or 3}
Find the family of subsets of each set t h a t do not contain consecutive integers. 38. {1,2}
39. {1,2,3}
40. Let an denote the n u m b e r of subsets of the set S  {1, 2 , . . . , n} t h a t do not contain consecutive integers, where n > 1. F i n d a 3 and a4. In Exercises 4146, a language L over E  {a, b} is given. Find five words in each language. 41. L  {x e E ' I x begins with and ends in b.} 42. L  {x ~ E*lx contains exactly one b. } 43. L  {x E E*fx contains an even n u m b e r of a's. } 44. L  {x e E ' I x contains an even n u m b e r of a ' s followed by an odd n u m b e r of b's. } C o m p u t e the length of each word over {a, b }. 45. aab
46. aabbb
47. ab 4
48. a3b 2
A r r a n g e the b i n a r y words of the given length in increasing order of magnitude. 49. L e n g t h two.
50. L e n g t h three.
Chapter 2 The Language of Sets
78
A t e r n a r y w o r d is a word over the alphabet {0, 1, 2}. A r r a n g e the t e r n a r y words of the given length in increasing order of magnitude. 51. Length one.
52. Length two.
Prove each. *53. The empty set is a subset of every set. (Hint: Consider the implication x e ~ ~ x e A.) *54. The empty set is unique. (Hint: Assume there are two empty sets, 01 and ~2. T h e n use Exercise 53.) *55. Let A, B, and C be a r b i t r a r y sets such t h a t A c B and B c C. T h e n
AcC. (transitive property) *56. If E is a n o n e m p t y alphabet, t h e n E* is infinite. (Hint: Assume Z* is finite. Since Z # 0, it contains an e l e m e n t a. Let x e E* with largest length. Now consider xa.)
J u s t as propositions can be combined in several ways to construct new propositions, sets can be combined in different ways to build new sets. You will find a close relationship between logic operations and set operations.
Union The u n i o n of two sets A and B, denoted by A u B, is obtained by merging them; t h a t is, A u B  {xl(x e A) v (x e B)}. Notice the similarity between union and disjunction.
~
Let A  {a, b, c}, B  {b, C, d , e } , a n d C  { x , y } . T h e n A u B  { a , b ,
BUAandBUC
{b,c,d,e,x,y}CUB.
C, d , e } 
m
The shaded areas in Figure 2.4 r e p r e s e n t the set A u B in t h r e e different cases.
Intersection The i n t e r s e c t i o n of two sets A and B, denoted by A N B, is the set of elements common to both A and B; t h a t is, A 5 B  {xl(x e A) v (x e B)}.
79
2.2 Operationswith Sets F i g u r e 2.4
U
A UB
U
AUB=B
A U B, where A and B are disjoint
Notice the relationship between intersection and conjunction.
Let A {Nov, Dec, Jan, Feb}, B  {Feb, Mar, Apr, May}, and C  {Sept, Oct, Nov, Dec}. T h e n A n B  {Feb}  B N A a n d B n C  0  C A B . (Notice t h a t B and C are disjoint sets. More generally, two sets are disjoint if and only if their intersection is null.) m
F i g u r e 2.5
O9 O9
Berkeley Street intersection
Figure 2.5 shows the intersection of two lines and t h a t of two streets, and Figure 2.6 displays the set A n B in three different cases.
F i g u r e 2.6
U
U
G ANB
Let A  {a, b, c, d, g}, B and (A u B) N (A U C).
ANB=O
{b, c, d, e, f}, and C 
ANB=A
{b,c,e,g,h}.FindAu(BnC)
Chapter 2 The Language of Sets
80
SOLUTION: (1)
BNC
= {b,c,e} {a,b, c, d, e, g}
AU(BNC)
A UB = { a , b , c , d , e , f,g}
(2)
AuC{a, (AuB) N(AuC)
b, c, d, e, g, h} {a,b,c,d,e,g}
= A u (B n C) See the Venn diagram in Figure 2.7. F i g u r e 2.7
m A third way of combining two sets is by finding their difference, as defined below. Difference The d i f f e r e n c e of two sets A and B (or the r e l a t i v e c o m p l e m e n t of B in A), denoted by A  B (notice the order), is the set of e l e m e n t s in A t h a t are not in B. T h u s A  B = {x ~ AIx r B}. L e t A  { a , . . . , z , 0 , . . . , 9 } , and B  {0,...,9}. T h e n A  B  { a , . . . , z } and BA=O. The shaded areas in Figure 2.8 r e p r e s e n t the set A  B in t h r e e different cases.
F i g u r e 2.8
U
U
AB
A  B =A
AB
m F o r any set A r U, a l t h o u g h A  U  ~, the difference U  A ~: ~. This shows yet a n o t h e r way of obtaining a new set.
2.2 Operationswith Sets
81
Complement T h e difference U  A is the (absolute) c o m p l e m e n t of A, d e n o t e d by A' (A prime). T h u s A ' = U  A = {x ~ U Ix r A}. I F i g u r e 2.9 r e p r e s e n t s t h e c o m p l e m e n t of a set A. ( C o m p l e m e n t a t i o n c o r r e s p o n d s to negation.)
F i g u r e 2.9
U
A'
~
{ a , . . . , Z }. F i n d the c o m p l e m e n t s of the sets A  {a, e, i, O~U} a n d Let U B = {a, c, d, e, . . . , w}. T h e n A' = U  A = set of all c o n s o n a n t s in t h e alphabet, and B' = U  B = {b, x, y, z}. m Let A = { a , b , x , y , z } , B F i n d (A u B)' and A' N B'. SOLUTION: (1)
{c, d, e, x, y, z}, and U 
{a,b,c,d,e,w,x,y,z}.
A U B = {a, b, c, d, e, x, y, z} (A u B)' = {w}
(2)
A' = {c, d, e, w}
B' = {a, b, w} A' N B' = {w} = (A U B)' See F i g u r e 2.10.
F i g u r e 2.10
U
m Since as a rule, A  B r B  A , new set.
by t a k i n g t h e i r u n i o n we can form a
82
Chapter2 The Language of Sets Symmetric Difference The s y m m e t r i c d i f f e r e n c e of A and B, denoted by A @ B, is defined by A @ B  (A  B) U (B  A ) . LetA  {a,...,z,0,...,9} andB  {0,...,9,+,,.,/}. ThenAB { a , . . . , z } and B  A = {+,,.,/}.
[email protected]  (AB) u (BA) { a , . . . , z, + ,  , . , / } .
a
The s y m m e t r i c difference of A and B is pictorially displayed in Figure 2.11 in three different cases.
Figure 2.11
A
AOB=AUB
A
=AB
Set and Logic Operations Set operations and logic operations are closely related, as Table 2.1 shows.
Table 2.1
Set operation AuB ANB A'
[email protected] Logic operation pvq pvq ~p
pXORq
The i m p o r t a n t properties satisfied by the set operations are listed in Table 2.2. (Notice the similarity between these properties and the laws of logic in Section 1.2.) We shall prove one of them. Use its proof as a model to prove the others as routine exercises. We shall prove law 16. It uses De M o r g a n ' s law in symbolic logic, a n d the fact t h a t X = Y if and only if X ___ Y and Y __C_X.
PROOF: In order to prove t h a t (AUB)'  A' NB', we m u s t prove two parts" (A UB)' c A' N B' and A' n B' c (A u B)'. 9 To prove t h a t (A u B)' c_ (A' n B')Let x be an a r b i t r a r y element of (A u B)'. T h e n x r (A u B). Therefore, by De M o r g a n ' s law, x r A and x r B; t h a t is, x e A' and x e B'. So x e A' NB'. T h u s every element of (A UB)' is also an element ofA' NB'; t h a t is, (A u B)' ___A' n B'.
83
2.2 Operations with Sets
T a b l e 2.2
Laws of Sets Let A, B, and C be any three sets and U the universal set. Then:
I d e m p o t e n t laws 2. A n A = A
1. A u A = A
I d e n t i t y laws 4. A n U = A
3. A u O = A
Inverse laws
6. A n A I = O
5. A u W = U
D o m i n a t i o n laws 8. A n O = O
7. A u U = U
C o m m u t a t i v e laws 10. A n B = B n A
9. A u B = B u A
Double c o m p l e m e n t a t i o n law 11. (At)t = A
A s s o c i a t i v e laws 12. A u ( B u C )  ( A u B ) u C
13. A n ( B n C )  ( A n B ) n C
D i s t r i b u t i v e laws 14. A u ( B n C ) = ( A u B ) n ( A u C )
15. A n ( B u C )  ( A n B ) u ( A n C )
De Morgan's laws 16. (A u B)'  A' n B'
17. (A n B)'  A' u B'
A b s o r p t i o n laws 18. A u ( A n B )  A
19. A n ( A u B ) = A (Note: The following laws have no names.)
20. I f A c _ B , t h e n A n B = A .
21. I f A c _ B , t h e n A u B = B .
22. I f A c _ B , t h e n B tC_A t.
23. A  B  A n B
t
24. A 
9 To p r o v e t h a t A' c~ B' c_ (A u B ) " Let x be a n y e l e m e n t of A' n B'. T h e n x E A' a n d x ~ B'. T h e r e f o r e , x r A a n d x r B. So, by De M o r g a n ' s law, x r (A u B). C o n s e q u e n t l y , x ~ (A w B)'. T h u s , since x is a r b i t r a r y , A' • B' c (A u B)'. T h u s , (A u B)'  A' n B'. See t h e V e n n d i a g r a m s in F i g u r e 2.12 also.
Note" L a w 23 is a v e r y u s e f u l r e s u l t a n d will be u s e d in t h e n e x t section.
A few words of explanation" T h e c o m m u t a t i v e laws i m p l y t h a t t h e o r d e r in w h i c h t h e u n i o n (or i n t e r s e c t i o n ) of t w o sets is t a k e n is i r r e l e v a n t . T h e associative laws i m p l y t h a t w h e n t h e u n i o n (or i n t e r s e c t i o n ) of t h r e e or m o r e
84
Chapter 2 The Language of Sets
F i g u r e 2.12
(A U B)' = shaded area
A' n B' = crossshaded area
I
sets is taken, the way the sets are grouped is immaterial; in other words, such expressions without p a r e n t h e s e s are perfectly legal. For instance, A U B u C  A u (B U C) = (A u B) U C is certainly valid. The two De M o r g a n ' s laws in propositional logic play a central role in deriving the corresponding laws in sets. Again, as in propositional logic, p a r e n t h e s e s are essential to indicate the groupings in the distributive laws. For example, if you do not parenthesize the expression A N (B U C) in law 15, t h e n the LHS becomes A N B U C = (A N B ) U C = (A U C) N (B U C) r (A N B ) U (A N C).
Notice the similarity between the set laws and the laws of logic. For example, properties 1 t h r o u g h 19 and 22 have their c o u n t e r p a r t s in logic. Every corresponding law of logic can be obtained by replacing sets A, B, and C with propositions p, q, and r, respectively, the set operators N, U, a n d ' with the logic operators A, v, and ~ respectively, and equality (=) with logical equivalence (=_). Using this procedure, the absorption law A w (A N B) = A, for instance, can be t r a n s l a t e d as p v (p A q)  p, which is the corresponding absorption law in logic. J u s t as t r u t h tables were used in Chapter I to establish the logical equivalence of compound statements, they can be applied to verify set laws as well. The next example illustrates this method. Using a t r u t h table, prove t h a t (A u B)'  A' N B'. SOLUTION. Let x be an a r b i t r a r y element. T h e n x may or may not be in A. Likewise, x may or may not belong to B. E n t e r this information, as in logic, in the first two columns of the table, which are headed by x E A and x e B. The table needs five more columns, headed by x ~ (A u B), x ~ (A u B)', x ~ A', x ~ B', and x ~ (A' n B') (see Table 2.3). Again, as in logic, use the entries in the first two columns to fill in the r e m a i n i n g columns, as in the table.
85
2.2 Operations with Sets
T a b l e 2.3
x e A
x e B
x e (A U B )
x e (A u B)'
x e A'
x 9 B'
x 9 (A' n B')
T T F F
T F T F
T T T F
F F F T
F F T T
F T F T
F F F T
Note: The shaded columns are identical Since the columns headed by x e (A u B)' and x e (A' n B') are identical, it follows t h a t (A u B)' = A' n B'. I Using t r u t h tables to prove set laws is purely mechanical a n d elem e n t a r y . It does not provide any insight into the d e v e l o p m e n t of a m a t h e m a t i c a l proof. Such a proof does not build on previously k n o w n set laws, so we shall not r e s o r t to such proofs in s u b s e q u e n t discussions. .
.
.
.
.
.
J u s t as the laws of logic can be used to simplify logic expressions a n d derive new laws, set laws can be applied to simplify set expressions a n d derive new laws. In order to be successful in this art, you m u s t k n o w the laws well and be able to apply t h e m as needed. So, practice, practice, practice.
Using set laws, verify t h a t (X  Y)  Z = X  (Y u Z). PROOF(X  Y)  Z  (X  Y) n Z'
AB
=AnB'
= (X n Y') n Z'
AB
=AnB'
= X n (Y' n Z')
associative law 13
= X n (Y u Z)'
De M o r g a n ' s law 16
=X(YuZ)
AB
=AnB'
Simplify the set expression (A n B') U (A' n B) U (A' n B'). SOLUTION: (You m a y supply the justification for each step.) (A n B') u (A' n B) u (A' n B')  (A n B') u [(A' n B) u (A' n B')] = ( A n B') u [A' n (B u B')] = ( A n B') u (A' n U) = (A riB') u A '
I
(Jhapter2 The Languageof Sets
86
= A' u (A N B') = (A' u A) n (A' u B') = U n (A' u B')
=A'uB'
m
Often subscripts are used to n a m e sets, so we now t u r n o u r a t t e n t i o n to such sets.
Indexed Sets Let I, called the i n d e x s e t , be the set of subscripts i used to n a m e the sets Ai. T h e n the u n i o n of the sets Ai as i varies over I is d e n o t e d b y . u Ai. Similarly, tEI
n Ai denotes the i n t e r s e c t i o n of the sets Ai as i r u n s over I. In p a r t i c u l a r ,
iEI
let I  {1, 2 , . . . , n }. T h e n u Ai  A1 uA2 u . . . UAn, which is often w r i t t e n as n U
i=1
iEI
n
Ai or simply U1 Ai. Likewise, N
iEI
Ai 
n n Ai i=I cx.)
I f I  N, the expression
n ? Ai  A1 n A2 N...
NAn.
('x.)
u Ai is w r i t t e n as u Ai  ~ Ai, u s i n g the infinity iEb~
/el
symbol oc; similarly, i ?b~ A i  i=ln A i  N1 A i . Before we proceed to define a new b i n a r y o p e r a t i o n on sets n , we define an o r d e r e d set. i=1
Ordered Set Recall t h a t the set {al, a 2 , . . . , a , } is an u n o r d e r e d collection of elements. Suppose we assign a position to each element. T h e r e s u l t i n g set is an o r d e r e d s e t with n e l e m e n t s or an n  t u p l e , d e n o t e d by ( a l , a 2 , . . . , an). (Notice the use of p a r e n t h e s e s versus braces.) The set (al, a2) is an o r d e r e d pair. Two ntuples are e q u a l if and only if t h e i r c o r r e s p o n d i n g e l e m e n t s are equal. T h a t is, (al, a 2 , . . . , a,,) = (bl, b 2 , . . . , b,) if a n d only if ai = bi for every i.
•
Every n u m e r a l and word can be considered an ntuple. F o r instance, 345 = (3, 4, 5) l
t
$
ones tens hundreds
c o m p u t e r = (c, o, m, p, u, t, e, r) 1001011 = (1, 0, 0, 1, 0, 1, 1) 11010010 = (1, 1, 0, 1, 0, 0, 1, 0)
ASCII* code for letter K EBCDIC** code for letter K
*American Standard Code for Intbrmation Interchange. **Extended Binary Coded Decimal Interchange Code.
m
87
2.2 Operationswith Sets
Rend D e s c a r t e s (15961650) was born near Tours, France. At eight, he entered the Jesuit school at La Fleche, where because of poor health he developed the habit of lying in bed thinking until late in the morning; he considered those times the most productive. He left the school in 1612 and moved to Paris, where he studied mathematics for a brief period. After a short military career and travel through Europe for about 5years, he returned to Paris and studied mathematics and philosophy. He then moved to Holland, where he lived for 20years writing several books. In 1637 he wrote Discours, which contains his contributions to analytic geometry. In 1649 Descartes moved to Sweden at the invitation of Queen Christina. There he contracted pneumonia and died.
~Z ( .::
We are now ready to define the next and final operation on sets.
Cartesian Product The c a r t e s i a n p r o d u c t of two sets A and B, denoted by A x B, is the set of all ordered pairs (a,b) with a 9 A and b 9 B. T h u s A x B = {(a,b)l a 9 A A b 9 B}.A x A is denoted b y A 2. It is n a m e d after the F r e n c h philosopher and m a t h e m a t i c i a n Ren~ Descartes. Let A 
{a, b } and B 
{x, y, z}. T h e n
A x B = {(a, x), (a, y), (a, z), (b, x), (b, y), (b, z)} B z A = {(x, a), (x, b), (y, a), (y, b), (z, a), (z, b)} A 2  A x A = {(a, a), (a, b), (b, a), (b, b)}
m
(Notice t h a t A x B r B x A.)
The various elements of A x B in Example 2.22 can be displayed in a r e c t a n g u l a r fashion, as in F i g u r e 2.13, and pictorially, using dots as in Figure 2.14. The circled dot in row a and column y, for instance, r e p r e s e n t s the element (a, y). The pictorial r e p r e s e n t a t i o n in F i g u r e 2.14 is the g r a p h ofA x B .
F i g u r e 2.13
a Elements of A b
(a, x)
(a, y)
(a, z)
(b, x)
(b, y)
(b, z)
x
y Elements of B
z
88
Figure
2.14
Pictorial representation ofA •
Chapter 2
The Language of Sets
a
9

9
x
y
z
Figure 2.15 shows the graph of the infinite set 1~ 2   1~ x N. The circled dot in column 4 and row 3, for instance, represents the element (4,3). The horizontal and vertical dots indicate that the pattern is to be continued indefinitely in both directions. Figure
2.15
9
9
9
1
2
3
},
4
.
.
.
More generally, R 2   R • R consists of all possible ordered pairs (x,y) of real numbers. It is represented by the familiar x y  p l a n e or the c a r t e s i a n p l a n e used for graphing (see Figure 2.16).
Figure
2.16
The cartesian plane R 2"
(3,4)
(0,3)
(5,0) The following example presents an application of cartesian product. ~
Linda would like to make a trip from Boston to New York and then to London. She can travel by car, plane, or ship from Boston to New York, and by plane or ship from New York to London. Find the set of various modes of transportation for the entire trip. SOLUTION:
Let A be the set of means of transportation from Boston to New York and B the set from New York to London. Clearly A  {car, plane, ship} and B  {plane, ship}. So the set of possible modes of transportation is given by
89
2.2 Operationswith Sets F i g u r e 2.17
London N Boston
e
plane
w
~
.~"~'~ship
A x B  {(car, plane), (car, ship), (plane, plane), (plane, ship), (ship, plane), (ship, ship)}. See Figure 2.17. I The definition of the product of two sets can be extended to n sets. The c a r t e s i a n p r o d u c t o f n s e t s A 1 , A 2 , . . . ,An consists of all possible n tuples (al, a 2 , . . . , an), where ai ~ Ai for every i; it is denoted by A1 x A2 x ... x An. If all Ai's are equal to A, the product set is denoted by An. LetA
{x},B  {y,z}, and C 
A x B x C
{1,2,3}. Then
{(a,b,c)la ~ A , b ~ B, a n d c ~ C}
= {(x, y, 1), (x, y, 2), (x, y, 3), (x, z, 1), (x, z, 2), (x, z, 3)} Finally, take a look at the map of the continental United States in Figure 2.18. It provides a geographical illustration of partitioning, a concept that can be extended to sets in an obvious way.
F i g u r e 2.18
I
Partition
Consider the set S = {a, b, c, d, e, f, g, h, i} and the subsets $1 = {a, b }, $ 2  {c}, $3 = {d, e, f}, $4  {g, h}, and $5  {i}. Notice t h a t these subsets have three interesting properties: (1) They are nonempty; (2) they are pairwise disjoint; that is, no two subsets have any common elements; (3) their union
90
Chapter 2 The Language of Sets is S. (See Figure 2.19.) T h e set P  { S 1 , $ 2 , $ 3 , $ 4 , $ 5 } of S.
is called a p a r t i t i o n
F i g u r e 2.19
S1
More generally, let I be an index set and P a family of s u b s e t s Si of a n o n e m p t y set S, w h e r e i ~ I. T h e n P is a p a r t i t i o n of S if: 9 Each set Si is n o n e m p t y . 9 The subsets are pairwise disjoint; t h a t is, Si n S j  ~,~ if i ~=j. 9 The union of the subsets Si is S; t h a t is, u Si = S. i~I
(Each subset Si is a b l o c k of the partition.) T h u s a p a r t i t i o n of S is a collection of n o n e m p t y , pairwise disjoint subsets of S whose u n i o n is S. ~
Let Z,. denote the set of integers which, w h e n divided by 5, leave r as the r e m a i n d e r . T h e n 0 < r < 5 (see Section 4.1): w
Z0 = { . . . ,  5 , 0 , 5 , . . . } Zl = { . . . ,  4 , 1 , 6 , . . . } Z 2  { . . . ,  3 , 2 , 7,...} Z3 = { . . . ,  2 , 3 , 8 , . . . } Z4 = { . . . ,  1 , 4 , 9 , . . . } P = {Z0, Zl, Z2, Z3, Z4} is a partition of the set of integers. See F i g u r e 2.20. (This example is discussed in more detail in Section 7.4.) m
F i g u r e 2.20 Set of integers Z.
2.2 Operationswith Sets
91
The sports pages of newspapers provide fine examples of partitions, as the next example illustrates. ~
In 2003, the set of teams S in the National Football League was divided into two conferences, American and National, and each conference into four divisions m East, South, North, and West. Let El, $1, N1, and W1 denote the set of teams in East, South, North, and West Divisions in the American Conference, respectively, and E2, $2, N2, and W2 the corresponding sets in the National Conference. Then: E1  {Buffalo, Miami, New England, NY Jets} $1  {Indianapolis, Tennessee, Houston, Jacksonville} N1  {Baltimore, Cincinnati, Cleveland, Pittsburgh} W1  {Denver, Kansas City, Oakland, San Diego} E2 = {Washington, Philadelphia, Dallas, NY Giants} $2  {Atlanta, Tampa Bay, Carolina, New Orleans} N2 = {Chicago, Detroit, Minnesota, Green Bay } W2 = {Arizona, Seattle, St. Louis, San Francisco } Clearly, P  {E1,S1,N1, W 1 , E 2 , S 2 , N 2 , W2} is a partition of S. We close this section with a brief introduction to fuzzy sets.
Fuzzy Sets (optional) Fuzzy sets, a generalization of ordinary sets, were introduced in 1965 by Lotfi A. Zadeh of the University of California at Berkeley. They have applications to h u m a n cognition, communications, decision analysis, psychology, medicine, law, information retrieval, and, of course, artificial intelligence. Like fuzzy logic, they model the fuzziness in the natural language for example, in terms like young, healthy, wealthy, and beautiful. In fuzzy set theory, every element x in the universal set U has a certain degree of membership du(x), where 0 < du(x) < 1;du(x) indicates the degree of fuzziness. Accordingly, a fuzzy set S is denoted by listing its elements along with their degrees of membership; an element with zero degree of membership is not listed. For example, let Ube the fuzzy set of wealthy people and S  {Tom 0.4, Dick 0.7, Harry 0.6}. Then Harry belongs to S with degree of membership 0.6; ds(Harry)  0.6 measures Harry's degree of wealthiness. The concept of an ordinary subset can be extended to fuzzy sets also.
Fuzzy Subset Let A and B be fuzzy sets. Then A is a f u z z y s u b s e t of B if A ___B and dA(x) < dB(x) for every element x in A.
Chapter2 The Language of Sets
92
~
":t .... ."'
" ....
Lotfi A. Z a d e h (1921) was born in Baku, Azerbaijan. An alumnus of the University of Tehran (1942) and the Massachusetts Institute of Technology (1946), he received his Ph.D. from Columbia University in 1949 for his dissertation on frequency analysis of timevarying networks. He began his professional career in the Department of Electrical Engineering at Columbia. In 1959, he joined the Department of Electrical Engineering and Computer Science at the University of California, Berkeley, serving as its chair during the years 19631968. Currently, he is a professor at Berkeley and Director of Berkeley Initiative in Soft Computing. Zadeh's earlier "work was centered on systems analysis, decision analysis, and information systems. Since then his current research has shifted to the theory of fuzzy sets and its applications to artificial intelligence (AI). His research interest now is focused on fuzzy logic, soft computing, computing with words, and the newly developed computational theory of perceptions and precisiated natural language, "according to the University of California Web site. A truly gifted mind and an expert on AI, Zadeh has authored about 200journal articles on a wide variety of subjects relating to the conception, design, and analysis of information~intelligent systems. He serves on the editorial boards of more than 50 journals and on the advisory boards of a number of institutions related to AI. Zadeh is a recipient of numerous awards and medals, including the IEEE Education Medal, IEEE Richard W. Hamming Medal, IEEE Medal of Honor, the A S M E Rufus Oldenburger Medal, B. Bolzano Medal of the Czech Academy of Sciences, Kampe de Feriet Medal, AACC Richard E. Bellman Central Heritage Award, the Grigore Moisil Prize, Honda Prize, Okawa Prize, AIM Information Science Award, IEEESMC J. P. Wohl Career Achievement Award, SOFT Scientific Contribution Memorial Award of the Japan Society for Fuzzy Theory, IEEE Millennium Medal, and the ACM 2000 Allen Newell Award. He has received honorary doctorates from many universities from around the world. 9
2
"~
For example, letS = {Betsey 0.6, Mat 0.5} and T  {Betsey 0.8, J o n a t h a n 0.3, Mary 0.5, Mat 0.7} by fuzzy sets of smart people. Then S is a fuzzy subset of T. Operations on ordinary sets can be extended to fuzzy sets as well. Operations on Fuzzy Sets Let A and B be any fuzzy set. The u n i o n of A and B is AUB, where dA uB(x) = max{dA(x), ds(x)}; their i n t e r s e c t i o n is A N B, where dANB(X) = m i n { d A ( x ) , d s ( x ) } ; and the c o m p l e m e n t of A is A', where dA,(X) = 1  dA(x); in A' only the degrees of membership change. Using the sets S and T above, S u T = {Betsey 0.8, J o n a t h a n 0.3, Mary 0.5, Mat 0.7 } S n T = {Betsey 0.6, Mat 0.5} S' = {Betsey 0.4, Mat 0.5} Additional opportunities to practice the various operations are given in the exercises.
2.2
Operations with Sets
93
Exercises 2.2
L e t A = {a, e, f, g,i}, B = {b, d, e, g, h}, C = {d, e, f, h, i}, and U = { a , b , . . . ,k}. Find each set. 1. C l
2. B Q C '
3. C Q A '
4. ( A U B ) '
5. (B n C)'
6. (A u C')'
7. (B N C')'
8. A ~ B
9. ( A  B )  C
10. A  ( B  C )
11. (A u B)  C
12. (A N B)  C
Using the Venn d i a g r a m in Figure 2.21 find each set. 13. ( A U B ) N C
14. A n (B U C)
15. A 
16. ( A @ B ) U C
17. A Q ( B O C )
18. A  ( B @ C )
F i g u r e 2.21
(BC)
U a
A w
f
Let A = {b, c}, B = {x}, and C = {x,z}. Find each set. 19. A x B
20. B x A
21. A x 0
22. A x B x O
23. A x ( B u C )
24. A x ( B N C )
25. A x B x C
26. A x C x B
M a r k each as true or false, where A, B, and C are a r b i t r a r y sets and U the universal set. 27. A  ~ ) = A
28. 0  A =  A
29. O  O = 0
30. A  A = O
31. A  B = B  A
32. A  A ' = O
33. (A')' = A
34. (A N B ) ' = A' NB'
35. (A U B)' = A' U B'
36. A c A u B
37. A c A N B
38. B D (A  B ) = O
Give a counterexample to disprove each proposition. 39. ( A  B )  C = A  ( B  C )
40. A U ( B  C ) = ( A U B )  ( A U C )
41. A u ( B @ C ) = ( A u B ) G ( A u C )
42. A O ( B N C ) = ( A O B ) N ( A G C )
D e t e r m i n e if each is a partition of the set { a , . . . , z, 0 , . . . , 9}. 43. { { a , . . . , z } , { 0 , . . . , 9 } , 0 } 44. { { a , . . . , j } , { i , . . . , t } , { u , . . . , z } , { 0 , . . . , 9 } }
94
Chapter 2 The Language of Sets 45. {{a,... ,1}, { n , . . . , t } , {u,...,z}, {0,...,9}} 46. {{a,...,u}, {v,...,z}, {0,3}, { 1 , 2 , 4 , . . . , 9 } Prove each, where A, B, and C are any sets. 47. (A')' = A
48. A u (A n B ) = A
49. A N ( A u B ) = A
50. (A N B)' = A' u B'
51. A @ A = O
52. A @ U = A '
53. A @ B = B @ A
54. A  B = A A B '
55. (A u B u C)' = A' n B' n C'
56.
(A A B n C)' = A' u B' u C'
Simplify each set expression. 57. A A (A  B)
58. (A  A') u (B  A)
59.
(A  B')  (B  A')
60.
(A u B) u (A A B')'
61.
(A u B)  (A n B)'
62.
(A U B)' N (A n B')
63.
(A n B)' u (A u B')
64.
(A u B')' A (A' c~ B)
65.
(A' u B')' u (A' ~ B)
*66. State De Morgan's laws for sets Ai, i ~ I. (I is an index set.) *67. State the distributive laws using the sets A and Bi, i ~ I. The s u m of two fuzzy sets A and B is the fuzzy set A  B, where dA ~ B(X) = 1A IdA (x) + d B ( x ) l; their d i f f e r e n c e is the fuzzy set A  B , where dAB(X) = 0 v IdA(x)  dB(x)i; and their c a r t e s i a n p r o d u c t is the fuzzy set A x B, where dA xB(X,Y) = dA(x) AdB(x). Use the fuzzy s e t s A = {Angelo 0.4, Bart 0.7, Cathy 0.6} and B = {Dan 0.3, Elsie 0.8, F r a n k 0.4} to find each fuzzy set. 68. A U B
69. A A B
70. A'
71. A u B '
72. A N B '
73. A ~ A '
74. A 
75. A  B
76. B  A
77. A x B
78. B x A
79. A x A
Let A and B be any fuzzy sets. Prove each. *80.
(A u B)' = A' • B'
"81.
(A • B)' = A' u B'
Sets and the various set operations can be implemented in a c o m p u t e r in an elegant manner. Computer Representation
Although the elements of a set have no i n h e r e n t order, when the set is represented in a computer, an order is imposed upon t h e m to p e r m i t
2.3 ComputerOperations with Sets (optional)
95
implementation. The universal set U with n elements is represented as an array with n cells, each containing a 1: n1
2
U ] 1[ 1]
...
1
0
]1111111]
The elements are represented by the binary digits (or bits) 0 and 1 in the righttoleft fashion. Subsets of U are represented by assigning appropriate bits to the various cells. A bit 1 in a cell indicates the corresponding element belongs to the set, whereas a 0 would indicate the element does n o t belong to the set. ~
UsingU={a,b,...,h},representthesetsA={a,b,g}andB{c, 8bit strings.
e, h } a s
SOLUTION: Remember, the elements are represented in the righttoleft order. Thus:
h
g
f
e
d
c
b
a
lllllrll ll llll AlOlllOlOlOlOlllll lllOlOlllOlllOlOl
m
Next we discuss how the various subsets of a finite set can be found methodically. T a b l e 2.4
Subset
Bit String
0 {x} {y} {x,y} {z} {x,z} {y,z} {x,y,z}
000 001 010 011 100 101 110 111
Interestingly enough, there is a close relationship between sets and bit strings. Table 2.4, for instance, lists the various subsets of the set {x, y, z }. Notice that the table contains all possible threebit strings and their decimal
96
Chapter 2
The Language of Sets
values increase from 0 to 7. (See Section 4.3 for a discussion of nondecimal bases.) Next we present a systematic procedure to find the bit string of the subset that "follows" a given subset with bit string b 2 b l b o . Such a recipe for solving a problem in a finite number of steps is called an algorithm.*
NextSubset Algorithm Take a good look at each string in Table 2.4. Can you find a rule to obtain each, except 000, from the preceding string? It is fairly simple: From right to left, locate the first 0. Change it to 1 and the l's to its right to 0's. For example, suppose you would like to find the subset following {x, y} with bit string b 2 b l b o  011. From right to left, the first 0 is b2. Change it to 1, and bl and b0 to 0's. The resulting string is 100 and the corresponding subset is {z }. This rule can be generalized and translated into an algorithm. See Algorithm 2.1. Use it to find the subsets following {z} and { y, z}. Algorithm nextsubset (bnlbn2 ... bo) (* This algorithm finds the b i t string of the subset that follows a given subset of an nelement set S. *) Begin (* nextsubset *) find the f i r s t 0 from the r i g h t change i t to I replace the bits to i t s r i g h t with O's End (* nextsubset *) Algorithm 2.1
The nextsubset algorithm can be employed to find all subsets of a finite set S. Algorithm 2.2 shows the steps involved. Use it to find the subsets of {x,y,z}. Algorithm subsets (S) (* Using the nextsubset algorithm, this algorithm finds the b i t representations of all subsets of an nelement set S. *) Begin (* subsets *) bnlbn2 . . . b o 4000
*WordPerfect is a wordprocessing program marketed by Corel Corporation. **A character within single quotes indicates a literal character.
m
Chapter 3 Functions and Matrices
122
~
Let A  {0, 1 , . . . , 127}, the set of ordinal n u m b e r s in ASCII. Let f 9 A * ASCII be defined by n o n p r i n t a b l e control c h a r a c t e r uppercase letter lowercase letter other printable c h a r a c t e r
f(n)
if 0 < n < 31 or n  127 if 65 < n < 90 i f 9 7 < n < 122 otherwise
Clearly, f is defined piecewise.
m
F u n c t i o n s defined piecewise are w r i t t e n as i f  t h e n  e l s e s t a t e m e n t s in most p r o g r a m m i n g languages. For example, the function in E x a m p l e 3.6 can be w r i t t e n as follows: if
(x _> O) and (x _< 4000) then f ( x ) ~ 50 else f ( x ) ~ 50 + O.O06(x  4000)
The geometrical r e p r e s e n t a t i o n of a function, called a g r a p h , is often used to study functions. Remember, a picture is w o r t h a t h o u s a n d words. Since every function f : X . Y is a set of ordered pairs (x,y), the g r a p h of f consists of points corresponding to the ordered pairs in f, as the next example illustrates. ~
G r a p h each function. (1) Let f 9 Z ~ Z defined by f(x) (2) Let g 9 R * R defined by

x 2.
X 1 3X + 4
g(x)
ifx > 0 if2<X__x. The floor ofx r o u n d s d o w n x while the ceiling ofx r o u n d s up. Accordingly, if x r Z, the floor of x is the nearest integer to the left o f x on the n u m b e r line and the ceiling of x is the nearest integer to the right o f x, as shown in Figure 3.11. The floor f u n c t i o n f(x)  lxJ and the
F i g u r e 3.11
lxJ [xl1
[xJ+ 1 x
Ix]
32 SpecialFunctions
127
...., ,~," ,.2
,,
'~,~ .
,.,..:.
,.
a/b or a/b  1 < k. So a a l ]YI. T h e n w h a t can we say a b o u t the function f? (Obviously, f c a n ' t be bijective.) This is a n s w e r e d in the next section.
Chapter3 Functionsand Matrices
140
Cardinality of an Infinite Set (optional) Before closing this section, we e x t e n d t h e concept o f c a r d i n a l i t y of finite sets to infinite sets, a topic of g r e a t i m p o r t a n c e to t h e o r e t i c a l c o m p u t e r science a n d c e r t a i n l y to m a t h e m a t i c s . Recall t h a t two finite sets h a v e t h e s a m e c a r d i n a l i t y if t h e r e is a bijection b e t w e e n t h e m . This leads to t h e following definition. Two sets X a n d Y have t h e s a m e c a r d i n a l i t y if t h e r e exists a bijection from X to Y, d e n o t e d by IXI = I YI. This definition can be u s e d to p a r t i t i o n t h e family of infinite sets into two disjoint classes. To this end, we m a k e t h e following definition.
Countable and Uncountable Sets A set S is c o u n t a b l y infinite if t h e r e exists a bijection b e t w e e n S a n d N. A set t h a t is finite or c o u n t a b l y infinite is c o u n t a b l e . A set t h a t is n o t c o u n t a b l e is u n c o u n t a b l e . T h e c a r d i n a l i t y of N is d e n o t e d by ~0 (read " a l e p h  n a u g h t , " " a l e p h " b e i n g t h e first l e t t e r of t h e H e b r e w a l p h a b e t . This s y m b o l w a s i n t r o d u c e d by Cantor). T h u s a set S is c o u n t a b l y infinite if ISI  ~0 = INI. I f A a n d B are finite sets such t h a t A c B, t h e n IAI < IBI. This, h o w e v e r , need not be t r u e in t h e case of infinite sets. F o r e x a m p l e , E c N, w h e r e E d e n o t e s t h e set of even positive integers; n o n e t h e l e s s , IEI  I NI = ~0, as s h o w n by t h e p a i r i n g s in F i g u r e 3.18.
F i g u r e 3.18
1
2
3
4
5
.
.
.
n
.
.
.
2
4
6
8
10
.
.
.
2n
.
.
.
Show t h a t N • 1~ is c o u n t a b l y infinite.
PROOF: A l t h o u g h we shall not give a f o r m a l proof, t h e a r r o w s in F i g u r e 3.19 show how t h e v a r i o u s e l e m e n t s of N • N can be listed as t h e first, second, third, a n d so on in a s y s t e m a t i c way, s h o w i n g t h a t N x N is c o u n t a b l y infinite.
F i g u r e 3.19
~(5,
(1,1)~
(1,2)
(2, 1)
(2, 2)
~, (1,3) (2, 3) ~
(1,4) (2, 4) I ~ ' .
. . . . .
(3, 1) j ' ~ " (3,2) ~ I ~ (3,3) ~
(3,4)
9 9 9
(4, 1) ~ ' I
(4, 4)
" 9 .
(4, 2) ~
(4, 3)
1)/~"
m
3.3
Properties of Functions
141
It follows by Example 3.22 t h a t the set of positive rational n u m b e r s is countable. Consequently, the set of negative rational n u m b e r s is also countable. Since the union of two countable sets is countable (see Exercise 49), it follows t h a t Q is countable. It may seem improbable t h a t t h e r e exist infinite sets t h a t are u n c o u n t able. For instance, the open interval (0,1) is such a set, as the next example shows. Show t h a t the open interval (0,1) is uncountable. P R O O F (by contradiction): Assume t h a t the interval (0,1) is countable. T h e n every real n u m b e r between 0 and i can be listed as al, a2, a3,. 99 Each a i has a unique decimal expansion (for n u m b e r s with two different decimal expansions, choose the expansion with trailing 9's. For example, a l t h o u g h 0.5 = 0.5000 . . . . 0.4999..., select 0.4999... for our discussion.): al
= O.alla12a13a14...
a 2 = O. a 2 1 a 2 2 a 2 3 a 2 4
999
a 3 = O. a 3 1 a 3 2 a 3 3 a 3 4
999
a 4   O. a 4 1 a 4 2 a 4 3 a 4 4
999
where each a i j is a digit. Now construct a real n u m b e r b = bi
O.blb2b3b4...
l1
if all
#1
!2
if all
 1
as follows:
Clearly, 0 < b < 1; therefore b m u s t be one of the n u m b e r s in the above list al, a2, a3, a4, .... However, since bi # a i i for every i, b cannot be in the list. This leads to a contradiction. Therefore, the real n u m b e r s between 0 and 1 cannot be listed and hence the interval (0,1) is uncountable. (The technique employed is called C a n t o r ' s d i a g o n a l i z a t i o n p r o c e d u r e . ) m Since the interval (0,1) is uncountable, it follows t h a t R is also uncountable. So, although both 1~ and IR are infinite, IIRI > ~0. Exercises 3.3
Determine if each function is the identity function. x
a
b
c
d
1.
x
a
b
c
d
2. f(x)
a
b
c
d
a
b
c
d
a
b
c
c
0
f(x)
b
c
d
a
f(x)
142
Chapter3
Functions and Matrices
D e t e r m i n e if each function is injective, w h e r e t r u n c ( x ) d e n o t e s t h e i n t e g r a l p a r t of the real n u m b e r of x. 4. f(x) = Ix l , x ~ R
5. g ( x ) = 2
x,xeR
6. h(x) = lg x, x ~ IR+
7. f ( x ) =
LxJ, x c
8. g ( x ) = [ x ] , x ~ R
9. h ( x ) = trunc(x), x ~ R
10. f : S ~ W defined by f ( A ) = IAf, w h e r e S is the family of all finite sets. D e t e r m i n e if each function from R to Z is surjective. 11. f ( x ) =
Ix l
12. g ( x ) =
[xJ
13. h ( x ) =
Ix]
14. h ( x ) = lg Ix], x ~: 0
15. ORD: ASCII ~ W defined by ORD(c) = ordinal n u m b e r of t h e c h a r a c t e r c. 16. Let f : R ~ • defined by f(x) = ax + b, w h e r e a,b ~ R a n d a r 0. Show t h a t f is surjective; t h a t is, find a real n u m b e r x such t h a t f(x) = c. D e t e r m i n e if each function f :A ~ B is bijective. 17. f ( x ) = x2, A = B = R
18. f ( x ) = v/x,A = R +, B = R
19. f(x) = ] x I , A = B = R
20. f(x) = LxJ,A = B = R
2 1 . f(x) = [ x ] , A = B = R
2 2 . f(x) = 2 IxI,A = B = R
D e t e r m i n e if the functions in Exercises 2330 are bijective. If t h e y are not bijective, explain why. 23. f : E* ~ W defined by fix) = decimal value of x, w h e r e E = {0,1 }. 24. f : E* • Z* ~ E* defined by f(x,v) = xy, w h e r e E d e n o t e s t h e English alphabet. 25. g : E* ~ E* defined by g(w) = a w a , where E = {a,b,c }. 26. f : R • IR * R • R defined by f ( x , y ) = ( x ,  y ) . 27. The ORD function on ASCII. 28. The p r e d e c e s s o r f u n c t i o n ( P R E D ) and s u c c e s s o r f u n c t i o n ( S U C C ) are two i m p o r t a n t functions used in c o m p u t e r science. T h e y are defined on ordered sets. Ifc is a p r i n t a b l e ASCII c h a r a c t e r , PRED(c) denotes the predecessor of c a n d SUCC(c) denotes the successor of c; for example, P R E D ( ' ? ' ) = '@' a n d SUCC(':') = ';'. D e t e r m i n e if P R E D and SUCC are bijective. U s i n g the hash function in E x a m p l e 3.2, c o m p u t e the location corresponding to the given key. 29. 012398745
30. 430358856
S t u d e n t records are m a i n t a i n e d in a table u s i n g the h a s h i n g function h(x) = x mod 9767, w h e r e x denotes the s t u d e n t ' s social s e c u r i t y n u m b e r .
3.3 Properties of Functions
143
Compute the location in the table corresponding to the given key, where the record is stored. 31. 012345678
32. 876543210
3334. Redo Exercises 31 and 32 if h ( x )  first part in x mod 13. 35. Store the following twoletter abbreviations of states in the United States in a hash table with 26 cells, using the hashing function h ( x ) = first letter in x: NY, OH, FL, AL, MA, CA, MI, AZ 3{}. Redo Exercise 35 with the following state abbreviations: MD, CT, ID, MA, NB, NJ, MI, WI, CA, IA, WA, MN, NH, IN, NC, WY, NM, MS, MO, CO, NY, IL, NV, WV, ND, MT Two sets A and B are e q u i v a l e n t , denoted by A ~ B, if there exists a bijection between them. Prove each. 37. A ~ A (reflexive p r o p e r t y ) 38. A ~ A x
{1}
39. IfA ~ B, t h e n A x {1} ~ B x {2} 40. Z ~ O, the set of odd integers Prove each. 41. A bijection exists between any two closed intervals [a, b] and [c, d], where a < b and c < d. ( H i n t : Find a suitable function that works.) 42. The set of odd positive integers is countably infinite. 43. The set of integers is countably infinite. 44. Any subset of a countable set is countable. 45. A set A is infinite if and only if there exists a bijection between A and a proper subset of itself. 46. The open interval (a,b) is uncountable. [Hint: Find a suitable bijection from (0,1) to (a,b).] 47. The set Q+ of positive rational numbers is countable. 48. The set of irrational numbers is uncountable. ( H i n t : Prove by contradiction.) *49. A countable union of countable sets is countable. "50. The cartesian product of two countable sets is countable. "51. If E is a finite alphabet, then E* is countable.
144
Chapter3 Functions and Matrices
Suppose m pigeons fly into n pigeonholes to roost, where m > n. T h e n obviously at least two pigeons m u s t roost in the same pigeonhole (see F i g u r e s 3.20 and 3.21). This property, called the pigeonhole principle, can be s t a t e d in t e r m s of functions, as the next t h e o r e m shows.
F i g u r e 3.20
F i g u r e 3.21
J J
J
dS f J
( T h e P i g e o n h o l e P r i n c i p l e ) L e t f" X ~ Y, where X and Y are finite sets, IX I  m, I Y I  n, and m > n. T h e n t h e r e exist at least two distinct e l e m e n t s Xl and x2 in X such t h a t f ( x l )  f(x2).
PROOF" Let X  {Xl,... ,Xm}. Suppose f is injective. T h e n f ( x l ) , . . . , f ( x m ) are distinct elements in Y. So m _< n. But this contradicts the a s s u m p t i o n t h a t m > n. Therefore, f is not injective and there m u s t be at least two distinct elements x l and x2 such t h a t f ( x l )  f(x2). Hence the theorem, m The pigeonhole principle is a simple but i m p o r t a n t c o u n t i n g principle t h a t we shall use in C h a p t e r s 4, 7, and 8. The pigeonhole principle, which can be applied in a variety of situations, can be restated as follows: If m objects are placed into n boxes, t h e n at least one box m u s t contain two or more objects, where m > n. Accordingly, the pigeonhole principle is also called the D i r i c h l e t B o x P r i n c i p l e after the G e r m a n m a t h e m a t i c i a n P e t e r G u s t a v Lejeune Dirichlet, who used it extensively in his work on n u m b e r theory. Although the principle looks simple and straightforward, to apply it successfully you m u s t choose the pigeons and pigeonholes appropriately, as the next few examples illustrate. Suppose we select 367 s t u d e n t s from campus. Show t h a t at least two of t h e m m u s t have the same birthday.
SOLUTION: The m a x i m u m n u m b e r of days in a year is 366, and this occurs in a leap year. T h i n k of s t u d e n t s as pigeons and days of the year as pigeonholes. Let A be the set of s t u d e n t s and B the set of days, where IA[ = m = 367 and ]B] = n = 366. Let f : A ~ B defined by f ( a ) = b i r t h d a y of s t u d e n t a.
3.4 The Pigeonhole Principle
145
Gustav Peter Lejeune Dirichlet (18051859) was born in Duren, Germany. The son of a postmaster, he first attended a public school and then a private school that emphasized Latin. After attending the Gymnasium in Bonn for 2 years, Dirichlet entered a Jesuit college in Cologne where he received a strong background in theoretical physics under the physicist Georg Simon Ohm. In May 1822, he moved to the University of Paris. In 1826, Dirichlet returned to Germany and taught at the University of Breslau. Three years later, he moved to the University of Berlin where he spent the next 27 years. Dirichlet's primary interest in mathematics was number theory, inspired by Gauss' masterpiece, Disquisitiones Arithmeticae (1801). He established Fermat's Last Theorem for n = 14. Among the many results he discovered include the proof of a theorem presented to the Paris Academy of Sciences on algebraic number theory in 1837: The sequence {an + b} contains infinitely many primes, where a and b are relatively prime. In 1855, when Gauss died, Dirichlet moved to the University of GSttingen. Three years later, he went to Montreaux, Switzerland, to deliver a speech in honor of Gauss. While there, he suffered a heart attack and was barely able to return home. During his illness his wife succumbed to a stroke, and Dirichlet died.
Since m > n, by the pigeonhole principle, there should be at least two students al and a2 such t h a t f(al)  f(a2); t h a t is, at least two students have the same birthday, m The next example* is geometric, d e m o n s t r a t i n g t h a t the pigeonhole principle can pop up in seemingly unusual situations. Suppose five l a t t i c e p o i n t s , t h a t is, points with integer coordinates, are selected on the cartesian plane and each pair of points is joined by a line segment. Show t h a t at least one of the line segments m u s t contain a lattice point between its endpoints. SOLUTION: The set of lattice points can be partitioned into four n o n e m p t y disjoint classes according to the p a r i t y (evenness or oddness) of their coordinates: (odd,odd), (odd,even), (even,odd), and (even,even). Since there are five points (pigeons) and four classes (pigeonholes), by the pigeonhole principle, at least two of t h e m   s a y , A(a,b) and B(c,d)must belong to the same class. By the m i d p o i n t f o r m u l a in analytic geometry, the midpoint M of the line segment AB is (a~c, b+d)2 . Since the sum of any two odd or even integers /
is an even integer, it follows t h a t M is also a lattice point. Thus AB contains a lattice point M different from its endpoints, m *Based on C. T. Long, "On Pigeons and Problems," Mathematics Teacher, Vol. 81 (January 1988), pp. 2830, 64.
146
Chapter 3
Functions and Matrices
It is well known that the decimal expansions of rational n u m b e r s are periodic. Using the pigeonhole principle, we shall establish this, but first a few words of explanation may be helpful. Using the familiar long division method, you may verify that 4111 = 0. 12345345345345... 33300 Although the decimal expansion is nonterminating, it is p e r i o d i c ; t h a t is, a certain block of digits, namely, 345, gets repeated. Accordingly, the expansion is usually written as 0.12345, using a bar over the first repeating block. The number of digits in the smallest repeating block is the p e r i o d of the expansion; here it is 3. We are now ready to prove the above proposition. ~
Prove that the decimal expansion of a rational number is periodic. PROOF"
a
Consider, for convenience, a positive rational number ~, where 0 < a < b. a
Let ~  O . d l d 2 d 3 . . . where, by the division algorithm (see Section 4.1), we have: 10a  b d l + rl 10rl  bd2 + r2 10r2  bd3 + r3 (3.2) lOrj  bdj+ 1 + rj+ 1
and 0 _< ri < b for every i. (Note: The digits a l l , d 2 , . . , in the decimal expansion are the quotients when 10a, 10rl,... are divided by b. Since a remainder has only b choices, by the pigeonhole principle, two of the remainders r l , r 2 , . . . ,rb+l must be equal; that is, rj  rk for s o m e j and k, where 1 < j < k _< b + 1. Consequently, dk+l  dj+l, dk+2  d]+2,..., d2kj  d k , d 2 k  j + l  dj+l, and so on. Thus d j + l . . , dk is the smallest block getting repeated and the period of the decimal expansion is k  j . m The next example, a rather sophisticated application of the pigeonhole principle, is due to the Hungarian mathematician Paul ErdSs. ~
( E r d S s T h e o r e m ) Ifn + 1 integers are selected from the set {1, 2 , . . . , 2n}, one of them divides another integer that has been selected. PROOF"
Let al, a2,... ,an+l denote the integers selected. Write each of them as a product of a power of 2 and an odd integer; that is, ai  2e/bi, where 1 < i < n + 1 andei > 0. The integers bl, b2,... ,bn+l are odd positive integers < 2n.
147
3.4 The Pigeonhole Principle
.. (..~,.v,.o:
.r
....~"i~(,: . ~.~.~ ""'"'~.~"~.:~ ~".:~'~ " " t ..~ . ..~. ~..'~. . Jo,~, , f~t..~. ~. .;~, ." ...... "~z;i!~ ~:: ~ ....~,_,~:.~,_:~: ......... ~ " ~ ~is~~ ! ' .'~' "" ~ " '   ~ " ~,: " ~j N ~
P a u l E r d 6 s (19131996) was born in Budapest, Hungary. Except for about three years in schools, Erd6s (pronounced airdosh) was taught at home, mostly by his father, who had returned from a Siberian prison after 6 years. A child prodigy, Erd6s, at age 3, discovered negative numbers for himself. In 1930 Erd6s entered the Peter Pazmany University in Budapest. Three years later, he discovered a beautiful proof of the celebrated Chebyshev theorem that there is a prime between any positive integer n and 2n. In 1934 he received his Ph.D. from the university. ~i , ~ . , ~ ~ An author of about 1500 articles and coauthor of about 500, Erd6s was 9~ one of the mostprolific writers in mathematics. A tribute in 1983 described him as "the prince of problemsolvers and the absolute monarch of problemposers." As "the Euler of our time," he contributed extensively to number theory, combinatorics, function theory, complex analysis, set theory, group theory, and probability, the first two areas being closest to his heart. "Always searching for mathematical truths," he deemed worldly possessions a nuisance, so he never had a home, a car, checks, or even an address. Always traveling from meeting to meeting, carrying a halfempty suitcase, he would stay with mathematicians wherever he went and donate the honoraria he earned as prizes to students. A recipient of many honors, Erd6s died of a heart attack while attending a mathematics meeting in Warsaw.
Since t h e r e are exactly n odd positive i n t e g e r s < 2n, by t h e pigeonhole principle, two of t h e e l e m e n t s bl, b 2 , . . . , bn+l m u s t be equal, say, bi  bj. T h a t is, aj  2~1b/  2(:Jbi. Thus, if ei < e] t h e n ai aj, a n d if ei < ei t h e n aj [ai.* m T h e pigeonhole principle tells us t h a t if m pigeons are d i s t r i b u t e d into n pigeonholes, w h e r e m > n, at least two pigeons m u s t s h a r e t h e s a m e pigeonhole. In fact, if m o r e t h a n 2m pigeons are assigned to m pigeonholes, t h e n at least t h r e e pigeons m u s t s h a r e t h e s a m e pigeonhole. T h u s t h e pigeonhole principle can be generalized as follows. ( T h e G e n e r a l i z e d P i g e o n h o l e P r i n c i p l e ) If m pigeons are assigned to n pigeonholes, t h e r e m u s t be a pigeonhole c o n t a i n i n g at least [ ( m  1)/nJ + 1 pigeons.
P R O O F (by c o n t r a d i c t i o n ) : Suppose no pigeonhole c o n t a i n s m o r e t h a n [(m  1)/nJ pigeons. Then: m a x i m u m n u m b e r of pigeons  n . [(m  1)/nJ
k 9 ]Y], and k c IN. T h e n t h e r e is an e l e m e n t t ~ Y such t h a t f  l ( t ) c o n t a i n s m o r e t h a n k elements. 18. Prove t h a t any set S of t h r e e integers contains at least two i n t e g e r s whose s u m is even. (Hint: Define a suitable function f 9 S ~ {0, 1} and use Exercise 17.) "19. U s i n g the pigeonhole principle, prove t h a t the c a r d i n a l i t y of a finite set is unique.
Besides adding and m u l t i p l y i n g functions, t h e r e is a very f u n d a m e n t a l way of c o n s t r u c t i n g new functions. Consider the f u n c t i o n s f , g : R ~ I~ defined b y f ( x ) = 2 x + 3 a n d g ( x ) = x 2. Let x be an input into f. T h e n f(x) = 2x + 3 is a real n u m b e r a n d hence can be considered an i n p u t into g. The r e s u l t i n g o u t p u t is g ( f ( x ) ) (see F i g u r e 3.24). T h u s the functions f and g can be employed to define a new function, called the c o m p o s i t e of f and g, as shown in F i g u r e 3.25.
F i g u r e 3.24
f(x)
g(f(x) )
F i g u r e 3.25
go/"
This leads us to the following definition.
3.5
151
Compositionof F u n c t i o n s
Composition Let f :X ~ Y and g : Y ~ Z. The c o m p o s i t i o n o f f and g, denoted by g o f (notice the order of the functions), is a function from X to Z, defined by (g o f)(x) = g ( f ( x ) ) . Read g o f as g circle f or the composition o f f a n d g. [In general, dom(g) need not be the same as codom(f); all t h a t is needed is t h a t range (f) c_ dom(g).] L e t f , g " R ~ IRt defined b y f ( x ) ( f o g)(x).
2x + 3 a n d g ( x )  x 2. Find ( g o f ) ( x ) a n d
SOLUTION: f ( x )  2x + 3
Then
(g o f)(x)  g ( f (x))
= g(2x + 3)  (2x + 3) 2 g(x)
D
So

x 2
( f o g)(x)  f (g(x))
= f(x 2)  2(x 2) + 3 =
m
2x2+3
It follows from Example 3.31 that, in general, f o g r g o f; in other words, composition is not a commutative operation. For instance, putting clothes in a washing machine and t h e n in a dryer does not yield the same result as p u t t i n g t h e m in a dryer and then in a washing machine!
(optional) Composition is easily accomplished in computer science. To illustrate this, study the following algorithm fragment, where x ~ ]R: i.
if
2. 3.
x < 4 then x 0; g ( x )  v/x, x >_ 0
14. f ( x )  x 2, x _< 0; g ( x )   v ~ ,
x >_ 0
Define the inverse g of each function f. x
a
b
c
d
15.
x
a
b
c
d
f(x)
b
c
d
a
16. f(x)
4
1
3
2
Determine if the given function is invertible. If it is not invertible, explain why. 17. ORD on Z. 18. f" ASCII ~ W defined by f ( c )  ordinal n u m b e r of the character c. 19. f" W ~ W defined by f ( n ) = n (mod 5). 20. f" E* ~ E* defined by f ( w ) = awa, where E  {a, b, c}. 21. f" S ~ N defined by f ( x )  decimal value of x, where S is the set of binary r e p r e s e n t a t i o n s of positive integers with no leading zeros.
156
Chapter3 Functions and Matrices
22. f 9 E* ~ W defined by f ( x ) = decimal value of x, where E = {0,1}. n
23. Let f 9 ~n
__.>
W defined by f ( x ) 
~ xi, where
]~n
denotes the set
i=l
of words of length n over E  {0, 1, 2} and x w e i g h t of x; for example, f(10211) = 5.]

XlX2...Xn
. If(x)
is the
Mark each sentence as true or false. Assume the composites and inverses are defined: 24. The composition of two injections is injective. 25. The composition of two surjections is surjective. 26. The composition of two bijections is a bijection. 27. Every function is invertible. 28. Every injective function is invertible. 29. Every invertible function is injective. 30. Every invertible function is surjective. 31. Every invertible function is bijective. 32. Every bijection is invertible. 33. The composition of two invertible functions is invertible. Using the algorithm fragment in Example 3.32, compute the o u t p u t resulting from each initial value of x. 34.  5
35. 0
36. 3
37. 7
Let f 9X ~ Y and g 9 Y ~ Z. Prove each. 38. f o l z  f
39. 1 y o f = f
40. If f and g are injective, then g o f is injective. 41. If f and g are surjective, then g o f is surjective. 42. If f and g are bijective, then g o f is bijective. 43. The identity function 1x is bijective. 44. I f g o f is injective, then f is injective. 45. I f g o f is surjective, then g is surjective. 46. If g o f is bijective, then f is injective and g is surjective. Let f 9X ~ Y and g 9 Y , Z be invertible functions. Prove each. 47. f  l o f _ l x
48. f o f  l  l y
49. f  1 is bijective.
50. ( f  1 )  I _ f
51. (g o f )  i _ f  1 o g  1
*52. The inverse o f f is unique.
3.6 Sequencesand the Summation Notation
157
53. L e t f ' A   + B , g ' B   + C, a n d h ' C   + D . Provethatho(gof)(h o g) o f ( a s s o c i a t i v e property). [Hint: Verify t h a t (h o (g o f ) ) ( x ) = ((h o g) o f ) ( x ) for every x in A.] *54. Let f a n d g denote t h e functions defined by t h e i f  t h e n  e l s e statem e n t s in E x a m p l e 3.31. Show t h a t g o f is defined as given in t h e example. (Hint" Consider the cases x _< 4 a n d x > 4, a n d t h e n two subcases in each case.) Prove each, w h e r e X ~ Y implies set X is e q u i v a l e n t to set Y. *55. I f A ~ B, t h e n B ~ A ( s y m m e t r i c property). *56. I f A ~ B and B ~ C, t h e n A ~ C ( t r a n s i t i v e property). Let f 9X + Y be bijective. Let S a n d T be subsets of Y. Prove each. *57. f  l ( s u T)  f  l ( S ) u f  l ( T )
*58. f  l ( s n T)  f  l ( s ) n f  l ( T )
Sequences and the s u m m a t i o n n o t a t i o n play a key role in the next t h r e e chapters, so we p r e s e n t t h e m here. Let a be a whole n u m b e r and X  {a,a + 1,a + 2,...}. A function s with d o m a i n X or a subset of X is called a s e q u e n c e . Let n e X. T h e n s(n) is called a t e r m of the sequence, denoted by Sn. T h e various t e r m s of the sequence can be listed as Sa, Sa+l, 8a+2,... in i n c r e a s i n g order of subscripts. In particular, let X = l~. T h e n the t e r m s of the sequence are: 81,82,83,.
9 9 ,8n,.
9 9
t
general term
The n t h t e r m 8n is t h e general term of the sequence; the sequence is often denoted by {Sn}~ or simply {Sn}. (It should be clear from the context w h e t h e r the braces indicate a set or a sequence.) T h e g e n e r a l t e r m is often used to define a sequence. Consider the sequence {Sn}, w h e r e Sn  2n  1. T h e various t e r m s of t h e sequence are 1, 3, 5, 7, . . . . Formally, the sequence is t h e function s : l~ + 1~ defined by s(n) = 2n  1. I Let an be t h e b i n a r y r e p r e s e n t a t i o n of t h e positive i n t e g e r n with no leading zeros. The various t e r m s of the sequence {an} are 1, 10, 11, 100, 101, 110, 111, . . . . I Sequences can be classified as finite or infinite, as t h e next definition shows.
158
Chapter3 Functionsand Matrices Finite and Infinite Sequences A sequence is f i n i t e if its domain is finite; otherwise, it is i n f i n i t e . Thus, a finite sequence is made up of a finite n u m b e r of terms, a n d an infinite sequence contains infinitely m a n y terms. Both types are useful in mathematics and computer science as well. Every word over an alphabet can be considered a finite sequence. F o r instance, the binary word 010110111 is a finite sequence c o n t a i n i n g nine terms. The elements of a finite language form a finite sequence; for example, the words of length _< 2 over the alphabet {a, b, c} form a finite sequence, namely, k, a, b, c, aa, ab, ac, ba, bb, bc, ca, cb, cc; on the other hand, k, a, a2 a3 is an infinite sequence We now t u r n to the s u m m a t i o n notation you will find very useful t h r o u g h o u t the remainder of the book.
The Summation Notation Often we need to work with sums of terms of n u m b e r sequences {a,z }. S u m s such as ak + ah+l + " + am can be written in a compact form u s i n g the s u m m a t i o n s y m b o l ~ , which denotes the word s u m . The s u m m a t i o n notation was introduced in 1772 by the brilliant French m a t h e m a t i c i a n Joseph Louis Lagrange. (Recall t h a t E denoted an alphabet in C h a p t e r 2; its actual meaning should be clear from the context.) A typical term in the above sum can be denoted by ai, so it is the s u m of i=m
the terms ai as i runs from k to m. It is denoted by ~ ai. T h u s i=k
im
E ai = ak + ak+l + i=k
"
'
"
+ am
The variable i is the s u m m a t i o n i n d e x . The values k and m are the l o w e r and u p p e r l i m i t s of the index i. The "i  " above the ~ is usually omitted; in fact, the indices above and below the ~ are also omitted w h e n there is no confusion. Thus i    DI
Dl
EaiEaii =k i =k
171
~~ai k
For example,
6 ~~i 1 + 2 + 3 + 4 + 5 + 6   2 1 i=1
3.6
Sequences and the Summation Notation
159
J o s e p h L o u i s L a g r a n g e (17361813) ranks with Leonhard Euler (see Chapter 8) as one of the greatest mathematicians of the 18th century. The eldest of 11 children in a wealthy family in Turin, Italy, Lagrange was forced to pursue a profession after his father, an influential cabinet official, lost all his wealth by engaging in unsuccessful financial speculations. While studying the classics at the College of Turin, the 17yearold Lagrange found his interest in mathematics kindled by an essay by the astronomer Edmund Halley on the superiority of the analytical methods of calculus over geometry in the solution of optical problems. In 1754, he began corresponding with several outstanding mathematicians in Europe. The following year, he was appointed professor of mathematics at the Royal Artillery School in Turin. Three years later, he helped to found a society that later became the Turin Academy of Sciences. While at Turin, Lagrange developed revolutionary results in the calculus of variations, mechanics, sound, and probability, winning the prestigious Grand Prix of the Paris Academy of Sciences in 1764 and 1766. In 1766, when Euler left the Berlin Academy of Sciences, Frederick the Great wrote to Lagrange that "the greatest king in Europe" would like to have "the greatest mathematician of Europe" at his court. Accepting the invitation, Lagrange moved to Berlin to head the Academy and remained there for 20 years. When Frederick died in 1786, Lagrange moved to Paris at the invitation of Louis XVI. He was appointed professor at the Ecole Normale and then at the E,cole Polytechnique, where he taught until 1799. He died in Paris. Lagrange made significant contributions to analysis, analytic mechanics, calculus, probability, and number theory, as well as helping to establish the French metric system.
2 i(i 1)= (1)(1
1)+ 0(0 1)+ 1(1 1)+ 2(2 1) 4
i=1 T h e index i is a d u m m y variable; you can use a n y variable as t h e index w i t h o u t affecting t h e value of t h e sum, so m
m
m
Eai=Eaj=Eak i=~
j=e
k=e
3 E v a l u a t e ~ i 2. i=2 SOLUTION: 3 ~ i 2  (  2 ) 2 + (  1 ) 2 + 0 2 + 12 + 2 2 + 3 2  19
m
i=2 The following r e s u l t s are e x t r e m e l y useful in e v a l u a t i n g finite sums. T h e y can be proved u s i n g m a t h e m a t i c a l i n d u c t i o n (Section 4.4).
160
Chapter 3
Functions and Matrices
Let n e N a n d c e R. L e t sequences. Then:
al,
a n d b l , b 2 , . . . , be a n y two n u m b e r
a2, . . . ,
n
9
E
c 
(3.3)
nc
i=1 n
9
(cai)
c
(3.4)
ai
i=1
"
E (ai + bi) i=1
ai
+
bi
(3.5)
(These r e s u l t s can be e x t e n d e d for a n y lower limit k ~ Z.)
m
T h e n e x t e x a m p l e i l l u s t r a t e s this t h e o r e m . 2
E v a l u a t e ~ [(5j) 3  2j]. j=l
SOLUTION: E j=
1(5j)3  2j] 1
(5j) 3 
j
=1
= 125
j3 =1
_ 2 ~j j=i
= 125[(1) a + 0a + i a + 23] 2(1+
0 + 1 + 2)
= 996
m
Indexed Summation
T h e s u m m a t i o n n o t a t i o n can be e x t e n d e d to s e q u e n c e s w i t h index sets I as t h e i r d o m a i n s . F o r instance, ~ ai d e n o t e s t h e s u m of t h e values ai as i ieI
r u n s over t h e v a r i o u s values in I. As a n example, let I = {0, 1, 3, 5}. T h e n ~ (2i + 1) r e p r e s e n t s t h e s u m of iEI
t h e values of 2i + 1, so (2i + 1) = ( 2 . 0 + 1) + ( 2 . 1 +
1) + ( 2 . 3 + 1) + ( 2 . 5 + 1)  22
i~I
O f t e n we need to e v a l u a t e s u m s of t h e form ~ aij, w h e r e t h e s u b s c r i p t s i p a n d j satisfy c e r t a i n p r o p e r t i e s P. (Such s u m m a t i o n s are used in C h a p t e r s 4 a n d 6.)
3.6 Sequences and the Summation Notation
161
For example, let I = {1, 2, 3, 4}. Then
~
(2i + 3j) denotes the sum
ll dJ6
= sum of positive factors of 6 = 1+2+3+6=
12
m
Multiple summations arise often in mathematics. They are evaluated in the righttoleft fashion. For example, the double summation ~ ~ aij i
j
is evaluated as E (~~aij) and the triple summation E E ~~aijk as i j i j k E [ E ( E aijk)] 9 i j k We close this section with an example of a double summation. 1
2
Evaluate E
E (2i +3j).
i=lj=0
SOLUTION: 1
2
~~(2i + 3 j ) i=lj=0
(2i + 3j) i =  1 [_j=0 1
[(2i + 3 . 0 ) + (2i + 3 . 1 ) + (2i + 3 . 2 ) ] i=1 1
= ~
(6i + 9)
i=1
= [6. (  1 ) + 91 + ( 6 . 0 + 9) + ( 6 . 1 + 9) =27
m
Chapter3 Functionsand Matrices
162
Exercises 3.6 Evaluate each sum. 4
6
2.
1. E i i=1
2
Y~ (3n  2)
j=2
3
E3k k=2 5
3
3(k 2)
0
(3k) 2
0
k=2
k=I
4
5
0;2) 2
11.
~ (32k)k
12. ~ (0.1)i
j=i
k=l
2)
E J(J
0
n=0
4
10.
j=o
4
5.
E3 i=1
7.
3. ~ ( j  1)
k=O
4
4.
4
~ (3+k)
(0.9)5i
i=0
Rewrite each s u m u s i n g the s u m m a t i o n notation.
14. 3 1 + 3 2 + . . . + 3 1 ~
13. 1 + 3 + 5 + . . . + 2 3 15. 1 . 2 + 2 . 3 + . . . +
16. 1 ( 1 + 2 ) + 2 ( 2 + 2 ) + . .  + 5 ( 5 + 2 )
11.12
D e t e r m i n e if each is t r u e or false. n
n
n
i= m
n
18. E x i  E
17. ~~ i  ~~ (n + m  i )
i=m
i= m
xn+mi i=m
n
19. S u m s of the form S 
E
t h a t S  an  am.
i=m+ l
(ai ai1)
20. Using Exercise 19 and the identity
are
telescoping
1 + i(i 1)
1
i
sums.
1 + i 1
Show
derive a
n 1 formula for ~ i(i + 1)" i=1
21. Using Exercise 19 and the identity (i + 1) 2  i 2  2i + 1, find a f o r m u l a r/
forE/. i=1
Evaluate each sum, where ~ij is defined as follows.
~/j =
1
ifij
0
otherwise
[~ij is called K r o n e c k e r ' s delta, after the G e r m a n m a t h e m a t i c i a n Leopold Kronecker (18231891).]
3.6 Sequences and the Summation Notation 5
163
6
3
22. E E (2i + 3j)
i=lj=l
i=lj=l 6
5
6
24. E E (2i + 3j)
i=1j=1
6
6
26. E E (i2 _ j + 1)
j=1i=1
5
3
28. E E ~ij
5
29. E E (2 + 3~ij)
i=1j=1 6
5
27. E ~ ( i 2  j + 1)
i=lj=l 5
5
25. E E ( i 2  i )
j = l i=1 5
i
23. E ~ ( ] + 3 )
i=lj=l
7
30. Y~. y~ (i2 _ 3i + ~ij) i=lj=l
Just as ~ is used to denote sums, the product akak+l.., am is denoted by m
ai. The p r o d u c t s y m b o l Yl is the Greek capital letter pi. For example, i=k
n
n!  [I i. Evaluate each product. i=1 3
31.
5
~ (i+l)
32.
i=1
50
~ (j2+l)
33.
j=3
50
1I 1
34.
II (  1 ) k
j=5
k=0
Evaluate each sum and product, where p is a prime and I = { 1, 2, 3, 5 }. 3
35.
~:k!
36.
}~p
37.
p__l
d>_l
d112
dJl2
41.
43. 42.
d>l
El ij
[I
i,jeI
i<j
ilj
4
46. ~ ( 3 j  3 j  l ) j=l
Expand each. 3
2
48. ~ aij j1
i1 3
2
49. E E aij i=lj=l
51.
~ (ai + aN) 1 O , a r 1)
(page 125).
9 P o l y n o m i a l function f ( x ) = ~ a i x i l=O
9 E x p o n e n t i a l function f ( x ) =
9 L o g a r i t h m i c function f ( x )  log a x (a > 0, a ~: 1)
(page 126).
9 Absolute value function f ( x )  Ixl
(page 126).
9 Floor function f ( x ) = Ix]
(page 126).
9 Ceiling function f ( x ) = Ix]
(page 126). 1
9 Characteristic
functionfA(x)
0
ifx ~A otherwise
(page 131 ).
9 Mod function f ( x , y ) = x mod y
(page 132).
9 Div function g ( x , y ) = x divy
(page 132).
Properties of Functions 9 A function f : X ~ X is the i d e n t i t y f u n c t i o n every x ~ X 9 f " X ~ Y is i n j e c t i v e , i f x l r
X2 ~
f ( x l ) r f(x2)
9 f 9X . Y is s u r j e c t i v e , if r a n g e ( f )  Y
on X if f ( x ) = x for (page 136). (page 136). (page 137).
9 f " X . Y is b i j e c t i v e , if it is both injective a n d surjective (page 137). 9 I f X and Y are finite sets with IX[ = [Y I, f : X ~ Y is injective if a n d only if it is surjective (page 139). 9 Two sets have the s a m e cardinality if and only if a bijection exists between them (page 140).
The Pigeonhole Principle 9 S i m p l e v e r s i o n If m pigeons fly into n pigeonholes to roost, w h e r e m > n, t h e n at least two pigeons m u s t roost in t h e s a m e pigeonhole (page 147 ). 9 G e n e r a l i z e d v e r s i o n If m pigeons fly into n pigeonholes to roost, w h e r e m > n, one pigeonhole m u s t contain at least L(m  1)/nJ + 1 pigeons (page 147).
Composition 9 T h e c o m p o s i t i o n of the functions f : X ~ by ( g o f ) ( x ) = g ( f ( x ) ) for every x in X
Y a n d g : Y ~ Z is given (page 151).
Chapter Summary
177
9 The composition of two bijections is bijective
(page 152).
9 The function g : Y ~ X is the inverse of the function f : X ~ Y if g o f = 1z and f o g  1y (page 153). 9 A function is invertible if and only if it is bijective
(page 154).
S e q u e n c e s and the S u m m a t i o n Notation
9 A sequence {Sn} is a function with domain X = {a,a + 1,a + 2,...} or a finite subset of X, where a 9 W (page 157). 9 Using the s u m m a t i o n symbol ~ , the sum ak + a k + l + " " + a m is w r i t t e n i=m
as ~ ai
(page 158).
i=k
Matrix
9 An m x n m a t r i x (aij)m • is a r e c t a n g u l a r a r r a n g e m e n t of elements, where aij denotes the element in row i and c o l u m n j (page 165).
Review Exercises
Find the n u m b e r of positive integers < 1776 and divisible by each: 1. 5 o r 7
2. 5 b u t n o t 7
3. 5, 6, or 7
4. 3 or 5, but not 7
Find the day of the week in each case. 5. 1024 days from Sunday
6. 1948 days from T h u r s d a y
Find the m o n t h of the year in each case. 7. 256 m o n t h s from March
8. 1976 m o n t h s from August
Using formula (3.1) in Exercises 3.2, determine the first day in each year. 9. 2048
10. 4076
11. 7776
12. 7997
Using the formula in Exercises 3.2, compute the date for E a s t e r S u n d a y in each year. 13. 2550
14. 3443
15. 4076
16. 6666
Determine if each function is injective. 17. f ( x )   ] x l ,
x e R
18. g ( x ) = v/x, x 9 IR +
Determine if each function f :A ~ B is surjective. 19. f(x) =  ~ / ~ , A = IR+, B  JR
20. f(x) = 2 x, A = R , B = R +
178
Chapter 3
Functions and Matrices
D e t e r m i n e if each function f :A ~ B is bijective. 21. f ( x ) = 2
IxI,A=B=I~
22. f = O R D ,
A=BZ
Let A and B be finite sets with IAI  3 and IBI  2. Find the n u m b e r of: 23. Functions t h a t can be defined from A to B. 24. Constant functions t h a t can be defined from A to B. 25. Injections t h a t can be defined from A to B. 26. Surjections t h a t can be defined from A to B. 27. Bijections t h a t can be defined from A to B. S t u d e n t records are m a i n t a i n e d in a table using the h a s h i n g function h ( x ) = x mod 9767, where x denotes the s t u d e n t ' s social security n u m b e r . Compute the location in the table corresponding to the given key, w h e r e the record is stored. 28. 011531212
29. 212447557
3 0  3 1 . Redo Exercises 28 and 29 if h ( x ) = first p a r t in x mod 23. 32. The confirmation n u m b e r for flight reservations made with an airline over the I n t e r n e t consists of three letters followed by a digit and then two letters. Store the following confirmation n u m b e r s in a hash table of 26 cells using the hashing function h ( x ) = first letter in x: VPS3SL, NBC4GK, CBS1AA, AQX5CD, CBA3BA, NCR4SK, CNN 1TK, ABC5ZZ 33. Redo Exercise 32 if h ( x ) = last letter in x. 34. Redo Exercise 32 using a hash table of 10 cells and h ( x )  digit in x. 35. Redo Exercise 32 using a hash table of 10 cells and h ( x ) = digit in x m o d 5. 36. Show t h a t in any group of seven positive integers, at least two of t h e m leave the same r e m a i n d e r when divided by six. 37. The total cost of mailing six letters is $19. Show t h a t the mailing charge for at least one letter is $3 or more. Let f, g : R ~ R defined by f ( x ) = 2[x7+ 1 and g ( x ) = 3Lx]  2. Compute each. 38. (g o f)(2.56)
39. (f o g ) (  3 . 4 5 )
40. (g o f ) (  4 . 6 7 )
41. (f o g ) (  5 . 7 3 )
Chapter Summary
179
Let f, g : W ~ W defined by f ( x ) = x m o d 6 a n d g ( x )  x div 6. E v a l u a t e each. 42. (g o f)(31)
43. ( f o f)(49)
44. ( f o g ) ( 1 7 6 )
45. (g o g ) ( 1 3 3 1 )
M a r k each s e n t e n c e as t r u e or false, w h e r e x a n d y are a r b i t r a r y real numbers. 46.
Ix + yj = [xj + [yj
47.
Ix + Y l = [xl + [Yl
48.
LxyJ = LxJ [yJ
49.
[xyl = Ix] [y]
Give a c o u n t e r e x a m p l e to disprove each proposition, w h e r e x, y e R a n d neZ. 50.
[x+yJ = [xJ+[yJ
51.
[x+yl
= [xl+[yl
53.
[xyl = [x][yl
54.
LnxJ = n LxJ
52.
[xyJ = [xJ[yJ
55.
[nxl = n f x ]
F i n d t h e first four t e r m s of t h e s e q u e n c e w i t h t h e given g e n e r a l t e r m , w h e r e = (1 + v/5)/2 a n d fl = (1  j ~ ) / 2 . (The n u m b e r a is t h e g o l d e n r a t i o . ) 56.
an
L'" J
57.
bn 
['" l
58.
L n  oen + r '~ , n >__ 1
59.
fn 
 ~ (or n  r n )
~ + 89 , n >__ l
_~ _1
, n >__ l
60. A r r a n g e t h e t e r m s of t h e s e q u e n c e of t e r n a r y w o r d s of l e n g t h < 2 over the a l p h a b e t {0,1,2} in i n c r e a s i n g o r d e r of t h e i r n u m e r i c values. E v a l u a t e each. 10
10
i
i
z [.]
.1. FIL J
i=1
17
i
17
F1L'J
i
E I.]
"
i=1
65. Let f 9 A ~ injective.
B a n d g 9 B ~ A such t h a t f o g 
lB. P r o v e t h a t g is
66. Let f 9 A o surjective.
B and g 9 B o
1A. P r o v e t h a t g is
A such t h a t g o f 
Supplementary Exercises Let A  ( a i j ) n x n a n d B  ( b i j ) n x n . P r o v e each. 1. (A + B ) T  A T + B T
2. (AB) T  B T A T
3. A A T is s y m m e t r i c .
4. Let G  {0, 1} a n d d" G n x G n + W defined by d ( x , y )  n u m b e r of c o m p o n e n t s in w h i c h t h e w o r d s x a n d y differ, d ( x , y ) is called t h e H a m m i n g d i s t a n c e b e t w e e n t h e nbit w o r d s x a n d y. Is d bijective?
Chapter 3
180
,,,
.o
,'
.
,~2~:~~
j
Functions and Matrices
R i c h a r d Wesley H a m m i n g (19151998) was born in Chicago, graduated from the University of Chicago in 1937, and received an M.S. from the University of Nebraska 2 years later. After receiving his Ph.D. in mathematics in 1942 from the University of Illinois, he began his teaching career at the university and moved to the university of Louisville until 1945. After a year working on the Manhattan project at Los Alamos Science Laboratory, he joined the technical staff at Bell Telephone Labs in 1946; he headed the numerical methods research department from 1964 to 1967, and then the computer science research department until 1977. He left Bell in 1977 and became an adjunct professor in computer science at the Naval Postgraduate School, Monterey, California. Recipient of numerous awards, Hamming made significant contributions to algebraic coding theory, numerical methods, statistics, and digital filters.
5. Let al, a 2 , . . . ,an c IR+. Prove t h a t at least one of t h e m is g r e a t e r t h a n n
or equal to their average n1 (Y~ai). 1
E v a l u a t e each sum and product. 2
3
6. ~_, Z
1
1
7. Z
Z
1
Z(i+J+
k)
i=0 j=I
i = 0 j = 1 k=O
8.
3
4
~
~
5
~ 2 i +j  k
3
9.
i=1 j = 2 k = l 2
10.
i=0 j=l
+3k)
k=O
2
lI lI 2i +j i=1 j=O
3
1I [l ( i  j )
2
Z(i+2j
5
11.
3
[1 1] 2 i=2 j=0
An a r i t h m e t i c s e q u e n c e is a n u m b e r sequence in which every t e r m except the first is obtained by adding a fixed n u m b e r , called the c o m m o n d i f f e r e n c e , to the preceding term. For example, 1, 3, 5, 7 , . . . is an a r i t h m e t i c sequence with c o m m o n difference 2. Let an denote the n t h t e r m of the a r i t h m e t i c sequence with first t e r m a and c o m m o n difference d. 12. Find a formula for an. 13. Let Sn denote the s u m of the first n t e r m s of the sequence. Prove t h a t n Sn  ~12a + ( n  1)d]. A g e o m e t r i c s e q u e n c e is a n u m b e r sequence in which every t e r m except the first is obtained by m u l t i p l y i n g the previous t e r m by a constant, called the c o m m o n r a t i o . For example, 2, 6, 18, 5 4 , . . . is a geometric sequence with c o m m o n ratio 3. Let an denote the n t h t e r m of the geometric sequence with first t e r m a and c o m m o n ratio r.
Chapter Summary
181
14. Find a formula for an. 15. Let Sn denote the sum of the first n t e r m s of the sequence. Prove t h a t S n  a ( r n  1 ) r1
(r ~= 1).
* 16. Let f: X ~ Y be injective and A __ B _ X. Prove t h a t f(A • B) f (A) n f (B). "17. L e t f : X ~ Y a n d A _ B c X such t h a t f ( A ~ B ) = f ( A ) A f ( B ) . Give a counterexample to show t h a t f need not be injective. "18. Suppose a l + a2 + . . . § an  n + 1 pigeons occupy n pigeonholes Hi, 1 < i < n. Prove t h a t either H1 contains > a l pigeons, or H2 contains > a2 p i g e o n s , . . . , or Hn contains > an pigeons. Use Exercise 18, prove each. 19. The pigeonhole principle. 20. The generalized pigeonhole principle. oo
21. The sequence {an }~ satisfies the property t h a t an 
ai for every i=n+l
n > 1. Show t h a t an+l  89
> 1. (T. Fletcher, 1978).
Computer Exercises Write a program to perform each task. 1. Read in the a m o u n t of water used by a household for 6 m o n t h s and compute the water bill, using the rate in Example 3.6. 2. Read in a positive integer n < 1000 and print all perfect n u m b e r s < n. (There are three perfect n u m b e r s < 1000. See Exercises 3.1.) 3. Read in a year y > 1600 and determine each: 9 W h e t h e r or not it is a leap year. 9 The n u m b e r of leap years > 1600 and < y. 4. Read in a year y and find the following (see Exercises 3.2): 9 The day of J a n u a r y 1 in year y and year y + 1. 9 The n u m b e r of F r i d a y  t h e  t h i r t e e n t h s in year y. 5. J a n u a r y 1, 2000, fell on a Saturday. Determine the day of the week of J a n u a r y 1, 1776, and J a n u a r y 1, 3000. P r i n t the calendar for J a n u a r y in each year. 6. Read in a series of years greater t h a n 2000 and determine the Easter date in each year. (See Exercises 3.2.)
182
Chapter3 Functions and Matrices 7. The discrete probability p ( r ) t h a t two people in a g r o u p of r people selected at r a n d o m have the same b i r t h d a y is given by p(r)
1
365.364 ... (365  r + 1) 365 r
a s s u m i n g 365 days in a year. C o m p u t e the probability for each value of r: 10, 20, 3 0 , . . . , 100, including 23. (You will see t h a t i f 2 3 people are selected at r a n d o m , t h e r e is a b e t t e r t h a n 50% chance t h a t two have the same birthday. This is k n o w n as the b i r t h d a y p a r a d o x . ) 8. Assign the n u m b e r s 051 in order to the 52 playing cards in a standard deck. Read in a n u m b e r x, w h e r e 0 _< x < 51. I d e n t i f y t h e card n u m b e r e d x. Use the suit labels 0  clubs, 1  d i a m o n d s , 2  h e a r t s , and 3  spades, and the card labels 0  ace, 1  deuce, 2  t h r e e , . . . , in each suit. 9. Assign the n u m b e r s 063, row by row, to the various s q u a r e s on an 8 x 8 chessboard. Read in two n u m b e r s x and y, w h e r e 0 < x , y < 63. D e t e r m i n e if the queen at square x can c a p t u r e the q u e e n at s q u a r e y. 10. Read in n c u s t o m e r s ' ninedigit account n u m b e r s at a b a n k , w h e r e n is a positive integer _< 100. Store t h e m in a hash table u s i n g t h e h a s h i n g function h ( x )  x mod 113, w h e r e x denotes an account n u m b e r . P r i n t the h a s h table. 11. Read in n s t u d e n t s ' social security n u m b e r s and store t h e m in a h a s h table using the h a s h i n g function h ( x )  x mod 109, w h e r e x d e n o t e s a social security n u m b e r and n is a positive integer _< 100. P r i n t the hash table. 12. Read in the twoletter a b b r e v i a t i o n s of all states in the U n i t e d States. Store t h e m in a hash table of 26 cells, using the hash f u n c t i o n h ( x ) first letter in x. 13. Read in a positive integer n < 15 and a square m a t r i x A of o r d e r n. D e t e r m i n e if it is symmetric. 14. Read in an m x n m a t r i x A and a p x q m a t r i x B. Find A + B, A  B, and AB if they are defined. n
15. The t r a c e of a m a t r i x ( a i j ) n x n is ~ aii. Read in a positive i n t e g e r i=l
n < 20 and an n x n matrix, and p r i n t the trace of the m a t r i x .
Exploratory Writing Projects Using library and I n t e r n e t resources, write a t e a m r e p o r t on each of the following in y o u r own words. Provide a w e l l  d o c u m e n t e d bibliography. 1. Discuss the d e v e l o p m e n t of the concept of a function.
Chapter Summary
183
2. Collect the various rates for water and electricity consumption from neighboring towns and cities. Write each as a word problem and then define each as a function. 3. Describe the history of perfect numbers and their relationship to Mersenne primes. Comment on the existence of odd perfect numbers. 4. Give a number of applications of the floor and ceiling functions to everyday life. 5. Investigate the twoqueens puzzle on an n x n chessboard, where 2 2 cents be paid using two and threecent stamps?
The division algorithm, with which you are already familiar, is often employed to verify the correctness of a division problem. Its proof is based on the following cardinal fact, which is accepted as an axiom. (An a x i o m is a proposition that is accepted as true. It is usually a selfevident proposition and is consistent with known facts.)
185
186
Chapter4 Inductionand Algorithms The WellOrdering Principle Every n o n e m p t y set of positive integers has a least element.
i
For example, the set { 1 3 , 5 , 8 , 2 3 } has a least element, 5. The wellordering principle applies to any n o n e m p t y subset S of T = {n ~ Z Jn _>_no }, where no is any integer. To see this, let S*  {n  no + 1 I n e S} a n d T *  {n  no + 1 In ~ T}. Since S * c_ T * and T * c_ N , by the wellordering principle, S* contains a least element t~*. T h e n no + g*  1 is a least e l e m e n t of S (why?). For example, l e t S  {3,  1 , 0, 1, 3, 5} and T = {n ~ Z l n > 5}. T h e n S*  {3,5,6, 7,9, 11} has a least element t~*  3, s o n 0 + t ~ *  1   5 + 3  1 =  3 is the least element of S. Next we present the division algorithm. Its proof is a bit complicated, so we omit it here; but a proof, using the wellordering principle, can be established (see, for instance, the a u t h o r ' s n u m b e r theory book).
The Division Algorithm When an integer a is divided by a positive integer b, we get a u n i q u e (integer) q u o t i e n t q and a unique (integer) r e m a i n d e r r, where 0 < r < b. The integer a is the dividend and b the divisor. This is formally s t a t e d as follows.
(The Division Algorithm) Let a be any integer and b any positive integer. Then there exist unique integers q and r such t h a t a Dividend

b.q
~
l
+ l
r ~
Divisor
Remainder Quotient
where 0 < r < b.
m
Although this t h e o r e m does not present an algorithm for finding q and r, it has been traditionally called the division algorithm. The values of q and r can be found using the familiar long division method. Notice t h a t the equation a  b q + r can be w r i t t e n as a
r
=q+g
so q = a div b  [a/bJ and r  a  b q  a mod b. The next example shows t h a t we should be careful in finding the q u o t i e n t and the r e m a i n d e r when the dividend is negative.
4.1 The Division Algorithm [ ~ ~ ~ ~ ~
187
Find t h e q u o t i e n t q a n d t h e r e m a i n d e r r w h e n  2 3 is divided by 5. SOLUTION: Since  2 3  5. (  4 ) + (  3 ) , you m i g h t be t e m p t e d to say t h a t q =  4 a n d r   3 . Recall t h a t t h e r e m a i n d e r can n e v e r be negative, so we r e w r i t e  2 3 as  2 3 = 5 . (  5 ) + 2, w h e r e 0 < r ( = 2) < 5 (see t h e n u m b e r line in F i g u r e 4.1). T h u s q =  5 a n d r  2; in o t h e r words,  2 3 div 5   5 and  2 3 mod 5 = 2.
F i g u r e 4.1 A
25T 23
25
15
10
5
0
We close this section w i t h two applications of t h e division a l g o r i t h m a n d the pigeonhole principle, m Let b be an i n t e g e r > 2. If b + 1 distinct i n t e g e r s are r a n d o m l y selected, prove t h a t the difference of some two of t h e m m u s t be divisible by b.
PROOF Let q be t h e q u o t i e n t and r the r e m a i n d e r w h e n an i n t e g e r a is divisible by b. Then, by the division algorithm, a = bq + r w h e r e 0 < r < b. T h e b + 1 distinct integers yield b + 1 r e m a i n d e r s (pigeons); b u t t h e r e are only b possible r e m a i n d e r s (pigeonholes). Therefore, by the pigeonhole principle, two of t h e r e m a i n d e r s m u s t be equal. Let x and y be the c o r r e s p o n d i n g integers. T h e n x = b q l + r a n d y  bq2 + r for some q u o t i e n t s ql a n d q2. T h e n xy
= ( b q l + r)  (bq2 + r)
=b(ql q2) Thus, x  y is divisible by b.
m
Let n be an integer >_ 2 and let a l, a 2 , . . . , a n ~ Z. Prove t h a t t h e r e exist integers k a n d g such t h a t a k + l + ak+2 + " " + ae is divisible by n, w h e r e 1 _< k < t~ < n; t h a t is, t h e r e exist consecutive e l e m e n t s a k + l , a k + 2 , . . . ,ae whose s u m is divisible by n.
P R O O F (by cases): Consider the n s u m s Case 1
S i 
a l + a2 + . . . + a i , w h e r e 1 < i < n.
If a n y of t h e s u m s S i is divisible by n, t h e n t h e s t a t e m e n t is true.
Chapter 4 Induction and Algorithms
188
C a s e 2 S u p p o s e n o n e of t h e s u m s S i is divisible by n. W h e n S i is divided by n, t h e r e m a i n d e r m u s t be n o n z e r o . So, by t h e division a l g o r i t h m , t h e possible r e m a i n d e r s are 1, 2 , . . . , (n  1). Since t h e r e a r e n s u m s a n d n  1 possible r e m a i n d e r s , by t h e p i g e o n h o l e principle, t w o of t h e s u m s S k a n d Se m u s t yield t h e s a m e r e m a i n d e r r w h e n divided by n, w h e r e k < e. T h e r e f o r e , t h e r e m u s t exist i n t e g e r s qz a n d q2 s u c h t h a t a l + a2 + . . . +ak = n q l + r a n d a l + a2 + . . . + ae  n q 2 + r, w h e r e k < e. S u b t r a c t i n g , we get a k + l + ak+2 + " " + ae = n ( q l  q2). T h u s a k + l + ak+2 + " " + ae is divisible by n. m To cite a specific e x a m p l e , c o n s i d e r t h e s e v e n i n t e g e r s 2, 3, 8, 15, 23, 29, a n d 57. T h e n $1 = a l  2 = 0 9 7 + 2 a n d $5 = a l + a2 + a3 ~ a4 + a5 = 2 + 3 + 8 + 15 + 23 = 51 = 7 9 7 + 2. T h e n $5  $1 = a2 + a3 ~ a4 + a5 3 + 8 + 15 + 23  49 is divisible by 7. H e r e k = 1 a n d e = 5. (You m a y n o t i c e t h a t $4 = a l + a2 + a3 F a4 = 2 + 3 + 8 + 15 is also divisible by 7.)
Exercises 4.1 1. Is t h e set of positive odd i n t e g e r s w e l l  o r d e r e d ? 2. Is t h e set of positive even i n t e g e r s w e l l  o r d e r e d ? In E x e r c i s e s 36, find t h e q u o t i e n t a n d t h e r e m a i n d e r w h e n t h e first i n t e g e r is divided by t h e second. 3. 137, 11
4. 15, 23
5.  4 3 , 16
6.  3 7 , 73
F i n d t h e set of possible r e m a i n d e r s w h e n a n i n t e g e r is divided by t h e given integer. 7. T w o
8. Five
9. S e v e n
10. T w e l v e
11. P r o v e t h a t t h e r e exists no i n t e g e r b e t w e e n 0 a n d 1. 12. Let a ~ Z. P r o v e t h a t no i n t e g e r exists b e t w e e n a a n d a + 1. 13. Let no E Z , S be a n o n e m p t y s u b s e t of t h e set T = {n ~ Z ln > no}, a n d ~* be a least e l e m e n t of t h e set T*  {n  no + 1 In e T}. P r o v e t h a t no + 6"  1 is a least e l e m e n t of S. 14. U s i n g t h e w e l l  o r d e r i n g principle, prove t h a t i is t h e s m a l l e s t positive integer. ( H i n t : P r o v e by c o n t r a d i c t i o n . ) "15. Let a ~ Z, S  {a, a + 1,...}, T c S, a n d a ~ T. Let k be a n y e l e m e n t of S s u c h t h a t w h e n e v e r k e T, k + 1 e T. P r o v e t h a t S = T. "16. Let a e Z a n d S = { a , a + 1,...}. Let P(n) be a p r e d i c a t e on S s u c h t h a t t h e following c o n d i t i o n s a r e satisfied: (1) P ( a ) i s t r u e ; (2) I f P ( a ) ,
4.2 DivisibilityProperties
189
P(a + 1),... ,P(k) are t r u e for a n y k > a, t h e n P(k + 1) is also true. Prove t h a t P(n) is t r u e for every n > a.
T h e celebrated euclidean a l g o r i t h m can be used to find the g r e a t e s t c o m m o n divisor of two positive integers, b u t first a very few properties of p r i m e a n d composite n u m b e r s , and some divisibility properties. Let a and b (r 0) be any two integers. If t h e r e is an integer q such t h a t a  b q , we say b d i v i d e s a, b is a f a c t o r of a, a is d i v i s i b l e by b, or a is a multiple of b. We t h e n write b l a ; otherwise, b ya. (Again, the m e a n i n g of the vertical b a r should be clear from the context.) For instance, 316, 8124, b u t 6 y14. A positive factor b of a positive integer a is a proper factor of a if b ~: a. For example, the proper factors of 6 are 1, 2, and 3. T h e r e are positive integers with exactly two positive factors. Accordingly, we m a k e the following definition.
Prime Numbers and Composite Numbers A positive integer > 1 is a prime n u m b e r (or simply a p r i m e ) if its only positive factors are 1 and itself. A positive integer > 1 is a c o m p o s i t e n u m b e r if it is not a prime. For example, 2 and 19 are primes, w h e r e a s 6 and 21 are composite n u m b e r s (why?). T h e r e is a systematic procedure for d e t e r m i n i n g w h e t h e r or not a positive integer n >_ 2 is a prime. It is based on the next theorem. Any composite n u m b e r n has a p r i m e factor _< L~/nJ.
PROOF (by contradiction): Since n is composite, t h e r e are positive integers a and b such t h a t n = a b w h e r e 1 < a < n and 1 < b < n. Suppose a > j ~ and b > ~/n. T h e n n  a b > v/n 9 j ~  n, which is impossible. Therefore, either a < v/n or b < j ~ . Since both a and b are integers, it follows t h a t either a < / v Z n / o r
b_< [J~J. By the f u n d a m e n t a l t h e o r e m of a r i t h m e t i c (see T h e o r e m 4.13), every positive integer has a prime factor. Any such factor of a or b is also a factor of a . b  n, so n m u s t have a p r i m e factor [vZnJ. I
It follows from T h e o r e m 4.2 t h a t if n has n o p r i m e factors < L~r~J, ] t h e n n is a prime; otherwise, it is a composite n u m b e r . This fact can be used to d e t e r m i n e w h e t h e r or not an integer n > 2 is a prime, as the next example illustrates.
190
Chapter 4 Inductionand Algorithms D e t e r m i n e if 1601 is a p r i m e n u m b e r .
SOLUTION: F i r s t list all p r i m e s < Lx/1601J. T h e y are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, and 37. N o n e of t h e m is a factor of 1601 (verify); so 1601 is a prime, m An a l g o r i t h m for d e t e r m i n i n g the p r i m a l i t y of a positive i n t e g e r n >_ 2 is given in A l g o r i t h m 4.1.
Algorithm prime number(n) (* This algorithm determines i f a positive integer n>__2 is prime or not using Theorem 4.2. *) Begin (* algorithm *) l i s t all primes < Lv/n] i f any of them is a factor of n then n is not a prime else n is a prime End (* algorithm *)
Algorithm 4.1
In the r e m a i n d e r of this section we discuss some useful divisibility properties. We begin with a simple and s t r a i g h t f o r w a r d property. If a and b are positive integers such t h a t a i b and b i a, t h e n a  b.
i
Notice t h a t this t h e o r e m does n o t hold if a a n d b are a n y integers. F o r example, 3 i (  3 ) a n d (  3 ) 13, b u t 3 r  3 .
Let a, b, and c be a n y integers. Then: (1) (2) (3) (4)
If a If a If a Ifa
I b and b i c, t h e n a I c ( t r a n s i t i v e p r o p e r t y ) . i b and a I c, t h e n a I (b + c). i b and a I c, t h e n a I (b  c). I b, t h e n a I bc.
PROOF: We shall prove p r o p e r t i e s 1 and 2, and leave the o t h e r s as exercises. (1) Since a lb, t h e r e exists an integer q l such t h a t b = a q ] . Similarly, t h e r e exists an i n t e g e r q2 such t h a t c = bq2. T h e n c = bq2 = ( a q l ) q 2 a ( q l q 2 ) . T h u s , t h e r e exists an integer q = q l q 2 such t h a t c = a q . Therefore, a lc. 

(2) As above, we have b  a q l and c  aq3. T h e n b + c  a q l + aq3 = a ( q l + q3). Since ql + q3 is an integer, it follows t h a t a l(b + c). i
4.2 DivisibilityProperties
191
The Greatest Common Divisor A positive integer can be a factor of two positive integers a and b. Such a positive integer is a c o m m o n f a c t o r of a and b. The largest such common factor is the g r e a t e s t c o m m o n d i v i s o r (gcd) of a and b, denoted by gcd {a, b }. For instance, gcd{6, 9}  3, gcd{12, 24}  12, and gcd{6, 35} = 1. This definition of gcd, although simple and clear, is not practical, so we give an alternate, equivalent definition below.
An Alternate Definition of GCD A positive integer d is the g c d of two positive integers a and b if: 9 d[aandd[b;and
9 if d' [a and d' [ b, then d' I d, where d' is a positive integer. Thus, d is gcd{a, b} if (1) d is a common divisor of both a and b; and (2) any common divisor of a and b is also a divisor of d. The next theorem, an extremely useful and powerful result, can be applied to develop an algorithm to compute gcd{a, b}. Let a and b be any positive integers, and r the remainder when a is divided by b. Then gcd{a, b}  gcd{b, r}.
PROOF Let gcd{a, b} = d and gcd{b, r} = d'. To prove t h a t d = d', it suffices to show that d i d ' and d ' l d . By the division algorithm, a unique quotient q exists such that a = bq + r
(4.1)
To show that d i d ' : Since d = gcd{a, b}, d f a and d I b. Therefore, d f bq, by Theorem 4.4. Then d l(a  bq), again by Theorem 4.4. In other words, d lr, by Equation (4.1). Thus, d Ib and d I r. Therefore, d I gcd{b, r }; that is, d I d'. Similarly, it can be shown that d' Id. (See Exercise 33.) Thus, by Theorem 4.3, d = d'; that is, gcd{a, b} = gcd{b, r}. 1
Illustrate Theorem 4.5, using a = 108 and b  20. SOLUTION: gcd{108, 20} = 4 (verify). When 108 is divided by 20, the remainder is 8. gcd{20, 8}  4 (verify). Thus, gcd{108, 20} = gcd{20, 8}. 1
Euclidean Algorithm Among several procedures for finding the gcd of two positive integers, one efficient algorithm is the e u c l i d e a n a l g o r i t h m , named after the
Chapter 4 Induction and Algorithms
192
.:
\
~,"~'/ }~!! .. ,
:.:..
~'~ ..... "": .....=~: .
.~
.
9
Little is known about Euclid's life. He taught at the University of Alexandria and founded the Alexandrian School of Mathematics. When the Egyptian ruler King Ptolemy I asked Euclid if there were an easier way to learn geometry than by studying The Elements, he replied, "There is no royal road to geometry." Euclid is called the father of geometry. No work, except for the Bible, has been more widely read, studied, or edited," according to J. E. Lightner of Western Maryland College, Westminister, Maryland. "More than 2000 editions of the work have appeared since the first printed one in 1482; however, no extant copy of The Elements dates from Euclid's own time."
Greek m a t h e m a t i c i a n Euclid (330?275 B.c.), who included it in his extrao r d i n a r y work The Elements. The algorithm repeatedly applies the division algorithm and T h e o r e m 4.5. Before formally discussing the algorithm, we illustrate it in the next example. ~
Find gcd{ 1976, 1776}. SOLUTION: Apply the division a l g o r i t h m with 1976 (the larger of the two n u m b e r s ) as the dividend and 1776 as the divisor:
1976 = 1. 1776 + 200 Apply the division algorithm again with 1776 and 200, u s i n g 1776 as the dividend and 200 as the divisor: 1776 = 8. 200 + 176 Continue this procedure until a zero r e m a i n d e r is obtained: 1976 = 1. 1776 + 200 1776=8.
200+176
200=1.
176+24
176=7. 24=3.
24+
last nonzero r e m a i n d e r
8
8+0
The last nonzero r e m a i n d e r gcd{1976, 1776} = 8.
in
this
procedure
is the
gcd.
Thus II
Will this m e t h o d work for any two positive integers a and b? If a  b , t h e n g c d { a , b }  a . So assume, for convenience, a > b. (If this is not true,
4.2 Divisibility Properties
193
simply switch them.) Let ro  b. T h e n by successive application of the division algorithm, we get a sequence of equations: a

qoro + rl
ro = q l r l
0 r3 > ... _> 0
Since the remainders are nonnegative and getting smaller and smaller, this sequence m u s t eventually t e r m i n a t e with r e m a i n d e r rn  O. Thus, the last two equations in the above procedure arer n  2  q n  l r n  l
F rn
0 < rn < r n  1
and rn1
 q n r n
It then follows t h a t gcd{a, b } = gcd{a, r0 }  gcd{r0, rl }  gcd{rl, r2 } . . . . . }  r n , the last nonzero remainder. (This can be established by using mathematical induction; see Exercise 56 in Section 4.4.) gcd{rnl,rn
~
Apply the euclidean algorithm to find gcd{ 2076, 1024}.
SOLUTION: By the successive application of the division algorithm, we get" 2076 = 2. 1024 + 28 1024=36.
28+16
2 8 = 1.
1 6 + 12
16 = 1.
12 + 4
12=3.
4+0
__y>O.*) O. Begin (* algorithm *) i. dividend ~ x 2. d i v i s o r 1. 52. Let n be a positive integer. Prove t h a t (n + 1)! + 2, (n + 1)! + 3 , . . . , (n + 1)! + (n + 1) are n consecutive composite numbers.
In everyday life we use the decimal notation, base ten, to represent any real number. For example, 234  2(102) + 3 (101) + 4(10~ which is the d e c i m a l e x p a n s i o n of 234. Likewise, 23.45  2( 101 ) + 3( 10 ~ + 4( 10 1 ) + 5(102). Computers use base two ( b i n a r y ) , and very long binary n u m b e r s are often handled by h u m a n s (as opposed to computers) using bases eight (octal) and sixteen ( h e x a d e c i m a l ) . Actually, any positive integer b > 2 is a valid choice for a base. This is a consequence of the following f u n d a m e n t a l result. ~
Let b be a positive integer>_ 2. Then every positive integer a can be expressed uniquely in the form a  akb k + a k  1 b k  1 + . . . + a 1b + a0, where ao, a 1 , . . . , ak are nonnegative integers less t h a n b, ak # 0, and k > 0. m This leads us to the following definition.
Baseb Representation The expression a k b k 4 a k  l b k  1 + . . . + a l b + ao is the b a s e  b e x p a n s i o n of the integer a. Accordingly, we write a = ( a k a k  1 " " a l a o ) b in base b. The base is omitted when it is 10. For example, 234 = 234ten and 22 = 10110two (see Example 4.9). When the base is greater t h a n 10, to avoid confusion we use the letters A, B, C , . . . to represent the d i g i t s 10, 11, 12, . . . , respectively. It is easy to find the decimal value of an integer from its baseb representation, as the next example illustrates. [ ~ ~ ~ ~ ~
Express 10110two in base 10.
SOLUTION: 10110two  1(24) + 0(23) + 1(22) + 1(21) + 0(2 ~
1 by condition 1. Since t~' is the least element in S', 6 '  1 r S'; so t~' 1 E S. Consequently, by condition 2, (t~'  1) + 1  t~' e S. This contradiction establishes the theorem, i This theorem can be generalized as in Theorem 4.10. We leave its proof as an exercise. Let no be a fixed integer. Let S be a subset of Z satisfying the following conditions" 9 noeS.
9 If k is an arbitrary integer >_ no such that k E S, then k + 1 E S. Then S ~_ {n E Z ln > n0}.
i
Weak Version of Induction
Before we formalize the principle of induction, let's look at a trivial example. Consider an infinite n u m b e r of dominoes arranged in a row (see Figure 4.16a). Suppose we knock down the first domino. W h a t happens to the rest of the dominoes? Do they all fall? Not necessarily; see Figures 4.16b and c. So let's further assume the following: If the kth domino is knocked down, then the (k + 1)st domino also falls down. If we topple the first domino, what would happen to the rest? They all would fall; see Figure 4.16d. This illustration can be expressed in symbols. Let P(n) denote the predicate t h a t the n t h domino falls. (Note: UD = N.) Assume the following propositions are true" 9 P(1).
9 P(k) ~ P(k + 1) for every positive integer k.
4.4 MathematicalInduction
209
F i g u r e 4.16
a
b
c
d
Then P(n) is true for every positive integer n; t h a t is, every domino would fall. This leads us to the w e a k v e r s i o n of the principle. ( T h e P r i n c i p l e o f M a t h e m a t i c a l I n d u c t i o n ) Let P(n) be a predicate satisfying the following conditions, where n is an integer: (1) P(n0) is true for some integer no. (2) If P(k) is true for an arbitrary integer k >__ no, then P(k + 1) is also true. Then P(n) is true for every integer n >_ no.
PROOF: Let S denote the set of integers >_ no for which P(n) is true. Since P(n0) is true, no e S. By condition 2, whenever k e S, k + 1 ~ S. Therefore, by Theorem 4.10, S consists of all integers >__no. Consequently, P(n) is true for every integer n >_no. This establishes the validity of the principle, m Condition 1 assumes the proposition P(n) is true when n = no. Look at condition 2: If P(n) is true for an arbitrary integer k >__no, it is also true for n = k + 1. Then, by the repeated applications of condition 2 and the law of detachment, it follows t h a t P(n0 + 1), P(n0 + 2), ... all hold true. In other words, P(n) holds for every n >_no. Proving a result by PMI involves two key steps: 1. B a s i s s t e p Verify t h a t P(n0) is true. 2. I n d u c t i o n step Assume P(k) is true for an arbitrary integer k > no ( i n d u c t i v e h y p o t h e s i s ) . Then verify t h a t P(k + 1) is also true.
210
Chapter 4
Induction and Algorithms
A word of caution: A question frequently asked is, "Isn't this cyclic reasoning? Are you not assuming what you are asked to prove?" The confusion stems from misinterpreting step 2 for the conclusion. The induction step involves showing that the implication P(k) * P(k + 1) is a tautology; that is, if P(k) is true, then so is P(k + 1). The conclusion is "P(n) is true for every n > no." So be careful. A variety of interesting examples will show how useful this i m p o r t a n t proof technique is. The next example gives a nice formula for computing the total n u m b e r of cans in the collection in Figure 4.15. Using PMI, prove that, for every positive integer n,
n(n + 1)
1+2+3+.+nP R O O F (by i n d u c t i o n ) : n n ( n + 1) Let P(n)" ~ i  ~ . i=l 2
To verify that P(1) is true (Note" Here no  1)"
Basis step
1
Whenn1
'
RHS
1 (21 + 1) _ 1 _ ~ i  L H S ; s o
P(1) is true.
i=l
I n d u c t i o n s t e p Let k be an arbitrary positive integer. We would like to show that P(k) , P(k + 1)" Assume P(k) is true; that is,
k
k(k + 1) 2
i
_ 1; t h a t is, the f o r m u l a holds for every positive integer n. m F i g u r e 4.17 provides a geometric proof of this f o r m u l a w i t h o u t words. Figure
4.17
S
+
=
S 9",
S

n(n+l) n(n+l) 2
The next example, again an application of induction, employs a divisibility property, so we follow it in some detail. Prove t h a t 2n 3 + 3n 2 + n is divisible by 6 for every integer n > 1. P R O O F (by PMI): Let P(n): 2n 3 § 3n 2 § n is divisible by 6. Basis
step
When
n  1,
2n 3 + 3n 2 + n  2 ( 1 ) + 3(1) + 1  6
is clearly
divisible by 6. Therefore, P(1) is true. step Assume P(k) is true, t h a t is, 2 k 3 + 3 k 2 + k is divisible by 6 for any k > 1. T h e n 2 k 3 + 3 k 2 + k  6 m for some integer m (inductive hypothesis). We m u s t show t h a t P(k + 1) is true; t h a t is, 2(k + 1)3 + 3(k + 1)2 + (k + 1) is divisible by 6. Notice t h a t
Induction
2(k + 1) 3 + 3(k + 1)2 + (k + 1) = 2(k 3 + 3k 2 + 3k + 1) + 3(k 2 + 2k + 1) + (k + 1) = (2k 3 + 3k 2 + k) + 6(k 2 + 2k + 1) = 6m + 6(k 2 + 2k + 1)
by the inductive hypothesis
= 6(m + k 2 + 2k + 1), which is clearly divisible by 6. T h u s P(k + 1) is true. Thus, by induction, the given s t a t e m e n t is t r u e for every n >_ 1.
m
Notice t h a t in the above examples, n o  1, b u t it need not always be 1, as the next example shows.
Chapter4 Inductionand Algorithms
212
J a c o b L B e r n o u l l i (16541705), a member of the most distinguished family of mathematicans (see the family tree in Section 9.1), was born in Basel, Switzerland. His grandfather, a pharmacist in Amsterdam, had become a Swiss through marriage, and his father was a town councilor and a magistrate. Bernoulli received his M.A. in philosophy in 1671 and a theological degree 5years later. During this time, he studied mathematics and astronomy against his father's will. He spent the next 2 years tutoring in Geneva. In 1687 he became professor of mathematics at the University of Basel, .... 9 :,: remaining there until his death. His brother Johann succeeded him at ~" .."" ,,';S" " .~ "'~:::''7~ :S'' Basel. In May 1690 he used the term integral in the calculus sense known today. Bernoulli's most famous work, Ars Conjectandi, was published posthumously in 1713. It contains significant contributions to probability theory, the theory of series, and gravitational theory. .. ::'::,:...,,,
( B e r n o u l l i ' s I n e q u a l i t y ) Let x be a n y real n u m b e r g r e a t e r t h a n  1 . Prove t h a t (1 + x) n > 1 + nx for every n > 0. P R O O F (by PMI)" Let x be any real n u m b e r >  1 . Let P(n)" ( l § n >__ l + n x . (Note" The induction is on the discrete variable n and not on the " c o n t i n u o u s " variable x.)
Basis step
To verify t h a t P(0) is true: Notice t h a t (1 + x) ~  1 > l+0x
So P(0) is true. (Note" H e r e no  0.)
I n d u c t i o n s t e p A s s u m e P(k) is true; t h a t is, (1 + x ) k > 1 + kx for an arbitrary integer k > 0. We need to show t h a t P(k + 1) is true; t h a t is, (1 + x ) k+l > 1 + (k + 1)x. By the inductive hypothesis, we have (1 + x) k > 1 + kx. T h e n (1 + x) k+l  (1 + x)(1 + x) k, >_ (1 + x)(1 + kx),
by IH a n d since 1 + x > 0
= 1 + (k + 1)x + k x 2
> 1 + (k + 1)x,
since kx 2 > 0
Therefore, P(k + 1) is also true. Thus, by PMI, (1 + X ) n >_ 1 + nx for every n >_ 0.
m
T h e next e x a m p l e inductively establishes T h e o r e m 2.3 from C h a p t e r 2.
4.4 Mathematical Induction
~
213
A finite set A with n elements has exactly 2 n subsets. PROOF
(by PMI)"
B a s i s s t e p When n = 0, A = r so A has exactly 1 = 20 subset. T h u s the result is t r u e when n  0. I n d u c t i o n s t e p Assume any finite set with k elements has 2 k subsets, where k > 0. Let A be a set with k + 1 elements. We would like to show t h a t A has 2 k+l subsets. To this end, let x e A. Let B  A  {x}. Since IB ]  k, B has 2 k subsets by the inductive hypothesis. Each of the subsets of B is a subset of A. Now add x to each of them. The resulting 2 k sets are also subsets of A. Since every subset of A either contains x or does not contain x, by the addition principle, A has 2 k + 2 k  2 k+l subsets. Thus, by the principle of induction, the result holds for every finite set. i Both the basis and the induction steps are essential in the principle of induction, as the next two examples illustrate.
Let g(n) denote the m a x i m u m n u m b e r of nonoverlapping regions formed inside a circle by joining n distinct points on it. Figures 4.184.22 show the cases n  1, 2, 3, 4, and 5, where the various regions are n u m b e r e d 1, 2, 3, etc. The results are s u m m a r i z e d in Table 4.1. F i g u r e 4.18
Figure 4.19
214
Chapter 4 Inductionand Algorithms
F i g u r e 4.20
F i g u r e 4.21
l F i g u r e 4.22
1 F i g u r e 4.23
/ It appears from the table t h a t g ( n )  2 n1. T h e n g(1) = 20  1, which is true (basis step). Nonetheless, this does not g u a r a n t e e t h a t g ( n )  2 n1 for every n >_ 1. If the formula were true, there would be g(6)  25  32 nonoverlapping regions with six points. Unfortunately, there are only 31 such regions (see Figure 4.23) We shall derive the correct formula in Chapter 6.
4.4
Table
4.1
215
Mathematical Induction
Number of points n
1
2
3
4
5
6
Maximum number of nonoverlapping regions g(n)
1
2
4
8
16
?
We can conclude t h a t t h e t r u t h f u l n e s s of t h e basis step a n d an a p p a r e n t p a t t e r n do n o t e n s u r e t h a t P(n) is t r u e for every n. m The following example shows t h a t t h e validity of t h e i n d u c t i o n step is necessary, b u t not sufficient, to g u a r a n t e e t h a t P(n) is t r u e for all i n t e g e r s in the UD. Consider the " f o r m u l a " P(n) 9 1 + 3 + 5 + . . . + (2n  1)  n 2 + 1. Suppose k P(k) is true: ~ (2i  1)  k 2 + 1. Then: i=l
k+l ~~(2i 1)
k ~~(2i 1 ) + (2k + 1)
i=l
i=l
= (k 2 + 1) + (2k + 1)  (k + 1) 2 + 1 So if P(k) is true, P(k + 1) is true. Nevertheless, the f o r m u l a does not hold for a n y positive integer n. Try P(1) (see Exercise 5). m U s i n g induction, t h e next e x a m p l e "proves" t h a t every person is of t h e s a m e sex. " P r o v e " t h a t every person in a set of n people is of the s a m e sex. "PROOF"" Let P(n): E v e r y o n e in a set of n people is of t h e s a m e sex. Clearly, P(1) is true. Let k be a positive i n t e g e r such t h a t P(k) is true; t h a t is, everyone in a set of k people is of t h e s a m e sex. To show t h a t P(k + 1) is true, consider a set A  { a l , a 2 , . . . , a k + l } of k + 1 people. P a r t i t i o n A into two overlapping sets, B  { a l , a 2 , . . . , a k } a n d C  { a 2 , . . . , a k + l } , as in F i g u r e 4.24. Since I B I  k  I CI, by the inductive hypothesis, everyone in B is of the s a m e sex and everyone in C is of the s a m e sex. Since B a n d C overlap, everyone in B U C m u s t be of t h e s a m e sex; t h a t is, everyone in A is of the s a m e sex. Thus, by PMI, P(n) is t r u e for every n > 1. This concludes the "proof." m
N o t e : T h e a s s e r t i o n t h a t everyone is of t h e s a m e sex is clearly false.
Can you find t h e flaw in t h e "proof"? See Exercise 46.
Chapter4 Inductionand Algorithms
216 Figure 4.24
Before discussing the second version of the principle of induction, we will look at a few applications of the formula in Example 4.15. First a definition.
Polygonal Number A polygonal n u m b e r is a positive integer n that can be represented by n dots in a polygonal array in a systematic fashion. For example, the integers 1, 3, 6, 10, ... are t r i a n g u l a r n u m b e r s since they can be represented by triangular arrays, as shown in Figure 4.25; the number of pins in a bowling alley and that of balls in the game of pool are triangular numbers. Let t,z denote the nth triangular number. Then t~ = 1 + 2 + 3 + . . . + n =
n ( n + 1)
Figure 4.25
tl=l
t2=3
t3=6
Triangular numbers manifest delightful properties.
t4=lO For example,
tn + t n  1  n2; Figures 4.26 and 4.27 provide a nonverbal, geometric proof
of this result. See Exercises 4750.
Figure 4.26
\
\
\
\
\
4.4 Mathematical Induction
217
F i g u r e 4.27
The next example is a n o t h e r application of the formula in Example 4.15 and the generalized pigeonhole principle. Let a l , a 2 , . . ~ be the first n positive integers in some order. Suppose they are a r r a n g e d a r o u n d a circle (see Figure 4.28). Let k be any positive integer < n. Prove t h a t there exists a set of k consecutive elements in the a r r a n g e m e n t with a sum [ [ k n ( n + 1)  2]/2nJ, where [xJ denotes the floor ofx. F i g u r e 4.28
a.~ a2 an
PROOF: Consider the following sums: S1  a l
+a2
+ ...
$ 2  a2 + a3 + . . .
+ak + ak+l
S n  a n + a l + "'" + a k  1
Each of the first n positive integers appears k times in this set of sums. Then n
ESi
i=1
k
ai
i=1
k
i

, by Example 4.15
218
Chapter4 Inductionand Algorithms Consider kn(n + 1)/2 pigeons. We would like to distribute t h e m among n pigeonholes, called $ 1 , $ 2 , . . . ,Sn. By the generalized pigeonhole principle, at least one of the pigeonholes Si m u s t contain more t h a n Lkn(n § 1)/2n  1/nJ = L[kn(n § 1  2)]/2nJ pigeons. In other words, si > Lkn(n § 1)  2/2n J, as desired, m In particular, if n u m b e r s 1 t h r o u g h 10 are r a n d o m l y placed a r o u n d a circle, at least three consecutive integers in the a r r a n g e m e n t m u s t have a sum exceeding L [ 3 . 1 0 . 1 1  2]/(2.10)J = 16. We now discuss the strong version of the principle of induction.
Strong Version of Induction Sometimes the t r u t h of P(k) might not be enough to establish t h a t of P(k § 1). In other words, the t r u t h f u l n e s s of P(k + 1) may require more t h a n t h a t of P(k). In such cases, we have to assume a s t r o n g e r inductive hypothesis t h a t P(n0), P(no + 1), ... , P(k) are all true; t h e n verify t h a t P(k § 1) is also true. This strong version, which can be proved using the weak version (see Exercise 57), is stated as follows. ( T h e S e c o n d Principle of M a t h e m a t i c a l Induction) Let P(n) be a predicate satisfying the following conditions, where n is any integer: 9 P(n0) is true for some integer no. 9 If k is an arbitrary integer > no such t h a t P(n0) A P(n0 + 1) A . . . A P(k) is true, then P(k + 1) is also true. Then P(n) is true for every n > no. The next theorem illustrates this proof technique,
m
( T h e F u n d a m e n t a l T h e o r e m o f A r i t h m e t i c ) Every positive integer n >_2 either is a prime or can be written as a product of primes. P R O O F ( b y strong induction): Let P(n) denote the given predicate.
Basis s t e p
Choose n o  2. Since 2 is itself a prime, P(2) is true.
Inductive step Let k be a positive integer > 2 such t h a t P(2), P(3), . . . , P(k) are true; t h a t is, assume t h a t integers 2 t h r o u g h k are primes or can be written as products of primes. We would like to show t h a t P(k + 1) is also true; t h a t is, integer k + 1 is a prime or can be expressed as a product of primes. If k + 1 is itself a prime, t h e n we are done. If k + 1 is not a prime, it m u s t be the product of two positive integers x and y, where 1 < x , y < k + 1. By the inductive hypothesis, both x and y are primes or products of primes. Therefore, k + 1  x • is also a product of two or more prime numbers. In other words, P(k + 1) also holds: Thus, by the strong version of induction, P(n) is true for every n > 2. m
4.4 Mathematical Induction
219
We now present an interesting application of the f u n d a m e n t a l t h e o r e m of arithmetic, which is the cornerstone of n u m b e r theory, and the floor function. Find the n u m b e r of trailing zeros in 123! SOLUTION: By the f u n d a m e n t a l t h e o r e m of arithmetic, 123! can be factored as 2a5bc, where c denotes the product of primes other t h a n 2 and 5. Clearly a > b. Each trailing zero in 123! corresponds to a factor of 10 and vice versa. .'. N u m b e r of trailing zeros
_ f N u m b e r of products of the form~ \ 2 . 5 in the prime factorization ]  m i n i m u m of a and b b,
sincea>b
We proceed to find b" N u m b e r of positive integers < 123 and divisible by 5  [123/5J  24 Each of t h e m contributes a 5 to the prime factorization of 123! N u m b e r of positive integers < 123 and divisible by 25  [123/25J  4 (See Figure 4.29.) Each of t h e m contributes an additional 5 to the prime factorization. Since no higher power of 5 contributes a 5 in the prime factorization of 123!, the total n u m b e r of 5's in the prime factorization equals 24 + 4 = 28. Thus the total n u m b e r of trailing zeros in 123! is 28. Figure 4.29
each contributes a 5
123!= 1 2 3 . . . 1 2 0 . . . 1 1 5 . . . 1 0 0 . . . 9 5 . . . 7 5 . . . 5 0 . . . 2 5 . . . 1 0 . . . 5 . . . 1
each contributes an additional 5 m
The next example is a n o t h e r interesting application of the floor function. It employs the following facts from n u m b e r theory: 9 Every positive integer that is not a square has an even n u m b e r ofpositive factors. For example, 18 has six positive factors: 1, 2, 3, 6, 9, 18; 21 has four: 1, 3, 7, 21; 19 has two: 1, 19. 9 Every perfect square has an odd n u m b e r of positive factors. For example, 25 has three positive factors, namely, 1, 5, and 25; 64 has seven: 1, 2, 4, 8, 16, 32, and 64.
220
Chapter 4
Induction
and
Algorithms
9 T h e r e a r e [~/nJ p e r f e c t s q u a r e s 1. n
5. ~ ( 2 i  1 ) = n i=1
2
n [ (n+l)~ 2 7. ~_~i3  ~ i=1 2 9. n 2 + n is divisible by 2.
n
6. ~~i2= i=1
(n + 1)(2n + 1)
n 8. ~~ a r i  l = i=1
6 a(rn_l) r
(r#
1)
1
10. n 4 + 2n 3 + n 2 is divisible by 4.
11. The n u m b e r of lines formed by joining n (> 2) distinct points in a plane, no t h r e e of which being collinear, is n ( n  1)/2. 12. T h e n u m b e r of diagonals of a convex ngon* is n ( n  1)/2 > 3. 13. Let a be a positive integer a n d p a p r i m e n u m b e r such t h a t p [a n. T h e n p [ a , w h e r e n > 1. (Hint: Use Exercise 37 in Section 4.2.) *An ngon is a polygon with n sides. An ngon such that the line segment joining any two points inside it lies within it is a convex polygon.
222
Chapter4 Inductionand Algorithms 14. P r o v e t h a t 1 + 2 + . . . + n  n ( n + 1)/2 by c o n s i d e r i n g t h e s u m in t h e r e v e r s e order.* (Do n o t u s e i n d u c t i o n . ) Evaluate each sum. 30 50 15. ~ (3k 2  1) 16. ~ (k 3 + 2) k=l k=l
n
n
17. ~ Li/2]
18. ~ [i/21
i=l
i=l
F i n d t h e v a l u e of x r e s u l t i n g f r o m e x e c u t i n g e a c h a l g o r i t h m f r a g m e n t .
19.
x ~0
20.
i=l
for
x + x +
21.
x ~0
t o n do (2i
for

1)
i=l
t o n do
x _ 2) cents can be made using two and threecent stamps. (Hint: Use the division algorithm and induction.) *56. Let a and b be any two positive integers with a>__b. Using the sequence of equations in the euclidean algorithm prove t h a t god {a, b }  god {r~_ 1, rn }, n >_ 1. *57. Prove the strong version of mathematical induction, using the weak version. *58. Prove the weak version of induction, principle.
using the wellordering
**59. Let Sn denote the sum of the elements in the nth set of the sequence of sets of squares { 1 }, {4, 9 }, { 16, 25, 36 }, . . . . Find a formula for Sn. (J. M. Howell, 1989) **60. Redo Exercise 59 using the sequence of triangular numbers {1}, {3,6}, {10,15,21}, . . . . (J.M. Howell, 1988)
Suppose we wrote an algorithm to solve a problem and translated the algorithm into a computer program. Since it is impossible to test the program for all sets of input values, we rely on a mathematical proof to ensure that the program will always yield the correct output. The principle of induction can certify the correctness of algorithms. Correct P r o g r a m
A c o r r e c t program yields the correct result for all legal input values, assuming the program contains no compilation and execution errors.
4.5 AlgorithmCorrectness
225
Proving the correctness of a program, especially a complex one, is not at all an easy task. It consists of two steps: (1) Proving t h a t the program will always terminate; and (2) proving t h a t it will always produce the correct result. The second step constitutes the partial c o r r e c t n e s s of the program.
Loop Invariant First, we will establish the partial correctness of simple w h i l e loops. Let n denote the n u m b e r of iterations of a w h i l e loop. Assume a predicate P(n). A relationship among the variables holds true before the loop is executed and after each iteration of the loop, no m a t t e r how large n is. As the algorithm execution progresses, the values of the variables in the loop may vary, but the relationship remains unaffected. Such a predicate is a loop invariant. To prove t h a t P(n) is a loop invariant, we apply PMI, as the next two examples demonstrate.
•
Algorithm 4.6 computes the product of two positive integers x and y. Notice t h a t the values of the variables x and y are not affected by the loop in lines 37. But the values of i and a n s w e r do get changed during each iteration of the loop. Algorithm m u l t i p l i c a t i o n ( x , y ) (* This algorithm computes the product of the p o s i t i v e integers x and y, and p r i n t s the answer. *) O. Begin (* algorithm *) i. answer O. Basis step
W h e n n  0, f a c t ( 0 )  1  1! by line 1; so P(0) is true.
227
4.5 AlgorithmCorrectness Induction step
Assume P(k) is true: f a c t ( k )  k!. Then:
fact(k + 1) = fact(k) 9(k + 1), by line 6 = k!. (k + 1), by the inductive hypothesis = (k + 1)~ Therefore, P(k + 1) is true. Thus, by induction, P(n) holds t r u e for every n _>_O; t h a t is, P(n) is a loop invariant and hence the algorithm correctly computes the value of n!, for every n >__O. I
Searching and Sorting Algorithms The r e m a i n d e r of this section establishes the partial correctness of a few s t a n d a r d searching and sorting algorithms. We begin with two searching algorithms, linear and binary.
Linear Search Algorithm L e t X = [ X l , X 2 , . . . , X n ] be an u n o r d e r e d list (also known as a onedimensional a r r a y or simply an array) of n distinct items. We would like to search the list for a specific item, called key. If key exists in the list, the algorithm should r e t u r n the location of key. We search the list from right to left for convenience. Compare Xn and key. If Xn  key, key occurs and location = n. Otherwise, compare Xn1 and key. If they are equal, we are done. Otherwise, continue the search until it is successful or the list is empty. This algorithm is the linear search
algorithm. For example, let X = [Dallas, Boston, Nashville, Albany, Portland] and
key = Albany. T h e n key occurs in the list at location 4. In general, we cannot assume key occurs in the list. To make the search process always successful, we store key in location 0: xo ~ key. So if the search routine r e t u r n s the value zero for location, it implies key does not occur in the list. An iterative version Algorithm 4.8.
of the
linear
search algorithm
is given in
Algorithm linear search (X,n,key,location) (* This algorithm searches a l i s t by the l i n e a r search method f o r a key and returns i t s location in the l i s t . To make the search always successful, we store key in xo. I f the algorithm returns the value 0 for l o c a t i o n , key does not occur in the l i s t . *) O. Begin (* algorithm *) i. x0 0.
P R O O F (by PMI)" Let P(n)" The algorithm r e t u r n s the correct location for every list of size n>0. D
B a s i s step When n = 0, the w h i l e loop is skipped. The algorithm r e t u r n s the value 0 in location by line 5, which is correct. So P(0) is true. I n d u c t i o n step Assume P(k) is true for an arbitrary integer k > 0; t h a t is, the algorithm works when the list contains k items. To show that P(k + 1) is true, consider a list X with k + 1 elements. Case 1 Ifxk+l = key in line 3, the w h i l e loop will not be entered and the algorithm r e t u r n s the correct value k + 1 for location in line 5. Case 2 Ifxk+l r key, i  k at the end of the first iteration. This restricts us to a sublist with k elements. By the inductive hypothesis, the algorithm works correctly for such a list. In both cases, P(k + 1) holds. Thus, by induction, P(n) is true for n >_0. In other words, the algorithm r e t u r n s the correct location for every list with n > 0 elements.
Binary Search Algorithm The b i n a r y s e a r c h a l g o r i t h m searches for a given key if the list X is ordered. The technique employed is d i v i d e and c o n q u e r . First compute the m i d d l e (mid) of the list, where m i d = [(1 + n)/2J. The middle item is Xmid. Now partition the list into three disjoint sublists: [x 1 , . . . , Xmid1], [Xmid], and [Xmid+l,., Xn]. If Xmidkey, the search is successful and location = mid. If they are not equal, we search only the lower half or the upper half of the list. If key < Xmid, search the sublist [xl, . . . , Xmid1]; otherwise, search the sublist [Xmid+l,..., Xn]. Continue like this until the search is successful or the sublist is empty. ~
se the binary search algorithm to search the list X = {3, 5, 8, 13, 21, 34, 55, 89] for key = 5.
SOLUTION" Let xi denote the ith element of the list X, where 1 < i < n and n  8.
4.5 Algorithm Correctness
Step 1
229
C o m p u t e m i d for t h e list X:
m i d  L(1 + n)/2]  L(1 + 8)/2]  4. Therefore, t h e middle t e r m is Xmid  13. S t e p 2 C o m p a r e Xmid a n d key: Since x4 r 5, key, if it occurs, m u s t exist in t h e lower sublist [Xl,X2,X3]  [3, 5, 8] or in t h e u p p e r sublist [x5,x6,x7,x8] = [21, 34, 55, 89]. Since key < x4, search t h e first sublist a n d c o n t i n u e steps 1 a n d 2 u n t i l e i t h e r key is located or t h e sublist becomes empty. Step 3
C o m p u t e m i d for t h e list [Xl,X2,X3]"
mid
[(1 + 3)/2]  2
So Xmid  x2  5. S t e p 4 C o m p a r e Xmid a n d key: Since Xmid = key, the search is successful. Key occurs at location 2 and we are done. (As an exercise, use the a l g o r i t h m to search t h e list X with key = 23.) m The steps in this e x a m p l e can be t r a n s l a t e d into an a l g o r i t h m . See A l g o r i t h m 4.9. Algorithm binary search(X,l,n,key,mid) (* This algorithm searches an ordered l i s t X of n elements f o r a special item (key). I t returns the location of key i f the search is successful and zero otherwise. The variable mid returns such a value. The variables low and high denote the lower and upper indices of the l i s t being searched. *) O. Begin (* algorithm *) 1. low 0.
P R O O F (by strong induction): Let P(n)" The algorithm works for every ordered list of size n. B a s i s step When n  0, l o w  1 and h i g h  O. Since l o w < h i g h is false in line 3, the w h i l e loop is not executed. So the algorithm r e t u r n s the correct value 0 from line 14, as expected, and P(0) is true. I n d u c t i o n step Assume P(i) holds for every i < k, where k > 0; t h a t is, the algorithm r e t u r n s the correct value for any list of size i < k. To show t h a t P(k + 1) is true, consider an ordered list X of size k + 1. Since h i g h = k + 1 > 1 = low, the loop is entered and the middle index is computed in line 5.
Case 1 If k e y   X m i d , we exit the loop (line 7) and the value of m i d is returned, so the algorithm works. Case 2 If key < Xmid, search the sublist x l, . . . , Xmid1; otherwise, search the sublist Xmid+l, ... , X,z. In both cases, the sublists contain fewer t h a n k + 1 elements, so the algorithm works in either case by the inductive hypothesis. Thus P(k + 1) is true. So, by PMI, P(n) is true for n >_ 0; t h a t is, the algorithm works correctly for every ordered list of zero or more items, i Next we present two s t a n d a r d sorting algorithms and prove their correctness.
Sorting Algorithms Suppose we are given a list ofn items and would like to sort t h e m in "ascending order." Several methods are available. Two algorithms t h a t can do the job are bubble sort and selection sort.
Bubble Sort B u b b l e sort is a simple, elegant algorithm for sorting a list of n items. It "bubbles up" smaller items to the top and pushes larger items to the bottom: Compare consecutive elements, beginning with the first pair. Swap t h e m if they are out of order. Compare the next pair and swap t h e m if necessary. Continue like this to the end of the list. This ends the first pass. Now place the largest element at the end of the list. Repeat these steps with all but the largest element until the resulting sublist consists of one element. The list is now ordered.
4.5 AlgorithmCorrectness
231
The following example demonstrates this method. Using bubble sort, sort the list X = [34, 13, 21, 3, 89]. SOLUTION: Let xi denote the ith element in the list, where 1 < i < 5. The given list is 1
Step 1
Compare Xl and
X2.
2
Since Xl 1
2
3
4
> X2,
3
5
swap them. This yields the list
4
5
Now comparex2 andx3. Sincex2 > x3, interchangex2 andx3. This produces the list 1
Since
X3 > X4,
2
3
4
5
switch them, yielding the list 1
2
3
4
5
Compare X4 and x5. Since X4 < X5, they are in the correct order and no interchanging is needed. This completes the first pass. At the end of the first pass, the largest element in the list is placed in proper position: 1 X
2
3
4
5
11312113134189] in correct position
to b e s o r t e d
S t e p 2 In the second pass, compare the elements xl through X4 and swap them if necessary. This results in the two largest elements being placed correctly: 1
2
3
to be sorted
4
5
correctly sorted
232
Chapter 4 Induction and Algorithms
S t e p 3 The t h i r d pass involves t h e e l e m e n t s x l t h r o u g h this pass, the t h r e e largest e l e m e n t s are correctly placed: 1
2
~
3
9
to be sorted
Step 4
4
X 3.
At t h e end of
5
y
,J
in correct order
At the end of t h e f o u r t h pass the list is completely sorted: 1
2
3
4
5
all in correct order
Two i m p o r t a n t observations"
9 At the end of t h e i t h pass, t h e i largest e l e m e n t s are correctly placed at the end of t h e list, w h e r e 1 < i < n. So t h e (i + 1)st pass involves t h e e l e m e n t s Xl t h r o u g h Xn_ i . 9 Bubble sort t a k e s n  1 passes to sort a list of n items, even if the list becomes o r d e r e d at t h e end of the ith pass, w h e r e i < n  1. Once the list is sorted, it m a k e s no sense to go t h r o u g h t h e r e m a i n ing passes, so t h e additional passes can be avoided w i t h a boolean variable.
T h e various steps in E x a m p l e 4.30 can be developed into an a l g o r i t h m for bubble sort, as p r e s e n t e d in A l g o r i t h m 4.10.
Algorithm bubble sort(X,n) (* This algorithm sorts a l i s t X of n elements using the bubble algorithm. *) O. Begin (* algorithm *) 1. for i   1 to n  1 do 2. for j = l to n  i do 3. i f Xj > Xj+ 1 then 4. swap xj and Xj+ 1 5. End (* algorithm *)
Algorithm 4.10
4.5 Algorithm Correctness
233
Establish the correctness of the bubble sort algorithm. P R O O F (by PMI)" Let P(n)" The algorithm sorts every list of size n > 1. B a s i s s t e p When n  1, the list contains just one element and hence is clearly sorted, so P(1) is true. I n d u c t i o n s t e p Assume P(k) is true; t h a t is, the algorithm sorts correctly every list of k (> 1) items. To show t h a t P(k + 1) is true, consider a list X  [ X l , X 2 , . . . , Xk+l]. Since k + 1 > 2, the f o r loop in line 1 is entered. When i  1, j runs from 1 t h r o u g h n  1. Lines 3 and 4 are executed: the consecutive elements xj and Xj+l are compared and swapped if out of order. The inner f o r loop places the largest of the elements Xl, x2,..., Xk+l in position k + 1. This leaves a sublist of k elements, [xl, x2, . . . , Xk]. By the inductive hypothesis, the algorithm correctly sorts it. It follows t h a t the algorithm correctly sorts the entire list X; t h a t is, P(k + 1) is true. Thus, by the principle of induction, P(n) is true for n > 1; that is, the bubble sort algorithm always works, m
Selection
Sort
Unlike bubble sort, s e l e c t i o n s o r t finds the largest element and swaps it with xn if xn is not the largest element. Find the largest of the remaining elements Xl, x2,..., Xn1, and switch it with Xn1 if it isn't xn1. Continue like this until the list is completely sorted. In each pass, unlike in bubble sort, if two elements are out of order, we do not swap t h e m right away but wait to find the largest element of the sublist. At the end of the ith pass, the largest of the elements xl, x 2 , . . . , X n  i + l is swapped with X n _ i + l , where 1 < i < n. This outline of the selection sort algorithm can be a bit refined. In the ith pass, initially assume Xni+l is the largest element. Find the largest of the elements x l, x 2 , . . . , Xni. Swap it with Xni+l if necessary. Algorithm 4.11 results. Algorithm selection sort(X,n) (* This algorithm sorts a l i s t X of n items using the i t e r a t i v e version of selection sort. Maxindex denotes the index of the largest element in a given pass. *) O. Begin (* algorithm *) 1. i f n > i then(* l i s t contains at least two elements *) 2. for i = 1 to n  1 do
234
Chapter4 Inductionand Algorithms 3. 4.
begin (* for *) maxindex ~ n  i + i (* assume Xn_i+ 1 is the largest element; save its index. *) for j1 to n  i do i f Xj > Xmaxindex, then (* update maxindex *) maxindex ~ j i f maxindex r i + i , then (* found a larger element; swap the corresponding elements *) swap Xmaxindex and Xn_i+ 1
5. 6. 7. 8. g. I0. 11.
endfor End
(* algorithm *) A1gori thm 4. i i
~
Establish the correctness of Algorithm 4.11.
P R O O F (by PMI): Let P(n)" The algorithm works correctly for every list of size n _> 1. B a s i s s t e p When n = 1, the list contains one element and is clearly sorted, so P(1) is true. I n d u c t i o n s t e p Assume P(k) is true; t h a t is, the algorithm sorts correctly every list of size k >__1. To show that P(k + 1) is true, consider a list X = x x, x2, . . . , Xk+l] with k + 1 elements, where k + 1 _> 2. Since k + I >_ 2, the condition in line 1 is satisfied, and we enter the loop in line 2. When i = 1, m a x i n d e x = (k + 1) 1 + 1 = k + 1. The f o r loop in lines 57 compares each of the elements Xl, x 2 , . . . , xh with Xmaxindex and updates it as needed. Line 8 updates m a x i n d e x if we have found an element larger than Xh+l. If maxindex ~: k + 1, then the elements xk+ 1 and Xmaxindex are swapped. This stores the largest of the k + 1 elements in position k + 1, leaving a sublist of k elements, namely, x 1, x2,.. 9 xh to be sorted. Therefore, by the inductive hypothesis, the algorithm sorts correctly the list X containing k + 1 elements. Thus, by induction, P(n) is true for every n >_ 1; t h a t is, the algorithm correctly sorts every list of size n. B These searching and sorting algorithms are pursued again in Section 4.7. Additional sorting algorithms appear in the exercises.
Exercises 4.5 Prove that the given predicate P(n) in each algorithm is a loop invariant.
1.
Algorithmexponential (x,n) (* This algorithm computes xn, where xEIR + and n ~W. *)
2.
Algorithm divi sion(x,y) (* This algorithm computes the quotient and the remainder when a positive
4.5 Algorithm Correctness
235
0. Begin (* algorithm *) i. answer + I 2. while n > 0 do 3. begin (* while *) 4. answer + answer, x 5. n _ n], and ]f2(n)l _< C2[g2(n)] for n >_ n2. Let C  C1C2 and no  max{n],n2}. Then" ](f]. f 2 ) ( n ) i  ]f](n). f2(n)i = [fl(n)]. ]f2(n)]
_ no T h u s (fl" f2)(n)  O(gl(n)g2(n)).
I
The next two examples employ this h a n d y t h e o r e m along with the earlier theorems. Let f ( n ) = 6n 2 + 5n + 7 lg n!. E s t i m a t e the g r o w t h off(n). SOLUTION: Since 6n 2  O(n 2) and 5n  O(n), 6n 2 + 5n  O(n 2) by T h e o r e m 4.15. F u r t h e r m o r e , 7  O(1), and lg n!  O(n lg n) by Example 4.34. So 7 lg n! = O(1). O(n lg n) = O(1. n lg n), by T h e o r e m 4.16 = O(n lg n)
4.6 The Growthof Functions
243
Since lg n < n, n lg n < n 2 for n > 1 (see Figure 4.31), it follows by T h e o r e m 4.15 t h a t f ( n )  O(n 2) + O(n lg n)  O(n2). i Let f ( n )  (3n 2 + 4n  5) lg n. E s t i m a t e the g r o w t h off(n).
SOLUTION: 3n 2 + 4n  5  O(n2), by T h e o r e m 4.14 Clearly, lg n  O(lg n) So f ( n )  (3n 2 + 4n  5) lg n
= O(n2) 9O(lg n) = O ( n 2 lg n), by T h e o r e m 4.16
II
We now t u r n to the bigomega and the bigtheta n o t a t i o n s for investigating the g r o w t h of functions.
The BigOmega and BigTheta Notations The bigoh notation has been widely used in the study of the g r o w t h of functions; however, it does not give us an exact order of growth. F o r instance, f(n)O(g(n)) j u s t implies t h a t the function f does not grow any faster t h a n g. In other words, it simply provides an u p p e r b o u n d for the size of f ( n ) for large values of n, b u t no lower bound. W h e n we need the lower bound, we employ the bigomega notation. W h e n we need both b o u n d s to estimate the growth of f, we use the bigt h e t a notation. Both n o t a t i o n s were introduced in the 1970s by Donald K n u t h of Stanford University. We now p u r s u e the bigomega notation. As you could imagine by now, its definition closely resembles t h a t of the bigoh notation; it can be obtained by simply changing _< to >_.
The BigOmega Notation Let f , g " • ~ R. Suppose t h e r e is a positive c o n s t a n t C and a positive integer no such t h a t If(n)] >__C i g ( n ) i for every n >_ no. T h e n f ( n ) is g2(g(n)); t h a t is, f ( n )  ~ ( g ( n ) ) . [As above, read this as f ( n ) is bigomega of g(n).] The following example illustrates this definition. Let f ( n ) C50
50n 3  6n + 23. W h e n n >_ 0, 50n 3  6n + 23 > 50n 3. So, with
and g ( n )  n 3, it follows t h a t f ( n ) >__ C . g ( n ) for every n >_ 0. T h u s f ( n )  g2(n3). (Notice t h a t here no  0.) i
244
Chapter 4 Induction and Algorithms
D o n a l d E r v i n K n u t h (1938), a pioneer in the development of the theory of compilers, programming languages, and the analysis of algorithms, is also a prolific writer in computer science. He was born in Milwaukee, Wisconsin, where his father, the first college graduate in the Knuth family, taught bookkeeping at a Lutheran high school; his talent for mathematics and music played a significant role in the intellectual development and pursuit of the young Knuth. As a youngster, Knuth had a marvelous gift for solving complex problems. As an eighth grader, he entered the Ziegler's Candies Contest to find the number of words that can be formed from the letters in Ziegler's Giant Bar. Knuth listed 4500 such words, 2000 more than in Ziegler's master list. This won a television set for the school and enough Ziegler candy for the entire student body. In high school, Knuth entered the prestigious Westinghouse Science Talent Search (now Intel Science Talent Search) with his project, The Prtrzebie System of Weights and Measures, that would replace the cumbersome British system. His project won an honorable mention, and $25 from MAD Magazine for publishing it. When he graduated from high school, he was already an accomplished mathematician, musician, and writer. He majored in physics at the Case Institute of Technology (now Case Western Reserve University) and was introduced to an IBM 650 computer, one of the earliest mainframes. After studying the manual from cover to cover, he decided that he could do better and wrote assembler and compiler code for the school's IBM 650. In 1958, Knuth developed a system for analyzing the value of a basketball player, which the coach then used to help the team win a league championship. Newsweek wrote an article about Knuth's system and Walter Cronkite carried it on the CBS Evening News. In his sophomore year, Knuth switched his major to mathematics. His work at Case was so distinguished that when he was awarded his B.S. in 1960, the faculty made an unprecedented decision to grant him an M.S. concurrently. Knuth then entered the California Institute of Technology for graduate work and received his Ph.D. in mathematics 3 years later. He joined the faculty there, also consulting for the Burroughs Corporation writing compilers for various programming languages, including ALGOL 58 and F O R T R A N H. From 19681969, he worked at the Institute for Defense Analyses, Princeton, New Jersey. In 1969, Knuth joined the faculty at Stanford University. Knuth's landmark project, The Art of Computer Programming, was initiated by AddisonWesley Publishing Co. in early 1962, while he was still in graduate school. Dedicated to the study of algorithms, it would be a sevenvolume series when completed. A revered work, it was the pioneer textbook in the 1970s and continues to be an invaluable resource. Knuth developed two computer languages to deal with mathematics typography, TEX, a typesetting program, and Metafont, a program to develop the shapes of letters. He has received numerous honorary degrees from universities around the world: the Grace Murray Hopper Award (1971), the Alan M. Turing Award (1974), the Lester R. Ford Award (1975), the National Medal of Science (1979), the McDowell Award (1980), the Computer Pioneer Award (1982), and the Steele Prize (1987). An accomplished church organist and composer of music for the organ, Knuth retired from Stanford in 1992.
245
4.6 The Growth of Functions
We now m a k e an i n t e r e s t i n g observation. To this end, let f ( n ) = f~(g(n)); so If(n)[ >_ Clg(n)[ for n >_ no. T h e n Ig(n)l _< C'[f(n)[ for some positive c o n s t a n t C t = 1/C; so g ( n ) = O ( f ( n ) ) . Conversely, let g ( n ) = O(f(n)). By r e t r a c i n g these steps, it follows t h a t f(n) = ~ ( g ( n ) ) . T h u s f(n) = f~(g(n)) if and only i f g ( n )  O(f(n)). We now define the bigtheta notation, using the bigoh and bigomega notations.
The BigTheta Notation
Let f , g 9 N + R such t h a t f ( n )  O(g(n)) and f ( n ) = gz(g(n)). T h e n f ( n ) is said to be of o r d e r g(n). We t h e n write f ( n ) =  read this as f(n) is bigtheta of g(n). The next two examples illustrate this definition. Let f ( n )  (3n 2 + 4n  5) lg n. By Example 4.37, f ( n )  O(n 2 lg n). W h e n n > 1, we also have: (3n 2 + 4n  5)lg n > 3n 2 lg n T h a t is, f ( n ) > 3(n 2 lg n)
So f ( n )  f2(n 2 lg n)
T h u s f(n)  O(n 2 lg n)  f2(n 2 lg n), so f(n)  
2 lg n).
II
Let f(n) show the n u m b e r of times the a s s i g n m e n t s t a t e m e n t x ~ x + 1 is executed by the nested for loops in Example 4.35. Recall t h a t f ( n ) n ( n + 1)/2  O(n2). Since n + 1 >_ n for every n >_ 1, it follows t h a t n ( n + 1)/2 >_ n2/2; so f(n)  f2(n2). T h u s f(n)   II
We now m a k e two i n t e r e s t i n g observations from Examples 4.39 and 4.40: 9 Iff(n) is a polynomial in n of degree m, t h e n f ( n ) =O(nm). 9 f(n)=  if and only if Alg(n)] _ 2 and select b + i integers at random. Find two integers in the list such t h a t their difference is divisible by b. 2. Read in an integer n >_ 2 and select n positive integers at random. Find a sequence of integers from the list whose sum is divisible by n. 3. Read in a positive integer > 2 and determine if it is a prime.
258
Chapter 4 Induction and Algorithms
4. Determine if each value of f ( n ) n_ 4
7. The nth L u c a s n u m b e r L n , named after the French mathematician Fran~oisEdouardAnatole Lucas, is defined recursively as follows" L1  1, Ln
L23
Ln1 + Ln2, n > 3
(The Lucas sequence and the Fibonacci sequence satisfy the same recurrence relation, but have different initial conditions.) Compute the first six Lucas numbers. The gcd of two integers x (> 0) and y (_> 0) can be defined recursively as follows: gcdly,
gcd{x,y} =
I
xl
x
g c d { y , x m o d y}
ify>x i f y _< x a n d y  0 i f y _< x a n d y > 0
Using this definition, compute the gcd of each pair of integers. 8. 28, 18
9. 24, 75
274
Chapter 5 Recursion
F r a n ~ ' o i s  E d o u a r d  A n a t o l e L u c a s (18421891) was born in Amiens, France. After completing his studies at the E,cole Normale in Amiens, he worked as an assistant at the Paris Observatory. He served as an artillery officer in the FrancoPrussian war and then became professor of mathematics at the Lycde SaintLouis and Lycde Charlemagne, both in Paris. A gifted and entertaining teacher, Lucas died of a freak accident at a banquet: His cheek was gashed by a piece of a plate that was accidentally dropped, and he died from infection within a few days. Lucas loved computing and developed plans for a computer that never materialized. Besides his contributions to number theory, he is known for his fourvolume classic on recreational mathematics. Best known among the problems he developed is the Tower of Brahma.
A person deposits $1000 in a b a n k at an a n n u a l i n t e r e s t r a t e of 6%. Let A ( n ) denote the c o m p o u n d a m o u n t she will receive at t h e e n d of n i n t e r e s t periods. Define A ( n ) recursively if i n t e r e s t is compounded: 10. S e m i a n n u a l l y
11. Q u a r t e r l y
12. M o n t h l y
Ned deposits a certain a m o u n t A0 in a b a n k at an a n n u a l i n t e r e s t r a t e of 12% c o m p o u n d e d annually. The c o m p o u n d a m o u n t he would receive at the end of n y e a r s is given by A,,  1.12A,,_1, w h e r e n >_ 1. D e t e r m i n e the initial deposit A0 if he would receive" 13. $1804.64 at the end of 5 years.
14. $3507.00 at t h e end of 6 years.
Define recursively each sequence of n u m b e r s . (Hint: Look for a p a t t e r n a n d define the n t h t e r m a,, recursively.)
15. 1, 4, 7, 10, 13 ...
16. 3, 8, 13, 18, 23 ...
17. 0, 3, 9, 21, 45 ...
18. 1, 2, 5, 26, 677 ...
19. An nbit word c o n t a i n i n g no two consecutive ones can be c o n s t r u c t e d recursively as follows" Append a 0 to such (n  1)bit words or a p p e n d a 01 to such (n  2)bit words. U s i n g this procedure c o n s t r u c t all 5bit words c o n t a i n i n g no two consecutive ones. T h e r e are 13 such words. Define each recursively, w h e r e n > 0. 20. T h e n t h power of a positive real n u m b e r x. 21. The u n i o n of n sets. 22. The intersection of n sets. 23. The n u m b e r Sn of s u b s e t s of a set with n elements. 24. The n t h t e r m an of an a r i t h m e t i c sequence with first t e r m a a n d c o m m o n difference d.
5.1 RecursivelyDefined Functions
.. "~'.. ~.),:: Z
L', ,',
:, '~,.
'1
"~.
"
~. .,
9....
"
b
~?, "
~
"4
"t .~ ~'i ~ ', ..
275
John McCarthy (1927), one of the fathers of artificial intelligence (AI), was born in Boston. He graduated in mathematics from the California Institute of Technology, receiving his Ph.D. from Princeton in 1951. After teaching at Princeton, Stanford, Dartmouth, and MIT, he returned to Stanford as a full professor. While at Princeton, he was named a Proctor Fellow and later the Higgins Research Instructor in mathematics. At Stanford, he headed the Artificial Intelligence Laboratory. During his tenure at Dartmouth, McCarthy coined the term artificial intelligence (AI). He developed LISP (LISt Programming), one of the most widely used programming languages in AI. He also helped develop ALGOL 58 and ALGOL 60. In 1971 he received the prestigious Alan M. Turing award for his outstanding contributions to data processing.
25. The n t h t e r m an of a geometric sequence with first t e r m a and c o m m o n ratio r. 26. Let f 9X ~ X be bijective. Define fn recursively, where f2 _ f o f. The 9 1  f u n c t i o n f, invented by J o h n McCarthy, is defined recursively on W as follows. f(x) = I x 
10
i f x > 100
[ f (f (x + l l ) )
if0_<x_ 4 40. F 2  F n  l F n + l = (  1 ) n  l , n _> 2 41. F5n is divisible by 5, n >__1. 42.
F~ < a n
1, n
_> 3
43. Fn _ 1 44" L e t A  [
11 ~] T h e n A n  "
Fn + 1 Fn
Fn 1 , n >__1. Assume F0 = 0. Fn1
45. Using Exercise 44, deduce t h a t F,~+1 g n  1 
F2

( 1)n.
(Hint" Let A be a square matrix. Then [An[  In[ n, where [A[ denotes the d e t e r m i n a n t of A.) 2n2
46. Ln = F n + l + F n  l , n
47. L2n  3 +
>_ 2
~
Lk
k=l ~n _ ~n
The n t h term bn of a n u m b e r sequence is defined by bn 
where
a  (1 + v/5)/2 and fl  (1  v/5)/2 are solutions of the equation x 2 = x + 1. Verify each. 48. bl = 1
49. b 2  1
50. b n  bn1 + bn2, n >_ 3
(It follows from Exercises 4850 t h a t bn = Fn. It is called the B i n e t f o r m of the n t h Fibonacci number, after the French m a t h e m a t i c i a n JacquesPhillipeMarie Binet.) With a and fl as above, let U n   a n + fin, n > 1. Verify each. 51.
Ul = 1
52.
u2
3
53.
Un = Un1
+ Un2, n >_ 3
[These exercises indicate t h a t Un = Ln, the n t h Lucas number. Accordingly, the Binet form of L n . ]
U n   a n Jr f i n i s
5.1
Recursively Defined Functions
277
Jacques Phillippe Marie Binet (17861865), a French mathematician and astronomer, was born at Rennes, Brittany. In 1804, he entered the Ecole Polytechnique in Paris, graduated 2 years later, and took a job in the Department of Bridges and Roads of the French government. In 1807, Binet became a teacher at the E,cole Polytechnique, and the following year became assistant to the professor of applied analysis and descriptive geometry. In 1814, he was appointed examiner of descriptive geometry, then professor of mechanics (1815), and then inspector general of studies .A (1816). In 1821, he was awarded the Chevalier de la Ldgion d'Honneur. :: ,~ ~ Two years later, Binet was appointed chair of astronomy at the Coll~ge de ~" ":~. France. ~ y But the July 1830 revolution was not kind to him. A strong supporter of Charles X, Binet became a victim of Charles' abdication; he was dismissed from Ecole Polytechnique by King LouisPhiUipe in November, 1830. Binet made many contributions to mathematics, physics, and astronomy. In 1812, he discovered the rule for matrix multiplication and, in 1840, discovered the explicit formula for the nth Fibonacci number. In 1843, he was elected to the Academy of Sciences and later became its president. A devout Catholic, Binet died in Paris.
54. Let al, a2, . . . , an E 1~, where n > 2. Prove that gcd{al, a2, . . . , an}   g c d { g c d { a l , a2, . . . , an1},an}. Using Exercise 54 compute the gcd of each set of numbers. 55. 6, 12, 20, 38
56. 12, 28, 48, 104, 252
Let an denote the n u m b e r of times the assignment statement x 1.
m
M o r e generally, y o u m a y verify t h a t t h e solution of t h e r e c u r r e n c e r e l a t i o n a n  C a n  1 + 1, w h e r e c is a c o n s t a n t (r 1), is Cn
an

+
c n aO

1
c1
F o r i n s t a n c e , in E x a m p l e 5.12, b0  0 a n d c  2, so 2 n  1 bn 
2n "O + ~
=2 n 1
21 as expected. Let us p u r s u e E x a m p l e 5.12 a bit f u r t h e r . S u p p o s e t h e r e a r e 64 d i s k s at peg X, as in t h e original puzzle, a n d it t a k e s 1 second to m o v e a disk f r o m one peg to a n o t h e r . T h e n it t a k e s a t o t a l of 264  1 s e c o n d s to solve t h e puzzle. To get a n idea how incredibly large t h i s total is, notice t h a t t h e r e a r e about 365.24.60.60  31,536,000 seconds in a year. T h e r e f o r e , T o t a l t i m e t a k e n  264  1 seconds 1. 8 4 4 6 7 4 4 0 7 x 1019 seconds 5 . 8 4 9 4 2 4 1 7 x 1011 y e a r s 600 billion years! I n t r i g u i n g l y , a c c o r d i n g to s o m e e s t i m a t e s , 18 billion y e a r s old.
t h e u n i v e r s e is only a b o u t
Exercises 5.2 U s i n g t h e i t e r a t i v e m e t h o d , p r e d i c t a s o l u t i o n to each r e c u r r e n c e r e l a t i o n s a t i s f y i n g t h e given initial condition. 1. s 0  1 Sn 
2. a l = l 2Sn1, n > 1
5.
an1
+ n, n > 1
a n  a n  1
+
(2n  1), n >_ 2
6. s l = l
aoO an 
+ n, n > 2
4. a 1 = 1
3. a 0  1 an 
an an1
an1
+ 4n, n > 1
Sn   ~ S n 
1 + n3, n _> 2
5.2 Solving Recurrence Relations
283
7. s l = l
8. a 1   1
8n = S n  1 + n 2 , n > 2
an  2 a n  1
+ (2 n 
1), n >_ 2
9  1 6 . Using induction, verify the solutions to Exercises 18. 17. Using the data in Example 5.2, show t h a t the c o m p o u n d a m o u n t J u d y will receive at the end o f n years is given b y A ( n ) = 1000(1.08) n, w h e r e n>0.
Use the recursive definition of fn in E x a m p l e 5.5 to a n s w e r Exercises 18 and 19. 18. Predict a formula for fn. 19. Prove t h a t the formula holds for n > 1. 20. Using induction, establish the explicit f o r m u l a for bn in E x a m p l e 5.12. Using induction, prove t h a t each is a solution to the c o r r e s p o n d i n g recurrence relation, where c is a c o n s t a n t and f ( n ) a function of n. n
21.
an

ao + ~ f(i),
an
 an1
+
f (n)
i=1
22. a n  c n a o
+
cn  1 ~ anCan_l cl'
+ l ( a s s u m e c ~: l )
n
23. an  c n a o + ~ c n  i f (i), an  C a n  1 b f (n) i=1
Let an denote the n u m b e r of times the s t a t e m e n t x ~ x + 1 is executed by the following loops. for
i
= 1 to
n do
f o r j = 1 to [ i / 2 ] x~x+l
do
24. Define an recursively. 0 25. Show t h a t an =
an1 + n/2 anl+(n1)/2
if n  1 if n > 1 and even if n > l a n d o d d
26. Solve the recurrence relation satisfied by an. Let an denote the n u m b e r of times the s t a t e m e n t x ~ x + 1 is executed by the following f o r loops: for
i
= 1 t o n do
f o r j = 1 to [ i / 2 ] do x,x+1
27. Define an recursively. 1 28. Show t h a t an 
an1 + n/2 a n  1 + (n + 1)/2
if n  1 if n > 1 and even if n > l a n d o d d
284
Chapter 5 Recursion 29. Solve the recurrence relation satisfied by an. Let an denote the number of times the statement x ~ x + 1 is executed by the nested f o r loops in Exercise 35 in Section 4.4. 30. Define an recursively. 31. Solve the recurrence relation satisfied by an. 3233. Redo Exercises 30 and 31 using the loops in Exercise 36 in Section 4.4. 3435. Redo Exercises 30 and 31 using the loops in Exercise 37 in Section 4.4. 3637. Redo Exercises 30 and 31 using the loops in Exercise 38 in Section 4.4. Let tn denote the nth triangular number. 38. Define tn recursively. 39. Find an explicit formula for tn. 40. Prove that 8tn + 1 is a perfect square. The nth p e n t a g o n a l n u m b e r Pn is obtained from its predecessor by adding three rows of dots plus one. The first four pentagonal numbers are represented pictorially in Figure 5.11.
Figure 5.11 9
9
9
9
9
9
9
9
9 9
9
pl1
9
9
P2 = 5
9
9
9
9
9
9
9
9
9
9
9
9
P 3 = 12
9
9
9
9
9
9
9
9
P4 = 22
41. Represent P5 pictorially. 4243. Redo Exercises 38 and 39 usingpn. The nth h e x a g o n a l n u m b e r hn is obtained from its predecessor by adding four rows of dots plus one dot. The first four hexagonal numbers are shown pictorially in Figure 5.12. 4446. Redo Exercises 4143 using h n . 47. Prove that hn  Pn + tn  n, using the explicit formulas for Pn and tn. 48. Prove that hn  Pn + tn  n, using the recurrence relations for Pn and tn.
285
5.2 SolvingRecurrence Relations Figure
5.12
9
hl = 1
h2 = 6
9
9
h 3 = 15
9
9
9
9
Q
h 4 = 28
Triangular pyramidal n u m b e r s Tn (or t e t r a h e d r a l numbers) a r e positive i n t e g e r s t h a t can be r e p r e s e n t e d by t r i a n g u l a r p y r a m i d a l s h a p e s . T h e first f o u r t e t r a h e d r a l n u m b e r s a r e 1, 4, 10, a n d 20; see F i g u r e 5.13.
Figure 5.13
T1 1
T2 = 4
T 3 = 10
T 4 = 20
49. Define Tn r e c u r s i v e l y . 50. C o n j e c t u r e a n explicit f o r m u l a for Tn. 51. E s t a b l i s h t h e f o r m u l a in E x e r c i s e 50.
Square pyramidal n u m b e r s Sn a r e positive i n t e g e r s t h a t c a n be repr e s e n t e d by p y r a m i d a l s h a p e s , w h e r e t h e b a s e is a s q u a r e . T h e first f o u r s q u a r e p y r a m i d a l n u m b e r s a r e 1, 5, 14, a n d 30; see F i g u r e 5.14. 5254.
Redo Exercises 4951 with
Sn.
L e t an d e n o t e t h e n u m b e r of s u b s e t s of t h e set S  {1, 2, . . . , n } t h a t c o n t a i n no c o n s e c u t i v e i n t e g e r s , w h e r e n > 0. W h e n n  0, S  O.t C o m p u t e each. 55. a0
56. a l
57. a2
t Proposed by Irving Kaplansky of The University of Chicago.
58. a3
286
Chapter 5 Recursion
F i g u r e 5.14 a
? ....
~

e
9
9
.
S1 = 1
.
S 2 =
59. Define
.
~
  
.
S 3 = 14
5
an
. . . .
S 4 =
30
recursively.
60. Solve the recurrence relation satisfied by an. Suppose we introduce a mixed pair of lmonthold rabbits into a large enclosure on the first day of a certain month. By the end of each month, the rabbits become m a t u r e and each pair produces k  1 mixed pairs of offspring at the beginning of the following month. ( N o t e : k >_ 2.) For instance, at the beginning of the second month, there is one pair of 2monthold rabbits and k  1 pairs of 0montholds; at the beginning of the third month, there is one pair of 3montholds, k  1 pairs of lmontholds, and k ( k  1) pairs of 0montholds. Assume the rabbits are immortal. Let a,~ denote the average age of the rabbit pairs at the beginning of the nth month. (P. Filipponi, 1990) * ' 6 1 . Define a , recursively. **62. Predict an explicit formula for a,~. **63. Prove the formula in Exercise 64. 64. (For those familiar with the concept of limits) Find lim
an.
n  ~
Unfortunately, the iterative method illustrated in the preceding section can be applied to only a small and simple class of recurrence relations. The present section develops a method for solving two large, i m p o r t a n t classes of recurrence relations.
5.3 SolvingRecurrence Relations Revisited
287
Linear Homogeneous Recurrence Relations with Constant Coefficients (LHRRWCCs) l i n e a r h o m o g e n e o u s r e c u r r e n c e r e l a t i o n w i t h cons t a n t c o e f f i c i e n t s is a r e c u r r e n c e r e l a t i o n of t h e f o r m
A kthorder
an
 Clan1
4 C 2 a n  2
4 " ' " 4 C k a n  k
(5.7)
w h e r e Cl, C2,... ,Ck E R a n d ck r 0. First, a few w o r d s of e x p l a n a t i o n : T h e t e r m l i n e a r m e a n s t h a t e v e r y t e r m on t h e R H S of E q u a t i o n (5.7) c o n t a i n s at m o s t t h e first p o w e r of a n y p r e d e c e s s o r a i . A r e c u r r e n c e r e l a t i o n is h o m o g e n e o u s if e v e r y t e r m on t h e R H S is a m u l t i p l e of s o m e a i ; in o t h e r w o r d s , t h e relation is satisfied by t h e s e q u e n c e {0}; t h a t is, a n  0 for e v e r y n. All coefficients ci a r e c o n s t a n t s . Since a n d e p e n d s on its k i m m e d i a t e predecessors, t h e o r d e r of t h e r e c u r r e n c e r e l a t i o n is k. Accordingly, to solve a k t h  o r d e r L H R R W C C , we will n e e d k initial conditions, say, a0  Co, a l  e l , . . . , a k  1   C k  1 . T h e n e x t e x a m p l e i l l u s t r a t e s in detail t h e v a r i o u s t e r m s in t h i s definition. 9 T h e r e c u r r e n c e r e l a t i o n Sn  2Sn1 is a L H R R W C C . Its o r d e r is one. 9 T h e r e c u r r e n c e r e l a t i o n a n  n a , ~ _ l is l i n e a r a n d h o m o g e n e o u s . B u t t h e coefficient on t h e R H S is not a c o n s t a n t . T h e r e f o r e , it is not a LHRRWCC. 9
hn h n  1 + ( n  1) is a l i n e a r r e c u r r e n c e relation. B u t it is not h o m o g e n e o u s b e c a u s e of t h e t e r m n  1.
9 T h e r e c u r r e n c e r e l a t i o n a n  a n2_ 1 + 3 a n  2 not l i n e a r since t h e p o w e r of an1 is 2. 9 an  an1
4 2 a n  2
4 3 a n  6
is h o m o g e n e o u s . B u t it is
is a L H R R W C C of o r d e r six.
m
Before we discuss solving s e c o n d  o r d e r L H R R W C C s , notice t h a t t h e solution of t h e r e c u r r e n c e r e l a t i o n Sn  2 S n  1 , w h e r e so  1, is Sn  2 n, n >_ 0 (see Exercise 1 in Section 5.2). M o r e generally, you m a y verify t h a t t h e solution of t h e r e c u r r e n c e r e l a t i o n a n  o t a n _ l , w h e r e a0  c, is a n  c a n, n>0. We now t u r n o u r a t t e n t i o n to t h e s e c o n d  o r d e r L H R R W C C an
 aan_l
~ b a n  2
(5.8)
w h e r e a a n d b a r e n o n z e r o c o n s t a n t s . If it h a s a n o n z e r o solution of t h e f o r m t h e n c a n  acot n  1 + b c o t n  2 . Since ca r 0, t h i s yields c~2  a a + b; t h a t is, ot 2   a a  b  0, so a m u s t be a solution of t h e c h a r a c t e r i s t i c e q u a t i o n
c a n,
X 2  ax 
b 
0
(5.9)
Chapter 5 Recursion
288
of the r e c u r r e n c e relation (5.8). The roots of E q u a t i o n (5.9) are t h e c h a r a c t e r i s t i c r o o t s of r e c u r r e n c e relation (5.8). T h e o r e m s 5.2 t h r o u g h 5.4 show how characteristic roots help solve LHRRWCCs. ~
Let a and fl be the distinct (real or complex) solutions of the e q u a t i o n 0, where a, b e R and b 4: 0. T h e n every solution of the LHRRWCC a n  a a n  1 + b a n  2 , where a0  Co and al  C1, is of the form an  A a n + B fl n for some c o n s t a n t s A and B.
x 2  ax  b 
PROOF"
The proof consists of two parts: 9 First, we will show t h a t a n = A(~ n + B f l n is a solution of the r e c u r r e n c e relation for any c o n s t a n t s A and B. 9 We will t h e n find the values of A and B satisfying the given initial conditions. First, notice t h a t since c~ and fl are solutions of equation (5.9), and ~ 2 __ a f t + b. 9 T o s h o w t h a t an  A a n + B f l n is a s o l u t i o n
~2 _
of the recurrence
a~ + b
relation"
a a n  1 + b a n  2  a ( A ~ '~1 + B f l n  l ) + b ( A a n  2 + B f l n  2 ) = Ac~'~2(acv + b) + B f l n  2 ( a f l = Ac~,~2 . ol2 + B f l n  2
+ b)
f12
= Aol '~ + B f l n
T h u s an  A a n + B f l n is a solution of the recurrence relation (5.8). 9 Secondly, let an  A~x n + B f l n be a solution of (5.8). To find the values of A and B, notice t h a t the conditions a0  Co and a l  C1 yield the following linear system" Co  A + B
(5.10)
C1  A a + B f l
(5.11)
Solving this system, we get (Verify.) A 
C1  C o i l
~Z
and C0d  C1 (Remember, a 4= ft.)
at~
With these values for A and B, an satisfies the initial conditions and the recurrence relation. Since the recurrence relation and the initial conditions d e t e r m i n e a u n i q u e sequence, { a n } , an  A a n + B f l n is indeed the u n i q u e solution of the r e c u r r e n c e relation, m
5.3
Solving Recurrence Relations Revisited
289
A few interesting observations:
9 T h e s o l u t i o n s a a n d ~ a r e n o n z e r o , since a = 0, for i n s t a n c e , w o u l d i m p l y t h a t b = 0. 9 T h e o r e m 5.2 c a n n o t be applied if a  ft. H o w e v e r , it w o r k s even if a n d fl a r e c o m p l e x n u m b e r s . 9 T h e s o l u t i o n s otn a n d fin a r e t h e b a s i c s o l u t i o n s of t h e r e c u r r e n c e relation. In g e n e r a l , t h e n u m b e r of basic s o l u t i o n s e q u a l s t h e o r d e r of t h e r e c u r r e n c e relation. T h e g e n e r a l s o l u t i o n an  Aot n 4 B f l n is a l i n e a r c o m b i n a t i o n of t h e basic solutions. T h e p a r t i c u l a r solution is o b t a i n e d by selecting A a n d B in s u c h a w a y t h a t t h e initial c o n d i t i o n s a r e satisfied, as in T h e o r e m 5.2. T h e n e x t t h r e e e x a m p l e s i l l u s t r a t e h o w to LHRRWCCs using their characteristic equations. Solve t h e r e c u r r e n c e r e l a t i o n an  5 a n  1  6 a n  2 ,
solve
secondorder
w h e r e a0  4 a n d a l  7.
SOLUTION: 9 To find the general solution of the recurrence relation:
T h e c h a r a c t e r i s t i c e q u a t i o n of t h e r e c u r r e n c e r e l a t i o n is x 2  5x + 6  0; t h e c h a r a c t e r i s t i c roots a r e 2 a n d 3. T h e r e f o r e , by T h e o r e m 5.2, t h e g e n e r a l solution of t h e r e c u r r e n c e r e l a t i o n is an = A . 2 n + B . 3 n. (This solution is u s e d in E x a m p l e s 5.19 a n d 5.20.) 9 To find the values of A and B:
U s i n g t h e initial c o n d i t i o n s we find: a0 = A + B = 4 a l = 2 A + 3B = 7 Solving this l i n e a r s y s t e m yields A  5 a n d B   1 (Verify this.). T h u s t h e solution of t h e r e c u r r e n c e r e l a t i o n s a t i s f y i n g t h e given conditions is an  5 . 2 n  3 n, n >_ O. m T h e n e x t e x a m p l e finds a n explicit f o r m u l a for t h e n t h Fibonacci n u m b e r F n , w h i c h we h a v e b e e n w a i t i n g for.
Solve t h e Fibonacci r e c u r r e n c e F1 = 1  F2.
relation Fn

Fn1
+ Fn2,
where
SOLUTION: T h e c h a r a c t e r i s t i c e q u a t i o n of t h e r e c u r r e n c e r e l a t i o n is x 2  x  1  O, a n d 1+~/5 1~/5 its solutions are a = 2 a n d fl  ~ . You m a y verify a + fl = 1 a n d aft   1. T h e g e n e r a l solution is F n  A a n + B f l n. To find A a n d B, we have: F1  Aot + B fl 
1
F2  A ot 2 }B fl 2 
1
290
Chapter 5 Recursi0n
Solving these two equations, we get (Verify)" A
a
_ (1 + v/5)/2 (5 + vf5)/2
1 + c~2 (1 + v ~ ) ( 5 
v~)
5 + 5~/5 v~
(5 + v ~ ) ( 5  j 5 ) and similarly B 
_ 1 + j5 5 + ~/5
25  5
fl 1 + ~2 
5
1 j~
1
v/~ (Verify this.).
T h u s t h e solution of t h e r e c u r r e n c e relation satisfying t h e given conditions is o,n _ _ ~n c~n _ _ ~n an
~

which is the B i n e t f o r m for the n t h Fibonacci n u m b e r Fn. (See E x a m p l e 5.26 for a different method.) m The next example, proposed by Irving K a p l a n s k y of T h e U n i v e r s i t y of Chicago, also illustrates solving second order L H R R W C C s a n d is closely related to E x a m p l e 5.15. ~
{ 1, 2 , . . . n} t h a t do not Let an denote the n u m b e r of subsets of the set S contain consecutive integers, w h e r e n >__ 0. W h e n n  0, S  0 . Find an explicit f o r m u l a for a,,. SOLUTION: To get an idea about a,,, let us find its value for n  0, 1, 2, 3, and 4 by c o n s t r u c t i n g a table, as in Table 5.3. It a p p e a r s from the table t h a t an is a Fibonacci n u m b e r and a,,  F,+2.
Table 5.3
S u b s e t s of S t h a t do n o t contain consecutive integers
an
D,
D,{I} D, {I}, {2} O, {i}, {2}, {3}, {1,3} O, {I}, {2}, {3}, {4}, {1,3}, {1,4}, {2,4} Fn
+2
We shall, in fact, prove t h a t an  Fn+2 in two steps" F i r s t we shall define an recursively and t h e n solve the r e c u r r e n c e relation to obtain this explicit formula. 9 To define an recursively" F r o m Table 5.3, a0 = 1 and a l  2. So let n >_ 2. Let A be a subset of S t h a t does not contain two consecutive integers. T h e n e i t h e r n e A or nCA.
5.3
Solving Recurrence Relations Revisited
291
C a s e 1 Suppose n ~ A. T h e n n 1 r A. By definition, S* = {1, 2 , . . . , n  2} has a n  2 subsets not c o n t a i n i n g two consecutive integers. Add n to each of the subsets. T h e r e s u l t i n g sets are subsets of S satisfying t h e desired property, so S has a n  2 such subsets. C a s e 2 Suppose n r A. By definition, t h e r e are a n  1 such subsets of S h a v i n g the r e q u i r e d property. Since these two cases are m u t u a l l y exclusive, by t h e addition principle, an
 an
Thus
an
1 zr a n  2 .
can be defined recursively as a0  1 , a l  2 an 
9 To solve
the recurrence
n>2.
a n  1 + a n  2 ,
relation"
This r e c u r r e n c e relation is exactly the s a m e as t h e Fibonacci one w i t h the initial conditions a0 = 1, a l = 2. So i n s t e a d of going t h r o u g h a complete solution, as in E x a m p l e 5.15, notice t h a t this definition yields the Fibonacci n u m b e r s 1, 2, 3, 5, 8, . . . . It follows t h a t an = F n + 2 , n > 0. m
U s i n g the values of a and ~ from E x a m p l e 5.15, an+2
an
Fn+2

_
fin+2
n > 0 u
,
a~
(Verify this. See Exercise 13.)
m
T h e o r e m 5.2 does not work if the c h a r a c t e r i s t i c roots a and fl are equal, t h a t is, if a is a root w i t h degree of multiplicity two. T h e following t h e o r e m , however, comes to our rescue. It shows t h a t , in addition to a n , n a n is a basic solution. Let a, b ~ I~ and b r 0. Let a be a real or complex solution of the e q u a t i o n a x  b = 0 w i t h degree of multiplicity two. T h e n a n the general solution of the L H R R W C C a n  a a n _ 1 ~ b a n  2 .
x 2 
PROOF" Since a is a root of t h e e q u a t i o n x 2 city two, x 2 
ax
b =
ax 
b 
(x
a)2
X 2 
2ax +
0
Aa n + Bnan
is
with degree of multipli
a 2
Therefore, a  2a
and
b

a
2
(5.12)
Chapter 5 Recursion
292
9 To show
that
an 
na n satisfies
the recurrence
relation:
1)a n  l ] +

Notice t h a t aan1
+ ban2

a[(n

= 2a[(n

l)a nl]
b[(n
~
2)a n2]
(a2)[(n  2)a n2] by (5.12)
= an[2(n  1)  (n  2)]  na n 
Therefore,
na n
an
is a solution of t h e r e c u r r e n c e relation.
T h e n a n  A a n + B n f l n is t h e general solution of t h e given r e c u r r e n c e relation, w h e r e A and B are selected in such a way t h a t t h e initial conditions are satisfied. (The values of A a n d B can be found u s i n g initial conditions, as in T h e o r e m 5.2.) n The next example i l l u s t r a t e s T h e o r e m 5.3. Solve the r e c u r r e n c e relation
an 
6an 1
9an2,
w h e r e a0 = 2 a n d a 1
3.
SOLUTION: The characteristic e q u a t i o n of the r e c u r r e n c e relation is x 2  6x + 9  0; its solution is 3 with degree of multiplicity two. Therefore, by T h e o r e m 5.3, the general solution of the r e c u r r e n c e relation is a , , = A . 3 '2 + B 9 n 3 n . (We use this in E x a m p l e 5.21.) The initial conditions a0 = 2 and a l = 3 yield the e q u a t i o n s A.3 ~ +B. 0.3 ~  2 and
A.3+B.1.33.
Solving these equations, we get A  2 and B   1 . (Verify). T h u s the solution of t h e r e c u r r e n c e relation satisfying t h e given conditions is a n  2 . 3 n  n . 3 n , n >_ O. m T h e o r e m s 5.2 and 5.3 can be combined to yield t h e following general result. ~
Let a be a c h a r a c t e r i s t i c root of t h e L H R R W C C (5.7). 9 If the degree of multiplicity of c~ is 1, t h e n a n is a basic solution of the LHRRWCC. 9 If t h e degree of multiplicity of a is m, t h e n a n, n a n , . . . , n m  l a n are basic solutions of t h e L H R R W C C . ( N o t e : A k t h  o r d e r L H R R W C C has k basic solutions.) 9 T h e general solution of the L H R R W C C is a linear c o m b i n a t i o n of all basic solutions, m The following e x a m p l e illustrates this general t h e o r e m .
5.3 Solving Recurrence Relations Revisited
293
Solve t h e r e c u r r e n c e r e l a t i o n an  7 a n  1  1 3 a n  2  3 a n  3 + 1 8 a n  4 , w h e r e a0  5 , a l  3 , a 2  6, a n d a3   2 1 . SOLUTION: T h e c h a r a c t e r i s t i c e q u a t i o n of t h e L H R R W C C i s x 4  7X 3 + 13X 2 +3X 18  0. Since x 4  7 x 3 + 13x 2 + 3x  18  (x + 1)(x  2)(x  3) 2, t h e c h a r a c t e r i s t i c r o o t s are:  1 a n d 2 w i t h d e g r e e of m u l t i p l i c i t y o n e e a c h and
3 w i t h d e g r e e of m u l t i p l i c i t y t w o
Since 3 is a r o o t w i t h d e g r e e of m u l t i p l i c i t y two, it y i e l d s t w o b a s i c s o l u t i o n s , 3 n a n d n 3 n. T h u s t h e g e n e r a l s o l u t i o n of t h e L H R R W C C is a l i n e a r c o m b i n a t i o n of t h e basic s o l u t i o n s (  1 ) n , 2 n , 3 n, a n d n3n; t h a t is, an = A (  1 ) n + B 2 n + C3 n ~ D n 3 n. T o f i n d the v a l u e s o f A, B, C, a n d D: Since a0  5, a l  3, a2  6, a n d a3   2 1 , w e h a v e A+B+C
=5
 A + 2B + 3C + 3D = 3 A + 4B + 9C + 18D = 6 and
A
+ 8B + 27C + 81D =  2 1
S o l v i n g t h i s l i n e a r s y s t e m , we g e t A  2  C, B  1, a n d D   1 (Verify this.). T h u s t h e s o l u t i o n of t h e L H R R W C C s a t i s f y i n g t h e i n i t i a l c o n d i t i o n s is an  2 (  1 ) n _+_ 2 n _+_2 . 3 n  n 3 n, n >_ O. m T h e t e c h n i q u e of s o l v i n g L H R R W C C s c a n n o t b e a p p l i e d to t h e s e e m i n g l y s i m p l e r e c u r r e n c e r e l a t i o n s fn  f n  1 + n ( E x a m p l e 5.5) a n d bn  2 b n  1 + 1 ( E x a m p l e 5.4), w h i c h a r e l i n e a r , b u t n o n h o m o g e n e o u s . So we n o w t u r n to solving l i n e a r n o n h o m o g e n e o u s r e c u r r e n c e r e l a t i o n s w i t h c o n s t a n t coefficients (LNHRRWCCs).
LNHRRWCCs T h e g e n e r a l f o r m of a L N H R R W C C is an  Clan1 + C2an2 + "'" + Ckank + f (n)
(5.13)
w h e r e cl, C 2 , . . . , Ck E R, Ck 7s O, a n d f ( n ) is n o t i d e n t i c a l l y zero. I t s s o l u t i o n d e p e n d s on t h a t of t h e a s s o c i a t e d l i n e a r h o m o g e n e o u s recurrence relation with constant coefficients (ALHRRWCCs) an  Clan1 + C2an2 + "'" + Ckank
we s t u d i e d e a r l i e r .
(5.14)
294
Chapter 5 Recursion
Solving LNHRRWCCs To solve the LNHRRWCCs (5.13), let a(nh) denote the general solution of the ALHRRWCCs (5.14). Suppose we know some solution a(np) of t h e r e c u r r e n c e relation (5.13); a(np) is a p a r t i c u l a r s o l u t i o n of the L N H R R W C C s (5.13). T h e n the g e n e r a l s o l u t i o n of (5.13) is given by an
a (nh) + a (np)
This fact is confirmed by the following theorem; we leave its proof as an exercise (see Exercise 44). Let a(nh) denote the general solution of the ALHRRWCCs (5.14) and a(np) a particular solution of the L N H R R W C C (5.13). T h e n an  a(nh) + a(np) is the general solution of the L N H R R W C C s (5.13). I It follows from this t h e o r e m t h a t solving the L N H R R W C C s (5.13) depends on finding a particular solution a~np . Although no general algor i t h m exists for solving an a r b i t r a r y LNHRRWCCs, two special cases can be handled fairly easily. When f ( n ) is a polynomial in n or is of the form Cot n, a particular solution can be extracted with ease, as the next two examples demonstrate, where C and a are constants. The techniques we employ are similar to those used to solve linear nonhomogeneous differential equations. Solve the LNHRRWCCs an al7.
5an1
 6a~_2 + 8n 2, where a0 
4 and
SOLUTION: It follows from Example 5.14 t h a t the general solution of the ALHRRWCCs an  5 a n  1  6 a n  2 is given by a(nh/ = A . 2 n + B . 3 n. Since f ( n )  8n 2 is a quadratic polynomial in n, it seems reasonable to look for a particular solution of the same form, say, an  a n 2 + b n + c . T h e n the given recurrence relation yields an 2 +bn +c

5 1 a ( n  1) 2 + b ( n 
=
(8a)n
1)+c]61a(n2)
2 +(14ab)n
2 +b(n2)+c]+8n
2
19a+ 7bc
E q u a t i n g the coefficients of like terms, we get the linear systema8a b = 14ab
c = 19a + 7bc Solving the system, we get a  4, b  28, and c  60 (Verify). We now claim t h a t t(P) 4n 2 + 28n + 60 is a particular solution (Verify). tn 
5.3
Solving Recurrence Relations Revisited
295
T h u s , by T h e o r e m 5.5, t h e g e n e r a l s o l u t i o n of t h e given r e c u r r e n c e r e l a t i o n is an
a (nh) + a (np)
= A . 2n + B .
3 n + 4n 2 + 28n + 60
U s i n g t h e two given initial conditions, t h i s yields t h e l i n e a r s y s t e m : A +B

56
2A + 3B   8 5 T h i s yields A   8 3 a n d B  27 (Verify t h i s also.). T h u s t h e d e s i r e d solution is an 
f(n)
(  8 3 ) . 2 n + 2 7 . 3 n ~ 4n 2 A 28n + 60,
n > 0
m
T h e n e x t e x a m p l e i l l u s t r a t e s h o w to solve t h e L N H R R W C C s (5.13) w h e n is of t h e f o r m C a n, w h e r e C a n d a a r e c o n s t a n t s .
Solve t h e L N H R R W C C s a n al=7.
5an1
6an2
+
3 . 5 n , w h e r e a0 
4 and
SOLUTION: As in E x a m p l e 5.19, t h e g e n e r a l solution of t h e A L H R R W C C s a n  5 a n  1 6 a n  2 is given by a(nh)  A . 2 n 4 B . 3 n. Since f ( n )  3 . 5 n, we s e a r c h for a p a r t i c u l a r solution of t h e f o r m a n = c 9 5 n. T h e n we m u s t h a v e c. 5 n  5(c. 5 n  l )  6(c. 5 n2) 4 3 . 5 n
Canceling 5 n2 f r o m b o t h sides, t h e r e s u l t i n g e q u a t i o n yields c  25/2. We n o w claim t h a t a n  (25/2)5 n is a p a r t i c u l a r solution of t h e r e c u r r e n c e r e l a t i o n (Verify this.). T h u s t h e g e n e r a l solution of t h e L N H R R W C C s is a n  A . 2 n + B . 3 n +
(25/2)5 n
U s i n g t h e initial conditions, we get t h e l i n e a r s y s t e m : A + B  17/2 2A + 3B   1 1 1 / 2 Solving this s y s t e m , we get A  30 a n d B   7 7 / 2 (Verify this.). T h u s t h e solutions of t h e given r e c u r r e n c e r e l a t i o n a r e given by an 
(Verify this also.)
(30). 2 n  (77/2). 3 n + (25/2). 5 n,
n > 0 m
Chapter 5 Recursion
296
A n i m p o r t a n t observation: In t h i s example, notice t h a t t h e 5 in f ( n ) is not a c h a r a c t e r i s t i c r o o t of t h e A L H R R W C C s . If it were, we w o u l d h a v e n e e d e d to m a k e a d j u s t m e n t s in o u r search for a p a r t i c u l a r solution, as in T h e o r e m 5.3. We shall p u r s u e this case shortly.
T h e following t h e o r e m justifies t h e t e c h n i q u e s d e m o n s t r a t e d in t h e s e two examples; we o m i t its p r o o f in t h e i n t e r e s t of brevity. In t h e L N H R R W C C s (5.13), s u p p o s e f (n) = (bk nk + b k _ l n k1 + . . . + b i n + bo)(~ n. If a is not a c h a r a c t e r i s t i c root of t h e A L H R R W C C s (5.14), t h e n a p a r t i c u l a r solution is of t h e f o r m (dk nk + d k _ l n k1 + ... + d i n 4 do)or n. If a is a c h a r a c t e r i s t i c root w i t h m u l t i p l i c i t y m, t h e n a p a r t i c u l a r s o l u t i o n is of t h e f o r m n m ( e k n k + ek_l n k  1 + . . . + e l n + eo)ot n. m We conclude this section w i t h t h e following example, w h i c h i l l u s t r a t e s this t h e o r e m w h e n a is a c h a r a c t e r i s t i c root of t h e A L H R R W C C s . Solve t h e L N H R R W C C s an  6an1  9an2 + 4(n + 1)3 n, w h e r e a0 = 2 a n d a l  3. SOLUTION: F r o m E x a m p l e 5.17, t h e general solution of t h e A L H R R W C C s is a ~ ) = A. 3 ~ + B. n3 n, w h e r e n >_ 0. Since 3 is a c h a r a c t e r i s t i c root w i t h m u l t i p l i c i t y 2, we search for a p a r t i c u l a r solution of the form n2(cn + d)3 n, w h e r e t h e c o n s t a n t s c a n d d are to be d e t e r m i n e d . T h e n we m u s t have n2(cn + d ) 3 n  6{(n  1)21c(n  1) + d l 3 nl}
 9{(n  2)2[c(n  2) + dl3 n2 } + 4(n + 1)3 'z E q u a t i n g the coefficients of like t e r m s , this yields c  2/3 a n d d  4 (Verify);  ( p ) _ 2n2(n + 6)3 n1 SO ~n
Thus the general soution of the recurrence relation is an  A . 3 n + B . n3 n + 2n2(n + 6)3 n  l ,
n _> 0
U s i n g the initial conditions, this yields an  (6  19n). 3 n1 + 2n2(n + 6)3 n1 ,
n>_0
(You can confirm this.) E x e r c i s e s 5.3
D e t e r m i n e if each r e c u r r e n c e r e l a t i o n is a L H R R W C C . 1. Ln  Ln1 + L n  2
2. Dn  n D n  1 + (  1 ) n
3. an  1 . 0 8 a n _ l
4. bn = 2bn1 + 1
i
5.3
Solving Recurrence Relations Revisited
297
5. a n  a n  1 + n
6. an  2 a n  1 + (2 n  1)
7. a n  a n  1 + 2 a n  2 + 3 a n  5
8.
a n  a n  1 + 2 a n  3 + n 2
Solve each LHRRWCC. 9. a n  a n  1 + 2 a n  2 , a o 10.
an 
11.
an = an1 + 6an_2,ao
5an1
= 0
 6 a n  2 , a o = 4, a l 
12. an = 4an2,ao 13. an an1
 3 , a l
= 5,al
= 2,al
+ an2,ao
7
= 0
= 8
= 1 , a l  2
1 4 . a n = a n  1 + a n  2 , ao  2, a l = 3 1 5 . Ln = L n  1 + L n  2 , L1 = 1, L 2 = 3 16.
an = 4an1
17. an = 6an
 4an_2,ao 1
= 3,al
9 a n  2 , a o = 2, a l

= 10 ~
18. an = 3an1 + 4an2  12an_3,ao 19.
3
 3 , a l
= 7,a2
= 7
 21an2 + 18an_3,ao
 0 , a l
 2 , a 2 = 13
20. a n  7 a n  1  1 6 a n  2 + 1 2 a n _ 3 , a o
 0 , a l
 5 , a 2  19
21.
48an_4,ao
a n  8 a n  1
an
=
+ 16an2 + 4an3
an1
= 0,al
=
16,a2
 2,
a3 = 1 4 2
22. a n = 1 3 a n  2  3 6 a n  4 , a0 = 7, a l = 6, a2 = 38, a3    8 4 23. a n = 9 a n  1  3 0 a n  2 + 4 4 a n _ 3  2 4 a n  4 , a o
= 5,al
=
12,a2
=
38,
4,a2
=
44,
a3 = 1 2 6 24.
an
=
8an1

24an2 + 32an3
 1 6 a n  4 , a o
=
1,al
=
a3 = 2 7 2 Find the general form of a particular corresponding to each function f(n).
solution of the LNHRRWCCs
(5.13)
25. f ( n ) =
n
26. f(n)=
1
27. f(n)=
3n 2
28. f ( n ) =
3n
29. f(n)
n2 n
30. f(n)
43n25 n
Find
the
a n  4 a n  1
general
form
of a particular
 4an2 + f (n) c o r r e s p o n d i n g
solution of the LNHRRWCCs to each function f (n).
31. f (n) = 3  2 n
32. f ( n ) = n 2 n
33. f ( n )  2 3 n 2 2 n
34.
(17n 3
Solve each LNHRRWCCs.
35. a n  2 a n  1
+ 1, a 0 
1
36. a n  7 a n  1  1 0 a n _ 2 + n 2, ao = 0, a l = 1
1)2 n
298
Chapter 5 Recursion
37. a n  7 a n  1  12an2 + 3n,a0  0 , a l = 2 38. a n  7 a n  1  12an2 + 3 n 4 n , a o *39.
an

a n  1 ~ n , ao
*40.
an

a n  1 + n


= 0,al  2
1
1,al  0
41. Let rn and Sn be two solutions of the r e c u r r e n c e relation (5.8). P r o v e t h a t a n   r n + S n is also a solution. 42. Let a be a solution of the e q u a t i o n x k  C l x k  1 t h a t c~n is a solution of L H R R W C C (5.7).
.....
Ck
 0. S h o w
43. Let a be a characteristic root of the L H R R W C C a n = a a n _ 1 + b a n  2 +C a n  3 with degree of multiplicity three. Show t h a t a n, n(~ n, n 2 a n are solutions of LHRRWCC. 44. Let a(nh) denote the general solution of the A L H R R W C C s (5.14) a n d a particular solution of the L N H R R W C C s (5.13). P r o v e t h a t a n  a(nh) + a(np) is the general solution of the L N H R R W C C s (5.13). a(np)
G e n e r a t i n g functions provide a powerful tool for solving L H R R W C C s , as will be seen shortly. They were invented in 1718 by the F r e n c h m a t h e matician A b r a h a m De Moivre, w h e n he used t h e m to solve the Fibonacci recurrence relation (see Example 5.26). G e n e r a t i n g functions can also solve combinatorial problems, as the next c h a p t e r shows. To begin with, notice t h a t the polynomial 1 + x + x ~ + x a + X 4 ~  X 5 can x6 1 be w r i t t e n as ~ . You may verify this by either crossmultiplication x1
the familiar long division method, or Exercise 8 in Section 4.4. AccordX6 
ingly, f ( x ) 
1
~ i s x1
called the g e n e r a t i n g
f u n c t i o n of the sequence of
coefficients 1, 1, 1, 1, 1, 1 in the polynomial. More generally, we m a k e the following definition.
Generating Function
Let a0, al, a 2 , . . , be a sequence of real n u m b e r s . T h e n the function g ( x )  ao + a I x + a2 x2 + . . . + a n x n + . . .
(5.15)
is the g e n e r a t i n g f u n c t i o n for the sequence { a n }. G e n e r a t i n g functions for the finite sequence a0, a l , . . . , a n can also be defined by letting ai = 0 for i > n; t h u s g ( x )  ao + a l x + a2 x 2 + . . . + a n x n is the g e n e r a t i n g function for the finite sequence a0, a l , . . . , a n .
5.4 GeneratingFunctions
299
A b r a h a m De M o i v r e (16671754), son of a surgeon, was born in VitryleFrancois, France. His formal education began at the Catholic village school, .' . .'.,~.' a n d then continued at the Protestant Academy at Sedan a n d later at S a u m u r . : , . , ,,' ..$ He did not receive good training in mathematics until he moved to Paris in 1684, where he studied Euclid's later books and other texts. A r o u n d 1686, De Moivre emigrated to England, where he began his lifelong profession, tutoring in mathematics, and mastered Newton's Principia Mathematica. In 1695 he presented a paper, his first, on Newton's theory of fluxions to the Royal Society of London a n d 2 years later he was elected a member of the Society. Unfortunately, despite his influential friends, he could not find an academic position. He h a d to earn a living as a tutor, author, a n d expert on applications of probability to g a m b l i n g a n d annuities. He dedicated his first book, a masterpiece, The Doctrine of Chances, to Newton. His most notable discovery concerns probability theory: The binomial probability distribution can
be approximated by the normal distribution. De Moivre died in London.
For example, g ( x )  1 + 2x + 3x 2 + . . .
+ (n + 1)x n + . . .
is the g e n e r a t i n g function for the sequence of positive integers and f ( x )  1 + 3x + 6x 2 + . . . +
n ( n + 1)x2 + . . .
2
is the g e n e r a t i n g function for the sequence of t r i a n g u l a r n u m b e r s . Since xn  1
= 1 + x +x2 ~... + x n1
x1 x n  1 g(x) 
x1
is the g e n e r a t i n g function for the sequence of n ones.
A w o r d o f caution" The RHS of E q u a t i o n (5.15) is a f o r m a l p o w e r
s e r i e s in x. The letter x does not r e p r e s e n t anything. T h e various powers x n of x are simply used to keep t r a c k of the c o r r e s p o n d i n g t e r m s an of the sequence. In o t h e r words, t h i n k of the powers x n as placeholders.
Consequently, unlike in calculus, the convergence of the series is of no i n t e r e s t to us. ,, ,,
,
.
.
.
.
.
Equality of Generating Functions Two g e n e r a t i n g functions f (x) an  bn for every n >__0.
~
n =0
a n x n and g ( x ) = ~
n =0
bnx n are e q u a l if
300
Chapter 5 Recurslon F o r example, let f ( x ) = 1 + 3x +
6X 2 q 10x 3 +  . .
2.3 3.4x2 4 . 5 x3 g(x)  1 +   ~ x +  ~ + ~ +....
and
T h e n f (x) = g(x).
A g e n e r a t i n g function we will use f r e q u e n t l y is =
1 + ax
+ a 2 x 2 nt . . .
(5.16)
[a n x n Jr " "
Xax
1 1  x = 1 +x +x 2 +...fX
Then
n
(5.17)
Jr'''
Can we add a n d m u l t i p l y g e n e r a t i n g functions? Yes! Such o p e r a t i o n s a r e p e r f o r m e d exactly t h e s a m e way as polynomials are combined.
Addition and Multiplication of Generating Functions CO
CO
Let f (x) = ~ a n X n a n d g(x) n=0
~
be two g e n e r a t i n g functions. T h e n
bnx n
n=0
f (x) + g(x)  ~
(an
+ bn)x n
a n d f (x)g(x)  ~
n=0
n=0
~
aibn_
i =0
i
)
Xn
For example,
(1
 x) 2
1
1
1 x
1 x

Xi
Xi
)

1.1
n=O
)
xn
CX~
= E(n
+ 1)x n
n=O
= 1 + 2 x + 3x 2 + . . . + (n + 1)x n + . . . and 1 (lx) 3
1 1x 
1 (lx) 2
(n
Xn
+ 1)X n
Ln =0
1.(n + 1i)
xn
n=0 (X)
= ~[(n n0
+ 1) + n + . . . + 1 ] x n
(5.18)
5.4 Generating Functions
301 (x)
(n + 1)(n + 2)xn 2
= E n0
= 1 + 3x + 6x 2 + 10x 3 + . . .
(5.19)
Before exploring how valuable g e n e r a t i n g functions are in solving LHRRWCCs, we illustrate how t h e t e c h n i q u e of p a r t i a l f r a c t i o n d e e o m p o s i t i o n , used in integral calculus, enables us to express t h e q u o t i e n t
p(x) of two polynomials p(x) a n d q(x) as a s u m of p r o p e r fractions, w h e r e q(x) degp(x) < deg q(x). t For example, 6X+1
1
2
( 2 X  1)(2X + 3)
2X 1
2X + 3
P a r t i a l F r a c t i o n Decomposition Rule for p ( x )
q(x)'
w h e r e d e g p ( x ) < deg q(x)
If q(x) has a factor of the form (ax + b) m, t h e n the decomposition contains a sum of the form
A1 ax + b
~
A2
t
(ax + b) 2
Am (ax + b) m
+'"+
w h e r e Ai is a r a t i o n a l n u m b e r . Examples 5.225.24 illustrate the partial fraction decomposition technique. We use t h e i r results to solve the r e c u r r e n c e relations in E x a m p l e s 5.255.27. x a Express (1  x)(1  2x) as s u m of partial fractions. SOLUTION: Since the d e n o m i n a t o r contains two linear factors, we let x (1  x)(1  2x)
=
A 1 x
B
+
1  2x
To find the c o n s t a n t s A and B, multiply b o t h sides by (1  x)(1  2x)"
x  A ( 1  2x) + B ( 1  x) Now give c o n v e n i e n t values to x. S e t t i n g x  i yields A   1 and s e t t i n g x  1/2 yields B  1. (The values of A and B can also be found by e q u a t i n g tdeg f(x) denotes the degree of the polynomial f(x).
302
Chapter 5 Recursion
coefficients of like terms from either side of the equation resulting linear system.) x
1

(1  x ) ( 1  2x)
1
..J_
1  x
and solving the
1  2x
(You m a y verify this by c o m b i n i n g t h e s u m fraction.) W e u s e t h i s r e s u l t in E x a m p l e 5.25.
on the RHS
into a single II
x
Express
1  x

X2 a s a s u m o f p a r t i a l f r a c t i o n s .
SOLUTION: F i r s t , f a c t o r 1  x  x 2" 1  x  x 2 = (1  a x ) ( 1  f i x )
1J5
1F w/5 a n d fl w h e r e c~ = a  fl  v ~ . ) 2
~
.
1  x  x
2
( N o t i c e t h a t a + fl 
1, aft 
1, and
Let x
A
B
=
t 1 
ax
1 
fix
Then x 
Equating
A(1

fix) + B( 1 
otx)
c o e f f i c i e n t s o f l i k e t e r m s , w e get" A + B  O flA

Solving this linear system yields A 
otB 
1
,/g
1
= B
(Verify this.).
Thus
(1
x
x
2)
111
v/5
1]
1c~x
1  fix
m
W e u s e t h i s r e s u l t i n E x a m p l e 5.26. Express
2  9x 1  6x + 9x 2
as a sum of partial fractions.
SOLUTION: Again, factor the denominator: l  6X + 9X 2  (l
 3X) 2
5.4
Generating Functions
303
By t h e d e c o m p o s i t i o n r u l e , let 2  9x
A
1  6x + 9x 2
=
B t
1  3x
(1  3x) 2
Then 2  9x  A(1  3x) + B T h i s y i e l d s A  3 a n d B   1 (Verify this.). Thus 29x 1 6x
+ 9x 2
3
1
1  3x
(1  3x) 2
W e u s e t h i s r e s u l t in E x a m p l e 5.27.
I
N o w we a r e r e a d y to u s e p a r t i a l f r a c t i o n d e c o m p o s i t i o n s a n d g e n e r a t i n g f u n c t i o n s to solve r e c u r r e n c e r e l a t i o n s in t h e n e x t t h r e e e x a m p l e s . U s e g e n e r a t i n g f u n c t i o n s to solve t h e r e c u r r e n c e r e l a t i o n bn  2 b n  1 + 1, w h e r e b l  1. SOLUTION: F i r s t , n o t i c e t h a t t h e c o n d i t i o n bl  i y i e l d s b0 = 0. To find t h e s e q u e n c e {bn} t h a t satisfies t h e r e c u r r e n c e r e l a t i o n , c o n s i d e r t h e c o r r e s p o n d i n g generating function b2 x2 +
g ( x )  bo + b l x +
bnx n q...
b3 x 3 + . . . +
Then 2 b l x 2 + 2b2x 3 + . 99+ 2bn_
2xg(x) =
1 xnJr"
99
Also,
1x
=l+x+
X 2 +
X 3Jr" 9 9 ~
X nJr" 9 9
Then g(x)  2xg(x)
1x
= 1+
(bl  1)x + (b2  2bl  1)x 2 + . . .
+ (bn  2 b n  1  1)x n
=1 since bl  I a n d bn  2 b n  1 + 1 for n >_ 2. T h a t is, 1
(1  2 x ) g ( x ) 
1 x
1
x
1 x
Then g(x) =
(1  x ) ( 1  ~ c )
Jr ' ' '
304
Chapter 5 Recursion 1
=
1x
= 
§
xn
: E(2 n
1 12x' +
by E x a m p l e 5.22
2nx n
, by (5.16)
1)X n
n=O OG
But g(x) 
b n x n, so bn 
~
1, n > 1. (Notice t h a t t h i s is t h e s a m e
2n 
n:0
solution o b t a i n e d in E x a m p l e 5.12.)
II
U s i n g g e n e r a t i n g functions, solve t h e Fibonacci r e c u r r e n c e r e l a t i o n F n F n  1 + F n  2 , w h e r e F1  1  F2.
SOLUTION: Notice t h a t t h e two initial c o n d i t i o n s yield F0  0. Let g ( x )  Fo + F i x + F 2 x 2 4  . . . 4 F n x n 4  ' "
be t h e g e n e r a t i n g f u n c t i o n of t h e Fibonacci sequence. Since t h e o r d e r s of F n  1 a n d F n  2 a r e 1 a n d 2 less t h a n t h e o r d e r of F n , respectively, we find xg(x) and x2g(x)" x g ( x )  F i x 2 4 F 2 x 3 4 F3 x4 4  . . . 4 F n  1 xn 4  ' " x2 g ( x )  F i x 3 4 F 2 x 4 4 F3 x5 4  . . . 4 F n _ 2 xn 4  . . .
Then g ( x )  x g ( x )  x 2 g ( x )  F i x 4 (F2  F1)x 2 4 (F3  F2  F1)x 3 + ... + (Fn  F , _ 1  F n  2) xn 4 "'" =X
since F2  F1 a n d Fn = F n  1 + F n  2 . T h a t is, (1  x  x 2 ) g ( x )  x g(x) =
1 XX
2
111
1ax
where a =
1+V/5 2
a n d fl =
14 2
1]
1  fix
' by E x a m p l e 5.23
5.4 Generating Functions
305
Then ~ / 5 g ( x ) 
1
1
1  otX
1  fiX
oo oo  E o l n x n   E ~ n x n  
n0
n0
(DO E(oln
 ~ n ) x n
n=0
So o4) g(x) 
~
n=0
(Oln  ~ n ) x n
x/~
Therefore, by the e q u a l i t y of g e n e r a t i n g functions, oln _ fin
otn _ fin
,/g
~  ~
Fn
(Recall t h a t this is t h e B i n e t f o r m of F n . )
m
We close this section with the following example. ~
U s i n g g e n e r a t i n g functions, solve the r e c u r r e n c e r e l a t i o n an 9 a n  2 , w h e r e ao  2 a n d a l  3.
6an1
SOLUTION: Let g ( x )  ao + a l x + a2 x2 + . . . + a n X n + . . .
Then 6 x g ( x ) = 6 a o x + 6 a l x 2 + 6a2x 3 + ... + 6 a n _ i X n + . . . 9x2g(x) 
9a0x 2 + 9 a l x 3 + 9a2x 4 + . . . + 9 a n _ 2 x n + . . .
Then g ( x )  6 x g ( x ) + 9 x 2 g ( x )  ao + ( a l  6a0)x + (a2  6al + 9a0)x 2 + . . . + (an  6 a n  1 + 9 a n  2 ) x n + ' "
=29x u s i n g the given conditions. T h u s (1  6x + 9 x 2 ) g ( x ) = 2  9 x Therefore, g(x) =
29x 1  6 x + 9x 2

306
Chapter 5 Recursion 3 1 = 1  3X  (1  3x) 2' by E x a m p l e 5.24 " 3
3nx n

(n + 1)3nx n nO
(x:)
 E [ 3 n+l   ( n + 1 ) 3 n ] x n n0 (x)
 E
3n(2  n ) x n
n0
Thus an 
(2  n ) 3 n
n > 0
m
T h e following e x e r c i s e s p r o v i d e a m p l e p r a c t i c e in t h i s p r o b l e m  s o l v i n g technique.
Exercises 5.4 E x p r e s s e a c h q u o t i e n t as a s u m of p a r t i a l f r a c t i o n s . 1.
3. 5.
7.
9.
x + 7
2.
( x  1)(x + 3) 5 l_x_6x
2
4.
x ( x + 2) (2 + 3x)(x 2 + 1) X 3 ~  X 2 + X ~
4X 2  
2+4x l+8x+15x 2X 2 
6.
3
3x 3  x 2 + 4x
x 3 + x 2 + 5x  2
11.
an = 2an_l, ao = 1
12.
an = an1
1,al = 1 = 1
14. a n = a n  1 + 2 a n  2 , a o = 3, a l = 0 15. a n = 4 a n  2 , a o = 2, a l =  8 an = an1
2x + 2
( x  1)(x 2 + 2x)
*10.
X4 X 2 +X
U s i n g g e n e r a t i n g f u n c t i o n s , solve e a c h L H R R W C C .
16.
2
8. x 4 + x 3 + x + 1
X 4   X 3 ~ 2X 2   X ~ 1
13. a n = a n  l + 2 , a l
25
X 3 + 2X 2 ~  X
x 4 + 5x 2 + 6
+
3x
(x + 1 ) ( x  2)(x + 3)
+ 6 a n  2 , a o = 5, a l = 0
1
307
5.5 RecursiveAlgorithms
17. a n = 5 a n  z 18. a n  a n  1
6an2,ao
= 4,a1 = 7
+ an2,ao
= 1,al = 2
19. a n = a n  1 + a n  2 , a o
= 2 , a l  3
20. L n  L n  1 + L n  2 , L 1
= 1, L2 = 3
21.
an
=
4an1
 4 a n  2 , a0 = 3, a l = 10
22.
an

6an1
 9an2,ao
 2,a1  3
23. a n  3 a n  z + 4 a n  2  1 2 a n  3 , a0 = 3, a l =  7 , a2 = 7 24. a n  8 a n  1  2 1 a n  2 + 1 8 a n _ 3 , a o
= 0, a l = 2, a2 = 13
2 5 . a n = 7 a n  1  1 6 a n  2 + 1 2 a n  3 , a0 = 0, a l = 5, a2 = 19 26. a n = 3 a n  1 + 4 a n  2  12an_3, a0 = 3, a l =  7 , a2 = 7 27. a n = 6 a n  1  1 2 a n  2 + 8 a n _ 3 , a o
28.
= 0,al = 2,a2 = 2
a n  1 3 a n  2  3 6 a n  4 , a0 = 7, a l     6 ,
29. a n =  a n  z + 3 a n  2 + 5 a n  3 + 2 a n  4 , a o
a2 = 38, a3 =  8 4 = 0, a l =  8 , a 2
= 4,a3 = 42
Recall t h a t t h e r e c u r s i v e d e f i n i t i o n o f t h e f a c t o r i a l f u n c t i o n f e x p r e s s e s in t e r m s of i t s e l f w i t h a s m a l l e r a r g u m e n t n  1. A c c o r d i n g l y , it c a n b e e m p l o y e d to w r i t e a s i m p l e a l g o r i t h m to c o m p u t e n! T h i s a l g o r i t h m h a s t h e i n t e r e s t i n g p r o p e r t y t h a t it i n v o k e s i t s e l f w i t h a s m a l l e r a r g u m e n t . S u c h a n a l g o r i t h m is a r e c u r s i v e a l g o r i t h m .
f(n)
Recursive Algorithm A n a l g o r i t h m is r e c u r s i v e if it i n v o k e s i t s e l f w i t h a s m a l l e r a r g u m e n t ; t h a t is, if it i n v o k e s a r e d u c e d v e r s i o n of itself. (See F i g u r e 5.1.) R e c u r s i v e d e f i n i t i o n s i n v a r i a b l y l e a d to r e c u r s i v e a l g o r i t h m s . T h i s sect i o n t r a n s l a t e s s o m e o f t h e e x a m p l e s d i s c u s s e d in S e c t i o n 5.1 i n t o r e c u r s i v e a l g o r i t h m s a n d p r e s e n t s a few n e w o n e s   g c d , b i n a r y s e a r c h , a n d m e r g e sort. ~
W r i t e a r e c u r s i v e a l g o r i t h m to c o m p u t e n!, w h e r e n >__0. SOLUTION: W h e n n = 0, t h e a l g o r i t h m m u s t t e r m i n a t e a n d y i e l d t h e v a l u e 1. W h e n n > 0, t h e r e c u r r e n c e r e l a t i o n f ( n ) = n . f ( n  1) m u s t be applied: t h e a l g o r i t h m m u s t i n v o k e i t s e l f w i t h n  1 as t h e n e w a r g u m e n t . T h e r e c u r s i v e a l g o r i t h m is g i v e n in A l g o r i t h m 5.1.
Chapter 5 Reeurslon
308
Algorithm factorial (n) (* This algorithm computes n! using recursion *) O. Begin (* algorithm *) i. i f n = 0 then (* base case *) 2. factorial ~ I 3. else (* invoke the algorithm *) 4. factorial +  n 9 f a c t o r i a l ( n  1) 5. End (* algorithm *) Algorithm 5.1
l
F i g u r e 5.15 shows t h e r e s u l t of invoking the factorial a l g o r i t h m w i t h n  3, w h e r e f m e a n s f a c t o r i a l . Figure 5.15
~f(l) 9 / f recursive I 1. 11. Write an iterative algorithm to compute the n t h Fibonacci n u m b e r . 12. Mrs. Zee deposits A dollars at a b a n k at an annual interest rate of r% compounded semiannually. Write a recursive algorithm to compute the compound a m o u n t she will receive at the end of n years. Using the recursive binary search algorithm in Example 5.33, determine if the given key occurs in the corresponding list. Show the successive values of low, high, and m i d . 13. 2, 3, 5, 8, 13, 21; key  13
14. 3, 5, 7, 8, 10; key  9
Using the merge sort algorithm, arrange each list into ascending order. 15. 9, 5, 2, 7, 19, 17, 3, 11
16. 9, 11, 6, 2, 12, 3, 8, 5, 31, 13
17. Write an algorithm to compute the n t h Lucas n u m b e r Ln using recursion. 18. Let x be a positive real n u m b e r and n a nonnegative integer. Write a recursive algorithm to compute x n. Let X  [Xl,X2,... ,Xn] and Y  [Yl,y2,...,Yn] be two lists of numbers. Write a recursive algorithm to accomplish the tasks in Exercises 1931. 19. Find the sum of the n u m b e r s from left to right. 20. Find the sum of the n u m b e r s from right to left. 21. Compute the product of the n u m b e r s from left to right. 22. Compute the product of the n u m b e r s from right to left. 23. Find the m a x i m u m of the n u m b e r s in the list. 24. Find the m i n i m u m of the n u m b e r s in the list. 25. Print the n u m b e r s in the given order xl, x 2 , . . . , Xn. 26. Print the n u m b e r s in the reverse order Xn, Xn 1 ,  . . , X2, Xl. 27. ( L i n e a r s e a r c h ) Search the list for a specific item (key). R e t u r n the location of key if the search is successful. 28. Determine if two lists X and Y of n items of the same type are identical.
316
Chapter 5 Recursion 29. Determine if a word of n a l p h a n u m e r i c characters is a palindrome. 30. Evaluate A c k e r m a n n ' s functionA(x,y), w h e r e x a n d y are n o n n e g a t i v e integers. See Exercises 5.1 for a definition of A(x,y). 31. Sort the list X using bubble sort. 32. Use the recursive bubble sort algorithm to sort the list 13, 5, 2, 8, 3. Q u i c k s o r t , invented in 1962 by C. A n t h o n y R. Hoare of Oxford University, is an extremely efficient technique for sorting a large list X of n items X l,X2,... ,Xn. It is based on the fact t h a t it is easier to sort two small lists t h a n one large list. Choose the first element x l as the pivot. To place the pivot in its final resting place, compare it to each element in the list. Move the elements less t h a n x 1 to the front of the list and those greater t h a n Xl to the rear. Now place pivot in its final position. Partition the list X into two sublists such t h a t the elements in the first sublist are less t h a n Xl and the elements in the second sublist are greater t h a n xl. Continue this procedure recursively with the two sublists. *33. Use quicksort to sort the list 7, 8, 13, 11, 5, 6, 4. *34. Use quicksort to write a recursive algorithm to sort a list X of n elements.
We now use induction to establish the correctness of two wellknown recursive algorithms, linear search and bubble sort. We begin with the linear search algorithm. Recall t h a t the linear search algorithm searches a list X of n elements for a given key. If the search is successful, the algorithm r e t u r n s the location of key; otherwise, it r e t u r n s zero. A recursive version is given in Algorithm 5.8. Again, as an exercise, use it to search the list X = [13, 5, 47, 7, 11, 8, 3] for key  11. Algorithm linear search (X,n,key,location) (* This a l g o r i t h m returns the p o s i t i o n of key in the v a r i a b l e l o c a t i o n . I f l o c a t i o n = O, then key does not e x i s t in the l i s t . *) O. Begin (* algorithm *) i. i f n = 0 then (* unsuccessful search *) 2. l o c a t i o n + 0 3. else i f Xn = key then 4. l o c a t i o n , n 5. else 6. l i n e a r search(X,n  l , k e y , l o c a t i o n ) 7. End (* algorithm *)
Algorithm 5.8
5.6 Correctness of Recursive Algorithms
~
317
Establish the correctness of Algorithm 5.8. P R O O F (by PMI): To prove the correctness of the algorithm, we m u s t show t h a t it works correctly for n > 0. Let P(n)" The algorithm r e t u r n s the correct value of location for every list of size n. B a s i s s t e p When n  0, lines 3 t h r o u g h 6 in the algorithm are skipped and the algorithm r e t u r n s the value 0 from line 2. So the algorithm works correctly when n  0. I n d u c t i o n s t e p Let k be an arbitrary integer k > 0 such t h a t P(k) is true; that is, assume the algorithm works correctly for a list of a r b i t r a r y size k > 0. To prove t h a t P(k + 1) is true, invoke the algorithm for a list X of size k + 1. Note t h a t k + 1 > 1. Case 1 line 4.
If Xk+ 1

key,
the algorithm r e t u r n s the value k + 1 from
C a s e 2 IfXk+l r key, line 6 is executed; so the algorithm is invoked for a list with k elements. By our inductive hypothesis, the algorithm works for such a list.
Thus in both cases, the algorithm r e t u r n s the correct value of location. Therefore, P(k + 1) is true. Consequently, P(n) holds for n > 0 by induction; t h a t is, the algorithm works correctly for every list. m Next we verify the correctness of the recursive version of the bubble sort algorithm, given in Algorithm 5.9. To get used to it, you may use it to sort the list X  [13, 5, 47, 7, 11, 8, 3]. Algorithm Bubble Sort(X,n) (* This algorithm sorts a l i s t X of n items using recursion. *) O. Begin (* algorithm *) i. i f n > 1 then (* l i s t contains at least two elements *) 2. b e g i n (* i f *) 3. f o r i = 1 to n  1 do 4. i f x i > Xi+ I then (* they are out of order *) 5. swap xi and xi + i 6. bubble sort(X,n  I)
7. 8.
endi f End (* algorithm *)
Algorithm 5.9
~
Establish the correctness of Algorithm 5.9. P R O O F (by PMI)" Let P(n)" The algorithm works for every list of size n.
Chapter 5 Recursion
318
B a s i s s t e p When n  0, the list contains no elements. So the a l g o r i t h m works by default. Thus, P(0) is true. I n d u c t i o n s t e p Assume P(k) is true for an arbitrary integer k >_ 0; t h a t is, the algorithm correctly sorts every list ofk (>_ 0) elements. To prove t h a t P(k + 1) is true, invoke the algorithm for a list X with k + 1 elements, w h e r e k+1>1. If k + 1  1, the f o r loop is not entered. So P(k + 1) is true, by default. If k + 1 > 1, the f o r loop is entered. Consecutive elements xi and Xi+l are compared in line 4 and switched in line 5 if necessary. W h e n we exit the loop, the largest of the k + 1 elements is placed in the correct position, in location k + 1. This leaves a sublist of k elements, x l , . . . , xk. By the inductive hypothesis, the algorithm correctly sorts such a list. Thus if P(k) is true, then P(k + 1) is also true. Therefore, by induction, P(n) is true for every n >__0: the algorithm sorts every list of every size n >_ 0. m The following exercises provide additional opportunities to establish the correctness of recursive algorithms. Exercises 5.6 Establish the correctness of each algorithm. 1. The factorial algorithm in Example 5.28. 2. The h a n d s h a k e algorithm in Example 5.29. 3. The Tower of B r a h m a algorithm in Example 5.30. 4. The Fibonacci algorithm in Example 5.31. 5. The binary search algorithm in Example 5.33. 6. The merge sort algorithm in Algorithm 5.7. 717. The algorithms in Exercises 1929 of Section 5.5. Algorithm 5.10 computes the n t h power of a positive real n u m b e r x, where n > 0. Use it to answer Exercises 1824.
Algorithm exponentiation (x,n) (* This algorithm computes the nth power of x using recursion and returns the value in the variable answer. *) O. Begin (* algorithm *) 1. i f n = 0 then 2. answer 0; so C n   O ( n ) = (~)(n). Thus, in the worst case, the algorithm takes O(n)   comparisons to locate the key, the same as the iterative version, m Next we analyze the recursive and iterative Fibonacci algorithms. ~
Using the recursive algorithm in Example 5.31, estimate the n u m b e r of additions an needed to compute the n t h Fibonacci number.
SOLUTION: By Exercises 9 and 10 in Section 5.5, a n  F n  1, n >_ 1. But, by Exercise 43 in Section 5.1, F n _ 1. Therefore, an _< 2 n 
1
< 2n  O ( 2 n)
Thus, the recursive Fibonacci algorithm takes 0(2 n) additions,
m
For comparison, we now study the complexity of the iterative version of the Fibonacci algorithm. Estimate the n u m b e r of additions a,~ required in line 5 to compute the n t h Fibonacci n u m b e r F , by Algorithm 5.11. Algorithm iterative Fibonacci(n) (* This i t e r a t i v e a l g o r i t h m uses the values of the v a r i a b l e s of the l a s t and the c u r r e n t Fibonacci numbers to compute the next Fibonacci number. *) O. Begin (* a l g o r i t h m *) I. l a s t ~ i 2. c u r r e n t ~ I 3. f o r i = 2 to n do 4. begin (* f o r *) 5. next ,  l a s t + c u r r e n t 6. l a s t ~ c u r r e n t 7. c u r r e n t , next
8. 9.
endfor End (* a l g o r i t h m *)
A]gorithm 5.11
SOLUTION: The first two Fibonacci n u m b e r s need no computations; therefore, a l 0 = a2. Suppose n > 2. It takes one addition to compute the next item
5.7 Complexities of Recursive Algorithms (optional)
321
Fn from the current term Fn1. So an  an1 ~ 1. Solving this recurrence
relation (try), we get ann2,
n>_2
=  Thus the iterative version takes 
additions to compute Fn.
B
The time it takes to compute Fn by the recursive algorithm grows exponentially with n, whereas by the iterative algorithm it grows only linearly. As n gets larger and larger, it takes more time to compute Fn by recursion than by iteration. Thus, by dividing and conquering the problem, we have made it complicated. Should we prefer the iterative method to the recursive method? Since every recursive algorithm has a nonrecursive version, if the algorithm makes just one recursive call to itself, as in the factorial algorithm, the iterative approach will, in general, save time. On the other hand, if the problem has a recursive definition, it will be easy to write a recursive algorithm for the problem. Writing the nonrecursive version of a recursive algorithm is often a painful task and the resulting algorithm is often much longer, complicated, and difficult to understand. For instance, the nonrecursive version of the Tower of Brahma algorithm is longer and that of quicksort is rather complicated. Next we estimate the number of elementcomparisons required by the recursive selection sort algorithm presented in Algorithm 5.12. (See Algorithm 4.11 in Chapter 4 for an iterative version.) Algorithm selection sort(X,n) (* This algorithm invokes a subalgorithm called swap which switches two elements. MoxZndex denotes the index of the largest of the n elements. *) O. Begin (* algorithm *) I. maxindex~ n ( * i n i t i a l i z e maxindex at each pass *) 2. for i = 1 to n  1 do 3. i f xi > Xmaxindex then 4. maxindex ~ i 5. i f maxindex # n then (* swap the corresponding items *) 6. swap Xmaxindex and Xn 7. selection sort(X,n  1) 8. End (* algorithm *)
Algorithm 5.12
Estimate the number Cn of comparisons (lines 3 and 5) required by Algorithm 5.12. SOLUTION: To estimate Cn, first define it recursively. If the list contains just one element, lines 3 and 5 are not executed; therefore, c1  0.
322
Chapter 5 Recursion
Suppose n > 2. Since t h e f o r loop is executed n  1 times, line 3 is executed n  I times. F u r t h e r m o r e , line 5 is executed once. Therefore, Cn Cn1
n t
1)+ 1
(n
=Cn1 ~n,
n > 2
Solving the r e c u r r e n c e relation by the iterative m e t h o d , we get n ( n + 1) Cn 
2
1,
n>__l
 (~)(n2) T h u s the a l g o r i t h m takes ()(n 2) comparisons to sort a list of n items, as in the iterative version, m E x a m p l e 5.41 investigates one of the m a n y properties of Fibonacci n u m bers. E x a m p l e 5.42 uses the p r o p e r t y to e s t i m a t e the n u m b e r of divisions in the euclidean algorithm. 1+,/5 Let F,~ denote the n t h Fibonacci n u m b e r and c~ = Prove t h a t c~ n  2 < gn < c~ n  1 n > 3. P R O O F (by s t r o n g induction): (We shall prove t h a t c~'~2 < Fn and leave the o t h e r half as an exercise.) You may verify t h a t a is a solution of the e q u a t i o n x 2  x + 1, so a2 _ a + 1. Let P(n)" O~n  2 < F , , w h e r e n > 3. B a s i s s t e p Since the induction step below uses the r e c u r r e n c e relation Fk+i  Fk + Fh1, the basis step involves verifying t h a t both P(3) and P(4) are true. 9 To s h o w t h a t P ( 3 ) is true: W h e n n  3,
1+~/~ ot n  2
  o~  
2
2  F3. Consequently, we have: r n  2  r n  l q n  1
t rn
>__r n  1 + rn
>F3+l = F3 + F2  F4
324
Chapter 5 Recursion
r n  3  r n  2 q n  2
+ rn1
>__ r n  2 + r n  1
>_ F4 + F 3  F 5 C o n t i n u i n g like this, r l  r 2 q 2 + r3 > r2 + r3
>_ Fn + Fn1  Fn+l
T h a t is, b >_Fn+l
B y E x a m p l e 5.41, Fn+ 1 > c~n  l , w h e r e a =
b
1 + ~/5. Therefore, 2
>C~ n1
Then log b > (n  1) log Since a 
1+v/5
1 1.618033989, log c~ ~ 0.2089876403 > ~. So log b >
n1 5
Suppose b contains k decimal digits. T h e n b < 10 h. Therefore, log b < k n1 and hence k > ~ . T h u s n < 5k + 1 or n < 5k. T h a t is, the n u m b e r of 5 divisions needed by the a l g o r i t h m is no more t h a n five times the n u m b e r of decimal digits in n. n n1 Let us p u r s u e this example a bit further. Since log b > ~ , n 1 + 5 log b. Also, since b > 2, 5 log b >_ 5 log 2 >1 Thus n < 1 + 51ogb < 51ogb + 51ogb = 10 logb = O(log b) T h u s it takes O(log b) divisions to c o m p u t e gcd{a,b} by the euclidean algorithm.
_ 2
(5.21)
328
Chapter 5 Recursion
To solve this r e c u r r e n c e relation, a s s u m e , for convenience, t h a t n is a p o w e r of 2, say n  2 k, w h e r e k >_ 0. Let Cn  a k . T h e n t h e r e c u r r e n c e r e l a t i o n (5.21) becomes ak  ak1 + 1, w h e r e a0 = 1. Solving t h i s r e c u r r e n c e r e l a t i o n yields ak  k + 1, k >_ 0 (Verify.). Since n = 2 k, k  l g n , so cn = 1 + lg n, n >_ 1. T h u s , if n is a p o w e r of 2, t h e n Cn  (O(lg n). S u p p o s e n is n o t a p o w e r of 2. T h e n , by induction, it can be s h o w n t h a t cn  1 + llg n J, w h e r e n >_ 1 (see Exercise 44), so Cn  (')(lg n). Thus, in b o t h cases, t h e a l g o r i t h m t a k e s ()(lg n) e l e m e n t c o m p a r i s o n s in t h e w o r s t case. m The p r e c e d i n g e x a m p l e is a special case of t h e following t h e o r e m . Since the p r o o f is s o m e w h a t complicated, we skip it (see Exercises 65 a n d 66). Let a, b E 1~ a n d c, d ~ IR+ w i t h b > 2. Let f be a n o n d e c r e a s i n g function* such t h a t f ( n )  a f ( n / b ) + c a n d f(1)  d. T h e n
f(n)

O(lgn) O(nlogba)
if a  1 otherwise
m
F o r example, let f be a n o n d e c r e a s i n g f u n c t i o n such t h a t f ( n ) = + 5 a n d f(1)  8. T h e n , by T h e o r e m 5.7, f ( n )  O(nlgS). The n e x t t h e o r e m is a g e n e r a l i z a t i o n of T h e o r e m 5.7. We s t a t e it w i t h o u t proof (see Exercises 6 7  6 9 for special cases of t h e t h e o r e m ) a n d apply it in E x a m p l e 5.45. 3f(n/2)
Let a, b ~ N a n d c, d e IR+ w i t h b >_ 2. Let f be a n o n d e c r e a s i n g f u n c t i o n such t h a t f ( n )
 af(n/b)
+ c n d. T h e n
f(n)
(optional) Let A


O(n d)
if a < b d
O(ndlgn)
ifab
O(nlogba)
if a > b d
(aij) a n d B 
d m
(bij) be two n x n m a t r i c e s . Let C = n
(cij) be t h e i r p r o d u c t w h e r e cij
a i h b k j . Since C h a s n 2 e n t r i e s a n d k=l each t a k e s n multiplications, t h e p r o d u c t C can be c o m p u t e d u s i n g n 3 = O(n 3) multiplications; in fact, it can be c o m p u t e d u s i n g O(n 3) c o m p u t a t i o n s (additions a n d multiplications), as Exercises 40 a n d 41 indicate, m =
~
We close this section w i t h an analysis of t h e m e r g e sort a l g o r i t h m , a d i v i d e  a n d  c o n q u e r strategy. ( m e r g e s o r t ) T h e m e r g e sort m e t h o d in A l g o r i t h m 5.7 s o r t s a list of n elements. Assume, for convenience, t h a t n is a p o w e r of 2, say, n  2 k, k >_ 0. *Let S c_ IR. A function f 9S ~ •+ is said to be nondecreasing ifx < y implies f(x) 2. The list is split into two, with each sublist containing n/2 elements. In the worst case, the n u m b e r of comparisons resulting from line 4 is Cn/2, as it is from line 5. W h e n the merge a l g o r i t h m is invoked in line 6, each sublist contains n/2 elements; so the m a x i m u m n u m b e r of c o m p a r i s o n s from line 6 is n  1. T h u s c2
1
Cn  2Cn/2 + ( n 
Let ak

Cn
1),
n>3
where n  2 k, k _> 0. T h e n a11 ak   2 a k _ l + (2 k 
1),
k>2
This recurrence relation (see Exercise 8 in Section 5.2) yields ak  ( k 
1)2 k + 1,
k>l
= k . 2k  2k + 1 Thus c,~  (lg n)n  n + 1 _ 1
n>2
7. f
2
1
fn 2fn1 + (2 n  1), n >_ 2
fn = f n  l + n , n > 2
Find the number of comparisons needed to search for key  13 in each ordered list using the recursive binary search algorithm in Example 5.33. 8. 1 , 2 , 3 , 5 , 8 , 13
9. 5,8, 13,21,34
10. 3, 7, 8, 13, 21
11. 15, 16, 19, 21
Compute the maximum number of comparisons needed to search for a particular item in an ordered list containing the following number of items, using the recursive binary search algorithm. 12. 8
13. 20
15. 31
14. 25
Let bn denote the number of multiplications needed to compute n! using the recursive factorial algorithm in Example 5.1. 16. Define bn recursively. 17. Solve the recurrence relation satisfied by b,~. 18. Show that bn = O(n). 1922. Estimate the number of times a,~ the assignment statement, x x + 1, is executed by the nested f o r loops in Exercises 3538 of Section 4.4. Estimate the number a,~ of times the statement, x ~ x + 1, is executed by each nested f o r loop. 23.
f o r i = 1 t o n do f o r j = 1 t o Li / 2 ]
24. do
x _ 0 . 3. Prove t h a t fn+ 1  f 2
_ 2fn + 2. (J. M. Schram, 1983)
4. Using Exercise 3, compute fl, f2, f3, and f4. 5. Let an be an infinite sequence with al  1, a5  5, a12  144, and an + an+3  2an+2. Prove t h a t an  Fn. (H. Larson, 1977)
1+4~ 6. Let a
=
2 lim F~+I = ~.
n,~
and Fn the n t h Fibonacci n u m b e r .
Prove t h a t
gn
*7. Let S n denote the sum of the n u m b e r s in the n t h t e r m of the sequence of sets of p e n t a g o n a l n u m b e r s { 1}, {5, 12}, {22, 35, 51}, {70, 92,117, 145}, . . . . Find a formula for S n . *8. Let S n denote the sum of the n u m b e r s in the n t h t e r m of the sequence of sets of Fibonacci n u m b e r s {1}, {1,2}, {3,5,8}, {13,21,34,55}, . . . . Find a f o r m u l a for S n .
Chapter Summary
339
Describe the behavior of each n u m b e r sequence {an }, where a0  a, a l  b, and a2  c are positive numbers. (R. L. G r a h a m , 1991) 9. an+2  (1 + an+l)/an
10. an+3  (1 + an+l + an+2)/an
Let n E N and ~ Euler's phifunction. Define ~k _ ~gk1 o q), where ~1 _ and o denotes composition. Let f ( n )  ~(n) + ~2(n) + ~3(n) + . . . + q~(1). (D. L. Silverman, 1981) 11. Compute f(5) and f(8). 12. Prove t h a t if n  2 k, t h e n f ( n )  n. 13. Prove t h a t f ( n ) is even. [Hint: ~(n) is even for n > 2.] 14. Consider the sequence of right triangles Tn, n > 1, with legs An and Bn, and hypotenuse Cn such t h a t An+l  Bn and Bn+l  Cn. Compute lim Bn Cn n   * ~ An and n.eclim ~ . (M. Flavio, 1980) A set of integers A is f a t if each of its elements is >__ IAI. For example, {5, 7, 91} is a fat set, but {3, 7, 36, 41} is not. ~ is considered a fat set. Let fn denote the n u m b e r of fat subsets of the set {1, 2 , . . . , n}. (G. F. Andrews) "15. Define fn recursively.
* 16. Find an explicit formula for fn.
Let f ( n , k ) denote the n u m b e r of kelement subsets of the set S {1, 2 , . . . ,n} t h a t do not contain consecutive integers. Let fn denote the total n u m b e r of subsets of S t h a t do not contain consecutive integers. (I. Kaplansky) "17. Define f(n, k) recursively.
"18. Find an explicit formula for fn.
Computer Exercises Write a p r o g r a m to perform each task. 1. Read in a positive integer n < 20, and p r i n t the various moves and the n u m b e r of moves needed to transfer n disks from peg X to peg Z, using the rules in Example 5.4. 2. Read in a positive integer n, and print the first n t r i a n g u l a r and t e t r a h e d r a l numbers. 3. P r i n t the t r i a n g u l a r n u m b e r s _< 1000 t h a t are perfect squares. 4. P r i n t the t r i a n g u l a r n u m b e r s < 1000 t h a t are primes. 5. There are eight palindromic t r i a n g u l a r n u m b e r s < 1000. Find them. 6. Search for two t r i a n g u l a r n u m b e r s tn such t h a t tn and n are palindromic, where 9 < n < 100. 7. Read in a positive integer n and print the first n Fibonacci numbers, using recursion and iteration.
340
Chapter 5 Recursion 8. Read in a positive integer n _< 20 and print the first n Lucas n u m b e r s . 9. Read in a positive integer n < 25 and print the values of Fn+l and 
Fn
Ln+l correct to 10 decimal places, where Fn denotes the n t h Fibonacci Ln n u m b e r and Ln the n t h Lucas number. Read in a list of n positive integers. Use recursion to print each. 10. Their sum, product, m a x i m u m , and m i n i m u m . 11. The list in the given order. 12. The list in the reverse order. 13. Read in a key and search the list for key. P r i n t the location if the search is successful; otherwise, print a suitable message. 14. Read in a key and a sorted list of n items; d e t e r m i n e if key occurs in the list using recursion and iteration. P r i n t the location of key if the search is successful. 15. Read in a list of n words and determine if each is a palindrome, using recursion. 16. Read in two lists of n integers. Determine if they are identical, using recursion. 17. Read in a nonnegative real n u m b e r x and a nonnegative integer n; compute the n t h power ofx. 18. Read in a positive integer n < 100 and a positive real n u m b e r x < 2. Use the binary r e p r e s e n t a t i o n of n and the technique of successive squaring to compute x n. P r i n t the n u m b e r of multiplications needed to compute it. n
19. Read in a n u m b e r a, and a polynomial ~ aix ~(that is, coefficients and the corresponding exponents); print tt~e~ using H o r n e r ' s method.
of the polynomial at a,
20. Read in n positive integers and print their m i n i m u m and m a x i m u m , using both iteration and recursion. 21. Read in a positive integer n < 10 and a r r a n g e the Stirling n u m b e r s of the second kind S(n, r) in a t r i a n g u l a r form, where 1 < r < n. 22. Read in n positive integers and sort t h e m using bubble sort, selection sort, and insertion sort. P r i n t the n u m b e r of elementcomparisons needed by each algorithm. 23. Read in n fourletter words. Sort them, using merge sort and quicksort. P r i n t the n u m b e r of element comparisons needed by each sort.
Chapter Summary
341
Exploratory Writing Projects Using library and Internet resources, write a team report on each of the following in your own words. Provide a welldocumented bibliography. 1. Describe the properties of Fibonacci numbers, their occurrences in nature, applications to various disciplines, and relationships to Lucas numbers. 2. Explain how the golden ratio is related to Fibonacci and Lucas numbers. Describe its various occurrences in nature. 3. Describe the various forms of Ackermann's function. Investigate its importance in the study of recursive functions and the analysis of algorithms. 4. Investigate the Josephus problem, named for the first century Jewish historian Flavius Josephus (37?100?). 5. Describe how, using Fibonacci numbers Fn (n >_ 2) as bases, nonnegative integers can be represented as binary numbers with no two adjacent l's. Express the integers 125 as such binary numbers. 6. Define continued fractions and describe their relationship to Fibonacci numbers. 7. Describe the Game of Life, invented in 1970 by British mathematician John H. Conway, now at Princeton University. 8. Describe the Game of Halma, invented in 1883 by George H. Monks, a Harvard Medical School graduate. 9. Examine the history of Catalan numbers and their properties and applications. Include a biography of E. C. Catalan. 10. Write an essay on the Tower of Brahma (Hanoi). 11. Write an essay on Quicksort. 12. Discuss the fifteen puzzle, invented by American puzzlist Samuel Loyd (18411911). 13. Discuss Markov chains, named after Russian mathematician Andrei A. Markov (18561922), who developed the theory of stochastic processes, and their applications to business.
Enrichment Readings 1. G. Brassard and P. Bratley, Algorithmics: Theory & Practice, PrenticeHall, Englewood Cliffs, NJ, 1986, pp. 2634, 4861.
342
Chapter5 Recursion 2. R. P. Grimaldi, Discrete and Combinatorial Mathematics: An Applied Introduction, 4th edition, AddisonWesley, Boston, MA, 1999, pp. 351403. 3. B. W. Jackson and D. Thro, Applied Combinatorics with Problem Solving, AddisonWesley, Reading, MA, 1990, pp. 226252. 4. T. Koshy, Fibonacci and Lucas Numbers with Applications, Wiley, New York, 2001. 5. C. Oliver, "The Twelve days of Christmas," Mathematics Teacher, Vol. 70 (Dec. 1977), pp. 752754. 6. S. Sahni, Concepts in Discrete Mathematics, 2nd ed., Camelot, Fridley, MN, 1985, pp. 205335. 7. R. Sedgewick, Algorithms, 2nd ed., AddisonWesley, Reading, MA, 1988, pp. 3189. 8. K. B. Strangeman, "The Sum of n Polygonal Numbers," Mathematics Teacher, Vol. 67 (Nov. 1974), pp. 655658. 9. C. W. Trigg, "Palindromic Triangular Numbers," J. Recreational Mathematics, Vol. 6 (Spring 1973), pp. 146147. 10. A. Tucker, Applied Combinatorics, Wiley, New York, 1984, pp. 222298. 11. H. S. Wilf, Algorithms and Complexity, PrenticeHall, Englewood Cliffs, NJ, 1986, pp. 2634, 4861.
Chapter 6
C o m b i n a t o r i c s and D i s c r e t e Probability The theory of probability affords an excellent illustration of the application of the theory of permutations and combinations which is the fundamental part of the algebra of discrete quantity. 
G. CRYSTAL
C
ombinatorics is a fascinating branch of discrete mathematics, which deals with the art of counting. Very often we ask the question, In how m a n y ways can a certain task be done? Usually combinatorics comes to our rescue. In most cases, listing the possibilities and counting them is the least desirable way of finding the answer to such a problem. Often we are not interested in enumerating the possibilities, but rather would like to know the total number of ways the task can be done. For instance, consider the following combinatorial problem: O n e t y p e of a u t o m o b i l e license p l a t e n u m b e r in M a s s a c h u s e t t s consists of one l e t t e r a n d five digits. F i n d t h e n u m b e r of s u c h license p l a t e n u m b e r s possible. 9 9
5H0515 MASSACHUSETTS
~ ~
Suppose you are willing to list all the possibilities and count them to find the answer. Assuming you know how to enumerate them systematically and that it would take a second to count each, it would take about 6 months to complete the counting alone. Obviously, this is an inefficient way to find the answer, especially when combinatorics can do the job in seconds. (See Example 6.6.) A few other interesting problems we examine in this chapter are: 9 A secretary types up 10 different letters and 10 envelopes. In how many different ways can she place each letter in an envelope so that no letter is placed in the correct envelope? 343
344
Chapter 6
Combinatorics and Discrete Probability
9 Eleven guests would like to order soft drinks with their dinner. There are five choices for a soft drink: Coke Classic, Diet Coke, root beer, Pepsi, and Sprite. Find the n u m b e r of different beverage selections possible. 9 What is the m a x i m u m n u m b e r of nonoverlapping regions formed inside a circle by joining n points on it? 9 In how m a n y ways can n married couples be seated at a r o u n d table so t h a t men and women sit on alternate chairs and no wife sits next to her husband? This chapter presents the f u n d a m e n t a l s of combinatorics.* In several instances, you will find recursion and generating functions useful in solving combinatorial problems, so review t h e m as needed.
This section, a natural continuation of Section 2.4, presents three fundamental principles that form the foundation of combinatorics. The addition and the inclusionexclusion principles discussed in Section 2.4 have fine applications to combinatorics, as will be seen shortly. Find the n u m b e r of ways of drawing a red queen or a black king from a standard deck of playing cards. SOLUTION: Let A denote the set of red queens and B the set of black kings. Clearly, ]Ai = 2 = IB]. Since A and B are disjoint sets, by the addition principle, IA u Bi = IA] + iBI = 2 + 2 = 4. Thus there are four different ways of drawing a red queen or a black king. m In this example, drawing a red queen can be considered a t a s k , say, task A. Likewise, drawing a black king can be considered task B. Since the two tasks cannot occur simultaneously, they are m u t u a l l y e x c l u s i v e . So finding the n u m b e r of ways of drawing a red queen or a black king is equivalent to finding the n u m b e r of ways task A or t a s k B can be performed. Accordingly, the addition principle can be restated in t e r m s of tasks as follows.
(Addition Principle) Let A and B be two mutually exclusive tasks. Suppose task A can be done in m ways and task B in n ways. T h e n task A or task B can take place in m + n ways. m *The first book on combinatorics is Ars Conjectandi, written by the Swiss mathematician, Jakob Bernoulli (16541705) and published posthumously in 1713.
6.1 The Fundamental Counting Principles
345
The addition principle can be e x t e n d e d to a n y finite n u m b e r of pairwise m u t u a l l y exclusive tasks, u s i n g induction. For instance, let T 1 , . . . , Tn be n pairwise m u t u a l l y exclusive tasks. Suppose t a s k Ti can be done in mi ways, w h e r e 1 < i < n. T h e n t a s k T1, T 2 , . . . , or Tn can be done in m l § m2 § § mn ways, as the next e x a m p l e illustrates. ~
A f r e s h m a n has selected four courses and needs one m o r e course for t h e next t e r m . T h e r e are 15 courses in English, 10 in French, a n d 6 in G e r m a n she is eligible to take. In how m a n y ways can she choose t h e fifth course? SOLUTION: Let E be the t a s k of selecting a course in English, F the t a s k of selecting a course in F r e n c h , and G t h a t of selecting a course in G e r m a n . T h e s e t a s k s can be done in 15, 10, a n d 6 ways, respectively, a n d are m u t u a l l y exclusive, so, by the addition principle, the fifth course can be selected in IEI + IFI + I G I  15 + 10 + 6  31 ways. i Like the addition principle, the inclusionexclusion principle can be r e s t a t e d in t e r m s of t a s k s in an obvious way. It can also be e x t e n d e d to a finite n u m b e r of t a s k s (see T h e o r e m 6.19). ( I n c l u s i o n  E x c l u s i o n P r i n c i p l e ) Suppose a t a s k A can be done in m ways, t a s k B in n ways, and both can be accomplished in k different ways. T h e n t a s k A or B can be done in m + n  k ways. m The next p r o b l e m exemplifies this. In how m a n y ways can you deal a king or a black card from a s t a n d a r d deck of cards? SOLUTION: A king can be selected in four different ways and a black card in 26 different ways. These two t a s k s can be done s i m u l t a n e o u s l y in two ways, n a m e l y , by selecting a black king; so, by T h e o r e m 6.2, a king or a black card can be selected in 4 + 26  2  28 ways. m Before s t a t i n g the next c o u n t i n g principle, let us r e t u r n to E x a m p l e 2.23 in C h a p t e r 2. The t a s k of selecting a mode of t r a n s p o r t a t i o n for t h e trip from Boston to London via New York consists of two s u b t a s k s A a n d B" A is selecting a mode of t r a n s p o r t a t i o n from Boston to New Y o r k ~ c a r , plane, or ship ~ and B is selecting a mode of t r a n s p o r t a t i o n from New York to Lond o n ~ plane or ship. They can be done in ]Ai  3 a n d iBi  2 ways. Recall t h a t the trip can be m a d e in 6  IAI. iB] ways; t h a t is, IA • BI  6  IA]. IBI. More generally, we have t h e following result. ( M u l t i p l i c a t i o n P r i n c i p l e ) Suppose a t a s k T is m a d e up of two s u b t a s k s , s u b t a s k T1 followed by s u b t a s k T2. If s u b t a s k T1 can be done in m ] ways and s u b t a s k T2 in m2 different ways for each way s u b t a s k T1 can be done, t h e n t a s k T can be done in m l m 2 ways. m The next four examples i l l u s t r a t e this principle.
346
Chapter6 Comblnatoricsand Discrete Probability F i n d t h e n u m b e r of t w o  l e t t e r words t h a t begin with a v o w e l   a, e, i, or
O,
u.
SOLUTION: The t a s k of f o r m i n g a t w o  l e t t e r word consists of two s u b t a s k s T1 a n d T2" T1 consists of selecting t h e first l e t t e r a n d T2 selecting t h e second letter, as F i g u r e 6.1 shows.
F i g u r e 6.1
number of choices
subtask T1
subtask T2
Since each word m u s t begin w i t h a vowel, T1 can be a c c o m p l i s h e d in five ways. T h e r e are no r e s t r i c t i o n s on t h e choice of t h e second letter, so T2 can be done in 26 ways (see F i g u r e 6.2). Therefore, by t h e m u l t i p l i c a t i o n principle, the t a s k can be p e r f o r m e d in 5 . 2 6 = 130 different ways. In o t h e r words, 130 t w o  l e t t e r words begin with a vowel.
F i g u r e 6.2
number of choices 5 subtask T 1
9
26 subtask T 2
m
T h e various twoletter words in this e x a m p l e can be e n u m e r a t e d systematically by c o n s t r u c t i n g a tree d i a g r a m , as in Figure 6.3. All desired words can be obtained by t r a v e r s i n g t h e various b r a n c h e s of the tree, as indicated.
F i g u r e 6.3
first element
second element ~
a
~
word )
a
aa
9
~
z a
~ ~
az ea
z
~ ~
9 ez
a
___.)
ua
e ~
..)
u
z
~
9
.
uz
6.1 The Fundamental Counting Principles
347
The multiplication principle can also be extended to any finite n u m b e r of subtasks. Suppose a task T can be done by n successive subtasks, T1, T2,..., Tn. If subtask Ti can be done in mi different ways after Ti1 has been completed, where 1 < i < n, t h e n task T can be done in m l m 2 . . . m n ways. The multiplication principle can be applied to prove t h a t a set with size n has 2 n subsets, as shown below. Show t h a t a set S with n elements has 2 n subsets. SOLUTION: Every subset of S can be uniquely identified by an nbit word (see Figure 6.4). The task of forming an nbit word can be broken down to n subtasks: selecting a bit for each of the n positions. Each position in the word
F i g u r e 6.4
number of choices
9 .
9 .
9 .
.
.
9
9
.
.
9
n bits has two choices, 0 or 1; so, by the multiplication principle, the total number of nbit words t h a t can be formed is 2 . 2 . . . . . 2  2 n (see Figure 6.5). In other words, S has 2 n subsets, n times Y
F i g u r e 6.5
number of choices
2.2.2
..
2.2.2
II
We now t u r n to solving the problem posed at the beginning of the chapter. One type of automobile license plate n u m b e r in Massachusetts consists of one letter and five digits. Compute the n u m b e r of such license plate n u m b e r s possible. SOLUTION: For convenience, we decompose the task into three subtasks: 9 Choosing a letter. It can be done in 26 ways. 9 Choosing the position of the letter. It has six possible slots.
Chapter 6 Combinatorics and Discrete Probability
348
9 Choosing the five digits. T h e y can be selected in 1 0 . 1 0 . 1 0 . 1 0 . 1 0 100,000 ways.
=
Now we are ready to find the final answer. By t h e multiplication principle, the total n u m b e r of license plates is 2 6 . 6 . 100000 = 15,600,000. m The next example depends on the multiplication and addition principles. (optional) An identifier in a p r o g r a m m i n g language consists of a letter followed by a l p h a n u m e r i c characters.* F i n d the n u m b e r of legal identifiers of length at most 10. SOLUTION: Let Si denote the set of identifiers of length i, where i < i < 10. T h e n ISil 2 6 . 3 6 i1 (see Figure 6.6). Since the s u b t a s k s $ 1 , . . . ,$10 are m u t u a l l y F i g u r e 6.6 26 9 36 9 36 . . . . .
36
I letter ~
(i  1) alphanumeric characters
exclusive, by the addition principle, the total n u m b e r of identifiers of length < 10 is given by 10
JSit = ~ 2 6 "
i=1
36 i1  26
36 i
i=1  26.
(36 l ~
1)
26(36 l ~
36 1
1)
35
 2,716,003,412,618,210 2.7 quadrillion!
m
The final example in this opening section employs the multiplication and the inclusionexclusion principles. ~
An eightbit word is called a b y t e . Find the n u m b e r of bytes with t h e i r second bit 0 or the t h i r d bit 1. SOLUTION: N u m b e r of bytes with second bit 0  2 . 1 . 2 . 2 . 2 . 2 . 2 . 2 N u m b e r of bytes with third bit 1  2 . 2 . 1 . 2 . 2 . 2 . 2 . 2
 27  27
Since these two s u b t a s k s are n o t m u t u a l l y exclusive, we c a n n o t add these two partial answers and claim t h a t the a n s w e r is 27 + 27  128 + 128  256. *An alphanumeric character is a letter or a digit.
6.1 The Fundamental Counting Principles
349
So, we m u s t find the n u m b e r of bytes t h a t have both properties. The number of bytes with second bit 0 and third bit 1 equals 2 . 1 . 1 . 2 . 2 . 2 . 2 . 2 26  64, so, by the inclusionexclusion principle, the n u m b e r of bytes with the given properties is 27 + 27  26  128 + 128  64  192. m Exercises 6.1
Find the n u m b e r of positive integers < 1976 and divisible by: 1. 2 o r 3 .
2. 3 o r 5 .
3. 2,3, o r 5 .
4. 3,5, o r 7 .
5. In one version of BASIC, a variable name consists of a letter, or a letter followed by a digit, or the dollar sign ($). Find the total n u m b e r of possible variable names. Find the n u m b e r of terms in the expansion of each expression.
6. (a + b)(c + d + e)(x + y)
7. (b + c)(d + e + f)(x + y + z)
8. ( ~~ai)( ~ bi)( ~ci)
9. ( ~ el)( ~ bi)( ~ci)
i=o
i=l
i=2
i=2
i1
i=o
10. Find the n u m b e r of palindromes of length n over the English alphabet. 11. Find the n u m b e r of palindromic alphanumeric identifiers of length n. (See Example 6.7.) 12. Find the total n u m b e r of bytes. (See Example 6.8.) 13. A word over the alphabet {0, 1, 2} is called a t e r n a r y word. Find the n u m b e r of t e r n a r y words of length n that can be formed. A typical automobile license plate in New York contains three letters followed by three digits. Find the n u m b e r of license plates of this kind that: 14. Can be formed.
15. Begin with the word BAT.
16. Begin with a vowel.
17. Begin with the digit 6.
18. Repeat no letters or digits. 19. Contain the same letters and the same digits. 20. Have the property t h a t both words and n u m b e r s are palindromes. An old zip code in the United States consists of five digits. Find the total n u m b e r of possible zip codes that" 21. Have no repetitions.
22. Begin with 0.
23. End in K.
24. Are palindromes.
350
Chapter 6 Combinatorics and Discrete Probability
A zip code in C a n a d a consists of t h r e e letters and t h r e e digits. Each zip code begins with a letter. The letters and digits alternate; for instance, A1B2C3. Find the n u m b e r of zip codes that: 25. End in 6.
26. Begin with A and end in 3.
27. End in Z.
28. Are possible.
The password for a computer system consists of six a l p h a n u m e r i c characters and begins with a letter. Find the total n u m b e r of passwords that" 29. Are possible.
30. End in RED.
31. Contain the word BAT.
32. End in 2076.
33. Every radio and television station in the United States has a u n i q u e call name. Each contains three or four letters, beginning with K or W. For example, KEY and WASP are legal call names. Find the n u m b e r of possible call names. Find the n u m b e r of bytes that: 34. Begin with 101.
35. End with 110.
36. Begin with and end in the same bit. 37. Begin with and end in different bits. 38. Have the same third and fourth bits. 39. Have third bit or fifth bit 1.
40. Have second bit 0 or third bit 1.
41. Are palindromes. Find the n u m b e r of positive divisors of the following positive integers. 42. 2 i. 3 j . 5 k
43. 600
44. A numbertheoretic function used in the study of perfect n u m b e r s is the t a u f u n c t i o n v on N. (v is the Greek letter, tau.) v(n) denotes el, the n u m b e r of positive divisors of n ~ N. Let n  P l (" 1P2(?2 "" "Pk', where P l , P 2 , . . . ,Ph are distinct primes and e l , e 2 , . . . ,e]~ ~ W. Find r(n). 45. Find the n u m b e r of n x n matrices t h a t can be constructed using bits. Find the n u m b e r of t e r n a r y words t h a t have: 46. Length at most 3.
47. Length at most 5.
48. Length at most n.
49. Length 3 and are palindromes.
50. Length 4 and are palindromes.
51. 4 < length < 6
In an alphabet of m characters, how m a n y words have: 52. Length 3?
6.2
Permutations
351
53. L e n g t h not m o r e t h a n 2? 54. L e n g t h at least 2, b u t not m o r e t h a n 4? 55. L e n g t h not m o r e t h a n n? Let A and B be two finite sets w i t h ]A] = m a n d ]B]  n. How m a n y : 56. F u n c t i o n s can be defined from A to B? 57. Bijections can be defined from A to B ( a s s u m e m  n)? 58. Invertible functions can be defined from A to B ( a s s u m e m  n)? 59. Injections can be defined from A to B ( a s s u m e m < n)? Let T denote t h e t a u function. Prove each. 60. T(n) is odd if a n d only if n is a square. "61. If m and n are relatively p r i m e n u m b e r s , t h e n ~'(mn)  ~:(m). T(n). *62. The h a r m o n i c m e a n m of t h e n u m b e r s a l , a 2 , . . . , an is the reciprocal of the a r i t h m e t i c m e a n of t h e i r reciprocals; t h a t is, 1
1~(1)
m
n i=1
ai
Prove t h a t the h a r m o n i c m e a n of the positive factors of a perfect n u m b e r N is an integer. (Hint" I f d is a factor of N, t h e n so is N / d . ) (R. Euler, 1987)
The concept of ordered a r r a n g e m e n t s was familiar to Chinese m a t h e m a t i cians as early as 1100 B.C. This section i n t r o d u c e s t h e concept of ordered a r r a n g e m e n t s a n d shows how to find the n u m b e r of such a r r a n g e m e n t s . Suppose a p h o t o g r a p h e r would like to a r r a n g e 10 cats in a row for a television commercial. She can a r r a n g e t h e m in a n y order she likes. In how m a n y ways can she accomplish this? A l t h o u g h we could use the multiplication principle to arrive at an answer, we shall apply the concept of a p e r m u t a t i o n (see E x a m p l e 6.11). Recall t h a t t h e e l e m e n t s of an ordered set are assigned u n i q u e positions. For convenience, let us denote the ordered set (a,b,c) as the a r r a n g e m e n t abc. The words acb and bac are two different a r r a n g e m e n t s of the very s a m e letters. ( R e m e m b e r , the order m a k e s a difference.) E a c h of these a r r a n g e m e n t s is a p e r m u t a t i o n of the t h r e e l e t t e r s t a k e n all at a time, or a 3permutation.
Chapter 6 Combinatorics and Discrete Probability
352
Permutation
A p e r m u t a t i o n of a set of n (distinct) elements t a k e n r (0 < r < n) at a time is an a r r a n g e m e n t of r elements of the set. For convenience, it is called an r  p e r m u t a t i o n . If r = n, t h e n the r  p e r m u t a t i o n is called simply a p e r m u t a t i o n . The n u m b e r of r  p e r m u t a t i o n s of a set of size n is d e n o t e d by P(n, r). We begin our discussion with a simple example. Find the n u m b e r of permutations; t h a t is, 3  p e r m u t a t i o n s of the e l e m e n t s of the set {a, b, c}. SOLUTION: By the multiplication principle, the n u m b e r of 3  p e r m u t a t i o n s of t h r e e elements is 3 92 9 1 = 6. Thus P(3, 3) = 6. m The various p e r m u t a t i o n s in Example 6.9 can be obtained systematically using a tree diagram, as Figure 6.7 shows. They are abc, acb, bac, bca, cab, and cba.
F i g u r e 6.7
first element
second element
third element
b
c
~
abc
c
b
~
acb
a
C
~
bac
12
a
bca
a b
b a
cab
~~x~~
x c ~
~
permutations
cba
Find the n u m b e r of 2permutations of the elements of the set {a, b, c}. SOLUTION" Again, by the multiplication principle, the n u m b e r of 2  p e r m u t a t i o n s is 3 . 2  6; t h a t is, P(3, 2) = 6. m The various 2permutations in Example 6.10 are ab, ac, ba, bc, ca, and cb. They can be obtained using the tree diagram in Figure 6.8. Examples 6.9 and 6.10 can be i n t e r p r e t e d as follows: Suppose you have three books in your hands and would like to a r r a n g e t h e m in a bookcase. If there is enough room for three books, they can be a r r a n g e d in 3 . 2 . 1  6 different ways. On the other hand, if there is room for only two books, t h e y can be a r r a n g e d in 3 . 2 = 6 different ways.
6.2 Permutations
F i g u r e 6.8
353
first element
a
second element
permutations
b
~
ab
c
~
ac
a
~
ba
C
'>
be
a
~
ca
b
>
cb
~
b
c ~
M o r e g e n e r a l l y , we h a v e t h e following r e s u l t . ~
T h e n u m b e r of r  p e r m u t a t i o n s of a set of n (distinct) e l e m e n t s is g i v e n by n! P(n,r)

~ . (nr)!
PROOF: Since t h e r e a r e n e l e m e n t s , t h e first e l e m e n t can be c h o s e n in n ways. N o w n  1 e l e m e n t s a r e left; so t h e s e c o n d e l e m e n t c a n be c h o s e n in n  1 ways. C o n t i n u e like t h i s u n t i l t h e r t h e l e m e n t is r e a d y to be c h o s e n . At t h i s p o i n t t h e r e a r e n  r + 1 e l e m e n t s left. C o n s e q u e n t l y , t h e r t h e l e m e n t c a n be c h o s e n in n  r + 1 ways. T h u s , by t h e m u l t i p l i c a t i o n principle, P(n,r)

n(n

1)(n  2 ) . . . (n  r + 1)
n(n

1 ) . . . (n  r + 1)(n  r ) . . . 2 . 1 (n  r ) . . . 2 . 1
=
n!
m
(n  r ) !
A l t h o u g h it is e a s y to r e m e m b e r t h e v a l u e of P ( n , r ) u s i n g t h i s form u l a , P ( n , r ) is o f t e n c o m p u t e d u s i n g t h e f o r m u l a P ( n , r ) = n ( n  1) (n  2 ) . . . (n  r + 1). T h e v a l u e s n! a n d (n  r)! m a y be too l a r g e e v e n for a c a l c u l a t o r to c o m p u t e . T h e n t h e v a l u e n ! / ( n  r ) ! m a y n o t be exact. You will find t h e fact t h a t n !  n ( n  1)! u s e f u l in c o m p u t i n g P ( n , r ) . F o r example, P(25, 5) =
25! ( 2 5  5)!
=
25! 20!
 25.24.23.22.21
=
25.24.23.2221.20! 20!  6,375,600
Chapter 6 Combinatorics and Discrete Probability
354
Suppose we let r = n in T h e o r e m 6.4. T h e n n? n? P ( n , n )  (n  n)?  O?
n? 1  n?
Accordingly, we have the following result. ~
The n u m b e r of p e r m u t a t i o n s of a set of size n is given by P(n, n) = n!. T h a t is, n elements can be arranged in n? ways. m Interestingly enough, applications of this formula appear in the anonymous Hebrew book Sefer Yetzirah (The Book o f Creation), written between 300 and 600 A.D. The next two examples illustrate this theorem.
~
A
p h o t o g r a p h e r would like to arrange 10 cats for a television commercial. How m a n y ways can she arrange t h e m in a row? SOLUTION: Since all the cats have to be in the commercial at the same time, r  n = 10. Therefore, the n u m b e r of possible a r r a n g e m e n t s is P(10, 10) = 107 = 3,628,800. m
~
Find the n u m b e r of words that can be formed by scrambling the letters of the word SCRAMBLE. (Remember, a word is j u s t an a r r a n g e m e n t of symbols; it need not make sense.) SOLUTION: The word SCRAMBLE contains eight distinct letters. Therefore, the number of words t h a t can be formed equals the n u m b e r of a r r a n g e m e n t s of the letters in the word, namely, P(8, 8) = 8? = 40,320. m The next example uses the Theorem 6.5.
~
A
multiplication
principle,
as well as
salesperson at a computer store would like to display six models of personal computers, five models of computer monitors, and four models of keyboards. In how m a n y different ways can he a r r a n g e t h e m in a row if items of the same family are to be next to each other? SOLUTION: There are three types of items: personal computers, monitors, and keyboards. Think of the items in each family as tied together into one unit. These families can be arranged in P(3, 3) = 3! ways. Now the items within each family can be rearranged. The six models of personal computers can be arranged in P(6, 6)  6? ways, the monitors in P(5, 5)  5? ways, and the keyboards in P(4, 4) = 4? different ways. Thus, by the multiplication principle, the total n u m b e r of possible a r r a n g e m e n t s is 3? 6? 5? 4? = 12,441,600. m
6.2 Permutations
355
P e r m u t a t i o n s of e l e m e n t s a r r a n g e d in a circle w o r k s o m e w h a t differently from the linear s i t u a t i o n s we have i n v e s t i g a t e d up to this point.
Cyclic Permutation In how m a n y different ways can you place five beads on a necklace? T h e a n s w e r is n o t 5! = 120, b u t far less, since it contains a lot of duplicate a r r a n g e m e n t s . For instance, t h e two circular a r r a n g e m e n t s s h o w n in F i g u r e 6.9 are identical. (Look at t h e relative positions of t h e beads p l t h r o u g h P5.) E a c h circular a r r a n g e m e n t is a cyclic permutation.
Figure 6.9
Pl
P5
Before we find the n u m b e r of cyclic p e r m u t a t i o n s of t h e five beads in E x a m p l e 6.14, the following general r e s u l t will be useful to prove. The n u m b e r of cyclic p e r m u t a t i o n s of n (distinct) items is (n  1)!.
PROOF: To avoid duplicates, let us assign a fixed position to the first i t e m a 1 a r o u n d the circle (see Figure 6.10). Now n  1 positions are left. So t h e second i t e m a2 can be placed in any one of the n  1 positions. Now n  2 positions
Figure 6.10
a1
are left. Therefore, the t h i r d i t e m a3 can be placed in a n y of the n  2 positions. C o n t i n u e like this until all i t e m s have been placed. Thus, by t h e multiplication principle, the n u m b e r of cyclic p e r m u t a t i o n s is 1. (n  1) (n  2 ) .  . 2 . 1 = (n  1)! m The next example illustrates this result. ~
F i n d the n u m b e r of different ways five zinnias can be p l a n t e d in a circle.
Chapter 6 Combinatorics and Discrete Probability
356
SOLUTION: N u m b e r of ways of planting~ _ f N u m b e r of cyclic p e r m u t a t i o n s ) five zinnias in a circle ] ~of five items = ( 5  1 ) ~  24
m
Let us r e t u r n to "linear" p e r m u t a t i o n s . We would like to find the recurrence relation satisfied by P ( n , r). We will use a combinatorial a r g u m e n t to find it, leaving the s t r a i g h t f o r w a r d algebraic proof as a r o u t i n e exercise (see Exercise 45). The n u m b e r of r  p e r m u t a t i o n s of n distinct e l e m e n t s satisfies the recurrence relation P ( n , r)  P ( n  1, r) + r P ( n  1, r  1), where 0 < r < n. PROOF: Let X be a set with n elements and x an a r b i t r a r y element in it. The set of r  p e r m u t a t i o n s of X can be partitioned into two subsets: A, the set of p e r m u t a t i o n s n o t containing x, and B, the set of p e r m u t a t i o n s c o n t a i n i n g x. o f e l e m e n t s i n A" Since no p e r m u t a t i o n s in A contain x, every element in A is an r  p e r m u t a t i o n of n  1 elements. The n u m b e r of such p e r m u t a t i o n s is P ( n  1, r).
9 To find the number
o f e l e m e n t s i n B: Since every p e r m u t a t i o n in B contains x, n  1 candidates are left in X for the r e m a i n i n g r  1 positions. They can be a r r a n g e d in P ( n  1, r  1) ways. Now the position ofx in a p e r m u t a t i o n has r choices. Therefore, by the multiplication principle, r P ( n  1, r  1) p e r m u t a t i o n s contain x.
9 To find the number
Since A and B are disjoint sets, by the addition principle, P(n,r) 
IAI + IBI
= P ( n  1, r) + r P ( n  1, r  1)
m
Note t h a t it's m u c h easier to c o m p u t e P ( n , r) u s i n g the explicit f o r m u l a in T h e o r e m 6.4 r a t h e r t h a n by u s i n g the recursive. T r y P(5, 3) b o t h ways to see the difference.
Fibonacci Numbers Revisited The following example p r e s e n t s an i n t e r e s t i n g confluence of p e r m u t a t i o n s and Fibonacci n u m b e r s . ~
Let Pn denote the n u m b e r of p e r m u t a t i o n s f of the set Sn   { 1 , 2 , . . . n } such t h a t li  f ( i ) l < 1 for all 1 < i < n, where n > 1. So Pn counts the n u m b e r of p e r m u t a t i o n s t h a t move each element no more t h a n one position from its n a t u r a l position.
o~.~
L~
L~
~L
cr~
o
~
0
o
~D
~
~'~.
~.~
j~
Jr
II
II
C~
II
II
iL
cr~
h~
C~
L~
II
IIo
o
~D
IIo 0
0
~L pmL
i., o
~j
0
= S~
Chapter 6 Combinatorics and Discrete Probability
358
C a s e 1 L e t f ( n ) = n. T h e n t h e r e m a i n i n g n  1 e l e m e n t s can be u s e d to form P n  1 p e r m u t a t i o n s such t h a t ]i  f ( i ) ] < 1 for all i. C a s e 2 L e t f ( n ) V= n. T h e n f ( n )  n  1 a n d f ( n  1)  n. T h e r e m a i n i n g n  2 e l e m e n t s can be e m p l o y e d to form P n  2 p e r m u t a t i o n s w i t h t h e d e s i r e d property.
T h u s , by t h e a d d i t i o n principle, Pn  Pn1 tPn2, w h e r e P l  1 a n d P2 = 2. It now follows t h a t Pn = Fn+l, w h e r e n > 1, as conjectured, m Since t h e total n u m b e r of p e r m u t a t i o n s of Sn is n!, it follows from this e x a m p l e t h a t t h e r e are n !  F n + l p e r m u t a t i o n s f o f S n such t h a t l i  f ( i ) i > 1 for some i n t e g e r i, w h e r e 1 < i < n. T h u s t h e r e are n !  F n + l p e r m u t a t i o n s of Sn t h a t move at least one e l e m e n t of Sn by two spaces f r o m its n a t u r a l position. In p a r t i c u l a r , t h e r e are 3!  F4  3 such p e r m u t a t i o n s of t h e set {1, 2, 3}, as F i g u r e 6.12 depicts. Figure 6.12 1
1
1
1
2
2
2
2
Exercises 6.2
E v a l u a t e each. 1. 5! 4~
2.
10'. 3~7~
3. P ( 5 , 3 )
4. P ( 6 , 6 )
M a r k each s e n t e n c e as t r u e or false, w h e r e n is an a r b i t r a r y n o n n e g a t i v e i n t e g e r a n d 0 < r _< n. 5. 0 !  0
6. 1 !  1
7. 5 . 4 ! = 5!
8. (m + n)!  m! + n!
9. ( 2 + 3 ) ! = 2 ! + 3 !
10. (mn)! = m! n!
11. ( 2 . 3 ) !  2! 3!
12.
13. P ( n , 0) = 0
14. P ( n , 1) = P ( n , n  1)
15. P(n, r) = P ( n , n  r)
16. n! is divisible by 10 if n > 4.
n(n

1 ) !  n!
17. F i n d t h e n u m b e r of twodigit n u m e r a l s t h a t can be f o r m e d u s i n g t h e digits 2, 3, 5, 6, and, 9 a n d t h a t c o n t a i n no r e p e a t e d digits. 18. F i n d t h e n u m b e r of t h r e e  d i g i t n u m e r a l s t h a t can be f o r m e d u s i n g t h e digits 2, 3, 5, 6, a n d 9, if r e p e t i t i o n s are not allowed.
6.2
Permutations
359
Find the n u m b e r of words t h a t can be formed by scrambling the letters in each word. 19. algorithm
20. word
21. computer
22. logic
The password for a computer system consists of eight distinct alphabetic characters. Find the n u m b e r of passwords possible that: 23. End in the string MATH.
24. Begin with the string CREAM.
25. Contain the word C O M P U T E R as a substring. 26. Contain the letters WORD together, but in any order. 27. Contain the string BLACK or the string WHITE. 28. Contain the strings BLACK and WHITE. 29. Do not contain the string SAMPLE. A botanist would like to plant three coleus, four zinnias, and five dahlias in a row in her front garden. How m a n y ways can she plant t h e m if: 30. They can be planted in any order. 31. Plants of the same family m u s t be next to each other. 32. The family of zinnias must be in between the other two families. Find the n u m b e r of ways seven boys and three girls can be seated in a row if: 33. A boy sits at each end of the row. 34. A girl sits at each end of the row. 35. The girls sit together at one end of the row. 36. Show t h a t P ( n , O) = 1. Using the recursive definition of P ( n , r), evaluate each. 37. P(5, 4)
38. P(6, 0)
39. P(3,2)
40. P(6,3)
Solve each equation.
P(n, 2)
41. P ( n , 1 ) = 6
42.
43. P(n, n  1)  5040
44. P(5, r)  20
 42
45. Using Theorem 6.4, prove t h a t P ( n , r)  P ( n  1, r) + r P ( n  1, r  1). Verify each. 46. ( n + l ) ! + n !  ( n + 2 ) n !
47.
(n+l)!n!n(n!)
48. Prove by induction that 1.1! + 2 . 2 ! + . . . + n . n !  ( n + 1 ) !  1, n > 1. 49. Write an algorithm to compute P ( n , r), using Theorem 6.4.
360
Chapter6 Combinatorics and Discrete Probability 50. Write a recursive algorithm to compute P(n, r). "51. Show t h a t (n!)! > (2n)!, if n > 3.
At the beginning of the 18th century, the following problem was proposedi1!
i
iii
A secretary had written n different letters and addressed n different envelopes for them. Unfortunately, a wind storm mixed up the letters and the envelopes. After the storm was over, each letter was placed in an envelope. In how m a n y ways can the letters be placed in the envelopes, so t h a t every letter is in a w r o n g envelope? i
i
i
i
ii
i
i i
i
ill
This problem has several variations. One involves n couples a t t e n d i n g a dance. In how m a n y ways can the men dance with women other t h a n their own wives? A second variation involves n guests checking in their coats at the coat room of a fancy r e s t a u r a n t . In how m a n y ways can the a t t e n d a n t r e t u r n their coats, so no person gets the right coat? Before answering these problems, we make the following definition.
Derangement A p e r m u t a t i o n of n distinct items a 1, a 2 , . . . , a , in which no item ai appears in its original position i for any i, 1 _< i _< n, is called a d e r a n g e m e n t . We would like to find the n u m b e r of possible d e r a n g e m e n t s of n items, so we begin with an example. ~
Find the n u m b e r of d e r a n g e m e n t s of the elements 1, 2, 3, and 4. SOLUTION: There are nine d e r a n g e m e n t s of the four elements, namely: 2143
3142
4123
2341
3412
4312
2413
3421
4321
The p e r m u t a t i o n 2314 is not a d e r a n g e m e n t since 4 appears in its n a t u r a l position, m
6.3 Derangements
361
Let Dn denote the n u m b e r of d e r a n g e m e n t s of n items. T h e n Do  1, since t h e r e is one d e r a n g e m e n t with no elements. (This will be verified later.) T h e r e is no d e r a n g e m e n t with one element, so D1  0. T h e r e is exactly one d e r a n g e m e n t of the e l e m e n t s 1 and 2, namely, 21; therefore, D2  1. T h e r e are two d e r a n g e m e n t s of the elements 1, 2, and 3 : 2 3 1 and 312; therefore, D3  2. It follows by Example 6.15 t h a t D4  9.
To find an explicit formula for Dn, first we derive a r e c u r r e n c e relation satisfied by Dn. Let Dn denote the n u m b e r of d e r a n g e m e n t s of n distinct items. T h e n Dn  ( n 
1)(Dn1 + Dn2),
n > 2
(6.1)
PROOF: Let a l, a 2 , . . . , an denote the n items. I t e m an can be placed in a n y one of the positions 1, 2 , . . . , (n  1), so the location of an has n  1 choices. Suppose it is placed in position 1 (see Figure 6.13). Let us now consider the following two cases:
F i g u r e 6.13
available an 1 .
.
.
.
.
2
.
.
3
n
positions
C a s e 1 Suppose al is placed in position n (see Figure 6.14). T h e n n  2 items are left, namely, a 2 , a 3 , . . . ,an1 and also n  2 positions, namely, positions 2 t h r o u g h n  1. The n u m b e r of d e r a n g e m e n t s of n  2 items, by definition, is Dn2. Thus, if a l is placed in position n, t h e r e are Dn2 derangements.
F i g u r e 6.14
available an .
.
.
f
.
.
~
.
.
,
positions
,
a1
Chapter6 Combinatoricsand Discrete Probability
362
C a s e 2 Suppose a l is not placed in position n (see F i g u r e 6.15). T h e n a l m u s t occupy one of t h e positions 2 t h r o u g h n  1 a n d one of t h e i t e m s a2 t h r o u g h an1 m u s t occupy position n. Thus, we have n  1 i t e m s a n d n  1 positions, yielding Dn1 d e r a n g e m e n t s . F i g u r e 6.15
available
an
~~2",..~
not a 1
9 S n
positions Thus, with an in position 1, a total of Dn1 +Dn2 d e r a n g e m e n t s are possible, by the addition principle. Since an m a y occupy a n y one of the n  1 positions, t h e total n u m b e r of d e r a n g e m e n t s is (n  1)(Dn1 + Dn2). T h u s Dn  ( n 1)(Dn1 +Dn2), n >_ 2. m U s i n g T h e o r e m 6.8, Dn can be defined recursively as follows.
A Recursive Definition of
Dn Do
1,
Dn
(n
D1  0 1)(D~_1 + Dn2),
n > 2
(6.1)
T h e r e c u r r e n c e relation (6.1) can prove t h a t Do m u s t be 1. U s i n g t h e recurrence relation, D2 = (2  1)(D1 + Do). Since D2 = 1 a n d D1  0, this yields 1 = 1(0 + Do). F o r this to be true, Do m u s t be 1, as in t h e above definition. T h e next example illustrates this recursive definition. Use t h e recursive definition of Dn to c o m p u t e D3 and D4. SOLUTION: D3  (3  1)(D2 + D1)
D4  (4  1)(D3 + D2)
= 2(1 + 0)
= 3(2 + 1)
=2
=9
Notice t h a t these a n s w e r s agree w i t h those obtained earlier,
m
The above recursive definition can be used to derive an a l t e r n a t e definition of Dn.
6.3 Derangements
363
Notice t h a t the r e c u r r e n c e relation (6.1) can be r e w r i t t e n as Dn  nDn1 :
(  1 ) [ D n  1  (n  1)Dn2]
To simplify this, we s u b s t i t u t e d n  D n  n D n  1 . T h e n it becomes dn1, w h e r e d l  D1  1D0 = 0  1(1)   1 . Solving this r e c u r r e n c e relation (see Exercise 9), dn  (  1 ) n,
dn
=
n _> 1
Thus Dn  nDn_ l 
(  1)n
Dn 
nDn_l
T h a t is, ~ (1)n,
(6.2)
n >__ 1
Accordingly, D n can be defined recursively as follows: An Alternate Recursive Definition of Dn Do
1
n>l
D n  n D n  1 + (  1)n,
(6.3)
The next example uses this a l t e r n a t e definition. ~
With the a l t e r n a t e definition (6.3), c o m p u t e D 3 and D4. SOLUTION: D3 = 3D2 + (  1 ) 3
D4 = 4D3 + (  1 ) 4
= 3(1) + (  1 )
= 4(2) + 1
=2
=9
m
The r e c u r r e n c e relation (6.2) can be solved u s i n g the i t e r a t i o n m e t h o d (see Exercise 10). The solution is given in the following t h e o r e m . The n u m b e r of d e r a n g e m e n t s of n distinct e l e m e n t s is
D n = n !
1
1 1 1 1 ~.. + 2~"  3~ + 4~ . . . .
(  1 ) n II
+
n!
A'
n > 0
m

r An i n t e r e s t i n g observation: It is s h o w n in calculus t h a t e1 = ~ n=O
(_l)n n!
so t h e expression inside t h e b r a c k e t s in t h e f o r m u l a is t h e s u m of t h e first (n + 1) t e r m s in t h e expansion of e 1. See Exercise 40.
Chapter6 Comblnatoricsand Discrete Probability
364
The next example illustrates this theorem. ~
Using Theorem 6.9, compute D5. SOLUTION:
( D5=57
1 1~q
1
1
2?
37 F 4?
( =120
1 1 11t
2
1
1) 5?
1
1)
6 ~ 24
120
= 44 II Returning to the 18thcentury problem, n letters can be placed in wrong envelopes in Dn ways, where the value of Dn is given by Theorem 6.9. Exercises 6.3 Using the recursive definition (6.1), compute the number of derangements
Dn for each value of n. 1.5
2.6
3.7
4.10
7. D6
8. D7
Using Theorem 6.9, compute each. 5. D2
6. D4
9. Solve the recurrence relation dn   d n  1 , n >__2, where d l   1 . 10. Solve the recurrence relation (6.2). Prove each.
11. Dn is even if n is an odd integer. 12. Dn is odd if n is an even integer. Let bn denote the number of computations (additions and multiplications) needed to find Dn, using the recursive definition (6.1). Compute the following. 13. b2
14. b3
15. b4
16. b5
17. Define bn recursively. Using the recursive definition of bn, compute each. 18. b4
19. b5
20. b6
21. b8
22. Prove that bn is an even integer for every n >_ 0. Let Cn denote the number of computations (additions and multiplications) needed to find Dn, using the recursive definition (6.3). Compute each. 23. C2
24. c3
25. C4
26. c5
6.4 Combinations
27. Define
365
recursively.
Cn
Using the recursive definition of Cn, compute each. 28. c4
29. c5
30. c6
31. c8
32. Solve the recurrence relation obtained in Exercise 27. 33. Show that
Cn  O ( n ) .
34. Show that
D n  3 . 4
.....
n  4.5
.....
n ~ . . . q (  1 ) n  1
n + ( _ 1)n.
35. Let a n denote the n u m b e r of multiplications needed to compute using the formula in Exercise 34. Show t hat a n  O(n2). Letpn

Compute the value
Dn.
n! value of n. 36. 5
ofpn
37. 6
Dn,
correct to six decimal places for each 38. 7
39.
10
*40. (For those familiar with the concept of a limit) Using Exercises 3639, predict lim P n . n~
(1) n 41. Show t h a t p n
Pn1
= ~ .
n! 42. Using Exercise 41, derive the explicit formula for D n . ( H i n t : Solve the recurrence relation in Exercise 41.) The formula in Exercise 41 can be derived in a slightly different way also. Verify each. n1 43.
Pn
45.
Pn
n
1 Pn1 +Pn2 n
1 44.
PnPn1
=
n
(Pn1Pn2)
(1) n  Pn1
(Hint:
Let
=
n! gn = Pn  Pn 1
in Exercise 44.)
46. Write a nonrecursive algorithm to compute 47. Write a recursive algorithm to compute
Dn.
Dn.
Recall that a permutation is an ordered arrangement of elements in a set. Sometimes, however, the order of elements is irrelevant; only their membership is important. We will investigate such unordered arrangements in this section. For example, a committee such as A  {Costa, Shea,. Weiss, Hall, Chen} is just a set, and the order in which the names are listed is immaterial. Suppose we would like to form a subcommittee of A
Chapter 6 Combinatorics and Discrete Probability
366
c o n s i s t i n g of t h r e e m e m b e r s . T h r e e s u c h s u b c o m m i t t e e s are: { C o s t a , S h e a , W e i s s } , {Costa, S h e a , Hall}, a n d { C o s t a , S h e a , C h e n } (see E x a m p l e 6.20). E a c h is a c o m b i n a t i o n of t h e five e l e m e n t s t a k e n t h r e e a t a t i m e , o r a
3combination. More generally, we m a k e the following definition.
Combination A n r  c o m b i n a t i o n of a s e t of n e l e m e n t s , w h e r e 0 < r < n, is a s u b s e t containing r elements. T h e n u m b e r o f r  c o m b i n a t i o n s o f a s e t w i t h n e l e m e n t s is d e n o t e d b y
C(n,r) o r ( n ) . * B o t h n o t a t i o n s f r e q u e n t l y a p p e a r in c o m b i n a t o r i c s . T h e n u m b e r of c o m b i n a t i o n s is also c a l l e d t h e b i n o m i a l c o e f f i c i e n t * * for r e a s o n s t h a t will b e c l e a r f r o m S e c t i o n 6.6. B e f o r e d e r i v i n g a f o r m u l a for C(n, r), let u s s t u d y t h e f o l l o w i n g e x a m p l e . F i n d t h e n u m b e r of r  c o m b i n a t i o n s of t h e s e t {a, b, c}, w h e n r or3.
0, 1, 2,
SOLUTION: 9 E x a c t l y o n e s u b s e t c o n t a i n s z e r o e l e m e n t s : t h e n u l l set. N u m b e r of 0  c o m b i n a t i o n s  C(3, 0)  1. 9 T h r e e s u b s e t s c o n t a i n o n e e l e m e n t each" { a}, {b }, a n d { c }. N u m b e r of 1  c o m b i n a t i o n s  C(3, 1)  3. 9 T h r e e s u b s e t s c o n t a i n t w o e l e m e n t s each" {a, b }, { b, c }, a n d { c, a }. N u m b e r of 2  c o m b i n a t i o n s  C(3, 2)  3. 9 F i n a l l y , e x a c t l y o n e s u b s e t c o n t a i n s t h r e e e l e m e n t s : t h e s e t itself. N u m b e r of 3  c o m b i n a t i o n s  C(3, 3)  1.
m
W e n o w d e r i v e a f o r m u l a for C(n, r). T h e n u m b e r of r  c o m b i n a t i o n s of a s e t of n e l e m e n t s is g i v e n b y C(n, r) n! 0 0. In particular, g5  C(5, 0) + C(5,2) + C(5, 4)  1 + 10 + 5  16 a n d g6  C(6, 0) + C(6, 2) + C(6, 4)  1 + 15 + 15  31, as expected. We shall p u r s u e this example a bit f u r t h e r in Section 6.6. m
Exercises 6.4 A c o m m i t t e e consists of nine m e m b e r s . F i n d t h e n u m b e r of s u b c o m m i t t e e s t h a t can be formed of each size. 1. Two
2. Five
3. Six
4. Seven
6.4
Combinations
373
5. Find the n u m b e r of ways a committee of three s t u d e n t s and five professors can be formed from a group of seven s t u d e n t s and 11 professors. 6. Find the n u m b e r of ways a committee of four students, four professors, and t h r e e a d m i n i s t r a t o r s can be formed from a group of six students, eight professors, and five administrators. 7. Find the n u m b e r of lines t h a t can be d r a w n using 10 distinct points, no three being collinear. 8. Find the n u m b e r of triangles t h a t can be drawn using 10 points, no three being collinear. 9. Solve the recurrence relation in Example 6.22. 10.
Prove the formula in Example 6.22, using induction.
Let A be a 10element subset of the set {1, 2 , . . . , 20}. 11. Determine if A has two fiveelement subsets t h a t yield the same sum of the elements. 12. Determine if A has two eightelement subsets t h a t yield the same sum of the elements. ( T w e l v e D a y s o f C h r i s t m a s ) Suppose t h a t on the first day of C h r i s t m a s you sent your love 1 gift, 1 + 2 gifts on the second day, 1 + 2 + 3 gifts on the third day, and so on. 13. Find the n u m b e r of gifts sent on the 12th day. 14. Find the total n u m b e r of gifts sent in 12 days. 15. Show t h a t the n u m b e r of gifts sent on the n t h day is C(n + 1, 2), where 1 n, the elements of each combination m u s t be repeated. Consequently, a combination may contain t h r e e a's, two a's and one b, one a and two b's, or three b's. Using the set notation, the 3combinations are {a,a,a}, {a,a,b}, {a,b,b} and {b,b,b}. So there are four 3combinations of a set of two elements, m Find the n u m b e r of 3combinations of the set {a, b, c}, where the elements may be repeated. SOLUTION: Again, using the set notation, the various 3combinations are:
{a,a,a}
{a,a,b}
{b,b,b}
{b,b,c} {b,c,c}
{a,a,c}
{a,b,b}
{a,b,c}
{a,c,c}
{c,c,c} Thus the set
{a,b,c} has 10 3combinations.
m
Before developing a formula for the n u m b e r of combinations with repetitions, let us r e t u r n to the beverage problem posed earlier. Five friends would like to order beverages with their d i n n e r at a local r e s t a u r a n t t h a t serves iced tea, hot tea, or coffee. Find the n u m b e r of beverage orders possible. SOLUTION: A convenient notation will prevent confusion.
380
Chapter 6
Combinatorics and Discrete Probability
Denote each type of beverage by a dash and separate t h e m u s i n g two slashes, as shown below: /
/
iced tea
hot tea
coffee
M a r k each person's selection by an X in the appropriate area. For instance, the distribution XX / X / XX indicates t h a t two people selected iced tea, one selected hot tea, and two selected coffee; t h e distribution XXX / / XX means, three people selected iced tea, none ordered hot tea, and two selected coffee. T h u s the n u m b e r of possible beverage orders equals the n u m b e r of p e r m u t a t i o n s of seven items (five X's and t w o / ' s ) of which five are alike (X's) and the other two are alike (/'s): 7? =21 5?2?
m
This solution strategy produces the following theorem. The n u m b e r of rcombinations with repetitions from a set of n e l e m e n t s is C(n + r 
1, r).
PROOF"
Each rcombination with repeated elements from a set of n e l e m e n t s can be considered a string of r X's and (n  1) slashes, as in Example 6.32. Each string contains r + n  1  n + r  1 symbols, of which r are alike (X's) and n  1 are alike (slashes). Therefore, by T h e o r e m 6.13, the n u m b e r of such strings, t h a t is, rcombinations, equals (n + r  1)?
r?(n1)?
=C(n+rl,r)
m
This theorem helps solve the next example. There are five types of soft drinks at a fast food r e s t a u r a n t : Coke Classic, Diet Coke, root beer, Pepsi, and Sprite. Find the n u m b e r of beverage orders 11 guests can make. SOLUTION: Since there are five types of soft drinks, n = 5. Each beverage order is a selection containing 11 items, t h a t is, an l 1combination with r e p e a t i n g elements. Therefore, by T h e o r e m 6.14, the n u m b e r of possible beverage orders equals C(n + r 
1, r)  C(5 + 11  1, 11) = C(15, 11) =
157 = 1365 11747
m
6.5 Permutations and Combinations with Repetitions
381
This problem has a nice i n t e r p r e t a t i o n . Let x i denote the n u m b e r of guests ordering soft d r i n k i, w h e r e 1 < i < 5. T h e n xl +x2 +x3 +x4 +x5 = 11, where x i > O. T h e n u m b e r of n o n n e g a t i v e integer solutions of this e q u a t i o n is the same as the n u m b e r of possible beverage orders, so the n u m b e r of integer solutions of this e q u a t i o n is C(5 + 11  1, 11)  C(15, 11)  1365. The next t h e o r e m generalizes this result, simply r e s t a t i n g T h e o r e m 6.14. Let X l , X 2 , . . . ,Xn be n n o n n e g a t i v e integer variables and integer. The e q u a t i o n X l + x 2 + . . . + X n  r has C ( n + solutions,
r a r
nonnegative 1, r) integer m
F i n d the n u m b e r of solutions of the equation Xl ~x2 + x3  5
(6.6)
where Xl,X2, and x3 are n o n n e g a t i v e integer variables. SOLUTION: H e r e r  5 and n  3. By T h e o r e m 6.15, the n u m b e r of solutions is C(n
+ r 
1, r)  C(3 + 5  1, 5) = C(7, 5)  21
(Can you list all the solutions? See E x a m p l e 6.35 also.)
m
T a k i n g this example a step further, suppose you would like to find all solutions of E q u a t i o n (6.6), w h e r e Xl, x2, x3 > 1. M a k e the s u b s t i t u t i o n Y i  x i  1, 1 _< i _< 3. Clearly, Y i >_ O. E q u a t i o n (6.6) becomes Yl +Y2 +Y3  2 By T h e o r e m 6.15, this equation has C ( n + r  1, r) = C(3 + 2  1, 2) C(4, 2)  6 solutions: (0, 1, 1), (1, 0, 1), (1, 1, 0), (2, 0, 0), (0,2, 0), and (0, 0,2). Consequently, E q u a t i o n (6.6) with x i > 1 has six solutions" (1, 2, 2), (2, 1,2), (2,2, 1), (3, 1, 1), (1,3, 1), and (1, 1,3).
F o r Loops Revisited The following two examples provide applications of both these formulas to loops.
for
Find the n u m b e r of times the a s s i g n m e n t s t a t e m e n t x ~ x + 1 is executed by the following nested f o r loops: For i = 1 t o n do For j
= 1 t o i do
x ~x+
1
Chapter6 Combinatoricsand Discrete Probability
382
SOLUTION: Notice t h a t t h e a s s i g n m e n t s t a t e m e n t is e x e c u t e d for each p a i r (j, i), w h e r e 1 < j _< i < n. F o r example, t h e s t a t e m e n t is e x e c u t e d w h e n j  3, i  5; j  3, i  3; b u t not w h e n j  3 a n d i  2. T h u s t h e n u m b e r of e x e c u t i o n s equals t h e n u m b e r of pairs (j, i) w i t h r e p e t i t i o n s allowed; in o t h e r w o r d s , it equals t h e n u m b e r of 2selections of t h e set {1, 2, 3 , . . . , n}. T h e n u m b e r of such 2selections is C(n + 2  1,2), so t h e a s s i g n m e n t is e x e c u t e d in C(n + 2  1, 2)  C(n + 1, 2)  tn different ways. m We now p u r s u e this e x a m p l e w i t h one m o r e level added to t h e for loops. F i n d t h e n u m b e r of t i m e s t h e a s s i g n m e n t s t a t e m e n t x ~ x + 1 is e x e c u t e d by t h e following n e s t e d for loops" For
i
= 1 to
For j For
n do
= 1 to
i
do
k = I to
j
do
x ~  x + l
SOLUTION: T h e a s s i g n m e n t s t a t e m e n t is e x e c u t e d for each t r i p l e t (k, j , i), w h e r e 1 _~ k _<j _~ i _~ n and w h e r e r e p e t i t i o n s are allowed. T h e n u m b e r of s u c h 3selections of the set {1, 2, 3 , . . . , n} is C(n + 3  1, 3), so t h e a s s i g n m e n t is e x e c u t e d C(n + 3  1, 3)  C(n + 2, 3)  T,, different ways. m
A Generalization More generally, consider t h e following n e s t e d s e q u e n c e of for loops: For
ii
For
= 1 to i2 = i
For
n do to
i3 = i
For
il to
i r 
do i2
I to
do
i r

i
do
x ~ x + l
It follows from the c o m b i n a t o r i a l a r g u m e n t s in E x a m p l e s 6.35 a n d 6.36 t h a t t h e n u m b e r of t i m e s t h e a s s i g n m e n t s t a t e m e n t x ~ x + 1 is e x e c u t e d is given by t h e n u m b e r of rselections of t h e set {1, 2, 3 , . . . , n } w i t h r e p e t i t i o n s allowed, t h a t is, C(n + r  1, r).
Triangular and Tetrahedral Numbers Revisited R e t u r n i n g to t r i a n g u l a r n u m b e r s t,z a n d t e t r a h e d r a l n u m b e r s Tn, we find t h a t t h e i r f o r m u l a s fit into t h e one in T h e o r e m 6.14:
t,z  n(n + 1)/2
= C(n + 1,2)  C(n + 2  1,2)
Tn  n(n + 1)(n + 2)/6  C(n + 2, 3)  C(n + 3  1, 3)
6.5 Permutations and Combinations with Repetitions
383
Generating F u n c t i o n s and Combinations
G e n e r a t i n g functions, i n t r o d u c e d in C h a p t e r 5, are a valuable tool in solving c o m b i n a t o r i a l p r o b l e m s involving r e p e a t e d elements. T h e n e x t t h r e e examples i l l u s t r a t e this m e t h o d . U s i n g g e n e r a t i n g functions, find t h e n u m b e r of beverage orders possible in E x a m p l e 6.32. SOLUTION: Suppose the t h r e e beverages are ordered by i, j , a n d k patrons. T h e n every beverage order can be considered a 3tuple (i, j, k), w h e r e i, j , k > 0 a n d i + j § k  5. Let x i denote t h a t iced t e a was ordered by i c u s t o m e r s . Since 0, 1, 2, 3, 4, or 5 people can order iced tea, we use t h e polynomial 1 § x § x 2 § x 3 § x 4 § x 5 to r e p r e s e n t t h e various possibilities. (Notice t h e exponents.) Both hot tea a n d coffee can also be ordered by 0, 1, 2, 3, 4, or 5 people; correspondingly, t h e polynomials 1 + y § y2 § y3 § y4 § y5 a n d 1 § z § z 2 § z 3 § z 4 § z 5 r e p r e s e n t these possibilities. C o n s e q u e n t l y , we w a n t p r o d u c t s of the form xiyJz k, w h e r e i § j § k  5 in t h e p r o d u c t
t x/timothy!
i=o = k=O For convenience, let x  y  z. T h u s the total n u m b e r of beverage orders
possible is the coefficient o f x 5 in the product ( ~ xi) a. i=0 You m a y verify t h a t it is 21. Table 6.2 shows the various possible beverage orders.
T a b l e 6.2
Iced tea
Hot tea
Coffee
Iced tea
Hot tea
Coffee
0 0 0 0 0 0 1 1 1 1 1
0 1 2 3 4 5 0 1 2 3 4
5 4 3 2 1 0 4 3 2 1 0
2 2 2 2 3 3 3 4 4 5
0 1 2 3 0 1 2 0 1 0
3 2 1 0 2 1 0 1 0 0
The next p r o b l e m r e s e m b l e s E x a m p l e 6.34, w i t h additional c o n s t r a i n t s on the variables. F i n d the n u m b e r of solutions of t h e e q u a t i o n xl + xl > 7, 1 < x2, x3 < 3, and 0 < x4 < 3.
X2 +
X3 +
X4  
11, w h e r e
384
Chapter 6 Combinatorics and Discrete Probability
SOLUTION: Again, g e n e r a t i n g functions can do t h e job for us. Since xl > 7 , x l can be 7, 8, 9, 10, or 11, r e p r e s e n t e d by the polynomial x 7 + x 8 + . . . + x 11 T h e polynomials c o r r e s p o n d i n g to the c o n s t r a i n t s 1 _< x 2 , x 3 ~ 3 are x + x 2 + x 3 and x + x 2 + x 3. For 0 < x4 < 3, we have t h e polynomial 1 + x + x 2 + x 3. T h u s the n u m b e r of solutions is t h e coefficient of x 11 in t h e p r o d u c t
t h a t is, t h e coefficient of x 2 in the p r o d u c t
( xi) ( xi) ( xi) (ioxi) You can verify t h a t it is 10. T h u s the e q u a t i o n has 10 solutions satisfying t h e given conditions. [The various solutions ( i , j , k , l) can be o b t a i n e d by picking the e x p o n e n t s in the p r o d u c t s xixJxkx l t h a t yield x 11 in t h e product (6.7).] m We close this section with an e x a m p l e t h a t is closely r e l a t e d to E x a m p l e 6.38. In how m a n y ways can 11 cookies be d i s t r i b u t e d a m o n g four c h i l d r e n m Amy, Betsy, Carol, and D a i s y   s o t h a t A m y gets at least seven cookies, both Betsy and Carol get at least one cookie each but not m o r e t h a n t h r e e , and Daisy gets no more t h a n t h r e e cookies? SOLUTION" Since A m y gets at least seven cookies, this case yields the p o l y n o m i a l x 7 + x s + x 9 + x 1~ + x 11. Similarly, the o t h e r t h r e e c o n s t r a i n t s yield t h e polynomials x + x 2 + x 3, x + x 2 + x 3, and 1 + x + x 2 + x 3. T h u s the n u m b e r of ways of d i s t r i b u t i n g 11 cookies u n d e r t h e given conditions is the coefficient of x 11 in the product (6.7) above, namely, 10. m Exercises 6.5 Find t h e n u m b e r of distinct words t h a t can be formed by s c r a m b l i n g t h e letters in each word. 1. C A L C U L U S
2. T A L L A H A S S E E
Find the n u m b e r of bytes that: 3. C o n t a i n exactly two 0's.
4. C o n t a i n exactly five 0's.
5. C o n t a i n at least five 0's.
6. C o n t a i n not m o r e t h a n two 0's.
7. C o n t a i n exactly eight 0's.
8. C o n t a i n exactly nine 0's.
6.5
Permutations and Combinations with Repetitions
385
Find the num ber of ternary words over the alphabet {0, 1, 2} that are of length four and: 9. Contain exactly three O's.
10. Contain at least six O's.
11. Contain at most two 0's. 12. Contain two O's, three l's, and three 2's. In Exercises 1316, use Figure 6.17 to find the num ber of possible routes from A to the given point, traveling easterly or northerly for the given number of blocks. 13. Point F and 5 blocks.
14. Point C and 8 blocks.
15. Point D and 9 blocks.
16. Point E and 11 blocks.
List the 4combinations of each set. 17. {a}
18. {a,b}
19. There are five types of desserts available at a restaurant. Find the number of ways eight people can select them, if order does not matter. 20. A restaurant offers six choices for the main dish. How many ways can a group of nine women select the main dish? Assume that order does not matter. 21. In how many ways can 10 quarters in a piggy bank be distributed among 7 people? Find the number of solutions to each equation, where the variables are nonnegative integers. 22. Xl +x2 +x3 = 3
23. Xl +x2 +x3 +x4 = 7
24. xl +x2 +x3 +x4 = 10
25. Xl +x2 +x3 +x4 +x5 = 11
Find the number of solutions to each equation, where xi > 1. 26. x l + x 2 + x 3 + x 4 = l l
27. x l + x 2 + x 3 + x 4 + x 5 = 1 3
Use generating functions to solve the following counting problems. 28. Use generating function (6.7) to find the various solutions of the equation in Example 6.38. Find the number of solutions to each equation. 29.
x l + x2 + x3
=
10, Xl >__3, 1 < X2 _< 3, X3 > 5
30. X l + X 2 + X 3 = 1 2 , Xl, X 2 > 5 , 1 < X 3 < 4 31. X l + X 2 + X 3 + X 4 = 1 0 ,
Xl, X 2 > 2 , X 3 > 0 , X 4 > 5
32. X l + X 2 + X 3 + x 4 = l l ,
x1, x 2 > 2 , 2 < X 3 < 4 ,
X4>3
3334. Find the solutions to the equations in Exercises 29 and 30.
386
Chapter 6
Combinatorics and
Discrete Probability
35. Find the number of ways 10 quarters can be distributed among three peopleAaron, Beena, and C a t h y   s o that both Aaron and Beena get at least one quarter, Beena gets no more than three, and Cathy gets at least two. 36. Find the number of ways 11 raisins can be distributed among four childrenDaisy, Emily, Francis, G a l l   s o that Daisy, Emily, and Francis get at least two raisins, Francis gets no more than four, and Gail gets at least three.
The binomial coefficients satisfy a vast array of properties. We shall visit a few of them shortly.
Pascal's Triangle The various binomial coefficients ( n ) , where 0 _< r _< n, can be arranged in the form of a triangle, called P a s c a l ' s t r i a n g l e , * as shown in Figures 6.20 and 6.21.
Figure 6.20
(0) (1) (~1) (20) (21)0 (0)(]) (~)0 (4)0 (~)(~)(4)
Figure 6.21
1 1 1
4
2
6
e r o w l ( r o w 2 e r o w 3 ( r o w 4
e row0 1
3 ~,
e r o w 0
( r o w 1 1
3
e row2 1
4
e r o w 3 1
e r o w 4
Pascal's triangle has many intriguing properties: 9 Every row begins with and ends in 1. This is no coincidence, since C ( n , O) = 1 = C ( n , n ) . *Although Pascal's triangle is n am e d after Pascal, it appeared in a 1303 work by the Chinese m a t h e m a t i c i a n Chu ShiKie.
6.6
387
The Binomial Theorem
9 Pascal's triangle is symmetric about a vertical line through the middle. This is so since C(n, r) = C(n, n  r). 9 Any interior number in each row is the sum of the numbers immediately to its left and to its right in the preceding row. This is so by virtue of Pascal's identity. 9 Recall that, by Example 6.22, the maximum number of places fn into which a pizza can be divided with n distinct cuts is given by fn = C(n, O) + C(n, 1) + C(n, 2). It is the sum of the first three numbers in row n of Pascal's triangle (see Figure 6.22).
Figure 6.22 fl~
(1
f2~ f3~
f4 ~ (1
(1
1) 3
4
3) 6)
1 4
1
9 By Example 6.26, the maximum number of nonoverlapping regions formed inside a circle by joining n distinct points on it is given by gn = C(n, O) + C(n, 2) + C(n, 4). Using Pascal's identity, this formula can be rewritten a s g n = C ( n  1,0) + C ( n 1,1)+C(n1,2)+ C ( n  1, 3 ) + C ( n  1, 4). (Verify this.) Consequently, the value of gn can be obtained by adding the first five numbers in row n  1 of Pascal's identity (see Figure 6.23).
Figure 6.23
gl> g3 g4 g5 g6
~ (1 ~ (1 ~ ~ 4 ~ ~'1 5
(1) 2 3
3 6
10
0 1)
10
Pascal's Triangle and Abracadabra Next we show how Polya's abracadabra problem is related to Pascal's triangle. Beginning with a 1 at the apex and using Pascal's identity, build the rhombic array in Figure 6.24. Each entry in Figure 6.24 gives the number of paths from the apex to the corresponding location. So there are 252 = C(10, 5) possible paths from the apex to the bottommost point in the array.
388
Chapter6 Combinatoricsand Discrete Probability
Figure 6.24
1 1
1
1 1 1 1
2 3
4 5
6
1
3 6
4
10
10
15 21
1
20
5 15
35
35
56
1
70
1 6
21 56
126
126 252
Next we show how Catalan n u m b e r s 1, 1, 2, 5, 14, 4 2 , . . . can be extracted from Pascal's triangle.
Pascal's Triangle and Catalan Numbers In C h a p t e r 2 we defined the n t h
Catalan number C,,
as
(2n)! C, = ( n + l ) ! n ! '
(2n)! Since n i n i =
(2n) n
n >_0
, this can be r e w r i t t e n as
n) n+l
'
n~0 
Consequently, C,z can be obtained by dividing the
central binomial coeffi
cient(2;)byn+l. For example, C4  ~1(48 )  70 5 = 14; t h u s C4 is obtained by dividing by 5 the central element 70 in row 8 in Pascal's triangle. Figure 6.25 shows the first five central binomial coefficients; we can use t h e m to c o m p u t e the first five Catalan n u m b e r s . Catalan n u m b e r s , like Fibonacci and Lucas n u m b e r s , have a propensity to appear in quite unexpected places. We shall p u r s u e a few such delightful occurrences later in this Section and in Section 9.6. The next t h e o r e m shows how the binomial coefficients and T h e o r e m 6.13 are useful in finding the binomial expansion of (x + y)n. We shall prove it using a combinatorial a r g u m e n t (see Exercise 43 for an algebraic method).
6.6 The Binomial Theorem
389
F i g u r e 6.25 The central binomial coefficients.
1
1 1 1 1 1 1 1
7 8
4
Q
15
1 4
1
10 Q
35 56
1 3
10
21 28
 3
5 6
1
5 15
35 O
1 6
21 56
1 7
28
1 8
1
( T h e B i n o m i a l T h e o r e m ) * Let x a n d y be a n y real n u m b e r s , a n d n a n y n ( n) n o n n e g a t i v e integer. T h e n (x + y)n _ ~ xnryr. r0
PROOF: Since (x + y)n _ (x + y)(x + y)... (x + y) to n factors, (x + y)n is e x p a n d e d by m u l t i p l y i n g an x from some of t h e factors on t h e R H S a n d a y f r o m t h e r e m a i n i n g factors. T h a t is, every t e r m is o b t a i n e d by selecting an x f r o m a n y of t h e n  r factors a n d a y from t h e r e m a i n i n g r factors. Thus, every t e r m in t h e e x p a n s i o n is of the form Cxnry r, w h e r e C d e n o t e s t h e coefficient a n d 0 1. T h a t is, the s u m of the "even" binomial coefficients equals t h a t of the "odd" binomial coefficients. PROOF: Again by the binomial theorem,
Set x  1 and y   1 . Then, for n >_ 1, rl
0  I1 + (  1 ) i n  ~~ ( n ) l n  r (  1 ) r
r0
(o) T h a t is,
I
We now m a k e an i n t e r e s t i n g observation. Recall t h a t the binomial coefficient C(n,r) denotes the n u m b e r of subsets of size r of a set with n elements, so the L H S of E q u a t i o n (6.8) r e p r e s e n t s the total n u m b e r of subsets with an even n u m b e r of elements and the RHS r e p r e s e n t s t h a t with an odd n u m b e r of elements. Since the total n u m b e r of subsets is 2 n, each equals 2 n  l , by E q u a t i o n (6.8).
6.6 The Binomial Theorem
393
Next we t u r n to two i n t e r e s t i n g occurrences of Catalan n u m b e r s .
Catalan Numbers Revisited
The great Swiss m a t h e m a t i c i a n L e o n h a r d E u l e r (see C h a p t e r 8) discovered Catalan numbers. He found t h e m in his study of triangulations of convex polygons, t h a t is, dividing the interior of a convex polygon into t r i a n g u l a r areas by drawing nonintersecting diagonals. Let Cn denote the n u m b e r of triangulations of convex (n + 2)gon, where n > 1. It follows from Figure 6.26 t h a t C1  1, C2 = 2, C3  5, and C4 = 14. More generally, Euler established t h a t
Ca
2 96 . 1 0 . . . ( 4 n  2) (n + 1)!
F i g u r e 6.26
Triangulations of convex (n + 2)gons. CI= 1
C2=2
C35
C5=14
In 1759, the G e r m a n m a t h e m a t i c i a n J o h a n n Andreas von Segner (17071777), a contemporary of Euler, established a recursive procedure to compute Cn" Cn 
CoCn1
+ C1Cn2
'~ " " ~ C n  I C o
where Co  1. For example, C4  1 . 5 + 1 . 2 + 2 . 1 + 5 . 1 = 14.
Chapter6 Combinatoricsand Discrete Probability
394
Catalan's Parenthesization Problem Interestingly, Euler's triangulation problem is essentially the same as Catalan's parenthesization problem, which he solved in 1838: using n pairs of left and right parentheses, how many different ways we can parenthesize a sequence of n + 1 symbols for a binary nonassociative operation? For example, with two symbols, there is one possibility: (ab); with three symbols, there are two possibilities: ((ab)c) and (a(ab)); and with four symbols, there are five ways: ((ab) (cd)), (((ab)c)d), (a(b(cd))), (a((bc)d), and ((a(bc))d); they yield the Catalan numbers 1, 2, and 5; and so on. In 1961, H.G. Forder of the University of Auckland, New Zealand, showed that every triangulation of convex polygon yields a correctly parenthesized expression, and vice versa. This close relationship becomes clear if we consider the triangulation of the hexagon in Figure 6.27. We have labeled five of its sides a through e, leaving the base unlabeled. Label every diagonal spanning two adjacent sides with the concatentation of the corresponding labels in parentheses. Continue this algorithm until the base gets a label, as in Figure 6.28. Thus the triangulation in Figure 6.27 yields the correctly parenthesized expression (((ab)c)(de)). Retracing the steps, we can recover the triangulation from the expression.
Figure 6.27 Triangulation of a hexagon.
Figure 6.28
C
b
L
d
C
d
Parenthesized triangulation of a hexagon.
e
(((ab)c) (de))
Triangulations, Parenthesized Expressions, and Binary Numbers Each triangulation of a convex polygon and by extension the corresponding parenthesized expression, can be uniquely represented by a binary number. For instance, consider the expression (((ab)c)(de)) in Figure 6.28. If we replace each left parenthesis with a 1 and each letter with a 0 and ignore all right parentheses, the expression yields the binary number 111000100. We do not need to store the right parentheses, because if we know the left parentheses, we can insert the matching right parentheses correctly.
6.6 The Binomial Theorem
395
T h u s such a b i n a r y n u m b e r is a compact way of r e p r e s e n t i n g the expression and hence the triangulation. We shall revisit this b i n a r y designation in Section 9.6 on b i n a r y trees. Before closing this section, we derive the explicit f o r m u l a for C n . T h e proof employs the following g e n e r a t i n g function. (1  4x) 1/2  1  2 ~ 1 ( 2 n n   } ) x n n=l n
An Explicit Formula for the nth Catalan Number (optional) Let P n denote the n u m b e r of ways of p a r e n t h e s i z i n g n + 1 symbols u s i n g n pairs of left and right p a r e n t h e s e s . T h e n P1  1. So, let n > 2. T h e first i symbols can be p a r e n t h e s i z e d in P i ways and the next n  i symbols in P n  i ways, where I _< i _% 0 for every n, we t a k e the m i n u s sign, so 1~/i4x f(x)

Chapter 6 Combinatorics and Discrete Probability
396
Using the above power series expansion, this yields f(x) = ~
xn

n=l n
Thus
Pnl_n (2:~2) Cn1 So
Cn 
1(2;)
n+l
'
n>0 
as desired. Exercises 6.6
Find the coefficient of each. 1. xny 5 in the expansion of (x + y)8 2. x4y 6 in the expansion of ( x  y)10 3. x2y 6 in the expansion of (2x + y)8 4. x4y 5 in the expansion of (2x  3y) 9
Using the binomial theorem, expand each. 5. (x +y)4
6. ( x  y ) 5
7. ( 2 x  1 ) 5
8. (x + 2y) 6
Find the middle term in the binomial expansion of each. 9.
11.
(1)
4
x+
(
10.
x
2x+
12.
x
(1) x

6
x
( )1o x2 + ~5
Find the largest binomial coefficient in the expansion of each. 13. (x + y)5
14. (x + y)6
15. (x + y)7
16. (x + y)8
17. Using Exercises 1316, predict the largest binomial coefficient in the expansion of (x + y)n. Use Pascal's triangle in Figure 6.21 to answer Exercises 18 and 19. 18. Find the sum of the numbers along the northeast diagonals. 19. What do you notice about them?
6.6 The Binomial Theorem
397
The nth Fibonacci number Fn is given by the sum of the numbers along the nth northeast diagonal of Pascal's triangle; that is,
[(n1)/2J Fn
"
E
(ni1)~.
i=0 Using this formula, compute each Fibonacci number. 21. F2
20. F1
22. F5
23. F6
The Bell n u m b e r s Bn, named after the English mathematician Eric T. Bell (18831960) and used in combinatorics, are defined recursively as follows: B0=I Bn=~(nl)
i=0
i
Bi,
n>l
Compute each Bell number. 24. B2
25. B3
26. B4
27. B5
Using the binomial theorem, prove each. 28. 24n + 3n  1 is divisible by 9. (Hint: 2 = 3  1.) 29. 42n + 10n  1 is divisible by 25. (Hint: 4 = 5  1.) 30. 31.
n(2n) (2n )int:UseheoremO18
Y~ 2r r=0 r=l n (;) }~2r 3 n r=0
2r 1
32. r~0 (;)(n nr)(22) [Hint: Consider (1 + x) 2n = (1 + x)n(1 + x) n. Equate the coefficients of x n from either side.] 33. i~1 (in l ) ( ? )
 (n2+nl)
[Hint: Consider (1 + x) 2n = (x + 1)n(1 + x) n. Equate the coefficients of x n+l from both sides.]
Evaluate each sum. 34. 1 ( 1 ) + 2 ( 2
)+3(3
)+...+n(:)
(Hint: Let S denote the sum. Use S and the sum in the reverse order to compute 2S.)
398
Chapter6 CombinatoricsandDiscreteProbability 35. a ( ; ) + ( a + d ) ( n l ) + ( a + 2 d ) ( 2 (Hint:
)+...+(a+nd)(n)
Use the same hint as in Exercise 34.)
n+l 36. Show that C ( n , r  1) < C ( n , r ) if and only if r < ~ , 2 0 < r < n.
where
37. Using Exercise 36, prove that the largest binomial coefficient C ( n , r ) occurs when r = [n/2J. Using induction, prove each. 38" ( ; ) + ( (Hint:
n + l l ~/+(n+2)2 +  . . + ( n + r ) r
= (n+r+l)r
Use Pascal's identity.)
39. 1 ( 1 ) + 2 ( 2 ) + . . . + n ( n )
= n 2 n1
40. ( ; ) 2 + ( 1 ) 2 + ( 2 ) 2 + . . . +
( n ) 2 = (2n)
,~ (n)
From the binomial expansion (1 + x) '~  ~
,~ (n)
calculus that n(1 + x) "1  ~
x r,
it can be shown using
r0
rx '  1 .
Using this result, prove each.
r=l
41.
1(1 ) +2(:)+3(3 ) +...+n( n)n2
n1
42. 1 ( 1 ) + 3 ( 3 ) + 5 ( 5 ) + . . . . 2 ( 2 ) + 4 ( 4 ) + 6 ( 6 ) +
. . . . n2 n2
43. Prove the binomial theorem, using mathematical induction. *44. Using a combinatorial argument prove that
(n) (m)= (n)(n r) (Hint:
(Newton's identity)
Select an relement subset of an nelement set in two ways.)
45. Prove the result in Exercise 44 algebraically. The following result is known as Vandermonde's identity, after the German mathematician AbnitTheophile Vandermonde (17351796):
(m :nl  (0) (nl + (~) (r n l)~ (~) (r n2)~ ~ (mr)(0)
6.7 The Generalized InclusionExclusion Principle (GIEP) (optional)
399
*46. Prove V a n d e r m o n d e ' s identity, using a combinatorial a r g u m e n t . (Hint: Consider the ways of selecting r people from a group of m m e n and n women.) 47. Prove V a n d e r m o n d e ' s identity algebraically. [Hint: Consider (1 + x)m(x + 1) n = (1 + X) m+n. ]
n (:) 48. Find a formula for ~
.
i=2 49. Using induction, establish the formula guessed in Exercise 48. 50. Find a formula for ~
.
i=3 51. Using induction, establish the formula guessed in Exercise 50. 52. Using Exercises 4851, predict a formula for ~ i=k
.
The generalized version of the inclusionexclusion principle has interesting applications to n u m b e r theory, surjections, and d e r a n g e m e n t s , as will be seen shortly. ( G I E P ) Let A 1 , A 2 , . . . ,An be n finite sets. T h e n
U Ai
i=1
 E Inil E 1_ n. L e t a be a n y e l e m e n t inA. Since f(a) has n choices, a total o f n m f u n c t i o n s can be defined from A to B. It now follows t h a t
N  n m  ( n u m b e r of functions t h a t are not surjective) So it suffices to count the n u m b e r of functions from A to B t h a t a r e n o t surjective; this is w h e r e we shall invoke t h e G I E P . For convenience, let A = {1, 2, 3 , . . . , m} a n d B  {bl, b2, b 3 , . . . , bn }. L e t Si denote t h e set of all functions from A to B t h a t do not p r o d u c e bi a s a n output, w h e r e 1 < i _< n. T h e n S  $1 u $2 u ... u Sn denotes t h e set of all functions from A to B t h a t do not o u t p u t at least one e l e m e n t of B. In o t h e r words, S  $1 u $2 u ... u Sn d e n o t e s t h e set of n o n s u r j e c t i o n s f r o m A to B, so IS[  IS1 u $2 u ... u Snl denotes the n u m b e r of n o n s u r j e c t i o n s f r o m A to B. By T h e o r e m 6.19, n
ISI  E Isil  E lSi nSjl Jr E Isi n S j nSkl . . . . +IS1 N32 n.nSnl i=1 i2
1)(Dn1 § D n  2 ) , where Do  1 and D1  0
Dn (n
Dn  nDn1
§
(  1 ) n,
(page 362)
n > 1 (page 363)
where Do = 1 [ 1   : : 1.1§12!4 7 Z!
9 Dnn!
(  n, 1)n ] ,n>_O
(page 363).
Combinations
9 An rcombination of a set of n elements is a subset with size r, where 0 < r < n. The n u m b e r of rcombinations is denoted by C ( n , r ) or( n ) 9 C(n,
(page 366).
r) =
n~
(page 366)
r!(nr!)
9 Pascal's identity
C ( n , r) 
C ( n , r)  C ( n , n  r)
1, r  1) + C ( n  1, r)
C(n 
(page 369) (page 370).
9 P e r m u t a t i o n s w i t h R e p e t i t i o n s The n u m b e r of p e r m u t a t i o n s of n items where nl are alike, n2 are a l i k e , . . . , and n k are alike, is given by
nI
9
(page 376).
nl!n2!...nk!
9 C o m b i n a t i o n s w i t h R e p e t i t i o n s The n u m b e r of rcombinations with repetitions from a set with size n is C ( n + r  1, r) (page 380). 9 Let Xl, X 2 , . . . ,Xn be n nonnegative integer variables. The equation X l + x2 + . . . + Xn  r has C ( n + r  1, r) integer solutions (page 381). 9 The binomial
theorem
Let x and y be real variables and n any
nonnegative integer. T h e n
n ( n)
(X § y ) n _ E
xnryr
(page 389)
r0 n
9
~
C(n,r)
(page 391)
 2n
r=0
9 The n u m b e r of surjections N from a finite set A to a finite set B is given by
n N = E(1)
(:) r
( n  r) m
r=0 where IAI = m, IB]  n, and m > n
(page 403).
Chapter Summary
429
9 The n u m b e r of d e r a n g e m e n t s N of n items is given by n
Y~~(1)r(n)(nr)!
(page 406)
r=O
Discrete Probability 9 Let E be an event of a finite sample space S consisting of equally likely outcomes. T h e n p ( E )  IEI/ISI (page 410). 9 If the outcomes are not equally likely, p ( E ) denotes the probability of outcome ai ~ E
~ p ( a i ) , where p(ai) (page 414).
9 I n c l u s i o n  E x c l u s i o n P r i n c i p l e I f A and B are any two events, t h e n (page 414).
p ( A u B) = p ( A ) + p ( B )  p ( A N B)
9 A d d i t i o n P r i n c i p l e If A and B are m u t u a l l y exclusive events, t h e n p ( A u B)  p ( A ) + p ( B ) (page 415).
Conditional Probability 9 M u l t i p l i c a t i o n T h e o r e m p ( A n B)  p ( A ) 9p(BIA)
(page 418).
9 The e x p e c t e d value E of an experiment with numerical outcomes n
a l , . . . ,an is given by E  ~ aip(ai)
(page 420).
i=l
Bernoulli Trials 9 Repeated trials of an experiment are B e r n o u l l i t r i a l s if: They are independent; Each trial has exactly two outcomes; and P(outcome) remains the same in every trial
(page 422).
9 The b i n o m i a l p r o b a b i l i t y of exactly k successes in a sequence of n Bernoulli trials is given by b ( n , k , p )  C ( n , k ) p k q nk, where p  p(success) and q  p(failure) (page 423).
Review Exercises Find the n u m b e r of positive integers < 2076 and divisible by: 1. 3 or 4.
2. 3 or 4, but not 12.
3. 2, 3, or 5, but not 30.
4. 3, 4, or 5, but not 60.
5. In F O R T R A N 77, a variable n a m e consists of six a l p h a n u m e r i c characters, beginning with a letter. Find the total n u m b e r of legal variable names possible.
430
Chapter 6 Combinatorics and Discrete Probability
In C+ +, an identifier consists of a letter followed by a n y n u m b e r of a l p h a n u m e r i c characters. How m a n y such identifiers of length six: 6. End in a letter?
7. End in a digit?
8. Are palindromes?
9. Are not palindromes?
A license plate n u m b e r in Connecticut consists of two letters followed by four digits. Find each. 10. The total n u m b e r of license plates possible. 11. The n u m b e r of license plates t h a t end in GREEN. 12. The n u m b e r of license plates t h a t end in JAVA. Find the n u m b e r of t e r m s in the expansion of each.
13.(i=~oai)(i=~obi(i)3=~_lCi) (i=~_2ai) (i 3=~_l(ib=i)~oCi) 14.
M a s t e r M i n d is a fascinating logic game played by two players, the code
m a k e r and the code breaker. T h e r e are 72 pegs, available in six colors. The code m a k e r creates a secret code p a t t e r n using four pegs in a row. The code breaker tries to figure out the code. Find the n u m b e r of coding p a t t e r n s possible if: 15. T h e r e are no restrictions on the use of colors. 16. The same color cannot be repeated. 1718. Redo Exercises 15 and 16 if pegs are available in seven colors. Find the n u m b e r of ways three dahlias, four cannas, and five zinnias can be planted: 19. In a row such t h a t all plants of the same family are next to each other. 20. In a row such t h a t the zinnias are planted in between the other two families of plants. 21. In a circle. 22. In a circle such t h a t all plants of the same family are next to each other. Find the n u m b e r of t e r n a r y words t h a t can be formed of: 23. Length 10.
24. Length at most 10.
Find the n u m b e r of subcommittees t h a t can be formed from a 10member committee such t h a t each contains: 25. At least seven members.
26. At most five members.
A s t a n d a r d deck of playing cards contains 52 cards. n u m b e r of: 27. Poker hands (five cards).
Find the total
Chapter Summary
431
28. Flushes (all cards of the same suit). 29. Full houses (three of one kind and two of another). Find the n u m b e r of ways the letters of each word can be scrambled. 30. T E N N E S S E E
31. M I S S I S S I P P I
How many words of length six over the alphabet {a, b, c, d, e} contain: 32. Two a's, three b's, and one c.
33. Three a's and three b's.
(A m o d i f i e d h a n d s h a k e p r o b l e m ) Mr. and Mrs. Matrix hosted a party for n married couples. Each person shook hands with everyone else, excluding his or her spouse. No person shook hands with himself or herself. Let h ( n ) denote the n u m b e r of h a n d s h a k e s made. 34. Using induction, prove t h a t h ( n )  C ( 2 n  1, 2) + (n  1), n > 0. 35. Derive a recurrence relation satisfied by h ( n ) . 36. Show t h a t h ( n )  2 n ( n 
1), n > 0.
A local fast food r e s t a u r a n t serves four types of pizza m pepperoni, mushroom, cheese, and onion. Find the n u m b e r of different pizza orders possible by: 37. Six students.
38. Seven students.
3940. Redo Exercises 37 and 38, using generating functions. Find the n u m b e r of nonnegative integer solutions of each equation. 41. Xl +
42. Xl ~ X2 ~ X3 ~ X4 + X5
X2 + X3 + X4   8

12
Find the coefficient of each. 43. x4y 7 in the expansion of ( x  y)ll 44. x5y 5 in the expansion of (2x + 3y) 1~ Using the binomial theorem, expand each. 45. (x + y)6
46. ( x  2y) 4
47. ( 3 x  y)5
48. (2x + 3y) 5
49. Prove t h a t C(n, r) is an integer for every integer n > 0, where 0 _< r < n. n
50. Prove by induction t h a t ~ C(i, r)  C(n + 1, r + 1). i=r
( T w e l v e d a y s o f C h r i s t m a s ) Suppose you sent your love 1 gift on the first day of Christmas, 1 + 2 gifts on the second day, 1 + 2 + 3 gifts on the third day, and so on. Show t h a t the n u m b e r of gifts: 51. Sent on the n t h day is C ( n + 1, 2). 52. Received by your love in the first n days is C ( n + 2, 3).
Chapter 6 Combinatorics and Discrete Probability
432
Let Dn denote the n u m b e r of d e r a n g e m e n t s of n items. Prove each. 53. The n u m b e r of p e r m u t a t i o n s of n items with exactly k of t h e m in t h e i r n a t u r a l positions is C(n,k)Dn_k, where 0 < k < n.
54. C(n, O)Do + C(n, 1)D1 § ... + C(n,n)Dn  n!, n > 0 55. Find the sum of the n u m b e r s in rows 0 t h r o u g h n  1 of Pascal's triangle. Let A(n, r) denote the n u m b e r of additions needed to compute C(n, r) using recursion. 56. Find A(n, 0) and A(n, n). 57. Find an explicit formula for A(n, 1). *58. Find the recurrence relation satisfied by A(n, r). *59. Prove t h a t A(n, r)  C(n, r)  1. *60. Find the probability of obtaining no two consecutive heads w h e n a coin is tossed n times. 61. A contractor bids to construct a shopping plaza. It has been e s t i m a t e d t h a t there is a 5/8 probability t h a t she would m a k e a profit of $3 million, and a 3/8 probability t h a t she would lose $4 million. C o m p u t e her expected profit. 62. A construction company is bidding on two contracts to build apartm e n t complexes in Janesville and Jeansville. The probabilities t h a t the two bids will be accepted are 60% and 80%, respectively, and the bidding expenses are $100,000 and $150,000, respectively. It is estimated t h a t the company will make profits of $3 million from Janesville and $2.3 million from Jeansville. In which location should the company be more interested? It is found that 10% of the snow blowers m a n u f a c t u r e d by a certain company need repair before they can be sold. Five snow blowers are selected at random. Find the probability that: {}3. At least one needs repair.
Supplementary
64. Not all need repair.
Exercises Prove each, where Cn denotes the n t h Catalan number. 1. The product of any r consecutive positive integers is divisible by r!. 2. C(2n, n) is an integer for every n > 0.
3. (n + 1) l C(2n, n) for every n > 0. 4. p lC(p, r), where p is a prime and 0 < r < p.
433
Chapter Summary
C(2n + 1, n) .
6.
C n
Cn =
2n+1 2(2n  1) Cnl,n n+l
>_1
7. ( P r o b l e m e des M e n a g e s ) Let Mn denote the n u m b e r of ways n married couples can be seated around a round table with men and women in alternate chairs and no wife next to her husband. It can be shown that n
Mn  n! + E (  1 ) k=l
k
2n ( 2 n k k ) ( n _ k ) ' 2n  k
(E. Lucas)
Compute M5 and M6. 8. Prove that
(ni) (r : i) (n ; i) _ (n ; i) (n : i ) (r n i) Evaluate each.
k=O *10.
**11.
~n ( k ) k a k=O
(N. J. Kuenzi and B. Prielipp, 1985)
n1 d ndl(jwk)
~ ~ ~ d= 1 j=0 k0
j
(U.S.A Mathematical Olympiad, 1991)
"12. Guess the number of odd binomial coefficients in row n of Pascal's triangle. (Hint: Compare the number of odd binomial coefficients in row n and the binary expansion of n.)
a,b ~ W, a  (anan1 ""a0)two, ai >_bi for every i, we say a i m p l i e s
and b  (bnbn1 ""b0)two. If b and write a ~ b; otherwise a :~ b. Determine if 43 ~ 25 and 47 ~ 29.
13. Let
14. The binomial coefficient C(n,r) is odd if and only if n =~ r. Using this fact, determine the p a r i t y (oddness or evenness) of C(25, 18) and C (29, 19). (See Exercise 13. ) Let f(n,k) denote the number of kelement subsets of the set S = {1, 2 , . . . , n} that do not contain consecutive integers. (I. Kaplansky) 15. Define 16. Prove
f(n,k) recursively. that f(n, k)  C(n  k + 1, k).
434
Chapter6 Combinatoricsand Discrete Probability 17. Prove t h a t the total n u m b e r of subsets of S t h a t do not contain consecutive integers is Fn+2, where Fn denotes the n t h Fibonacci number. An ordered pair of subsets (A, B) of the set Sn  {1, 2 , . . . , n} is a d m i s s i b l e i f a > IBI for e v e r y a e A a n d b > IAI for e v e r y b e B. For example, ({ 2, 3 }, {4 }) is an admissible pair of subsets of $4. 18. Find the various admissible ordered pairs of subsets of the sets So, $1, and $2. "19. Predict the n u m b e r of admissible ordered pairs of subsets of Sn.
Computer Exercises Write a program to perform each task, where n ~ 1~ and n < 20. 1. Read in positive integers n and r, where r < n < 10, and compute P(n, r), using Theorem 6.4 and recursion. 2. Print the n u m b e r of d e r a n g e m e n t s Dn of n distinct items using the recursive definition (6.1), the alternate recursive definition (6.3), and the explicit formula in Theorem 6.9. 3. Print the values ofpn 
Dn 1 n! and e correct to 10 decimal places, for
comparison. 4. Read in positive integers n and r, where 0 < r < n , and compute C(n, r) using Theorem 6.10 and recursion. 5. P r i n t all solutions of the equation Xl + x2 + x3  5, where Xl,X2,X 3 E W. Also print all solutions i f x l , x 2 , x 3 E 1~. 6. Compute the nth Catalan number, using recursion. 7. P r i n t all bytes in increasing order. 8. Print in tabular form all binary words of length n and the corresponding subsets of the set {1, 2, 3 , . . . , n}. 9. Print Pascal's triangle, as indicated below: 9 As in Figure 6.21 9 Rightjustified 9 Leftjustified
E x p l o r a t o r y Writing P r o j e c t s Using library and I n t e r n e t resources, write a team report on each of the following in your own words. Provide a welldocumented bibliography.
Chapter Summary
435
1. Study the number of plates on automobiles in your state and neighboring states. Create a combinatorial problem that will generate such license plate numbers in each system. 2. Investigate the origin of binomial coefficients and the twolevel parentheses notation. 3. Study the numerous properties of binomial coefficients, including their parity. 4. Examine the history of Pascal's triangle. Why is Pascal's triangle named after Pascal? 5. Investigate the numerous properties of Pascal's triangle. Explain how figurate numbers, Fibonacci numbers, and Catalan numbers can be extracted from it. 6. Extend Pascal's triangle upward, the definition of the binomial coefficient C(n,r) to include negative and fractional values of n, and the binomial theorem to fractional exponents. 7. Discuss the number of ways n rooks of n different colors can be placed on an n x n chessboard such that no two rooks can attack each other. 8. Investigate the properties of Bell numbers and their applications to combinatorics. Include a biography of E. T. Bell. 9. Define Stirling numbers of the first and second kind. Include their applications to combinatorics and a biography of J. Stirling. 10. Discuss Eulerian numbers and their relationships to Stirling numbers. 11. Discuss Bernoulli numbers, named after Jakob Bernoulli (16541705), and their relationships with power series and the tangent function. 12. Write an essay on the inclusionexclusion principle. 13. Study the origin of discrete probability. 14. Describe the various casino games and compute the expected value of each. 15. Examine the HardyWeinberg probabilities in population genetics, named for the English mathematician Godfrey. H. Hardy (18771947) and the German physician Wilhelm Weinberg (18621937) who published them independently in 1980.
Enrichment Readings 1. G. Berman and K. D. Fryer, Introduction to Combinatorics, Academic Press, New York, 1972, pp. 35125. 2. K. P. Bogart, Introductory Combinatorics, Pitman, Boston, MA, 1983.
436
Chapter 6 Combinatorics and Discrete Probability
3. P. Z. Chinn, "Inductive Patterns, Finite Differences, and a Missing Region," Mathematics Teacher, Vol. 81 (Sept. 1988), pp. 446449. 4. D. I. A. Cohen, Basic Techniques of Combinatorial Theory, Wiley, New York, 1978, pp. 13178. 5. J. Dugle, "The Twelve Days of Christmas and Pascal's Triangle," Mathematics Teacher, Vol. 75 (Dec. 1982), pp. 755757. 6. P.O. Eckhardt, "Discretely Deranged Squares," Mathematics Teacher, Vol. 83 (April 1990), pp. 318320. 7. A.W.F. Edwards, Pascal's Arithmetical Triangle, The Johns Hopkins University Press, Baltimore, MD, 2002. 8. M. Eng and J. Casey, "Pascal's TriangleA Serendipitous Source for Programming Activities," Mathematics Teacher, Vol. 76 (Dec. 1983), pp. 686690. 9. B. W. Jackson and D. Thoro, Applied Combinatorics with Problem Solving, AddisonWesley, Reading, MA, 1990, pp. 42133. 10. B. H. Litwiller and D. R. Duncan, "Poker Probabilities," Mathematics Teacher, Vol. 70 (Dec. 1977), pp. 766771. 11. C. Oliver, "The Twelve Days of Christmas," Mathematics Teacher, Vol. 70 (Dec. 1977), pp. 752754. 12. J. Varnadore, "Pascal's Triangle and Fibonacci Numbers," Mathematics Teacher, Vol. 84 (April 1991), pp. 314316, 319.
Chapter 7
Relations The invention of the symbol  by Gauss affords a striking example of the advantage which may be derived from an appropriate notation, and marks an epoch in the development of the science of arithmetic. 
G. B. MATHEWS
F
unctions are a special case of relations, which are also used in everyday life. Relations have applications to many disciplines, including biology, computer science, psychology, and sociology. The EQUIVALENCE statement in FORTRAN, for example, is based on the relation has the s a m e location as (see Example 7.42). Graphs, digraphs, formal languages, finite state m a c h i n e s  all to be discussed in the next four c h a p t e r s   a r e closely related to the theory of relations. In this chapter we will examine the concept of a relation, its computer representations and properties, and different ways to construct new relations from known ones. We will deal with the following problems, as well as others: 9 Is it possible to arrange all nbit words around a circle in such a way that any two adjacent words differ by exactly one bit? 9 Can we determine the day corresponding to a given date m/d/y, where y > 1582, the year the Gregorian calendar was adopted? 9 Five sailors and a monkey are marooned on a desert island. During the day they gather coconuts for food. They decide to divide them up in the morning and retire for the night. While the others sleep, one sailor gets up and divides them into equal piles, with one left over that he throws out for the monkey. He hides his share, puts the remaining coconuts together, and goes back to sleep. Later a second sailor gets up and divides the pile into five equal shares with one coconut left over, which he discards for the monkey. He also hides his share, puts the remaining coconuts together, and goes back to sleep. Later the remaining sailors repeat the process. Find the smallest possible number of coconuts in the original pile. 437
Chapter 7 Relations
438
9 The computer science courses required for a c o m p u t e r science major at a college are given in Table 7.1. In which order can a s t u d e n t t a k e them? T a b l e 7.1
Number
CS CS CS CS CS CS CS CS
100 150 200 250 300 350 400 450
Course
Prerequisite
Computer Science I Computer Science II Computer Organization Data Structures Computer Architecture Programming Languages Software Engineering Operating Systems
None CS 100 CS 150 CS 150 CS 200 CS 250 CS 250 CS 250, CS 300
A special class of matrices called boolean matrices is used to s t u d y relations, so we begin with a brief discussion of such matrices.
(This section is closely related to Section 3.7 on matrices; you will probably find t h a t section useful to review before reading further.) A b o o l e a n m a t r i x is a matrix with bits as its entries. T h u s A  ( a i j ) m • [101] is a boolean matrix if a i j  0 or i for every i a n d j . For instance, 0 1 0 is a boolean matrix, whereas
[1
is
not.
Boolean Operations and and or
The boolean operations a n d (A) and o r (v), defined by Table 2.1, signal the combining of boolean matrices to construct new ones. Listed below are several properties of these bit operations. They can be verified easily, so try a few. Let a and b be a r b i t r a r y bits. Then: 9 aAa=a
9 a Ab
9 ava=a
9 a v b = b v a
9 aA(bAc)=(aAb)Ac
9 av(bvc)=(avb)vc
9 av(bAc)=(avb)A(avc)
9 aA(bvc)=(aAb)v(aAc)
=b
Aa
n
Using the two bitoperations, we now define two operations on boolean matrices.
7.1 BooleanMatrices
439
Join and Meet The j o i n of the boolean matrices A = ( a i j ) m x n and B  ( b i j ) m x n , denoted by A v B, is defined by A v B = (aij v b i j ) m x n . Each element of A v B is obtained by o r i n g the corresponding elements of A and B. The m e e t of A and B, denoted by A A B, is defined by A A B  (aij A bij)m • Every element of A A B is obtained by a n d i n g the corresponding elements of A and B. The following example illustrates these two definitions. ~
Let
o1 0,]
and
Foo0
B =
,]
1
Find A v B and A A B. SOLUTION:
[,v00v0 l v,] [,01] Or1 lvO Or1  1 1 1 AA _[ A0 0AO 0 '] AvB=
A1
1A0
0A1
0
0
II
Boolean Product The b o o l e a n p r o d u c t of the boolean matrices A  ( a i j ) m x p and B ( b j k ) p x n , denoted by A  B, is the m a t r i x C  ( c i j ) m • where cij ( a l l A b l j ) v (ai2 A b2j) v . . . v (aip A bpj). (See Figure 7.1).
F i g u r e 7.1 all
I
a12
...
alp
ai2
...
a m. 2. . .. ...
aip amp
@
Cll
Clj
...
Cln
...
cij
...
tin
b; 1 . . . . . . bpj . . . ... . . . b .p n.J . . .LCml . . . ...
Cmj
...
Cmn
bll
...
blj
...
. . . . . . . . . . . . . bil
bin 1
i~~
...
bij
...
b n
.
.
.
......... =
Oil
Notice the similarity between this definition and t h a t of the usual product of matrices. The next example clarifies this definition. Let
A
0
1
Find A @ B and B  A, if defined.
and
B=[!i]
440
Chapter 7
Relations
SOLUTION:
(1) Since the n u m b e r of columns in A equals the n u m b e r of rows in B, A  B is defined:
101J [1!]
A(I)B
0
1
0 (D
1
0 _
r(1 A 1) v
(o A 1) v (1 A 0)
 L(0A1) V ( 1 A 1 ) V ( 0 A 0 )
(1 A O) v (0 A 1) v (1 A 0)] (0 A O) V (1 A 1) v (0 A O)
J
Ol] (2) N u m b e r of columns i n B  2  N u m b e r of rows inA. Therefore, B ( D A is also defined:
B (i) A =
=
(1 A 1) V (0 A 0) (1A1) V(1A0) (0 A 1) V (0 A 0)
=
1 1 0
0 1 0
(1 A 0) v (0 A 1) (1 A 0) V (1 A 1) (0 A 0) V (0 A 1)
(1 A 1) v (0 A 0)~ (1 A 1) v (1 A 0) (0 A 1) V (0 A 0)
J
1] 1 0 m
The f u n d a m e n t a l properties of the boolean m a t r i x operations are listed in the following theorem. Their proofs being fairly straightforward, a p p e a r as routine exercises (see Exercises 3643). Let A, B, and C be three boolean matrices. Then: 9 A vA
=A
9 AAA=A
9 AvB=BvA
9 AAB
9 Av(BvC)=(AvB)vC
9 AA(BAC):(AAB)AC
9 Av(BAC)=(AvB)A(AvC)
9 AA(BvC)=(AAB)v(AAC)
=B
AA
9 A(i)(B(DC)=(A(DB)(DC
The sizes of the matrices are a s s u m e d compatible for the c o r r e s p o n d i n g matrix operations, m
7.1 BooleanMatrices
441
Boolean Power of a Boolean Matrix
Let A be an m x m boolean matrix and n any positive integer. The n t h b o o l e a n p o w e r of A, denoted by A In], is defined recursively as follows"
A [~  Im A [nl  A [nl] Q A
(the identity matrix) ifn > 1
The following example illustrates this definition. Let A
[ li] 1 0
Compute A [21 and A 131. SOLUTION:
1!] [11!] Ill i] Ill i] Ill i]
A 121  A [11 (3A  A (3A  I11 1 0 0
A TM  A 121 @A 
1 0
1 0
(3
il @ [!
1 0
1 0
1 0


1 0
1 0
1 0
1 0
(You can verify that in this case, A Inl  A for every n >_ 1.)
m
You will find boolean matrices and their properties useful in the next few sections, so review them as needed. Exercises 7.1
Using the boolean matrices
~ [~ ~]~ [01
~]'andCI0
~]
find each. 1. A v B
2. A A B
3. A @ C
4. C @ A
5. A v ( B v C )
6. A / x ( B / x C )
7. A @ ( B @ C )
8. (A@B) @ C
Using the boolean matrices A
find each.
01] [i01] I 1 0 1
0 0 ,B= 1 0
1 0
0 0
,andC
[i0!] 0 1
442
Chapter 7 Relations
9. AA (BvC)
I0. Av(BAC)
11. (A A B ) v (A A C)
12. (AvB) A(AvC)
13. (A A B) v (A v C)
14. A (3 (B (3 C)
15. (A (i) B ) (!) C
16. B @C (!)A
17. A @ A @ A
18. Using the boolean matrix
A
[i0 1] 1 0
0 1
find A [31 and A I51. Let A and B be any two n x n boolean matrices. Find the n u m b e r of boolean operations needed to compute each. 19. A v B
20. A A B
21. A @ B
22. Find the n u m b e r of m x n boolean matrices t h a t can be defined. 23. Let A be an m x p boolean matrix and B a p x n boolean matrix. Find the n u m b e r of boolean operations needed to compute A (3 B. 24. For the boolean m a t r i x A in Example 7.3, prove t h a t A Inl = A for every n>l. The c o m p l e m e n t of a boolean matrix A, denoted by A', is obtained by taking the one's complement of each element in A, t h a t is, by replacing O's with l ' s and l ' s with 0's. Use the boolean matrices A, B, and C in Exercises 18 to compute each. 25. A'
26. B'
27. (A v B)'
28. A ' A B'
29. (A A B)'
30. A' v B'
31. A A (B' v C')
32. (A (3 B) (i) C'
Let A and O be two m x n boolean matrices such t h a t every entry of A is 1 and every entry of O is 0. Let B be any m x n boolean matrix. What can you say about each? 33. A v B
34. A A B
35. A'
Let A, B, and C be any n x n boolean matrices. Prove each. 36. A v A = A
37. A A A = A
38. A v B = B v A
39. A A B = B A A
40. A v (B v C) = (A v B) v C
41. A A (B A C) = (A A B) A C
42. A v ( B A C ) = ( A v B ) A ( A v C )
43. A A ( B v C ) = ( A A B ) v ( A A C )
Write an algorithm to find each. 44. The join of two boolean matrices A and B. 45. The meet of two boolean matrices A and B.
7.2 Relations and Digraphs
443
46. The complement of a boolean matrix A. 47. The boolean product of two boolean matrices A and B. 48. The n t h boolean power of an m x m boolean matrix A.
Clearly m a n y relationships exist in the world around us. On the h u m a n level, they are parentchild, husbandwife, s t u d e n t  t e a c h e r , doctorpatient, and so on. Relationships exist between n u m b e r s also; the equality relation (  ) and the lessthan relation (3
1
2
4
2
3
5
2
1
6
1
4
7
3
2
T
H
N
1
2
2
4
2
4
3
6
3
1
2
5
4
2
3
7
5
2
1
1
6
1
4
0
7
3
2
2
START
F i g u r e 7.11 Static linked list r e p r e s e n t a t i o n of the relation.
START
Most m o d e r n p r o g r a m m i n g languages s u p p o r t dynamic data structures. In this type of language, a linked list consists of a set of n o d e s and each node contains (at least) two fields: a d a t a f i e l d and a l i n k f i e l d (or p o i n t e r field) (see Figure 7.12). The data field contains a data item, whereas the link field contains the address of the next node in the list. For instance, consider the linked list in Figure 7.13. H E A D E R contains the address of the first node in the list; it corresponds to START in the previous discussion. The link field of the last node contains a special pointer called the n i l p o i n t e r t h a t signals the end of the list. This pointer corresponds to 0 in the static representation; a slash (/) in the field signifies it.
452
Chapter 7 Relations
F i g u r e 7.12
Data Field
A typical node.
Figure 7.13
Link Field
Link
HEADER I
all
,I
_1o .i , "
I
"
Data The relation in Figure 7.13 illustrates the dynamic linked list representation. First, for each vertex, create a linked list of vertices adjacent to it. Then store the header nodes in an array. The resulting linked representation appears in Figure 7.14.
Figure 7.14
....
4 . We can abbreviate this representation by storing the header nodes in an array of pointers, as in Figure 7.15. This simplified version is the a d j a c e n c y list r e p r e s e n t a t i o n of the digraph and hence of the relation. The next example shows how to find the adjacency matrix of a relation from its adjacency list representation. Using the adjacency list representation of the relation in Figure 7.15, find its adjacency matrix. SOLUTION: The figure indicates vertex 1 is related to 2 and 4; vertex 2 is related to 1, 2, and 3; vertex 3 is related to 2; and vertex 4 is related to 3. Thus, the
7.3 Computer Representations of Relations (optional)
453
adjacency matrix of the relation is
1 M R 
F i g u r e 7.15
1
2
3
4
0
1
0
1
3 4
11 i
1 0
0 1
I
121 1 ~ 'Ld
12H
Array of pointers
II
13H
I Exercise 7.3
Find the static linked list representation of each relation. 1.
1
2.
2 5
2 3
4 34. Find the adjacency list representation of the relations in Exercises 1 and 2. Find the adjacency matrix of the relation with each adjacency list representation. 0
,
~i2H
2
~121 ~i3H
3
~111 I~121 I~I3H
454
Chapter 7 Relations u
1211 14H
o
Y
I3H il]%1 ] 78. Draw the digraphs of the relations represented by the adjacency lists in Exercises 5 and 6. Find the adjacency list representation of the relation with the given adjacency matrix.
.
1
2
3
1 0 2 :3
1 0 1
0
10.
1
1 2 1 0
3
0
4
0
3 4 1 1
11~ 1 1 0
1
Write an algorithm to find the adjacency list representation of a relation R on the set {1, 2 , . . . , n} using: 11. The relation, given in terms of ordered pairs. 12. Its adjacency matrix A. 13. Write an algorithm to find the adjacency matrix A of a relation on the set {1, 2 , . . . , n} from its adjacency list representation.
Since relations on finite sets can be represented by matrices, their properties can be identified from their adjacency matrices. In this section we will study the properties of reflexivity, symmetry, antisymmetry, and transitivity. To begin with, consider the relation R, is logically equivalent to, on the set of propositions. Since every proposition is logically equivalent to itself, it has the property that xRx for every proposition x. Such a relation is reflexive.
7.4 Properties of Relations
455
Reflexive R e l a t i o n
A relation R on a set A is r e f l e x i v e if x R x for every element x in A, t h a t is, if x R x for every x e A. Since every set A is a subset of itself, the relation is a subset o f on its power set is reflexive. Similarly, the e q u a l i t y r e l a t i o n (  ) is also reflexive; it is denoted by A. Thus, a relation is reflexive if and only if A _c R. The next two examples illustrate additional reflexive relations. Since x < x for every real n u m b e r x, the relation < on I~ is reflexive. No n u m b e r is less t h a n itself, so the less t h a n relation is not reflexive, m Which of the following relations on A 
{x,y,z} are reflexive?
9 R1  {(x,x), (x,y), (y,y), (z,z)}
9 R2
9 R3  (~ [the e m p t y r e l a t i o n ]
9 R4 = {(x,x), (y,y), (z,z)}

{(x,x), (y,y), (y,z), (z,y)}
SOLUTION: For a relation R on A to be reflexive, every element in A m u s t be related to itself, t h a t is, (a, a) c R for every a c A. The element a has three choices, namely, x, y, and z; therefore, the ordered pairs (x,x), (y,y), and (z,z) m u s t be in the relation for it to be reflexive. Consequently, the relations R1 and R4 are reflexive, whereas R2 and R3 are not. m
How can we characterize the adjacency m a t r i x M = (mij) of a reflexive relation on the set A = {al, a 2 , . . , an }? A relation R on A is reflexive if and only if aiRai for every ai in A. Thus, R is reflexive if and only if mii = 1 for every ij t h a t is, if and only if the m a i n diagonal elements of MR are all l's, as Figure 7.16 shows.
Figure
7.16
1 MR 1
J,,
The digraph of a reflexive relation m u s t contain a loop at each vertex, since every element of A is related to itself; see Figure 7.16.
Chapter 7 Relations
456
N u m b e r of Reflexive R e l a t i o n s
We can use the adjacency m a t r i x MR of a relation R on a set A to c o m p u t e the n u m b e r of reflexive relations t h a t can be defined on A, as the following example demonstrates. ~
~
~
~
l
Find the n u m b e r of reflexive relations R t h a t can be defined on a set w i t h n elements. SOLUTION: Since R is reflexive, every element on the main diagonal of MR is 1; t h e r e are n such elements. Since MR contains n 2 elements, there are n 2  n  n(n  1) elements t h a t do not lie on the main diagonal; each can be a 0 or 1; so each such element mij has two choices. Thus, by the multiplication principle, we can form 2 n(n1) such adjacency matrices, t h a t is, 2 n(n1) reflexive relations onA. ll For an exploration of symmetric and a n t i s y m m e t r i c relations, again let R be the relation, is logically equivalent to, on the set of propositions. I f x and y are any two propositions such t h a t xRy, t h e n yRx. Thus xRy implies yRx. On the other hand, let x and y be any two real n u m b e r s such t h a t x < y and y < x. T h e n x  y. Thus the relation R(__ 1. So (X2
S~  U Sn C S
n=l
m
Thus S ~ cS By assumption, R c S; so R ~ c S ~ , since every p a t h in R is also a p a t h in S. Therefore, R ~ _ S. Consequently, S c R ~ and R ~ __ S. Therefore, S = R ~ . In o t h e r words, t h e r e are no transitive relations in between R and R ~. So R ~ is the smallest transitive relation containing R. m It follows by T h e o r e m s 7.7 and 7.8 t h a t R*  R U R 2 u ... u R" and hence M R ~  M R v MR.2 v . . . v MR,,
To illustrate this, using Example 7.36, the transitive closure of the relation R = {(a,b), (b,a), (b,c), ( c , d ) , (d,a)} on { a , b , c , d } is R* = R ~ = {(a,a), (a, b), (a, c), (a, d), (b, a), (b, b), (b, c), (b, d), (c, a), (c, b), (c, c), (c, d), (d, a), (d, b), (d,c),(d,d)}.
Since R ~ = R*, the connectivity relation algorithm can be used to compute M R , , but it is not efficient, especially when M R . is fairly large. A b e t t e r m e t h o d to find R* is W a r s h a l l ' s a l g o r i t h m , n a m e d in h o n o r of S t e p h e n Warshall, who invented it in 1962.
Warshall's Algorithm Let a  x l  x 2 . . . . . xmb be a p a t h in a relation R on a set A  { a l , a 2 , . . . ,an}. The verticesxl, x2,... ,Xm are the i n t e r i o r p o i n t s of the path. For instance, vertices c and d are the interior points on the p a t h acdb of the d i g r a p h in Figure 7.5. The essence of Warshall's a l g o r i t h m lies in c o n s t r u c t i n g a sequence of n boolean matrices W 1 , . . . , Wn, b e g i n n i n g with Wo  M R . Let Wk  (w/j), where 1 < k < n. Define wij  1 if a p a t h r u n s from ai to aj in R whose interior vertices, if any, belong to the set {al, a 2 , . . . , ak}. Since the i j t h
478
Chapter 7 Relations
element of Wn equals 1 if and only if a path exists from ai to aN whose interior points belong to the set {a l, a 2 , . . . , an }, Wn = WR*. In fact, the m a t r i x Wk = (WIN) can be constructed from its predecessor W k  1 = (vij) as follows. When can wij = 1? For WiN = 1, t h e r e m u s t be a path from ai to aj whose interior vertices belong to the set {a l, a 2 , . . . , ak }.
Case 1
If ak is not an interior vertex, all interior vertices m u s t belong to the set {hi, a 2 , . . . , ak1 }, so vij : 1.
C a s e 2 Suppose ak is an interior vertex (see Figure 7.35). If a cycle exists at ak, eliminate it to yield a shorter path. (This g u a r a n t e e s t h a t the vertex ak occurs exactly once in the path.) Therefore, all interior vertices of the paths ai . . . . . ak and ak . . . . . aj belong to the set {a l, a 2 , . . , ak1}. In other words, Vik = 1 and Vkj = 1.
Figure 7.35
ak
/ /
a/
ai
Consequently, WiN = 1 only if v,ii = 1, or vik = 1 and vkj = 1. This is the crux of Warshall's algorithm. Thus the i j t h element of Wk is 1 if: 9 The corresponding element of Wt,._1 is 1 or 9 Both the i k t h element and the k j t h element of Wk1 are 1; t h a t is, the ith element in column k of Wk1 and t h e j t h element in row k of Wk1 are 1. Use this property to construct W1 from W0 = M R , W2 from W1,..., and Wn from Wn1. Since Wn = MR*, the actual elements of R* can be read from Wn. The next two examples clarify this algorithm. Using Warshall's algorithm, find the transitive closure of the relation R = {(a, b), (b, a), (b,c)} o n A = {a,b,c}. SOLUTION: S t e p 1 Find W0.
Wo   M R 
[il!] 0
0
Step2
Find W1.
If the i j t h element of W0 is 1, the i j t h element of W1 is also 1. In other words, every 1 in W0 stays in W1. To find the r e m a i n i n g l ' s in W1, locate
7.7 Transitive Closure (optional)
479
the l ' s in column 1 (  k); there is just one 1; it occurs in position i  2. Now locate the l's in row 1 (  k). Again, there is just one 1, namely, in position j  2. Therefore, the ijth entry in W1 should be 1, where i  2 a n d j  2. Thus
W1
EoI!I 1 0
1 0
S t e p 3 Find W2. Again, all the l's in W1 stay in W2. To find the other l's, if any, locate the l's in column 2( k) and row 2 (  k). They occur in positions 1 and 2 of column 2 and in positions 1, 2, and 3 of row 2, so the ijth entry of W2 must be 1, where i = 1, 2 and j = 1, 2, 3. So change the O's in such locations of W1 to l's. Thus
W2
Ei x 1] 1 0
1 0
S t e p 4 Find W3. All the l's in W2 remain in W3. To find the remaining l's, if any, locate the l's in column 3   namely, positions 1 and 2 m and the l's in row 3. Because no l's appear in row 3, we get no new l's, so W3  W2. Since A contains three elements, WR. = W3. Thus,
WR,
I
1 1 0
1 1 0
11
1 0
which agrees with the transitive closure obtained in Example 7.37.
II
Using Warshall's algorithm, find the transitive closure of the relation R = {(a, a), (a, b), (a, d), (b, a), (c, b), (c, c), (d, b), (d, c), (d, d)} on {a,b,c,d}. SOLUTION: S t e p 1 Find W0.
Wo = MR 
1 1 0 0
1 0 1 1
0 0 1 1
1 0 0 1
480
Chapter 7 Relations
Step 2 Find W1. Locate the l ' s in column 1 and row 1; positions 1 and 2 in column 1; and positions 1, 2, and 4 in row 1. Therefore, W1 should contain a 1 in locations (1,1), (1,2), (1,4), (2,1), (2,2), and (2,4): 1 1 W1 ~'0 0
1 1 1 1
0 0 1 1
1 1 0 1
(All the l's in W0 remain in W1.)
Step 3 Find W2. Locate the l's in column 2 and in row 2; positions 1, 2, 3, and 4 in column 2, and positions 1, 2, and 4 in row 2. So W2 should contain a 1 in locations (1,1), (1,2), (1,4), (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), and (4,4). Again, since all the l's in W1 stay in W2,
W2
1 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
Step 4 Find W3. The l's of column 3 occur in positions 3 and 4; those of row 3 in positions 1, 2, 3, and 4. Consequently, W3 should contain a 1 in locations (i,j) where i = 3,4 a n d j  1,2, 3,4:
W3
1 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
Step 5 Find W4. The l's of column 4 appear in positions 1, 2, 3, and 4; the l ' s of row 4 in positions 1, 2, 3, and 4. So W4 should contain a 1 in locations (i,j) where i = 1, 2, 3, 4 a n d j = 1, 2, 3, 4: 1
1
1
1
i
1 1 1
1 1 1
1 1 1
Since MR.  W4, this is the adjacency matrix of the transitive closure. (Finding the connectivity relation of R will verify this.) m
481
77 TransitiveClosure (optional)
Warshall's algorithm is presented in Algorithm 7.2. It is based on the discussion preceding Example 7.38. Algorithm Warshal] (MR,W) (* This algorithm employs the adjacency matrix of a relation R on f i n i t e set with n elements to find the adjacency matrix MR* of i t s t r a n s i t i v e closure. *) O. Begin (* algorithm *) (* I n i t i a l i z e W = (wij) *) I. W +MR 2. for k = i to n do (* compute Wk *) 3. for i = i to n do 4. for j = I to n do
5. Wij + Wij V (Wik A Wkj ) (*compute the i j  t h 6. MR +W 7. End (* algorithm *)
element *)
Algorithm 7.2
A Comparison of Warshall's Algorithm with the Connectivity Algorithm Why is this algorithm far more efficient than the connectivity relation algorithm? Notice that the number of boolean operations in line 5 is 2, so the total number of boolean operations in lines 2 through 5 (and hence in the algorithm) is 2. n. n. n  2n 3  ()(n3), whereas the connectivity algorithm takes ()(n4) bit operations.
Exercises 7.7 Find the transitive closure of each relation on A = {a, b, c}. 1. {(a,b), (b,a)}
2. {(a,b), (b,c), (c,a)}
3. {(b,a), (b,c), (c,b)}
4. {(a,a), (a,c), (b,c), (c,a)}
Find the transitive closure of each relation on A = {a, b, c, d}.
5. {(a,a), (a,b)}
6. {(a,b), (b,c), (c,a)}
In Exercises 79, find the adjacency matrix of the transitive closure of each relation R on {a, b, c} with the given adjacency matrix.
7.
0 0
1 0
8.
[i0 ] 1 1
0 1
9.
1012. Using the connectivity relation algorithm, closure R* of each relation in Exercises 79.
[!IZ] 1 0
find the transitive
482
Chapter 7 Relations 1315. For the relation R on {a,b,c} with each adjacency matrix in Exercises 79, compute the boolean matrix W1 in Warshall's algorithm. In Exercises 1618, the adjacency matrix of a relation R on {a, b, c, d} is given. In each case, compute the boolean matrices W1 and W2 in Warshall's algorithm. 0
1
0
0 1
1 0
0
Z0 i
1
0
0
0 0
1 1
0 0
0
i
0
18.
1
0
1
0 0
0 0
1 1
i010
1924. Using Warshall's algorithm, find the transitive closure of each relation in Exercises 79 and 1618. 2533. The r e f l e x i v e c l o s u r e of a relation on a set is the smallest reflexive relation t h a t contains it. Find the reflexive closures of the relations in Exercises 19. Find the reflexive closure of each relation on IR. 34. The lessthan relation.
35. The g r e a t e r  t h a n relation.
3644. The s y m m e t r i c c l o s u r e of a relation on a set is the smallest symmetric relation t h a t contains it. Find the symmetric closures of the relations in Exercises 19. Let R be any relation on a set A. Prove each. 45. R is reflexive if and only if A ___R. 46. R U A is reflexive. *47. R u A is the smallest reflexive relation containing R . (Hint: Assume there is a reflexive relation S such t h a t R c S c R u A. Prove that S  R or S  R u A.) *48. R U R 1 is symmetric. [Hint: Consider (R u R  1) 1.]
*49. R u R 1 is the smallest symmetric relation t h a t contains R. (Hint: Suppose there is a symmetric relation S such t h a t R __ S c_
RuR1.)
Section 7.4 introduced relations t h a t are reflexive, symmetric, and transitive. Naturally we can now ask: Are there relations t h a t simultaneously manifest all three properties? The answer is yes; for instance, the relation is logically equivalent to on the set of
7.8 Equivalence Relations
483
propositions has all t h e s e properties. Such a r e l a t i o n is an equivalence relation.
Equivalence Relation A relation on a set is an e q u i v a l e n c e r e l a t i o n if it is reflexive, s y m m e t r i c , a n d transitive. E x a m p l e s 7.407.42 explore equivalence relations. ~
The relation has the s a m e color h a i r as on the set of people is reflexive, s y m m e t r i c , and transitive. So it is an equivalence relation, m Let Z denote an alphabet. Define a relation R on E* by x R y if Ilxli  liYll, w h e r e Ilwll denotes t h e length of t h e word w. Is R an equivalence relation? SOLUTION: 9 Since every word has the s a m e length as itself, R is reflexive. 9 Suppose t h a t x R y . T h e n IIxll = Ilyll, so Ilyll = IIxll. Consequently, y R x . T h u s R is s y m m e t r i c . 9 I f x R y 4 and y R z , t h e n Ilxli = IlYl] and Ilyli = ilzil. Therefore, and hence x R y . In o t h e r words, R is transitive.
Thus, R is an equivalence relation,
lixll = Ilzli m
(optional) Is the relation has the s a m e m e m o r y location as on the set of variables in a p r o g r a m an equivalence relation? SOLUTION: 9 Since every variable has the s a m e location as itself, the relation is reflexive. 9 If a variable x has the s a m e location as a variable y, t h e n y has the s a m e location as x, so t h e relation is s y m m e t r i c . 9 Suppose x has t h e s a m e location as y and y has t h e s a m e location as z. T h e n x has the s a m e location as z, so the relation is transitive. T h u s the relation is an equivalence relation,
m
F O R T R A N provides an e q u i v a l e n c e s t a t e m e n t , so called since t h e relation has the s a m e location as is an equivalence relation. We can see this in the following F O R T R A N s t a t e m e n t :
EQUIVALENCE (A,B),(C,D,E),(F,G,H) It m e a n s the variables A and B s h a r e the s a m e m e m o r y location; the variables C, D, and E s h a r e the s a m e m e m o r y location; a n d so do the variables F, G, and H. The congruence relation, an i m p o r t a n t relation in m a t h e m a t i c s , is a classic example of an equivalence relation. It is closely r e l a t e d to the equality relation a n d p a r t i t i o n s of a finite set, as will be seen shortly.
484
Chapter 7 Relations
K a r l F r i e d r i c h G a u s s (17771855), son of a laborer, was born in Brunswick, Germany. A child prodigy, he detected an error in his father's bookkeeping when he was 3. The Duke of Brunswick, recognizing his remarkable talents, sponsored his education. Gauss received his doctorate in 1799 from the University of Helmstedt. In his doctoral dissertation, he gave the first rigorous proof of the fundamental theorem of algebra, which states, "Every polynomial of degree n (> 1) with real coefficients has at least one zero." Newton and Euler, among other brilliant minds, had attempted to prove it, but failed. He made significant contributions to algebra, number theory, geometry, analysis, physics, and astronomy. His impressive work Disquisitiones Arithmeticae of 1801 laid the foundation for modern number theory. From 1807 until his death, he was the director of the observatory and professor of mathematics at the University of GSttingen. Called the "prince of mathematics" by his contemporary mathematicians, Gauss made the famous statement, "Mathematics is the queen of the sciences and the theory of numbers the queen of mathematics. "
The congruence symbol _ was invented a r o u n d 1800 by Karl Friedrich Gauss, the g r e a t e s t m a t h e m a t i c i a n of the 19th century.
Congruence Relation L e t a , b , m ~ Z, w h e r e m > 2. T h e n a is c o n g r u e n t to b m o d u l o m , denoted by a = b(mod m), if a  b is divisible by m. The i n t e g e r m is the m o d u l u s of the c o n g r u e n c e r e l a t i o n . (This definition provides the basis of the r o o d o p e r a t o r we studied in C h a p t e r 3.) If a is not c o n g r u e n t to b modulo m, we write a ~ b (mod m). For example, since 5 1 ( 1 3  3), 13 = 3 (mod 5). Also,  5 ~ 3 (mod 4) since 41(5  3). But 17 ~ 4 (mod 6), since 6 ~ (17  4). The congruence relation has several useful properties, some of which are given below. Let a, b, c, d , m ~ Z w i t h m
> 2. Then:
(1) a = a (mod m). ( r e f l e x i v e p r o p e r t y ) (2) I f a  b (mod m), t h e n b = a (mod m). ( s y m m e t r i c p r o p e r t y ) (3) I f a = b (mod m) and b _= c (mod m), t h e n a _ c (mod m). ( t r a n s i t i v e
property) (4) Let r be the r e m a i n d e r w h e n a is divided by m. T h e n a ~ r ( m o d m).
PROOF: We shall prove p a r t 3 and leave the o t h e r p a r t s as exercises. (3) Suppose a _= b (mod m) and b =_ c (mod m). T h e n m i ( a  b) and m l ( b  c). Consequently, a  b  m q l and b  c  mq2 for some
7.8 EquivalenceRelations
485
integers q l and q2. T h e n a
c
( a  b) + ( b  c)
= mql = m(ql
+ mq2 +
q2)
Therefore, m l ( a  c) and a  c (mod m).
m
It follows by the t h e o r e m t h a t the congruence relation is an equivalence relation.
The Congruence Relation and the Mod Operator Suppose a  r (mod b), w h e r e 0 < r < b. T h e n it can be shown t h a t r  a mod b. Conversely, if r  a mod b, t h e n a  r (mod b). T h u s a  r (mod b) if and only if r  a mod b, where 0 < r < b. See exercises 49 and 50. For example, 43  3 (mod 5) and 0 _< 3 < 5; clearly, 3  43 mod 5. Let us digress briefly to look at an i n t e r e s t i n g application of congruences*.
Fridaythe13th Congruences can be employed to find the n u m b e r of F r i d a y  t h e  1 3 t h s in a given year. W h e t h e r or not F r i d a y  t h e  1 3 t h occurs in a given m o n t h depends on two factors: the day on which the 13th fell in the previous m o n t h and the n u m b e r of days in the previous month. Suppose t h a t this is a n o n  l e a p y e a r and t h a t we would like to find the n u m b e r of F r i d a y  t h e  1 3 t h s in this year. Suppose also t h a t we know the day the 13th occurred in December of last year. Let Mi denote each of the m o n t h s D e c e m b e r t h r o u g h N o v e m b e r in t h a t order and Di the n u m b e r of days in m o n t h Mi. T h e various values of Di a r e 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, and 30, respectively. We label the days S u n d a y t h r o u g h S a t u r d a y by 0 t h r o u g h 6 respectively; so day 5 is a Friday. Let Di  di (mod 7), w h e r e 0 _< di < 7. T h e c o r r e s p o n d i n g values of di are 3, 3, 0, 3, 2, 3, 2, 3, 3, 2, 3, and 2, respectively. Each value of di indicates the n u m b e r of days the day of the 13th in m o n t h Mi m u s t be advanced to find the day the 13th falls in m o n t h Mi+l. For example, D e c e m b e r 13, 2000, was a Wednesday. So J a n u a r y 13, 2001, fell on day (3 + 3)  day 6, which was a S a t u r d a y . *T. Koshy, Elementary Number Theory with Applications, Harcourt/Academic Press, Boston, MA, 2002.
Chapter 7 Relations
486
i Let ti = ~ dj (mod 7), where 1 < i < 12. T h e n ti r e p r e s e n t s the j=l total n u m b e r of days the day of December 13 m u s t be moved f o r w a r d to d e t e r m i n e the day of the t h i r t e e n t h in m o n t h Mi. F o r example, t3  d l + d2 + d3 = 3 + 3 + 0  6 (mod 7). So, t h e day of December 13, 2000 (Wednesday) m u s t be advanced by six days to d e t e r m i n e the day of March 13, 2001; it is given by day (3 + 6) = day 2  T u e s d a y . Notice t h a t the various values of ti modulo 7 are 3, 6, 6, 2, 4, 0, 2, 5, 1, 3, 6, and 1, respectively; they include all the least residues modulo 7. Given the day of December 13, they can be used to d e t e r m i n e the day of the t h i r t e e n t h of each m o n t h Mi in a n o n  l e a p year. Table 7.5 s u m m a r i z e s the day of the 13th of each m o n t h in a nonleap year, corresponding to every choice of the day of December 13 of the previous year. You m a y verify this. Notice from the table t h a t t h e r e can be at most three F r i d a y  t h e  1 3 t h s in a n o n  l e a p year. T a b l e 7.5
~11 Jan. Feb. March April May June July Aug. Sept. Oct. Nov. Dec. Dec. 13 il 3 6 6 2 4 0 2 5 1 3 6 1
Day of the 13th in Each M o n t h in a N o n  l e a p Year.
Sun Mon Tue Wed Thu Sat
II 3 II 4 II 5 II 6 It0
6 0 1 2 a
6 0 1 2 a
2 3 4 5 6
II
4
4
0
II 2
5
5
1
4 5 6 0 1
0 1 2 3 4
3
6
2 3 4 5 6
5 6 0 1 2
1 2 3 4 5
3 4 5 6 0
6 0 1 2 3
1
4
4
0
2
5
0
1
1 2 3 4 5 0
For a leap year, the various values of di are 3, 3, 1, 3, 2, 3, 2, 3, 3, 2, 3, and 2; and the c o r r e s p o n d i n g values of ti are 3, 6, 0, 3, 5, 1, 3, 6, 2, 4, 0, and 2. Using these, we can construct a similar table for a leap year. R e t u r n i n g to the congruence relation, we now explore a close relationship between equivalence relations and partitions; but first we m a k e the following definition.
Equivalence Class
Let R be an equivalence relation on a set A and let a ~ A. The e q u i v a l e n c e c l a s s of a, denoted by [a], is defined as [a] = {x ~ A I xRa}. It consists of all elements in A t h a t are linked to a by the relation R. If x ~ [a], t h e n x is a r e p r e s e n t a t i v e of the class [a]. The next two examples explore equivalence relations. I
!
The relation R  {(a, a), (a, b), (b, a), (b, b), (c, c)} on A  {a, b, c} is an equivalence relation. Find the equivalence class of each element in A.
7.8 Equivalence Relations
SOLUTION: (1) [ a ]  {x e AlxRa} = {a,b}
487
(2) [b] = {x e AlxRb} = {a,b} = [a]
(3) [c] = {x e AixRc} = {c}
Two distinct equivalence classes exist, [a] and [c]. Class [a] has two r e p r e s e n t a t i v e s and class [c] one representative. I The relation R on the set of words over the a l p h a b e t {a, b}, defined by xRy if ]]xli  Ilyli, is an equivalence relation (see E x a m p l e 7.41). Infinitely m a n y equivalence classes exist for R, such as {~ }, {a, b }, and {aa, ab, ba, bb}. l Find all equivalence classes of the congruence relation mod 5 on the set of integers. SOLUTION: Let r be the r e m a i n d e r w h e n an integer a is divided by 5. T h e n a  r ( m o d 5). Since the possible values of r, by the division algorithm, are 0, 1, 2, 3, a n d 4, t h e r e are five distinct equivalence classes: [0] = { . . . ,  1 0 ,  5 , 0 , 5 , [1] = { . . . ,  9 ,  4 ,
10,...}
1,6, 1 1 , . . . }
[2] = { . . . ,  8 ,  3 , 2 ,
7, 12,...}
[31 = { . . . ,  7 ,  2 , 3 , 8 ,
13,...}
[4] = { . . . ,  6 ,  1 , 4 , 9 , 1 4 , . . . }
I
These t h r e e examples lead us to the following observations: 9 Every e l e m e n t belongs to an equivalence class. 9 Any two distinct equivalence classes are disjoint. These results can be stated more formally as follows. Let R be an equivalence relation on a set A, with a and b any two e l e m e n t s in A. T h e n the following properties hold: (1) a ~ [a]. (3) If [a] r [bl, t h e n [ a ] n [b] = 0.
(2) [a] = [bl if and only i f a R b .
PROOF: (1) Since R is reflexive, aRa for every a ~ A, so a e [a]. (2) Suppose [a]  [b]. Since a ~ [a] by p a r t (1), a ~ [b]. Therefore, by definition, aRb.
Chapter 7 Relations
488
Conversely, let aRb. To show that [a] c_ [b]: Let x e [a]. Then xRa. Since x R a and aRb, x R b by transitivity. Therefore, x e [b] by definition. Thus [a] __ [b]. Similarly, [b] __ [a]. Thus, [a] = [b]. (3) We will prove the contrapositive of the given statement: If[a]u[b] r ~, then [a]  [b]. Suppose [a] u [b] r ~. Then an element x should be in [a] A [b]. Then x e [a] and x e [b]. Since x e [a], x R a and hence a R x by symmetry. In addition, since x e [b], xRb. Thus a R x and x R b . Therefore, a R b by transitivity. Thus [a] = [b], by part 2. This concludes the proof.
m
It follows by Theorem 7.10 that any two equivalence classes are either identical or disjoint, but not both. Notice that Example 7.43 has two disjoint equivalence classes, la] and [c]; their union is the whole set A. Therefore, {la], [c]} is a partition of A. In fact, every equivalence relation on a set induces a partition of the set, as given by the next theorem. ~
Let R be an equivalence relation on a set A. Then the set of distinct equivalence classes forms a partition of A. m The next four examples illuminate this theorem.
~
• •
The relation belongs to the s a m e d i v i s i o n as is an equivalence relation on the set of teams in the American (National) League of majorleague baseball. Let x denote a certain team in the American League. Then the class [x l consists of all teams that belong to the same division as x. By Theorem 7.11, the set of teams in the league can be partitioned as {IYankees ], [White Sox], [Marinersl }. m By Example 7.41, the relation has the same length as on the set of words Z* over the alphabet E = {a, b} is an equivalence relation. Then the set of equivalence classes formed is {lZ 1, Ia 1, laa !, laaa 1,... }; it is a partition of E*. m (optional) Suppose a FORTRAN program contains the variables A through J and the equivalence statement:
EQUIVALENCE (A,B),(C,D),(F,A,G),(C,J),(E,H) By Example 7.42 the relation shares the s a m e m e m o r y location as is an equivalence relation on the set of variables V. Let V1 = {A,B,F,G}, V2 = {C,D,J}, V3 = {E,H}, and V4 = {I}. The partition of V induced by this relation is {V1, V2, V3, V4 }. See Figure 7.36.
7.8 EquivalenceRelations
489
F i g u r e 7.36 Set of variables V.
m By Example 7.45, the distinct equivalence classes formed by the c o n g r u e n c e relation modulo 5 on Z are [0], [1], [2], [3], and [4]. T h e y form a p a r t i t i o n of the set of integers, as shown in Figure 7.37.
F i g u r e 7.37 A partition of the set of integers Z.
m
Conversely, does every partition yield an equivalence relation? The next t h e o r e m shows t h a t every partition does. Every partition of a set induces an equivalence relation on it.
PROOF: Let P = {A1,A2,... } be a partition of a set A. Define a relation R on A as: aRb if a belongs to the same block as b. We shall show t h a t R is indeed an equivalence relation. 9 Since every element in A belongs to the same block as itself, R is reflexive. 9 Let aRb. T h e n a belongs to the same block as b. So b belongs to the same block as a. T h u s R is symmetric. 9 Let aRb and bRc. T h e n a belongs to the same block as b and b to the same block as c. So a belongs to the same block as c. Therefore, R is transitive. T h u s R is an equivalence relation,
m
How can we find the equivalence relation c o r r e s p o n d i n g to a partition of a set? The next example d e m o n s t r a t e s how to accomplish this. Find the equivalence relation o n A
{{a,b},{c}}.
{a,b,c} c o r r e s p o n d i n g to the p a r t i t i o n
Chapter 7 Relations
490
SOLUTION: Define a relation R o n A as follows (see the above proof): xRy ifx belongs to the same block as y. Since a and b belong to the same block, aRa, aRb, bRa, and bRb. Similarly, cRc. T h u s R = {(a, a), (a,b,), (b, a), (b, b), (c, c)}. I
Example 7.50 can serve to develop an algorithm for finding the equivalence relation corresponding to a partition P of a finite set A. It is given in Algorithm 7.3.
Algorithm Equivalence Relation (P,A,R) (* This algorithm determines the equivalence r e l a t i o n R corresponding to a p a r t i t i o n P of a f i n i t e set A. *) Begin (* algorithm *) while P # ~ do begin (* while *) e x t r a c t a block B pair each element in B with every element in B P 1582) is given by d = r + [2.6m  0.2J  2C + D + [C/4J + [D/4J (mod 7) where C = [y/100J and D = y mod 100; d = 0 denotes Sunday; and m  1 denotes March, m  11 J a n u a r y , and m  12 F e b r u a r y . This f o r m u l a is called Z e l l e r ' s f o r m u l a , after Christian J u l i u s J o h a n n e s Zeller ( 1 8 4 9 1899). Find the C h r i s t m a s day of each year. 43. 2000
44. 2020
45. 2345
46. 3000
Let a, b, c, d, m ~ Z with m > 2. Prove each. 47. If a _= b (mod m) and c _= d (mod m), t h e n a § c = b § d (mod m). 48. If a  b (mod m) and c _ d (mod m), t h e n ac =_ bd (mod m). 49. Let r be the r e m a i n d e r when a is divided by m. T h e n a ~ r (mod m). 50. If a  r (mod m) and 0 < r < m, r is the r e m a i n d e r w h e n a is divided by m. 51. Let rl and r2 be the r e m a i n d e r s w h e n a and b are divided by m, respectively. T h e n a  b (mod m) if and only if rl  r2 (mod m). 52. A positive integer N is divisible by 3 if and only if the s u m of its digits is divisible by 3. [Hint" 10  1 (mod 3).] 53. A positive integer N is divisible by 9 if and only if the s u m of its digits is divisible by 9. [Hint: 10  1 (mod 9).] Using the congruence relation, find the r e m a i n d e r w h e n the first integer is divided by the second. 54. 256, 3
55. 657, 3
56. 1976, 9
57. 389, 276, 9
(Hint" Use Exercise 52 or 53.)
58. The U n i t e d Parcel Service assigns to each parcel an identification n u m b e r of nine digits and a check digit. The check digit is the r e m a i n d e r mod 9 of the 9digit n u m b e r . C o m p u t e the check digit for 359,876,015.
7.9 Partialand Total Orderings
493
59. Every b a n k check has an 8digit identification n u m b e r d i d 2 . . , d8 followed by a check digit d given by d  ( d l d 2 , . . . , d s ) (7, 3, 9, 7, 3, 9, 7, 3) mod 10, where (Xl,X2,... ,Xn) 9 (Yl,Y2,..,Yn) n xiYi. (It is the dot p r o d u c t of the two ntuples.) C o m p u t e the i=1 check digit for 17,761,976. 60. Libraries use a sophisticated c o d e  a  b a r s y s t e m to assign each book a 13digit identification n u m b e r dl, d 2 . . . d13 and a check digit d. Let k denote the n u m b e r of digits a m o n g d l , d 3 , d5, d7, d9, d11, and d13 greater t h a n or equal to 5. T h e n d is c o m p u t e d as d  [  ( d l , d 2 , . . . ,d13) 9 (2, 1,2, 1,2, 1,2, 1,2, 1,2, 1,2)  k] m o d 10, where the dot indicates the dot product. C o m p u t e the check digit for 2,035,798,008,938. "61. ( T h e c o c o n u t s a n d m o n k e y p r o b l e m ) * Five sailors and a m o n k e y are m a r o o n e d on a desert island. D u r i n g the day they g a t h e r coconuts for food. They decide to divide t h e m up in the m o r n i n g and retire for the night. While the others are asleep, one sailor gets up and divides t h e m into equal piles, with one left over t h a t he t h r o w s out for the monkey. He hides his share, puts the r e m a i n i n g coconuts together, and goes back to sleep. L a t e r a second sailor gets up and divides the pile into five equal shares with one coconut left over, which he discards for the monkey. Later the r e m a i n i n g sailors repeat the process. Find the smallest possible n u m b e r of coconuts in the original pile.
J u s t as we used the concepts of reflexivity, s y m m e t r y , and t r a n s i t i v i t y to define equivalence relations, we can use reflexivity, a n t i s y m m e t r y , and transitivity to introduce a new class of relations: partial orders. We begin this section with an example. Building a house can be b r o k e n down into several tasks, as Table 7.6 shows. Define a relation R on the set of tasks as follows: Let x and y be any two tasks; t h e n x R y if x = y or m u s t be done before y. This relation is reflexive, antisymmetric, and transitive (verify). Such a relation is a partial order. *Writer Ben Ames Williams used this problem in a short story titled "Coconuts," which appeared in the October 9, 1926, issue of The Saturday Evening Post. The story concerned a contractor who wanted to bid on a large contract. Knowing of their competitor's strong passion for recreational mathematics, one of his employees gave him this problem. The competitor became so obsessed with solving the puzzle that he forgot to enter his bid before the deadline.
Chapter 7 Relations
494 Table 7.6
Task
Requires the completion of
(1) Building the foundation (t 1) (2) Framing (t 2) (3) Subflooring (t 3) (4) Partitioning into rooms (t4) (5) Roofing (t 5) (6) Plumbing (t6) (7) Wiring (t 7) (8) Siding (t8) (9) Flooring (t 9) (10) Interior painting (tlo) (11) Exterior painting (tll) (12) Carpeting (t12) ( 13 ) Installing fixtures (t 13 )
None tl tl,t2 tl,t2,t3
tl,t2 tl,t2 tl,t2 tl,t2,t5,t9 tl,t2,t6
tl through t5, t 7 tl,t2,t8,t9
t 1 through t 7 , t 9 , t l o t I through t 11
Partial Order A r e l a t i o n R on a set A is a p a r t i a l o r d e r if it is reflexive, a n t i s y m m e t r i c , a n d t r a n s i t i v e . T h e set A w i t h its p a r t i a l o r d e r R is a p a r t i a l l y o r d e r e d set (or poset), d e n o t e d by (A, R). W h e n t h e p a r t i a l order is clear from t h e context, call t h e poset A. T h e n e x t t h r e e e x a m p l e s i l l u s t r a t e t h e s e definitions. T h e r e l a t i o n < on R is reflexive, a n t i s y m m e t r i c , a n d t r a n s i t i v e , so < is a p a r t i a l order on R a n d (R, ___ 0. Let An denote the n u m b e r of i n d e p e n d e n t subsets of vertices in the path. W h e n n = 0, the p a t h Po consists of a single point vo, so t h e r e are two possible i n d e p e n d e n t subsets: O, {vo}. W h e n n = 1, the p a t h is vovl. T h e n t h e r e are t h r e e i n d e p e n d e n t subsets of {vo, Vl }, namely, O, {vo}, and {Vl}. W h e n n  2, the p a t h P2 contains t h r e e vertices" vo, vl, and v2. So, t h e r e are five i n d e p e n d e n t subsets: O, {vo}, {Vl}, {v2}, and {vo, v2}. *T. Koshy, Fibonacci and Lucas Numbers with Applications, Wiley, New York, 2001.
Chapter 8 Graphs
548
T h e s e d a t a are s u m m a r i z e d in Table 8.5. Clearly, a p a t t e r n e m e r g e s . It seems safe to conjecture t h a t A n  F n + 3 , w h e r e n > 0. We invite y o u to confirm this. T a b l e 8.5
P a t h Pn
Independent subsets
9 v0 : : v0 Vl
0,
{v0}
2
O,
{v0},{Vl}
3
O,
{ v o } , { V l } , { v 2 } , { v o , v2}
5
O,
{v0}, {v1}, {v2}, {v3}
8
: vo
: Vl
: v2
. . . . v0 Vl v2
v3
An
{v0,v2}, {v0, v3}, {Vl, v3} r~+3
m
A simple p a t h m a y r e p e a t vertices if t h e y be its endpoints. S u c h a p h e n o m e n o n carries a special label.
Cycle and Circuit A p a t h with e n d p o i n t s v0 and vn is c l o s e d if v0  v,z; otherwise, it is o p e n . A simple closed p a t h is a c y c l e ; a closed p a t h with no r e p e a t e d edges is a circuit. Table 8.6 s u m m a r i z e s and illustrates the basic t e r m s i n t r o d u c e d t h u s far in this section. Refer to it as often as needed.
T a b l e 8.6
Term
Meaning
E x a m p l e from Figure 8.43
Path
Sequence v o  e l  v 1 . . . . . e n  v n , ei={vi_l,vi} ,l_2
where Hn1 R denotes the list Hn1 in the reversed order. For example,
H2
00 01 11 10
H3
DO0 001 011 010 110 111 101 100
Then
This clearly agrees with the Hamiltonian cycle in Figure 8.75. (Can you find such a cycle for Q4 using recursion? See Exercise 60.) m
Qn serves as a fine model of parallel computation, where each vertex represents a processor. Since Qn is Hamiltonian, it follows that every processor can communicate with every other processor, either directly or indirectly. Hamiltonian cycles are a misnomer because Hamilton was certainly not the first person to look for them. One earlier reference to them can be found in the famous K n i g h t ' s t o u r p r o b l e m stated below. Knight's Tour Problem
Is it possible for a knight to visit each square on a chessboard exactly once and return to the home square? To see how this problem is related to finding Hamiltonian cycles in a graph, represent each square by a vertex and the two squares connecting a knight's move by an edge. Then a knight's tour is possible if and only if the corresponding graph is Hamiltonian.
568
Chapter 8 Graphs
Figure 8.76
Figure 8.77
For example, Figure 8.76 shows a 4 x 4 chessboard and Figure 8.77 a graph representing a set of legal moves by a knight; u n f o r t u n a t e l y , it lands at some squares more t h a n once; therefore, the graph is not Hamiltonian. With a bit of patience, we will find t h a t a k n i g h t ' s tour is impossible on a 4 x 4 board. F u r t h e r m o r e , no solution exists on an n x n board if n is odd. In 1759, Euler published an algorithm for finding a tour on an 8 z 8 chessboard, and 12 years later AlexandreTh~ophile V a n d e r m o n d e presented another one. Figure 8.78 shows a solution. (Finding a k n i g h t ' s tour is a good exercise in "backtracking"; see Section 9.2.)
Figure 8.78
An interesting observation: Using the t o u r in Figure 8.78, suppose we n u m b e r the squares 1 t h r o u g h 64 in the order they are visited. The resulting square (see Figure 8.79) is a pseudomagic square: the row sums and column sums are equal, each being 260, but the diagonal sums are different.
8.5
Eulerian and Hamiltonian Graphs
569
A l e x a n d r e  T h d o p h i l e V a n d e r m o n d e (17351796) was born in Paris. Music was his first love and he developed an interest in mathematics later in life. His entire mathematical contribution consists of four papers published in the Histoires of the Academy of Sciences during 17711772. They are concerned with the theory of equations, theory of determinants, factorials, and the knight's tour. Vandermonde died in Paris.
Figure 8.79
35 26 37
14 63
38 15 34 25 27 36
12
13 40 21
16 39 28 33 52
24 11 50 51 62 23 64 49 9
10
22 61
29 54 41 20
1
60
7
48
42 17 32
8
45
4
59
19 44 57
2
47
6
18 43 56 31 46
5
58
3
55 30
53
Hamiltonian paths have interesting applications to combinatorics, as the next example shows. (optional) A p o w e r c h a i n o f o r d e r n is a p e r m u t a t i o n of the first n (>_ 2) natural n u m b e r s such that the sum of every pair of adjacent elements is a power. For example, 81726354 is a power chain of order 8. Find all power chains of order 7.*
SOLUTION: A systematic procedure for finding all power chains of order n follows. Draw a graph with n vertices, labeled 1 t h r o u g h n. Two vertices i and j in the graph are adjacent if i + j is a power. Each Hamiltonian path yields a power chain. In particular, consider the graph in Figure 8.80 with n = 7. Since the degree of vertex 4 is 1, it is easier to list all Hamiltonian paths beginning with 4. For convenience, redraw the graph as in Figure 8.81. It follows from this graph t h a t there are exactly two power chains of order 7:4531726 and 4536271.
*Proposed by H. L. Nelson and solved by B. Barwell, J. Recreational Mathematics, Vol. 12:1, 19791980, pp. 6768.
570
Chapter 8 Graphs
F i g u r e 8.80
3
2=,
4
~
6
6
5
F i g u r e 8.81
1 4o
s
7
3_7

~
Ha
Although a simple test will determine whether or not a connected graph has an Eulerian circuit or an Eulerian path, nothing similar exists for determining if it contains a Hamiltonian cycle or path. Finding such a test, without an exhaustive search of all n! possible paths, remains a major unsolved problem in graph theory. A host of sufficient conditions exist, however, for a connected graph to be Hamiltonian. Two lie below, without proofs, but illustrated. ( D i r a c ' s T h e o r e m ) A simple connected graph G with n > 3 vertices is n
Hamiltonian if deg (v) > ~ for every vertex v in G.
m
Notice that the condition in Theorem 8.9 does not apply to the graph G1 in Figure 8.73 and the 3cube in Figure 8.75. Nonetheless, both are Hamiltonian. So even if the condition is not satisfied, the graph may be Hamiltonian. The next theorem provides another sufficient criterion for a graph to be Hamiltonian. (Ore's T h e o r e m ) Let G be a simple connected graph with n > 3 vertices. If deg (u) + deg (v) > n for every pair of nonadjacent vertices u and v, then G is Hamiltonian. m You may verify that deg (u) + deg (v) > 5 for every pair of nonadjacent vertices u and v of the graph G3 in Figure 8.67. Consequently, by Theorem 8.10, G3 is Hamiltonian, as expected. The condition in the theorem is not satisfied by the 3cube in Figure 8.75; nonetheless, Q3 is Hamiltonian. Next we present the wellknown traveling salesperson problem, which employs Hamiltonian cycles and has interesting applications to the theory of communications.
8.5 Eulerianand HamiltonianGraphs
571
Gabriel A n d r e w D i r a c (19251984), a pioneer in graph theory, was born in Budapest. He moved to England in 1937 when his mother married Paul Adrien Maurice Dirac, a Nobel Laureate in physics. In 1942, he began his studies at Cambridge University. After a twoyear interruption by the war, he continued his studies at Cambridge and London, receiving his Ph.D. in mathematics from the University of London (1951). That same year he was awarded the Rayleigh Prize by Cambridge University. Dirac taught at the universities of London, Toronto, Hamburg, Wales, and Aarhus. He was a member of the editorial board of the Journal of Graph Theory and the European Journal of Combinatorics. Besides his work with graph theory, he made outstanding contributions to number theory and geometry. He was also a passionate art connoisseur.
Oystein Ore (18991968), a Norwegian mathematician, was born in Oslo. He received his Ph.D. from Oslo University in 1924 and taught there for 2 years. He joined Yale University in 1927 and taught there until his retirement in 1967, holding the chail ,:,fthe mathematics department from 1936 to 1945. Ore served on the board of American Relief for Norway from 1942 to 1947 and chaired the Relief Mission in 19451946. Recognizing his contributions to Norway, King Haakon VII decorated him Knight of St. Olaf in 1947. As an author of numerous articles and several books, Ore made outstanding contributions to graph theory, abstract algebra, number theory, and probability.
Traveling Salesperson Problem Study the weighted graph in Figure 8.82. The vertices represent cities and the weights represent the distances between them. A salesperson assigned to city a would like to visit every other city exactly once and return to the home city so that the total distance traveled is a minimum. In other words, beginning at a, he would like to find a Hamiltonian cycle, so the sum of the weights along the cycle is a minimum. This is the celebrated t r a v e l i n g salesperson problem.
Figure 8.82
C
b , ~ ~ 1 0~  d a
9
e
572
Chapter 8 Graphs When the graph contains only a few vertices, you can list all possible cycles, compute the sum of the weights along each, and find the shortest Hamiltonian cycle. For example, the graph in Figure 8.82, by the multiplication principle, has 4!/2 = 12 distinct Hamiltonian cycles, each containing five edges. You may verify that the cycle with least weight is adbcea; so the total distance traveled is 60 miles. Finding a Hamiltonian cycle in a complete graph Kn without searching through all distinct (n  1)!/2 Hamiltonian cycles remains an unsolved problem. We close this section with another graphtheoretic game. (optional) The game of H a m (for H a m p t o n Court) is a graphtheoretic game quite similar to SIM, invented by B. Recaman in 1977. Like SIM, Ham is also a twoperson game. Two players, R (for red) and B (for blue), take turns coloring an edge of the complete graph Kn. (A game on Kn is a game of o r d e r n.) Player R's objective is to obtain a Hamiltonian cycle made up entirely of red edges while B's goal is to prevent R from doing it. Whoever achieves his\her goal wins the game. The game of order 3 has a trivial winning strategy for player B. She can always block R from completing a Hamiltonian cycle. (So B wins.) See Figure 8.83, where solid edges indicate red edges and broken edges, blue.
Figure 8.83 iI
Player B wins.
Figure 8.84
J ~_
.
~
I
.
~k
.
.
.
Player B wins.
Figure 8.85 Player B wins. Player B can always win games of orders 4 and 5, as in Figures 8.84 and 8.85. The game of order 6, although a bit more complicated, can also be won by player B. After R has colored the first edge {x, y}, B colors an edge {u, v} not incident with x or y. Thereafter, B colors four more edges incident with x or y. At this stage, if R has at least two edges incident with both u and v, she can win (see Figure 8.86). But B can avoid this by coloring one of the two edges missing in the Hamiltonian cycle (see Figure 8.87). Is player B favored to win if the game is of order 7 or more? It is conjectured so.
573
8.5 Eulerianand Hamiltonian Graphs
F i g u r e 8.86
u
U
Player R wins. ", "b.~
~~/
',,V.:x.~
Figure 8.87 Player B wins. t
x
3'
u
U
,," J.~{ z  . k \ "
I
\
i
x
I ".
\
3'
I
Exercises 8.5
Determine if each graph is Eulerian.
2.fi
3.
5./%
7,,
0
'V"

Construct an Eulerian circuit for each Eulerian graph. 7.
b a ~ c e5
8.
b a ~ c e8x x ~ / e5 d
9.
b ae
~ e~
c
574
Chapter 8 Graphs a
10.
e2
b
11.
/
~e 4 X x ~ e 6
e
d
e7
d
C
12. e2
'Je4~ //~ 6= e8 d
13. Is the Petersen graph in Figure 8.28 Eulerian? 1419. Do the nonEulerian graphs in Exercises 16 have an Eulerian path? Find an Eulerian path in each graph, if possible. 20.
el e
21. e4
c
22.
a
e
b
a
e
12
d
d
Under what conditions will each graph be Eulerian? 23.
Kn
24.
Km,n
2 5  2 6 . Under what conditions will each graph in Exercises 23 and 24 contain an Eulerian path, but not an Eulerian circuit? 27. If a connected rregular graph is Eulerian, what can you say about r? 2 8  4 2 . Are the graphs in Exercises 112 and 2022 Hamiltonian? If one is not Hamiltonian, determine if it has a H a m i l t o n i a n path; if so, find it. 43. Under what conditions will the complete graph K,~ be Hamiltonian? 44. If G is a connected graph containing a vertex with degree 1, can it be Hamiltonian? Determine if each complete bipartite graph Km,n is Hamiltonian. If a graph is not Hamiltonian, does it contain a Hamiltonian path? 45. K2,3
46. K3,3
49. For what values of m and n will
47. K2,4
Km,n be
48. K3,4
Hamiltonian?
50. Is the Petersen graph in Figure 8.28 Hamiltonian?
8.,5 Eulerianand Hamiltonian Graphs
Consider the t r i a n g l e Figure 8.88.*
graph
575
Tn, where T1, T2, T3 are shown in
F i g u r e 8.88
A
A
/%
w
T1
T2
T3
51. When will Tn be Eulerian? Consider the s q u a r e Figure 8.89.
graph
52. When will Tn be Hamiltonian? Sn,
where $1, $2, $3 are pictured in
F i g u r e 8.89 v
_
S1
$2
$3
53. When will S,, be Eulerian?
54. When will Sn be Hamiltonian?
Give an example of a graph that is: 55. Both Eulerian and Hamiltonian. 56. Eulerian, but not Hamiltonian. 57. Hamiltonian, but not Eulerian. 58. Neither Eulerian nor Hamiltonian. 59. Figure 8.90 shows five cities, a t h r o u g h e, and the distances between them. A salesperson based at a would like to visit each city exactly once and r e t u r n to a, covering the fewest miles. Find the route she should take and the m i n i m u m distance she would travel. 60. Display a Hamiltonian cycle for the 4cube. Find all power chains of order n, if they exist, for each value of n. 61. 6
62. 8
63. 9
64. 15
*Exercises 5154 are based on A. Guckin et al., The Euler Circuit Project, COMAP, Inc., Lexington, MA, 1989.
576
Chapter 8 Graphs
Figure 8.90
c
1
3
~
a
5
6 e
*65. A power cycle of order n is a cyclic permutation of the first n (> 2) positive integers such that the sum of every pair of adjacent elements is a power. Find a power cycle of order 17. 66. Write an algorithm to determine if a connected graph is Eulerian, using its adjacency list representation. 67. Write an algorithm to determine if a connected graph contains an Eulerian path, using its adjacency matrix.
Take a good look at every graph presented so far. In some cases, the edges meet only at the vertices, whereas in the other cases the edges meet at nonvertices. For example, the edges of the K6nigsberg bridge model (Figure 8.2) meet only at its vertices, whereas the edges of the pentagram in Figure 8.5 intersect at points that are not necessarily vertices. However, the confusion can be resolved. The pentagram can be redrawn in such a way that its edges intersect only at its vertices, as Figure 8.91 shows. The graphs in Figures 8.5 and 8.91 are clearly isomorphic. You are invited to verify this.
Figure 8.91
0
3
2
w
From these observations arises the following definition.
Planar Graph A graph is p l a n a r if it can be drawn in the plane, so its edges meet only at the vertices. Such a drawing is a p l a n a r r e p r e s e n t a t i o n of the graph.
8.6 Planar Graphs
577
The previous discussion indicates the pentagram is planar. The graph in Figure 8.92 is planar since it can transform to Figure 8.93; they are isomorphic graphs. The graph in Figure 8.94 is also planar; Figure 8.95 shows its planar representation. F i g u r e 8.92
F i g u r e 8.93
a
d
c
a
b
C v
F i g u r e 8.94
b
wd
b c
d
F i g u r e 8.95
c) Planarity is an important concept with fine, realworld applications. For example, it figures prominently in designing circuit boards. The following example answers a problem posed at the beginning of the chapter.
~
(The T h r e e H o u s e s  U t i l i t i e s P u z z l e ) A developer is building three new houses m A , B, and C r o o n one side of a street. She would like to connect three utilities m electricity (E), telephone (T), and water ( W )  to each house. This situation can be modeled by the complete bipartite graph K3,3 in Figure 8.96. Can the developer lay the utility lines without any crossings? To answer this, try to make a planar representation of K3,3. Draw eight of the nine edges in K3,3 (see the solid edges in Figure 8.97). How can we draw the edge from W to A? Notice that W lies on the circuits WCTBW and WCEBW. Consider the regions bounded by them. Since A lies outside both, any edge
578
Chapter 8 Graphs
F i g u r e 8.96
A
B
C
E
T
W
from W to A m u s t cross a b o u n d a r y (see the broken edges in the figure). T h u s K3,3 is n o n p l a n a r and the puzzle has no solution. The utility lines will cross. (Example 8.41 will prove this algebraically.)
F i g u r e 8.97
"
B
RI", ,,,~_~ 1 , C
m An a r g u m e n t like this geometric proof can show t h a t the complete graph /(,5 is also nonplanar. Notice t h a t any planar r e p r e s e n t a t i o n of a connected graph divides the plane into regions, including the u n b o u n d e d region. For instance, the planar graph in Figure 8.98 divides the plane into five regions, m a r k e d 1 t h r o u g h 5. The graph has six edges and and three vertices. Let r denote the n u m b e r of regions formed by e edges and v vertices. T h e n r  e  v + 2. In fact, this relationship is not a coincidence, but is always true.
F i g u r e 8.98 5
( E u l e r ' s f o r m u l a ) Let G be a connected planar graph with e edges and v vertices. Let r be the n u m b e r of regions formed by a planar r e p r e s e n t a t i o n of G. T h e n r  e  v + 2.
8.6 PlanarGraphs
579
P R O O F (by i n d u c t i o n on e)" Let P(n)" E u l e r ' s f o r m u l a holds for every c o n n e c t e d p l a n a r g r a p h w i t h n edges. B a s i s s t e p Clearly, P(0) is t r u e . S u p p o s e t h e g r a p h c o n t a i n s one edge. T h e n t h e r e are two possibilities: T h e edge m a y or m a y not be a loop (see F i g u r e 8.99). In t h e first case, e  1, v  1, a n d r  2. In t h e second case, e  1, v  2, a n d r  1. In b o t h cases, r  e  v + 2. C o n s e q u e n t l y , P(1) is also true.
F i g u r e 8.99
I n d u c t i o n s t e p A s s u m e P(k) is t r u e for some k > 0; t h a t is, a s s u m e t h e f o r m u l a holds for every c o n n e c t e d p l a n a r g r a p h w i t h k edges. C o n s i d e r a c o n n e c t e d p l a n a r g r a p h G w i t h k + 1 edges, v vertices, a n d r regions. G e i t h e r has a cycle or does not. C a s e 1 Suppose G does not c o n t a i n a cycle. T h e n G m u s t c o n t a i n a v e r t e x u of degree 1. To find such a vertex, build a p a t h from a n y v e r t e x u n t i l you c a n n o t go a n y f u r t h e r . The last v e r t e x visited is such a v e r t e x u. (See F i g u r e 8.100.) Let a be t h e edge i n c i d e n t w i t h u. Let H be t h e g r a p h o b t a i n e d by deleting v e r t e x u a n d edge a from G. H c o n t a i n s e'  k edges a n d v' = v  1 vertices. Since G is connected, so is H. Therefore, by t h e i n d u c t i v e hypothesis, the f o r m u l a holds for H ; t h e n u m b e r of regions r' f o r m e d by H satisfies t h e f o r m u l a r' = e'  v' + 2. B u t r' = r. So r'
( e  1)  (v  1) + 2
T h a t is,
rev+2 F i g u r e 8.100
/a C a s e 2 Suppose G c o n t a i n s a cycle. Let a be an edge in the cycle (see F i g u r e 8.101). Let L be t h e s u b g r a p h o b t a i n e d by d e l e t i n g edge a from G. (Note: Deleting edge a m e r g e s regions R1 a n d R2.) This s u b g r a p h c o n t a i n s e' = k edges a n d v'  v vertices; t h e r e f o r e , by t h e i n d u c t i o n hypothesis, t h e n u m b e r of regions r' f o r m e d by L is given by r' = e'  v' + 2. B u t r'  r  1, e' = e  1, a n d v' = v. So r
1(e
1) v+2
Chapter 8 Graphs
580
That is, rev+2
F i g u r e 8.101
By cases 1 and 2, P(k + 1) is true. Therefore, by PMI, the formula holds for every connected planar graph, n The next two examples illustrate Euler's formula. ~
Verify Euler's formula for the connected planar graph in Figure 8.102.
F i g u r e 8.102
SOLUTION: The graph contains 22 edges and 13 vertices, which form 11 regions. So e  v + 2  2 2  1 3 + 2  11  r. m ~
A connected planar graph has 17 edges, dividing the plane into nine regions. How many vertices does the graph have?
SOLUTION: Here e  17 and r  9. By Euler's formula, ver+2
179+2
10
m
Euler's formula can generate simple relationships in special cases. One such formula is given in the following theorem. If G is a simple, connected, planar graph t h a t contains e (>2) edges and v vertices, then e < 3v  6.
PROOF: Suppose the plane is divided into r regions by a planar r e p r e s e n t a t i o n of the graph. First, we shall show t h a t 2e > 3r. If e  2, then r  1. So 2e > 3r.
8.6 PlanarGraphs
581
Suppose e > 2. Let s be the total n u m b e r of edges along the boundaries of the various regions. Every edge appears on the b o u n d a r y of two regions, so every edge is counted twice in this sum and s  2e. Now every region, including the u n b o u n d e d region, is bounded by at least t h r e e edges. Therefore, s > 3r and 2e > 3r. By Euler's formula, r+v2e
Then 3r + 3v  6

3e
3 r + 3v  6  e

2e
3r + 3v  6  e
> 3r
So
T h a t is, e4. Figure 8.117 shows a coloring with exactly four colors. How do we interpret this result? Since the chromatic number is four, the final exams can be scheduled conflictfree using four time slots, as Table 8.8 shows.
Table 8.8
Block Course(s)
Ibl A,C
234 B,G
D
E,F
We close this section with the next example, which illustrates how graph coloring can be employed to design traffic light patterns at intersections. Figure 8.118 shows the intersection of two divided avenues, California and New York, where all left and right turns are permitted. The arrows indicate
8.7
GraphColoring
593
the traffic flows along each avenue. Assuming they are equally heavy in each direction, design a traffic signal pattern for the intersection.* F i g u r e 8.118 I I i I
li il orniaAve Ca,i I
i
'l
I I
I t
SOLUTION: Since the four right turns do not interfere with the other traffic flows, they can safely be dropped from our discussion. The remaining traffic directions are pictured in Figure 8.119 and are labeled A through H. We need to develop a traffic pattern so that vehicles can pass through the intersection without interfering with other traffic flows. F i g u r e 8.119
>
,H,~
~i~I~
~.I+I I I ~ California Ave. F
k A
iill'i ,, I ICID
*Based on J. Burling et al., "Using Graphs to Solve the Traffic Light Problem," F A I M Module, COMAP, Inc., Lexington, MA, 1989.
594
Chapter 8 Graphs
Figure 8.120
To this end, represent each traffic flow with a vertex. Two vertices are adjacent if the corresponding traffic flows cross each other. For instance, directions C and E intersect, so vertices C and E are adjacent. Figure 8.120 shows the resulting graph. Now color its vertices. Since the graph contains C3 as subgraph (verify), its chromatic number is > 3. Is it in fact three? Let us see.
Figure 8.121 H
A E
Suppose we color A red, C blue, and H yellow (see Figure 8.121). Then B cannot be blue or yellow, but can be red; so color it red. Then E must be yellow; this forces F to be blue. D, being adjacent to B, F, and H can't be red, blue, or yellow, so it must be assigned a new color, say, green. Thus the graph takes at least four colors. Figure 8.121 shows a coloring of the graph with exactly four colors, which depicts an efficient way of designing the traffic signal pattern. It consists of four phases: 9 Vertices B and F are green, so traffic flows B and F proceed, while others are waiting. 9 Vertices D and H are yellow; that is, only traffic directions D and H proceed simultaneously.
8.7 GraphColoring
595
9 Vertices A and E are red; t h a t is, traffic flows A and E continue at the same time, while others are stopped. 9 Vertices C and G are blue, so only traffic directions C and G proceed simultaneously. See Table 8.9 also.
T a b l e 8.9
Traffic light pattern Phase 1
Phase 2
Phase 3
Phase 4
Only B and F proceed.
Only D and H proceed.
Only A and E proceed.
Only C and G proceed.
Exercises 8.7 Find the chromatic n u m b e r of each map or graph. 0
0
3.
~
4.
II
Chapter 8 Graphs
596
.
Figure
8.122
Continental United States.
i
6. Figure 8.5
7. Figure 8.18
8. Figure 8.56
9. Graph G1 in Figure 8.73 10. Figure 8.102 12. Petersen graph
11. Figure 8.106
13. The Star of David 14. Wheel graph
Wn
15. 3cube Q3 16. Characterize graphs with chromatic number 1. 17. Let G be the union of two simple disconnected subgraphs H1 and H2 with chromatic numbers m and n, respectively. What can you say about the chromatic number c of G? In Exercises 18 and 19, schedule conflictfree committee meetings using the smallest number of blocks. Identify such a schedule. 18.
Committee
Committee
Committee
Committee
Committee
1
2
3
4
5
B
A
C
A
B
D E F
E G H
D F H
C E F
G H I
19.
Committee Committee Committee Committee Committee Committee 1
2
3
4
5
6
B
D
A
B
A
C
D E F
E G H
C D F H
C E H I
E G H
G H I
8.7
Graph Coloring
597
A zoo curator would like to add new open "islands" for seven species of animals to roam about freely. Unfortunately, some species prey on some others, as given by the incompatibility a r r a y in Table 8.10. Table 8.10
Species
1
2
.
3
4
5
6
7
.
.
9
9
9
.
9
.
7
9
.
.
9
9
9
9
.
9
9
9
9
9
9
9
9
20. Determine the m i n i m u m n u m b e r of islands needed to keep them. 21. Find a possible way of accommodating the animals on those islands. A pet shop owner just received a shipment of 10 species of tropical fish. Since some species are incompatible, t h a t is, they fight with some other species, they cannot be kept in the same tank. Table 8.11 s u m m a r i z e s the incompatibility of the various species, where a dot in row i and column j indicates species i and j are incompatible. Table 8.11
Species
1
2
9
9
3
4
9
5
6
9
9
9
9
9
10
9
9
9
9
9
.
.
8
9
9
9
9
7
,
9
.
9
9
9
9
.,
,
.
.,
9
9
9 10
22. Determine the m i n i m u m n u m b e r of t a n k s needed to store the fish. 23. Find a possible way of storing t h e m a m o n g those tanks. Figure 8.123 shows the traffic flows at an exit from a shopping center into a twoway street. (J. Williams, 1992) 24. Represent this information in a graph.
Chapter 8 Graphs
598
25. Develop a traffic light pattern so that traffic will flow smoothly at the exit.
Figure
8.123
Shopping Center
D A
v
I I I I
B
Figure
C
8.124
Shopping Center
C~ ~
j
F
A
E D
I
Figure 8.124 shows the traffic flows from a twoway street into a shopping center and from the shopping center into the street. 2627. Redo Exercises 8.24 and 8.25. *28. Let G be a simple graph with n vertices. Let k denote the maximum degree of any vertex in G. Prove that the chromatic number of G is 2) denote the n u m b e r of vertices in a selfcomplementary graph. Show t h a t n _ 0 (mod 4) or n  1 (mod 4). 18. Delete any vertex and edges incident with it in the Petersen graph. Show t h a t the resulting subgraph is Hamiltonian. 19. Find the n u m b e r of distinct H a m i l t o n i a n cycles in Kn, where n > 3. "20. Find the n u m b e r of distinct H a m i l t o n i a n cycles in Kn,n, where n > 2. "21. Find the n u m b e r of distinct Hamiltonian paths in Kn+l,n, where n>l. 22. Delete any edge from K5. Show t h a t the resulting subgraph is planar.
Chapter 8 Graphs
606
23. Delete any edge from g3, 3. Show t h a t the resulting subgraph is planar.
Computer Exercises Let G be a graph with n vertices, labeled 1 t h r o u g h n, where 1 _< n < 10. Write a program to do each task. 1. Read in n and the various edges {i, j} of G, where 1 < i, j < n. 9 P r i n t the adjacency matrix. 9 Print the degree of each vertex. 9 Print the linked list representation of G. 9 Determine if G is a simple graph. 9 Determine if G is a complete graph. 2. Let G be a weighted graph. Read in n, the various edges {i, j} of the graph, and their weights w. 9 Print the weighted adjacency matrix. 9 Print the adjacency list representation. 3. Read in the adjacency matrix of G. Print its adjacency list representation. Use the adjacency list representation to print the adjacency matrix of G. 4. Read in the adjacency matrix of a simple graph and determine if it is rregular. 5. Read in two positive integers m and n, where m, n sets V1 and V2 of a bipartite graph G, where iVll and the edges {i, j} in G, where i, j < 10. Using the and adjacency list representation of G, determine if bipartite graph Km,n.
< 10; the vertex m and IV2i  n; adjacency matrix it is the complete
6. Read in the adjacency matrix of G. Determine if G is connected. Find how many simple paths r u n from vertex i to vertex j, where 1 _< i, j < 10 and i r 7. Read in the adjacency matrix of a graph and determine if the graph is Eulerian. If it is not, see if it contains an Eulerian path. 8. Read in the edges {i, j} of a graph, where 1 < i, j < n. With the linked list representation of the graph, determine if it is Eulerian. If it is not Eulerian, determine if it contains an Eulerian path. 9. Let G be a complete weighted graph whose vertices and weights represent cities and distances between them, respectively. Read in the
Chapter Summary
607
weighted adjacency matrix of G. Find a Hamiltonian cycle so t h a t the sum of the weights along the cycle is a minimum. 10. Solve the knights puzzle in Example 8.26. List all moves. 11. Read in the various class lists in Example 8.46. Find a conflictfree final exam schedule for the courses. 12. Read in the adjacency matrix for the 48 states of the continental United States. Assign a coloring to them in such a way that adjacent states receive different colors.
Exploratory Writing Projects Using library and Internet resources, write a team report on each of the following in your own words. Provide a welldocumented bibliography. 1. Explain the applications of graph theory to various disciplines. 2. Describe the history of the traveling salesperson problem. Name a few practical applications. 3. Give a historical background of the fourcolor problem. Include a biography of K. Appel and W. Haken. 4. Explain the Chinese Postman Problem. 5. Explain how de Bruijn sequences, named after Nicolaas G. de Bruijn, can be generated from Eulerian circuits. 6. Explain the relationship between Hamiltonian cycles and the Tower of Brahma. 7. Example 8.11 is an application of Ramsey's theorem, developed by the English logician Frank P. Ramsey (19031930). The theorem laid the foundation for a branch of combinatorics called Ramsey theory. Give a brief introduction to Ramsey theory. 8. Discuss the Instant Insanity Puzzle. 9. Discuss the relationship between ncubes and parallel computers. How can two (n  1)cubes be used to construct an ncube? Also, discuss the relationship between an ncube and Gray code. 10. Write an essay on graceful graphs, a term coined by S. W. Golomb of the University of Southern California. 11. Write an essay on the game of SIM. 12. Write an essay on the game DIM, a threedimensional variation of SIM invented in 1972 by D. Engel. 13. Describe the game of Hackenbush. 14. Write an essay on chromatic polynomials.
Chapter 8 Graphs
608
15. Write an essay on the Petersen graph. 16. Investigate rook polynomials. 17. Write an essay on matching theory. 18. Write an essay on parallel algorithms. Enrichment Readings 0
0
0
0
0
0
Q
0
0
K. Appel and W. Haken, "Every Planar Map is 4Colorable," Bulletin of the American Mathematical Society, Vol. 82 (1976), pp. 711712. J. A. Bondy and U. S. R. Murty, Graph Theory with Applications, Elsevier, New York, 1976. R. A. Brualdi, Introductory Combinatorics, 3rd ed., PrenticeHall, Upper Saddle River, NJ, 1999. N. Cristofides, Graph Theory: An Algorithmic Approach, Academic Press, New York, 1975. B. W. Jackson and D. Thoro, Applied Combinatorics with Problem Solving, AddisonWesley, Reading, MA, 1990, pp. 134200. J. A. McHugh, Algorithmic Graph Theory, PrenticeHall, Englewood Cliffs, NJ, 1990. A. Ralston, "Debruijn Sequences   A Model Example of the Interaction of Discrete Mathematics and Computer Science," Mathematics Magazine, Vol. 55 (May 1982), pp. 131143. S. S. Skieno, Implementing Discrete Mathematics, AddisonWesley, Reading, MA, 1990. K. Thulasiraman and M. N. S. Swamy, Graphs: Theory and Algo
rithms, Wiley, New York, 1992. 10. A. Tucker, Applied Combinatorics, 2nd ed., Wiley, New York, 1984, pp. 379, 389410. 11. D. West, Introduction to Graph Theory, 2nd ed., PrenticeHall, Upper Saddle River, NJ, 2001. 12. R. J. Wilson and J. J. Watkins, Graphs: An Introductory Approach, Wiley, New York, 1990.
Chapter 9
Trees A n expert problem solver m u s t be endowed with two incompatible qualities m a restless imagination a n d a pertinent pertinacity. HOWARD W. EVES
T
rees are the most important class of graphs and they make fine modeling tools. In 1847, the German physicist Gustav Robert Kirchoff used them to solve systems of linear equations for electrical networks. Ten years later, A r t hur Cayley studied the isomers of saturated hydrocarbons CnH2n+2 with them. Today trees are widely used in mathematics and computer science, as well as in linguistics and the social sciences. This chapter presents the concept of a tree, and two necessary and sufficient conditions for a graph to be a tree. In addition, it presents the concept of a spanning tree for a connected graph, algorithms for finding it, and the important class of rooted trees. Trees facilitate sorting and searching, representing and computing numeric expressions, and constructing efficient coding schemes and algorithms, as will be seen in Sections 9.5 and 9.6. Some of the interesting problems trees handle well are: 9 Can four queens be placed on a 4 • 4 chessboard, so they are not attacking each other? 9 A utility company would like to lay pipelines for natural gas between five towns, a through e, as in Figure 9.1. The weights of the edges represent the costs of building the various pipelines. How cheaply can the company build them, so natural gas can be sent from any town to any other town? 9 There are 64 entrants in a singles tennis tournament. The winner of each round is advanced to the next round and plays another. Find the number of matches and rounds played to determine the champion. 9 There are eight coins in a collection plate. Although they look identical, one of them is counterfeit and heavier. Identify it, using an equal arm balance and a m i n i m u m number of weighings. 609
(;hapter 9 Trees
610
~ ~ 1 1 ~
9 ~"~ , ",/"
Gustav R o b e r t K i r c h o f f (18241887), an outstanding German physicist and son of a lawyer, was born in K6nigsberg, Prussia. After graduating from the local gymnasium, Kirchoff entered the University of K6nigsberg at the age of 18 and received his doctorate 5 years later. In 1848, his teaching career began in Berlin. Two years later he joined the faculty of the University of Breslau, where he met his future research collaborator, the wellknown chemist Robert Bunsen. In 1854, they both moved to Heidelberg. While there, with Bunsen's collaboration, Kirchoff made his greatest contributions to science. In 1875, he accepted the chair of theoretical physics at the University of Berlin, a position he held with great distinction until his death. Although Kirchoff made significant contributions to every branch of physics, he is best known for his pioneering work in spectroscopy.
~'., ," ' . ~ ' ' ,,' "~n
F i g u r e 9.1
c
1010
7~~/~,,~/15 v
a
12
e
As in graph theory, tree terminology is not standardized, so be aware of this when you refer to different texts on the topic.
Notice t h a t the graphs in Figures 9.2 and 9.3 are connected; each is a c y c l i c , m e a n i n g it does not contain a cycle. Such a graph is a tree.
Figure 9.2
H
H
H
I I CI I HI HI H
Propane C3H8.
H~C~C
F i g u r e 9.3
H
a
d
e
f
g
9.1 Trees
611
Tree A connected, acyclic graph is a tree. Trees are everywhere. You have seen several examples of trees in earlier chapters. Figure 1.1, Figures 5.165.19, and Figures 6.7, 6.8, and 6.14 offer fine cases. The graph in Figure 9.4 is not a tree, since it contains a cycle. The graphs in Figures 9.2 and 9.3 are connected; each is acyclic, so each is a tree. The graph in Figure 9.5 is not a tree, either, because it is not connected. Nonetheless, it is a set of disjoint trees, called a forest.
F i g u r e 9.4
H
H
H
H
Ethylene C2H4.
F i g u r e 9.5 A forest.
Figure 9.6 shows the f a m i l y t r e e of the Bernoullis of Switzerland, the most distinguished family of mathematicians.
F i g u r e 9.6
Nicolaus
The Bernoulli family. Jakob I
Nicolaus Nicolaus
Johann Nicolaus
Daniel
Johann III
Johann II Daniel II
Jakob II
I
Christoph Figure 9.7 depicts a tree of a corporation's top management. The tree in Figure 9.8 shows a partial hierarchical chart for teams in the National Hockey League (NHL) during the 20022003 season. To qualify as a tree, a connected graph must fulfill path and edge requirements. First we present the path requirement.
612
Chapter 9 Trees
Figure 9.7
Chief Executive Officer
l [President[
I
I Vice President Finance
Vice President Manufacturing
I Vice President Marketing
Vice President Research and Development
Figure 9.8 NHL
Eastern Conference
Atlantic
Devils
Northeast
Southeast
Western Conference
Central
Northwest
Pacific
San ,Jose
~
A connected graph is a tree if and only if there is a unique, simple path between any two vertices.
PROOF: Let G be a tree, and v and w any two of its vertices. Since G is connected, by Theorem 8.3, a simple path m u s t run between them. If there are two distinct simple paths between them, then one path followed by the other in reverse order would form a cycle. This, however, is impossible since G is a tree; so G contains a unique, simple path between v and w. Conversely, let G be a graph with a unique, simple path between any two vertices. Clearly, G is connected. Suppose G contains a cycle, and v and w are two vertices in it. Then the cycle can be split into two distinct simple paths between v and w, a contradiction, so G is acyclic. Being connected and acyclic, G is a tree. m You may verify t h a t the graph in Figure 9.2 is connected and contains a unique, simple path between any two vertices. Therefore, by Theorem 9.1, it is a tree. The same holds for Figure 9.3. Next we establish the edge r e q u i r e m e n t for a connected graph to be a tree.
9.1 Trees
613
A connected g r a p h w i t h n vertices is a tree if a n d only if it has exactly n  1 edges.
PROOF: F i r s t we prove by s t r o n g induction t h a t a tree w i t h n vertices has n  1 edges. Let P(n)" A tree T with n vertices has n  1 edges. B a s i s s t e p Suppose T contains one vertex. Since T is acyclic, it is loopfree; consequently, T contains no edges and P(1) is true. I n d u c t i o n s t e p Suppose t h e r e s u l t is t r u e for every t r e e with k or fewer vertices. Let T be a tree with k + 1 vertices and e an edge in T. Deleting e from T yields two connected disjoint graphs, T1 a n d T2. E a c h is a tree. Suppose T1 has p vertices; t h e n T2 has q  k + 1  p vertices. By the inductive hypothesis, T1 contains p  1 edges and T2 contains q  1 edges; so T contains (p  1)+ (q  1) + 1  (p  1) + (k  p ) + 1  k edges. T h u s by s t r o n g induction, P(n) is t r u e for every n _> 1. Conversely, let G be a connected g r a p h with n vertices a n d n  1 edges. If it were not a tree, it would have a cycle. Remove an edge from this cycle. The r e s u l t i n g g r a p h is still connected. If it is not acyclic, r e m o v e an edge from a cycle. C o n t i n u e this p r o c e d u r e to get an acyclic g r a p h H. T h u s H is connected and acyclic; it m u s t be a tree with n vertices. So, by the first part, H has n  1 edges and hence G has more t h a n n  1 edges, which is a contradiction. T h u s G is connected and acyclic, and hence a tree. m For example, the g r a p h in F i g u r e 9.2 is connected and has 11 vertices, so it m u s t contain 10 edges to be a tree, which is true.
Exercises 9.1 D e t e r m i n e if each g r a p h is a tree.
l.d/~
2.
i 
//x,
"x
7. Is the g r a p h in Figure 8.4b a tree? If not, why? 8. D e t e r m i n e if the P e t e r s e n g r a p h in Figure 8.28 is a tree. If it isn't, explain why.
614
Chapter 9 Trees
Let n denote t h e n u m b e r of vertices of a t r e e a n d e t h e n u m b e r of edges. Verify t h a t e  n  1 for each tree.
I I I I
/A
12. How m a n y bonds does the h y d r o c a r b o n molecule CnH2n+2 have? A s s u m e a carbon molecule has degree four. n
13. Let T be a tree with vertices v l , . . . , Vn. Show t h a t ~ deg ( v i )  2 n  2. i=1
14. For w h a t values of n is K n a tree? D e t e r m i n e if each complete b i p a r t i t e g r a p h is a tree. 15. K1,2
16. K1,3
17. K2,2
18. K2,3
19. For w h a t values of m and n is K m , n a tree? 20. Let G be an r  r e g u l a r tree with n vertices. Prove t h a t n = 1 or n = 2. D r a w all n o n i s o m o r p h i c trees with the given n u m b e r of vertices n.
21. 2
22. 3
23. 4
24. 5
25. The e c c e n t r i c i t y of a vertex v in a tree is the length of the longest simple p a t h from v. Find the eccentricity of each vertex of t h e t r e e in Figure 9.9.
F i g u r e 9.9
e c
a
h
f
d
i
26. T h e c e n t e r of a tree is a vertex w i t h t h e least eccentricity. F i n d t h e center(s) of the tree in F i g u r e 9.9. 27. U s i n g the adjacency m a t r i x of a connected g r a p h with n vertices, w r i t e an a l g o r i t h m to d e t e r m i n e if it is a tree.
All connected g r a p h s have trees t h a t span t h e m . We will discuss t h r e e ways to find t h e m , as well as solve the 4queens problem s t a t e d at the
9.2 Spanningtrees
615
beginning of the chapter, but first a more pressing practical job needs to be done. A county's five towns, A through E, are connected by roads (see Figure 9.10), but 2 feet of snow cover them. The county would like to plow as few roads as possible, so one can travel between the towns. Figure 9.11 displays one possible solution.
F i g u r e 9.10 Graph G.
C
B D A
E
F i g u r e 9.11 Graph H.
C
B A
D
You may notice that the graph H in Figure 9.11 is a subgraph of G. In fact, it is a tree containing every vertex of G. It is called a spanning tree of G.
Spanning Tree A subgraph H of a connected graph G is a s p a n n i n g t r e e of G if H is a tree containing every vertex of G. Using this definition, we can solve the snowplowing problem by finding a spanning tree for the graph G. Two additional spanning trees are given in Figure 9.12, indicating that the spanning tree of a graph need not be unique. C
B
B
F i g u r e 9.12 o
A
E
o
E
Does every connected graph have a spanning tree? Theorem 9.3 affirms that it does.
616
Chapter 9 Trees
J o s e p h B e r n a r d K r u s k a l (1928) was born in New York City. He graduated from the University of Chicago in 1948 and received his Ph.D. from Princeton in 1954. After being an instructor at Princeton and at the University of Wisconsin, he became an assistant professor at the University of Michigan in 1958. The following year he joined the technical staff at Bell Telephone Labs, a position he still holds. Kruskal has served as visiting professor at Yale, Columbia, and Rutgers.
Every connected graph has a spanning tree.
PROOF: Let G be a connected graph. If G is a tree, we are done. If G is not a tree, it must contain a cycle. Remove an edge from the cycle. The new graph is still connected. If it is acyclic, then it is a tree and hence a spanning tree. Otherwise, it m u s t have a n o t h e r cycle. Remove an edge from this cycle. Continue this procedure until a subgraph H is acyclic. Since H is both connected and acyclic, it is a tree. H also contains every vertex of G, so it is a spanning tree of G. I Three algorithms to find a spanning tree in a connected graph are given below: Kruskal's, the DepthFirst Search, and the BreadthFirst Search. Kruskal's algorithm represents a special case of an algorithm for a minimal spanning tree that we will examine in the next section. It was developed by the American m a t h e m a t i c i a n Joseph Bernard Kruskal in 1956.
Kruskars Algorithm for a Spanning Tree Although the proof of Theorem 9.3 can find a spanning tree, Kruskal's algorithm takes advantage of Theorem 9.2. It is given in Algorithm 9.1.
Algorithm Spanning Tree (G,T) (* G = (V, E) is a connected graph with n v e r t i c e s and T denotes a spanning t r e e . Numedges denotes the number of edges selected. Begin (* algorithm *) O~ T 1; (2) T n  1 is a s p a n n i n g tree; and (3) Tk is contained in a minimal s p a n n i n g tree. If the tree Tk is contained in a m i n i m a l s p a n n i n g tree at the end of every iteration, it m u s t be t r u e even w h e n the loop is t e r m i n a t e d . C o n s e q u e n t l y , w h e n we leave the loop, a minimal s p a n n i n g tree m u s t result. So we begin with the proof of p a r t 1. 9 To prove t h a t Tkis a tree for every k >__1 (use induction)"
Basis step tree.
When k 
1, T1 consists of a single vertex. So T1 is a
I n d u c t i o n s t e p A s s u m e Tk is a tree for any k >_ 1. In the (k + 1)st iteration, w h e n a new edge is added to Tk the r e s u l t i n g s u b g r a p h Tk+l is still connected and acyclic, so Tk+l is also a tree. Thus, by induction, Tk is a tree for every k >_ 1. 9 To prove that T,~_ 1is a s p a n n i n g tree: The w h i l e loop is t e r m i n a t e d w h e n k  n  1. The r e s u l t i n g s u b g r a p h T,,_ 1, by part 1, is a tree with n  1 edges; so it contains all the vertices of G. Hence T,~_ 1 is a s p a n n i n g tree. 9 To prove that Tk is c o n t a i n e d induction)"
in a m i n i m a l
spanning
tree (use
B a s i s s t e p Since Tx consists of a single vertex, T1 is contained in every minimal s p a n n i n g tree. I n d u c t i o n s t e p A s s u m e Tk is contained in a minimal s p a n n i n g tree T after k iterations of the loop. We would like to show t h a t Th+l is c o n t a i n e d in some minimal s p a n n i n g tree of G. Let V b e the set of vertices in Tk. In the (k + 1)st iteration, the a l g o r i t h m selects an edge e  {u, v} of least weight, where u c V and v r V, and adds it to Tk. This yields the tree Tk+l" Tk+l  Tk U {e}.
Case 1
If e c T, clearly Tk+l is contained in T.
Case 2 If e r T, T u {e} contains a cycle C. Choose an edge e'  {x,y} in C such t h a t x c V , y r V, and e r e'. T h e n w(e) l. m Kruskal's and Prim's algorithms present fine paradigms of a greedy algorithm. A g r e e d y a l g o r i t h m makes an optimal choice at every step. For instance, each step in the two algorithms sought an edge with minimum weight. Although greedy algorithms may not always yield optimal solutions, they do in these two cases.
Exercises 9.3
Using Kruskal's algorithm, construct a minimal spanning tree for each connected weighted graph. 1. b
5
2.
2 a~
a
3
5"
 ~d
d 4.
0
a
c
~ e 2 f 7
8
7
,d d
0
.
b
eY
c 8
g
6
712. Using Prim's algorithm, construct a minimal spanning tree for each graph in Exercises 16. Compute the weight of each. Using Kruskal's algorithm, create a minimal spanning tree for the connected weighted graph with each adjacency list.
634
Chapter 9 Trees 13.
4 5 6
12141~1315~ 12r~i ~13171 ~[616H 1516H
14. I
2_ [11~! ~[3[~! ~[417 H 3_ 11131~1218I~14[9[ ~1514H 4 ! ~11161 ~12171 ~13191~1515H 5_ ~11151/*13141 ~1415H _ _ . . .
1516. Redo Exercises 13 and 14 using Prim's algorithm. Using Kruskal's algorithm, construct a minimal spanning tree for the connected, weighted graph with each modified adjacency matrix. The symbol oc in row i and column j indicates the absence of edge {i, j}; think of it as a number larger than any of the weights in the graph. 17.
18. 1 1 oc 2 i 3 4
2 6 oo 5 7
3 3 5 ~ 6
4 7 7 6 oc
1 1 oc 2 8 3 9 4 5
2 8 oc 11 ~ 3
1920. Redo Exercises 17 and 18 using Prim's algorithm.
3 9 11 oc 10 c~
4 7 oc 10 oc 5
5 7 3 5
9.4 RootedTrees
635
T a k e a good look at the trees in F i g u r e s 9.6, 9.7, a n d 9.8. E a c h c o n t a i n s a specially d e s i g n a t e d vertex called t h e root: Nicolaus, Chief Executive Officer, and NHL, respectively. A tree w i t h a root is a r o o t e d t r e e . Consider the simple a l g o r i t h m for m a k i n g an overseas t e l e p h o n e call: 9 Dial the i n t e r n a t i o n a l access code (011). 9 Dial the c o u n t r y code. 9 Dial the city code. 9 Dial the local n u m b e r . This a l g o r i t h m can be used to c o n s t r u c t a rooted tree, a portion of which is shown in F i g u r e 9.31. The root of the tree is t h e vertex 011.
F i g u r e 9.31
011
~.
I~
root
A rooted tree. UK 44
London(i)
21 41 232
France 33
1
7
Japan 81
91
93
75
45
11
6
TokyoI3~
Rooted trees are d r a w n with the root at the top, especially in c o m p u t e r science; t h e y grow downward. Every " u n r o o t e d " tree can be r e d r a w n as rooted and each choice of the root produces a differently shaped tree. For example, the tree in Figure 9.2 w h e n r e d r a w n as a rooted tree a p p e a r s in Figure 9.32. The s p a n n i n g tree in Figure 9.26 can be r e d r a w n as differently shaped trees (see F i g u r e 9.33).
F i g u r e 9.32
H
H Since a unique, simple p a t h r u n s b e t w e e n any two vertices in a tree, such a p a t h exists from the root of a rooted tree to a n y o t h e r vertex. The basic t e r m i n o l o g y of rooted trees r e s e m b l e s t h a t of a family tree.
Chapter 9 Trees
636 F i g u r e 9.33
a
b
eV
dd /
~hlc
c/ Tree rooted at a
Tree rooted at b
P a r e n t , Child, Sibling, Ancestor, D e s c e n d a n t , a n d S u b t r e e Let T be a r o o t e d tree w i t h root v0. Let v o  v l from v0 to Vn. Then: "
Vn1
9 Vn
is t h e p a r e n t
. . . . .
VnlVn
be t h e p a t h
of Vn.
is a c h i l d of V n  1 .
9 Vertices w i t h the s a m e p a r e n t are s i b l i n g s . 9 T h e vertices v0, V l , . . . ,v,~_l are a n c e s t o r s 9 The d e s c e n d a n t s ancestor.
ofv,~.
of a vertex v are t h o s e vertices for w h i c h v is a n
9 A v e r t e x w i t h no c h i l d r e n is a l e a f or a t e r m i n a l 9 A v e r t e x t h a t is not a leaf is an i n t e r n a l
vertex.
vertex.
9 T h e s u b t r e e of T rooted at v consists of v, its d e s c e n d a n t s , a n d all edges incident w i t h t h e m . The tree T in F i g u r e 9.34 is rooted at a. V e r t e x b is the p a r e n t of b o t h e a n d f; so e a n d f are t h e c h i l d r e n of b.
F i g u r e 9.34
a
i k
1
Since b, c, a n d d h a v e t h e same p a r e n t , t h e y are siblings. Vertices a, b, and e are a n c e s t o r s of 1. Vertices e, f, k, a n d l are d e s c e n d a n t s of b. V e r t e x f has no children, so it is a leaf. Vertices c a n d e have at least one child, so b o t h are i n t e r n a l vertices. F i g u r e 9.35 displays t h e s u b t r e e s r o o t e d at b a n d d. I Tree s t r u c t u r e s occur not only in genealogy a n d h i e r a r c h i c a l studies, b u t in t h e s t u d y of g a m e s such as chess, checkers, a n d tictactoe. In such
9.4 Rooted Trees
F i g u r e 9.35
637
b
d
k l Subtree rooted atb
Subtree rooted atd
a game, two players take t u r n s for their moves. The root of a g a m e t r e e r e p r e s e n t s the initial board position and every other vertex r e p r e s e n t s a s u b s e q u e n t board position. Figure 9.36, for example, shows a portion of a game tree for tictactoe.
F i g u r e 9.36
i l
il ~ I I
io !!o ,,o
X 0
0
0
!i I Ix
The unique, simple p a t h from the root of a tree to any vertex can define its level and hence its height.
Level and Height The l e v e l of a vertex v in a rooted tree, denoted by level (v), is the length of the p a t h from the root to v. The h e i g h t of a rooted tree is the m a x i m u m level of any leaf in the tree. ~
For the rooted tree in Figure 9.34, level (a)  0, level (b)  level (c) level (d) = 1, level (e) = level (f) = level (j) = 2, and level (k)= level (l) = 3. Since the m a x i m u m level of any leaf is 3, the height of the tree is 3. m The level of a vertex can also be defined recursively.
Chapter 9 Trees
638
A Recursive l)efinition of the Level of a Vertex v level ( v )  [{0 i 1 + level (parent of v)
if v is the root otherwise
Using the recursive definition, c o m p u t e the level of vertex k in F i g u r e 9.34. SOLUTION: level (k) = 1 + level (e) = 1 + [1 + level (b)]  2 + level (b) = 2 + [1 + level (a)]  3 + level (a) =3+0
=3
m
An i m p o r t a n t class of rooted trees is ordered rooted trees.
Ordered Rooted Tree A rooted tree in which the vertices at each level are ordered as the first, second, third, and so on is an o r d e r e d r o o t e d tree. As ordered trees, the rooted trees in Figure 9.37 are not the same (Why?). As u n o r d e r e d trees, however, t h e r e is no difference between them.
F i g u r e 9.37
a
II When an ordered rooted tree is drawn, vertices at the same level are placed along the same horizontal line. Children from oldest to y o u n g e s t are a r r a n g e d from left to right. For example, in Figure 9.37, b < c, whereas in Figure 9.38, c < b.
F i g u r e 9.38
a
Since only ordered rooted trees are discussed from here on, the t e r m rooted tree will imply ordered rooted tree. Rooted trees need not have the same n u m b e r of children at every i n t e r n a l vertex. D e p e n d i n g on the m a x i m u m n u m b e r of children of any vertex, these trees have special names.
9.4 RootedTrees
639
m  a r y Tree A rooted tree is an m  a r y t r e e if every vertex has at most m children. If m = 2, it is a b i n a r y tree; if m  3, it is a t e r n a r y tree. An mary tree is full if every internal vertex has exactly m children. With the rooted trees in Figure 9.39, every internal vertex in T1 has at most two children, so T1 is a binary tree. In fact, it is a full binary tree. Tree T2 F i g u r e 9.39
J / T1 Full binary tree
T3 Ternary tree
T2 Binary tree
is binary, but not full (Why?). Every vertex in T3 has at most three children, so T3 is ternary. It is not, however, a full ternary tree (Why?). m mary trees help analyze the structures of computer programs. (optional) A computer program P consists of two subprograms A1 and A2. A1 has three modules B1, B2, and B3; A2 has two modules B4 and B5. In addition, B1 contains a module C1, B2 contains two modules C2 and C3, B4 contains C4, and B5 contains C5, C6, and C7. The structure of the program forms the ternary tree in Figure 9.40. F i g u r e 9.40
P
A1
C1
C2
C3
A2
/.,. C4
C5
C6
C7
m
mary Tree and Partitions Rooted trees can also find the partitions of a finite set, as the next example illustrates. ~ ~ ] ~ ~ ~
Using an mary tree, find the partitions of the set S = {a, b, c}. SOLUTION: Build a tree with root {a }. Use the remaining elements in S to make sets of subsets until each set becomes a partition of S, as Figure 9.41 shows. For
Chapter 9 Trees
640
{a} ~ ~ ~ ~ . ~ . . . .
F i g u r e 9.41 {a, b}
{a, b, c}
a, t h e n c o m p a r e b a n d c. If b < c, t h e n a < b < c; otherwise, a_ [lg n!l a n d the list will t a k e at least [lg n!l comparisons to sort. A m o r e d o w n  t o  e a r t h application follows.
678
Chapter 9 Trees
Figure 9.114
a?b}
Dr, then Di+ 1  Di(v) = Dr. In either case, Di+l(v) is the weight of a shorter path to v after i + 1 iterations. Therefore, by induction, Di(v) is the weight of a shortest path to any vertex v at the end of i iterations for every i _> 0. Consequently, when the for loop ends, D,,_l(v) gives the weight of a shortest path from the source to any vertex v in D. This establishes the validity of the algorithm. (A similar a r g u m e n t proves the algorithm also yields a shortest path to any vertex v.) II We close this section with an analysis of the algorithm.
An Analysis of Dijkstra's Algorithm The n u m b e r of additions and comparisons measures the computational complexity of Dijkstra's algorithm. The for loop (lines 821) is executed n  1 = ()(n) times. Each iteration takes at most n  1 additions and n  i comparisons: a total of 2 ( n  1) = (~)(n) operations. Thus, by Theorem 4.15, Dijkstra's algorithm in the worst case takes ()(n2) operations to find a shortest path from the source to any vertex in a weighted digraph. (Dijkstra's algorithm is also a greedy algorithm.)
Exercises 10.3 Using the weighted digraph in Figure 10.38, compute the length of each path in Exercises 14.
1. acbe
2. abe
3. ecbe
4. acbeg
724
Chapter 10 Digraphs
F i g u r e 10.38
a~.~
~
~ 4 ~
~ ;
~e
J d
~,,.g
Find the weighted adjacency matrix of each weighted digraph. 5.
c b
6.
b
.Oct
d 8
3
4
a.,
.,e
2
c
Use F i g u r e 1 0 . 3 8 f o r E x e r c i s e s 710.
7. List all possible paths from vertex a to vertex f and their lengths. 8. Find the shortest path from vertex a to vertex f and its length. 9. Redo Exercise 7 with vertices a and g. 10. Redo Exercise 8 with vertices a and g. Using Figure 10.32, compute the length of a shortest path from vertex a to each vertex. 11. Vertex b
12. Vertex c
13. Vertex d
14. Vertex e
1518. Redo Exercises 1114 with Table 10.3. 1922. Using the predecessor array P in Table 10.4, redo Exercises 1114. With Table 10.5, find the shortest distance in the weighted digraph of Figure 10.37 from the source to each vertex. 23. Vertex b
24. Vertex c
25. Vertex d
2628. Use Table 10.5 to find the shortest path from the source to the vertices in Exercises 2325. 2931. Using Figure 10.37, compute the length of the paths in Exercises 2628. Using Dijkstra's algorithm, find a shortest path and its length from vertex a to the other vertices in Exercises 3235.
10.3 WeightedDigraphs
32.
b
8
~"
55
725
~
34.
33.
e
w
f
a
b
c
d
e
f
a
cx~
3
5
c~
cx~
~
b
~
~
~z
2
~z
~
~
c~
3
c~
e
5
~
cx~
~
f
c~
4
~
c~
g
c
35.
a
b
c
d
e
f
g
a
c~
cx~
2
5
o0
c~
c~
b
3
c~
~
~
~
5
c~
4
c~
~
7
c~
c~
e
~
cx~
c~
c~
c~
8
f
c~
6
oc
c~
~z
c~
6
c~
~
cxv
c
Algorithm 10.2, developed by R. W. Floyd in 1962, can also find the weight of a shortest path from the source to any vertex in a weighted digraph.
Algorithm Floyd (D,W) (* D is a connected weighted graph with weighted adjacency m a t r i x W = (Wij)nxn. The algorithm computes the weight of a s h o r t e s t path from the unique source 1 to any other vertex. *) O. Begin (* Floyd *) i. f o r i = i to n do 2. f o r j  I to n do 3. f o r k = I to n do 4. i f Wji + Wik < Wjk then (* a shorter path runs from vertex j to vertex k passing through vertex i *) 5.
Wjk 0, where i r j. R is the reachability matrix of the digraph (page 699).
de Bruijn Sequence 9 A d e B r u i j n s e q u e n c e for a binary alphabet is a 2nbit sequence t h a t contains every nbit word as a subsequence in a cyclic fashion (page 702).
Dag 9 A d a g is a digraph with no directed cycles
(page 707).
9 Dags pictorially represent assignment s t a t e m e n t s and algebraic expressions with repeating subexpressions; they help find the prefix and postfix forms of such expressions (page 707).
Weighted Digraph 9 A
weighted digraph has a weight w for every edge
(page 715).
9 The weight of a directed path is the sum of the weights of the edges along the path (page 715). 9 The weighted where
adjacency matrix of a digraph (V,E) is W = (wij),
WiN = 9 A
(vi, vj) ~ E
oo
if
weight of edge (i, j)
otherwise
(page 716).
shortest path from u to v in a weighted digraph weighs the least (page 717 ).
9 Dijkstra's algorithm finds a shortest path and its length from the source to any vertex in a weighted digraph (page 717).
Review Exercises Use the digraph in Figure 10.39 to answer Exercises 17. 1. Find the indegree and outdegree of each vertex. 2. Find the adjacency matrix.
728
Chapter I0 Digraphs
F i g u r e 10.39
2 1
5 3. Is vertex 3 reachable from vertex 5? 4. Is the digraph strongly connected? 5. Find the reachability matrix R of the digraph. 6. Is every entry of R positive, except those on the main diagonal? 7. Is the digraph weakly connected? In 1953, E. N. Gilbert of Bell Telephone Laboratories developed a method for constructing a de Bruijn sequence for nonzero binary ntuples: Start with any nonzero nbit word x. If the first two digits in x are the same, then append a 0 to it; otherwise, append a 1. Drop the first digit in the new word to produce the next nbit word. Using this technique successively, produce all nonzero nbit words from each word. 8. 01
10. 1011
9. 010
Using Gilbert's algorithm, check if all nonzero binary quintuples can be generated beginning with the given word. 11. 01011
12. 11111
13. Using Exercises 11 and 12, determine if Gilbert's algorithm works for n5.
14. List all possible ternary couplets. 15. Construct a de Bruijn sequence for ternary couplets. (There are 24 such choices.) 16. List all possible 4ary couplets. 17. Construct a de Bruijn sequence for 4ary couplets. (There are 16 such couplets.) F i g u r e 10.40
c
d
b
e
a
f
Chapter Summary
729
The digraph in Figure 10.40 shows the results from a roundrobin tournament of six players, a through f. Find each. 18. The number of wins by player e. 19. The number of losses by player f. 20. The champion of the tournament. 21. The secondbest player. 2223. Are the digraphs in Figures 10.39 and 10.40 dags? 24. Represent the courses given by Table 7.1 in a dag. Represent each assignment statement and algebraic expression in a dag. 25. x ~ ( x + y ) + ( y + z )
26. x ~ [ x + ( x + y ) ] , z
27. [(a 9 b) 1' c] + [(b  a) + d]
28. {[(a 9 b) + c] + d} ~ [c/(b + a)]
Evaluate the arithmetic expression represented by each dag. 29.
30.
+
/

3
/x,a
+
,
,
4
3134. Find the postfix expressions represented by the dags in Exercises 273O. 3538. Find the prefix expressions represented by the dags in Exercises 2730. 3940. Find the infix expressions represented by the dags in Exercises 29 and 30. Provide parentheses as needed.
Use the weighted digraph in Figure 10.41 to answer Exercises 4146. Compute the length of each path. 41. abed
42. adgh
43. acfe
44. acegh
45. Find the shortest path from vertex a to vertex g. 46. Find the shortest path from vertex e to vertex h. 4748. With Dijkstra's algorithm, find a shortest path from vertex a to vertex g and its length in the weighted digraphs in Figures 10.4210.43.
730
Chapter I0 Digraphs
Figure 10.41 2 a
~1
5 b
h
3
7 6
Figure 10.42
g
C
10
3
g 5 e
Figure 10.43
~ a ~
r 3
f
g
Supplementary Exercises 1. Find the s u m of the indegrees and outdegrees of a vertex in a t o u r n a m e n t with n vertices. 2. Find the m a x i m u m n u m b e r of edges in a digraph w i t h n vertices and no parallel edges. 3. Find the m a x i m u m n u m b e r of edges in a loopfree d i g r a p h with n vertices and no parallel edges. 4. Identify the u n d e r l y i n g g r a p h of a t o u r n a m e n t w i t h n vertices. 5. How m a n y edges are in a t o u r n a m e n t ? 6. How m a n y different t o u r n a m e n t s (simple digraphs) w i t h n vertices are there?
Chapter Summary
731
*7. Prove t h a t every t o u r n a m e n t has a H a m i l t o n i a n path, t h a t is, a simple path containing every vertex. 8. Let V be a set of n elements. Let R be an irreflexive binary relation on V such t h a t either aRb or bRa, but not both, for every a, b E V. Characterize the digraph of R. 9. A r o o t in a dag D is a vertex v such t h a t every vertex (r v) is reachable from v. Which dags in Figure 10.13 are rooted? Give a Hamiltonian path in each digraph, if it exists. 10. Figure 10.4
11. Figure 10.5
12. Figure 10.6
13. D1 in Figure 10.13
A graph G is strongly o r i e n t a b l e if a direction can be assigned to each edge in such a way t h a t the resulting digraph is strongly connected. Such an assignment of directions is a s t r o n g o r i e n t a t i o n of G. The graphs in Figure 10.44 show the twoway streets in three towns. D e t e r m i n e if each is strongly orientable; when yes, give a strong orientation. ;
16.
/%
"17. Using Exercises 1416, predict a condition for a graph to be strongly orientable.
Computer Exercises Let n be a positive integer 5 20. Write a program to perform each task. 1. Read in the adjacency matrix for a roundrobin t o u r n a m e n t of n players, 1 t h r o u g h n. Determine the champion and the secondbest t e a m of the tournament. 2. Read in the adjacency matrix of a digraph with n vertices, 1 t h r o u g h n. 9 Is v e r t e x j reachable from vertex i, where 1 < i , j < n, i # j ? 9 Find the reachability matrix of the digraph. 9 Is the digraph strongly connected? 3. Read in the weighted adjacency matrix of a weighted digraph with n vertices, i through n. Use Dijkstra's algorithm to compute the length of
732
ChapterI0 Digraphs a shortest path from vertex I to vertex n and print the corresponding path.
Exploratory Writing Projects Using library and Internet resources, write a team report on each of the following in your own words. Provide a welldocumented bibliography. 1. Investigate the various applications of digraphs to the various disciplines. 2. Explain the Program Evaluation and Review Technique (PERT) for scheduling the various tasks in a large project. 3. Examine the teleprinter's problem.
E n r i c h m e n t Readings 1. A. V. Aho et al., Data Structures and Algorithms, AddisonWesley, Reading, MA, 1983, pp. 198229. 2. R. K. Ahuja et al., Network Flows, PrenticeHall, Englewood Cliffs, NJ, 1993. 3. J. A. Bondy and U. S. R. Murty, Graph Theory with Applications, Elsevier, New York, 1976. 4. T. H. Cormen, Introduction to Algorithms, McGrawHill, New York, 1990. 5. L. Ford and D. Fulkerson, Flows and Networks, Princeton University Press, Princeton, NJ, 1962. 6. R. P. Grimaldi, Discrete and Combinatorial Mathematics: An Applied Introduction, 4th ed., AddisonWesley, Reading, MA, 1999, pp. 324480. 7. E. Horowitz and S. Sahni, Fundamentals of Data Structures, Computer Science Press, Potomac, MD, 1976, pp. 301334. 8. S. Sahni, Concepts in Discrete Mathematics, 2nd ed., Camelot, Fridley, MN, 1985, pp. 379397. 9. R. J. Wilson and J. J. Watkins, Graphs: An Introductory Approach, Wiley, New York, 1990, pp. 80111.
Chapter 11
Formal Languages and F i n i t e  S t a t e Machines Time as he grows old teaches many lessons. B
AESCHYLUS
T
he study of finitestate machines began with the neural networks investigations of Warren S. McCulloch and Walter Pitts in 1943. Today paradigms of finitestate constructs can be seen everywhere: turnstiles, traffic signal controllers, automated teller machines, automated telephone service, garage door openers, household appliances, and coinoperated machines such as vending machines and slot machines. Finitestate machines significantly assist the study of formal languages; for example, a machine can be designed (or a program developed) that determines if an input string over the alphabet {a,b} contains abba as a substring. (The string babaabbaba does, while abaaba does not.) This type of machine produces no output, but instead tells whether or not the input string has a certain property. (Example 11.26 explores this.) Some machines, however, produce output values. For instance, adding two binary numbers requires the input of two numbers, and yields their sum as the output (see Example 11.42). Such a machine, a finitestate automaton, is described in Section 11.4. Since all finitestate automata must recognize particular languages, formal languages and types of grammars become important. This chapter explores formal languages, and how automata and formal languages are related, as well as other interesting questions such as: 9 How do we determine if a string of characters contains a certain substring? 9 How do we simulate an automatic teller machine?
733
734
Chapter 11 FormalLanguages and FiniteState Machines
9 Can we develop a p r o g r a m t h a t accepts two b i n a r y n u m b e r s , a d d s t h e m bit by bit, and o u t p u t s t h e i r sum? 9 W h a t sort of l a n g u a g e s are accepted by finitestate a u t o m a t a ?
We now continue our s t u d y of formal languages, b e g u n in Section 2.1. The l a n g u a g e of sets plays an i m p o r t a n t role in the study, as we saw in C h a p t e r 2. You m a y recall t h a t an a l p h a b e t E is a finite set of symbols; a n d a word (or string) over E is a finite sequence of symbols from E. How do we d e t e r m i n e w h e t h e r or not two words over E are equal? To this end, we m a k e the following definition.
Equality of Words Two words x = X l X 2 . . . x m a n d y = y l y 2 . . . y , , over Z are e q u a l , d e n o t e d by x = y, if m = n and xi = Yi for every i. T h u s two words are equal if t h e y contain the same n u m b e r of symbols and the c o r r e s p o n d i n g s y m b o l s are the same. For example, if 0 l z = xyO, t h e n x = 0, y = 1, and z = 0. Also, 011 4= 001. The length of a word w is the n u m b e r of symbols in it. A word of l e n g t h zero is the e m p t y word, denoted by the lowercase G r e e k l e t t e r ~; it c o n t a i n s no symbols. Again recall t h a t E* denotes the set of words over E. (E* can be defined recursively. See Exercise 23.) A l a n g u a g e over E is a s u b s e t of Z*; it m a y be finite or infinite. Let E  { x , y , z , + ,  , . , / , 1",(,)}, w h e r e 9 denotes m u l t i p l i c a t i o n a n d 1" denotes exponentiation. Define a l a n g u a g e L over E r e c u r s i v e l y as follows: 9 x,y,z
e L.
9 I f u and v are in L, t h e n so are ( + u ) , (  u ) , (u + v), (u  v), (u , v ) , (u/v), a n d (u 1" v). T h e n L consists of all fully and legally p a r e n t h e s i z e d algebraic expressions in x, y, and z. For instance, ((((x 9 (y t z))  (y 9 z)) + x) 1" z) is a fully p a r e n t h e s i z e d and wellformed algebraic expression. Note t h a t E* c o n t a i n s nonsensical expressions such as ) + x (/y* 1" also. II (optional) Let E  {_, $ , a , . . . ,z, 0 , . . . , 9, }. Define t h e l a n g u a g e L of legal identifiers in J a v a recursively.
11.! FormalLanguages
735
SOLUTION: An identifier in J a v a begins with a letter, underscore (_), or the dollar sign ($), followed by any n u m b e r of alphanumeric characters (letters or digits). (See the syntax diagram in Figure 11.1.) A letter, an underscore, or $ by itself is a valid identifier. It can also be followed by a letter or a digit; t h a t is, if x ~ L and y ~ E, then xy ~ L. Thus the language L can be defined recursively as follows: 9 _ (underscore), $, and every letter of the English alphabet are in L.
F i g u r e 11.1
9 If x ~ L a n d y e
E, t h e n x y ~ L .
~
_~~
letter underscore
~~ letter
$
digit
m
(optional) The alphabet E for Java on a computer system that uses the ASCII character set consists of the blank character, the upper and lowercase letters, digits, arithmetic and relational operators, special characters, and control characters. So Java is a subset of Z*, consisting of all words over E that are recognizable by a Java compiler, m Since both ~ and {s } are subsets of E*, both are languages by definition. The language ~ is the e m p t y l a n g u a g e . The language {~ } is denoted by the upper case Greek letter A. We emphasize t h a t ~ r A, since J~J = 0, whereas IAI  1. However, if E  ~, E*  A. Why?
Suppose an alphabet E contains at least one element a. Then L = {a, aa, aaa,...} is an infinite language over E. Since L _ E*, E* is also infinite. Thus, i f E r ~, E* contains infinitely m a n y words, each of finite length (see Exercise 29). .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Let z be the concatenation of the words x and y; t h a t is, z = xy. Then x is a p r e f i x of z and y a suffix of z. For instance, consider the word z = readability over the English alphabet; x  read is a prefix ofz a n d y = ability is a suffix ofz. Since x  )~x = x~, every word is a prefix and a suffix of itself; further, ~ is both a prefix and a suffix of every word. The operations of union and intersection can be applied to languages also; after all, languages are also sets. To this end, we extend the definition of concatenation of strings to languages.
Chapter 11 FormalLanguages and FiniteState Machines
736
Concatenation of Languages Let A and B be any two languages over E. The c o n c a t e n a t i o n of A and B, denoted by AB, is the set of all words ab with a 9 A and b 9 B. T h a t is, A B = {ab l a 9
9
The next two examples illustrate this definition. ~
Let E  {0, 1},A = {0, 01}, and B 
{X, 1,110}. Find the concatenations
AB and B A . SOLUTION: 9 AB consists of strings of the form ab with a 9 A and b 9 B. So AB = {0~, 01, 0110, 01)~, 011, 01110} = {0, 01, 0110, 01,011, 01110} = {0, 01,011, 0110, 01110} BA
{ba ] b 9 B A a 9
= {~0, ~01, 10, 101, 1100, 11001}
II
 {0, 01, 10, 101, 1100, 11001}
Tree diagrams are useful in finding the various strings in the concatenation of two finite languages. Figure 11.2, for example, shows the different ways of obtaining the elements in AB for the languages in Example 11.4.
F i g u r e 11.2
strings in A
strings in B
~
0
~
0
1
strings in AB
;L 1 110
~ ~, ~
0~. 01 0110 01~
/
'
~
/ 1
1 1
'
0
~
011 01110
Two interesting points arise from this example: (1) AB r B A . (2) Further, IABI = 5 < 6 = 2 . 3 = IAI. IBI; whereas IBAI = 6 3 . 2  IB[IAI. This is so since the word 01 in AB can be obtained in two ways. Therefore, all we can say in general is, ifA and B are finite languages, then IABI < IAI. iB].
II.I
Formal Languages
737
(optional) In the p r o g r a m m i n g language QUICKBASIC, a n u m e r i c variable n a m e m u s t begin with a letter followed by either a period or an a l p h a n u m e r i c character. (QUICKBASIC does not distinguish between u p p e r and lowercases in variable names.) Let A = {a, b , . . . ,z} a n d B  { a , . . . , z, 0 , . . . , 9,. }. The concatenation AB gives all n u m e r i c variable n a m e s containing exactly two characters, namely: aa, a b , . . . , a 0 , . . . ,
a9, a.
ba, b b , . . . , b0, . . . , b9, b.
za, zb, . . . , zO, . . . , z9, z.
I
Let A be a language over E. Identify the languages A ~ and AA. SOLUTION"  {abta ~ AAb ~ ~}. Since 0 contains no elements, no concatenations ab can be performed; therefore, A ~  ~. (Similarly,
9 AO
~n  0.)
9 AA =A{)~} = {aZ l a E A}  { a l a c A}  A .
(Similarly, AA  A . )
I
We are now ready to study some properties of the c o n c a t e n a t i o n operation on languages. Let A, B, C, and D be any languages over an a l p h a b e t Z. Then: (1) A ~  0  OA
(2) AA  A
= AA
(3) A ( B C ) = ( A B ) C
(4) A ( B u C) = A B U A C
(5) (B u C ) A  B A u CA
(6) A ( B ~ C) c_ A B n A C
(7) (B A C ) A c B A ~ CA
(8) If A c_ B and C c_ D, t h e n AC c_ BD. PROOF:
We already proved parts i and 2 in Example 11.6. We now shall prove p a r t s 4 and 6, and leave the other p a r t s as exercises. (4) To p r o v e t h a t A ( B u C) = A B u AC: We need to show t h a t ( a ) A ( B U C ) c_ A B u A C
and (b) AB UAC _c A ( B U C ) .
9 To p r o v e t h a t A ( B u C) c_ A B u AC: Let x ~ A ( B u C). T h e n x is of the form yz, where y ~ A and z e B u C. If z ~ B, t h e n y z ~ A B and hence y z ~ A B u A C . If z ~ C, t h e n y z ~ A C and therefore y z E A B u A C . Thus, in b o t h cases x = y z ~ A B u A C . Consequently, A ( B u C) c A B u A C .
Chapter I I
738
Formal Languages and FiniteState Machines
9 To prove that A B u A C c A ( B U C)" L e t x 9 A B u A C . S u p p o s e x 9 AB. T h e n x  ab for s o m e a 9 A a n d b 9 B. Since b 9 B, b also b e l o n g s to B U C. So x  ab 9 A ( B u C). S i m i l a r l y , if x 9 AC, t h e n also x 9 A ( B u C). T h u s in b o t h cases x 9 A ( B U C). C o n s e q u e n t l y , AB u A C c A ( B u C). T h e r e f o r e , by p a r t s (a) a n d (b), A ( B u C)  A B u AC. (6) To prove that A ( B N C) c_ A B n AC" L e t x 9 A(BNC). T h e n x  yz for s o m e e l e m e n t y 9 A a n d z 9 BNC. S i n c e z 9 B n C, z 9 B a n d z 9 C. So yz b e l o n g s to b o t h AB a n d AC, a n d h e n c e yz 9 A B NAC; in o t h e r words, x 9 A B N A C . T h u s A ( B n C) c_ A B N A C . m T h e n e x t e x a m p l e verifies p a r t s (4) a n d (6) of this t h e o r e m . I

I
Let E  {a,b,c}, A (1)A(BuC)ABuAC
{a, ab}, B {b, ab}, a n d C and (2)A(BNC) c_ABNAC.
{)~,bc}. Verify t h a t
SOLUTION:
A B  {ab, aab, abb, abab } A C  {aZ, abc, ab)~, abbc} = {a, ab, abc, abbc} AB u A C  {a, ab, aab, abb, abc, abab, abbc}
A B n A C  {ab, abb } (1) Then
B u C  {s
ab, bc}
A ( B u C)  {aZ, ab, aab, abc, ab)~, abb, abab, abbc} = {a, ab, aab, abb, abc, abab, abbc}
=ABuAC (2) Since B N C  0, A ( B n C)  0 a n d h e n c e A(B n C) c A B a AC.
m
If t h e l a n g u a g e s A a n d B a r e t h e s a m e , t h e n AB is often d e n o t e d by A 2. T h u s A 2 consists of w o r d s o b t a i n e d by c o n c a t e n a t i n g each w o r d in A w i t h every w o r d in A: A 2  {xy Ix,y 9 A}. M o r e generally, let n 9 N. T h e n A n consists of all words o b t a i n e d by n  1 c o n c a t e n a t i o n s of w o r d s in A. In p a r t i c u l a r , Z" d e n o t e s t h e set of w o r d s o b t a i n e d by n  1 c o n c a t e n a t i o n s of s y m b o l s in Z, t h a t is, w o r d s of l e n g t h n. Let E  {a, b, c}, A  {a, ab, bc}, a n d B = {a, bc}. F i n d
]E 2, A 2, a n d B 3.
SOLUTION: 9 E 2 = { x y l x , y 9 E} = {aa, ab, ac, ba, bb, bc, ca, cb, cc}
9 A 2 = {xy I x,y 9 A} = {aa, aab, abc, aba, abab, abbc, bca, bcab, bcbc} 9 B 2 = {aa, abc, bca, bcbc} So
B 3 = {aaa, aabc, abca, abcbc, bcaa, bcabc, bcbca, bcbcbc }
m
1I.I
., . ,,, .
FormalLanguages
739
:,:. ; ; ,
Stephen Cole Kleene (19091994) was born in Hartford, Connecticut. His father was an economics professor and his mother, a poet. In 1930, he graduated from Amherst College and 4 years later received his Ph.D. in mathematics from 9 . :t i\ ' Princeton. After teaching for 6 years at the University of Wisconsin, Madison, he joined the faculty at Amherst College for a year. For the next 4 years he served in the U.S. Naval Reserve. In 1946, he returned to the Madison campus and in 1964 became the Cyrus C. MacDuffee Professor of Mathematics and Computer .~ 9 r / Science. He served as Chairman of the Department of Mathematics, Acting Director of the Mathematics Research Center, and Dean of the College of Letters , ,.~ , and Science. Kleene was awarded an honorary Doctor of Science by Amherst College in 1970, the Steele Prize by the American Mathematical Society in 1983, and the National Medal of Science in 1990. Kleene contributed significantly to the theory of recursive functions and the theory of automata. ,
:.
..,.;%,,
,,.
Note" It follows by p a r t 8 of T h e o r e m 11.1 t h a t i f A _c B, t h e n A 2 c B 2 (Why?). More generally, it can be s h o w n by i n d u c t i o n t h a t ifA c B, t h e n A n c_ B n for every n e N.
Finally, from a n y l a n g u a g e A over E, we can c o n s t r u c t a n e w l a n g u a g e A* using the v a r i o u s powers of A. F i r s t we define A ~ = A.
Kleene Closure Let A be a l a n g u a g e over an a l p h a b e t Z. T h e n A*  u A n is t h e K l e e n e n=0
c l o s u r e of A, in h o n o r of the A m e r i c a n logician S t e p h e n Kleene. A* consists of strings o b t a i n e d by an a r b i t r a r y n u m b e r of c o n c a t e n a t i o n s of w o r d s f r o m A. * is the K l e e n e o p e r a t o r . The following e x a m p l e i l l u s t r a t e s this definition. Let A  {0}, B  {11}, C = {000}, a n d E  {0, 1 }. F i n d t h e i r Kleene closures.
SOLUTION:
CA3
9 S i n c e A = {0}, A n 
{0n}. S o A * 
U An 
{0n ] n > 0}. In o t h e r
n0
words, A* consists of s t r i n g s of zero or m o r e 0's. 9 Since B 
{11} = {12}, B 2 = B B = {1111} = {14}. So B 3  B B
2 =
oo
{12}{14}  {16}. Thus, in general, B n  { 1 2 n } .
T h u s B* 
U Bn = n0
{12n I n > 0}. It consists of w o r d s of l ' s of even length. 9 Since C  {03}, C n  {03n}. So C* = {03n I n > 0}, the set of s t r i n g s of O's whose l e n g t h s are divisible by 3.
Chapter I l
740
FormalLanguages and FiniteState Machines
9 The Kleene closure is the set of all possible words over E, namely, E*. (This explains why we denoted it by E* from the beginning of the section.) m We now t u r n to a few properties satisfied by the Kleene operator. We shall prove one of t h e m and leave the others as exercises. P r o p e r t y 6 is a bit hard to prove, so we omit it; properties 4 and 5 require induction. Let A and B be any languages over an alphabet E. Then" (1) A c A *
(2) A c A *
(3) A ' A *  A *
(4) If A _c B, t h e n A* _ B*.
(5) (A*)*  A *
(6) (A u B)*  (A* u B*)*  ( A ' B * ) *
PROOF.
(3) To p r o v e t h a t A ' A * = A*: 9 T o p r o v e t h a t A * c A ' A * " Since A c A*, A*A c A ' A * by T h e o r e m
11.1. But A*A  A * by Theorem 11.1. So A* c A ' A * m
(11.1)
9 T o p r o v e t h a t A * A * c A*" L e t x ~ A ' A * . T h e n x  y z withy, z ~ A*. Since y , z E A*, y ~ A m and z ~ A n where m, n ~ W. So y z A r e A ''  A m+n. But A m+n c_ A*, so x  y z ~ A*. Thus A ' A * c A*
(11.2)
Thus, by set inclusions ( 11.1 ) and ( 11.2), A'A*  A*.
m
A n i n t e r e s t i n g o b s e r v a t i o n " For any language A, A _c A*. That is, w h e n we apply the Kleene operator * on A, the resulting language A* contains A. However, if we apply * to A*, we find t h a t (A*)*  A*; so we do not get a new language. This explains why A* is called the Kleene closure of A. We conclude this section with an example involving both concatenation and the Kleene operators.
~
Identify each language over E  {a, b}. (1) {a,b}*{b}
(2) {a}{a, b}*
(3) {aI{a, bI*{b} (4) {a, bI*{b}*
SOLUTION" (1) {a, b }* consists of all possible words over E including )~, whereas {b} contains just one word, namely, b. Therefore, the language {a,b }* {b } consists of words over E t h a t have b as a suffix. (2) Similarly, {a} {a, b }* consists of words t h a t have a as a prefix.
I I.I
Formal Languages
741
(3) {a} {a, b}* {b} consists of words t h a t begin with a and end in b. (4) E v e r y e l e m e n t in {b}* consists of a finite n u m b e r of b's. T h e r e f o r e , {a, b}* {b}* consists of strings followed by a finite n u m b e r of b's. Notice t h a t this is different from {a, b}* {b} (Why?). m Exercises II.I In Exercises 14, a language L over E = {a, b} is given. F i n d five w o r d s in each language. 1. L = {x e E* Ix begins with and ends in b.} 2. L = {x e E* Ix contains exactly one b.} 3. L is defined recursively as follows:
(i) ~ e L
4. L is defined recursively as follows: (i) ~, ~ L
(ii) x ~ L ~ x b b ~ L (ii) x ~ L ~
axb ~ L
Define each language L over the given a l p h a b e t recursively. 5. The language L of all p a l i n d r o m e s over E  {a, b}. (A p a l i n d r o m e over E is a word t h a t reads the s a m e both forwards and b a c k w a r d s . For instance, a b b a is a palindrome.) 6. L = {anbn l n E N}, E = {a,b}
7. L = {0,00, 10, 100, 110,0000, 1010,...}, E = {0, 1} 8. L = set of b i n a r y r e p r e s e n t a t i o n s of positive integers, E = {0,1 } 9. L = {1, 11,111, 1111, 11111,...}, E = {0, 1}
10. L = { x e
E* I x = b n a b n, n > O } , E = { a , b }
11. L = set of words over Z = {0, 1} with prefix 00 12. L = set of words over Z = {0, 1} with suffix 11 M a r k each as t r u e or false. 13. E v e r y language over an alphabet is infinite. 14. If E = El, t h e n Z* = E~. 15. C + + is a finite language. 16. Every language is a set. Using E x a m p l e 11.1, d e t e r m i n e if each is a wellformed and fully p a r e n t h e sized a r i t h m e t i c expression. 17. (((x + y ) / ( ( ( x  y )
9 z) t z))
19. (y + (z t (+x))/(x))
18. (x 1" ((Y  x ) ? (z))) 20.
((x
(y t z)) 9 (x + (3' t (+z))))
21. Define the set of words S over an a l p h a b e t Z recursively. ( H i n t : Use concatenation.)
742
(',hapter 11 Formal Languages and FiniteState Machines 22. Define the language L of all b i n a r y r e p r e s e n t a t i o n s of n o n n e g a t i v e integers recursively. 23. Let E be an alphabet. Define E* recursively. (Hint: Use concatenation.) 24. Define recursively the set S of integers acceptable in Java. A r r a n g e the b i n a r y words of each length in increasing order. 25. L e n g t h two.
26. L e n g t h three.
A t e r n a r y w o r d is a word over the a l p h a b e t {0, 1, 2 }. A r r a n g e the t e r n a r y words of each length in increasing order. 27. L e n g t h one
28. L e n g t h two
*29. Let Z be a n o n e m p t y alphabet. Prove t h a t Z* is infinite. (Hint: A s s u m e E* is finite. Since E r O, it contains an e l e m e n t a. Let x ~ Z* with largest length. Now consider xa.) Let A  {a, bc} and B = {k, ab, bc}. F i n d each concatenation. 30. AB
LetA
31. B A {a, a b } , B 
34. A A  A
{a,b, ab}, C 
32. A 2
33. A 3
{c}, a n d D 
{c, bc}. Verify each.
35. A A  A
37. (B u C)A  B A u CA
36. A ( B U C )  A B u A C
38. A ( B N C)  A B N A C
39. (B N C ) A  B A n C A 40. If A c_ B and C c D, then A C c_ BD.
M a r k each as t r u e or false, where A and B are a r b i t r a r y finite languages. 41. A = O 45. A A  A A
42. A O  O
43. A O  O A
46. [ A x B I  I B x A [
44. AAA
47. ] A B I  I B A I
Find t h r e e words belonging to each language over ~  10, 11. 48. {0}*
49. {0}{1}*
50. {0}*{1}
51. { 0 } { 1 1 } * { 1 }
52. {0}*{1}*
53. {01}*
54. { 0 } { 0 , 1 } * { 1 }
55. {0}*{1}*{0}*
Prove each, where A, B, and C are a r b i t r a r y languages over Z and x e E. 56. IIxn II  nllxll for every n > 0.
57. If A c_ B, t h e n A n c_ B n for every n > 0. 58. If A Z B, t h e n A* Z B*. 60. OA=D
61. A A  A
59. (A*)*  A*
62. A cA*
11.2 Grammars
743
63. A c A *
64. A(B n C) c A B N A C
65. (B U C)A = BA u CA
66. (B N C)A c B A N CA
67. (A'B*)*  (B'A*)*
68. (A* u B*)* = (A u B)*
Words in a natural language such as English or French can be combined in several ways. Some combinations form valid sentences, while others do not. The g r a m m a r of a language is a set of rules that determines whether or not a sentence is considered valid. For instance, The milk drinks child quickly, although meaningless, is a perfectly legal sentence. The sentences in a language may be nonsensical, but must obey the grammar. Our discussion deals with only the s y n t a x of sentences (the way words are combined), and not with the s e m a n t i c s of sentences (meaning). Although listing the rules that govern a natural language such as English is extremely complex, specifying the rules for subsets of English is certainly possible. The next example introduces such a language. ~ ~ ~ ] ~
The sentence The child drinks milk quickly, has two parts: a subject, The child, and a predicate drinks milk quickly. The subject consists of the definite article The and the noun child. The predicate, on the other hand, consists of the verb drinks and the object phrase milk quickly; the object phrase in turn has the object milk and the adverb quickly. This structure of the sentence can appear as a sequence of trees (Figures 11.311.7), with the d e r i v a t i o n t r e e of the sentence in Figure 11.7.
F i g u r e 11.3
sentence subject
predicate
F i g u r e 11.4
sentence subject article
predicate
noun
The derivation tree exhibits certain characteristics: 9 Each leaf represents a word, a t e r m i n a l symbol. The set of terminal symbols is T  {the, child, drinks, milk, quickly}.
744
ChapterII FormalLanguages and FiniteState Machines
F i g u r e 11.5
sentence subject
predicate
article
noun
I
child
I
The F i g u r e 11.6
sentence subject article
I
predicate
noun
I
The
child
F i g u r e 11.7
verbl drinks
o b ~ objict
adverbl
noun
quickly
sentence subject
predicate
article
noun
verbl
The
child
drinks
o b ~ objict noun
adverb quickly
I
milk 9 Each internal vertex represents a grammatical class, a n o n t e r m i n a l . The set of nonterminals is N = {sentence, subject, predicate, article, noun, object phrase, object, verb, adverb}. A nonterminal symbol is enclosed within a n g l e b r a c k e t s , ( and >. For instance, the nonterminal "subject" is denoted by (subject>. 9 The root of the tree represents the nonterminal symbol (sentence> called the s t a r t symbol, denoted by a. Certain rules can generate the above sentence. Every rule, called a prod u c t i o n rule or a s u b s t i t u t i o n rule, is of the form w ~ w' where w ~ N
11.2 Grammars
745
and w' may be a terminal symbol, a nonterminal symbol, or a combination of both. The production rules of the above sentence are: (sentence) ~ (subject)(predicate) (subject) ~ (article)(noun) (article) ~ the (noun) ~ child (noun) ~ milk {predicate) * {verb){object phrase) (verb) * drinks (object phrase) ~ (object)(adverb) (object) ~ (noun) (adverb) ~ quickly
I
The production rules specify the arrangement of words in a sentence: the s y n t a x of the language. They produce syntactically correct sentences (which can be meaningless). For instance, the sentence, The milk drinks child quickly makes no sense but is syntactically valid. Figure 11.8 shows the derivation tree of this sentence. F i g u r e 11.8
(subject} (articie) the
(niun)
(veib)
milk
drinks
~
)
(object)
I
(niun)
(adverb)
I
quickly
child Determining whether a program is syntactically correct is of the utmost importance in computer science. Before executing a program, the compiler checks the syntax of each sentence (or expression) by constructing derivation trees. (This process is p a r s i n g , and the corresponding derivation tree is a p a r s e tree.) We now turn to present the definition of a phrasestructure grammar.
746
Chapter II
Formal Languages and FiniteState Machines
PhraseStructure Grammar A p h r a s e  s t r u c t u r e g r a m m a r (or simply a g r a m m a r ) features: 9 A finite set N of n o n t e r m i n a l
G b e a r s four
symbols;
9 A finite set T of t e r m i n a l s y m b o l s , w h e r e N n T = ~ ; 9 A finite subset P of [(N u T)*  T*] x (N u T)*; each e l e m e n t of P is called a p r o d u c t i o n ; 9 A s t a r t s y m b o l ~ belonging to N; The g r a m m a r G is d e n o t e d by G = (N, T, P, a).
These features m e e t c e r t a i n r e q u i r e m e n t s : 9 The s t a r t symbol a is n o n t e r m i n a l . 9 No symbol can be b o t h t e r m i n a l and n o n t e r m i n a l . 9 E v e r y p r o d u c t i o n has at least one n o n t e r m i n a l symbol on its LHS, because P __  ( N u T)*  T*] x (N u T)*. Also, P is a b i n a r y relation from (N u T)*  T* to (N u T)*. 9 If (w,w') e P, we t h e n write w ~ w'; since w e (N u T)*  T*, w contains at least one n o n t e r m i n a l symbol; b u t w' e (N u T)*; so it m a y contain t e r m i n a l symbols, n o n t e r m i n a l s , or both.
G r a m m a r s not only produce n a t u r a l languages, b u t also formal ones, as the next two examples d e m o n s t r a t e .
a}. LetN{A,B,c~}, T {a,b}, a n d P {a * aA, A ~ bA, A T h e n G  (N, T, P, a) is a g r a m m a r . Notice t h a t the p r o d u c t i o n A ~ bA is recursive. I Let N = {A,a}, T {a,b}, and P {a * a a , a . A a , A ~ b}. T h e n G  (N, T, P, a) is a g r a m m a r . Again notice t h a t the p r o d u c t i o n a ~ a a is recursive. I Next we define the language g e n e r a t e d by a g r a m m a r .
Derivation and Language Let G = (N, T, P, a) be a g r a m m a r . If w = x a y and w' = xfly are any two words in (N u T)*, and if t h e r e exists a p r o d u c t i o n a ~ fl, t h e n the word w' is said to be d i r e c t l y d e r i v a b l e from w; we t h e n w r i t e w .~ w'. If t h e r e is a finite sequence of words w0, w l , . . . , Wn in (N U T)* such t h a t
11.2 Grammars
747
Wl, Wl ~ W2, . . . , W n  1 ~ W n , t h e n Wn is d e r i v a b l e from w0. The finite sequence of steps, wo ~ Wl   ~ . . . ~ Wn, is a d e r i v a t i o n of Wn from w0. The set of words in T* derivable from a by G is the l a n g u a g e g e n e r a t e d by G, denoted by L ( G ) . The next two examples illustrate these definitions. WO ~
Identify the language L ( G ) g e n e r a t e d by the g r a m m a r in E x a m p l e 11.12. SOLUTION:
Since the g r a m m a r contains exactly one production involving ~, namely, ~ a A , s t a r t with it to find every word in the language. Now select the next production: A ~ b A or A ~ a. The production A ~ a produces exactly one word, a. A ~ b A chosen n times, produces ~ == : a A abA ~ ab2A ~ ... ~ a b n A . Now A ~ a yields the word a b n a and, when n  0, this yields a ~ a = aa. (Note: b ~ = ;~, the null word.) E v e r y word derivable from c~ fits the form abna, where n > 0. In o t h e r words, L ( G ) = {abna l n >_ 0}. m Example 11.14 illustrates t h a t a g r a m m a r G can d e t e r m i n e if it g e n e r a t e s a string in the language L ( G ) . With some difficulty, the language could be described. Again with some difficulty, and a lot of patience and practice, a g r a m m a r G t h a t generates a given language can be found, as Example 11.15 d e m o n s t r a t e s . Define a g r a m m a r G {anb n I n > 1}.
(N,T,P,~)
t h a t generates the language L =
SOLUTION:
Since every word in L m u s t contain the same n u m b e r of a's and b's, G m u s t contain a production of the form c~ ~ aAb. Consequently, to produce a new word from aAb containing the same n u m b e r of a's and b's requires a n o t h e r production A ~ aAb. F r o m these two productions, we can derive all strings of the form an Ab n (Verify this.). All t h a t r e m a i n s to be done to define the g r a m m a r is the production A ~ ~ to t e r m i n a t e the recursive procedure. Thus, N = {a, A}, T = {a, b, ~}, and P = {a ~ aAb, A ~ aAb, A~}. m The first two production rules in this example look quite similar, except for the s t a r t symbol, and can be combined into a single production, ~ ~ ac~b. The production rules ~ * aAb and A ~ ~ can yield the word ab, so the third production is ~ ~ ab. T h u s P ' = {a ~ aab, a ~ ab} is an additional production set t h a t yields the same language. In other words, the g r a m m a r s G = (N, T, P, of) and G' = {N', T', P', ~) g e n e r a t e the same language L, where N' = {a} and T' = {a, b}. T h u s L ( G ) = L ( G ' ) , so the g r a m m a r s G and G' are e q u i v a l e n t . O u r conclusion: T h e g r a m m a r t h a t g e n e r a t e s a l a n g u a g e n e e d n o t be u n i q u e .
Chapter II
748
Formal Languages and FiniteState Machines
J o h n W. B a c k u s (1924) was born in Philadelphia. He received his B.S. and M.S. in mathematics from Columbia University. After joining IBM in 1950, he became instrumental in the development of F O R T R A N and ALGOL (ALGOrithmic Language). He received the W. W. McDowell Award from The Institute of Electrical and Electronics Engineers (IEEE) in 1967, the National Medal of Science in 1975, the A. M. Turing Award from the Association for Computing Machinery in 1977, the Harold Pender Award from the University of Pennsylvania in 1983, and an honorary doctorate from York University, England, in 1985.
9
,J"
P e t e r N a u r (1928), a computer scientist and prolific writer, was born in
. L:" Y,
,
9
,~
~
,
.
.~,'.:,,:~
,
Frederiksberg, Denmark. After receiving his M.A. in astronomy from Copenhagen University in 1949, he spent the next two years at Cambridge University, England, where he used the EDSAC, one of the earliest computers, to pursue astronomy. He received his Ph.D. in astronomy from Copenhagen in 1957. From 1953 to 1959, he consulted for the design of the first Danish computer, the DASK. Beginning around 1964, he became increasingly involved in datalogy (a word he coined), the study of data and data processes. In 1963, Naur was given the Hagemanns Gold Medal and three years later the Rosenhjaer Prize.
BackusNormal Form The most widely used notation for describing the syntax of programming languages is the B a c k u s  N o r m a l F o r m (BNF), developed by John Backus, who described ALGOL 60 with it. Peter Naur edited the ALGOL 60 report, which appeared in 1963, so the BNF notation is also called the BackusNaur Form. In BNF, the production symbol ~ is denoted by ::; thus the production w ~ w' is written as w ::= w'. Production rules with the same LHS are combined by separating their RHS with vertical bars. For instance, the productionsw ~ Wl, w ~ w 2 , . . . , w ~ wn b e c o m e w " '  w l I w 2 1 . . . I wn. (You may read the vertical bar as or.) Nonterminal symbols have angle brackets around them.
11.2 Grammars
•
749
Study the following production rules: (sentence) ~ (subject)(predicate) {subject) ~ {article){noun) ( p r e d i c a t e / ~ (verb/(object/ (object) ~ (article)(noun) (article) ~ a (article} ~ the (noun/~ hare (noun) ~ tortoise (noun)~ race (verb) ~ beats {verb)~ wins BNF shortens these rules: {sentence) ::= (subject) (predicate) (subject/::= (article/(noun/ (predicate) ::= (verb) (object) (object) ::= {article) (noun) (article} ::= a I the ( n o u n / : : = hare l t o r t o i s e t r a c e (verb) ::= beats l w i n s
m
The g r a m m a r for the language of correctly nested parentheses contains one production: (nested p a r e n t h e s e s / : :  ~ [ ((nested parentheses/) where ~ denotes the null string. [Using this definition, you may verify that (()) and ((())) are valid nested parentheses, whereas (() and (())) are not.] m (optional) An integer is a string of digits preceded by an optional sign, + or  . Using BNF, it can be defined as follows: (integer} :: = (signed integer)I (unsigned integer) (signed integer):: = (sign)I (unsigned integer) (sign/:: = + 1 
750
Chapter 11 Formal Languages and FiniteState Machines
""  i :: = O I 1 1 2 1 3 1 4 1 5 1 6 1
71819
For instance, 234, +234, and  2 3 4 are valid integers. Figure 11.9 shows the derivation tree for the integer +234. F i g u r e 11.9
Derivation tree for the integer +234. <signed integer> <sign>
2
2(digit>
23
23
234 The g r a m m a r defined in this example is G = (N, T, P, a), where: 9 N = {, <signed integer>, , <sign>, (digit> }, 9 T = {+,,0,1,2,3,4,5,6,7,8,9}, 9 The production rules are: +Oi 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 i 9
m
11.2 Grammars
751
9 The start symbol a is (integer). Grammars are categorized by the productions that define them.
ContextSensitive, ContextFree, and Regular Grammars Let G = (N, T,P, a) be a grammar. Let A ,B ~ N and ~, ~', fl ~ (N u T)*. Notice that ~, ~', and fl could be the null word. 9 Any phrasestructure g r a m m a r is t y p e 0.
9 G is contextsensitive (or t y p e 1) if every production is of the form ~A~' ~ ~fl~'. 9 G is c o n t e x t  f r e e (or t y p e 2) if every production is of the form A ~ a. 9 G is r e g u l a r (or t y p e 3) if every production is of the form A ~ t or A ~ tB, where t ~ T. In a contextsensitive grammar, fl can replace A in the word c~Ac~' only when A lies between c~ and a'. In a contextfree grammar, the LHS of every production is a single nonterminal symbol A, which c~ can replace. In a regular grammar, the LHS of every production consists of a single nonterminal symbol A and the RHS consists of a terminal symbol t or a terminal symbol t followed by a nonterminal symbol B; t or tB can always replace A. (In tB, the nonterminal must be on the RHS of the terminal symbol t.) A regular g r a m m a r is also contextfree and a contextfree g r a m m a r is also contextsensitive. The Venn diagram in Figure 11.10 shows the C h o m s k y h i e r a r c h y of the various grammars, named in honor of Noam Chomsky, who developed the theory of formal languages.
Figure 11.10 Chomsky hierarchy of grammars.
I
type 3
]
type 2 type 1 type 0
ContextSensitive, ContextFree, and Regular Languages A language L(G) is contextsensitive, contextfree, or regular if the g r a m m a r G is contextsensitive, contextfree, and regular, respectively. The next five examples clarify these definitions.
Chapter I I
752
Formal Languages and FiniteState Machines
( A v r a m ) N o a m C h o m s k y (1928), a linguist, writer, and political activist, was born in Philadelphia, as the son of a Hebrew scholar. At 10 he proofread the manuscript of his father's edition of a 13th century Hebrew grammar. "This backdoor introduction to 'historical linguistics' had considerable impact on his future" (The New York Times Magazine). The young Chomsky, however, was more passionate about politics than about grammar. 9 % '7 On graduating from Central High School in Philadelphia in 1945, Chomsky entered the University of Pennsylvania and received his B.A. in 1949 and M.A. 2 years later. Chomsky received his Ph.D. in linguistics from the University of Pennsylvania in 1955 and joined the faculty at the Massachusetts Institute of Technology. His first book, Syntactic Structures (1957), developed from his notes for an introductory course in linguistics, triggered the Chomskyan revolution in linguistics "by disputing traditional ideas about language development." Chomsky is considered the father of the theory of formal languages. In 1966, Chomsky became the Ferrari P. Ward Professor of Modern Languages and Linguistics. He had been a visiting professor at Columbia, Princeton, and the University of California at Los Angeles and at Berkeley. A recipient of numerous awards and honorary degrees, including the Kyoto prize in Basic Sciences in 1988, Chomsky was named one of the thousand "makers of the twentieth century" by the London Times.
Every production of the g r a m m a r G in Example 11.12 is A ~ t or A ~ tB, so G is a regular grammar. Consequently, L(G) = {abnal n > 0} is a regular language. (See also Example 11.14.) i ~
In Example 11.13, the RHS of the production a ~ Aa contains the t e r m i n a l symbol a on the right of the n o n t e r m i n a l symbol A, so G is not regular. However, since every production appears as w ~ c~ where w ~ N and ~ (N u T)*, G is contextfree; thus L(G) is a contextfree language, m (optional) Not every production of the g r a m m a r G in Example 11.18 is of the form A ~ t or A ~ tB. For instance, the production (unsigned integer) ::= (digit){unsigned integer) is not of either form. The production rules, however, can be rewritten as follows: (integer) ::= +(unsigned integer) {integer) ::=  { u n s i g n e d integer) (unsigned integer) ::= 0(unsigned integer)[... [ 9(unsigned integer) (unsigned i n t e g e r ) : : = 0 [ 1 [ 2 ] 3 1 4 1 5 [ 6 1 7 1 8 1 9
11.2 Grammars
753
Clearly, the form A ~ t or A ~ tB always results. So this g r a m m a r G for the set L(G) of integers is regular. T h u s the set of integers is a r e g u l a r language and hence contextfree. I1 Consider the g r a m m a r G  (N, T, P, a), w h e r e N  {A, B, a }, T = {a, b }, and P = {a ~ aab, a ~ aAb, aAb ~ aBb, A ~ a, B ~ b, A ~ ~, B ~ ~ }. In the production aAb ~ aBb, A can be replaced with B only ifA is s u r r o u n d e d by a and b. Notice t h a t L(G) = {ambm,ambm+l,am+lb m [ m >_ 1}. I The g r a m m a r G = (N, T , P , ~) in E x a m p l e 11.15 is contextfree, so L(G) {anb n In >_ 1} is a contextfree language. E x a m p l e 11.53 will d e m o n s t r a t e t h a t G is not regular. II A language L(G) m a y contain words derivable from a in more t h a n one way. Accordingly, we m a k e the following definition.
Ambiguous Grammar A g r a m m a r G is a m b i g u o u s ifa string in L(G) has more t h a n one derivation tree. The next two examples p r e s e n t a m b i g u o u s g r a m m a r s . The following g r a m m a r G defines the s y n t a x of simple algebraic expressions: (expression) ::= (expression) (sign> (expression) I (letter>
<sign> ::= + l : : = a l b l c l . . .
Iz
This g r a m m a r can produce the expression a  b + c two ways, as the derivation trees in F i g u r e 11.11 show. As a result, G is an a m b i g u o u s grammar. 1 o ~
(optional) T h e following are simplified production rules for an ifthen statement S: S ::= if (expression) t h e n <statement> I if (expression) t h e n (statement) else (statement) (expression) ::= E1 I E2 (statement) ::= S1 I $2 I if (expression> t h e n (statement) To see t h a t these rules produce an a m b i g u o u s g r a m m a r , notice t h a t the ifthen s t a t e m e n t I f E1 then i f E2 then $1 else $2
(11.3)
754
Chapter I!
FormalLanguages and FiniteState Machines
F i g u r e 11.11
(expre ssion)
(sign)
I
(let ;er)
(expression)
I
(letter)
(sign)
(expression)
I
+
c
(expression) ~
~
(expression)
(sign)
(expression)
I
(letter)
(sign)
(expression)
I
(letter)
+
I I b
I
(letter)
c
can be interpreted in two ways: (i) If E1 then (if E2 then S 1 else $2), or (ii) If E 1 then (if E2 then S 1) else $2. Using indentation, these possibilities can be displayed as follows: (i)
i f El then i f E2 then Sl else $2
(ii)
i f El then i f E2 then Sl else $2
Accordingly, statement ( 11.3) can be generated by two distinct derivation trees (see Figure 11.12). To avoid this confusion, each e l s e is paired with the nearest if. Consequently, statement (i) is the correct interpretation of statement (11.3). If you would like statement (11.3) to mean statement (ii), you have
11.2 Grammars
755
F i g u r e 11.12
if
(expression>
then
I
<stat~nt)
if
E1
(expression}
then
(statement}
else
(statement}
I
I
J
E2
S1
S2
<exprission }
t
E1
h if
e
n
(expression)
~
~
<statement}l
then
(statement)
I
S2
I
E2
S1
two options: i f El then i f E2 then Sl else el se
i f El then begin i f E2 then SI end
$2
else S2
m
The way a grammar produces its language of terminal and nonterminal symbols determines whether it is regular, contextfree, or contextsensitive. The BNF notation facilitates such a differentiation. Exercises 11.2
In the grammar G = (N, T,P, a), N = {(sentence), (noun phrase}, (verb}, {object phrase), (article), (noun)}, T = {a, the, cat, dog, chicken, milk, drinks, eats}, a = (sentence) and the production rules are: (sentence) ~ (noun phrase)(verb)(object phrase) (noun phrase) > (article)(noun) (article) > a Jthe
756
Chapter 11 FormalLanguages and FiniteState Machines {noun) ~ cat l d o g J c h i c k e n J milk {verb) ~ drinks leats {object phrase) ~ {article)(noun) D e t e r m i n e if each is a valid sentence in L(G). 1. The cat drinks the milk.
2. A chicken eats the dog.
3. The dog swallows the cat.
4. The chicken d r i n k s a rabbit.
C o n s t r u c t a derivation tree for each sentence in L(G). 5. The cat eats the chicken.
6. A dog d r i n k s the milk.
With the g r a m m a r in Example 11.12, c o n s t r u c t a derivation tree for each word in L(G). 7. aa
8. aba
9. ab2a
10. ab3a
D e t e r m i n e if each word belongs to the language g e n e r a t e d by the g r a m m a r in Example 11.13. 11. aba
12. abba
13. a3ba
14. a2b3a 4
Use the g r a m m a r G = ( N , T , P , a ) , where N = {A,a}, T = {a,b}, a n d P = {a ~ aa, a ~ aA, A ~ b}, to a n s w e r Exercises 1523. Draw a derivation tree for each word in L(G). 15. ab
16. a2b
17. a3b
18. a4b
Do the following words belong to L(G)? 19. aba
20. abba
21. a3b
22. a5b
23. Identify the language L(G). Consider the g r a m m a r G  ( N , T , P , a ) , where N = {a}, T  {a,b}, and P  {a ~ aab, a ~ ab}. D e t e r m i n e if each word belongs to L(G). 24. abba
25. abab
26. a2b 2
27. a3b 3
28. Identify the language L(G). Find the language g e n e r a t e d by each g r a m m a r G = (N, T, P, a) where: 29. N = {a,A,B}, T = { a , b } , P = {a ~ aA, A  ~ Bb, A ~ a, B   . b} 30. N = {a,A,B}, T = {a,b}, P = {a ~ aAa, A ~ bBb, a ~ k, A ~ B* a, B ~ b} Develop a g r a m m a r t h a t generates each language over {0, 1 }. 31. {1, 11, 1111, 11111111,...}
32. {0, 00, 10, 100, 110, 0000, 1010,...}
a,
11.2 Grammars
757
33. The set of words with prefix 00. 34. The set of words with suffix 11. 35. The set of binary representations of positive integers. Create a g r a m m a r to produce each language over {a,b}. 36. {bnab n I n > 0 }
37. { a n b l n > l }
39. {amb n I m, n > 1}
38. { a n b a l n > l }
40. The set of palindromes.
Using Example 11.18, draw the derivation tree for each integer. 41. 234
42.  2 3 4
43. An identifier in Java is a letter, underscore, or $, followed by any number of alphanumeric characters. With BNF, define the g r a m m a r for a Java identifier. Use the g r a m m a r in Exercise 43 to see if each string is a valid J a v a identifier. 44. catch 22
45. 20/20
46. algorist
47. three roots
Construct a derivation tree for each identifier. 48. result2
49. value
50. R2D2
51. math
The production rules of a g r a m m a r for simple arithmetic expressions are: (expression) ::= (digit)I ((expression))I + ((expression)) I  ((expression)) I (expression)(operator)(expression)
(digit) ::= 01 1 1 2 1 3 1 4 1 5 1 6 1
71819
(operator) ::= + l  I * [ / i 1 " Use this g r a m m a r to a n s w e r Exercises 5 2  5 9 .
Determine if each is a valid arithmetic expression. 52. 2 , 3 + 4
53.  ( 3 , 4 1 " 5 )
54. 3+ 1"7
55. 6 + 5 / 8 ,
Construct a derivation tree for each expression. 56. 3 + 5 , 6
57. 5 + ( 4 1 " 3 )
58. ( 5 + 3 )  7 / 4
59.  ( 3 1" ( 5 + 2 ) )
A number in ALGOL (excluding the exponential form) is defined as follows: (number) ::= (decimal number)I (sign) (decimal number) (decimal number) :: (unsigned integer) I. (unsigned integer) i (unsigned integer). (unsigned integer)
758
Chapter II
FormalLanguages and FiniteState Machines
(unsigned integer) "' (digit) I (unsigned integer)(digit) (digit)::=01 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 <sign> ::= + l Use this g r a m m a r to answer Exercises 6067. Determine if each is a valid ALGOL number. 60. 234
61. 2.34
62. 234.
63..234
Draw a derivation tree for each ALGOL number. 64.  3 . 7 6
65. +376
66..376
67. 0.23
For Exercises 6873, use the following definition of a simple algebraic expression: (expression) ::= (term) I (sign) (term) I {expression) {adding operator){term) (sign) ::= + 1 (adding operator) ::= + l (term) ::= (factor) [ {term) {multiplying operator){factor) (multiplying operator) ::= * 1 / (factor) ::= (letter) I ( (expression) )l (expression) (letter/::=alblcl...
Iz
Determine if each is a legal expression. 68. a + b , ( c / d )
69. a + b + c
70.  a , b / c + d
71. ( ( a  b ) + c )
Construct a derivation tree for each expression. 72. ( a , b ) + c / d
73. a , ( b + c / d )
74. Use BNF to define a g r a m m a r for the language of wellformed parentheses (wfp). Use the g r a m m a r in Exercise 74 to see if each is a valid sequence of parentheses. 75. (())
76. ()(())
77. (()())
78. ()()()
79. Figures 11.13 and 11.14 diagram the syntax for an unsigned integer and an unsigned number, respectively. Define the g r a m m a r for an unsigned number in BNF.
11.3 FiniteState Automata F i g u r e 11.13
unsigned integer:
759
[ ~
digit
]
F i g u r e 11.14
unsigned number: unsigned integer
~ 9
unsigned ~ integer
~ ~
E
+
unsigned integer
Using the g r a m m a r in Exercise 79, check if each is a valid unsigned number. 80. 177.76
81. .1776
82. 1776.
83. 17.76E2
This section presents an abstract model of a machine that accepts input values, but produces no output values. Often the question arises whether or not a word over an alphabet is acceptable. For example, is 2R2D an acceptable identifier or is 17.06 a valid real number in C + +? Finitestate automata can model the steps in determining if a given word exists in a language. Accordingly, finitestate automata, also known as l a n g u a g e r e c o g n i z e r s , play a central role in the development of compilers. Before we study the definition, we present a simple example of a language recognizer. Determining if an input string over the alphabet {a, b} contains abba substring involves the following five steps:
as
a
S t e p 0 If the first symbol in the string is a, move to step 1 and look for the character b. Otherwise, no progress has been made. S t e p 1 If the next character is b, the substring ab has occurred, so go to step 2 and look for another b. Otherwise, the symbol b is still missing, so stay in step 1. S t e p 2 If the next symbol is b, the substring abb exists; go to step 3; if a, return to step 1. S t e p 3 If the next symbol is a, the given input string contains the substring abba; otherwise, return to step 0 and start all over again.
760
Chapter II FormalLanguages and FiniteState Machines S t e p 4 Once the substring abba has occurred in the input string, any sequence of a's and b's may follow. These steps can be represented by a digraph (see Figure 11.15), each vertex representing a step. Exactly two edges, labeled a or b, leave each vertex.
F i g u r e 11.15
a
b
To determine the action required from a given step, simply follow the directed edges from the corresponding vertex. For example, at vertex s3 (step 3) if the next input symbol is a, move to vertex s4 (step 4); otherwise, return to vertex so (step 0). The other (labeled) edges are interpreted similarly. The digraph indicates a string contains abba as a substring if and only if the directed path the string determines terminates at vertex s4. The string abab determines the path sosls2sls2, which does not end at s4; consequently, abab is not acceptable. On the other hand, the string ababbab determines the path sosls2sls2s~s4s4, which terminates at s4; so the string does have the desired property, m The digraph in Figure 11.15 displays a f i n i t e  s t a t e a u t o m a t o n . (Automaton is the singular form of automata.) Its five vertices, so through s4, are the s t a t e s of the automaton. Since the whole process begins at so (step 0), so is the i n i t i a l s t a t e . A string is acceptable, that is, contains abba as a substring, if and only if its path ends at s4; accordingly, s4 is an a c c e p t i n g state. The digraph shows the transition of the machine between states. For example, if the automaton is at state s2 and the input symbol is a, the automaton switches its state to s 1. The digraph is the t r a n s i t i o n d i a g r a m of the finitestate automaton. The initial state is customarily identified by an arrow pointing to it and an accepting state by two concentric circles, as Figure 11.16 shows. The transition diagram appears in Figure 11.17.
F i g u r e 11.16
The initial state
An accepting state
!1.3 FiniteStateAutomata F i g u r e 11.17
761
a
a a
b
Each state si and an input symbol d e t e r m i n e a u n i q u e state sj. So we can define a function f : S x I ~ S as follows, where S = {so, Sl, s2, s3, s4 }, the set of states, and I = {a, b}, the i n p u t alphabet: f ( s o , a) = 81 f(s2, a) = s i f(s4, a)   8 4
f ( s o , b) = so
f(s2, b) = s 3 f(s4, b) = 8 4
f ( s l , a) = s i f(s3, a)   8 4
f(si,b) =s2 f(s3, b)   s o
The function f is the t r a n s i t i o n f u n c t i o n of the finitestate a u t o m a t o n . It can also be defined by the t r a n s i t i o n t a b l e in Table 11.1.
T a b l e 11.1
State
s0 Sl s2 s3 s4
Input symbol a
b
s1 81 81 s4 s4
80 s2 s3 so s4
We are now ready to define a finitestate a u t o m a t o n .
FiniteState Automaton A f i n i t e  s t a t e a u t o m a t o n (FSA), M, manifests five characteristics: 9 A finite set, S, of s t a t e s of the a u t o m a t o n . 9 A specially designated state, so, called the i n i t i a l state. 9 A subset A of S, consisting of the a c c e p t i n g s t a t e s (or final s t a t e s ) of the a u t o m a t o n . 9 A finite set, I, of i n p u t s y m b o l s . 9 A function f : S x I ~ S, called the t r a n s i t i o n f u n c t i o n or the n e x t state function. In symbols, M  ( S , A , I , f
, so).
762
Chapter II
FormalLanguages and FiniteState Machines
For instance, for the FSA in Example 11.26, S {80,s1,82,83,84}, A = {s4}, I  {a,b}, and the t r a n s i t i o n function f i s defined by Table 11.1. New York City subway c o m m u t e r s use an FSA everyday, as the next example shows. =
A turnstile in the s u b w a y e n t r a n c e contains four a r m s at waist level (Figure 11.18). Initially, it is locked so t h a t the a r m s c a n n o t be moved. Depositing a token into the slot, however, unlocks it and allows the a r m s to r o t a t e t h r o u g h one q u a r t e r of a complete turn, so the c o m m u t e r passes t h r o u g h the turnstile.
F i g u r e 11.18
The t u r n s t i l e has two states: locked (l) and unlocked (u). Depositing a token (t) shifts the t u r n s t i l e from the locked state to the unlocked state and no m a t t e r how m a n y times the c o m m u t e r inputs t, the t u r n s t i l e r e m a i n s in the same state. P u s h i n g (p), the arms, takes the t u r n s t i l e back to the locked state. Once it is in the locked state, it r e m a i n s t h e r e regardless of how m a n y times the c o m m u t e r pushes the arms; t h a t is, regardless of the n u m b e r of times he inputs p into the device. The t u r n s t i l e exemplifies an FSA. Figure 11.19 shows its t r a n s i t i o n diagram.
F i g u r e 11.19
l The next two examples draw t r a n s i t i o n d i a g r a m s of FSAs from their algebraic definitions. Draw the t r a n s i t i o n d i a g r a m of the FSA M  (S, A, I, f, so), where S { s o , s l , s 2 } , A = {s2}, I = {a, b}, and the t r a n s i t i o n function f is defined by
f(s0, a) f(sl,
= Sl, b) = so,
f(s0, b) f(s2, a)
= so,
 s2,
f(sl, f(s2,
a)  s2, b)  so.
*Based on B. Hayes, "On the FiniteState Machine, A Minimal Model of Mousetraps, Ribosomes, and the Human Soul," Scientific American, Vol. 249 (Dec. 1983), pp. 2028, 178.
11.3 FiniteStateAutomata
763
SOLUTION: The FSA contains t h r e e states   so, S l , and s2   w i t h s2 t h e only accepting state. Since t h e r e are two i n p u t symbols, exactly two edges leave each vertex. D r a w a directed edge from s t a t e si to s t a t e sj if t h e r e is an i n p u t symbol x such t h a t f ( s i , x )  sj; t h e n label the edge x. F o r example, since f ( s l , b)  so, a directed edge r u n s from Sl to so labeled b. F i g u r e 11.20 shows the r e s u l t i n g t r a n s i t i o n diagram.
F i g u r e 11.20
b
a a
~
m D r a w the t r a n s i t i o n d i a g r a m of the FSA M  (S, A, I, f, so), w h e r e S {so, s 1, s2, s3, s4}, A  {s2}, I  {a, b, c }, and f is defined by Table 11.2.
T a b l e 11.2 a
s4
s3 s4
Sl s4
b
c
s2
s3
s2
s3
s4
83
s2
s4
s4
s4
SOLUTION: The a u t o m a t o n contains five states, with s2 the only accepting one. Since t h e r e are t h r e e i n p u t symbols, t h r e e edges originate from every state. D r a w a directed edge from s t a t e si to s t a t e sj if t h e r e exists an i n p u t symbol x such t h a t f ( s i , x )  sj. For instance, f ( s l , c ) = s3, so a directed edge labeled c r u n s from state Sl to s t a t e s3. F i g u r e 11.21 displays the r e s u l t i n g t r a n s i t i o n diagram, where, for convenience, t h e t h r e e loops at s4 a p p e a r as a single loop with labels a, b, a n d c.
F i g u r e 11.21 c
c
m
Chapter11 FormalLanguages and FiniteState Machines
764
Suppose a string is input into an FSA. If the path it determines ends at an accepting state, the string is a c c e p t e d (or r e c o g n i z e d ) by the automaton; otherwise, it is r e j e c t e d by the automaton. Determine if the strings a3b2ab and ab3a are accepted by the FSA in Figure 11.17. SOLUTION: First, find the path determined by the string and check if it terminates at s4, the accepting state. (Recall that a3b2ab  aaabbab.) Begin at the initial state, so. When a is input, move to state Sl. Every time a is input, remain there, so the path defined by a a a is sos 1s 1s 1. When b is input, transfer to state s2. The path obtained thus far is s o  s l  s l  s l  s 2 . Now b moves to s3 and a to s4, yielding the path 808181s1828384. Once in s4, remain there no matter what the input is. Thus the path determined by the given string is SOSl8181S2SnS4S4. Since it terminates at s4, the FSA accepts the given word. Notice that the path determined by the string ab3a is 808182838081, and it does not end at the accepting state s4; consequently, the automaton rejects the string, m Two different FSAs may accept the same language over an alphabet. This occurrence requires that we make a new definition.
Equivalent FiniteState Automata The set of words accepted by an FSA, M, is the l a n g u a g e a c c e p t e d (or r e c o g n i z e d ) by M and is denoted by L ( M ) . Two finitestate automata, M and M', are e q u i v a l e n t if they recognize the same language: L ( M ) = L ( M ' ) . Identify the language L ( M ) accepted by the automaton M in Figure 11.20.
SOLUTION: Look for paths beginning at so and terminating at words over {a, b } that end in aa.
82. L ( M )
consists of all 1
By Example 11.31, the automaton in Figure 11.20 accepts the language of words over {a, b } ending in a a . You may verify that the FSA in Figure 11.22 accepts the same language. Consequently, the automata in Figures 11.20 and 11.22 are equivalent.
F i g u r e 11.22
b
v
1
11.3 FiniteStateAutomata
765
The next four examples build FSAs with desired properties, as Example 11.26 did. Design an FSA t h a t accepts words over I = {a, b} containing an even n u m b e r of a's. SOLUTION: Every word over I contains either an even n u m b e r of a ' s (E) or an odd n u m b e r of a's (O), so the a u t o m a t o n has two states, E and O, E being the accepting state. Initially, the n u m b e r of a ' s in the word is zero, an even integer; E is the initial state of the automaton. If the a u t o m a t o n is at E and an a is input, it moves to state O. If it is at O and an a is input, it moves to state E. Figure 11.23 shows the transition diagram of the FSA.
Figure 11.23 9 a
A word over I has even parity if it contains an even n u m b e r of a's and o d d p a r i t y if an odd number. Since the a u t o m a t o n in Example 11.33 determines w h e t h e r a word has even or odd parity, it is called a parity
c h e c k machine.
I
Design an FSA accepting words over {a, b } t h a t begin with a a and end in bb. SOLUTION: We build the a u t o m a t o n step by step:
Step 0
Initially, the a u t o m a t o n is at the initial state so.
Step 1 If the first symbol is a, move to state Sl from so and wait for the next symbol. But if the first symbol is b, the word is not acceptable (state s2 ). See Figure 11.24. Figure 11.24
Step 2
If the input symbol at s l is a, move to state s3 and d e t e r m i n e w h e t h e r the string ends with bb. On the other hand, if the input symbol at Sl is b, move to s2 to trap such unacceptable words. Once at s2, r e m a i n there no m a t t e r w h a t the input symbol is. See Figure 11.25.
766
Chapter 11 FormalLanguages and FiniteState Machines
F i g u r e 11.25 b
b
S t e p 3 Every word t h a t triggers a move from so to 8 3 begins w i t h aa. Any n u m b e r o f a ' s can follow it (see the loop at s3 in Figure 11.26). However, if b follows the word, move to a new state s4, as in Figure 11.26.
F i g u r e 11.26 a
b
a
b
b
S t e p 4 If the input symbol at 8 4 is a, r e t u r n to S 3 and look for the pair bb. But if it is b, move to a new state s5. See Figure 11.27.
F i g u r e 11.27
a a
a,b
S t e p 5 Once at s5, any n u m b e r of b's may occur. However, if the input symbol at s5 is a, r e t u r n to s3 to look for bb. Since words e n d i n g in bb are acceptable, s5 is the accepting state. These six steps create the FSA in Figure 11.28.
F i g u r e 11.28
a
b b
b
b a
Y m
! 1.3
FiniteState Automata
767
(optional) An identifier in a p r o g r a m m i n g language consists of a letter followed by any n u m b e r of alphanumeric characters (Section 11.1). Design an FSA that recognizes such legal identifiers. SOLUTION: Let I denote the set of all characters in the alphabet recognizable by a compiler. Let l denote a letter, d a digit, and n any n o n a l p h a n u m e r i c character. The a u t o m a t o n will have three states: so, s l, and s2. State s2 traps all invalid strings. (Accordingly, it is called a t r a p s t a t e or a d u m p s t a t e . ) The resulting a u t o m a t o n appears in Figure 11.29. F i g u r e 11.29
d, n
B
The FSA in Figure 11.29 can be translated into an algorithm which determines if a sequence of characters is a legal identifier. See Algorithm 11.1. Algorithm i d e n t i f i e r (* This algorithm determines whether a sequence of characters is a v a l i d i d e n t i f i e r , using the FSA in Figure 11.29. All characters are read from the same input l i n e . SymboZ denotes an a r b i t r a r y character; stote denotes an a r b i t r a r y s t a t e ; stoteO, stotel, and store2 denote the various states of the FSA. stote2 is a dump state. *) Begin (* algorithm *) state ~ state0 (* i n i t i a l i z e state *) read (symboI ) while not at the end of the current l i n e begin case state of state0: i f symbol is a l e t t e r then state
a/O
b/O
Using the FSM in Figure 11.33, find the output from each input string. 17. abba
18. baab
19. a2b3a
20. a3b2ab 3
Using the unit delay machine in Figure 11.35, find the output of each input string. 21.
1101
22.
1111
23. 0000
24.
101110
25. With a transition table, define the transition function f a n d the output function g of the FSM for binary addition in Figure 11.39. Using the FSM in Figure 11.39, compute the sum of each pair of binary numbers. 26.
1001 0110
27.
00111
28.
10010
1011 0110
29.
11011 10101
30. Redraw Figure 11.20 as the transition diagram of an FSM. 3134. Redraw the transition diagram of each automaton in Exercises 1720 of Section 11.3 as that of an FSM. Determine if the input string in Exercises 3538 is accepted by the FSM in Figure 11.42. F i g u r e 11.42
('~ b/O
a/1
b/O 35. abba
36. aabb
37. a 3
38. b3a 4
39. Identify the language accepted by the FSM in Figure 11.42. Design an FSM accepting strings over {a, b} that: 40. Contain aa as a substring.
41. Contain exactly one a.
With x an input symbol and s an arbitrary state of an FSM M (S,I, O , f , g , so), define g(s,x) in each case. 42. f(s, x) is an accepting state.

43. f(s, x) is a nonaccepting state.
1I.,5 Deterministic FiniteState Automata and Regular Languages
779
Is the language accepted by an FSA contextsensitive? Or is it contextfree, regular, or something else? This section provides a definitive a n s w e r to these questions. In an F S A M  ( S , A , I , f , so), where II[ = m, exactly m outgoing edges leave every state si, each labeled with a unique element of I. Besides, since f 9S x I ~ S, every s t a t e  i n p u t pair yields a unique state; in other words, every s t a t e  i n p u t pair uniquely determines the next state. For the a u t o m a t o n in Figure 11.15, the pair (s2, a) determines the state Sl, whereas the pair (s2, b) determines the state s3. Accordingly, the a u t o m a t a in Section 11.3 are called d e t e r m i n i s t i c f i n i t e  s t a t e a u t o m a t a (DFSA). This determinism suggests t h a t the language accepted by a DFSA is indeed regular, as the next example demonstrates. By Example 11.31 the language L ( M ) accepted by the DFSA in Figure 11.43 consists of words over {a, b } ending in a a . Employing it, a regular g r a m m a r G  ( N , T , P , c~) can be constructed. Choose {a, b} as the set of t e r m i n a l symbols: T  {a, b}. Choose the states as the n o n t e r m i n a l symbols: N = {so, s l, s2 }. Select the initial state so as the start symbol: a  so. Figure
11.43
b a
a
a
Define the two productions rules: 9 If there is an edge labeledx from state si to state sj, define the production si ~ xsj. The various productions obtained this way are" so ~
asl,
So ~
bso,
81 ~
as2,
S l > bso,
s2 ~
as2,
and
s2 > bso.
9 If there is an edge labeled x from state si to an accepting state, induce the production si ~ x. Two additional productions can be obtained by this method: Sl > a
and
s2 > a
The g r a m m a r G  (N, T, P, a) where N, T, P, and a are defined as above is clearly regular, therefore L ( G ) is a regular language. You may verify t h a t L ( G ) consists of strings over T ending in a a . T h u s L ( M )  L ( G ) . m
Chapter II
780
FormalLanguages and FiniteState Machines
This example leads us to a f u n d a m e n t a l result whose proof resembles t h a t in Example 11.44. ~
The language accepted by a DFSA is regular. PROOF:
Let M = (S,A, I, f, so) be a DFSA and L ( M ) denote the language accepted by the automaton. We shall construct a regular g r a m m a r G using the machine M and show t h a t L ( G )  L ( M ) . To construct the g r a m m a r G  (N, T , P , ~ ) , choose N  S as the set of states, T = I as the input alphabet, and a = so as the initial state. Define the productions P this way: Let si and sj be any two states, and x any input symbol. If f ( s i , x ) = sj, define the production si ~ xsj; if f ( s i , x ) = sj, an accepting state, include the production si > x. Clearly, G is a regular g r a m m a r . T o p r o v e t h a t L ( M ) c_ L ( G ) :
Let x = X l X 2 . . . Xn be a string accepted by the a u t o m a t o n M; t h a t is, let x ~ L ( M ) . Then the transition diagram of the a u t o m a t o n contains a directed path sos 1s2 . . . . . Sn, where Sn is an accepting state. Correspondingly, these production rules follow: (11.4)
so * X l S l S 1 > X 2 8 2
Si1
> X i S o i
S n  1 > X n
(Note: Sn is an accepting state.)
and the derivation of the string x: ~, X l S 1
(11.5)
~, x l X 2 8 2
~, X l X 2 . . . X n _ l S n _ l XlX2 . . . Xn_lXn
since S n  1 > X n . Thus x ~ L(G), so L ( M ) c_ L ( G ) . Conversely, let x = X l X 2 . . . X n E L ( G ) . Then it m u s t have a derivation of the form (11.4). Correspondingly, the transition diagram of the a u t o m a t o n M m u s t contain a directed path, sos lS2 . . . . . Sn. The string determined by this path is x = X l X 2 . . . Xn. Since the last production in the derivation
11.5 Deterministic FiniteState Automata and Regular Languages
(11.4) is S n 
1 > Xn, Sn
L ( G ) c_ L ( M ) . Thus L ( M ) = L ( G ) .
is regular,
781
must be an accepting state, thus x ~ L ( M ) and hence In other words, the language accepted by the DFSA m
This proof provides an elegant method for finding the regular language accepted by a DFSA. We demonstrate it again in the next example. ~
Find the g r a m m a r of the regular language accepted by the parity check machine in Example 11.33. SOLUTION: Using the transition diagram in Figure 11.23, N = {E, O}, T  {a, b}, S {E}, and the production rules are: EoaO,
E~bE,
O~aE,
O+bO,
Eob,
and
O+a
The regular g r a m m a r defined by the parity check machine M is G (N, T , P , S ) . [So L ( G ) = L ( M )  the set of strings over T containing an even number of a's.] n Finally, is the converse of Theorem 11.3 true? With G a regular grammar, does a DFSA exist such that L ( M ) = L(G)? The next two sections will give us an answer. Exercises 11.5
Determine if each is a DFSA. 0
0
a
b
a
b
)
a
0
+
a ~..
Chapter II
782
Formal Languages and FiniteState Machines
a,b
0
a
a a,b r
Write the regular g r a m m a r defined by the DFSA in each figure. 5. Figure 11.17
6. Figure 11.28
714. Construct the regular g r a m m a r defined by each DFSA in Exercises 1724 of Section 11.3. By m a k i n g a DFSA, define a regular g r a m m a r G = (N, T , P , ~ ) t h a t generates the language consisting of strings over {a, b } that: 15. Contain exactly one a.
16. Contain at least one a.
17. Begin with aa.
18. End with bb.
19. Contain a b a as a substring.
20. Contain a a a as a substring.
21. Begin with a a or bb.
22. Contain b a a b as a substring.
We ended the preceding section with a question: For a regular g r a m m a r G, is there a DFSA M such t h a t L ( G ) = L(M)? The obvious t e m p t a t i o n is to simply reverse the steps in Example 11.44 (or Theorem 11.3) to look for it. Let's see what happens if we do so. ~
With the regular g r a m m a r G  (N, T, P, ~), where N  {A, c~}, T = {a, b}, and P = {a * aa, a ~ aA, A ~ b}, let us see what happens if we reverse the steps in Theorem 11.3 in order to construct a D F S A M = ( S , A , I , f , so). Then I = T = {a, b} and so = a. Corresponding to the productions a  . aa and a * aA, there m u s t be two states, namely, a and A; besides, by virtue of the production A ~ b, an accepting state F must exist. T h u s S m u s t be {a,A, F}. Use the productions to draw the edges in the transition diagram of the automaton: If si ~ xsj, draw an edge from state si to sj and label it x; if si ~ x, draw an edge from si to the accepting state F and label it x. The diagram in Figure 11.44 results. Unfortunately, it is not a DFSA for two reasons: (1) A state, c~, has two outgoing edges with the same label a; (2) not every state, namely A and F, has two edges with different labels. T h u s reversing the steps illustrated in Example 11.44 does n o t yield a DFSA.
11.6 NondeterministicFiniteState Automata
783
F i g u r e 11.44
a
@
b
@
n
But, fortunately, we have a n o t h e r option. The a u t o m a t o n in F i g u r e 11.44 is a n o n d e t e r m i n i s t i c finitestate a u t o m a t o n . " N o n d e t e r m i n i s t i c " m e a n s t h a t each s t a t e  i n p u t pair m a y d e t e r m i n e more t h a n one state. F o r instance, the pair (a, a) d e t e r m i n e s two states, a and A. If a is i n p u t at state a, two choices exist for the next state: r e m a i n at a or move to A. We can now move to the following definition.
Nondeterministic FiniteState Automata A n o n d e t e r m i n i s t i c f i n i t e  s t a t e a u t o m a t o n (NDFSA) M exhibits five characteristics: 9 A finite set S of states; 9 A specially designated state ~, called the i n i t i a l state; 9 A subset A of S consisting of the a c c e p t i n g s t a t e s (or final s t a t e s ) of the a u t o m a t o n ; 9 A finite set I of i n p u t symbols; 9 A function f : S • I , P(S), called the t r a n s i t i o n f u n c t i o n (or the n e x t  s t a t e function). [Note: P(S) denotes the power set of S.] In symbols, M  (S, A, I, f, c~). In an NDFSA, each s t a t e  i n p u t pair is linked with a set of states, not necessarily a u n i q u e state; it can be the null set. A N D F S A can be represented by a t r a n s i t i o n d i a g r a m and a t r a n s i t i o n table can define a t r a n s i t i o n function, as the next two examples illustrate. For the N D F S A in Figure 11.44, S  {a, A, F} a n d A table in Table 11.7 defines the t r a n s i t i o n function.
Table 11.7
{F}. The t r a n s i t i o n
a
{c~,A} 0 0
0 {F} 0
m The N D F S A M  (S, A, I, f, a), where S  { a , A , B , C } , A  {F}, I {a, b}, and f is defined by Table 11.8. Its t r a n s i t i o n d i a g r a m is given in Figure 11.45.
Chapter 11 FormalLanguages and FiniteState Machines
784 T a b l e 11.8
I
A
F i g u r e 11.45
a
{a,A} {A} {F} O
O
{B,F} {B} O
~a~b~a ~
~
~~/....
b
I
The definition of a string accepted by an FSA can be extended to NDFSA as well.
Equivalent Nondeterministic FiniteState Automata A string is a c c e p t e d or r e c o g n i z e d by a NDFSA M = (S, A, I, f, so) if a directed path runs from the initial vertex so to an accepting state t h a t generates the string. The language of all strings accepted by M is L ( M ) . Two NDFSAs are e q u i v a l e n t if they accept the same language. The next two examples illustrate the definition of ( L ( M ) ) . The word a3b is accepted by the NDFSA in Figure 11.44 since the corresponding path, ~~~AF, ends at an accepting state F. Notice t h a t L ( M ) = {a nb I n >_ 1 }.
I
The string a2b3a is accepted by the NDFSA in Figure 11.45. Two paths generate it, a  a  A  B  B  B  F and a  A  A  B  B  B  F . The a u t o m a t o n accepts strings amb and a mb ha, where m, n >_ 1. Thus L ( M )  {a rob, amb na ] m, n>l}. I The question we posed at the beginning of this section can be partially answered now. Every regular language is accepted by an NDFSA.
PROOF: Let G = (N, T, P, a) be a regular g r a m m a r . Through essentially the same steps as in Example 11.46, make a suitable N D F S A M = (S, A, I, f, so) such that L ( G ) = L ( M ) . Select I = T, so = {a }, and N as the set of nonaccepting states of M. Since the g r a m m a r contains productions of the form si x, introduce an accepting state F; choose S = N u {F} and A  {F}. Finally, since every production of G is si ~ xsj or si ~ x, the t r a n s i t i o n function f : S x I + P ( S ) follows: f ( s i , x )  {sj I si + x s j } U {F I si + x}.
785
11.6 Nondeterministie FiniteState Automata
As in Theorem 11.3, it can be shown t h a t L ( G ) = L ( M ) . (Complete the proof.) m Although nondeterministic finitestate a u t o m a t a have been defined, an explicit answer to the question posed earlier has yet to surface: Given a regular g r a m m a r G, does there exist a DFSA such t h a t L ( G ) = L(M)? We will answer this in the next section. E x e r c i s e s 11.6
Draw the transition diagram of the NDFSA M = ( S , A , I , f , so), where: 1. S = {so,sl,s2), A = {s2} S
I
80 81
J
a
b
{81} {81}
{80} {81,82}
O .
.
0 .
I s2
.
3. S = { s o , s l , s 2 , s 3 ) , A = {s2} S
I
80 81 s2
a
{s~}
80 81
{s3} {s3}
s2
a
b
{S0,Sl} {Sl,82} 0 0 {84}
{84} {81,S3} 0 0 {84}
{81} {82}
{80} {81,82}
0
0
a
b
{80, 81 } {81,82}
{83 } {80}
0
0
{81}
{83}
6. S  { 8 0 , 8 1 , 8 2 , s 3 , 8 4 , 8 5 ) , A = {s2,s5} a
s0 81 82 83 84 85
Construct a transition table for each NDFSA.
a ,
b
I
{83} {81}
5. S{8o,81,'32,83,84), A = {s2,s3}
a
4. S = {so, s l , s 2 , s 3 } , A = {s2} S
{80,81} {81,82} {s2}
so 81 82 83 84
{so,sl,s2}, A = {81}
2. S 
~
b
{80,81} {Sl,S2} {S2} {S3} {83} {s5}
b
{84} {83} {S2} {S3} {S4,S5} {s5}
786
Chapter 11 Formal Languages and FiniteState Machines
0
a
b 0
a
a
a
b
a
b
10.
a, b Does the NDFSA in Figure 11.45 accept each string? Identify a path defined by any accepted string. 11. ab2a
12. abab
13. a3b
14. ab2ab
Is each string accepted by the NDFSA in Exercise 1? Give a path for accepted strings. 15. a2b
16. ab2a
17. a3b 3
18. (ab) 3
Does the NDFSA in Exercise 10 accept each string? Show a path that defines any accepted string. 19. abba
20. (ab) 3
21. a2b 2
22. a4b2ab 3
Construct a NDFSA that accepts the language generated by the regular grammar G  (N, T, P, or), where: 23. N = { a , A , B } , T = { a , b } , a n d P = { a o a A , bB, A ~ a}
A~aA,
A~bB,
B~
24. N = {a,A,B}, T = {a,b}, a n d P = {a ~ aA, a ~ bA, A  ~ aB, a b, B o b}
11.7 Automataand Regular Languages
787
25. N = {a, A, B, C, D}, T = {a,b}, and P = {cT ~ ba, a ~ aA, A ~ aA, A ~ bB, B ~ aA, B ~ bC, C ~ aD, C ~ ba, D ~ aD, D ~ bD, C
a} 26. N = { a , A , B , C } , T = {a,b}, and P = {a ~ bcr, a ~ A ~ bB, B o aA, B ~ bC, C ~ aA, C ~ bcr, B ~ b}
aA, A ~
aA,
Create a N D F S A t h a t accepts the regular language over {a, b} of strings that: 27. Contain exactly one a.
28. C o n t a i n at least one a.
29. Begin with aa.
30. E n d with bb.
31. Contain a b a as a substring.
32. Contain a 3 as a substring.
33. Begin with a a or bb.
34. Contain ba2b as a substring.
*35. Begin with aa, but not end in bb. *36. Begin with aa and end in bb.
The preceding two sections d e m o n s t r a t e d t h a t the language accepted by a DFSA is regular and t h a t every regular language is accepted by an NDFSA. This section shows t h a t every N D F S A is equivalent to a DFSA, which answers affirmatively our question about the existence of a possible DFSA M such t h a t L ( G ) = L ( M ) . Every regular language is, in fact, accepted by a suitable DFSA. The next two examples illustrate step by step how to construct a DFSA equivalent to a given NDFSA. Consider the regular g r a m m a r G  (N, T, P, a), where N  {A, a }, T = {a,b}, a n d P {a ~ aa, c~ ~ aA, A ~ b}. The N D F S A M = ( S , A , I , f , so) t h a t accepts L ( G ) is shown in Figure 11.46 (same as Figure 11.44). By Example 11.49, L ( M )  {a'~b In > 1}. U s i n g M, we shall construct the DFSA M'  (S',A', I', f', s 0) ' which accepts L(G)"
F i g u r e 11.46 a
r@
b
~@
!
S t e p 1 C h o o s e I '  I = {a,b}, s 0  {so}  {a}, a n d S '  P ( S ) . The various states in M' are subsets of S. If t h e r e are n states in M, there can be 2 n states in M', so the states of M' are: O, {or}, {A}, {F}, {~,A}, {cy,F}, {A,F}, and { ~ , A , F }
788
Chapter II
Formal Languages and FiniteState Machines
S t e p 2 The accepting states of M' are those states of M' t h a t contain an accepting state of M. They are {F}, { a , F } , {A,F}, and {a,A,F}. Step 3
Let X  {s l, s 2 , . . . , sin} be a state in M'. An input symbol x leads m
from state X to state Y, where Y 
u f ( s i , x ) . In other words, an edge i=l m
labeled x r u n s from state X to state Y if Y = u f ( s i , x ) . i=l
Figure 11.46 produces all possible transitions: f ( 0 , a) = 0 f ( A , a) = 0
f ( 0 , b) = 0 f ( A , b) = {F}
f ( a , a)  {a,A} f ( F , a) = 0
f ( a , b) = 0 f ( F , b) = 0
Since f(O, a)  0  f ( 0 , b), edges r u n from 0 to itself labeled a and b. Since f ( a , a)  {~, A} and f ( a , b ) = O, an edge labeled a goes from {~ } to {a, A} and an edge b from {a } to 0. Similarly, t h e r e is an edge labeled a from {A} to 0, an edge b from {A} to {F}, and two edges a and b from {F} to O. Since f(~, a) u f ( A , a) = {~, A} u O = {~, A}, an edge labeled a r u n s from {a, A} to {a, A}. Also, f ( a , b ) u f(A,b) = 0 u {F} = {F}, so an edge b goes from {a,A} to {F}. Similarly, t h e r e are edges labeled a and b from {a, F} to {~, A} and 0, respectively; edges a and b from {A, F} to 0 and {F}, respectively; and edges a and b from {cr, A,F} to {or, A} and {F}, respectively. These results appear in the transition table in Table 11.9. Table 11.9 0
{A} {F} {cy,A} {a,F} {A,F} {a,A,F}
a
b
0 {a,A} 0 0 {a,A} {a,A} 0 {a,A}
0 0 {F} 0 {F} 0 {F} {F}
Figure 11.47 shows the resulting DFSA. Since the states {A}, {or, F}, {A, F}, and {~, A, F} cannot be reached from the initial state {or }, they can be dropped out to yield the simplified DFSA M' in Figure 11.48. F r o m this transition diagram, L ( M ' )  {aanb In > 0}  { a n b l n > 1} L(G). T h u s the a u t o m a t a M and M' are equivalent, so the NDFSA is the same as the DFSA. m Construct a DFSA M ' (S',A' , I' , f', 8 0') equivalent to the N D F S A M  ( S , A , I , f , so) in Example 11.50. Recall t h a t L(M)  {amb, ambna[ m, n > 1 }. The key steps lie below. (Fill in the details.)
11.7 Automataand Regular Languages F i g u r e 11.47
789
a,b
a,b
b
F i g u r e 11.48
SOLUTION: S t e p 1 Select I' = I  {a,b}, s~  {so}  {a }, and S' = P(S). The states of M' are 0, {a}, {A}, {B}, {F}, {a,A}, {a,B}, {a,F}, {A,B}, {A,F}, {B,F}, {a,A,B}, {a,A,F}, {~,B,F}, {A,B,F}, and {~,A,B,F}. S t e p 2 The accepting states of M' are {F}, {a,F}, {A,F}, {B,F}, {a,A,F}, { a , B , F } , {A,B,F}, and {a,A,B,F}.
790
Chapter 11 FormalLanguages and FiniteState Machines Step 3
The transition table of the DFSA is Table 11.10.
Table 11.10
0 {A} {B} {F} {c~, A} {a,B} {c~, F} {A, B} {A, F} {B,F} {c~, A, B} {c~,A, F}
{a,B,F} {A,B,F} {a,A,B,F}
a
b
0 {a,A} {A} {F}
0 0 {B, F} {B}
{a,A} {a, A, F} {or, A} {A,F} {A} {F} {c~,A, F} {a,A} {a, A, F} {A, F} {a, A, F}
{B,F} {B} 0 {B, F} {B, F} {B}
{B,F} {B,F} {B} {B, F} {B,F}
S t e p 4 The table indicates the states {a, B}, {a, F}, {A, B}, {c~, A, B}, {a, B, F }, {A, B, F}, and {a, A, B, F } are not reachable from any state, so they are not the initial state {a }. Delete the corresponding rows from the table. It is now obvious from the table that the states {A}, {A, F}, {a, A, F} also cannot be reached from {a }; delete those rows also from the table. The resulting transition Figure 11.49. F i g u r e 11.49
:,b
~
a
~
diagram
of the
DFSA M'
appears
in
a,b
b
From the diagram, it follows that L(M') = {amb,ambnaim, n >_ 1} = a n d M ' are equivalent automata. As in the previous example, we have shown that the equivalency between an NDFSA and a DFSA. i
L(M). T h u s M
The techniques illustrated in the two previous examples can be generalized to arrive at the following result. (The proof is a bit complicated, so we omit it.)
11.7 Automataand Regular Languages ~
791
Every NDFSA is equivalent to a DFSA.
m
The next theorem follows from Theorems 11.3, 11.4, and 11.5. ~
A
language is regular if and only if it is accepted by a DFSA.
m
As Theorem 11.6 indicates, a DFSA can define a regular g r a m m a r and vice versa. Each is a characterization of the other. We now look for an example of a simplelooking language that is regular.
~
not
Show that the language L  {anb n I n > 1} is not regular. P R O O F (by c o n t r a d i c t i o n ) : Suppose L is regular. Then, by Theorem 11.6, a DFSA M exists such that L ( M )  L . Suppose M has m states. Since the string x  a m + l b m+ 1 E L , x is accepted by the DFSA. Let P be the path corresponding to x; it ends at an accepting state F.
F i g u r e 11.50
a
)
@a@a ~
a
,~
@b@ ...
The path corresponding to the substring a m+l contains m + 1 states. But, since only m states exist, by the pigeonhole principle at least two of the m + 1 states, say, si and sj, where i < j, must be the same; consequently, there must be a directed cycle at si, each edge labeled a (see Figure 11.50). Let l be the length of the cycle. The path s o  s 1 . . . . . s i  s j + 1sj+2 . . . . . F generates the string x' = a m + l  l b m + l . Since this path ends at F (an accepting state), x' is accepted by the automaton; so x' ~ L. This is a contradiction, since x' does not contain the same number of a's and b's. Thus L is not a regular language, m It follows by this example that the set of wellformed nested parentheses is also n o t a regular language. (Why?) These discussions lead us to a powerful conclusion: Regular languages are accepted by DFSAs.
792
Chapter 11 FormalLanguages and FiniteState Machines
Exercises 11.7 16. Construct a DFSA equivalent to each N D F S A in Exercises 14, 7, 8 of Section 11.6. Eliminate all unreachable states. 78. Design DFSAs equivalent to the NDFSAs in Exercises 23 and 24 of Section 11.6. Eliminate all unreachable states. Let L be the language recognized by an FSA and L R   { X n . . . Xl [ X l . . . X n E L}. Construct an NDFSA t h a t accepts L R from each FSA in Exercises 916. ( H i n t : Reverse the directions of the edges; switch the roles of the initial state and the accepting states.) 9. Figure 11.20
10. Exercise 17 in Section 11.3
11. Exercise 18 in Section 11.3
12. Exercise 36 in Section 11.3
13. Exercise 37 in Section 11.3
14. Exercise 38 in Section 11.3
15. Exercise 40 in Section 11.3
16. Exercise 41 in Section 11.3
1724. Identify the language L ( M ) accepted by the FSA in Exercises 916. 2532. Construct a DFSA equivalent to the NDFSA in Exercises 916.
The abstract models of computing machines with limited capabilities are DFSA, FSM, and NDFSA. An a u t o m a t o n checks if a given input string has a desired property and produces no output values. An FSM, on the other hand, yields an o u t p u t value corresponding to each input.
Formal Language 9 A f o r m a l l a n g u a g e over an alphabet E is a subset of E*, the set of all possible words over Z (page 734). 9 The c o n c a t e n a t i o n of two languages A and B over Z consists of words ab with a e A and b E B (page 736). 9 A ~ = { a l a 2 . . . an l ai E A}, where A ~  A
(page 739).
cx$
9 A* = u A n is the K l e e n e c l o s u r e of a language A
(page 739).
n:0
Grammar 9 A g r a m m a r G = (N, T , P , ~) consists of a finite set N of n o n t e r m i h a l s y m b o l s , a finite set T of t e r m i n a l s y m b o l s , a finite set P of p r o d u c t i o n r u l e s , and a s t a r t s y m b o l c~ (page 746).
Chapter Summary
793
9 A word w' = xfly is directly derivable from a word w = x a y if a p r o d u c t i o n a ~ / ~ exists; we t h e n write w ~ w'. A word Wn is d e r i v a b l e from wi if t h e r e exists a finite sequence of d e r i v a t i o n s , Wl =, w2, w2 w 3 , . . . , W n  1 ~ Wn. T h e l a n g u a g e derivable from cr is t h e l a n g u a g e generated by G, denoted by L ( G ) (page 746). 9 In BNF, each production w ~ w' is w r i t t e n as w ::= w'; each n o n t e r m i nal symbol w is enclosed by angle brackets, as in (w); a n d p r o d u c t i o n rules with t h e s a m e lefthand sides are c o m b i n e d w i t h vertical b a r s into a single rule (page 748). 9 A t y p e 0 g r a m m a r has p h r a s e  s t r u c t u r e
(page 751 ).
9 In a contextsensitive (type 1) g r a m m a r , every p r o d u c t i o n is of t h e form c~Ac~' ~ c~flc~'.
(page 751)
9 In a contextfree (type 2) g r a m m a r , every p r o d u c t i o n is of t h e form A ~ ~. (page 751). 9 In a regular (type 3) g r a m m a r , every p r o d u c t i o n is of the form A~ t or A ~ tB (page 751). 9 A language L ( G ) is contextsensitive, contextfree, or regular according as w h e t h e r G is contextsensitive, contextfree, or r e g u l a r (page 751). 9 An ambiguous language contains a word t h a t has more t h a n one derivation tree (page 753). 9 The l a n g u a g e accepted by a DFSA is r e g u l a r
(page 780).
FiniteState Automaton (FSA) 9 A F S A M  ( S , A , I , f , so) consists of a finite set S of s t a t e s , a finite set A of a c c e p t i n g states, a finite set I of i n p u t s y m b o l s , a t r a n s i t i o n function f : S x I ~ S, and an initial state so. Every s t a t e  i n p u t pair yields a u n i q u e n e x t  s t a t e of the a u t o m a t o n (page 761). 9 A transition table defines t h e t r a n s i t i o n function.
(page 761).
9 A transition diagram can r e p r e s e n t a DFSA. The initial s t a t e so is identified by d r a w i n g an a r r o w t o w a r d it; an accepting s t a t e by two concentric circles a r o u n d it (page 762). 9 An i n p u t s t r i n g is a c c e p t e d by an a u t o m a t o n M if a n d if the s t r i n g traces a p a t h t h a t ends at an accepting state. language L(M) accepted by M consists of all words recognized (page 9 Two a u t o m a t a M and M ' are equivalent if L ( M ) = L ( M ' )
only The by it 764 ).
(page 764).
Chapter II FormalLanguages and FiniteState Machines
794
FiniteState Machine (FSM) 9 An F S M M = (S,I, O , f , g , so) consists of a finite set S of states, a finite set I of i n p u t s y m b o l s , a finite set O of o u t p u t s y m b o l s , a t r a n s i t i o n f u n c t i o n f : S x I ~ S, an o u t p u t f u n c t i o n g : S x I ~ O, and an i n i t i a l s t a t e so. Every s t a t e  i n p u t pair produces a n e x t  s t a t e and an o u t p u t value (page 772). 9 A t r a n s i t i o n t a b l e can define the t r a n s i t i o n and o u t p u t functions of an FSM (page 772). 9 A transition
d i a g r a m also can define an FSM
(page 772).
Nondeterministic FiniteState Automaton (NDFSA) 9 An N D F S A M  ( S , A , I , f , ~) consists of a finite set S of states, a subset A of S of accepting states, a finite set I of i n p u t symbols, a t r a n s i t i o n function f : S x I ~ P(S), and an initial state a. A s t a t e  i n p u t pair m a y be paired with zero, one, or more states (page 783). 9 Every regular language is accepted by a N D F S A
(page 784).
9 Every N D F S A is equivalent to a DFSA
(page 787).
9 Every regular language is accepted by a DFSA
(page 787).
Review Exercises Let A = {4, a, bc} and B = {a, ab}. Find each.
1. A B
2. B A
3. A 3
4. B 3
Find three words belonging to each language over {a,b,c }. 5. {a,b}{c}*
6. {a}b*{c}*
7. {ab}{ab}*
8. {b} {a,b,c}* {b}
A g r a m m a r G = (N, T , P , a ) has N = {{noun phrase), (verb}, (adjective), (noun}, (article) }, T = {a,the,chicken,wolf, cabbage,eats,walks,reliable, discreet, gracious}, a = (sentence), and the production rules are: (sentence) ~ (noun phrase) ~
{noun p h r a s e ) ( v e r b ) ( n o u n phrase) (article){noun) I (article){adjective)(noun)
{article) ~ the la (noun) ~ chicken I w o l f l c a b b a g e (adjective) ~ reliable I discreet I gracious (verb) ~ eats l w a l k s
Chapter Summary
795
D e t e r m i n e if each is a valid sentence in L ( G ) . 9. The gracious chicken walks the wolf. 10. The reliable wolf eats a chicken. Make a derivation tree for each sentence. 11. The discreet wolf eats the cabbage. 12. The reliable cabbage walks the gracious chicken. Using the g r a m m a r G  (N, T, P, a) where N = {a, A, B}, T = {a,b}, and P = {a ~ ba, a > aA, A ~ aB, A ~ b a , B ~ aB, B > b B , A ~ a , B ~ a, B ~ b}, d e t e r m i n e if each string belongs to L ( G ) . 13. ab3a
14. (ab) 3
15. aba2b
16. ab2a 4
19. aba 2
20. a2b2a 2
C o n s t r u c t a parse tree for each string. 17. ba2b
18. b2a3b
Develop a g r a m m a r t h a t generates each language over {a,b }. 21. {b n in >_ 1~ 22. {anba'* In > 0} 23. {b 2n+l In >_0} 24. {ab'*aln >_ 0} With the g r a m m a r below, construct parse trees for the simple w h i l e s t a t e m e n t s in Exercises 25 and 26. (while s t a t e m e n t ) ' 
while (expression) do (statement)
(statement) " '  ( a s s i g n m e n t s t a t e m e n t ) f (while s t a t e m e n t ) I s {assignment s t a t e m e n t ) " '  ( v a r i a b l e ) = {expression) (variable/ "' a [b {c I . . . I z (expression) "  (variable)(sign)(variable) J (variable) (operator)(variable/ (operator) " '  =J ~:l < l < l > l > {sign)' +125. While x >_y do x "= y + z. 26. While x >_y do while y < z do a " b + c. 27. Draw the t r a n s i t i o n diagram of the D F S A M  ( S , A , I , f , so), w h e r e S  { s o , s l , s 2 , s 3 , s 4 } , A = {83} , I = {a,b}, and f is defined by Table 11.11. 28. Redo Exercise 27 w i t h A
=
{81,83}
and f defined by Table 11.12.
C o n s t r u c t the t r a n s i t i o n table for each DFSA.
796
Chapter 11
Formal Languages and FiniteState Machines
T a b l e 11.11 sO Sl s2 s3 s4
T a b l e 11.12
s2 s3 s4
Sl s4 s4 s3 s4
s4 s2 S3 s3 s4
a
b
Sl Sl s4 s3 s4
s2 Sl s3 s3 s4
29.
b 30.
a;b
3 1  3 4 . Identify the language L ( M ) accepted by the a u t o m a t a in Exercises 2730. Design a DFSA t h a t accepts strings over {a, b } that: 35. Begin with aaa. 37.
II s0 81 82 s3
36. Contain abb as a substring.
f
s0 s2 80 s3
38.
g b
a
b
sl 82 83
1 0 1 1
0 0 0 0
S2
f
80 81 S2 S3
g
a
b
a
b
sO
Sl
0
0
s2 s3 s3
s3 s2 s2
0 1 1
1 1 0
Chapter Summary
797
Construct a transition table for each FSM. 39.
a/O ~
O Jl b/1
40.
~
a/O
a / O~~)
b/1
~
a / l~')
b/O
Using the FSM in Figure 11.33 (Example 11.38), find the output from each input string. 41. a2b2aba
42. aba2ba
43. ab3ab
44. a2b3a 2
4546. Redraw the DFSAs in Exercises 27 and 29 as FSMs. Design an FSM to accept string over {a, b } that: 47. Contain ab 2 as a substring.
48. Begin with a or b 2.
4950. Compose the regular g r a m m a r defined by the DFSA in Exercises 2728. Draw the transition diagram of the NDFSA M  ( S , A , I , f , {a,b } and: 51.
= {Sl}
S{so,sl,s2),A
52.
so), where I 
{80, S l , 8 2 } , A 
S 
a
{81 } . b
11
so II {sit
si I] {s2} s2 {s2}
{s0t
{sl,s2} {s2}
I
s2
I]
{80,82} {Sl}
{81} {80,82}
Construct a transition table for each NDFSA. Determine if the NDFSA in Exercise 52 accepts each input string. 53.
a,b
)
b
798
Chapter II
FormalLanguages and FiniteState Machines
54.
('~ a, b a
a, b ~") b
_...)
55. a 3
56. ab2ab 4
57. a2b 3
58. a3b 4
5 9  6 2 . D e t e r m i n e if each i n p u t s t r i n g in Exercises 5558 is accepted by t h e N D F S A in Exercise 54. Create a N D F S A t h a t accepts t h e language L ( G ) g e n e r a t e d by t h e r e g u l a r g r a m m a r G = (N, T, P, a), where: 63. N = { a , A , B } , T = { A , B } , a n d P = {a ~ ba, a ~ aA, A ~ aB, A bA, A ~ b B , B ~ aB, B ~ b B , a ~ a , A ~ b} 64. N = { a , A , B } , T = {a,b}, a n d P = {a > aA, a ~ b a , A ~ a a , A ~ aB, A ~ bA, B > aB, B ~ b a , B ~ bA, a ~ a , A ~ b} 6 5  6 6 . C o n s t r u c t a DFSA e q u i v a l e n t to each N D F S A in Exercises 51 and 52. 6 7  6 8 . W h a t languages do the DFSAs in Exercises 65 a n d 66 accept? Let A and B be a n y languages over a finite a l p h a b e t E. Prove each. *69. (A u B*)* = (A* u B)*
**70. (AUB)* = (A*UB*)* = ( A ' B * ) *
Supplementary Exercises
Let m denote the n u m b e r of a ' s and n the n u m b e r of b's in a s t r i n g over {a,b }. Design an FSA t h a t accepts strings with t h e given properties. 1. m  l ( m o d 2) and n  l ( m o d 2). 2. m  0 ( m o d 3) and n  l ( m o d 3).
3. m  0(mod 2) and n  l ( m o d 2), or m  l ( m o d 2) a n d n  0(mod 2). 4. Design an FSA t h a t accepts positive integers n divisible by 3. *5. Using t h e s y n t a x d i a g r a m in Figure 11.51 for a real n u m b e r , design an FSA to recognize valid real n u m b e r s . *6. The R o m a n n u m e r a l s M, D, C, L, X, V, and I have values 1000, 500, 100, 50, 10, 5, and 1, respectively. In the strict additive n o t a t i o n no n u m e r a l with a s m a l l e r value precedes a n u m e r a l w i t h a larger value. For instance, 19 is w r i t t e n as XVIIII in lieu of t h e s h o r t e r
Chapter Summary
799
F i g u r e 11.51
representation XIX and MMLXXVI, unlike MCMXCVI, is a wellformed sequence. Excepting M's, C, X, or I should not appear more than four times in the same sequence, and D, L, or V no more t h a n once. This makes sense since CCCCC  D, XXXXX  L, and so on. Design an FSA to recognize the language of such wellformed sequences of additive Roman numerals. Develop a g r a m m a r t h a t generates each language over {a,b }. *7. The set of words t h a t begin and end with a. *8. The set of words t h a t begin with aa and end with bb. 9. Using BNF, define a g r a m m a r for the language of wellformed nested parentheses. 10. Use productions instead of BNF to define the g r a m m a r in Exercise 9. A M o o r e m a c h i n e M = (S,I, O , f , g , so), named after Edward Moore who introduced it in 1956, is an FSM consisting of a finite set S of states, a finite set I of input symbols, a finite set O of o u t p u t symbols, a transition function f : S • I ~ S, an o u t p u t function g : S ~ O, and an initial state so. Draw a transition diagram for the Moore machine defined by each transition table.
f input
input 1
$
s
o
1 .
11.
SO Sl S2 S3
SO 83 S2 80
12.
Sl S2 S3 81
0
1
.
Sl
S3
0
Sl s2
Sl s3
82 s2
1 1
83
Sl
82
0
13.
1
.
SO
Construct a transition table for each Moore machine.
1
.
1
Chapter II
800
Formal Languages and FiniteState Machines
F o r r e s t M o o r e (1925) was born in Baltimore, Maryland. He graduated from Virginia Polytechnic Institute in 1947 and received his Ph.D. in mathematics from Brown 3 years later. After teaching at the University of Illinois for a year, he joined the technical staff at Bell Telephone Labs. In 1966, he joined the faculty of the University of Wisconsin, Madison, and taught there until his retirement in 1985. Moore has made outstanding contributions to the logical design of switching circuits, automata theory, graph theory, and database management. Edward
14. 0 ~
0
~'~
0 Y
~
ff
1
v
1 The o u t p u t g e n e r a t e d by the Moore m a c h i n e M  (S,I, O , f , g , so) for the i n p u t s t r i n g a l a 2 . . . a m is g ( s o ) g ( s l ) . . . g ( s m ) , w h e r e si  f ( s i  l , a i ) a n d 1 _< i _< m. F i n d the o u t p u t produced by the m a c h i n e in Exercise 13 for each input. 15. 011
16. 1010
17. 10001
18. 1101101
19. L e t L be a r e g u l a r language. Prove t h a t L R  { x n . . . x ] I x l . . . x , , ~ L} is also regular. **20. An F S M M  (S,I, O , f , g , so) is s i m p l y m i n i m a l if no o u t p u t rows in its t r a n s i t i o n table are identical. If l S i  n, I I I  m, a n d I O i  P, how m a n y simply m i n i m a l F S M s are possible?
Computer Exercises Write a p r o g r a m to do each task, w h e r e E  {a,b }. 1. D e t e r m i n e if a s t r i n g over Z" 9 Begins w i t h aa.
9 C o n t a i n s exactly one a.
9 E n d s with bb.
9 C o n t a i n s at least one a.
9 C o n t a i n s aba as a substring. 9 Has its n u m b e r of a ' s c o n g r u e n t to 1 mod 3.
Chapter Summary
801
9 Has an even n u m b e r of a ' s a n d b's. 9 Has b o t h its n u m b e r of a's a n d b's c o n g r u e n t to 1 mod 3. 2. Let m d e n o t e the n u m b e r of a ' s a n d n t h e n u m b e r of b's in a s t r i n g over E. Read in a word over E and see if it has: 9 m  0(mod 5)
9 m _ 3(mod 5)
9 m _ 0(mod 3) or m  l ( m o d 3)
9 m  0(mod 3) or m  2(mod 3)
9 m0(mod3)
9 m0(mod3)
andnl(mod3)
andn=2(mod3)
3. For a DFSA with n (< 10) states, labeled 1 t h r o u g h n, r e a d in its n u m b e r of states n a n d t r a n s i t i o n table. Read in a sequence of i n p u t strings over Z and d e t e r m i n e if each is accepted by t h e DFSA. 4. I m p l e m e n t the u n i t delay m a c h i n e in E x a m p l e 11.41. 5. Read in two b i n a r y n u m b e r s and use the F S M in E x a m p l e 11.42 to c o m p u t e t h e i r sum. 6. By m e a n s of the s y n t a x d i a g r a m in F i g u r e 11.31, d e t e r m i n e if a s t r i n g of c h a r a c t e r s r e p r e s e n t s a valid integer. 7. Ascertain with the s y n t a x d i a g r a m in F i g u r e 11.32 w h e t h e r a s t r i n g of c h a r a c t e r s r e p r e s e n t s a valid real n u m b e r . (Excluding t h e exponential form.) 8. Read in the n u m b e r of s t a t e s n, the t r a n s i t i o n table, a n d a set of i n p u t strings for an FSM with n (< 10) states, labeled 1 t h r o u g h n. P r i n t the o u t p u t produced by each i n p u t string. 9. U s i n g a DFSA with n (< 10) states, labeled 1 t h r o u g h n, read its n u m b e r of s t a t e s n and t r a n s i t i o n table. D e t e r m i n e the c o r r e s p o n d i n g r e g u l a r grammar.
Exploratory Writing Projects Using library a n d I n t e r n e t resources, write a t e a m r e p o r t on each of t h e following in y o u r own words. Provide a w e l l  d o c u m e n t e d bibliography. 1. Discuss how B N F rules are used to define p r o g r a m m i n g l a n g u a g e s such as C § § and Java. 2. Discuss T u r i n g m a c h i n e s and C h u r c h ' s thesis. 3. Explain how v e n d i n g m a c h i n e s , slot m a c h i n e s , a n d garage door openers can be modeled by FSAs. 4. Write an essay on Kleene closure. 5. Write an essay on different types of F S M s a n d t h e i r applications.
Chapter II
802
FormalLanguages and FiniteState Machines
Enrichment Readings
1. W. J. Barnier, "FiniteState Machines as Recognizers," The UMAP Module 671 (1986), pp. 209232. 2. B. Hayes, "On the FiniteState Machine, a Minimal Model of Mousetraps, Ribosomes, and the Human Soul," Scientific American, Vol. 249 (Dec. 1983), pp. 1928, 178. 3. J. E. Hopcroft and J. D. Ullman, Introduction to Automata Theory, Languages, and Computation, AddisonWesley, Reading, MA, 1979. 4. Z. Kohavi, Switching and Finite Automata Theory, 2nd ed., McGrawHill, New York, 1978. 5. P. Linz, An Introduction to Formal Languages and Automata, D. C. Heath, Lexington, MA 1990. 6. J. C. Martin, Introduction to Languages and the Theory of Computation, 2nd ed., McGrawHill, New York, 1997. 7. M. Sipser, Introduction to the Theory of Computation, PWS, Boston, 1997. 8. W.A. Wulfet al., Fundamental Structures of Computer Science, AddisonWesley, Reading, MA, 1981, pp. 164.
Chapter 12
B o o l e a n A l g e b r a and Combinatorial Circuits Mathematics is music for the mind; music is mathematics for the soul. ANONYMOUS
G
eorge Boole's classic An Investigation of the Laws of Thought, published in 1854, led to the development of two closely related areas of mathematics: symbolic logic and the mathematical system called boolean algebra. Chapter i of Boole's book demonstrated how valuable the symbols and the laws of logic are in investigating how we reason in order to reach conclusions. That material will help us in studying this chapter. Until the late 1930s boolean algebra did not seem to have many useful applications. In 1938, Claude E. Shannon, while working at the Massachusetts Institute of Technology, used boolean algebra to analyze electrical circuits, thus opening the door for a world of applications of boolean algebra. Since then, boolean algebra has played a central role in the design, analysis, and simplification of electronic devices, including digital computers. In this chapter we will address some of the interesting problems that boolean algebra handles well: 9 Three switches for a light fixture are in a hallway. If the light is on, it can be turned off by flipping one of the switches off. On the other hand, if it is off, flipping one of the tongues turns it on. What does the circuit look like? 9 Design a circuit to compute the sum of two 3bit numbers. 9 How can boolean algebra simplify a circuit while maintaining the circuit's capabilities? 9 Electronic devices display digits by lighting up a maximum of seven line segments in the adjacent configuration. What kind of circuit will accept the binarycoded decimal expansion of a decimal digit and light up a segment? 803
Chapter 12 Boolean Algebra and Combinatorial Circuits
804
~ ~~  ; ~ ~ ~ _ ~ ~ ~, ' , , ~  ~
C l a u d e E l w o o d S h a n n o n (19162001), a pioneer in artificial intelligence and information theory, was born in Gaylord, Michigan. After graduating from the University of Michigan in 1936, he earned his M.S. in 1937 and Ph.D. in applied mathematics in 1940 from MIT. Shannon was a research mathematician at the Bell Telephone Labs from 1941 to 1957 and became professor of electrical engineering at M I T in 1958. In 1948, he coined the word b i t for binary digit. He received honorary doctorates from many universities: Michigan, Pittsburgh, Princeton, Northwestern, Edinburgh, Oxford, CarnegieMellon, Tufts, and Pennsylvania. He also received numerous honors and awards, including the Nobel Peace Prize, Morris Liebmann Memorial Award, Ballantine Medal, Medal of Honor from the Institute of Electrical and Electronics Engineers (IEEE), and National Medal of Science. Shannon made outstanding contributions to the theory of switching circuits, information theory, cryptography, and artificial intelligence. .~
A boolean algebra is a m a t h e m a t i c a l s y s t e m ; it consists of a n o n e m p t y set S with one or more operations defined on S, and a set of axioms t h a t the elements of S satisfy. A m a t h e m a t i c a l system can be t h o u g h t of as a skeleton, like a h u m a n skeleton. W h e t h e r people are black or white, Caucasian or Chinese, their skeletons have common characteristics. Likewise, concrete examples of a m a t h e m a t i c a l system share common properties. When we study m a t h e m a t ical systems, we need to study properties common to all examples of such systems. Real n u m b e r s form an i m p o r t a n t n u m b e r system. Before formally defining a boolean algebra, we examine two concrete examples. Look for the properties common to both because they will help us define a boolean algebra. Let U b e an a r b i t r a r y set and P(U) its power set. Let A , B , and Cbe any t h r e e elements of P(U). You may recall from Chapter 2 t h a t the union, intersection, and complementation operations on P ( U ) satisfy the following: C o m m u t a t i v e properties 9 ANB=BNA
9 AUB=BUA Associative properties 9 AU(BUC)=(AUB)UC
9 AN(BNC)=(ANB)NC
Distributive properties 9 AN(BuC)
= (ANB)U(ANC)
9 Au(BNC)
= (AuB)N(AuC)
12.1 Boolean Algebra
805
I d e n t i t y properties 9 AUO=A
9 AN
U =A
C o m p l e m e n t properties 9 AUA'U
9 ANA'O
I
T h e n e x t e x a m p l e is a b i t s o p h i s t i c a t e d . ~
L e t D30 b e t h e s e t o f p o s i t i v e f a c t o r s o f 3 0 : D 3 0 D e f i n e t h r e e o p e r a t i o n s @, @, a n d '
 {1, 2, 3, 5, 6, 10, 15, 30}.
o n D30 a s f o l l o w s :
a @ b  l c m {a, b} a  b  g c d {a,b} t at u
3O a
For instance, 2 
3  lcm{2,3}  6  3 
2 Q 3  gcd{2,3} 6'=
2;
1  3 E) 2;
3O ~ =5; 6
6 @ 6'  6 
5  l c m {6, 5}  30;
6 @ 10  l c m { 6 , 10}  30 
10 @ 6;
6 G 10  g c d { 6 , 10}  2 = 10 E) 6; 5' 
3O 5
= 6;
5 Q 5'  5 E) 6  g c d { 5 , 6 }  1.
These operations satisfy the following properties:
C o m m u t a t i v e properties 9 a@b=b•a
9 aQbbQa
Associative properties 9 aO(b@c)=(aGb)oc
9 aQ(bQc)=(aQb)@c
Distributive properties 9 a@(boc)=(aQb)@(a@c)
9 a@(bQc)=(a@b)G(a@c)
I d e n t i t y properties 9 a@
1 =a
9 aQ30a
tRecall that lcm {a,b} denotes the least common multiple of a and b, and gcd {a,b} the greatest common divisor of a and b.
Chapter 12 Boolean Algebra and Combinatorial Circuits
806
Complement properties 9 a@a ~=30
9 a(3a'=l
I
Take a close look at both examples. W h a t do they have in common? In each case, a n o n e m p t y set B contains two special elements ~ ~ and U in Example 12.1; 1 and 30 in Example 12.2. F u r t h e r m o r e , both examples contain two binary operators and a u n a r y operator, satisfying the same 10 properties. These commonalities suggest a m a t h e m a t i c a l system called a boolean algebra, defined below. Boolean Algebra
A b o o l e a n a l g e b r a consists of a n o n e m p t y set B containing two distinct elements 0 and 1, two binary operators + a n d . , and a u n a r y o p e r a t o r ' satisfying the following conditions for all x,y, and z in B: Commutative laws 9 x+y=y+x
9 x.y=y.x
Associative laws 9 x+(y+z)=(x+y)+z
9 x.(y.z)=(x.y).z
Distributive laws 9 x.(y+z)=(x.y)+(x.z)
9 x+(y.z)=(x+y).(x+z)
Identity laws 9 x+0=x
9 x.l=x
Complement laws 9 x+x'=l
9 x.x'=O
In symbols, the boolean algebra is denoted by (B, +,., ', 0, 1) . We clarify a few points before we go any further: 9 The operations +,., a n d ' are called s u m , p r o d u c t , and c o m p l e m e n t a t i o n , respectively. (For instance, in Example 12.1, the binary operators are n and U; the u n a r y operator is complementation.) These operators are generic symbols: the operator + does not stand for addition nor the o p e r a t o r , for multiplication. 9 Since + a n d . are binary operators, both x + y and x .y belong to B for all x and y in B; since' is a u n a r y operator, x' belongs to B for every x ~ B. 9 The elements 0 and 1 are the z e r o e l e m e n t and the u n i t e l e m e n t , respectively. Again, they are generic symbols for the zero element and the unit element, respectively; they need not be the familiar n u m b e r s zero and one. In Example 12.1, the zero element is 0 and the unit element is U; in Example 12.2, they are 1 and 30, respectively.
12.1 BooleanAlgebra
807
9 When it is clear from the context, we designate the boolean algebra (B, +,, ', 0, 1) as the boolean algebra B for convenience. 9 The o p e r a t o r , in x . y is usually omitted for convenience; t h u s x y = x . y . No p a r e n t h e s e s appear when t h e r e is no danger of confusion. F o r instance, (xy) + (xz) = x y + x z and x + y + z = x + (y + z) = (x + y) + z. 9 Precedence rules govern evaluating expressions in boolean algebra: First, parenthesized subexpressions are evaluated. Complementation has the highest priority a m o n g the operators, followed b y . and then +. For example, x y + z x ' = (xy) + [z(x')]. 9 The 10 axioms are paired off in two columns. In each pair, an axiom can be obtained from the other by swapping + w i t h . , and 0 with 1. These are d u a l a x i o m s . For instance, the dual of x + x' = 1 (axiom 9) is x . x' = 0 (axiom 10). (In a boolean algebra, the dual of every t r u e s t a t e m e n t is also true. This property is the p r i n c i p l e o f d u a l i t y . ) According to the definition, a boolean algebra contains at least two elements, the zero and the unit. Consequently we can ask: Does any boolean algebra contain exactly two elements? The next example shows t h a t a n s w e r is yes. Let B  {0, 1 }. Define the operations +, 0+0=0 0.0=0 0'= 1
0+1=1 0.1=0 1'=0
" 3
and'
1+0=1 1.0=0
on
B
follows:
as
1+1=1 1.1=1
[Think of the operators + and 9 as the o r (v) and the a n d (A) operators, respectively.J By T h e o r e m 7.1, the commutative, associative, and distributive laws are satisfied. Clearly, x + 0 = x and x 9 1 = x, for every x e B; therefore, 0 is the zero element and 1 is the unit element. Besides, 0+0'=0+1=1 0.0'=0.1=0
and
1+ 1'= 1+0=
and
1.1'=
1
1.0=0
Consequently, the complement laws are also satisfied. T h u s (B, +,., ', 0, 1) is a boolean algebra. Some f u n d a m e n t a l facts about boolean algebras come from the above axioms. We will find t h e m useful in later discussions. ( U n i q u e I d e n t i t i e s ) The zero element and the unit element of algebra B are unique.
a
boolean
Chapte," 12 Boolean Algebra and Combinatorial Circuits
808
PROOF: T h e zero e l e m e n t in B will be p r o v e d u n i q u e ; t h e o t h e r h a l f will be left as a r o u t i n e exercise. S u p p o s e t h e r e a r e two zero e l e m e n t s , 01 a n d 02, in B. Since 02 is a zero e l e m e n t , 01 + 02 : 01. Likewise, since 01 is a zero e l e m e n t , 02 § 01  02. B u t 02 + 01 = 01 + 02, by t h e c o m m u t a t i v e law. T h e r e f o r e , 01 = 01 + 02 02 § 01  02, SO t h e zero e l e m e n t is u n i q u e . I
•
T h e c o m p l e m e n t of e v e r y e l e m e n t in a b o o l e a n
(Unique Complement) a l g e b r a is u n i q u e .
PROOF: Let x be an a r b i t r a r y e l e m e n t in a b o o l e a n algebra. T h e n by t h e c o m p l e m e n t laws, x + x' = 1 a n d x x ' = 0. If x h a s a second c o m p l e m e n t y, x + y = 1 a n d xy =O. T o s h o w t h a t y = x' (the r e a s o n for each step is given on t h e RHS):
y  yl
Thus the unique.
i d e n t i t y law
= y(x + x')
c o m p l e m e n t law
= yx + yx'
d i s t r i b u t i v e law
= xy + x'y
c o m m u t a t i v e law
= 0 + x'y
c o m p l e m e n t law
 xx' + x'y
c o m p l e m e n t law
 x ' x + x ' y
c o m m u t a t i v e law
= x' (x + y)
d i s t r i b u t i v e law
 x'l
c o m p l e m e n t law
= x'
i d e n t i t y law
complement
of
every
element
in
a
boolean
algebra
T h e n e x t e x a m p l e uses T h e o r e m 12.2. ~
W i t h t h e b o o l e a n a l g e b r a D30 in E x a m p l e 12.2, verify each. (1) 6 @ 6 = 6 (4) 3 @ (3 Q 5)  3
(2) 5 @ 30 = 30 (5) (3 @ 5)' = 3' Q 5'
(3) (5')' = 5 (6) (5 Q 6)'  5' @ 6'
SOLUTION: (1) 6 @ 6  lcm{6,6}  6
(2) 5  30  lcm {5, 30 }  30
3O (3) 5 ' =   = 6 5
(4) 3 G 5  gcd{3,5}  1
So (5')'  6' 
3O 6
= 5
So3@(3Q5)30
1
= lcm {3, 1}  1
is I
12.1
Boolean Algebra
809
(5) 3 @ 5  lcm {3, 5}  15 So (3 @ 5)'  15' =
(6) 5 (9 6 = gcd {5, 6} = 1
3O
So (5 (9 6)'  1' =
15
=2 3' ( 9 5 ' 
3O 1
=30 10(96
=5@6
= gcd {10, 6}  2
= lcm {5, 6}
= (3 @ 5)'
= 5' @ 6'
I
We now establish a few m o r e p r o p e r t i e s of boolean algebras. [ ~ ~ ~ ~ ~ ~
Let x a n d y be a r b i t r a r y e l e m e n t s in a boolean algebra (B, + , . , Then: Idempotent 9
laws
XffX
X
Boundedness
9
XXX
laws
9 x+11
9 x0=0
Involution laws 9
(X')l
9 0'=1
__ X
9 1'0
Absorption laws 9 x +xyx
De Morgan's
9 x(x + y)  x
laws
9 (x + y ) ' = x ' y '
9 ( x y ) '  x' + y '
PROOF: 9 T o p r o v e t h a t x + x  x:
x +x
(x + x ) l
identity law
= (x + x ) ( x + x')  xx
+ x x t Jr x x
c o m p l e m e n t law q XX t
d i s t r i b u t i v e law
 X ~ XX I
i d e m p o t e n t law
=x+0
i d e n t i t y law
~X
identity law
9 To prove that x + 1 x + 1 x
1"
+ (x + x ' )
c o m p l e m e n t law
', 0, 1).
810
Chapter 12 Boolean Algebra and Combinatorial Circuits
= (x + x) + x' = x + x' = 1
associative law i d e m p o t e n t law c o m p l e m e n t law
9 T o p r o v e t h a t (x')' = x:
Since x' is the c o m p l e m e n t of x, x + x' = 1 and x x ' = 0. U s i n g the c o m m u t a t i v e laws, these e q u a t i o n s can be r e w r i t t e n as x'+x=l
and
(12.1)
x'x=O
Since x' is also an e l e m e n t of B, it has a c o m p l e m e n t (x')'. Therefore, x' + (x')' = 1
and
x'(x')' = 0
(12.2)
E q u a t i o n s (12.1) imply x is a c o m p l e m e n t of x'; by E q u a t i o n s (12.2), (x')' is also a c o m p l e m e n t of x'. But, by T h e o r e m 12.2, the c o m p l e m e n t of x' is unique; therefore, (x')'  x. 9 T o p r o v e t h a t x + x y  x" x+xy=xl
+xy
=x(1 +y) = x ( y + 1) =xl ~x
identity law distributive law c o m m u t a t i v e law b o u n d e d n e s s law
9 T o p r o v e t h a t (x + y)'  x ' y "
By the c o m p l e m e n t laws, we m u s t show t h a t (x + y ) + x ' y ' 
1 and
(x + y ) ( x ' y ' )  O.
(1) (x + y ) + x'y'  x + (y + x ' y ' ) = x + (yl + x'y') = x + [y(x + x') + x 'y 'i
= x + [(yx + yx') + x'y'! = x + [xy + (x'y + x ' y ' ) l = (x + x y ) + (x'y + x 'y ')
= x(1 + y ) + x'(y + y ' ) = x l + x'(y + y') = xl + x'l x+x
f
=1 (2) (x + y ) ( x ' y ' )  ( x ' y ' ) ( x + y ) = ( x 'y ') x + (x'y')y = x ( x ' y ' ) + ( x 'y ') y
= (xx')y' + x'(y'y)
associative law identity law c o m p l e m e n t law distributive law c o m m u t a t i v e law associative law distributive law b o u n d e d n e s s law c o m p l e m e n t law identity law c o m p l e m e n t law c o m m u t a t i v e law distributive law c o m m u t a t i v e law associative law
12.1 BooleanAlgebra
811
= (xx')y' + x'(yy')
= Oy' + x'O = y'O + x'O
=0+0 =0
commutative law complement law commutative law boundedness law identity law
Thus, by parts i and 2, x'y' is the complement of x + y; that is, (x + y)' = I
x'y'.
A close relationship exists between the boolean algebras D30 and P(U), where U  {a,b,c}. They are displayed by the Hasse diagrams in Figures 12.1 and 12.2, respectively, which have the same structure. This is not surprising because the algebras are i s o m o r p h i c . [Find an isomorphism f : D 3 0 ~ P ( U ) that preserves the operations; that is, f ( x G y) = f ( x ) U f ( y ) , f ( x Q y) = f ( x ) n f(y), and f( x ' ) = (f(x))'.] F i g u r e 12.1
30
6
15
2
5
1 F i g u r e 12.2
U
{a,b}
{b,c}
{a}
{c}
0 Any boolean algebra, then, has at least two elements, as well as two binary operators and a unary one that adhere to various laws.
812
Chapter 12 BooleanAlgebra and Combinatorial Circuits
Exercises 12.1
Using Example 12.2, evaluate each. 1. 6 @ 1 0
2. 6 Q 1 0
3. 2 @ ( 3 @ 5 )
4. ( 2 @ 3 ) @ 5
5. 3  1 7 4
6. ( 3 Q 5 ) Q 6
7. (3@6)'
8. 3 ' 
10. 5'@ 10'
9. (5 Q 10)'
11. 2 Q 3 @ 5
12. 2 @ 3 G 5
The set D70  {1, 2, 5, 7, 10, 14, 35, 70} of positive factors of 70 is a boolean algebra under the operations @, Q, and ' defined by x @ y = lcm {x, y}, x  y = gcd {x, y}, and x' = 70/x. Compute each. 13. 7 @ 5
14. 2 G 7
15. (5@7)'
16. 5 ' Q 7 '
17. ( 7 
18. 7'@2'
19. 1 0 @ 1 0
20. 7 Q 7
Using the boolean algebra D70, verify each. 21. ( 5 ' ) ' = 5
22. 7 @ ( 7 Q 5 ) = 7
23. 5 Q ( 5 @ 7 ) = 5
24. ( 5 @ 7 ) ' = 5 ' G 7 '
25. With the boolean algebras in Examples 12.1, 12.2, and 12.3, and D70, predict the n u m b e r of elements in a finite boolean algebra. 26. Define the operations + , . , and ' on B = {0, 1} as follows: x § y = m a x { x , y } , x . y = min{x,y},0'  1, and 1' = 0. Is ( B , § ',0,1) a boolean algebra? Determine if (S, +,, ', 0, 1) is a boolean algebra for each subset S of the boolean algebra D30. 27. {1,2,15,30}
28. {1,6,10,30}
29. {1,3,5,30}
30. {1,5,6,30}
Is (S, u, N,', O, U) a boolean algebra for each subset S of P ( U ) , where U = {a,b,c}?
31. {0, {a}, {b,c}, U}
32. {0, {a}, {b}, {a,b}, U}
33. {0, {b}, {a,c}, U}
34. {0, {c}, {a,b}, U}
35. Define the operations + , . , a n d ' on B = {a, b, 0, 1} so t h a t {B, +,., ', 0, 1) would be a boolean algebra.
Find the dual of each boolean property. 36. 0 '   1
37. x ( x + y )  x
38. (x + y ) z = xz + y z
39. (xy)'  x' + y'
Prove algebraically. 40. The unit element in a boolean algebra is unique.
12.2 Boolean functions
813
41. x x = x
42. x 0 = 0
43. 0 '  1
44. 1' = 0
45. x(x + y) = x
46. (xy)' = x' + y'
47. x + y = (x'y')'
48. x y = (x' + y')'
49. (x + y ) z = x z + yz
50. xy' + x'y = (x + y)(xy)'
51. (xy) + z = (x + z)(y + z)
*52. Let U = {a,b,c}. Define a suitable function f : D 3 0 ~ P ( U ) t h a t preserves the operations; t h a t is, f (x @ y) = f (x) u f (y), f (x Q y) = f (x) n f (y), and f (x') = if(x))'.
This section introduces the concept of a boolean function and illustrates the possible equality of boolean expressions. We also see t h a t t h r o u g h the laws of boolean algebra, boolean expressions can be r e w r i t t e n in a s t a n d a r d form. Since the design and analysis of electronic devices rely on the twoelement boolean algebra 1 ~ (x+y)(xy)'=s [~
U xy = c
FullAdder Fulladders find the sum of any two binary numbers. A f u l l  a d d e r accepts three bits" the two bits xi and Yi in the addends and the carry bit ci, w h e r e i > 1. After accepting these three bits, the fulladder o u t p u t s the s u m bit si and the new carry bit Ci+l. Table 12.15 lays out the values of si and Ci+l for various values of xi, Yi, and ci. According to the table, /
!
!
/
!
/
si  xiYiCi + xiYiCi + xiYiC i + xiYiCi
and !
Ci+l  xiYiCi ~ xiYiC I ~ xiYtiCi + xiYiCi
Consequently, a fulladder can be made with AND, OR, and N O T gates.
T a b l e 12.15 Logic table for a fulladder.
Output
Input xi
Yi
ci
si
0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1
0 1 1 0 1 0 0 1
Ci+l 0 0 0 1 0 1 1 1
But two halfadders will work well together as a fulladder. To find the sum bit si and the carry bit Ci+l, input xi and Yi into a halfadder H1; it produces a sum bit s and a carry bit c. The bits s and ci are t h e n input to another halfadder H2. Their sum is the desired sum bit si. The carry bits from H1 and H2 are sent to an OR gate, which outputs the new carry bit Ci+l. This fulladder appears in Figure 12.32.
12.4 Combinatorial Circuits Figure 12.32
837
sum bit si I I >[ Half [ '1 adder '1 carry=cis sum] bit s~ [ _ _ ~
ci
A fulladder. ,,L
Halfadder
"~
c=xy
We close this section by showing how we can use half and fulladders to compute the sum of two nbit numbers. ~
Using a halfadder and fulladder, design a circuit t h a t computes the sum of two 3bit n u m b e r s x = X2XlXo and y = Y2ylYO. SOLUTION: A halfadder accepts two bits o u t p u t i n g the sum and carry bits. On the other hand, a fulladder accepts three bits to produce the sum and carry bits. The circuit in Figure 12.33 produces the sum s  s 3 8 2 8 1 8 0 .
Figure 12.33 Xo
SO
~,
Halfadder
Yo
Sl Xl
p.
Yl
Fulladder r
X2
.
.
.
.
.
.
Fulladder
.
Y2
.~
82
83
I
The range of possible combinatorial circuits expands as logic gates from half and fulladders describe m a n y electrical systems t h r o u g h the production of bit sums.
Exercises 12.4
When will the combinatorial circuit for each boolean expression produce 1 as the output?
1. x'
2. x + y
3. xy
Find the output produced by the combinatorial circuits in Exercises 413. 4. x y
>
838
Chapter 12 BooleanAlgebra and Combinatorial Circuits 5o
JC
~t>~*'~) o
x
v
_
Y
~
E>
8"
xy
9,
x
z
10. x
:i
V
~
i~~] ~
~____
12.4
C o m b i n a t o r i a l Circuits
839
11. x
rt ~~
j v v v
12.
x
:[
I
13.
I
x
:D
I,
))
,
"[
I
r
:E)
1417. Devise a logic table for each circuit in Exercises 69. 1821. Construct a combinatorial circuit for each boolean expression in Exercises 9, 10, 14, and 15 in Section 12.2. Using only NAND gates, design a combinatorial circuit that receives x and y as input signals and outputs: 22. x'
23. x + y
24. xy
2527. Redo Exercises 2224, using only NOR gates.
(~hapter12 BooleanAlgebra and Combinatorial Circuits
840
28. Make a combinatorial circuit for a hallway light fixture controlled by two switches x and y. Assume the light is off when both switches are. By means of the circuit in Figure 12.33, add each pair of binary numbers. 29. 101, 100
30. 110, 011
31. 110, 101
32. 101, 111
Using the fulladder in Figure 12.32, verify each algebraically. I
f
I
X 1
I
33. si  xiYiCi + xiylci + xiYiC i + iYiCi 3 4 . Ci+l  xiYiCi + xiYiC I + xiylci + xlyici.
Design a halfadder with: 35. NAND gates.
36. NOR gates.
With the set of gates {AND, OR, NOT} functionally complete, every combinatorial circuit can be represented by a boolean expression. So simplifying such expressions amounts to simplifying, or minimizing, circuits. Boolean algebraic laws can reduce boolean expressions, as two examples with three variables will demonstrate. However, Karnaugh maps give us an easier method. The DNFs of expressions of up to fbur variables will shrink through this graphic procedure. Section 12.2 explored boolean expressions that yield the same value no matter how we combine the values of variables. Consequently, the corresponding combinatorial circuits yield the same output for the same set of input values. Accordingly, we make the following definition. Equivalent Combinatorial Circuits Two combinatorial circuits are e q u i v a l e n t if the corresponding boolean expressions are equal.
~ F i g u r e 12.34
Are the combinatorial circuits C1 and C2 in Figure 12.34 equivalent? x
:[} y .________~
Combinatorial circuit C1
Combinatorial circuit C2
SOLUTION: First, find the boolean expressions representing the two circuits: E1 x t Y ' and E2 = (x + y)', respectively. Since E1 = E2 by De Morgan's law, the two combinatorial circuits are equivalent, m 

12.5 Minimization of Combinatorial Circuits
841
Circuit C1 contains three gates; C2, only two. Accordingly, C2 is s i m p l e r than C1. Through two methods, algebraic and graphical, we can find a circuit C2 equivalent to but simpler than a given circuit C1. Since a gate in a combinatorial circuit corresponds to a boolean operator, the key to both procedures lies in locating a boolean expression with fewer boolean operators and literals. We can simplify a boolean expression through the laws of boolean algebra, as the next two examples demonstrate. However, we can use this method successfully only if we know the laws of boolean algebra well, so review them as often as needed. ~
Find a simpler combinatorial circuit equivalent to the one in Figure 12.35.
F i g u r e 12.35
SOLUTION: The boolean expression represented by the circuit is f ( x , y , z )  xyz t xyz' + xy'z + x'y'z. Simplify it as much as possible, justifying every step: f (x, y, z)  xyz + xyz' + xy'z + x'y'z = x y ( z + z') + y ' z ( x + x') = xyl + y'zl = xy + y'z
Consequently, we can replace the given circuit by the much simpler version in Figure 12.36. F i g u r e 12.36
I
Chapter 12 Boolean Algebra and Combinatorial Circuits
842
~
Find a simpler combinatorial circuit equivalent to the one represented by the boolean function f ( x , y , z ) = xyz + xyz' + xy'z + x'yz + xy'z' (see Example 12.13 also). SOLUTION: f ( x , y , z ) = xyz + xyz' + xy'z + x'yz + xy'z' = xyz + xyz' + xy'z + x'yz + (xy'z' + xy'z') = xyz + (xy'z + xy'z') + (xyz' + xy'z') + x'yz = xyz + xy'(z + z') + xz'(y + y') + x'yz = xyz + x y ' l + x z ' l + x'yz = xyz + xy' + xz' + x'yz = xyz + x(y' + z') + x'yz + xyz = xyz + x(yz)' + yz(x + x') = x[yz + (yz)']+ y z l
=xl +yz =x+yz
Therefore, the simpler twogate circuit in Figure 12.37 can replace the original ninegate one, saving seven gates. F i g u r e 12.37
z
m
Simplifying a boolean expression can be arduous and frustrating. To a large extent, success depends on grouping the various terms properly and then ingeniously applying the laws of boolean algebra. But there is another way. Maurice Karnaugh developed the graphical method of the K a r n a u g h m a p at Bell Laboratories in 1953 to simplify the DNFs of boolean expressions. The essence of the Karnaugh map lies in grouping minterms that differ by exactly one literal. For instance, the minterms xy'z and xy'z' differ in exactly one literal and their sum can shrink: xy'z + xy'z' = xy'(z + z') = x y ' l = xy' (see also Example 12.29). The Karnaugh map has a rectangular grid of squares. Each square stands for a possible minterm in the DNF of the boolean expression that represents
i2.5 Minimization of Combinatorial Circuits
843
M a u r i c e K a r n a u g h (1924), a physicist, was born in New York City. After graduating from the City College of New York in 1948, he received his Ph.D. in physics from Yale in 1952. Karnaugh was a member of the research staff at Bell Telephone Labs from 1952 to 1966, and a research and development manager at the Federal Systems Division of A T & T during the next 4 years. In 1970, he joined the research staff at IBM. His research interests include knowledgebased computer systems.
the circuit. Each contains a 1 if the corresponding m i n t e r m exists in the expression. A 2 • 2 K a r n a u g h map can help simplify the D N F of a boolean expression in two variables x and y. Since four possible m i n t e r m s are feasible m x y , xy', x'y', and x ' y m the map consists of four squares. Label the squares so the m i n t e r m s in any two adjacent squares in each row and column differ by exactly one literal; two such squares are a d j a c e n t . For instance, the squares xy and xy' are adjacent, whereas the squares xy and x'y' are not. The resulting a r r a n g e m e n t appears in Table 12.16. y
y'
X
xy
xy'
X p
x'y
x'y'
T a b l e 12.16
Find the K a r n a u g h map for each boolean expression. (1) xy + x'y + x'y'
(2) xy' + x'y
SOLUTION: Place a 1 in the square c o r r e s p o , d i n g to each m i n t e r m and leave the other squares blank, as shown in Tables 12.17 and 12.18, respectively.
y'
Table 12.17
K a r n a u g h map for xy + x'y + x'y'.
X X p
Chapter 12 Boolean Algebra and Combinatorial Circuits
844 Table 12.18
Karnaugh map for xy' + xty.
x x'
y
y'
1 1
1
m
The sum of minterms in adjacent squares can be simplified. By drawing a loop around such blocks containing l's, always beginning with the largest block, we can always reduce the minterms corresponding to the blocks, as Example 12.31 suggests. ~
Using a Karnaugh map, simplify the boolean expressions in Example 12.30, if possible. SOLUTION: (1) First, loop off adjacent squares containing l's. There are two such blocks in Table 12.19. Therefore,
xy + x'y + x'y' = (xy + x'y ) + (x'y + x'y')
= (x + x')y + x'(y + y') = ly + x'l
=y+x' =x+y
(With a little practice, we can read this answer directly from the map.) y
Table 12.19
y'
X X p
(2) The squares corresponding to the minterms xy' and x'y are not adjacent, so each forms a block by itself in Table 12.20. Consequently, the expression xy' + x'y cannot be simplified.
TP
Table 12.20
x p
m
A Karnaugh map for three variables x, y, and z expands to a 2 • 4 rectangular grid. Each of the eight squares in Table 12.21 corresponds to a possible minterm in x, y, and z. Again, two squares are a d j a c e n t if the
12.5 Minimization of Combinatorial Circuits
845
corresponding m i n t e r m s differ by exactly one literal. For instance, the s q u a r e s x y ' z a n d x y z are adjacent. To see this geometrically, cut out the grid, bend, and glue the two shorter edges to form the cylinder in Figure 12.38. Two adjacent squares share a boundary. T a b l e 12.21
F i g u r e 12.38
X
yz xyz
yz' xyz t
y' z' xy tz ~
y' z xff z
Xp
x'yz
x'yz'
x'y'z'
x'y'z
xyz_.~_..... ~
r
xy'z
I
,, x'y'z
To simplify a sum of m i n t e r m s in x, y, and z, identify blocks of m i n t e r m s that can be combined by drawing loops around them. Always begin with the largest block and use the m i n i m u m n u m b e r of blocks. This time a block may be a I x 1, 1 x 2, 1 x 4, 2 x 2, or a 2 x 4 rectangle. An example of each possibility appears in Figure 12.39. F i g u r e 12.39
A few possible blocks in a 2 • 4 K a r n a u g h map.
yz
yz"
y' z'
y'z
yz
X
X
X'
X'
yz'
(a)
y' z'
y'z
y'z"
y'z
(b) yz
yz"
y'z '
y "z
X'
(c) yz
yz'
y'z"
y'z
yz
yz'
X Xp
Xp
(d)
(e)
C,hapter 12 Boolean Algebra and Combinatorial Circuits
846
Notice that the sum of the minterms in the block in Figure 12.39d can be simplified as follows: xyz' + xy'z' + x'yz' + x'y'z'  yz'(x + x') + y'z'(x + x') = y z ' l + y'z' 1 = yz' + y'z' = (y+y')z' = lz' 
Z/
The next example provides two additional exercises in simplifying boolean expressions. ~
Simplify each boolean expression with a Karnaugh map. (1)
E 1 
xyz + xyz' +xy'z + x~y'z
(2)
E2
xyz +xyz' + xy'z + x~yz + xy'z'

SOLUTION:
Figures 12.40 and 12.41 show the Karnaugh maps for the two expressions. yz
F i g u r e 12.40
yz"
y'Z'
0
x p
X
F i g u r e 12.41
y'Z
yz
yz'
y'z'
y'z
~1'~~
~1
(1) From Figure 12.40, E1  (xyz + xyz') + (xy'z + x~y'z) = xy(z + z') + y'z(x + x') = xyl + y'zl = xy + y'z
(also see Example 12.28)
12.5 Minimization of Combinatorial Circuits
847
(2) Since a 1 occurs in both loops, count the corresponding m i n t e r m xyz twice using the idempotent law:
E2  (xyz + xyz' + xy'z' + xy'z) + (xyz + x'yz) = x[y(z + z') + y'(z' + z)] + (x + x')yz = x(yl + y ' l ) + lyz
 x ( y + yt) + yz = x + yz
(also see Example 12.29)
Again, with a little experience and patience, we can read these expressions directly from the maps. I Finally, a K a r n a u g h map for four variables w, x, y, and z encompasses the 4 x 4 grid of Table 12.22. Each of the 16 squares r e p r e s e n t s a possible m i n t e r m in w, x, y, and z. Geometrically, the grid can be cut, bent, and glued to form the doughnutshaped surface called a t o r u s . Two squares are adjacent if they share a border on the torus. The cells wxy'z and wxyz are adjacent; wxyz and wx'yz' are not. Table
12.22
K a r n a u g h map for xy + x'y + x'y'.
yz
yz'
yP z p
yP z
wxyz
wxyz'
wxy'z'
wxy'z
wx 'yz
wx'yz'
wx'yz'
wx'y' z
WPX p
WlxIyz
w'x'yz'
w'x'y'z'
w'x'y'z
w'x
w'xyz
wl xyz t
wf xyf z p
wl xyl z
wx wx' .
As was done with two and threevariable expressions, place a 1 in the square corresponding to each m i n t e r m in the boolean expression and loop off the l ' s into blocks of m i n t e r m s t h a t can be combined, always looking for the largest block first and using as few blocks as possible. The blocks useful for minimization are 1 x 1, 1 x 2, I x 4, 2 x 2, 2 x 4, and 4 x 4. F o u r such blocks are highlighted in Figure 12.42. For example, the sum of the m i n t e r m s in Figure 12.42a can be simplified:
wxyz + wxy'z + w'xyz + w'xy'z = wxz(y + y') + w'xz(y + y')  WXZ + W lXZ
= (w + w')xz  XZ
We conclude this section with an example t h a t illustrates how to simplify fourvariable boolean expressions using a K a r n a u g h map.
848
Chapter 12 Boolean Algebra and Combinatorial Circuits
F i g u r e 12.42
yz
yz'
y'z'
y'z
yz
WX
wx
WX ~
WX p
W PX p
W PXp
WPX
w'x
yz'
(a)
yz
yz'
y'z
y'z"
y'z
(b)
y'z'
y'z
yz
WX
wx
WX p
WX p
W PX p
W PX~
W'X
w'x
yz'
(c) ~
y'z p
(d)
Using a Karnaugh map, simplify each boolean expression. (1) E1  wxyz + wxyz' + wxy'z' + wxy'z + w'xyz + w'xy'z (2) E2  wxy'z' + wx'y'z' + wx'y'z + w'x'yz + w'x'yz' + w'xyz + w'xyz' SOLUTION:
(1) Place a 1 in the square for each minterm in the expression. Loop off each block, beginning with the largest. The Karnaugh map is shown in Figure 12.43. Accordingly, the expression E1 can be pruned: F i g u r e 12.43
yz ~
wx
yz'
y'z'
1
1
yPz ~
p
wx WX p WPX p
E1  (wxyz + wxyz' + wxy'z' + wxy'z) + (w'xyz + w'xy'z) = wxD,(z + z') + y'(z' + z)] + w'xz(y + y') = wx(y + y') + w'xz  WX
~
W lXZ
12.5 Minimization of Combinatorial Circuits
Figure 12.44
yz
yz"
y'z'
849
y'z
WX WX t W'X'
w'x
(2) Using the Karnaugh map in Figure 12.44, we have: E2 = (wxy'z' + wx'y'z') + (wx'y'z I + wx'y'z) + (w'x'yz + w'x'yz' + w'xyz + w'xyz') = wy'z'(x + x') + wx'y'(z + z') + [w'x'y(z + z') + w'xy(z + z')] = wy'z' + wx'y' + (w'x'y + w'xy) = wy'z' + wx'y' + w'y(x + x') !
t
= wy z + wx'y' + w'y
m
These cases suggest that Karnaugh maps simplify boolean expressions more easily than algebraic laws, especially when variables are few. Exercises 12.5
Simplify each boolean expression using the laws of boolean algebra. 1. xy + xy'
2. x(x + y) + xy'
3. (x + y )xy'
4. xy + xy' + x'y'
5. x'yz + x'y'z' + x'yz' + x'y'z
6. xy'z' + x'y'z' + xy'z + x'y'z
7. (x + y)(x + y + z)xy
8. (x + y + z)xyz
9. (x + y)(y + z)(z + x)
10. (xy + yz + zx)xyz
11. (x + y)(x' + y)(x + y')
12. (x + y' + z)(x + y + z')xy'z'
13. (x + y)(y + z)(z + x)xyz
14. (x + yz)(y + zx)(z + xy)
15. wxyz + w'xy'z' + wxyz' + w'xy'z
16. wx' yz + wx' yz' + w 'x' yz' + w'xyz'
Find the boolean expression represented by each Karnaugh map. 1"I.
y y' x[111]
18.
x
y y~ 1[
19.
y
x
y'
20.
y
y'
1 1
Display each sum of minterms in a Karnaugh map. 21. xy + x'y'
22. x'y + xy'
850
Chapter 12 Boolean Algebra and Combinatorial Circuits
Using a Karnaugh map, simplify each sum of minterms. 24. xy + xy' + x'y'
23. xy + xy'
Find the boolean expression represented by each Karnaugh map. 25. yz yz' y'z' y'z 26. yz yz' y'z' y'z x
1
1
x'
1
x
1
1
x'
1
1
1
.
27.
yz yz' y'z ~ y~z
!
28.
yz yz' y'z' y'z
x
1
1
x
1
x'
1
1
x'
1
]
1
Using a Karnaugh map, simplify each boolean expression. 29. xy' z + xy' z'
30. xyz + xy' z + x' yz + x' y' z
31. xy'z' + xy'z + x'y'z' + x'y'z
32. xyz + xyz' + x'y'z' + x'y'z
333{}. Using a Karnaugh map, simplify the boolean expressions in Exercises 2528. Find the boolean expression represented by each Karnaugh map.
37.
yz yz' y'z' y'z 1 1 1 1 1 1 1 1
38.
wx wx' w'x' w'x
yz yz' y'z' y'z 1 1 1 1 1 1
40.
wx wx' w'x' w'x
39.
wx wx' w'x' w'x wx wx' w'x' w'x
yz yz' y'z' y'z 1 1
i
1
yz yz' y'z' y'z 1 1 1 1 1 1
Represent each sum of minterms in a Karnaugh map. 41. wxy'z + w'xyz
42. wxyz + wxy'z + w'xyz + w'xy'z
43. wxy'z + wx'y'z + w'xy'z + w'x'y'z 44. wx'yz'+wx'y'z' +w'x'yz'+w'x'y'z' 4548. Using a Karnaugh map, simplify the boolean expressions in Exercises 3740. Using a Karnaugh map, simplify each boolean expression. 49. wxyz + wx'yz + w'x'yz + w'xyz 50. wx'yz' + wx'y'z' + w'x'yz' + w'x'y'z' 51. wx'yz + wx'yz' + wx'y'z' + w'x'y'z' + w'xy'z' + w'xy'z 52. wxyz + wxyz' + wxy'z' + wxy'z + wx'y'z + w'x'y'z + w'xy'z
12.6 Don'tCare Conditions
851
Occasionally, combinatorial circuits may occur that do not accept certain combinations of input signals. In other words, a boolean function f can be generated for which some value combinations never occur as input. Consequently, we d o n ' t care about corresponding o u t p u t values of f in such cases; they remain u n s p e c i f i e d . Such functions are i n c o m p l e t e l y s p e c i f i e d , and such input values are d o n ' t c a r e c o n d i t i o n s . Consequently, to simplify the sum of m i n t e r m s of such a combinatorial circuit with a K a r n a u g h map, place a d in each cell corresponding to a don't care condition. This simply means that the corresponding value of the function can be arbitrarily assigned: the m i n t e r m may or may not enter the simplification process. Whenever the t e r m can help minimization, its d counts as a 1 in the otherwise standard procedure. Three examples will illustrate this technique. Simplify the boolean expression E represented by the K a r n a u g h map in Figure 12.45. F i g u r e 12.45
yz
yz"
y'z"
y'z
wx
d
d
d
d
wx"
d
d
w'x"
1
1
w'x
1
1
, .
SOLUTION: First, draw a loop around each block as usual. Since the top row contains no l's, exclude it in the minimization process; t h i n k of its cells as blanks. The resulting blocks are shown in Figure 12.46. You may verify that: F i g u r e 12.46 WX
yz
yz'
y 'z"
y' z
d
d
d
d
WX p
w'x'
C
w'x
E = (wx'yz + wx'yz' + w'x'yz + w'x'yz') + (w'xyz + w'xy'z) = x'y + w'xz N o t e : If the top row counts as a block, E  w x + x ' y + w ' x z . Thus, w x + x ' y + w ' x z  x ' y + w ' x z for all combined values of the variables for which the
852
Chapter 12 Boolean Algebra and Combinatorial Circuits
expression E is specified. As a result, the solution depends on a judicious choice of d's. m The next two examples provide interesting applications of don't care conditions. A system to represent decimal numbers is to replace each decimal digit with its binary version. Four bits are needed to encode the 10 digits, as Table 12.23 shows. With Table 12.23, 345 can be encoded as 001101000101. This binary representation is the b i n a r y c o d e d d e c i m a l ( B C D ) e x p a n s i o n . Although there are 16 possible arrangements of four bits, only 10 of them appear in the table. The others (see Table 12.24) are never used, meaning those BCD expansions manifest don't care conditions. T a b l e 12.23
Decimal digit
Binary representation
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
T a b l e 12.24
Decimal number
Binary representation
10 11 12 13 14 15
1010 1011 1100 1101 1110 1111
We would like to design a combinatorial circuit that will accept four input signals w, x, y, and z, and output 1 if they represent the BCD expansion of a decimal prime number < 10 (see Figure 12.47). That is, we would like to develop a boolean function f such that f(w,x,y,z) equals 1 if (wxyz)two is the BCD expansion of a prime number and equals 0 otherwise. Combining Tables 12.23 and 12.24, Table 12.25 reveals the logic table of the function f. Since the numbers 10 through 15 do not apply, they generate
853
12.6 Don'tCare Conditions
F i g u r e 12.47
W
prime number detector
X
y
>
f(w, x, y, z)
z
T a b l e 12.25
BCD input
Decimal value
W
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Output
x
y
z
0 0 0 0 1 1 1 1 0 0
0 0 1 1 0 0 1 1 0 0
0 1 0 1 0 1 0 1 0 1
0 0 1 1 1 1
1 1 0 0 1 1
0 1 0 1 0 1
don't care conditions
d o n ' t care c o n d i t i o n s , s i g n a l e d by t h e d ' s in t h e last c o l u m n . (Recall t h a t dcells crop u p in t h e m a p only if h e l p f u l in m i n i m i z a t i o n . ) T h e blocks a r e s h o w n in F i g u r e 12.48. It follows t h a t :
f ( w , x , y , z ) = (wxyz + wxy'z) + (wx'yz + wx'yz' + w'x'yz + w'x'yz') + (w'xyz + w'xy'z) = wxz(y + y') + wx'y(z + z') + w'x'y(z + z') + w'xz(y + y') = wxz + wx'y + w'x'y + w'xz
F i g u r e 12.48
wx
yz
yz"
y"z"
y'z
d
d
d
d
wx W~X p
w,x
C
854
Chapter 12 Boolean Algebra and Combinatorial Circuits
= xz(w + w') + x'y(w + w') = x z +x'y Figure 12.49 gives the circuit. Figure 12.49
x
~ ~>~
~ ~
f(w, x, y, z,)=x'y+xz
r
m
The next example presents a familiar application of don't care conditions to everyday life. ( D i g i t a l D i s p l a y s ) E l e c t r o n i c devices such as modern calculators, microwave ovens, and video cassette recorders display digits by lighting up a maximum of seven line segments, labeled a through g in Figure 12.50. Figure 12.51 provides the display strategy: the digit 0 lights up the segments a, b, c, d, e, and f; 6 lights up the segments c, d, e, f, and g; and SO o n .
F i g u r e 12.50
a
rl lb el Ic d
F i g u r e 12.51
@ lq P, 0
1
2
5
6
7
3
89 8
4
9
Design a combinatorial circuit to accept the BCD expansion (wxyz)two of a decimal digit and light up a segment s. In other words, develop a boolean expression to define the boolean function f ( w , x , y , z ) = s. As in Example 12.35, 16 arrangements are available. However, since there are only 10 digits, six arrangements are don't care conditions; they are listed in Table 12.26. (Verify them.) For instance, suppose we wish to find a minimal boolean expression for the boolean function f(w,x,y,z) = a. Table 12.26 gives the corresponding
855
12.6 Don'tCare Conditions
Table 12.26
Inputs
BCD input D e c i m a l digit
w
x
y
z
a
b
c
d
e
f
g
0 0 0 0 0 0 0 0 1 1
0 0 0 0 1 1 1 1 0 0
0 0 1 1 0 0 1 1 0 0
0 1 0 1 0 1 0 1 0 1
1 0 1 1 0 1 0 1 1 1
1 1 1 1 1 0 0 1 1 1
1 1 0 1 1 1 1 1 1 1
1 0 1 1 0 1 1 0 1 0
1 0 1 0 0 0 1 0 1 0
1 0 0 0 1 1 1 0 1 1
0 0 1 1 1 1 1 0 1 1
DNF, a  w'x'y'z' + w'x'yz' + w'x'yz + w'xy'z + w'xyz + wx'y'z' + wx'y'z, which leads to t h e K a r n a u g h m a p in F i g u r e 12.52. T h e c a r t o g r a p h i c a l c o n c l u s i o n , a = w + x'y + x'y'z' + w'xz, yields t h e d e s i r e d c i r c u i t in F i g u r e 12.53.
F i g u r e 12.52
yz
wx
yz '
y' z'
y'z
d
Figure 12.53 X
f(w, x, y, z)=a
)
z
)
~>o
I In a c c o m m o d a t i n g t h r o u g h dcells, t h e d o n ' t c a r e c o n d i t i o n s of i n c o m p l e t e l y specified f u n c t i o n s in K a r n a u g h m a p s e m e r g e as s t r o n g w o r k h o r s e s in t h e s e a r c h for c o m b i n a t o r i a l c i r c u i t s t h a t m i m i c b o o l e a n e x p r e s s i o n s .
856
Chapter I~ Boolean Algebra and Combinatorial Circuits
Exercises 12.6
Simplify the boolean expression represented by each K a r n a u g h map.
1.
yz ~ y'z'
yz x
1
y'z
d
d
2.
d
x
1
Xf
yz
yz'
y'z
WX
1
1
WX ~
d
d d
4.
yz
yz'
WX
1
d
d
WX ~
d
1
WlX t
d
d
d
d
U2 PX f
WtX
1
1
d
d
W~X
yz
yz'
y'z'
y'z
d
1
1
1
0
WX
6.
yz
1
WX l
WlX I
1
1
yZ f
y'z'
y'z
1
d
1
1
y'z'
y'z
1
1
y'z'
y'z
d
WX
WX t
w 'x
yz'
XI
y'z ~
0
yz
d
w~x ~
d
w'x
d
Simplify the boolean expression defined by each table. 11,
0
x
y
z
1 1 0 0
0 0 0 0
0 0 1 1
0 1 0 1
d d 1 1
1 1 1 1
0 0 1 1
0 1 0 1
x
y
z
f
0 0 0 0
0 0 1 1
0 1 0 1
1 1 1 1
0 0 1 1
0 1 0 1
Using the BCD expansions of the decimal n u m b e r s 0 t h r o u g h 9, develop a boolean function f ( w , x , y , z ) such t h a t the decimal value of (wxyzltwo is divisible by: 9. Two 11. Two and three
10. T h r e e 12. Two or three
13. Seven or more 14. Not more t h a n five, but not one 1522. Find the D N F s of the boolean functions in Exercises 714. 2328. Generate a minimal boolean expression for each function b t h r o u g h g in Example 12.36.
Chapter Summary
857
Boolean operators and the laws of boolean algebra serve well in designing and simplifying electronic circuits.
Boolean Algebra 9 A boolean algebra i. (Such a m a t r i x is called an u p p e r t r i a n g u l a r m a t r i x . ) T h e n IAI = a l i a 2 2 . . , ann. Solve each equation for f(x). f(x)
1
x
17. f(a)
1
a
f(b)
1
b
=0
If(x)
1
x
x2
If(a)
1
a
a2
1 b
b2
1
c2
18. if(b ) If(c)
c
=0
Exponential functions and their closest relatives, the logarithmic functions, form one of the most i m p o r t a n t classes of functions. They have a wide range of fine applications to computer science, life sciences, m a n a g e m e n t science, physical sciences, and social sciences. For example, an exponential function occurs in the n a t u r a l sciences: E. coli are microscopic unicellular bacteria t h a t multiply by halving into two bacteria about every 20 m i n u t e s u n d e r ideal conditions. Suppose initially there is one bacterium and 20 minutes is unit time. T h e n t h e r e will be two bacteria in one unit time, four in two units, eight in three units, and so on. In general, there will be f ( x ) = 2x bacteria in x units of time, a s s u m i n g none dies during the period. This function belongs to the large class of exponential functions, defined below.
Exponential Function Let a be a positive real n u m b e r different from 1 and x an a r b i t r a r y real number. Then the function f ( x ) = a x is called an e x p o n e n t i a l f u n c t i o n w i t h b a s e a. If a = 1 in this definition, the function becomes the constant function f( x ) = 1, which holds little practical interest. As a result, it is excluded from the definition. Figure A. 1 shows the graphs of several exponential functions with different bases a, where a > 1. They grow faster and more steeply as a increases. We can use such exponential functions to describe growths of populations; consequently, their graphs are called g r o w t h c u r v e s . Figure A.2 shows some exponential graphs y = a x, where 0 < a < 1. Each can be obtained by flipping the corresponding graph y = a x, where a > 1, about the yaxis. *Based on T. Koshy, College Algebra New York, pp. 204241.
and Trigonometry with Applications,
McGrawHill,
A.3 Exponentialand Logarithmic Functions F i g u r e A.1
l
G r o w t h curves y  a x, a>l.
Y ~ ~
875
y = lO x y=7 x y=3 x
~X
~l F i g u r e A.2 Decay curves y = a x, 0 x2 t h e n f ( x l ) > f(x2). If 0 < a < 1, t h e n f is a d e c r e a s i n g f u n c t i o n ; t h a t is, i f x l > x2 t h e n f ( x l ) < f(x2). 9 The xaxis is a h o r i z o n t a l a s y m p t o t e for t h e graph. 9 The g r a p h s y = a x a n d y = a x are reflections of each o t h e r a b o u t t h e yaxis. The next two examples explore some applications of the e x p o n e n t i a l function.
Appendix A
876 ~
Consider the world population (in billions) given by Table A.1. We can use these to draw a smooth curve as in Figure A.3. The graph shows t h a t the world population is growing exponentially.
T a b l e A.1 Yeart
Population P(t)
II
1
1000
1650
1750
1800
1850
1900
1950
1970
1990
2000
0.25?
0.3
0.5
0.7
0.9
1.1
1.6
2.5
3.6
5.2
6.1
F i g u r e A.3 The exponential growth of the world population.
1
1650
1750
1850
1950
2000
Years
Suppose a bowl of n u t r i e n t broth contains 1,000,000 viable bacteria per millimeter. Table A.2 shows the n u m b e r of viable bacteria per millimeter in the broth after an agent has been added to kill them. The population at time t is only onetenth of t h a t at time t  10. Let N(t) denote the n u m b e r of viable bacteria at time t. Then N(t)  1060"it. If t can take on all possible values (at least theoretically), then the graph of N(t) looks like the decay curve in Figure A.4.
T a b l e A.2
Time t (in minutes)
II
N u m b e r of bacteria N(t) II
0
10
20
30
40
50
60
106
105
104
103
102
10
1
m An extremely useful exponential function is f(x) = ex, where e is the irrational n u m b e r 2.718281828 . . . . (The letter e for the base was chosen in *Based on R. M. Thrall et al. (ed.), "Extermination of Bacteria," Some Mathematical Models in Biology, The University of Michigan Press, Ann Arbor, MI, 1967, pp. PE3.1PE3.2.
A.3 Exponential and Logarithmic Functions
Figure A.4
877
Nqt)
106 1
t
Time
O
honor of Euler.) It is established in calculus t h a t e
lim n* oo
The graphs of the functions
f(x)
= ex
( 1)n 1+ n
and
g(x)

e x
appear in Figure A.5.
F i g u r e A.5
y
=e
 e x
x
,11
~X 0
Logarithms Logarithms were invented by the Scottish m a t h e m a t i c i a n J o h n Napier (15501617), to facilitate numeric computations. He published a s u m m a r y of his results in 1614.
Logarithmic Function In the exponential function y = a x , where a > 0 and a #= 1, the exponent x is the l o g a r i t h m o f y t o t h e b a s e a; it is denoted by loga (y) or
878
Appendix A simply loga y. Thus,
logay = x if a n d only i f y = a x
The function g ( x ) = log a x is t h e l o g a r i t h m i c f u n c t i o n w i t h base a. W i t h the base a always positive, y = a x is positive for all values of x. As a result, x = log a y is defined only if y is positive. In o t h e r words, only l o g a r i t h m s of positive n u m b e r s are defined. T h u s the d o m a i n of t h e logarithmic function is (0,~z), a n d the r a n g e is (  ~ , ~ ) . Since the exponential function is bijective w i t h d o m a i n (  ~ , ~ ) a n d r a n g e (0, ~ ) , it has an inverse function. The next t h e o r e m shows t h e inverse is indeed the logarithmic function. T h e logarithmic function g ( x ) function f (x) = a x.
log a x is the inverse of t h e e x p o n e n t i a l

PROOF: By definition a l~ Then
x __ x for every x > 0, and t h a t l o g a ( a x) = x for every x.
(f og)(x)
 f ( log a x)  a l~
 f(g(x))
 x
and (g o f ) ( x ) = g ( f ( x ) )
So the logarithmic function fi
function
= g ( a x) = l o g a ( a x) = x
g
is the
inverse
of t h e
exponential
II
According to this t h e o r e m , we can obtain the g r a p h of y = logax by reflecting t h a t o f y = a x a b o u t the line y = x, as shown in F i g u r e A.6.
Y
F i g u r e A.6
x
Y =a x
IY
2ey =x
/ / // / ~

'
~
/
/ ~/
1/
~X
~x
f a>l
O 1, g is a n i n c r e a s i n g function; if 0 < a < 1, g is a d e c r e a s i n g function. 9 T h e yaxis is a vertical a s y m p t o t e . 9 T h e f u n c t i o n s f ( x ) = a x a n d g ( x ) = loga x are i n v e r s e s of each o t h e r ; t h e i r g r a p h s are m i r r o r i m a g e s of each o t h e r a b o u t t h e line y  x. 9 T h e g r a p h s o f y  loga x a n d y  lOgl/a x are reflections of each o t h e r a b o u t t h e xaxis.
Frequently Used B a s e s T h r e e bases of l o g a r i t h m s , 10, e, a n d 2, are f r e q u e n t l y used. L o g a r i t h m s to t h e base 10 are called c o m m o n l o g a r i t h m s or B r i g g s i a n l o g a r i t h m s , after t h e E n g l i s h m a t h e m a t i c i a n H e n r y Briggs (15611631). T h e i r base is u s u a l l y omitted: log x  log 10 x L o g a r i t h m s to t h e base e are n a t u r a l l o g a r i t h m s a n d a b b r e v i a t e d as ln" In x = log e x L o g a r i t h m s to t h e base 2, which we often use in c o m p u t e r science, a p p e a r as lg: lg x  log 2 x T h e f u n d a m e n t a l p r o p e r t i e s of l o g a r i t h m s are given in the next t h e o r e m . We can prove each u s i n g t h e definition of a l o g a r i t h m . Let a, b, x, a n d y be a n y positive real n u m b e r s such t h a t a r 1, b r 1, a n d n a n y real n u m b e r . Then" (1)
al~
x = X
X
(3) loga  = loga X Y (5) loga 1 = 0 (7) loga (a x) = x logax (9) logb x = log a b

loga Y
(2) (4) (6) (8)
l o g a ( x y ) = loga x + loga y log a ( x n ) ~ n 1Oga X
1Ogaa = 1 1OgaX  1Ogay if a n d only if Xy
m
880
Appendix A The next t h r e e examples i l l u s t r a t e some useful e x p o n e n t i a l a n d logarithmic functions. T h e pH of a solution, which m e a s u r e s its acidity or alkalinity, is defined as t h e negative l o g a r i t h m of t h e h y d r o g e n c o n c e n t r a t i o n x in moles per liter of the solution; t h a t is, pH =  l o g x. At 25~ the p H of p u r e w a t e r is 7; if the pH of a solution falls below 7, t h e solution is acidic, a n d above 7 it is alkaline. C o m p u t e the pH of a solution w h e r e the c o n c e n t r a t i o n of h y d r o g e n ions is 6.7 x 10 4. SOLUTION: pH of the solution   log (6.7 x 10 4) =  log 6.7  log (10 4) = log6.7
+ 4
0.8261 + 4 = 3.1739 The world population (in billions) at any t i m e t (in years) is given by
m
p(t)

6.1e ~176 w h e r e p(0) denotes the population in 2000. At t h e given rate, w h e n will the population double the 2000 level? SOLUTION: P o p u l a t i o n in 2000  p ( 0 )  6.1 billion. We need the value of t w h e n 12.2 (Figure A.7). T h e n
p(t)

6. le 00167t = 12.2 Divide by 6.1" e 0"0167t  2 Then In e 00167t  In 2 0 . 0 1 6 7 t  In 2 ln2 t
m
0.0167 42 y e a r s
F i g u r e A.7 12.2 t=? 2000
6.1
m
A.3 Exponential and Logarithmic Functions
881
The h a l f  l i f e of a radioactive substance is the time required for onehalf of t h a t substance to decay. The a m o u n t of 11C, an isotope of carbon, present at a future time t (in months) is given by A ( t )  100e 00338t. Find the halflife of the material. SOLUTION: Original a m o u n t = A(0)  100 grams. The halflife is the value of t when A ( t )  89  50 (Figure A.8). T h e n 100e 0"0338t  50
Divide by 100" e
0.0338t

1
2
Then In e 00338t  In 1 2  0 . 0 3 3 8 t   In 2 t
__
ln2 0.0338 20.5 m o n t h s
F i g u r e A.8
t=?
II
Exercises A.3 Prove each, where f denotes the exponential function. 1. f ( x + y ) = f ( x ) . f ( y )
3. f is injective,
2. f ( x  y ) 
f(x)
f(y)
4. The logarithmic function g ( x ) = loga x is injective.
882
Appendix A Prove, by contradiction, that each is an irrational number.
5. log 2
6. log 3
7. The concentration of hydrogen ions in a solution is 3.76 • 10 s. Compute the pH of the solution. 8. The pH of a solution concentration.
is 5.3575.
Compute
its hydrogen
ion
The intensity of sound the h u m a n ear can hear is measured in decibels, named after Alexander Graham Bell (18471922), the American scientist who invented the telephone. The n u m b e r of decibels in a sound of intensity I is given by I B  10 log I0 where I0 is the standard intensity of 1012 watts per square meter (W/m 2). Find the number of decibels in: 9. Threshold of hearing (10 12 W/m 2) 10. Normal conversation (10 6 W/m 2) 11. Street traffic (10 5 W/m 2) 12. Two sounds of intensity 11 and I2 have decibels B 1 and B2, respectively. I1 Show that B1  B2  10 log I2 13. The magnitude of a star is a measure of its brightness: the brighter the star, the smaller the magnitude. If b l and b2 denote the magnitudes of two stars, B1 and B2 their respective brightnesses, B1 bl  b2 = 2.512 log B~. The brightest stars, Aldebaran and Sirius, have magnitudes 1 and  1.6, respectively. How much brighter is Sirius than Aldebaran? The absolute magnitude M of a star, its apparent magnitude m, and the stellar distance d (in parsecs) from the earth are related by the formula rn = M § 5 log 0.1d. 14. Compute the stellar distance of Aldebaran if its absolute magnitude is  0 . 2 and its apparent magnitude 0.86. 15. The difference m  M is the d i s t a n c e m o d u l u s of the star. The distance modulus of Sirius is 2.86. Find its stellar distance. 16. With R u b i k ' s C u b e , a popular game, the goal is to turn its faces to one particular configuration. The minimum number of turns required from 4.3 • 1019 some position to the desired state is 1 + log15 ( 18 ). Compute ~ it. (C. Kluepfel, 1982)
883
A.4 GeneratingPermutations and Combinations
Sections 6.2 t h r o u g h 6.5 presented various types of p e r m u t a t i o n s and combinations, as well as formulas for computing the n u m b e r of rpermutations and rcombinations of a finite set S. In this section, we will study algorithms which e n u m e r a t e them.
Generating Permutations For convenience, we choose S = {1, 2 , . . . , n }. We shall present an algorithm for generating the various p e r m u t a t i o n s of elements of the set. The algor i t h m is based on l e x i c o g r a p h i c ordering, the same type used to arrange words in a dictionary.
Lexicographic Order In lexicographic ordering, a p e r m u t a t i o n A = a l a 2 . . , B  b i b 2 . . , b,~ if:
an
is less than (or
precedes) a p e r m u t a t i o n 9
al < b l o r
9 ai  bi
for 1 _< i < k  1, and ak < bk.
This is precisely the order we use to alphabetize words (of the same length) in the dictionary. For instance, the word c o m p u t e precedes the word p e r m u t e and the word e s t a t e precedes the word e s t e e m . Consider the p e r m u t a t i o n a l a ' z a 3 a 4  2134 and b l b 2 b 3 b 4  2143 of the set {1,2,3,4}. They agree in the first two positions: al = bl and a2 = b2. But they differ in the third positions: a3 r b3. Since a3 < b3, the p e r m u t a t i o n 2134 precedes the p e r m u t a t i o n 2143 in lexicographic ordering, m Recall that there are 4! = 24 p e r m u t a t i o n s of the set {1, 2, 3, 4}. They are listed in Figure A.9 in lexicographic order columnwise, beginning with 1234. (Can you find a p a t t e r n for e n u m e r a t i n g them?)
Figure A.9
1234 1243 1324 1342
1423 1432 2134 2143
2314 2341 2413 2431
3124 3142 3214 3241
3412 3421 4123 4132
4213 4231 4312 4321
Suppose we have an algorithm to generate the next larger p e r m u t a t i o n b i b 2 . . , bn from a given p e r m u t a t i o n a l a 2 . . , a n . Then, beginning with the p e r m u t a t i o n 123... n, we can invoke it to generate the remaining n  1 permutations. The procedure to generate the next larger p e r m u t a t i o n from a given p e r m u t a t i o n in lexicographic order is described below.
884
Appendix A
If a n  1 < an, switch them to obtain the new p e r m u t a t i o n a l a 2 . . , a n  2 a n a n  1 . Then the p e r m u t a t i o n a l a 2 . . , a n  2 a n  l a n precedes the p e r m u t a tion a l a 2 . . , a n  2 a n a n  1 in lexicographic ordering. For example, consider the p e r m u t a t i o n 2143; it succeeds the p e r m u t a tion 2134 in the listing in Figure A.9. On the other hand, suppose a n  1 > an. (Then by switching t h e m we cannot obtain a larger permutation.) So we look at a n  2 . If a n  2 < a n  l , find the smaller of the elements a n  1 and an t h a t is larger t h a n a n  2 ; place it in position n  2; and now arrange a n  2 and the remaining elements in increasing order. For instance, consider the p e r m u t a t i o n a l a 2 a 3 a 4  2341 in Figure A.9. Here a3 > a4, but a2 < a3; the smaller of a3 and a4 t h a t is larger t h a n a2 = 3 is a3  4; so hold 4 in position 2 to get 2 4 ; now arrange the r e m a i n i n g elements a2  3 and a4 = 1 in ascending order to yield the next larger p e r m u t a t i o n 2413 (see Figure A.9). If an2 > a n  l , we need to look at the elements a n  3 t h r o u g h an. More generally, all we need to do is the following. From right to left, find the first pair of elements ai, ai+l such t h a t ai < a i + l , where ai+l > ai+2 > . . . > an. Then find the smallest of the elements ai+l, a i + 2 , . . . , a n , say, aj, that is larger t h a n ai. Place aj in position i. Now arrange the elements ai, a i + l , . . . , a j  1 , aj, a j + l , . . . , a n in increasing order in positions i § 1 t h r o u g h n. The following example illustrates this procedure. Find the next p e r m u t a t i o n larger t h a n a l a 2 a 3 a 4  3421. SOLUTION: The first pair ai, ai+l from right to left for which ai < ai+l is the pair al, a2. So find the smallest of the elements a2  4, a3  2, and a4 = 1 t h a t is larger t h a n a l  3. Clearly, it is a2  4. So we place 4 in position 1. Now we arrange the elements 3, 2, and 1 in increasing order: 123. Consequently, the next larger p e r m u t a t i o n is 4123. (See Figure A.9.) 9 The above discussion can be translated into an algorithm for finding the next larger p e r m u t a t i o n t h a t follows a given p e r m u t a t i o n a l a 2 . . , an. It is presented in Algorithm A.1.
Algorithm nextpermutation (ala2...an) (* This algorithm finds the permutation larger than the given permutation ala2...an. Assume i t is not the largest permutation n(n  1)...321. *) O. Begin (* algorithm *) (* From right to l e f t , find the f i r s t pair ai, ai+l for which ai < ai+l. *) 1. i K   n  1
A.4 GeneratingPermutations and Combinations
885
2. While ai > a i + l do (* continue searching to the l e f t *) 3. i ~ii (* When we e x i t the loop, we w i l l have found an i for which ai < a i + l . Find the smallest of the elements a i + l through an, say, ak, that is larger than ai. Since ai + 1 > ai +2 > "'" > an, scan from r i g h t to l e f t to find the element ak that is larger than ai. *) 4. k ~  n 5. While ak < ai do (* continue scanning to the l e f t . *) 6. k+k1 (* ak is the smallest of the elements ai+1 through an that is larger than ai. Swap ai and ak. *) 7. swap ai and ak 8. sort the elements in positions i + 1 through n into ascending order. 9. End (* algorithm *)
Algorithm A.I
Using Algorithm A. 1, e n u m e r a t e in lexicographic order all p e r m u t a t i o n s of elements of the set {1, 2, 3}. SOLUTION: We begin with the basic p e r m u t a t i o n ala2a3 : 123. Since a2 < a3 and the least of the elements(s) a3 t h a t is g r e a t e r t h a n a2 is a3, switch 2 and 3 to get the next larger p e r m u t a t i o n 132. Since 1 < 3 and the smaller of 3 a n d 2 t h a t is larger t h a n 1 is 2, swap 1 and 2, and sort the elements 3 and 1 to yield the p e r m u t a t i o n 213. C o n t i n u i n g like this, we get the r e m a i n i n g p e r m u t a t i o n s in lexicographic order: 123, 132, 213, 231,312, 321. (Also see the tree diagram in Figure A. 10.) F i g u r e A.10
permutations 3
>
123
2
>
132
3
>
213
1
~
>
231
2
~
>
312
1
~
>
321
1
Generating Combinations How can we g e n e r a t e the rcombinations of set {1,2,... ,n}, where 0 < r < n? First we list the various combinations in increasing numerical order.
886
Appendix A
F o r i n s t a n c e , t h e r e a r e t e n 3  c o m b i n a t i o n s of t h e set S = {1, 2, 3, 4, 5}; in lexicographic order, t h e y are: 123, 124, 125, 134, 135, 145, 234, 235, 245, 345 H o w can we find t h e n e x t l a r g e r 3  c o m b i n a t i o n f r o m a given 3  c o m b i n a t i o n ? F o r i n s t a n c e , c o n s i d e r t h e 3  c o m b i n a t i o n 134 of t h e set S. T h e last e l e m e n t (from right) is less t h a n 5 (= n). So we i n c r e m e n t it by 1 to get t h e n e x t l a r g e r c o m b i n a t i o n 135. U s i n g t h e set S  {1, 2, 3, 4, 5}, find t h e c o m b i n a t i o n following a l a 2 a 3 135 in lexicographic order.

SOLUTION: Notice t h a t a3 = 5, t h e l a r g e s t i n S . So look at a2; a2 < 5; so i n c r e m e n t a2 by 1 to get 4. Replace a3 by 1 m o r e t h a n t h e c u r r e n t a 2 : a 3 ~ a2 + 1 = 4 + 1 = 5. T h u s t h e n e x t l a r g e r c o m b i n a t i o n is 145. m F i n d t h e c o m b i n a t i o n t h a t follows in lexicographic o r d e r t h e 3  c o m b i n a t i o n 145 of t h e set {1, 2, 3, 4, 5}.
SOLUTION: Let a l a 2 a 3 = 145. Clearly, a3 = 5 a n d a2 = 4. Since a2 < 5, if we i n c r e m e n t it by 1 to get 4 + 1 = 5, t h e n e w a3 s h o u l d be 5 + 1; u n f o r t u n a t e l y , it does not exist in t h e set. So go to a l = 1, w h i c h is less t h a n 5. (Notice t h a t a3  5  3 + 3; a 2  4  5  3 + 2; b u t a l  1 r 5  3 + 1. T h e r e t b r e , a l is t h e first e l e m e n t f r o m t h e r i g h t such t h a t a i r n  r + i.) I n c r e a s e a l by 1 : a l * a l + 1; so a l * 2. Now assign a2 = al + 1 = 3 a n d a:3 = al + 2 = 4. T h e r e s u l t i n g c o m b i n a t i o n is 234. m
T h u s , to find t h e c o m b i n a t i o n t h a t follows t h e r  c o m b i n a t i o n a l a 2 . . , a r , we proceed as follows. F r o m r i g h t to left, we find t h e first e l e m e n t a i s u c h t h a t a i ~= n  r + i. I n c r e m e n t a i by 1 : a i ~ a i + 1. ( U s i n g t h e n e w a i ) a s s i g n t h e v a l u e s a i + 1 , a i + 2 , . . . to a i + l , a i + 2 , . . . , a r , respectively; t h a t is, a j ~ a i + j  i, w h e r e i + 1 < j _< r.
F i n d t h e c o m b i n a t i o n t h a t follows in lexicographic c o m b i n a t i o n 245 of t h e set {1, 2, 3, 4, 5}.
ordering
the
3
SOLUTION: H e r e n  5, r = 3, a n d n  r = 2. Let a l a 2 a 3  245. F r o m r i g h t to left, find t h e first a i s u c h t h a t a i ~= n  r + i = 2 + i. Clearly, a3  5  2 + 3, a2 = 4 = 2 + 2; b u t a l = 2 r 2 + 1. T h e r e f o r e , t h e first such a i is a l . U p d a t e a l as a l + l : a l ~ 2 + 1  3 . N o w assign t h e v a l u e a l + j  1 to a j for t h e r e m a i n i n g positions j , n a m e l y , 2 a n d 3: W h e n j = 2, a2 = a l
+2
1 =3+
1 =4
A4
Generating Permutations and Combinations
887
W h e n j = 3 , a3   a l + 3 
1 =3+2=5
The r e s u l t i n g c o m b i n a t i o n is 345.
m
These discussions lead us to A l g o r i t h m A.2. Algorithm nextcombination (ala2...ar) (* This a l g o r i t h m f i n d s the combination t h a t f o l l o w s the r  c o m b i n a t i o n a l a 2 . . . a r of the set {1,2 . . . . . n}. Assume the given combination is not the l a r g e s t combination (n r + Z)...(nl ) n . *) O. Begin (* n e x t  c o m b i n a t i o n *) (* Find the f i r s t ai from r i g h t to l e f t f o r which ai ~ n  r + i . *) I . i k o i~!k!xiyizk i+j+k=2
To find the various t e r m s in the expansion, let us list t h e possible combinations of values of i, j , a n d k, a n d the c o r r e s p o n d i n g coefficients a n d t e r m s in a table, as in Table A.3. Table A.3
i
j
k
2
0
0
0
2
0
0
0
2
1
1
0
0
1
1
1
0
1
2~
i!j!k! 2~ = 1 2~0~0~ 2~ = 1 0~2~0~ 2~  1 0~0~2~ 2~ =2 1~1~0~ 2~ =2 0~1~1~ 2~  2 1!0!1!
xi yJ z k
Term
X2
X2
y2
y2
z2
z2
xy
2xy
yz
2yz
zx
2zx
Adding up t h e t e r m s in the last column, (X § y § Z) 2   X 2 +
y2 +
Z2 +
2xy + 2yz + 2zx
m
Find the coefficient of x2y3z 4 in t h e expansion of (x + y + z) 9. SOLUTION: H e r e i = 2, j  3, k  4, and n = 9. Therefore, the r e q u i r e d coefficient is given by n! 9! = = 1260 i~'!k! 2!3!4! m T h e proof used to develop the t r i n o m i a l t h e o r e m can be generalized to k variables x l, x 2 , . . . ,Xk in an obvious way. The r e s u l t i n g t h e o r e m , called t h e m u l t i n o m i a l t h e o r e m , is given below. [ ~ ~ ~ ~ ~
(The Multinomial T h e o r e m ) Let x l , x 2 , . . . ,xk be a n y k real variables a n d n any n o n n e g a t i v e integer. T h e n
(Xl § X2 § " ' " § X k ) n  
E
n! il i2 ik il!i2!. . . ik !xl x2 "" "xk
w h e r e il, i 2 , . . . , ik >_ 0 and il + i2 + . . . + ik n.
m
A.5
The Multinomial Theorem
891
Using the multinomial theorem, find the coefficient of XlX22X33X44X55 in the expansion of (Xl + x2 + x3 + x4 + x5) 15. SOLUTION: 15! 1!2~3!4!5~ = 37,837,800
Required c o e f f i c i e n t 
The coefficient
n~
m
in the multinomial theorem,
il!i2!...ik!
m u l t i n o m i a l c o e f f i c i e n t , is denoted by 5 ) 3,0,1,1 J u s t as the binomial coefficient
called the
il, i 2 , . . . , ik " For instance,
5~ = 20 3!0!1!1!
(n) i,j
denotes the n u m b e r of ways a set of
size n can be divided into two disjoint subsets of sizes i a n d j = n  i, the multinomial coefficient can also be interpreted in a similar way, as stated in the following theorem. The n u m b e r of ways of dividing a set S of size n into k mutually disjoint ordered subsets $1, $2,..., Sk of sizes i 1 , i 2 ,  9 9 ik, respectively, is given by the multinomial coefficient
(
n
)
i l, i2, . . . ,ire
where il, i2,.., ik >_ 0 and il + i2 + ... + ik  n. PROOF:
The il elements of the subset $1 can be selected in ( nil)
ways. This leaves
n  il elements in S  $1. Therefore, the i2 elements of $2 can be selected in ( n  iwl )a y S ' i 2 (nil\
13
Similarly, the i3 elements of S3 can be selected in
t2] ways, and so on. The ik elements of Sk can be ch ;en in /
( n  i l  i2  ' "
 ik1) ways
C~
by the multiplicati~
ciple, the n u m b e r of ways of choosing the mutually disjoint subsets $1, $ 2 , . . . , Sk is
(i )(nil
) ( ni i i .....
Appendix A
892
n!
( n  i l ) !
( n  i l . . . .
i l ! ( n  i l ) !
i 2 ! ( n  i l  i2)!
i k ! ( n  i l . . . . .
n!
( N o t e 9 n = i l + i2 + . . .
il!i2!...ik!
(
n
ik1) ik)!
+ ik)
)
i1,i2,... , i k
m
This concludes the proof.
Find the number of ways of dividing a set of size five into three mutually disjoint ordered subsets of sizes 2, 1, and 2. SOLUTION: By Theorem A.10, the number of ways of dividing the set in the desired way is given by the multinomial coefficient 5 ) 5! 2, 1,2 = 2!1!2! = 30
m
Find the number of w a y s of dividing the set S  {a, b, c } into three mutually disjoint ordered subsets $1, $2, and $3 of sizes 1, 1, and 1, respectively, and list them. SOLUTION: Number of possibilities =
1!1!1!  6
1, 1, 1
They are listed in Table A.4.
Table A.4
$1
$2
S:~
{a} {a} {b} {b} {c} {c}
{b} {c} {a} {c} {a} {b}
{c} {b} {c} {a} {b} {a}
A n i m p o r t a n t o b s e r v a t i o n " The division { {a}, {b}, {c} } is different from the division { {a}, {c}, {b} }. However, they form the same partition, m
Exercises A.5 Evaluate each multinomial coefficient. 1.
(\2, 2,5 1 )
2. ( 1 2 ,
72)
3. (2, 3,
4 (085) 3,
A.5 The Multinomial Theorem
5.
(8) 2, 1, 3,2
6.
893
(10)(10)(lo) 1, 2, 3,4
7.
3, 1, 4,2
8.
1, 4, 4,1
Find the coefficient of each.
9. x2yz 2 in the expansion of (x + y + z) 5 10. xy2z 3 in the expansion of (x + y + z) 6
11. yz in the expansion of (x + y  z) 2 12. xy 2 in the expansion of (x  y  z) 3 13. xy2z 5 in the expansion of (x + y + z) s 14. xy3z 3 in the expansion of (x  y + 2z) 8
Expand each.
15. ( x + y  z ) 18. ( x  y 
2 z) 3
16. ( x  y  z )
2
17. ( x  y + z )
19. (x + 2y + Z) 3
20.
3
( x  2y + 2z) 3
Consider the various ways of dividing the set S = {a, b, c, d} into three mutually disjoint ordered subsets $1, $2, and $3 with sizes 1, 1, and 2, respectively. 21. Find the number of possibilities. 22. List the possibilities. Find the number of ways of dividing a set of size n into k mutually disjoint ordered subsets of sizes il, i 2 , . . . , ik in each case. 23. n = 10, three subsets with sizes 2, 3, and 5. 24. n = 10, four subsets with sizes 2, 2, 3, and 3. 25. n = 12, six subsets with sizes 1, 1, 2, 2, 3, and 3. 26. n = 15, four subsets with sizes 1, 1, 5, and 8. 27. Find the sum of the multinomial coefficients
( il, i 2 ,n. . . , ik ) "
It follows by Theorem A.10 that the n u m b e r of permutations of n items of which il are alike, i2 are alike, ..., and ik are alike is given by the multinomial coefficient ( i l n, i 2 ,). . . ,. ik
Using this fact, compute each.
28. The n u m b e r of ways of scrambling the letters of the word ABRACADABRA. 29. The number of ways of scrambling TINTINNABULATION.
the
letters
of the
word
30. The n u m b e r of binary words of length 10 and containing exactly three l's and seven 0's.
894
Appendix A 31. The n u m b e r of bytes c o n t a i n i n g exactly five 0's. 32. The n u m b e r of t e r n a r y words of length 12 and c o n t a i n i n g t h r e e 0's, four l's, and five 2's. Find the n u m b e r of t e r m s in the expansion of each. 33. (x + y + z) a
34. (x + y + z) 10
35. (w + x + y + Z) 12
36. (2w  3x + 4y  4 z ) 15
Find the coefficient of each. 37.
x2y2z 3 in
38.
xy3z in
the expansion of (x + y + z) 7
the expansion of (x + 2y  z) 5
Using the m u l t i n o m i a l t h e o r e m , expand each.
39. ( x  2 y + z )
3
40. ( x + y  z )
4
Find the n u m b e r of t e r m s in the expansion of each.
41. ( x + y  z )
6
43. ( 2 x + 3 y  4 z )
42. ( x  y  z ) 9
44. ( w + x  y  z )
s 1~
Find the n u m b e r of ways of dividing a set with size 15 into each. 45. F o u r m u t u a l l y disjoint subsets with sizes 7, 2, 3, and 3. 46. Five m u t u a l l y disjoint subsets with sizes 3, 6, 2, 1, and 3.
A B F A E Z H () I K A M
a /~ y 5 e r ~ O r K )~ tt
alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu
N
P
nu
E 0 FI P
~ o rr p a r v r x ~ w
xi omicron pi rho sigma tau upsilon phi chi psi omega
T T X s2
A.7
Web Sites
895
The following Web sites provide valuable information for further exploration and enrichment. The status of a Web site could change with time, so it may not exist when you look for it; if it does not, use a search engine to locate a similar Web site. 1. Wilhelm Ackermann wwwgap.dcs.stand, ac.uk/~history/Mathematicians/ Ackermann.html 2. John Backus www.digitalcentury.com/encyclo/update/backus.html wwwgap.dcs.stand.ac.uk/~history/Mathematicians/Backus.html www.acm, org/awards/turing_citations/backus.html www.cs.nyu, edu/cs/faculty/shashaoutofmind/backus.html 3. George Boole wwwgap.dcs.stand.ac.uk/~history/Mathematicians/Boole.html www.digitalcentury.com/encyclo/update/boole.html homepages.enterprise.net/rogerp/george/boole.html 4. Georg Cantor wwwgroups.dcs.stand.ac.uk/~ history/Mathematicians/Cantor, html www.treasuretroves.com/bios/CantorGeorg.html www.aug.edu/dvskel/JohnsonSU97.html 5. Arthur Cayley wwwgap.dcs.stand.ac.uk/~history/Mathematicians/Cayley.html scienceworld.wolfram.com/biography/Cayley.html www.stetson.edu/~efriedma/periodictable/html/C.html www.optisyn.com/presentations/Cayley.html www.math.ukans.edu/~engheta/bio/cayley.html www.geometry.net/Biographer/Cayley.html 6. Edsger Dijkstra www.digidome.nl/edgser_wybe_dijkstra.html www.acm.org/classics/oct95/ www.cs.utexas/users/EWD/obituary.html news.com.com/21001001949023, html 7. Leonhard Euler
www.maths.tcd.ie/pub/HistMath/People/Euler/RouseBall/ RB Euler.html
www.shu.edu/html/teaching/math/reals/history/euler.html 8. Pierre de Fermat www.maths.tcd.ie/pub/HistMath/People/Fermat/RouseBall/ RB Fermat.html 9. Karl F. Gauss wwwgap.dcs.stand.ac.uk/~history/Mathematicians/Gauss.html www.english.upenn.edu/~jlynch/FrankenDemo/People/gauss.html scienceworld.wolfram.com/biography/Gauss.html www.indiana.edu/~intell/gauss.html www.brown.edu/Students/OHJC/hm4/gauss.html
896
Appendix A 10. Christian Goldbach
11.
12.
13.
14.
15.
16.
17.
18.
19.
wwwgap.dcs.stand.ac.uk/~history/Mathematicians/Goldbach.html www.geocities.com/Heartland/Hills/7972/mathgoldbach.html www.wikipedia.org/wiki/Christian_Goldbach plus. maths, org/issue 11/news/Goldbach Sir William R. Hamilton www.chembio.uoguelph.ca/educmat/chm386/rudiment/tourclas/ hamilton.html scienceworld.wolfram.com/biography/HamiltonWilliamRowan.html www.iaste.com/hall of fame/hamilton.html Maurice Karnaugh www.informatik.unitrier.de/~ley/db/indices/atree/k/Karnaugh" Maurice.html www.maxmon.com/library.html wwwcse, st an ford. edu/classes/cs 103 a/h 9BooleanAlgebra. pdf Alfred B. Kempe wwwgap.dcs.stand.ac.uk/~history/Mathematicians/Kempe.html mappa.mundi.net/locus_014 www.uwinnipeg.ca/~ooellerm/guthrie/FourColor.html www.mathsyear2000.org/explorer/morphing/13usedownload.shtml Stephen C. Kleene www.library.wisc.edu/libraries/Math/kleen.html wwwgroups.dcs.stand.ac.uk/~history/Mathematicians/Kleene.html www.dcs.ed.ac.uk/homes/als/lics/newsletters/19.html www.student.math.uwaterloo.ca/~cs462/Hall/kleene.html Donald E. Knuth sunburn.stanford.edu/~knuth wwwcsstaff.stanford.edu/~knuth/index.html www.digitalcentury.com/encyclo/update/knuth.html laurel.actlab.utexas.edu/~cynbe/muq/muf3_20.html Kazimierz Kuratowski wwwgroups.dcs.stand.ac.uk/~history/Mathematiticains/ Kuratowski.html www.stetson.edu/~efriedma/periodictable/html/Kr.html Gabriel Lame wwwgroups.dcs.stand.ac.uk/~history/Mathematicians/Lame.html www.bath.ac.uk/~ma0dmp/Lamelife.html Edmund Landau www.ma.huji.ac.il/~landau/landuniv.html wwwgroups, dcs. stand, ac. uk/~ hi st ory/M athem atician s/Landau, ht m 1 www. ard. huj i. ac.il/publications/25years/chap 13. html PierreSimon Laplace wwwgroups.dcs.stand.ac.uk/~history/Mathematicians/Laplace.html www.maths.tcd.ie/pub/HistMath/People/Laplace/RouseBall/ RB_Laplace.html www. stetson.edu/~efriedma/periodictable/html/La.html
A.7 Web Sites
897
20. Gottfried W. Leibniz
www.maths.tcd.ie/pub/HistMath/People/Leibniz/RouseBall/ RB Leibniz.html 21. J a n Lukasiewicz
22.
23.
24.
25.
26.
27.
28.
29.
30.
wwwgroup s. dcs. s tand, ac. u k / ~ history/Mat hematiticain s/ Lukawiewicz.html www. h p m u s e u m . o r g / r p n . h t m l www.wikipedia.com/wiki/Jan_Lukawiewicz George H. Mealy www.informatik.unitrier.de/~ley/db/indices/atree/m/Mealy: George_H = .html Marin Mersenne wwwgroups, dcs. stand.ac.uk/~history/Mathematiticains/ Mersenne.html www2.andrews.edu/~calkins/math/biograph/biomerse.html Blaise Pascal members.aol.com/KatherenaE/private/Philo/Pascal/pascal.html www. m at hs. tcd. ie/pub/HistM at h/P eo ple/P ascal/Rouse Ball/ RB Pascal.html www. cs. was h i ngt o n. edu/h o m e s/j b ae r/clas se s/b laise/b lai se. h tml www.math, sfu.ca/histmath/Europe/17thCenturyAD/Balise.html wwwgroups.dcs.stand.ac.uk/~history/Mathematiticains/ Pascal.html Bertrand Russell www. m cm aster, c a/ru s sd o c s/ru sse ll. h tml plato.stanford.edu/entries/russell desktop 12.cis.macmaster.cal/~bertrand Claude E. S h a n n o n wwwgroups.dcs.stand.ac.uk/~history/Mathematiticains/ Shannon.html www.belllabs.com/news/2001/february/26/1.html www.digitalcentury.com/encyclo/update/shannon.html Alan M. Turing www.turing.org.uk/turing e i. cs. vt. edu/~ histo ry/Tu ring. h tml wwwgroups.dcs.stand.ac.uk/~history/Mathematiticains/ Turing.html AlexandreTheophile Vandermonde wwwgroups.dcs.stand.ac.uk/~history/Mathematiticains/ Vandermonde.html J o h n Venn wwwgroups.dcs, stand, ac.uk/~history/Mathematicians/Venn.html userwww.sfsu.edu/~rsauzier/Venn.html Boolean Algebra educ.queensu.ca/~compsci/units/BoolLogic/titlepage.html www.bit.umkc.edu/vu/course/cs281/lectures/booleanalgebra/ booleanalgebra.html
898
Appendix A
32.
33.
34.
35.
36.
37.
ever.phys.ualberta, ca/~ gingrich/phys395/notes/node 121.html www. maxmon, com/1847ad, h t m l www.yale, edu/ynhti/curri culu m/u nits/ 1989/7/89.07.07.x.h tml FiniteState Machines www.cs.brown.edu/people/jes/book/BOOK/nodel0.html classwww.gsfc.nasa.gov/CAGESite/pages/cage_gpf_fsm.html www. cs. ariz ona. edu/classes/cs352/summer 02/fsa. ht m 1 www.c3.1anl.gov/megamath/workbk/machine/mabkgd.html www.beigarath.demon, co.uk/j ava/fsme.html members.aol.com/asakharov/fsm.html Fuzzy Sets www.answermath.com/fuz z y m a t h . h t m l sun 16. cecs. missouri, edu/index, html news:comp.ai.fuzzy G e n e r a t i n g Functions www.cs.wpi.edu/~cs504/s00m/notes/ln/1999/class06/class06.html www. cs. wp i. edu/~ cs504/s00m/classes/clas s05/Class 05. h tml msl. cs.uiuc, edu/~ lavalle/cs576/proj ects/wmchan www. eco. rug. nl/gau ss/GAUSS00/mho narc. db www. m a t h s. surrey, ac. u k/per sonal/st/d, fi sher/M S 103/MS 1055. mws Graph Theory www. u t m. e du/de p a r t m e n t s/m at h/gr ap h www.c3.1anl.gov/megamath/workbk/graph/graph, html www groups, dcs. st and. ac. u k / ~ histo ry/M athe mat ician s/E rdo s. h tml www.nada.kth.se/~viggo/problemlist/compendium.html www. shodor.org/interactive/lessions/frac 1.html www.scism.sbu.ac.uk/law/Section3/chapter3/s3c3int.html www. cs. cm u. edu/~ cbu rch/survey/recur se www.nd.edu/~cholak/computability/computability, html Hilbert's Paradoxes www.wordsmith.demon.co.uk/paradoxes eluzions.com/Puzzles/Logic/Paradoxes.shtml www.c3.1anl.gov/megamath/workbk/infinity/infinity.html www. cs. tpu. ee/~j aagu p/uk/fm m/m at h/1.2.5.3.h tml The FourColor Problem www.math.gatech.edu/~thomas/FC/fourcolor.html www.cs.uidaho.edu/~casey931/megamath/gloss/math/4ct.html wwwgroups.dcs, stand.ac.uk/~ history/HistTopics/The_four_colour theorem.html
References 1. A. V. Aho et al., Compilers: Principles, Techniques, and Tools, AddisonWesley, Reading, MA 1986. 2. V. S. Alagar, Fundamentals of Computing: Theory and Practice, PrenticeHall, Englewood Cliffs, NJ, 1989. 3. K. Appel and W. Haken, "Every Planar Graph is 4colorable," Bulletin of the American Mathematical Society, Vol. 82 (Sept. 1976), pp. 711712. 4. R. G. Archibald, An Introduction to the Theory of Numbers, 3rd edition, Wiley, New York, 1972. 5. V. Bain, "An Algorithm for Drawing the ncube," The College Mathematics Journal, Vol. 29 (Sept. 1998), pp. 320322. 6. C. Baltus,
"A Truth Table on the Island of Truthtellers and Liars,"
Mathematics Teacher, Vol. 94 (Dec. 2001), pp. 730732. 7. W. J. Barneir, "FiniteState Machines as Recognizers," The UMAP Journal, 7:3 (1986), pp. 209232. 8. W. Barnier and J. B. Chan, Discrete Mathematics with Applications, West, St. Paul, MN, 1989. 9. B. Barwell, Solution to Problem 702, J. Recreational Mathematics, Vol. 12:1 (19791980), p. 6768. 10. B. Barwell, Solution to Problem 1046, J. Recreational Mathematics, Vol. 15:1 (19811982), pp. 7072. 11. R. Bellman et al., Algorithms, Graphs, and Computers, Academic Press, New York, 1970. 12. M. Bellmore and G. L. Nemhauser, "The Traveling Salesman Problem," Operations Research, Vol. 16 (1968), pp. 538558. 13. S. J. Bezsuska, Solution to Problem 791, J. Recreational Mathematics, 12:4 (19791980), p. 311. 14. B. Bissinger, "Ask Marilyn," Parade Magazine (April 25, 1993), p. 15. 15. M. L. Bittinger, Logic and Proof, AddisonWesley, Reading, MA 1972. 16. W. G. Brown, "Historical Note on a Recurrent Combinatorial Problem," The American Mathematical Monthly, Vol. 72 (Nov. 1965), pp. 973977.
899
900
References
17. A. V. Boyd and M. J. Glencorss, "Dissecting a Circle by Chords through n Points," Mathematics Teacher, Vol. 84 (April 1991), pp. 318319. 18. J. Burling et al., "Using Graphs to Solve the Traffic Light Problem," FAIM Module, COMAP, Inc., Lexington, MA, 1989. 19. D. Burns, Problem 596, J. Recreational Mathematics, Vol. 10:1 (19771978), p. 52. 20. W. H. Bussey, "Origin of Mathematical Induction," The American Mathematical Monthly, Vol. 24 (May 1917), pp. 199207. 21. Calendar Problems, Mathematics Teacher, Vol. 83 (Oct. 1990), p. 550. 22. Calendar Problems, Mathematics Teacher, Vol. 85 (Dec. 1992), p. 736. 23. Calendar Problems, Mathematics Teacher, Vol. 79 (April 1986), p. 274. 24. Calendar Problems, Mathematics Teacher, Vol. 79 (Nov. 1986), p. 627. 25. D. M. Campbell, "The Computation of Catalan Numbers," Mathematics Magazine, Vol. 57 (Sept. 1984), pp. 195208. 26. L. Carlitz, Solution to Problem B180, The Fibonacci Quarterly, Vol. 8:5 (Dec. 1970), pp. 547548. 27. M. Caudill, "Using Neural Nets: Fuzzy Decisions," A/Expert, Vol. 5 (April 1990), pp. 5964. 28. M. Charosh, Problem 1160, J. Recreational Mathematics, Vol. 15:1 (19831984), p. 58. 29. E. F. Codd, "A Relational Model of Data for Large Shared Databanks," Communications of the ACM, Vol. 13 (1970), pp. 377387. 30. F. Cohen and J. L. Selfridge, "Not Every Integer Is the Sum or Difference of Two Prime Powers," Mathematics of Computation, Vol. 29 (1975), p. 79. 31. E. Comfort, Solution to Problem 596, J. Recreational Mathematics, Vol. 11:1 (19781979), p. 66. 32. J. W. Cortada, Historical Dictionary of Data Processing: Biographies, Greenwood Press, New York, 1987. 33. M. Coughlin and C. Kerwin, "Mathematical Induction and Pascal's Problem of the Points," Mathematics Teacher, Vol. 78 (May 1985), pp. 376380. 34. T. Crilly, "A Victorian Mathematician," The Mathematical Gazette, Vol. 79 (July 1995), pp. 259262. 35. P. Cull and E. F. Ecklund, Jr., "Towers of Hanoi and Analysis of Algorithms," The American Mathematical Monthly, Vol. 92 (JuneJuly 1985), pp. 407420. 36. N. Deo, Graph Theory with Applications to Engineering and Computer Science, Prentice Hall, Englewood Cliffs, NJ, 1974. 37. R. C. Drake, Problem B180, The Fibonacci Quarterly, Vol. 8:1 (Feb. 1970), p. 106. 38. L.R. Duffy, "The Duffinian Numbers," J. Recreational Mathematics, Vol. 12:2 (19791980), pp. 112115. 39. R. Euler, Problem 1551, J. Recreational Mathematics, Vol. 19:2, 1987, p. 151. 40. H. Eves, Problem E579, The American Mathematical Monthly, Vol. 50 (JuneJuly 1943), p. 386. 41. H. Eves, An Introduction to the History of Mathematics, 3rd edition, Holt, Rinehart and Winston, New York, 1969.
References
901
42. A. Filz, Problem 1046, J. Recreational Mathematics, Vol. 14:1 (19811982), p. 64. 43. T. Fletcher, Problem 602, J. Recreational Mathematics, Vol. 10:1 (19781979), p. 52. 44. H. G. Forder, "Some Problems in Combinatorics," The Mathematical Gazette, Vol. 45 ( 1961), pp. 199201. 45. A. J. Friedland, Puzzles in Math & Logic, Dover, New York, 1970. 46. J. A. Gallian and S. Winters, "Modular Arithmetic in the Market Place," The American Mathematical Monthly, Vol. 95 (JuneJuly 1988), pp. 548551. 47. M. Gardner, "Mathematical Games," Scientific American, Vol. 219 (Sept. 1968), pp. 218230. 48. M. Gardner, "Catalan Numbers: An Integer Sequence That Materializes in Unexpected Places," Scientific American, Vol. 234 (June 1976), pp. 120125. 49. M. Gardner, Mathematical Circus, Knopf, New York, NY, 1979. 50. M. Gardner, Mathematical Puzzles and Diversions, The University of Chicago Press, Chicago, IL, 1987. 51. M. Gardner, "Ask Marilyn," Parade Magazine (April 18, 1993), p. 12. 52. S.W. Golomb, "Pairings and Groupings," Johns Hopkins Magazine, Vol. 45:2 (April 1993), p. 7. 53. R. L. Graham et al., Concrete Mathematics, AddisonWesley, Reading, MA, 1990. 54. J. J. Gray, "Arthur Cayley (18211895)," The Mathematical Intelligencer, Vol. 17:4 (1995), pp. 6263. 55. T. M. Green, "Pascal's Pizza," Mathematics Teacher, Vol. 81 (Sept. 1988), p. 445, 454. 56. R. P. Grimaldi, Discrete and Computational Mathematics, 4th ed., AddisonWesley, Reading, MA, 1999. 57. A. Guckin et al., The Euler Circuit Project, COMAP, Inc., Lexington, MA, 1989. 58. S. Gudder, A Mathematical Journey, McGrawHill, New York, 1976. 59. B. Hamilton, Brainteasers and Mindbenders, Freeside, New York, 1992. 60. D. K. Hanson et al., "Matching, Derangements, and Rencontres," Mathematics Magazine, Vol. 56 (Sept. 1983), pp. 224229. 61. F. Harray and J. S. Maybee (eds.), Graphs and Applications, Wiley, New York, 1985. 62. B. Hayes, "On the FiniteState Machine, A Minimal Model of Mousetraps, Ribosomes, and the Human Soul," Scientific American, Vol. 249 (Dec. 1983), pp. 2028, 178. 63. L. Henkin, "On Mathematical Induction," The American Mathematical Monthly, Vol. 67 (April 1960), pp. 323338. 64. V. E. Hoggatt, Jr., Fibonacci and Lucas Numbers, Houghton Mifflin, Boston, 1963. 65. V. E. Hoggatt, Jr., and S. L. Basin, "A Primer on the Fibonacci Sequence, Part II," The Fibonacci Quarterly, Vol. 1:2 (April 1963), pp. 6168.
902
References
66. V. E. Hoggatt, Jr., "Some Special Fibonacci and Lucas Generating Functions," The Fibonacci Quarterly, Vol. 9:2 (April 1971), pp. 121133. 67. T. C. Hu, Combinatorial Algorithms, AddisonWesley, Reading, MA, 1982. 68. K.K. Huang, Solution to Problem 1160, J. Recreational Mathematics, Vol. 16:1 (19831984), p. 69. 69. R. V. Jean, "The Fibonacci Sequence," The UMAP Journal, Vol. 5:1 (1984), pp. 2347. 70. J. T. Johnson, "Fuzzy Logic," Popular Science, Vol. 237 (July 1990), pp. 8789. 71. R. Johnsonbaugh, Discrete Mathematics, 5th ed., PrenticeHall, Upper Saddle River, NJ, 2001. 72. E. Just, "A Note on the nth Term of the Fibonacci Sequence," Mathematics Magazine, Vol. 44 (Sept.Oct. 1971), p. 199. 73. M. Karnaugh, "The Map Method for Synthesis of Combinational Logic Circuits," Transactions of the AIEE, Part I, Vol. 72:9 (Nov. 1953), pp. 593599. 74. F. H. Kierstead, Jr., Problem 791, J. Recreational Mathematics, Vol. 11:4 (19781979), p. 302. 75. F. H. Kierstead, Jr., Problem 1014, J. Recreational Mathematics, Vol. 14:4 (19811982), p. 309. 76. M. Keith and T. Carver, "The Ultimate Perpetual Calendar," J. Recreational Mathematics, Vol. 22:4 (1990), pp. 280282. 77. D. E. Knuth, "Algorithms," Scientific American, Vol. 243 (April 1977), pp. 6380. 78. D. E. Knuth, "Algorithmic Thinking and Mathematical Thinking," The American Mathematical Monthly, Vol. 92 (March 1985), pp. 170181. 79. B. Kolman et al., Discrete Mathematical Structures, 4th ed., PrenticeHall, Upper Saddle River, NJ, 2000. 80. T. Koshy, Finite Mathematics and Calculus with Applications, Goodyear, Pacific Palisades, CA, 1979. 81. T. Koshy, Fibonacci and Lucas Numbers with Applications, Wiley, New York, 2001. 82. T. Koshy, Elementary Number Theo~ with Applications, Harcourt/Academic Press, Boston, 2002. 83. B. Kosko and S. Isaka, "Fuzzy Logic," Scientific American, Vol. 269 (July 1993), pp. 7681. 84. J. B. Kruskal, "On the Shortest Spanning Subtree of a Graph and the Traveling Salesman Problem," Proceedings of the American Mathematical Society, Vol. 1 (1956), pp. 4850. 85. E. A. Kuehls, "The TruthValue of {v, 3,P(x,y)}: A Graphical Approach," Mathematics Teacher, Vol. 43 (Nov. 1970), pp. 260261. 86. N.J. Kuenzi and B. Prielipp, Problem 4026, School and Science Mathematics, Vol. 85 (Dec. 1985), pp. 714716. 87. L.J. Lander et al., "A Survey of Equal Sums of Like Powers," Mathematics of Computation, Vol. 21 (1967), p. 446.
References
903
88. E. L. Lawler et al. (eds.), The Traveling Salesman Problem, Wiley, New York, 1986. 89. C. T. Long, "On Pigeons and Problems," Mathematics Teacher, Vol. 81 (Jan. 1988), pp. 2830, 64. 90. E. Maier, "Counting Pizza Pieces and Other Combinatorial Problems," Mathematics Teacher, Vol. 81 (Jan. 1988), pp. 2226. 91. C. L. Mallows, "Conway's Challenge Sequence," The American Mathematical Monthly, Vol. 98 (Jan. 1991), pp. 520. 92. L.E. Mauland, "An Exercise with Polygonal Numbers," Mathematics Teacher, Vol. 78 (May 1985), pp. 340344. 93. S. B. Maurer and A. Ralston, Discrete Algorithmic Mathematics, AddisonWesley, Reading, MA, 1991. 94. M. Martelli, "The Farmer and the G o o s e  a Generalization," Mathematics Teacher, Vol. 86 (March 1993), pp. 202203. 95. W. S. McCulloch and W. Pitts, "A Logical Calculus of the Ideas Imminent in Nervous Activity," Bulletin of Mathematical Biophysics, Vol. 5 (1943), pp. 115133. 96. W. A. Miller, "Polynomial Numbers and Recursion," Mathematics Teacher, Vol. 83 (Oct. 1990), pp. 555558. 97. B. R. Myers, "Number of Spanning Trees in a Wheel," IEEE Transactions on Circuit Theory, CT18 (March 1971), pp. 280281. 98. H. L. Nelson, Problem 702, J. Recreational Mathematics, Vol. 11:1 (19781979), p. 36. 99. H. L. Nelson, "Two Counterfeits," J. Recreational Mathematics, Vol. 15:1 (19821983), p. 65. 100. J. C. Nichols, Solution to Problem 602, J. Recreational Mathematics, Vol. 11:1 (19781979), p. 75. 101. G. Polya, Mathematical Discovery, combined ed., Wiley, New York, 1981. 102. R. C. Prim, "Shortest Connection Networks and Some Generalizations," Bell System Technical Journal, Vol. 36 (1957), pp. 13891401. 103. I. Vun and P. Belcher, "Catalan Numbers," Mathematical Spectrum, Vol. 29:3 (19961997), pp. 35. 104. A. Wayne, Solution to Problem E579, The American Mathematical Monthly, Vol. 51 (March 1944), p. 165. 105. A. Ralston, "De Bruijn Sequences   A Model Example of the Interaction of Discrete Mathematics and Computer Science," Mathematics Magazine, Vol. 55 (May 1982), pp. 131143. 106. B. Recaman, "The Games of Ham," J. Recreational Mathematics, Vol. 10 (19771978), pp. 251253. 107. J. V. Roberti, "The Indirect Method," Mathematics Teacher, Vol. 80 (Jan. 1987), pp. 4143. 108. K.H. Rosen, Discrete Mathematics and Its Applications, 4th ed., McGrawHill, New York, 1999. 109. K.A. Ross and C. R. B. Wright, Discrete Mathematics, 3rd ed., PrenticeHall, Englewood Cliffs, NJ, 1992.
904
References
110. G.L. Ritter et al., "An Aid to the Superstitious," Mathematics Teacher, Vol. 70 (May 1977), pp. 456457. 111. S. Sahni, Concepts in Discrete Mathematics, 2nd ed., Camelot, Fridley, MN, 1985. 112. B. J. Schwartz, Solution to Problem 1014, J. Recreational Mathematics, Vol. 14:4 (19811982), p. 309. 113. J. Sedlacek, "On the Skeletons of a Graph or Digraph," Proceedings of the Calgary International Conference of Combinatorial Structures and their Applications, Gordon & Breach, New York, pp. 387391. 114. D. E. Shasha, Out of their Minds: The Lives and Discoveries of 15 Great Computer Scientists, Copernicus, New York, 1995, pp. 89101. 115. D.R. Sherbert, "Difference Equations with Applications," UMAP Module 322, Arlington, MA, 1980. 116. R. M. Smullyan, What is the name of this book?, PrenticeHall, Englewood Cliffs, NJ, 1978. 117. R. M. Smullyan, Alice in PuzzleLand: A Carrollian Tale for Children Under Eighty, Penguin Books, New York, 1982. 118. R. M. Smullyan, "Leaps of Logic," Discover (March 1993), p. 96. 119. S.K. Stein, "The Mathematician as an Explorer,"Scientific American, Vol. 204 (May 1961), pp. 149158. 120. S. K. Stein, Mathematics: The ManMade Universe, W. H. Freeman, San Francisco, CA, 1969. 121. A. Sterrett, "Gambling Doesn't Pay," Mathematics Teacher, Vol. 60 (March 1967), pp. 210214. 122. P. Stevens, Patterns in Nature, Little Brown, Boston, 1974. 123. D. R. Stone, "A Different Prime Proof," Mathematics Teacher, Vol. 83 (Jan. 1990), p. 63. 124. A. S. Tanenbaum, Structured Computer Organization, PrenticeHall, Englewood Cliffs, NJ, 1976, pp. 420423. 125. The Official LSAT PrepBook, Law Services, Newtown, PA, 1991. 126. R. M. Thrall, "Insulin Requirements as a Linear Process in Time," Some Mathematical Models in Biology (R. F. Baum, ed.), The University of Michigan Press, Ann Arbor, MI, 1967, pp. 0L2.10L2.4. 127. P. M. Tuchinsky, "International Standard Book Numbers," The UMAP Journal, Vol. 6:1 (1985), pp. 4153. 128. A. Tucker, Applied Combinatorics, Wiley, New York, NY, 1980. 129. T. Tymoczko, "Computers, Proofs, and Mathematics: A Philosophical Investigation of the FourColor Proof," Mathematics Magazine, Vol. 53 (May 1980), pp. 131138. 130. J. H. van Lint and R. M. Wilson, A Course in Combinatorics, Cambridge University Press, New York, 1992. 131. M. vos Savant, Ask Marilyn, St. Martin Press, New York, p. 228. 132. S. Warshall, "A Theorem on Boolean Matrices," J. of the Association of Computing Machinery, Vol. 9 (1962), pp. 1112. 133. J. Williams, "Graph Coloring Used to Model Traffic Lights," Mathematics Teacher, Vol. 85 (March 1992), pp. 212214.
References
905
134. R. J. Wilson and J. J. Watkins, Graphs: An Introductory Approach, Wiley, New York, 1990. 135. D. Wood, "Towers of Brahma and Hanoi Revisited," J. Recreational Mathematics, Vol. 14:1 (19811982), pp. 1724. 136. R. V. Young (ed.), Notable Mathematicians, Gale Research, Detroit, MI, 1997.
Solutions
to OddNumbered Exercises
Exercises 1.1 (p. 17) 1. yes
3. no
9. 1 + 1 ~ = 0 17. F
23.
p
q
TT T F F
F T F
5. F
11. ~ p v q
13. ~ p A ( q v r )
19. T
21. F
~p
~q
~p v ~q
F F T T
F T F T
F T T T
27. F
7. T
29. T
25.
31.
15. T
p q
pvq
TT TF FT FF
T T T F
~q ( p v q ) v ~ q F T F T
p
q
p XOR q
T T F F
T F T F
F T T F
T T T T
33. If two lines are p e r p e n d i c u l a r to the same line, t h e n they are parallel. 35. If x = 1, t h e n
X 2 ~
1.
907
Solutions to OddNumbered Exercises
908
37. converse: If Paris is in England, t h e n London is in France. inverse: If London is not in France, t h e n Paris is not in England. contrapositive: If Paris is not in England, t h e n London is not in France. 39.
~q vp ~ r
41.
~p A ~ q o
45. yes
47. T
51. T
53. T
55.
q
pAq
~p
pAq*
T T F F
T F T F
T F F F
F F T T
F T T T
P
q
pvq
pAq
pVq~pAq
F T F
T T T F
T F F F
TT T F F 59. yes 67.
71. (A' A C)
~'p
T F F T
63. yes
61. yes
(p A q) ~
69.
( ( ~ p) v ( ~ q))
(B'
v
v
C')
v
43. no
49. F
p
57.
~r
65. no
(p ~ q) o
((~p) v q)
(A A B) 75.
73.

@ @
i

@ 9
i
77. Ellen m u s t use computer 1. Exercises 1.2 (p. 29)
1. F
3.
P
,,~p
~ (~p)
T F
F T
T F
L identical J"
0
P
pvp
T F
T F
t
L~
L~
IL
L~
~J
J~
e~
ii~
>
>
r i~~
D.io
~D
0)
23. (Vx)(3y)(xy = x)
25. F
27. T
29. The s q u a r e of every i n t e g e r is nonnegative. 31. T h e r e are i n t e g e r s x a n d y such t h a t x § y  7. 33. T h e r e is an i n t e g e r x such t h a t y  x  y for every i n t e g e r y.
35. T
37. T
39. T
41. T
43. F
45. T
47. T
49. T
51. T
53. T
55. F
57. F
59. T
61. T
63. T
65. F
67. T
p vq
p   . (p v q )
T T T F
T T T T
Exercises 1.4 (p. 45)
1. p ~q ~q
o
p
q
T T 11. valid
T F F
F T F
5. invalid 7. valid 9. valid 13. q is false.
15. r is true. 17. The p r o g r a m is r u n n i n g .
19. Carol is a baby.
21. B e n j a m i n to Cindy a n d Aaron to Daphne; t h e y are 34, 27, 28, and 29 y e a r s old, respectively.
23. blue
25. B 27. A is a k n i g h t and B a knave.
29. yes
31. k n i g h t
33. "I a m red."
35. Kitty is guilty.
37. At least one cowgirl will escape injury.
Exercises 1.5 (p. 54)
1. yes
3. yes
912
Solutions to OddNumbered Exercises
5. L e t x a n d y b e a n y t w o e v e n i n t e gers. Thenx  2m andy 2 n f o r s o m e i n t e g e r s m a n d n. Then x + y  2m + 2n = 2 ( m + n) w h i c h is a l s o a n e v e n i n t e g e r .
7. L e t x  2 m b e a n y e v e n i n t e g e r . T h e n X 2   ( 2 m ) 2  2 ( 2 m 2) is also an even integer. 9. L e t x b e a n y o d d i n t e g e r . T h e n x 2m + 1 for some integer m. .. x 2  ( 2 m + 1)2 = 2(2m 2 + 2m) + 1 w h i c h is a n o d d i n t e g e r .
11. L e t x b e a n y e v e n i n t e g e r a n d y any odd integer. Then x 2m a n d y  2n + 1 for s o m e i n t e g e r s m a n d n. T h e n x y = ( 2 m ) ( 2 n + 1)  2 ( 2 r a n + m ) , a n even integer.
13.
15. L e t x b e a n y i n t e g e r . A s s u m e it is n o t e v e n ; it is o d d a n d is o f t h e f o r m 2 m + 1. T h e n x 2  ( 2 m + 1) 2  4 m 2 + 4 m + 1 = 2(2m 2 + 2m) + 1 w h i c h is a n o d d i n t e g e r . So t h e g i v e n h y p o t h e s i s is f a l s e a n d t h e r e s u l t follows.
L e t x = 4k + 1. T h e n x 2 = (4k + 1) 2  16k 2 + 8k + 1 = 4 ( 4 k 2 + 2 k ) + 1  4 m + 1, w h e r e m  4k 2 + 2k is a n e v e n integer. .'. x 2 is a l s o a n i n t e g e r o f t h e same form.
17.
Let x and y be any two integers. Assume that the given conclus i o n is false; t h a t is, a s s u m e that both x and y are odd integers. Then x  2m + 1 and y  2n + 1 for s o m e i n t e g e r s m andn.." xy = (2m + l)(2n + l) = 2(2ran+re+n)+ 1, a n o d d integer. This negates the given h y p o t h e s i s a n d so t h e r e s u l t follows.
21.
A s s u m e ~/p is a r a t i o n a l n u m b e r a/b, w h e r e a a n d b h a v e no positive common factors except 1. T h e n v / p  a / b , ( a / b ) 2  p o r a 2 = p b 2. C o n s e q u e n t l y , p is a f a c t o r o f a 2 a n d h e n c e o f a. So let a  mp. Then (mp) 2 pb 2 o r b 2  p m 2. A s b e f o r e , t h i s s h o w s p is a f a c t o r o f b. T h u s p is a c o m m o n f a c t o r o f a a n d b, a contradiction.
23.
proof:
19. S u p p o s e ~/2 is n o t a n i r r a t i o n a l n u m b e r ; t h a t is, , / 2 is a r a t i o n a l n u m b e r . L e t ~/2  a / b , w h e r e a and b have no positive common f a c t o r s e x c e p t 1. T h e n ( a / b ) 2 = 2 o r a 2  2b 2. .'. 2 is a fact o r o f a 2 a n d h e n c e o f a. T h e n a  2 m for s o m e i n t e g e r m. .'. ( 2 m ) 2 _ 2b 2 o r b 2  2 m 2 . .'. 2 is a f a c t o r o f b 2 a n d h e n c e o f b. C o n s e q u e n t l y , 2 is a f a c t o r o f b o t h a a n d b, w h i c h c o n t r a d i c t s the assumption.
25. p r o o f : E v e r y i n t e g e r n is o f t h e f o r m 3k, 3k + 1, o r 3k + 2. c a s e 1 L e t n  3k. T h e n n 3  n  (3k) 3  (3k) = 27k33k  3(9k3_k) c a s e 2 L e t n  3k + 1. T h e n n 3  n  (3k + 1) 3  (3k + 1) = (27k 3 + 2 7 k 2 + 9 k + 1)  ( 3 k + 1) = 2 7 k 3 + 2 7 k 2 + 6k = 3 ( 9 k 3 + 9k 2 + 2k)
c a s e I n is a n e v e n i n t e g e r , s a y , 2m. Thenn2+n = (2m)2+2m 2(2m 2 + m), an even integer. c a s e 2 n is a n o d d i n t e g e r , s a y , 2m+1.
Chapter 1
The Language of Logic
913
c a s e 3 L e t n = 3k + 2. T h e n n 3  n  (3k + 2) 3  (3k + 2) = (27k 3 + 5 4 k 2 + 3 6 k + 8)  ( 3 k + 2) = 27k 3 + 54k 2 + 33k + 6 = 3 ( 9 k 3 + 18k 2 + l l k + 2) T h u s , i n e v e r y c a s e , n 3  n is d i v i s i b l e b y 3. 29.
proof: Choose x x 2  x.
31.
proof: Let a be any nonzero integer. T h e n 1729a 3  a 3 + (12a)3 = (9a) 3 + ( 1 0 a ) 3
0
3 7 . x  2, P(x)"
35.
2
x 2 
4.
27.
1. C l e a r l y ,
S i n c e a is a r b i t r a r y , 1 7 2 9 a 3 h a s infinitely many choices.
33.
Then n 2 + n  ( 2 m + 1) 2 + ( 2 m + 1) = ( 4 m 2 + 4 m + 1) + ( 2 m + 1) = 2 ( 2 m 2 + 3 m + 1), again, an even integer.
39.
C a n c e l i n g a  b is i n v a l i d s i n c e ab.
43.
p r o o f : L e t a 9 b  0 a n d a r 0. Sincea.0  0, a . b  a.0. Canceling a from both sides, we g e t b  0.
proof: c a s e 1 L e t x , y > O. T h e n I x . y l = x y = Ixl. [yl c a s e 2 L e t x < 0, y < 0. T h e n I x [   x a n d [y[ =  y . Ix.y[  x y  (  x ) . (  y )  Ix 9lYl c a s e 3 L e t x >__ 0, y < 0. T h e n ]x . y [   ( x y ) = x. (y)  JxJ . [y] c a s e 4 L e t x < 0, y >_ 0. T h i s c a s e is s i m i l a r t o c a s e 3.
41.
p r o o f : S i n c e a < b, t h e r e is a positive real number x such t h a t a + x  b. .. (a + x ) + c = b + c T h a t is, (a + c) + x  b + c ..a+c
(w+y
<xez)
914
Solutions to OddNumbered Exercises p
q
r
q~r
p~(q.r)
T
F
F
T
T
F
T
T
T
T
F
T
F
F
T
17. T
19. T
F
F
T
T
T
23. F
25. T
F
F
F
T
T
27. F
29. F
11. y e s 13. n o
15. [A A (A' v B)] v (A A B') 21. T
31. converse 9 I f x + z < y + z, t h e n x < y. inverse 9 I f x >_ y, t h e n x + z _>_y + z. c o n t r a p o s i t i v e : I f x + z >_ y + z, t h e n x >__y. 33.
I f x _< 3 a n d x >_  3 , t h e n Ixl _< 3.
35. yes
37.
no
39. yes
41. y e s
43.
T
45. T
47. F
49. ( A ' A B ) v ( A ' A B ' ) v ( A A B ' ) = A ' v B '
i 51.
invalid
@ 53.
F
63. p r o o f : L e t x a n d x + I b e
55.
T
57.
F
59. F
61. F
6 5 . p r o o f : n 4  n 2  l ( n  1 ) n ( n + 1)In contains a product of three consecutive integers. Therefore, b y c a s e s , it is d i v i s i b l e b y 3.
two consecutive integers. c a s e 1 L e t x be e v e n . T h e n x  2 m for s o m e i n t e g e r m. T h e n x ( x + 1) = 2 m ( 2 m + 1) 67. i n d i r e c t p r o o f : A s s u m e t h a t t h e = 2 1 m ( m + 1)1, g i v e n c o n c l u s i o n is false, t h a t is, an even integer. a < 6 a n d b < 6. T h e n a + b < c a s e 2 L e t x b e odd. T h e n 6 + 6, t h a t is, a + b < 12, w h i c h x = 2 m + 1 for s o m e i n t e g e r m . contradicts the hypothesis. .'. x ( x + 1)  ( 2 m + 1 ) ( 2 m + 2) 69. p r o o f : A s s u m e t h a t a 2  b 2 a n d = 2 [ ( m + 1 ) ( 2 m + 1)i, a ~: b. S i n c e a 2  b 2 = (a  b) again an even integer. (a + b ) = 0 a n d a  b r O, a + b = O. .. a =  b .
71. 0.5
73. 0.5
75. 0
77. 0.3
79. S i n c e t ( p )  x, t ( p ' ) = 1  x. T h e n t ( p v p ' ) = m a x { x , 1  x}. S u p p o s e t ( p v p ' ) = 1. T h e n m a x { x , 1  x}  1. S u p p o s e 0 < x < 1/2. T h e n max{x, 1  x } = 1  x  1, so x  0; t h a t is, t ( p )  O. O n t h e o t h e r h a n d , let 1/2 < x < 1. T h e n m a x I x , 1  x } = x  1; so t ( p ) = 1. I n e i t h e r case, t ( p ) = 0 or 1. C o n v e r s e l y , let t ( p )  0 o r 1. T h e n t ( p v p ' ) = m a x { t ( p ) , t ( p ' ) } m a x { t ( p ) , 1  t ( p ) } . I f t ( p ) = O, t h e n t ( p v p ' ) = m a x { 0 , 1} = 1; o n t h e o t h e r h a n d , if t ( p )  1, t h e n a l s o t ( p v p ' ) = m a x { l , 0}  1. I n b o t h cases, t ( p v p ' ) = 1. T h u s t ( p v p ' )  1 if a n d o n l y if t ( p )  0 o r t ( p )  1.
Chapter I
81. proof:
915
The Language of Logic
t ( ( p v q ) ' )  1  t ( p v q) = 1  max{t(p),t(q)} = 1  max{x,y}
1
/y
i f x __a, t h e n x >__a o r x _<  a . c o n t r a p o s i t i v e " I f (x _<  a ) o r (x >_ a), t h e n Ixl >__a.
3. ~ q
5.
9.
=_ ( R x ) ( 3 y ) ( x y # y x )
11.
~ [(Vx)(Vy)(xyyx)
~pv~q
7. y e s
~ [ ( V x ) ( 3 y ) ( 3 z ) ( x + y  z) =_ ( 3 x ) ( V y ) ( V z ) ( x + y r z)
13. proof: L e t K = (n 2 9 n ) ( n 2 9 n + 1)(n 2 + n + 2) and L  (n 2  n ) ( n 2  n + 1)(n 2  n + 2). 2 n ( 3 n 4 + 7n 2 + 2)  6 n 5 + 1 4 n 3 + 4 n = (4n 3 + 6 n ) n 2 + (2n 4 + 8 n 2 + 4 ) n = (n 2 + n ) ( n 4 + 2 n 3 + 4 n 2 + 3 n + 2)  (n 2  n ) ( n 4  2 n 3 + 4 n 2  3 n + 2) =KL S i n c e K is t h e p r o d u c t o f t h r e e c o n s e c u t i v e i n t e g e r s , it is d i v i s i b l e b y 3. B u t n 2 + n  n ( n + 1) is d i v i s i b l e b y 2. So n 2 + n + 2 is d i v i s i b l e b y 4. T h u s K is d i v i s i b l e b y 24. L i k e w i s e , L is a l s o d i v i s i b l e b y 24. T h e r e f o r e , K  L is d i v i s i b l e b y 24. T h u s n ( 3 n 4 + 7n 2 + 2) is d i v i s i b l e b y 12.
15. 40 17. t ( p v p ' ) =
max{t(p),t(p')} = m a x { t ( p ) , 1  t(p)} o n l y if t ( p )  0 o r t ( p )  1.
19. N o t a t h r e e  v a l u e d t a u t o l o g y . 21.
Is a t h r e e  v a l u e d t a u t o l o g y .
1
916
Solutions to OddNumbered Exercises
23.
p v q
(p v q)'
p'
q'
p ' ^ q'
0 u 1 u
1 u 0 u
1 1 1 u
1 u 0 1
1 u 0 u
u
u
u
u
u
1 1 1 1
0 0 0 0
u 0 0 0
0 1 u 0
0 0 0 0
1"
identical
t
Exercises 2.1 (p. 76) 1. {April, August}
3. {Jan, March, May, July, Aug, Oct, Dec}
5. { x E Z l 0 < x < 5 }
7. {xlx is a member of the United Nations}
9. yes 15. T
11. yes 17. F
19. F
13. F
21. T
23. T
25. T
27. T
33. 2 n
29. F
31. {O,A}
35. {3,6,9}
37. {2,3,4,6,8,9}
39. {0,{1},{2},{3},{1,3}}
41. b, b2 bab, b3,ba2b
43. ~, b, a 2, a2b, aba
45. 3
49. 00,01,10,11
51. 0, 1, 2
47. 5
53. Consider the implication x e 0 ~ x e A. Since the hypothesis is false, this is a true implication. ". 0 _c A.
55. Let x be an arbitrary element in A. Since A _ B, x e B. Since B C, x e C. Thus, every element in A is also in C..'. A c C. m
Exercises 2.2 (p. 93) 1. {a,b,c,g, j,k}
3. {d, h}
7. { a , c , d , e , f , h , i , j,k}
9. {a}
13. {w,y, z}
15. {a, b}
5. { a , b , c , f , g , i , j,k} 11. {a, b, g} 17. {x,y}
Chapter 2
The Language of Sets
19. {(b, x), (c, x) }
917
21. 0
23. {(b, x), (b, z), (c, x), (c, z) }
25. {(b,x,x), (b,x,z), (c,x,x), (c,x,z)}
27. T
29. F
33. T
35. F
37. F
31. F
39. A = { a } , B  O , C  { a }
41. A 
43. no
45. no
47. Let x e (A')'. Then x r A', so x e A. Thus (A')' _c A. Conversely, let x e A. Then x r A', so x e (A')'. .'. A c_ (A')'. Thus (A')' = A.
49. A N (A u B)  (A N A) u (A N B)
{a},B = {b}, C  {a,b}
=Au(ANB)
 A, since ANB _c A.
51. A o A = ( A  A ) u ( A  A ) =OuO=O
53. A ~ B = (A  B ) u (B  A ) = (B  A) u (A  B) =B~A
55. (A u B u C)' = = =
[(A u B) u C]' (A u B)' N C' (A' n B') N C' A' n B' n C'
57. A N ( A  B )  A  B 59. ( A  B')  ( B  A') = 0
61. A u B  ( A N B ) '  A N B
63. (A N B)' U (A u B')  U
65. (A' u B')' u (A' N B) = B
67. A u
(N ieI
Bi) =
N(A u Bi) ieI
n N (U Bi) = U (n n Bi) ieI
ieI
69. 0
71. {Angelo 0.4, Bart 0.7, Cathy 0.6, Dan 0.7, Elsie 0.2, F r a n k 0.6}
73. 0
75. {Angelo 0.4, Bart 0.7, Cathy 0.6}
77. {(Angelo, Dan) 0.3, (Angelo, Elsie) 0.4, (Angelo, Frank) 0.4, (Bart, Dan) 0.3, (Bart, Elsie) 0.7, (Bart, Frank) 0.4, (Cathy, Dan) 0.3, (Cathy, Elsie) 0.6, (Cathy, Frank) 0.4} 79. {(Angelo, Angelo) 0.4, (Angelo, Bart) 0.4, (Angelo, Cathy) 0.4, (Bart, Angelo) 0.4, (Bart, Bart) 0.7, (Bart, Cathy) 0.6, (Cathy, Angelo) 0.4, (Cathy, Bart) 0.6, (Cathy, Cathy) 0.6} 81. p r o o f : By De Morgan's law in ordinary sets, it suffices to show t h a t d(AnB)'(x)  dA'uB'(X) for every element x.
918
Solutions to OddNumbered Exercises
Let x e (A N B)'. Then d(AnB),(x)  1  d(AnB)(X) = 1  min {dA(x), dB(x)} I 1  dA(x) if clA (x) max then
sum ~ 0 for
Algorithm product (A, B, C)
*)
End 23.
Algorithm print(X) Begin (* algorithm *) i ~1 flag ~ true while (i < n) and (flag) do i f si = Sn+l_ i then i ~i+1 else flag ~ false End (* algorithm *)
25.
max K x i (* algorithm
*)
Algorithm palindrome (S) Begin (* algorithm *) for i = I to n do w r i t e (x i) End (* algorithm *)
(In E x e r c i s e s 2636, P ( n ) d e n o t e s t h e s t a t e m e n t t h a t t h e a l g o r i t h m w o r k s correctly.) 27. Initially, flag = t r u e , a n d i = j = 1. b a s i s s t e p : W h e n n = 1, if azz = b11, flag is still t r u e , so A = B; o t h e r w i s e , flag = false (line 10), so A r B. In b o t h cases, P(1) is t r u e .
938
Solutions to OddNumberedExercises i n d u c t i o n step: A s s u m e P(k) is true. To show t h a t P ( k + 1) is true, it suffices to show t h a t the a l g o r i t h m works correctly for the (k + 1)st rows and columns of A and B. W h e n j  k + 1, t h e n inner loop (lines 610) checks w h e t h e r or not ai(k+l)  bi(k+l). So as i varies from 1 to k + 1, the o u t e r loop (lines 412) checks if the (k + 1)st columns ofA a n d B are equal. W h e n i  k + 1, the i n n e r loop checks if the (k + 1)st rows of A and B are equal. Thus, P(k) > P(k + 1). So, by induction, the a l g o r i t h m works correctly u > 1. 29. Initially, flag = true, and i = j  1. b a s i s step: W h e n n = 1, if a l l _< b11, flag is still true, so A  B; otherwise, flag  false (line 10), so A < B. In both cases, P(1) is true. i n d u c t i o n step: A s s u m e P(k) is true. To show t h a t P ( k + 1) is true, it suffices to show t h a t the a l g o r i t h m works correctly for the (k + 1)st rows and columns of A and B. W h e n j  k + 1, t h e n i n n e r loop (lines 610) checks w h e t h e r or not ai(k+l) 1.
31. b a s i s step: W h e n n  1, A = !all I and B  Ibll I. So
Cll  allbll by line 3.." P(1) is true. i n d u c t i o n step: Assume P(k) is true, t h a t is, the a l g o r i t h m works correctly for any two k x k matrices. In o t h e r words, line 3 gives the correct value of c(i for 1 _< i,j __1. 33. b a s i s step: W h e n n = 1, lines 24, are skipped. So the a l g o r i t h m r e t u r n s the correct value of m i n form line 1..'. P(1) is true. i n d u c t i o n step: Assume P(k) is true. Suppose the algorithm is invoked with n = k + 1 elements. As i varies from 2 t h r o u g h k, the for loop finds the m i n i m u m of the first k n u m b e r s . W h e n n = k + 1, this value is c o m p a r e d to xk+ 1 and the m i n i m u m of x 1 t h r o u g h Xk+ 1 is r e t u r n e d in line 4. .'. P(k) ~ P(k + 1). So, by induction, the a l g o r i t h m works correctly Vn>l.
35. b a s i s step: W h e n n  1, Xn_ i+l

Xl_ 1+1

Xl is p r i n t e d . . ' . P(1) is
true.
i n d u c t i o n step: Assume P(k) is true. Suppose the algorithm is invoked with n = k + 1 elements. W h e n i  1, Xk+l is printed. T h e n as
Chapter4
Induction and Algorithms
939
i varies from 2 t h r o u g h k, Xk, X k  l , . .  , X l a r e p r i n t e d , by t h e i n d u c t i v e h y p o t h e s i s . . ' . P(k) ~ P(k + 1). So, by i n d u c t i o n , t h e a l g o r i t h m w o r k s correctly Vn >_ 1. 37. 2, 3, 5, 6, 8, 13
39. b a s i s step: W h e n n  1, t h e f o r loop is skipped. So P(1) is t r u e by default. i n d u c t i o n step: A s s u m e P(k) is true. S u p p o s e t h e a l g o r i t h m is invoked w i t h n  k + 1 e l e m e n t s . W h e n i = 2, t h e a l g o r i t h m places t h e l a r g e s t of t h e k + 1 e l e m e n t s in t h e correct position. This leaves a sublist w i t h k e l e m e n t s . By t h e i n d u c t i v e hypothesis, t h e a l g o r i t h m correctly sorts it.." P(k) ~ P(k + 1). So, by induction, t h e a l g o r i t h m works correctly Vn > 1. Exercises 4.6 (p. 246) 1. O(n)
3. O(n 3)
7. O ( n l g n )
9. O(n a)
13. 2 ' 2  2 . 2 ..... 2(ntimes) < 2.3 ..... n = O(n!) 17.
5. O ( l g n )
11. O(n 2) 15.
n n n ~i(i+1)= ~i2+ ~i i=1 i=1 i1 n(n + 1) n(n + 1)(2n + 1) = + 6 = O(n 3)
33. (3n)!  3 . 6 . 9 . . .
tt
~ i k 2n  f2(n) m
>3n2 n wheren>4
39. 2 n 3  3 n 2 + 4 n > 2 n 3 , w h e r e n >_ 2 = f2(n 3)
= f2(6") 41. 31gn + 2 _> 31gn = S2(lgn)
43. T r u e , since s u m = n2/4 if n is even a n d (n 2  1)/4.
47. 45. Since fl(n)  O(n(g(n)), Ifl(n)l < Aig(n)l for some c o n s t a n t A > 0. f 2 ( n )  kfl(n). .'. If2(n)[ = klfl(n)l 1. 3 3 . W h e n n = 1, L H S
=
1
(21)(2+1)

31
_

R H S . 9 P ( 1 ) is t r u e .
Chapter 4
Induction and Algorithms
k A s s u m e P ( k ) is true" ~
1
(2i
/=1
k+l
941
1)(2i + 1)

k §   . 2k+1
Then
1
k 1 V~ + A_~ ( 2 i  1)(2i + 1) = Z_, ( 2 i  1)(2i + 1) (2k + 1)(2k + 3)
i=l
i
=
l
k
2k + 1
4
1
k ( 2 k + 3) + 1
(2k + 1)(2k + 3)
(2k + 1)(2k + 3)
(k + 1)(2k + 1)
k + 1
(2k + 1)(2k + 3)
(k+l)+l
.'. P ( k ) ~ P ( k + 1). So the r e s u l t holds by induction.
35. Let P(n): We m u s t select at least 2n + 1 socks to e n s u r e n m a t c h i n g pairs. By the P H P , P(1) is true. A s s u m e P ( k ) is true, t h a t is, we m u s t select at least 2k + 1 socks to e n s u r e k m a t c h i n g pairs. Add one m a t c h i n g pair. This e n s u r e s k + 1 m a t c h i n g pairs by selecting (2k + 1) + 2 = 2(k + 1) + 1 s o c k s . . . P ( k ) ~ P ( k + 1). T h u s P ( n ) is t r u e Vn > 1. D
37.
5(n + 1)n 2
39. 2 n + 2  n 
4 41. 3 n(n+l)(n+2)/6 43. (n + 1 ) !  1
2 m
45.
~ [5i/nj, w h e r e m is the largest i n t e g e r such t h a t 5 m _< n. i=1
47.
t19 + t20 + t21 + t22 + t23 + t24  t25 + t26 + t27 + t28 t29 + t30 + t31 + t32 + t33 + t34 + t35  t36 + t37 + t38 + t39 + t40
49. tn + t n  l t n + l  n ( n + 1)/2 + (n  1)n/2. (n + 1)(n + 2)/2 = [n(n + 1)/2][1 + (n 2 + n  2)/2] = ln(n + 1)/211n(n + 1)/2] = t2n Supplementary Exercises (p. 256) 1. (m 2  n2) 2 + (2mn) 2 = (m 4  2m2n 2 + n 4) + 4m2n 2 = m 4 + 2m2n 2 + n 4 _ (m 2 + n2) 2 3. tk = k ( k + 1)/2. Let n  m ( m + 1)/2. T h e n
(2k + 1)2n + tk 
(2k + 1) 2. m ( m + 1) 2
+
k ( k + 1)
2
[(2k + 1)2(m 2 + m ) + k ( k + 1)] 2 [(2k + 1)2m 2 + (2k + 1 ) 2 m ) + k ( k + 1)] 2 [(2k + 1)m + k][(2k + 1)m + (k + 1)] 2
N ( N + 1)
Solutions to OddNumbered Exercises
942
where N number.

(2k + 1)m + k
". (2k + 1)2n + tk is a t r i a n g u l a r
5. p r o o f : S u p p o s e 111 is a perfect s q u a r e a 2 in some b a s e b, so a 2 b 2 4b + 1 < (b + 1) 2. T h e n (b + 1/2) 2   b 2 4b + 1/4 < b 2 4b + 1. T h a t is, (b + 1/2) 2 < a 2 < (b + 1) 2. This yields b + 1/2 < a < b4 1; t h a t is, a lies b e t w e e n b + 1/2 a n d b + 1, which is impossible. T h u s 111 c a n n o t be a s q u a r e in a n y base. 7. 25 a n d 36 are Duffinian, b u t 18 a n d 43 are not. 9. a ( n ) = ~ d + n  s + n..'. N o n e of t h e n ' s factors, except 1, divides s dtn d#r~
if a n d only if n o n e divides c~(n). T h u s n is Duffinian if a n d only if n o n e of n's factors, except 1, divides z (n). 11. In Exercise 59 in Section 4.4, we e s t a b l i s h e d t h a t Sn  S n  1  n ( 3 n 4 + 7n 2 +2)/12. Since b o t h S n a n d S n  1 a r e positive integers, it follows t h a t n(3n 4 + 7n 2 + 2 ) / 1 2 is also an integer. C o n s e q u e n t l y , 121n(3n 4 + 7n 2 +2).
13. n 3
15. Georgia
Exercises 5.1 (p. 273)
1. 1 , 4 , 7 , 1 0
3. 1 , 2 , 3 , 4
7. 1 , 3 , 4 , 7 , 1 1 , 1 8
9. 3
11. A ( 0 ) A(n)
5. 1 , 1 , 2 , 4
13. $1024
1000 1.015A(n1),n
15. a l 
> 1
1
anan_l+3,
n > 2
17. a o = O an = 2 a n  l + 3, n > 1
19. 00000, 00001, 0 0 0 1 0 , 0 0 0 1 1 , 0 0 1 0 0 , 0 0 1 0 1 , 0 0 1 1 0 , 0 1 0 0 0 , 0 1 0 0 1 , 0 1 0 1 0 , 01011,01100,01101
23. S o 
21. S1 = A1 Sn  Sn1 U An,n
> 2
1
S n  2 S , , _ 1, n > 1
25. a l  a an  r a n _ l , n
>_ 2
Chapter 5
27. 91
Recursion
943
29. 91
31. f(99)  f ( f ( l l 0 ) )  f(100) = f ( f ( l l l ) ) = f(101) =91
33. Let k be t h e smallest i n t e g e r such 90 < x + l l k < 100. T h e n f ( x )  f ( f ( x + 11)) = f(f(f(x + 11.2)))
35. 429
37. 1, 2, 1.66666667, 1.5, 1.66666667, 1.6, 1.625, 1.61538462, 1.61904762, 1.61764706, 1.61818182, = fk+l(x +llk) 1.61797753 Since 90 < x + 11k < 100 a n d k > 1, 39. 2 F n  2 + Fn3 f ( x + 11k) = 91, by Exercise 43. .'. f k + l ( x + l l k ) = fk(91) = F n  2 + (Fn2 + F n  3 ) Since f(91)  91 by Exercise 32, = Fn2 + Fnx fk(91)  9 1 . . ' . f ( x )  91 for = Fn 0<x_ 2
a n  a n  1
31.
an 
n(n
+
1)/2,n > 1
35. an  n ( n + 1)(n + 2)/6, n >_ 1 39.
tn 
n(n
+ 1)/2
an 33.
an 
37. an

n ( n + 2)
if n is e v e n
(n + 1)2/4
otherwise
n(n
+
1)(2n + 1)/6, n > 1
[n(n + 1)/2] 2 n > 1 m
Recursion
Chapter 5
41.
947
9
43. P n  n ( 3 n  1)/2 1 hn1 + 4 ( n 
45. hn 
if n  1
1)+ 1
if n _> 2
47. P n + tn  n  n ( 3 n  1)/2 + n ( n + 1)/2  n = 2n 2  n  (2n  1)n  h n
49. T 1  1 Tn 
T n  1 + n ( n + 1)/2, n _> 2
51. W e s h a l l p r o v e b y P M I t h a t T n  n ( n + 1)(n + 2)/6 Vn > 1. W h e n n  1, T1 = 1 2 . 3 / 6  1, w h i c h is t r u e . So t h e f o r m u l a w o r k s w h e n n  1. A s s u m e it w o r k s for a n a r b i t r a r y p o s i t i v e i n t e g e r k. U s i n g t h e r e c u r rence relation, T k + l  T k + (k + 1)(k + 2)/2  k ( k + 1)(k + 2)/6 + (k + 1)(k + 2)/2
= (k + 1)(k + 2)(k + 3)/6 T h u s , b y P M I , t h e f o r m u l a h o l d s for e v e r y n > 1.
53. Sn = S n  1 + n 2  Sn2
+ (n  1) 2 + n 2
= S n  2 + (n  2) 2 + (n  1) 2 + n 2
= $1 + 22 + 32 + . . .
+ n2
= 12+22+32+...+n
2
= n ( n + 1)(2n + 1)/6
So we c o n j e c t u r e t h a t S n  n ( n + 1)(2n + 1)/6, w h e r e n >_ 1.
55.
1
57. 2
59. a 0  1 , a l  2 an  a n  1
61. a n + an2, n > 2
l1
if n  0
[ (1 + a n  1 ) / k
if n > 1
63. p r o o f (by PMI)" L e t P ( n ) d e n o t e t h e g i v e n s t a t e m e n t . C l e a r l y P ( 0 ) is t r u e . So a s s u m e P ( m ) is t r u e for a n y m >_ 0. T h e n a m + l = (1 + a m ) / k km + k  2
=1+
km(k
1)k
km+l + k  2 km+l(k
1)
T h u s P ( k ) i m p l i e s P ( k + 1), so t h e r e s u l t follows b y P M I .
Exercises 5.3 (p. 296)
1. y e s
3. y e s
5. n o
948
Solutions to OddNumbered E x e r c i s e s
7. y e s
9.
an
= 2 (  1)n _~_ 2 n, n > 0
13.
an

Fn+2, n > 0
17.
an
=
2.3
11.
an 
3 (  2 ) n + 2 . 3 n, n > 0
15.
Ln

a n ~
~n
19.
an

2n 
3n + n . 3n
21.
an

2 n + (2)n+l
23.
a n = 3 . 2 n  n 2 n + n 2 2 n + 2 . 3 n, n > 0
25.
a (np)  a n + b
29.
a(np) ( a n + b ) 2 n if 2 is a c h a r a c t e r i s t i c n m ( a n + b ) 2 n.
31.
a(np) = a n 2 2 n
33.
a(np)  n 2 ( a n 2 + b n + c ) 2 n
35.
an
 2 n  1, n > 0
37.
a n  11 9 4 n  1 1 9 3 n  n 9 3 n + l
39.
an
=
41.
1 + n(n
+
,
m
2.3 n 
(4)n,
> 0
n
a(np) 
an 2 + bn + c
root;
otherwise,
S i n c e rn a n d Sn a r e s o l u t i o n s o f e q u a t i o n
(9), rn  a r n  1
+ S n  1 ) 4 b ( r n  2
n > 0
+ brn2
3,
b, and a 3 = c
(1)
Also, a 3 = aa2 + ba + c a) M u l t i p l y e q u a t i o n
(2) b y a n  3 . a n
(2) 

aa n1 + ba n2 + ca n3
.'. a n is a s o l u t i o n . b) W h e n a n  n a n, + ban2 + Can3
= a(n 
1)a n1 + b(n  2)a n1 + c(n  3)a n3
_ n(aan1
+ ba n2 + ca n3) _ (aa n1 + 2ba n2 + 3ca n3)

n a n  ( 3 a n  6 a n + 3 a n), b y e q u a t i o n s

an = na n
(1)
.'. n a n is a s o l u t i o n .
c) W h e n a n  n 2 a n, aan1
4 b a n  2
= a(n 
and
(x  or)3 _ x 3 _ 3 a x 2 + 3 a 2 x _ a 3
bx  c 
.'. 3 a = a , 3 a 2 
aan_l

+ Sn2). Thus
S i n c e a is a r o o t o f x 3  a x 2  b x  c = 0 w i t h m u l t i p l i c i t y X 3  ax 2
a(np)
1)/2, n > 0
Sn  a S n  1 + b s n  2 . ". rn + Sn  a ( r n  1 a n is a s o l u t i o n o f (9).
43.
n . 3 n, n > 0
n > 0
27.
+
n 
b C a n  3
1)2a n  1 + b ( n  2 ) 2 a n  1 + c ( n  3 ) 2 a n  3
(3)
Chapter 5
Recursion
949
 n 2 ( a o l n  1 Jrbot n  2 F cot n  3 )
 
2 n ( a a n  1 H 2bc~ n  2 H 3cc~n  3 )
H (aol n  1 F 4bol n  2 F 9c(z n  3 )
 n2o~n  a n
2 n (3c~n  6c~n + 3c~n) + (3c~n  12c~ n + 9c~n), b y (1)
_
n2ol n
=
.'. n2ot n is a s o l u t i o n .
Exercises 5.4 (p. 306)
1. 7.
2
1
x1
x+3
1x x2 + 2
f
2x

l,n
5.2
n 
1+2x x1
9.
x2 + 3
13. a n   2 n 
2
3.
§
3 2x + 1
x2+1
17.
an
>
21.
an 
25.
a n  2 n + 3 n . 2 n  3 n, n > 0
8 13(2+3x)
+
7x+4 13(x 2 + 1 )
11 9 an  2 n ,n>O
x2x+l
>_ 1 3 n, n
5.
1  3x
0
(2n + 3)2 n, n > 0
15.
a n = 3 (   2 ) n  2 n, n _> 0
19.
an

Fn+3 , n > 0
2 3 . a n  3 (  2 ) n + 2 n  3 n, n > 0 2 7 . an  n 92 n + l  n 2 2 n , n > O
2 9 . a n  (3n 2  n + 2 ) (  1 ) n  2 n+l n > 0 E x e r c i s e s 5.5 (p. 314)
1.2 7.
3.13 7
9.
an=Fn1
11. Algorithm Fibonacci (n) Begin
(* a l g o r i t h m *) (n = I) or (n = 2) then answer ~ ' ~ ~
0
.,..)
~
O
4
=
~
~ ~
,
(i;
,,
rO
~
4"
c
c
7
~
o
O
O0~
o
"O
v
.,,t1~
s ~Z~ " O
,N
,"',
r'~
~ ,
~
o u~
_~
~
0
~
~
~,,,
~
~
(D
r,
.~
u'}
e~ ~
~
~
O C
+_,
'4
~
, .,~ 4.4
eQ;
~ u~ ,
,.~,;
c~.~ ,rr0
~
+
,'0
•
~~
v
I~ .r
~j ~
H
C
o
~~ (I,1 ~c 4~
.m
~
o c.,
", ~'
s
~
EE ","
~ ~ c
~
o .
4
o',,
o
~
r'~ m ~
3~
3~
....
,
~
~ ~ 4..a v
o
,L
~
ca
e~
~ O
~o
~
.,
~
x
s.r0
9 J:3
0'~
. ~ . , c~
(I.}
~
0
q.~ ,
~ C
E
W
E
~ .~J ~
~
,
v
c~
~ .,s
u'~ ",
~
..
I
,
o~E
s.0
= ~
~
~ ~
~
.e.L
~
~~ ~f~
,,~
"'" ~ ~
~
,4
~
t..,:
~ .,.
~ ,4
+
.,
E ~ E
E
~ X v 4
.,.
• n~ E
4,
~
.,X
,,o
ram ~. "~"~ ,~
~
*r
~ I ~
~
~ ~
~
~J
0
~'o
~
,
~ x
.,
~
(..)
Q;
c~4J
.,
f,
,
U
.
~ q
~
.~
.,
x
C ~
~
~
',4
., ., 4
V I
(1) ",
.,
re
~
~.~
,
.
~ E ~ ., ~
~
~
,
~
E
","
~
~
lJ ~ x
*
.,
v
~
~
.~
X
~
~
.,
~..
~
o
cc.,
~ o
E
I/~ 4~ "~
w
~
"I~ ",
~
0 ~.
,~
..
n~
",'
• •
4.~
~
4~
E
~ 9, 4f O
,.~
o'~
,.~
.,
X
"I~
~ ,,
v
"m~.
.~
I~
,,~I~ ~
~
"~
~
"o
0
,~
O
o
.
o 0 ~
.,
~
E
.
"~ 0
(i.~
E
~ "~ 4
m,
X I~S
.~.~ ~ 9~ " ~
.~
v
c
.r
4~
~
(i;
E
~
=
m~
~
E O
E X ::~
~ .
~
~
ca
" 9r
~
_c:::  , 
4
x
,
,., '  ' ' ~  ~ 4
E ~
,
f,.
X
u
E
s
o~
o'~
rI
v
~
O
.~. ~m
,4
.,.,
~.~ ,~
Q;
#.~
E ~I
X v
t,,,.,
E Z ,,~ 9 0
"~
~
~
~'~
=
o~
Chapter 5
Recursion
951
Exercises 5.6 (p. 318) In Exercises 129, P(n) denotes t h e following s t a t e m e n t : The a l g o r i t h m works correctly. 1. p r o o f : W h e n n  0, t h e a l g o r i t h m r e t u r n s t h e value 1 from line 2. So it works correctly w h e n n  0. A s s u m e P(k) is true, t h a t is, a s s u m e t h a t t h e a l g o r i t h m r e t u r n s t h e value k! for an a r b i t r a r y i n t e g e r k > 0. Suppose we invoke t h e a l g o r i t h m with n  k + 1. T h e n factorial = (k + 1) * factorial (k + 1 ) . k !  (k + 1)!. .'. P(k + 1) is true. Thus, by induction, t h e a l g o r i t h m works correctly Vn>0. 3. p r o o f : Let bn denote t h e n u m b e r of moves needed. W h e n n = 1, disk 1 is moved from X to Z a n d bl  1 (line 4). So P(1) is true. A s s u m e P(k) is true. Suppose t h e a l g o r i t h m is invoked w i t h n  k + 1. By line 8, it correctly t r a n s f e r s the top n  1  k disks from X to Y; count  bk. Disk k + 1 is moved from X to Z (line 9); count  bk + 1. By the IH, the a l g o r i t h m moves the k disks at Y to Z u s i n g bk moves (line 11). Now c o u n t  (bk + 1) + bk  2bk + 1. T h u s P(k) ~ P(k + 1). T h u s the r e s u l t follows by PMI. 5. p r o o f (by s t r o n g induction): Initially, low  1 a n d high  n. W h e n n = 1, mid  1 (line 4). If key  Xl, t h e n the list contains key. If key V: x 1, the a l g o r i t h m is invoked in line 9 or 11. In e i t h e r case, t h e condition in line 1 is not satisfied; so the a l g o r i t h m r e t u r n s the value false for
found. A s s u m e P(i) is t r u e u _ 1. Suppose t h e a l g o r i t h m is invoked with n = k + 1; t h e n low  1, high  k + 1, a n d mid  /(k + 2)/21. If key  X m i d , P(k + 1) is true. Otherwise, line 9 or line 11 is executed. In either case, t h e a l g o r i t h m is invoked for a sublist c o n t a i n i n g < k elements. By t h e IH, the a l g o r i t h m works correctly in each case. So P(k) ~ P(k + 1). T h u s t h e r e s u l t follows by PMI.
7. proof: W h e n n  1, s u m  Xl by line 2. So P(1) is true. A s s u m e P(k) is t r u e for some k >_ 1. Suppose the a l g o r i t h m is invoked for a list w i t h k + 1 elements. If i < k + 1, t h e n t h e a l g o r i t h m c o m p u t e s t h e correct s u m of t h e first k items by t h e hypothesis. W h e n i = k + 1, line 2 is excuted. T h e n s u m  Xk+l is added to t h e previous s u m in line 4. .'. P(k + 1) is true. Thus, by PMI, t h e r e s u l t holds u >_ 1. 9. p r o o f : W h e n n = 1, product = x 1 by line 2. So P(1) is true. A s s u m e P(k) is t r u e for an a r b i t r a r y k >_ 1. Suppose t h e a l g o r i t h m is invoked for a list with k + 1 elements. If i < k + 1, t h e n t h e a l g o r i t h m correctly c o m p u t e s t h e p r o d u c t of t h e first k e l e m e n t s by t h e inductive hypothesis. W h e n i = k + 1, line 2 is executed. T h e n p r o d u c t  Xk+l * previous p r o d u c t in line 4.." P(k + 1) is true. T h u s t h e r e s u l t follows by PMI.
Solutions to OddNumbered Exercises
952
11. p r o o f : W h e n n  1, the list contains one element. So P(1) is true. Now a s s u m e P(k) is true. Consider a list X with k + 1 elements. W h e n i = 1, max  Xl from line 3. In line 7, the a l g o r i t h m is invoked for a list with k elements. By the inductive hypothesis, the correct m a x i m u m value of max and the k elements is r e t u r n e d in line 7. So P(k + 1) is true. T h u s the result follows by PMI. 13. p r o o f : W h e n n = 1, Xl is p r i n t e d by line 2. So P(1) is true. A s s u m e P(k) is true. Consider a list with k + 1 elements. T h e n lines 4 a n d 5 are executed. In line 5, Xl is printed. The r e m a i n i n g k e l e m e n t s are printed in order by line 6, by the inductive hypothesis.." P(k + 1) is true. Thus, by PMI, P(n) is t r u e u > 1. Hence the result. 15. Same as Example 4.36. 17. p r o o f (by s t r o n g induction): W h e n n  1, i  1  j ; flag  t r u e from line 2. ". P(1) is true. Assume P(i) is t r u e Vi < k. Consider a word with k + 1 elements. c a s e 1 I f w l  Wk+l, t h e n f l a g  true by line 5. F u r t h e r , the a l g o r i t h m is invoked with i  2 a n d j  k for a word with k  1 elements. By the inductive hypothesis, the algorithm works correctly for such a word. c a s e 2 I f w l r Wk+l, t h e n f l a g  false by line 9. T h e n also the a l g o r i t h m works correctly. T h u s in both cases, P(k + 1) is true..'. By PMI, the result holds Vn >_ 1.
19. 0
21. 3
23. l g n
25. Clearly, the a l g o r i t h m works when n  1 or n  2. So P(1) and P(2) are true. A s s u m e P(k) is true for any k > 2. Suppose the a l g o r i t h m is invoked with n  k + 1 (> 3). In line 9, the previous n u m b e r (Fk_ 1) and the c u r r e n t n u m b e r (Fk) are added to yield Fk+l. So P(k) ~ P(k + 1). T h u s the result follows by PMI.
Exercises 5.7 (p. 329) 1. O(n 2)
3. O(n 2)
5. O(n 2)
7. O(n2 n)
9.3
11.3
13. 5
15. 5
19. O(n 2)
21. O(n 3)
17. bn  n ,
23. O(n 2)
27. b n = 31. r
n > 0
{o
25. O(n 4) if n 
bnl+(n1)
nb+
35. f ( n )  ( n  1 )
if n > 2
1, n >__0
2+1,n>__1
1
29. O(n 2) 33. Cn [n(n + 1 ) / 2  lib, n > 1 37. f ( n ) 
[lgnJ + 1
(~hapter 5
953
Recursion
39. f ( n )  (no. of bits in t h e b i n a r y r e p r e s e n t a t i o n of n) + (no. of m u l t i p l i c a t i o n s ) < (no. of bits) + (no. of bits in n  1) = ([lg nJ + 1) + [lg nJ = 2[lg nJ + 1 = O(lg n) 41. O(n 3)
43.
Cn
'
(a + 1)n  1  O(n )
45. f ( n ) = af(n/b) + g(n)
= a2f(n/b 2) + ag(n/b) + g(n) = a3f(n/b 3) + a2g(n/b 2) + ag(n/b) + g(n)
k1
= akf(n/bk) + E
aig(n/bi)
i=O k1
= akf(1) + E aig(n/bi) i =0
47. Let n  2 h. By Exercise 45,
49. By Exercise 48,
k1
f(n)
an  2 . 0 + ~ 2i(n/2 i)
 da k + cn[ k1 ~ (a/b)i] 0
0
= 0 + nk = n lg n
= 0 +cn(k~~)1)   c k n 0
= O(nlg n)
= O(nlg n) 53. 5a + 12b, 7a + 19b
51. C n  2 n  2 55. Since n is a p o w e r of 2,
t(n)
[a 2t(n/2) + bn
1
if n  1 otherwise
.'. By Exercise 45,
k1 bn t(n) = 2kt(1) + ~ 2 i i=O 2i = a2 k + kbn = an + kbn = Cn + Ckn, w h e r e C  max{a, b} < Ckn  O(nlg n), since k = lg n
954
Solutions to OddNumbered Exercises
57. By Exercise 56, f (n)  2cn 2 + ( d 
5 9 . hn = ~ 1 hnl+l/n
/
2c)n
if n = 1 if n > 2
< C n 2 JrCn < 2Cn 2
= O(n 2) w h e r e C  max{ 2c, d  2c}. 61. p r o o f (by induction) Let P ( m ) be the given s t a t e m e n t . Since h20 hi  1 > 1 + 0/2, P(0) is true. A s s u m e P ( k ) is t r u e  h 2 k > 1 + k/2. T h e n 2k+l 1 ~ 
h2k + 1 
i=1
l
2k 1 _ / ~~1v~ _, _
1 2k+l
1 ~_..._~ 2 k+l
( k)
1
>
1+~
+2k+1+.+
>
1+ ~
+

1+~
1)
2k+l
2k+1 1
k+l
~
+~1~
.'. P ( k ) ~ P ( k + 1). So the result follows by PMI.
63. h n = .
n 1 i /~1_
b2k;thatis,
a k > n 2.
f (n ) < A a k + B a k = (A + B ) a k = (A + B ) a l~  (A + B ) n l~
O(n
l~
n
956
Solutions to OddNumbered Exercises
Review exercises (p. 334) 1. 7
3. a l   0   a 2 an an1
5. h ( n )  2 n ( n + 1), n > 1 9.
an
2 n
4. a n  2
4.
1, n > 3
7. an  n ( n 4. 1)(n + 2)/3, n > 1
1, n >_ 1
11. L e t P ( n ) : an P ( 1 ) is t r u e . k ( k + 1)(k + (k + 2)(k + by PMI.
 n ( n + 1)(n + 2)/3. W h e n n = 1, az  1 9 2 . 3 / 3 = 2. So A s s u m e P ( k ) is t r u e . T h e n a k + l = ak + (k + 1)(k + 2) 2)/3 + (k + 1)(k + 2)  (k + 1)(k + 2)[k/3 + 1] = (k + 1) 3)/3. T h u s P ( k ) ~ P ( k + 1). So t h e r e s u l t follows
13. L e t P(n)" an  2 n  1. S i n c e a l  21  1 = 1, P ( 1 ) is t r u e . A s s u m e P ( k ) is t r u e : ak  2 k  1. T h e n ak+l  ak + 2 k  (2 k  1) + 2 k = 2 k+l  1. So P ( k ) ~ P ( k + 1). T h u s t h e r e s u l t follows b y P M I . 6 ~ 17. an = ~ (  1 ) n 4.
15. an  F n + 2
333 . 19. an . . . 144
2n  28 28 ~  ~ (  2 ) n 4 2~3n,n >_ 0
173 5 10 3 n 4 ~ 9 7 n + ~x~n + z,n _> 0 IZ
23.
21. an = L(n + 1)/2J < (n + 1)/2
Cn   (n 
1)n/2
27. Cl = 0
m
= O(n)
Cn  ~  C n  1 4. 3n, n > 2
25. 8 29. Cn  3 n ( n
+ 1)/2
33. bn  2 n
31. 9
= O ( n 2)
37. an
35. a l   2 , a 2   4 , a 3   7 an
a n  1 4 . a n  2 4. an_3, n >_ 4
39. Algorithm sum (n) Begin (* sum *) if
n = 1 then sum _ 2
45.
 Fn
Cn   O ( n )
4 1 . L e t P(n)" T h e a l g o r i t h m w o r k s c o r r e c t l y V n > 1. W h e n n = 1, s u m = 1, b y line 2. So P ( 1 ) is true. A s s u m e P ( k ) is t r u e . S u p p o s e n = k + 1. T h e n , b y line 4, s u m  s u m (k) + (2k + 1), w h i c h is t h e s u m of t h e first k o d d p o s i t i v e i n t e g e r s , b y t h e IH. .. P ( k ) ~ P ( k + 1). T h u s t h e r e s u l t follows b y P M I .
Chapter 5
Recursion
957
47. p r o o f (by P M I ) : W h e n n = 1, L H S = 1 = R H S ; so t h e r e s u l t is t r u e w h e n n = 1. N o w a s s u m e it is t r u e for a n for a n a r b i t r a r y p o s i t i v e i n t e g e r k. T h e n k+l
k F2i 1  ~ F2i 1 ~ F2k + 1 i1 i=1 = F2k + F 2 k + l
 F2(k+l)
49. p r o o f (by P M I ) : W h e n n  1, L H S = L1 = 1  L3; so t h e r e s u l t is t r u e w h e n n  1. N o w a s s u m e it is t r u e for a n a r b i t r a r y p o s i t i v e i n t e g e r k. Then k+l
Li i=1
k ~ Li JrLk+l i=1
 (Lk+2  3) fLk+ 1
So, by P M I , t h e r e s u l t is t r u e V n > 1.
= Lk+3  3
m
T h u s , t h e r e s u l t is t r u e V n > 1. 51. p r o o f (by P M I ) : W h e n n  1, L H S  3 = R H S ; so t h e r e s u l t is t r u e w h e n n = 1. N o w a s s u m e it is t r u e for a n a r b i t r a r y p o s i t i v e i n t e g e r k. Then k+l
k+l L2i = ~ L2i 'F L2k+2   ( L 2 k + l i=1 i=1 = L2k+3  1
1)+ L2k+2
T h u s , by P M I , t h e r e s u l t is t r u e Vn > 1. 5 3 . g 2 + l  g 2 _ l  ( F n + l f g n  1 ) ( g n + l  g n  1 ) = LnFn = F2n
55. L e t P ( n ) be t h e g i v e n s t a t e m e n t . W h e n n = 2, R H S = F 2 x + F1  x + 1 = x 2  L H S . . " P ( 2 ) is t r u e . A s s u m e P ( k ) is t r u e : x k  x F k + F k _ 1. T h e n
x k+l = x2Fk + x F k _ l = (x + 1)Fk + x F k _ l
= x(Fk + Fk_ ~) + Fk = xFk+l + gk T h u s P ( k ) ~ P ( k + 1). So t h e r e s u l t follows by P M I . 2n1 5 9 . A ( n )  (n + 1) +  ~ A ( i ) n i=2 n1 n A ( n ) = n ( n + 1) + 2 ~ A ( i ) i=2 n2 .'. (n  1)A(n  1) = (n  1)n + 2 ~ A ( i ) i=2 n A ( n )  (n  1)A(n  1) = 2n + 2 A ( n  1) n A ( n ) = 2 n + (n + 1 ) A ( n 
1)
958
Solutions to OddNumbered Exercises
A(n
A(n)
1)
2 4
n
n+l
A ( n  2) n1 A(1)
n+l
2 2 +  + ~ n n+l
2
2
2
+ g + 4 + ' " + n ~+ l 2 2
2
0 =~ +~+...+ ~ n+l n+l
=2~
1
i=3 t
S u p p l e m e n t a r y E x e r c i s e s (p. 338)
1. t l  1 tn  t n  1
+ n ( n + 1)/2, n >__ 2
3. f 2 _ 2fn + 2  (22'2 + 1) 2  2(22" + 1) + 2 _ 22''+1 + 22+1 '' + 1 22+1 ''  2 + 2 __ 2 2''+1

+ 1 =fn+l
5. ( s o l u t i o n by V. G. F e s e r ) a) C o n s i d e r t h e s e q u e n c e {a,}, w h e r e an+2  a n + l + a,,. T h e n a,,+3 an+2 + a n + l  an+2 + (an+2  an). T h u s a , + an+3 = 2an+2 .'. T h e s e q u e n c e s a t i s f i e s t h e g i v e n r e c u r r e n c e r e l a t i o n . b) U s i n g t h e g i v e n c o n d i t i o n s , a4  2a3  a l  2a3  1 a5  5 a6  2a5  a3 = 1 0  a3
(1)
a12 = 2 9 0  70a3  144 .'. a3 = 2 a n d a4 = 3. S i n c e a5 = 2 a 4  a 2  5, a2 = 1. T h u s t h e s e q u e n c e {an} s a t i s f i e s t h e g i v e n c o n d i t i o n s a n d t h e r e c u r r e n c e r e l a t i o n , so it m u s t b e t h e F i b o n a c c i s e q u e n c e ; t h a t is, an = F,~. 7.
Sn

(3n 2 + 5)n3/8
13. f(n) = r ~(2) + r
9. an  an+5, n E Z
Since r  1 + 1 = 2 is e v e n , f ( n ) is e v e n .
15. f~  2, f2  3 fnfn1
+fn2, n > 3
17. f ( n , 1) = 1 f (n, n ) f(n,k)
1
ifnOorl
0
otherwise
= f ( n  2 , k  1) + f ( n  1,k)
11. 8, 8 is e v e n for n
> 2 and
Combinatorics
Chapter 6
959
Exercises 6.1 (p. 349)
1. 1,317
3. 1,449
9. 200
5. 312
7. 18
11. 36[n/2]
13. 3 n
15. 1000
17. 0
19. 260
21. 100,000
23. 0
25. 1,757,600
27. 0
29. 1,572,120,576
31. 186,624
33. 36,504
35. 32
37. 128
39. 192
41.
43. 24
45. 2 n2
47. 364
16
49. 9 m n+l
55.
1
51. 1,053
53. 1 + m + m2
57. m!
59.
m1
n! (nm)!
b1 bm p~" and . n = . q l 9 . q.m Since . gcd { m . , n }  1, m n b,,, P l "'" ql ...qm is the prime factorization of m n . ". r ( m n ) = (al + 1)... (am + 1)(bl + 1)... (bin + 1 )  r ( m ) r ( n )
61. Let m  plal . al p~,, bl
Exercises 6.2 (p. 358) 1.5
3.60
5. F
7. T
9. F
11. F
13. F
15. F
17. 20
19. 362,880
21. 40,320
23. 358,800
25. 1
27. 562,432
29. 208,827,062,548
31. 103,680
33. 1,693,440
35. 60,480
37. 120 45. P ( n 
39. 6 1, r ) + r P ( n 
41. 6 1, r 
1)
43. 7
( n  1)! r ( n  1)! + 1)! (nr)!
(nr
(n  1)!(n  r + r) (nr)! n! (nr)!
 P ( n , r)
Solutions to OddNumbered Exercises
960
47.
(n + l)!  n!  (n + l)n!  n! = (n + 1  1)n! = n ( n ! )
Algorithm permutations (n,r,answer) (* This algorithm computes P(n,r) where 0 < r _ 4 .'.
(n!)! > (2n)!,
if n > 4
E x e r c i s e s 6.3 (p. 364) 1. 44
3.
1,854
5.
1
7. 265
9. d n 

11. p r o o f : D n  ( n  1)(Dn1 + D n  2 ) S u p p o s e n is odd. T h e n n .'. (n  1)(Dn1 + D n  2 )  D n is e v e n . 13. 2
15. 8
17. bo  0  bl bn 
21.
66
25.
8
29.
10
19.
b n  1 + b n  2 ~ 2, n >_ 2
23. 4
27. 31.
16
= O ( n 2)
0.367879
4 1 . P n  P n  1 =
Dn
n!
Dn1
(n
1)!
Dn  nDn 1
n~ (1) n ~ , by e q u a t i o n (4) n! Dn
~ 2, n > 1
35. a n  ( n  3)(n  2)
37. 0 . 3 6 8 8 7 9
43. D n = ( n 
co  0 Cn  Cn1
33. Cn  2 n = O ( n )
39.
14
1)(Dn1 + D n  2 )
( n  1)  ~ ( D n  1 t D n  2 ) n! n! n1 1 Pn = Pn1 +Pn2 n n
(1)n
1 is e v e n .
Chapter 6
45.
Combinatorics
Notice thatpo E x e r c i s e 43,
= 1 andpl
gn (1)gn1
: (1)
961
Pn Pn1. Thengl
 0. L e t g n

1. By
1)
( ~ n  1 gn2
1
1
n_l)"'(~)g' (1) n n!
47. Algorithm derangements (n) (* This a l g o r i t h m computes the number of derangements of n items using the a l t e r n a t e r e c u r s i v e d e f i n i t i o n . *) Begin (* derangements *) i f n = 0 then derangements ~ I else i f n is odd then derangements + n . derangements(ni)  I else derangements . n . derangements(nl) + I End (* derangements *)
Exercises 6.4 (p. 372) 1. 36
3. 84
9. f n = f n _ l
5. 1 6 , 1 7 0 11. y e s
+n
= f n  2 + (n  1) + n
=fo+(1+2+...
= 1 + n(n +
7. 45
13. 78 15. No. o f g i f t s s e n t o n t h e n t h d a y n i = n(n + 1 ) / 2  C(n + 1 , 2 ) i=1
+n) 1)/2, n >_ 0
17. a n y n o n n e g a t i v e i n t e g e r . 19. 8
21. C(n,r)
23.
243
25.
29. 42
31. 40
33.
105
35. 5
37.
n ( n  1)! RHS =. r ( r  1)!(n  r)! n ( n  1)!
l r ( r  1)!](n  r)! =
n!
r!(nr)!
= LHS
39. R H S =
260
27.
957
C ( n  1, r ) + C ( n  1, r  1) (n
1)!
r!(n  r  1)!
+
(n
1)!
(r  1)!(n  r)! (n  1)!(n  r + r)
r!(nr)! n! = LHS r!(nr)!
962
Solutions to OddNumbered Exercises
41. L e t P ( n ) " g n  C ( n , O) + C ( n , 2 ) +
43. 2
C ( n , 4). T h e n C(0, 0) + C(0, 2) + C(0, 4)
= 1 +0+0=
1 =go.
.'. P(0) is t r u e . A s s u m e P ( k ) is true: gk = C(k, 0) + C ( k , 2) + C(k, 4) g k + l  gk zt C ( k , 1) + C ( k , 3) = C ( k , O) + C ( k , 2) + C ( k , 4) + C ( k , 1) + C ( k , 3) = C ( k , 0) + [C(k, 1) + C ( k , 2)1 + [C(k, 3) + C ( k , 4)1 = C ( k + 1, 0 ) + C ( k + 1, 2 ) + C ( k + 1, 4) .'. P ( k ) ~
P ( k + 1). T h u s t h e r e s u l t follows by PMI.
0 45. A ( n , r) 
ifrO if r = n
0 A(n
l,r) +A(n
l,r1)
+ l
ifO_ 1. 41. p r o o f : n(1 +
n y~r
r1
(;)
X)n1 =
n(n)
r=l
rx r1. Let x  1. T h e n n 9 2 n1 __
9~
I
9
II ~
I
II
~
~'~ ~ ~
~
I
~
~
~
~
9
II
+~
~
~o
o ~
~ ~ +
oII ["~J "~
II
o
o ~
I
'
I
O0
~ I
~I ~
~ ~
"
~
I
~~
"I
I
.
,~
II
~
~
~
0Q II
~~
~
~
~
~
"
IV
~
~O ~
~
~'"
+
o
II
+
I
++
i
I
~.~
+
+
II
~
~ I
l
+
~
~ +
~
~ ++
~
~,
I
I
~~
4
+
+
+
oI
II
ot
II
~ ~
+
~
~
I +
~~
~
~
II [ v J ~ "
~
I
+ +
9
I
~
+
I
[~
+
~'~
+
~
~
+
~
~
~
~
+
+
~ m
~
~
~~
"~
~
9.
IV
0
~,'~x r~
i~
~
~
"
~
Oo
II ~
II
~.
~~
~:
0 0
966
Solutions to OddNumbered Exercises
P(n)
49. proof: Let
d e n o t e t h e g i v e n s t a t e m e n t , w h e r e n > 2. W h e n n  2, k
LHS(~)l(~)=RHS..'P(2)istrue.
AssumeP(k)'i~2(2)
(k+w l ) h e r e' k > 2 " 3
Thenk~_]l(~)_ ~ (~)_~_ ( k + 1 ) = i=2
.. P(k +
i=2
2
(k+l) + (k+l) 3 / 2
1) is t r u e . T h u s , by P M I , t h e r e s u l t h o l d s Vn >_ 2.
51. p r o o f : L e t P ( n ) d e n o t e t h e g i v e n s t a t e m e n t , w h e r e n >_ 3. W h e n n  3, / n \
/ A N
L H S  ( 3 )  1 \,
~ (3)i=3
/
(4)\
RHS...
P(3)is
9
true. AssumeP(k)
/
( k + 'lw ) herek>34

"
Then i=3
.'. P(k +
i=3
1) is t r u e . T h u s , by P M I , t h e f o r m u l a h o l d s Vn >_ 3.
Exercises 6.7 (p. 408) 1. 74
3. 533
5. 266
7. 21
9. 30
11. L e t S b e t h e s e t of s o l u t i o n s w i t h x < 3, y _< 4, a n d z < 4. T h e n IS]C(3+111,11)=78. Letp1"x>3, p2y>4, andp3z> 5. Then"
N(pl)
= no. of s o l u t i o n s w i t h x >__ 4  C(3 + 7  1, 7)  36
N ( p 2 ) = no. of s o l u t i o n s w i t h y >_ 5  C(3 + 6  1, 6)  28
N(p3)
= no. o f s o l u t i o n s w i t h z >_ 6  C(3 + 5  1, 5)  21
N ( p l p 2 )  no. o f s o l u t i o n s w i t h x >_ 4 a n d y >_ 5  C(3 + 2  1, 2)  6 N ( p l P 3 )  no. o f s o l u t i o n s w i t h x >__4 a n d z >_ 6  C(3 + 1  1, 1) = 3 N ( p 2 P 3 ) = no. of s o l u t i o n s w i t h y >_ 5 a n d z >_ 6  C(3 + 0  1, 0) 
1
N ( p l p 2 P 3 )  no. o f s o l u t i o n s w i t h x >__4, y >_ 5, a n d z >__6 = C(3  4  1 ,  4 )  0 ..
answer 
N(p'lp'~' 3) 
78  (36 + 28 + 21) + (6 + 1 + 3)  0  3
13. W h e n n  5, L H S  600 = 5(5!)  R H S ; w h e n n  6, L H S  3 6 0 0 5(6~) RHS.
Chapter 6
Combinatorlcs
967
Exercises 6.8 (p. 416)
1 13 1890 11. 1000000 1.  
21.
0
2 5. 13 98901 13. 15. 1000000 2 23. 25. 5 3.
~
1 36 1 w 17
1 12 13 17. 102 39 27. 95
9 1000000 2 19. 429 9 29. 19
7.
0
9.
Exercises 6.9 (p. 425)
1.
2
11
3.
5
5. 1
13
4
15. ~1
17. 161
29.
31.
$1.75
19. 0
9.0
4
7
21.
125 5 92.11r 33. 3~ 35. ~
n
43. E 
7. 1
11
425
1 13. 2 
27.
20 273
256 39. 6~ 4 1 .
624 625
15 25. 91
23. 0 37.
"
n
~ k C ( n , k ) p k q n  k = ~ kC(n,k)pkq nh k=0
k=l n
By the binomial theorem, n(1 + x) n 1 = ~ kC(n, k)x h 1. Let x = p/q. k=l
Then n(p + q)n1 n pk1 n pk1 qn1 = ~ kC(n,k) qk1 = ~ kC(n,k) qk1 k=l
k=l
Since p + q = 1 in a Bernoulli trial, this yields n
np  y~ kC(n, k )pkqnk = E k=l
Review Exercises (p. 429)
1. 1,038
3. 1,454
9. 1,542,636,576 11. 0
5. 1,572,120,576 13. 80
7. 436,700,160 15. 1,296
17. 2,401
19. 103,680
21.
39,916,800
23. 59,049
25. 176
27. 2,598,960
29.
3,744
31. 34,650
33. 20
968
Solutions to OddNumbered Exercises
35. h ( n )  h ( n  1)+ n u m b e r of h a n d s h a k e s m a d e by t h e n t h couple  h ( n  1) + 4n, w h e r e h(0)  0. 37. 84
39. 26
41. 165
43.  3 3 0
45. x 6 + 6 x 5 y + 15x4y 2 + 20x3y 3 + 15x2y 4 + 6 x y 5 + y 6 47. 243x 5  4 0 5 x 4 y + 270x3y 2  90x2y 3 + 15xy 4  y 5 49. p r o o f (by s t r o n g induction)" Let P ( n ) d e n o t e t h e given s t a t e m e n t . W h e n n  0, r  0. T h e n C ( n , r )  C(0,0)  1 is an integer. So P(0) is true. A s s u m e P ( i ) is t r u e Vi _< k, w h e r e k > 0. By T h e o r e m 6.12, C ( k + 1, r) C ( k , r  1) + C ( k , r ) . Both n u m b e r s on t h e R H S are integers, by t h e inductive hypothesis. So t h e i r s u m is also an integer. ". P ( k + 1) is true. Thus, by PMI, t h e result holds u > 0. n
51. No. of gifts s e n t on t h e n t h day = ~ i  n ( n + 1)/2 = C ( n + 1, 2) i=1
53. p r o o f : Consider a p e r m u t a t i o n of a set of n e l e m e n t s . Suppose exactly k of t h e m occupy t h e i r n a t u r a l positions. N o n e of t h e r e m a i n i n g n  k e l e m e n t s occupy t h e i r n a t u r a l positions; the n u m b e r of such a r r a n g e m e n t s is D n  k . B u t the original k e l e m e n t s can be chosen in C ( n , k ) different ways. Therefore, the n u m b e r of p e r m u t a t i o n s with exactly k e l e m e n t s in t h e i r n a t u r a l positions is C ( n , k ) D n _ k . 57. A(n, 1 )  n  1
55. 2 n
59. p r o o f ( b y PMI)" L e t A ( n , r )  C ( n , r )  l . W h e n n  1, R H S  C ( n , 1 ) 1  n  1  A ( n , 1)  LHS. So P(1) is true. A s s u m e P ( k ) is true: A ( k , r)  C ( k , r)  1. T h e n A ( k + 1, r)  A ( k , r = IC(k,r.'.
1)+A(k,r)+
1 
1) ll+lC(k,r)
IC(k,r
11+ 1
1) + C ( k , r ) l  1  C ( k + 1, r)  1  A ( k + 1, r)
P ( k )  ~ P ( k + 1). T h u s the r e s u l t follows by PMI.
61. $375,000
63. 0.40951
Supplementary Exercises (p. 432)
1. Let k be the least of the r consecutive positive integers. T h e i r p r o d u c t is k ( k + 1 ) . . . (k + r  1). Since
k ( k + 1 ) . . . (k + r 
1)
rt is an integer, k ( k + 1 ) . . . (k + r  1) is divisible by rt.
3.
 C ( k + r  1, r)
Cn  C ( 2 n , n ) / ( n + 1) is an integer; so n + 1 I C ( 2 n , n ) .
5. R H S 
C ( 2 n + 1, n) 2n + 1
=
(2n + 1)! n t ( n + 1)t(2n + 1)
=
(2n)t n t ( n + 1)!
= Cn  L H S
(~hapter 6
Combinatorics
969
7. 13; 80 9. We shall use the following identities:
1. n
(;_~)
=k
(;)
k2  ~ k=O
k=O
=n
n (;) 2. ~ k=O
k.k  ~ n k=O
k  n Z k=O
n (~ ~)
~ k =0
(k1)+n
nk~o(n1)
(k 2) §
=n(n1)
n (;) 3. ~~k k=l
2 n
 n 2 n1 ( k  1 + 1)
~(~ ~) k=0
( k
n2 ( ~ _ 2 ) ni (;_ ~ +n k=2 k=l
~)
+ n n~ ) ( 1n k 1 k=O = n(n  1). 2 ''2 + n . 2 ''1  n(n + 1)2 ''2
= n(n  1) k=o'~2() n _ k2
11. d=lj=0 k=0
d=lj=0 =F~
J + 1
1E(~11) +
1]
d=l
'~'()
= ~ n+l ,=o r
()(
_
n+l 0
_
n+l 1
)
(:~11) + ,n 1, =2 ''~11(n+1)1(n1) = 2 ''+1 
2n  2
13. no, no
15.
f(n,k)

n
if k  1
0
ifkn
f(n2,
k1)+f(nl,k)
if 1 < k < n
17. By Exercise 16, f ( n , k )  C(n  k + 1, k). .'. Total no. of subsets t h a t do not contain consecutive integers is given by n
t/
n
f ( n , k )  ~ C(n  k + 1, k)  ~ C((n + 2)  k  1,k)  F,,+2 k=o k=o k =o 19. Fn+2
r._,.
~, 0
o
>
"~"
II
~'11
'~" 1'
m
3x
~5I'D
0
3
~n
~
~
'..
~.
,'4
m
3
II
o
~.
r
e@
~
'"
~
,.., m 
0
>
C ~
> ~.: ~"
>
~,:
>
~..:
~"~':
II
,~,
II
II
~' ~ :
II
II
II
"' ~,:
~
II
II
II
~
II
~"
~> "
"~"
>
.~.
~
>
~.. ~.: ~.:
II
II
>
~1
">~