Phil Lucht Math & Physics Archive
Home / Math and Physics Files / Math / Math Book Downloads

Ken, Rosen, Discrete Mathematics and Its Applications 6th Edition 2007

PDF · 1008 pages · 25.3 MB
Open PDF file

Published textbook by Kenneth H. Rosen (McGraw-Hill, 2007), kept among the math book downloads and not written by Phil. The front matter lists chapters on logic and proofs, sets and functions, algorithms and integers, induction and recursion, counting, discrete probability, recurrence relations, relations, graphs, trees and Boolean algebra. Later chapters appear to cover further topics, but only the first part of the text was seen.

AI-written summary; may contain errors.

Extracted text (machine-read; may contain errors)
DISCRETE MATHEMATICS AND ITSAPPLICATIONS Kenneth H.Rosen weonaw.mite INTABNATIONAL EDITION LIST OF SYMBOLS TOPIC SYMBOL MEANING PAGE LOGIC ""'p negation of p 3 p 1\ q conjunction of p and q 4 pvq disjunction of p and q 4 P (J) q exclusive or of p and q 5 p-+q the implication p implies q 6 p ++ q biconditional of p and q 9 p=.q equivalence of p and q 22 T tautology 24 F contradiction 24 P(xJ, ... ,xn) propositional function 32 VxP(x) universal quantification of P (x) 34 3xP(x) existential quantification of P (x) 36 3!xP(x) uniqueness quantification of P(x) 37 therefore 63 p{S}q partial correctne ss of S 323 SETS XES x is a member of S 112 x fj S x is not a member of S 112 {aJ, ... , an} list of elements of a set 112 {x I P(x)} set builder notation 112 N set of natural numbers 112 Z set of integers 112 z+ set of positive integers 113 Q set of rational numbers 113 R set of real numbers 113 S=T set equality 113 0 the empty (or null) set 114 SC;T S is a subset of T 114 SeT S is a proper subset of T 115 lSI cardinality of S 116 peS) the power set of S 116 (aJ, ... , an) n-tuple 117 (a, b) ordered pair 117 AxB Cartesian product of A and B 118 AUB union of A and B 121 AnB intersection of A and B 121 A-B the difference of A and B 123 A complement of A 123 n UAi union of Ai, i = 1, 2, ... , n 127 i=J n nAi intersection of Ai, i = 1, 2, ... , n 128 i=J A(J)B symmetric difference of A and B 131 TOPIC SYMBOL MEANING PAGE FUNCTIONS J(a) value of the function J at a 133 J:A ---+ B function from A to B 133 JI+h sum of the functions JI and J2 135 Jd2 product of the functions JI and 12 135 J(5) image of the set 5 under J 136 LA(S) identity function on A 138 J-I(x) inverse of J 139 Jog composition of J and g 140 LxJ floor function of x 143 fxl ceiling function of x 143 an term of {ai} with subscript n 150 n Lai sum of ai, a2, ... , an 153 i=1 Laa sum of aa over a E 5 156 aES n fl an product of ai, a2, ... , an 162 i=! J(x) is O(g(x» J(x) is big-O of g(x) 180 n! n factorial 185 J(x) is Q(g(x» J(x) is big-Omega of g(x) 189 J(x) is 8(g(x» J(x) is big-Theta of g(x) 189 asymptotic 192 min(x,y) minimum of x and y 216 max(x,y) maximum of x and y 217 � approximately equal to 395 INTEGERS alb a divides b 201 alb a does not divide b 201 a div b quotient when a is divided by b 202 a mod b remainder when a is divided by b 202 a == b (modm) a is congruent to b modulo m 203 a =1= b (mod m) a is not congruent to b modulo m 203 gcd(a, b) greatest common divisor of a and b 215 1cm (a, b) least common multiple of a and b 217 (akak-I ... alaO)b base b representation 219 MATRICES [aij ] matrix with entries aij 247 A+B matrix sum of A and B 247 AB matrix product of A and B 248 In identity matrix of order n 251 At transpose of A 251 AvB join of A and B 252 AAB the meet of A and B 252 A0B Boolean product of A and B 253 A[n] nth Boolean power of A 254 (List of Symbols continued at back of book) Discrete Mathematics and Its Applications Sixth Edition Kenneth H. Rosen AT&T Laboratories Boston Burr Ridge, IL Dubuque, IA New York San Francisco St. Louis Bangkok Bogota Caracas Kuala Lumpur Lisbon London Madrid Mexico City Milan Montreal New Delhi Santiago Seoul Singapore Sydney Taipei Toronto The McGraw Hilt Compames 4 DISCRETE MATHEMATICS AND ITS APPLICATIONS, SIXTH EDITION International Edition 2007 Exclusive rights by McGraw-Hill Education (Asia), for manufacture and export. This book cannot be re-exported from the country to which it is sold by McGraw- Hill. The International Edition is not available in North America. Published by McGraw-Hill, a business unit of The McGraw-Hill Companies, Inc., 1221 Avenue of the Americas, New York, NY 10020. Copyright © 2007 by Kenneth H. Rosen. All rights reserved. No part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written consent of The McGraw-Hill Companies, Inc., including, but not limited to, in any network or other electronic storage or transmission, or broadcast for distance learning. Some ancillaries, including electronic and print components, may not be available to customers outside the United States. 10 09 08 07 06 05 04 03 02 01 20 09 08 07 06 CTF BJE The credits section for this book begins on page C-I and is considered an extension of the copyright page. When ordering this title, use ISBN-13: 978-007-124474-9 or ISBN-I0: 007-124474-3 Printed in Singapore www.rnhhe.com Contents Preface vii The MathZone Companion Website xviii To the Student xx 1 The Foundations: Logic and Proofs .................... .............. 1 1.1 Propositional Logic .................... ............ .......... ................... 1 1.2 Propositional Equivalences ............. ................... .......... ........... 21 1.3 Predicates and Quantifiers ...................................................... 30 1.4 Nested Quantifiers ............................................................. 50 1.5 Rules of Inference ............................................................. 63 1.6 Introduction to Proofs ..................................... .......... ........... 75 1.7 Proof Methods and Strategy .................................................. .. 86 End-of-Chapter Material ...................................................... 104 2 Basic Structures: Sets, Functions, Sequences, and Sums ....... .... 111 2.1 . Sets ................................................................ : ........ 111 2.2 Set Operations ............................................................... 121 2.3 Functions .............................. ...................................... 133 2.4 Sequences and Summations ................................................... 149 End-of-Chapter Material ...................................................... 163 3 The Fundamentals: Algorithms, the Integers, and Matrices ........ 167 3.1 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 3.2 The Growth of Functions ...................................................... 180 3.3 Complexity of Algorithms ..................................................... 193 3.4 The Integers and Division ......................... ............................ 200 3.5 Primes and Greatest Common Divisors ......................................... 210 3.6 Integers and Algorithms ....................................................... 219 3.7 Applications of Number Theory ............................................... 231 3.8 Matrices ..................................................................... 246 End-of-Chapter Material .......... ............................................ 257 4 Induction and Recursion ................................... ....... 263 4.1 Mathematical Induction ....................................................... 263 4.2 Strong Induction and Well-Ordering ........................................... 283 4.3 Recursive Definitions and Structural Induction .................................. 294 4.4 Recursive Algorithms ...................... ................................... 311 iii Contents 4.5 Program Correctness .................................. ....................... 322 End-of-Chapter Material ............................ .......................... 328 5 Counting .............................. ............................ 335 5.1 The Basics of Counting ........................ ............... ................ 335 5.2 The Pigeonhole Principle ................... ............ ................... .... 347 5.3 Permutations and Combinations ............................ ................... 355 5.4 Binomial Coefficients ..................... .......... .......................... 363 5.5 Generalized Permutations and Combinations .......... .......................... 370 5.6 Generating Permutations and Combinations .................. ................... 382 End-of-Chapter Material .............................. ......... ............... 386 6 Discrete Probability ............................ ................... 393 6.1 An Introduction to Discrete Probability ........... ......... ..................... 393 6.2 Probability Theory ........................ ............ ....................... 400 6.3 Bayes' Theorem ............................. ................................. 417 6.4 Expected Value and Variance .................. ................................ 426 End-of-Chapter Material ............................. ......................... 442 7 Advanced Counting Techniques ........... ........................ 449 7.1 Recurrence Relations ............................. ............................ 449 7.2 Solving Linear Recurrence Relations .................... ....................... 460 7.3 Divide-and-Conquer Algorithms and Recurrence Relations ....................... 474 7.4 Generating Functions ............ .... ......... .... ............................ 484 7.5 Inclusion-Exclusion ..................... ..................................... 499 7.6 Applications ofInclusion-Exclusion ... ........ ......... ...... ................. 505 End-of-Chapter Material .................. ............. ....................... 513 8 Relations ................................. ......................... 519 8.1 Relations and Their Properties ..................... ............................ 519 8.2 n-ary Relations and Their Applications ........... .............................. 530 8.3 Representing Relations ............................... ........................ 537 8.4 Closures of Relations ................................. ........................ 544 8.5 Equivalence Relations ................................ ........................ 555 8.6 Partial Orderings ............. .................... ............................ 566 End-of-Chapter Material .......................... ............................ 581 9 Graphs ..................................... ....................... 589 9.1 Graphs and Graph Models ................ ........... .......................... 589 9.2 Graph Terminology and Special Types of Graphs ................................ 597 9.3 Representing Graphs and Graph Isomorphism .............. ..................... 611 9.4 Connectivity ............................. ........ ............................ 621 Contents v 9.5 Euler and Hamilton Paths ..................................................... 633 9.6 Shortest-Path Problems ....................................................... 647 9.7 Planar Graphs ................................................................ 657 9.8 Graph Coloring .............................................................. 666 End-of-Chapter Material ...................................................... 675 10 Trees ............................................................... 683 10.1 Introduction to Trees .......................................................... 683 10.2 Applications of Trees ......................................................... 695 10.3 Tree TraversaL ............................................................... 710 10.4 Spanning Trees .............................................................. 724 10.5 Minimum Spanning Trees ..................................................... 737 End-of-Chapter Material ...................................................... 743 11 Boolean Algebra ................................................... 749 11.1 Boolean Functions ............................................................ 749 11.2 Representing Boolean Functions ............................................... 757 11.3 Logic Gates .................................................................. 760 11.4 Minimization of Circuits ...................................................... 766 End-of-Chapter Material ...................................................... 781 12 Modeling Computation ................................. .......... 785 12.1 Languages and Grammars ..................................................... 785 12.2 Finite-State Machines with Output ............................................. 796 12.3 Finite-State Machines with No Output .......................................... 804 12.4 Language Recognition ........................................................ 817 12.5 Turing Machines ............................................................. 827 End-of-Chapter Material ...................................................... 838 Appendixes ....................................................... A-I A-I Axioms for the Real Numbers and the Positive Integers .......................... A-I A-2 Exponential and Logarithmic Functions ........................................ A-7 A-3 Pseudocode ................................................................ A-IO Suggested Readings B-1 Answers to Odd-Numbered Exercises S-1 Photo Credits C-l Index of Biographies 1-1 Index 1-2 About the Author Kenneth H. Rosen has had a long career as a Distinguished Member of the Technical Staff at AT&T Laboratories in Monmouth County, New Jersey. He currently holds the position of visiting research professor at Monmouth University where he is working on the security and privacy aspects of the Rapid Response Database Project and where he is teaching a course on cryptographic applications. Dr. Rosen received his B.S. in Mathematics from the University of Michigan, Ann Arbor (1972), and his Ph.D. in Mathematics from M.LT. (1976), where he wrote his thesis in the area of number theory under the direction of Harold Stark. Before joining Bell Laboratori es in 1982, he held positions at the University of Colorado, Boulder; The Ohio State University, Columbus; and the University of Maine, Orono, where he was an associate professor of mathema tics. While working at AT&T Labs, he taught at Monmouth University, teaching courses in discrete mathemati cs, coding theory, and data security. Dr. Rosen has published numerous articles in professional journals in the areas of number theory and mathematical modeling. He is the author ofthe textbooks Elementary Number Theory and Its Applications, published by Addison-Wesley and currently in its fifth edition, and Discrete Mathematics and Its Applications, published by McGraw-Hili and currently in its sixth edition. Both of these textbooks have been used extensively at hundreds of universities throughout the world. Discrete Mathematics and Its Applications has sold more than 300,000 copies in its lifetime with translations into Spanish, French, Chinese, and Korean. He is also co-author of UNIX The Complete Reference; UNIX System V Release 4: An Introduction; and Best UNIX Tips Ever, each published by Osborne McGraw-Hili. These books have sold more than 150,000 copies with translations into Chinese, German, Spanish, and Italian. Dr. Rosen is also the editor of the Handbook of Discrete and Combinatorial Mathematics, published by CRC Press, and he is the advisory editor of the CRC series of books in discrete mathematics, consisting of more than 30 volumes on different aspects of discrete mathematics, most of which are introduced in this book. He is also interested in integrating mathematical software into the educational and professional environments, and has worked on projects with Waterloo Maple Inc.'s Maple software in both these areas. At Bell Laboratories and AT&T Laborator ies, Dr. Rosen worked on a wide range of projects, including operations research studies and product line planning for computers and data commu­ nications equipment. He helped plan AT&T's products and services in the area of multimedia, including video communications, speech recognition and synthesis, and image networking. He evaluated new technology for use by AT&T and did standards work in the area of image network­ ing. He also invented many new services, and holds or has submitted more than 70 patents. One of his more interesting projects involved helping evaluate technology for the AT&T attraction at EPCOT Center. Preface In writing this book, I was guided by my long-standing experience and interest in teaching discrete mathematics. For the student, my purpose was to present material in a precise, readable manner, with the concepts and techniques of discrete mathematics clearly presented and demonstrated. My goal was to show the relevance and practicality of discrete mathematics to students, who are often skeptical. I wanted to give students studying computer science all of the mathematical foundations they need for their future studies. I wanted to give mathematics students an understanding of important mathematical concepts together with a sense of why these concepts are important for applications. And most importantly, I wanted to accomplish these goals without watering down the material. For the instructor, my purpose was to design a flexible, comprehensive teaching tool using proven pedagogical techniques in mathemati cs. I wanted to provide instructors with a package of materials that they could use to teach discrete mathematics effectively and efficiently in the most appropriate manner for their particular set of students. I hope that I have achieved these goa ls. I have been extremely gratified by the tremendous success of this text. The many improve­ ments in the sixth edition have been made possible by the feedback and suggestions of a large number of instructors and students at many of the more than 600 schools where this book has been successfully used. There are many enhancements in this edition. The companion website has been substantially enhanced and more closely integrated with the text, providing helpful material to make it easier for students and instructors to achieve their goals. This text is designed for a one-or two-term introductory discrete mathematics course taken by students in a wide variety of majors, including mathematics, computer science, and engineering. College algebra is the only explicit prerequisite, although a certain degree of mathematical maturity is needed to study discrete mathematics in a meaningful way. Goals of a Discrete Mathematics Course A discrete mathemati cs course has more than one purpose. Students should learn a particular set of mathematical facts and how to apply them; more importantly, such a course should teach students how to think logically and mathematically. To achieve these goals, this text stresses mathematical reasoning and the different ways problems are solved. Five important themes are interwoven in this text: mathematical reasoning, combinatorial analysis, discrete structures, algorithmic thinking, and applications and modeling. A successful discrete mathemati cs course should carefully blend and balance all five themes. 1. Mathematical Reasoning: Students must understand mathematical reasoning in order to read, comprehend, and construct mathematical arguments. This text starts with a discussion of mathematical logic, which serves as the foundation for the subsequent discussions of methods of proof. Both the science and the art of constructing proofs are addressed. The technique of mathematical induction is stressed through many different types of examples of such proofs and a careful explanation of why mathematical induction is a valid proof technique. 2. Combinatorial Analysis: An important problem-solving skill is the ability to count or enu­ merate objects. The discussio n of enumeration in this book begins with the basic techniques of counting. The stress is on performing combinatorial analysis to solve counting problems and analyze algorithms, not on applying formulae. vii vJli Preface 3. Discrete Structures: A course in discrete mathematics should teach students how to work with discrete structures, which are the abstract mathematical structures used to represent discrete objects and relationships between these objects. These discrete structures include sets, permutati ons, relations, graphs, trees, and finite-state machines. 4. Algorithmic Thinking: Certain classes of problems are solved by the specification of an algorithm. After an algorithm has been described, a computer program can be constructed implementing it. The mathematical portions of this activity, which include the specification of the algorithm, the verification that it works properly, and the analysis ofthe computer memory and time required to perform it, are all covered in this text. Algorithms are described using both English and an easily understood form of pseudocode. 5. Applications and Modeling: Discrete mathemati cs has applications to almost every conceiv­ able area of study. There are many applications to computer science and data networking in this text, as well as applications to such diverse areas as chemistry, botany, zoology, linguis­ tics, geography, business, and the Internet. These applications are natural and important uses of discrete mathematics and are not contrived. Modeling with discrete mathematics is an extremely important problem-solving skill, which students have the opportunity to develop by constructing their own models in some of the exercises. Changes in the Sixth Edition The fifth edition of this book has been used successfully at over 600 schools in the United States, dozens of Canadian universities, and at universities throughout Europe, Asia, and Oceania. Although the fifth edition has been an extremely effective text, many instructors, including longtime users, have requested changes designed to make this book more effective. I have devoted a significant amount of time and energy to satisfy these requests and I have worked hard to find my own ways to make the book better. The result is a sixth edition that offers both instructors and students much more than the fifth edition did. Most significantly, an improved organization of topics has been implemented in this sixth edition, making the book a more effective teaching tool. Changes have been implemented that make this book more effective for students who need as much help as possible, as well as for those students who want to be challenged to the maximum degree. Substantial enhancements to . the material devoted to logic, method of proof, and proof strategies are designed to help students master mathematical reasoning. Additional explanations and examples have been added to clarify material where students often have difficulty. New exer cises, both routine and challenging, have been inserted into the exercise sets. Highly relevant applications, including many related to the Internet and computer science, have been added. The MathZone companion website has benefited from extensive development activity and now provides tools students can use to master key concepts and explore the world of discrete mathematics. Improved Organization • The first part of the book has been restructured to present core topics in a more efficient, more effective, and more flexible way. • Coverage of mathematical reasoning and proof is con­ centrated in Chapter 1, flowing from propositional and predicate logic, to rules of inference, to basic proof techniques, to more advanced proof techniques and proof strategies. • A separate chapter on discrete structures--Chapter 2 in this new edition---covers sets, functions, sequence, and sums. • Material on basic number theory, covered in one sec­ tion in the fifth edition, is now covered in two sections, the first on divisibility and congruences and the second onpnmes. • The new Chapter 4 is entirely devoted to induction and recursion. Logic • Coverage of logic has been amplified with key ideas explained in greater depth and with more care. • Conditional statements and De Morgan's laws receive expanded coverage. • The construction of truth tables is introduced earlier and in more detail. Writing and Understanding Proofs • Proof methods and proof strategies are now treated in separate sections of Chapter 1. • An appendix listing basic axioms for real numbers and for the integers, and how these axioms are used to prove new results, has been added. The use of these axioms and basic results that follow from them has been made explicit in many proof s in the text. • The process of making conjectures, and then using different proof methods and strategies to attack these Algorithms and Applications • More coverage is devoted to the use of strong induc­ tion to prove that recursive algorithms are correct. • How Bayes' Theorem can be used to construct spam filters is now described. Preface ix • More care is devoted to introducing predicates and quantifiers, as well as to explaining how to use and work with them. • The application of logic to system specifications-a topic of interest to system, hardware, and software engineers-has been expanded. • Material on valid arguments and rules of inference is now presented in a separate section. conjectures, is illustrated using the easily accessible topic of tilings of checkerboards. • Separate and expanded sections on mathematical in­ duction and on strong induction begin the new Chapter 4. These sections include more motivation and a rich collection of examples, providing many examples dif­ ferent than those usually seen. • More proofs are displayed in a way that makes it pos­ sible to explicitly list the reason for each step in the proof. • Examples and exercises from computational geometry have been added, including triangulations of polygons. • The application of bipartite graphs to matching prob­ lems has been introduced. Number Theory, Combinatorics, and Probability Theory • Coverage of number theory is now more flexible, with four sections covering different aspects of the subject and with coverage of the last three of these sections optional. • The introduction of basic counting techniques, and permutations and combinations, has been enhanced. Graphs and Theory of Computation • The introduction to graph theory has been streamlined and improved. • A quicker introduction to terminology and applica­ tions is provided, with the stress on making the correct decisions when building a graph model rather than on terminology. • Material on bipartite graphs and their applications has been expanded. • Coverage of counting techniques has been expanded; counting the ways in which objects can be distributed in boxes is now covered. • Coverage of probability theory has been expanded with the introduction of a new section on Bayes' Theorem. • Examples illustrating the construction of finite-state automata that recognize specified sets have been added. • Minimization of finite-state machines is now men­ tioned and developed in a series of exercises. • Coverage of Turing machines has been expanded with a brief introduction to how Turing machines arise in the study of computational complexity, decidabilit y, and computability. x Preface Exercises and Examples • Many new routine exercises and examples have been added throughout, especially at spots where key con­ cepts are introduced. • Extra effort has been made to ensure that both odd­ numbered and even-numbered exercises are provided for basic concepts and skills. • A better correspondence has been made between examples introducing key concepts and routine exercises. • Many new challenging exercises have been added. • Over 400 new exercises have been added, with more on key concepts, as well as more introducing new topics. Additional Biographies, Historical Notes, and New Discoveries • Biographies have been added for Archimedes, Hop- • The historical notes included in the main body ofthe per, Stirling, and Bayes. book and in the footnotes have been enhanced. • Many biographies found in the previous edition have been enhanced, including the biography of Augusta Ada. • New discoveries made since the publication ofthe pre­ vious edition have been noted. The MathZone Companion Website (www.mhhe.comlrose n) • MathZone course management and online tutorial sys­ tem now provides homework and testing questions tied directly to the text. • Expanded annotated links to hundreds of Internet resources have been added to the Web Resources Guide. • Additional Extra Examples are now hosted online, covering all chapters of the book. These Extra Examples have benefited from user review and feedback. Special Features • Additional Self Assessments of key topics have been added, with 14 core topics now addressed. • Existing Interactive Demonstration Applets support­ ing key algorithms are improved. Additional applets have also been developed and additional explanations are given for integrating them with the text and in the classroom. • An updated and expanded Exploring Discrete Mathe­ matics with Maple companion workbook is also hosted online. ACCESSIBILITY This text has proved to be easily read and understood by beginning students. There are no mathematical prerequisites beyond college algebra for almost all of this text. Students needing extra help will find tools on the MathZone companion website for bringing their mathematical maturity up to the level of the text. The few places in the book where calculus is referred to are explicitly noted. Most students should easily understand the pseudocode used in the text to express algorithms, regardless of whether they have formally studied programming languages. There is no formal computer science prerequisite. Each chapter begins at an easily understood and accessible level. Once basic mathematical concepts have been carefully developed, more difficult material and applications to other areas of study are presented. FLEXIBILITY This text has been carefully designed for flexible use. The dependence of chapters on previous material has been minimized. Each chapter is divided into sections of approximately the same length, and each section is divided into subsections that form natural blocks of material for teaching. Instructors can easily pace their lectures using these blocks. Preface xi WRITING STYLE The writing style in this book is direct and pragmatic. Precise mathe­ mati cal language is used without excessive formalism and abstraction. Care has been taken to balance the mix of notation and words in mathematical statements. MATHEMATICAL RIGOR AND PRECISION All definitions and theorems in this text are stated extremely carefully so that students will appreciate the precision of language and rigor needed in mathemati cs. Proofs are motivated and developed slowly; their steps are all carefully justified. The axioms used in proofs and the basic properties that follow from them are explicitly described in an appendix, giving students a clear idea of what they can assume in a proof. Recursive definitions are explained and used extensively. WORKED EXAMPLES Over 750 examples are used to illustrate concepts, relate different topics, and introduce applications. In most examples, a question is first posed, then its solution is presented with the appropriate amount of detail. APPLICATIONS The applications included in this text demonstrate the utility of discrete mathematics in the solution of real-world problems. This text includes applications to a wide va­ riety of areas, including computer science, data networking, psychology, chemi stry, engineering, linguistics, biology, business, and the Internet. ALGORITHMS Results in discrete mathematics are often expressed in terms of algo­ rithms; hence, key algorithms are introduced in each chapter of the book. These algorithms are expressed in words and in an easily understood form of structured pseudocode, which is described and specified in Appendix A.3. The computational complexity of the algorithms in the text is also analyzed at an elementary level. HISTORICAL INFORMATION The background of many topics is succinctly described in the text. Brief biographies of more than 65 mathematicians and computer scientists, accom­ panied by photos or images, are included as footnotes. These biographies include information about the lives, careers, and accomplishments of these important contributors to discrete mathe­ matics and images of these contributors are displayed. In addition, numerous historical footnotes are included that supplement the historical information in the main body of the text. Efforts have been made to keep the book up-to-date by reflecting the latest discoveries. KEY TERMS AND RESULTS A list of key terms and results follows each chapter. The key terms include only the most important that students should learn, not every term defined in the chapter. EXERCISES There are over 3800 exercises in the text, with many different types of ques­ tions posed. There is an ample supply of straightforward exercises that develop basic skills, a large number of intermediate exercises, and many challenging exercises. Exercises are stated clearly and unambiguous ly, and all are carefully graded for level of difficulty. Exercise sets contain special discussions that develop new concepts not covered in the text, enabling students to discover new ideas through their own work. Exercises that are somewhat more difficult than average are marked with a single star *; those that are much more challenging are marked with two stars **. Exercises whose solutions require calculus are explicitly noted. Exercises that develop results used in the text are clearly identified with the symbol G>. Answers or outlined solutions to all odd-numbered exercises are provided at the back of the text. The solutions include proofs in which most of the steps are clearly spelled out. REVIEW QUESTIONS A set of review questions is provided at the end of each chapter. These questions are designed to help students focus their study on the most important concepts Iii Preface and techniques of that chapter. To answer these questions students need to write long answers, rather than just perform calculations or give short replies. SUPPLEMENTARY EXERCISE SETS Each chapter is followed by a rich and varied set of supplemen tary exercises. These exercises are generally more difficult than those in the exercise sets following the sections. The supplemen tary exercises reinforce the concepts of the chapter and integrate different to pics more effectively. COMPUTER PROJECTS Each chapter is followed by a set of computer projects. The approximately 150 computer projects tie together what students may have learned in computing and in discrete mathemati cs. Computer projects that are more difficult than average, from both a mathematical and a programming point of view, are marked with a star, and those that are extremely challenging are marked with two stars. COMPUT ATIONS AND EXPLORATIONS A set of computations and explorations is included at the conclusion of each chapter. These exercises (approximately 100 in total) are designed to be completed using existing software tools, such as programs that students or instructors have written or mathematical computation packages such as Maple or Mathematica. Many of these exercises give students the opportunity to uncover new facts and ideas through computation. (Some of these exercises are discussed in the Exploring Discrete Mathematics with Maple companion workbook available online.) WRITING PROJECTS Each chapter is followed by a set of writing projects. To do these projects students need to consult the mathematical literature. Some of these projects are historical in nature and may involve looking up original sources. Others are designed to serve as gateways to new topics and ideas. All are designed to expose students to ideas not covered in depth in the text. These projects tie mathematical concepts together with the writing process and help expose students to possible areas for future study. (Suggested references for these projects can be found online or in the printed Student's Solutions Guide.) APPENDIXES There are three appendixes to the text. The first introduces axioms for real numbers and the integers, and illustrates how facts are proved directly from these axioms. The second covers exponential and logarithmic functions, reviewing some basic material used heavily in the course. The third specifies the pseudocode used to describe algorithms in this text. SUGGESTED READINGS A list of suggested readings for each chapter is provided in a section at the end of the text. These suggested readings include books at or below the level of this text, more difficult books, expository articles, and articles in which discoveries in discrete mathematics were originally published. Some of these publications are classics, published many years ago, while others have been published within the last few years. How to Use This Book This text has been carefully written and constructed to support discrete mathematics courses at several levels and with differing foci. The following table identifies the core and optional sections. An introductory one-term course in discrete mathematics at the sophomore level can be based on the core sections of the text, with other sections covered at the discretion of the instructor. A two-term introductory course can include all the optional mathematics sections in addition to the core sections. A course with a strong computer science emphasis can be taught by covering some or all of the optional computer science sections. Ancillaries Chapter 1 2 3 4 5 6 7 8 9 10 11 12 Core Sections 1.1-1.7 (as needed) 2.1-2.4 (as needed) 3.1-3.5,3.8 (as needed) 4.1--4.3 5.1-5.3 6.1 7.1,7.5 8.1, 8.3, 8.5 9.1-9.5 10.1 Optional Computer Science Sections 3.6 4.4,4.5 5.6 6.4 7.3 8.2 10.2, 10.3 11.1-11.4 12.1-12.5 Preface xiii Optional Mathematics Sections 3.7 5.4,5.5 6.2,6.3 7.2, 7.4, 7.6 8.4,8.6 9.6-9.8 10.4, 10.5 Instructors using this book can adjust the level of difficulty of their course by choosing either to cover or to omit the more challenging examples at the end of sections, as well as the more challenging exercises. The dependence of chapters on earlier chapters is shown in the following chart. Chapter I I Chapter 2 I Chapter 3 I Chapter 4 I Chapter 5 ---�I� Chapler 6 Chapler 7 Chapler 8 Chapter 9 Chapter 11 Chapter 12 I Chapter 10 STUDENT'S SOLUTIONS GUIDE This student manual, available separately, contains full solutions to all odd-numbered problems in the exercise sets. These solutions explain why a particular method is used and why it works. For some exercises, one or two other possible approaches are described to show that a problem can be solved in several different ways. Suggested references for the writing projects found at the end of each chapter are also included in this volume. Also included are a guide to writing proofs and an extensive description of common mistakes students make in discrete mathematics, plus sample tests and a sample crib sheet for each chapter designed to help students prepare for exams. (ISBN-lO: 0-07-310779-4) (ISBN-J3: 978-0-07-310779-0) INSTRUCTOR'S RESOURCE GUIDE This manual, available by request for instructors, contains full solutions to even-numbered exercises in the text. Suggestions on how to teach the material in each chapter of the book are provided, including the points to stress in each section and how to put the material into perspective. It also offers sample tests for each chapter and a test xiv Preface bank containing over 1300 exam questions to choose from. Answers to all sample tests and test bank questions are included. Finally, several sample syllabi are presented for courses with differ­ ing emphasis and student ability levels, and a complete section and exercise migration guide is included to help users of the fifth edition update their course materials to match the sixth edition. (ISBN-10: 0-07-310781-6) (ISBN-13: 978-0-07-310781-3) INSTRUCTOR'S TESTING AND RESOURCE CD An extensive test bank of more than 1300 questions using Brownstone Diploma testing software is available by request for use on Windows or Macintosh systems. Instructors can use this software to create their own tests by selecting questions of their choice or by random selection. They can also sort questions by section, difficulty level, and type; edit existing questions or add their own; add their own headings and instructions; print scrambled versions of the same test; export tests to word processors or the Web; and create and manage course grade books. A printed version of this test bank, including the questions and their answers, is included in the Instructor s Resource Guide. (ISBN-1O: 0-07-310782-4) (ISBN-13: 978-0-07-310782-0) Acknowled gments I would like to thank the many instructors and students at a variety of schools who have used this book and provided me with their valuable feedback and helpful suggestions. Their input has made this a much better book than it would have been otherwise. I especially want to thank Jerrold Grossman, John Michaels, and George Bergman for their technical reviews of the sixth edition and their "eagle eyes," which have helped ensure the accuracy of this book. I also appreciate the help provided by all those who have submitted comments via the website. I thank the reviewers of this sixth and the five previous editions. These reviewers have provided much helpful criticism and encouragement to me. I hope this edition lives up to their high expectations. Reviewers for the Sixth Edition Charles Ashbacher, Mount Mercy College Ian Barlami, Rice University George Bergman, University o/California, Berkeley David Berman, University 0/ North Carolina, Wilmington Miklos Bona, University 0/ Florida Prosenjit Bose, Carleton University Kirby Brown, Polytechnic University Michael Button, The Master s College Greg Cameron, Brigham Young University John Carter, University o/Toronto Greg Chapman, Cosumnes River College Chao-Kun Cheng, Virginia Commonwealth University Thomas Cooper, Georgia Perimeter College, Lawrenceville Barbara Cortzen, DePaul University Daniel Cunningham, Buffalo State College George Davis, University 0/ Georgia Beverly Diamond, College o/Charleston Thomas Dunion, Atlantic Union College Bruce Elenbogen, University 0/ Michigan, Dearborn Herbert Enderton, University o/California, Los Angeles Anthony Evans, Wright State University Kim Factor, Marquette University William Farmer, McMaster University Li Feng, Albany State University Stephanie Fitchett, Florida Atlantic University Jerry Fluharty, Eastern Shore Community College Joel Fowler, Southern Polytechnic State University William Gasarch, University of Maryland Sudpito Ghosh, Colorado State University Jerrold Grossman, Oakland University Ruth Haas, Smith College Patricia Hammer, Hollins University Keith Harrow, Brooklyn College Bert Hartnell, St. Mary s University Julia Hassett, Oakton College Kavita Hatwal, Portland Community College John Helm, Radford University Arthur Hobbs, Texas A&M University Fannie Howell, Roanoke High School WeiHu, Houghton College Nan Jiang, University of South Dakota Margaret Johnson, Stariford University Martin Jones, College of Charleston Tim Kearns, California Polytechnic State University, San Luis Obispo Reviewers of Previous Editions Eric Allender, Rutgers University Stephen Andrilli, La Salle University Kendall Atkinson, University of Iowa, Iowa City Jonathan Knappenberger, LaSalle University Ed Korntved, Northwest Nazarene University Przemo Kranz, University of Mississippi Loredana Lanzani, University of Arkansas Yoonjin Lee, Smith College Miguel Lerma, Northwestern University Jason Levy, University of Hawaii Lauren Lilly, Tabor College Ekaterina Lioutikova, Saint Joseph College Vladimir Logvinenko, De Anza College Joan Lukas, University of Massachusetts, Boston Lester McCann, University of Arizona Jennifer McNulty, University of Montana John G. Michaels, SUNY Brockport Michael Oppedisano, Morrisville State College Michael O'Sullivan, San Diego State University Charles Parry, Virginia Polytechnic Institute Linda Powers, Virginia Polytechnic Institute Dan Pritikin, Miami University Anthony Quas, University of Memphis Eric Rawdon, Duquesne University Zhaojun Bai, University of California, Davis Jack R. Barone, Baruch College Klaus Bichteler, University of Texas, Austin Henry Ricardo, Medgar Evers College Oskars Rieksts, Kutztown University Stefan Robila, Preface xv Montclair State University Chris Rodger, Auburn University Robert Rodman, North Carolina State University Shai Simonson, Stonehill College Barbara Smith, Cochise College Wasin So, San Jose State University Diana Staats, Dutchess Community College Lorna Stewart, University of Alberta Bogdan Suceava, California State University, Fullerton Kathleen Sullivan, Seattle University Laszlo Szekely, University of South Carolina Daniel Tauritz, University of Missouri, Rolla Don VanderJagt, Grand Valley State University Fran Vasko, Kutztown University Susan Wallace, University of North Florida Zhenyuan Wang, University of Nebraska, Omaha Tom Wineinger, University of Wisconsin, Eau Claire Charlotte Young, South Plains College Alfred E. Borm, Southwest Texas State University Ken W Bosworth, University of Maryland Lois Brady, California Polytechnic State University, San Luis Obispo xvi Preface Scott Buffett, University of New Brunswick Russell Campbell, University of Northern Iowa E. Rodney Canfield, University of Georgia Kevin Carolan, Marist College Tim Carroll, Bloomsburg University Kit C. Chan, Bowling Green State University Allan C. Cochran, University of Arkansas Peter Collinge, Monroe Community College Ron Davis, Millersville University Nachum Dershowitz, University of Illinois, Urbana-Cham paign Thomas Dowling, The Ohio State University Patrick C. Fischer, Vanderbilt University Jane Fritz, University of New Brunswick Ladnor Geissinger, University of North Carolina Jonathan Goldstine, Pennsylvania State University Paul Gormley, Villanova University Brian Gray, Howard Community College Jonathan Gross, Columbia University Laxmi N. Gupta, Rochester Institute of Technology Daniel Gusfield, University of California, Davis David F. Hayes, San Jose State University XinHe, SUNY at Buffalo Donald Hutchison, Clackamas Community College Kenneth Johnson, North Dakota State University David Jonah, Wayne State University Akihiro Kanamori, Boston University W Thomas Kiley, George Mason University Takashi Kimura, Boston University Nancy Kinnersley , University of Kansas Gary Klatt, University of Wisconsin Nicholas Krier, Colorado State University Lawrence S. Kroll, San Francisco State University Shui F. Lam, California State University, Long Beach Robert Lavelle, lona College Harbir Lamba, George Mason University Sheau Dong Lang, University of Central Florida Cary Lee, Grossmont Community College Yi-Hsin Liu, University of Nebraska, Omaha Stephen C. Locke, Florida Atlantic University George Luger, University of New Mexico David S. McAllister, North Carolina State University Robert McGuigan, Wesifield State College Michael Maller, Queens College Ernie Manes, University of Massachusetts Francis Masat, Glassboro State College 1. M. Metzger, University of North Dakota Thomas D. Morley, Georgia Institute of Technology D. R. Morrison, University of New Mexico Ho Kuen Ng, San Jose State University Timothy S. Norfolk, Sr. University of Akron Truc T. Nguyen, Bowling Green State University George Novacky , University of Pittsburgh Jeffrey Nunemacher, Ohio Wesleyan University Jaroslav Opatmy, Concordia University Jonathan Pakianathan, University of Rochester Thomas W Parsons, Hofstra University Mary K. Prisco, University of Wisconsin, Green Bay Halina Przymusinska, California Polytechnic State University, Pomona Don Reichman, Mercer County Community College Harold Reiter, University of North Carolina Adrian Riskin, Northern Arizona State University Amy L. Rocha, San Jose State University Janet Roll, University of Findlay Matthew 1. Saltzman, Clemson University Alyssa Sankey, Slippery Rock University Dinesh Sarvate, College of Charleston Michael 1. Schlosser, The Ohio State University Steven R. Seidel, Michigan Technological University Douglas Shier, Clemson University Alistair Sinclair, University of California, Berkeley Carl H. Smith, University of Maryland Hunter S. Snevily, University of Idaho Preface xvii Daniel Somerville, Philip D. Tiu, Thomas Upson, University of Massachusetts, Boston Oglethorpe University Rochester Institute of Technology Patrick Tantalo, Roman Voronka, University of California, Santa Cruz Lisa Townsley-Kulich, Illinois Benedictine College New Jersey Institute of Technology Bharti Temkin, George Trapp, James Walker, Texas Tech University West Virginia University University of South Carolina Wallace Terwilligen, David S. Tucker, Anthony S. Wojcik, Bowling Green State University Midwestern State University Michigan State University I would like to thank the staff of McGraw-Hill for their strong support of this project. In particular, thanks go to Liz Haefele, Publisher, for her backing; to Liz Covello, Senior Sponsoring Editor, for her advocacy and enthusiasm; and to Dan Seibert, Developmental Editor, for his dedication and attention. I would also like to thank the original editor, Wayne Yuhasz, whose insights and skills helped ensure the book's success, as well as all the other previous editors of this book. To the staff involved in the production of this sixth edition, I offer my thanks to Peggy Selle, Lead Project Manager; Michelle Whitaker, Senior Designer; JeffHuettman, Lead Media Producer; Sandy Schnee, Senior Media Project Manager; Melissa Leick, Supplements Coordi­ nator; and Kelly Brown, Executive Marketing Manager. I am also grateful to Jerry Grossman, who checked the entire manuscript for accuracy; Rose Kramer and Gayle Casel, the technical proofreaders; Pat Steele, the manuscript copyeditor; and Georgia Mederer, who checked the accuracy ofthe solutions in the Student's Solutions Guide and Instructor's Resource Guide. Kenneth H. Rosen xviii The MathZone Companion Website MathZon� '", T he extensive companion website accompanying this text has been substantially enhanced lL for the sixth edition and is now integrated with MathZone, McGr aw-Hill's customizabl e Web-based course management system. Instructors teaching from the text can now use Math­ Zone to administer online homework, quizzing, testing, and student assessment through sets of questions tied to the text which are automatically graded and accessible in an integrated, exporta ble grade book. MathZone also provides a wide variety of interactive student tutorials, including practice problems with step-by-step solutions; Interactive Demonstration applets; and NetTutor, a live, personalized tutoring service. This website is accessible at www.mathzon e.com by choosing this text off the pull-down menu, or by navigating to www.mhhe.com/rosen. The homepage shows the Information Center, and contains login links for the site's Student Edition and Instructor Edition. Key features of each area are described below: THE INFORMATION CENTER The Information Center contains basic information about the book including the Table of Contents, Preface, descriptions of the Ancillaries, and a sample chapter. Instructors can also learn more about MathZ one's key features, and learn about custom publishing options for the book. MATHZONE STUDENT EDITION The Student Edition contains a wealth of resources available for student use, including the following, tied into the text wherever the special icons below are shown in the text: a Extra Examples You can find a large number of additional examples on the site, covering all chapters of the book. These examples are concentrated in areas where students often ask for additional material. Although most of these examples amplify the basic concepts, more-challenging examples can also be found here. a Interactive Demon stration Applets These applets enable you to interactively explore how important algorithms work, and are tied directly to material in the text such as examples and exercises. Additional resources are provided on how to use and apply these applets. a Self Assessment s These interactive guides help you assess your understanding of 14 key concepts, providing a question bank where each question includes a brief tutorial followed by a multiple-choice question. If you select an incorrect answer, advice is provided to help you understand your error. Using these Self Assessments, you should be able to diagnose your problems and focus on available remedies. a Web Resources Guide This guide provides annotated links to hundreds of external websites containing relevant material such as historical and biograph ical information, puzzles and problems, discussions, applets, programs, and more. These links are keyed to the text by page number. Additional resources in the MathZone Student Edition include: a Exploring Discrete Mathematics with Maple This ancillary provides help for using the Maple computer algebra system to do a wide range of computations in discrete mathemati cs. Each chapter provides a description of relevant Maple functions and how they are used, Maple programs to carry out computations in discrete mathemati cs, examples, and exercises that can be worked using Maple. The MathZone Companion Website xix • Applications of Discrete Mathematics This ancillary contains 24 chapters--each with its own set of exercises-presenting a wide variety of interesting and important applications covering three general areas in discrete mathematics: discrete structures, combinatorics, and graph theory. These applications are ideal for supplementing the text or for independent study. • A Guide to Proof-Writing This guide provides additional help for writing proofs, a skill that many students find difficult to master. By reading this guide at the beginning of the course and periodically thereafter when proof writing is required, you will be rewarded as your proof-writing ability grows. (Also available in the Student's Solutions Guide.) • Common Mistakes in Discrete Mathematics This guide includes a detailed list of com­ mon misconceptions that students of discrete mathematics often have and the kinds of errors they tend to make. You are encouraged to review this list from time to time to help avoid these common traps. (Also available in the Student's Solutions Guide.) • Advice on Writing Projects This guide offers helpful hints and suggestions for the Writ­ ing Projects in the text, including an extensive bibliography of helpful books and articles for research; discussion of various resources available in print and online; tips on doing library re­ search; and suggestions on how to write well. (Also available in the Student's Solutions Guide.) • NetTutor™ NetTutor is a live, Web-based service that provides online tutorial help. Students can ask questions relating to this text and receive answers in real-time during regular, scheduled hours or ask questions and later receive answers. MATHZONE INSTRUCTOR EDITION This part of the website provides access to all of the resources in the MathZone Student Edition, as well as the following resources for instructors: • Course Management Using MathZone's course management capabilities instructors can create and share courses and assignments with colleagues; automatically assign and grade homework, quiz, and test questions from a bank of questions tied directly to the text; create and edit their own questions or import their own course content; manage course announcements and due dates; and track student progress. • Instructor's Resource Library Collected here are a wide variety of additional instructor teaching resources, created by our wide community of users. These resources include lecture notes, classroom presentation materials such as transparency masters and PowerPoint slides, and supplementary reading and assignments. We will periodically update and add to this library, and encourage users to contact us with their materials and ideas for possible future additions. • Suggested Syllabi Several detailed course outlines are shown, offering suggestions for courses with different emphases and different student backgrounds and ability levels. • Migration Guide This guide provides instructors with a complete correlation of all sections and exercises from the fifth edition to the sixth edition, and is very useful for instructors migrating their syllabi to the sixth edition. • Teaching Suggestions This guide contains detailed teaching suggestions for instructors, including chapter overviews for the entire text, detailed remarks on each section, and com­ ments on the exercise sets. • Printable Tests Printable tests are offered in Word and PDF format for every chapter, and are ideal for in-class exams. These tests were created from the Brownstone Diploma electronic test bank available separately by request on the Instructor's Testing and Resource CD. • Image Bank PowerPoints Downloadable PowerPoints files are offered containing images of all Figures and Tables from the text. These image banks are ideal for preparing instructor class materials customized to the text. xx To the Student W hat is discrete mathematics? Discrete mathematics is the part of mathematics devoted to the study of discrete objects. (Here discrete means consisting of distinct or unconnected elements.) The kinds of problems solved using discrete mathematics include: a How many ways are there to choose a valid password on a computer system? a What is the probability of winning a lottery? a Is there a link between two computers in a network? a How can I identify spam e-mail messages? a How can I encrypt a message so that no unintended recipient can read it? a What is the shortest path between two cities using a transportation system? a How can a list of integers be sorted so that the integers are in increasing order? a How many steps are required to do such a sorting? a How can it be proved that a sorting algorithm correctly sorts a list? a How can a circuit that adds two integers be designed? a How many valid Internet addresses are there? You wi11learn the discrete structures and techniques needed to solve problems such as these. More generally, discrete mathematics is used whenever objects are counted, when relation­ ships between finite (or countable) sets are studied, and when processes involving a finite number of steps are analyzed. A key reason for the growth in the importance of discrete mathematics is that information is stored and manipulated by computing machines in a discrete fashion. WHY STUDY DISCRETE MATHEMATICS? There are several important reasons for studying discrete mathematics. First, through this course you can develop your mathematical maturity: that is, your ability to understand and create mathematical arguments. You will not get very far in your studies in the mathematical sciences without these skills. Second, discrete mathematics is the gateway to more advanced courses in all parts of the mathematical sciences. Discrete mathematics provides the mathematical foundations for many computer science courses including data structures, algorithms, database theory, automata theory, formal languages, compiler theory, computer security, and operating systems. Students find these courses much more difficult when they have not had the appropriate mathematical foundations from discrete math. One student has sent me an e-mail message saying that she used the contents of this book in every computer science course she took! Math courses based on the material studied in discrete mathematics include logic, set theory, number theory, linear algebra, abstract algebra, combinatorics, graph theory, and probability theory (the discrete part of the subject). Also, discrete mathematics contains the necessary mathematical background for solving problems in operations research (including many discrete optimization techniques), chemistry, engineering, biology, and so on. In the text, we will study applications to some of these areas. Many students find their introductory discrete mathematics course to be significantly more challenging than courses they have previously taken. One reason for this is that one of the primary goals of this course is to teach mathematical reasoning and problem solving, rather than a discrete set of skills. The exercises in this book are designed to reflect this goal. Although there are plenty of exercises in this text similar to those addressed in the examples, a large To the Student xxi percentage of the exercises require original thought. This is intentional. The material discussed in the text provides the tools needed to solve these exercises, but your job is to successfully apply these tools using your own creativity. One of the primary goals of this course is to learn how to attack problems that may be somewhat different from any you may have previously seen. Unfortunately, learning how to solve only particular types of exercises is not sufficient for success in developing the problem-solving skills needed in subsequent courses and professional work. This text addresses many different topics, but discrete mathematics is an extremely diverse and large area of study. One of my goals as an author is to help you develop the skills needed to master the additional material you will need in your own future pursuits. THE EXERCISES I would like to offer some advice about how you can best learn discrete mathematics (and other subjects in the mathematical and computing sciences). You will learn the most by actively working exercises. I suggest that you solve as many as you possibly can. After working the exercises your instructor has assigned, I encourage you to solve additional exercises such as those in the exercise sets following each section of the text and in the supplementary exercises at the end of each chapter. (Note the key explaining the markings preceding exercises .) Key to the Exercises No marking * ** (Requires calculus) A routine exercise A difficult exercise An extremely challenging exercise An exercise containing a result used in the book (The Table below shows where each of these exercises are used.) An exercise whose solution requires the use of limits or concepts from differential or integral calculus Hand-Icon Exercises and Where They Are Used Section Exercise Section Where Used Page Where Used 1.2 42 11.2 758 1.6 81 1.6 81 2.3 75a 2.4 158 3.1 43 3.1 174 3.2 62 10.2 700 3.7 30 6.2 412 4.1 79 4.1 270 4.2 28 4.2 287 4.3 56 4.3 296 4.3 57 4.3 297 5.4 17 6.2 414 5.4 21 6.4 428 6.2 15 6.2 414 8.1 24 8.4 545 9.4 49 10.1 685 10.1 15 10.1 688 10.1 30 10.1 693 10.1 48 10.2 700 ILl 12 11.3 763 A.2 4 7.3 477 xxii To the Student The best approach is to try exercises yourself before you consult the answer section at the end of this book. Note that the odd-numbered exercise answers provided in the text are answers only and not full solutions; in particular, the reasoning required to obtain answers is omitted in these answers. The Student 50 Solutions Guide, available separately, provides complete, worked solutions to all odd-numbered exercises in this text. When you hit an impasse trying to solve an odd-numbered exercise, I suggest you consult the Student 50 Solutions Guide and look for some guidance as to how to solve the problem. The more work you do yourself rather than passively reading or copying solutions, the more you will learn. The answers and solutions to the even­ numbered exercises are intentionally not available from the publisher; ask your instructor if you have trouble with these. WEB RESOURCES You are strongly encouraged to take advantage of additional re­ sources available on the Web, especially those on the MathZone companion website for this book found at www.mhhe .com/rosen. You will find many Extra Examples designed to clar­ ify key concepts; Self Assessments for gauging how well you understand core topics; Interactive Demonstration Applets exploring key algorithms and other concepts; a Web Resources Guide containing an extensive selection of links to external sites relevant to the world of discrete mathematics; extra explanations and practice to help you master core concepts; added instruc­ tion on writing proofs and on avoiding common mistakes in discrete mathematics; in-depth discussions of important applications; and guidance on utilizing Maple software to explore the computational aspects of discrete mathematics. Places in the text where these additional online resources are available are identified in the margins by special icons. You will also find NetTutor, an online tutorial service that you can use to receive help from tutors either via real-time chat or via messages. For more details on these online resources, see the description of the MathZone companion website immediately preceding this "To the Student" message. THE VALUE OF THIS BOOK My intention is to make your investment in this text an excellent value. The book, the associated ancillaries, and MathZone companion website have taken many years of effort to develop and refine. I am confident that most of you will find that the text and associated materials will help you master discrete mathematics. Even though it is likely that you will not cover some chapters in your current course, you should find it helpful­ as many other students have-to read the relevant sections of the book as you take additional courses. Most of you will return to this book as a useful tool throughout your future studies, especially for those of you who continue in computer science, mathematics, and engineering. I have designed this book to be a gateway for future studies and explorations, and I wish you luck as you begin your journey. Kenneth H. Rosen CHAPTER 1.1 Propositional Logic 1.2 Propositional Equivalences 1.3 Predicates and Quantifiers 1.4 Nested Quantifiers 1.5 Rules of Inference 1.6 Introduction to Proofs 1.7 Proof Methods and Strategy The Foundations: Logic and Proofs T he rules of logic specify the meaning of mathematical statemen ts. For instance, these rules 11. help us understand and reason with statements such as "There exists an integer that is not the sum of two squares" and "For every positive integer n, the sum of the positive integers not exceeding n is n(n + 1)/2." Logic is the basis of all mathematical reasoning, and of all automated reasoning. It has practical applications to the design of computing machines, to the specification of systems, to artificial intelligence, to computer programming, to programming languages, and to other areas of computer science, as well as to many other fields of study. To understand mathema tics, we must understand what makes up a correct mathematical argument, that is, a proof. Once we prove a mathematical statement is true, we call it a theorem. A collection of theorems on a topic organize what we know about th is topic. To learn a mathematical topic, a person needs to actively construct mathematical arguments on this topic, and not just read exposition. Moreover, because knowing the proof of a theorem often makes it possible to modify the result to fit new situations, proofs play an essential role in the development of new ideas. Students of computer science often find it surprising how important proofs are in computer science. In fact, proofs play essential roles when we verify that computer programs produce the correct output for all possible input values, when we show that algorithms always produce the correct result, when we establish the security of a system, and when we create artificial intelligence. Automated reasoning systems have been constructed that allow computers to construct their own proofs. In this chapter, we will explain what makes up a correct mathematical argument and intro­ duce tools to construct these arguments. We will develop an arsenal of different proof methods that will enable us to prove many different types of results. After introducing many different methods of proof, we will introduce some strategy for constructing proofs. We will introduce the notion of a conjecture and exp lain the process of developing mathemati cs by studying conjectures. 1.1 Propositional Logic I-I Introduction The rules of logic give precise meaning to mathematical statemen ts. These rules are used to distinguish between valid and invalid mathematical arguments. Because a major goal ofthis book is to teach the reader how to understand and how to construct correct mathematical arguments, we begin our study of discrete mathematics with an introduction to logic. In addition to its importance in understanding mathematical reasoning, logic has numerous applications in computer science. These rules are used in the design of computer circuits, the construction of computer programs, the verification of the correctness of programs, and in many other ways. Furthermore, software systems have been developed for constructing proofs automatically. We will discuss these applications of logic in the upcoming chapters. Propositions Our discussion begins with an introduction to the basic building blocks of logic-propositions. A proposition is a declarative sentence (that is, a sentence that declares a fact) that is either true or false, but not both. 2 1 / The Foundations: Logic and Proofs 1-2 EXAMPLE l All the following declarative sentences are propositions. Exam=� 1. Washington, D.C., is the capital of the United States of America. 2. Toronto is the capital of Canada. 3. 1 + 1 = 2. 4. 2 + 2 = 3. Propositions 1 and 3 are true, whereas 2 and 4 are false. Some sentences that are not propositions are given in Example 2. EXAMPLE 2 Consider the following sentences. unkS� 1. What time is it? 2. Read this carefully. 3. x + 1 = 2. 4. x + y =Z. Sentences 1 and 2 are not propositions because they are not declarative sentences. Sentences 3 and 4 are not propositions because they are neither true nor false. Note that each of sentences 3 and 4 can be turned into a proposition if we assign values to the variables. We will also discuss other ways to turn sentences such as these into propositions in Section 1.3. � We use letters to denote propositional variables (or statement variables), that is, variables that represent propositions, just as letters are used to denote numerical variables. The conven­ tionalletters used for propositional variables are p, q, r, S, ... . The truth value ofa proposition is true, denoted by T, if it is a true proposition and false, denoted by F, if it is a false proposition. The area oflogic that deals with propositions is called the propositional calculus or propo­ sitional logic. It was first developed systematically by the Greek philosopher Aristotle more than 2300 years ago. ARISTOTLE (384 B.C.E.-322 B.C.E.) Aristotle was born in Stagirus (Stagira) in northern Greece. His father was the personal physician of the King of Macedonia. Because his father died when Aristotle was young, Aristotle could not follow the custom of following his father's profession. Aristotle became an orphan at a young age when his mother also died. His guardian who raised him taught him poetry, rhetoric, and Greek. At the age of 17, his guardian sent him to Athens to further his education. Aristotle joined Plato's Academy where for 20 years he attended Plato's lectures, later presenting his own lectures on rhetoric. When Plato died in 347 B.C. E., Aristotle was not chosen to succeed him because his views differed too much from those of Plato. Instead, Aristotle joined the court of King Hermeas where he remained for three years, and married the niece of the King. When the Persians defeated Hermeas, Aristotle moved to Mytilene and, at the invitation of King Philip of Macedonia, he tutored Alexander, Philip's son, who later became Alexander the Great. Aristotle tutored Alexander for five years and after the death of King Philip, he returned to Athens and set up his own school, called the Lyceum. Aristotle's followers were called the peripateti cs, which means "to walk about;' because Aristotle often walked around as he discussed philosophical questions. Aristotle taught at the Lyceum for 13 years where he lectured to his advanced students in the morning and gave popular lectures to a broad audience in the evening. When Alexander the Great died in 323 B.C.E., a backlash against anything related to Alexander led to trumped-up charges of impiety against Aristotle. Aristotle fled to Chalcis to avoid prosecution. He only lived one year in Chalcis, dying of a stomach ailment in 322 B.C.E. Aristotle wrote three types of works: those written for a popular audience, compilations of scientific facts, and systematic treatises. The systematic treatises included works on logic, philosophy, psycholo gy, physics, and natural history. Aristotle's writings were preserved by a student and were hidden in a vault where a wealthy book collector discovered them about 200 years later. They were taken to Rome, where they were studied by scholars and issued in new editions, preserving them for posterity. 1-3 unkS� 1.1 Propositional Logic 3 We now turn our attention to methods for producing new propositions from those that we already have. These methods were discussed by the English mathematician George Boole in 1854 in his book The Laws of Thought. Many mathematical statements are constructed by combining one or more propositions. New propositions, called compound propositions, are formed from existing propositions using logical operator s. DEFINITION 1 Let p be a proposition. The negation of p, denoted by ..... p (also denoted by p), is the statement "It is not the case that p." The proposition ..... p is read "not p." The truth value of the negation of p, ..... p, is the opposite of the truth value of p. EXAMPLE 3 Find the negation of the proposition "Today is Friday." Exam: � and express this in simple English. Solution: The negation is "It is not the case that today is Friday." This negation can be more simply expressed by "Today is not Friday," or "It is not Friday today." EXAMPLE 4 Find the negation of the proposition "At least 10 inches of rain fell today in Miami." and express this in simple English. Solution: The negation is "It is not the case that at least 10 inches of rain fell today in Miami." This negation can be more simply expressed by "Less than 10 inches of rain fell today in Miami." TABLE 1 The Remark: Strictly speaking, sentences involving variable times such as those in Examples 3 and 4 are not propositions unless a fixed time is assumed. The same holds for variable places unless a fixed place is assumed and for pronouns unless a particular person is assumed. We will always assume fixed times, fixed places, and particular people in such sentences unless otherwise noted. Truth Table for the Negation of a Proposition. p ""p T F F T Table 1 displays the truth table for the negation of a proposition p. This table has a row for each of the two possible truth values of a proposition p. Each row shows the truth value of -'p corresponding to the truth value of p for this row. 4 1 / The Foundations: Logic and Proofs /-4 DEFINITION 2 The negation of a proposition can also be considered the result of the operation of the negation operator on a proposition. The negation operator constructs a new proposition from a single existing proposition. We will now introduce the logical operators that are used to form new propositions from two or more existing propositions. These logical operators are also called connectives. Let p and q be propositions. The conjunction of p and q, denoted by p /\ q, is the proposition "p and q." The conjunction p /\ q is true when both p and q are true and is false otherwise. Table 2 displays the truth table of p /\ q. This table has a row for each of the four possible combinations of truth values of p and q. The four rows correspond to the pairs of truth values TT, TF, FT, and FF, where the first truth value in the pair is the truth value of p and the second truth value is the truth value of q . Note that in logic the word "but" sometimes is used instead of "and" in a conjunction. For example, the statement "The sun is shining, but it is raining" is another way of saying "The sun is shining and it is raining." (In natural language, there is a subtle difference in meaning between "and" and "but"; we will not be concerned with this nuance here.) EXAMPLE 5 Find the conjunction of the propositions p and q where p is the proposition "Today is Friday" and q is the proposition "It is raining today." DEFINITION 3 Solution: The conjunction of these propositions, p /\ q, is the proposition "Today is Friday and it is raining today." This proposition is true on rainy Fridays and is false on any day that is not a Friday and on Fridays when it does not rain. � Let p and q be propositions. The disjunction of p and q, denoted by p V q, is the proposition "p or q." The disjunction p V q is false when both p and q are false and is true otherwise. Table 3 displays the truth table for p V q. The use of the connective or in a disjunction corresponds to one of the two ways the word or is used in English, namely, in an inclusive way. A disjunction is true when at least one of the two propositions is true. For instance, the inclusive or is being used in the statement "Students who have taken calculus or computer science can take this class." TABLE 2 The Truth Table for TABLE 3 The Truth Table for the Conjunction of Two the Disjunction of Two Propositions. Propositions. p q p/\q p q pVq T T T T T T T F F T F T F T F F T T F F F F F F 1-5 EXAMPLE 6 1.1 Propositional Logic 5 Here, we mean that students who have taken both calculus and computer science can take the class, as well as the students who have taken only one of the two subjects. On the other hand, we are using the exclusive or when we say "Students who have taken calculus or computer science, but not both, can enroll in this class." Here, we mean that students who have taken both calculus and a computer science course cannot take the class. Only those who have taken exactly one of the two courses can take the class. Similarly, when a menu at a restaurant states, "Soup or salad comes with an entree," the restaurant almost always means that customers can have either soup or salad, but not both. Hence, this is an exclusive, rather than an inclusive, or. What is the disjunction ofthe propositions p and q where p and q are the same propositions as in Example 5? Solution: The disjunction of p and q, p v q, is the proposition "Today is Friday or it is raining today." This proposition is true on any day that is either a Friday or a rainy day (including rainy Fridays). It is only false on days that are not Fridays when it also does not rain. .... As was previously remarked, the use of the connective or in a disjunction corresponds to one of the two ways the word or is used in English, namely, in an inclusive way. Thus, a disjunction is true when at least one of the two propositions in it is true. Sometimes, we use or in an exclusive sense. When the exclusive or is used to connect the propositions p and q, the proposition "p or q (but not both)" is obtained. This proposition is true when p is true and q is false, and when p is false and q is true. It is false when both p and q are false and when both are true. DEFINITION 4 Let p and q be propositions. The exclusive or of p and q, denoted by p E9 q, is the proposition that is true when exactly one of p and q is true and is false otherwise. unkS� The truth table for the exclusive or of two propositions is displayed in Table 4. GEORGE BOOLE (1815-1864) George Boole, the son of a cobbler, was born in Lincoln, England, in November 1815. Because of his family's difficult financial situation, Boole had to struggle to educate himself while supporting his family. Neverthe less, he became one of the most important mathematicians of the 1800s. Although he considered a career as a clergyman, he decided instead to go into teaching and soon afterward opened a school of his own. In his preparation for teaching mathema tics, Boole-unsatisfied with textbooks of his day-decided to read the works of the great mathema ticians. While reading papers of the great French mathematician Lagrange, Boole made discoveries in the calculus of variations, the branch of analysis dealing with finding curves and surfaces optimizing certain parameters. In 1848 Boole published The Mathematical Analysis a/Logic, the first of his contributions to symbolic logic. In 1849 he was appointed professor of mathematics at Queen's College in Cork, Ireland. In 1854 he published The Laws 0/ Thought, his most famous work. In this book, Boole introduced what is now called Boolean algebra in his honor. Boole wrote textbooks on differential equations and on difference equations that were used in Great Britain until the end of the nineteenth century. Boole married in 1855; his wife was the niece of the professor of Greek at Queen's College. In 1864 Boole died from pneumonia, which he contracted as a result of keeping a lecture engagement even though he was soaking wet from a rainstorm. 6 I / The Founda tions: Logic and Proofs 1-6 DEFINITION 5 Assessment i::J TABLE 4 The Truth Table for TABLE 5 The Truth Table for the Exclusive Or of Two the Conditional Statement Propositions. p--q. p q pffiq p q p-+q T T F T T T T F T T F F F T T F T T F F F F F T Conditional Statements We will discuss several other important ways in which propositions can be combined. Let p and q be propositions. The conditional statement p � q is the proposition "if p, then q." The conditional statement p � q is false when p is true and q is false, and true otherwise. In the conditional statement p � q, p is called the hypothesis (or antecedent or premise) and q is called the conclusion (or consequence ). The statement p � q is called a conditional statement because p � q asserts that q is true on the condition that p holds. A conditional statement is also called an implication. The truth table for the conditional statement p � q is shown in Table 5. Note that the statement p � q is true when both p and q are true and when p is false (no matter what truth value q has). Because conditional statements play such an essential role in mathematical reasoning, a variety of terminology is used to express p � q. You will encounter most if not all of the following ways to express this conditional statement: "if p, then q" "if p, q" "p is sufficient for q" "q if p" "q when p" "a necessary condition for p is q" "q unless -'p" "p implies q" "p only if q" "a sufficient condition for q is p" "q whenever p" "q is necessary for p" "q follows from p" A useful way to understand the truth value of a conditional statement is to think of an obligation or a contract. For example, the pledge many politicians make when running for office is "If I am elected, then I will lower taxes." If the politician is elected, voters would expect this politician to lower taxes. Furthermore, if the politician is not elected, then voters will not have any expectation that this person will lower taxes, although the person may have sufficient influence to cause those in power to lower taxes. It is only when the politician is elected but does not lower taxes that voters can say that the politician has broken the campaign pledge. This last scenario corresponds to the case when p is true but q is false in p � q. Similarly, consider a statement that a professor might make: "If you get 100% on the final, then you will get an A." /-7 EXAMPLE 7 Exlra� ExampleS� 1.1 Propositional Logic 7 If you manage to get a 100% on the final, then you would expect to receive an A. If you do not get 100% you mayor may not receive an A depending on other factors. However, if you do get 100%, but the professor does not give you an A, you will feel cheated. Many people find it confusing that "p only if q" expresses the same thing as "if p then q." To remember this, note that "p only if q" says that p cannot be true when q is not true. That is, the statement is false if p is true, but q is false. When p is false, q may be either true or false, because the statement says nothing about the truth value of q. A common error is for people to think that "q only if p" is a way of expressing p -* q. However, these statements have different truth values when p and q have different truth values. The word "unless" is often used to express conditional statements. Observe that "q unless -.p" means that if -'p is false, then q must be true. That is, the statement "q unless -.p" is false when p is true and q is false, but it is true otherwise. Consequently, "q unless -.p" and p -* q always have the same truth value. We illustrate the translation between conditional statements and English statements in Example 7. Let p be the statement "Maria learns discrete mathematics" and q the statement "Maria will find a good job." Express the statement p -* q as a statement in English. Solution: From the definition of conditional statements, we see that when p is the statement "Maria learns discrete mathematics" and q is the statement "Maria will find a good job," p -* q represents the statement "If Maria learns discrete mathematics, then she will find a good job." There are many other ways to express this conditional statement in English. Among the most natural of these are: "Maria will find a good job when she learns discrete mathematics." "For Maria to get a good job, it is sufficient for her to learn discrete mathematics." and "Maria will find a good job unless she does not learn discrete mathematics." Note that the way we have defined conditional statements is more general than the meaning attached to such statements in the English language. For instance, the conditional statement in Example 7 and the statement "If it is sunny today, then we will go to the beach." are statements used in normal language where there is a relationship between the hypothesis and the conclusion. Further, the first of these statements is true unless Maria learns discrete mathematics, but she does not get a good job, and the second is true unless it is indeed sunny today, but we do not go to the beach. On the other hand, the statement "If today is Friday, then 2 + 3 = 5." is true from the definition of a conditional statement, because its conclusion is true. (The truth value of the hypothesis does not matter then.) The conditional statement "Iftoday is Friday, then 2 + 3 = 6." is true every day except Friday, even though 2 + 3 = 6 is false. We would not use these last two conditional statements in natural language (except perhaps in sarcasm), because there is no relationship between the hypothesis and the conclusion in either 8 I / The Foundations: Logic and Proofs 1-8 statement. In mathematical reasoning, we consider conditional statements of a more general sort than we use in English. The mathematical concept of a conditional statement is independent of a cause-and-effect relationship between hypothesis and conclusion. Our definition of a conditional statement specifies its truth values; it is not based on English usage. Propositional language is an artificial language; we only parallel English usage to make it easy to use and remember. The if-then construction used in many programming languages is different from that used in logic. Most programming languages contain statements such as if p then S, where p is a . proposition and S is a program segment (one or more statements to be executed). When execution of a program encounters such a statement, S is executed if p is true, but S is not executed if p is false, as illustrated in Example 8. EXAMPLE 8 What is the value of the variable x after the statement if2 + 2 = 4 then x := x + 1 if x = 0 before this statement is encountered? (The symbol := stands for assignment. The statement x : = x + 1 means the assignment of the value of x + I to x .) Solution: Because 2 + 2 = 4 is true, the assignment statement x := x + I is executed. Hence, x has the value 0 + I = I after this statement is encountered. .... CONVERSE, CONTRAPOSITIVE, AND INVERSE We can form some new conditional statements starting with a conditional statement p -+ q. In particular, there are three related conditional statements that occur so often that they have special names. The proposition q -+ p is called the converse of p -+ q. The contrapos itive of p -+ q is the proposition -.q -+ -'p. The proposition -'p -+ -.q is called the inverse of p -+ q. We will see that of these three conditional statements formed from p -+ q, only the contrapositive always has the same truth value as p -+ q . .. We first show that the contrapositive, -.q -+ -'p, of a conditional statement p -+ q always has the same truth value as p -+ q. To see this, note that the contrapositive is false only when -'p is false and -.q is true, that is, only when p is true and q is false. We now show that neither the converse, q -+ p, nor the inverse, -'p -+ -'q, has the same truth value as p -+ q for all possible truth values of p and q. Note that when p is true and q is false, the original conditional statement is false, but the converse and the inverse are both true. When two compound propositions always have the same truth value we call them equivalent, so that a conditional statement and its contrapositive are equivalent. The converse and the inverse of a conditional statement are also equivalent, as the reader can verify, but neither is equivalent to the original conditional statement. (We will study equivalent propositions in Section 1.2.) Take note that one of the most common logical errors is to assume that the converse or the inverse of a conditional statement is equivalent to this conditional statement. We illustrate the use of conditional statements in Example 9. EXAMPLE 9 What are the contrapositive, the converse, and the inverse of the conditional statement "The home team wins whenever it is raining."? Solution: Because "q whenever p" is one of the ways to express the conditional statement p -+ q, the original statement can be rewritten as "If it is raining, then the home team wins." Consequently, the contrapositive of this conditional statement is "If the home team does not win, then it is not raining." 1-9 1.1 Propositional Logic 9 The converse is "If the home team wins, then it is raining." The inverse is "If it is not raining, then the home team does not win." Only the contrapositive is equivalent to the original statement. BICONDITION ALS We now introduce another way to combine propositions that expresses that two propositions have the same truth value. DEFINITION 6 Let p and q be propositions. The biconditional statement p *+ q is the proposition "p if and only if q." The biconditional statement p *+ q is true when p and q have the same truth values, and is false otherwise. Biconditional statements are also called bi-implications. The truth table for p *+ q is shown in Table 6. Note that the statement p *+ q is true when both the conditional statements p -+ q and q -+ P are true and is false otherwise. That is why we use the words "if and only if" to express this logical connective and why it is symbolically written by combining the symbols -+ and +-. There are some other common ways to express p *+ q: "p is necessary and sufficient for q" "if p then q, and conversely" "p iff q." The last way of expressing the biconditional statement p *+ q uses the abbreviation "iff" for "if and only if." Note that p *+ q has exactly the same truth value as (p -+ q) 1\ (q -+ p). EXAMPLE 10 Let p be the statement "You can take the flight" and let q be the statement "You buy a ticket." Then p *+ q is the statement "You can take the flight if and only if you buy a ticket." This statement is true if p and q are either both true or both false, that is, if you buy a ticket and can take the flight or if you do not buy a ticket and you cannot take the flight. It is false when p and q have opposite truth values, that is, when you do not buy a ticket, but you can take the flight (such as when you get a free trip) and when you buy a ticket and cannot take the flight (such as when the airline bumps you). � TABLE 6 The Truth Table for the Biconditional p +-+ q. p q p+-+q T T T T F F F T F F F T 10 1/ The Foundations: Logic and Proofs 1-10 Demo� EXAMPLE 11 IMPLICIT USE OF BICONDITIONALS You should be aware that biconditionals are not always explicit in natural language. In particular, the "if and only if" construction used in biconditionals is rarely used in common language. Instead, biconditionals are often expressed using an "if, then" or an "only if" construction. The other part of the "if and only if" is implicit. That is, the converse is implied, but not stated. For example, consider the statement in English "If you finish your meal, then you can have dessert." What is really meant is "You can have dessert if and only if you finish your meal." This last statement is logically equivalent to the two statements "If you finish your meal, then you can have dessert" and "You can have dessert only if you finish your meal." Because of this imprecision in natural language, we need to make an assumption whether a conditional statement in natural language implicitly includes its converse. Because precision is essential in mathematics and in logic, we will always distinguish between the conditional statement p --+ q and the biconditional statement p ++ q. Truth Tables of Compound Propositions We have now introduced four important logical connectives--con junctions, disjunctions, con­ ditional statements, and biconditional statements-as well as negations. We can use these con­ nectives to build up complicated compound propositions involving any number of propositional variables. We can use truth tables to determine the truth values of these compound propositions, as Example 11 illustrates. We use a separate column to find the truth value of each compound expression that occurs in the compound proposition as it is built up. The truth values of the compound proposition for each combination of truth values of the propositional variables in it is found in the final column of the table. Construct the truth table of the compound proposition (p v -.q) --+ (p /\ q). Solution: Because this truth table involves two propositional variables p and q, there are four rows in this truth table, corresponding to the combinations of truth values TT, TF, FT, and FE The first two columns are used for the truth values of p and q, respectively. In the third column we find the truth value of -'q, needed to find the truth value of p v -'q, found in the fourth column. The truth value of p /\ q is found in the fifth column. Finally, the truth value of (p v -.q) --+ (p /\ q) is found in the last column. The resulting truth table is shown in Table 7. � Precedence of Logical Operators We can construct compound propositions using the negation operator and the logical operators defined so far. We will generally use parentheses to specify the order in which logical operators TABLE 7 The Truth Table of (p v .., q) -+ (p /\ q). P q ...,q pV""q p/\q (P V ...,q) -> (p /\ q) T T F T T T T F T T F F F T F F F T F F T T F F 1-11 TABLES Precedence of Logical Operators. Operator Precedence � 1 1\ 2 v 3 ---+ 4 *+ 5 1.1 Propositional Logic 11 in a compound proposition are to be applied. For instance, (p v q) /\ (-,r) is the conjunction of p v q and -'r. However, to reduce the number of parentheses, we specify that the negation operator is applied before all other logical operators. This means that -'p /\ q is the conjunc­ tion of -'p and q, namely, (-,p) /\ q, not the negation of the conjunction of p and q, namely -'(p /\ q). Another general rule of precedence is that the conjunction operator takes precedence over the disjunction operator, so that p /\ q V r means (p /\ q) V r rather than p /\ (q V r). Because this rule may be difficult to remember, we will continue to use parentheses so that the order of the disjunction and conjunction operators is clear. Finally, it is an accepted rule that the conditional and biconditional operators -+ and *+ have lower precedence than the conjunction and disjunction operators, /\ and v. Consequently, p v q -+ r is the same as (p v q) -+ r. We will use parentheses when the order ofthe conditional operator and biconditional operator is at issue, although the conditional operator has precedence over the biconditional operator. Table 8 displays the precedence levels of the logical operators, -', /\, v, -+, and *+. Translatin g English Sentences There are many reasons to translate English sentences into expre ssions involving propositional variables and logical connective s. In particular, English (and every other human language) is often ambiguous. Translating sentences into compound statements (and other types of logical expressions, which we will introduce later in this chapter) removes the ambiguity. Note that this may involve making a set of reasonable assumptions based on the intended meaning of the sentence. Moreover, once we have translated sentences from English into logical expressions we can analyze these logical expressions to determine their truth values, we can manipulate them, and we can use rules of inference (which are discussed in Section 1.5) to reason about them. To illustrate the process of translating an English sentence into a logical expression, consider Examples 12 and 13. EXAMPLE 12 How can this English sentence be translated into a logical expression? "You can access the Internet from campus only if you are a computer science major or you are not a freshman." Solution: There are many ways to translate this sentence into a logical expression. Although it is possible to represent the sentence by a single propositional variable, such as p, this would not be useful when analyzing its meaning or reasoning with it. Instead, we will use propositional variables to represent each sentence part and determine the appropriate logical connectives between them. In particular, we let a, c, and / represent "You can access the Internet from campus," "You are a computer science major," and "You are a freshman," respectively. Noting that "only if" is one way a conditional statement can be expressed, this sentence can be repre­ sented as a -+ (c V -,/). EXAMPLE 13 How can this English sentence be translated into a logical expression? "You cannot ride the roller coaster if you are under 4 feet tall unless you are older than 16 years old." 12 1 / The Foundations: Logic and Proofs 1-12 EXAMPLE 14 Solution: Let q, r, and s represent "You can ride the roller coaster," "You are under 4 feet tall," and "You are older than 16 years old," respectively. Then the sentence can be translated to Of course, there are other ways to represent the original sentence as a logical expression, but the one we have used should meet our needs. <III! System Specifications Translating sentences in natural language (such as English) into logical expressions is an essential part of specifying both hardware and software systems. System and software engineers take requirements in natural language and produce precise and unambiguous specifications that can be used as the basis for system development. Example 14 shows how compound propositions can be used in this process. . Express the specification "The automated reply cannot be sent when the file system is full" using logical connectives. Solution: One way to translate this is to let p denote "The automated reply can be sent" and q denote "The file system is full." Then -'p represents "It is not the case that the automated reply can be sent," which can also be expressed as "The automated reply cannot be sent." Consequently, our specification can be represented by the conditional statement q ---+ -'p. <III! System specifications should be consistent, that is, they should not contain conflicting requirements that could be used to derive a contradiction. When specifications are not consistent, there would be no way to develop a system that satisfies all specifications. EXAMPLE 15 Determine whether these system specifications are consistent: "The diagnostic message is stored in the buffer or it is retransmitted." "The diagnostic message is not stored in the buffer." "If the diagnostic message is stored in the buffer, then it is retransmitted." Solution: To determine whether these specifications are consistent, we first express them using logical expressions. Let p denote "The diagnostic message is stored in the buffer" and let q denote "The diagnostic message is retransmitted." The specifications can then be written as p v q, -'p, and p ---+ q. An assignment of truth values that makes all three specifications true must have p false to make -'p true. Because we want p v q to be true but p must be false, q must be true. Because p ---+ q is true when p is false and q is true, we conclude that these specifications are consistent because they are all true when p is false and q is true. We could come to the same conclusion by use of a truth table to examine the four possible assignments of truth values to p andq. <III! EXAMPLE 16 Do the system specifications in Example 15 remain consistent ifthe specification "The diagnostic message is not retransmitted" is added? Solution: By the reasoning in Example 15, the three specifications from that example are true only in the case when p is false and q is true. However, this new specification is -'q, which is false when q is true. Consequently, these four specifications are inconsistent. <III! J-/3 unkS� EXAMPLE 17 unkS� EXAMPLE 18 Exam=� 1.1 Propositional Logic 13 Boolean Searches Logical connectives are used extensively in searches of large collections of information, such as indexes of Web pages. Because these searches employ techniques from propositional logic, they are called Boolean sear ches. In Boolean searches, the connective AND is used to match records that contain both of two search terms, the connective OR is used to match one or both of two search terms, and the connective NOT (sometimes written as AND NOT) is used to exclude a particular search term. Careful planning of how logical connectives are used is often required when Boolean searches are used to locate information of potential interest. Example 17 illustrates how Boolean searches are carried out. Web Page Searching Most Web search engines support Boolean searching techniques, which usually can help find Web pages about particular subjects. For instance, using Boolean searching to find Web pages about universities in New Mexico, we can look for pages matching NEW AND MEXICO AND UNIVERSITIES. The results of this search will include those pages that contain the three words NEW, MEXICO, and UNIVERSITIE S. This will include all of the pages of interest, together with others such as a page about new universities in Mexico. (Note that in Google, and many other search engi nes, the word "AND" is not needed, although it is understood, because all search terms are included by default.) Next, to find pages that deal with universities in New Mexico or Arizona, we can search for pages matching (NEW AND MEXICO OR ARIZONA) AND UNIVERS ITIES. (Note: Here the AND operator takes precedence over the OR operator. Also, in Google, the terms used for this search would be NEW MEXICO OR ARIZONA .) The results ofthis search will include all pages that contain the word UNIVERS ITIES and either both the words NEW and MEXICO or the word ARIZONA. Again, pages besides those of interest will be listed. Finally, to find Web pages that deal with universities in Mexico (and not New Mexico), we might first look for pages matching MEXICO AND UNIVERSITIES, but because the results of this search will include pages about universities in New Mexico, as well as universities in Mexico, it might be better to search for pages matching (MEXICO AND UNIVERSITIES) NOT NEW. The results of this search include pages that contain both the words MEXICO and UNIVERSITIES but do not contain the word NEW. (In Google, and many other search engines, the word "NOT" is replaced by a minus sign "-". In Google, the terms used for this last search would be MEXICO UNIVERSITIES -NEW.) .... Logic Puzzles Puzzles that can be solved using logical reasoning are known as logic puzzles. Solving logic puzzles is an excellent way to practice working with the rules of logic. Also, computer pro­ grams designed to carry out logical reasoning often use well-known logic puzzles to illustrate their capabilities. Many people enjoy solving logic puzzles, which are published in books and periodicals as a recreational activity. We will discuss two logic puzzles here. We begin with a puzzle that was originally posed by Raymond Smullyan, a master of logic puzzles, who has published more than a dozen books containing challenging puzzles that involve logical reasoning. In [Sm78] Smullyan posed many puzzles about an island that has two kinds of inhabitants, knights, who always tell the truth, and their opposites, knaves, who always lie. You encounter two people A and B. What are A and B if A says "B is a knight" and B says "The two of us are opposite types"? 14 1 / The Foundations: Logic and Proofs 1-14 Solution: Let p and q be the statements that A is a knight and B is a knight, respectively, so that -'p and -.q are the statements that A is a knave and that B is a knave, respectively. We first consider the possibility that A is a knight; this is the statement that p is true. If A is a knight, then he is telling the truth when he says that B is a knight, so that q is true, and A and B are the same type. However, if B is a knight, then B's statement that A and B are of opposite types, the statement (p /\ -.q) V (-'p /\ q), would have to be true, which it is not, because A and B are both knights. Consequently, we can conclude that A is not a knight, that is, that p is false. If A is a knave, then because everything a knave says is false, A's statement that B is a knight, that is, that q is true, is a lie, which means that q is false and B is also a knave. Furthermore, if B is a knave, then B's statement that A and B are opposite types is a lie, which is consistent with both A and B being knaves. We can conclude that both A and B are knaves. <0lIl We pose more of Smullyan's puzzles about knights and knaves in Exercises 55-59 at the end of this section. Next, we pose a puzzle known as the muddy children puzzle for the case of two children. EXAMPLE 19 A father tells his two children, a boy and a girl, to play in their backyard without getting dirty. unkS� However, while playing, both children get mud on their foreheads. When the children stop playing, the father says "At least one of you has a muddy forehead," and then asks the children to answer "Yes" or "No" to the question: "Do you know whether you have a muddy forehead?" The father asks this question twice. What will the children answer each time this question is asked, assuming that a child can see whether his or her sibling has a muddy forehead, but cannot see his or her own forehead? Assume that both children are honest and that the children answer each question simultaneousl y. Solution: Let s be the statement that the son has a muddy forehead and let d be the statement that the daughter has a muddy forehead. When the father says that at least one of the two children has a muddy forehead, he is stating that the disjunction s V d is true. Both children will answer "No" the first time the question is asked because each sees mud on the other child's forehead. That is, the son knows that d is true, but does not know whether s is true, and the daughter knows that s is true, but does not know whether d is true. After the son has answered "No" to the first question, the daughter can determine that d must be true. This follows because when the first question is asked, the son knows that s V d is RAYMOND SMULLYAN (BORN 1919) Raymond Smullyan dropped out of high school. He wanted to study what he was really interested in and not standard high school material. After jumping from one university to the next, he earned an undergraduate degree in mathematics at the University of Chicago in 1955. He paid his college expenses by performing magic tricks at parties and clubs. He obtained a Ph.D. in logic in 1959 at Princeton, studying under Alonzo Church. After graduating from Princeton, he taught mathematics and logic at Dartmouth College, Princeton University, Yeshiva University, and the City University of New York. He joined the philosophy department at Indiana University in 1981 where he is now an emeritus professor. Smullyan has written many books on recreational logic and mathema tics, including Satan, Cantor, and Infinity; What Is the Name of This Book?; The Lady or the Tiger?; Alice in Puzzleland; To Mock a Mockingbird; Forever Undecided; and The Riddle of Scheherazade: Amazing Logic Puzzles, Ancient and Modern. Because his logic puzzles are challenging, entertaining, and thought-prov oking, he is considered to be a modem-day Lewis Carroll. Smullyan has also written several books about the application of deductive logic to chess, three collections of philosophical essays and aphorisms, and several advanced books on mathematical logic and set theory. He is particularly interested in self-reference and has worked on extending some of Godel's results that show that it is impossible to write a computer program that can solve all mathematical problems. He is also particularly interested in explaining ideas from mathematical logic to the public. Smullyan is a talented musician and often plays piano with his wife, who is a concert-level pianist. Making telescopes is one of his hobbies. He is also interested in optics and stereo photography. He states "I've never had a conflict between teaching and research as some people do because when I'm teaching, I'm doing research." 1-15 Truth Value Bit T 1 F 0 unllS� DEFINITION 7 1.1 Propositional Logic 15 TABLE 9 Table for the Bit Operators OR, AND, and XOR. x y xVy x/\y xEBy 0 0 0 0 0 0 1 1 0 1 1 0 1 0 1 1 1 1 1 0 true, but cannot determine whether s is true. Using this information, the daughter can conclude that d must be true, for if d were false, the son could have reasoned that because s V d is true, then s must be true, and he would have answered "Yes" to the first question. The son can reason in a similar way to determine that s must be true. It follows that both children answer "Yes" the second time the question is asked. .... Logic and Bit Operations Computers represent information using bits. A bit is a symbol with two possible values, namely, o (zero) and 1 (one). This meaning of the word bit comes from binary digit, because zeros and ones are the digits used in binary representations of numbers. The well-known statistician John Tukey introduced this terminology in 1946. A bit can be used to represent a truth value, because there are two truth values, namely, true and false. As is customarily done, we will use a 1 bit to represent true and a 0 bit to represent false. That is, 1 represents T (true), 0 represents F (false). A variable is called a Boolean variable if its value is either true or false. Consequently, a Boolean variable can be represented using a bit. Computer bit operations correspond to the logical connectives. By replacing true by a one and false by a zero in the truth tables for the operators /\, v, and EB, the tables shown in Table 9 for the corresponding bit operations are obtained. We will also use the notation OR, AND, and XOR for the operators V, /\, and EB, as is done in various programming languages. Information is often represented using bit strings, which are lists of zeros and ones. When this is done, operations on the bit strings can be used to manipulate this information. A bit string is a sequence of zero or more bits. The length of this string is the number of bits in the string. EXAMPLE 20 101010011 is a bit string oflength nine. We can extend bit operations to bit strings. We define the bitwise OR, bitwise AND, and bitwise XOR of two strings of the same length to be the strings that have as their bits the OR, AND, and XOR of the corresponding bits in the two strings, respectively. We use the symbols V, /\, and EB to represent the bitwise OR, bitwise AND, and bitwiseXOR operations, respectively. We illustrate bitwise operations on bit strings with Example 21. EXAMPLE 21 Find the bitwise OR, bitwise AND, and bitwise XOR of the bit strings 0110110110 and 11 0001 110 1. (Here, and throughout this book, bit strings will be split into blocks of four bits to make them easier to read.) 16 1 / The Foundations: Logic and Proofs 1-16 Solution: The bitwise OR, bitwise AND, and bitwise XOR of these strings are obtained by taking the OR, AND, and XOR of the corresponding bits, respectively. This gives us Exercises 01 10110110 11 0001 1101 11 1011 1111 bitwise OR 01 0001 0100 bitwise AND 10 1010 1011 bitwiseXOR 1. Which ofthese sentences are propositions? What are the truth values of those that are propositions? a) Boston is the capital of Massachusetts. b) Miami is the capital of Florida. c) 2 + 3 = 5. d) 5 + 7 = 10. e) x + 2 = II. 1) Answer this question. 2. Which ofthese are propositions? What are the truth values of those that are propositions? a) Do not pass go. b) What time is it? c) There are no black flies in Maine. d) 4 +x = 5. e) The moon is made of green cheese. 1) 2n::: 100. 3. What is the negation of each of these propositions? a) Today is Thursday. unkS� b) There is no pollution in New Jersey. c) 2 + 1 = 3. d) The summer in Maine is hot and sunny. 4. Let p and q be the propositions p : I bought a lottery ticket this week. q : I won the million dollar jackpot on Friday. Express each ofthese propositions as an English sentence. a) --'p b) p v q c) p --+ q d) p /\ q e) p - q 1) --'p --+ --.q g) --'p /\ --.q b) --'p v (p /\ q) 5. Let p and q be the propositions "Swimming at the New Jersey shore is allowed" and "Sharks have been spotted near the shore," respective ly. Express each of these com­ pound propositions as an English sentence. a) --.q b) p /\ q c) --'p v q d) p --+ --.q e) --.q --+ p 1) --'p --+ --.q g) p _ --.q b) --'p /\ (p V --.q) JOHN WILDER TUKEY (1915-2000) Tukey, born in New Bedford, Massachusetts, was an only child. His parents, both teachers, decided home schooling would best develop his potential. His formal education began at Brown University, where he studied mathematics and chemistry. He received a master's degree in chemistry from Brown and continued his studies at Princeton University, changing his field of study from chemistry to mathema tics. He received his Ph.D. from Princeton in 1939 for work in topology, when he was appointed an instructor in mathematics at Princeton. With the start of World War II, he joined the Fire Control Research Office, where he began working in statistics. Tukey found statistical research to his liking and impressed several leading statisticians with his skills. In 1945, at the conclusion ofthe war, Tukey returned to the mathematics department at Princeton as a professor of statistics, and he also took a position at AT&T Bell Laboratori es. Tukey founded the Statistics Department at Princeton in 1966 and was its first chairman. Tukey made significant contributions to many areas of statistics, including the analysis of variance, the estimation of spectra of time series, inferences about the values of a set of parameters from a single experiment, and the philosophy of statistics. However, he is best known for his invention, with 1. W Cooley, of the fast Fourier transform. In addition to his contributions to statistics, Tukey was noted as a skilled wordsmith; he is credited with coining the terms bit and software. Tukey contributed his insight and expertise by serving on the President's Science Advisory Committee. He chaired several important committees dealing with the environment, education, and chemicals and health. He also served on committees working on nuclear disarmament. Tukey received many awards, including the National Medal of Science. HISTORICAL NOTE There were several other suggested words for a binary digit, including binit and bigit, that never were widely accepted. The adoption of the word bit may be due to its meaning as a common English word. For an account of Tukey's coining of the word bit, see the April 1984 issue of Annals of the History of Computing. 1-17 6. Let p and q be the propositions "The election is de­ cided" and "The votes have been counted," respectively. Express each of these compound propositions as an English sentence. a) ""'p c) ""'p /\ q e) ""'q --+ ""'p g) p++q b) p vq d) q --+ P I) ""'p --+ ""'q b) ....,qv(....,P/\ q) 7. Let p and q be the propositions p : It is below freezing. q : It is snowing. Write these propositions using p and q and logical connective s. a) It is below freezing and snowing. b) It is below freezing but not snowing. c) It is not below freezing and it is not snowing. d) It is either snowing or below freezing (or both). e) If it is below freezing, it is also snowing. I) It is either below freezing or it is snowing, but it is not snowing if it is below freezing. g) That it is below freezing is necessary and sufficient for it to be snowing. 8. Let p, q, and r be the propositions p : You have the flu. q : You miss the final examination. r : You pass the course. Express each ofthese propositions as an English sentence. a) p --+ q b) ""'q ++ r c) q --+ ....,r d) p v q V r e) (p --+ ....,r) v (q --+ ....,r) I) (p/\q)v(""'q /\r) 9. Let p and q be the propositions p : You drive over 65 miles per hour. q : You get a speeding ticket. Write these propositions using p and q and logical connectives. a) You do not drive over 65 miles per hour. b) You drive over 65 miles per hour, but you do not get a speeding ticket. c) You will get a speeding ticket if you drive over 65 miles per hour. d) If you do not drive over 65 miles per hour, then you will not get a speeding ticket. e) Driving over 65 miles per hour is sufficient for getting a speeding ticket. I) You get a speeding ticket, but you do not drive over 65 miles per hour. g) Whenever you get a speeding ticket, you are driving over 65 miles per hour. 10. Let p, q, and r be the propositions p : You get an A on the final exam. q : You do every exercise in this book. r : You get an A in this class. 1.1 Propositional Logic 17 Write these propositions using p, q, and r and logical connectives. a) You get an A in this class, but you do not do every exercise in this book. b) You get an A on the final, you do every exercise in this book, and you get an A in this class. c) To get an A in this class, it is necessary for you to get an A on the final. d) You get an A on the final, but you don't do every ex­ ercise in this book; nevertheless, you get an A in this class. e) Getting an A on the final and doing every exercise in this book is sufficient for getting an A in this class. I) You will get an A in this class if and only if you either do every exercise in this book or you get an A on the final. 11. Let p, q, and r be the propositions p : Grizzly bears have been seen in the area. q : Hiking is safe on the trail. r : Berries are ripe along the trail. Write these propositions using p, q, and r and logical connectives. a) Berries are ripe along the trail, but grizzly bears have not been seen in the area. b) Grizzly bears have not been seen in the area and hiking on the trail is safe, but berries are ripe along the trail. c) If berries are ripe along the trail, hiking is safe if and only if grizzly bears have not been seen in the area. d) It is not safe to hike on the trail, but grizzly bears have not been seen in the area and the berries along the trail are ripe. e) For hiking on the trail to be safe, it is necessary but not sufficient that berries not be ripe along the trail and for grizzly bears not to have been seen in the area. I) Hiking is not safe on the trail whenever grizzly bears have been seen in the area and berries are ripe along the trail. 12. Determine whether these biconditionals are true or false. a) 2 + 2 = 4 if and only if I + 1 = 2. b) 1 + 1 = 2 if and only if2 + 3 = 4. c) 1 + I = 3 if and only if monkeys can fly. d) 0> I if and only if2 > l. 13. Determine whether each of these conditional statements is true or false. a) If I + I = 2, then 2 + 2 = 5. b) If 1 + 1 = 3, then 2 + 2 = 4. c) If I + I = 3, then 2 + 2 = 5. d) If monkeys can fly, then 1 + I = 3. 14. Determine whether each of these conditional statements is true or false. a) If I + I = 3, then unicorns exists. b) If 1 + 1 = 3, then dogs can fly. c) If I + I = 2, then dogs can fly. d) If2 + 2 = 4, then I + 2 = 3. 18 I / The Foundations: Logic and Proofs 15. For each of these sentences, determine whether an inclu­ sive or or an exclusive or is intended. Explain your answer. a) Coffee or tea comes with dinner. b) A password must have at least three digits or be at least eight characters long. e) The prerequisite for the course is a course in number theory or a course in cryptography. d) You can pay using U.S. dollars or euros. 16. For each of these sentences, determine whether an inclu­ sive or or an exclusive or is intended. Explain your answer. a) Experience with C++ or Java is required. b) Lunch includes soup or salad. e) To enter the country you need a passport or a voter registration card. d) Publish or perish. 17. For each of these sentences, state what the sentence means ifthe or is an inclusive or (that is, a disjunction) versus an exclusive or. Which of these meanings of or do you think is intended? a) To take discrete mathematics, you must have taken calculus or a course in computer science. b) When you buy a new car from Acme Motor Company, you get $2000 back in cash or a 2% car loan. e) Dinner for two includes two items from column A or three items from column B. d) School is closed if more than 2 feet of snow falls or if the wind chill is below -100. 18. Write each of these statements in the form "if p, then q" in English. [Hint: Refer to the list of common ways to express conditional statements provided in this section.] a) It is necessary to wash the boss's car to get promoted. b) Winds from the south imply a spring thaw. e) A sufficient condition for the warranty to be good is that you bought the computer less than a year ago. d) Willy gets caught whenever he cheats. e) You can access the website only if you pay a subscrip­ tion fee. t) Getting elected follows from knowing the right people. g) Carol gets seasick whenever she is on a boat. 19. Write each of these statements in the form "if p, then q" in English. [Hint: Refer to the list of common ways to express conditional statements.] a) It snows whenever the wind blows from the northeast. b) The apple trees will bloom if it stays warm for a week. e) That the Pistons win the championship implies that they beat the Lakers. d) It is necessary to walk 8 miles to get to the top of Long's Peak. e) To get tenure as a professor, it is sufficient to be world-famous. t) If you drive more than 400 miles, you will need to buy gasoline. g) Your guarantee is good only if you bought your CD player less than 90 days ago. h) Jan will go swimming unless the water is too cold. I-Iii 20. Write each of these statements in the form "if p, then q" in English. [Hint: Refer to the list of common ways to express conditional statements provided in this section.] a) I will remember to send you the address only if you send me an e-mail message. . b) To be a citizen of this country, it is sufficient that you were born in the United States. e) If you keep your textbook, it will be a useful reference in your future courses. d) The Red Wings will win the Stanley Cup iftheir goalie plays well. e) That you get the job implies that you had the best credentials. t) The beach erodes whenever there is a storm. g) It is necessary to have a valid password to log on to the server. h) You will reach the summit unless you begin your climb too late. 21. Write each of these propositions in the form "p if and only if q" in English. a) If it is hot outside you buy an ice cream cone, and if you buy an ice cream cone it is hot outside. b) For you to win the contest it is necessary and sufficient that you have the only winning ticket. e) You get promoted only if you have connections, and you have connections only if you get promoted. d) If you watch television your mind will decay, and conversely. e) The trains run late on exactly those days when I take it. 22. Write each of these propositions in the form "p if and only if q" in English. a) For you to get an A in this course, it is necessary and sufficient that you learn how to solve discrete mathe­ matics problems. b) If you read the newspaper every day, you will be in­ formed, and conversely. e) It rains ifit is a weekend day, and it is a weekend day ifit rains. d) You can see the wizard only if the wizard is not in, and the wizard is not in only if you can see him. 23. State the converse, contrapositive, and inverse of each of these conditional statements. a) Ifit snows today, I will ski tomorrow. b) I come to class whenever there is going to be a quiz. e) A positive integer is a prime only if it has no divisors other than I and itself. 24. State the converse, contrapositive, and inverse of each of these conditional statements. a) Ifit snows tonight, then I will stay at home. b) I go to the beach whenever it is a sunny summer day. c) When I stay up late, it is necessary that I sleep until noon. 25. How many rows appear in a truth table for each of these compound propositions? /-/9 a) p -+ -'p b) (p v -.r) /\ (q V -.s) c) q v p v -'s V -.r V -.t V u d) (p /\ r /\ t) ++ (q /\ t) 26. How many rows appear in a truth table for each of these compound propositions? a) (q -+ -'p) v (-'P -+ -.q) b) (p v -.t) /\ (p v -'s) c) (p -+ r) V (-,s -+ -.t) v (-,u -+ v) d) (p /\ r /\ s) V (q /\ t) V (r /\ -.t) 27. Construct a truth table for each of these compound propositions. a) p /\ -.p b) P v -'p c) (pv-'q)-+q d) (pvq)-+(p/\q) e) (p -+ q) ++ (-.q -+ -'p) t) (p -+ q) -+ (q -+ p) 28. Construct a truth table for each of these compound propositions. a) p -+ -'p c) ptIJ(pvq) e) (q -+ -'p) ++ (p ++ q) t) (p ++ q) tIJ (p ++ -.q) b) P ++-'P d) (p /\ q) -+ (p v q) 29. Construct a truth table for each of these compound propositions. a) (pvq)-+ (ptIJq) c) (p V q) tIJ (p /\ q) e) (p ++ q) tIJ (-'P ++ -.r) t) (p tIJ q) -+ (p tIJ -.q) b) (p tIJ q) -+ (p /\ q) d) (p ++ q) tIJ (-'P ++ q) 30. Construct a truth table for each of these compound propositions. a) p tIJ p c) p tIJ -.q e) (p tIJ q) v (p tIJ -.q) b) P tIJ -'p d) -.p tIJ -.q t) (p tIJ q) /\ (p tIJ -.q) 31. Construct a truth table for each of these compound propositions. a) p -+ -.q c) (p -+ q) v (-'P -+ q) e) (p ++ q) v (-'P ++ q) t) (-'P ++ -.q) ++ (p ++ q) b) -'p ++ q d) (p -+ q) /\ (-'P -+ q) 32. Construct a truth table for each of these compound propositions. a) (pvq)vr c) (p /\ q) V r e) (p V q) /\ -.r b) (pvq)/\r d) (p /\ q) /\ r t) (p /\ q) V -'r 33. Construct a truth table for each of these compound propositions. a) p -+ (-.q V r) b) -'p -+ (q -+ r) c) (p -+ q) v (-'P -+ r) d) (p -+ q) /\ (-'P -+ r) e) (p ++ q) v (-.q ++ r) t) (-'P ++ -.q) ++ (q ++ r) 34. Construct a truth table for «p -+ q) -+ r) -+ s. 1.1 Propositional Logic 19 35. Construct a truth table for (p ++ q) ++ (r ++ s). 36. What is the value of x after each of these statements is encountered in a computer program, if x = 1 before the statement is reached? a) if 1 + 2 = 3 then x := x + 1 b) if (1 + 1 = 3) OR (2 + 2 = 3) then x := x + 1 c) if (2 + 3 = 5) AND (3 + 4 = 7) then x := x + 1 d) if(1 + 1 = 2)XOR (1 + 2 = 3) then x := x + 1 e) if x < 2 then x := x + 1 37. Find the bitwise OR, bitwise AND, and bitwise XOR of each ofthese pairs of bit strings. a) 1011110,0100001 b) 1111 0000, 1010 1010 c) 000111 0001, 10 01001000 d) 11 1111 1111, 00 0000 0000 38. Evaluate each of these expressions. a) I 1000/\(0 1011 v 1 1011) b) (0 1111 /\ 1 0101) v 0 1000 c) (0 1010 tIJ 1 1011) tIJ 0 1000 d) (1 1011 v 01010) /\ (10001 v 1 1011) Fuzzy logic is used in artificial intelligence. In fuzzy logic, a proposition has a truth value that is a number between 0 and I, inclusive. A proposition with a truth value of 0 is false and one with a truth value of 1 is true. Truth values that are between 0 and 1 indicate varying degrees of truth. For instance, the truth value 0.8 can be assigned to the statement "Fred is happy," because Fred is happy most of the time, and the truth value 0.4 can be assigned to the statement "John is happy," because John is happy slightly less than halfthe time. 39. The truth value of the negation of a proposition in fuzzy logic is 1 minus the truth value of the proposition. What are the truth values of the statements "Fred is not happy" and "John is not happy"? 40. The truth value of the conjunction of two propositions in fuzzy logic is the minimum of the truth values of the two propositions. What are the truth values of the statements "Fred and John are happy" and "Neither Fred nor John is happy"? 41. The truth value of the disjunction of two propositions in fuzzy logic is the maximum ofthe truth values of the two propositions. What are the truth values of the statements "Fred is happy, or John is happy" and "Fred is not happy, or John is not happy"? *42. Is the assertion "This statement is false" a proposition? *43. The nth statement in a list of 100 statements is "Exactly n ofthe statements in this list are false." a) What conclusions can you draw from these statements? b) Answer part (a) if the nth statement is "At least n of the statements in this list are false." c) Answer part (b) assuming that the list contains 99 statements. 44. An ancient Sicilian legend says that the barber in a re­ mote town who can be reached only by traveling a dan­ gerous mountain road shaves those people, and only those 20 1 / The Foundations: Logic and Proofs people, who do not shave themselves. Can there be such a barber? 45. Each inhabitant of a remote village always tells the truth or always lies. A villager will only give a "Yes" or a "No" response to a question a tourist asks. Suppose you are a tourist visiting this area and come to a fork in the road. One branch leads to the ruins you want to visit; the other branch leads deep into the jungle. A villager is standing at the fork in the road. What one question can you ask the villager to determine which branch to take? 46. An explorer is captured by a group of cannibals. There are two types of cannibals-those who always tell the truth and those who always lie. The cannibals will barbecue the explorer unless he can determine whether a particular cannibal always lies or always tells the truth. He is allowed to ask the cannibal exactly one question. a) Explain why the question "Are you a liar?" does not work. b) Find a question that the explorer can use to determine whether the cannibal always lies or always tells the truth. 47. Express these system specifications using the propositions p "The message is scanned for viruses" and q "The mes­ sage was sent from an unknown system" together with logical connectives. a) "The message is scanned for viruses whenever the message was sent from an unknown system." b) "The message was sent from an unknown system but it was not scanned for viruses." c) "It is necessary to scan the message for viruses when­ ever it was sent from an unknown system." d) "When a message is not sent from an unknown system it is not scanned for viruses." 48. Express these system specifications using the proposi­ tions p "The user enters a valid password," q "Access is granted," and r "The user has paid the subscription fee" and logical connective s. a) "The user has paid the subscription fee, but does not enter a valid password." b) "Access is granted whenever the user has paid the sub­ scription fee and enters a valid password." c) "Access is denied ifthe user has not paid the subscrip­ tion fee." d) "If the user has not entered a valid password but has paid the subscription fee, then access is granted." 49. Are these system specifications consistent? "The system is in multiuser state if and only if it is operating normally. If the system is operating normally, the kernel is func­ tioning. The kernel is not functioning or the system is in interrupt mode. If the system is not in multiuser state, then it is in interrupt mode. The system is not in interrupt mode." 50. Are these system specifications consistent? "Whenever the system software is being upgraded, users cannot ac­ cess the file system. If users can access the file system, /-20 then they can save new files. Ifusers cannot save new files, then the system software is not being upgraded." 51. Are these system specifications consistent? "The router can send packets to the edge system only if it supports the new address space. For the router to support the new ad­ dress space it is necessary that the latest software release be installed. The router can send packets to the edge sys­ tem if the latest software release is installed, The router does not support the new address space." 52. Are these system specifications consistent? "If the file system is not locked, then new messages will be queued. If the file system is not locked, then the system is func­ tioning normally, and conversely. Ifnew messages are not queued, then they will be sent to the message buffer. If the file system is not locked, then new messages will be sent to the message buffer. New messages will not be sent to the message buffer." 53. What Boolean search would you use to look for Web pages about beaches in New Jersey? What if you wanted to find Web pages about beaches on the isle of Jersey (in the English Channel)? 54. What Boolean search would you use to look for Web pages about hiking in West Virginia? What if you wanted to find Web pages about hiking in Virginia, but not in West Virginia? Exercises 55-59 relate to inhabitants of the island of knights and knaves created by Smullyan, where knights always tell the truth and knaves always lie. You encounter two people, A and B. Determine, if possible, what A and B are if they address you in the ways described. If you cannot determine what these two people are, can you draw any conclusions? 55. A says "At least one of us is a knave" and B says nothing. 56. A says "The two of us are both knights" and B says" A is a knave." 57. A says "I am a knave or B is a knight" and B says nothing. 58. Both A and B say "I am a knight." 59. A says "We are both knaves" and B says nothing. Exercises 60-65 are puzzles that can be solved by translating statements into logical expressions and reasoning from these expressions using truth tables. 60. The police have three suspects for the murder of Mr. Cooper: Mr. Smith, Mr. Jones, and Mr. Williams. Smith, Jones, and Williams each declare that they did not kill Cooper. Smith also states that Cooper was a friend of Jones and that Williams disliked him. Jones also states that he did not know Cooper and that he was out of town the day Cooper was killed. Williams also states that he saw both Smith and Jones with Cooper the day of the killing and that either Smith or Jones must have killed him. Can you determine who the murderer was if a) one of the three men is guilty, the two innocent men are telling the truth, but the statements of the guilty man mayor may not be true? b) innocent men do not lie? 1-21 61. Steve would like to determine the relative salaries ofthree coworkers using two facts. First, he knows that if Fred is not the highest paid of the three, then Janice is. Sec­ ond, he knows that if Janice is not the lowest paid, then Maggie is paid the most. Is it possible to determine the relative salaries of Fred, Maggie, and Janice from what Steve knows? If so, who is paid the most and who the least? Explain your reasoning. 62. Five friends have access to a chat room. Is it possible to determine who is chatting if the following information is known? Either Kevin or Heather, or both, are chatting. Either Randy or Vijay, but not both, are chatting. If Abby is chatting, so is Randy. Vijay and Kevin are either both chatting or neither is. If Heather is chatting, then so are Abby and Kevin. Explain your reasoning. 63. A detective has interviewed four witnesses to a crime. From the stories of the witnesses the detective has con­ cluded that if the butler is telling the truth then so is the cook; the cook and the gardener cannot both be telling the truth; the gardener and the handyman are not both lying; and if the handyman is telling the truth then the cook is lying. For each ofthe four witnesses, can the detective de­ termine whether that person is telling the truth or lying? Explain your reasoning. 64. Four friends have been identified as suspects for an unau­ thorized access into a computer system. They have made statements to the investigating authorities. Alice said "Carlos did it." John said "I did not do it." Carlos said 1.2 Propositional Equivalences Introduction 1.2 Propositional Equivalences 21 "Diana did it." Diana said "Carlos lied when he said that I did it." a) Ifthe authorities also know that exactly one of the four suspects is telling the truth, who did it? Explain your reasoning. b) If the authorities also know that exactly one is lying, who did it? Explain your reasoning. *65. Solve this famous logic puzzle, attributed to Albert Einstein, and known as the zebra puzzle. Five men with different nationalities and with different jobs live in con-� secutive houses on a street. These houses are painted different colors. The men have different pets and have dif­ ferent favorite drinks. Determine who owns a zebra and whose favorite drink is mineral water (which is one ofthe favorite drinks) given these clues: The Englishman lives in the red house. The Spaniard owns a dog. The Japanese man is a painter. The Italian drinks tea. The Norwegian lives in the first house on the left. The green house is immediately to the right of the white one. The photogra­ pher breeds snails. The diplomat lives in the yellow house. Milk is drunk in the middle house. The owner of the green house drinks coffee. The Norwegian's house is next to the blue one. The violinist drinks orange juice. The fox is in a house next to that of the physician. The horse is in a house next to that of the diplomat. [Hint: Make a table where the rows represent the men and columns represent the color of their houses, their jobs, their pets, and their favorite drinks and use logical reasoning to determine the correct entries in the table.] An important type of step used in a mathematical argument is the replacement of a statement with another statement with the same truth value. Because of this, methods that produce propo­ sitions with the same truth value as a given compound proposition are used extensively in the construction of mathematical arguments. Note that we will use the term "compound propo­ sition" to refer to an expression formed from propositional variables using logical operators, such as p /\ q. DEFINITION 1 We begin our discussion with a classification of compound propositions according to their possible truth values. A compound proposition that is always true, no matter what the truth values of the propositions that occur in it, is called a tautology. A compound proposition that is always false is called a contradiction. A compound proposition that is neither a tautology nor a contradiction is called a contingenc y. Tautologies and contradictions are often important in mathematical reasoning. Example I illus­ trates these types of compound propositions. 22 1 / The Foundations: Logic and Proofs /-22 TABLE 1 Examples of a Tautology and a Contradiction. p -,p p v-,p P I\-,p T F T F F T T F TABLE 2 De Morgan's Laws. -'(p 1\ q) == -'p v -.q -'(p v q) == -'p 1\ -.q EXAMPLE 1 We can construct exam ples oftautologies and contradictions using just one propositional vari­ able. Consider the truth tables of p v """p and p 1\ """p, shown in Table 1. Because p v """p is always true, it is a tautology. Because p 1\ """p is always false, it is a contradiction. � Demo� DEFINITION 2 Extra� Examples ..... EXAMPLE 2 Logical Equivalences Compound propositions that have the same truth values in all possible cases are called logically equivalent. We can also define this notion as follows. The compound propositions p and q are called logically equivalent if p � q is a tautology. The notation p == q denotes that p and q are logically equivalent. Remark: The symbol == is not a logical connective and p == q is not a compound proposition but rather is the statement that p � q is a tautology. The symbol {} is sometimes used instead of == to denote logical equivalence. One way to determine whether two compound propositions are equivalent is to use a truth table. In particular, the compound propositions p and q are equivalent if and only if the columns giving their truth values agree. Example 2 illustrates this method to establish an extremely important and useful logical equivalence, namely, that ...... (p v q) of """p 1\ ...... q. This logical equivalence is one of the two De Morgan laws, shown in Table 2, named after the English mathematician Augustus De Morgan, of the mid-nineteenth century. Show that ...... (p v q) and """p 1\ ...... q are logically equivalent. Solution: The truth tables for these compound propositions are <l:isplayed in Table 3. Because the truth values of the compound propositions ...... (p v q) and """p 1\ ...... q agree for all possible combinations ofthe truth values of p and q, it follows that ...... (p v q) � ( ...... p 1\ ...... q) is a tautology and that these compound propositions are logically equivalent. � TABLE 3 Truth Tables for.(p V q) and.p 1\ .q. p q pVq -,(p v q) -,p -'q -,p 1\ -,q T T T F F F F T F T F F T F F T T F T F F F F F T T T T 1.'.1 1.2 Propositional Equivalences 23 TABLE 4 Truth Tables for -,p V q and p-q. p q "'p "'p V q p-q T T F T T T F F F F F T T T T F F T T T EXAMPLE 3 Show that p -+ q and...,p v q are logically equivalent. Solution: We construct the truth table for these compound propositions in Table 4. Because the truth values of ""p v q and p -+ q agree, they are logically equivalent. .... We will now establish a logical equivalence of two compound propositions involving three different propositional variables p, q, and r. To use a truth table to establish such a logical equivalence, we need eight rows, one for each possible combination of truth values of these three variables. We symbolically represent these combinations by listing the truth values of p, q, and r, respectively. These eight combinations of truth values are TTT, TTF, TFT, TFF, FTT, FTF, FFT, and FFF; we use this order when we display the rows ofthe truth table. Note that we need to double the number of rows in the truth tables we use to show that compound propositions are equivalent for each additional propositional variable, so that 16 rows are needed to establish the logical equivalence of two compound propositions involving four propositional variables, and so on. In general, 2n rows are required if a compound proposition involves n propositional variables. EXAMPLE 4 Show that p V (q 1\ r) and (p v q) 1\ (p V r) are logically equivalent. This is the distributive law of disjunction over conjunction. Solution: We construct the truth table for these compound propositions in Table 5. Because the truth values of p V (q 1\ r) and (p v q) 1\ (p V r) agree, these compound propositions are logically equivalent. .... TABLE 5 A Demonstration That p V (q 1\ r) and (p V q) 1\ (p V r) Are Logically Equivalent . p q r q 1\ r pV(ql\r) pVq pVr (p V q) 1\ (p Vr) T T T T T T T T T T F F T T T T T F T F T T T T T F F F T T T T F T T T T T T T F T F F F T F F F F T F F F T F F F F F F F F F 24 1 / The Foundations: Logic and Proofs TABLE 6 Logical Equivalences. Equivalence p/\T=p pvF=p pvT=T p/\F=F pv p=p P/\p=p �(�p) = p pvq=qvp P/\q=q/\P (p V q) V r = p V (q v r) (p /\ q) /\ r = p /\ (q /\ r) p v (q /\ r) = (p V q) /\ (p V r) p /\ (q V r) = (p /\ q) V (p /\ r) �(p/\q)=�pv�q �(p v q) = �p /\ �q P V (p /\ q) = p p /\ (p V q) = p pv�p =T p /\ �p = F 1-24 Name Identity laws Domination laws Idempotent laws Double negation law Commutative laws Associative laws Distributive laws De Morgan's laws Absorption laws Negation laws Table 6 contains some important equivalences. * In these equivalences, T denotes the com­ pound proposition that is always true and F denotes the compound proposition that is al­ ways false. We also display some useful equivalences for compound propositions involving conditional statements and biconditional statements in Tables 7 and 8, respectively. The reader is asked to verify the equivalences in Tables 6-8 in the exercises at the end of the section. The associative law for disjunction shows that the expression P v q V r is well defined, in the sense that it does not matter whether we first take the disjunction of P with q and then the disjunction of P v q with r, or if we first take the disjunction of q and r and then take the disjunction of P with q v r. Similarly, the expression P /\ q /\ r is well defined. By extending this reasoning, it follows that PI v P2 V ... V Pn and PI /\ P2 /\ ... /\ Pn are well defined whenever PI, P2, ... , Pn are propositions. Furthermore, note that De Morgan's laws extend to and --'(PI /\ P2 /\ ... /\ Pn) == (--'PI V --'P2 V ... v --'Pn). (Methods for proving these identities will be given in Section 4.1.) 'Readers familiar with the concept of a Boolean algebra will notice that these identities are a special case of identities that hold for any Boolean algebra. Compare them with set identities in Table I in Section 2.2 and with Boolean identities in Table 5 in Section Il.l. 1-25 TABLE 7 Logical Equivalences Involvin g Conditional Statements. p ---+ q == �p v q p ---+ q == �q ---+ �p p v q == �p ---+ q P 1\ q == �(p ---+ �q) �(p ---+ q) == P 1\ �q (p ---+ q) 1\ (p ---+ r) == p ---+ (q 1\ r) (p ---+ r) 1\ (q ---+ r) == (p V q) ---+ r (p ---+ q) v (p ---+ r) == p ---+ (q V r) (p ---+ r) V (q ---+ r) == (p 1\ q) ---+ r Using De Morgan's Laws 1.2 Propositional Equivalences 25 TABLE 8 Logical Equivalences Involvin g Biconditionals. P *+ q == (p ---+ q) 1\ (q ---+ p) P *+q ==�p *+ �q P *+ q == (p 1\ q) V (�p 1\ �q) �(p *+ q) == P *+ �q The two logical equivalences known as De Morgan's laws are particularly important. They tell us how to negate conjunctions and how to negate disjunctions. In particular, the equivalence -'(p v q) == -'p /\ -.q tells us that the negation of a disjunction is formed by taking the con­ junction of the negations of the component propositions. Similarly, the equivalence -'(p /\ q) == -'p v -.q tells us that the negation of a conjunction is formed by taking the disjunction of the negations of the component propositions. Example 5 illustrates the use of De Morgan's laws. EXAMPLE 5 Use De Morgan's laws to express the negations of "Migue1 has a cellphone and he has a laptop computer" and "Heather will go to the concert or Steve will go to the concert." unkS� Solution: Let p be "Miguel has a cellphone" and q be "Miguel has a laptop computer." Then "Miguel has a cellphone and he has a laptop computer" can be represented by p /\ q. By the first of De Morgan's laws, -'(p /\ q) is equivalent to -'p v -'q. Consequently, we can express AUGUSTUS DE MORGAN (1806-1871) Augustus De Morgan was born in India, where his father was a colonel in the Indian army. De Morgan's family moved to England when he was 7 months old. He attended private schools, where he developed a strong interest in mathematics in his early teens. De Morgan studied at Trinity College, Cambridge, graduating in 1827. Although he considered entering medicine or law, he decided on a career in mathema tics. He won a position at University College, London, in 1828, but resigned when the college dismissed a fellow professor without giving reasons. However, he resumed this position in 1836 when his successor died, staying there until 1866. De Morgan was a noted teacher who stressed principles over techniques. His students included many famous mathemati cians, including Augusta Ada, Countess of Lovelace, who was Charles Babbage's collaborator in his work on computing machines (see page 27 for biographical notes on Augusta Ada). (De Morgan cautioned the countess against studying too much mathemati cs, because it might interfere with her childbearing abilities!) De Morgan was an extremely prolific writer. He wrote more than 1000 articles for more than 15 periodicals. De Morgan also wrote textbooks on many subjects, including logic, probability, calculus, and algebra. In 1838 he presented what was perhaps the first clear explanation of an important proof technique known as mathematical induction (discussed in Section 4.1 of this text), a term he coined. In the 1 840s De Morgan made fundamental contributions to the development of symbolic logic. He invented notations that helped him prove propositional equivalences, such as the laws that are named after him. In 1842 De Morgan presented what was perhaps the first precise definition of a limit and developed some tests for convergence of infinite series. De Morgan was also interested in the history of mathematics and wrote biographies of Newton and Halley. In 1837 De Morgan married Sophia Frend, who wrote his biography in 1882. De Morgan's research, writing, and teaching left little time for his family or social life. Neverthe less, he was noted for his kindness, humor, and wide range of knowledge. 26 1 / The FOWldati ons: Logic and Proofs /-26 EXAMPLE 6 Extra� Examples� the negation of our original statement as "Miguel does not have a cell phone or he does not have a laptop computer." Let r be "Heather will go to the concert" and s be "Steve will go to the concert." Then "Heather will go to the concert or Steve will go to the concert" can be represented by r v s. By the second of De Morgan's laws, ..... (r v s) is equivalent to ..... r /\ ..... s. Consequently, we can express the negation of our original statement as "Heather will not go to the concert and Steve will not go to the concert." .... Constructing New Logical Equivalences The logical equivalences in Table 6, as well as any others that have been established (such as those shown in Tables 7 and 8), can be used to construct additional logical equivalences. The reason for this is that a proposition in a compound proposition can be replaced by a compound proposition that is logically equivalent to it without changing the truth value of the original compound proposition. This technique is illustrated in Examples 6-8, where we also use the fact that if p and q are logically equivalent and q and r are logically equivalent, then p and r are logically equivalent (see Exercise 56). Show that ..... (p -+ q) and p /\ ..... q are logically equivalent. Solution: We could use a truth table to show that these compound propositions are equivalent (similar to what we did in Example 4). Indeed, it would not be hard to do so. However, we want to illustrate how to use logical identities that we already know to establish new logical identities, something that is of practical importance for establishing equivalences of compound propositions with a large number of variables. So, we will establish this equivalence by developing a series of logical equivalences, using one of the equivalences in Table 6 at a time, starting with ..... (p -+ q) and ending with p /\ ..... q. We have the following equivalences . ..... (p -+ q) == ..... ( ..... p v q) by Example 3 == ..... ( ..... p) /\ ..... q by the second De Morgan law == p /\ ..... q by the double negation law EXAMPLE 7 Show that ..... (p v ( ..... p /\ q)) and ""'p /\ ..... q are logically equivalent by developing a series of logical equivalences. Solution: We will use one of the equivalences in Table 6 at a time, starting with ..... (p v ( ..... p /\ q)) and ending with ""'p /\ ..... q. (Note: we could also easily establish this equivalence using a truth table.) We have the following equivalences . ..... (p v ( ..... p /\ q)) == ""'p /\ ..... ( ..... p /\ q) == ""'p /\ [ ..... ( ..... p) v ..... q] == ""'p /\ (p V ..... q) == ( ..... p /\ p) v ( ..... p /\ ..... q) == F v ( ..... p /\ ..... q) == ( ..... p /\ ..... q) v F by the second De Morgan law by the first De Morgan law by the double negation law by the second distributive law because -'p /\ P == F by the commutative law for disjunction by the identity law for F Consequently ..... (p v ( ..... p /\ q)) and ..... p /\ ..... q are logically equivalent. / :1 7 1.2 Propositional Equivalences 27 EXAMPLE 8 Show that (p 1\ q) --+ (p v q) is a tautology. Ulksitl Solution: To show that this statement is a tautology, we will use logical equivalences to demon­ strate that it is logically equivalent to T. (Note: This could also be done using a truth table.) (p 1\ q) --+ (p v q) =. -'(p 1\ q) V (p V q) =. (-'p v -.q) v (p v q) =. (-'p v p) v (-.q v q) =.TvT =.T by Example 3 by the first De Morgan law by the associative and commutative laws for disjunction by Example I and the commutative law for disjunction by the domination law A truth table can be used to determine whether a compound proposition is a tautology. This can be done by hand for a compound proposition with a small number of variables, but when the number of variables grows, this becomes impractical. For instance, there are 220 = 1,048,576 rows in the truth value table for a compound proposition with 20 variables. Clearly, you need a computer to help you determine, in this way, whether a compound proposition in 20 variables is a tautology. But when there are 1000 variables, can even a computer determine in a reasonable amount of time whether a compound proposition is a tautology? Checking every one of the 21000 (a number with more than 300 decimal digits) possible combinations of truth values simply cannot be done by a computer in even trillions of years. Furthermore, no other procedures are known that a computer can follow to determine in a reasonable amount of time whether a compound proposition in such a large number of variables is a tautology. We will study questions such as this in Chapter 3, when we study the complexity of algorithms. AUGUST A ADA, COUNTESS OF LOVELACE (1815-1852) Augusta Ada was the only child from the marriage of the famous poet Lord Byron and Lady Byron, Annabella Millbanke, who separated when Ada was 1 month old, because of Lord Byron's scandalous affair with his half sister. The Lord Byron had quite a reputation, being described by one of his lovers as "mad, bad, and dangerous to know." Lady Byron was noted for her intellect and had a passion for mathema tics; she was called by Lord Byron "The Princess of Parallelograms." Augusta was raised by her mother, who encouraged her intellectual talents especially in music and mathematics, to counter what Lady Byron considered dangerous poetic tendencies. At this time, women were not allowed to attend universities and could not join learned societies. Nevertheless, Augusta pursued her mathematical studies independently and with mathemati cians, including William Frend. She was also encouraged by another female mathematician, Mary Somerville, and in 1834 at it dinner party hosted by Mary Somerville, she learned about Charles Babbage's ideas for a calculating machine, called the Analytic Engine. In 1838 Augusta Ada married Lord King, later elevated to Earl of Lovelace. Together they had three children. Augusta Ada continued her mathematical studies after her marriage. Charles Babbage had continued work on his Analytic Engine and lectured on this in Europe. In 1842 Babbage asked Augusta Ada to translate an article in French describing Babbage's invention. When Babbage saw her translation, he suggested she add her own notes, and the resulting work was three times the length of the original. The most complete accounts of the Analytic Machine are found in Augusta Ada's notes. In her notes, she compared the working of the Analytic Engine to that of the Jacquard loom, with Babbage's punch cards analogous to the cards used to create patterns on the loom. Furthermore, she recognized the promises of the machine as a general purpose computer much better than Babbage did. She stated that the "engine is the material expression of any indefinite fimction of any degree of generality and complexity." Her notes on the Analytic Engine anticipate many future developments, including computer-generated music. Augusta Ada published her writings under her initials A.A.L. concealing her identity as a women as did many women did at a time when women were not considered to be the intellectual equals of men. After 1845 she and Babbage worked toward the development of a system to predict horse races. Unfortunately, their system did not work well, leaving Augusta heavily in debt at the time of her death at an unfortunately young age from uterine cancer. In 1953 Augusta Ada's notes on the Analytic Engine were republished more than 100 years after they were written, and after they had been long forgotten. In his work in the 1950s on the capacity of computers to think (and his famous Turing Test), Alan Turing responded to Augusta Ada's statement that "The Analytic Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform." This "dialogue" between Turing and Augusta Ada is still the subject of controversy. Because of her fimdamental contributions to computing, the programming language Augusta is named in honor of the Countess of Lovelace. 28 I / The Foundations: Logic and Proofs Exercises 1. Use truth tables to verify these equivalences. a) p /\ T == p b) p V F == P c) P /\ F == F d) p v T == T e) p v p == p t) P /\ P == P 2. Show that -'(-'p) and p are logically equivalent. 3. Use truth tables to verify the commutative laws a) p V q == q v p b) p /\ q == q /\ P 4. Use truth tables to verify the associative laws a) (p v q) V r == p V (q V r) b) (p /\ q) /\ r == p /\ (q /\ r) 5. Use a truth table to verify the distributive law p /\ (q V r) == (p /\ q) V (p /\ r). 6. Use a truth table to verify the first De Morgan law -'(p /\ q) == -'p v -'q. 7. Use De Morgan's laws to find the negation of each of the following statements. a) Jan is rich and happy. b) Carlos will bicycle or run tomorrow. c) Mei walks or takes the bus to class. d) Ibrahim is smart and hard working. 8. Use De Morgan's laws to find the negation of each of the following statements. a) Kwame will take ajob in industry or go to graduate school. b) Yoshiko knows Java and calculus. c) James is young and strong. d) Rita will move to Oregon or Washington. 1-28 9. Show that each of these conditional statements is a tau­ tology by using truth tables. a) (P/\q)-,;p c) -'p -'; (p -'; q) e) -'(p -'; q) -'; P b) p -'; (p v q) d) (p /\ q) -'; (p -'; q) t) -'(p -'; q) -'; -.q 10. Show that each of these conditional statements is a tau­ tology by using truth tables. a) [-'P /\ (p V q)] -'; q b) [(p -'; q) /\ (q -'; r)] -'; (p -'; r) c) [p /\ (p -'; q)] -'; q d) [(p v q) /\ (p -'; r) /\ (q -'; r)] -'; r 11. Show that each conditional statement in Exercise 9 is a tautology without using truth tables. 12. Show that each conditional statement in Exercise lOis a tautology without using truth tables. 13. Use truth tables to verify the absorption laws. a) p v (p /\ q) == p b) p /\ (p V q) == P 14. Determine whether (-'P /\ (p -'; q» -'; -.q is a tautology. 15. Determine whether (-.q /\ (p -'; q» -'; -'p is a tautology. Each of Exercises 16-28 asks you to show that two compound propositions are logically equivalent. To do this, either show that both sides are true, or that both sides are false, for exactly the same combinations of truth values of the propositional variables in these expressions (whichever is easier). 16. Show that p *+ q and (p /\ q) V (-'P /\ -.q) are equivalent. 17. Show that -'(p *+ q) and p *+ -.q are logically equivalent. HENRY MAURICE SHEFFER (1883-1964) Henry Maurice Sheffer, born to Jewish parents in the western Ukraine, emigrated to the United States in 1892 with his parents and six siblings. He studied at the Boston Latin School before entering Harvard, where he completed his undergraduate degree in 1905, his master's in 1907, and his Ph.D. in philosophy in 1908. After holding a postdoctoral position at Harvard, Henry traveled to Europe on a fellowship. Upon returning to the United States, he became an academic nomad, spending one year each at the University of Washington, Cornell, the University of Minnesota, the University of Missouri, and City College in New York. In 1916 he returned to Harvard as a faculty member in the philosophy department. He remained at Harvard until his retirement in 1952. Sheffer introduced what is now known as the Sheffer stroke in 1913; it became well known only after its use in the 1925 edition of Whitehead and Russell's Principia Mathematica. In this same edition Russell wrote that Sheffer had invented a powerful method that could be used to simplify the Principia. Because of this comment, Sheffer was something ofa mystery man to logicians, especially because Sheffer, who published little in his career, never published the details of this method, only describing it in mimeographed notes and in a brief published abstract. Sheffer was a dedicated teacher of mathematical logic. He liked his classes to be small and did not like auditors. When strangers appeared in his classroom, Sheffer would order them to leave, even his colleagues or distinguished guests visiting Harvard. Sheffer was barely five feet tall; he was noted for his wit and vigor, as well as for his nervousness and irritability. Although widely liked, he was quite lonely. He is noted for a quip he spoke at his retirement: "Old professors never die, they just become emeriti." Sheffer is also credited with coining the term "Boolean algebra" (the subject of Chapter 11 ofthis text). Sheffer was briefly married and lived most of his later life in small rooms at a hotel packed with his logic books and vast files of slips of paper he used to jot down his ideas. Unfortunately, Sheffer suffered from severe depression during the last two decades of his life. 1-29 18. Show that p -+ q and �q -+ �p are logically equivalent. 19. Show that �p ++ q and p ++ �q are logically equivalent. 20. Show that �(p EB q) and p ++ q are logically equivalent. 21. Show that �(p ++ q) and �p ++ q are logically equivalent. 22. Show that (p -+ q) /\ (p -+ r) and p -+ (q /\ r) are log­ ically equivalent. 23. Show that (p -+ r) /\ (q -+ r) and (p v q) -+ r are log­ ically equivalent. 24. Show that (p -+ q) v (p -+ r) and p -+ (q V r) are log­ ically equivalent. 25. Show that (p -+ r) V (q -+ r) and (p /\ q) -+ r are log­ ically equivalent. 26. Show that � p -+ (q -+ r) and q -+ (p V r) are logically equivalent. 27. Show that p ++ q and (p -+ q) /\ (q -+ p) are logically equivalent. 28. Show that p ++ q and �p ++ �q are logically equivalent. 29. Showthat(p -+ q) /\ (q -+ r) -+ (p -+ r)isatautology. 30. Show that (p v q) /\ (�p V r) -+ (q V r) is a tautology. 31. Show that (p -+ q) -+ rand p -+ (q -+ r) are not equivalent. 32. Show that (p /\ q) -+ rand (p -+ r) /\ (q -+ r) are not equivalent. 33. Showthat(p -+ q) -+ (r -+ s)and(p -+ r) -+ (q -+ s) are not logically equivalent. The dual of a compound proposition that contains only the logical operators v, /\, and � is the compound proposition obtained by replacing each v by /\, each /\ by v, each T by F, and each F by T. The dual of s is denoted by s*. 34. Find the dual of each ofthese compound propositions. a) p V �q b) P /\ (q V (r /\ T» c) (p/\�q)v(q/\F) 35. Find the dual of each of these compound propositions. a) p/\�q/\�r b) (p/\q/\r)vs c) (p v F) /\ (q v T) 36. When does s* = s, where s is a compound proposition? 37. Show that (s*)* = s when s is a compound proposition. 38. Show that the logical equivalences in Table 6, except for the double negation law, come in pairs, where each pair contains compound propositions that are duals of each other. **39. Why are the duals of two equivalent compound proposi­ tions also equivalent, where these compound propositions contain only the operators /\, v, and�? 40. Find a compound proposition involving the propositional variables p, q, and r that is true when p and q are true and r is false, but is false otherwise. [Hint: Use a conjunction of each propositional variable or its negation.] 41. Find a compound proposition involving the propositional variables p, q, and r that is true when exactly two of p, q, and r are true and is false otherwise. [Hint: Form a dis­ junction of conjunctions. Include a conjunction for each 1.2 Propositional Equivalences 29 combination of values for which the propositional vari­ able is true. Each conjunction should include each of the three propositional variables or their negations.] l? 42. Suppose that a truth table in n propositional variables is specified. Show that a compound proposition with this truth table can be formed by taking the disjunction of conjunctions of the variables or their negations, with one conjunction included for each combination of values for which the compound proposition is true. The resulting compound proposition is said to be in disjunctive normal form. A collection of logical operators is called functionally com­ plete if every compound proposition is logically equiva­ lent to a compound proposition involving only these logical operators. 43. Show that �, /\, and v form a functionally complete col­ lection oflogical operators. [Hint: Use the fact that every compound proposition is logically equivalent to one in disjunctive normal form, as shown in Exercise 42.] *44. Show that � and /\ form a functionally complete collec­ tion of logical operators. [Hint: First use a De Morgan law to show that p v q is equivalent to �(�p /\ �q).] *45. Show that � and v form a functionally complete collec- tion oflogical operators. The following exercises involve the logical operators NAND and NOR. The proposition p NAND q is true when either p or q, or both, are false; and it is false when both p and q are true. The proposition p NOR q is true when both p and q are false, and it is false otherwise. The propositions p NAND q and p NOR q are denoted by p I q and p + q, respectively. (The op­ erators I and + are called the Sheffer stroke and the Peirce arrow after H. M. Sheffer and C. S. Peirce, respectively.) 46. Construct a truth table for the logical operator NAND. 47. Show that p I q is logically equivalent to �(p /\ q). 48. Construct a truth table for the logical operator NOR. 49. Show that p + q is logically equivalent to �(p V q). 50. In this exercise we will show that {+} is a functionally complete collection of logical operators. a) Show that p + p is logically equivalent to �p. b) Show that (p + q) + (p + q) is logically equivalent to pvq. c) Conclude from parts (a) and (b), and Exercise 49, that {+} is a functionally complete collection of logical operators. *51. Find a compound proposition logically equivalent to p -+ q using only the logical operator +. 52. Show that {I} is a functionally complete collection oflog­ ical operators. 53. Show that p I q and q I p are equivalent. 54. Show that p I (q I r) and (p I q) I r are not equivalent, so that the logical operator I is not associative. *55. How many different truth tables of compound proposi­ tions are there that involve the propositional variables p andq? 30 1 / The Foundations: Logic and Proofs 56. Show that if p, q, and r are compound propositions such that p and q are logically equivalent and q and r are logically equivalent, then p and r are logically equivalent. 57. The following sentence is taken from the specification of a telephone system: "If the directory database is opened, then the monitor is put in a closed state, if the system is not in its initial state." This specification is hard to under­ stand because it involves two conditional statements. Find an equivalent, easier-to-understand specification that in­ volves disjunctions and negations but not conditional statements. 58. How many of the disjunctions p v -'q, -'p v q, q V r, q V -'r, and -'q v -,r can be made simultaneously true by an assignment of truth values to p, q, and r? 59. How many of the disjunctions p v -'q v s, -,p V -,r V s, -'p V -'r V -,s, -'p v q v -'s,q V r V -'s,q V -,r V -'S, -'p V -'q V -'S, P v r v s, and p v r v-'s can be made 1.3 Predicate� and Quantifiers Introduction /-30 simultaneously true by an assignment of truth values to p,q,r,ands? A compound proposition is satisfiable if there is an assign­ ment of truth values to the variables in the compound propo­ sition that makes the statement form true. 60. Which of these compound propositions are satisfiable? a) (p v q V -,r) /\ (p V -'q V -,s) /\ (p V -'r V -,s) /\ (-'p v -'q v -,s) /\ (p V q V -,s) b) (-'p v -'q v r) /\ (-'p v q v -,s) /\ (p V -'q V -'s) /\ (-'p v -,r V -,s) /\ (p V q V -'r) /\ (p V -'r V -,s) c) (p v q v r) /\ (p V -'q V -,s) /\ (q V -'r V s) /\ (-'p v r V s) /\ (-'p v q v -,s) /\ (p V -'q V -,r) /\ (-'p v -'q v s) /\ (-'p v -'r V -,s) 61. Explain how an algorithm for determining whether a compound proposition is satisfiable can be used to de­ termine whether a compound proposition is a tautology. [Hint: Look at -'P, where p is the compound proposition that is being examined.] Propositional logic, studied in Sections 1.1 and 1.2, cannot adequately express the meaning of statements in mathematics and in natural language. For example, suppose that we know that "Every computer connected to the university network is functioning properly." No rules of propositional logic allow us to conclude the truth of the statement "MATH3 is functioning properly," where MATH3 is one of the computers connected to the university network. Likewise, we cannot use the rules of propositional logic to conclude from the statement "CS2 is under attack by an intruder," where CS2 is a computer on the university network, to conclude the truth of "There is a computer on the university network that is under attack by an intruder." In this section we will introduce a more powerful type of logic called predicate logic. We . will see how predicate logic can be used to express the meaning of a wide range of statements in mathematics and computer science in ways that permit us to reason and explore relationships between objects. To understand predicate logic, we first need to introduce the concept of a predicate. Afterward, we will introduce the notion of quantifiers, which enable us reason with statements that assert that a certain property holds for all objects of a certain type and with statements that assert the existence of an object with a particular property. /-3/ Predicates Statements involving variables, such as and "x > 3," "x = y + 3," "x + y = z," "computer x is under attack by an intruder," "computer x is functioning properly," 1.3 Predicates and Quantifiers 31 are often found in mathematical assertions, in computer programs, and in system specifications. These statements are neither true nor false when the values of the variables are not specified. In this section, we will discuss the ways that propositions can be produced from such statements. The statement "x is greater than 3" has two parts. The first part, the variable x, is the subject of the statement. The second part-the predicate, "is greater than 3"-refers to a property that the subject of the statement can have. We can denote the statement "x is greater than 3" by P(x), where P denotes the predicate "is greater than 3" and x is the variable. The statement P(x) is also said to be the value of the propositional function P at x. Once a value has been assigned to the variable x, the statement P(x) becomes a proposition and has a truth value. Consider Examples 1 and 2. EXAMPLE 1 Let P(x) denote the statement "x > 3." What are the truth values of P(4) and P(2)? Solution: We obtain the statement P (4) by setting x = 4 in the statement "x > 3." Hence, P (4), which is the statement "4 > 3," is true. However, P(2), which is the statement "2 > 3," is false. .... EXAMPLE 2 Let A(x) denote the statement "Computer x is under attack by an intruder." Suppose that ofthe computers on campus, only CS2 and MATH 1 are currently under attack by intruders. What are truth values of A(CSl), A(CS2), and A(MATHl)? EXAMPLE 3 Solution: We obtain the statement A(CSl) by setting x = CSI in the statement "Computer x is under attack by an intruder." Because CS 1 is not on the list of computers currently under attack, we conclude that A(CSI) is false. Similarly, because CS2 and MATH 1 are on the list of computers under attack, we know that A(CS2) and A(MATHl) are true. .... We can also have statements that involve more than one variable. For instance, consider the statement "x = y + 3." We can denote this statement by Q(x, y), where x and yare variables and Q is the predicate. When values are assigned to the variables x and y, the statement Q(x, y) has a truth value. Let Q(x, y) denote the statement "x = y + 3." What are the truth values of the propositions Q(1, 2) and Q(3, O)? Solution: To obtain Q(I, 2), set x = 1 andy = 2 in the statement Q(x, y). Hence, Q(1, 2) is the statement "1 = 2 + 3," which is false. The statement Q(3, 0) is the proposition "3 = 0 + 3," which is true. .... 32 1 / The Foundations: Logic and Proofs 1-32 EXAM PLE 4 Let A(c, n) denote the statement "Computer c is connected to network n," where c is a variable representing a computer and n is a variable representing a network. Suppose that the computer MATHI is connected to network CAMPUS2, but not to network CAMPUSl. What are the values of A(MATHI, CAMPUS 1) and A(MATHI, CAMPUS2)? Solution: Because MATH 1 is not connected to the CAMPUS 1 network, we see that A(MATHI, CAMPUSI) is false. However, because MATHI is connected to the CAMPUS2 network, we see that A (MATH 1 , CAMPUS2) is true. ... Similarly, we can let R(x, y, z) denote the statement "x + y = z." When values are assigned to the variables x, y, and z, this statement has a truth value. EXAMPLE 5 What are the truth values of the propositions R(1, 2, 3) and R(O, 0, I)? unkS� Solution: The proposition R(1, 2, 3) is obtained by setting x = 1, Y = 2, and z = 3 in the statement R (x, y, z). We see that R(1, 2, 3) is the statement "1 + 2 = 3," which is true. Also note that R(O, 0, 1), which is the statement "0 + 0= 1," is false. ... In general, a statement involving the n variables Xl, X2, ••• ,Xn can be denoted by A statement of the form P(XI, X2, ••. , Xn) is the value of the propositional function P at the n-tuple (Xl, X2, .•• ,xn), and P is also called a n-place predicate or a n-ary predicate. Propositional functions occur in computer programs, as Example 6 demonstrate s. CHARLES SANDERS PEIRCE (1839-1914) Many consider Charles Peirce the most original and versatile intellect from the United States; he was born in Cambridge, Massachusetts. He made important contributions to an amazing number of disciplines, including mathema tics, astronomy, chemistry, geodesy, metrology, engineer­ ing, psycholo gy, philology, the history of science, and economics. He was also an inventor, a lifelong student of medicine, a book reviewer, a dramatist and an actor, a short story writer, a phenomenologist, a logician, and a metaphysician. He is noted as the preeminent system-building philosopher competent and productive in logic, mathematics, and a wide range of sciences. His father, Benjamin Peirce, was a prof essor of mathematics and natural philosophy at Harvard. Peirce attended Harvard (1855-1859) and received a Harvard master of arts degree (1862) and an advanced degree in chemistry from the Lawrence Scientific School (1863). His father encouraged him to pursue a career in science, but instead he chose to study logic and scientific methodology. In 1861, Peirce became an aide in the United States Coast Survey, with the goal of better understanding scientific methodology. His service for the Survey exempted him from military service during the Civil War. While working for the Survey, Peirce carried out astronomical and geodesic work. He made fundamental contributions to the design of pendulums and to map projections, applying new mathematical developments in the theory of elliptic functions. He was the first person to use the wavelength of light as a unit of measurement. Peirce rose to the position of Assistant for the Survey, a position he held until he was forced to resign in 1891 when he disagreed with the direction taken by the Survey's new administration. Although making his living from work in the physical sciences, Peirce developed a hierarchy of sciences, with mathematics at the top rung, in which the methods of one science could be adapted for use by those sciences under it in the hierarchy. He was also the founder of the American philosophical theory of pragmatism. The only academic position Peirce ever held was as a lecturer in logic at Johns Hopkins University in Baltimore from 1879 to 1884. His mathematical work during this time included contributions to logic, set theory, abstract algebra, and the philoso phy of mathema tics. His work is still relevant today; some of his work on logic has been recently applied to artificial intelligence. Peirce believed that the study of mathematics could develop the mind's powers of imagination, abstraction, and generalization. His diverse activities after retiring from the Survey included writing for newspapers and journals, contributing to scholarly dictionaries, translating scientific papers, guest lecturing, and textbook writing. Unfortunately, the income from these pursuits was insufficient to protect him and his second wife from abject poverty. He was supported in his later years by a fund created by his many admirers and administered by the philosopher William James, his lifelong friend. Although Peirce wrote and published voluminously in a vast range of subjects, he left more than 100,000 pages of unpublished manuscripts. Because of the difficulty of studying his unpublished writings, scholars have only recently started to understand some of his varied contributions. A group of people is devoted to making his work available over the Internet to bring a better appreciation of Peirce's accomplishments to the world. 1-33 1.3 Predicates and Quantifiers 33 EXAMPLE 6 Consider the statement if x > 0 then x := x + 1. When this statement is encountered in a program, the value of the variable x at that point in the execution of the program is inserted into P(x), which is "x > 0." If P(x) is true for this value of x, the assignment statement x := x + 1 is executed, so the value of x is increased by 1. If P(x) is false for this value of x, the assignment statement is not executed, so the value of x is not changed. � Predicates are also used in the verification that computer programs always produce the desired output when given valid input. The statements that describe valid input are known as preconditions and the conditions that the ouput should satisfy when the program has run are known as postconditions. As Example 7 illustrates, we use predicates to describe both preconditions and postconditions. We will study this process in greater detail in Section 4.4. EXAMPLE 7 Consider the following program, designed to interchange the values of two variables x and y. Assessment � temp := x x .-y y := temp Find predicates that we can use as the precondition and the postcondition to verify the correctness of this program. Then explain how to use them to verify that for all valid input the program does what is intended. Solution: For the precondition, we need to express that x and y have particular values before we run the program. So, for this precondition we can use the predicate P(x, y), where P(x, y) is the statement "x = a and y = b," where a and b are the values of x and y before we run the program. Because we want to verify that the program swaps the values of x and y for all input values, for the postcondition we can use Q(x, y), where Q(x, y) is the statement "x = band y = a." To verify that the program always does what it is supposed to do, suppose that the precon­ dition P(x, y) holds. That is, we suppose that the statement "x = a and y = b" is true. This means that x = a andy = b. The first step of the program, temp := x, assigns the value ofx to the variable temp, so after this step we know that x = a, temp = a, and y = b. After the second step of the program, x := y, we know that x = b, temp = a, and y = b. Finally, after the third step, we know that x = b, temp = a, and y = a. Consequently, after this program is run, the postcondition Q(x, y) holds, that is, the statement "x = b and y = a" is true. � Quantifiers When the variables in a propositional function are assigned values, the resulting statement becomes a proposition with a certain truth value. However, there is another important way, called quantification, to create a proposition from a propositional function. Quantification expresses the extent to which a predicate is true over a range of elements. In English, the words all, some, many, none, and few are used in quantifications. We will focus on two types of quantification here: universal quantification, which tells us that a predicate is true for every element under consideration, and existential quantification, which tells us that there is one or more element under consideration for which the predicate is true. The area of logic that deals with predicates and quantifiers is called the predicate calculus. 34 I / The Foundations: Logic and Proofs 1-34 Assessmenl � DEFINITION 1 EXAMPLE S THE UNIVERSAL QUANTIFIER Many mathematical statements assert that a property is true for all values of a variable in a particular domain, called the domain of discourse (or the universe of discourse), often just referred to as the domain. Such a statement is expressed using universal quantification. The universal quantification of P (x) for a particular domain is the proposition that asserts that P(x) is true for all values of x in this domain. Note that the domain specifies the possible values of the variable x. The meaning of the universal quantification of P(x) changes when we change the domain. The domain must always be specified when a universal quantifier is used; without it, the universal quantification of a statement in not defined. The universal quantification of P(x) is the statement "P(x) for all values ofx in the domain." The notation VxP(x) denotes the universal quantification of P(x). Here V is called the universal quantifier. We read Vx P(x) as "for all x P(x)" or "for every x P(x )." An element for which P(x) is false is called a counterexample ofVx P(x). The meaning of the universal quantifier is summarized in the first row of Table 1. We illustrate the use of the universal quantifier in Examples 8-13. Let P(x) be the statement "x + 1 > x." What is the truth value of the quantification VxP(x), where the domain consists of all real numbers? Solution: Because P(x) is true for all real numbers x, the quantification VxP(x) is true. Remark: Generally, an implicit assumption is made that all domains of discourse for quantifiers are nonempty. Note that if the domain is empty, then VxP(x) is true for any propositional function P(x) because there are no elements x in the domain for which P(x) is false. Besides "for all" and "for every," universal quantification can be expressed in many other ways, including "all of," "for each," "given any," "for arbitrary," "for each," and "for any." Remark: It is best to avoid using "for any x" because it is often ambiguous as to whether "any" means "every" or "some." In some cases, "any" is unambiguous, such as when it is used in negatives, for example, "there is not any reason to avoid studying." TABLE 1 Quantifiers . Statement When True? When False? VxP(x) P(x) is true for every x. There is an x for which P(x) is false. 3xP(x) There is an x for which P(x) is true. P(x) is false for every x. 1-35 1.3 Predicates and Quantifiers 35 A statement 'Ix P(x) is false, where P(x) is a propositional function, if and only if P(x) is not always true when x is in the domain. One way to show that P (x) is not always true when x is in the domain is to find a counterexamp le to the statement 'Ix P(x). Note that a single counterexample is all we need to establish that 'Ix P(x) is false. Example 9 illustrates how counterexamp les are used. EXAMPLE 9 Let Q(x) be the statement "x < 2." What is the truth value of the quantification VxQ(x), where the domain consists of all real numbers? Solution: Q(x) is not true for every real number x, because, for instance, Q(3) is false. That is, x = 3 is a counterexample for the statement VxQ(x). Thus VxQ(x) is false. EXAMPLE 10 Suppose that P(x) is "x2 > 0." To show that the statement VxP(x) is false where the uni­ verse of discourse consists of all integers, we give a counterexamp le. We see that x = 0 is a counterexamp le because x2 = 0 when x = 0, so that x2 is not greater than 0 when x = O. .... Looking for counterexamp les to universally quantified statements is an important activity in the study of mathematics, as we will see in subsequent sections of this book. When all the elements in the domain can be listed-say, XI, X2, ... , xn-it follows that the universal quantification 'Ix P(x) is the same as the conjunction because this conjunction is true if and only if P(XI), P(X2), ... , P(xn) are all true. EXAMPLE 11 What is the truth value of 'Ix P(x), where P(x) is the statement "x2 < 10" and the domain consists of the positive integers not exceeding 4? Solution: The statement 'Ix P(x) is the same as the conjunction P(l) 1\ P(2) 1\ P(3) 1\ P(4), because the domain consists of the integers 1,2,3, and 4. Because P(4), which is the statement "42 < 10," is false, it follows that 'Ix P(x) is false. .... EXAMPLE 12 What does the statement VxN(x) mean if N(x) is "Computer x is connected to the network" and the domain consists of all computers on campus? Solution: The statement VxN(x) means that for every computer x on campus, that computer x is connected to the network. This statement can be expressed in English as "Every computer on campus is connected to the network." .... As we have pointed out, specifying the domain is mandatory when quantifiers are used. The truth value of a quantified statement often depends on which elements are in this domain, as Example 13 shows. 36 1 / The Foundations: Logic and Proofs 1-36 EXAMPLE 13 What is the truth value ofYx(x2 2: x) if the domain consists of all real numbers? What is the truth value of this statement if the domain consists of all integers? DEFINITION 2 EXAMPLE 14 Solution: The universal quantification Yx(x2 2: x), where the domain consists of all real num­ bers, is false. For example, (i)2 t i. Note that x2 2: x if and only if x2 -x = x(x -1) � O. Consequently, x2 � x if and only if x ::'S 0 or x � 1. It follows that Yx(x2 2: x) is false if the domain consists of all real numbers (because the inequality is false for all real numbers x with o < x < 1). However, if the domain consists of the integers, Yx(x2 2: x) is true, because there are no integers x with 0 < x < 1. .... THE EXISTENTIAL QUANTIFIER Many mathematical statements assert that there is an element with a certain property. Such statements are expressed using existential quantification. With existential quantification, we form a proposition that is true if and only if P(x) is true for at least one value of x in the domain. The existential quantification of P(x) is the proposition "There exists an element x in the domain such that P (x )." We use the notation 3xP(x) for the existential quantification of P(x). Here 3 is called the existential quantifier. A domain must always be specified when a statement 3xP(x) is used. Furthermore, the meaning of 3xP(x) changes when the domain changes. Without specifying the domain, the statement 3x P (x) has no meaning. The existential quantification 3x P (x) is read as "There is an x such that P(x)," "There is at least one x such that P(x )," or "For some xP(x)." Besides the words "there exists," we can also express existential quantification in many other ways, such as by using the words "for some," "for at least one," or "there is." The meaning of the existential quantifier is summarized in the second row of Table 1. We illustrate the use of the existential quantifier in Examples 14-16. Let P (x) denote the statement "x > 3." What is the truth value of the quantification 3x P (x), where the domain consists of all real numbers? Solution: Because "x > 3" is sometimes true-for instance, when x = 4-the existential quan­ tification of P(x), which is 3x P(x), is true. .... Observe that the statement 3x P (x) is false if and only ifthere is no element x in the domain for which P(x) is true. That is, 3x P(x) is false if and only P(x) is false for every element of the domain. We illustrate this observation in Example 15. EXAM PLE 15 Let Q(x) denote the statement "x = x + I." What is the truth value of the quantification 3x Q(x), where the domain consists of all real numbers? 1-37 1.3 Predicates and Quantifiers 37 Solution: Because Q(x) is false for every real number x, the existential quantification of Q(x), which is 3x Q(x), is false. � Remark: Generally, an implicit assumption is made that all domains of discourse for quantifiers are nonempty. If the domain is empty, then 3xQ(x) is false whenever Q(x) is a propositional function because when the domain is empty, there can be no element in the domain for which Q(x) is true. When all elements in the domain can be listed-say, XI, X2, ... , xn-the existential quan­ tification 3x P (x) is the same as the disjunction because this disjunction is true if and only if at least one of P(XI), P(X2), ... , P(xn) is true. EXAMPLE 16 What is the truth value onxP(x), where P(x) is the statement "x2 > 10" and the universe of discourse consists of the positive integers not exceeding 4? Solution: Because the domain is {l, 2, 3, 4}, the proposition 3x P (x) is the same as the disjunction P(l) v P(2) v P(3) v P(4). Because P(4), which is the statement "42 > 10," is true, it follows that 3xP(x) is true. It is sometimes helpful to think in terms of looping and searching when determining the truth value of a quantification. Suppose that there are n objects in the domain for the variable x. To determine whetherYxP(x) is true, we can loop through all n values ofx to see if P(x) is always true. Ifwe encounter a value x for which P(x) is false, then we have shown that Yx P(x) is false. Otherwise, Yx P(x) is true. To see whether 3x P(x) is true, we loop through the n values of x searching for a value for which P(x) is true. If we find one, then 3x P(x) is true. If we never find such an x, then we have determined that 3xP(x) is false. (Note that this searching procedure does not apply if there are infinitely many values in the domain. However, it is still a useful way of thinking about the truth values of quantifications.) Other Quantifiers We have now introduced universal and existential quantifiers. These are the most important quantifiers in mathematics and computer science. However, there is no limitation on the number of different quantifiers we can define, such as "there are exactly two," "there are no more than three," "there are at least 100," and so on. Of these other quantifiers, the one that is most often seen is the uniqueness quantifier , denoted by 3! or 31. The notation 3!xP(x) [or3lx P(x)] states "There exists a unique x such that P(x) is true." Other phrases for uniqueness quantification include "there is exactly one" and "there is one and only one." Observe that we can use quantifiers and propositional logic to express uniqueness (see Exercise 52 in Section 1.4), so the uniqueness quantifier can be avoided. Generally, it is best to stick with existential and universal quantifiers so that rules of inference for these quantifiers can be used. 38 I / The Foundations: Logic and Proofs Quantifiers with Restricted Domains /-38 An abbreviated notation is often used to restrict the domain of a quantifier. In this notation, a condition a variable must satisfY is included after the quantifier. This is illustrated in Example 17. We will also describe other forms of this notation involving set membership in Section 2.1. EXAMPLE 17 What do the statements '<Ix < 0 (x2 > 0), '<Iy =I- 0 (y3 =I-0), and 3z > 0 (z2 = 2) mean, where the domain in each case consists of the real numbers? Solution: The statement '<Ix < 0 (x2 > 0) states that for every real number x with x < 0, x2 > O. That is, it states "The square of a negative real number is positive." This statement is the same as '<Ix(x < 0 -+ x2 > 0). The statement '<Iy =I- 0 (y3 =I-0) states that for every real number y with y =I- 0 and, we have y3 =I-O. That is, it states "The cube of every nonzero real number is nonzero." Note that this statement is equivalent to '<Iy(y =I- 0 -+ y3 =I-0). Finally, the statement 3z > 0 (z2 = 2) states thatthere exists a real number z with z > 0 such that z2 = 2. That is, it states "There is a positive square root of 2." This statement is equivalent to 3z(z > 0 1\ z2 = 2). <0lIl Note that the restriction of a universal quantification is the same as the universal quantifi­ cation of a conditional statement. For instance, '<Ix < 0 (x2 > 0) is another way of expressing '<Ix(x < 0 -+ x2 > 0). On the other hand, the restriction of an existential quantification is the same as the existential quantification of a conjunction. For instance, 3z > 0 (z2 = 2) is another way of expressing 3z(z > 0 1\ z2 = 2). Precedence of Quantifiers The quantifiers '<I and 3 have higher precedence then all logical operators from propositional calculus. For example, '<Ix P(x) v Q(x) is the disjunction of '<Ix P(x) and Q(x). In other words, it means (VxP(x» v Q(x) rather than '<Ix(P(x) v Q(x». Binding Variables When a quantifier is used on the variable x, we say that this occurrence of the variable is bound. An occurrence of a variable that is not bound by a quantifier or set equal to a particular value is said to be free. All the variables that occur in a propositional function must be bound or set equal to a particular value to turn it into a proposition. This can be done using a combination of universal quantifiers, existential quantifiers, and value assignments. The part of a logical expression to which a quantifier is applied is called the scope of this quantifier. Consequently, a variable is free if it is outside the scope of all quantifiers in the formula that specifies this variable. EXAMPLE 18 In the statement 3x(x + y = 1), the variable x is bound by the existential quantification 3x, but the variable y is free because it is not bound by a quantifier and no value is assigned to this variable. This illustrates that in the statement 3x(x + y = I), x is bound, but y is free. In the statement 3x(P(x) 1\ Q(x» v '<IxR(x), all variables are bound. The scope of the first quantifier, 3x, is the expression P(x) 1\ Q(x) because 3x is applied only to P(x) 1\ Q(x), and not to the rest of the statement. Similarly, the scope of the second quantifier, '<Ix, is the expression R(x). That is, the existential quantifier binds the variable x in P(x) 1\ Q(x) and the universal quantifier '<Ix binds the variable x in R(x). Observe that we could have written our statement /-39 1.3 Predicates and Quantifiers 39 using two different variables x and y, as 3x(P(x) 1\ Q(x)) v VyR(y), because the scopes of the two quantifiers do not overlap. The reader should be aware that in common usage, the same letter is often used to represent variables bound by different quantifiers with scopes that do not overlap. � Logical Equivalences Involvin g Quantifiers In Section 1.2 we introduced the notion of logical equivalences of compound propositions. We can extend this notion to expressions involving predicates and quantifiers. DEFINITION 3 Statements involving predicates and quantifiers are logically equivalent if and only if they have the same truth value no matter which predicates are substituted into these statements and which domain of discourse is used for the variables in these propositional functions. We use the notation S == T to indicate that two statements Sand T involving predicates and quantifiers are logically equivalent. Example 19 illustrates how to show that two statements involving predicates and quantifiers are logically equivalent. EXAMPLE 19 Show that Vx(P(x) 1\ Q(x)) and VxP(x) 1\ VxQ(x) are logically equivalent (where the same domain is used throughout) . This logical equivalence shows that we can distribute a universal quantifier over a conjunction. Furthermore, we can also distribute an existential quantifier over a disjunction. However, we cannot distribute a universal quantifier over a disjunction, nor can we distribute an existential quantifier over a conjunction. (See Exercises 50 and 5l.) Solution: To show that these statements are logically equivalent, we must show that they always take the same truth value, no matter what the predicates P and Q are, and no matter which domain of discourse is used. Suppose we have particular predicates P and Q, with a common domain. We can show that Vx(P(x) 1\ Q(x)) and VxP(x) 1\ VxQ(x) are logically equivalent by doing two things. First, we show that if Vx(P(x) 1\ Q(x)) is true, then VxP(x) 1\ VxQ(x) is true. Second, we show that ifVx P(x) 1\ Vx Q(x) is true, then Vx(P(x) 1\ Q(x)) is true. So, suppose that Vx(P(x) 1\ Q(x)) is true. This means that if a is in the domain, then P(a) 1\ Q(a) is true. Hence, P(a) is true and Q(a) is true. Because P(a) is true and Q(a) is true for every element in the domain, we can conclude that VxP(x) and VxQ(x) are both true. This means that VxP(x) 1\ VxQ(x) is true. Next, suppose that Vx P(x) 1\ Vx Q(x) is true. It follows that Vx P(x) is true and Vx Q(x) is true. Hence, if a is in the domain, then P(a) is true and Q(a) is true [because P(x) and Q(x) are both true for all elements in the domain, there is no conflict using the same value of a here]. It follows that for all a, P(a) 1\ Q(a) is true. It follows that Vx(P(x) 1\ Q(x)) is true. We can now conclude that Vx(P(x) 1\ Q(x)) == VxP(x) 1\ VxQ(x). Negating Quantified Expressions We will often want to consider the negation of a quantified expression. For instance, consider the negation of the statement "Every student in your class has taken a course in calculus." 40 I / The Foundations: Logic and Proofs 1-40 ASS8SSlIeo.ii::! This statement is a universal quantification, namely, VxP(x), where P(x) is the statement "x has taken a course in calculus" and the domain consists of the students in your class. The negation of this statement is "It is not the case that every student in your class has taken a course in calculus." This is equivalent to "There is a student in your class who has not taken a course in calculus." And this is simply the existential quantification of the negation of the original propositional function, namely, 3x -.P(x). This example illustrates the following logical equivalence: -'VxP(x) == 3x -.P(x). To show that -.VxP(x) and 3x (x) are logically equivalent no matter what the propositional function P(x) is and what the domain is, first note that -.Vx P(x) is true if and only if "Ix P(x) is false. Next, note that "Ix P(x) is false if and only there is an element x in the domain for which P(x) is false. This holds if and only ifthere is an element x in the domain for which -.P(x) is true. Finally, note that there is an element x in the domain for which -. P (x) is true if and only if3x -.P(x) is true. Putting these steps together, we can conclude that -.VxP(x) is true if and only if3x -.P(x) is true. It follows that -.VxP(x) and 3x -.P(x) are logically equivalent. Suppose we wish to negate an existential quantification. For instance, consider the proposi­ tion "There is a student in this class who has taken a course in calculus." This is the existential quantification 3xQ(x), where Q(x) is the statement "x has taken a course in calculus." The negation of this statement is the proposition "It is not the case that there is a student in this class who has taken a course in calculus." This is equivalent to "Every student in this class has not taken calculus," which is just the universal quantification of the negation of the original propositional function, or, phrased in the language of quantifiers, "Ix -.Q(x). This example illustrates the equivalence -'3xQ(x) == "Ix -.Q(x). To show that -.3xQ(x) == "Ix -.Q(x) are logically equivalent no matter what Q(x) is and what the domain is, first note that -.3xQ(x) is true if and only if3xQ(x) is false. This is true if and only if no x exists in the domain for which Q(x) is true. Next, note that no x exists in the domain for which Q(x) is true if and only if Q(x) is false for every x in the domain. Finally, note that Q(x) is false for every x in the domain if and only if -.Q(x) is true for all x in the domain, which holds if and only ifVx-.Q(x) is true. Putting these steps together, we see that -.3xQ(x) is true if and only ifVx-.Q(x) is true. We conclude that -.3xQ(x) and "Ix -.Q(x) are logically equivalent. The rules for negations for quantifiers are called De Morgan's laws for quantifiers. These rules are summarized in Table 2. 1-41 EXAMPLE 20 EXAMPLE 21 TABLE 2 De Morgan's Laws for Quantifiers. Negation Equivalent Statement When Is Negation True? -,3xP(x) Yx-,P(x) For every x, P(x) is false. -,YxP(x) 3x-,P(x) There is an x for which P(x) is false. 1.3 Predicates and Quantifiers 41 When False? There is an x for which P(x) is true. P(x) is true for every x. Remark: When the domain of a predicate P(x) consists of n elements, where n is a positive integer, the rules for negating quantified statements are exactly the same as De Morgan's laws discussed in Section 1.2. This is why these rules are called De Morgan's laws for quantifiers. When the domain has n elements Xl, x2,''''Xn, it follows that -,YxP(x) is the same as -,(P(XI) /\ P(X2) /\ ... /\ P(xn)), which is equivalent to -,P(xJ) V -,P(X2) V ... V -,P(xn) by De Morgan's laws, and this is the same as 3x -,P(x). Similarly, -,3x P(x) is the same as -,(P(XI) v P(X2) v ... v P(xn)), which by De Morgan's laws is equivalent to -,P(xJ) /\ -,P(X2) /\ ... /\ -,P(xn), and this is the same as Yx-,P(x). We illustrate the negation of quantified statements in Examples 20 and 21. What are the negations of the statements "There is an honest politician" and "All Americans eat cheeseburgers"? Solution: Let H (x) denote "x is honest." Then the statement "There is an honest politician" is represented by 3xH (x), where the domain consists of all politicians. The negation of this statement is -,3xH(x), which is equivalent to Yx-,H(x). This negation can be expressed as "Every politician is dishonest." (Note: In English, the statement "All politicians are not honest" is ambiguous. In common usage, this statement often means "Not all politicians are honest." Consequently, we do not use this statement to express this negation.) Let C (x) denote "x eats cheeseburgers." Then the statement "All Americans eat cheese­ burgers" is represented by YxC (x), where the domain consists of all Americans. The negation of this statement is -,YxC(x), which is equivalent to 3x-,C(x). This negation can be expressed in several different ways, including "Some American does not eat cheeseburgers" and "There is an American who does not eat cheeseburgers." .... What are the negations of the statements Yx(x2 > x) and 3x(x2 = 2)? Solution: The negation of Yx(x2 > x) is the statement -,Yx(x2 > x), which is equivalent to 3x-,(x2 > x). This can be rewritten as 3x(x2 :s: x). The negation of3x(x2 = 2) is the statement -,3x(x2 = 2), which is equivalent to Yx-,(x2 = 2). This can be rewritten as Yx(x2 =f. 2). The truth values of these statements depend on the domain. .... We use De Morgan's laws for quantifiers in Example 22. EXAMPLE 22 Show that -,Yx(P(x) --+ Q(x)) and 3x(P(x) /\ -,Q(x)) are logically equivalent. Solution: By De Morgan's law for universal quantifiers, we know that -,Yx(P(x) --+ Q(x)) and 3x(-,(P(x) --+ Q(x))) are logically equivalent. By the fifth logical equivalence in Table 7 in Section 1.2, we know that -,(P(x) --+ Q(x)) and P(x) /\ -,Q(x) are logically equivalent for every x. Because we can substitute one logically equivalent expression for another in a logical equivalence, it follows that -'Yx(P(x) --+ Q(x)) and 3x(P(x) /\ -,Q(x)) are logically equivalent. .... 42 1 / The Foundations: Logic and Proofs Translatin g from English into Logical Expressions 1-42 Translating sentences in English (or other natural languages ) into logical expressions is a crucial task in mathematics, logic programming, artificial intelligence, software engineering, and many other disciplines. We began studying this topic in Section 1.1, where we used propositions to express sentences in logical expressions. In that discussion, we purposely avoided sentences whose translations required predicates and quantifiers. Translating from English to logical ex­ pressions becomes even more complex when quantifiers are needed. Furthermore, there can be many ways to translate a particular sentence. (As a consequence, there is no "cookbook" approach that can be followed step by step.) We will use some examples to illustrate how to translate sentences from English into logical expressions. The goal in this translation is to pro­ duce simple and useful logical expressions. In this section, we restrict ourselves to sentences that can be translated into logical expressions using a single quantifier; in the next section, we will look at more complicated sentences that require multiple quantifiers. EXAMPLE 23 Express the statement "Every student in this class has studied calculus" using predicates and quantifiers. Solution: First, we rewrite the statement so that we can clearly identify the appropriate quantifiers to use. Doing so, we obtain: "For every student in this class, that student has studied calculus." Next, we introduce a variable x so that our statement becomes "For every student x in this class, x has studied calculus." Continuing, we introduce C(x), which is the statement "x has studied calculus." Consequently, if the domain for x consists of the students in the class, we can translate our statement as 'v'xC(x). However, there are other correct approaches; different domains of discourse and other predicates can be used. The approach we select depends on the subsequent reasoning we want to carry out. For example, we may be interested in a wider group of people than only those in this class. Ifwe change the domain to consist of all people, we will need to express our statement as "For every person x, if person x is a student in this class then x has studied calculus." If S(x) represents the statement that person x is in this class, we see that our statement can be expressed as 'v'x(S(x) -+ C(x)). [Caution! Our statement cannot be expressed as 'v'x(S(x) 1\ C(x)) because this statement says that all people are students in this class and have studied calculus!] Finally, when we are interested in the background of people in subjects besides calculus, we may prefer to use the two-variable quantifier Q(x, y) for the statement "student x has studied subject y." Then we would replace C(x) by Q(x, calculus) in both approaches to obtain 'v'xQ(x, calculus) or'v'x(S(x) -+ Q(x, calculus)). .... In Example 23 we displayed different approaches for expressing the same statement using predicates and quantifiers. However, we should always adopt the simplest approach that is adequate for use in subsequent reasoning. EXAMPLE 24 Express the statements "Some student in this class has visited Mexico" and "Every student in this class has visited either Canada or Mexico" using predicates and quantifiers. 1-43 EXAMPLE 25 Exam=� 1.3 Predicates and Quantifiers 43 Solution: The statement "Some student in this class has visited Mexico" means that "There is a student in this class with the property that the student has visited Mexico." We can introduce a variable x, so that our statement becomes "There is a student x in this class having the property that x has visited Mexico." We introduce M(x), which is the statement "x has visited Mexico." If the domain for x consists of the students in this class, we can translate this first statement as 3xM(x). However, if we are interested in people other than those in this class, we look at the statement a little differently. Our statement can be expressed as • "There is a person x having the properties that x is a student in this class and x has visited Mexico." In this case, the domain for the variable x consists of all people. We introduce S (x) to represent "x is a student in this class." Our solution becomes 3x(S(x) /\ M(x)) because the statement is that there is a person x who is a student in this class and who has visited Mexico. [Caution! Our statement cannot be expressed as 3x(S(x) -+ M(x)), which is true when there is someone not in the class because, in that case, for such a person x, S(x) -+ M(x) becomes either F -+ Tor F -+ F, both of which are true.] Similarly, the second statement can be expressed as "For every x in this class, x has the property that x has visited Mexico or x has visited Canada." (Note that we are assuming the inclusive, rather than the exclusive, or here.) We let C(x) be "x has visited Canada." Following our earlier reasoning, we see that if the domain for x consists of the students in this class, this second statement can be expressed as 'v'x(C(x) v M(x)). However, if the domain for x consists of all people, our statement can be expressed as "For every person x, if x is a student in this class, then x has visited Mexico or x has visited Canada." In this case, the statement can be expressed as 'v'x(S(x) -+ (C(x) v M(x))). Instead of using M (x) and C (x) to represent that x has visited Mexico and x has visited Canada, respectively, we could use a two-place predicate V (x. y) to represent "x has visited country y." In this case, V (x. Mexico) and V (x. Canada) would have the same meaning as M (x) and C (x) and could replace them in our answers. If we are working with many statements that involve people visiting different countries, we might prefer to use this two-variable approach. Otherwise, for simplicity, we would stick with the one-variable predicates M(x) and C(x). .... Using Quantifiers in System Specifications In Section 1.1 we used propositions to represent system specifications. However, many system specifications involve predicates and quantifications. This is illustrated in Example 25. Use predicates and quantifiers to express the system specifications "Every mail message larger than one megabyte will be compressed" and "If a user is active, at least one network link will be available." Solution: Let S(m, y) be "Mail message m is larger than y megabytes," where the variable x has the domain of all mail messages and the variable y is a positive real number, and let C (m) denote 44 1 / The Foundations: Logic and Proofs 1-44 "Mail message m will be compressed." Then the specification "Every mail message larger than one megabyte will be compressed" can be represented as Vm(S(m, 1) --+ C(m)). Let A(u) represent "User u is active," where the variable u has the domain of all users, let S(n, x) denote "Network link n is in state x," where n has the domain of all network links and x has the domain of all possible states for a network link. Then the specification "If a user is active, at least one network link will be available" can be represented by 3uA(u) --+ 3nS(n, available). .... Examples from Lewis Carroll Lewis Carroll (really C. L. Dodgson writing under a pseudonym), the author of Alice in Wonder­ land, is also the author of several works on symbolic logic. His books contain many examples of reasoning using quantifiers. Examples 26 and 27 come from his book Symbolic Logic; other examples from that book are given in the exercises at the end of this section. These examples illustrate how quantifiers are used to express various types of statements. EXAMPL E 26 Consider these statements. The first two are called premises and the third is called the conclusion. unkS� The entire set is called an argument. "All lions are fierce." "Some lions do not drink coffee." "Some fierce creatures do not drink coffee." (In Section 1.5 we will discuss the issue of determining whether the conclusion is a valid conse­ quence of the premises. In this example, it is.) Let P(x), Q(x), and R(x) be the statements "x is a lion," "x is fierce," and "x drinks coffee," respectively. Assuming that the domain consists of all creatures, express the statements in the argument using quantifiers and P(x), Q(x), and R(x). Solution: We can express these statements as: Vx(P(x) --+ Q(x)). 3x(P(x) /\ --.R(x)). 3x(Q(x) /\ --.R(x )). Notice that the second statement cannot be written as 3x(P(x) --+ --.R(x)). The reason is that P(x) --+ --.R(x) is true whenever x is not a lion, so that 3x(P(x) --+ --.R(x)) is true as long as there is at least one creature that is not a lion, even if every lion drinks coffee. Similarly, the third statement cannot be written as 3x(Q(x) --+ --.R(x )). CHARLES LUTWIDGE DODGSON (1832-1898) We know Charles Dodgson as Lewis Carroll-the pseudonym he used in his writings on logic. Dodgson, the son of a clergyman, was the third of 11 chil­ dren, all of whom stuttered. He was uncomfortable in the company of adults and is said to have spoken without stuttering only to young girls, many of whom he entertained, corresponded with, and photographed (sometimes in poses that today would be considered inappropriate ). Although attracted to young girls, he was extremely puritanical and religious. His friendship with the three young daughters of Dean Liddell led to his writing Alice in Wonderland, which brought him money and fame. Dodgson graduated from Oxford in 1854 and obtained his master of arts degree in 1857. He was appointed lecturer in mathematics at Christ Church College, Oxford, in 1855. He was ordained in the Church of England in 1861 but never practiced his ministry. His writings include articles and books on geometry, determinants, and the mathematics of tournaments and elections. (He also used the pseudonym Lewis Carroll for his many works on recreational logic.) 1-45 1.3 Predicates and Quantifiers 45 EXAMPLE 27 Consider these statements, of which the first three are premises and the fourth is a valid conclusion. linkS� EXAMPLE 28 "All hummingbirds are richly colored." "No large birds live on honey." • "Birds that do not live on honey are dull in color." "Hummingbirds are small." Let P(x), Q(x), R(x), and S(x) be the statements "x is a hummingbir d," "x is large," "x lives on honey," and "x is richly colored," respective ly. Assuming that the domain consists of all birds, express the statements in the argument using quantifiers and P(x), Q(x), R(x), and Sex). Solution: We can express the statements in the argument as Yx(P(x) ---+ Sex»�. -.3x(Q(x) 1\ R(x». Yx( -.R(x) ---+ -,S(x ». Yx(P(x) ---+ -.Q(x». (Note we have assumed that "small" is the same as "not large" and that "dull in color" is the same as "not richly colored." To show that the fourth statement is a valid conclusion of the first three, we need to use rules of inference that will be discussed in Section 1.5.) .... Logic Programming An important type of programming language is designed to reason using the rules of predicate logic. Prolog (from Programming in Logic), developed in the 1970s by computer scientists working in the area of artificial intelligence, is an example of such a language. Prolog programs include a set of declarations consisting of two types of statements, Prolog facts and Prolog rules. Prolog facts define predicates by specifying the elements that satisfy these predicates. Prolog rules are used to define new predicates using those already defined by Prolog facts. Example 28 illustrates these notions. Consider a Prolog program given facts telling it the instructor of each class and in which classes students are enrolled. The program uses these facts to answer queries concerning the professors who teach particular students. Such a program could use the predicates instructor(p, c) and enrolled(s, c) to represent that professor p is the instructor of course c and that student s is enrolled in course c, respectively. For example, the Prolog facts in such a program might include: instructor (chan,math2 73) instructor(patel,ee222) instr uctor(grossman,cs301) enrolled (kevin, math273) enrolled(juana,ee222) enrolled(juana,cs301) enrolled(kiko,math273) enrolled(kiko,cs301) (Lowercase letters have been used for entries because Prolog considers names beginning with an uppercase letter to be variables.) 46 I / The Foundations: Logic and Proofs 1-46 A new predicate teaches(p, s), representing that professor p teaches student s, can be defined using the Prolog rule teaches (P,S) :-instructor (P,C) , enrolled(S,C) which means that teaches(p, s) is true if there exists a class c such that professor p is the instructor of class c and student s is enrolled in class c. (Note that a comma is used to represent a conjunction of predicates in Prolog. Similarly, a semicolon is used to represent a disjunction of predicates.) Prolog answers queries using the facts and rules it is given. For example, using the facts and rules listed, the query ?enrolled (kevin,math273) produces the response yes because the fact enrolled (kevin, math273) was provided as input. The query ?enrolled(X,math273) produces the response kevin kiko To produce this response, Prolog determines all possible values of X for which enrolled(X , math273) has been included as a Prolog fact. Similarly, to find all the professors who are instructors in classes being taken by Juana, we use the query ?teaches (X,juana) This query returns Exercises patel grossman 1. Let P(x) denote the statement "x :s 4." What are the truth values? a) P(O) b) P(4) c) P(6) 2. Let P(x) be the statement "the word x contains the letter a." What are the truth values? a) P(orange) b) P(lemon) c) P(true) d) P(false) 3. Let Q(x, y) denote the statement "x is the capital of y." What are these truth values? a) Q(Denver, Colorado) b) Q(Detroit, Michigan) c) Q(Massachusetts, Boston) d) Q(New York, New York) 4. State the value of x after the statement if P (x) then x := 1 is executed, where P(x) is the statement "x > I," if the value of x when this statement is reached is a) x = O. c) x = 2. b) x = 1. 5. Let P (x) be the statement "x spends more than five hours every weekday in class," where the domain for x consists of all students. Express each of these quantifications in English. 1-47 a) 3xP(x) e) 3x ..... P(x) b) VxP(x) d) "Ix ..... P(x) 6. Let N(x) be the statement "x has visited North Dakota," where the domain consists of the students in your school. Express each of these quantifications in English. a) 3xN(x) b) VxN(x) e) ..... 3xN(x) d) 3x ..... N(x) e) ..... VxN(x) t) Vx ..... N(x) 7. Translate these statements into English, where C(x) is "x is a comedian" and F(x) is "x is funny" and the domain consists of all people. a) Vx(C(x) -+ F(x» e) 3x(C(x) -+ F(x» b) Vx(C(x) /\ F(x» d) 3x(C(x) /\ F(x» 8. Translate these statements into English, where R(x) is "x is a rabbit" and H (x) is "x hops" and the domain consists of all animals. a) Vx(R(x) -+ H(x» e) 3x(R(x) -+ H(x» b) Vx(R(x) /\ H(x» d) 3x(R(x) /\ H(x» 9. Let P(x) be the statement "x can speak Russian" and let Q(x) be the statement "x knows the computer language C++." Express each of these sentences in terms of P (x), Q(x), quantifiers, and logical connectives. The domain for quantifiers consists of all students at your school. a) There is a student at your school who can speak Rus­ sian and who knows C++. b) There is a student at your school who can speak Rus­ sian but who doesn't know C++. e) Every student at your school either can speak Russian or knows C++. d) No student at your school can speak Russian or knows C++. 10. Let C(x) be the statement "x has a cat," let D(x) be the statement "x has a dog," and let F(x) be the statement "x has a ferret." Express each of these statements in terms of C (x), D(x), F (x), quantifiers, and logical connectives. Let the domain consist of all students in your class. a) A student in your class has a cat, a dog, and a ferret. b) All students in your class have a cat, a dog, or a ferret. e) Some student in your class has a cat and a ferret, but not a dog. d) No student in your class has a cat, a dog, and a ferret. e) For each of the three animals, cats, dogs, and ferrets, there is a student in your class who has one of these animals as a pet. 11. Let P(x) be the statement "x = x2." If the domain con­ sists ofthe integers, what are the truth values? a) P(O) d) P(-l) b) P(l) e) 3xP(x) e) P(2) t) VxP(x) 12. Let Q(x) be the statement "x + 1 > 2x." If the domain consists of all integers, what are these truth values? a) Q(O) b) Q(-I) e) Q(l) d) 3xQ(x) e) VxQ(x) t) 3x ..... Q(x) g) Vx ..... Q(x) 13. Determine the truth value of each of these statements if the domain consists of all integers. 1.3 Predicates and Quantifiers 47 a) Vn(n + 1 > n) e) 3n(n = -n) b) 3n(2n = 3n) d) Vn(n2 :::: n) 14. Determine the truth value of each of these statements if the domain consists of all real numbers. a) 3x(x3 = -1) b) 3X(x4 < x2) e) VX«_X)2 = x2) d) Vx(2x > x) 15. Determine the truth value of each of these statements if the domain for all variables consists of all integers. a) Vn(n2 :::: 0) b) 3n(n2 = 2) e) Vn(n2 :::: n) d) 3n(n2 < 0) 16. Determine the truth value of each of these statements if the domain of each variable consists of all real numbers. a) 3x(x2 = 2) b) 3x(x2 = -1) e) Vx(x2 + 2 :::: 1) d) Vx(x2 =1= x) 17. Suppose that the domain of the propositional function P(x) consists of the integers 0, 1,2,3, and 4. Write out each of these propositions using disjunctions, conjunc­ tions, and negations. a) 3xP(x) d) Vx ..... P(x) b) VxP(x) e) ..... 3xP(x) e) 3x ..... P(x) t) ..... VxP(x) 18. Suppose that the domain of the propositional function P(x) consists of the integers -2, -1,0,1, and 2. Write out each of these propositions using disjunctions, con­ junctions, and negations. a) 3xP(x) b) VxP(x) d) Vx ..... P(x) e) ..... 3xP(x) e) 3x ..... P(x) t) ..... VxP(x) 19. Suppose that the domain of the propositional function P(x) consists of the integers 1,2,3,4, and 5. Express these statements without using quantifiers, instead using only negations, disjunctions, and conjunctions. a) 3xP(x) b) VxP(x) e) ..... 3xP(x) d) ..... VxP(x) e) Vx« x =1= 3) -+ P(x» v 3x ..... P(x) 20. Suppose that the domain of the propositional function P(x) consists of -5, -3, -1,1,3, and 5. Express these statements without using quantifiers, instead using only negations, disjunctions, and conjunctions. a) 3xP(x) b) VxP(x) e) Vx« x =1= 1) -+ P(x» d) 3x« x :::: 0) /\ P(x» e) 3x( ..... P(x» /\ Vx« x < 0) -+ P(x» 21. For each of these statements find a domain for which the statement is true and a domain for which the statement is false. a) Everyone is studying discrete mathematics. b) Everyone is older than 21 years. e) Every two people have the same mother. d) No two different people have the same grandmother. 22. For each of these statements find a domain for which the statement is true and a domain for which the statement is false. a) Everyone speaks Hindi. b) There is someone older than 21 years. 48 1 / The Foundations: Logic and Proofs e) Every two people have the same first name. d) Someone knows more than two other people. 23. Translate in two ways each of these statements into logi­ cal expressions using predicates, quantifiers, and logical connective s. First, let the domain consist of the students in your class and second, let it consist of all people. a) Someone in your class can speak Hindi. b) Everyone in your class is friendly. e) There is a person in your class who was not born in California. d) A student in your class has been in a movie. e) No student in your class has taken a course in logic programming. 24. Translate in two ways each of these statements into logi­ cal expressions using predicates, quantifiers, and logical connectives. First, let the domain consist of the students in your class and second, let it consist of all people. a) Everyone in your class has a cellular phone. b) Somebody in your class has seen a foreign movie. e) There is a person in your class who cannot swim. d) All students in your class can solve quadratic equations. e) Some student in your class does not want to be rich. 25. Translate each of these statements into logical expressions using predicates, quantifiers, and logical connectives. a) No one is perfect. b) Not everyone is perfect. e) All your friends are perfect. d) At least one of your friends is perfect. e) Everyone is your friend and is perfect. f) Not everybody is your friend or someone is not perfect. 26. Translate each ofthese statements into logical expressions in three different ways by varying the domain and by using predicates with one and with two variables. a) Someone in your school has visited Uzbekistan. b) Everyone in your class has studied calculus and C++. e) No one in your school owns both a bicycle and a motorcycle. d) There is a person in your school who is not happy. e) Everyone in your school was born in the twentieth century. 27. Translate each ofthese statements into logical expressions in three different ways by varying the domain and by using predicates with one and with two variables. a) A student in your school has lived in Vietnam. b) There is a student in your school who cannot speak Hindi. e) A student in your school knows Java, Prolog, and C++. d) Everyone in your class enjoys Thai food. e) Someone in your class does not play hockey. 28. Translate each ofthese statements into logical expressions using predicates, quantifiers, and logical connectives. a) Something is not in the correct place. 1-48 b) All tools are in the correct place and are in excellent condition. e) Everything is in the correct place and in excellent condition. d) Nothing is in the correct place and is in excellent condition. e) One of your tools is not in the correct place, but it is in excellent condition. 29. Express each of these statements using logical operators, predicates, and quantifiers. a) Some propositions are tautologies. b) The negation ofa contradiction is a tautology. e) The disjunction of two contingencies can be a tautology. d) The conjunction of two tautologies is a tautology. 30. Suppose the domain of the propositional function P (x, y) consists of pairs x and y, where x is I, 2, or 3 and y is 1,2, or 3. Write out these propositions using disjunctions and conjunctions. a) 3x P(x, 3) e) 3y-'P(2, y) b) 'v'yP(I,y) d) 'v'x -'P(x, 2) 31. Suppose that the domain of Q(x, y, z) consists of triples x, y, z, where x = 0, I, or 2, y = 0 or I, and z = 0 or 1. Write out these propositions using disjunctions and con­ junctions. a) 'v'yQ(O, y, 0) e) 3z-'Q(O, 0, z) b) 3xQ(x, I, I) d) 3x-'Q(x,O,I) 32. Express each of these statements using quantifiers. Then form the negation of the statement so that no negation is to the left of a quantifier. Next, express the negation in simple English. (Do not simply use the words "It is not the case that.") a) All dogs have fleas. b) There is a horse that can add. e) Every koala can climb. d) No monkey can speak French. e) There exists a pig that can swim and catch fish. 33. Express each of these statements using quantifiers. Then form the negation of the statement, so that no negation is to the left of a quantifier. Next, express the negation in simple English. (Do not simply use the words "It is not the case that.") a) Some old dogs can learn new tricks. b) No rabbit knows calculus. e) Every bird can fly. d) There is no dog that can talk. e) There is no one in this class who knows French and Russian. 34. Express the negation of these propositions using quanti­ fiers, and then express the negation in English. a) Some drivers do not obey the speed limit. b) All Swedish movies are serious. e) No one can keep a secret. d) There is someone in this class who does not have a good attitude. /-49 35. Find a counterexample, if possible, to these universally quantified statements, where the domain for all variables consists of all integers. a) \lx(x2 ::: x) b) \lx(x > 0 v x < 0) c) \lx(x = 1) 36. Find a counterexamp le, if possible, to these universally quantified statements, where the domain for all variables consists of all real numbers. a) \lx(x2 ¥ x) c) \lx(lxl > 0) b) \lx(x2 ¥ 2) 37. Express each of these statements using predicates and quantifiers. a) A passenger on an airline qualifies as an elite flyer if the passenger flies more than 25,000 miles in a year or takes more than 25 flights during that year. b) A man qualifies for the marathon if his best previous time is less than 3 hours and a woman qualifies for the marathon if her best previous time is less than 3.5 hours. c) A student must take at least 60 course hours, or at least 45 course hours and write a master's thesis, and receive a grade no lower than a B in all required courses, to receive a master's degree. d) There is a student who has taken more than 21 credit hours in a semester and received all A's. Exercises 38-42 deal with the translation between system specification and logical expressions involving quantifiers. 38. Translate these system specifications into English where the predicate S(x, y) is "x is in state y" and where the do­ main for x and y consists of all systems and all possible states, respectively. a) 3xS(x, open) b) \lx(S(x, malfunctioning) v Sex, diagnostic» c) 3xS(x, open) v 3xS(x, diagnostic) d) 3x...,S(x, available) e) \lx...,S(x, working) 39. Translate these specifications into English where F(p) is "Printer p is out of service," B (p) is "Printer p is busy," L(j) is "Print job j is lost," and Q(j) is "Print job j is queued." a) 3p(F(p) 1\ B(p» -+ 3jL(j) b) \lpB(p) -+ 3jQ(j) c) 3j(Q(j) 1\ L(j» -+ 3pF(p) d) (\lpB(p) 1\ \ljQ(j» -+ 3jL(j) 40. Express each of these system specifications using predi­ cates, quantifiers, and logical connectives. a) When there is less than 30 megabytes free on the hard disk, a warning message is sent to all users. b) No directories in the file system can be opened and no files can be closed when system errors have been detected. c) The file system cannot be backed up if there is a user currently logged on. 1.3 Predicates and Quantifiers 49 d) Video on demand can be delivered when there are at least 8 megabytes of memory available and the con­ nection speed is at least 56 kilobits per second. 41. Express each of these system specifications using predi­ cates, quantifiers, and logical connectives. a) At least one mail message, among the nonempty set of messages, can be saved if there is a disk with more than 10 kilobytes of free space. b) Whenever there is an active alert, all queued messages are transmitted. c) The diagnostic monitor tracks the status of all systems except the main console. d) Each participant on the conference call whom the host of the call did not put on a special list was billed. 42. Express each of these system specifications using predi­ cates, quantifiers, and logical connectives. a) Every user has access to an electronic mailbox. b) The system mailbox can be accessed by everyone in the group if the file system is locked. c) The firewall is in a diagnostic state only if the proxy server is in a diagnostic state. d) At least one router is functioning normally if the throughput is between 100 kbps and 500 kbps and the proxy server is not in diagnostic mode. 43. Determine whether \lx(P(x) -+ Q(x» and \lxP(x) -+ \Ix Q(x) are logically equivalent. JustifY your answer. 44. Determine whether \lx(P(x) � Q(x» and \Ix P(x) � \lxQ(x) are logically equivalent. JustifY your answer. 45. Show that 3x(P(x) v Q(x» and 3xP(x) v 3xQ(x) are logically equivalent. Exercises 46-49 establish rules for null quantification that we can use when a quantified variable does not appear in part of a statement. 46. Establish these logical equivalences, where x does not occur as a free variable in A. Assume that the domain is nonempty. a) (\lxP(x» v A == \lx(P(x) v A) b) (3xP(x» v A == 3x(P(x) v A) 47. Establish these logical equivalences, where x does not occur as a free variable in A. Assume that the domain is nonempty. a) (\IxP(x» 1\ A == \lx(P(x) 1\ A) b) (3xP(x» 1\ A == 3x(P(x) 1\ A) 48. Establish these logical equivalences, where x does not occur as a free variable in A. Assume that the domain is nonempty. a) \lx(A -+ P(x» == A -+ \lxP(x) b) 3x(A -+ P(x» == A -+ 3xP(x) 49. Establish these logical equivalences, where x does not occur as a free variable in A. Assume that the domain is nonempty. a) \lx(P(x) -+ A) == 3xP(x) -+ A b) 3x(P(x) -+ A) == \lxP(x) -+ A 50 1 I The Foundations: Logic and Proofs 50. Show that "Ix P(x) V VxQ(x) and Vx(P(x) v Q(x)) are not logically equivalent. 51. Show that 3xP(x) 1\ 3xQ(x) and 3x(P(x) 1\ Q(x» are not logically equivalent. 52. As mentioned in the text, the notation 3!x P(x) denotes "There exists a unique x such that P(x) is true." If the domain consists of all integers, what are the truth values of these statements? a) 3!x(x > 1) b) 3!x(x2 = 1) c) 3!x(x + 3 = 2x) d) 3!x(x = x + 1) 53. What are the truth values of these statements? a) 3!xP(x) -+ 3xP(x) b) VxP(x) -+ 3!xP(x) c) 3!x-'P(x) -+ -,VxP(x) 54. Write out 3!x P(x), where the domain consists ofthe inte­ gers 1,2, and 3, in terms of negations, conjunctions, and disjunctions. 55. Given the Prolog facts in Example 28, what would Prolog return given these queries? a) ?instructor (chan,math273) b) ?instructor(patel,cs301) c) ?enrolled(X,cs301) d) ?enrolled(kiko,Y) e) ?teaches (grossman,Y) 56. Given the Prolog facts in Example 28, what would Prolog return when given these queries? a) ?enrolled (kevin ,ee222) b) ?enrolled(kiko,math273) c) ?instructor(grossman,X) d) ?instructor (X, cs301) e) ?teaches (X,kevin) 57. Suppose that Prolog facts are used to define the predicates mother(M, Y) andfather(F, X), which represent that M is the mother of Y and F is the father of X, respectively. Give a Prolog rule to define the predicate sibling(X, y), which represents that X and Yare siblings (that is, have the same mother and the same father). 58. Suppose that Prolog facts are used to define the pred­ icates mother(M, Y) and father(F, X), which repre­ sent that M is the mother of Y and F is the father of X, respective ly. Give a Prolog rule to define the predicate grandfather(X, y), which represents that X is the grand­ father of Y. [Hint: You can write a disjunction in Prolog 1.4 Nested Quantifiers Introd udion 1-50 either by using a semicolon to separate predicates or by putting these predicates on separate lines.] Exercises 59-62 are based on questions found in the book Symbolic Logic by Lewis Carroll. 59. Let P(x), Q(x), and R(x) be the statements "x is a profes­ sor," "x is ignorant," and "x is vain," respectively. Express each of these statements using quantifiers; logical con­ nectives; and P(x), Q(x), and R(x), where the domain consists of all people. a) No professors are ignorant. b) All ignorant people are vain. c) No professors are vain. d) Does (c) follow from (a) and (b)? 60. Let P(x), Q(x), and R(x) be the statements "x is a clear explanation," "x is satisfactory," and "x is an excuse," respectively. Suppose that the domain for x consists of all English text. Express each of these statements using quan­ tifiers, logical connectives, and P(x), Q(x), and R(x). a) All clear explanations are satisfactory. b) Some excuses are unsatisfactory. c) Some excuses are not clear explanati ons. *d) Does (c) follow from (a) and (b)? 61. Let P(x), Q(x), R(x), and S(x) be the statements "x is a baby," "x is logical," "x is able to manage a crocodile," and "x is despised," respectively. Suppose that the domain consists of all people. Express each of these statements using quantifiers; logical connectives; and P(x), Q(x), R(x), and S(x). a) Babies are illogical. b) Nobody is despised who can manage a crocodile. c) Illogical persons are despised. d) Babies cannot manage crocodiles. >te) Does (d) follow from (a), (b), and (c)? If not, is there a correct conclusion? 62. Let P(x), Q(x), R(x), and S(x) be the statements "x is a duck," "x is one of my poultry," "x is an officer," and "x is willing to waltz," respectively. Express each of these statements using quantifiers; logical connectives; and P(x), Q(x), R(x), and S(x). a) No ducks are willing to waltz. b) No officers ever decline to waltz. c) All my poultry are ducks. d) My poultry are not officers. *e) Does (d) follow from (a), (b), and (c)? If not, is there a correct conclusion? In Section 1.3 we defined the existential and universal quantifiers and showed how they can be used to represent mathematical statements. We also explained how they can be used to translate 1-51 1.4 Nested Quantifiers 51 English sentences into logical expressions. In this section we will study nested quantifiers . Two quantifiers are nested if one is within the scope of the other, such as Vx3y(x + y = 0). Note that everything within the scope of a quantifier can be thought of as a propositional function. For example, Vx3y(x + y = 0) is the same thing as VxQ(x), where Q(x) is 3yP(x,y), where P(x,y) is x + y = O. Nested quantifiers commonly occur in mathematics and computer science. Although nested quantifiers can sometimes be difficult to understand, the rules we have already studied in Section 1.3 can help us use them. To understand these statements involving many quantifiers, we need to unravel what the quantifiers and predicates that appear mean. This is illustrated in Examples I and 2. EXAMPLE 1 Assume that the domain for the variables x and y consists of all real numbers. The statement VxVy(x + y = y +x) says that x + y = y + x for all real numbers x and y. This is the commutative law for addition of real numbers. Likewise, the statement Vx3y(x + y = 0) says that for every real number x there is a real number y such that x + y = O. This states that every real number has an additive inverse. Similarly, the statement VxVyVz(x + (y + z) = (x + y) + z) is the associative law for addition of real numbers. EXAMPLE 2 Translate into English the statement VxVy«x > 0) /\ (y < 0) -+ (xy < 0», where the domain for both variables consists of all real numbers. Solution: This statement says that for every real number x and for every real number y, if x > 0 andy < 0, thenxy < O. That is, this statement says that for real numbers x andy, if x is positive and y is negative, then xy is negative. This can be stated more succinctly as "The product of a positive real number and a negative real number is always a negative real number." <III THINKING OF QUANTIFICATION AS LOOPS In working with quantifications of more than one variable, it is sometimes helpful to think in terms of nested loops. (Of course, if there are infinitely many elements in the domain of some variable, we cannot actually loop through all values. Nevertheless, this way of thinking is helpful in understanding nested quamifiers .) For example, to see whether VxVyP(x, y) is true, we loop through the values for x, and for each x we loop through the values for y. If we find that P(x, y) is true for all values for x and y, we have determined that VxVy P(x, y) is true. If we ever hit a value x for which we hit a value y for which P(x, y) is false, we have shown that VxVy P(x, y) is false. Similarly, to determine whether Vx3yP(x, y) is true, we loop through the values for x. For each x we loop through the values for y until we find a y for which P(x, y) is true. If for 52 I / The Foundations: Logic and Proofs 1-52 every x we hit such a y, then 'v'x3yP(x, y) is true; if for some x we never hit such a y, then 'v'x3y P(x, y) is false. To see whether 3x 'v' y P (x, y) is true, we loop through the values for x until we find an x for which P (x, y) is always true when we loop through all values for y. Once we find such an x, we know that 3x'v'yP(x, y) is true. Ifwe never hit such anx, then we know that 3x'v'yP(x, y) is false. Finally, to see whether 3x3yP(x, y) is true, we loop through the values for x, where for each x we loop through the values for y until we hit an x for which we hit a y for which P (x , y) is true. The statement 3x3yP( x, y) is false only if we never hit an x for which we hit a y such that P(x, y) is true. The Order of Quantifiers Many mathematical statements involve multiple quantifications of propositional functions in­ volving more than one variable. It is important to note that the order of the quantifiers is important, unless all the quantifiers are universal quantifiers or all are existential quantifiers. These remarks are illustrated by Examples 3-5. EXAMPLE 3 Let P(x, y) be the statement "x + y = Y + x." What are the truth values ofthe quantifications 'v'x'v'yP(x, y) and 'v'y'v'x P(x, y) where the domain for all variables consists of all real numbers? Solution: The quantification 'v'x'v'yP(x, y) Extra� Examples � denotes the proposition "For all real numbers x, for all real numbers y, x + y = y + x." Because P (x, y) is true for all real numbers x and y (it is the commutative law for addition, which is an axiom for the real numbers-see Appendix 1), the proposition 'v'x'v'yP(x,y) is true. Note that the statement 'v'y'v'x P(x, y) says "For all real numbers y, for all real numbers x, x + y = y + x." This has the same meaning as the statement as "For all real numbers x, for all realnumbers y,x + y = y + x." That is, 'v'x'v'yP(x, y)and'v'y'v' xP(x, y)have the same meaning, and both are true. This illustrates the principle that the order of nested universal quantifiers in a statement without other quantifiers can be changed without changing the meaning of the quantified statement. .... EXAMPLE 4 Let Q(x, y) denote "x + y = 0." What are the truth values of the quantifications 3y'v'xQ(x, y) and 'v'x3yQ(x, y), where the domain for all variables consists of all real numbers? Solution: The quantification 3y'v'x Q(x, y) denotes the proposition "There is a real number y such that for every real number x, Q(x, y)." No matter what value of y is chosen, there is only one value of x for which x + y = O. Because there is no real number y such that x + y = 0 for all real numbers x, the statement 3y'v'x Q(x, y) is false. /-53 1.4 Nested Quantifiers S3 TABLE 1 Quantifications of Two Variables. Statement When True? When False? VxVyP(x,y) P(x, y) is true for every pair x, y. There is a pair x, y for VyVxP(x, y) which P(x, y) is false. Vx3yP(x,y) For every x there is a y for There is an x such that which P(x, y) is true. P(x, y) is false for every y. 3xVyP(x,y) There is an x for which P(x, y) For every x there is a y for is true for every y. which P(x, y) is false. 3x3yP(x,y) There is a pair x, y for which P(x, y) is false for every 3y3xP(x,y) P(x, y) is true. pair x, y. The quantification Vx3yQ(x, y) denotes the proposition "For every real number x there is a real number y such that Q(x, y)." Given a real number x, there is a real number y such that x + y = 0; namely, y = -x. Hence, the statement Yx3yQ(x, y) is true. .... Example 4 illustrates that the order in which quantifiers appear makes a difference. The state­ ments 3yVxP(x, y)andVx3yP(x, y)arenotlogically equivalent. The statement 3yVxP(x, y)is true if and only ifthere is a y that makes P (x, y) true for every x . So, for this statement to be true, there must be a particular value of y for which P (x , y) is true regardless ofthe choice of x . On the other hand, Vx3yP(x, y) is true ifand only if for every value ofx there is a value ofy for which P (x, y) is true. So, for this statement to be true, no matter which x you choose, there must be a value of y (possibly depending on the x you choose) for which P (x, y) is true. In other words, in the second case, y can depend on x, whereas in the first case, y is a constant independent of x . From these observations, it follows that if3yVxP(x, y) is true, then Vx3yP(x, y) must also be true. However, ifVx3yP(x, y) is true, it is not necessa ry for 3yVxP(x, y) to be true. (See Supplementary Exercises 24 and 25 at the end of this chapter.) Table 1 summarizes the meanings of the different possible quantifications involving two variables. Quantifications of more than two variables are also common, as Example 5 illustrates. EXAMPLE 5 Let Q(x, y, z) be the statement "x + y = z." What are the truth values of the statements VxVy3z Q(x, y, z) and 3zVxVy Q(x, y, z), where the domain of all variables consists of all real numbers? Solution: Suppose that x and y are assigned values. Then, there exists a real number z such that x + y = z. Conseq uently, the quantification VxVy3z Q(x, y, z), 54 I / The Foundations: Logic and Proofs 1-54 which is the statement "For all real numbers x and for all real numbers y there is a real number z such that x + y = z," is true. The order of the quantification here is important, because the quantification 3zVxVyQ(x, y, z), which is the statement "There is a real number z such that for all real numbers x and for all real numbers y it is true that x + y = z," is false, because there is no value of z that satisfies the equation x + y = z for all values of x andy. .... Translating Mathematical Statements into Statements Involving Nested Quantifiers Mathematical statements expressed in English can be translated into logical expressions, as Examples 6-8 show. EXAMPLE 6 Translate the statement "The sum of two positive integers is always positive" into a logical expression. Exam=� Solution: To translate this statement into a logical expression, we first rewrite it so that the implied quantifiers and a domain are shown: "For every two integers, if these integers are both positive, then the sum of these integers is positive." Next, we introduce the variables x and y to obtain "For all positive integers x and y, x + y is positive." Consequently, we can express this statement as VxVy((x > 0) 1\ (y > 0) --+ (x + y > 0», where the domain for both variables consists of all integers. Note that we could also translate this using the positive integers as the domain. Then the statement "The sum of two positive integers is always positive" becomes "For every two positive integers, the sum of these integers is positive. We can express this as VxVy(x + y > 0), where the domain for both variables consists of all positive integers. EXAMPLE 7 Translate the statement "Every real number except zero has a multiplica tive inverse." (A mul­ tiplicative inverse of a real number x is a real number y such that x y = 1.) Solution: We first rewrite this as "For every real number x except zero, x has a multiplica tive inverse." We can rewrite this as "For every real number x, if x =I 0, then there exists a real number y such that x y = I." This can be rewritten as Vx((x =I 0) --+ 3y(xy = 1». One example that you may be familiar with is the concept of limit, which is important in calculus. 1-55 1.4 Nested Quantifiers 55 EXAMPLE 8 (Requires Calculus) Express the definition of a limit using quantifiers. Solution: Recall that the definition of the statement lim f(x) = L x->a is: For every real number E > 0 there exists a real number 8 > 0 such that If(x) -L I < E whenever 0 < Ix - a I < 8. This definition of a limit can be phrased in terms of quantifiers by VE38Vx(0 < Ix -al < 8 -+ If(x) -LI < E), where the domain for the variables 8 and E consists of all positive real numbers and for x consists of all real numbers. This definition can also be expressed as VE>038>OVx(0 < Ix -al < 8 -+ If(x) -LI < E) when the domain for the variables E and 8 consists of all real numbers, rather than just the positive real numbers. [Here, restricted quantifiers have been used. Recall that 'Ix> 0 P(x) means that for all x with x> 0, P(x) is true.] � Translating from Nested Quantifiers into English Expressions with nested quantifiers expressing statements in English can be quite complicated. The first step in translating such an expression is to write out what the quantifiers and predicates in the expression mean. The next step is to express this meaning in a simpler sentence. This process is illustrated in Examples 9 and 10. EXAMPLE 9 Translate the statement Vx(C(x) v 3y(C(y) 1\ F(x, y») into English, where C (x) is "x has a computer," F (x, y) is "x and yare friends," and the domain for both x and y consists of all students in your school. Solution: The statement says that for every student x in your school, x has a computer or there is a student y such that y has a computer and x and yare friends. In other words, every student in your school has a computer or has a friend who has a computer. � EXAMPLE 10 Translate the statement 3xVyVz«F(x, y) 1\ F(x, z) 1\ (y :j:. z» -+ --.F(y,z» into English, where F(a,b) means a and b are friends and the domain for x, y, and z consists of all students in your school. Solution: We first examine the expression (F(x, y) 1\ F(x, z) 1\ (y :j:. z» -+ --.F(y, z). This expression says that if students x and yare friends, and students x and z are friends, and further­ more, if y and z are not the same student, then y and z are not friends. It follows that the original statement, which is triply quantified, says that there is a student x such that for all students y and all students z other than y, if x and yare friends and x and z are friends, then y and z are not friends. In other words, there is a student none of whose friends are also friends with each other. � 56 1 / The Foundati ons: Logic and Proofs Translating English Sentences into Logical Expressions I-56 In Section 1.3 we showed how quantifiers can be used to translate sentences into logical expres­ sions. However, we avoided sentences whose translation into logical expressions required the use of nested quantifiers. We now address the translation of such sentences. EXAMPLE 11 Express the statement "If a person is female and is a parent, then this person is someone's mother" as a logical expression involving predicates, quantifiers with a domain consisting of all people, and logical connective s. Solution: The statement "If a person is female and is a parent, then this person is someone's mother" can be expressed as "For every person x, if person x is female and person x is a parent, then there exists a person y such that person x is the mother of person y." We introduce the propositional functions F(x) to represent "x is female," P(x) to represent "x is a parent," and M (x, y) to represent "x is the mother of y." The original statement can be represented as Vx«F(x) /\ P(x» -+ 3yM(x, y». Using the null quantification rule in part (b) of Exercise 47 in Section 1.3, we can move 3y to the left so that it appears just after Vx, because y does not appear in F(x) /\ P(x). We obtain the logically equivalent expression Vx3y«F(x) /\ P(x» -+ M(x, y». EXAMPLE 12 Express the statement "Everyone has exactly one best friend" as a logical expression involving predicates, quantifiers with a domain consisting of all people, and logical connective s. Solution: The statement "Everyone has exactly one best friend" can be expressed as "For every person x, person x has exactly one best friend." Introducing the universal quantifier, we see that this statement is the same as "Vx (person x has exactly one best friend)," where the domain consists of all people. To say that x has exactly one best friend means that there is a person y who is the best friend of x, and furthermore, that for every person z, if person z is not person y, then z is not the best friend of x. When we introduce the predicate B(x, y) to be the statement "y is the best friend of x ," the statement that x has exactly one best friend can be represented as 3y(B(x, y) /\ Vz«z # y) -+ -.B(x, z»). Consequently, our original statement can be expressed as Vx 3y(B (x , y) /\ Vz«z # y) -+ -.B(x, z»). [Note that we can write this statement as Vx3!yB(x, y), where 3! is the "uniqueness quantifier" defined on page 37.] ... EXAMPLE 13 Use quantifiers to express the statement "There is a woman who has taken a flight on every airline in the world." /-57 Assessmenl � EXAMPLE 14 Exlra� EIIamples� EXAMPLE 15 1.4 Nested Quantifiers 57 Solution: Let P(w, f) be "w has taken f" and QU, a) be "f is a flight on a." We can express the statement as 3w'v'a3f(P(w, f) 1\ QU, a», where the domains of discourse for w, f, and a consist of all the women in the world, all airplane flights, and all airlines, respectively. The statement could also be expressed as 3w'v'a3fR(w, f, a), where R(w, f, a) is "w has taken f on a." Although this is more compact, it somewhat obscures the relationships among the variables. Consequently, the first solution is usually preferable. .... Negating Nested Quantifiers Statements involving nested quantifiers can be negated by successively applying the rules for negating statements involving a single quantifier. This is illustrated in Examples 14-16. Express the negation of the statement 'v'x3y(xy = 1) so that no negation precedes a quantifier. Solution: By successively applying De Morgan's laws for quantifiers in Table 2 of Section 1.3, we can move the negation in -Nx3y(xy = 1) inside all the quantifiers. We find that -Nx3y(xy = 1) isequivalentt03x ..... 3y(xy = 1),whichisequivalent t03x'v'y ..... (xy = 1). Because ..... (xy = l)can be expressed more simply as xy =1= 1, we conclude that our negated statement can be expressed as 3x'v'y(xy =1= 1). . .... Use quantifiers to express the statement that "There does not exist a woman who has taken a flight on every airline in the world." Solution: This statement is the negation of the statement "There is a woman who has taken a flight on every airline in the world" from Example 13. By Example 13, our statement can be expressed as ..... 3w'v'a3f(P(w, f) 1\ QU, a», where P(w, f) is "w has taken f" and QU, a) is "f is a flight on a." By successively applying De Morgan's laws for quantifiers in Table 2 of Section 1.3 to move the negation inside successive quantifiers and by applying De Morgan's law for negating a conjunction in the last step, we find that our statement is equivalent to each of this sequence of statements: 'v'w ..... 'v'a3f(P(w, f) 1\ QU, a» == 'v'w3a ..... 3f(P(w, f) 1\ Q(J, a» == 'v'w3a'v' f ..... (P(w, f) 1\ QU, a» == 'v'w3a'v' f(""'P(w, f) V ..... QU, a». This last statement states "For every woman there is an airline such that for all flights, this woman has not taken that flight or that flight is not on this airline." .... EXAMPLE 16 (Requires Calculus) Use quantifiers and predicates to express the fact that limx-+a f(x) does not exist. Solution: To say that limx-->a f(x) does not exist means that for all real numbers L, limx-->a f(x) =1= L. By using Example 8, the statement limx-->a f(x) =1= L can be expressed as ..... 'v'E >0 38 >0 'v'x(O < Ix -al < 8 --* If(x)-LI < E). 58 I / The Foundations: Logic and Proofs 1-58 Successively applying the rules for negating quantified expressions, we construct this sequence of equivalent statements -,vE >038 >0 Vx(O< Ix -al <8 -* If(x) -LI <E) == 3E >0 -.38>0 Vx(O< Ix -al <8 -* If(x) -LI <E) == 3E >0 V8>0 -.VX(O< Ix -al <8 -* If(x) - LI <E) == 3E >0 V8>0 3x -.(0< Ix -al <8 -* If(x) - LI <E) == 3E >0 V8>0 3x(0< Ix -al <8/\ If(x) -LI � E). In the last step we used the equivalence -'(p -* q) == P /\ -'q, which follows from the fifth equivalence in Table 7 of Section 1.2. Because the statement "limx---+a f(x) does not exist" means for all real numbers L, limx---+a f(x) i= L, this can be expressed as VL3E>0 V8>0 3x(0 < Ix -al < 8/\ If(x) -LI � E). This last statement says that for every real number L there is a real number E > 0 such that for every real number 8 > 0, there exists a real number x such that 0 < Ix - a I < 8 and If(x) -LI � E. � Exercises 1. Translate these statements into English, where the domain for each variable consists of all real numbers. a) 'v'x3y(x < y) b) 'v'x'v'y(((x ::: 0) 1\ (y ::: 0» -+ (xy ::: 0» c) 'v'x'v'y3z(xy = z) 2. Translate these statements into English, where the domain for each variable consists of all real numbers. a) 3x'v'y(xy = y) b) 'v'x'v'y(((x ::: 0) 1\ (y < 0» -+ (x -y > 0» c) 'v'x'v'y3z(x = Y + z) 3. Let Q(x, y) be the statement "x has sent an e-mail mes­ sage to y," where the domain for both x and y consists of all students in your class. Express each of these quantifi­ cations in English. a) 3x3yQ(x,y) c) 'v'x3yQ(x, y) e) 'v'y3xQ(x,y) b) 3x'v'yQ(x,y) d) 3y'v'xQ(x, y) 1) 'v'x'v'yQ(x,y) 4. Let P(x, y) be the statement "student x has taken class y," where the domain for x consists of all students in your class and for y consists of all computer science courses at your school. Express each of these quantifications in English. a) 3x3yP(x, y) c) 'v'x3yP(x, y) e) 'v'y3xP(x, y) b) 3x'v'yP(x,y) d) 3y'v'x P(x,y) 1) 'v'x'v'yP(x, y) 5. Let W (x, y) mean that student x has visited website y, where the domain for x consists of all students in your school and the domain for y consists of all websites. Express each of these statements by a simple English sentence. a) W(Sarah Smith, www.att.com) b) 3xW(x, www.imdb.org) c) 3yW(Jose Orez, y) d) 3y(W(Ashok Puri, y) 1\ W(Cindy Yoon, y» e) 3y'v'z(y i= (David Belcher) 1\ (W(David Belcher, z) -+ W(y,z» ) 1) 3x3y'v'z((x i= y) 1\ (W(x,z) B W(y,z))) 6. Let C (x, y) mean that student x is enrolled in class y, where the domain for x consists of all students in your school and the domain for y consists of all classes being given at your school. Express each of these statements by a simple English sentence. a) C(Randy Goldberg, CS 252) b) 3xC(x, Math 695) c) 3yC(Carol Sitea, y) d) 3x(C(x, Math 222) 1\ C(x, CS 252» e) 3x3y'v'z((x i= y) 1\ (C(x, z) -+ C(y, z») 1) 3x3y'v'z((x i= y) 1\ (C(x, z) B C(y, z») 7 .. Let T (x, y) mean that student x likes cuisine y, where the domain for x consists of all students at your school and the domain for y consists of all cuisines. Express each of these statements by a simple English sentence. a) -.T(Abdallah Hussein, Japanese) b) 3xT(x, Korean) 1\ 'v'xT(x, Mexican) 1-59 c) 3y(T(Monique Arsenault, y) V T(Jay Johnson, y» d) 'v'x'v'z3y« x =f. z) --+ -'(T(x, y) /\ T(z, y») e) 3x 3z'v'y(T(x , y) *+ T(z, y» t) 'v'x'v'z3y(T(x,y) *+ T(z,y» 8. Let Q(x, y) be the statement "student x has been a contestant on quiz show y." Express each of these sentences in terms of Q(x, y), quantifiers, and logical con­ nectives, where the domain for x consists of all students at your school and for y consists of all quiz shows on television. a) There is a student at your school who has been a con­ testant on a television quiz show. b) No student at your school has ever been a contestant on a television quiz show. c) There is a student at your school who has been a con­ testant on Jeopardy and on Wheel of Fortune. d) Every television quiz show has had a student from your school as a contestant. e) At least two students from your school have been con­ testants on Jeopardy. 9. Let L(x, y) be the statement "x loves y," where the do­ main for both x and y consists of all people in the world. Use quantifiers to express each of these statements. a) Everybody loves Jerry. b) Everybody loves somebody. c) There is somebody whom everybody loves. d) Nobody loves everybody. e) There is somebody whom Lydia does not love. t) There is somebody whom no one loves. g) There is exactly one person whom everybody loves. h) There are exactly two people whom Lynn loves. i) Everyone loves himself or herself. j) There is someone who loves no one besides himself or herself. 10. Let F(x, y) be the statement "x can fooly," where the do­ main consists of all people in the world. Use quantifiers to express each of these statements. a) Everybody can fool Fred. b) Evelyn can fool everybody. c) Everybody can fool somebody. d) There is no one who can fool everybody. e) Everyone can be fooled by somebody. t) No one can fool both Fred and Jerry. g) Nancy can fool exactly two people. h) There is exactly one person whom everybody can fool. i) No one can fool himself or herself. j) There is someone who can fool exactly one person besides himself or herself. 11. Let S(x) be the predicate "x is a student," F(x) the pred­ icate "x is a faculty member," and A(x, y) the predicate "x has asked y a question," where the domain consists of all people associated with your school. Use quantifiers to express each of these statements. a) Lois has asked Professor Michaels a question. b) Every student has asked Professor Gross a question. 1.4 Nested Quantifiers S9 c) Every faculty member has either asked Professor Miller a question or been asked a question by Profes­ sor Miller. d) Some student has not asked any faculty member a question. e) There is a faculty member who has never been asked a question by a student. t) Some student has asked every faculty member a question. g) There is a faculty member who has asked every other faculty member a question. h) Some student has never been asked a question by a faculty member. 12. Let I (x) be the statement "x has an Internet connection" and C (x, y) be the statement "x and y have chatted over the Internet," where the domain for the variables x and y consists of all students in your class. Use quantifiers to express each ofthese statements. a) Jerry does not have an Internet connection. b) Rachel has not chatted over the Internet with Chelsea. c) Jan and Sharon have never chatted over the Internet. d) No one in the class has chatted with Bob. e) Sanjay has chatted with everyone except Joseph. t) Someone in your class does not have an Internet connection. g) Not everyone in your class has an Internet connection. h) Exactly one student in your class has an Internet connection. i) Everyone except one student in your class has an Internet connection. j) Everyone in your class with an Internet connection has chatted over the Internet with at least one other student in your class. k) Someone in your class has an Internet connection but has not chatted with anyone else in your class. I) There are two students in your class who have not chatted with each other over the Internet. m) There is a student in your class who has chatted with everyone in your class over the Internet. n) There are at least two students in your class who have not chatted with the same person in your class. 0) There are two students in the class who between them have chatted with everyone else in the class. 13. Let M (x, y) be "x has sent y an e-mail message" and T (x, y) be "x has telephoned y," where the domain consists of all students in your class. Use quantifiers to express each of these statements. (Assume that all e-mail messages that were sent are received, which is not the way things often work.) a) Chou has never sent an e-mail message to Koko. b) Arlene has never sent an e-mail message to or tele­ phoned Sarah. c) Jose has never received an e-mail message from Deborah. d) Every student in your class has sent an e-mail mes­ sage to Ken. 60 I / The Foundations: Logic and Proofs e) No one in your class has telephoned Nina. 1) Everyone in your class has either telephoned Avi or sent him an e-mail message. g) There is a student in your class who has sent everyone else in your class an e-mail message. h) There is someone in your class who has either sent an e-mail message or telephoned everyone else in your class. i) There are two different students in your class who have sent each other e-mail message s. j) There is a student who has sent himself or herself an e-mail message. k) There is a student in your class who has not received an e-mail message from anyone else in the class and who has not been called by any other student in the class. I) Every student in the class has either received an e-mail message or received a telephone call from another student in the class. m) There are at least two students in your class such that one student has sent the other e-mail and the second student has telephoned the first student. n) There are two different students in your class who between them have sent an e-mail message to or telephoned everyone else in the class. 14. Use quantifiers and predicates with more than one variable to express these statements. a) There is a student in this class who can speak Hindi. b) Every student in this class plays some sport. c) Some student in this class has visited Alaska but has not visited Hawaii. d) All students in this class have learned at least one programming language. e) There is a student in this class who has taken ev­ ery course offered by one of the departments in this school. 1) Some student in this class grew up in the same town as exactly one other student in this class. g) Every student in this class has chatted with at least one other student in at least one chat group. 15. Use quantifiers and predicates with more than one variable to express these statements. a) Every computer science student needs a course in discrete mathema tics. b) There is a student in this class who owns a personal computer. c) Every student in this class has taken at least one computer science course. d) There is a student in this class who has taken at least one course in computer science. e) Every student in this class has been in every building on campus. 1) There is a student in this class who has been in every room of at least one building on campus. g) Every student in this class has been in at least one room of every building on campus. 1-60 16. A discrete mathema tics class contains I mathema tics major who is a freshman, 12 mathema tics majors who are sophomores, 15 computer science majors who are sophomores, 2 mathema tics majors who are juniors, 2 computer science majors who are juniors, and 1 computer science major who is a senior. Express each of these state­ ments in terms of quantifiers and then determine its truth value. a) There is a student in the class who is a junior. b) Every student in the class is a computer science major. c) There is a student in the class who is neither a math­ ematics major nor a junior. d) Every student in the class is either a sophomore or a computer science major. e) There is a major such that there is a student in the class in every year of study with that major. 17. Express each of these system specifications using predi­ cates, quantifiers, and logical connectives, if necessary. a) Every user has access to exactly one mailbox. b) There is a process that continues to run during all error conditions only if the kernel is working correctly. c) All users on the campus network can access all web­ sites whose urI has a .edu extension. *d) There are exactly two systems that monitor every remote server. 18. Express each of these system specifications using predi­ cates, quantifiers, and logical connectives, if necessary. a) At least one console must be accessible during every fault condition. b) The e-mail address of every user can be retrieved whenever the archive contains at least one message sent by every user on the system. c) For every security breach there is at least one mecha­ nism that can detect that breach if and only if there is a process that has not been compromised. d) There are at least two paths connecting every two distinct endpoints on the network. e) No one knows the password of every user on the sys­ tem except for the system administrator, who knows all passwords. 19. Express each of these statements using mathematical and logical operators, predicates, and quantifiers, where the domain consists of all integers. a) The sum of two negative integers is negative. b) The difference of two positive integers is not neces­ sarily positive. c) The sum ofthe squares of two integers is greater than or equal to the square of their sum. d) The absolute value of the product of two integers is the product of their absolute values. 20. Express each of these statements using predicates, quan­ tifiers, logical connectives, and mathematical operators where the domain consists of all integers. a) The product of two negative integers is positive. b) The average of two positive integers is positive. /-6/ e) The difference of two negative integers is not neces­ sarily negative. d) The absolute value of the sum of two integers does not exceed the sum ofthe absolute values ofthese integers. 21. Use predicates, quantifiers, logical connectives, and mathemat ical operators to express the statement that every positive integer is the sum of the squares of four integers. 22. Use predicates, quantifiers, logical connectives, and math­ ematical operators to express the statement that there is a positive integer that is not the sum ofthree squares. 23. Express each of these mathematical statements using predicates, qUahtifiers, logical connectives, and mathe­ matical operators. a) The product of two negative real numbers is positive. b) The difference of a real number and itself is zero. e) Every positive real number has exactly two square roots. d) A negative real number does not have a square root that is a real number. 24. Translate each of these nested quantifications into an En­ glish statement that expresses a mathematical fact. The domain in each case consists of all real numbers. a) 3xVy(x + y = y) b) VxVy(((x � 0) /\ (y < 0» --+ (x -y > 0» e) 3x3y(((x � 0) /\ (y � 0» /\ (x -Y > 0» d) VxVy((x ::/= 0) /\ (y ::/= 0) _ (xy ::/= 0» 25. Translate each ofthese nested quantifications into an En­ glish statement that expresses a mathematical fact. The domain in each case consists of all real numbers. a) 3xVy(xy = y) b) VxVy(((x < 0) /\ (y < 0» --+ (xy > 0» e) 3x3y((x2 > y) /\ (x < y» d) VxVy3z(x + y = z) 26. Let Q(x, y) be the statement "x + y = x -y." If the domain for both variables consists of all integers, what are the truth values? a) Q(I, 1) e) VyQ(I, y) e) 3x3yQ(x, y) g) 3yVxQ(x, y) i) VxVyQ(x,y) b) Q(2,0) d) 3xQ(x,2) t) Vx3yQ(x,y) b) Vy3xQ(x,y) 27. Determine the truth value of each of these statements if the domain for all variables consists of all integers. a) Vn3m(n2 < m) b) 3nVm(n < m2) e) Vn3m(n + m = 0) d) 3nVm(nm = m) e) 3n3m(n2 + m2 = 5) t) 3n3m(n2 + m2 = 6) g) 3n3m(n + m = 4/\ n -m = 1) h) 3n3m(n + m = 4 /\ n -m = 2) i) VnVm3p(p = (m + n)/2) 28. Determine the truth value of each of these statements if the domain of each variable consists of all real numbers. a) Vx3y(x2 = y) e) 3xVy(xy = 0) d) 3x3y(x + y ::/= y + x) e) Vx(x::/= 0 --+ 3y(xy = 1» t) 3xVy(y::/= 0 --+ xy = 1) g) Vx3y(x + y = 1) 1.4 Nested Quantifiers 61 b) 3x3y(x + 2y = 2 /\ 2x + 4y = 5) i) Vx3y(x + y = 2/\ 2x -Y = 1) j) VxVy3z(z = (x + y)/2) 29. Suppose the domain of the propositional function P(x, y) consists ofpai� x and y, where x is 1,2, or 3 and y is 1,2, or 3. Write out these propositions using disjunctions and conjunctions. a) VxVyP(x,y) e) 3xVyP(x,y) b) 3x3yP(x,y) d) Vy3xP(x, y) 30. Rewrite each of these statements so that negations ap­ pear only within predicates (that is, so that no negation is outside a quantifier or an expression involving logical connective s). a) -.3y3xP(x, y) b) -.Vx3yP(x, y) e) -.3y(Q(y) /\ Vx-.R(x, y» d) -.3y(3xR(x,y) v VxS(x, y» e) -.3y(Vx3zT(x, y, z) v 3xVzU(x, y, z» 31. Express the negations of each of these statements so that all negation symbols immediately precede predicates. a) Vx3yVzT(x, y, z) b) Vx3yP(x,y)vVx3yQ(x,y) c) Vx3y(P(x, y) /\ 3zR(x, y, z» d) Vx3y(P(x,y)--+ Q(x,y» 32. Express the negations of each ofthese statements so that all negation symbols immediately precede predicates. a) 3zVyVxT(x,y,z) b) 3x3yP(x,y)/\ VxVyQ(x,y) e) 3x3y(Q(x, y) _ Q(Y, x» d) Vy3x3z(T(x,y,z)v Q(x,y» 33. Rewrite each of these statements so that negations ap­ pear only within predicates (that is, so that no negation is outside a quantifier or an expression involving logical connective s). a) -.VxVyP(x, y) b) -.Vy3xP(x, y) e) -.VyVx(P(x, y) v Q(x, y» d) -.(3x3y-.P(x, y) /\ VxVyQ(x, y» e) -.Vx(3yVzP(x, y, z) /\ 3zVyP(x, y, z» 34. Find a common domain for the variables x, y, and z for which the statement VxVy((x ::/= y) --+ Vz((z = x) V (z = y») is true and another domain for which it is false. 35. Find a common domain for the variables x, y, z, and w for which the statement VxVyVz3w((w ::/= x) /\ (w ::/= y) /\ (w ::/= z» is true and another common domain for these variables for which it is false. 36. Express each of these statements using quantifiers. Then form the negation of the statement so that no negation is to the left of a quantifier. Next, express the negation in 62 I / The Foundations: Logic and Proofs simple English. (Do not simply use the words "It is not the case that.") a) No one has lost more than one thousand dollars play­ ing the lottery. b) There is a student in this class who has chatted with exactly one other student. c) No student in this class has sent e-mail to exactly two other students in this class. d) Some student has solved every exercise in this book. e) No student has solved at least one exercise in every section of this book. 37. Express each of these statements using quantifiers. Then form the negation of the statement so that no negation is to the left of a quantifier. Next, express the negation in simple English. (Do not simply use the words "It is not the case that.") a) Every student in this class has taken exactly two mathematics classes at this school. b) Someone has visited every country in the world except Libya. c) No one has climbed every mountain in the Himalayas. d) Every movie actor has either been in a movie with Kevin Bacon or has been in a movie with someone who has been in a movie with Kevin Bacon. 38. Express the negations of these propositions using quan­ tifiers, and in English. a) Every student in this class likes mathemati cs. b) There is a student in this class who has never seen a computer. c) There is a student in this class who has taken every mathematics course offered at this school. d) There is a student in this class who has been in at least one room of every building on campus. 39. Find a counterexam ple, if possible, to these universally quantified statements, where the domain for all variables consists of all integers. a) VxVy(x2 = y2 -+ X = y) b) Vx3y(y2 = x) c) VxVy(xy::: x) 40. Find a counterexam ple, if possible, to these universally quantified statements, where the domain for all variables consists of all integers. a) Vx3y(x = l/y) b) Vx3y(y2 -X < 100) c) VxVy(x2 =1= y3) 41. Use quantifiers to express the associative law for multi­ plication of real numbers. 42. Use quantifiers to express the distributive laws of multi­ plication over addition for real numbers. 43. Use quantifiers and logical connectives to express the fact that every linear polynomial (that is, polynomial of degree 1) with real coefficients and where the coefficient of x is nonzero, has exactly one real root. 44. Use quantifiers and logical connectivesto express the fact 1-62 that a quadratic polynomial with real number coefficients has at most two real roots. 45. Determine the truth value ofthe statement V x 3 y( x y = 1) if the domain for the variables consists of a) the nonzero real numbers. b) the nonzero integers. c) the positive real numbers. 46. Determine the truth value of the statement 3xVy(x :s: y2) if the domain for the variables consists of a) the positive real numbers. b) the integers. c) the nonzero real numbers. 47. Show that the two statements -.3xVy P(x, y) and Vx3y-.P(x, y), where both quantifiers over the first vari­ able in P (x, y) have the same domain, and both quantifiers over the second variable in P (x, y) have the same domain, are logically equivalent. *48. Show that VxP(x) v VxQ(x) and VxVy(P(x) v Q(Y», where all quantifiers have the same nonempty domain, are logically equivalent. (The new variable y is used to combine the quantifications correctly.) *49. a) Show that VxP(x) 1\ 3xQ(x) is logically equivalent to Vx3y (P(x) 1\ Q(Y», where all quantifiers have the same nonempty domain. b) Show that VxP(x) v 3xQ(x) is equivalent to Vx3y(P(x) v Q(Y», where all quantifiers have the same nonempty domain. A statement is in prenex normal form (PNF) if and only if it is of the form Q\x\ Q2X2 ... QkXkP(X\, X2, ... , Xk), where each Qi, i = 1,2, ... , k, is either the existential quanti­ fierorthe universal quantifier, and P(x\, ... ,Xk) is a predicate involving no quantifiers. For example, 3xVy(P(x, y) 1\ Q(y» is in prenex normal form, whereas 3xP(x) v VxQ(x) is not (because the quantifiers do not all occur first). Every statement formed from propositional variables, predicates, T, and F using logical connectives and quantifiers is equivalent to a statement in prenex normal form. Exercise 51 asks for a proof of this fact. *50. Put these statements in prenex normal form. [Hint: Use logical equivalence from Tables 6 and 7 in Section 1.2, Table 2 in Section 1.3, Example 19 in Section 1.3, Ex­ ercises 45 and 46 in Section 1.3, and Exercises 48 and 49 in this section.] a) 3xP(x) v 3xQ(x) v A, where A is a proposition not involving any quantifiers. b) -.(VxP(x) v VxQ(x» c) 3xP(x) -+ 3xQ(x) **51. Show how to transform an arbitrary statement to a state­ ment in prenex normal form that is equivalent to the given statement . *52. Express the quantification 3!xP(x), introduced on page 38, using universal quantifications, existential quantifica­ tions, and logical operators. 1-63 1.5 Rules ofInference 63 1.5 Rules of Inference Introduction Later in this chapter we will study proofs. Proofs in mathematics are valid arguments that estab­ lish the truth of mathematical statements. By an argument, we mean a sequence of statements that end with a conclusion. By valid, we mean that the conclusion, or final statement of the argument, must follow from the truth ofthe preceding statements, or premises, ofthe argument. That is, an argument is valid if and only if it is impossible for all the premises to be true and the conclusion to be false. To deduce new statements from statements we already have, we use rules of inference which are templates for constructing valid arguments. Rules of inference are our basic tools for establishing the truth of statements. Before we study mathematical proofs, we will look at arguments that involve only compound propositions. We will define what it means for an argument involving compound propositions to be valid. Then we will introduce a collection of rules of inference in propositional logic. These rules of inference are among the most important ingredients in producing valid arguments. After we illustrate how rules of inference are used to produce valid arguments, we will describe some common forms of incorrect reasoning, called fallacies, which lead to invalid arguments. After studying rules of inference in propositional logic, we will introduce rules of inference for quantified statements. We will describe how these rules of inference can be used to produce valid arguments. These rules of inference for statements involving existential and universal quantifiers play an important role in proofs in computer science and mathematics, although they are often used without being explicitly mentioned. Finally, we will show how rules of inference for propositions and for quantified statements can be combined. These combinations of rule of inference are often used together in complicated arguments. Valid Arguments in Propositional Logic Consider the following argument involving propositions (which, by definition, is a sequence of propositions): "If you have a current password, then you can log onto the network." "You have a current password." Therefore, "You can log onto the network." We would like to determine whether this is a valid argument. That is, we would like to determine whether the conclusion "You can log onto the network" must be true when the premises "If you have a current password, then you can log onto the network" and "You have a current password" are both true. Before we discuss the validity of this particular argument, we will look at its form. Use p to represent "You have a current password" and q to represent "You can log onto the network." Then, the argument has the form p-+q p :.q where.'. is the symbol that denotes "therefore." 64 1 / The Foundati ons: Logic and Proofs /-64 DEFINITION 1 We know that when P and q are propositional variables, the statement «p -+ q) 1\ p) -+ q is a tautology (see Exercise lO(c) in Section 1.2). In particul ar, when both P -+ q and pare true, we know that q must also be true. We say this form of argument is valid because whenever all its premises (all statements in the argument other than the final one, the conclusion) are true, the conclusion must also be true. Now suppose that both "If you have a current password, then you can log onto the network" and "You have a current password" are true statements. When we replace p by "You have a current password" and q by "You can log onto the network," it necessarily follows that the conclusion "You can log onto the network" is true. This argument is valid because its form is valid. Note that whenever we replace p and q by propositions where p -+ q and p are both true, then q must also be true. What happens when we replace p and q in this argument form by propositions where not both p and p -+ q are true? For example, suppose that p represents "You have access to the network" and q represents "You can change your grade" and that p is true, but p -+ q is false. The argument we obtain by substituting these values of p and q into the argument form is "If you have access to the network, then you can change your grade." "You have access to the network." .'. "You can change your grade." The argument we obtained is a valid argument, but because one of the premises, namely the first premise, is false, we cannot conclude that the conclusion is true. (Most likely, this conclusion is false.) In our discussion, to analyze an argument, we replaced propositions by propositional vari­ ables. This changed an argument to an argument form. We saw that the validity of an argument follows from the validity of the form of the argument. We summarize the terminology used to discuss the validity of arguments with our definition of the key notions. An argument in propositional logic is a sequence of propositions. All but the final proposition in the argument are called premises and the final proposition is called the conclusion . An argument is valid if the truth of all its premises impl}es that the conclusion is true. An argument form in propositional logic is a sequence of compound propositions in­ volving propositional variables. An argument form is valid if no matter which particular propositions are substituted for the propositional variables in its premises, the conclusion is true if the premises are all true. From the definition of a valid argument form we see that the argument form with premises PI, P2, ... , Pn and conclusion q is valid, when (PI 1\ P2 1\ ... 1\ Pn) -+ q is a tautology. The key to showing that an argument in propositional logic is valid is to show that its argument form is valid. Consequently, we would like techniques to show that argument forms are valid. We will now develop methods for accomplishing this task. Rules of Inference for Propositional Logic We can always use a truth table to show that an argument form is valid. We do this by showing that whenever the premises are true, the conclusion must also be true. However, this can be a tedious approach. For example, when an argument form involves 10 different propositional variables, to use a truth table to show this argument form is valid requires 210 = 1024 different 1-65 1.5 Rules ofinference 65 rows. Fortunately, we do not have to resort to truth tables. Instead, we can first establish the validity of some relatively simple argument forms, called rules of inference. These rules of inference can be used as building blocks to construct more complicated valid argument forms. We will now introduce the most important rules of inference in propositional logic. The tautology (p 1\ (p ---+ q)) ---+ q is the basis ofthe rule of inference called modus ponens, or the law of detachment. (Modus ponens is Latin for mode that affirms.) This tautology leads to the following valid argument form, which we have already seen in our initial discussion about arguments (where, as before, the symbol:. denotes "therefore."): p p ---+ q :.q Using this notation, the hypotheses are written in a column, followed by a horizontal bar, followed by a line that begins with the therefore symbol and ends with the conclusion. In particular, modus ponens tells us that if a conditional statement and the hypothesis of this conditional statement are both true, then the conclusion must also be true. Example 1 illustrates the use of modus ponens. EXAMPLE 1 Suppose that the conditional statement "If it snows today, then we will go skiing" and its hypothesis, "It is snowing today," are true. Then, by modus ponens, it follows that the conclusion of the conditional statement, "We will go skiing," is true. .... As we mentioned earlier, a valid argument can lead to an incorrect conclusion if one or more of its premises is false. We illustrate this again in Example 2. EXAMPLE 2 Determine whether the argument given here is valid and determine whether its conclusion must be true because of the validity of the argument. "If J2 > �, then (J2) 2 > (D 2. We know that J2 > �. Consequently, (J2)2 = 2> G)2 = �." Solution: Let p be the proposition "J2 > �" and q the proposition "2 > (�)2." The premises of the argument are p ---+ q and p, and q is its conclusion. This argument is valid because it is constructed by using modus ponens, a valid argument form. However, one of its premises, J2 > �, is false. Consequently, we cannot conclude that the conclusion is true. Furthermore, note that the conclusion of this argument is false, because 2 < �. .... Table 1 lists the most important rules of inference for propositional logic. Exercises 9, 10, 15, and 30 in Section 1.2 ask for the verifications that these rules of inference are valid argument forms. We now give examples of arguments that use these rules of inference. In each argument, we first use propositional variables to express the propositions in the argument. We then show that the resulting argument form is a rule of inference from Table 1. EXAMPLE 3 State which rule of inference is the basis of the following argument: "It is below freezing now. Therefore, it is either below freezing or raining now." 66 I / The Foundations: Logic and Proofs TABLE 1 Rules of Inference. Rule of Inference Tautology p [p 1\ (p -+ q)] -+ q p-+q -- :. q -.q [-.q 1\ (p -+ q)] -+ -'p p-+q -- :. -'p p-+q [(p -+ q) 1\ (q -+ r)] -+ (p -+ r) q-+r -- :. p -+ r pvq [(p V q) 1\ -'p] -+ q -'p -- :. q p p-+(p vq) -- :. pvq p I\q (pl\q)-+p -- :. p p [(p) 1\ (q)] -+ (p 1\ q) q -- :. p 1\ q pvq [(p v q) 1\ (-'P V r)] -+ (q V r) -'p v r -- :. q Vr 1-66 Name Modus ponens Modustollens Hypothetical syllogism Disjunctive syllogism Addition Simplification Conjunction Resolution Solution: Let p be the proposition "It is below freezing now" and q the proposition "It is raining now." Then this argument is ofthe form p :.pvq This is an argument that uses the addition rule. EXAMPLE 4 State which rule of inference is the basis of the following argument: "It is below freezing and raining now. Therefore, it is below freezing now." Solution: Let p be the proposition "It is below freezing now," and let q be the proposition "It is raining now." This argument is of the form P I\q :. p This argument uses the simplification rule. 1-67 1.5 Rules ofInference 67 EXAMPLE 5 State which rule of inference is used in the argument: EXAMPLE 6 Exam:C If it rains today, then we will not have a barbecue today. If we do not have a barbecue today, then we will have a barbecue tomorrow. Therefore, if it rains today, then we will have a barbecue tomorrow. Solution: Let p be the proposition "It is raining today," let q be the proposition "We will not have a barbecue today," and let r be the proposition "We will have a barbecue tomorrow." Then this argument is of the form p-+q q-+r :. p -+ r Hence, this argument is a hypothetical syllogism. Using Rules of Inference to Build Arguments When there are many premises, several rules of inference are often needed to show that an argument is valid. This is illustrated by Examples 6 and 7, where the steps of arguments are displayed on separate lines, with the reason for each step explicitly stated. These examples also show how arguments in English can be analyzed using rules of inference. Show that the hypotheses "It is not sunny this afternoon and it is colder than yesterday," "We will go swimming only if it is sunny," "If we do not go swimming, then we will take a canoe trip," and "If we take a canoe trip, then we will be home by sunset" lead to the conclusion "We will be home by sunset." Solution: Let p be the proposition "It is sunny this afternoon," q the proposition "It is colder than yesterday," r the proposition "We will go swimming," s the proposition "We will take a canoe trip," and t the proposition "We will be home by sunset." Then the hypotheses become -op 1\ q, r -+ p, -or -+ s, and s -+ t. The conclusion is simply t. We need to give a valid argument with hypotheses -op 1\ q, r -+ p, -or -+ s, and s -+ t and conclusion t. We construct an argument to show that our hypotheses lead to the desired conclusion as follows. Step 1. -op 1\ q 2. -op 3. r -+ p 4. -or 5. -or -+ s 6. s 7. s -+ t 8. t Reason Hypothesis Simplification using (1) Hypothesis Modus tollens using (2) and (3) Hypothesis Modus ponens using (4) and (5) Hypothesis Modus ponens using (6) and (7) Note that we could have used a truth table to show that whenever each of the four hypotheses is true, the conclusion is also true. However, because we are working with five propositional variables, p, q, r, s, and t, such a truth table would have 32 rows. ... EXAMPLE 7 Show that the hypotheses "If you send me an e-mail message, then I will finish writing the program," "If you do not send me an e-mail message, then I will go to sleep early," and "If I go 68 1 / The Foundations: Logic and Proofs 1-68 to sleep early, then I will wake up feeling refreshed" lead to the conclusion "If I do not finish writing the program, then I will wake up feeling refreshed." Solution: Let p be the proposition "You send me an e-mail message," q the proposition "I will finish writing the program," r the proposition "I will go to sleep early," and s the proposition "I will wake up feeling refreshed." Then the hypotheses are p -+ q, ""'p -+ r, and r -+ s. The desired conclusion is ..... q -+ s. We need to give a valid argument with hypotheses p -+ q, ""'p -+ r, and r -+ s and conclusion ..... q -+ s. This argument form shows that the hypotheses p lead to the desired conclusion. Step 1.p-+q 2 . ..... q -+ ""'p 3. ""'p -+ r 4 . ..... q -+ r 5. r -+ s 6 . ..... q -+ s Resolution Reason Hypothesis Contrapositive of (1) Hypothesis Hypothetical syllogism using (2) and (3) Hypothesis Hypothetical syllogism using (4) and (5) Computer programs have been developed to automate the task of reasoning and proving theo­ rems. Many of these programs make use of a rule of inference known as resolution. This rule � of inference is based on the tautology Unks� EXAMPLE S Extra� Examples� «p v q) 1\ ( ..... p V r» -+ (q V r). (The verification that this is a tautology was addressed in Exercise 30 in Section 1.2.) The final disjunction in the resolution rule, q v r, is called the resolvent. When we let q = r in this tautology, we obtain (p v q) 1\ ( ..... p V q) -+ q. Furthermore, when we let r = F, we obtain (p v q) 1\ ( ..... p) -+ q (because q v F = q), which is the tautology on which the rule of disjunc­ tive syllogism is based. Use resolution to show that the hypotheses "Jasmine is skiing or it is not snowing" and "It is snowing or Bart is playing hockey" imply that "Jasmine is skiing or Bart is playing hockey." Solution: Let p be the proposition "It is snowing," q the proposition "Jasmine is skiing," and r the proposition "Bart is playing hockey." We can represent the hypotheses as ""'p v q and p v r, respectively. Using resolution, the proposition q v r, "Jasmine is skiing or Bart is playing hockey," follows. ... Resolution plays an important role in programming languages based on the rules of logic, such as Prolog (where resolution rules for quantified statements are applied). Furthermore, it can be used to build automatic theorem proving systems. To construct proofs in propositional logic using resolution as the only rule of inference, the hypotheses and the conclusion must be expressed as clauses, where a clause is a disjunction of variables or negations of these variables. We can replace a statement in propositional logic that is not a clause by one or more equivalent statements that are clauses. For example, suppose we have a statement of the form p v (q 1\ r). Because p v (q 1\ r) = (p V q) 1\ (p V r), we can replace the single statement p v (q 1\ r) by two statements p v q and p v r, each of which is a clause. We can replace a statement of 1-69 1.5 Rules of Inference 69 the form -'(p v q) by the two statements -'p and -.q because De Morgan's law tells us that -'(p v q) == -'p 1\ -'q. We can also replace a conditional statement p -+ q with the equivalent disjunction -'p v q. EXAMPLE 9 Show that the hypothe ses (p 1\ q) V r and r -+ s imply the conclusion p v s. unkS� EXAMPLE 10 Solution: We can rewrite the hypothesis (p 1\ q) V r as two clauses, p V r and q v r. We can also replace r -+ s by the equivalent clause -.r v s. Using the two clauses p V r and -'r v s, we can use resolution to conclude p v s. .... Fallacies Several common fallacies arise in incorrect arguments. These fallacies resemble rules of infer­ ence but are based on contingencies rather than tautologies. These are discussed here to show the distinction between correct and incorrect reasoning. The proposition [(p -+ q) 1\ q] -+ p is not a tautology, because it is false when p is false and q is true. However, there are many incorrect arguments that treat this as a tautology. In other words, they treat the argument with premises p -+ q and q and conclusion p as a valid argument form, which it is not. This type of incorrect reasoning is called the fallacy of affirming the conclusion. Is the following argument valid? If you do every problem in this book, then you will learn discrete mathematics. You learned discrete mathematics. Therefore, you did every problem in this book. Solution: Let p be the proposition "You did every problem in this book." Let q be the proposition "You learned discrete mathematics." Then this argument is of the form: if p -+ q and q, then p. This is an example of an incorrect argument using the fallacy of affirming the conclusion. Indeed, it is possible for you to learn discrete mathematics in some way other than by doing every problem in this book. (You may learn discrete mathematics by reading, listening to lectures, doing some, but not all, the problems in this book, and so on.) .... The proposition [(p -+ q) 1\ -'p] -+ -.q is not a tautology, because it is false when p is false and q is true. Many incorrect arguments use this incorrectly as a rule of inference. This type of incorrect reasoning is called the fallacy of denying the hypothesis. EXAMPLE 11 Let p and q be as in Example 10. If the conditional statement p -+ q is true, and -'p is true, is it correct to conclude that -.q is true? In other words, is it correct to assume that you did not learn discrete mathematics if you did not do every problem in the book, assuming that if you do every problem in this book, then you will learn discrete mathemati cs? Solution: It is possible that you learned discrete mathematics even if you did not do every problem in this book. This incorrect argument is of the form p -+ q and -'p imply -'q, which is an example of the fallacy of denying the hypothesis. .... 70 1 / The Foundations: Logic and Proofs Rules of Inference for Quantified Statements 1-70 We have discussed rules of inference for propositions. We will now describe some important rules of inference for statements involving quantifiers. These rules of inference are used extensively in mathematical arguments, often without being explicitly mentioned. Universal instantiation is the rule of inference used to conclude that P(c) is true, where c is a particular member of the domain, given the premise Vx P(x). Universal instantiation is used when we conclude from the statement "All women are wise" that "Lisa is wise," where Lisa is a member of the domain of all women. Universal generalization is the rule of inference that states that Vx P(x) is true, given the premise that P(c) is true for all elements c in the domain. Universal generalization is used when we show that Vx P(x) is true by taking an arbitrary element c from the domain and showing that P(c) is true. The element c that we select must be an arbitrary, and not a specific, element of the domain. That is, when we assert from VxP(x) the existence of an element c in the domain, we have no control over c and cannot make any other assumptions about c other than it comes from the domain. Universal generalization is used implicitly in many proofs in mathematics and is seldom mentioned explicitly. However, the error of adding unwarranted assumptions about the arbitrary element c when universal generalization is used is all too common in incorrect reasoning. Existential instantiation is the rule that allows us to conclude that there is an element c in the domain for which P(c) is true if we know that 3x P(x) is true. We cannot select an arbitrary value of c here, but rather it must be a c for which P(c) is true. Usually we have no knowledge of what c is, only that it exists. Because it exists, we may give it a name (c) and continue our argument. Existential generalization is the rule of inference that is used to conclude that 3xP(x) is true when a particular element c with P(c) true is known. That is, if we know one element c in the domain for which P(c) is true, then we know that 3xP(x) is true. We summarize these rules of inference in Table 2. We will illustrate how one of these rules of inference for quantified statements is used in Example 12. EXAMPLE 12 Show that the premises "Everyone in this discrete mathematics class has taken a course in computer science" and "Marla is a student in this class" imply the conclusion "Marla has taken a course in computer science." TABLE 2 Rules of Inference for Quantified Statements. Rule of Inference Name VxP(x) Universal instantiation :. P(c) P(c) for an arbitrary c Universal generalization :. VxP(x) 3xP(x) Existential instantiation :. P(c) for some element c P(c) for some element c Existential generalization :.3xP(x) 1-71 l.5 Rules oflnference 71 Solution: Let D(x) denote "x is in this discrete mathematics class," and let C(x) denote "x has taken a course in computer science." Then the premises are Vx(D(x) ---+ C(x» and D(MarIa). The conclusion is C(Marla). The following steps can be used to establish the conclusion from the premises. Step 1. Vx(D(x) ---+ C(x» 2. D(Marla) ---+ C(Marla) 3. D(MarIa) 4. C(MarIa) Reason Premise Universal instantiation from (1) Premise Modus ponens from (2) and (3) EXAMPLE 13 Show that the premises "A student in this class has not read the book," and "Everyone in this class passed the first exam" imply the conclusion "Someone who passed the first exam has not read the book." Solution: Let C(x) be "x is in this class," B(x) be "x has read the book," and P(x) be "x passed the first exam." The premises are 3x(C(x) 1\ -.B(x» and Vx(C(x) ---+ P(x». The conclusion is 3x(P(x) 1\ -.B(x». These steps can be used to establish the conclusion from the premises. Step 1. 3x(C(x) 1\ -.B(x» 2. C(a) 1\ -.B(a) 3. C(a) 4. Vx(C(x) ---+ P(x» 5. C(a) ---+ pea) 6. pea) 7. -.B(a) 8. pea) 1\ -.B(a) 9. 3x(P(x) 1\ -.B(x» Reason Premise Existential instantiation from (l) Simplification from (2) Premise Universal instantiation from (4) Modus ponens from (3) and (5) Simplification from (2) Conjunction from (6) and (7) Existential generalization from (8) Combining Rules of Inference for Propositions and Quantified Statements We have developed rules of inference both for propositions and for quantified statements. Note that in our arguments in Examples 12 and 13 we used both universal instantiation, a rule of inference for quantified statements, and modus ponens, a rule of inference for propositional logic. We will often need to use this combination of rules of inference. Because universal instantiation and modus ponens are used so often together, this combination of rules is sometimes called universal modus ponens. This rule tells us that ifVx(P(x) ---+ Q(x» is true, and if P(a) is true for a particular element a in the domain of the universal quantifier, then Q(a) must also be true. To see this, note that by universal instantiation, pea) ---+ Q(a) is true. Then, by modus ponens, Q(a) must also be true. We can describe universal modus ponens as follows: Vx(P(x) ---+ Q(x» P (a), where a is a particular element in the domain :. Q(a) Universal modus ponens is commonly used in mathematical arguments. This is illustrated in Example 14. EXAMPLE 14 Assume that "For all positive integers n, if n is greater than 4, then n2 is less than 2n" is true. Use universal modus ponens to show that 1002 < 2100• 72 I I The Foundations: Logic and Proofs 1-72 Solution: Let P(n) denote "n > 4" and Q(n) denote "n2 < 2n." The statement "For all positive integers n, ifn is greater than 4, thenn2 is less than 2n" can be represented byVn(P(n) --+ Q(n», where the domain consists of all positive integers. We are assuming that Vn(P(n) --+ Q(n» is true. Note that P(l 00) is true because 100 > 4. It follows by universal modus ponens that Q(n) is true, namely that 1002 < 2100• � Another useful combination of a rule of inference from propositional logic and a rule of inference for quantified statements is universal modus tollens. Universal modus toll ens combines universal instantiation and modus tollens and can be expressed in the following way: Vx(P(x) --+ Q(x» -'Q(a), where a is a particular element in the domain :. -,P(a) We leave the verification of universal modus tollens to the reader (see Exercise 25). Exercise 26 develops additional combinations of rules of inference in propositional logic and quantified statements. Exercises 1. Find the argument form for the following argument and determine whether it is valid. Can we conclude that the conclusion is true if the premises are true? If Socrates is human, then Socrates is mortal. Socrates is human. ... Socrates is mortal. 2. Find the argument form for the following argument and determine whether it is valid. Can we conclude that the conclusion is true if the premises are true? If George does not have eight legs, then he is not an insect. George is an insect. .'. George has eight legs. 3. What rule of inference is used in each of these arguments? a) Alice is a mathematics major. Therefore, Alice is ei­ ther a mathematics major or a computer science major. b) Jerry is a mathematics major and a computer science major. Therefore, Jerry is a mathematics major. c) If it is rainy, then the pool will be closed. It is rainy. Therefore, the pool is closed. d) If it snows today, the university will close. The uni­ versity is not closed today. Therefore, it did not snow today. e) If! go swimming, then I will stay in the sun too long. If! stay in the sun too long, then I will sunburn. There­ fore, if I go swimming, then I will sunburn. 4. What rule of inference is used in each ofthese arguments? a) Kangaroos live in Australia and are marsupials. There­ fore, kangaroos are marsupials. b) It is either hotter than 100 degrees today or the pollu­ tion is dangerous. It is less than 100 degrees outside today. Therefore, the pollution is dangerous. c) Linda is an excellent swimmer. If Linda is an excellent swimmer, then she can work as a lifeguard. Therefore, Linda can work as a lifeguard. d) Steve will work at a computer company this summer. Therefore, this summer Steve will work at a computer company or he will be a beach bum. e) If! work all night on this homework, then I can answer all the exercises. If! answer all the exercises, I will un­ derstand the material. Therefore , ifI work all night on this homework, then I will understand the material. 5. Use rules of inference to show that the hypotheses "Randy works hard," "If Randy works hard, then he is a dull boy," and "If Randy is a dull boy, then he will not get the job" imply the conclusion "Randy will not get the job." 6. Use rules of inference to show that the hypotheses "If it does not rain or if it is not foggy, then the sailing race will be held and the lifesaving demonstration will go on," "If the sailing race is held, then the trophy will be awarded," and "The trophy was not awarded" imply the conclusion "It rained." 7. What rules of inference are used in this famous argu­ ment? "All men are mortal. Socrates is a man. Therefore, Socrates is mortal." 8. What rules of inference are used in this argument? "No man is an island. Manhattan is an island. Therefore, Manhattan is not a man." 1-73 9. For each of these sets of premises, what relevant conclu­ sion or conclusions can be drawn? Explain the rules of in­ ference used to obtain each conclusion from the premises. a) "IfI take the day off, it either rains or snows." "I took Tuesday off or I took Thursday off." "It was sunny on Tuesday." "It did not snow on Thursday." b) "If! eat spicy foods, then I have strange dreams." "I have strange dreams if there is thunder while I sleep." "I did not have strange dreams." c) "I am either clever or lucky." "I am not lucky." "If I am lucky, then I will win the lottery." d) "Every computer science major has a personal com­ puter." "Ralph does not have a personal computer." "Ann has a personal computer." e) "What is good for corporations is good for the United States." "What is good for the United States is good for you." "What is good for corporations is for you to buy lots of stuff." t) "All rodents gnaw their food." "Mice are rodents." "Rabbits do not gnaw their food." "Bats are not ro­ dents." 10. For each of these sets of premises, what relevant conclu­ sion or conclusions can be drawn? Explain the rules of in­ ference used to obtain each conclusion from the premises. a) "If! play hockey, then I am sore the next day." "I use the whirlpool if I am sore." "I did not use the whirlpool." b) "If! work, it is either sunny or partly sunny." "I worked last Monday or I worked last Friday." "It was not sunny on Tuesday." "It was not partly sunny on Friday." c) "All insects have six legs." "Dragonflies are insects." "Spiders do not have six legs." "Spiders eat dragon­ flies." d) "Every student has an Internet account." "Homer does not have an Internet account." "Maggie has an Internet account." e) "All foods that are healthy to eat do not taste good." "Tofu is healthy to eat." "You only eat what tastes good." "You do not eat tofu." "Cheeseburgers are not healthy to eat." t) "I am either dreaming or hallucinating." "I am not dreaming." "IfI am hallucinating, I see elephants run­ ning down the road." 11. Show that the argument form with premises PI, P2, ... , Pn and conclusion q -+ r is valid ifthe argument form with premises PI, P2, ... , Pn, q, and conclusion r is valid. 12. Show that the argument form with premises (p /\ t) -+ (r v s), q -+ (u /\ t), u -+ p, and --.s and conclusion q -+ r is valid by first using Exercise 11 and then using rules of inference from Table 1. 13. For each of these arguments, explain which rules of infer­ ence are used for each step. a) "Doug, a student in this class, knows how to write programs in JAVA. Everyone who knows how to write programs in JAVA can get a high-paying job. There­ fore, someone in this class can get a high-paying job." 1.5 Rules ofInference 73 b) "Somebody in this class enjoys whale watching. Every person who enjoys whale watching cares about ocean pollution. Therefore, there is a person in this class who cares about ocean pollution." c) "Each of the 93 students in this class owns a personal computer. Everyone who owns a personal computer can use a word processing program. Therefore, Zeke, a student in this class, can use a word processing pro­ gram." d) "Everyone in New Jersey lives within 50 miles ofthe ocean. Someone in New Jersey has never seen the ocean. Therefore, someone who lives within 50 miles of the ocean has never seen the ocean." 14. For each of these arguments, explain which rules of infer­ ence are used for each step. a) "Linda, a student in this class, owns a red convertib le. Everyone who owns a red convertible has gotten at least one speeding ticket. Therefore, someone in this class has gotten a speeding ticket." b) "Each of five roommates, Melissa, Aaron, Ralph, Ve­ neesha, and Keeshawn, has taken a course in discrete mathema tics. Every student who has taken a course in discrete mathematics can take a course in algorithms. Therefore, all five roommates can take a course in al­ gorithms next year." c) "All movies produced by John Sayles are wonder­ ful. John Sayles produced a movie about coal min­ ers. Therefore, there is a wonderful movie about coal miners." d) "There is someone in this class who has been to France. Everyone who goes to France visits the Lou­ vre. Therefore, someone in this class has visited the Louvre." 15. For each of these arguments determine whether the argu­ ment is correct or incorrect and explain why. a) All students in this class understand logic. Xavier is a student in this class. Therefore, Xavier understands logic. b) Every computer science major takes discrete mathe­ matics. Natasha is taking discrete mathema tics. There­ fore, Natasha is a computer science major. c) All parrots like fruit. My pet bird is not a parrot. There­ fore, my pet bird does not like fruit. d) Everyone who eats granola every day is healthy. Linda is not healthy. Therefore, Linda does not eat granola every day. 16. For each of these arguments determine whether the argu­ ment is correct or incorrect and explain why. a) Everyone enrolled in the university has lived in a dor­ mitory. Mia has never lived in a dormitory. Therefore, Mia is not enrolled in the university. b) A convertible car is fun to drive. Isaac's car is not a convertible. Therefore, Isaac's car is not fun to drive. c) Quincy likes all action movies. Quincy likes the movie Eight Men Out. Therefore, Eight Men Out is an action movie. 74 1/ The Foundations: Logic and Proofs d) Alliobstennen set at least a dozen traps. Hamilton is a lobstennan. Therefore, Hamilton sets at least a dozen traps. 17. What is wrong with this argument? Let H(x) be "x is happy." Given the premise 3x H (x), we conclude that H(Lola). Therefore, Lola is happy. 18. What is wrong with this argument? Let S(x, y) be "x is shorter than y." Given the premise 3s S(s, Max), it follows that S(Max, Max). Then by existential generalization it follows that 3x S(x, x), so that someone is shorter than himself. 19. Detennine whether each of these arguments is valid. Ifan argument is correct, what rule of inference is being used? If it is not, what logical error occurs? a) If n is a real number such that n > 1, then n2 > 1. Suppose that n2 > 1. Then n > 1. b) Ifn is areal numberwithn > 3, thenn2 > 9. Suppose that n2 :s 9. Then n :s 3. c) Ifnisarealnumberwithn > 2,thenn2 > 4. Suppose that n :s 2. Then n2 :s 4. 20. Detennine whether these are valid arguments. a) If x is a positive real number, then x2 is a positive real number. Therefore, if a2 is positive, where a is a real number, then a is a positive real number. b) If x2 =1= 0, where x is a real number, then x =1= O. Let a be a real number with a2 =1= 0; then a =1= O. 21. Which rules of inference are used to establish the conclusion of Lewis Carroll's argument described in Example 26 of Section 1.3? 22. Which rules of inference are used to establish the conclu­ sion of Lewis Carroll's argument described in Example 27 of Section 1.3? 23. Identify the error or errors in this argument that sup­ posedly shows that if 3xP(x) 1\ 3xQ(x) is true then 3x(P(x) 1\ Q(x» is true. 1. 3xP(x) 1\ 3xQ(x) Premise 2. 3xP(x) Simplification from (1) 3. Pee) Existential instantiation from (2) 4. 3xQ(x) Simplification from (1) 5. Q(e) Existential instantiation from (4) 6. Pee) 1\ Q(e) Conjunction from (3) and (5) 7. 3x(P(x) 1\ Q(x» Existential generalization 24. Identify the error or errors in this argument that sup­ posedly shows that if Vx(P(x) v Q(x» is true then VxP(x) v VxQ(x) is true. 1. Vx(P(x) v Q(x» Premise 2. Pee) v Q(e) Universal instantiation from (1) 3. Pee) Simplification from (2) 4. VxP(x) Universal generalization from (3) 5. Q(e) Simplification from (2) 6. VxQ(x) Universal generalization from (5) 7. Vx(P(x) v VxQ(x» Conjunction from (4) and (6) 25. Justify the rule of universal modus tollens by showing that the premises Vx(P(x) � Q(x» and --.Q(a) for a partic­ ular element a in the domain, imply --.P(a). 1-74 26. Justify the rule of universal transitivity, which states thatifVx(P(x) � Q(x»andVx(Q(x) � R(x»aretrue, then Vx(P(x) � R(x» is true, where the domains of all quantifiers are the same. 27. Use rules of inference to show that ifVx(P(x) � (Q(x) 1\ Sex»�) and Vx(P(x) 1\ R(x» are true, then Vx(R(x) 1\ Sex»� is true. 28. Use rules of inference to show that if Vx(P(x) v Q(x» and Vx«--.P(x) 1\ Q(x» � R(x» are true, then Vx(--.R(x) � P(x» is also true, where the domains of all quantifiers are the same. 29. Use rules of inference to show that ifVx(P(x) v Q(x», Vx(--.Q(x) v Sex»�, Vx(R(x) � --.S(x» , and 3x--.P(x) are true, then 3x--.R(x) is true. 30. Use resolution to show the hypotheses "Allen is a bad boy or Hillary is a good girl" and "Allen is a good boy or David is happy" imply the conclusion "Hillary is a good girl or David is happy." 31. Use resolution to show that the hypotheses "It is not rain­ ing or Yvette has her umbrella," "Yvette does not have her umbrella or she does not get wet," and "It is raining or Yvette does not get wet" imply that "Yvette does not get wet." 32. Show that the equivalence p 1\ --'p == F can be derived using resolution together with the fact that a condi­ tional statement with a false hypothesis is true. [Hint: Let q = r = F in resolution.] 33. Use resolution to show that the compound proposition (p v q) 1\ (--'P V q) 1\ (p V --.q) 1\ (--'P V --'q) is not sat­ isfiable. *34. The Logic Problem, taken from WFF'N PROOF, The Game of Logic, has these two assumptions: 1. "Logic is difficult or not many students like logic." 2. "If mathematics is easy, then logic is not difficult." By translating these assumptions into statements involv­ ing propositional variables and logical connectives, deter­ mine whether each of the following are valid conclusions of these assumptions: a) That mathematics is not easy, if many students like logic. b) That not many students like logic, if mathematics is not easy. c) That mathematics is not easy or logic is difficult. d) That logic is not difficult or mathematics is not easy. e) That if not many students like logic, then either math­ ematics is not easy or logic is not difficult. *35. Detennine whether this argument, taken from Kalish and Montague [KaM064], is valid. If Superman were able and willing to prevent evil, he would do so. If Supennan were unable to prevent evil, he would be impotent; if he were unwilling to prevent evil, he would be malevolent. Supennan does not prevent evil. If Supennan exists, he is nei­ ther impotent nor malevolent. Therefore, Supennan does not exist. 1-75 1.6 Introduction to Proofs 75 1.6 Introduction to Proofs UDkSit:'l Introduction In this section we introduce the notion of a proof and describe methods for constructing proofs. A proof is a valid argument that establishes the truth of a mathematical statement. A proof can use the hypotheses of the theorem, if any, axioms assumed to be true, and previously proven theorems. Using these ingredients and rules of inference, the final step of the proof establishes the truth of the statement being proved. In our discussion we move from formal proofs of theorems toward more informal proofs. The arguments we introduced in Section 1.5 to show that statements involving propositions and quantified statements are true were formal proofs, where all steps were supplied, and the rules for each step in the argument were given. However, formal proofs of useful theorems can be extremely long and hard to follow. In practice, the proofs of theorems designed for human consumption are almost always informal proofs, where more than one rule of inference may be used in each step, where steps may be skipped, where the axioms being assumed and the rules of inference used are not explicitly stated. Informal proofs can often explain to humans why theorems are true, while computers are perfectly happy producing formal proofs using automated reasoning systems. The methods of proof discussed in this chapter are important not only because they are used to prove mathematical theorems, but also for their many applications to computer science. These applications include verifying that computer programs are correct, establishing that operating systems are secure, making inferences in artificial intelligence, showing that system specifica­ tions are consistent, and so on. Consequently, understanding the techniques used in proofs is essential both in mathematics and in computer science. Some Terminology Formally, a theorem is a statement that can be shown to be true. In mathematical writing, the term theorem is usually reserved for a statement that is considered at least somewhat important. Less important theorems sometimes are called proposit ions. (Theorems can also be referred to as facts or results.) A theorem may be the universal quantification of a conditional statement with one or more premises and a conclusion. However, it may be some other type of logical statement, as the examples later in this chapter will show. We demonstrate that a theorem is true with a proof. A proof is a valid argument that establishes the truth of a theorem. The statements used in a proof can include axioms (or postulates), which are statements we assume to be true (for example, see Appendix 1 for axioms for the real numbers), the premises, if any, of the theorem, and previously proven theorems. Axioms may be stated using primitive terms that do not require definition, but all other terms used in theorems and their proofs must be defined. Rules of inference, together with definitions of terms, are used to draw conclusions from other assertions, tying together the steps of a proof. In practice, the final step of a proof is usually just the conclusion of the theorem. However, for clarity, we will often recap the statement ofthe theorem as the final step of a proof. A less important theorem that is helpful in the proof of other results is called a lemma (plural lemmas or lemmata). Complicated proofs are usually easier to understand when they are proved using a series of lemmas, where each lemma is proved individual ly. A corollary is a theorem that can be established directly from a theorem that has been proved. A conjecture is a statement that is being proposed to be a true statement, usually on the basis of some partial evidence, a heuristic argument, or the intuition of an expert. When a proof of a conjecture is found, the conjecture becomes a theorem. Many times conjectures are shown to be false, so they are not theorems. 76 1 / The Foundations: Logic and Proofs 1-76 Extra� Examples � Assessmenl iC Understandin g How Theorems Are Stated Before we introduce methods for proving theorems, we need to understand how many math­ ematical theorems are stated. Many theorems assert that a property holds for all elements in a domain, such as the integers or the real numbers. Although the precise statement of such theorems needs to include a universal quantifier, the standard convention in mathema tics is to omit it. For example, the statement "If x > y, where x and yare positive real numbers, then x2 > y2." really means "For all positive real numbers x and y, if x > y, then x2 > y2." Furthermore, when theorems of this type are proved, the law of universal instantiation is often used without explicit mention. The first step of the proof usually involves selecting a general element of the domain. Subsequent steps show that this element has the property in question. Finally, universal generalization implies that the theorem holds for all members of the domain. Methods of Proving Theorems We now tum our attention to proofs of mathematical theorems. Proving theorems can be difficult. We need all the ammunition that is available to help us prove different results. We now introduce a battery of different proof methods. These methods should become part of your repertoire for proving theorems. ToproveatheoremoftheformVx(P(x) --+ Q(x», our goal is to show that P(c) --+ Q(c) is true, where c is an arbitrary element of the domain, and then apply universal generalization. In this proof, we need to show that a conditional statement is true. Because of this, we now focus on methods that show that conditional statements are true. Recall that p --+ q is true unless p is true but q is false. Note that when the statement p --+ q is proved, it need only be shown that q is true if p is true. The following discussion will give the most common techniques for proving conditional statements. Later we will discuss methods for proving other types of statements. In this section, and in Section 1.7, we will develop an arsenal of many different prooftechniques that can be used to prove a wide variety of theorems. When you read proofs, you will often find the words "obviously" or "clearly." These words indicate that steps have been omitted that the author expects the reader to be able to fill in. Unfortunately, this assumption is often not warranted and readers are not at all sure how to fill in the gaps. We will assiduously try to avoid using these words and try not to omit too many steps. However, if we included all steps in proofs, our proofs would often be excruciatingly long. Direct Proofs A direct proof of a conditional statement p --+ q is constructed when the first step is the assumption that p is true; subsequent steps are constructed using rules of inference, with the final step showing that q must also be true. A direct proof shows that a conditional statement p --+ q is true by showing that if p is true, then q must also be true, so that the combination p true and q false never occurs. In a direct proof, we assume that p is true and use axioms, definitions, and previously proven theorems, together with rules of inference, to show that q must also be true. You will find that direct proofs of many results are quite straightforward, with a fairly obvious sequence of steps leading from the hypothe sis to the conclusion. However, direct 1-77 DEFINITION 1 EXAMPLE 1 Extra� Examples � EXAMPLE 2 1.6 Introduction to Proofs 77 proofs sometimes require particular insights and can be quite tricky. The first direct proofs we present here are quite straightforward; later in the text you will see some that are less obvious. We will provide examples of several different direct proofs. Before we give the first exam ple, we need a definition. The integer n is even ifthere exists an integer k such that n = 2k, and n is odd ifthere exists an integer k such that n = 2k + 1. (Note that an integer is either even or odd, and no integer is both even and odd.) Give a direct proof of the theorem "If n is an odd integer, then n2 is odd." Solution: Note that this theorem states VnP«n) -+ Q(n)), where P(n) is "n is an odd integer" and Q(n) is "n2 is odd." As we have said, we will follow the usual convention in mathematical proofs by showing that P(n) implies Q(n), and not explicitly using universal instantiation. To begin a direct proof of this theorem, we assume that the hypothesis of this conditional statement is true, namely, we assume that n is odd. By the definition of an odd integer, it follows that n = 2k + I, where k is some integer. We want to show that n2 is also odd. We can square both sides of the equation n = 2k + I to obtain a new equation that expresses n2• When we do this, we find that n2 = (2k + 1)2 = 4k2 + 4k + I = 2(2k2 + 2k) + I. By the definition of an odd integer, we can conclude that n2 is an odd integer (it is one more than twice an integer). Consequently, we have proved that if n is an odd integer, then n2 is an odd integer. ... Give a direct proof that if m and n are both perfect squares, then nm is also a perfect square. (An integer a is a perfect square if there is an integer b such that a = b2.) Solution: To produce a direct proof of this theorem, we assume that the hypothesis of this conditional statement is true, namely, we assume that m and n are both perfect squares. By the definition of a perfect square, it follows that there are integers sand t such that m = s2 and n = t2. The goal of the proof is to show that mn must also be a perfect square when m and n are; looking ahead we see how we can show this by multiplying the two equations m = s2 and n = t2 together. This shows that mn = s2t2, which implies that mn = (st)2 (using commutativity and associativity of multiplication). By the definition of perfect square, it follows that mn is also a perfect square, because it is the square of s t, which is an integer. We have proved that if m and n are both perfect squares, then mn is also a perfect square. ... Proof by Contraposition Direct proofs lead from the hypothesis of a theorem to the conclusion. They begin with the premises, continue with a sequence of deductions, and end with the conclusion. However, we will see that attempts at direct proofs often reach dead en ds. We need other methods of proving theo­ rems ofthe form V x (P (x) -+ Q (x)). Proofs of theorems of this type that are not direct proofs, that is, that do not start with the hypothe sis and end with the conclusion, are called indirect proofs. An extremely useful type of indirect proof is known as proof by contraposition. Proofs by contraposition make use of the fact that the conditional statement p -+ q is equivalent to its contrapositive, -.q -+ -'p. This means that the conditional statement p -+ q can be proved by showing that its contrapositive, -.q -+ -'p, is true. In a proof by contraposition of p -+ q, we take -.q as a hypothesis, and using axioms, definitions, and previously proven theorems, together with rules of inference, we show that -'p must follow. We will illustrate proof by contraposition 78 1 / The Foundati ons: Logic and Proofs 1-78 EXAMPLE 3 Exam:C EXAMPLE 4 with two examples. These examples show that proof by contraposition can succeed when we cannot easily find a direct proof. Prove that if n is an integer and 3n + 2 is odd, then n is odd. Solution: We first attempt a direct proof. To construct a direct proof, we first assume that 3n + 2 is an odd integer. This means that 3n + 2 = 2k + 1 for some integer k. Can we use this fact to show that n is odd? We see that 3n + 1 = 2k, but there does not seem to be any direct way to conclude that n is odd. Because our attempt at a direct proof failed, we next try a proof by contraposition. The first step in a proof by contraposition is to assume that the conclusion of the conditional statement "If 3n + 2 is odd, then n is odd" is false; namely, assume that n is even. Then, by the definition of an even integer, n = 2k for some integer k. Substituting 2k for n, we find that 3n + 2 = 3(2k) + 2 = 6k + 2 = 2(3k + 1). This tells us that 3n + 2 is even (because it is a multiple of 2), and therefore not odd. This is the negation of the hypothesis of the theorem. Because the negation of the conclusion of the conditional statement implies that the hypothesis is false, the original conditional statement is true. Our proof by contraposition succeeded; we have proved the theorem "If 3n + 2 is odd, then n is odd." .... Prove that if n = ab, where a and b are positive integers, then a :::: .jfi or b :::: .jfi. Solution: Because there is no obvious way of showing that a :::: .jfi or b :::: .jfi directly from the equation n = ab, where a and b are positive integers, we attempt a proof by contraposition. The first step in a proofby contraposition is to assume that the conclusion of the conditional statement "If n = ab, where a and b are positive integers, then a :::: .jfi or b :::: .jfi" is false. That is, we assume that the statement (a :::: .jfi) V (b :::: .jfi) is false. Using the meaning of disjunction together with De Morgan's law, we see that this implies that both a :::: .jfi and b :::: .jfi are false. This implies that a > .jfi and b > .jfi. We can multiply these inequalities together (using the fact that if 0 < t and 0 < v, then su < tv) to obtain ab > .jfi . .jfi = n. This shows that ab =I-n, which contradicts the statement n = abo Because the negation of the conclusion of the conditional statement implies that the hy­ pothesis is false, the original conditional statement is true. Our proof by contraposition suc­ ceeded; we have proved that if n = ab, where a and b are positive integers, then a :::: .jfi or b :::: .jfi. .... VACUOUS AND TRIVIAL PROOFS We can quickly prove that a conditional statement p -+ q is true when we know that p is false, because p -+ q must be true when p is false. Consequently, if we can show that p is false, then we have a proof, called a vacuous proof, of the conditional statement p -+ q. Vacuous proofs are often used to establish special cases of theorems that state that a conditional statement is true for all positive integers [i.e., a theorem of the kind VnP(n), where pen) is a propositional function]. Proof techniques for theorems of this kind will be discussed in Section 4.1. EXAMPLE 5 Show that the proposition P(O) is true, where pen) is "If n > 1, then n2 > n" and the domain consists of all integers. Solution: Note that P(O) is "If 0 > 1, then 02 > 0." We can show P(O) using a vacuous proof, because the hypothesis 0 > 1 is false. This tells us that P(O) is automatically true. .... 1-79 1.6 Introduction to Proofs 79 Remark: The fact that the conclusion of this conditional statement, 02 > 0, is false is irrelevant to the truth value of the conditional statement, because a conditional statement with a false hypothesis is guaranteed to be true. We can also quickly prove a conditional statement p --+ q if we know that the conclusion q is true. By showing that q is true, it follows that p --+ q must also be true. A proof of p --+ q that uses the fact that q is true is called a trivial proof. Trivial proofs are often important when special cases of theorems are proved (see the discussion of proof by cases in Section 1.7) and in mathematical induction, which is a proof technique discussed in Section 4.1. EXAMPLE 6 Let P(n) be "If a and b are positive integers with a ::: b, then an ::: bn," where the domain consists of all integers. Show that P(O) is true. Solution: The proposition P(O) is "If a ::: b,thenao::: bO."Becauseao = bO = I,theconclusion of the conditional statement "If a ::: b, then aO ::: bO" is true. Hence, this conditional statement, which is P(O), is true. This is an example of a trivial proof. Note that the hypothe sis, which is the statement "a ::: b," was not needed in this proof. .... A LITILE PROOF STRATEGY We have described two important approaches for proving theorems of the form Vx(P(x) --+ Q(x»: direct proof and proofby contraposition. We have also given exam ples that show how each is used. However, when you are presented with a theorem of the form Vx(P(x) --+ Q(x», which method should you use to attempt to prove it? We will provide a few rules of thumb here; in Section 1.7 we will discuss proof strategy at greater length. When you want to prove a statement of the form Vx(P(x) --+ Q(x», first evaluate whether a direct proof looks promising. Begin by expanding the definitions in the hypotheses. Start to reason using these hypotheses, together with axioms and available theorems. If a direct proof does not seem to go anywhere, try the same thing with a proof by contraposition. Recall that in a proof by contraposition you assume that the conclusion of the conditional statement is false and use a direct proof to show this implies that the hypothesis must be false. We illustrate this strategy in Examples 7 and 8. Before we present our next example, we need a definition. DEFINITION 2 The real number r is rational if there exist integers p and q with q =f:. 0 such that r = p / q. A real number that is not rational is called irrational. EXAMPLE 7 Prove that the sum of two rational numbers is rational. (Note that if we include the implicit quantifiers here, the theorem we want to prove is "For every real number r and every real number s, if rand s are rational numbers, then r + s is rational.) Solution: We first attempt a direct proof. To begin, suppose that rand s are rational numbers. From the definition of a rational number, it follows that there are integers p and q, with q =f:. 0, such that r = p / q, and integers t and u, with u =f:. 0, such that s = t / u. Can we use this information to show that r + s is rational? The obvious next step is to add r = p / q and s = t /u, to obtain P t pu + qt r+s=-+-==----=--q u qu Because q =f:. 0 and u =f:. 0, it follows that qu =f:. O. Consequently, we have expressed r + s as the ratio of two integers, pu + qt and qu, where qu =f:. O. This means that r + s is rational. We have proved that the sum of two rational numbers is rational; our attempt to find a direct proof succeeded. .... 80 I / The Foundations: Logic and Proofs 1-80 EXAMPLE 8 Prove that if n is an integer and n2 is odd, then n is odd. EXAMPLE 9 EXAMPLE 10 Solution: We first attempt a direct proof. Suppose that n is an integer and n2 is odd. Then, there exists an integer k such that n2 = 2k + 1. Can we use this information to show that n is odd? There seems to be no obvious approach to show that n is odd because solving for n produces the equation n = ±J2k + 1, which is not terribly useful. Because this attempt to use a direct proof did not bear fruit, we next attempt a proof by contraposition. We take as our hypothesis the statement that n is not odd. Because every integer is odd or even, this means that n is even. This implies that there exists an integer k such that n = 2k. To prove the theorem, we need to show that this hypothesis implies the conclusion that n2 is not odd, that is, that n2 is even. Can we use the equation n = 2k to achieve this? By squaring both sides ofthis equation, we obtain n2 = 4k2 = 2(2k2), which implies that n2 is also even because n2 = 2t, where t = 2k2. We have proved that if n is an integer and n2 is odd, then n is odd. Our attempt to find a proof by contraposition succeeded. ... Proofs by Contradiction Suppose we want to prove that a statement p is true. Furthermore, suppose that we can find a contradiction q such that -'p -+ q is true. Because q is false, but -'p -+ q is true, we can conclude that -'p is false, which means that p is true. How can we find a contradiction q that might help us prove that p is true in this way? Because the statement r 1\ -'r is a contradiction whenever r is a proposition, we can prove that p is true if we can show that -'p -+ (r 1\ -.r) is true for some proposition r. Proofs of this type are called proofs by contradiction. Because a proofby contradiction does not prove a result directly, it is another type of indirect proof. We provide three examples of proofby contradiction. The first is an example of an application of the pigeonhole principle, a combinatorial technique that we will cover in depth in Section 5.2. Show that at least four of any 22 days must fall on the same day of the week. Solution: Let p be the proposition "At least four of 22 chosen days fall on the same day of the week." Suppose that -'p is true. This means that at most three of the 22 days fall on the same day of the week. Because there are seven days of the week, this implies that at most 21 days could have been chosen because for each of the days of the week, at most three of the chosen days could fall on that day. This contradicts the hypothesis that we have 22 days under consideration. That is, if r is the statement that 22 days are chosen, then we have shown that -'p -+ (r 1\ ....,r). Consequently, we know that p is true. We have proved that at least four of 22 chosen days fall on the same day of the week. ... Prove that V2 is irrational by giving a proof by contradiction. Solution: Let p be the proposition " V2 is irrational." To start a proofby contradiction, we suppose that -'p is true. Note that ""'p is the statement "It is not the case that V2 is irrational, " which says that V2 is rational. We will show that assuming that ""'p is true leads to a contradiction. If V2 is rational, there exist integers a and b with V2 = alb, where a and b have no common factors (so that the fraction alb is in lowest terms.) (Here, we are using the fact that every rational number can be written in lowest terms.) Because V2 = alb, when both sides of this equation are squared, it follows that 1-81 Hence, 1.6 Introduction to Proofs 81 By the definition of an even integer it follows that a2 is even. We next use the fact that if a2 is even, a must also be even, which follows by Exercise 16. Furthermore, because a is even, by the definition of an even integer, a = 2c for some integer c. Thus, Dividing both sides of this equation by 2 gives By the definition of even, this means that b2 is even. Again using the fact that if the square of an integer is even, then the integer itself must be even, we conclude that b must be even as well. We have now shown that the assumption of ...... p leads to the equation ,J2 = alb, where a and b have no common factors, but both a and b are even, that is, 2 divides both a and b. Note that the statement that ,J2 = alb, where a and b have no common factors, means, in particular, that 2 does not divide both a and b. Because our assumption of """p leads to the contradiction that 2 divides both a and b and 2 does not divide both a and b, """p must be false. That is, the statement p, ",J2 is irrational," is true. We have proved that ,J2 is irrational. .... Proofby contradiction can be used to prove conditional statements. In such proofs, we first assume the negation of the conclusion. We then use the premises of the theorem and the negation of the conclusion to arrive at a contradiction. (The reason that such proofs are valid rests on the logical equivalence of p -+ q and (p 1\ ...... q) -+ F. To see that these statements are equivalent, simply note that each is false in exactly one case, namely when p is true and q is false.) Note that we can rewrite a proof by contraposition of a conditional statement as a proof by contradiction. In a proof of p -+ q by contraposition, we assume that ...... q is true. We then show that """p must also be true. To rewrite a proof by contraposition of p -+ q as a proof by contradiction, we suppose that both p and ...... q are true. Then , we use the steps from the proof of ...... q -+ """p to show that """p is true. This leads to the contradiction p 1\ """p, completing the proof. Example 11 illustrates how a proof by contraposition of a conditional statement can be rewritten as a proof by contradiction. EXAMPLE 11 Give a proof by contradiction of the theorem "If 3n + 2 is odd, then n is odd." Solution: Let p be "3n + 2 is odd" and q be "n is odd." To construct a proof by contradiction, assume that both p and ...... q are true. That is, assume that 3n + 2 is odd and that n is not odd. Because n is not odd, we know that it is even. Following the steps in the solution of Example 3 (a proofby contraposition), we can show that if n is even, then 3n + 2 is even. First, because n is even, there is an integer k such that n = 2k. This implies that 3n + 2 = 3(2k) + 2 = 6k + 2 = 2(3k + 1). Because 3n + 2 is 2t, where t = 3k + 1, 3n + 2 is even. Note that the statement "3n + 2 is even" """p, because an integer is even if and only ifit is not odd. Because both p and """p are true, we have a contradiction. This completes the proof by contradiction, proving that if 3n + 2 is odd, then n is odd. .... Note that we can also prove by contradiction that p -+ q is true by assuming that p and ...... q are true, and showing that q must be also be true. This implies that ...... q and q are both true, a contradiction. This observation tells us that we can tum a direct proof into a proof by contradiction. 82 1 / The Foundati ons: Logic and Proofs 1-82 EXAMPLE 12 Exam=� PROOFS OF EQUIVALENCE To prove a theorem that is a biconditional statement, that is, a statement of the form P *+ q, we show that P -+ q and q -+ P are both true. The validity of this approach is based on the tautology (p *+ q) *+ [(p -+ q) 1\ (q -+ p)]. Prove the theorem "If n is a positive integer, then n is odd if and only if n2 is odd." Solution: This theorem has the form "P if and only if q ," where P is "n is odd" and q is "n2 is odd." (As usual, we do not explicitly deal with the universal quantificati on.) To prove this theorem, we need to show that P -+ q and q -+ P are true. We have already shown (in Example 1) that P -+ q is true and (in Example 8) that q -+ P is true. Because we have shown that both P -+ q and q -+ P are true, we have shown that the theorem is true. .... Sometimes a theorem states that several propositions are equivalent. Such a theorem states that propositions PI, P2, P3, ... , Pn are equivalent. This can be written as PI *+ P2 *+ ... *+ Pn, which states that all n propositions have the same truth values, and consequently, that for all i and j with 1 :s i :s nand 1 :s j :s n, Pi and P j are equivalent. One way to prove these mutually equivalent is to use the tautology This shows that if the conditional statements PI -+ P2, P2 -+ P3, ... , Pn -+ PI can be shown to be true, then the propositions PI, P2, ... , Pn are all equivalent. This is much more efficient than proving that Pi -+ P j for all i =f. j with 1 :s i :s nand 1 :s j :s n. When we prove that a group of statements are equivalent, we can establish any chain of conditional statements we choose as long as it is possible to work through the chain to go from anyone of the-se statements to any other statement. For example, we can show that PI, P2, and P3 are equivalent by showing that PI -+ P3, P3 -+ P2, and P2 -+ PI. EXAMPLE 13 Show that these statements about the integer n are equivalent: PI: n is even. P2: n - 1 is odd. P3: n2 is even. Solution: We will show that these three statements are equivalent by showing that the conditional statements PI -+ P2, P2 -+ P3, and P3 -+ PI are true. We use a direct proof to show that PI -+ P2. Suppose that n is even. Then n = 2k for some integer k. Consequently, n - 1 = 2k - 1 = 2(k -1) + 1. This means that n - 1 is odd because it is of the form 2m + 1, where m is the integer k -1. We also use a direct proof to show that P2 -+ P3. Now suppose n - 1 is odd. Then n - 1 = 2k + 1 for some integer k. Hence, n = 2k + 2 so that n2 = (2k + 2i = 4k2 + 8k + 4 = 2(2k2 + 4k + 2). This means that n2 is twice the integer 2k2 + 4k + 2, and hence is even. To prove P3 -+ PI, we use a proofby contraposition. That is, we prove that if n is not even, then n2 is not even. This is the same as proving that if n is odd, then n2 is odd, which we have already done in Example 1. This completes the proof. .... 1-83 EXAMPLE 14 Ex1ra� Examples IIiiiiiI unllsE:J EXAMPLE 15 1.6 Introduction to Proofs 83 COUNTERE XAMPLES In Section 1.3 we stated that to show that a statement of the form VxP(x) is false, we need only find a counterexample, that is, an example x for which P(x) is false. When presented with a statement of the form VxP(x), which we believe to be false or which has resisted all proof attempts, we look for a counterexamp le. We illustrate the use of counterexamp les in Example 14. Show that the statement "Every positive integer is the sum of the squares of two integers" is false. Solution: To show that this statement is false, we look for a counterexamp le, which is a par­ ticular integer that is not the sum of the squares of two integers. It does not take long to find a counterexamp le, because 3 cannot be written as the sum of the squares of two integers. To show this is the case, note that the only perfect squares not exceeding 3 are 02 = 0 and 12 = 1. Furthermore, there is no way to get 3 as the sum of two terms each of which is 0 or I. Conse­ quently, we have shown that "Every positive integer is the sum of the squares of two integers" is false. .... Mistakes in Proofs There are many common errors made in constructing mathematical proofs. We will briefly describe some of these here. Among the most common errors are mistakes in arithmetic and basic algebra. Even professional mathematicians make such errors, especially when working with complicated formulae. Whenever you use such computations you should check them as carefully as possible. (You should also review any troublesome aspects of basic algebra, especially before you study Section 4.1.) Each step of a mathematical proof needs to be correct and the conclusion needs to follow logically from the steps that precede it. Many mistakes result from the introduction of steps that do not logically follow from those that precede it. This is illustrated in Examples 15-17. What is wrong with this famous supposed "proof" that 1 = 2? "Proof:" We use these step s, where a and b are two equal pos itive integers. Step l.a=b 2. a2 = ab 3. a2 - b2 = ab - b2 4. (a-b)(a+b)=b(a-b) 5. a + b = b 6. 2b = b 7. 2 = 1 Reason Given Multiply both sides of (1) by a Subtract b2 from both sides of (2) Factor both sides of (3) Divide both sides of (4) by a - b Replace a by b in (5) because a = b and simplify Divide both sides of (6) by b Solution: Every step is valid except for one, step 5 where we divided both sides by a-b. The error is that a - b equals zero; division of both sides of an equation by the same quantity is valid as long as this quantity is not zero. .... EXAMPLE 16 What is wrong with this "proof"? "Theorem :" If n2 is positive, then n is positive. 84 1 / The Foundati ons: Logic and Proofs 1-84 "Proof:" Suppose that n2 is positive. Because the conditional statement "If n is positive, then n2 is positive" is true, we can conclude that n is positive. Solution: Let P(n) be "n is positive" and Q(n) be "n2 is positive." Then our hypothesis is Q(n). The statement "If n is positive, then n2 is positive" is the statement "In(P(n) --+ Q(n)). From the hypothesis Q(n) and the statement "In(P(n) --+ Q(n)) we cannot conclude pen), because we are not using a valid rule of inference. Instead, this is an example of the fallacy of affirming the conclusion. A counterexample is supplied by n = -1 for which n2 = 1 is positive, but n is negative. � EXAMPLE 17 What is wrong with this "proof"? "Theorem :" If n is not positive, then n2 is not positive. (This is the contrapositive of the "theorem" in Example 16.) "Proof:" Suppose that n is not positive. Because the conditional statement "If n is positive, then n2 is positive" is true, we can conclude that n2 is not positive. Solution: Let P(n) and Q(n) be as in the solution of Example 16. Then our hypothesis is -,P(n) and the statement "Ifn is positive, then n2 is positive" is the statement "In(P(n) --+ Q(n)). From the hypothesis -,P(n) and the statement "In(P(n) --+ Q(n)) we cannot conclude -'Q(n), because we are not using a valid rule of inference. Instead, this is an example of the fallacy of denying the hypothesis. A counterexample is supplied by n = -1, as in Example 16. � Finally, we briefly discuss a particularly nasty type of error. Many incorrect arguments are based on a fallacy called begging the question. This fallacy occurs when one or more steps of a proof are based on the truth of the statement being proved. In other words, this fallacy arises when a statement is proved using itself, or a statement equivalent to it. That is why this fallacy is also called circular reasoning. EXAMPLE 18 Is the following argument correct? It supposedly shows that n is an even integer whenever n2 is an even integer. Suppose that n2 is even. Then n2 = 2k for some integer k. Let n = 21 for some integer I. This shows that n is even. Solution: This argument is incorrect. The statement "let n = 21 for some integer I" occurs in the proof. No argument has been given to show that n can be written as 21 for some integer I. This is circular reasoning because this statement is equivalent to the statement being proved, namely, "n is even." Of course, the result itself is correct; only the method of proof is wrong. � Making mistakes in proofs is part of the learning process. When you make a mistake that someone else finds, you should carefully analyze where you went wrong and make sure that you do not make the same mistake again. Even professional mathematicians make mistakes in proofs. More than a few incorrect proofs of important results have fooled people for many years before subtle errors in them were found. Just a Beginning We have now developed a basic arsenal of proof methods. In the next section we will introduce other important proof methods. We will also introduce several important proof techniques in /-85 1.6 Introduction to Proofs 85 Chapter 4, including mathematical induction, which can be used to prove results that hold for all positive integers. In Chapter 5 we will introduce the notion of combinatorial proofs. In this section we introduced several methods for proving theorems of the form Vx(P(x) --+ Q(x )), including direct proofs and proofs by contraposition. There are many theorems of this type whose proofs are easy to construct by directly working through the hypotheses and definitions of the terms of the theorem. However, it is often difficult to prove a theorem without resorting to a clever use of a proof by contraposition or a proof by contradiction, or some other proof technique. In Section 1.7 we will address proof strategy. We will describe various approaches that can be used to find proofs when straightforward approaches do not work. Constructing proofs is an art that can be learned only through experience, including writing proofs, having your proofs critiqued, and reading and analyzing other proofs. Exercises 1. Use a direct proof to show that the sum of two odd integers is even. 2. Use a direct proof to show that the sum oftwo even inte­ gers is even. 3. Show that the square of an even number is an even number using a direct proof. 4. Show that the additive inverse, or negative, of an even number is an even number using a direct proof. 5. Prove that if m + nand n + p are even integers, where m, n, and p are integers, then m + p is even. What kind of proof did you use? 6. Use a direct proof to show that the product of two odd numbers is odd. 7. Use a direct proof to show that every odd integer is the difference of two squares. 8. Prove that if n is a perfect square, then n + 2 is not a perfect square. 9. Use a proof by contradiction to prove that the sum of an irrational number and a rational number is irrational. 10. Use a direct proof to show that the product of two rational numbers is rational. 11. Prove or disprove that the product of two irrational num­ bers is irrational. 12. Prove or disprove that the product of a nonzero rational number and an irrational number is irrational. 13. Prove that if x is irrational, then 1/ x is irrational. 14. Prove that if x is rational and x =1= 0, then I/x is rational. 15. Use a proof by contraposition to show that if x + y :::: 2, where x and yare real numbers, then x :::: I or y :::: I. S'16. Prove that if m and n are integers and m n is even, then m is even or n is even. 17. Show that if n is an integer and n 3 + 5 is odd, then n is even using a) a proof by contraposition. b) a proof by contradiction. 18. Prove that if n is an integer and 3n + 2 is even, then n is even using a) a proof by contraposition. b) a proof by contradiction. 19. Prove the proposition P(O), where P(n) is the proposition "If n is a positive integer greater than I, then n2 > n." What kind of proof did you use? 20. Prove the proposition P(I), where pen) is the proposi­ tion "If n is a positive integer, then n2 :::: n." What kind of proof did you use? 21. Let pen) be the proposition "If a and b are positive real numbers, then (a + b)n :::: an + bn." Prove that P (I) is true. What kind of proof did you use? 22. Show that if you pick three socks from a drawer contain­ ing just blue socks and black socks, you must get either a pair of blue socks or a pair of black socks. 23. Show that at least 10 of any 64 days chosen must fall on the same day of the week. 24. Show that at least 3 of any 25 days chosen must fall in the same month of the year. 25. Use a proofby contradiction to show that there is no ratio­ nal number r for which r3 + r + I = O. [Hint: Assume that r = a / b is a root, where a and b are integers and a / b is in lowest terms. Obtain an equation involving integers by multiplying by b3• Then look at whether a and bare each odd or even.] 26. Prove that if n is a positive integer, then n is even if and only if 7 n + 4 is even. 27. Prove that if n is a positive integer, then n is odd if and only if 5n + 6 is odd. 28. Prove that m2 = n2 if and only if m = n or m = -n. 29. Prove or disprove that if m and n are integers such that mn = 1, then either m = I and n = I, or else m = -I and n = -1. 30. Show that these three statements are equivalent, where a and b are real numbers: (i) a is less than b, (ii) the average of a and b is greater than a, and (iii) the average of a and b is less than b. 31. Show that these statements about the integer x are equiv­ alent: (i) 3x + 2 is even, (ii) x + 5 is odd, (iii) x2 is even. 86 1 / The Foundations: Logic and Proofs 32. Show that these statements about the real number x are equivalent: (i) x is rational, (ii) x /2 is rational, and (iii) 3x -1 is rational. 33. Show that these statements about the real number x are equivalent: (i) x is irrational, (ii) 3x + 2 is irrational, (iii) x /2 is irrational. 34. Is this reasoning for finding the solutions of the equation .J2x2 -1 = x correct? (1) .J2x2 -1 = x is given; (2) 2X2 - 1 = x2, obtained by squaring both sides of (1); (3) x2 -1 = 0, obtained by subtracting x2 from both sides of (2); (4) (x - 1 )(x + 1) = 0, obtained by factoring the left­ hand side of x2 -1; (5) x = 1 or x = -1, which follows because ab = 0 implies that a = 0 or b = O. 35. Are these steps for finding the solutions of .Jx + 3 = 3 - x correct? (1) .Jx + 3 = 3 - x is given; (2) x + 3 = x2 -6x + 9, obtained by squaring both sides of (I); (3) 0 = x2 -7x + 6, obtained by subtracting x + 3 from both sides of (2); (4) 0 = (x -1)(x -6), obtained by factoring the right-hand side of (3); (5) x = 1 or x = 6, which follows from (4) because ab = 0 implies that a = 0 or b = O. 36. Show thatthe propositions PI, P2, P3, and P4 can be shown 1.7 Proof Methods and Strategy Introduction 1-86 to be equivalent by showing that PI ++ P4, P2 ++ P3, and PI ++ P3· 37. Show that the propositions PI, P2, P3, P4, and P5 can be shown to be equivalent by proving that the conditional statement s PI -+ P4, P3 -+ PI. P4 -+ P2, P2 -+ P5, and P5 -+ P3 are true. 38. Find a counterexample to the statement that every positive integer can be written as the sum of the squares of three integers. 39. Prove that at least one of the real numbers aI. a2, ... , an is greater than or equal to the average of these numbers. What kind of proof did you use? 40. Use Exercise 39 to show that if the first 10 positive inte­ gers are placed around a circle, in any order, there exist three integers in consecutive locations around the circle that have a sum greater than or equal to 17. 41. Prove that if n is an integer, these four statements are equivalent: (i) n is even, (ii) n + 1 is odd, (iii) 3n + 1 is odd, (iv) 3n is even. 42. Prove that these four statements about the integer n are equivalent: (i) n2 is odd, (ii) 1 -n is even, (iii) n3 is odd, (iv) n2 + 1 is even. Assessment � In Section 1.6 we introduced a variety of different methods of proof and illustrated how each method is used. In this section we continue this effort. We will introduce several other important proof methods, including proofs where we consider different cases separately and proofs where we prove the existence of objects with desired properties. In Section 1.6 we only briefly discussed the strategy behind constructing proofs. This strategy includes selecting a proof method and then successfully constructing an argument step by step, based on this method. In this section, after we have developed a wider arsenal of proof methods, we will study some additional aspects of the art and science of proofs. We will provide advice on how to find a proof of a theorem. We will describe some tricks of the trade, including how proofs can be found by working backward and by adapting existing proofs. When mathematicians work, they formulate conjectures and attempt to prove or disprove them. We will briefly describe this process here by proving results about tiling checkerboards with dominoes and other types of pieces. Looking at tilings of this kind, we will be able to quickly formulate conjectures and prove theorems without first developing a theory. We will conclude the section by discussing the role of open questions. In particular, we will discuss some interesting problems either that have been solved after remaining open for hundreds of years or that still remain open. Exhaust ive Proof and Pro of by Cases Sometimes we cannot prove a theorem using a single argument that holds for all possible cases. We now introduce a method that can be used to prove a theorem, by considering different cases /-87 EXAMPLE 1 Ex1ra� Examples IIiiiiiiI EXAMPLE 2 1.7 Proof Methods and Strategy 87 separately. This method is based on a rule of inference that we will now introduce. To prove a conditional statement of the form the tautology can be used as a rule of inference. This shows that the original conditional statement with a hypothesis made up ofa disjunction of the propositions PI, P2, ... , Pn can be proved by proving each of the n conditional statements Pi -+ q, i = 1,2, ... , n, individually. Such an argument is called a proof by cases. Sometimes to prove that a conditional statement P -+ q is true, it is convenient to use a disjunction PI v P2 V ... V Pn instead of P as the hypothesis of the conditional statement, where P and PI v P2 V ... V Pn are equivalent. EXHAUSTIVE PROOF Some theorems can be proved by examining a relatively small num­ ber of examples. Such proofs are called exhaustive proofs, because these proofs proceed by exhausting all possibilities. An exhaustive proof is a special type of proof by cases where each case involves checking a -single example. We now provide some illustrations of exhaustive proofs. Prove that (n + 1)2 � 3n if n is a positive integer with n :s 4. Solution: We use a proof by exhaustion. We only need verify the inequality (n + 1)2 ::: 3n when n = 1,2, 3, and 4. For n = 1, we have (n + 1)2 = 22 = 4 and 3n = 31 = 3; for n = 2, we have (n + Ii = 32 = 9and3n = 32 = 9; forn = 3, wehave(n + 1)3 = 43 = 64and3n = 33 = 27; and for n = 4, we have (n + 1)3 = 53 = 125 and 3n = 34 = 81. In each ofthese four cases, we see that (n + 1)2 � 3n. We have used the method of exhaustion to prove that (n + 1)2 � 3n if n is a positive integer with n :s 4. .... Prove that the only consecutive positive integers not exceeding 100 that are perfect powers are 8 and 9. (An integer is a perfect power if it equals na, where a is an integer greater than 1.) Solution: We can prove this fact by showing that the only pair n, n + 1 of consecutive positive integers that are both perfect powers with n < 100 arises when n = 8. We can prove this fact by examining positive integers n not exceeding 100, first checking whether n is a perfect power, and if it is, checking whether n + 1 is also a perfect power. A quicker way to do this is simply to look at all perfect powers not exceeding 100 and checking whether the next largest integer is also a perfect power. The squares of positive integers not exceeding 100 are 1,4,9,16,25,36,49,64,81, and 100. The cubes of positive integers not exceeding 100 are 1, 8,27, and 64. The fourth powers of positive integers not exceeding 100 are 1, 16, and 81. The fifth powers of positive integers not exceeding 100 are 1 and 32. The sixth powers of positive integers not exceeding 100 are 1 and 64. There are no powers of positive integers higher than the sixth power not exceeding 100, other than 1. Looking at this list of perfect powers not exceeding 100, we see that n = 8 is the only perfect power n for which n + 1 is also a perfect power. That is, 23 = 8 and 32 = 9 are the only two consecutive perfect powers not exceeding 100. .... People can carry out exhaustive proofs when it is necessary to check only a relatively small number of instances of a statement. Computers do not complain when they are asked to check a much larger number of instances of a statement, but they still have limitations. Note that not even a computer can check all instances when it is impossible to list all instances to check. 88 1 / The Foundations: Logic and Proofs 1-88 EXAMPLE 3 PROOF BY CASES A proof by cases must cover all possible cases that arise in a theorem. We illustrate proofby cases with a couple of examples. In each example, you should check that all possible cases are covered. Prove that if n is an integer, then n 2 :::: n. Solution: We can prove that n2 :::: n for every integer by considering three cases, when n = 0, when n :::: 1, and when n :::: -1. We split the proof into three cases because it is straightforward to prove the result by considering zero, posi tive integers, and negative integers separately. Case (i). When n = 0, because 02 = 0, we see that 02 :::: 0. It follows that n2 :::: n is true in this case. Case (ii). When n :::: 1, when we multiply both sides of the inequality n :::: 1 by the positive integer n, we obtain n . n :::: n . 1. This implies that n2 :::: n for n :::: 1. Case (iii). In this case n :::: -1. However, n2 :::: 0. It follows that n2 :::: n. Because the inequality n2 :::: n holds in all three cases, we can conclude that if n is an integer, then n2 :::: n. <III EXAMPLE 4 Use a proof by cases to show that IxYI = Ixllyl, where x and yare real numbers. (Recall that lal, the absolute value of a, equals a when a :::: ° and equals -a when a :::: 0.) Solution: In our proof of this theorem, we remove absolute values using the fact that la I = a when a :::: ° and la I = -a when a < 0. Because both Ix I and Iyl occur in our formula, we will need four cases: (i) x and y both nonnegative, (ii) x nonnegative and y is negative, (iii) x negative and y nonnegative, and (iv) x negative and y negative. (Note that we can remove the absolute value signs by making the appropriate choice of signs within each case.) Case (i). We see that PI --+ q because xy :::: ° when x :::: ° and y :::: 0, so that IxYI = xy = Ixllyl· Case (ii). To see that P2 --+ q, note that if x :::: ° and y < 0, then xy :::: 0, so that IxYI = -xy = x(-y) = Ixllyl. (Here, because y < 0, we have Iyl = -y.) Case (iii). To see that P3 --+ q, we follow the same reasoning as the previous case with the roles of x and y reversed. Case (iv). To see that P4 --+ q, note that when x < ° and y < 0, it follows that xy > 0. Hence, IxYI = xy = (-x)(-y) = Ixllyl. Because we have completed all four cases and these cases exhaust all possibilities, we can conclude that Ix y I = Ix Ily I, whenever x and yare real numbers. <III LEVERAGING PROOF BY CASES The examples we have presented illustrating proof by cases provide some insight into when to use this method of proof. In particular, when it is not possible to consider all cases of a proof at the same time, a proofby cases should be considered. When should you use such a proof? Generally, look for a proofby cases when there is no obvious way to begin a proof, but when extra information in each case helps move the proof forward. Example 5 illustrates how the method of proof by cases can be used effectively. EXAMPLE 5 Formulate a conjecture about the decimal digits that occur as the final digit of the square of an integer and prove your result. Solution: The smallest perfect squares are 1,4,9,16, 25,36,49, 64,81,100,121, 144,169, 196, 225, and so on. We notice that the digits that occur as the final digit of a square are 0,1,4,5,6, and 9, with 2, 3, 7, and 8 never appearing as the finaldigitofa square. We conjecture /-89 1.7 Proof Methods and Strategy 89 this theorem: The final decimal digit of a perfect square is 0, 1,4,5,6 or 9. How can we prove this theorem? We first note that we can express an integer n as lOa + b, where a and b are positive integers andb is 0, 1,2,3,4,5,6, 7, 8, or9. Here a is the integer obtained by subtracting the final decimal digit of n from n and dividing by 10. Next, note that (1 Oa + b i = 100a2 + 20ab + b2 = 10(10a2 + 2b) + b 2, so thatthe final decimal digit of n2 is the same as the final decimal digit of b2. Furthermore, note that the final decimal digit of b2 is the same as the final decimal digit of (10 - b)2 = 100 - 20b + b 2. Consequently, we can reduce our proof to the consideration of six cases. Case (i}. The final digit ofn is 1 or 9. Then the final decimal digit ofn2 is the final decimal digit of 12 = 1 or 92 = 81, namely, 1. Case (ii). The final digit of n is 2 or 8. Then the final decimal digit of n2 is the final decimal digit of 22 = 4 or 82 = 64, namely, 4. Case (iii}. The final digit of n is 3 or 7. Then the final decimal digit of n2 is the final decimal digit of32 = 9 or 72 = 49, namely, 9. Case (iv). The final digit of n is 4 or 6. Then the final decimal digit of n2 is the final decimal digit of 42 = 16 or 62 = 36, namely, 6. Case (v). The final decimal digit of n is 5. Then the final decimal digit of n2 is the final decimal digit of 52 = 25, namely 5. Case (vi}. The final decimal digit of n is O. Then the final decimal digit of n2 is the final decimal digit of 02 = 0, namely O. Because we have considered all six cases, we can conclude that the final decimal digit of n2, where n is an integer is either 0, 1,2,4,5,6, or 9. ... Sometimes we can eliminate all but a few examples in a proof by cases, as Example 6 illustrates. EXAMPLE 6 Show that there are no solutions in integers x and y of x2 + 3 y2 = 8. Solution: We can quickly reduce a proof to checking just a few simple cases because x2 > 8 when Ix I ::: 3 and 3 y2 > 8 when Iy I ::: 2. This leaves the cases when x takes on one of the values -2, -1,0, 1, or2 andy takes on one of the values -1,0, or 1. We can finish using an exhaustive proof. To dispense with the remaining cases, we note that possible values for x2 are 0, 1, and 4, and possible values for 3y2 are 0 and 3, and the largest sum of possible values for x2 and 3y2 is 7. Consequently, it is impossible for x2 + 3 y2 = 8 to hold when x and yare integers. ... WITHOUT LOSS OF GENERALITY In the proof in Example 4, we dismissed case (iii), where x < 0 and y ::: 0, because it is the same as case (ii), where x ::: 0 and y < 0, with the roles of x and y reversed. To shorten the proof, we could have proved cases (ii) and (iii) together by assuming, without loss of generalit y, that x ::: 0 and y < O. Implicit in this statement is that we can complete the case with x < 0 and y ::: 0 using the same argument as we used for the case with x ::: 0 and y < 0, but with the obvious changes. In general, when the phrase "without loss of generality" is used in a proof( often abbreviated as WLOG), we assert that by proving one case of a theorem, no additional argument is required to prove other specified cases. That is, other cases follow by making straightforward changes to the argument, or by filling in some straightforward initial step. Of course, incorrect use of this principle can lead to unfortunate errors. Sometimes assumptions are made that lead to a loss in generality. Such assumptions can be made that do not take into account that one case may be substantially different from others. This can lead to an incomplete, and possibly unsalvageable, proof. In fact, many incorrect proofs of famous theorems turned out to rely on arguments that used the idea of "without loss of generality" to establish cases that could not be quickly proved from simpler cases. We now illustrate a proof where without loss of generality is used effectively. 90 1 / The Foundations: Logic and Proofs 1-90 EXAMPLE 7 Show that (x + y Y < xr + yr whenever x and y are positive real numbers and r is a real number with 0 < r < 1. Solution: Without loss of generality we can assume that x + y = 1. [To see this, suppose we have proved the theorem with the assumption that x + y = 1. Suppose that x + y = t. Then (x/t) + (y/t) = 1, which implies that «x/t) + (y/t)Y < (x/tY + (y/tY. Multiplying both sides of this last equation by tr shows that (x + y y < xr + yr.] Assuming that x + y = 1, because x and yare positive, we have 0 < x < 1 and 0 < y < 1. Because 0 < r < 1, it follows that 0 < 1 -r < 1, so X I-r < 1 and yl-r < 1. This means that x < xr andy < yr.Consequently,xr + yr > X + y = 1. This means that (x + yy = lr < xr + yr. This proves the theorem for x + y = 1. Because we could assume x + y = 1 without loss of generality, we know that (x + y y < xr + yr whenever x and y are positive real numbers and r is a real number with 0 < r < 1. .... COMMON ERRORS WITH EXHAUSTIVE PROOF AND PROOF BY CASES A common error of reasoning is to draw incorrect conclusions from exam ples. No matter how many separate examples are considered, a theorem is not proved by considering examples unless every possible case is covered. The problem of proving a theorem is analogous to showing that a computer program always produces the output desired. No matter how many input values are tested, unless all input values are tested, we cannot conclude that the program always produces the correct output. EXAMPLE 8 Is it true that every positive integer is the sum of 18 fourth powers of integers? Solution: To determine whether n can be written as the sum of 18 fourth powers of integers, we might begin by examining whether n is the sum of 18 fourth powers of integers for the smallest positive integers. Because the fourth powers of integers are 0, 1, 16, 81, ... , if we can select 18 terms from these numbers that add up to n, then n is the sum of 18 fourth powers. We can show that all positive integers up to 78 can be written as the sum of 18 fourth powers. (The details are left to the reader.) However, if we decided this was enough checking, we would come to the wrong conclusion. It is not true that every positive integer is the sum of 18 fourth powers because 79 is not the sum of 18 fourth powers (as the reader can verify). .... Another common error involves making unwarranted assumptions that lead to incorrect proofs by cases where not all cases are considered. This is illustrated in Example 9. EXAMPLE 9 What is wrong with this "proof"? "Theorem:" If x is a real number, then x2 is a positive real number. "Proof:" Let PI be "x is positive," let P2 be "x is negative," and let q be "x2 is positive." To show that PI -+ q is true, note that when x is positive, x2 is positive because it is the product of two positive numbers, x and x. To show that P2 -+ q, note that when x is negative, x2 is positive because it is the product of two negative numbers, x and x. This completes the proof. Solution: The problem with the proof we have given is that we missed the case x = O. When x = 0, x2 = 0 is not positive, so the supposed theorem is false. If P is "x is a real number," then we can prove results where P is the hypothesis with three cases, PI, P2, and P3, where PI is "x is positive," P2 is "x is negative," and P3 is "x = 0" because of the equivalence P B PI V P2 V P3· .... 1-91 Existence Proofs 1.7 Proof Methods and Strategy 91 Many theorems are assertions that objects of a particular type exist. A theorem of this type is a proposition of the form 3x P (x), where P is a predicate. A proof of a proposition of the form 3xP(x) is called an existence proof. There are several ways to prove a theorem of this type. Sometimes an existence proof of 3xP(x) can be given by finding an element a such that P (a) is true. Such an existence proof is called constructive. It is also possible to give an existence proof that is nonconstructive; that is, we do not find an element a such that Pea) is true, but rather prove that 3x P (x) is true in some other way. One common method of giving a nonconstructive existence proof is to use proof by contradiction and show that the negation of the existential quantification implies a contradiction. The concept of a constructive existence proof is illustrated by Example 10 and the concept of a nonconstructive existence proof is illustrated by Example 11. EXAMPLE 10 A Constructive Existence Proof Show that there is a positive integer that can be written as the sum of cubes of positive integers in two different ways. Extra;'" Solution: After considerable computation (such as a computer search) we find that Examples� Because we have displayed a positive integer that can be written as the sum of cubes in two different ways, we are done. � EXAMPLE 11 A Noncons tructive Existence Proof Show that there exist irrational numbers x and y such that xY is rational. EXAMPLE 12 unkS� Solution: By Example 10 in Section 1.6 we know that .j2 is irrational. Consider the number .j2v"i. If it is rational, we have two irrational numbers x and y with xY rational, namely, x = .j2 and y = .j2. On the other hand if .j2v"i is irrational, then we can let x = .j2v"i and y = .j2 r,;v"i h � v"i.v"i) r,;2 sothatxY = (v2 )'-'2 = '\1'2 = v2 = 2. This proof is an example of a nonconstructive existence proof because we have not found irrational numbers x and y such that xY is rational. Rather, we have shown that either the pair x = .j2, y = .j2 or the pair x = .j2v"i, Y = .j2 have the desired property, but we do not know which of these two pairs works! � Nonconstructive existence proofs often are quite subtle, as Example 12 illustrates. Chomp is a game played by two players. In this game, cookies are laid out on a rectangular grid. The cookie in the top left position is poisoned, as shown in Figure lea). The two players take turns making moves; at each move, a player is required to eat a remaining cookie, together with all cookies to the right and/or below it (see Figure I(b), for example). The loser is the player who has no choice but to eat the poisoned cookie. We ask whether one of the two players has a winning strategy. That is, can one of the players always make moves that are guaranteed to lead to a win? Solution: We will give a nonconstructive existence proof of a winning strategy for the first player. That is, we will show that the first player always has a winning strategy without explicitly describing the moves this player must follow. First, note that the game ends and cannot finish in a draw because with each move at least one cookie is eaten, so after no more than m x n moves the game ends, where the initial grid is 92 1 / The Foundations: Logic and Proofs (b) ............... ®-'@" ,. '-.-. '''',--!' ... -". '\ .. � FIGURE 1 (a) Chomp, the Top Left Cookie is Poison (b) Three Possible Moves. 1-92 m x n_ Now, suppose that the first player begins the game by eating just the cookie in the bottom right comer. There are two possibilities, this is the first move of a winning strategy for the first player, or the second player can make a move that is the first move of a winning strategy for the second player. In this second case, instead of eating just the cookie in the bottom right comer, the first player could have made the same move that the second player made as the first move of a winning strategy (and then continued to follow that winning strategy). This would guarantee a win for the first player. Note that we showed that a winning strategy exists, but we did not specify an actual winning strategy. Consequently, the proof is a nonconstructive existence proof. In fact, no one has been able to describe a winning strategy for that Chomp that applies for all rectangular grids by describing the moves that the first player should follow. However, winning strategies can be described for certain special cases, such as when the grid is square and when the grid only has two rows of cookies (see Exercises 15 and 16 in Section 4.2). <l1li Uniqueness Proofs Some theorems assert the existence of a unique element with a particular property. In other words, these theorems assert that there is exactly one element with this property. To prove a statement of this type we need to show that an element with this property exists and that no other element has this property. The two parts of a uniqueness proof are: Existence: We show that an element x with the desired property exists. Uniqueness: We show that if y #-x, then y does not have the desired property. Equivalently, we can show that if x and y both have the desired property, then x = y. Remark: Showing that there is a unique element x such that P(x) is the same as proving the statement 3x(P(x) /\ Vy(y #- x -+ -,P(y»). We illustrate the elements ofa uniqueness proof in Example 13. 1-93 EXAMPLE 13 ExIra;"" Examples"'" linllsE:2 1.7 Proof Methods and Strategy 93 Show that if a and b are real numbers and a =I=-0, then there is a unique real number r such that ar + b = O. Solution: First, note that the real number r = -b/a is a solution of ar + b = 0 because a( -b/a) + b = -b + b = O. Consequently, a real number r exists for which ar + b = O. This is the existence part of the proof. Second, suppose that s is a real number such that as + b = O. Then ar + b = as + b, where r = -b / a. Subtracting b from both sides, we find that ar = as. Dividing both sides of this last equation by a, which is nonzero, we see that r = s. This means that if s =I=-r, then as + b =I=-O. This establishes the uniqueness part of the proof. .... Proof Strategies Finding proofs can be a challenging business. When you are confronted with a statement to prove, you should first replace terms by their definitions and then carefully analyze what the hypotheses and the conclusion mean. After doing so, you can attempt to prove the result using one of the available methods of proof. Generally, if the statement is a conditional statement, GODFREY HAROLD HARDY (1877-1947) Hardy, born in Cranleigh, Surrey, England, was the older of two children of Isaac Hardy and Sophia Hall Hardy. His father was the geography and drawing master at the Cranleigh School and also gave singing lessons and played soccer. His mother gave piano lessons and helped run a boardinghouse for young students. Hardy's parents were devoted to their children's education. Hardy demonstrated his numerical ability at the early age of two when he began writing down numbers into the millions. He had a private mathematics tutor rather than attending regular classes at the Cranleigh School. He moved to Winchester College, a private high school, when he was 13 and was awarded a scholarship. He excelled in his studies and demonstrated a strong interest in mathema tics. He entered Trinity College, Cambridge, in 1896 on a scholarship and won several prizes during his time there, graduating in 1899. Hardy held the position oflecturer in mathematics at Trinity College at Cambridge University from 1906 to 1919, when he was appointed to the Sullivan chair of geometry at Oxford. He had become unhappy with Cambridge over the dismissal of the famous philosopher and mathematician Bertrand Russell from Trinity for antiwar activities and did not like a heavy load of administrative duties. In 1931 he returned to Cambridge as the Sadleirian professor of pure mathema tics, where he remained until his retirement in 1942. He was a pure mathematician and held an elitist view of mathema tics, hoping that his research could never be applied. Ironically, he is perhaps best known as one of the developers of the Hardy-W einberg law, which predicts patterns of inheritance. His work in this area appeared as a letter to the journal Science in which he used simple algebraic ideas to demonstrate errors in an article on genetics. Hardy worked primarily in number theory and function theory, exploring such topics as the Riemann zeta function, Fourier series, and the distribution of primes. He made many important contributions to many important problems, such as Waring's problem about representing positive integers as sums of kth powers and the problem of representing odd integers as sums of three primes. Hardy is also remembered for his collaborations with John E. Littlewood, a colleague at Cambridge, with whom he wrote more than 100 papers, and the famous Indian mathematical prodigy Srinivasa Ramanujan. His collaboration with Littlewood led to the joke that there were only three important English mathematicians at that time, Hardy, Littlewood, and Hardy­ Littlewood, although some people thought that Hardy had invented a fictitious person, Littlewood, because Littlewood was seldom seen outside Cambridge. Hardy had the wisdom of recognizing Ramanujan's genius from uncoventional but extremely creative writings Ramanuj an sent him, while other mathematicians failed to see the genius. Hardy brought Ramanujan to Cambridge and collaborated on important joint papers, establishing new results on the number of partitions of an integer. Hardy was interested in mathema tics education, and his book A Course of Pure Mathematics had a profound effect on undergraduate instruction in mathematics in the first half of the twentieth century. Hardy also wrote A Mathematician 50 Apology, in which he gives his answer to the question of whether it is worthwhile to devote one's life to the study of mathema tics. It presents Hardy's view of what mathematics is and what a mathematician does. Hardy had a strong interest in sports. He was an avid cricket fan and followed scores closely. One peculiar trait he had was that he did not like his picture taken (only five snapshots are known) and disliked mirrors, covering them with towels immediately upon entering a hotel room. HISTORICAL NOTE The English mathematician G. H. Hardy, when visiting the ailing Indian prodigy Ramanujan in the hospital, remarked that 1729, the number ofthe cab he took, was rather dull. Ramanujan replied "No, it is a very interesting number; it is the smallest number expressible as the sum of cubes in two different ways." 94 1 / The Foundations: Logic and Proofs /-94 unkS� you should first try a direct proof; if this fails, you can try an indirect proof. If neither of these approaches works, you might try a proof by contradiction. FORWARD AND BACKWARD REASONING Whichever method you choose, you need a starting point for your proof. To begin a direct proof of a conditional statement, you start with the premises. Using these premises, together with axioms and known theorems, you can construct a proof using a sequence of steps that leads to the conclusion. This type of reasoning, calledforward reasoning, is the most common type of reasoning used to prove relatively simple results. Similarly, with indirect reasoning you can start with the negation of the conclusion and, using a sequence of steps, obtain the negation of the premises. Unfortunately, forward reasoning is often difficult to use to prove more complicated results, because the reasoning needed to reach the desired conclusion may be far from obvious. In such cases it may be helpful to use backward reasoning. To reason backward to prove a statement q, we find a statement p that we can prove with the property that p ---+ q. (Note that it is not helpful to find a statement r that you can prove such that q ---+ r, because it is the fallacy of begging the question to conclude from q ---+ rand r that q is true.) Backward reasoning is illustrated in Examples 14 and 15. . SRINIVASA RAMANUJAN (1887-1920) The famous mathematical prodigy Ramanujan was born and raised in southern India near the city of Madras (now called Chennai). His father was a clerk in a cloth shop. His mother contributed to the family income by singing at a local temple. Ramanujan studied at the local English language school, displaying his talent and interest for mathema tics. At the age of 13 he mastered a textbook used by college students. When he was 15, a university student lent him a copy of Synopsis of Pure Mathematics. Ramanujan decided to work out the over 6000 results in this book, stated without proof or explanation, writing on sheets later collected to form notebooks. He graduated from high school in 1904, winning a scholarship to the University of Madras. Enrolling in a fine arts curriculum, he neglected his subjects other than mathematics and lost his scholarship. He failed to pass examinations at the university four times from 1904 to 1907, doing well only in mathema tics. During this time he filled his notebooks with original writings, sometimes rediscovering already published work and at other times making new discoveries. Without a university degree, it was difficult for Ramanujan to find a decent job. To survive, he had to depend on the goodwill of his friends. He tutored students in mathema tics, but his unconventional ways of thinking and failure to stick to the syllabus caused problems. He was married in 1909 in an arranged marriage to a young woman nine years his junior. Needing to support himself and his wife, he moved to Madras and sought a job. He showed his notebooks of mathematical writings to his potential employers, but the books bewildered them. However, a professor at the Presidency College recognized his genius and supported him, and in 1912 he found work as an accounts clerk, earning a small salary. Ramanujan continued his mathematical work during this time and published his first paper in 1910 in an Indian journal. He realized that his work was beyond that ofIndian mathematicians and decided to write to leading English mathema ticians. The first mathematicians he wrote to turned down his request for help. But in January 1913 he wrote to G. H. Hardy, who was inclined to tum Ramanujan down, but the mathematical statements in the letter, although stated without proof, puzzled Hardy. He decided to examine them closely with the help of his colleague and collaborator J. E. Littlewood. They decided, after careful study, that Ramanujan was probably a genius, because his statements "could only be written down by a mathematician of the highest class; they must be true, because if they were not true, no one would have the imagination to invent them." Hardy arranged a scholarship for Ramanujan, bringing him to England in 1914. Hardy personally tutored him in mathematical analysis, and they collaborated for five years, proving significant theorems about the number of partitions of integers. During this time, Ramanujan made important contributions to number theory and also worked on continued fractions, infinite series, and elliptic functions. Ramanujan had amazing insight involving certain types of functions and series, but his purported theorems on prime numbers were often wrong, illustrating his vague idea of what constitutes a correct proof. He was one of the youngest members ever appointed a Fellow of the Royal Society. Unfortunately, in 1917 Ramanujan became extremely ill. At the time, it was thought that he had trouble with the English climate and had contracted tuberculosis. It is now thought that he suffered from a vitamin deficiency, brought on by Ramanujan's strict vegetarianism and shortages in wartime England. He returned to India in 1919, continuing to do mathematics even when confined to his bed. He was religious and thought his mathematical talent came from his family deity, Namagiri. He considered mathematics and religion to be linked. He said that "an equation for me has no meaning unless it expresses a thought of God." His short life came to an end in April 1920, when he was 32 years old. Ramanuj an left several notebooks of unpublished results. The writings in these notebooks illustrate Ramanujan's insights but are quite sketchy. Several mathematicians have devoted many years of study to explaining and justifying the results in these notebooks. /-95 1.7 Proof Methods and Strategy 95 EXAMPLE 14 Given two positive real numbers x and y, their arithmetic mean is (x + y) /2 and their geometric mean is.jXY. When we compare the arithmetic and geometric means of pairs of distinct positive real numbers, we find that the arithmetic mean is always greater than the geometric mean. [For example, when x = 4 andy = 6, we have 5 = (4 + 6)/2 > � = .J24.] Can we prove that this inequality is always true? Solution: To prove that (x + y) /2 > .jXY when x and yare distinct positive real numbers, we can work backward. We construct a sequence of equivalent inequalities. The equivalent inequalities are (x + y)/2 > .jXY, (x + y)2/4 > xy, (x + y)2 > 4xy, x2 + 2xy + y2 > 4xy, x2 -2xy + y2 > 0, (x -y)2 > o. Because (x -y)2 > 0 when x =1= y, it follows that the final inequality is true. Because all these inequalities are equivalent, it follows that (x + y) /2 > .jXY when x =1= y. Once we have carried out this backward reasoning, we can easily reverse the steps to construct a proof using forward reasoning. We now give this proof. Suppose that x and y are distinct real numbers. Then (x -y)2 > 0 because the square of a nonzero real number is positive (see Appendix 1). Because (x -y)2 = x2 -2xy + y2, this implies that x2 -2xy + y2 > O. Adding 4xy to both sides, we obtain x2 + 2xy + y2 > 4xy. Becausex2 + 2xy + y2 = (x + y)2, this means that (x + y)2 2: 4xy. Dividing both sides of this equation by 4, we see that (x + y)2/4 > xy. Finally, taking square roots of both sides (which preserves the inequality because both sides are positive) yields (x + y)/2 > .jXY. We conclude that if x andy are distinct positive real numbers, then their arithmetic mean (x + y)/2 is greater than their geometric mean .jXY. � EXAMPLE 15 Suppose that two people playa game taking turns removing one, two, or three stones at a time from a pile that begins with 15 stones. The person who removes the last stone wins the game. Show that the first player can win the game no matter what the second player does. Solution: To prove that the first player can always win the game, we work backward. At the last step, the first player can win if this player is left with a pile containing one, two, or three stones. The second player will be forced to leave one, two, or three stones if this player has to remove stones from a pile containing four stones. Consequently, one way for the first person to win is to leave four stones for the second player on the next-to-last move. The first person can leave four stones when there are five, six, or seven stones left at the beginning of this player's move, which happens when the second player has to remove stones from a pile with eight stones. Consequently, to force the second player to leave five, six, or seven stones, the first player should leave eight stones for the second player at the second-to-last move for the first player. This means that there are nine , ten, or eleven stones when the first player makes this move. Similarly, the first player should leave twelve stones when this player makes the first move. We can reverse this argument to show that the first player can always make moves so that this player wins the game no matter what the second player does. These moves successively leave twelve, eight, and four stones for the second player. � 96 1 / The Foundations: Logic and Proofs 1-96 EXAMPLE 16 Ex1ra� Examples � EXAMPLE 17 ADAPTING EXISTING PROOFS An excellent way to look for possible approaches that can be used to prove a statement is to take advantage of existing proofs. Often an existing proof can be adapted to prove a new result. Even when this is not the case, some of the ideas used in existing proofs may be helpful. Because existing proofs provide clues for new proofs, you should read and understand the proofs you encounter in your studies. This process is illustrated in Example 16. In Example 10 of Section 1.6 we proved that ..ti is irrational. We now conjecture that J3 is irrational. Can we adapt the proof in Example lOin Section 1.6 to show that J3 is irrational? Solution: To adapt the proof in Example lOin Section 1.6, we begin by mimicking the steps in that proof, but with ..ti replaced with J3. First, we suppose that J3 = d / c where the fraction c / d is in lowest terms. Squaring both sides tells us that 3 = c2 / d2, so that 3d2 = c2. Can we use this equation to show that 3 must be a factor of both c and d, similar to how we used the equation 2b2 = a2 in Example 10 in Section 1.6 to show that 2 must be a factor of both a and b? (Recall that an integer s is a factor of the integer t if t / s is an integer. An integer n is even if and only if 2 is a factor of n.) In turns out that we can, but we need some ammunition from number theory, which we will develop in Chapter 3. We sketch out the remainder of the proof, but leave the justification of these steps until Chapter 3. Because 3 is a factor of c2, it must also be a factor of c. Furthermore, because 3 is a factor of c, 9 is a factor of c2, which means that 9 is a factor of 3d2• This implies that 3 is a factor of d2, which means that 3 is a factor of that d. This makes 3 a factor of both c and d, which is a contradiction. After we have filled in the justification for these steps, we will have shown that J3 is irrational by adapting the proof that ..ti is irrational. Note that this proof can be extended to show that In is irrational whenever n is a positive integer that is not a perfect square. We leave the details of this to Chapter 3. � A good tip is to look for existing proofs that you might adapt when you are confronted with proving a new theorem, particularly when the new theorem seems similar to one you have already proved. Looking for Counterexam ples In Section 1.5 we introduced the use of counterexamp les to show that certain statements are false. When confronted with a conjecture, you might first try to prove this conjecture, and if your attempts are unsuccessful, you might try to find a counterexamp le. If you cannot find a counterexamp le, you might again try to prove the statement. In any case, looking for counterex­ amples is an extremely important pursuit, which often provides insights into problems. We will illustrate the role of counterexamp les with a few examples. In Example 14 in Section 1.6 we showed that the statement "Every positive integer is the sum of two squares of integers" is false by finding a counterexamp le. That is, there are positive integers that cannot be written as the sum of the squares of two integers. Although we cannot write every positive integer as the sum of the squares of two integers, maybe we can write every positive integer as the sum of the squares of three integers. That is, is the statement "Every positive integer is the sum of the squares of three integers" true or false? Solution: Because we know that not every positive integer can be written as the sum of two squares of integers, we might initially be skeptical that every positive integer can be written as the sum of three squares of integers. So, we first look for a counterexamp le. That is, we can show that the statement "Every positive integer is the sum of three squares of integers" unkS� 1.7 Proof Methods and Strategy 97 is false if we can find a particular integer that is not the sum of the squares of three integers. To look for a counterexamp le, we try to write successive positive integers as a sum of three squares. We find that 1= 02 + 02 + 12,2 = 02 + 12 + 12,3 = 12 + 12 + 12,4 = 02 + 02 + 22, 5 = 02 + 12 + 22,6 = 12 + 12 + 22, but we cannot find a way to write 7 as the sum of three squares. To show that there are not three squares that add up to 7, we note that the only possible squares we can use are those not exceeding 7, namely, 0, 1, and 4. Because no three terms where each term is 0, 1, or 4 add up to 7, it follows that 7 is a counterexamp le. We conclude that the statement "Every positive integer is the sum of the squares of three integers" is false. We have shown that not every positive integer is the sum of the squares of three integers. The next question to ask is whether every positive integer is the sum of the squares of four positive integers. Some experimentation provides evidence that the answer is ye s. For example, 7 = 12 + 12 + 12 + 22, 25 = 42 + 22 + 22 + 12, and 87 = 92 + 22 + 12 + 12. It turns out the conjecture "Every positive integer is the sum of the squares offour integers" is true. For a proof, see [Ro05]. � Proof Strategy in Action Mathematics is generally taught as if mathematical facts were carved in stone. Mathematics texts (including the bulk of this book) formally present theorems and their proofs. Such presen­ tations do not convey the discovery process in mathemati cs. This process begins with exploring concepts and examples, asking questions, formulating conjectures, and attempting to settle these conjectures either by proof or by counterexamp le. These are the day-to-day activities of math­ ematicians. Believe it or not, the material taught in textbooks was originally developed in this way. People formulate conjectures on the basis of many types of possible evidence. The exam­ ination of special cases can lead to a conjecture, as can the identification of possible patterns. Altering the hypotheses and conclusions of known theorems also can lead to plausible conjec­ tures. At other times, conjectures are made based on intuition or a belief that a result holds. No matter how a conjecture was made, once it has been formulated, the goal is to prove or disprove it. When mathemati cians believe that a conjecture may be true, they try to find a proof. If they can­ not find a proof, they may look for a counterexamp le. When they cannot find a counterexamp le, they may switch gears and once again try to prove the conjecture. Although many conjectures are quickly settled, a few conjectures resist attack for hundreds of years and lead to the devel­ opment of new parts of mathemati cs. We will mention a few famous conjectures later in this section. Tilings We can illustrate aspects of proof strategy through a brief study of tHings of checkerboar ds. Looking at tHings of checkerboards is a fruitful way to quickly discover and prove many different results, with these proofs using a variety of proof methods. There are almost an endless number of conjectures that can be made and studied in this area too. To begin, we need to define some terms. A checkerboard is a rectangle divided into squares of the same size by horizontal and vertical lines. The game of checkers is played on a board with 8 rows and 8 columns; this board is called the standard checkerboard and is shown in Figure 2. In this section we use the term board to refer to a checkerboard of any rectangular size as well as parts of checkerboards obtained by removing one or more squares. A domino is a rectangular piece that is one square by two squares, as shown in Figure 3. We say that a board is tiled by dominoes when all its squares are covered with no overlapping dominoes and no dominoes overhanging the board. We now develop some results about tiling boards using dominoes. 98 1 / The Foundati ons: Logic and Proofs FIGURE 2 The Standard Checkerboard. EXAMPLE 18 Can we tile the standard checkerboard using dominoes? B rn FIGURE 3 Two Dominoes. 1-98 Solution: We can find many ways to tile the standard checkerboard using dominoes. For example, we can tile it by placing 32 dominoes horizontal ly, as shown in Figure 4. The existence of one such tiling completes a constructive existence proof. Of course, there are a large number of other ways to do this tiling. We can place 32 dominoes vertically on the board or we can place some tiles vertically and some horizontal ly. But for a constructive existence proof we needed to find just one such tiling. .... EXAMPLE 19 Can we tile a board obtained by removing one ofthe four comer squares of a standard checker­ board? Solution: To answer this question, note that a standard checkerboard has 64 squares, so removing a square produces a board with 63 squares. Now suppose that we could tile a board obtained from the standard checkerboard by removing a comer square. The board has an even number of squares because each domino covers two squares and no two dominoes overlap and no dominoes overhang the board. Consequently, we can prove by contradiction that a standard checkerboard with one square removed cannot be tiled using dominoes because such a board has an odd number of squares. .... We now consider a trickier situation. EXAMPLE 20 Can we tile the board obtained by deleting the upper left and lower right comer squares of a standard checkerboard, shown in Figure 5? Solution: A board obtained by deleting two squares of a standard checkerboard contains 64 - 2 = 62 squares. Because 62 is even, we cannot quickly rule out the existence of a tiling of the standard checkerboard with its upper left and lower right squares removed, unlike Example 19, where we ruled out the existence of a tiling of the standard checkerboard with one comer square removed. Trying to construct a tiling of this board by successively placing dominoes might be a first approach, as the reader should attempt. However, no matter how much we try, we cannot find such a tiling. Because our efforts do not produce a tiling, we are led to conjecture that no tiling exists. 1-99 OJ OJ OJ 1.7 Proof Methods and Strategy 99 OJOJOJOJ FIGURE 4 Tiling the Standard Checkerboard. FIGURE 5 The Standar d Checkerboard with the Upper Left and Lower Right Corners Removed. Eb FIGURE 6 A Right Triomino and a Straight Triomino. We might try to prove that no tiling exists by showing that we reach a dead end however we successively place dominoes on the board. To construct such a proof, we would have to consider all possible cases that arise as we run through all possible choices of successively placing dominoes. For example, we have two choices for covering the square in the second column of the first row, next to the removed top left comer. We could cover it with a horizontally placed tile or a vertically placed tile. Each of these two choices leads to further choices, and so on. It does not take long to see that this is not a fruitful plan of attack for a person, although a computer could be used to complete such a proof by exhaustion. (Exercise 21 asks you to supply such a proof to show that a 4 x 4 checkerboard with opposite comers removed cannot be tiled.) We need another approach. Perhaps there is an easier way to prove there is no tiling of a standard checkerboard with two opposite comers. As with many proofs, a key observation can help. We color the squares of this checkerboard using alternating white and black squares, as in Figure 2. Observe that a domino in a tiling of such a board covers one white square and one black square. Next, note that this board has unequal numbers of white square and black squares. We can use these observations to prove by contradiction that a standard checkerboard with opposite comers removed cannot be tiled using dominoes. We now present such a proof. Proof: Suppose we can use dominoes to tile a standard checkerboard with opposite comers removed. Note that the standard checkerboard with opposite comers removed contains 64 - 2 = 62 squares. The tiling would use 62/2 = 31 dominoes. Note that each domino in this tiling covers one white and one black square. Consequently, the tiling covers 31 white squares and 31 black squares. However, when we remove two opposite comer squares, either 32 of the remaining squares are white and 30 are black or else 30 are white and 32 are black. This contradicts the assumption that we can use dominoes to cover a standard checkerboard with opposite comers removed, completing the proof. .... We can use other types of pieces besides dominoes in tilings. Instead of dominoes we can study tilings that use identically shaped pieces constructed from congruent squares that are connected along their edges. Such pieces are called polyominoes, a term coined in 1953 by the mathematician Solomon Golomb, the author of an entertaining book about them [G094]. We will consider two polyominoes with the same number of squares the same if we can rotate and/or flip one ofthe polyominoes to get the other one. For example, there are two types of triominoes (see Figure 6), which are polyominoes made up of three squares connected by their sides. One 100 1/ The Foundations: Logic and Proofs /-/00 type of triomino, the straight triomino, has three horizontally connected squares; the other type, right triominoes, resembles the letter L in shape, flipped and/or rotated, if necessary. We will study the tHings of a checkerboard by straight triominoes here; we will study tilings by right triominoes in Section 4.1. EXAMPLE 21 Can you use straight triominoes to tile a standard checkerboard? Solution: The standard checkerboard contains 64 squares and each triomino covers three squares. Consequently, if triominoes tile a board, the number of squares of the board must be a mul­ tiple of 3. Because 64 is not a multiple of 3, triominoes cannot be used to cover an 8 x 8 checkerboard. .... In Example 22, we consider the problem of using straight triominoes to tile a standard checkerboard with one comer missing. EXAMPLE 22 Can we use straight triominoes to tile a standard checkerboard with one of its four comers removed? An 8 x 8 checkerboard with one comer removed contains 64 -1 = 63 squares. Any tiling by straight triominoes of one of these four boards uses 63/3 = 21 triominoes. However, when we experiment, we cannot find a tiling of one of these boards using straight triominoes. A proof by exhaustion does not appear promising. Can we adapt our prooffrom Example 20 to prove that no such tiling exists? THEOREM 1 Solution: We will color the squares of the checkerboard in an attempt to adapt the proof by contradiction we gave in Example 20 of the impossibility of using dominoes to tile a standard checkerboard with opposite comers removed. Because we are using straight triominoes rather than dominoes, we color the squares using three colors rather than two colors, as shown in Figure 7. Note that there are 21 green squares, 21 black squares, and 22 white squares in this coloring. Next, we make the crucial observation that when a straight triomino covers three squares of the checkerboa rd, it covers one green square, one black square, and one white square. Next, note that each of the three colors appears in a comer square. Thus without loss of generality, we may assume that we have rotated the coloring so that the missing square is colored green. Therefore, we assume that the remaining board contains 20 green squares, 21 black squares, and 22 white squares. If we could tile this board using straight triominoes, then we would use 63/3 = 21 straight triominoes. These triominoes would cover 21 green squares, 21 black squares, and 21 white squares. This contradicts the fact that this board contains 20 green squares, 21 black square s, and 22 white squares. Therefore we cannot tile this board using straight triominoes. .... The Role of Open Problems Many advances in mathematics have been made by people trying to solve famous unsolved problems. In the past 20 years, many unsolved problems have finally been resolved, such as the proof of a conjecture in number theory made more than 300 years ago . This conjecture asserts the truth of the statement known as Fermat's Last Theorem . FERMAT'S LAST THEOREM The equation xn + yn = zn has no solutions in integers x, y, and z with x yz i= 0 whenever n is an integer with n > 2. 1-101 unkS� EXAMPLE 23 unkS� 1.7 Proof Methods and Strategy 101 FIGURE 7 Coloring the Squares ofthe Standard Checkerboard with Opposite Corners Removed with Three Colors. Remark: The equation x2 + y2 = z2 has infinitely many solutions in integers x, y, and z; these solutions are called Pythagorean triples and correspond to the lengths of the sides of right triangles with integer lengths. See Exercise 30. This problem has a fascinating history. In the seventeenth century, Fermat jotted in the margin of his copy of the works of Diophantus that he had a "wondrous proof" that there are no integer solutions of xn + yn = zn when n is an integer greater than 2 with xyz =1= O. However, he never published a proof (Fermat published almost nothing), and no proof could be found in the papers he left when he died. Mathematicians looked for a proof for three centuries without success, although many people were convinced that a relatively simple proof could be found. (Proofs of special cases were found, such as the proof of the case when n = 3 by Euler and the proof of the n = 4 case by Fermat himself.) Over the years, several established mathematicians thought that they had proved this theorem. In the nineteenth century, one ofthese failed attempts led to the development of the part of number theory called algebraic number theory. A correct proof, requiring hundreds of pages of advanced mathematics, was not found until the 1990s, when Andrew Wiles used recently developed ideas from a sophisticated area of number theory called the theory of elliptic curves to prove Fermat's last theorem. Wiles's quest to find a proof of Fermat's last theorem using this powerful theory, described in a program in the Nova series on public television, took close to ten years! (The interested reader should consult [Ro05] for more information about Fermat's Last Theorem and for additional references concerning this problem and its resolution.) We now state an open problem that is simple to describe, but that seems quite difficult to resolve. The 3x + 1 Conjecture Let T be the transformation that sends an even integer x to x /2 and an odd integer x to 3x + 1. A famous conjecture, sometimes known as the 3x + 1 conjecture, states that for all positive integers x, when we repeatedly apply the transformation T, we will eventually reach the integer 1. For example, starting with x = 13, we find T(13) = 3 . 13 + 1 = 40, T(40) = 40/2 = 20, T(20) = 20/2 = 10, T(10) = 10/2 = 5, T(5) = 3 ·5+ 1 = 16, T(16) = 8, T(8) = 4, T(4) = 2, and T(2) = 1. The 3x + 1 conjecture has been verified for all integers x up to 5.6·1013. 102 I / The Foundations: Logic and Proofs 1-102 Exercises The 3x + 1 conjecture has an interesting history and has attracted the attention of math­ ematicians since the 1950s. The conjecture has been raised many times and goes by many other names, including the Collatz problem, Hasse's algorithm, Ulam's problem, the Syracuse problem, and Kakutani's problem. Many mathemati cians have been diverted from their work to spend time attacking this conjecture. This led to the joke that this problem was part of a conspiracy to slow down American mathematical research. See the article by Jeffrey Lagarias [La85] for a fascinating discussion of this problem and the results that have been found by mathematicians attacking it. .... In Chapter 3 we will describe additional open questions about prime numbers. Students already familiar with the basic notions about primes might want to explore Section 3. 4, where these open questions are discussed. We will mention other important open questions throughout the b09k. Additional Proof Methods In this chapter we introduced the basic methods used in proofs. We also described how to leverage these methods to prove a variety of results. We will use these proof methods in Chapters 2 and 3 to prove results about sets, functions, algorithms, and number theory. Among the theorems we will prove is the famous halting theorem which states that there is a problem that cannot be solved using any procedure. However, there are many important proof methods besides those we have covered. We will introduce some of these methods later in this book. In particular, in Section 4.1 we will discuss mathematical induction, which is an extremely useful method for proving statements of the form VnP(n), where the domain consists of all positive integers. In Section 4.3 we will introduce structural induction, which can be used to prove results about recursively defined sets. We will use the Cantor diagonalization method, which can be used to prove results about the size of infinite sets, in Section 2.4. In Chapter 5 we will introduce the notion of combinatorial proofs, which can be used to prove results by counting arguments. The reader should note that entire books have been devoted to the activities discussed in this section, including many excellent works by George P6lya ([P061], [P071], [P090]). Finally, note that we have not given a procedure that can be used for proving theorems in mathemati cs. It is a deep theorem of mathematical logic that there is no such procedure. 1. Prove that n2 + I ::: 2n when n is a positive integer with 1 :::: n :::: 4. 6. Prove that there is a positive integer that equals the sum of the positive integers not exceeding it. Is your proof constructive or nonconstructive? 2. Prove that there are no positive perfect cubes less 1000 that are the sum ofthe cubes of two positive integers. 3. Prove that if x and y are real numbers, then max(x, y) + min(x, y) = x + y. [Hint: Use a proof by cases, with the two cases corresponding to x ::: y and x < y, respectively. ] 4. Use a proof by cases to show that min(a, min(b, c» = min(min(a , b), c) whenever a, b, and c are real numbers. 5. Prove the triangle inequality, which states that if x and y are real numbers, then Ixl + Iyl ::: Ix + yl (where Ixl rep­ resents the absolute value of x, which equals x if x ::: 0 and equals -x if x < 0). 7. Prove that there are 100 consecutive positive integers that are not perfect squares. Is your proof constructive or non­ constructive? 8. Prove that either 2· 10500 + 15 or 2· 10500 + 16 is not a perfect square. Is your proof constructive or nonconstructive? 9. Prove that there exists a pair of consecutive integers such that one of these integers is a perfect square and the other is a perfect cube. 10. Show that the product of two of the numbers 651000 - 82001 + 3177,791212 _ 92399 + 22001, and 244493 -58192 + 1-103 71777 is nonnegative. Is your proof constructive or noncon­ structive? [Hint: Do not try to evaluate these numbers!] 11. Prove or disprove that there is a rational number x and an irrational number y such that xY is irrational. 12. Prove or disprove that if a and b are rational numbers, then ab is also rational. 13. Show that each of these statements can be used to ex­ press the fact that there is a unique element x such that P(x) is true. [Note that we can also write this statement as 3!xP(x).] a) 3xVy(P(y) ++ x = y) b) 3xP(x) /\ VxVy(P(x) /\ P(y) -+ X = y) c) 3x(P(x) /\ Vy(P(y) -+ x = y» 14. Show that if a, b, and c are real numbers and a =I 0, then there is a unique solution of the equation ax + b = c. 15. Suppose that a and b are odd integers with a =I b. Show there is a unique integer c such that la -cl = Ib -cl. 16. Show that if r is an irrational number, there is a unique integer n such that the distance between r and n is less than 1/2. 17. Show that if n is an odd integer, then there is a unique integer k such that n is the sum of k - 2 and k + 3. 18. Prove that given a real number x there exist unique num­ bers n and E such that x = n + E, n is an integer, and O�E<l. 19. Prove that given a real number x there exist unique num­ bers nand E such that x = n -E, n is an integer, and O �E<l. 20. Use forward reasoning to show that if x is a nonzero real number, then x2 + l/x2 :::: 2. [Hint: Start with the inequality (x -l/x)2 :::: 0 which holds for all nonzero real numbers x .] 21. The harmonic mean of two real numbers x and y equals 2xy /(x + y). By computing the harmonic and geometric means of different pairs of positive real numbers, formu­ late a conjecture about their relative sizes and prove your conjecture. 22. The quadratic mean of two real numbers x and y equals J(x2 + y2)/2. By computing the arithmetic and quadratic means of different pairs of positive real num­ bers, formulate a conjecture about their relative sizes and prove your conjecture. *23. Write the numbers 1,2, ... , 2n on a blackboard, where n is an odd integer. Pick any two of the numbers, j and k, write Ij -kl on the board and erase j and k. Continue this process until only one integer is written on the board. Prove that this integer must be odd. *24. Suppose that five ones and four zeros are arranged around a circle. Between any two equal bits you insert a 0 and between any two unequal bits you insert a 1 to produce nine new bits. Then you erase the nine original bits. Show that when you iterate this procedure, you can never get nine zeros. [Hint: Work backward, assuming that you did end up with nine zeros.] 1.7 Proof Methods and Strategy 103 25. Formulate a conjecture about the decimal digits that ap­ pear as the final decimal digit of the fourth power of an integer. Prove your conjecture using a proof by cases. 26. Formulate a conjecture about the final two decimal digits of the square of an integer. Prove your conjecture using a proof by cases. 27. Prove that there is no positive integer n such that n2 + n3 = too. 28. Prove that there are no solutions in integers x and y to the equation 2x2 + 5y2 = 14. 29. Prove that there are no solutions in positive integers x and y to the equation x4 + y4 = 625. 30. Prove that there are infinitely many solutions in posi­ tive integers x, y, and z to the equation x2 + y2 = z2. [Hint: Let x = m2 - n2, y = 2mn, and z = m2 + n2, where m and n are integers.] 31. Adapt the proof in Example 4 in Section 1.6 to prove that if n = abc, where a, b, and c are positive integers, then a � �,b � �,orc ��. 32. Prove that � is irrational. 33. Prove that between every two rational numbers there is an irrational number. 34. Prove that between every rational number and every irra­ tional number there is an irrational number. *35. Let S = xlYI + X2Y2 + ... + XnYn, where XI, X2, ... , Xn and YI, Y2, .•. , Yn are orderings of two different se­ quences of positive real numbers, each containing n elements. a) Show that S takes its maximum value over all order­ ings of the two sequences when both sequences are sorted (so that the elements in each sequence are in nondecreasing order). b) Show that S takes its minimum value over all order­ ings ofthe two sequences when one sequence is sorted into nondecreasing order and the other is sorted into nonincreasing order. 36. Prove or disprove that if you have an 8-gallon jug of water and two empty jugs with capacities of 5 gal­ lons and 3 gallons, respective ly, then you can measure 4 gallons by successively pouring some of or all of the water in a jug into another jug. 37. Verify the 3x + 1 conjecture for these integers. a) 6 b) 7 c) 17 d) 21 38. Verify the 3x + 1 conjecture for these integers. a) 16 b) 11 c) 35 d) 113 39. Prove or disprove that you can use dominoes to tile the standard checkerboard with two adjacent comers removed (that is, comers that are not opposite). 40. Prove or disprove that you can use dominoes to tile a stan­ dard checkerboard with all four comers removed. 41. Prove that you can use dominoes to tile a rectangular checkerboard with an even number of squares. 42. Prove or disprove that you can use dominoes to tile a 5 x 5 checkerboard with three comers removed. 104 1/ The Foundations: Logic and Proofs 43. Use a proof by exhaustion to show that a tiling using dominoes of a 4 x 4 checkerboard with opposite cor­ ners removed does not exist. [Hint: First show that you can assume that the squares in the upper left and lower right corners are removed. Number the squares of the original checkerboard from 1 to 16, starting in the first row, moving right in this row, then starting in the left­ most square in the second row and moving right, and so on. Remove squares 1 and 16. To begin the proof, note that square 2 is covered either by a domino laid horizontally, which covers squares 2 and 3, or vertically, which covers squares 2 and 6. Consider each of these cases separately, and work through all the subcases that arise.] *44. Prove that when a white square and a black square are removed from an 8 x 8 checkerboard (colored as in the text) you can tile the remaining squares of the checkerboard using dominoes. [Hint: Show that when one black and one white square are removed, each part of the partition of the remaining cells formed by insert­ ing the barriers shown in the figure can be covered by dominoes.] 45. Show that by removing two white squares and two black squares from an 8 x 8 checkerboard (colored as in the text) you can make it impossible to tile the remaining squares using dominoes. *46. Find all squares, if they exist, on an 8 x 8 checkerboard so that the board obtained by removing one of these square can be tiled using straight triominoes. [Hint: First use Key Terms and Results TERMS proposition: a statement that is true or false propositional variable: a variable that represents a proposition truth value: true or false ...., p (negation of p): the proposition with truth value opposite to the truth value of p logical operators: operators used to combine propositions compound propositi on: a proposition constructed by combin­ ing propositions using logical operators truth table : a table displaying the truth values of propositions p V q (disjunction ofp and q): the proposition "p or q ," which is true if and only if at least one of p and q is true p /\ q (conjunction of p and q): the proposition "p and q" which is true if and only ifboth p and q are true p EB q (exclusive or of p and q): the proposition "p XOR q" which is true when exactly one of p and q is true p -q (p implies q): the proposition "if p, then q ," which is false if and only if p is true and q is false converse of p _ q: the conditional statement q � p contrapos itive of p _ q: the conditional statement ""'q �""'p 1-104 Figure for Exercise 44. arguments based on coloring and rotations to eliminate as many squares as possible from consideration.] *47. a) Draw each of the five different tetrominoes, where a tetromino is a polyomino consisting of four squares. b) For each of the five different tetrominoes, prove or disprove that you can tile a standard checkerboard using these tetrominoes. *48. Prove or disprove that you can tile a lOx 10 checkerboard using straight tetrominoes. inverse of p -q: the conditional statement ""'p � ""'q p � q (biconditional): the proposition "p if and only if q ," which is true if and only if p and q have the same truth value bit: either a 0 or a 1 Boolean variable: a variable that has a value of 0 or 1 bit operation: an operation on a bit or bits bit string: a list of bits bitwise operations: operations on bit strings that operate on each bit in one string and the corresponding bit in the other string tautology: a compound proposition that is always true contradicti on: a compound proposition that is always false contingency: a compound proposition that is sometimes true and sometimes false consistent compound propositions: compound propositions for which there is an assignment of truth values to the vari­ ables that makes all these propositions true logically equivalent compound propositi ons: compound propositions that always have the same truth values 1-105 predicate: part of a sentence that attributes a property to the subject propositional function: a statement containing one or more variables that becomes a proposition when each of its vari­ ables is assigned a value or is bound by a quantifier domain (or universe) of discourse: the values a variable in a propositional function may take 3x P(x) (existential quantification of P(x»: the proposition that is true if and only if there exists an x in the domain such that P(x) is true 'v'xP(x) (universal quantification of P(x»: the proposition that is true if and only if P(x) is true for every x in the domain logically equivalent expressions: expressions that have the same truth value no matter which propositional functions and domains are used free variable: a variable not bound in a propositional function bound variable: a variable that is quantified scope of a quantifier: portion of a statement where the quan­ tifier binds its variable argument: a sequence of statements argument form: a sequence of compound propositions involv­ ing propositional variables premise: a statement, in an argument, or argument form, other than the final one conclusion: the final statement in an argument or argument form valid argument form: a sequence of compound propositions involving propositional variables where the truth of all the premises implies the truth of the conclusion valid argument: an argument with a valid argument form rule of inference: a valid argument form that can be used in the demonstration that arguments are valid fallacy: an invalid argument form often used incorrectly as a rule of inference (or sometimes, more generally, an incor­ rect argument) circular reasoning or begging the question: reasoning where one or more steps are based on the truth of the statement being proved theorem: a mathematical assertion that can be shown to be true conjecture: a mathematical assertion proposed to be true, but that has not been proved Review Questions 1. a) Define the negation of a proposition. b) What is the negation of "This is a boring course"? 2. a) Define (using truth tables) the disjunction, conjunc­ tion, exclusive or, conditional, and biconditional of the propositions p and q. b) What are the disjunction, conjunction, exclusive or, conditional, and biconditional ofthe propositions "I'll go to the movies tonight" and "I'll finish my discrete mathematics homework"? Review Questions 105 proof: a demonstration that a theorem is true axiom: a statement that is assumed to be true and that can be used as a basis for proving theorems lemma: a theorem used to prove other theorems corollary: a proposition that can be proved as a consequence of a theorem that has just been proved vacuous proof: a proof that p � q is true based on the fact that p is false trivial proof: a proofthat p � q is true based on the fact that q is true direct proof: a proofthat p � q is true that proceeds by show­ ing that q must be true when p is true proof by contrapositio n: a proof that p � q is true that pro­ ceeds by showing that p must be false when q is false proof by contradiction: a proof that p is true based on the truth of the conditional statement �p � q, where q is a contradiction exhaustive proof: a proof that establishes a result by checking a list of all cases proof by cases: a proof broken into separate cases where these cases cover all possibilities without loss of generality: an assumption in a proof that makes it possible to prove a theorem by reducing the number of cases needed in the proof counterexample: an element x such that P (x) is false constructive existence proof: a proof that an element with a specified property exists that explicitly finds such an ele­ ment nonconstru ctive existence proof: a proof that an element with a specified property exists that does not explicitly find such an element rational number: a number that can be expressed as the ratio of two integers p and q such that q # 0 uniqueness proof: a proof that there is exactly one element satisfying a specified property RESULTS The logical equivalences given in Tables 6, 7, and 8 in Sec- tion 1.2. De Morgan's laws for quantifiers. Rules of inference for propositional calculus. Rules of inference for quantified statements. 3. a) Describe at least five different ways to write the con­ ditional statement p � q in English. b) Define the converse and contrapositive of a conditional statement. c) State the converse and the contrapositive of the con­ ditional statement "If it is sunny tomorrow, then I will go for a walk in the woods." 4. a) What does it mean for two propositions to be logically equivalent? 106 1 I The Foundations: Logic and Proofs b) Describe the different ways to show that two com­ pound propositions are logically equivalent. c) Show in at least two different ways that the compound propositions �P v (r -+ �q) and �P v �q V �r are equivalent. 5. (Depends on the Exercise Set in Section 1.2) a) Given a truth table, explain how to use disjunctive nor­ mal form to construct a compound proposition with this truth table. b) Explain why part (a) shows that the operators /\, v, and � are functionally complete. c) Is there an operator such that the set containing just this operator is functionally complete? 6. What are the universal and existential quantifications of a predicate P(x)? What are their negations? 7. a) What is the difference between the quantification 3xVyP(x,y) and Vy3xP(x,y), where P(x,y) is a predicate? b) Give an example of a predicate P (x, y) such that 3xVyP(x, y) and Vy3xP(x, y) have different truth values. 8. Describe what is meant by a valid argument in proposi­ tional logic and show that the argument "If the earth is flat, then you can sail off the edge of the earth," "You can­ not sail off the edge of the earth," therefore, "The earth is not flat" is a valid argument. 9. Use rules of inference to show that if the premises "All zebras have stripes" and "Mark is a zebra" are true, then the conclusion "Mark has stripes" is true. Supplementary Exercises 1. Let P be the proposition "I will do every exercise in this book" and q be the proposition "I will get an ''I>:' in this course." Express each of these as a combination of P andq. a) I will get an "Pl' in this course only if! do every exer­ cise in this book. b) I will get an "Pl' in this course and I will do every exercise in this book: c) Either I will not get an "Pl' in this course or I will not do every exercise in this book. d) For me to get an "A" in this course it is necessary and sufficient that I do every exercise in this book. 2. Find the truth table of the compound proposition (p v q) -+ (p /\ �r). 3. Show that these compound propositions are tautologies. a) (�q /\ (p -+ q» -+ �P b) «pvq)/\�p)-+ q 4. Give the converse, the contrapositive, and the inverse of these conditional statements. a) If it rains today, then I will drive to work. b) Iflxl = x, then x ::: O. c) If n is greater than 3, then n2 is greater than 9. 1-106 10. a) Describe what is meant by a direct proof, a proof by contraposition, and a proof by contradiction ofa con­ ditional statement P -+ q. b) Give a direct proof, a proof by contraposition and a proof by contradiction of the statement: "If n is even, then n + 4 is even." 11. a) Describe a way to prove the biconditional P # q. b) Prove the statement: "The integer 3n + 2 is odd if and only if the integer 9n + 5 is even, where n is an integer." 12. To prove that the statements PI, P2, P3, and P4 are equivalent, is it sufficient to show that the conditional statements P4 -+ P2, P3 -+ PI, and PI -+ P2 are valid? If not, provide another set of conditional statements that can be used to show that the four statements are equivalent. 13. a) Suppose that a statement ofthe form VxP(x) is false. How can this be proved? b) Show that the statement "For every positive integer n, n2 ::: 2n" is false. 14. What is the difference between a constructive and nonconstructive existence proof? Give an example of each. 15. What are the elements of a proof that there is a unique element x such that P(x), where P(x) is a propositional function? 16. Explain how a proof by cases can be used to prove a result about absolute values, such as the fact that Ixyl = Ixllyl for all real numbers x and y. 5. Given a conditional statement P -+ q, find the converse of its inverse, the converse of its converse, and the con­ verse of its contrapositive. 6. Given a conditional statement p -+ q, find the inverse of its inverse, the inverse of its converse, and the inverse of its contrapositive. 7. Find a compound proposition involving the propositional variables p, q, r, and s that is true when exactly three of these propositional variables are true and is false otherwise. 8. Show that these statements are inconsistent: "If Sergei takes the job offer then he will get a signing bonus." "If Sergei takes the job offer, then he will receive a higher salary." "If Sergei gets a signing bonus, then he will not receive a higher salary." "Sergei takes the job offer." 9. Show that these statements are inconsistent: "If Miranda does not take a course in discrete mathema tics, then she will not graduate." "If Miranda does not graduate, then she is not qualified for the job." "If Miranda reads this book, then she is qualified for the job." "Miranda does not take a course in discrete mathematics but she reads this book." I-I 1!7 10. Suppose that you meet three people, A, B, and C, on the island of knights and knaves described in Example 18 in Section 1.1. What are A, B, and C if A says "I am a knave and B is a knight" and B says "Exactly one of the three of us is a knight"? 11. (Adapted from [Sm78]) Suppose that on an island there are three types of people, knights, knaves, and normals. Knights always tell the truth, knaves always lie, and nor­ mals sometimes lie and sometimes tell the truth. Detec­ tives questioned three inhabitants of the island-Amy, Brenda, and Claire-as part of the investigation of a crime. The detectives knew that one ofthe three commit­ ted the crime, but not which one. They also knew that the criminal was a knight, and that the other two were not. Ad­ ditionally, the detectives recorded these statements: Amy: "I am innocent." Brenda: "What Amy says is true." Claire: "Brenda is not a normal." After analyzing their informa­ tion, the detectives positively identified the guilty party. Who was it? 12. Show that if S is a proposition, where S is the condi­ tional statement "If S is true, then unicorns live," then "Unicorns live" is true. Show that it follows that S can­ not be a proposition. (This paradox is known as Lob s paradox.) 13. Show that the argument with premises "The tooth fairy is a real person" and "The truth fairy is not a real person" and conclusion "You can find gold at the end of the rainbow" is a valid argument. Does this show that the conclusion is true? 14. Let P(x) be the statement "student x knows calculus" and let Q(y) be the statement "class y contains a student who knows calculus." Express each ofthese as quantifications of P(x) and Q(y). a) Some students know calculus. b) Not every student knows calculus. c) Every class has a student in it who knows calculus. d) Every student in every class knows calculus. e) There is at least one class with no students who know calculus. 15. Let P(m, n) be the statement "m divides n," where the domain for both variables consists of all positive inte­ gers. (By "m divides n" we mean that n = km for some integer k.) Determine the truth values of each of these statements. a) P(4,5) c) "1m "In P(m, n) e) 3n "1m P(m, n) b) P(2,4) d) 3m "In P(m, n) 1) VnP(I,n) 16. Find a domain for the quantifiers in 3x3y(x =1= y 1\ Vz«z =1= x) 1\ (z =1= y» such that this statement is true. 17. Find a domain for the quantifiers in 3x3y(x =1= y 1\ Vz«z = x) V (z = y») such that this statement is false. 18. Use existential and universal quantifiers to express the statement "No one has more than three grandmothers" us-Supplementary Exercises 107 ing the propositional function G(x, y), which represents "x is the grandmother of y." 19. Use existential and universal quantifiers to express the statement "Everyone has exactly two biological parents" using the propositional function P(x, y), which repre­ sents "x is the biological parent of y." 20. The quantifier 3 n denotes "there exists exactly n ," so that 3nx P (x) means there exist exactly n values in the do­ main such that P(x) is true. Determine the true value of these statement where the domain consists of all real numbers. a) 30x(xz = -1) c) 3zx(xz = 2) b) 3\x(lxl = 0) d) 33x(x = Ix I) 21. Express each of these statements using existential and universal quantifiers and propositional logic where 3n is defined in Exercise 20. a) 30xP(x) b) 3\xP(x) c) 3zxP(x) d) 33XP(X) 22. Let P(x, y) be a propositional function. Show that 3x Vy P(x, y) --+ Vy 3x P(x, y) is a tautology. 23. Let P(x) and Q(x) be propositional functions. Show that 3x (P(x) --+ Q(x»and "Ix P(x) --+ 3x Q(x) always have the same truth value. 24. If Vy 3x P(x, y) is true, does it necessarily follow that 3x Vy P(x, y) is true? 25. If "Ix 3y P(x, y) is true, does it necessarily follow that 3x Vy P(x, y) is true? 26. Find the negations of these statements. a) If it snows today, then I will go skiing tomorrow. b) Every person in this class understands mathematical induction. c) Some students in this class do not like discrete math­ ematics. d) In every mathematics class there is some student who falls asleep during lectures. 27. Express this statement using quantifiers: "Every student in this class has taken some course in every department in the school of mathematical sciences." 28. Express this statement using quantifiers: "There is a build­ ing on the campus of some college in the United States in which every room is painted white." 29. Express the statement "There is exactly one student in this class who has taken exactly one mathematics class at this school" using the uniqueness quantifier. Then express this statement using quantifiers, without using the uniqueness quantifier. 30. Describe a rule of inference that can be used to prove that there are exactly two elements x and y in a domain such that P(x) and P(y) are true. Express this rule of inference as a statement in English. 31. Use rules of inference to show that if the premises Vx(P(x) --+ Q(x», Vx(Q(x) --+ R(x», and -.R(a), where a is in the domain, are true, then the conclusion -'P(a) is true. 108 1/ The Foundations: Logic and Proofs 32. Prove that if x3 is irrational, then x is irrational. 33. Prove that if x is irrational and x � 0, then .jX is irra­ tional. 34. Prove that given a nonnegative integer n, there is a unique nonnegative integer m such that m2 � n < (m + 1)2. 35. Prove that there exists an integer m such that m 2 > 10 1000 . Is your proof constructive or nonconstructive? 36. Prove that there is a positive integer that can be written· as the sum of squares of positive integers in two differ­ ent ways. (Use a computer or calculator to speed up your work.) Computer Projects Write programs with the specified input and output. 1. Given the truth values of the propositions p and q, find the truth values of the conjunction, disjunction, exclusive or, conditional statement, and biconditional ofthese proposi­ tions. 2. Given two bit strings of length n, find the bitwise AND, bitwise OR, and bitwiseXOR of these strings. 3. Given the truth values of the propositions p and q in fuzzy logic, find the truth value of the disjunction and the con- Computations and Explorations /-/08 37. Disprove the statement that every positive integer is the sum of the cubes of eight nonnegative integers. 38. Disprove the statement that every positive integer is the sum of at most two squares and a cube of nonnegative integers. 39. Disprove the statement that every positive integer is the sum of 36 fifth powers of nonnegative integers. 40. Assuming the truth of the theorem that states that ...;n is irrational whenever n is a positive integer that is not a perfect square, prove that ..fi + .../3 is irrational. junction of p and q (see Exercises 40 and 41 of Section 1.1). *4. Given positive integers m and n, interactively play the game of Chomp. * 5. Given a portion of a checkerboard, look for tilings of this checkerboard with various types of pol yomi noes, including dominoes, the two types oftriominoes, and largerpolyomi­ noes. Use a computational program or programs you have written to do these exercises. 1. Look for positive integers that are not the sum of the cubes of nine different positive integers. 2. Look for positive integers greater than 79 that are not the sum of the fourth powers of 18 positive integers. 3. Find as many positive integers as you can that can be writ- Writing Proj ects Respond to these with essa ys using outside sources. 1. Discuss logical paradoxes, including the paradox of Epi­ menides the Cretan, Jourdain's card paradox, and the bar­ ber paradox, and how they are resolved. 2. Describe how fuzzy logic is being applied to practical ap­ plications. Consult one or more of the recent books on fuzzy logic written for general audiences. 3. Describe the basic rules of WFF'N PROOF, The Game of Modern Logic, developed by Layman Allen. Give examples of some of the games included in WFF'N PROOF. ten as the sum of cubes of positive integers, in two different ways, sharing this property with 1729. *4. Try to find winning strategies for the game of Chomp for different initial configurations of cookies. *5. Look for tilings of checkerboards and parts of checker­ boards with polynominoes. 4. Read some of the writings of Lewis Carroll on symbolic logic. Describe in detail some of the models he used to represent logical arguments and the rules of inference he used in these arguments. 5. Extend the discussion of Prolog given in Section 1.3, ex­ plaining in more depth how Prolog employs resolution. 6. Discuss some of the techniques used in computational logic, including Skolem's rule. 7. ''Automated theorem proving" is the task of using com­ puters to mechanically prove theorems. Discuss the goals 1-109 and applications of automated theorem proving and the progress made in developing automated theorem provers. 8. Describe how DNA computing has been used to solve instances of the satisfiability problem. 9. Discuss what is known about winning strategies in the game of Chomp. Writing Projects 109 10. Describe various aspects of proof strategy discussed by George P6lya in his writings on reasoning, including [P062], [P071], and [P090]. 11. Describe a few problems and results about tilings with polyominoes, as described in [G094] and [Ma91], for example. CHAPTER 2.1 Sets 2.2 Set Operations 2.3 Functions 2.4 Sequences and Summations 2.1 Sets Basic Structures: Sets, Functions, Sequences, and Sums M uch of discrete mathematics is devoted to the study of discrete structure s, used to represent discrete objects. Many important discrete structures are built using sets, which are collections of objects. Among the discrete structures built from sets are combina tions, unordered collections of objects used extensively in counting; relations, sets of ordered pairs that represent relationships between objects; graphs, sets of vertices and edges that connect vertices; and finite state machine s, used to model computing machine s. These are some of the topics we will study in later chapters. The concept of a function is extremely important in discrete mathema tics. A function assigns to each element of a set exactly one element of a set. Functions play important roles throughout discrete mathemati cs. They are used to represent the computational complex ity of algorithm s, to study the size of sets, to count objects, and in a myriad of other ways. Useful structures such as sequenc es and strings are special types of functions. In this chapter, we will introduce the notion of a sequenc e, which represents ordered lists of elements. We will introduce some important types of sequences, and we will address the problem of identifying a pattern for the terms of a sequence from its first few terms. Using the notion of a sequence, we will define what it means for a set to be countable, namely, that we can list all the elements of the set in a sequence. In our study of discrete mathema tics, we will often add consecu tive terms of a sequence of numbers. Because adding terms from a sequenc e, as well as other indexed sets of numbers, is such a common occurrenc e, a special notation has been developed for adding such terms. In this section, we will introduce the notation used to express summations. We will develop formulae for certain types of summati ons. Such summations appear throughout the study of discrete mathema tics, as, for instance, when we analyze the number of steps a procedure uses to sort a list of numbers into increasing order. Introduction In this section, we study the fundamental discrete structure on which all other discrete structures are built, namely, the set. Sets are used to group objects together. Often, the objects in a set have similar properti es. For instance, all the students who are currently enrolled in your school make up a set. Likewise, all the students currently taking a course in discrete mathematics at any school make up a set. In addition, those students enrolled in your school who are taking a course in discrete mathematics form a set that can be obtained by taking the elements common to the first two collections. The language of sets is a means to study such collections in an organized fashion. We now provide a definition of a set. This definition is an intuitive definition, which is not part of a formal theory of sets. DEFINITION 1 A set is an unordered collection of objects. Note that the term object has been used without specifying what an object is. This description of a set as a collection of objects, based on the intuitive notion of an object, was first stated Lijfill�m by the German mathematician Georg Cantor in 1895. The theory that results from this intuitive 2-/ 111 112 2 / Basic Structures: Sets, Functions, Sequences, and Sums 2-2 Assessmenl � DEFINITION 2 definition of a set, and the use of the intuitive notion that any property whatever there is a set consisting of exactly the objects with this property, leads to paradoxes, or logical inconsistenc ies. This was shown by the English philosopher Bertrand Russell in 1902 (see Exercise 38 for a description of one of these paradoxes). These logical inconsistencies can be avoided by building set theory beginning with axioms. We will use Cantor's original version of set theory, known as naive set theory, without developing an axiomatic version of set theory, because all sets considered in this book can be treated consistently using Cantor's original theory. The objects in a set are called the elements, or members, of the set. A set is said to contain its elements. We will now introduce notation used to describe membership in sets. We write a E A to denote that a is an element of the set A. The notation a f/ A denotes that a is not an element of the set A. Note that lowercase letters are usually used to denote elements of sets. There are several ways to describe a set. One way is to list all the members of a set, when this is possible. We use a notation where all members of the set are listed between braces. For example, the notation {a, b, c, d} represents the set with the four elements a, b, c, and d. EXAMPLE 1 The set V of all vowels in the English alphabet can be written as V = {a, e, i, 0, u}. .... EXAMPLE 2 The set 0 of odd positive integers less than 10 can be expressed by 0 ,= {l, 3, 5, 7, 9}. EXAMPLE 3 Although sets are usually used to group together elements with common properties, there is nothing that prevents a set from having seemingly unrelated elements. For instance, {a, 2, Fred, New Jersey} is the set containing the four elements a, 2, Fred, and New Jersey. .... Sometimes the brace notation is used to describe a set without listing all its members. Some members of the set are listed, and then ellipses ( ... ) are used when the general pattern of the elements is obvious. EXAMPLE 4 The set of positive integers less than 100 can be denoted by {I, 2, 3, ... , 99}. Extra� Examples � Another way to describe a set is to use set builder notation. We characterize all those elements in the set by stating the property or properties they must have to be members. For instance, the set 0 of all odd positive integers less than 10 can be written as o = {x I x is an odd positive integer less than 1O}, or, specifying the universe as the set of positive integers, as o = {x E Z+ I x is odd and x < 1O}. We often use this type of notation to describe sets when it is impossible to list all the elements of the set. For instance, the set Q+ of all positive rational numbers can be written as Q+ = {x E R I x = p / q, for some positive integers p and q. These sets, each denoted using a boldface letter, play an important role in discrete mathemati cs: N = {O, 1,2,3, ... }, the set of natural numbers Z = { ... , -2, -1,0,1,2, ... }, the set of integers 2-3 z+ = {I, 2, 3, ... }, the set of positive integers Q = {p/q I p E Z, q E Z, and q =I=-OJ, the set of rational numbers R, the set of real numbers 2.1 Sets 113 (Note that some people do not consider 0 a natural number, so be careful to check how the term natural numbers is used when you read other books.) Sets can have other sets as members, as Example 5 illustrates. EXAMPLE 5 The set {N, Z, Q, R} is a set containing four elements, each of which is a set. The four elements of this set are N, the set of natural numbers; Z, the set of integers; Q, the set of rational numbers; and R, the set of real numbers. ..... Remark: Note that the concept of a datatype, or type, in computer science is built upon the concept of a set. In particular, a datatype or type is the name of a set, together with a set of operations that can be performed on objects from that set. For example, boolean is the name of the set {O, I} together with operators on one or more elements of this set, such as AND, OR, and NOT. Because many mathematical statements assert that two differently specified collections of objects are really the same set, we need to understand what it means for two sets to be equal. DEFINITION 3 Two sets are equal if and only if they have the same elements. That is, if A and B are sets, then A and B are equal if and only ifYx(x E A ++ x E B). We write A = B if A and Bare equal sets. EXAMPLE 6 The sets {l, 3, 5} and {3, 5, I} are equal, because they have the same elements. Note that the order in which the elements of a set are listed does not matter. Note also that it does not matter if an element of a set is listed more than once, so {I, 3, 3, 3, 5, 5, 5, 5} is the same as the set {l, 3, 5} because they have the same elements. ..... Sets can be represented graphically using Venn diagrams, named after the English mathe­ matician John Venn, who introduced their use in 1881. In Venn diagrams the universal set U, which contains all the objects under consideration, is represented by a rectangle. (Note that the universal set varies depending on which objects are of interest.) Inside this rectangle, circles or other geometrical figures are used to represent sets. Sometimes points are used to represent the particular elements of the set. Venn diagrams are often used to indicate the relationships between sets. We show how a Venn diagram can be used in Example 7. GEORG CANTOR (1845-1918) Georg Cantor was born in St. Petersburg, Russia, where his father was a successful merchant. Cantor developed his interest in mathematics in his teens. He began his university studies in Zurich in 1862, but when his father died he left Zurich. He continued his university studies at the University of Berlin in 1863, where he studied under the eminent mathematicians Weierstrass, Kummer, and Kronecker. He received his doctor's degree in 1867, after having written a dissertation on number theory. Cantor assumed a position at the University of Halle in 1869, where he continued working until his death. Cantor is considered the founder of set theory. His contributions in this area include the discovery that the set of real numbers is uncountable. He is also noted for his many important contributions to analysis. Cantor also was interested in philosophy and wrote papers relating his theory of sets with metaphysics. Cantor married in 1874 and had five children. His melancholy temperament was balanced by his wife's happy disposition. Although he received a large inheritance from his father, he was poorly paid as a professor. To mitigate this, he tried to obtain a better-paying position at the University of Berlin. His appointment there was blocked by Kronecker, who did not agree with Cantor's views on set theory. Cantor suffered from mental illness throughout the later years of his life. He died in 1918 in a psychiatric clinic. 114 2 / Basic Structure s: Sets, Functions, Sequences, and Sums 2-4 • o a • v u FIGURE 1 Venn Diagram for the Set of Vowels. EXAMPLE 7 Draw a Venn diagram that represents V, the set of vowels in the English alphabet. DEFINITION 4 unkS� Solution: We draw a rectangle to indicate the universal set U, which is the set of the 26 letters of the English alphabet. Inside this rectangle we draw a circle to represent V. Inside this circle we indicate the elements of V with points (see Figure 1). ... There is a special set that has no elements. This set is called the empty set, or null set, and is denoted by 0. The empty set can also be denoted by { } (that is, we represent the empty set with a pair of braces that encloses all the elements in this set). Often, a set of elements with certain properties turns out to be the null set. For instance, the set of all positive integers that are greater than their squares is the null set. A set with one element is called a singleton set. A common error is to confuse the empty set 0 with the set {0}, which is a singleton set. The single element of the set {0} is the empty set itself1 A useful analogy for remembering this difference is to think of folders in a computer file system. The empty set can be thought of as an empty folder and the set consisting of just the empty set can be thought of as a folder with exactly one folder inside, namely, the empty folder. The set A is said to be a subset of B if and only if every element of A is also an element of B. We use the notation A 5; B to indicate that A is a subset of the set B. We see that A 5; B if and only if the quantification Yx(x E A --+ x E B) is true. BERTRAND RUSSELL (1872-1970) Bertrand Russell was born into a prominent English family active in the progressive movement and having a strong commitment to liberty. He became an orphan at an early age and was placed in the care of his father's parents, who had him educated at home. He entered Trinity College, Cambridge, in 1890, where he excelled in mathematics and in moral science. He won a fellowship on the basis of his work on the foundations of geometry. In 1910 Trinity College appointed him to a lectureship in logic and the philosophy of mathema tics. Russell fought for progressive causes throughout his life. He held strong pacifist views, and his protests against World War I led to dismissal from his position at Trinity College. He was imprisoned for 6 months in 1918 because of an article he wrote that was branded as seditious. Russell fought for women's suffrage in Great Britain. In 1961, at the age of89, he was imprisoned for the second time for his protests advocating nuclear disarmament. Russell's greatest work was in his development of principles that could be used as a foundation for all of mathema tics. His most famous work is Principia Mathematica, written with Alfred North Whitehead, which attempts to deduce all of mathema tics using a set of primitive axioms. He wrote many books on philosoph y, physics, and his political ideas. Russell won the Nobel Prize for literature in 1950. 2-5 u FIGUR E 2 Venn Diagram Showing that A Is a Subset of B. 2.1 Sets 115 EXAMPLE 8 The set of all odd positive integers less than 10 is a subset of the set of all positive integers less than 10, the set of rational numbers is a subset of the set of real numbers, the set of all computer science majors at your school is a subset of the set of all students at your school, and the set of all people in China is a subset of the set of all people in China (that is, it is a subset of itself). .... THEOREM ! unkS� Theorem 1 shows that every nonempty set S is guaranteed to have at least two subsets, the empty set and the set S itself, that is, 0 � Sand S � S. For every set S, (i) 0 � S and (ii) S � S. Proof: We will prove (i) and leave the proof of (ii) as an exercise. Let S be a set. To show that 0 � S, we must show that Yx(x E 0 -+ XES) is true. Because the empty set contains no elements, it follows that x E 0 is always false. It follows that the conditional statement x E 0 -+ XES is always true, because its hypothesis is always false and a conditional statement with a false hypothesis is true. That is, Yx(x E 0 -+ XES) is true. This completes the proof of (i). Note that this is an example of a vacuous proof. <J When we wish to emphasize that a set A is a subset of the set B but that A =F B, we write A C B and say that A is a proper subset of B. For A C B to be true, it must be the case that A � B and there must exist an element x of B that is not an element of A. That is, A is a proper subset of B if YX(X E A -+ x E B) !\ 3x(x E B !\ x f/ A) is true. Venn diagrams can be used to illustrate that a set A is a subset of a set B. We draw the universal set U as a rectangle. Within this rectangle we draw a circle for B. Because A is a subset of B, we draw the circle for A within the circle for B. This relationship is shown in Figure 2. JOHN VENN (1834-1923) John Venn was born into a London suburban family noted for its philanthropy. He attended London schools and got his mathematics degree from Caius College, Cambridge, in 1857. He was elected a fellow of this college and held his fellowship there until his death. He took holy orders in 1859 and, after a brief stint of religious work, returned to Cambridge, where he developed programs in the moral sciences. Besides his mathematical work, Venn had an interest in history and wrote extensively about his college and family. Venn's book Symbolic Logic clarifies ideas originally presented by Boole. In this book, Venn presents a systematic development of a method that uses geometric figures, known now as foenn diagrams. Today these diagrams are primarily used to analyze logical arguments and to illustrate relationships between sets. In addition to his work on symbolic logic, Venn made contributions to probability theory described in his widely used textbook on that subject. 116 2 / Basic Structure s: Sets, Functions, Sequences, and Sums 2-6 DEFINITION 5 One way to show that two sets have the same elements is to show that each set is a subset of the other. In other words, we can show that if A and B are sets with A S; Band B S; A, then A = B. This turns out to be a useful way to show that two sets are equal. That is, A = B, where A and B are sets, if and only ifYx(x E A � x E B) and Yx(x E B � x E A), or equivalently if and only ifYx(x E A ++ x E B). Sets may have other sets as members. For instance, we have the sets A = {0, {a}, {b}, {a, b}} and B = {x I x is a subset of the set {a, b}}. Note that these two sets are equal, that is, A = B. Also note that {a} E A, but a rI. A. Sets are used extensively in counting problems, and for such applications we need to discuss the size of sets. Let S be a set. Ifthere are exactly n distinct elements in S �re n is a nonnegative integer, we say that S is a finite set and that n is the cardinality of S. The cardinality of S is denoted by I S I. EXAMPLE 9 Let A be the set of odd positive integers less than 10. Then IAI = 5. EXAMPLE 10 Let S be the set ofletiers in the English alphabet. Then lSI = 26. EXAMPLE 11 Because the null set has no elements, it follows that 101 = 0. DEFINITION 6 EXAMPLE 12 We will also be interested in sets that are not finite. A set is said to be infinite if it is not finite. The set of positive integers is infinite. The cardinality of infinite sets will be discussed in Section 2.4. The Power Set Many problems involve testing all combinations of elements of a set to see if they satisfy some property. To consider all such combinations of elements of a set S, we build a new set that has as its members all the subsets of S. DEFINITION 7 Given a set S, the power set of S is the set of all su\?sets of the set S. The power set of S is denoted by P(S). EXAMPLE 13 What is the power set of the set to, 1, 2}? Solution: The power set P({O, I, 2}) is the set of all subsets of to, 1, 2}. Hence, P({O, 1, 2}) = {0, to}, {l}, {2}, to, 1}, to, 2}, {l, 2}, to, 1, 2}}. Note that the empty set and the set itself are members of this set of subsets. 2-7 2.1 Sets 117 EXAMPLE 14 What is the power set ofthe empty set? What is the power set of the set {0}? DEFINITION 8 unks� Solution: The empty set has exactly one subset, namely, itself. Consequently, P(0) = {0}. The set {0} has exactly two subsets, namely, 0 and the set {0} itself. Therefore, P({0}) = {0, {0}}. If a set has n elements, then its power set has 2n elements. We will demonstrate this fact in several ways in subsequent sections of the text. Cartesian Prod uets The order of elements in a collection is often important. Because sets are unordered, a different structure is needed to represent ordered collections. This is provided by ordered n-tuples. The ordered n-tuple (aI, a2, ... , an) is the ordered collection that has al as its first element, a2 as its second element, ... , and an as its nth element. We say that two ordered n-tuples are equal if and only if each corresponding pair of their elements is equal. In other words, (aI, a2, ... , an) = (bl, b2, •.• , bn) if and only if ai = bi, for i = 1,2, ... , n. In particular, 2-tuples are called ordered pairs. The ordered pairs (a, b) and (c, d) are equal if and only if a = c and b = d. Note that (a, b) and (b, a) are not equal unless a =b. Many of the discrete structures we will study in later chapters are based on the notion of the Cartesian product of sets (named after Rene Descartes). We first define the Cartesian product of two sets. RENE DESCARTES (1596-1650) Rene Descartes was born into a noble family near Tours, France, about 200 miles southwest of Paris. He was the third child of his father's first wife; she died several days after his birth. Because of Rene1s poor health, his father, a provincial judge, let his son's formal lessons slide until, at the age of 8, Rene entered the Jesuit college at La Fleche. The rector of the school took a liking to him and permitted him to stay in bed until late in the morning because of his frail health. From then on, Descartes spent his mornings in bed; he considered these times his most productive hours for thinking. Descartes left school in 1612, moving to Paris, where he spent 2 years studying mathema tics. He earned a law degree in 1616 from the University of Poitiers. At 18 Descartes became disgusted with studying and decided to see the world. He moved to Paris and became a successful gambler. However, he grew tired of bawdy living and moved to the suburb of Saint-Germain, where he devoted himself to mathematical study. When his gambling friends found him, he decided to leave France and undertake a military career. However, he never did any fighting. One day, while escaping the cold in an overheated room at a military encampment, he had several feverish dreams, which revealed his future career as a mathematician and philosopher. After ending his military career, he traveled throughout Europe. He then spent several years in Paris, where he studied mathemat­ ics and philosophy and constructed optical instruments. Descartes decided to move to Holland, where he spent 20 years wandering around the country, accomplishing his most important work. During this time he wrote several books, including the Discours, which contains his contributions to analytic geometry, for which he is best known. He also made fundamental contributions to philosophy. In 1649 Descartes was invited by Queen Christina to visit her court in Sweden to tutor her in philosophy. Although he was reluctant to live in what he called "the land of bears amongst rocks and ice," he finally accepted the invitation and moved to Sweden. Unfortunately, the winter of 1649-1650 was extremely bitter. Descartes caught pneumonia and died in mid-February. 118 2 / Basic Structure s: Sets, Functions, Sequences, and Sums 2-8 DEFINITION 9 EXAMPLE 15 E.IItra� Examples llliiiiiil EXAMPLE 16 Let A and B be sets. The Cartesian product of A and B, denoted by A x B, is the set of all ordered pairs (a, b), where a E A and b E B. Hence, A x B = {(a, b) I a E A /\ bE B}. Let A represent the set of all students at a university, and let B represent the set of all courses offered at the university. What is the Cartesian product A x B? Solution: The Cartesian product A x B consists of all the ordered pairs of the form (a, b), where a is a student at the university and b is a course offered at the university. The set A x B can be used to represent all possible enrollments of students in courses at the university. .... What is the Cartesian product of A = {I, 2} and B = {a, b, c}? Solution: The Cartesian product A x B is A x B = {(1, a), (1, b), (1, c), (2, a), (2, b), (2, c)}. A subset R of the Cartesian product A x B is called a relation from the set A to the set B. The elements of R are ordered pairs, where the first element belongs to A and the second to B. For example, R = {(a, 0), (a, 1), (a, 3), (b, 1), (b, 2), (c, 0), (c, 3)} is a relation from the set {a, b, c} to the set to, 1,2, 3}. We will study relations at length in Chapter 8. The Cartesian products A x Band B x A are not equal, unless A = 0 or B ::::;:: 0 (so that A x B = 0) or A = B (see Exercises 26 and 30, at the end of this section). This is illustrated in Example 17. EXAMPLE 17 Show that the Cartesian product B x A is not equal to the Cartesian product A x B, where A and B are as in Example 16. DEFINITION 10 Solution: The Cartesian product B x A is B x A = {(a, I), (a, 2), (b, I), (b, 2), (c, 1), (c, 2)}. This is not equal to A x B, which was found in Example 16. The Cartesian product of more than two sets can also be defined. The Cartesian product of the sets A), A2, ... , An, denoted by A) x A2 X ••• x An, is the set of ordered n-tuples (a), a2, ... , an), where aj belongs to Aj for i = 1,2, ... , n. In other words, A) X A2 x··· x An = {(a),a2, ... ,an) I aj E Ai fori = 1,2, ... ,n}. EXAMPLE 18 What is the Cartesian product A x B x C, where A = to, l}, B = {I, 2}, and C = to, 1, 2}? Solution: The Cartesian product A x B x C consists of all ordered triples (a, b, c), where a E A, bE B, and c E C. Hence, A x B xC = {CO, 1,0), (0, I, I), (0, 1,2), (0, 2, 0), (0, 2, I), (0, 2, 2), (1,1,0), (1,1,1), (1, 1,2), (1, 2, 0), (1, 2,1), (1, 2, 2)}. .... 2-9 2.1 Sets 119 Using Set Notation with Quantifiers Sometimes we restrict the domain of a quantified statement explicitly by making use of a particular notation. For examp le, VXE S (P (x» denotes the universal quantification of P (x) over all elements in the set S. In other words, VXE S(P(x» is shorthand for Vx(x E S -+ P(x». Similarly, 3xE S(P(x» denotes the existential quantification of P(x) over all elements in S. That is, 3XE S(P(x» is shorthand for 3x(x E S 1\ P(x». EXAMPLE 19 What do the statements VXER (x2 ::: 0) and 3XEZ (x2 = 1) mean? Solution: The statement VXE R(x2 ::: 0) states that for every real number x, x2 ::: O. This state­ ment can be expressed as "The square of every real number is nonnegative." This is a true statement. The statement 3XE Z(x2 = 1) states that there exists an integer x such that x2 = 1. This statement can be expressed as "There is an integer whose square is I." This is also a true statement because x = 1 is such an integer (as is -1). -<III Truth Sets of Quantifiers We will now tie together concepts from set theory and from predicate logic. Given a predicate P, and a domain D, we define the truth set of P to be the set of elements x in D for which P(x) is true. The truth set of P(x) is denoted by {x E D I P(x)}. EXAMPLE 20 What are the truth sets of the predicates P(x), Q(x), and R(x), where the domain is the set of integers and P(x) is "Ixl = 1," Q(x) is "x2 = 2," and R(x) is "Ixl = x." Solution: The truth set of P, {x E Z I Ix I = I}, is the set of integers for which Ix I = 1. Because Ix I = 1 when x = 1 or x = -1, and for no other integers x, we see that the truth set of P is the set {-I, I}. The truth set of Q, {x E Z I x 2 = 2}, is the set of integers for which x2 = 2. This is the empty set because there are no integers x for which x2 = 2. The truth set of R, {x E Z I lxl = x}, is the set of integers for which Ixl = x. Because Ixl = x if and only if x ::: 0, it follows that the truth set of R is N, the set of nonnegative integers. -<III Note that "Ix P(x) is true over the domain U if and only if the truth set of P is the set U. Likewise, 3x P (x) is true over the domain U if and only if the truth set of P is nonempty. Exercises 1. List the members of these sets. a) {x I x is a real number such that x2 = I} b) {x I x is a positive integer less than 12} c) {x I x is the square of an integer and x < 100} d) {x I x is an integer such that x2 = 2} 2. Use set builder notation to give a description of each of these sets. a) to, 3, 6, 9, I2} b) {-3,-2,-I,O, I, 2,3} c) {m, n, 0, p} 3. Determine whether each of these pairs of sets are equal. a) {I, 3, 3, 3, 5, 5, 5, 5, 5}, {5, 3, I} b) {{I}}, {I, {I}} c) 0, {0} 4. Suppose that A = {2, 4, 6}, B = {2, 6}, C = {4, 6}, and D = {4, 6, 8}. Determine which of these sets are subsets of which other of these sets. 5. For each of the following sets, determine whether 2 is an element of that set. a) {x E R I x is an integer greater than I} b) {x E R I x is the square of an integer} c) {2,{2}} d) {{2},{{2}}} e) ({2},{2, {2}}} 1) {{ {2}}} 120 2 / Basic Structures: Sets, Functions, Sequences, and Sums 6. For each of the sets in Exercise 5, determine whether {2} is an element of that set. 7. Determine whether each of these statements is true or false. a) 0 E 0 c) {OJ c 0 e) {OJ E {OJ g) {0} S; {0} b) 0 E {OJ d) 0 c {OJ t) {OJ C {OJ 8. Determine whether these statements are true or false. a) 0 E {0} c) {0} E {0} b) 0 E {0, {0}} d) {0} E {{0}} e) {0} C {0, {0}} t) {{0}} C {0, {0}} g) {{0}} c {{0}, {0}} 9. Determine whether each of these statements is true or false. a) x E {x} d) {x} E {{x}} b) {x} S; {x} e) 0 S; {x} c) {x} E {x} t) 0 E {x} 10. Use a Venn diagram to illustrate the subset of odd integers in the set of all positive integers not exceeding 10. 11. Use a Venn diagram to illustrate the set of all months of the year whose names do not contain the letter R in the set of all months of the year. 12. Use a Venn diagram to illustrate the relationship A S; B and B S; C. 13. Use a Venn diagram to illustrate the relationships A C B andB c C. 14. Use a Venn diagram to illustrate the relationships A C B and A C C. 15. Suppose that A, B, and C are sets such that A S; Band B S; C. Show that A S; C. 16. Find two sets A and B such that A E B and A S; B. 17. What is the cardinality of each of these sets? a) {a} b) {{a}} c) {a, {a}} d) {a, {a}, {a, {am 18. What is the cardinality of each of these sets? a) 0 b) {0} c) {0, {0}} d) {0, {0}, {0, {0m 19. Find the power set of each of these sets, where a and b are distinct elements. a) {a} b) {a, b} c) {0, {0}} 20. Can you conclude that A = B if A and B are two sets with the same power set? 21. How many elements does each of these sets have where a and b are distinct elements? a) P({a, b, {a, b}}) b) P({0, a, {a}, ({a}}}) c) P(P(0» 2-10 22. Determine whether each of these sets is the power set of a set, where a and b are distinct elements. a) 0 b) {0, {a}} c) {0, {a}, {0, a}} d) {0, {a}, {b}, {a, b}} 23. Let A = {a, b, c, d} and B = {y, z}. Find a) A x B b) B x A 24. What is the Cartesian product A x B, where A is the set of courses offered by the mathematics depart­ ment at a university and B is the set of mathematics pro­ fessors at this university? 25. What is the Cartesian product A x B xC, where A is the set of all airlines and Band C are both the set of all cities in the United States? 26. Suppose that A x B = 0, where A and B are sets. What can you conclude? 27. Let A be a set. Show that 0 x A = A x 0 = 0. 28. Let A = {a, b, c}, B = {x, y}, and C = {O, I}. Find a) A x B xC c) C x A x B b) C x B x A d) B x B x B 29. How many different elements does A x B have if A has m elements and B has n elements? 30. Show that A x B =1= B x A, when A and Bare nonempty, unless A = B. 31. Explain why A x B x C and (A x B) x C are no� the same. 32. Explain why (A x B) x (C x D) and A x (B x C) x D are not the same. 33. Translate each of these quantifications into English and determine its truth value. a) 'IxER(x2 =1= -1) c) 'IxE Z (x2 > 0) b) 3XEZ (x2 = 2) d) 3XE R (x2 = x) 34. Translate each of these quantifications into English and determine its truth value. a) 3XER(x3 = -1) c) 'IxEZ(X -1 E Z) b) 3xEZ(x+l >x) d) 'IxE Z (x2 E Z) 35. Find the truth set of each of these predicates where the domain is the set of integers. a) P(x): "X2 < 3" b) Q(x): "X2 > x" c) R(x): "2x + 1 = 0" 36. Find the truth set of each of these predicates where the domain is the set of integers. a) P(x): "x3 2': I" b) Q(x): "X2 = 2" c) R(x): "x < x2" *37. The defining property of an ordered pair is that two or­ dered pairs are equal if and only if their first elements are equal and their second elements are equal. Surpris­ ingly, instead of taking the ordered pair as a primitive con­ cept, we can construct ordered pairs using basic notions from set theory. Show that if we define the ordered pair (a, b) to be {{a}, {a, b}}, then (a, b) = (c, d) if and only 2-11 2.2 Set Operations 121 if a = e and b = d. [Hint: First show that {{a}, {a, b}} = {{e}, {e, d}} if and only if a = e and b = d.] b) Show the assumption that S is not a member of S leads to a contradiction. *38. This exercise presents Russell's paradox. Let S be the set that contains a set x if the set x does not belong to itself, so that S = {x I x ¢ x}. By parts (a) and (b) it follows that the set S cannot be de­ fined as it was. This paradox can be avoided by restricting the types of elements that sets can have. :J a) Show the assumption that S is a member of S leads to a contradiction. *39. Describe a procedure for listing all the subsets of a finite set. 2.2 Set Operations unks� DEFINITION 1 Introduction Two sets can be combined in many different ways. For instance, starting with the set of mathe­ matics majors at your school and the set of computer science majors at your school, we can form the set of students who are mathematics majors or computer science ma jors, the set of students who are joint majors in mathemati cs and computer science, the set of all students not majoring in mathematics, and so on. Let A and B be sets. The union of the sets A and B, denoted by A U B, is the set that contains those elements that are either in A or in B, or in both. An element x belongs to the union of the sets A and B if and only if x belongs to A or x belongs to B. This tells us that AU B = {x I x E A v x E B}. The Venn diagram shown in Figure 1 represents the union of two sets A and B. The area that represents A U B is the shaded area within either the circle representing A or the circle representing B. We will give some examples of the union of sets. EXAMPLE 1 The union of the sets {l, 3, 5} and {l, 2, 3} is the set {l, 2, 3, 5}; that is, {l, 3, 5} U {l, 2, 3} = {l,2,3,5}. � EXAMPLE 2 The union ofthe set of all computer science majors at your school and the set of all mathematics majors at your school is the set of students at your school who are majoring either in mathematics or in computer science (or in both). � DEFINITION 2 Let A and B be sets. The intersection of the sets A and B, denoted by An B, is the set containing those elements in both A and B. An element x belongs to the intersection of the sets A and B if and only if x belongs to A and x belongs to B. This tells us that An B = {x I x E A 1\ X E B}. 122 2 / Basic Structures: Sets, Functions, Sequences, and Sums A U B is shaded. u 2-12 A n B is shaded. FIGURE 1 Venn Diagram Representing the Union of A and B. FIGURE 2 Venn Diagram Representing the Intersection of A and B. The Venn diagram shown in Figure 2 represents the intersection of two sets A and B. The shaded area that is within both the circles representing the sets A and B is the area that represents the intersection of A and B. We give some examples of the intersection of sets. EXAMPLE 3 The intersection ofthe sets {I, 3, 5} and {I, 2, 3} is the set {I, 3}; that is, {I, 3, 5} n {l, 2, 3} = {I,3}. .... EXAMPLE 4 The intersection of the set of all computer science majors at your school and the set of all mathemati cs majors is the set of all students who are joint majors in mathematics and computer science. .... DEFINITION 3 Two sets are called disjoint if their intersection is the empty set. EXAMPLE 5 Let A = {I, 3, 5, 7, 9} and B = {2, 4, 6, 8, 1O}. Because An B = 0, A and B are disjoint. .... We often are interested in finding the cardinality of a union of two finite sets A and B. Note that I A I + I B I counts each element that is in A but not in B or in B but not in A exactly once, and each element that is in both A and B exactly twice. Thus, if the number of elements that are in both A and B is subtracted from I A I + I B I, elements in A n B will be counted only once. Hence, IA U BI = IAI + IBI- IA n BI. The generalization of this result to unions of an arbitrary number of sets is called the principle of inclusion -exclusion. The principle of inclusion-exclusion is an important technique used in enumeration. We will discuss this principle and other counting techniques in detail in Chapters 5 and 7. There are other important ways to combine sets. 2-13 --��-'---------, B A -B is shaded. FIGURE 3 Venn Diagram for the Difference of A and B. u 2.2 Set Operations 123 A is shaded. FIGURE 4 Venn Diagram for the Complement of the Set A. DEFINITION 4 Let A and B be sets. The difference of A and B, denoted by A -B, is the set containing those elements that are in A but not in B. The difference of A and B is also called the complem ent of B with respect to A. An element x belongs to the difference of A and B if and only if x E A and x rt B. This tells us that A - B = {x I x E A /\ x rt B}. The Venn diagram shown in Figure 3 represents the difference ofthe sets A and B. The shaded area inside the circle that represents A and outside the circle that represents B is the area that represents A -B. We give some examples of differences of sets. EXAMPLE 6 The difference of {I, 3, 5} and {l, 2, 3} is the set {5}; that is, {l, 3, 5} -{l, 2, 3} = {5}. This is different from the difference of {I , 2, 3} and {I, 3, 5}, which is the set {2}. .... EXAMPLE 7 The difference of the set of computer science majors at your school and the set of mathematics majors at your school is the set of all computer science majors at your school who are not also mathematics majors. .... DEFINITION 5 Once the universal set U has been specified, the complement of a set can be defined. Let U be the universal set. The complem ent of the set A, denoted by A, is the complement of A with respect to U. In other words, the complement of the set A is U -A. An element belongs to A if and only if x rt A. This tells us that if = {x I x rt A}. In Figure 4 the shaded area outside the circle representing A is the area representing A. We give some examples of the complement of a set. 124 2 / Basic Structure s: Sets, Functions, Sequences, and Swns TABLE 1 Set Identities. Identity AU0=A AnU =A AUU=U An0=0 AUA=A AnA=A - (:4) = A AUB=BUA AnB=BnA A U (B U C) = (A U B) U C A n (B n C) = (A n B) n C An (B U C) = (A n B) U (A n C) AU (B n C) = (A U B) n (A U C) AUB=AnB AnB=AUB A U(A n B) = A An(A UB) = A AUA=U AnA=0 2-14 Name Identity laws Domination laws Idempotent laws Complementation law Commutative laws Associative laws Distributive laws De Morgan's laws Absorption laws Complement laws EXAMPLE 8 Let A = {a, e, i, 0, u} (where the universal set is the set of letters of the English alpha- bet). Then A = {b, c, d, j, g, h, j, k, I, m, n, p, q, r, S, t, v, w, x, y, z}. � EXAMPLE 9 Let A be the set of positive integers greater than 10 (with universal set the set of all positive integers). Then A = {l, 2, 3,4,5,6, 7, 8, 9, lO}. � Set Identities Table 1 lists the most important set identities. We will prove several of these identities here, using three different methods. These methods are presented to illustrate that there are often many different approaches to the solution of a problem. The proofs of the remaining identities will be left as exercises. The reader should note the similarity between these set identities and the logical equivalences discussed in Section 1.2. In fact, the set identities given can be proved directly from the corresponding logical equivalences. Furthermore, both are special cases of identities that hold for Boolean algebra (discussed in Chapter 11). One way to show that two sets are equal is to show that each is a subset of the other. Recall that to show that one set is a subset of a second set, we can show that if an element belongs to the first set, then it must also belong to the second set. We generally use a direct proof to do this. We illustrate this type of proof by establishing the second of De Morgan's laws. 2-15 2.2 Set Operations 125 EXAMPLE 10 Prove that A n B = AU B. Solution: To show that A n B = A U B, we will show that A nBS; A U B and that A U B S; AnB. First, we will show that A nBS; A U B. So suppose that x E A n B. By the definition of complement, x f/ A n B. By the definition of intersection, -.( (x E A) /\ (x E B» is true. Applying De Morgan's law (from logic), we see that -.(x E A) or -.(x E B). Hence, by the definition of negation, x f/ A or x f/ B. By the definition of complement, x E A or x E B. It follows by the definition of union that x E A U B. This shows that A nBS; if U B. Next, we will show that if U B S; A n B. Now suppose that x E A U B. By the definition of union, x E A or x E B. Using the definition of complement, we see that x f/ A or x f/ B. Consequently, -.(x E A) V -.(x E B) is true. By De Morgan's law (from logic), we conclude that -.«x E A) /\ (x E B» is true. By the definition of intersection, it follows that -.(x E A n B) holds. We use the definition of complement to conclude that x E A n B. This shows that if U B � A n B. Because we have shown that each set is a subset of the other, the two sets are equal, and the identity is proved. ... We can more succinctly express the reasoning used in Example lOusing set builder notation, as Example 11 illustrates. EXAMPLE 11 Use setbuildernotation and logical equivalences to establisht he second De Morgan law An B = AUB. Solution: We can prove this identity with the following steps. An B = {x I x fJ. An B} = {x I -.(x E (A n B»} = {x I -.(x E A /\ X E B)} = {x I -.(x E A) V -.(x E B)} = {x I x fJ. A V x fJ. B} = {x I x E A V X E B} = {x I x E AU B} =AUB by definition of complement by definition of does not belong symbol by definition of intersection by the first De Morgan law for logical equivalences by definition of does not belong symbol by definition of complement by definition of union by meaning of set builder notation Note that besides the definitions of complement, union, set membership, and set builder notation, this proof uses the first De Morgan law for logical equivalences. ... Proving a set identity involving more than two sets by showing each side of the identity is a subset of the other often requires that we keep track of different cases, as illustrated by the proof in Example 12 of one of the distributive laws for sets. EXAMPLE 12 Prove the first distributive law from Table 1, which states that A n (B U C) = (A n B) U (A n C) for all sets A, B, and C. Solution: We will prove this identity by showing that each side is a subset of the other side. Suppose that x E A n (B U C). Then x E A and x E B U C. By the definition of union, it follows that x E A, and x E B or x E C (or both). Consequently, we know that x E A and x E B or that x E A and x E C. By the definition of intersection, it follows that x E A n B or x E A n C . Using the definition of union, we conclude that x E (A n B) U (A n C). We conclude that A n (B U C) S; (A n B) U (A n C). 126 2 / Basic Structure s: Sets, Functions, Sequences, and Swns TABLE 2 A Membership Table for the Distributive Property . A B C BUC A nCB UC) AnB 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 2-16 AnC (A n B) U (A n C) 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 Now suppose that x E (A n B) U (A n C). Then, by the definition of union, x E An B or x E A n C. By the definition of intersection, it follows that x E A and x E B or that x E A and x E C. From this we see that x E A, and x E B or x E C. Consequently, by the definition of union we see that x E A and x E B U C. Furthermore, by the definition of intersection, it follows thatx E An (B U C). We conclude that (A n B) U (A n C) <; An (B U C). This completes the proof of the identity. .... Set identities can also be proved using membership tables. We consider each combination of sets that an element can belong to and verify that elements in the same combinations of sets belong to both the sets in the identity. To indicate that an element is in a set, a 1 is used; to indicate that an element is not in a set, a 0 is used. (The reader should note the similarity between membership tables and truth tables.) EXAMPLE 13 Use a membership table to show that A n (B U C) = (A n B) U (A n C). Solution: The membership table for these combinations of sets is shown in Table Z. This table has eight rows. Because the columns for An (B U C) and (A n B) U (A n C) are the same, the identity is valid. .... Additional set identities can be established using those that we have already proved. Consider Example 14. EXAMPL E 14 Let A, B, and C be sets. Show that AU (B n C) = (C U B) n A. Solution: We have AU (B n C) = An (B n C) by the first De Morgan law = An (B U C) by the second De Morgan law = (B U C) n A by the commutative law for intersections = (C U B) n A by the commutative law for unions. Generalized Unions and Intersections Because unions and intersections of sets satisfy associative laws, the sets A U B U C and A n B n C are well defined; that is, the meaning of this notation is unambiguous when A, B, and 2-17 2.2 Set Operations 127 -�---.- -- .-----.-. -�----, (a) A U B U C is shaded. (b) An B n C is shaded. FIGURE 5 The Union and Intersection of A, B, and C. u C are sets. That is, we do not have to use parentheses to indicate which operation comes first because AU (B U C) = (A U B) U C and A n (B n C) = (A n B) n C. Note that A U B U C contains those elements that are in at least one of the sets A, B, and C, and that A n B n C contains those elements that are in all of A, B, and C. These combinations of the three sets, A, B, and C, are shown in Figure 5. EXAMPLE 15 Let A = {a, 2, 4, 6, 8}, B = {a, 1,2,3, 4}, and C = {a, 3, 6, 9}. What are AU B U C and An B nc? DEFINITION 6 Solution: The set A U B U C contains those elements in at least one of A, B, and C. Hence, AU B U C = {a, 1,2,3,4,6,8, 9}. The set A n B n C contains those elements in all three of A, B, and C. Thus, An B n C = {oJ. We can also consider unions and intersections of an arbitrary number of sets. We use these definitions. The union of a collection of sets is the set that contains those elements that are members of at least one set in the collection. We use the notation n A I U A2 U ... U An = U Ai i=1 to denote the union of the sets AI, A2, ... , An. D EFINITI 0 N 7 The intersection of a collection of sets is the set that contains those elements that are members of all the sets in the collection. 128 2 / Basic Structure s: Sets, Functions, Sequences, and Sums We use the notation n A I n A2 n ... nAn = n Ai i=1 2-18 to denote the intersection of the sets A I, A2, ... , An. We illustrate generalized unions and intersections with Example 16. EXAMPLE 16 Let A; = {i, i + 1, i + 2, ... }. Then, and n n U A; = U {i, i + 1, i + 2, ... } = {l, 2, 3, ... }, ;=1 i=1 , \ n n n A; = n {i, i + 1, i + 2, ... } = {n, n + 1, n + 2, ... }. i=1 i=1 We can extend the notation we have introduce for unions and intersections to other families of sets. In particular, we can use the notation 00 AI U A2 U··· U An U··· = U Ai ;=1 to denote the union ofthe sets AI, A2, ... , An' .... Similarly, the intersection of these sets can be denoted by 00 AlnA2n ... nAnn···=nA;. ;=1 More generally, the notations n;EI Ai and U;EI Ai are used to denote the inte,section and union of the sets Ai for i E I, respectively. Note that we have n;EI A; = {x I Vi E I (x E A;)} and U;EI A; = {x I 3i E I (x E Ai)}. EXAMPLE 17 Suppose that A; = {l, 2, 3, ... , i} for i = 1,2,3, .... Then, and 00 00 U A; = U {l, 2, 3, ... , i} = {I, 2, 3, ... } = Z+ ;=1 ;=1 00 00 n A; = n {l, 2, 3, ... , i} = {l}. ;=1 ;=1 To see that the union of these sets is the set of positive integers, note that every positive integer is in at least one of the sets, because the integer n belongs to An = {I, 2, ... , n} and every element of the sets in the union is a positive integer. To see that the intersection of these sets, note that the only element that belongs to all the sets AI, A2, ... is 1. To see this note that AI = {l} and 1 E Ai fori = 1,2, .... ... 2-19 2.2 Set Operations 129 Computer Representation of Sets There are various ways to represent sets using a computer. One method is to store the elements of the set in an unordered fashion. However, if this is done, the operations of computing the union, intersection, or difference of two sets would be time-consuming, because each of these operations would require a large amount of searching for elements. We will present a method for storing elements using an arbitrary ordering of the elements of the universal set. This method of representing sets makes computing combinations of sets easy. Assume that the universal set U is finite (and of reasonable size so that the number of elements of U is not larger than the memory size of the computer being used). First, specify an arbitrary ordering of the elements of U, for instance ai, a2, ... , an. Represent a subset A of U with the bit string of length n, where the i th bit in this string is 1 if aj belongs to A and is 0 if aj does not belong to A. Example 18 illustrates this technique. EXAMPLE 18 Let U = {l, 2, 3, 4,5,6,7,8,9, 1O}, and the ordering of elements of U has the elements in increasing order; that is, aj = i. What bit strings represent the subset of all odd integers in U, the subset of all even integers in U, and the subset of integers not exceeding 5 in U? Solution: The bit string that represents the set of odd integers in U, namely, {I, 3, 5, 7, 9}, has a one bit in the first, third, fifth, seventh, and ninth positions, and a zero elsewhere. It is 10 1010 1010. (We have split this bit string of length ten into blocks of length four for easy reading because long bit strings are difficult to read.) Similarly, we represent the subset of all even integers in U, namely, {2, 4, 6, 8, 10}, by the string 01 0101 0101. The set of all integers in U that do not exceed 5, namely, {l, 2, 3, 4, 5}, is represented by the string 11 11100000. Using bit strings to represent sets, it is easy to find complements of sets and unions, inter­ sections, and differences of sets. To find the bit string for the complement of a set from the bit string for that set, we simply change each 1 to a 0 and each 0 to 1, because x E A if and only if x ¢ A. Note that this operation corresponds to taking the negation of each bit when we associate a bit with a truth value-with 1 representing true and 0 representing false. EXAMPLE 19 We have seen that the bit string for the set {I, 3, 5, 7, 9} (with universal set {l, 2, 3, 4, 5,6,7,8, 9, 1OJ) is 10 1010 1010. What is the bit string for the complement of this set? Solution: The bit string for the complement of this set is obtained by replacing Os with 1 s and vice versa. This yields the string 010101 0101 , which corresponds to the set {2, 4, 6, 8, 1O}. 130 2 / Basic Structures: Sets, Functions, Sequences, and Sums 2-20 To obtain the bit string for the union and intersection of two sets we perform bitwise Boolean operations on the bit strings representing the two sets. The bit in the ith position of the bit string of the union is 1 if either of the bits in the ith position in the two strings is 1 (or both are 1), and is 0 when both bits are O. Hence, the bit string for the union is the bitwise OR of the bit strings for the two sets. The bit in the i th position of the bit string of the intersection is 1 when the bits in the corresponding position in the two strings are both 1, and is 0 when either of the two bits is 0 (or both are). Hence, the bit string for the intersection is the bitwise AND of the bit strings for the two sets. EXAMPLE 20 The bit strings for the sets {I, 2, 3, 4, 5} and {I, 3, 5, 7, 9} are 11 1110 0000 and 10 1010 1010, respectively. Use bit strings to find the union and intersection of these sets. Solution: The bit string for the union of these sets is 11 11100000 v 10 1010 1010 = 11 1110 1010, which corresponds to the set {l, 2, 3,4,5,7, 9}. The bit string for the intersection of these sets IS 11 11100000/\ 10 1010 1010 = 10 1010 0000, which corresponds to the set {I, 3, 5}. Exercises 1. Let A be the set of students who live within one mile of school and let B be the set of students who walk to classes. Describe the students in each of these sets. a) AnB c) A - B b) AUB d) B-A 2. Suppose that A is the set of sophomores at your school and B is the set of students in discrete mathematics at your school. Express each of these sets in terms of A and B. a) the set of sophomores taking discrete mathematics in your school b) the set of sophomores at your school who are not tak­ ing discrete mathematics c) the set of students at your school who either are sopho­ mores or are taking discrete mathematics d) the set of students at your school who either are not sophomores or are not taking discrete mathematics 3. Let A = {l, 2, 3,4, 5} and B = {O, 3, 6}. Find a) AUB. b) AnB. c) A -B. d) B -A. 4. Let A = {a, b, c, d, e} g, h}. Find a) AU B. c) A-B. and B = {a, b, c, d, e, f, b) An B. d) B -A. In Exercises 5-10 assume that A is a subset of some underlying universal set U. 5. �ove the complementation law in Table 1 by showing that A=A. 6. Prove the identity laws in Table 1 by showing that a) A U 0 = A. b) A n U = A. 7. Prove the domination laws in Table 1 by showing that a) AU U = U. b) An 0 = 0. 8. Prove the idempotent laws in Table 1 by showing that a) A U A = A. b) A n A = A. 9. Prove the complement laws in Table I by showing that a) A U A = U. b) A n A = 0. 10. Show that a) A -0 = A. b) 0 - A = 0. 11. Let A and B be sets. Prove the commutative laws from Table 1 by showing that a) A UB = B U A. b) A nB = B n A. 12. Prove the first absorption law from Table 1 by showing that if A and B are sets, then A U (A n B) = A. 13. Prove the second absorption law from Table 1 by showing that if A and B are sets, then A n (A U B) = A. 14. Find the sets A and B if A - B = {I, 5, 7, 8}, B - A = {2, lO}, and A n B = {3, 6, 9}. 15. Prove the first De Morgan law in Table 1 by showing that if A and B are sets, then A U B = An B ::-21 a) by showing each side is a subset of the other side. b) using a membership table. 16. Let A and B be sets. Show that a) (A n B) � A. b) A � (A U B). c) A - B � A. d) An (B -A) = 0. e) AU (B - A) = AU B. 17. Show that if A, B, and C are sets, then An B n C = AUEUC a) by showing each side is a subset of the other side. b) using a membership table. 18. Let A, B, and C be sets. Show that a) (A U B) � (A U B U C). b) (A n B n C) � (A n B). c) (A -B) - C � A-C. d) (A - C) n (C - B) = 0. e) (B -A) U (C -A) = (B U C) -A. 19. Show that if A and B are sets, then A - B = A n E. 20. Show that if A and B are sets, then (A n B) U (AnE)= A. 21. Prove the first associative law from Table I by showing that if A, B,andC are sets, then A U (B U C) = (A U B) U C. 22. Prove the second associative law from Table I by show­ ing that if A, B, and C are sets, then An (B n C) = (A n B) n C. 23. Prove the second distributive law from Table I by show­ ing that if A, B, and C are sets, then AU (B n C) = (A U B) n (A U C). 24. Let A, B, and C be sets. Show that (A -B) - C = (A - C) - (B - C). 2S. Let A = {O, 2, 4, 6, 8, 10}, B = {O, 1,2, 3,4,5, 6}, and C = {4, 5, 6, 7, 8, 9, 1O}. Find a) An B n C. c) (AUB)nc. b) AUB UC. d) (AnB)UC. 26. Draw the Venn diagrams for each of these combinations of the sets A, B, and C. - - - a) An (B U C) b) An B n C c) (A - B) U (A - C) U (B - C) 27. Draw the Venn diagrams for each of these combinations of the sets A, B, and C. a) An (B -C) b) (A n B) U (A n C) c) (A n E) U (A n C) 28. Draw the Venn diagrams for each of these combinations of the sets A, B, C, and D. a) (A n B) U (C n D) c) A -(B n C n D) b) AUBUCUD 29. What can you say about the sets A and B if we know that a) AUB=A? b) AnB=A? c) A-B=A ? d) AnB=BnA? e)A-B=B-A? 30. Can you conclude that A = B if A, B, and C are sets such that a) AU C = B U C? b) An C = B n C? c) A U B = B U C and A n C = B n C? 2.2 Set Operations 131 31. Let A and B be subsets of a universal set U. Show that A � B if and only ifE � A? The symmetric difference of A and B, denoted by A EB B, is the set containing those elements in either A or B, but not in both A and B. 32. Find the symmetric difference of {I, 3, 5} and {I, 2, 3}. 33. Find the symmetric difference of the set of computer science majors at a school and the set of mathematics majors at this school. 34. Draw a Venn diagram for the symmetric difference of the sets A and B. 35. Show that A EB B = (A U B) -(A n B). 36. Show that A EB B = (A -B) U (B -A). 37. Show that if A is a subset of a universal set U, then a) A EB A = 0. b) A EB 0 = A. c) A EB U = A. d) A EB A = U. 38. Show that if A and B are sets, then a) A EB B = B EB A. b) (A EB B) EB B = A. 39. What can you say about the sets A and B if A EB B = A? *40. Determine whether the symmetric difference is associa­ tive; that is, if A, B, and C are sets, does it follow that A EB (B EB C) = (A EB B) EB C? *41. Suppose that A, B, and C are sets such that A EB C = B EB C. Must it be the case that A = B? 42. If A, B, C, and D are sets, does it follow that (A EB B) EB (C EB D) = (A EB C) EB (B EB D)? 43. If A, B, C, and D are sets, does it follow that (A EB B) EB (C EB D) = (A EB D) EB (B EB C)? *44. Show that if A, B, and C are sets, then IA U B U C I = IAI + IBI + ICI -IA n BI - IA n C I -IB n CI + IA n B n C I· (This is a special case of the inclusion-exclusion princi­ ple, which will be studied in Chapter 7.) *45. Let Ai = {I, 2, 3, ... , i} for i = 1,2, 3, .... Find n a) UAi. i=1 i=1 *46. Let Ai = { ... , -2, -1, 0, I, ... , i}. Find i=1 i=1 47. Let Ai be the set of all nonempty bit strings (that is, bit strings of length at least one) of length not exceeding i. Find n a) UAi• i=1 i=1 48. Find U�I Ai and n�1 Ai iffor every positive integer i, a) Ai = Ii, i + I, i + 2, ... }. b) Ai = {O, n. c) Ai = (0, i), that is, the set of real numbers x with 0< x < i. d) Ai = (i, 00), that is, the set of real numbers x with x> i. 132 2 / Basic Structures: Sets, Functions, Sequences, and Sums 49. Find U::I Aj and n::1 Aj iffor every positive integer i, a) Aj={-i,-i+l, ... ,-I,O,I, ... ,i-l,i}. b) Aj = {-i, i}. c) Aj = [-i, i], that is, the set of real numbers x with -i :::: x :::: i. d) Aj = [i, 00], that is, the set of real numbers x with x ?: i. 50. Suppose that the universal set is U = {I, 2, 3,4, 5,6,7,8,9,1O}.Expresseachofthesesetswithbitstrings where the ith bit in the string is 1 if i is in the set and 0 otherwise. a) {3, 4, 5} b) {l, 3, 6, IO} c) {2, 3, 4, 7,8, 9} 51. Using the same universal set as in the last problem, find the set specified by each of these bit strings. a) 11 11001111 b) 01 0111 1000 c) 10 0000 0001 52. What subsets of a finite universal set do these bit strings represent? a) the string with all zeros b) the string with all ones 53. What is the bit string corresponding to the difference of two sets? 54. What is the bit string corresponding to the symmetric difference of two sets? 55. Show how bitwise operations on bit strings can be used to find these combinations of A = {a, b, e, d, e}, B = {b, e, d, g, p, t, v}, C = {e, e, i, 0, u, x, y, z}, and D = {d,e,h,i,n,o,t,u,x,y}. a) AUB b) AnB c) (A U D) n (B U C) d) AUB UC UD 56. How can the union and intersection of n sets that all are subsets of the universal set U be found using bit strings? The successor ofthe set A is the set A U {A}. 57. Find the successors of the following sets. a) {1,2,3} b) 0 c) {0} d) .{0, {0}} 58. How many elements does the successor of a set with n elements have? Sometimes the number of times that an element occurs in an unordered collection matters. Multisets are unor­ dered collections of elements where an element can oc­ cur as a member more than once. The notation {mi' ai, m2' a2, ... , mr . ar} denotes the multisetwith elemental oc­ curring m I times, element a2 occurring m2 times, and so on. The numbers m j, i = 1, 2, ... , r are called the multiplicities ofthe elements aj, i = 1,2, ... , r. Let P and Q be multi sets. The union of the multisets P and Q is the multi set where the multiplicity of an element is the maximum of its multiplicities in P and Q. The intersec­ tion of P and Q is the multi set where the multiplicity of an element is the minimum of its multiplicities in P and Q. The 2-22 difference of P and Q is the multi set where the multiplicity of an element is the multiplicity of the element in P less its mul­ tiplicity in Q unless this difference is negative, in which case the multiplicity is O. The sum of P and Q is the multiset where the multiplicity of an element is the sum of multiplicities in P and Q. The union, intersection, and difference of P and Q are denoted by P U Q, P n Q, and P - Q, respectively (where these operations should not be confused with the anal­ ogous operations for sets). The sum of P and Q is denoted by P+Q. 59. Let A and B be the multisets {3· a, 2· b, 1 . e} and {2 . a, 3 . b, 4 . d}, respectively. Find a) AUB. b) AnB. c) A -B. d) B -A. e) A + B. 60. Suppose that A is the multiset that has as its elements the types of computer equipment needed by one department of a university where the multiplicities are the number of pieces of each type needed, and B is the analogous multi set for a second department of the university. For instance, A could be the multi set {I 07 . personal comput­ ers, 44 . routers, 6 . servers} and B could be the multiset {14 . personal computers, 6 . routers, 2 . mainfram es}. a) What combination of A and B represents the equip­ ment the university should buy assuming both depart­ ments use the same equipment? b) What combination of A and B represents the equip­ ment that will be used by both departments if both departments use the same equipment? c) What combination of A and B represents the equip­ ment that the second department uses, but the first department does not, if both departments use the same equipment? d) What combination of A and B represents the equip­ ment that the university should purchase if the depart­ ments do not share equipment? Fuzzy sets are used in artificial intelligence. Each element in the universal set U has a degree of membership, which is a real number between 0 and 1 (including 0 and 1), in a fuzzy set S. The fuzzy set S is denoted by listing the elements with � their degrees of membership (elements with 0 qegree of mem­ bership are not listed). For instance, we write {0.6 Alice, 0.9 Brian, 004 Fred, 0.1 Oscar, 0.5 Rita} for the set F (offamous people) to indicate that Alice has a 0.6 degree of membership in F, Brian has a 0.9 degree of membership in F, Fred has a 004 degree of membership in F, Oscar has a 0.1 degree of membership in F, and Rita has a 0.5 degree of membership in F (so that Brian is the most famous and Oscar is the least famous ofthese people). Also suppose that R is the set of rich people with R = {OA Alice, 0.8 Brian, 0.2 Fred, 0.9 Oscar, 0.7 Rita}. 61. The complement of a fuzzy set S is the set S, with the degree of the membership of an element in S equal to 1 minus the degree of membership of this element in S. Find F (the fuzzy set of people who are not famous) and R (the fuzzy set of people who are not rich). 2-23 2.3 Functions 133 62. The union of two fuzzy sets Sand T is the fuzzy set S U T, where the degree of membership of an element in S U T is the maximum of the degrees of membership of this element in S and in T. Find the fuzzy set FUR of rich or famous people. 63. The intersection of two fuzzy sets S and T is the fuzzy set S nT, where the degree of membership of an element in S nTis the minimum of the degrees of membership of this element in S and in T. Find the fuzzy set F n R of rich and famous people. 2.3 Functions DEFINITION 1 Assessmenl � Introduction In many instances we assign to each element of a set a particular element of a second set (which may be the same as the first). For example, suppose that each student in a discrete mathematics class is assigned a letter grade from the set {A, B, C, D, F}. And suppose that the grades are A for Adams, C for Chou, B for Goodfriend, A for Rodriguez, and F for Stevens. This assignment of grades is illustrated in Figure 1. This assignment is an example of a function. The concept of a function is extremely impor­ tant in mathemati cs and computer scien ce. For example, in discrete mathemati cs functions are used in the definition of such discrete structures as sequences and strings. Functions are also used to represent how long it takes a computer to solve problems of a given size. Many computer programs and subroutines are designed to calculate values of functions. Recursive functions, which are functions defined in terms of themselves, are used throughout computer science; they will be studied in Chapter 4. This section reviews the basic concepts involving functions needed in discrete mathemati cs. Let A and B be nonempty sets. A jUnction f from A to B is an assignment of exactly one element of B to each element of A. We write f(a) = b if b is the unique element of B assigned by the function f to the element a of A. If f is a function from A to B, we write f: A -+ B. Remark: Functions are sometimes also called mappings or transformations. Functions are specified in many different ways. Sometimes we explicitly state the assign­ ments, as in Figure 1. Often we give a formula, such as f(x) = x + 1, to define a function. Other times we use a computer program to specify a function. Adams .--------------�.� .A Chou • .B Goodfriend • .c Rodriguez • .D Stevens • -------------- � .. �. F FIGURE 1 Assignment of Grades in a Discrete Mathematics Class. 134 2 / Basic Structures: Sets, Functions, Sequences, and Sums 2-24 DEFINITION 2 f . ----�-----------r--� . a b=f(a) A B FIGURE 2 The Function! Maps A to B. A function! : A -+ B can also be defined in terms of a relation from A to B. Recall from Section 2.1 that a relation from A to B is just a subset of Ax B. A relation from A to B that contains one, and only one, ordered pair (a, b) for every element a E A, defines a function f from A to B. This function is defined by the assignment f(a) = b, where (a, b) is the unique ordered pair in the relation that has a as its first element. If! is a function from A to B, we say that A is the domain of f and B is the codomain of f. If f( a) = b, we say that b is the image of a and a is a preimage of b. The range of f is the set of all images of elements of A. Also, if f is a function from A to B, we say thatfmaps A toB. Figure 2 represents a function f from A to B. When we define a function we specify its domain, its codomain, and the mapping of elements of the domain to elements in the codomain. Two functions are equal when they have the same domain, have the same codomain, and map elements of their common domain to the same elements in their common codomain. Note that if we change either the domain or the codomain of a function, then we obtain a different function. If we change the mapping of elements, then we also obtain a different function. Examples 1-5 provide examples of functions. In each case, we describe the domain, the codomain, the range, and the assignment of values to elements of the domain. EXAMPLE 1 What are the domain, codomain, and range of the function that assigns grades to students described in the first paragraph of the introduction of this section? Solution: Let G be the function that assigns a grade to a student in our discrete mathematics class. Note that G(Adams) = A, for instance. The domain ofG is the set {Adams, Chou, Goodfriend, Rodriguez, Stevens}, and the codomain is the set {A, B, C, D, F}. The range of G is the set {A, B, C, F}, because each grade except D is assigned to some student. .... EXAMPLE 2 Let R be the relation consisting of ordered pairs (Abdul, 22), (Brenda, 24), (Carla, 21), (Desire, 22), (Eddie, 24), and (Felicia, 22), where each pair consists of a graduate student and the age of this student. What is the function that this relation determines? Solution: This relation defines the function f, where with f(Abdul) = 22, f(Brenda) = 24, f(Carla) = 21, f(Desire) = 22, f(Eddie) = 24, and f(Felicia) = 22. Here the domain is the set {Abdul, Brenda, Carla, Desire, Eddie, Felicia}. To define the function f, we need to specify a codomain. Here, we can take the codomain to be the set of positive integers to make sure 2-25 EXAMPLE 3 Extra� Examples"'" EXAMPLE 4 EXAMPLES 2.3 Functions 135 that the codomain contains all possible ages of students. (Note that we could choose a smaller codomain, but that would change the function.) Finally, the range is the set {21, 22, 24}. � Let I be the function that assigns the last two bits of a bit string of length 2 or greater to that string. For example, 1(110 10) = 10. Then, the domain of I is the set of all bit strings oflength 2 or greater, and both the codomain and range are the set {OO, 01, 10, II}. � Let I: Z -+ Z assign the square of an integer to this integer. Then, I(x) = x2, where the domain of I is the set of all integers, we take the the codomain of I to be the set of all integers, and the range of I is the set of all integers that are perfect squares, namely, {O, 1,4, 9, ... }. � The domain and codomain of functions are often specified in programming languages. For instance, the Java statement int ftoor(float real){ ... } and the Pascal statement functionftoor(x: real): integer both state that the domain of the floor function is the set of real numbers and its codomain is the set of integers. � Two real-valued functions with the same domain can be added and multiplied. DEFINITION 3 Let II and!2 be functions from A to R. Then II +!2 and Id2 are also functions from A to Rdefined by (II + !2)(x) = II (x) + !2(x), (/1!2)(X) = fi(x)/2(X). Note that the functions It + !2 and It!2 have been defined by specifying their values at x in terms of the values of It and !2 at x. EXAMPLE 6 Let It and !2 be functions from R to R such that It(x) = x2 and 12(X) = x - x2. What are the functions It + !2 and It!2? Solution: From the definition of the sum and product of functions, it follows that (/t + !2)(x) = It (x) + !2(x) = x2 + (x - x2) = X and When I is a function from a set A to a set B, the image of a subset of A can also be defined. 136 2 / Basic Structure s: Sets, Functions, Sequences, and Sums 2-26 DEFINITION 4 Let f be a function from the set A to the set B and let S be a subset of A. The image of S under the function f is the subset of B that consists of the images of the elements of S. We denote the image of S by f(S), so f(S) = { t I3sES (t = f(s»}. We also use the shorthand (f(s) I s E S} to denote this set. Remark: The notation f(S) for the image of the set S under the function f is potentially ambiguous. Here, f(S) denotes a set, and not the value of the function f for the set S. EXAMPLE 7 Let A = {a, b, c, d, e} and B = {l, 2, 3, 4} with f(a) = 2, f(b) = I, f(c) = 4, f(d) = I, and f(e) = 1. The image of the subset S = {b, c, d} is the set f(S) = {I, 4}. .... One-to-One and Onto Functions Some functions never assign the same value to two different domain elements. These functions are said to be one-to-o ne. DEFINITION 5 A function f is said to be one-to-one, or injective, if and only if f(a) = f(b) implies that a = b for all a and b in the domain of f. A function is said to be an injection if it is one-to-one. Assessmenl � EXAMPLE S Extra� Examples � EXAMPLE 9 Note that a function f is one-to-one if and only if f(a) =1= f(b) whenever a =1= b. This way of expressing that f is one-to-one is obtained by taking the contrapositive of the implication in the definition. Remark: Wecanexpressthatfis one-to-one using quantifiers asVaVb(f(a) = f(b) -+ a = b) or equivalently VaVb(a =1= b -+ f(a) =1= f(b », where the universe of discourse is the domain of the function. We illustrate this concept by giving examples of functions that are one-to-one and other functions that are not one-to-one. Determine whether the function f from fa, b, c, d} to {I, 2, 3, 4, 5} with f(a) == 4, f(b) = 5, f(c) = I, and f(d) = 3 is one-to-one. Solution: The function f is one-to-one because f takes on different values at the four elements of its domain. This is illustrated in Figure 3. .... Determine whether the function f(x) = x2 from the set of integers to the set of integers is one-to-one. Solution: The function f(x) = x2 is not one-to-one because, for instance, f(l) f(-I) = 1, but 1=1=-1. 2-27 a. b. c. d. .5 FIGURE 3 A One-to-One Function. 2.3 Functions 137 Note that the function f(x) = x2 with its domain restricted to Z+ is one-to-one. (Technical ly, when we restrict the domain of a function, we obtain a new function whose values agree with those of the original function for the elements of the restricted domain. The restricted function is not defined for elements of the original domain outside of the restricted domain.) .... EXAMPLE 10 Determine whether the function f(x) = x + I from the set of real numbers to itselfis one-to-one. DEFINITION 6 DEFINITION 7 Solution: The function f(x) = x + I is a one-to-one function. To demonstrate this, note that x + I =1= y + I when x =1= y. .... We now give some conditions that guarantee that a function is one-to-one. A function f whose domain and codomain are subsets of the set of real numbers is called increasing if f(x) .::: f(Y), and strictly increasing if f(x) < f(Y), whenever x < y and x and y are in the domain of f. Similarly, f is called decreasing if f(x) ::: f(Y), and strictly decreasing if f(x) > f(Y), whenever x < y and x and y are in the domain of f. (The word strictly in this definition indicates a strict inequality.) Remark: A function f is increasing if VxVy(x < y -+ f(x) .::: f(Y», strictly increasing if VxVy(x < y -+ f(x) < f(Y», decreasing if VxVy(x < y -+ f(x) ::: f(Y», and strictly de­ creasing ifVxVy(x < y -+ f(x) > f(Y», where the universe of discourse is the domain of f. From these definitions, we see that a function that is either strictly increasing or strictly decreasing must be one-to-one. However, a function that is increasing, but not strictly increasing, or decreasing, but not strictly decreasing, is not necessarily one-to-one. For some functions the range and the codomain are equal. That is, every member of the codomain is the image of some element of the domain. Functions with this property are called onto functions. A function f from A to B is called onto, or surjective, if and only if for every element b E B there is an element a E A with f(a) = b. A function f is called a surjection ifit is onto. Remark: A function f is onto ifVy3x(f(x) = y), where the domain for x is the domain of the function and the domain for y is the codomain of the function. We now give examples of onto functions and functions that are not onto. 138 2 / Basic Structure s: Sets, Functions, Sequences, and Sums 2-28 EXAMPLE 11 Ex1ra� Examples .... EXAMPLE 12 ae� be el ce e2 d e ---- -; .. � e 3 FIGUR E 4 An Onto Function. Let f be the function from {a, b, c, d} to {I, 2, 3} defined by f(a) = 3, f(b) = 2, f(c) = 1, and f(d) = 3. Is f an onto function? Solution: Because all three elements of the codomain are images of elements in the domain, we see that f is onto. This is illustrated in Figure 4. Note that if the codomain were {l, 2, 3, 4}, then f would not be onto. .... Is the function f(x) = x2 from the set of integers to the set of integers onto? Solution: ·The function f is not onto because there is no integer x with x2 = -1, for instance. .... EXAMPLE 13 Is the function f(x) = x + 1 from the set of integers to the set of integers onto? DEFINITION 8 Solution: This function is onto, because for every integer y there is an integer x such that f(x) = y. To see this, note that f(x) = y if and only if x + 1 = y, which holds if and only if x=y-l. .... The function f is a one-to-one correspondence, or a bijection, if it is both one-to-one and onto. Examples 14 and 15 illustrate the concept of a bijection. EXAMPLE 14 Let f be the function from {a, b, c, d} to {l, 2, 3, 4} with f(a) = 4, f(b) = 2, f(c) = 1, and f(d) = 3. Is f a bijection? Solution: The function f is one-to-one and onto. It is one-to-one because no two values in the domain are assigned the same function value. It is onto because all four elements of the codomain are images of elements in the domain. Hence, f is a bijection. .... Figure 5 displays four functions where the first is one-to-one but not onto, the second is onto but not one-to-one, the third is both one-to-one and onto, and the fourth is neither one-to-one nor onto. The fifth correspondence in Figure 5 is not a function, because it sends an element to two different elements. Suppose that f is a function from a set A to itself. If A is finite, then f is one-to-one if and only if it is onto. (This follows from the result in Exercise 68 at the end of this section.) This is not necessarily the case if A is infinite (as will be shown in Section 2.4). EXAMPLE 15 Let A be a set. The identity function on A is the function LA : A -+ A, where LA(X) = x for all x E A. In other words, the identity function LA is the function that assigns each element to itself. The function LA is one-to-one and onto, so it is a bijection. (Note that L is the Greek letter iota.) .... 2-29 2.3 Functions 139 (a) One-to-one, (b) Onto, (c) One-to-one, (d) Neither one-to-one (e) Not a function not onto not one-to-one and onto nor onto .1 a .� a . .1 a.>< .1 .1 .o;z .1 .O� • 2 b • b. .2 ::::=:::: . 2 b. .2 b. .3 c.� c • .3 .3 c. • 3 c. .4 d. d. .4 d. .4 .4 FIGURE 5 Examples of Different Types of Corresp ondences. DEFINITION 9 Inverse Functions and Compositions of Functions Now consider a one-to-one correspondence f from the set A to the set B. Because f is an onto function, every element of B is the image of some element in A. Furthermore, because f is also a one-to-one function, every element of B is the image of a unique element of A. Consequently, we can define a new function from B to A that reverses the correspondence given by f. This leads to Definition 9. Let f be a one-to-one correspondence from the set A to the set B. The inverse function of f is the function that assigns to an element b belonging to B the unique element a in A such that f(a) = b. The inverse function of f is denoted by f-I. Hence, f-I(b) = a when f(a) = b. Remark: Be sure not to confuse the function f-I with the function Ilf, which is the function that assigns to each x in the domain the value 1 I f(x). Notice that the latter makes sense only when f(x) is a non-zero real number. They are not the same. Figure 6 illustrates the concept of an inverse function. If a function f is not a one-to-one correspondence, we cannot define an inverse function of f. When f is not a one-to-one correspondence, either it is not one-to-one or it is not onto. If f is not one-to-one, some element b in the codomain is the image of more than one element in the domain. If f is not onto, for some element b in the codomain, no element a in the domain exists for which f(a) = b. Consequently, if f is not a one-to-one correspondence, we cannot assign to each element b in the codomain a unique element a in the domain such that f(a) = b (because for some b there is either more than one such a or no such a). FIGUR E 6 The Function I-I Is the Inverse of Function I. 140 2/ Basic Structure s: Sets, Functions, Sequences, and Sums 2-30 A one-to-one correspondence is called invertible because we can define an inverse of this function. A function is not invertible if it is not a one-to-one correspondence, because the inverse of such a function does not exist. EXAMPLE 16 Let f be the function from {a, b, c} to {l, 2, 3} such that f(a) = 2, feb) = 3, and f(c) = 1. Is f invertible, and if it is, what is its inverse? Solution: The function f is invertible because it is a one-to-one correspondence. The in­ verse function f-I reverses the correspondence given by f, so f-I(1) = c, f-I(2) = a, and f-I(3) = b. .... EXAMPLE 17 Let f : Z -+ Z be such that f(x) = x + 1. Is f invertible, and ifit is, what is its inverse? Solution: The function f has an inverse because it is a one-to-one correspondence, as we have shown. To reverse the correspondence, suppose that y is the image of x, so that y = x + 1. Then x = y -1. This means that y - 1 is the unique element of Z that is sent to y by f. Consequently, f-I(y) = Y -1. .... EXAMPLE 18 Let f be the function from R to R with f(x) = x2. Is f invertible? Solution: Because f( -2) = f(2) = 4, f is not one-to-one. If an inverse function were defined, it would have to assign two elements to 4. Hence, f is not invertible. .... Sometimes we can restrict the domain or the codomain of a function, or both, to obtain an invertible function, as Example 19 illustrates. EXAMPLE 19 Show that if we restrict the function f(x) = x2 in Example 18 to a function from the set of all nonnegative real numbers to the set of all nonnegative real numbers, then f is invertible. DEFINITION 10 Solution: The function f(x) = x2 from the set of nonnegative real numbers to the set of non­ negative real numbers is one-to-one. To see this, note that if f(x) = f(Y), then x2 = y2, so x2 - y2 = (x + y)(x -y) = O. This means that x + y = Oorx -y = 0, sox = -yorx = y. Because both x and yare nonnegative, we must have x = y. So, this function is one-to-one. Furthermore, f(x) = x2 is onto when the codomain is the set of all nonnegative real numbers, because each nonnegative real number has a square root. That is, if y is a nonnegative real number, there exists a nonnegative real number x such that x = ,.;y, which means that x2 = y. Because the function f(x) = x2 from the set of nonnegative real numbers to the set of non­ negative real numbers is one-to-one and onto, it is invertible. Its inverse is given by the rule f-I(y) = ,.;y. .... Let g be a function from the set A to the set B and let f be a function from the set B to the set C. The composition of the functions f and g, denoted by fog, is defined by (f 0 g)(a) = f(g(a)). In other words, fog is the function that assigns to the element a of A the element assigned by f to g(a). That is, to find (f 0 g)(a) we first apply the function g to a to obtain g(a) and then we apply the function f to the result g(a) to obtain (f 0 g)(a) = f(g(a)). Note that the 2-31 (f 0 g)(a) A B fog FIGURE 7 The Composition ofthe Functions! and g. 2.3 Functions 141 • f(g(a» c composition fog cannot be defined unless the range of g is a subset of the domain of f. In Figure 7 the composition of functions is shown. EXAMPLE 20 Let g be the function from the set {a, b, c} to itself such that g(a) = b, g(b) = c, andg(c) = a. Letfbe the function from the set {a, b, c} to the set {l, 2, 3} such that f(a) = 3, feb) = 2, and f(c) = 1. What is the composition of f and g, and what is the composition of g and f? Solution: The composition fog is defined by (f 0 g)(a) = f(g(a)) = feb) = 2, (f 0 g) (b) = f(g(b)) = f(c) = 1, and (f 0 g)(c) = f(g(c)) = f(a) = 3. Note that g 0 f is not defined, because the range of f is not a subset ofthe domain of g ..... EXAMPLE 21 .Let f and g be the functions from the set of integers to the set of integers defined by f(x) = 2x + 3 and g(x) = 3x + 2. What is the composition of f and g? What is the composition of g andf? Solution: Both the compositions fog and g 0 f are defined. Moreover, (f 0 g)(x) = f(g(x)) = f(3x + 2) = 2(3x + 2) + 3 = 6x + 7 and (g 0 f)(x) = g(f(x)) = g(2x + 3) = 3(2x + 3) + 2 = 6x + 11. Remark: Note that even though fog and g 0 f are defined for the functions f and g in Example 21, fog and g 0 f are not equal. In other words, the commutative law does not hold for the composition of functions. When the composition of a function and its inverse is formed, in either order, an identity function is obtained. To see this, suppose that f is a one-to-one correspondence from the set A to the set B. Then the inverse function f -I exists and is a one-to-one correspondence from B to A. The inverse function reverses the correspondence of the original function, so f-I(b) = a when f(a) = b, and f(a) = b when f -I(b) = a. Hence, (f-I 0 f)(a) = f-I(f(a)) = f-\b) = a, and (f 0 f-I)(b) = f(f-I(b)) = f(a) = b. 142 2/ Basic Structure s: Sets, Functions, Sequences, and Sums 2-32 DEFINITION 11 o 0 o • 000 o 0 0 0 000 o 0 .00 0 0 o 0 0 0 0 0 0 o 0 o • o 0 o 0 0 0 0 00000 o 0 0 0 0 FIGURE 8 The Graph of f(n) = 2n + 1 from Z to Z. e(-3,9) e(-2,4) (2,4) e (-1,1) e e (I, 1) (0,0) FIGURE 9 The Graph of f(x) = x2 from Z to Z. (3,9)e Consequently 1-1 0 I = LA and 101-1 = LB, where LA and LB are the identity functions on the sets A and B, respectively. That is, (1-1)-1 = I. The Graphs of Functions We can associate a set of pairs in A x B to each function from A to B. This set of pairs is called the graph of the function and is often displayed pictorially to aid in understanding the behavior of the function. Let I be a function from the set A to the set B. The graph of the function I is the set of ordered pairs {(a, b) I a E A and I(a) = b}. From the definition, the graph of a function I from A to B is the subset of A x B containing the ordered pairs with the second entry equal to the element of B assigned by I to the first entry. EXAMPLE 22 Display the graph of the function I(n) = 2n + 1 from the set of integers to the set of integers. Solution: The graph of I is the set of ordered pairs of the form (n, 2n + 1), where n is an integer. This graph is displayed in Figure 8. .... EXAMPLE 23 Display the graph of the function I(x) = x2 from the set of integers to the set of integers. Solution: The graph of I is the set of ordered pairs of the form (x, I(x» = (x, x2), where x is an integer. This graph is displayed in Figure 9. .... Some Important Functions Next, we introduce two important functions in discrete mathemati cs, namely, the floor and ceiling functions. Let x be a real number. The floor function rounds x down to the closest integer less than or equal to x, and the ceiling function rounds x up to the closest integer greater than or 2-33 DEFINITION 12 -3 -2 -1 3 2 -1 ---.0-2 -3 (a) y = [xl ---.0 2 3 3 2 -3 -2 -1 � -1 -2 -3 (b) y=[xl FIGURE 10 Graphs ofthe (a) Floor and (b) Ceiling Functions. 2.3 Functions 143 � 2 3 equal to x. These functions are often used when objects are counted. They play an important role in the analysis of the number of steps used by procedures to solve problems of a particular SIze. Thefioor function assigns to the real number x the largest integer that is less than or equal to x. The value ofthe floor function at x is denoted by Lx J. The ceilingfimction assigns to the real number x the smallest integer that is greater than or equal to x. The value of the ceiling function at x is denoted by r xl. Remark: The floor function is often also called the greatest integer function. It is often denoted by [xl EXAMPLE 24 These are some values ofthe floor and ceiling functions: linkS� EXAMPLE 25 L!J = 0, r!l = 1, L -!J = -1, r -!1 = 0, L3.1J = 3, r3.1l = 4, L7J = 7, f71 = 7. � We display the graphs of the floor and ceiling functions in Figure 10. In Figure 10(a) we display the graph of the floor function LxJ. Note that this function has the same value throughout the interval [n, n + 1), namely n, and then it jumps up to n + 1 when x = n + 1. In Figure 10(b) we display the graph of the ceiling function r xl. Note that this function has the same value throughout the interval (n, n + 1], namely n + 1, and then jumps to n + 2 when x is a little larger than n + 1. The floor and ceiling functions are useful in a wide variety of applications, including those involving data storage and data transmission. Consider Examples 25 and 26, typical of basic calculations done when database and data communications problems are studied. Data stored on a computer disk or transmitted over a data network are usually represented as a string of bytes. Each byte is made up of 8 bits. How many bytes are required to encode 100 bits of data? Solution: To determine the number of bytes needed, we determine the smallest integer that is at least as large as the quotient when 100 is divided by 8, the number of bits in a byte. Consequently, POO/81 = r12.51 = 13 bytes are required. � 144 2/ Basic Structure s: Sets, Functions, Sequences, and Sums TABLE 1 Useful Properties ofthe Floor and Ceiling Functions. (n is an integer) (I a) LxJ = n if and only ifn :-:: x < n + I (I b) r xl = n if and only if n - I < x :-:: n (I c) Lx J = n if and only if x - I < n :-:: x (I d) r xl = n if and only if x :-:: n < x + I (2) x - I < LxJ :-:: x :-:: rxl < x + I (3a) L -xJ = -fxl (3b) r -xl = -LxJ (4a) Lx +nJ = LxJ +n (4b) rx + nl = rxl + n 2-34 EXAMPLE 26 In asynchronous transfer mode (ATM) (a communications protocol used on backbone networks), data are organized into cells of 53 bytes. How many ATM cells can be transmitted in 1 minute over a connection that transmits data at the rate of 500 kilobits per second? Solution: In 1 minute, this connection can transmit 500,000·60 = 30,000,000 bits. Each ATM cell is 53 bytes long, which means that it is 53 . 8 = 424 bits long. To determine the number of cells that can be transmitted in 1 minute, we determine the largest integer not exceeding the quotient when 30,000,000 is divided by 424. Consequently, L30,000,000j424J = 70,754 ATM cells can be transmitted in 1 minute over a 500 kilobit per second connection. ... Table 1, with x denoting a real number, displays some simple but important properties of the floor and ceiling functions. Because these functions appear so frequently in discrete mathematics, it is useful to look over these identities. Each property in this table can be established using the definitions of the floor and ceiling functions. Properties (la), (lb), (lc), and (ld) follow directly from these definitions. For example, (l a) states that Lx J = n if and only if the integer n is less than or equal to x and n + 1 is larger than x. This is precisely what it means for n to be the greatest integer not exceeding x, which is the definition of LxJ = n. Properties (lb), (lc), and (ld) can be established similarly. We will prove property (4a) using a direct proof. Proof: Suppose that LxJ = m, where m is a positive integer. By property (la), it follows thatm ::::; x < m + 1. Adding n to both sides of this inequality shows that m + n ::::; x + n < m + n + 1. Using property (la) again, we see that Lx + nJ = m + n = LxJ + n. This completes the proof. Proofs of the other properties are left as exercises. <I The floor and ceiling functions enjoy many other useful properties besides those displayed in Table 1. There are also many statements about these functions that may appear to be correct, but actually are not. We will consider statements about the floor and ceiling functions in Examples 27 and 28. A useful approach for considering statements about the floor function is to let x = n + E, where n = Lx J is an integer, and E, the fractional part of x, satisfies the inequality 0 ::::; E < 1. Similarly, when considering statements about the ceiling function, it is useful to write x = n -E, where n = rxl is an integer and 0 ::::; E < 1. 2-35 EXAMPLE 27 2.3 Functions 145 Prove that if x is a real number, then L2x J = Lx J + Lx + � J. Solution: To prove this statement we let x = n + E, where n is a positive integer and 0 :s E < I. There are two cases to consider, depending on whether E is less than or greater than or equal to �. (The reason we choose these two cases will be made clear in the proof.) We first consider the case when 0 :s E < �. In this case, 2x = 2n + 2E and L2xJ = 2n because 0 :s 2E < 1. Similarly, x + � = n + (� + E), so Lx + �J = n, because 0 < � + E < 1. Consequently, L2xJ = 2n and LxJ + Lx + �J = n + n = 2n. Next, we consider the case when � :s E < I. In this case, 2x = 2n + 2E = (2n + 1) + (2E - 1). Because 0 :s 2E - 1 < 1, it follows that L2xJ = 2n + 1. Because Lx + �J = Ln + (� + E)J = Ln + 1 + (E -�)J and 0 :s E -� < 1, it follows that Lx + �J = n + 1. Consequently, L2xJ = 2n + 1 and LxJ + Lx + �J = n + (n + 1) = 2n + 1. This concludes the proof. � EXAMPLE 28 Prove or disprove that r x + y 1 = r xl + r y 1 for all real numbers x and y. Solution: Although this statement may appear reasonable, it is false. A counterexample is supplied by x = � and y = �. With these values we find that rx + yl = r� + �l = rl1 = 1, but r xl + r y 1 = r � 1 + r � 1 = 1 + 1 = 2. � There are certain types of functions that will be used throughout the text. These include polynomial, logarithm ic, and exponential functions. A brief review of the properties of these functions needed in this text is given in Appendix 2. In this book the notation log x will be used to denote the logarithm to the base 2 of x, because 2 is the base that we will usually use for logarithm s. We will denote logarithms to the base b, where b is any real number greater than 1, by 10gb x, and the natural logarithm by In x . Another function we will use throughout this text is the factorial function f : N -+ Z+, denoted by f(n) = n!. The value of f(n) = n! is the product of the first n positive integers, so f(n) = I ·2··· (n -I)· n [and f(O) = O! = 1]. EXAMPLE29 We have f(l)=l!=l, f(2)=2!=1·2=2, f(6)=6!=1·2·3·4·5·6=720, and f(20) 1·2·3·4·5·6·7·8·9·10·11 . 12·13 ·14·15·16·17·18·19·20 = 2,432,902,008, 176,640,000. � unkS� JAMES STIRLING (\692-\770) was born near the town of Stirling, Scotland. His family strongly supported the Jacobite cause of the Stuarts as an alternative to the British crown. The first infonnation known about James is that he entered Balliol College, Oxford, on a scholar�hip in 1711. However, he later lost his scholarship when he refused to pledge his allegiance to the British crown. The first Jacobean rebellion took place in 1715, and Stirling was accused of communicating with rebels. He was charged with cursing King George, but he was acquitted of these charges. Even though he could not graduate from Oxford because of his politics, he remained there for several years. Stirling published his first work, which extended Newton's work on plane curves, in 1717. He traveled to Venice, where a chair of mathematics had been promised to him, an appointment that unfortunately fell through. Neverthe less, Stirling stayed in Venice, continuing his mathematical work. He attended the University of Padua in 1721, and in 1722 he returned to Glasgow. Stirling apparently fled Italy after learning the secrets of the Italian glass industry, avoiding the efforts of Italian glass makers to assassinate him to protect their secrets. In late 1724 Stirling moved to London, staying there 10 years teaching mathematics and actively engaging in research. In 1730 he published Methodus Differentialis, his most important work, presenting results on infinite series, summations, interpolation, and quadrature . It is in this book that his asymptotic fonnula for n! appears. Stirling also worked on gravitation and the shape of the earth; he stated, but did not prove, that the earth is an oblate spheroid. Stirling returned to Scotland in 1735, when he was appointed manager of a Scottish mining company. He was very successful in this role and even published a paper on the ventilation of mine shafts. He continued his mathematical research, but at a reduced pace, during his years in the mining industry. Stirling is also noted for surveying the River Clyde with the goal of creating a series of locks to make it navigable. In 1752 the citizens of Glasgow presented him with a silver teakettle as a reward for this work. 146 2/ Basic Structures: Sets, Functions, Sequences, and Sums 2-36 Example 29 illustrates that the factorial function grows extremely rapidly as n grows. The rapid growth of the factorial function is made clearer by Stirling's formula, a result from higher mathematics that tell us that n! '" J2rrn(n/et. Here, we have used the notation f(n) '" g(n), which means that the ratio f(n)/g(n) approaches 1 as n grows without bound (that is, limn-4oo f(n)/ g(n) = 1). The symbol '" is read "is asymptotic to." Stirling's formula is named after James Stirling, a Scottish mathemati cian of the eighteenth century. Exercises 1. Why is f not a function from R to R if a) f(x) = 1/x? b) f(x) = .JX? ,.--,,..--c) f(x) = ±J(x2 + I)? 2. Determine whether f is a function from Z to R if a) f(n) = ±n. b) f(n) = J1i2+1. c) f(n) = 1/(n2 -4). 3. Determine whether f is a function from the set of all bit strings to the set of integers if a) f(S) is the position of a 0 bit in S. b) f(S) is the number of 1 bits in S. c) f(S) is the smallest integer i such that the ith bit of Sis 1 and f(S) = 0 when S is the empty string, the string with no bits. 4. Find the domain and range of these functions. Note that in each case, to find the domain, determine the set of ele­ ments assigned values by the function. a) the function that assigns to each nonnegative integer its last digit b) the function that assigns the next largest integer to a positive integer c) the function that assigns to a bit string the number of one bits in the string d) the function that assigns to a bit string the number of bits in the string 5. Find the domain and range of these functions. Note that in each case, to find the domain, determine the set of ele­ ments assigned values by the function. a) the function that assigns to each bit string the number of ones minus the number of zeros b) the function that assigns to each bit string twice the number of zeros in that string c) the function that assigns the number of bits left over when a bit string is split into bytes (which are blocks of 8 bits) d) the function that assigns to each positive integer the largest perfect square not exceeding this integer 6. Find the domain and range of these functions. a) the function that assigns to each pair of positive inte­ gers the first integer of the pair b) the function that assigns to each positive integer its largest decimal digit c) the function that assigns to a bit string the number of ones minus the number of zeros in the string d) the function that assigns to each positive integer the largest integer not exceeding the square root of the integer e) the function that assigns to a bit string the longest string of ones in the string 7. Find the domain and range of these functions. a) the function that assigns to each pair of positive inte­ gers the maximum of these two integers b) the function that assigns to each positive integer the number of the digits 0, 1,2,3,4,5,6,7, 8,9 that do not appear as decimal digits of the integer c) the function that assigns to a bit string the number of times the block 11 appears d) the function that assigns to a bit string the numerical position of the first 1 in the string and that assigns the value 0 to a bit string consisting of all Os 8. Find these values. a) Ll.1J c) L -O.lJ e) r2.991 g) L!+r!lJ 9. Find these values. b) r1.11 d) r -0·11 1) r -2.991 h) r L! J + r! 1 + ! 1 a) r�l b) L�J c) r-�l d) L-�J e) r31 1) L -lJ g) L! + r � 1 J h) L!' L � J J 10. Determine whether each of these functions from {a, b, c, d} to itself is one-to-one. a) f(a) = b, f(b) = a, f(c) = c, f(d) = d b) f(a) = b, f(b) = b, f(c) = d, f(d) = c c) f(a) = d, f(b) = b, f(c) = c, f(d) = d 11. Which functions in Exercise 10 are onto? 12. Determine whether each of these functions from Z to Z is one-to-one. a) f(n) = n - 1 c) f(n) = n3 b) f(n) = n2 + 1 d) f(n) = rn 121 13. Which functions in Exercise 12 are onto? 14. Determine whether f: Z x Z � Z is onto if a) f(m,n)=2m-n. b) f(m, n) = m2 -n2• 2-37 c) f(m, n) = m + n + 1. d) f(m, n) = Iml-Inl. e) f(m, n) = m2 -4. 15. Detennine whether the function f: Z x Z � Z is onto if a) f(m, n) = m + n. b) f(m, n) = m2 + n2• c) f(m, n) = m. d) f(m, n) = Inl. e) f(m,n)=m-n. 16. Give an example of a function from N to N that is a) one-to-one but not onto. b) onto but not one-to-one. c) both onto and one-to-one (but different from the iden­ tity function). d) neither one-to-one nor onto. 17. Give an explicit fonnula for a function from the set of integers to the set of positive integers that is a) one-to-one, but not onto. b) onto, but not one-to-one. c) one-to-one and onto. d) neither one-to-one nor onto. 18. Detennine whether each of these functions is a bijection fromRtoR. a) f(x) = -3x +4 b) f(x) = -3x2 + 7 c) f(x) = (x + I)/(x + 2) d) f(x) = x 5 + I 19. Detennine whether each of these functions is a bijection fromR to R. a) f(x) = 2x + 1 b) f(x) = x2 + 1 c) f(x) = x3 d) f(x) = (x2 + I)/(x2 + 2) 20. Let f: R � R and let f(x) > 0 for all x E R. Show that f(x) is strictly increasing if and only if the function g(x) = 1/ f(x) is strictly decreasing. 21. Letf: R � Randletf(x) > O.Showthatf(x)isstrictly decreasing if and only if the function g(x) = 1/ f(x) is strictly increasing. 22. Qive an example of an increasing function with the set of real numbers as its domain and codomain that is not one-to-one. 23. Give an example of a decreasing function with the set of real numbers as its domain and codomain that is not one-to-one. 24. Show that the function f(x) = eX from the set of real number to the set of real numbers is not invertible, but if the codomain is restricted to the set of positive real numbers, the resulting function is invertible. 25. Show that the function f(x) = Ix I from the set of real numbers to the set of nonnegative real numbers is not invertible, but if the domain is restricted to the set of nonnegative real numbers, the resulting function is invertible. 2.3 Functions 147 26. Let S = {-I, 0, 2, 4, 7}. Find f(S) if a) f(x) = 1. b) f(x) = 2x + 1. c) f(x) = fx/5l d) f(x)=L(x2 + I)/3J. 27. Let f(x) = Lx2/3J. Find f(S) if a) S = {-2, -1, 0,1,2, 3}. b) S = {O, 1, 2, 3 ,4, 5}. c) S = {l, 5, 7, ll}. d) S = {2, 6, 10, 14}. 28. Let f(x) = 2x. What is a) f(Z)? b) f(N)? c) f(R)? 29. Suppose that g is a function from A to B and f is a func­ tion from B to C. a) Show that if both f and g are one-to-one functions, then fog is also one-to-one. b) Show that if both f and g are onto functions, then fog is also onto. *30. If f and fog are one-to-one, does it follow that g is one-to-one? Justify your answer. *31. If f and fog are onto, does it follow that g is onto? Justify your answer. 32. Find fog and g 0 f, where f(x) = x2 + 1 and g(x) = x + 2, are functions from R to R. 33. Find f + g and fg for the functions f and g given in Exercise 32. 34. Letf(x) = ax + bandg(x) = ex + d, where a, b, e,and d are constants. Detennine for which constants a, b, e, and d it is true that fog = g 0 f. 35. Show that the function f(x) = ax + b from R to R is invertible, where a and b are constants, with a #-0, and find the inverse of f. 36. Let f be a function from the set A to the set B. Let Sand T be subsets of A. Show that a) f(S U T) = f(S) U f(T). b) f(S n T) c;:: f(S) n f(T). 37. Give an example to show that the inclusion in part (b) in Exercise 36 may be proper. Let f be a function from the set A to the set B. Let S be a subset of B. We define the inverse image of S to be the subset of A whose elements are precisely all pre-images of all ele­ ments of S. We denote the inverse image of S by f-I (S), so f-I(S) = {a E A I f(a) E S}. (Beware: The notation f-I is used in two different ways. Do not confuse the notation intro­ duced here with the notation f-I (y) for the value at y of the inverse of the invertible function f. Notice also that f-I(S), the inverse image of the set S, makes sense for all functions f, not just invertible functions.) 38. Let f be the function from R to R defined by f(x) = x2• Find a) f-I({I}). b) f-I({x 10 < x < I}). c) f-I({x I x > 4}). 39. Letg(x) = LxJ.Find a) g-I({O}). b) g-I({-I, O, I}). c) g-I({X 10 < x < I}). 148 2/ Basic Structures: Sets, Functions, Sequences, and Sums 40. Let f be a function from A to B. Let S and T be subsets of B. Show that a) f-I(S U T) = f-I(S) U f-I(T). b) f-I(S n T) = f-I(S) n f-I(T). 41. Let f be a function from A to B. Let S be a subset of B. Show that f-I(:5) = f-I(S). 42. Show that Lx + � J is the closest integer to the number x, except when x is midway between two integers, when it is the larger of these two integers. 43. Show that f x -� 1 is the closest integer to the number x, except when x is midway between two integers, when it is the smaller of these two integers. 44. Show that if x is a real number, then fxl -LxJ = I if x is not an integer and fxl -LxJ = 0 if x is an integer. 45. Show that if x is a real number, then x-I < Lx J ::::: x ::::: fxl < x + 1. 46. Show that if x is a real number and m is an integer, then fx + ml = fxl + m. 47. Show that if x is a real number and n is an integer, then a) x < n if and only if LxJ < n. b) n < x if and only ifn < fxl 48. Show that if x is a real number and n is an integer, then a) x ::::: n ifandonlyi[fxl ::::: n. b) n ::::: x if and only ifn ::::: LxJ. 49. Prove that ifn is an integer, then Ln/2J = n/2 ifn is even and (n -1)/2 ifn is odd. 50. Prove that if x is a real number, then L -x J = -f x 1 and f -xl = -LxJ. 51. The function INT is found on some calculators, where INT(x) = Lx J when x is a nonnegative real number and INT(x) = f xl when x is a negative real number. Show that this INT function satisfies the identity INT( -x) = -INT(x). 52. Let a and b be real numbers with a < b. Use the floor and/or ceiling functions to express the number of integers n that satisfy the inequality a ::::: n ::::: b. 53. Let a and b be real numbers with a < b. Use the floor and/or ceiling functions to express the number of integers n that satisfy the inequality a < n < b. 54. How many bytes are required to encode n bits of data where n equals a) 4? b) 1O? c) 500? d) 3000? 55. How many bytes are required to encode n bits of data where n equals a)7? b) 17? c) 1001? d) 28,800? 56. How many ATM cells (described in Example 26) can be transmitted in 10 seconds over a link operating at the fol­ lowing rates? a) 128 kilobits per second (1 kilobit = 1000 bits) b) 300 kilobits per second c) I megabit per second (1 megabit = 1,000,000 bits) 2-38 57. Data are transmitted over a particular Ethernet network in blocks of 1500 octets (blocks of 8 bits). How many blocks are required to transmit the following amounts of data over this Ethernet network? (Note that a byte is a syn­ onym for an octet, a kilobyte is 1000 bytes, and a megabyte is 1,000,000 bytes.) a) 150 kilobytes of data b) 384 kilobytes of data c) 1.544 megabytes of data d) 45.3 megabytes of data 58. Draw the graph of the function fen) = 1 - n2 from Z to z. 59. Draw the graph of the function f(x) = L2xJ from R to R. 60. Draw the graph of the function f(x) = Lx/2J from R to R. 61. Draw the graph of the function f(x) = Lx J + Lx /2J from RtoR. 62. Draw the graph ofthe function f(x) = fxl + Lx/2J from RtoR. 63. Draw graphs of each of these functions. a) f(x) = Lx + �J b) f(x) = L2x + IJ c) f(x) = fx/31 d) f(x) = fl/xl e) f(x) = fx - 21 + Lx + 2J f) f(x) = L2xHx/21 g) f(x) = fLx -�J + �l 64. Draw graphs of each of these functions. a) f(x) = f3x - 21 b) f(x) = fO.2xl c) f(x) = L -1/xJ d) f(x) = Lx2 J e) f(x) = fx/21 Lx/2J f) f(x) = Lx/2J + fx/2J g) f(x) = L2 fx/2l + �J 65. Find the inverse function of fex) = x3 + 1. 66. Suppose that f is an invertible function from Y to Z and g is an invertible function from X to Y. Show that the inverse of the composition fog is given by (f 0 g)-I = g-Iof-I. 67. Let S be a subset of a universal set U. The characteristic function fs of S is the function from U to the set to, 1} such that fs(x) = 1 if x belongs to Sand fsex) = 0 if x does not belong to S. Let A and B be sets. Show that for all x, a) hnBeX) = hex) . fBeX) b) hUB(X) = hex) + /Hex) - fA(X) . /Hex) c) hex) = 1 - fA(X) d) fAEBB(X) = fAeX) + fBeX) - 2h(x)fBeX) 68. Suppose that f is a function from A to B, where A and B are finite sets with 1 A 1 = 1 B I. Show that f is one-to-one if and only if it is onto. 69. Prove or disprove each ofthese statements about the floor and ceiling functions. a) rLx J 1 = Lx J for all real numbers x. b) L2xJ = 2LxJ whenever x is a real number. c) fxl + fyl -fx + yl = 0 or 1 �henever x and yare real numbers. . 2-39 d) rxyl = rxl ryl for all real numbers x andy. e) I � l = l x ; 1 J for all real numbers x. 70. Prove or disprove each ofthese statements about the floor and ceiling functions. a) LrxlJ = rxl for all real numbers x. b) Lx + y J = LxJ + LY J for all real numbers x and y. c) rrx/21/21 = rx/41 for all real numbersx. d) L,JrxT J = L..jX J for all positive real numbers x. e) LxJ + LYJ + Lx + yJ .::: L2xJ + L2yJ for all real numbers x and y. 71. Prove that if x is a positive real number, then a) LvTxT J = L..jX J. b) r,JrxTl = r..jXl. 72. Let x be a real number. Show that L3xJ = LxJ + Lx + 1 J + Lx + � J. A program designed to evaluate a function may not produce the correct value ofthe function for all elements in the domain of this function. For example, a program may not produce a correct value because evaluating the function may lead to an infinite loop or an overflow. Similarly, in abstract mathemat­ ics, we often want to discuss functions that are defined only for a subset of the real numbers, such as 1/ x, ..jX, and arcsin (x). Also, we may want to use such notions as the "youngest child" function, which is undefined for a couple having no children, or the "time of sunrise," which is undefined for some days above the Arctic Circle. To study such situations, we use the concept of a partial function. A partial function f from a set A to a set B is an assignment to each element a in a subset of A, called the do­ main of definition of f, of a unique element b in B. The sets A and B are called the domain and codomain of f, respec­ tively. We say that f is undefined for elements in A that are not in the domain of definition of f. We write f : A --+ B to denote that f is a partial function from A to B. (This is the same notation as is used for functions. The context in which the notation is used determines whether f is a partial function or a total function.) When the domain of definition of f equals A, we say that f is a total function. 2.4 Sequences and Summati ons Introduction 2.4 Sequences and Summations 149 73. For each of these partial functions, determine its domain, codomain, domain of definition, and the set of values for which it is undefined. Also, determine whether it is a total function. a) f: Z --+ R, fen) = lin b) f: Z --+ Z, fen) = rn/21 c) f:ZxZ--+ Q,f(m,n)=m/n d) f:ZxZ--+ Z,f(m,n)=mn e) f:ZxZ--+Z,f(m,n)=m-nifm>n 74. a) Show that a partial function from A to B can be viewed as a function f* from A to B U {u}, where u is not an element of Band If(a) f*(a) = u if a belongs to the domain of definition of f if f is undefined at a. b) Using the construction in (a), find the function f* cor­ responding to each partial function in Exercise 73. E3f>75. a) Show that if a set S has cardinality m, where m is a positive integer, then there is a one-to-one correspon­ dence between S and the set {I, 2, ... , m}. b) Show that if S and T are two sets each with m ele­ ments, where m is a positive integer, then there is a one-to-one correspondence between Sand T. *76. Show that a set S is infinite if and only ifthere is a proper subset A of S such that there is a one-to-one correspon­ dence between A and S. *77. Show that the polynomial function f : Z+ x Z+ --+ Z+ with f(m, n) = (m + n -2)(m + n -1)/2 + m is one­ to-one and onto. *78. Show that when you substitute (3n + 1)2 for each occur­ rence of n and (3m + 1)2 for each occurrence of m in the right-hand side of the formula for the function f(m, n) in Exercise 77, you obtain a one-to-one polynomial func­ tion Z x Z --+ Z. It is an open question whether there is a one-to-one polynomial function Q x Q --+ Q. Sequences are ordered lists of elements. Sequences are used in discrete mathematics in many ways. They can be used to represent solutions to certain counting problems, as we will see in Chapter 7. They are also an important data structure in computer science. This section contains a review of the notation used to represent sequences and sums of terms of sequences. When the elements of an infinite set can be listed, the set is called countable. We will conclude this section with a discussion of both countable and uncountable sets. We will prove that the set of rational numbers is countable, but the set of real numbers is not. 150 2/ Basic Structures: Sets, Functions, Sequences, and Sums 2-40 DEFINITION 1 Sequences A sequence is a discrete structure used to represent an ordered list. For example, 1,2,3,5,8 is a sequence with five terms and 1,3,9,27,81, ... ,30, ... is an infinite sequence. A sequence is a function from a subset of the set of integers (usually either the set{O, 1,2, ... } or the set {I, 2, 3, ... J) to a set S. We use the notation an to denote the image of the integer n. We call an a term of the sequence. We use the notation {an} to describe the sequence. (Note that an represents an individual term of the sequence {an}. Also note that the notation {an} for a sequence conflicts with the notation for a set. However, the context in which we use this notation will always make it clear when we are dealing with sets and when we are dealing with sequences. Note that although we have used the letter a in the notation for a sequence, other letters or expressions may be used depending on the sequence under consideration. That is, the choice of the letter a is arbitrary.) We describe sequences by listing the terms of the sequence in order of increasing subscripts. EXAMPLE 1 Consider the sequence {an}, where The list of the terms of this sequence, beginning with aI, namely, starts with 1 1 1 1, 2' 3' 4'···· DEFINITION 2 A geometric progression is a sequence of the form a, ar, ar2, ... , arn , ••• where the initial term a and the common ratio r are real numbers. Remark: A geometric progression is a discrete analogue ofthe exponential function f(x) = arX. EXAMPLE 2 The sequences {bn} with bn = (_I)n, {en} with Cn = 2· 5n, and {dn} with dn = 6· (l/3t are geometric progressions with initial term and common ratio equal to 1 and -1; 2 and 5; and 6 and 1/3, respective ly, if we start at n = 0. The list of terms bo, bl, b2, b3, b4, ••• begins with 1, -1,1, -1,1, ... ; 2-41 the list of terms co, C I , C2, C3, C4, ••• begins with 2,10,50,250,1250, ... ; and the list of terms do, dl, d2, d3, d4, ••. begins with 2 2 2 6,2,3'9'27'···· 2.4 Sequences and Summations 151 DEFINITION 3 An arithmetic progression is a sequence of the form a, a + d, a + 2d, ... , a + nd, ... where the initial term a and the common difference d are real numbers. Remark: An arithmetic progression is a discrete analogue of the linear function f(x) = dx + a. EXAMPLE 3 The sequences {sn} with Sn = -1 + 4n and {In} with fn = 7 - 3n are both arithmetic progres­ sions with initial terms and common differences equal to -1 and 4, and 7 and -3, respectively, if we start at n = 0. The list of terms so, Sl, S2, S3, ... begins with -1,3,7,11, ... , and the list of terms 10, fl, 12, 13, ... begins with 7,4,1,-2, .... Sequences of the form aI, a2, ... , an are often used in computer science. These finite sequences are also called strings. This string is also denoted by ala2 ... an. (Recall that bit strings, which are finite sequences of bits, were introduced in Section 1.1.) The length of the string S is the number of terms in this string. The empty string, denoted by A, is the string that has no terms. The empty string has length zero. EXAMPLE 4 The string abcd is a string of length four. Special Integer Sequences A common problem in discrete mathematics is finding a formula or a general rule for constructing the terms of a sequence. Sometimes only a few terms of a sequence solving a problem are known; the goal is to identify the sequence. Even though the initial terms of a sequence do not determine the entire sequence (after all, there are infinitely many different sequences that start with any finite set of initial terms), knowing the first few terms may help you make an educated conjecture about the identity of your sequence. Once you have made this conjecture, you can try to verify that you have the correct sequence. When trying to deduce a possible formula or rule for the terms of a sequence from the initial terms, try to find a pattern in these terms. You might also see whether you can determine how a term might have been produced from those preceding it. There are many questions you could ask, but some of the more useful are: • Are there runs of the same value? That is, does the same value occur many times in a row? • Are terms obtained from previous terms by adding the same amount or an amount that depends on the position in the sequence? • Are terms obtained from previous terms by multiplying by a particular amount? 152 2/ Basic Structure s: Sets, Functions, Sequences, and Sums 2-42 EXAMPLES Extra� ExampleS � • Are terms obtained by combining previous terms in a certain way? • Are there cycles among the terms? Find formulae for the sequences with the following first five terms: (a) 1, 1/2, 1/4, 1/8, 1/16 (b) 1,3,5,7,9 (c) 1, -1,1, -1,1. Solution: (a) We recognize that the denominators are powers of2. The sequence with an = 1 /2n, n = 0, 1, 2, ... is a possible match. This proposed sequence is a geometric progression with a = 1 and r = 1/2. (b) We note that each term is obtained by adding 2 to the previous term. The se­ quence with an = 2n + 1, n = 0, 1, 2, ... is a possible match. This proposed sequence is an arithmetic progression with a = 1 and d = 2. (c) The terms alternate between 1 and -1. The sequence with an = (-1 t, n = 0, 1, 2 ... is a possible match. This proposed sequence is a geometric progression with a = 1 and r = -1. ... Examples 6 and 7 illustrate how we can analyze sequences to find how the terms are constructed. EXAMPLE 6 How can we produce the terms of a sequence if the first 10 terms are 1, 2, 2, 3, 3, 3, 4, 4, 4, 4? Solution: Note that the integer 1 appears once, the integer 2 appears twice, the integer 3 appears three times, and the integer 4 appears four times. A reasonable rule for generating this sequence is that the integer n appears exactly n times, so the next five terms of the sequence would all be 5, the following six terms would all be 6, and so on. The sequence generated this way is a possible match. ... EXAMPLE 7 How can we produce the terms of a sequence if the first 10 terms are 5, 11, 17,23,29,35,41, 47, 53, 59? Solution: Note that each of the first 10 terms ofthis sequence after the first is obtained by adding 6 to the previous term. (We could see this by noticing that the difference between consecutive terms is 6.) Consequently, the nth term could be produced by starting with 5 and adding 6 a total of n - 1 times; that is, a reasonable guess is that the nth term is 5 + 6( n -1) = 6n -1. (This is an arithmetic progression with a = 5 and d = 6.) ... Another useful technique for finding a rule for generating the terms of a sequence is to compare the terms of a sequence of interest with the terms of a well-known integer sequence, such as terms of an �thmetic progression, terms of a geometric progression, perfect squares, perfect cubes, and so on. The first 10 terms of some sequences you may want to keep in mind are displayed in Table 1. EXAMPLE 8 Conjecture a simple formula for an if the first 10 terms of the sequence {an} are 1, 7, 25, 79, 241,727,2185,6559,19681,59047. Solution: To attack this problem, we begin by looking at the difference of consecutive terms, but we do not see a pattern. When we form the ratio of consecutive terms to see whether each term is a multiple of the previous term, we find that this ratio, although not a constant, is close to 3. So it is reasonable to suspect that the terms of this sequence are generated by a formula involving 3n• Comparing these terms with the corresponding terms of the sequence {3n}, we notice that the nth term is 2 less than the corresponding power of 3. We see that an = 3n - 2 for 1 :s n :s 10 and conjecture that this formula holds for all n. ... linkS� TABLE 1 nth Term n2 n3 Some Useful Sequences. First 10 Terms 1,4,9,16,25,36,49,64,81,100, ... 1,8,27,64,125,216,343,512,729,1000, ... 2.4 Sequences and Su�ations 153 n4 1,16,81,256,625,1296,2401,4096,6561,10000, ... 2n 2,4,8,16,32,64,128,256,512,1024, ... 3n 3,9,27,81,243,729,2187,6561,19683,59049, ... n! 1,2,6,24,120,720,5040,40320,362880,3628800, ... We will see throughout this text that integer sequences appear in a wide range of contexts in discrete mathematics. Sequences we have or will encounter include the sequence of prime numbers (Chapter 3), the number of ways to order n discrete objects (Chapter 5), the number of moves required to solve the famous Tower of Hanoi puzzle with n disks (Chapter 7), and the number of rabbits on an island after n months (Chapter 7). Integer sequences appear in an amazingly wide range of subject areas besides discrete mathematics, including biology, engineering, chemistry, and physics, as well as in puzzles. An amazing database of over 100,000 different integer sequences can be found in the On­ Line Encyclopedia of Integer Seq uences. This database was originated by Neil Sloane in the 1960s. The last printed version of this database was published in 1995 ([SIPI95]); the current encyclopedia would occupy more than 150 volumes of the size of the 1995 book. New sequences are added regularly to this database. There is also a program accessible via the Web that you can use to find sequences from the encyclopedia that match initial terms you provide. Summati ons Next, we introduce summation notation. We begin by describing the notation used to express the sum of the terms from the sequence {an}. We use the notation or to represent Here, the variable j is called the index of summation, and the choice of the letter j as the variable is arbitrary; that is, we could have used any other letter, such as i or k. Or, in notation, n n n La} = Lai = Lak. }=m i=m k=m Here, the index of summation runs through all integers starting with its lower limit m and ending with its upper limit n. A large uppercase Greek letter sigma, L, is used to denote summation. 154 2 / B�sic Structure s: Sets, Functions, Sequences, and Sums 2-44 EXAMPLE 9 Extra� Examples IIiiiiiI The usual laws for arithmetic apply to summations. For example, when a and bare real numbers, we have L�=I(aXj + bYj) = a L�=l Xj + b L�=l Yj' where Xl, X2,"" Xn and Yl, Y2, ... , Yn are real numbers. (We do not present a formal proof of this identity here. Such a proof can be constructed using mathematical induction, a proof method we introduce in Chap­ ter 4. The proof also uses the commutative and associative laws for addition and the distributive law of multiplication over addition.) We give some examples of summation notation. Express the sum of the first 100 terms of the sequence {an}, where an = 1/ n for n = 1, 2, 3, .... Solution: The lower limit for the index of summation is 1, and the upper limit is 100. We write this sum as 100 1 L�' j=l} EXAMPLE 10 What is the value of L�=l j2? Solution: We have 5 L / = 12 + 22 + 32 + 42 + 52 j=l = 1 + 4 + 9 + 16 + 25 = 55. EXAMPLE 11 What is the value ofL�=4(-I)k? unksiC Solution: We have 8 L ( _1)k = (_1)4 + ( -1)5 + ('-1)6 + ( _1)7 + (_1)8 k=4 = 1 +(-1) + 1 +(-1)+ 1 =1. NEIL SLOANE (BORN 1939) Neil Sloane studied mathematics and electrical engineering at the Uni­ versity of Melbourne on a scholarship from the Australian state telephone company. He mastered many telephone-related jobs, such as erecting telephone poles, in his summer work. After graduating, he de­ signed minimal-cost telephone networks in Australia. In 1962 he came to the United States and studied electrical engineering at Cornell University. His Ph.D. thesis was on what are now called neural networks. He took a job at Bell Labs in 1969, working in many areas, including network design, coding theory, and sphere packing. He now works for AT&T Labs, moving there from Bell Labs when AT&T split up in 1996. One of his favorite problems is the kissing problem (a name he coined), which asks how many spheres can be arranged in n dimensions so that they all touch a central sphere of the same size. (In two dimensions the answer is 6, because 6 pennies can be placed so that they touch a central penny. In three dimensions, 12 billiard balls can be placed so that they touch a central billiard ball. Two billiard balls that just touch are said to "kiss," giving rise to the terminology "kissing problem" and "kissing number." ) Sloane, together with Andrew Odlyzko, showed that in 8 and 24 dimensions, the optimal kissing numbers are, respective ly, 240 and 196,560. The kissing number is known in dimensions I, 2, 3, 8, and 24, but not in any other dimensions. Sloane's books include Sphere Packings, Lattices and Groups, 3d ed., with John Conway; The Theory of Error-Correcting Codes with Jessie MacWilliams; The Encyclopedia of Integer Sequences with Simon Plouffe; and The Rock-Climbing Guide to New Jersey Crags with Paul Nick. The last book demonstrates his interest in rock climbing; it includes more than 50 climbing sites in New Jersey. 2-45 2.4 Sequences and Summations 155 Sometimes it is useful to shift the index of summation in a sum. This is often done when two sums need to be added but their indices of summation do not match. When shifting an index of summation, it is important to make the appropriate changes in the corresponding summand. This is illustrated by Example 12. EXAMPLE 12 Suppose we have the sum Extra� ExampleS � THEOREM 1 5 L/ j=l but want the index of summation to run between ° and 4 rather than from 1 to 5. To do this, we let k = j -1. Then the new summation index runs from ° to 4, and the term j2 becomes (k + Ii. Hence, 5 4 L/= L(k+li. j=l k=O It is easily checked that both sums are 1 + 4 + 9 + 16 + 25 = 55. Sums of terms of geometric progressions commonly arise (such sums are called geometric series). Theorem 1 gives us a formula for the sum of terms of a geometric progression. If a and r are real numbers and r i= 0, then n { arn+l -a Larj = r-l j=O (n + l)a Proof: Let ifr i 1 ifr = 1. To compute S, first multiply both sides of the equality by r and then manipulate the resulting sum as follows: n r S = r L arj substituting summation formula for S j=o n = L arj+1 by the distributive property j=o n+l = L ark shifting the index of summation, with k = j + I k=l = (t ark) + (arn+ 1 -a) removing k = n + 1 term and adding k = 0 term k=O = S + (arn+1 -a) substituting S for summation formula 156 2/ Basic Structures: Sets, Functions, Sequences, and Sums From these equalities, we see that rS = S + (arn+! -a). Solving for S shows that if r =1= 1, then arn+! -a S =---­r - 1 Ifr = 1, then clearly the sum equals (n + l)a. 2-46 <J EXAMPLE 13 Double summations arise in many contexts (as in the analysis of nested loops in computer programs). An example of a double summation is 4 3 LLij· i=! j=! To evaluate the double sum, first expand the inner summation and then continue by computing . the outer summation: 4 3 4 LLij = L(i +2i + 3i) i=! j=! i=! 4 = L6i i=! = 6 + 12 + 18 + 24 = 60. We can also use summation notation to add all values of a function, or terms of an indexed set, where the index of summation runs over all values in a set. That is, we write L/(S) SES to represent the sum of the values /(s), for all members s of S. EXAMPLE 14 What is the value of Ls E {O,2,4} s? Solution: Because Ls E {O,2,4} S represents the sum of the values of s for all the members of the set {O, 2, 4}, it follows that L s = 0 + 2 + 4 = 6. .... S E {O,2,4} Certain sums arise repeatedly throughout discrete mathematics. Having a collection of formulae for such sums can be useful; Table 2 provides a small table offormulae for commonly occurring sums. We derived the first formula in this table in Theorem 1. The next three formulae give us the sum of the first n positive integers, the sum of their squares, and the sum of their cubes. These three formulae can be derived in many different ways (for example, see Exercises 21 and 22 at the end of this section). Also note that each of these formulae, once known, can easily be proved using mathematical induction, the subject of Section 4.1. The last two formulae in the table involve infinite series and will be discussed shortly. Example 15 illustrates how the formulae in Table 2 can be useful. 2-47 2.4 Sequences and Summations 157 EXAMPLE 16 Exlra� Examples � TABLE 2 Some Useful Summation Formulae. Sum Closed Form n arn+1 -a Lark (r =P 0) ,r =P I k=O r -I n n(n + I) Lk 2 k=l n n(n + I)(2n + I) Lk2 k=l 6 n n2(n + 1)2 Lk3 4 k=l 00 I LXk, Ixl < I -- k=O I-x 00 I L, kXk-1, Ixl < I --- k=l (I-x)2 Using the fonnula L�= Ik2 = n(n + 1)(2n + 1)/6 from Table 2, we see that 100 � 2 100 · 101 ·201 49·50·99 � k = - = 338,350 -40,425 = 297,925. k=50 6 6 SOME INFINITE SERIES Although most of the summations in this book are finite sums, infinite series are important in some parts of discrete mathematics. Infinite series are usually studied in a course in calculus and even the definition of these series requires the use of calculus, but sometimes they arise in discrete mathematics, because discrete mathematics deals with infi­ nite collections of discrete elements. In particular, in our future studies in discrete mathematics, we will find the closed fonns for the infinite series in Examples 16 and 17 to be quite useful. (Requires calculus) Let x be a real number with Ix I < 1. Find L::O=o xn. . ' k k+l I SolutIOn: By Theorem 1 with a = 1 and r = x we see that Ln = 0 xn = x x-I . Because Ix I < 1, xk+1 approaches 0 as k approaches infinity. It follows that 00 xk+1 - 1 -1 � xn = lim = = f::o k--+oo x-I x-I 1 -x We can produce new summation fonnulae by differentiating or integrating existing fonnulae. 158 2 / Basic Structures: Sets, Functions, Sequences, and Sums 2-48 EXAMPLE 17 (Requires calculus) Differentiating both sides of the equation DEFINITION 4 DEFINITION 5 00 1 Lxk=-, k=O 1 -x from Example 16 we find that � k k-l 1 � x = (1 _ x)2· k=l (This diff erentiation is valid for Ix I < 1 by a theorem about infinite series.) Cardinali ty In Section 2.1 we defined the cardinality of a finite set to be the number of elements in the set. That is, the cardinality of a finite set tells us when two finite sets are the same size, or when one is bigger than the other. Can we extend this notion to an infinite set? Recall from Exercise 75 in Section 2.3 that there is a one-to-one correspondence between any two finite sets with the same number of elements. This observation lets us extend the concept of cardinality to all sets, both finite and infinite, with Definition 4. The sets A and B have the same cardinality if and only ifthere is a one-to-one correspondence from A to B. We will now split infinite sets into two groups, those with the same cardinality as the set of natural numbers and those with different cardinality. A set that is either finite or has the same cardinality as the set of positive integers is called countable. A set that is not countable is called uncountab le. When an infinite set S is countable, we denote the cardinality of S by �o (where � is aleph, the first letter of the Hebrew alphabet). We write lSI = �o and say that S has cardinality "aleph null." We now give examples of countable and uncountable sets. EXAMPLE 18 Show that the set of odd positive integers is a countable set. Solution: To show that the set of odd positive integers is countable, we will exhibit a one-to-one correspondence between this set and the set of positive integers. Consider the function fen) = 2n - 1 from z+ to the set of odd positive integers. We show that f is a one-to-one correspondence by showing that it is both one-to-one and onto. To see that it is one-to-one, suppose that f(n) = f(m). Then 2n - 1 = 2m -1, so n = m. To see that it is onto, suppose thatt is an odd positive 2-49 2 3 4 5 6 7 8 9 10 11 12 Tenns not circled are not listed because they repeat previously listed tenns 2.4 Sequences and Summations 159 I I I I I I I I I I I I 3 5 7 9 11 13 15 17 19 21 23 FIGURE 1 A One-to-One Correspondence Between Z+ and the Set of Odd Positive Integers. FIGURE 2 Countable. The Positive Rational Numbers Are integer. Then t is 1 less than an even integer 2k, where k is a natural number. Hence t = 2k - 1 = f(k). We display this one-to-one correspondence in Figure 1. ... An infinite set is countable if and only if it is possible to list the elements of the set in a sequence (indexed by the positive integers). The reason for this is that a one-to-one correspon­ dence f from the set of positive integers to a set S can be expressed in terms of a sequence ai, a2, ... , an, ... , where al = f(1), a2 = f(2), ... , an = f(n), .... For instance, the set of odd integers can be listed in a sequence a I , a2, ... , an, ... , where an = 2n -1. We can show that the set of all integers is countable by listing its members. EXAMPLE 19 Show that the set of all integers is countable. Solution: We can list all integers in a sequence by starting with 0 and alternating between positive and negative integers: 0, 1, -1, 2, -2, .... Alternate ly, we could find a one-to-one correspondence between the set of positive integers and the set of all integers. We leave it to the reader to show that the function f(n) = n/2 when n is even and f(n) = -(n -1)/2 when n is odd is such a function. Consequently, the set of all integers is countable. ... It is not surprising that the set of odd integers and the set of all integers are both countable sets. Many people are amazed to learn that the set of rational numbers is countable, as Example 20 demonstrate s. EXAM PLE 20 Show that the set of positive rational numbers is countable. Solution: It may seem surprising that the set of positive rational numbers is countable, but we will show how we can list the positive rational numbers as a sequence rl , r2, ... , r n, .•.. First, note that every positive rational number is the quotient p / q of two positive integers. We can arrange the positive rational numbers by listing those with denominator q = 1 in the first row, those with denominator q = 2 in the second row, and so on, as displayed in Figure 2. The key to listing the rational numbers in a sequence is to first list the positive ratio­ nal numbers p/q with p + q = 2, followed by those with p + q = 3, followed by those with p + q = 4, and so on, following the path shown in Figure 2. Whenever we encounter a num­ ber p/q that is already listed, we do not list it again. For example, when we come to 2/2 = 1 we do not list it because we have already listed 1/1 = 1. The initial terms in the list of posi­ tive rational numbers we have constructed are 1, 1/2,2,3,1/3,1/4,2/3,3/2,4,5, and so on. These numbers are shown circled; the uncircled numbers in the list are those we leave out 160 2/ Basic Structures: Sets, Functions, Sequences, and Sums 2-50 unks� EXAMPLE 21 Extra� Examples ..... Exercises because they are already listed. Because all positive rational numbers are listed once, as the reader can verify, we have shown that the set of positive rational numbers is countable. ... We have seen that the set of rational numbers is a countable set. Do we have a promising candidate for an uncountable set? The first place we might look is the set of real numbers. In Example 21 we use an important proof method, introduced in 1879 by Georg Cantor and known as the Cantor diagonalization argument, to prove that the set of real numbers is not countable. This proof method is used extensively in mathematical logic and in the theory of computation. Show that the set of real numbers is an uncountable set. Solution: To show that the set of real numbers is uncountable, we suppose that the set of real numbers is countable and arrive at a contradiction. Then, the subset of all real numbers that fall between 0 and 1 would also be countable (because any subset of a countable set is also countable; see Exercise 36 at the end of the section). Under this assumption, the real numbers between 0 and 1 can be listed in some order, say, r" r2, r3, .... Let the decimal representation of these real numbers be r, = O.d" d12d13d'4 .. . r2 = O.d21 d22d23d24 .. . r3 = 0.d3,d32d33d34 .. . r4 = 0.d4,d42d43d44 .. . wheredjj E (O, 1,2,3,4,5,6, 7,8,9}.(Forexample,ifr, = 0.23794102 ... , we have dl1 =2, d12 = 3, d13 = 7, and so on.) Then, form a new real number with decimal expansion r = 0.d,d2d3d4 ... , where the decimal digits are determined by the following rule: if djj "# 4 if djj = 4. (As an example, suppose that r, = 0.23794102 ... , r2 = 0.44590138 ... , r3 = 0.09118764 ... , r4 = 0.80553900 ... , and so on. Then we have r = 0.d,d2d3d4 ... = 0.4544 ... , where d, = 4 because d" "# 4, d2 = 5 because d22 = 4, d3 = 4 because d33 "# 4, d4 = 4 because d44 "# 4, and so on.) Every real number has a unique decimal expansion (when the possibility that the expansion has a tail end that consists entirely of the digit 9 is excluded). Then, the real number r is not equal to any of r, , r2, ... because the decimal expansion of r differs from the decimal expansion of rj in the ith place to the right of the decimal point, for each i. Because there is a real number r between 0 and 1 that is not in the list, the assumption that all the real numbers between 0 and 1 could be listed must be false. Therefore, all the real nuinbers between 0 and 1 cannot be listed, so the set of real numbers between 0 and 1 is uncounta ble. Any set with an uncountable subset is uncountable (see Exercise 37 at the end of this section). Hence, the set of real numbers is uncountable. ... 1. Find these terms of the sequence {an}, where an = 2· (_3)n+5n. 2. What is the term as of the sequence {an} if an equals a) 2n-1? b) 7? a) ao d) as c) 1 + (_l)n? d) -(-2n ·2-51 3. What are the terms ao, aI, a2, anda3 ofthe sequence {an}, where an equals a) 2n+l? c) Lnj2J? b) (n + It+l? d) Lnj2J + fnj21? 4. What are the terms ao, aI, a2, anda3 ofthe sequence {an}, where an equals a) (-2t? c) 7 + 4n? b) 3? d) 2n + (-2t? 5. List the first 10 terms of each of these sequences. a) the sequence that begins with 2 and in which each successive term is 3 more than the preceding term b) the sequence that lists each positive integer three times, in increasing order c) the sequence that lists the odd positive integers in in­ creasing order, listing each odd integer twice d) the sequence whose nth term is n! - 2n e) the sequence that begins with 3, where each succeed­ ing term is twice the preceding term t) the sequence whose first two terms are 1 and each succeeding term is the sum ofthe two preceding terms (This is the famous Fibonacci sequence, which we will study later in this text.) g) the sequence whose nth term is the number of bits in the binary expansion of the number n (defined in Section 3.6) h) the sequence where the nth term is the number of let­ ters in the English word for the index n 6. List the first 10 terms of each of these sequences. a) the sequence obtained by starting with 10 and obtain­ ing each term by subtracting 3 from the previous term b) the sequence whose nth term is the sum of the first n positive integers c) the sequence whose nth term is 3n - 2n d) the sequence whose nth term is L.Ji1J e) the sequence whose first two terms are 1 and 2 and each succeeding term is the sum of the two previous terms t) the sequence whose nth term is the largest integer whose binary expansion (defined in Section 3.6) has n bits (Write your answer in decimal notation.) g) the sequence whose terms are constructed sequentially as follows: start with 1, then add 1, then multiply by 1, then add 2, then multiply by 2, and so on h) the sequence whose nth term is the largest integer k such that k! � n 7. Find at least three different sequences beginning with the terms 1,2,4 whose terms are generated by a simple for­ mula or rule. S. Find at least three different sequences beginning with the terms 3, 5, 7 whose terms are generated by a simple for­ mula or rule. 9. For each of these lists ofintegers, provide a simple formula or rule that generates the terms of an integer sequence that begins with the given list. Assuming that your formula 2.4 Sequences and Summations 161 or rule is correct, determine the next three terms of the sequence. a) 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, .. . b) 1,2,2,3,4,4,5,6,6,7,8,8, .. . c) 1, 0,2, 0, 4, 0,8,0,16, 0, .. . d) 3,6,12,24,48,96,192, .. . e) 15,8,1, -6, -l3, -20, -27, .. . t) 3,5,8, 12, 17,23,30,38,47, .. . g) 2,16,54,128,250,432,686, .. . h) 2,3, 7,25,121,721,5041,40321, ... 10. For each of these lists of integers, provide a simple formula or rule that generates the terms of an integer sequence that begins with the given list. Assuming that your formula or rule is correct, determine the next three terms of the sequence. a) 3,6,11,18,27,38,51,66,83,102, .. . b) 7,11,15,19,23,27,31,35,39,43, .. . c) 1,10,11,100,101,110, Ill, 1000, 1001, 1010,1011, ... d) 1,2,2,2,3,3, 3, 3, 3, 5, 5, 5, 5, 5,5,5, ... e) 0, 2,8,26,80,242,728,2186,6560,19682, ... t) 1,3,15,105,945,10395, l35135, 2027025, 34459425, ... g) 1,0,0,1,1, 1, 0,0, 0,0,1, 1,1,1,1, .. . b) 2,4,16,256,65536,4294967296 , .. . ** 11. Show that if an denotes the nth positive integer that is not a perfect square, then an = n + {.Ji1}' where {x} denotes the integer closest to the real number x. *12. Let an be the nth term of the sequence 1,2, 2,3,3,3,4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, ... , constructed by including the integer k exactly k times. Show that an = Lffn+1J· 13. What are the values of these sums? 5 4 a) L(k+l) b) L(-2)j k=1 j=O 10 8 c) L 3 d) L (2j+1 - 2j) ;=1 j=O 14. What are the values of these sums, where S = {I, 3, 5,7}? a) L} jES c) L (Ij}) d) L 1 jES jES 15. What is the value of each of these sums of terms of a geometric progression? 8 8 a) L 3 ·2j b) L 2j j=O j=1 8 8 c) L(-3)j d) L 2·(-3)j j=2 j=O 16. Find the value of each of these sums. 8 a) L(I +(-IY) j=O 8 b) L (3j - 2j) j=O 162 2/ Basic Structures: Sets, Functions, Sequences, and Sums 8 c) L (2 . 3j + 3 .2j) j=o 8 d) L (2j+1 - 2j) j=O 17. Compute each ofthese double sums. 2 3 a) L L (i + j) i=lj=1 3 2 c) L L i i=lj=O 2 3 b) L L (2i + 3j) i=Oj=O 2 3 d) L L ij i=Oj=1 18. Compute each of these double sums. 3 2 3 2 a) L L (i -j) b) L L (3i + 2j) i=lj=1 3 2 i=Oj=O 2 3 c) L L j d) L L i2l i=1 j=O i=Oj=O 19. 5how that L�=I(aj-aj-I)=an-aO, where ao, aI, ... , an is a sequence of real numbers. This type of sum is called telescopin g. 20. Use the identity l/(k(k + 1» = 1/ k -l/(k + 1) and Ex­ ercise 19 to compute L�= I l/(k(k + 1». 21. Sum both sides of the identity k2 -(k -1)2 = 2k - 1 from k = 1 to k = n and use Exercise 19 to find a) a formula for L� = I (2k -1) (the sum of the first n odd natural numbers). b) a formula for L�= I k. *22. Use the technique given in Exercise 19, together with the result of Exercise 21 b, to derive the formula for L� = I k2 given in Table 2. (Hint: Take ak = k3 in the telescoping sum in Exercise 19.) 23. Find L�� lOok. (Use Table 2.) 24. Find L��99k3. (Use Table 2.) *25. Find a formula for L;=o L .J7(j, when m is a positive integer. * 26. Find a formula for L; = 0 L Jk j, when m is a positive integer. There is also a special notation for products. The product of am, am+ I, ... , an is represented by j=m 27. What are the values of the following products? a) n:�oi b) n�=5i ) nlOo (_I)i d) nlo 2 c 1=1 1=1 Recall that the value of the factorial function at a posi­ tive integer n, denoted by n!, is the product of the posi­ tive integers from 1 to n, inclusive. Also, we specify that O! = 1. 28. Express n! using product notation. 29. Find L�=oj!. 30. Find n�=oj!. 2-52 31. Determine whether each ofthese sets is countable or un­ countable. For those that are countable, exhibit a one-to­ one correspondence between the set of natural numbers and that set. a) the negative integers b) the even integers c) the real numbers between 0 and! d) integers that are multiples of 7 32. Determine whether each of these sets is countable or un­ countable. For those that are countable, exhibit a one-to­ one correspondence between the set of natural numbers and that set. a) the integers greater than 10 b) the odd negative integers c) the real numbers between 0 and 2 d) integers that are multiples of 10 33. Determine whether each of these sets is countable or un­ countable. For those that are countable, exhibit a one-to­ one correspondence between the set of natural numbers and that set. a) all bit strings not containing the bit 0 b) all positive rational numbers that cannot be written with denominators less than 4 c) the real numbers not containing 0 in their decimal representation d) the real numbers containing only a finite number of 1 s in their decimal representation 34. Determine whether each of these sets is countable or un­ countable. For those that are countable, exhibit a one-to­ one correspondence between the set of natural numbers and that set. a) integers not divisible by 3 b) integers divisible by 5 but not by 7 c) the real numbers with decimal representations con­ sisting of all 1 s d) the real numbers with decimal representations of all Is or 9s 35. If A is an uncountable set and B is a countable set, must A -B be uncountable? 36. Show that a subset of a countable set is also countable. 37. Show that if A and B are sets, A is uncountable, and A S; B, then B is uncountabl e. ' , 38. Show that if A and B are sets with the same cardinality, then the power set of A and the power set of B have the same cardinality. 39. Show that if A and B are sets with the same cardinal­ ity and C and D are sets with the same cardinality, then A x C and B x D have the same cardinality. 40. Show that the union oftwo countable sets is countable. **41. Show that the union of a countable number of countable sets is countable. 2-53 42. Show that the set Z+ x Z+ is countable. *43. Show that the set of all finite bit strings is countable. *44. Show that the set of real numbers that are solutions of quadratic equations ax2 + bx + c = 0, where a, b, and c are integers, is countable. *45. Show that the set of all computer programs in a partic­ ular programming language is countable. [Hint: A com­ puter program written in a programming language can be thought of as a string of symbols from a finite alphabet.] *46. Show that the set of functions from the positive inte­ gers to the set {O, 1,2,3,4,5,6,7,8, 9} is uncountable. [Hint: First set up a one-to-one correspondence between Key Terms and Results TERMS set: a collection of distinct objects axiom: a basic assumption of a theory paradox: a logical inconsistency element, member of a set: an object in a set o (empty set, null set): the set with no members universal set: the set containing all objects under consideration Venn diagram: a graphical representation of a set or sets S = T (set equality ): S and T have the same elements S � T (S is a subset of T).: every element of S is also an element ofT SeT (S is a proper subset of T): S is a subset of T and S=I=T finite set: a set with n elements, where n is a nonnegative integer infinite set: a set that is not finite lSI (the cardinality of S): the number of elements in S P(S) (the power set of S): the set of all subsets of S A U B (the union of A and B): the set containing those ele­ ments that are in at least one of A and B An B (the intersection of A and B): the set containing those elements that are in both A and B. A - B (the difference of A and B): the set containing those elements that are in A but not in B A (the complement of A): the set of elements in the universal set that are not in A A EEl B (the symmetric difference of A and B): the set con­ taining those elements in exactly one of A and B membership table: a table displaying the membership of ele­ ments in sets function from A to B : an assignment of exactly one element of B to each element of A domain of I: the set A, where I is a function from A to B codomain of I: the set B, where I is a function from A to B b is the image of a under I: b = I(a) a is a pre-image of b under I: I(a) = b Key Terms and Results 163 the set of real numbers between 0 and I and a subset of these functions. Do this by associating to the real number 0.d1d2 ••• dn •.• the function I with I(n) = dn.] *47. We say that a function is computable if there is a com­ puter program that finds the values of this function. Use Exercises 45 and 46 to show that there are functions that are not computable. *48. Show that if S is a set, then there does not exist an onto function I from S to peS), the power set of S. [Hint: Suppose such a function I existed. Let T = (s E Sis f/. I(s)} and show that no element s can exist for which I(s) = T.] range of I: the set of images of I onto function, surjection: a function from A to B such that every element of B is the image of some element in A one-to-one function, injection: a function such that the im­ ages of elements in its domain are all different one-to-one correspondence, bijection: a function that is both one-to-one and onto inverse of I: the function that reverses the correspondence given by I (when I is a bijection) log (composition of 1 and g): the function that assigns I(g(x» to x Lx J (floor function): the largest integer not exceeding x fx 1 (ceiling function): the smallest integer greater than or equal to x sequence: a function with domain that is a subset ofthe set of integers geometric progression: a sequence ofthe form a, ar, ar2, ... , where a and r are real numbers arithmetic progression: a sequence of the form a, a + d, a + 2d, ... , where a and d are real numbers string: a finite sequence empty string: a string of length zero L7=1 aj: the sum al + a2 + ... + an D7 = 1 aj: the product ala2· .. an countable set: a set that either is finite or can be placed in one-to-one correspondence with the set of positive integers uncountable set: a set that is not countable Cantor diagonalization argument: a proof technique that can be used to show that the set of real numbers is uncountable RESULTS The set identities given in Table 1 in Section 2.2 The summation formulae in Table 2 in Section 2.4 The set of rational numbers is countable. The set of real numbers is uncountable. 164 2/ Basic Structures: Sets, Functions, Sequences, and Sums Review Questions 1. Explain what it means for one set to be a subset of another set. How do you prove that one set is a subset of another set? 2. What is the empty set? Show that the empty set is a subset of every set. 3. a) Define lSI, the cardinality of the set S. b) Give a formula for IA UBI, where A and B are sets. 4. a) Define the power set of a set S. b) When is the empty set in the power set of a set S? c) How many elements does the power set of a set S with n elements have? 5. a) Define the union, intersection, difference, and sym­ metric difference of two sets. b) What are the union, intersection, difference, and sym­ metric difference of the set of positive integers and the set of odd integers? 6. a) Explain what it means for two sets to be equal. b) Describe as many of the ways as you can to show that two sets are equal. c) Show in at least two different ways that the sets A -(B n C) and (A -B) U (A -C) are equal. 7. Explain the relationship between logical equivalences and set identities. 8. a) Define the domain, codomain, and the range of a function. b) Let f(n) be the function from the set of integers to the set of integers such that f(n) = n2 + 1. What are the domain, codomain, and range of this function? 9. a) Define what it means for a function from the set of positive integers to the set of positive integers to be one-to-one. Supplem entary Exer cises 1. Let A be the set of English words that contain the letter x, and let B be the set of English words that contain the letter q. Express each of these sets as a combination of A and B. a) The set of English words that do not contain the letter x. b) The set of English words that contain both an x and a q. c) The set of English words that contain an x but not a q. d) The set of English words that do not contain either an x or a q. e) The set of English words that contain an x or a q, but not both. 2. Show that if A is a subset of B, then the power set of A is a subset of the power set of B. 3. Suppose that A and B are sets such that the power set of A is a subset of the power set of B. Does it follow that A is a subset of B? 2-54 b) Define what it means for a function from the set of positive integers to the set of positive integers to be onto. c) Give an example of a function from the set of posi­ tive integers to the set of positive integers that is both one-to-one and onto. d) Give an example of a function from the set of positive integers to the set of positive integers that is one-to-one but not onto. e) Give an example of a function from the set of posi­ tive integers to the set of positive integers that is not one-to-one but is onto. f) Give an example of a function from the set of positive integers to the set of positive integers that is neither one-to-one nor onto. 10. a) Define the inverse of a function. b) When does a function have an inverse? c) Does the function f(n) = 10 -n from the set ofinte­ gers to the set of integers have an inverse? If so, what is it? 11. a) Define the floor and ceiling functions from the set of real numbers to the set of integers. b) For which real numbers x is it true that LxJ = rxl? 12. Conjecture a formula for the terms of the sequence that begins 8, 14,32,86,248 and find the next three terms of your sequence. 13. What is the sum ofthe terms ofthe geometric progression a + ar + ... + arn when r =1= I? 14. Show that the set of odd integers is countable. 15. Give an example of an uncountable set. 4. Let E denote the set of even integers and 0 denote the set of odd integers. As usual, let Z denote the set of all integers. Determine each of these sets. a)EUO b)EnO c)Z-E d)Z-O 5. Show that if A and B are sets, then A -(A -B) = An B. 6. Let A and B be sets. Show that A � B if and only if AnB = A. 7. Let A, B, and C be sets. Show that (A -B) - C is not necessarily equal to A -(B -C). 8. Suppose that A, B, and C are sets. Prove or disprove that (A -B) - C = (A -C) -B. 9. Suppose that A, B, C, and D are sets. Prove or disprove that (A -B) -(C -D) = (A -C) -(B -D). 10. Show that if A and B are finite sets, then IA n BI :::: IA UBI. Determine when this relationship is an equality. 2-55 11. Let A and B be sets in a finite universal set U. List the following in order of increasing size. a) IAI, IA UBI, IA n BI, lUI, 101 b) IA - BI, IA EEl BI, IAI + IBI, IA UBI, 101 12. Let A and B be subsets ofthe finite universal set U. Show that IAn 81 = IUI-IAI-IBI + IA n BI· 13. Let 1 and g be functions from {I, 2, 3, 4} to {a, b, c, d} and from {a, b, c, d} to {I, 2, 3, 4}, respectively, such that 1(1) = d, 1(2) = c, 1(3) = a, and 1(4) = b, and g(a) = 2, g(b) = I, g(c) = 3, and g(d) = 2. a) Is 1 one-to-one? Is g one-to-one? b) Is 1 onto? Is g onto? c) Does either 1 or g have an inverse? If so, find this inverse. 14. Let 1 be a one-to-one function from the set A to the set B. Let Sand T be subsets of A. Show that I(S n T) = I(S) n I(T). 15. Give an example to show that the equality in Exercise 14 may not hold if 1 is not one-to-one. Suppose that 1 is a function from A to B. We define the func­ tion S I from P(A) to P(B) by the rule S I(X) = I(X) for each subset X of A. Similarly, we define the function S I-I from P(B) to P(A) by the rule SI-I(y) = I-I(y) for each subset Y of B. Here, we are using Definition 4, and the defi­ nition of the inverse image of a set found in the preamble to Exercise 38, both in Section 2.3. *16. a) Prove that if 1 is a one-to-one function from A to B, then S I is a one-to-one function from P(A) to P(B). b) Prove that if 1 is an onto function from A to B, then S I is an onto function from P(A) to P(B). c) Prove that if 1 is an onto function from A to B, then S I-I is a one-to-one function from P(B) to P(A). d) Prove that if 1 is a one-to-one function from A to B, then S I-I is an onto function from P(B) to P(A). e) Use parts (a) through (d) to conclude that if 1 is a one-to-one correspondence from A to B, then S I is a one-to-one correspondence from P(A) to P(B) and S I-I is a one-to-one correspondence from P(B) to P(A). 17. Prove that if 1 and g are functions from A to B and S I = Sg (using the definition in the preamble to Exercise 16), then I(x) = g(x) for all x E A. 18. Show that if n is an integer, then n = r n/21 + Ln/2J. 19. For which real numbers x and y is it true that Lx + yJ = LxJ + LYJ? Computer Projects Write programs with the specified input and output. 1. Given subsets A and B of a set with n elements, use bit strings to find A, AU B, An B, A -B, and A EEl B. Computer Projects 165 20. For which real numbers x and y is it true that rx + y1 = rx1 + ryl? 21. For which real numbers x and y IS it true that rx + y1 = rx1 + LYJ? 22. Prove that Ln/2J rn/21 = Ln2/4J for all integers n. 23. Prove that if m is an integer, then LxJ + Lm -xJ = m -1, unless x is an integer, in which case, it equals m. 24. Prove that if x is a real number, then LLx /2J/2J = Lx /4J. 25. Prove that if n is an odd integer, then rn2/41 = (n2 + 3)/4. 26. Prove that if m and n are positive integers and x is a real number, then lLxJm+nJ=lx�nJ *27. Prove that if m is a positive integer and x is a real number, then LmxJ = LxJ + lx + �J + lx + �J + ... + lx + m � 1 J *28. We define the U1am numbers by setting UI = 1 and U2 = 2. Furthermore, after determining whether the in­ tegers less than n are Ulam numbers, we set n equal to the next Ulam number if it can be written uniquely as the sum of two different Ulam numbers. Note that U3 = 3, U4 = 4, U5 = 6, and U6 = 8. a) Find the first 20 Ulam numbers. b) Prove that there are infinitely many Ulam numbers. 29. Determine the value ofn!�OI kt I . (The notation used here for products is defined in the preamble to Exercise 27 in Section 2.4.) *30. Determine a rule for generating the terms ofthe sequence that begins 1, 3, 4, 8,15,27,50,92, ... ,andfindthenext four terms of the sequence. *31. Determine a rule for generating the terms of the se­ quence that begins 2,3,3,5,10,13,39,43,172,177, 885,891, ... , and find the next four terms of the sequence. *32. Prove that if A and B are countable sets, then A x B is also a countable set. 2. Given multisets A and B from the same universal set, find AU B, A n B, A -B, and A + B (see preamble to Exer­ cise 59 of Section 2.2). 166 2/ Basic Structures: Sets, Functions, Sequences, and Sums 3. Given fuzzy sets A and B, find if, A U B, and A n B (see preamble to Exercise 61 of Section 2.2). 4. Given a function I from {I, 2, ... , n} to the set of integers, determine whether I is one-to-one. Computations and Explorations 2-56 S. Given a function I from {I, 2, ... , n} to itself, determine whether I is onto. 6. Given a bijection I from the set {I, 2, ... , n} to itself, find 1-1. Use a comput ational program or programs you have written to do these exercises. 1. Given two finite sets, list all elements in the Cartesian prod­ uct of these two sets. 2. Given a finite set, list all elements of its power set. 3. Calculate the number of one-to-one functions from a set S to a set T, where Sand T are finite sets of various sizes. Can you determine a formula for the number of such functions? (We will find such a formula in Chapter 5.) 4. Calculate the number of onto functions from a set S to a set T, where S and T are finite sets of various sizes. Can Writing Projects Respond to these with essays using outside sources. 1. Discuss how an axiomatic set theory can be developed to avoid Russell's paradox. (See Exercise 38 of Sec­ tion 2.1.) 2. Research where the concept of a function first arose, and describe how this concept was first used. 3. Explain the different ways in which the Encyclopedia 01 Integer Sequences has been found useful. Also, describe a few of the more unusual sequences in this encyclopedia and how they arise. you determine a formula for the number of such functions? (We will find such a formula in Chapter 7.) * S. Develop a collection of different rules for generating the terms of a sequence and a program for randomly select­ ing one of these rules and the particular sequence gen­ erated using these rules. Make this part of an interactive program that prompts for the next term of the sequence and determine whether the response is the intended next term. 4. Define the recently invented EKG sequence and describe some of its properties and open questions about it. S. Look up the definition of a transcendental number. Explain how to show that such numbers exist and how such num­ bers can be constructed. Which famous numbers can be shown to be transcendental and for which famous numbers is it still unknown whether they are transcendental? 6. Discuss infinite cardinal numbers and the continuum hypothesis. 3.1 Algorithms 3.2 The Growth of Functions 3.3 Complex ity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors 3.6 Integers and Algorithms 3.7 Applications of Number Theory 3.8 Matrices The Fundamentals: Algorithms, the Integers, and Matrices M any problems can be solved by considering them as special cases of general problems. For instance, consider the problem of locating the largest integer in the sequence 10 I, 12, 144, 212, 98. This is a specific case of the problem of locating the largest integer in a sequence of integers . To solve this general problem we must give an algorithm , which specifies a sequence of steps used to solve this general problem. We will study algorithms for solving many different types of problems in this book. For instance, algorithms will be developed for finding the greatest common divisor of two integers, for generating all the orderings of a finite set, for searching a list, for sorting the elements of a sequenc e, and for finding the shortest path between nodes in a network. One important consideration concerning an algorithm is its computa tional complexit y. That is, what are the computer resources needed to use this algorithm to solve a problem of a specified size? To measure the complexity of algorithms we use big-O and big-Theta notation, which we develop in this chapter. We will illustrate the analysis of the complexity of algorithms in this chapter. Furthermore, we will discuss what the complexity of an algorithm means in practical and theoretical terms. The set of integers plays a fundamental role in discrete mathema tics. In particul ar, the concept of division of integers is fundamental to computer arithmetic. We will briefly review some of the important concepts of number theory, the study of integers and their properti es. Some important algorithms involving integers will be studied, including the Euclidean algorithm for computing greatest common divisors, which was first described thousands of years ago. Integers can be represented using any positive integer greater than I as a base. Binary expansions, which are used throughout computer science, are representations with 2 as the base. In this chapter we discuss base b representations of integers and give an algorithm for finding them. Algorithms for integer arithmetic, which were the first procedures called algorithms, will also be discussed. This chapter also introduces several important applications of number theory. For example, in this chapter we will use number theory to make messages secret, to generate pseudorandom numbers, and to assign memory locations to computer files. Number theory, once considered the purest of subjects, has become an essential tool in providing computer and Internet security. Matrices are used in discrete mathematics to represent a variety of discrete structure s. We review the basic material about matrices and matrix arithmetic needed to represent relations and graphs. Matrix arithmetic will be used in numerous algorithms involving these structure s. 3.1 Algorithms 3-/ Introduction There are many general classes of problems that arise in discrete mathema tics. For instance: given a sequence of integers, find the largest one; given a set, list all its subsets; given a set of integers, put them in increasing order; given a network, find the shortest path between two vertices. When presented with such a problem, the first thing to do is to construct a model that translates the problem into a mathematical context. Discrete structures used in such models include sets, sequences, and function s-structures discussed in Chapter 2-as well as such other structures as permuta tions, relations, graphs, trees, networks, and finite state machines-concepts that will be discussed in later chapters. 167 168 3 / The Fundament als: Algorithms, the Integers, and Matrices 3-2 DEFINITION 1 EXAMPLE l Exam=� unkS� Setting up the appropriate mathematical model is only part of the solution. To complete the solution, a method is needed that will solve the general problem using the model. Ideally, what is required is a procedure that follows a sequence of steps that leads to the desired answer. Such a sequence of steps is called an algorithm. An algorithm is a finite set of precise instructions for performing a computation or for solving a problem. The term algorithm is a corruption of the name al-Khowarizm i, a mathematician of the ninth century, whose book on Hindu numerals is the basis of modern decimal notation. Originally, the word algorism was used for the rules for performing arithmetic using decimal notation. Algorism evolved into the word algorithm by the eighteenth century. With the growing interest in computing machines, the concept of an algorithm was given a more general meaning, to include all definite procedures for solving problems, not just the procedures for performing arithmetic. (We will discuss algorithms for performing arithmetic with integers in Section 3.6.) In this book, we will discuss algorithms that solve a wide variety of problems. In this section we will use the problem of finding the largest integer in a finite sequence of integers to illustrate the concept of an algorithm and the properties algorithms have. Also, we will describe algorithms for locating a particular element in a finite set. In subsequent sections, procedures for finding the greatest common divisor of two integers, for finding the shortest path between two points in a network, for multiplying matrices, and so on, will be discussed. Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers. Even though the problem of finding the maximum element in a sequence is relatively trivial, it provides a good illustration of the concept of an algorithm. Also, there are many instances where the largest integer in a finite sequence of integers is required. For instance, a university may need to find the highest score on a competitive exam taken by thousands of students. Or a sports organization may want to identify the member with the highest rating each month. We want to develop an algorithm that can be used whenever the problem of finding the largest element in a finite sequence of integers arises. We can specify a procedure for solving this problem in several ways. One method is simply to use the English language to describe the sequence of steps used. We now provide such a solution. Solution of Example 1: We perform the following steps. 1. Set the temporary maximum equal to the first integer in the sequence. (The temporary maximum will be the largest integer examined at any stage of the procedure.) 2. Compare the next integer in the sequence to the temporary maximum, and if it is larger than the temporary maximum, set the temporary maximum equal to this integer. ABU JA'FAR MOHAMM ED IBN MUSA AL-KHO WARIZMI (C. 780-C. 850) al-Khowarizmi, an as­ tronomer and mathematician, was a member of the House of Wisdom, an academy of scientists in Baghdad. The name al-Khowarizmi means "from the town of Kowarzizm," which was then part of Persia, but is now called Khiva and is part of Uzbekistan. al-Khowarizmi wrote books on mathema tics, astronomy, and geography. Western Europeans first learned about algebra from his works. The word algebra comes from al-jabr, part of the title of his book Kitab al-jabr w'al muquabala. This book was translated into Latin and was a widely used textbook. His book on the use of Hindu numerals describes procedures for arithmetic operations using these numerals. European authors used a Latin corruption of his name, which later evolved to the word algorithm, to describe the subject of arithmetic with Hindu numerals. 3.1 Algorithms 169 3. Repeat the previous step ifthere are more integers in the sequence. 4. Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence. <II1II An algorithm can also be described using a computer language. However, when that is done, only those instructions permitted in the language can be used. This often leads to a description of the algorithm that is complicated and difficult to understand. Furthermore, because many programming languages are in common use, it would be undesirable to choose one particular language. So, instead of using a particular computer language to specify algorithms, a form of pseudocode will be used in this book. (All algorithms will also be described using the English language.) Pseudocode provides an intermediate step between an English language descrip­ tion of an algorithm and an implementation of this algorithm in a programming language. The steps of the algorithm are specified using instructions resembling those used in programming languages. However, in pseudocode, the instructions used can include any well-defined opera­ tions or statements. A computer program can be produced in any computer language using the pseudocode description as a starting point. The pseudocode used in this book is loosely based on the programming language Pascal. However, the syntax of Pascal, or that of other programming languages, will not be followed. Furthermore, any well-defined instruction can be used in this pseudocode. The details of the pseudocode used in the text are given in Appendix 3. The reader should refer to this appendix whenever the need arises. A pseudocode description of the algorithm for finding the maximum element in a finite sequence fo llows. ALGOR ITHM 1 Finding the Maximum Element in a Finite Sequence. procedure max(a], a2, ... , an: integers) max:= a] for i := 2 to n if max < aj then max := aj {max is the largest element} This algorithm first assigns the initial term of the sequence, ai, to the variable max. The "for" loop is used to successively examine terms of the sequence. If a term is greater than the current value of max, it is assigned to be the new value of max. There are several properties that algorithms generally share. They are useful to keep in mind when algorithms are described. These properties are: • Input. An algorithm has input values from a specified set. • Output. From each set of input values an algorithm produces output values from a specified set. The output values are the solution to the problem. • Definiteness. The steps of an algorithm must be defined precisely. • Correctness. An algorithm should produce the correct output values for each set of input values. • Finiteness. An algorithm should produce the desired output after a finite (but perhaps large) number of steps for any input in the set. . • Effectiveness. It must be possible to perform each step of an algorithm exactly and in a finite amount of time. • Generality. The procedure should be applicable for all problems of the desired form, not just for a particular set of input values. 170 3 / The Fundament als: Algorithms, the Integers, and Matrices 3-4 EXAMPLE 2 Show that Algorithm 1 for finding the maximum element in a finite sequence of integers has all the properties listed. unkS� Solution: The input to Algorithm 1 is a sequence of integers. The output is the largest integer in the sequence. Each step of the algorithm is precisely defined, because only assignments, a finite loop, and conditional statements occur. To show that the algorithm is correct, we must show that when the algorithm terminate s, the value of the variable max equals the maximum of the terms of the sequence. To see this, note that the initial value of max is the first term of the sequence; as successi ve terms of the sequence are examined, max is updated to the value of a term if the term exceeds the maximum of the terms previously examined. This (informal) argument shows that when all the terms have been examined, max equals the value of the largest term. (A rigorous proof of this requires techniques developed in Section 4.l.) The algorithm uses a finite number of steps, because it terminates after all the integers in the sequence have been examined. The algorithm can be carried out in a finite amount of time because each step is either a comparison or an assignment. Final ly, Algorithm 1 is general, because it can be used to find the maximum of any finite sequence of integers. .... Searching Algorithms The problem of locating an element in an ordered list occurs in many contexts. For instance, a program that checks the spelling of words searches for them in a dictionary, which is just an ordered list of words. Problems of this kind are called searching problems. We will discuss several algorithms for searching in this section. We will study the number of steps used by each of these algorithms in Section 3.3. The general searching problem can be described as follows: Locate an element x in a list of distinct elements aj, a2, ... , an, or determine that it is not in the list. The solution to this search problem is the location of the term in the list that equals x (that is, i is the solution if x = aj) and is 0 if x is not in the list. THE LINEAR SEARCH The first algorithm that we will present is called the linear search, or sequential search, algorithm. The linear search algorithm begins by comparing x and aj. When x = aj, the solution is the location of aj, namely, 1. When x #-ai, compare x with a2. If x = a2, the solution is the location of a2, namely, 2. When x #-a2, compare x with a3. Continue this process, comparing x successively with each term of the list until a match is found, where the solution is the location of that term, unless no match occurs. If the entire list has been searched without locating x, the solution is O. The pseudocode for the linear search algorithm is displayed as Algorithm 2. ALGORITHM 2 The Linear Search Algorithm. procedure linear search(x: integer, a1, a2, ... , an: distinct integers) i := 1 while (i :5 n and x =1= ai) i := i + 1 if i :5 n then location := i else location := 0 {location is the subscript of the term that equals x, or is 0 if x is not found} 3-5 links� EXAM PLE 3 3.1 Algorithms 171 THE BINARY SEARCH We will now consider another searching algorithm. This algorithm can be used when the list has terms occurring in order of increasing size (for instance: if the terms are numbers, they are listed from smallest to largest; if they are words, they are listed in lexicographic, or alphabetic, order). This second searching algorithm is called the binary search algorithm. It proceeds by comparing the element to be located to the middle term of the list. The list is then split into two smaller sublists of the same size, or where one of these smaller lists has one fewer term than the other. The search continues by restricting the search to the appropriate sublist based on the comparison of the element to be located and the middle term. In Section 3.3, it will be shown that the binary search algorithm is much more efficient than the linear search algorithm. Example 3 demonstrates how a binary search works. To search for 19 in the list 1235678 10 12 13 15 16 18 192022, first split this list, which has 16 terms, into two smaller lists with eight terms each, namely, 1235678 10 12 13 15 1618 192022. Then, compare 19 and the largest term in the first list. Because 10 < 19, the search for 19 can be restricted to the list containing the 9th through the 16th terms of the original list. Next, split this list, which has eight terms, into the two smaller lists of four terms each, namely, 12131516 18192022. Because 16 < 19 (comparing 19 with the largest term of the first list) the search is restricted to the second of these lists, which contains the 13th through the 16th terms of the original list. The list 18 19 20 22 is split into two lists, namely, 18 19 2022. Because 19 is not greater than the largest term of the first of these two lists, which is also 19, the search is restricted to the first list: 1819, which contains the 13th and 14th terms of the original list. Next, this list of two terms is split into two lists of one term each: 18 and 19. Because 18 < 19, the search is restricted to the second list: the list containing the 14th term of the list, which is 19. Now that the search has been narrowed down to one term, a comparison is made, and 19 is located as the 14th term in the original list. .... We now specify the steps of the binary search algorithm. To search for the integer x in the list aI, a2, ... , an, where al < a2 < ... < an, begin by comparing x with the middle term of the sequence, am, where m = L(n + 1)/2J. (Recall that LxJ is the greatest integer not exceeding x.) If x > am, the search for x can be restricted to the second half of the sequence, which is am+l, am+2, ... , an. Ifx is not greater than am, the search for x can be restricted to the first half ofthe sequence, which is aI, a2, ... , am. The search has now been restricted to a list with no more than r n 121 elements. (Recall that r xl is the smallest integer greater than or equal to x .) Using the same procedure, compare x to the middle term of the restricted list. Then restrict the search to the first or second half of the list. Repeat this process until a list with one term is obtained. Then determine whether this term is x. Pseudocode for the binary search algorithm is displayed as Algorithm 3. 172 3 / The Fundamenta ls: Algorithms, the Integers, and Matrices 3-6 Demo� ALGORITHM 3 The Binary Search Algorithm. procedure binary search (x: integer, aI, a2, ... ,an: increasing integers) i : = I {i is left endpoint of search interval} j : = n {j is right endpoint of search interval} while i < j begin end m := L(i + j)/2J if x > am then i := m + 1 else j := m if x = ai then location := i else location := 0 {location is the subscript of the term equal to x, or 0 if x is not found} Algorithm 3 proceeds by successively narrowing down the part of the sequence being searched. At any given stage only the terms beginning with ai and ending with a j are under consideration. In other words, i and j are the smallest and largest subscripts of the remaining terms, respective ly. Algorithm 3 continues narrowing the part of the sequence being searched until only one term of the sequence remains. When this is done, a comparison is made to see whether this term equals x. Sorting Ordering the elements of a list is a problem that occurs in many contexts. For example, to produce a telephone directory it is necessary to alphabetize the names of subscribers. Putting addresses in order in an e-mail mailing list can determine whether there are duplicated addresses. Creating a useful dictionary requires that words be put in alphabetical order. Similarly, generating a parts list requires that we order them according to increasing part number. Suppose that we have a list of elements of a set. Furthermore, suppose that we have a way to order elements of the set. (The notion of ordering elements of sets will be discussed in detail in Section 8.6.) Sorting is putting these elements into a list in which the elements are in increasing order. For instance, sorting the list 7,2, 1,4,5,9 produces the list 1,2,4,5, 7, 9. Sorting the list d, h, c, a,f (using alphabetical order) produces the list a, c, d,J, h. An amazingly large percentage of computing resources is devoted to sorting one thing or another. Hence, much effort has been devoted to the development of sorting algorithms. A surprisingly large number of sorting algorithms have been devised using distinct strategies. In his fundamental work, The Art o/Computer Programming, Donald Knuth devotes close to 400 pages to sorting, covering around 15 different sorting algorithms in depth! There are many reasons why sorting algorithms interest computer scientists and mathematicians. Among these reasons are that some algorithms are easier to implement, some algorithms are more efficient (either in general, or when given input with certain characteri stics, such as lists slightly out of order), some algorithms take advantage of particular computer architecture s, and some algorithms are particularly clever. In this section we will introduce two sorting algorithms, the bubble sort and the insertion sort. Two other sorting algorithms, the selection sort and the binary insertion sort, are introduced in the exercises at the end ofthis section, and the shaker sort is introduced in the Supplemen tary Exercises at the end of this chapter. In Section 4.4 we will discuss the merge sort and introduce the quick sort in the exercises in that section; the tournament sort is introduced in the exercise set in Section 10.2. We cover sorting algorithms both because sorting is an important problem and because these algorithms can serve as examples of many important concepts. 3-7 unkS� EXAM PLE 4 First pass C; 2 2 2 Second pass (3 3 3 4 4 C� 1 1 (4 5 5 5 5 Third pass C � 1 Fourth pass (2 3 3 [iJ [iJ FIGURE 1 The Steps of a Bubble Sort. 3.1 Algorithms 173 (� 2 2 C� 1 (! 4 4 [1] [1] [1] ( 1 2 C : an interchange rn ( : pair in correct order numbers in color guaranteed to be in correct order THE BUBBLE SORT The bubble sort is one ofthe simplest sorting algorithms, but not one of the most efficient. It puts a list into increasing order by successively comparing adjacent elements, interchanging them if they are in the wrong order. To carry out the bubble sort, we perform the basic operation, that is, interchanging a larger element with a smaller one following it, starting at the beginning of the list, for a full pass. We iterate this procedure until the sort is complete. Pseudocode for the bubble sort is given as Algorithm 4. We can imagine the elements in the list placed in a column. In the bubble sort, the smaller elements "bubble" to the top as they are interchanged with larger elements. The larger elements "sink" to the bottom. This is illus­ trated in Example 4. Use the bubble sort to put 3, 2, 4, 1, 5 into increasing order. Solution: The steps of this algorithm are illustrated in Figure 1. Begin by comparing the first two elements, 3 and 2. Because 3 > 2, interchange 3 and 2, producing the list 2,3,4, 1,5. Because 3 < 4, continue by comparing 4 and 1. Because 4 > 1, interchange 1 and 4, producing the list 2, 3, 1,4, 5. Because 4 < 5, the first pass is complete. The first pass guarantees that the largest element, 5, is in the correct position. The second pass begins by comparing 2 and 3. Because these are in the correct order, 3 and 1 are compared. Because 3 > 1, these numbers are interchanged, producing 2, I, 3, 4, 5. Because 3 < 4, these numbers are in the correct order. It is not necessary to do any more comparisons for this pass because 5 is already in the correct position. The second pass guarantees that the two largest elements, 4 and 5, are in their correct positions. The third pass begins by comparing 2 and I. These are interchanged because 2 > 1, produc­ ing 1,2,3,4,5. Because 2 < 3, these two elements are in the correct order. It is not necessary to do any more comparisons for this pass because 4 and 5 are already in the correct positions. The third pass guarantees that the three largest elements, 3,4, and 5, are in their correct positions. The fourth pass consists of one comparison, namely, the comparison of I and 2. Because 1 < 2, these elements are in the correct order. This completes the bubble sort. .... ALGORITHM 4 The Bubble Sort. procedure bubblesort(a J, ... ,an: real numbers with n ::: 2) for i := I to n -1 for j := 1 to n -i if aj > aj+J then interchange aj and aj+J {aJ, ... , an is in increasing order} 174 3/ The Fundament als: Algorithms, the Integers, and Matrices 3-8 linkS� THE INSERTION SORT The insertion sort is a simple sorting algorithm, but it is usually not the most efficient. To sort a list with n elements, the insertion sort begins with the second element. The insertion sort compares this second element with the first element and inserts it before the first element if it does not exceed the first element and after the first element if it exceeds the first element. At this point, the first two elements are in the correct order. The third element is then compared with the first element, and if it is larger than the first element, it is compared with the second element; it is inserted into the correct position among the first three elements. In general, in the jth step of the insertion sort, the jth element of the list is inserted into the correct posi tion in the list of the previously sorted j -1 elements. To insert the jth element in the list, a linear search technique is used (see Exercise 43); the jth element is successively compared with the already sorted j -1 elements at the start ofthe list until the first element that is not less than this element is found or until it has been compared with all j -1 elements; the jth element is inserted in the correct position so that the first j elements are sorted. The algorithm continues until the last element is placed in the correct position relative to the already sorted list of the first n -1 elements. The insertion sort is described in pseudocode in Algorithm 5. EXAMPLE 5 Use the insertion sort to put the elements of the list 3, 2, 4, 1, 5 in increasing order. Solution: The insertion sort first compares 2 and 3. Because 3 > 2, it places 2 in the first position, producing the list 2,3,4, 1,5 (the sorted part of the list is shown in color). At this point, 2 and 3 are in the correct order. Next, it inserts the third element, 4, into the already sorted part of the list by making the comparisons A > 2 and 4 > 3. Because 4 > 3,4 is placed in the third position. At this point, the list is 2, 3, 4, 1, 5 and we know that the ordering of the first three elements is correct. Next, we find the correct place for the fourth element, 1, among the already sorted elements, 2, 3, 4. Because 1 < 2, we obtain the list 1,2, 3, 4,5. Finally, we insert 5 into the correct position by successively comparing it to 1,2,3, and 4. Because 5 > 4, it goes at the end of the list, producing the correct order for the entire list. � ALGORITHM 5 The Insertion Sort. procedure insertion sort(al' a2, ... , an: real numbers with n ::: 2) for j := 2 to n begin i := 1 whileaj > aj i := i + I m :=aj for k := 0 to j -i-I aj-k := aj-k-l aj :=m end {ai, a2, ... , an are sorted} Greedy Algorithms Many algorithms we will study in this book are designed to solve optimization problems. The goal of such problems is to find a solution to the given problem that either minimizes or maximizes the value of some parameter. Optimization problems studied later in this text include finding a route between two cities with smallest total mileage, determining a way to encode 3-9 unks� EXAMPLE 6 Demo� LEMMA l 3.1 Algorithms 175 messages using the fewest bits possible, and finding a set of fiber links between network nodes using the least amount of fiber. Surprisingl y, one of the simplest approaches often leads to a solution of an optimization problem. This approach selects the best choice at each step, instead of considering all sequences of steps that may lead to an optimal solution. Algorithms that make what seems to be the "best" choice at each step are called greedy algorithms. Once we know that a greedy algorithm finds a feasible solution, we need to determine whether it has found an optimal solution. To do this, we either prove that the solution is optimal or we show that there is a counterexample where the algorithm yields a nonoptimal solution. To make these concepts more concrete, we will consider an algorithm that makes change using coins. Consider the problem of making n cents change with quarters, dimes,nickels, and pennies, and using the least total number of coins. We can devise a greedy algorithm for making change for n cents by making a locally optimal choice at each step; that is, at each step we choose the coin ofthe largest denomination possible to add to the pile of change without exceeding n cents. For example, to make change for 67 cents, we first select a quarter (leaving 42 cents). We next select a second quarter (leaving 17 cents), followed by a dime (leaving 7 cents), followed by a nickel (leaving 2 cents), followed by a penny (leaving 1 cent), followed by a penny. .... We display a greedy change-making algorithm for n cents, using any set of denominations of coins, as Algorithm 6. ALGOR ITHM 6 Greedy Change-Making Algorithm. procedure change( Cl , C2, ••• , Cr: values of denominations of coins, where Cl > C2 > ... > Cr; n: a positive integer) for i := I to r while n ::: Ci begin end add a coin with value Ci to the change n:= n -Cj We have described a greedy algorithm for making change using quarters, dimes, nickels, and pennies. We will show that this algorithm leads to an optimal solution in the sense that it uses the fewest coins possible. Bef ore we embark on our proof, we show that there are sets of coins for which the greedy algorithm (Algorithm 6) does not necessarily produce change using the fewest coins possible. For example, if we have only quarters, dimes, and pennies (and no nickels) to use, the greedy algorithm would make change for 30 cents using six coins-a quarter and five pennies-whereas we could have used three coins, namely, three dimes. If n is a positive integer, then n cents in change using quarters, dimes, nickels, and pennies using the fewest coins possible has at most two dimes, at most one nickel, at most four pennies, and cannot have two dimes and a nickel. The amount of change in dimes, nickels, and pennies cannot exceed 24 cents. 176 3 / The Fundament als: Algorithms, the Integers, and Matrices 3-/0 THEOREM l unkS� Proof: We use a proof by contradiction. We will show that if we had more than the specified number of coins of each type, we could replace them using fewer coins that have the same value. We note that if we had three dimes we could replace them with a quarter and a nickel, if we had two nickels we could replace them with a dime, if we had five pennies we could replace them with a nickel, and if we had two dimes and a nickel we could replace them with a quarter. Because we can have at most two dimes, one nickel, and four pennies, but we cannot have two dimes and a nickel, it follows that 24 cents is the most money we can have in dimes, nickels, and pennies when we make change using the fewest number of coins for n cents. <J The greedy algorithm (Algorithm 6) produces change using the fewest coins possible. Proof: We will use a proof by contradiction. Suppose that there is a positive integer n such that there is a way to make change for n cents using quarters, dimes, nickels, and pennies that uses fewer coins than the greedy algorithm finds. We first note that q', the number of quarters used in this optimal way to make change for n cents, must be the same as q, the number of quarters used by the greedy algorithm. To show this, first note that the greedy algorithm uses the most quarters possible, so q' :s q. However, it is also the case that q' cannot be less than q. If it were, we would need to make up at least 25 cents from dimes, nickels, and pennies in this optimal way to make change. But this is impossible by Lemma 1. Because there must be the same number of quarters in the two ways to make change, the value of the dimes, nickels, and pennies in these two ways must be the same, and these coins are worth no more than 24 cents. There must be the same number of dimes, because the greedy algorithm used the most dimes possible and by Lemma 1, when change is made using the fewest coins possible, at most one nickel and at most four pennies are used, so that the most dimes possible are also used in the optimal way to make change. Similarly, we have the same number of nickels and, finally, the same number of pennies. <J The Halting Problem We will now describe a proof of one of the most famous theorems in computer science. We will show that there is a problem that cannot be solved using any procedure. That is, we will show there are unsolvable problems. The problem we will study is the baiting problem. It asks whether there is a procedure that does this: It takes as input a computer program and input to the program and determines whether the program will eventually stop when run with this input. It would be convenient to have such a procedure, if it existed. Certainly being able to test whether a program entered into an infinite loop would be helpful when writing and debugging programs. However, in 1936 Alan Turing showed that no such procedure exists (see his biography in Section 12.4). Before we present a proof that the halting problem is unsolvable, first note that we cannot simply run a program and observe what it does to determine whether it terminates when run with the given input. If the program halts, we have our answer, but if it is still running after any fixed length of time has elapsed, we do not know whether it will never halt or we just did not wait long enough for it to terminate. After all, it is not hard to design a program that will stop only after more than a billion years has elapsed. We will describe Turing's proof that the halting problem is unsolvable; it is a proof by contradiction. (The reader should note that our proof is not completely rigorous, because we have not explicitly defined what a procedure is. To remedy thi s, the concept of a Turing machine is needed. This concept is introduced in Section 12.5.) Proof: Assume there is a solution to the halting problem, a procedure called H (P, I). The procedure H (P, I) takes two inputs, one a program P and the other I, an input to the program .p. H(P,l) generates the string "halt" as output if H determines that P stops when given I as 3-11 Exercises P as program Input Program Output H(P'l) ProgramP H(P, P) P as in ut p Program K(P) 3.1 Algorithms 177 If H(P, P) = "halts," then loop forever If H(P, P) = "loops forever," then halt FIGURE 2 Showing that the Halting Problem is Unsolvable. input. Otherwise, H (P, l) generates the string "loops forever" as output. We will now derive a contradiction. When a procedure is coded, it is expressed as a string of characters; this string can be interpreted as a sequence of bits. This means that a program itself can be used as data. Therefore a program can be thought of as input to another program, or even itself. Hence, H can take a program P as both of its inputs, which are a program and input to this program. H should be able to determine if P will halt when it is given a copy of itself as input. To show that no procedure H exists that solves the halting problem, we construct a simple procedure K(P), which works as follows, making use of the output H(P, P). If the output of H (P, P) is "loops forever," which means that P loops forever when given a copy of itself as input, then K(P) halts. Ifthe output of H(p, P) is "halt," which means that P halts when given a copy of itself as input, then K (P) loops forever. That is, K (P) does the opposite of what the output of H(P, P) specifies. (See Figure 2.) Now suppose we provide K as input to K. We note that if the output of H(K, K) is "loops forever," then by the definition of K we see that K (K) halts. Otherwise, if the output of H (K, K) is "halt," then by the definition of K we see that K (K) loops forever, in violation of what H tells us. In both cases, we have a contradi ction. Thus, H cannot always give the correct answers. Consequently, there is no procedure that solves the halting problem. <l 1. List all the steps used by Algorithm 1 to find the maximum of the list 1, 8, 12,9, 11,2, 14,5, 10,4. 4. Describe an algorithm that takes as input a list of n integers and produces as output the largest difference obtained by subracting an integer in the list from the one following it. 2. Determine which characteristics of an algorithm the fol­ lowing procedures have and which they lack. a) procedure double(n: positive integer) while n > 0 n:= 2n b) procedure divide(n: positive integer) while n ::: 0 begin m := lin n:= n -1 end c) procedure sum(n: positive integer) sum:= 0 while i < 10 sum:= sum + i d) procedure choose(a,b: integers) x := either a or b 3. Devise an algorithm that finds the sum of all the integers in a list. 5. Describe an algorithm that takes as input a list of n inte­ gers in non decreasing order and produces the list of all values that occur more than once. 6. Describe an algorithm that takes as input a list of n in­ tegers and finds the number of negative integers in the list. 7. Describe an algorithm that takes as input a list of n inte­ gers and finds the location of the last even integer in the list or returns 0 if there are no even integers in the list. 8. Describe an algorithm that takes as input a list of n distinct integers and finds the location of the largest even integer in the list or returns 0 if there are no even integers in the list. 9. A palindrome is a string that reads the same forward and backward. Describe an algorithm for determining whether a string of n characters is a palindrome. 10. Devise an algorithm to compute xn, where x is a real 178 3 / The Fundamenta ls: Algorithms, the Integers, and Matrices 3-12 number and n is an integer. [Hint: First give a procedure for computing xn when n is nonnegative by successive multiplication by x, starting with 1. Then extend this pro­ cedure, and use the fact that x-n = I/xn to compute xn when n is negative.] 11. Describe an algorithm that interchanges the values of the variables x and y, using only assignments. What is the minimum number of assignment statements needed to do this? 12. Describe an algorithm that uses only assignment state­ ments that replaces the triple (x, y, z) with (y, z, x). What is the minimum number of assignment statements needed? 13. List all the steps used to search for 9 in the sequence 1,3, 4, 5, 6, 8, 9, 11 using a) a linear search. b) a binary search. 14. List all the steps used to search for 7 in the sequence given in Exercise 13. 15. Describe an algorithm that inserts an integer x in the ap­ propriate position into the list ai, a2, ... , an of integers that are in increasing order. 16. Describe an algorithm for finding the smallest integer in a finite sequence of natural numbers. 17. Describe an algorithm that locates the first occurrence of the largest element in a finite list of integers, where the integers in the list are not necessarily distinct. 18. Describe an algorithm that locates the last occurrence of the smallest element in a finite list of integers, where the integers in the list are not necessarily distinct. 19. Describe an algorithm that produces the maximum, me­ dian, mean, and minimum of a set of three integers. (The median of a set of integers is the middle element in the list when these integers are listed in order of increasing size. The mean of a set of integers is the sum of the integers divided by the number of integers in the set.) 20. Describe an algorithm for finding both the largest and the smallest integers in a finite sequence of integers. 21. Describe an algorithm that puts the first three terms of a sequence of integers of arbitrary length in increasing order. 22. Describe an algorithm to find the longest word in an English sentence (where a sentence is a sequence ofsym­ bois, either a letter or a blank, which can then be broken into alternating words and blanks). 23. Describe an algorithm that determines whether a function from a finite set of integers to another finite set of integers is onto. 24. Describe an algorithm that determines whether a function from a finite set to another finite set is one-to-one. 25. Describe an algorithm that will count the number of 1 s in a bit string by examining each bit ofthe string to determine whether it is a 1 bit. 26. Change Algorithm 3 so that the binary search procedure compares x to am at each stage ofthe algorithm, with the algorithm terminating if x = am. What advantage does this version of the algorithm have? . 27. The ternary search algorithm locates an element in a list of increasing integers by successively splitting the list into three sublists of equal (or as close to equal as possible) size, and restricting the search to the appropriate piece. Specify the steps of this algorithm. 28. Specify the steps of an algorithm that locates an element in a list of increasing integers by successively splitting the list into four sublists of equal (or as close to equal as possible) size, and restricting the search to the appropriate piece. 29. A mode of a list of integers is an element that occurs at least as often as each of the other elements. Devise an algorithm that finds a mode in a list of nondecreasing integers. 30. Devise an algorithm that finds all modes (defined in Exercise 29)in a list of nondecreasing integers. 31. Devise an algorithm that finds the first term of a sequence of integers that equals some previous term in the sequence. 32. Devise an algorithm that finds all terms of a finite se­ quence of integers that are greater than the sum of all previous terms of the sequence. 33. Devise an algorithm that finds the first term of a sequence of positive integers that is less than the immediately pre­ ceding term of the sequence. 34. Use the bubble sort to sort 6, 2, 3, 1, 5,4, showing the lists obtained at each step. 35. Use the bubble sort to sort 3, 1,5, 7,4, showing the lists obtained at each step. 36. Use the bubble sort to sort d, J, k, m, a, b, showing the lists obtained at each step. *37. Adapt the bubble sort algorithm so that it stops when no interchanges are required. Express this more efficient ver­ sion of the algorithm in pseudocode. 38. Use the insertion sort to sort the list in Exercise 34, show­ ing the lists obtained at each step. 39. Use the insertion sort to sort the list in Exercise 35, show­ ing the lists obtained at each step. 40. Use the insertion sort to sort the list in Exercise 36, show­ ing the lists obtained at each step. The selection sort begins by finding the least element in the list. This element is moved to the front. Then the least element � among the remaining elements is found and put into the sec­ ond position. This procedure is repeated until the entire list has been sorted. 41. Sort these lists using the selection sort. a) 3,5,4, 1,2 b) 5,4,3,2, 1 c) 1,2,3,4,5 42. Write the selection sort algorithm in pseudocode. I3r 43. Describe an algorithm based on the linear search for de­ termining the correct position in which to insert a new element in an already sorted list. 44. Describe an algorithm based on the binary search for de­ termining the correct position in which to insert a new element in an already sorted list. 3-/3 45. How many comparisons does the insertion sort use to sort the list 1,2, ... , n? 46. How many comparisons does the insertion sort use to sort the list n, n -1, ... , 2, I? The binary insertion sort is a variation of the insertion sort that uses a binary search technique (see Exercise 44) rather than a linear search technique to insert the ith element in the correct place among the previously sorted elements. 47. Show all the steps used by the binary insertion sort to sort the list 3, 2, 4, 5, 1, 6. 48. Compare the number of comparisons used by the inser­ tion sort and the binary insertion sort to sort the list 7, 4, 3,8, 1,5,4,2. *49. Express the binary insertion sort in pseudocode. 50. a) Devise a variation of the insertion sort that uses a lin­ ear search technique that inserts the jth element in the correct place by first comparing it with the (j - 1 )st element, then the (j -2)th element if necessary, and soon. b) Use your algorithm to sort 3, 2, 4,5, 1,6. c) Answer Exercise 45 using this algorithm. d) Answer Exercise 46 using this algorithm. 51. When a list of elements is in close to the correct order, would it be better to use an insertion sort or its variation described in Exercise 50? 52. Use the greedy algorithm to make change using quarters, dimes, nickels, and pennies for a) 87 cents. b) 49 cents. c) 99 cents. d) 33 cents. 53. Use the greedy algorithm to make change using quarters, dimes, nickels, and pennies for a) 51 cents. c) 76 cents. b) 69 cents. d) 60 cents. 54. Use the greedy algorithm to make change using quar­ ters, dimes, and pennies (but no nickels) for each of the amounts given in Exercise 52. For which ofthese amounts does the greedy algorithm use the fewest coins of these denominations possible? 55. Use the greedy algorithm to make change using quar­ ters, dimes, and pennies (but no nickels) for each of the amounts given in Exercise 53. For which of these amounts does the greedy algorithm use the fewest coins of these denominations possible? 56. Show that ifthere were a coin worth 12 cents, the greedy algorithm using quarters, l2-cent coins, dimes, nickels, and pennies would not always produce change using the fewest coins possible. 57. a) Describe a greedy algorithm that solves the problem of scheduling a subset of a collection of proposed talks in a lecture hall by selecting at each step the proposed talk that has the earliest finish time among all those that do not conflict with talks already scheduled. (In Section 4.1 we will show that this greedy algorithm 3.1 Algorithms 179 always produces a schedule that includes the largest number of talks possible.) b) Use your algorithm from part (a) to schedule the largest number of talks in a lecture hall from a pro­ posed set of talks, if the starting and finishing times of the talks are 9:00 A.M. and 9:45 A.M.; 9:30 A.M. and 10:00 A.M.; 9:50 A.M. and 10:15 A.M.; 10:00 A.M. and 10:30 A.M.; 10:10 A.M. and 10:25 A.M.; 10:30 A.M. and 10:55 A.M.; 10:15 A.M. and 10:45 A.M.; 10:30 A.M. and 11 :00 A.M.; 10:45 A.M. and 11 :30 A.M.; 10:55 A.M. and 11 :25 A.M.; 11 :00 A.M. and 11: 15 A.M. Suppose we have s men m I, m2, ... , ms and s women WI, W2, ... , W s' We wish to match each person with a mem­ � ber of the opposite gender. Furthermore, suppose that each � person ranks, in order of preference, with no ties, the people ofthe opposite gender. We say that an assignment of people of opposite genders to form couples is stable if we cannot find a man m and a woman W who are not assigned to each other such that m prefers W over his assigned partner and W prefers m to her assigned partner. 58. Suppose we have three men ml, m2, and m3 and three women WI, W2, and W3. Furthermore, suppose that the preference rankings of the men for the three women, from highest to lowest, are ml: W3, WI, W2; m2: WI, W2, W3; m3: W2, W3, WI; and thepreferencerankings ofthe women for the three men, from highest to lowest, are WI: m I , m 2, m3; W2: m2, ml, m3; W3: m3, m2, mI. For each of the six possible assignments of men and women to form three couples, determine whether this assignment is stable. *59. The deferred acceptance algorithm can be used to con­ struct an assignment of men and women. In this algorithm, members of one gender are the suitors and members of the other gender the suitees. The algorithm uses a se­ ries of rounds; in each round every suitor whose proposal was rejected in the previous round proposes to his or her highest ranking suitee who has not already rejected a pro­ posal from this suitor. The suitees reject all the proposals except that from the suitor that this suitee ranks highest among all the suitors who have proposed in this round or previous rounds. The proposal of this highest ranking suitor remains pending and is rejected in a later round if a more appealing suitor proposes in that round. The series of rounds ends when every suitor has exactly one pending proposal. All pending proposals are then accepted. a) Write the deferred acceptance algorithm in pseudocode. b) Show that the deferred acceptance algorithm terminates. c) Show that the deferred acceptance always terminates with a stable assignment. 60. Show that the problem of determining whether a program with a given input ever prints the digit 1 is unsolvable. 61. Show that the following problem is solvable. Given two programs with their inputs and the knowledge that exactly one of them halts, determine which halts. 62. Show that the problem of deciding whether a specific pro­ gram with a specific input halts is solvable. 180 3/ The Fundament als: Algorithms, the Integers, and Matrices 3-14 3.2 The Gr owth of Functi ons DEFINITION 1 Introduction In Section 3.1 we discussed the concept of an algorithm. We introduced algorithms that solve a variety of problems , including searching for an element in a list and sorting a list. In Section 3.3 we will study the number of operations used by these algorithm s. In particular, we will esti­ mate the number of comparisons used by the linear and binary search algorithms to find an element in a sequence of n elements. We will also estimate the number of comparisons used by the bubble sort and by the insertion sort to sort a list of n elements. The time required to solve a problem depends on more than only the number of operations it uses. The time also depends on the hardware and software used to run the program that implements the algorithm. However, when we change the hardware and software used to implement an algorithm, we can closely approximate the time required to solve a problem of size n by multiplying the previous time required by a constant. For example, on a supercomputer we might be able to solve a problem of size n a million times faster than we can on a PC. However, this factor of one million will not depend on n (except perhaps in some minor ways). One of the advan­ tages of using big-O notation, which we introduce in this section, is that we can estimate the growth of a function without worrying about constant multipliers or smaller order terms. This means that, using big-O notation, we do not have to worry about the hardware and software used to implement an algorithm. Furthermore, using big-O notation, we can assume that the different operations used in an algorithm take the same time, which simplifies the analysis considerably. Big-O notation is used extensively to estimate the number of operations an algorithm uses as its input grows. With the help of this notation, we can determine whether it is practical to use a particular algorithm to solve a problem as the size of the input increases. Furthermore, using big-O notation, we can compare two algorithms to determine which is more efficient as the size of the input grows. For instance, if we have two algorithms for solving a problem, one using 100n2 + 17n + 4 operations and the other using n3 operations, big-O notation can help us see that the first algorithm uses far fewer operations when n is large, even though it uses more operations for small values ofn, such as n = 10. This section introduces big-O notation and the related big-Omega and big-Theta notations. We will explain how big-O, big-Omega, and big-Theta estimates are constructed and establish estimates for some important functions that are used in the analysis of algorithms. Big-O Notation The growth of functions is often described using a special notation. Definition 1 describes this notation. Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f(x) is 0 (g(x» if there are constants C and k such that If(x)1 :::: C1g(x)1 whenever x > k. [This is read as "f(x) is big-oh of g(x )."] 3-/5 Assessment � unks� EXAM PLEl Extra� Examples Iiiiiiiij 3.2 The Growth of Functions 181 The constants C and k in the definition of big-O notation are called witnesses to the relationship I(x) is O(g(x». To establish that I(x) is O(g(x» we need only one pair of witnesses to this relationship. That is, to show that I(x) is O(g(x» , we need find only one pair of constants C and k, the witnesses, such that I/(x)1 � Clg(x)1 whenever x > k. Note that when there is one pair of witnesses to the relationship I(x) is O(g(x» , there are irifinitely many pairs of witnesses. To see this, note that if C and k are one pair of witnesses, then any pair C' and k', where C < C' and k < k', is also a pair of witnesses, because I/(x)1 � Cjg(x) 1 � C'lg(x)1 whenever x > k' > k. A useful approach for finding a pair of witnesses is to first select a value of k for which the size of I/(x)1 can be readily estimated when x > k and to see whether we can use this estimate to find a value of C for which I/(x)1 < C Ig(x)1 for x > k. This approach is illustrated in Example I. Show that I(x) = x2 + 2x + 1 is O(x2). Solution: We observe that we can readily estimate the size of I(x) when x > I because x < x2 and 1 < x2 when x > 1. It follows that whenever x > 1, as shown in Figure 1. Consequently, we can take C = 4 and k = 1 as witnesses to show that I(x) is O(x2). That is, I(x) = x2 + 2x + 1 < 4x2 whenever x > 1. (Note that it is not necessary to use absolute values here because all functions in these equalities are positive when x is positive.) Alternatively, we can estimate the size of I(x) when x > 2. When x > 2, we have 2x � x2 and 1 � x2• Consequently, if x > 2, we have It follows that C = 3 and k = 2 are also witnesses to the relation I(x) is O(x2). 4 3 2 2 The part of the graph of / (x) = x2+ 2x + 1 that satisfies/ex) < 4x2 is shown in color. FIGURE 1 The Function x2 + 2x + 1 is O(x2). 182 3/ The Fundament als: Algorithms, the Integers, and Matrices 3-16 unllS� Observe that in the relationship "f(x) is O(x2)," x2 can be replaced by any function with larger values than x2• For example, f(x) is O(x3), f(x) is O(x2 + X + 7), and so on. It is also true that x2 is O(x2 + 2x + 1), because x2 < x2 + 2x + 1 whenever x > 1. This means that C = 1 and k = 1 are witnesses to the relationship x2 is O(x2 + 2x + 1). .... Note that in Example 1 we have two functions, f(x) = x2 + 2x + 1 and g(x) = x2, such that f(x) is O(g(x)) and g(x) is O(f(x))-the latter fact following from the inequality x2 ::: x2 + 2x + 1, which holds for all nonnegative real numbers x. We say that two functions f(x) and g(x) that satisfy both of these big-O relationships are of the same order. We will return to this notion later in this section. Remark: The fact that f(x) is O(g(x)) is sometimes written f(x) = O(g(x)). However, the equals sign in this notation does not represent a genuine equality. Rather, this notation tells us that an inequality holds relating the values of the functions f and g for sufficiently large numbers in the domains of these functions. However, it is acceptable to write f(x ) E O(g(x)) because O(g(x)) represents the set of functions that are O(g(x)). Big-O notation has been used in mathemati cs for more than a century. In computer science it is widely used in the analysis of algorithms, as will be seen in Section 3.3. The German mathematician Paul Bachmann first introduced big-O notation in 1892 in an important book on number theory. The big-O symbol is sometimes called a Landau symbol after the German mathematician Edmund Landau, who used this notation throughout his work. The use ofbig-O notation in computer science was popularized by Donald Knuth, who also introduced the big-Q and big-e notations defined later in this section. When f(x) is o (g(x)), and h(x) is a function that has larger absolute values than g(x) does for sufficiently large values of x, it follows that f(x) is O(h(x)). In other words, the function g(x) in the relationship f(x) is o (g(x )) can be replaced by a function with larger absolute values. To see this, note that if If(x)1 ::: Clg(x) 1 if x > k, and if Ih(x)1 > Ig(x)1 for all x > k, then If(x)1 ::: Clh(x)1 if x > k. Hence, f(x) is O(h(x)). When big- O notation is used, the function g in the relationship f(x) is O(g(x)) is chosen to be as small as possible (sometimes from a set of reference functions, such as functions of the form xn, where n is a positive integer). PAUL GUSTAV HEINRICH BACHMANN (1837-1920) Paul Bachmann, the son ofa Lutheran pastor, shared his father's pious lifestyle and love of music. His mathematical talent was discovered by one of his teachers, even though he had difficulties with some of his early mathematical studies. After recuperating from tuberculosis in Switzerland, Bachmann studied mathema tics, first at the University of Berlin and later at Gottingen, where he attended lectures presented by the famous number theorist Dirichlet. He received his doctorate under the German number theorist Kummer in 1862; his thesis was on group theory. Bachmann was a professor at Breslau and later at Munster. After he retired from his professorship, he continued his mathematical writing, played the piano, and served as a music critic for newspapers. Bachmann's mathematical writings include a five-volume survey of results and methods in number theory, a two-volume work on elementary number theory, a book on irrational numbers, and a book on the famous conjecture known as Fermat's Last Theorem. He introduced big-O notation in his 1892 book Analytische Zahlentheorie. 3-17 k 3.2 The Growth of Functions 183 The part of the graph ofj(x) that satisfies j(x) < Cg(x) is shown in color. FIGURE 2 The Function I(x) is O(g(x». In subsequent discussions, we will almost always deal with functions that take on only positive values. All references to absolute values can be dropped when working with big-O estimates for such functions. Figure 2 illustrates the relationship I(x) is O(g(x» . Example 2 illustrates how big-O notation is used to estimate the growth of functions. EXAMPLE 2 Show that 7x2 is O(x3). Solution: Note that when x > 7, we have 7 x2 < X 3• (We can obtain this inequality by multiplying both sides of x > 7 by x2.) Consequently, we can take e = 1 and k = 7 as witnesses to establish the relationship 7x2 is O(x3). Alternatively, when x > 1, we have 7x2 < 7x3, so that e = 7 and k = 1 are also witnesses to the relationship 7x2 is O(x3). .... Example 3 illustrates how to show that a big-O relationship does not hold. EXAMPLE 3 Show that n2 is not O(n). unks� Solution: To show that n2 is not O(n), we must show that no pair of constants e and k exist such that n2 � en whenever n > k. To see that there are no such constants, observe that when n > 0 we can divide both sides of the inequality n2 � en by n to obtain the equivalent inequality n :::: e. We now see that no matter what e and k are, the inequality n � e cannot hold for all n with n > k. In particular, once we set a value of k, we see that when n is larger than the maximum of k and e, it is not true that n � e even though n > k. .... EDMUND LANDAU (1877-1938) Edmund Landau, the son ofa Berlin gynecologist, attended high school and university in Berlin. He received his doctorate in 1899, under the direction of Frobenius. Landau first taught at the University of Berlin and then moved to Gottingen, where he was a full professor until the Nazis forced him to stop teaching. Landau's main contributions to mathematics were in the field of analytic number theory. In particular, he established several important results concerning the distribution of primes. He authored a three-volume exposition on number theory as well as other books on number theory and mathematical analysis. 184 3 / The Fundament als: Algorithms, the Integers, and Matrices 3-/8 EXAM PLE 4 Example 2 shows that 7x2 is O(x3). Is it also true that x3 is O(7x2)? THEOREM l unkS� Solution: To determine whether x3 is 0(7x2), we need to determine whether there are constants C and k such that x3 ::::: C(7x2) whenever x > k. The inequality x3 ::::: C(7x2) is equivalent to the inequality x ::::: 7C, which follows by dividing the original inequality by the positive quantity x2. Note that no C exists for which x ::::: 7C for all x > k no matter what k is, because x can be made arbitrarily large. It follows that no witnesses C and k exist for this proposed big-O relationship. Hence, x3 is not 0(7x2). <OIl! Some Important Big-O Results Polynomials can often be used to estimate the growth of functions. Instead of analyzing the growth of polynomials each time they occur, we would like a result that can always be used to estimate the growth of a polynomial. Theorem I does this. It shows that the leading term of a polynomial dominates its growth by asserting that a polynomial of degree n or less is O(xn). Letf(x) = anxn + an_IXn-1 + ... + alx + ao, whereao, al, " " an-I, an are real numbers. Then f(x) is O(xn). DONALD E. KNUTH (BORN \938) Knuth grew up in Milwaukee, where his father taught bookkeeping at a Lutheran high school and owned a small printing business. He was an excellent student, earning academic achievement awards. He applied his intelligence in unconventional ways, winning a contest when he was in the eighth grade by finding over 4500 words that could be formed from the letters in "Ziegler's Giant Bar." This won a television set for his school and a candy bar for everyone in his class. Knuth had a difficult time choosing physics over music as his major at the Case Institute of Technology. He then switched from physics to mathemati cs, and in 1960 he received his bachelor of science degree, simultane­ ously receiving a master of science degree by a special award of the faculty who considered his work outstanding. At Case, he managed the basketball team and applied his talents by constructing a formula for the value of each player. This novel approach was covered by Newsweek and by Walter Cronkite on the CBS television network. Knuth began graduate work at the California Institute of Technology in 1960 and received his Ph.D. there in 1963. During this time he worked as a consultant, writing compilers for different computers. Knuth joined the staff ofthe California Institute of Technology in 1963, where he remained until 1968, when he took a job as a full professor at Stanford University. He retired as Professor Emeritus in 1992 to concentrate on writing. He is especially interested in updating and completing new volumes of his series The Art of Computer Programming. a work that has had a profound influence on the development of computer science, which he began writing as a graduate student in 1962, focusing on compilers. In common jargon, "Knuth," referring to The Art of Computer Programming. has come to mean the reference that answers all questions about such topics as data structures and algorithms. Knuth is the founder ofthe modern study of computational complexity. He has made fundamental contributions to the subject of compilers. His dissatisfaction with mathematics typography sparked him to invent the now widely used TeX and Metafont systems. TeX has become a standard language for computer typography. Two of the many awards Knuth has received are the 1974 Turing Award and the 1979 National Medal of Technology, awarded to him by President Carter. Knuth has written for a wide range of professional journals in computer science and in mathemati cs. However, his first publication, in 1957, when he was a college freshman, was a parody ofthe metric system called "The Potrzebie Systems of Weights and Measures," which appeared in MAD Magazine and has been in reprint several times. He is a church organist, as his father was. He is also a composer of music for the organ. Knuth believes that writing computer programs can be an aesthetic experience, much like writing poetry or composing music. Knuth pays $2.56 for the first person to find each error in his books and $0.32 for significant suggestions. If you send him a letter with an error (you will need to use regular mail, because he has given up reading e-mail), he will eventually inform you whether you were the first person to tell him about this error. Be prepared for a long wait, because he receives an overwhelming amount of mail. (The author received a letter years after sending an error report to Knuth, noting that this report arrived several months after the first report of this error.) 3-/9 3.2 The Growth of Functions 185 Proof: Using the triangle inequality (see Exercise 5 in Section 1.7), if x > 1 we have If(x)1 = lanxn + an_Ixn-1 + ... + alx + aol ::::: Ian Ixn + lan_Ilxn-1 + ... + lallx + laol = xn (Ian I + lan-II/x + ... + lall/xn-I + laol/xn) ::::: xn (Ianl + lan-II + ... + lall + lao!). This shows that where C = Ian I + lan-II + ... + lao I whenever x > 1. Hence, the witnesses C = Ian I + lan-II + ... + laol and k = 1 show that f(x) is O(xn). <l We now give some examples involving functions that have the set of positive integers as their domains. EXAM PLE 5 How can big-O notation be used to estimate the sum of the first n positive integers? Solution: Because each of the integers in the sum of the first n positive integers does not exceed n, it follows that 1 + 2 + ... + n ::::: n + n + ... + n = n2• From this inequality it follows that 1 + 2 + 3 + ... + n is O(n2), taking C = 1 and k = 1 as witnesses. (In this example the domains of the functions in the big-O relationship are the set of positive integers.) .... In Example 6 big-O estimates will be developed for the factorial function and its loga­ rithm. These estimates will be important in the analysis of the number of steps used in sorting procedure s. EXAM PLE 6 Give big-O estimates for the factorial function and the logarithm of the factorial function, where the factorial function f(n) = n! is defined by n! = 1·2·3· ... ·n whenever n is a positive integer, and O! = 1. For example, 1!=1, 2!=1·2=2, 3!=1·2·3=6, 4!=1·2·3·4=24. Note that the function n! grows rapidly. For instance, 20! = 2,432,902,008,176,640,000. Solution: A big-O estimate for n! can be obtained by noting that each term in the product does not exceed n. Hence, n!=1·2·3· ····n ::::: n· n· n· .... n 186 3 / The Fundament als: Algorithms, the Integers, and Matrices 3-20 This inequality shows that n! is O(nn), taking C = 1 and k = 1 as witnesses. Taking logarithms of both sides of the inequality established for n!, we obtain logn! :::: lognn = n logn. This implies that log n! is 0 (n log n), again taking C = 1 and k = 1 as witnesses. EXAMPLE 7 In Section 4.1, we will show that n < 2n whenever n is a positive integer. Show that this inequality implies that n is O(2n), and use this inequality to show that logn is O(n). Solution: Using the inequality n < 2n, we quickly can conclude that n is O(2n) by taking k = C = I as witnesses. Note that because the logarithm function is increasing, taking logarithms (base 2) of both sides of this inequality shows that logn < n. It follows that logn is O(n). (Again we take C = k = 1 as witnesses.) If we have logarithms to a base b, where b is different from 2, we still have 10gb n is O(n) because logn n 10gb n = log b < log b whenever n is a positive integer. We take C = 1/ log b and k = 1 as witnesses. (We have used Theorem 3 in Appendix 2 to see that 10gb n = log n / log b.) � As mentioned before, big-O notation is used to estimate the number of operations needed to solve a problem using a specified procedure or algorithm. The functions used in these estimates often include the following: I, logn, n, nlogn, n2, 2n, n! Using calculus it can be shown that each function in the list is smaller than the succeeding function, in the sense that the ratio of a function and the succeeding function tends to zero as n grows without bound. Figure 3 displays the graphs of these functions, using a scale for the values of the functions that doubles for each successive marking on the graph. That is, the vertical scale in this graph is logarithmic. The Growth of Combinations of Functions Many algorithms are made up of two or more separate subprocedur es. The number of steps used by a computer to solve a problem with input of a specified size using such an algorithm is the sum of the number of steps used by these subprocedur es. To give a big- O estimate for the number of steps needed, it is necessary to find big-O estimates for the number of steps used by each subproced ure and then combine these estimates. Big-O estimates of combinations of functions can be provided if care is taken when different big-O estimates are combined. In particular, it is often necessary to estimate the growth of the sum and the product of two functions. What can be said if big-O estimates for each of two 4096 2048 \024 512 256 128 64 32 16 8 4 2 2 3 4 5 6 7 8 3.2 The Growth of Functions 187 FIGURE 3 A Display ofthe Growth of Functions Common ly Used in Big-O Estimates. functions are known? To see what sort of estimates hold for the sum and the product of two functions, suppose that fl(x) is O(gl(x» and fz(x) is O(g2(X» . From the definition ofbig-O notation, there are constants C), C2, k), and k2 such that when x > k), and Ifz(x)1 :s C2Ig2(X)1 when x > k2. To estimate the sum of fl (x) and fz(x), note that lUI + fz)(x)1 = Ifl(x) + fz(x)1 :s Ifl(x)1 + Ifz(x)1 using the triangle inequality la + hi::: lal + IhI- When x is greater than both kl and k2, it follows from the inequalities for Ifl(x)1 and Ifz(x)1 that Ifl(x)1 + Ifz(x)1 :s Cllgl(x)1 + C2Ig2(X)1 :s Cllg(x)1 + C2Ig(x)1 = (CI + C2)lg(x)1 = Clg(x) I , where C = CI + C2 andg(x) = max(lgl(x)l, Ig2(X)I). [Here max(a, b) denotes the maximum, or larger, of a and b.] This inequality shows that lUI + fz)(x) I :s Clg(x)1 whenever x > k, where k = max(kl, k2). We state this useful result as Theorem 2. 188 3 / The Fundament als: Algorithms, the Integers, and Matrices 3-22 THEOREM 2 Suppose that II (x) is O(gl(x)) and hex) is 0(g2(X)). Then (II + h)(x) is O(max(lgl(x)l, Ig2(X)I))· COROL LARY 1 THEOREM 3 We often have big-0 estimates for II and 12 in terms of the same function g. In this situation, Theorem 2 can be used to show that (II + h)(x) is also O(g(x)), because max(g(x), g(x)) = g(x). This result is stated in Corollary 1. Suppose that II (x) and h(X) are both O(g(x)). Then (II + h)(x) is O(g(x)). In a similar way big-O estimates can be derived for the product of the functions II and h. When x is greater than max(kl, k2) it follows that 1(llh)(x)1 = 1/1(x)llh(x)1 :::: C1Igl(X)IC2Ig2(X)1 :::: CIC21(glg2)(x)1 :::: CI(glg2)(x)l, where C = CIC2. From this inequality, it follows that II(x)h(x) is 0(glg2), because there are constants C and k, namely, C = CIC2 and k = max(kl, k2), such that I (ld2)(X) I :::: Clgl(X)g2(X)1 whenever x > k. This result is stated in Theorem 3. Suppose that II (x) is O(gI(X)) and 12(x) is 0(g2(X)). Then (llh)(x) is 0(gl(X)g2(X)). The goal in using big-O notation to estimate functions is to choose a function g(x) that grows relatively slowly so that I(x) is 0 (g(x )). Examples 8 and 9 illustrate how to use Theorems 2 and 3 to do this. The type of analysis given in these examples is often used in the analysis of the time used to solve problems using computer programs. EXAMPLE 8 Give a big-O estimate for I(n) = 3n log(n!) + (n2 + 3) log n, where n is a positive integer. Solution: First, the product 3n log(n!) will be estimated. From Example 6 we know that log(n!) is O(n logn). Using this estimate and the fact that 3n is O(n), Theorem 3 gives the estimate that 3n 10g(n!) is 0(n210gn). Next, the product (n2 + 3) log n will be estimated. Because (n2 + 3) < 2n2 when n > 2, it follows that n2 + 3 is 0(n2). Thus, from Theorem 3 it follows that (n2 + 3) log n is 0(n210g n). Using Theorem 2 to combine the two big-O estimates for the products shows that I(n) = 3n 10g(n!) + (n2 + 3) log n is 0(n210g n). .... EXAMPLE 9 Give a big-O estimate for I(x) = (x + 1) log(x2 + 1) + 3x2. Solution: First, a big-O estimate for (x + 1) 10g(x2 + 1) will be found. Note that (x + 1) is o (x). Furthermore, x2 + 1 :::: 2X2 when x > 1. Hence, log(x2 + 1) :::: log(2x2) = log 2 + logx2 = log 2 + 210gx :::: 310gx, if x > 2. This shows that log(x2 + 1) is O(logx). 3-23 3.2 The Growth of Functions 189 From Theorem 3 it follows that (x + 1)log(x2 + 1) is O(x 10gx). Because 3x2 is O(x2), Theorem 2 tells us that f(x) is O(max(x log x, x2». Because x logx ::::: x2, for x > 1, it follows that f(x) is O(x2). .... Big-Omega and Big-Theta Notation Big-O notation is used extensively to describe the growth of functions, but it has limitations. In particular, when f(x) is O(g(x» , we have an upper bound, in terms of g(x), for the size of f(x) forlarge values of x. However, big-O notation does not provide a lower bound for the size of f(x) for large x. For this, we use big-Omega (big-Q) notation. When we want to give both an upper and a lower bound on the size of a function f(x), relative to a reference function g(x), we use big­ Theta (big-8) notation. Both big-Omega and big-Theta notation were introduced by Donald Knuth in the 1970s. His motivation for introducing these notations was the common misuse of big-O notation when both an upper and a lower bound on the size of a function are needed. We now define big-Omega notation and illustrate its use. After doing so, we will do the same for big-Theta notation. There is a strong connection between big-O and big-Omega notation. In particular, f(x) is Q(g(x» if and only if g(x) is o (f(x» . We leave the verification of this fact as a straightforward exercise for the reader. DEFINITION 2 Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f(x) is Q(g(x» ifthere are positive constants C and k such that If(x)1 ::: Clg(x)1 whenever x > k. [This is read as "f(x) is big-Omega of g(x)."] EXAMPLE 10 The function f(x) = 8x3 + 5x2 + 7 is Q(g(x» , where g(x) is the function g(x) = x3• This is easy to see because f(x) = 8x3 + 5x2 + 7 ::: 8x3 for all positive real numbers x. This is equivalent to saying that g(x) = x3 is O(8x3 + 5x2 + 7), which can be established directly by turning the inequality around. .... DEFINITION 3 Often, it is important to know the order of growth of a function in terms of some relatively simple reference function such as xn when n is a positive integer or eX, where e > 1. Knowing the order of growth requires that we have both an upper bound and a lower bound for the size of the function. That is, given a function f(x), we want a reference function g(x) such that f(x) is O(g(x» and f(x) is Q(g(x» . Big-Theta notation, defined as follows, is used to express both of these relationships, providing both an upper and a lower bound on the size of a function. Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f(x) is 8(g(x» if f(x) is O(g(x» and f(x) is Q(g(x». When f(x) is 8(g(x», we say that "f is big-Theta of g(x)" and we also say that f(x) is of order g(x). When f(x) is 8(g(x» , it is also the case that g(x) is 8(f(x». Also note that f(x) is 8(g(x» if and only if f(x) is o (g(x» and g(x) is O(f(x» (see Exercise 25). 190 3/ The Fundamental s: Algorithms, the Integers, and Matrices 3-24 EXAM PLE 11 EXAMPLE 12 Usually, when big-Theta notation is used, the function g(x) in 8 (g(x » is a relatively simple reference function, such as xn, c;X, log x, and so on, while f(x) can be relatively complicated. We showed (in Example 5) that the sum of the first n positive integers is O(n2). Is this sum of order n2? Solution: Let fen) = 1 + 2 + 3 + ... + n. Because we already know that fen) is O(n2), to show that f(n) is of order n2 we need to find a positive constant C such that f(n) > Cn2 for sufficiently large integers n. To obtain a lower bound for this sum, we can ignore the first half of the terms. Summing only the terms greater than r n /21, we find that 1 + 2 + ... + n � r n /21 + ( r n /21 + I) + ... + n � rn/21 + rn/21 + ... + rn/21 = (n -rn/21 + I) rn/21 � (n/2)(n/2) = n2/4. This shows that fen) is Q(n2). We conclude that fen) is of order n2, or in symbols, fen) is 8(n2). .... We can show that f(x) is 8(g(x» if we can find positive real numbers C1 and C2 and a positive real number k such that C1Ig(x)1 :s If(x)1 :s C2Ig(x)1 whenever x > k. This shows that f(x) is O(g(x» and f(x) is Q(g(x». Show that 3x2 + 8x logx is 8(x2). Solution: Because 0 :s 8x log x :s 8x2, it follows that 3x2 + 8x log x :s IIx2 for x > I. Conse­ quently, 3x2 + 8x log x is O(x2). Clearly, x2 is O(3x2 + 8x log x). Consequently, 3x2 + 8x log x is 8(x2). .... One useful fact is that the leading term of a polynomial determines its order. For example, if f(x) = 3x5 + X4 + 17x3 + 2, then f(x) is of order x5• This is stated in Theorem 4, whose proof is left as an exercise at the end of this section. THEOREM 4 Let f(x) = anxn + an_lXn-1 + ... + alX + ao, where aO,al, ... , an are real numbers with an i-O. Then f(x) is of order xn . EXAMPLE 13 The polynomials 3x8 + IOx7 + 221x2 + 1444,x19 -18x4 -10, 112, and _x99 +40,OOlx98 + IOO,003x are of orders x8, x19, and x99, respective ly. .... Unfortunately, as Knuth observed, big-O notation is often used by careless writers and speakers as if it had the same meaning as big-Theta notation. Keep this in mind when you see big-O notation used. The recent trend has been to use big-Theta notation whenever both upper and lower bounds on the size of a function are needed. 3-25 Exercises In Exercises 1-14, to establish a big-O relationship, find wit­ nesses C and k such that If(x)1 ::: Clg(x)1 whenever x > k. 1. Determine whether each of these functions is O(x). a) f(x) = 10 b) f(x) = 3x + 7 c) f(x) = x2 +x + 1 d) f(x) = Slogx e) f(x) = LxJ t) f(x) = fx/21 2. Determine whether each of these functions is 0(x2). a) f(x) = 17x + 11 b) f(x) = x2 + 1000 c) f(x)=xlogx d) f(x)=x4/2 e) f(x) = 2x t) f(x) = LxJ . fxl 3. Use the definition of "f(x) is O(g(x» " to show that x4 + 9x3 + 4x + 7 is 0(X4). 4. Use the definition of "f(x) is O(g(x» " to show that 2x + 17 is OW). 5. Show that (x2 + I)/(x + I) is O(x). 6. Show that (x3 + 2x)/(2x + I) is 0(x2). 7. Find the least integer n such that f(x) is O(xn) for each of these functions. a) f(x) = 2x3 + x210gx b) f(x) = 3x3 + (logx)4 c) f(x) = (x4 + x2 + 1)/(x3 + I) d) f(x) = (x4 + Slogx)/(x4 + 1) 8. Find the least integer n such that f(x) is O(xn) for each of these functions. a) f(x) = 2x2 + x310gx b) f(x) = 3x5 + (logx)4 c) f(x) = (x4 + x2 + 1)/(x4 + 1) d) f(x) = (x3 + Slogx)/(x4 + I) 9. Show that x2 + 4x + 17 is 0(x3) but that x3 is not 0(x2 + 4x + 17). 10. Show that x3 is 0(X4) but that X4 is not 0(x3). 11. Show that 3x4 + I is 0(x4/2) and x4/2 is 0(3x4 + I). 12. Show that x logx is 0(x2) but that x2 is not O(x logx). 13. Show that 2n is 0(3n) but that 3n is not 0(2n). 14. Is it true that x3 is o (g(x », if g is the given function? [For example, if g(x) = x + I, this question asks whether x3 is O(x + I).] a) g(x) = x2 c) g(x) = x2 + x3 e) g(x) = ]X b) g(x) = x3 d) g(x) = x2 +x4 t) g(x) = x3/2 15. Explain what it means for a function to be 0(1). 16. Show that if f(x) is O(x), then f(x) is 0(x2). 17. Suppose that f(x), g(x), and h(x) are functions such that f(x) is O(g(x» and g(x) is O(h(x». Show that f(x) is O(h(x». 18. Let k be a positive integer. Show that I k + 2k + ... + nk is O(nk+l). 19. Give as good a big-O estimate as possible for each of these functions. a) (n2 + 8)(n + I) b) (n logn + n2)(n3 + 2) 3.2 The Growth of Functions 191 c) (n! + 2n)(n3 + log(n2 + I» 20. Give a big-O estimate for each of these functions. For the function g in your estimate f(x) is O(g), use a simple function g of smallest order. a) (n3 + n210gn)(logn + I) + (1710gn + 19)(n3 + 2) b) (2n + n2)(n3 + 3n) c) (nn + n2n + sn)(n! + sn) 21. Give a big-O estimate for each ofthese functions. For the function g in your estimate that f(x) is O(g(x» , use a simple function g of the smallest order. a) n log(n2 + I) + n210gn b) (n logn + 1)2 + (logn + 1)(n2 + I) c) n2n + nn2 22. For each function in Exercise I, determine whether that function is Q(x) and whether it is 8(x). 23. For each function in Exercise 2, determine whether that function is Q(x2) and whether it is 8(x2). 24. a) Show that 3x + 7 is 8(x). b) Show that 2x2 + x - 7 is 8(x2). c) Show that Lx + 1/2J is 8(x). d) Show that log(X2 + I) is 8(log2 x). e) Show that 10gIO x is 8(log2 x). 25. Show that f(x) is 8(g(x» if and only if f(x) is O(g(x» andg(x) is O(f(x» . 26. Show that if f(x) and g(x) are functions from the set of real numbers to the set of real numbers, then f(x) is O(g(x» if and only if g(x) is Q(f(x» . 27. Show that if f(x) and g(x) are functions from the set of real numbers to the set of real numbers, then f(x) is 8(g(x» if and only if there are positive constants k, C1, andC2 such thatCIIg(x)1 ::: If(x)1 ::: C2Ig(x)1 whenever x> k. 28. a) Show that 3x2 + x + I is 8(3x2) by directly finding the constants k, C1, and C2 in Exercise 27. b) Express the relationship in part (a) using a picture showing the functions 3x2 + x + I, C I . 3x2, and C2 ·3x2, and the constant k on the x-axis, where C1, C2, and k are the constants you found in part (a) to show that 3x2 + x + I is 8(3x2). 29. Express the relationship f(x) is 8(g(x» using a picture. Show the graphs of the functions f(x), CIIg(x)l, and C2Ig(x)l, as well as the constant k on the x-axis. 30. Explain what it means for a function to be Q(1). 31. Explain what it means for a function to be 8(1). 32. Give a big-O estimate of the product of the first n odd positive integers. 33. Show that if f and g are real-valued functions such that f(x) is O(g(x» , then for every positive integer k, fk(x) is O(gk(x» . [Note that fk(x) = f(xl.] 34. Show that for all real numbers a and b with a > I and b > I, if f(x) is o (logb x), then f(x) is O(loga x). 192 3 / The Fundament als: Algorithms, the Integers, and Matrices 35. Suppose that f(x) is o (g(x » where f and g are increas­ ing and unbounded functions. Show that log If(x)1 is O(log Ig(x )1). 36. Suppose that f(x) is O(g(x» . Does it follow that 2f(x) is o (2g(x»? . 37. Let fl(x) and hex) be functions from the set of real num­ bers to the set of positive real numbers. Show that if fl (x) and f2(X) are both 8(g(x », where g(x) is a function from the set of real numbers to the set of positive real numbers, then fl(x) + hex) is 8(g(x» . Is this still true if fl(x) and f2(X) can take negative values? 38. Suppose that f(x), g(x), and h(x) are functions such that f(x) is 8(g(x» and g(x) is 8(h(x». Show that f(x) is 8(h(x». 39. If fl(x) and hex) are functions from the set ofposi­ tive integers to the set of positive real numbers and fl (x) and f2 (x) are both 8(g(x» , is (fl - f2)(X) also 8(g(x» ? Either prove that it is or give a counterexam ple. 40. Show that if fl(x) and f2(X) are functions from the set of positive integers to the set of real numbers and fl(x) is 8(gl(x» and hex) is 8(g2(X», then (fJ/2)(X) is 8(glg2(X». 41. Find functions f and g from the set of positive integers to the set of real numbers such that fen) is not O(g(n» and g(n) is not O(f(n» simultaneously. 42. Express the relationship f(x) is Q(g(x» using a picture. Show the graphs of the functions f(x) and Cg(x), as well as the constant k on the real axis. 43. Show that if fl(x) is 8(gl(x» , hex) is 8(g2(X», and f2(X) i= 0 and g2 (x) i= 0 for all real numbers x > 0, then (fJ/h)(x) is 8«gJ/g2)(X». 44. Show that if f(x) = anxn + an_lxn-I + ... + alx + ao, where ao, ai, ... , an-I, and an are real numbers and an i= 0, then f(x) is 8(xn). Big-O, big-Theta, and big-Omega notation can be extended to functions in more than one variable. For example, the state­ ment f(x, y) is O(g(x, y» means that there exist constants C, kl, and k2 such that If(x, y)1 :::: Clg(x, y)1 whenever x > kl andy > k2. 45. Define the statement f(x, y) is 8(g(x, y». 46. Define the statement f(x, y) is Q(g(x, y». 47. Show that (x2 + xy + X logy)3 is O(x6y3). 48. Show that x5y3 + x4y4 + x3y5 is Q(x3y3). 49. Show that Lxy J is O(xy). 3-26 c) x2 is o(2X). d) x2 + X + 1 is not o(x2). 52. (Requires calculus) a) Show that if f(x) andg(x) are functions such that f(x) is o(g(x» and c is a constant, then c f(x) is o(g(x», where (cf)(x) = cf(x). b) Show that if fl(X), hex), and g(x) are functions such that fl(x) is o(g(x» and h ex) is o(g(x», then (fl + h)(x) is o(g(x», where (fl + h)(x) = fl(x) + hex). 53. (Requires calculus) Represent pictorially that x log x is O(X2) by graphing x logx, x2, and x logx/x2. Explain how this picture shows that x log x is O(X2). 54. (Requires calculus) Express the relationship f(x) is o(g(x» using a picture. Show the graphs of f(x), g(x), and f(x)/g(x ). *55. (Requires calculus) Suppose that f(x) is o(g(x». Does it follow that 2f(x) is o(2g(x»? *56. (Requires calculus) Suppose that f(x) is o(g(x». Does it follow that log If(x)1 is o(log Ig(x )I)? 57. (Requires calculus) The two parts of this exercise describe the relationship between little-o and big-O notation. a) Show that if f(x) and g(x) are functions such that f(x) is o(g(x», then f(x) is O(g(x» . b) Show that if f(x) and g(x) are functions such that f(x) is O(g(x» , then it does not necessarily follow that f(x) is o(g(x» . 58. (Requires calculus) Show that if f(x) is a polynomial of degree nand g(x) is a polynomial of degree m where m > n, then f(x) is o(g(x». 59. (Requires calculus) Show that if fl(x) is O(g(x» and hex) is o(g(x», then fl(x) + hex) is O(g(x» . 60. (Requires calculus) Let Hn be the nth harmonic number 1 1 1 Hn=I+2"+3+"'+;;' Show that Hn is o (log n). [Hint: First establish the inequality n 1 In 1 L -: < -dx j=2J I X by showing that the sum of the areas of the rectangles of height I/} with base from} -1 to}, for} = 2, 3, ... , n, is less than the area under the curve y = 1/ x from 2 to n.] *61. Show that n logn is O(logn!). 50. Show that fxYl is Q(xy). The following problems deal with another type of asymptotic notation, called Iittle-o notation. Because little-o notation is based on the concept of limits, a knowledge of calculus is needed for these problems. We say that f(x) is o(g(x» [read f(x) is "little-oh" of g(x)], when �62. Determine whether logn! is 8(n logn). Justify your answer. lim f(x) = o. x--->oo g(x) 51. (Requires calculus) Show that a) x2 is o(x3). b) x log x is O(X2). *63. Show that log n! is greater than (n logn)/4 for n > 4. [Hint: Begin with the inequality n! > n(n -1) (n -2)··· fn/2l] Let f(x) andg(x) be functions from the set of real numbers to the set of real numbers. We say that the functions f and g are asymptotic and write f(x) � g(x) iflimx--->oo f(x)/ g(x) = 1. 64. (Requires calculus) For each of these pairs of functions, determine whether f and g are asymptotic. 3-27 3.3 Complexity of Algorithms 193 a) f(x) = x2 + 3x + 7,g(x) = x2 + 10 65. (Requires calculus) For each of these pairs of functions, determine whether f and g are asymptoti c. b) f(x) = x2logx, g(x) = x3 c) f(x) = X4 + log(3x8 + 7), g(x) = (x2 + 17x + 3)2 d) f(x) = (x3 + x2 + X + 1)4, g(x) = (x4 + x3 + x2 + X + 1)3. 3.3 Complexity of Algorithms Introduction a) f(x) = log(X2 + 1), g(x) = logx b) f(x) = 2x+3,g(x) = 2 x+7 c) f(x) = 21', g(x) = 2X2 d) f(x) = 2x2+x+l, g(x) = 2x2+2x When does an algorithm provide a satisfactory solution to a problem? First, it must always produce the correct answer. How this can be demonstrated will be discussed in Chapter 4. Second, it should be efficient. The efficiency of algorithms will be discussed in this section. How can the efficiency of an algorithm be analyzed? One measure of efficiency is the time used by a computer to solve a problem using the algorithm, when input values are of a specified size. A second measure is the amount of computer memory required to implement the algorithm when input values are of a specified size. Questions such as these involve the computational complexity ofthe algorithm. An analysis of the time required to solve a problem of a particular size involves the time complexity of the algorithm. An analysis of the computer memory required involves the space complexity of the algorithm. Considerations of the time and space complexity of an algorithm are essential when algorithms are implemented. It is obviously important to know whether an algorithm will produce an answer in a microsecond, a minute, or a billion years. Likewise, the required memory must be available to solve a problem, so that space complexity must be taken into account. Considerations of space complexity are tied in with the particular data structures used to implement the algorithm. Because data structures are not dealt with in detail in this book, space complexity will not be considered. We will restrict our attention to time comple xity. Time Complexity The time complexity of an algorithm can be expressed in terms of the number of operations used by the algorithm when the input has a particular size. The operations used to measure time complexity can be the comparison of integers, the addition of integers, the multiplication of integers, the division of integers, or any other basic operation. Time complexity is described in terms of the number of operations required instead of actual computer time because of the difference in time needed for different computers to perform basic operations. Moreover, it is quite complicated to break all operations down to the basic bit operations that a computer uses. Furthermore, the fastest computers in existence can perform basic bit operations (for instance, adding, multiplying, comparing, or exchanging two bits) in 10-9 second (1 nanosecond), but personal computers may require 10-6 second (1 microsecond), which is 1000 times as long, to do the same operations. We illustrate how to analyze the time complexity of an algorithm by considering Algorithm 1 of Section 3.1, which finds the maximum of a finite set of integers. EXAMPLE 1 Describe the time complexity of Algorithm 1 of Section 3.1 for finding the maximum element in a set. Solution: The number of comparisons will be used as the measure of the time complexity of the algorithm, because comparisons are the basic operations used. 194 3 / The Fundament als: Algorithms, the Integers, and Matrices .3-28 To find the maximum element of a set with n elements, listed in an arbitrary order, the temporary maximum is first set equal to the initial term in the list. Then, after a comparison has been done to determine that the end of the list has not yet been reached, the temporary maximum and second term are compared, updating the temporary maximum to the value of the second term if it is larger. This procedure is continued, using two additional comparisons for each term of the list--one to determine that the end of the list has not been reached and another to determine whether to update the temporary maximum. Because two comparisons are used for each of the second through the nth elements and one more comparison is used to exit the loop when i = n + 1, exactly 2(n -1) + 1 = 2n - 1 comparisons are used whenever this algorithm is applied. Hence, the algorithm for finding the maximum of a set of n elements has time complexity 8(n), measured in terms of the number of comparisons used. ... Next, we will analyze the time complexity of searching algorithms. EXAMPLE 2 Describe the time complexity of the linear search algorithm. Solution: The number of comparisons used by the algorithm will be taken as the measure of the time complexity. At each step of the loop in the algorithm, two comparisons are performed­ one to see whether the end of the list has been reached and one to compare the element x with a term of the list. Finally, one more comparison is made outside the loop. Consequently, if x = aj, 2i + 1 comparisons are used. The most comparisons, 2n + 2, are required when the element is not in the list. In this case, 2n comparisons are used to determine that x is not aj, for i = 1, 2, ... , n, an additional comparison is used to exit the loop, and one comparison is made outside the loop. So when x is not in the list, a total of 2n + 2 comparisons are used. Hence, a linear search requires at most O(n) comparisons. ... WORST -CASE COMPLEX ITY The type of complexity analysis done in Example 2 is a worst­ case analysis. By the worst-case performance of an algorithm, we mean the largest number of operations needed to solve the given problem using this algorithm on input of specified size. Worst-case analysis tells us how many operations an algorithm requires to guarantee that it will produce a solution. EXAMPLE 3 Describe the time complexity of the binary search algorithm in terms of the number of compar­ isons used (and ignoring the time required to compute m = [(i + j)/2] in each iteration of the loop in the algorithm). Solution: For simplicity, assume there are n = 2k elements in the list ai, a2, ... , an, where k is a nonnegative integer. Note that k = log n. (If n, the number of elements in the list, is not a power of 2, the list can be considered part of a larger list with 2k+1 elements, where 2k < n < 2k+l. Here 2k+ 1 is the smallest power of 2 larger than n.) At each stage of the algorithm, i and j, the locations of the first term and the last term of the restricted list at that stage, are compared to see whether the restricted list has more than one term. If i < j, a comparison is done to determine whether x is greater than the middle term of the restricted list. At the first stage the search is restricted to a list with 2k-1 terms. So far, two comparisons have been used. This procedure is continued, using two comparisons at each stage to restrict the search to a list with half as many terms. In other words, two comparisons are used at the first stage of the algorithm when the list has 2k elements, two more when the search has been reduced to a list with 2k-1 elements, two more when the search has been reduced to a list with 2k-2 elements, and so on, until two comparisons are used when the search has been reduced to a list with 21 = 2 elements. Finally, when one term is left in the list, one comparison tells us that there are no additional terms left, and one more comparison is used to determine if this term is x. 3-29 3.3 Complexity of Algorithms 195 Hence, at most 2k + 2 = 2 log n + 2 comparisons are required to perform a binary search when the list being searched has 2k elements. (If n is not a power of 2, the original list is expanded to a list with 2k+ 1 terms, where k = L log n J, and the search requires at most 2 flog n 1 + 2 comparisons.) Consequently, a binary search requires at most 8(log n) comparisons. From this analysis it follows that the binary search algorithm is more efficient, in the worst case, than a linear search. .... AVERAGE-CASE COMPLEXITY Another important type of complexity analysis, besides worst-case analysis, is called average-case analysis. The average number of operations used to solve the problem over all inputs of a given size is found in this type of analysis. Average-case time complexity analysis is usually much more complicated than worst-case analysis. However, the average-case analysis for the linear search algorithm can be done without difficu lty, as shown in Example 4. EXAM PLE 4 Describe the average-case performance of the linear search algorithm, assuming that the element x is in the list. Solution: There are n types of possible inputs when x is known to be in the list. If x is the first term of the list, three comparisons are needed, one to determine whether the end of the list has been reached, one to compare x and the first term, and one outside the loop. If x is the second term of the list, two more comparisons are needed, so that a total of five comparisons are used. In general, if x is the ith term of the list, two comparisons will be used at each of the i steps of the loop, and one outside the loop, so that a total of 2i + 1 comparisons are needed. Hence, the average number of comparisons used equals 3 + 5 + 7 + ... + (2n + 1) 2(1 + 2 + 3 + ... + n) + n n n In Section 4.1 we will show that n(n + 1) 1+2+3+···+n = 2 . Hence, the average number of comparisons used by the linear search algorithm (when x is known to be in the list) is 2[n(n + 1)/2] 2 ----- +I=n+ , n which is 8(n). Remark: In the analysis in Example 4 it has been assumed that x is in the list being searched and it is equally likely that x is in any position. It is also possible to do an average-case analysis of this algorithm when x may not be in the list (see Exercise 13 at the end of this section). Remark: Although we have counted the comparisons needed to determine whether we have reached the end of a loop, these comparisons are often not counted. From this point on we will ignore such comparisons. WORST-CASE COMPLEXITY OF TWO SORTING ALGORITHMS We analyze the worst-case complexity of the bubble sort and the insertion sort in Examples 5 and 6. EXAMPLE 5 What is the worst-case complexity of the bubble sort in terms of the number of comparisons made? 196 3/ The Fundament als: Algorithms, the Integers, and Matrices 3-30 Solution: The bubble sort (described in Example 4 in Section 3.1) sorts a list by performing a sequence of passes through the list. During each pass the bubble sort successively compares adjacent elements, interchanging them if necessary. When the ith pass begins, the i-I largest elements are guaranteed to be in the correct positions. During this pass, n - i comparisons are used. Consequently, the total number of comparisons used by the bubble sort to order a list of n elements is (n -l)n (n -1) + (n -2) + ... + 2 + 1 = 2 using a summation formula that we will prove in Section 4.1. Note that the bubble sort always uses this many comparisons, because it continues even if the list becomes completely sorted at some intermediate step. Consequently, the bubble sort uses (n -l)n/2 comparisons, so it has 8(n2) worst-case complexity in terms of the number of comparisons used. .... EXAMPLE 6 What is the worst-case complexity of the insertion sort in terms of the number of comparisons made? Solution: The insertion sort (described in Section 3.1) inserts the jth element into the correct position among the first j - 1 elements that have already been put into the correct order. It does this by using a linear search technique, successively comparing the jth element with successive terms until a term that is greater than or equal to it is found or it compares a j with itself and stops because a j is not less than itself. Consequently, in the worst case, j comparisons are required to insert the jth element into the correct position. Therefore, the total number of comparisons used by the insertion sort to sort a list of n elements is n(n + 1) 2+3+"'+n= -1, 2 using the summation formula for the sum of consecutive integers that we will prove in Section 4.1 and noting that the first term, 1, is missing in this sum. Note that the insertion sort may use considerably fewer comparisons if the smaller elements started out at the end of the list. We conclude that the insertion sort has worst-case complexity 8(n2). .... Understanding the Complexit y of Algorithms Table 1 displays some common terminology used to describe the time complexity of algorithms. For example, an algorithm that finds the largest of the first 100 terms of a list of n elements by TABLE 1 Commonly Used Terminology for the Complexity of Algorithms. Complexity Terminology 8(1) Constant complexity 8(log n) Logarithmic complexity 8(n) Linear complexity 8(n log n) n log n complexity 8(nb) Polynomial complexity 8(bn), where b > 1 Exponential complexity 8(n!) Factorial complexity unks� 3.3 Complexity of Algorithms 197 applying Algorithm 1 to the sequence of the first 100 terms, where n is an integer with n ::: 100, has constant complexity because it uses 99 comparisons no matter what n is (as the reader can verify). The linear search algorithm has linear (worst-case or average-case) complexity and the binary search algorithm has logarithmic (worst-case) complexit y. Many important algorithms have n log n complexity, such as the merge sort, which we will introduce in Chapter 4. An algorithm has polynomial complexity ifit has complexity 8(nh), where b is an integer with b ::: 1. For example, the bubble sort algorithm is a polynomial-time algorithm because it uses 8(n2) comparisons in the worst case. An algorithm has exponential complexity if it has time complexity 8(bn), where b > 1. The algorithm that determines whether a compound proposition in n variables is satisfiable by checking all possible assignments of truth variables is an algorithm with exponential complexity because it uses 8(2n) operations. Finally, an algorithm has factorial complexity if it has 8(n!) time complexity. The algorithm that finds all orders that a travelling salesman could use to visit n cities has factorial complexity; we will discuss this algorithm in Chapter 9. A problem that is solvable using an algorithm with polynomial worst-case complexity is called tractable, because the expectation is that the algorithm will produce the solution to the problem for reasonably sized input in a relatively short time. However, if the polynomial in the big-8 estimate has high degree (such as degree 100) or if the coefficients are extremely large, the algorithm may take an extremely long time to solve the problem. Consequently, that a problem can be solved using an algorithm with polynomial worst-case time complexity is no guarantee that the problem can be solved in a reasonable amount of time for even relatively small input value s. Fortunately, in practice, the degree and coefficients of polynomials in such estimates are often small. The situation is much worse for problems· that cannot be solved using an algorithm with worst-case polynomial time complexit y. Such problems are called intractable. Usually, but not always, an extremely large amount of time is required to solve the problem for the worst cases of even small input values. In practice, however, there are situations where an algorithm with a certain worst-case time complexity may be able to solve a problem much more quickly for most cases than for its worst case. When we are willing to allow that some, perhaps small, number of cases may not be solved in a reasonable amount of time, the average-case time complexity is a better measure of how long an algorithm takes to solve a problem. Many problems important in industry are thought to be intractable but can be practically solved for essentially all sets of input that arise in daily life. Another way that intractable problems are handled when they arise in prac­ tical applications is that instead oflooking for exact solutions of a problem, approximate solutions are sought. It may be the case that fast algorithms exist for finding such approximate solutions, perhaps even with a guarantee that they do not differ by very much from an exact solution. Some problems even exist for which it can be shown that no algorithm exists for solving them. Such problems are called unsolvable (as opposed to solvable problems that can be solved using an algorithm) . The first proof that there are unsolvable problems was provided by the great English mathemati cian and computer scientist Alan Turing when he showed that the halting problem is unsolvable. Recall that we proved that the halting problem is unsolvable in Section 3.1. (A biography of Alan Turing and a description of some of his other work can be found in Chapter 12.) The study of the complexity of algorithms goes far beyond what we can describe here. Note, however, that many solvable problems are believed to have the property that no algorithm with polynomial worst-case time complexity solves them, but that a solution, if known, can be checked in polynomial time. Problems for which a solution can be checked in polynomial time are said to belong to the class NP (tractable problems are said to belong to class P).* There is also an important class of problems, called NP-complete problems, with the property that if any of these problems can be solved by a polynomial worst-case time algorithm, then all problems in the class NP can be solved by polynomial worst-case time algorithm s. 'NP stands for nondeterministic polynomial time. 198 3/ The Fundament als: Algorithms, the Integers, and Matrices 3-32 The satisfiability problem is an example of an NP-complete problem-we can quickly verify that an assignment of truth values to the variables of a compound proposition makes it true, but no polynomial time algorithm has been discovered for finding such an assignment of truth values. [For example, an exhaustive search of all possible truth values requires E>(2n) bit operations where n is the number of variables in the compound proposition.] Furthermore, if a polynomial time algorithm for solving the satisfiability problem were known, there would be polynomial time algorithms for all problems known to be in this class of problems (and there are many important problems in this class). Despite extensive research, no polynomial worst-case time algorithm has been found for any problem in this class. It is generally accepted, although not proven, that no NP-complete problem can be solved in polynomial time. For more information about the complexity of algorithms, consult the references, including [CoLeRiStOl], for this section listed at the end of this book. (Also, for a more formal discussion of computational complexity in terms of Turing machines, see Section 12.5.) Note that a big-E> estimate of the time complexity of an algorithm expresses how the time required to solve the problem can change as the input grows in size. In practice, the best estimate (that is, with the smallest reference function) that can be shown is used. However, big-E> esti­ mates of time complexity cannot be directly translated into the actual amount of computer time used. One reason is that a big-E> estimate f(n) is E>(g(n» , where f(n) is the time complexity of an algorithm and g(n) is a reference function, means that CJg(n) � f(n) � C2g(n) when n > k, where C J, C 2, and k are constants. So without knowing the constants C J, C 2, and k in the inequality, this estimate cannot be used to determine a lower bound and an upper bound on the number of operations used in the worst case. As remarked before, the time required for an oper­ ation depends on the type of operation and the computer being used. Often, instead of a big-E> estimate on the worst-case time complexity of an algorithm, we have only a big-O estimate. Note that a big-O estimate on the time complexity of an algorithm provides an upper, but not a lower, bound on the worst-case time required for the algorithm as a function of the input size. Never­ theless, for simplicity, we will often use big-O estimates when describing the time complexity of algorithms, with the understanding that big-E> estimates would provide more information. However, the time required for an algorithm to solve a problem of a specified size can be determined if all operations can be reduced to the bit operations used by the computer. Table 2 displays the time needed to solve problems of various sizes with an algorithm using the indicated number of bit operations. Times of more than 10100 years are indicated with an asterisk. (In Section 3.6 the number of bit operations used to add and multiply two integers will be discussed .) In the construction of this table, each bit operation is assumed to take 10-9 second, which is the time required for a bit operation using the fastest computers today. In the future, these times will decrease as faster computers are developed. It is important to know how long a computer will need to solve a problem. For instance, if an algorithm requires 10 hours, it may be worthwhile to spend the computer time (and money) TABLE 2 The Computer Time Used by Algorithms. Problem Size Bit Operations Used n logn n nlogn n2 2n n! 10 3 x 10-9 S 10-8 S 3 X 10-8 S 10-7 S 10-6 S 3 X 10-3 S 102 7 X 10-9 S 10-7 S 7 X 10-7 S 10-5 S 4 X 1013 yr * 103 1.0 X 10-8 S 10-6 S 1 X 10-5 S 10-3 S * * 104 1.3 X 10-8 s 10-5 S 1 X 10-4 S 10-1 S * * 105 1.7 X 10-8 s 10-4 S 2 X 10-3 S 10 s * * 106 2 X 10-8 S 10-3 S 2 X 10-2 s 17min * * 3-33 3.3 Complexity of Algorithms 199 required to solve this problem. But, if an algorithm requires 10 billion years to solve a problem, it would be unreasonable to use resources to implement this algorithm. One of the most interesting phenomena of modem technology is the tremendous increase in the speed and memory space of computers . Another important factor that decreases the time needed to solve problems on computers is parallel processing, which is the technique of performing sequences of operations simultaneously. Efficient algorithm s, including most algorithms with polynomial time complexit y, bene­ fit most from significant technology improvements. However, these technology improvements offer little help in overcoming the complexity of algorithms of exponential or factorial time complexity. Because of the increased speed of computation, increases in computer memory, and the use of algorithms that take advantage of parallel processing, many problems that were considered impossible to solve five years ago are now routinely solved, and certainly five years from now this statement will still be true. Exercises 1. How many comparisons are used by the algorithm given in Exercise 16 of Section 3.1 to find the smallest natural number in a sequence of n natural numbers? 2. Write the algorithm that puts the first four terms of a list of arbitrary length in increasing order. Show that this al­ gorithmhas time complexity 0(1) in terms ofthe number of comparisons used. 3. Suppose that an element is known to be among the first four elements in a list of 32 elements. Would a linear search or a binary search locate this element more rapidly? 4. Determine the number of multiplications used to find X2k starting with x and successively squaring (to find x2, X4, and so on). Is this a more efficient way to find x2' than by multiplying x by itself the appropriate number of times? 5. Give a big-O estimate for the number of comparisons used by the algorithm that determines the number of 1 s in a bit string by examining each bit of the string to determine whether it is a 1 bit (see Exercise 25 of Section 3.1). *6. a) Show that this algorithm determines the number of 1 bits in the bit string S: procedure bit count (S: bit string) count:= 0 while S i= 0 begin count := count + 1 S := S /\ (S -1) end {count is the number of 1 s in S} Here S -I is the bit string obtained by changing the rightmost 1 bit of S to a 0 and all the 0 bits to the right of this to 1 s. [Recall that S /\ (S -1) is the bitwise AND of Sand S -1.] b) How many bitwise AND operations are needed to find the number of 1 bits in a string S? 7. The conventional algorithm for evaluating a polynomial anxn + an_lXn-1 + ... + alx + ao at x = c can be ex­ pressed in pseudocode by procedure polynomial(c, ao, aI, ... , an: real numbers) power:= I y:=ao for i := 1 to n begin power := power * c y := y + ai * power end {y = ancn + an_lCn-1 + ... + alc + aol where the final value of y is the value of the polynomial atx = c. a) Evaluate 3x2 + x + 1 at x = 2 by working through each step ofthe algorithm showing the values assigned at each assignment step. b) Exactly how many multiplications and additions are used to evaluate a polynomial of degree n at x = c? (Do not count additions used to increment the loop variable.) 8. There is a more efficient algorithm (in terms of the num­ ber of multiplications and additions used) for evaluating polynomials than the conventional algorithm described in the previous exercise. It is called Horner's method. This pseudocode shows how to use this method to find the value' of anxn + an_lXn-1 + ... + alX + ao at x = c. procedure Horner(c, ao, aI, a2, ... , an: real numbers) y:=an for i := 1 to n y := y * c + an-i {y = ancn + an_lCn-1 + ... + alc + aol a) Evaluate 3x2 + x + 1 at x = 2 by working through each step of the algorithm showing the values assigned at each assignment step. b) Exactly how many multiplications and additions are used by this algorithm to evaluate a polynomial of degree n at x = c? (Do not count additions used to increment the loop variable.) 9. What is the largest n for which one can solve in one 200 3 / The Fundament als: Algorithms, the Integers, and Matrices second a problem using an algorithm that requires fen) bit operations, where each bit operation is carried out in 10-9 second, with these values for f(n)? a) logn b) n c) n logn d) n2 e) 2n 1) n! 10. How much time does an algorithm take to solve a problem of size n if this algorithm uses 2n2 + 2n bit operations, each requiring 10-9 second, with these values of n? a) 10 b) 20 c) 50 d) 100 11. How much time does an algorithm using 250 bit operations need if each bit operation takes these amounts of time? a) 10-6 second b) 10-9 second c) 10-12 second 12. Determine the least number of comparisons, or best-case performance, a) required to find the maximum of a sequence of n in­ tegers, using Algorithm 1 of Section 3.1. b) used to locate an element in a list of n terms with a linear search. c) used to locate an element in a list of n terms using a binary search. 13. Analyze the average-case performance of the linear search algorithm, if exactly half the time element x is not in the list and if x is in the list it is equally likely to be in any position. 14. An algorithm is called optimal for the solution of a prob­ lem with respect to a specified operation if there is no algorithm for solving this problem using fewer operations. a) Show that Algorithm 1 in Section 3.1 is an optimal al­ gorithm with respect to the number of comparisons of integers. (Note: Comparisons used for bookkeeping in the loop are not of concern here.) b) Is the linear search algorithm optimal with respect to the number of comparisons of integers (not including comparisons used for bookkeeping in the loop)? 15. Describe the worst-case time complexit y, measured in terms of comparisons, of the ternary search algorithm described in Exercise 27 of Section 3.1. 16. Describe the worst-case time complexit y, measured in terms of comparisons, of the search algorithm described in Exercise 28 of Section 3.1. 17. Analyze the worst-case time complexity of the algorithm you devised in Exercise 29 of Section 3.1 for locating a mode in a list of nondecreasing integers. 18. Analyze the worst-case time complexity of the algorithm you devised in Exercise 30 of Section 3.1 for locating all modes in a list of nondecreasing integers. 3.4 The Integer s and Division Introduction 3-34 19. Analyze the worst-case time complexity of the algorithm you devised in Exercise 31 of Section 3.1 for finding the first term of a sequence of integers equal to some previous term. 20. Analyze the worst-case time complexity ofthe algorithm you devised in Exercise 32 of Section 3.1 for finding all terms of a sequence that are greater than the sum of all previous terms. 21. Analyze the worst-case time complexity of the algorithm you devised in Exercise 33 of Section 3.1 for finding the first term of a sequence less than the immediately preced­ ing term. 22. Determine the worst-case complexity in terms of com­ parisons of the algorithm from Exercise 5 in Section 3.1 for determining all values that occur more than once in a sorted list of integers. 23. Determine the worst-case complexity in terms of compar­ isons of the algorithm from Exercise 9 in Section 3.1 for determining whether a string is a palindrome. 24. How many comparisons does the selection sort (see preamble to Exercise 41 in Section 3.1) use to sort n items? Use your answer to give a big-O estimate of the complex­ ity ofthe selection sort in terms of number of comparisons for the selection sort. 25. Find a big-O estimate for the worst-case complexity in terms of number of comparisons used and the number of terms swapped by the binary insertion sort described in the preamble to Exercise 47 in Section 3.1. 26. Show that the greedy algorithm for making change for n cents using quarters, dimes, nickels, and pennies has O(n) complexity measured in terms of comparisons needed. 27. Describe how the number of comparisons used in the worst case changes when these algorithms are used to search for an element of a list when the size of the list doubles from n to 2n, where n is a positive integer. a) linear search b) binary search 28. Describe how the number of comparisons used in the worst case changes when the size of the list to be sorted doubles from n to 2n, where n is a positive integer when these sorting algorithms are used. a) bubble sort b) insertion sort c) selection sort (described in the preamble to Exercise 41 in Section 3.1) d) binary insertion sort (described in the preamble to Exercise 47 in Section 3.1) The part of mathematics involving the integers and their properties belongs to the branch of mathematics called number theory. This section is the beginning of a four-section introduction 3-35 3.4 The Integers and Division 201 to number theory. We will develop the basic concepts of number theory used throughout com­ puter science. In this section we will review some basic concepts of number theory, including divisibility and modular arithmetic. In Section 3.5 we will cover prime numbers and begin our discussion of greatest common divisors. In Section 3.6 we will describe several important algo­ rithms from number theory, tying together the material in Sections 3.1 and 3.3 on algorithms and their complexity with the notions introduced in this section and in Section 3.5. For example, we will introduce algorithms for finding the greatest common divisor of two positive integers and for performing computer arithmetic using binary expansions. Finally, in Section 3.7 , we will con­ tinue our study of number theory by introducing some important results and their applications to computer arithmetic and cryptology, the study of secret messages. We will rely on proof methods we studied in Chapter 1 to develop basic theorems in number theory. This will give you an oppor­ tunity to use what you have learned about proof methods and proof strategies in a new setting. The ideas that we will develop in this section are based on the notion of divisibility. Divi­ sion of an integer by a positive integer produces a quotient and a remainder. Working with these remainders leads to modular arithmetic, which is used throughout computer science. We will discuss three applications of modular arithmetic in this section: generating pseudorandom num­ bers, assigning computer memory locations to files, and encrypting and decrypting messages. Division When one integer is divided by a second, nonzero integer, the quotient mayor may not be an inte­ ger. For example, 12/3 = 4 is an integer, whereas 11/4 = 2.75 is not. This leads to Definition 1. DEFINITION 1 If a and b are integers with a #-0, we say that a divides b if there is an integer c such that b = ac. When a divides b we say that a is afactor of b and that b is a multiple of a. The notation a I b denotes that a divides b. We write a 1 b when a does not divide b. Remark: We can express a I b using quantifiers as 3c(ac = b), where the universe of discourse is the set of integers. In Figure 1 a number line indicates which integers are divisible by the positive integer d. EXAMPLE 1 Determine whether 3 17 and whether 3 I 12. EXAMPLE 2 Solution: It follows that 317, because 7/3 is not an integer. On the other hand, 3 I 12 because 12/3 = 4. <III Let nand d be positive integers. How many positive integers not exceeding n are divisible by d? Solution: The positive integers divisible by d are all the integers of the form dk, where k is a positive integer. Hence, the number of positive integers divisible by d that do not exceed n equals the number of integers k with 0 < dk :s n, or with 0 < k :s n/d. Therefore, there are Ln/dJ positive integers not exceeding n that are divisible by d. <III Some of the basic properties of divisibility of integers are given in Theorem 1. "I -3d -2d -d o d 2d FIGURE 1 Integers Divisible by the Positive Integer d. 1 .. 3d 202 3 / The Fundamental s: Algorithms, the Integers, and Matrices 3-36 THEOREM l COROLLAR Y l THEOREM 2 DEFINITION 2 Let a, b, and c be integers. Then (i) if a I b and a I c, then a I (b + c); (ii) if a I b, then a I bc for all integers c; (iii) if a I b and b I c, then a I c. Proof: We will give a direct proof of (i). Suppose that a I b and a I c. Then, from the definition of divisibility, it follows that there are integers sand t with b = as and c = at. Hence, b + c = as + at = a(s + t). Therefore, a divides b + c. This establishes part (i) of the theorem. The proofs of parts (ii) and (iii) are left as exercises for the reader. <J Theorem 1 has this useful consequence. If a, b, and c are integers such that a I b and a I c, then a 1mb + nc whenever m and n are integers. Proof: We will give a direct proof. By part (ii) of Theorem 1 it follows that a I mb and a Inc whenever m and n are integers. By part (i) of Theorem 1 it follows that a 1mb + nco <J The Div ision Algorithm When an integer is divided by a positive integer, there is a quotient and a remainder, as the division algorithm shows. THE DIVISION ALGORITHM Let a be an integer and d a positive integer. Then there are unique integers q and r, with 0 � r < d, such that a = dq· + r. We defer the proof of the division algorithm to Example 5 and Exercise 37 in Section 4.2. Remark: Theorem 2 is not really an algorithm. (Why not?) Nevertheless, we use its traditional name. In the equality given in the division algorithm, d is called the divisor, a is called the dividend, q is called the quotient, and r is called the remainder. ihis notation is used to express the quotient and remainder: q = a div d, r = a mod d. Examples 3 and 4 illustrate the division algorithm. 3-37 3.4 The Integers and Division 203 EXAMPLE 3 What are the quotient and remainder when 101 is divided by II? Solution: We have 101 = 11 ·9 + 2. Hence, the quotient when 10 1 is divided by 11 is 9 = 101 div 11, and the remainder is 2 = 101 mod 11. .... EXAMPLE 4 What are the quotient and remainder when -11 is divided by 3? DEFINITION 3 THEOREM 3 Solution: We have -11 = 3(-4) + 1. Hence, the quotient when -11 is divided by 3 is -4 = -11 div 3, and the remainder is 1 = -11 mod 3. Note that the remainder cannot be negative. Consequently, the remainder is not -2, even though -11 = 3(-3) -2, because r = -2 does not satisfy 0 .::: r < 3. Note that the integer a is divisible by the integer d if and only if the remainder is zero when a is divided by d. Modular Arithmetic In some situations we care only about the remainder of an integer when it is divided by some specified positive integer. For instance, when we ask what time it will be (on a 24-hour clock) 50 hours from now, we care only about the remainder when 50 plus the current hour is divided by 24. Because we are often interested only in remainders, we have special notations for them. We have a notation to indicate that two integers have the same remainder when they are divided by the positive integer m. If a and b are integers and m is a positive integer, then a is congruent to b modulo m if m divides a-b. We use the notation a = b (mod m) to indicate that a is congruent to b modulo m. If a and b are not congruent modulo m, we write a¢: b (mod m). The connection between the notations used when working with remainders is made clear in Theorem 3. Let a and b be integers, and let m be a positive integer. Then a = b (mod m) if and only if a mod m = b mod m. The proof of Theorem 3 is left as Exercises 11 and 12 at the end of this section. EXAMPLE 5 Determine whether 17 is congruent to 5 modulo 6 and whether 24 and 14 are congruent modulo 6. 204 3 / The Fundamental s: Algorithms, the Integers, and Matrices 3-38 THEOREM 4 THEOREM S links� Solution: Because 6 divides 17 -5 = 12, we see that 17 == 5 (mod 6). However, because 24 - 14 = 10 is not divisible by 6, we see that 24 ¢ 14 (mod 6). .... The great German mathematician Karl Friedrich Gauss developed the concept of congru­ ences at the end of the eighteenth century. The notion of congruences has played an important role in the development of number theory. Theorem 4 provides a useful way to work with congruenc es. Let m be a positive integer. The integers a and b are congruent modulo m if and only if there is an integer k such that a = b + km. Proof: If a == b (mod m), then m I (a -b). This means that there is an integer k such that a - b = km, so that a = b + km. Conversely, if there is an integer k such that a = b + km, then km = a-b. Hence, m divides a - b, so that a == b (mod m). <l The set of all integers congruent to an integer a modulo m is called the congruence class of a modulo m. In Chapter 8 we will show that there are m pairwise disjoint equivalence classes modulo m and that the union of these equivalence classes is the set of integers. Theorem 5 shows how congruences work with respect to addition and multiplication. Let m be a positive integer. If a == b (mod m) and c == d (mod m), then a +c == b +d (modm) and ac == bd (modm). Proof: Because a == b (mod m) and c == d (mod m), there are integers sand t with b = a + sm and d = c + tm. Hence, b + d = (a + sm) + (c + tm) = (a + c) + m(s + t) and bd = (a + sm)(c + tm) = ac + meat + cs + stm). Hence, a + c == b + d (mod m) and ac == bd (mod m). <l KARL FRIEDRICH GAUSS (1777-1855) Karl Friedrich Gauss, the son of a bricklayer, was a child prodigy. He demonstrated his potential at the age of 10, when he quickly solved a problem assigned by a teacher to keep the class busy. The teacher asked the students to find the sum of the first 100 positive integers. Gauss realized that this sum could be found by forming 50 pairs, each with the sum 101: 1 + 100,2 + 99, ... ,50 + 51. This brilliance attracted the sponsorship of patrons, including Duke Ferdinand of Brunswick, who made it possible for Gauss to attend Caroline College and the University of Gottingen. While a student, he invented the method of least squares, which is used to estimate the most likely value of a variable from experimental results. In 1796 Gauss made a fundamental discovery in geometry, advancing a subject that had not advanced since ancient times. He showed that a 17 -sided regular polygon could be drawn using just a ruler and compass. In 1799 Gauss presented the first rigorous proof of the Fundamental Theorem of Algebra, which states that a polynomial of degree n has exactly n roots (counting multiplicities). Gauss achieved worldwide fame when he successfully calculated the orbit of the first asteroid discovered, Ceres, using scanty data. Gauss was called the Prince of Mathematics by his contemporary mathemati cians. Although Gauss is noted for his many discoveries in geometry, algebra, analysis, astronom y, and physics, he had a special interest in number theory, which can be seen from his statement "Mathematics is the queen of the sciences, and the theory of numbers is the queen of mathemati cs." Gauss laid the foundations for modem number theory with the publication of his book Disquisitiones Arithmeticae in 1801. 3-39 3.4 The Integers and Division 205 EXAM PLE 6 Because 7 == 2 (mod 5) and 11 == 1 (mod 5), it follows from Theorem 5 that COROLLAR Y 2 EXAM PLE 7 UnkS� 18 = 7 + 11 == 2 + 1 = 3 (mod 5) and that 77 = 7 . 11 == 2 . 1 = 2 (mod 5). We will need the following corollary of Theorem 5 in Section 4.4. Let m be a positive integer and let a and b be integers. Then (a + b)modm = «amodm) +(bmodm))modm and abmodm = «amodm)(bmodm))modm. Proof: By the definitions mod m and the definition of congruence modulo m, we know that a == (a modm)(modm) and b == (b modm) (modm). Hence, Theorem 5 tell us that a + b == (a modm) + (bmodm) (modm) and ab == (amodm)(bmodm) (modm). The equalities in this corollary follow from these last two congruences by Theorem 3. <l We must be careful working with congruenc es. Some properties we may expect to be true are not valid. For example, if ae == be (mod m), the congruence a == b (mod m) may be false. Similarly, if a == b (mod m) and e == d (mod m), the congruence aC == bd (mod m) may be false. (See Exercise 23.) Applications of Congruences Number theory has applications to a wide range of areas. We will introduce three applications in this section: the use of congruences to assign memory locations to computer files, the generation of pseudorandom numbers, and cryptosystems based on modular arithmetic. Hashing Functions The central computer at an insurance company maintains records for each of its customers. How can memory locations be assigned so that customer records can be retrieved quickly? The solution to this problem is to use a suitably chosen hashing function. Records are identified using a key, which uniquely identifies each customer's records. For in­ stance, customer records are often identified using the Social Security number of the customer as the key. A hashing function h assigns memory location h(k) to the record that has k as its key. In practice, many different hashing functions are used. One of the most common is the function h(k) = k mod m where m is the number of available memory locations. 206 3 / The Fundamental s: Algorithms, the Integers, and Matrices 3-40 EXAMPLE 8 unkS� Hashing functions should be easily evaluated so that files can be quickly located. The hashing function h(k) = k mod m meets this requirement; to find h(k), we need only compute the remainder when k is divided by m. Furthermore, the hashing function should be onto, so that all memory locations are possible. The function h(k) = k mod m also satisfies this property. For example, when m = 111, the record of the customer with Social Security number 064212848 is assigned to memory location 14, because h(0642 12848) = 064212848 mod 111 = 14. Similarly, because h(037149212) = 037149212 mod 111 = 65, the record of the customer with Social Security number 037149212 is assigned to memory location 65. Because a hashing function is not one-to-one (because there are more possible keys than memory locations), more than one file may be assigned to a memory location. When this happens, we say that a collision occurs. One way to resolve a collision is to assign the first free location following the occupied memory location assigned by the hashing function. For example, after making the two earlier assignments, we assign location 15 to the record of the customer with the Social Security number 107405723. To see this, first note that h(k) maps this Social Security number to location 14, because h(107405723) = 107405723 mod 111 = 14, but this location is already occupied (by the file of the customer with Social Security number 064212848). However, memory location 15, the first location following memory location 14, is free. There are many more sophisticated ways to resolve collisions that are more efficient than the simple method we have described. These are discussed in the references on hashing functions given at the end of the book. � Pseudorandom Numbers Randomly chosen numbers are often needed for computer simu­ lations. Different methods have been devised for generating numbers that have properties of randomly chosen numbers. Because numbers generated by systematic methods are not truly random, they are called pseudorandom numbers. The most commonly used procedure for generating pseudorandom numbers is the linear congruential method. We choose four integers: the modulus m, multiplier a, increment c, and seed Xo, with 2 :s a < m, 0 :s c < m, and 0 :s Xo < m. We generate a sequence of pseudorandom numbers {xn}, with 0 :s Xn < m for all n, by successively using the congruence Xn+! = (axn + c) mod m. (This is an example of a recursive definition, discussed in Section 4.3. In that section we will show that such sequences are well defined.) Many computer experiments require the generation of pseudorandom numbers between 0 and 1. To generate such numbers, we divide numbers generated with a linear congruential generator by the modulus: that is, we use the numbers Xn / m. 3-41 3.4 The Integers and Division 207 For instance, the sequence of pseudorandom numbers generated by choosing m = 9, a = 7, c = 4, and Xo = 3, can be found as follows: XI = txo + 4 mod 9 = 7 . 3 + 4 mod 9 = 25 mod 9 = 7 X2 = 7x1 + 4 mod 9 = 7 . 7 + 4 mod 9 = 53 mod 9 = 8 X3 = 7X2 + 4 mod 9 = 7 . 8 + 4 mod 9 = 60 mod 9 = 6 X4 = 7X3 + 4 mod 9 = 7 . 6 + 4 mod 9 = 46 mod 9 = 1 X5 = 7X4 + 4 mod 9 = 7 . 1 + 4 mod 9 = 11 mod 9 = 2 X6 = 7X5 + 4 mod 9 = 7 . 2 + 4 mod 9 = 18 mod 9 = ° X7 = 7 X6 + 4 mod 9 = 7 . ° + 4 mod 9 = 4 mod 9 = 4 Xg = 7X7 + 4 mod 9 = 7 ·4 + 4 mod 9 = 32 mod 9 = 5 X9 = txg + 4 mod 9 = 7 . 5 + 4 mod 9 = 39 mod 9 = 3 Because X9 = Xo and because each term depends only on the previous term, this sequence is generated: 3,7,8,6,1,2,0,4,5,3,7,8,6,1,2,0,4, 5,3, .... This sequence contains nine different numbers before repeating. Most computers do use linear congruential generators to generate pseudorandom numbers. Often, a linear congruential generator with increment c = ° is used. Such a generator is called a pure multiplic ative generator . For example, the pure multiplica tive generator with modulus 231 - 1 and multiplier 75 = 16,807 is widely used. With these values, it can be shown that 231 - 2 numbers are generated before repetition begins. ... Cryptology Congruences have many applications to discrete mathematics and computer science. Discussions of these applications can be found in the suggested readings given at the end of the book. One of the most important applications of congruences involves cryptolo gy, which is the study of secret messages. One of the earliest known uses of cryptology was by Julius Caesar. He made messages secret by shifting each letter three letters forward in the alphabet (sending the last three letters of the alphabet to the first three). For instance, using this scheme the letter B is sent to E and the letter X is sent to A. This is an example of encryption, that is, the process of making a message secret. To express Caesar's encryption process mathematical ly, first replace each letter by an integer from ° to 25, based on its position in the alphabet. For example, replace A by 0, K by 10, and Z by 25. Caesar's encryption method can be represented by the function f that assigns to the nonnegative integer p, p :::: 25, the integer f(p) in the set {O, 1,2, ... , 25} with f(p) = (p + 3) mod 26. In the encrypted version of the message, the letter represented by p is replaced with the letter represented by (p + 3) mod 26. EXAMPLE 9 What is the secret message produced from the message "MEET YOU IN THE PARK" using the Caesar cipher? Solution: First replace the letters in the message with numbers. This produces 1244 19 241420 813 1974 1501710. 208 3 / The Fundamental s: Algorithms, the Integers, and Matrices 3-42 Now replace each ofthese numbers p by f(p) = (p + 3) mod 26. This gives IS 7722 1 1723 11 16 22107 1832013. Translating this back to letters produces the encrypted message "PHHW BRX LQ WKH SDUN." <0lIl To recover the original message fr om a secret message encrypted by the Caesar cipher, the function f-I, the inverse of f, is used. Note that the function f-I sends an integer p from {O, 1,2, ... , 25} to f-I(p) = (p -3) mod 26. In other words, to find the original message, each letter is shifted back three letters in the alphabet, with the first three letters sent to the last three letters of the alphabet. The process of determining the original message from the encrypted message is called decryption. There are various ways to generalize the Caesar cipher. For example, instead of shifting each letter by 3, we can shift each letter by k, so that f(p) = (p + k) mod 26. Such a cipher is called a shift cipher. Note that decryption can be carried out using f-I(p) = (p -k) mod 26. Obviously, Caesar's method and shift ciphers do not provide a high level of security. There are various ways to enhance this method. One approach that slightly enhances the security is to use a function of the form f(p) = (ap + b) mod 26, where a and b are integers, chosen such that f is a bijection. (Such a mapping is called an affine transformation.) This provides a number of possible encryption systems. The use of one of these systems is illustrated in Example 10. EXAMPLE 10 What letter replaces the letter K when the function f(p) = (7p + 3) mod 26 is used for en­ cryption? unkS� Exercises Solution: First, note that 10 represents K. Then, using the encryption function specified, it follows that f(lO) = (7·10 + 3) mod 26 = 21. Because 21 represents V, K is replaced by V in the encrypted message. <0lIl Caesar's encryption method, and the generalization of this method, proceed by replacing each letter of the alphabet by another letter in the alphabet. Encryption methods of this kind are vulnerable to attacks based on the frequency of occurrence of letters in the message. More sophisticated encryption methods are based on replacing blocks of letters with other blocks of letters. There are a number of techniques based on modular arithmetic for encrypting blocks of letters. A discussion of these can be found in the suggested readings listed at the end of the book. 1. Does 17 divide each of these numbers? 4. Show that part(iii) of Theorem 1 is true. a) 68 b) 84 c) 357 d) 1001 2. Show that if a is an integer other than 0, then a) 1 divides a. b) a divides O. 3. Show that part (ii) of Theorem 1 is true. 5. Show that if a I band b I a, where a and b are integers, then a = b or a = -b. 6. Show that if a, b, c, and d are integers such that a I C and bid, then ab I cd. 3-43 7. Show that if a, b, and e are integers with e =1= 0, such that ae I be, then a I b. 8. Prove or disprove that if a I be, where a, b, and e are pos­ itive integers, then a I b or a Ie. 9. What are the quotient and remainder when a) 19 is divided by 7? b) -111 is divided by II? c) 789 is divided by 23? d) 1001 is divided by 13? e) 0 is divided by 19? t) 3 is divided by 5? g) -1 is divided by 3? b) 4 is divided by I? 10. What are the quotient and remainder when a) 44 is divided by 8? b) 777 is divided by 21 ? c) -123 is divided by 19? d) -1 is divided by 23? e) -2002 is divided by 87? t) 0 is divided by 17? g) 1,234,567 is divided by 1001? b) -100 is divided by 1OI? 11. Let m be a positive integer. Show that a = b (mod m) if a mod m = b mod m. 12. Let m be a positive integer. Show that a mod m = b mod m if a = b (mod m). 13. Show that if nand k are positive integers, then r n / k 1 = L(n -1)/kJ + 1. 14. Show that if a is an integer and d is a positive inte­ ger greater than 1, then the quotient and remain­ der obtained when a is divided by dare La/dJ and a -dLa/dJ, respectively. 15. Find a formula for the integer with smallest absolute value that is congruent to an integer a modulo m, where m is a positive integer. 16. Evaluate these quantities. a) -17 mod 2 c) -101 mod 13 17. Evaluate these quantities. a) 13 mod 3 c) 155 mod 19 b) 144 mod 7 d) 199 mod 19 b) -97 mod 11 d) -221 mod 23 18. List five integers that are congruent to 4 modulo 12. 19. Decide whether each of these integers is congruent to 5 modulo 17. a) 80 c) -29 b) 103 d) -122 20. Show that if a = b (mod m) and e = d (mod m), where a, b, e, d, and m are integers with m =:': 2, then a -e = b -d (modm). 21. Show that if n I m, where nand m are positive integers greater than 1, and if a = b (mod m), where a and b are integers, then a = b (mod n). 22. Show that if a, b, e, and m are integers such that m =:': 2, e > 0, and a = b (mod m), then ae = be (mod me). 3.4 The Integers and Division 209 23. Find counterexamp les to each of these statements about congruences. a) If ae = be (mod m), where a, b, e, and m are integers with m =:': 2, then a = b (mod m). b) If a = b (mod m) and e = d (mod m), where a, b, e, d, and m are integers with e and d positive and m =:': 2, then aC = bd (mod m). 24. Prove that if n is an odd positive integer, then n2 = 1 (mod 8). 25. Show that if a, b, k, and m are integers such that k =:': 1, m =:': 2, and a = b (modm), then ak = bk(mod m) whenever k is a positive integer. 26. Which memory locations are assigned by the hashing function h(k) = k mod 101 to the records of insurance company customers with these Social Security numbers? a) 104578690 b) 432222187 c) 372201919 d) 501338753 27. A parking lot has 31 visitor spaces, numbered from 0 to 30. Visitors are assigned parking spaces using the hash­ ing function h(k) = k mod 31, where k is the number formed from the first three digits on a visitor's license plate. a) Which spaces are assigned by the hashing function to cars that have these first three digits on their license plates? 317,918,007,100,111,310 b) Describe a procedure visitors should follow to find a free parking space, when the space they are assigned is occupied. 28. What sequence of pseudorandom numbers is gener­ ated using the linear congruential generator Xn+1 = (4xn + 1)mod7withseedxo=3? 29. What sequence of pseudorandom numbers is gener­ ated using the pure multiplicative generator Xn+1 = 3xn mod 11 with seed Xo = 2? 30. Write an algorithm in pseudocode for generating a se­ quence of pseudorandom numbers using a linear congru­ ential generator. 31. Encrypt the message "DO NOT PASS GO" by translating the letters into numbers, applying the encryption func­ tion given, and then translating the numbers back into letters. a) f(p) = (p + 3) mod 26 (the Caesar cipher) b) f(p) = (p + 13) mod 26 c) f(p) = (3p + 7) mod 26 32. Decrypt these messages encrypted using the Caesar cipher. a) EOXH MHDQV b) WHVW WRGDB c) HDW GLP VXP All books are identified by an International Standard Book Number (ISBN), a lO-digit code XIX2 .•. XIO, assigned by the publisher. (This system will change in 2007 when a new 13-digit code will be introduced.) These 10 digits consist of 210 3/ The Fundament als: Algorithms, the Integers, and Matrices 3-44 blocks identifying the language, the publisher, the number as­ signed to the book by its publishing company, and finally, a I-digit check digit that is either a digit or the letter X (used to represent 10). This checkdigitis selected so that L:�l ix; == 0 (mod 11) and is used to detect errors in individual digits and transposition of digits. of the fifth edition of this book are 0-07-119881. What is the check digit for that book? 34. The ISBN of the fifth edition of Elementary Number The­ory and Its Applications is 0-32-123Q072, where Q is a digit. Find the value of Q. 35. Determine whether the check digit of the ISBN for this textbook was computed correctly by the publisher. 33. The first nine digits of the ISBN of the European version 3.5 Primes and Greatest Common Divisors DEFINITION 1 Introduction In Section 3. 4 we studied the concept of divisibility of integers. One important concept based on divisibility is that of a prime number. A prime is an integer greater than I that is divisible only by I and by itself. The study of prime numbers goes back to ancient times. Thousands of years ago it was known that there are infinitely many primes. Great mathemati cians of the last 400 years have studied primes and have proved many important results about them. However, many old conjectures about primes remain unsettled. Primes have become essential in modern cryptographic systems. An important theorem, the Fundamental Theorem of Arithmetic, asserts that every positive integer can be written uniquely as the product of prime numbers. The length oftime required to factor large integers into their prime factors plays an important role in cryptography. In this section we will also study the greatest common divisor of two integers, as well as the least common multiple of two integers. We will develop an algorithm for computing greatest common divisors in Section 3.6. Primes Every positive integer greater than I is divisible by at least two integers, because a positive integer is divisible by I and by itself. Positive integers that have exactly two different positive integer factors are called primes. A positive integer p greater than 1 is called prime if the only positive factors of p are 1 and p. A positive integer that is greater than 1 and is not prime is called composite. Remark: The integer n is composite if and only if there exists an integer a such that a I nand 1 < a < n. EXAMPLE 1 The integer 7 is prime because its only positive factors are 1 and 7, whereas the integer 9 is composite because it is divisible by 3. .... The primes less than 100 are 2,3,5, 7, 11, 13, 17, 19,23,29,31,37,41,43,47,53,59,61, 67, 71, 73, 79, 83, 89, and 97. In Section 7.6 we introduce a procedure, known as the sieve of Eratosthenes, that can be used to find all the primes not exceeding an integer n. The primes are the building blocks of positive integers, as the Fundamental Theorem of Arithmetic shows. The proof will be given in Section 4.2. 3-45 THEOREM l EXAM PLE 2 THEOREM 2 3.5 Primes and Greatest Common Divisors 211 THE FUNDAMENT AL THEOREM OF ARITHMETIC Every positive integer greater than 1 can be written uniquely as a prime or as the product of two or more primes where the prime factors are written in order of nondecreasing size. Example 2 gives some prime factorizations of integers. The prime factorizations of 100,641,999, and 1024 are given by 100 = 2·2·5·5 = 2252, 641 = 641, 999 = 3 . 3 ·3 ·37 = 33 ·37, 1024 = 2·2·2·2·2·2·2·2·2·2 = 210. It is often important to show that a given integer is prime. For instance, in cryptology, large primes are used in some methods for making messages secret. One procedure for showing that an integer is prime is based on the following observation. If n is a composite integer, then n has a prime divisor less than or equal to ..;n. Proof: If n is composite, by the definition of a composite integer, we know that it has a factor a with 1 < a < n. Hence, by the definition of a factor ofa positive integer, we have n = ab, where b is a positive integer greater than I. We will show that a :s ..;n or b :s ..;n. If a > ..;n and b > ..;n, then ab > ..;n . ..;n = n, which is a contradiction. Consequently, a :s ..;n or b :s ..;n. Because both a and b are divisors of n, we see that n has a positive divisor not exceeding ..;n. This divisor is either prime or, by the Fundamental Theorem of Arithmetic, has a prime divisor less than itself. In either case, n has a prime divisor less than or equal to ..;n. <J From Theorem 2, it follows that an integer is prime if it is not divisible by any prime less than or equal to its square root. In the following example this observation is used to show that 10 1 is prime. EXAMPLE 3 Show that 101 is prime. Solution: The only primes not exceeding -JIOf are 2,3, 5, and 7. Because 101 is not divisible by 2, 3, 5, or 7 (the quotient of 101 and each ofthese integers is not an integer), it follows that 10 1 is prime. � Because every integer has a prime factorization, it would be useful to have a procedure for finding this prime factorization. Consider the problem of finding the prime factorization of n. Begin by dividing n by successive primes, starting with the smallest prime, 2. If n has a prime factor, then by Theorem 3 a prime factor p not exceeding ..;n will be found. So, if no prime factor not exceeding ..;n is found, then n is prime. Otherwise, if a prime factor p is found, continue by factoring nj p. Note that nj p has no prime factors less than p. Again, if nj p has no prime factor greater than or equal to p and not exceeding its square root, then it is prime. Otherwise, if it has a prime factor q, continue by factoring n j (pq). This procedure is continued until the factorization has been reduced to a prime. This procedure is illustrated in Example 4. 212 3 / The Fundament als: Algorithms, the Integers, and Matrices 3-46 EXAMPLE 4 Find the prime factorization of 7007. LinkS� THEOREM 3 Solution: To find the prime factorization of 7007, first perform divisions of 7007 by successive primes, beginning with 2. None of the primes 2, 3, and 5 divides 7007. However, 7 divides 7007, with 7007/7 = 1001. Next, divide 1001 by successive primes, beginning with 7. It is immediately seen that 7 also divides 1001, because 1001/7 = 143. Continue by dividing 143 by successive primes, beginning with 7. Although 7 does not divide 143, 11 does divide 143, and 143/11 = 13. Because 13 is prime, the procedure is completed. It follows that the prime factorization of7007 is 7 . 7 . 11 . 13 = 72 . 11 . 13. .... Prime numbers were studied in ancient times for philosophical reasons. Today, there are highly practical reasons for their study. In particular, large primes pla y a crucial role in cryp­ tography, as we will see in Section 3.7. THE INFINITUDE OF PRIMES It has long been known that there are infinitely many primes. This means that whenever P I. P2 •...• Pn are the n smallest primes, we know there is a larger prime not listed. We will prove this fact using a proof given by Euclid in his famous mathematics text, The Elements. There are infinitely many primes. Proof: We will prove this theorem using a proofby contradiction. We assume that there are only finitely many primes, PI. P2 •...• Pn. Let Q = PIP2'" Pn + 1. By the Fundamental Theorem of Arithmetic, Q is prime or else it can be written as the product of two or more primes. However, none of the primes P j divides Q, for if P j I Q, then P j divides Q -PIP2 ... Pn = 1. Hence, there is a prime not in the list PI. P2 •...• Pn. This prime is either Q. if it is prime, or a prime factor of Q. This is a contradiction because we assumed that we have listed all the primes. Consequently, there are infinitely many primes. (Note that in this proof we do not state that Q is prime! Furthermore, in this proof, we have given a nonconstructive existence proof that given any n primes, there is a prime not in this list. For this proof to be constructive, we would have had to explicitly give a prime not in our original list of n primes.) <J Because there are infinitely many primes, given any positive integer there are primes greater than this integer. There is an ongoing quest to discover larger and larger prime numbers; for almost all the last 300 years, the largest prime known has been an integer of the special form 2P -I, where P is also prime. Such primes are called Mersenne primes, after the French monk Marin Mersenne, who studied them in the seventeenth century. The reason that the largest known prime has usually been a Mersenne prime is that there is an extremely efficient test, known as the Lucas-Lehmer test, for determining whether 2P -1 is prime. Furthermore, it is not currently possible to test numbers not of this or certain other special forms anywhere near as quickly to determine whether they are prime. EXAMPLE 5 The numbers 22 - 1 = 3, 23 -1 = 7, and 25 -1 = 31 are Mersenne primes, while 211 -1 = 2047 is not a Mersenne prime because 2047 = 23·89. .... 3-47 THEOREM 4 unkS� unks� 3.5 Primes and Greatest Common Divisors 213 Progress in finding Mersenne primes has been steady since computers were invented. As of early 2006, 43 Mersenne primes were known, with 12 found since 1990. The largest Mersenne prime known (again as of early 2006) is 230,402,457 -1, a number with over nine million dig­ its, which was shown to be prime in December 2005. A communal effort, the Great Internet Mersenne Prime Search (GIMPS), is devoted to the search for new Mersenne primes. By the way, even the search for Mersenne primes has practical implication s. One quality control test for supercomputers has been to replicate the Lucas-Lehmer test that establishes the primality of a large Mersenne prime. THE DISTRIBUTION OF PRIMES Theorem 3 tells us that there are infinitely many primes. However, how many primes are less than a positive number x? This question interested mathe­ maticians for many years; in the late eighteenth century, mathematicians produced large tables of prime numbers to gather evidence concerning the distribution of primes. Using this evidence, the great mathematicians of the day, including Gauss and Legendre, conjectured, but did not prove, Theorem 4. THE PRIME NUMBER THEOREM The ratio of the number of primes not exceeding x and x / In x approaches 1 as x grows without bound. (Here In x is t4e natural logarithm of x.) The Prime Number Theorem was first proved in 1896 by the French mathematician Jacques Hadamard and the Belgian mathemati cian Charles-Jean-Gu stave-Nicholas de la Vallee-Poussin using the theory of complex variables. Although proofs not using complex variables have been found, all known proofs of the Prime Number Theorem are quite complicated. We can use the Prime Number Theorem to estimate the odds that a randomly chosen number is prime. The Prime Number Theorem tells us that the number of primes not exceeding x can be approximated by x / In x. Consequently, the odds that a randomly selected positive integer less than n is prime are approximately (n/ In n )/n = 1/ Inn. Sometimes we need to find a prime with a particular number of digits. We would like an estimate of how many integers with a particular number of digits we need to select before we encounter a prime. Using the Prime Number Theorem and calculus, it can be shown that the probability that an integer n is prime is also approximately 1/ In n. For example, the odds that an integer near 101000 is prime are approximately 1/ In 101000, which is approximately 1/2300. (Of course, by choosing only odd numbers, we double our chances of finding a prime.) MARIN MERSENNE (1588-1648) Mersenne was born in Maine, France, into a family of laborers and attended the College of Mans and the Jesuit College at La Fleche. He continued his education at the Sor­ bonne, studying theology from 1609 to 1611. He joined the religious order of the Minims in 1611, a group whose name comes from the word minimi (the members of this group were extremely humble; they consid­ ered themselves the least of a}1 religious orders). Besides prayer, the members of this group devoted their energy to scholarship and study. In 1612 he became a priest at the Place Royale in Paris; between 1614 and 1618 he taught philosophy at the Minim Convent at Nevers. He returned to Paris in 1619, where his cell in the Minims de l' Annociade became a place for meetings of French sCientists, philosophers, and mathe­ maticians, including Fermat and Pascal. Mersenne corresponded extensively with scholars throughout Europe, serving as a clearinghouse for mathematical and scientific knowledge, a function later served by mathematical journals (and today also by the Internet). Mersenne wrote books covering mechanics, mathematical physics, mathemati cs, music, and acoustics. He studied prime numbers and tried unsuccessfully to construct a formula representing all primes. In 1644 Mersenne claimed that 2P - 1 is prime for p = 2, 3, 5, 7, 13, 17, 19,31,67, 127,257 but is composite for all other primes less than 257. It took over 300 years to determine that Mersenne's claim was wrong five times. Specificall y, 2P - 1 is not prime for p = 67 and p = 257 but is prime for p = 61, P = 87, and p = 107. It is also noteworthy that Mersenne defended two of the most famous men of his time, Descartes and Galileo, from religious critics. He also helped expose alchemists and astrologers as frauds. 214 31 The Fundament als: Algorithms, the Integers, and Matrices 3-48 EXAMPLE 6 Extra� Examples IIiiiiI1 EXAMPLE 7 linkS� U sing trial division with Theorem 2 gives procedures for factoring and for primality testing. However, these procedures are not efficient algorithms; many much more practical and efficient algorithms for these tasks have been developed. Factoring and primality testing have become important in the applications of number theory to cryptography. This has led to a great interest in developing efficient algorithms for both tasks. Clever procedures have been devised in the last 30 years for efficiently generating large primes. However, even though powerful new factor­ ization methods have been developed in the same time frame, factoring large numbers remains extraordinarily more time consuming. Nevertheless, the challenge of factoring large numbers interests many people. There is a communal effort on the Internet to factor large numbers, especially those of the special form kn ± 1, where k is a small positive integer and n is a large positive integer (such numbers are called Cunningham numbers). At any given time, there is a list of the "Ten Most Wanted" large numbers of this type awaiting factorization. Conjectures and Open Problems About Primes Number theory is noted as a subject for which it is easy to formulate conjectures, some of which are difficult to prove and others that have remained open problems for many years. We will describe some conjectures in number theory and discuss their status in Examples 6-9. It would be useful to have a function f(n) such that f(n) is prime for all positive integers n. If we had such a function, we could find large primes for use in cryptography and other applications. Looking for such a function, we might check out different polynomial functions, as some mathemati cians did several hundred years ago. After a lot of computation we may encounter the polynomial f(n) = n2 - n + 41. This polynomial has the interesting property that f(n) is prime for all positive integers n not exceeding 40. [We have f(l) = 41, f(2) = 43, f(3) = 47, f(4) = 53, and so on.] This can lead us to the conjecture that f(n) is prime for all positive integers n. Can we settle this conjecture? Solution: Perhaps not surprisingly, this conjecture turns out to be false; we do not have to look far to find a positive integer n for which f(n) is composite, because f( 41) = 412 -41 + 41 = 412. Because f(n) = n2 - n + 41 is prime for all positive integers n with 1 ::: n ::: 40, we might be tempted to find a different polynomial with the property that f(n) is prime for all positive integers n. However, there is no such polynomial. It can be shown that for every polynomial f(n) with integer coefficients, there is a positive integer y such that f(y) is composite. (See Exercise 37 in the Supplementary Exercises at the end of the chapter.) ... Many famous problems about primes still await ultimate resolution by clever people. We describe a few of the most accessible and better known of these open problems in Examples 7-9. Number theory is noted for its wealth of easy-to-understand conjectures that resist attack by all but the most sophisticated techiques, or simply resist all attacks. We present these conjectures to show that many questions that seem relatively simple remain unsettled even in the twenty-first century. Goldbach 's Conjecture In 1742, Christian Goldbach, in a letter to Leonhard Euler, conjec­ tured that every odd integer n, n > 5, is the sum of three primes. Euler replied that this conjecture is equivalent to the conjecture that every even integer n, n > 2, is the sum of two primes (see Exercise 39 in the Supplemen tary Exercises at the end of the chapter). The conjecture that every even integer n, n > 2, is the sum of two primes is now called Goldbach 's conjecture. We can check this conjecture for small even numbers. For example, 4 = 2 + 2, 6 = 3 + 3, 8 = 5 + 3, 10 = 7 + 3, 12 = 7 + 5, and so on. Goldbach's conjecture was verified by hand calculations for numbers up to the millions prior to the advent of computers. With computers it can be checked for extremely large numbers. As of early 2006, the conjecture has been checked for all positive even integers up to 2 . 10 17 • 3-49 EXAMPLE S linkS� EXAM PLE 9 unkS� DEFINITION 2 3.5 Primes and Greatest Common Divisors 215 Although no proof of Goldbach's conjecture has been found, most mathematicians believe it is true. Several theorems have been proved, using complicated methods from analytic number theory far beyond the scope of this book, establishing results weaker than Goldbach's conjecture. Among these are the result that every even positive integer greater than 2 is the sum of at most six primes (proved in 1995 by O. Raman!) and that every sufficiently large positive integer is the sum of a prime and a number that is either prime or the product of two primes (proved in 1966 by 1. R. Chen). Perhaps Goldbach's conjecture will be settled in the not too distant future. � There are many conjectures asserting that there are infinitely many primes of certain special forms. A conjecture of this sort is the conjecture that there are infinitely many primes of the form n2 + 1, where n is a positive integer. For example, 5 = 22 + 1, 17 =42 + 1,37 = 62 + 1, and so on. The best result currently known is that there are infinitely many positive integers n such that n2 + 1 is prime or the product of at most two primes (proved by Henryk Iwaniec in 1973 using advanced techniques from analytic number theory, far beyond the scope of this book). � The Twin Prime Conjecture Twin primes are primes that differ by 2, such as 3 and 5, 5 and 7, 11 and 13, 17 and 19, and 4967 and 4969. The twin prime conjecture asserts that there are infinitely many twin primes. The strongest result proved concerning twin primes is that there are infinitely many pairs p and p + 2, where p is prime and p + 2 is prime or the product of two primes (proved by 1. R. Chen in 1966). The world's record for twin primes, as of early 2006, consists of the numbers 16,869,987,339,975 .2171,960 ± 1 numbers with 51,779 digits. � Greatest Common Divisors and Least Common Multiples The largest integer that divides both of two integers is called the greatest common divisor of these integers. . Let a and b be integers, not both zero. The largest integer d such that d I a and d I b is called the greatest common divisor of a and b. The greatest common divisor of a and b is denoted by gcd(a, b). The greatest common divisor of two integers, not both zero, exists because the set of common divisors of these integers is finite. One way to find the greatest common divisor of two integers is to find all the positive common divisors of both integers and then take the largest divisor. This is done in Examples 10 and 11. Later, a more efficient method of finding greatest common divisors will be given. EXAMPLE 10 What is the greatest common divisor of24 and 36? unkS� Solution: The positive common divisors of 24 and 36 are 1, 2, 3, 4, 6, and 12. Hence, gcd(24, 36) = 12. � CHRISTIAN GOLDB ACH (1690-1764) Christian Goldbach was born in Konigsberg, Prussia, the city noted for its famous bridge problem (which will be studied in Section 9.5). He became professor of mathematics at the Academy in St. Petersburg in 1725. In 1728 Goldbach went to Moscow to tutor the son ofthe Tsar. He entered the world of politics when, in 1742, he became a staff member in the Russian Ministry of Foreign Aff airs. Goldbach is best known for his correspondence with eminent mathema ticians, including Euler and Bernoulli, for his famous conjectures in number theory, and for several contributions to analysis. 216 3 / The Fundament als: Algorithms, the Integers, and Matrices EXAMPLE 11 What is the greatest common divisor of 17 and 22? 3-50 Solution: The integers 17 and 22 have no positive common divisors other than 1, so that gcd(17, 22) = 1. ... Because it is often important to specify that two integers have no common positive divisor other than 1, we have Definition 3. DEFINITION 3 The integers a and b are relatively prime iftheir greatest common divisor is 1. EXAMPLE 12 By Example 11 it follows that the integers 17 and 22 are relatively prime, because gcd(17, 22) = 1. ... Because we often need to specify that no two integers in a set of integers have a common positive divisor greater than 1, we make Definition 4. DEFINITION 4 The integers ai, a2, ... , an are pairwise relatively prime if gcd(ai> aj) = 1 whenever 1 � i < j � n. EXAM PLE 13 Determine whether the integers 10, 17, and 21 are pairwise relatively prime and whether the integers 10, 19, and 24 are pairwise relatively prime. Solution: Because gcd(10, 17) = 1, gcd(10, 21) = 1, and gcd(17, 21) = 1, we conclude that 10, 17, and 21 are pairwise relatively prime. Because gcd(10, 24) = 2 > 1, we see that 10, 19, and 24 are not pairwise relatively prime . ... Another way to find the greatest common divisor of two integers is to use the prime factor­ izations of these integers. Suppose that the prime factorizations of the integers a and b, neither equal to zero, are . a, a2 an b b, b2 bn a = PI P2 ... Pn, = PI P2 ... Pn ' where each exponent is a nonnegative integer, and where all primes occurring in the prime factorization of either a or b are included in both factorizations, with zero exponents if necessary. Then gcd(a, b) is given by d( b) -min(a"b,) min(a2,b2) min(an,bn) gc a, -PI P2 '''Pn ' where min(x, y) represents the minimum of the two numbers x and y. To show that this formula for gcd(a, b) is valid, we must show that the integer on the right-hand side divides both a and b, and that no larger integer also does. This integer does divide both a and b, because the power of each prime in the factorization does not exceed the power of this prime in either the factorization of a or that of b. Further, no larger integer can divide both a and b, because the exponents of the primes in this factorization cannot be increased, and no other primes can be included. EXAMPLE 14 Because the prime factorizations of 120 and 500 are 120 = 23 . 3 . 5 and 500 = 22 . 53, the greatest common divisor is Prime factorizations can also be used to find the least common multiple of two integers. 3-5/ 3.5 Primes and Greatest Common Divisors 217 DEFINITION 5 The least common multiple of the positive integers a and b is the smallest positive integer that is divisible by both a and b. The least common multiple of a and b is denoted by lcm(a, b). The least common multiple exists because the set of integers divisible by both a and b is nonempty, and every nonempty set of positive integers has a least element (by the well-ordering property, which will be discussed in Section 4.2). Suppose that the prime factorizations of a and b are as before. Then the least common multiple of a and b is given by I ( b) max(a\, btl max(a2, b2) max(an, bn) cm a, = PI P2 ... Pn where max(x , y) denotes the maximum ofthe two numbers x and y. This formula is valid because a common multiple of a and b has at least max(ai, bi) factors of Pi in its prime factorization, and the least common multiple has no other prime factors besides those in a and b. EXAMPLE 15 What is the least common multiple of233572 and 2433? Solution: We have Theorem 5 gives the relationship between the greatest common divisor and least common multiple of two integers. It can be proved using the formulae we have derived for these quantities. The proof of this theorem is left as an exercise for the reader. THEO REM 5 Exercises Let a and b be positive integers. Then ab = gcd(a, b) ·lcm(a, b). 1. Determine whether each of these integers is prime. a) 21 b) 29 c) 71 d) 97 e) 111 1) 143 2. Determine whether each of these integers is prime. a) 19 b) 27 c) 93 d) 101 e) 107 1) 113 3. Find the prime factorization of each of these integers. a) 88 b) 126 c) 729 d) 1001 e) 1111 1) 909,090 4. Find the prime factorization of each of these integers. a) 39 b) 81 c) 101 d) 143 e) 289 1) 899 5. Find the prime factorization of 1O!. *6. How many zeros are there at the end of 100!? *7. Show that log2 3 is an irrational number. Recall that an ir­ rational number is a real number x that cannot be written as the ratio of two integers. 8. Prove that for every positive integer n, there are n consecu­ tive composite integers. [Hint: Consider the n consecutive integers starting with (n + I)! + 2.] *9. Prove or disprove that there are three consecutive odd positive integers that are primes, that is, odd primes ofthe form p, p + 2, and p + 4. 10. Which positive integers less than 12 are relatively prime to 12? 11. Which positive integers less than 30 are relatively prime to 30? 12. Determine whether the integers in each of these sets are pairwise relatively prime. a) 21,34,55 b) 14,17,85 c) 25,41,49,64 d) 17,18,19,23 218 3/ The Fundamental s: Algorithms, the Integers, and Matrices 13. Determine whether the integers in each of these sets are pairwise relatively prime. a) 11, 15, 19 b) 14, 15,21 c) 12,17,31,37 d) 7,8,9,11 14. We call a positive integer perfect if it equals the sum of its positive divisors other than itself. a) Show that 6 and 28 are perfect. b) Show that 2P-I(2P -1) is a perfect number when 2P - 1 is prime. 15. Show that if 2n - 1 is prime, then n is prime. [Hint: Usetheidentity2ab - 1 = (2a -1). (2a(b-l) + 2a(b-2) + ... + 2a + 1).] 16. Determine whether each of these integers is prime, veri­ fYing some of Mersenne's claims. a) 27 - I b) 29 - 1 c) 211 - I d) 213 - 1 The value of the Euler q,-function at the positive integer n is defined to be the number of positive integers less than or equal to n that are relatively prime to n. (Note: ¢ is the Greek letter phi.) 17. Find a) ¢(4). b) ¢(IO). c) ¢(13). 18. Show that n is prime if and only if ¢(n) = n -I. 19. What is the value of ¢(pk) when P is prime and k is a positive integer? 20. What are the greatest common divisors of these pairs of integers? a) 22 . 33 . 55, 25 . 33 . 52 b) 2·3 · 5 ·7· 11 . 13, 211 .39 • 11 . 1714 c) 17, 1717 d) 22.7, 53·13 e) 0, 5 1) 2·3·5·7 , 2·3 . 5 ·7 21. What are the greatest common divisors of these pairs of integers? a) 37 ·53 .73, 211 .35.59 b) 11· 13 . 17, 29 . 37 . 55 .73 c) 2331, 2317 d) 41 ·43·53, 41 ·43·53 e) 313.517, 212 .721 1) 1111, 0 22. What is the least common multiple of each pair in Exercise 20? 23. What is the least common multiple of each pair in Exercise 21? 24. Find gcd(lOOO, 625) and lcm(1000, 625) and verifY that gcd(1000, 625)· lcm(1000, 625) = 1000 ·625. 25. Find gcd( 92928, 123552) and lcm(92928, 123552), and verifY that gcd(92928, 123552) . lcm(92928, 123552) = 92928 . 123552. [Hint: First find the prime factorizations of92928 and 123552.] 26. lEthe product of two integers is 273852711 and their great­ est common divisor is 23345, what is their least common multiple? 27. Show that if a and b are positive integers, then ab = gcd(a, b) ·lcm(a, b). [Hint: Use the prime factorizations 3-52 of a and b and the formulae for gcd(a, b) and lcm(a, b) in terms of these factorizations.] 28. Find the smallest positive integer with exactly n different factors when n is a) 3. d) 6. b) 4. e) 10. c) 5. 29. Can you find a formula or rule for the nth term of a se­ quence related to the prime numbers or prime factoriza­ tions so that the initial terms of the sequence have these values? a) 0, 1, 1, 0, 1,0, 1, 0, 0, 0, 1,0, 1, ... b) 1,2, 3,2,5,2,7,2,3,2,11, 2,13,2, .. . c) 1, 2, 2,3,2, 4, 2,4,3,4, 2,6, 2,4, .. . d) 1, 1, 1, 0, 1, 1, 1, 0, 0, I, 1,0, 1, 1, .. . e) 1,2,3,3,5, 5, 7,7,7,7,11, II, 13, 13, ... 1) 1,2,6,30,210,2310,30030,510510,9699690, 223092870, ... 30. Can you find a formula or rule for the nth term of a se­ quence related to the prime numbers or prime factoriza­ tions so that the initial terms of the sequence have these values? a) 2,2,3,5, 5, 7, 7,11,11, ll, ll, 13, 13, ... b) 0,1,2,2,3, 3,4,4,4,4,5,5,6,6, .. . c) 1, 0, 0,1,0,1,0, I, I, 1,0, 1,0, I, .. . d) 1, -I, -1,0, -1,1, -1,0,0,1, -1,0, -I, I, 1, ... e) I, I, 1, 1, 1, 0, 1, 1, 1, 0, I, 0, 1, 0, 0, ... 1) 4,9,25,49,121,169,289,361,529,841,961, 1369, ... 31. Prove that the product of any three consecutive integers is divisible by 6. 32. Show that if a, b, and m are integers such that m :::: 2 and a == b (mod m), then gcd(a, m) = gcd(b, m). *33. Prove or disprove that n2 -79n + 1601 is prime when­ ever n is a positive integer. 34. Prove or disprove that PIP2 ... Pn + I is prime for every positive integer n, where PI, P2, ... , Pn are the n smallest prime numbers. 35. Adapt the proof in the text that there are infinitely many primes to prove that there are infinitely many primes of the form 4k + 3, where k is a nonnegative integer. [Hint: Suppose that there are only finitely many such primes ql, q2,···, qn,andconsiderthe number4q lq2" ·qn -I.] *36. Prove that the set of positive rational numbers is countable by setting up a function that assigns to a rational number P / q with gcd(p, q) = 1 the base II number formed from the decimal representation of P followed by the base II digit A, which corresponds to the decimal number 10, followed by the decimal representation of q . *37. Prove that the set of positive rational numbers is countable by showing that the function K is a one­ to-one correspondence between the set of positive rational numbers and the set of positive integers 'f K( / ). 2al 2a2 2a, 2bl-1 2b2-1 q2b,-1 I m n = PI P2 •.. Ps ql q2 • .. t , where gcd(m, n) = I and the prime-power factorizations f d al a2 a, d bl b2 b, o man n are m = PI P2 .•• Ps an n = ql q2 ... qt . 3-53 3.6 Integers and Algorithms 219 3.6 Integers and Algorithms THEOREM l Introduction As mentioned in Section 3.1, the term algorithm originally referred to procedures for performing arithmetic operations using the decimal representations of integers. These algorithms, adapted for use with binary representa tions, are the basis for computer arithmetic. They provide good illustrations of the concept of an algorithm and the complexity of algorithms. For these reasons, they will be discussed in this section. There are many important algorithms involving integers besides those used in arithmetic, including the Euclidean algorithm, which is one of the most useful algorithms, and perhaps the oldest algorithm, in mathema tics. We will also describe an algorithm for finding the base b expansion of a positive integer for any base b and for modular exponentiation, an algorithm important in cryptography. Representati ons of Integers In everyday life we use decimal notation to express integers. For example, 965 is used to denote 9· 102 + 6· 10 + 5. However, it is often convenient to use bases other than 10. In particular, computers usually use binary notation (with 2 as the base) when carrying out arithmetic, and octal (base 8) or hexadecimal (base 16) notation when expre ssing characters, such as letters or digits. In fact, we can use any positive integer greater than I as the base when expressing integers. This is stated in Theorem 1. Let b be a positive integer greater than I. Then if n is a positive integer, it can be expressed uniquely in the form n = akbk + ak_lbk-' + ... + alb + ao, where k is a nonnegative integer, ao, a" ... ,ak are nonnegative integers less than b, and ak::j:. o. A proof of this theorem can be constructed using mathematical induction, a proof method that is discussed in Section 4.1. It can also be found in [Ro05]. The representation of n given in Theorem I is called the base b expansion of n. The base b expansion of n is denoted by (akak-' ... a,aoh. For instance, (245)8 represents 2.82 + 4·8 + 5 = 165. BINARY EXPANSIONS Choosing 2 as the base gives binary expansions of integers. In binary notation each digit is either a 0 or a 1. In other words, the binary expansion of an integer is just a bit string. Binary expansions (and related expansions that are variants of binary expansions) are used by computers to represent and do arithmetic with integers. EXAM PLE 1 What is the decimal expansion of the integer that has (l 0101 1111)2 as its binary expansion? Solution: We have (l0101111lh=I.28+ 0.27+1.26+ 0.25+1.24 + 1 . 23 + 1 . 22 + 1 . 2' + 1 . 20 = 351. HEXADECIMAL EXPANSIONS Sixteen is another base used in computer science. The base 16 expansion of an integer is called its hexadecimal expansion. Sixteen different digits are 220 3 / The Fundamental s: Algorithms, the Integers, and Matrices 3-54 required for such expansions. Usually, the hexadecimal digits used are 0, 1,2, 3,4, 5, 6, 7, 8, 9, A, B, C, D, E, and F, where the letters A through F represent the digits corresponding to the numbers 10 through 15 (in qecimal notation). EXAMPLE 2 What is the decimal expansion of the hexadecimal expansion of (2AEOB)'6 ? EXAMPLE 3 Extra� ExaDlples � Solution: We have (2AEOB)'6 = 2.164 + 10.163 + 14.162 + 0·16 + 11 = (175627)10. Each hexadecimal digit can be represented using four bits. For instance, we see that (1110 0101)2 = (E5)'6 because (1110h = (E)'6 and (0101h = (5)'6. Bytes, which are bit strings of length eight, can be represented by two hexadecimal digits. BASE CONVERSION We will now describe an algorithm for constructing the base b expan­ sion of an integer n. First, divide n by b to obtain a quotient and remainder, that is, n = bqo +ao, 0::; ao < b. The remainder, ao, is the rightmost digit in the base b expansion of n. Next, divide qo by b to obtain We see that a, is the second digit from the right in the base b expansion of n. Continue this process, successively dividing the quotients by b, obtaining additional base b digits as the remainders. This process terminates when we obtain a quotient equal to zero. Find the base 8, or octal, expansion of (12345)10. Solution: First, divide l2345 by 8 to obtain 12345 = 8 . 1543 + 1. Successively dividing quotients by 8 gives 1543 = 8 . 192 + 7, 192=8·24+0, 24 = 8·3 + 0, 3 = 8·0 + 3. Because the remainders are the digits of the base 8 expansion of 12345, it follows that (12345)10 = (30071)8. EXAMPLE 4 Find the hexadecimal expansion of (177130)10. Solution: First divide 177130 by 16 to obtain 177130 = 16·11070 + 10. 3-55 Successively dividing quotients by 16 gives 11070 = 16·691 + 14, 691 = 16·43 +3, 43=16·2+11, 2 = 16·0 +2. 3.6 Integers and Algorithms 221 Because the remainders are the digits of the hexadecimal (base 16) expansion of (177130)10, it follows that (177130)10 = (2B3EA)16. (Recall that the integers 10, 11, and 14 correspond to the hexadecimal digits A, B, and E, respectively.) .... EXAMPLE 5 Find the binary expansion of (241 )10. Solution: First divide 241 by 2 to obtain 241 = 2 . 120 + 1. Successively dividing quotients by 2 gives 120 = 2 . 60 + 0, 60 = 2·30 + 0, 30 = 2·15 + 0, 15=2·7+ 1, 7=2·3+1, 3=2·1+1, 1=2·0+1. Because the remainders are the digits of the binary (base 2) expansion of(241)1O, it follows that (241)10 = (1111 0001)2. The pseudocode given in Algorithm 1 finds the base b expansion (ak-l ... alaO)b of the integer n. ALGORITHM 1 Constru cting Base b Expansions. procedure base b expansion(n: positive integer) q:= n k:=O while q =1= 0 begin ak:= q mod b q:=Lq/bJ k:= k+ 1 end {the base b expansion ofn is (ak-l ... alao)b} 222 3 / The Fundament als: Algorithms, the Integers, and Matrices 3-56 TABLE 1 Hexadecimal, Octal, and Binary Representation of the Integers 0 through 15. Decimal 0 1 Hexadecimal 0 1 Octal 0 1 Binary 0 1 2 3 4 5 6 7 8 9 10 II 12 13 14 15 2 3 4 5 6 7 8 9 A B C D E F 2 3 4 5 6 7 10 11 12 13 14 15 16 17 10 11 100 101 110 III 1000 1001 1010 1011 1100 1101 1110 1111 In Algorithm 1, q represents the quotient obtained by successive divisions by b, starting with q = n. The digits in the base b expansion are the remainders of these divisions and are given by q mod b. The algorithm terminates when a quotient q = 0 is reached. Remark: Note that Algorithm 1 can be thought of as a greedy algorithm. Conversion between binary and hexadecimal expansions is extremely easy because each hexadecimal digit corresponds to a block of four binary digits, with this correspondence shown in Table 1 without initial Os shown. (We leave it as Exercises 11 and 12 at the end ofthis section to show that this is the case.) This conversion is illustrated in Example 6. EXAMPLE 6 Find the hexadecimal expansion of (11 1110 1011 1100)z and the binary expansion of(A8D)16. Solution: To convert (11 1110 1011 1100h into hexadecimal notation we group the binary digits into blocks of four, adding initial zeros at the start of the leftmost block if necessary. These blocks are 0011, 1110, 10 11, and 1100, which correspond to the hexadecimal digits 3, E, B, and C, respectively. Consequently, (11 11101011 1100)2 = (3EBC)16' To convert (A8D)16 into binary notation, we replace each hexadecimal digit by a block of four binary digits. These blocks are 1010, 1000, 1101. Consequently, (A8D)16 = (1010 1000 1101)z. .... Algorithms for Integer Operations The algorithms for performing operations with integers using their binary expansions are ex­ tremely important in computer arithmetic. We will describe algorithms for the addition and the multiplication of two integers expressed in binary notation. We will also analyze the compu­ tational complexity of these algorithms, in terms of the actual number of bit operations used. Throughout this discussion, suppose that the binary expansions of a and bare a = (an-Ian-2 ... alaO)2, b = (bn-Ibn-2 ... blbo)2, so that a and b each have n bits (putting bits equal to 0 at the beginning of one of these expansions if necessary). We will measure the complexity of algorithms for integer arithmetic in terms of the number of bits in these numbers. Consider the problem of adding two integers in binary notation. A procedure to perform addition can be based on the usual method for adding numbers with pencil and paper. This method proceeds by adding pairs of binary digits together with carries, when they occur, to compute the sum of two integers. This procedure will now be specified in detail. To add a and b, first add their rightmost bits. This gives . ao + bo = Co . 2 + So, where So is the rightmost bit in the binary expansion of a + b and Co is the carry, which is either o or 1. Then add the next pair of bits and the carry, al + bl + Co = CI . 2 + Sl, 3-57 3.6 Integers and Algorithms 223 where SI is the next bit (from the right) in the binary expansion of a + b, and CI is the carry. Continue this process, adding the corresponding bits in the two binary expansions and the carry, to determine the next bit from the right in the binary expansion of a + b. At the last stage, add an-I, bn-I, and Cn-2 to obtain Cn-I ·2 + Sn-I. The leading bit of the sum is Sn = Cn-I. This procedure produces the binary expansion of the sum, namely, a + b = (SnSn-ISn-2 .•• SISO)z. EXAMPLE 7 Add a = (1110h and b = (1011)2. 111 1 1 1 0 101 1 I 1 00 I Solution: Following the procedure specified in the algorithm, first note that ao + bo = 0 + 1 = 0 . 2 + 1, so that Co = 0 and So = 1. Then, because al + bl + Co = 1 + 1 + 0 = 1 . 2 + 0, it follows that CI = 1 and SI = O. Continuing, a2 + b2 + CI = 1 + 0 + 1 = 1 . 2 + 0, so that C2 = 1 and S2 = O. Finally, because a3 + h3 + C2 = 1 + 1 + 1 = I . 2 + I, FIGURE 1 Adding (lllOh and (lOllh-it follows that C3 = 1 and S3 = 1. This means that S4 = C3 = 1. Therefore, S = a + b = (1 1001 h. This addition is displayed in Figure l. .... The algorithm for addition can be described using pseudocode as follows. ALGORITHM 2 Addition of Integers. procedure add(a, b: positive integers) {the binary expansions of a and b are (an-Ian-2 ... alaO)2 and (bn-Ibn-2 ••• blbo)2, respectively} c:= 0 for j := 0 to n -1 begin end d := L(aj + bj + c)/2J Sj := aj + bj + C -2d c :=d Sn := c {the binary expansion of the sum is (SnSn-l ., . SO)2} Next, the number of additions of bits used by Algorithm 2 will be analyzed. EXAMPLE 8 How many additions of bits are required to use Algorithm 2 to add two integers with n bits (or less) in their binary representa tions? 224 3 / The Fundamental s: Algorithms, the Integers, and Matrices 3-58 Solution: Two integers are added by successively adding pairs of bits and, when it occurs, a carry. Adding each pair of bits and the carry requires three or fewer additions of bits. Thus, the total number of additions of bits used is less than three times the number of bits in the expansion. Hence, the number of additions of bits used by Algorithm 2 to add two n-bit integers is O(n). .... Next, consider the multiplication of two n-bit integers a and b. The conventional algorithm (used when multiplying with pencil and paper) works as follows. Using the distributive law, we see that ab = a(b02° + bl21 + ... + bn_12n-l) = a(b020) + a(h21) + ... + a(bn_12n-I). We can compute ab using this equation. We first note that abj = a if bj = I and abj = 0 if b j = O. Each time we multiply a term by 2, we shift its binary expansion one place to the left and add a zero at the tail end of the expansion. Consequently, we can obtain (abj)2j by shifting the binary expansion of abj j places to the left, adding j zero bits at the tail end of this binary expansion. Finally, we obtain ab by adding the n integers ab j 2j , j = 0, I, 2, ... , n-l. This procedure for multiplication can be described using the pseudocode shown as Algorithm 3. ALGOR ITHM 3 Multiplying Integers. procedure multiply(a, b: positive integers) {the binary expansions of a and bare (an-lan-2 ... alao)2 and (bn-1bn-2 ••• b1boh. respectively} for j := 0 to n -1 begin end if bj = 1 then Cj := a shifted j places else Cj := 0 {co, Cl, ... , Cn-l are the partial products } p :=0 for j := 0 to n -1 p:= p +Cj {p is the value of ab} Example 9 illustrates the use of this algorithm. EXAMPLE 9 Find the product of a = (l1O)z and b = (l01)z. Solution: First note that abo· 2° = (11O)z . I .2° = (11O)z, ab1 .21 = (l1O)z . 0.21 = (0000)z, 3-59 1 1 0 101 1 1 0 000 1 1 0 1 1 110 FIGURE 2 Multiplying (llOh and (lOlh- EXAMPLE 10 and 3.6 Integers and Algorithms 225 To find the product, add (110)2, (OOOOh, and (11000h. Carrying out these additions (using Algorithm 2, including initial zero bits when necessa ry) shows that ab = (l IIlOh. This mul­ tiplication is displayed in Figure 2. .... Next, we determine the number of additions of bits and shifts of bits used by Algorithm 3 to multiply two integers. How many additions of bits and shifts of bits are used to multiply a and b using Algorithm 3? Solution: Algorithm 3 computes the products of a and b by adding the partial products Co, C\, C2, ... , and Cn-I. When bj = 1, we compute the partial product Cj by shifting the binary expansion of aj bits. When bj = 0, no shifts are required because Cj = O. Hence, to find all n of the integers ab j 2j , j = 0, 1, ... , n -1, requires at most 0+I+2+···+n-I shifts. Hence, by Example 5 in Section 3.2 the number of shifts required is O(n2). To add the integers ab j from j = 0 to j = n - 1 requires the addition of an n-bit integer, an (n + I)-bit integer, ... , and a (2n)-bit integer. We know from Example 8 that each ofthese additions requires O(n) additions of bits. Consequently, a total of O(n2) additions of bits are required for all n additions. .... ALGOR ITHM 4 Comput ing div and mod. procedure division algorithm(a: integer, d: positive integer) q:= 0 r:= lal whiler � d begin end r:= r -d q := q + 1 if a < 0 and r > 0 then begin r:= d-r q := -(q + 1) end {q = a div d is the quotient , r = a mod d is the remainder} Surprisingly, there are more efficient algorithms than the conventional algorithm for mul­ tiplying integers. One such algorithm, which uses O(n 1.585) bit operations to multiply n-bit numbers, will be described in Section 7.3. Given integers a and d, d > 0, we can find q = a div d and r = a mod d using Algorithm 4. In this algorithm, when a is positive we subtract d from a as many times as necessary until what 226 3 / The Fundament als; Algoritluns, the Integers, and Matrices 3-60 is left is less than d. The number oftimes we perform this subtraction is the quotient and what is left over after all these subtractions is the remainder. Algorithm 4 also covers the case where a is negative. This algorithm finds the quotient q and remainder r when la I is divided by d. Then, when a < 0 and r > 0, it uses these to find the quotient -(q + 1) and remainder d -r when a is divided by d. We leave it to the reader (Exercise 57) to show that, assuming that a > d, this algorithm uses 0 (q log a) bit operations. There are more efficient algorithms than Algorithm 4 for determining the quotient q = a div d and the remainder r = a mod d when a positive integer a is divided by a positive integer d (see [Kn98] for details). These algorithms require 0 (log a . log d) bit operations. If both of the binary expansions of a and d contain n or fewer bits, then we can replace log a . log d by n2. This means that we need O(n2) bit operations to find the quotient and remainder when a is divided by d. Modular Exponentiation In cryptography it is important to be able to find bn mod m efficiently, where b, n, and m are large integers. It is impractical to first compute bn and then find its remainder when divided by m because bn will be a huge number. Instead, we can use an algorithm that employs the binary expansion of the exponent n, say n = (ak-I ... alao)z. Before we present this algorithm, we illustrate its basic idea. We will explain how to use the binary expansion of n to compute bn. First, note that To compute bn, we find the values of b, b2, (b2)2 = b4, (b4)2 = b8, ••• , b2k. Next, we multiply the terms b2j in this list, where a j = 1. This gives us bn• For example, to compute 311 we first note that 11 = (1011)2, so that 311 = 383231• By successively squaring, we find that 32 = 9, 34 = 92 = 81, and 38 = (81)2 = 6561. Consequently, 311 = 383231 = 6561 ·9·3 = 177,147. The algorithm successively finds b mod m, b2 mod m, b4 mod m, ... , b2k-' mod m and multiplies together those terms b2j mod m where a j = 1, finding the remainder of the product when divided by m after each multiplication. Pseudocode for this algorithm is shown in Al­ gorithm 5. Note that in Algorithm 5 we use the most efficient algorithm available to compute values of the mod function, not Algorithm 4. ALGORITHM 5 Modular Exponentiation. procedure modular exponentiation(b; integer, n = (ak-]ak-2 ... a]aoh, m; positive integers) x;= 1 power ;= b mod m for i ;= 0 to k -1 begin end if ai = 1 then x ;= (x· power) mod m power ;= (power· power) mod m {x equals bn mod m} We illustrate how Algorithm 5 works in Example 11. 3-61 3.6 Integers and Algorithms 227 EXAMPLE 11 Use Algorithm 5 to find 3644 mod 645. Solution: Algorithm 5 initially sets x = 1 and power = 3 mod 645 = 3. In the computation of 3644 mod 645, this algorithm determines 32i mod 645 for j = 1,2, ... , 9 by successively squaring and reducing modulo 645. If aj = 1 (where aj is the bit in the jth position in the binary expansion of 644), it multiplies the current value of x by 32i mod 645 and reduces the result modulo 645. Here are the steps used: i = 0: Because ao = 0, we have x = 1 and power = 32 mod 645 = 9 mod 645 = 9; i = 1: Because a) = 0, we have x = 1 and power = 92 mod 645 = 81 mod 645 = 81; i = 2: Because a2 = 1, we have x = 1 ·81 mod 645 = 81 and power = 812 mod 645 = 6561 mod 645 = 111; i = 3: Because a3 = 0, we have x = 81 and power = 1112 mod 645 = 12,321 mod 645 = 66; i = 4: Because a4 = 0, we have x = 81 and power = 662 mod 645 = 4356 mod 645 = 486; i = 5: Because a5 = 0, we have x = 81 and power = 4862 mod 645 = 236,196 mod 645 = 126; i = 6: Because a6 = 0, we have x = 81 and power = 1262 mod 645 = 15,876 mod 645 = 396; i = 7: Because a7 = 1, we find that x = (81 ·396) mod 645 = 471 and power = 3962 mod 645 = 156,816 mod 645 = 81; i = 8: Because ag = 0, we have x = 471 and power = 812 mod 645 = 6561 mod 645 = 111; i = 9: Because a9 = 1, we find that x = (471 . 111) mod 645 = 36. unkS� This shows that following the steps of Algorithm 5 produces the result 3644 mod 645 = 36. .... Algorithm 5 is quite efficient; it uses 0 ((log m )2 log n) bit operations to find bn mod m (see Exercise 56). The Euclidean Algorithm The method described in Section 3.5 for computing the greatest common divisor of two integers, using the prime factorizations of these integers, is inefficient. The reason is that it is time­ consuming to find prime factorizations. We will give a more efficient method of finding the greatest common divisor, called the Euclidean algorithm. This algorithm has been known since ancient times. It is named after the ancient Greek mathematician Euclid, who included a description of this algorithm in his book The Elements. Before describing the Euclidean algorithm, we will show how it is used to find gcd(91, 287). First, divide 287, the larger of the two integers, by 91, the smaller, to obtain 287 = 91 . 3 + 14. Any divisor of91 and 287 must also be a divisor of287 -91 ·3= 14. Also, any divisor of91 and 14 must also be a divisor of287 = 91 ·3+ 14. Hence, the greatest common divisor of91 and 287 is the same as the greatest common divisor of91 and 14. This means that the problem of finding gcd(91, 287) has been reduced to the problem of finding gcd(91, 14). Next, divide 91 by 14 to obtain 91 = 14·6 + 7. 228 3 / The Fundament als: Algorithms, the Integers, and Matrices 3-62 LEMMA 1 unkS� Because any common divisor of91 and 14 also divides 91 -14 . 6 = 7 and any common divisor of 14 and 7 divides 91, it follows that gcd(91, 14) = gcd(14, 7). Continue by dividing 14 by 7, to obtain 14 = 7·2. Because 7 divides 14, it follows that gcd(14, 7) = 7. Furthermore, because gcd(287, 91) = gcd(91, 14) = gcd(14, 7) = 7, the original problem has been solved. We now describe how the Euclidean algorithm works in generality. We will use successive divisions to reduce the problem of finding the greatest common divisor of two positive integers to the same problem with smaller integers, until one of the integers is zero. The Euclidean algorithm is based on the following result about greatest common divisors and the division algorithm. Let a = bq + r, where a, b, q, and r are integers. Then gcd(a, b) = gcd(b, r). Proof: If we can show that the common divisors of a and b are the same as the common divisors of b and r, we will have shown that gcd(a, b) = gcd(b, r), because both pairs must have the same greatest common divisor. So suppose that d divides both a and b. Then it follows that d also divides a -bq = r (from Theorem 1 of Section 3.4). Hence, any common divisor of a and b is also a common divisor of b and r. Likewise, suppose that d divides both band r. Then d also divides b q + r = a. Hence, any common divisor of band r is also a common divisor of a and b. Consequently, gcd(a, b) = gcd(b, r). <l Suppose that a and b are positive integers with a :::: b. Let ro = a and rl = b. When we successively apply the division algorithm, we obtain ro = rlql+r2 rl = r2q2 +r3 0 ::::: r2 < rl, 0 ::::: r3 < r2, rn-2 = rn-Iqn-I + rn 0 ::::: rn < rn-I, rn-I = rnqn' Eventually a remainder of zero occurs in this sequence of successive divisions, because the sequence of remainders a = ro > rl > r2 > ... :::: 0 cannot contain more than a terms. Fur­ thermore, it follows from Lemma 1 that gcd(a, b) = gcd(ro, rl) = gcd(rl, r2) = ... = gcd(rn-2, rn-d = gcd(rn_l, rn) = gcd(rn, 0) = rn. Hence, the greatest common divisor is the last nonzero remainder in the sequence of divisions. EUCLID (C. 325-265 B.C.E.) Euclid was the author ofthe most successful mathematics book ever written, The Elements, which appeared in over 1000 different editions from ancient to modem times. Little is known about Euclid's life, other than that he taught at the famous academy at Alexandria in Egypt. Apparently, Euclid did not stress applications. When a student asked what he would get by learning geometry, Euclid explained that knowledge was worth acquiring for its own sake and told his servant to give the student a coin "because he must make a profit from what he learns." 3-63 3.6 Integers and Algorithms 229 EXAM PLE 12 Find the greatest common divisor of 414 and 662 using the Euclidean algorithm. Exercises Solution: Successive uses of the division algorithm give: 662 = 414 . 1 + 248 414 = 248 . 1 + 166 248 = 166 . 1 + 82 166 = 82·2 + 2 82 = 2·41. Hence, gcd(414, 662) = 2, because 2 is the last nonzero remainder. The Euclidean algorithm is expressed in pseudocode in Algorithm 6. ALGOR ITHM 6 The Euclidean Algorithm. procedure gcd(a, b: positive integers) x :=a y:=b while y =1= 0 begin r :=x mody x :=y y:=r end (gcd(a, b) is x} In Algorithm 6, the initial values of x and yare a and b, respectively. At each stage of the procedure, x is replaced by y, and y is replaced by x mod y, which is the remainder when x is divided by y. This process is repeated as long as y =1= O. The algorithm terminates when y = 0, and the value of x at that point, the last nonzero remainder in the procedure, is the greatest common divisor of a and b. We will study the time complexity of the Euclidean algorithm in Section 4.3, where we will show that the number of divisions required to find the greatest common divisor of a and b, where a ?: b, is O(log b). 1. Convert these integers from decimal notation to binary notation. 4. Convert these integers from binary notation to decimal notation. a) 231 b) 4532 c) 97644 2. Convert these integers from decimal notation to binary notation. a) 321 b) 1023 c) 100632 3. Convert these integers from binary notation to decimal notation. a) 1 III 1 b) 10 0000 0001 c) 101010101 d) 110 1001 0001 0000 a) 1 lOll b) 10 1011 0101 c) II lOll 111O d) III II 00 0001 llll 5. Convert these integers from hexadecimal notation to bi­ nary notation. a) 80E c) ABBA b) 135AB d) DEFACED 6. Convert (BADFACED)16 from its hexadecimal expansion to its binary expansion. 230 3 / The Fundamenta ls: Algorithms, the Integers, and Matrices 7. Convert (ABCDEF) 16 from its hexadecimal expansion to its binary expansion. 8. Convert each of these integers from binary notation to hexadecimal notation. a) 11110111 b) 1010 1010 1010 c) 111011101110111 9. Convert (1011 0111 IOIlh from its binary expansion to its hexadecimal expansion. 10. Convert (1 10000110 0011h from its binary expansion to its hexadecimal expansion. 11. Show that the hexadecimal expansion of a positive integer can be obtained from its binary expansion by grouping to­ gether blocks offour binary digits, adding initial digits if necessary, and translating each block of four binary digits into a single hexadecimal digit. 12. Show that the binary expansion of a positive integer can be obtained from its hexadecimal expansion by translat­ ing each hexadecimal digit into a block of four binary digits. 13. Give a simple procedure for converting from the binary expansion of an integer to its octal expansion. 14. Give a simple procedure for converting from the octal expansion of an integer to its binary expansion. 15. Convert (7345321)8 to its binary expansion and (10 10 11 1011)2 to its octal expansion. 16. Give a procedure for converting from the hexadecimal ex­ pansion of an integer to its octal expansion using binary notation as an intermediate step. 17. Give a procedure for converting from the octal expansion of an integer to its hexadecimal expansion using binary notation as an intermediate step. 18. Convert (12345670)8 to its hexadecimal expansion and (ABB093BABBA) 16 to its octal expansion. 19. Use Algorithm 5 to find 7644 mod 645. 20. Use Algorithm 5 to find 11644 mod 645. 21. Use Algorithm 5 to find 32003 mod 99. 22. Use Algorithm 5 to find 1231001 mod 101. 23. Use the Euclidean algorithm to find a) gcd(12,18). b) gcd(111,201). c) gcd(1001,1331). d) gcd(12345,54321). e) gcd(1000,5040). f) gcd(9888,6060). 24. Use the Euclidean algorithm to find a) gcd(I,5). b) gcd(100, 101). c) gcd(123, 277). d) gcd(1529, 14039). e) gcd(1529,14038). f) gcd(l1 I 11, 111111). 25. How many divisions are required to find gcd(21, 34) using the Euclidean algorithm? 26. How many divisions are required to find gcd(34, 55) using the Euclidean algorithm? 27. Show that every positive integer can be represented uniquely as the sum of distinct powers of 2. [Hint: Con­ sider binary expansions of integers.] 3-64 28. It can be shown that every integer can be uniquely repre­ sented in the form ek3k + ek_13k-1 + ... + el3 + eo, whereej = -I, O,or 1 for j = 0, 1,2, ... , k. Expansions of this type are called balanced ternary expansions. Find the balanced ternary expansions of a) 5. b) 13. c) 37. d) 79. 29. Show that a positive integer is divisible by 3 if and only if the sum of its decimal digits is divisible by 3. 30. Show that a positive integer is divisible by 11 if and only if the difference of the sum of its decimal digits in even­ numbered positions and the sum of its decimal digits in odd-numbered positions is divisible by 11. 31. Show that a positive integer is divisible by 3 if and only if the difference of the sum of its binary digits in even­ numbered positions and the sum of its binary digits in odd-numbered positions is divisible by 3. One's complement representations of integers are used to simplify computer arithmetic. To represent positive and nega­ tive integers with absolute value less than 2n-l, a total ofn bits is used. The leftmost bit is used to represent the sign. A 0 bit in this position is used for positive integers, and a 1 bit in this position is used for negative integers. For positive integers, the remaining bits are identical to the binary expansion ofthe integer. For negative integers, the remaining bits are obtained by first finding the binary expansion of the absolute value of the integer, and then taking the complement of each of these bits, where the complement of a I is a 0 and the complement ofa 0 is a 1. 32. Find the one's complement representations, using bit strings oflength six, ofthe following integers. a) 22 b) 31 c) -7 d) -19 33. What integer does each of the following one's complement representations of length five represent? a) 11001 b) 01101 c) 10001 d) 11111 34. If m is a positive integer less than 2n-l, how is the one's complement representation of -m obtained from the one's complement ofm, when bit strings oflength n are used? 35. How is the one's complement representation of the sum of two integers obtained from the one's complement rep­ resentations of these integers? 36. How is the one's complement representation ofthe differ­ ence of two integers obtained from the one's complement representations ofthese integers? 37. Show that the integer m with one's complement represen­ tation (an-I an-2 ... al ao) can be found using the equation m = -an_I(2n-1 -1) + an_22n-2 + ... + al · 2 + ao. Two's complement representations of integers are also used to simplify computer arithmetic and are used more commonly than one's complement representa tions. To represent an in­ teger x with _2n-1 :::: x :::: 2n-1 - 1 for a specified positive integer n, a total of n bits is used. The leftmost bit is used to represent the sign. A 0 bit in this position is used for positive 3-65 integers, and a 1 bit in this position is used for negative inte­ gers, just as in one's complement expansions. For a positive integer, the remaining bits are identical to the binary expan­ sion of the integer. For a negative integer, the remaining bits are the bits of the binary expansion of2n-1 -Ix I. Two's com­ plement expansions of integers are often used by computers because addition and subtraction of integers can be performed easily using these expansions, where these integers can be either positive or negative. 38. Answer Exercise 32, but this time find the two's comple­ ment expansion using bit strings of length six. 39. Answer Exercise 33 if each expansion is a two's comple- ment expansion of length five. 40. Answer Exercise 34 for two's complement expansions. 41. Answer Exercise 35 for two's complement expansions. 42. Answer Exercise 36 for two's complement expansions. 43. Show that the integer m with two's complement represen­ tation (an-lan-2 ... alaO) can be found using the equation m = -an-I' 2n-1 + an_22n-2 + ... + al ·2 + ao. 44. Give a simple algorithm for forming the two's comple­ ment representation of an integer from its one's comple­ ment representa tion. 45. Sometimes integers are encoded by using four-digit binary expansions to represent each decimal digit. This produces the binary coded decimal form of the in­ teger. For instance, 791 is encoded in this way by 011110010001. How many bits are required to represent a number with n decimal digits using this type of encoding? A Cantor expansion is a sum ofthe form ann! + an-len -1)! + ... + a22! + all!, where ai is an integer with 0 ::: ai ::: i for i = 1, 2, ... , n. 3.7 Applications of Number Theory 231 46. Find the Cantor expansions of a) 2. b) 7. c) 19. d) 87. e) 1000. t) 1,000,000. *47. Describe an algorithm that finds the Cantor expansion of an integer. *48. Describe an algorithm to add two integers from their Cantor expansions. 49. Add(10111)2 and (11010)2 by working through each step of the algorithm for addition given in the text. 50. Multiply (111O)z and (101O)z by working through each step of the algorithm for multiplication given in the text. 51. Describe an algorithm for finding the difference of two binary expansions. 52. Estimate the number of bit operations used to subtract two binary expansions. 53. Devise an algorithm that, given the binary expansions of the integers a and b, determines whether a > b, a = b, ora < b. 54. How many bit operations does the comparison algorithm from Exercise 53 use when the larger of a and b has n bits in its binary expansion? 55. Estimate the complexity of Algorithm 1 for finding the base b expansion of an integer n in terms of the number of divisions used. *56. Show that Algorithm 5 uses O((logmi 10gn) bit opera­ tions to find bn mod m. 57. Show that Algorithm 4 uses O(q log a) bit operations, assuming that a > d. 3.7 Applications of Numb er Theory Introduction Number theory has many applications, especially to computer science. In Section 3.4 we de­ scribed several of these applications, including hashing functions, the generation of pseudo­ random numbers, and shift ciphers. This section continues our introduction to number theory, developing some key results and presenting two important applications: a method for perform­ ing arithmetic with large integers and a recently invented type of cryptosystem, called a public key system. In such a cryptosystem, we do not have to keep encryption keys secret, because knowledge of an encryption key does not help someone decrypt messages in a realistic amount of time. Privately held decryption keys are used to decrypt messages. Before developing these applications, we will introduce some key results that playa central role in number theory and its applications. For example, we will show how to solve systems of linear congruences modulo pairwise relatively prime integers using the Chinese Remainder Theorem, and then show how to use this result as a basis for performing arithmetic with large integers. We will introduce Fermat's Little Theorem and the concept of a pseudoprime and will show how to use these concepts to develop a public key cryptosystem. 232 3 / The Fundament als: Algorithms, the Integers, and Matrices Some Useful Results 3-66 An important result we will use throughout this section is that the greatest common divisor of two integers a and b can be expressed in the form sa + tb, where s and t are integers. In other words, gcd( a, b) can be expressed as a linear combination with integer coefficients of a and b. For example, gcd(6, 14) = 2, and 2 = (-2) ·6+ 1 . 14. We state this fact as Theorem 1. THEOREM 1 If a and b are positive integers, then there exist integers s and t such that gcd(a, b) = sa + tb. We will not give a formal proof of Theorem 1 here (see Exercise 36 in Section 4.2 and [Ro05] for proofs), but we will provide an example of a method for finding a linear combination of two integers equal to their greatest common divisor. (In this section, we will assume that a linear combination has integer coefficients .) The method proceeds by working backward through the divisions of the Euclidean algorithm. (We also describe an algorithm called the extended Euclidean algorithm that can be used to express gcd(a, b) as a linear combination of a and b in the preamble to Exercise 48.) EXAMPLE 1 Express gcd(252, 198) = 18 as a linear combination of252 and 198. Solution: To show that gcd(252, 198) = 18, the Euclidean algorithm uses these divisions: 252 = 1 . 198 + 54 198=3·54+36 54= 1·36+ 18 36 = 2·18. Using the next-to-1ast division (the third division), we can express gcd(252, 198) = 18 as a linear combination of 54 and 36. We find that 18 = 54 - 1 . 36. The second division tells us that 36 = 198 - 3 . 54. Substituting this expression for 36 into the previous equation, we can express 18 as a linear combination of 54 and 198. We have 18 = 54 - 1 ·36 = 54 - 1 . (198 -3·54) = 4·54 -1 . 198. The first division tells us that 54 = 252 - 1 . 198. Substituting this expression for 54 into the previous equation, we can express 18 as a linear combination of 252 and 198. We conclude that 18 = 4· (252 - 1 . 198) - 1 . 198 = 4·252 - 5 . 198, completing the solution. 3-67 LEMMA 1 LEMMA 2 3.7 Applications of Number Theory 233 We will use Theorem I to develop several useful results. One of our goals will be to prove the part of the Fundamental Theorem of Arithmetic asserting that a positive integer has at most one prime factorization. We will show that if a positive integer has a factorization into primes, where the primes are written in nondecreasing order, then this factorization is unique. First, we need to develop some results about divisibility. If a, b, and e are positive integers such that gcd(a, b) = I and a I be, then a I e. Proof: Because gcd( a, b) = I, by Theorem I there are integers s and t such that sa + tb = 1. Multiplying both sides of this equation by c, we obtain sac + tbc = c. Using Theorem I of Section 3.4, we can use this last equation to show that a I c. By part (ii) of that theorem, a I tbc. Because a I sac and a I tbc, by part (i) of that theorem, we conclude that a divides sac + tbc, and hence a I c. This finishes the proof. <I We will use the following generalization of Lemma I in the proof of uniqueness of prime factorizations. (The proof of Lemma 2 is left as Exercise 60 in Section 4.1, because it can be most easily carried out using the method of mathematical induction, which will be covered in that section.) If P is a prime and pial a2 ... an, where each aj is an integer, then P I aj for some i. We can now show that a factorization of an integer into primes is unique. That is, we will show that every integer can be written as the product of primes in nondecreasing order in at most one way. This is part of the Fundamental Theorem of Arithmetic. We will prove the other part, that every integer has a factorization into primes, in Section 4.2. Proof (of the uniqueness of the prime factorization of a positive integer): We will use a proof by contradiction. Suppose that the positive integer n can be written as the product of primes in two different ways, say, n = PIP2 ... Ps and n = ql q2 ... qt, each pj and qj are primes such that PI :::: P2 :::: ... :::: Ps and ql :::: q2 :::: ... :::: qt. When we remove all common primes from the two factorizations, we have where no prime occurs on both sides of this equation and u and v are positive integers. By Lemma I it follows that Pi, divides qjk for some k. Because no prime divides another prime, this is impossible. Consequently, there can be at most one factorization of n into primes in nondecreasing order. <I 234 3 / The Fundamen tals: Algorithms, the Integers, and Matrices 3-68 Lemma 1 can also be used to prove a result about dividing both sides of a congruence by the same integer. We have shown (Theorem 5 in Section 3.4) that we can multiply both sides of a congruence by the same integer. However, dividing both sides of a congruence by an integer does not always produce a valid congruence, as Example 2 shows, EXAMPLE 2 The congruence 14 == 8 (mod 6) holds, but both sides of this congruence cannot be divided by THEOREM 2 THEOREM 3 2 because 14/2 = 7 and 8/2 = 4, but 7 ¢= 4 (mod 6). ... However, using Lemma 1, we can show that we can divide both sides of a congruence by an integer relatively prime to the modulus. This is stated as Theorem 2. Letm be a positive integer and leta, b, ande be integers. Ifae == be (modm)andgcd(e, m) = I, then a == b (mod m). Proof: Because ae == be (mod m),m lac -be = e(a -b). By Lemma 1, becausegcd(e, m) = 1, it follows that m I a-b. We conclude that a == b (mod m). <I Linear Congruences A congruence of the form ax == b (mod m), where m is a positive integer, a and b are integers, and x is a variable, is called a linear congruence. Such congruences arise throughout number theory and its applications. How can we solve the linear congruence ax == b (mod m), that is, find all integers x that satisfy this congruence? One method that we will describe uses an integer a such that aa == 1 (mod m), if such an integer exists. Such an integer a is said to be an inverse of a modulo m. Theorem 3 guarantees that an inverse of a modulo m exists whenever a and m are relatively prime. If a and m are relatively prime integers and m > 1, then an inverse of a modulo m exists. Furthermore, this inverse is unique modulo m. (That is, there is a unique positive integer a less than m that is an inverse of a modulo m and every other inverse of a modulo m is congruent to a modulo m.) Proof: By Theorem 1, because gcd(a, m) = 1, there are integers s and I such that sa + 1m = 1. This implies that sa + 1m == 1 (mod m). Because 1m == 0 (mod m), it follows that sa == 1 (mod m). Consequently, s is an inverse of a modulo m. That this inverse is unique modulo m is left as Exercise 9 at the end of this section. <I 3-69 3.7 Applications of Number Theory 235 The proof of Theorem 3 describes a method for finding the inverse of a modulo m when a and m are relatively prime: find a linear combination of a and m that equals I (which can be done by working backward through the steps ofthe Euclidean algorithm); the coefficie nt of a in this linear combination is an inverse of a modulo m. We illustrate this procedure in Example 3. EXAMPLE 3 Find an inverse of3 modulo 7. Solution: Because gcd(3, 7) = I, Theorem 3 tells us that an inverse of 3 modulo 7 exists. The Euclidean algorithm ends quickly when used to find the greatest common divisor of 3 and 7: 7=2·3+1. From this equation we see that -2·3+ 1 ·7 = 1. This shows that -2 is an inverse 00 modulo 7. (Note that every integer congruent to -2 modulo 7 is also an inverse of3, such as 5, -9, 12, and so on.) ... When we have an inverse a of a modulo m, we can easily solve the congruence ax == b (mod m) by multiplying both sides of the linear congruence by a, as Example 4 illustrates. EXAMPLE 4 What are the solutions of the linear congruence 3x == 4 (mod 7)? linkS� EXAMPLE S Solution: By Example 3 we know that -2 is an inverse of3 modulo 7. Multiplying both sides ofthe congruence by -2 shows that -2·3x == -2·4 (mod 7). Because -6 == 1 (mod 7) and -8 == 6 (mod 7), it follows that if x is a solution, then x == -8 == 6 (mod 7). We need to determine whether every x with x == 6 (mod 7) is a solution. Assume that x == 6 (mod 7). Then, by Theorem 5 of Section 3.4, it follows that 3x == 3 ·6= 18 == 4 (mod 7), which shows that all such x satisfy the congruence. We conclude that the solutions to the congruence are the integers x such that x == 6 (mod 7), namely, 6, 13,20, ... and -1, -8, -15, .... ... The Chinese Remainder Theorem Systems oflinear congruences arise in many contexts. For example, as we will see later, they are the basis for a method that can be used to perform arithmetic with large integers. Such systems can even be found as word puzzles in the writings of ancient Chinese and Hindu mathematicians, such as that given in Example 5. In the first century, the Chinese mathematician Sun-Tsu asked: There are certain things whose number is unknown. When divided by 3, the remainder is 2; when divided by 5, the remainder is 3; and when divided by 7, the remainder is 2. What will be the number of things? 236 3 / The Fundament als: Algorithms, the Integers, and Matrices 3-70 THEOREM 4 This puzzle can be translated into the following question: What are the solutions of the systems of congruences x == 2 (mod 3), x == 3 (mod 5), x == 2 (mod 7)? We will solve this system, and with it Sun-Tsu's puzzle, later in this section. The Chinese Remainder Theorem, named after the Chinese heritage of problems involving systems of linear congruenc es, states that when the moduli of a system of linear congruences are pairwise relatively prime, there is a unique solution of the system modulo the product of the moduli. THE CHINESE REMAINDER THEOREM Let m 1, m2, ... , mn be pairwise relatively prime positive integers and ai, a2, ... , an arbitrary integers. Then the system x == al (mod ml), x == a2 (mod m2), has a unique solution modulo m = m 1 m 2 ••• m n. (That is, there is a solution x with 0 ::: x < m, and all other solutions are congruent modulo m to this solution.) Proof: To establish this theorem, we need to show that a solution exists and that it is unique modulo m. We will show that a solution exists by describing a way to construct this solution; showing that the solution is unique modulo m is Exercise 24 at the end of this section. To construct a simultaneous solution, first let for k = 1,2, ... , n. That is, Mk is the product of the moduli except for mk. Because mj and mk have no common factors greater than 1 when i -I k, it follows that gcd(mb Md = 1. Conse­ quently, by Theorem 3, we know that there is an integer Yk, an inverse of Mk modulo mb such that To construct a simultaneous solution, form the sum We will now show that x is a simultaneous solution. First, note that because Mj == 0 (mod mk) whenever j -I k, all terms except the kth term in this sum are congruent to 0 modulo mk. Because MkYk == 1 (mod mk) we see that x == akMkYk == ak (mod mk), for k = I, 2, ... , n. We have shown that x is a simultaneous solution to the n congruences. <l 3-71 3.7 Applications of Number Theory 237 Example 6 illustrates how to use the construction given in the proof of Theorem 4 to solve a system of congruenc es. We will solve the system given in Example 5, arising in Sun-Tsu's puzzle. EXAMPLE 6 To solve the system of congruences in Example 5, firstletm = 3·5·7 = 105,MI =m/3 = 35, M2 = m/5 = 21, and M3 = m/7 = 15. We see that 2 is an inverse of MI = 35 modulo 3, because 35 == 2 (mod 3); 1 is an inverse of M2 = 21 modulo 5, because 21 == 1 (mod 5); and 1 isan inverse of M3 = 15 (mod 7), because 15 == 1 (mod 7). The solutions to this system are those x such that x == alM1Yl + a2M2Y2 + a3M3Y3 = 2·35·2 + 3·21 . 1 + 2·15·1 = 233 == 23 (mod 105). It follows that 23 is the smallest positive integer that is a simultaneous solution. We conclude that 23 is the smallest positive integer that leaves a remainder of 2 when divided by 3, a remainder of3 when divided by 5, and a remainder of2 when divided by 7. .... Computer Arithmetic with Large Integers Suppose that ml, m2, ... , mn are pairwise relatively prime integers greater than or equal to 2 and let m be their product. By the Chinese Remainder Theorem, we can show (see Exercise 22) that an integer a with ° :::: a < m can be uniquely represented by the n-tuple consisting of its remainders upon division by m i, i = 1, 2, ... , n. That is, we can uniquely represent a by (a mod ml, a mod m2, ... , a mod mn). EXAMPLE 7 What are the pairs used to represent the nonnegative integers less than 12 when they are rep­ resented by the ordered pair where the first component is the remainder of the integer upon division by 3 and the second component is the remainder of the integer upon division by 4? Solution: We have the following representati ons, obtained by finding the remainder of each integer when it is divided by 3 and by 4: 0= (0, 0) 4=(1,0) 8 = (2, 0) 1=(1,1) 5=(2,1) 9 = (0,1) 2 = (2,2) 6 = (0,2) 10=(1, 2) 3 = (0,3) 7=(1,3) 11 = (2,3). .... To perform arithmetic with large integers, we select moduli m l, m2, ... , mn, where each mi is an integer greater than 2, gcd( m i, m j) = 1 whenever i =I-j, and m = m 1 m 2 ••• m n is greater than the result of the arithmetic operations we want to carry out. Once we have selected our moduli, we carry out arithmetic operations with large integers by performing componentwise operations on the n-tuples representing these integers using their remainders upon division by mi, i = 1,2, ... , n. Once we have computed the value of each component in the result, we recover its value by solving a system of n congruences modulomj, i = 1,2, ... , n. This method of performing arithmetic with large integers has severa 1 valuable features. First, it can be used to perform arithmetic with integers larger than can ordinarily be carried out on a computer. Second, computations with respect to the different moduli can be done in parallel, speeding up the arithmetic. 238 3 / The Fundamenta ls: Algorithms, the Integers, and Matrices 3-72 EXAMPLE 8 Suppose that performing arithmetic with integers less than 100 on a certain processor is much quicker than doing arithmetic with larger integers. We can restrict almost all our computations to integers less than 100 if we represent integers using their remainders modulo pairwise relatively prime integers less than 100. For example, we can use the moduli of99, 98, 97, and 95. (These integers are relatively prime pairwise, because no two have a common factor greater than 1.) By the Chinese Remainder Theorem, every nonnegative integer less than 99 . 98 . 97 . 95 = 89,403,930 can be represented uniquely by its remainders when divided by these four moduli. For example, we represent 123,684 as (33, 8, 9, 89), because 123,684 mod 99 = 33,123,684 mod 98 = 8; 123,684 mod 97 = 9; and 123,684 mod 95 = 89. Similarly, we represent 413,456 as (32, 92, 42, 16). To find the sum of 123,684 and 413,456, we work with these 4-tuples instead of these two integers directly. We add the 4-tuples componentwise and reduce each component with respect to the appropriate modulus. This yields (33,8,9,89) + (32, 92, 42, 16) = (65 mod 99,100 mod 98, 51 mod 97,105 mod 95) = (65,2,51,10). To find the sum, that is, the integer represented by (65, 2, 51, 10), we need to solve the system of congruences x == 65 (mod 99) x == 2 (mod 98) x == 51 (mod 97) x == 10 (mod 95) It can be shown (see Exercise 39) that 537,140 is the unique nonnegative solution of this system less than 89,403,930. Conseq uently, 537,140 is the sum. Note that it is only when we have to recover the integer represented by (65, 2, 51, 10) that we have to do arithmetic with integers larger than 100. .... Particularly good choices for moduli for arithmetic with large integers are sets of integers of the form 2k -1, where k is a positive integer, because it is easy to do binary arithmetic modulo such integers, and because it is easy to find sets of such integers that are pairwise relatively prime. [The second reason is a consequence of the fact that gcd(2a -I, 2b -I) = 2gcd(a, b) -I, as Exercise 41 shows.] Suppose, for instance, that we can do arithmetic with integers less than 235 easily on our computer, but that working with larger integers requires special procedure s. We can use pairwise relatively prime moduli less than 235 to perform arithmetic with integers as large as their product. For example, as Exercise 42 shows, the integers 235 -I, 234 -I, 233 -1,231 -1,229 -I, and 223 -I are pairwise relatively prime. Because the product of these six moduli exceeds 2184, we can perform arithmetic with integers as large as 2184 (as long as the results do not exceed this number) by doing arithmetic modulo for each of these six moduli, none of which exceeds 235. Pseudoprimes In Section 3.5 we showed that an integer n is prime when it is not divisible by any prime p with p � In. Unfortunately, using this criterion to show that a given integer is prime is inefficient. It requires that we find all primes not exceeding In and that we carry out trial division by each such prime to see whether it divides n. 3-73 THEOREM S 3.7 Applications of Number Theory 239 Are there more efficient ways to determine whether an integer is prime? According to some sources, ancient Chinese mathematicians believed that n was prime if and only if 2n-1 == 1 (mod n). Ifthis were true, it would provide an efficient primality test. Why did they believe this congruence could be used to determine whether an integer is prime? First, they observed that the congruence holds whenever n is prime. For example, 5 is prime and 25-1 = 24 = 16 == 1 (mod 5). Second, they never found a composite integer n for which the congruence holds. The ancient Chinese were only partially correct. They were correct in thinking that the congruence holds whenever n is prime, but they were incorrect in concluding that n is necessarily prime if the congruence holds. The great French mathematician Fermat showed that the congruence holds when n is prime. He proved the following, more general result. FERMA T'S LITTLE THEOREM If P is prime and a is an integer not divisible by p, then aP-1 == 1 (modp). Furthermore, for every integer a we have aP == a (mod p). The proof of Theorem 5 is outlined in Exercise 17 at the end of this section. Unfortunately, there are composite integers n such that 2n-1 == 1 (mod n). Such integers are called pseudopr imes to the base 2. EXAMPLE 9 The integer 341 is a pseudoprime to the base 2 because it is composite (341 = 11 . 31) and as Exercise 27 shows UDksit:J 2340 == 1 (mod 341). We can use an integer other than 2 as the base when we study pseudoprime s. PIERRE DE FERMAT (1601-1665) Pierre de Fermat, one of the most important mathematicians of the seventeenth century, was a lawyer by profession. He is the most famous amateur mathematician in history. Fermat published little of his mathematical discoveries. It is through his correspondence with other mathematicians that we know of his work. Fermat was one of the inventors of analytic geometry and developed some of the fundamental ideas of calculus. Fermat, along with Pascal, gave probability theory a mathematical basis. Fermat formulated what was the most famous unsolved problem in mathemati cs. He asserted that the equation xn + yn = zn has no nontrivial positive integer solutions when n is an integer greater than 2. For more than 300 years, no proof (or counterexample) was found. In his copy of the works of the ancient Greek mathematician Diophantus, Fermat wrote that he had a proof but that it would not fit in the margin. Because the first proof, found by Andrew Wiles in 1994, relies on sophistica ted, modem mathema tics, most people think that Fermat thought he had a proof, but that the proof was incorrect. However, he may have been tempting others to look for a proof, not being able to find one himself. 240 3 I The Fundament als: Algorithms, the Integers, and Matrices 3-74 DEFINITION 1 Let b be a positive integer. If n is a composite positive integer, and bn-1 == 1 (mod n), then n is called a pseudo prime to the base b. Given a positive integer n, determining whether 2n-1 == 1 (mod n) is a useful test that provides some evidence concerning whether n is prime. In particular, if n satisfies this congruence, then it is either prime or a pseudoprime to the base 2; if n does not satisfy this congruence, it is composite . We can perform similar tests using bases b other than 2 and obtain more evidence as to whether n is prime. If n passes all such tests, it is either prime or a pseudoprime to all the bases b we have chosen. Furthermore, among the positive integers not exceeding x, where x is a positive real number, compared to primes there are relatively few pseudoprimes to the base b, where b is a positive integer. For example, less than 1010 there are 455,052,512 primes, but only 14,884 pseudoprimes to the base 2. Unfortunately, we cannot distinguish between primes and pseudoprimes just by choosing sufficiently many bases, because there are composite integers n that pass all tests with bases with gcd(b, n) = 1. This leads to Definition 2. D EFINITI ON 2 A composite integer n that satisfies the congruence bn-1 == 1 (mod n) for all positive integers b with gcd(b, n) = 1 is called a Carmichael number. (These numbers are named after Robert Carmichael, who studied them in the early twentieth century.) EXAM PLE 10 The integer 561 is a Carmichael number. To see this, first note that 561 is composite because 561 = 3·11 . 17. Next, note that if gcd(b, 561) = I, then gcd(b, 3) = gcd(b, 11) = gcd(b, 17) = 1. Using Fermat's Little Theorem we find that b2 == 1 (mod 3), blO == 1 (mod 11), and bl6 == 1 (mod 17). It follows that b560 = (b2)280 == 1 (mod 3), b560 = (blO)56 == 1 (mod 11), b560 = (bI6)35 == 1 (mod 17). By Exercise 23 at the end of this section, it follows that b560 == 1 (mod 561) for all positive integers b with gcd(b, 561) = 1. Hence 561 is a Carmichael number. .... Although there are infinitely many Carmichael numbers, more delicate tests, described in the exercise set at the end ofthis section, can be devised that can be used as the basis for efficient ROBERT DANIEL CARMICHAE L (1879-1967) Robert Daniel Carmichael was born in Alabama. He re­ ceived his undergraduate degree from Lineville College in 1898 and his Ph.D. in 1911 from Princeton. Carmichael held positions at Indiana University from 1911 until 1915 and at the University of Illinois from 1915 until 1947. Carmichael was an active researcher in a wide variety of areas, including number theory, real analysis, differential equations, mathematical physics, and group theory. His Ph.D. thesis, written under the direction of G. D. Birkhoff , is considered the first significant American contribution to the subject of differential equations. 3.7 Applications of Nwnber Theory 241 probabilistic primality tests. Such tests can be used to quickly show that it is almost certainly the case that a given integer is prime. More precisely, if an integer is not prime, then the probability that it passes a series of tests is close to O. We will describe such a test in Chapter 6 and discuss the notions from probability theory that this test relies on. These probabilistic primality tests can be used, and are used, to find large primes extremely rapidly on computers. Public Key Cryptography In Section 3.4 we introduced methods for encrypting messages based on congruences. When these encryption methods are used, messages, which are strings of characters, are translated into numbers. Then the number for each character is transformed into another number, either using a shift or an affine transformation modulo 26. These methods are examples of private key cryp­ tosystems. Knowing the encryption key lets you quickly find the decryption key. For example, when a shift cipher is used with encryption key k, a number p representing a letter is sent to c = (p + k) mod 26. Decryption is carried out by shifting by -k; that is, p = (c -k) mod 26. When a private key cryptosystem is used, a pair of people who wish to communicate in secret must have a separate key. Because anyone knowing this key can both encrypt and decrypt messages easily, these two people need to securely exchange the key. In the mid-1970s, cryptologists introduced the concept of public key cryptosystems. When such cryptosystems are used, knowing how to send someone a message does not help you decrypt messages sent to this person. In such a system, every person can have a publicly known encryption key. Only the decryption keys are kept secret, and only the intended recipient of a message can decrypt it, because the encryption key does not let someone find the decryption key without an extraordinary amount of work (such as more than 2 billion years of computer time). The RSA Cryptosystem In 1976, three researchers at M.I.T.-Ronald Rivest, Adi Shamir, and Leonard Adleman­ introduced to the world a public key cryptosystem, known as the RSA system, from the initials of its inventors. (As is often the case with cryptographic discoveries, the RSA system had already been discovered in secret government research. Clifford Cocks, working in secrecy at the United Kingdom's Government Communications Headquarters, had discovered this cryptosystem in 1973, but his invention was unknown to the outside world until the late 1990s. An excellent account of this earlier disco very, as well as the work of Rivest, Shamir, and Adleman, can be found in [Si99].) The RSA cryptosystem is based on modular exponentiation modulo the product of two large primes, which can be done rapidly using Algorithm 5 in Section 3.6. Each individual has an encryption key consisting of a modulus n = pq, where p and q are large primes, say, with 200 digits each, and an exponent e that is relatively prime to (p -1)(q -1). To produce a usable key, two large primes must be found. This can be done quickly on a computer using probabilistic primality tests, referred to earlier in this section. However, the product of these primes n = pq, with approximately 400 digits, cannot be factored in a reasonable length of time. As we will see, this is an important reason why decryption cannot be done quickly without a separate decryption key. 242 3 / The Fundament als: Algorithms, the Integers, and Matrices RSA Encryption 3-76 In the RSA encryption method, messages are translated into sequences of integers. This can be done by translating each letter into an integer, as is done with the Caesar cipher. These integers are grouped together to form larger integers, each representing a block ofletters. The encryption proceeds by transforming the integer M, representing the plaintext (the original message), to an integer C, representing the ciphertext (the encrypted message), using the function C = Me modn. (To perform the encryption, we use an algorithm for fast modular exponentiation, such as Algorithm 5 in Section 3.6.) We leave the encrypted message as blocks of numbers and send these to the intended recipient. Example 11 illustrates how RSA encryption is performed. For practical reasons we use small primes p and q in this example, rather than primes with 100 or more digits. Although the cipher described in this example is not secure, it does illustrate the techniques used in the RSA cipher. EXAMPLE 11 Encrypt the message STOP using the RSA cryptosystem with p = 43 and q = 59, so that n = 43 . 59 = 2537, and with e = 13. Note that gcd(e, (p -l)(q -1)) = gcd(13, 42·58) = 1. Solution: We translate the letters in STOP into their numerical equivalents and then group the numbers into blocks of four. We obtain 1819 1415. RONALD RIVEST (BORN 1948) Ronald Rivest received a B.A. from Yale in 1969 and his Ph.D. in computer science from Stanford in 1974. Rivest is a computer science professor at M.I.T. and was a cofounder ofRSA Data Security, which held the patent on the RSA cryptosystem that he invented together with Adi Shamir and Leonard Adleman. Areas that Rivest has worked in besides cryptography include machine learning, VLSI design, and computer algorithms. He is a coauthor of a popular text on algorithms ([CoLeRiStO 1 D. ADI SHAMIR (BORN 1952) Adi Shamir was born in Tel Aviv, Israel. His undergraduate degree is from Tel Aviv University (1972) and his Ph.D. is from the Weizmann Institute of Science (1977). Shamir was a research assistant at the University of Warwick and an assistant professor at M.I. T. He is currently a profes­ sor in the Applied Mathematics Department at the Weizmann Institute and leads a group studying computer security. Shamir's contributions to cryptography, besides the RSA cryptosystem, include cracking knapsack cryptosystems, cryptanaly sis of the Data Encryption Standard (DES), and the design of many cryptographic protocols. LEONARD AD LEMAN (BORN 1945) Leonard Adleman was born in San Francisco, California. He received a B.S. in mathematics (1968) and his Ph.D. in computer science (1976) from the University of California, Berkeley. Adleman was a member of the mathematics faculty at M.I.T. from 1976 until 1980, where he was a coinventor of the RSA cryptosystem, and in 1980 he took a position in the computer science department at the University of Southern California (USC). He was appointed to a chaired position at USC in 1985. Adleman has worked on computer security, computational complexity, immunology, and molecular biology. He invented the term "computer virus." Adleman's recent work on DNA computing has sparked great interest. He was a technical adviser for the movie Sneakers, in which computer security played an important role. 3-77 We encrypt each block using the mapping C = MI3 mod 2537. 3.7 Applications of Number Theory 243 Computations using fast modular multiplication show that 181913 mod 2537 = 2081 and 141513 mod 2537 = 2182. The encrypted message is 2081 2182. ... RSA Decryption The plaintext message can be quickly recovered when the decryption key d, an inverse of e modulo (p -I)(q -I), is known. [Such an inverse exists because gcd(e, (p -1)(q -I)) = 1.] To see this, note that if de == I (mod (p -I)(q -I)), there is an integer k such that de = 1 + k(p -I)(q -I). It follows that By Fermat's Little Theorem [assuming that gcd(M, p) = gcd(M, q) = 1, which holds except in rare cases], it follows that MP-l == I (mod p) and Mq-I == I (mod q). Consequently, Cd == M . (MP-1i(q-l) == M . I == M (mod p) and Cd == M · (Mq-I)k(p-I) == M·I == M (modq). Because gcd(p, q) = I, it follows by the Chinese Remainder Theorem that Cd == M (mod pq). Example 12 illustrates how to decrypt messages sent using the RSA cryptosystem. EXAMPLE 12 We receive the encrypted message 0981 0461. What is the decrypted message if it was encrypted using the RSA cipher from Example II? unkS� Solution: The message was encrypted using the RSA cryptosystem with n = 43 . 59 and expo­ nent 13. As Exercise 4 shows, d = 937 is an inverse of 13 modulo 42 . 58 = 2436. We use 937 as our decryption exponent. Consequently, to decrypt a block C, we compute p = C937 mod 2537. To decrypt the message, we use the fast modular exponentiation algorithm to compute 0981937 mod 2537 = 0704 and 0461937 mod 2537 = 1115. Consequently, the numerical version of the original message is 0704 1115. Translating this back to English letters, we see that the message is HELP. ... RSA as a Public Key System Why is the RSA cryptosystem suitable for public key cryptography? When we know the factorization ofthe modulus n, that is, when we know p and q, we can use the Euclidean algorithm to quickly find an exponent d inverse to e modulo (p - 1 )(q -1). This lets us decrypt messages sent using our key. However, no method is known to decrypt messages that is not based on 244 3 / The Fundamental s: Algorithms, the Integers, and Matrices 3-78 finding a factorization of n, or that does not also lead to the factorization of n. Factorization is believed to be a difficult problem, as opposed to finding large primes p and q, which can be done quickly. The most efficient factorization methods known (as of 2005) require billions of years to factor 400-digit integers. Consequently, when p and q are 200-digit primes, messages encrypted using n = pq as the modulus cannot be found in a reasonable time unless the primes p and q are known. Active research is under way to find new ways to efficiently factor integers. Integers that were thought, as recently as several years ago, to be far too large to be factored in a reasonable amount of time can now be factored routinely. Integers with more than 100 digits, as well as some with more than 150 digits, have been factored using team efforts. When new factorization techniques are found, it will be necessary to use larger primes to ensure secrecy of messages. Unfortunately, messages that were considered secure earlier can be saved and subsequently decrypted by unintended recipients when it becomes feasible to factor the n = pq in the key used for RSA encryption. The RSA method is now widely used. However, the most commonly used cryptosystems are private key cryptosystems. The use of public key cryptography, via the RSA system, is growing. However, there are applications that use both private key and public key systems. For example, a public key cryptosystem, such as RSA, can be used to distribute private keys to pairs of individuals when they wish to communicate. These people then use a private key system for encryption and decryption of messages. Exercises 1. Express the greatest common divisor of each of these pairs of integers as a linear combination of these integers. a) 10, II b) 21,44 c) 36,48 d) 34,55 e) 117,213 t) 0,223 g) 123,2347 h) 3454,4666 i) 9999, 11111 2. Express the greatest common divisor of each ofthese pairs of integers as a linear combination of these integers. a) 9, 11 b) 33,44 c) 35, 78 d) 21,55 e) 101,203 t) 124,323 g) 2002,2339 h) 3457,4669 i) 10001, 13422 3. Show that 15 is an inverse of7 modulo 26. 4. Show that 937 is an inverse of 13 modulo 2436. 5. Find an inverse of 4 modulo 9. 6. Find an inverse of 2 modulo 17. 7. Find an inverse of 19 modulo 141. 8. Find an inverse of 144 modulo 233. *9. Show that if a and m are relatively prime positive inte­ gers, then the inverse of a modulo m is unique modulo m. [Hint: Assume that there are two solutions b and e of the congruence ax == 1 (mod m). Use Theorem 2 to show that b == e(mod m).] 10. Show that an inverse of a modulo m does not exist if gcd(a, m) > 1. 11. Solve the congruence 4x == 5 (mod 9). 12. Solve the congruence 2x == 7 (mod 17). *13. Show that if m is a positive integer greater than 1 and ae == be (mod m), then a == b mod m/gcd(e, m). 14. a) Show that the positive integers less than II, except 1 and 10, can be split into pairs of integers such that each pair consists of integers that are inverses of each other modulo 11. b) Use part (a) to show that 1O! == -1 (mod 11). 15. Show that if p is prime, the only solutions of x2 == 1 (mod p) are integers x such that x == 1 (mod p) and x == -1 (modp). *16. a) Generalize the result in part (a) of Exercise 14; that is, show that if p is a prime, the positive integers less thanp, except 1 and p -1, can be split into (p -3)/2 pairs of integers such that each pair consists of integers that are inverses of each other. [Hint: Use the result of Exercise 15.] b) From part (a) conclude that (p -I)! == -1 (mod p) whenever p is prime. This result is known as Wilson's Theorem. c) What can we conclude if n is a positive integer such that(n -I)! ¥= -1 (modn)? *17. This exercise outlines a proof of Fermat's Little Theorem. a) Suppose that a is not divisible by the prime p. Show that no two of the integers 1 . a, 2 · a, ... , (p -l)a are congruent modulo p. b) Conclude from part (a) that the product of I, 2, ... , p - I is congruent modulo p to the prod­ uct of a, 2a, ... , (p -I)a. Use this to show that (p -I)! == aP-1(p -I)! (mod pl. 3-79 c) Use Theorem 2 to show from part (b) that aP-1 == 1 (mod p) if P fa. [Hint: Use Lemma 2 to show that P does not divide (p -1)1 and then use Theorem 2. Alternatively, use Wilson's Theorem.] d) Use part (c) to show that a p == a (mod p) for all inte­ gers a. 18. Find all solutions to the system of congruen ces. x == 2 (mod 3) x == 1 (mod 4) x == 3 (mod 5) 19. Find all solutions to the system of congruences. x == 1 (mod 2) x == 2 (mod 3) x == 3 (mod 5) x == 4 (mod 11) *20. Find all solutions, if any, to the system of congruences. x == 5 (mod 6) x == 3 (mod 10) x == 8 (mod 15) *21. Find all solutions, if any, to the system of congruences. x == 7 (mod 9) x == 4 (mod 12) x == 16 (mod 21) 22. Use the Chinese Remainder Theorem to show that an integer a, with 0 :::: a < m = mlm2'" mn, where the integers ml, m2,"" mn are pairwise relatively prime, can be represented uniquely by the n-tuple (a mod m I, a mod m2, ... , a mod mn). *23. Let m I, m2, ... ,mn be pairwise relatively prime integers greater than or equal to 2. Show that if a == b (mod mil for i = 1, 2, ... , n, then a == b(mod m), where m = m 1 m 2 ... m n' (This result will be used in Exercise 24 to prove the Chinese remainder theorem. Consequently, do not use the Chinese remainder theorem to prove it.) *24. Complete the proof of the Chinese Remainder Theorem by showing that the simultaneous solution of a system of linear congruences modulo pairwise relatively prime integers is unique modulo the product of these moduli. [Hint: Assume that x and y are two simultaneous solu­ tions. Show that mi I x -y for all j. Using Exercise 23, conclude that m = mlm2 ... mn I x -y.] 25. Which integers leave a remainder of 1 when divided by 2 and also leave a remainder of 1 when divided by 3? 26. Which integers are divisible by 5 but leave a remainder of 1 when divided by 3? 27. a) Show that 2340 == 1 (mod 11) by Fermat's Little The­ orem and noting that 2340 = (210)34. b) Show that 2340 == 1 (mod 31) using the fact that 2340 = (25)68 = 3268. c) Conclude from parts (a) and (b) that 2340 == 1 (mod 341). 28. a) Use Fermat's Little Theorem to compute 3 302 mod 5, 3302 mod 7, and 3302 mod 11. b) Use your results from part (a) and the Chinese Re-3.7 Applications of Number Theory 245 mainder Theorem to find 3302 mod 385. (Note that 385 = 5 . 7 . 11.) 29. a) Use Fermat's Little Theorem to compute 52003 mod 7, 52003 mod 11, and 52003 mod 13. b) Use your results from part (a) and the Chinese Re­ mainder Theorem to find 52003 mod 1001. (Note that 1001 = 7 . 11 . 13.) I3rLet n be a positive integer and let n - 1 = 2St, where s is a nonnegative integer and t is an odd positive integer. We say that n passes Miller's testfor the base b if either Y == 1 (mod n) or b2it == -1 (mod n) for some j with 0 :::: j :::: s -1. It can be shown (see [Ro05]) that a composite integer n passes Miller's test for fewer than n/4 bases b with 1 < b < n. *30. Show that if n is prime and b is a positive integer with n f b, then n passes Miller's test to the base b. 31. Show that 2047 passes Miller's test to the base 2, but that it is composite. A composite positive integer n that passes Miller's test to the base b is called a strong pseudoprime to the base b.1t follows that 2047 is a strong pseudoprime to the base 2. 32. Show that 1729 is a Carmichael number. 33. Show that 2821 is a Carmichael number. *34. Show that if n = PI P2 ... Pk. where PI, P2, ... , Pk are distinct primes that satisfy P j -1 I n - 1 for j = 1,2, ... ,k, then n is a Carmichael number. 35. a) Use Exercise 34 to show that every integer ofthe form (6m + 1)(12m + 1)(18m + 1), where m is a positive integer and 6m + 1, 12m + 1, and 18m + 1 are all primes, is a Carmichael number. b) Use part (a) to show that 172,947,529 is a Car­ michael number. 36. Find the nonnegative integer a less than 28 represented by each ofthese pairs, where each pair represents (a mod 4, a mod 7). a) (0,0) d) (2, 1) g) (2,0) b) (1,0) e) (2,2) h) (3,5) c) (1, 1) f) (0,3) i) (3, 6) 37. Express each nonnegative integer a less than 15 using the pair (a mod 3, a mod 5). 38. Explain how to use the pairs found in Exercise 37 to add 4 and 7. 39. Solve the system of congruences that arises in Example 8. *40. Show that whenever a and b are both positive integers, then (2a -1) mod (2b -1) = 2a mod b -1. **41. Use Exercise 40 to show that if a and b are pos­ itive integers, then gcd(2a - 1, 2b -1) = 2gcd(a, b) -1. [Hint: Show that the remainders obtained when the Eu­ clidean algorithm is used to compute gcd(2a - 1, 2b -1) are of the form 2r -1, where r is a remainder arising when the Euclidean algorithm is used to find gcd(a, b).] 42. Use Exercise 41 to show thatthe integers 235 -1,234 -1, 233 -1, 231 -1,229 -1, and 223 - 1 are pairwise rela­ tively prime. 43. Show that if P is an odd prime, then every divisor of the 246 3 / The Fundamental s: Algorithms, the Integers, and Matrices Mersenne number 2P -I is ofthe form 2kp + 1, where k is a nonnegative integer. [Hint: Use Fermat's Little The­ orem and Exercise 41.] 44. Use Exercise 43 to determine whether M13 = 213 - 1 = 8191 and M23 = 223 - 1 = 8,388,607 are prime. *45. Show that we can easily factor n when we know that n is the product of two primes, p and q, and we know the value of(p -1)(q -1). 46. Encrypt the message ATTACK using the RSA system with n = 43 . 59 and e = 13, translating each letter into inte­ gers and grouping together pairs of integers, as done in Example 11. 47. What is the original message encrypted using the RSA system with n = 43 . 59 and e = 13 ifthe encrypted mes­ sage is 0667 19470671? (Note: Some computational aid is needed to do this in a realistic amount oftime.) The extended Euclidean algorithm can be used to express � gcd(a, b) as a linear combination with integer coefficients � of the integers a and b. We set So = 1, SI = 0, to = 0, and tl = 1 and letsj = Sj-2 -qj_ISj_1 and tj = tj-2 -qj_ltj_1 for j = 2, 3, ... , n, where the qj are the quotients in the divi­ sions used when the Euclidean algorithm finds gcd(a, b) (see page 228). It can be shown (see [Ro05]) that gcd(a, b) = Sna + tnb. 48. Use the extended Euclidean algorithm to express gcd(252, 356) as a linear combination of252 and 356. 49. Use the extended Euclidean algorithm to express gcd(144, 89) as a linear combination of 144 and 89. 50. Use the extended Euclidean algorithm to express gcd(1001, 100001) as a linear combination of 1001 and 100001. 51. Describe the extended Euclidean algorithm using pseudocode. If m is a positive integer, the integer a is a quadratic residue of m if gcd(a, m) = 1 and the congruence x2 == a (modm) has a solution. In other words, a quadratic residue of m is an integer relatively prime to m that is a perfect square modulo m. For example, 2 is a quadratic residue of 7 because gcd(2, 7) = 1 and 32 == 2 (mod 7) and 3 is a quadratic nonresidue of 7 because gcd(3, 7) = 1 and x2 == 3 (mod 7) has no solution. 52. Which integers are quadratic residues of 11 ? 53. Show that if p is an odd prime and a is an integer not divisible by p, then the congruence x2 == a (mod p) has 3.8 Matrices Introduction 3-80 either no solutions or exactly two incongruent solutions modulop. 54. Show that if p is an odd prime, then there are exactly (p -1)/2 quadratic residues of p among the integers 1,2, ... , p -1. If p is an odd prime and a is an integer not divisible by p, the Legendre symbol (�) is defined to be 1 if a is a quadratic residue of p and -1 otherwise. 55. Show that if p is an odd prime and a and b are integers with a == b (mod p), then 56. Prove that if p is an odd prime and a is a positive integer not divisible by p, then (�) == a(p-I)/2 (mod p). 57. Use Exercise 56 to show that if p is an odd prime and a and b are integers not divisible by p, then (�) = (�)(%). 58. Show that if p is an odd prime, then -1 is a quadratic residue of p if p == 1 (mod 4), and -1 is not a quadratic residue of p if p == 3 (mod 4). [Hint: Use Exercise 56.] 59. Find all solutions of the congruence x2 == 29 (mod35). [Hint: Find the solutions of this congruence modulo 5 and modulo 7, and then use the Chinese Remainder Theorem.] 60. Find all solutions ofthe congruence x2 == 16 (mod 105). [Hint: Find the solutions of this congruence modulo 3, modulo 5, and modulo 7, and then use the Chinese Re­ mainder Theorem.] *61. Explain why it would not be suitable to use p, where p is a large prime, as the modulus for encryption in the RSA cryptosystem. That is, explain how someone could, without excessive computation, find a private key from the corresponding public key if the modulus were a large prime, rather than the product of two large primes. Matrices are used throughout discrete mathematics to express relationships between elements in sets. In subsequent chapters we will use matrices in a wide variety of models. For instance, matrices will be used in models of communications networks and transportation systems. Many algorithms will be developed that use these matrix models. This section reviews matrix arithmetic that will be used in these algorithms. 3-81 3.8 Matrices 247 DEFINITION 1 A matrix is a rectangular array of numbers. A matrix with m rows and n columns is called an m x n matrix. The plural of matrix is matrices. A matrix with the same number of rows as columns is called square. Two matrices are equal if they have the same number of rows and the same number of columns and the corresponding entries in every position are equal. EXAM PLE 1 The matrix [� n is a 3 x 2 matrix. We now introduce some terminology about matrices. Boldface uppercase letters will be used to represent matrices. DEFINITION 2 Let DEFINITION 3 A= all al2 aln a2l a22 a2n The ith row of A is the 1 x n matrix [ail, ai2,"" ain]. The jth column of A is the n x 1 matrix The (i, j)th element or entry of A is the element aij, that is, the number in the ith row and jth column of A. A convenient shorthand notation for expressing the matrix A is to write A = [aij], which indicates that A is the matrix with its (i, j)th element equal to aij. Matrix Arithmetic The basic operations of matrix arithmetic will now be discussed, beginning with a definition of matrix addition. Let A = [aij] and B = [bij] be m x n matrices. The sum of A and B, denoted by A + B, is the m x n matrix that has aij + bij as its (i, j)th element. In other words, A + B = [aij + bij]. 248 3 / The Fundament als: Algorithms, the Integers, and Matrices 3-82 EXAMPLE 2 DEFINITION 4 The sum of two matrices of the same size is obtained by adding elements in the corresponding positions. Matrices of different sizes cannot be added, because the sum of two matrices is defined only when both matrices have the same number ofrows and the same number of columns. [1 0 We have 2 2 3 4 -1] [3 -3 + 1 o -1 4 -3 1 -1] [4 0=3 2 2 4 -1 5 -2] -3 . 2 We now discuss matrix products. A product of two matrices is defined only when the number of columns in the first matrix equals the number of rows of the second matrix. Let A be an m x k matrix and B be a k x n matrix. The product of A and B, denoted by AB, is the m x n matrix with its (i, J)th entry equal to the sum of the products ofthe corresponding elements from the ith row of A and the Jth column ofB. In other words, if AB = [cii]' then cij = ail blj + ai2b2i + ... + aikbki. In Figure 1 the colored row of A and the colored column of B are used to compute the element cii of AB. The product of two matrices is not defined when the number of columns in the first matrix and the number of rows in the second matrix are not the same. We now give some examples of matrix products. EXAMPLE 3 Let Extra� Examples� o 4] 1 1 1 0 2 2 Find AB if it is defined. and Solution: Because A is a 4 x 3 matrix and B is a 3 x 2 matrix, the product AB is defined and is a 4 x 2 matrix. To find the elements of AB, the corresponding elements of the rows of A and the columns ofB are first multiplied and then these products are added. For instance, the element in the (3, l)th position of AB is the sum of the products ofthe corresponding elements of the third row of A and the first column of B; namely, 3 . 2 + 1 . 1 + 0 . 3 = 7. When all the elements of all a12 a2l a22 ail ail amI am2 FIGUR E 1 alk a2k [bll b21 aik bkl amk b12 blj b22 b2j bk2 bkj bl'] [Cll b2n C21 · . · . · . bkn Cml The Product of A = [aii] and B = [hii]. Cl2 CI. ] C22 C2n Cij Cm2 Cmn 3-83 3.8 Matrices 249 AB are computed, we see that [14 AB� � Matrix multiplication is not commutative. That is, if A and B are two matrices, it is not necessarily true that AB and BA are the same. In fact, it may be that only one of these two products is defined. For instance, if A is 2 x 3 and B is 3 x 4, then AB is defined and is 2 x 4; however, BA is not defined, because it is impossible to multiply a 3 x 4 matrix and a 2 x 3 matrix. In general, suppose that A is an m x n matrix and B is an r x s matrix. Then AB is defined only when n = rand BA is defined only when s = m. Moreover, even when AB and BA are both defined, they will not be the same size unless m = n = r = s. Hence, ifboth AB and BA are defined and are the same size, then both A and B must be square and of the same size. Furthermore, even with A and B both n x n matrices, AB and BA are not necessarily equal, as Example 4 demonstra tes. EXAMPLE 4 Let A= U �J and B= [i �J. DoesAB = BA? Solution: We find that and BA= [j �J. Hence, AB f=. BA. Algorithms for Matrix Multiplication The definition of the product of two matrices leads to an algorithm that computes the product of two matrices. Suppose that C = [cij] is the m x n matrix that is the product of the m x k matrix A = [aij] and the k x n matrix B = [bij]. The algorithm based on the definition ofthe matrix product is expressed in pseudocode in Algorithm 1. ALGORITHM 1 Matrix Multiplication. procedure matrix multiplication(A, B: matrices) for i := 1 to m for j := 1 to n begin Cij:= 0 for q := 1 to k Cij := Cij + aiqbqj end Ie = [Cij] is the product of A and B} 250 3 / The Fundament als: Algorithms, the Integers, and Matrices 3-84 We can determine the complexity of this algorithm in terms of the number of additions and multiplications used. EXAMPLE 5 How many additions of integers and multiplications of integers are used by Algorithm 1 to multiply two n x n matrices with integer entries? linkS� EXAMPLE 6 Solution: There are n2 entries in the product of A and B. To find each entry requires a total of n multiplications and n - 1 additions. Hence, a total of n3 multiplications and n2(n -1) additions are used. .... Surprisingl y, there are more efficient algorithms for matrix multiplication than that given in Algorithm 1. As Example 5 shows, multiplying two n x n matrices directly from the definition requires O(n3) multiplications and additions. Using other algorithms, two n x n matrices can be multiplied using O(n..n) multiplications and additions. (Details of such algorithms can be found in [CoLeRiStOl].) MATRIX-CHAIN MULTIPLICATION There is another important problem involving the complexity of the multiplication of matrices. How should the matrix-chain AIA2··· An be computed using the fewest multiplications of integers, where AI, A2, ... , An are ml x m2, m2 x m3, ... ,mn x mn+1 matrices, respectively, and each has integers as entries? (Because matrix multiplication is associative, as shown in Exercise 13 at the end of this section, the order of the multiplication used does not matter.) Before studying this. problem, note that mlm2m3 multiplications of integers are performed to multiply anml x m2 matrix and an m2 x m3 matrix using Algorithm 1 (see Exercise 23 at the end of this section). Example 6 illustrates this problem. In which order should the matrices AI, A2, and A3-where AI is 30 x 20, A2 is 20 x 40, and A3 is 40 x 10, all with integer entries-be multiplied to use the least number of multiplications of integers? Solution: There are two possible ways to compute AIA2A3. These are AI (A2A3) and (AIA2)A3. If A2 and A3 are first multiplied, a total of20 ·40 . 10 = 8000 multiplications of integers are used to obtain the 20 x 10 matrix A2A3. Then, to multiply A I and A2A3 requires 30 . 20 . 10 = 6000 multiplications. Hence, a total of 8000 + 6000 = 14,000 multiplications are used. On the other hand, if AI and A2 are first multiplied, then 30 . 20 ·40 = 24,000 multiplications are used to obtain the 30 x 40 matrix AIA2. Then, to multiply AIA2 and A3 requires 30 x 40 x 10 = 12,000 multiplications. Hence, a total of 24,000 + 12,000 = 36,000 multiplications are used. Clearly, the first method is more efficient. Algorithms for determining the most efficient way to carry out matrix-chain multiplication are discussed in [CoLeRiStOl]. 3-85 3.8 Matrices 251 Transposes and Powers of Matrices We now introduce an important matrix with entries that are zeros and ones. DEFINITION 5 The identity matrix of order n is the n x n matrix In = [8ij], where 8ij = 1 if i = j and 8ij = 0 ifi #-j. Hence 1 0 0 o 1 0 o 0 1 Multiplying a matrix by an appropriately sized identity matrix does not change this matrix. In other words, when A is an m x n matrix, we have Powers of square matrices can be defined. When A is an n x n matrix, we have Ar = AAA···A. '-..-' r times The operation of interchanging the rows and columns of a square matrix is used in many algorithms. DEFINITION 6 Let A = [aij] be an m x n matrix. The transpose of A, denoted by N, is the n x m matrix obtained by interchanging the rows and columns of A. In other words, if At = [hij], then hij =aji fori = 1,2, ... ,n andj = 1,2, ... ,m. EXAMPLE 7 The transpose oftbe matrix [! � �] is the matrix U n DEFINITION 7 Matrices that do not change when their rows and columns are interchanged are often im­ portant. A square matrix A is called symmetric if A = At. Thus A = [aij] is symmetric if aij = a ji for all i and j with 1 ::s i ::s nand 1 ::s j ::s n. 252 3 / The Fundamentals: Algorithms, the Integers, and Matrices 3-86 Note that a matrix is symmetric if and only if it is square and it is symmetric with respect to its main diagonal (which consists of entries that are in the ith row and ith column for some i). This symmetry is displayed in Figure 2. EXAMPLE 8 The matrix [ ! b �] is symmetri c. 0 1 0 FIGUR E2 A Symmetric Matrix. Zero-One Matrices A matrix with entries that are either 0 or 1 is called a zero-one matrix. Zero-one matrices are often used to represent discrete structure s, as we will see in Chapters 8 and 9. Algorithms using these structures are based on Boolean arithmetic with zero-one matrices. This arithmetic is based on the Boolean operations v and /\, which operate on pairs of bits, defined by {I if bl = b2 = 1 bl /\ b2 = 0 otherwise, {I if bl = 1 or b2 = 1 bl V b2 = 0 otherwise. DEFINITION 8 Let A = [aij] and B = [bij] be m x n zero-one matrices. Then thejoin of A and B is the zero-one matrix with (i, j)th entry aij v bij. The join of A and B is denoted by A v B. The meet of A and B is the zero-one matrix with (i, j)th entry aij /\ bij. The meet of A and B is denoted by A /\ B. EXAMPLE 9 Find the join and meet of the zero-one matrices [1 0 1] A= 0 1 0 ' 1 1 Solution: We find that the join of A and B is A B_[IVO Ovl v -Ovl Ivl The meet of A and B is A B_[I/\O 0 /\1 /\ -0/\1 1/\1 IVO]_[1 OvO -1 1/\0]_[000] 0/\0 -0 1 0 . We now define the Boolean product of two matrices. 3-87 DEFINITION 9 3.8 Matrices 253 Let A = [aij] be an m x k zero-one matrix and B = [bij] be a k x n zero-one matrix. Then the Boolean product of A and B, denoted by A 0 B, is the m x n matrix with (i, j)th entry Cij where Cij = (ail /\ b1j) v (ai2 /\ b2j) v ... V (aik /\ bkj). Note that the Boolean product of A and B is obtained in an analogous way to the ordinary product of these matrices, but with addition replaced with the operation V and with multiplication replaced with the operation I\. We give an example of the Boolean products of matrices. EXAM PLE 10 Find the Boolean product of A and B, where Solution: The Boolean product A 0 B is given by [(1/\ l)v(O/\O) (1/\ l)v(O/\ 1) A0B= (O/\I)v(1/\O) (O/\I)v(1/\I) (1/\ l)v(O/\O) (1/\ l)v(O/\ 1) [1 vO 1 vO OVO] = OvO Ovl Ovl IvO IvO OvO (1/\ 0) V (0 /\ 1)] (O/\O)v(l/\ 1) (1/\O)v(O/\I) Algorithm 2 displays pseudocode for computing the Boolean product of two matrices. ALGORITHM 2 The Boolean Product. procedure Boolean product (A, B: zero-one matrices) for i := 1 to m for j := 1 to n begin Cij:= 0 forq := 1 to k Cij := Cij V (aiq /\ bqj) end {C = [cij] is the Boolean product of A and B} We can also define the Boolean powers of a square zero-one matrix. These powers will be used in our subsequent studies of paths in graphs, which are used to model such things as communications paths in computer networks. 254 3 / The Fundamentals: Algorithms, the Integers, and Matrices 3-88 DEFINITION 10 Let A be a square zero-one matrix and let r be a positive integer. The rth Boolean power of A is the Boolean product ofr factors of A. The rth Boolean product of A is denoted by A[r]. Hence A[r] = A0A0A0 ... 0A. \ v I r times (This is well defined because the Boolean product of matrices is associative.) We also define A[O] to be In. EXAMPLE 11 Let A � [r � � J Find A [,] for all positive integers n. Solution: We find that We also find that A[3] � A[2] 0 A � [l o 1] I 0 , 1 1 Additional computation shows that 1 o 1 II The reader can now see that A[n] = A[5] for all positive integers n with n � 5. The number of bit operations used to find the Boolean product of two n x n matrices can be easily determined. EXAMPLE 12 How many bit operations are used to find A 0 B, where A and Bare n x n zero-one matrices? Solution: There are n2 entries in A 0 B. Using Algorithm 2, a total of n DRs and n ANDs are used to find an entry of A 0 B. Hence, 2n bit operations are used to find each entry. Therefore, 2n3 bit operations are required to compute A 0 B using Algorithm 2. .... Exercises 1.uth U � � n a) What size is A? b) What is the third column of A? c) What is the second row of A? d) What is the element of A in the (3, 2)th position? e) What is AI? 2. Find A + B, where a) A = [-� � o -2 i] , -3 3-89 [-I 3 -�l B= ; 2 -3 [-1 0 5 -�J. b) A = -4 -3 5 [-3 9 -3 �l B= 0 -2 -1 3. FindAB if a) A = [; �J.B=[� �J. b) h [! -ll B� [i -2 -IJ 0 2 . c) A � [ 1 ::i] _ [-' 3 2 -2 ,B -0 -1 4 -1 5 4. Find the product AB, where 1 [' 0 '] [ 0 a) A = 0 -1 -1 ,B = 1 . -1 -1 1 0 -1 b)hD -3 2 1 glB� [-: -1 -3 -1 -1 0 -2 2 c) h [ � -4 ��l B � [-� o 3 5. Find a matrix A such that 0 -2J -3 . -I] o . 1 2 -n 3 0 3 �l 4 [Hint; Finding A requires that you solve systems oflinear equations.] 6. Find a matrix A such that [1 3 2] [7 2 1 1 A= 1 4 0 3 -1 1 3] o 3 -3 7 7. Let A be an m x n matrix and let 0 be the m x n matrix that has all entries equal to zero. Show that A = 0 + A = A+O. 8. Show that matrix addition is commutative; that is, show that if A and B are both m x n matrices, then A+B=B+A. 3.8 Matrices 255 9. Show that matrix addition is associative; that is, show that if A, B, and C are all m x n matrices, then A + (B + C) = (A + B) + c. 10. Let A be a 3 x 4 matrix, B be a 4 x 5 matrix, and C be a 4 x 4 matrix. Determine which of the following products are defined and find the size of those that are defined. a) AB b) BA c) AC d) CA e) BC t) CB 11. What do we know about the sizes of the matrices A and B if both of the products AB and BA are defined? 12. In this exercise we show that matrix multiplication is dis­ tributive over matrix addition. a) Suppose that A and B are m x k matrices and that C is a k x n matrix. Show that (A + B)C = AC + BC. b) Suppose that C is an m x k matrix and that A and Bare k x n matrices. Show that C(A + B) = CA + CB. 13. In this exercise we show that matrix multiplication is associative. Suppose that A is an m x p matrix, B is a p x k matrix, and C is a k x n matrix. Show that A(BC) = (AB)C. 14. The n x n matrix A = [aij] is called a diagonal matrix if aij = 0 when i =1= j. Show that the product of two n x n diagonal matrices is again a diagonal matrix. Give a sim­ ple rule for determining this product. 15. Let Find a formula for An, whenever n is a positive integer. 16. Show that (Aty = A. 17. Let A and B be two n x n matrices. Show that a) (A + B)I = AI + BI. b) (ABY = BlAt. If A and Bare n x n matrices with AB = BA = In, then B is called the inverse of A (this terminology is appropriate be­ cause such a matrix B is unique) and A is said to be invertible. The notation B = A -\ denotes that B is the inverse of A. 18. Show that [ i ; -�] -1 -1 3 is the inverse of 19. Let A be the 2 x 2 matrix A= [� �l 256 3 I The Fundamentals: Algorithms, the Integers, and Matrices Show that if ad -be =1= 0, then 20. Let A-I = [ad �be -e ad -be [-1 2J A= 1 3 . -b ] ad � be . ad -be a) Find A-I. [Hint: Use Exercise 19.] b) Find A3. c) Find (A-I)3. d) Use your answers to (b) and (c) to show that (A -1)3 is the inverse of A3. 21. Let A be an invertible matrix. Show that (An)-I = (A -I)n whenever n is a positive integer. 22. Let A be a matrix. Show that the matrix AN is symmetric. [Hint: Show that this matrix equals its transpose with the help of Exercise 17b.] 23. Show that the conventional algorithm uses m ImZm3 mul­ tiplications to compute the product ofthe m I x mz matrix A and the mz x m3 matrix B. 24. What is the most efficient way to multiply the matrices AI, Az, and A3 with sizes a) 20 x 50,50 x 10, 10 x 40? b) 10 x 5,5 x 50,50 xl? 25. What is the most efficient way to multiply the matrices A I, Az, A3, and � if the dimensions of these matrices are 10 x 2, 2 x 5, 5 x 20, and 20 x 3, respectively? 26. a) Show that the system of simultaneous linear equations allxl + a12xZ + ... + alnxn = h a21xI + azzxz + ... + aznxn = bz in the variables XI, Xz, ... ,Xn can be expressed as AX = B, where A = [aij], X is an n x 1 matrix with Xi the entry in its ith row, and B is an n x 1 matrix with bi the entry in its ith row. b) Show that if the matrix A = [aij] is invertible (as de­ fined in the preamble to Exercise 18), then the solution of the system in part (a) can be found using the equa­ tion X = A-lB. 27. Use Exercises 18 and 26 to solve the system 7xI -8X2 + 5X3 = 5 -4xI + 5Xl -3X3 = -3 XI -Xl +X3 = 0 3-90 28. Let A= [� �J and B= [� �l Find a) A v B. b) A A B. c) AOB. 29. Let A� [i 0 �] B� [! 1 lJ 1 and 0 0 0 Find a) A v B. b) AA B. c) AOB. 30. Find the Boolean product of A and B, where [' 0 0 1] A= 0 I 0 1 1 I and 31. Let h[i 0 !J 0 I Find a) A[l]. c) A v A[l] v A[3]. 32. Let A be a zero-one matrix. Show that a) Av A=A. b) AAA= A. 33. In this exercise we show that the meet and join opera­ tions are commutative. Let A and B be m x n zero-one matrices. Show that a) A v B = B v A. b) B A A = A A B. 34. In this exercise we show that the meet and join opera­ tions are associative. Let A, B, and C be m x n zero-one matrices. Show that a) (A v B) v C = A v (B v C). b) (A A B) A C = A A (B A C). 35. We will establish distributive laws of the meet over the join operation in this exercise. Let A, B, and C be m x n zero-one matrices. Show that a) A v (B A C) = (A v B) A (A v C). b) A A (B v C) = (A A B) v (A A C). 36. Let A be an n x n zero-one matrix. Let I be the n x n identity matrix. Show that A 0 I = lOA = A. 37. In this exercise we will show that the Boolean prod­ uct of zero-one matrices is associative. Assume that A is an m x p zero-one matrix, B is a p x k zero-one matrix, and C is a k x n zero-one matrix. Show that AO(BOC) = (AOB)OC. 3-9/ Key Terms and Results TERMS algorithm: a finite set of precise instructions for performing a computation or solving a problem searching algorithm: the problem of locating an element in a list linear search algorithm: a procedure for searching a list element by element binary search algorithm: a procedure for searching an or­ dered list by successively splitting the list in half sorting: the reordering of the elements of a list into nonde­ creasing order greedy algorithm: an algorithm that makes the best choice at each step I(x) is O(g(x»: the fact that I/(x)1 ::::: Clg(x)1 for all x > k for some constants C and k witness to the relationship I(x) is O(g(x»: a pair C and k such that I/(x)1 ::::: Clg(x)1 whenever x > k I(x) is O(g(x»: the fact that I/(x)1 :::: Clg(x)1 for all x > k for some positive constants C and k I(x) is 8(g(x»: the fact that I(x) is O(g(x» and I(x) is Q(g(x» time complexity: the amount of time required for an algorithm to solve a problem space complexity: the amount of storage space required for an algorithm to solve a problem worst-case time complexity: the greatest amount of time re­ quired for an algorithm to solve a problem of a given size average-case time complexity: the average amount of time required for an algorithm to solve a problem of a given size a I b (a divides b): there is an integer c such that b = ac prime: a positive integer greater than I with exactly two pos­ itive integer divisors composite: a positive integer greater than 1 that is not prime Mersenne prime: a prime of the form 2P -I, where p is prime gcd(a, b) (greatest common divisor of a and b): the largest integer that divides both a and b relatively prime integers: integers a and b such that gcd(a, b) = 1 pairwise relatively prime integers: a set of integers with the property that every pair of these integers is relatively prime Icm(a, b) (least common multiple of a and b): the smallest positive integer that is divisible by both a and b a mod b: the remainder when the integer a is divided by the positive integer b a = b (mod m) (a is congruent to b modulo m): a -b is divisible by m encryption: the process of making a message secret decryption: the process of returning a secret message to its original form n = (akak-\ ••• a\aO)b: the base b representation of n binary representat ion: the base 2 representation of an integer hexadecimal representatio n: the base 16 representation of an integer Key Terms and Results 257 octal representation: the base 8 representation of an integer linear combination of a and b with integer coefficients: a number of the form sa + tb, where sand t are integers inverse of a modulo m: an integer 7i such that aa == I (mod m) linear congruence: a congruence ofthe form ax == b (mod m ), where x is a variable pseudoprime to the base 2: a composite integer n such that 2n-\ == I (mod n) pseudoprime to the base b: a composite integer n such that bn-l == 1 (mod n) Carmichael number: a composite integer n such that n is a pseudoprime to the base b for all positive integers b with gcd(b, n) = I private key encryption: encryption where both encryption keys and decryption keys must be kept secret public key encryption: encryption where encryption keys are public knowledge, but decryption keys are kept secret matrix: a rectangular array of numbers matrix addition: see page 247 matrix mUltiplicatio n: see page 248 In (identity matrix of order n): the n x n matrix that has entries equal to 1 on its diagonal and Os elsewhere A' (transpose of A): the matrix obtained from A by inter­ changing the rows and columns symmetric: a matrix is symmetric if it equals its transpose zero-one matrix: a matrix with each entry equal to either 0 or I A V B (the join of A and B): see page 252 A 1\ B (the meet of A and B): see page 252 A 0 B (the Boolean product of A and B): see page 253 RESULTS linear and binary search algorithms: (given in Section 3.1) bubble sort: a sorting that uses passes where successive items are interchanged if they are out of order insertion sort: a sorting that at the jth step inserts the jth el­ ement into the correct position in the list of the sorted first j -I elements The linear search has O(n) complexity. The binary search has O(logn) complexity. The bubble and insertion sorts have O(n2) complexity. logn! is O(n logn). If II (x) is O(gl(X» and hex) is O(g2(X», then (Ji + h)(x) is o (max(g\ (x), g2(X») and (ftf2)(X) is O(gl (x) g2(X ». If ao, aI, ... ,an are real numbers with an =I 0, then anxn + an_lXn-l + ... + alX + ao is O(xn) and 8(xn). Fundamental Theorem of Arithmetic: Every positive inte­ ger can be written uniquely as the product of primes, where the prime factors are written in order of increasing size. division algorithm: Let a and d be integers with d positive. Then there are unique integers q and r with 0 ::::: r < d such that a = dq +r. If a and b are positive integers, then ab = gcd(a, b)-lcm(a, b). 258 3 / The Fundamentals: Algorithms, the Integers, and Matrices Euclidean algorith m: for finding greatest common divisors (see Algorithm 6 in Section 3.6) Let b be a positive integer greater than 1. Then if n is a positive integer, it can be expressed uniquely in the form n = akbk + ak_lbk-l + ... + alb + ao. The algorithm for finding the base b expansion of an integer (see Algorithm 1 in Section 3.6) The conventional algorithms for addition and multiplication of integers (given in Section 3.6) The modular exponentiation algorithm (see Algorithm 5 in Section 3.6) Review Questions 1. a) Define the term algorithm. b) What are the different ways to describe algorithms? c) What is the difference between an algorithm for solv­ ing a problem and a computer program that solves this problem? 2. a) Describe, using English, an algorithm for finding the largest integer in a list of n integers. b) Express this algorithm in pseudocode. c) How many comparisons does the algorithm use? 3. a) State the definition of the fact that f(n) is o (g(n », where f(n) and g(n) are functions from the set of positive integers to the set of real numbers. b) Use the definition of the fact that fen) is O(g(n» directly to prove or disprove that n2 + 18n + 107 is O(n3). c) Use the definition of the fact that fen) is O(g(n» directly to prove or disprove that n3 is O(n2 + 18n + 107). 4. a) How can you produce a big-O estimate for a function that is the sum of different terms where each term is the product of several functions? b) Give a big-O estimate for the function fen) = (n! + 1)(2n + 1) + (nn-2 + 8nn-3)(n3 + 2n). For the functiong in your estimate f(x) is o (g(x» use a sim­ ple function of smallest possible order. 5. a) Define what the worst-case time complexity, average­ case time complexity, and best-case time complexity (in terms of comparisons) mean for an algorithm that finds the smallest integer in a list of n integers. b) What are the worst-case, average-case, and best-case time complexities, in terms of comparisons, ofthe al­ gorithm that finds the smallest integer in a list of n integers by comparing each of the integers with the smallest integer found so far? 6. a) Describe the linear search and binary search algorithm for finding an integer in a list of integers in increasing order. b) Compare the worst-case time complexities of these two algorithms. c) Is one of these algorithms always faster than the other (measured in terms of comparisons)? 3-92 The greatest common divisor of two integers can be expressed as a linear combination with integer coefficients of these integers. Ifm is a positive integer and gcd(a, m) = 1, then a has a unique inverse modulo m. Chinese Remainder Theorem: A system of linear congru­ ences modulo pairwise relatively prime integers has a unique solution modulo the product of these moduli. Fermat's Little Theorem: If p is prime and pIa, then aP-1 == 1 (mod p). 7. a) Describe the bubble sort algorithm. b) Use the bubble sort algorithm to sort the list 5, 2, 4, 1,3. c) Give a big-O estimate for the number of comparisons used by the bubble sort. 8. a) Describe the insertion sort algorithm. b) Use the insertion sort algorithm to sort the list 2,5, 1, 4,3. c) Give a big-O estimate for the number of comparisons used by the insertion sort. 9. a) Explain the concept of a greedy algorithm. b) Provide an example of a greedy algorithm that pro­ duces an optimal solution and explain why it produces an optimal solution. c) Provide an example of a greedy algorithm that does not always produce an optimal solution and explain why it fails to do so. 10. State the Fundamental Theorem of Arithmetic. 11. a) Describe a procedure for finding the prime factoriza­ tion of an integer. b) Use this procedure to find the prime factorization of 80,707. 12. a) Define the greatest common divisor of two integers. b) Describe at least three different ways to find the great­ est common divisor of two integers. When does each method work best? c) Find the greatest common divisor of 1,234,567 and 7,654,321. d) Find the greatest common divisor of 2335577911 and 2937557313. 13. a) Define what it means for a and b to be congruent mod­ ulo 7. b) Which pairs of the integers -11, -8, -7, -1,0,3, and 17 are congruent modulo 7? c) Show that if a and b are congruent modulo 7, then lOa + 13 and -4b + 20 are also congruent modulo 7. 14. Describe a procedure for converting decimal (base 10) expansions of integers ipto hexadecimal expansions. 15. a) How can you find a linear combination (with integer coefficients) of two integers that equals their greatest common divisor? 3-93 b) Express gcd(84, 119) as a linear combination of 84 and 119. 16. a) What does it mean for a to be an inverse of a modulo m? b) How can you find an inverse of a modulo m when m is a positive integer and gcd(a, m) = I? c) Find an inverse of7 modulo 19. 17, a) How can an inverse of a modulo m be used to solve the congruence ax == b (mod m) when gcd(a, m) = I? b) Solve the linear congruence 7x == 13 (mod 19). 18. a) State the Chinese Remainder Theorem. b) Find the solutions to the system x == 1 (mod 4), x == 2 (mod 5), and x == 3 (mod 7). 19. Suppose that 2n-1 == 1 (modn). Is n necessarily prime? Supplementar y Exercises 1. a) Describe an algorithm for locating the last occurrence of the largest number in a list of integers. b) Estimate the number of comparisons used. 2. a) Describe an algorithm for finding the first and second largest elements in a list of integers. b) Estimate the number of comparisons used. 3. a) Give an algorithm to determine whether a bit string contains a pair of consecutive zeros. b) How many comparisons does the algorithm use? 4. a) Suppose that a list contains integers that are in order oflargest to smallest and an integer can appear repeat­ edly in this list. Devise an algorithm that locates all occurrences of an integer x in the list. b) Estimate the number of comparisons used. 5. a) Adapt Algorithm 1 in Section 3.1 to find the maxi­ mum and the minimum of a sequence of n elements by employing a temporary maximum and a temporary minimum that is updated as each successive element is examined. b) Describe the algorithm from part (a) in pseudocode. c) How many comparisons of elements in the sequence are carried out by this algorithm? (Do not count com­ parisons used to determine whether the end of the se­ quence has been reached.) 6. a) Describe in detail the steps of an algorithm that finds the maximum and minimum of a sequence of n el­ ements by examining pairs of successive elements, keeping track of a temporary maximum and a tempo­ rary minimum. If n is odd, both the temporary maxi­ mum and temporary minimum should initially equal the first term, and if n is even, the temporary minimum and temporary maximum should be found by compar­ ing the initial two elements. The temporary maximum and temporary minimum should be updated by com-Supplementary Exercises 259 20. a) What is the difference between a public key and a private key cryptosystem? b) Explain why using shift ciphers is a private key system. c) Explain why the RSA cipher system is a public key system. 21. Define the product oftwo matrices A and B. When is this product defined? 22. a) How many different ways are there to evaluate the product AIA2A3A! by successively multiplying pairs of matrices, when this product is defined? b) Suppose that AI, A2, A3, and A! are 10 x 20,20 x 5, 5 x 10, and lOx 5 matrices, respectively. How should AIA2A3A! be computed to use the least number of multiplications of entries? paring them with the maximum and minimum of the pair of elements being examined. b) Express the algorithm described in part (a) in pseudo­ code. c) How many comparisons of elements of the sequence are carried out by this algorithm? (Do not count com- o pari sons used to determine whether the end of the se­ quence has been reached.) How does this compare to the number of comparisons used by the algorithm in Exercise 5? *7. Show that the worst-case complexity in terms of compar­ isons of an algorithm that finds the maximum and mini­ mum ofn elements is at least f3n/21 -2. 8. Devise an efficient algorithm for finding the second largest element in a sequence of n elements and deter­ mine the worst-case complexity of your algorithm. The shaker sort (or bidirectional bubble sort) successively compares adjacent pairs of elements, exchanging them ifthey are out of order, and alternately passing through the list from the beginning to the end and then from the end to the beginning until no exchanges are needed. 9. Show the steps used by the shaker sort to sort the list 3, 5,1,4,6,2. 10. Express the shaker sort in pseudocode. 11. Show thatthe shaker sort has 0 (n2) complexity measured in terms of the number of comparisons it uses. 12. Explain why the shaker sort is efficient for sorting lists that are already in close to the correct order. 13. Show that (n logn + n2)3 is O(n6). 14. Show that 8x3 + 12x + 100 log x is O(x3). 15. Give a big-O estimate for (x2 + x(logx)3). (2X + x3). 16. Find a big-O estimate for 2:J=1 JU + 1). *17. Showthatn! is not O(2n). 260 3 / The Fundamentals: Algorithms, the Integers, and Matrices �18. Show that nn is not O(n I). 19. Find four numbers congruent to 5 modulo 17. 20. Show that if a and d are positive integers, then there are integers q and r such that a = d q + r where -d 12 < r ::::: d12. �1. Show that if ae == be (mod m), then a == b (mod mid), where d = gcd(m, e). �2. How many zeros are at the end of the binary expansion of IOOIO!? 23. Use the Euclidean algorithm to find the greatest common divisor of 10,223 and 33,341. 24. How many divisions are required to find gcd(144, 233) using the Euclidean algorithm? 25. Find gcd(2n + I, 3n + 2), where n is a positive integer. [Hint: Use the Euclidean algorithm.] 26. a) Show that if a and b are positive integers with a ::: b, then gcd(a, b) = a if a = b, gcd(a, b) = 2 gcd(aI2, b12) if a and b are even, gcd(a, b) = gcd(aI2, b) if a is even and b is odd, and gcd(a, b) = gcd(a - b, b) if both a and b are odd. b) Explain how to use (a) to construct an algorithm for computing the greatest common divisor of two posi­ tive integers that uses only comparisons, subtractions, and shifts of binary expansions, without using any divisions. c) Find gcd(1202, 4848) using this algorithm. 27. Show that an integer is divisible by 9 if and only if the sum of its decimal digits is divisible by 9. 28. Prove that if n is a positive integer such that the sum of the divisors of n is n + I, then n is prime. 29. Adapt the proof that there are infinitely many primes (The­ orem 3 in Section 3.5) to show that are infinitely many primes ofthe form 6k + 5 where k is a positive integer. 30. a) Explain why n div 7 equals the number of weeks in n days. b) Explain why n div 24 equals the number of days in n hours. A set of integers is called mutually relatively prime if the greatest common divisor of these integers is 1. 31. Determine whether these sets of integers are mutually rel­ atively prime. a) 8,10,12 c) 15,21,28 b) 12,15,25 d) 21,24,28,32 32. Find a set of four mutually relatively prime integers such that no two of them are relatively prime. 33. a) Suppose that messages are encrypted using the func­ tion f(p) = (ap + b) mod 26 such that gcd(a, 26) = 1. Determine a function that can be used to decrypt messages. b) The encrypted version of a message is LJMKG MGMXF QEXMW Ifit was encrypted using the func­ tion f(p) = (7 p + 10) mod 26, what was the original message? 34. Show that the system of congruences x == 2 (mod 6) and x == 3 (mod 9) has no solutions. 3-94 35. Find all solutions of the system of congruences x == 4 (mod 6) and x == 13 (mod IS). *36. a) Show that the system of congruences x == al (mod m I) and x == a2 (mod m2) has a solution if and only if gcd(ml,m2) I al -a2· b) Show that the solution in part (a) is unique modulo lcm(ml, m2). *37. Prove that if f(x) is a nonconstant polynomial with inte­ ger coefficients, then there is an integer y such that f(y) is composite. [Hint: Assume that f(xo) = p is prime. Show that p divides f(xo + kp) for all integers k. Obtain a con­ tradiction ofthe fact that a polynomial of degree n, where n > 1, takes on each value at most n times.] * *38. Show that if a and b are positive irrational numbers such that 1 I a + 1 I b = 1, then every positive integer can be uniquely expressed as either L ka J or L kb J for some posi­ tive integer k. 39. Show that Goldbach's conjecture, which states that every even integer greater than 2 is the sum of two primes, is equivalent to the statement that every integer greater than 5 is the sum ofthree primes. 40. Prove that there are no solutions in integers x and y to the equation x2 - 5 y2 = 2. [Hint: Consider this equation modulo 5.] 41. Find An if A is 42. Show that if A = cI, where e is a real number and I is the n x n identity matrix, then AB = BA whenever B is an n x n matrix. 43. Show that if A is a 2 x 2 matrix such that AB = BA when­ ever B is a 2 x 2 matrix, then A = cI, where e is a real number and I is the 2 x 2 identity matrix. An n x n matrix is called upper triangular if aij = 0 when­ everi > j. 44. From the definition of the matrix product, devise an algo­ rithm for computing the product of two upper triangular matrices that ignores those products in the computation that are automatically equal to zero. 45. Give a pseudocode description of the algorithm in Exer­ cise 44 for multiplying two upper triangular matrices. 46. How many multiplications of entries are used by the al­ gorithm found in Exercise 44 for multiplying two n x n upper triangular matrices? 47. Show that if A and B are invertible matrices and AB exists, then (AB)-I = B-1 A-I. 48. What is the best order to form the product ABCD if A, B, C, and D are matrices with dimensions 30 x 10, lOx 40, 40 x 50, and 50 x 30, respectively? Assume that the number of multiplications of entries used to mul­ tiply a p x q matrix and a q x r matrix is pqr. 49. Let A be an n x n matrix and let 0 be the n x n matrix all of whose entries are zero. Show that the following are true. 3-95 a) A <:) 0 = 0 <:) A = 0 b) A v 0 = 0 v A = A c) A /\ 0 = 0 /\ A = 0 50. Showthatifthe denominations of coins are co, cl, ... , ck, where k is a positive integer and c is a positive integer, c > 1, the greedy algorithm always produces change us­ ing the fewest coins possible. Computer Projects Write programs with these inputs and outputs. 1. Given a list of n integers, find the largest integer in the list. 2. Given a list of n integers, find the first and last occurrences of the largest integer in the list. 3. Given a list of n distinct integers, determine the position of an integer in the list using a linear search. 4. Given an ordered list of n distinct integers, determine the position of an integer in the list using a binary search. 5. Given a list of n integers, sort them using a bubble sort. 6. Given a list of n integers, sort them using an insertion sort. 7. Given an integer n, use the greedy algorithm to find the change for n cents using quarters, dimes, nickels, and pennies. 8. Given an ordered list of n integers and an integer x, find the number of comparisons used to determine the position of an integer in the list using a linear search and using a binary search. 9. Given a list of integers, determine the number of compar­ isons used by the bubble sort and by the insertion sort to sort this list. 10. Given a positive integer, determine whether it is prime. 11. Given a message, encrypt this message using the Caesar cipher; and given a message encrypted using the Caesar cipher, decrypt this message. 12. Given two positive integers, find their greatest common divisor using the Euclidean algorithm. 13. Given two positive integers, find their least common multiple. *14. Given a positive integer, find the prime factorization of this integer. Compu tations and Explorations Computer Projects 261 51. Devise an algorithm for guessing a number between 1 and 2n - 1 by successively guessing each bit in its binary expansion. 52. Determine the complexity, in terms of the number of guesses, needed to determine a number between 1 and 2n - 1 by successively guessing the bits in its binary expansion. 15. Given a positive integer and a positive integer b greater than 1, find the base b expansion of this integer. 16. Given the positive integers a, b, and m > 1, find ab modm. 17. Given a positive integer, find the Cantor expansion of this integer (see the preamble to Exercise 46 of Section 3.6). 18. Given a positive integer n, a modulus m, multiplier a, increment c, and seed xo, with 0 .:s a < m, 0 .:s c < m, and 0 .:s Xo < m, generate the sequence of n pseudo­ random numbers using the linear congruential generator Xn+1 = (axn + c) mod m. 19. Given positive integers a and b, find integers sand t such that sa + tb = gcd(a, b). 20. Given n linear congruences modulo pairwise relatively prime moduli, find the simultaneous solution ofthese con­ gruences modulo the product of these moduli. 21. Given an m x k matrix A and a k x n matrix B, find AB. 22. Given a square matrix A and a positive integer n, find An. 23. Given a square matrix, determine whether it is symmetric. 24. Given an nl x n2 matrix A, an n2 x n3 matrix B, an n3 x n4 matriJi. C, and an n4 x n5 matrix D, all with inte­ ger entries, determine the most efficient order to multiply these matrices (in terms of the number of multiplications and additions of integers). 25. Given two m x n Boolean matrices, find their meet and join. 26. Given an m x k Boolean matrix A and a k x n Boolean matrix B, find the Boolean product of A and B. 27. Given a square Boolean matrix A and a positive integer n, find A[n). Use a comput ational program or programs you have written to do these exercises. 1. We know that nb is O(dn) when b and d are positive num­ bers with d :::: 2. Give values ofthe constants C and k such that nb .:s C dn whenever x > k for each of these sets of values: b = 10, d = 2; b = 20, d = 3; b = 1000, d = 7. 2. Compute the change for different values of n with coins of different denominations using the greedy al­ gorithm and determine whether the smallest number of coins was used. Can you find conditions so that the 262 3 / The Fundamentals: Algorithms, the Integers, and Matrices greedy algorithm is guaranteed to use the fewest coins possible? 3. Using a generator of random orderings of the integers I, 2, ... , n, find the number of comparisons used by the bubble sort, insertion sort, binary insertion sort, and selection sort to sort these integers. 4. Determine whether 2P -I is prime for each ofthe primes not exceeding 100. 5. Test a range of large Mersenne numbers 2P -I to de­ termine whether they are prime. (You may want to use software from the GIMPS project.) 6. Look for polynomials in one variables whose values at long runs of consecutive integers are all primes. 7. Find as many primes of the form n2 + I where n is a Writing Projects 3-96 positive integer as you can. It is not known whether there are infinitely many such primes. 8. Find 10 different primes each with 100 digits. 9. How many primes are there less than 1,000,000, less than 10,000,000, and less than 100,000,000? Can you propose an estimate for the number of primes less than x where x is a positive integer? 10. Find a prime factor of each of 10 different 20-digit odd in­ tegers, selected at random. Keep track of how long it takes to find a factor of each ofthese integers. Do the same thing for 10 different 30-digit odd integers, 10 different 40-digit odd integers, and so on, continuing as long as possible. 11. Find all pseudoprimes to the base 2, that is, composite integers n such that 2n-1 == I (mod n), where n does not exceed 10,000. Respond to these questions with essays using outside sources. 1. Examine the history of the word algorithm and describe the use of this word in early writings. 2. Look up Bachmann's original introduction of big-O no­ tation. Explain how he and others have used this notation. 3. Explain how sorting algorithms can be classified into a taxonomy based on the underlying principle on which they are based. 4. Describe the radix sort algorithm. 5. Describe what is meant by a parallel algorithm. Explain how the pseudocode used in this book can be extended to handle parallel algorithms. 6. Explain how the complexity of parallel algorithms can be measured. Give some examples to illustrate this concept, showing how a parallel algorithm can work more quickly than one that does not operate in parallel. 7. Describe the Lucas-Lehmer test for determining whether a Mersenne number is prime. Discuss the progress of the GIMPS project in finding Mersenne primes using this test. 8. Explain how probabilistic primality tests are used in prac­ tice to produce extremely large numbers that are almost certainly prime. Do such tests have any potential draw­ backs? 9. The question of whether there are infinitely many Carmichael numbers was solved recently after being open for more than 75 years. Describe the ingredients that went into the proof that there are infinitely many such numbers. 10. Summarize the current status of factoring algorithms in terms oftheir complexity and the size of numbers that can currently be factored. When do you think that it will be feasible to factor 200-digit numbers? 11. Describe the algorithms that are actually used in modern computers to add, subtract, multiply, and divide positive integers. 12. Describe the history of the Chinese Remainder Theorem. Describe some of the relevant problems posed in Chi­ nese and Hindu writings and how the Chinese Remainder Theorem applies to them. 13. When are the numbers of a sequence truly random num­ bers, and not pseudorandom? What shortcomings have been observed in simulations and experiments in which pseudorandom numbers have been used? What are the properties that pseudorandom numbers can have that ran­ dom numbers should not have? 14. Describe how public key cryptography is being applied. Are the ways it is applied secure given the status of fac­ toring algorithms? Will information kept secure using public key cryptography become insecure in the future? 15. Describe how public key cryptography can be used to send signed secret messages so that the recipient is rela­ tively sure the message was sent by the person claiming to have sent it. 16. Show how a congruence can be used to tell the day ofthe week for any given date. 17. Describe some of the algorithms used to efficiently mul­ tiply large integers. 18. Describe some of the algorithms used to efficiently mul­ tiply large matrices. 19. Describe the Rabin public key cryptosystem, explaining how to encrypt and how to decrypt messages and why it is suitable for use as a public key cryptosystem. CHAPTER 4.1 Mathematical Induction 4.2 Strong Induction and Well-Ordering 4.3 Recursive Definitions and Structural Induction 4.4 Recursive Algorithms 4.5 Program Correctness Induction and Recursion Many mathematical statements assert that a property is true for all positive integers. Examples of such statements are that for every positive integer n: n! ::: nn, n3 -n is divisible by 3; a set with n elements has 2n subsets; and the sum of the first n positive integers is n(n + 1)/2. A major goal of this chapter, and the book, is to give the student a thorough understanding of mathematical induction, which is used to prove results of this kind. Proofs using mathematical induction have two parts. First, they show that the statement holds for the positive integer 1. Second, they show that if the statement holds for a positive integer then it must also hold for the next larger integer. Mathematical induction is based on the rule of inference that tells us that if P(I) and Vk(P(k) � P(k + 1» are true for the domain of positive integers, then V P(n) is true. Mathematical induction can be used to prove a tremendous variety of results. Understanding how to read and construct proofs by mathematical induction is a key goal of learning discrete mathemati cs. In Chapters 2 and 3 we explicitly defined sets and functions. That is, we described sets by listing their elements or by giving some property that characterizes these elements. We gave formulae for the values of functions. There is another important way to define such objects, based on mathematical induction. To define functions, some initial terms are specified, and a rule is given for finding subsequent values from values already known. Sets can be defined by listing some of their elements and giving rules for constructing elements from those already known to be in the set. Such definitions, called recursive definitions, are used throughout discrete mathematics and computer science. Once we have defined a set recursively, we can use a proof method called structural induction to prove results about this set. When a procedure is specified for solving a problem, this procedure must always solve the problem correctly. Just testing to see that the correct result is obtained for a set of input values does not show that the procedure always works correctly. The correctness of a procedure can be guaranteed only by proving that it always yields the correct result. The final section of this chapter contains an introduction to the techniques of program verification. This is a formal technique to verify that procedures are correct. Program verification serves as the basis for attempts under way to prove in a mechanical fashion that programs are correct. 4.1 Mathematical Induction 4-/ Introduction Suppose that we have an infinite ladder, as shown in Figure 1, and we want to know whether we can reach every step on this ladder. We know two things: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of the ladder, then we can reach the next rung. Can we conclude that we can reach every rung? By (1), we know that we can reach the first rung of the ladder. Moreover, because we can reach the first rung, by (2), we can also reach the second rung; it is the next rung after the first rung. Applying (2) again, because we can reach the second rung, we can also reach the third rung. Continuing in this way, we can show that we can reach the fourth rung, the fifth rung, and so on. For example, after 100 uses of (2), we know that we can reach the 101 st rung. But can we conclude that we are able to reach every rung of this 263 264 4 / Induction and Recursion 4-2 Assessmenl � FIGURE 1 Climbing an Infinite Ladder. infinite ladder? The answer is yes, something we can verify using an important proof technique called mathematical inductio n. That is, we can show that P(n) is true for every positive integer n, where P(n) is the statement that we can reach the nth rung of the ladder. Mathematical induction is an extremely important proof technique that can be used to prove assertions of this type. As we will see in this section and in subsequent sections of this chapter and later chapters, mathematical induction is used extensively to prove results about a large variety of discrete objects. For example, it is used to prove results about the complexity of algorithms, the correctness of certain types of computer programs, theorems about graphs and trees, as well as a wide range of identities and inequalities. In this section, we will describe how mathematical induction can be used and why it is a valid proof technique. It is extremely important to note that mathematical induction can be used only to prove results obtained in some other way. It is not a tool for discovering formulae or theorems. Mathematical Induction In general, mathematical induction* can be used to prove statements that assert that P(n) is true for all positive integers n, where P(n) is a propositional function. A proof by mathe­ matical induction has two parts, a basis step, where we show that P(l) is true, and an in­ ductive step, where we show that for all positive integers k, if P(k) is true, then P(k + 1) is true. ·Unfortunately. using the terminology "mathematical induction" clashes with the terminology used to describe different types of reasoning. In logic, deductive reasoning uses rules of inference to draw conclusions from premises, whereas inductive reasoning makes conclusions only supported, but not ensured, by evidence. Mathematical proofs, including arguments that use mathematical induction, are deductive, not inductive. 4-3 unkS� 4.1 Mathematical Induction 265 PRINCIPLE OF MATHEMATICAL INDUCTION To prove that Pen) is true for all pos­ itive integers n, where P(n) is a propositional function, we complete two steps: BASIS STEP: We verify that pel) is true. INDUCTIVE STEP: We show that the conditional statement P(k) -+ P(k + 1) is true for all positive integers k. To complete the inductive step of a proof using the principle of mathematical induction, we assume that P(k) is true for an arbitrary positive integer k and show that under this assumption, P(k + 1) must also be true. The assumption that P(k) is true is called the inductive hypothesis. Once we complete both steps in a proof by mathematical induction, we have shown that Pen) is true for all positive integers, that is, we have shown that Yn P(n) is true where the quantification is over the set of positive integers. In the inductive step, we show that Yk(P(k) -+ P(k + 1)) is true, where again, the domain is the set of positive integers. Expressed as a rule of inference, this proof technique can be stated as [P(1) A Y k(P(k) -+ P(k + 1))] -+ Y nP(n), when the domain is the set of positive integers. Because mathematical induction is such an important technique, it is worthwhile to explain in detail the steps of a proof using this technique. The first thing we do to prove that Pen) is true for all positive integers n is to show that pel) is true. This amounts to showing that the particular statement obtained when n is replaced by 1 in Pen) is true. Then we must show that P(k) -+ P(k + 1) is true for every positive integer k. To prove that this conditional statement is true for every positive integer k, we need to show that P(k + 1) cannot be false when P(k) is true. This can be accomplished by assuming that P(k) is true and showing that under this hypothesis P(k + 1) must also be true. Remark: In a proofby mathematical induction it is not assumed that P(k) is true for all positive integers! It is only shown that ifi! is assumed that P(k) is true, then P(k + 1) is also true. Thus, a proof by mathematical induction is not a case of begging the question, or circular reasoning. When we use mathematical induction to prove a theorem, we first show that P(1) is true. Then we know that P(2) is true, because P(1) implies P(2). Further, we know that P(3) is true, because P(2) implies P(3). Continuing along these lines, we see that Pen) is true for every positive integer n. WAYS TO REMEMBER HOW MATHEMATICAL INDUCTION WORKS Thinking of the infinite ladder and the rules for reaching steps can help you remember how mathematical induction works. Note that statements (1) and (2) for the infinite ladder are exactly the basis step and inductive step, respective ly, of the proof that Pen) is true for all positive integers n, where P(n) is the statement that we can reach the nth rung of the ladder. Consequently, we can invoke mathema tical induction to conclude that we can reach every rung. Besides the infinite ladder, several other useful illustrations of mathematical induction can help you remember how this principle works. One ofthese involves a line of people: person one, HISTORICAL NOTE The first known use of mathematical induction is in the work of the sixteenth -century mathematician Francesco Maurolico (1494-1575). Maurolico wrote extensively on the works of classical mathematics and made many contributions to geometry and optics. In his book Arithmeticorum Libri Duo. Maurolico presented a variety of properties of the integers together with proofs of these properties. To prove some of these properties he devised the method of mathematical induction. His first use of mathematical induction in this book was to prove that the sum of the first n odd positive integers equals n2• 266 4 I Induction and Recursion ,.�""" 14 Pt",,,, /J Ptrsan 12 Persanll Per�on /0 Person 9 Person 8 Person 7 Person 6 Person 5 Person 4 P 2 Person 3 Person 1 erson FIGURE 2 People Telling Secrets. 4-4 person two, and so on. A secret is told to person one, and each person tells the secret to the next person in line, if the former person hears it. Let Pen) be the proposition that person n knows the secret. Then P(1) is true, because the secret is told to person one; P(2) is true, because person one tells person two the secret; P(3) is true, because person two tells person three the secret; and so on. By the principle of mathematical induction, every person in line learns the secret. This is illustrated in Figure 2. (Of course, it has been assumed that each person relays the secret in an unchanged manner to the next person, which is usually not true in real life.) Another way to illustrate the principle of mathema tical induction is to consider an infinite row of dominoes, labeled 1,2,3, ... , n, ... , where each domino is standing up. Let Pen) be the proposition that domino n is knocked over. If the first domino is knocked over-i.e., if P(1) is true-and if, whenever the kth domino is knocked over, it also knocks the (k + l)st domino over-i.e., if P(k) -+ P(k + 1) is true for all positive integers k-then all the dominoes are knocked over. This is illustrated in Figure 3. Examples of Proofs by Mathematical Induction Many theorems state that Pen) is true for all positive integers n, where pen) is a propositional function, such as the statement that 1 + 2 + ... + n = n(n + 1)/2 for all positive integers n or the statement that n ::::: 2n for all positive integers n. Mathematical induction is a technique FIGURE 3 Illustrating How Mathematical Induction Works Using Dominoes. 4-5 unks� EXAM PLE 1 Ex1ra� ExamPleslliiiiiiil 4.1 Mathematical Induction 267 for proving theorems of this kind. In other words, mathematical induction can be used to prove statements of the form lin P(n), where the domain is the set of positive integers. Mathematical induction can be used to prove an extremely wide variety of theorems, each of which is a statement of this form. We will use a variety of examples to illustrate how theorems are proved using mathematical induction. The theorems we will prove include summation formulae, inequalities, identities for combinations of sets, divisibility results, theorems about algorithms, and some other creative results. In later sections, we will employ mathematical induction to prove many other types of results, including the correctness of computer programs and algorithm s. Mathematical induction can be used to prove a wide variety of theorems, not just summation formulae, inequalities, and other types of examples we illustrate here. Note that there are many opportunities for error in induction proofs. We will describe some incorrect proofs by mathema tical induction at the end of this section and in the exercises. PROVING SUMMATION FORMUL AE We begin by using mathematical induction to prove several different summation formulae. As we will see, mathematical induction is particularly well suited for proving that such formulae are valid. However, summation formulae can be proven in other ways. This is not surprising because there are often different ways to prove a theorem. The major disadvantage of this use of mathematical induction is that you cannot use it to derive a summation formula. That is, you must already have the formula before you attempt to prove it by mathema tical induction. We begin by using mathematical induction to prove a formula for the sum ofthe smallest n positive integers. Show that if n is a positive integer, then n(n + 1) 1+2+···+n= . 2 Solution: Let P(n) be the proposition that the sum of the first n positive integers is n(n + 1)/2. We must do two things to prove that P(n) is true for n = 1,2, 3, .... Namely, we must show that P(I) is true and that the conditional statement P(k) implies P(k + 1) is true for k = 1,2, 3, .... . 1(1 + 1) BASIS STEP: P(1) IS true, because 1 = 2 . INDUCTIVE STEP: For the inductive hypothesis we assume that P(k) holds for an arbitrary positive integer k. That is, we assume that k(k + 1) 1+2+···+k= . 2 Under this assumption, it must be shown that P(k + 1) is true, namely, that 1 + 2 + ... + k + (k + 1) = (k + 1)[(k + 1) + 1] = (k + 1)(k + 2) 2 2 is also true. When we add k + 1 to both sides of the equation in P(k), we obtain k(k + 1) 1 + 2 + ... + k + (k + 1) = 2 + (k + 1) = k(k+ 1)+2(k+ 1) 2 (k+ 1)(k+2) 2 268 4 / Induction and Recursion 4-6 This last equation shows that P(k + 1) is true under the assumption that P(k) is true. This completes the inductive step. We have completed the basis step and the inductive step, so by mathematical induction we know that P (n) is true for all positive integers n. That is, we have proven that 1 + 2 + ... + n = n(n + 1)/2 for all positive integers n. <III As we noted, mathematical induction is not a tool for finding theorems about all positive integers. Rather, it is a proof method for proving such results once they are conjectured. In Example 2, using mathematical induction to prove a summation formula, we will both formulate and then prove a conjecture. EXAMPLE 2 Conjecture a formula for the sum of the first n positive odd integers. Then prove your conjecture using mathematical induction. Solution: The sums of the first n positive odd integers for n = 1,2,3,4,5 are I = I, 1 + 3 + 5 + 7 = 16, 1 + 3 = 4, 1 + 3 + 5 = 9, 1 + 3 + 5 + 7 + 9 = 25. From these values it is reasonable to conjecture that the sum of the first n positive odd integers is n2, that is, 1 + 3 + 5 + ... + (2n -1) = n2• We need a method to prove that this conjecture is correct, if in fact it is. Let pen) denote the proposition that the sum of the first n odd positive integers is n2• Our conjecture is that pen) is true for all positive integers. To use mathema tical induction to prove this conjecture, we must first complete the basis step; that is, we must show that P(1) is true. Then we must carry out the inductive step; that is, we must show that P(k + 1) is true when P(k) is assumed to be true. We now attempt to complete these two steps. BASIS STEP: P(l) states that the sum of the first one odd positive integer is 12. This is true because the sum of the first odd positive integer is 1. The basis step is complete. INDUCTIVE STEP: To complete the inductive step we must show that the proposition P(k) -+ P(k + I) is true for every positive integer k. To do thi s, we first assume the inductive hypothesis. The inductive hypothesis is the statement that P(k) is true, that is, I + 3 + 5 + ... + (2k -I) = k2• [Note that the kth odd positive integer is (2k - 1), because this integer is obtained by adding 2 a total of k - 1 times to 1.] To show that Vk(P(k) -+ P(k + I» is true, we must show that if P(k) is true (the inductive hypothesis), then P(k + I) is true. Note that P(k + I) is the statement that 1 + 3 + 5 + ... + (2k -1) + (2k + 1) = (k + 1)2. So, assuming that P(k) is true, it follows that 1 + 3 + 5 + ... + (2k -1) + (2k + 1) = [1 + 3 + ... + (2k -1)] + (2k + 1) = k2 + (2k + 1) = k2 + 2k + 1 =(k+l)2. This shows that P(k + 1) follows from P(k). Note that we used the inductive hypothesis P(k) in the second equality to replace the sum of the first k odd positive integers by k2. 4-7 4.1 Mathematical Induction 269 We have now completed both the basis step and the inductive step. That is, we have shown that P(1) is true and the conditional statement P(k) --+ P(k + 1) is true for all positive integers k. Consequently, by the principle of mathematical induction we can conclude that P (n) is true for all positive integers n. That is, we know that 1 + 3 + 5 + ... + (2n -1) = n2 for all positive integers n. .... Sometimes we need to show that P(n) is true for n = b, b + 1, b + 2, ... , where b is an integer other than 1. We can use mathematical induction to accomplish this as long as we change the basis step. For instance, consider Example 3, which states that a summation fo rmula is valid for all nonnegative integers, so that we need to prove that P (n) is true for n = 0, 1, 2, .... EXAMPLE 3 Use mathematical induction to show that 1 + 2 + 22 + ... + 2n = 2n+ 1 -1 for all nonnegative integers n. Solution: Let P(n) be the proposition that 1 + 2 + 22 + ... + 2n = 2n+1 -1 for the integer n. BASIS STEP: P(O) is true because 2° = 1 = 21 -1. This completes the basis step. INDUCTIVE STEP: For the inductive hypothesis, we assume that P(k) is true. That is, we assume that To carry out the inductive step using this assumption, we must show that when we assume that P(k) is true, then P(k + 1) is also true. That is, we must show that assuming the inductive hypothesis P(k). Under the assumption of P(k), we see that 1 + 2 + 22 + ... + 2k + 2k+1 = (1 + 2 + 22 + ... + 2k) + 2k+1 = (2k+1 -1) + 2k+1 = 2· 2k+1 -1 = 2k+2 -1. Note that we used the inductive hypothesis in the second equation in this string of equalities to replace 1 + 2 + 22 + ... + 2k by 2k+! -1. We have completed the inductive step. Because we have completed the basis step and the inductive step, by mathematical induction we know that P(n) is true for all nonnegative integers n. That is, 1 + 2 + ... + 2n = 2n+1 -1 for all nonnegative integers n. .... In Example 3, in the basis step we showed that P(O) is true, because the theorem we wanted to prove was of the form VnP(n), where the domain was the set of all nonnegative integers. As this example demonstrates, to use mathematical induction to show that P(n) is true for n = b, b + 1, b + 2, ... , where b is an integer other than 1, we show that P(b) is true (the basis step) and then show that the conditional statement P(k) --+ P(k + 1) is true for k = b, b + 1, b + 2, ... (the inductive step). Note that b can be negative, zero, or positive. Following the domino analogy we used earlier, imagine that we begin by knocking down the bth 270 4 / Induction and Recursion 4-8 domino (the basis step), and as each domino falls, it knocks down the next domino (the inductive step). We leave it to the reader to show that this form of induction is valid (see Exercise 79). The formula given in Example 3 is a special case of a general result for the sum of terms of a geometric progression (Theorem I in Section 2.4). We will use mathematical induction to provide an alternative proof of this formula. EXAMPLE 4 Sums of Geometric Progressions Use mathematical induction to prove this formula for the sum of a finite number of terms of a geometric progre ssion: n . arn+! -a L arJ = a + ar + ar2 + ... + arn = ----j=O r - 1 where n is a nonnegative integer. when r =f. I, Solution: To prove this formula using mathematical induction, let P(n) be the statement that the sum of the first n + 1 terms of a geometric progression in this formula is correct. BASIS STEP: P(O) is true, because ar -a r - 1 a(r -1) r -I =a. INDUCTIVE STEP: The inductive hypothesis is the statement that P(k) is true, where k is a nonnegative integer. That is, P(k) is the statement that ark+! -a a +ar +ar2 + ... +ark = ---­r - 1 To complete the inductive step we must show that if P(k) is true, then P(k + I) is also true. To show that this is the case, we first add ark+! to both sides of the equality asserted by P(k). We find that ark+! -a a +ar +ar2 + ... +ark +ark+! = +ark+! r -I Rewriting the right-hand side of this equation shows that ark+! -a ----+ark+! r -I ark+! -a ark+2 -ark+! + -----r-l r-l ark+2 -a r -1 Combining these last two equations gives ark+2 -a a + ar + ar2 + ... + ark + ark+! = ---­r-l This shows that if the inductive hypothesis P(k) is true, then P(k + 1) must also be true. This completes the inductive argument. We have completed the basis step and the inductive step, so by mathematical induction P(n) is true for all nonnegative integers n. This shows that the formula for the sum of the terms of a geometric series is correct. .... 4-9 4.1 Mathematical Induction 271 As previously mentioned, the formula in Example 3 is the case of the formula in Example 4 with a = 1 and r = 2. The reader should verify that putting these values for a and r into the general formula gives the same formula as in Example 3. PROVING INEQU ALITIES Mathematical induction can be used to prove a variety of in­ equalities that hold for all positive integers greater than a particular positive integer, as Examples 5-7 illustrate. EXAM PLE 5 Use mathematical induction to prove the inequality Extra� n < 2n Examples ..... for all positive integers n. Solution: Let P(n) be the proposition that n < 2n. BASIS STEP: P(I) is true, because 1 < 21 = 2. This completes the basis step. INDUCTIVE STEP: We first assume the inductive hypothesis that P(k) is true for the positive integer k. That is, the inductive hypothesis P(k) is the statement that k < 2k. To complete the inductive step, we need to show that if P(k) is true, then P(k + 1), which is the statement that k + I < 2k+ I, is true. That is, we need to show that if k < 2k, then k + 1 < 2k+ I. To show that this conditional statement is true for the positive integer k, we first add 1 to both sides of k < 2k, and then note that 1 ::: 2k. This tells us that This shows that P(k + 1) is true, namely, that k + 1 < 2k+l, based on the assumption that P(k) is true. The induction step is complete. Therefore, because we have completed both the basis step and the inductive step, by the principle of mathematical induction we have shown that n < 2n is true for all positive integers n. .... EXAM PLE 6 Use mathematical induction to prove that 2n < n! for every positive integer n with n ::: 4. (Note that this inequality is false for n = 1,2, and 3.) Solution: Let P(n) be the proposition that 2n < n!. BASIS STEP: To prove the inequality for n ::: 4 requires that the basis step be P(4). Note that P(4) is true, because 24 = 16 < 24 = 4! INDUCTIVE STEP: For the inductive step, we assume that P(k) is true for the positive integer k with k ::: 4. That is, we assume that 2k < k! for the positive integer k with k ::: 4. We must show that under this hypothesis, P(k + 1) is also true. That is, we must show that if2k < k! for the positive integer k where k ::: 4, then 2k+1 < (k + I)!. We have 2k+ 1 = 2 . 2k by definition of exponent < 2 . k! by the inductive hypothesis < (k + l)k! because 2 < k + I = (k + I)! by definition offactorial function. This shows that P(k + 1) is true when P(k) is true. This completes the inductive step of the proof. 272 4 / Induction and Recursion 4-/0 We have completed the basis step and the inductive step. Hence, by mathema tical induction P(n) is true for all integers n with n ::: 4. That is, we have proved that 2n < n! is true for all integers n with n ::: 4. .... An important inequality for the sum of the reciprocals of a set of positive integers will be proved in Example 7. . EXAMPLE 7 An Inequality for Harmonic Number s The harmonic numbers Hj, j = 1,2,3, ... , are defined by I I I H·=I+-+-+···+ -. } 2 3 j For instance, I I 1 25 H4 = I + -+ -+ - = -. 2 3 4 12 Use mathematical induction to show that n H2" ::: I + 2" ' whenever n is a nonnegative integer. n Solution: To carry out the proof, let P(n) be the proposition that H2" ::: I + 2". . 0 BASIS STEP: P(O) IS true, because H2o = HI = I ::: 1+2". INDUCTIVE STEP: The inductive hypothesis is the statement that P(k) is true, that is, H2k ::: I + �, where k is a nonnegative integer. We must show that if P(k) is true, then P(k + I), which states that H2k+l ::: I + k; I , is also true. So, assuming the inductive hypothesis, it fol­ lows that I I I I I H2k+l = I + - + - + ... + - + -- + ... + --2 3 2 k 2k + I 2 k+ I 1 I = H2k + -- + ... + --2k + 1 2 k+1 ( k) 1 I > 1+- + --+ ... + ­-2 2k + I 2 k+1 > 1 + -+2 .-( k) k 1 -2 2 k+1 > (1 +�) + � -2 2 k+l -1+ ---2· This establishes the inductive step of the proof. by the definition of harmon ic number by the definition of 2kth harmonic number by the inductive hypothesis because there are 2k terms each � 1 /2k+ I canceling a common factor of 2k in second term 4-11 4.1 Mathematical Induction 273 We have completed the basis step and the inductive step. Thus, by mathematical induction pen) is true for all nonnegative integers. That is, the inequality H2n ::: 1 + � for the harmonic numbers is valid for all nonnegative integers n. .... Remark: The inequality established here shows that the harmonic series 1 1 1 1+-+-+ "·+- +·,, 2 3 n is a divergent infinite series. This is an important example in the study of infinite series. PROVING DIVISIBILITY RESULTS Mathematical induction can be used to prove divisibil­ ity results about integers. Although such results are often easier to prove using basic results in number theory, it is instructive to see how to prove such results using mathematical induction, as Example 8 illustrates. EXAMPLE 8 Use mathematical induction to prove that n3 -n is divisible by 3 whenever n is a positive integer. Exam:: � Solution: To construct the proof, let P(n) denote the proposition: "n3 -n is divisible by 3." BASIS STEP: The statement P(1) is true because 13 - 1 = 0 is divisible by 3. This completes the basis step. INDUCTIVE STEP: For the inductive hypothesis we assume that P(k) is true; that is, we assume that k3 -k is divisible by 3. To complete the inductive step, we must show that when we assume the inductive hypothesis, it follows that P(k + 1), the statement that (k + 1)3 -(k + 1) is divisible by 3, is also true. That is, we must show that (k + 1)3 -(k + I) is divisible by 3. Note that (k + 1)3 -(k + 1) = (k3 + 3k2 + 3k + 1) -(k + 1) = (k3 -k) + 3(k2 + k). Because both terms in this sum are divisible by 3 (the first by the inductive hypothesis, and the second because it is 3 times an integer), it follows that (k + 1)3 -(k + 1) is also divisible by 3. This completes the inductive step. Because we have completed both the basis step and the inductive step, by the principle of mathematical induction we know that n3 -n is divisible by 3 whenever n is a positive integer. .... PROVING RESULTS ABOUT SETS Mathematical induction can be used to prove many results about sets. In particular, in Example 9 we prove a formula for the number of subsets of a finite set and in Example lOwe establish a set identity. EXAMPLE 9 The Number of Subsets of a Finite Set Use mathematical induction to show that if S is a finite set with n elements where n is a nonnegative integer, then S has 2n subsets. (We will prove this result directly in several ways in Chapter 5.) Solution: Let pen) be the proposition that a set with n elements has 2n subsets. BASIS STEP: P(O) is true, because a set with zero elements, the empty set, has exactly 2° = 1 subset, namely, itself. 274 4/ Induction and Recursion 0: FI G URE 4 Generating Subsets of a Set with k + 1 Elements. Here T = S U {a}. 4-12 INDUCTIVE STEP: For the inductive hypothesis we assume that P(k) is true for the nonnega­ tive integer k, that is, we assume that every set with k elements has 2k subsets. It must be shown that under this assumption, P (k + I), which is the statement that every set with k + I elements has 2k+ I subsets, must also be true. To show this, let T be a set with k + I elements. Then, it is possible to write T = S U {a}, where a is one of the elements of T and S = T -{a} (and hence lSI = k). The subsets of T can be obtained in the following way. For each subset X of S there are exactly two subsets of T, namely, X and X U {a}. (This is illustrated in Figure 4.) These constitute all the subsets of T and are all distinct. Because there are 2k subsets of S, there are 2 . 2k = 2k+ I subsets of T. This finishes the inductive argument. Because we have completed the basis step and the inductive step, by mathematical induction it follows that P(n) is true for all nonnegative integers n. That is, we have proved that a set with n elements has 2n subsets whenever n is a nonnegative integer. <III EXAMPLE 10 Use mathematical induction to prove the following generalization of one of De Morgan's laws: whenever A I, A2, ••• , An are subsets of a universal set U and n :::: 2. Solution: Let P(n) be the identity for n sets. BASIS STEP: The statement P(2) asserts that Al n A2 = � U A2. This is one of De Morgan's laws; it was proved in Section 2.2. INDUCTIVE STEP: The inductive hypothesis is the statement that P(k) is true, where k is an integer with k :::: 2; that is, it is the statement that k k nAj = U Aj j=1 j=1 whenever AI, A2, ••• , Ak are subsets of the universal set U. To carry out the inductive step, we need to show that this assumption implies that P(k + 1) is true. That is, we need to show that 4-13 4.1 Mathematical Induction 275 if this equality holds for every collection of k subsets of U, then it must also hold for every collection of k + 1 subsets of U. Suppose that A I, A 2, •.• , Ak, AH I are subsets of U. When the inductive hypot hesis is assumed to hold, it follows that = (n Aj) U Ak+ I by De Morgan's law (where the two sets are n� � I Aj and Ak+ I) } =1 � (�I A j) U Ak+ I by Ih, induolive hypolh"i' HI = U Aj by the definition of union. j=1 This completes the inductive step. Because we have completed both the basis step and the inductive step, by mathematical induction we know that P(n) is true whenever n is a positive integer, n � 2. That is, we know that whenever AI, A2, ••• , An are subsets of a universal set U and n � 2. PROVING RESULTS ABOUT ALG ORITHMS Next, we provide an example (somewhat more difficult than previous examples) that illustrates one of many ways mathematical induction is used in the study of algorithms. We will show how mathematical induction can be used to prove that a greedy algorithm yields an optimal solution. (For an introduction to greedy algorithms, see Section 3.1.) EXAMPLE 11 Suppose we have a group of proposed talks with preset times. We would like to schedule as many of these lectures as possible in the main lecture hall. How can we schedule the lectures? One approach is to use a greedy algorithm to schedule a subset of the m proposed talks tl, t2, ... , tm in the main lecture hall. Suppose that talk tj begins at time bj and ends at time ej. (No two lectures can proceed at the same time, but a lecture can begin at the same time another one ends.) We assume thatthe talks are listed in order of nondecreasing ending time, so that el .:s e2 .:s . . . .:s em. The greedy algorithm proceeds by selecting at each stage a talk with the earliest ending time among all those talks that begin after all talks already scheduled end. Note that a lecture with an earliest end time is always selected first by the algorithm. We will show that this greedy algorithm is optimal in the sense that it always schedules the most talks possible in the main lecture hall. To prove the optimality of this algorithm we use mathematical induction on the variable n, the number of talks scheduled by the algorithm . We let P(n) be the proposition that if the greedy algorithm schedules n talks, then it is not possible to schedule more than n talks. BASIS STEP: Suppose that the greedy algorithm managed to schedule just one talk, tl, in the main lecture hall. This means that every other talk cannot start after el, the ending time of tl. Otherwise, the first such talk we come to as we go through the talks in order of nondecreasing 276 4/ Induction and Recursion 4-14 EXAMPLE 12 Ex1ra� Examples ..... end time could be added. Hence, at time el each of the remaining talks needs to use the lecture hall because they all start at or before el and end after el. It follows that no two talks can be scheduled because both need to use the lecture hall at time el. This shows that P (I) is true and completes the basis step. INDUCTIVE STEP: The inductive hypothesis is that P(k) is true, where k is a positive integer, that is, that the greedy algorithm always schedules the most possible talks when it selects k talks, where k is a positive integer, given any set of talks, no matter how large. We must show that P(k + 1) follows from the assumption that P(k) is true, that is, we must show that under the assumption of P(k), the greedy algorithm always schedules the most possible talks when it selects k + 1 talks. Now suppose that the algorithm has selected k + I talks. Our first step in completing the inductive step is to show there is a schedule including the most talks possible that contains talk t\, a talk with the earliest end time . This is easy to see because a schedule that begins with the talk tj in the list, where i > 1, can be changed so that talk t\ replaces talk tj. To see this, note that because e\ ::: ej, all talks that were scheduled to follow talk ti can still be scheduled. Once we included talk t\, scheduling the talks so that as many as possible are scheduled is reduced to scheduling as many talks as possible that begin at or after time el. So, if we have scheduled as many talks as possible, the schedule of talks other than talk t\ is an optimal schedule of the original talks that begin once talk tl has ended. Because the greedy algorithm schedules k talks when it creates this schedule, we can apply the inductive hypothesis to conclude that it has scheduled the most pos sible talks. It follows that the greedy algorithm has scheduled the most possible talks, k + 1, when it produced a schedule with k + 1 talks, so P(k + 1) is true. This completes the inductive step. We have completed the basis step and the inductive step. So, by mathematical induction we know that P (n ) is true for all positive integers n. This completes the proof of optimalit y. That is, we have proved that when the greedy algorithm schedules n talks, when n is a positive integer, then it is not possible to schedule more than n talks. <l1li CREATIVE USES OF MATHEMATICAL INDUCTION Mathematical induction can often be used in unexpected ways. We will illustrate two particularly clever uses of mathema tical induction here, the first relating to survivors in a pie fight and the second relating to tilings with regular triominoes of checkerboards with one square missing. Odd Pie Fights An odd number of people stand in a yard at mutually distinct distances. At the same time each person throws a pie at their nearest neighbor, hitting this person. Use mathematical induction to show that there is at least one survivor, that is, at least one person who is not hit by a pie. (This problem was introduced by Carmony [Ca79]. Note that this result is false when there are an even number of people; see Exercise 71.) Solution: Let P(n) be the statement that there is a survivor whenever 2n + 1 people stand in a yard at distinct mutual distances and each person throws a pie at their nearest neighbor. To prove this result, we will show that P(n) is true for all positive integers n. This follows because as n runs through all positive integers, 2n + 1 runs through all odd integers greater than or equal to 3. Note that one person cannot engage in a pie fight because there is no one else to throw the pie at. BASIS STEP: When n = 1, there are 2n + 1 = 3 people in the pie fight. Of the three people, suppose that the closest pair are A and B, and C is the third person. Because distances between pairs of people are different, the distance between A and C and the distance between Band Care both different from, and greater than, the distance between A and B. It follows that A and B throw pies at each other, while C throws a pie at either A or B, whichever is closer. Hence, C is not hit by a pie. This shows that at least one of the three people is not hit by a pie, completing the basis step. 4-15 LinkS� EXAMPLE 13 FIGURE 5 A Right Triomino. 4.1 Mathematical Induction 277 IND U CTIVE STEP: For the inductive step, assume that P (k) is true. That is, assume thatthere is at least one survivor whenever 2k + I people stand in a yard at distinct mutual distances and each throws a pie at their nearest neighbor. We must show that if the inductive hypothesis P(k) is true, then P(k + 1), the statement that there is at least one survivor whenever 2(k + I) + 1 = 2k + 3 people stand in a yard at distinct mutual distances and each throws a pie at their nearest neighbor, is also true. So suppose that we have 2(k + I) + I = 2k + 3 people in a yard with distinct distance between pairs of people. Let A and B be the closest pair of people in this group of 2k + 3 people. When each person throws a pie at the nearest person, A and B throw pies at each other. If someone else throws a pie at either A or B, then altogether at least three pies are thrown at A and B, and at most (2k + 3) -3 = 2k pies are thrown at the remaining 2k + I people. This guarantees that at least one person is a survivor, for if each of these 2k + I people was hit by at least one pie, a total of at least 2k + 1 pies would have to be thrown at them. (The reasoning used in this last step is an example of the pigeonhole principle discussed further in Section 5.2.) To complete the inductive step, suppose no one else throws a pie at either A or B. Besides A and B, there are 2k + I people. Because the distances between pairs of these people are all different, we can use the inductive hypothesis to conclude that there is at least one survivor S when these 2k + 1 people each throw pies at their nearest neighbor. Furthermore, S is also not hit by either the pie thrown by A or the pie thrown by B because A and B throw their pies at each other, so S is a survivor because S is not hit by any of the pies thrown by these 2k + 3 people. This completes the inductive step and proves that P(n) is true for all positive integers n. We have completed both the basis step and the inductive step. So by mathematical induction it follows that Pen) is true for all positive integers n. We conclude that whenever an odd number of people located in a yard at distinct mutual distances each throw a pie at their nearest neighbor, there is at least one survivor. .... In Section 1.7 we discussed the tiling of checkerboards by polyominoes. Example 13 illus­ trates how mathematical induction can be used to prove a result about covering checkerboards with right triomino es, pieces shaped like the letter "L." Let n be a positive integer. Show that every 2n x 2n checkerboard with one square removed can be tiled using right triominoes, where these pieces cover three squares at a time, as shown in Figure 5. Solution: Let P(n) be the proposition that every 2n x 2n checkerboard with one square removed can be tiled using right triomino es. We can use mathematical induction to prove that Pen) is true for all positive integers n. BASIS STEP: pel) is true, because each of the four 2 x 2 ch�ckerboards with one square removed can be tiled using one right triomino, as shown in Figure 6. INDUCTIVE STEP: The inductive hypothesis is the assumption that P(k) is true for the positive integer k; that is, it is the assumption that every 2k x 2k checkerboard with one square removed can be tiled using right triominoes. It must be shown that under the assumption of the inductive FIGUR E 6 Tiling 2 X 2 Checkerboards with One Squar e Removed. 278 4 / Induction and Recursion • FIGURE 7 Dividing a 2k+ 1 X 2k+ 1 Checkerboard into Four 2k X 2k Checkerboards. 4-16 • FIGURE 8 Tiling the 2k+ 1 X 2k+ 1 Checkerboard with One Square Removed. hypothesis, P(k + 1) must also be true; that is, any 2k+! x 2k+! checkerboard with one square removed can be tiled using right triominoes. To see this, consider a 2k+! x 2k+! checkerboard with one square removed. Split this checkerboard into four checkerboards of size 2k x 2k, by dividing it in half in both direc­ tions. This is illustrated in Figure 7. No square has been removed from three of these four checkerboar ds. The fourth 2k x 2k checkerboard has one square removed, so by the inductive hypothesis, it can be covered by right triominoes. Now temporarily remove the square from each of the other three 2k x 2k checkerboards that has the center of the original, larger checkerboard as one of its comers, as shown in Figure 8. By the inductive hypothesis, each of these three 2k x 2k checkerboards with a square removed can be tiled by right triominoes. Furthermore, the three squares that were temporarily removed can be covered by one right triomino. Hence, the entire 2k+! x 2k+! checkerboard can be tiled with right triominoes. We have completed the basis step and the inductive step. Therefore, by mathematical in­ duction P(n) is true for all positive integers n. This shows that we can tile every 2n x 2n checkerbo ard, where n is a positive integer, with one square removed, using right triominoes. ... Why Mathematical Induction is Valid Why is mathematical induction a valid proof technique? The reason comes from the well­ ordering property listed in Appendix 1 as an axiom for the set of positive integers. Suppose we know that P (1) is true and that the proposition P (k) -+ P (k + 1) is true for all positive integers k. To show that Pen) must be true for all positive integers n, assume that there is at least one positive integer for which P(n) is false. Then the set S of positive integers for which P(n) is false is nonempty. Thus, by the well-ordering property, S has a least element, which will be denoted by m. We know that m cannot be 1, because P(1) is true. Because m is positive and greater than 1, m -1 is a positive integer. Furthermore, because m -1 is less than m, it is not in S, so P(m -1) must be true. Because the conditional statement P(m -1) -+ P(m) is also true, it must be the case that P(m) is true. This contradicts the choice of m. Hence, P(n) must be true for every positive integer n. Errors in Proofs Using Mathematical Induction As in every proof method, there are many opportunities for making errors. In every proof using mathematical induction, we must complete a correct basis step and a correct inductive step. 4-17 4.1 Mathematical Induction 279 Note that it is sometimes difficult to locate the error in a faulty proofby mathematical induction, as Example 14 illustrates. EXAMPLE 14 Find the error in this "proof" of the clearly false claim that every set oflines in the plane, no two of which are parallel, meet in a common point. "Proof": Let Pen) be the statement that every set ofn lines in the plane, no two of which are parallel, meet in a common point. We will attempt to prove that Pen) is true for all positive integers n ::: 2. BASIS STEP: The statement P(2) is true because any two lines in the plane that are not parallel meet in a common point (by the definition of parallel lines ). INDUCTIVE STEP: The inductive hypothesis is the statement that P(k) is true for the positive integer k, that is, it is the assumption that every set of k lines in the plane, no two of which are parallel, meet in a common point. To complete the inductive step, we must show that if P(k) is true, then P(k + 1) must also be true. That is, we must show that if every set of k lines in the plane, no two of which are paral lel, meet in a common point, then every set of k + 1 lines in the plane, no two of which are parallel, meet in a common point. So, consider a set of k + 1 distinct lines in the plane. By the inductive hypothesis, the first k of these lines meet in a common point PI. Moreover, by the inductive hypothesis, the last k of these lines meet in a common point P2. We will show that PI and P2 must be the same point. If PI and P2 were different points, all lines containing both of them must be the same line because two points determine a line. This contradicts our assumption that all these lines are distinct. Thus, PI and P2 are the same point. We conclude that the point PI = P2 lies on all k + 1 lines. We have shown that P(k + 1) is true assuming that P( k) is true. That is, we have shown that if we assume that every k, k ::: 2, distinct lines meet in a common point, then every k + 1 distinct lines meet in a common point. This completes the inductive step. We have completed the basis step and the inductive step, and supposedly we have a correct proof by mathematical induction. Solution: Examining this supposed proof by mathematical induction it appears that everything is in order. However, there is an error, as there must be. The error is rather subtle. Carefully looking at the inductive step shows that this step requires that k ::: 3. We cannot show that P(2) implies P(3). When k = 2, our goal is to show that every three distinct lines meet in a common point. The first two lines must meet in a common point PI and the last two lines must meet in a common point P2. But in this case, PI and P2 do not have to be the same, because only the second line is common to both sets of lines. Here is where the inductive step fails. .... Exercises 1. There are infinitely many stations on a train route. Sup­ pose that the train stops at the first station and suppose that if the train stops at a station, then it stops at the next station. Show that the train stops at all stations. 2. Suppose that you know that a golfer plays the first hole of a golf course with an infinite number of holes and that if this golfer plays one hole, then the golfer goes on to play the next hole. Prove that this golfer plays every hole on the course. D se mathematical induction in Exercises 3-17 to prove sum­ mation formulae. 3. Let pen) be the statement that 12 + 22 + ... + n2 = n(n + 1)(2n + 1)/6 for the positive integer n. a) What is the statement pel)? b) Show that pel) is true, completing the basis step of the proof. c) What is the inductive hypothesis? d) What do you need to prove in the inductive step? e) Complete the inductive step. 1) Explain why these steps show that this formula is true whenever n is a positive integer. 4. Let pen) be the statement that 13 + 23 + ... + n3 = (n(n + 1)/2)2 for the positive integer n. a) What is the statement pel) ? b) Show that pel) is true, completing the basis step of the proof. 280 4 / Induction and Recursion c) What is the inductive hypothesis? d) What do you need to prove in the inductive step? e) Complete the inductive step. 1) Explain why these steps show that this formula is true whenever n is a positive integer. 5. Prove that 12 + 32 + 52 + ... + (2n + 1)2 = (n + I) (2n + 1)(2n + 3)/3 whenever n is a nonnegative integer. 6. Prove that 1·1!+2·2!+···+n·n!=(n+I)!-1 whenever n is a positive integer. 7. Prove that 3 + 3·5 + 3.52+ ... + 3· 5n=3(5n+l-1)/4 whenever n is a nonnegative integer. 8. Prove that 2 -2 . 7 + 2 . 72 -... + 2(-7t = (1 - (_7)n+1 )/4 whenever n is a nonnegative integer. 9. a) Find a formula for the sum of the first n even positive integers. b) Prove the formula that you conjectured in part (a). 10. a) Find a formula for II I -+ -+ ... + ----:--".,. 1·22·3 n(n+I) by examining the values of this expression for small values ofn. b) Prove the formula you conjectured in part (a). 11. a) Find a formula for I I 1 1 -+- + -+ ... +-2 4 8 2n by examining the values of this expression for small values ofn. b) Prove the formula you conjectured in part (a). 12. Prove that n ( I)J L -- J=O 2 whenever n is a nonnegative integer. 13. Prove that 12 -22 + 32 -... + (_I)n-In2 = (_1)n-1 n(n + 1)/2 whenever n is a positive integer. 14. Prove that for every positive integer n, LZ = I k2k = (n -I)2n+1 + 2. 15. Prove that for every positive integer n, 1 ·2 + 2·3 + ... + n(n + 1) = n(n + I)(n + 2)/3. 16. Prove that for every positive integer n, 1 ·2·3 + 2·3·4 + ... + n(n + 1)(n + 2) = n(n + I)(n + 2)(n + 3)/4. 17. Prove that LJ=lj4=n(n+I)(2n+I)(3n2+3n-I)/ 30 whenever n is a positive integer. Use mathematical induction to prove the inequalities in Exercises 18-30. 18. Let P(n) be the statement that n! < nn, where n is an integer greater than 1. a) What is the statement P(2)? b) Show that P(2) is true, completing the basis step of the proof. 4-18 c) What is the inductive hypothesis? d) What do you need to prove in the inductive step? e) Complete the inductive step. 1) Explain why these steps show that this inequality is true whenever n is an integer greater than 1. 19. Let P(n) be the statement that 1 1 1 1 1 + - + - + ... + -< 2 --, 4 9 n2 n where n is an integer greater than 1. a) What is the statement P(2)? b) Show that P(2) is true, completing the basis step of the proof. c) What is the inductive hypothesis? d) What do you need to prove in the inductive step? e) Complete the inductive step. 1) Explain why these steps show that this inequality is true whenever n is an integer greater than I. 20. Prove that 3n < n! if n is an integer greater than 6. 21. Prove that 2n > n2 if n is an integer greater than 4. 22. For which nonnegative integers n is n2 ::: n!? Prove your answer. 23. For which nonnegative integers n is 2n + 3 ::: 2n? Prove your answer. 24. Prove that 1/(2n) ::: [1 ·3·5···· . (2n -1)]/(2·4····· 2n) whenever n is a positive integer. *25. Prove that if h > -1, then 1 + nh ::: (1 + h)n for all non­ negative integers n. This is called Bernoulli's inequality. *26. Suppose that a and b are real numbers with 0 < b < a. Prove that if n is a positive integer, then an -bn ::: nan-I(a -b). *27. Prove that for every positive integer n, 1 1 1 1 + - + - + ... + -> 2(.Jn+f - 1) . .j2 y'3 .jfi 28. Prove that n2 -7n + 12 is nonnegative whenever n is an integer with n � 3. In Exercises 29 and 30, Hn denotes the nth harmonic number. *29. Prove that H2n ::: 1 + n whenever n is a nonnegative integer. *30. Prove that HI + H2 + ... + Hn = (n + I)Hn -n. Use mathematical induction in Exercises 31-37 to prove di­ visibility facts. 31. Prove that 2 divides n2 + n whenever n is a positive integer. 32. Prove that 3 divides n3 + 2n whenever n is a positive integer. 33. Prove that 5 divides n5 -n whenever n is a nonnegative integer. 34. Prove that 6 divides n3 -n whenever n is a nonnegative integer. 4-19 '35. Prove that n2 -1 is divisible by 8 whenever n is an odd positive integer. '36. Prove that 21 divides 4n+1 + 52n-1 whenever n is a posi­ tive integer. '37. Prove that if n is a positive integer, then 133 divides lln+1 + 122n-l. Use mathematical induction in Exercises 38-46 to prove re­ sults about sets. 38. Prove that if AI, A2, ... , An and BI, B2, ... , Bn are sets such that Aj � Bj for j = 1,2, ... , n, then n n U Aj � U Bj. j=1 j=1 39. Prove that if AI, A2, ... , An and BI, B2, ... , Bn are sets such that Aj � Bj for j = 1,2, ... , n, then n n n Aj � n Bj. j=1 j=1 40. Prove that if AI, A2, ... , An and B are sets, then (AI n A2 n ... nAn) U B = (AI U B) n (A2 U B) n ... n (An U B). 41. Prove that if A I, A2, ... , An and B are sets, then (AI U A2 U ... U An) n B = (AI n B) U (A2 n B) U .. · U (An n B). 42. Prove that if AI, A2,"" An and B are sets, then (AI -B) n (A2 -B) n .. · n (An -B) = (AI n A2 n .. · nAn) - B. 43. Prove that if AI, A2, ... , An are subsets ofa universal set U, then n n U Ak = n Ak· k=1 k=1 44. Prove that if A I, A2, ... , An and B are sets, then (AI -B) U (A2 - B) U ... U (An -B) = (AI U A2 U· .. U An) -B. 45. Prove that a set with n elements has n(n -1)/2 subsets containing exactly two elements whenever n is an integer greater than or equal to 2. *46. Prove that a set with n elements has n(n -I)(n -2)/6 subsets containing exactly three elements whenever n is an integer greater than or equal to 3. Exercises 47-49 present incorrect proofs using mathemati­ cal induction. You will need to identify an error in reasoning in each exercise. 47. What is wrong with this "proof" that all horses are the same color? Let P(n) be the proposition that all the horses in a set of n horses are the same color. Basis Step: Clearly, P(I) is true. Inductive Step: Assume that P(k) is true, so that all the horses in any set of k horses are the same color. 4.1 Mathematical Induction 281 Consider any k + 1 horses; number these as horses 1,2,3, ... ,k, k + 1. Now the first k of these horses all must have the same color, and the last k of these must also have the same color. Because the set of the first k horses and the set of the last k horses overlap, all k + 1 must be the same color. This shows that P(k + 1) is true and finishes the proof by induction. 48. What is wrong with this "proof"? "Theorem " For every positive integer n, L:7 = Ii = (n + 1)2/2. Basis Step: The formula is true for n = 1. Inductive Step: Suppose that L:7=1 i = (n + 1)2/2. Then L:7:/ i = (L:7=1 i) + (n + 1). By the inductive hypothesis, 'L7:/ i = (n + 1)2/2 + n + 1 = (n2 + n + �)/2 + n + 1 = (n2 + 3n + �)/2 = (n+�)2/2= [en + 1) + 1]2/2, completing the inductive step. 49. What is wrong with this "proof"? "Theorem" For every positive integer n, if x and yare positive integers with max(x, y) = n, then x = y. Basis Step: Suppose that n = 1. If max(x, y) = I and x and yare positive integers, we have x = I and y = I. Inductive Step: Let k be a positive integer. Assume that whenever max(x, y) = k and x and yare positive inte­ gers, then x = y. Now let max(x, y) = k + I, where x and y are positive integers. Then max(x -I, Y -I) = k, so by the inductive hypothesis, x - I = Y - I. It follows that x = y, completing the inductive step. 50. Use mathematical induction to show that given a set of n + I positive integers, none exceeding 2n, there is at least one integer in this set that divides another integer in the set. *51. A knight on a chessboard can move one space horizon­ tally (in either direction) and two spaces vertically (in either direction) or two spaces horizontally (in either di­ rection) and one space vertically (in either direction). Suppose that we have an infinite chessboard, made up of all squares (m, n) where m and n are nonnegative in­ tegers. Use mathematical induction to show that a knight starting at (0, 0) can visit every square using a finite se­ quence of moves. [Hini: Use induction on the variable s = m +n.] 52. Suppose that A = [� �l where a and b are real numbers. Show that for every positive integer n. 282 4 / Induction and Recursion 53. (Requires calculus) Use mathematical induction to prove that the derivative of f(x) = xn equals nxn-I whenever n is a positive integer. (For the inductive step, use the product rule for derivatives.) 54. Suppose that A and B are square matrices with the prop­ erty AB = BA. Show that ABn = Bn A for every positive integer n. 55. Suppose that m is a positive integer. Use mathematical induction to prove that if a and b are integers with a == b (mod m), then ak == bk (mod m) whenever k is a nonneg­ ative integer. 56. Use mathematical induction to show that -'(PI v P2 V ... v Pn) is equivalent to -'PI /\ -'P2 /\ ... /\ -'Pn when­ ever Ph P2, ... , Pn are propositions. *57. Show that [(PI --+ P2) /\ (P2 --+ P3) /\ ... /\ (Pn-I --+ Pn)] --+ [(PI /\ P2 /\ ... /\ Pn-I) --+ Pn] is a tautology whenever PI , P2, ... , Pn are propositions, where n ::: 2. *58. Show that n lines separate the plane into (n2 + n + 2)/2 regions if no two of these lines are parallel and no three pass through a common point. **59. Let aI, a2, ... , an be positive real numbers. The arith­ metic mean of these numbers is defined by A = (al +az + ... +an)/n, and the geometric mean of these numbers is defined by G = (alaz ... an)l/n. Use mathematical induction to prove that A ::: G. 60. Use mathematical induction to prove Lemma 2 of Section 3.6, which states that if P is a prime and P I alaz ... an, where ai is an integer for i = 1,2,3, ... , n, then pi ai for some integer i. 61. Show that if n is a positive integer, then (Here the sum is over all nonempty subsets of the set of the n smallest positive integers.) *62. Use the well-ordering property to show that the following form of mathematical induction is a valid method to prove that P(n) is true for all positive integers n. Basis Step: P(l) and P(2) are true. Inductive Step: For each positive integer k, if P(k) and P(k + 1) are both true, then P(k + 2) is true. 63. Show that if A I, Az, ... , An are sets where n ::: 2, and for all pairs of integers i and j with 1 .:s i < j .:s n either Ai is a subset of of Aj or Aj is a subset of Ai, then there is an integer i, 1 .:s i .:s n such that Ai is a subset of Aj for all integers j with 1 .:s j .:s n. *64. A guest at a party is a celebrity if this person is known by every other guest, but knows none ofthem. There is at most one celebrity at a party, for if there were two, they 4-20 would know each other. A particular party may have no celebrity. Your assignment is to find the celebrity, if one exists, at a party, by asking only one type of question­ asking a guest whether they know a second guest. Ev­ eryone must answer your questions truthfully. That is, if Alice and Bob are two people at the party, you can ask Alice whether she knows Bob; she must answer correctly. Use mathematical induction to show that if there are n people at the party, then you can find the celebrity, ifthere is one, with 3(n -1) questions. [Hint: First ask a ques­ tion to eliminate one person as a celebrity. Then use the inductive hypothesis to identify a potential celebrity. Fi­ nally, ask two more questions to determine whether that person is actually a celebrity.] Suppose there are n people in a group, each aware of a scandal no one else in the group knows about. These people commu­ nicate by telephone; when two people in the group talk, they share information about all scandals each knows about. For example, on the first call, two people share information, so by the end of the call, each ofthese people knows about two scandals. The gossip problem asks for G(n), the minimum number of telephone calls that are needed for all n people to learn about all the scandals. Exercises 65--67 deal with the gossip problem. 65. Find G(l), G(2), G(3), and G(4). 66. Use mathematical induction to prove that G(n) .:s 2n -4 for n ::: 4. [Hint: In the inductive step, have a new person call a particular person at the start and at the end.] **67. Prove that G(n) = 2n -4 for n ::: 4. *68. Show that it is possible to arrange the numbers I, 2, ... , n in a row so that the average of any two of these numbers never appears between them. [Hint: Show that it suffices to prove this fact when n is a power of 2. Then use math­ ematical induction to prove the result when n is a power of2.] *69. Show that if II, /z, ... , In is a collection of open intervals on the real number line, n ::: 2, and every pair of these in­ tervals has a nonempty intersection, that is, Ii n I j =1= 13 whenever 1 .:s i .:s n and 1 .:s j .:s n, then the intersection of all these sets is nonempty, that is, II n /z n ... n In =1= 13. (Recall that an open interval is the set of real numbers x with a < x < b, where a and b are real numbers with a < b.) Sometimes we cannot use mathematical induction to prove a result we believe to be true, but we can use mathematical induction to prove a stronger result. Because the inductive hy­ pothesis of the stronger result provides more to work with, this process is called inductive loading. We use inductive loading in Exercise 70. 70. Suppose that we want to prove that 1 3 2n - 1 I -.-... --- <--2 4 2n v'3ii for all positive integers n. 4-21 a) Show that if we try to prove this inequality using math­ ematical induction, the basis step works, but the induc­ tive step fails. b) Show that mathematical induction can be used to prove the stronger inequality I 3 2n - I I -.- ... -- <---;:::== 2 4 2n ,J3ri+1 for al1 integers greater than 1, which, together with a verification for the case where n = I, establishes the weaker inequality we original1y tried to prove using mathematical induction. 4.2 Strong Induction and Well-Ordering 283 74. Prove or disprove that al1 checkerboards of these shapes can be completely covered using right triominoes when­ ever n is a positive integer. a) 3 x 2n c) 3n x 3n b) 6 x 2n d) 6n x 6n *75. Show that a three-dimensional 2n x 2n x 2n checker­ board with one 1 x 1 x 1 cube missing can be completely covered by 2 x 2 x 2 cubes with one 1 x 1 x 1 cube removed. *76. Show that an n x n checkerboard with one square re­ moved can be completely covered using right triominoes ifn > 5, n is odd, and 3 f n. 71. Let n be an even positive integer. Show that when n peo­ ple stand in a yard at mutual1y distinct distances and each person throws a pie at their nearest neighbor, it is possible that everyone is hit by a pie. 77. Show that a 5 x 5 checkerboard with a comer square re­ moved can be tiled using right triominoes. *78. Find a 5 x 5 checkerboard with a square removed that cannot be tiled using right triominoes. Prove that such a tiling does not exist for this board. 72. Construct a tiling using right triominoes of the 4 x 4 checkerboard with the square in the upper left comer removed. 73. Construct a tiling using right triominoes of the 8 x 8 checkerboard with the square in the upper left comer removed. 79. Use the principle of mathematical induction to show that pen) is true for n = b, b + 1, b + 2, ... , where b is an integer, if PCb) is true and the conditional statement P(k) ---+ P(k + 1) is true for al1 positive integers k with k?:. b. 4.2 Strong Induction and Well-Or dering Introduction In Section 4.1 we introduced mathema tical induction and we showed how to use it to prove a variety of theorems. In this section we will introduce another form of mathematical induction, called strong induction, which can often be used when we cannot easily prove a result using mathematical induction. The basis step of a proof by strong induction is the same as a proof of the same result using mathema tical induction. That is, in a strong induction proof that Pen) is true for all positive integers n, the basis step shows that P (I) is true. However, the inductive steps in these two proof methods are different. In a proof by mathematical induction, the inductive step shows that if the inductive hypothesis P(k) is true, then P(k + 1) is also true. In a proof by strong induction, the inductive step shows that if PU) is true for all positive integers not exceeding k, then P(k + 1) is true. That is, for the inductive hypothesis we assume that PU) is true for j = 1, 2, ... , k. The validity of both mathematical induction and strong induction follow from the well­ ordering property in Appendix 1. In fact, mathematical induction, strong induction, and well­ ordering are all equivalent principles. That is, the validity of each can be proved from either of the other two. This means that a proof using one of these two principles can be rewritten as a proof using either of the other two principles. Just as it is sometimes the case that it is much easier to see how to prove a result using strong induction rather than mathematical induction, it is sometimes easier to use well-ordering than one ofthe two forms of mathematical induction. In this section we will give some examples of how the well-ordering property can be used to prove theorems. Strong Induction Before we illustrate how to use strong induction, we state this principle again. 284 4 / Induction and Recursion 4-22 STRONG INDUCTION To prove that Pen) is true for all positive integers n, where Pen) is a propositional function, we complete two steps: BASIS STEP: We verify that the proposition P(l) is true. INDUCTIVE STEP: Weshowthatthe conditional statement [P(1) /\ P(2) /\ ... /\ P(k)] --+ P(k + 1) is true for all positive integers k. Note that when we use strong induction to prove that Pen) is true for all positive integers n, our inductive hypothesis is the assumption that PU) is true for j = 1,2, ... , k. That is, the inductive hypothesis includes all k statements P(1), P(2), ... , P(k). Because we can use all k statements P(1), P(2), ... , P(k) to prove P(k + 1), rather than just the statement P(k) as in a proof by mathematical induction, strong induction is a more flexible proof technique. You may be surprised that mathema tical induction and strong induction are equivalent. That is, each can be shown to be a valid proof technique assuming that the other is valid. In particula r, any proof using mathematical induction can also be considered to be a proofby strong induction because the inductive hypothesis of a proof by mathematical induction is part of the inductive hypothesis in a proof by strong induction. That is, if we can complete the inductive step of a proof using mathematical induction by showing that P (k + 1) follows from P (k) for every positive integer k, then it also follows that P(k + 1) follows from all the statements P(l), P(2), ... , P(k), because we are assuming that not only P(k) is true, but also more, namely, that the k -1 statements P(1), P(2), ... , P(k -1) are true. However, it is much more awkward to convert a proof by strong induction into a proof using the principle of mathematical induction. (See Exercise 42.) Strong induction is sometimes called the second principle of mathematical induction or complete induction. When the terminology "complete induction" is used, the principle of mathematical induction is called incomplete induction, a technical term that is a somewhat unfortunate choice because there is nothing incomplete about the principle of mathematical induction; after all, it is a valid proof technique. STRONG INDUCT ION AND THE INFINITE LADDER To better understand strong in­ duction, consider the infinite ladder in Section 4.1. Strong induction tells us that we can reach all rungs if 1. we can reach the first rung, and 2. for every integer k, if we can reach all the first k rungs, then we can reach the (k + l)st rung. That is, if P(n) is the statement that we can reach the nth rung of the ladder, by strong induction we know that P(n) is true for all positive integers n, because (1) tells us P(1) is true, completing the basis step and (2) tells us that P(1) /\ P(2) /\ ... /\ P(k) implies P(k + 1), completing the inductive step. Example 1 illustrates how strong induction can help us prove a result that cannot easily be proved using the principle of mathematical induction. EXAMPLE 1 Suppose we can reach the first and second rungs of an infinite ladder, and we know that if we can reach a rung, then we can reach two rungs higher. Can we prove that we can reach every rung using the principle of mathematical induction? Can we prove that we can reach every rung using strong induction? Solution: We first try to prove this result using the principle of mathematical induction. 4-23 4.2 Strong Induction and Well-Ordering 285 BASIS STEP: The basis step of such a proof holds; here it simply verifies that we can reach the first rung. ATTEMPTED INDUCTIVE STEP: The inductive hypothesis is the statement that we can reach the kth rung of the ladder. To complete the inductive step, we need to show that if we assume the inductive hypothesis for the positive integer k, namely, if we assume that we can reach the kth rung of the ladder, then we can show that we can reach the (k + l)st rung of the ladder. However, there is no obvious way to complete this inductive step because we do not know from the given information that we can reach the (k + 1 )st rung from the kth rung. After all, we only know that if we can reach a rung we can reach the rung two higher. Now consider a proof using strong induction. BASIS STEP: The basis step is the same as before; it simply verifies that we can reach the first rung. INDUCTIVE STEP: The inductive hypothesis states that we can reach each of the first k rungs. To complete the inductive step, we need to show that if we assume that the inductive hypothesis is true, that is, if we can reach each of the first k rungs, then we can reach the (k + 1 )st rung. We already know that we can reach the second rung. We can complete the inductive step by noting that as long as k > 2, we can reach the (k + 1 )st rung from the (k -1 )st rung because we know we can climb two rungs from a rung we can already reach. This completes the inductive step and finishes the proofby strong induction. We have proved that if we can reach the first two rungs of an infinite ladder and for every integer k if we can reach all the first k rungs then we can reach the (k + l)st rung, then we can reach all rungs of the ladder. .... Examples of Proofs Using Strong Induction Now that we have both mathematical induction and strong induction, how do we decide which method to apply in a particular situation? Although there is no cut-and-dried answer, we can supply some useful pointers. In practice, you should use mathematical induction when it is straightforward to prove that P(k) --+ P(k + 1) is true for all positive integers k. This is the case for all the proofs in the examples in Section 4.1. In general, you should restrict your use of the principle of mathematical induction to such scenarios. Unless you can clearly see that the inductive step of a proof by mathematical induction goes through, you should attempt a proof by strong induction. That is, use strong induction and not mathematical induction when you see how to prove that P(k + 1) is true from the assumption that PU) is true for all positive integers j not exceeding k, but you cannot see how to prove that P(k + 1) follows fromjust P(k). Keep this in mind as you examine the proofs in this section. For each of these proofs, consider why strong induction works better than mathematical induction. We will illustrate how strong induction is employed in Examples 2-5. In these examples, we will prove a diverse collection of results. Pay particular attention to the inductive step in each of these examples, where we show that a result P(k + 1) follows under the assumption that PU) holds for all positive integers j not exceeding k, where P(n) is a propositional function. We begin with one of the most prominent uses of strong induction, the part of the Fun­ damental Theorem of Arithmetic that tells us that every positive integer can be written as the product of primes. EXAMPLE 2 Show that if n is an integer greater than 1, then n can be written as the product of primes. Extra� Examples IiiiiiI Solution: Let P (n) be the proposition that n can be written as the product of primes. 286 4 / Induction and Recursion 4-24 BASIS STEP: P(2) is true, because 2 can be written as the product of one prime, itself. [Note that P(2) is the first case we need to establish.] INDUCTIVE STEP: The inductive hypothesis is the assumption that PU) is true for all positive integers j with j ::: k, that is, the assumption that j can be written as the product of primes when­ ever j is a positive integer at least 2 and not exceeding k. To complete the inductive step, it must be shown that P (k + 1) is true under this assumption, that is, that k + 1 is the product of primes. There are two cases to consider, namely, when k + 1 is prime and when k + 1 is composite. If k + I is prime, we immediately see that P(k + 1) is true. Otherwise, k + I is composite and can be written as the product of two positive integers a and b with 2 ::: a ::: b < k + I. By the inductive hypothesis, both a and b can be written as the product of primes. Thus, if k + 1 is composite, it can be written as the product of primes, namely, those primes in the factorization of a and those in the factorization of b. .... Remark: Because I can be thought of as the empty product of no primes, we could have started the proof in Example 2 with P (I) as the basis step. We chose not to do this because many people find this confusing. Example 2 completes the proof of the Fundamental Theorem of Arithmetic, which asserts that every nonnegative integer can be written uniquely as the product of primes in nondecreasing order. We showed in Section 3.5 (see page 233) that an integer has at most one such factorization into primes. Example 2 shows there is at least one such factorization. Next, we show how strong induction can be used to prove that a player has a winning strategy in a game. EXAMPLE 3 Consider a game in which two players take turns removing any positive number of matches they want from one of two piles of matches. The player who removes the last match wins the game. Show that if the two piles contain the same number of matches initially, the second player can always guarantee a win. Solution: Let n be the number of matches in each pile. We will use strong induction to prove Pen), the statement that the second player can win when there are initially n matches in each pile. BASIS STEP: When n = 1, the first player has only one choice, removing one match from one of the piles, leaving a single pile with a single match, which the second player can remove to win the game. INDUCTIVE STEP: The inductive hypothesis is the statement that PU) is true for all j with 1 ::: j ::: k, that is, the assumption that the second player can always win whenever there are j matches, where 1 ::: j ::: k in each ofthe two piles at the start of the game. We need to show that P (k + 1) is true, that is, that the second player can win when there are initially k + 1 matches in each pile, under the assumption that PU) is true for j = 1,2, ... , k. So suppose that there are k + 1 matches in each of the two piles at the start of the game and suppose that the first player removes r matches (1 ::: r ::: k) from one of the piles, leaving k + 1 -r matches in this pile. By removing the same number of matches from the other pile, the second player creates the situation where there are two piles each with k + 1 -r matches. Because 1 ::: k + 1 -r ::: k, the second player can always win by the inductive hypothesis. We complete the proof by noting that if the first player removes all k + I matches from one of the piles, the second player can win by removing all the remaining matches. .... Using the principle of mathematical induction, instead of strong induction, to prove the results in Examples 2 and 3 is difficult. However, as Example 4 shows, some results can be readily proved using either the principle of mathematical induction or strong induction. 4-25 4.2 Strong Induction and Well-Ordering 287 Before we present Example 4, note that we can slightly modify strong induction to handle a wider variety of situations. In particular, we can adapt strong induction to handle cases where the inductive step is valid only for integers greater than a particular integer. Let b be a fixed integer and j a fixed positive integer. The form of strong induction we need tells us that P (n) is true for all integers n with n ::: b if we can complete these two steps: BASIS STEP: We verify that the propositions P(b), P(b + 1), ... , P(b + j) are true. INDUCTIVE STEP: We show that [P(b) /\ P(b + 1) /\ ... /\ P(k)] -+ P(k + 1) is true for every positive integer k ::: b + j. We will use this alternative form in the strong induction proof in Example 4. That this alternative form is equivalent to strong induction is left as Exercise 28. EXAMPLE 4 Prove that every amount of postage of 12 cents or more can be formed using just 4-cent and 5-cent stamps. Solution: We will prove this result using the principle of mathematical induction. Then we will present a proof using strong induction. Let P(n) be the statement that postage of n cents can be formed using 4-cent and 5-cent stamps. We begin by using the principle of mathematical induction. BASIS STEP: Postage of 12 cents can be formed using three 4-cent stamps. INDUCTIVE STEP: The inductive hypothesis is the statement that P(k) is true. That is, under this hypothe sis, postage of k cents can be fo rmed using 4-cent and 5-cent stamps. To complete the inductive step, we need to show that when we assume P(k) is true, then P(k + 1) is also true where k ::: 12. That is, we need to show that if we can form postage of k cents, then we can form postage of k + 1 cents. To see this, suppose that at least one 4-cent stamp was used to form postage of k cents. Then we can replace this stamp with a 5-cent stamp to form postage of k + 1 cents. But ifno 4-cent stamps were used, we can form postage of k cents using only 5-cent stamps. Moreover, because k ::: 12, we needed at least three 5-cent stamps to form postage of k cents. So, we can replace three 5-cent stamps with four 4-cent stamps to form postage of k + 1 cents. This completes the inductive step. Because we have completed the basis step and the inductive step, we know that P(n) is true for all n ::: 12. That is, we can form postage of n cents, where n ::: 12 using just 4-cent and 5-cent stamps. Next, we will use strong induction to prove the same result. In this proof, in the basis step we show that P(12), P(13), P(14), and P(15) are true, that is, that postage of 12, 13, 14, or 15 cents can be formed using just 4-cent and 5-cent stamps. In the inductive step we show how to get postage of k + 1 cents for k ::: 15 from postage of k - 3 cents. BASIS STEP: We can form postage of 12, 13, 14, and 15 cents using three 4-cent stamps, two 4-cent stamps and one 5-cent stamp, one 4-cent stamp and two 5-cent stamps, and three 5-cent stamps, respective ly. This shows that P(12), P(13), P(14), and P(15) are true. This completes the basis step. INDUCTIVE STEP: The inductive hypothesis is the statement that P(j) is true for 12 � j � k, where k is an integer with k ::: 15. That is, we assume that we can form postage of j cents, where 12 � j � k. To complete the inductive step we need to show that under this assumption, P(k + 1) is true, that is, we can form postage of k + 1 cents. Using the inductive hypothesis, we can assume that P(k -3) is true because k - 3 ::: 12, that is, we can form postage of k - 3 cents using just 4-cent and 5-cent stamps. To form postage of k + 1 cents, we need only add another 4-cent stamp to the stamps we used to form postage of k - 3 cents. That is, we have shown that if the inductive hypothesis is true, then P (k + 1) is also true. This completes the inductive step. 288 4 / Induction and Recursion 4-26 THEOREM l Because we have completed the basis step and the inductive step of a strong induction proof, we know by strong induction that P(n) is true for all integers n with n :::: 12. That is, we know that every postage of n cents, where n is at least 12, can be formed using 4-cent and 5-cent stamps. (There are other ways to approach this problem besides those described here. Can you find a solution that does not use mathematical induction?) .... Using Strong Induction in Computa tional Geometry Our next example of strong induction will come from computational geometry, the part of discrete mathema tics that studies computational problems involving geometric objects. Compu­ tational geometry is used extensively in computer graphics, computer games, robotics, scientific calculations, and a vast array of other areas. Before we can present this result, we introduce some terminol ogy, possibly familiar from earlier studies in geometry. A polygon is a closed geometric figure consisting of a sequence of line segments SI, S2, ••• , Sn, called sides. Each pair of consecutive sides, S; and S;+I, i = 1,2, ... , n -1, as well as the last side Sn and the first side SI, of the polygon meet at a common endpoint, called a vertex. A polygon is called simple if no two nonconse cutive sides intersect. Every simple polygon divides the plane into two regions: its interior, consisting of the points inside the curve, and its exterior, consisting ofthe points outside the curve. This last fact is suprisingly complicated to prove. It is a special case of the famous Jordan Curve Theorem, which tells us that every simple curve divides the plane into two regions; see [orOO], for example. A polygon is called convex if every line segment connecting two points in the interior of the polygon lies entirely inside the polygon. (A polygon that is not convex is said to be noncon vex.) Figure 1 displays some polygons; polygons (a) and (b) are convex, but polygons (c) and (d) are not. A diagonal of a simple polygon is a line segment connecting two nonconse cutive vertices of the polygon, and a diagonal is called an interior diagonal if it lies entirely inside the polygon, except for its endpoints. For example, in polygon (d), the line segment connecting a and f is an interior diagonal, but the line segment connecting a and d is a diagonal that is not an interior diagonal. One of the most basic operations of computational geometry involves dividing a simple polygon into triangles by adding nonintersecting diagonal s. This process is called triangulation. Note that a simple polygon can have many different triangulations, as shown in Figure 2. Perhaps the most basic fact in computational geometry is that it is possible to triangulate every simple polygon, as we state in Theorem 1. Furthermore, this theorem tell us that every triangulation of a simple polygon with n sides includes n -2 triangles. A simple polygon with n sides, where n is an integer with n :::: 3, can be triangulated into n -2 triangles. ubo-A b ee d b d (a) (b) (c) FIGURE 1 Convex and Nonconvex Polygons. a f b af is an interior diagonal c ad is not an interior diagonal (d) 4-27 LEMMA l 4.2 Strong Induction and Well-Ordering 289 Two different triangulations of a simple polygon with seven sides into five triangles, shown with dotted lines and with dashed lines, respectively FIGURE 2 Triangulations of a Polygon. It seems obvious that we should be able to triangulate a simple polygon by successively adding interior diagonal s. Consequently, a proofby strong induction seems promising. However, such a proof requires this crucial lemma. Every simple polygon has an interior diagonal. Although Lemma 1 seems particularly simple, it is surprisingly tricky to prove. In fact, as recently as 30 years ago, a variety of incorrect proofs thought to be correct were commonly seen in books and articles. We defer the proof of Lemma 1 until after we prove Theorem 1. It is not uncommon to prove a theorem pending the later proof of an important lemma. Proof (of Theorem 1): We will prove this result using strong induction. Let T(n) be the statement that every simple polygon with n sides can be triangulated into n - 2 triangles. BASIS STEP: T(3) is true because a simple polygon with three sides is a triangle. We do not need to add any diagonals to triangulate a triangle; it is already triangulated into one triangle, itself. INDUCTIVE STEP: For the inductive hypothesis, we assume that TU) is true for all integers j with 3 � j � k. That is, we assume that we can triangulate a simple polygon with j sides into j - 2 triangles whenever 3 � j � k. To complete the inductive step, we must show that when we assume the inductive hypothesis, P(k + 1) is true, that is, that every simple polygon with k + 1 sides can be triangulated into (k + 1) - 2 = k - 1 triangles. So, suppose that we have a simple polygon P with k + 1 sides. By Lemma 1, P has an interior diagonal abo Now, ab splits P into two simple polygons Q, with s sides, and R, with t sides. The sides of Q and R are the sides of P, together with the side ab, which is a side of both Q and R. Note that 3 � s � k and 3 � t � k because both Q and R have at least one fewer side than P does (after all, each of these is formed from P by deleting at least two sides and replacing these sides by the diagonal ab). Furthermore, the number of sides of P is two less than the sum of the numbers of sides of Q and the number of sides of R, because each side of P is a side of either Q or of R, but not both, and the diagonal ab is a side of both Q and R, but not P. That is, k + 1 = s + t -2. By the inductive hypothesis, because both 3 � s � k and 3 � t � k, we can triangulate Q and R into s - 2 and t - 2 triangles, respectively. Next, note that these triangulations together produce a triangulation of P. (Each diagonal added to triangulate one of these smaller polygons is also a diagonal of P.) Consequently, we can triangulate P into a total of (s -2) + (t -2) = s + t - 4 = (k + 1) - 2 triangles. This completes the proof by strong induction. That is, we have shown that every simple polygon with n sides, where n ::: 3, can be triangulated into n - 2 triangles. <l 290 4 / Induction and Recursion b c a T is the triangle abc p is the vertex of P inside T such that the Lbap is smallest bp must be an interior diagonal of P FIGURE 3 Constructing an Interior Diagonal of a Simple Polygon. 4-28 We now return to our proof of Lemma 1. We present a proof published by Chung-Wu Ho [H07S]. Note that although this proof may be omitted without loss of continuity, it does show how it may be difficult to prove a result that seems to be quite obvious. Proof: Suppose that P is a simple polygon drawn in the plane. Furthermore, suppose that b is the point of P or the interior of P with the least y-coordinate among the vertices with the smallest x -coordinate. Then b must be a vertex of P, for if it is an interior point, there would have to be a vertex of P with a smaller x-coordinate. Two other vertices each share an edge with b, say a and c. It follows that the angle in the interior of P formed by ab and bc must be less than 180 degrees (otherwise, there would be points of P with smaller x-coordinates than b). Now let T be the triangle f:::.abc. Ifthere are no vertices of P on or inside T, we can connect a and c to obtain an interior diagonal. On the other hand, if there are vertices of P inside T, we will find a vertex p of P on or inside T such that bp is an interior diagonal. (This is the tricky part. Ho noted that in many published proofs ofthis lemma a vertex p was found such that bp was not necessarily an interior diagonal of P. See Execise 21.) The key is to select a vertex p such that the angle Lbap is smallest. To see this, note that the ray starting at a and passing through p hits the line segment bc at a point, say q. It then follows that the triangle f:::.baq cannot contain any vertices of P in its interior. Hence, we can connect band p to produce an interior diagonal of P. Locating this vertex p is illustrated in Figure 3. <l Proofs Using the Well-Orde ring Propert y The validity of both the principle of mathematical induction and strong induction follows from a fundamental axiom of the set of integers, the well-ordering property (see Appendix 1). The well-ordering property states that every nonempty set of nonnegative integers has a least element. We will show how the well-ordering property can be used directly in proofs. Furthermore, it can be shown that the well-ordering property, the principle of mathematical induction, and strong induction are all equivalent. That is, given anyone of these three proof techniques, its validity can be shown from the validity of either of the other two technique s. In Section 4.1 we showed that the principle of mathematical induction follows from the well-ordering property. The other parts of this equivalence are left as Exercises 31, 42, and 43 at the end of this section. THE WELL-ORDERING PROPERTY Every nonempty set of nonnegative integers has a least element. The well-ordering property can often be used directly in proofs. EXAMPLE 5 Use the well-ordering property to prove the division algorithm. Recall that the division algorithm states that if a is an integer and d is a positive integer, then there are unique integers q and r with 0 .:::: r < d and a = dq + r. 4-29 Extra� Examples� EXAMPLE 6 Exercises 4.2 Strong Induction and Well-Ordering 291 Solution: Let S be the set of nonnegative integers of the form a -d q, where q is an integer. This set is nonempty because -dq can be made as large as desired (taking q to be a negative integer with large absolute value). By the well-ordering property, S has a least element r = a -dqo. The integer r is nonnegative. It is also the case that r < d. If it were not, then there would be a smaller nonnegative element in S, namely, a -d(qo + 1). To see this, suppose that r � d. Because a = dqo + r, it follows that a -d(qo + 1) = (a -dqo) -d = r -d � O. Conse­ quently, there are integers q and r with 0 :::: r < d. The proof that q and r are unique is left as an exercise for the reader. ... In a round-robin tournament every player plays every other player exactly once and each match has a winner and a loser. We say that the players PI, P2, ... , Pm form.a cycle if PI beats P2, P2 beats P3, ... , Pm-I beats Pm, and Pm beats PI. Use the well-ordering principle to show that if there is a cycle oflength m (m � 3) among the players in a round-robin tournament, there must be a cycle of three of these players. Solution: We assume that there is no cycle of three players. Because there is at least one cycle in the round-robin tournament, the set of all positive integers n for which there is a cycle of length n is nonempty. By the well-ordering property, this set of positive integers has a least element k, which by assumption must be greater than three. Consequently, there exists a cycle of players PI, P2, P3, ... , Pk and no shorter cycle exists. Because there is no cycle of three players, we know that k > 3. Consider the first three elements ofthis cycle, PI, P2, and P3. There are two possible outcomes of the match between PI and P3· If P3 beats PI, it follows that PI, P2, P3 is a cycle of length three, contradicting our assumption that there is no cycle of three players. Consequently, it must be the case that PI beats P3. This means that we can omit P2 from the cycle PI, P2, P3, ... , Pk to obtain the cycle PI, P3, P4, ... , Pk of length k -1, contradicting the assumption that the smallest cycle has length k. We conclude that there must be a cycle oflength three. ... 1. Use strong induction to show that if you can n,m one mile or two miles, and if you can always run two more miles once you have run a specified number of miles, then you can run any number of miles. parts ofthis exercise outline a strong induction proof that P(n) is true forn � IS. a) Show statements P(lS), P(19), P(20), and P(21) are true, completing the basis step of the proof. 2. Use strong induction to show that all dominoes fall in an infinite arrangement of dominoes if you know that the first three dominoes fall, and that when a domino falls, the domino three farther down in the arrangement also falls. 3. Let P (n ) be the statement that a postage of n cents can be formed using just 3-cent stamps and 5-cent stamps. The parts of this exercise outline a strong induction proof that P(n) is true for n � S. a) Show that the statements P(S), P(9), and P(lO) are true, completing the basis step of the proof. b) What is the inductive hypothesis of the proof? c) What do you need to prove in the inductive step? d) Complete the inductive step for k � lO. e) Explain why these steps show that this statement is true whenever n � S. 4. Let P (n ) be the statement that a postage of n cents can be formed using just 4-cent stamps and 7-cent stamps. The b) What is the inductive hypothesis of the proof? c) What do you need to prove in the inductive step? d) Complete the inductive step for k � 21. e) Explain why these steps show that this statement is true whenever n � IS. 5. a) Determine which amounts of postage can be formed using just 4-cent and II-cent stamps. b) Prove your answer to (a) using the principle of math­ ematical induction. Be sure to state explicitly your inductive hypothesis in the inductive step. c) Prove your answer to (a) using strong induction. How does the inductive hypothesis in this proof differ from that in the inductive hypothesis for a proof using math­ ematical induction? 6. a) Determine which amounts of postage can be formed using just 3-cent and IO-cent stamps. 292 4 / Induction and Recursion b) Prove your answer to (a) using the principle of math­ ematical induction. Be sure to state explicitly your inductive hypothesis in the inductive step. c) Prove your answer to (a) using strong induction. How does the inductive hypothesis in this proof differ from that in the inductive hypothesis for a proof using math­ ematical induction? 7. Which amounts of money can be formed using just two­ dollar bills and five-dollar bills? Prove your answer using strong induction. S. Suppose that a store offers gift certificates in denomina­ tions of25 dollars and 40 dollars. Determine the possible total amounts you can form using these gift certficates. Prove your answer using strong induction. *9. Use strong induction to prove that -J2 is irrational. [Hint: Let P (n ) be the statement that -J2 =1= n / b for any positive integer b.] 10. Assume that a chocolate bar consists of n squares arranged in a rectangular pattern. The bar, a smaller rectangular piece of the bar, can be broken along a vertical or a hor­ izontal line separating the squares. Assuming that only one piece can be broken at a time, determine how many breaks you must successively make to break the bar into n separate squares. Use strong induction to prove your answer. 11. Consider this variation of the game of Nim. The game begins with n matches. Two players take turns removing matches, one, two, or three at a time. The player removing the last match loses. Use strong induction to show that if each player plays the best strategy possible, the first player wins if n = 4}, 4} + 2, or 4} + 3 for some nonnegative integer} and the second player wins in the remaining case when n = 4} + 1 for some nonnegative integer }. 12. Use strong induction to show that every positive integer n can be written as a sum of distinct powers of two, that is, as a sum of a subset of the integers 2° = 1, 21 = 2, 22 = 4, and so on. [Hint: For the inductive step, separately con­ sider the case where k + 1 is even and where it is odd. When it is even, note that (k + 1)/2 is an integer.] *13. A jigsaw puzzle is put together by successively joining pieces that fit together into blocks. A move is made each time a piece is added to a block, or when two blocks are joined. Use strong induction to prove that no matter how the moves are carried out; exactly n - 1 moves are re­ quired to assemble a puzzle with n pieces. 14. Suppose you begin with a pile of n stones and split this pile into n piles of one stone each by successively splitting a pile of stones into two smaller piles. Each time you split a pile you multiply the number of stones in each of the two smaller piles you form, so that if these piles have r and s stones in them, respectively, you compute r s. Show that no matter how you split the piles, the sum of the products computed at each step equals n(n -1)/2. 15. Prove that the first player has a winning strategy for the game of Chomp, introduced in Example 12 in Section 1.7, if the initial board is square. [Hint: Use strong induction 4-30 to show that this strategy works. For the first move, the first player chomps all cookies except those in the left and top edges. On subsequent moves, after the second player has chomped cookies on either the top or left edge, the first player chomps cookies in the same relative positions in the left or top edge, respectively.] *16. Prove that the first player has a winning strategy for the game of Chomp, introduced in Example 12 in Section 1.7, if the initial board is two squares wide, that is, a 2 x n board. [Hint: Use strong induction. The first move of the first player should be to chomp the cookie in the bottom row at the far right.] 17. Use strong induction to show that if a simple polygon with at least four sides is triangulated, then at least two of the triangles in the triangulation have two sides that border the exterior of the polygon. *IS. Use strong induction to show that when a convex poly­ gon P with consecutive vertices VI, V2, ... , Vn is trian­ gulated into n -2 triangles, the n -2 triangles can be numbered 1, 2, ... , n -2 so that Vi is a vertex oftriangle i for i = 1,2, ... , n -2. *19. Pick's theorem says that the area of a simple polygon P in the plane with vertices that are all lattice points (that is, points with integer coordinates) equals I(P) + R(P)/2 -1, where I(P) and R(P) are the number oflat­ tice points in the interior of P and on the boundary of P, respectively. Use strong induction on the number ofver­ tices of P to prove Pick's theorem. [Hint: For the basis step, first prove the theorem for rectangles, then for right triangles, and finally for all triangles by noting that the area of a triangle is the area of a larger rectangle contain­ ing it with the areas of at most three triangles subtracted. For the inductive step, take advantage of Lemma 1.] **20. Suppose that P is a simple polygon with vertices VI , V2, ... , Vn listed so that consecutive vertices are con­ nected by an edge, and VI and Vn are connected by an edge. A vertex Vi is called an ear ifthe line segment connecting the two vertices adjacent to Vi is an interior diagonal of the simple polygon. Two ears Vi and Vi are called nonover­ lapping if the interiors of the triangles with vertices Vi and its two adjacent vertices and Vi and its two adjacent vertices do not intersect. Prove that every simple polygon with at least four vertices has at least two nonoverlapping ears. 21. In the proof of Lemma 1 we mentioned that many incorrect methods for finding a vertex p such that the line segment bp is an interior diagonal of P have been published. This exercise presents some of the incorrect ways p has been chosen in these proofs. Show, by considering one of the polygons drawn here, that for each of these choices of p, ' the line segment bp is not necessarily an interior diagonal of P. a) p is the vertex of P such that the angle Labp is smallest. b) p is the vertex of P with the least x-coordinate (other than b). c) p is the vertex of P that is closest to b. 4-3/ a b p b g s h c e Exercises 22 and 23 present examples that show inductive loading can be used to prove results in computational geom­ etry. *22. Let P(n) be the statement that when nonintersecting di­ agonals are drawn inside a convex polygon with n sides, at least two vertices of the polygon are not endpoints of any of these diagonals. a) Show that when we attempt to prove P(n) for all inte­ gers n with n ::: 3 using strong induction, the inductive step does not go through. b) Show that we can prove that P (n) is true for all inte­ gers n with n ::: 3 by proving by strong induction the stronger assertion Q(n), for n ::: 4, where Q(n) states that whenever nonintersecting diagonals are drawn in­ side a convex polygon with n sides, at least two non­ adjacent vertices are not endpoints of any of these diagonals. 23. Let E(n) be the statement that in a triangulation of a sim­ ple polygon with n sides, at least one of the triangles in the triangulation has two sides bordering the exterior of the polygon. a) Explain where a proof using strong induction that E(n) is true for all integers n ::: 4 runs into difficulties. b) Show that we can prove that E(n) is true for all inte­ gers n ::: 4 by proving by strong induction the stronger statement T(n) for all integers n ::: 4, which states that in every triangulation of a simple polygon, at least two of the triangles in the triangulation have two sides bor­ dering the exterior of the polygon. *24. A stable assignment, defined in the preamble to Exer­ cise 58 in Section 3.1, is called optimal for suitors if no stable assignment exists in which a suitor is paired with a suitee whom this suitor prefers to the person to whom this suitor is paired in this stable assignment. Use strong induction to show that the deferred acceptance algorithm produces a stable assignment that is optimal for suitors. 25. Suppose that P(n) is a propositional function. Determine for which positive integers n the statement P(n) must be true, and justify your answer, if a) P(I) is true; for all positive integers n, if P(n) is true, then P(n + 2) is true. a 4.2 Strong Induction and Well-Ordering 293 b) P(I) and P(2) are true; for all positive integers n, if P(n) and P(n + 1) are true, then P(n + 2) is true. c) P(I) is true; for all positive integers n, if P(n) is true, then P(2n) is true. d) P( 1) is true; for all positive integers n, if P(n) is true, then P(n + I) is true. 26. Suppose that P(n) is a propositional function. Determine for which nonnegative integers n the statement P(n) must be true if a) P(O) is true; for all nonnegative integers n, if P(n) is true, then P(n + 2) is true. b) P(O) is true; for all nonnegative integers n, if P(n) is true, then P(n + 3) is true. c) P(O) and P(l) are true; for all nonnegative integers n, if P(n) and P(n + 1) are true, then P(n + 2) is true. d) P(O) is true; for all nonnegative integers n, if P(n) is true, then P(n + 2) and P(n + 3) are true. 27. Show that if the statement P(n) is true for infinitely many positive integers nand P(n + 1) -+ P(n) is true for all positive integers n, then P(n) is true for all positive integers n. 28. Let b be a fixed integer and j a fixed positive integer. Show that if P(b), P(b + 1), ... , P(b + j) are true and [P(b) /\ P(b + 1) A··· /\ P(k)] -+ P(k + 1) is true for every positive integer k ::: b + j, then P(n) is true for all integers n with n ::: b. 29. What is wrong with this "proof" by strong induction? "Theorem " For every nonnegative integer n, 5n = O. Basis Step: 5· 0 = O. Inductive Step: Suppose that 5 j = 0 for all nonnegative integers j with 0 ::c j ::c k. Write k + 1 = i + j, where i and j are natural numbers less than k + 1. By the inductive hypothesis, 5(k + 1) = 5(i + j) = 5i + 5j = 0+0 = O. *30. Find the flaw with the following "proof" that an = 1 for all nonnegative integers n, whenever a is a nonzero real number. Basis Step: aO = I is true by the definition of aO. Inductive Step: Assume that ai = 1 for all nonnegative integers j with j ::c k. Then note that k+ 1 ak . ak 1 . 1 a = ak-1 = -1-= 1. *31. Show that strong induction is a valid method of proof by showing that it follows from the well-ordering property. 32. Find the flaw with the following "proof" that every postage of three cents or more can be formed using just three-cent and four-cent stamps. Basis Step: We can form postage of three cents with a single three-cent stamp and we can form postage of four cents using a single four-cent stamp. Inductive Step: Assume that we can form postage of j cents for all nonnegative integers j with j ::c k using just 294 4 / Induction and Recursion three-cent and four-cent stamps. We can then form postage of k + 1 cents by replacing one three-cent stamp with a four-cent stamp or by replacing two four-cent stamps by three three-cent stamps. 33. Show that we can prove that pen, k) is true for all pairs of positive integers n and k if we show a) P(l, 1) is true and pen, k) ---+ [Pen + I, k) /\ Pen, k + 1)] is true for all positive integers n andk. b) P(l, k) is true for all positive integers k, and P (n, k) ---+ P (n + I, k) is true for all positive integers n and k. c) pen, 1) is true for all positive integers n, and P (n, k) ---+ P (n, k + 1) is true for all positive integers n andk. 34. Prove that L:J=l j(j + 1)(j + 2)· .. (j + k -1) = n(n + l)(n + 2)· .. (n + k)/(k + 1) for all positive integers k and n. [Hint: Use a technique from Exercise 33.] *35. Show that if aI, a2, ... , an are n distinct real numbers, ex­ actly n -I multiplications are used to compute the prod­ uct of these n numbers no matter how parentheses are inserted into their product. [Hint: Use strong induction arid consider the last multiplication.] *36. The well-ordering property can be used to show that there is a unique greatest common divisor of two positive inte­ gers. Let a and b be positive integers, and let S be the set of positive integers ofthe form as + bt, where sand tare integers. a) Show that Sis nonempty. b) Use the well-ordering property to show that S has a smallest element c. c) Show that if d is a common divisor of a and b, then d is a divisor of c. d) Show that c I a and c I b. [Hint: First, assume that c y a. Then a = qc + r, where 0 < r < c. Show that rES, contradicting the choice of c.] 4-32 e) Conclude from (c) and (d) that the greatest common divisor of a and b exists. Finish the proofby showing that this greatest common divisor is unique. 37. Let a be an integer and d be a positive integer. Show that the integers q and r with a = dq + rand 0 � r < d, which were shown to exist in Example 5, are unique. 38. Use mathematical induction to show that a rectangu­ lar checkerboard with an even number of cells and two squares missing, one white and one black, can be covered by dominoes. **39. Can you use the well-ordering property to prove the state­ ment: "Every positive integer can be described using no more than fifteen English words"? Assume the words come from a particular dictionary of English. [Hint: Sup­ pose that there are positive integers that cannot be de­ scribed using no more than fifteen English words. By well ordering, the smallest positive integer that cannot be de­ scribed using no more than fifteen English words would then exist.] 40. Use the well-ordering principle to show that if x and y are real numbers with x < y, then there is a rational number r with x < r < y. [Hint: Use the Archimedean property, given in Appendix 1, to find a positive integer A with A > 1/(y -x). Then show that there is a ratio­ nal number r with denominator A between x and y by looking at the numbers Lx J + j / A, where j is a positive integer.] *41. Show that the well-ordering property can be proved when the principle of mathematical induction is taken as an axlOm. *42. Show that the principle of mathematical induction and strong induction are equivalent; that is, each can be shown to be valid from the other. *43. Show that we can prove the well-ordering property when we take either the principle of mathematical induction or strong induction as an axiom instead of taking the well­ ordering property as an axiom. 4.3 Recursive Definitions and Structural Induction Introduction Sometimes it is difficult to define an object explicitly. However, it may be easy to define this object in terms of itself. This process is called recursion. For instance, the picture shown in Figure I is produced recursively. First, an original picture is given. Then a process of suc­ cessively superimposing centered smaller pictures on top of the previous pictures is carried out. We can use recursion to define sequences, functions, and sets. In previous discussions, we specified the terms of a sequence using an explicit formula. For instance, the sequence of powers of2 is given by an = 2n for n = 0, 1,2, .... However, this sequence can also be defined by giving the first term of the sequence, namely, ao = 1, and a rule for finding a term of the 4-33 Assessmenl E::J 4.3 Recursive Definitions and Structural Induction 295 FIGURE 1 A Recursively Defined Picture. sequence from the previous one, namely, an+! = 2an for n = 0, 1,2, .... When we define a sequence recursively by specifying how terms of the sequence are found from previous terms, we can use induction to prove results about the sequence. When we define a set recursively, we specify some initial elements in a basis step and provide a rule for constructing new elements from those we already have in the recur­ sive step. To prove results about recursively defined sets we use a method called structural induction. Recursivel y Defined Functions We use two steps to define a function with the set of nonnegative integers as its domain: BASIS STEP: Specify the value of the function at zero. RECURSIVE STEP: Give a rule for finding its value at an integer from its values at smaller integers. Such a definition is called a recursive or inductive definition. EXAMPLE 1 Suppose that 1 is defined recursively by 1(0) = 3, Extra" Examples� I(n + 1) = 2/(n) + 3. Find 1(1), 1(2), 1(3), and 1(4). 296 4 / Induction and Recursion Solution: From the recursive definition it follows that f(1) = 2f(0) + 3 = 2·3 + 3 = 9, f(2) = 2f(1) + 3 = 2·9 + 3 = 21, f(3) = 2f(2) + 3 = 2·21 + 3 = 45, f(4) = 2f(3) + 3 = 2 . 45 + 3 = 93. 4-34 Many functions can be studied using their recursive definitions. The factorial function is one such example. EXAMPLE 2 Give an inductive definition of the factorial function F(n) = nL Solution: We can define the factorial function by specifying the initial value of this function, namely, F(O) = 1, and giving a rule for finding F(n + 1) from F(n). This is obtained by noting that (n + I)! is computed from n! by multiplying by n + 1. Hence, the desired rule is F(n + 1) = (n + I)F(n). To determine a value of the factorial function, such as F(5) = 5!, from the recursive definition found in Example 2, it is necessary to use the rule that shows how to express F (n + 1) in terms of F(n) several times: F(5) = 5F(4) = 5 . 4F(3) = 5 ·4· 3F(2) = 5 ·4·3· 2F(I) = 5 ·4 . 3 . 2 . 1 . F(O) = 5 . 4 . 3 . 2 . 1 . 1 = 120. Once F(O) is the only value of the function that occurs, no more reductions are necessary. The only thing left to do is to insertthe value of F(O) into the formula. Recursively defined functions are well defined. That is, for every positive integer, the value of the function at this integer is determined in an unambiguous way. This means that given any positive integer, we can use the two parts of the definition to find the value of the function at that integer, and that we obtain the same value no matter how we apply the two parts of the definition. This is a consequence of the principle of mathematical induction. (See Exercise 56 at the end of this section.) Additional examples of recursive definitions are given in Examples 3 and 4. EXAMPLE 3 Give a recursive definition of an, where a is a nonzero real number and n is a nonnegative integer. Solution: The recursive definition contains two parts. First aD is specified, namely, aD = 1. Then the rule for finding an+! from an, namely, an+! = a . an, for n = 0, 1,2,3, ... , is given. These two equations uniquely define an for all nonnegative integers n. .... EXAMPLE 4 Give a recursive definition of 4-35 DEFINITION 1 linkS� EXAM PLE 5 EXAM PLE 6 4.3 Recursive Definitions and Structural Induction 297 Solution: The first part of the recursive definition is The second part is In some recursive definitions of functions, the values of the function at the first k positive integers are specified, and a rule is given for determining the value of the function at larger integers from its values at some or all of the preceding k integers. That recursive definitions defined in this way produce well-defined functions follows from strong induction (see Exercise 57 at the end of this section). The Fibonacci numbers, /0, /1, /2, ... , are defined by the equations /0 = 0, /1 = 1, and /n = /n-I + 1n-2 for n = 2, 3, 4, .... Find the Fibonacci numbers /2, /3, 14, /5, and /6. Solution: Because the first part of the definition states that /0 = 0 and /1 = 1, it follows from the second part of the definition that /2=/1+/0=1+0=1, /3 = /2 + /1 = 1 + 1 = 2, 14 = /3 + /2 = 2 + 1 = 3, /5 = 14 + /3 = 3 + 2 = 5, /6 = 15 + 14 = 5 + 3 = 8. We can use the recursive definition of the Fibonacci numbers to prove many properties of these numbers. We give one such property in Example 6 .. Show that whenever n ::: 3, In > an-2, where a = (1 + -J5)/2. Solution: We can use strong induction to prove this inequality. Let P(n) be the statement In > an-2• We want to show that P(n) is true whenever n is an integer greater than or equal to 3. BASIS STEP: First, note that a < 2 = /3, a2 = (3 + .J5)/2 < 3 = 14, so P(3) and P(4) are true. 298 4 / Induction and Recursion 4-36 THEOR EM! unkS� INDUCTIVE STEP: Assume that P(j) is true, namely, that jj > aj-2, for all integers j with 3 ::::: j ::::: k, where k ::: 4. We must show that P(k + 1) is true, that is, that fk+1 > ak-I. Because a is a solution of x2 -x-I = 0 (as the quadratic formula shows), it follows that a2 = a + 1. Therefore, ak-I = a2 • ak-3 = (a + 1)ak-3 = a . ak-3 + 1 . ak-3 = ak-2 + ak-3. By the inductive hypothesis, if k ::: 4, it follows that r. k-3 Jk-I > a , Therefore, we have r. r. + r. k-2 + k-3 k-I Jk+1 = Jk Jk-I > a a = a . It follows that P(k + I) is true. This completes the proof. Remark: The inductive step shows that whenever k ::: 4, P(k + I) follows from the assumption that P(j) is true for 3 ::::: j ::::: k. Hence, the inductive step does not show that P(3) -+ P(4). Therefore, we had to show that P(4) is true separately. We can now show that the Euclidean algorithm uses 0 (log b) divisions to find the greatest common divisor of the positive integers a and b, where a ::: b. LAME'S THEOREM Let a and b be positive integers with a ::: b. Then the number of divisions used by the Euclidean algorithm to find gcd(a, b) is less than or equal to five times the number of decimal digits in b. Proof: Recall that when the Euclidean algorithm is applied to find gcd(a, b) with a ::: b, this sequence of equations (where a = ro and b = rl) is obtained. 0::::: r2 < rl 0::::: r3 < r2 Here n divisions have been used to findrn = gcd(a, b). Note that the quotients ql, q2, ... , qn-I are all at least 1. Moreover, qn ::: 2, because r n < r n-I. This implies that FIBONACCI (1170-1250) Fibonacci (short for filius Bonacci, or "son of Bonacci") was also known as Leonardo of Pi sa. He was born in the Italian commercial center of Pi sa. Fibonacci was a merchant who traveled extensively throughout the Mideast, where he came into contact with Arabian mathemat ics. In his book Liber Abaci, Fibonacci introduced the European world to Arabic notation for numerals and algorithms for arithmetic. It was in this book that his famous rabbit problem (described in Section 7.1) appeared. Fibonacci also wrote books on geometry and trigonome try and on Diophantine equations, which involve finding integer solutions to equations . 4-37 Assessmenl � unks� rn ::: 1 = Iz, rn-l ::: 2rn ::: 21z = 13, rn-2 ::: rn-l + rn ::: 13 + Iz = 14, r2 ::: r3 + r4 ::: In-l + In-2 = In, 4.3 Recursive Definitions and Structural Induction 299 b = rl ::: r2 + r3 ::: In + In-I = In+l. It follows that ifn divisions are used by the Euclidean algorithm to find gcd(a, b) with a ::: b, then b ::: In+l. From Example 6 we know that In+1 > an-I for n > 2, where a = (1 + "f5)/2. Therefore, it follows that b > an-I. Furthermore, because 10g10 a'" 0.208 > 1/5, we see that 10gIOb > (fl -1) log 10 a > (n..." 1)/5. Hence, n -1 < 5 . log 10 b. Now suppose that b has k decimal d�gits. Then b < 10k and 10gIO b < k. It follows that n -1 < 5k, �d because k is an integer, it follows that n :::: 5k. This finishes the proof. <l Because the number of decimal digits in b, which equals Lloglo b J + 1, is less than or equal to loglo b + 1, Theorem 1 tells Ull that the number of divisions required to find gcd(a, b) with a > b is less than or equal to 5(loglO,b + 1). Because 5(lOgl(l b + 1) is O(logb), we see that o (log b) divisions are used by the Euclidean algorithm to find gcd(a, b) whenever a > b. Recursively Defined Sets and Structures We have explored how functions can be defined recursively. We now turn our attention to how sets can be defined recursively. Just as in the recursive definition of functions, recursive definitions of sets have two parts, a b�sis step and a recursive step. In the basis step, an initial collection of element!> is specifi�. In the recursive step, rules for forming new elements in the set from those already known. tp b� in the set are provided. Recursive definitions may also include an exclusion rule, which sp�ifies that a recursively defined set contains nothing other than those elements specified in the basis step or generated by applications of the recursive step. In our discussions, we will always tacitly assume that the exclusion rule holds and no element belongs to a recursively defined set unless it is in the initial collection specified in the basis step or can GABRIEL LAME (1795-1870) Gabriel Lame entered the Ecole Polytec hnique in 1813, graduating in 1817. He continued his education at the Ecole des Mines, graduating in 1820. In 1820 Lame went to Russia, where he was appointed director of the Schools of Highways and Trans­ portation in St. Petersburg. Not only did he teach, but he also planned roads and bridges while in Russia. He returned to Paris in 1832, where he helped found an engineering firm. However, he soon left the finn, accepting the chair of physics at the Ecole Polytechnique, which he held until 1844. While holding this position, he was active outside academia as an engineering consultant, serving as chief engineer of mines and participating in the building of railways. Lame contributed original work to number theory, applied mathematics, and thennodynamics. His best­ known work involves the introduction of curvilinear coordina tes. His work on number theory includes proving Fennat's Last Theorem for n = 7, as well as providing the upper bound for the number of divisions used by the Euclidean algorithm given in this text. In the opinion of Gauss, one of the most important mathematicians of all time, Lame was the foremost French mathematician of his time. However, French mathematicians considered him too practical, whereas French scientists considered him too theoretic al. 300 4 / Induction and Recursion 4-38 be generated using the recursive step one or more times. Later we will see how we can use a technique known as structural induction to prove results about recursively defined sets. Examples 7, 8, 10, and 11 illustrate the recursive definition of sets. In each example, we show those elements generated by the first few applications of the recursive step. EXAMPL E 7 Consider the subset S of the set of integers defined by BASIS STEP: 3 E S. Extra� Examples� DEFINITION 2 RECURSIVE STEP: If XES and yES, then x + yES. The new elements found to be in S are 3 by the basis step, 3 + 3 = 6 at the first application of the recursive step, 3 + 6 = 6 + 3 = 9 and 6 + 6 = 12 at the second application of the recursive step, and so on. We will show later that S is the set of all positive multiples of 3. � Recursive definitions play an important role in the study of strings. (See Chapter 12 for an introduction to the theory of formal languages, for example.) Recall from Section 2.4 that a string over an alphabet � is a finite sequence of symbols from �. We can define �*, the set of strings over �, recursively, as Definition 2 shows. The set �* of strings over the alphabet � can be defined recursively by BASIS STEP: A E �* (where A is the empty string containing no symbols). RECURSIVE STEP: Ifw E �* and x E �,then wx E �*. The basis step of the recursive definition of strings says that the empty string belongs to � *. The recursive step states that new strings are produced by adding a symbol from � to the end of strings in � *. At each application of the recursive step, strings containing one additional symbol are generated. EXAMPLE 8 If � = {O, I}, the strings found to be in � *, the set of all bit strings, are A, specified to be in � * in the basis step, ° and 1 formed during the first application of the recursive step, 00, 01, 10, and 11 formed during the second application of the recursive step, and so on. � Recursive definitions can be used to define operations or functions on the elements of recursively defined sets. This is illustrated in Definition 3 of the concatenation of two strings and Example 9 concerning the length of a string. DEFINITION 3 Two strings can be combined via the operation of concatenation. Let � be a set of symbols . and �* the set of strings formed from symbols in �. We can define the concatenation of two strings, denoted by ., recursively as follows. BASIS STEP: lfw E �*, then w . A = w, where A is the empty string. RECURSIVE STEP: Ifw\ E �* and W2 E �* and x E �,then WI· (W2X) = (WI · W2)x. The concatenation of the strings WI and W2 is often written as WIW2 rather than WI • W2. By repeated application of the recursive definition, it follows that the concatenation of two strings 4-39 4.3 Recursive Definitions and Structural Induction 301 WI and W2 consists of the symbols in WI followed by the symbols in W2. For instance, the concatenation of WI = abra and W2 = cadabra is WIW2 = abracadabra. EXAMPL E 9 Length of a String Give a recursive definition of l(w), the length of the string w. Solution: The length of a string can be defined by leA) = 0; l(wx) = lew) + 1 ifw E :E* and x E :E. Another important use of recursive definitions is to define well-formed formulae of various types. This is illustrated in Examples 10 and 11. EXAMPLE 10 Well-Formed Formulae for Compound Statement Forms We can define the set of well­ formed formulae for compound statement forms involving T, F, propositional variables, and operators from the set {-., /\, v, -+, *+}. BASIS STEP: T, F, and s, where s is a propositional variable, are well-formed formulae. RECURSIVE STEP: If E and F are well-formed formulae, then (-.E), (E /\ F), (E v F), (E -+ F), and (E *+ F) are well-formed formulae. For example, by the basis step we know that T, F, p, and q are well-formed formulae, where p and q are propositional variables. From an initial application of the recursive step, we know that (p v q), (p -+ F), (F -+ q), and (q /\ F) are well-formed formulae. A second application of the recursive step shows that «p v q) -+ (q /\ F)), (q v (p v q )), and «p -+ F) -+ T) are well-formed formulae. We leave it to the reader to show that P-' /\ q, pq /\, and -. /\ pq are not well-formed formulae, by showing that none can be obtained using the basis step and one or more applications of the recursive step. .... EXAMPLE 11 Well-Formed Formulae of Operators and Operands We can define the set of well-formed formulae consisting of variables, numerals, and operators from the set {+, -, *, /, t } (where * denotes multiplication and t denotes exponentiation) recursively. BASIS STEP: x is a well-formed formula if x is a numeral or variable. RECURSIVE STEP: If F and G are well-formed formulae, then (F + G), (F -G), (F * G), (F / G), and (F t G) are well-formed formulae. For example, by the basis step we see that x, y, 0, and 3 are well-formed formulae (as is any variable or numeral). Well-formed formulae generated by applying the recursive step once include (x +3), (3+y), (x -y), (3-0), (x *3), (3*y), (3/0), (x/y), (3tx), and (Ot3). Applying the recursive step twice shows that formulae such as «x + 3) + 3) and (x -(3 * y)) are well-formed formulae. [Note that (3/0) is a well-formed formula because we are concerned only with syntax matters here.] We leave it to the reader to show that each of the formulae x3 +, y * +x, and * x/y is not a well-formed formula by showing that none of them can be obtained using the basis step and one or more applications of the recursive step. .... We will study trees extensively in Chapter 10. A tree is a special type of a graph; a graph is made up of vertices and edges connecting some pairs of vertices. We will study graphs in Chapter 9. We will briefly introduce them here to illustrate how they can be defined recursively. 302 4 / Induction and Recursion 4-40 DEFINITION 4 DEFINITION 5 Basis step • Step I Step 2 The set of rooted trees, where a rooted tree consists of a set of vertices containing a distin­ guished vertex called the root, and edges connecting these vertices, can be defined recursively by these steps: BASIS STEP: A single vertex r is a rooted tree. RECURSIVE STEP: Suppose that T1, T2, ... , Tn are disjoint rooted trees with roots rl, r2, ... , r n, respectively. Then the graph formed by starting with a root r, which is not in any of the rooted trees T1, T2, ... , Tn, and adding an edge from r to each of the vertices rl, r2, ... ,rn, is also a rooted tree .. In Figure 2 we illustrate some ofthe rooted trees formed starting with the basis step and applying the recursive step one time and two times. Note that infinitely many rooted trees are formed at each application of the recursive definition. Binary trees are a special type of rooted trees. We will provide recursive definitions of two types of binary trees-full binary trees and extended binary trees. In the recursive step of the definition of each type of binary tree, two binary trees are combined to form a new tree with one of these trees designated the left subtree and the other the right subtree. In extended binary trees, the left subtree or the right subtree can be empty, but in full binary trees this is not possible. Binary trees an� one of the most important types of structures in computer science. In Chapter lOwe will see how they can be used in searching and sorting algorithms, in algorithms for compressing data, and in many other applications. We first define extended binary trees. The set of extended binary trees can be defined recursively by these steps: BASIS STEP: The empty set is an extended binary tree. RECURSIVE STEP: If Tl and T2 are disjoint extended binary trees, there is an extended binary tree, denoted by Tl . T2, consisting of a root r together with edges connecting the root to each of the roots of the left subtree Tl and the right subtree T2 when these trees are nonempty. FIGURE 2 Building Up Rooted Trees. 4-41 4.3 Recursive Definitions and Structural Induction 303 Basis step Step I • Step 2 /\1 \ Step 3 FIGURE 3 Building Up Extended Binary Trees. Figure 3 shows how extended binary trees are built up by applying the recursive step from one to three times. We now show how to define the set of full binary trees. Note that the difference between this recursive definition and that of extended binary trees lies entirely in the basis step. DEFINITION 6 The set of full binary trees can be defined recursively by these steps: BASIS STEP: There is a full binary tree consisting only of a single vertex r. RECURSIVE STEP: If TJ and T2 are disjoint full binary trees, there is a full binary tree, denoted by TJ • T2, consisting of a root r together with edges connecting the root to each of the roots of the left subtree TJ and the right subtree T2. Figure 4 shows how full binary trees are built up by applying the recursive step one and two times. Structural Induction To prove results about recursively defined sets we generally use some form of mathematical induction. Example 12 illustrates the connection between recursively defined sets and mathe­ matical induction. EXAMPLE 12 Show that the set S defined in Example 7 by specifying that 3 E S and that if XES and yES, then x + YES, is the set of all positive integers that are multiples of 3. 304 4 / Induction and Recursion 4-42 Basis step _ Step 1 Step 2 FIGURE 4 Building Up Full Binary Trees. Solution: Let A be the set of all positive integers divisible by 3. To prove that A = S, we" must show that A is a subset of S and that S is a subset of A. To prove that A is a subset of S, we must show that every positive integer divisible by 3 is in S. We will use mathematical induction to prove this. Let Pen) be the statement that 3n belongs to S. The basis step holds because by the first part of the recursive definition of S, 3 . 1 = 3 is in S. To establish the inductive step, assume that P(k) is true, namely, that 3k is in S. Because 3k is in S and because 3 is in S, it follows from the second part of the recursive definition of S that 3k + 3 = 3(k + 1) is also in S. To prove that S is a subset of A, we use the recursive definition of S. First, the basis step of the definition specifies that 3 is in S. Because 3 = 3 . 1, all elements specified to be in S in this step are divisible by 3 and are therefore in A. To finish the proof, we must show that all integers in S generated using the second part of the recursive definition are in A. This consists of showing that x + y is in A whenever x and yare elements of S also assumed to be in A. Now if x and yare both in A, it follows that 3 I x and 3 I y. By part (i) of Theorem 1 of Section 3.4, it follows that 3 I x + y, completing the proof. <l1li In Example 12 we used mathematical induction over the set of positive integers and a recursive definition to prove a result about a recursively defined set. However, instead of using mathematical induction directly to prove results about recursively defined sets, we can use a more convenient form of induction known as structural induction. A proof by structural induction consists of two parts. These parts are BASIS STEP: Show that the result holds for all elements specified in the basis step of the recursive definition to be in the set. RECURSIVE STEP: Show that if the statement is true for each ofthe elements used to construct new elements in the recursive step of the definition, the result holds for these new elements. The validity of structural induction follows from the principle of mathematical induction for the nonnegative integers. To see this, let Pen) state that the claim is true for all elements of the set that are generated by n or fewer applications of the rules in the recursive step of a recursive definition. We will have established that the principle of mathematical induction implies the principle of structural induction if we can show that P (n) is true whenever n is a positive integer. In the basis step of a proof by structural induction we show that P(O) is true. That is, we show that the result is true of all elements specified to be in the set in the basis step of the definition. A consequence of the inductive step is that if we assume P(k) is true, it follows that P(k + 1) is true. When we have completed a proof using structural induction, we have shown that P(O) is true and that P(k) implies P(k + 1). By mathematical induction it 4-43 4.3 Recursive Definitions and Structural Induction 305 follows that Pen) is true for all nonnegative integers n. This also shows that the result is true for all elements generated by the recursive definition, and shows that structural induction is a valid proof technique. EXAMPLES OF PROOFS USING STRUCTUR AL INDUCTION Structural induction can be used to prove that all members of a set constructed recursively have a particular property. We will illustrate this idea by using structural induction to prove results about well-formed formulae, strings, and binary trees. For each proof, we have to carry out the appropriate basis step and the appropriate recursive step. For example, to use structural induction to prove a result about the set of well-formed formulae defined in Example 10, where we specify that T, F, and every propositional variable s are well-formed formulae and where we specify that if E and Fare well-formed formulae, then (-,E), (E 1\ F), (E v F), (E -* F), and (E B-F) are well-formed formulae, we need to complete this basis step and this recursive step. BASIS STEP: Show that the result is true for T, F, and s whenever s is a propositional variable. RECURSIVE STEP: Show that if the result is true for the compound propositions p and q, it is also true for (-'p), (p v q), (p 1\ q), (p -* q), and (p B-q). Example 13 illustrates how we can prove results about well-formed formulae using structural induction. EXAM PLE 13 Show that every well-formed formulae for compound propositions, as defined in Example 10, contains an equal number of left and right parentheses. Solution: BASIS STEP: Each of the formulae T, F, and s contains no parentheses, so clearly they contain an equal number of left and right parentheses. RECURSIVE STEP: Assume p and q are well-formed formulae each containing an equal number of left and right parentheses. That is, if I p and Iq are the number of left parentheses in p and q ,respectively, and r p and r q are the number of right parentheses in p and q , respectively, then I p = r p and Iq = r q' To complete the inductive step, we need to show that each of (-'p), (p v q), (p 1\ q), (p -* q), and (p B-q) also contains an equal number of left and right parentheses. The number of left parentheses in the first of these compound propositions equals I p + I and in each of the other compound propositions equals I p + Iq + 1. Similarly, the number of right parentheses in the first of these compound propositions equals r p + 1 and in each of the other compound propositions equals r p + r q + 1. Because I p = r p and Iq = r q, it follows that each of these compound expressions contains the same number of left and right parentheses. This completes the inductive proof. ... Suppose that P (w) is a propositional function over the set of strings w E L *. To use structural induction to prove that P (w) holds for all strings w E L *, we need to complete both a basis step and a recursive step. These steps are: BASIS STEP: Show that P()..) is true. RECURSIVE STEP: Assume that P(w) is true, wherew E L*.Showthatifx E L,thenP(wx) must also be true. Example 14 illustrates how structural induction can be used in proofs about strings. EXAMPLE 14 Use structural induction to prove that I(xy) = I(x) + l(y), where x and y belong to L*, the set of strings over the alphabet L. 306 4 / Induction and Recursion 4-44 Solution: We will base our proof on the recursive definition of the set �* given in Definition 2 and the definition of the length of a string in Example 9, which specifies that l()") = 0 and l(wx) = lew) + I when w E �* and x E �. Let P(y) be the statement thatl(xy) = lex) + l(y) whenever x belongs to �*. BASIS STEP: To complete the basis step, we must show that P()..) is true. That is, we must show that l(x)..) = lex) + l()") for all x E �*. Because l(x)..) = lex) = lex) + 0 = lex) + l()") for every string x, it follows that P()..) is true. RECURSIVE STEP: To complete the inductive step, we assume that P(y) is true and show that this implies that P(ya) is true whenever a E �. What we need to show is that l(xya) = I (x) + I (ya) for every a E �. To show this, note that by the recursive definition of I (w) (given in Example 9), we havel(xya) = l(xy) + 1 andl(ya) = l(y) + 1. And, by the inductive hypoth­ esis, l(xy) = lex) + l(y). We conclude that l(xya) = lex) + l(y) + 1 = lex) + l(ya). ... We can prove results about trees or special classes of trees using structural induction. For example, to prove a result about full binary trees using structural induction we need to complete this basis step and this recursive step. BASIS STEP: Show that the result is true for the tree consisting of a single vertex. RECURSIVE STEP: Show that if the result is true for the trees T\ and T2, then it is true for tree TJ • T2 consisting of a root r, which has T\ as its left subtree and T2 as its right subtree. Before we provide an example showing how structural induction can be used to prove a result about full binary trees, we need some definitions. We will recursively define the height h(T) and the number of vertices neT) of a full binary tree T. We begin by defining the height of a full binary tree. DEFINITION 7 We define the height h(T) ofa full binary tree T recursivel y. BASIS STEP: The height of the full binary tree T consisting of only a root r is h(T) = O. RECURSIVE STEP: If TI and T2 are full binary trees, then the full binary tree T = TI . T2 has height h(T) = 1 + max(h(T1), h(T2». Ifwe let neT) denote the number of vertices in a full binary tree, we observe that neT) satisfies the following recursive formula: BASIS STEP: The number of vertices neT) of the full binary tree T consisting of only a root r is neT) = 1. RECURSIVE STEP: IfTI and T2 are full binary trees, then the number of vertices ofthe full binary tree T = TJ . T2 is neT) = 1 + n(TJ) + n(T2). We now show how structural induction can be used to prove a result about full binary trees. THEO REM 21fT is a full binary tree T, then neT) :::: 2h(T)+J -1. Proof: We prove this inequality using structural induction. BASIS STEP: For the full binary tree consisting of just the root r the result is true because neT) = 1 and h(T) = 0, so that neT) = 1 :::: 20+\ -1 = 1. 4-45 4.3 Recursive Definitions and Structural Induction 307 INDUCTIVE STEP: For the inductive hypothesis we assume that n(Tl) ::::: 2h(TIHl -1 and n(T2) ::::: 2h(T2)+1 -1 whenever TI and T2 are full binary trees. By the recursive formulae for n(T) and h(T) we have n(T) = 1 + n(Td + n(T2) and h(T) = 1 + max(h(TI), h(T2». We find that n(T) = 1 + n(TI) + n(T2) by the recursive formula for neT) ::::: 1 + (2h(TJ)+ 1 -1) + (2h(T2)+ 1 -1) by the inductive hypothesis ::::: 2 . max(2h(Td+ I, 2h(T2)+ I) -1 because the sum of two terms is at most 2 times the larger = 2 . 2max(h(Td,h(T2»+1 -1 because max(2X, 21') = 2max(x,y) = 2 . 2h(T) -1 by the recursive definition of h(T) = 2h(T)+1 -1. This completes the inductive step. Generalized Induction <l We can extend mathematical induction to prove results about other sets that have the well­ ordering property besides the set of integers. Although we will discuss this concept in detail in Section 8.6, we provide an example here to illustrate the usefulness of such an approach. As an example, note that we can define an ordering on N x N, the ordered pairs of non­ negative integers, by specifying that (XI, YI) is less than or equal to (X2, Y2) if either XI < X2, or XI = X2 and YI < Y2; this is called the lexicographic ordering. The set N x N with this ordering has the property that every subset of N x N has a least element (see Supplementary Exercise 53 in Section 8.6). This implies that we can recursively define the terms am,n, with mEN and n E N, and prove results about them using a variant of mathematical induction, as illustrated in Example 15. EXAM PLE 15 Suppose that am,n is defined recursively for (m, n) E N x N by ao,O = 0 and jam-I,n + 1 ifn = 0 andm > 0 amn = , am,n-I + n ifn > O. Show that am,n = m + n(n + 1)/2 for all (m, n) E N x N, that is, for all pairs of nonnegative integers. So/ution:Wecanprovethatam,n = m + n(n + 1)/2 using a generalized version of mathematical induction. The basis step requires that we show that this formula is valid when (m, n) = (0, 0). The induction step requires that we show that if the formula holds for all pairs smaller than (m, n) in the lexicographic ordering ofN x N, then it also holds for (m, n). BASIS STEP: Let (m, n) = (0,0). Then by the basis case of the recursive definition of am,n we have ao,O = O. Furthermore, when m = n = 0, m + n(n + 1)/2= 0 + (0 . 1)/2 = O. This completes the basis step. INDUCTIVE STEP: Suppose that am',n' = m' + n'(n' + 1)/2 whenever (m', n') is less than (m, n) in the lexicographic ordering ofN x N. By the recursive definition, if n = 0, then am ,n = am-I,n + 1. Because (m -1, n) is smaller than (m, n), the inductive hypothesis tells us that am-I,n = m -1 + n(n + 1)/2, so that am,n = m -1 + n(n + 1)/2 + 1 = m + n(n + 1)/2, 308 4 / Induction and Recursion 4-46 giving us the desired equality. Now suppose that n > 0, so am,n = am,n-l + n. Because (m, n -1) is smallerthan(m, n), the inductive hypothesis tells us thatam,n-l = m + (n -1)n/2, so am,n = m + (n -1)n/2 + n = m + (n2 - n + 2n)/2 = m + n(n + 1)/2. This finishes the inductive step. <III As mentioned, we will justify this proof technique in Section 8.6. Exercises 1. Find f(I), f(2), f(3), and f(4) if fen) is defined recur­ sively by f(O) = 1 and for n = 0, 1, 2, ... a) fen + 1) = fen) + 2. b) fen + 1) = 3f(n). c) fen + 1) = 2!(n). d) fen + 1) = f(n)2 + fen) + 1. 2. Find f(I), f(2), f(3), f(4), and f(5) if fen) is defined recursively by f(O) = 3 and for n = 0, 1, 2, ... a) fen + 1) = -2f(n). b) fen + 1) = 3f(n) + 7. c) fen + 1) = f(np -2f(n) -2. d) fen + 1) = 3!(n)/3. 3. Find f(2), f(3), f(4), and f(5) if f is defined recursively by f(O) = -1, f(I) = 2 and for n = 1,2, ... a) fen + I) = fen) + 3f(n -1). b) fen + 1) = f(n)z fen -1). c) fen + 1) = 3f(np -4f(n -I)Z. d) fen + 1) = fen -I)/f(n). 4. Find f(2), f(3), f( 4), and f(5) if f is defined recursively by f(O) = f(I) = 1 and for n = 1,2, ... a) fen + 1) = fen) -fen -1). b) fen + 1) = f(n)f(n -1). c) fen + 1) = f(n)z + fen -1)3. d) fen + 1) = f(n)/f(n -1). 5. Detennine whether each of these proposed definitions is a valid recursive definition of a function f from the set of nonnegative integers to the set of integers. If f is well defined, find a fonnula for f(n) when n is a nonnegative integer and prove that your fonnula is valid. a) f(O) = 0, fen) = 2f(n -2) for n � 1 b) f(O) = 1, fen) = fen -1) - 1 for n � 1 c) f(O) = 2,1(1) = 3,f(n) = fen -1) -lfor n � 2 d) f(O) = 1, f(I) = 2, fen) = 2f(n - 2) for n � 2 e) f(O) = I,f(n) = 3f(n -l)ifnisoddandn � land f(n) = 9 fen -2) if n is even and n � 2 6. Detennine whether each of these proposed definitions is a valid recursive definition of a function f from the set of nonnegative integers to the set of integers. If f is well defined, find a fonnula for f(n) when n is a nonnegative integer and prove that your fonnula is valid. a) f(O) = I,f(n) = -fen -1) for n � 1 b) f(O) = 1, f(l) = 0, f(2) = 2, fen) = 2f(n - 3) for n�3 c) f(O) = 0, f(I) = 1, fen) = 2f(n + 1) for n � 2· d) f(O) = 0, f(I) = 1, fen) = 2f(n -1) for n � 1 e) f(O) = 2,f(n) = fen -1) ifn is odd andn � 1 and fen) = 2f(n -2) ifn � 2 7. Give a recursive definition of the sequence {an}, n = 1,2,3, ... if a) an = 6n. c) an = IOn. b) an = 2n + 1. d) an = 5. 8. Give a recursive definition of the sequence {an}, n=I,2,3, ... if a) an = 4n -2. b) an = 1 + (_I)n. c) an = n(n + 1). d) an = nZ. 9. Let F be the function such that F(n) is the sum of the first n positive integers. Give a recursive definition of F(n). 10. Give a recursive definition of Sm(n), the sum ofthe integer m and the nonnegative integer n. 11. Give a recursive definition of Pm(n), the product of the integer m and the nonnegative integer n. In Exercises 12-19 In is the nth Fibonacci number. 12. Prove that ff + Ii + ... + f� = fn fn+ I when n is a pos­ itive integer. 13. Prove that fl + /3 + ... + fZn-1 = hn when n is a pos­ itive integer. *14. Show that fn+dn-I -f� = (_I)n when n is a positive integer. *15. Show that fofl + fdz + ... + fZn-dzn = f�n when n is a positive integer. *16. Show that fo -fl + h -... -hn-I + hn = hn-I - 1 when n is a positive integer. 17. Detennine the number of divisions used by the Euclidean algorithm to find the greatest common divisor of the Fi­ bonacci numbers fn and fn+ I, where n is a nonnegative integer. VerifY your answer using mathematical induction. 18. Let A= U �J Show that fn ] fn-I when n is a positive integer. 4-47 19. By taking determinants of both sides of the equation in Ex­ ercise 18, prove the identity given in Exercise 14. (Recall that the determinant of the matrix I � � I is ad -be.) *20. Give a recursive definition ofthe functions max and min so that max(al, a2, ... ,an) and min(al' a2, ... ,an) are the maximum and minimum ofthe n numbers aI, a2, ... , an, respectively. *21. Let aI, a2, ... , an, and bl, b2, ... , bn be real numbers. Use the recursive definitions that you gave in Exercise 20 to prove these. a) max( -aI, -a2, ... , -an) = -min(al' a2, ... ,an) b) max(al + bl, a2 + b2, ... , an + bn) � max(al, a2, ... , an) + max(bl, b2, ... , bn) c) min(al + bl, a2 + b2, ... , an + bn) ::: min(al' a2, ... ,an) + min(bl' b2, ... , bn) 22. Show that the set S defined by 1 E S and s + t E S when­ ever s E Sand t E S is the set of positive integers. 23. Give a recursive definition of the set of positive integers that are multiples of 5. 24. Give a recursive definition of a) the set of odd positive integers. b) the set of positive integer powers of 3. c) the set of polynomials with integer coefficients. 25. Give a recursive definition of a) the set of even integers. b) the set of positive integers congruent to 2 modulo 3. c) the set of positive integers not divisible by 5. 26. Let S be the subset of the set of ordered pairs of integers defined recursively by Basis step: (0,0) E S. Recursive step: If(a, b) E S, then (a + 2, b + 3) E Sand (a+3,b+2)ES. a) List the elements of S produced by the first five appli­ cations of the recursive definition. b) Use strong induction on the number of applications of the recursive step of the definition to show that 51 a + b when (a, b) E S. c) Use structural induction to show that 51 a + b when (a, b) E S. 27. Let S be the subset of the set of ordered pairs of integers defined recursively by Basis step: (0,0) E S. Recursive step: If (a, b) E S, then (a, b + 1) E S, (a + 1, b + 1) E S, and (a + 2, b + 1) E S. a) List the elements of S produced by the first four ap­ plications of the recursive definition. b) Use strong induction on the number of applications of the recursive step of the definition to show that a � 2b whenever (a, b) E S. c) Use structural induction to show that a � 2b whenever (a, b) E S. 28. Give a recursive definition of each of these sets of ordered pairs of positive integers. [Hint: Plot the points in the set 4.3 Recursive Definitions and Structural Induction 309 in the plane and look for lines containing points in the set.] a) S = {(a, b) I a E Z+, bE Z+, and a + b is odd} b) S={(a,b)laEZ+,bEZ+,andalb} c) S = {(a, b) I a E Z+, b E Z+, and 31 a + b) 29. Give a recursive definition of each of these sets of or­ dered pairs of positive integers. Use structural induction to prove that the recursive definition you found is correct. [Hint: To find a recursive definition, plot the points in the set in the plane and look for patterns.] a) S = {(a, b) la E Z+, bE Z+, and a + b is even} b) S={(a,b)laEZ+,bEZ+,andaorbisodd} c) S = {(a,b)la E Z+,b E Z+,a +bisodd,and3Ib) 30. Prove that in a bit string, the string 01 occurs at most one more time than the string 10. 31. Define well-formed formulae of sets, variables represent­ ing sets, and operators from {-, U, n, -}. 32. a) Give a recursive definition of the function ones(s), which counts the number of ones in a bit string s. b) Use structural induction to prove that ones(st) = ones(s) + ones(t). 33. a) Give a recursive definition ofthe function m(s), which equals the smallest digit in a nonempty string of dec­ imal digits. b) Use structural induction to prove that m(st) = min(m(s), met»�. The reversal of a string is the string consisting ofthe symbols of the string in reverse order. The reversal of the string w is denoted by w R • 34. Find the reversal ofthe following bit strings. a) 0101 b) 11011 c) 100010010111 35. Give a recursive definition of the reversal of a string. [Hint: First define the reversal ofthe empty string. Then write a string w oflength n + 1 as xy, where x is a string oflength n, and express the reversal ofw in terms of xR andy.] *36. Use structural induction to prove that (WIW2)R = wfwf. 37. Give a recursive definition of Wi , where w is a string and i is a nonnegative integer. (Here Wi represents the con­ catenation of i copies of the string w .) *38. Give a recursive definition of the set of bit strings that are palindromes. 39. When does a string belong to the set A of bit strings de­ fined recursively by AEA Ox 1 E A if x E A, where A is the empty string? *40. Recursively define the set of bit strings that have more zeros than ones. 41. Use Exercise 37 and mathematical induction to show that l(wi) = i ·l(w), wherew is a string and i is a nonnegative integer. 310 4/ Induction and Recursion *42. Show that (wR)i = (wi)R whenever w is a string and i is a nonnegative integer; that is, show that the ith power of the reversal of a string is the reversal of the i th power of the string. 43. Use structural induction to show that neT) :::: 2h(T) + I, where T is a full binary tree, neT) equals the number of vertices ofT, and h(T) is the height ofT. The set ofleaves and the set of internal vertices of a full binary tree can be defined recursively. Basis step: The root r is a leaf of the full binary tree with exactly one vertex r. This tree has no internal vertices. Recursive step: The set ofleaves of the tree T = TI . T2 is the union of the set ofleaves ofT1 and the set ofleaves ofT2. The internal vertices of T are the root r of T and the union of the set of internal vertices of TI and the set of internal vertices ofT2. 44. Use structural induction to show that leT), the number of leaves of a full binary tree T, is 1 more than i(T), the number of internal vertices of T . 45. Use generalized induction as was done in Example 15 to show that if am,n is defined recursively by ao,O = 0 and \am-l n + 1 if n = 0 and m > 0 am n = 'l'f 0 ' am,n-l + 1 n > , thenam,n = m + n forall(m, n) E N X N. 46. Use generalized induction as was done in Example 15 to show that if am,n is defined recursively by al,l = 5 and {am-1n+2 ifn=landm>1 am n = '2'f 1 ' am,n-l + 1 n > , then am,n = 2(m + n) + 1 for all (m, n) E Z+ X Z+. *47. A partition of a positive integer n is a way to write n as a sum of positive integers where the order of terms in the sum does not matter. For instance, 7 = 3 + 2 + 1 + 1 is a partition of7. Let Pm equal the number of different par­ titions of m, and let P m,n be the number of different ways to express m as the sum of positive integers not exceed­ ing n. a) Show that Pm,m = Pm. b) Show that the following recursive definition for Pm,n is correct: 11 1 Pm,n = Pm,m 1 + Pm,m-l Pm,n-l + Pm-n,n ifm = 1 ifn = 1 ifm < n ifm = n > 1 ifm > n > 1. c) Find the number of partitions of 5 and of 6 using this recursive definition. Consider an inductive definition of a version of Ackermann's � function. This function was named after Wilhelm Ackermann, ..... a German mathematician who was a student ofthe great math­ ematician David Hilbert. Ackermann's function plays an im­ portant role in the theory of recursive functions and in the 4-48 study of the complexity of certain algorithms involving set unions. (There are several different variants of this function. All are called Ackermann's function and have similar proper­ ties even though their values do not always agree.) 12n if m = 0 o if m > 1 and n = 0 A(m,n)= 2 ifm :::: 1 andn = 1 A(m -1, A(m, n -1» ifm ::::landn :::: 2 Exercises 48-55 involve this version of Ackermann's function. 48. Find these values of Ackermann's function. a) A(I,O) c) A(I,I) b) A(O, 1) d) A(2,2) 49. Show that A(m, 2) = 4 whenever m :::: 1. SO. Show that A( 1, n) = 2n whenever n :::: 1. 51. Find these values of Ackermann's function. a) A(2, 3) *b) A(3,3) *52. Find A(3, 4). **53. Prove that A(m, n + 1) > A(m, n) whenever m and n are nonnegative integers. *54. Prove that A(m + 1, n) :::: A(m, n) whenever m and n are nonnegative integers. 55. Prove that A(i, j) :::: j whenever i and j are nonnegative integers. 56. Use mathematical induction to prove that a function F de­ fined by specifying F(O) and a rule for obtaining F(n + 1) from F(n) is well defined. 57. Use strong induction to prove that a function F defined by specifying F(O) and a rule for obtaining F(n + 1) from the values F(k) for k = 0,1,2, ... , n is well defined. 58. Show that each of these proposed recursive definitions of a function on the set of positive integers does not produce a well-defined function. a) F(n) = 1 + F(ln/2J) for n :::: 1 and F(I) = 1. b) F(n) = 1 + F(n -3) for n :::: 2, F(I) = 2, and F(2) = 3. c) F(n) = 1 + F(n/2) for n :::: 2, F(I) = I, and F(2) = 2. d) F(n) = 1 + F(n/2) if n is even and n :::: 2, F(n) = 1 -F(n -1) ifn is odd, and F(I) = 1. e) F(n) = 1 + F(n/2) if n is even and n :::: 2, F(n) = F(3n -1) ifn is odd and n :::: 3, and F(I) = 1. 59. Show that each of these proposed recursive definitions of a function on the set of positive integers does not produce a well-defined function. a) F(n) = 1 + F(l(n + 1)/2J) for n :::: 1 and F(I) = 1. b) F(n) = 1 + F(n -2) for n :::: 2 and F(I) = O. c) F(n) = 1 + F(n/3) for n :::: 3, F(I) = I, F(2) = 2, and F(3) = 3. d) F(n) = 1 + F(n/2) if n is even and n :::: 2, F(n) = 1 + F(n -2) ifn is odd, and F(I) = 1. e) F(n) = 1 + F(F(n - 1» ifn :::: 2 and F(I) = 2. 4-49 Exercises 60-62 deal with iterations ofthe logarithm function. Let log n denote the logarithm of n to the base 2, as usual. The function log(k) n is defined recursively by In ifk = 0 I (k) _ log(log(k-l) n) if log(k-l) n is defined og n -and positive undefined otherwise. The iterated logarithm is the function log* n whose value at n is the smallest nonnegative integer k such that log(k) n ::: I. 60. Find each of these values: a) log(2) 16 b) 10g(3) 256 c) log(3) 265536 d) log( 4) 2265536 . 61. Find the value oflog* n for each of these values of n: a) 2 b) 4 c) 8 d) 16 e) 256 f) 65536 g) 22048 62. Find the largest integer n such that log* n = 5. Detennine the number of decimal digits in this number. 4.4 Recursive Algorithms Introduction 4.4 Recursive Algorithms 311 Exercises 63-65 deal with values of iterated functions. Sup­ pose that f(n) is a function from the set of real numbers, or positive real numbers, or some other set of real numbers, to the set of real numbers such that f( n ) is monotonically increasing [that is, f(n ) < f(m) when n < m) and f(n) < n for all n in the domain of f.] The function f(k)(n) is defined recursively by (k) {n ifk = 0 f (n) = f(f(k-l)(n» ifk>O. Furthennore, let c be a positive real number. The iterated function f; is the number of iterations of f required to reduce its argument to c or less, so f;(n) is the smallest nonnegative integer k such that fk(n) ::: c. 63. Let f(n) = n -a, where a is a positive integer. Find a fonnula for f(k)(n). What is the value of fo(n) when n is a positive integer? 64. Let f(n) = nj2. Find a fonnula for f(k)(n). What is the value of fj(n) when n is a positive integer? 65. Let f(n) = ,.;n. Find a fonnula for j<k)(n). What is the value of fi(n) when n is a positive integer? Sometimes we can reduce the solution to a problem with a particular set of input to the solution of the same problem with smaller input values. For instance, the problem of finding the greatest common divisor of two positive integers a and b, where b > a, can be reduced to finding the greatest common divisor of a pair of smaller integers, namely, b mod a and a, because gcd(b moda, a) = gcd(a, b). When such a reduction can be done, the solution to the original problem can be found with a sequence of reductions, until the problem has been reduced to some initial case for which the solution is known. For instance, for finding the greatest common divisor, the reduction continues until the smaller of the two numbers is zero, because gcd(a, 0) = a when a > o. DEFINITION 1 unks� EXAMPLE 1 Extra� Exampleslliiiiiiiit We will see that algorithms that successively reduce a problem to the same problem with smaller input are used to solve a wide variety of problems. An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input. We will describe a variety of different recursive algorithms in this section. Give a recursive algorithm for computing n !, where n is a nonnegative integer. Solution: We can build a recursive algorithm that finds n!, where n is a nonnegative integer, based on the recursive definition ofn!, which specifies thatn! = n . (n -I)! when n is a positive integer, and that o! = 1. To find n! for a particular integer, we use the recursive step n times, each time replacing a value of the factorial function with the value of the factorial function at 312 41 Induction and Recursion 4-50 the next smaller integer. At this last step, we insert the value ofO!. The recursive algorithm we obtain is displayed as Algorithm 1. To help understand how this algorithm works, we trace the steps used by the algorithm to compute 4!. First, we first use the recursive step to write 4! = 4· 3!. We then use the recursive step repeatedly to write 3! = 3 . 2!, 2! = 2 . I!, and I! = 1 . O!. Inserting the value of O! = 1, and working back through the steps, we see that I! = 1 . 1 = 1, 2! = 2 . I! = 2, 3! = 3 . 2! = 3·2 = 6, and 4! = 4· 3! = 4·6 = 24. .... ALGORITHM 1 A Recursive Algorithm for Computing n!. procedure factorial( n: nonnegative integer) if n = 0 thenfactorial(n) := 1 elsefactorial(n) := n ·f actorial(n -1) Example 2 shows how a recursive algorithm can be constructed to evaluate a function from its recursive definition. EXAMPLE 2 Give a recursive algorithm for computing an, where a is a nonzero real number and n is a nonnegative integer. Solution: We can base a recursive algorithm on the recursive definition of an. This definition states that an+ 1 = a . an for n > 0 and the initial condition aO = 1. To find an, successively use the recursive step to reduce the exponent until it becomes zero. We give this procedure in Algorithm 2. .... ALGOR ITHM 2 A Recursive Algorithm for Computing an. procedure power(a: nonzero real number, n: nonnegative integer) if n = 0 then power(a , n) := I else power(a , n):= a ·power(a, n -I) EXAMPLE 3 Devise a recursive algorithm for computing bn mod m, where b, n, and m are integers with m � 2, n � 0, and I ::: b < m. Solution: We can base a recursive algorithm on the fact that bn mod m = (b .(bn-1 mod m)) mod m, which follows by Corollary 2 in Section 3.4, and the initial condition bO mod m = 1. We leave this as Exercise 12 for the reader at the end of the section. However, we can devise a much more efficient recursive algorithm based on the observation that bn mod m = (bnl2 mod m i mod m when n is even and bn modm = «bLn/2J modmi modm · b modm) modm when n is odd, which we describe in pseudocode as Algorithm 3. 4-51 4.4 Recursive Algorithms 313 We use a trace of Algorithm 3 with input b = 2, n = 5, and m = 3 to illustrate how it works. First, because n = 5 is odd we use the "else" clause to see that mpower(2, 5, 3) = (mpower(2, 2, 3f mod 3 . 2 mod 3) mod 3. We next use the "else if" clause to see that mpower(2, 2, 3) = mpower(2, 1, 3)2 mod 3. Using the "else" clause again, we see that mpower(2, 1,3) = (mpower(2, 0, 3)2 mod 3 ·2 mod 3) mod 3. Finally, using the "if" clause, we see that mpower(2, 0, 3) = 1. Working backwards, it follows that mpower(2, 1, 3) = (12 mod 3 . 2 mod 3) mod 3 = 2, so mpower(2, 2, 3) = 22 mod 3 = 1, and finally mpower(2, 5, 3) = (12 mod 3·2 mod 3) mod 3 = 2. � ALGORITHM 3 Recursive Modular Exponentiation. procedure mpower(b, n, m: integers with m :=:: 2, n :=:: 0) ifn = 0 then mpower(b, n, m) = 1 else if n is even then mpower(b, n, m) = mpower(b, n12, m)2 mod m else mpower(b, n, m) = (mpower(b, LnI2J, m)2 mod m . b mod m) mod m {mpower(b, n, m) = bn mod m} Next we give a recursive algorithm for finding greatest common divisors. EXAM PLE 4 Give a recursive algorithm for computing the greatest common divisor of two nonnegative integers a and b with a < b. Solution: We can base a recursive algorithm on the reduction gcd(a, b) = gcd(b mod a, a) and the condition gcd(O, b) = b when b > 0. This produces the procedure in Algorithm 4, which is a recursive version of the Euclidean algorithm. We illustrate the workings of Algorithm 4 with a trace when the input is a = 5, b = 8. With this input, the algorithm uses the "else" clause to find that gcd(5, 8) = gcd(8 mod 5,5) = gcd(3, 5). It uses this clause again to find that gcd(3, 5) = gcd(5 mod 3,3) = gcd(2, 3), then to get gcd(2, 3) = gcd(3 mod 2, 2) = gcd(1, 2), then to get gcd(1, 2) = gcd(2 mod 1, 1) = gcd(O, 1). Finally, to find gcd(O, 1) it uses the first step with a = ° to find that gcd(O, 1) = 1. Consequently, the algorithm finds that gcd(5, 8) = 1. � ALGORITHM 4 A Recursive Algorithm for Computing gcd(a, b). procedure gcd(a, b: nonnegative integers with a < b) if a = 0 then gcd(a, b) := b else gcd(a, b) := gcd(b mod a, a) We will now give recursive versions of searching algorithms that were introduced in Section 3.1. 314 4/ Induction and Recursion EXAMPLE 5 Express the linear search algorithm as a recursive procedure. 4-52 Solution: To search for x in the search sequence aI, a2, ... , an, at the ith step of the algorithm, x and aj are compared. If x equals aj, then i is the location of x. Otherwise, the search for x is reduced to a search in a sequence with one fewer element, namely, the sequence aj + I, ... , an. We can now give a recursive procedure, which is displayed as pseudocode in Algorithm 5. Let search (i, j, x) be the procedure that searches for x in the sequence aj, aj + I, ... , a j. The input to the procedure consists of the triple (l, n, x). The procedure terminates at a step if the first term of the remaining sequence is x or if there is only one term of the sequence and this is not x. If x is not the first term and there are additional terms, the same procedure is carried out but with a search sequence of one fewer term, obtained by deleting the first term of the search sequence. � ALGOR ITHM 5 A Recursive Linear Search Algorithm. procedure search(i, j, x: i, j, x integers, 1 ::::: i ::::: n, 1 ::::: j ::::: n) ifai = x then location := i else if i = j then location := 0 else search(i + 1, j, x) EXAMPLE 6 Construct a recursive version of a binary search algorithm. Solution: Suppose we want to locate x in the sequence at. a2, ... , an, integers in increasing order. To perform a binary search, we begin by comparing x with the middle term, aL(n+I)/2J. Our algorithm will terminate if x equals this term. Otherwise, we reduce the search to a smaller search sequence, namely, the first half of the sequence if x is smaller than the middle term of the original sequence, and the second half otherwise. We have reduced the solution of the search problem to the solution of the same problem with a sequence approximately half as long. We express this recursive version of a binary search algorithm as Algorithm 6. � ALGORITHM 6 A Recursive Binary Search Algorithm. procedure binary search(i, j, x: i, j, x integers, 1 ::::: i ::::: n, 1 ::::: j ::::: n) m := L(i + j)/2J if x = am then location := m else if (x < am and i < m) then binary search(x, i, m -1) else if (x > am and j > m) then binary search(x, m + 1, j) else location := 0 4-53 4.4 Recursive Algorithms 315 Proving Recursive Algorithms Correct Mathematical induction, and its variant strong induction, can be used to prove that a recursive algorithm is correct, that is, that it produces the desired output for all possible input value s. Examples 7 and 8 illustrate how mathematical induction or strong induction can be used to prove that recursive algorithms are correct. First, we will show that Algorithm 2 is correct. EXAMPLE 7 Prove that Algorithm 2, which computes powers of real numbers, is correct. EXAMPLE S Extra� ExamPlesllliiiiii-' Solution: We use mathematical induction on the exponent n. BASIS STEP: If n = 0, the first step of the algorithm tells us that power(a, 0) = 1. This is correct because aO = 1 for every nonzero real number a. This completes the basis step. INDUCTIVE STEP: The inductive hypothesis is the statement that power (a, k) = ak for all a =I 0 for the nonnegative integer k. That is, the inductive hypothesis is the statement that the algorithm correctly computes ak. To complete the inductive step, we show that ifthe inductive hypothesis is true, then the algorithm correctly computes ak+l. Because k + 1 is a positive integer, when the algorithm computes ak+l, the algorithm sets power (a, k + 1) = a· power (a, k). By the inductive hypothesis, we have power (a, k) = ak, so power (a, k + 1) = a . power (a, k) = a . ak = ak+l. This completes the inductive step. We have completed the basis step and the inductive step, so we can conclude that Algorithm 2 always computes an correctly when a =I 0 and n is a nonnegative integer. .... Generally, we need to use strong induction to prove that recursive algorithms are correct, rather than just mathematical induction. Example 8 illustrates this; it shows how strong induction can be used to prove that Algorithm 3 is correct. Prove that Algorithm 3, which computes modular powers, is correct. Solution: We use strong induction on the exponent n. BASIS STEP: Let b be an integer and m an integer with m ::: 2. When n = 0, the algorithm sets mpower( b, n, m) equal to 1. This is correct because b O mod m = 1. The basis step is complete. INDUCTIVE STEP: For the inductive hypothesis we assumethatmpower(b, j, m) = bj mod m for all integers 0 ::::: j < k whenever b is a positive integer and m is an integer with m ::: 2. To complete the inductive step, we show that if the inductive hypothesis is correct, then mpower(b, k, m) = bk modm. Because the recursive algorithm handles odd and even values of k differently, we split the inductive step into two cases. When k is even, we have mpower(b, k, m) = mpower(b, kj2, m)2 mod m = (bk/2 mod m)2 mod m = bk mod m, where we have used the inductive hypothesis to replace mpower(b, kj2, m) by bk/2 mod m. When k is odd, we have mpower(b, k, m) = «mpower( b, Lkj2j, m))2 mod m· b mod m) mod m = «bLk/2J mod mf mod m· b mod m) mod m = b2Lk/2J+l mod m = bk mod m, using Corollary 2 in Section 3.4, because 2Lkj2j + 1 = 2(k -1)j2 + 1 = k when k is odd. Here we have used the inductive hypothesis to replace mpower(b, Lkj2j, m) by bLk/2J mod m. This completes the inductive step. 316 41 Induction and Recursion 4-54 We have completed the basis step and the inductive step, so by strong induction we know that Algorithm 3 is correct. .... Recursion and Iteration A recursive definition expresses the value of a function at a positive integer in terms of the values ofthe function at smaller integers. This means that we can devise a recursive algorithm to evaluate a recursively defined function at a positive integer. Instead of successively reducing the computation to the evaluation of the function at smaller integers, we can start with the value ofthe function at one or more integers, the base cases, and successively apply the recursive definition to find the values ofthe function at successive larger integers. Such a procedure is called iterative. Often an iterative approach for the evaluation of a recursively defined sequence requires much less computation than a procedure using recursion (unless special-purpose recursive machines are used). This is illustrated by the iterative and recursive procedures for finding the nth Fibonacci number. The recursive procedure is given first. ALGOR ITHM 7 A Recursive Algorithm for Fibonacci Numbers. procedure fibonacci( n: nonnegative integer) if n = 0 thenfibonacci(O) := 0 else ifn = 1 thenfibonacci( I):= 1 elsefibonacci(n) := fibonacci(n -I) + fibonacci(n -2) When we use a recursive procedure to find In, we first express In as In-I + In-2. Then we replace both ofthese Fibonacci numbers by the sum of two previous Fibonacci numbers, and so on. When II or 10 arises, it is replaced by its value. Note that at each stage of the recursion, until II or 10 is obtained, the number of Fibonacci numbers to be evaluated has doubled. For instance, when we find 14 using this recursive algo­ rithm, we must carry out all the computations illustrated in the tree diagram in Figure 1. This tree consists of a root labeled with 14, and branches from the root to vertices labeled with the two Fibonacci numbers 1 3 and h that occur in the reduction of the computation of 14. Each subsequent reduction produces two branches in the tree. This branching ends when 10 and II are reached. The reader can verify that this algorithm requires In+ I -1 additions to find In. Now consider the amount of computation required to find In using the iterative approach in Algorithm 8. 11 10 FIGURE 1 Evaluating 14 Recursively. 4-55 UIlIsE:J EXAM PLE 9 4.4 Recursive Algorithms 317 ALGOR ITHM 8 An Iterative Algorithm for Computing Fibonacci Numbers. procedure iterative fibonacci(n: nonnegative integer) ifn = Otheny:= 0 else begin end x :=0 y:= 1 for i := 1 to n -1 begin end z :=x + y x :=y y:=z {y is the nth Fibonacci number} This procedure initializes x as 10 = 0 and y as II = 1. When the loop is traversed, the sum of x and y is assigned to the auxiliary variable z. Then x is assigned the value of y and y is assigned the value of the auxiliary variable z. Therefore, after going through the loop the first time, it follows that x equals II and y equals 10 + II = h. Furthermore, after going through the loop n -1 times, x equals In-I and y equals In (the reader should verify this statement). Only n -1 additions have been used to find In with this iterative approach when n > 1. Consequently, this algorithm requires far less computation than does the recursive algorithm. We have shown that a recursive algorithm may require far more computation than an iterative one when a recursively defined function is evaluated. It is sometimes preferable to use a recursive procedure even if it is less efficient than the iterative procedure. In particular, this is true when the recursive approach is easily implemented and the iterative approach is not. (Also, machines designed to handle recursion may be available that eliminate the advantage of using iteration.) The Merge Sort We now describe a recursive sorting algorithm called the merge sort algorithm. We will demonstrate how the merge sort algorithm works with an example before describing it in generality. Sort the list 8, 2, 4, 6, 9, 7, lO, 1,5,3 using the merge sort. Solution: A merge sort begins by splitting the list into individual elements by successively splitting lists in two. The progression of sublists fo r this example is represented with the balanced binary tree of height 4 shown in the upper half of Figure 2. Sorting is done by successively merging pairs of lists. At the first stage, pairs of individual elements are merged into lists of length two in increasing order. Then successive merges of pairs of lists are performed until the entire list is put into increasing order. The succession of merged lists in increasing order is represented by the balanced binary tree of height 4 shown in the lower half of Figure 2 (note that this tree is displayed "upside down"). .... In general, a merge sort proceeds by iteratively splitting lists into two sublists of equal length (or where one sublist has one more element than the other) until each sublist contains one 318 4/ Induction and Recursion 4-56 DeDlO� 824 6 9 7 10 1 5 3 / � 82469 7 10 153 / � / � 8 2 4 6 9 7 10 1 5 3 /\ /\ /\ /\ 8 2 4 6 9 7 10 5 /\ /\ 8 2 7 10 8 2 7 10 \/ \/ 2 8 4 6 9 7 10 5 \/ \/ \/ \/ 2 4 8 6 9 7 10 3 5 � / � / 2 4 6 8 9 135 7 10 � / 1 2 3 4 5 6 7 8 9 10 FIGURE 2 The Merge Sort of 8,2,4,6,9,7, 10, 1,5,3. 3 3 element. This succession of sublists can be represented by a balanced binary tree. The procedure continues by successively merging pairs of lists, where both lists are in increasing order, into a larger list with elements in increasing order, until the original list is put into increasing order. The succession of merged lists can be represented by a balanced binary tree. We can also describe the merge sort recursively. To do a merge sort, we split a list into two sublists of equal, or approximately equal, size, sorting each sublist using the merge sort algorithm, and then merging the two lists. The recursive version of the merge sort is given in Algorithm 9. This algorithm uses the subroutine merge, which is described in Algorithm 10. ALGOR ITHM 9 A Recursive Merge Sort. procedure mergesort(L = a\, ... , an} ifn> I then m := Ln/2J L\ :=a\,a2, ... ,am L2 := am+\, am+2,···, an L := merge(mergesort(L\},mergesort(L2}} {L is now sorted into elements in nondecreasing order} 4-57 4.4 Recursive Algorithms 319 TABLE 1 Merging the Two Sorted Lists 2, 3, 5, 6 and 1, 4. First List Second List Merged List Comparison 2356 14 1<2 2356 4 1 2<4 356 4 12 3<4 56 4 123 4<5 56 1234 123456 An efficient algorithm for merging two ordered lists into a larger ordered list is needed to implement the merge sort. We will now describe such a procedure. EXAMPLE 10 Merge the two lists 2, 3, 5, 6 and 1,4. Solution: Table 1 illustrates the steps we use. First, compare the smallest elements in the two lists, 2 and 1, respectively. Because I is the smaller, put it at the beginning of the merged list and remove it from the second list. At this stage, the first list is 2, 3, 5, 6, the second is 4, and the combined list is 1. Next, compare 2 and 4, the smallest elements of the two lists. Because 2 is the smaller, add it to the combined list and remove it from the first list. At this stage the first list is 3, 5, 6, the second is 4, and the combined list is 1,2. Continue by comparing 3 and 4, the smallest elements of their respective lists. Because 3 is the smaller of these two elements, add it to the combined list and remove it from the first list. At this stage the first list is 5, 6, and the second is 4. The combined list is 1, 2, 3. Then compare 5 and 4, the smallest elements in the two lists. Because 4 is the smaller of these two elements, add it to the combined list and remove it from the second list. At this stage the first list is 5, 6, the second list is empty, and the combined list is 1,2,3,4. Finally, because the second list is empty, all elements of the first list can be appended to the end of the combined list in the order they occur in the first list. This produces the ordered list 1, 2,3,4,5,6. � We will now consider the general problem of merging two ordered lists L, and L2 into an ordered list L. We will describe an algorithm for solving this problem. Start with an empty list L. Compare the smallest elements of the two lists. Put the smaller of these two ele­ ments at the left end of L, and remove it from the list it was in. Next, if one of L, and L2 is empty, append the other (nonempty) list to L, which completes the merging. If neither L, nor L2 is empty, repeat this process. Algorithm 10 gives a pseudocode description of this procedure. We will need estimates for the number of comparisons used to merge two ordered lists in the analysis of the merge sort. We can easily obtain such an estimate for Algorithm 10. Each time a comparison of an element from L, and an element from L2 is made, an additional element is added to the merged list L. However, when either L, or L2 is empty, no more comparisons are needed. Hence, Algorithm 10 is least efficient when m + n -2 comparisons are carried out, where m and n are the number of elements in L, and L2, respective ly, leaving one element in each of L, and L2• The next comparison will be the last one needed, because it will make one of these lists empty. Hence, Algorithm lOuses no more than m + n -1 comparisons. Lemma 1 summarizes this estimate. 320 4 / Induction and Recursion 4-58 LEMMA l ALGORITHM 10 Merging Two Lists. procedure merge( L I , L 2: sorted lists) L : = empty list while L I and L2 are both nonempty begin remove smaller of first element of LI and L2 from the list it is in and put it at the right end of L if removal of this element makes one list empty then remove all elements from the other list and append them to L end {L is the merged list with elements in increasing order} Two sorted lists with m elements and n elements can be merged into a sorted list using no more than m + n - 1 comparisons. Sometimes two sorted lists oflength m and n can be merged using far fewer than m + n - 1 comparisons. For instance, when m = 1, a binary search procedure can be applied to put the one element in the first list into the second list. This requires only rtog n 1 comparisons, which is much smaller than m + n - 1 = n, for m = 1. On the other hand, for some values of m and n, Lemma 1 gives the best possible bound. That is, there are lists with m and n elements that cannot be merged using fewer than m + n - 1 comparisons. (See Exercise 47 at the end of this section.) We can now analyze the complexity of the merge sort. Instead of studying the general problem, we will assume that n, the number of elements in the list, is a power of 2, say 2m. This will make the analysis less complicated, but when this is not the case, various modifications can be applied that will yield the same estimate. At the first stage of the splitting procedure, the list is split into two sublists, of2m -I elements each, at level 1 of the tree generated by the splitting. This process continues, splitting the two sublists with 2m-1 elements into four sublists of 2m-2 elements each at level 2, and so on. In general, there are 2k-1 lists at level k -1, each with 2m-HI elements. These lists at level k - 1 are split into 2k lists at level k, each with 2m-k elements. At the end of this process, we have 2m lists each with one element at level m. We start merging by combining pairs of the 2m lists of one element into 2m-1 lists, at level m -1, each with two elements. To do this, 2m -I pairs oflists with one element each are merged. The merger of each pair requires exactly one comparison. The procedure continues, so that at level k (k = m, m -1, m -2, ... ,3,2,1), 2k lists each with 2m-k elements are merged into 2k-1 lists, each with 2m-HI elements, at level k -1. To do this a total of 2k-1 mergers of two lists, each with 2m-k elements, are needed. But, by Lemma 1, each of these mergers can be carried out using at most 2m-k + 2m-k -1 = 2m-HI - 1 comparisons. Hence, going from level k to k - 1 can be accomplished using at most 2k-l(2m-k+1 -1) comparisons. Summing all these estimates shows that the number of comparisons required for the merge sort is at most m m m L 2k-I(2m-k+1 -1) = L 2m -L 2k-1 = m2m -(2m -1) = n logn - n + I, k=1 k=1 k=1 because m = log n and n = 2m• (We evaluated 'L,; = I 2m by noting that it is the sum of m identical terms, each equal to 2m• We evaluated 'L,; = I 2k-1 using the formula for the sum of the terms of a geometric progression from Theorem 1 of Section 2.4.) 4-59 4.4 Recursive Algorithms 321 This analysis shows that the merge sort achieves the best possible big-O estimate for the number of comparisons needed by sorting algorithms, as stated in the following theorem. THEOREM 1 The number of comparisons needed to merge sort a list with n elements is O(n log n). We describe another efficient algorithm, the quick sort, in the exercises. Exercises 1. Trace Algorithm 1 when it is given n = 5 as input. That is, show all steps used by Algorithm 1 to find 5!, as is done in Example 1 to find 4!. 2. Trace Algorithm 1 when it is given n = 6 as input. That is, show all steps used by Algorithm 1 to find 6!, as is done in Example 1 to find 4!. 3. Trace Algorithm 3 when it is given m = 5, n = 11, and b = 3 as input. That is, show all the steps Algorithm 3 uses to find 311 mod 5. 4. Trace Algorithm 3 when it is given m = 7, n = 10, and b = 2 as input. That is, show all the steps Algorithm 3 uses to find 2lOmod 7. 5. Trace Algorithm 4 when it finds gcd(8, 13). That is, show all the steps used by Algorithm 4 to find gcd(8, 13). 6. TraceAlgorithm 4whenitfindsgcd(12, 17). Thatis,show all the steps used by Algorithm 4 to find gcd(12, 17). 7. Give a recursive algorithm for computing nx whenever n is a positive integer and x is an integer, usingjust addition. 8. Give a recursive algorithm for finding the sum of the first n positive integers. 9. Give a recursive algorithm for finding the sum of the first n odd positive integers. 10. Give a recursive algorithm for finding the maximum of a finite set of integers, making use of the fact that the max­ imum of n integers is the larger of the last integer in the list and the maximum ofthe first n - 1 integers in the list. 11. Give a recursive algorithm for finding the minimum of a finite set of integers, making use of the fact that the mini­ mum of n integers is the smaller of the last integer in the list and the minimum of the the first n - 1 integers in the list. 12. Devise a recursive algorithm for finding xn mod m when­ ever n, x, and m are positive integers based on the fact thatxn mod m = (xn-1 mod m . x mod m) mod m. 13. Give a recursive algorithm for finding n! mod m whenever nand m are positive integers. 14. Give a recursive algorithm for finding a mode of a list of integers. (A mode is an element in the list that occurs at least as often as every other element.) 15. Devise a recursive algorithm for computing the greatest common divisor of two nonnegative integers a and b with a < b using the fact that gcd(a, b) = gcd(a, b -a). 16. Prove that the recursive algorithm for finding the sum of the first n positive integers you found in Exercise 8 is correct. 17. Describe a recursive algorithm for multiplying two non­ negative integers x and y based on the fact that xy = 2(x . (y /2)) when y is even and xy = 2(x . Iy /2J) + x when y is odd, together with the initial condition xy = 0 wheny = O. 18. Prove that Algorithm 1 for computing n! when n is a non­ negative integer is correct. 19. Prove that Algorithm 4 for computing gcd(a, b) when a and b are positive integers with a < b is correct. 20. Prove that the algorithm you devised in Exercise 11 is correct. 21. Prove that the recursive algorithm that you found in Ex­ ercise 7 is correct. 22. Prove that the recursive algorithm that you found in Ex­ ercise lOis correct. 23. Devise a recursive algorithm for computing n2 where n is a nonnegative integer using the fact that (n + 1)2 = n2 + 2n + 1. Then prove that this algorithm is correct. 24. Devise a recursive algorithm to find a2n , where a is a real number and n is a positive integer. [Hint: Use the equality a2n+1 = (a2n)2.] 25. How does the number of multiplications used by the al­ gorithm in Exercise 24 compare to the number of multi­ plications used by Algorithm 2 to evaluate a2n? *26. Use the algorithm in Exercise 24 to devise an algorithm for evaluating an when n is a nonnegative integer. [Hint: Use the binary expansion of n.] *27. How does the number of multiplications used by the al­ gorithm in Exercise 26 compare to the number of multi­ plications used by Algorithm 2 to evaluate an? 28. How many additions are used by the recursive and itera­ tive algorithms given in Algorithms 7 and 8, respective ly, to find the Fibonacci number h? 29. Devise a recursive algorithm to find the nth term ofthe se­ quence defined by ao = 1, al = 2, and an = an-I' an-2, for n = 2, 3,4, .... 30. Devise an iterative algorithm to find the nth term of the sequence defined in Exercise 29. 322 4 / Induction and Recursion 31. Is the recursive or the iterative algorithm for finding the sequence in Exercise 29 more efficient? 32. Devise a recursive algorithm to find the nth term of the sequence defined by ao = I, al = 2, a2 = 3, and an = an-I + an-2 + an-3, for n = 3,4,5, .... 33. Devise an iterative algorithm to find the nth term of the sequence defined in Exercise 32. 34. Is the recursive or the iterative algorithm for finding the sequence in Exercise 32 more efficient? 35. Give iterative and recursive algorithms for finding the nth term of the sequence defined by ao = I, al = 3, a2 = 5, and an = an-I . a�_2 . a�_3. Which is more efficient? 36. Give a recursive algorithm to find the number of partitions of a positive integer based on the recursive definition given in Exercise 47 in Section 4.3. 37. Give a recursive algorithm for finding the reversal of a bit string. (See the definition of the reversal of a bit string in the preamble of Exercise 34 in Section 4.3.) 38. Give a recursive algorithm for finding the string Wi, the concatenation of i copies of w, when w is a bit string. 39. Prove that the recursive algorithm for finding the reversal of a bit string that you gave in Exercise 37 is correct. 40. Prove that the recursive algorithm for finding the concate­ nation of i copies of a bit string that you gave in Exercise 38 is correct. *41. Give a recursive algorithm for tiling a 2n x 2n checker­ board with one square missing using right triominoes. 42. Give a recursive algorithm for triangulating a simple poly­ gon with n sides, using Lemma I in Section 4.2. 43. Give a recursive algorithm for computing values of the Ackermann function. [Hint: See the preamble to Exer­ cise 48 in Section 4.3.] 44. Use a merge sort to sort 4,3,2,5, 1,8,7,6. Show all the steps used by the algorithm. 45. Use a merge sort to sort b, d, a,f, g, h, Z, p, 0, k. Show all the steps used by the algorithm. 4.5 Program Correctness Introductio n 4-60 46. How many comparisons are required to merge these pairs of lists using Algorithm 10? a) 1,3,5,7,9; 2, 4, 6, 8, 10 b) 1,2,3,4,5; 6, 7, 8, 9, 10 c) 1,5,6,7,8; 2, 3,4,9, 10 47. Show that for all positive integers m and n there are lists with m elements and n elements, respectively, such that Algorithm lOuses m + n -1 comparisons to merge them into one sorted list. *48. What is the least number of comparisons needed to merge any two lists in increasing order into one list in increasing order when the number of elements in the two lists are a)I,4? b)2,4? c)3,4? d) 4, 4? *49. Prove that the merge sort algorithm is correct. The quicksort is an efficient algorithm. To sortal , a2, ... , an, this algorithm begins by taking the first element al and form-� ing two sublists, the first containing those elements that are less than aI, in the order they arise, and the second contain­ ing those elements greater than aI, in the order they arise. Then al is put at the end of the first sublist. This proce­ dure is repeated recursively for each sublist, until all sub­ lists contain one item. The ordered list of n items is ob­ tained by combining the sublists of one item in the order they occur. 50. Sort 3,5, 7, 8, 1,9,2,4,6 using the quick sort. 51. Letal, a2, ... , an be a list ofn distinct real numbers. How many comparisons are needed to form two sublists from this list, the first containing elements less than al and the second containing elements greater than al? 52. Describe the quick sort algorithm using pseudocode. 53. What is the largest number of comparisons needed to or­ der a list of four elements using the quick sort algorithm? 54. What is the least number of comparisons needed to order a list of four elements using the quick sort algorithm? 55. Determine the worst-case complexity of the quick sort algorithm in terms of the number of comparisons used. Suppose that we have designed an algorithm to solve a problem and have written a program to implement it. How can we be sure that the program always produces the correct answer? After all the bugs have been removed so that the syntax is correct, we can test the program with sample input. It is not correct if an incorrect result is produced for any sample input. But even if the program gives the correct answer for all sample input, it may not always produce the correct answer (unless all possible input has been tested). We need a proof to show that the program always gives the correct output. Program verification, the proof of correctness of programs, uses the rules of inference and proof techniques described in this chapter, including mathematical induction. Because an incor­ rect program can lead to disastrous results, a large amount of methodology has been constructed for verifying programs. Efforts have been devoted to automating program verification so that it 4-61 4.5 Program Correctness 323 can be carried out using a computer. However, only limited progress has been made toward this goal. Indeed, some mathematicians and theoretical computer scientists argue that it will never be realistic to mechanize the proof of correctness of complex programs. Some of the concepts and methods used to prove that programs are correct will be introduced in this section. Many different methods have been devised for proving that programs are correct. We will discuss a method for program verification introduced by Tony Hoare in this section; many other methods have been devised. Furthermore, we will not develop a complete methodology for program verification in this book. This section is meant to be a brief introduction to the area of program verification, which ties together the rules of logic, proof techniques, and the concept of an algorithm. Program Verification A program is said to be correct if it produces the correct output for every possible input. A proof that a program is correct consists of two parts. The first part shows that the correct answer is obtained ifthe program terminate s. This part of the proof establishes the partial correctness of the program. The second part of the proof shows that the program always terminate s. To specify what it means for a program to produce the correct output, two propositions are used. The first is the initial assertion, which gives the properties that the input values must have. The second is the final assertion, which gives the properties that the output of the program should have, if the program did what was intended. The appropriate initial and final assertions must be provided when a program is checked. DEFINITION 1 A program, or program segment, S is said to be partially correct with respect to the initial assertion p and the final assertion q if whenever p is true for the input values of S and S terminates, then q is true for the output values of S. The notation p{S}q indicates that the program, or program segment, S is partially correct with respect to the initial assertion p and the final assertion q. linlls� EXAMPLE l Extra� Note: The notation p{S}q is known as a Hoare triple. Tony Hoare introduced the concept of partial correctness. Note that the notion of partial correctness has nothing to do with whether a program termi­ nates; it focuses only on whether the program does what it is expected to do if it terminate s. A simple example illustrates the concepts of initial and final assertions. Show that the program segment y:=2 z:= x + y Examples IIiiiiiI is correct with respect to the initial assertion p: x = 1 and the final assertion q: z = 3. Solution: Suppose that p is true, so that x = I as the program begins. Then y is assigned the value 2, and z is assigned the sum of the values of x and y, which is 3. Hence, S is correct with respect to the initial assertion p and the final assertion q. Thus, p{ S}q is true. ..... Rules of Inference A useful rule of inference proves that a program is correct by splitting the program into a series of subprograms and then showing that each subprogram is correct. 324 4 / Induction and Recursion 4-62 lInkS� Suppose that the program S is split into subprograms Sl and S2. Write S = Sl; S2 to indicate that S is made up of Sl followed by S2. Suppose that the correctness of Sl with respect to the initial assertion p and final assertion q, and the correctness of S2 with respect to the initial assertion q and the final assertion r, have been established. It follows that if p is true and S 1 is executed and terminates, then q is true; and if q is true, and S2 executes and terminates, then r is true. Thus, if p is true and S = Sl; S2 is executed and terminates, then r is true. This rule of inference, called the composition rule, can be stated as This rule of inference will be used later in this section. Next, some rules of inference for program segments involving conditional statements and loops will be given. Because programs can be split into segments for proofs of correctness, this will let us verify many different programs. Conditional Statements First, rules of inference for conditional statements will be given. Suppose that a program segment has the form if condition then S where S is a block of statements. Then S is executed if condition is true, and it is not executed when condition is false. To verify that this segment is correct with respect to the initial assertion p and final assertion q, two things must be done. First, it must be shown that when p is true and condition is also true, then q is true after S terminate s. Second, it must be shown that when p is true and condition is false, then q is true (because in this case S does not execute). This leads to the following rule of inference: (p /\ condition){S}q (p /\ -,condition) --+ q ... p{if condition then S}q. Example 2 illustrates how this rule of inference is used. c. ANTHONY R. HOARE (BORN 1934) Tony Hoare is currently Professor of Computer Science at Oxford University, England, and is a Fellow of the Royal Society. Hoare has made many important contributions to the theory of programming languages and to programming methodology. He was the first person to define a programming language based on how programs could be proved to be correct with respect to their specifications. Hoare is also the creator ofthe quick sort, one ofthe most commonly used and studied sorting algorithms (see the preamble to Exercise 50 in Section 4.4). Hoare is a noted writer in the technical and social aspects of computer science. 4-63 4.5 Program Correctness 325 EXAM PLE 2 Verify that the program segment if x > y then y:=x is correct with respect to the initial assertion T and the final assertion y ::: x. Solution: When the initial assertion is true and x > y, the assignment y := x is carried out. Hence, the final assertion, which asserts that y ::: x, is true in this case. Moreover, when the initial assertion is true and x > y is false, so that x ::: y, the final assertion is again true. Hence, using the rule of inference for program segments of this type, this program is correct with respect to the given initial and final assertions. .... Similarly, suppose that a program has a statement of the form if condition then SI else S2 If condition is true, then S 1 executes; if condition is false, then S2 executes. To verify that this program segment is correct with respect to the initial assertion p and the final assertion q, two things must be done. First, it must be shown that when p is true and condition is true, then q is true after SI terminate s. Second, it must be shown that when p is true and condition is false, then q is true after S2 terminates. This leads to the following rule of inference: (p 1\ condition){Sdq (p 1\ -.condition){S2}q ... p{if condition then SI else S2}q. Example 3 illustrates how this rule of inference is used. EXAMPLE 3 Verify that the program segment ifx < 0 then abs:= -x else abs:= x is correct with respect to the initial assertion T and the final assertion abs = Ix I. Solution: Two things must be demonstrated. First, it must be shown that if the initial assertion is true and x < 0, then abs = Ix I. This is correct, because when x < 0 the assignment statement abs := -x sets abs = -x, which is Ixl by definition when x < O. Second, it must be shown that if the initial assertion is true and x < 0 is false, so that x ::: 0, then abs = Ix I. This is also correct, because in this case the program uses the assignment statement abs := x, and x is Ix I 326 4 / Induction and Recursion 4-64 unks� by definition when x � 0, so abs := x. Hence, using the rule of inference for program segments of this type, this segment is correct with respect to the given initial and final assertions. ..... Loop Invariants Next, proofs of correctness of while loops will be described. To develop a rule of inference for program segments of the type while condition S note that S is repeatedly executed until condition becomes false. An assertion that remains true each time S is executed must be chosen. Such an assertion is called a loop invariant. In other words, p is a loop invariant if(p /\ condition){S}p is true. Suppose that p is a loop invariant. It follows that if p is true before the program segment is executed, p and -.condition are true after termination, ifit occurs. This rule of inference is (p /\ condition){S}p :. p{while condition S}(-. condition /\ p). The use of a loop invariant is illustrated in Example 4. EXAMPLE 4 A loop invariant is needed to verify that the program segment Extra� Examples � i := 1 factorial := 1 while i < n begin i := i + 1 factorial := factorial· i end terminates withfactorial = n! when n is a positive integer. Let p be the assertion ''factorial = i! and i ::::: n." We first prove that p is a loop invariant. Suppose that, at the beginning of one execution of the while loop, p is true and the condition of the while loop holds; in other words, assume that factorial = i! and that i < n. The new values inew andfactorialnew ofi andfactorial are inew = i + 1 andfactorialnew = factorial· (i + 1) = (i + I)! = inewL Because i < n, we also have inew = i + 1 ::::: n. Thus, p is true atthe end of the execution of the loop. This shows that p is a loop invariant. Now we consider the program segment. Just before entering the loop, i = 1 ::::: nand factorial = 1 = I! = i! both hold, so p is true. Because p is a loop invariant, the rule of inference just introduced implied that if the while loop terminates, it terminates with p true and with i < n false. In this case, at the end,factorial = i! and i ::::: n are true, but i < n is false; in other words, i = n and factorial = i! = n!, as desired. 4-65 4.5 Program Correctness 327 Finally, we need to check that the while loop actually terminates. At the beginning of the program i is assigned the value I, so after n -I traversals of the loop, the new value of i will be n, and the loop terminates at that point. .... A final example will be given to show how the various rules of inference can be used to verify the correctness of a longer program. EXAMPLE 5 We will outline how to verify the correctness of the program S for computing the product of two integers. procedure multiply(m, n: integers) S {ifn < o then a :=-n 1 else a := n S {k:= 0 2 x:= 0 {while k < a begin S3 x:= x +m k:= k + I end S {ifn < 0 then product := -x 4 else product := x The goal is to prove that after Sis executed, product has the value mn. The proof of correctness can be carried out by splitting S into four segments, with S = SI; S2; S3; S4, as shown in the listing of S. The rule of composition can be used to build the correctness proof. Here is how the argument proceeds. The details will be left as an exercise for the reader. Let p be the initial assertion "m andn are integers." Then, it can be shown that p{Sdq is true, when q is the proposition p /\ (a = In I). Next, let r be the proposition q /\ (k = 0) /\ (x = 0). It is easily verified that q{S2}r is true. It can be shown that "x = mk and k ::: a" is an invariant for the loop in S3. Furthermore, it is easy to see that the loop terminates after a iterations, with k = a, so x = ma at this point. Because r implies that x = m . 0 and 0 ::: a, the loop invariant is true before the loop is entered. Because the loop terminates with k = a, it follows that r{S3}s is true where s is the proposition "x = ma and a = In I." Finally, it can be shown that S4 is correct with respect to the initial assertion s and final assertion t, where t is the proposition "product = mn." Putting all this together, because p{Sdq, q{S2}r, r{S3}s, and S{S4}t are all true, it follows from the rule of composition that p {S} t is true. Furthermore, because all four segments terminate, S does terminate. This verifies the correctness of the program. .... Exercises 1. Prove that the program segment y:= 1 z :=x + y is correct with respect to the initial assertion x = 0 and the final assertion z = 1. 2. Verify that the program segment if x < 0 then x := 0 is correct with respect to the initial assertion T and the final assertion x 2: O. 328 4 / Induction and Recursion 3. Verify that the program segment x :=2 z:= x + y if y > 0 then z:= z + I else z:= 0 is correct with respect to the initial assertion y = 3 and the final assertion z = 6. 4. Verify that the program segment if x < y then min:= x else min:= y is correct with respect to the initial assertion T and the final assertion (x :s y 1\ min = x) V (x > Y 1\ min = y). *5. Devise a rule of inference for verificatiQn of partial cor­ rectness of statements of the form if condition I then SI else if condition 2 then S2 else Sn where SI, S2, ... , Sn are blocks. 6. Use the rule of inference developed in Exercise 5 to verify that the program if x < 0 then y:= -2lxl/x else if x > 0 then y:= 21xl/x else if x = 0 then y:=2 is correct with respect to the initial assertion T and the final assertion y = 2. Key Terms and Results TERMS sequence: a function with domain that is a subset of the set of integers geometric progression: a sequence of the form a, ar ,ar2, ••• , where a and r are real numbers arithmetic progression: a sequence of the form a, a + d, a + 2d, ... , where a and d are real numbers the principle of mathematical inductio n: The statement "In Pen) is true if P(I) is true and Vk[P(k) --+ P(k + I)] is true. basis step: the proof of P( I) in a proof by mathematical in­ duction ofVnP(n) 4-66 7. Use a loop invariant to prove that the following program segment for computing the nth power, where n is a positive integer, of a real number x is correct. power:= I i := I while i :s n begin power := power * x i := i + I end *8. Prove that the iterative program for finding In given in Section 4.4 is correct. 9. Provide all the details in the proof of correctness given in Example 5. 10. Suppose that both the conditional statement Po --+ PI and the program assertion PI {S}q are true. Show that Po{S}q also must be true. 11. Suppose that both the program assertion p{S}qo and the conditional statementqo --+ ql are true. Show that p{S}ql also must be true. 12. This program computes quotients and remainders. r :=a q :=0 whiler ::: d begin r :=r-d q := q + I end Verify that it is partially correct with respect to the ini­ tial assertion "a and d are positive integers" and the final assertion "q and r are integers such that a = dq + rand o :s r < d." 13. Use a loop invariant to verify that the Euclidean algorithm (Algorithm 6 in Section 3.6) is partially correct with re­ spect to the initial assertion "a and b are positive integers" and the final assertion "x = gcd(a, b)." inductive step: the proof of P(k) --+ P(k + I) for all pos­ itive integers k in a proof by mathematical induction of VnP(n) strong induction: The statement "In P(n) is true if P(I) is true and Vk[(P(I) 1\ ... 1\ P(k)) --+ P(k + I)] is true. well-ordering property: Every nonempty set of nonnegative integers has a least element. recursive definition of a function: a definition of a function that specifies an initial set of values and a rule for obtaining values ofthis function at integers from its values at smaller integers 4-67 recursive definition of a set: a definition of a set that specifies an initial set of elements in the set and a rule for obtaining other elements from those in the set structural induction: a technique for proving results about recursively defined sets recursive algorithm: an algorithm that proceeds by reducing a problem to the same problem with smaller input merge sort: a sorting algorithm that sorts a list by splitting it in two, sorting each of the two resulting lists, and merging the results into a sorted list Review Questions 1. a) Can you use the principle of mathematical induction to find a formula for the sum of the first n terms of a sequence? b) Can you use the principle of mathematical induction to determine whether a given formula for the sum of the first n terms of a sequence is correct? c) Find a formula for the sum of the first n even positive integers, and prove it using mathematical induction. 2. a) For which positive integers n is lln + 17 ::: 2n? b) Prove the conjecture you made in part (a) using math­ ematical induction. 3. a) Which amounts of postage can be formed using only 5-cent and 9-cent stamps? b) Prove the conjecture you made using mathematical induction. c) Prove the conjecture you made using strong induction. d) Find a proof of your conjecture different from the ones you gave in (b) and (c). 4. Give two different examples. of proofs that use strong induction. 5. a) State the well-ordering property for the set of positive integers. b) Use this property to show that every positive integer can be written as the product of primes. 6. a) Explain why a function is well-defined ifit is defined recursively by specifYing f(l) and a rule for finding f(n) from f(n -1). b) Provide a recursive definition of the function f(n) = (n + I)!. 7. a) Give a recursive definition of the Fibonacci numbers. b) Show that fn > an-2 whenever n ::: 3, where fn is the nth term of the Fibonacci sequence and a = (l + ./5)/2. Supplementar y Exercises 1. Use mathematical induction to show that � + � + 3 9 1.. + ... + 1.. - 1 -1.. whenever n is a positive 27 3" -3" integer. Supplementary Exercises 329 iteration: a procedure based on the repeated use of operations in a loop program correctness: verification that a procedure always produces the correct result loop invariant: a property that remains true during every traversal of a loop initial assertion: the statement specifYing the properties of the input values of a program final assertion: the statement specifYing the properties the output values should have if the program worked correctly 8. a) Explain why a sequence an is well defined if it is de­ fined recursively by specifYing a I and a2 and a rule for finding an from aI, a2, ... , an-I for n = 3,4,5, .... b) Find the value of an if al = 1, a2 = 2, and an = an-I +an-2 + ... +al, forn = 3,4, 5, .... 9. Give two examples of how well-formed formulae are defined recursively for different sets of elements and operators. 10. a) Give a recursive definition ofthe length of a string. b) Use the recursive definition from part (a) and struc­ tural induction to prove that l(xy) = l(x) + l(y). 11. a) What is a recursive algorithm? b) Describe a recursive algorithm for computing the sum of n numbers in a sequence. 12. Describe a recursive algorithm for computing the greatest common divisor of two positive integers. 13. a) Describe the merge sort algorithm. b) Use the merge sort algorithm to putthe list 4, 10, 1,5, 3,8, 7, 2, 6, 9 in increasing order. c) Give a big-O estimate for the number of comparisons used by the merge sort. 14. a) Does testing a computer program to see whether it produces the correct output for certain input values verifY that the program always produces the correct output? b) Does showing that a computer program is partially correct with respect to an initial assertion and a final assertion verifY that the program always produces the correct output? If not, what else is needed? 15. What techniques can you use to show that a long com­ puter program is partially correct with respect to an initial assertion and a final assertion? 16. What is a loop invariant? How is a loop invariant used? 2. Use mathematical induction to show that 13 + 33 + 53 + ... + (2n + 1)3 = (n + 1)2(2n2 + 4n + 1) whenevern is a positive integer. 330 4 / Induction and Recursion 3. Use mathematical induction to show that 1 . 20 + 2 . 21 + 3.22 + ... + n ·2n-1 = (n -1)· 2n + 1 whenevern isa positive integer. 4. Use mathematical induction to show that 1 li n -+-+ ... + =--1 . 3 3 . 5 (2n - 1 )(2n + 1) 2n + 1 whenever n is a positive integer. 5. Show that 1 li n -+-+ ... + =--1·44·7 (3n-2)(3n+l) 3n+l whenever n is a positive integer. 6. Use mathematical induction to show that 2n > n2 + n whenever n is an integer greater than 4. 7. Use mathematical induction to show that 2n > n3 when­ ever n is an integer greater than 9. 8. Find an integer N such that 2n > n4 whenever n is greater than N. Prove that your result is correct using mathemat­ ical induction. 9. Use mathematical induction to prove that a -b is a factor of an - bn whenever n is a positive integer. 10. Use mathematical induction to prove that 9 divides n3 + (n + 1)3 + (n + 2)3 whenever n is a nonnegative integer. 11. Use mathematical induction to prove this formula for the sum of the terms of an arithmetic progression. a + (a + d) + ... + (a + nd) = (n + 1)(2a + nd)/2 12. Suppose that aj == bj (mod m) for j = 1,2, ... , n. Use mathematical induction to prove that n n a) L aj == L bj (modm). j=1 j=1 n n b) n aj == n bj (modm). j=1 j=1 13. Show that if n is a positive integer, then n k + 4 n(3n + 7) Lk= I k(k + 1)(k + 2) = 2(n + 1)(n + 2) 14. For which positive integers n is n + 6 < (n2 -8n)/16? Prove your answer using mathematical induction. 15. (Requires calculus) Suppose that I(x) = � and g(x) = xeX• Use mathematical induction together with the product rule and the fact that I'(x) = � to prove that g(n)(x) = (x + n)eX whenever n is a positive integer. 16. (Requires calculus) Suppose that I(x) = � and g(x) = eCx, where c is a constant. Use mathematical induction together with the chain rule and the fact that I' (x) = � to prove that g(n) = cnecx whenever n is a positive integer. �17. Determine which Fibonacci numbers are even, and use a form of mathematical induction to prove your conjecture. 4-68 *18. Determine which Fibonacci numbers are divisible by 3. Use a form of mathematical induction to prove your conjecture. *19. Provethatlkln + lk+l/n+1 = In+k+1 for all nonnegative integers n, where k is a nonnegative integer and J; denotes the ith Fibonacci number. The sequence of Lucas numbers is defined by 10 = 2, h = 1, and In = In-I + In-2 for n = 2,3,4, .... 20. Show that In + In+2 = In+1 whenever n is a positive in­ teger, where J; and Ii are the ith Fibonacci number and ith Lucas number, respectively. 21. Show that 15 + It + ... + I� = Inln+1 + 2 whenever n is a nonnegative integer and Ii is the ith Lucas number. *22. Use mathematical induction to show that the product of any n consecutive positive integers is divisible by n!. [Hint: Use the identity m(m + 1)· ··(m +n -1)/n! = (m -l)m(m + 1)··· (m + n -2)/n! + m(m+ 1)··· (m + n -2)/(n -I)!.] 23. Use mathematical induction to show that (cos x + i sin x)n =-cos nx + i sin nx whenever n is a positive in­ teger. (Here i is the square root of -1.) [Hint: Use the identities cos( a + b) = cos a cos b -sin a sin band sin(a + b) = sin a cosb + cosa sin b.] *24. Use mathematical induction to show that LJ = I cos j X = cos[(n + l)x /2] sin(nx /2)/ sin(x /2) whenever n is a pos­ itive integer and sin(x /2) :/= O. 25. Use mathematical induction to prove that LJ=I j22j = n22n+1 -n2n+2 + 3· 2n+1 -6 for every positive inte­ ger n. 26. (Requires calculus) Suppose that the sequence XI, X2, ••• , Xn, •.• is recursively defined by XI = 0 and Xn+l = "';xn + 6. a) Use mathematical induction to show that XI < X2 < ... < Xn < ... , that is, the sequence {xn} is monoton­ ically increasing. b) Use mathematical induction to prove that Xn < 3 for n = 1,2, .... c) Show that limn-HXl Xn = 3. 27. Show if n is a positive integer with n :::: 2, then t _1_ = (n -1)(3n + 2) j=2 j2 - 1 4n(n + 1). 28. Use mathematical induction to prove Theorem 1 in Sec­ tion 3.6, that is, show if b is a positive integer, b > 1, and n is a positive integer, then n can be expressed uniquely in the form n = akbk + ak_Ibk-1 + ... + alb + ao. *29. A lattice point in the plane is a point (x, y) where both x and yare integers. Use mathematical induction to show that at least n + 1 straight lines are needed to ensure that every lattice point (x, y) with x :::: 0, y :::: 0, and x + y :'S n lies on one of these lines. 30. (Requires calculus) Use mathematical induction and the product rule to show that if n is a positive integer and 4-69 f,(x), fz(x), ... , fn(x), are all differentiable functions, then (f,(x)fz(x)··· fn(x))' f,(x)fz(x)··· fn(x) = f{(x) + f�(x) + ... + f�(x) f,(x) fz(x) fn(x)· 31. (Requires material in Section 3.7) Suppose that B = MAM-', where A and B are n x n matrices and M is invertible. Show that Bk = MAkM-' for all positive in­ tegers k. 32. Use mathematical induction to show that if you draw lines in the plane you only need two colors to color the regions formed so that no two regions that have an edge in com­ mon have a common color. 33. Show that n! can be represented as the sum of n of its distinct positive divisors whenever n � 3. [Hint: Use in­ ductive loading. First try to prove this result using mathe­ matical induction. By examining where your proof fails, find a stronger statement that you can easily prove using mathematical induction.] *34. Use mathematical induction to prove that ifx" X2, ••• , Xn are positive real numbers with n � 2, then 35. Use mathematical induction to prove that ifn people stand in a line, where n is a positive integer, and ifthe first per­ son in the line is a woman and the last person in line is a man, then somewhere in the line there is a woman directly in front of a man. *36. Suppose that for every pair of cities in a country there is a direct one-way road connecting them in one direction or the other. Use mathematical induction to show that there is a city that can be reached from every other city either directly or via exactly one other city. 37. Use mathematical induction to show that when n circles divide the plane into regions, these regions can be col­ ored with two different colors such that no regions with a common boundary are colored the same. *38. Suppose that among a group of cars on a circular track there is enough fuel for one car to complete a lap. Use mathematical induction to show that there is a car in the group that can complete a lap by obtaining gas from other cars as it travels around the track. 39. Show that if n is a positive integer, then 'tt(2j -1) (j;; 1/ k) = n(n + 1)/2. 40. A unit or Egyptian fraction is a fraction of the form l/n, where n is a positive integer. In this exercise, we will use strong induction to show that a greedy algorithm can be used to express every rational number p / q with Supplementary Exercises 331 o < p / q < I as the sum of distinct unit fractions. At each step of the algorithm, we find the smallest positive integer n such that 1/ n can be added to the sum without exceed­ ing p / q. For example, to express 517 we first start the sum with 1/2. Because 5/7 -1/2 = 3/14 we add 1/5 to the sum because 5 is the smallest positive integer k such that 1/ k < 3/14. Because 3/14 -1/5 = 1/70, the a1go­ rithmterminates,showingthat5j7 = 1/2 + 1/5 + 1170. Let T (p) be the statement that this algorithm terminates for all rational numbers p / q with 0 < p / q < 1. We will prove that the algorithm always terminates by showing that T(p) holds for all positive integers p. a) Show that the basis stepT(l) holds. b) Suppose that T(k) holds for positive integers k with k < p. That is, assume that the algorithm terminates for all rational numbers k / r, where 1 � k < p. Show that if we start with p / q and the fraction 1/ n is se­ lected in the first step of the algorithm, then p / q = p' /q' + l/n, where p' = np -q and q' = nq. After considering the case where p/q = l/n, use the in­ ductive hypothesis to show that the greedy algorithm terminates when it begins with p' / q' and complete the inductive step. The McCarthy 91 functioQ. (defined by John McCarthy, one of the founders of artificial intelligence) is defined using the rule {n -10 M(n) = M(M(n + 11)) for all positive integers n. ifn > 100 ifn � 100 41. By successively using the defining rule for M(n), find a) M(102). b) M(101). c) M(99). d) M(97). e) M(87). t) M(76). *>il:t2. Show that the function M(n) is a well-defined function from the set of positive integers to the set of positive inte­ gers. [Hint: Prove that M(n) = 91 for all positive integers n with n � 101.] 43. Is this proofthat 1 1 1 3 -+-+ ... + -- -1 ·2 2·3 (n -l)n -2 -n whenever n is a positive integer, correct? Justify your answer. Basis step: The result is true when n = 1 because 1 3 1 1·2 2 Inductive step: Assume that the result is true for n. Then 1 1 1 1 -+-+ ... + +---1·22·3 (n-1)n n(n+1) = � _ .!. + (.!. __ 1_) 2 n n n+1 3 1 = ----2 n + 1 Hence, the result is true for n + 1 if it is true for n. This completes the proof. 332 4 / Induction and Recursion 44. Suppose that AI. A2, ... , An are a collection of sets. Suppose that R2 = Al EB A2 and Rk = Rk-I EB Ak for k = 3,4, ... , n. Use mathematical induction to prove that x E Rn if and only if x belongs to an odd number of the sets A I, A2, ... , An. (Recall that S EB T is the symmetric difference of the sets Sand T defined in Section 2.2.) *45. Show that n circles divide the plane into n2 -n + 2 re­ gions if every two circles intersect in exactly two points and no three circles contain a common point. *46. Show that n planes divide three-dimensional space into (n3 + 5n + 6)/6 regions ifany three of these planes have a point in common and no four contain a common point. *47. Use the well-ordering property to show that .../i is irra­ tional. [Hint: Assume that .../i is rational. Show that the set of positive integers of the form b.../i has a least el­ ement a. Then show that a.../i -a is a smaller positive integer of this form.] 48. A set is well ordered if every nonempty subset of this set has a least element. Determine whether each of the following sets is well ordered. a) the set of integers b) the set of integers greater than -100 c) the set of positive rationals d) the set of positive rationals with denominator less than 100 49. a) Show that if aI, a2, ... , an are positive integers, then gcd(al' a2, ... , an-I, an) = gcd(al' a2, ... , an-2, gcd(an_I' an». b) Use part (a), together with the Euclidean algorithm, to develop a recursive algorithm for computing the great­ est common divisor of a set of n positive integers. *50. Describe a recursive algorithm for writing the greatest common divisor of n positive integers as a linear combi­ nation of these integers. 51. Find an explicit formula for f(n)iff(l) = 1 andf(n) = unkS� 4-70 fen -1) + 2n - 1 for n ::: 2. Prove your result using mathematical induction. **52. Give a recursive definition of the set of bit strings that contain twice as many Os as 1 s. 53. Let S be the set of bit strings defined recursively by A E S and Ox E S, x I E S if XES, where A is the empty string. a) Find all strings in S oflength not exceeding five. b) Give an explicit description of the elements of S. 54. Let S be the set of strings defined recursively by abc E S, bac E S, and acb E S, where a, b, and c are fixed letters; and for all XES, abcx E S; abxc E S, axbc E S, and xabc E S, where x is a variable representing a string of letters. a) Find all elements of S oflength eight or less. b) Show that every element of S has a length divisible by three. The set B of all balanced strings of parentheses is defined recursively by A E B, where A is the empty string; (x) E B, x y E B ifx, Y E B. 55. Show that (00) is a balanced string of parentheses and (0» is not a balanced string of parentheses. 56. Find all balanced strings of parentheses with exactly six symbols. 57. Find all balanced strings of parentheses with four or fewer symbols. 58. Use induction to show that if x is a balanced string of parentheses, then the number of left parentheses equals the number of right parentheses in x. Define the function N on the set of strings of parentheses by N(A) = 0, N(O = 1, NO) = -I, N(uv) = N(u) + N(v), where A is the empty string, and u and v are strings. It can be shown that N is well defined. JOHN McCARTHY (BORN 1927) John McCarthy was born in Boston. He grew up in Boston and in Los Angeles. He studied mathematics as both an undergraduate and a graduate student, receiving his B.S. in 1948 from the California Institute of Technology and his Ph.D. in 1951 from Princeton. After graduating from Princeton, McCarthy held positions at Princeton, Stanford, Dartmouth, and M.1. T. He held a position at Stanford from 1962 until 1994, and is now an emeritus professor there. At Stanford, he was the director of the Artificial Intelligence Laboratory, held a named chair in the School of Engineering, and was a senior fellow in the Hoover Institution. McCarthy was a pioneer in the study of artificial intelligence, a term he coined in 1955. He worked on problems related to the reasoning and information needs required for intelligent computer behavior. Mc­ Carthy was among the first computer scientists to design time-sharing computer systems. He developed LISp, a programming language for computing using symbolic expressions. He played an important role in using logic to verify the correctness of computer programs. McCarthy has also worked on the social implications of computer technology. He is currently working on the problem of how people and computers make conjectures through assumptions that complications are absent from situations. McCarthy is an advocate of the sustainability of human progress and is an optimist about the future of humanity. He has also begun writing science fiction stories. Some of his recent writing explores the possibility that the world is a computer program written by some higher force. Among the awards McCarthy has won are the Turing Award from the Association for Computing Machinery, the Research Excellence Award of the International Conference on Artificial Intelligence, the Kyoto Prize, and the National Medal of Science. 4-71 59. Find a) N «». c) N «( )« » . b) N 0»0)(0. d) N « )«( »)« »). **60. Show that a string w of parentheses is balanced if and only if N(w) = 0 and N(u) ::: 0 whenever u is a prefix of w, that is, w = uv. *61. Give a recursive algorithm for finding all bal­ anced strings of parentheses containing n or fewer symbols. 62. Give a recursive algorithm for finding gcd(a, b), where a and b are nonnegative integers, based on these facts: gcd(a, b) = gcd(b, a) if a > b, gcd(O, b) = b, gcd(a, b) = 2gcd(aI2, b12) if a and b are even, gcd(a, b) = gcd(aI2, b) if a is even and b is odd, and gcd(a, b) = gcd(a, b -a). 63. Verify the program segment if x > y then x :=y with respect to the initial assertion T and the final asser­ tionx :::s y. *64. Develop a rule of inference for verifying recursive pro­ grams and use it to verify the recursive program for com­ puting factorials given in Section 4.4. 65. Devise a recursive algorithm that counts the number of times the integer 0 occurs in a list of integers. Exercises 66-73 deal with some unusual sequences, infor­ mally called self-generating sequences, produced by simple recurrence relations or rules. In particular, Exercises 66-71 deal with the sequence {a(n)} defined by a(n) = n -a(a(n - 1» for n ::: 1 and a(O) = o. (This sequence, as well as those in Exercises 70 and 71, are defined in Douglas Hofstader's fascinating book GOdel, Escher, Bach ([Ho99]). 66. Find the first 10 terms of the sequence {a(n)} defined in the preamble to this exercise. Computer Projects Write programs with these input and output. * *1. Given a 2n x 2n checkerboard with one square miss­ ing, construct a tiling of this checkerboard using right triominoes. **2. Generate all well-formed formulae for expressions in­ volving the variables x, y, and z and the operators {+, *, I, -} with n or fewer symbols. **3. Generate all well-formed formulae for propositions with n or fewer symbols where each symbol is T, F, one of the propositional variables p and q, or an operator from {--', v, 1\, -+, �}. 4. Given a string, find its reversal. 5. Given a real number a and a nonnegative integer n, find an using recursion. Computer Projects 333 *67. Prove that this sequence is well defined. That is, show that a(n) is uniquely defined for all nonnegative integers n. **68. Prove that a(n) = L(n + 1)t-tJ where t-t = (-1 + ./5)/2. [Hint: First show for all n > 0 that (t-tn -Lt-tnJ) + (t-t2n -Lt-t2nJ) = 1. Then show for all real numbers a with 0 :::s a < 1 and a =11 -t-t that L(1 + t-t)(I - a)J + La + t-t J = 1, considering the cases 0 :::s a < 1 -t-t and 1 -t-t < a < 1 separately.] *69. Use the formula from Exercise 68 to show that a(n) = a(n -1) if t-tn -Lt-tnJ < 1 -t-t and a(n) = a(n -1) + 1 otherwise. 70. Find the first 10 terms of each of the following self­ generating sequences: a) a(n) = n -a(a(a(n -1») for n ::: 1 with a(O) = 0 b) a(n) = n -a(a(a(a(n - 1»» for n ::: 1 with a(O) = o . c) a(n) = a(n -a(n -1» + a(n -a(n - 2» for n ::: 3 with a(l) = 1 and a(2) = 1 71. Find the first 10 terms of both the sequences m(n) and fen) defined by the following pair of interwoven recurrence relations: men) = n -f(m(n -1», fen) = n -m(f(n -1» for n ::: 1 with f(O) = 1 and m(O) = o. Golomb's self-generating sequence is the unique nonde­ creasing sequence of positive integers a I, a2, a3, ... , which has the property that it contains exactly ak occurrences of k for each positive integer k. 72. Find the first 20 terms of Golomb's self-generating se­ quence. *73. Show that if f(n) is the largest integer m such that am = n, where am is the mth term of Golomb's self-generating sequence, then fen) = L�= I ak and f(f(n» = L�= I kak. 6. Given a real number a and a nonnegative integer n, find a2n using recursion. *7. Given a real number a and a nonnegative integer n, find an using the binary expansion of n and a recursive algorithm for computing a2k • 8. Given two integers not both zero, find their greatest com­ mon divisor using recursion. 9. Given a list of integers and an element x, locate x in this list using a recursive implementation of a linear search. 10. Given a list of integers and an element x, locate x in this list using a recursive implementation of a binary search. 11. Given a nonnegative integer n, find the nth Fibonacci number using iteration. 334 4 / Induction and Recursion 12. Given a nonnegative integer n, find the nth Fibonacci number using recursion. 13. Given a positive integer, find the number of parti­ tions of this integer. (See Exercise 47 of Section 4.3.) Computa tions and Explorations 4-72 14. Given positive integers m andn, find A(m, n), the value of Ackermann's function at the pair (m, n). (See the pream­ ble to Exercise 48 of Section 4.3.) 15. Given a list of n integers, sort these integers using the merge sort. Use a computational program or programs you have written to do these exercises. 1. What are the largest values of n for which n! has fewer than 100 decimal digits and fewer than 1000 decimal digits? 2. Determine which Fibonacci numbers are divisible by 5, which are divisible by 7, and which are divisible by II. Prove that your conjectures are correct. 3. Construct tilings using right triominoes of various 16 x 16, 32 x 32, and 64 x 64 checkerboards with one square missing. 4. Explore which m x n checkerboards can be completely Writing Projects Respond to these with essays using outside sources. 1. Describe the origins of mathematical induction. Who were the first people to use it and to which problems did they apply it? 2. Explain how to prove the Jordan curve theorem for sim­ ple polygons and describe an algorithm for determining whether a point is in the interior or exterior of a simple polygon. 3. Describe how the triangulation of simple polygons is used in some key algorithms in computational geometry. covered by right triominoes. Can you make a conjecture that answers this question? **5. Implement an algorithm for determining whether a point is in the interior or exterior of the simple polygon. **6. Implement an algorithm for triangulating a simple polygon. 7. Which values of Ackermann's function are small enough that you are able to compute them? 8. Compare either the number of operations or the time needed to compute Fibonacci numbers recursively versus that needed to compute them iteratively. 4. Describe a variety of different applications of the Fibonacci numbers to the biological and the physical sciences. 5. Discuss the uses of Ackermann's function both in the theory of recursive definitions and in the analysis ofthe complex­ ity of algorithms for set unions. 6. Discuss some of the various methodologies used to es­ tablish the correctness of programs and compare them to Hoare's methods described in Section 4.5. 7. Explain how the ideas and concepts of program correctness can be extended to prove that operating systems are secure. CHAPTER 5.1 The Basics of Counting 5.2 The Pigeonhole Principle 5.3 Permutations and Combinations 5.4 Binomial Coefficients 5.5 Generalized Permutations and Combinations 5.6 Generating Permutations and Combinations Counting � ombinatori cs, the study of arrangements of objects, is an important part of discrete math­ \l...,. ematics. This subject was studied as long ago as the seventeenth century, when combi­ natorial questions arose in the study of gambling games. Enumeration, the counting of objects with certain properties, is an important part of combinator ics. We must count objects to solve many different types of problems. For instance, counting is used to determine the complexity of algorithms. Counting is also required to determine whether there are enough telephone num­ bers or Internet protocol addresses to meet demand. Furthermore, counting techniques are used extensively when probabilities of events are computed. The basic rules of counting, which we will study in Section 5.1, can solve a tremendous variety of problems. For instance, we can use these rules to enumerate the different telephone numbers possible in the United States, the allowable passwords on a computer system, and the different orders in which the runners in a race can finish. Another important combinatorial tool is the pigeonhole principle, which we will study in Section 5.2. This states that when objects are placed in boxes and there are more objects than boxes, then there is a box containing at least two objects. For instance, we can use this principle to show that among a set of 15 or more students, at least 3 were born on the same day of the week. We can phrase many counting problems in terms of ordered or unordered arrangements of the objects of a set. These arrangements, called permutations and combinations, are used in many counting problems. For instance, suppose the 100 top finishers on a competitive exam taken by 2000 students are invited to a banquet. We can count the possible sets of 100 students that will be invited, as well as the ways in which the top 10 prizes can be awarded. Another problem in combinatorics involves generating all the arrangements of a specified kind. This is often important in computer simulations. We will devise algorithms to generate arrangements of various types. 5.1 The Basics of Counting 5-1 Introduction Suppose that a password on a computer system consists of six, seven, or eight characters. Each of these characters must be a digit or a letter of the alphabet. Each password must contain at least one digit. How many such passwords are there? The techniques needed to answer this question and a wide variety of other counting problems will be introduced in this section. Counting problems arise throughout mathematics and computer science. For example, we must count the successful outcomes of experiments and all the possible outcomes of these experiments to determine probabilities of discrete events. We need to count the number of operations used by an algorithm to study its time complexity. We will introduce the basic techniques of counting in this section. These methods serve as the foundation for almost all counting technique s. Basic Cou nting Principles We will present two basic counting principles, the product rule and the sum rule. Then we will show how they can be used to solve many different counting problems. The product rule applies when a procedure is made up of separate tasks. 335 336 5 I Counting Extra� 5-2 THE PRODUCT RULE Suppose that a procedure can be broken down into a sequence of two tasks. If there are n 1 ways to do the first task and for each of these ways of doing the first task, there are n2 ways to do the second task, then there are nln2 ways to do the procedure. Examples � Examples 1-10 show how the product rule is used. EXAMPLE 1 A new company with just two employees, Sanchez and Patel, rents a floor of a building with 12 offices. How many ways are there to assign different offices to these two employees? Solution: The procedure of assigning offices to these two employees consists of assigning an office to Sanchez, which can be done in 12 ways, then assigning an office to Patel different from the office assigned to Sanchez, which can be done in 11 ways. By the product rule, there are 12· 11 = 132 ways to assign offices to these two employees. ... EXAMPLE 2 The chairs of an auditorium are to be labeled with a letter and a positive integer not exceeding 100. What is the largest number of chairs that can be labeled differently? Solution: The procedure of labeling a chair consists of two tasks, namely, assigning one of the 26 letters and then assigning one of the 100 possible integers to the seat. The product rule shows that there are 26 . 100 = 2600 different ways that a chair can be labeled. Therefore, the largest number of chairs that can be labeled differently is 2600. ... EXAMPLE 3 There are 32 microcom puters in a computer center. Each microcom puter has 24 ports. How many different ports to a microcomputer in the center are there? Solution: The procedure of choosing a port consists of two tasks, first picking a microcomputer and then picking a port on this microcomputer. Because there are 32 ways to choose the micro­ computer and 24 ways to choose the port no matter which microcomputer has been selected, the product rule shows that there are 32 . 24 = 768 ports. ... An extended version of the product rule is often useful. Suppose that a procedure is carried out by performing the tasks T1, T2, ... , T m in sequence. If each task Ti, i = 1, 2, ... , n, can be done in ni ways, regardless of how the previous tasks were done, then there are n 1 • n2· .... nm ways to carry out the procedure. This version of the product rule can be proved by mathematical induction from the product rule for two tasks (see Exercise 60 at the end of the section). EXAMPLE 4 How many different bit strings oflength seven are there? Solution: Each of the seven bits can be chosen in two ways, because each bit is either 0 or 1. Therefore, the product rule shows there are a total of 27 = 128 different bit strings of length seven. ... EXAMPLE 5 How many different license plates are available if each plate contains a sequence of three letters followed by three digits (and no sequences ofletters are prohibited, even if they are obscene)? 26 choices 10 choices for each for each letter digit Solution: There are 26 choices for each of the three letters and ten choices for each of the three digits. Hence, by the product rule there are a total of 26·26·26· 10· 10· 10 = 17,576,000 possible license plates. ... EXAMPLE 6 Counting Functions How many functions are there from a set with m elements to a set with n elements? 5-3 5.1 The Basics of Counting 337 Solution: A function corresponds to a choice of one of the n elements in the codomain for each of the m elements in the domain. Hence, by the product rule there are n . n ..... n = nm functions from a set with m elements to one with n elements. For example, there are 53 different functions from a set with three elements to a set with five elements. ..... EXAM PLE 7 Counting One-to-One Functions How many one-to-one functions are there from a set with m elements to one with n elements? EXAM PLES unkS� Solution: First note when m > n there are no one-to-one functions from a set with m elements to a set with n elements. Now let m ::::: n. Suppose the elements in the domain are ai, a2, ... , am. There are n ways to choose the value of the function at al. Because the function is one-to-one, the value of the function at a2 can be picked in n -1 ways (because the value used for al cannot be used again). In general, the value of the function at ak can be chosen in n -k + 1 ways. By the product rule, there are n(n -1)(n -2)·.· (n -m + 1) one-to-one functions from a set with m elements to one with n elements. For example, there are 5 . 4 . 3 = 60 one-to-one functions from a set with three elements to a set with five elements. ..... The Telephone Numbering Plan The format of telephone numbers in North America is specified by a numbering plan. A telephone number consists of 10 digits, which are split into a three-digit area code, a three-digit office code, and a four-digit station code. Because of signaling considerations, there are certain restrictions on some of these digits. To specify the allowable format, let X denote a digit that can take any of the values 0 through 9, let N denote a digit that can take any of the values 2 through 9, and let Y denote a digit that must be a 0 or a 1. Two numbering plans, which will be called the old plan and the new plan, will be discussed. (The old plan, in use in the 1960s, has been replaced by the new plan, but the recent rapid growth in demand for new numbers for mobile phones and devices make even this new plan obsolete . In this example, the letters used to represent digits follow the conventions of the North American Numbering Plan.) As will be shown, the new plan allows the use of more numbers. In the old plan, the formats of the area code, office code, and station code are NYJ(, NNJ(, and X\'X¥, respectivel y, so that telephone numbers had the fo rm NYX -NNX -X\'XX In the new plan, the formats of these codes are NIT, NIT, and XXXX, respectively, so that telephone numbers have the form NXX-NXX-X\'XX How many different North American telephone numbers are possible under the old plan and under the new plan? Solution: By the product rule, there are 8·2· 10 = 160 area codes with format NYX and 8· 10 . 10 = 800 area codes with format NXX. Similarly, by the product rule, there are 8 . 8 . 10 = 640 office codes with format NNX. The product rule also shows that there are 10· 10 . 10· 10 = 10,000 station codes with formatX\'XX Consequently, applying the product rule again, it follows that under the old plan there are 160·640· 10,000 = 1,024,000,000 different numbers available in North America. Under the new plan there are 800·800· 10,000 = 6,400,000,000 different numbers available. 338 5 / Counting 5-4 EXAMPLE 9 What is the value of k after the following code has been executed? k:= 0 for il := 1 to nl for i2 := 1 to n2 for im := 1 to nm k:= k+ 1 Solution.' The initial value of k is zero. Each time the nested loop is traversed, I is added to k. Let Ti be the task of traversing the ith loop. Then the number of times the loop is traversed is the number of ways to do the tasks TJ, T2, ••• , Tm. The number of ways to carry out the task Tj, j = I, 2, ... , m, is n j, because the j th loop is traversed once for each integer i j with 1 .s: i j .s: n j. By the product rule, it follows that the nested loop is traversed n I n2 ... nm times. Hence, the final value of k is nln2'" nm. .... EXAMPLE 10 Counting Subsets of a Finite Set Use the product rule to show that the number of different subsets of a finite set S is 21SI• Solution: Let S be a finite set. List the elements of S in arbitrary order. Recall from Section 2.2 that there is a one-to-one correspondence between subsets of S and bit strings of length lSI. Namely, a subset of S is associated with the bit string with a 1 in the ith position if the ith element in the list is in the subset, and a 0 in this position otherwise. By the product rule, there are 21s1 bit strings oflength lSI. Hence, IP(S)I = 21sl• .... The product rule is often phrased in terms of sets in this way: If AI, A2, ... , Am are finite sets, then the number of elements in the Cartesian product of these sets is the product of the number of elements in each set. To relate this to the product rule, note that the task of choosing an element in the Cartesian product A I X A2 X ... x Am is done by choosing an element in A I, an element in A2, ... , and an element in Am. By the product rule it follows that We now introduce the sum rule. THE SUM RULE If a task can be done either in one of n I ways or in one of n2 ways, where none of the set of n I ways is the same as any of the set of n2 ways, then there are n I + n2 ways to do the task. Example 11 illustrates how the sum rule is used. EXAM PLE 11 Suppose that either a member of the mathematics faculty or a student who is a mathematics major is chosen as a representative to a university committee. How many different choices are there for this representative if there are 37 members of the mathematics faculty and 83 mathematics majors and no one is both a faculty member and a student? Solution: There are 37 ways to choose a member of the mathemati cs faculty and there are 83 ways to choose a student who is a mathematics major. Choosing a member ofthe mathema tics 5-5 5.1 The Basics of Counting 339 faculty is never the same as choosing a student who is a mathematics major because no one is both a faculty member and a student. By the sum rule it follows that there are 37 + 83 = 120 possible ways to pick this representative. .... We can extend the sum rule to more than two tasks. Suppose that a task can be done in one of n I ways, in one of n2 ways, ... , or in one of nm ways, where none of the set of ni ways of doing the task is the same as any of the set of nj ways, for all pairs i and j with I � i < j � m. Then the number of ways to do the task is nl + n2 + ... + nm. This extended version of the sum rule is often useful in counting problems, as Examples 12 and 13 show. This version of the sum rule can be proved using mathematical induction from the sum rule for two sets. (This is Exercise 59 at the end of the section.) EXAM PLE 12 A student can choose a computer project from one of three lists. The three lists contain 23, 15, and 19 possible projects, respectively. No project is on more than one list. How many possible projects are there to choose from? Solution: The student can choose a project by selecting a project from the first list, the second list, or the third list. Because no project is on more than one list, by the sum rule there are 23 + 15 + 19 = 57 ways to choose a project, .... EXAMPLE 13 What is the value of k after the following code has been executed? k :=0 foril:= 1 toni k:= k+ 1 for i2 := 1 to n2 k:= k+ 1 for im := 1 to nm k:= k + 1 Solution: The initial value of k is zero. This block of code is made up of m different loops. Each time a loop is traversed, 1 is added to k. To determine the value of k after this code has been executed, we need to determine how many times we traverse a loop. Note that there are ni ways to traverse the ith loop. Because we only traverse one loop at a time, the sum rule shows that the final value of k, which is the number of ways to traverse one of the m loops is nl +n2 + ... +nm. .... The sum rule can be phrased in terms of sets as: If AI, A2, •.• , Am are disjoint finite sets, then the number of elements in the union of these sets is the sum of the numbers of elements in the sets. To relate this to our statement of the sum rule, note there are I Ai I ways to choose an element from Ai for i = 1, 2, ... , m. Because the sets are disjoint, when we select an element from one of the sets Ai, we do not also select an element from a different set Aj• Consequently, by the sum rule, because we cannot select an element from two of these sets at the same time, the number of ways to choose an element from one of the sets, which is the number of elements in the union, is 340 5 / Counting EXAMPLE 14 Extra� Examples� 5-6 This equality applies only when the sets in question are disjoint. The situation is much more complicated when these sets have elements in common. That situation will be briefly discussed later in this section and discussed in more depth in Chapter 7. More Complex Counting Problems Many counting problems cannot be solved using just the sum rule or just the product rule. However, many complicated counting problems can be solved using both of these rules in combination. In a version of the computer language BASIC, the name of a variable is a string of one or two alphanumeric characters, where uppercase and lowercase letters are not distinguished. (An alphanumeric character is either one of the 26 English letters or one of the 10 digits.) Moreover, a variable name must begin with a letter and must be different from the five strings of two characters that are reserved for programming use. How many different variable names are there in this version of BASIC? Solution: Let V equal the number of different variable names in this version of BASIC. Let VI be the number of these that are one character long and V2 be the number of these that are two characters long. Then by the sum rule, V = VI + V2. Note that VI = 26, because a one-character variable name must be a letter. Furthermore, by the product rule there are 26 . 36 strings oflength two that begin with a letter and end with an alphanumeric character. However, five of these are excluded, so V2 = 26 . 36 -5 = 931. Hence, there are V = VI + V2 = 26 + 931 = 957 different names for variables in this version of BASIC. .... EXAMPLE 15 Each user on a computer system has a password, which is six to eight characters long, where each character is an uppercase letter or a digit. Each password must contain at least one digit. How many possible passwords are there? Solution: Let P be the total number of possible passwords, and let P6, P7, and Pg denote the number of possible passwords of length 6, 7, and 8, respectively. By the sum rule, P = P6 + P7 + P8. We will now find P6, P7, and P8• Finding P6 directly is difficult. To find P6 it is easier to find the number of strings of uppercase letters and digits that are six characters long, including those with no digits, and subtract from this the number of strings with no digits. By the product rule, the number of strings of six characters is 366, and the number of strings with no digits is 266• Hence, P6 = 366 -266 = 2,176,782,336 -308,915,776 = 1,867,866,560. Similarly, it can be shown that and P7 = 367 -267 = 78,364,164,096 -8,031,810,176 = 70,332,353,920 P8 = 368 -268 = 2,821,109,907,456 -208,827,064,576 = 2,612,282,842,880. Consequently, P = P6 + P7 + P8 = 2,684,483,063,360. 5-7 EXAMPLE 16 unkS� Bit Number o 234 Class A 0 netid Class B 1 0 netid Class C I 1 0 Class D 1 1 1 0 Class E 1 1 1 1 o I FIGURE 1 Internet Addresses (IPv4). 8 16 I hostid I netid Multicast Address Address 5.1 The Basics of Counting 341 24 31 hostid I hostid Counting Internet Addresses In the Internet, which is made up of interconnected physical networks of computers, each computer (or more precisely, each network connection of a com­ puter) is assigned an Internet address. In Version 4 of the Internet Protocol (IPv4), now in use, an address is a string of32 bits. It begins with a network number (netid). The netid is followed by a host number (hostid), which identifies a computer as a member of a particular network. Three forms of addresses are used, with different numbers of bits used for netids and hostids. Class A addresses, used for the largest networks, consist of 0, followed by a 7 -bit netid and a 24-bit hostid. Class B addresses, used for medium-sized networks, consist of 10, followed by a 14-bit netid and a 16-bit hostid. Class C addresses, used for the smallest networks, consist of 110, followed by a 21-bit netid and an 8-bit hostid. There are several restrictions on addresses because of special uses: 1111111 is not available as the netid of a Class A network, and the hostids consisting of all Os and all I s are not available for use in any network. A computer on the Internet has either a Class A, a Class B, or a Class C address. (Besides Class A, B, and C addresses, there are also Class D addresses, reserved for use in multicasting when multiple computers are addressed at a single time, consisting of 1110 followed by 28 bits, and Class E addresses, reserved for future use, consisting of 11110 followed by 27 bits. Neither Class D nor Class E addresses are assigned as the IP address of a computer on the Internet.) Figure 1 illustrates IPv4 addressing. (Limitations on the number of Class A and Class B netids have made IPv4 addressing inadequate; IPv6, a new version ofIp, uses 128-bit addresses to solve this problem.) How many different IPv4 addresses are available for computers on the Internet? Solution: Let x be the number of available addresses for computers on the Internet, and let x A, X B, and Xc denote the number of Class A, Class B, and Class C addresses available, respective ly. By the sum rule, x = XA + XB + xc. To find XA, note that there are 27 - I = 127 Class A netids, recalling that the netid 1111111 is unavailable. For each netid, there are 224 - 2 = 16,777,214 hostids, recalling that the hostids consisting of all Os and all Is are unavailable. Consequently, XA = 127 · 16,777,214 = 2,130,706,178. To find XB and Xc, note that there are 214 = 16,384 Class B netids and 221 = 2,097,152 Class C netids. For each Class B netid, there are 216 - 2 = 65,534 hostids, and for each Class C netid, there are 28 - 2 = 254 hostids, recalling that in each network the hostids consisting of all Os and allIs are unavailable. Consequently, XB = 1,073,709,056 and Xc = 532,676,608. We conclude that the total number of IPv4 addresses available is x = XA + XB + Xc = 2,130,706,178 + 1,073,709,056 + 532,676,608 = 3,737,091 ,842. � The Inclusion-Exclusion Principle Suppose that a task can be done in n 1 or in n2 ways, but that some of the set of n 1 ways to do the task are the same as some of the n2 other ways to do the task. In this situation, we cannot use the sum rule to count the number of ways to do the task. Adding the number of ways to do 342 5 / Counting 5-8 1 EXAMPLE 17 Ex1ra� Exampleslliiiiliil the tasks in these two ways leads to an overcount, because the ways to do the task in the ways that are common are counted twice. To correctly count the number of ways to do the two tasks, we add the number of ways to do it in one way and the number of ways to do it in the other way, and then subtract the number of ways to do the task in a way that is both among the set of n I ways and the set of n2 ways. This technique is called the principle of inclusion-exclusion. Sometimes, it is also called the subtraction principle for counting. Example 17 illustrates how we can solve counting problems using this principle. How many bit strings of length eight either start with a 1 bit or end with the two bits OO? Solution: We can construct a bit string of length eight that either starts with a 1 bit or ends with the two bits 00, by constructing a bit string of length eight beginning with a 1 bit or by constructing a bit string of length eight that ends with the two bits 00. We can construct a bit string oflength eight that begins with a 1 in 27 = 128 ways. This follows by the product rule, because the first bit can be chosen in only one way and each of the other seven bits can be chosen in two ways. Similarly, we can construct a bit string of length eight ending with the two bits 00, in 26 = 64 ways. This follows by the product rule, because each of the first six bits can be chosen in two ways and the last two bits can be chosen in only one way. --------Some of the ways to construct a bit string of length eight starting with a 1 are the same as the ways to construct a bit string of length eight that ends with the two bits 00. There are 25 = 32 ways to construct such a string. This follows by the product rule, because the first bit can be chosen in only one way, each of the second through the sixth bits can be chosen in two ways, and the last two bits can be chosen in one way. Consequently, the number of bit strings of length eight that begin with a 1 or end with a 00, which equals the number of ways to construct a bit string of length eight that begin with a 1 or that ends with 00, equals 128 + 64 -32 = l6Q � 27= 128 ways o 0 -------- 25= 32 ways We can phrase this counting principle in terms of sets. Let A I and A2 be sets. There are IA II ways to select an element from AI and IA21 ways to select an element from A2. The number of ways to select an element from AI or from A2, that is, the number of ways to select an element from their union, is the sum of the number of ways to select an element from A 1 and the number of ways to select an element from A2, minus the number of ways to select an element that is in both AI and A2. Because there are IAI U A21 ways to select an element in either AI or in A2, and IAI n A21 ways to select an element common to both sets, we have IAI U A21 = IAII + IA21 -IAI n A21. This is the formula given in Section 2.2 for the number of elements in the union of two sets. We present an example that illustrates how the formulation of the principle of inclusion­ exclusion can be used to solve counting problems. EXAMPLE 18 A computer company receives 350 applications from computer graduates for a job planning a line of new Web servers. Suppose that 220 of these people majored in computer science, 147 majored in business, and 51 majored both in computer science and in business. How many of these applicants majored neither in computer science nor in business? Solution: To find the number of these applicants who majored neither in computer science nor in business, we can subtract the number of students who majored either in computer science or in business (or both) from the total number of applicants. Let A I be the set of students who majored in computer science and A2 the set of students who majored in business. Then A I U A2 is the set of students who majored in computer science or business (or both), and AI n A2 is the set of students who majored both in computer science and in business. By the principle of 5-9 1st bit 2nd bit 4th bit o 1 0 1 001 0 :=:0808:=:08 ::::=::=:58888 FIGURE 2 Bit Strings of Length Four without Consecutive Is. EXAM PLE 19 5.1 The Basics of Counting 343 inclusion-exclusion, the number of students who majored either in computer science or in business (or both) equals IAI U A21 = IAII + IA21 -IAI n A21 = 220 + 147 -51 = 316. We conclude that 350 -316 = 34 of the applicants majored neither in computer science nor in business. .... The principle of inclusion-exclusion can be generalized to find the number of ways to do one of n different tasks or, equivalently, to find the number of elements in the union of n sets, whenever n is a positive integer. We will study the inclusion-exclusion principle and some of its many applications in Chapter 7. Tree Diagrams Counting problems can be solved using tree diagrams. A tree consists of a root, a number of branches leaving the root, and possible additional branches leaving the endpoints of other branches. (We will study trees in detail in Chapter 10.) To use trees in counting, we use a branch to represent each possible choi ce. We represent the possible outcomes by the leaves, which are the endpoints of branches not having other branches starting at them. Note that when a tree diagram is used to solve a counting problem, the number of choices required to reach a leaf can vary (see Example 20, for example). How many bit strings of length four do not have two consecutive 1 s? Solution: The tree diagram in Figure 2 displays all bit strings of length four without two con­ secutive 1 s. We see that there are eight bit strings of length four without two consecutive 1 s ..... EXAMPL E 20 A playoffbetween two teams consists of at most five games. The first team that wins three games wins the playoff. In how many different ways can the playoff occur? Winning team shown in color ..., " 0> S IV ..., ..., " " 0> 0> S S IV ..., ..., ..., ..., " " " " 0> 0> 0> 0> S S s a -IV IV FIGURE 3 Best Three Games Out of Five Playoffs. Gamel Game 4 Game 5 ..., ..., ..., ..., ..., " " " " " 0> 0> 0> 0> 0> s s s s S IV IV 344 5 / Counting 5-10 w = white, R = red, G = green, B = black WRG BWRG BW RG BR GB GB FIGURE 4 Counting Varieties of T-Shirts. Solution: The tree diagram in Figure 3 displays all the ways the playoff can proceed, with the winner of each game shown. We see that there are 20 different ways for the playoff to occur ..... EXAMPLE 21 Suppose that "I Love New Jersey" T-shirts come in five different sizes: S, M, L, XL, and XXL. Further suppose that each size comes in four colors, white, red, green, and black, except for XL, which comes only in red, green, and black, and XXL, which comes only in green and black. How many different shirts does a souvenir shop have to stock to have at least one of each available size and color of the T-shirt? Solution: The tree diagram in Figure 4 displays all possible size and color pairs. It follows that the souvenir shop owner needs to stock 17 different T-shirts. .... Exercises 1. There are 18 mathematics majors and 325 computer sci­ ence majors at a college. a) How many ways are there to pick two representatives so that one is a mathematics major and the other is a computer science major? b) How many ways are there to pick one representative who is either a mathematics major or a computer sci­ ence major? 2. An office building contains 27 floors and has 37 offices on each floor. How many offices are in the building? 3. A multiple-choice test contains 10 questions. There are four possible answers for each question. a) How many ways can a student answer the questions on the test if the student answers every question? b) How many ways can a student answer the questions on the test if the student can leave answers blank? 4. A particular brand of shirt comes in 12 colors, has a male version and a female version, and comes in three sizes for each sex. How many different types of this shirt are made? 5. Six different airlines fly from New York to Denver and seven fly from Denver to San Francisco. How many dif­ ferent pairs of airlines can you choose on which to book a trip from New York to San Francisco via Denver, when you pick an airline for the flight to Denver and an airline for the continuation flight to San Francisco? How many of these pairs involve more than one airline? 6. There are four major autoroutes from Boston to Detroit and six from Detroit to Los Angeles. How many major auto routes are there from Boston to Los Angeles via Detroit? 7. How many different three-letter initials can people have? 8. How many different three-letter initials with none of the letters repeated can people have? 9. How many different three-letter initials are there that be­ gin with an A? 10. How many bit strings are there of length eight? 11. How many bit strings of length ten both begin and end with a I? 12. How many bit strings are there oflength six or less? 13. How many bit strings with length not exceeding n, where n is a positive integer, consist entirely of 1 s? 14. How many bit strings of length n, where n is a positive integer, start and end with Is? 15. How many strings are there oflowercase letters oflength four or less? 16. How many strings are there of four lowercase letters that have the letter x in them? 17. How many strings of five ASCII characters contain the character @ ("at" sign) at least once? (Note: There are 128 different ASCII characters.) 18. How many positive integers between 5 and 31 a) are divisible by 3? Which integers are these? b) are divisible by 4? Which integers are these? c) are divisible by 3 and by 4? Which integers are these? 5-11 19. How many positive integers between 50 and 100 a) are divisible by 7? Which integers are these? b) are divisible by II? Which integers are these? c) are divisible by both 7 and 11? Which integers are these? 20. How many positive integers less than 1000 a) are divisible by 7? b) are divisible by 7 but not by II? c) are divisible by both 7 and II? d) are divisible by either 7 or II? e) are divisible by exactly one of7 and II? t) are divisible by neither 7 nor II? g) have distinct digits? h) have distinct digits and are even? 21. How many positive integers between 100 and 999 inclusive a) are divisible by 7? b) are odd? c) have the same three decimal digits? d) are not divisible by 4? e) are divisible by 3 or 4? t) are not divisible by either 3 or 4? g) are divisible by 3 but not by 4? h) are divisible by 3 and 4? 22. How many positive integers between 1000 and 9999 inclusive a) are divisible by 9? b) are even? c) have distinct digits? d) are not divisible by 3? e) are divisible by 5 or 7? t) are not divisible by either 5 or 7? g) are divisible by 5 but not by 7? h) are divisible by 5 and 7? 23. How many strings of three decimal digits a) do not contain the same digit three times? b) begin with an odd digit? c) have exactly two digits that are 4s? 24. How many strings of four decimal digits a) do not contain the same digit twice? b) end with an even digit? c) have exactly three digits that are 9s? 25. A committee is formed consisting of one representative from each ofthe 50 states in the United States, where the representative from a state is either the governor or one of the two senators from that state. How many ways are there to form this committee? 26. How many license plates can be made using either three digits followed by three letters or three letters followed by three digits? 27. How many license plates can be made using either two letters followed by four digits or two digits followed by four letters? 5.1 The Basics of Counting 345 28. How many license plates can be made using either three letters followed by three digits or four letters followed by two digits? 29. How many license plates can be made using either two or three letters followed by either two or three digits? 30. How many strings of eight English letters are there a) ifletters can be repeated? b) ifno letter can be repeated? c) that start with X, ifletters can be repeated? d) that start with X, if no letter can be repeated? e) that start and end with X, ifletters can be repeated? t) that start with the letters BO (in that order), ifletters can be repeated? g) that start and end with the letters BO (in that order), if letters can be repeated? h) that start or end with the letters BO (in that order), if letters can be repeated? 31. How many strings of eight English letters are there a) that contain no vowels, ifletters can be repeated? b) that contain no vowels, ifletters cannot be repeated? c) that start with a vowel, ifletters can be repeated? d) that start with a vowel, if letters cannot be repeated? e) that contain at least one vowel, if letters can be re­ peated? t) that contain exactly one vowel, if letters can be re­ peated? g) that start with X and contain at least one vowel, if letters can be repeated? h) that start and end with X and contain at least one vowel, if letters can be repeated? 32. How many different functions are there from a set with 10 elements to sets with the following numbers of elements? a) 2 b) 3 c) 4 d) 5 33. How many one-to-one functions are there from a set with five elements to sets with the following number of elements? a) 4 b) 5 c) 6 d) 7 34. How many functions are there from the set {I, 2, ... , n}, where n is a positive integer, to the set {O, I}? 35. How many functions are there from the set {I, 2, ... , n}, where n is a positive integer, to the set {O, l} a) that are one-to-one? b) that assign 0 to both I and n? c) that assign I to exactly one of the positive integers less than n? 36. How many partial functions (see the preamble to Exercise 73 in Section 2.3) are there from a set with five elements to sets with each of these number of elements? a) I b) 2 c) 5 d) 9 37. How many partial functions (see the preamble to Exercise 73 in Section 2.3) are there from a set with m elements to a set with n elements, where m and n are positive integers? 38. How many subsets of a set with 100 elements have more than one element? 346 5 / Counting 39. A palindrome is a string whose reversal is identical to the string. How many bit strings oflength n are palindromes? 40. In how many ways can a photographer at a wedding ar­ range 6 people in a row from a group of 10 people, where the bride and the groom are among these 10 people, if a) the bride must be in the picture? b) both the bride and groom must be in the picture? c) exactly one of the bride and the groom is in the picture? 41. In how many ways can a photographer at a wedding ar­ range six people in a row, including the bride and groom, if a) the bride must be next to the groom? b) the bride is not next to the groom? c) the bride is positioned somewhere to the left of the groom? 42. How many bit strings of length seven either begin with two Os or end with three 1 s? 43. How many bit strings oflength 10 either begin with three Os or end with two Os? *44. How many bit strings oflength 10 contain either five con­ secutive Os or five consecutive 1 s? 1<*45. How many bit strings oflength eight contain either three consecutive Os or four consecutive Is? 46. Every student in a discrete mathematics class is either a computer science or a mathematics major or is a joint major in these two subjects. How many students are in the class if there are 38 computer science majors (includ­ ingjoint majors), 23 mathematics majors (including joint majors), and 7 joint majors? 47. How many positive integers not exceeding 100 are divis­ ible either by 4 or by 6? 48. How many different initials can someone have if a person has at least two, but no more than five, different initials. Assume that each initial is one of the 26 letters of the English language. 49. Suppose that a password for a computer system must have at least 8, but no more than 12, characters, where each character in the password is a lowercase English letter, an uppercase English letter, a digit, or one of the six special characters *, >, <, !, +, and =. a) How many different passwords are available for this computer system? b) How many ofthese passwords contain at least one oc­ currence of at least one of the six special characters? c) If it takes one nanosecond for a hacker to check whether each possible password is your password, how long would it take this hacker to try every possible password? 50. The name of a variable in the C programming language is a string that can contain uppercase letters, lowercase let­ ters, digits, or underscores. Further, the first character in the string must be a letter, either uppercase or lowercase, or an underscore. If the name of a variable is determined by its first eight characters, how many different variables 5-12 can be named in C? (Note that the name of a variable may contain fewer than eight characters.) 51. Suppose that at some future time every telephone in the world is assigned a number that contains a coun­ try code 1 to 3 digits long, that is, of the form X, XX, or XXX, followed by a 10-digit telephone num­ ber of the form NXX-NXX-XXXX (as described in Example 8). How many different telephone numbers would be available worldwide under this numbering plan? 52. Use a tree diagram to find the number of bit strings of length four with no three consecutive Os. 53. How many ways are there to arrange the letters a, b, c, and d such that a is not followed immediately by b? 54. Use a tree diagram to find the number of ways that the World Series can occur, where the first team that wins four games out of seven wins the series. 55. Use a tree diagram to determine the number of subsets of {3, 7, 9, 11, 24} with the property that the sum of the elements in the subset is less than 28. 56. a) Suppose that a store sells six varieties of soft drinks: cola, ginger ale, orange, root beer, lemonade, and cream soda. Use a tree diagram to determine the num­ ber of different types of bottles the store must stock to have all varieties available in all size bottles if all vari­ eties are available in 12-ounce bottles, all but lemon­ ade are available in 20-ounce bottles, only cola and ginger ale are available in 32-ounce bottles, and all but lemonade and cream soda are available in 64-ounce bottles? b) Answer the question in part (a) using counting rules. 57. a) Suppose that a popular style of running shoe is avail­ able for both men and women. The woman's shoe comes in sizes 6, 7, 8, and 9, and the man's shoe comes in sizes 8, 9, 10, 11, and 12. The man's shoe comes in white and black, while the woman's shoe comes in white, red, and black. Use a tree diagram to determine the number of different shoes that a store has to stock to have at least one pair of this type of running shoe for all available sizes and colors for both men and women. b) Answer the question in part (a) using counting rules. *58. Use the product rule to show that there are 22" different truth tables for propositions in n variables. 59. Use mathematical induction to prove the sum rule for m tasks from the sum rule for two tasks. 60. Use mathematical induction to prove the product rule for m tasks from the product rule for two tasks. 61. How many diagonals does a convex polygon with n sides have? (Recall that a polygon is convex if every line seg­ ment connecting two points in the interior or boundary of the polygon lies entirely within this set and that a diagonal of a polygon is a line segment connecting two vertices that are not adjacent.) 62. Data are transmitted over the Internet in datagrams, which are structured blocks of bits. Each datagram 5-13 contains header information organized into a maximum of 14 different fields (specifying many things, including the source and destination addresses) and a data area that contains the actual data that are transmitted. One of the 14 header fields is the header length field (denoted by HLEN), which is specified by the protocol to be 4 bits long and that specifies the header length in terms of32-bit blocks of bits. For example, if HLEN = 0110, the header is made up of six 32-bit blocks. Another of the 14 header fields is the 16-bit-long total length field (denoted by TOTAL LENGTH), which specifies the length in bits of the entire datagram, including both the header fields and the data area. The length ofthe data area is the total length of the datagram minus the length of the header. 5.2 The Pigeonhole Principle Introduction 5.2 The Pigeonhole Principle 347 a) The largest possible value of TOTAL LENGTH (which is 16 bits long) determines the maximum total length in octets (blocks of 8 bits) of an Internet datagram. What. is this value? b) The largest possible value ofHLEN (which is 4 bits long) determines the maximum total header length in 32-bit blocks. What is this value? What is the maximum total header length in octets? c) The minimum (and most common) header length is 20 octets. What is the maximum total length in octets of the data area of an Internet datagram? d) How many different strings of octets in the data area can be transmitted if the header length is 20 octets and the total length is as long as possible? unkS� Suppose that a flock of 20 pigeons flies into a set of 19 pigeonholes to roost. Because there are 20 pigeons but only 19 pigeonholes, a least one of these 19 pigeonholes must have at least two pigeons in it. To see why this is true, note that if each pigeonhole had at most one pigeon in it, at most 19 pigeons, one per hole, could be accommodated. This illustrates a general principle called the pigeonhole principle, which states that if there are more pigeons than pigeonholes, then there must be at least one pigeonhole with at least two pigeons in it (see Figure 1). Of course, this principle applies to other objects besides pigeons and pigeonh oles. THEOREM l � �� � � � � � � (a) THE PIGEONHOLE PRINCIPLE If k is a positive integer and k + 1 or more objects are placed into k boxes, then there is at least one box containing two or more of the objects. Proof: We will prove the pigeonhole principle using a proof by contraposition. Suppose that none of the k boxes contains more than one object. Then the total number of objects would be at most k. This is a contradiction, because there are at least k + 1 objects. <l � � � � �� �� � �� � � �� � � �� � � � �� � , � � � � � (b) (c) FIGUR E 1 There Are More Pigeons Than Pigeonholes. 348 5 / Counting COROL LARY l 5-14 The pigeonhole principle is also called the Dirichlet drawer principle, after the nineteenth­ century German mathematician Dirichlet, who often used this principle in his work. The pigeonhole principle can be used to prove a useful corollary about functions. A function I from a set with k + 1 or more elements to a set with k elements is not one-to-one. Proof: Suppose that for each element y in the codomain of I we have a box that contains all elements x of the domain of I such that I(x) = y. Because the domain contains k + 1 or more elements and the codomain contains only k elements, the pigeonhole principle tells us that one of these boxes contains two or more elements x of the domain. This means that I cannot be one-to-one. <l Examples 1-3 show how the pigeonhole principle is used. EXAMPLE 1 Among any group of 367 people, there must be at least two with the same birthday, because there are only 366 possible birthdays. "III EXAMPLE 2 In any group of 27 English words, there must be at least two that begin with the same letter, because there are 26 letters in the English alphabet. "III EXAMPLE 3 How many students must be in a class to guarantee that at least two students receive the same score on the final exam, if the exam is graded on a scale from 0 to 100 points? EXAMPLE 4 Exam=� Solution: There are 101 possible scores on the final. The pigeonhole principle shows that among any 102 students there must be at least 2 students with the same score. "III The pigeonhole principle is a useful tool in many proofs, including proofs of surprising results, such as that given in Example 4. Show that for every integer n there is a multiple of n that has only Os and I s in its decimal expansion. Solution: Let n be a positive integer. Consider the n + 1 integers 1, 11, 111, ... , 11 ... 1 (where the last integer in this list is the integer with n + 1 Is in its decimal expansion). Note that there are n possible remainders when an integer is divided by n. Because there are n + 1 integers in this list, by the pigeonhole principle there must be two with the same remainder when divided by n. The larger of these integers less the smaller one is a multiple of n, which has a decimal expansion consisting entirely of Os and 1 s. "III G. LEJEUNE DIRICHLET (1805-1859) G. Lejeune Dirichlet was born into a French family living near Cologne, Germany. He studied at the University of Paris and held positions at the University of Breslau and the University of Berlin. In 1855 he was chosen to succeed Gauss at the University of Gottingen. Dirichlet is said to be the first person to master Gauss's Disquisitiones Arithmeticae, which appeared 20 years earlier. He is said to have kept a copy at his side even when he traveled. Dirichlet made many important discoveries in number theory, including the theorem that there are infinitely many primes in arithmetical progressions an + b when a and b are relatively prime. He proved the n = 5 case of Fermat's Last Theorem, that there are no nontrivial solutions in integers to x5 + y5 = z5. Dirichlet also made many contributions to analysis. 5-15 THEOREM 2 The Generalized Pigeonhole Principle 5.2 The Pigeonhole Principle 349 The pigeonhole principle states that there must be at least two objects in the same box when there are more objects than boxes. However, even more can be said when the number of objects exceeds a multiple of the number of boxes. For instance, among any set of 21 decimal digits there must be 3 that are the same. This follows because when 21 objects are distributed into 10 boxes, one box must have more than 2 objects. THE GENERA LIZED PIGEONHOLE PRINCIPLE If N objects are placed into k boxes, then there is at least one box containing at least r N / k 1 objects. Proof: We will use a proofby contradiction. Suppose that none ofthe boxes contains more than r N / k 1 -1 objects. Then, the total number of objects is at most where the inequality r N / k 1 < (N / k) + 1 has been used. This is a contradiction because there are a total of N objects. <l A common type of problem asks for the minimum number of objects such that at least r of these objects must be in one of k boxes when these objects are distributed among the boxes. When we have N objects, the generalized pigeonhole principle tells us there must be at least r objects in one of the boxes as long as rN / k1 ::: r. The smallest integer N with N / k > r -1, namely, N = k(r -1) + 1, is the smallest integer satisfying the inequality r N / k 1 ::: r. Could a smaller value of N suffice? The answer is no, because if we have k(r -1) objects, we could put r - 1 of them in each of the k boxes and no box would have at least r objects. When thinking about problems of this type, it is useful to consider how you can avoid having at least r objects in one ofthe boxes as you add successive objects. To avoid adding a rth object to any box, you eventually end up with r - 1 objects in each box. There is no way to add the next object without putting an rth object in that box. Examples 5-8 illustrate how the generalized pigeonhole principle is applied. EXAMPLE 5 Among 100 people there are at least r100/121 = 9 who were born in the same month. ..... EXAM PLE 6 Extra" Exa1lP18S� What is the minimum number of students required in a discrete mathemati cs class to be sure that at least six will receive the same grade, if there are five possible grades, A, B, C, D, and F? Solution: The minimum number of students needed to ensure that at least six students receive the same grade is the smallest integer N such that rN /51 = 6. The smallest such integer is N = 5 . 5 + I = 26. If you have only 25 students, it is possible for there to be five who have received each grade so that no six students have received the same grade. Thus, 26 is the minimum number of students needed to ensure that at least six students will receive the same grade. ..... EXAMPLE 7 a) How many cards must be selected from a standard deck of 52 cards to guarantee that at least three cards of the same suit are chosen? b) How many must be selected to guarantee that at least three hearts are selected? 350 5 / Counting 5-/6 Solution: a) Suppose there are four boxes, one for each suit, and as cards are selected they are placed in the box reserved for cards of that suit. Using the generalized pigeonhole principle, we see that if N cards are selected, there is at least one box containing at least r N 141 cards. Consequently, we know that at least three cards of one suit are selected if r N 141 :::: 3. The smallest integer N such that rN 141 :::: 3 is N = 2 ·4+ I = 9, so nine cards suffice. Note that ifeight cards are selected, it is possible to have two cards of each suit, so more than eight cards are needed. Consequently, nine cards must be selected to guarantee that at least three cards of one suit are chosen. One good way to think about this is to note that after the eighth card is chosen, there is no way to avoid having a third card of some suit. b) We do not use the generalized pigeonhole principle to answer this question, because we want to make sure that there are three hearts, not just three cards of one suit. Note that in the worst case, we can select all the clubs, diamonds, and spades, 39 cards in all, before we select a single heart. The next three cards will be all hearts, so we may need to select 42 cards to get three hearts. ... EXAMPLE 8 What is the least number of area codes needed to guarantee that the 25 million phones in a state can be assigned distinct 1O-digit telephone numbers? (Assume that telephone numbers are of the form NXX-NXX- XXXX, where the first three digits form the area code, N represents a digit from 2 to 9 inclusive, and X represents any digit.) Solution: There are eight million different phone numbers of the form N X X -X X X X (as shown in Example 8 of Section 5.l). Hence, by the generalized pigeonhole principle, among 25 mil­ lion telephones, at least r25,000,000/8,000,0001 of them must have identical phone numbers. Hence, at least four area codes are required to ensure that all 10-digit numbers are different. ... Example 9, although not an application of the generalized pigeonhole principle, makes use of similar principles. EXAMPLE 9 Suppose that a computer science laboratory has 15 workstations and 10 servers. A cable can be used to directly connect a workstation to a server. For each server, only one direct connection to that server can be active at any time. We want to guarantee that at any time any set of 10 or fewer workstations can simultaneously access different servers via direct connections. Although we could do this by connecting every workstation directly to every server (using 150 connections), what is the minimum number of direct connections needed to achieve this goal? Solution: Suppose that we label the workstations WI, W2, ••• , WI5 and the servers SI, S2, ... , SIO. Furthermore, suppose that we connect Wk to Sk for k = 1,2, ... ,10 and each of WII, W12, W13, W14, and WI5 to all 10 servers. We have a total of 60 direct connections. Clearly any set of 10 or fewer workstations can simultaneously access different servers. We see this by noting that if workstation W; is included with 1 :::: j :::: 10, it can access server Sj' and for each workstation Wk with k :::: 11 included, there must be a corresponding worksta­ tion W; with 1 :::: j :::: 10 not included, so Wk can access server Sj. (This follows because there are at least as many available servers Sj as there are workstations W; with 1 :::: j :::: 10 not included.) Now suppose there are fewer than 60 direct connections between workstations and servers. Then some server would be connected to at most L59/10J = 5 workstations. (lfall servers were connected to at least six workstfltl O,ns, there would be at least 6 . 10 = 60 direct connectio ns.) This means that the remaining nine servers are not enough to allow the other 10 workstations to 5-17 5.2 The Pigeonhole Principle 351 simultaneously access different servers. Consequently, at least 60 direct connections are needed. It follows that 60 is the answer. .... Some Elegant Applications of the Pigeonhole Principle In many interesting applications of the pigeonhole principle, the objects to be placed in boxes must be chosen in a clever way. A few such applications will be described here. EXAM PLE 10 During a month with 30 days, a baseball team plays at least one game a day, but no more than 45 games. Show that there must be a period of some number of consecutive days during which the team must play exactly 14 games. Solution: Let aj be the number of games played on or before the }th day of the month. Then a I, a2, ... , a30 is an increasing sequence of distinct positive integers, with 1 ::::: aj ::::: 45. More­ over, al + 14, a2 + 14, ... , a30 + 14 is also an increasing sequence of distinct positive integers, with 15 ::::: aj + 14 ::::: 59. The 60 positive integers ai, a2, ... , a30, al + 14, a2 + 14, ... , a30 + 14 are all less than or equal to 59. Hence, by the pigeonhole principle two of these integers are equal. Because the integers aj, } = 1, 2, ... , 30 are all distinct and the integers aj + 14, } = 1, 2, ... , 30 are all distinct, there must be indices i and} with ai = aj + 14. This means that exactly 14 games were played from day } + 1 to day i. .... EXAMPLE 11 Show that among any n + 1 positive integers not exceeding 2n there must be an integer that divides one of the other integers. THEOREM 3 Solution: Write each of the n + 1 integers ai, a2, ... , an+1 as a power of2 times an odd integer. In other words, let aj = 2kjqj for} = 1,2, ... , n + 1, where kj is a nonnegative integer and qj is odd. The integers ql, q2, ... ,qn+1 are all odd positive integers less than 2n. Because there are only n odd positive integers less than 2n, it follows from the pigeonhole principle that two of the integers ql, q2, ... , qn+1 must be equal. Therefore, there are integers i and} such that qi = qj. Let q be the common value of qi and qj. Then, ai = 2k; q and aj = 2kj q. It follows that if ki < '9, then ai divides aj; while if ki > '9, then aj divides ai· .... A clever application of the pigeonhole principle shows the existence of an increasing or a decreasing subsequence of a certain length in a sequence of distinct integers. Some definitions will be reviewed before this application is presented. Suppose that ai, a2, ... , aN is a sequence of real numbers. A subsequence of this sequence is a sequence of the form ai I ' ai2' ... , aim' where 1 ::::: i I < i2 < ... < im ::::: N. Hence, a subsequence is a sequence obtained from the original sequence by including some of the terms of the original sequence in their original order, and perhaps not including other terms. A sequence is called strictly increasing if each term is larger than the one that precedes it, and it is called strictly decreasing if each term is smaller than the one that precedes it. Every sequence of n'l-+ 1 distinct real numbers contains a subsequence of length n + 1 that is either strictly increasing or strictly decreasing. We give an example before presenting the proof of Theorem 3. 352 5 / Counting 5-18 EXAMPLE 12 The sequence 8, 11,9, 1,4,6, 12, 10,5, 7 contains 10 terms. Note that 10 = 32 + 1. There are four increasing subsequences oflength four, namely, 1,4,6, 12; 1,4,6, 7; 1,4,6, 10; and 1,4, 5, 7. There is also a decreasing subsequence oflength four, namely, 11,9,6, 5. � UDksiC EXAMPLE 13 lInksiC The proof of the theorem will now be given. Proof: Leta!, a2, ... , an2+! be a sequence ofn2 + 1 distinct real numbers. Associate an ordered pair with each term of the sequence, namely, associate Uk. dk) to the term ak. where ik is the length of the longest increasing subsequence starting at ak, and dk is the length of the longest decreasing subsequence starting at ak. Suppose that there are no increasing or decreasing subsequences of length n + 1. Then h and dk are both positive integers less than or equal to n, for k = 1,2, ... , n2 + 1. Hence, by the product rule there are n2 possible ordered pairs for (ik. dk). By the pigeonhole principle, two of these n2 + 1 ordered pairs are equal. In other words, there exist terms as and at, with s < t such that is = it and ds = dt. We will show that this is impossible. Because the terms of the sequence are distinct, either as < at or as > at. If as < at, then, because is = it, an increasing sub­ sequence of length it + 1 can be built starting at as, by taking as followed by an increasing subsequence oflength it beginning at at. This is a contradiction. Similarly, if as > at, it can be shown that ds must be greater than dt, which is a contradiction. <I The final example shows how the generalized pigeonhole principle can be applied to an important part of combinatorics called Ramsey theory, after the English mathematician F. P. Ramsey. In general, Ramsey theory deals with the distribution of subsets of elements of sets. Assume that in a group of six people, each pair of individuals consists of two friends or two enemies. Show that there are either three mutual friends or three mutual enemies in the group. Solution: Let A be one of the six people. Of the five other people in the group, there are either three or more who are friends of A, or three or more who are enemies of A. This follows from the generalized pigeonhole principle, because when five objects are divided into two sets, one of the sets has at least f5j21 = 3 elements. In the former case, suppose that B, C, and Dare friends of A. If any two of these three individuals are friends, then these two and A form a group of three mutual friends. Otherwise, B, C, and D form a set of three mutual enemies. The proof in the latter case, when there are three or more enemies of A, proceeds in a similar manner. � The Ramsey number R(m, n), where m and n are positive integers greater than or equal to 2, denotes the minimum number of people at a party such that there are either m mutual friends or n mutual enemies, assuming that every pair of people at the party are friends or enemies. Example 13 shows that R(3, 3) ::s 6. We conclude that R(3, 3) = 6 because in a group of five people where every two people are friends or enemies, there may not be three mutual friends or three mutual enemies (see Exercise 24). FRANK PLUMPTON RAMSEY (1903-1930) Frank P1urnpton Ramsey, son of the president of Magda1ene College, Cambridge, was educated at Winchester and Trinity Colleges. After graduating in 1923, he was elected a fellow of King's College, Cambridge, where he spent the remainder of his life. Ramsey made important contributions to mathematical logic. What we now call Ramsey theory began with his clever combinatorial arguments, published in the paper "On a Problem of Formal Logic." Ramsey also made contributions to the mathematical theory of economics. He was noted as an excellent lecturer on the foundations of mathemati cs. His death at the age of26 deprived the mathematical community and Cambridge University of a brilliant young �,*",'Oii""""�","ili"""'" scholar. 5-19 5.2 The Pigeonhole Principle 353 It is possible to prove some useful properties about Ramsey numbers, but for the most part it is difficult to find their exact values. Note that by symmetry it can be shown that R(m, n} = R(n, m)(see Exercise 28}. We also have R(2, n} = n for every positive integern ::: 2 (see Exercise 27). The exact values of only nine Ramsey numbers R(m, n} with 3 S m Snare known, including R(4, 4} = 18. Only bounds are known for many other Ramsey numbers, in­ cluding R(5, 5}, which is known to satisfy 43 S R(5, 5} S 49. The reader interested in learning more about Ramsey numbers should consult [MiR091] or [GrRoSp90]. Exercises 1. Show that in any set of six classes, each meeting regularly once a week on a particular day of the week, there must be two that meet on the same day, assuming that no classes are held on weekends. 2. Show that if there are 30 students in a class, then at least two have last names that begin with the same letter. 3. A drawer contains a dozen brown socks and a dozen black socks, all unmatched. A man takes socks out at random in the dark. a) How many socks must he take out to be sure that he has at least two socks of the same color? b) How many socks must he take out to be sure that he has at least two black socks? 4. A bowl contains 10 red balls and 10 blue balls. A woman selects balls at random without looking at them. a) How many balls must she select to be sure of having at least three balls of the same color? b) How many balls must she select to be sure of having at least three blue balls? 5. Show that among any group of five (not necessar­ ily consecutive) integers, there are two with the same remainder when divided by 4. 6. Let d be a positive integer. Show that among any group of d + 1 (not necessarily consecutive) integers there are two with exactly the same remainder when they are divided byd. 7. Let n be a positive integer. Show that in any set of n consecutive integers there is exactly one divisible byn. 8. Show that if f is a function from S to T, where Sand T are finite sets with lSI> ITI, then there are elements Sl and S2 in S such that f(sd = f(sz), or in other words, f is not one-to-one. 9. What is the minimum number of students, each of whom comes from one of the 50 states, who must be enrolled in a university to guarantee that there are at least 100 who come from the same state? *10. Let(xi,Yi),i = 1,2,3,4,5, be a set offive distinct points with integer coordinates in the xy plane. Show that the midpoint ofthe line joining at least one pair ofthese points has integer coordinates. *11. Let(xi,Yi,zi),i = 1,2,3,4,5,6, 7,8,9,beasetofnine distinct points with integer coordinates in xyz space. Show that the midpoint of at least one pair of these points has integer coordinates. 12. How many ordered pairs of integers (a, b) are needed to guarantee that there are two ordered pairs (ai, bl) and (az, bz) such that al mod 5 = az mod 5 and bl mod 5 = bz mod 5? 13. a) Show that if five integers are selected from the first eight positive integers, there must be a pair of these integers with a sum equal to 9. b) Is the conclusion in part (a) true if four integers are selected rather than five? 14. a) Show that if seven integers are selected from the first 10 positive integers, there must be at least two pairs ofthese integers with the sum 11. b) Is the conclusion in part (a) true if six integers are selected rather than seven? 15. How many numbers must be selected from the set {I, 2, 3, 4, 5, 6} to guarantee that at least one pair of these numbers add up to 7? 16. How many numbers must be selected from the set {I, 3, 5, 7, 9, 11, 13, I5} to guarantee that at least one pair ofthese numbers add up to 16? 17. A company stores products in a warehouse. Storage bins in this warehouse are specified by their aisle, location in the aisle, and shelf. There are 50 aisles, 85 horizontal locations in each aisle, and 5 shelves throughout the ware­ house. What is the least number of products the company can have so that at least two products must be stored in the same bin? 18. Suppose that there are nine students in a discrete mathe­ matics class at a small college. a) Show that the class must have at least five male stu­ dents or at least five female students. b) Show that the class must have at least three male stu­ dents or at least seven female students. 19. Suppose that every student in a discrete mathematics class of 25 students is a freshman, a sophomore, or a junior. a) Show that there are at least nine freshmen, at least nine sophomores, or at least nine juniors in the class. 354 5 / Counting b) Show that there are either at least three freshmen, at least 19 sophomores, or at least five juniors in the class. 20. Find an increasing subsequence of maximal length and a decreasing subsequence of maximal length in the se­ quence 22,5,7,2,23, 10, 15,21,3, 17. 21. Construct a sequence of 16 positive integers that has no increasing or decreasing subsequence of five terms. 22. Show that if there are 10 I people of different heights standing in a line, it is possible to find II people in the order they are standing in the line with heights that are either increasing or decreasing. *23. Describe an algorithm in pseudocode for producing the largest increasing or decreasing subsequence of a se­ quence of distinct integers. 24. Show that in a group of five people (where any two peo­ ple are either friends or enemies), there are not necessarily three mutual friends or three mutual enemies. 25. Show that in a group of 10 people (where any two people are either friends or enemies), there are ei­ ther three mutual friends or four mutual enemies, and there are either three mutual enemies or four mutual friends. 26. Use Exercise 25 to show that among any group of 20 people (where any two people are either friends or ene­ mies), there are either four mutual friends or four mutual enemies. 27. Show that if n is a positive integer with n ::: 2, then the Ramsey number R(2, n) equals n. 28. Show that if m and n are positive integers with m ::: 2 and n ::: 2, then the Ramsey numbers R(m, n) and R(n, m) are equal. 29. Show that there are at least six people in California (pop­ ulation: 36 million) with the same three initials who were born on the same day of the year (but not necessar­ ily in the same year). Assume that everyone has three initials. 30. Show that if there are 100,000,000 wage earners in the United States who earn less than 1,000,000 dollars, then there are two who earned exactly the same amount of money, to the penny, last year. 31. There are 38 different time periods during which classes at a university can be scheduled. If there are 677 different classes, how many different rooms will be needed? 32. A computer network consists of six computers. Each com­ puter is directly connected to at least one of the other com­ puters. Show that there are at least two computers in the network that are directly connected to the same number of other computers. 33. A computer network consists of six computers. Each com­ puter is directly connected to zero or more of the other computers. Show that there are at least two computers in the network that are directly connected to the same num­ ber of other computers. [Hint: It is impossible to have 5-20 a computer linked to none of the others and a computer linked to all the others.] 34. Find the least number of cables required to connect eight computers to four printers to guarantee that four comput­ ers can directly access four different printers. Justify your answer. 35. Find the least number of cables required to connect 100 computers to 20 printers to guarantee that 20 computers can directly access 20 different printers. (Here, the as­ sumptions about cables and computers are the same as in Example 9.) Justify your answer. *36. Prove that at a party where there are at least two people, there are two people who know the same number of other people there. 37. An arm wrestler is the champion for a period of75 hours. (Here, by an hour, we mean a period starting from an exact hour, such as 1 P.M., until the next hour.) The arm wrestler had at least one match an hour, but no more than 125 total matches. Show that there is a period of con­ secutive hours during which the arm wrestler had exactly 24 matches. *38. Is the statement in Exercise 37 true if 24 is replaced by a) 2? b) 23? c) 25? d) 30? 39. Show that if f is a function from S to T, where Sand T are finite sets and m = n S 1 / 1 T 11, then there are at least m elements of S mapped to the same value of T. That is, show that there are distinct elements s" S2, ... , Sm of S such that f(sd = f(S2) = ... = f(sm). 40. There are 51 houses on a street. Each house has an address between 1000 and 1099, inclusive. Show that at least two houses have addresses that are consecutive integers. *41. Let x be an irrational number. Show that for some posi­ tive integer j not exceeding n, the absolute value of the difference between j x and the nearest integer to j x is less than lin. 42. Let n" n2, ... , nl be positive integers. Show that if n, + n2 + ... + nl - t + 1 objects are placed into t boxes, then for some i, i = 1,2, ... , t, the ith box con­ tains at least ni objects. *43. A proof of Theorem 3 based on the generalized pigeon­ hole principle is outlined in this exercise. The notation used is the same as that used in the proof in the text. a) Assume that h ::::: n for k = 1,2, ... , n2 + l. Use the generalized pigeonhole principle to show that there are n + 1 terms ak1 ' ak2, ... , akn+1 with ik1 = ik2 = ... = ikn+1, where I ::::: k, < k2 < ... < kn+,. b) Show that akj > akj+1 for j = 1,2, ... , n. [Hint: As­ sume that akj < akj+1, and show that this implies that ikj > ikj+1, which is a contradiction.] c) Use parts (a) and (b) to show that if there is no increasing subsequence of length n + I, then there must be a decreasing subsequence of this length. 5-21 5.3 Pennutations and Combinations 355 53 Permutations and Comb inations EXAMPLE 1 Extra� Exampleslliiiiiiil unks� EXAMPLE 2 Introduction Many counting problems can be solved by finding the number of ways to arrange a specified number of distinct elements of a set of a particular size, where the order of these elements matters. Many other counting problems can be solved by finding the number of ways to select a particular number of elements from a set of a particular size, where the order of the elements selected does not matter. For example, in how many ways can we select three students from a group of five students to stand in line for a picture? How many different committees of three students can be formed from a group of four students? In this section we will develop methods to answers questions such as these. Permutations We begin by solving the first question posed in the introduction to this section, as well as related questions. In how many ways can we select three students from a group of five students to stand in line for a picture? In how many ways can we arrange all five of these students in a line for a picture? Solution: First, note that the order in which we select the students matters. There are five ways to select the first student to stand at the start of the line. Once this student has been selected, there are four ways to select the second student in the line. After the first and second students have been selected, there are three ways to select the third student in the line. By the product rule, there are 5 . 4 . 3 = 60 ways to select three students from a group of five students to stand in line for a picture. To arrange all five students in a line for a picture, we select the first student in five ways, the second in four ways, the third in three ways, the fourth in two ways, and the fifth in one way. Consequently, there are 5 ·4·3 ·2· 1 = 120 ways to arrange all five students in a line for a picture. � Example 1 illustrates how ordered arrangements of distinct objects can be counted. This leads to some terminology. A permutation of a set of distinct objects is an ordered arrangement of these objects. We also are interested in ordered arrangements of some of the elements of a set. An ordered arrangement of r elements of a set is called an r-permutation. Let S = {t, 2, 3}. The ordered arrangement 3, 1,2 is a permutation of S. The ordered arrange­ ment 3, 2 is a 2-permutation of S. � The number of r-permutations of a set with n elements is denoted by Pen, r). We can find Pen, r) using the product rule. EXAM PLE 3 Let S = {a, b, c}. The 2-permutations of S are the ordered arrangements a, b; a, c; b, a; b, c; c, a; and c, b. Consequently, there are six 2-permutations of this set with three elements. To see that there are always six 2-permutations of a set with three elements, note that there are three ways to choose the first element of the arrangement and two ways to choose the second element of the arrangement because it must be different from the first element. By the product rule, it follows that P(3, 2) = 3 ·2 = 6. � 356 5 / Counting THEOREM l COROL LARY l 5-22 We now use the product rule to find a formula for P (n, r) whenever n and r are positive integers with 1 ::: r ::: n. If n is a positive integer and r is an integer with 1 ::: r ::: n, then there are pen, r) = n(n -l)(n -2)··· (n -r + 1) r-permutations of a set with n distinct elements. Proof: We will use the product rule to prove that this formula is correct. The first element of the permutation can be chosen in n ways because there are n elements in the set. There are n -1 ways to choose the second element of the permutation, because there are n -1 elements left in the set after using the element picked for the first position. Similarly, there are n -2 ways to choose the third element, and so on, until there are exactly n -(r -I) = n - r + 1 ways to choose the rth element. Consequently, by the product rule, there are n(n -1)(n -2)···(n -r + 1) r-permutations of the set. <l Note that P (n, 0) = 1 whenever n is a nonnegative integer because there is exactly one way to order zero elements. That is, there is exactly one list with no elements in it, namely the empty list. We now state a useful corollary of Theorem 1. Ifn and r are integers with 0 ::: r ::: n, then pen, r) = (n �! r)! Proof: When nand r are integers with 1 ::: r ::: n, by Theorem 1 we have n! pen, r) = n(n -1)(n -2)··· (n -r + 1) = -----,-(n -r)! n! n' Because ( ) = .....:. = 1 whenever n is a nonnegative integer, we see that the formula n -O! n! n! pen, r) = also holds when r = o. <l (n -r)! By Theorem 1 we know that ifn is a positive integer, then pen, n) = n!. We will illustrate this result with some examples. EXAMPLE 4 How many ways are there to select a first-prize winner, a second-prize winner, and a third-prize winner from 100 different people who have entered a contest? Solution: Because it matters which person wins which prize, the number of ways to pick the three prize winners is the number of ordered selections of three elements from a set of 100 elements, that is, the number of 3-permutations of a set of 100 elements. Consequently, the answer is P{lOO, 3) = 100·99·98 = 970,200. EXAMPLE 5 Suppose that there are eight runners in a race. The winner receives a gold medal, the second­ place finisher receives a silver medal, and the third-place finisher receives a bronze medal. How 5-23 5.3 Pennutations and Combinations 357 many different ways are there to award these medals, if all possible outcomes of the race can occur and there are no ties? Solution: The number of different ways to award the medals is the number of 3-permutations of a set with eight elements. Hence, there are P(8, 3) = 8 . 7 . 6 = 336 possible ways to award the medals. .... EXAMPLE 6 Suppose that a saleswoman has to visit eight different cities. She must begin her trip in a specified city, but she can visit the other seven cities in any order she wishes. How many possible orders can the saleswoman use when visiting these cities? Solution: The number of possible paths between the cities is the number of permutations of seven elements, because the first city is determined, but the remaining seven can be ordered arbitrarily. Consequently, there are 7! = 7·6·5·4·3·2· 1 = 5040 ways for the saleswoman to choose her tour. If, for instance, the saleswoman wishes to find the path between the cities with minimum distance, and she computes the total distance for each possible path, she must consider a total of 5040 paths! .... EXAM PLE 7 How many permutations of the letters ABCDEFGH contain the string ABC? Solution: Because the letters ABC must occur as a block, we can find the answer by finding the number of permutations of six objects, namely, the block ABC and the individual letters D, E, F, G, and H. Because these six objects can occur in any order, there are 6! = 720 permutations of the letters ABCDEFGH in which ABC occurs as a block. .... Combinations We now turn our attention to counting unordered selection of objects. We begin by solving a question posed in the introduction to this section of the chapter. EXAMPLE 8 How many different committees ofthree students can be formed from a group off our students? unkS� Solution: To answer this question, we need only find the number of subsets with three elements from the set containing the four students. We see that there are four such subsets, one for each of the four students, because choosing four students is the same as choosing one of the four students to leave out of the group. This means that there are four ways to choose the three students for the committee, where the order in which these students are chosen does not matter. .... Example 8 illustrates that many counting problems can be solved by finding the number of subsets of a particular size of a set with n elements, where n is a positive integer. An r-combination of elements of a set is an unordered selection of r elements from the set. Thus, an r-combination is simply a subset of the set with r elements. EXAMPLE 9 Let S be the set {l, 2, 3, 4}. Then {l, 3, 4} is a 3-combination from S. The number ofr-combinations ofa set with n distinct elements is denoted by C(n, r). Note that C(n, r) is also denoted by (;) and is called a binomial coefficient. We will learn where this terminology arises in Section 5.4. EXAMPLE 10 We see that C(4, 2) = 6, because the 2-combinations of {a, b, e, d} are the six subsets {a, b}, {a, e}, {a, d}, {b, e}, {b, d}, and {e, d}. .... 358 5 / Counting THEOREM 2 5-24 We can detennine the number of r-combinations of a set with n elements using the fonnula for the number of r-pennutations of a set. To do this, note that the r-pennutations of a set can be obtained by first fonning r-combinations and then ordering the elements in these combinations. The proof of Theorem 2, which gives the value ofC(n, r), is based on this observation. The number of r-combinations of a set with n elements, where n is a nonnegative integer and r is an integer with 0 :s r :s n, equals n! C(n, r) = '( _ ) ' r. n r. Proof: The r-pennutations of the set can be obtained by fonning the C(n, r) r-combinations of the set, and then ordering the elements in each r-combination, which can be done in P(r, r) ways. Consequently, P(n, r) = C(n, r) . P(r, r). This implies that P(n, r) n!/(n -r)! n! C(n,r) = = = ---P(r, r) r!/(r -r)! r! (n -r)! <1 The fonnula in Theorem 2, although explicit, is not helpful when C(n, r) is computed for large values of n and r. The reasons are that it is practical to compute exact values of factorials exactly only for small integer values, and when floating point arithmetic is used, the fonnula in Theorem 2 may produce a value that is not an integer. When computing C(n, r), first note that when we cancel out (n -r)! from the numerator and denominator ofthe expression for C(n, r) in Theorem 2, we obtain n! C(n, r) = '( _ )' r. n r. n(n ,-1)·· ·(n -r + 1) r! Consequently, to compute C(n, r) you can cancel out all the tenns in the larger factorial in the denominator from the numerator and denominator, then multiply all the tenns that do not cancel in the numerator and finally divide by the smaller factorial in the denominator. [When doing this calculation by hand, instead of by machine, it is worthwhile to factor out common factors in the numerator n(n -1)· .. (n - r + 1) and in the denominator r!.] Note that many calculators have a built-in function for C (n, r) that can be used for relatively small values of nand r and many computational programs can be used to find C(n, r). [Such functions may be called chose(n, k) or binom(n, k)]. Example 11 illustrates how C(n, k) is computed when k is relatively small compared to n and when k is close to n. It also illustrates a key identity enjoyed by the numbers C(n, k). EXAMPLE 11 How many poker hands of five cards can be dealt from a standard deck of 52 cards? Also, how many ways are there to select 47 cards from a standard. d.e�k of 52 cards? Solution: Because the order in which the five cards ar� dealt from a deck of 52 cards does not matter, there are 52! C(52, 5) = 5!47! 5-25 COROLL ARY 2 DEFINITION 1 5.3 Permutations and Combinations 359 different hands of five cards that can be dealt. To compute the value ofC(52, 5), first divide the numerator and denominator by 47! to obtain 52 . 51 . 50 . 49 . 48 C(52, 5) = . 5·4·3·2·1 This expression can be simplified by first dividing the factor 5 in the denominator into the factor 50 in the numerator to obtain a factor lOin the numerator, then dividing the factor 4 in the denominator into the factor 48 in the numerator to obtain a factor of 12 in the numerator, then dividing the factor 3 in the denominator into the factor 51 in the numerator to obtain a factor of 17 in the numerator, and finally, dividing the factor 2 in the denominator into the factor 52 in the numerator to obtain a factor of 26 in the numerator. We find that C(52, 5) = 26 . 17· 10 . 49· 12 = 2,598,960. Consequently, there are 2,598,960 different poker hands of five cards that can be dealt from a standard deck of 52 cards. Note that there are 52! C(52,47) = -47!5! different ways to select 47 cards from a standard deck of 52 cards. We do not need to compute this value because C(52, 47) = C(52, 5). (Only the order of the factors 5! and 47! is different in the denominators in the formulae for these quantities.) It follows that there are also 2,598,960 different ways to select 47 cards from a standard deck of 52 cards. .... In Example 11 we observed that C(52, 5) = C(52, 47). This is a special case of the useful identity for the number of r-combinations of a set given in Corollary 2. Let nand r be nonnegative integers with r ::::: n. Then C(n, r) = C(n, n -r). Proof: From Theorem 2 it follows that and n! C(n,r)= --­r!(n-r)! n! n! C(n,n -r) = = --- (n-r)![n-(n-r)]! (n-r)!r! Hence, C(n, r) = C(n, n -r). <l We can also prove Corollary 2 using a proof that shows that both sides of the equation in Corollary 2 count the same objects using different reasoning. We describe this important type of proof in Definition 1. A combinatorial proof of an identity is a proof that uses counting arguments to prove that both sides of the identity count the same objects but in different ways. 360 5 / Counting EXAM PLE 12 Exam=� 5-26 Many identities involving binomial coefficients can be proved using combinatorial proofs. We now provide a combinatorial proof of Corollary 2. Proof: Suppose that S is a set with n elements. Every subset A of S with r elements corresponds to a subset of S with n - r elements, namely A. Consequently, C(n, r) = C(n, n -r). <l How many ways are there to select five players from a 10-member tennis team to make a trip to a match at another school? Solution: The answer is given by the number of 5-combinations of a set with 10 elements. By Theorem 2, the number of such combinations is 1O! C(lO, 5) = 5! 5! = 252. EXAMPLE 13 A group of 30 people have been trained as astronauts to go on the first mission to Mars. How many ways are there to select a crew of six people to go on this mission (assuming that all crew members have the same job)? Solution: The number of ways to select a crew of six from the pool of 30 people is the number of 6-combinations of a set with 30 elements, because the order in which these people are chosen does not matter. By Theorem 2, the number of such combinations is 30! 30 . 29 . 28 . 27 . 26 . 25 C(30, 6) = 6! 24! = 6.5 .4.3 .2. 1 = 593,775. EXAMPLE 14 How many bit strings oflength n contain exactly r Is? Solution: The positions of r Is in a bit string of length n form an r-combination of the set {I, 2, 3, ... , n}. Hence, there are C(n, r) bit strings oflength n that contain exactly r Is. <III EXAMPLE 15 Suppose that there are 9 faculty members in the mathema tics department and 11 in the computer science department. How many ways are there to select a committee to develop a discrete mathematics course at a school if the committee is to consist of three faculty members from the mathematics department and four from the computer science department? Solution: By the product rule, the answer is the product of the number of 3-combinations of a set with nine elements and the number of 4-combinations of a set with 11 elements. By Theorem 2, the number of ways to select the committee is 9! II! C(9,3)·C(lI,4)= 3!6!' 4!7! =84·330=27,720. Exercises 1. List all the permutations of {a, b, e}. 2. How many different permutations are there of the set {a, b, e, d, e, f, g}? 3. How many permutations of {a, b, e, d, e, t, g} end with a? 4. Let S = {I, 2, 3, 4, 5}. a) List all the 3-permutations of S. b) List all the 3-combinations of S. 5. Find the value of each of these quantities. a) P(6, 3) b) P(6,5) 5-27 c) P(8, 1) e) P(8,8) d) P(8,5) t) P(IO,9) 6. Find the value of each of these quantities. a) C(5, 1) b) C(5,3) c) C(8, 4) d) C(8,8) e) C(8,0) t) C(l2,6) 7. Find the number of 5-permutations of a set with nine elements. 8. In how many different orders can five runners finish a race if no ties are allowed? 9. How many possibilities are there for the win, place, and show (first, second, and third) positions in a horse race with 12 horses if all orders of finish are possible? 10. There are six different candidates for governor of a state. In how many different orders can the names ofthe candi­ dates be printed on a ballot? 11. How many bit strings oflength 10 contain a) exactly four Is? b) at most four Is? c) at least four Is? d) an equal number of Os and 1 s? 12. How many bit strings oflength 12 contain a) exactly three Is? b) at most three 1 s? c) at least three Is? d) an equal number of Os and Is? 13. A group contains n men and n women. How many ways are there to arrange these people in a row if the men and women alternate? 14. In how many ways can a set of two positive integers less than 100 be chosen? 15. In how many ways can a set of five letters be selected from the English alphabet? 16. How many subsets with an odd number of elements does a set with 10 elements have? 17. How many subsets with more than two elements does a set with 100 elements have? 18. A coin is flipped eight times where each flip comes up either heads or tails. How many possible outcomes a) are there in total? b) contain exactly three heads? c) contain at least three heads? d) contain the same number of heads and tails? 19. A coin is flipped 10 times where each flip comes up either heads or tails. How many possible outcomes a) are there in total? b) contain exactly two heads? c) contain at most three tails? d) contain the same number of heads and tails? 20. How many bit strings oflength 10 have a) exactly three Os? b) more Os than Is? 5.3 Permutations and Combinations 361 c) at least seven Is? d) at least three Is? 21. How many permutations of the letters ABCDEFG contain a) the string BCD? b) the string CFGA? c) the strings BA and GF? d) the strings ABC and DE? e) the strings ABC and CDE? t) the strings CBA and BED? 22. How many permutations of the letters ABCDEFGH contain a) the string ED? b) the string CDE? c) the strings BA and FGH? d) the strings AB, DE, and GH? e) the strings CAB and BED? t) the strings BCA and ABF? 23. How many ways are there for eight men and five women to stand in a line so that no two women stand next to each other? [Hint: First position the men and then consider possible positions for the women.] 24. How many ways are there for 10 women and six men to stand in a line so that no two men stand next to each other? [Hint: First position the women and then consider possible positions for the men.] 25. One hundred tickets, numbered I, 2, 3, ... ,100, are sold to 100 different people for a drawing. Four different prizes are awarded, including a grand prize (a trip to Tahiti). How many ways are there to award the prizes if a) there are no restrictions? b) the person holding ticket 47 wins the grand prize? c) the person holding ticket 47 wins one of the prizes? d) the person holding ticket 47 does not win a prize? e) the people holding tickets 19 and 47 both win prizes? t) the people holding tickets 19, 47, and 73 all win prizes? g) the people holding tickets 19,47, 73, and 97 all win prizes? h) none of the people holding tickets 19,47, 73, and 97 wins a prize? i) the grand prize winner is a person holding ticket 19, 47,73, or 917 j) the people holding tickets 19 and 47 win prizes, but the people holding tickets 73 and 97 do not win prizes? 26. Thirteen people on a softball team show up for a game. a) How many ways are there to choose 10 players to take the field? b) How many ways are there to assign the 10 posi­ tions by selecting players from the 13 people who show up? 362 5 / Counting c) Of the 13 people who show up, three are women. How many ways are there to choose 10 players to take the field if at least one of these players must be a woman? 27. A club has 25 members. a) How many ways are there to choose four members of the club to serve on an executive committee? b) How many ways are there to choose a president, vice president, secretary, and treasurer of the club, where no person can hold more than one office? 28. A professor writes 40 discrete mathematics true/false questions. Of the statements in these questions, 17 are true. Ifthe questions can be positioned in any order, how many different answer keys are possible? *29. How many 4-permutations of the positive integers not exceeding 100 contain three consecutive integers k, k + 1, k + 2, in the correct order a) where these consecutive integers can perhaps be sep­ arated by other integers in the permutation? b) where they are in consecutive positions in the permu­ tation? 30. Seven women and nine men are on the faculty in the math­ ematics department at a school. a) How many ways are there to select a committee of five members of the department if at least one woman must be on the committee? b) How many ways are there to select a committee of five members of the department if at least one woman and at least one man must be on the committee? 31. The English alphabet contains 21 consonants and five vowels. How many strings of six lowercase letters of the English alphabet contain a) exactly one vowel? b) exactly two vowels? c) at least one vowel? d) at least two vowels? 32. How many strings of six lowercase letters from the En­ glish alphabet contain a) the letter a? b) the letters a and b? c) the letters a and b in consecutive positions with a pre­ ceding b, with all the letters distinct? d) the letters a and b, where a is somewhere to the left of b in the string, with all the letters distinct? 33. Suppose that a department contains 10 men and 15 women. How many ways are there to form a commit­ tee with six members if it must have the same number of men and women? 34. Suppose that a department contains 10 men and 15 women. How many ways are there to form a commit­ tee with six members if it must have more women than men? 35. How many bit strings contain exactly eight Os and lOis if every 0 must be immediately followed by a I? 5-28 36. How many bit strings contain exactly five Os and 14 Is if every 0 must be immediately followed by two Is? 37. How many bit strings of length 10 contain at least three Is and at least three Os? 38. How many ways are there to select 12 countries in the United Nations to serve on a council if 3 are se­ lected from a block of 45, 4 are selected from a block of 57, and the others are selected from the remaining 69 countries? 39. How many license plates consisting of three letters followed by three digits contain no letter or digit twice? 40. How many ways are there to seat six people around a cir­ cular table, where seatings are considered to be the same if they can be obtained from each other by rotating the table? 41. How many ways are there for a horse race with three horses to finish if ties are possible? (Note: Two or three horses may tie.) *42. How many ways are there for a horse race with four horses to finish if ties are possible? (Note: Any number of the four horses may tie.) *43. There are six runners in the 100-yard dash. How many ways are there for three medals to be awarded if ties are possible? (The runner or runners who finish with the fastest time receive gold medals, the runner or runners who finish with exactly one runner ahead re­ ceive silver medals, and the runner or runners who finish with exactly two runners ahead receive bronze medals.) *44. This procedure is used to break ties in games in the championship round of the World Cup soccer tourna­ ment. Each team selects five players in a prescribed or­ der. Each of these players takes a penalty kick, with a player from the first team followed by a player from the second team and so on, following the order of players specified. If the score is still tied at the end of the 10 penalty kicks, this procedure is repeated. If the score is still tied after 20 penalty kicks, a sudden-death shootout occurs, with the first team scoring an unanswered goal victorious. a) How many different scoring scenarios are possible if the game is settled in the first round of 10 penalty kicks, where the round ends once it is impossible for a team to equal the number of goals scored by the other team? b) How many different scoring scenarios for the first and second groups of penalty kicks are possible if the game is settled in the second round of 10 penalty kicks? c) How many scoring scenarios are possible for the full set of penalty kicks if the game is settled with no more than 10 total additional kicks after the two rounds of five kicks for each team? 5-29 5.4 Binomial Coefficients 363 5.4 Binomial Coefficients links it::I EXAMPLE 1 THEOREM 1 As we remarked in Section 5.3, the number of r-combinations from a set with n elements is often denoted by C). This number is also called a binomial coefficient because these numbers occur as coefficients in the expansion of powers of binomial expressions such as (a + b t. We will discuss the Binomial Theorem, which gives a power of a binomial expression as a sum of terms involving binomial coefficients. We will prove this theorem using a combinatorial proof. We will also show how combinatorial proofs can be used to establish some of the many different identities that express relationships among binomial coefficients . The Binomial Theorem The Binomial Theorem gives the coefficients of the expansion of powers of binomial expres­ sions. A binomial expression is simply the sum oftwo terms, such as x + y. (The terms can be products of constants and variables, but that does not concern us here.) Example I illustrates why this theorem holds. The expansion of (x + y)3 can be found using combinatorial reasoning instead of multiplying the three terms out. When (x + y)3 = (x + y)(x + y)(x + y) is expanded, all products ofa term in the first sum, a term in the second sum, and a term in the third sum are added. Terms of the form x3, x2y, xy2, and y3 arise. To obtain a term of the form x3, an x must be chosen in each of the sums, and this can be done in only one way. Thus, the x3 term in the product has a coefficient of l. To obtain a term of the formx2y, an x must be chosen in two of the three sums (and consequently a y in the other sum). Hence, the number of such terms is the number of 2-combinations of three objects, namely, @. Similarly, the number of terms of the form xy2 is the number of ways to pick one of the three sums to obtain an x (and consequently take a y from each ofthe other two sums). This can be done in (D ways. Finally, the only way to obtain a y3 term is to choose the y for each of the three sums in the product, and this can be done in exactly one way. Consequently, it follows that (x + y)3= (x + y)(x + y)(x + y) = (xx + xy + yx + yy)(x + y) =�+�+�+�+�+�+�+� = x3 + 3x2y + 3xy2 + y3. The Binomial Theorem will now be stated. THE BINOMIAL THEOREM Let x and y be variables, and let n be a nonnegative integer. Then Proof: A combinatorial proof of the theorem will be given. The terms in the product when it is expanded are of the form xn-iyi for j = 0,1,2, ... , n. To count the number of terms of the form xn-i yi , note that to obtain such a term it is necessary to choose n -j xs from the n sums (so that the other j terms in the product are ys). Therefore, the coefficient ofxn-iyi is C�), which is equal to (;). This proves the theorem. <l 364 5 / Counting EXAMPLE 2 Exlra� ExamPles� The use of the Binomial Theorem is illustrated by Examples 2-4. What is the expansion of (x + y)4? Solution: From the Binomial Theorem it follows that EXAMPLE 3 What is the coefficient Ofx12y13 in the expansion of (x + y)25? Solution: From the Binomial Theorem it follows that this coefficient is (25) 25! 13 = 13! 12! = 5,200,300. EXAMPLE 4 What is the coefficient of x 12 y 13 in the expansion of (2x -3 Y i5? 5-30 Solution: First, note that this expression equals (2x + ( -3 y) )25. By the Binomial Theorem, we have COROL LARY l (2x+(-3y))25 = t (2�)(2Xi5-j(-3y)i. j=O ) Consequently, the coefficient Ofx12y13 in the expansion is obtained when j = 13, namely, We can prove some useful identities using the Binomial Theorem, as Corollaries 1, 2, and 3 demonstrate. Let n be a nonnegative integer. Then t(n) =2n. k=O k Proof: Using the Binomial Theorem with x = 1 and y = 1, we see that This is the desired result. <I There is also a nice combinatorial proof of Corollary 1, which we now present. 5-31 COROLLAR Y 2 COROLL ARY 3 5.4 Binomial Coefficients 365 Proof: A set with n elements has a total of 2n different subsets. Each subset has zero elements, one element, two elements, ... , or n elements in it. There are (�) subsets with zero elements, (�) subsets with one element, (;) subsets with two elements, ... , and (:) subsets with n elements. Therefore, counts the total number of subsets of a set with n elements. This shows that t (n) = 2n. k=O k <l Let n be a positive integer. Then t(-li(�) = O. k=O Proof: When we use the Binomial Theorem with x = -1 and y = 1, we see that This proves the corollary. <l Remark: Corollary 2 implies that G) + G) + (:) + ... = G) + G) + G) + .... Let n be a nonnegative integer. Then Proof: We recognize that the left-hand side of this formula is the expansion of (1 + 2)n provided by the Binomial Theorem. Therefore, by the Binomial Theorem, we see that Hence <l 366 5 / Counting THEOREM 2 linkS� 5-32 Pascal's Identity and Triangle The binomial coefficients satisfy many different identities. We introduce one of the most im­ portant of these now. PASCAIJS IDENTITY Let nand k be positive integers with n ::: k. Then Proof: Suppose that T is a set containing n + 1 elements. Let a be an element in T, and let S = T -{a}. Note that there are (n1') subsets ofT containing k elements. However, a subset of T with k elements either contains a together with k -1 elements of S, or contains k elements of S and does not contain a. Because there are (k:') subsets of k -1 elements of S, there are (k:') subsets of k elements of T that contain a. And there are G) subsets of k elements of T that do not contain a, because there are (;) subsets of k elements of S. Consequently, <l Remark: A combinatorial proof of Pascal's Identity has been given. It is also possible to prove this identity by algebraic manipulation from the formula for C) (see Exercise 19 at the end of this section). Remark: Pascal's Identity, together with the initial conditions (�) = C) = 1 for all integers n, can be used to recursively define binomial coefficients. This recursive definition is useful in the computation of binomial coefficients because only addition, and not multiplication, of integers is needed to use this recursive definition. Pascal's Identity is the basis for a geometric arrangement of the binomial coefficients in a triangle, as shown in Figure 1. The nth row in the triangle consists of the binomial coefficients (�). k=O, l, ... ,n. This triangle is known as Pascal's triangle. Pascal's Identity shows that when two adjacent binomial coefficients in this triangle are added, the binomial coefficient in the next row between these two coefficients is produced. BLAISE PASCAL (1623-1662) Blaise Pascal exhibited his talents at an early age, although his father, who had made discoveries in analytic geometry, kept mathematics books away from him to encourage other interests. At 16 Pascal discovered an important result concerning conic sections. At 18 he designed a calculating machine, which he built and sold. Pascal, along with Fermat, laid the foundations for the modern theory of probabilit y. In this work he made new discoveries concerning what is now called Pascal's triangle. In 1654, Pascal abandoned his mathematical pursuits to devote himself to theology. After this, he returned to mathematics only once. One night, distracted by a severe toothache, he sought comfort by studying the mathematical properties of the cycloid. Miraculous ly, his pain subsided, which he took as a sign of divine approval of the study of mathema tics. 5-33 5.4 Binomial Coefficients 367 (n (n (D (n (n (D By Pascal's identity: 2 (n (D (D (D (D+(D=(D 3 3 (6) (i) (i) (j) (:) 4 6 4 (n (D (D (D (D (D 5 10 10 5 (�) (�) (�) (�) (�) (�) (�) 6 15 20 15 6 \/ \ / (b) (;) (�) (�) (�) (�) (�) (;) 7 21 35 35 21 7 (g) (�) (�) (�) (�) (�) (�) (�) (�) 8 28 56 70 56 28 8 (a) (b) FIGURE 1 Pascal's Triangle. THEOREM 3 Some Other Identities of the Binomial Coefficients We conclude this section with combinatorial proofs of two of the many identities enjoyed by the binomial coefficients. VANDERMONDE 'S IDENTITY Let m, n, and r be nonnegative integers with r not exceeding either m or n. Then Remark: This identity was discovered by mathematician Alexandre-Theophile Vandermonde in the eighteenth century. Proof: Suppose that there are m items in one set and n items in a second set. Then the total number of ways to pick r elements from the union of these sets is (m -:-n). Another way to pick r elements from the union is to pick k elements from the first set and then r -k elements from the second set, where k is an integer with 0 ::: k ::: r. This can be done in (�) � : J ways, using the product rule. Hence, the total number of ways to pick r elements from the union also equals This proves Vandermonde's Identity. <l Corollary 4 follows from Vandermonde's Identity. 368 5 / Counting COROL LARY 4 THEOREM 4 unkS� If n is a nonnegative integer, then Proof: We use Vandermonde's Identity with m = r = n to obtain (2n) = t ( n ) (n) = t (n)2 n k=O n -k k k=O k The last equality was obtained using the identity (�) = C � k)' 5-34 <I We can prove combinatorial identities by counting bit strings with different properties, as the proof of Theorem 4 will demonstrate. Let nand r be nonnegative integers with r ::::: n. Then (n + 1) = t (j). r + 1 . r J=r Proof: We use a combinatorial proof. By Example 14 in Section 5.3, the left-hand side, (;! D, counts the bit strings of length n + 1 containing r + 1 ones. We show that the right-hand side counts the same objects by considering the cases corre­ sponding to the possible locations of the final 1 in a string with r + 1 ones. This final one must occur at position r + 1, r + 2, ... , or n + 1. Furthermore, if the last one is the kth bit there must be r ones among the first k -1 positions. Consequently, by Example 14 in Section 5.3, there are (k � I) such bit strings. Summing over k with r + 1 ::::: k ::::: n + 1, we find that there are I: (k - 1) = t (j) k=r+1 r j=r r bit strings of length n containing exactly r + 1 ones. (Note that the last step follows from the change of variables j = k -1.) Because the left-hand side and the right-hand side count the same objects, they are equal. This completes the proof. <I ALEXANDRE- THEOPHILE VANDERMONDE (1735-1796) Because Alexandre- Theophile Vandermonde was a sickly child, his physician father directed him to a career in music. However, he later developed an interest in mathemati cs. His complete mathematical work consists of four papers published in 1771-1772. These papers include fundamental contributions on the roots of equations, on the theory of determinants, and on the knight's tour problem (introduced in the exercises in Section 9.5). Vandermonde's interest in mathematics lasted for only 2 years. Afterward, he published papers on harmony, experiments with cold, and the manufacture of steel. He also became interested in politics, joining the cause of the French revolution and holding several different positions in government. 5-35 Exercises 1. Find the expansion of (x + y)4 a) using combinatorial reasoning, as in Example 1. b) using the Binomial Theorem. 2. Find the expansion of (x + y)5 a) using combinatorial reasoning, as in Example 1. b) using the Binomial Theorem. 3. Find the expansion of (x + y)6. 4. Find the coefficient of x5 y8 in (x + y)13. 5. How many terms are there in the expansion of (x + y)IOO after like terms are collected? 6. What is the coefficient ofx 7 in (I + x) II? 7. What is the coefficient ofx9 in (2 -x)19? 8. What is the coefficient of x8 y9 in the expansion of (3x + 2y)17? 9. What is the coefficient of x 101 y99 in the expansion of (2x -3y)200? *10. Give a formula for the coefficient of xk in the expansion of (x + l/x)100, where k is an integer. *11. Give a formula for the coefficient of xk in the expansion of(x2 -1/x)100, where k is an integer. 12. The row of Pascal's triangle containing the binomial co­ efficients CkO), 0 ::s k ::s 10, is: 1 10 45 120 210 252 210 120 45 10 1 Use Pascal's Identity to produce the row immediately fol­ lowing this row in Pascal's triangle. 13. What is the row of Pascal's triangle containing the bino­ mial coefficients G), 0 ::s k ::s 9? 14. Show that if n is a positive integer, then 1 = (�) < G) < ... < (Ln/2J) = (rn/21) > ... > (n�l) > C) = 1. 15. Show that m ::s 2n for all positive integers n and all inte­ gers k with 0 ::s k ::s n. 16. a) Use Exercise 14 and Corollary 1 to show that if n is an integer greater than 1, then (n/2J) ::::: 2n / n. b) Conclude from part (a) that if n is a positive integer, then e:) ::::: 4n /2n. "17. Show that if nand k are integers with 1 ::s k ::s n, then (;) ::s nk /2k-l. 18. Suppose that b is an integer with b ::::: 7. Use the Binomial Theorem and the appropriate row of Pascal's triangle to find the base-b expansion of (ll)� [that is, the fourth power of the number (ll)b in base-b notation]. 19. Prove Pascal's Identity, using the formula for C). 20. Suppose that k and n are integers with 1 ::s k < n. Prove the hexagon identity (n -I) ( n ) (n + I) = (n -I) ( n ) (n + I) k-I k+1 k k k-I k+I' which relates terms in Pascal's triangle that form a hexagon. 5.4 Binomial Coefficients 369 r3r21. Prove that if nand k are integers with 1 ::s k ::s n, then kG) = nG:::D a) using a combinatorial proof. [Hint: Show that the two sides of the identity count the number of ways to select a subset with k elements from a set with n elements and then an element ofthis subset.] b) using an algebraic proof based on the formula for C) given in Theorem 2 in Section 5.3. 22. Prove the identity C)G) = G)(�:::Z), whenever n, r, and k are nonnegative integers with r ::s n and k ::s r, a) using a combinatorial argument. b) using an argument based on the formula for the num­ ber of r-combinations of a set with n elements. 23. Show that if n and k are positive integers, then Use this identity to construct an inductive definition of the binomial coefficients . 24. Show that if p is a prime and k is an integer such that 1 ::s k ::s p -1, then p divides (f). 25. Let n be a positive integer. Show that C�I) + e) = e:12)/2. *26. Let nand k be integers with 1 ::s k ::s n. Show that L:�=I G)(k�l) = e:12)/2 -e)· *27. Prove that whenever n and r are positive integers, a) using a combinatorial argument. b) using Pascal's identity. 28. Show that ifn is a positive integer, then en = 2(;) + n2 a) using a combinatorial argument. b) by algebraic manipulation. *29. Give a combinatorial proof that L:Z= I kG) = n2n-l. [Hint: Count in two ways the number of ways to select a committee and to then select a leader of the committee .] *30. Give a combinatorial proof that L:Z=I kG)2 = ne..=-/). [Hint: Count in two ways the number of ways to select a committee, with n members from a group of n mathemat­ ics professors and n computer science professors, such that the chairperson of the committee is a mathematics professor. ] 31. Show that a nonempty set has the same number of subsets with an odd number of elements as it does subsets with an even number of elements. *32. Prove the Binomial Theorem using mathematical induc­ tion. 33. In this exercise we will count the number of paths in the xy plane between the origin (0,0) and point (m, n) such 370 5 / Counting that each path is made up of a series of steps, where each step is a move one unit to the right or a move one unit upward. (No moves to the left or downward are allowed.) Two such paths from (0, 0) to (5, 3) are illustrated here. ,----,.----,---r----.---, (5, 3) (0, 0) '-----''------'-----'----'----' ,..-----,---r---r-----,---. (5, 3) (0, 0) '----'-----'-----'-----'-------' a) Show that each path ofthe type described can be rep­ resented by a bit string consisting of m Os and n 1 s, where a 0 represents a move one unit to the right and a 1 represents a move one unit upward. b) Conclude from part (a) that there are (m :n) paths of the desired type. 34. Use Exercise 33 to prove that G) = t�k) whenever k is an integer with 0 ::: k ::: n. [Hint: Consider the number of paths of the type described in Exercise 33 from (0, 0) to (n -k, k) and from (0, 0) to (k, n -k).] 35. Use Exercise 33 to prove Theorem 4. [Hint: Count the number of paths with n steps of the type described in Ex-5-36 ercise 33. Every such path must end at one of the points (n -k, k) for k = 0,1,2, ... , n.] 36. Use Exercise 33 to prove Pascal's Identity. [Hint: Show that a path ofthe type described in Exercise 33 from (0, 0) to(n + 1 -k, k) passes through either(n + 1 -k, k -1) or (n -k, k), but not through both.] 37. Prove the identity in Exercise 27 using Exercise 33. [Hint: First, note that the number of paths from (0, 0) to (n + 1, r) equals e + � + r). Second, count the num­ ber of paths by summing the number of these paths that start by going k units upward for k = 0, 1, 2, ... , r.] 38. Give a combinatorial proof that if n is a positive integer then L:Z=o k2G) = n(n + 1)2n-2. [Hint: Showthatboth sides count the ways to select a subset of a set of n ele­ ments together with two not necessarily distinct elements from this subset. Furthermore, express the right-hand side as n(n -1)2n-2 + n2n-l.] *39. Determine a formula involving binomial coefficients for the nth term of a sequence if its initial terms are those listed. [Hint: Looking at Pascal's triangle will be helpful. Although infinitely many sequences start with a specified set of terms, each of the following lists is the start of a sequence of the type desired.] a) 1,3,6, 10, 15,21,28,36,45, 55,66, .. . b) 1,4,10,20,35,56,84,120,165,220, .. . c) 1,2,6,20,70,252,924,3432,12870,48620, ... d) 1, 1,2,3,6, 10,20,35,70, 126, ... e) 1, 1, 1,3, 1,5, 15,35, 1,9, ... t) 1, 3, 15, 84, 495, 3003, 18564, 116280, 735471, 4686825, ... 5.5 Generalized Permutati ons and Combinations unkS� Introduction In many counting problems, elements may be used repeatedly. For instance, a letter or digit may be used more than once on a license plate. When a dozen donuts are selected, each variety can be chosen repeatedly. This contrasts with the counting problems discussed earlier in the chapter where we considered only permutations and combinations in which each item could be used at most once. In this section we will show how to solve counting problems where elements may be used more than once. Also, some counting problems involve indistinguishable elements. For instance, to count the number of ways the letters of the word SUCCESS can be rearranged, the placement of identical letters must be considered. This contrasts with the counting problems discussed earlier where all elements were considered distinguishable. In this section we will describe how to solve counting problems in which some elements are indistinguishable. Moreover, in this section we will explain how to solve another important class of counting problems, problems involving counting the ways to place distinguishable elements in boxes. An example of this type of problem is the number of different ways poker hands can be dealt to four players. 5-37 5.5 Generalized Permutations and Combinations 371 Taken together, the methods described earlier in this chapter and the methods introduced in this section form a useful toolbox for solving a wide range of counting problems. When the additional methods discussed in Chapter 7 are added to this arsenal, you will be able to solve a large percentage of the counting problems that arise in a wide range of areas of study. Permutations with Repetition Counting permutations when repetition of elements is allowed can easily be done using the product rule, as Example 1 shows. EXAMPLE 1 How many strings oflength r can be formed from the English alphabet? THEOREM l Solution: By the product rule, because there are 26 letters, and because each letter can be used repeatedly, we see that there are 26r strings of length r. .... The number of r-permutations of a set with n elements when repetition is allowed is given in Theorem 1. The number of r-permutations of a set of n objects with repetition allowed is nr. Proof: There are n ways to select an element of the set for each of the r positions in the r-permutation when repetition is allowed, because for each choice all n objects are available. Hence, by the product rule there are nr r-permutations when repetition is allowed. <l Combinations with Repetition Consider these examples of combinations with repetition of elements allowed. EXAMPLE 2 How many ways are there to select four pieces offruit from a bowl containing apples, oranges, and pears if the order in which the pieces are selected does not matter, only the type of fruit and not the individual piece matters, and there are at least four pieces of each type of fruit in the bowl? Solution: To solve this problem we list all the ways possible to select the fruit. There are 15 ways: 4 apples 3 apples, 1 orange 3 oranges, 1 pear 2 apples, 2 oranges 2 apples, 1 orange, 1 pear 4 oranges 3 apples, 1 pear 3 pears, 1 apple 2 apples, 2 pears 2 oranges, 1 apple, 1 pear 4 pears 3 oranges, 1 apple 3 pears, 1 orange 2 oranges, 2 pears 2 pears, 1 apple, 1 orange The solution is the number of 4-combinations with repetition allowed from a three-element set, {apple, orange, pear}. .... To solve more complex counting problems of this type, we need a general method for counting the r-combinations of an n-element set. In Example 3 we will illustrate such a method. 372 5 / Counting $100 $50 $20 $10 $5 $2 FIGURE 1 Cash Box with Seven Types of Bills. 5-38 $1 EXAMPLE 3 How many ways are there to select five bills from a cash box containing $1 bills, $2 bills, $5 bills, $10 bills, $20 bills, $50 bills, and $100 bills? Assume that the order in which the bills are chosen does not matter, that the bills of each denomination are indistinguishable, and that there are at least five bills of each type. Solution: Because the order in which the bills are selected does not matter and seven dif­ ferent types of bills can be selected as many as five times, this problem involves counting 5-combinations with repetition allowed from a set with seven elements. Listing all possibilities would be tedious, because there are a large number of solutions. Instead, we will illustrate the use of a technique for counting combinations with repetition allowed. Suppose that a cash box has seven compartments, one to hold each type of bill, as illustrated in Figure 1. These compartments are separated by six dividers, as shown in the picture. The choice of five bills corresponds to placing five markers in the compartments holding different types of bills. Figure 2 illustrates this correspondence for three different ways to select five bills, where the six dividers are represented by bars and the five bills by stars. The number of ways to select five bills corresponds to the number of ways to arrange six bars and five stars. Consequently, the number of ways to select the five bills is the number of ways to select the positions of the five stars, from 11 possible positions. This corresponds to the number of unordered selections of 5 objects from a set of 11 objects, which can be done in C (11, 5) ways. Consequently, there are I I 1** I I 1** * FIGURE 2 Examples of Ways to Select Five Bills. 5-39 THEOREM 2 EXAM PLE 4 Extra� Exampleslliiiiiiil II! C(ll, 5) = 5! 6! = 462 5.5 Generalized Permutations and Combinations 373 ways to choose five bills from the cash box with seven types of bills. Theorem 2 generalizes this discussion. There are C(n + r -1, r) = C(n + r -1, n -1) r-combinations from a set with n elements when repetition of elements is allowed. Proof: Each r-combination of a set with n elements when repetition is allowed can be rep­ resented by a list of n -1 bars and r stars. The n -1 bars are used to mark off n different cells, with the ith cell containing a star for each time the ith element of the set occurs in the combination. For instance, a 6-combination of a set with four elements is represented with three bars and six stars. Here ** I * II * * * represents the combination containing exactly two of the first element, one ofthe second element, none of the third element, and three of the fourth element of the set. As we have seen, each different list containing n -1 bars and r stars corresponds to an r-combination of the set with n elements, when repetition is allowed. The number of such lists is C(n -1 + r, r), because each list corresponds to a choice of the r positions to place the r stars from the n -1 + r positions that contain r stars and n -1 bars. The number of such lists is also equal to C(n -1 + r, n -1), because each list corresponds to a choice of the n -1 positions to place the n -1 bars. <l Examples 4-6 show how Theorem 2 is applied. Suppose that a cookie shop has four different kinds of cookies. How many different ways can six cookies be chosen? Assume that only the type of cookie, and not the individual cookies or the order in which they are chosen, matters. Solution: The number of ways to choose six cookies is the number of 6-combinations of a set with four elements. From Theorem 2 this equals C (4 + 6 -1, 6) = C (9, 6). Because 9·8·7 C(9, 6) = C(9, 3) = = 84, 1 ·2·3 there are 84 different ways to choose the six cookies. Theorem 2 can also be used to find the number of solutions of certain linear equations where the variables are integers subject to constraints. This is illustrated by Example 5. EXAM PLE 5 How many solutions does the equation have, where Xl, X2, and X3 are nonnegative integers? Solution: To count the number of solutions, we note that a solution corresponds to a way of selecting 11 items from a set with three elements so that Xl items of type one, X2 items of type 374 5/ Counting 5-40 two, and X3 items of type three are chosen. Hence, the number of solutions is equal to the number of II-combinations with repetition allowed from a set with three elements. From Theorem 2 it follows that there are C(3 + 11 -1, 11) = C(13, 11) = C(13, 2) = 13· 12 = 78 1·2 solutions. The number of solutions of this equation can also be found when the variables are subject to constraints. For instance, we can find the number of solutions where the variables are integers with XI ::: 1, X2 ::: 2, and X3 ::: 3. A solution to the equation subject to these constraints corresponds to a selection of 11 items with XI items of type one, X2 items of type two, and X3 items of type three, where, in addition, there is at least one item of type one, two items of type two, and three items of type three. So, choose one item of type one, two of type two, and three of type three. Then select five additional items. By Theorem 2 this can be done in 7·6 C(3 + 5 -1,5) = C(7, 5) = C(7, 2) = -= 21 1·2 ways. Thus, there are 21 solutions of the equation subject to the given constraints. Example 6 shows how counting the number of combinations with repetition allowed arises in determining the value of a variable that is incremented each time a certain type of nested loop is traversed. EXAMPLE 6 What is the value of k after the following pseudocode has been executed? k :=0 foril:=lton for i2 := 1 to i I for im := 1 to im-I k:= k+ 1 Solution: Note that the initial value of k is 0 and that 1 is added to k each time the nested loop is traversed with a sequence of integers ii, i2, ... , im such that The number of such sequences of integers is the number of ways to choose m integers from {l, 2, ... , n}, with repetition allowed. (To see this, note that once such a sequence has been selected, if we order the integers in the sequence in nondecreasing order, this uniquely defines an assignment of im, im-I, ... , i I. Conversely, every such assignment corresponds to a unique unordered set.) Hence, from Theorem 2, it follows that k = C(n + m - 1, m) after this code has been executed. ... The formulae for the numbers of ordered and unordered selections of r elements, chosen with and without repetition allowed from a set with n elements, are shown in Table 1. 5-41 5.5 Generalized Permutations and Combinations 375 TABLE 1 Combinations and Permutations with and without Repetition. Type Repetition Allowed? Formula r-permutations No n! --- (n -r)! r-combinations No n! r! (n -r)! r-permutations Yes n' r-combinations Yes (n + r -I)! r! (n -I)! Permutations with Indistinguishable Objects Some elements may be indistinguishable in counting problems. When this is the case, care must be taken to avoid counting things more than once. Consider Example 7. EXAMPLE 7 How many different strings can be made by reordering the letters of the word ExIra � SUCCESS? Examples IiiiiiI THEOREM 3 Solution: Because some of the letters of SUCCESS are the same, the answer is not given by the number of permutations of seven letters. This word contains three S s, two C s, one U, and one E. To determine the number of different strings that can be made by reordering the letters, first note that the three Ss can be placed among the seven positions in C(7, 3) different ways, leaving four positions free. Then the two C s can be placed in C (4, 2) ways, leaving two free positions. The U can be placed in C (2, I) ways, leaving just one position free. Hence E can be placed in C (l, 1) way. Consequently, from the product rule, the number of different strings that can be made is 7! 4! 2! 1! C(7, 3)C(4, 2)C(2, l)C(l, 1) = 3! 4! . 2! 2! . 1! 1! . 1! O! 7! 3!2! 1! 1! =420. We can prove Theorem 3 using the same sort of reasoning as in Example 7. The number of different permutations of n objects, where there are n 1 indistinguishable objects of type 1, n2 indistinguishable objects of type 2, ... , and nk indistinguishable objects of type k, is n! Proof: To determine the number of permutati ons, first note that the n] objects of type one can be placed among the n positions in C(n, n]) ways, leaving n -n] positions free. Then the objects of type two can be placed in C(n -n], n2) ways, leaving n -n] -n2 positions free. Continue 376 5/ Counting unks� Extra� Examples ...... 5-42 placing the objects of type three, ... , type k -I, until at the last stage, nk objects of type k can be placed in C(n -nJ -n2 -... -nk-J, nk) ways. Hence, by the product rule, the total number of different permutations is n! nl! (n -nJ)! n2! (n -nl -n2)! n! Distributing Objects into Boxes (n -nJ -... -nk-I)! nk! O! <l Many counting problems can be solved by enumerating the ways objects can be placed into boxes (where the order these objects are placed into the boxes does not matter). The objects can be either distinguishable, that is, different from each other, or indistinguishable, that is, considered identical. Distinguishable objects are sometimes said to be labeled, whereas indistinguishable objects are said to be unlabeled. Similarly, boxes can be distinguishable, that is, different, or indinguishable, that is, identical. Distinguishable boxes are often said to be labeled, while indistinguishable boxes are said to be unlabeled. When you solve a counting problem using the model of distributing objects into boxes, you need to determine whether the objects are distinguishable and whether the boxes are distinguisha ble. Although the context of the counting problem makes these two decisions clear, counting problems are sometimes ambiguous and it may be unclear which model applies. In such a case it is best to state whatever assumptions you are making and explain why the particular model you choose conforms to your assumptions. We will see that there are closed formulae for counting the ways to distribute objects, distinguishable or indistinguishable, into distinguishable boxes. We are not so lucky when we count the ways to distribute objects, distinguishable or indistinguishable, into indistinguishable boxes; there are no closed formulae to use in these cases. DISTINGUISH ABLE OBJECTS AND DISTINGUISH ABLE BOXES We first consider the case when distinguishable objects are placed into distinguishable boxes. Consider Example 8 in which the objects are cards and the boxes are hands of players. EXAMPLE 8 How many ways are there to distribute hands of5 cards to each of four players from the standard deck of 52 cards? Solution: We will use the product rule to solve this problem. To begin, note that the first player can be dealt 5 cards in C(52, 5) ways. The second player can be dealt 5 cards in C(47, 5) ways, because only 47 cards are left. The third player can be dealt 5 cards in C(42, 5) ways. Finally, the fourth player can be dealt 5 cards in C(37, 5) ways. Hence, the total number of ways to deal four players 5 cards each is 52! 47! 42! 37! C(52 5)C(47 5)C(42 5)C(37 5) = --. --. --. -- , , , , 47! 5! 42! 5! 37! 5! 32! 5! 52! 5!5!5!5!32! Remark: The solution to Example 8 equals the number of permutations of 52 objects, with 5 in­ distinguishable objects of each off our different types, and 32 objects of a fifth type. This equality 5-43 THEOREM 4 5.5 Generalized Permutations and Combinations 377 can be seen by defining a one-to-one correspondence between permutations of this type and dis­ tributions of cards to the players. To define this correspondence, first order the cards from 1 to 52. Then cards dealt to the first player correspond to the cards in the positions assigned to objects of the first type in the permutation. Similarly, cards dealt to the second, third, and fourth players, re­ spectively, correspond to cards in the positions assigned to objects of the second, third, and fourth type, respective ly. The cards not dealt to any player correspond to cards in the positions assigned to objects of the fifth type. The reader should verify that this is a one-to-one correspondence. Example 8 is a typical problem that involves distributing distinguishable ob jects into dis­ tinguishable boxes. The distinguishable objects are the 52 cards, and the five distinguishable boxes are the hands of the four players and the rest of the deck. Counting problems that involve distributing distinguishable objects into boxes can be solved using Theorem 4. The number of ways to distribute n distinguishable objects into k distinguishable boxes so that ni objects are placed into box i, i = 1, 2, ... , k, equals n! Theorem 4 can be proved using the product rule. We leave the details as Exercise 47. It can also be proved (see Exercise 48) by setting up a one-to-one correspondence between the permutations counted by Theorem 3 and the ways to distribute objects counted by Theorem 4. INDISTI NGUISH ABLE OBJECTS AND DISTINGUISH ABLE BOXES Counting the number of ways of placing n indistinguishable objects into k distinguishable boxes turns out to be the same as counting the number of n-combinations for a set with k elements when repti­ tions are allowed. The reason behind this is that there is a one-to-one correspondence between n-combinations from a set with k elements when repetition is allowed and the ways to place n indistinguishable balls into k distinguishable boxes. To set up this correspondence, we put a ball in the ith bin each time the ith element of the set is included in the n-combination. EXAMPLE 9 How many ways are there to place 10 indistinguishable balls into eight distinguishable bins? Solution: The number of ways to place 10 indistinguishable balls into eight bins equals the num­ ber of 10-combinations from a set with eight elements when repetition is allowed. Consequently, there are 17! C(8 + 10 -1,10) = C(17, 10) = 10!7! = 19,448. This means that there are C (n + r -1, n -1) ways to place r indistinguishable objects into n distinguishable boxes. DISTINGUISH ABLE OBJECTS AND INDISTINGUISHABLE BOXES Counting the ways to place n distinguishable objects into k indistinguishable boxes is more difficult than counting the ways to place objects, distinguishable or indistinguishable objects, into distin­ guishable boxes. We illustrate this with an example. EXAMPLE 10 How many ways are there to put four different employees into three indistinguishable offices, when each office can contain any number of employees? 378 5/ Counting 5-44 Solution: We will solve this problem by enumerating all the ways these employees can be placed into the offices. We represent the four employees by A, B, C, and D. First, we note that we can distribute employees so that all four are put into one office, three are put into one office and a fourth is put into a second office, two employees are put into one office and two put into a second office, and finally, two are put into one office, and one each put into the other two offices. Each way to distribute these employees to these offices can be represented by a way to partition the elements A, B, C, and D into disjoint subsets. We can put all four employees into one office in exactly one way, represented by { {A, B, C, D}}. We can put three employees into one office and the fourth employee into a different office in exactly four ways, represented by {{ A, B, C}, {D}}, {{ A, B, D}, {C}}, {{ A, C, D}, {B}}, and {{ B , C, D}, {A}}. We can put two employees into one office and two into a second office in exactly three ways, represented by {{ A, B}, {C, D}}, {{ A, C}, {B, D}}, and {{ A, D}, {B, C}}. Finally, we can put two employees into one office, and one each into each of the remaining two offices in six ways, represented by {{ A, B}, {C}, {D}}, {{ A, C}, {B}, {D}}, {{A, D}, {B}, {C}}, {{B, C}, {A}, {D}}, {{B, D}}, {A}, {C}}, and {{C, D}, {A}, {B}}. Counting all the possibilities, we find that there are 14 ways to put four different employees into three indistinguishable offices. Another way to look at this problem is to look at the number of offices into which we put employees. Note that there six ways to put four different employees into three indistinguishable offices so that no office is empty, seven ways to put four different employees into two indistinguishable offices so that no office is empty, and one way to put four employees into one office so that it is not empty. .... There is no simple closed formula for the number of ways to distribute n distinguishable objects into j indistinguishable boxes. However, there is a rather complicated formula, which we will now describe; readers can omit the discussion of this formula if desired. Let S(n, j) denote the number of ways to distribute n distinguishable objects into j indistinguishable boxes so that no box is empty. The numbers S(n, j) are called Stirling numbers ofthe second kind. For instance, Example 10 shows that S(4, 3) = 6, S(4, 2) = 7, and S(4, 1) = 1. We see that the number of ways to distribute n distinguishable objects into k indistinguishable boxes (where the number of boxes that are nonempty equals k, k -1, ... ,2, or 1) equals L�=I S(n, j). For instance, following the reasoning in Example 10, the number of ways to distribute four distinguishable objects into three indistinguishable boxes equals S(4, 1) + S(4, 2) + S(4, 3) = 1 + 7 + 6 = 14. Using the inclusion-exclusion principle (see Section 7.6) it can be shown that S(n, j) = � I:( _1)i (�)U -it· J. i=O I Consequently, the number of ways to distribute n distinguishable objects into k indistinguishable boxes equals ts(n,j) = t � I:(-I)i(�)U -i)n. j=1 j=1 J. i=O I (For more information about Stirling numbers of the second kind, see combinatorics textbooks such as [Bo07], [Br99], and [RoTe05], and Chapter 6 in [MiR091].) INDISTINGUISH ABLE OBJECTS AND INDISTINGUISH ABLE BOXES Some count­ ing problems can be solved by determining the number of ways to distribute indistinguishable objects into indistinguishable boxes. We illustrate this principle with an example. 5-45 5.5 Generalized Permutations and Combinations 379 EXAM PLE 11 How many ways are there to pack six copies of the same book into four identical boxes, where a box can contain as many as six books? Solution: We will enumerate all ways to pack the books. For each way to pack the books, we will list the number of books in the box with the largest number of books, followed by the numbers of books in each box containing at least one book, in order of decreasing number of books in a box. The ways we can pack the books are 6, 5, 1 4,2 4, 1, 1 3,3 3,2,1 3, 1, I, 1 2,2,2 2,2, 1, 1. For example, 4, 1, I indicates that one box contains four books, a second box contains a single book, and a third box contains a single book (and the fourth box is empty). Because we have enumerated all ways to pack six books into at most four boxes, we see that there are nine ways to pack them in this way. ... Observe that distributing n indistinguishable objects into k indistinguishable boxes is the same as writing n as the sum of at most k positive integers in nonincreasing order. If al + a2 + ... + aj = n, where a I, a2, ... ,aj are positive integers with a I ::=: a2 ::=: ... ::=: aj' we say that ai, a2, ... , aj is a partition ofthe positive integer n into j positive integers. We see that if Pk(n) is the number of partitions of n into at most k positive intetgers, then there are h(n) ways to distribute n indistinguishable objects into k indistinguishable boxes. No simple closed formula exists for this number. Exercises 1. In how many different ways can five elements be selected in order from a set with three elements when repetition is allowed? 2. In how many different ways can five elements be selected in order from a set with five elements when repetition is allowed? 3. How many strings of six letters are there? 4. Every day a student randomly chooses a sandwich for lunch from a pile of wrapped sandwiches. If there are six kinds of sandwiches, how many different ways are there for the student to choose sandwiches for the seven days of a week if the order in which the sandwiches are chosen matters? 5. How many ways are there to assign three jobs to five employees if each employee can be given more than one job? 6. How many ways are there to select five unordered ele­ ments from a set with three elements when repetition is allowed? 7. How many ways are there to select three unordered el­ ements from a set with five elements when repetition is allowed? 8. How many different ways are there to choose a dozen donuts from the 21 varieties at a donut shop? 9. A bagel shop has onion bagels, poppy seed bagels, egg bagels, salty bagels, pumpernickel bagels, sesame seed bagels, raisin bagels, and plain bagels. How many ways are there to choose a) six bagels? b) a dozen bagels? c) two dozen bagels? d) a dozen bagels with at least one of each kind? e) a dozen bagels with at least three egg bagels and no more than two salty bagels? 10. A croissant shop has plain croissants, cherry croissants, chocolate croissants, almond croissants, apple croissants, and broccoli croissants. How many ways are there to choose a) a dozen croissants? 380 5 / Counting b) three dozen croissants? c) two dozen croissants with at least two of each kind? d) two dozen croissants with no more than two broccoli croissants? e) two dozen croissants with at least five chocolate crois­ sants and at least three almond croissants? 1) two dozen croissants with at least one plain croissant, at least two cherry croissants, at least three choco­ late croissants, at least one almond croissant, at least two apple croissants, and no more than three broccoli croissants? 11. How many ways are there to choose eight coins from a piggy bank containing 100 identical pennies and 80 iden­ tical nickels? 12. How many different combinations of pennies, nickels, dimes, quarters, and half dollars can a piggy bank contain if it has 20 coins in it? 13. A book publisher has 3000 copies of a discrete mathemat­ ics book. How many ways are there to store these books in their three warehouses if the copies of the book are indistinguishable? 14. How many solutions are there to the equation Xl +X2 +X3 +X4 = 17, where Xl, X2, X3, and X4 are nonnegative integers? 15. How many solutions are there to the equation Xl +X2 +X3 +X4 +X5 = 21, where Xi, i = 1,2,3,4,5, is a nonnegative integer such that a) Xl ::: I? b) Xi ::: 2 for i = 1,2,3,4, 5? c) 0 ::: Xl ::: IO? d) 0 ::: Xl ::: 3, I ::: X2 < 4, and X3 ::: IS? 16. How many solutions are there to the equation Xl +X2 +X3 +X4 +Xs +X6 = 29, wherexi,i = 1,2,3,4,5, 6,is a nonnegative integer such that a) Xi> 1 fori = 1,2,3,4,5,6? b) Xl ::: 1, X2 ::: 2, X3 ::: 3, X4 ::: 4, Xs > 5, and X6 ::: 6? c) Xl ::: 5? d) Xl < 8 and X2 > 8? 17. How many strings of 10 ternary digits (0,1, or 2) are there that contain exactly two Os, three 1 s, and five 2s? 18. How many strings of20-decimal digits are there that con­ tain two Os, four Is, three 2s, one 3, two 4s, three 5s, two 7s, and three 9s? 19. Suppose that a large family has 14 children, including two sets of identical triplets, three sets of identical twins, and two individual children. How many ways are there to seat these children in a row of chairs if the identi­ cal triplets or twins cannot be distinguished from one another? 20. How many solutions are there to the inequality Xl + X2 + X3 ::: II, 5-46 where Xl, X2, and X3 are nonnegative integers? [Hint: In­ troduce an auxiliary variable X4 such that Xl + X2 + X3 + X4 = 11.] 21. How many ways are there to distribute six indistinguish­ able balls into nine distinguishable bins? 22. How many ways are there to distribute 12 indistinguish­ able balls into six distinguishable bins? 23. How many ways are there to distribute 12 distinguishable objects into six distinguishable boxes so that two objects are placed in each box? 24. How many ways are there to distribute IS distinguishable objects into five distinguishable boxes so that the boxes have one, two, three, four, and five objects in them, re­ spectively. 25. How many positive integers less than 1,000,000 have the sum of their digits equal to 19? 26. How many positive integers less than 1,000,000 have ex­ actly one digit equal to 9 and have a sum of digits equal to 13? 27. There are 10 questions on a discrete mathematics final exam. How many ways are there to assign scores to the problems ifthe sum ofthe scores is 100 and each question is worth at least 5 points? 28. Show that there are C(n + r -ql -q2 - ... - qr -I, n -ql -q2 -... - qr) different unordered selections of n objects of r different types that include at least ql objects of type one, q2 objects of type two, ... , and qr objects of type r. 29. How many different bit strings can be transmitted if the string must begin with a 1 bit, must include three addi­ tional 1 bits (so that a total of four 1 bits is sent), must include a total of 12 0 bits, and must have at least two 0 bits following each 1 bit? 30. How many different strings can be made from the letters in MISSISSIPPI, using all the letters? 31. How many different strings can be made from the letters in ABRACADABRA, using all the letters? 32. How many different strings can be made from the letters in AARD�RK, using all the letters, if all three As must be consecutive? 33. How many different strings can be made from the letters in ORONO, using some or all of the letters? 34. How many strings with five or more characters can be formed from the letters in SEERESS? 35. How many strings with seven or more characters can be formed from the letters in EVERGREEN? 36. How many different bit strings can be formed using six I s and eight Os? 37. A student has three mangos, two papayas, and two kiwi fruits. If the student eats one piece of fruit each day, and only the type of fruit matters, in how many different ways can these fruits be consumed? 38. A professor packs her collection of 40 issues of a mathematics journal in four boxes with 10 issues per 5-47 box. How many ways can she distribute the journals if a) each box is numbered, so that they are distinguishable? b) the boxes are identical, so that they cannot be distin­ guished? 39. How many ways are there to travel in xyz space from the origin (0,0,0) to the point (4,3,5) by taking steps one unit in the positive x direction, one unit in the positive y direction, or one unit in the positive z direction? (Moving in the negative x, y, or z direction is prohibited, so that no backtracking is allowed.) 40. How many ways are there to travel in xyzw space from the origin (0, 0, 0, 0) to the point (4, 3, 5, 4) by taking steps one unit in the positive x, positive y, positive z, or positive w direction? 41. How many ways are there to deal hands of seven cards to each of five players from a standard deck of 52 cards? 42. In bridge, the 52 cards of a standard deck are dealt to four players. How many different ways are there to deal bridge hands to four players? 43. How many ways are there to deal hands of five cards to each of six players from a deck containing 48 different cards? 44. In how many ways can a dozen books be placed on four distinguishable shelves a) if the books are indistinguishable copies of the same title? b) ifno two books are the same, and the positions of the books on the shelves matter? [Hint: Break this into 12 tasks, placing each book separately. Start with the sequence 1,2,3,4 to represent the shelves. Represent the books by bi, i = 1, 2, ... , 12. Place bl to the right of one of the terms in 1, 2, 3,4. Then successively place b2, b3, ••• , and b12.] 45. How many ways can n books be placed on k distinguish­ able shelves a) if the books are indistinguishable copies of the same title? b) ifno two books are the same, and the positions ofthe books on the shelves matter? 46. A shelf holds 12 books in a row. How many ways are there to choose five books so that no two adjacent books are chosen? [Hint: Represent the books that are chosen by bars and the books not chosen by stars. Count the num­ ber of sequences of five bars and seven stars so that no two bars are adjacent.] *47. Use the product rule to prove Theorem 4, by first placing objects in the first box, then placing objects in the second box, and so on. *48. Prove Theorem 4 by first setting up a one-to-one cor­ respondence between permutations of n objects with ni indistinguishable objects of type i, i = 1,2,3, ... , k, and the distributions of n objects in k boxes such that ni objects are placed in box i, i = 1,2,3, ... , k and then applying Theorem 3. 5.5 Generalized Permutations and Combinations 381 *49. In this exercise we will prove Theorem 2 by set­ ting up a one-to-one correspondence between the set of r-combinations with repetition allowed of S = {I, 2, 3, ... ,n} and the set of r-combinations of the set T = {I, 2, 3, ... , n + r -I}. a) Arrange the elements in an r-combination, with rep­ etition allowed, of S into an increasing sequence XI � X2 � ... � xr• Show that the sequence formed by adding k - 1 to the kth term is strictly increasing. Conclude that this sequence is made up of r distinct elements from T. b) Show that the procedure described in (a) defines a one-to-one correspondence between the set of r­ combinations, with repetition allowed, of S and the r­ combinations of T. [Hint: Show the correspondence can be reversed by associating to the r-combination {xI,x2, •.• ,xr}ofT,withl �XI <X2 < ... <xr � n + r -1, the r-combination with repetition allowed from S, formed by subtracting k - 1 from the kth element.] c) Conclude that there are C(n + r -1, r) r-combin­ ations with repetition allowed from a set with n elements. 50. How many ways are there to distribute five distinguishable objects into three indistinguishable boxes? 51. How many ways are there to distribute six distinguishable objects into four indistinguishable boxes so that each of the boxes contains at least one object? 52. How many ways are there to put five temporary employees into four identical offices? 53. How many ways are there to put six temporary em­ ployees into four identical offices so that there is at least one temporary employee in each of these four offices? 54. How many ways are there to distribute five indistinguish­ able objects into three indistinguishable boxes? 55. How many ways are there to distribute six indistinguish­ able objects into four indistinguishable boxes so that each of the boxes contains at least one object? 56. How many ways are there to pack eight identical DVDs into five indistinguishable boxes so that each box contains at least one DVD? 57. How many ways are there to pack nine identical DVDs into three indistinguishable boxes so that each box contains at least two DVDs? 58. How many ways are there to distribute five balls into seven boxes if each box must have at most one ball in it if a) both the balls and boxes are labeled? b) the balls are labeled, but the boxes are unlabeled? c) the balls are unlabeled, but the boxes are labeled? d) both the balls and boxes are unlabeled? 59. How many ways are there to distribute five balls into three boxes if each box must have at least one ball in it if a) both the balls and boxes are labeled? b) the balls are labeled, but the boxes are unlabeled? 382 5 / Counting 5-48 c) the balls are unlabeled, but the boxes are labeled? 62. How many different terms are there in the expansion of (XI + X2 + ... + xm)n after all terms with identical sets of exponents are added? d) both the balls and boxes are unlabeled? 60. Suppose that a basketball league has 32 teams, split into two conferences of 16 teams each. Each conference is split into three divisions. Suppose that the North Central Division has five teams. Each of the teams in the North Central Divsion plays four games against each of the other teams in this division, three games against each of the 11 remaining teams in the conference, and two games against each of the 16 teams in the other conference. In how many different orders can the games of one of the teams in the North Central Division be scheduled? *63. Prove the Multinomial Theorem: If n is a positive inte­ ger, then (XI +X2 + ... +xmt L C(n; nl, n2, ... , nm)x�' X�2 ... x�m, nt+n2+···+nm=n where n! C(n;nl, n2, ... , nm) = ----­nl! n2!" ·nm! *61. Suppose that a weapons inspector must inspect each of five different sites twice, visiting one site per day. The inspector is free to select the order in which to visit these sites, but cannot visit site X, the most suspicious site, on two consecutive days. In how many different orders can the inspector visit these sites? is a multinomial coefficient. 64. Find the expansion of (x + y + z)4. 65. Find the coefficient of x3 y2z5 in (x + y + z)lO. 66. How many terms are there in the expansion of (x + y + z)100? 5.6 Generating Permutations and Combinations unkS� Introduction Methods for counting various types of permutations and combinations were described in the previous sections of this chapter, but sometimes permutations or combinations need to be gen­erated, not just counted. Consider the following three problems. First, suppose that a salesman must visit six different cities. In which order should these cities be visited to minimize total travel time? One way to determine the best order is to determine the travel time for each of the 6! = 720 different orders in which the cities can be visited and choose the one with the smallest travel time. Second, suppose we are given a set of six positive integers and wish to find a subset of them that has 100 as their sum, if such a subset exists. One way to find these numbers is to generate all 26 = 64 subsets and check the sum of their elements. Third, suppose a laboratory has 95 employees. A group of 12 of these employees with a particular set of25 skills is needed for a project. (Each employee can have one or more of these skills.) One way to find such a set of employees is to generate all sets of 12 of these employees and check whether they have the desired skills. These examples show that it is often necessary to generate permutations and combinations to solve problems. Generating Permutations Any set with n elements can be placed in one-to-one correspondence with the set {I, 2, 3, ... , n}. We can list the permutations of any set of n elements by generating the permutations of the n smallest positive integers and then replacing these integers with the corresponding elements. Many different algorithms have been developed to generate the n! permutations of this set. We will describe one of these that is based on the lexicographic (or dictionary) ordering of the set of permutations of {I, 2, 3, ... , n}. In this ordering, the permutation ala2" ·an precedes the permutation of b1b2··· bn, if for some k, with I � k � n, al = bl, a2 = b2, ... , ak-I = bk-!, and ak < bk. In other words, a permutation of the set of the n smallest positive integers precedes (in lexicographic order) a second permutation if the number in this permutation in the first position where the two permutations disagree is smaller than the number in that position in the second permutation. 5-49 5.6 Generating Permutations and Combinations 383 EXAMPLE 1 The permutation 23415 of the set {I, 2, 3,4, 5} precedes the permutation 23514, because these permutations agree in the first two positions, but the number in the third position in the first permutation, 4, is smaller than the number in the third position in the second permutation, 5. Similarly, the permutation 41532 precedes 52143. .... EXAMPLE 2 An algorithm for generating the permutations of {I, 2, ... , n} can be based on a proce­dure that constructs the next permutation in lexicographic order following a given permutation ala2··· an. We will show how this can be done. First, suppose that an-I < an. Interchange an-I and an to obtain a larger permutation. No other permutation is both larger than the original per­mutation and smaller than the permutation obtained by interchanging an-I and an. For instance, the next larger permutation after 234156 is 234165. On the other hand, if an-I> an, then a larger permutation cannot be obtained by interchanging these last two terms in the permutation. Look at the last three integers in the permutation. If an-2 < an-I, then the last three integers in the permutation can be rearranged to obtain the next largest permutation. Put the smaller of the two integers an-I and an that is greater than an-2 in position n -2. Then, place the remaining integer and an-2 into the last two positions in increasing order. For instance, the next larger permutation after 234165 is 234516. On the other hand, if an-2 > an-I (and an-I> an), then a larger permutation cannot be obtained by permuting the last three terms in the permutation. Based on these observations, a general method can be described for producing the next larger permutation in increasing order following a given permutation ala2··· an. First, find the integers aj and aj+1 with aj < aj+! and that is, the last pair of adjacent integers in the permutation where the first integer in the pair is smaller than the second. Then, the next larger permutation in lexicographic order is obtained by putting in the jth position the least integer among aj+l. tlj+2 •... , and an that is greater than aj and listing in increasing order the rest of the integers aj. aj+ I, ... , an in positions j + 1 to n. It is easy to see that there is no other permutation larger than the permutation a 1 a2 ... an but smaller than the new permutation produced. (The verification of this fact is left as an exercise for the reader.) What is the next permutation in lexicographic order after 362541 ? Solution: The last pair of integers aj and aj+1 where aj < aj+1 is a3 = 2 and a4 = 5. The least integer to the right of 2 that is greater than 2 in the permutation is a5 = 4. Hence, 4 is placed in the third position. Then the integers 2, 5, and 1 are placed in order in the last three positions, giving 125 as the last three positions ofthe permutation. Hence, the next permutation is 364125. .... To produce the n! permutations ofthe integers 1, 2. 3, ... , n, begin with the smallest permu­tation in lexicographic order, namely, 123· .. n, and successively apply the procedure described for producing the next larger permutation of n! -1 times. This yields all the permutations of the n smallest integers in lexicographic order. EXAM PLE 3 Generate the permutations of the integers 1,2,3 in lexicographic order. Solution: Begin with 123. The next permutation is obtained by interchanging 3 and 2 to obtain 132. Next, because 3 > 2 and 1 < 3, permute the three integers in 132. Put the smaller on and 2 in the first position, and then put 1 and 3 in increasing order in positions 2 and 3 to obtain 213. This is followed by 231, obtained by interchanging 1 and 3, because 1 < 3. The next larger 384 5 / Counting linkS� 5-50 pennutation has 3 in the first position, followed by 1 and 2 in increasing order, namely, 312. Finally, interchange 1 and 2 to obtain the last pennutation, 321. � Algorithm 1 displays the procedure for finding the next pennutation in lexicographic order after a pennutation that is not n n -1 n -2 ... 2 1, which is the largest pennutation. ALGOR ITHM 1 Generating the Next Permutation in Lexicographic Order. procedure next permutation(a l a2 ... an: permutation of {I, 2, ... , n} not equal to n n -1 ... 2 1) j:= n -1 while aj > 0+1 j := j -1 {j is the largest subscript with aj < 0+d k:= n while aj > ak k:= k -1 {ak is the smallest integer greater than aj to the right of aj} interchange aj and ak r :=n s:= j + 1 whiler > s begin interchange ar and as r:= r -1 s := s + 1 end {this puts the tail end of the permutation after thejth position in increasing order} Generating Combinations How can we generate all the combinations of the elements of a finite set? Because a combination is just a subset, we can use the correspondence between subsets of {aI, a2, ... ,an} and bit strings oflength n. Recall that the bit string corresponding to a subset has a 1 in position kif ak is in the subset, and has a 0 in this position if ak is not in the subset. If all the bit strings of length n can be listed, then by the correspondence between subsets and bit strings, a list of all the subsets is obtained. Recall that a bit string of length n is also the binary expansion of an integer between 0 and 2n -1. The 2n bit strings can be listed in order of their increasing size as integers in their binary expansions. To produce all binary expansions oflength n, start with the bit string 000 ... 00, with n zeros. Then, successively find the next expansion until the bit string 111 ... 11 is obtained. At each stage the next binary expansion is found by locating the first position from the right that is not a 1, then changing all the Is to the right of this position to Os and making this first 0 (from the right) a 1. EXAMPLE 4 Find the next bit string after 10 0010 0111. 5-51 5.6 Generating Permutations and Combinations 385 Solution: The first bit from the right that is not a I is the fourth bit from the right. Change this bit to a I and change all the following bits to Os. This produces the next larger bit string, 10 0010 1000. .... The procedure for producing the next larger bit string after bn-1bn-2 ••• b1bo is given as Algorithm 2. ALGORITHM 2 Generating the Next Larger Bit String. procedure next bit string( bn-I bn-2 ... bl bo: bit string not equal to 11 ... 11) i:= 0 while bi = 1 begin bi :=0 i := i + 1 end bi:= 1 Next, an algorithm forgeneratingther-combinations ofthe set {l, 2, 3, ... , n} will be given. An r-combination can be represented by a sequence containing the elements in the subset in increasing order. The r-combinations can be listed using lexicographic order on these sequences. The next combinations after ala2 ... ar can be obtained in the following way: First, locate the last element ai in the sequence such that ai =f. n -r + i. Then, replace ai with ai + I and aj with ai + j -i + I, for j = i + I, i + 2, ... , r. It is left for the reader to show that this produces the next larger combination in lexicographic order. This procedure is illustrated with Example 5. EXAM PLE 5 Find the next larger4-combination ofthe set {I, 2, 3, 4, 5, 6} after {I, 2,5, 6}. Solution: The last term among the terms ai with aI = 1, a2 = 2, a3 = 5, and a4 = 6 such that ai =f. 6 -4 + i is a2 = 2. To obtain the next larger 4-combination, increment a2 by I to obtain a2 = 3. Then set a3 = 3 + I = 4 and a4 = 3 + 2 = 5. Hence the next larger 4-combination is {I, 3, 4, 5}. .... Algorithm 3 displays pseudocode for this procedure. ALGORITHM 3 Generating the Next r-Combination in Lexicographic Order. procedure next r-combinati on({al, a2, ... ,ar}: proper subset of { I, 2, ... , n} not equal to {n -r + 1, ... , n} with al <a2 < ... <ar) i:= r while ai = n -r + i i := i-I ai := ai + 1 for j := i + 1 to r aj := ai + j -i 386 5 / CQunting Exercises 1. PlacethesepermutatiQns Qf{l, 2, 3, 4, 5} inlexicQgraphic order: 43521,15432,45321,23451,23514,14532,21345, 45213,31452,31542. 2. Place these permutatiQns .of {1,2,3,4,5,6} in lexicQ­ graphic .order: 234561,231456,165432,156423,543216, 541236, 231465, 314562, 432561, 654321, 654312, 435612. 3. Find the next larger permutatiQn in lexicQgraphic .order after each .of these permutatiQns. a) 1432 b) 54123 c) 12453 d) 45231 e) 6714235 Q 31528764 4. Find the next larger permutatiQn in lexicQgraphic .order after each .of these permutatiQns. a) 1342 b) 45321 c) 13245 d) 612345 e) 1623547 Q 23587416 5. Use AlgQrithm tQ generate the 24 permutatiQns .of the first fQur PQsitive integers in lexicQgraphic .order. 6. Use AlgQrithm 2 tQ list all the subsets .of the set {I, 2, 3, 4}. 7. Use AlgQrithm 3 tQ list all the 3-cQmbinatiQns .of {I, 2, 3,4, 5}. 8. ShQW that AlgQrithm 1 produces the next larger permuta­ tiQn in lexicQgraphic .order. 9. ShQW that AlgQrithm 3 produces the next larger r-cQmbinatiQn in lexicQgraphic .order after a given r-cQmbinatiQn. 10. DevelQP an algQrithm fQr generating the r-permuta­ tiQns .of a set .of n elements. 11. List all 3-permutatiQns .of {l, 2, 3, 4, 5}. Key Terms and Results TERMS combinator ics: the study .of arrangements .of .objects enumeration: the cQunting .of arrangements .of .objects tree diagram: a diagram made up .of a roQt, branches leaving the roQt, and .other branches leaving SQme .of the endpQints .of branches permutation: an .ordered arrangement .of the elements .of a set r-permutatio n: an .ordered arrangement .of r elements .of a set P(n, r): the number .of r-permutatiQns .of a set with n elements r-combination: an unQrdered selectiQn .of r elements .of a set C(n, r): the number Qfr-cQmbinatiQns .of a set with n elements (:) (binomial coefficient): alsQ the number .of r­ cQmbinatiQns .of a set with n elements 5-52 The remammg exercises in this sectiQn develQP anQther algorithm for generating the permutatiQns .of {I, 2, 3, ... , n}. This algQrithm is based .on CantQr expansiQns .of integers. Every nQnnegative integer less than n! has a unique Cantor expansiQn all! +a22! + ... +an-I(n -I)! where ai is a nQnnegative integer nQt exceeding i, fQr i = 1,2, ... , n -1. The integers ai, a2, ... ,an-I are called the Cantor digits .of this integer. Given a permutatiQn .of {I, 2, ... , n}, let ak-I, k = 2,3, ... , n, be the number .of integers less than k that fQllQW k in the permutatiQn. FQr instance, in the permutatiQn 43215, al is the number .of integers less than 2 that fQllQW 2, SQ a I = 1. Similarly, fQr this example a2 = 2, a3 = 3, and a4 = O. CQn­ sider the functiQn frQm the set .of permutatiQns {I, 2, 3, ... , n} tQ the set .of nQnnegative integers less than n! that sends a per­ mutatiQn tQ the integer that has ai, a2, ... ,an-I, defined in this way, as its CantQr digits. 12. Find the integers that cQrresPQnd tQ these permutatiQns . a) 246531 b) 12345 c) 654321 *13. ShQW that the cQrresPQndence described here is a bijec­ tiQn between the set QfpermutatiQns .of {I, 2, 3, ... , n} and the nQnnegative integers less than n !. 14. Find the permutatiQns Qf{l, 2, 3,4, 5} thatcQrresPQnd tQ these integers with respect tQ the cQrresPQndence between CantQr expansiQns and permutatiQns as described in the preamble tQ Exercise 12. a) 3 b) 89 c) 111 15. DevelQP an algQrithm fQr producing all permutatiQns .of a set .of n elements based .on the cQrresPQndence described in the preamble tQ Exercise 12. combinatorial proof of an identity: a proQf that uses CQunt­ ing arguments tQ prove that bQth sides .of an identity CQunt the same .objects but in different ways Pascal's triangle: a representatiQn .of the binQmial c.oeffi­ cients where the ith row .of the triangle cQntains C) fQr j = 0, 1, 2, ... , i RESULTS the product rule: a basic cQunting technique, which states that the number .of ways tQ dQ a procedure that cQnsists .of twQ subtasks is the product .of the number .of ways tQ dQ the first task and the number .of ways t.o dQ the secQnd task after the first task has been dQne the sum rule: a basic cQunting technique, which states that the number .of ways tQ dQ a task in .one QftwQ ways is the sum 5-53 of the number of ways to do these tasks if they cannot be done simultaneously the pigeonhole principle: When more than k objects are placed in k boxes, there must be a box containing more than one object. the generalized pigeonhole principle: When N objects are placed in k boxes, there must be a box containing at least r N / k 1 objects. n! P(n,r)= -­(n -r)! C(n, r) = G) = -r:-C!(-nn_�---:r)""'! Review Questions 1. Explain how the sum and product rules can be used to find the number of bit strings with a length not exceeding 10. 2. Explain how to find the number of bit strings of length not exceeding 10 that have at least one 0 bit. 3. a) How can the product rule be used to find the number of functions from a set with m elements to a set with n elements? b) How many functions are there from a set with five elements to a set with 10 elements? c) How can the product rule be used to find the number of one-to-one functions from a set with m elements to a set with n elements? d) How many one-to-one functions are there from a set with five elements to a set with 10 elements? e) How many onto functions are there from a set with five elements to a set with 10 elements? 4. How can you find the number of possible outcomes of a playoff between two teams where the first team that wins four games wins the playoff? 5. How can you find the number of bit strings of length ten that either begin with 10 I or end with 01O? 6. a) State the pigeonhole principle. b) Explain how the pigeonhole principle can be used to show that among any II integers, at least two must have the same last digit. 7. a) State the generalized pigeonhole principle. b) Explain how the generalized pigeonhole principle can be used to show that among any 91 integers, there are at least ten that end with the same digit. 8. a) What is the difference between an r-combination and an r-permutation of a set with n elements? b) Derive an equation that relates the number of r­ combinations and the number of r-permutations of a set with n elements. Review Questions 387 the Binomial Theorem: (x + y)n = 2:%=0 G}xn-kyk There are nr r-permutations of a set with n elements when repetition is allowed. There are C(n + r -I, r) r-combinations ofa set with n elements when repetition is allowed. There are n!/(nl! n2!'" nk!) permutations ofn objects where there are ni indistinguishable objects of type i for i = 1,2,3, ... , k. The algorithm for generating the permutations of the set {I, 2, ... , n}. c) How many ways are there to select six students from a class of 25 to serve on a committee? d) How many ways are there to select six students from a class of 25 to hold six different executive positions on a committee? 9. a) What is Pascal's triangle? b) How can a row of Pascal's triangle be produced from the one above it? 10. What is meant by a combinatorial proof of an identity? How is such a proof different from an algebraic one? 11. Explain how to prove Pascal's Identity using a combina­ torial argument. 12. a) State the Binomial Theorem. b) Explain how to prove the Binomial Theorem using a combinatorial argument. c) Find the coefficient of xI00yl01 in the expansion of (2x + 5y)201. 13. a) Explain how to find a formula for the number of ways to select r objects from n objects when repetition is allowed and order does not matter. b) How many ways are there to select a dozen objects from among objects of five different types if objects of the same type are indistinguishable? c) How many ways are there to select a dozen objects from these five different types if there must be at least three objects of the first type? d) How many ways are there to select a dozen objects from these five different types if there cannot be more than four objects ofthe first type? e) How many ways are there to select a dozen objects from these five different types ifthere must be at least two objects of the first type but no more than three objects of the second type? 14. a) Let n and r be positive integers. Explain why the number of solutions of the equation XI + X2 + ... + 388 5 / Counting Xn = r, where Xi is a nonnegative integer for i = 1,2,3, ... , n, equals the number of r-combinations of a set with n elements. b) How many solutions in nonnegative integers are there to the equation XI + X2 + X3 + X4 = 17? c) How many solutions in positive integers are there to the equation in part (b)? 15. a) Derive a formula for the number of permutations ofn objects of k different types, where there are n I indis­ tinguishable objects of type one, n2 indistinguishable objects of type two, ... , and nk indistinguishable ob­ jects of type k. Supplementar y Exercises 1. How many ways are there to choose 6 items from 10 dis­ tinct items when a) the items in the choices are ordered and repetition is not allowed? b) the items in the choices are ordered and repetition is allowed? c) the items in the choices are unordered and repetition is not allowed? d) the items in the choices are unordered and repetition is allowed? 2. How many ways are there to choose 10 items from 6 dis­ tinct items when a) the items in the choices are ordered and repetition is not allowed? b) the items in the choices are ordered and repetition is allowed? c) the items in the choices are unordered and repetition is not allowed? d) the items in the choices are unordered and repetition is allowed? 3. A test contains 100 true/false questions. How many dif­ ferent ways can a student answer the questions on the test, if answers may be left blank? 4. How many bit stringsoflength 10 either start with 000 or end with 1111? 5. How many bit strings of length 10 over the alpha­ bet {a, b, c} have either exactly three as or exactly four bs? 6. The internal telephone numbers in the phone system on a campus consist of five digits, with the first digit not equal to zero. How many different numbers can be assigned in this system? 7. An ice cream parlor has 28 different flavors, 8 different kinds of sauce, and 12 toppings. a) In how many different ways can a dish of three scoops of ice cream b� made where each flavor can be used more than once and the order of the scoops does not matter? 5-54 b) How many ways are there to order the letters of the word INDISCREETNESS? 16. Describe an algorithm for generating all the permutations of the set of the n smallest positive integers. 17. a) How many ways are there to deal hands of five cards to six players from a standard 52-card deck? b) How many ways are there to distribute n distinguish­ able objects into k distinguishable boxes so that ni objects are placed in box i? 18. Describe an algorithm for generating all the combinations ofthe set of the n smallest positive integers. b) How many different kinds of small sundaes are there if a small sundae contains one scoop of ice cream, a sauce, and a topping? c) How many different kinds of large sundaes are there if a large sundae contains three scoops of ice cream, where each flavor can be used more than once and the order of the scoops does not matter; two kinds of sauce, where each sauce can be used only once and the order of the sauces does not matter; and three top­ pings, where each topping can be used only once and the order of the toppings does not matter? 8. How many positive integers less than 1000 a) have exactly three decimal digits? b) have an odd number of decimal digits? c) have at least one decimal digit equal to 9? d) have no odd decimal digits? e) have two consecutive decimal digits equal to 5? t) are palindromes (that is, read the same forward and backward)? 9. When the numbers from 1 to 1000 are written out in dec­ imal notation, how many of each of these digits are used? a) 0 b) I c)2 d) 9 10. There are 12 signs of the zodiac. How many people are needed to guarantee that at least six of these people have the same sign? 11. A fortune cookie company makes 213 different fortunes. A student eats at a restaurant that uses fortunes from this company. What is the largest possible number of times that the student can eat at the restaurant without getting the same fortune four times? 12. How many people are needed to guarantee that at least two were born on the same day of the week and in the same month (perhaps in different years)? 13. Show that given any set of 10 positive integers not ex­ ceeding 50 there exist at least two different five-element subsets of this set that have the same sum. 14. A package of baseball cards contains 20 cards. How many packages must be purchased to ensure that two cards in 5-55 these packages are identical if there are a total of 550 different cards? 15. a) How many cards must be chosen from a deck to guar­ antee that at least two aces are chosen? b) How many cards must be chosen from a deck to guar­ antee that at least two aces and two kinds are chosen? c) How many cards must be chosen from a deck to guar­ antee that there are at least two cards of the same kind? d) How many cards must be chosen from a deck to guar­ antee that there are at least two cards of two different kinds? *16. Show that in any set ofn + I positive integers not exceed­ ing 2n there must be two that are relatively prime. *17. Show that in a sequence ofm integers there exists one or more consecutive terms with a sum divisible by m. 18. Show that if five points are picked in the interior of a square with a side length of 2, then at least two ofthese points are no farther than ..Ji apart. 19. Show that the decimal expansion of a rational number must repeat itself from some point onward. 20. Once a computer worm infects a personal computer via an infected e-mail message, it sends a copy of itselfto 100 e-mail addresses it finds in the electronic message mailbox on this personal computer. What is the maximum number of different computers this one computer can infect in the time it takes for the infected message to be forwarded five times? 21. How many ways are there to choose a dozen donuts from 20 varieties a) ifthere are no two donuts ofthe same variety? b) if all donuts are of the same variety? c) if there are no restrictions? d) if there are at least two varieties? e) if there must be at least six blueberry-filled donuts? t) if there can be no more than six blueberry-filled donuts? 22. Find n if a) pen, 2) = 110. b) pen, n) = 5040. c) P(n, 4) = 12P(n, 2). 23. Findn if a) C(n, 2) = 45. b) C(n, 3) = pen, 2). c) C(n, 5) = C(n, 2). 24. Show that if n and r are nonnegative integers and n :::: r, then pen + I, r) = pen, r)(n + I)/(n + 1 -r). *25. Suppose that S is a set with n elements. How many or­ dered pairs (A, B) are there such that A and·B are subsets of S with A S; B? [Hint: Show that each element of S belongs to A, B -A, or S -B.] 26. Give a combinatorial proof of Corollary 2 of Section 5.4 by setting up a correspondence between the subsets of a set with an even number of elements and the subsets of this set with an odd number of elements. [Hint: Take an element a in the set. Set up the correspondence by putting Supplementary Exercises 389 a in the subset if it is not already in it and taking it out if it is in the subset.] 27. Let nand r be nonnegative integers with r < n. Show that C(n,r -I) = C(n + 2,r + I) -2C(n + I, r + I) + C(n, r + I). 28. Prove using mathematical induction that LJ = 2 C(j, 2) = C (n + I, 3) whenever n is an integer greater than 1. 29. Show that if n is an integer then 30. In this exercise we will derive a formula for the sum of the squares of the n smallest positive integers. We will count the number of triples (i, j, k) such that i, j, and k are in­ tegers such that 0 S i < k, 0 S j < k, and I S k S n in two ways. a) Show that there are k2 such triples with a fixed k. Conclude that there are L� = I k2 such triples. b) Show that the number of such triples with 0 S i < j < k and the number of such triples with 0 S j < i < k both equal C(n + 1,3). c) Show that the number of such triples with 0 S i = j < k equals C(n + 1,2). d) Combining part (a) with parts (b) and (c), conclude that n L e = 2C(n + I, 3) + C(n + I, 2) k=1 = n(n + 1)(2n + 1)/6. *31. How many bit strings of length n, where n :::: 4, contain exactly two occurrences of 0 I? 32. Let S be a set. We say that a collection of subsets AI, A2, ... , An each containing d elements, where d :::: 2, is 2-colorable if it is possible to assign to each element of S one of two different colors such that in every subset Ai there are elements that have been assigned each color. Let m(d) be the largest integer such that every collection of fewer than m(d) sets each con­ taining d elements is 2-colorable. a) Show that the collection of all subsets with d elements of a set S with 2d - 1 elements is not 2-colorable. b) Show that m(2) = 3. **c) Show that m(3) = 7. [Hint: Show that the collec­ tion {I, 3, 5}, {I, 2, 6}, {I, 4, 7}, {2, 3, 4}, {2, 5, 7}, {3, 6, 7}, {4, 5, 6} is not 2-colorable. Then show that all collections of six sets with three elements each are 2-colorable. ] 33. A professor writes 20 multiple-choice questions, each with the possible answer a, b, c, or d, for a discrete math­ ematics test. If the number of questions with a, b, c, and d as their answer is 8, 3, 4, and 5, respectively, how many different answer keys are possible, if the questions can be placed in any order? 390 5 / Counting 34. How many different arrangements are there of eight peo­ ple seated at a round table, where two arrangements are considered the same if one can be obtained from the other by a rotation? 35. How many ways are there to assign 24 students to five faculty advisors? 36. How many ways are there to choose a dozen apples from a bushel containing 20 indistinguishable Delicious apples, 20 indistinguishable Macintosh apples, and 20 indistin­ guishable Granny Smith apples, if at least three of each kind must be chosen? 37. How many solutions are there to the equation Xl + X2 + X3 = 17, where Xl, x2, and X3 are nonnegative integers with a) Xl > l,x2 > 2,andx3 > 3? b) Xl < 6andx3 > 5? c) Xl < 4, X2 < 3, and X3 > 5? 38. a) How many different strings can be made from the word PEPPER CORN when all the letters are used? b) How many of these strings start and end with the letter P? c) In how many of these strings are the three letter Ps consecutive? 39. How many subsets of a set with ten elements a) have fewer than five elements? b) have more than seven elements? c) have an odd number of elements? 40. A witness to a hit-and-run accident tells the police that the license plate ofthe car in the accident, which contains three letters followed by three digits, starts with the let- Computer Projects Write programs with these input and output. 1. Given a positive integer n and a nonnegative integer not exceeding n, find the number of r-permutations and r­ combinations of a set with n elements. 2. Given positive integers n and r, find the number of r-permutations when repetition is allowed and r­ combinations when repetition is allowed of a set with n elements. 3. Given a sequence of positive integers, find the longest in­ creasing and the longest decreasing subsequence of the sequence. *4. Given an equation Xl + X2 + ... + Xn = C, where C is a constant, and Xl, X2, ••• ,Xn are nonnegative integers, list all the solutions. 5. Given a positive integer n, list all the permutations of the set {I, 2, 3, ... , n} in lexicographic order. 5-56 ters AS and contains both the digits 1 and 2. How many different license plates can fit this description? 41. How many ways are there to put n identical objects into m distinct containers so that no container is empty? 42. How many ways are there to seat six boys and eight girls in a row of chairs so that no two boys are seated next to each other? 43. How many ways are there to distribute six objects to five boxes if a) both the objects and boxes are labeled? b) the objects are labeled, but the boxes are unlabeled? c) the objects are unlabeled, but the boxes are labeled? d) both the objects and the boxes are unlabeled? 44. How many ways are there to distribute five objects into six boxes if a) both the objects and boxes are labeled? b) the objects are labeled, but the boxes are unlabeled? c) the objects are unlabeled, but the boxes are labeled? d) both the objects and the boxes are unlabeled? 45. Devise an algorithm for generating all the r-permu­ tations of a finite set when repetition is allowed. 46. Devise an algorithm for generating all the r-combinations of a finite set when repetition is allowed. *47. Show that ifm and n are integers with m � 3 and n � 3, then R(m, n) :s R(m, n -1) + R(m -1, n). *48. Show that R(3, 4) � 7 by showing that in a group of six people, where any two people are friends or enemies, there are not necessarily three mutual friends or four mutual enemies. 6. Given a positive integer n and a nonnegative integer r not exceeding n, list all the r-combinations of the set { 1, 2, 3, ... , n} in lexicographic order. 7. Given a positive integer n and a nonnegative integer r not exceeding n, list all the r-permutations of the set {l, 2, 3, ... , n} in lexicographic order. 8. Given a positive integer n, list all the combinations ofthe set {I, 2, 3, ... , n}. 9. Given positive integers nand r, list all the r-permutations, with repetition allowed, of the set {1,2,3, ... ,n}. 10. Given positive integers nand r , list all the r-combinations, with repetition allowed, of the set {I, 2, 3, ... , n}. 5-57 Writing Projects 391 Computations and Explorations Use a computational program or programs you have written to do these exercises. 1. Find the number of possible outcomes in a two-team play­ off when the winner is the first team to win 5 out of 9, 6 out of 11,7 out of 13, and 8 out of 15. 2. Which binomial coefficients are odd? Can you formulate a conjecture based on numerical evidence? 3. Verify that C (2n, n) is divisible by the square of a prime, when n =I 1, 2, or 4, for as many positive integers n as you can. [The theorem that tells that C (2n, n) is divisible by the square of a prime with n =I 1, 2, or 4 was proved in 1996 by Andrew Granville and Olivier Raman!. Their proof set­ tled a conjecture made in 1980 by by Paul Erdos and Ron Graham.] Writing Projects Respond to these with essays using outside sources. 1. Describe some of the earliest uses of the pigeonhole prin­ ciple by Dirichlet and other mathematicians. 2. Discuss ways in which the current telephone numbering plan can be extended to accommodate the rapid demand for more telephone numbers. (See if you can find some of the proposals coming from the telecommunications in­ dustry.) For each new numbering plan you discuss, show how to find the number of different telephone numbers it supports. 3. Many combinatorial identities are described in this book. Find some sources of such identities and de­ scribe important combinatorial identities besides those al­ ready introduced in this book. Give some representative proofs, including combinatorial on es, of some of these identities. 4. Describe the different models used to model the dis­ tribution of particles in statistical mechanics, includ-4. Find as many odd integers n less than 200 as you can for which C(n, Ln/2J) is not divisible by the square ofa prime. Formulate a conjecture based on your evidence. *5. For each integer less than 100 determine whether C (2n, n) is divisible by 3. Can you formulate a conjecture that tells us for which integers n the binomial coefficient C (2n, n) is divisible by 3 based on the digits in the base three expansion ofn? 6. Generate all the permutations of a set with eight elements. 7. Generate all the 6-permutations ofa set with nine elements. 8. Generate all combinations of a set with eight elements. 9. Generate all 5-combinations with repetition allowed of a set with seven elements. ing Maxwell-Boltzmann, Bose-Einstein, and Fermi-Dirac statistics. In each case, describe the counting techniques used in the model. 5. Define the Stirling numbers of the first kind and describe some of their properties and the identities they satisfy. 6. Define the Stirling numbers ofthe second kind and describe some of their properties and the identities they satisfy. 7. Describe the latest discoveries of values and bounds for Ramsey numbers. 8. Describe additional ways to generate all the permutations of a set with n elements besides those found in Section 5.6. Compare these algorithms and the algorithms described in the text and exercises of Section 5.6 in terms of their computational complexity. 9. Describe at least one way to generate all the partitions of a positive integer n. (See Exercise 47 in Section 4.3.) CH APTER 6.1 An Introduction to Discrete Probability 6.2 Probability Theory 6.3 Bayes' Theorem 6.4 Expected Value and Variance Discrete Probability cc ombinatorics and probability theory share common origins. The theory of probability was first developed more than 300 years ago, when certain gambling games were analyzed. Although probability theory was originally invented to study gambling, it now plays an essential role in a wide variety of disciplines. For example, probability theory is extensively applied in the study of genetics, where it can be used to help understand the inheritance of traits. Of course, probability still remains an extremely popular part of mathematics because of its applicability to gambling, which continues to be an extremely popular human endeavor. In computer science, probability theory plays an important role in the study of the com­plexity of algorithms. In particular, ideas and techniques from probability theory are used to determine the average-case complexity of algorithms. Probabilistic algorithms can be used to solve many problems that cannot be easily or practically solved by deterministic algorithms. In a probabilistic algorithm, instead of always following the same steps when given the same input, as a deterministic algorithm does, the algorithm makes one or more random choices, which may lead to different output. In combinatorics, probability theory can even be used to show that objects with certain properties exist. The probabilistic method, a technique in combinatorics introduced by Paul Erdos and Alfred Renyi, shows that an object with a specified property exists by showing that there is a positive probability that a randomly constructed object has this property. Probability theory can help us answer questions that involve uncertainty, such as determining whether we should reject an incoming mail message as spam based on the words that appear in the message. 6.1 An Introduction to Discrete Probability 6-/ Introduction Probability theory dates back to the seventeenth century when the French mathematician Blaise Pascal determined the odds of winning some popular bets based on the outcome when a pair of dice is repeatedly rolled. In the eighteenth century, the French mathematician Laplace, who also studied gambling, defined the probability of an event as the number of successful outcomes divided by the number of possible outcomes. For instance, the probability that a die comes up an odd number when it is rolled is the number of successful outcomes-namely, the number of ways it can come up odd-divided by the number of possible outcomes-namely, the number of different ways the die can come up. There are a total of six possible outcomes-namely, 1,2,3,4,5, and 6-and exactly three of these are successful outcomes-namely, 1,3, and 5. Hence, the probability that the die comes up an odd number is 3/6 = 1/2. (Note that it has been assumed that all possible outcomes are equally likely, or, in other words, that the die is fair.) In this section we will restrict ourselves to experiments that have finitely many, equally likely, outcomes. This permits us to use Laplace's definition of the probability of an event. We will continue our study of probability in Section 6.2, where we will study experiments with finitely many outcomes that are not necessarily equally likely. In Section 6.2 we will also introduce some key concepts in probability theory, including conditional probability, independence of events, and random variables. In Section 6.4 we will introduce the concepts of the expectation and variance of a random variable. 393 394 6 / Discrete Probability 6-2 DEFINITION 1 EXAMPLE 1 EK1ra� ExampleS� EXAMPLE 2 unr(s� EXAMPLE 3 lint(S� Finite Probability An experiment is a procedure that yields one of a given set of possible outcomes. The sample space of the experiment is the set of possible outcomes. An event is a subset of the sample space. Laplace's definition of the probability of an event with finitely many possible outcomes will now be stated. If S is a finite sample space of equally likely outcomes, and E is an event, that is, a subset of S, then the probability of E is p(E) = II�II. Examples 1-8 illustrate how the probability of an event is found. An urn contains four blue balls and five red balls. What is the probability that a ball chosen from the urn is blue? Solution: To calculate the probability, note that there are nine possible outcomes, and four of these possible outcomes produce a blue ball. Hence, the probability that a blue ball is chosen is 4/9. � What is the probability that when two dice are rolled, the sum of the numbers on the two dice is 7? Solution: There are a total of 36 equally likely possible outcomes when two dice are rolled. (The product rule can be used to see this; because each die has six possible outcomes, the total number of outcomes when two dice are rolled is 62 = 36.) There are six successful outcomes, namely, (1,6), (2,5), (3,4), (4,3), (5,2), and (6,1), where the values of the first and second dice are represented by an ordered pair. Hence, the probability that a seven comes up when two fair dice are rolled is 6/36 = 1/6. � Lotteries are extremely popular. We can easily compute the odds of winning different types of lotteries. In a lottery, players win a large prize when they pick four digits that match, in the correct order, four digits selected by a random mechanical process. A smaller prize is won if only three digits PIERRE-SI MON LAPLACE (1749-1827) Pierre-Simon Laplace came from humble origins in Normandy. In his childhood he was educated in a school run by the Benedictines. At 16 he entered the University of Caen intending to study theology. However, he soon realized his true interests were in mathematics. After completing his studies, he was named a provisional professor at Caen, and in 1769 he became professor of mathematics at the Paris Military School. Laplace is best known for his contributions to celestial mechanics, the study of the motions of heavenly bod­ ies. His Traite du Mecanique Celeste is considered one of the greatest scientific works of the early nineteenth cen­ tury. Laplace was one ofthe founders of probability theory and made many contributions to mathematical statis­ tics. His work in this area is documented in his book Theorie Analytique des Probabilitees, in which he defined the probability of an event as the ratio of the number of favorable outcomes to the total number of outcomes of an experiment. Laplace was famous for his political flexibility. He was loyal, in succession, to the French Republic, Napoleon, and King Louis XVIII. This flexibility permitted him to be productive before, during, and after the French Revolution. 6-3 6.1 An Introduction to Discrete Probability 395 are matched. What is the probability that a player wins the large prize? What is the probability that a player wins the small prize? Solution: There is only one way to choose all four digits correctly. By the product rule, there are 104 = 10,000 ways to choose four digits. Hence, the probability that a player wins the large prize is 1/10,000 = 0.000l. Players win the smaller prize when they correctly choose exactly three of the four digits. Exactly one digit must be wrong to get three digits correct, but not all four correct. By the sum rule, to find the number of ways to choose exactly three digits correctly we add the number of ways to choose four digits matching the digits picked in all but the ith position, for i = 1,2, 3,4. To count the number of successes with the first digit incorrect, note that there are nine possible choices for the first digit (all but the one correct digit), and one choice for each of the other digits, namely, the correct digits for these slots. Hence, there are nine ways to choose four digits where the first digit is incorrect, but the last three are correct. Similarly, there are nine ways to choose four digits where the second digit is incorrect, nine with the third digit incorrect, and nine with the fourth digit incorrect. Hence, there is a total of 36 ways to choose four digits with exactly three of the four digits correct. Thus, the probability that a player wins the smaller prize is 36/10,000 = 9/2500 = 0.0036. .... EXAMPLE 4 There are many lotteries now that award enormous prizes to people who correctly choose a set of six numbers out of the first n positive integers, where n is usually between 30 and 60. What is the probability that a person picks the correct six numbers out of 40? unkS� EXAM PLES Solution: There is only one winning combination. The total number of ways to choose six numbers out of 40 is 40! C(40, 6) = 34! 6! = 3,838,380. Consequently, the probability of picking a winning combination is 1/3,838,380 � 0.00000026. (Here the symbol � means approximately equal to.) .... Poker, and other card games, are growing in popularity. To win at these games it helps to know the probability of different hands. We can find the probability of specific hands that arise in card games using the techniques developed so far. A deck of cards contains 52 cards. There are 13 different kinds of cards, with four cards of each kind. (Among the terms commonly used instead of "kind" are "rank," "face value," "denomination," and "value.") These kinds are twos, threes, fours, fives, sixes, sevens, eights, nines, tens, jacks, queens, kings, and aces. There are also four suits: spades, clubs, hearts, and diamonds, each containing 13 cards, with one card of each kind in a suit. In many poker games, a hand consists of five cards. Find the probability that a hand of five cards in poker contains four cards of one kind. Solution: By the product rule, the number of hands of five cards with four cards of one kind is the product of the number of ways to pick one kind, the number of ways to pick the four of this kind out of the four in the deck of this kind, and the number of ways to pick the fifth card. This is C(13, 1)C(4, 4)C(48, 1). 396 6 / Discrete Probability 6-4 By Example 11 in Section 5.3 there are C(52, 5) different hands of five cards. Hence, the probability that a hand contains four cards of one kind is C(13, 1)C(4, 4)C(48, 1) = 13· 1 . 48 � 0.00024. C(52,5) 2,598,960 EXAMPLE 6 What is the probability that a poker hand contains a full house, that is, three of one kind and two of another kind? Solution: By the product rule, the number of hands containing a full house is the product of the number of ways to pick two kinds in order, the number of ways to pick three out of four for the first kind, and the number of ways to pick two out of four for the second kind. (Note that the order of the two kinds matters, because, for instance, three queens and two aces is different than three aces and two queens.) We see that the number of hands containing a full house is P(13, 2)C(4, 3)C(4, 2) = 13 . 12·4·6 = 3744. Because there are 2,598,960 poker hands, the probability of a full house is 3744 --- � 0.0014. 2,598,960 EXAMPLE 7 What is the probability that the numbers 11,4,17,39, and 23 are drawn in that order from a bin containing 50 balls labeled with the numbers 1,2, ... ,50 if (a) the ball selected is not returned to the bin before the next ball is selected and (b) the ball selected is returned to the bin before the next ball is selected? Solution: (a) By the product rule, there are 50·49 . 48 ·47 . 46 = 254,251,200 ways to select the balls because each time a ball is drawn there is one fewer ball to choose from. Consequently, the probability that 11,4,17,39, and 23 are drawn in that order is 1/254,251,200. This is an example of sampling without replacement. (b) By the product rule, there are 505 = 312,500,000 ways to select the balls because there are 50 possible balls to choose from each time a ball is drawn. Consequently, the probability that 11,4, 17,39, and 23 are drawn in that order is 1/312,500,000. This is an example of sampling with replacement. .... The Probability of Combinations of Events We can use counting techniques to find the probability of events derived from other events. THEOREM 1 Let E be an event in a sample space S. The probability of the event E, the complementary event of E, is given by p(E) = 1 -p(E). Proof: To find the probability of the event E, note that I E I = I S I -I E I. Hence, (E) = lSI -lEI = 1 -� = 1 -(E). p lSI lSI p <l 6-5 6.1 An Introduction to Discrete Probability 397 There is an alternative strategy for finding the probability of an event when a direct approach does not work well. Instead of determining the probability of the event, the probability of its complement can be found. This is often easier to do, as Example 8 shows. EXAMPLE 8 A sequence of 10 bits is randomly generated. What is the probability that at least one ofthese bits is O? Solution: Let E be the event that at least one of the 10 bits is O. Then E is the event that all the bits are 1 s. Because the sample space S is the set of all bit strings of length 10, it follows that - lEI 1 p(E) = 1 -p(E) = 1 -lSi = 1 -210 1 1023 = 1 -1024 = 1024' Hence, the probability that the bit string will contain at least one 0 bit is 1023/1024. It is quite difficult to find this probability directly without using Theorem 1. .... We can also find the probability of the union of two events. THEOREM 2 Let E, and E2 be events in the sample space S. Then EXAMPLE 9 Extra� EXamples IiiiiiiI Proof: Using the formula given in Section 2.2 for the number of elements in the union of two sets, it follows that Hence, (E UE)= IE,UE21 p, 2 lSI = IE,I + IE21-IE, n E21 lSI lEI! IE21 IE, n E21 = 1ST + 1ST - lSI = p(EJ) + P(E2) -p(E, n E2)' <I What is the probability that a positive integer selected at random from the set of positive integers not exceeding 100 is divisible by either 2 or 5? Solution: Let E, be the event that the integer selected is divisible by 2, and let E2 be the event that it is divisible by 5. Then E, U E2 is the event that it is divisible by either 2 or 5. Also, 398 6 / Discrete Probability 6-6 EXAMPLE 10 linkS� Exercises EI n E2 is the event that it is divisible by both 2 and 5, or equivalently, that it is divisible by 10. Because lEd = 50, IE21 = 20, and lEI n E21 = 10, it follows that p(EI u E2) = p(EI) + P(E2) -p(EI n E2) 50 20 10 3 = 100 + 100 -100 = s· Probabilistic Reasoning A common problem is determining which of two events is more likely. Analyzing the probabil­ities of such events can be tricky. Example 10 describes a problem of this type. It discusses a famous problem originating with the television game show Let s Make a Deal. The Monty Hall Three-Door Pnzzle Suppose you are a game show contestant. You have a chance to win a large prize. You are asked to select one of three doors to open; the large prize is behind one of the three doors and the other two doors are losers. Once you select a door, the game show host, who knows what is behind each door, does the following. First, whether or not you selected the winning door, he opens one of the other two doors that he knows is a losing door (selecting at random ifhoth are losing doors). Then he asks you whether you would like to switch doors. Which strategy should you use? Should you change doors or keep your original selection, or does it not matter? Solution: The probability you select the correct door (before the host opens a door and asks you w4ether you want to change) is 1/3, because the three doors are equally likely to be the correct door. The probability this is the correct door does not change once the game show host opens one of the other doors, because he will always open a door that the prize is not behind. >-The probability that you selected incorrectly is the probability the prize is behind one of the two doors you did not select. Consequently, the probability that you selected incorrectly is 2/3. If you selected incorrectly, when the game show host opens a door to show you that the prize is not behind it, the prize is behind the other door. You will always win if your initial choice was incorrect and you change doors. So, by changing doors, the probability you win is 2/3. In other words, you should always change doors when given the chance to do so by the game show host. This doubles the probability that you will win. (A more rigorous treatment of this puzzle can be found in Exercise 15 of Section 6.3.) � 1. What is the probability that a card selected from a deck is an ace? 7. What is the probability that when a coin is flipped six times in a row, it lands heads up every time? 2. What is the probability that a die comes up six when it is rolled? 3. What is the probability that a randomly selected integer chosen from the first 100 positive integers is odd? 4. What is the probability that a randomly selected day of the year (from the 366 possible days) is in April? 5. What is the probability that the sum of the numbers on two dice is even when they are rolled? 6. What is the probability that a card selected from a deck is an ace or a heart? 8. What is the probability that a five-card poker hand con­ tains the ace of hearts? 9. What is the probability that a five-card poker hand does not contain the queen of hearts? 10. What is the probability that a five-card poker hand con­ tains the two of diamonds and the three of spades? 11. What is the probability that a five-card poker hand contains the two of diamonds, the three of spades, the six of hearts, the ten of clubs, and the king of hearts? 6-7 12. What is the probability that a five-card poker hand con­ tains exactly one ace? 13. What is the probability that a five-card poker hand con­ tains at least one ace? 14. What is the probability that a five-card poker hand con­ tains cards of five different kinds? 15. What is the probability that a five-card poker hand con­ tains two pairs (that is, two of each of two different kinds and a fifth card of a third kind)? 16. What is the probability that a five-card poker hand con­ tains a flush, that is, five cards of the same suit? 17. What is the probability that a five-card poker hand con­ tains a straight, that is, five cards that have consecutive kinds? (Note that an ace can be considered either the low­ est card of an A-2-3-4-5 straight or the highest card of a lO-J-Q-K-A straight.) 18. What is the probability that a five-card poker hand con­ tains a straight flush, that is, five cards of the same suit of consecutive kinds? *19. What is the probability that a five-card poker hand con­ tains cards of five different kinds and does not contain a flush or a straight? 20. What is the probability that a five-card poker hand con­ tains a royal flush, that is, the 10, jack, queen, king, and ace of one suit? 21. What is the probability that a die never comes up an even number when it is rolled six times? 22. What is the probability that a positive integer not exceed­ ing 100 selected at random is divisible by 3? 23. What is the probability that a positive integer not exceed­ ing 100 selected at random is divisible by 5 or 7? 24. Find the probability of winning the lottery by selecting the correct six integers, where the order in which these integers are selected does not matter, from the positive integers not exceeding a) 30. b) 36. c) 42. d) 48. 25. Find the probability of winning the lottery by selecting the correct six integers, where the order in which these integers are selected does not matter, from the positive integers not exceeding a) 50. b) 52. c) 56. d) 60. 26. Find the probability of selecting none of the correct six integers, where the order in which these integers are se­ lected does not matter, from the positive integers not exceeding a) 40. b) 48. c) 56. d) 64. 27. Find the probability of selecting exactly one of the correct six integers, where the order in which these integers are selected does not matter, from the positive integers not exceeding a) 40. b) 48. c) 56. d) 64. 28. In a superlottery, a player selects 7 numbers out of the first 80 positive integers. What is the probability that a person 6.1 An Introduction to Discrete Probability 399 wins the grand prize by picking 7 numbers that are among the 11 numbers selected at random by a computer. 29. In a superlottery, players win a fortune if they choose the eight numbers selected by a computer from the positive integers not exceeding 100. What is the probability that a player wins this superlottery? 30. What is the probability that a player wins the prize offered for correctly choosing five (but not six) numbers out of six integers chosen at random from the integers between 1 and 40, inclusive? 31. Suppose that 100 people enter a contest and that different winners are selected at random for first, second, and third prizes. What is the probability that Michelle wins one of these prizes if she is one ofthe contestants? 32. Suppose that 100 people enter a contest and that different winners are selected at random for first, second, and third prizes. What is the probability that Kumar, Janice, and Pedro each win a prize if each has entered the contest? 33. What is the probability that Abby, Barry, and Sylvia win the first, second, and third prizes, respectively, in a draw­ ing if 200 people enter a contest and a) no one can win more than one prize. b) winning more than one prize is allowed. 34. What is the probability that Bo, Colleen, Jeff, and Rohini win the first, second, third, and fourth prizes, respectively, in a drawing if 50 people enter a contest and a) no one can win more than one prize. b) winning more than one prize is allowed. 35. In roulette, a wheel with 38 numbers is spun. Ofthese, 18 are red, and 18 are black. The other two numbers, which are neither black nor red, are 0 and 00. The probability that when the wheel is spun it lands on any particular number is 1/38. a) What is the probability that the wheel lands on a red number? b) What is the probability that the wheel lands on a black number twice in a row? c) What is the probability that the wheel lands on 0 or OO? d) What is the probability that in five spins the wheel never lands on either 0 or OO? e) What is the probability that the wheel lands on a num­ ber between 1 and 6, inclusive, on one spin, but does not land between them on the next spin? 36. Which is more likely: rolling a total of 8 when two dice are rolled or rolling a total of8 when three dice are rolled? 37. Which is more likely: rolling a total of 9 when two dice are rolled or rolling a total of9 when three dice are rolled? 38. Two events EI and E2 are called independent if p(EI n E2) = p(EJ)p(E2). For each of the following pairs of events, which are subsets ofthe set of all possible outcomes when a coin is tossed three times, determine whether or not they are independent. a) E I: the first coin comes up tails; E 2: the second coin comes up heads. 400 6 / Discrete Probability 6-8 b) E 1: the first coin comes up tails; E2: two, and not three, heads come up in a row. behind each door, opens a losing door and gives you the chance to change doors? What is the probability that you win by changing the door you select to one of the two remaining doors among the three that you did not select? e) E 1: the second coin comes up tails; E 2: two, and not three, heads come up in a row. (We will study independence of events in more depth in Section 6.2.) 41. This problem was posed by the Chevalier de Mere and was solved by Blaise Pascal and Pierre de Fermat. 39. Explain what is wrong with the statement that in the Monty Hall Three-Door Puzzle the probability that the prize is behind the first door you select and the probabil­ ity that the prize is behind the other of the two doors that Monty does not open are both 1/2, because there are two doors left. a) Find the probability of rolling at least one six when a die is rolled four times. b) Find the probability that a double six comes up at least once when a pair of dice is rolled 24 times. Answer the query the Chevalier de Mere made to Pascal asking whether this probability was greater than 112. "0. Suppose that instead of three doors, there are four doors in the Monty Hall puzzle. What is the probability that you win by not changing once the host, who knows what is e) Is it more likely that a six comes up at least once when a die is rolled four times or that a double six comes up at least once when a pair of dice is rolled 24 times? 6.2 Probability Theory unkS� LinkS� Introduction In Section 6.1 we introduced the notion of the probability of an event. (Recall that an event is a subset of the possible outcomes of an experiment.) We defined the probability of an event E as Laplace did, that is, E -� p( ) -lSI' the number of outcomes in E divided by the total number of outcomes. This definition assum�s that all outcomes are equally likely. However, many experiments have outcomes that are not equally likely. For instance, a coin may be biased so that it comes up heads twice as often as tails. Similarly, the likelihood that the input of a linear search is a particular element in a list, or is not in the list, depends on how the input is generated. How can we model the likelihood of events in such situations? In this section we will show how to define probabilities of outcomes to study probabilities of experiments where outcomes may not be equally likely. Suppose that a fair coin is flipped four times, and the first time it comes up heads. Given this information, what is the probability that heads comes up three times? To answer this and similar questions, we will introduce the concept of conditional probability. Does knowing that the first flip comes up heads change the probability that heads comes up three times? If not, these two events are called independent, a concept studied later in this section. Many questions address a particular numerical value associated with the outcome of an experiment. For instance, when we flip a coin 100 times, what is the probability that exactly 40 heads appear? How many heads should we expect to appear? In this section we will intro­duce random variables, which are functions that associate numerical values to the outcomes of experiments. HISTORICAL NOTE The Chevalier de Mere was a French nobleman, a famous gambler, and a bon vivant. He was successful at making bets with odds slightly greater than 112 (such as having at least one six come up in four tosses of a die). His correspondence with Pascal asking about the probability of having at least one double six come up when a pair of dice is rolled 24 times led to the development of probability theory. According to one account, Pascal wrote to Fermat about the Chevalier saying something like "He's a good guy but, alas, he's no mathematician." 6-9 6.2 Probability Theory 401 Assigning Probabilities Let S be the sample space of an experiment with a finite or countable number of outcomes. We assign a probability p(s) to each outcome s. We require that two conditions be met: and (i) O.:s p(s) .:s I for each s E S (ii) L p(s) = 1. SES Condition (i) states that the probability of each outcome is a nonnegative real number no greater than 1. Condition (ii) states that the sum of the probabilities of all possible outcomes should be 1; that is, when we do the experiment, it is a certainty that one of these outcomes occurs. (Note that when the sample space is infinite, LSES p(s) is a convergent infinite series.) This is a generalization of Laplace's definition in which each of n outcomes is assigned a probability of l/n. Indeed, conditions (i) and (ii) are met when Laplace's definition of probabilities of equally likely outcomes is used and S is finite. (See Exercise 4.) are and Note that when there are n possible outcomes, XI, X2, ••• ,Xn, the two conditions to be met (i) O.:s p(x;) .:s I for i = I, 2, ... , n n (ii) LP(x;) = 1. ;=1 The function P from the set of all outcomes of the sample space S is called a probability distribution. . To model an experiment, the probability p(s) assigned to an outcome s should equal the limit of the number of times s occurs divided by the number of times the experiment is performed, as this number grows without bound. (We will assume that all experiments discussed have outcomes that are predictable on the average, so that this limit exists. We also assume that the outcomes of successive trials of an experiment do not depend on past results.) Remark: We will not discuss probabilities of events when the set of outcomes is not finite or countable, such as when the outcome of an experiment can be any real number. In such cases, integral calculus is usually required for the study of the probabilities of events. We can model experiments in which outcomes are either equally likely or not equally likely by choosing the appropriate function p(s), as Example 1 illustrates. EXAM PLE 1 What probabilities should we assign to the outcomes H (heads) and T (tails) when a fair coin is flipped? What probabilities should be assigned to these outcomes when the coin is biased so that heads comes up twice as often as tails? Solution: For a fair coin, the probability that heads comes up when the coin is flipped equals the probability that tails comes up, so the outcomes are equally likely. Consequently, we assign the probability 1/2 to each of the two possible outcomes, that is, p(H) = p(T) = 1/2. 402 6 / Discrete Probability 6-10 DEFINITION 1 DEFINITION 2 For the biased coin we have p(H) = 2p(T). Because p(H) + p(T) = 1, it follows that 2p(T) + p(T) = 3p(T) = 1. We conclude that p(T) = 1/3 and p(H) = 2/3. Suppose that S is a set with n elements. The uniform distribution assigns the probability 1/ n to each element of S. We now define the probability of an event as the sum of the probabilities of the outcomes in this event. The probability of the event E is the sum of the�probabilities of the outcomes in E. That is, p(E) = I:>(s). seE (Note that when E is an infinite set, LseE p(s) is a convergent infinite series.) Note that when there are n outcomes in the event E, that is, if E = {ai, 02, ... , an}, then p(E) = L7=1 p(aj). Note also that the uniform distribution assigns the same probability to an event that Laplace's original definition of probability assigns to this event. The experiment of selecting an element from a sample space with a uniform distribution is called selecting an element of S at random. EXAMPLE 2 Suppose that a die is biased (or loaded) so that 3 appears twice as often as each other number but that the other five outcomes are equally likely. What is the probability that an odd number appears when we roll this die? Solution: We want to find the probability of the event E = {I, 3, 5}. By Exercise 2 at the end of this section, we have p(l) = p(2) = p(4) = p(5) = p(6) = 1/7; p(3) = 2/7. It follows that p(E) = p(l) + p(3) + p(5) = 1/7 + 2/7 + 1/7 = 4/7. 6-11 6.2 Probability Theory 403 When events are equally likely and there are a finite number of possible outcomes, the definition of the probability of an event given in this section (Definition 2) agrees with Laplace's definition (Definition 1 of Section 6.1). To see this, suppose that there are n equally likely outcomes; each possible outcome has probability 1/ n, because the sum of their probabilities is 1. Suppose the event E contains m outcomes. According to Definition 2, m 1 m pee) = L -= -:, i=1 n n Because lEI = m and lSI = n, it follows that m lEI p(E) = -= -. niSI This is Laplace's definition of the probability of the event E. Combinations of Events The formulae for probabilities of combinations of events in Section 6.1 continue to hold when we use Definition 2 to define the probability of an event. For example, Theorem 1 of Section 6.1 asserts that pee) = 1 --pee), where E is the complementary event of the event E. This equality also holds when Definition 2 is used. To see this, note that because the sum of the probabilities of the n possible outcomes is 1, and each outcome is either in E or in E, but not in both, we have LP(S) = 1 = pee) + pee). S€s Hence, pee) = 1 --pee). Under Laplace's definition, by Theorem 2 in Section 6.1, we have whenever E I and E2 are events in a sample space S. This also holds when we define the probability of an event as we do in this section. To see this, note that p(EI U E2) is the sum of the probabilities of the outcomes in EI U E2. When an outcome x is in one, but not both, of EI and E2, p(x) occurs in exactly one of the sums for peed and P(E2)' When an outcome x is in both EI and E2, p(x) occurs in the sum for P(EI), in the sum for P(E2), and in the sum for p(EI n E2), so it occurs 1 + 1 -- 1 = 1 time on the right-hand side. Consequently, the left-hand side and right-hand side are equal. The probability of the union of pairwise disjoint events can be found using Theorem 1. 404 6 / Discrete Probability 6-12 THEOREM l unks� DEFINITION 3 EXAM PLE 3 Exam=� If E1, E2, ••• is a sequence of pairwise disjoint events in a sample space S, then (Note that this theorem applies when the sequence E1, E2, ••• consists ofa finite number or a countably infinite number of pairwise disjoint events.) We leave the proof of Theorem 1 to the reader (see Exercises 36 and 37). Conditional Probability Suppose that we flip a coin three times, and all eight possibilities are equally likely. Moreover, suppose we know that the event F, that the first flip comes up tails, occurs. Given this information, what is the probability of the event E, that an odd number of tails appears? Because the first flip comes up tails, there are only four possible outcomes: TTT, TTH, THT, and THH, where Hand T represent heads and tails, respectively. An odd number of tails appears only for the outcomes TTT and THH. Because the eight outcomes have equal probability, each of the four possible' outcomes, given that F occurs, should also have an equal probability of 1/4. This suggests that we should assign the probability of 2/4 = 1/2 to E, given that F occurs. This probability is called the conditional probability of E given F. In general, to find the conditional probability of E given F, we use F as the sample space. For an outcome from E to occur, this outcome must also belong to E n F. With this motivation, we make Definition 3. Let E and F be events with p(F) > O. The conditional probability of E given F, denoted by peE I F), is defined as (E I F) = peE n F) . p p(F) A bit string oflength four is generated at random so that each ofthe 16 bit strings oflength four is equally likely. What is the probability that it contains at least two consecutive Os, given that its first bit is a O? (We assume that 0 bits and 1 bits are equally likely.) Solution: Let E be the event that a bit string oflength four contains at least two consecutive Os, and let F be the event that the first bit of a bit string of length four is a o. The probability that a bit string of length four has at least two consecutive Os, given that its first bit is a 0, equals (E I F) = peE n F) . p p(F) Because En F = {OOOO, 0001, 0010, 0011, 0100}, we see that peE n F) = 5/16. Because there are eight bit strings of length four that start with a 0, we have p(F) = 8/16 = 1/2. 6-13 Consequently, 5/16 5 peE I F) = -= -. 1/2 8 6.2 Probability Theory 405 EXAM PLE 4 What is the conditional probability that a family with two children has two boys, given they have at least one boy? Assume that each of the possibilities BB, BG, GB, and GG is equally likely, where B represents a boy and G represents a girl. (Note that BG represents a family with an older boy and a younger girl while GB represents a family with an older girl and a younger boy.) URksi:J Solution: Let E be the event that a family with two children has two boys, and let F be the event that a family with two children has at least one boy. It follows that E = {BB}, F = {BB, BG, GB}, and E n F = {BB}. Because the four possibilities are equally likely, it follows that p(F) = 3/4 and peE n F) = 1/4. We conclude that p(EnF) 1/4 1 peE I F) = p(F) = 3/4 = 3' Independence Suppose a coin is flipped three times, as described in the introduction to our discussion of conditional probability. Does knowing that the first flip comes up tails (event F) alter the probability that tails comes up an odd number of times (event E)? In other words, is it the case that peE I F) = peE)? This equality is valid for the events E and F, because peE I F) = 1/2 and peE) = 1/2. Because this equality holds, we say that E and F are independent events. When two events are independent, the occurrence of one of the events gives no information about the probability that the other event occurs. Because peE I F) = peE n F)/ p(F), asking whether peE I F) = peE) is the same as asking whether peE n F) = p(E)p(F). This leads to Definition 4. DEFINITION 4 The events E and F are independent if and only if peE n F) = p(E)p(F). EXAM PLES Extra� Examples� Suppose E is the event that a randomly generated bit string of length four begins with a 1 and F is the event that this bit string contains an even number of 1 s. Are E and F independent, if the 16 bit strings of length four are equally likely? Solution: There are eight bit strings of length four that begin with a one: 1000, 1001, 1010, 10 11, 1100, 1101, 1110, and 1111. There are also eight bit strings of length four that contain an even number of ones: 0000,0011, 0101, 0110, 1001, 1010, 1100, 1111. Because there are 16 bit strings of length four, it follows that peE) = p(F) = 8/16 = 1/2. Because E n F = {1111, 11 00, 10 10, 100 I}, we see that peE n F) = 4/16 = 1/4. 406 6 / Discrete Probability 6-14 Because p(E n F) = 1/4 = (l/2)(1/2) = p(E)p(F), we conclude that E and F are independent. Probability has many applications to genetics, as Examples 6 and 7 illustrate. EXAMPLE 6 Assume, as in Example 4, that each of the four ways a family can have two children is equally likely. Are the events E, that a family with two children has two boys, and F, that a family with two children has at least one boy, independent? Solution: Because E = {BB}, we have p(E) = 1/4. In Example 4 we showed that p(F) = 3/4 and that p(E n F) = 1/4. But p(E)p(F) = � . £ = k Therefore p(E n F) #-p(E)p(F), so the events E and F are not independent. .... EXAMPLE 7 Are the events E, that a family with three children has children of both sexes, and F, that this family has at most one boy, independent? Assume that the eight ways a family can have three children are equally likely. unkS� EXAMPLE 8 Solution: By assumption, each of the eight ways a family can have three children, BBB, BBG, BGB, BGG, GBB, GBG, GGB, and GGG, has a probability of 1/8. Because E = {BBG, BGB, BGG, GBB, GBG, GGB}, F = {BGG, GBG, GGB, GGG}, and E n F = {BGG, GBG, GGB}, it follows that p(E) = 6/8 = 3/4, p(F) = 4/8 = 1/2, and p(E n F) = 3/8. Because 3 1 3 p(E)p(F) = 4 . 2 = 8' it follows that p(E n F) = p(E)p(F), so E and F are independent. (This conclusion may seem surprising. Indeed, if we change the number of children, the conclusion may no longer hold. See Exercise 27 at the end of this section.) .... Bernoulli Trials and the Binomial Distribution Suppose that an experiment can have only two possible outcomes. For instance, when a bit is generated at random, the possible outcomes are 0 and 1. When a coin is flipped, the possible outcomes are heads and tails. Each performance of an experiment with two possible outcomes is called a Bernoulli trial, after James Bernoulli, who made important contributions to probability theory. In general, a possible outcome of a Bernoulli trial is called a success or a failure. If p is the probability of a success and q is the probability of a failure, it follows that p + q = 1. Many problems can be solved by determining the probability of k successes when an ex­periment consists of n mutually independent Bernoulli trials. (Bernoulli trials are mutually independent if the conditional probability of success on any given trial is p, given any infor­mation whatsoever about the outcomes of the other trials.) Consider Example 8. A coin is biased so that the probability of heads is 2/3. What is the probability that exactly four heads come up when the coin is flipped seven times, assuming that the flips are independent? Solution: There are 27 = 128 possible outcomes when a coin is flipped seven times. The number of ways four of the seven flips can be heads is C(7, 4). Because the seven flips are independent, 6-15 THEOREM 2 EXAM PLE 9 Eltlra� ElIamiJleS� 6.2 Probability Theory 407 the probability of each of these outcomes (four heads and three tails) is (2/3)4(1/3)3. Conse­ quently, the probability that exactly four heads appear is C(7,4)(2/3)\1/3)3 = 353./6 = ;tS07. <I Following the same reasoning as was used in Example S, we can find the probability of k successes in n independent Bernoulli trials. The probability of exactly k successes in n independent Bernoulli trials, with probability of success p and probability of failure q = 1 -p, is Proof: When n Bernoulli trials are carried out, the outcome is an n-tuple (11,12, ... , In), where Ii = S (for success) or Ii = F (for failure) for i = 1,2, ... , n. Because the n trials are indepen­ dent, the probability of each outcome of n trials consisting of k successes and n - k failures (in any order) is pkqn-k. Because there are C(n, k) n-tuples of S's and F's that contain k S's, the probability of k successes is <l We denote by b( k; n, p) the probability of k successes in n independent Bernoulli trials with probability of success p and probability of failure q = 1 -p. Considered as a function of k, we call this function the binomial distribution. Theorem 2 tells us thatb(k; n, p) = C(n, k)pkqn-k. Suppose that the probability that a 0 bit is generated is 0.9, that the probability that a 1 bit is generated is 0.1, and that bits are generated independently. What is the probability that exactly eight 0 bits are generated when 10 bits are generated? Solution: By Theorem 2, the probability that exactly eight 0 bits are generated is b(S; 10, 0.9) = C(IO, 8)(0.9)8(0.1)2 = 0.1937102445. JAMES BERNOULLI (\654-\705) James Bernoulli (also known as Jacob I), was born in Basel, Switzerland. He is one of the eight prominent mathematicians in the Bernoulli family (see Section 10.1 for the Bernoulli family tree of mathema ticians). Following his father's wish, James studied theology and entered the ministry. But contrary to the desires of his parents, he also studied mathematics and astronomy. He traveled throughout Europe from 1676 to 1682, learning about the latest discoveries in mathematics and the sciences. Upon returning to Basel in 1682, he founded a school for mathematics and the sciences. He was appointed professor of mathematics at the University of Basel in 1687, remaining in this position for the rest of his life. James Bernoulli is best known for the work Ars Conjectandi, published eight years after his death. In this work, he described the known results in probability theory and in enumeration, often providing alternative proofs of known results. This work also includes the application of probability theory to games of chance and his introduction ofthe theorem known as the law oflarge numbers. This law states that if E > 0, as n becomes arbitrarily large the probability approaches I that the fraction of times an event E occurs during n trials is within E of peE). 408 6 / Discrete Probability 6-16 DEFINITION 5 Note that the sum of the probabilities that there are k successes when n independent Bernoulli trials are carried out, for k = 0, 1,2, ... , n, equals n L C(n, k)pkqn-k = (p + qt = 1, k=O as should be the case. The first equality in this string of equalities is a consequence of the Binomial Theorem (see Section 5.4). The second equality follows because q = I -p. Random Variables Many problems are concerned with a numerical value associated with the outcome of an experi­ ment. For instance, we may be interested in the total number of one bits in a randomly generated string of 10 bits; or in the number of times tails come up when a coin is flipped 20 times. To study problems of this type we introduce the concept of a random variable. A random variable is a function from the sample space of an experiment to the set of real numbers. That is, a random variable assigns a real number to each possible outcome. Remark: Note that a random variable is a function. It is not a variable, and it is not random! EXAMPLE 10 Suppose that a coin is flipped three times. Let X (t) be the random variable that equals the number of heads that appear when t is the outcome. Then X(t) takes on the following value s: X(HHH) = 3, X(HHT) = X(HTH) = X(THH) = 2, X(TTH) = X(THT) = X(HTT) = 1, X(TTT) = O. D EFINITI 0 N 6 The distribution of a random variable X on a sample space S is the set of pairs (r, p( X = r)) for all r E X(S), where p(X = r) is the probability that X takes the value r. A distribution is usually described by specifying p( X = r) for each rEX (S). EXAMPLE 11 Because each of the eight possible outcomes when three coins are flipped has probability 1/8, the distribution of the random variable X(t) in Example 10 is given by P(X = 3) = 1/8, P(X = 2) = 3/8, P(X = 1) = 3/8, P(X = 0) = 1/8. � EXAMPLE 12 Let X be the sum of the numbers that appear when a pair of dice is rolled. What are the values ofthis random variable for the 36 possible outcomes (i, j), where i and j are the num­ bers that appear on the first die and the second die, respectively, when these two dice are rolled? 6-/7 6.2 Probability Theory 409 Solution: The random variable X takes on the following values: X«I, 1» = 2, X«(1, 2» = X«2, 1» = 3, X«(1, 3» = X«2, 2» = X«3, 1» = 4, X«(l, 4» = X«2, 3» = X«3, 2» = X«4, 1» = 5, X«(1, 5» = X«2, 4» = X«3, 3» = X«4, 2» = X«5, I» = 6, X«(1, 6» = X«2, 5» = X«3, 4» = X«4, 3» = X«5, 2» = X«6, I» = 7, X«2, 6» = X«3, 5» = X«4, 4» = X«5, 3» = X«6, 2» = 8, X«3, 6» = X«4, 5» = X«5, 4» = X«6, 3» = 9, X«4, 6» = X«5, 5» = X«6, 4» = 10, X«5, 6» = X«6, 5» = 11, X«6, 6» = 12. We will continue our study of random variables in Section 6.4, where we will show how they can be used in a variety of applications. The Birthday Problem A famous puzzle asks for the smallest number of people needed in a room so that it is more likely than not that at least two of them have the same day of the year as their birthday. Most people find the answer, which we determine in Example 13, to be surprisingly small. After we solve this famous problem, we will show how similar reasoning can be adapted to solve a question about hashing functions. EXAMPLE 13 The Birthday Problem What is the minimum number of people who need to be in a room so � that the probability that at least two of them have the same birthday is greater than 112? UnkS� Solution: First, we state some assumptions. We assume that the birthdays of the people in the room are independent. Furthermore, we assume that each birthday is equally likely and that there are 366 days in the year. (In reality, more people are born on some days of the year than others, such as days nine months after some holidays including New Year's Eve, and only leap years have 366 days.) To find the probability that at least two of n people in a room have the same birthday, we first calculate the probability Pn that these people all have different birthdays. Then, the probability that at least two people have the same birthday is 1-Pn. To compute Pn, we consider the birthdays of the n people in some fixed order. Imagine them entering the room one at a time; we will compute the probability that each successive person entering the room has a birthday different from those of the people already in the room. The birthday of the first person certainly does not match the birthday of someone already in the room. The probability that the birthday of the second person is different from that ofthe first person is 365/366 because the second person has a different birthday when he or she was born on one of the 365 days of the year other than the day the first person was born. (The assumption that it is equally likely for someone to be born on any of the 366 days ofthe year enters into this and subsequent steps.) The probability that the third person has a birthday different from both the birthdays of the first and second people given that these two people have different birthdays is 364/366. In general, the probability that the jth person, with 2 :s: j :s: 366, has a birthday different from the 410 6/ Discrete Probability 6-18 birthdays of the j - 1 people already in the room given that these j - 1 people have different birthdays is 366 -(j -1) 367 - j 366 366 Because we have assumed that the birthdays of the people in the room are independent, we can conclude that the probability that the n people in the room have different birthdays is 365 364 363 367 -n Pn = 366 366 366 . . . 366 It follows that the probability that among n people there are at least two people with the same birthday is 365 364 363 367 -n I -Pn = 1 -366 366 366 . . . 366 To determine the minimum number of people in the room so that the probability that at least two of them have the same birthday is greater than 1/2, we use the formula we have found for 1 -Pn to compute it for increasing values of n until it becomes greater than 1/2. (There are more sophisticated approaches using calculus that can eliminate this computation, but we will not use them here.) After considerable computation we find that for n = 22, 1 -Pn � 0.475, while for n = 23, I -Pn � 0.506. Consequently, the minimum number of people needed so that the probability that at least two people have the same birthday is greater than 1/2 is 23. <III The solution to the birthday problem leads to the solution of the question in Example 14 about hashing functions. EXAMPLE 14 Probability of a Collision in Hashing Functions Recall from Section 3.4 that a hashing function h (k) is a mapping of the keys (of the records that are to be stored in a database) to storage locations. Hashing functions map a large universe of keys (such as the approximately 300 million Social Security numbers in the United States) to a much smaller set of storage locations. A good hashing function yields few collisions, which are mappings of two different keys to the same memory location, when relatively few of the records are in play in a given application. What is the probability that no two keys are mapped to the same location by a hashing function, or, in other words, that there are no collisions? Solution: To calculate this probabilit y, we assume that the probability that a randomly selected key is mapped to a location is 1/ m, where m is the number of available locations, that is, the hashing function distributes keys uniformly. (In practice, hashing functions may not satisfy this assumption. However, for a good hashing function, this assumption should be close to correct.) Furthermore, we assume that the keys of the records selected have an equal probability to be any of the elements of the key universe and that these keys are independeritly selected. Suppose that the keys are k\, k2, ... , kn• When we add the second record, the probability that it is mapped to a location different from the location of the first record, that h(k2) =j:. h(kd, is (m -1)/ m because there are m - 1 free locations after the first record has been placed. The probability that the third record is mapped to a free location after the first and second records have been placed without a collision is (m -2)/m. In general, the probability that the jth record is mapped to a free location after the first j - 1 records have been mapped to locations h(kd, h(k2), ... , h(kj_\) without collisions is (m -(j - 1»/m because j - 1 of the m locations are taken. 6-19 6.2 Probability Theory 411 Because the keys are independent, the probability that all n keys are mapped to different locations is m-l m-2 m-n+l Pn= --·--· .... m m m It follows that the probability that there is at least one collision, that is, at least two keys are mapped to the same location, is m-l m-2 m-n+l I-Pn = 1- --.--..... ----m m m Techniques from calculus can be used to find the smallest value of n given a value of m such that the probability of a collision is greater than a particular threshold. It can be shown that the smallest integer n such that the probability of a collision is greater than 1/2 is approximately n = 1.177Jm. For example, when m = 1,000,000, the smallest integer n such that the probability of a collision is greater than 1/2 is 1178. .... Monte Carlo Algorithms The algorithms discussed so far in this book are all deterministic. That is, each algorithm always proceeds in the same way whenever given the same input. However, there are many situations where we would like an algorithm to make a random choice at one or more steps. Such a situation arises when a deterministic algorithm would have to go through a huge number, or even an unknown number, of possible cases. Algorithms that make random choices at one or more steps are called probabilistic algorithms. We will discuss a particular class of probabilistic algorithms in this section, namely, Monte Carlo algorithms, for decision problems. Monte Carlo algorithms always produce answers to problems, but a small probability remains that these answers may be incorrect. However, the probability that the answer is incorrect decreases rapidly when the algorithm carries out sufficient computation. Decision problems have either "true" or "false" as their answer. The designation "Monte Carlo" is a reference to the famous casino in Monaco; the use of randomness and the repetitive processes in these algorithms make them similar to some gambling games. This name was introduced by the inventors of Monte Carlo methods, including Stan Ulam, Enrico Fermi, and John von Neumann. A Monte Carlo algorithm for a decision problem uses a sequence of tests. The probability that the algorithm answers the decision problem correctly increases as more tests are carried out. At each step of the algorithm, possible responses are "true," which means that the answer is "true" and no additional iterations are needed, or "unknown," which means that the answer could be either "true" or "false." After running all the iterations in such an algorithm, the final answer produced is "true" if at least one iteration yields the answer "true," and the answer is "false" if every iteration yields the answer "unknown." If the correct answer is "false," then the algorithm answers "false," because every iteration will yield "unknown." However, if the correct answer is ''true,'' then the algorithm could answer either "true" or "false," because it may be possible that each iteration produced the response "unknown" even though the correct response was "true." We will show that this possibility becomes extremely unlikely as the number of tests increases. Suppose that P is the probability that the response of a test is "true," given that the answer is ''true.'' It follows that 1-P is the probability that the response is ''unknown,'' gjven that the answer is "true." Because the algorithm answers "false" when all n iterations yield the answer ''unknown'' and the iterations perform independent tests, the probability of error is (1 _ P )n . When P =f:. 0, this probability approaches 0 as the number of tests increases. Consequently, the probability that the algorithm answers ''true'' when the answer is "true" approaches 1. 412 6/ Discrete Probability 6-20 EXAMPLE 15 Quality Control (This example is adapted from [AhUI95].) Suppose that a manufacturer orders processor chips in batches of size n, where n is a positive integer. The chip maker has tested only some of these batches to make sure that all the chips in the batch are good (replacing any bad chips found during testing with good ones). In previously untested batches, the probability that a particular chip is bad has been observed to be 0.1 when random testing is done. The PC manufacturer wants to decide whether all the chips in a batch are good. To do this, the PC manufacturer can test each chip in a batch to see whether it is good. However, this requires n tests. Assuming that each test can be carried out in constant time, these tests require O(n) seconds. Can the PC manufacturer test whether a batch of chips has been tested by the chip maker using less time? Solution: We can use a Monte Carlo algorithm to determine whether a batch of chips has been tested by the chip maker as long as we are willing to accept some probability of error. The algorithm is set up to answer the question: "Has this batch of chips not been tested by the chip maker?" It proceeds by successively selecting chips at random from the batch and testing them one by one. When a bad chip is encountered, the algorithm answers "true" and stops. If a tested chip is good, the algorithm answers "unknown" and goes on to the next chip. After the algorithm has tested a specified number of chips, say k chips, without getting an answer of "true," the algorithm terminates with the answer "false"; that is, the algorithm concludes that the batch is good, that is, that the chip maker has tested all the chips in the batch. The only way for this algorithm to answer incorrectly is for it to conclude that an untested batch of chips has been tested by the chip maker. The probability that a chip is good, but that it came from an untested batch, is 1 -0.1 = 0.9. Because the events of testing different chips from a batch are independent, the probability that all k steps of the algorithm produce the answer "unknown," given that the batch of chips is untested, is 0.9k• By taking k large enough, we can make this probability as small as we like. For example, by testing 66 chips, the probability that the algorithm decides a batch has been tested by the chip maker is 0.966, which is less than 0.00l. That is, the probability is less than 1 in 1000 that the algorithm has answered incorrectl y. Note that this probability is independent of n, the number of chips in a batch. That is, the Monte Carlo algorithm uses a constant number, or 0 (1), tests and requires 0(1) seconds, no matter how many chips are in a batch. As long as the PC manufacturer can live with an error rate ofless than 1 in 1000, the Monte Carlo algorithm will save the PC manufacturer a lot of testing. If a smaller error rate is needed, the PC manufacturer can test more chips in each batch; the reader can verify that l32 tests lower the error rate to less than 1 in 1,000,000. � EXAMPLE 16 Probabilistic Primality Testing In Chapter 3 we remarked that a composite integer, that is, an integer that is not prime, passes Miller's test (see the preamble to Exercise 30 in Section 3.7) for fewer than n/4 bases b with 1 < b < n. This observation is the basis for a Monte Carlo algorithm to determine whether a positive integer is prime. Because large primes play an essential role in public-key cryptography (see Section 3.7), being able to generate large primes quickly has become extremely important. The goal of the algorithm is to decide the question "Is n composite?" Given an integer n, we select an integer b at random with 1 < b < n and determine whether n passes Miller's test to the base b. If n fails the test, the answer is "true" because n must be composite, and the algorithm ends. Otherwise, we repeat the test k times, where k is an integer. Each time we select a random integer b and determine whether n passes Miller's test to the base b. Ifthe answer is "unknown" at each step, the algorithm answers "false," that is, it says that n is not composite, so that it is prime. The only possibility for the algorithm to return an incorrect answer occurs when n is composite, and the answer "unknown" is the output at each of the k iterations. The probability that a composite integer n passes Miller's test for a randomly selected base b is less than 1/4. Because the integer b with 1 < b < n is selected at random at each iteration and these iterations 6-21 THEOREM 3 THEOREM 4 6.2 Probability Theory 413 are independent, the probability that n is composite but the algorithm responds that n is prime is less than (lj4)k. By taking k to be sufficiently large, we can make this probability extremely small. For example, with 10 iterations, the probability that the algorithm decides that n is prime when it really is composite is less than 1 in 1,000,000. With 30 iterations, this probability drops to less than 1 in 1018, an extremely unlikely event. To generate large primes, say with 200 digits, we randomly choose an integer n with 200 digits and run this algorithm, with 30 iterations. If the algorithm decides that n is prime, we can use it as one of the two primes used in an encryption key for the RSA cryptosystem. If n is actually composite and is used as part of the key, the procedures used to decrypt mes sages will not produce the original encrypted message. The key is then discarded and two new possible primes are used. <III The Probabilistic Method We discussed existence proofs in Chapter 1 and illustrated the difference between constructive existence proofs and nonconstructive existence proofs. The probabilistic method, introduced by Paul Erdos and Alfred Renyi, is a powerful technique that can be used to create nonconstructive existence proofs. To use the probabilistic method to prove results about a set S, such as the existence of an element in S with a specified property, we assign probabilities to the elements of S. We then use methods from probability theory to prove results about the elements of S. In particular, we can show that an element with a specified property exists by showing that the probability an element XES has this property is positive. The probabilistic method is based on the equivalent statement in Theorem 3. THE PROBABILI STIC METHOD If the probability that an element ofa set S does not have a particular property is less than 1, there exists an element in S with this property. An existence proof based on the probabilistic method is nonconstructive because it does not find a particular element with the desired property. We illustrate the power of the probabilistic method by finding a lower bound for the Ramsey number R(k, k). Recall from Section 5.2 that R(k, k) equals the minimum number of people at a party needed to ensure that there are at least k mutual friends or k mutual enemies (assuming that any two people are friends or enemies). If k is an integer with k ::: 2, then R(k, k) ::: 2k/2. Proof: We note thatthe theorem holds fork = 2 andk = 3 because R(2, 2) = 2 andR(3, 3) = 6, as was shown in Section 5.2. Now suppose that k ::: 4. We will use the probabilistic method to show that if there are fewer than 2k/2 people at a party, it is possible that no k of them are mutual friends or mutual enemies. This will show that R(k, k) is at least 2k/2. To use the probabilistic method, we assume that it is equally likely for two people to be friends or enemies. (Note that this assumption does not have to be realistic.) Suppose there are n people at the party. It follows that there are (�) different sets of k people at this party, which we list as SI, S2, ... , Sm. Let Ei be the event that all k people in Si are either mutual friends 414 6/ Discrete Probability 6-22 Exercises or mutual enemies. The probability that there are either k mutual friends or k mutual enemies among the n people equals P(U�I Ei)' According to our assumption it is equally likely for two people to be friends or enemies. The probability that two people are friends equals the probability that they are enemies; both probabilities equal 1/2. Furthermore, there are (�) = k(k-l)/2 pairs of people in S; because there are k people in S;. Hence, the probability that all k people in S; are mutual friends and the probability that all k people in S; are mutual enemies both equal (1/2)k(k-I)/2. It follows that peE;) = 2(l/2)k(k-I)/2. The probability that there are either k mutual friends or k mutual enemies in the group of n people equals P(U�I E;). Using Boole's Inequality (Exercise 15), it follows that (G) ) G) (n) (l)k(k-I)/2 p UEi .:s LP(E;) = ·2-;=1 ;=1 k 2 By Exercise 17 in Section 5.4, we have G) .:s nk /2k-l• Hence, (n)2(�)k(k-I)/2 < �2(�)k(k-I)/2 k 2 -2k-1 2 Now if n < 2k/2, we have -2 - < --2 - = 2 (J) < 1 nk (1 )k(k-I)/2 2k(k/2) (1 )k(k-I)/2 2-k 2 2k-1 2 2k-1 2 - where the last step follows because k ::: 4. We can now conclude that P(U�I Ei) < I when k ::: 4 .. Hence, the probability of the complem entary event, that there is no set of either k mutual friends or mutual enemies at the party, is greater than O. It follows that if n < 2k/2, there is at least one set such that no subset of k people are mutual friends or mutual enemies. <I 1. What probability should be assigned to the outcome of heads when a biased coin is tossed, ifheads is three times as likely to come up as tails? What probability should be assigned to the outcome of tails? on the second die is 2/7. Other outcomes for each die appear with probability 1/7. What is the probability of7 appearing as the sum of the numbers when the two dice are rolled? 2. Find the probability of each outcome when a loaded die is rolled, if a 3 is twice as likely to appear as each of the other five numbers on the die. 3. Find the probability of each outcome when a biased die is rolled, if rolling a 2 or rolling a 4 is three times as likely as rolling each of the other four numbers on the die and it is equally likely to roll a 2 or a 4. 4. Show that conditions (i) and (ii) are met under Laplace's definition of probability, when outcomes are equally likely. 5. A pair of dice is loaded. The probability that a 4 appears on the first die is 2/7, and the probability that a 3 appears 6. What is the probability ofthese events when we randomly select a permutation of {I, 2, 3)? a) 1 precedes 3. b) 3 precedes 1. c) 3 precedes 1 and 3 precedes 2. 7. What is the probability ofthese events when we randomly select a permutation of {I, 2, 3, 4)? a) 1 precedes 4. b) 4 precedes 1. c) 4 precedes 1 and 4 precedes 2. d) 4 precedes 1, 4 precedes 2, and 4 precedes 3. e) 4 precedes 3 and 2 precedes 1. 6-23 8. What is the probability of these events when we randomly select a permutation of {I, 2, ... , n} where n 0:: 4? a) 1 precedes 2. b) 2 precedes 1. c) I immediately precedes 2. d) n precedes 1 and n -1 precedes 2. e) n precedes 1 and n precedes 2. 9. What is the probability of these events when we randomly select a permutation of the 26 lowercase letters of the English alphabet? a) The permutation consists of the letters in reverse alphabetic order. b) z is the first letter of the permutation. c) z precedes a in the permutation. d) a immediately precedes z in the permutation. e) a immediately precedes m, which immediately pre­ cedes z in the permutation. 1) m, n, and 0 are in their original places in the permutation. 10. What is the probability ofthese events when we randomly select a permutation of the 26 lowercase letters of the English alphabet? a) The first 13 letters ofthe permutation are in alphabet­ icalorder. b) a is the first letter of the permutation and z is the last letter. c) a and z are next to each other in the permutation. d) a and b are not next to each other in the permutation. e) a and z are separated by at least 23 letters in the permutation. 1) z precedes both a and b in the permutation. 11. Suppose that E and F are events such that pee) = 0.7 and p(F) = 0.5. Showthatp(E U F) 0:: 0.7 andp(E n F) 0:: 0.2. 12. Suppose that E and F are events such that pee) = 0.8 and p(F) = 0.6. Show that p(E U F) 0:: 0.8 andp(E n F) 0:: 004. 13. Show that if E and F are events, then peE n F) 0:: pee) + p(F) -1. This is known as Bonferroni's Inequality. 14. Use mathematical induction to prove the following gen­ eralization of Bonferroni 's Inequality: p(EI n E2 n··· n En) 0:: p(EI) + P(E2) + ... + peen) -(n -1), where E I, E2, ... , En are n events. 15. Show that if E I, E2, ... , En are events from a finite sam­ ple space, then p(EI U E2 U .. · U En) ::::: p(EI) + p(E2) + ... + peen). This is known as Boole's Inequality. 16. Show that if E and F are independent events, then E and F are also independent events. 6.2 Probability Theory 415 17. If E and F are independent events, prove or disprove that E and F are necessarily independent events. In Exercises 18, 20, and 21 assume that the year has 366 days and all birthdays are equally likely. In Exercise 19 assume it is equally likely that a person is born in any given month of the year. 18. a) What is the probability that two people chosen at ran­ dom were born on the same day of the week? b) What is the probability that in a group of n people chosen at random, there are at least two born on the same day of the week? c) How many people chosen at random are needed to make the probability greater than 112 that there are at least two people born on the same day of the week? 19. a) What is the probability that two people chosen at ran­ dom were born during the same month of the year? b) What is the probability that in a group of n people chosen at random, there are at least two born in the same month of the year? c) How many people chosen at random are needed to make the probability greater than 1/2 that there are at least two people born in the same month of the year? 20. Find the smallest number of people you need to choose at random so that the probability that at least one of them has a birthday today exceeds 112. 21. Find the smallest number of people you need to choose at random so that the probability that at least two of them were both born on April 1 exceeds 112. *22. February 29 occurs only in leap years. Years divisible by 4, but not by 100, are always leap years. Years divisible by 100, but not by 400, are not leap years, but years divisible by 400 are leap years. a) What probability distribution for birthdays should be used to reflect how often February 29 occurs? b) Using the probability distribution from part (a), what is the probability that in a group of n people at least two have the same birthday? 23. What is the conditional probability that exactly four heads appear when a fair coin is flipped five times, given that the first flip came up heads? 24. What is the conditional probability that exactly four heads appear when a fair coin is flipped five times, given that the first flip came up tails? 25. What is the conditional probability that a randomly gen­ erated bit string of length four contains at least two con­ secutive Os, given that the first bit is a I? (Assume the probabilities of a 0 and a 1 are the same.) 26. Let E be the event that a randomly generated bit string of length three contains an odd number of Is, and let F be the event that the string starts with 1. Are E and F independent? 27. Let E and F be the events that a family ofn children has children of both sexes and has at most one boy, respec­ tively. Are E and F independent if a) n = 2? b) n = 4? c) n = 5? 416 6/ Discrete Probability 28. Assume that the probability a child is a boy is 0.51 and that the sexes of children born into a family are indepen­ dent. What is the probability that a family of five children has a) exactly three boys? b) at least one boy? c) at least one girl? d) all children of the same sex? 29. A group of six people play the game of "odd person out" to determine who will buy refreshments. Each person flips a fair coin. If there is a person whose outcome is not the same as that of any other member of the group, this person has to buy the refreshments. What is the probability that there is an odd person out after the coins are flipped once? 30. Find the probability that a randomly generated bit string of length 10 does not contain a 0 if bits are independent and if a) a 0 bit and a I bit are equally likely. b) the probability that a bit is a I is 0.6. c) the probability that the ith bit is a I is 1/2; for i=I,2,3, ... ,IO. 31. Find the probability that a family with five children does not have a boy, if the sexes of children are independent and if a) a boy and a girl are equally likely. b) the probability ofa boy is 0.51. c) the probability that the ith child is a boy is 0.51 - (i /100). 32. Find the probability that a randomly generated bit string oflength 10 begins with a I or ends with a 00 for the same conditions as in parts (a), (b), and (c) of Exercise 30, if bits are generated independently. 33. Find the probability that the first child of a family with five children is a boy or that the last two children of the family are girls, for the same conditions as in parts (a), (b), and (c) of Exercise 31. 34. Find each of the following probabilities when n indepen­ dent Bernoulli trials are carried out with probability of success p. a) the probability of no successes b) the probability of at least one success c) the probability of at most one success d) the probability of at least two successes 35. Find each of the following probabilities when n indepen­ dent Bernoulli trials are carried out with probability of success p. a) the probability of no failures b) the probability of at least one failure c) the probability of at most one failure d) the probability of at least two failures 36. Use mathematical induction to prove that if E I, E2, ... , En is a sequence of n pairwise disjoint events in a sample space S, where n is a positive integer, then p(U7=1 E;) = 2:7=1 pee;). *37. (Requires calculus) Show that if E I, E2, ••• is an infinite 6-24 sequence of pairwise disjoint events in a sample space S, then P(U:I E;) = 2::1 pee;). [Hint: Use Exercise 36 and take limits.] 38. A pair of dice is rolled in a remote location and when you ask an honest observer whether at least one die came up six, this honest observer answers in the affirmative. a) What is the probability that the sum of the numbers that came up on the two dice is seven, given the infor­ mation provided by the honest observer? b) Suppose that the honest observer tells us that at least one die came up five. What is the probability the sum ofthe numbers that came up on the dice is seven, given this information? **39. This exercise employs the probabilistic method to prove a result about round-robin tournaments. In a round­ robin tournament with m players, every two players play one game in which one player wins and the other loses. We want to find conditions on positive integers m and k with k < m such that it is possible for the outcomes of the tournament to have the property that for every set of k players, there is a player who beats every member in this set. So that we can use probabilistic reasoning to draw conclusions about round-robin tournaments, we as­ sume that when two players compete it is equally likely that either player wins the game and we assume that the outcomes of different games are independent. Let E be the event that for every set S with k players, where k is a positive integer less than m, there is a player who has beaten all k players in S. a) Show that peE) :s 2:�21 p(Fj), where Fj is the event that there is no player who beats all k players from the jth set in a list of the (7) sets of k players. b) Show that the probability of Fj is (1_2-k)m-k. c) Conclude from parts (a) and (b) that p(E):S (7)(1 -2-k)m-k and, therefore, that there must be a tournament with the described property if (7)C1- 2-k)m-k < 1. d) Use part (c) to find values of m such that there is a tournament with m players such that for every set S of two players, there is a player who has beaten both players in S. Repeat for sets of three players. *40. Devise a Monte Carlo algorithm that determines whether a permutation ofthe integers I through n has already been sorted (that is, it is in increasing order), or instead, is a ran­ dom permutation. A step of the algorithm should answer "true" if it determines the list is not sorted and "unknown" otherwise. After k steps, the algorithm decides that the in­ tegers are sorted ifthe answer is "unknown" in each step. Show that as the number of steps increases, the proba­ bility that the algorithm produces an incorrect answer is extremely small. [Hint: For each step, test whether cer­ tain elements are in the correct order. Make sure these tests are independent.] 41. Use pseudocode to write out the probabilistic primality test described in Example 16. 6-25 6.3 Bayes' Theorem 417 6.3 Bayes' Theorem EXAMPLE l Extra� Examples� Introduction There are many times when we want to assess the probability that a particular event occurs on the basis of partial evidence. For example, suppose we know the percentage of people who have a particular disease for which there is a very accurate diagnostic test. People who test positive for this disease would like to know the likelihood that they actually have the disease. In this section we introduce a result that can be used to determine this probability, namely, the probability that a person has the disease given that they test positive for it. To use this result, we will need to know the percentage of people who do not have the disease but test positive for it and the percentage of people who have the disease but test negative for it. Similarly, suppose we know the percentage of incoming e-mail messages that are spam. We will see that we can determine the likelihood that an incoming e-mail message is spam using the occurrence of words in the message . To determine this likelihood, we need to know the percentage of incoming messages that are spam, the percentage of spam messages in which each of these words occurs, and the percentage of messages that are not spam in which each of these words occurs. The result that we can use to answer questions such as these is called Bayes' Theorem and dates back to the eighteenth century. In the past two decades, Bayes' Theorem has been extensively applied to estimate probabilities based on partial evidence in areas as diverse as medicine, law, machine learning, engineering, and software development. Bayes' Theorem We illustrate the idea behind Bayes' Theorem with an example that shows that when extra information is available, we can derive a more realistic estimate that a particular event occurs. That is, suppose we know p(F), the probability that an event F occurs, but we have knowledge that an event E occurs. Then the conditional probability that F occurs given that E occurs, p(F I E), is a more realistic estimate than p(F) that F occurs. In Example 1 we will see that we can find p(F I E) when we know p(F), peE I F), and peE I F). We have two boxes. The first contains two green balls and seven red balls; the second contains four green balls and three red balls. Bob selects a ball by first choosing one of the two boxes at random. He then selects one of the balls in this box at random. If Bob has selected a red ball, what is the probability that he selected a ball from the first box? Solution: Let E be the event that Bob has chosen a red ball; E is the event that Bob has chosen a green ball. Let F be the event that Bob has chosen a ball from the first box; F is the event that Bob has chosen a ball from the second box. We want to find p( F I E), the probability that the ball Bob selected came from the first box, given that it is red. By the definition of conditional probability, we have p(F I E) = p(F n E)/ peE). Can we use the information provided to determine both p(F n E) and peE) so that we can find p(F I E)? First, note that because the first box contains seven red balls out of a total of nine balls, we know that peE I F) = 7/9. Similarly, because the second box contains three red balls out of a total of seven balls, we know that peE I F) = 3/7. We assumed that Bob selects a box at random, so p(F) = p(F) = 1/2. Because peE I F) = peE n F)/p(F), it follows that peE n F) = peE I F)p(F) = � . � = -is [as we remarked earlier, this is one of the quantities 418 6/ Discrete Probability 6-26 we need to find to detennine p(F I E)]. Similarly, because p(E I F) = p(E n F)I p(F), it follows that p(E n F) = p(E I F)p(F) = � . 4 = k _ _ We can now find p(E). Note that E = (E n F) U (E n F), where E n F and E n Fare disjoint sets. (If x belongs to both E n F and E n F, then x belongs to both F and F, which is impossible.) It follows that - 7 3 49 27 76 38 p(E) = p(E n F) + p(E n F) = 18 + 14 = 126 + 126 = 264 = 63' We have now found both p(F n E) = 7/18 and p(E) = 38/63. We conclude that p(F n E) 7/18 49 p(F I E)= = --= -�0.645. p(E) 38/63 76 Before we had any extra infonnation, we assumed that the probability that Bob selected the first box was 112. However, with the extra infonnation that the ball selected at random is red, this probability has increased to approximately 0.645. That is, the probability that Bob selected a ball from the first box increased from 1/2, when no extra infonnation was available, to 0.645 once we knew that the ball selected was red. .... Using the same type of reasoning as in Example 1, we can find the conditional proba­ bility that an event F occurs, given that an event E has occurred, when we know p(E I F), p(E I F), and p(F). The result we can obtain is called Bayes' Theorem; it is named after Thomas Bayes, an eighteenth-cen tury British mathematician and minister who introduced this result. THEOREM 1 BAYES'THEOREM Suppose that E and F are events from a sample space S such that p(E) =1= 0 and p(F) =1= O. Then F E) _ p(E I F)p(F) p( I -p(E I F)p(F) + p(E I F)p(F) Proof: The definition of conditional probability tells us that p(F I E) = p(E n F)I p(E) and p(E I F) = p(E n F)I p(F). Therefore, p(E n F) = p(F I E)p(E) and p(E n F) = p(E I F)p(F). Equating these two expressions for p(E n F) shows that p(F I E)p(E) = p(E I F)p(F). Dividing both sides by p(E), we find that (F I E) = p(E I F)p(F). P p(E) To complete the proof, we show that p(E) = p(E I F)p(F) + p(E I F)p(F). First, note that E = Ens = E n (F U F) = (E n F) U (E n F). Furthennore, E n F and E n Fare disjoint, because if x E E n F and x E E n F, then x E F n F = 0. Consequently, p(E) = 6-27 linkS� 6.3 Bayes' Theorem 419 p(E n F) + p(E n F). We have already shown that p(E n F) = p(E I F)p(F). Moreover, we have p(E I F) = p(E n F)/ p(F), which shows that p(E n F) = p(E I F)p(F).1t follows that p(E) = p(E n F) + p(E n F) = p(E I F)p(F) + p(E I F)p(F). To complete the proof we insert this expression for p(E) into the equation p(F I E) = p(E I F)p(F)/ p(E). We have proved that p(F I E) = p(E I F)p(F) p(E I F)p(F) + p(E I F)p(F). <l Bayes' Theorem can be used to assess the probability that someone testing positive for a disease actually has this disease. The results obtained from Bayes' Theorem are often somewhat surprising, as Example 2 shows. EXAM PLE 2 Suppose that one person in 100,000 has a particular rare disease for which there is a fairly accurate diagnostic test. This test is correct 99% of the time when given to someone with the disease; it is correct 99.5% of the time when given to someone who does not have the disease. Given this information can we find (a) the probability that someone who tests positive for the disease has the disease? (b) the probability that someone who tests negative for the disease does not have the disease? Should someone who tests positive be very concerned that he or she has the disease? Solution: (a) Let F be the event that a person has the disease, and let E be the event that this person tests positive for the disease. We want to compute p(F I E). To use Bayes' Theorem to compute p(F I E) we need to find p(E I F), p(E I F), p(F), and p(F). We know that one person in 100,000hasthisdisease,so thatp(F) = 1/100,000 = 0.00001 and p(F) = 1 -0.00001 = 0.99999. Because someone who has the disease tests positive 99% ofthetime, we know that p(E I F) = 0.99; this is the probability ofa true positive, that someone with the disease tests positive. We also know that p(E I F) = 0.01; this is the probability ofa false negative, that someone who has the disease tests negative. Furthermore, because someone who does not have the disease tests negative 99.5% ofthe time, we know that p(E I F) = 0.995. This is the probability of a true negative, that someone without the disease tests negative. Finally, we know that p(E I F) = 0.005; this is the probability of a false positive, that someone without the disease tests positive. The probability that someone who tests positive for the disease actually has the disease is p(F I E). By Bayes' Theorem, we know that F E _ p(E I F)p(F) p( I ) -p(E I F)p(F) + p(E I F)p(F) (0.99)(0.00001) = (0.99)(0.00001) + (0.005)(0.99999) � 0.002. This means that only 0.2% of people who test positive for the disease actually have the disease. Because the disease is extremely rare, the number of false positives on the diagnostic test is far greater than the number of true positives, making the percentage of people who test positive 420 6 / Discrete Probability 6-28 THEOREM 2 linkS� who actually have the disease extremely small. People who test positive for the diseases should not be overly concerned that they actually have the disease. (b) The probability that someone who tests negative for the disease does not have the disease is p( F I E). By Bayes' Theorem, we know that - - p(E I F)p(F) p(F I E) = peE I F)p(F) + peE I F)p(F) (0.995)(0.99999) (0.995)(0.99999) + (0.01)(0.00001) � 0.9999999. Consequently, 99.99999% of the people who test negative really do not have the disease. <0lIl Note that in the statement of Bayes' Theorem, the events F and F are mutually exclusive and cover the entire sample space S (that is, F U F = S). We can extend Bayes' Theorem to any collection of mutually exclusive events that cover the entire sample space S, in the following way. GENER ALIZED BAYES' THEOREM Suppose that E is an event from a sample space S and that F1, F2, ... , Fn are mutually exclusive events such that U7=1 Fi = S. Assume that peE) =f. 0 and P(Fi) =f. 0 for i = 1,2, ... , n. Then F E _ peE I Fj)p(Fj) p( J I ) -"n (E I F,.) (F,.)' L.,,/=l P / P / We leave the proof of this generalized version of Bayes' Theorem as Exercise 17. THOMAS BAYES (1702-1761) was the son a minister in a religious sect known as the Nonconf ormists. This sect was considered heretical in eighteenth-century Great Britain. Because ofthe secrecy of the Nonconf ormists, little is known of Thomas Bayes' life. When Thomas was young, his family moved to London. Thomas was likely educated privately; Nonconformist children generally did not attend school. In 1719 Bayes entered the University of Edinburgh, where he studied logic and theology. He was ordained as a Nonconformist minister like his father and began his work as a minister assisting his father. In 1733 he became minister of the Presbyterian Chapel in Tunbridge Wells, southeast of London, where he remained minister until 1752. Bayes is best known for his essay on probability published in 1764, three years after his death. This essay was sent to the Royal Society by a friend who found it in the papers left behind when Bayes died. In the introduction to this essay, Bayes stated that his goal was to find a method that could measure the probability that an event happens, assuming that we know nothing about it, but that, under the same circumstances, it has happened a certain proportion of times. Bayes' conclusions were accepted by the great French mathematician Laplace but were later challenged by Boole, who questioned them in his book Laws a/Thought. Since then Bayes' techniques have been subject to controversy. Bayes also wrote an article that was published posthumously: "An Introduction to the Doctrine of Fluxions, and a Defense of the Mathematicians Against the Objections of the Author of The Analyst," which supported the logical foundations of calculus. Bayes was elected a Fellow of the Royal Society in 1742, with the support of important members of the Society, even though at that time he had no published mathematical works. Bayes' sole known publication during his lifetime was allegedly a mystical book entitled Divine Benevolence , discussing the original causation and ultimate purpose ofthe universe. Although the book is commonly attributed to Bayes, no author's name appeared on the title page, and the entire work is thought to be of dubious provenance. Evidence for Bayes' mathematical talents comes from a notebook that was almost certainly written by Bayes, which contains much mathematical work, including discussions of probabil ity, trigonometry, geometry, solutions of equations, series, and differential calculus. There are also sections on natural philosophy, in which Bayes looks at topics that include electricity, optics, and celestial mechanics. Bayes is also the author of a mathematical publication on asymptotic series, which appeared after his death. 6-29 linkS� 6.3 Bayes' Theorem 421 Bayesian Spam Filters Most electronic mailboxes receive a flood of unwanted and unsolicited messages, known as spam. Because spam threatens to overwhelm electronic mail systems, a tremendous amount of work has been devoted to filtering it out. Some of the first tools developed for eliminating spam were based on Bayes' Theorem, such as Bayesian spam filters. A Bayesian spam filter uses information about previously seen e-mail messages to guess whether an incoming e-mail message is spam. Bayesian spam filters look for occurrences of particular words in messages. For a particular word w, the probability that w appears in a spam e-mail message is estimated by determining the number oftimes w appears in a message from a large set of messages known to be spam and the number of times it appears in a large set of messages known not to be spam. When we examine e-mail messages to determine whether they might be spam, we look at words that might be indicators of spam, such as "offer," "special," or "opportunity," as well as words that might indicate that a message is not spam, such as "mom," "lunch," or "Jan" (where Jan is one of your friends). Unfortunately, spam filters sometimes fail to identify a spam message as spam; this is called a false negative. And they sometimes identify a message that is not spam as spam; this is called a false positive. When testing for spam, it is important to minimize false positives, because filtering out wanted e-mail is much worse than letting some spam through. We will develop some basic Bayesian spam filters. First, suppose we have a set B of messages known to be spam and a set G of messages known not to be spam. (For example, users could classify messages as spam when they examine them in their inboxes.) We next identify the words that occur in B and in G. We count the number of messages in the set containing each word to find nB(w) and nc(w), the number of messages containing the word w in the sets B and G, respectively. Then, the empirical probability that a spam message contains the word w is p( w) = n B (w ) / I B I, and the empirical probability that a message that is not spam contains the word w is q(w) = nc(w)/IGI. We note that pew) and q(w) estimate the probabilities that an incoming spam message, and an incoming message that is not spam, contain the word w, respectively. Now suppose we receive a new e-mail message containing the word w. Let S be the event that the message is spam. Let E be the event that the message contains the word w. The events S, that the message is spam, and S, that the message is not spam, partition the set of all messages. Hence, by Bayes' Theorem, the probability that the message is spam, given that it contains the word w, is S E _ peE I S)p(S) p( I ) -peE I S)p(S) + peE I S)p(S) To apply this formula, we first estimate peS), the probability that an incoming message is spam, as well as peS), the probability that the incoming message is not spam. Without prior knowledge about the likelihood that an incoming message is spam, for simplicity we assume that the message is equally likely to be spam as it is not to be spam. That is, we assume that peS) = peS) = 1/2. Using this assumption, we find that the probability that a message is spam, given that it contains the word w, is S E _ peE IS) p( I ) -peE IS) + peE I S) (Note that if we have some empirical data about the ratio of spam messages to messages that are not spam, we can change this assumption to produce a better estimate for peS) and for p(S); see Exercise 22.) Next, we estimate peE I S), the conditional probability that the message contains the word w given that the message is spam, by p(w). Similarly, we estimate peE I S), the conditional 422 6 / Discrete Probability 6-30 probability that the message contains the word w, given that the message is not spam, by q (w). Inserting these estimates for p(E I S) and p(E I S) tells us that p(S I E) can be estimated by r(w) = p(w) ; p(w) + q(w) that is, r(w) estimates the probability that the message is spam, given that it contains the word w. Ifr(w) is greater than a threshold that we set, such as 0.9, then we classify the message as spam. EXAMPLE 3 Suppose that we have found that the word "Rolex" occurs in 250 of 2000 messages known to be spam and in 5 of 1000 messages known not to be spam. Estimate the probability that an incoming message containing the word "Rolex" is spam, assuming that it is equally likely that an incoming message is spam or not spam. If our threshold for rejecting a message as spam is 0.9, will we reject this message? Solution: We use the counts that the word "Rolex" appears in spam messages and messages that are not spam to find thatr(Rolex) = 250/2000 = 0.125 andq(Rolex) = 5/1000 = 0.005. Because we are assuming that it is equally likely that an incoming message is or is not spam, we can estimate the probability that the message is spam by I p(Rolex) r(Ro ex) = ------­p(Rolex) + q(Rolex) 0.125 0.125 ----- = -- � 0.962. 0.125 + 0.005 0.130 Because r(Rolex) is greater than the threshold 0.9, we reject this message as spam. ... Detecting spam based on the presence of a single word can lead to excessive false positives and false negatives. Consequently, spam filters look at the presence of multiple words. For example, suppose that the message contains the words w I and W2. Let E I and E 2 denote the events that the message contains the words WI and W2, respective ly. To make our computations simpler, we assume that EI and E2 are independent events and that EI I Sand E2 I S are independent events and that we have no prior knowledge regarding whether or not the message is spam. (The assumptions that EI and E2 are independent and that EI I Sand E2 I S are independent may introduce some error into our computations; we assume that this error is small.) Using Bayes' Theorem and our assumptions, we can show (see Exercise 23) that p(S I EI n E2), the probability that the message is spam given that it contains both WI and W2, is p(S I EI n E2) = p(EI I S)P(E2 I S) p(EI I S)P(E2 I S) + p(EI I S)P(E2 I S) We estimate the probability p(S I EI n E2) by That is, r(wI' W2) estimates the probability that the message is spam, given that it contains the words WI and W2. When r(wI, W2) is greater than a preset threshold, such as 0.9, we determine that the message is likely spam. EXAMPLE 4 Suppose that we train a Bayesian spam filter on a set of2000 spam messages and 1000 messages that are not spam. The word "stock" appears in 400 spam messages and 60 messages that are not spam, and the word "undervalued" appears in 200 spam messages and 25 messages that are 6-31 6.3 Bayes' Theorem 423 not spam. Estimate the probability that an incoming message containing both the words "stock" and "undervalued" is spam, assuming that we have no prior knowledge about whether it is spam. Will we reject the message as spam when we set the threshold at 0.9? Solution: Using the counts of each of these two words in messages known to be spam or known not to be spam, we obtain the following estimates: p(stock) = 400/2000 = 0.2, q(stock) = 60/1000 = 0.06, p(undervalued) = 200/2000 = 0.1, and q(undervalued) = 25/1000 = 0.025. Using these probabilities, we can estimate the probability that the message is spam by ( k d 1 d) p(stock)p(undervalued) r stoc , un erva ue = ----,---::..,...:. ----'..:........: --- ---'- --- - ­ p(stock)p(undervalued) + q(stock)q(undervalued) (0.2)(0.1) � 0.930. (0.2)(0.1) + (0.06)(0.025) Because we have set the threshold for rejecting messages at 0.9, the message will be rejected by the filter. .... The more words we use to estimate the probability that an incoming mail message is spam, the better is our chance that we correctly determine whether it is spam. In general, if Ei is the event that the message contains word Wi, assuming that the number of incoming spam messages is approximately the same as the number of incoming messages that are not spam, and that the events E i I S are independent, then by Bayes' Theorem the probability that a message containing all the words Wi, i = 1,2, ... , k, is (s I nk £.) _ n�=, P(Ei I S) P I -k k • i=1 ni=1 P(Ei I S) + ni=1 P(Ei I S) We can estimate this probability by ( ) n�=, P(Wi) r WI, W2,···, Wk = k k ' ni=1 P(Wi) + ni=1 q(Wi) For the most effective spam filter, we choose words for which the probability that each of these words appears in spam is either very high or very low. When we compute this value for a particular message, we reject the message as spam if r( WI, W2, .•. , W k) exceeds a preset threshold, such as 0.9. Another way to improve the performance of a Bayesian spam filter is to look at the prob­ abilities that particular pairs of words appear in spam and in messages that are not spam. We then treat appearances of these pairs of words as appearance of a single block, rather than as the appearance of two separate words. For example, the pair of words "enhance performance" most likely indicates spam, while "operatic performance" indicates a message that is not spam. Similarly, we can assess the likelihood that a message is spam by examining the structure of a message to determine where words appear in it. Also, spam filters look at appearances of certain types of strings of characters rather than just words. For examp le, a message with the valid e-mail address of one of your friends is less likely to be spam (if not sent by a worm) than one containing an e-mail address that came from a country known to originate a lot of spam. There is an ongoing war between people who create spam and those trying to filter their messages out. This leads to the introduction of many new techniques to defeat spam filters, including inserting into spam messages long strings of words that appear in messages that are not spam, as well as including words inside pictures. The techniques we have discussed here are only the first steps in fighting this war on spam. 424 6 / Discrete Probability Exercises 1. Suppose that E and F are events in a sample space andp(E) = 1/3,p(F) = 1/2,andp(E I F) = 2/5. Find p(F I E). 2. Suppose that E and F are events in a sample space andp(E) = 2/3,p(F) = 3/4,andp(F I E) = 5/8. Find peE I F). 3. Suppose that Frida selects a ball by first picking one of two boxes at random and then selecting a ball from this box at random. The first box contains two white balls and three blue balls, and the second box contains four white balls and one blue ball. What is the probability that Frida picked a ball from the first box if she has selected a blue ball? 4. Suppose that Ann selects a ball by first picking one of two boxes at random and then selecting a ball from this box. The first box contains three orange balls and four black balls, and the second box contains five orange balls and six black balls. What is the probability that Ann picked a ball from the second box if she has selected an orange ball? 5. Suppose that 8% of all bicycle racers use steroids, that a bicyclist who uses steroids tests positive for steroids 96% of the time, and that a bicyclist who does not use steroids tests positive for steroids 9% of the time. What is the probability that a randomly selected bicyclist who tests positive for steroids actually uses steroids? 6. When a test for steroids is given to soccer players, 98% of the players taking steroids test positive and 12% of the players not taking steroids test positive. Suppose that 5% of soccer players take steroids. What is the probability that a soccer player who tests positive takes steroids? 7. Suppose that a test for opium use has a 2% false positive rate and a 5% false negative rate. That is, 2% of people who do not use opium test positive for opium, and 5% of opium users test negative for opium. Furthermore, sup­ pose that 1 % of people actually use opium. a) Find the probability that someone who tests negative for opium use does not use opium. b) Find the probability that someone who tests positive for opium use actually uses opium. 8. Suppose that one person in 10,000 people has a rare ge­ netic disease. There is an excellent test for the disease; 99.9% of people with the disease test positive and only 0.02% who do not have the disease test positive. a) What is the probability that someone who tests posi­ tive has the genetic disease? b) What is the probability that someone who tests nega­ tive does not have the disease? 9. Suppose that 8% of the patients tested in a clinic are in­ fected with HIV Furthermore, suppose that when a blood test for HIV is given, 98% of the patients infected with 6-32 HIV test positive and that 3% of the patients not infected with HIV test positive. What is the probability that a) a patient testing positive for mv with this test is in­ fected with it? b) a patient testing positive for HIV with this test is not infected with it? c) a patient testing negative for mv with this test is in­ fected with it? d) a patient testing negative for mv with this test is not infected with it? 10. Suppose that 4% of the patients tested in a clinic are in­ fected with avian influenza. Furthermore, suppose that when a blood test for avian influenza is given, 97% of the patients infected with avian influenza test positive and that 2% of the patients not infected with avian influenza test positive. What is the probability that a) a patient testing positive for avian influenza with this test is infected with it? b) a patient testing positive for avian influenza with this test is not infected with it? c) a patient testing negative for avian influenza with this test is infected with it? d) a patient testing negative for avian influenza with this test is not infected with it? 11. An electronics company is planning to introduce a new camera phone. The company commissions a marketing re­ port for each new product that predicts either the success or the failure of the product. Of new products introduced by the company, 60% have been successes. Furthermore, 70% of their successful products were predicted to be successes, while 40% of failed products were predicted to be successes. Find the probability this new camera phone will be successful if its success has been predicted. *12. A space probe near Neptune communicates with Earth using bit strings. Suppose that in its transmissions it sends a lone-third of the time and a 0 two-thirds of the time. When a 0 is sent, the probability that it is received cor­ rectly is 0.9, and the probability that it is received incor­ rectly (as a I) is 0.1. When a 1 is sent, the probability that it is received correctly is 0.8, and the probability that it is received incorrectly (as a 0) is 0.2. a) Find the probability that a 0 is received. b) Use Bayes' Theorem to find the probability that a 0 was transmitted, given that a 0 was received. 13. Suppose that E, Fl, F2, and F3 are events from a sample space S and that Fl, F2, and F3 are mutually disjoint and their union is S. Find P(FI I E) if peE I Fl) = 1/8, peE I F2)=1/4,p(E I F3)= 1/6,p(FI)= 1/4, P(F2) = 1/4, and p(F3) = 1/2. 14. Suppose that E, FJ, F2, and F3 are events from a sample space S and that Fl, F2, and F3 are mutually disjoint and their union is S. Find p(F2 I E) if peE I Fl) = 2/7, 6-33 p(E I F2) = 3/8, p(E I F3) = 1/2, p(FI) = 1/6, p(F2) = 1/2, and p(F3) = 1/3. 15. In this exercise we will use Bayes' Theorem to solve the Monty Hall puzzle (Example lOin Section 6.1). Recall that in this puzzle you are asked to select one of three doors to open. There is a large prize behind one of the three doors and the other two doors are losers. After you select a door, Monty Hall opens one of the two doors you did not se­ lect that he knows is a losing door, selecting at random if both are losing doors. Monty asks you whether you would like to switch to this door. Suppose that the three doors in the puzzle are labeled 1,2, and 3. Let W be the ran­ dom variable whose value is the number of the winning door; assume that p(W = k) = 1/3 for k = 1,2,3. Let M denote the random variable whose value is the num­ ber of the door that Monty opens. Suppose you choose door i. a) What is the probability that you will win the prize if the game ends before Monty asks you whether you want to change doors? b) Find p(M = j I W = k) for j = 1,2,3 and k = 1,2,3. c) Use Bayes' Theorem to find p(W = jiM = k) where i and j and k are distinct values. d) Explain why the answer to part (c) tells you whether you should change doors when Monty gives you the chance to do so. 16. Ramesh can get to work three different ways: by bicycle, by car, or by bus. Because of commuter traffic, there is a 50% chance that he will be late when he drives his car. When he takes the bus, which uses a special lane reserved for buses, there is a 20% chance that he will be late. The probability that he is late when he rides his bicycle is only 5%. Ramesh arrives late one day. His boss wants to es­ timate the probability that he drove his car to work that day. a) Suppose the boss assumes that there is a 1/3 chance that Ramesh takes each of the three ways he can get to work. What estimate for the probability that Ramesh drove his car does the boss obtain from Bayes' Theo­ rem under this assumption? b) Suppose the boss knows that Ramesh drives 30% of the time, takes the bus only 10% of the time, and takes his bicycle 60% of the time. What estimate for the probability that Ramesh drove his car does the boss obtain from Bayes' Theorem using this information? "17. Prove Theorem 2, the extended form of Bayes' Theo­ rem. That is, suppose that E is an event from a sample space S and that FI, F2, ... , Fn are mutually exclusive eventssuchthatU7=1 Fj = S.Assumethatp(E) =1= o and p(Fj) =1= 0 for i = 1,2, ... , n. Show that p(Fj I E) = "n (E I F) (F)' L.../=I P / P / [Hint: Use the fact that E = U7=1 (E n Fj ).] 6.3 Bayes' Theorem 425 18. Suppose that a Bayesian spam filter is trained on a set of 500 spam messages and 200 messages that are not spam. The word "exciting" appears in 40 spam messages and in 25 messages that are not spam. Would an in­ coming message be rejected as spam if it contains the word "exciting" and the threshold for rejecting spam is 0.9? 19. Suppose that a Bayesian spam filter is trained on a set of 1000 spam messages and 400 messages that are not spam. The word "opportunity" appears in 175 spam messages and 20 messages that are not spam. Would an incom­ ing message be rejected as spam if it contains the word "opportunity" and the threshold for rejecting a message is 0.9? 20. Would we reject a message as spam in Example 4 a) using just the fact that the word "undervalued" occurs in the message? b) using just the fact that the word "stock" occurs in the message? 21. Suppose that a Bayesian spam filter is trained on a set of 10,000 spam messages and 5000 messages that are not spam. The word "enhancement" appears in 1500 spam messages and 20 messages that are not spam, while the word "herbal" appears in 800 spam messages and 200 messages that are not spam. Estimate the probabil­ ity that a received message containing both the words "enhancement" and "herbal" is spam. Will the message be rejected as spam if the threshold for rejecting spam is 0.9? 22. Suppose that we have prior information concerning whether a random incoming message is spam. In par­ ticular, suppose that over a time period, we find that s spam messages arrive and h messages arrive that are not spam. a) Use this information to estimate p(S), the probabil­ ity that an incoming message is spam, and p(S), the probability an incoming message is not spam. b) Use Bayes' Theorem and part (a) to estimate the probability that an incoming message containing the word w is spam, where p(w) is the probability that w occurs in a spam message and q(w) is the probability that w occurs in a message that is not spam. 23. Suppose that EI and E2 are the events that an incoming mail message contains the words WI and W2, respectively. Assuming that E I and E 2 are independent events and that EI I Sand E2 I S are independent events, where S is the event that an incoming message is spam, and that we have no prior knowledge regarding whether or not the message is spam, show that p(EI I S)P(E2 IS) p(EI I S)P(E2 IS) + p(EI I S)P(E2 IS) 426 6 / Discrete Probability 6-34 6.4 Expected Value and Variance linkS� DEFINITION 1 Introduction The expected value of a random variable is the sum over all elements in a sample space of the product of the probability of the element and the value of the random variable at this element. Consequently, the expected value is a weighted average of the values ofa random variable. The expected value of a random variable provides a central point for the distribution of values of this random variable. We can solve many problems using the notion of the expected value of a random variable, such as determining who has an advantage in gambling games and computing the average-case complexity of algorithm s. Another useful measure of a random variable is its variance, which tells us how spread out the values of this random variable are. We can use the variance of a random variable to help us estimate the probability that a random variable takes values far removed from its expected value. Expected Values Many questions can be formulated in terms of the value we expect a random variable to take, or more precisely, the average value of a random variable when an experiment is performed a large number of times. Questions of this kind include: How many heads are expected to appear when a coin is flipped 100 times? What is the expected number of comparisons used to find an element in a list using a linear search? To study such questions we introduce the concept of the expected value of a random variable. The expected value (or expectation) of the random variable X (s) on the sample space S is equal to E(X) = I>(s)X(s). SES Note that when the sample space S has n elements S = {XI, X2, ••• , xn}, E(X) = I:7=1 P(Xi)X(Xi). Remark: When there are infinitely many elements of the sample space, the expectation is de­ fined only when the infinite series in the definition is absolutely convergent. In particular, the expectation of a random variable on an infinite sample space is finite if it exists. EXAMPLE 1 Expected Value of a Die Let X be the number that comes up when a die is rolled. What is the expected value of X? Solution: The random variable X takes the values 1,2,3,4,5, or 6, each with probability 1/6. It follows that 1 1 1 1 1 1 21 7 E(X) = -·1 + -·2 + -·3 + -·4 + -·5 + -·6 = - = -. 666666 62 6-35 EXAMPLE 2 Ex1ra� Examples� THEOREM l 6.4 Expected Value and Variance 427 A fair coin is flipped three times. Let S be the sample space of the eight possible outcomes, and let X be the random variable that assigns to an outcome the number of heads in this outcome. What is the expected value of X? Solution: In Example 10 of Section 6.2 we listed the values of X for the eight possible outcomes when a coin is flipped three times. Because the coin is fair and the flips are independent, the probability of each outcome is 1/8. Consequently, E(X) = t[X(HHH) + X(HHT) + X(HTH) + X(THH) + X(TTH) + X (THT) + X(HTT) + X(TTT)] I 12 = 8(3 + 2 + 2 + 2 + 1 + 1 + 1 + 0) = 8 3 - 2 Consequently, the expected number of heads that come up when a fair coin is flipped three times is 3/2. .... When an experiment has relatively few outcomes, we can compute the expected value of a random variable directly from its definition, as was done in Example 2. However, when an experiment has a large number of outcomes, it may be inconvenient to compute the expected value of a random variable directly from its definition. Instead, we can find the expected value of a random variable by grouping together all outcomes assigned the same value by the random variable, as Theorem 1 shows. If X is a random variable and p(X = r) is the probability that X = r, so that p(X = r) = LSES,X(s)=r p(s), then E(X) = L p(X = r)r. rEX(S) Proof: Suppose that X is a random variable with range X(S), and let p(X = r) be the proba­ bility that the random variable X takes the value r. Consequently, p( X = r) is the sum ofthe probabilities of the outcomes s such that X (s) = r. It follows that E(X) = L p(X = r)r. <l rEX(S) Example 3 and the proof of Theorem 2 illustrate the use of this formula. In Example 3 we will find the expected value of the sum of the numbers that appear on two fair dice when they are rolled. In Theorem 2 we will find the expected value of the number of successes when n Bernoulli trials are performed. EXAM PLE 3 What is the expected value of the sum of the numbers that appear when a pair of fair dice is rolled? Solution: Let X be the random variable equal to the sum of the numbers that appear when a pair of dice is rolled. In Example 12 of Section 6.2 we listed the value of X for the 36 outcomes of 428 6 / Discrete Probability 6-36 THEOREM 2 this experiment. The range of X is {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}. By Example 12 of Section 6.2 we see that p(X = 2) = p(X = 12) = 1/36, p(X = 3) = p(X = 11) = 2/36 = 1/18, p(X = 4) = p(X = 10) = 3/36 = 1/12, p(X = 5) = p(X = 9) = 4/36 = 1/9, p(X = 6) = p(X = 8) = 5/36, p(X = 7) = 6/36 = 1/6. Substituting these values in the formula, we have 1 1 1 1 5 1 E(X) = 2· 36 + 3.18 + 4.12 + 5.9 + 6· 36 + 7."6 5 1 1 1 1 + 8 . -+ 9 . -+ 10 . -+ 11 . -+ 12 . - 36 9 12 18 36 =7. The expected number of successes when n independent Bernoulli trials are performed, where p is the probability of success on each trial, is np. Proof: Let X be the random variable equal to the number of successes in n trials. By Theorem 2 of Section 6.2 we see that p(X = k) = C(n, k)pkqn-k. Hence, we have n E(X) = L kp(X = k) by Theorem I k=l n = L kC(n, k)pkqn-k by Theorem 2 in Section 6.2 k=l n = L nC(n -1, k - I)pkqn-k by Exercise 21 in Section 5.4 k=l n = np L C(n -I, k -I)pk-lqn-k factoring np from each term k=l n-l = np L C(n -1, )p}qn-l-} shifting index of summation with} = k -I }=o = np(p + q )n-l by the Binomial Theorem = np. because p + q = I This completes the proof because it shows that the expected number of successes in n indepen­dent Bernoulli trials is np. <l 6-37 6.4 Expected Value and Variance 429 Next we will show that the hypothesis that the Bernoulli trials are independent in Theorem 2 is not necessary. Linearity of Expectations Theorem 3 tells us that expected values are linear. For example, the expected value of the sum of random variables is the sum of their expected values. We will find this property exceedingly useful. THEO REM 3 If Xi, i = 1, 2, ... , n with n a positive integer, are random variables on S, and if a and b are real numbers, then (i) E(XI + X2 + ... + Xn) = E(Xd + E(X2) + ... + E(Xn) (ii) E(aX + b) = aE(X) + b. Proof: The first result for n = 2 follows directly from the definition of expected value, because E(XI + X2) = LP(s)(XI(s) + X2(S» SES SES SES The case with n random variables follows easily using mathematical induction from the case of two random variables. Finally, note that E(aX + b) = LsEs p(s)(aX(s) + b) = a LSES p(s)X(s) + b LSES p(s) = aE(X) + b because LSES p(s) = 1. Examples 4 and 5 illustrate how to use Theorem 3. <l EXAM PLE 4 Use Theorem 3 to find the expected value of the sum of the numbers that appear when a pair of fair dice is rolled. (This was done in Example 3 without the benefit ofthis theorem.) Solution: Let X I and X2 be the random variables with X I «i, j» = i and X2«i, j» = j, so that X I is the number appearing on the first die and X 2 is the number appearing on the second die. It is easy to see that E(XI) = E(X2) = 7/2 because both equal (1 + 2 + 3 + 4 + 5 + 6)/6 = 21/6 = 7/2. The sum of the two numbers that appear when the two dice are rolled is the sum XI + X2. By Theorem 3, the expected value of the sum is E(XI + X2) = E(Xd + E(X2) = 7/2 + 7/2 = 7. � EXAMPLE 5 In the proof of Theorem 2 we found the expected value of the number of successes when n independent Bernoulli trials are performed, where p is the probability of success on each trial 430 6 / Discrete Probability 6-38 by direct computation. Show how Theorem 3 can be used to derive this result where the Bernoulli trials are not necessarily independent. Solution: Let Xi be the random variable with Xi«(tl, t2, ... , tn» = 1 if ti is a success and Xi «tl, t2, ... , tn» = 0 if ti is a failure. The expected value of Xi is E(Xi) = 1 . p + o· (1 -p) = p fori = 1,2, ... , n. Let X = XI + X2 + ... + Xn, so that X counts the number of successes when these n Bernoulli trials are performed. Theorem 3, applied to the sum of n random variables, shows that E(X) = E(X I) + E(X2) + ... + E(Xn) = np. � We can take advantage ofthe linearity of expectations to find the solutions of many seemingly difficult problems. The key step is to express a random variable whose expectation we wish to find as the sum of random variables whose expectations are easy to find. Examples 6 and 7 illustrate this technique. EXAMPLE 6 Expected Value in the Hatcheck Problem A new employee checks the hats of n people at a restaurant, forgetting to put claim check numbers on the hats. When customers return for their hats, the checker gives them back hats chosen at random from the remaining hats. What is the expected number of hats that are returned correctly? Solution: Let X be the random variable that equals the number of people who receive the correct hat from the checker. Let Xi be the random variable with Xi = 1 if the ith person receives the correct hat and Xi = 0 otherwi se. It follows that X=XI+X2+···+Xn· Because it is equally likely that the checker returns any of the hats to this person, it follows that the probability that the ith person receives the correct hat is lin. Consequently, by Theorem 1, for all i we have E(Xi) = 1· P(Xi = 1) + o· P(Xi = 0) = 1· lin + 0 = lin. By the linearity of expectations (Theorem 3), it follows that E(X) = E(XI) + E(X2) + ... + E(Xn) = n· lin = l. Consequently, the average number of people who receive the correct hat is exactly 1. Note that this answer is independent of the number of people who have checked their hats! (We will find an explicit formula for the probability that no one receives the correct hat in Section 7.6.) � EXAMPLE 7 Expected Number ofInversions in a Permutation The ordered pair (i, j) is called an inver­ sion in a permutation of the first n positive integers if i < j but j precedes i in the permutation. For instance, there are six inversions in the permutation 3,5, 1,4,2; these inversions are (l, 3), (l, 5), (2, 3), (2, 4), (2, 5), (4, 5). To find the expected number of inversions in a random permutation of the first n positive integers, we let Ii,j be the random variable on the set of all permutations of the first n positive integers with Ii,j = 1 if(i, j) is an inversion ofthe permutation and Ii,j = 0 otherwise. It follows that if X is the random variable equal to the number of inversions in the permutation, then X = L Ii,j' l::'Oi<j::'On 6-39 LinkS� 6.4 Expected Value and Variance 431 Note that it is equally likely for i to precede j in a randomly chosen permutation as it is for j to precede i. (To see this, note that there are an equal number of permutations with each of these properties.) Consequently, for all pairs i and j we have E(Ii,j) = 1 . P(Ii,j = 1) + O· P(Ii,j = 0) = 1·1/2 + 0 = 1/2. Because there are G) pairs i and j with 1 :s i < j :s n and by the linearity of expectations (Theorem 3), we have " (n) 1 n(n -1) E(X) = � Ii,j = . -= . I . . 2 2 4 :S1<;:Sn It follows that there are an average of n(n -1)/4 inversions in a permutation of the first n positive integers. ... Average-Case Computational Complexity Computing the average-case computational complexity of an algorithm can be interpreted as computing the expected value of a random variable. Let the sample space of an experiment be the set of possible inputs a j, j = 1,2, ... , n, and let X be the random variable that assigns to a j the number of operations used by the algorithm when given a j as input. Based on our knowledge of the input, we assign a probability p( a j) to each possible input value a j. Then, the average-case complexity of the algorithm is n E(X) = LP(aj)X(aj). j=1 This is the expected value of X. Finding the average-case computational complexity of an algorithm is usually much more difficult than finding its worst-case computational complex ity, and often involves the use of sophisticated methods. However, there are some algorithms for which the analysis required to find the average-case computational complexity is not difficult. For instance, in Example 8 we will illustrate how to find the average-case computational complexity of the linear search algorithm under different assumptions concerning the probability that the element for which we search is an element of the list. EXAMPLE 8 Average-Case Complexity of the Linear Search Algorithm We are given a real number x and a list of n distinct real numbers. The linear search algorithm, described in Section 3.1, lo­ cates x by successively comparing it to each element in the list, terminating when x is located or when all the elements have been examined and it has been determined that x is not in the list. What is the average-case computational complexity of the linear search algorithm if the probability that x is in the list is p and it is equally likely that x is any of the n elements in the list? (There are n + 1 possible types of input: the n numbers in the list and a number not in the list, which we treat as a single input .) Solution: In Example 4 of Section 3.3 we showed that 2i + 1 comparisons are used if x equals the ith element of the list and, in Example 2 of Section 3.3, that 2n + 2 comparisons are used if x is not in the list. The probability that x equals ai, the i th element in the list, is p / n, and the 432 6 / Discrete Probability 6-40 probability that x is not in the list is q = 1 -p. It follows that the average-case computational complexity of the linear search algorithm is 3p 5p (2n + l)p E = -+ -+ ... + + (2n + 2)q n n n = E(3 + 5 + ... + (2n + 1)) + (2n + 2)q n = E«n + Ii -1) + (2n +2)q n = p(n + 2) + (2n + 2)q. (The third equality follows from Example 2 of Section 4.1.) For instance, when x is guaranteed to be in the list, we have p = 1 (so the probability that x = ai is l/n for each i) and q = O. Then E = n + 2, as we showed in Example 4 in Section 3.3. When p, the probability that x is in the list, is 1/2, it follows that q = 1 -p = 1/2, so E = (n + 2)/2 + n + 1 = (3n + 4)/2. Similarly, if the probability that x is in the list is 3/4, we have p = 3/4 and q = 1/4, so E = 3(n + 2)/4 + (n + 1)/2 = (5n + 8)/4. Finally, when x is guaranteed not to be in the list, we have p = 0 and q = 1. It follows that E = 2n + 2, which is not surprising because we have to search the entire list. ... Example 9 illustrates how the linearity of expectations can help us find the average-case complexity of a sorting algorithm, the insertion sort. EXAMPLE 9 Average-Case Complexity of the Insertion Sort What is the average number of comparisons used by the insertion sort to sort n distinct elements? Solution: We first suppose that X is the random variable equal to the number of comparisons used by the insertion sort (described in Section 3.1) to sort a list ai, a2, ... , an of n distinct elements. Then E(X) is the average number of comparisons used. (Recall that at step i for i = 2, ... , n, the insertion sort inserts the ith element in the original list into the correct position in the sorted list of the first i-I elements of the original list. ) We let Xi be the random variable equal to the number of comparisons used to insert ai into the proper position after the first i-I elements a I, a2, ... , ai -I have been sorted. Because we can use the linearity of expectations to conclude that To find E(X;) for i = 2, 3, ... , n, let pj(k) denote the probability that the largest of the first j elements in the list occurs at the kth position, that is, that max(al, a2, ... , aj) = ak, where 1 :s k :s j. Because the elements of the list are randomly distributed, it is equally likely for the largest element among the first j elements to occur at any position. Consequently, pj(k) = l/j. If Xi(k) equals the number of comparisons used by the insertion sort if ai is inserted into the kth position in the list once ai, a2, ... , ai-J have been sorted, it follows that Xi(k) = k. Because it. 6-41 EXAMPLE 10 LinkS� 6.4 Expected Value and Variance 433 is possible that ai is inserted in any of the first i positions, we find that i i 1 1 i 1 i(i + 1) i + 1 E(Xi) = LPi(k). Xi(k) = L -;-. k = -;-. Lk = -;-. = -. k=1 k=1 I I k=1 I 2 2 It follows that n n i + 1 1 n+1 E(X) = LE(Xi)= L-= -Lj . 2 .22 2 . 3 1= 1= J= = � (n + l)(n + 2) _ �(1 + 2) = n2 + 3n -4. 2 2 2 4 To obtain the third of these equalities we shifted the index of summation, setting j = i + 1. To obtain the fourth equality, we used the formula L:;=I k = m(m + 1)/2 (from Table 2 in Section 2.4) with m = n + 1, subtracting off the missing terms with j = 1 and j = 2. We conclude that the average number of comparisons used by the insertion sort to sort n elements equals (n2 + 3n -4)/4, which is 8(n2). .... The Geometric Distribution We now tum our attention to a random variable with infinitely many possible outcome s. Suppose that the probability that a coin comes up tails is p. This coin is flipped repeatedly until it comes up tails. What is the expected number of flips until this coin comes up tails? Solution: We first note that the sample space consists of all sequences that begin with any number of heads, denoted by H, followed by a tail, denoted by T. Therefore, the sample space is the set {T, HT, HHT, HHHT, HHHHT, ... }. Note that this is an infinite sample space. We can determine the probability of an element of the sample space by noting that the coin flips are independent and that the probability of a head is 1 -p. Therefore, p(T) = p, p(HT) = (1 -p)p, p(HHT)= (1 -pip, and in general the probability that the coin is flipped n times before a tail comes up, that is, that n - 1 heads come up followed by a tail, is (1 -pt-1p. (Exercise 14 asks for a verification that the sum of the probabilities of the points in the sample space is 1.) Now let X be the random variable equal to the number of flips in an element in the sample space. That is, X(T) = 1, X(HT)= 2, X(HHT)= 3, and so on. Note that p(X = j) = (1 -p))-I p. The expected number of flips until the coin comes up tails equals E(X). U sing Theorem 1, we find that 00 00. 00 . 1 1 E(X) = Lj· p(X = j) = Lj(l -p)1-lp = P Lj(l -p)J-I = p. 2" = -. )=1 )=1 )=1 P P [The third equality in this chain follows from Table 2 in Section 2.4, which tells us that L:�I j(l -p))-I = 1/(1-(1-p))2 = 1/p2.] It follows that the expected number of times the coin is flipped until tails comes up is 1/ p. Note that when the coin is fair we have p = 1/2, so the expected number of flips until it comes up tails is 1/(1/2) = 2 . .... 434 6 / Discrete Probability 6-42 DEFINITION 2 THEOREM 4 DEFINITION 3 EXAMPLE 11 Exlra� Examples� The random variable X that equals the number of flips expected before a coin comes up tails is an example of a random variable with a geometric distribution. A random variable X has a geometric distribution with parameter p if p(X = k) = (l-pt-tpfork= 1,2,3, .... Geometric distributions arise in many applications because they are used to study the time required before a particular event happens, such as the time required before we find an object with a certain property, the number of attempts before an experiment succeeds, the number of times a product can be used before it fails, and so on. When we computed the expected value of the number of flips required before a coin comes up tails, we proved Theorem 4. Ifthe random variable X has the geometric distribution with parameter p, then E(X) = 1/ p. Independent Random Variables We have already discussed independent events. We will now define what it means for two random variables to be independent. The random variables X and Y on a sample space S are independent if p(X(s) = rt and Y(s) = r2) = p(X(s) = rl)' p(Y(s) = r2), or in words, if the probability that X(s) = rt and Y(s) = r2 equals the product of the proba­ bilities that X (s) = rt and Y (s) = r2, for all real numbers rl and r2. Are the random variables X t and X 2 from Example 4 independent? Solution: Let S = {t, 2, 3, 4, 5, 6}, and let i E Sand j E S. Because there are 36 possible outcomes when the pair of dice is rolled and each is equally likely, we have P(XI = i and X2 = j) = 1/36. Furthermore, P(XI = i) = 1/6 and P(X2 = j) = 1/6, because the probability that i appears on the first die and the probability that j appears on the second die are both 1/6. It follows that 1 1 1 1 p(Xt=iandX2=j)=36 and p(Xt=i)p(X2=j)=6'6=36' so X 1 and X 2 are independent. 6-43 6.4 Expected Value and Variance 435 EXAM PLE 12 Show that the random variables XI and X = XI + X2, where XI and X2 are as defined in Example 4, are not independent . THEOREM S Solution: Note that P(XI = 1 and X = 12) = 0, because XI = 1 means the number appear­ ing on the first die is 1, which implies that the sum of the numbers appearing on the two dice cannot equal 12. On the other hand, p(X I = 1) = 1/6 and p(X = 12) = 1/36. Hence P(XI = 1 and X = 12) =1= P(XI = 1)· p(X = 12). This counterexample shows that XI and X are not independent. .... The expected value of the product of two independent random variables is the product of their expected values, as Theorem 5 shows. If X and Yare independent random variables on a space S, then E(XY) = E(X)E(Y). Proof: From the definition of expected value and because X and Yare independent random variables, it follows that E(XY) = L X(s)Y(s)p(s) SES L rlr2 . p(X(s) = rl and Yes) = r2) rl EX(S),r2EY(S) L rlr2 . p(X(S) = rd . p(Y(S) = r2) rl EX(S),r2EY(S) = E(X)E(Y). This completes the proof. <l Note that when X and Yare random variables that are not independent, we cannot conclude that E(XY) = E(X)E(Y), as Example 13 shows. EXAMPLE 13 Let X and Y be random variables that count the number of heads and the number of tails when a coin is flipped twice. Because p(X = 2) = 1/4, p(X = 1) = 1/2, and p(X = 0) = 1/4, by Theorem 1 we have 1 1 1 E(X) = 2 . - + 1 . - + 0 . -= 1. 424 A similar computation shows that E(Y) = 1. We note that XY = 0 when either two heads and no tails or two tails and no heads come up and that XY = 1 when one head and one tail come up. Hence, 1 1 1 E(XY) = 1 . - + O· -= -. 22 2 436 6 / Discrete Probability 6-44 unkS� DEFINITION 4 It follows that E(XY) =1= E(X)E(Y). This does not contradict Theorem 5 because X and Yare not independent, as the reader should verify (see Exercise 16). .... Variance The expected value of a random variable tells us its average value, but nothing about how widely its values are distributed. For example, if X and Yare the random variables on the set S = {I, 2, 3, 4, 5, 6}, withX(s) = o for all s E S andY(s) = -1 ifs E {I, 2, 3} andY(s) = 1 if s E {4, 5, 6}, then the expected values of X and Yare both zero. However, the random variable X never varies from 0, while the random variable Y always differs from 0 by 1. The variance of a random variable helps us characterize how widely a random variable is distributed. Let X be a random variable on a sample space S. The variance of X, denoted by V (X), is VeX) = L(X(s) -E(X))2p(s). seS The standard deviation of X, denoted a(X), is defined to be JV (X). Theorem 6 provides a useful simple expression for the variance of a random variable. THEOREM 6 If X is a random variable on a sample space S, then VeX) = E(X2) -E(X)2. Proof: Note that VeX) = L(X(s) -E(X))2p(s) seS = LX(S)2p(S) -2E(X) LX(s)p(s) + E(Xf LP(s) seS seS = E(X2) -2E(X)E(X) + E(X)2 = E(X2) -E(Xf. seS We have used the fact that Lses p(s) = 1 in the next-to-last step. <l EXAMPLE 14 What is the variance of the random variable X with X(t) = I if a Bernoulli trial is a success and X (t) = 0 if it is a failure, where p is the probability of success? Extra � Solution: Because X takes only the values 0 and 1, it follows that X2(t) = X(t). Hence, Examples� VeX) = E(X2) -E(X)2 = P -p2 = pel -p) = pq. EXAMPLE 15 Variance of the Valne of a Die What is the variance of the random variable X, where X is the number that comes up when a die is rolled? 6-45 6.4 Expected Value and Variance 437 Solution: We have VeX) = E(X2) -E(xi. By Example 1 we know that E(X) = 7/2. To find E(X2) note that X2 takes the values i2, i = 1,2, ... ,6, each with probability 1/6. It follows that We conclude that 91 (7)2 35 VeX) = 6 -2 = 12 EXAMPLE 16 What is the variance of the random variable X((i, j» = 2i, where i is the number appearing on the first die and j is the number appearing on the second die, when two dice are rolled? Solution: We will use Theorem 6 to find the variance of X. To do so, we need to find the expected values of X and X2. Note that because p(X = k) is 1/6 for k = 2,4,6,8, 10, 12 and is 0 otherwise, E(X) = (2 + 4 + 6 + 8 + 10 + 12)/6 = 7, and It follows from Theorem 6 that VeX) = E(X2) -E(X)2 = 182/3 -49 = 35/3. Another useful fact about variances is that the variance of the sum of two independent random variables is the sum of their variances. This result is useful for computing the variance of the result of n independent Bernoulli trials, for instance. THEOREM 7 If X and Yare two independent random variables on a sample space S, then VeX + Y) = V (X) + V (Y). Furthermore, if Xi, i = 1,2, ... , n, with n a positive integer, are pairwise in­ dependent random variables on S, then VeX, + X2 + ... + Xn) = VeX,) + V(X2) + ... + V(Xn). Proof: From Theorem 6, we have VeX + Y) = E((X + y)2) -E(X + y)2. It follows that VeX + Y) = E(X2 + 2XY + y2) -(E(X) + E(Y)i = E(X2) + 2E(XY) + E(y2) -E(X)2 -2E(X)E(Y) -E(Yi. Because X and Yare independent, by Theorem 5 we have E(XY) = E(X)E(Y). It follows that VeX + Y) = (E(X2) -E(Xi) + (E(y2) -E(Y)2) = VeX) + V(Y). 438 6 / Discrete Probability 6-46 We leave the proof of the case with n pairwise independent random variables to the reader (Exercise 28). Such a proof can be constructed by generalizing the proof we have given for the case for two random variables. Note that it is not possible to use mathematical induction in a straightforward way to prove the general case (see Exercise 27). <I EXAMPLE 17 Find the variance and standard deviation of the random variable X whose value when two dice are rolled is X((i, j» = i + j, where i is the number appearing on the first die and j is the number appearing on the second die. Solution: Let X I and X 2 be the random variables defined by X I ((i, j» = i and X 2( (i, j» = j for a roll of the dice. Then X = X I + X2, and X I and X2 are independent, as Example 11 showed. From Theorem 7 it follows that VeX) = V(XI) + V(X2). A simple computation as in Example 16, together with Exercise 25 in the Supplemen tary Exer cises at the end of the chapter, tells us that V(XI) = V(X2) = 35/12. Hence, VeX) = 35/12 + 35/12 = 35/6 and u(X) = J35/6. � We will now find the variance of the random variable that counts the number of successes when n independent Bernoulli trials are carried out. EXAMPLE 18 What is the variance of the number of successes when n independent Bernoulli trials are per­ formed, where p is the probability of success on each trial? Linl(S� Solution: Let X; be the random variable with X;((tl' t2, ... , tn» = 1 if t; is a success and X;((tl' t2, ... , tn» = 0 ift; is a failure. Let X = XI + X2 + ... + Xn. Then X counts the num- ber of successes in the n trials. From Theorem 7 it follows that V (X) = V (X I) + V (X 2) + ... + V(Xn). Using Example 14 we have VeX;) = pq for i = 1,2, ... , n. It follows that VeX) = npq. � Chebyshev's Inequalit y How likely is it that a random variable takes a value far from its expected value? Theorem 8, called Chebyshev's Inequality, helps answer this question by providing an upper bound on the probability that the value of a random variable differs from the expected value of the random variable by more than a specified amount. PAFNUTY LVOVICH CHEBYSHEV (1821-1894) Chebyshev was born into the gentry in Okatovo, Russia. His father was a retired army officer who had fought against Napoleon. In 1832 the family, with its nine children, moved to Moscow, where Pafnuty completed his high school education at home. He entered the Department of Physics and Mathematics at Moscow University. As a student, he developed a new method for approximating the roots of equations. He graduated from Moscow University in 1841 with a degree in mathemati cs, and he continued his studies, passing his master's exam in 1843 and completing his master's thesis in 1846. Chebyshev was appointed in 1847 to a position as an assistant at the University of St. Petersburg. He wrote and defended a thesis in 1847. He became a professor at St. Petersburg in 1860, a position he held until 1882. His book on the theory of congruences written in 1849 was influential in the development of number theory. His work on the distribution of prime numbers was seminal. He proved Bertrand's conjecture that for every integer n > 3, there is a prime between n and 2n - 2. Chebyshev helped develop ideas that were later used to prove the Prime Number Theorem. Chebyshev's work on the approximation of functions using polynomials is used extensively when computers are used to find values of functions. Chebyshev was also interested in mechanics. He studied the conversion of rotary motion into rectilinear motion by mechanical coupling. The Chebyshev parallel motion is three linked bars approximating rectilinear motion. 6-47 THEOREM 8 6.4 Expected Value and Variance 439 CHEBYSHEV 'S INEQUALITY Let X be a random variable on a sample space S with probability function p. If r is a positive real number, then p(lX(s) -E(X)I ::: r) :::: V(X)/r2. Proof: Let A be the event A = {s E S IIX(s) -E(X)I::: r}. What we want to prove is that p(A) :::: V(X)/r2. Note that V(X) = L(X(s) -E(X))2 p(s) SES = L(X(s) -E(X)2 p(s) + L(X(s) -E(X)2 p(s). SEA s¢A The second sum in this expression is nonnegative, because each of its summands is nonnegative. Also, because for each element s in A, (X(s) -E(X)2 ::: r2, the first sum in this expression is at least LSEA r2 p(s). Hence, V(X) ::: LSEA r2 p(s) = r2 p(A).1t follows that V(X)/r2 ::: p(A), so p(A) :::: V(X)/r2, completing the proof. <l EXAMPLE 19 Deviation from the Mean when Counting Tails Suppose that X is the random variable that counts the number of tails when a fair coin is tossed n times. Note that X is the number of successes when n independent Bernoulli trials, each with probability of success 1/2, are performed. It follows that E(X) = n/2 (by Theorem 2) and V(X) = n/4 (by Example 18). Applying Chebyshev's Inequality with r = In shows that p(IX(s) -n/21 ::: In) :::: (n/4)/(Jn)2 = 1/4. Consequently, the probability is no more than 1/4 that the number of tails that come up when a fair coin is tossed n times deviates from the mean by more than In. ... Chebyshev's Inequality, although applicable to any random variable, often fails to provide a practical estimate for the probability that the value of a random variable exceeds its mean by a large amount. This is illustrated by Example 20. EXAMPLE 20 Let X be the random variable whose value is the number appearing when a fair die is rolled. We have E(X) = 7/2 (see Example 1) and V(X) = 35/12 (see Example 15). Because the only possible values of X are 1, 2, 3, 4, 5, and 6, X cannot take a value more than 5/2 from its mean, E(X) = 7/2. Hence, p(IX -7/21 ::: r) = 0 if r > 5/2. By Chebyshev's Inequality we know that p(IX -7/21 ::: r) :::: (35/12)/r2. For example, when r = 3, Chebyshev's Inequality tells us that p(IX -7/21 ::: 3) :::: (35/12)/9 = 35/108, which is a poor estimate, because p(IX -7/21 ::: 3) = O. ... Exercises 1. What is the expected number of heads that come up when a fair coin is flipped five times? 2. What is the expected number of heads that come up when a fair coin is flipped 10 times? 3. What is the expected number of times a 6 appears when a fair die is rolled 10 times? 4. A coin is biased so that the probability a head comes up when it is flipped is 0.6. What is the expected number of heads that come up when it is flipped 10 times? 440 6 / Discrete Probability 5. What is the expected sum of the numbers that appear on two dice, each biased so that a 3 comes up twice as often as each other number? 6. What is the expected value when a $1 lottery ticket is bought in which the purchaser wins exactly $10 million if the ticket contains the six winning numbers chosen from the set {I, 2, 3, ... , SO} and the purchaser wins nothing otherwise? 7. The final exam of a discrete mathematics course con­ sists of SO true/false questions, each worth two points, and 2S multiple-choice questions, each worth four points. The probability that Linda answers a true/false question correctly is 0.9, and the probability that she answers a multiple-choice question correctly is 0.8. What is her ex­ pected score on the final? 8. What is the expected sum ofthe numbers that appear when three fair dice are rolled? 9. Suppose that the probability that x is in a list of n distinct integers is 2/3 and that it is equally likely that x equals any element in the list. Find the average number of com­ parisons used by the linear search algorithm to find x or to determine that it is not in the list. 10. Suppose that we flip a coin until either it comes up tails twice or we have flipped it six times. What is the expected number of times we flip the coin? 11. Suppose that we roll a die until a 6 comes up or we have rolled it 10 times. What is the expected number of times we roll the die? 12. Suppose that we roll a die until a 6 comes up. a) What is the probability that we roll the die n times? b) What is the expected number oftimes we roll the die? 13. Suppose that we roll a pair of dice until the sum of the numbers on the dice is seven. What is the expected number of times we roll the dice? 14. Show that the sum of the probabilities of a random vari­ able with geometric distribution with parameter p, where o < p :'S I, equals 1. 15. Show that if the random variable X has the geometric distribution with parameter p, and j is a positive integer, then p(X ::: j) = (1 _ py-I. 16. Let X and Y be the random variables that count the number of heads and the number of tails that come up when two coins are flipped. Show that X and Yare not independent. 17. Estimate the expected number of integers with 1000 dig­ its that need to be selected at random to find a prime, if the probability a number with 1000 digits is prime is approximately 112302. 18. Suppose that X and Yare random variables and that X and Yare nonnegative for all points in a sample space S. Let Z be the random variable defined by Z(s) = max(X(s), Y(s» forallelementss E S. Show that E(Z) :'S E(X) + E(Y). 19. Let X be the number appearing on the first die when two dice are rolled and let Y be the sum of the numbers ap­ pearing on the two dice. Show that E(X)E(Y) i= E(XY). 6-48 20. Let A be an event. Then lA, the indicator random vari­ able of A, equals I if A occurs and equals 0 otherwise. Show that the expectation of the indicator random vari­ able of A equals the probability of A, that is, E(lA) = p(A). 21. A run is a maximal sequence of successes in a se­ quence of Bernoulli trials. For example, in the sequence S. S, S, F, S, S, F, F, S, where S represents success and F represents failure, there are three runs consisting of three successes, two successes, and one success, respec" tively. Let R denote the random variable on the set of se­ quences of n independent Bernoulli trials that counts the number of runs in this sequence. Find E(R). [Hint: Show that R = L�=I lj, where lj = I if a run begins at the jth Bernoulli trial and lj = 0 otherwise. Find E(lJ) and then find E(lj), where I < j :'S n.] 22. Let X (s) be a random variable, where X (s) is a nonneg­ ative integer for all s E S, and let Ak be the event that X(s) ::: k. Show that E(X) = L�I p(Ad. 23. What is the variance of the number of heads that come up when a fair coin is flipped 10 times? 24. What is the variance ofthe number of times a 6 appears when a fair die is rolled 10 times? 25. Let X n be the random variable that equals the number of tails minus the number of heads when n coins are flipped. a) What is the expected value of Xn? b) What is the variance of Xn? 26. Provide an example that shows that the variance of the sum of two random variables is not necessarily equal to the sum of their variances when the random variables are not independent. 27. Suppose that X I and X 2 are independent Bernoulli trials each with probability 112, and that X 3 = (XI + Xz)mod2. a) Show that XI, Xz, and X3 are pairwise independent, but X 3 and X I + X 2 are not independent. b) Show that V(XI + X2 + X3) = V(XJ) + V(X2) + V(X3). c) Explain why a proof by mathematical induction of Theorem 7 does not work by considering the random variables X], Xz, and X3. *28. Prove the general case of Theorem 7. That is, show that if X I, X z, ... , X n are pairwise independent ran­ dom variables on a sample space S, where n is a pos­ itive integer, then V(XI + X2 + ... + Xn) = V(XI) + V(X2) + ... + V(Xn). [Hint: Generalize the proof given in Theorem 7 for two random variables. Note that a proof using mathematical induction does not work; see Exercise 27.] 29. Use Chebyshev's Inequality to find an upper bound on the probability that the number of tails that come up when a fair coin is tossed n times deviates from the mean by more than s.jii. 30. Use Chebyshev's Inequality to find an upper bound on the probability that the number of tails that come up when a 6-4':) biased coin with probability of heads equal to 0.6 is tossed n times deviates from the mean by more than .;n. 31. Let X be a random variable on a sample space S such that X(s)::: o for all s E S. Showthatp(X(s)::: a):s E(X)la for every positive real number a. This inequality is called Markov's Inequali ty. 32. Suppose that the number of cans of soda pop filled in a day at a bottling plant is a random variable with an expected value of 10,000 and a variance of 1000. a) Use Markov's Inequality (Exercise 31) to obtain an upper bound on the probability that the plant will fill more than 11,000 cans on a particular day. b) Use Chebyshev's Inequality to obtain a lower bound on the probability that the plant will fill between 9000 and 11,000 cans on a particular day. 33. Suppose that the number of tin cans recycled in a day at a recycling center is a random variable with an expected value of 50,000 and a variance of 10,000. a) Use Markov's Inequality (Exercise 31) to find an upper bound on the probability that the center will recycle more than 55,000 cans on a particular day. b) Use Chebyshev's Inequality to provide a lower bound on the probability that the center will recycle 40,000 to 60,000 cans on a certain day. +=34. Suppose the probability that x is the ith element in a list of n distinct integers is i I[n(n + 1)]. Find the average num­ ber of comparisons used by the linear search algorithm to find x or to determine that it is not in the list. +=35. In this exercise we derive an estimate of the average-case complexity of the variant of the bubble sort algorithm that terminates once a pass has been made with no inter­ changes. Let X be the random variable on the set of per­ mutations of a set of n distinct integers {a" az, ... ,an} with a, < az < ... < an such that X(P) equals the num­ ber of comparisons used by the bubble sort to put these integers into increasing order. a) Show that, under the assumption that the input is equally likely to be any of the n! permutations ofthese integers, the average number of comparisons used by the bubble sort equals E(X). b) Use Example 5 in Section 3.3 to show that E(X) :s n(n -1)/2. c) Show that the sort makes at least one comparison for every inversion of two integers in the input. d) Let I (P) be the random variable that equals the num­ ber of inversions in the permutation P. Show that E(X) ::: E(I). e) Let Ij,k be the random variable with Ij,k(P) = 1 if ak precedes aj in P and Ij,k = 0 otherwise. Show that I(P) = Lk Lj<k Ij,k(P), I) Show that E(I) = Lk Lj<k E(lj,k)' g) Show that E(lj,k) = 1/2. [Hint: Show that E(lj,k) = probability that ak precedes a j in a permutation P. Then show it is equally likely for ak to precede a j as it is for a j to precede ak in a permutation,] h) Use parts (f) and (g) to show that E(I) = n(n -1)/4. 6.4 Expected Value and Variance 441 i) Conclude from parts (a), (b), and (h) that the aver­ age number of comparisons used to sort n integers is B(nz). *36. In this exercise we find the average-case complexity of the quick sort algorithm, described in the preamble to Exercise 50 in Section 4.4, assuming a uniform distribu­ tion on the set of permutations. a) Let X be the number of comparisons used by the quick sort algorithm to sort a list of n distinct integers. Show that the average number of comparisons used by the quick sort algorithm is E(X) (where the sample space is the set of all n! permutations of n integers). b) Let Ij,k denote the random variable that equals 1 if the ith smallest element and the kth smallest element of the initial list are ever compared as the quick sort algorithm sorts the list and equals 0 otherwise. Show that X = L�=2 L�:: Ij,k. c) Show that E(X) = L�=z L�:: p (the ith smallest element and the kth smallest element are compared). d) Show that p (the ith smallest element and the kth smallest element are compared), where k > i, equals 2/(k -i + 1). e) Use parts (c) and (d) to show that E(X) = 2(n + I)(L7=2Ili) -2(n -1). I) Conclude from part (e) and the fact that L�=' Iii :::::: Inn + y, where y = 0.57721 ... is Euler's constant, that the average number of comparisons used by the quick sort algorithm is B(n log n). *37. What is the variance of the number of fixed elements, that is, elements left in the same position, of a randomly selected permutation ofn elements? [Hint: Let X denote the number of fixed points of a random permutation. Write X = X, + X2 + ... + Xn, where Xi = 1 if the permuta­ tion fixes the ith element and Xi = 0 otherwise.] The covariance of two random variables X and Y on a sample space S, denoted by Cov(X, y), is defined to be the expected value of the random variable (X -E(X»(Y -E(Y». That is, Cov(X, Y) = E«X -E(X»(Y -E(Y»). 38. Show that Cov(X, Y) = E(XY) -E(X)E(Y), and use this result to conclude that Cov(X, Y) = 0 if X and Y are independent random variables. 39. Show that VeX + Y) = VeX) + V(Y) + 2 Cov(X, Y). 40. Find Cov(X, Y) if X and Yare the random variables with X«i, i» = 2i and Y«i, i» = i + i, where i and i are the numbers that appear on the first and second of two dice when they are rolled. 41. When m balls are distributed into n bins uniformly at random, what is the probability that the first bin remains empty? 42. What is the expected number of balls that fall into the first bin when m balls are distributed into n bins uniformly at random? 43. What is the expected number of bins that remain empty when m balls are distributed into n bins uniformly at random? 442 6 / Discrete Probability Key Terms and Results TERMS sample space: the set of possible outcomes of an experiment event: a subset ofthe sample space of an experiment probability of an event (Laplace's definition): the number of successful outcomes of this event divided by the number of possible outcomes probability distributio n: a function p from the set of all out­ comes of a sample space S for which 0 � P(Xi) � I for i = 1,2, ... , nand L7=1 P(Xi) = 1, where XI are the pos­ sible outcomes probability of an event E: the sum of the probabilities of the outcomes in E p(EIF) (conditional probability of E given F): the ratio peE n F)/ p(F) independent events: events E and F such that peE n F) = p(E)p(F) random variable: a function that assigns a real number to each outcome of an experiment distribution of a random variable X: the set of pairs (r, p(X = r» for r E XeS) uniform distribut ion: the assignment of equal probabilities to the elements of a finite set expected value of a random variable: the weighted av­ erage of a random variable, with values of the random variable weighted by the probability of outcomes, that is, E(X) = LSES p(s)X(s) geometric distributio n: the distribution of a random variable X such that p(X = k) = (1 -p)k-Ip for k = 1,2, ... for somep independent random variables: random variables X and Y such that p(X(s) = rl and Yes) = rl) = p(X(s) = rl)p(Y(s) = rl) for all real numbers rl and rl variance of a random variable: the weighted average of the square of the difference between the value of the Review Questions 1. a) Define the probability of an event when all outcomes are equally likely. b) What is the probability that you select the six winning numbers in a lottery ifthe six different winning num­ bers are selected from the first 50 positive integers? 2� a) What conditions should be met by the probabilities assigned to the outcomes from a finite sample space? b) What probabilities should be assigned to the outcome of heads and the outcome of tails if heads comes up three times as often as tails? 3. a) Define the conditional probability of an event E given an event F. b) Suppose E is the event that when a die is rolled it comes up an even number, and F is the event that when a die is rolled it comes up 1,2, or 3. What is the probability of F given E? 6-50 random variable and its expected value, with weights given by the probability of outcomes, that is, VeX) = LSES(X(S) -E(X»l pes) Bernoulli trial: an experiment with two possible outcomes probabilistic (or Monte Carlo) algorithm: an algorithm in which random choices are made at one or more steps probabilistic method: a technique for proving the existence of objects in a set with certain properties that proceeds by assigning probabilities to objects and showing that the prob­ ability that an object has these properties is positive RESULTS The probability of k successes when n independent Bernoulli trials are carried out equals C(n, k)pkqn-k, where p is the probability of success and q = I -P is the probability of failure. Bayes' Theorem: If E and F are events from a sample space S such that pee) =1= 0 and p(F) =1= 0, then (F I E) _ peE I F)p(F) P -peE I F)p(F) + peE I F)p(F) E(X) = LrEX(S)P(X = r)r. linearity of expectatio ns: E(XI +Xl + ... + Xn)= E(Xd+ E(Xl) + ... + E(Xn) if XI, Xl,"" Xn are random variables If X and Yare independent random variables, then E(XY) = E(X)E(Y). If X I, Xl, ... , Xn are independent random variables, then V(XI + Xl + ... + Xn) = V(XI) + V(Xl) + ... + V(Xn). Chebyshev's Inequality: p(IX(s) -E(X)I ::: r) � V(X)/rl, where X is a random variable with probability function p and r is a positive real number. 4. a) When are two events E and F independent? b) Suppose E is the event that an even number appears when a fair die is rolled, and F is the event that a 5 or 6 comes up. Are E and F independent? 5. a) What is a random variable? b) What are the values assigned by the random variable X that assigns to a roll of two dice the larger number that appears on the two dice? 6. a) Define the expected value of a random variable X. b) What is the expected value of the random variable X that assigns to a roll of two dice the larger number that appears on the two dice? 7. a) Explain how the average-case computational com­ plexity of an algorithm, with finitely many possi­ ble input values, can be interpreted as an expected value. 6-51 b) What is the average-case computational complexity of the linear search algorithm, if the probability that the element for which we search is in the list is 1/3, and it is equally likely that this element is any of the n elements in the list? 8. a) What is meant by a Bernoulli trial? b) What is the probability of k successes in n independent Bernoulli trials? c) What is the expected value of the number of successes in n independent Bernoulli trials? 9. a) What does the linearity of expectations of random variables mean? b) How can the linearity of expectations help us find the expected number of people who receive the correct hat when a hatcheck person returns hats at random? 10. a) How can probability be used to solve a decision prob­ lem, if a small probability of error is acceptable? b) How can we quickly determine whether a positive integer is prime, if we are willing to accept a small probability of making an error? Supplementar y Exercises 1. What is the probability that six consecutive numbers will be chosen as the winning numbers in a lottery where each number chosen is between I and 40 (inclusive)? 2. What is the probability that a hand of 13 cards contains no pairs? 3. What is the probability that a 13-card bridge hand con­ tains a) all 13 hearts? b) 13 cards of the same suit? c) seven spades and six clubs? d) seven cards of one suit and six cards of a second suit? e) four diamonds, six hearts, two spades, and one club? 1) four cards of one suit, six cards of a second suit, two cards of a third suit, and one card of the fourth suit? 4. What is the probability that a seven-card poker hand con­ tains a) four cards of one kind and three cards of a second kind? b) three cards of one kind and pairs of each of two dif­ ferent kinds? c) pairs of each ofthree different kinds and a single card of a fourth kind? d) pairs of each of two different kinds and three cards of a third, fourth, and fifth kind? e) cards of seven different kinds? 1) a seven-card flush? g) a seven-card straight? h) a seven-card straight flush? An octahedral die has eight faces that are numbered 1 through 8. Supplementary Exercises 443 11. State Bayes' Theorem and use it to find p(F I E) if peE I F) = 1/3, peE I "F') = 1/4, and p(F) = 2/3, where E and F are events from a sample space S. 12. a) What does it mean to say that a random variable has a geometric distribution with probability p? b) What is the mean of a geometric distribution with probability p? 13. a) What is the variance of a random variable? b) What is the variance of a Bernoulli trial with proba­ bility p of success? 14. a) What is the variance ofthe sum ofn independent ran­ dom variables? b) What is the variance of the number of successes when n independent Bernoulli trials, each with probability p of success, are carried out? 15. What does Chebyshev's Inequality tell us about the prob­ ability that a random variable deviates from its mean by more than a specified amount? 5. a) What is the expected value of the number that comes up when a fair octahedral die is rolled? b) What is the variance ofthe number that comes up when a fair octahedral die is rolled? A dodecahedral die has 12 faces that are numbered 1 through 12. 6. a) What is the expected value of the number that comes up when a fair dodecahedral die is rolled? b) What is the variance of the number that comes up when a fair dodecahedral die is rolled? 7. Suppose that a pair off air octahedral dice is rolled. a) What is the expected value of the sum of the numbers that come up? b) What is the variance of the sum of the numbers that come up? 8. Suppose that a pair of fair dodecahedral dice is rolled. a) What is the expected value of the sum of the numbers that come up? b) What is the variance of the sum of the numbers that come up? 9. Suppose that a fair standard (cubic) die and a fair octahe­ dral die are rolled together. a) What is the expected value of the sum ofthe numbers that come up? b) What is the variance of the sum of the numbers that come up? 10. Suppose that a fair octahedral die and a fair dodecahedral die are rolled together. a) What is the expected value of the sum ofthe numbers that come up? 444 6 / Discrete Probability b) What is the variance of the sum of the numbers that come up? 11. Suppose n people, n � 3, play "odd person out" to de­ cide who will buy the next round of refreshments. The n people each flip a fair coin simultaneousl y. If all the coins but one come up the same, the person whose coin comes up different buys the refreshments. Otherwise, the people flip the coins again and continue until just one coin comes up different from all the others. a) What is the probability that the odd person out is de­ cided in just one coin flip? b) What is the probability that the odd person out is de­ cided with the kth flip? c) What is the expected number of flips needed to decide odd person out with n people? 12. Suppose that p and q are primes and n = pq. What is the probability that a randomly chosen positive integer less than n is not divisible by either p or q? 13. Suppose that m and n are positive integers. What is the probability that a randomly chosen positive integer less than mn is not divisible by either morn? 14. Suppose that EI, E2, ... , En are n events withp(Ei) > 0 for i = 1,2, ... , n. Show that p(EI n E2 n··· n En) = P(EI)P(E2 I EI)P(E3 lEI n E2) . . . p(En I EI n E2 n ... n En-I). 15. There are three cards in a box. Both sides of one card are black, both sides of one card are red, and the third card has one black side and one red side. We pick a card at random and observe only one side. a) If the side is black, what is the probability that the other side is also black? b) What is the probability that the opposite side is the same color as the one we observed? 16. What is the probability that when a fair coin is flipped n times an equal number of heads and tails appear? 17. What is the probability that a randomly selected bit string of length lOis a palindrome? 18. What is the probability that a randomly selected bit string of length 11 is a palindrome? 19. Consider the following game. A person flips a coin re­ peatedly until a head comes up. This person receives a payment of2n dollars ifthe first head comes up at the nth flip. a) Let X be a random variable equal to the amount of money the person wins. Show that the expected value of X does not exist (that is, it is infinite). Show that a rational gambler, that is, someone willing to pay to play the game as long as the price to play is not more than the expected payoff, should be willing to wager any amount of money to play this game. (This is known as the St. Petersburg Paradox. Why do you suppose it is called a paradox?) 6-52 b) Suppose that the person receives 2n dollars if the first head comes up on the nth flip where n < 8 and 28 = 256 dollars if the first head comes up on or after the eighth flip. What is the expected value of the amount of money the person wins? How much money should a person be willing to pay to play this game? 20. Suppose that n balls are tossed into b bins so that each ball is equally likely to fall into any of the bins and that the tosses are independent. a) Find the probability that a particular ball lands in a specified bin. b) What is the expected number of balls that land in a particular bin? c) What is the expected number of balls tossed until a particular bin contains a ball? * d) What is the expected number of balls tossed until all bins contain a ball? [Hint: Let Xi denote the number of tosses required to have a ball land in an ith bin once i-I bins contain a ball. Find E(Xi) and use the linearity of expectations.] 21. Suppose that A and B are events with probabilities p(A) = 3/4 and p(B) = 1/3. a) What is the largest p(A n B) can be? What is the smallest it can be? Give examples to show that both extremes for p(A n B) are possible . b) What is the largest p(A U B) can be? What is the smallest it can be? Give examples to show that both extremes for p(A U B) are possible. 22. Suppose that A and B are events with probabilities p(A) = 2/3 and p(B) = 1/2. a) What is the largest p(A n B) can be? What is the smallest it can be? Give examples to show that both extremes for p(A n B) are possible. b) What is the largest p(A U B) can be? What is the smallest it can be? Give examples to show that both extremes for p(A U B) are possible. 23. We say that the events E I , E 2, ... , En are mu­ tually independent if P(Ei\ n Ei, n ... n Eim) = P(Ei\)p(Ei,)··· P(Eim) whenever ij, j = 1,2, ... , m, are integers with 1 :s il < i2 < ... < im :s n andm � 2. a) Write out the conditions required for three events E I , E 2, and E 3 to be mutually independent. b) Let E J, E2, and E3 be the events that the first flip comes up heads, that the second flip comes up tails, and that the third flip comes up tails, respectively, when a fair coin is flipped three times. Are EI, E2, and E3 mutually independent? c) Let E I, E 2, and E 3 be the events that the first flip comes up heads, that the third flip comes up heads, and that an even number of heads come up, respec­ tively, when a fair coin is flipped three times. Are E I , E2, and E3 pairwise independent? Are they mutually independent? 6-53 d) Let E I, E 2, and E 3 be the events that the first flip comes up heads, that the third flip comes up heads, and that exactly one of the first flip and third flip come up heads, respectively, when a fair coin is flipped three times. Are E I, E2, and E3 pairwise independent? Are they mutually independent? e) How many conditions must be checked to show that n events are mutually independent? 24. Suppose that A and B are events from a sample space S such that p(A) =1= 0 and p(B) =1= O. Show that if p(B I A) < p(B), then p(A I B) < p(A). 25. Let X be a random variable on a sample space S. Show that V(aX + b) = a2V(X) whenever a and b are real num­ bers. 26. Use Chebyshev's Inequality to show that the probabil­ ity that more than 10 people get the correct hat back when a hatcheck person returns hats at random does not exceed 1 I 100 no matter how many people check their hats. [Hint: Use Example 6 and Exercise 37 in Section 6.4.] 27. Suppose that at least one of the events Ej, j = 1, 2, ... , m, is guaranteed to occur and no more than two can occur. Show that if p(Ej) = q for j = 1,2, ... , m and p(Ej n Ek) = r for 1 ::s j < k ::s m, then q 2: 11m andr ::s 21m. 28. Show that if m is a positive integer, then the probability that the mth success occurs on the (m + n )th trial when independent Bernoulli trials, each with probability p of success, are run, is (n+�-I )qn pm. 29. There are n different types of collectible cards you can get as prizes when you buy a particular product. Suppose that every time you buy this product it is equally likely that you get any type of these cards. Let X be the random variable equal to the number of products that need to be purchased to obtain at least one of each type of card and let X j be the random variable equal to the number of ad­ ditional products that must be purchased after j different cards have been collected until a new card is obtained for j = 0, I, ... ,n -I. a) Show that X = L�:,IO Xj. b) Show that after j distinct types of cards have been obtained, the card obtained with the next purchase will be a card of a new type with probability (n -j)ln. c) Show that X j has a geometric distribution with pa­ rameter (n -j)1 n. d) Use parts (a) and (c) to show that E(X) = n L�=I Ilj· e) Use the approximation L�=I Ilj � Inn + y, where y = 0.57721 ... is Euler's constant, to find the ex­ pected number of products that you need to buy to get one card of each type if there are 50 different types of cards. 30. The maximum satisfiability problem asks for an as­ signment of truth values to the variables in a compound Supplementary Exercises 445 proposition in conjunctive normal form (which expresses a compound proposition as the conjunction of clauses where each clause is the disjunction of two or more vari­ ables or their negations) that makes as many of these clauses true as possible. For example, three but not four of the clauses in (p v q) 1\ (p V -.q) 1\ (-'P v r) 1\ (-'P v -.r) can be made true by an assignment of truth values to p, q, and r. We will show that probabilistic methods can provide a lower bound for the number of clauses that can be made true by an assignment of truth values to the variables. a) Suppose that there are n variables in a compound proposition in conjunctive normal form. If we pick a truth value for each variable randomly by flipping a coin and assigning true to the variable if the coin comes up heads and false if it comes up tails, what is the probability of each possible assignment of truth values to the n variables? b) Assuming that each clause is the disjunction of exactly two distinct variables or their negations, what is the probability that a given clause is true, given the random assignment of truth values from part (a)? c) Suppose that there are D clauses in the compound proposition. What is the expected number of these clauses that are true, given the random assignment of truth values of the variables? d) Use part (c) to show that for every compound proposi­ tion in conjunctive normal form there is an assignment of truth values to the variables that makes at least 3/4 of the clauses true. 31. What is the probability that each player has a hand con­ taining an ace when the 52 cards of a standard deck are dealt to four players? *32. The following method can be used to generate a ran­ dom permutation of a sequence of n terms. First, inter­ change the nth term and the r(n)th term where r(n) is a randomly selected integer with 1 ::s r(n) ::s n. Next, in­ terchange the (n -I)st term of the resulting sequence with its r(n -l)st term where r(n -1) is a randomly selected integer with 1 ::s r(n -1) ::s n -1. Continue this process until j = n, where at the jth step you in­ terchange the (n -j + I)st term of the resulting se­ quence with its r(n -j + l)st term, where r(n -j + 1) is a randomly selected integer with 1 ::s r(n -j + I) ::s n -j + 1. Show that when this method is followed, each of the n! different permutations of the terms of the sequence is equally likely to be generated. [Hint: Use mathematical induction, assuming that the probability that each of the permutations of n - 1 terms produced by this procedure for a sequence of n -1 terms is I/(n -I)!.] 446 6 / Discrete Probability Computer Projects Write programs with these input and output. 1. Given a real number p with 0 :'S p :'S 1, generate random numbers taken from a Bernoulli distribution with proba­ bility p. 2. Given a positive integer n, generate a random permuta­ tion of the set {I, 2, 3, ... , n}. (See Exercise 32 in the Supplementary Exercises at the end ofthis chapter.) 3. Given positive integers m and n, generate m random per­ mutations of the first n positive integers. Find the number of inversions in each permutation and determine the av­ erage number ofthese inversions. 4. Given a positive integer n, simulate n repeated flips of a biased coin with probability p of heads and determine the number of heads that come up. Display the cumulative results. 5. Given positive integers nand m, generate m random per­ mutations of the first n positive integers. Sort each per­ mutation using the insertion sort, counting the number of comparisons used. Determine the average number of comparisons used over all m permutations. 6. Given positive integers nand m, generate m random per­ mutations ofthe first n positive integers. Sort each permu­ tation using the version of the bubble sort that terminates Computations and Explorations 6-54 when a pass has been made with no interchanges, counting the number of comparisons used. Determine the average number of comparisons used over all m permutations. 7. Given a positive integer m, simulate the collection of cards that come with the purchase of products to find the number of products that must be purchased to obtain a full set of m different collector cards. (See Supplementary Exercise 29.) 8. Given positive integers m and n, simulate the placement of n keys, where a record with key k is placed at location h(k) = k mod m and determine whether there is at least one collision. 9. Given a positive integer n, find the probability of select­ ing the six integers from the set {I, 2, ... , n} that were mechanically selected in a lottery. 10. Simulate repeated trials of the Monty Hall Three-Door problem (Example lOin Section 6.1) to calculate the prob­ ability of winning with each strategy. 11. Given a list of words and the empirical probabilities they occur in spam e-mails and in e-mails that are not spam, determine the probability that a new e-mail message is spam. Use a computational program or programs you have written to do these exercises. 1. Find the probabilities of each type of hand in five-card poker and rank the types of hands by their probability. 2. Find some conditions such that the expected value of buy­ ing a $1 lottery ticket in the New Jersey Pick-61ottery has an expected value of more than $1. To win you have to se­ lect the six numbers drawn, where order does not matter, from the positive integers 1 to 49, inclusive. The winnings are split evenly among holders of winning tickets. Be sure to consider the total size of the pot going into the drawing and the number of people buying tickets. 3. Estimate the probability that two integers selected at ran­ dom are relatively prime by testing a large number of randomly selected pairs of integers. Look up the theorem that gives this probability and compare your results with the correct probability. 4. Determine the number of people needed to ensure that the probability at least two of them have the same day of the year as their birthday is at least 70%, at least 80%, at least 90%, at least 95%, at least 98%, and at least 99%. 5. Generate a list of 100 randomly selected permutations of the set of the first 100 positive integers. (See Exer­ cise 32 in the Supplementary Exercises at the end of this chapter.) 6. Given a collection of e-mail messages, each determined to be spam or not to be spam, develop a Bayesian fil­ ter based on the appearance of particular words in these messages. 7. Simulate the odd-person-out procedure (described in Ex­ ercise 11 of the Supplementary Exercises) for n people with 3 :'S n :'S 10. Run a large number of trials for each value of n and use the results to estimate the expected number of flips needed to find the odd person out. Does your result agree with that found in Exercise 29 in Sec­ tion 6.2? Vary the problem by supposing that exactly one person has a biased coin with probability of heads p ",0.5. 8. Given a positive integer n, simulate a hatcheck person ran­ domly giving hats back to people. Determine the number of people who get the correct hat back. 6-55 Writing Projects Respond to these with essays using outside sources. 1. Describe the origins of probability theory and the first uses of this theory. 2. Describe the different bets you can make when you play roulette. Find the probability of each of these bets in the American version where the wheel contains the numbers o and 00. Which is the best bet and which is the worst for you? 3. Discuss the probability of winning when you play the game of blackjack versus a casino. Is there a winning strategy for the person playing against the house? 4. Investigate the game of craps and discuss the probability that the shooter wins and how close to a fair game it is. Writing Projects 447 5. Discuss issues involved in developing successful spam fil­ ters and the current situation in the war between spammers and people trying to filter spam out. 6. Discuss the history and solution of what is known as the Newton-Pepys problem, which asks which is most likely: rolling at least one six when six dice are rolled, rolling at least two sixes when 12 dice are rolled, or rolling at least three sixes when 18 dice are rolled. 7. Explain how Erdos and Renyi first used the probabilis­ tic method and describe some other applications of this method. S. Discuss the different types of probabilistic algorithms and describe some examples of each type. CHAPTER ·7 I 7.1 Recurrence Relations 7.2 Solving Linear Recurrence Relations 7.3 Divide-and­ Conquer Algorithms and Recurrence Relations 7.4 Generating Functions 7.5 Inclusion­ Exclusion 7.6 Applications of Inclusion­ Exclusion Advanced Counting Techniques Many counting problems cannot be solved easily using the methods discussed in Chapter 5. One such problem is: How many bit strings of length n do not contain two consecutive zeros? To solve this problem, let an be the number of such strings of length n. An argument can be given that shows an+1 = an + an-I. This equation, called a recurrence relation, and the initial conditions al = 2 and a2 = 3 determine the sequence {an}. Moreover, an explicit formula can be found for an from the equation relating the terms ofthe sequence. As we will see, a similar technique can be used to solve many different types of counting problems. We will also see that many counting problems can be solved using formal power series, called generating functions, where the coefficients of powers of x represent terms ofthe sequence we are interested in. Besides solving counting problems, we will also be able to use generating functions to solve recurrence relations and to prove combinatorial identities. Many other kinds of counting problems cannot be solved using the techniques discussed in Chapter 5, such as: How many ways are there to assign seven jobs to three employees so that each employee is assigned at least one job? How many primes are there less than 1000? Both of these problems can be solved by counting the number of elements in the union of sets. We will develop a technique, called the principle of inclusion-exclusion, that counts the number of elements in a union of sets, and we will show how this principle can be used to solve counting problems. The techniques studied in this chapter, together with the basic techniques of Chapter 5, can be used to solve many counting problems. 7.1 Recurrence Relations Assessment � 7-1 Introduction The number of bacteria in a colony doubles every hour. If a colony begins with five bacteria, how many will be present in n hours? To solve this problem, let an be the number of bacteria at the end of n hours. Because the number of bacteria doubles every hour, the relationship an = 2an-1 holds whenever n is a positive integer. This relationship, together with the initial condition ao = 5, uniquely determines an for all nonnegative integers n. We can find a formula for an from this information. Some of the counting problems that cannot be solved using the techniques discussed in Chapter 5 can be solved by finding relationships, called recurrence relations, between the terms of a sequence, as was done in the problem involving bacteria. We will study a variety of counting problems that can be modeled using recurrence relations. We will develop methods in this section and in Section 7.2 for finding explicit formulae for the terms of sequences that satisfy certain types of recurrence relations. Recurrence Relations In Chapter 4 we discussed how sequences can be defined recursive ly. Recall that a recursive definition of a sequence specifies one or more initial terms and a rule for determining subsequent terms from those that precede them. A rule of the latter sort (whether or not it is part of a 449 450 7 / Advanced Counting Techniques 7-2 DEFINITION 1 recursive definition) is called a recurrence relation. Such relations can be used in studying and solving counting problems. A recurrence relation for the sequence {an} is an equation that expresses an in terms of one or more of the previous terms of the sequence, namely, ao, ai, ... , an-I. for all integers n with n ::::: no, where no is a nonnegative integer. A sequence is called a solution of a recurrence relation if its terms satisfy the recurrence relation. There is an important connection between recursion and recurrence relations that we will exploit later in this chapter. A recursive algorithm provides the solution of a problem of size n in terms of the solutions of one or more instances of the same problem of smaller size. Consequently, when we analyze the complexity of a recursive algorithm, we obtain a recurrence relation that expresses the number of operations required to solve a problem of size n in tenns of the number of operations required to solve the problem for one or more instances of smaller SIze. EXAMPLE 1 Let {an} be a sequence that satisfies the recurrence relation an = an-I -an-2 for n = 2,3,4, ... , and suppose that ao = 3 and al = 5. What are a2 and a3? Solution: We see from the recurrence relation that a2 = al -ao = 5 - 3 = 2 and a3 = a2 -al = 2 - 5 = -3. We can find a4, a5, and each successive term in a similar way. .... EXAMPLE 2 Determine whether the sequence {an}, where an = 3n for every nonnegative integer n, is a solution of the recurrence relation an = 2an_1 -an-2 for n = 2, 3, 4, .... Answer the same question where an = 2n and where an = 5. Solution: Suppose that an = 3n for every nonnegative integer n. Then, for n ::::: 2, we see that 2an-1 -an-2 = 2[3(n -1)] -3(n -2) = 3n = an. Therefore, {an}, where an = 3n, is a solu­ tion of the recurrence relation. Supposethatan = 2n for every nonnegative integer n. Notethatao = 1, al = 2, anda2 = 4. Because 2al -ao = 2 . 2 -1 = 3 =1= a2, we see that {an}, where an = 2n, is not a solution of the recurrence relation. Suppose that an = 5 for every nonnegative integer n. Then for n ::::: 2, we see that an = 2an-1 -an-2 = 2·5 - 5 = 5 = an. Therefore, {an}, where an = 5, is a solution of the recurrence relation. .... The initial conditions for a sequence specify the terms that precede the first term where the recurrence relation takes effect. For instance, in Example 1, ao = 3 and al = 5 are the initial conditions. The recurrence relation and initial conditions uniquely determine a sequence. This is the case because a recurrence relation, together with initial conditions, provide a recursive definition of the sequence. Any term of the sequence can be found from the initial conditions using the recurrence relation a sufficient number of times. However, there are better ways for computing the terms of certain classes of sequences defined by recurrence relations and initial conditions. We will discuss these methods in this section and in Section 7.2. Modeling with Recurrence Relations We can use recurrence relations to model a wide variety of problems, such as finding compound interest, counting rabbits on an island, determining the number of moves in the Tower of Hanoi puzzle, and counting bit strings with certain properties. 7-3 EXAMPLE 3 Elltra� Examples IIiiW EXAM PLE 4 unkS� 7.1 Recurrence Relations 451 Compound Interest Suppose that a person deposits $10,000 in a savings account at a bank yielding II % per year with interest compounded annually. How much will be in the account after 30 years? Solution: To solve this problem, let Pn denote the amount in the account after n years. Because the amount in the account after n years equals the amount in the account after n -I years plus interest for the nth year, we see that the sequence {Pn} satisfies the recurrence relation Pn = Pn-I +O.llPn_1 = (l.ll)Pn_l• The initial condition is Po = 10,000. We can use an iterative approach to find a formula for Pn. Note that PI = (1.11)Po P2 = (1.11)PI = (1.11)2Po P3 = (1.11)P2 = (1.11)3 Po Pn = (1.l1)Pn-1 = (1.1 It Po· When we insert the initial condition Po = 10,000, the formula Pn = (1.11)n 10,000 is obtained. We can use mathematical induction to establish its validity. That the formula is valid for n = 0 is a consequence of the initial condition. Now assume that Pn = (1.11)n 10,000. Then, from the recurrence relation and the induction hypothesis, Pn+1 = (1.1I)Pn = (1.11)(1.11t10,000 = (1.11t+11O,000. This shows that the explicit formula for Pn is valid. Inserting n = 30 into the formula Pn = (l.l1t 10,000 shows that after 30 years the account contains P30 = (1.11)3010,000 = $228,922.97. Example 4 shows how the population of rabbits on an island can be modeled using a recurrence relation. Rabbits and the Fibonacci Numbers Consider this problem, which was originally posed by Leonardo Pisano, also known as Fibonacci, in the thirteenth century in his book Liber abaci. A young pair of rabbits (one of each sex) is placed on an island. A pair of rabbits does not breed until they are 2 months old. After they are 2 months old, each pair of rabbits produces another pair each month, as shown in Figure 1. Find a recurrence relation for the number of pairs of rabbits on the island after n months, assuming that no rabbits ever die. Solution: Denote by In the number of pairs of rabbits after n months. We will show that In, n = 1,2,3, ... , are the terms of the Fibonacci sequence. The rabbit population can be modeled using a recurrence relation. At the end of the first month, the number of pairs of rabbits on the island is II = 1. Because this pair does not breed during the second month, h = 1 also. To find the number of pairs after n months, add the number on the island the previous month, In-I, and the number of newborn pairs, which equals In-2, because each newborn pair comes from a pair at least 2 months old. 452 7 / Advanced Counting Techniques 7-4 Reproducing pairs Young pairs Reproducing Young Total (at least two months old) (less than two months old) Month pairs pairs pairs --I 0 I I --2 0 I I -- --3 I I 2 -- --_. 4 I 2 3 _._. -.-.-. 5 2 3 5 _._._. _._._. 6 3 5 8 -. -. FIGURE 1 Rabbits on an Island. Demo� EXAMPLE 5 Consequently, the sequence {In} satisfies the recurrence relation In = In-I + In-2 for n :::: 3 together with the initial conditions II = 1 and fz = 1. Because this recurrence relation and the initial conditions uniquely determine this sequence, the number of pairs of rabbits on the island after n months is given by the nth Fibonacci number. <l1li Example 5 involves a famous puzzle. The Tower of Hanoi A popular puzzle of the late nineteenth century invented by the French mathematician Edouard Lucas, called the Tower of Hanoi, consists of three pegs mounted on a board together with disks of different sizes. Initially these disks are placed on the first peg in order of size, with the largest on the bottom (as shown in Figure 2). The rules of the puzzle allow disks to be moved one at a time from one peg to another as long as a disk is never placed Peg I Peg 2 FIGURE 2 The Initial Position in the Tower of Hanoi. Peg 3 7-5 Peg 1 Peg 2 Peg 3 FIGURE 3 An Intermediate Position in the Tower of Hanoi. 7.1 Recurrence Relations 453 on top of a smaller disk. The goal of the puzzle is to have all the disks on the second peg in order of size, with the largest on the bottom. Let Hn denote the number of moves needed to solve the Tower of Hanoi problem with n disks. Set up a recurrence relation for the sequence {Hn}. Solution: Begin with n disks on peg 1. We can transf er the top n -1 disks, following the rules of the puzzle, to peg 3 using Hn-1 moves (see Figure 3 for an illustration of the pegs and disks at this point). We keep the largest disk fixed during these moves. Then, we use one move to transfer the largest disk to the second peg. We can transf er the n -1 disks on peg 3 to peg 2 using Hn-1 additional moves, placing them on top of the largest disk, which always stays fixed on the bottom of peg 2. Moreover, it is easy to see that the puzzle cannot be solved using fewer steps. This shows that Hn = 2Hn_1 + 1. The initial condition is HI = 1, because one disk can be transferred from peg 1 to peg 2, according to the rules of the puzzle, in one move. We can use an iterative approach to solve this recurrence relation. Note that Hn = 2Hn_1 + 1 = 2(2Hn-2 + 1) + 1 = 22 Hn-2 + 2 + 1 = 22(2Hn_3 + 1) + 2 + 1 = 23 Hn-3 + 22 + 2 + 1 = 2n-1 HI + 2n-2 + 2n-3 + ... + 2 + 1 = 2n-1 + 2n-2 + ... + 2 + 1 = 2n -1. We have used the recurrence relation repeatedly to express Hn in terms of previous terms of the sequence. In the next to last equality, the initial condition HI = 1 has been used. The last equality is based on the formula for the sum of the terms of a geometric series, which can be found in Theorem 1 in Section 2.4. The iterative approach has produced the solution to the recurrence relation Hn = 2Hn_1 + 1 with the initial condition HI = 1. This formula can be proved using mathematical induction. This is left for the reader as an exercise at the end of the section. A myth created to accompa ny the puzzle tells of a tower in Hanoi where monks are trans­ ferring 64 gold disks from one peg to another, according to the rules of the puzzle. The myth 454 7 / Advanced Counting Techniques 7-6 LinkS� says that the world will end when they finish the puzzle. How long after the monks started will the world end if the monks take one second to move a disk? From the explicit formula, the monks require 264 - 1 = 18,446,744,073,709,551,615 moves to transfer the disks. Making one move per second, it will take them more than 500 billion years to complete the transfer, so the world should survive a while longer than it already has. � Remark: Many people have studied variations of the original Tower of Hanoi puzzle discussed in Example 5. Some variations use more pegs, some allow disks to be of the same size, and some restrict the types of allowable disk moves. One of the oldest and most interesting variations is the Reve's puzzle,* proposed in 1907 by Henry Dudeney in his book The Canterbury Puzzles. The Reve's puzzle involves pilgrims challenged by the Reve to move a stack of cheeses of varying sizes from the first of four stools to another stool without ever placing a cheese on one of smaller diameter. The Reve's puzzle, expressed in terms of pegs and disks, follows the same rules as the Tower of Hanoi puzzle, except that four pegs are used. You may find it surprising that no one has been able to establish the minimum number of moves required to solve this puzzle for n disks. However, there is a conjecture, now more than 50 years old, that the minimum number of moves required equals the number of moves used by an algorithm invented by Frame and Stewart in 1939. (See Exer cises 54-61 at the end of this section and [St94] for more information.) Example 6 illustrates how recurrence relations can be used to count bit strings of a specified length that have a certain property. EXAMPLE 6 Find a recurrence relation and give initial conditions for the number of bit strings of length n that do not have two consecutive Os. How many such bit strings are there of length five? Solution: Let an denote the number of bit strings of length n that do not have two consecutive Os. To obtain a recurrence relation for {an}, note that by the sum rule, the number of bit strings of length n that do not have two consecutive Os equals the number of such bit strings ending with a 0 plus the number of such bit strings ending with a 1. We will assume that n ::: 3, so that the bit string has at least three bits. The bit strings oflength n ending with 1 that do not have two consecutive Os are precisely the bit strings of length n -1 with no two consecutive Os with a 1 added at the end. Consequently, there are an-I such bit strings. Bit strings of length n ending with a 0 that do not have two consecutive Os must have 1 as their (n -l)st bit; otherwise they would end with a pair of Os. It follows that the bit strings of length n ending with a 0 that have no two consecutive Os are precisely the bit strings of length n -2 with no two consecutive Os with 10 added at the end. Consequently, there are an-2 such bit strings. We conclude , as illustrated in Figure 4, that for n ::: 3. The initial conditions are al = 2, because both bit strings oflength one, 0 and 1 do not have consecutive Os, and a2 = 3, because the valid bit strings of length two are 01, 10, and 11. To • Reve, more commonly spelled reeve, is an archaic word for governor. 7-7 End with a 1: End with aO: Any bit string of length n -I with no two con sec uti ve Os Any bit string of length n -2 with no two consecutive Os o 7.1 Recurrence Relations 455 Number of bit strings of length n with no two consecutive Os: Total: (/" = (/,,_1 + (/,,_2 FIGURE 4 Counting Bit Strings of Length n with No Two Consecutive Os. obtain a5, we use the recurrence relation three times to find that a3 = a2 + al = 3 + 2 = 5, a4 = a3 + a2 = 5 + 3 = 8, a5 = a4 + a3 = 8 + 5 = 13. Remark: Note that {an} satisfies the same recurrence relation as the Fibonacci sequence. Because al = /3 and a2 = /4 it follows that an = /n+2. Example 7 shows how a recurrence relation can be used to model the number of codewords that are allowable using certain validity checks. EXAMPLE 7 Codeword Enumeration A computer system considers a string of decimal digits a valid codeword ifit contains an even number of 0 digits. For instance, 1230407869 is valid, whereas 120987045608 is not valid. Let an be the number of valid n-digit codewords. Find a recurrence relation for an. Solution: Note that al = 9 because there are 10 one-digit strings, and only one, namely, the string 0, is not valid. A recurrence relation can be derived for this sequence by considering how a valid n-digit string can be obtained from strings of n -1 digits. There are two ways to form a valid string with n digits from a string with one fewer digit. First, a valid string of n digits can be obtained by appending a valid string of n -1 digits with a digit other than O. This appending can be done in nine ways. Hence, a valid string with n digits can be formed in this manner in 9an_1 ways. Second, a valid string of n digits can be obtained by appending a 0 to a string oflength n -1 that is not valid. (This produces a string with an even number of 0 digits because the invalid string of length n -1 has an odd number of 0 digits.) The number of ways that this can be done equals the number of invalid (n -I)-digit strings. Because there are lOn-I strings of length n -1, and an-I are valid, there are lon-I -an-I valid n-digit strings obtained by appending an invalid string of length n -1 with a O. Because all valid strings of length n are produced in one of these two ways, it follows that there are an = 9an_1 + (lon-I -an-d = 8an-l + lOn-I valid strings oflength n. Example 8 establishes a recurrence relation that appears in many different contexts. 456 7 / Advanced Counting Techniques 7-8 EXAMPLE 8 Find a recurrence relation for Cn, the number of ways to parenthe size the product ofn + 1 num­ bers, Xo • Xl . X2 ..... Xn, to specify the order of multiplication. For example, C 3 = 5 because there are five ways to parenthesize Xo . Xl . X2 . X3 to determine the order of multiplication: unkS� Exercises ((Xo . Xl) . X2) . X3 Xo • ((Xl· X2) . X3) (XO . (Xl· X2)) . X3 Xo . (Xl· (X2 . X3)). Solution: To develop a recurrence relation for C n, we note that however we insert parentheses in the product Xo . Xl . X2 ..... Xn, one "." operator remains outside all parenthe ses, namely, the operator for the final multiplication to be performed. [For example, in (xo . (Xl· X2)) • X3, it is the final ".", while in (xo . xt) . (X2 . X3) it is the second ".".] This final operator appears between two of the n + 1 numbers, say, Xk and Xk+l. There are CkCn-k-1 ways to insert paren­ theses to determine the order of the n + 1 numbers to be multiplied when the final operator appears between Xk and Xk+l, because there are Ck ways to insert parentheses in the product Xo . Xl ..... Xk to determine the order in which these k + 1 numbers are to be multiplied and Cn-k-l ways to insert parentheses in the product Xk+1 . Xk+2 ..... Xn to determine the order in which these n -k numbers are to be multiplied. Because this final operator can appear between any two of the n + 1 numbers, it follows that n-l = L CkCn-k-l. k=O Note that the initial conditions are Co = 1 and C1 = 1. This recurrence relation can be solved using the method of generating functions, which will be discussed in Section 7.4. It can be shown that Cn = C(2n, n)j(n + 1). (See Exercise 41 at the end of that section.) .... The sequence {C n} is the sequence of Catalan numbers. This sequence appears as the solution of many different counting problems besides the one considered here (see the chapter on Catalan numbers in [MiR091] or [R084a] for details). 1. Find the first five terms of the sequence defined by each of these recurrence relations and initial conditions. b) Showthata2 = 5al -6ao, a3 = 5a2 -6al, anda4 = 5a3 -6a2. a) an = 6an-l, ao = 2 b) an=a�_I,al=2 c) an = an-I + 3an-2, ao = 1, al = 2 d) an = nan_1 + n2an_2' ao = 1, al = 1 e) an = an-I + an-3, ao = 1, al = 2, a2 = 0 2. Find the first six terms of the sequence defined by each of these recurrence relations and initial conditions. a) an = -2an_l, ao = -1 b) an = an-I -an-2, ao = 2, al = -1 c) an = 3a�_I' ao = 1 d) an = nan_1 + a�_2' ao = -1, al = 0 e) an = an-I -an-2 + an-3, ao = 1, al = 1, a2 = 2 3. Let an = 2n + 5 . 3n for n = 0, 1, 2, .... a) Find ao, ai, a2, a3, and a4. c) Show that an = 5an-1 -6an-2 for all integers n with n ::: 2. 4. Show that the sequence {an} is a solution ofthe recurrence relation an = -3an-1 + 4an-2 if a) an = O. b) an = 1. c) an = (_4)n. d) an = 2(-4)n + 3. 5. Is the sequence {an} a solution ofthe recurrence relation an = 8an_1 -16an_2 if a) an = O? c) an = 2n? e) an = n4n? g) an = (_4)n? b) an =l? d) an = 4n? t) an = 2 ·4n + 3n4n? h) an = n24n? 6. For each of these sequences find a recurrence relation satisfied by this sequence. (The answers are not unique 7-9 because there are infinitely many different recurrence relations satisfied by any sequence.) a) an = 3 b) an = 2n c) an = 2n + 3 d) an = 5n e) an = n2 t) an = n2 + n g) an = n + ( -1 t h) an = n! 7. Show that the sequence {an} is a solution of the recurrence relation an = an-I + 2an-2 + 2n -9 if a) an = -n + 2. b) an = 5(-lt -n + 2. c) an = 3(-lt + 2n -n + 2. d) an = 7 . 2n -n + 2. 8. Find the solution to each ofthese recurrence relations with the given initial conditions. Use an iterative approach such as that used in Example 5. a) an = -an-I,ao = 5 b) an =an-I +3,ao = I c) an = an-I -n, ao = 4 d) an = 2an_1 -3, ao = -1 e) an = (n + l)an-I, ao = 2 t) an = 2nan_l,ao = 3 g) an = -an-I + n -1, ao = 7 9. Find the solution to each of these recurrence relations and initial conditions. Use an iterative approach such as that used in Example 5. a) an = 3an-l, ao = 2 b) an = an-I + 2, ao = 3 c) an =an-I +n,ao = I d) an = an-I + 2n + 3, ao = 4 e) an = 2an_1 -l,ao = 1 t) an = 3an-1 + I,ao = I g) an = nan-I, ao = 5 h) an = 2nan-l, ao = 1 10. A person deposits $1000 in an account that yields 9% interest compounded anually. a) Set up a recurrence relation for the amount in the ac­ count at the end of n years. b) Find an explicit formula for the amount in the account at the end of n years. c) How much money will the account contain after 100 years? 11. Suppose that the number of bacteria in a colony triples every hour. a) Set up a recurrence relation for the number of bacteria after n hours have elapsed. b) If 100 bacteria are used to begin a new colony, how many bacteria will be in the colony in 10 hours? 112. Assume that the population of the world in 2002 was 6.2 billion and is growing at the rate of 1.3% a year. a) Set up a recurrence relation for the population of the world n years after 2002. b) Find an explicit formula for the population ofthe world n years after 2002. c) What will the population of the world be in 2022? 7.1 Recurrence Relations 457 13. A factory makes custom sports cars at an increasing rate. In the first month only one car is made, in the second month two cars are made, and so on, with n cars made in the nth month. a) Set up a recurrence relation for the number of cars produced in the first n months by this factory. b) How many cars are produced in the first year? c) Find an explicit formula for the number of cars pro­ duced in the first n months by this factory. 14. An employee joined a company in 1999 with a starting salary of $50,000. Every year this employee receives a raise of $1000 plus 5% of the salary of the previous year. a) Set up a recurrence relation for the salary of this em­ ployee n years after 1999. b) What will the salary of this employee be in 2007? c) Find an explicit formula for the salary ofthis employee n years after 1999. 15. Find a recurrence relation for the balance B(k) owed at the end of k months on a loan of $5000 at a rate of 7% if a payment of $100 is made each month. [Hint: Ex­ press B(k) in terms of B(k -1); the monthly interest is (0.07/12)B(k -1).] � 16. a) Find a recurrence relation for the balance B (k) owed at the end of k months on a loan at a rate of r if a payment P is made on the loan each month. [Hint: Express B(k) in terms of B(k -1) and note that the monthly interest rate is r /12.] b) Determine what the monthly payment P should be so that the loan is paid off after T months. 17. Use mathematical induction to verity the formula derived in Example 5 for the number of moves required to com­ plete the Tower of Hanoi puzzle. 18. a) Find a recurrence relation for the number of perm uta­ tions of a set with n elements. b) Use this recurrence relation to find the number of per­ mutations of a set with n elements using iteration. 19. A vending machine dispensing books of stamps accepts only dollar coins, $1 bills, and $5 bills. a) Find a recurrence relation for the number of ways to deposit n dollars in the vending machine, where the order in which the coins and bills are deposited matters. b) What are the initial conditions? c) How many ways are there to deposit $10 for a book of stamps? 20. A country uses as currency coins with values of 1 peso, 2 pesos, 5 pesos, and 10 pesos and bills with values of 5 pesos, 10 pesos, 20 pesos, 50 pesos, and 100 pesos. Find a recurrence relation for the number of ways to pay a bill of n pesos if the order in which the coins and bills are paid matters. 21. How many ways are there to pay a bill of 17 pesos using the currency described in Exercise 20, where the order in which coins and bills are paid matters? 458 7 / Advanced Counting Techniques '22. a) Find a recurrence relation for the number of strictly increasing sequences of positive integers that have 1 as their first term and n as their last term, where n is a pos­ itive integer. That is, sequences ai, a2, ... , ak, where al = 1, ak = n, and aj < aj+1 for j = 1,2, ... , k -1. b) What are the initial conditions? c) How many sequences of the type described in (a) are there when n is a positive integer with n :::: 2? 23. a) Find a recurrence relation for the number of bit strings of length n that contain a pair of consecutive Os. b) What are the initial conditions? c) How many bit strings oflength seven contain two con­ secutive Os? 24. a) Find a recurrence relation for the number of bit strings of length n that contain three consecutive Os. b) What are the initial conditions? c) How many bit strings of length seven contain three consecutive Os? 25. a) Find a recurrence relation for the number of bit strings oflength n that do not contain three consecutive Os. b) What are the initial conditions? c) How many bit strings of length seven do not contain three consecutive Os? 26. a) Find a recurrence relation for the number of bit strings that contain the string 01. b) What are the initial conditions? c) How many bit strings oflength seven contain the string Ol? 27. a) Find a recurrence relation for the number of ways to climb n stairs ifthe person climbing the stairs can take one stair or two stairs at a time. b) What are the initial conditions? c) How many ways can this person climb a flight of eight stairs? 28. a) Find a recurrence relation for the number of ways to climb n stairs if the person climbing the stairs can take one, two, or three stairs at a time. b) What are the initial conditions? c) How many ways can this person climb a flight of eight stairs? A string that contains only Os, Is, and 2s is called a ternary string. 29. a) Find a recurrence relation for the number of ternary strings that do not contain two consecutive Os. b) What are the initial conditions? c) How many ternary strings oflength six do not contain two consecutive Os? 30. a) Find a recurrence relation for the number of ternary strings that contain two consecutive Os. b) What are the initial conditions? c) How many ternary strings of length six contain two consecutive Os? 31. a) Find a recurrence relation for the number of ternary strings that do not contain two consecutive Os or two consecutive 1 s. 7-10 b) What are the initial conditions? c) How many ternary strings oflength six do not contain two consecutive Os or two consecutive 1 s? *32. a) Find a recurrence relation for the number of ternary strings that contain either two consecutive Os or two consecutive 1 s. b) What are the initial conditions? c) How many ternary strings of length six contain two consecutive Os or two consecutive 1 s? *33. a) Find a recurrence relation for the number of ternary strings that do not contain consecutive symbols that are the same. b) What are the initial conditions? c) How many ternary strings oflength six do not contain consecutive symbols that are the same? **34. a) Find a recurrence relation for the number of ternary strings that contain two consecutive symbols that are the same. b) What are the initial conditions? c) How many ternary strings of length six contain con­ secutive symbols that are the same? 35. Messages are transmitted over a communications channel using two signals. The transmittal of one signal requires I microsecond, and the transmittal of the other signal re­ quires 2 microseconds. a) Find a recurrence relation for the number of different messages consisting of sequences of these two sig­ nals, where each signal in the message is immediately followed by the next signal, that can be sent in n mi­ croseconds. b) What are the initial conditions? c) How many different messages can be sent in 10 microseconds using these two signals? 36. A bus driver pays all tolls, using only nickels and dimes, by throwing one coin at a time into the mechanical toll collector. a) Find a recurrence relation for the number of different ways the bus driver can pay a toll of n cents (where the order in which the coins are used matters). b) In how many different ways can the driver pay a toll of 45 cents? 37. a) Find the recurrence relation satisfied by Rn, where Rn is the number of regions that a plane is divided into by n lines, if no two ofthe lines are parallel and no three ofthe lines go through the same point. b) Find Rn using iteration. *38. a) Find the recurrence relation satisfied by Rn, where Rn is the number of regions into which the surface of a sphere is divided by n great circles (which are the in­ tersections of the sphere and planes passing through the center ofthe sphere), if no three ofthe great circles go through the same point. b) Find Rn using iteration. *39. a) Find the recurrence relation satisfied by Sn, where Sn is the number of regions into which three-dimensional space is divided by n planes if every three of the planes 7-11 meet in one point, but no four ofthe planes go through the same point. b) Find Sn using iteration. 40. Find a recurrence relation for the number of bit sequences of length n with an even number of Os. 41. How many bit sequences oflength seven contain an even number of Os? 42. a) Find a recurrence relation for the number of ways to completely cover a 2 x n checkerboard with I x 2 dominoes. [Hint: Consider separately the coverings where the position in the top right comer of the checkerboard is covered by a domino positioned hor­ izontally and where it is covered by a domino posi­ tioned vertically.] b) What are the initial conditions for the recurrence re­ lation in part (a)? c) How many ways are there to completely cover a 2 x 17 checkerboard with 1 x 2 dominoes? 43. a) Find a recurrence relation for the number of ways to layout a walkway with slate tiles if the tiles are red, green, or gray, so that no two red tiles are adjacent and tiles of the same color are considered indistinguish­ able. b) What are the initial conditions for the recurrence re­ lation in part (a)? c) How many ways are there to layout a path of seven tiles as described in part (a)? 44. Show that the Fibonacci numbers satisfy the recurrence re­ lation In = 51n-4 + 3ln-s for n = 5,6,7, ... , together with the initial conditions 10 = 0,11 = 1,12 = 1, h = 2, and 14 = 3. Use this recurrence relation to show that ISn is divisible by 5, for n = 1,2,3, .... �45. Let S (m, n) denote the number of onto functions from a set with m elements to a set with n elements. Show that SCm, n) satisfies the recurrence relation n-l SCm, n) = nm -L C(n, k)S(m, k) k=l whenever m ::: nand n > 1, with the initial condition SCm, 1) = 1. 46. a) Write out all the ways the product Xo • Xl • X2 • X3 . X4 can be parenthesized to determine the order of multi­ plication. b) Use the recurrence relation developed in Example 8 to calculate C4, the number of ways to parenthesize the product offive numbers so as to determine the or­ der of multiplication. Verify that you listed the correct number of ways in part (a). c) Check your result in part (b) by finding C4, using the closed formula for C n mentioned in the solution of Example 8. 47. a) Use the recurrence relation developed in Example 8 to determine C s, the number of ways to parenthesize the product of six numbers so as to determine the order of multiplication. 7.1 Recurrence Relations 459 b) Check your result with the closed formula for C S men­ tioned in the solution of Example 8. *48. In the Tower of Hanoi puzzle, suppose our goal is to trans­ fer all n disks from peg 1 to peg 3, but we cannot move a disk directly between pegs 1 and 3. Each move of a disk must be a move involving peg 2. As usual, we cannot place a disk on top of a smaller disk. a) Find a recurrence relation for the number of moves re­ quired to solve the puzzle for n disks with this added restriction. b) Solve this recurrence relation to find a formula for the number of moves required to solve the puzzle for n disks. c) How many different arrangements are there of the n disks on three pegs so that no disk is on top of a smaller disk? d) Show that every allowable arrangement of the n disks occurs in the solution of this variation of the puzzle. � Exercises 49-53 deal with a variation of the Josephus problem described by Graham, Knuth, and Patashnik in [GrKnPa94]. This problem is based on an account by the his­ torian Flavius Josephus, who was part of a band of 41 Jewish rebels trapped in a cave by the Romans during the Jewish­ Roman war of the first century. The rebels preferred suicide to capture; they decided to form a circle and to repeatedly count off around the circle, killing every third rebel left alive. However, Josephus and another rebel did not want to be killed this way; they determined the positions where they should stand to be the last two rebels remaining alive. The variation we consider begins with n people, numbered 1 to n, stand­ ing around a circle. In each stage, every second person still left alive is eliminated until only one survives. We denote the number of the survivor by l(n). 49. Determine the value of l(n) for each integer n with 1 ::: n ::: 16. 50. Use the values you found in Exercise 49 to conjecture a formula for l(n). [Hint: Write n = 2m + k, where m is a nonnegative integer and k is a nonnegative integer less than 2m.] 51. Show that 1 (n) satisfies the recurrence relation 1 (2n) = 21(n) -1 and 1(2n + 1) = 21(n) + 1, for n ::: 1, and 1(1) = 1. 52. Use mathematical induction to prove the formula you con­ jectured in Exercise 50, making use of the recurrence re­ lation from Exercise 51. 53. Determine 1(100), 1(1000), and 1(10,000) from your formula for l(n). Exercises 54-61 involve the Reve's puzzle, the variation of the Tower of Hanoi puzzle with four pegs and n disks. Before presenting these exercises, we describe the Frame-Stewart al­ gorithm for moving the disks from peg 1 to peg 4 so that no disk is ever on top of a smaller one. This algorithm, given the number of disks n as input, depends on a choice of an integer k with 1 ::: k ::: n. When there is only one disk, move it from peg 1 to peg 4 and stop. For n > 1, the algorithm proceeds 460 7 / Advanced Counting Techniques recursively, using these three steps. Recursively move the stack ofthe n -k smallest disks from peg 1 to peg 2, using all four pegs. Next move the stack of the k largest disks from peg 1 to peg 4, using the three-peg algorithm from the Tower of Hanoi puzzle without using the peg holding the n -k small­ est disks. Finally, recursively move the smallest n -k disks to peg 4, using all four pegs. Frame and Stewart showed that to produce the fewest moves using their algorithm, k should be chosen to be the smallest integer such that n does not exceed tk = k(k + 1)/2, the kth triangular number, that is, tk-l < n :::: tk. The unsettled conjecture, known as Frame's conjecture, is that this algorithm uses the fewest number of moves required to solve the puzzle, no matter how the disks are moved. 54. Show that the Reve's puzzle with three disks can be solved using five, and no fewer, moves. 55. Show that the Reve's puzzle with four disks can be solved using nine, and no fewer, moves. 56. Describe the moves made by the Frame-Stewart algo­ rithm, with k chosen so that the fewest moves are required, for a) 5 disks. b) 6 disks. c) 7 disks. d) 8 disks. '57. Show that if R(n) is the number of moves used by the Frame-Stewart algorithm to solve the Reve's puzzle with n disks, where k is chosen to be the smallest integer with n :::: k(k + 1)/2, then R(n) satisfies the recurrence relation R(n) = 2R(n -k) + 2k -1, with R(O) = 0 and R(l) = 1. '58. Show that if k is as chosen in Exercise 57, then R(n)­ R(n -1) = 2k-l. 7-12 *59. Show that if k is as chosen in Exercise 57, then R(n) = "k '2i-l _ (t _ )2k-l L-i= 11 k n . *60. Use Exercise 59 to give an upper bound on the number of moves required to solve the Reve's puzzle for all integers n with 1 :::: n :::: 25. *61. Show that R(n) is o (..jii2ffn). Let {an} be a sequence of real numbers. The backward differ­ ences of this sequence are defined recursively as shown next. The first difference Van is The (k + l)st difference Vk+1an is obtained from Vkan by 62. Find Van for the sequence {an}, where a) an = 4. b) an = 2n. c) an =n2• d) an =2n. 63. Find V2an for the sequences in Exercise 62. 64. Show that an-l = an -Van. 65. Show that an-2 = an -2Van + V2an. *66. Prove that an-k can be expressed in terms of an, Van, V2an, ... , Vkan. 67. Express the recurrence relation an = an-l + an-2 in terms of an, Van, and V2an. 68. Show that any recurrence relation for the sequence {an} can be written in terms of an, Van, V2an, .... The result­ ing equation involving the sequences and its differences is called a difference equation. 7.2 Solving Linear Recurrence Relations unkS� DEFINITION 1 Introduction A wide variety of recurrence relations occur in models. Some of these recurrence relations can be solved using iteration or some other ad hoc technique. However, one important class of recurrence relations can be explicitly solved in a systematic way. These are recurrence relations that express the terms of a sequence as linear combinations of previous terms. A linear homogeneous recurrence relation of degree k with constant coefficients is a recur­ rence relation of the form where CI. C2 •••.• Ck are real numbers, and Ck =1= O. The recurrence relation in the definition is linear because the right-hand side is a sum of previous terms of the sequence each multiplied by a function of n. The recurrence relation is 7-13 7.2 Solving Linear Recurrence Relations 461 homogeneous because no terms occur that are not multiples of the ajs. The coefficients of the terms of the sequence are all constants, rather than functions that depend on n. The degree is k because an is expressed in terms of the previous k terms of the sequence. A consequence of the second principle of mathematical induction is that a sequence satis­ fying the recurrence relation in the definition is uniquely determined by this recurrence relation and the k initial conditions EXAM PLE 1 The recurrence relation Pn = (1.11 )Pn-I is a linear homogeneous recurrence relation of degree one. The recurrence relation In = In-I + In-2 is a linear homogeneous recurrence relation of degree two. The recurrence relation an = an-5 is a linear homogeneous recurrence relation of degree five. ... Example 2 presents some examples of recurrence relations that are not linear homogeneous recurrence relations with constant coefficients. EXAMPLE 2 The recurrence relation an = an-I + a;_2 is not linear. The recurrence relation Hn = 2Hn_1 + 1 is not homogeneous. The recurrence relation Bn = nBn_1 does not have constant co­ efficients. ... Linear homogeneous recurrence relations are studied fo r two reasons. First, they often occur in modeling of problems. Second, they can be systematically solved. Solving Linear Homogeneous Recurrence Relatio ns with Constant Coefficients The basic approach for solving linear homogeneous recurrence relations is to look for solutions of the form an = rn, where r is a constant. Note that an = rn is a solution of the recurrence relation an = Clan-I + C2an-2 + ... + Ckan-k if and only if When both sides ofthis equation are divided by rn-k and the right-hand side is subtracted from the left, we obtain the equation Consequently, the sequence {an} with an = rn is a solution if and only if r is a solution of this last equation, which is called the characteristic equation of the recurrence relation. The solutions of this equation are called the characteristic roots of the recurrence relation. As we will see, these characteristic roots can be used to give an explicit formula for all the solutions of the recurrence relation. We will first develop results that deal with linear homogeneous recurrence relations with constant coefficients of degree two. Then corresponding general results when the degree may be greater than two will be stated. Because the proofs needed to establish the results in the general case are more complicated, they will not be given in the text. We now turn our attention to linear homogeneous recurrence relations of degree two. First, consider the case when there are two distinct characteristic roots. 462 7 / Advanced Counting Techniques 7-14 THEOREM 1 Let CI and C2 be real numbers . Suppose that r2 -clr -C2 = ° has two distinct roots rl and r2. Then the sequence {an} is a solution of the recurrence relation an = Clan-I + C2an-2 if and only if an = air! + a2rg for n = 0, 1,2, ... , where al and a2 are constants. Proof: We must do two things to prove the theorem. First, it must be shown that if rl and r2 are the roots of the characteristic equation, and a I and a2 are constants, then the sequence {an} with an = air, + a2rg is a solution of the recurrence relation. Second, it must be shown that if the sequence {an} is a solution, then an = air, + a2rg for some constants al and a2. Now we will show that if an = a I r, + a2rg, then the sequence {an} is a solution of the recurrence relation. Because rl and r2 are roots of r2 -CI r -C2 = 0, it follows that rr = Clrl + C2, rj: = Clr2 + C2. From these equations, we see that ( n-I n-I) ( n-2 + n-2) Clan-I + C2an-2 = CI alrl + a2r2 + C2 alrl a2r2 = alr�-2(clrl + C2) + a2r;-2(clr2 + C2) n-2 2 n-2 2 = alrl rl + a2r2 r2 = air, + a2rg. This shows that the sequence {an} with an = air, + a2rg is a solution of the recurrence relation. To show that every solution {an} of the recurrence relation an = Clan_1 + C2an-2 has an = air, + a2r2 for n = 0,1,2, ... , for some constants al and a2, suppose that {an} is a solution ofthe recurrence relation, and the initial conditions ao = Co and a I = C I hold. It will be shown that there are constants al and a2 such that the sequence {an} with an = air, + a2r2 satisfies these same initial conditions. This requires that ao = Co = al + a2, al = CI = alrl + a2r2. We can solve these two equations for al and a2. From the first equation it follows that a2 = Co -a I. Inserting this expression into the second equation gives Hence, This shows that and 7-15 7.2 Solving Linear Recurrence Relations 463 where these expressions for al and a2 depend on the fact that rl i= r2. (When rl = r2, this theorem is not true.) Hence, with these values foral and a2, the sequence {an} with air? + a2r2 satisfies the two initial conditions. We know that {an} and {a I r? + a2r2} are both solutions of the recurrence relation an = Clan_1 + C2an-2 and both satisfy the initial conditions when n = 0 and n = 1. Because there is a unique solution of a linear homogeneous recurrence relation of degree two with two initial conditions, it follows that the two solutions are the same, that is, an = air? + a2r2 for all nonnegative integers n. We have completed the proof by showing that a solution of the linear homogeneous recurrence relation with constant coefficients of degree two must be of the form an = al r? + a2r2' where al and a2 are constants. <l The characteristic roots of a linear homogeneous recurrence relation with constant coeffi­ cients may be complex numbers . Theorem 1 (and also subsequent theorems in this section) still applies in this case. Recurrence relations with complex characteristic roots will not be discussed in the text. Readers familiar with complex numbers may wish to solve Exercises 38 and 39 at the end of this section. Examples 3 and 4 show how to use Theorem 1 to solve recurrence relations. EXAMPLE 3 What is the solution of the recurrence relation Extra� Examples� with ao = 2 and al = 7? Solution: Theorem 1 can be used to solve this problem. The characteristic equation of the recurrence relation is r2 -r -2 = O. Its roots are r = 2 and r = -1. Hence, the sequence {an} is a solution to the recurrence relation if and only if for some constants al and a2. From the initial conditions, it follows that ao = 2 = al + a2, al =7=al·2+a2·(-I). Solving these two equations shows that al = 3 and a2 = -1. Hence, the solution to the recur­ rence relation and initial conditions is the sequence {an} with EXAMPL E 4 Find an explicit formula for the Fibonacci numbers. Solution: Recall that the sequence of Fibonacci numbers satisfies the recurrence relation In = In-I + In-2 and also satisfies the initial conditions 10 = 0 and II = 1. The roots of the characteristic equation r2 -r -1 = 0 are rl = (1 + ,J5)/2 and r2 = (1 -,J5)/2. Therefore, from Theorem 1 it follows that the Fibonacci numbers are given by J, (1 + ,J5) n (1 -,J5) n n = al 2 +a2 2 ' 464 7 / Advanced Counting Techniques 7-16 THEOREM 2 for some constants al and a2. The initial conditions 10 = ° and II = 1 can be used to find these constants. We have 10 = al + a2 = 0, f, � a, ( 1+2 v's) + a, C -2 v's) � 1. The solution to these simultaneous equations for al and a2 is a2 = -1/v's. Consequently, the Fibonacci numbers are given by f, = _1 (1 + -J5)n _ _ 1 (1 --J5)n n -J5 2 -J5 2 Theorem I does not apply when there is one characteristic root of multiplicity two. If this happens, then an = nr3 is another solution of the recurrence relation when ro is a root of multiplicity two of the characteristic equation. Theorem 2 shows how to handle this case. Let Cl and C2 be real numbers with C2 =I 0. Suppose that r2 -Clr -C2 = ° has only one root roo A sequence {an} is a solution of the recurrence relation an = Clan-l + C2an-2 if and only if an = al r3 + a2nr3, for n = 0, 1, 2, ... , where al and a2 are constants. The proof of Theorem 2 is left as an exercise at the end of the section. Example 5 illustrates the use of this theorem. EXAMPLE 5 What is the solution of the recurrenCe relation with initial conditions ao = 1 and al = 6? Solution: The only root of r2 -6r + 9 = ° is r = 3. Hence, the solution to this recurrence relation is for some constants al and a2. Using the initial conditions, it follows that ao = 1 = aI, al = 6 = al ·3 + a2 ·3. Solving these two equations shows that al = 1 and a2 = 1. Consequently, the solution to this recurrence relation and the initial conditions is We will now state the general result about the solution of linear homogeneous recur­ rence relations with constant coefficients, where the degree may be greater than two, under the 7-/7 THEOREM 3 7.2 Solving Linear Recurrence Relations 465 assumption that the characteristic equation has distinct roots. The proof of this result will be left as an exercise for the reader. Let CI, C2, ••• ,Ck be real numbers . Suppose that the characteristic equation has k distinct roots rl, r2, ... , rk. Then a sequence {an} is a solution of the recurrence relation if and only if for n = 0, 1,2, ... , where ai, a2, ... , ak are constants . We illustrate the use of the theorem with Example 6. EXAMPLE 6 Find the solution to the recurrence relation with the initial conditions ao = 2, al = 5, and a2 = 15. Solution: The characteristic polynomial of this recurrence relation is r3 -6r2 + llr -6. The characteristic roots are r = 1, r = 2, and r = 3, because r3 -6r2 + llr -6 = (r -1) (r -2)(r -3). Hence, the solutions to this recurrence relation are of the form To find the constants ai, a2, and a3, use the initial conditions. This gives ao = 2 = al + a2 + a3, al = 5 = al + a2 ·2 + a3 . 3, a2 = 15 = al + a2 ·4 + a3 ·9. When these three simultaneous equations are solved for ai, a2, and a3, we find that al = 1, a2 = -1, and a3 = 2. Hence, the unique solution to this recurrence relation and the given initial conditions is the sequence {an} with We now state the most general result about linear homogeneous recurrence relations with constant coefficients, allowing the characteristic equation to have multiple roots. The key point is that for each root r of the characteristic equation, the general solution has a summand of the 466 7 / Advanced Counting Techniques 7-18 THEOREM 4 form p(n)rn, where P(n) is a polynomial of degree m -1, with m the multiplicity of this root. We leave the proof of this result as a challenging exercise for the reader. Let CI , C2, •.. , Ck be real numbers. Suppose that the characteristic equation has t distinct roots rl, r2, ... , rt with multiplicities m I, m2, ... ,mt, respectively, so that mi ::: 1 for i = 1,2, ... , t and ml + m2 + ... + mt = k. Then a sequence {an} is a solution of the recurrence relation if and only if an = (al,o + al,ln + ... + al,m,_lnm,-I)rl' + (a2,O + a2,In + ... + a2,m2_lnm2-I)ri + ... + (at,O + at,ln + ... + at,mt_Inmt-l)r? for n = 0,1,2, ... , where ai,j are constants for 1 :::: i :::: t and 0 :::: j :::: mi - 1. Example 7 illustrates how Theorem 4 is used to find the general form of a solution of a linear homogeneous recurrence relation when the characteristic equation has several repeated roots. EXAMPLE 7 Suppose that the roots of the characteristic equation of a linear homogeneous recurrence relation are 2, 2, 2, 5, 5, and 9 (that is, there are three roots, the root 2 with multiplicity three, the root 5 with multiplicity two, and the root 9 with multiplicity one). What is the form of the general solution? Solution: By Theorem 4, the general form of the solution is We now illustrate the use of Theorem 4 to solve a linear homogeneous recurrence relation with constant coefficients when the characteristic equation has a root of multiplicity three. EXAMPLE 8 Find the solution to the recurrence relation with initial conditions ao = 1, al = -2, and a2 = -1. Solution: The characteri stic equation of this recurrence relation is r3 + 3r2 + 3r + 1 = O. 7-19 7.2 Solving Linear Recurrence Relations 467 Because r3 + 3r2 + 3r + 1 = (r + 1)3, there is a single root r = -1 of multiplicity three of the characteristic equation. By Theorem 4 the solutions of this recurrence relation are of the form To find the constants al,O, al,l, and al,2, use the initial conditions. This gives aO = 1 = al,O, al = -2 = -al,O -al,l -al,2, a2 = -1 = al,O + 2al,1 + 4a1,2. The simultaneous solution ofthese three equations is al,O = 1, al,l = 3, and al,2 = -2. Hence, the unique solution to this recurrence relation and the given initial conditions is the sequence {an} with Linear Nonhomogeneous Recurrence Relations with Constant Coefficients We have seen how to solve linear homogeneous recurrence relations with constant coefficients. Is there a relatively simple technique for solving a linear, but not homogeneous, recurrence relation with constant coefficients, such as an = 3an-1 + 2n? We will see that the answer is yes for certain families of such recurrence relations. The recurrence relation an = 3an-1 + 2n is an example of a linear nonhomo geneous recurrence relation with constant coefficients, that is, a recurrence relation of the form where CI, C2, ••• , Ck are real numbers and F(n) is a function not identically zero depending only on n. The recurrence relation is called the associated homogeneous recurrence relation. It plays an important role in the solution of the nonhomogeneous recurrence relation. EXAMPLE 9 Each of the recurrence relations an = an-I + 2n, an = an-I + an-2 + n2 + n + 1, an = 3an-l + n3n, and an = an-I + an-2 + an-3 + n! is a linear nonhomogeneous recurrence re­lation with constant coefficients. The associated linear homogeneous recurrence relations are an = an-I, an = an-I + an-2, an = 3an-l, and an = an-I + an-2 + an-3, respectively. ..... The key fact about linear nonhomogeneous recurrence relations with constant coefficients is that every solution is the sum of a particular solution and a solution of the associated linear homogeneous recurrence relation, as Theorem 5 shows. 468 7 / Advanced Counting Techniques 7-20 THEOREM S If (a¥)} is a particular solution of the nonhomogeneous linear recurrence relation with con­ stant coefficients an = Clan-l + C2an-2 + ... + Ckan-k + F(n), then every solution is of the form (a¥) + a�h)}, where (a�h)} is a solution of the associated homogeneous recurrence relation Proof: Because (a�p)} is a particular solution of the nonhomogeneous recurrence relation, we know that aCp) = e aCp) + C aCp) + ... + e aCp) + F(n). n 1 n-I 2 n-2 k n-k Now suppose that Ibn} is a second solution of the nonhomogeneous recurrence relation, so that Subtracting the first of these two equations from the second shows that bn -a�p) = el(bn-I -a��I) + e2(bn-2 ,.-a��2) + ... + ek(bn-k -a��k)' It follows that Ibn -a�p)} is a solution of the associated homogeneous linear recurrence, say, (a�h)}. Consequently, bn = a�p) + a�h) for all n. <l By Theorem 5, we see that the key to solving nonhomogeneous recurrence relations with constant coefficients is finding a particular solution. Then every solution is a sum of this solution and a solution of the associated homogeneous recurrence relation. Although there is no general method for finding such a solution that works for every function F (n), there are techniques that work for certain types of functions F (n), such as polynomials and powers of constants. This is illustrated in Examples 10 and 11. EXAMPLE 10 Find all solutions of the recurrence relation an = 3an-1 + 2n. What is the solution with al = 3? Solution: To solve this linear nonhomogeneous recurrence relation with constant coefficients, we need to solve its associated linear homogeneous equation and to find a particular solution for the given nonhomogeneous equation. The associated linear homogeneous equation is an = 3an-l. Its solutions are a�h) = a3n, where a is a constant. We now find a particular solution. Because F(n) = 2n is a polynomial in n of degree one, a reasonable trial solution is a linear function in n, say, Pn = en + d, where e and dare constants. To determine whether there are any solutions ofthis form, suppose that Pn = en + d is such a solution. Then the equation an = 3an-1 + 2n becomes en + d = 3(e(n -1) + d) + 2n. Simplifying and combining like terms gives (2 + 2e)n + (2d -3e) = O. It follows that en + d is a solution if and only if 2 + 2c = 0 and 2d -3c = O. This shows that en + d is a solution if and only if e = -1 and d = -3/2. Consequently, a�p) = -n -3/2 is a particular solution. By Theorem 5 all solutions are of the form a = aCp) + aCh) = -n -� + a . 3n n n n 2 ' where a is a constant. 7-21 7.2 Solving Linear Recurrence Relations 469 To find the solution with al = 3, let n = 1 in the formula we obtained for the general solution. We find that 3 = -1 -3/2 + 3a, which implies that a = 11/6. The solution we seek is an = -n -3/2 + (11/6)3n. .... EXAMPLE 11 Find all solutions of the recurrence relation Ex1ra� Examples � an = 5an-1 -6an-2 + r. THEOREM 6 Solution: This is a linear nonhomogeneous recurrence relation. The solutions of its associated homogeneous recurrence relation area�h) = al . 3n + a2 . 2n, whereal anda2 are constants. Because F(n) = 7n ,areasonabl etrial solution is a�p) = C . 7n, where C is a constant. Substituting the terms of this sequence into the recurrence relation implies that C . 7n = 5C . 7n-1 -6C . 7n-2 + 7n. Factoring out 7n-2, this equation becomes 49C = 35C -6C + 49, which implies that 20C = 49, or that C = 49/20. Hence, a�p) = (49/20)7 n is a particular solution. By Theorem 5, all solutions are of the form In Examples 10 and II, we made an educated guess that there are solutions of a particular form. In both cases we were able to find particular solutions. This was not an accident. Whenever F (n) is the product of a polynomial in n and the nth power of a constant, we know exactly what form a particular solution has, as stated in Theorem 6. We leave the proof of Theorem 6 as a challenging exercise for the reader. Suppose that {an} satisfies the linear nonhomogeneous recurrence relation where CI, C2, ••• , Ck are real numbers, and where bo, bl, ... , bt ands are real numbers . When s is not a root of the characteristic equation of the associated linear homogeneous recurrence relation, there is a particular solution ofthe form When s is a root of this characteristic equation and its multiplicity is m, there is a particular solution of the form Note that in the case when s is a root of multiplicity m of the characteristic equation of the associated linear homogeneous recurrence relation, the factor nm ensures that the proposed particular solution will not already be a solution ofthe associated linear homogeneous recurrence 470 7 / Advanced Counting Techniques 7-22 relation. We next provide Example 12 to illustrate the form of a particular solution provided by Theorem 6. EXAMPLE 12 What form does a particular solution of the linear nonhomogeneous recurrence relation an = 6an-I -9an-2 + F(n) have when F(n) = 3n, F(n) = n3n, F(n) = n22n, and F(n) = (n2 + 1)3n? Solution: The associated linear homogeneous recurrence relation is a n = 6an-I -9an-2. Its characteristic equation, r2 -6r + 9 = (r -3)2 = 0, has a single root, 3, of multiplicity two. To apply Theorem 6, with F(n) of the form P(n)sn, where pen) is a polynomial ands is a constant, we need to ask whether s is a root of this characteristic equation. Because s = 3 is a root with multiplicity m = 2 but s = 2 is not a root, Theorem 6 tells us that a particular solution has the formpon23n if F(n) = 3n, the formn2(PIn + Po)3n if F(n) = n3n, the form (P2n2 + PIn + Po)2n if F(n) = n22n, and the form n2(p2n2 + PIn + Po)3n if F(n) = (n2 + 1)3n• .... Care must be taken when s = 1 when solving recurrence relations of the type covered by Theorem 6. In particular, to apply this theorem with F(n) = btnt + bt-Int-I + ... + bIn + bo, the parameter s takes the value s = I (even though the term 1 n does not explicitly appear). By the theorem, the form of the solution then depends on whether I is a root of the characteristic equation of the associated linear homogeneous recurrence relation. This is illustrated in Example 13, which shows how Theorem 6 can be used to find a formula for the sum of the first n positive integers. EXAMPLE 13 Let an be the sum of the first n positive integers, so that Note that an satisfies the linear nonhomogeneous recurrence relation (To obtain an, the sum of the first n positive integers, from an-I, the sum of the first n - 1 positive integers, we add n.) Note that the initial condition is aI = I. The associated linear homogeneous recurrence relation for an is The solutions of this homogeneous recurrence relation are given by a�h) = c(l)n = c, where c is a constant. To find all solutions of an = an-I + n, we need find only a single particular solution. By Theorem 6, because F(n) = n = n . (l)n and s = I is a root of degree one of the characteristic equation of the associated linear homogeneous recurrence relations, there is a particular solution of the form n(PIn + Po) = PIn2 + pon. Inserting this into the recurrence relation gives PI n 2 + pon = PI (n -1)2 + po(n -1) + n. Simplifying, we see that n(2pI -1) + (Po - PI) = 0, which means that 2PI -I = ° and Po -PI = 0, so Po = PI = 1/2. Hence, 7-23 7.2 Solving Linear Recurrence Relations 471 is a particular solution. Hence, all solutions of the original recurrence relation an = an-I + n are given by an = a�h) + a�p) = c + n(n + 1)/2. Because al = 1, we have 1 = al = c + 1 ·2/2 = c + 1, so c = O. It follows that an = n(n + 1)/2. (This is the same formula given in Table 2 in Section 2.4 and derived previously.) .... Exercises 1. Determine which ofthese are linear homogeneous recur­ rence relations with constant coefficients. Also, find the degree ofthose that are. a) an = 3an-1 + 4an-2 + 5an-3 b) an = 2nan_1 + an-2 c) an = an-l + an-4 d) an = an-l + 2 e) an = a�_l + an-2 t) an = an-2 g) an = an-l + n 2. Determine which ofthese are linear homogeneous recur­ rence relations with constant coefficients. Also, find the degree of those that are. a) an = 3an-2 b) an = 3 c) an = a�_l d) an = an-l + 2an-3 e) an = an-lin t) an = an-l + an-2 + n + 3 g) an = 4an-2 + 5an-4 + 9an-7 3. Solve these recurrence relations together with the initial conditions given. a) an = 2an-1 for n ::: 1, ao = 3 b) an = an-l for n ::: 1, ao = 2 c) an = 5an-1 -6an-2 for n ::: 2, ao = 1, al = 0 d) an = 4an-1 -4an-2 for n ::: 2, ao = 6, al = 8 e) an = -4an-1 -4an-2 for n ::: 2, ao = 0, al = 1 t) an = 4an-2 for n ::: 2, ao = 0, al = 4 g) an = an-2 14 for n ::: 2, ao = 1, al = 0 4. Solve these recurrence relations together with the initial conditions given. a) an = an-l + 6an-2 for n ::: 2, ao = 3, al = 6 b) an = 7an-1 -lOan-2 for n ::: 2, ao = 2, al = I c) an = 6an-1 -8an-2 for n ::: 2, ao = 4, al = 10 d) an = 2an-1 -an-2 for n ::: 2, ao = 4, al = 1 e) an = an-2 for n ::: 2, ao = 5, al = -1 t) an = -6an-1 -9an-2 for n ::: 2, ao = 3, al = -3 g) an+2 = -4an+1 + 5an for n ::: 0, ao = 2, al = 8 5. How many different messages can be transmitted in n microseconds using the two signals described in Exercise 35 in Section 7.1? 6. How many different messages can be transmitted in n microseconds using three different signals if one signal requires 1 microsecond for transmittal, the other two sig­ nals require 2 microseconds each for transmittal, and a signal in a message is followed immediately by the next signal? 7. In how many ways can a 2 x n rectangular checkerboard be tiled using 1 x 2 and 2 x 2 pieces? 8. A model for the number of lobsters caught per year is based on the assumption that the number of lobsters caught in a year is the average of the number caught in the two previous years. a) Find a recurrence relation for {Ln}, where Ln is the number oflobsters caught in year n, under the assump­ tion for this model. b) Find Ln if 100,000 lobsters were caught in year 1 and 300,000 were caught in year 2. 9. A deposit of $100,000 is made to an investment fund at the beginning of a year. On the last day of each year two dividends are awarded. The first dividend is 20% of the amount in the account during that year. The second divi­ dend is 45% ofthe amount in the account in the previous year. a) Find a recurrence relation for {Pn}, where Pn is the amount in the account at the end of n years if no money is ever withdrawn. b) How much is in the account after n years if no money has been withdrawn? *10. Prove Theorem 2. 11. The Lucas numbers satisfy the recurrence relation and the initial conditions Lo = 2 and Ll = 1. a) ShowthatLn = fn-l + fn+l forn = 2, 3, ... , where fn is the nth Fibonacci number. b) Find an explicit formula for the Lucas numbers. 12. Find the solution to an = 2an-1 + an-2 -2an-3 for n = 3,4,5, ... , withao = 3, al = 6, anda2 = O. 13. Find the solution to an = 7an-2 + 6an-3 with ao = 9, al = 10, and a2 = 32. 14. Find the solution to an = 5an-2 -4an-4 with ao = 3, al = 2, a2 = 6, and a3 = 8. 15. Find the solution to an = 2an-1 + 5an-2 -6an-3 with ao = 7, al = -4, and a2 = 8. *16. Prove Theorem 3. 17. Prove this identity relating the Fibonacci numbers and the binomial coefficients : fn+l = C(n, 0) + C(n -1, 1) + ... + C(n -k, k), where n is a positive integer and k = Ln 12J. [Hint: Let an = C(n, 0) + C(n -1, 1) + ... + C(n -k, k). Show that the sequence {an} satisfies the same recurrence re­ lation and initial conditions satisfied by the sequence of Fibonacci numbers.] 472 7 / Advanced Counting Techniques 18. Solve the recurrence relation an = 6an-1 -12an-2 + 8an-3 with ao = -5, al = 4, and a2 = 88. 19. Solve the recurrence relation an = -3an-1 -3an-2 - an-3 with ao = 5, al = -9, and a2 = 15. 20. Find the general form of the solutions of the recurrence relation an = 8an-2 -16an_4. 21. What is the general form of the solutions ofa linear homo­ geneous recurrence relation if its characteristic equation has roots I, I, I, I, -2, -2, -2,3,3, -4? 22. What is the general form ofthe solutions of a linear homo­ geneous recurrence relation if its characteristic equation has the roots -I, -I, -1,2,2,5,5, 7? 23. Consider the nonhomogeneous linear recurrence relation an = 3an-1 + 2n. a) Show that an = _2n+1 is a solution ofthis recurrence relation. b) Use Theorem 5 to find all solutions of this recurrence relation. e) Find the solution with ao = 1. 24. Consider the nonhomogeneous linear recurrence relation an = 2an_1 +2n. a) Show that an = n2n is a solution of this recurrence relation. b) Use Theorem 5 to find all solutions of this recurrence relation. e) Find the solution with ao = 2. 25. a) Determine values of the constants A and B such that an = An + B is a solution of recurrence relation an = 2an-1 +n + 5. b) Use Theorem 5 to find all solutions of this recurrence relation. e) Find the solution of this recurrence relation with ao = 4. 26. What is the general form of the particular solution guaranteed to exist by Theorem 6 of the linear nonho­ mogeneous recurrence relation an = 6an-1 -12an-2 + 8an-3 + F(n) if a) F(n) = n2? e) F(n) = n2n? e) F(n) = n22n? g) F(n) = 3? b) F(n) = 2n? d) F(n) = (-2t? t) F(n) = n3( _2)n? 27. What is the general form ofthe particular solution guaran­ teed to exist by Theorem 6 of the linear nonhomogeneous recurrence relation an = 8an-2 -16an-4 + F(n) if a) F(n) = n3? b) F(n) = (-2t? e) F(n) = n2n? d) F(n) = n24n? e) F(n) = (n2 - 2)( -2t? t) F(n) = n42n? g) F(n)=2? 28. a) Find all solutions of the recurrence relation an = 2an_1 +2n2. b) Find the solution of the recurrence relation in part (a) with initial condition al = 4. 29. a) Find all solutions of the recurrence relation an = 2an_1 +3n. 7-24 b) Find the solution of the recurrence relation in part (a) with initial condition al = 5. 30. a) Find all solutions of the recurrence relation an = -5an_1 -6an-2 + 42 . 4n. b) Find the solution of this recurrence relation with al = 56 and a2 = 278. 31. Find all solutions ofthe recurrence relation an = 5an-1 - 6an-2 + 2n + 3n. [Hint: Lookforaparticularsolutionof the form qn2n + Pin + P2, where q, PI, and P2 are con­ stants.] 32. Find the solution of the recurrence relation an = 2an-1 + 3·2n. 33. Find all solutions of the recurrence relation an = 4an_1 - 4an-2 + (n + 1)2n. 34. Find all solutions of the recurrence relation an = 7an_1 - 16an_2 + 12an-3 + n4n with ao = -2, al = 0, and a2 = 5. 35. Find the solution ofthe recurrence relation an = 4an-1 - 3an-2 + 2n + n + 3 with ao = I and al = 4. 36. Let an be the sum of the first n perfect squares, that is, an = I:Z = J k2. Show that the sequence {an l satis­ fies the linear nonhomogeneous recurrence relation an = an-I + n2 and the initial condition aJ = 1. Use Theorem 6 to determine a formula for an by solving this recurrence relation. 37. Let an be the sum of the first n triangular numbers, that is, an = I:�=I tk, where tk = k(k + 1)/2. Showthat{anl satisfies the linear nonhomogeneous recurrence relation an = an-I + n(n + 1)/2 and the initial condition al = 1. Use Theorem 6 to determine a formula for an by solving this recurrence relation. 38. a) Find the characteristic roots of the linear ho­ mogeneous recurrence relation an = 2an-1 -2an-2. (Note: These are complex numbers.) b) Find the solution of the recurrence relation in part (a) withao = I andaJ = 2. *39. a) Find the characteristic roots of the linear homogeneous recurrence relation an = an-4. (Note: These include complex numbers.) b) Find the solution of the recurrence relation in part (a) with ao = I, al = 0, a2 = -I, and a3 = 1. *40. Solve the simultaneous recurrence relations an = 3an-J + 2bn-J bn = an-I + 2bn_1 with ao = I and bo = 2. *41. a) Use the formula found in Example 4 for in, the nth Fibonacci number, to show that in is the integer closest to 7-25 b) Determine for which n fn is greater than _1 (1 + vIs)n vis 2 and for which n fn is less than 42. Show that if an = an-l + an-2, ao = sand al = t, where sand t are constants, then an = Sfn-l + tfn for all posi­ tive integers n. 43. Express the solution of the linear nonhomogenous re­ currence relation an = an-l + an-2 + I for n :::: 2 where ao = 0 and al = I in terms of the Fibonacci numbers. [Hint: Let bn = an + 1 and apply Exercise 42 to the se­ quence bn.] *44. (Linear algebra required) Let An be the n x n matrix with 2s on its main diagonal, 1 s in all positions next to a diagonal element, and Os everywhere else. Find a recur­ rence relation for dn, the determinant of An. Solve this recurrence relation to find a formula for dn• 45. Suppose that each pair of a genetically engineered species of rabbits left on an island produces two new pairs ofrab­ bits at the age of I month and six new pairs of rabbits at the age of2 months and every month afterward. None of the rabbits ever die or leave the island. a) Find a recurrence relation for the number of pairs of rabbits on the island n months after one newborn pair is left on the island. b) By solving the recurrence relation in (a) determine the number of pairs of rabbits on the island n months after one pair is left on the island. 46. Suppose that there are two goats on an island initially. The number of goats on the island doubles every year by natural reproduction, and some goats are either added or removed each year. a) Construct a recurrence relation for the number of goats on the island at the start ofthe nth year, assuming that during each year an extra 100 goats are put on the island. b) Solve the recurrence relation from part (a) to find the number of goats on the island at the start of the nth year. c) Construct a recurrence relation for the number of goats on the island at the start of the nth year, assuming that n goats are removed during the nth year for each n :::: 3. d) Solve the recurrence relation in part (c) for the number of goats on the island at the start of the nth year. 47. A new employee at an exciting new software company starts with a salary of$50,000 and is promised that at the 7.2 Solving Linear Recurrence Relations 473 end of each year her salary will be double her salary of the previous year, with an extra increment of $10,000 for each year she has been with the company. a) Construct a recurrence relation for her salary for her nth year of employment. b) Solve this recurrence relation to find her salary for her nth year of employment. Some linear recurrence relations that do not have constant co­ efficients can be systematically solved. This is the case for recurrence relations of the form f(n )an = g(n )an-l + h(n). Exercises 48-50 illustrate this. *48. a) Show that the recurrence relation f(n)an = g(n)an-l + h(n), for n :::: 1, and with ao = C, can be reduced to a re­ currence relation of the form bn = bn-1 + Q(n)h(n), where bn = g(n + I)Q(n + l)an, with Q(n) = (f(1)f(2)··· f(n - 1»j(g(l)g(2)··· g(n». b) Use part (a) to solve the original recurrence relation to obtain C + ,£7-1 Q(i)h(i) an = . g(n + I)Q(n + 1) *49. Use Exercise 48 to solve the recurrence relation (n + 1) an = (n + 3)an-l + n, for n :::: 1, with ao = 1. 50. It can be shown that the average number of comparisons made by the quick sort algorithm (described in preamble to Exercise 50 in Section 4.4), when sorting n elements in random order, satisfies the recurrence relation 2 n-l Cn = n + 1 + -L Ck n k=O for n = 1,2, ... , with initial condition Co = O. a) Show that {Cn} also satisfies the recurrence relation nCn = (n + I)Cn-1 + 2n for n = 1,2, .... b) Use Exercise 48 to solve the recurrence relation in part (a) to find an explicit formula for Cn. **51. Prove Theorem 4. **52. Prove Theorem 6. 53. Solve the recurrence relation T(n) = nT2(nj2) with ini­ tial condition T (1) = 6. [Hint: Let n = 2k and then make the substitution ak = log T (2k) to obtain a linear nonho­ mogeneous recurrence relation.] 474 7/ Advanced Counting Techniques 7-26 7.3 Divide-and-Conquer Algorithms and Recurrence Relations linkS� EXAMPLE 1 Extra� Examples� Introduction Many recursive algorithms take a problem with a given input and divide it into one or more smaller problems. This reduction is successively applied until the solutions of the smaller prob­ lems can be found quickly. For instance, we perform a binary search by reducing the search for an element in a list to the search for this element in a list half as long. We successively apply this reduction until one element is left. When we sort a list of integers using the merge sort, we split the list into two halves of equal size and sort each half separately. We then merge the two sorted halves. Another example of this type of recursive algorithm is a procedure for multiplying integers that reduces the problem of the multiplication of two integers to three multiplications of pairs of integers with half as many bits. This reduction is successively applied until integers with one bit are obtained. These procedures are called divide-and-conquer algorithms because they divide a problem into one or more instances of the same problem of smaller size and they conquer the problem by using the solutions of the smaller problems to find a solution of the original problem, perhaps with some additional work. In this section we will show how recurrence relations can be used to analyze the compu­ tational complexity of divide-and-conquer algorithms. We will use these recurrence relations to estimate the number of operations used by many different divide-and-conquer algorithms, including several that we introduce in this section. Divide-and-Conquer Recurrence Relations Suppose that a recursive algorithm divides a problem of size n into a subproblems, where each subproblem is of size nib (for simplicity, assume that n is a multiple of b; in reality, the smaller problems are often of size equal to the nearest integers either less than or equal to, or greater than or equal to, nib). Also, suppose that a total of g(n) extra operations are required in the conquer step of the algorithm to combine the solutions of the subproblems into a solution of the original problem. Then, if f(n) represents the number of operations required to solve the problem of size n, it follows that f satisfies the recurrence relation f(n) = af(n Ib) + g(n). This is called a divide-and-conquer recurrence relation. We will first set up the divide-and-conquer recurrence relations that can be used to study the complexity of some important algorithms. Then we will show how to use these divide-and­ conquer recurrence relations to estimate the complexity of these algorithms. Binary Search We introduced a binary search algorithm in Section 3.1. This binary search algorithm reduces the search for an element in a search sequence of size n to the binary search for this element in a search sequence of size n 12, when n is even. (Hence, the problem of size n has been reduced to one problem of size nI2.) Two comparisons are needed to implement this reduction (one to determine which half of the list to use and the other to determine whether any terms of the list remain). Hence, if f(n) is the number of comparisons required to search for an element in a search sequence of size n, then f(n) = f(nI2) + 2 when n is even. 7-27 7.3 Divide-and-Conquer Algorithms and Recurrence Relations 475 EXAMPLE 2 Finding the Maximum and Minimum of a Sequence Consider the following algorithm for locating the maximum and minimum elements of a sequence ai, a2, ... , an. If n = 1, then al is the maximum and the minimum. If n > 1, split the sequence into two sequences, either where both have the same number of elements or where one of the sequences has one more element than the other. The problem is reduced to finding the maximum and minimum of each of the two smaller sequences. The solution to the original problem results from the comparison of the separate maxima and minima of the two smaller sequences to obtain the overall maximum and minimum. Let f(n) be the total number of comparisons needed to find the maximum and minimum elements of the sequence with n elements. We have shown that a problem of size n can be reduced into two problems of size n /2, when n is even, using two comparisons, one to compare the maxima of the two sequences and the other to compare the minima of the two sequences. This gives the recurrence relation f(n) = 2f(n/2) + 2 when n is even. EXAMPLE 3 Merge Sort The merge sort algorithm (introduced in Section 4.4) splits a list to be sorted with n items, where n is even, into two lists with n /2 elements each, and uses fewer than n comparisons to merge the two sorted lists of n /2 items each into one sorted list. Consequently, the number of comparisons used by the merge sort to sort a list of n elements is less than M (n), where the function M(n) satisfies the divide-and-conquer recurrence relation EXAMPLE 4 lInksE::l M(n) = 2M(n/2) + n. Fast Multiplication ofIntegers Surprisingly, there are more efficient algorithms than the con­ventional algorithm (described in Section 3.6) for multiplying integers. One of these algorithms, which uses a divide-and-conquer technique, will be described here. This fast multiplication al­gorithm proceeds by splitting each of two 2n-bit integers into two blocks, each with n bits. Then, the original multiplication is reduced from the multiplication of two 2n-bit integers to three multiplications of n-bit integers, plus shifts and additions. Suppose that a and b are integers with binary expansions of length 2n (add initial bits of zero in these expansions if necessary to make them the same length). Let Let where Al = (a2n-1 ... an+lanh, BI = (b2n-1 ... bn+lbnh. and Ao = (an-I' .. alaO)2, Bo = (bn-I ••. blboh· The algorithm for fast multiplication of integers is based on the fact that ab can be rewritten as 476 7 / Advanced Counting Techniques 7-28 EXAMPLE 5 linkS� The important fact about this identity is that it shows that the multiplication of two 2n-bit integers can be carried out using three multiplications ofn-bit integers, together with additions, subtractions, and shifts. This shows that if f(n) is the total number of bit operations needed to multiply two n-bit integers, then f(2n) = 3f(n) + Cn. The reasoning behind this equation is as follows. The three multiplications of n-bit integers are carried out using 3 f(n )-bit operations. Each of the additions, subtractions, and shifts uses a constant multiple of n-bit operations, and C n represents the total number of bit operations used by these operations. � Fast Matrix Multiplication In Example 5 of Section 3.8 we showed that multiplying two n x n matrices using the definition of matrix multiplication required n3 multiplications and n2(n -I) additions. Consequently, computing the product of two n x n matrices in this way requires O(n3) operations (multiplications and additions). Surprisingly, there are more efficient divide-and-conquer algorithms for multiplying two n x n matrices. Such an algorithm, invented by V. Strassen in 1969, reduces the multiplication of two n x n matrices, when n is even, to seven multiplications of two (n/2) x (n/2) matrices and 15 additions of(n/2) x (n/2) matrices. (See [CoLeRiStOl] for the details of this algorithm .) Hence, if f(n) is the number of operations (multiplications and additions) used, it follows that f(n) = 7f(n/2)+ 15n2/4 when n is even. As Examples 1-5 show, recurrence relations of the form f(n) = af(n/b) + g(n) arise in many different situations. It is possible to derive estimates of the size of functions that satisfy such recurrence relations. Suppose that f satisfies this recurrence relation whenever n is divisible by b. Let n = bk, where k is a positive integer. Then f(n) = af(n/b) + g(n) = a2f(n/b2) + ag(n/b) + g(n) = a3f(n/b3) + a2g(n/b2) + ag(n/b) + g(n) k-J = akf(n/bk) + L ai g(n/bi). i=O Because n / bk = I, it follows that k-J f(n) = akf(1) + L ai g(n / bi). i=O We can use this equation for f(n) to estimate the size of functions that satisfy divide-and-conquer relations. 7-29 THEOREM 1 7.3 Divide-and-Conquer Algorithms and Recurrence Relations 477 Let f be an increasing function that satisfies the recurrence relation fen) = af(njb) + c whenever n is divisible by b, where a :::: 1, b is an integer greater than 1, and c is a positive real number. Then fen) is ' 1 o (niogba) if a > 1 O(logn) if a = 1. Furthermore, when n = bk, where k is a positive integer, where C1 = f(l) + cj(a -1) and C2 = -cj(a -1). Proof: First let n = bk• From the expression for f(n) obtained in the discussion preceding the theorem, with g(n) = c, we have k-l k-l fen) = akf(1) + L ajc = akf(1) + c L aj. j =O j=O First consider the case when a = 1. Then fen) = f(1) + ck. Because n = bk, we have k = 10gb n. Hence, fen) = f(l) + c 10gb n. When n is not a power of b, we have bk < n < bk+1, for a positive integer k. Because f is increasing, it follows that fen) ::::: f(bk+1) = f(1) + c(k + 1) = (/(1) + c) + ck ::::: (/(1) + c) + clogb n. Therefore, in both cases, fen) is O(logn) when a = 1. Now suppose that a > 1. First assume that n = bk, where k is a positive integer. From the formula for the sum of terms of a geometric progression (Theorem 1 in Section 2.4), it follows that fen) = akf(l) + c(ak -l)j(a -1) = ak[f(1) + cj(a -1)] -cj(a -1) because ak = aiogbn = niogba (see Exercise 4 in Appendix 2), where Cl = f(1) + cj(a -1) and C2 = -cj(a -1). 478 7 / Advanced Counting Techniques 7-30 EXAMPLE 6 Extra� Examples IIiiiiiiiI Now suppose that n is not a power of b. Then bk < n < bk+l, where k is a nonnegative integer. Because f is increasing, f(n) ::::: f(bk+l) = C1ak+1 + C2 ::::: (Cla)alogbn + C2 ::::: (Cla)nlogba + C2, because k ::::: 10gb n < k + 1. Hence, we have f(n) is O(nlogba). Examples 6-9 illustrate how Theorem 1 is used. <J Let f(n) = 5 f(n/2) + 3 and f(1) = 7. Find f(2k), where k is a positive integer. Also, estimate f(n) if f is an increasing function. Solution: From the proof of Theorem 1, with a = 5, b = 2, and c = 3, we see that if n = 2k, then f(n) = ak[f(1) + c/(a -1)] + [-c/(a -1)] = 5k[7 + (3/4)] -3/4 = 5k(31/4) -3/4. Also, if f(n) is increasing, Theorem 1 shows that f(n) is o (n1ogb a) = O(n1og5). We can use Theorem 1 to estimate the computational complexity of the binary search algorithm and the algorithm given in Example 2 for locating the minimum and maximum of a sequence. EXAMPLE 7 Estimate the number of comparisons used by a binary search. Solution: In Example 1 it was shown that f(n) = f(n/2) + 2 when n is even, where f is the number of comparisons required to perform a binary search on a sequence of size n. Hence, from Theorem 1, it follows that f(n) is O(logn). <III! EXAMPLE 8 Estimate the number of comparisons used to locate the maximum and minimum elements in a sequence using the algorithm given in Example 2. Solution: In Example 2 we showed that f(n) = 2f(n/2) + 2, when n is even, where f is the number of comparisons needed by this algorithm. Hence, from Theorem 1, it follows that f(n) is O(n1og2) = O(n). <III! We now state a more general, and more complicated, theorem, which has Theorem 1 as a special case. This theorem (or more powerful versions, including big-Theta estimates) is sometimes known as the Master Theorem because it is useful in analyzing the complexity of many important divide-and-conquer algorithms. 7-31 THEOREM 2 7.3 Divide-and-Conquer Algorithms and Recurrence Relations 479 MASTER THEO REM Let f be an increasing function that satisfies the recurrence relation f(n) = af(n/b) + end whenever n = bk, where k is a positive integer, a :::: 1, b is an integer greater than 1, and e and d are real numbers with e positive and d nonnegative. Then The proof of Theorem 2 is left for the reader as Exercises 29-33 at the end of this section. EXAM PLE 9 Complexity of Merge Sort In Example 3 we explained that the number of comparisons used by the merge sort to sort a list of n elements is less than M(n), where M(n) = 2M(n/2) + n. By the Master Theorem (Theorem 2) we find that M(n) is O(n logn), which agrees with the estimate found in Section 4.4. .... EXAMPLE 10 Estimate the number of bit operations needed to multiply two n-bit integers using the fast multiplication algorithm described in Example 4. Solution: Example 4 shows that f(n) = 3f(n/2) + en, when n is even, where f(n) is the number of bit operations required to multiply two n-bit integers using the fast multiplication algorithm. Hence, from the Master Theorem (Theorem 2), it follows that f(n) is O(nlog3). Note that log 3 ,...., 1.6. Because the conventional algorithm for multiplication uses O(n2) bit operations, the fast multiplication algorithm is a substantial improvement over the conventional algorithm in terms of time complexity for sufficiently large integers, including large integers that occur in practical applications. .... EXAM PLE 11 Estimate the number of multiplications and additions required to multiply two n x n matrices using the matrix multiplication algorithm referred to in Example 5. EXAMPLE 12 unkS� Solution: Let f(n) denote the number of additions and multiplications used by the algorithm mentioned in Example 5 to multiply two n x n matrices. We have f(n) = 7 f(n/2) + 15n2/4, when n is even. Hence, from the Master Theorem (Theorem 2), it follows that f(n) is O(nlog7). Note that log 7 ,...., 2.8. Because the conventional algorithm for multiplying two n x n matrices uses O(n3) additions and multiplications, it follows that for sufficiently large integers n, including those that occur in many practical applications, this algorithm is substantially more efficient in time complexity than the conventional algorithm. .... THE CLOSEST -PAIR PROBLEM We conclude this section by introducing a divide-and­ conquer algorithm from computational geometry, the part of discrete mathematics devoted to algorithms that solve geometric problems. The Closest-Pair Problem Consider the problem of determining the closest pair of points in a set of n points (Xl, Yl), ... , (Xn, Yn) in the plane, where the distance between two points (Xi, Yi) and (Xj, Yj) is the usual Euclidean distance J(Xi - Xj)2 + (Yi - Yj)2. This problem arises in 480 7 / Advanced Counting Techniques • • • • • R In this illustration the problem of finding the closest pair in a set of 16 points is reduced to two problems of finding the closest pair in a set of eight points and the problem of determining whether there are points closer than d = min(dL, dR) within the strip of width 2d centered at e . 7-32 FIGURE 1 The Recursive Step of the Algorithm for Solving the Closest-Pair Problem. many applications such as determining the closest pair of airplanes in the air space at a particular altitude being managed by an air traffic controller. How can this closest pair of points be. found in an efficient way? Solution: To solve this problem we can first determine the distance between every pair of points and then find the smallest of these distances. However, this approach requires O(n2) computations of distances and comparisons because there are C(n, 2) = n(n -1)/2 pairs of points. Surprisingl y, there is an elegant divide-and-conquer algorithm that can solve the closest­ pair problem for n points using 0 (n log n) computations of distances and comparisons. The algorithm we describe here is due to Michael Samos (see [PrSa85]). For simplicity, we assume that n = 2k, where k is a positive integer. (We avoid some technical considerations that are needed when n is not a power of 2.) When n = 2, we have only one pair of points; the distance between these two points is the minimum distance. At the start of the algorithm we use the merge sort twice, once to sort the points in order of increasing x coordinate s, and once to sort the points in order of increasing y coordinate s. Each of these sorts requires o (n log n) operations . We will use these sorted lists in each recursive step. The recursive part of the algorithm divides the problem into two subproblems, each involving half as many points. Using the sorted list of the points by their x coordinate s, we construct a vertical line e dividing the n points into two parts, a left part and a right part of equal size, each containing n /2 points, as shown in Figure 1. (If any points fall on the dividing line e, we divide them among the two parts if necessary.) At subsequent steps of the recursion we need not sort on x coordinates again, because we can select the corresponding sorted subset of all the points. This selection is a task that can be done with O(n) comparisons. There are three possibilities concerning the positions of the closest points: (l) they are both in the left region L, (2) they are both in the right region R, or (3) one point is in the left region and the other is in the right region. Apply the algorithm recursively to compute dL and dR, where dL is the minimum distance between points in the left region and dR is the minimum distance between points in the right region. Let d = min(dL, dR). To successfu lly divide the problem of finding the closest two points in the original set into the two problems of finding the shortest distances between points in the two regions separately, we have to handle the conquer part of the algorithm, which requires that we consider the case where the closest points lie in different regions, that is, one point is in L and the other in R. Because there is a pair of points at distance d where both points lie in R or both points lie in L, for the closest points to lie in different regions requires that they must be a distance less than d apart. 7-33 e d/2 d/2 d/2 d/2 p d/2 7.3 Divide-and-Conquer Algorithms and Recurrence Relations 481 d/2 � At most eight point�, including p, can lie in or on the 2d x d rectangle centered at € because at most one point can lie in or on each of the eight (d/2) x (d/2) squares. FIGUR E 2 Showing That There Are at Most Seven Other Points to Consider for Each Point in the Strip. For a point in the left region and a point in the right region to lie at a distance less than d apart, these points must lie in the vertical strip of width 2d that has the line e as its center. (Otherwi se, the distance between these points is greater than the difference in their x coordinates, which exceeds d.) To examine the points within this strip, we sort the points so that they are listed in order of increasing y coordinates, using the sorted list of the points by their y coordinate s. At each recursive step, we form a subset of the points in the region sorted by their y coordinates from the already sorted set of all points sorted by their y coordinates, which can be done with O(n) comparisons. Beginning with a point in the strip with the smallest y coordinate, we successively examine each point in the strip, computing the distance between this point and all other points in the strip that have larger y coordinates that could lie at a distance less than d from this point. Note that to examine a point p, we need only consider the distances between p and points in the set that lie within the rectangle of height d and width 2d with p on its base and with vertical sides at distance d from e. We can show that there are at most eight points from the set, including p, in or on this 2d x d rectangle. To see this, note that there can be at most one point in each of the eight d /2 x d /2 squares shown in Figure 2. This follows because the farthest apart points can be on or within one of these squares is the diagonal length d /,J2 (which can be found using the Pythagorean Theorem), which is less than d, and each of these d/2 x d/2 squares lies entirely within the left region or the right region. This means that at this stage we need only compare at most seven distances, the distances between p and the seven or fewer other points in or on the rectangle, with d. Because the total number of points in the strip of width 2d does not exceed n (the total number of points in the set), at most 7n distances need to be compared with d to find the minimum distance between points. That is, there are only 7n possible distances that could be less than d. Consequently, once the merge sort has been used to sort the pairs according to their x coordinates and according to their y coordinates, we find that the increasing function f(n) satisfying the recurrence relation fen) = 2f(n/2) + 7n, 482 7 / Advanced Counting Techniques 7-34 where f(2) = 1, exceeds the number of comparisons needed to solve the closest-pair problem for n points. By the Master Theorem (Theorem 2), it follows that fen) is O(n logn). The two sorts of points by their x coordinates and by their y coordinates each can be done using 0 (n log n) comparisons, by using the merge sort, and the sorted subsets of these coordinates at each of the O(logn) steps of the algorithm can be done using O(n) comparisons each. Thus, we find that the closest-pair problem can be solved using O(n logn) comparisons. .... Exercises 1. How many comparisons are needed for a binary search in a set of 64 elements? 2. How many comparisons are needed to locate the max­ imum and minimum elements in a sequence with 128 elements using the algorithm in Example 2? 3. Multiply (111O)z and (101O)z using the fast multiplication algorithm. 4. Express the fast multiplication algorithm in pseudocode. 5. Determine a value for the constant C in Example 4 and use it to estimate the number of bit operations needed to multiply two 64-bit integers using the fast multiplication algorithm. 6. How many operations are needed to mUltiply two 32 x 32 matrices using the algorithm referred to in Example 4? 7. Suppose that fen) = f(n/3) + 1 when n is divisible by 3, and f(l) = 1. Find a) f(3). b) f(27). c) f(729). 8. Suppose that fen) = 2f(n/2) + 3 when n is even, and f(1) = 5. Find a) f(2). b) f(8). c) f(64). d) f(1024). 9. Suppose that fen) = f(n/5) + 3n2 when n is divisible by 5, and f(l) = 4. Find a) f(5). b) f(125). c) f(3125). 10. Find f(n) when n = 2k, where f satisfies the recurrence relation fen) = f(n/2) + 1 with f(1) = 1. 11. Estimate the size of f in Exercise 10 if f is an increasing function. 12. Find f(n) when n = 3k, where f satisfies the recurrence relation fen) = 2f(n/3) + 4 with f(l) = 1. 13. Estimate the size of f in Exercise 12 if f is an increasing function. 14. Suppose that there are n = 2k teams in an elimination tournament, where there are n /2 games in the first round, with the n /2 = 2k-1 winners playing in the second round, and so on. Develop a recurrence relation for the number of rounds in the tournament. 15. How many rounds are in the elimination tournament de­ scribed in Exercise 14 when there are 32 teams? 16. Solve the recurrence relation for the number of rounds in the tournament described in Exercise 14. 17. Suppose that the votes of n people for different candi­ dates (where there can be more than two candidates) for a particular office are the elements of a sequence. A person wins the election ifthis person receives a majority of the votes. a) Devise a divide-and-conquer algorithm that deter­ mines whether a candidate received a majority and, ifso, determine who this candidate is. [Hint: Assume that n is even and split the sequence of votes into two sequences, each with n /2 elements. Note that a candidate could not have received a ma jority of votes without receiving a majority of votes in at least one of the two halves.] b) Use the Master Theorem to estimate the number of comparisons needed by the algorithm you devised in part (a). 18. Suppose that each person in a group of n people votes for exactly two people from a slate of candidates to fill two positions on a committee. The top two finishers both win positions as long as each receives more than n /2 votes. a) Devise a divide-and-conquer algorithm that deter­ mines whether the two candidates who received the most votes each received at least n /2 votes and, if so, determine who these two candidates are. b) Use the Master Theorem to estimate the number of comparisons needed by the algorithm you devised in part (a). 19. a) Set up a divide-and-conquer recurrence relation for the number of multiplications required to compute xn , where x is a real number and n is a positive integer, using the recursive algorithm from Exercise 26 in Sec­ tion 4.4. b) Use the recurrence relation you found in part (a) to construct a big-O estimate for the number of mul­ tiplications used to compute xn using the recursive algorithm. 20. a) Set up a divide-and-conquer recurrence relation for: the number of modular multiplications required to com­ pute an mod m, where a, m, and n are positive inte­ gers, using the recursive algorithms from Example 3 in Section 4.4. b) Use the recurrence relation you found in part (a) to construct a big-O estimate for the number of modular multiplications used to compute an mod m using the recursive algorithm. 7-35 21. Suppose that the function f satisfies the recurrence rela­ tion fen) = 2f(Jn) + 1 whenever n is a perfect square greater than 1 and f(2) = 1. a) Find f(16). b) Find a big-O estimate for f(n). [Hint: Make the sub­ stitution m = log n.] 22. Suppose that the function f satisfies the recurrence re­ lation fen) = 2f(Jn) + logn whenever n is a perfect square greater than 1 and f(2) = 1. a) Find f(16). b) Find a big-O estimatefor fen). [Hint: Make the sub­ stitution m = log n.] **23. This exercise deals with the problem offinding the largest sum of consecutive terms of a sequence of n real numbers. When all terms are positive, the sum of all terms provides the answer, but the situation is more complicated when some terms are negative. For example, the maximum sum of consecutive terms of the sequence -2, 3, -1, 6, -7,4 is 3 + (-1) + 6 = 8. (This exercise is based on [Be86].) a) Use pseudocode to describe an algorithm that solves this problem by finding the sums of consecutive terms starting with the first term, the sums of consecutive terms starting with the second term, and so on, keep­ ing track of the maximum sum found so far as the algorithm proceeds. b) Determine the computational complexity of the al­ gorithm in part (a) in terms of the number of sums computed and the number of comparisons made. c) Devise a divide-and-conquer algorithm to solve this problem. [Hint: Assume that there are an even num­ ber ofterms in the sequence and split the sequence into two halves. Explain how to handle the case when the maximum sum of consecutive terms includes terms in both halves.] d) Use the algorithm from part (c) to find the maxi­ mum sum of consecutive terms of each of the se­ quences: -2, 4,-1,3,5,-6,1,2;4,1,-3,7,-1,-5, 3, -2; and -1, 6, 3, -4, -5, 8, -1, 7. e) Find a recurrence relation for the number of sums and comparisons used by the divide-and-conquer al­ gorithm from part (c). 1) Use the Master Theorem to estimate the computational complexity of the divide-and-conquer algorithm. How does it compare in terms of computational complexity with the algorithm from part (a)? 24. Apply the algorithm described in Example 12 for finding the closest pair of points, using the Euclidean distance be­ tween points, to find the closest pair of the points (1, 3), (1,7), (2, 4), (2, 9), (3,1), (3, 5), (4, 3), and (4, 7). 25. Apply the algorithm described in Example 12 for finding the closest pair of points, using the Euclidean distance be­ tween points, to find the closest pair of the points (1, 2), (1,6), (2,4), (2,8), (3, 1), (3,6), (3, 10), (4,3), (5, 1), (5,5), (5, 9), (6, 7), (7,1), (7,4), (7, 9), and (8,6). 7.3 Divide-and-Conquer Algorithms and Recurrence Relations 483 *26. Use pseudocode to describe the recursive algorithm for solving the closest-pair problem as described in Exam­ ple 12. 27. Construct a variation of the algorithm described in Ex­ ample 12 along with justifications of the steps used by the algorithm to find the smallest distance between two points if the distance between two points is defined to be d«x;, Y;), (Xj, Yj» = max(lx; -xjl, Iy; -Yjl)· *28. Suppose someone picks a number x from a set of n num­ bers. A second person tries to guess the number by succes­ sively selecting subsets of the n numbers and asking the first person whether x is in each set. The first person an­ swers either "yes" or "no." When the first person answers � each query truthfully, we can find x using log n queries by successively splitting the sets used in each query in half. Ularn's problem, proposed by Stanislaw Ulam in 1976, asks for the number of queries required to find x, supposing that the first person is allowed to lie exactly once. a) Show that by asking each question twice, given a num­ ber x and a set with n elements, and asking one more question when we find the lie, Ulam's problem can be solved using 2 log n + 1 queries. b) Show that by dividing the initial set of n elements into four parts, each with n /4 elements, 114 of the elements can be eliminated using two queries. [Hint: Use two queries, where each of the queries asks whether the element is in the union of two of the subsets with n/4 elements and where one ofthe subsets of n /4 elements is used in both queries.] c) Show from part (b) that if fen) equals the number of queries used to solve Ularn's problem using the method from part (b) and n is divisible by 4, then fen) = f(3n/4) + 2. d) Solve the recurrence relation in part (c) for f(n). e) Is the naive way to solve Ulam's problem by asking each question twice or the divide-and-conquer method based on part (b) more efficient? The most efficient way to solve Ularn's problem has been determined by A. Pelc [Pe87]. In Exercises 29-33, assume that f is an increasing func­ tion satisfying the recurrence relation f(n) = af(n/b) + end, where a ::: 1, b is an integer greater than 1, and e and d are positive real numbers. These exercises supply a proof of Theorem 2. *29. Show that if a = bd and n is a power of b, then f(n) = f(l)nd + end 10gb n. 30. Use Exercise 29 to show that if a = bd, then f(n) is O(nd logn). *31. Show that if a =1= bd and n is a power of b, then fen) = C]nd + C2n]ogba, where C] = bde/(bd - a) and C2 = f(l) + bde/(a -bd). 32. Use Exercise 31 to show that if a < bd, then fen) is O(nd). 33. Use Exercise 31 to show that if a> bd, then fen) is o (n]ogb a). 484 7 / Advanced Counting Techniques 7-36 34. Find f(n) when n = 4k, where f satisfies the recurrence relation f(n) = 5f(n/4) + 6n, with f(l) = 1. 36. Find f(n) when n = 2k, where f satisfies the recurrence relation f(n) = 8f(n/2) + n2 with f(l) = 1. 35. Estimate the size of f in Exercise 34 if f is an increasing function. 37. Estimate the size of f in Exercise 36 if f is an increasing function. 7.4 Generating Functi ons unkS� DEFINITION 1 EXAMPLE 1 Exam=� Introduction Generating functions are used to represent sequences efficiently by coding the terms of a se­ quence as coefficie nts of powers of a variable x in a formal power series. Generating functions can be used to solve many types of counting problems, such as the number of ways to select or distribute objects of different kinds, subject to a variety of constraints, and the number of ways to make change for a dollar using coins of different denomina tions. Generating functions can be used to solve recurrence relations by translating a recurrence relation for the terms of a sequence into an equation involving a generating function. This equation can then be solved to find a closed form for the generating function. From this closed form, the coefficients of the power series for the generating function can be found, solving the original recurrence relation. Generating functions can also be used to prove combinatorial identities by taking advantage of relatively simple relationships between functions that can be translated into identities involving the terms of sequences . Generating functions are a helpful tool for studying many properties of sequences besides those described in this section, such as their use for establishing asymptotic formulae for the terms of a sequence. We begin with the definition of the generating function for a sequence. The generatingfunctionfor the sequence ao, al,"" ako ... of real numbers is the infinite series 00 G(x) = ao + alx + ... + akxk + ... = L akxk. k=O Remark: The generating function for {ak} given in Definition 1 is sometimes called the ordinary generating function of {ak} to distinguish it from other types of generating functions for this sequence. The generating functions for the sequences {ak} with ak = 3, ak = k + 1, and ak = 2k are I:r:'= 0 3xk, I:r:'= o(k + 1 )xk, and I:r:'= 0 2k xk, respectively. .... We can define generating functions for finite sequences of real numbers by extending a finite sequence ao, ai, ... , an into an infinite sequence by setting an+l = 0, an+2 = 0, and so on. The generating function G(x) of this infinite sequence {an} is a polynomial of degree n because no terms of the form ajxj with j > n occur, that is, 7-37 7.4 Generating Functions 485 EXAM PLE 2 What is the generating function for the sequence 1, 1, 1, 1, 1, I? Solution: The generating function of 1, 1, 1, 1, 1, 1 is By Theorem 1 of Section 2.4 we have whenx =f. 1. Consequently, G(x) = (x6 -l)/(x -1) is the generating function of the sequence 1, 1, 1, 1, 1, 1. [Because the powers of x are only place holders for the terms of the sequence in a generating function, we do not need to worry that G(1) is undefined.] .... EXAM PLE 3 Let m be a positive integer. Let ak = C(m, k), for k = 0,1,2, ... , m. What is the generating function for the sequence ao, ai, ... , am? Solution: The generating function for this sequence is G(x) = C(m, 0) + C(m, l)x + C(m, 2)x2 + ... + C(m, m)xm. The Binomial Theorem shows that G(x) = (1 + x)m. Useful Facts About Power Series When generating functions are used to solve counting problems, they are usually considered to be formal power series. Questions about the convergence of these series are ignored. However, to apply some results from calculus, it is sometimes important to consider for which x the power series converges. The fact that a function has a unique power series around x = 0 will also be important. Generally, however, we will not be concerned with questions of convergence or the uniqueness of power series in our discussio ns. Readers familiar with calculus can consult textbooks on this subject for details about power series, including the convergence of the series we consider here. We will now state some important facts about infinite series used when working with generating functions. A discussio n of these and related results can be found in calculus texts. EXAM PLE 4 The function f(x) = 1/(1 - x) is the generating function ofthe sequence 1, 1, 1, 1, ... , because 1/(1-x)=1+x+x2+ ... for Ixl < 1. EXAM PLE 5 The function f(x) = 1/(1 -ax) is the generating function of the sequence 1, a, a2, a3, ••. , because 1/(1 -ax) = 1 + ax + a2x2 + ... when laxl < 1, or equivalently, for Ixl < l/lal for a =f. O. We also will need some results on how to add and how to multiply two generating functions. Proofs of these results can be found in calculus texts. 486 7 / Advanced Counting Techniques 7-38 THEOREM ! Remark: Theorem 1 is valid only for power series that converge in an interval, as all series considered in this section do. However, the theory of generating functions is not limited to such series. In the case of series that do not converge, the statements in Theorem 1 can be taken as definitions of addition and multiplication of generating functions. We will illustrate how Theorem 1 can be used with Example 6. EXAMPLE 6 Let f(x) = 1/(1 -xi. Use Example 4 to find the coefficients ao, ai, a2, ... in the expansion f(x) = Lr'=o akxk. DEFINITION 2 Solution: From Example 4 we see that 1/(1 -x) = 1 + x + x2 + x3 + .... Hence, from Theorem 1, we have Remark: This result also can be derived from Example 4 by differen tiation. Taking derivatives is a useful technique for producing new identities from existing identities for generating functions. To use generating functions to solve many important counting problems, we will need to apply the Binomial Theorem for exponents that are not positive integers. Before we state an extended version of the Binomial Theorem, we need to define extended binomial coefficients. Let u be a real number and k a nonnegative integer. Then the extended binomial coefficient m is defined by (�) = {�(u -1)·· . (u - k + 1)/ k! if k > 0, ifk = O. EXAMPLE 7 Find the values of the extended binomial coefficients (-;2) and C�2). Solution: Taking u = -2 and k = 3 in Definition 2 gives us (-2) = (-2)(-3) (-4) = -4. 3 3! 7-39 Similarly, taking u = 1/2 and k = 3 gives us (1/2) = (1/2)(1/2 -1)(1/2 -2) 3 3! = (1/2)(-1/2)(-3/2)/6 = 1/16. 7.4 Generating Functions 487 Example 8 provides a useful formula for extended binomial coefficients when the top parameter is a negative integer. It will be useful in our subsequent discussio ns. EXAMPLE 8 When the top parameter is a negative integer, the extended binomial coefficient can be expressed in terms of an ordinary binomial coefficient. To see that this is the case, note that THEOREM 2 (-n) = (-n)(-n -1)·· ·(-n -r + 1) r r! = (-Iyn(n + 1)·· ·(n +r -1) r! (-Inn +r -I)(n +r -2)·· ·n r! (-lnn+r-l)! r!(n -I)! = (-lr(n +: -1) = (-IrC(n +r -I,r). We now state the extended Binomial Theorem. by definition of extended binomial coeffici ent factoring out -I from each term in the numerator by the commutative law for multipli cation multiplying both the numerator and denomina tor by(n -I)! by the definition of binomial coeffici ents using alter native notation for binomial coeffici ents THE EXTENDED BINOMIAL THEOREM Let x be a real number with Ix I < 1 and let u be a real number. Then Theorem 2 can be proved using the theory of the Maclaurin series. We leave its proof to the reader with a familiarity with this part of calculus. Remark: When u is a positive integer, the extended Binomial Theorem reduces to the Binomial Theorem presented in Section 5.4, because in that case G) = 0 if k > u. Example 9 illustrates the use of Theorem 2 when the exponent is a negative integer. 488 7 / Advanced Counting Techniques 7-40 EXAM PLE 9 Find the generating functions for (1 + x )-n and (1 -x )-n, where n is a positive integer, using the extended Binomial Theorem. Solution: By the extended Binomial Theorem, it follows that Using Example 8, which provides a simple formula for C"kn), we obtain 00 (1 + x)-n = L(-I)kC(n + k -1, k)xk. k=O Replacing x by -x, we find that 00 (1-x)-n = LC(n +k-l,k)xk. k=O Table 1 presents a useful summary of some generating functions that arise frequently. Remark: Note that the second and third formulae in this table can be deduced from the first formula by substituting ax and xr for x, respective ly. Similarly, the sixth and seventh formulae can be deduced from the fifth formula using the same substitutions. The tenth and eleventh can be deduced from the ninth formula by substituting -x and ax for x, respectively. Also, some of the formulae in this table can be derived from other formulae using methods from calculus (such as differentiation and integration). Students are encouraged to know the core formulae in this table (that is, formulae from which the others can be derived, perhaps the first, fourth, fifth, eighth, ninth, twelfth, and thirteenth formulae) and understand how to derive the other formulae from these core formulae. Counting Problems and Generating Functions Generating functions can be used to solve a wide variety of counting problems. In particula r, they can be used to count the number of combinations of various types. In Chapter 5 we developed techniques to count the r-combinations from a set with n elements when repetition is allowed and additional constraints may exist. Such problems are equivalent to counting the solutions to equations of the form e, + e2 + ... + en = C, where C is a constant and each ei is a nonnegative integer that may be subject to a specified constraint. Generating functions can also be used to solve counting problems of this type, as Examples 10-12 show. EXAMPLE 10 Find the number of solutions of where e" e2, and e3 are nonnegative integers with 2 :s e, :s 5,3 :s e2 :s 6, and 4 :s e3 :s 7. 7-41 TABLE 1 Useful Generatin g Functions. G(x) n (1 + x)n = LC(n,k)xk k=O = I +C(n, I)x + C(n, 2)x2 + ... +xn n (l + ax)n = L C(n, k)akxk k=O = I + C(n, I)ax + C(n, 2)a2x2 + ... + anxn n (I + xr)n = L C(n, k)xrk k=O = I + C(n, I)xr + C(n, 2)x2r + ... + xrn 1 -xn+1 n I-x = L xk = I + x + x 2 + ... + xn k=O 1 00 --= L xk = 1 + x + x 2 + ... I-x k=O 1 00 --= L akxk = 1 + ax + a2x2 + ... 1 -ax k=O I 00 --= L xrk = 1 + xr + x2r + ... 1-xr k=O 1 00 (l -X)2 = L(k + I)xk = 1 + 2x + 3x2 + ... k=O I 00 (1 -x)n = L C(n + k -I, k)xk k=O = I +C(n, I)x+C(n+ I,2)x2+ ... I 00 (1 +x)n = L C(n + k -I, k)(_I)kxk k=O = I -C(n, I)x + C(n + 1, 2)x2 -... I 00 (l -ax)n = LC(n +k-I,k)akxk k=O = 1+ C(n, I)ax + C(n + I, 2)a2x2 + ... 00 xk x2 x3 ex=L-=I+x+-+-+ ... k=O k! 2! 3! 00 (_I)k+1 k x2 x3 X4 In(I + x) = L ---x = x --+ ---+ ... k=O k 2 3 4 7.4 Generating Functions 489 ak C(n, k) C(n, k)ak C(n, k/r) ifr I k; 0 otherwise I if k :s n; 0 otherwise 1 ak 1 if r I k; 0 otherwise k+I C(n + k -I, k) = C(n + k -1, n -1) (-I)kC(n + k -1, k) = (-I)kC(n + k -1, n -1) C (n + k -I, k )ak = C (n + k -I, n - 1 )ak 1/ k! (-I)k+l/k Note. The series for the last two generating functions can be found in most calculus books when power series are discussed. 490 7 / Advanced Counting Techniques 7-42 Solution: The number of solutions with the indicated constraints is the coefficient of x 17 in the expansion of This follows because we obtain a term equal to x 17 in the product by picking a term in the first sum xe" a term in the second sum xe2, and a term in the third sum xe3, where the exponents el, e2, and e3 satisfy the equation el + e2 + e3 = 17 and the given constraints. It is not hard to see that the coefficient of xl7 in this product is 3. Hence, there are three solutions. (Note that the calculating ofthis coefficient involves about as much work as enumer­ ating all the solutions of the equation with the given constraints. However, the method that this illustrates often can be used to solve wide classes of counting problems with special formu­ lae, as we will see. Furthermore, a computer algebra system can be used to do such computa­ tions.) .... EXAMPLE 11 In how many different ways can eight identical cookies be distributed among three distinct children if each child receives at least two cookies and no more than four cookies? Solution: Because each child receives at least two but no more than four cookies, for each child there.is a factor equal to in the generating function for the sequence {cn}, where Cn is the number of ways to distribute n cookies. Because there are three children, this generating function is We need the coefficient of x8 in this product. The reason is that the x8 terms in the expansion correspond to the ways that three terms can be selected, with one from each factor, that have exponents adding up to 8. Furthermore, the exponents of the term from the first, second, and third factors are the numbers of cookies the first, second, and third children receive, respectively. Computation shows that this coefficient equals 6. Hence, there are six ways to distribute the cookies so that each child receives at least two, but no more than four, cookies. .... EXAMPLE 12 Use generating functions to determine the number of ways to insert tokens worth $1, $2, and $5 into a vending machine to pay for an item that costs r dollars in both the cases when the order in which the tokens are inserted does not matter and when the order does matter. (For example, there are two ways to pay for an item that costs $3 when the order in which the tokens are inserted does not matter: inserting three $1 tokens or one $1 token and a $2 token. When the order matters, there are three ways: inserting three $1 tokens, inserting a $1 token and then a $2 token, or inserting a $2 token and then a $1 token.) Solution: Consider the case when the order in which the tokens are inserted does not matter. Here, all we care about is the number of each token used to produce a total of r dollars. Because we can use any number of $1 tokens, any number of $2 tokens, and any number of $5 tokens, the answer is the coefficient of xr in the generating function 7-43 7.4 Generating Functions 491 (The first factor in this product represents the $1 tokens used, the second the $2 tokens used, and the third the $5 tokens used.) For example, the number of ways to pay for an item costing $7 using $1, $2, and $5 tokens is given by the coefficient of x 7 in this expansion, which equals 6. When the order in which the tokens are inserted matters, the number of ways to insert exactly n tokens to produce a total of r dollars is the coefficient of xr in because each of the r tokens may be a $1 token, a $2 token, or a $5 token. Because any number of tokens may be inserted, the number of ways to produce r dollars using $1, $2, or $5 tokens, when the order in which the tokens are inserted matters, is the coefficient of xr in 1 1 + (x + x2 + x5) + (x + x2 + x5i + ... = -----::- ----::-1 -(x + x2 + x5) 1 = ------:: ----=-1 -x -x2 -x5' where we have added the number of ways to insert 0 tokens, 1 token, 2 tokens, 3 tokens, and so on, and where we have used the identity 1/(1 -x) = 1 + x + x2 + ... with x replaced with x + x2 + x5. For example, the number of ways to pay for an item costing $7 using $1, $2, and $5 tokens, when the order in which the tokens are used matters, is the coefficient of x 7 in this expansion, which equals 26. [Hint: To see that this coefficient equals 26 requires the addition of the coefficients of x 7 in the expansions (x + x2 + x5)k for 2 :'S k :'S 7. This can be done by hand with considerable computation, or a computer algebra system can be used.] .... Example 13 shows the versatility of generating functions when used to solve problems with differing assumptions. EXAMPLE 13 Use generating functions to find the number of k-combinations of a set with n elements. Assume that the Binomial Theorem has already been established. Solution: Each of the n elements in the set contributes the term (1 + x) to the generating function f(x) = L�=o akxk. Here f(x) is the generating function for {ak}, where ak represents the number of k-combinations of a set with n elements. Hence, f(x) = (1 +xt· But by the Binomial Theorem, we have where (n) n! k -k!(n -k)! . Hence, C (n, k), the number of k-combinations of a set with n elements, is n! k!(n - k)! 492 7 / Advanced Counting Techniques 7-44 Remark: We proved the Binomial Theorem in Section 5.4 using the formula for the number of r-combinations of a set with n elements. This example shows that the Binomial Theorem, which can be proved by mathematical induction, can be used to derive the formula for the number of r-combinations of a set with n elements. EXAMPLE 14 Use generating functions to find the number ofr-combinations from a set with n elements when repetition of elements is allowed. Solution: Let G(x) be the generating function for the sequence {ar}, where ar equals' the number of r-combinations of a set with n elements with repetitions allowed. That is, G(x) = L�= 0 arxr . Because we can select any number of a particular member of the set with n elements when we form an r-combination withrepetition allowed, each of the n elements con­ tributes (1 + x + x2 + x3 + ... ) to a product expansion for G(x). Each element contributes this factor because it may be selected zero time s, one time, two time s, three times, and so on, when an r-combination is formed (with a total ofr elements selected). Because there are n elements in the set and each contributes this same factor to G(x), we have G(x) = (l +x +x2 + .. ·t. As long as Ix I < 1, we have 1 + x + x2 + ... = 1 j (l -x), so G(x) = Ij(l-x)n = (l-x)-n. Applying the extended Binomial Theorem (Theorem 2), it follows that The number of r-combinations of a set with n elements with repetitions allowed, when r is a positive integer, is the coefficient ar of xr in this sum. Consequently, using Example 8 we find that ar equals (�n) (-IY = (-IYC(n +r -l,r). (-IY = C(n +r -l,r). Note that the result in Example 14 is the same result we stated as Theorem 2 in Section 5.5. EXAMPLE 15 Use generating functions to find the number of ways to select r objects of n different kinds if we must select at least one object of each kind. Solution: Because we need to select at least one object of each kind, each of the n kinds of objects contributes the factor (x + x2 + x3 + ... ) to the generating function G(x) for the sequence {ar}, where ar is the number of ways to select r objects of n different kinds if we need at least one object of each kind. Hence, 7-45 EXAMPLE 16 Extra� Exampleslliiiiiill Using the extended Binomial Theorem and Example 8, we have G(x) = xn /(1 -x)n = xn . (1 -x )-n 00 = xn L(-lYC(n + r -1, r)(-lYxr r=O 00 = LC(n +r -l,r)xn+r r=O 00 = L C(t -1, t -n)xt t=n 00 = L C (r -1, r -n )xr . r=n 7.4 Generating Functions 493 We have shifted the summation in the next-to-Iast equality by setting t = n + r so that t = n when r = 0 and n + r -1 = t -1, and then we replaced t by r as the index of summation in the last equality to return to our original notation. Hence, there are C (r -1, r -n) ways to select r objects of n different kinds if we must select at least one object of each kind. .... Using Generating Functions to Solve Recurrence Relations We can find the solution to a recurrence relation and its initial conditions by finding an explicit formula for the associated generating function. This is illustrated in Examples 16 and 17. Solve the recurrence relation ak = 3ak-1 for k = 1, 2, 3, ... and initial condition ao = 2. Solution: Let G(x) be the generating function for the sequence {ak}, that is, G(x) = Lr= 0 akxk. First note that 00 00 xG(x) = Lakxk+1 = Lak_IXk. k=O k=1 Using the recurrence relation, we see that 00 00 G(x) -3xG(x) = Lakxk - 3 Lak_IXk k=O k= I 00 = ao + L(ak -3ak_dxk k=1 =2, 494 7 / Advanced Counting Techniques because ao = 2 and ak = 3ak-l. Thus, G(x) -3xG(x) = (l -3x)G(x) = 2. 7-46 Solving for G(x) shows that G(x) = 2/(1 -3x). Using the identity 1/(1 -ax) = Lr'=o akxk, from Table 1, we have 00 00 G(x)=2L3kxk= L2.3kxk. k=O k=O Consequently, ak = 2 . 3k. EXAM PLE 17 Suppose that a valid codeword is an n-digit number in decimal notation containing an even number of Os. Let an denote the number of valid codewords of length n. In Example 7 of Section 7.1 we showed that the sequence {an} satisfies the recurrence relation and the initial condition al = 9. Use generating functions to find an explicit formula for an. Solution: To make our work with generating functions simpler, we extend this sequence by setting ao = 1; when we assign this value to ao and use the recurrence relation, we have al = 8ao + 100 = 8 + I = 9, which is consistent with our original initial condition. (It also makes sense because there is one code word oflength O-the empty string.) We multiply both sides of the recurrence relation by xn to obtain Let G(x) = L�=oanXn be the generating function of the sequence ao, aI, a2, .... We sum both sides of the last equation starting with n = 1, to find that 00 00 G(x) -1 = LanXn = L(8an_IXn + IOn-Ixn) n=1 n=1 00 00 = 8 Lan_IXn + L IOn-Ixn n=1 n=1 00 00 = 8x Lan_IXn-1 +x L IOn-Ixn-1 n=1 n=1 00 00 = 8x LanXn +x L IOnxn n=O n=O = 8xG(x) + x/(1 -lOx), where we have used Example 5 to evaluate the second summation. Therefore, we have G(x) -1 = 8xG(x) + x/(1 -lOx). Solving for G(x) shows that 1 -9x G(x) = -----­(I -8x)(1 -lOx) 7-47 7.4 Generating Functions 495 Expanding the right-hand side of this equation into partial fractions (as is done in the integration of rational functions studied in calculus) gives G(x) = - -- + . 1 (1 1) 2 1 -8x 1 -lOx Using Example 5 twice (once with a = 8 and once with a = 10) gives 00 I = L "2(8n + lOn)xn. n=O Consequently, we have shown that 1 n n an = "2(8 + 10 ). Proving Identities via Generating Functions In Chapter 5 we saw how combinatorial identities could be established using combinatorial proofs. Here we will show that such identities, as well as identities for extended binomial coef­ ficients, can be proved using generating functions. Sometimes the generating function approach is simpler than other approaches, especiall y when it is simpler to work with the closed form of a generating function than with the terms of the sequence themselves. We illustrate how generating functions can be used to prove identities with Example 18. EXAMPLE 18 Use generating functions to show that n L C(n, ki = C(2n, n) k=O whenever n is a positive integer. Solution: First note that by the Binomial Theorem C (2n, n) is the coefficient of xn in (1 + x fn . However, we also have (1 + xfn = [(1 + xy]2 = [C(n, 0) + C(n, l)x + C(n, 2)x2 + ... + C(n, n)xn]2. The coefficient of xn in this expression is C(n, O)C(n, n) + C(n, I)C(n, n -1) + C(n, 2)C(n, n -2) + ... + C(n, n)C(n, 0). This equals L:Z=o C(n, k)2, because C(n, n -k) = C(n, k). Because both C(2n, n) and L:Z = 0 C (n, k)2 represent the coefficient of xn in (1 + X )2n, they must be equal. .... Exercises 42 and 43 at the end of this section ask that Pascal's identity and Vandermonde's identity be proved using generating functions . 496 7 / Advanced Counting Techniques Exercises 1. Find the generating function for the finite sequence 2, 2, 2,2,2,2. 2. Find the generating function for the finite sequence 1, 4, 16,64,256. In Exercises 3-8, by a closed fo rm we mean an algebraic ex­ pression not involving a summation over a range of values or the use of ellipses. 3. Find a closed form for the generating function for each ofthese sequences. (For each sequence, use the most ob­ vious choice of a sequence that follows the pattern of the initial terms listed.) a) 0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, ... b) 0, 0, 0, 1, 1, 1, 1, 1, 1, ... c) 0, 1,0,0, 1,0,0, 1,0,0, 1, .. . d) 2,4,8,16,32,64,128,256, .. . e) (�), (;) , (�). ... , (�) ,0,0,0,0,0, ... t) 2, -2,2, -2,2, -2, 2, -2, ... g) 1, I, 0, 1, I, 1, 1, 1, 1, 1, ... h) 0,0,0, 1,2,3,4, ... 4. Find a closed form for the generating function for each of these sequences. (Assume a general form for the terms of the sequence, using the most obvious choice of such a sequence.) a) -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, ... b) 1,3,9,27,81,243,729, .. . c) 0,0,3, -3, 3, -3,3, -3, .. . d) 1, 2, 1, 1, 1, 1, 1, 1, 1, .. . e) (�), 2(;), 22(�). ... , 27(�), 0, 0, 0, 0, ... t) -3,3, -3, 3, -3,3, ... g) 0, 1, -2,4, -8, 16, -32,64, ... h) 1, 0, 1, 0, 1, 0, 1, 0, ... 5. Find a closed form for the generating function for the sequence {an}, where a) an = 5 for all n = 0, 1, 2, ... . b) an = 3n for all n = 0,1,2, ... . c) an = 2 forn = 3,4,5, ... andao = al = a2 = 0. d) an = 2n + 3 for all n = 0, 1,2, ... . e) an = G) for all n = 0,1,2, ... . t) an = C :4) foralln = 0,1,2, .... 6. Find a closed form for the generating function for the sequence {an}, where a) an = -1 for all n = 0, 1, 2, ... . b) an = 2n for n = 1,2,3,4, ... and ao = 0. c) an = n - 1 for n = 0,1,2, ... . d) an = 1/(n + I)! for n = 0,1,2, .... e) an = (;) for n = 0,1,2, .... t) an = ( 10 ) for n = 0, 1, 2, .... n+l 7-48 7. For each of these generating functions, provide a closed formula for the sequence it determines. a) (3x -4)3 b) (x3 + 1)3 c) 1/(1 -5x) d) x3/(1 + 3x) e) x2 + 3x + 7 + (1/(1 -x2» t) (x4/(1 -x4» -x3 -x2 -x-I g) x2/(I-x)2 h) 2e2x 8. For each of these generating functions, provide a closed formula for the sequence it determines. a) (x2 + 1)3 b) (3x -1)3 c) 1/(1 -2x2) d) x2/(1 -x)3 e) x-l+(1/(1-3x» t) (1 +x3)/(1+x)3 *g) x/(1 +x +x2) h) e3x2 - 1 9. Find the coefficient of x to in the power series of each of these functions. a) (I+X5+XIO+XI5+ ... )3 b) (X3+X4+X5+X6+X7+ ... )3 c) (x4 + x5 + x6)(x3 + x4 + x5 + x6 + x 7)(1 + x + x2 +x3 +x4 + ... ) d) (x2 +X4 +x6 +x8 + ... )(x3 +x6 +x9 + .. ·)(x4 + x8 +x12 + ... ) e) (1 +X2+X4+x6+x8+ ... )(1 +x4+x8+xI2 + ... ) (1 +x6 +x12 +x18 + ... ) 10. Find the coefficient of x9 in the power series of each of these functions. a) (1 +x3 +x6 +x9 + ... ? b) (x2 +x3 +x4 +x5 +x6 + ... )3 c) (x3 + x5 + x6)(x3 + x4)(x + x2 + x3 + x4 + ... ) d) (x +x4 +x7 +xlO + ... )(x2 +x4 +x6 +x8 + ... ) e) (1 + x + x2)3 11. Find the coefficient of x lOin the power series of each of these functions. a) 1/(1 -2x) c) 1/(1 -x)3 e) x4/(1 -3X)3 b) 1/(1 + x)2 d) 1/(1 + 2X)4 12. Find the coefficient of x 12 in the power series of each of these functions. a) 1/(1 +3x) c) 1/(1 +X)8 e) x3/(1 + 4X)2 b) 1/(1 -2X)2 d) 1/(1 -4x)3 13. Use generating functions to determine the number of different ways 10 identical balloons can be given to four children if each child receives at least two balloons. 14. Use generating functions to determine the number of dif­ ferent ways 12 identical action figures can be given to five children so that each child receives at most three action figures. 15. Use generating functions to determine the number of dif­ ferent ways 15 identical stuffed animals can be given to 7-49 six children so that each child receives at least one but no more than three stuffed animals. 16. Use generating functions to find the number of ways to choose a dozen bagels from three varieties--egg, salty, and plain-if at least two bagels of each kind but no more than three salty bagels are chosen. 17. In how many ways can 25 identical donuts be distributed to four police officers so that each officer gets at least three but no more than seven donuts? 18. Use generating functions to find the number of ways to select 14 balls from a jar containing 100 red balls, 100 blue balls, and 100 green balls so that no fewer than 3 and no more than 10 blue balls are selected. Assume that the order in which the balls are drawn does not matter. 19. What is the generating function for the sequence {cd, where Ck is the number of ways to make change for k dollars using $1 bills, $2 bills, $5 bills, and $10 bills? 20. What is the generating function for the sequence {cd, where Ck represents the number of ways to make change for k pesos using bills worth 10 pesos, 20 pesos, 50 pesos, and 100 pesos? 21. Give a combinatorial interpretation of the coefficient of X4 in the expansion (I + x + X2 + X3 + ... )3. Use this interpretation to find this number. 22. Give a combinatorial interpretation of the coefficient of x6 in the expansion (1 +x +x2 +x3 + .. ·t. Use this interpretation to find this number. 23. a) What is the generating function for {ad, where ak is the number of solutions of XI + X2 + X3 = k when X I, X2, and X3 are integers with XI ::: 2, 0 ::: X2 ::: 3, and 2 ::: X3 ::: 5? b) Use your answer to part (a) to find a6. 24. a) What is the generating function for {ad, where ak is the number of solutions of XI + X2 + X3 + X4 = k when XI, X2, X3, and X4 are integers with XI ::: 3, I ::: X2 ::: 5, 0 ::: X3 ::: 4, and X4 ::: I? b) Use your answer to part (a) to find a7. 25. Explain how generating functions can be used to find the number of ways in which postage of r cents can be pasted on an envelope using 3-cent, 4-cent, and 20-cent stamps. a) Assume that the order the stamps are pasted on does not matter. b) Assume that the stamps are pasted in a row and the order in which they are pasted on matters. c) Use your answer to part (a) to determine the number of ways 46 cents of postage can be pasted on an en­ velope using 3-cent, 4-cent, and 20-cent stamps when the order the stamps are pasted on does not matter. (Use of a computer algebra program is advised.) d) Use your answer to part (b) to determine the number of ways 46 cents of postage can be pasted in a row on an envelope using 3-cent, 4-cent, and 20-cent stamps when the order in which the stamps are pasted on mat­ ters. (Use of a computer algebra program is advised.) 26. a) Show that 1/(1 -X -x2 -x3 -X4 -x5 -X6) is the generating function for the number of ways that the 7.4 Generating Functions 497 sum n can be obtained when a die is rolled repeatedly and the order of the rolls matters. b) Use part (a) to find the number of ways to roll a total of 8 when a die is rolled repeatedly, and the order of the rolls matters. (Use of a computer algebra package is advised.) 27. Use generating functions (and a computer algebra pack­ age, if available) to find the number of ways to make change for $1 using a) dimes and quarters. b) nickels, dimes, and quarters. c) pennies, dimes, and quarters. d) pennies, nickels, dimes, and quarters. 28. Use generating functions (and a computer algebra pack­ age, if available) to find the number of ways to make change for $1 using pennies, nickels, dimes, and quarters with a) no more than 10 pennies. b) no more than 10 pennies and no more than 10 nickels. *c) no more than 10 coins. 29. Use generating functions to find the number of ways to make change for $100 using a) $10, $20, and $50 bills. b) $5, $10, $20, and $50 bills. c) $5, $10, $20, and $50 bills if at least one bill of each denomination is used. d) $5, $10, and $20 bills if at least one and no more than four of each denomination is used. 30. If G(x) is the generating function for the sequence {ak}, what is the generating function for each of these se­ quences? a) 2ao, 2al, 2a2, 2a3, .. . b) 0, ao, aI, a2, a3, ... (assuming that terms follow the pattern of all but the first term) c) 0, 0, 0, 0, a2, a3, ... (assuming that terms follow the pattern of all but the first four terms) d) a2, a3, a4, ... e) aI, 2a2, 3a3, 4a4, ... [Hint: Calculus required here.] 1) a5, 2aoal, ai + 2aOa2, 2aoa3 + 2ala2, 2aOa4 + 2ala3 + ai,··· 31. If G(x) is the generating function for the sequence {ad, what is the generating function for each of these se­ quences? a) 0,0,0, a3, a4, a5, ... (assuming that terms follow the pattern of all but the first three terms) b) ao, 0, aI, 0, a2, 0, ... c) 0, 0, 0, 0, ao, a" a2, ... (assuming that terms follow the pattern of all but the first four terms) d) ao, 2a" 4a2, 8a3, 16a4, .. . e) 0, ao, a1/2, a2/3, a3/4, ... [Hint: Calculus required here.] 1) ao, ao + aI, ao + al + a2, ao + al + a2 + a3,··· 32. Use generating functions to solve the recurrence relation ak = 7ak-1 with the initial condition ao = 5. 498 7 / Advanced Counting Techniques 33. Use generating functions to solve the recurrence relation ak = 3ak-1 + 2 with the initial condition ao = 1. 34. Use generating functions to solve the recurrence relation ak = 3ak-1 + 4k-1 with the initial condition ao = 1. 35. Use generating functions to solve the recurrence relation ak = 5ak-1 -6ak-2 with initial conditions ao = 6 and al = 30. 36. Use generating functions to solve the recurrence relation ak = ak-I + 2ak-2 + 2k with initial conditions ao = 4 and al = 12. 37. Use generating functions to solve the recurrence relation ak = 4ak-1 -4ak-2 + k2 with initial conditions ao = 2 andal = 5. 38. Use generating functions to solve the recurrence rela­ tion ak = 2ak_1 + 3ak-2 + 4k + 6 with initial conditions ao = 20, al = 60. 39. Use generating functions to find an explicit formula for the Fibonacci numbers. *40. a) Show that if n is a positive integer, then (-1/2) = (�n). n (-4)n b) Use the extended Binomial Theorem and part (a) to show that the coefficient of xn in the expansion of (1 -4X)-1/2 is e) for all nonnegative integers n. *41. (Calculus required) Let {Cn} be the sequence of Catalan numbers, that is, the solution to the recurrence relation Cn = L:�:loCkCn-k-1 with Co = CI = 1 (see Example 8 in Section 7.1). a) Show that if G(x) is the generating function for the sequence of Catalan numbers, thenxG(x)2 -G(x) + 1 = o. Conclude (using the initial conditions) that G(x) = (1 -.Jl -4x)/(2x). b) Use Exercise 40 to conclude that so that Cn = _1 (2n). n+l n 42. Use generating functions to prove Pascal's identity: C(n, r) = C(n -1, r) + C(n -1, r -1) when n and r are positive integers with r < n. [Hint: Use the identity (1 +x)n = (1 +x)n-I +x(1 +xy-I.] 43. Use generating functions to prove Vandermonde's iden­ tity: C(rn + n, r) = L:�=o C(rn, r -k)C(n, k), when­ ever rn, n, and r are nonnegative integers with r not exceeding either rn or n. [Hint: Look at the coefficient ofxr in both sides of (1 + x)m+n = (1 + x)m(1 + x)n.] 44. This exercise shows how to use generating functions to derive a formula for the sum of the first n squares. 7-50 a) Show that (x2 + x)/(1 -X)4 is the generating function for the sequence {an}, where an = 12 + 22 + ... + n2• b) Use part (a) to find an explicit formula for the sum 12 +22 + ... +n2. The exponential generatin g function for the sequence {an} is the series 00 '""" an n �-x. n=O n! For example, the exponential generating function for the se­ quence 1, 1, 1, ... is the function L:::"= 0 xn / n! = ee. (You will find this particular series useful in these exercises.) Note that ee is the (ordinary) generating function for the sequence 1,1, 1/2!, 1/3!, 1/4!, .... 45. Find a closed form for the exponential generating function for the sequence {an}, where a) an = 2. b) an = (_1)n. e) an = 3n. d) an = n + 1. e) an = 1/(n + 1). 46. Find a closed form for the exponential generating func­ tion for the sequence {an}, where a) an = (_2)n. b) an = -1. e) an = n. d) an = n(n -1). e) an = 1/((n + 1)(n + 2». 47. Find the sequence with each of these functions as its ex­ ponential generating function. a) f(x) = e-x b) f(x) = 3x2x e) f(x) = e3x -3e2x d) f(x) = (1 -x) + e-2x e) f(x) = e-2x -(1/(1 -x» t) f(x) = e-3x -(1 + x) + (I/O -2x» g) f(x) = ee' 48. Find the sequence with each ofthese functions as its ex­ ponential generating function. a) f(x) = e3x b) f(x) = 2e-3x+1 e) f(x) = e4x + e-4x d) f(x) = (1 + 2x) + e3x e) f(x) = ee -(1/(1 + x» t) f(x) =xex g) f(x)=ex3 49. A coding system encodes messages using strings of octal (base 8) digits. A codeword is considered valid if and only ifit contains an even number of7s. a) Find a linear nonhomogeneous recurrence relation for the number of valid codewords oflength n. What are the initial conditions? b) Solve this recurrence relation using Theorem 6 in Sec­ tion 7.2. e) Solve this recurrence relation using generating functions. *50. A coding system encodes messages using strings of base 4 digits (that is, digits from the set {O, 1, 2, 3 n. A code­ word is valid if and only if it contains an even number of Os and an even number of 1 s. Let an equal the number of valid codewords of length n. Furthermore, let bn, en, and dn equal the number of strings of base 4 digits of length n with an even number of Os and an odd number of Is, with an odd number of Os and an even number of 7-51 I s, and with an odd number of Os and an odd number of Is, respectively. a) Show that dn = 4n -an -bn -Cn. Use this to show thatan+1 = 2an + bn + cn, bn+1 = bn -Cn + 4n, and Cn+1 = Cn -bn + 4n. b) Whatareal,h,cl, anddl? c) Use parts (a) and (b) to find a3, b3, C3, and d3. d) Use the recurrence relations in part (a), together with the initial conditions in part (b), to set up three equa­ tions relating the generating functions A(x), B (x), and C(x) for the sequences {an}, {bn}, and {en}, respec­ tively. e) Solve the system of equations from part (d) to get ex­ plicit formulae for A(x), B(x), and C(x) and use these to get explicit formulae for an, bn, Cn, and dn. Generating functions are useful in studying the number of different types of partitions of an integer n. A partition of a positive integer is a way to write this integer as the sum of positive integers where repetition is allowed and the order of the integers in the sum does not matter. For example, the partitions of 5 (with no restrictions) are I + I + I + I + I, I + I + I + 2, I + I + 3, I + 2 + 2, I + 4, 2 + 3, and 5. Ex­ ercises 51-56 illustrate some of these uses. 51. Show that the coefficient p(n) of xn in the formal power series expansion of 1/«(1 -x)(1 -x2)(1 -x3) ••. ) equals the number of partitions of n. 52. Show that the coefficient po(n) of xn in the formal power series expansion of 1/« 1 -x)(1 -x3)(1 -x5) ..• ) equals the number of partitions of n into odd integers, that is, the number of ways to write n as the sum of odd pos­ itive integers, where the order does not matter and repe­ titions are allowed. 53. Show that the coefficient Pd(n) of xn in the formal power seriesexpansionof(1 +x)(1 +x2)(1 +x3) ••. equals the number of partitions of n into distinct parts, that is, the number of ways to write n as the sum of positive inte­ gers, where the order does not matter but no repetitions are allowed. 54. Find po(n), the number of partitions of n into odd parts with repetitions allowed, and Pd(n), the number of par­ titions of n into distinct parts, for I :::: n :::: 8, by writing each partition of each type for each integer. 55. Show that if n is a positive integer, then the number of partitions of n into distinct parts equals the number of 7.S Inclusi on-Exclusion Introduction 7.5 Inclusion-Exclusion 499 partitions of n into odd parts with repetitions allowed; that is, po(n) = pd(n). [Hint: Show that the generating functions for po(n) and Pd(n) are equal.] **56. (Requires calculus) Use the generating function of pen) to show that pen) :::: eC..jii for some constant C. [Hardy and Ramanujan showed that pen) rv e7r..j2i3..jii /(4.J3n), which means that the ratio of p(n) and the right-hand side approaches I as n approaches infinity.] � Suppose that X is a random variable on a sample space S such that X (s) is a nonnegative integer for all s E S. The proba­ bility generatin g function for X is 00 Gx(x) = L p(X(s) = k)xk• k=O 57. (Requires calculus) Show that if Gx is the probability generating function for a random variable X such that X (s) is a nonnegative integer for all s E S, then a) Gx(l) = 1. b) E(X) = Gf(1). c) VeX) = G;(I) + Gf(1) - Gf(1)2. 58. Let X be the random variable whose value is n if the first success occurs on the nth trial when independent Bernoulli trials are performed, each with probability of success p. a) Find a closed formula for the probability generating function G x . b) Find the expected value and the variance of X using Exercise 57 and the closed form for the probability generating function found in part (a). 59. Let m be a positive integer. Let Xm be the random vari­ able whose value is n if the mth success occurs on the (n + m)th trial when independent Bernoulli trials are per­ formed, each with probability of success p. a) Using Exercise 28 in the Supplementary Exercises of Chapter 6, show that the probability generating func­ tion G Xm is given by G Xm(x) = pm /(1 -qx)m, where q = I -p. b) Find the expected value and the variance of X musing Exercise 57 and the closed form for the probability generating function in part (a), 60. Show that if X and Y are independent random variables on a sample space S such that X (s) and Y (s) are nonnegative integers for all s E S, then Gx+y(x) = Gx(x)Gy(x). A discrete mathema tics class contains 30 women and 50 sophomor es. How many students in the class are either women or sophomores? This question cannot be answered unless more information is provided. Adding the number of women in the class and the number of sophomores probably does not give the correct answer, because women sophomores are counted twice. This observation shows that the number of students in the class that are either sophomores or women 500 7 / Advanced Counting Techniques 7-52 is the sum of the number of women and the number of sophomores in the class minus the number of women sophomor es. A technique for solving such counting problems was introduced in Section 5.1. In this section we will generalize the ideas introduced in that section to solve prob­ lems that require us to count the number of elements in the union of more than two sets. The Principle of Inclusi on-Ex clusion How many elements are in the union of two finite sets? In Section 2.2 we showed that the number of elements in the union of the two sets A and B is the sum of the numbers of elements in the sets minus the number of elements in their intersection. That is, IA U BI = IAI + IBI - IA n BI. As we showed in Section 5.1, the formula for the number of elements in the union of two sets is useful in counting problems. Examples 1-3 provide additional illustrations of the usefulness of this formula. EXAMPLE 1 In a discrete mathematics class every student is a major in computer science or mathema tics, or both. The number of students having computer science as a major (possibly along with mathema tics) is 25; the number of students having mathemati cs as a major (possibly along with computer science) is 13; and the number of students majoring in both computer science and mathemati cs is 8. How many students are in this class? Solution: Let A be the set of students in the class majoring in computer science and B be the set of students in the class majoring in mathema tics. Then A n B is the set of students in the class who are joint mathema tics and computer science majors. Because every student in the class is majoring in either computer science or mathema tics (or both), it follows that the number of students in the class is I A UBI. Therefore, IA U BI = IAI + IBI-IA n BI = 25 + 13 - 8 = 30. Therefore, there are 30 students in the class. This computation is illustrated in Figure 1. ... EXAMPLE 2 How many positive integers not exceeding 1000 are divisible by 7 or II? Solution: Let A be the set of positive integers not exceeding 1000 that are divisible by 7, and let B be the set of positive integers not exceeding 1000 that are divisible by 11. Then A U B is the set of integers not exceeding 1000 that are divisible by either 7 or 11, and A n B is the set of integers not exceeding 1000 that are divisible by both 7 and 11. From Example 2 of Section 3.4, we know that among the positive integers not exceeding 1000 there are LI000/7j integers divisible by 7 and L 1 000/1 1 j divisible by 11. Because 7 and 11 are relatively prime, the integers divisible by both 7 and 11 are those divisible by 7· 11. Consequently, there are LI000/(11 ·7)j positive integers not exceeding 1000 that are divisible by both 7 and 11. It follows that there are IA U BI = IAI + IBI - IA n BI = l10700 J + II ��O J -l ; ��� J = 142 + 90 -12 = 220 7-53 IBI= 13 FIGURE 1 The Set of Students in a Discrete Mathematics Class. 7.5 Inclusion-Exclusion 501 IA uBI = IAI+IBI-IA n BI = 142+ 90-12 = 220 IA n BI= 12 IBI=90 FIGURE 2 The Set of Positive Integers Not Exceeding 1000 Divisible by Either 7 or 11. positive integers not exceeding 1000 that are divisible by either 7 or 11. This computation is illustrated in Figure 2. ... Example 3 shows how to find the number of elements in a finite universal set that are outside the union of two sets. EXAM PLE 3 Suppose that there are 1807 freshmen at your school. Of these, 453 are taking a course in computer science, 567 are taking a course in mathema tics, and 299 are taking courses in both computer science and mathema tics. How many are not taking a course either in computer science or in mathema tics? Solution: To find the number of freshmen who are not taking a course in either mathematics or computer science, subtract the number that are taking a course in either of these subjects from the total number of freshmen. Let A be the set of all freshmen taking a course in computer science, and let B be the set of all freshmen taking a course in mathema tics. It follows that IAI = 453, IBI = 567, and IA n BI = 299. The number of freshmen taking a course in either computer science or mathema tics is IA U BI = IAI + IBI-IA n BI = 453 + 567 -299 = 721. Consequently, there are 1807 -721 = 1086 freshmen who are not taking a course in computer science or mathema tics. ... We will now begin our development of a formula for the number of elements in the union of a finite number of sets. The formula we will develop is called the principle of inclusion ­ exclusion. For concretene ss, before we consider unions of n sets, where n is any positive integer, we will derive a formula for the number of elements in the union of three sets A, B, and C. To construct this formula, we note that I A I + I B I + I C I counts each element that is in exactly one ofthe three sets once, elements that are in exactly two of the sets twice, and elements in all three sets three times. This is illustrated in the first panel in Figure 3. To remove the overcount of elements in more than one of the sets, we subtract the number of elements in the intersections of all pairs of the three sets. We obtain IAI + IBI + ICi-IAnBI-IA nCi-IB nCI· 502 7 / Advanced Counting Techniques 7-54 (a) Count of elements by (b) Count of elements by IAI+IBI+lcl-IAnBI­ IAncl-iBncl (c) Count of elements by IAI+IBI+lcl IAI+IBI+lcl-IAnBI- IA n cl-IB n cl + IA n B n cl FIGURE 3 Finding a Formula for the Number of Elements in the Union of Three Sets. This expression still counts elements that occur in exactly one of the sets once. An element that occurs in exactly two of the sets is also counted exactly once, because this element will occur in one of the three intersections of sets taken two at a time. However, those elements that occur in all three sets will be counted zero times by this expression, because they occur in all three intersections of sets taken two at a time. This is illustrated in the second panel in Figure 3. To remedy this undercount, we add the number of elements in the intersection of all three sets. This final expression counts each element once, whether it is in one, two, or three of the sets. Thus, IA U B U CI = IAI + IBI + ICI-IA n BI-IA n CI-IB n CI + IA n B n CI. This formula is illustrated in the third panel of Figure 3. Example 4 illustrates how this formula can be used. EXAMPLE 4 A total of 1232 students have taken a course in Spanish, 879 have taken a course in French, and 114 have taken a course in Russian. Further, 103 have taken courses in both Spanish and French, 23 have taken courses in both Spanish and Russian, and 14 have taken courses in both French and Russian. If 2092 students have taken at least one of Spanish, French, and Russian, how many students have taken a course in all three languages? Solution: Let S be the set of students who have taken a course in Spanish, F the set of students who have taken a course in French, and R the set of students who have taken a course in Russian. Then lSI = 1232, IFI = 879, IRI=114, IsnFI = 103, ISnRI =23, IF nRI = 14, and IS U FUR I = 2092. When we insert these quantities into the equation IS U F URl = lSI + IFI + IRI-IS nFl-IS n RI-IF n RI + IS n F n RI 7-55 THEOREM l IsnFnRI=? IsnFI=103 IsnRI=23 ISUFURI=2092 FIGURE 4 The Set of Students Who Have Taken Courses in Spanish, French, and Russian. we obtain 2092 = 1232 + 879 + 114 -103 -23 -14 + 15 n F n RI. 7.5 Inclusion-Exclusion 503 We now solve for 15 n F n R I. We find that 15 n F n R I = 7. Therefore, there are seven students who have taken courses in Spanish, French, and Russian. This is illustrated in Figure 4. ... We will now state and prove the inc1usion-exc1usion principle, which tells us how many elements are in the union of a finite number of finite sets. THE PRINCIPLE OF INCLUSION-EXCLUSION Let AI, A2,"" An be finite sets. Then + L IAi n Aj n Akl-··· + (-l)n+IIAI n A2 n··· n Ani. 1::9<j<k�n Proof: We will prove the formula by showing that an element in the union is counted exactly once by the right-hand side of the equation. Suppose that a is a member of exactly r of the sets AI, Az, ... , An where 1 .:::: r .:::: n. This element is counted C(r, 1) times by � IAi 1.1t is counted C (r, 2) times by � I Ai n A j I. In general, it is counted C (r, m) times by the summation involving m of the sets Ai' Thus, this element is counted exactly C(r, 1) -C(r, 2) + C(r, 3) -... + (_l)r+IC(r, r) 504 7 / Advanced Counting Techniques 7-56 times by the expression on the right-hand side of this equation. Our goal is to evaluate this quantity. By Corollary 2 of Section 5.4, we have C(r, 0) -C(r, 1) + C(r, 2) -... + (-lYC(r, r) = O. Hence, 1 = C(r, 0) = C(r, 1) -C(r, 2) + ... + (_1)r+lC(r, r). Therefore, each element in the union is counted exactly once by the expression on the right-hand side of the equation. This proves the principle of inclusion-exclusion. <l The inclusion-exclusion principle gives a formula for the number of elements in the union of n sets for every positive integer n. There are terms in this formula for the number of elements in the intersection of every nonempty subset of the collection of the n sets. Hence, there are 2n -1 terms in this formula. EXAMPLE S Give a formula for the number of elements in the union of four sets. Ex1ra� Examples� Solution: The inclusion-exclusion principle shows that IAI U A2 U A3 U A41 = IAII + IA21 + IA31 + IA41 -IAI n A21 -IAI n A31 -IAI n A41 -IA2 n A31 -IA2 n A41 -IA3 n A41 + IAI n A2 n A31 + IAI n A2 n A41 + IAI n A3 n A41 + IA2 n A3 n A41 -IAI n A2 n A3 n A41. Note that this formula contains 15 different terms, one for each nonempty subset of {AI, A2, A3, A4}. .... Exercises 1. How many elements are in A I U A2 if there are 12 ele­ ments in AI, 18 elements in A2, and a) Al n A2 = 0? b) IAI n A21 = I? c) IAI n A21 = 6? d) Al S; A2? 2. There are 345 students at a college who have taken a course in calculus, 212 who have taken a course in dis­ crete mathematics, and 188 who have taken courses in both calculus and discrete mathematics. How many stu­ dents have taken a course in either calculus or discrete mathematics? 3. A survey of households in the United States reveals that 96% have at least one television set, 98% have telephone service, and 95% have telephone service and at least one television set. What percentage of households in the United States have neither telephone service nor a televi­ sion set? 4. A marketing report concerning personal computers states that 650,000 owners will buy a modem for their machines next year and 1,250,000 will buy at least one software package. If the report states that 1,450,000 owners will buy either a modem or at least one software package, how many will buy both a modem and at least one software package? 5. Find the number of elements in A I U A2 U A3 ifthere are 100 elements in each set and if a) the sets are pairwise disjoint. b) there are 50 common elements in each pair of sets and no elements in all three sets. c) there are 50 common elements in each pair of sets and 25 elements in all three sets. d) the sets are equal. 6. Find the number of elements in A I U A2 U A3 if there are 100 elements in AI, 1000 in A2, and 10,000 in A3 if a) Al S; A2 and A2 S; A3. b) the sets are pairwise disjoint. c) there are two elements common to each pair of sets and one element in all three sets. 7. There are 2504 computer science students at a school. Of these, 1876 have taken a course in Pascal, 999 have taken a course in Fortran, and 345 have taken a course in C. 7-57 Further, 876 have taken courses in both Pascal and For­ tran, 231 have taken courses in both Fortran and C, and 290 have taken courses in both Pascal and C. If 189 of these students have taken courses in Fortran, Pascal, and C, how many of these 2504 students have not taken a course in any of these three programming languages? 8. In a survey of270 college students, it is found that 64 like brussels sprouts, 94 like broccoli, 58 like cauliflower, 26 like both brussels sprouts and broccoli, 28 like both brus­ sels sprouts and cauliflower, 22 like both broccoli and cauliflower, and 14 like all three vegetables. How many of the 270 students do not like any of these vegetables? 9. How many students are enrolled in a course either in cal­ culus, discrete mathematics, data structures, or program­ ming languages at a school if there are 507, 292, 312, and 344 students in these courses, respectively; 14 in both calculus and data structures; 213 in both calculus and programming languages; 211 in both discrete mathemat­ ics and data structures; 43 in both discrete mathematics and programming languages; and no student may take calculus and discrete mathematics, or data structures and programming languages, concurrently? 10. Find the number of positive integers not exceeding 100 that are not divisible by 5 or by 7. 11. Find the number of positive integers not exceeding 100 that are either odd or the square of an integer. 12. Find the number of positive integers not exceeding 1000 that are either the square or the cube of an integer. 13. How many bit strings of length eight do not contain six consecutive Os? *14. How many permutations of the 26 letters of the English alphabet do not contain any ofthe strings fish, rat, or bird? 15. How many permutations ofthe 10 digits either begin with the 3 digits 987, contain the digits 45 in the fifth and sixth positions, or end with the 3 digits 123? 16. How many elements are in the union of four sets if each of the sets has 100 elements, each pair of the sets shares 50 elements, each three ofthe sets share 25 elements, and there are 5 elements in all four sets? 17. How many elements are in the union of four sets if the sets have 50, 60, 70, and 80 elements, respectively, each 7.6 Applications of Inclusion-Exclusion 505 pair of the sets has 5 elements in common, each triple of the sets has 1 common element, and no element is in all four sets? 18. How many terms are there in the formula for the number of elements in the union of 10 sets given by the principle of inclusion-exclusion? 19. Write out the explicit formula given by the principle of inclusion-exclusion for the number of elements in the union of five sets. 20. How many elements are in the union of five sets if the sets contain 10,000 elements each, each pair of sets has 1000 common elements, each triple of sets has 100 com­ mon elements, every four of the sets have 10 common elements, and there is 1 element in all five sets? 21. Write out the explicit formula given by the principle of inclusion-exclusion for the number of elements in the union of six sets when it is known that no three ofthese sets have a common intersection. *22. Prove the principle of inclusion-exclusion using mathe­ matical induction. 23. Let E 1, E2, and E3 be three events from a sample space S. Find a formula for the probability of E\ U E2 U E3. 24. Find the probability that when a fair coin is flipped five times tails comes up exactly three times, the first and last flips come up tails, or the second and fourth flips come up heads. 25. Find the probability that when four numbers from 1 to 100, inclusive, are picked at random with no repetitions allowed, either all are odd, all are divisible by 3, or all are divisible by 5. 26. Find a formula for the probability of the union of four events in a sample space if no three of them can occur at the same time. 27. Find a formula for the probability of the union of five events in a sample space if no four of them can occur at the same time. 28. Find a formula for the probability of the union of n events in a sample space when no two ofthese events can occur at the same time. 29. Find a formula for the probability of the union ofn events in a sample space. 7.6 Applica tions of Inclusi on-Exclusion Introduction Many counting problems can be solved using the principle of inclusion-exclusion. For instance, we can use this principle to find the number of primes less than a positive integer. Many problems can be solved by counting the number of onto functions from one finite set to another. The inclusion-exclusion principle can be used to find the number of such functions. The famous hatcheck problem can be solved using the principle of inclusion-exclusion. This problem asks for the probability that no person is given the correct hat back by a hatcheck person who gives the hats back randomly. 506 7 / Advanced Counting Techniques An Alternative Form of Inclusion -Exclusion 7-58 There is an alternative form of the principle of inc1usion-exc1usion that is useful in counting problems. In particular, this form can be used to solve problems that ask for the number of elements in a set that have none of n properties PI, P2, ••• , Pn• Let Ai be the subset containing the elements that have property Pi. The number of elements with all the properties Pil, Pi2, ... 'Pik will be denoted by N (Pil Pi2 ... PiJ. Writing these quantities in terms of sets, we have If the number of elements with none of the properties PI, P2, ••• ,Pn is denoted by N (P[ P; ... P�) and the number of elements in the set is denoted by N, it follows that N(P{P; ... P�) = N -IAI U A2 U··· U Ani. From the inc1usion-exc1usion principle, we see that L N(PiPjPd + ... + (-ltN(PIP2··· Pn). I-::;i<j<k-::;n Example 1 shows how the principle of inc1usion-exc1usion can be used to determine the number of solutions in integers of an equation with constraints. EXAMPLE 1 How many solutions does have, where XI, X2, and X3 are nonnegative integers with XI ::::: 3, X2 ::::: 4, and X3 ::::: 6? Solution: To apply the principle of inc1usion-exc1usion, let a solution have property PI if XI > 3, property P2 if X2 > 4, and property P3 if X3 > 6. The number of solutions satisfying the inequalities XI ::::: 3, X2 ::::: 4, and X3 ::::: 6 is N(P[P;PD = N -N(PI) -N(P2) -N(P3) + N(PIP2) + N(PIP3) + N(P2P3) -N(PIP2P3). Using the same techniques as in Example 5 of Section 5.5, it follows that • N = total number of solutions = C (3 + 11 -1, 11) = 78, • N(PI) = (number of solutions with XI ::: 4) = C(3 + 7 -1,7) = C(9, 7) = 36, • N(P2) = (number of solutions with X2 ::: 5) = C(3 + 6 - 1,6) = C(8, 6) = 28, • N(P3) = (number of solutions withx3 ::: 7) = C(3 + 4 - 1,4) = C(6, 4) = 15, • N (PI P2) = (number of solutions with XI ::: 4 and X2 ::: 5) = C (3 + 2 -1, 2) = C(4, 2) = 6, • N (PI P3) = (number of solutions with XI ::: 4 and X3 ::: 7) = C (3 + 0 - 1, 0) = 1, • N(P2P3) = (number of solutions with X2 ::: 5 and X3 ::: 7) = 0, • N(PI P2P3) = (number of solutions with XI ::: 4, X2 ::: 5, and X3 ::: 7) = O. 7-59 Lin!(S� 7.6 Applications of Inclusion-Exclusion 507 Inserting these quantities into the formula for N (P{ P� PD shows that the number of solutions with x] .:::: 3, X2 .:::: 4, and X3 .:::: 6 equals N(P{ P�PD = 78 -36 -28 -15 + 6 + 1 + 0 -0 = 6. The Sieve of Eratost henes The principle of inc1usion-exc1usion can be used to find the number of primes not exceeding a specified positive integer. Recall that a composite integer is divisible by a prime not exceeding its square root. So, to find the number of primes not exceeding 100, first note that composite integers not exceeding 100 must have a prime factor not exceeding 10. Because the only primes less than 10 are 2, 3, 5, and 7, the primes not exceeding 100 are these four primes and those positive integers greater than 1 and not exceeding 100 that are divisible by none of 2, 3, 5, or 7. To apply the principle of inc1usion-exc1usion, let p] be the property that an integer is divisible by 2, let P2 be the property that an integer is divisible by 3, let P3 be the property that an integer is divisible by 5, and let P4 be the property that an integer is divisible by 7. Thus, the number of primes not exceeding 100 is Because there are 99 positive integers greater than 1 and not exceeding 100, the principle of inclusion-exc1usion shows that N(P{ P�P;Pd) = 99 -N(P]) -N(P2) -N(P3) -N(P4) +N(P]P2) + N(P]P3) + N(P]P4) + N(P2P3) + N(P2P4) + N(P3P4) -N(P]P2P3) -N(P]P2P4) -N(P]P3P4) -N(P2P3P4) +N(P]P2P3P4). The number of integers not exceeding 100 (and greater than 1) that are divisible by all the primes in a subset of {2, 3, 5, 7} is L100j N J, where N is the product of the primes in this subset. (This follows because any two of these primes have no common factor.) Consequently, l100J lIOOJ l100J l�J l�J l�J + 2·3 + 2·5 + 2·7 + 3·5 + 3·7 + 5·7 l100 J l100 J l100 J lIOO J l 100 J -2·3·5 -2·3·7 -2·5·7 -3·5·7 + 2·3·5·7 = 99 - 50 - 33 -20 -14 + 16 + 10 + 7 + 6 + 4 + 2 - 3 -2 -1 -0 + 0 =21. Hence, there are 4 + 21 = 25 primes not exceeding 100. The Sieve of Eratosthenes is used to find all primes not exceeding a specified positive integer. For instance, the following procedure is used to find the primes not exceeding 100. First the integers that are divisible by 2, other than 2, are deleted. Because 3 is the first integer greater 508 7 / Advanced Counting Techniques 7-60 liobs TABLE 1 The Sieve of Eratosthenes. Integers divisible by 2 other than 2 receive an underline. I 2 3 1 5 Q 7 � 9 11 If 13 14 15 12 17 � 19 21 22 23 24 25 26 27 28 29 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 47 48 49 51 52 53 54 55 56 57 58 59 61 62 63 64 65 66 67 68 69 71 72 73 74 75 76 77 78 79 81 82 83 84 85 86 87 88 89 91 92 93 94 95 96 97 98 99 100 Integers divisible by 5 other than 5 receive an underline. I 2 3 4 5 Q 7 � � II If 13 14 � 12 17 � 19 � 22 23 24 25 26 27 28 29 - 31 32 33 34 35 36 37 38 39 - 41 42 43 44 45 46 47 48 49 lQ 71 72 73 74 75 76 77 78 79 80 Integers divisible by 3 other than 3 receive an underline. 1 2 315 Q 7 8 � II If 13 14 � 12 17 � 19 � n D M � M n 28 � - 31 32 33 34 35 36 37 38 39 - 41 42 43 44 45 46 47 48 49 .2! 52 53 54 55 56 57 58 59 61 62 63 64 65 66 67 68 69 71 72 73 74 75 76 77 78 79 - - � 82 83 84 85 86 87 88 89 91 92 93 94 95 96 97 98 99 100 Integers divisible by 7 other than 7 receive an underline; integers in color are prime. I 2 3 1 5 Q 7 � � II If 13 14 � 16 17 � 19 21 22 23 24 25 26 27 28 29 - - - 31 32 33 34 35 36 37 38 39 - 41 42 43 44 45 46 47 48 49 .2! 52 53 54 55 56 57 58 59 - - 61 62 63 64 65 66 67 68 69 71 72 73 74 75 76 77 78 79 - - � 82 83 84 85 86 87 88 89 -- 2! 92 93 94 95 96 97 98 99 10 than 2 that is left, all those integers divisible by 3, other than 3, are deleted. Because 5 is the next integer left after 3, those integers divisible by 5, other than 5, are deleted. The next integer left is 7, so those integers divisible by 7, other than 7, are deleted. Because all composite integers not exceeding 100 are divisible by 2,3,5, or 7, all remaining integers except 1 are prime. In Table 1, the panels display those integers deleted at each stage, where each integer divisible by 2, other than 2, is underlined in the first panel, each integer divisible by 3, other than 3, is underlined in the second panel, each integer divisible by 5, other than 5, is underlined in the third panel, ERATOSTHENES (276-194 B.C.E.) It is known that Eratosthenes was born in Cyrene, a Greek colony west of Egypt, and spent time studying at Plato's Academy in Athens. We also know that King Ptolemy II invited Eratosthenes to Alexandria to tutor his son and that later Eratosthenes became chief librarian at the famous library at Alexandria, a central repository of ancient wisdom. Eratosthenes was an extremely versatile scholar, writing on mathematics, geography, astronomy, history, philosoph y, and literary criticism. Besides his work in mathema tics, he is most noted for his chronology of ancient history and for his famous measurement of the size of the earth. 7-61 7.6 Applications ofInclusion-Exclusion 509 and each integer divisible by 7, other than 7, is underlined in the fourth panel. The integers not underlined are the primes not exceeding 100. The Number of Onto Functions The principle of inclusion-exclusion can also be used to determine the number of onto functions from a set with m elements to a set with n elements. First consider Example 2. EXAMPLE 2 How many onto functions are there from a set with six elements to a set with three elements? THEOREM l Solution: Suppose that the elements in the codomain are hI, h2, and h3. Let PI, P2, and P3 be the properties that bl, h2' and h3 are not in the range of the function, respectivel y. Note that a function is onto if and only if it has none of the properties PI, P2, or P3. By the inclusion­ exclusion principle it follows that the number of onto functions from a set with six elements to a set with three elements is N(P{ P;PD = N -[N(PI) + N(P2) + N(P3)] + [N(PI P2) + N(PI P3) + N(P2P3)] -N(PI P2P3), where N is the total number of functions from a set with six elements to one with three elements. We will evaluate each of the terms on the right-hand side of this equation. From Example 6 of Section 5.1, it follows that N = 36. Note that N(Pi) is the number of functions that do not have hi in their range. Hence, there are two choices for the value of the function at each element of the domain. Therefore, N(Pi) = 26. Furthermore, there are C(3, 1) terms of this kind. Note that N(PiPj) is the number of functions that do not have bi and bj in their range. Hence, there is only one choice for the value of the function at each element of the domain. Therefore, N(PiPj) = 16 = 1. Furthermore, there are C(3, 2) terms of this kind. Also, note that N(PI P2P3) = 0, because this term is the number of functions that have none of bl, h2, and h3 in their range. Clearly, there are no such functions. Therefore, the number of onto functions from a set with six elements to one with three elements is 36 -C(3, 1)26 + C(3, 2)16 = 729 -192 + 3 = 540. The general result that tells us how many onto functions there are from a set with m elements to one with n elements will now be stated. The proof of this result is left as an exercise for the reader. Let m and n be positive integers with m :::: n. Then, there are nm -C(n, 1)(n _1)m + C(n, 2)(n -2)m -... + (-I)n-IC(n, n -1). 1m onto functions from a set with m elements to a set with n elements . An onto function from a set with m elements to a set with n elements corresponds to a way to distribute the m elements in the domain to n indistinguishable boxes so that no box is empty, and then to associate each of the n elements of the codomain to a box. This means that the number of onto functions from a set with m elements to a set with n elements is the number of ways to distribute m distinguishable objects to n indistinguishable boxes so that no box is empty multiplied by the number of permutations of a set with n elements. Consequently, the number of onto functions from a set with m elements to a set with n elements equals n! S (m, n), 510 7/ Advanced Counting Techniques 7-62 where S(m, n) is a Stirling number of the second kind defined in Section 5.5. This means that we can use Theorem 1 to deduce the formula given in Section 5.5 for S(m, n). (See Chapter 6 of [MiR091] for more details about Stirling numbers of the second kind.) One of the many different applications of Theorem 1 will now be described. EXAMPLE 3 How many ways are there to assign five different jobs to four different employees if every employee is assigned at least one job? Solution: Consider the assignment of jobs as a function from the set of five jobs to the set of four employees. An assignment where every employee gets at least one job is the same as an onto function from the set of jobs to the set of employees. Hence, by Theorem 1 it follows that there are 45 -C(4, 1)35 + C(4, 2)25 -C(4, 3)15 = 1024 -972 + 192 -4 = 240 ways to assign the jobs so that each employee is assigned at least one job. Derangements The principle of inclusion--exclusion will be used to count the permutations of n objects that leave no objects in their original positions. Consider Example 4 . EXAMPLE 4 The Hatcheck Problem A new employee checks the hats ofn people at a restaurant, forgetting to put claim check numbers on the hats. When customers return for their hats, the checker gives them back hats chosen at random from the remaining hats. What is the probability that no one receives the correct hat? <III unkS� EXAMPLE S THEOREM 2 Remark: The answer is the number of ways the hats can be arranged so that there is no hat in its original position divided by n!, the number of permutations of n hats. We will return to this example after we find the number of permutations of n objects that leave no objects in their original position. A derangement is a permutation of objects that leaves no object in its original position. To solve the problem posed in Example 4 we will need to determine the number of derangements of a set of n objects. The permutation 21453 is a derangement of 12345 because no number is left in its original position. However, 21543 is not a derangement of 12345, because this permutation leaves 4 fixed. <III Let Dn denote the number of derangements of n objects. For instance, D3 = 2, because the derangements of 123 are 231 and 312. We will evaluate Dn, for all positive integers n, using the principle of inclusion--exclusion. The number of derangements of a set with n elements is [ 1 1 1 n1] D = n! 1 -- + - - - + ... + (-1) - n 1! 2! 3! n! . 7-63 linkS� 7.6 Applications of Inclusion-Exclusion 511 Proof: Let a permutation have property Pi if it fixes element i. The number of derangements is the number of permutations having none of the properties Pi for i = I, 2, ... , n. This means that Dn = N(P{ p� ... P�). Using the principle of inclusion-exclusion, it follows that Dn =N-LN(Pi)+ LN(PiPj)-L N(PiPjPk)+···+(-I)nN(P1P2···Pn), i i<j i<j<k where N is the number of permutations of n elements. This equation states that the number of permutations that fix no elements equals the total number of permutations, less the number that fix at least one element, plus the number that fix at least two elements, less the number that fix at least three elements, and so on. All the quantities that occur on the right-hand side of this equation will now be found. First, note that N = n!, because N is simply the total number of permutations of n elements. Also, N(Pi) = (n -I)!. This follows from the product rule, because N(Pi) is the number of permutations that fix element i, so the i th position of the permutation is determined, but each of the remaining positions can be filled arbitrarily. Similarly, because this is the number of permutations that fix elements i and j, but where the other n -2 elements can be arranged arbitrarily. In general, note that because this is the number of permutations that fix elements ii, i2, ... , im, but where the other n -m elements can be arranged arbitrarily. Because there are C (n, m) ways to choose m ele­ ments from n, it follows that L N(Pi) = C(n, I)(n - I)!, l::;i::;n L N(PiPj) = C(n, 2)(n -2)!, l::;i<j::;n and in general, L N(Pil Pi2 ... Pim) = C(n, m)(n -m)!. 1:91 <i2<···<im-':::;'n HISTORICAL NOTE In rencontres (matches), an old French card game, the 52 cards in a deck are laid out in a row. The cards of a second deck are laid out with one card of the second deck on top of each card of the first deck. The score is determined by counting the number of matching cards in the two decks. In 1708 Pierre Raymond de Montmort (1678-1719) posed Ie probleme de rencontres: What is the probability that no matches take place in the game of rencontres? The solution to Montmort's problem is the probability that a randomly selected permutation of 52 objects is a derangement, namely, DS2/52!, which, as we will see, is approximately 1/ e. 512 7/ Advanced Counting Techniques 7-64 Exercises TABLE 2 The Probability of a Derangement. n 2 3 4 5 6 7 Dnln! 0.50000 0.33333 0.37500 0.36667 0.36806 0.36786 Consequently, inserting these quantities into our formula for Dn gives Dn = n! -C(n, l)(n - I)! + C(n, 2)(n -2)! -... + (-l)nC(n, n)(n -n)! n! n! n n! =n!- (n-l)!+ (n-2)!-···+(-1) -o!. l!(n - I)! 2!(n -2)! n! o! Simplifying this expression gives [ 1 1 n1] D =n! 1--+--···+(-1)-. n 1! 2! n! <I It is now simple to find Dn for a given positive integer n. For instance, using Theorem 2, it follows that D3 = 3! [1 -� + � - �] = 6 (1 - 1 + � - �) = 2 I! 2! 3! 2 6 ' as we have previously remarked. The solution of the problem in Example 4 can now be given. Solution: The probability that no one receives the correct hat is Dn In!. By Theorem 2, this probability is Dn l i n 1 -=1- -+- -···+(-1) -. n! 1! 2! n! The values of this probability for 2 ::; n ::; 7 are displayed in Table 2. Using methods from calculus it can be shown that _\ 1 1 n 1 e = 1 - -+ - -... + (-1) -+ ... '" 0.368. 1! 2! n! Because this is an alternating series with terms tending to zero, it follows that as n grows without bound, the probability that no one receives the correct hat converges to e-1 '" 0.368. In fact, this probability can be shown to be within 1/(n + 1)! of e-1• .... 1. Suppose that in a bushel of 100 apples there are 20 that have worms in them and 15 that have bruises. Only those apples with neither worms nor bruises can be sold. If there are 10 bruised apples that have worms in them, how many of the 100 apples can be sold? enough shape, and 30 have allergies. An applicant qual­ ifies if and only if this applicant does not get altitude sickness, is in good shape, and does not have allergies. If there are III applicants who get altitude sickness and are not in good enough shape, 14 who get altitude sickness and have allergies, 18 who are not in good enough shape and have allergies, and 9 who get altitude sickness, are 2. Of 1000 applicants for a mountain-climbing trip in the Himalayas, 450 get altitude sickness, 622 are not in good 7-65 not in good enough shape, and have allergies, how many applicants qualify? 3. How many solutions does the equation Xl + X2 + X3 = 13 have where Xl, X2, and X3 are nonnegative integers less than 6? 4. Find the number of solutions of the equation Xl + X2 + X3 + X4 = 17, where Xi, i = 1, 2, 3, 4, are nonnegative integers such that Xl ::: 3, X2 ::: 4, X3 ::: 5, and X4 ::: 8. 5. Find the number of primes less than 200 using the princi­ ple of inclusion-exclusion. 6. An integer is called squarefree if it is not divisible by the square of a positive integer greater than 1. Find the number of squarefree positive integers less than 100. 7. How many positive integers less than 10,000 are not the second or higher power of an integer? 8. How many onto functions are there from a set with seven elements to one with five elements? 9. How many ways are there to distribute six different toys to three different children such that each child gets at least one toy? 10. In how many ways can eight distinct balls be distributed into three distinct urns if each urn must contain at least one ball? 11. In how many ways can seven different jobs be assigned to four different employees so that each employee is assigned at least one job and the most difficult job is assigned to the best employee? 12. List all the derangements of {I, 2, 3, 4}. 13. How many derangements are there of a set with seven elements? 14. What is the probability that none of 10 people receives the correct hat if a hatcheck person hands their hats back randomly? 15. A machine that inserts letters into envelopes goes haywire and inserts letters randomly into envelopes. What is the probability that in a group of 100 letters a) no letter is put into the correct envelope? b) exactly one letter is put into the correct envelope? c) exactly 98 letters are put into the correct envelopes? d) exactly 99 letters are put into the correct envelopes? e) all letters are put into the correct envelopes? 16. A group of n students is assigned seats for each of two classes in the same classroom. How many ways can these Key Terms and Results TERMS recurrence relation: a formula expressing terms of a se­ quence, except for some initial terms, as a function of one or more previous terms of the sequence initial conditions for a recurrence relation: the values of Key Terms and Results 513 seats be assigned if no student is assigned the same seat for both classes? *17. How many ways can the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 be arranged so that no even digit is in its original position? *18. Use a combinatorial argument to show that the sequence {Dn}, where Dn denotes the number of derangements of n objects, satisfies the recurrence relation for n :::: 2. [Hint: Note thatthere are n - 1 choices for the first element k of a derangement. Consider separately the derangements that start with k that do and do not have 1 in the kth position.] *19. Use Exercise 18 to show that for n :::: 1. 20. Use Exercise 19 to find an explicit formula for Dn. 21. For which positive integers n is Dn, the number of de­ rangements of n objects, even? 22. Suppose that p and q are distinct primes. Use the prin­ ciple of inclusion-exclusion to find ¢(pq), the number of positive integers not exceeding pq that are relatively prime topq. *23. Use the principle of inclusion-exclusion to derive a for­ mula for ¢(n) when the prime factorization of n is *24. Show that if n is a positive integer, then n! = C(n, O)Dn + C(n, I)Dn-1 + ... + C(n, n -I)DI + C(n, n)Do, where Dk is the number of derangements of k objects. 25. How many derangements of {I, 2, 3, 4, 5, 6} begin with the integers 1,2, and 3, in some order? 26. How many derangements of {I, 2, 3,4,5, 6} end with the integers 1,2, and 3, in some order? 27. Prove Theorem 1. the terms of a sequence satisfying the recurrence relation before this relation takes effect linear homogeneous recurrence relation with constant coefficients : a recurrence relation that expresses the terms of a sequence, except initial terms, as a linear combination of previous terms 514 7/ Advanced Counting Techniques characteristic roots of a linear homogeneous recurrence relation with constant coefficients : the roots of the poly­ nomial associated with a linear homogeneous recurrence relation with constant coefficients linear nonhomo geneous recurrence relation with constant coefficients : a recurrence relation that expresses the terms of a sequence, except for initial terms, as a linear combina­ tion of previous terms plus a function that is not identically zero that depends only on the index divide-and-conquer algorithm: an algorithm that solves a problem recursively by splitting it into a fixed number of smaller problems of the same type generatin g function of a sequence : the formal series that has the nth term of the sequence as the coefficient of xn sieve of Eratosthe nes: a procedure for finding the primes less than a specified positive integer derangement: a permutation of objects such that no object is in its original place RESULTS the formula for the number of elements in the union of two finite sets: IA U BI = IAI + IBI-IA n BI Review Questions 1. a) What is a recurrence relation? b) Find a recurrence relation for the amount of money that will be in an account after n years if $1,000,000 is deposited in an account yielding 9% annually. 2. Explain how the Fibonacci numbers are used to solve Fi­ bonacci's problem about rabbits. 3. a) Find a recurrence relation for the number of steps needed to solve the Tower of Hanoi puzzle. b) Show how this recurrence relation can be solved using iteration. 4. a) Explain how to find a recurrence relation for the num­ ber of bit strings of length n not containing two con­ secutive Is. b) Describe another counting problem that has a solution satisfying the same recurrence relation. 5. Define a linear homogeneous recurrence relation of degree k. 6. a) Explain how to solve linear homogeneous recurrence relations of degree 2. b) Solve the recurrence relation an = 13an-l -22an-2 for n � 2 if ao = 3 and al = 15. c) Solve the recurrence relation an = 14an-l -49an-2 for n � 2 if ao = 3 and al = 35. 7-66 the formula for the number of elements in the union ofthree finite sets: IA U B U CI = IAI + IBI + ICI-IA n BI-IA n CI -IB n CI + IA n B n CI the principle of inclusion-exclusion: IAIUA2U···UAnl= L IA;I-L IA;nAjl l:::;:i::::n l-:s.i<}::::n + L IA; n Aj n Akl l�i<j<k�n the number of onto functions from a set with m elements to a set with n elements: nm -C(n, I)(n -I)m + C(n, 2)(n -2)m -... + (_l)n-lC(n, n -1)· 1m the number of derangements of n objects: D =n! 1--+--···+(-1)- [ 1 1 nl] n I! 2! n! 7. a) Explain how to find f(bk) where k is a positive inte­ ger if f (n ) satisfies the divide-and-conquer recurrence relation f(n) = af(n / b) + g(n) whenever b divides the positive integer n. b) Findf(256)iff(n) = 3f(n/4) + 5n/4andf(l) = 7. 8. a) Derive a divide-and-conquer recurrence relation for the number of comparisons used to find a number in a list using a binary search. b) Give a big-O estimate for the number of comparisons used by a binary search from the divide-and-c onquer recurrence relation you gave in (a) using Theorem I in Section 7.3. 9. a) Give a formula for the number of elements in the union ofthree sets. b) Explain why this formula is valid. c) Explain how to use the formula from (a) to find the number of integers not exceeding 1000 that are divis­ ible by 6, 10, or 15. d) Explain how to use the formula from (a) to find the number of solutions in nonnegative integers to the equation Xl + X2 + X3 + X4 = 22 with Xl < 8, X2 < 6, andx3 < 5. 10. a) Give a formula for the number of elements in the union of four sets and explain why it is valid. 7-67 b) Suppose the sets AI, A2, A3, and A4 each contain 25 elements, the intersection of any two ofthese sets con­ tains 5 elements, the intersection of any three of these sets contains 2 elements, and 1 element is in all four of the sets. How many elements are in the union of the four sets? 11. a) State the principle of inclusion-exclusion. b) Outline a proof of this principle. 12. Explain how the principle of inclusion-exclusion can be used to count the number of onto functions from a set with m elements to a set with n elements. 13. a) How can you count the number of ways to assign m jobs to n employees so that each employee is assigned at least one job? Supplementar y Exercises 1. A group of 10 people begin a chain letter, with each per­ son sending the letter to four other people. Each ofthese people sends the letter to four additional people. a) Find a recurrence relation for the number of letters sent at the nth stage of this chain letter, if no person ever receives more than one letter. b) What are the initial conditions for the recurrence re­ lation in part (a)? c) How many letters are sent at the nth stage of the chain letter? 2. A nuclear reactor has created 18 grams of a particular ra­ dioactive isotope. Every hour 1 % of a radioactive isotope decays. a) Set up a recurrence relation for the amount of this isotope left after n hours. b) What are the initial conditions for the recurrence re­ lation in part (a)? c) Solve this recurrence relation. 3. Every hour the U.S. government prints 10,000 more $1 bills, 4000 more $5 bills, 3000 more $10 bills, 2500 more $20 bills, 1000 more $50 bills, and the same number of $100 bills as it did the previous hour. In the initial hour 1000 of each bill were produced. a) Set up a recurrence relation for the amount of money produced in the nth hour. b) What are the initial conditions for the recurrence re­ lation in part (a)? c) Solve the recurrence relation for the amount of money produced in the nth hour. d) Set up a recurrence relation for the total amount of money produced in the first n hours. e) Solve the recurrence relation for the total amount of money produced in the first n hours. 4. Suppose that every hour there are two new bacteria in a colony for each bacterium that was present the previous hour, and that all bacteria 2 hours old die. The colony starts with 100 new bacteria. Supplementary Exercises 515 b) How many ways are there to assign seven jobs to three employees so that each employee is assigned at least one job? 14. Explain how the inclusion-exclusion principle can be used to count the number of primes not exceeding the positive integer n. 15. a) Define a derangement. b) Why is counting the number of ways a hatcheck per­ son can return hats to n people, so that no one receives the correct hat, the same as counting the number of de­ rangements of n objects? c) Explain how to count the number of derangements of n objects. a) Set up a recurrence relation for the number of bacteria present after n hours. b) What is the solution of this recurrence relation? c) When will the colony contain more than 1 million bacteria? 5. Messages are sent over a communications channel using two different signals. One signal requires 2 microseconds for transmittal, and the other signal requires 3 microsec­ onds for transmittal. Each signal of a message is followed immediately by the next signal. a) Find a recurrence relation for the number of different signals that can be sent in n microseconds. b) What are the initial conditions of the recurrence rela­ tion in part (a)? c) How many different messages can be sent in 12 mi­ croseconds? 6. A small post office has only 4-cent stamps, 6-cent stamps, and 10-cent stamps. Find a recurrence relation for the number of ways to form postage of n cents with these stamps if the order that the stamps are used mat­ ters. What are the initial conditions for this recurrence relation? 7. How many ways are there to form these postages using the rules described in Exercise 6? a) 12 cents b) 14 cents c) 18 cents d) 22 cents 8. Find the solutions of the simultaneous system of congru­ ences an = an-I + bn-I bn = an-I -bn-I with ao = 1 and bo = 2. 9. Solve the recurrence relation an = a�_dan-2 if ao = 1 and al = 2. [Hint: Take logarithms of both sides to ob­ tain a recurrence relation for the sequence log an, n = 0, 1,2, .... ] 516 7/ Advanced Counting Techniques *10. Solvetherecurrencerelationan = a�_la�_2 ifao = 2 and al = 2. (See the hint for Exercise 9.) 11. Find the solution of the recurrence relation an = 3an-1 - 3an-2 + an-3 + 1 if ao = 2, al = 4, and a2 = 8. 12. Find the solution ofthe recurrence relation an = 3an-1 - 3an-2 + an-3 if ao = 2, al = 2, and a2 = 4. *13. Suppose that in Example 4 of Section 7.l a pair of rabbits leaves the island after reproducing twice. Find a recur­ rence relation for the number of rabbits on the island in the middle of the nth month. 14. Find the solution to the recurrence relation f(n) = 3f(nI5) + 2n\ when n is divisible by 5, for n = 5k, where k is a positive integer and f(l) = 1. 15. Estimate the size of f in Exercise 14 if f is an increasing function. 16. Find a recurrence relation that describes the number of comparisons used by the following algorithm: Find the largest and second largest elements of a sequence of n numbers recursively by splitting the sequence into two subsequences with an equal number of terms, or where there is one more term in one subsequence than in the other, at each stage. Stop when subsequences with two terms are reached. 17. Estimate the number of comparisons used by the algo- rithm described in Exercise 16. Let {an} be a sequence of real numbers. The forward dif­ ferences of this sequence are defined recursively as fol­ lows: The first forward difference is flan = an+l -an; the (k + l)st forward difference flk+lan is obtained from flkan by flk+lan = flkan+l -flkan. 18. Find flan, where a) an = 3. b) an = 4n + 7. c) an = n2 + n + 1. 19. Let an = 3n3 + n + 2. Find flkan, where k equals a) 2. b) 3. c) 4. *20. Suppose that an = P(n), where P is a polynomial of degree d. Prove that fld+lan = 0 for all nonnegative in­ tegers n. 21. Let {an} and Ibn} be sequences of real numbers. Show that fl(anbn) = an+l(flbn) + bn(flan). 22. Show that if F(x) and G(x) are the generating functions for the sequences {ad and {bk}, respectively, and c and d are real numbers, then (cF + dG)(x) is the generating function for {cak + dbd. 23. (Requires calculus) This exercise shows how generating functions can be used to solve the recurrence relation (n + l)an+1 = an + (lin!) for n ::: 0 with initial condi­ tion ao = 1. a) Let G(x) be the generating function for {an}. Show that G'(x) = G(x) + eX and G(O) = 1. b) Show from part (a) that (e-xG(x»' = 1, and conclude that G(x) = xex + eX. c) Use part (b) to find a closed form for an. 7-68 24. Suppose that 14 students receive an A on the first exam in a discrete mathematics class, and 18 receive an A on the second exam. If 22 students received an A on either the first exam or the second exam, how many students received an A on both exams? 25. There are 323 farms in Monmouth County that have at least one of horses, cows, and sheep. If 224 have horses, 85 have cows, 57 have sheep, and 18 farms have all three types of animals, how many farms have exactly two of these three types of animals? 26. Queries to a database of student records at a college pro­ duced the following data: There are 2175 students at the college, 1675 of these are not freshmen, 1074 students have taken a course in calculus, 444 students have taken a course in discrete mathematics, 607 students are not freshmen and have taken calculus, 350 students have taken calculus and discrete mathematics, 201 students are not freshmen and have taken discrete mathematics, and 143 students are not freshmen and have taken both calculus and discrete mathematics. Can all the responses to the queries be correct? 27. Students in the school of mathematics at a university ma­ jor in one or more of the following four areas: applied mathematics (AM), pure mathematics (PM), operations research (OR), and computer science (CS). How many students are in this school if(includingjoint majors) there are 23 students majoring in AM; 17 students majoring in PM; 44 in OR; 63 in CS; 5 in AM and PM; 8 in AM and CS; 4 in AM and OR; 6 in PM and CS; 5 in PM and OR; 14 in OR and CS; 2 in PM, OR, and CS; 2 in AM, OR, and CS; 1 in PM, AM, and OR; 1 in PM, AM, and CS; and 1 in all four fields. 28. How many terms are needed when the inclusion­ exclusion principle is used to express the number of ele­ ments in the union of seven sets if no more than five of these sets have a common element? 29. How many solutions in positive integers are there to the equation Xl + X2 + X3 = 20 with 2 < Xl < 6, 6 < Xz < 10, and 0 < X3 < 5? 30. How many positive integers less than 1,000,000 are a) divisible by 2, 3, or 5? b) not divisible by 7, 11, or 13? c) divisible by 3 but not by 7? 31. How many positive integers less than 200 are a) second or higher powers of integers? b) either second or higher powers of integers or primes? c) not divisible by the square of an integer greater than I? d) not divisible by the cube of an integer greater than I? e) not divisible by three or more primes? *32. How many ways are there to assign six different jobs to three different employees if the hardest job is assigned 7-69 to the most experienced employee and the easiest job is assigned to the least experienced employee? 33. What is the probability that exactly one person is given back the correct hat by a hatcheck person who gives n people their hats back at random? Computer Projects Write programs with these input and output. 1. Given a positive integer n, list all the moves re­ quired in the Tower of Hanoi puzzle to move n disks from one peg to another according to the rules of the puzzle. 2. Given a positive integer n and an integer k with I :s k :s n, list all the moves used by the Frame-Stewart algorithm (described in the preamble to Exercise 54 of Section 7.1) to move n disks from one peg to another using four pegs according to the rules of the puzzle. 3. Given a positive integer n, list all the bit sequences of length n that do not have a pair of consecutive Os. 4. Given a positive integer n, write out all ways to parenthe­ size the product of n + I variables. j. Given a recurrence relation an = Clan-l + C2an-2, where Cl and C2 are real numbers, initial conditions ao = Co and a 1 = C l, and a positive integer k, find ak using iteration. 6. Given a recurrence relationan = Clan-l + C2an-2 and Computations and Explorations Computations and Explorations 517 34. How many bit strings of length six do not contain four consecutive Is? 35. What is the probability that a bit string oflength six con­ tains at least four Is? initial conditions ao = Co and al = C l, determine the unique solution. 7. Givenarecurrencerelationoftheform/(n) = al(n/b) + c, where a is a real number, b is a positive integer, and C is a real number, and a positive integer k, find I(bk) using iteration. 8. Given the number of elements in the intersection of three sets, the number of elements in each pairwise intersection of these sets, and the number of elements in each set, find the number of elements in their union. 9. Given a positive integer n, produce the formula for the number of elements in the union of n sets. 10. Given positive integers m and n, find the number of onto functions from a set with m elements to a set with n elements. 11. Given a positive integer n, list all the derangements of the set{I, 2, 3, ... , n}. Use a computational program or programs you have written to do these exercises. 1. Find the exact value of 1100,1500, and 11000, where In is the nth Fibonacci number. 2. Find the smallest Fibonacci number greater than 1,000,000, greater than 1,000,000,000, and greater than 1,000,000,000,000 . 3. Find as many prime Fibonacci numbers as you can. It is unknown whether there are infinitely many of these. 4. Write out all the moves required to solve the Tower of Hanoi puzzle with 10 disks. 5. Write out all the moves required to use the Frame-Stewart algorithm to move 20 disks from one peg to another peg using four pegs according to the rules of the Reve's pUzzle. 6. Verify the Frame conjecture for solving the Reve's puzzle for n disks for as many integers n as possible by show­ ing that the puzzle cannot be solved using fewer moves than are made by the Frame-Stewart algorithm with the optimal choice of k. 7. Compute the number of operations required to multiply two integers with n bits for various integers n includ-ing 16, 64, 256, and 1024 using the fast multiplication described in Section 7.3 and the standard algorithm for multiplying integers (Algorithm 3 in Section 3.6). 8. Compute the number of operations required to multiply two n x n matrices for various integers n including 4, 16, 64, and 128 using the fast matrix multiplication described in Section 7.3 and the standard algorithm for multiplying matrices (Algorithm 1 in Section 3.8). 9. Use the sieve of Eratosthenes to find all the primes not exceeding 1000. 10. Find the number of primes not exceeding 10,000 using the method described in Section 7.6 to find the number of primes not exceeding 100. 11. List all the derangements of {I, 2, 3,4,5,6,7, 8}. 12. Compute the probability that a permutation of n objects is a derangement for all positive integers not exceeding 20 and determine how quickly these probabilities ap­ proach the number e. 518 7 / Advanced Counting Techniques Writing Projects Respond with essays using outside sources. 1. Find the original source where Fibonacci presented his puzzle about modeling rabbit populations. Dis­ cuss this problem and other problems posed by Fibonacci and give some information about Fibonacci himself. 2. Explain how the Fibonacci numbers arise in a variety of applications, such as in phyllotaxis, the study of arrange­ ment of leaves in plants, in the study of reflections by mirrors, and so on. 3. Describe different variations of the Tower of Hanoi puz­ zle, including those with more than three pegs (including the Reve's puzzle discussed in the text and exercises), those where disk moves are restricted, and those where disks may have the same size. Include what is known about the number of moves required to solve each varia­ tion. 4. Discuss as many different problems as possible where the Catalan numbers arise. 5. Describe the solution of Ulam's problem (see Exercise 28 in Section 7.3) involving searching with one lie found by Andrzej Pe1c. 6. Discuss variations ofUlam's problem (see Exercise 28 in Section 7.3) involving searching with more than one lie and what is known about this problem. 7-70 7. Define the convex hull ofa set of points in the plane and describe divide-and-conquer algorithms for finding the convex hull of a set of points in the plane. 8. Look up the definition of the lucky numbers. Explain how they are found using a sieve technique similar to the sieve of Eratosthene s. Find all the lucky numbers less than 1000. 9. Describe how sieve methods are used in number theory. What kind of results have been established using such methods? 10. Look up the rules of the old French card game ofrencon­ tres. Describe these rules and describe the work of Pierre Raymond de Montmort on Ie probleme de rencontres. 11. Describe how exponential generating functions can be used to solve a variety of counting problems. 12. Describe the Polyci theory of counting and the kind of counting problems that can be solved using this theory. 13. The probleme des menages (the problem of the house­ holds) asks for the number of ways to arrange n couples around a table so that the sexes alternate and no husband and wife are seated together. Explain the method used by E. Lucas to solve this problem. 14. Explain how rook polynomials can be used to solve count­ ing problems. CH APTER 8.1 Relations and Their Properties 8.2 n-ary Relations and Their Applications 8.3 Representing Relations 8.4 Closures of Relations 8.5 Equivalence Relations 8.6 Partial Orderings Relations IT)) elationships between elements of sets occur in many contexts. Every day we deal with l&.relationships such as those between a business and its telephone number, an employee and his or her salary, a person and a relative, and so on. In mathematics we study relationships such as those between a positive integer and one that it divides, an integer and one that it is congruent to modulo 5, a real number and one that is larger than it, a real number x and the value f(x) where f is a function, and so on. Relationships such as that between a program and a variable it uses and that between a computer language and a valid statement in this language often arise in computer science. Relationships between elements of sets are represented using the structure called a relation, which is just a subset ofthe Cartesian product of the sets. Relations can be used to solve problems such as determining which pairs of cities are linked by airline flights in a network, finding a viable order for the different phases of a complicated project, or producing a useful way to store information in computer databases. In some computer languages, only the first 31 characters of the name of a variable matter. The relation consisting of ordered pairs of strings where the first string has the same initial 31 characters as the second string is an example of a special type of relation, known as an equivalence relation. Equivalence relations arise throughout mathematics and computer science. We will study equivalence relations, and other special types of relations, in this chapter. 8.1 Relations and Their Properties DEFINITION 1 Introduction The most direct way to express a relationship between elements of two sets is to use ordered pairs made up of two related elements. For this reason, sets of ordered pairs are called binary relations. In this section we introduce the basic terminology used to describe binary relations. Later in this chapter we will use relations to solve problems involving communications networks, project scheduling, and identifying elements in sets with common properties. Let A and B be sets. A binary relation from A to B is a subset of A x B. In other words, a binary relation from A to B is a set R of ordered pairs where the first element of each ordered pair comes from A and the second element comes from B. We use the notation aRb to denote that (a, b) E R and aRb to denote that (a, b) t/:. R. Moreov er, when (a, b) belongs to R, a is said to be related to b by R. Binary relations represent relationships between the elements of two sets. We will introduce n-ary relations, which express relationships among elements of more than two sets, later in this chapter. We will omit the word binary when there is no danger of confusion. Examples 1 � 3 illustrate the notion of a relation. EXAMPLE 1 Let A be the set of students in your school, and let B be the set of courses. Let R be the relation that consists of those pairs (a, b), where a is a student enrolled in course b. For instance, if 8-/ 519 520 8 / Relations oe� R a b ea 0 X X Ie X 2 X 2e / eb FIGURE 1 Displaying the Ordered Pairs in the Relation R from Example 3. 8-2 Jason Goodfriend and Deborah Sherman are enrolled in CS518, the pairs (Jason Goodfriend, CS518) and (Deborah Sherman, CS518) belong to R. If Jason Goodfriend is also enrolled in CS51O, then the pair (Jason Goodfriend, CS51O) is also in R. However, if Deborah Sherman is not enrolled in CS51O, then the pair (Deborah Sherman, CS51O) is not in R. Note that if a student is not currently enrolled in any courses there will be no pairs in R that have this student as the first element. Similarly, if a course is not currently being offered there will be no pairs in R that have this course as their second element. .... EXAMPLE 2 Let A be the set of all cities, and let B be the set of the 50 states in the United States of America. Define the relation R by specifying that (a, b) belongs to R if city a is in state b. For instance, (Boulder, Colorado), (Bangor, Maine), (Ann Arbor, Michigan), (Middletown, New Jersey), (Middletown, New York), (Cupertino, California), and (Red Bank, New Jersey) are in R. .... EXAMPLE 3 Let A = {O, 1, 2} and B = {a, b}. Then {(O, a), (0, b), (1, a), (2, b)} is a relation from A to B. This means, for instance, that ORa, but that 1 � b. Relations can be represented graphical ly, as shown in Figure 1, using arrows to represent ordered pairs. Another way to represent this relation is to use a table, which is also done in Figure 1. We will discuss representations of relations in more detail in Section 8.3. .... Functions as Relations Recall that a function f from a set A to a set B (as defined in Section 2.3) assigns exactly one element of B to each element of A. The graph of f is the set of ordered pairs (a, b) such that b = f(a). Because the graph of f is a subset of A x B, it is a relation from A to B. Moreover, the graph of a function has the property that every element of A is the first element of exactly one ordered pair of the graph. Converse ly, if R is a relation from A to B such that every element in A is the first element of exactly one ordered pair of R, then a function can be defined with R as its graph. This can be done by assigning to an element a of A the unique element bE B such that (a, b) E R. (Note that the relation R in Example 2 is not the graph of a function because Middletown occurs more than once as the first element of an ordered pair in R.) A relation can be used to express a one-to-ma ny relationship between the elements of the sets A and B (as in Example 2), where an element of A may be related to more than one element of B . A function represents a relation where exactly one element of B is related to each element of A. Relations are a generalization of functions; they can be used to express a much wider class of relationships between sets. 8-3 Relations on a Set Relations from a set A to itself are of special interest. 8.1 Relations and Their Properties 521 DEFINITION 2 A relation on the set A is a relation from A to A. In other words, a relation on a set A is a subset of A x A. EXAMPLE 4 Let A be the set{l, 2, 3, 4}. Which ordered pairs are in the relation R = {(a, b) I a divides b}? Solution: Because (a, b) is in R if and only if a and b are positive integers not exceeding 4 such that a divides b, we see that R = {(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2,4), (3, 3), (4, 4)}. The pairs in this relation are displayed both graphically and in tabular form in Figure 2. Next, some examples of relations on the set of integers will be given in Example 5. EXAMPLE 5 Consider these relations on the set of integers: R\ = {(a, b) I a ::s b}, R2 = {(a, b) I a> b}, R3 = {(a, b) I a = b or a = -b}, R4 = {(a, b) I a = b}, Rs = {(a, b) I a = b + I}, R6 = {(a, b) I a + b ::s 3}. Which of these relations contain each of the pairs (1, 1), (1,2), (2, 1), (1, -1), and (2, 2)? Remark: Unlike the relations in Examples 1-4, these are relations on an infinite set. Solution: The pair (1,1) is in R\, R3, R4, and R6; (1, 2) is in R\ and R6; (2,1) is in R2, Rs, and R6; (1, -1) is in R2, R3, and R6; and finally, (2, 2) is in R\, R3, and R4• .... 4. -----.< .. R 2 3 4 2 3 4 x x x x x x x x FIGURE 2 Displaying the Ordered Pairs in the Relation R from Example 4. 522 8 / Relations 8-4 It is not hard to determine the number of relations on a finite set, because a relation on a set A is simply a subset of A x A. EXAMPLE 6 How many relations are there on a set with n elements? DEFINITION 3 Solution: A relation on a set A is a subset of A x A. Because A x A has n2 elements when A has n elements, and a set with m elements has 2m subsets, there are 2n2 subsets of A x A. Thus, there are 2n2 relations on a set with n elements. For example, there are 232 = 29 = 512 relations on the set {a, b, c}. � Properties of Relations There are several properties that are used to classify relations on a set. We will introduce the most important of these here. In some relations an element is always related to itself. For instance, let R be the relation on the set of all people consisting of pairs (x, y) where x and y have the same mother and the same father. Then x R x for every person x. A relation R on a set A is called reflexive if (a, a) E R for every element a EA. Remark: Using quantifiers we see that the relation R on the set A is reflexive if Va«a, a) E R), where the universe of discourse is the set of all elements in A. We see that a relation on A is reflexive if every element of A is related to itself. Examples 7-9 illustrate the concept of a reflexive relation. EXAMPLE 7 Consider the following relations on {l, 2, 3, 4}: RJ = {(1, 1),(1,2),(2, 1),(2,2),(3,4),(4, 1), (4,4)}, R2 = {(1, 1), (1, 2), (2, I)}, R3 = {(I, 1), (1, 2), (1,4), (2,1), (2, 2), (3, 3), (4,1), (4, 4)}, R4 = {(2, 1), (3, 1), (3, 2), (4, 1), (4, 2), (4, 3)}, R5 = {(1, 1), (1, 2), (1, 3), (1,4), (2, 2), (2, 3), (2, 4), (3, 3), (3,4), (4, 4)}, R6 = {(3, 4)}. Which of these relations are reflexive? Solution: The relations R3 and R5 are reflexive because they both contain all pairs of the form (a, a), namely, (1, 1), (2, 2), (3, 3), and (4,4). The other relations are not reflexive because they do not contain all of these ordered pairs. In particular, R J, R2, R4, and R6 are not reflexive because (3, 3) is not in any of these relations. � EXAMPLE 8 Which ofthe relations from Example 5 are reflexive? Solution: The reflexive relations from Example 5 are RJ (because a :::: a for every integer a), R3, and R4. For each of the other relations in this example it is easy to find a pair of the form (a, a) that is not in the relation. (This is left as an exercise for the reader.) � 8-5 8.1 Relations and Their Properties 523 EXAMPLE 9 Is the "divides" relation on the set of positive integers reflexive? DEFINITION 4 EXAMPLE 10 Ex1ra� Examples � EXAMPLE 11 Solution: Because a I a whenever a is a positive integer, the "divides" relation is reflexive. (Note that if we replace the set of positive integers with the set of all integers the relation is not reflexive because 0 does not divide 0.) � In some relations an element is related to a second element if and only if the second element is also related to the first element. The relation consisting of pairs (x, y), where x and yare students at your school with at least one common class has this property. Other relations have the property that if an element is related to a second element, then this second element is not related to the first. The relation consisting of the pairs (x, y), where x and yare students at your school, where x has a higher grade point average than y has this property. A relation R onasetA is called symmetric if(b, a) E R whenever (a, b) E R, for all a, bE A. A relation R on a set A such that for all a, bE A, if(a, b) E Rand (b, a) E R, then a = b is called antisymmetric. Remark: Using quantifiers, we see that the relation R on the set A is symmetric if VaVb«a, b) E R --+ (b, a) E R). Similarly, the relation R on the set A is antisymmetric if VaVb«(a, b) E R /\ (b, a) E R) --+ (a = b)). That is, a relation is symmetric if and only if a is related to b implies that b is related to a. A relation is anti symmetric if and only if there are no pairs of distinct elements a and b with a related to band b related to a. That is, the only way to have a related to band b related to a is for a and b to be the same element. The terms symmetric and antisymmetric are not opposites, because a relation can have both of these properties or may lack both of them (see Exercise 8 at the end of this section). A relation cannot be both symmetric and anti symmetric if it contains some pair of the form (a, b), where a =1= b. Remark: Although relatively few of the 2n2 relations on a set with n elements are symmetric or antisymmetr ic, as counting arguments can show, many important relations have one of these properties. (See Exercise 45.) Which of the relations from Example 7 are symmetric and which are anti symmetric? Solution: The relations R2 and R3 are symmetric, because in each case (b, a) belongs to the relation whenever (a, b) does. For R2, the only thing to check is that both (2, 1) and (1,2) are in the relation. For R3, it is necessary to check that both (1,2) and (2, I) belong to the relation, and (I, 4) and (4, I) belong to the relation. The reader should verify that none of the other relations is symmetri c. This is done by finding a pair (a, b) such that it is in the relation but (b, a) is not. R4, Rs, and R6 are all antisymmetric. For each of these relations there is no pair of elements a and b with a =1= b such that both (a, b) and (b, a) belong to the relation. The reader should verify that none of the other relations is anti symmetric. This is done by finding a pair (a, b) with a =1= b such that (a, b) and (b, a) are both in the relation. � Which of the relations from Example 5 are symmetric and which are anti symmetric? Solution: The relations R3, R4, and R6 are symmetri c. R3 is symmetric, for if a = b or a = -b, then b = a or b = -a. R4 is symmetric because a = b implies that b = a. R6 is symmetric 524 8 / Relations 8-6 because a + b ::: 3 implies that b + a ::: 3. The reader should verify that none of the other relations is symmetri c. The relations RI, R2, R4, and R5 are antisymmetri c. RI is anti symmetric because the inequalities a ::: band b ::: a imply that a = b. R2 is anti symmetric because it is impossible for a > b and b > a. R4 is antisymmetric, because two elements are related with respect to R4 if and only if they are equal. R5 is anti symmetric because it is impossible that a = b + I and b = a + I. The reader should verify that none of the other relations is antisymmetr ic. ... EXAMPLE 12 Is the "divides" relation on the set of positive integers symmetric? Is it antisymmetric? DEFINITION 5 EXAMPLE 13 Extra� Examples� EXAMPLE 14 Solution: This relation is not symmetric because 1 12, but 2 y 1. It is anti symmetric, for if a and b are positive integers with alb and b 1 a, then a = b (the verification of this is left as an exercise for the reader). ... Let R be the relation consisting of all pairs (x, y) of students at your school, where x has taken more credits than y. Suppose that x is related to y and y is related to z. This means that x has taken more credits than y and y has taken more credits than z. We can conclude that x has taken more credits than z, so that x is related to z. What we have shown is that R has the transitive property, which is defined as follows. A relation R on a set A is called transitive if whenever (a, b) E R and (b, c) E R, then (a, c) E R, for all a, b, c E A. Remark: Using quantifiers we see that the relation R on a set A is transitive if we have 'Va'Vb'Vc«(a, b) E R /\ (b, c) E R) -+ (a, c) E R). Which of the relations in Example 7 are transitive? Solution: R4, R5, and R6 are transitive. For each of these relations, we can show that it is transitive by verifying that if (a, b) and (b, c) belong to this relation, then (a, c) also does. For instance, R4 is transitive, because (3,2) and (2, I), (4, 2) and (2, I), (4, 3) and (3, I), and (4,3) and (3, 2) are the only such sets of pairs, and (3, I), (4, I), and (4, 2) belong to R4• The reader should verify that R5 and R6 are transitive. RI is not transitive because (3,4) and (4, I) belong to RI, but (3, I) does not. R2 is not transitive because (2, I) and (1, 2) belong to R2, but (2, 2) does not. R3 is not transitive because (4, I) and (1,2) belong to R3, but (4,2) does not. ... Which of the relations in Example 5 are transitive? Solution: The relations R I, R2, R3, and R4 are transitive. R I is transitive because a ::: band b ::: c imply that a ::: c. R2 is transitive because a > b and b > c imply that a > c. R3 is transitive because a = ±b and b = ±c imply that a = ±c. R4 is clearly transitive, as the reader should verify. R5 is not transitive because (2, I) and (1, 0) belong to R5, but (2, 0) does not. R6 is not transitive because (2, 1) and (1, 2) belong to R6, but (2, 2) does not. ... EXAMPLE 15 Is the "divides" relation on the set of positive integers transitive? Solution: Suppose that a divides b and b divides c. Then there are positive integers k and I such that b = ak and c = bl. Hence, c = a(kl), so a divides c. It follows that this relation is transitive. ... 8-7 8.1 Relations and Their Properties 525 We can use counting techniques to determine the number of relations with specific proper­ ties. Finding the number of relations with a particular property provides information about how common this property is in the set of all relations on a set with n elements. EXAMPLE 16 How many reflexive relations are there on a set with n elements? Solution: A relation R on a set A is a subset of A x A. Consequently, a relation is determined by specifying whether each of the n2 ordered pairs in A x A is in R. However, if R is reflexive, each of the n ordered pairs (a, a) for a E A must be in R. Each of the other n(n -1) ordered pairs of the form (a, b), where a =f. b, mayor may not be in R. Hence, by the product rule for counting, there are 2n (n-I) reflexive relations [this is the number of ways to choose whether each element (a, b), with a =f. b, belongs to R]. .... The number of symmetric relations and the number of anti symmetric relations on a set with n elements can be found using reasoning similar to that in Example 16 (see Exercise 45 at the end of this section). Counting the transitive relations on a set with n elements is a problem beyond the scope of this book. Combining Relations Because relations from A to B are subsets of A x B, two relations from A to B can be combined in any way two sets can be combined. Consider Examples 17-19. EXAMPLE 17 Let A = {l, 2, 3} and B = {I, 2, 3, 4}. The relations RI = {(I, 1), (2,2), (3,3)} and R2 = {(1, 1), (1, 2), (1, 3), (1, 4)} can be combined to obtain RI U R2 = {(I, 1), (1, 2), (1, 3), (1, 4), (2,2), (3, 3)}, RI n R2 = {(1, I)}, RI - R2 = {(2, 2), (3, 3)}, R2 -RI = {(1, 2), (1, 3), (1, 4)}. EXAMPLE 18 Let A and B be the set of all students and the set of all courses at a school, respectively. Suppose that R I consists of all ordered pairs (a, b), where a is a student who has taken course b, and R2 consists of all ordered pairs (a, b), where a is a student who requires course b to graduate. What are the relations RI U R2, RI n R2, RI � R2, RI - R2, and R2 -RI? Solution: The relation RI U R2 consists of all ordered pairs (a, b), where a is a student who either has taken course b or needs course b to graduate, and RI n R2 is the set of all ordered pairs (a, b), where a is a student who has taken course b and needs this course to graduate. Also, RI � R2 consists of all ordered pairs (a, b), where student a has taken course b but does not need it to graduate or needs course b to graduate but has not taken it. RI - R2 is the set of ordered pairs (a, b), where a has taken course b but does not need it to graduate; that is, b is an elective course that a has taken. R2 -RI is the set of all ordered pairs (a, b), where b is a course that a needs to graduate but has not taken. .... EXAMPLE 19 Let R I be the "less than" relation on the set of real numbers and let R2 be the "greater than" relation on the set of real numbers, that is, RI = {(x,y) I x < y} and R2 = {(x,y) I x> y}. What are RI U R2, RI n R2, RI - R2, R2 - RI, and RI � R2? 526 8 / Relations DEFINITIO N 6 8-8 Solution: We note that (x, y) E R, U R2 ifandonlyif(x, y) E R, or(x, y) E R2. Hence, (x, y) E R, U R2 if and only if x < y or x > y. Because the condition x < y or x > y is the same as the condition x "I y, it follows that R, U R2 = {(x, y) I x "I y}. In other words, the union of the "less than" relation and the "greater than" relation is the "not equals" relation. Next, note that it is impossible for a pair (x, y) to belong to both R, and R2 because it is impossible for x < y and x > y. It follows that R, n R2 = 0. We also see that R, - R2 = R" R2 -R, = R2, and R, EI1 R2 = R, U R2 -R, n R2 = {(x, y) I x "I y}. <0lIl There is another way that relations are combined that is analogous to the composition of functions. Let R be a relation from a set A to a set B and S a relation from B to a set C. The composite of Rand S is the relation consisting of ordered pairs (a, c), where a E A, c E C, and for which there exists an element bE Bsuch that (a, b) E Rand (b, c) E S. We denote the composite of R and S by S 0 R. Computing the composite of two relations requires that we find elements that are the second element of ordered pairs in the first relation and the first element of ordered pairs in the second relation, as Examples 20 and 21 illustrate. EXAMPLE 20 What is the composite oftherelations R andS, where R is the relation from {I, 2, 3} to {I, 2, 3, 4} with R = {(1, 1), (1, 4), (2, 3), (3,1), (3, 4)} and S is the relation from {l, 2, 3, 4} to {O, 1, 2} with S = {(1, 0), (2, 0), (3,1), (3, 2), (4, I)}? Solution: S 0 R is constructed using all ordered pairs in R and ordered pairs in S, where the second element of the ordered pair in R agrees with the first element of the ordered pair in S. For example, the ordered pairs (2, 3) in R and (3, 1) in S produce the ordered pair (2, 1) in S 0 R. Computing all the ordered pairs in the composite, we find So R = {(1, 0), (1,1), (2,1), (2, 2), (3, 0), (3, I)}. EXAMPLE 21 Composing the Parent Relation with Itself Let R be the relation on the set of all people such that (a, b) E R if person a is a parent of person b. Then (a, c) E R 0 R if and only if there is a person b such that (a, b) E Rand (b, c) E R, that is, if and only if there is a person b such that a is a parent of b and b is a parent of c. In other words, (a, c) E R 0 R if and only if a is a grandparent of c. <0lIl DEFINITIO N 7 The powers of a relation R can be recursively defined from the definition of a composite of two relations. Let R be a relation on the set A. The powers R n, n = 1,2,3, ... , are defined recursively by and The definition shows that R2 = R 0 R, R3 = R2 0 R = (R 0 R)o R, and so on. 8-9 8.1 Relations and Their Properties 527 EXAMPLE 22 Let R = {(I, 1), (2, 1), (3,2), (4, 3)}. Find the powers Rn, n = 2,3,4, .... THEOREMl Exercises Solution: Because R2 = R oR, we find that R2 = {(1, 1), (2,1), (3,1), (4, 2)}. Furthermore, because R3 = R2 oR, R3 = {(I, 1), (2,1), (3,1), (4, I)}. Additional computation shows that R4 is the same as R3, so R4 = {(1, 1), (2,1), (3,1), (4, I)}. It also follows that Rn = R3 for n = 5,6,7, .... The reader should verify this. .... The following theorem shows that the powers of a transitive relation are subsets of this relation. It will be used in Section 8.4. The relation R on a set A is transitive if and only if Rn � R for n = 1, 2, 3, .... Proof: We first prove the "if"partofthetheorem. We suppose that Rn � R forn = 1,2,3, .... In particular, R2 � R. To see that this implies R is transitive, note that if (a, b) E Rand (b, c) E R, then by the definition of composition, (a, c) E R2. Because R2 � R, this means that (a, c) E R. Hence, R is transitive. We will use mathematical induction to prove the only ifpart of the theorem. Note that this part of the theorem is trivially true for n = 1. Assume that Rn � R, where n is a positive integer. This is the inductive hypothesis. To complete the inductive step we must show that this implies that Rn+1 is also a subset of R. To show this, assume that (a, b) E Rn+l. Then, because Rn+1 = Rn 0 R, there is an element x with x E A such that (a, x) E R and (x, b) E Rn. The inductive hypothesis, namely, that Rn � R, implies that (x, b) E R. Furthermore, because R is transitive, and (a, x) E R and (x, b) E R, it follows that (a, b) E R. This shows that Rn+1 � R, completing the proof. <J 1. List the ordered pairs in the relation R from A = {O, 1,2,3, 4} to B = {O, 1,2, 3}, where (a, b) E R ifand 4. Determine whether the relation R on the set of all people is reflexive, symmetric, antisymmetric, and/or transitive, where (a, b) E R if and only if only if a) a = b. e) a> b. e) gcd(a, b) = 1. b) a +b = 4. d) a lb. t) Icm(a, b) = 2. 2. a) List all the ordered pairs in the relation R = {(a, b) I a divides b } on the set {I, 2, 3, 4,5, 6}. b) Display this relation graphically, as was done in Example 4. e) Display this relation in tabular form, as was done in Example 4. 3. For each of these relations on the set {I, 2, 3, 4}, decide whether it is reflexive, whether it is symmetri c, whether it is antisymmetric, and whether it is transitive. a) {(2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4)} b) {(I, 1), (1, 2), (2,1), (2, 2), (3, 3), (4, 4)} e) {(2,4),(4,2)} d) {(I, 2), (2, 3), (3, 4)} e) {(I, 1), (2, 2), (3, 3), (4, 4)} t) {(I, 3), (1, 4), (2, 3), (2,4), (3,1), (3, 4)} a) a is taller than b. b) a and b were born on the same day. e) a has the same first name as b. d) a and b have a common grandparent. 5. Determine whether the relation R on the set of all Web pages is reflexive, symmetric, anti symmetric, and/ortran­ sitive, where (a, b) E R if and only if a) everyone who has visited Web page a has also visited Web page b. b) there are no common links found on both Web page a and Web page b. e) there is at least one common link on Web page a and Web page b. d) there is a Web page that includes links to both Web page a and Web page b. 6. Determine whether the relation R on the set of all real numbers is reflexive, symmetric, antisymmetric, and/or transitive, where (x, Y) E R if and only if 528 8 / Relations a) x + y = o. c) x -y is a rational number. e) xy � O. g) x = 1. b) x = ±y. d) x = 2y. t) xy = O. h) x = 1 or y = 1. 7. Determine whether the relation R on the set of all integers is reflexive, symmetric, anti symmetric, and/or transitive, where (x, y) E R if and only if a) x '" y. b) xy � 1. c) x = y + 1 or x = y-1. d) x == y (mod 7). e) x is a multiple of y. t) x and yare both negative or both nonnegative. g) x = y2. h) x � y2. 8. Give an example of a relation on a set that is a) symmetric and anti symmetr ic. b) neither symmetric nor anti symmetr ic. A relation R on the set A is irreftexive if for every a E A, (a, a) ¢ R. That is, R is irreflexive if no element in A is related to itself. 9. Which relations in Exercise 3 are irreflexive? 10. Which relations in Exercise 4 are irreflexive? 11. Which relations in Exercise 5 are irreflexive? 12. Which relations in Exercise 6 are irreflexive? 13. Can a relation on a set be neither reflexive nor irreflexive? 14. Use quantifiers to express what it means for a relation to be irreflexive. 15. Give an example of an irreflexive relation on the set of all people. A relation R is called asymmetric if (a, b) E R implies that (b, a) ¢ R. Exercises 16-22 explore the notion of an asym­ metric relation. Exercise 20 focuses on the difference between asymmetry and anti symmetry. 16. Which relations in Exercise 3 are asymmetric? 17. Which relations in Exercise 4 are asymmetric? 18. Which relations in Exercise 5 are asymmetric? 19. Which relations in Exercise 6 are asymmetric? 20. Must an asymmetric relation also be anti symmetric? Must an anti symmetric relation be asymmetric? Give reasons for your answers. 21. Use quantifiers to express what it means for a relation to be asymmetri c. 22. Give an example of an asymmetric relation on the set of all people. 23. How many different relations are there from a set with m elements to a set with n elements? �Let R be a relation from a set A to a set B. The inverse rela­ tion from B to A, denoted by R-I, is the set of ordered pairs {(b, a) I (a, b) E R}. The complementary relation R is the set of ordered pairs {(a, b) I (a, b) ¢ R}. 24. Let R be the relation R = {(a, b) I a < b} on the set of integers. Find a) R-I. b) R. 8-/0 25. Let R be the relation R = {(a, b) I a divides b} on the set of positive integers. Find a) R-I. b)R. 26. Let R be the relation on the set of all states in the United States consisting of pairs (a, b) where state a borders state b. Find a) R-I• b) R. 27. Suppose that the function f from A to B is a one-to­ one correspondence. Let R be the relation that equals the graph of f. That is, R = {(a, f(a)) I a E A}. What is the inverse relation R-I? 28. Let RI = {(I, 2), (2, 3), (3, 4)} and R2 = {(I, 1), (1,2), (2, 1), (2,2), (2,3), (3, 1), (3,2), (3,3), (3, 4)} be rela­ tions from {I, 2, 3} to {I, 2, 3, 4}. Find a) RI U R2. c) RI - R2. b) RI n R2. d) R2 -RI. 29. Let A be the set of students at your school and B the set of books in the school library. Let RI and R2 be the relations consisting of all ordered pairs (a, b), where student a is required to read book b in a course, and where student a has read book b, respectively. Describe the ordered pairs in each ofthese relations. a) RI U R2 c) RI ED R2 e) R2 - RI 30. Let R be the relation {(I, 2), (1, 3), (2, 3), (2, 4), (3, I)}, and let S be the relation {(2, 1), (3, 1), (3, 2), (4, 2)}. Find S oR. 31. Let R be the relation on the set of people consisting of pairs ( a, b), where a is a parent of b. Let S be the relation on the set of people consisting of pairs (a, b), where a and b are siblings (brothers or sisters). What are S 0 Rand R oS? Exercises 32-35 deal with these relations on the set of real numbers: RI = {(a, b) E R2 I a > b}, the "greater than" relation, R2 = {(a, b) E R2 I a � b}, the "greater than or equal to" relation, R3 = {(a, b) E R2 I a < b}, the "less than" relation, R4 = {(a, b) E R2 I a :::: b}, the "less than or equal to" relation, Rs = {(a, b) E R2 I a = b}, the "equal to" relation, R6 = {(a, b) E R2 I a '" b}, the "unequal to" relation. 32. Find a) RI UR3· b) RI U Rs. c) R2 n R4. d) R3 n Rs. e) RI -R2. t) R2 -RI. g) RI ED R3· h) R2 ED R4. 33. Find a) R2 U R4. b) R3 U R6. c) R3 n R6. d) R4 n R6. e) R3 - R6· t) R6 -R3· g) R2 ED R6. h) R3 ED Rs. 8-11 34. Find a) R, 0 R,. b) R, 0 R2. c) R, 0 R3. d) R, 0 R4. e) R, 0 R5. f) R, 0 R6. g) R2 0 R3. h) R3 0 R3. 35. Find a) R2 0 R,. b) R2 0 R2. c) R3 0 R5. d) R4 0 R,. e) R5 0 R3. f) R3 0 R6. g) R4 0 R6. h) R6 0 R6. 36. Let R be the parent relation on the set of all people (see Example 21). When is an ordered pair in the rela­ tion R3? 37. Let R be the relation on the set of people with doctorates such that (a, b) E R if and only if a was the thesis advi­ sor of b. When is an ordered pair (a, b) in R2? When is an ordered pair (a, b) in Rn, when n is a positive inte­ ger? (Note that every person with a doctorate has a thesis advisor.) 38. Let R, and R2 be the "divides" and "is a multiple of" relations on the set of all positive integers, respectively. That is, R, = {(a, b) I a divides b} and R2 = {(a, b) I a is a multiple of b}. Find a) R, U R2. c) R, - R2. e) R, E9 R2. b) R, n R2. d) R2 -R,. 39. Let R, and R2 be the "congruent modulo 3" and the "congruent modulo 4" relations, respectively, on the set of integers. That is, R, = {(a, b) I a == b(mod 3)} and R2 = {(a, b) I a == b (mod 4)}. Find a) R, U R2. b) R, n R2. c) R, - R2. d) R2 -R,. e) R, E9 R2. 40. List the 16 different relations on the set {O, I}. 41. How many of the 16 different relations on {O, I} contain the pair (0, I)? 42. Which of the 16 relations on {O, l}, which you listed in Exercise 40, are a) reflexive? b) irreflexive? c) symmetric? d) anti symmetric? e) asymmetric? f) transitive? 43. a) How many relations are there on the set {a, b, c, d}? b) How many relations are there on the set {a, b, c, d} that contain the pair (a, a)? 44. Let S be a set with n elements and let a and b be distinct elements of S. How many relations are there on S such that a) (a, b) E S? b) (a, b) � S? c) there are no ordered pairs in the relation that have a as their first element? d) there is at least one ordered pair in the relation that has a as its first element? e) there are no ordered pairs in the relation that have a as their first element and there are no ordered 8.1 Relations and Their Properties 529 pairs in the relation that have b as their second element? f) there is at least one ordered pair in the relation that either has a as its first element or has b as its second element? *45. How many relations are there on a set with n elements that are a) symmetric? b) anti symmetric? c) asymmetric? d) irreflexive? e) reflexive and symmetric? f) neither reflexive nor irreflexive? *46. How many transitive relations are there on a set with n elements if a) n = I? b) n = 2? c) n = 3? 47. Find the error in the "proof" ofthe following "theorem." "Theorem" : Let R be a relation on a set A that is symmet­ ric and transitive. Then R is reflexive. "Proof": Let a E A. Take an element bE A such that (a, b) E R. Because R is symmetric, we also have (b, a) E R. Now using the transitive property, we can conclude that (a, a) E R because (a, b) E R imd (b, a) E R. 48. Suppose that R and S are reflexive relations on a set A. Prove or disprove each ofthese statements. a) R U S is reflexive. b) R n S is reflexive. c) R E9 S is irreflexive. d) R -S is irreflexive. e) So R is reflexive. 49. Show that the relation R on a set A is symmetric if and only if R = R-', where R-' is the inverse relation. 50. Show that the relation R on a set A is anti symmetric if and only if R n R-' is a subset ofthe diagonal relation � = {(a, a) I a E A}. 51. Show that the relation R on a set A is reflexive if and only if the inverse relation R-' is reflexive. 52. Show that the relation R on a set A is reflexive if and only if the complementary relation R is irreflexive. 53. Let R be a relation that is reflexive and transitive. Prove that Rn = R for all positive integers n. 54. Let R be the relation on the set {I, 2, 3, 4, 5} contain­ ing the ordered pairs (1,1), (1,2), (1,3), (2,3), (2,4), (3, 1), (3,4), (3, 5), (4, 2), (4, 5), (5, 1), (5, 2), and (5 ,4). Find a) R2. b) R3. c) R4. d) R5. 55. Let R be a reflexive relation on a set A. Show that Rn is reflexive for all positive integers n. *56. Let R be a symmetric relation. Show that Rn is symmetric for all positive integers n. 57. Suppose that the relation R is irreflexive. Is R2 necessarily irreflexive? Give a reason for your answer. 530 8 / Relations 8-12 8.2 n-ary Relations and Their Applica tions Introduction Relationships among elements of more than two sets often arise. For instance, there is a relation­ ship involving the name of a student, the student's major, and the student's grade point average. Similarly, there is a relationship involving the airline, flight number, starting point, destination, departure time, and arrival time of a flight. An example of such a relationship in mathematics involves three integers, where the first integer is larger than the second integer, which is larger than the third. Another example is the betweenness relationship involving points on a line, such that three points are related when the second point is between the first and the third. We will study relationships among elements from more than two sets in this section. These re­ lationships are called n-ary relations. These relations are used to represent computer databases. These representations help us answer queries about the information stored in databases, such as: Which flights land at O'Hare Airport between 3 A.M. and 4 A.M.? Which students at your school are sophomores majoring in mathematics or computer science and have greater than a 3.0 average? Which employees of a company have worked for the company less than 5 years and make more than $50,000? n-ary Relations We begin with the basic definition on which the theory of relational databases rests. DEFINITIO N 1 LetAI, A2, ... , An besets. An n-ary relation on these sets is a subset of Al x A2 X .,. x An. The sets AI, A2, ... , An are called the domains of the relation, and n is called its degree. EXAMPLE 1 Let R be the relation on N x N x N consisting of triples (a, b, c), where a, b, and c are integers witha < b < c. Then (1 , 2, 3) E R, but (2, 4, 3) 1-R. The degree ofthisrelation is 3. Its domains are all equal to the set of natural numbers. .... EXAMPLE 2 Let R be the relation on Z x Z x Z consisting of all triples of integers (a, b, c) in which a, b, and c form an arithmetic progression. That is, (a, b, c) E R if and only if there is an integer k such that b = a + k and c = a + 2k, or equivalentl y, such that b - a = k and c - b = k. Note that (1,3,5) E R because 3 = 1+2 and 5 = 1+2·2, but (2, 5, 9) 1-R because 5 - 2 = 3 while 9 - 5 = 4. This relation has degree 3 and its domains are all equal to the set of integers. .... EXAMPLE 3 LetR betherelationonZ x Z x Z+ consisting oftriples(a, b, m), where a, b,andm are integers with m ::: 1 and a == b (modm). Then (8,2,3), (-1,9,5), and (14, 0, 7) all belong to R, but (7,2,3), (-2, -8, 5), and(ll, 0, 6) do not belong to R because 8 == 2 (mod 3), -1 == 9 (mod 5), and 14 == o (mod 7), but 7 ¢. 2 (mod 3), -2 ¢. -8 (mod 5), and 11 ¢. 0 (mod 6). This relation has degree 3 and its first two domains are the set of all integers and its third domain is the set of positive integers. .... EXAMPLE 4 Let R be the relation consisting of 5-tuples (A, N, S, D, T) representing airplane flights, where A is the airline, N is the flight number, S is the starting point, D is the destination, and T is the departure time. For instance, if Nadir Express Airlines has flight 963 from Newark to Bangor at 15:00, then (Nadir, 963, Newark, Bangor, 15:00) belongs to R. The degree of this relation is 8-13 linkS� 8.2 n-ary Relations and Their Applications 531 5, and its domains are the set of all airlines, the set of flight numbers, the set of cities, the set of cities (again), and the set of times. .... Databases and Relations The time required to manipulate information in a database depends on how this information is stored. The operations of adding and deleting records, updating records, searching for records, and combining records from overlapping databases are performed millions of times each day in a large database. Because of the importance ofthese operations, various methods for representing databases have been developed. We will discuss one of these methods, called the relational data model, based on the concept of a relation. A database consists of records, which are n-tuples, made up of fields. The fields are the entries of the n-tuples. For instance, a database of student records may be made up of fields containing the name, student number, major, and grade point average of the student. The relational data model represents a database of records as im n-ary relation. Thus, student records are represented as 4-tuples of the form (STUDENT NAME, ID NUMBER, MAJOR, GPA). A sample database of six such records is (Ackermann, 231455, Computer Science, 3.88) (Adams, 888323, Physics, 3.45) (Chou, 102147, Computer Science, 3.49) (Goodfriend, 453876, Mathemati cs, 3.45) (Rao, 678543, Mathemati cs, 3.90) (Stevens, 786576, Psycholo gy, 2.99). Relations used to represent databases are also called tables, because these relations are often displayed as tables. Each column of the table corresponds to an attribute of the database. For instance, the same database of students is displayed in Table I. The attributes of this database are Student Name, ID Number, Major, and GPA. A domain of an n-ary relation is called a primary key when the value of the n-tuple from this domain determines the n-tuple. That is, a domain is a primary key when no two n-tuples in the relation have the same value from this domain. Records are often added to or deleted from databases. Because of this, the property that a domain is a primary key is time-dependent. Consequently, a primary key should be chosen that remains one whenever the database is changed. The current collection of n-tuples in a relation is called the extension of the relation. The more permanent part of a database, including the name and attributes of the database, is called its intension. When selecting a primary key, the goal should be to select a key that can serve as a primary key for all possible extensions of the database. To do this, it is necessary to examine the intension of the database to understand the set of possible n-tuples that can occur in an extension. TABLE 1 Students. Student-name ID_number Major GPA Ackermann 231455 Computer Science 3.88 Adams 888323 Physics 3.45 Chou 102147 Computer Science 3.49 Goodfriend 453876 Mathemat ics 3.45 Rao 678543 Mathema tics 3.90 Stevens 786576 Psychology 2.99 532 8/ Relations 8-14 EXAMPLE 5 Which domains are primary keys for the n-ary relation displayed in Table 1, assuming that no n-tuples will be added in the future? Solution: Because there is only one 4-tuple in this table for each student name, the domain of student names is a primary key. Similarly, the ID numbers in this table are unique, so the domain of ID numbers is also a primary key. However, the domain of major fields of study is not a primary key, because more than one 4-tuple contains the same major field of study. The domain of grade point averages is also not a primary key, because there are two 4-tuples containing the same GPA. .... Combinations of domains can also uniquely identify n-tuples in an n-ary relation. When the values of a set of domains determine an n-tuple in a relation, the Cartesian product of these domains is called a composite key. EXAMPLE 6 Is the Cartesian product of the domain of major fields of study and the domain of GPAs a composite key for the n-ary relation from Table 1, assuming that no n-tuples are ever added? Solution: Because no two 4-tuples from this table have both the same major and the same GPA, this Cartesian product is a composite key. .... Because primary and composite keys are used to identify records uniquely in a database, it is important that keys remain valid when new records are added to the database. Hence, checks should be made to ensure that every new record has values that are different in the appropriate field, or fields, from all other records in this table. For instance, it makes sense to use the student identification number as a key for student records if no two students ever have the same student identification number. A university should not use the name field as a key, because two students may have the same name (such as John Smith). Operations on n-ary Relations There are a variety of operations on n-ary relations that can be used to form new n-ary relations. Applied together, these operations can answer queries on databases that ask for all tuples that satisfy certain conditions. The most basic operation on an n-ary relation is determining all n-tuples in the n-ary relation that satisfy certain condition s. For example, we may want to find all the records of all computer science majors in a database of student records. We may want to find all students who have a grade point average above 3.5 in this same database. We may want to find the records of all computer science majors who have a grade point average above 3.5 in this database. To perform such tasks we use the selection operator. o EFINITI 0 N 2 Let R be an n-ary relation and C a condition that elements in R may satisfy. Then the selection operator Sc maps the n-ary relation R to the n-ary relation of all n-tuples from R that satisfy the condition C. EXAMPLE 7 To find the records of computer science majors in the n-ary relation R shown in Table 1, we use the operator SCI' where C 1 is the condition Major = "Computer Science." The result is the two 8-15 8.2 n-ary Relations and Their Applications 533 4-tuples (Ackermann, 231455, Computer Science, 3.88) and (Chou, 102147, Computer Science, 3.49). Similarly, to find the records of students who have a grade point average above 3.5 in this database, we use the operator sc2, where C2 is the condition GPA > 3.5. The result is the two 4-tuples (Ackermann, 231455, Computer Science, 3.88) and (Rao, 678543, Mathemati cs, 3.90). Finally, to find the records of computer science majors who have a GPA above 3.5, we use the operator SC3' where C 3 is the condition (Major = "Computer Science" /\ GPA > 3.5). The result consists of the single 4-tuple (Ackermann, 231455, Computer Science, 3.88). <III Projections are used to form new n-ary relations by deleting the same fields in every record of the relation. DEFINITION 3 The projection Pili2 ..... im where il < i2 < ... < im, maps the n-tuple (aI, a2, ... , an) to the m-tuple (ail' ai2"'" aim)' where m � n. In other words, the projection Pil.i2 ..... im deletes n -m of the components of an n-tuple, leaving the ilth, i2th, ... , and imth components. EXAMPLE 8 What results when the projection P1,3 is applied to the 4-tuples (2,3,0,4), (Jane Doe, 234111001, Geography, 3.14), and (ai, a2, a3, a4)? Solution: The projection PI.3 sends these 4-tuples to (2, 0), (Jane Doe, Geography), and (ai, a3), respective ly. <III Example 9 illustrates how new relations are produced using projections. EXAMPLE 9 What relation results when the projection PI,4 is applied to the relation in Table I? Solution: When the projection PI.4 is used, the second and third columns of the table are deleted, and pairs representing student names and grade point averages are obtained. Table 2 displays the results ofthis projection. <III Fewer rows may result when a projection is applied to the table for a relation. This happens when some of the n-tuples in the relation have identical values in each ofthe m components of the projection, and only disagree in components deleted by the projection. For instance, consider the following example. EXAMPLE 10 What is the table obtained when the projection Pl,2 is applied to the relation in Table 3? Solution: Table 4 displays the relation obtained when PI.2 is applied to Table 3. Note that there are fewer rows after this projection is applied. <III The join operation is used to combine two tables into one when these tables share some identical fields. For instance, a table containing fields for airline, flight number, and gate, and another table containing fields for flight number, gate, and departure time can be combined into a table containing fields for airline, flight number, gate, and departure time. 534 8 / Relations 8-16 TABLE 2 GPAs. TABLE 3 Enrollments. TABLE 4 Majors. StudentJlame GPA Student Major Course Student Major Ackermann 3.88 Glauser Biology BI290 Glauser Biology Adams 3.45 Glauser Biology MS475 Marcus Mathematics Chou 3.49 Glauser Biology PY 410 Miller Computer Science Goodfriend 3.45 Marcus Mathematics MS 511 Rao 3.90 Marcus Mathematics MS603 Stevens 2.99 Marcus Mathematics CS 322 Miller Computer Science MS575 Miller Computer Science CS455 DEFINITION 4 Let R be a relation of degree m and S a relation of degree n. The join Jp(R. S), where p::: m and p::: n, is a relation of degree m + n - p that consists of all (m + n -p)-tuples (at. a2 ••••• am-po Ct. C2 •.••• cpo bt• b2 •..•• bn-p), where the m-tuple (at. a2 ••••• am-po Ct. C2 ••••• cp) belongs to R and the n-tuple (Ct. C2 •.••• cpo bt• b2 •••.• bn-p) belongs to S. In other words. the join operator Jp produces a new relation from two relations by combining all m-tuples of the first relation with all n-tuples of the second relation. where the last p components of the m-tuples agree with the first p components of the n-tuples. EXAMPLE 11 What relation results when the join operator h is used to combine the relation displayed in Tables 5 and 6? Solution: The join h produces the relation shown in Table 7. There are other operators besides projections and joins that produce new relations from existing relations. A description of these operations can be found in books on database theory. TABLE 5 Teaching-.assignments. TABLE 6 Class..schedule. Course_ Course_ Professor Department number Department number Room Time Cruz Zoology 335 Computer Science 518 N521 2:00 P.M. Cruz Zoology 412 Mathematics 575 N502 3:00 P.M. Farber Psychology 501 Mathematics 611 N521 4:00P.M. Farber Psychology 617 Physics 544 B505 4:00P.M. Grammer Physics 544 Psychology 501 AI00 3:00 P.M. Grammer Physics 551 Psychology 617 AllO 11:00 A.M. Rosen Computer Science 518 Zoology 335 A 100 9:00A.M. Rosen Mathematics 575 Zoology 412 A 100 8:00A.M. 8-/7 unkS� EXAMPLE 12 TABLE 7 Teaching_schedule. 8.2 n-ary Relations and Their Applications 535 Professor Department Course..number Room Time Cruz Zoology 335 A 100 9:00A.M. Cruz Zoology 412 A 100 8:00 A.M. Farber Psychology 501 A 100 3:00 P.M. Farber Psychology 617 AI10 11:00 A.M. Grammer Physics 544 B505 4:00P.M. Rosen Computer Science 518 N521 2:00 P.M. Rosen Mathematics 575 N502 3:00 P.M. SQL The database query language SQL (short for structured query language) can be used to carry out the operations we have described in this section. Example 12 illustrates how SQL commands are related to operations on n-ary relations. We will illustrate how SQL is used to express queries by showing how SQL can be employed to make a query about airline flights using Table 8. The SQL statement SELECT Departure_time FROM Flights WHERE Destination= 'Detroit , is used to find the projection P5 (on the Departure_time attribute) of the selection of 5-tuples in the Flights database that satisfy the condition: Destination = 'Detroit'. The output would be a list containing the times of flights that have Detroit as their destination, namely, 08: 10,08:47, and 09:44. SQL uses the FROM clause to identify the n-ary relation the query is applied to, the WHERE clause to specify the condition of the selection operation, and the SELECT clause to specify the projection operation that is to be applied. (Beware: SQL uses SELECT to represent a projection, rather than a selection operation. This is an unfortunate example of conflicting terminology.) .... Example 13 shows how SQL queries can be made involving more than one table. TABLE 8 Flights. Airline Flight..number Gate Destination Departure..time Nadir 122 34 Detroit 08:10 Acme 221 22 Denver 08:17 Acme 122 33 Anchorage 08:22 Acme 323 34 Honolulu 08:30 Nadir 199 13 Detroit 08:47 Acme 222 22 Denver 09:10 Nadir 322 34 Detroit 09:44 536 8 / Relations EXAMPLE 13 The SQL statement SELECT Professor , Time 8-/8 FROM Teachin g_assig nments, Class schedule WHERE Depar tment= 'Mathematics , is used to find the projection P1,5 of the 5-tuples in the database (shown in Table 7), which is the join lz of the Teaching_assignments and Class_schedule databases in Tables 5 and 6, respective ly, which satisfy the condition: Department = Mathema tics. The output would consist ofthe single 2-tuple (Rosen, 3:00 P.M.). The SQL FROM clause is used here to find the join of two different databases. ... We have only touched on the basic concepts of relational databases in this section. More information can be found in [AhUI95]. Exercises 1. List the triples in the relation {(a, b, c) I a, b, and care integers with 0 < a < b < e < 5}. 2. Which 4-tuples are in the relation {(a, b, e, d) I a, b, e, and d are positive integers with abed = 6}? 3. List the 5-tuples in the relation in Table 8. 4. Assuming that no new n-tuples are added, find all the primary keys for the relations displayed in a) Table 3. b) Table 5. .c) Table 6. d) Table 8. 5. Assuming that no new n-tuples are added, find a compos­ ite key with two fields containing the Airline field for the database in Table 8. 6. Assuming that no new n-tuples are added, find a compos­ ite key with two fields containing the Professor field for the database in Table 7. 7. The 3-tuples in a 3-ary relation represent the following at­ tributes of a student database: student ID number, name, phone number. a) Is student ID number likely to be a primary key? b) Is name likely to be a primary key? c) Is phone number likely to be a primary key? 8. The 4-tuples in a 4-ary relation represent these attributes of published books: title, ISBN, publication date, number of pages. a) What is a likely primary key for this relation? b) Under what conditions would (title, publication date) be a composite key? c) Under what conditions would (title, number of pages) be a composite key? 9. The 5-tuples in a 5-ary relation represent these attributes of all people in the United States: name, Social Security number, street address, city, state. a) Determine a primary key for this relation. b) Under what conditions would (name, street address) be a composite key? c) Under what conditions would (name, street address, city) be a composite key? 10. What do you obtain when you apply the selection oper­ ator Sc, where C is the condition Room = AIOO, to the database in Table 7? 11. What do you obtain when you apply the selection operator Sc, where C is the condition Destination = Detroit, to the database in Table 8? 12. What do you obtain when you apply the selection operator Sc , where C is the condition (Project = 2) 1\ (Quantity ::: 50), to the database in Table 10 (found later in this exercise set)? 13. What do you obtain when you apply the selection oper­ ator Sc , where C is the condition (Airline = Nadir) v (Destination = Denver), to the database in Table 8? 14. What do you obtain when you apply the projection P2,3,5 to the 5-tuple (a, b, e, d, e)? 15. Which projection mapping is used to delete the first, second, and fourth components of a 6-tuple? 16. Display the table produced by applying the projection P1,2,4 to Table 8. 17. Display the table produced by applying the projection P1,4 to Table 8. 18. How many components are there in the n-tuples in the table obtained by applying the join operator h to two tables with 5-tuples and 8-tuples, respectively? 19. Construct the table obtained by applying the join operator fz to the relations in Tables 9 and 10. 20. Show that if C 1 and C 2 are conditions that elements of the n-ary relation R may satisfy, then SCI Ac,(R) = SCI (sc,(R». 21. Show that if C1 and C2 are conditions that elements of the n-ary relation R may satisfy, then SCI (Sc, (R» = sc,(sci (R». 22. Show that if C is a condition that elements of the n­ ary relations Rand S may satisfy, then sc(R U S) = sc(R) U sc(S). 23. Show that if C is a condition that elements of the n­ ary relations Rand S may satisfy, then sc(R n S) = sc(R) n sc(S). 8-/9 8.3 Representing Relations 537 TABLE 9 ParLneeds. TABLE 10 Parts-.inventory. Supplier Part_number Project Part..number Project Quantity Color_code 23 1092 I 23 1101 3 23 9048 4 31 4975 3 31 3477 2 32 6984 4 32 . 9191 2 33 1001 I 24. Show that if C is a condition that elements of the n­ ary relations Rand S may satisfy, then sc(R - S) = sc(R) -sc(S). 25. Show that if Rand S are both n-ary relations, then Pi,.i2 ..... im(R U S) = Pi,.i2 ..... im(R) U Pi,.i2 ..... im(S). 26. Give an example to show that if Rand S are both n-ary relations, then Pi .. i2 ..... im(R n S) may be different from Pi,.i2 ..... im(R) n Pi,.i2 ..... im(S). 27. Give an example to show that if R and S are both n-ary relations, then Pi,.i2 ..... im(R - S) may be different from Pi,.i2 ..... im(R) -Pi,.i2 ..... im(S). 28. a) What are the operations that correspond to the query expressed using this SQL statement? SELECT Supplier FROM Part_needs WHERE 1000 � Part_numb er � 5000 b) What is the output of this query given the database in Table 9 as input? 8.3 Repres enting Relations Introduction 1001 I 14 8 1092 I 2 2 1101 3 I I 3477 2 25 2 4975 3 6 2 6984 4 10 1 9048 4 12 2 9191 2 80 4 29. a) What are the operations that correspond to the query expressed using this SQL statement? SELECT Supplier , Project FROM Part_needs , Parts_inventory WHERE Quant ity � 10 b) What is the output of this query given the databases in Tables 9 and 10 as input? 30. Determine whether there is a primary key for the relation in Example 2. 31. Determine whether there is a primary key for the relation in Example 3. . 32. Show that an n-ary relation with a primary key can be thought of as the graph of a function that maps values of the primary key to (n -I)-tuples formed from values of the other domains. In this section, and in the remainder of this chapter, all relations we study will be binary relations. Because of this, in this section and in the rest of this chapter, the word relation will always refer to a binary relation. There are many ways to represent a relation between finite sets. As we have seen in Section 8.1, one way is to list its ordered pairs. Another way to represent a relation is to use a table, as we did in Example 3 in Section 8.1. In this section we will discuss two alternative methods for representing relations. One method uses zer()-{)ne matrices. The other method uses pictorial representations called directed graphs, which we will discuss later in this section. Generally, matrices are appropriate for the representation of relations in computer programs. On the other hand, people often find the representation of relations using directed graphs useful for understanding the properties of these relations. 538 8 / Relations 8-20 1 1 1 Representing Relations Using Matrices A relation between finite sets can be represented using a zero-one matrix. Suppose that R is a relation from A = {ai, a2, ... , am} to B = {bl, b2, ... , bn}. (Here the elements of the sets A and B have been listed in a particular, but arbitrary, order. Furthermore, when A = B we use the same ordering for A and B.) The relation R can be represented by the matrix MR = [mij], where In other words, the zero-one matrix representing R has a 1 as its (i, j) entry when ai is related to bj, and a 0 in this position if ai is not related to bj. (Such a representation depends on the orderings used for A and B .) The use of matrices to represent relations is illustrated in Examples 1-6. EXAMPLE 1 Suppose that A = {I, 2, 3} and B = {I, 2}. Let R be the relation from A to B containing (a, b) if a E A, b E B, and a > b. What is the matrix representing R if al = 1, a2 = 2, and a3 = 3, and bl = 1 and b2 = 2? Solution: Because R = {(2, 1), (3, 1), (3, 2)}, the matrix for R is MR = [: �l The Is in MR show that the pairs (2, 1), (3, 1), and (3, 2) belong to R. The Os show that no other pairs belong to R. ... EXAMPLE 2 Let A = {ai, a2, a3} and B = {bl, b2, b3, b4, b5}. Which ordered pairs are in the relation R represented by the matrix 1 1 [0 1 0 0 MR = 1 0 1 1 1 0 1 0 Solution: Because R consists ofthose ordered pairs (ai, bj) with mij = 1, it follows that R = {(ai, b2), (a2, bl)' (a2, b3), (a2, b4), (a3, bd, (a3, b3), (a3, b5)}. The matrix of a relation on a set, which is a square matrix, can be used to determine whether the relation has certain proper ties. Recall that a relation R on A is reflexive if( a, a) E R whenever a E A. Thus, R is reflexive if and only if(ai, ai) E R for i = 1,2, ... , n. Hence, R is reflexive if and only ifmii = 1, for i = 1,2, ... , n. In other words, R is reflexive if all the elements on the main diagonal ofMR are equal to 1, as shown in Figure 1. FIGURE 1 The Zero-One Matrix for a Reflexive Relation. The relation R is symmetric if (a, b) E R implies that (b, a) E R. Consequently, the relation R on the set A = {aI, a2, ... , an} is symmetric if and only if(aj, ai) E R whenever (ai, aj) E R. In terms of the entries ofMR, R is symmetric if and only if m ji = 1 whenever m ij = 1. This also means mji = 0 whenever mij = o. Consequently, R is symmetric if and only ifmij = mji, for all pairs of integers i and j with i = 1, 2, ... , n and j = 1, 2, ... , n. Recalling the definition of the transpose ofa matrix from Section 3.8, we see that R is symmetric if and only if 8-2! (a) Symmetric (b) Antisym metric FIGURE 2 The Zero-One Matrices for Symmetric and Antisymmetric Relations. 8.3 Representing Relations 539 that is, ifMR is a symmetric matrix. The form of the matrix for a symmetric relation is illustrated in Figure 2(a). The relation R is anti symmetric if and only if(a, b) E R and (b, a) E R imply that a = b. Consequently, the matrix of an anti symmetric relation has the property that if mij = I with i =I-j, then mji = O. Or, in other words, either mij = 0 or m ji = 0 when i =I-j. The form of the matrix for an anti symmetric relation is illustrated in Figure 2(b). EXAMPLE 3 Suppose that the relation R on a set is represented by the matrix 1 I I Is R reflexive, symmetric, and/or anti symmetric? Solution: Because all the diagonal elements ofthis matrix are equal to I, R is reflexive. Moreover, because MR is symmetri c, it follows that R is symmetri c. It is also easy to see that R is not anti symmetric . � The Boolean operations join and meet (discussed in Section 3.8) can be used to find the matrices representing the union and the intersection of two relations. Suppose that R\ and R2 are relations on a set A represented by the matrices MRI and MR2, respective ly. The matrix representing the union of these relations has a I in the positions where either MRI or MR2 has a 1. The matrix representing the intersection of these relations has a 1 in the positions where both MRI and MR2 have a 1. Thus, the matrices representing the union and intersection of these relations are and EXAMPLE 4 Suppose that the relations R\ and R2 on a set A are represented by the matrices o 1] o 0 I 0 and What are the matrices representing R\ U R2 and R\ n R2? 540 8 / Relations 8-22 Solution: The matrices of these relations are We now turn our attention to determining the matrix for the composite of relations. This matrix can be found using the Boolean product of the matrices (discussed in Section 3.8) for these relations. In particular, suppose that R is a relation from A to Band S is a relation from B to C. Suppose that A, B, and C have m, n, and p elements, respective ly. Let the zero--one matrices for S oR, R, and S be Ms oR = [tjj], MR = [rjj], and Ms = [Sjj], respectively (these matrices have sizes m x p, m x n, and n x p, respectively). The ordered pair (aj. Cj) belongs to So R if and only ifthere is an element bk such that (aj. bk) belongs to Rand (bk, Cj) belongs to S. It follows that tjj = 1 if and only if rjk = Skj = 1 for some k. From the definition of the Boolean product, this means that EXAMPLE 5 Find the matrix representing the relations S 0 R, where the matrices representing Rand S are MR = [i o '] [0 , n 1 0 and Ms= 0 0 o 0 1 0 Solution: The matrix for So R is [" 1J MSOR=MR0Ms= 01 o 0 .... The matrix representing the composite of two relations can be used to find the matrix for MR". In particular, MR" = M�l. from the definition of Boolean powers. Exercise 35 at the end of this section asks for a proof of this formula. EXAMPLE 6 Find the matrix representing the relation R 2, where the matrix representing R is Solution: The matrix for R2 is M 2 _M[21_ R -R-[! llJ 8-23 8.3 Representing Relations 541 Representing Relations Using Digraphs We have shown that a relation can be represented by listing all of its ordered pairs or by using a zero-one matrix. There is another important way of representing a relation using a pictorial representati on. Each element of the set is represented by a point, and each ordered pair is represented using an arc with its direction indicated by an arrow. We use such pictorial representations when we think of relations on a finite set as directed graphs, or digraphs. DEFINITIO N 1 A directed graph, or digraph, consists ofa set V of vertices (or nodes) together with a set E of ordered pairs of elements of V called edges (or arcs). The vertex a is called the initial vertex of the edge (a, b), and the vertex b is called the terminal vertex of this edge. a d An edge of the form (a, a) is represented using an arc from the vertex a back to itself. Such an edge is called a loop. EXAMPLE 7 The directed graph with vertices a, b, c, and d, and edges (a, b), (a, d), (b, b), (b, d), (c, a), b c (c, b), and (d, b) is displayed in Figure 3. .... FIGUR E 3 The relation R on a set A is represented by the directed graph that has the elements of A as its vertices and the ordered pairs (a, b), where (a, b) E R, as edges. This assignment sets up a one­ to-one correspondence between the relations on a set A and the directed graphs with A as their set of vertices. Thus, every statement about relations corresponds to a statement about directed graphs, and vice versa. Directed graphs give a visual display of information about relations. As such, they are often used to study relations and their properties. (Note that relations from a set A to a set B can be represented by a directed graph where there is a vertex for each element of A and a vertex for each element of B, as shown in Section 8.1. However, when A = B, such representation provides much less insight than the digraph representations described here.) The use of directed graphs to represent relations on a set is illustrated in Examples 8-10. A Directed Graph. EXAMPLE S The directed graph of the relation R = {(1, 1),(1,3),(2, 1),(2,3),(2,4),(3, 1),(3,2),(4, I)} on the set {I, 2, 3, 4} is shown in Figure 4. EXAMPLE 9 What are the ordered pairs in the relation R represented by the directed graph shown in Figure 5? Solution: The ordered pairs (x, y) in the relation are R = {(1, 3), (1, 4), (2,1), (2, 2), (2, 3), (3,1), (3, 3), (4,1), (4, 3)}. Each of these pairs corresponds to an edge of the directed graph, with (2, 2) and (3,3) corresponding to loops. .... The directed graph representing a relation can be used to determine whether the relation has various properties. For instance, a relation is reflexive if and only if there is a loop at every vertex of the directed graph, so that every ordered pair of the form (x, x) occurs in the relation. A relation is symmetric if and only if for every edge between distinct vertices in its digraph there is an edge in the opposite direction, so that (y, x) is in the relation whenever (x, y) is in the 542 8 / Relations 4 3 FIGURE 4 The Directed Graph of the Relation R. FIGURE 5 The Directed Graph of the Relation R. 8-24 (a) Directed graph of R (b) Directed graph of S FIGURE 6 The Directed Graphs ofthe Relations Rand S. relation. Similarly, a relation is anti symmetric if and only ifthere are never two edges in opposite directions between distinct vertices. Finally, a relation is transitive if and only if whenever there is an edge from a vertex x to a vertex y and an edge from a vertex y to a vertex z, there is an edge from x to z (completing a triangle where each side is a directed edge with the correct direction). Remark: Note that a symmetric relation can be represented by an undirected graph, which is a graph where edges do not have directions. We will study undirected graphs in Chapter 9. EXAMPLE 10 Determine whether the relations for the directed graphs shown in Figure 6 are reflexive, sym­ metric, anti symmetri c, and/or transitive. Solution: Because there are loops at every vertex of the directed graph of R, it is reflexive. R is neither symmetric nor anti symmetric because there is an edge from a to b but not one from b to a, but there are edges in both directions connecting b and c. Finally, R is not transitive because there is an edge from a to b and an edge from b to c, but no edge from a to c. Because loops are not present at all the vertices of the directed graph of S, this relation is not reflexive. It is symmetric and not anti symmetric, because every edge between distinct vertices is accompanied by an edge in the opposite direction. It is also not hard to see from the directed graph that S is not transitive, because (c, a) and (a, b) belong to S, but (c, b) does not belong to S. .... Exercises 1. Represent each ofthese relations on {I, 2, 3} with a matrix (with the elements ofthis set listed in increasing order). a) {(1, 1), (1, 2), (1, 3)} b) {(I, 2), (2,1), (2, 2), (3, 3)} c) {(I, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)} d) {(I, 3), (3, I)} 2. Represent each of these relations on {l, 2, 3, 4} with a matrix (with the elements of this set listed in increasing order). a) {(1, 2), (1,3), (1,4), (2, 3), (2, 4), (3, 4)} b) {(I, 1), (1, 4), (2, 2), (3, 3), (4, I)} c) {(I, 2), (1, 3), (1, 4), (2,1), (2, 3), (2, 4), (3,1), (3, 2), (3,4), (4, 1), (4, 2), (4, 3)} d) {(2, 4), (3, 1), (3, 2), (3, 4)} 3. List the ordered pairs in the relations on {I, 2, 3} corresponding to these matrices (where the rows and columns correspond to the integers listed in increasing order). a) [� ! �] 0) [! � !] 4. List the ordered pairs in the relations on {I, 2, 3, 4} corresponding to these matrices (where the rows and 11-25 columns correspond to the integers listed in increasing order). 5. How can the matrix representing a relation R on a set A be used to determine whether the relation is irreflexive? 6. How can the matrix representing a relation R on a set A be used to determine whether the relation is asymmetric? 7. Determine whether the relations represented by the ma­ trices in Exercise 3 are reflexive, irreflexive, symmetric, anti symmetric, and/or transitive. 8. Determine whether the relations represented by the ma­ trices in Exercise 4 are reflexive, irreflexive, symmetric, antisymmetric, and/or transitive. 9. How many nonzero entries does the matrix representing the relation R on A = {I, 2, 3, ... , 100} consisting of the first 100 positive integers have if R is a) {(a, b) I a > b}? b) {(a, b) I a =1= b}? c) {(a, b) I a = b + I}? d) {(a, b) I a = I}? e) {(a, b) lab = l}? 10. How many nonzero entries does the matrix representing the relation R on A = {I, 2, 3, ... , 1000} consisting of the first 1000 positive integers have if R is a) {(a, b) I a :'S b}? b) {(a, b) I a = b ± I}? c) {(a, b) I a + b = 1000}? d) {(a, b) I a + b :'S IOOI}? e) {(a, b) I a =1= O}? 11. How can the matrix for R, the complement ofthe relation R, be found from the matrix representing R, when R is a relation on a finite set A? 12. How can the matrix for R -I , the inverse of the relation R, be found from the matrix representing R, when R is a relation on a finite set A? 13. Let R be the relation represented by the matrix 1 1] 1 0 . o 1 Find the matrix representing 1 - a)R-. b)R. 8.3 Representing Relations 543 14. Let RI and R2 be relations on a set A represented by the matrices Find the matrices that represent a) RI U R2. b) RI n R2. d) R1oR1. e) R1tBR2. 15. Let R be the relation represented by the matrix [0 1 0] MR = 0 0 1 . 110 Find the matrices that represent a) R2. b) R3. 16. Let R be a relation on a set A with n elements. If there are k nonzero entries in MR, the matrix representing R, how many nonzero entries are there in MR-l, the matrix representing R-1, the inverse of R? 17. Let R be a relation on a set A with n elements. If there are k nonzero entries in MR, the matrix representing R, how many nonzero entries are there in MR, the matrix representing Ii, the complement of R? 18. Draw the directed graphs representing each of the rela­ tions from Exercise 1. 19. Draw the directed graphs representing each of the rela­ tions from Exercise 2. 20. Draw the directed graph representing each of the relations from Exercise 3. 21. Draw the directed graph representing each ofthe relations from Exercise 4. 22. Draw the directed graph that represents the relation {(a, a), (a, b), (b, c), (c, b), (c, d), (d, a), (d, b)}. In Exercises 23-28 list the ordered pairs in the relations rep­ resented by the directed graphs. 23. 24. a A bg , 25. 26. a b a b D c d c d 544 8 / Relations 27. a b c 28. 8-26 32. Determine whether the relations represented by the di­ rected graphs shown in Exercises 26-28 are reflexive, ir­ reflexive, symmetric, anti symmetric, asymmetric, and/or transitive. 33. Let R be a relation on a set A. Explain how to use the di­ rected graph representing R to obtain the directed graph representing the inverse relation R-'. 29. How can the directed graph of a relation R on a finite set A be used to determine whether a relation is asymmetric? 34. Let R be a relation on a set A. Explain how to use the di­ rected graph representing R to obtain the directed graph representing the complemen tary relation R. 35. Show that ifMR is the matrix representing the relation R, then M�l is the matrix representing the relation Rn. 30. How can the directed graph of a relation R on a finite set A be used to determine whether a relation is irreflexive? 31. Determine whether the relations represented by the di­ rected graphs shown in Exercises 23-25 are reflexive, ir­ reflexive, symmetric, anti symmetric, and/or transitive. 8.4 Closures of Relations Introduction 36. Given the directed graphs representing two relations, how can the directed graph of the union, intersection, sym­ metric difference, difference, and composition of these relations be found? A computer network has data centers in Boston, Chicago, Denver, Detroit, New York, and San Diego. There are direct, one-way telephone lines from Boston to Chicago, from Boston to Detroit, from Chicago to Detroit, from Detroit to Denver, and from New York to San Diego. Let R be the relation containing (a, b) ifthere is a telephone line from the data center in a to that in b. How can we determine if there is some (possibly indirect) link composed of one or more telephone lines from one center to another? Because not all links are direct, such as the link from Boston to Denver that goes through Detroit, R cannot be used directly to answer this. In the language of relations, R is not transitive, so it does not contain all the pairs that can be linked. As we will show in this section, we can find all pairs of data centers that have a link by constructing a transitive relation S containing R such that S is a subset of every transitive relation containing R. Here, S is the smallest transitive relation that contains R. This relation is called the transitive closure of R. In general, let R be a relation on a set A. R mayor may not have some property P, such as reflexivity, symmetry, or transitivit y. If there is a relation S with property P containing R such that S is a subset of every relation with property P containing R, then S is called the closure of R with respect to P. (Note that the closure of a relation with respect to a property may not exist; see Exercises 15 and 35 at the end of this section .) We will show how reflexive, symmetri c, and transitive closures of relations can be found. Closures The relation R = {(I, 1), (1, 2), (2,1), (3, 2)} on the set A = {l, 2, 3} is not reflexive. How can we produce a reflexive relation containing R that is as small as possible? This can be done by adding (2,2) and (3, 3) to R, because these are the only pairs of the form (a, a) that are not in R. Clearly, this new relation contains R. Furthermore, any reflexive relation that contains R must also contain (2,2) and (3,3). Because this relation contains R, is reflexive, and is contained within every reflexive relation that contains R, it is called the reflexive closure of R. As this example illustrates, given a relation R on a set A, the reflexive closure of R can be formed by adding to R all pairs of the form (a, a) with a E A, not already in R. The 8-27 8.4 Closures of Relations 545 addition of these pairs produces a new relation that is reflexive, contains R, and is con­ tained within any reflexive relation containing R. We see that the reflexive closure of R equals R U /),., where /),. = {(a, a) I a E A} is the diagonal relation on A. (The reader should verify this.) EXAMPLE 1 What is the reflexive closure of the relation R = {(a, b) I a < b} on the set of integers? EXAMPLE 2 Ex1ra� Examples � Solution: The reflexive closure of R is R U /),. = {(a, b) I a < b} U {(a, a) I a E Z} = {(a, b) I a S b}. The relation {(I, I), (1, 2), (2, 2), (2, 3), (3, I), (3, 2)} on {l, 2, 3} is not symmetri c. How can we produce a symmetric relation that is as small as possible and contains R? To do this, we need only add (2, I) and (1, 3), because these are the only pairs of the form (b, a) with (a, b) E R that are not in R. This new relation is symmetric and contains R. Furthermore, any symmetric relation that contains R must contain this new relation, because a symmetric relation that contains R must contain (2, 1) and (1,3). Consequently, this new relation is called the symmetric closure of R. As this example illustrates, the symmetric closure of a relation R can be constructed by adding all ordered pairs of the form (b, a), where (a, b) is in the relation, that are not already present in R. Adding these pairs produces a relation that is symmetric, that contains R, and that is contained in any symmetric relation that contains R. The symmetric closure of a relation can be constructed by taking the union of a relation with its inverse (defined in the preamble of Exercise 24 in Section 8.1); that is, R U R-1 is the symmetric closure of R, where R-1 = feb, a) I (a, b) E R}. The reader should verify this statement. What is the symmetric closure of the relation R = {(a, b) I a > b} on the set of positive integers? Solution: The symmetric closure of R is the relation R U R-1 = {(a, b) I a > b} U {(b, a) I a > b} = {(a, b) I a # b}. This last equality follows because R contains all ordered pairs of positive integers where the first element is greater than the second element and R -1 contains all ordered pairs of positive integers where the first element is less than the second. ..... Suppose that a relation R is not transitive. How can we produce a transitive relation that contains R such that this new relation is contained within any transitive relation that contains R? Can the transitive closure of a relation R be produced by adding all the pairs of the form (a, c), where (a, b) and (b, c) are already in the relation? Consider the relation R = {(1, 3), (1, 4), (2, I), (3, 2)} on the set {I, 2, 3, 4}. This relation is not transitive because it does not contain all pairs ofthe form (a, c) where (a, b) and (b, c) are in R. The pairs of this form not in Rare (1,2), (2, 3), (2,4), and (3, I). Adding these pairs does not produce a transitive relation, because the resulting relation contains (3, I) and (I, 4) but does not contain (3, 4). This shows that constructing the transitive closure of a relation is more complicated than construct­ ing either the reflexive or symmetric closure. The rest of this section develops algorithms for constructing transitive closures. As will be shown later in this section, the transitive closure of a relation can be found by adding new ordered pairs that must be present and then repeating this process until no new ordered pairs are needed. 546 8 / Relations DEFINITIO N 1 8-28 Paths in Directed Graphs We will see that representing relations by directed graphs helps in the construction of transitive closures. We now introduce some terminology that we will use for this purpose. A path in a directed graph is obtained by traversing along edges (in the same direction as indicated by the arrow on the edge). A path from a to b in the directed graph G is a sequence of edges (xo, Xl), (Xl, X2), (X2' X3), ... , (Xn-l, xn) in G, where n is a nonnegative integer, and Xo = a and Xn = b, that is, a sequence of edges where the terminal vertex of an edge is the same as the initial vertex in the next edge in the path. This path is denoted by xo, Xl, X2, ... ,Xn-l, Xn and has length n. We view the empty set of edges as a path from a to a. A path oflength n � 1 that begins and ends at the same vertex is called a circuit or cycle. A path in a directed graph can pass through a vertex more than once. Moreover, an edge in a directed graph can occur more than once in a path. EXAMPLE 3 Which of the following are paths in the directed graph shown in Figure 1: a, b, e, d; a, e, c, d, b; b, a, c, b, a, a, b; d, c; c, b, a; e, b, a, b, a, b, e? What are the lengths of those that are paths? Which of the paths in this list are circuits? Solution: Because each of (a, b), (b, e), and (e, d) is an edge, a, b, e, d is a path oflength three. Because (c, d) is not an edge, a, e, c, d, b is not a path. Also, b, a, c, b, a, a, b is a path of length six because (b, a), (a, c), (c, b), (b, a), (a, a), and (a, b) are all edges. We see that d, c is a path oflength one, because (d, c) is an edge. Also c, b, a is a path of length two, because (c, b) and (b, a) are edges. All of (e, b), (b, a), (a, b), (b, a), (a, b), and (b, e) are edges, so e, b, a, b, a, b, e is a path oflength six. The two paths b, a, c, b, a, a, b and e, b, a, b, a, b, e are circuits because they begin and end at the same vertex. The paths a, b, e, d; c, b, a; and d, c are not circuits. <l1li The term path also applies to relations. Carrying over the definition from directed graphs to relations, there is a path from a to b in R if there is a sequence of elements a, Xl , X2, ... , Xn-l , b with (a, Xl) E R, (Xl, X2) E R, ... , and (Xn-l, b) E R. Theorem 1 can be obtained from the definition of a path in a relation. c FIGURE 1 A Directed Graph. 8-29 THEOREM l 8.4 Closures of Relations 547 Let R be a relation on a set A. There is a path oflength n, where n is a positive integer, from a to b if and only if(a, b) ERn. Proof: We will use mathematical induction. By definition, there is a path from a to b of length one if and only if (a, b) E R, so the theorem is true when n = 1. Assume that the theorem is true for the positive integer n. This is the inductive hypothesis. There is a path of length n + 1 from a to b if and only if there is an element c E A such that there is a path oflength one from a to c, so (a, c) E R, and a path oflength n from c to b, that is, (c, b) E Rn. Consequently, by the induction hypothesis, there is a path of length n + 1 from a to b if and only if there is an element c with (a, c) E R and (c, b) E Rn. But there is such an element if and only if (a, b) E Rn+ I. Therefore, there is a path of length n + 1 from a to b if and only if(a, b) E Rn+l. This completes the proof. <I Transitive Closures We now show that finding the transitive closure of a relation is equivalent to determining which pairs of vertices in the associated directed graph are connected by a path. With this in mind, we define a new relation. DEFINITION 2 Let R be a relation on a set A. The connectivity relation R* consists of the pairs (a, b) such that there is a path oflength at least one from a to b in R. Because Rn consists ofthe pairs (a, b) such that there is a path oflength n from a to b, it follows that R* is the union of all the sets Rn. In other words, 00 R* = URn. n=1 The connectivity relation is useful in many models. EXAMPLE 4 Let R be the relation on the set of all people in the world that contains (a, b) if a has met b. What is Rn, where n is a positive integer greater than one? What is R*? Solution: The relation R2 contains (a, b) if there is a personc such that (a, c) E R and(c, b) E R, that is, if there is a person c such that a has met c and c has met b. Similarly, Rn consists of those pairs (a, b) such that there are people XI, X2, ..• , Xn-I such that a has met XI, XI has met X2, •.• , and Xn-I has met b. The relation R* contains (a, b) if there is a sequence of people, starting with a and ending with b, such that each person in the sequence has met the next person in the sequence. (There are many interesting conjectures about R*. Do you think that this connectivity relation includes the pair with you as the first element and the president of Mongolia as the second element? We will use graphs to model this application in Chapter 9.) .... EXAMPLE 5 Let R be the relation on the set of all subway stops in New York City that contains (a, b) ifit is possible to travel from stop a to stop b without changing trains. What is Rn when n is a positive integer? What is R*? 548 8 / Relations 8-30 Solution: The relation Rn contains (a, b) if it is possible to travel from stop a to stop b by making n -1 changes of trains. The relation R* consists of the ordered pairs (a, b) where it is possible to travel from stop a to stop b making as many changes of trains as necessary. (The reader should verify these statemen ts.) .... EXAMPLE 6 Let R be the relation on the set of all states in the United States that contains (a, b) if state a and state b have a common border. What is R n, where n is a positive integer? What is R*? THEOREM 2 LEMMA l Solution: The relation Rn consists ofthe pairs (a, b), where it is possible to go from state a to state b by crossing exactly n state borders. R* consists of the ordered pairs (a, b), where it is possible to go from state a to state b crossing as many borders as necessary. (The reader should verify these statemen ts.) The only ordered pairs not in R* are those containing states that are not connected to the continental United States (i.e., those pairs containing Alaska or Hawaii). .... Theorem 2 shows that the transitive closure of a relation and the associated connectivity relation are the same. The transitive closure of a relation R equals the connectivity relation R*. Proof: Note that R* contains R by definition. To show that R* is the transitive closure of R we must also show that R* is transitive and that R* � S whenever S is a transitive relation that contains R. First, we show that R* is transitive. If(a, b) E R* and (b, c) E R*, then there are paths from a to b and from b to c in R. We obtain a path from a to c by starting with the path from a to b and following it with the path from b to c. Hence, (a, c) E R*. It follows that R* is transitive. Now suppose that S is a transitive relation containing R. Because S is transitive, sn also is transitive (the reader should verify this) and s n � S (by Theorem 1 of Section 8.1). Furthermore, because and Sk � S, it follows that S* � S. Now note that if R � S, then R* � S*, because any path in R is also a path in S. Consequently, R* � S* � S. Thus, any transitive relation that contains R must also contain R*. Therefore, R* is the transitive closure of R. <I Now that we know that the transitive closure equals the connectivity relation, we tum our attention to the problem of computing this relation. We do not need to examine arbitrarily long paths to determine whether there is a path between two vertices in a finite directed graph. As Lemma 1 shows, it is sufficient to examine paths containing no more than n edges, where n is the number of elements in the set. Let A be a set with n elements, and let R be a relation on A. If there is a path of length at least one in R from a to b, then there is such a path with length not exceeding n. Moreover, when a # b, if there is a path oflength at least one in R from a to b, then there is such a path with length not exceeding n -1. 8-31 THEOREM 3 ...... . .. a=xo Xl X2 --.. ......... -�---+-- ... ----.. Xm= b FIGURE 2 Producing a Path with Length Not Exceeding n. 8.4 Closures of Relations 549 Proof: Suppose there is a path from a to b in R. Let m be the length of the shortest such path. Suppose that xo, XI, X2, ••• ,Xm-I, Xm, where Xo = a and Xm = b, is such a path. Suppose that a = b and that m > n, so that m :::: n + 1. By the pigeonhole principle, because there are n vertices in A, among the m vertices xo, XI, ..• , Xm-I, at least two are equal (see Figure 2). Suppose that Xi = X j with 0 .:::: i < j .:::: m -1. Then the path contains a circuit from Xi to itself. This circuit can be deleted from the path from a to b, leaving a path, namely, xo, X I, ••. , Xi, X j +I, ... , Xm _I , Xm, from a to b of shorter length. Hence, the path of shortest length must have length less than or equal to n. The case where a #-b is left as an exercise for the reader. <l From Lemma 1, we see that the transitive closure of R is the union of R, R2, R3, ••• , and Rn. This follows because there is a path in R* between two vertices if and only if there is a path between these vertices in Ri, for some positive integer i with i .:::: n. Because and the zero-Dne matrix representing a union of relations is the join of the zero-Dne matrices of these relations, the zero-Dne matrix for the transitive closure is the join of the zero-Dnc matrices of the first n powers of the zero-Dne matrix of R. Let MR be the zero-Dne matrix of the relation R on a set with n elements. Then the zero-Dne matrix of the transitive closure R* is EXAMPLE 7 Find the zero-Dne matrix of the transitive closure of the relation R where [1 0 1] MR = 0 1 0 . 1 1 0 Solution: From Theorem 3, it follows that the zero-Dne matrix of R* is 550 8 / Relations linkS� linkS� Because M�l � [� it follows that MR' � [� �] and o 1] [I 1 0 v 0 1 0 1 8-32 M[31_ R -[� �l �] V [� �] � [� �l Theorem 3 can be used as a basis for an algorithm for computing the matrix of the relation R *. To find this matrix, the successive Boolean powers ofMR, up to the nth power, are computed. As each power is calculated, its join with the join of all smaller powers is formed. When this is done with the nth power, the matrix for R* has been found. This procedure is displayed as Algorithm 1. ALGORITHM 1 A Procedure for Computing the Transitive Closure. procedure transitive closure (MR : zenrone n x n matrix) A:=MR B:=A for i := 2 to n begin A:=A0MR B :=BvA end {B is the zero-one matrix for R*} We can easily find the number of bit operations used by Algorithm 1 to determine the transitive closure ofa relation. Computing the Boolean powers MR , M�l, ... , M�l requires that n -1 Boolean products of n x n zero-one matrices be found. Each of these Boolean products can be found using n2(2n -1) bit operations. Hence, these products can be computed using n2(2n -1)(n -1) bit operations. To find MR* from the n Boolean powers of MR, n -I joins of zero-one matrices need to be found. Computing each of these joins uses n2 bit operations. Hence, (n -1 )n2 bit operations are used in this part of the computation. Therefore, when Algorithm I is used, the matrix ofthe transitive closure of a relation on a set with n elements can be found using n2(2n -I )(n -I) + (n -l)n2 = 2n3(n -1), which is O(n4) bit operations. The remainder of this section describes a more efficient algorithm for finding transitive closures. W arshall' s Algorithm Warshall's algorithm, named after Stephen Warshall, who described this algorithm in 1960, is an efficient method for computing the transitive closure of a relation. Algorithm 1 can find the transitive closure of a relation on a set with n elements using 2n3(n -1) bit operations. However, the transitive closure can be found by Warshall's algorithm using only 2n3 bit operations. 8-33 a 8.4 Closures of Relations 551 Remark: Warshall's algorithm is sometimes called the Roy-Warshall algorithm, because Bernard Roy described this algorithm in 1959. Suppose that R is a relation on a set with n elements. Let VI, V2, •.• , Vn be an arbitrary listing of these n elements. The concept of the interior vertices of a path is used in Warshall's algorithm. If a, XI, X2, ••• , Xm-I, b is a path, its interior vertices are XI, X2, ••• , Xm-I, that is, all the vertices of the path that occur somewhere other than as the first and last vertices in the path. For instance, the interior vertices of a path a, c, d, f, g, h, b, J in a directed graph are c, d, f, g, h, and b. The interior vertices of a, c, d, a, f, bare c, d, a, and f. (Note that the first vertex in the path is not an interior vertex unless it is visited again by the path, except as the last vertex. Similarly, the last vertex in the path is not an interior vertex unless it was visited previously by the path, except as the first vertex.) Warshall's algorithm is based on the construction ofa sequence ofzero-one matrices. These matrices are Wo, WI, ... , W n, where W 0 = MR is the zero-one matrix of this relation, and W k = [w�5)], where w�? = 1 if there is a path from Vi to V j such that all the interior vertices of this path are in the set {VI, V2, ••• , vd (the first k vertices in the list) and is 0 otherwise. (The first and last vertices in the path may be outside the set of the first k vertices in the list.) Note that W n = MR', because the (i, J)th entry of MR' is 1 if and only ifthere is a path from Vi to V j, with all interior vertices in the set {VI, V2, ••• , vn} (but these are the only vertices in the directed graph). Example 8 illustrates what the matrix W k represents. EXAMPLE 8 Let R be the relation with directed graph shown in Figure 3. Let a, b, c, d be a listing of the elements of the set. Find the matrices Wo, WI, W2, W3, and W4. The matrix W4 is the transitive b closure of R. Solution: Let VI = a, V2 = b, V3 = c, and V4 = d. Wo is the matrix of the relation. Hence, FIGURE 3 The Directed Graph of the Relation R. WI has 1 as its (i, J)th entry if there is a path from Vi to Vj that has only VI = a as an interior vertex. Note that all paths oflength one can still be used because they have no interior vertices. STEPHE N WARS HALL (BORN 1935) Stephen Warshall, born in New York City, went to public school in Brooklyn. He attended Harvard University, receiving his degree in mathematics in 1956. He never received an advanced degree, because at that time no programs were available in his areas of interest. However, he took graduate courses at several different universities and contributed to the development of computer science and software engineering. After graduating from Harvard, Warshall worked at ORO (Operation Research Office), which was set up by Johns Hopkins to do research and development for the U.S. Army. In 1958 he left ORO to take a position at a company called Technical Operations, where he helped build a research and development labo­ ratory for military software projects. In 1961 he left Technical Operations to found Massachusetts Computer Associates. Later, this company became part of Applied Data Research (ADR). After the merger, Warshall sat on the board of directors of ADR and managed a variety of projects and organizations. He retired from ADR in 1982. During his career Warshall carried out research and development in operating systems, compiler design, language design, and operations research. In the 1971-1972 academic year he presented lectures on software engineering at French universities. There is an interesting anecdote about his proofthat the transitive closure algorithm, now known as Warshall's algorithm, is correct. He and a colleague at Technical Operations bet a bottle of rum on who first could determine whether this algorithm always works. Warshall came up with his proof overnight, winning the bet and the rum, which he shared with the loser of the bet. Because Warshall did not like sitting at a desk, he did much of his creative work in unconventional places, such as on a sailboat in the Indian Ocean or in a Greek lemon orchard. 552 8 / Relations LEMMA 2 8-34 Also, there is now an allowable path from b to d, namely, b, a, d. Hence, W2 has 1 as its (i, j)th entry if there is a path from Vi to Vj that has only VI = a and/or V2 = b as its interior vertices, if any. Because there are no edges that have b as a terminal vertex, no new paths are obtained when we permit b to be an interior vertex. Hence, W 2 = WI. W3 has I as its (i, j)th entry if there is a path from Vi to Vj that has only VI = a, V2 = b, and/or V3 = C as its interior vertices, ifany. We now have paths from d to a, namely, d, c, a, and from d to d, namely, d, c, d. Hence, Finally, W4 has 1 as its (i, j)th entry if there is a path from Vi to Vj that has VI = a, V2 = b, V3 = c, and/or V4 = d as interior vertices, if any. Because these are all the vertices of the graph, this entry is 1 if and only if there is a path from Vi to Vj. Hence, This last matrix, W4, is the matrix of the transitive closure. Warshall's algorithm computes MR* by efficiently computing W 0 = MR, WI, W 2, ... , W n = MR*. This observation shows that we can compute W k directly from W k-I: There is a path from Vi to Vj with no vertices other than VI, V2, ••• , Vk as interior vertices if and only if either there is a path from Vi to Vj with its interior vertices among the first k -1 vertices in the list, or there are paths from Vi to Vk and from Vk to Vj that have interior vertices only among the first k -1 vertices in the list. That is, either a path from Vi to Vj already existed before Vk was permitted as an interior vertex, or allowing Vk as an interior vertex produces a path that goes from Vi to Vk and then from Vk to Vj. These two cases are shown in Figure 4. The first type of path exists if and only if wlJ-Il = 1, and the second type of path exists if and only ifboth Wl!-Il and w��-Il are 1. Hence, wlJl is 1 if and only if either witll is 1 or both wl!-Il and w��-Il are 1. This gives us Lemma 2. Let W k = [wiJl] be the zero-one matrix that has a 1 in its (i, j)th position if and only if there is a path from Vj to Vj with interior vertices from the set {VI, V2, .•• , Vk}. Then [kl [k-Il V ( [k-Il/\ [k-Il) wij = wij Wik wkj, whenever i, j, and k are positive integers not exceeding n. 8-35 Exercises Case I � � . .. . .. . •... .. . .. . All interior vertices in {v l' V2' ' , . , Vk-l} Vk � c'/x�� Vi All interior vertices Vj in {vl,v2"' " vk-l} FIGURE 4 Adding Uk to the Set of Allowable Interior Vertices. 8.4 Closures of Relations 553 Lemma 2 gives us the means to compute efficiently the matrices Wk. k = 1,2, ... , n. We display the pseudocode for Warshall's algorithm, using Lemma 2, as Algorithm 2. ALGORITHM 2 Warshall Algorithm. procedure Warshall (MR : n x n zero-one matrix) W:=MR for k : = I to n begin for i : = 1 to n begin for j : = 1 to n Wi} : = Wi} V (Wik A Wk}) end end {W = [Wi}] is MR,} The computational complexity of Warshall 's algorithm can easily be computed in terms of bit operations. To find the entry w�;l from the entries W�;-ll, w�Z-II, and W��-ll using Lemma 2 requires two bit operations. To find all n2 entries ofWk from those OfWk-l requires 2n2 bit operations. Because Warshall's algorithm begins with Wo = MR and computes the sequence of n zero-one matrices WI, W 2, •.. , W n = MR" the total number of bit operations used is n . 2n2 = 2n3. 1. Let R be the relation on the set {O, 1, 2, 3} containing the ordered pairs (0, 1), (I, I), (l, 2), (2, 0), (2, 2), and (3, 0). Find the 3. Let R be the relation {(a, b) I a divides b} on the set of integers. What is the symmetric closure of R? a) reflexive closure of R. b) symmetric closure of R. 2. Let R be the relation {(a, b) I a i= b} on the set of integers. What is the reflexive closure of R? 4. How can the directed graph representing the reflexive closure of a relation on a finite set be constructed from the directed graph of the relation? 554 8 / Relations In Exercises 5-7 draw the directed graph of the reflexive closure of the relations with the directed graph shown. 5. 7. c d 6. c • d 8. How can the directed graph representing the symmetric closure of a relation on a finite set be constructed from the directed graph for this relation? 9. Find the directed graphs of the symmetric closures of the relations with directed graphs shown in Exercises 5-7. 10. Find the smallest relation containing the relation in Ex­ ample 2 that is both reflexive and symmetric. 11. Find the directed graph ofthe smallest relation that is both reflexive and symmetric for each of the relations with di­ rected graphs shown in Exercises 5-7. 12. Suppose that the relation R on the finite set A is rep­ resented by the matrix MR' Show that the matrix that represents the reflexive closure of R is MR V In. 13. Suppose that the relation R on the finite set A is rep­ resented by the matrix MR' Show that the matrix that represents the symmetric closure of R is MR V M�. 14. Show that the closure of a relation R with respect to a property P, if it exists, is the intersection of all the rela­ tions with property P that contain R. 15. When is it possible to define the "irreflexive closure" of a relation R, that is, a relation that contains R, is ir­ reflexive, and is contained in every irreflexive relation that contains R? 16. Determine whether these sequences of vertices are paths in this directed graph. a b a) a, b, c, e b) b,e,c,b,e c) a,a,b,e, d,e d) b,c,e, d,a,a,b e) b,c,c,b,e,d,e,d Q a,a, b, b,c,c,b,e, d 8-36 17. Find all circuits of length three in the directed graph in Exercise 16. 18. Determine whether there is a path in the directed graph in Exercise 16 beginning at the first vertex given and ending at the second vertex given. a) a,b b) b,a d) a,e e) b,d g) d,d h) e,a c) b, b Q c,d i) e, c 19. Let R be the relation on the set {I, 2, 3, 4, 5} containing the ordered pairs (1,3), (2, 4), (3,1), (3, 5), (4, 3), (5, I), (5, 2), and (5, 4). Find a) R2. b) R3. d) R5. e) R6. 20. Let R be the relation that contains the pair (a, b) if a and b are cities such that there is a direct non-stop airline flight from a to b. When is (a, b) in a) R2? b) R3? c) R*? 21. Let R be the relation on the set of all students contain­ ing the ordered pair (a, b) if a and b are in at least one common class and a i= b. When is (a, b) in a) R2? b) R3? c) R*? 22. Suppose that the relation R is reflexive. Show that R* is reflexive. 23. Suppose that the relation R is symmetri c. Show that R* is symmetri c. 24. Suppose that the relation R is irreflexive. Is the relation R2 necessarily irreflexive? 25. Use Algorithm 1 to find the transitive closures of these relations on {I, 2, 3, 4}. a) {(I, 2), (2,1), (2,3), (3,4), (4, I)} b) {(2, 1), (2,3), (3, I), (3,4), (4, I), (4, 3)} c) {(I, 2), (1,3), (1,4),(2,3), (2,4�(3,4)} d) {(I, I), (1,4), (2, I), (2,3), (3, I), (3, 2), (3,4), (4, 2)} 26. Use Algorithm 1 to find the transitive closures of these relations on {a, b, c, d, e}. a) {(a, c), (b, d), (c, a), (d, b), (e, d)} b) {(b, c), (b, e), (c, e), (d, a), (e, b), (e, c)} c) {(a, b), (a, c), (a, e), (b, a), (b, c), (c, a), (c, b), (d, a), (e, d)} d) {(a, e), (b, a), (b, d), (c, d), (d, a), (d, c), (e, a), (e, b), (e, c), (e, e)} 27. Use Warshall's algorithm to find the transitive closures of the relations in Exercise 25. 28. Use Warshall's algorithm to find the transitive closures of the relations in Exercise 26. 29. Find the smallest relation containing the relation {(I, 2), (I, 4), (3, 3), (4, I)} that is a) reflexive and transitive. 8-37 8.S Equivalence Relations 555 b) symmetric and transitive. in a path to find the length of the shortest path between two vertices in a directed graph, if such a path exists. c) reflexive, symmetric, and transitive. 30. Finish the proof of the case when a # b in Lemma 1. 33. Adapt Algorithm I to find the reflexive closure of the transitive closure of a relation on a set with n elements. 31. Algorithms have been devised that use O(n2.8) bit op­ erations to compute the Boolean product of two n x n zero--one matrices. Assuming that these algorithms can be used, give big-O estimates for the number of bit oper­ ations using Algorithm I and using Warshall's algorithm to find the transitive closure of a relation on a set with n elements. 34. Adapt Warshall's algorithm to find the reflexive closure of the transitive closure of a relation on a set with n elements. 35. Show that the closure with respect to the property P of the relation R = {CO, 0), (0, 1), (1, 1), (2, 2») on the set {O, 1, 2) does not exist ifP is the property a) "is not reflexive." :32. Devise an algorithm using the concept of interior vertices b) "has an odd number of elements." 8.5 Equivalence Relations unkS� DEFINITION 1 Introduction In some programming languages the names of variables can contain an unlimited number of characters. However, there is a limit on the number of characters that are checked when a compiler determines whether two variables are equal. For instance, in traditional C, only the first eight characters of a variable name are checked by the compiler. (These characters are uppercase or lowercase letters, digit�, or underscor es.) Consequently, the compiler considers strings longer than eight characters that agree in their first eight characters the same. Let R be the relation on the set of strings of characters such that s R t, where sand t are two strings, if s and t are at least eight characters long and the first eight characters of s and t agree, or s = t. It is easy to see that R is reflexive, symmetric, and transitive. Moreover, R divides the set of all strings into classes, where all strings in a particular class are considered the same by a compiler for traditional C. The integers a and b are related by the "congruence modulo 4" relation when 4 divides a-b. We will show later that this relation is reflexive, symmetric, and transitive. It is not hard to see that a is related to b if and only if a and b have the same remainder when divided by 4. It follows that this relation splits the set of integer& into four different classes. When we care only what remainder an integer leaves when it is divi4ed by 4, we need only know which class it is in, not its particular value. These two relations, R and congruence modulo 4, are examples of equivalence relations, namely, relations that are reflexive, symmetric, and transitive. In this section we will show that such relations split sets into disjoint classes of equivalent elemen ts. Equivalence relations arise whenever we care only whether an element of a set is in a certain class of elements, instead of caring about its particular identity. Equivalence Relations In this section we will study relations with a particular combination of properties that allows them to be used to relate objects that are similar in some way. A relation on a set A is called an equivalence relation if it is reflexive, symmetric, and transitive. . 556 8 / Relations DEFINITION 2 EXAMPLE 1 EXAMPLE 2 8-38 Equivalence relations are important throughout mathema tics and computer science. One reason for this is that in an equivalence relation, when two elements are related it makes sense to say they are equivalent. Two elements a and b that are related by an equivalence relation are called equivalent. The notation a "-' b is often used to denote that a and b are equivalent elements with respect to a particular equivalence relation. For the notion of equivalent elements to make sense, every element should be equivalent to itself, as the reflexive property guarantees for an equivalence relation. It makes sense to say that a and b are related (not just that a is related to b) by an equivalence relation, because when a is related to b, by the symmetric property, b is related to a. Furthermore, because an equivalence relation is transitive, if a and b are equivalent and band c are equivalent, it follows that a and c are equivalent. Examples 1-5 illustrate the notion of an equivalence relation. Let R be the relation on the set of integers such that aR b if and only if a = b or a = -b. In Section 8.1 we showed that R is reflexive, symmetric, and transitive. It follows that R is an equivalence relation. ... Let R be the relation on the set of real numbers such that aRb if and only if a - b is an integer. Is R an equivalence relation? Solution: Because a -a = 0 is an integer for all real numbers a, aRa for all real numbers a. Hence, R is reflexive. Now suppose that aRb. Then a - b is an integer, so b -a is also an integer. Hence, bRa. It follows that R is symmetri c. If aRb and b R c, then a -band b -c are integers. Therefore, a -c = (a -b) + (b - c) is also an integer. Hence, aRc. Thus, R is transitive. Consequently, R is an equivalence relation. ... One of the most widely used equivalence relations is congruence modulo m, where m is a positive integer greater than 1. EXAMPLE 3 Congruence Modulo m Let m be a positive integer with m > 1. Show that the relation R = {(a, b) I a == b (modm)} is an equivalence relation on the set of integers. Solution: Recall from Section 3.4 that a == b (mod m) if and only if m divides a-b. Note that a - a = 0 is divisible by m, because 0 = o· m. Hence, a == a (mod m), so congruence modulo m is reflexive. Now suppose that a == b (mod m). Then a - b is divisible by m, so a - b = km, where k is an integer. It follows that b -a = (-k)m, so b == a (mod m). Hence, congruence modulo m is symmetri c. Next, suppose that a == b (mod m) and b == c (mod m). Then m divides both a - b and b - c. Therefore, there are integers k and I with a - b = km and b -c = 1m. Adding these two equations shows that a -c = (a -b) + (b - c) = km + 1m = (k + I)m . Thus, a == c (mod m). Therefore, congruence modulo m is transitive. It follows that congruence modulo m is an equivalence relation. ... 8-39 8.5 Equivalence Relations 557 EXAMPLE 4 Suppose that R is the relation on the set of strings of English letters such that aRb if and only ifl(a) = l(b), where l(x) is the length of the string x. Is R an equivalence relation? Solution: Because l(a) = l(a), it follows that aRa whenever a is a string, so that R is reflexive. Next, suppose that aRb, so that l(a) = l(b). Then bRa, because l(b) = l(a). Hence, R is sym­ metric. Finally, suppose that aRb andbRc. Then l(a) = l(b) andl(b) = l(c). Hence, l(a) = l(c), so aRc. Consequently, R is transitive. Because R is reflexive, symmetri c, and transitive, it is an equivalence relation. <41 EXAMPLE 5 Let n be a positive integer and S a set of strings. Suppose that Rn is the relation on S such that s Rn t if and only if s = t, or both s and t have at least n characters and the first n characters of s and t are the same. That is, a string of fewer than n characters is related only to itself; a string s with at least n characters is related to a string t if and only if t has at least n characters and t begins with the n characters at the start of s. For example, let n = 3 and let S be the set of all bit strings. Then s R 3 t either when s = t or both sand t are bit strings of length 3 or more that begin with the same three bits. For instance, 01 R3 Oland 00111 R3 0010 1, but 01 R 3 0 10 and 01011 R3 01110. Show that for every set S of strings and every positive integer n, Rn is an equivalence relation on S. Solution: The relation Rn is reflexive because s = s, so that s Rn s whenever s is a string in S. If s Rn t, then either s = t or s and t are both at least n characters long that begin with the same n characters. This means that t Rn s. We conclude that Rn is symmetri c. Now suppose that s Rn t and t Rn u. Then either s = t or sand t are at least n characters long and sand t begin with the same n characters, and either t = u or t and u are at least n characters long and t and u begin with the same n characters. From this, we can deduce that either s = u or both sand u are n characters long and s and u begin with the same n characters (because in this case we know that s, t, and u are all at least n characters long and both s and u begin with the same n characters that t does). Consequently, Rn is transitive. It follows that Rn is an equivalence relation. <41 In Examples 6 and 7 we look at two relations that are not equivalence relations. EXAMPLE 6 Show that the "divides" relation on the set of positive integers in not an equivalence relation. Solution: By Examples 9 and 15 in Section 8.1, we know that the "divides" relation is reflexive and transitive. However, by Example 12 in Section 8.1, we know that this relation is not symmetric (for instance, 2 I 4 but 4 t 2). We conclude that the "divides" relation on the set of positive integers is not an equivalence relation. <41 EXAMPLE 7 Let R be the relation on the set of real numbers such that x R y if and only if x and yare real numbers that differ by less than 1, that is Ix -y I < 1. Show that R is not an equivalence relation. Solution: R is reflexive because Ix - x I = 0 < 1 whenever x E R. R is symmetric, for if x R y, where x and yare real numbers, then Ix - y I < 1, which tells us that Iy - x I = Ix - y I < 1, so that yRx. However, R is not an equivalence relation because it is not transitive. Take x = 2.8, y = 1.9, and z = 1.1, so that Ix -yl = 12.8 -1.91 = 0.9 < 1, Iy -zl = 11.9 -1.11 = 0.8 < 1, but Ix -zl = 12.8 -1.11 = 1.7 > 1. That is, 2.8 R 1.9, 1.9 R 1.1, but 2.8 R 1.1. <41 558 8 / Relations DEFINITION 3 8-40 Equivalence Classes Let A be the set of all students in your school who graduated from high school. Consider the relation R on A that consists of all pairs (x, y), where x and y graduated from the same high school. Given a student x, we can form the set of all students equivalent to x with respect to R. This set consists of all students who graduated from the same high school as x did. This subset of A is called an equivalence class of the relation. Let R be an equivalence relation on a set A. The set of all elements that are related to an element a of A is called the equivalence class of a. The equivalence class of a with respect to R is denoted by [a]R. When only one relation is under consideration, we can delete the subscript R and write [a] for this equivalence class. In other words, if R is an equivalence relation on a set A, the equivalence class of the element a is [a]R = {s I (a, s) E R}. If bE [a]R, then b is called a representative of this equivalence class. Any element ofa class can be used as a representative of this class. That is, there is nothing special about the particular element chosen as the representative of the class. EXAMPLE 8 What is the equivalence class of an integer for the equivalence relation of Example I? Solution: Because an integer is equivalent to itself and its negative in this equivalence relation, it follows that [a] = {-a, a}. This set contains two distinct integers unless a = O. For instance, [7] = {-7, 7}, [-5] = {-5, 5}, and [0] = {OJ. <III EXAMPLE 9 What are the equivalence classes of 0 and 1 for congruence modulo 4? Solution: The equivalence class of 0 contains all integers a such that a == 0 (mod 4). The integers in this class are those divisible by 4. Hence, the equivalence class of 0 for this relation is [0] = { ... , -8, -4,0,4,8, ... }. The equivalence class of 1 contains all the integers a such that a == 1 (mod 4). The integers in this class are those that have a remainder of 1 when divided by 4. Hence, the equivalence class of 1 for this relation is [1] = { ... , -7, -3, 1,5,9, ... }. In Example 9 the equivalence classes of 0 and 1 with respect to congruence modulo 4 were found. Example 9 can easily be generalized, replacing 4 with any positive integer m. The equivalence classes of the relation congruence modulo m are called the congruence classes modulo m. The congruence class of an integer a modulo m is denoted by [a]m, so [a]m = { ... , a -2m, a -m, a, a + m, a + 2m, ... }. For instance, from Example 9 it follows that [0]4 = { ... , -8, -4,0,4,8, ... } and [1]4 = { ... , -7, -3,1, 5, 9, ... }. 8-41 8.5 Equivalence Relations 559 EXAMPLE 10 What is the equivalence class of the string 0111 with respect to the equivalence relation R3 from Example 5 on the set of all bit strings? (Recall that s R3 t if and only if s and t are bit strings with s = t or s and t are strings of at least three bits that start with the same three bits.) Solution: The bit strings equivalent to 0111 are the bit strings with at least three bits that begin with OIl. These are the bit strings 011,0110,0111,01100,01101,01110,01111, and so on. Consequently, [011lR3 = {OIl, 0110, 0111, 01100, 01101, 01110, 01111, ... }. EXAMPLE 11 Identifiers in the C Programming Langu age In the C programming language, an identifier is the name of a variable, a function, or another type of entity. Each identifier is a nonempty string of characters where each character is a lowercase or an uppercase English letter, a digit, or an underscore, and the first character is a lowercase or an uppercase English letter. Identifiers can be any length. This allows developers to use as many characters as they want to name an entity, such as a variable. However, for compilers for some versions of C, there is a limit on the number of characters checked when two names are compared to see whether they refer to the same thing. For example, Standard C compilers consider two identifiers the same when they agree in their first 31 characters. Consequently, developers must be careful not to use identifiers with the same initial 31 characters for different things. We see that two identifiers are considered the same when they are related by the relation R31 in Example 5. Using Example 5, we know that R31, on the set of all identifiers in Standard C, is an equivalence relation. What are the equivalence classes of the each of the identifiers NumbecoLtrop icaL storms, Number_oLnamed_trop icaLstorms, and Number_oLnamed_ tropicaLstormsjn_the_ Atlantidn- 2005? Solution: Note that when an identifier is less than 31 characters long, by the definition of R31, its equivalence class contains only itself. Because the identifier NumbecoLtrop icaLstorms is 25 characters long, its equivalence class contains exactly one element, namely, itself. The identifier NumbecoLnamed_ tropicaLstorms is exactly 31 characters long. An identifier is equivalent to it when it starts with these same 31 characters. Consequently, every identifier at least 31 characters long that starts with NumbecoLnamed_tr opicaLstorms is equivalent to this identifier. It follows that the equivalence class of NumbecoLnamed_trop icaLstorms is the set of all identifiers that begin with the 31 characters NumbecoLnamed_ tropicaLstorms. An identifier is equivalent to the NumbecoLnamed_ tropicaLstormsjn_the.Atlantic_in_ 2005 if and only if it begins with its first 31 characters. Because these characters are NumbecoLnamed_ tropicaLstorms, we see that an identifier is equivalent to Num­ becoLnamed_tr opicaLstormsjn_ the.Atlanti c_in-2005 if and only if it is equivalent to Num­ ber _of-Ilamed_tr opicaLstorms. It follows that these last two identifiers have the same equivalence cl� � Equivalence Classes and Partitions Let A be the set of students at your school who are majoring in exactly one subject, and let R be the relation on A consisting of pairs (x, y), where x andy are students with the same major. Then R is an equivalence relation, as the reader should verify. We can see that R splits all students in A into a collection of disjoint subsets, where each subset contains students with a specified major. For instance, one subset contains all students majoring (just) in computer science, and a second subset contains all students majoring in history. Furthermore, these subsets are equivalence classes of R. This example illustrates how the equivalence classes of an equivalence relation 560 8 / Relations THEOREM 1 8-42 partition a set into disjoint, nonempty subsets. We will make these notions more precise in the following discussion. Let R be a relation on the set A. Theorem I shows that the equivalence classes of two elements of A are either identical or disjoint. Let R be an equivalence relation on a set A. These statements for elements a and b of A are equivalent: (i) aRb (ii) [a] = [b] (iii) [a] n [b] =10 Proof: We first show that (i) implies (ii). Assume that aRb. We will prove that [a] = [b] by showing [a] � [b] and [b] � [a]. Suppose c E [a]. ThenaRc. Because aRb and R is symmetric, we know that bRa. Furthermore, because R is transitive and bRa and aRc, it follows that b R c. Hence, c E [b]. This shows that [a] � [b]. The proof that [b] � [a] is similar; it is left as an exercise for the reader. Second, we will show that (ii) implies (iii). Assumethat[a] = [b].It follows that[a] n [b] =I o because [a] is nonempty (because a E [a] because R is reflexive). Next, we will show that (iii) implies (i). Suppose that [a] n [b] =I 0. Then there is an element c with c E [a] and c E [b]. In other words, aRc and b R c. By the symmetric property, c R b. Then by transitivit y, because aRc and cR b, we have aR b. Because (i) implies (ii), (ii) implies (iii), and (iii) implies (i), the three statements, (i), (ii), and (iii), are equivalent. <l We are now in a position to show how an equivalence relation partitions a set. Let R be an equivalence relation on a set A. The union of the equivalence classes of R is all of A, because an element a of A is in its own equivalence class, namely, [a]R. In other words, In addition, from Theorem 1, it follows that these equivalence classes are either equal or disjoint, so [a]R n [b]R = 0, when [a]R =I [b]R' These two observations show that the equivalence classes form a partition of A, because they split A into disjoint subsets. More precisely, a partition of a set S is a collection of disjoint nonempty subsets of S that have S as their union. In other words, the collection of subsets Ai, i E I (where I is an index set) forms a partition of S if and only if Ai =I 0 for i E I, Ai n A j = 0 when i =I j, and (Here the notation UiEI Ai represents the union of the sets Ai for all i E I.) Figure 1 illustrates the concept of a partition of a set. 8-43 8.5 Equivalence Relations 561 FIGURE 1 A Partition of a Set. EXAMPLE 12 Suppose that S = {I, 2, 3,4, -5, 6}_ The collection of sets Al = {I, 2, 3}, A2 = {4, 5}, and A3 = THEOREM 2 {6} forms a partition of S, because these sets are disjoint and their union is S _ ... We have seen that the equivalence classes of an equivalence relation on a set form a partition of the set. The subsets in this partition are the equivalence classes. Converse ly, every partition of a set can be used to form an equivalence relation. Two elements are equivalent with respect to this relation if and only if they are in the same subset of the partition. To see this, assume that {Ai liE I} is a partition on S. Let R be the relation on S consisting of the pair (x, y), where x and y belong to the same subset Ai in the partition. To show that R is an equivalence relation we must show that R is reflexive, symmetric, and transitive. We see that (a, a) E R for every a E S, because a is in the same subset as itself. Hence, R is reflexive. If (a, b) E R, then b and a are in the same subset of the partition, so that (b, a) E R as well. Hence, R is symmetr ic. If (a, b) E R and (b, c) E R, then a and b are in the same subset in the partition, X, and b and c are in the same subset of the partition, Y. Because the subsets of the partition are disjoint and b belongs to X and Y, it follows that X = Y. Consequently, a and c belong to the same subset of the partition, so (a, c) E R. Thus, R is transitive. It follows that R is an equivalence relation. The equivalence classes of R consist of subsets of S containing related elements, and by the definition of R, these are the subsets of the partition. Theorem 2 summarizes the connections we have established between equivalence relations and partitions. Let R be an equivalence relation on a set S. Then the equivalence classes of R form a partition of S. Conversely, given a partition {Ai liE I} of the set S, there is an equivalence relation R that has the sets Ai, i E I, as its equivalence classes. Example 13 shows how to construct an equivalence relation from a partition. EXAMPLE 13 List the ordered pairs in the equivalence relation R produced by the partition A I = {I, 2, 3}, A2 = {4, 5}, and A3 = {6} of S = {I, 2, 3,4,5, 6}, given in Example 12. Solution: The subsets in the partition are the equivalence classes of R. The pair (a, b) E R if and only if a and b are in the same subset of the partition. The pairs (1, 1), (1, 2), (1, 3), (2, 1), (2,2), (2,3), (3, 1), (3, 2), and (3,3) belong to R because Al = {I, 2, 3} is an equivalence class; the pairs (4,4), (4,5), (5, 4), and (5,5) belong to R because A2 = {4, 5} is an equivalence class; and finally the pair (6, 6) belongs to R because {6} is an equivalence class. No pair other than those listed belongs to R. ... The congruence classes modulo m provide a useful illustration of Theorem 2. There are m different congruence classes modulo m, corresponding to the m different remainders 562 8 / Relations 8-44 possible when an integer is divided by m. These m congruence classes are denoted by [O]m, [1]m, ... , [m -l]m. They form a partition of the set of integers. EXAMPLE 14 What are the sets in the partition of the integers arising from congruence modulo 4? Solution: There are four congruence classes, corresponding to [0]4, [1]4, [2]4, and [314. They are the sets [0]4 = { ... , -8, -4,0,4,8, ... }, [1]4 = { ... , -7, -3, 1,5,9, ... }, [2]4 = { ... , -6, -2,2,6, 10, ... }, [3]4 = { ... , -5, -1, 3, 7,11, ... }. These congruence classes are disjoint, and every integer is in exactly one of them. In other words, as Theorem 2 says, these congruence classes form a partition. .... We now provide an example of a partition of the set of all strings arising from an equivalence relation on this set. EXAMPLE 15 Let R3 be the relation from Example 5. What are the sets in the partition ofthe set of all bit strings arising from the relation R3 on the set of all bit strings? (Recall that s R3 t, where sand t are bit strings, if s = t or sand t are bit strings with at least three bits that agree in their first three bits.) Exercises Solution: Note that every bit string of length less than three is equivalent only to itself. Hence P"]R3 = {A}, [0]R3 = {OJ, [1]R3 = {I}, [00]R3 = {OO}, [01]R3 = {01}, [1O]R3 = {l0}, and [11 ]R3 = {II}. Note that every bit string oflength three or more is equivalent to one of the eight bit strings 000, 001,010,011, 100, 101, 110, and 111. We have [000]R3 = {OOO, 0000, 0001, 00000, 00001, 00010, 00011, ... }, [001]R3 = {001, 0010, 0011, 00100, 00101, 00110, 00111, ... }, [01O]R3 = {01O, 0100, 0101, 01000, 01001, 01010, 01011, ... }, [011]R3 = {OIl, 0110, 0111, 01100, 01101, 01110, 01111, ... }, [100b3 = {100, 1000, 1001, 10000, 10001, 10010, 10011, ... }, [101]R3 = {101, 1010, 1011, 10100, 10101, 10110, 10111, ... }, [110b3 = {11O, 1100, 1101, 11000, 11001, 11010, 11011, ... }, and [111]R3 = {l11, 1110, 1111, 11100, 11101, 11110, 11111, ... }. These 15 equivalence classes are disjoint and every bit string is in exactly one of them. As Theorem 2 tells us, these equivalence classes partition the set of all bit strings. .... 1. Which of these relations on {O, 1,2, 3} are equivalence relations? Determine the properties of an equivalence re­ lation that the others lack. a) {(O, 0), (1, 1), (2, 2), (3, 3)} b) {(O, 0), (0, 2), (2, 0), (2, 2), (2, 3), (3, 2), (3, 3)} c) {(O, 0), (1,1), (I, 2), (2, I), (2, 2), (3, 3)} 8-45 d) {(O, 0), (1, I), (I, 3), (2, 2), (2, 3), (3, I), (3, 2), (3,3)} e) {CO, 0), (0, I), (0, 2), (I, 0), (I, I), (I, 2), (2, 0), (2, 2), (3, 3)} 2. Which ofthese relations on the set of all people are equiv­ alence relations? Determine the properties of an equiva­ lence relation that the others lack. a) {(a, b) I a and b are the same age} b) {(a, b) I a and b have the same parents} c) {(a, b) I a and b share a common parent} d) {(a, b) I a and b have met} e) {(a, b) I a and b speak a common language} 3. Which of these relations on the set of all functions from Z to Z are equivalence relations? Determine the properties of an equivalence relation that the others lack. a) {(f, g) I 1(1) = g(1)} b) {(f, g) I 1(0) = g(O) or 1(1) = g(1)} c) {(f, g) I I(x) -g(x) = I for all x E Z} d) {(f,g) I for some C E Z, for all x E z, I(x)­ g(x) = C} e) {(f, g) I 1(0) = gel) and 1(1) = g(O)} 4. Define three equivalence relations on the set of students in your discrete mathematics class different from the re­ lations discussed in the text. Determine the equivalence classes for each of these equivalence relations. 5. Define three equivalence relations on the set of buildings on a college campus. Determine the equivalence classes for each of these equivalence relations. 6. Define three equivalence relations on the set of classes offered at your school. Determine the equivalence classes for each of these equivalence relations. 7. Show that the relation of logical equivalence on the set of all compound propositions is an equivalence relation. What are the equivalence classes of F and of T? 8. Let R be the relation on the set of all sets of real num­ bers such that S R T if and only if S and T have the same cardinality. Show that R is an equivalence rela­ tion. What are the equivalence classes ofthe sets {O, I, 2} andZ? 9. Suppose that A is a nonempty set, and I is a function that has A as its domain. Let R be the relation on A consisting of all ordered pairs (x, y) such that I(x) = l(y). a) Show that R is an equivalence relation on A. b) What are the equivalence classes of R? 10. Suppose that A is a nonempty set and R is an equivalence relation on A. Show that there is a function I with A as its domain such that (x, y) E R if and only if I(x) = l(y). 11. Show that the relation R consisting of all pairs (x, y) such that x and yare bit strings of length three or more that agree in their first three bits is an equivalence re­ lation on the set of all bit strings of length three or more. 12. Show that the relation R consisting of all pairs (x, y) such that x and yare bit strings of length three or more that 8.5 Equivalence Relations 563 agree except perhaps in their first three bits is an equiva­ lence relation on the set of all bit strings of length three or more. 13. Show that the relation R consisting of all pairs (x, y) such that x and yare bit strings that agree in their first and third bits is an equivalence relation on the set of all bit strings of length three or more. 14. Let R be the relation consisting of all pairs (x, y) such that x and yare strings of uppercase and lowercase English letters with the property that for every positive integer n, the nth characters in x and yare the same letter, either uppercase or lowercase. Show that R is an equivalence relation. 15. Let R be the relation on the set of ordered pairs of positive integers such that «a, b), (c, d» E R if and only if a + d = b + c. Show that R is an equivalence relation. 16. Let R be the relation on the set of ordered pairs of pos­ itive integers such that «a, b), (c, d» E R if and only if ad = bc. Show that R is an equivalence relation. 17. (Requires calculus) a) Show that the relation R on the set of all differentiable functions from R to R consisting of all pairs (f, g) such that I'(x) = g'(x) for all real numbers x is an equivalence relation. b) Which functions are in the same equivalence class as the function I(x) = x2? 18. (Requires calculus) a) Let n be a positive integer. Show that the relation R on the set of all polynomials with real-valued coeffi- . cients consisting of all pairs (f, g) such that I(n)(x) = g(n) (x ) is an equivalence relation. [Here I(n)(x) is the nth derivative of I(x).] b) Which functions are in the same equivalence class as the function I(x) = x\ where n = 3? 19. Let R be the relation on the set of all URLs (or Web ad­ dresses) such that x R y if and only if the Web page at x is the same as the Web page at y. Show that R is an equivalence relation. 20. Let R be the relation on the set of all people who have visited a particular Web page such that x R y if and only if person x and person y have followed the same set of links starting at this Web page (going from Web page to Web page until they stop using the Web). Show that R is an equivalence relation. In Exercises 21-23 determine whether the relation with the directed graphs shown is an equivalence relation. 21. 22. 564 8 / Relations 23. 24. Determine whether the relations represented by these zer�ne matrices are equivalence relations. a) [� i] b) [� � ��] c) [i 1 1 �] o 1 0 1 000 1 25. Show that the relation R on the set of all bit strings such that s R t if and only if s and t contain the same number of 1 s is an equivalence relation. 26. What are the equivalence classes of the equivalence rela­ tions in Exercise I? 27. What are the equivalence classes of the equivalence relations in Exercise 2? 28. What are the equivalence classes of the equivalence rela­ tions in Exercise 3? 29. What is the equivalence class of the bit string 011 for the equivalence relation in Exercise 25? 30. What are the equivalence classes of these bit strings for the equivalence relation in Exercise II? a) 010 b) 1011 c) 11111 d) 01010101 31. What are the equivalence classes of the bit strings in Ex­ ercise 30 for the equivalence relation from Exercise II? 32. What are the equivalence classes of the bit strings in Exercise 30 for the equivalence relation from Exercise 12? 33. What are the equivalence classes of the bit strings in Ex­ ercise 30 for the equivalence relation R4 from Example 5 on the set of all bit strings? (Recall that bit strings s and t are equivalent under R4 if and only if they are equal or they are both at least four bits long and agree in their first four bits.) 34. What are the equivalence classes ofthe bit strings in Ex­ ercise 30 for the equivalence relation Rs from Example 5 on the set of all bit strings? (Recall that bit strings s and t are equivalent under Rs if and only if they are equal or they are both at least five bits long and agree in their first five bits.) 35. What is the congruence class [nJs (that is, the equivalence class of n with respect to congruence modulo 5) when n is a) 2? b) 3? c) 6? d) -3? 36. What is the congruence class [4]m when m is a) 2? b) 3? c) 6? d) 8? 37. Give a description of each ofthe congruence classes mod­ ulo 6. 38. What is the equivalence class of the strings with respect to the equivalence relation in Exercise 14? a) No b) Yes c ) Help 8-46 39. a) What is the equivalence class of (1, 2) with respect to the equivalence relation in Exercise 15? b) Give an interpretation of the equivalence classes for the equivalence relation R in Exercise 15. [Hint: Look at the difference a -b corresponding to (a, b).] 40. a) What is the equivalence class of (1,2) with respect to the equivalence relation in Exercise 16? b) Give an interpretation of the equivalence classes for the equivalence relation R in Exercise 16. [Hint: Look at the ratio alb corresponding to (a, b).] 41. Which of these collections of subsets are partitions of {I, 2, 3,4,5, 6}? a) {I, 2}, {2, 3, 4}, {4, 5, 6} b) {l}, {2, 3, 6}, {4}, {5} c) {2, 4, 6}, {I, 3, 5} d) {I, 4, 5}, {2, 6} 42. Which of these collections of subsets are partitions of {-3, -2, -1,0,1,2, 3}? a) {-3, -1, 1, 3}, {-2, 0, 2} b) {-3, -2, -1, O}, {O, 1,2, 3} c) {-3, 3}, {-2, 2}, {-I, I}, (OJ d) {-3,-2,2,3},{-I,l} 43. Which of these collections of subsets are partitions on the set of bit strings oflength 8? a) the set of bit strings that begin with 1, the set of bit strings that begin with 00, and the set of bit strings that begin with 01 b) the set of bit strings that contain the string 00, the set of bit strings that contain the string 01, the set of bit strings that contain the string 10, and the set of bit strings that contain the string 11 c) the set of bit strings that end with 00, the set of bit strings that end with 01, the set of bit strings that end with 10, and the set of bit strings that end with 11 d) the set of bit strings that end with Ill, the set of bit strings that end with 011, and the set of bit strings that end with 00 e) the set of bit strings that have 3k ones, where k is a nonnegative integer; the set of bit strings that contain 3k + lones, where k is a nonnegative integer; and the set of bit strings that contain 3k + 2 ones, where k is a nonnegative integer 44. Which ofthese collections of subsets are partitions of the set of integers? a) the set of even integers and the set of odd integers b) the set of positive integers and the set of negative integers c) the set of integers divisible by 3, the set of integers leaving a remainder of 1 when divided by 3, and the set of integers leaving a remainder of 2 when divided by 3 d) the set of integers less than -100, the set of integers with absolute value not exceeding 100, and the set of integers greater than 100 e) the set of integers not divisible by 3, the set of even integers, and the set of integers that leave a remainder of 3 when divided by 6 8-47 45. Which of these are partitions of the set Z x Z of ordered pairs of integers? a) the set of pairs (x, y), where x or y is odd; the set of pairs (x, y), where x is even; and the set of pairs (x, y), where y is even b) the set of pairs (x, y), where both x and yare odd; the set of pairs (x, y), where exactly one of x and y is odd; and the set of pairs (x, y), where both x and yare even c) the set of pairs (x, y), where x is positive; the set of pairs (x, y), where y is positive; and the set of pairs (x, y), where both x and yare negative d) the set of pairs (x, y), where 3 I x and 3 I y; the set of pairs (x, y), where 3 I x and 3 y y; the set of pairs (x, y), where 3 y x and 3 I y; and the set of pairs (x, y), where 3 y x and 3 y y e) the set of pairs (x, y), where x > 0 and y > 0; the set of pairs (x, y), where x > 0 and y :'S 0; the set of pairs (x, y), where x :'S 0 and y > 0; and the set of pairs (x, y), where x :'S 0 and y :'S 0 t) the set of pairs (x, y), where x =1= 0 and y =1= 0; the set of pairs (x, y), where x = 0 and y =1= 0; and the set of pairs (x, y), where x =1= 0 and y = 0 46. Which of these are partitions of the set of real numbers? a) the negative real numbers, {O}, the positive real numbers b) the set of irrational numbers, the set of rational numbers c) the set of intervals [k, k + 1], k = ... , -2, -1,0, 1,2, ... d) the set of intervals (k, k + I), k = ... , -2, -1,0, 1,2, ... e) the set of intervals (k, k + I], k = ... , -2, -1,0, 1,2, ... t) the sets {x + n In E Z} for all x E [0, I) 47. List the ordered pairs in the equivalence relations pro­ duced by these partitions of {O, 1,2,3,4, 5}. a) {OJ, {I, 2}, {3, 4, 5} b) {O, I}, {2, 3}, {4, 5} c) {O, I, 2}, {3, 4, 5} d) {O}, {I}, {2}, {3}, {4}, {5} 48. List the ordered pairs in the equivalence relations pro­ duced by these partitions of {a, b, c, d, e, f, g}. a) {a, b}, {c, d}, {e, f, g} b) {a}, {b}, {c, d}, {e, f}' {g} c) {a, b, c, d}, {e, f, g} d) {a, c, e, g}, {b, d}, {f} A partition PI is called a refinement of the partition Pz if every set in PI is a subset of one of the sets in Pz. 49. Show that the partition formed from congruence classes modulo 6 is a refinement of the partition formed from congruence classes modulo 3. 50. Show that the partition of the set of people living in the United States consisting of subsets of people living in the same county (or parish) and same state is a refinement of 8.5 Equivalence Relations 565 the partition consisting of subsets of people living in the same state. 51. Show that the partition ofthe set of bit strings oflength 16 . formed by equivalence classes of bit strings that agree on the last eight bits is a refinement of the partition formed from the equivalence classes of bit strings that agree on the last four bits. In Exercises 52 and 53, Rn refers to the family of equivalence relations defined in Example 5. Recall that s Rn t, where sand t are two strings if s = t or s and t are strings with at least n characters that agree in their first n characters. 52. Show that the partition of the set of all bit strings formed by equivalence classes of bit strings with respect to the equivalence relation R4 is a refinement of the partition formed by equivalence classes of bit strings with respect to the equivalence relation R3• 53. Show that the partition of the set of all identifiers in C formed by the equivalence classes of identifiers with re­ spect to the equivalence relation R31 is a refinement of the partition formed by equivalence classes of identifiers with respect to the equivalence relation R 8. (Compilers for "old" C consider identifiers the same when their names agree in their first eight characters, while compilers in standard C consider identifiers the same when their names agree in their first 31 characters .) 54. Suppose that RI and Rz are equivalence relations on a set A. Let PI and Pz be the partitions that correspond to RI and R z, respectively. Show that R I � R z if and only if PI is a refinement of Pz. 55. Find the smallest equivalence relation on the set {a, b, c, d, e} containing the relation {(a, b), (a, c), (d, e)}. 56. Suppose that R I and Rz are equivalence relations on the set S. Determine whether each of these combinations of RI and Rz must be an equivalence relation. a) RI U R2 b) RI n R2 c) RI tB R2 57. Consider the equivalence relation from Example 3, namely, R = {(x,y) I x -y is an integer}. a) What is the equivalence class of I for this equivalence relation? b) What is the equivalence class of 112 for this equiva­ lence relation? *58. Each bead on a bracelet with three beads is either red, white, or blue, as illustrated in the figure shown. Bead I Red Bead 30 Bead 2 Blue White Define the relation R between bracelets as: (BI, B2), where B I and B2 are bracelets, belongs to R if and only 566 8 / Relations if B2 can be obtained from B1 by rotating it or rotating it and then reflecting it. a) Show that R is an equivalence relation. b) What are the equivalence classes of R? *59. Let R be the relation on the set of all colorings of the 2 x 2 checkerboard where each of the four squares is col­ ored either red or blue so that (C 1, C 2), where eland C 2 are 2 x 2 checkerboards with each of their four squares colored blue or red, belongs to R if and only if C 2 can be obtained from C 1 either by rotating the checkerboard or by rotating it and then reflecting it. a) Show that R is an equivalence relation. b) What are the equivalence classes of R? 60. a) Let R be the relation on the set of functions from Z+ to Z+ such that (j, g) belongs to R if and only if f is 8(g) (see Section 3.2). Show that R is an equivalence relation. b) Describe the equivalence class containing fen) = n2 for the equivalence relation of part (a). 61. Determine the number of different equivalence relations on a set with three elements by listing them. 62. Determine the number of different equivalence relations on a set with four elements by listing them. *63. Do we necessarily get an equivalence relation when we 8.6 Partial Orderings Introduction 8-48 form the transitive closure of the symmetric closure ofthe reflexive closure of a relation? *64. Do we necessarily get an equivalence relation when we form the symmetric closure ofthe reflexive closure of the transitive closure of a relation? 65. Suppose we use Theorem 2 to form a partition P from an equivalence relation R. What is the equivalence rela­ tion R' that results if we use Theorem 2 again to form an equivalence relation from P? 66. Suppose we use Theorem 2 to form an equivalence rela­ tion R from a partition P. What is the partition P' that results if we use Theorem 2 again to form a partition from R? 67. Devise an algorithm to find the smallest equivalence re­ lation containing a given relation. *68. Let pen) denote the number of different equivalence relations on a set with n elements (and by Theorem 2 the number of partitions of a set with n elements). Show that p( n) satisfies the recurrence relation p( n) = L�:6C(n - I,j)p(n -j - I) and the initial condition p(O) = I. (Note: The numbers p(n) are called Bell numbers after the American mathematician E. T. Bell.) 69. Use Exercise 68 to find the number of different equiv­ alence relations on a set with n elements, where n is a positive integer not exceeding 10. LinkS� We often use relations to order some or all of the elements of sets. For instance, we order words using the relation containing pairs of words (x, y), where x comes before y in the dictionary. We schedule projects using the relation consisting of pairs (x, y), where x and yare tasks in a project such that x must be completed before y begins. We order the set of integers using the relation containing the pairs (x, y), where x is less than y. When we add all of the pairs of the form (x , x) to these relations, we obtain a relation that is reflexive, anti symmetri c, and transitive. These are properties that characterize relations used to order the elements of sets. DEFINITION 1 EXAMPLE l Extra� Examples � A relation R on a set S is called a partial ordering or partial order if it is reflexive, an tisym­ metric, and transitive . A set S together with a partial ordering R is called a partially ordered set, or poset, and is denoted by (S, R). Members of S are called elements of the poset. We give examples of po sets in Examples 1-3. Show that the "greater than or equal" relation C:::) is a partial ordering on the set of integers. Solution: Because a ::: a for every integer a, ::: is reflexive. If a ::: band b ::: a, then a = b. Hence, ::: is antisymmetri c. Finally, ::: is transitive because a ::: band b ::: c imply that a ::: c. It follows that ::: is a partial ordering on the set of integers and (Z, :::) IS a �� � 8-49 8.6 Partial Orderings 567 EXAMPLE 2 The divisibility relation 1 is a partial ordering on the set of positive integers, because it is reflexive, antisymmetri c, and transitive, as was shown in Section 8.1. We see that (Z+, I) is a poset. Recall that (Z+ denotes the set of positive integers.) ... EXAMPLE 3 Show that the inclusion relation � is a partial ordering on the power set of a set S. EXAMPLE 4 Extra� ExamPles "" Solution: Because A � A whenever A is a subset of S, � is reflexive. It is anti symmetric because A � B and B � A imply that A = B. Finally, � is transitive, because A � B and B � C imply that A � C. Hence, � is a partial ordering on P(S), and (P(S), �) is a po set. ... Example 4 illustrates a relation that is not a partial ordering. Let R be the relation on the set of people such that x R y if x and yare people and x is older than y. Show that R is not a partial ordering. Solution: Note that R is anti symmetric because if a person x is older than a person y, then y is not older than x. That is, if x R y, then y R x. The relation R is transitive because if person x is older than person y and y is older than person z, then x is older than z. That is, if x R y and y R z, then x R z. However, R is not reflexive, because no person is older than himself or herself. That is, x R x for all people x. It follows that R is not a partial ordering. ... In different po sets different symbols such as �, �, and I, are used for a partial ordering. However, we need a symbol that we can use when we discuss the ordering relation in an arbitrary poset. Customari ly, the notation a � b is used to denote that (a, b) ERin an arbitrary poset (S, R). This notation is used because the "less than or equal to" relation on the set of real numbers is the most familiar example of a partial ordering and the symbol � is similar to the � symbol. (Note that the symbol � is used to denote the relation in any poset, not just the "less than or equals" relation.) The notation a -< b denotes that a � b, but a =1= b. Also, we say "a is less than b" or "b is greater than a" if a -< b. When a and b are elements of the poset (S, �), it is not necessary that either a � b or b � a. For instance, in (P(Z), �), {l, 2} is not related to {I, 3}, and vice versa, because neither set is contained within the other. Similarly, in (Z+ , I), 2 is not related to 3 and 3 is not related to 2, because 2 [ 3 and 3 [ 2. This leads to Definition 2. DEFINITION 2 The elements a andbofaposet(S, �) are called comparable if either a � b orb � a. When a and b are elements of S such that neither a � b nor b � a, a and b are called incomparable. EXAMPLE 5 In the poset (Z+, I), are the integers 3 and 9 comparable? Are 5 and 7 comparable? Solution: The integers 3 and 9 are comparable, because 3 1 9. The integers 5 and 7 are incom­ parable, because 5[7 and 7 [ 5. ... The adjective "partial" is used to describe partial orderings because pairs of elements may be incompara ble. When every two elements in the set are comparable, the relation is called a total ordering. 568 8/ Relations 8-50 DEFINITION 3 If(S, �) is a poset and every two elements of S are comparable, S is called a totally ordered or linearly ordered set, and � is called a total order or a linear order. A totally ordered set is also called a chain. EXAMPLE 6 The poset (Z, ::::) is totally ordered, because a :::: b or b :::: a whenever a and b are integers. -<III EXAMPLE 7 The poset (Z+, I ) is not totally ordered because it contains elements that are incomparable, such as 5 and 7. -<III In Chapter 4 we noted that (Z+, ::::) is well-ordered, where :::: is the usual "less than or equal to" relation. We now define well-ordered sets. D EFINITI 0 N 4 (S, �) is a well-ordered set if it is a poset such that � is a total ordering and every nonempty subset of S has a least element. EXAMPLE 8 The set of ordered pairs of positive integers, Z+ x Z+, with (ai, a2) � (bl, b2) if al < bl, or if al = bl and a2 :::: b2 (the lexicographic ordering), is a well-ordered set. The verification of this is left as an exerc ise at the end of this section. The set Z, with the usual :::: ordering, is not well-ordered because the set of negative integers, which is a subset of Z, has no least element. -<III THEOREM 1 In Section 4.3 we showed how to use the principle of well-ordered induction (there called generalized induction) to prove results about a well-ordered set. We now state and prove that this proof technique is valid. THE PRINCIPLE OF WELL-ORDERED INDUCTION Suppose that S is a well­ ordered set. Then P(x) is true for all XES, if INDUCTIVE STEP: For every yES, if P(x) is true for all XES with x -< y, then P(y) is true. Proof: Suppose it is not the case that P(x) is true for all XES. Then there is an element yES such that P(y) is false. Consequently, the set A = {x E S I P(x) is false} is nonempty. Because S is well ordered, A has a least element a. By the choice of a as a least element of A, we know that P(x) is true for all XES with x -< a. This implies by the inductive step P(a) is true. This contradiction shows that P(x) must be true for all XES. <l Remark: We do not need a basis step in a proof using the principle of well-ordered induction because if Xo is the least element of a well ordered set, the inductive step tells us that P(xo) is true. This follows because there are no elements XES with x -< xo, so we know (using a vacuous proof) that P(x) is true for all XES with x -< xo. The principle of well-ordered induction is a versatile technique for proving results about well-ordered sets. Even when it is possible to use mathematical induction for the set of positive integers to prove a theorem, it may be simpler to use the principle of well-ordered induction, as 8-51 8.6 Partial Orderings 569 we saw in Examples 5 and 6 in Section 4.2, where we proved a result about the well-ordered set (N x N, �) where � is lexicographic ordering on N x N. Lexicographic Order The words in a dictionary are listed in alphabetic, or lexicographic, order, which is based on the ordering of the letters in the alphabet. This is a special case of an ordering of strings on a set constructed from a partial ordering on the set. We will show how this construction works in any poset. First, we will show how to construct a partial ordering on the Cartesian product oftwo posets, (AI, � I) and (A2, �2). The lexicographic ordering � on AI x A2 is defined by specifying that one pair is less than a second pair if the first entry of the first pair is less than (in A d the first entry of the second pair, or if the first entries are equal, but the second entry of this pair is less than (in A2) the second entry ofthe second pair. In other words, (aI, a2) is less than (bl, b2), that is, either if al -<I bl or if both al = bl and a2 -<2 b2. We obtain a partial ordering � by adding equality to the ordering -< on AI x A2. The verification of this is left as an exercise. EXAMPLE 9 Determine whether (3,5) -< (4,8), whether (3,8) -< (4,5), and whether (4, 9) -< (4, 11) in the poset (Z x Z, �), where � is the lexicographic ordering constructed from the usual :s relation onZ. Solution: Because 3 < 4, it follows that (3,5) -< (4,8) and that (3,8) -< (4,5). We have (4,9) -< (4, 11), because the first entries of (4, 9) and (4, 11) are the same but 9 < 11. .... In Figure 1 the ordered pairs in Z+ x Z+ that are less than (3,4) are highlighted. A lexicographic ordering can be defined on the Cartesian product of n po sets (A I, � I), (A2, �2), ... , (An, �n). Define the partial ordering � on AI x A2 X ... x An by if al -<I b), or if there is an integer i > 0 such that al = bl, ... , ai = bi, and ai+1 -<i+1 bi+l. In other words, one n-tuple is less than a second n-tuple if the entry of the first n-tuple in the first position where the two n-tuples disagree is less than the entry in that position in the second n-tuple. EXAMPLE 10 Note that (l, 2, 3, 5) -< (l, 2, 4, 3), because the entries in the first two positions of these 4-tuples agree, but in the third position the entry in the first 4-tuple, 3, is less than that in the second 4-tuple, 4. (Here the ordering on 4-tuples is the lexicographic ordering that comes from the usual "less than or equals" relation on the set of integers.) .... We can now define lexicographic ordering of strings. Consider the strings al a2 ... am and bl b2 ... bn on a partially ordered set S. Suppose these strings are not equal. Let t be the minimum of m and n. The definition of lexicographic ordering is that the string al a2 ... am is less than 570 8 / Relations • • • • (1,7) (2,7) (3,7) (4,7) • • • • (1,6) (2,6) (3,6) (4,6) • • • • (1,5) (2,5) (3,5) (4,5) • • • • (1,4) (2,4) (3,4) (4,4) • • • • (1,3) (2,3) (3,3) (4,3) • • • • (1,2) (2,2) (3,2) (4,2) • • • • (I, 1) (2, l) (3, l) (4,1) • • (5,7) (6,7) • • (5,6) (6,6) • • (5,5) (6,5) • • (5,4) (6,4) • • (5,3) (6,3) • • (5,2) (6,2) • • (5, I) (6,1) • (7,7) • (7,6) • (7,5) • (7,4) • (7,3) • (7,2) • (7,1) · .. · .. · .. · .. · .. · .. FIGURE 1 The Ordered Pairs Less Than (3,4) in Lexicographic Order. bl b2 ... bn if and only if (aI, a2,'" , at}-«bl, b2, ... , btl, or (aI, a2,··· , at) = (bl, b2, ... , btl and m < n, 8-52 where -< in this inequality represents the lexicographic ordering of st. In other words, to de­ termine the ordering of two different strings, the longer string is truncated to the length of the shorter string, namely, to t = min(m, n} terms. Then the t-tuples made up of the first t terms of each string are compared using the lexicographic ordering on st. One string is less than another string if the t-tuple corresponding to the first string is less than the t-tuple of the second string, or if these two t-tuples are the same, but the second string is longer. The verification that this is a partial ordering is left as an exercise for the reader. EXAMPLE 11 Consider the set of strings of lowercase English letters. Using the ordering of letters in the alphabet, a lexicographic ordering on the set of strings can be constructed. A string is less than a second string if the letter in the first string in the first position where the strings differ comes before the letter in the second string in this position, or if the first string and the second string agree in all positions, but the second string has more letters. This ordering is the same as that used in dictionaries. For example, discreet -< discrete, because these strings differ first in the seventh position, and e -< t. Also, discreet -< discreetness , because the first eight letters agree, but the second string is longer. Furthermore, discrete -< discretion, because discrete -< discreti. 8-53 lInkS� 4 (b) 4 3 2 1 (c) FIGURE 2 Constructing the Hasse Diagram for ({I, 2, 3, 4}, �). Hasse Diagrams 8.6 Partial Orderings 571 Many edges in the directed graph for a finite poset do not have to be shown because they must be present. For instance, consider the directed graph for the partial ordering {(a, b) I a S b} on the set {l, 2, 3, 4}, shown in Figure 2(a). Because this relation is a partial ordering, it is reflexive, and its directed graph has loops at all vertices. Consequently, we do not have to show these loops because they must be present; in Figure 2(b) loops are not shown. Because a partial ordering is transitive, we do not have to show those edges that must be present because of transitivity. For example, in Figure 2(c) the edges (1, 3), (1,4), and (2, 4) are not shown because they must be present. If we assume that all edges are pointed "upward" (as they are drawn in the figure), we do not have to show the directions of the edges; Figure 2(c) does not show directions. In general, we can represent a partial ordering on a finite set using this procedure: Start with the directed graph for this relation. Because a partial ordering is reflexive, a loop is present at every vertex. Remove these loops. Remove all edges that must be in the partial ordering because of the presence of other edges and transitivity. For instance, if (a, b) and (b, c) are in the partial ordering, remove the edge (a, c), because it must be present also. Furthermore, if (c, d) is also in the partial ordering, remove the edge (a, d), because it must be present also. Finally, arrange each edge so that its initial vertex is below its terminal vertex (as it is drawn on paper). Remove all the arrows on the directed edges, because all edges point "upward" toward their terminal vertex. (The edges left correspond to pairs in the covering relation of the poset. See the preamble to Exercise 28.) These steps are well defined, and only a finite number of steps need to be carried out for a finite poset. When all the steps have been taken, the resulting diagram contains sufficient information to find the partial ordering. This diagram is called a Hasse diagram, named after the twentieth-century German mathematician Helmut Hasse. HELMUT HASSE (1898-1979) Helmut Hasse was born in Kassel, Germany. He served in the German navy after high school. He began his university studies at Gottingen University in 1918, moving in 1920 to Marburg University to study under the number theorist Kurt Hensel. During this time, Hasse made fundamental contributions to algebraic number theory. He became Hensel's successor at Marburg, later becoming director of the famous mathematical institute at Gottingen in 1934, and took a position at Hamburg University in 1950. Hasse served for 50 years as an editor of Crelle s Journal, a famous German mathematics periodical, taking over . the job of chief editor in 1936 when the Nazis forced Hensel to resign. During World War II Hasse worked on applied mathematics research for the German navy. He was noted for the clarity and personal style of his lectures and was devoted both to number theory and to his students. (Hasse has been controvers ial for connections with the Nazi party. Investigations have shown he was a strong German nationalist but not an ardent Nazi.) 572 8 / Relations 8 4 2 (a) (b) FIGURE 3 Constructing the Hasse Diagram of ({I, 2, 3, 4, 6, 8, 12}, I). 8-54 12 6 3 (c) EXAMPLE 12 Draw the Hasse diagram representing the partial ordering {(a, b) 1 a divides b} on {I, 2, 3,4,6,8, 12}. Solution: Begin with the digraph for this partial order, as shown in Figure 3(a). Remove all loops, as shown in Figure 3(b). Then delete all the edges implied by the transitive property. These are (1,4), (1,6), (1,8), (1, 12), (2,8), (2, 12), and (3,12). Arrange all edges to point upward, and delete all arrows to obtain the Hasse diagram. The resulting Hasse diagram is shown in Figure 3(c). � EXAMPLE 13 Draw the Hasse diagram for the partial ordering {(A, B) 1 A � B} on the power set P(S) where S = {a, b, c}. Solution: The Hasse diagram for this partial ordering is obtained from the associated digraph by deleting all the loops and all the edges that occur from transitivity, namely, (0, {a, b}), (0, {a, c}), (0, {b, cD, (0, {a, b, cD, ({a}, {a, b, cD, ({b}, {a, b, c}), and ({c}, {a, b, c}). Finally all edges point upward, and arrows are deleted. The resulting Hasse diagram is illustrated in Figure 4. � Maximal and Minimal Elemen ts Elements of po sets that have certain extremal properties are important for many applications. An element of a po set is called maximal if it is not less than any element of the po set. That is, a is maximal in the poset (S, =:;<) if there is no b E S such that a -< b. Similarly, an element of a poset is called minimal if it is not greater than any element of the po set. That is, a is minimal if there is no element bE S such that b -< a. Maximal and minimal elements are easy to spot using a Hasse diagram. They are the "top" and "bottom" elements in the diagram. EXAMPLE 14 Which elements ofthe po set ({2, 4, 5, 10, 12,20, 25}, I) are maximal, and which are minimal? Solution: The Hasse diagram in Figure 5 for this poset shows that the maximal elements are 12,20, and 25, and the minimal elements are 2 and 5. As this example shows, a poset can have more than one maximal element and more than one minimal element. � 8-55 8.6 Partial Orderings 573 (a, b, c) {a, c) (b, c) {a} (c) o FIGURE 4 The Hasse Diagram of(P({a,b,c}), �). 12 20 4 2 5 FIGURE 5 The Hasse Diagram of a Po set. 25 Sometimes there is an element in a poset that is greater than every other element. Such an element is called the greatest element. That is, a is the greatest element of the poset (S, �) if b � a for all b E S. The greatest element is unique when it exists [see Exercise 40(a) at the end of this section]. Likewise, an element is called the least element if it is less than all the other elements in the po set. That is, a is the least element of(S, �) if a � b for all bE S. The least element is unique when it exists [see Exercise 40(b) at the end of the section]. EXAMPLE 15 Determine whether the po sets represented by each of the Hasse diagrams in Figure 6 have a greatest element and a least element. Solution: The least element of the po set with Hasse diagram (a) is a. This poset has no greatest element. The poset with Hasse diagram (b) has neither a least nor a greatest element. The poset with Hasse diagram (c) has no least element. Its greatest element is d. The poset with Hasse diagram (d) has least element a and greatest element d. .... EXAMPLE 16 Let S be a set. Determine whether there is a greatest element and a least element in the po set (P(S), �). Solution: The least element is the empty set, because 0 � T for any subset T of S. The set S is the greatest element in this poset, because T � S whenever T is a subset of S. .... EXAMPLE 17 Is there a greatest element and a least element in the poset (Z+, I)? Solution: The integer 1 is the least element because lin whenever n is a positive integer. Because there is no integer that is divisible by all positive integers, there is no greatest element. .... (a) (b) (c) FIGURE 6 Hasse Diagrams of Four Posets. (d) 574 8/ Relations 8-56 Sometimes it is possible to find an element that is greater than or equal to all the elements in a subset A of a po set (S, =:;<). If u is an element of S such that a =:;< u for all elements a E A, then u is called an upper bound of A. Likewise, there may be an element less than or equal to all the elements in A. If I is an element of S such that I =:;< a for all elements a E A, then I is called a lower bound of A. EXAMPLE 18 Find the lower and upper bounds of the subsets {a, b, c}, {j, h}, and {a, c, d, f} in the poset with the Hasse diagram shown in Figure 7. h g d b a FIGURE 7 The Hasse Diagram of a Poset. EXAMPLE 19 EXAMPLE 20 Elltra� Examples � j Solution: The upper bounds of {a, b, c} are e, J, j, and h, and its only lower bound is a. There f are no upper bounds of {j, h}, and its lower bounds are a, b, c, d, e, and J. The upper bounds of {a, c, d, J} are J, h, and j, and its lower bound is a. <III e The element x is called the least upper bound of the subset A if x is an upper bound that is less than every other upper bound of A. Because there is only one such element, if it exists, c it makes sense to call this element the least upper bound [see Exercise 42(a) at the end of this section]. That is, x is the least upper bound of A if a =:;< x whenever a E A, and x =:;< z whenever z is an upper bound of A. Similarly, the element y is called the greatest lower bound of A if y is a lower bound of A and z =:;< y whenever z is a lower bound of A. The greatest lower bound of A is unique if it exists [see Exercise 42(b) at the end of this section]. The greatest lower bound and least upper bound of a subset A are denoted by glb( A) and lub( A), respectively. Find the greatest lower bound and the least upper bound of {b, d, g}, if they exist, in the poset shown in Figure 7. Solution: The upper bounds of {b, d, g} are g and h. Because g -< h, g is the least upper bound. The lower bounds of {b, d, g} are a and b. Because a -< b, b is the greatest lower bound. <III Find the greatest lower bound and the least upper bound of the sets {3, 9, 12} and {I, 2, 4,5, IO}, if they exist, in the poset (Z+, I). Solution: An integer is a lower bound of {3, 9, 12} if 3, 9, and 12 are divisible by this integer. The only such integers are 1 and 3. Because 1 I 3, 3 is the greatest lower bound of {3, 9, 12}. The only lower bound for the set {I, 2, 4, 5, iO} with respect to I is the element 1. Hence, 1 is the greatest lower bound for {I, 2, 4, 5, iO}. An integer is an upper bound for {3, 9, 12} if and only if it is divisible by 3, 9, and 12. The integers with this property are those divisible by the least common multiple of 3, 9, and 12, which is 36. Hence, 36 is the least upper bound of {3, 9, 12}. A positive integer is an upper bound for the set {I, 2, 4, 5, iO} if and only if it is divisible by 1,2,4,5, and 10. The integers with this property are those integers divisible by the least common multiple of these integers, which is 20. Hence, 20 is the least upper bound of {l, 2, 4, 5, iO}. <III Lattices A partially ordered set in which every pair of elements has both a least upper bound and a greatest lower bound is called a lattice. Lattices have many special properties. Furthermore, lattices are used in many different applications such as models of information flow and play an important role in Boolean algebra. 8-57 8.6 Partial Orderings 575 h d e e g g c d b b c b d d a a a (a) (b) (c) FIGURE 8 Hasse Diagrams of Three Posets. EXAMPLE 21 Determine whether the posets represented by each of the Hasse diagrams in Figure 8 are lattices. Solution: The posets represented by the Hasse diagrams in (a) and (c) are both lattices because in each po set every pair of elements has both a least upper bound and a greatest lower bound, as the reader should verifY. On the other hand, the poset with the Hasse diagram shown in (b) is not a lattice, because the elements band c have no least upper bound. To see this, note that each of the elements d, e, and f is an upper bound, but none of these three elements precedes the other two with respect to the ordering of this poset. ... EXAMPLE 22 Is the poset (Z+, I) a lattice? Solution: Let a and b be two positive integers. The least upper bound and greatest lower bound of these two integers are the least common multiple and the greatest common divisor of these integers, respectively, as the reader should verifY. It follows that this poset is a lattice. .... EXAMPLE 23 Determine whether the po sets ({ 1,2,3,4, 5}, I) and ({ 1,2,4,8, 16}, I) are lattices. Solution: Because 2 and 3 have no upper bounds in ({ 1, 2, 3, 4, 5}, I), they certainly do not have a least upper bound. Hence, the first poset is not a lattice. Every two elements of the second po set have both a least upper bound and a greatest lower bound. The least upper bound of two elements in this poset is the larger of the elements and the greatest lower bound of two elements is the smaller of the elements, as the reader should verifY. Hence, this second poset is a lattice. ... EXAMPLE 24 Determine whether (P (S), �) is a lattice where S is a set. EXAMPLE 25 LinIIS� Solution: Let A and B be two subsets of S. The least upper bound and the greatest lower bound of A and B are A U B and A n B, respective ly, as the reader can show. Hence, (P(S), �) is a lattice. ... The Lattice Model of Information Flow In many settings the flow of information from one person or computer program to another is restricted via security clearances. We can use a lattice model to represent different information flow policies. For example, one common information flow policy is the multilevel security policy used in government and military systems. Each piece of information is assigned to a security class, and each security class is represented by a pair (A, C) where A is an authority level and C is a category. People and computer programs are then allowed access to information from a specific restricted set of security classes. The typical authority levels used in the U.S. government are unclassified (0), confidential (I), secret (2), and top secret (3). Categories used in security classes are the subsets of a set of all compartments relevant to a particular area of interest. Each compartment represents a particular 576 8 / Relations linkS� LEMMA l 8-58 subject area. For example, if the set of compartments is {spies, moles, double agents}, then there are eight different categories, one for each of the eight subsets of the set of compartments, such as {spies, moles}. We can order security classes by specifying that (A\, Cd 0;< (A2, C2) if and only if A\ ::s A2 and C\ � C2. Information is permitted to flow from security class (A\, Cd into security class (A2,C2) if and only if (A\,C\) 0;«A2,C2). For example, information is permitted to flow from the security class (secret, {spies, moles}) into the security class (top secret, {spies, moles, double agents}), whereas information is not allowed to flow from the security class (top secret, {spies, moles}) into either of the security classes (secret, {spies, moles, double agents}) or (top secret, {spies}). We leave it to the reader (see Exercise 48 at the end ofthis section) to show that the set of all security classes with the ordering defined in this example forms a lattice. .... Topological Sorting Suppose that a project is made up of20 different tasks. Some tasks can be completed only after others have been finished. How can an order be found for these tasks? To model this problem we set up a partial order on the set of tasks so that a -< b if and only if a and b are tasks where b cannot be started until a has been completed. To produce a schedule for the project, we need to produce an order for all 20 tasks that is compatible with this partial order. We will show how this can be done. We begin with a definition. A total ordering 0;< is said to be compatible with the partial ordering R if a 0;< b whenever aRb. Constructing a compatible total ordering from a partial ordering is called topological sorting. * We will need to use Lemma 1. Every finite nonempty poset (S, 0;<) has at least one minimal element. Proof: Choose an element ao of S. If ao is not minimal, then there is an element a\ with a\ -< ao. If al is not minimal, there is an element a2 with a2 -< a\. Continue this process, so that if an is not minimal, there is an element a n+ \ with an+ I -< an. Because there are only a finite number of elements in the poset, this process must end with a minimal element an. <l The topological sorting algorithm we will describe works for any finite nonempty poset. To define a total ordering on the poset (A, 0;<), first choose a minimal element a\; such an element exists by Lemma 1. Next, note that (A -{ad, 0;<) is also a poset, as the reader should verify. (Here by 0;< we mean the restriction of the original relation 0;< on A to A -{a\ }.) Ifit is nonempty, choose a minimal element a2 of this poset. Then remove a2 as well, and if there are additional elements left, choose a minimal element a3 in A -{a\, a2}. Continue this process by choosing ak+1 to be a minimal element in A -{ai, a2, ... , ad, as long as elements remain. Because A is a finite set, this process must terminate. The end product is a sequence of elements ai, a2,"" an. The desired total ordering O;<t is defined by '''Topological sorting" is tenninology used by computer scientists; mathematicians use the tenninology "linearization of a partial ordering" for the same thing. In mathematics, topology is the branch of geometry dealing with properties of geometric figures that hold for all figures that can be transfonned into one another by continuous bijections. In computer science, a topology is any arrangement of objects that can be connected with edges. 8-59 8.6 Partial Orderings 577 This total ordering is compatible with the original partial ordering. To see this, note that if b -< c in the original partial ordering, c is chosen as the minimal element at a phase of the algorithm where b has already been removed, for otherwise c would not be a minimal element. Pseudocode for this topological sorting algorithm is shown in Algorithm 1. ALGOR ITHM 1 Topological Sorting procedure topological sort «S, =;<): finite poset) k:= I while S =1= 0 begin ak : = a minimal element of S {such an element exists by Lemma I} S: = S - {ad k:=k+1 end {ai, a2, ... , an is a compatible total ordering of S} EXAM PLE 26 Find a compatible total ordering for the po set ({1, 2, 4,5, 12, 20}, I). Solution: The first step is to choose a minimal element. This must be 1, because it is the only minimal element. Next, select a minimal element of({2, 4, 5, 12, 20}, I). There are two minimal elements in this poset, namely, 2 and 5. We select 5. The remaining elements are {2, 4, 12, 20}. The only minimal element at this stage is 2. Next, 4 is chosen because it is the only minimal element of({4, 12, 20}, I). Because both 12 and 20 are minimal elements of({12, 20}, I), either can be chosen next. We select 20, which leaves 12 as the last element left. This produces the total ordering 1 -< 5 -< 2 -< 4 -< 20 -< 12. The steps used by this sorting algorithm are displayed in Figure 9. Topological sorting has an application to the scheduling of projects. 12 20 12 20 12 20 12 20 4/ +1 4r V 4 2 5 2 5 2 1 Minimal element 1 5 2 4 chosen FIGURE 9 A Topological Sort of ({t, 2, 4, 5, t2, 20}, I). 12 • 20 20 12 • • 12 578 8 / Relations G D B A c E FIGURE 10 The Hasse Diagram for G G kl .(1 A C E C E Minimal element A C chosen 8-60 G G G G G .r1 ~ A � • E E B E F D G Seven Tasks. FIGURE 11 A Topological Sort ofthe Tasks. EXAMPLE 27 A development project at a computer company requires the completion of seven tasks. Some of these tasks can be started only after other tasks are finished. A partial ordering on tasks is set up by considering task X -< task Y if task Y cannot be started until task X has been completed. The Hasse diagram for the seven tasks, with respect to this partial ordering, is shown in Figure 10. Find an order in which these tasks can be carried out to complete the project. Solution: An ordering of the seven tasks can be obtained by performing a topological sort. The steps of a sort are illustrated in Figure 11. The result of this sort, A -< C -< B -< E -< F -< D -< G, gives one possible order for the tasks. � Exercises 1. Which of these relations on {O, 1,2, 3} are partial order­ ings? Determine the properties of a partial ordering that the others lack. a) {(O, 0), (1,1), (2, 2), (3, 3)} b) {(O, 0), (1, I), (2, 0), (2, 2), (2, 3), (3, 2), (3, 3)} c) {(O, 0), (1,1), (1, 2), (2, 2), (3, 3)} d) {(O, 0), (1,1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)} e) {(O, 0), (0, I), (0, 2), (1, 0), (I, I), (1, 2), (2, 0), (2,2), (3, 3)} 2. Which of these relations on {O, 1,2, 3} are partial order­ ings? Determine the properties of a partial ordering that the others lack. a) {(O, 0), (2, 2), (3, 3)} b) {(O, 0), (1, 1), (2, 0), (2, 2), (2, 3), (3, 3)} c) {(O, 0), (1,1), (1, 2), (2, 2), (3,1), (3, 3)} d) {(O, 0), (1,1), (1, 2), (1, 3), (2, 0), (2, 2), (2, 3), (3, 0), (3,3)} e) {(O, 0), (0,1), (0, 2), (0, 3), (I, D), (1, I), (I, 2), (I, 3), (2,0), (2,2), (3, 3)} 3. Is (S, R) a poset if S is the set of all people in the world and (a, b) E R, where a and b are people, if a) a is taller than b? b) a is not taller than b? c) a = b or a is an ancestor of b? d) a and b have a common friend? 4. Is (S, R) a poset if S is the set of all people in the world and (a, b) E R, where a and b are people, if a) a is no shorter than b? b) a weighs more than b? c) a = b or a is a descendant of b? d) a and b do not have a common friend? 5. Which of these are posets? a) (Z, =) b) (Z, #) c) (Z, ::::) d) (Z, f) 6. Which of these are posets? a) (R, =) b) (R, <) c) (R,::s) d) (R, #) 7. Determine whether the relations represented by these zero--one matrices are partial orders. 8-6/ 8. Determine whether the relations represented by these zer()--{)ne matrices are partial orders. [1 ° �] [� ° �] a) I b) I ° ° [1 ° 1 f] c) 1 1 ° 1 1 ° In Exercises 9-11 determine whether the relation with the directed graph shown is a partial order. � 1� 11. 12. Let (S, R) be a poset. Show that (S, R-1) is also a poset, where R -I is the inverse of R. The poset (S, R -I ) is called the dual of (S, R). 13. Find the duals of these posets. a) ({O, 1, 2), ::::) b) (Z,�) c) (P(Z),2) d) (Z+, I) 14. Which of these pairs of elements are comparable in the poset (Z+, I)? a) 5, 15 b) 6,9 c) 8, 16 d) 7,7 15. Find two incomparable elements in these posets. a) (P({0,1,2}),S;) b) ({1,2,4,6,8}'1) 16. Let S = {I, 2, 3, 4}. With respect to the lexicographic order based on the usual "less than" relation, a) find all pairs in S x S less than (2, 3). b) find all pairs in S x S greater than (3, 1). c) draw the Hasse diagram of the poset (S x S, �). 17. Find the lexicographic ordering ofthese n-tuples: a) (1,1,2), (1,2,1) b) (0,1,2,3), (0,1,3,2) c) (1,0,1,0,1),(0,1,1,1,0) 18. Find the lexicographic ordering of these strings oflower­ case English letters: a) quack, quick, quicksilver, quicksand, quacking b) open, opener, opera, operand, opened c) zoo, zero, zoom, zoology, zoological 19. Find the lexicographic ordering of the bit strings 0, 01, 11, 001,010, Oil, 0001, and 0101 based on the ordering 0< 1. 20. Draw the Hasse diagram for the "greater than or equal to" relation on to, 1,2,3,4, 5}. 21. Draw the Hasse diagram for the "less than or equal to" relation on to, 2, 5, 10, 11, 15}. 8.6 Partial Orderings 579 22. Draw the Hasse diagram for divisibility on the set a) {1,2,3,4,5,6}. b) {3,5,7, 11, 13, 16,17}. c) {2, 3, 5,10,11, IS, 25}. d) {I, 3, 9, 27, 81, 243}. 23. Draw the Hasse diagram for divisibility on the set a) {I, 2, 3, 4,5,6,7, 8}. b) {I, 2, 3, 5, 7,11, 13}. c) {I, 2, 3, 6, 12,24,36, 48}. d) {l, 2, 4, 8,16,32, 64}. 24. Draw the Hasse diagram for inclusion on the set peS), where S = {a, b, c, d}. In Exercises 25-27 list all ordered pairs in the partial ordering with the accompanying Hasse diagram. 25. 26. c d e d y blj , a a 27. >k b a c Let (S, �) be a poset. We say that an element YES covers an element XES if x -< y and there is no element z E S such that x -< z -< y. The set of pairs (x, y) such that y covers x is called the covering relation of (S, �). 28. What is the covering relation of the partial ordering {(a, b) I a divides b} on {l, 2,3,4,6, 121? 29. What is the covering relation of the partial ordering {(A, B) I A � B} on the power set of S, where S = {a, b, c}? 30. Show that the pair (x, y) belongs to the covering relation of the finite poset (S, �) if and only if x is lower than y and there is an edge joining x and y in the Hasse diagram of this poset. 31. Show that a finite poset can be reconstructed from its cov­ ering relation. [Hint: Show that the poset is the reflexive transitive closure of its covering relation.] 32. Answer these questions for the partial order represented by this Hasse diagram. m g f a b c 580 8 / Relations a) Find the maximal elements. b) Find the minimal elements. c) Is there a greatest element? d) Is there a least element? e) Find all upper bounds of {a, b, c}. I) Find the least upper bound of {a, b, c}, ifit exists. g) Find all lower bounds of {j, g, h}. h) Find the greatest lower bound of {f, g, h}, ifit exists. 33. Answer these questions for the poset ({3, 5, 9, IS, 24, 45}, I). a) Find the maximal elements. b) Find the minimal elements. c) Is there a greatest element? d) Is there a least element? e) Find all upper bounds of {3, 5}. I) Find the least upper bound of {3, S}, if it exists. g) Find all lower bounds of {IS, 4S}. h) Find the greatest lower bound of {IS, 4S}, ifit exists. 34. Answer these questions for the poset ({2, 4, 6, 9, 12, 18,27,36,48,60, 72l. I). a) Find the maximal elements. b) Find the minimal elements. c) Is there a greatest element? d) Is there a least element? e) Find all upper bounds of {2, 9}. I) Find the least upper bound of {2, 9}, ifit exists. g) Find all lower bounds of {60, 72}. h) Find the greatest lower bound of {60, 72}, if it exists. 35. Answer these questions for the poset ({ { I}, {2}, {4}, {I, 2}, {I,4}, {2,4}, {3, 4}, {I, 3, 4}, {2, 3, 4}}, �). a) Find the maximal elements. b) Find the minimal elements. c) Is there a greatest element? d) Is there a least element? e) Find all upper bounds of { {2}, {4}}. I) Find the least upper bound of {{2}, {4}}, if it exists. g) Find all lower bounds of {{I, 3, 4}, {2, 3, 4}}. h) Find the greatest lower bound of {{l, 3, 4}, {2, 3, 4}}, if it exists. 36. Give a po set that has a) a minimal element but no maximal element. b) a maximal element but no minimal element. c) neither a maximal nor a minimal element. 37. Show that lexicographic order is a partial ordering on the Cartesian product of two posets. 38. Show that lexicographic order is a partial ordering on the set of strings from a poset. 39. Suppose that (S, � \) and (T, �2) are posets. Show that (S x T, �) is a poset where (s, t) � (u, v) ifand only if s �1uandt �2V. 40. a) Show that there is exactly one greatest element of a poset, if such an element exists. b) Show that there is exactly one least element of a po set, if such an element exists. 8-62 41. a) Show that there is exactly one maximal element in a poset with a greatest element. b) Show that there is exactly one minimal element in a poset with a least element. 42. a) Show that the least upper bound of a set in a poset is unique if it exists. b) Show that the greatest lower bound of a set in a po set is unique if it exists. 43. Determine whether the posets with these Hasse diagrams are lattices. a) b) c) g h f f g h d d f g e e d e b c b c b c a a a 44. Determine whether these posets are lattices. a) ({I, 3, 6, 9, 12}, I) b) ({I, 5, 2S, 12S}, I) c) (Z,::::) d) (P(S), 2), where peS) is the power set of a set S 45. Show that every nonempty finite subset of a lattice has a least upper bound and a greatest lower bound. 46. Show that if the poset (S, R) is a lattice then the dual poset (S, R-1) is also a lattice. 47. In a company, the lattice model of information flow is used to control sensitive information with security classes rep­ resented by ordered pairs (A, C). Here A is an authority level, which may be nonproprietary (0), proprietary (1), restricted (2), or registered (3). A category C is a subset of the set of all projects {Cheetah, Impala, Puma}. (Names of animals are often used as code names for projects in companies.) a) Is information permitted to flow from (Proprietary, {Cheetah, Puma}) into (Restricted, {Puma})? b) Is information permitted to flow from (Restricted, {Cheetah}) into (Registered, {Cheetah, Impala})? c) Into which classes is information from (Proprietary, { Cheetah, Puma}) permitted to flow? d) From which classes is information permitted to flow into the security class (Restricted, {Impala, Puma})? 48. Show that the set S of security classes (A, C) is a lattice, where A is a positive integer representing an authority class and C is a subset of a finite set of compartments, with (A1, C1) � (A2, C2) ifand only if A1 ::'S A2 andC1 � C2. [Hint: First show that (S, �) is a poset and then show that the least upper bound and greatest lower bound of (A1,e1) and (A2,C2) are (max(A1,A2),C1 UC2) and (min(A1, A2), C1 n C2), respectively.] *49. Show that the set of all partitions of a set S with the re­ lation P1 � P2 if the partition P1 is a refinement of the partition P2 is a lattice. (See the preamble to Exercise 49 of Section 8.5.) 8-63 50. Show that every totally ordered set is a lattice. 51. Show that every finite lattice has a least element and a greatest element. 52. Give an example of an infinite lattice with a) neither a least nor a greatest element. b) a least but not a greatest element. c) a greatest but not a least element. d) both a least and a greatest element. 53. Verify that (Z+ x Z+, =<-) is a well-ordered set, where =<­ is lexicographic order, as claimed 'in Example 8. 54. Determine whether each of these po sets is well-ordered. a) (S, S), where S = {1O, II, 12, ... } b) (Q n [0, I], S) (the set of rational numbers between 0 and I inclusive) c) (S, S), where S is the set of positive rational numbers with denominators not exceeding 3 d) (Z-, 2:), where Z-is the set of negative integers A poset (R, =<-) is well-founded if there is no infinite de­ creasing sequence of elements in the poset, that is, elements XI, X2, ••• , Xn such that· .. -< Xn -< ... -< X2 -< XI. A poset (R, =<-) is dense iffor all XES and yES with X -< y, there is an element z E R such that X -< Z -< y. 55. Show that the poset (Z, =<-), where X -< Y if and only if Ixi < Iyl is well-founded but is not a totally ordered set. 56. Show that a dense poset with at least two elements that are comparable is not well-founded. 57. Show that the poset of rational numbers with the usual "less than or equal to" relation, (Q, S), is a dense poset. *58. Show that the set of strings of lowercase English let­ ters with lexicographic order is neither well-founded nor dense. 59. Show that a poset is well-ordered if and only if it is totally ordered and well-founded. 60. Show that a finite nonempty poset has a maximal element. 61. Find a compatible total order for the poset with the Hasse diagram shown in Exercise 32. 62. Find a compatible total order for the divisibility relation on the set {I, 2, 3, 6, 8, 12,24, 36}. Key Terms and Results TERMS binary relation from A to B: a subset of A x B relation on A: a binary relation from A to itself (i.e., a subset of A x A) So R: composite of Rand S R-1: inverse relation of R Rn: nth power of R reflexive: a relation R on A is reflexive if (a, a) E R for all aEA symmetric: a relation R on A is symmetric if(b, a) E R when­ ever (a, b) E R Key Terms and Results 581 63. Find an order different from that constructed in Ex­ ample 27 for completing the tasks in the development project. 64. Schedule the tasks needed to build a house, by specifying their order, if the Hasse diagram representing these tasks is as shown in the figure. Carpeting flooring Plumbing Interior fixtures Exterior fixtures Interior painting Exterior painting Wall-board Wiring Exterior siding Roof Framing Foundation 65. Find an ordering ofthe tasks of a software project if the Hasse diagram for the tasks of the proj ect is as shown. Develop module A Write documentation Completion f3 test Setup test sites Write functional requirements Determine user needs antisymmetric: a relation R on A is anti symmetric if a = b whenever (a, b) E Rand (b, a) E R transitive: a relation R on A is transitive if (a, b) E Rand (b, c) E R implies that (a, c) E R n-ary relation on At, A2, ••• , An: a subset of Al x A2 X ... x An relational data model: a model for representing databases using n-ary relations primary key: a domain of an n-ary relation such that an n-tuple is uniquely determined by its value for this domain 582 8 / Relations composite key: the Cartesian product of domains of an n-ary relation such that an n-tuple is uniquely determined by its values in these domains selection operator: a function that selects the n-tuples in an n-ary relation that satisfy a specified condition projection: a function that produces relations of smaller de­ gree from an n-ary relation by deleting fields join: a function that combines n-ary relations that agree on certain fields directed graph or digraph: a set of elements called vertices and ordered pairs of these elements, called edges loop: an edge oftheform (a, a) closure of a relation R with respect to a property P: the relation S (ifit exists) that contains R, has property P, and is contained within any relation that contains R and has property P path in a digraph: a sequence of edges (a, Xl), (Xl, X2), ... , (Xn-2, Xn-l), (Xn-l, b) such that the terminal vertex of each edge is the initial vertex of the succeeding edge in the sequence circuit (or cycle) in a digraph: a path that begins and ends at the same vertex R* (connectivity relation): the relation consisting of those ordered pairs (a, b) such that there is a path from a to b equivalence relation: a reflexive, symmetric, and transitive relation equivalent: if R is an equivalence relation, a is equivalent to b ifaRb lab (equivalence class of a with respect to R): the set of all elements of A that are equivalent to a [aIm (congruence class modulo m): the set of integers con­ gruent to a modulo m partition of a set S: a collection of pairwise disj oint non empty subsets that have S as their union partial ordering: a relation that is reflexive, anti symmetric, and transitive poset (S, R): a set S and a partial ordering R on this set comparable: the elements a and b in the poset (A, �) are comparable if a � b or b � a incomparable: elements in a poset that are not comparable total (or linear) orderin g: a partial ordering for which every pair of elements are comparable totally (or linearly) ordered set: a poset with a total (or linear) ordering well-ordered set: a poset (S, �), where � is a total order and every nonempty subset of S has a least element Review Questions 1. a) What is a relation on a set? b) How many relations are there on a set with n elements? 2. a) What is a reflexive relation? b) What is a symmetric relation? c) What is an anti symmetric relation? d) What is a transitive relation? 8-64 lexicographic order: a partial ordering of Cartesian products or strings (see pages 569-570) Hasse diagram: a graphical representation of a poset where loops and all edges resulting from the transitive property are not shown, and the direction of the edges is indicated by the position ofthe vertices maximal element: an element of a poset that is not less than any other element of the poset minimal element: an element of a poset that is not greater than any other element of the poset greatest element: an element of a poset greater than or equal to all other elements in this set least element: an element of a poset less than or equal to all other elements in this set upper bound of a set: an element in a poset greater than all other elements in the set lower bound of a set: an element in a poset less than all other elements in the set least upper bound of a set: an upper bound of the set that is less than all other upper bounds greatest lower bound of a set: a lower bound of the set that is greater than all other lower bounds lattice: a partially ordered set in which every two elements have a greatest lower bound and a least upper bound compatible total ordering for a partial ordering: a total or­ dering that contains the given partial ordering topological sort: the construction of a total ordering compat­ ible with a given partial ordering RESULTS The reflexive closure of a relation R on the set A equals R U Ll, where Ll = {(a, a) I a E A}. The symmetric closure of a relation R on the set A equals R U R-l, where R-l = {(b, a) I (a, b) E R}. The transitive closure of a relation equals the connectivity re­ lation formed from this relation. Warshall's algorithm for finding the transitive closure of a re­ lation (see pages 550--553). Let R be an equivalence relation. Then the following three statements are equivalent: (I) aRb; (2) [aJR n [b]R #-0; (3) [a]R = [bJR. The equivalence classes of an equivalence relation on a set A form a partition of A. Conversely, an equivalence relation can be constructed from any partition so that the equiva­ lence classes are the subsets in the partition. The principle of well-ordered induction. The topological sorting algorithm (see pages 576-578). 3. Give an example of a relation on the set {I, 2, 3, 4} that is a) reflexive, symmetric, and not transitive. b) not reflexive, symmetric, and transitive. c) reflexive, anti symmetric, and not transitive. d) reflexive, symmetric, and transitive. e) reflexive, antisymmetric, and transitive. 8-65 4. a) How many reflexive relations are there on a set with n elements? b) How many symmetric relations are there on a set with n elements? c) How many anti symmetric relations are there on a set with n elements? 5. a) Explain how an n-ary relation can be used to represent information about students at a university. b) How can the 5-ary relation containing names of stu­ dents, their addresses, telephone numbers, majors, and grade point averages be used to form a 3-ary relation containing the names of students, their majors, and their grade point averages? c) How can the 4-ary relation containing names of stu­ dents, their addresses, telephone numbers, and majors and the 4-ary relation containing names of students, their student numbers, majors, and numbers of credit hours be combined into a single n-ary relation? 6. a) Explain how to use a zero-one matrix to represent a relation on a finite set. b) Explain how to use the zero-one matrix representing a relation to determine whether the relation is reflexive, symmetric, and/or antisymmet ric. 7. a) Explain how to use a directed graph to represent a relation on a finite set. b) Explain how to use the directed graph representing a relation to determine whether a relation is reflexive, symmetric, and/or antisymmet ric. 8. a) Define the reflexive closure and the symmetric closure of a relation. b) How can you construct the reflexive closure of a relation? c) How can you construct the symmetric closure of a relation? d) Find the reflexive closure and the symmetric closure of the relation {(I, 2), (2, 3), (2, 4), (3, I)} on the set {I, 2, 3, 4}. 9. a) Define the transitive closure of a relation. b) Can the transitive closure of a relation be obtained by including all pairs (a, c) such that (a, b) and (b, c) be­ long to the relation? c) Describe two algorithms for finding the transitive clo­ sure of a relation. d) Find the tranSItIve closure of the relation {(l,l), (1,3), (2,1), (2,3), (2,4), (3,2), (3,4), (4,1)}. Supplemen tary Exercises 1. Let S be the set of all strings of English letters. Determine whether these relations are reflexive, irreflexive, symmet­ ric, antisymmetric, and/or transitive. a) RI = {(a, b) I a and b have no letters in common } b) R2 = {(a, b) I a and b are not the same length} c) R3 = {(a, b) I a is longer than b} 2. Construct a relation on the set {a, b, c, d} that is Supplementary Exercises 583 10. a) Define an equivalence relation. b) Which relations on the set {a, b, c, d} are equivalence relations and contain (a, b) and (b, d)? 11. a) Show that congruence modulo m is an equivalence re­ lation whenever m is a positive integer. b) Show that the relation {(a, b) I a == ±b (mod 7)} is an equivalence relation on the set of integers. 12. a) What are the equivalence classes of an equivalence relation? b) What are the equivalence classes of the "congruent modulo 5" relation? c) What are the equivalence classes of the equivalence relation in Question I I (b)? 13. Explain the relationship between equivalence relations on a set and partitions of this set. 14. a) Define a partial ordering. b) Show that the divisibility relation on the set of positive integers is a partial order. 15. Explain how partial orderings on the sets Al and A2 can be used to define a partial ordering on the set Al x A2. 16. a) Explain how to construct the Hasse diagram of a partial order on a finite set. b) Draw the Hasse diagram ofthe divisibility relation on the set {2, 3, 5, 9, 12, IS, 18}. 17 . a) Define a maximal element of a poset and the greatest element of a po set. b) Give an example of a poset that has three maximal elements. c) Give an example of a poset with a greatest element. 18. a) Define a lattice. b) Give an example of a poset with five elements that is a lattice and an example of a poset with five elements that is not a lattice. 19. a) Show that every finite subset of a lattice has a greatest lower bound and a least upper bound. b) Show that every lattice with a finite number of elements has a least element and a greatest element. 20. a) Define a well-ordered set. b) Describe an algorithm for producing a well-ordered set from a partially ordered set. . c) Explain how the algorithm from (b) can be used to order the tasks in a project if each task can be done only after one or more of the other tasks have been completed. a) reflexive, symmetric, but not transitive. b) irreflexive, symmetric, and transitive. c) irreflexive, antisymmetric, and not transitive. d) reflexive, neither symmetric nor anti symmetric, and transitive. e) neither reflexive, irreflexive, symmetric, antisymmet­ ric, nor transitive. 584 8 / Relations 3. Show that the relation R on Z x Z defined by (a, b) R (c, d) if and only if a + d = b + c is an equiva­ lence relation. 4. Show that a subset of an anti symmetric relation is also anti symmetric . 5. Let R be a reflexive relation on a set A. Show that R � R2. 6. Suppose that R\ and R2 are reflexive relations on a set A. Show that R\ EB R2 is irreflexive. 7. Suppose that R\ and R2 are reflexive relations on a set A. Is R\ n R2 also reflexive? Is R\ U R2 also reflexive? 8. Suppose that R is a symmetric relation on a set A. Is R also symmetr ic?· 9. Let R\ and R2 be symmetric relations. Is R\ n R2 also symmetric? Is R\ U R2 also symmetric? 10. A relation R is called circular if aRb and b R c imply that eRa. Show that R is reflexive and circular if and only if it is an equivalence relation. 11. Show that a primary key in an n-ary relation is a primary key in any projection ofthis relation that contains this key as one of its fields. 12. Is the primary key in an n-ary relation also a primary key in a larger relation obtained by taking the join of this relation with a second relation? 13. Show that the reflexive closure of the symmetric closure of a relation is the same as the symmetric closure of its reflexive closure. 14. Let R be the relation on the set of all mathematicians that contains the ordered pair (a, b) if and only if a and b have written a paper together. a) Describe the relation R2. unkS� 8-66 b) Describe the relation R*. c) The Erdos number of a mathematician is I if this mathematician wrote a paper with the prolific Hun­ garian mathematician Paul Erdos, it is 2 if this math­ ematician did not write a joint paper with Erdos but wrote a joint paper with someone who wrote a joint paper with Erdos, and so on (except that the Erdos number of Erdos himself is 0). Give a definition ofthe Erdos number in terms of paths in R. 15. a) Give an example to show that the transitive closure of the symmetric closure of a relation is not necessarily the same as the symmetric closure of the transitive closure of this relation. b) Show, however, that the transitive closure of the sym­ metric closure of a relation must contain the symmetric closure of the transitive closure of this relation. 16. a) Let S be the set of subroutines of a computer pro­ gram. Define the relation R by P R Q if subroutine P calls subroutine Q during its execution. Describe the transitive closure of R. b) For which subroutines P does (P, P) belong to the transitive closure of R? c) Describe the reflexive closure of the transitive closure of R. 17. Suppose that R and S are relations on a set A with R � S such that the closures of Rand S with respect to a prop­ erty P both exist. Show that the closure of R with respect to P is a subset of the closure of S with respect to P. 18. Show that the symmetric closure of the union of two re­ lations is the union of their symmetric closures. PAUL ERDOS (1913-1996) Paul Erdos, born in Budapest, Hungary, was the son of two high school mathemat ics teachers. He was a child prodigy; at age 3 he could multiply three-digit numbers in his head, and at 4 he discovered negative numbers on his own. Because his mother did not want to expose him to contagious diseases, he was mostly home-schooled. At 17 Erdos entered Eotvos University, graduating four years later with a Ph.D. in mathema tics. After graduating he spent four years at Manchester, England, on a postdoctoral fellowship. In 1938 he went to the United States because of the difficult political situation in Hungary, especially for Jews. He spent much of his time in the United States, except for 1954 to 1962, when he was banned as part ofthe paranoia ofthe McCarthy era. He also spent considerable time in Israel. Erdos made many significant contributions to combinatorics and to number theory. One of the discoveries of which he was most proud is his elementary proof (in the sense that it does not use any complex analysis) of the Prime Number Theorem, which provides an estimate for the number of primes not exceeding a fixed positive integer. He also participated in the modem development of the Ramsey theory. Erdos traveled extensively throughout the world to work with other mathema ticians, visiting conferences, universities, and research laboratori es. He had no permanent home. He devoted himself almost entirely to mathematics, traveling from one mathe­ matician to the next, proclaiming "My brain is open." Erdos was the author or coauthor of more than 1500 papers and had more than 500 coauthors. Copies of his articles are kept by Ron Graham, a famous discrete mathemat ician with whom he collaborated extensively and who took care of many of his worldly needs. Erdos offered rewards, ranging from $10 to $10,000, for the solution of problems that he found particularly interesting, with the size ofthe reward depending on the difficulty ofthe problem. He paid out close to $4000. Erdos had his own special language, using such terms as "epsilon" (child), "boss" (woman), "slave" (man), "captured" (married), "liberated" (divorced), "Supreme Fascist" (God), "Sam" (United States), and "Joe" (Soviet Union). Although he was curious about many things, he concentrated almost all his energy on mathematical research. He had no hobbies and no full-time job. He never married and apparently remained celibate. Erdos was extremely generous, donating much of the money he collected from prizes, awards, and stipends for scholarships and to worthwhile causes. He traveled extremely lightly and did not like having many material possessions. 8-67 *19. Devise an algorithm, based on the concept of interior ver­ tices, that finds the length ofthe longest path between two vertices in a directed graph, or determines that there are arbitrarily long paths between these vertices. 20. Which of these are equivalence relations on the set of all people? a) {(x, y) I x andy have the same sign of the zodiac} b) {(x, y) I x and y were born in the same year} c) {(x, y) I x and y have been in the same city} *21. How many different equivalence relations with exactly three different equivalence classes are there on a set with five elements? 22. Show that {(x, y) I x -Y E Q} is an equivalence relation on the set of real numbers, where Q denotes the set of rational numbers. What are [1], [�], and [JT]? 23. Suppose that PI = {AI, A2, ... , Am} and P2 = {BI, B2, ••• , Bn} are both partitions of the set S. Show that the collection of nonempty subsets of the form Ai n Bj is a partition of S that is a refinement of both PI and P2 (see the preamble to Exercise 49 of Section 8.5). *24. Show that the transitive closure ofthe symmetric closure of the reflexive closure of a relation R is the smallest equivalence relation that contains R. 25. Let R( S) be the set of all relations on a set S. Define the relation � on R(S) by RI � R2 if RI S; R2, where RI and R2 are relations on S. Show that (R(S), �) is a poset. 26. Let P( S) be the set of all partitions ofthe set S. Define the relation � on peS) by PI � P2 if PI is a refinement of P2 (see Exercise 49 of Section 8.5). Show that (P(S), �) is a poset. 27. Schedule the tasks needed to cook a Chinese meal by specifying their order, if the Hasse diagram representing these tasks is as shown here. Steam rice Find recipe Cut fish Clean fish Supplementary Exercises 585 A subset of a poset such that every two elements of this sub­ set are comparable is called a chain. A subset of a poset is called an antichain if every two elements of this subset are incomparable. 28. Find all chains in the po sets with the Hasse diagrams shown in Exercises 25-27 in Section 8.6. 29. Find all antichains in the po sets with the Hasse diagrams shown in Exercises 25-27 in Section 8.6. 30. Find an anti chain with the greatest number of elements in the poset with the Hasse diagram of Exercise 32 in Section 8.6. 31. Show that every maximal cha,in in a finite poset (S, �) contains a minimal element of S. (A maximal chain is a chain that is not a subset of a larger chain.) **32. Show that every finite poset can be partitioned into k chains, where k is the largest number of elements in an antichain in this poset. *33. Show that in any group ofmn + 1 people there is either a list of m + 1 people where a person in the list (except for the first person listed) is a descendant of the previous per­ son on the list, or there are n + 1 people such that none of these people is a descendant of any of the other n people. [Hint: Use Exercise 32.] Suppose that (S, �) is a well-founded partially ordered set. The principle of well-founded induction states that P(x) is true for all XES ifVx(Vy(y -< x -+ P(y» -+ P(x». 34. Show that no separate basis case is needed for the prin­ ciple of well-founded induction. That is, P(u) is true for all minimal elements u in S ifVx(Vy(y -< x -+ P(y» -+ P(x». *35. Show that the principle of well-founded induction is valid. A relation R on a set A is a quasi-ordering on A if R is reflexive and transitive. 36. Let R be the relation on the set of all functions from Z+ to Z+ such that (f, g) belongs to R if and only if f is O(g). Show that R is a quasi-ordering. 37. Let R be a quasi-ordering on a set A. Show that R n R-I is an equivalence relation. *38. Let R be a quasi-ordering and let S be the relation on the set of equivalence classes of R n R -I such that (C, D) belongs to S, where C and D are equivalence classes of R, if and only if there are elements c of C and d of D such that (c, d) belongs to R. Show that S is a partial ordering. Let L be a lattice. Define the meet (!\) and join (v) operations by x !\ Y = glb(x, y) and x v y = lub(x, y). 39. Show that the following properties hold for all elements x, y, and z of a lattice L. a) x !\ y = y!\ x and x v y = y v x (commutative laws) b) (x !\ y)!\ z = x !\ (y !\ z) and (x v y) v z = x V (y V z) (associative laws) c) x!\ (x V y) = x and x v (x !\ y) = x (absorption laws) d) x !\ x = x and x v x = x (idempotent laws) 586 8 I Relations 40. Show that if x and yare elements of a lattice L, then x v y = y if and only if x /\ y = x. A lattice L is bounded if it has both an upper bound, de­ noted by I, such that x � I for all x ELand a lower bound, denoted by 0, such that 0 � x for all x E L. 41. Show that if L is a bounded lattice with upper bound I and lower bound 0 then these properties hold for all elements x E L. a) x v I = I c) x v 0 = x b) x /\ I = x d) x /\ 0 = 0 42. Show that every finite lattice is bounded. A lattice is called distributive if x v (y /\ z) = (x v y) /\ (x V z) and x /\ (y V z) = (x /\y) V (x /\z) for all x, y, and z in L. *43. Give an example of a lattice that is not distributive. 44. Show that the lattice (P(S), �) where P( S) is the power set of a finite set S is distributive. 45. Is the lattice (Z+, I) distributive? The complement of an element a of a bounded lattice L with upper bound I and lower bound 0 is an element b such that a v b = I and a /\ b = O. Such a lattice is complemented if every element of the lattice has a complement. Computer Projects Write programs with these input and output. 1. Given the matrix representing a relation on a finite set, determine whether the relation is reflexive and/or irreflexive. 2. Given the matrix representing a relation on a finite set, determine whether the relation is symmetric and/or anti symmetric . 3. Given the matrix representing a relation on a finite set, determine whether the relation is transitive. 4. Given a positive integer n, display all the relations on a set with n elements. *5. Given a positive integer n, determine the number oftran­ sitive relations on a set with n elements. *6. Given a positive integer n, determine the number of equiv­ alence relations on a set with n elements . *7. Given a positive integer n, display all the equiva­ lence relations on the set of the n smallest positive integers. 8. Given an n-ary relation, find the projection of this relation when specified fields are deleted. 9. Given an m-ary relation and an n-ary relation, and a set of 8-68 46. Give an example of a finite lattice where at least one element has more than one complement and at least one element has no complement. 47. Show that the lattice (P(S), �) where P(S) is the power set of a finite set S is complemented. *48. Show that if L is a finite distributive lattice, then an ele- ment of L has at most one complement. The game of Chomp, introduced in Example 12 in Section I. 7, can be generalized for play on any finite partially ordered set (S, �) with a least element a. In this game, a move consists of selecting an element x in S and removing x and all elements larger than it from S. The loser is the player who is forced to select the least element a. 49. Show that the game of Chomp with cookies arranged in an m x n rectangular grid, described in Example 12 in Section 1.7, is the same as the game of Chomp on the po set (S, I), where S is the set of all positive inte­ gers that divide pm-lqn-l, where p and q are distinct primes. 50. Show that if (S,�) has a greatest element b, then a winning strategy for Chomp on this po set exists. [Hint: Generalize the argument in Example 12 in Sec­ tion 1.7.] common fields, find the join ofthese relations with respect to these common fields. 10. Given the matrix representing a relation on a finite set, find the matrix representing the reflexive closure of this relation. 11. Given the matrix representing a relation on a finite set, find the matrix representing the symmetric closure of this relation. 12. Given the matrix representing a relation on a finite set, find the matrix representing the transitive closure of this relation by computing the join ofthe powers of the matrix representing the relation. 13. Given the matrix representing a relation on a finite set, find the matrix representing the transitive closure of this relation using Warshall's algorithm. 14. Given the matrix representing a relation on a finite set, find the matrix representing the smallest equivalence relation containing this relation. 15. Given a partial ordering on a finite set, find a total ordering compatible with it using topological sorting. 8-69 Writing Projects 587 Computa tions and Explorations Use a computational program or programs you have written to do these exercises. 1. Display all the different relations on a set with four elements. 2. Display all the different reflexive and symmetric relations on a set with six elements. 3. Display all the reflexive and transitive relations on a set with five elements. '4. Determine how many transitive relations there are on a set with n elements for all positive integers n with n ::: 7. 5. Find the transitive closure of a relation of your choice on a set with at least 20 elements. Either use a relation that Writing Projects corresponds to direct links in a particular transportation or communications network or use a randomly generated relation. 6. Compute the number of different equivalence relations on a set with n elements for all positive integers n not exceeding 20. 7. Display all the equivalence relations on a set with seven elements. *8. Display all the partial orders on a set with five elements. *9. Display all the lattices on a set with five elements. Respond to these questions with essays using outside sources. 1. Discuss the concept of a fuzzy relation. How are fuzzy relations used? 2. Describe the basic principles of relational databases, go­ ing beyond what was covered in Section 8.2. How widely used are relational databases as compared with other types of databases? 3. Look up the original papers by Warshall and by Roy (in French) in which they develop algorithms for finding tran­ sitive closures. Discuss their approaches. Why do you suppose that what we call Warshall's algorithm was dis­ covered independently by more than one person? 4. Describe how equivalence classes can be used to define the rational numbers as classes of pairs of integers and how the basic arithmetic operations on rational numbers can be defined following this approach. (See Exercise 40 in Section 8.5.) 5. Explain how Helmut Hasse used what we now call Hasse diagrams. 6. Describe some of the mechanisms used to enforce infor­ mation flow policies in computer operating systems. 7. Discuss the use of the Program Evaluation and Review Technique (PERT) to schedule the tasks of a large com­ plicated project. How widely is PERT used? 8. Discuss the use ofthe Critical Path Method (CPM) to find the shortest time for the completion of a project. How widely is CPM used? 9. Discuss the concept of duality in a lattice. Explain how duality can be used to establish new results. 10. Explain what is meant by a modular lattice. Describe some of the properties of modular lattices and describe how modular lattices arise in the study of projective geometry. CHAPTER 9.1 Graphs and Graph Models 9.2 Graph Termi­ nologyand Special Types of Graphs 9.3 Representing Graphs and Graph Isomorphism 9.4 Connectivity 9.5 Euler and Hamilton Paths 9.6 Shortest-Path Problems 9.7 Planar Graphs 9.8 Graph Coloring Graphs � raphs are discrete structures consisting of vertices and edges that connect these verti ces. 'l.::D" Problems in almost every conceivable discipline can be solved using graph models. We will give examples to show how graphs are used as models in a variety of areas. For instance, we will show how graphs are used to represent the competition of different species in an ecological niche, how graphs are used to represent who influences whom in an organization, and how graphs are used to represent the outcomes of round-robin tournaments. We will describe how graphs can be used to model acquainta nceships between people, collaboration between researchers, telephone calls between telephone numbers, and links between websites. We will show how graphs can be used to model roadmaps and the assignment of jobs to employees of an organization. Using graph models, we can determine whether it is possible to walk down all the streets in a city without going down a street twice, and we can find the number of colors needed to color the regions of a map. Graphs can be used to determine whether a circuit can be imple­ mented on a planar circuit board. We can distinguish between two chemical compounds with the same molecular formula but different structures using graphs. We can determine whether two computers are connected by a communications link using graph models of computer networks. Graphs with weights assigned to their edges can be used to solve problems such as finding the shortest path between two cities in a transportation network. We can also use graphs to schedule exams and assign channels to television stations. This chapter will introduce the basic concepts of graph theory and present many different graph models. To solve the wide variety of problems that can be studied using graphs, we will introduce many different graph algorithms. We will also study the complexity of these algorithms. 9.1 Graphs and Graph Models DEFIN ITION 1 9-/ We begin with the definition of a graph. A graph G = (V, E) consists of V, a nonempty set of vertices (or nodes) and E, a set of edges. Each edge has either one or two vertices associated with it, called its endpoints. An edge is said to connect its endpoints. Remark: The set of vertices V ofa graph G may be infinite. A graph with an infinite vertex set is called an infinite graph, and in comparison, a graph with a finite vertex set is called a finite graph. In this book we will usually consider only finite graphs. Now suppose that a network is made up of data centers and communication links between computers. We can represent the location of each data center by a point and each communications link by a line segment, as shown in Figure 1. This computer network can be modeled using a graph in which the vertices of the graph represent the data centers and the edges represent communication links.ln general, we visualize graphs by using points to represent vertices and line segments, possibly curved, to represent edges, where the endpoints of a line segment representing an edge are the points representing the 589 590 9 / Graphs Detroit --41EE":::::::::�:::::=� New York San Francisco Denver Los Angeles FIGURE 1 A Computer Network. Washington 9-2 endpoints of the edge. When we draw a graph, we generally try to draw edges so that they do not cross. However, this is not necessary because any depiction using points to represent vertices and any form of connection between vertices can be used. Indeed, there are some graphs that cannot be drawn in the plane without edges crossing (see Section 9.7). The key point is that the way we draw a graph is arbitrary, as long as the correct connections between vertices are depicted. Note that each edge of the graph representing this computer network connects two different vertices. That is, no edge connects a vertex to itself. Furthermore, no two different edges connect the same pair of vertices. A graph in which each edge connects two different vertices and where no two edges connect the same pair of vertices is called a simple graph. Note that in a simple graph, each edge is associated to an unordered pair of vertices, and no other edge is associated to this same edge . Consequently, when there is an edge of a simple graph associated to {u, v}, we can also say, without possible confusion, that {u, v} is an edge of the graph. A computer network may contain multiple links between data centers, as shown in Figure 2. To model such networks we need graphs that have more than one edge connecting the same pair of vertices. Graphs that may have multiple edges connecting the same vertices are called multigraphs. When there are m different edges associated to the same unordered pair of vertices {u, v}, we also say that {u, v} is an edge of multiplicity m. That is, we can think of this set of edges as m different copies of an edge {u, v}. Sometimes a communications link connects a data center with itself, perhaps a feedback loop for diagnostic purposes. Such a network is illustrated in Figure 3. To model this network we need to include edges that connect a vertex to itself. Such edges are called loops. Graphs that may include loops, and possibly multiple edges connecting the same pair of vertices, are sometimes called pseudographs. So far the graphs we have introduced are undirected graphs. Their edges are also said to be undirected. However, to construct a graph model, we may find it necessary to assign directions to the edges of a graph. For example, in a computer network, some links may operate in only one direction (such links are called single duplex lines). This may be the case if there is a large amount of traffic sent to some data centers, with little or no traffic going in the opposite direction. Such a network is shown in Figure 4. To model such a computer network we use a directed graph. Each edge of a directed graph is associated to an ordered pair. The definition of directed graph we give here is more general than the one we used in Chapter 8, where we used directed graphs to represent relations. San Francisco Denver Los Angeles Detroit New York Washington FIGURE 2 A Computer Network with Multiple Links between Data Centers. 9-3 DEFINITION 2 unkS� San Francisco Washington Los Angeles FIGURE 3 A Computer Network with Diagnostic Links. 9.1 Graphs and Graph Models 591 A directed graph (or digraph) (V, E) consists ofa nonempty set of vertices V and a set of directed edges (or arcs) E. Each directed edge is associated with an ordered pair of vertices. The directed edge associated with the ordered pair Cu, v) is said to start at u and end at v. When we depict a directed graph with a line drawing, we use an arrow pointing from u to v to indicate the direction of an edge that starts at u and ends at v. A directed graph may contain loops and it may contain multiple directed edges that start and end at the same vertices. A directed graph may also contain directed edges that connect vertices u and v in both directions; that is, when a digraph contains an edge from u to v, it may also contain one or more edges from v to u. Note that we obtain a directed graph when we assign a direction to each edge in an undirected graph. When a directed graph has no loops and has no multiple directed edges, it is called a simple directed graph. Because a simple directed graph has at most one edge associated to each ordered pair of vertices (u, v), we call (u, v) an edge ifthere is an edge associated to it in the graph. In some computer networks, multiple communication links between two data centers may be present, as illustrated in Figure 5. Directed graphs that may have multiple directed edges from a vertex to a second (possibly the same) vertex are to used model such networks. We called such graphs directed multigrap hs. When there are m directed edges, each associated to an ordered pair of vertices (u, v), we say that (u, v) is an edge of multiplicity m. For some models we may need a graph where some edges are undirected, while others are directed. A graph with both directed and undirected edges is called a mixed graph. For example, a mixed graph might be used to model a computer network containing links that operate in both directions and other links that operate only in one direction. This terminology for the various types of graphs is summarized in Table 1. We will some­ times use the term graph as a general term to describe graphs with directed or undirected edges (or both), with or without loops, and with or without multiple edges. At other times, when the context is clear, we will use the term graph to refer only to undirected graphs. Because ofthe relatively modem interest in graph theory, and because it has applications to a wide variety of disciplines, many different terminologies of graph theory have been introduced. The reader should determine how such terms are being used whenever they are encountered. The Detroit � ....... � New York San Francisco Denver Washington Los Angeles FIGURE 4 A Communications Network with One-Way Communications Links. 592 9 / Graphs unllS� EXAMPLE 1 UnIlS� EXAMPLE 2 linllS� Detroit Los Angeles New York FIGURE 5 A Computer Network with Multiple One-Way Links. 9-4 terminology used by mathemati cians to describe graphs has been increasingly standardized, but the terminology used to discuss graphs when they are used in other discplines is still quite varied. Although the terminology used to describe graphs may vary, three key questions can help us understand the structure of a graph: • Are the edges of the graph undirected or directed (or both)? • Ifthe graph is undirected, are multiple edges present that connect the same pair of vertices? If the graph is directed, are multiple directed edges present? • Are loops present? Answering such questions helps us understand graphs. It is less important to remember the particular terminology used. Graph Models Graphs are used in a wide variety of models. We will present a few graph models from diverse fields here. Others will be introduced in subsequent sections of this and later chapters. When we build a graph model, we need to make sure that we have correctly answered the three key questions we posed about the structure of a graph. Niche Overlap Graphs in Ecology Graphs are used in many models involving the interaction of different species of animals. For instance, the competition between species in an ecosystem can be modeled using a niche overlap graph. Each species is represented by a vertex. An undirected edge connects two vertices if the two species represented by these vertices compete (that is, some of the food resources they use are the same). A niche overlap graph is a simple graph because no loops or multiple edges are needed in this model. The graph in Figure 6 models the ecosystem of a forest. We see from this graph that squirrels and raccoons compete but that crows and shrews do not. � Acquaintanceship Graphs We can use graph models to represent various relationships be­ tween people. For example, we can use a simple graph to represent whether two people know TABLE 1 Graph Terminology. Type Edges Multiple Edges Allowed? Loops Allowed? Simple graph Undirected No No Multigraph Undirected Yes No Pseudograph Undirected Yes Yes Simple directed graph Directed No No Directed multi graph Directed Yes Yes Mixed graph Directed and undirected Yes Yes 9-5 9.1 Graphs and Graph Models 593 Raccoon .... --- ..... --- ->e Eduardo Owl Kamlesh Opossum �---�--- ... Crow Ching Shrew...---- .. Mouse Woodpecker Koko Kari Shaquira FIGURE 6 A Niche Overlap Graph. FIGURE 7 An Acquaintanceship Graph. EXAM PLE 3 EXAM PLE 4 unkS� EXAMPLE S EXAMPLE 6 unkS� each other, that is, whether they are acquainted. Each person in a particular group of people is represented by a vertex. An undirected edge is used to connect two people when these peo­ ple know each other. No multiple edges and usually no loops are used. (If we want to include the notion of self-knowledge, we would include loops.) A small acquaintanceship graph is shown in Figure 7. The acquaintanceship graph of all people in the world has more than six billion vertices and probably more than one trillion edges! We will discuss this graph further in Section 9.4. .... Influence Graphs In studies of group behavior it is observed that certain people can influence the thinking of others. A directed graph called an influence graph can be used to model this behavior. Each person of the group is represented by a vertex. There is a directed edge from vertex a to vertex b when the person represented by vertex a influences the person represented by vertex b. This graph does not contain loops and it does not contain multiple directed edges. An example of an influence graph for members of a group is shown in Figure 8. In the group modeled by this influence graph, Deborah can influence Brian, Fred, and Linda, but no one can influence her. Also, Yvonne and Brian can influence each other. .... The Hollywood Graph The Hollywood graph represents actors by vertices and connects two vertices when the actors represented by these vertices have worked together on a movie. This graph is a simple graph because its edges are undirected, it contains no multiple edges, and it contains no loops. According to the Internet Movie Database, in Janurary 2006 the Hollywood graph had 637,099 vertices representing actors who have appeared in 339,896 films, and had more than 20 million edges. We will discuss some aspects of the Hollywood graph later in Section 9.4. .... Round-Robin Tournaments A tournament where each team plays each other team exactly once is called a round-robin tournament. Such tournaments can be modeled using directed graphs where each team is represented by a vertex. Note that (a, b) is an edge if team a beats team b. This graph is a simple directed graph, containing no loops or multiple directed edges (because no two teams play each other more than once). Such a directed graph model is presented in Figure 9. We see that Team 1 is undefeated in this tournament, and Team 3 is winless. .... Collaboration Graphs A graph called a collaboration graph can be used to model joint authorship of academic papers. In a collaboration graph, vertices represent people (perhaps restricted to members of a certain academic community) and edges link two people if they have jointly written a paper. This graph is a simple graph because it contains undirected edges and 594 9 / Graphs EXAMPLE 7 UnkS� EXAMPLE S UnkS� Linda Brian Deborah Fred Yvonne FIGURE 8 An Influence Graph. Team Team 2 Team Team 6 ������ 3 Team 5 Team 4 FIGURE 9 A Graph Model of a Round-Robin Tournament. 9-6 has no loops or multiple edges. The collaboration graph for people working together on research papers in mathema tics has been found to have more than 400,000 vertices and 675,000 edges. We will have more to say about this graph in Section 9.4. ... Call Graphs Graphs can be used to model telephone calls made in a network, such as a long­ distance telephone network. In particular, a directed multi graph can be used to model calls where each telephone number is represented by a vertex and each telephone call is represented by a directed edge. The edge representing a call starts at the telephone number from which the call was made and ends at the telephone number to which the call was made. We need directed edges because the direction in which the call is made matters. We need multiple directed edges because we want to represent each call made from a particular telephone number to a second number. A small telephone call graph is displayed in Figure lO(a), representing seven telephone numbers. This graph shows, for instance, that three calls have been made from 732-555-1234 to 732-555-9876 and two in the other direction, but no calls have been made from 732-555-4444 to any of the other six numbers except 732-555-0011. When we care only whether there has been a call connecting two telephone numbers, we use an undirected graph with an edge connecting telephone numbers when there has been a call between these numbers. This version of the call graph is displayed in Figure 1 O(b). Call graphs that model actual calling activities can be huge. For example, one call graph studied at AT&T, which models calls during 20 days, has about 290 million vertices and 4 billion edges. We will discuss call graphs further in Section 9.4. ... The Web Graph The World Wide Web can be modeled as a directed graph where each Web page is represented by a vertex and where an edge starts at the Web page a and ends at the Web page b if there is a link on a pointing to b. Because new Web pages are created and others 732-555- 1001 732-555-6666 (a) ---- -.... 732-555- 0011 732-555-1001 732-555- 6666 (b) -----.... 732-555- 001 1 FIGURE 10 A Call Graph. 9-7 S, a :=0 S2 b:= 1 S} c:= a + 1 S4 d :=b+a Ss e:= d + 1 S6 e :=c+d FIGURE 11 A Precedence Graph. 9.1 Graphs and Graph Models 595 removed somewhere on the Web almost every second, the Web graph changes on an almost continual basis. Currently the Web graph has more than three billion vertices and 20 billion edges. Many people are studying the properties of the Web graph to better understand the nature of the Web. We will return to Web graphs in Section 9.4, and in Chapter 10 we will explain how the Web graph is used by the Web crawlers that search engines use to create indices of Web pages. ... EXAMPLE 9 Precedence Graphs and Concurrent Processing Computer programs can be executed more rapidly by executing certain statements concurrently. It is important not to execute a statement that requires results of statements not yet executed. The dependence of statements on previous statements can be represented by a directed graph. Each statement is represented by a vertex, and there is an edge from one vertex to a second vertex if the statement represented by the second vertex cannot be executed before the statement represented by the first vertex has been executed. This graph is called a precedence graph. A computer program and its graph are displayed in Figure 11. For instance, the graph shows that statement S5 cannot be executed before statements Sl, S2, and S4 are executed. ... EXAMPLE 10 Roadmaps Graphs can be used to model roadmaps. In such models, vertices represent in­ tersections and edges represent roads. Undirected edges represent two-way roads and directed edges represent one-way roads. Multiple undirected edges represent multiple two-way roads connecting the same two intersections. Multiple directed edges represent multiple one-way roads that �tart at one intersection and end at a second intersection. Loops represent loop roads. Consequently, roadmaps depicting only two-way roads and no loop roads, and in which no two roads connect the same pair of intersections, can be represented using a simple undirected graph. Roadmaps depicting only one-way roads and no loop roads, and where no two roads start at the same intersection and end at the same intersection, can be modeled using simple directed graphs. Mixed graphs are needed to depict roadmaps that include both one-way and two-way roads. ... Exercises 1. Draw graph models, stating the type of graph (from Table 1) used, to represent airline routes where every day there are four flights from Boston to Newark, two flights from Newark to Boston, three flights from Newark to Miami, two flights from Miami to Newark, one flight from Newark to Detroit, two flights from Detroit to Newark, three flights from Newark to Washington, two flights from Washington to Newark, and one flight from Washington to Miami, with a) an edge between vertices representing cities that have a flight between them (in either direction). b) an edge between vertices representing cities for each flight that operates between them (in either direction). 596 9 / Graphs c) an edge between vertices representing cities for each flight that operates between them (in either direction ), plus a loop for a special sightseeing trip that takes off and lands in Miami. d) an edge from a vertex representing a city where a flight starts to the vertex representing the city where it ends. e) an edge for each flight from a vertex representing a city where the flight begins to the vertex representing the city where the flight ends. 2. What kind of graph (from Table 1) can be used to model a highway system between major cities where a) there is an edge between the vertices represent­ ing cities if there is an interstate highway between them? b) there is an edge between the vertices representing cities for each interstate highway between them? c) there is an edge between the vertices representing cities for each interstate highway between them, and there is a loop at the vertex representing a city ifthere is an interstate highway that circles this city? For Exercises 3-9, determine whether the graph shown has directed or undirected edges, whether it has multiple edges, and whether it has one or more loops. Use your answers to determine the type of graph in Table 1 this graph is. 3. a b V1 c d 5. 7. a 9. 4.�� ___ b r-- � c d 6. a b c d • e 9-8 10. For each undirected graph in Exercises 3-9 that is not simple, find a set of edges to remove to make it simple. 11. Let G be a simple graph. Show that the relation R on the set of vertices of G such that uRv if and only if there is an edge associated to {u, v} is a symmetric, irreflexive relation on G. 12. Let G be an undirected graph with a loop at every vertex. Show that the relation R on the set of vertices of G such that u R v if and only ifthere is an edge associated to {u, v} is a symmetric, reflexive relation on G. 13. The intersection graph of a collection of sets A I, A2, ••• , An is the graph that has a vertex for each of these sets and has an edge connecting the vertices represent­ ing two sets if these sets have a nonempty intersection. Construct the intersection graph of these collections of sets. a) AI = {O, 2, 4, 6, 8}, A2 = {O, 1,2,3, 4}, A3 = {I, 3, 5, 7, 9}, A4 = {5, 6, 7, 8, 9}, As = {O, 1, 8, 9} b) AI = { ... , -4, -3, -2, -1, O}, A2 = { ... , -2, -I,O,I,2, ... }, A3 = { ... , -6, -4, -2,0,2,4,6, ... }, A4 = { ... , -5, -3, -I,I,3,5, ... }, As = { ... , -6, -3,0,3,6, ... } c) AI = {x I x < O}, A2 = {x I -1 < x < O}, A3 = {x I 0 < x < I}, A4 = {x I -1 < x < I}, As={xlx>-I}, A6 = R 14. Use the niche overlap graph in Figure 6 to determine the species that compete with hawks. 15. Construct a niche overlap graph for six species of birds, where the hermit thrush competes with the robin and with the blue jay, the robin also competes with the mockingbird, the mockingbird also competes with the blue jay, and the nuthatch competes with the hairy woodpecker. 16. Draw the acquaintanceship graph that represents that Tom and Patricia, Tom and Hope, Tom and Sandy, Tom and Amy, Tom and Marika, Jeff and Patricia, Jeff and Mary, Patricia and Hope, Amy and Hope, and Amy and Marika know each other, but none of the other pairs of people listed know each other. 17. We can use a graph to represent whether two people were alive at the same time. Draw such a graph to represent whether each pair of the mathematicians and computer scientists with biographies in the first four chapters of this book who died before 1900 were contemporaneous. (Assume two people lived at the same time if they were alive during the same year.) 18. Who can influence Fred and whom can Fred influence in the influence graph in Example 3? 19. Construct an influence graph for the board members of a company if the President can influence the Director of Research and Development, the Director of Marketing, 9-9 and the Director of Operations; the Director of Research and Development can influence the Director of Opera­ tions; the Director of Marketing can influence the Director of Operations; and no one can influence, or be influenced by, the Chief Financial Officer. 20. Which other teams did Team 4 beat and which teams beat Team 4 in the round-robin tournament represented by the graph in Figure 9? 21. In a round-robin tournament the Tigers beat the Blue Jays, the Tigers beat the Cardinals, the Tigers beat the Orioles, the Blue Jays beat the Cardinals, the Blue Jays beat the Orioles, and the Cardinals beat the Orioles. Model this outcome with a directed graph. 22. Draw the call graph for the telephone numbers 555-00 II, 555-1221,555-1333, 555-8888, 555-2222, 555-0091, and 555-1200 ifthere were three calls from 555-001 1 to 555- 8888 and two calls from 555-8888 to 555- 00 II, two calls from 555-2222 to 555-0091, two calls from 555-1221 to each of the other numbers, and one call from 555-1333 to each of555-0011, 555-1221, and 555-1200. 23. Explain how the two telephone call graphs for calls made during the month of January and calls made during the month of February can be used to determine the new tele­ phone numbers of people who have changed their tele­ phone numbers. 24. a) Explain how graphs can be used to model electronic mail messages in a network. Should the edges be di­ rected or undirected? Should multiple edges be al­ lowed? Should loops be allowed? b) Describe a graph that models the electronic mail sent in a network in a particular week. 25. How can a graph that models e-mail messages sent in a network be used to find people who have recently changed their primary e-mail address? 26. How can a graph that models e-mail messages sent in a network be used to find electronic mail mailing lists used to send the same message to many diff erent e-mail addresses? 9.2 Graph Terminology and Special Types of Graphs 597 27. Describe a graph model that represents whether each per­ son at a party knows the name of each other person at the party. Should the edges be directed or undirected? Should multiple edges be allowed? Should loops be allowed? 28. Describe a graph model that represents a subway system in a large city. Should edges be directed or undirected? Should multiple edges be allowed? Should loops be al­ lowed? 29. Describe a graph model that represents traditional mar­ riages between men and women. Does this graph have any special proper ties? 30. Which statements must be executed before S6 is executed in the program in Example 9? (Use the precedence graph in Figure II.) 31. Construct a precedence graph for the following program: SI:X:= 0 S2: x := x + I S3:y:=2 S4: z:= y S5: x := x +2 S6: y:= x +z S7: z:= 4 32. Describe a discrete structure based on a graph that can be used to model airline routes and their flight times. [Hint: Add structure to a directed graph.] 33. Describe a discrete structure based on a graph that can be used to model relationships between pairs of individ­ uals in a group, where each individ ual may either like, dislike, or be neutral about another individual, and the reverse relationship may be different. [Hint: Add struc­ ture to a directed graph. Treat separately the edges in opposite directions between vertices representing two individuals . ] 34. Describe a graph model that can be used to represent all forms of electronic communication between two people in a single graph. What kind of graph is needed? 9.2 Graph Terminology and Special Types of Graphs LinIIS� Introduction We introduce some of the basic vocabulary of graph theory in this section. We will use this vocabulary later in this chapter when we solve many different types of problems. One such problem involves determining whether a graph can be drawn in the plane so that no two of its edges cross. Another example is deciding whether there is a one-to-one correspondence between the vertices of two graphs that produces a one-to-one correspondence between the edges of the graphs. We will also introduce several important families of graphs often used as examples and in models. Several important applications will be described where these special types of graphs arIse. 598 9 / Graphs DEFINITION 1 9-10 Basic Terminolog y First, we give some terminology that describes the vertices and edges of undirected graphs. Two vertices u and v in an undirected graph G are called adjacent (or neighbors) in G if u and v are endpoints of an edge of G. If e is associated with {u, v}, the edge e is called incident with the vertices u and v. The edge e is also said to connect u and v. The vertices u and v are called endpoints of an edge associated with {u, v}. To keep track of how many edges are incident to a vertex, we make the following definition. DEFINITION 2 The degree of a vertex in an undirected graph is the number of edges incident with it, except that a loop at a vertex contributes twice to the degree of that vertex. The degree of the vertex v is denoted by deg(v). EXAMPLE 1 What are the degrees of the vertices in the graphs G and H displayed in Figure 1? Solution: In G, deg(a) = 2, deg(b) = deg(c) = deg(f) = 4, deg(d) = 1, deg(e) = 3, and deg(g) = O. In H, deg(a) = 4, deg(b) = deg(e) = 6, deg(c) = 1, and deg(d) = 5. .... A vertex of degree zero is called isolated. It follows that an isolated vertex is not adjacent to any vertex. Vertex g in graph G in Example 1 is isolated. A vertex is pendant if and only if it has degree one. Consequently, a pendant vertex is adjacent to exactly one other vertex. Vertex d in graph G in Example 1 is pendant. Examining the degrees of vertices in a graph model can provide useful information about the model, as Example 2 shows. EXAMPLE 2 What does the degree of a vertex in a niche overlap graph (introduced in Example 1 in Section 9.1) represent? Which vertices in this graph are pendant and which are isolated? Use the niche overlap graph shown in Figure 6 of Section 9.1 to interpret your answers. Solution: There is an edge between two vertices in a niche overlap graph if and only if the two species represented by these vertices compete. Hence, the degree of a vertex in a niche overlap graph is the number of species in the ecosystem that compete with the species represented by this vertex. A vertex is pendant if the species competes with exactly one other species in the a b c f e G d • • g e FIGURE 1 The Undirected Graphs G and H. c H 9-11 THEOREM 1 9.2 Graph Terminology and Special Types of Graphs 599 ecosystem. Finally, the vertex representing a species is isolated if this species does not compete with any other species in the ecosystem. For instance, the degree of the vertex representing the squirrel in Figure 6 in Section 9.1 is four, because the squirrel competes with four other species: the crow, the opossum, the raccoon, and the woodpecker. In the graph in Figure 6, the mouse is the only species represented by a pendant vertex, because the mouse competes only with the shrew and all other species compete with at least two other species. The vertex representing a species is pendant if this species competes with only one other species. There are no isolated vertices in the graph in Figure 6 because every species in this ecosystem competes with at least one other species. .... What do we get when we add the degrees of all the vertices of a graph G = (V, E)? Each edge contributes two to the sum of the degrees of the vertices because an edge is incident with exactly two (possibly equal) vertices. This means that the sum ofthe degrees of the vertices is twice the number of edges. We have the result in Theorem I, which is sometimes called the Handshaking Theorem, because of the analogy between an edge having two endpoints and a handshake involving two hands. THE HANDSHAKING THEOREM Let G = (V, E) be an undirected graph with e edges. Then 2e = L deg(v). veV (Note that this applies even if multiple edges and loops are present.) EXAMPLE 3 How many edges are there in a graph with 10 vertices each of degree six? THEOREM 2 Solution: Because the sum of the degrees ofthe vertices is 6· 10 = 60, it follows that 2e = 60. Therefore, e = 30. .... Theorem 1 shows that the sum of the degrees of the vertices of an undirected graph is even. This simple fact has many consequences, one of which is given as Theorem 2. An undirected graph has an even number of vertices of odd degree. Proof: Let VI and Vz be the set of vertices of even degree and the set of vertices of odd degree, respective ly, in an undirected graph G = (V, E). Then 2e = L deg(v) = L deg(v) + L deg(v). veV VeV2 Because deg(v) is even for v E VI, the first term in the right-hand side of the last equality is even. Furthermore, the sum of the two terms on the right-hand side of the last equality is even, because this sum is 2e. Hence, the second term in the sum is also even. Because all the terms in this sum are odd, there must be an even number of such terms. Thus, there are an even number of vertices of odd degree. <l 600 9 / Graphs DEFINITION 3 9-12 Terminology for graphs with directed edges reflects the fact that edges in directed graphs have directions. When (u, v) is an edge of the graph G with directed edges, u is said to be adjacent to v and v is said to be adjacent from u. The vertex u is called the initial vertex of (u, v), and v is called the terminal or end vertex of (u, v). The initial vertex and terminal vertex of a loop are the same. Because the edges in graphs with directed edges are ordered pairs, the definition of the degree of a vertex can be refined to reflect the number of edges with this vertex as the initial vertex and as the terminal vertex. DEFINITION 4 In a graph with directed edges the in-degree ofa vertex v, denoted by deg-(v), is the number of edges with v as their terminal vertex. The out-degree of v, denoted by deg+(v), is the number of edges with v as their initial vertex. (Note that a loop at a vertex contributes 1 to both the in-degree and the out-degree of this vertex.) EXAMPLE 4 Find the in-degree and out-degree of each vertex in the graph G with directed edges shown in Figure 2. THEOREM 3 Solution: The in-degrees in G are deg-(a) = 2, deg-(b) = 2, deg-(c) = 3, deg-(d) = 2, deg-(e) = 3, and deg- (f) = O. The out-degrees are deg+(a) = 4, deg+(b) = 1, deg+(c) = 2, deg+(d) = 2, deg+(e) = 3, and deg+(f) = O. .... Because each edge has an initial vertex and a terminal vertex, the sum of the in-degrees and the sum of the out-degrees of all vertices in a graph with directed edges are the same. Both of these sums are the number of edges in the graph. This result is stated as Theorem 3. Let G = (V, E) be a graph with directed edges. Then VEV VEV • f G FIGURE 2 The Directed Graph G. 9-13 • • 9.2 Graph Terminology and Special Types of Graphs 601 FIGURE 3 The Graphs Kn for 1 :S n :S 6. There are many properties of a graph with directed edges that do not depend on the direction of its edges. Consequently, it is often useful to ignore these directions. The undirected graph that results from ignoring directions of edges is called the underlying undirected graph. A graph with directed edges and its underlying undirected graph have the same number of edges. Some Special Simple Graphs We will now introduce several classes of simple graphs. These graphs are often used as examples and arise in many applications. EXAMPLE 5 Complete Graphs The complete graph on n vertices, denoted by Kn, is the simple graph that contains exactly one edge between each pair of distinct vertices. The graphs Kn, for n = 1,2,3,4,5,6, are displayed in Figure 3. .... EXAMPLE 6 Cycles The cycle en, n ::: 3, consists of n vertices VI, V2, ... , Vn and edges {VI, V2}, {V2, V3}, ... , {Vn-I, vn}, and {vn, vd. The cycles e3, e4, es, and e6 are displayed III Figure 4. .... EXAMPLE 7 Wheels We obtain the wheel Wn when we add an additional vertex to the cycle en, for n ::: 3, and connect this new vertex to each of the n vertices in en, by new edges. The wheels W3, W4, Ws, and W6 are displayed in Figure 5. .... DOOO 602 9 / Graphs 110 10 11 100 101 • • D 010 0 I 00 01 000 DOl QI Q2 Q3 FIGURE 6 The n-cube Qn for n = 1,2, and 3. 9-14 III 011 EXAMPLE 8 n-Cubes The n-dimensional hypercube, or n-cube, denoted by Qn, is the graph that has vertices representing the 2n bit strings of length n. Two vertices are adjacent if and only if the bit strings that they represent differ in exactly one bit position. The graphs Q I, Q2, and Q3 are displayed in Figure 6. Note that you can construct the (n + I)-cube Qn+1 from the n-cube Qn by making two copies of Qn, prefacing the labels on the vertices with a 0 in one copy of Qn and with a 1 in the other copy of Qn, and adding edges connecting two vertices that have labels differing only in the first bit. In Figure 6, Q3 is constructed from Q2 by drawing two copies of Q2 as the top and bottom faces of Q3, adding 0 at the beginning of the label of each vertex. in the bottom face and 1 at the beginning of the label of each vertex in the top face. (Here, by face we mean a face of a cube in three-dimensional space. Think of drawing the graph Q3 in three-dimensional space with copies of Q2 as the top and bottom faces of a cube and then drawing the projection of the resulting depiction in the plane.) .... DEFINITION 5 Bipartite Graphs Sometimes a graph has the property that its vertex set can be divided into two disjoint subsets such that each edge connects a vertex in one of these subsets to a vertex in the other subset. For example, consider the graph representing marriages between men and women in a village, where each person is represented by a vertex and a marriage is represented by an edge. In this graph, each edge connects a vertex in the subset of vertices representing males and a vertex in the subset of vertices representing females. This leads us to Definition 5. A simple graph G is called bipartite if its vertex set V can be partitioned into two disjoint sets VI and V2 such that every edge in the graph connects a vertex in VI and a vertex in V2 (so that no edge in G connects either two vertices in VI or two vertices in V2). When this condition holds, we call the pair (VI. V2) a bipartition of the vertex set V of G. In Example 9 we will show that C6 is bipartite, and in Example 10 we will show that K3 is not bipartite. EXAMPLE 9 C6 is bipartite, as shown in Figure 7, because its vertex set can be partitioned into the two sets VI = {VI, V3. vs} and V2 = {V2. V4. V6}, and every edge of C6 connects a vertex in VI and a vertex in V2• .... EXAMPLE 10 K3 is not bipartite. To verify this, note that if we divide the vertex set of K3 into two disjoint sets, one of the two sets must contain two vertices. Ifthe graph were bipartite, these two vertices 9-15 g f 9.2 Graph Terminology and Special Types of Graphs 603 e d e d G H FIGURE 7 Showing That C6 Is Bipartite. FIGURE 8 The Undirected Graphs G and H. could not be connected by an edge, but in K 3 each vertex is connected to every other vertex by an edge. � EXAMPLE 11 Are the graphs G and H displayed in Figure 8 bipartite? THEOREM 4 Solution: Graph G is bipartite because its vertex set is the union of two disjoint sets, {a, b, d} and {c, e, j, g}, and each edge connects a vertex in one of these subsets to a vertex in the other subset. (Note that for G to be bipartite it is not necessary that every vertex in {a, b, d} be adjacent to every vertex in {c, e, j, g}. For instance, band g are not adjacent.) Graph H is not bipartite because its vertex set cannot be partitioned into two subsets so that edges do not connect two vertices from the same subset. (The reader should verify this by considering the vertices a, b, and j.) � Theorem 4 provides a useful criterion for determining whether a graph is bipartite. A simple graph is bipartite if and only if it is possible to assign one of two different colors to each vertex of the graph so that no two adjacent vertices are assigned the same color. Proof: First, suppose that G = (V, E) is a bipartite simple graph. Then V = VI U V2, where VI and V2 are disjoint sets and every edge in E connects a vertex in VI and a vertex in V2. If we assign one color to each vertex in VI and a second color to each vertex in V2, then no two adjacent vertices are assigned the same color. Now suppose that it is possible to assign colors to the vertices of the graph using just two colors so that no two adjacent vertices are assigned the same color. Let VI be the set of vertices assigned one color and V2 be the set of vertices assigned the other color. Then, VI and V2 are disjoint and V = VI U V2. Furthermore, every edge connects a vertex in VI and a vertex in V2 because no two adjacent vertices are either both in VI or both in V2. Consequently, G is bipartite. <I We illustrate how Theorem 4 can used to determine whether a graph is bipartite in Ex­ ample 12. EXAMPLE 12 Use Theorem 4 to determine whether the graphs in Example 11 are bipartite. Solution: We first consider the graph G. We will try to assign one of two colors, say red and blue, to each vertex in G so that no edge in G connects a red vertex and a blue vertex. Without loss of generality we begin by arbitrarily assigning red to a. Then, we must assign blue to c, e, j, and g, because each of these vertices is adjacent to a. To avoid having an edge with two blue endpoints, we must assign red to all the vertices adjacent to either c, e, j, or g. This means that 604 9 / Graphs FIGURE 9 Some Complete Bipartite Graphs. 9-16 we must assign red to both b and d (and means that a must be assigned red, which it already has been). We have now assigned colors to all vertices, with a, b, and d red and c, e, j, and g blue. Checking all edges, we see that every edge connects a red vertex and a blue vertex. Hence, by Theorem 4 the graph G is bipartite. Next, we will try to assign either red or blue to each vertex in H so that no edge in H connects a red vertex and a blue vertex. Without loss of generality we arbitrarily assign red to a. Then, we must assign blue to b, e, and j, because each is adjacent to a. But this is not possible because e and j are adjacent, so both cannot be assigned blue. This argument shows that we cannot assign one of two colors to each of the vertices of H so that no adjacent vertices are assigned the same color. It follows by Theorem 4 that H is not bipartite. ... Theorem 4 is an example of a result in the part of graph theory known as graph colorings. Graph colorings is an important part of graph theory with important applications. We will study graph colorings further in Section 9.8. Another useful criterion for determing whether a graph is bipartite is based on the notion of a path, a topic we study in Section 9.4. A graph is bipartite if and only if it is not possible to start at a vertex and return to this vertex by traversing an odd number of distinct edges. We will make this notion more precise when we discuss paths and circuits in graphs in Section 9.4 (see Exercise 53 in that section). EXAMPLE 13 Complete Bipartite Graphs The complete bipartite graph Km,n is the graph that has its vertex set partitioned into two subsets of m and n vertices, respectively. There is an edge between two vertices if and only if one vertex is in the first subset and the other vertex is in the second subset. The complete bipartite graphs K2,3, K3,3, K3,5, and K2,6 are displayed in Figure 9. ... Some Applications of Special Types of Graphs We will show how bipartite graphs and special types of graphs are used in models in Examples 14-16. EXAMPLE 14 Job Assignments Suppose that there are m employees in a group and j different jobs that need to be done where m ::::: j. Each employee is trained to do one or more of these j jobs. We can use a graph to model employee capabilities. We represent each employee by a vertex and each job by a vertex. For each employee, we include an edge from the vertex representing that employee to the vertices representing all jobs that the employee has been trained to do. 9-/7 EXAMPLE 15 LinkS� Alvarez Berkowitz Chen Davis requirements architecture implementation testing FIGURE 10 Modeling the Jobs for Which Employees Have Been Trained. 9.2 Graph Terminology and Special Types of Graphs 605 Note that the vertex set of this graph can be partitioned into two disjoint sets, the set of vertices representing employees and the set of vertices representingjobs, and each edge connects a vertex representing an employee to a vertex representing a job. Consequently, this graph is bipartite. For example, suppose that a group has four employees: Alvarez, Berkowitz, Chen, and Davis; and suppose that four jobs need to be done to complete a project: requirements, architecture, implementation, and testing. Suppose that Alvarez has been trained to do requirements and testing; Berkowitz has been trained to do architecture, implementation, and testing; Chen has been trained to do requirements, architecture, and implementa tion; and Davis has only been trained to do requirements. We can model these capabilities of employees using the bipartite graph shown in Figure 10. To complete the project, we must assign jobs to the employees so that every job has an em­ ployee assigned to it and no employee is assigned more than one job. In this case, we can assign Alvarez to do testing, Berkowitz to do implementation, Chen to do architecture, and Davis to do requirements, as shown in Figure 10 (where colored lines show this assignment of jobs). .... Finding an assignment of jobs to employees can be thought of as finding a matching in the graph model. A matching in a simple graph is a subset of the set of edges of the graph such that no two edges are incident with the same vertex; a maximal matching is a matching with the largest number of edges. In other words, a matching is a subset of edges such that if {s, t} and {u, v} are edges of the matching, then s, t, u, and v are distinct. To assign jobs to employees so that the largest number of employees are assigned jobs, we seek a maximum matching in the graph that models employee capabilities. (The interested reader can find more about matchings in books about graph theory, including [GrYe06].) Local Area Networks The various computers in a building, such as minicomputers and per­ sonal computers, as well as peripheral devices such as printers and plotters, can be connected using a local area network. Some of these networks are based on a star topology, where all devices are connected to a central control device. A local area network can be represented using a complete bipartite graph Kl,n, as shown in Figure II(a). Messages are sent from device to device through the central control de vice. Other local area networks are based on a ring topology, where each device is connected to exactly two others. Local area networks with a ring topology are modeled using n-cycles, en, as shown in Figure II (b). Messages are sent from device to device around the cycle until the intended recipient of a message is reached. (a) (b) (c) FIGURE 11 Star, Ring, and Hybrid Topologies for Local Area Networks. 606 9 / Graphs 9-18 Finally, some local area networks use a hybrid of these two topologies. Messages may be sent around the ring, or through a central device. This redundancy makes the network more reliable. Local area networks with this redundancy can be modeled using wheels Wn, as shown in Figure II(c). � EXAMPLE 16 Interconnection Networks for Parallel Computation For many years, computers executed programs one operation at a time. Consequently, the algorithms written to solve problems were designed to perform one step at a time; such algorithms are called serial. (Almost all algorithms described in this book are serial.) However, many computationally intense problems, such as weather simulations, medical imaging, and cryptanal ysis, cannot be solved in a reasonable amount of time using serial operations, even on a supercomputer. Furthermore, there is a physical limit to how fast a computer can carry out basic operations, so there will always be problems that cannot be solved in a reasonable length of time using serial operations. Parallel processing, which uses computers made up of many separate processors, each with its own memory, helps overcome the limitations of computers with a single processor. Parallel algorithms, which break a problem into a number of subproblems that can be solved concurrently, can then be devised to rapidly solve problems using a computer with multiple processors. In a parallel algorithm, a single instruction stream controls the execution of the algorithm, sending subproblems to different processors, and directs the input and output of these subproblems to the appropriate processors. When parallel processing is used, one processor may need output generated by another processor. Consequently, these processors need to be interconnected. We can use the appropriate type of graph to represent the interconnection network of the processors in a computer with multiple processors. In the following discussion, we will describe the most commonly used types of interconnection networks for parallel processors. The type of interconnection network used to implement a particular parallel algorithm depends on the requirements for exchange of data between processors, the desired speed, and, of course, the available hardware . The simplest, but most expensive, network-interconnecting processors include a two-way link between each pair of processors. This network can be represented by K n, the complete graph on n vertices, when there are n processors. However, there are serious problems with this type of interconnection network because the required number of connections is so large. In reality, the number of direct connections to a processor is limited, so when there are a large number of processors, a processor cannot be linked directly to all others. For example, when there are 64 processors, C(64, 2) = 2016 connections would be required, and each processor would have to be directly connected to 63 others. On the other hand, perhaps the simplest way to interconnect n processors is to use an arrangement known as a linear array. Each processor Pi, other than PI and Pn, is connected to its neighbors Pi-I and Pi+1 via a two-way link. PI is connected only to P2, and Pn is connected only to Pn-I. The linear array for six processors is shown in Figure 12. The advantage of a linear array is that each processor has at most two direct connections to other processors. The disadvantage is that it is sometimes necessary to use a large number of intermediate links, called hops, for processors to share information. The mesh network (or two-dimensional array) is a commonly used interconnection net­ work. In such a network, the number of processors is a perfect square, say n = m2• The n processors are labeled P(;, j), 0 :::: ; :::: m -1, 0 :::: j :::: m -1. Two-way links connect proces­ sor P(i, j) with its four neighbors, processors P(; ± 1, j) and P(i, j ± 1), as long as these are processors in the mesh. (Note that four processors, on the corners of the mesh, have only two adjacent processors, and other processors on the boundaries have only three neighbors. Sometimes a variant of a mesh network in which every processor has exactly four connections is used; see Exercise 66 at the end of this section.) The mesh network limits the number oflinks for each processor. Communication between some pairs of processors requires O(Jn) = O(m) 9-19 DEFINITION 6 PI P2 P, P4 Ps P6 • • • • • • FIGURE 12 A Linear Array for Six Processors. 9.2 Graph Tenninology and Special Types of Graphs 607 P(O,O) P(O,I) P(0,2) P(0,3) P(l,O) P(l,I) P(l,2) P(l,3) P(2, 0) P(2,1) P(2,2) P(2,3) P(3,O) P(3,1) P(3,2) P(3,3) FIGURE 13 A Mesh Network for 16 Processors. intermediate links. (See Exercise 67 at the end of this section.) The graph representing the mesh network for 16 processors is shown in Figure 13. One important type of interconnection network is the hypercub e. For such a network, the number of processors is a power of 2, n = 2m. The n processors are labeled Po, PI, ... , Pn-I. Each processor has two-way connections to m other processors. Processor Pi is linked to the processors with indices whose binary representations differ from the binary representation of i in exactly one bit. The hypercube network balances the number of direct connections for each processor and the number of intermediate connections required so that processors can communicate. Many computers have been built using a hypercube network, and many parallel algorithms have been devised that use a hypercube network. The graph Qm, the m-cube, represents the hypercube network with n = 2m processors. Figure 14 displays the hypercube network for eight processors. (Figure 14 displays a different way to draw Q3 than was shown in Figure 6.) .... New Graphs from Old Sometimes we need only part of a graph to solve a problem. For instance, we may care only about the part of a large computer network that involves the computer centers in New York, Denver, Detroit, and Atlanta. Then we can ignore the other computer centers and all telephone lines not linking two of these specific four computer centers. In the graph model for the large network, we can remove the vertices corresponding to the computer centers other than the four of interest, and we can remove all edges incident with a vertex that was removed. When edges and vertices are removed from a graph, without removing endpoints of any remaining edges, a smaller graph is obtained. Such a graph is called a subgraph of the original graph. A subgraph ofa graph G = (V, E) is a graph H = (W, F), where W � V and F � E. A subgraph H of G is a proper subgraph of G if H # G. a a e �--+---+--=-b d c c FIGURE 14 A Hypercube Network for Eight Processors. FIGURE 15 A Subgraph of Ks. 608 9 / Graphs abc a b c a b c LV d e d f d e f (a) (b) FIGURE 16 (a) The Simple Graphs Gt and G2; (b) Their Union Gt U G2. 9-20 EXAMPLE 17 The graph G shown in Figure 15 is a subgraph of K5. Two or more graphs can be combined in various ways. The new graph that contains all the vertices and edges of these graphs is called the union ofthe graphs. We will give a more formal definition for the union of two simple graphs. DEFINITION 7 The union of two simple graphs GI = (VI, Ed and G2 = (V2, E2) is the simple graph with vertex set VI U V2 and edge set EI U E2. The union of GI and G2 is denoted by GI U G2. EXAMPLE 18 Find the union of the graphs GI and G2 shown in Figure 16(a). Solution: The vertex set of the union G I U G2 is the union of the two vertex sets, namely, {a, b, c, d, e, fl. The edge set of the union is the union of the two edge sets. The union is displayed in Figure 16(b). Exercises In Exercises 1-3 find the number of vertices, the number of edges, and the degree of each vertex in the given undirected graph. Identify all isolated and pendant vertices. 1. a b c • fe d 2. a b c 3. • f h g e 4. Find the sum of the degrees of the vertices of each graph in Exercises 1-3 and verify that it equals twice the number of edges in the graph. 5. Can a simple graph exist with 15 vertices each of degree five? 6. Show that the sum, over the set of people at a party, of the number of people a person has shaken hands with, is even. Assume that no one shakes his or her own hand. In Exercises 7-9 determine the number of vertices and edges and find the in-degree and out-degree of each vertex for the given directed multigraph. 9-21 7. 9. • e o d c 10. For each of the graphs in Exercises 7-9 determine the sum of the in-degrees of the vertices and the sum of the out­ degrees of the vertices directly. Show that they are both equal to the number of edges in the graph. 11. Construct the underlying undirected graph for the graph with directed edges in Figure 2. 12. What does the degree of a vertex represent in the acquain­ tanceship graph, where vertices represent all the people in the world? What do isolated and pendant vertices in this graph represent? In one study it was estimated that the average degree ofa vertex in this graph is 1000. What does this mean in terms ofthe model? 13. What does the degree of a vertex represent in a collab­ oration graph? What do isolated and pendant vertices represent? 14. What does the degree of a vertex in the Hollywood graph represent? What do the isolated and pendant vertices represent? 15. What do the in-degree and the out-degree of a vertex in a telephone call graph, as described in Example 7 of Section 9.1, represent? What does the degree of a vertex in the undirected version of this graph represent? 16. What do the in-degree and the out-degree of a vertex in the Web graph, as described in Example 8 of Section 9.1, represent? 17. What do the in-degree and the out-degree ofa vertex in a directed graph modeling a round-robin tournament rep­ resent? 18. Show that in a simple graph with at least two vertices there must be two vertices that have the same degree. 19. Use Exercise 18 to show that in a group, there must be two people who know the same number of other people in the group. 20. Draw these graphs. a) K7 b) K1.8 d) C7 e) W7 In Exercises 21-25 determine whether the graph is bipartite. You may find it useful to apply Theorem 4 and answer the question by determining whether it is possible to assign either 9.2 Graph Terminology and Special Types of Graphs 609 red or blue to each vertex so that no two adjacent vertices are assigned the same color. 22. b c a�------��--.d c d e 23. b c a��--�---+�d f e 24. a b e d 25. b a c f d e 26. For which values of n are these graphs bipartite? a) Kn b) Cn c) Wn d) Qn 27. Suppose that a new company has five employees: Zamora, Agraharam, Smith, Chou, and Macintyre. Each employee will assume one of six responsiblities: planning, public­ ity, sales, marketing, development, and industry relations. Each employee is capable of doing one or more of these jobs: Zamora could do planning, sales, marketing, or in­ dustry relations; Agraharam could do planning or devel­ opment; Smith could do publicity, sales, or industry rela­ tions; Chou could do planning, sales, or industry relations; and Macintyre could do planning, publicity, sales, or in­ dustry relations. a) Model the capabilities ofthese employees using a bi­ partite graph. b) Find an assignment of responsibilites such that each employee is assigned a responsibil ity. 28. Suppose that there are five young women and six young men on an island. Each woman is willing to marry some 610 9/ Graphs ofthe men on the island and each man is willing to marry any woman who is willing to marry him. Suppose that Anna is willing to marry Jason, Larry, and Matt; Barbara is willing to marry Kevin and Larry; Carol is willing to marry Jason, Nick, and Oscar; Diane is willing to marry Jason, Larry, Nick, and Oscar; and Elizabeth is willing to marry Jason and Matt. a) Model the possible marriages on the island using a bipartite graph. b) Find a matching of the young women and the young men on the island such that each young woman is matched with a young man whom she is willing to marry. 29. How many vertices and how many edges do these graphs have? a) Kn d) Km,n b) Cn e) Qn The degree sequence of a graph is the sequence of the de­ grees of the vertices of the graph in nonincreasing order. For example, the degree sequence of the graph G in Example 1 in this section is 4, 4, 4, 3, 2, I, O. 30. Find the degree sequences for each of the graphs in Exer­ cises 21-25. 31. Find the degree sequence of each of the following graphs. a) K4 d) K2,3 32. What is the degree sequence of the bipartite graph Km•n where m and n are positive integers? Explain your answer. 33. What is the degree sequence of Kn, where n is a positive integer? Explain your answer. 34. How many edges does a graph have if its degree sequence is 4, 3, 3, 2, 2? Draw such a graph. 35. How many edges does a graph have if its degree sequence is 5, 2, 2, 2, 2, I? Draw such a graph. A sequence dl, d2, ... ,dn is called graphic ifit is the degree sequence of a simple graph. 36. Determine whether each of these sequences is graphic. For those that are, draw a graph having the given degree sequence. a) 5,4, 3,2, 1,0 c) 2, 2, 2, 2, 2, 2 e) 3,3,2,2, 2, 2 g) 5, 3, 3, 3, 3, 3 b) 6,5,4, 3,2, 1 d) 3, 3, 3, 2, 2, 2 1) 1, 1, 1, 1, 1, 1 h) 5, 5, 4, 3, 2, 1 37. Determine whether each of these sequences is graphic. For those that are, draw a graph having the given degree sequence. a) 3,3, 3,3, 2 b) 5,4,3,2, 1 c) 4, 4, 3, 2, 1 d) 4, 4, 3, 3, 3 e) 3,2,2, 1,0 1) 1, 1, 1, I, 1 '38. Suppose that dl, d2, ... , dn is a graphic sequence. Show that there is a simple graph with vertices VI, V2, .•• , Vn such that deg( Vi) = di for i = 1, 2, ... , n and VI is adja­ cent to V7, .•• , Vd, , , • 9-22 *39. Show that a sequence dl, d2, ... ,dn of nonnegative inte­ gers in nonincreasing order is a graphic sequence if and only if the sequence obtained by reordering the terms of the sequenced2 -1, ... , dd1+1 -1, dd1+2,"" dn so that the terms are in nonincreasing order is a graphic sequence. *40. Use Exercise 39 to construct a recursive algorithm for de­ termining whether a nonincreasing sequence of positive integers is graphic. 41. Show that every nonincreasing sequence of nonnegative integers with an even sum of its terms is the degree se­ quence of a pseudograph, that is, an undirected graph where loops are allowed. [Hint: Construct such a graph by first adding as many loops as possible at each vertex. Then add additional edges connecting vertices of odd de­ gree. Explain why this construction works.] 42. How many subgraphs with at least one vertex does K2 have? 43. How many subgraphs with at least one vertex does K 3 have? 44. How many subgraphs with at least one vertex does W3 have? 45. Draw all subgraphs ofthis graph. a b ~ c d 46. Let G be a graph with v vertices and e edges. Let M be the maximum degree of the vertices of G, and let m be the minimum degree of the vertices of G. Show that a) 2e/v :::: m. b) 2e/v ::; M. A simple graph is called regular if every vertex of this graph has the same degree. A regular graph is called n-regular if every vertex in this graph has degree n. 47. For which values of n are these graphs regular? a) Kn b) Cn c) Wn d) Qn 48. For which values of m and n is Km,n regular? 49. How many vertices does a regular graph of degree four with 10 edges have? In Exercises 50-52 find the union of the given pair of sim­ ple graphs. (Assume edges with the same endpoints are the same.) 50. a f b f b V e c d d 9-23 51. 52. x:r c d c g a b a e [XJ� c d f g d 53. The complementary graph G of a simple graph G has the same vertices as G. Two vertices are adjacent in G if and only if they are not adjacent in G. Describe each of these graphs. a) Kn b) Km•n c) en d) Qn 54. IfG is a simple graph with 15 edges and G has 13 edges, how many vertices does G have? 55. If the simple graph G has v vertices and e edges, how many edges does G have? 56. If the degree sequence of the simple graph G is 4,3,3,2,2, what is the degree sequence of G? 57. If the degree sequence of the simple graph G is dl, dz, ... ,dn, what is the degree sequence of G? 58. Show that if G is a bipartite simple graph with v vertices and e edges, then e ::s v2/4. 59. Show that if G is a simple graph with n vertices, then the union ofG and Gis Kn. 9.3 Representing Graphs and Graph Isomorphism 611 *60. Describe an algorithm to decide whether a graph is bipar­ tite based on the fact that a graph is bipartite if and only if it is possible to color its vertices two different colors so that no two vertices of the same color are adjacent. The converse of a directed graph G = (V, E), denoted by Gconv, is the directed graph (V, F), where the set F of edges of Gconv is obtained by reversing the direction of each edgeinE. 61. Draw the converse of each ofthe graphs in Exercises 7-9 in Section 9.1. 62. Show that (Gconv)conv = G whenever G is a directed graph. 63. Show that the graph G is its own converse if and only if the relation associated with G (see Section 8.3) is symmetr ic. 64. Show that if a bipartite graph G = (V, E) is n-regular for some positive integer n (see the preamble to Exercise 47) and (VI, V2) is a bipartition of V, then I VII = I Vzl. That is, show that the two sets in a bipartition of the vertex set of an n-regular graph must contain the same number of vertices. 65. Draw the mesh network for interconnecting nine parallel processors. 66. In a variant of a mesh network for interconnecting n = m2 processors, processor P(i, j) is connected to the four pro­ cessors P«i ± 1) mod m, j) and P(i, (j ± I) mod m), so that connections wrap around the edges of the mesh. Draw this variant of the mesh network for 16 processors. 67. Show that every pair of processors in a mesh network of n = m2 processors can communicate using O(../fi) = O(m) hops between directly connected processors. 9.3 Representing Graphs and Graph Isomor phism Introduction There are many useful ways to represent graphs. As we will see throughout this chapter, in working with a graph it is helpful to be able to choose its most convenient representation. In this section we will show how to represent graphs in several different ways. Sometimes, two graphs have exactly the same form, in the sense that there is a one-to-one correspondence between their vertex sets that preserves edges. In such a case, we say that the two graphs are isomorphic. Determining whether two graphs are isomorphic is an important problem of graph theory that we will study in this section. Representing Graphs One way to represent a graph without multiple edges is to list all the edges of this graph. Another way to represent a graph with no multiple edges is to use adjacency lists, which specify the vertices that are adjacent to each vertex of the graph. 612 9 / Graphs b a c e d FIGURE 1 A Simple Graph. 9-24 TABLE 1 An Adjacency List for a Simple Graph. J1?rtex Adjacent Vertices a b,c,e b a c a,d,e d c,e e a,c,d EXAMPLE 1 Use adjacency lists to describe the simple graph given in Figure 1. Solution: Table 1 lists those vertices adjacent to each of the vertices of the graph. EXAMPLE 2 Represent the directed graph shown in Figure 2 by listing all the vertices that are the terminal vertices of edges starting at each vertex of the graph. a b R c d FIGURE 3 Simple Graph. unkS� Solution: Table 2 represents the directed graph shown in Figure 2. TABLE 2 An Adjacency List for a e d FIGURE 2 A Directed Graph. Adjacency Matrices Directed Graph. Initial J1?rtex a b c d e Terminal J1?rtices b,c,d,e b,d a, C, e b, c,d Carrying out graph algorithms using the representation of graphs by lists of edges, or by adja­ cency lis ts, can be cumbersome if there are many edges in the graph. To simplify computation, graphs can be represented using matrices. Two types of matrices commonly used to represent graphs will be presented here. One is based on the adjacency of vertices, and the other is based on incidence of vertices and edges. Suppose that G = (V, E) is a simple graph where IVI = n. Suppose that the vertices of G are listed arbitrarily as VI, V2, •.• , Vn• The adjacency matrix A (or AG) of G, with respect to this listing of the vertices, is the n x n zero-one matrix with 1 as its (i, j)th entry when Vi and Vj are adjacent, and 0 as its (i, j)th entry when they are not adjacent. In other words, if its adjacency matrix is A = [aij], then if { Vi, Vj} is an edge of G, otherwise. EXAMPLE 3 Use an adjacency matrix to represent the graph shown in Figure 3. 9-25 9.3 Representing Graphs and Graph Isomorphism 613 Solution: We order the vertices as a, b, c, d. The matrix representing this graph is EXAM PLE 4 Draw a graph with the adjacency matrix a b Z d c FIGURE 4 A Graph with the Given Adjacency Matrix. with respect to the ordering of vertices a, b, c, d. Solution: A graph with this adjacency matrix is shown in Figure 4. Note that an adjacency matrix of a graph is based on the ordering chosen for the vertices. Hence, there are as many as n! different adjacency matrices for a graph with n vertices, because there are n! different orderings of n vertices. The adjacency matrix of a simple graph is symmetri c, that is, aij = a ji, because both of these entries are 1 when Vi and Vj are adjacent, and both are 0 otherwise. Furthermore, because a simple graph has no loops, each entry au, i = 1,2,3, ... , n, is o. Adjacency matrices can also be used to represent undirected graphs with loops and with multiple edges. A loop at the vertex ai is represented by a 1 at the (i, i)th position of the adjacency matrix. When multiple edges are present, the adjacency matrix is no longer a zero-one matrix, because the (i, j)th entry of this matrix equals the number of edges that are associated to {ai, aj}. All undirected graphs, including multi graphs and pseudographs, have symmetric adjacency matrices. EXAM PLE 5 Use an adjacency matrix to represent the pseudograph shown in Figure 5. FIGURE 5 A Pseudograph. Solution: The adjacency matrix using the ordering of vertices a, b, c, d is We used zero-one matrices in Chapter 8 to represent directed graphs. The matrix for a directed graph G = (V, E) has a 1 in its (i, j)th position if there is an edge from Vi to Vj, where VI, V2, •.. , Vn is an arbitrary listing of the vertices of the directed graph. In other words, if A = [aij] is the adjacency matrix for the directed graph with respect to this listing of the vertices, then {I if (Vi, Vj) is an edge of G, aij = 0 otherwise. The adjacency matrix for a directed graph does not have to be symmetric, because there may not be an edge from aj to ai when there is an edge from ai to aj. 614 9/ Graphs 9-26 Adjacency matrices can also be used to represent directed multigraph s. Again, such matrices are not zero-one matrices when there are multiple edges in the same direction connecting two vertices. In the adjacency matrix for a directed multigraph, aij equals the number of edges that are associated to (Vi, Vj). TRADE-OFFS BETWEEN ADJACENCY LISTS AND ADJACENCY MATRICES When a simple graph contains relatively few edges, that is, when it is sparse, it is usually preferable to use adjacency lists rather than an adjacency matrix to represent the graph. For example, if each vertex has degree not exceeding c, where e is a constant much smaller than n, then each adjacency list contains e or fewer vertices. Hence, there are no more than en items in all these adjacency lists. On the other hand, the adjacency matrix for the graph has n2 entries. Note, however, that the adjacency matrix of a sparse graph is a sparse matrix, that is, a matrix with few nonzero entries, and there are special techniques for representing, and computing with, sparse matrices. Now suppose that a simple graph is dense, that is, suppose that it contains many edges, such as a graph that contains more than half of all possible edges. In this case, using an adjacency matrix to represent the graph is usually preferable over using adjacency lists. To see why, we compare the complexity of determining whether the possible edge {Vi, Vj} is present. Using an adjacency matrix, we can determine whether this edge is present by examining the (i, j)th entry in the matrix. This entry is 1 if the graph contains this edge and is 0 otherwise. Consequently, we need make only one comparison, namely, comparing this entry with 0, to determine whether this edge is present. On the other hand, when we use adjacency lists to represent the graph, we need to search the list of vertices adjacent to either Vi or Vj to determine whether this edge is present. This can require 8(1V1) comparisons when many edges are present. Incidence Matrices Another common way to represent graphs is to use incidence matrices. Let G = (V, E) be an undirected graph. Suppose that VI, V2, ..• , Vn are the vertices and el, e2, ••• , em are the edges of G. Then the incidence matrix with respect to this ordering of V and E is the n x m matrix M = [mij], where mi. = {I when edge ej is incident with Vi, J 0 otherwise. EXAMPLE 6 Represent the graph shown in Figure 6 with an incidence matrix. FIGURE 6 An Undirected Graph. Solution: The incidence matrix is el e2 e3 e4 es e6 VI 1 1 0 0 0 0 V2 0 0 1 1 0 1 V3 0 0 0 0 1 1 V4 1 0 1 0 0 0 Vs 0 0 1 1 0 ... Incidence matrices can also be used to represent multiple edges and loops. Multiple edges are represented in the incidence matrix using columns with identical entries, because these edges are incident with the same pair of vertices. Loops are represented using a column with exactly one entry equal to 1, corresponding to the vertex that is incident with this loop. 9-27 9.3 Representing Graphs and Graph Isomorphism 615 EXAM PLE 7 Represent the pseudograph shown in Figure 7 using an incidence matrix. V5 es FIGURE 7 A Pseudograph. DEFINITION 1 EXAMPLE S FIGURE 8 The Graphs G and H. V3 Solution: The incidence matrix for this graph is el e2 e3 e4 e5 e6 e7 e8 VI I I I 0 0 0 0 0 V2 0 I I I 0 I I 0 V3 0 0 0 I I 0 0 0 V4 0 0 0 0 0 0 I I V5 0 0 0 0 I I 0 0 Isomorphism of Graphs We often need to know whether it is possible to draw two graphs in the same way. For instance, in chemistry, graphs are used to model compounds. Different compounds can have the same molecular formula but can differ in structure . Such compounds will be represented by graphs that cannot be drawn in the same way. The graphs representing previously known compounds can be used to determine whether a supposedly new compound has been studied before. There is a useful terminology for graphs with the same structure. The simple graphs GI = (VI, EI) and G2 = (V2, E2) are isomorphic if there is a one-to-one and onto function f from VI to V2 with the property that a and b are adjacent in GI if and only if f(a) and feb) are adjacent in G2, for all a and b in VI. Such a function f is called an isomorphism. * In other words, when two simple graphs are isomorphic, there is a one-to-one correspondence between vertices of the two graphs that preserves the adjacency relationship. Isomorphism of simple graphs is an equivalence relation. (We leave the verification of this as Exercise 45 at the end of this section.) Show that the graphs G = (V, E) and H = (W, F), displayed in Figure 8, are isomorphic. Solution: The function f with f(uI) = VI, f(U2) = V4, f(U3) = V3, and f(U4) = V2 is a one­ to-one correspondence between V and W. To see that this correspondence preserves adjacency, note that adjacent vertices in G are UI and U2, UI and U3, U2 and U4, and U3 and U4, and each of the pairs f(uI) = VI and f(U2) = V4, f(uI) = VI and f(U3) = V3, f(U2) = V4 and f(U4) = V2, and f(U3) = V3 and f(U4) = V2 are adjacent in H. <l1li It is often difficult to determine whether two simple graphs are isomorphic. There are n! possible one-to-one correspondences between the vertex sets of two simple graphs with n ver­ tices. Testing each such correspondence to see whether it preserves adjacency and nonadjacency is impractical if n is at all large. Sometimes it is not hard to show that two graphs are not isomorphic. In particular, we can show that two graphs are not isomorphic if we can find a property only one of the two graphs has, but that is preserved by isomorphism. A property preserved by isomorphism of graphs is called a graph invariant. For instance, isomorphic simple graphs must have the same number of vertices, because there is a one-to-one correspondence between the sets of vertices ofthe graph s. 'The word isomorphism comes from the Greek roots isos for "equal" and morphe for "form." 616 9/ Graphs 9-28 b b a b s w e_----"-.- -.- --- .. x h�---""g z:,...-__ _ .. y e d e d d c v u G H G H FIGURE 9 The Graphs G and H. FIGURE 10 The Graphs G and H. links� EXAMPLE 9 Extra� Examples IiiiiiiiI EXAMPLE 10 /b f v FIGURE 11 The Subgraphs of G and H Made Up of Vertices of Degree Three and the Edges Connecting Them. Isomorphic simple graphs also must have the same number of edges, because the one-to-one correspondence between vertices establishes a one-to-one correspondence between edges. In addition, the degrees of the vertices in isomorphic simple graphs must be the same. That is, a vertex v of degree d in G must correspond to a vertex f(v) of degree d in H, because a vertex win G is adjacent to v if and only if f(v) and f(w) are adjacent in H. Show that graphs displayed in Figure 9 are not isomorphic. Solution: Both G and H have five vertices and six edges. However, H has a vertex of degree one, namely, e, whereas G has no vertices of degree one. It follows that G and H are not isomorphic . .... The number of vertices, the number of edges, and the number of vertices of each degree are all invariants under isomorphism. If any of these quantities differ in two simple graphs, these graphs cannot be isomorphic. However, when these invariants are the same, it does not necessarily mean that the two graphs are isomorphic. There are no useful sets of invariants currently known that can be used to determine whether simple graphs are isomorphic. Determine whether the graphs shown in Figure 10 are isomorphic. Solution: The graphs G and H both have eight vertices and 10 edges. They also both have four vertices of degree two and four of degree three. Because these invariants all agree, it is still conceivable that these graphs are isomorphic. However, G and H are not isomorphic. To see this, note that because deg(a) = 2 in G, a must correspond to either t, u, x, or y in H, because these are the vertices of degree two in H. However, each of these four vertices in H is adjacent to another vertex of degree two in H, which is not true for a in G. Another way to see that G and H are not isomorphic is to note that the subgraphs of G and H made up of vertices of degree three and the edges connecting them must be isomorphic if these two graphs are isomorphic (the reader should verify this). However, these subgraphs, shown in Figure 11, are not isomorphic. .... To show that a function f from the vertex set of a graph G to the vertex set of a graph H is an isomorphism, we need to show that f preserves the presence and absence of edges. One helpful way to do this is to use adjacency matrices. In particular, to show that f is an isomorphism, we can show that the adjacency matrix of G is the same as the adjacency matrix of H, when rows and columns are labeled to correspond to the images under f of the vertices in G that are the labels of these rows and columns in the adjacency matrix of G. We illustrate how this is done in Example 11. 9-29 G FIGURE 12 Graphs G and H. H 9.3 Representing Graphs and Graph Isomorphism 617 EXAMPLE 11 Determine whether the graphs G and H displayed in Figure 12 are isomorphic. Solution: Both G and H have six vertices and seven edges. Both have four vertices of degree two and two vertices of degree three. It is also easy to see that the subgraphs of G and H consisting of all vertices of degree two and the edges connecting them are isomorphic (as the reader should verify). Because G and H agree with respect to these invariants, it is reasonable to try to find an isomorphism !. We now will define a function ! and then determine whether it is an isomorphism. Because deg(ut} = 2 and because u, is not adjacent to any other vertex of degree two, the image of u, must be either V4 or V6, the only vertices of degree two in H not adjacent to a vertex of degree two. We arbitrarily set !(u,) = V6. [If we found that this choice did not lead to isomorphism, we would then try !(u,) = V4.] Because U2 is adjacent to u" the possible images of U2 are V3 and Vs. We arbitrarily set !(U2) = V3. Continuing in this way, using adjacency of vertices and degrees as a guide, we set !(U3) = V4, !(U4) = vs, !(us) = v" and !(U6) = V2. We now have a one-to-one correspondence between the vertex set of G and the vertex set of H, namely, !(u,) = V6, !(U2) = V3, !(U3) = V4, !(U4) = vs, !(us) = v" !(U6) = V2. To see whether ! preserves edges, we examine the adjacency matrix of G, U, U2 U3 U4 Us U6 U, 0 1 0 1 0 0 U2 1 0 1 0 0 AG = U3 0 1 0 1 0 0 U4 1 0 1 0 1 0 Us 0 0 0 1 0 1 U6 0 0 0 0 and the adjacency matrix of H with the rows and columns labeled by the images of the corre­ sponding vertices in G, V6 V3 V4 Vs V, V2 V6 0 1 0 1 0 0 V3 0 1 0 0 1 AH = V4 0 1 0 0 0 Vs 1 0 0 0 v, 0 0 0 1 0 1 V2 0 0 0 0 618 9 / Graphs 9-30 Because AG = AH, it follows that f preserves edges. We conclude that f is an isomorphism, so G and H are isomorphic. Note that if f turned out not to be an isomorphism, we would not have established that G and H are not isomorphic, because another correspondence of the vertices in G and H may be an isomorphism. ... linkS� The best algorithms known for determining whether two graphs are isomorphic have expo­ nential worst-case time complexity (in the number of vertices of the graphs). However, linear average-case time complexity algorithms are known that solve this problem, and there is some hope that an algorithm with polynomial worst-case time complexity for determining whether two graphs are isomorphic can be found. The best practical algorithm, called NAUTY, can be used to determine whether two graphs with as many as 100 vertices are isomorphic in less than 1 second on a modern PC. The software for NAUTY can be downloaded over the Internet and experimented with. Exercises In Exercises 1-4 use an adjacency list to represent the given graph. 1. a b 2. a b c fSJ c d d e 3. 5. Represent the graph in Exercise with an adjacency matrix. 6. Represent the graph in Exercise 2 with an adjacency matrix. 7. Represent the graph in Exercise 3 with an adjacency matrix. 8. Represent the graph in Exercise 4 with an adjacency matrix. 9. Represent each of these graphs with an adjacency matrix. In Exercises 10-12 draw a graph with the given adjacency matrix. 10. In Exercises 13-15 represent the given graph using an adja­ cency matrix. 13. a b L1 c d 1�� c d 15. In Exercises 16-18 draw an undirected graph represented by the given adjacency matrix. 18. 9-3/ In Exercises 19-21 find the adjacency matrix of the given directed multi graph. 19. 20. 21. In Exercises 22-24 draw the graph represented by the given adjacency matrix. 22. U � !] 23. [1 2 1] 200 022 25. Is every zero-one square matrix that is symmetric and has zeros on the diagonal the ad jacency matrix of a sim­ ple graph? 26. Use an incidence matrix to represent the graphs in Exer­ cises 1 and 2. 27. Use an incidence matrix to represent the graphs in Exer­ cises 13-15. *28. What is the sum of the entries in a row of the adjacency matrix for an undirected graph? For a directed graph? *29. What is the sum of the entries in a column ofthe adjacency matrix for an undirected graph? For a directed graph? 30. What is the sum of the entries in a row of the incidence matrix for an undirected graph? 31. What is the sum ofthe entries in a column ofthe incidence matrix for an undirected graph? *32. Find an adjacency matrix for each of these graphs. a) Kn b) en c) Wn d) Km.n e) Qn *33. Find incidence matrices for the graphs in parts (a)-(d) of Exercise 32. In Exercises 3� determine whether the given pair of graphs is isomorphic. Exhibit an isomorphism or provide a rigorous argument that none exists. 34. U2 • 9.3 Representing Graphs and Graph Isomorphism 35. V2 V5t1"'------� V2 37. 39. V2 40. 620 9 / Graphs 42. Vg 43. 44. Vs • I Vg • 45. Show that isomorphism of simple graphs is an equiva­ lence relation. 46. Suppose that G and H are isomorphic simple graphs. Show that their complementary graphs G and H are also isomorphic. 9-32 47. Describe the row and column ofan adjacency matrix ofa graph corresponding to an isolated vertex. 48. Describe the row of an incidence matrix of a graph cor­ responding to an isolated vertex. 49. Show that the vertices of a bipartite graph with two or more vertices can be ordered so that its adjacency matrix has the form where the four entries shown are rectangular blocks. A simple graph G is called self-complementary if G and G are isomorphic. 50. Show that this graph is self-complem entary. a b n d c 51. Find a self-compleme ntary simple graph with five vertices. *52. Show that if G is a self-complemen tary simple graph with v vertices, then v == 0 or I (mod 4). 53. For which integers n is Cn self-complementary? 54. How many nonisomorphic simple graphs are there with n vertices, when n is a) 2? b) 3? c) 4? 55. How many nonisomorphic simple graphs are there with five vertices and three edges? 56. How many nonisomorphic simple graphs are there with six vertices and four edges? 57. Are the simple graphs with the following adjacency ma­ trices isomorphic? b) [0�1 1 0 1] [0 � � i ' i � [0011 1 1 0] [0 0001 0�1 0011] � � i ' � 9-33 58. Determine whether the graphs without loops with these incidence matrices are isomorphic. � ! � r],[� i ! r �] 111 010101 59. Extend the definition of isomorphism of simple graphs to undirected graphs containing loops and multiple edges. 60. Define isomorphism of directed graphs. In Exercises 61-64 determine whether the given pair of di­ rected graphs are isomorphic. (See Exercise 60.) 62. 63. 9.4 Connec tivity Introduction 64. Ul 9.4 Connectivity 621 65. Show that if G and H are isomorphic directed graphs, then the converses of G and H (defined in the preamble of Exercise 61 of Section 9.2) are also isomorphic. 66. Show that the property that a graph is bipartite is an iso­ morphic invariant. 67. Find a pair of nonisomorphic graphs with the same de­ gree sequence such that one graph is bipartite, but the other graph is not bipartite. *68. How many nonisomorphic directed simple graphs are there with n vertices, when n is a) 2? b) 3? c) 4? *69. What is the product of the incidence matrix and its trans­ pose for an undirected graph? *70. How much storage is needed to represent a simple graph with v vertices and e edges using a) adjacency lists? b) an adjacency matrix? c) an incidence matrix? A devil's pair for a purported isomorphism test is a pair of nonisomorphic graphs that the test fails to show are not isomorphic. 71. Find a devil's pair for the test that checks the degree sequence (defined in the preamble to Exercise 30 in Sec­ tion 9.2) in two graphs to make sure they agree. Many problems can be modeled with paths formed by traveling along the edges of graphs. For instance, the problem of determining whether a message can be sent between two computers using intermediate links can be studied with a graph model. Problems of efficiently planning routes for mail delivery, garbage pickup, diagnostics in computer networks, and so on can be solved using models that involve paths in graphs. 622 9 / Graphs DEFINITION 1 9-34 Paths Informally, a path is a sequence of edges that begins at a vertex of a graph and travels from vertex to vertex along edges of the graph. A formal definition of paths and related terminology is given in Definition 1. Let n be a nonnegative integer and G an undirected graph. A path of length n from u to v in G is a sequence of n edges el, ... , en of G such that el is associated with {xo, xd, e2 is associated with {Xl, X2}, and so on, with en associated with {xn-J, xn}, where Xo = u and X" = v. When the graph is simple, we denote this path by its vertex sequence Xo, Xl, ... , Xn (because listing these vertices uniquely determines the path). The path is a circuit ifit begins and ends at the same vertex, that is, if u = v, and has length greater than zero. The path or circuit is said topass through the vertices Xl , X2, ... , Xn-l or traverse the edges el, e2, ... , en. A path or circuit is simple if it does not contain the same edge more than once. When it is not necessary to distinguish between multiple edges, we will denote a path el, e2, ... , en, where ei is associated with {Xi-I, xd for i = 1,2, ... , n by its vertex sequence Xo, Xl, ... , Xn. This notation identifies a path only up to the vertices it passes through. There may be more than one path that passes through this sequence of vertices. Note that a path of length zero consists of a single vertex. Remark: There is considerable variation of terminology concerning the concepts defined in Defintion 1. For instance, in some books, the term walk is used instead of path, where a walk is defined to be an alternating sequence of vertices and edges of a graph, Vo, el, VI, e2, ... , Vn-I, en, Vn, where Vi-I and Vi are the endpoints of ei for i = 1,2, ... , n. When this terminology is used, closed walk is used instead of circuit to indicate a walk that begins and ends at the same vertex, and trail is used to denote a walk that has no repeated edge (replacing the term simple path). When this terminology is used, the terminology path is often used for a trail with no repeated vertices, conflicting with the terminology in Defi­ nition 1. Because of this variation in terminol ogy, you will need to make sure which set of definitions are used in a particular book or article when you read about traversing edges of a graph. The text [GrYe06] is a good reference for the alternative terminology described in this remark. EXAMPLE 1 In the simple graph shown in Figure 1, a, d, c, f, e is a simple path oflength 4, because {a, d}, {d, c}, {c, f}, and {f, e} are all edges. However, d, e, c, a is not a path, because {e, c} is not an edge. Note that b, c, f, e, b is a circuit oflength 4 because {b, c}, {c, f}, {f, e}, and {e, b} are edges, and this path begins and ends at b. The path a, b, e, d, a, b, which is of length 5, is not simple because it contains the edge {a, b} twice. .... a b c d e f FIGURE 1 A Simple Graph. 9-35 DEFINITION 2 EXAM PLE 2 UnkS� EXAM PLE 3 lInkS� EXAMPLE 4 UnkS� 9.4 Connectivity 623 Paths and circuits in directed graphs were introduced in Chapter 8. We now provide more general definitions. Let n be a nonnegative integer and G a directed graph. A path oflength n from u to v in G is a sequence of edges el, e2, ... ,en ofG such that el is associated with (xo, XI), e2 is associated with (XI, X2), and so on, with en associated with (Xn-I, xn), where Xo = u and Xn = v. When there are no multiple edges in the directed graph, this path is denoted by its vertex sequence xo, XI, X2, ... ,Xn• A path oflength greater than zero that begins and ends at the same vertex is called a circuit or cycle. A path or circuit is called simple if it does not contain the same edge more than once. Remark: Terminology other than that given in Definition 2 is often used for the concepts defined there. In particular, the alternative terminology that uses walk, closed walk, trail, and path (described in the remarks following Definition 1) may be used for directed graphs. See [GrY e06] for details. Note that the terminal vertex of an edge in a path is the initial vertex of the next edge in the path. When it is not necessary to distinguish between multiple edges, we will denote a path el, e2, ... , en, where ei is associated with (Xi -I, Xi) for i = 1, 2, ... , n by its vertex sequence xo, XI, ••. ,Xn• The notation identifies a path only up to the vertices it passes through. There may be more than one path that passes through this sequence of vertices. Paths represent useful information in many graph models, as Examples 2-4 demonstrate. Paths in Acquaintanceship Graphs In an acquaintanceship graph there is a path between two people ifthere is a chain of people linking these people, where two people adjacent in the chain know one another. For example, in Figure 7 in Section 9.1, there is a chain of six people linking Kamini and Ching. Many social scientists have conjectured that almost every pair of people in the world are linked by a small chain of people, perhaps containing just five or fewer people. This would mean that almost every pair of vertices in the acquaintanceship graph containing all people in the world is linked by a path oflength not exceeding four. The play Six Degrees of Separation by John Guare is based on this notion. .... Paths in Collaboration Graphs In a collaboration graph two vertices a and b, which represent authors, are connected by a path when there is a sequence of authors beginning at a and ending at b such thatthe two authors represented by the endpoints of each edge have written a joint paper. In the collaboration graph of all mathema ticians, the Erdos number of a mathematician m (defined in terms of relations in Supplementary Exercise 14 in Chapter 8) is the length of the shortest path between m and the vertex representing the extremely prolific mathematician Paul Erdos (who died in 1996). That is, the Erdos number of a mathematician is the length of the shortest chain of mathematicians that begins with Paul Erdos and ends with this mathemati cian, where each adjacent pair of mathemati cians have written a joint paper. The number of mathematicians with each Erdos number as of early 2006, according to the Erdos Number Project, is shown in Table 1. .... Paths in the Hollywood Graph In the Hollywood graph (see Example 4 in Section 9. 1) two vertices a and b are linked when there is a chain of actors linking a and b, where every two actors adjacent in the chain have acted in the same movie. In the Hollywood graph, the Bacon number of an actor c is defined to be the length of the shortest path connecting c and the well-known actor Kevin Bacon. As new movies are made, including new ones with Kevin Bacon, the Bacon 624 9 / Graphs DEFINITION 3 9-36 TABLE 1 The Number TABLE 2 The Number of Mathematicians of Actors with a Given with a Given Erdos Bacon Number (as of Number (�s of early early 2006). 2006). Bacon Number Erdos Number Number of People Number of People 0 1 0 1 1 1,902 1 504 2 160,463 2 6,593 3 457,231 3 33,605 4 111,310 4 83,642 5 8,168 5 87,760 6 810 6 40,014 7 81 7 11,591 8 14 8 3,146 9 819 10 244 11 68 12 23 13 5 number of actors can change. In Table 2 we show the number of actors with each Bacon number as of early 2006 using data from the Oracle of Bacon website. <III Connectedness In Undirected Graphs When does a computer network have the property that every pair of computers can share information, if messages can be sent through one or more intermediate computers? When a graph is used to represent this computer network, where vertices represent the computers and edges represent the communication links, this question becomes: When is there always a path between two vertices in the graph? An undirected graph is called connected if there is a path between every pair of distinct vertices of the graph. Thus, any two computers in the network can communicate if and only if the graph of this network is connected. EXAMPLE 5 The graph Gl in Figure 2 is connected, because for every pair of distinct vertices there is a path between them (the reader should verify this). However, the graph G2 in Figure 2 is not connected. For instance, there is no path in G2 between vertices a and d. <III We will need the following theorem in Chapter 10. 9-37 THEOREM l EXAMPLE 6 EXAMPLE 7 a b c f..---- .. d g G I e a b '�' d f FIGURE 2 The Graphs G1 and G2• 9.4 Connectivity 625 a c h g FIGURE 3 The Graph H and Its Connected Components HI, H2, and H3• There is a simple path between every pair of distinct vertices of a connected undirected graph. Proof: Let u and v be two distinct vertices of the connected undirected graph G = (V, E). Because G is connected, there is at least one path between u and v. Let Xo, XI, ... , Xn, where Xo = u and Xn = v, be the vertex sequence of a path of least length. This path of least length is simple. To see this, suppose it is not simple. Then Xi = Xi for some i and j with o :s i < j. This means that there is a path from u to v of shorter length with vertex sequence Xo, XI, .•• , Xi-I, xi, .•• , Xn obtained by deleting the edges corresponding to the vertex sequence xi, ... ,xi-I. <l A connected component of a graph G is a connected sub graph of G that is not a proper subgraph of another connected sub graph of G. That is, a connected component of a graph G is a maximal connected sub graph of G. A graph G that is not connected has two or more connected components that are disjoint and have G as their union. What are the connected components of the graph H shown in Figure 3? Solution: The graph H is the union of three disjoint connected subgraphs HI, H2, and H3, shown in Figure 3. These three subgraphs are the connected components of H. -<I Connected Components of Call Graphs Two vertices X and yare in the same component of a telephone call graph (see Example 7 in Section 9.1) when there is a sequence of telephone calls beginning at X and ending at y. When a call graph for telephone calls made during a particular day in the AT&T network was analyzed, this graph was found to have 53,767,087 vertices, more than 170 million edges, and more than 3.7 million connected components. Most of these components were small; approximately three-fourths consisted oftwo vertices representing pairs of telephone numbers that called only each other. This graph has one huge connected component with 44,989,297 vertices comprising more than 80% of the total. Furthermore, every vertex in this component can be linked to any other vertex by a chain of no more than 20 calls. -<I Sometimes the removal of a vertex and all edges incident with it produces a subgraph with more connected components than in the original graph. Such vertices are called cut vertices (or articulation points). The removal of a cut vertex from a connected graph produces a subgraph that is not connected. Analogousl y, an edge whose removal produces a graph with more connected components than in the original graph is called a cut edge or bridge. 626 9/ Graphs 9-38 EXAMPLE 8 Find the cut vertices and cut edges in the graph G shown in Figure 4. DEFINITION 4 DEFINITION 5 Solution: The cut vertices of G are b, c, and e. The removal of one of these vertices (and its adjacent edges) disconnects the graph. The cut edges are {a, b} and {c, e}. Removing either one ofthese edges disconnects G. ... Connect edness in Directed Graphs There are two notions of connectedness in directed graphs, depending on whether the directions of the edges are considered. A directed graph is strongly connected ifthere is a path from a to b and from b to a whenever a and b are vertices in the graph. For a directed graph to be strongly connected there must be a sequence of directed edges from any vertex in the graph to any other vertex. A directed graph can fail to be strongly connected but still be in "one piece." Definition 5 makes this notion precise. A directed graph is weakly connected if there is a path between every two vertices in the underlying undirected graph. That is, a directed graph is weakly connected if and only if there is always a path between two vertices when the directions of the edges are disregarded. Clearly, any strongly connected directed graph is also weakly connected. EXAMPLE 9 Are the directed graphs G and H shown in Figure 5 strongly connected? Are they weakly connected? Solution: G is strongly connected because there is a path between any two vertices in this directed graph (the reader should verify this). Hence, G is also weakly connected. The graph H is not strongly connected. There is no directed path from a to b in this graph. However, H is weakly connected, because there is a path between any two vertices in the underlying undirected graph of H (the reader should verify this). ... a b a b a d f g D> D> � [//J , d" d' bee h G H FIGURE 4 The Graph G. FIGURE 5 The Directed Graphs G and H. 9-39 EXAMPLE 10 EXAMPLE 11 unkS� 9.4 Connectivity 627 The subgraphs of a directed graph G that are strongly connected but not contained in larger strongly connected sub graphs, that is, the maximal strongly connected sub graphs, are called the strongly connected components or strong components of G . The graph H in Figure 5 has three strongly connected components, consisting of the vertex a; the vertex e; and the graph consisting of the vertices b, c, and d and edges (b, c), (c, d), and (d, b). .... The Strongly Connected Components of the Web Graph The Web graph introduced in Example 8 of Section 9.1 represents Web pages with vertices and links with directed edges. A snapshot of the Web in 1999 produced a Web graph with over 200 million vertices and over 1.5 billion edges. (See [BrOO] for details.) The underlying undirected graph of this Web graph is not connected and has a connected component that includes approximately 90% of the vertices in the graph. The sub graph of the original directed graph corresponding to this connected component of the underlying undirected graph (that is, with the same vertices and all directed edges connecting vertices in this graph) has one very large strongly connected component and many small ones. The former is called the giant strongly connected component (GSCC) of the directed graph. A Web page in this component can be reached following links starting at any other page in this component. The GSCC in the Web graph produced by this study was found to have over 53 million vertices. The remaining vertices in the large connected component of the undirected graph represent three different types of Web pages: pages that can be reached from a page in the GSCC, but do not link back to these pages following a series oflinks; pages that link back to pages in the GSCC following a series oflinks, but cannot be reached by following links on pages in the GSCC; and pages that cannot reach pages in the GSCC and cannot be reached from pages in the GSCC following a series of links. In this study, each of these three other sets was found to have approximately 44 million vertices. (It is rather surprising that these three sets are close to the same size.) .... Paths and Isomorphism There are several ways that paths and circuits can help determine whether two graphs are isomorphic. For example, the existence of a simple circuit of a particular length is a useful invariant that can be used to show that two graphs are not isomorphic. In addition, paths can be used to construct mappings that may be isomorphisms. As we mentioned, a useful isomorphic invariant for simple graphs is the existence of a simple circuit of length k, where k is a positive integer greater than 2. (The proof that this is an invariant is left as Exercise 50 at the end of this section.) Example 12 illustrates how this invariant can be used to show that two graphs are not isomorphic. EXAMPLE 12 Determine whether the graphs G and H shown in Figure 6 are isomorphic. Solution: Both G and H have six vertices and eight edges. Each has four vertices of degree three, and two vertices of degree two. So, the three invariants-number of vertices, number of edges, and degrees of vertices-all agree for the two graphs. However, H has a simple circuit of length three, namely, VI, V2, V6, VI, whereas G has no simple circuit of length three, as can be determined by inspection (all simple circuits in G have length at least four). Because the existence of a simple circuit of length three is an isomorphic invariant, G and H are not isomorphic. .... 628 9/ Graphs UI U6 U5 U4 G FIGURE 6 VI U2 V6 V2 U3 V5 V3 V4 H The Graphs G and H. 9-40 G H FIGURE 7 The Graphs G and H. We have shown how the existence of a type of path, namely, a simple circuit of a particular length, can be used to show that two graphs are not isomorphic. We can also use paths to find mappings that are potential isomorphisms. EXAMPLE 13 Determine whether the graphs G and H shown in Figure 7 are isomorphic. THEOREM 2 Solution: Both G and H have five vertices and six edges, both have two vertices of degree three and three vertices of degree two, and both have a simple circuit of length three, a simple circuit oflength four, and a simple circuit oflength five. Because all these isomorphic invariants agree, G and H may be isomorphic. To find a possible isomorphism, we can follow paths that go through all vertices so that the corresponding vertices in the two graphs have the same degree. For example, the paths UI, U4, U3, U2, U5 in G and V3, V2, VI, V5, V4 in H both go through every vertex in the graph; start at a vertex of degree three; go through vertices of degrees two, three, and two, respectively; and end at a vertex of degree two. By following these paths through the graphs, we define the mapping ! with !(UI) = V3, !(U4) = V2, !(U3) = VI, !(U2) = V5, and !(U5) = V4. The reader can show that! is an isomorphism, so G and H are isomorphic, either by showing that! preserves edges or by showing that with the appropriate orderings of vertices the adjacency matrices of G and H are the same. .... Counting Paths Between Vertices The number of paths between two vertices in a graph can be determined using its adjacency matrix. Let G be a graph with adjacency matrix A with respect to the ordering VI, V2, •.. , Vn (with directed or undirected edges, with multiple edges and loops allowed). The number of different paths of length r from Vi to Vj, where r is a positive integer, equals the (i, j)th entry of N . Proof: The theorem will be proved using mathematical induction. Let G be a graph with adjacency matrix A (assuming an ordering VI, V2, ... , Vn of the vertices of G). The number of paths from Vi to Vj of length 1 is the (i, j)th entry of A, because this entry is the number of edges from Vi to Vj. Assume that the (i, j)th entry of Ar is the number of different paths of length r from Vi to Vj. This is the induction hypothesis. Because Ar+1 = Ar A, the (i, j)th entry of Ar+1 equals 9-41 EXAMPLE 14 a b Z d c FIGURE 8 The Graph G. Extra� Examples � Exercises 9.4 Connectivity 629 where bik is the (i, k)th entry of N. By the induction hypothesis, bik is the number of paths of length r from Vi to Vk. A path of length r + I from Vi to Vi is made up of a path of length r from Vi to some intermediate vertex Vk, and an edge from Vk to vi' By the product rule for counting, the number of such paths is the product of the number of paths of length r from Vi to Vb namely, bik' and the number of edges from Vk to vi, namely, aki' When these products are added for all possible intermediate vertices Vb the desired result follows by the sum rule for counting. <l How many paths oflength four are there from a to d in the simple graph G in Figure 8? Solution: The adjacency matrix of G (ordering the vertices as a, b, c, d) is [0 I I 0] 100 1 A= I 0 0 1 . o 1 1 0 Hence, the number of paths oflength four from a to d is the (1, 4)th entry of A 4. Because [8 0 0 8] A4 = 0 8 8 0 o 8 8 0 ' 800 8 there are exactly eight paths oflength four from a to d. By inspection of the graph, we see that a,b,a,b,d;a,b,a,c,d;a,b,d,b,d;a,b,d,c,d;a,c,a,b,d;a,c,a,c,d;a,c,d,b,d;and a, c, d, c, d are the eight paths from a to d. � Theorem 2 can be used to find the length of the shortest path between two vertices of a graph (see Exercise 46), and it can also be used to determine whether a graph is connected (see Exercises 51 and 52). 1. Does each of these lists of vertices form a path in the following graph? Which paths are simple? Which are circuits? What are the lengths of those that are paths? c) a, d, b, e, a d) a, b,e, c,b,d,a c a) a,e,b, c,b b) a,e,a,d,b,c,a c) e,b,a,d,b,e d) c,b,d,a,e,c a b c d e 2. Does each of these lists of vertices form a path in the following graph? Which paths are simple? Which are cir­ cuits? What are the lengths of those that are paths? a) a,b,e, c,b b) a,d,a,d,a d e In Exercises 3-5 determine whether the given graph is connected. 630 9 / Graphs 4. 5. 6. How many connected components does each of the graphs in Exercises 3-5 have? For each graph find each of its con­ nected components. 7. What do the connected components of acquaintanceship graphs represent? 8. What do the connected components of a collaboration graph represent? 9. Explain why in the collaboration graph of mathematicians a vertex representing a mathematician is in the same con­ nected component as the vertex representing Paul Erdos if and only if that mathematician has a finite Erdos number. 10. In the Hollywood graph (see Example 4 in Section 9.1), when is the vertex representing an actor in the same connected component as the vertex representing Kevin Bacon? 11. Determine whether each of these graphs is strongly con­ nected and if not, whether it is weakly connected. a) .a� ______ �b � ____ � .. c c) e e b r---fri � f e d c 12. Determine whether each of these graphs is strongly con­ nected and if not, whether it is weakly connected. a) a b c \N\ f 0 A b) b ari4 f� e c) a b c ~ f e 9-42 13. What do the strongly connected components of a tele­ phone call graph represent? 14. Find the strongly connected components of each of these graphs. a) a ��----��----� .. b c e d b) .a� ______ �b� ______ �c f e d c) a b c d e 1. 1 1 'v i h g f 15. Find the strongly connected components of each of these graphs. a) b)� 9-43 16. Show that all vertices visited in a directed path connecting two vertices in the same strongly connected component of a directed graph are also in this strongly connected component. 17. Find the number of paths oflength n between two different vertices in K4 if n is a) 2. b) 3. c) 4. d) 5. 18. Use paths either to show that these graphs are not isomor­ phic or to find an isomorphism between these graphs. G H 19. Use paths either to show that these graphs are not isomor­ phic or to find an isomorphism between them. G H 20. Use paths either to show that these graphs are not isomor­ phic or to find an isomorphism between them. G H 21. Use paths either to show that these graphs are not isomor­ phic or to find an isomorphism between them. t4=ri ldj7-3 U5 U4 G H 9.4 Connectivity 631 22. Find the number of paths of length n between any two adjacent vertices in K3,3 for the values of n in Exercise 17. 23. Find the number of paths oflength n between any two non­ adjacent vertices in K3.3 for the values of n in Exercise 17. 24. Find the number of paths between c and d in the graph in Figure 1 of length a) 2. b) 3. c) 4. d) 5. e) 6. f) 7. 25. Find the number of paths from a to e in the directed graph in Exercise 2 oflength a) 2. b) 3. c) 4. d) 5. e) 6. f) 7. *26. Show that every connected graph with n vertices has at least n -1 edges. 27. Let G = (V, E) be a simple graph. Let R be the relation on V consisting of pairs of vertices (u, v) such that there is a path from u to v or such that u = v. Show that R is an equivalence relation. *28. Show that in every simple graph there is a path from any vertex of odd degree to some other vertex of odd degree. In Exercises 29-31 find all the cut vertices of the given graph. 29. a d e hlJ b c f 30. �� be d e 31. a b f e c __ ----�----__.g d h 32. Find all the cut edges in the graphs in Exercises 29-31. *33. Suppose that v is an endpoint of a cut edge. Prove that v is a cut vertex if and only if this vertex is not pendant. *34. Show that a vertex c in the connected simple graph G is a cut vertex if and only if there are vertices u and v, both different from c, such that every path between u and v passes through c. *35. Show that a simple graph with at least two vertices has at least two vertices that are not cut vertices. *36. Show that an edge in a simple graph is a cut edge if and only if this edge is not part of any simple circuit in the graph. 37. A communications link in a network should be provided with a backup link if its failure makes it impossible for some message to be sent. For each of the communications 632 9 I Graphs networks shown here in (a) and (b), determine those links that should be backed up. a) Chicago San Francisco Denver Washington Los Angeles b) sea� Portland San Denver / Francisco / Los Angeles " Salt Lake City Bangor Burlington ___..-=----." New York Boston A vertex basis in a directed graph is a set of vertices such that there is a path to every vertex in the directed graph not in the set from some vertex in this set and there is no path from any vertex in the set to another vertex in the set. 38. Find a vertex basis for each of the directed graphs in Ex­ ercises 7-9 of Section 9.2. 39. What is the significance of a vertex basis in an influence graph (described in Example 3 of Section 9.1)? Find a vertex basis in the influence graph in this example. 40. Show that if a connected simple graph G is the union of the graphs G, and G2, then G, and G2 have at least one common vertex. 1<41. Show that if a simple graph G has k connected com­ . ponents and these components have n" n2, ... ,nk ver­ tices, respective ly, then the number of edges of G does not exceed k LC(n;, 2). ;=, 1<42. Use Exercise 41 to show that a simple graph with n vertices and k connected components has at most (n -k) (n -k + 1)/2 edges. [Hint: First show that k Ln; � n2 -(k -1)(2n -k), ;=, where n; is the number of vertices in the ith connected component. ] 1<43. Show that a simple graph G with n vertices is connected if it has more than (n -1 )(n -2)/2 edges. 44. Describe the adjacency matrix of a graph with n con­ nected components when the vertices of the graph are listed so that vertices in each connected component are listed successively. 9-44 45. How many nonisomorphic connected simple graphs are there with n vertices when n is a) 2? b) 3? e) 4? d) 5? 46. Explain how Theorem 2 can be used to find the length of the shortest path from a vertex v to a vertex w in a graph. 47. Use Theorem 2 to find the length of the shortest path between a and f in the graph in Figure 1. 48. Use Theorem 2 to find the length ofthe shortest path from a to c in the directed graph in Exercise 2. I:3r 49. Let P, and P2 be two simple paths between the vertices u and v in the simple graph G that do not contain the same set of edges. Show that there is a simple circuit in G. 50. Show that the existence of a simple circuit of length k, where k is a positive integer greater than 2, is an isomor­ phic invariant. 51. Explain how Theorem 2 can be used to determine whether a graph is connected. 52. Use Exercise 51 to show that the graph G, in Figure 2 is connected whereas the graph G2 in that figure is not connected. 53. Show that a simple graph G is bipartite if and only if it has no circuits with an odd number of edges. 54. In an old puzzle attributed to Alcuin of York (735-804), a farmer needs to carry a wolf, a goat, and a cabbage across a river. The farmer only has a small boat, which can carry the farmer and only one object (an animal or a vegetable). He can cross the river repeatedly. However, if the farmer is on the other shore, the wolf will eat the goat, and, similarly, the goat will eat the cabbage. We can describe each state by listing what is on each shore. For example, we can use the pair (FG, WC) for the state where the farmer and goat are on the first shore and the wolf and cabbage are on the other shore. [The symbol 0 is used when nothing is on a shore, so that (FWGC, 0) is the initial state.] a) Find all allowable states of the puzzle, where neither the wolf and the goat nor the goat and the cabbage are left on the same shore without the farmer. b) Construct a graph such that each vertex of this graph represents an allowable state and the vertices repre­ senting two allowable states are connected by an edge if it is possible to move from one state to the other using one trip of the boat. e) Explain why finding a path from the vertex represent­ ing (FWGC, 0) to the vertex representing (0, FWGC) solves the puzzle. d) Find two different solutions ofthe puzzle, each using seven crossings. e) Suppose that the farmer must pay a toll of one dollar whenever he crosses the river with an animal. Which solution of the puzzle should the farmer use to pay the least total toll? *55. Use a graph model and a path in your graph, as in Exercise 54, to solve the jealous husbands problem. Two married couples, each a husband and a wife, want to cross 9-45 9.5 Euler and Hamilton Paths 633 a river. They can only use a boat that can carry one or two people from one shore to the other shore. Each husband is extremely jealous and is not willing to leave his wife with the other husband, either in the boat or on shore. How can these four people reach the opposite shore? empty either jug, and you may transfer water from either jug into the other jug. Use a path in a directed graph model to show that you can end up with a jug containing exactly one gallon. [Hint: Use an ordered pair (a, b) to indicate how much water is in each of the jugs and represent these ordered pairs by vertices. Add edges corresponding to the allowable operations with the jugs.] 56. Suppose that you have a three-gallon jug and a five-gallon jug, and you may fill either jug from a water tap, you may 9.5 Euler and Hamilton Paths unkS� DEFINITION 1 Introduction Can we travel along the edges of a graph starting at a vertex and returning to it by traversing each edge of the graph exactly once? Similarly, can we travel along the edges of a graph starting at a vertex and returning to it while visiting each vertex of the graph exactly once? Although these questions seem to be similar, the first question, which asks whether a graph has an Euler circuit, can be easily answered simply by examining the degrees of the vertices of the graph, while the second question, which asks whether a graph has a Hamilton circuit, is quite difficult to solve for most graphs. In this section we will study these questions and discuss the difficulty of solving them. Although both questions have many practical applications in many different areas, both arose in old puzzles. We will learn about these old puzzles as well as modern practical applications. Euler Paths and Circuits The town of Konigsberg, Prussia (now called Kaliningrad and part of the Russian republic), was divided into four sections by the branches of the Pregel River. These four sections in­ cluded the two regions on the banks of the Pregel, Kneiphof Island, and the region between the two branches of the Pregel. In the eighteenth century seven bridges connected these regions. Figure 1 depicts these regions and bridges. The townspeople took long walks through town on Sundays. They wondered whether it was possible to start at some location in the town, travel across all the bridges without crossing any bridge twice, and return to the starting point. The Swiss mathematician Leonhard Euler solved this problem. His solution, published in 1736, may be the first use of graph theory. Euler studied this problem using the multigraph obtained when the four regions are represented by vertices and the bridges by edges. This multigraph is shown in Figure 2. The problem of traveling across every bridge without crossing any bridge more than once can be rephrased in terms of this model. The question becomes: Is there a simple circuit in this multi graph that contains every edge? An Euler circuit in a graph G is a simple circuit containing every edge of G. An Euler path in G is a simple path containing every edge of G. Examples 1 and 2 illustrate the concept of Euler circuits and paths. EXAM PLE 1 Which of the undirected graphs in Figure 3 have an Euler circuit? Of those that do not, which have an Euler path? 634 9 / Graphs 9-46 c A __ --- �D B FIGURE 1 The Seven Bridges of Konigsberg. FIGURE 2 Multigraph Model of the Town of Konigsberg. EXAMPLE 2 Extra� Examples � Solution: The graph GI has an Euler circuit, forexample,a, e, c, d, e, b, a. Neither of the graphs G2 or G3 has an Euler circuit (the reader should verify this). However, G3 has an Euler path, namely, a, c, d, e, b, d, a, b. G2 does not have an Euler path (as the reader should verify). .... Which of the directed graphs in Figure 4 have an Euler circuit? Ofthose that do not, which have an Euler path? Solution: The graph H2 has an Euler circuit, for example, a, g, c, b, g, e, d, j, a. Neither HI nor H3 has an Euler circuit (as the reader should verify). H3 has an Euler path, namely, c, a, b, c, d, b, but HI does not (as the reader should verify). .... NECESSARY AND SUFFICIENT CONDITIONS FOR EULER CIRCUITS AND PATHS There are simple criteria for determining whether a multi graph has an Euler circuit or an Euler path. Euler discovered them when he solved the famous Konigsberg bridge problem. We will assume that all graphs discussed in this section have a finite number of vertices and edges. What can we say if a connected multigraph has an Euler circuit? What we can show is that every vertex must have even degree. To do this, first note that an Euler circuit begins with a vertex a and continues with an edge incident with a, say {a, b}. The edge {a, b} contributes one to deg( a). Each time the circuit passes through a vertex it contributes two to the vertex's degree, because the circuit enters via an edge incident with this vertex and leaves via another such edge. Finally, the circuit terminates where it started, contributing one to deg(a ). Therefore, deg(a) must be even, because the circuit contributes one when it begins, one when it ends, and two every time it passes through a (if it ever does). A vertex other than a has even degree because the circuit contributes two to its degree each time it passes through the vertex. We conclude that if a connected graph has an Euler circuit, then every vertex must have even degree. z�� d cd c c d e FIGURE 3 The Undirected Graphs GI, G2, and G3• FIGURE 4 The Directed Graphs HI, H2, and H3• 9-47 9.5 Euler and Hamilton Paths 635 Is this necessary condition for the existence of an Euler circuit also sufficient? That is, must an Euler circuit exist in a connected multigraph if all vertices have even degree? This question can be settled affirmatively with a construction. Suppose that G is a connected multi graph with at least two vertices and the degree of every vertex of G is even. We will form a simple circuit that begins at an arbitrary vertex a of G. Let Xo = a. First, we arbitrarily choose an edge {xo, Xl} incident with a which is possible because G is connected. We continue by building a simple path {xo, xd, {Xl, X2}, ... , {Xn-l, xn}, adding edges to the path until we cannot add another edge to the path. This happens when we reach a vertex for which we have already included all edges incident with that vertex in the path. For instance, in the graph G in Figure 5 we begin at a and choose in succession the edges {a, f}, {j, c}, {c, b}, and {b, a}. The path we have constructed must terminate because the graph has a finite number of edges, so we are guaranteed to eventually reach a vertex for which no edges are available to add to the path. The path begins at a with an edge of the form {a, x}, and we now show that it must terminate at a with an edge of the form {y, a}. To see that the path must terminate at a, note that each time the path goes through a vertex with even degree, it uses only one edge to enter this vertex, so because the degree must be at least two, at least one edge remains for the path to leave the vertex. Furthermore, every time we enter and leave a vertex of even degree, there are an even number of edges incident with this vertex that we have not yet used in our path. Consequently, as we form the path, every time we enter a vertex other than a, we can leave it. This means that path can end only at a. Next, note that the path we have constructed may use all the edges of the graph, or it may not if we have returned to a for the last time before using all the edges. An Euler circuit has been constructed if all the edges have been used. Otherwise, consider the subgraph H obtained from G by deleting the edges already used and vertices that are not incident with any remaining edges. When we delete the circuit a, f, c, b, a from the graph in Figure 5, we obtain the subgraph labeled as H. Because G is connected, H has at least one vertex in common with the circuit that has been deleted. Let w be such a vertex. (In our example, c is the vertex.) Every vertex in H has even degree (because in G all vertices had even degree, and for each vertex, pairs of edges incident with this vertex have been deleted to form H). Note that H may not be connected. Beginning at w, construct a simple path in H by choosing edges as long as possible, as was done in G. This path must terminate at w. For instance, in Figure 5, c, d, e, c is a path in H. Next, form a circuit in G by splicing the circuit in H with the original circuit in G (this can be done because w is one of the vertices in this circuit). When this is done in the graph in Figure 5, we obtain the circuit a, f, c, d, e, c, b, a. Continue this process until all edges have been used. (The process must terminate because there are only a finite number of edges in the graph.) This produces an Euler circuit. The LEONHARD EULER (1707-1783) Leonhard Euler was the son of a Calvinist minister from the vicinity of Basel, Switzerland. At 13 he entered the University of Basel, pursuing a career in theology, as his father wished. At the university Euler was tutored by Johann Bernoulli of the famous Bernoulli family of mathematicians. His interest and skills led him to abandon his theological studies and take up mathemati cs. Euler obtained his master's degree in philosophy at the age of 16. In 1727 Peter the Great invited him to join the Academy at St. Petersburg. In 1741 he moved to the Berlin Academy, where he stayed until 1766. He then returned to St. Petersburg, where he remained for the rest of his life. Euler was incredibly prolific, contributing to many areas of mathematics, including number theory, com­ binatorics, and analysis, as well as its applications to such areas as music and naval architecture. He wrote over 1100 books and papers and left so much unpublished work that it took 47 years after he died for all his work to be published. During his life his papers accumulated so quickly that he kept a large pile of articles awaiting publication. The Berlin Academy published the papers on top of this pile so later results were often published before results they depended on or superseded. Euler had 13 children and was able to continue his work while a child or two bounced on his knees. He was blind for the last 17 years of his life, but because of his fantastic memory this did not diminish his mathematical output. The project of publishing his collected works, undertaken by the Swiss Society of Natural Science, is ongoing and will require more than 75 volumes. 636 9 / Graphs THEOREM ! 9-48 a -b + t c d c d f --V e e G H FIGURE 5 Constructing an Euler Circuit in G. construction shows that if the vertices of a connected multigraph all have even degree, then the graph has an Euler circuit. We summarize these results in Theorem 1. A connected multigraph with at least two vertices has an Euler circuit if and only ifeach of its vertices has even degree. We can now solve the Konigsberg bridge problem. Because the multigraph representing these bridges, shown in Figure 2, has four vertices of odd degree, it does not have an Euler circuit. There is no way to start at a given point, cross each bridge exactly once, and return to the starting point. Algorithm 1 gives the constructive procedure for finding Euler circuits given in the discus­ sion preceding Theorem 1. (Because the circuits in the procedure are chosen arbitrarily, there is some ambiguity. We will not bother to remove this ambiguity by specifying the steps of the procedure more precisely.) ALGORITHM 1 Constructing Euler Circuits. procedure Euler(G: connected multigraph with all vertices of even degree) circuit := a circuit in G beginning at an arbitrarily chosen vertex with edges successively added to form a path that returns to this vertex H := G with the edges of this circuit removed while H has edges begin subcircuit := a circuit in H beginning at a vertex in H that also is an endpoint of an edge of circuit H := H with edges of subcircuit and all isolated vertices removed circuit := circuit with subcircuit inserted at the appropriate vertex end {circuit is an Euler circuit} Example 3 shows how Euler paths and circuits can be used to solve a type of puzzle. 9-49 a j f c k G FIGURE 6 Mohammed' s Scimitars. 9.5 Euler and Hamilton Paths 637 EXAMPLE 3 Many puzzles ask you to draw a picture in a continuous motion without lifting a pencil so that no part of the picture is retraced. We can solve such puzzles using Euler circuits and paths. For example, can Mohammed s scimitars, shown in Figure 6, be drawn in this way, where the drawing begins and ends at the same point? Solution: We can solve this problem because the graph G shown in Figure 6 has an Euler circuit. It has such a circuit because all its vertices have even degree. We will use Algorithm 1 to construct an Euler circuit. First, we form the circuit a, b, d, c, b, e, i, j, e, a. We obtain the subgraph H by deleting the edges in this circuit and all vertices that become isolated when these edges are removed. Then we form the circuit d, g, h, j, i, h, k, g, j, d in H. After forming this circuit we have used all edges in G. Splicing this new circuit into the first circuit at the appropriate place produces the Euler circuit a, b, d, g, h, j, i, h, k, g, j, d, c, b, e, i, j, e, a. This circuit gives a way to draw the scimitars without lifting the pencil or retracing part of the picture. .... Another algorithm for constructing Euler circuits, called Fleury's algorithm, is described in the exercises at the end of this section. We will now show that a connected multigraph has an Euler path (and not an Euler circuit) if and only if it has exactly two vertices of odd degree. First, suppose that a connected multigraph does have an Euler path from a to b, but not an Euler circuit. The first edge of the path contributes one to the degree of a. A contribution of two to the degree of a is made every time the path passes through a. The last edge in the path contributes one to the degree of b. Every time the path goes through b there is a contribution of two to its degree. Consequently, both a and b have odd degree. Every other vertex has even degree, because the path contributes two to the degree of a vertex whenever it passes through it. Now consider the converse. Suppose that a graph has exactly two vertices of odd degree, say a and b. Consider the larger graph made up of the original graph with the addition of an edge {a, b}. Every vertex of this larger graph has even degree, so there is an Euler circuit. The removal of the new edge produces an Euler path in the original graph. Theorem 2 summarizes these results. THEO REM 2 A connected mUltigraph has an Euler path but not an Euler circuit if and only if it has exactly two vertices of odd degree. EXAMPLE 4 Which graphs shown in Figure 7 have an Euler path? Solution: G I contains exactly two vertices of odd degree, namely, band d. Hence, it has an Euler path that must have band d as its endpoints. One such Euler path is d, a, b, c, d, b. Similarly, 638 9 / Graphs unkS� linkS� DEFINITION 2 FIGURE 7 Three Undirected Graphs. 9-50 Gz has exactly two vertices of odd degree, namely, band d. So it has an Euler path that must have band d as endpoints. One such Euler path is b, a, g, j, e, d, c, g, b, c, j, d. G3 has no Euler path because it has six vertices of odd degree. <III Returning to eighteenth-c entury Konigsberg, is it possible to start at some point in the town, travel across all the bridges, and end up at some other point in town? This question can be answered by determining whether there is an Euler path in the multigraph representing the bridges in Konigsberg. Because there are four vertices of odd degree in this multigraph, there is no Euler path, so such a trip is impossible. Necessary and sufficient conditions for Euler paths and circuits in directed graphs are discussed in the exercises at the end of this section. Euler paths and circuits can be used to solve many practical problems. For example, many applications ask for a path or circuit that traverses each street in a neighborhood, each road in a transportation network, each connection in a utility grid, or each link in a communications network exactly once. Finding an Euler path or circuit in the appropriate graph model can solve such problems. For example, if a postman can find an Euler path in the graph that represents the streets the postman needs to cover, this path produces a route that traverses each street of the route exactly once. If no Euler path exists, some streets will have to be traversed more than once. This problem is known as the Chinese postman problem in honor of Guan Meigu, who posed it in 1962. See [MiR091] for more information on the solution of the Chinese postman problem when no Euler path exists. Among the other areas where Euler circuits and paths are applied is in layout of circuits, in network multicasting, and in molecular biology, where Euler paths are used in the sequencing of DNA. Hamilton Paths and Circuits We have developed necessary and sufficient conditions for the existence of paths and circuits that contain every edge of a multigraph exactly on ce. Can we do the same for simple paths and circuits that contain every vertex of the graph exactly once? A simple path in a graph G that passes through every vertex exactly once is called a Hamilton path, and a simple circuit in a graph G that passes through every vertex exactly once is called a Hamilton circuit. That is, the simple pathxo, Xl, ... , xn-l, Xn in the graph G = (V, E) is a Hamilton path if V = {Xo, Xl, ... , Xn-l, xn} and Xi =I Xj for 0 :::: i < j :::: n, and the simple circuit xo, xl, ... ,Xn-l, Xn, Xo (with n > 0) is a Hamilton circuit if xo, Xl, ... ,Xn-l, Xn is a Hamilton path. 9-51 EXAMPLE S Extra" ExampleS � (a) (b) FIGURE 8 Hamilton's "A Voyage Round the World" Puzzle. 9.5 Euler and Hamilton Paths 639 FIGURE 9 A Solution to the "A Voyage Round the World" Puzzle. This terminology comes from a game, called the Icosian puzzle, invented in 1857 by the Irish mathematician Sir William Rowan Hamilton. It consisted of a wooden dodecahedron [a polyhedron with 12 regular pentagons as faces, as shown in Figure 8(a)], with a peg at each vertex of the dodecahedron, and string. The 20 vertices of the dodecahedron were labeled with different cities in the world. The object of the puzzle was to start at a city and travel along the edges of the dodecahedron, visiting each of the other 19 cities exactly once, and end back at the first city. The circuit traveled was marked off using the strings and pegs . Because the author cannot supply each reader with a wooden solid with pegs and string, we will consider the equivalent question: Is there a circuit in the graph shown in Figure 8(b) that passes through each vertex exactly once? This solves the puzzle because this graph is isomorphic to the graph consisting of the vertices and edges of the dodecahedron. A solution of Hamilton's puzzle is shown in Figure 9. Which of the simple graphs in Figure 10 have a Hamilton circuit or, ifnot, a Hamilton path? Solution: G 1 has a Hamilton circuit: a, b, c, d, e, a. There is no Hamilton circuit in G2 (this can be seen by noting that any circuit containing every vertex must contain the edge {a, b} twice), but G2 does have a Hamilton path, namely, a, b, c, d. G3 has neither a Hamilton circuit nor a Hamilton path, because any path containing all vertices must contain one of the edges {a, b}, {e, f}, and {c, d} more than once. .... Is there a simple way to determine whether a graph has a Hamilton circuit or path? At first, it might seem that there should be an easy way to determine this, because there is a simple way to answer the similar question of whether a graph has an Euler circuit. Surprisingl y, there are no known simple necessary and sufficient criteria for the existence of Hamilton circuits. However, many theorems are known that give sufficient conditions for the existence of Hamilton circuits. a b :zJ: I I e ..... --- .. c d c d FIGURE 10 Three Simple Graphs. d c e • f 640 9 / Graphs a d e a d U I><J b c b e G H FIGURE 11 Two Graphs That Do Not Have a Hamilton Circuit. 9-52 Also, certain properties can be used to show that a graph has no Hamilton circuit. For instance, a graph with a vertex of degree one cannot have a Hamilton circuit, because in a Hamilton circuit, each vertex is incident with two edges in the circuit. Moreover, if a vertex in the graph has degree two, then both edges that are incident with this vertex must be part of any Hamilton circuit. Also, note that when a Hamilton circuit is being constructed and this circuit has passed through a vertex, then all remaining edges incident with this vertex, other than the two used in the circuit, can be removed from consideration. Furthermore, a Hamilton circuit cannot contain a smaller circuit within it. EXAMPLE 6 Show that neither graph displayed in Figure 11 has a Hamilton circuit. LinllS� Solution: There is no Hamilton circuit in G because G has a vertex of degree one, namely, e. Now consider H. Because the degrees of the vertices a, b, d, and e are all two, every edge incident with these vertices must be part of any Hamilton circuit. It is now easy to see that no Hamilton circuit can exist in H, for any Hamilton circuit would have to contain four edges incident with c, which is impossible. .... . WILLIAM ROWAN HAMILTON (1805-1865) William Rowan Hamilton, the most famous Irish scien­ tist ever to have lived, was born in 1805 in Dublin. His father was a successful lawyer, his mother came from a family noted for their intelligence, and he was a child prodigy. By the age of 3 he was an excel­ lent reader and had mastered advanced arithmetic. Because of his brilliance, he was sent off to live with his uncle James, a noted linguist. By age 8 Hamilton had learned Latin, Greek, and Hebrew; by 10 he had also learned Italian and French and he began his study of oriental languages, including Arabic, Sanskrit, and Persian. During this period he took pride in knowing as many languages as his age. At 17, no longer de­ voted to learning new languages and having mastered calculus and much mathematical astronomy, he began original work in optics, and he also found an important mistake in Laplace's work on celestial mechanics. Before entering Trinity College, Dublin, at 18, Hamilton had not attended school; rather, he received private tutoring. At Trinity, he was a superior student in both the sciences and the classics. Prior to receiving his degree, because of his brilliance he was appointed the Astronomer Royal of Ireland, beating out several famous astronomers for the post. He held this position until his death, living and working at Dunsink Observatory outside of Dublin. Hamilton made important contributions to optics, abstract algebra, and dynamics. Hamilton invented algebraic objects called quaternions as an example of a noncommutative system. He discovered the appropriate way to multiply quaternions while walking along a canal in Dublin. In his excitement, he carved the formula in the stone of a bridge crossing the canal, a spot marked today by a plaque. Later, Hamilton remained obsessed with quaternions, working to apply them to other areas of mathematics, instead of moving to new areas of research. In 1857 Hamilton invented "The Icosian Game" based on his work in noncommutative algebra. He sold the idea for 25 pounds to a dealer in games and puzzles. (Because the game never sold well, this turned out to be a bad investment for the dealer.) The "Traveler's Dodecahedron," also called "A Voyage Round the World," the puzzle described in this section, is a variant of that game. Hamilton married his third love in 1833, but his marriage worked out poorly, because his wife, a semi-invalid, was unable to cope with his household affairs. He suffered from alcoholism and lived reclusively for the last two decades of his life. He died from gout in 1865, leaving masses of papers containing unpublished research. Mixed in with these papers were a large number of dinner plates, many containing the remains of desiccated, uneaten chops. 9-53 9.5 Euler and Hamilton Paths 641 EXAM PLE 7 Show that Kn has a Hamilton circuit whenever n ::: 3. THEOREM 3 THEOREM 4 unkS� Solution: We can form a Hamilton circuit in Kn beginning at any vertex. Such a circuit can be built by visiting vertices in any order we choose, as long as the path begins and ends at the same vertex and visits each other vertex exactly once. This is possible because there are edges in Kn between any two vertices. .... Although no useful necessary and sufficient conditions for the existence of Hamilton circuits are known, quite a few sufficient conditions have been found. Note that the more edges a graph has, the more likely it is to have a Hamilton circuit. Furthermore, adding edges (but not vertices) to a graph with a Hamilton circuit produces a graph with the same Hamilton circuit. So as we add edges to a graph, especially when we make sure to add edges to each vertex, we make it increasingly likely that a Hamilton circuit exists in this graph. Consequently, we would expect there to be sufficient conditions for the existence of Hamilton circuits that depend on the degrees of vertices being sufficiently large. We state two of the most important sufficient conditions here. These conditions were found by Gabriel A. Dirac in 1952 and Oystein Ore in 1960. DIRAC'S THEOREM If G is a simple graph with n vertices with n ::: 3 such that the degree of every vertex in G is at least n /2, then G has a Hamilton circuit. ORE'S THEOREM If G is a simple graph with n vertices with n ::: 3 such that deg(u) + deg( v) ::: n for every pair of nonadjacent vertices u and v in G, then G has a Hamilton circuit. The proof of Ore's Theorem is outlined in Exercise 65 at the end of this section. Dirac's Theorem can be proved as a corollary to Ore's Theorem because the conditions of Dirac's Theorem imply those of Ore's Theorem. Both Ore's Theorem and Dirac's Theorem provide sufficient conditions for a connected simple graph to have a Hamilton circuit. However, these theorems do not provide necessary conditions for the existence of a Hamilton circuit. For example, the graph Cs has a Hamilton circuit but does not satisfy the hypotheses of either Ore's Theorem or Dirac's Theorem, as the reader can verify. The best algorithms known for finding a Hamilton circuit in a graph or determining that no such circuit exists have exponential worst-case time complexity (in the number of vertices of the graph). Finding an algorithm that solves this problem with polynomial worst-case time complexity would be a major accomplish ment because it has been shown that this problem is NP­ complete (see Section 3.3). Consequently, the existence of such an algorithm would imply that many other seemingly intractable problems could be solved using algorithms with polynomial worst-case time complexity. Hamilton paths and circuits can be used to solve practical problems. For example, many applications ask for a path or circuit that visits each road intersection in a city, each place pipelines intersect in a utility grid, or each node in a communications network exactly once. Finding a Hamilton path or circuit in the appropriate graph model can solve such problems. The famous traveling salesman problem asks for the shortest route a traveling salesman should take to visit a set of cities. This problem reduces to finding a Hamilton circuit in a complete graph such that the total weight of its edges is as small as possible. We will return to this question in Section 9.6. 642 9 / Graphs FIGURE 12 Converting the Position of a Pointer into Digital Form. We now describe a less obvious application of Hamilton circuits to coding. 9-54 EXAMPLE 8 Gray Codes The position of a rotating pointer can be represented in digital form. One way to do this is to split the circle into 2n arcs of equal length and to assign a bit string of length n to each arc. Two ways to do this using bit strings oflength three are shown in Figure 12. The digital representation of the position of the pointer can be determined using a set of n contacts. Each contact is used to read one bit in the digital representation of the position. This is illustrated in Figure 13 for the two assignments from Figure 12. When the pointer is near the boundary of two arcs, a mistake may be made in reading its position. This may result in a major error in the bit string read. For instance, in the coding First bit is 1 here Third bit is 1 here Third bit is 1 here Second bit is 1 here FIGURE 13 The Digital Representation ofthe Position of the Pointer. 9-55 unkS� Exercises 110 111 100 ff---+--fflO l �-t---"; Ol1 000 001 FIGURE 14 A Hamilton Circuit for Q3. 9.5 Euler and Hamilton Paths 643 scheme in Figure 12(a), if a small error is made in determining the position of the pointer, the bit string 100 is read instead of OIl. All three bits are incorrect! To minimize the effect of an error in determining the position of the pointer, the assignment of the bit strings to the 2n arcs should be made so that only one bit is different in the bit strings represented by adjacent arcs. This is exactly the situation in the coding scheme in Figure 12(b). An error in determining the position of the pointer gives the bit string 010 instead of 0 11. Only one bit is wrong. A Gray code is a labeling of the arcs of the circle such that adjacent arcs are labeled with bit strings that differ in exactly one bit. The assignment in Figure 12(b) is a Gray code. We can find a Gray code by listing all bit strings of length n in such a way that each string differs in exactly one position from the preceding bit string, and the last string differs from the first in exactly one position. We can model this problem using the n-cube Qn. What is needed to solve this problem is a Hamilton circuit in Qn. Such Hamilton circuits are easily found. For instance, a Hamilton circuit for Q3 is displayed in Figure 14. The sequence of bit strings differing in exactly one bit produced by this Hamilton circuit is 000, 001, 011, 010, 110, Ill, 101, 100. Gray codes are named after Frank Gray, who invented them in the 1940s at AT&T Bell Laboratories to minimize the effect of errors in transmitting digital signals. .... In Exercises 1-8 determine whether the given graph has an Euler circuit. Construct such a circuit when one exists. If no Euler circuit exists, determine whether the graph has an Euler path and construct such a path if one exists. 3. a b ~ 4. a *---+----. b 1. b c JIV d e 2. a b c d�--��e--__:�1 g h c d b 5. a .------:. e e c c 644 9 / Graphs d e a __ -------- �� ------ __ �--� h g f 7. 8. a .-----�----��----.------. b d e c f�----��----�----�._----�j k m n o f 9. In Kaliningrad (the Russian name for Konigsberg) there are two additional bridges, besides the seven that were present in the eighteenth century. These new bridges con­ nect regions B and C and regions B and D, respective ly. Can someone cross all nine bridges in Kaliningrad exactly once and return to the starting point? 10. Can someone cross all the bridges shown in this map ex­ actly once and return to the starting point? 11. When can the centerlines of the streets in a city be painted without traveling a street more than once? (Assume that all the streets are two-way streets.) 12. Devise a procedure, similar to Algorithm I, for construct- ing Euler paths in multi graphs. In Exercises 13-15 determine whether the picture shown can be drawn with a pencil in a continuous motion without lifting the pencil or retracing part of the picture. 9-56 14. [ ] 15. *16. Show that a directed multigraph having no isolated ver­ tices has an Euler circuit if and only ifthe graph is weakly connected and the in-degree and out-degree of each vertex are equal. *17. Show that a directed multigraph having no isolated ver­ tices has an Euler path but not an Euler circuit if and only if the graph is weakly connected and the in-degree and out­ degree of each vertex are equal for all but two vertices, one that has in-degree one larger than its out-degree and the other that has out-degree one larger than its in-degree. In Exercises 18-23 determine whether the directed graph shown has an Euler circuit. Construct an Euler circuit if one exists. If no Euler circuit exists, determine whether the di­ rected graph has an Euler path. Construct an Euler path if one exists. b 18. a ------------:IiI 20. a b d e b 19. a ------------:IiI d c c 21. a�--------:�b�:::::=�C 9-57 23. a b c d e f h g k J *24. Devise an algorithm for constructing Euler circuits in di­ rected graphs. 25. Devise an algorithm for constructing Euler paths in di­ rected graphs. 26. For which values of n do these graphs have an Euler circuit? a) Kn b) en c) Wn d) Qn 27. For which values of n do the graphs in Exercise 26 have an Euler path but no Euler circuit? 28. For which values of m and n does the complete bipartite graph Km•n have an a) Euler circuit? b) Euler path? 29. Find the least number of times it is necessary to lift a pencil from the paper when drawing each of the graphs in Exercises 1-7 without retracing any part of the graph. In Exercises 30-36 determine whether the given graph has a Hamilton circuit. If it does, find such a circuit. If it does not, give an argument to show why no such circuit exists. 30. a d c f b e b c e d d e f 33. • e 34. a d i 0 n 9.5 Euler and Hamilton Paths 645 a b g k><J: c d f b c j k P q I m g c h �------��----�f 37. Does the graph in Exercise 30 have a Hamilton path? If so, find such a path. If it does not, give an argument to show why no such path exists. 38. Does the graph in Exercise 31 have a Hamilton path? If so, find such a path. If it does not, give an argument to show why no such path exists. 39. Does the graph in Exercise 32 have a Hamilton path? If so, find such a path. If it does not, give an argument to show why no such path exists. 40. Does the graph in Exercise 33 have a Hamilton path? If so, find such a path. If it does not, give an argument to show why no such path exists. *41. Does the graph in Exercise 34 have a Hamilton path? If so, find such a path. If it does not, give an argument to show why no such path exists. 42. Does the graph in Exercise 35 have a Hamilton path? If so, find such a path. If it does not, give an argument to show why no such path exists. 43. Does the graph in Exercise 36 have a Hamilton path? If so, find such a path. If it does not, give an argument to show why no such path exists. 646 9 / Graphs 44. For which values of n do the graphs in Exercise 26 have a Hamilton circuit? 45. For which values of m and n does the complete bipartite graph Km,n have a Hamilton circuit? *46. Show that the Petersen graph, shown here, does not have a Hamilton circuit, but that the sub graph obtained by deleting a vertex v, and all edges incident with v, does have a Hamilton circuit a e b d c 47. For each of these graphs, determine (i) whether Dirac's Theorem can be used to show that the graph has a Hamil­ ton circuit, (ii) whether Ore's Theorem can be used to show that the graph has a Hamilton circuit, and (iii) whether the graph has a Hamilton circuit a) b) LinIIS� 9-58 C)[g] d)C!J 48. Can you find a simple graph with n vertices w ith n ::: 3 that does not have a Hamilton circuit, yet the degree of every vertex in the graph is at least (n -1)/2? *49. Show that there is a Gray code of order n whenever n is a positive integer, or equivalently, show that the n­ cube Qn, n > 1, always has a Hamilton circuit [Hint: Use mathematical induction, Show how to produce a Gray code of order n from one of order n -1,] � Fleury's algorithm for constructing Euler circuits begins with an arbitrary vertex of a connected multigraph and forms a circuit by choosing edges successively, Once an edge is cho­ sen, it is removed, Edges are chosen successively so that each edge begins where the last edge ends, and so that this edge is not a cut edge unless there is no alternative, 50. Use Fleury's algorithm to find an Euler circuit in the graph G in Figure 5, *51. Express Fleury's algorithm in pseudocode, **52. Prove that Fleury's algorithm always produces an Euler circuit *53. Give a variant of Fleury's algorithm to produce Euler paths, 54. A diagnostic message can be sent out over a computer network to perform tests over all links and in all devices, What sort of paths should be used to test all links? To test all devices? 55. Show that a bipartite graph with an odd number of vertices does not have a Hamilton circuit JULIUS PETER CHRISTIAN PETERSE N (1839-1910) Julius Petersen was born in the Danish town of Som, His father was a dyer, In 1854 his parents were no longer able to pay for his schooling, so he became an apprentice in an uncle's grocery store, When this uncle died, he left Petersen enough money to return to schooL After graduating, he began studying engineering at the Polytechnical School in Copenhagen, later deciding to concentrate on mathema tics, He published his first textbook, a book on logarithms, in 1858. When his inheritance ran out, he had to teach to make a living, From 1859 until 1871 Petersen taught at a prestigious private high school in Copenhagen, While teaching high school he continued his studies, entering Copenhagen University in 1862, He married Laura Bertelsen in 1862; they had three children, two sons and a daughter. Petersen obtained a mathematics degree from Copenhagen University in 1866 and finally obtained his doctorate in 1871 from that schooL After receiving his doctorate, he taught at a polytechnic and military academy. In 1887 he was appointed to a professorship at the University of Copenhagen. Petersen was well known in Denmark as the author of a large series of textbooks for high schools and universities, One of his books, Methods and Theories for the Solution of Problems of Geometrical Construction, was translated into eight languages, with the English language version last reprinted in 1960 and the French version reprinted as recently as 1990, more than a century after the original publication date. Petersen worked in a wide range of areas, including algebra, analysis, cryptography, geometry, mechanics, mathematical economics, and number theory. His contributions to graph theory, including results on regular graphs, are his best-known work. He was noted for his clarity of exposition, problem-solving skills, originality, sense of humor, vigor, and teaching, One interesting fact about Petersen was that he preferred not to read the writings of other mathema ticians. This led him often to rediscover results already proved by others, often with embarrassing consequences. However, he was often angry when other mathematicians did not read his writings! Petersen's death was front-page news in Copenhagen. A newspaper of the time described him as the Hans Christian Andersen of science-a child of the people who made good in the academic world. 9-59 A knight is a chess piece that can move either two spaces horizontally and one space vertically or one space horizon­ tally and two spaces vertically. That is, a knight on square (x, y) can move to any of the eight squares (x ± 2, y ± 1), (x ± 1, y ± 2), if these squares are on the chessboa rd, as il­ lustrated here. A knight's tour is a sequence oflegal moves by a knight start­ ing at some square and visiting each square exactly once. A knight's tour is called reentrant if there is a legal move that takes the knight from the last square of the tour back to where the tour began. We can model knight's tours using the graph that has a vertex for each square on the board, with an edge connecting two vertices if a knight can legally move between the squares represented by these vertices. 56. Draw the graph that represents the legal moves of a knight on a 3 x 3 chessboard. 57. Draw the graph that represents the legal moves of a knight on a 3 x 4 chessboard. 58. a) Show that finding a knight's tour on an m x n chess­ board is equivalent to finding a Hamilton path on the graph representing the legal moves of a knight on that board. b) Show that finding a reentrant knight's tour on an m x n chessboard is equivalent to finding a Hamilton circuit on the corresponding graph. �59. Show that there is a knight's tour on a 3 x 4 chessboard. �60. Show that there is no knight's tour on a 3 x 3 chessboard. 9.6 Shortest-Path Problems Introduction 9.6 Shortest-Path Problems 647 *61. Show that there is no knight's tour on a 4 x 4 chessboard. 62. Show that the graph representing the legal moves of a knight on an m x n chessboa rd, whenever m and n are positive integers, is bipartite. 63. Show that there is no reentrant knight's tour on an m x n chessboard when m and n are both odd. [Hint: Use Exer­ cises 55, 58b, and 62.] *64. Show that there is a knight's tour on an 8 x 8 chessboard. [Hint: You can construct a knight's tour using a method invented by H. C. Wamsdorff in 1823: Start in any square, and then always move to a square connected to the fewest number of unused squares. Although this method may not always produce a knight's tour, it often does.] 65. The parts ofthis exercise outline a proof of Ore's Theorem. Suppose that G is a simple graph with n vertices, n ::: 3, and deg(x) + deg(y) ::: n whenever x and yare nonadja­ cent vertices in G. Ore's Theorem states that under these conditions, G has a Hamilton circuit. a) Show that if G does not have a Hamilton circuit, then there exists another graph H with the same vertices as G, which can be constructed by adding edges to G such that the addition of a single edge would produce a Hamilton circuit in H. [Hint: Add as many edges as possible at each successive vertex of G without pro­ ducing a Hamilton circuit.] b) Show that there is a Hamilton path in H. c) Let VI, V2, ... , Vn be a Hamilton path in H. Show that deg(vI) + deg(vn) ::: n and that there are at most deg( VI) vertices not adjacentto Vn (including Vn itself). d) Let S be the set of vertices preceding each vertex adja­ cent to VI in the Hamilton path. Show that S contains deg(vI) vertices and Vn !if-S. e) Show that S contains a vertex Vk, which is adjacent to Vn, implying that there are edges connecting VI and Vk+1 and Vk and Vn. t) Show that part (e) implies that VI, V2,···, Vk-I, Vb Vn, Vn-I, .•. , Vk+ I, VI is a Hamilton circuit in G. Conclude from this contradiction that Ore's Theorem holds. Many problems can be modeled using graphs with weights assigned to their edges. As an illustration, consider how an airline system can be modeled. We set up the basic graph model by representing cities by vertices and flights by edges. Problems involving distances can be modeled by assigning distances between cities to the edges. Problems involving flight time can be modeled by assigning flight times to edges. Problems involving fares can be modeled by assigning fares to the edges. Figure 1 displays three different assignments of weights to the edges of a graph representing distances, flight times, and fares, respectivel y. Graphs that have a number assigned to each edge are called weighted graphs. Weighted graphs are used to model computer networks. Communications costs (such as the monthly cost 648 9 / Graphs San Francisco ft""----:::------::;oe-� 4.05 FLIGHT TIMES San Francisco F-----:::----...."...-- San Francisco �----:�--..",.� FIGURE 1 Weighted Graphs Modeling an Airline System. 9-60 Boston Boston Boston of leasing a telephone line), the response times of the computers over these lines, or the distance between computers, can all be studied using weighted graphs. Figure 2 displays weighted graphs that represent three ways to assign weights to the edges of a graph of a computer network, corresponding to distance, response time, and cost. Several types of problems involving weighted graphs arise frequently. Determining a path of least length between two vertices in a network is one such problem. To be more specific, let the length of a path in a weighted graph be the sum of the weights of the edges of this path. (The reader should note that this use of the term length is different from the use of length to denote the number of edges in a path in a graph without weights.) The question is: What is a shortest path, that is, a path of least length, between two given vertices? For instance, in the airline system represented by the weighted graph shown in Figure 1, what is a shortest path in air distance between Boston and Los Angeles? What combinations of flights has the smallest total 9-61 unkS� DISTANCE Boston S lC�' � �====18=5=5==�������--���� an Fran,lsco 722 New York 349 Dallas RESPONSE TIME Boston Chicago San Fran,C�isC�O�====5=sec=3���?/�=--4�:::�New York 3 sec Dallas LEASE RATES (PER MONTH) Boston San Fran,C�iSC�O��=�$1=5=00=�����1=---����New York $400 Dallas 9.6 Shortest-Path Problems 649 FIGURE 2 Weighted Graphs Modeling a Computer Network. flight time (that is, total time in the air, not including time between flights) between Boston and Los Angeles? What is the cheapest fare between these two cities? In the computer network shown in Figure 2, what is a least expensive set oftelephone lines needed to connect the computers in San Francisco with those in New York? Which set of telephone lines gives a fastest response time for communications between San Francisco and New York? Which set of lines has a shortest overall distance? Another important problem involving weighted graphs asks for a circuit of shortest total length that visits every vertex of a complete graph exactly once. This is the famous traveling salesman problem, which asks for an order in which a salesman should visit each of the cities on his route exactly once so that he travels the minimum total distance. We will discuss the traveling salesman problem later in this section. A Shortest-Path Algorithm There are several different algorithms that find a shortest path between two vertices in a weighted graph. We will present an algorithm discovered by the Dutch mathema tician Edsger Dijkstra in 1959. The version we will describe solves this problem in undirected weighted graphs where all the weights are positive. It is easy to adapt it to solve shortest-path problems in directed graphs. Before giving a formal presentation of the algorithm, we will give a motivating example. 650 9 / Graphs b 3 c 2 3 a z d 3 e FIGURE 3 A Weighted Simple Graph. 9-62 EXAMPLE 1 What is the length of a shortest path between a and z in the weighted graph shown in Figure 3? Solution: Although a shortest path is easily found by inspection, we will develop some ideas useful in understanding Dijkstra's algorithm. We will solve this problem by finding the length of a shortest path from a to successive vertices, until z is reached. The only paths starting at a that contain no vertex other than a (until the terminal vertex is reached) are a, b and a, d. Because the lengths of a, b and a, dare 4 and 2, respectively, it follows that d is the closest vertex to a. We can find the next closest vertex by looking at all paths that go through only a and d (until the terminal vertex is reached). The shortest such path to b is still a, b, with length 4, and the shortest such path to e is a, d, e, with length 5. Consequently, the next closest vertex to a is b. To find the third closest vertex to a, we need to examine only paths that go through only a, d, and b (until the terminal vertex is reached). There is a path oflength 7 to c, namely, a, b, c, and a path of length 6 to z, namely, a, d, e, z. Consequently, z is the next closest vertex to a, and the length of a shortest path to z is 6. .... Example I illustrates the general principles used in Dijkstra's algorithm. Note that a shortest path from a to z could have been found by inspection. However, inspection is impractical for both humans and computers for graphs with large numbers of edges. We will now consider the general problem of finding the length of a shortest path between a and z in an undirected connected simple weighted graph. Dijkstra's algorithm proceeds by finding the length of a shortest path from a to a first vertex, the length of a shortest path from a to a second vertex, and so on, until the length of a shortest path from a to z is found. The algorithm relies on a series of iterations. A distinguished set of vertices is constructed by adding one vertex at each iteration. A labeling procedure is carried out at each iteration. In this labeling procedure, a vertex w is labeled with the length of a shortest path from a to w that contains only vertices already in the distinguished set. The vertex added to the distinguished set is one with a minimal label among those vertices not already in the set. EDSGER WYBE DIJKSTRA (1930-2002) Edsger Dijkstra, born in the Netherlands, began programming computers in the early 1950s while studying theoretical physics at the University of Lei den. In 1952, realizing that he was more interested in programming than in physics, he quickly completed the requirements for his physics degree and began his career as a programmer, even though programming was not a recognized profession. (In 1957, the authorities in Amsterdam refused to accept "programming" as his profession on his marriage license. However, they did accept "theoretical physicist" when he changed his entry to this.) Dijkstra was one of the most forceful proponents of programming as a scientific discipline. He has made fundamental contributions to the areas of operating systems, including deadlock avoidance; programming languages, including the notion of structured programming; and algorithms. In 1972 Dijkstra received the Turing Award from the Association for Computing Machinery, one of the most prestigious awards in computer science. Dijkstra became a Burroughs Research Fellow in 1973, and in 1984 he was appointed to a chair in Computer Science at the University of Texas, 9-63 9.6 Shortest-Path Problems 651 We now give the details of Dijkstra's algorithm. It begins by labeling a with 0 and the other vertices with 00. We use the notation Lo(a) = 0 and Lo(v) = 00 for these labels before any iterations have taken place (the subscript 0 stands for the "Oth" iteration). These labels are the lengths of shortest paths from a to the vertices, where the paths contain only the vertex a. (Because no path from a to a vertex different from a exists, 00 is the length of a shortest path between a and this vertex.) Dijkstra's algorithm proceeds by forming a distinguished set of vertices. Let Sk denote this set after k iterations of the labeling procedure. We begin with So = 0. The set Sk is formed from Sk-l by adding a vertex u not in Sk-l with the smallest label. Once u is added to Sk, we update the labels of all vertices not in Sk, so that Lk(V), the label of the vertex vat the kth stage, is the length of a shortest path from a to v that contains vertices only in Sk (that is, vertices that were already in the distinguished set together with u). Let v be a vertex not in Sk. To update the label of v, note that Lk(V) is the length ofa shortest path from a to v containing only vertices in Sk. The updating can be carried out efficiently when this observation is used: A shortest path from a to v containing only elements of Sk is either a shortest path from a to v that contains only elements of Sk-l (that is, the distinguished vertices not including u), or it is a shortest path from a to u at the (k - I)st stage with the edge (u, v) added. In other words, Lk(a, v) = min{Lk_1(a, v), Lk-l(a, u) + w(u, v)}. This procedure is iterated by successively, adding vertices to the distinguished set until z is added. When z is added to the distinguished set, its label is the length of a shortest path from a to z. Dijkstra's algorithm is given in Algorithm I. Later we will give a proof that this algorithm is correct. ALGORITHM 1 Dijkstra's Algorithm. procedure Dijkstra(G: weighted connected simple graph, with all weights positive) (G has vertices a = Va, VI, ••• , Vn = z and weights w(v;, Vj) where w(v;, Vj) = 00 if {v;, Vj} is not an edge in G) for i := I to n L(v;) := 00 L(a):= 0 S:= 0 {the labels are now initialized so that the label of a is 0 and all other labels are 00, and S is the empty set} while z rt S begin u := a vertex not in S with L(u) minimal S:= S U {u} for all vertices V not in S if L(u) + w(u, v) < L(v) then L(v) := L(u) + w(u, v) {this adds a vertex to S with minimal label and updates the labels of vertices not in S} end (L(z) = length of a shortest path from a to z} Example 2 illustrates how Dijkstra's algorithm works. Afterward, we will show that this algorithm always produces the length of a shortest path between two vertices in a weighted graph. 652 9 / Graphs 9-64 boo doo b 4(a) doo b 3 (a, c) d 10 (a, c) 4 5 Oa zOO O@ zOO O@ 2 z 00 10 3 coo eoo eoo (02(a) e 12 (a, c) (a) (b) (e) ® 3 (a, c) d 8 (a, c, b) ® 3 (a, c) 4 5 O@ z 00 O@ 8 2 z 14 (a, c, b, d) 2 10 e 12 (a, c) (02(a) e 10 (a, c, b, d) (e) ® 3 (a, c) @8(a,c,b) ® 3 (a, c) @8(a,c,b) O@ z 13 (a, c, b, d, e) O@ 013 (a, c, b, d, e) 10 3 2 10 3 (02(a) 01O(a,c,b,d) (02(a) o 10(a, c, b, d) (f) (g) FIGURE 4 Using Dijkstra's Algorithm to Find a Shortest Path from a to Z. EXAMPLE 2 Use Dijkstra's algorithm to find the length 0'£ a shortest path between the vertices a and z in the weighted graph displayed in Figure 4(a). Solution: The steps used by Dijkstra's algorithm to find a shortest path between a and z are shown in Figure 4. At each iteration of the algorithm the vertices of the set Sk are circled. A shortest path from a to each vertex containing only vertices in Sk is indicated for each iteration. The algorithm terminates when z is circled. We find that a shortest path from a to z is a, c, b, d, e, z, with length 13. .... Remark: In performing Dijkstra's algorithm it is sometimes more convenient to keep track of labels of vertices in each step using a table instead of redrawing the graph for each step. Next, we use an inductive argument to show that Dijkstra's algorithm produces the length of a shortest path between two vertices a and z in an undirected connected weighted graph. Take as the induction hypothesis the following assertion: At the kth iteration (i) the label of every vertex v in S is the length of a shortest path from a to this vertex, and (ii) the label of every vertex not in S is the length of a shortest path from a to this vertex that contains only (besides the vertex itself) vertices in S. When k = 0, before any iterations are carried out, S = 0, so the length of a shortest path from a to a vertex other than a is 00. Hence, the basis case is true. Assume that the inductive hypothesis holds for the kth iteration. Let v be the vertex added to S at the (k + 1 )st iteration, so v is a vertex not in S at the end of the kth iteration with the smallest label (in the case of ties, any vertex with smallest label may be used). 9-65 THEOREM l THEOREM 2 LinkS� 9.6 Shortest-Path Problems 653 From the inductive hypothesis we see that the vertices in S before the (k + 1 )st iteration are labeled with the length of a shortest path from a. Also, v must be labeled with the length of a shortest path to it from a. If this were not the case, at the end of the kth iteration there would be a path oflength less than L k( v) containing a vertex not in S [because Lk( v) is the length of a shortest path from a to v containing only vertices in S after the kth iteration]. Let u be the first vertex not in S in such a path. There is a path with length less than Lk(V) from a to u containing only vertices of S. This contradicts the choice ofv. Hence, (i) holds at the end of the (k + l)st iteration. Let u be a vertex not in S after k + I iterations. A shortest path from a to u containing only elements of S either contains v or it does not. Ifit does not contain v, then by the inductive hypothesis its length is Lk(U). If it does contain v, then it must be made up of a path from a to v of shortest possible length containing elements of S other than v, followed by the edge from v to u. In this case, its length would be Lk(V) + w(v, u). This shows that (ii) is true, because Lk+l(U) = min{Lk(u), Lk(V) + w(v, u)}. Theorem 1 has been proved. Dijkstra's algorithm finds the length of a shortest path between two vertices in a connected simple undirected weighted graph. We can now estimate the computational complexity of Dijkstra's algorithm (in terms of additions and comparisons). The algorithm uses no more than n - I iterations, because one vertex is added to the distinguished set at each iteration. We are done if we can estimate the number of operations used for each iteration. We can identify the vertex not in Sk with the smallest label using no more than n -1 comparisons. Then we use an addition and a comparison to update the label of each vertex not in Sk. It follows that no more than 2(n -I) operations are used at each iteration, because there are no more than n -1 labels to update at each iteration. Because we use no more than n -1 iterations, each using no more than 2(n -I) operations, we have Theorem 2. Dijkstra's algorithm uses O(n2) operations (additions and comparisons) to find the length of a shortest path between two vertices in a connected simple undirected weighted graph with n vertices. The Traveling Salesman Problem We now discuss an important problem involving weighted graphs. Consider the following prob­ lem: A traveling salesman wants to visit each of n cities exactly once and return to his starting point. For example, suppose that the salesman wants to visit Detroit, Toledo, Saginaw, Grand Rapids, and Kalamazoo (see Figure 5). In which order should he visit these cities to travel the minimum total distance? To solve this problem we can assume the salesman starts in Detroit (because this must be part of the circuit) and examine all possible ways for him to visit the other four cities and then return to Detroit (starting elsewhere will produce the same circuits). There are a total of24 such circuits, but because we travel the same distance when we travel a circuit in reverse order, we need only consider 12 different circuits to find the minimum total distance he must travel. We list these 12 different circuits and the total distance traveled for each circuit. As can be seen from the list, the minimum total distance of 458 miles is traveled using the circuit Detroit-Toledo-Kalamazoo-Grand Rapids-Sagina w-Detroit (or its reverse). 654 9 I Graphs Grand Rapids k��----���-�='---1--7 Detroit Toledo FIGURE 5 The Graph Showing the Distances between Five Cities. Route Total Distance (miles) Detroit-Toledo-Grand Rapids-Sagin aw-Kalamazoo-Detroit 610 Detroit-Toledo-Grand Rapids-Kalamazoo-Sagi naw-Detroit 516 Detroit-Toledo-Kalamazoo-Sagina w-Grand Rapids-Detroit 588 Detroit-Toledo-Kal amazoo-Grand Rapids-Sagi naw-Detroit 458 Detroit-Toledo-Sagi naw-Kalamazo o--Grand Rapids-Detroit 540 Detroit-Toledo-Sagina w-Grand Rapids-Kalamazoo-Detroit 504 Detroit-Sagina w-Toledo-Grand Rapids-Kalamazoo-Detroit 598 Detroit-Sagi naw-Toledo-Kalamazoo-Grand Rapids-Detroit 576 Detroit-Sagi naw-Kalamazoo- Toledo-Grand Rapids-Detroit 682 Detroit-Sagina w-Grand Rapids-Toledo-Kalamazoo-Detroit 646 Detroit-Grand Rapids-Sag inaw-Toledo-Kalamazoo-Detroit 670 Detroit-Grand Rapids-Toledo-Sagi naw-Kalamazoo-Detroit 728 9-66 We just described an instance of the traveling salesman problem. The traveling salesman problem asks for the circuit of minimum total weight in a weighted, complete, undirected graph that visits each vertex exactly once and returns to its starting point. This is equivalent to asking for a Hamilton circuit with minimum total weight in the complete graph, because each vertex is visited exactly once in the circuit. The most straightforward way to solve an instance of the traveling salesman problem is to examine all possible Hamilton circuits and select one of minimum total length. How many circuits do we have to examine to solve the problem if there are n vertices in the graph? Once a starting point is chosen, there are (n -I)! different Hamilton circuits to examine, because there are n -1 choices for the second vertex, n - 2 choices for the third vertex, and so on. Because a Hamilton circuit can be traveled in reverse order, we need only examine (n -1)!/2 circuits to find our answer. Note that (n -1)!/2 grows extremely rapidly. Trying to solve a traveling salesman problem in this way when there are only a few dozen vertices is impractical. For example, with 25 vertices, a total of 24!/2 (approximately 3.1 x 1023) different Hamilton circuits would have to be considered. If it took just one nanosecond (l0-9 second) to examine each Hamilton circuit, a total of approximately ten million years would be required to find a minimum-length Hamilton circuit in this graph by exhaustive search techniques. 9-67 Exercises 9.6 Shortest-Path Problems 655 Because the traveling salesman problem has both practical and theoretical importance, a great deal of effort has been devoted to devising efficient algorithms that solve it. However, no algorithm with polynomial worst-case time complexity is known for solving this problem. Furthermore, if a polynomial worst-case time complexity algorithm were discovered for the trav­ eling salesman problem, many other difficult problems would also be solvable using polynomial worst-case time complexity algorithms (such as determining whether a proposition in n variables is a tautology, discussed in Chapter 1). This follows from the theory of NP-complete ness. (For more information about this, consult [GaJ079].) A practical approach to the traveling salesman problem when there are many vertices to visit is to use an approximation algorithm. These are algorithms that do not necessarily produce the exact solution to the problem but instead are guaranteed to produce a solution that is close to an exact solution. That is, they may produce a Hamilton circuit with total weight W' such that W .::: W' .::: c W, where W is the total length of an exact solution and c is a constant. For example, there is an algorithm with polynomial worst-case time complexity that works if the weighted graph satisfies the triangle inequality such that c = 3/2. For general weighted graphs for every positive real number k no algorithm is known that will always produce a solution at most k times a best so­ lution. If such an algorithm existed, this would show that the class P would be the same as the class NP, perhaps the most famous open question about the complexity of algorithms (see Section 3.3). In practice, algorithms have been developed that can solve traveling salesman problems with as many as 1000 vertices within 2% of an exact solution using only a few minutes of computer time. For more information about the traveling salesman problem, including history, applications, and algorithms, see the chapter on this topic in Applications of Discrete Mathematics [MiR091] also available on the website for this book. 1. For each of these problems about a subway system, de­ scribe a weighted graph model that can be used to solve the problem. 4. a) What is the least amount of time required to travel between two stops? b) What is the minimum distance that can be traveled to reach a stop from another stop? c) What is the least fare required to travel between two stops iffares between stops are added to give the total fare? In Exercises 2-4 find the length of a shortest path between a and z in the given weighted graph. 2. b 2 a 3 c 5 5 c d z e 6 e 5 g z 5. Find a shortest path between a and z in each of the weighted graphs in Exercises 2-4. 6. Find the length of a shortest path between these pairs of vertices in the weighted graph in Exercise 3. a) a andd b) a and f c) c and f d) b andz 7. Find shortest paths in the weighted graph in Exercise 3 between the pairs of vertices in Exercise 6. 8. Find a shortest path (in mileage) between each of the following pairs of cities in the airline system shown in Figure 1. a) New York and Los Angeles b) Boston and San Francisco c) Miami and Denver d) Miami and Los Angeles 656 9 / Graphs 9. Find a combination of flights with the least total air time between the pairs of cities in Exercise 8, using the flight times shown in Figure I. 10. Find a least expensive combination of flights connecting the pairs of cities in Exercise 8, using the fares shown in Figure l. 11. Find a shortest route (in distance) between computer cen­ ters in each of these pairs of cities in the communications network shown in Figure 2. a) Boston and Los Angeles b) New York and San Francisco c) Dallas and San Francisco d) Denver and New York 12. Find a route with the shortest response time between the pairs of computer centers in Exercise II using the re­ sponse times given in Figure 2. 13. Find a least expensive route, in monthly lease charges, be­ tween the pairs of computer centers in Exercise II using the lease charges given in Figure 2. 14. Explain how to find a path with the least number of edges between two vertices in an undirected graph by considering it as a shortest path problem in a weighted graph. 15. Extend Dijkstra's algorithm for finding the length of a shortest path between two vertices in a weighted simple connected graph so that the length of a shortest path be­ tween the vertex a and every other vertex of the graph is found. 16. Extend Dijkstra's algorithm for finding the length of a shortest path between two vertices in a weighted simple connected graph so that a shortest path between these ver­ tices is constructed. 17. The weighted graphs in the figures here show some major roads in New Jersey. Part (a) shows the dis­ tances between cities on these roads; part (b) shows the tolls. Newark 20 Asbury Park Camden-...;::- __ (a) 9-68 Newark Asbury Park Camden --.;;;:-- __ (b) a) Find a shortest route in distance between Newark and Camden, and between Newark and Cape May, using these roads. b) Find a least expensive route in terms of total tolls using the roads in the graph between the pairs of cities in part (a) of this exercise. 18. Is a shortest path between two vertices in a weighted graph unique if the weights of edges are distinct? 19. What are some applications where it is necessary to find the length of a longest simple path between two vertices in a weighted graph? 20. What is the length of a longest simple path in the weighted graph in Figure 4 between a and z? Between c andz? � Floyd's algorithm, displayed as Algorithm 2, can be used to find the length of a shortest path between all pairs of vertices in a weighted connected simple graph. However, this algo­ rithm cannot be used to construct shortest paths. (We assign an infinite weight to any pair of vertices not connected by an edge in the graph.) 21. Use Floyd's algorithm to find the distance between all pairs of vertices in the weighted graph in Figure 4(a). *22. Prove that Floyd's algorithm determines the shortest dis­ tance between all pairs of vertices in a weighted simple graph. *23. Give a big-0 estimate of the number of operations (com­ parisons and additions) used by Floyd's algorithm to deter­ mine the shortest distance between every pair of vertices in a weighted simple graph with n vertices. *24. Show that Dijkstra's algorithm may not work if edges can have negative weights. 9-69 ALGORITHM 2 Floyd's Algorithm. procedure Floyd(G: weighted simple graph) {G has vertices VI, V2, ... , Vn and weights w( Vi, lJ) with W(Vi, lJ) = 00 if (Vi, lJ) is not an edge} for i := 1 to n for j := 1 to n d(Vi, lJ) := W(Vi, lJ) for i := 1 to n for j := 1 to n for k := 1 to n if d(lJ, Vi) + d(Vi, Vk) < d(lJ, Vk) then d(lJ, Vk) := d(lJ, Vi) + d(Vi, Vk) {d(Vi, lJ) is the length of a shortest path between Vi and lJ} 25. Solve the traveling salesman problem for this graph by finding the total weight of all Hamilton circuits and de­ termining a circuit with minimum total weight. a 3 b 2r:gJ6 d 7 c 26. Solve the traveling salesman problem for this graph by finding the total weight of all Hamilton circuits and de­ termining a circuit with minimum total weight. a 3 b e �-+-''--+-� c d 9.7 Planar Graphs Introduction 9.7 Planar Graphs 657 27. Find a route with the least total airfare that visits each of the cities in this graph, where the weight on an edge is the least price available for a flight between the two cities. $329 Detroit 189 San Francisc:o o�::3:�L�::�����:;::;:;�:::=::::::=�N�e�w tf: York Los Angeles Denver 28. Find a route with the least total airfare that visits each of the cities in this graph, where the weight on an edge is the least price available for a flight between the two cities. Seattle Phoenix New Orleans New York 29. Construct a weighted undirected graph such that the to­ tal weight of a circuit that visits every vertex at least once is minimized for a circuit that visits some vertices more than once. [Hint: There are examples with three vertices.] 30. Show that the problem of finding a circuit of minimum total weight that visits every vertex of a weighted graph at least once can be reduced to the problem of finding a circuit of minimum total weight that visits each vertex of a weighted graph exactly once. Do so by constructing a new weighted graph with the same vertices and edges as the original graph but whose weight of the edge connecting the vertices u and V is equal to the minimum total weight of a path from u to V in the original graph. unkS� Consider the problem of joining three houses to each of three separate utilities, as shown in Figure 1. Is it possible to join these houses and utilities so that none of the connections cross? This problem can be modeled using the complete bipartite graph K3,3. The original question can be rephrased as: Can K3,3 be drawn in the plane so that no two of its edges cross? In this section we will study the question of whether a graph can be drawn in the plane without edges crossing. In particular, we will answer the houses-and-utilities problem. There are always many ways to represent a graph. When is it possible to find at least one way to represent this graph in a plane without any edges crossing? 658 9 / Graphs DEFINITION 1 �.A o ---- - ;: 0 . (I ill - . -'" -" ..... � t , "\ ;, ... � .' • � � __ • ,�� " _. � .:.....:....:..,.. ... • � .... J \ FIGURE 1 Three Houses and Three Utilities. 9-70 A graph is called planar if it can be drawn in the plane without any edges crossing (where a crossing of edges is the intersection of the lines or arcs representing them at a point other than their common endpoint). Such a drawing is called a planar representation of the graph. A graph may be planar even if it is usually drawn with crossings, because it may be possible to draw it in a different way without crossings. EXAMPLE 1 Is K4 (shown in Figure 2 with two edges crossing) planar? Solution: K4 is planar because it can be drawn without crossings, as shown in Figure 3. EXAMPLE 2 Is Q3, shown in Figure 4, planar? FIGURE 2 The Graph K4. Solution: Q3 is planar, because it can be drawn without any edges crossing, as shown in Figure 5. � We can show that a graph is planar by displaying a planar representation. It is harder to show that a graph is nonplanar. We will give an example to show how this can be done in an ad hoc fashion. Later we will develop some general results that can be used to do this. FIGURE 3 K4 Drawn with No Crossings. FIGURE 4 The Graph Q3. FIGURE 5 A Planar Representation of Q3. 9-7/ FIGURE 6 The Graph K3,3. EXAMPLE 3 Is K3,3, shown in Figure 6, planar? VI Rl V4 (a) FIGURE 7 9.7 Planar Graphs 659 Vs VI Vs Rll Rl R22 V2 V4 vl (b) Showing that K3,3 Is Nonplan.ar. Solution: Any attempt to draw K3,3 in the plane with no edges crossing is doomed. We now show why. In any planar representation of K3,3, the vertices VI and V2 must be connected to both V4 and Vs. These four edges form a closed curve that splits the plane into two regions, RI and R2, as shown in Figure 7(a). The vertex V3 is in either RI or R2. When V3 is in R2, the inside of the closed curve, the edges between V3 and V4 and between V3 and Vs separate R2 into two subregions, R21 and R22, as shown in Figure 7(b). Next, note that there is no way to place the final vertex V6 without forcing a crossing, For if V6 is in R I, then the edge between V6 and V3 cannot be drawn without a crossing. If V6 is in R21, then the edge between V2 and V6 cannot be drawn without a crossing. If V6 is in R22, then the edge between VI and V6 cannot be drawn without a crossing. A similar argument can be used when V3 is in R I. The completion of this argument is left for the reader (see Exercise 10 at the end of this section). It follows that K3,3 is not planar. � Example 3 solves the utilities-and-houses problem that was described at the beginning of this section. The three houses and three utilities cannot be connected in the plane without a crossing. A similar argument can be used to show that Ks is nonplanar. (See Exercise 11 at the end of this section.) Planarity of graphs plays an important role in the design of electronic circuits. We can model a circuit with a graph by representing components of the circuit by vertices and connections between them by edges. We can print a circuit on a single board with no connections crossing if the graph representing the circuit is planar. When this graph is not planar, we must turn to more expensive options. For example, we can partition the vertices in the graph representing the circuit into planar subgraphs. We then construct the circuit using multiple layers. (See the preamble to Exercise 30 to learn about the thickness of a graph.) We can construct the circuit using insulated wires whenever connections cross. In this case, drawing the graph with the fewest possible crossings is important. (See the preamble to Exercise 26 to learn about the crossing number of a graph.) Euler's Formula A planar representation of a graph splits the plane into regions, including an unbounded region. For instance, the planar representation of the graph shown in Figure 8 splits the plane into six regions. These are labeled in the figure. Euler showed that all planar representations of a graph split the plane into the same number of regions. He accomplished this by finding a relationship among the number of regions, the number of vertices, and the number of edges of a planar graph. 660 9 / Graphs THEOREM 1 FIGURE 9 The Basis Case of the Proof of Euler's Formula. FIGURE 8 The Regions of the Planar Representation of a Graph. 9-72 EULER'S FORMULA Let G be a connected planar simple graph with e edges and v vertices. Let r be the number of regions in a planar representation of G. Then r = e -v + 2. Proof: First, we specifY a planar representation of G. We will prove the theorem by constructing a sequence of subgraphs G I, G2, ••• , Ge = G, successively adding an edge at each stage. This is done using the following inductive definition. Arbitrarily pick one edge of G to obtain G I. Obtain Gn from Gn-I by arbitrarily adding an edge that is incident with a vertex already in Gn-I, adding the other vertex incident with this edge if it is not already in Gn-I. This construction is possible because G is connected. G is obtained after e edges are added. Let r n, en, and v" represent the number of regions, edges, and vertices of the planar representation of Gn induced by the planar representation of G, respectively. The proof will now proceed by induction. The relationship rl = el -VI + 2 is true for G I, because el = 1, VI = 2, and rl = l. This is shown in Figure 9. Now assume that rn = en - Vn + 2. Let {an+I' bn+l} be the edge that is added to Gn to obtain Gn+l. There are two possibilities to consider. In the first case, both an+1 and bn+1 are already in Gn• These two vertices must be on the boundary of a common region R, or else it would be impossible to add the edge {an+l, bn+l} to Gn without two edges crossing (and Gn+1 is planar). The addition of this new edge splits R into two regions. Consequently, in this case, r n+ I = r n + I, en+ I = en + I, and Vn+l = Vn. Thus, each side of the formula relating the number of regions, edges, and vertices increases by exactly one, so this formula is still true. In other words, rn+1 = en+1 - Vn+1 + 2. This case is illustrated in Figure lO(a). In the second case, one of the two vertices of the new edge is not already in Gn. Suppose that an+1 is in Gn but that bn+1 is not. Adding this new edge does not produce any new regions, because bn+1 must be in a region that has an+1 on its boundary. Consequently, rn+1 = rn. Moreover, en+1 = en + 1 and Vn+1 = Vn + l. Each side of the formula relating the number an+l \ \ \ \ \ \ \ \ (a) R (b) FIGURE 10 Adding an Edge to Gn to Produce Gn+1. 9-73 9.7 Planar Graphs 661 of regions, edges, and vertices remains the same, so the formula is still true. In other words, rn+! = en+! - vn+! + 2. This case is illustrated in Figure lOeb). We have completed the induction argument. Hence, rn = en -Vn + 2 for all n. Because the original graph is the graph Ge, obtained after e edges have been added, the theorem is true. <l Euler's formula is illustrated in Example 4. EXAM PLE 4 Suppose that a connected planar simple graph has 20 vertices, each of degree 3. Into how many regions does a representation of this planar graph split the plane? Solution: This graph has 20 vertices, each of degree 3, so v = 20. Because the sum of the degrees of the vertices, 3v = 3·20 = 60, is equal to twice the number of edges, 2e, we have 2e = 60, . or e = 30. Consequently, from Euler's formula, the number of regions is r = e - v + 2 = 30 -20 + 2 = 12. Euler's formula can be used to establish some inequalities that must be satisfied by planar graphs. One such inequality is given in Corollary 1. COROLLAR Y l If G is a connected planar simple graph with e edges and v vertices, where v :::: 3, then e ::s 3v -6. COROL LARY 2 Before we prove Corollary 1 we will use it to prove the following useful result. If G is a connected planar simple graph, then G has a vertex of degree not exceeding five. Proof: If G has one or two vertices, the result is true. If G has at least three vertices, by Corollary 1 we know that e ::s 3v -6, so 2e ::s 6v -12. If the degree of every vertex were at least six, then because 2e = LVEV deg(v) (by the Handshaking Theorem), we would have 2e :::: 6v. But this contradicts the inequality 2e ::s 6v -12. It follows that there must be a vertex with degree no greater than five. <l The proof of Corollary 1 is based on the concept of the degree of a region, which is defined to be the number of edges on the boundary of this region. When an edge occurs twice on the boundary (so that it is traced out twice when the boundary is traced out), it contributes two to the degree. The degrees of the regions of the graph shown in Figure 11 are displayed in the figure. The proof of Corollary 1 can now be given. Proof: A connected planar simple graph drawn in the plane divides the plane into regions, say r of them. The degree of each region is at least three. (Because the graphs discussed here are simple graphs, no multiple edges that could produce regions of degree two, or loops that could produce regions of degree one, are permitted.) In particular, note that the degree of the unbounded region is at least three because there are at least three vertices in the graph. Note that the sum of the degrees of the regions is exactly twice the number of edges in the graph, because each edge occurs on the boundary of a region exactly twice (either in two 662 9 / Graphs c 7 b d a 6 e f FIGURE 11 The Degrees of Regions. 9-74 different regions, or twice in the same region). Because each region has degree greater than or equal to three, it follows that 2e = L deg(R) 2: 3r. all regions R Hence, (2j3)e 2: r. Using r = e - v + 2 (Euler's formula), we obtain e - v + 2 ::: ( 2j3)e. It follows that ej3 ::: v -2. This shows that e ::: 3v -6. This corollary can be used to demonstrate that K5 is nonplanar. <I EXAMPLE 5 Show that K5 is nonplanar using Corollary 1. COROL LARY 3 Solution: The graph K5 has five vertices and 10 edges. However, the inequality e ::: 3v -6 is not satisfied for this graph because e = 10 and 3 v -6 = 9. Therefore, K 5 is not planar. .... It was previously shown that K3.3 is not planar. Note, however, that this graph has six vertices and nine edges. This means that the inequality e = 9 ::: 12 = 3 . 6 - 6 is satisfied. Consequently, the fact that the inequality e ::: 3 v - 6 is satisfied does not imply that a graph is planar. However, the following corollary of Theorem 1 can be used to show that K3,3 is nonplanar. If a connected planar simple graph has e edges and v vertices with v 2: 3 and no circuits of length three, then e ::: 2v -4. The proof of Corollary 3 is similar to that of Corollary 1, except that in this case the fact that there are no circuits of length three implies that the degree of a region must be at least four. The details of this proof are left for the reader (see Exercise 15 at the end of this section). 9-75 9.7 Planar Graphs 663 c d e c d FIGURE 12 Homeomorph ic Graphs. EXAMPLE 6 Use Corollary 3 to show that K3•3 is nonplanar. e c d e Solution: Because K3•3 has no circuits oflength three (this is easy to see because it is bipartite), Corollary 3 can be used. K3,3 has six vertices and nine edges. Because e = 9 and 2v -4 = 8, Corollary 3 shows that K3,3 is nonplanar. <I Kurato wski's Theorem We have seen that K3,3 and Ks are not planar. Clearly, a graph is not planar ifit contains either of these two graphs as a subgraph. Surprisingly, all nonplanar graphs must contain a subgraph that can be obtained from K3,3 or K5 using certain permitted operations. Ifa graph is planar, so will be any graph obtained by removing an edge {u, v} and adding a new vertex w together with edges {u, w} and {w, v}. Such an operation is called an elementary subdivision. The graphs GI = (VI, EI) and G2 = (V2, E2) are called homeomorphic if they can be obtained from the same graph by a sequence of elementary subdivisions. EXAMPLE 7 Show that the graphs G I, G2, and G3 displayed in Figure 12 are all homeomorphic. Solution: These three graphs are homeomorphic because all three can be obtained from G I by elementary subdivisions. GI can be obtained from itself by an empty sequence of elementary subdivisions. To obtain G2 from G I we can use this sequence of elementary subdivisions: (i) remove the edge {a, c}, add the vertex f, and add the edges {a, f} and {j, c}; (ii) remove the edge {b, c}, add the vertex g, and add the edges {b, g} and {g, c}; and (iii) remove the edge {b, g}, add the vertex h, and add the edges {g, h} and {b, h }. We leave it to the reader to determine the sequence of elementary subdivisions needed to obtain G3 from G I. <I The Polish mathematician Kazimierz Kuratowski established Theorem 2 in 1930, which characterizes planar graphs using the concept of graph homeomorphism. KAZIMIERZ KURATOWSKI (1896-1980) Kazimierz Kuratowski, the son ofa famous Warsaw lawyer, attended secondary school in Warsaw. He studied in Glasgow, Scotland, from 1913 to 1914 but could not return there after the outbreak of World War I. In 1915 he entered Warsaw University, where he was active in the Polish patriotic student movement. He published his first paper in 1919 and received his Ph.D. in 1921. He was an active member of the group known as the Warsaw School of Mathematics, working in the areas of the foundations of set theory and topology. He was appointed associate professor at the Lwow Polytechnical University, where he stayed for seven years, collaborating with the important Polish mathematicians Banach and Ulam. In 1930, while at Lwow, Kuratowski completed his work characterizing planar graphs. In 1934 he returned to Warsaw University as a full professor. Until the start of World War II, he was active in research and teaching. During the war, because of the persecution of educated Poles, Kuratowski went into hiding under an assumed name and taught at the clandestine Warsaw University. After the war he helped revive Polish mathematics, serving as director of the Polish National Mathematics Institute. He wrote over 180 papers and three widely used textbooks. 664 9 / Graphs THEOREM 2 EXAMPLE S Elltra� Examples IIIiiiiiI EXAMPLE 9 d a b �---\--+--�c g G a b �---\--+--�c g H 9-76 a b �-�-�-�c FIGURE 13 The Undirected Graph G, a Subgraph H Homeomorphic to Ks, and Ks. A graph is nonplanar if and only if it contains a subgraph homeomorphic to K 3,3 or K s. It is clear that a graph containing a subgraph homeomorphic to K 3,3 or K s is nonplanar. However, the proof of the converse, namely that every nonplanar graph contains a subgraph homeomorphic to K3,3 or Ks, is complicated and will not be given here. Examples 8 and 9 illustrate how Kuratowski's Theorem is used. Determine whether the graph G shown in Figure 13 is planar. Solution: G has a subgraph H homeomorphic to K s. H is obtained by deleting h, j, and k and all edges incident with these vertices. H is homeomorphic to Ks because it can be obtained from Ks (with vertices a, b, c, g, and i) by a sequence of elementary subdivisions, adding the vertices d, e, and f. (The reader should construct such a sequence of elementary subdivisions.) Hence, G is nonplanar. .... Is the Petersen graph, shown in Figure 14( a), planar? (The Danish mathematician Julius Petersen studied this graph in 1891; it is often used to illustrate various theoretical properties of graphs.) Solution: The subgraph H of the Petersen graph obtained by deleting b and the three edges that have b as an endpoint, shown in Figure 14(b), is homeomorphic to K3,3, with vertex sets a f d j b a e e h c (a) (b) H (c) K3,3 FIGURE 14 (a) The Petersen Graph, (b) a Subgraph H Homeomorphic to K3,3, and (c) K3,3. 9-77 9.7 Planar Graphs 665 {f, d, j} and {e, i, h}, because it can be obtained by a sequence of elementary subdivisions, deleting {d, h} and adding {e, h} and {e, d}, deleting Ie, f} and adding {a, e} and {a, j}, and deleting Ii, j} and adding {g, i} and {g, j}. Hence, the Petersen graph is not planar. .... Exercises 1. Can five houses be connected to two utilities without con­ nections crossing? In Exercises 2-4 draw the given planar graph without any crossings. 2. 3. 4. In Exercises 5-9 determine whether the given graph is planar. If so, draw it so that no edges cross. 5. a 6. a b ~ d e f d e 7. a b 8. a b h c f c g d e d f e 9. a h g c e c f 10. Complete the argument in Example 3. 11. Show that Ks is nonplanar using an argument similar to that given in Example 3. 12. Suppose that a connected planar graph has eight vertices, each of degree three. Into how many regions is the plane divided by a planar representation of this graph? 13. Suppose that a connected planar graph has six vertices, each of degree four. Into how many regions is the plane divided by a planar representation of this graph? 14. Suppose that a connected planar graph has 30 edges. If a planar representation of this graph divides the plane into 20 regions, how many vertices does this graph have? 15. Prove Corollary 3. 16. Suppose that a connected bipartite planar simple graph has e edges and v vertices. Show that e ::::: 2v -4 if v ::: 3. *17. Suppose that a connected planar simple graph with e edges and v vertices contains no simple circuits of length 4 or less. Show that e ::::: (5/3)v -(10/3) if v ::: 4. 18. Suppose that a planar graph has k connected components, e edges, and v vertices. Also suppose that the plane is divided into r regions by a planar representation of the graph. Find a formula for r in terms of e, v, and k. 19. Which of these nonplanar graphs have the property that the removal of any vertex and all edges incident with that vertex produces a planar graph? a) Ks b) K6 c) K3,3 d) K3,4 In Exercises 20-22 determine whether the given graph is homeomorp hic to K3,3. 20. c 21. a b c d e f 666 9 / Graphs 22. a k c j d e g 9-78 � The crossing number of a simple graph is the minimum num­ ber of crossings that can occur when this graph is drawn in the plane where no three arcs representing edges are permitted to cross at the same point. 26. Show that K3,3 has 1 as its crossing number. **27. Find the crossing numbers of each of these nonplanar graphs. a) Ks d) K3,4 In Exercises 23-25 use Kuratowski's Theorem to determine whether the given graph is planar. *28. Find the crossing number of the Petersen graph. **29. Show that if m and n are even positive integers, the cross­ ing number of Km,n is less than or equal to mn(m -2) (n -2)/16. [Hint: Place m vertices along the x-axis so that they are equally spaced and symmetric about the ori­ gin and place n vertices along the y-axis so that they are equally spaced and symmetric about the origin. Now con­ nect each of the m vertices on the x-axis to each of the vertices on the y-axis and count the crossings.] 23. a b c e g 24. d The thickness of a simple graph G is the smallest number of planar subgraphs of G that have G as their union. 30. Show that K3,3 has 2 as its thickness. *31. Find the thickness of the graphs in Exercise 27. h�----�--�----� 32. Show that if G is a connected simple graph with v ver­ tices and e edges, then the thickness of G is at least r e/(3v -6)1. 25. b c f e *33. Use Exercise 32 to show that the thickness of Kn is at least L(n + 7)/6J whenever n is a positive integer. 34. Show that if G is a connected simple graph with v ver­ tices and e edges and no circuits of length three, then the thickness of G is at least r e/(2v -4)1. 35. Use Exercise 34 to show that the thickness of Km•n is at least rmn/(2m + 2n - 4)1 wheneverm andn are positive integers. *36. Draw Ks on the surface of a torus (a doughnut-shaped solid) so that no edges cross. *37. Draw K3,3 on the surface of a torus so that no edges cross. 9.8 Graph Coloring unkS� Introduction Problems related to the coloring of maps of regions, such as maps of parts of the world, have generated many results in graph theory. When a map* is colored, two regions with a common border are customarily assigned different colors. One way to ensure that two adjacent regions never have the same color is to use a different color for each region. However, this is inefficient, 'We will assume that all regions in a map are connected. This eliminates any problems presented by such geographical entities 9-79 9.8 Graph Coloring 667 DEFINITION 1 DEFINITION 2 FIGURE 1 Two Maps. B G A-c�------��--------� D E FIGURE 2 Dual Graphs of the Maps in Figure 1. and on maps with many regions it would be hard to distinguish similar colors. Instead, a small number of colors should be used whenever possible. Consider the problem of determining the least number of colors that can be used to color a map so that adjacent regions never have the same color. For instance, for the map shown on the left in Figure 1, four colors suffice, but three colors are not enough. (The reader should check this.) In the map on the right in Figure 1, three colors are sufficient (but two are not). Each map in the plane can be represented by a graph. To set up this correspondence, each region of the map is represented by a vertex. Edges connect two vertices if the regions represented by these vertices have a common border. Two regions that touch at only one point are not considered adjacent. The resulting graph is called the dual graph of the map. By the way in which dual graphs of maps are constructed, it is clear that any map in the plane has a planar dual graph. Figure 2 displays the dual graphs that correspond to the maps shown in Figure 1. The problem of coloring the regions of a map is equivalent to the problem of coloring the vertices of the dual graph so that no two adjacent vertices in this graph have the same color. We now define a graph coloring. A coloring of a simple graph is the assignment of a color to each vertex of the graph so that no two adjacent vertices are assigned the same color. A graph can be colored by assigning a different color to each of its vertices. However, for most graphs a coloring can be found that uses fewer colors than the number of vertices in the graph. What is the least number of colors necessary? The chromatic number of a graph is the least number of colors needed for a coloring of this graph. The chromatic number of a graph G is denoted by X (G). (Here X is the Greek letter chi.) 668 9 / Graphs THEOREM l Linl(S� EXAMPLE l EKlra� Examples � LinIlS� 9-80 Note that asking for the chromatic number of a planar graph is the same as asking for the minimum number of colors required to color a planar map so that no two adjacent regions are assigned the same color. This question has been studied for more than 100 years. The answer is provided by one of the most famous theorems in mathema tics. THE FOUR COLOR THEOREM The chromatic number of a planar graph is no greater than four. The Four Color Theorem was originally posed as a conjecture in the 1850s. It was finally proved by the American mathematicians Kenneth Appel and Wolfgang Haken in 1976. Prior to 1976, many incorrect proofs were published, often with hard-to-find errors. In addition, many futile attempts were made to construct counterexamp les by drawing maps that require more than four colors. (Proving the Five Color Theorem is not that difficult; see Exercise 36.) Perhaps the most notorious fallacious proof in all of mathematics is the incorrect proof of the Four Color Theorem published in 1879 by a London barrister and amateur mathematician, Alfred Kempe. Mathematicians accepted his proof as correct until 1890, when Percy Heawood found an error that made Kempe's argument incomplete. However, Kempe's line of reasoning turned out to be the basis of the successful proof given by Appel and Haken. Their proof relies on a careful case-by-case analysis carried out by computer. They showed that if the Four Color Theorem were false, there would have to be a counterexample of one of approximately 2000 different types, and they then showed that none of these types exists. They used over 1000 hours of computer time in their proof. This proof generated a large amount of controversy, because computers played such an important role in it. For example, could there be an error in a computer program that led to incorrect results? Was their argument really a proof if it depended on what could be unreliable computer output? Note that the Four Color Theorem applies only to planar graphs. Nonplanar graphs can have arbitrarily large chromatic numbers, as will be shown in Example 2. Two things are required to show that the chromatic number of a graph is k. First, we must show that the graph can be colored with k colors. This can be done by constructing such a coloring. Second, we must show that the graph cannot be colored using fewer than k colors. Examples 1-4 illustrate how chromatic numbers can be found. What are the chromatic numbers of the graphs G and H shown in Figure 3? Solution: The chromatic number of G is at least three, because the vertices a, b, and c must be assigned different colors. To see if G can be colored with three colors, assign red to a, blue to b, and green to c. Then, d can (and must) be colored red because it is adjacent to band c. Furthermore, e can (and must) be colored green because it is adjacent only to vertices colored red and blue, and f can (and must) be colored blue because it is adjacent only to vertices colored red and green. Finally, g can (and must) be colored red because it is adjacent only to vertices ALFRED BRAY KEMPE (1849-1922) Kempe was a barrister and a leading authority on ecclesiastical law. However, having studied mathematics at Cambridge University, he retained his interest in it, and later in life he devoted considerable time to mathematical research. Kempe made contributions to kinematics, the branch of mathematics dealing with motion, and to mathematical logic. However, Kempe is best remembered for his fallacious proof of the Four Color Theorem. 9-81 b e a g a c f G FIGURE 3 The Simple Graphs G and H. b Blue e Green cGreen fBlue G FIGURE 4 Colorings ofthe Graphs G and H. 9.8 Graph Coloring 669 g c H cGreen fBlue H colored blue and green. This produces a coloring of G using exactly three colors. Figure 4 displays such a coloring. The graph H is made up of the graph G with an edge connecting a and g. Any attempt to color H using three colors must follow the same reasoning as that used to color G, except at the last stage, when all vertices other than g have been colored. Then, because g is adjacent (in H) to vertices colored red, blue, and green, a fourth color, say brown, needs to be used. Hence, H has a chromatic number equal to 4. A coloring of H is shown in Figure 4. .... EXAM PLE 2 What is the chromatic number of Kn? unkS� Solution: A coloring of Kn can be constructed using n colors by assigning a different color to each vertex. Is there a coloring using fewer colors? The answer is no. No two vertices can be assigned the same color, because every two vertices of this graph are adjacent. Hence, the chromatic number of Kn = n. That is, X(Kn) = n. (Recall that Kn is not planar when n :::: 5, so HISTORICAL NOTE In 1852, an ex-student of Augustus De Morgan, Francis Guthrie, noticed that the counties in England could be colored using four colors so that no adjacent counties were assigned the same color. On this evidence, he conjectured that the Four Color Theorem was true. Francis told his brother Frederick, at that time a student of De Morgan, about this problem. Frederick in turn asked his teacher De Morgan about his brother's conjecture. De Morgan was extremely interested in this problem and publicized it throughout the mathematical community. In fact, the first written reference to the conjecture can be found in a letter from De Morgan to Sir William Rowan Hamilton. Although De Morgan thought Hamilton would be interested in this problem, Hamilton apparently was not interested in it, because it had nothing to do with quaternions. HISTORICAL NOTE Although a simpler proof of the Four Color Theorem was found by Robertson, Sanders, Seymour, and Thomas in 1996, reducing the computational part of the proof to examining 633 configurations, no proof that does not rely on extensive computation has yet been found. 670 9 / Graphs a Red b Blue Brown e *O---+--+-----:.c Green dYellow FIGURE 5 A Coloring of K5• a Red bRed eRed dBlue eBlue fBlue FIGURE 6 A Coloring of K3,4. 9-82 gBlue this result does not contradict the Four Color Theorem .) A coloring of K5 using five colors is shown in Figure 5. .... EXAMPLE 3 What is the chromatic number of the complete bipartite graph Km,n, where m and n are positive integers? Solution: The number of colors needed may seem to depend on m and n. However, as Theorem 4 in Section 9.2 tells us, only two colors are needed, because Km,n is a bipartite graph. Hence, X(Km,n) = 2. This means that we can color the set ofm vertices with one color and the set of n vertices with a second color. Because edges connect only a vertex from the set of m vertices and a vertex from the set of n vertices, no two adjacent vertices have the same color. A coloring of K3,4 with two colors is displayed in Figure 6. .... EXAMPLE 4 What is the chromatic number of the graph Cn, where n � 3? (Recall that Cn is the cycle with n vertices.) Solution: We will first consider some individual cases. To begin, let n = 6. Pick a vertex and color it red. Proceed clockwise in the planar depiction of C6 shown in Figure 7. It is necessary to assign a second color, say blue, to the next vertex reached. Continue in the clockwise direction; the third vertex can be colored red, the fourth vertex blue, and the fifth vertex red. Finally, the sixth vertex, which is adjacent to the first, can be colored blue. Hence, the chromatic number of C6 is 2. Figure 7 displays the coloring constructed here. Next, let n = 5 and consider C5• Pick a vertex and color it red. Proceeding clockwise, it is necessary to assign a second color, say blue, to the next vertex reached. Continuing in the clockwise direction, the third vertex can be colored red, and the fourth vertex can be colored blue. a b a f Blue e d d FIGURE 7 Colorings of C5 and C6. b 9-83 unkS� 9.8 Graph Coloring 671 The fifth vertex cannot be colored either red or blue, because it is adjacent to the fourth vertex and the first vertex. Consequently, a third color is required for this vertex. Note that we would have also needed three colors if we had colored vertices in the counterclockwise direction. Thus, the chromatic number of Cs is 3. A coloring of Cs using three colors is displayed in Figure 7. In general, two colors are needed to color Cn when n is even. To construct such a coloring, simply pick a vertex and color it red. Proceed around the graph in a clockwise direction (using a planar representation of the graph) coloring the second vertex blue, the third vertex red, and so on. The nth vertex can be colored blue, because the two vertices adjacent to it, namely the (n -1 )st and the first vertices, are both colored red. When n is odd and n > 1, the chromatic number of Cn is 3. To see this, pick an initial vertex. To use only two colors, it is necessary to alternate colors as the graph is traversed in a clockwise direction. However, the nth vertex reached is adjacent to two vertices of different colors, namely, the first and (n -1 )st. Hence, a third color must be used. We have shown that X (C n) = 2 if n is an even positive integer with n ::: 4 and X (C n) = 3 if n is an odd positive integer with n ::: 3. .... The best algorithms known for finding the chromatic number of a graph have exponential worst-case time complexity (in the number of vertices of the graph). Even the problem of finding an approximation to the chromatic number of a graph is difficult. It has been shown that if there were an algorithm with polynomial worst-case time complexity that could approximate the chromatic number of a graph up to a factor of 2 (that is, construct a bound that was no more than double the chromatic number of the graph), then an algorithm with polynomial worst-case time complexity for finding the chromatic number of the graph would also exist. Applications of Graph Colorings Graph coloring has a variety of applications to problems involving scheduling and assignments. (Note that because no efficient algorithm is known for graph coloring, this does not lead to efficient algorithms for scheduling and assignments.) Examples of such applications will be given here. The first application deals with the scheduling of final exams. EXAM PLE 5 Scheduling Final Exams How can the final exams at a university be scheduled so that no student has two exams at the same time? Solution: This scheduling problem can be solved using a graph model, with vertices representing courses and with an edge between two vertices ifthere is a common student in the courses they represent. Each time slot for a final exam is represented by a different color. A scheduling of the exams corresponds to a coloring of the associated graph. For instance, suppose there are seven finals to be scheduled. Suppose the courses are num­ bered 1 through 7. Suppose that the following pairs of courses have common students: 1 and 2, 1 and 3, 1 and 4, 1 and 7, 2 and 3, 2 and 4,2 and 5, 2 and 7, 3 and 4,3 and 6,3 and 7, 4 and 5, 4 and 6, 5 and 6, 5 and 7, and 6 and 7. In Figure 8 the graph associated with this set of classes is shown. A scheduling consists of a coloring of this graph. Because the chromatic number of this graph is 4 (the reader should verify this), four time slots are needed. A coloring of the graph using four colors and the associated schedule are shown in Figure 9. .... Now consider an application to the assignment of television channels. 672 9 / Graphs 9-84 IRed Time Period Courses I 1,6 II 2 III 3,5 6 3 Red 6 3 Green IV 4, 7 5 4 5 Green 4 Brown FIGURE 8 The Graph Representing the Scheduling of Final Exams. FIGURE 9 Using a Coloring to Schedule Final Exams. EXAMPLE 6 Frequency Assignments Television channels 2 through 13 are assigned to stations in North America so that no two stations within 150 miles can operate on the same channel. How can the assignment of channels be modeled by graph coloring? Solution: Construct a graph by assigning a vertex to each station. Two vertices are con­ nected by an edge if they are located within 150 miles of each other. An assignment of channels corresponds to a coloring of the graph, where each color represents a different channel. ... An application of graph coloring to compilers is considered in Example 7. EXAMPLE 7 Index Registers In efficient compilers the execution of loops is speeded up when frequently used variables are stored temporarily in index registers in the central processing unit, instead of in regular memory. For a given loop, how many index registers are needed? This problem can be addressed using a graph coloring model. To set up the model, let each vertex of a graph represent a variable in the loop. There is an edge between two vertices if the variables they represent must be stored in index registers at the same time during the execution of the loop. Thus, the chromatic number of the graph gives the number of index registers needed, because different registers must be assigned to variables when the vertices representing these variables are adjacent in the graph. ... Exercises In Exercises 1-4 construct the dual graph for the map shown. 2. Then find the number of colors needed to color the map so that no two adjacent regions have the same color. 1. 9-85 3. 4. In Exercises 5-11 find the chromatic number of the given graph. 5°171 c d 7. a b...._--- .. d c 9. a 8. b e __ --------�----� c d 10. a a c h�----+_�--�--------�c f e f d 9.8 Graph Coloring 673 11. e h n a b c k 12. For the graphs in Exercises 5-11, decide whether it is possible to decrease the chromatic number by removing a single vertex and all edges incident with it. 13. Which graphs have a chromatic number of 1 ? 14. What is the least number of colors needed to color a map of the United States? Do not consider adjacent states that meet only at a corner. Suppose that Michigan is one region. Consider the vertices representing Alaska and Hawaii as isolated vertices. 15. What is the chromatic number of Wn? 16. Show that a simple graph that has a circuit with an odd number of vertices in it cannot be colored using two colors. 17. Schedule the final exams for Math 115, Math 116, Math 185, Math 195, CS 101, CS 102, CS 273, and CS 473, using the fewest number of different time slots, if there are no students taking both Math 115 and CS 473, both Math 116 and CS 473, both Math 195 and CS 101, both Math 195 and CS 102, both Math 115 and Math 116, both Math 115 and Math 185, and both Math 185 and Math 195, but there are students in every other combina­ tion of courses. 18. How many different channels are needed for six stations located at the distances shown in the table, if two stations cannot use the same channel when they are within 150 miles of each other? 1 2 3 4 5 6 1 -85 175 200 50 100 2 85 -125 175 100 160 3 175 125 -100 200 250 4 200 175 100 -210 220 5 50 100 200 210 -100 6 100 160 250 220 100 - 19. The mathematics department has six committees each meeting once a month. How many different meeting times must be used to ensure that no member is scheduled to attend two meetings at the same time if the committees are C] = {Arlinghaus, Brand, Zaslavsk y}, C2 = {Brand, Lee, Rosen}, C3 = {Arlinghaus, Rosen, Zaslavsk y}, C4 = {Lee, Rosen, Zaslavsk y}, C5 = {Arlinghaus, Brand}, and C6 = {Brand, Rosen, Zaslavsk y}? 674 9/ Graphs 20. A zoo wants to set up natural habitats in which to exhibit its animals. Unfortunately, some animals will eat some of the others when given the opportunity. How can a graph model and a coloring be used to determine the number of different habitats needed and the placement ofthe animals in these habitats? An edge coloring of a graph is an assignment of colors to edges so that edges incident with a common vertex are as­ signed different colors. The edge chromatic number of a graph is the smallest number of colors that can be used in an edge coloring of the graph. 21. Find the edge chromatic number of each of the graphs in Exercises 5-11. �22. Find the edge chromatic numbers of a) Kn. b) Km,n' c) Cn. d) Wn. 23. Seven variables occur in a loop of a computer program. The variables and the steps during which they must be stored are t: steps 1 through 6; u: step 2; v: steps 2 through 4; w: steps 1,3, and 5; x: steps 1 and 6; y: steps 3 through 6; and z: steps 4 and 5. How many different index registers are needed to store these variables during execution? 24. What can be said about the chromatic number of a graph that has Kn as a subgraph? This algorithm can be used to color a simple graph: First, list the vertices VI, V2, V3, ••• , Vn in order of decreasing degree so that deg(vI) :::: deg(v2) :::: ... :::: deg(vn)' Assign color I to VI and to the next vertex in the list not adjacent to VI (if one exists), and successively to each vertex in the list not adjacent to a vertex already assigned color 1. Then assign color 2 to the first vertex in the list not already colored. Successively assign color 2 to vertices in the list that have not already been colored and are not adjacent to vertices assigned color 2. If uncolored vertices remain, assign color 3 to the first vertex in the list not yet colored, and use color 3 to successively color those vertices not already colored and not adjacent to vertices assigned color 3. Continue this process until all vertices are colored. 25. Construct a coloring of the graph shown using this algorithm. a b c d g h j 26. Use pseudocode to describe this coloring algorithm. 27. Show that the coloring produced in this algorithm may use more colors than are necessary to color a graph. A connected graph G is called chromatically k-critical ifthe chromatic number of G is k, but for every edge e of G, the 9-86 chromatic number of the graph obtained by deleting this edge from G is k -1. 28. Show that Cn is chromatically 3-critical whenever n is an odd positive integer, n :::: 3. 29. Show that Wn is chromatically 4-critical whenever n is an odd integer, n :::: 3. 30. Show that W4 is not chromatically 3-critical. 31. Show that if G is a chromatically k-critical graph, then the degree of every vertex of G is at least k -1. A k-tuple coloring of a graph G is an assignment of a set of k different colors to each of the vertices of G such that no two adjacent vertices are assigned a common color. We de­ note by Xk(G) the smallest positive integer n such that G has a k-tuple coloring using n colors. For example, X2(C4) = 4. To see this, note that using only four colors we can assign two colors to each vertex of C4, as illustrated, so that no two adjacent vertices are assigned the same color. Furthermore, no fewer than four colors suffice because the vertices VI and V2 each must be assigned two colors, and a common color cannot be assigned to both VI and V2. (For more information about k-tuple coloring, see [MiR091].) {red, blue} VI {green, yellow} V4 32. Find these values: a) X2(K3) d) X2(C5) *g) X3(C5) V2 {green, yellow} V3 {red, blue} b) X2(K4) e) X2(K3,4) h) X3(K4.5) *33. Let G and H be the graphs displayed in Figure 3. Find a) X2(G). b) X2(H). c) X3(G). d) X3(H). 34. What is Xk( G) if G is a bipartite graph and k is a positive integer? 35. Frequencies for mobile radio (or cellular) telephones are assigned by zones. Each zone is assigned a set of fre­ quencies to be used by vehicles in that zone. The same frequency cannot be used in zones where interference is a problem. Explain how a k-tuple coloring can be used to assign k frequencies to each mobile radio zone in a region. *36. Show that every planar graph G can be colored using six or fewer colors. [Hint: Use mathematical induction on the number of vertices of the graph. Apply Corollary 2 of Section 9.7 to find a vertex V with deg(v) :::: 5. Consider the subgraph of G obtained by deleting V and,all edges incident with it.] **37. Show that every planar graph G can be colored using five or fewer colors. [Hint: Use the hint provided for Exercise 36.] 9-87 The famous Art Gallery Problem asks how many guards are needed to see all parts of an art gallery, where the gallery is the interior and boundary of a polygon with n sides. To state this problem more precisely, we need some terminolo gy. A point x inside or on the boundary of a simple polygon P covers or sees a point y inside or on P if all points on the line segment x yare in the interior or on the boundary of P. We say that a set of points is a guarding set of a simple polygon P iffor every point y inside P or on the boundary of P there is a point x in this guarding set that sees y. Denote by G(P) the minimum number of points needed to guard the simple polygon P. The Art Gallery Problem asks for the function g( n), which is the maximum value ofG(P) over all simple polygons with n ver­ tices. That is, g( n) is the minimum positive integer for which it is guaranteed that a simple polygon with n vertices can be guarded with g(n) or fewer guards. 38. Show that g(3) = I and g( 4) = 1 by showing that all tri­ angles and quadrilaterals can be guarded using one point. '39. Show that g( 5) = 1. That is, show that all pentagons can be guarded using one point. [Hint: Show that there are ei­ ther 0, 1, or 2 vertices with an interior angle greater than 180 degrees and that in each case, one guard suffices.] '40. Show that g(6) = 2 by first using Exercises 38 and 39 as well as Lemma 1 in Section 4.2 to show that g(6) :::: 2 Key Terms and Results TERMS undirected edge: an edge associated to a set {u, v}, where u and v are vertices directed edge: an edge associated to an ordered pair (u, v), where u and v are vertices multiple edges: distinct edges connecting the same vertices multiple directed edges: distinct directed edges associated with the same ordered pair (u, v), where u and v are vertices loop: an edge connecting a vertex with itself undirected graph: a set of vertices and a set of undirected edges each of which is associated with a set of one or two ofthese vertices simple graph: an undirected graph with no multiple edges or loops multigraph: an undirected graph that may contain multiple edges but no loops pseudograph: an undirected graph that may contain multiple edges and loops directed graph: a set of vertices together with a set of directed edges each of which is associated with an ordered pair of vertices directed multigrap h: a graph with directed edges that may contain multiple directed edges simple directed graph: a directed graph without loops or mul­ tiple directed edges adjacent: two vertices are adjacent ifthere is an edge between them Key Terms and Results 675 and then find a simple hexagon for which two guards are needed. *41. Show that g(n) 2: Ln/3J. [Hint: Consider the polygon with 3k vertices that resembles a comb with k prongs, such as the polygon with 15 sides shown here.] 4! " , , , , , , , �---.. � � I \ 1\ I \ I \ , , , , , \ I \ I .---.. .---.. � � I \ 1\ , , , , , .---.. , , , e--------------------------------- ----------- -----e *42. Solve the Art Gallery Problem by proving the Art Gallery Theorem, which states that at most Ln/3J guards are needed to guard the interior and boundary of a simple polygon with n vertices. [Hint: Use Theorem 1 in Section 4.2 to triangulate the simple polygon into n -2 triangles. Then show that it is possible to color the vertices of the triangulated polygon using three colors so that no two ad­ jacent vertices have the same color. Use induction and Exercise 23 in Section 4.2. Finally, put guards at all ver­ tices that are colored red, where red is the color used least in the coloring of the vertices. Show that placing guards at these points is all that is needed.] incident: an edge is incident with a vertex if the vertex is an endpoint of that edge deg(v) (degree of the vertex v in an undirected graph): the number of edges incident with v with loops counted twice deg-(v) (the in-degree ofthe vertex v in a graph with di­ rected edges): the number of edges with v as their terminal vertex deg+ (v) (the out-degree of the vertex v in a graph with di­ rected edges): the number of edges with v as their initial vertex underlying undirected graph of a graph with directed edges: the undirected graph obtained by ignoring the di­ rections of the edges Kn (complete graph on n vertices): the undirected graph with n vertices where each pair of vertices is connected by an edge bipartite graph: a graph with vertex set that can be partitioned into subsets VI and V2 such that each edge connects a vertex in VI and a vertex in V2 Km,n (complete bipartite graph): the graph with vertex set partitioned into a subset of m elements and a subset of n elements such that two vertices are connected by an edge if and only if one is in the first subset and the other is in the second subset 676 9 / Graphs en (cycle of size n), n 2 3: the graph with n vertices VI. V2 •...• Vn and edges {VI. V2}, {V2. V3}, ...• {Vn-I. vn}, {vn• vd Wn (wheel of size n), n 2 3: the graph obtained from Cn by adding a vertex and edges from this vertex to the original vertices in C n Qn (n-cube), n 2 1: the graph that has the 2n bit strings of length n as its vertices and edges connecting every pair of bit strings that differ by exactly one bit isolated vertex: a vertex of degree zero pendant vertex: a vertex of degree one regular graph: a graph where all vertices have the same degree subgraph ofa graph G = (V, E): a graph (W. F), where W is a subset of V and F is a subset of E G) U G2 (union of G) and G2): the graph (VI U V2• EI U E2), where GI = (VI. EI) and G2 = (V2. E2) adjacency matrix: a matrix representing a graph using the adjacency of vertices incidence matrix: a matrix representing a graph using the in­ cidence of edges and vertices isomorphic simple graphs: the simple graphs GI = (VI. EI) and G2 = (V2. E2) are isomorphic if there is a one-to-one corresponden ce! from VI to V2 such that {f(VI). !(V2)} E E2 if and only if {VI. V2} E EI for all VI and V2 in VI invariant: a property that isomorphic graphs either both have or both do not have path from u to v in an undirected graph: a sequence of edges el. e2 •...• en, where ei is associated to {Xi. xi+d for i = 0.1 ....• n, wherexo = u andxn+1 = V path from u to v in a graph with directed edges: a sequence of edges el. e2 •...• en, where ei is associated to (Xi, Xi+l) for i = O. 1 •...• n, where Xo = u and Xn+l = V simple path: a path that does not contain an edge more than once circuit: a path oflength n � 1 that begins and ends at the same vertex connected graph: an undirected graph with the property that there is a path between every pair of vertices connected component of a graph G: a maximal connected sub graph of G strongly connected directed graph: a directed graph with the property that there is a directed path from every vertex to every vertex strongly connected component of a directed graph G: a maximal strongly connected subgraph of G Euler circuit: a circuit that contains every edge of a graph exactly once Review Questions 1. a) Define a simple graph, a multigraph, a pseudograph, a directed graph, and a directed multigraph. b) Use an example to show how each of the types of graph in part (a) can be used in modeling. For example, 9-88 Euler path : a path that contains every edge of a graph exactly once Hamilton path: a path in a simple graph that passes through each vertex exactly once Hamilton circuit: a circuit in a simple graph that passes through each vertex exactly once weighted graph: a graph with numbers assigned to its edges shortest-path problem: the problem of determining the path in a weighted graph such that the sum of the weights of the edges in this path is a minimum over all paths between specified vertices traveling salesman problem: the problem that asks for the circuit of shortest total length that visits every vertex ofthe graph exactly once planar graph: a graph that can be drawn in the plane with no crossings regions of a representation of a planar graph: the regions the plane is divided into by the planar representation of the graph elementary subdivision: the removal of an edge {u. v} of an undirected graph and the addition of a new vertex w together with edges {u. w} and {w, v} homeomor phic: two undirected graphs are homeomorphic if they can be obtained from the same graph by a sequence of elementary subdivisions graph coloring: an assignment of colors to the vertices of a graph so that no two adjacent vertices have the same color chromatic number: the minimum number of colors needed in a coloring of a graph RESULTS There is an Euler circuit in a connected multigraph if and only if every vertex has even degree. There is an Euler path in a connected multigraph if and only if at most two vertices have odd degree. Dijkstra's algorithm: a procedure for finding a shortest path between two vertices in a weighted graph (see Section 9.6). Euler's formula: r = e - v + 2 where r, e, and v are the num­ ber of regions of a planar representation, the number of edges, and the number of vertices, respectively, of a planar graph. Kuratowski's Theorem: A graph is nonplanar if and only if it contains a subgraph homeomorphic to K3.3 or Ks. (Proof beyond scope of this book.) The Four Color Theorem: Every planar graph can be colored using no more than four colors. (Proof far beyond the scope ofthis book!) explain how to model different aspects of a computer network or airline routes. 2. Give at least four examples of how graphs are used in modeling . 9-89 3. What is the relationship between the sum of the degrees of the vertices in an undirected graph and the number of edges in this graph? Explain why this relationship holds. 4. Why must there be an even number of vertices of odd degree in an undirected graph? 5. What is the relationship between the sum ofthe in-degrees and the sum of the out-degrees ofthe vertices in a directed graph? Explain why this relationship holds. 6. Describe the following families of graphs. a) Kn, the complete graph on n vertices b) K m.n, the complete bipartite graph on m and n vertices c) en, the cycle with n vertices d) Wn, the wheel of size n e) Qn, the n-cube 7. How many vertices and how many edges are there in each of the graphs in the families in Question 6? 8. a) What is a bipartite graph? b) Which ofthe graphs Kn, en, and Wn are bipartite? c) How can you determine whether an undirected graph is bipartite? 9. a) Describe three different methods that can be used to represent a graph. b) Draw a simple graph with at least five vertices and eight edges. Illustrate how it can be represented using the methods you described in part (a). 10. a) What does it mean for two simple graphs to be isomorphic? b) What is meant by an invariant with respect to isomor­ phism for simple graphs? Give at least five examples of such invariants. c) Give an example of two graphs that have the same numbers of vertices, edges, and degrees of vertices, but that are not isomorphic. d) Is a set of invariants known that can be used to ef­ ficiently determine whether two simple graphs are isomorphic? 11. a) What does it mean for a graph to be connected? b) What are the connected components of a graph? 12. a) Explain how an adjacency matrix can be used to rep­ resent a graph. b) How can adjacency matrices be used to determine whether a function from the vertex set of a graph G to the vertex set of a graph H is an isomorphism? Supplementar y Exercises 1. How many edges does a 50-regular graph with 100 ver­ tices have? 2. How many nonisomorphic subgraphs does K3 have? Supplementary Exercises 677 c) How can the adjacency matrix of a graph be used to determine the number of paths of length r, where r is a positive integer, between two vertices of a graph? 13. a) Define an Euler circuit and an Euler path in an undi­ rected graph. b) Describe the famous Konigsberg bridge problem and explain how to rephrase it in terms of an Euler circuit. c) How can it be determined whether an undirected graph has an Euler path? d) How can it be determined whether an undirected graph has an Euler circuit? 14. a) Define a Hamilton circuit in a simple graph. b) Give some properties of a simple graph that imply that it does not have a Hamilton circuit. 15. Give examples of at least two problems that can be solved by finding the shortest path in a weighted graph. 16. a) Describe Dijkstra's algorithm for finding the shortest path in a weighted graph between two vertices. b) Draw a weighted graph with at least 10 vertices and 20 edges. Use Dijkstra's algorithm to find the short­ est path between two vertices of your choice in the graph. 17. a) What does it mean for a graph to be planar? b) Give an example ofa nonplanar graph. 18. a) What is Euler's formula for planar graphs? b) How can Euler's formula for planar graphs be used to show that a simple graph is nonplanar? 19. State Kuratowski's Theorem on the planarity of graphs and explain how it characterizes which graphs are planar. 20. a) Define the chromatic number of a graph. b) What is the chromatic number of the graph Kn when n is a positive integer? c) What is the chromatic number of the graph en when n is a positive integer greater than 2? d) What is the chromatic number ofthe graph Km•n when m and n are positive integers? 21. State the Four Color Theorem. Are there graphs that can­ not be colored with four colors? 22. Explain how graph coloring can be used in modeling. Use at least two different examples. In Exercises 3-5 determine whether the given pair of graphs is isomorphic. 678 9 / Graphs v4 v3 UI u7 u3 9-90 The complete m-partite graph Kn1,nz, ... ,nm has vertices par­ titioned into m subsets ofn], n2"." nm elements each, and vertices are adjacent if and only ifthey are in different subsets in the partition. 6. Draw these graphs. a) K],2,3 b) K2,2,2 c) K1,2,2,3 *7. How many vertices and how many edges does the com­ plete m-partite graph Knl ,nz, ... ,nm have? *8. a) Prove or disprove that there are always two vertices with the same degree in a finite simple graph having at least two vertices. b) Do the same as in part (a) for finite multigraphs. Let G = (V, E) be a simple graph. The subgraph induced by a subset W ofthe vertex set V is the graph (W, F), where the edge set F contains an edge in E if and only if both endpoints of this edge are in W. 9. Consider the graph shown in Figure 3 of Section 9.4. Find the sub graphs induced by a) {a, b, c}, b) {a, e, g}. c) {b, c,f, g, h}. 10. Let n be a positive integer. Show that a subgraph induced by a nonempty subset of the vertex set of Kn is a complete graph. A clique in a simple undirected graph is a complete subgraph that is not contained in any larger complete subgraph. In Ex­ ercises 11-13 find all cliques in the graph shown. 11. abc e 12. a h d 13. b k d 9-91 A dominating set of vertices in a simple graph is a set of vertices such that every other vertex is adjacent to at least one vertex of this set. A dominating set with the least num­ ber of vertices is called a minimum dominating set. In Ex­ ercises 14-16 find a minimum dominating set for the given graph. 14. 15. a b a e c d c d b f 16. a b c d e __ --�f*---�g*---�h. j k A simple graph can be used to determine the minimum number of queens on a chessboard that control the entire chessboard. An n x n chessboard has n Z squares in an n x n cotifiguration. A queen in a given position controls all squares in the same row, the same column, and on the two diagonals containing this square, as illustrated. The appropriate simple graph has nZ vertices, one for each square, and two vertices are adjacent if a queen in the square represented by one of the vertices controls the square represented by the other vertex. The Squares Controlled by a Queen 17. Construct the simple graph representing the n x n chess­ board with edges representing the control of squares by queens for a) n = 3. b) n = 4. 18. Explain how the concept of a minimum dominating set applies to the problem of determining the minimum num­ ber of queens controlling an n x n chessboard. Supplementary Exercises 679 **19. Findtheminimum numberofqueens controlling ann x n chessboard for a) n = 3. b) n = 4. c) n = 5. 20. Suppose that G I and HI are isomorphic and that Gz and Hz are isomorphic. Prove or disprove that G I U Gz and HI U Hz are isomorphic. 21. Show that each of these properties is an invariant that iso­ morphic simple graphs either both have or both do not have. a) connectedness b) the existence ofa Hamilton circuit c) the existence of an Euler circuit d) having crossing number C e) having n isolated vertices t) being bipartite 22. How can the adjacency matrix of G be found from the adjacency matrix of G, where G is a simple graph? 23. How many nonisomorphic connected bipartite simple graphs are there with four vertices? *24. How many nonisomorphic simple connected graphs with five vertices are there a) with no vertex of degree more than two? b) with chromatic number equal to four? c) that are nonplanar? A directed graph is self-converse if it is isomorphic to its converse. 25. Determine whether the following graphs are self­ converse. a) a b d b) a b c d 26. Show that if the directed graph G is self-converse and H is a directed graph isomorphic to G, then H is also self-converse. An orientation of an undirected simple graph is an assignment of directions to its edges such that the resulting directed graph is strongly connected. When an orientation of an undirected graph exists, this graph is called orientable. In Exercises 27- 29 determine whether the given simple graph is orientable. 680 9 / Graphs 27. a e-------��------� b c e d 28. �� bed e b 29. a e---------. e d f h g 30. Because traffic is growing heavy in the central part of a city, traffic engineers are planning to change all the streets, which are currently two-way, into one-way streets. Ex­ plain how to model this problem. *31. Show that a graph is not orientable ifit has a cut edge. A tournament "is a simple directed graph such that if u and v are distinct vertices in the graph, exactly one of (u, v) and (v, u) is an edge of the graph. 32. How many different tournaments are there with n vertices? 33. What is the sum ofthe in-degree and out-degree of a ver­ tex in a tournament? *34. Show that every tournament has a Hamilton path. 35. Given two chickens in a flock, one of them is dominant. This defines the pecking order of the flock. How can a tournament be used to model pecking order? 36. Suppose that G is a connected multigraph with 2k ver­ tices of odd degree. Show that there exist k subgraphs that have G as their union, where each of these subgraphs has an Euler path and where no two of these subgraphs have an edge in common. [Hint: Add k edges to the graph con­ necting pairs of vertices of odd degree and use an Euler circuit in this larger graph.] *37. Let G be a simple graph with n vertices. The band width of G, denoted by B(G), is the minimum, over all permutations, ai, a2, ... ,an of the vertices of G, of max{li -jllai and aj are adjacent}. That is, the band 1)-1)2 width is the minimum over all listings of the vertices of the maximum difference in the indices assigned to adja­ cent vertices. Find the band widths of these graphs. a) Ks b) K1,3 c) K2,3 d) K3,3 e) Q3 t) CS C*38. The distance between two distinct vertices VI and V2 of a connected simple graph is the length (number of edges) of the shortest path between VI and V2. The radius of a graph is the minimum overall vertices v of the maximum distance from v to another vertex. The diameter of a graph is the maximum distance between two distinct vertices. Find the radius and diameter of a) K6. b) K4,S' c) Q3. d) C6. *39. a) Show that if the diameter of the simple graph G is at least four, then the diameter of its complement G is no more than two. b) Show that if the diameter of the simple graph G is at least three, then the diameter of its complement G is no more than three. *40. Suppose that a multigraph has 2m vertices of odd degree. Show that any circuit that contains every edge of the graph must contain at least m edges more than once. 41. Find the second shortest path between the vertices a and z in Figure 3 of Section 9.6. 42. Devise an algorithm for finding the second shortest path between two vertices in a simple connected weighted graph. 43. Find the shortest path between the vertices a and z that passes through the vertex e in the weighted graph in Fig­ ure 4 in Section 9.6. 44. Devise an algorithm for finding the shortest path between two vertices in a simple connected weighted graph that passes through a specified third vertex. *45. Show that if G is a simple graph with at least 11 vertices, then either G or G, the complement of G, is nonplanar. C A set of vertices in a graph is called independent if no two vertices in the set are adjacent. The independence number of a graph is the maximum number of vertices in an independent set of vertices for the graph. *46. What is the independence number of a) Kn? b) Cn? c) Qn? d) Km,n? 47. Show that the number of vertices in a simple graph is less than or equal to the product of the independence number and the chromatic number of the graph. 48. Show that the chromatic number of a graph is less than or equal to v -i + 1, where v is the number of vertices in the graph and i is the independence number of this graph. 49. Suppose that to generate a random simple graph with n vertices we first choose a real number p with 0 � p � 1. For each of the C(n, 2) pairs of distinct vertices we gener­ ate a random number x between 0 and 1. If 0 � x � p, we connect these two vertices with an edge; otherwise these vertices are not connected. 9-93 a) What is the probability that a graph with m edges where 0 ::: m ::: C(n, 2) is generated? b) What is the expected number of edges in a randomly generated graph with n vertices if each edge is in­ cluded with probability p? c) Show that if p = 1/2 then every simple graph with n vertices is equally likely to be generated. A property retained whenever additional edges are added to a simple graph (without adding vertices) is called monotone increasing, and a property that is retained whenever edges are removed from a simple graph (without removing vertices) is �alled monotone decreasing. 50. For each of these properties, determine whether it is monotone increasing and determine whether it is mono­ tone decreasing. Computer Projects Write programs with these input and output. 1. Given the vertex pairs associated to the edges of an undi­ rected graph, find the degree of each vertex. 2. Given the ordered pairs of vertices associated to the edges of a directed graph, determine the in-degree and out­ degree of each vertex. 3. Given the list of edges of a simple graph, determine whether the graph is bipartite. 4. Given the vertex pairs associated to the edges of a graph, construct an adjacency matrix for the graph. (Produce a version that works when loops, multiple edges, or directed edges are present.) 5. Given an adjacency matrix of a graph, list the edges of this graph and give the number of times each edge appears. 6. Given the vertex pairs associated to the edges of an undi­ rected graph and the number of times each edge appears, construct an incidence matrix for the graph. 7. Given an incidence matrix of an undirected graph, list its edges and give the number of times each edge appears. 8. Given a positive integer n, generate an undirected graph by producing an adjacency matrix for the graph so that all simple graphs are equally likely to be generated. 9. Given a positive integer n, generate a directed graph by producing an adjacency matrix for the graph so that all directed graphs are equally likely to be generated. to. Given the lists of edges of two simple graphs with no more than six vertices, determine whether the graphs are isomorphic. ll. Given an adjacency matrix of a graph and a positive inte­ ger n, find the number of paths of length n between two Computer Projects 681 a) The graph G is connected. b) The graph G is not connected. c) The graph G has an Euler circuit. d) The graph G has a Hamilton circuit. e) The graph G is planar. 1) The graph G has chromatic number four. g) The graph G has radius three. b) The graph G has diameter three. 51. Show that the graph property P is monotone increasing if and only if the graph property Q is monotone decreasing where Q is the property of not having property P. **52. Suppose that P is a monotone increasing property of sim­ ple graphs. Show that the probability a random graph with n vertices has property P is a monotonic nondecreasing function of p, the probability an edge is chosen to be in the graph. vertices. (Produce a version that works for directed and undirected graphs.) *12. Given the list of edges of a simple graph, determine whether it is connected and find the number of connected components if it is not connected. 13. Given the vertex pairs associated to the edges of a multi­ graph, determine whether it has an Euler circuit and, if not, whether it has an Euler path. Construct an Euler path or circuit if it exists. *14. Given the ordered pairs of vertices associated to the edges of a directed multi graph, construct an Euler path or Euler circuit, if such a path or circuit exists. * *15. Given the list of edges of a simple graph, produce a Hamil­ ton circuit, or determine that the graph does not have such a circuit. **16. Given the list of edges of a simple graph, produce a Hamil­ ton path, or determine that the graph does not have such a path. 17. Given the list of edges and weights of these edges of a weighted connected simple graph and two vertices in this graph, find the length of a shortest path between them using Dijkstra's algorithm. Also, find a shortest path. 18. Given the list of edges of an undirected graph, find a color­ ing ofthis graph using the algorithm given in the exercise set of Section 9.8. 19. Given a list of students and the courses that they are en­ rolled in, construct a schedule of final exams. 20. Given the distances between pairs of television stations, assign frequencies to these stations. 682 9 / Graphs 9-94 Computations and Explorations Use a computational program or programs you have written to do these exercises. 1. Display all the simple graphs with four vertices. 2. Display a full set of nonisomorphic simple graphs with six vertices. 3. Display a full set of nonisomorphic directed graphs with four vertices. 4. Generate at random 10 different simple graphs each with 20 vertices so that each such graph is equally likely to be generated. 5. Construct a Gray code where the code words are bit strings of length six. 6. Construct knight's tours on chessboards of various sizes. 7. Determine whether each of the graphs you generated in Exercise 4 of this set is planar. If you can, determine the thickness of each of the graphs that are not planar. 8. Determine whether each of the graphs you generated in Exercise 4 of this set is connected. If a graph is not con­ nected, determine the number of connected components ofthe graph. Writing Projects 9. Generate at random simple graphs with 10 vertices. Stop when you have constructed one with an Euler circuit. Dis­ play an Euler circuit in this graph. 10. Generate at random simple graphs with 10 vertices. Stop when you have constructed one with a Hamilton circuit. Display a Hamilton circuit in this graph. 11. Find the chromatic number of each of the graphs you generated in Exercise 4 of this set. **12. Find the shortest path a traveling salesperson can take to visit each of the capitals of the 50 states in the United States, traveling by air between cities in a straight line. *13. Estimate the probability that a randomly generated sim­ ple graph with n vertices is connected for each posi­ tive integer n not exceeding ten by generating a set of random simple graphs and determining whether each is connected. * *14. Work on the problem of determining whether the cross­ ing number of K (7, 7) is 77, 79, or 81. It is known that it equals one of these three values. Respond to these questions with essays using outside sources. 1. Describe the origins and development of graph theory prior to the year 1900. 2. Discuss the applications of graph theory to the study of ecosystems. 3. Discuss the applications of graph theory to sociology and psycholo gy. 4. Discuss what can be learned by investigating the proper­ ties of the Web graph. 5. Describe algorithms for drawing a graph on paper or on a display given the vertices and edges ofthe graph. What considerations arise in drawing a graph so that it has the best appearance for understanding its properties? 6. What are some of the capabilities that a software tool for inputting, displaying, and manipulating graphs should have? Which ofthese capabilities do available tools have? 7. Describe some ofthe algorithms available for determining whether two graphs are isomorphic and the computational complexity ofthese algorithms. What is the most efficient such algorithm currently known? 8; Describe how Euler paths can be used to help determine DNA sequences. 9. Define de Bruijn sequences and discuss how they arise in applications. Explain how de Bruijn sequences can be constructed using Euler circuits. to. Describe the Chinese postman problem and explain how to solve this problem. 11. Describe some of the different conditions that imply that a graph has a Hamilton circuit. 12. Describe some of the strategies and algorithms used to solve the traveling salesman problem. 13. Describe several different algorithms for determining whether a graph is planar. What is the computational com­ plexity of each of these algorithms? 14. In modeling, very large scale integration (VLSI) graphs are sometimes embedded in a book, with the vertices on the spine and the edges on pages. Define the book number of a graph and find the book number of various graphs including Kn for n = 3, 4,5, and 6. 15. Discuss the history of the Four Color Theorem. 16. Describe the role computers played in the proof of the Four Color Theorem. How can we be sure that a proof that relies on a computer is correct? 17. Describe and compare several different algorithms for coloring a graph, in terms of whether they produce a col­ oring with the least number of colors possible and in terms of their complexity. 18. Explain how graph multicolorings can be used in a variety of different models. 19. Explain how the theory of random graphs can be used in nonconstructive existence proofs of graphs with certain properties. CHAPTER 10.1 Introduction to Trees 10.2 Applications of Trees 10.3 Tree Traversal 10.4 Spanning Trees 10.5 Minimum Spanning Trees Trees � connected graph that contains no simple circuits is called a tree. Trees were used as long � ago as 1857, when the English mathema tician Arthur Cayley used them to count certain types of chemical compounds. Since that time, trees have been employed to solve problems in a wide variety of disciplines, as the examples in this chapter will show. Trees are particularly useful in computer science, where they are employed in a wide range of algorithms. For instance, trees are used to construct efficient algorithms for locating items in a list. They can be used in algorithms, such as Huffman coding, that construct efficient codes saving costs in data transmission and storage. Trees can be used to study games such as checkers and chess and can help determine winning strategies for playing these games. Trees can be used to model procedures carried out using a sequence of decisions. Constructing these models can help determine the computational complexity of algorithms based on a sequence of decisions, such as sorting algorithms. Procedures for building trees containing every vertex of a graph, including depth-first search and breadth-first search, can be used to systematically explore the vertices of a graph. Exploring the vertices of a graph via depth-first search, also known as backtracking, allows for the systematic search for solutions to a wide variety of problems, such as determining how eight queens can be placed on a chessboard so that no queen can attack another. We can assign weights to the edges of a tree to model many problems. For example, using weighted trees we can develop algorithms to construct networks containing the least expensive set of telephone lines linking different network nodes. 10.1 Introduction to Trees lilillW,) � In Chapter 9 we showed how graphs can be used to model and solve many problems. In this chapter we will focus on a particular type of graph called a tree, so named because such graphs resemble trees. For example,family trees are graphs that represent genealogical charts. Family trees use vertices to represent the members of a family and edges to represent parent­ child relationships. The family tree of the male members of the Bernoulli family of Swiss mathematicians is shown in Figure 1. The undirected graph representing a family tree (restricted to people of just one gender and with no inbreeding) is an example of a tree. DEFINITION 1 A tree is a connected undirected graph with no simple circuits. 10-/ Because a tree cannot have a simple circuit, a tree cannot contain multiple edges or loops. Therefore any tree must be a simple graph. EXAMPLE 1 Which of the graphs shown in Figure 2 are trees? Solution: G I and G2 are trees, because both are connected graphs with no simple circuits. G3 is not a tree because e, b, a, d, e is a simple circuit in this graph. Finally, G4 is not a tree because it is not connected. <1 683 684 10 / Trees THEOREM 1 Nikolaus (1623-1708) _________ I ________ Jacob I Nikolaus Johann I (1654-1705) (1662-1716) (1667-1748) /I� Nikolaus I Nikolaus II Daniel Johann II (1687-1759) (1695-1726) (1700-1782) (1710-1790) FIGURE 1 The Bernoulli Family of Mathematici ans. / \ Johann III Jacob II (1746-1807) (1759-1789) 10-2 Any connected graph that contains no simple circuits is a tree. What about graphs containing no simple circuits that are not necessarily connected? These graphs are called forests and have the property that each of their connected components is a tree. Figure 3 displays a forest. Trees are often defined as undirected graphs with the property that there is a unique simple path between every pair of vertices. Theorem I shows that this alternative definition is equivalent to our definition. An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. Proof: First assume that T is a tree. Then T is a connected graph with no simple circuits. Let x and y be two vertices of T. Because T is connected, by Theorem I of Section 9.4 there is a simple path between x and y. Moreover, this path must be unique, for if there were a second such path, the path formed by combining the first path from x to y followed by the path from y to x obtained by reversing the order of the second path from x to y would form a circuit. This a b a b a b a c d c d e f e f e f e FIGURE 2 Examples of Trees and Graphs That Are Not Trees. b f 10-3 DEFINITION 2 This is one graph with three connected components. FIGURE 3 Example ofa Forest. 10.1 Introduction to Trees 685 implies, using Exercise 49 of Section 9.4, that there is a simple circuit in T. Hence, there is a unique simple path between any two vertices of a tree. Now assume that there is a unique simple path between any two vertices of a graph T. Then T is connected, because there is a path between any two of its vertices. Furthermore, T can have no simple circuits. To see that this is true, suppose T had a simple circuit that contained the vertices x and y. Then there would be two simple paths between x and y, because the simple circuit is made up of a simple path from x to y and a second simple path from y to x. Hence, a graph with a unique simple path between any two vertices is a tree. <l In many applications of trees, a particular vertex of a tree is designated as the root. Once we specify a root, we can assign a direction to each edge as follows. Because there is a unique path from the root to each vertex of the graph (by Theorem l), we direct each edge away from the root. Thus, a tree together with its root produces a directed graph called a rooted tree. A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root. Rooted trees can also be defined recursively. Refer to Section 4.3 to see how this can be done. We can change an unrooted tree into a rooted tree by choosing any vertex as the root. Note that different choices of the root produce different rooted trees. For instance, Figure 4 displays the rooted trees formed by designating a to be the root and c to be the root, respective ly, in the tree T. We usually draw a rooted tree with its root at the top of the graph. The arrows indicating the T With root a With root c f g a c d c d f g e c f g FIGURE 4 A Tree and Rooted Trees Formed by Designating Two Roots. 686 IO / Trees EXAMPLE 2 Extra� ExampleS � DEFINITION 3 linkS� T a b f c h d e k FIGURE 5 A Rooted Tree T. m 10-4 g h k m FIGURE 6 The Subtree Rooted at g. directions of the edges in a rooted tree can be omitted, because the choice of root determines the directions of the edges. The terminology for trees has botanical and genealogical origins. Suppose that T is a rooted tree. If v is a vertex in T other than the root, the parent of v is the unique vertex u such that there is a directed edge from u to v (the reader should show that such a vertex is unique). When u is the parent of v, v is called a child of u. Vertices with the same parent are called siblings. The ancestors of a vertex other than the root are the vertices in the path from the root to this vertex, excluding the vertex itself and including the root (that is, its parent, its parent's parent, and so on, until the root is reached). The descenda nts of a vertex v are those vertices that have v as an ancestor. A vertex of a tree is called a leaf if it has no children. Vertices that have children are called internal vertices. The root is an internal vertex unless it is the only vertex in the graph, in which case it is a leaf. If a is a vertex in a tree, the subtree with a as its root is the subgraph of the tree consisting of a and its descendants and all edges incident to these descendants. In the rooted tree T (with root a) shown in Figure 5, find the parent of c, the children of g, the siblings of h, all ancestors of e, all descendants of b, all internal vertices, and all leaves. What is the subtree rooted at g? Solution: The parent of c is b. The children of g are h, i, and j. The siblings of hare i and j. The ancestors of e are c, b, and a. The descendants of bare c, d, and e. The internal vertices are a, b, c, g, h, and j. The leaves are d, e, f, i, k, I, and m. The subtree rooted at g is shown in Figure 6. <l1li Rooted trees with the property that all of their internal vertices have the same number of children are used in many different applications. Later in this chapter we will use such trees to study problems involving searching, sorting, and coding. A rooted tree is called an m-ary tree if every internal vertex has no more than m children. The tree is called afull m-ary tree if every internal vertex has exactly m children. An m-ary tree with m = 2 is called a binary tree. 10-5 10.1 Introduction to Trees 687 FIGURE 7 Four Rooted Trees. EXAM PLE 3 Are the rooted trees in Figure 7 full m-ary trees for some positive integer m? Solution: Tl is a full binary tree because each of its internal vertices has two children. T2 is a full 3-ary tree because each of its internal vertices has three children. In T3 each internal vertex has five children, so T3 is a fu1l5-ary tree. T4 is not a full m-ary tree for any m because some of its internal vertices have two children and others have three children. ... An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered. Ordered rooted trees are drawn so that the children of each internal vertex are shown in order from left to right. Note that a representation of a rooted tree in the conventional way determines an ordering for its edges. We will use such orderings of edges in drawings without explicitly mentioning that we are considering a rooted tree to be ordered. In an ordered binary tree (usually called just a binary tree), if an internal vertex has two children, the first child is called the left child and the second child is called the right child. The tree rooted at the left child of a vertex is called the left subtree of this vertex, and the tree rooted at the right child of a vertex is called the right subtree of the vertex. The reader should note that for some applications every vertex of a binary tree, other than the root, is designated as a right or a left child of its parent. This is done even when some vertices have only one child. We will make such designations whenever it is necessary, but not otherwise. Ordered rooted trees can be defined recursively. Binary trees, a type of ordered rooted trees, were defined this way in Section 4.3. EXAMPLE 4 What are the left and right children of d in the binary tree T shown in Figure 8(a) (where the order is that implied by the drawing)? What are the left and right subtrees of c? Solution: The left child of d is f and the right child is g. We show the left and right subtrees of c in Figures 8(b) and 8( c), respective ly. ... a i h I), I j rn f g j k rn (a) (b) (c) FIGURE 8 A Binary Tree T and Left and Right Subtrees ofthe Vertex c. 688 10 / Trees H I H-C-H I H-C-H I H-C- H I H-C-H I H Butane H H H I I I H-C-- C--C-H A I A H-C-H I H Isobutane FIGURE 9 The Two Isomers of Butane. /0-6 Just as in the case of graphs, there is no standard terminology used to describe trees, rooted trees, ordered rooted trees, and binary trees. This nonstandard terminology occurs because trees are used extensively throughout computer science, which is a relatively young field. The reader should carefully check meanings given to terms dealing with trees whenever they occur. Trees as Models Trees are used as models in such diverse areas as computer science, chemistry, geology, botany, and psychology. We will describe a variety of such models based on trees. EXAMPLE 5 Saturated Hydrocarbons and Trees Graphs can be used to represent molecules, where atoms are represented by vertices and bonds between them by edges. The English mathematician Arthur Cayley discovered trees in 1857 when he was trying to enumerate the isomers of compounds of the form CnH2n+2, which are called saturated hydrocarbons. linl(S� In graph models of saturated hydrocarbons, each carbon atom is represented by a vertex of degree 4, and each hydrogen atom is represented by a vertex of degree 1. There are 3n + 2 vertices in a graph representing a compound of the form CnH2n+2. The number of edges in such a graph is half the sum of the degrees of the vertices. Hence, there are (4n + 2n + 2)/2 = 3n + 1 edges in this graph. Because the graph is connected and the number of edges is one less than the number of vertices, it must be a tree (see Exercise 15 at the end of this section). The nonisomorphic trees with n vertices of degree 4 and 2n + 2 of degree 1 represent the different isomers of Cn H2n+2. For instance, when n = 4, there are exactly two nonisomorphic trees of this type (the reader should verify this). Hence, there are exactly two different isomers of C4HIO. Their structures are displayed in Figure 9. These two isomers are called butane and isobutane. .... ARTHUR CAYLEY (1821-1895) Arthur Cayley, the son ofa merchant, displayed his mathematical talents at an early age with amazing skill in numerical calculations. Cayley entered Trinity College, Cambridge, when he was 17. While in college he developed a passion for reading novels. Cayley excelled at Cambridge and was elected to a 3-year appointment as Fellow of Trinity and assistant tutor. During this time Cayley began his study of n-dimensional geometry and made a variety of contributions to geometry and to analysis. He also developed an interest in mountaineering, which he enjoyed during vacations in Switzerland. Because no position as a mathematician was available to him, Cayley left Cambridge, entering the legal profession and gaining admittance to the bar in 1849. Although Cayley limited his legal work to be able to continue his mathematics research, he developed a reputation as a legal specialist. During his legal career he was able to write more than 300 mathematical papers. In 1863 Cambridge University established a new post in mathematics and offered it to Cayley. He took this job, even though it paid less money than he made as a lawyer. 10-7 VP R&D VP Marketing VP Services 10.1 Introduction to Trees 689 VP Finance Director Research Director Software Development Director Hardware Development AVP Sales AVP Marketing Chief Field Operations Director Material Management Director Accounting Director MIS FIGURE 10 An Organizational Tree for a Compu�er Comp��r, EXAM PLE 6 Representing Organizations The strucmre of a large organization can be modeled using a rooted tree. Each vertex in this tree represents a position in the organization. An edge from one vertex to another indicates that the person represented by the initial vertex is the (direct) boss of the person represented by the terminal vertex. The graph shown in Figure 10 displays such a tree. In the organization represented by this tree, the Director of Hardware Development works directly for the Vice President of R&D. .... EXAMPLE 7 Computer File Systems Files in computer memory can be organized into directories. A directory can contain both files and subdirectori es. The root directory contains the entire file system. Thus, a file system may be represented by a rooted tree, where the root represents the root directory, internal vertices represent subdirector ies, and leaves represent ordinary files or empty directories. One such file system is shown in Figure 11. In this system, the file khr is in the directory rje. (Note that links to files where the same file may have more than one pathname can lead to circuits in computer file systems.) .... EXAM PLE 8 Tree-Connected Parallel Processors In Example 16 of Section 9.2 we described several in­ terconnection networks for parallel processing. A tree-connected network is another important The root is the root directory / / Internal vertices are directories __________ I � files usr bin tmp /I� /I� 1 bin rje spool Is mail who junk 11\ 1 /\ ed nroff vi khr opr uucp I I printer file FIGURE 11 A Computer File System, 690 10 / Trees FIGURE 12 A Tree-Connected Network of Seven Processors. THEOREM 2 unkS� THEOREM 3 10-8 way to interconnect processors. The graph representing such a network is a complete binary tree. Such a network interconnects n = 2k - 1 processors, where k is a positive integer. A processor represented by the vertex v that is not a root or a leaf has three two-way connections-one to the processor represented by the parent of v and two to the processors represented by the two children of v. The processor represented by the root has two two-way connections to the proces­ sors represented by its two children. A processor represented by a leaf v has a single two-way connection to the parent of v. We display a tree-connected network with seven processors in Figure 12. We will illustrate how a tree-connected network can be used for parallel computation. In particular, we will show how the processors in Figure 12 can be used to add eight numbers, using three steps. In the first step, we add Xl and X2 using P4, X3 and X4 using Ps, Xs and X6 using P6, and X7 and Xg using P7• In the second step, we add Xl + X2 and X3 + X4 using P2 and Xs + X6 and X7 + Xg using P3• Finally, in the third step, we add Xl + X2 + X3 + X4 and Xs + X6 + X7 + Xg using Pl. The three steps used to add eight numbers compares favorably to the seven steps required to add eight numbers serially, where the steps are the addition of one number to the sum of the previous numbers in the list. .... Properties of Trees We will often need results relating the numbers of edges and vertices of various types in trees. A tree with n vertices has n -1 edges. Proof: We will use mathematical induction to prove this theorem. Note that for all the trees here we can choose a root and consider the tree rooted. BASIS STEP: When n = 1, a tree with n = 1 vertex has no edges. It follows that the theorem is true for n = 1. INDUCTIVE STEP: The induction hypothesis states that every tree with k vertices has k - 1 edges, where k is a positive integer. Suppose that a tree T has k + 1 vertices and that v is a leaf ofT (which must exist because the tree is finite), and let w be the parent ofv. Removing from T the vertex v and the edge connecting w to v produces a tree T' with k vertices, because the resulting graph is still connected and has no simple circuits. By the induction hypothesis, T' has k - 1 edges. It follows that T has k edges because it has one more edge than T', the edge connecting v and w. This completes the induction step. <l The number of vertices in a full m-ary tree with a specified number of internal vertices is determined, as Theorem 3 shows. As in Theorem 2, we will use n to denote the number of vertices in a tree. A full m-ary tree with i internal vertices contains n = mi + 1 vertices. Proof: Every vertex, except the root, is the child of an internal vertex. Because each of the i internal vertices has m children, there are mi vertices in the tree other than the root. Therefore, the tree contains n = mi + 1 vertices. <l 10-9 THEOREM 4 EXAMPLE 9 a h FIGURE 13 A Rooted Tree. 10.1 Introduction to Trees 691 Suppose that T is a full m -ary tree. Let i be the number of internal vertices and I the number of leaves in this tree. Once one of n, i, and I is known, the other two quantities are determined. Theorem 4 explains how to find the other two quantities from the one that is known. A full m-ary tree with (i) n vertices has i = (n -1)/m internal vertices and 1 = [(m -l)n + 1]/m leaves, (ii) i internal vertices has n = mi + 1 vertices and I = (m -l)i + 1 leaves, (iii) lleaveshasn = (ml-1)/(m -l)verticesandi = (/-1)/(m -l)internalvertices. Proof: Let n represent the number of vertices, i the number of internal vertices, and I the number ofleaves. The three parts of the theorem can all be proved using the equality given in Theorem 3, that is, n = mi + 1, together with the equality n = I + i, which is true because each vertex is either a leaf or an internal vertex. We will prove part (i) here. The proofs of parts (ii) and (iii) are left as exercises for the reader. Solving for i in n = mi + 1 gives i = (n -1)/m. Then inserting this expression for i into the equation n = I + i shows that 1= n - i = n - (n -1)/m = [(m -l)n + l]/m. <l Example 9 illustrates how Theorem 4 can be used. Suppose that someone starts a chain letter. Each person who receives the letter is asked to send it on to four other people. Some people do this, but others do not send any letters. How many people have seen the letter, including the first person, if no one receives more than one letter and if the chain letter ends after there have been 100 people who read it but did not send it out? How many people sent out the letter? Solution: The chain letter can be represented using a 4-ary tree. The internal vertices correspond to people who sent out the letter, and the leaves correspond to people who did not send it out. Because 100 people did not send out the letter, the number ofleaves in this rooted tree is I = 100. Hence, part (iii) of Theorem 4 shows that the number of people who have seen the letter is n = (4·100 -1)/(4 -1) = 133. Also, the number of internal vertices is 133 -100 = 33, so 33 people sent out the letter. ..... It is often desirable to use rooted trees that are "balanced" so that the subtrees at each vertex contain paths of approximately the same length. Some definitions will make this concept clear. The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex. The level of the root is defined to be zero. The height of a rooted tree is the maximum of the levels of vertices. In other words, the height of a rooted tree is the length of the longest path from the root to any vertex. EXAMPLE 10 Find the level of each vertex in the rooted tree shown in Figure 13. What is the height of this tree? Solution: The root a is at level O. Vertices b, j, and k are at level 1. Vertices c, e, f, and I are at level 2. Vertices d, g, i, m, and n are at level 3. Finally, vertex h is at level 4. Because the largest level of any vertex is 4, this tree has height 4. ..... A rooted m -ary tree of height h is balanced if all leaves are at levels h or h -1. 692 10 / Trees 10-10 FIGURE 14 Some Rooted Trees. EXAMPLE 11 Which of the rooted trees shown in Figure 14 are balanced? THEOREM S Solution: Tl is balanced, because all its leaves are at levels 3 and 4. However, T2 is not balanced, because it has leaves at levels 2, 3, and 4. Finally, T3 is balanced, because all its leaves are at level 3. .... The results in Theorem 5 relate the height and the number ofleaves in m-ary trees. There are at most mh leaves in an m-ary tree of height h. Proof: The proof uses mathematical induction on the height. First, consider m-ary trees of height 1. These trees consist of a root with no more than m children, each of which is a leaf. Hence, there are no more than m 1 = m leaves in an m-ary tree of height 1. This is the basis step of the inductive argument. Now assume that the result is true for all m-ary trees of height less than h; this is the inductive hypothesis. Let T be an m -ary tree of height h. The leaves of T are the leaves of the subtrees of T obtained by deleting the edges from the root to each of the vertices at levell, as shown in Figure 15. Each of these subtrees has height less than or equal to h -1. So by the inductive hypothesis, each of these rooted trees has at most mh-1 leaves. Because there are at most m such subtrees, each with a maximum of mh-1 leaves, there are at most m . mh-1 = mh leaves in the rooted tree. This finishes the inductive argument. <l FIGURE 15 The Inductive Step of the Proof. 10-// COROLLAR Y l Exercises 10.1 Introduction to Trees 693 If an m-ary tree of height h has I leaves, then h 2: rlogm 11. If the m-ary tree is full and balanced, then h = rIogm 11. (We are using the ceiling function here. Recall that r xl is the smallest integer greater than or equal to x .) Proof: We know that I � mh from Theorem 5. Taking logarithms to the base m shows that logm 1 � h. Because h is an integer, we have h 2: rIogm Il Now suppose that the tree is balanced. Then each leaf is at level h or h -1, and because the height is h, there is at least one leaf at level h. It follows that there must be more than mh-1 leaves (see Exercise 30 at the end of this section). Because 1 � mh, we have mh-1 < 1 � mh. Taking logarithms to the base m in this inequality gives h -1 < logm 1 � h. Hence, h = rIogm 11. <I 1. Which of these graphs are trees? 3. Answer these questions about the rooted tree illustrated. d) 1&1 '»K 2. Which of these graphs are trees? d) .)� t) a d I m n u a) Which vertex is the root? b) Which vertices are internal? c) Which vertices are leaves? d) Which vertices are children of j? e) Which vertex is the parent of h? t) Which vertices are siblings of o? g) Which vertices are ancestors of m? b) Which vertices are descendants of b? 4. Answer the same questions as listed in Exercise 3 for the rooted tree illustrated. a q r s 694 10 / Trees 5. Is the rooted tree in Exercise 3 a full m -ary tree for some positive integer m? 6. Is the rooted tree in Exercise 4 a full m-ary tree for some positive integer m? 7. What is the level of each vertex of the rooted tree in Exercise 3? 8. What is the level of each vertex of the rooted tree in Exercise 4? 9. Draw the subtree of the tree in Exercise 3 that is rooted at a) a. b) c. c) e. 10. Draw the subtree ofthe tree in Exercise 4 that is rooted at a) a. b) c. c) e. 11. a) How many nonisomorphic unrooted trees are there with three vertices? b) How many nonisomorphic rooted trees are there with three vertices (using isomorphism for directed graphs)? *12. a) How many nonisomorphic unrooted trees are there with four vertices? b) How many nonisomorphic rooted trees are there with four vertices (using isomorphism for directed graphs)? *13. a) How many nonisomorphic unrooted trees are there with five vertices? b) How many nonisomorphic rooted trees are there with five vertices (using isomorphism for directed graphs)? *14. Show that a simple graph is a tree if and only ifit is con­ nected, but the deletion of any of its edges produces a graph that is not connected. * 15. Let G be a simple graph with n vertices. Show that G is a tree if and only if G is connected and has n -1 edges. [Hint: For the "if" part, use Exercise 14 and Theorem 2.] 16. Which complete bipartite graphs Km,n, where m and n are positive integers, are trees? 17. How many edges does a tree with 10,000 vertices have? 18. How many vertices does a fu1l5-ary tree with 100 internal vertices have? 19. How many edges does a full binary tree with 1000 internal vertices have? 20. How many leaves does a full 3-ary tree with 100 vertices have? 21. Suppose 1000 people enter a chess tournament. Use a rooted tree model of the tournament to determine how many games must be played to determine a champion, if a player is eliminated after one loss and games are played until only one entrant has not lost. (Assume there are no ties.) 22. A chain letter starts when a person sends a letter to five others. Each person who receives the letter either sends it to five other people who have never received it or does not send it to anyone. Suppose that 10,000 people send out the letter before the chain ends and that no one receives more than one letter. How many people receive the letter, and how many do not send it out? 10-12 23. A chain letter starts with a person sending a letter out to 10 others. Each person is asked to send the letter out to 10 others, and each letter contains a list ofthe previous six people in the chain. Unless there are fewer than six names in the list, each person sends one dollar to the first person in this list, removes the name of this person from the list, moves up each of the other five names one position, and inserts his or her name at the end ofthis list. If no person breaks the chain and no one receives more than one letter, how much money will a person in the chain ultimately receive? *24. Either draw a full m-ary tree with 76 leaves and height 3, where m is a positive integer, or show that no such tree exists. *25. Either draw a full m-ary tree with 84 leaves and height 3, where m is a positive integer, or show that no such tree exists. *26. A full m-ary tree T has 81 leaves and height 4. a) Give the upper and lower bounds for m. b) What is m if T is also balanced? A complete m-ary tree is a full m-ary tree, where every leaf is at the same level. 27. Construct a complete binary tree of height 4 and a com­ plete 3-ary tree of height 3. 28. How many vertices and how many leaves does a complete m-ary tree of height h have? 29. Prove a) part (ii) of Theorem 4. b) part (iii) of Theorem 4. II:? 30. Show that a full m-ary balanced tree of height h has more than mh-1 leaves. 31. How many edges are there in a forest of t trees containing a total of n vertices? 32. Explain how a tree can be used to represent the table of contents of a book organized into chapters, where each chapter is organized into sections, and each section is organized into subsections. 33. How many different isomers do these saturated hydro­ carbons have? 34. What does each of these represent in an organizational tree? a) the parent of a vertex b) a child of a vertex c) a sibling of a vertex d) the ancestors of a vertex e) the descendants of a vertex t) the level of a vertex g) the height of the tree 35. Answer the same questions as those given in Exercise 34 for a rooted tree representing a computer file system. 36. a) Draw the complete binary tree with 15 vertices that represents a tree-connected network of 15 processors. 10-13 b) Show how 16 numbers can be added using the 15 processors in part (a) using four steps. 37. Let n be a power of2. Show that n numbers can be added in log n steps using a tree-connected network of n -1 processors. *38. A labeled tree is a tree where each vertex is assigned a label. Two labeled trees are considered isomorphic when there is an isomorphism between them that preserves the labels of vertices. How many nonisomorphic trees are there with three vertices labeled with different integers from the set {I, 2, 3)? How many nonisomorphic trees are there with four vertices labeled with different integers from the set {I, 2, 3, 4)? The eccentricity of a vertex in an unrooted tree is the length ofthe longest simple path beginning at this vertex. A vertex is called a center if no vertex in the tree has smaller eccentricity than this vertex. In Exercises 39-41 find every vertex that is a center in the given tree. 39. a j k 40. a d f �, c j k 10.2 Applications of Trees Introduction 41. :);> f g lO.2 Applications of Trees 695 42. Show that a center should be chosen as the root to produce a rooted tree of minimal height from an unrooted tree. *43. Show that a tree has either one center or two centers that are adjacent. 44. Show that every tree can be colored using two colors. The rooted Fibonacci trees Tn are defined recursively in the following way. Tl and Tz are both the rooted tree consisting ofa single vertex, and for n = 3,4, ... , the rooted tree Tn is constructed from a root with Tn-l as its left subtree and Tn-z as its right subtree. 45. Draw the first seven rooted Fibonacci trees. *46. How many vertices, leaves, and internal vertices does the rooted Fibonacci tree Tn have, where n is a positive integer? What is its height? 47. What is wrong with the following "proof" using mathe­ matical induction of the statement that every tree with n vertices has a path oflength n -1. Basis step: Every tree with one vertex clearly has a path of length O. Inductive step: Assume that a tree with n vertices has a path of length n -1, which has u as its terminal vertex. Add a vertex v and the edge from u to v. The resulting tree has n + 1 vertices and has a path oflength n. This completes the induction step. �* 48. Show that the average depth of a leaf in a binary tree with n vertices is Q(log n). We will discuss three problems that can be studied using trees. The first problem is: How should items in a list be stored so that an item can be easily located? The second problem is: What series of decisions should be made to find an object with a certain property in a collection of objects of a certain type? The third problem is: How should a set of characters be efficiently coded by bit strings? unkS� Binary Search Trees Searching for items in a list is one of the most important tasks that arises in computer science. Our primary goal is to implement a searching algorithm that finds items efficiently when the 696 10 / Trees 10-14 items are totally ordered. This can be accomplished through the use of a binary search tree, which is a binary tree in which each child of a vertex is designated as a right or left child, no vertex has more than one right child or left child, and each vertex is labeled with a key, which is one of the items. Furthermore, vertices are assigned keys so that the key of a vertex is both larger than the keys of all vertices in its left subtree and smaller than the keys of all vertices in its right subtree. This recursive procedure is used to form the binary search tree for a list of items. Start with a tree containing just one vertex, namely, the root. The first item in the list is assigned as the key of the root. To add a new item, first compare it with the keys of vertices already in the tree, starting at the root and moving to the left if the item is less than the key of the respective vertex if this vertex has a left child, or moving to the right if the item is greater than the key of the respective vertex if this vertex has a right child. When the item is less than the respective vertex and this vertex has no left child, then a new vertex with this item as its key is inserted as a new left child. Similarly, when the item is greater than the respective vertex and this vertex has no right child, then a new vertex with this item as its key is inserted as a new right child. We illustrate this procedure with Example 1. EXAM PLE 1 Form a binary search tree for the words mathemat ics, physics, geography, zoology, meteorology, geology, psychology, and chemistry (using alphabetical order). Solution: Figure 1 displays the steps used to construct this binary search tree. The word mathe­ matics is the key of the root. Because physics comes after mathematics (in alphabetical order), add a right child of the root with key physics. Because geography comes before mathemat­ ics, add a left child of the root with key geography. Next, add a right child of the vertex with key physics, and assign it the key zoology, because zoology comes after mathemat ics and after physics. Similarly, add a left child of the vertex with key physics and assign this new vertex the key meteorology. Add a right child of the vertex with key geography and assign this new vertex the key geology. Add a left child ofthe vertex with key zoology and assign it the key psychology. mathematics • mathematics /"'. physics geog;;phy !\ meteorology zoology meteorology > mathematics meteorology < physics mathematics � physics physics> mathematics mathematics geograPhr/\ '-physics geology zoology meteorology geology < mathematics geology> geography FIGURE 1 Constructing a Binary Search Tree. mathematics /"'. geography physics geography < mathematics mathematics zoology psychology psychology > mathematics psychology > physics psychology < zoology mathematics /"'. physics geog;;phy '\ zoology zoology> mathematics zoology> physics mathematics zoology chemistry/ meteorology psychology chemistry < mathematics chemistry < geography /0-/5 10.2 Applications of Trees 697 Add a left child of the vertex with key geography and assign it the key chemistry. (The reader should work through all the comparisons needed at each step.) � Once we have a binary search tree, we need a way to locate items in the binary search tree, as well as a way to add new items. Algorithm 1, an insertion algorithm, actually does both of these tasks, even though it may appear that it is only designed to add vertices to a binary search tree. That is, Algorithm 1 is a procedure that locates an item x in a binary search tree if it is present, and adds a new vertex with x as its key if x is not present. In the pseudocode, v is the vertex currently under examination and label( v) represents the key of this vertex. The algorithm begins by examining the root. If the x equals the key of v, then the algorithm has found the location of x and terminate s; if x is less than the key of v, we move to the left child of v and repeat the procedure; and if x is greater than the key of v, we move to the right child of v and repeat the procedure. If at any step we attempt to move to a child that is not present, we know that x is not present in the tree, and we add a new vertex as this child with x as its key. ALGOR ITHM 1 Locating and Adding Items to a Binary Search Tree. procedure insertion(T: binary search tree, x: item) v := root ofT {a vertex not present in T has the value null} while v =1= null and label( v) =1= x begin if x < label( v) then ifleft child of v =1= null then v := left child of v else add new vertex as a left child of v and set v := null else if right child of v =1= null then v := right child of v else add new vertex as a right child of v to T and set v := null end if root of T = null then add a vertex v to the tree and label it with x else if v is null or label( v) =1= x then label new vertex with x and let v be this new vertex {v = location of x } Example 2 illustrates the use of Algorithm 1 to insert a new item into a binary search tree. EXAM PLE 2 Use Algorithm 1 to insert the word oceanography into the binary search tree in Example 1. Solution: Algorithm 1 begins with v, the vertex under examination, equal to the root of T, so label(v) = mathematics. Because v =I null and label(v) = mathematics < oceanography, we next examine the right child of the root. This right child exists, so we set v, the vertex under examination, to be this right child. At this step we have v =I null and label( v) = physics > oceanography, so we examine the left child of v. This left child exists, so we set v, the vertex under examination, to this left child. At this step, we also have v =I null and label( v) = metereology < oceanography, so we try to examine the right child of v. However, this right child does not exist, so we add a new vertex as the right child of v (which at this point is the vertex with the key metereology) and we set v := null. We now exit the while loop because v = null. Because the root of T is not null and v = null, we use the else if statement at the end of the algorithm to label our new vertex with the key oceanography. � 698 10 / Trees unkS� EXAMPLE 3 Extra� Examples "'" Unlabeled vertices circled FIGURE 2 Adding Unlabeled Vertices to Make a Binary Search Tree Full. /0-/6 We will now determine the computational complexity of this procedure. Suppose we have a binary search tree T for a list of n items. We can form a full binary tree U from T by adding unlabeled vertices whenever necessary so that every vertex with a key has two children. This is illustrated in Figure 2. Once we have done this, we can easily locate or add a new item as a key without adding a vertex. The most comparisons needed to add a new item is the length of the longest path in U from the root to a leaf. The internal vertices of U are the vertices ofT. It follows that U has n internal vertices. We can now use part (ii) of Theorem 4 in Section 10.1 to conclude that U has n + I leaves. Using Corollary 1 of Section 10.1, we see that the height of U is greater than or equal to h = flog(n + 1)1. Consequently, it is necessary to perform at least flog(n + 1)1 comparisons to add some item. Note that if U is balanced, its height is flog(n + 1)1 (by Corollary 1 of Section 10.1). Thus, if a binary search tree is balanced, locating or adding an item requires no more than flog(n + 1)1 comparisons. A binary search tree can become unbalanced as items are added to it. Because balanced binary search trees give optimal worst-case complexity for binary searching, algorithms have been devised that rebalance binary search trees as items are added. The interested reader can consult references on data structures for the description of such algorithm s. Decision Trees Rooted trees can be used to model problems in which a series of decisions leads to a solution. For instance, a binary search tree can be used to locate items based on a series of comparisons, where each comparison tells us whether we have located the item, or whether we should go right or left in a subtree. A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of the decision, is called a decision tree. The possible solutions of the problem correspond to the paths to the leaves of this rooted tree. Example 3 illustrates an application of decision trees. Suppose there are seven coins, all with the same weight, and a counterfeit coin that weighs less than the others. How many weighings are necessary using a balance scale to determine which of the eight coins is the counterfeit one? Give an algorithm for finding this counterfeit coin. Solution: There are three possibilities for each weighing on a balance scale. The two pans can have equal weight, the first pan can be heavier, or the second pan can be heavier. Consequently, the decision tree for the sequence of weighings is a 3-ary tree. There are at least eight leaves in JO-17 Lighter CD @ <» Balance @ ® ® Lighter CD ® CD 0) Impossible ® 10.2 Applications of Trees 699 @ Lighter Balance ® Lighter ® ® FIGURE 3 A Decision Tree for Locating a Counterfeit Coin. The counterfeit coin is shown in color below each final weighing. the decision tree because there are eight possible outcomes (because each of the eight coins can be the counterfeit lighter coin), and each possible outcome must be represented by at least one leaf. The largest number ofweighings needed to determine the counterfeit coin is the height of the decision tree. From Corollary 1 of Section 10.1 it follows that the height of the decision tree is at least flog3 81 = 2. Hence, at least two weighings are needed. It is possible to determine the counterfeit coin using two weighings. The decision tree that illustrates how this is done is shown in Figure 3. .... THE COMPLEXITY OF SORTING ALGORITHMS Many different sorting algorithms have been developed. To decide whether a particular sorting algorithm is efficient, its complexity is determined. Using decision trees as models, a lower bound for the worst-case complexity of sorting algorithms can be found. We can use decision trees to model sorting algorithms and to determine an estimate for the worst-case complexity of these algorithm s. Note that given n elements, there are n! possible orderings of these elements, because each of the n! permutations of these elements can be the correct order. The sorting algorithms studied in this book, and most commonly used sorting algorithms, are based on binary comparisons, that is, the comparison of two elements at a time. The result of each such comparison narrows down the set of possible orderings. Thus, a sorting algorithm based on binary comparisons can be represented by a binary decision tree in which each internal vertex represents a comparison of two elements. Each leaf represents one of the n! permutations of n elements. EXAM PLE 4 We display in Figure 4 a decision tree that orders the elements of the list a, b, c. The complexity of a sort based on binary comparisons is measured in terms of the number of such comparisons used. The largest number of binary comparisons ever needed to sort a list with n elements gives the worst-case performance of the algorithm. The most comparisons used equals the longest path length in the decision tree representing the sorting procedure. In other words, the largest number of comparisons ever needed is equal to the height of the decision tree. Because the height ofa binary tree with n! leaves is at least flog n!l (using Corollary 1 in Section 10.1), at least flog n11 comparisons are needed, as stated in Theorem 1. THEOREM l A sorting algorithm based on binary comparisons requires at least flog n 11 comparisons. 700 10 / Trees COROLL ARYl THEOREM 2 a>b a<b FIGURE 4 A Decision Tree for Sorting Three Distinct Elements. 10-18 We can use Theorem 1 to provide a big-Omega estimate for the number of comparisons used by a sorting algorithm based on binary comparison. We need only note that by Exercise 62 in Section 3.2 we know that flogn!l is 8(n logn), one of the commonly used ref erence functions for the computational complexity of algorithms. Corollary I is a consequence of this estimate. The number of comparisons used by a sorting algorithm to sort n elements based on binary comparisons is Q(n log n). A consequence of Corollary 1 is that a sorting algorithm based on binary comparisons that uses 8(n log n) comparisons, in the worst case, to sort n elements is optimal, in the sense that no other such algorithm has better worst-case complexity. Note that by Theorem 1 in Section 4.4 we see that the merge sort algorithm is optimal in this sense. We can also establish a similar result for the average-case complexity of sorting algorithms. The average number of comparisons used by a sorting algorithm based on binary comparisons is the average depth of a leaf in the decision tree representing the sorting algorithm. By Exercise 48 in Section 10.1 we know that the average depth of a leaf in a binary tree with N vertices is Q(log N). We obtain the following estimate when we let N = n! and note that a function that is Q(logn!) is also Q(n logn) because lo gn! is 8(n logn). The average number of comparisons used by a sorting algorithm to sort n elements based on binary comparisons is Q(n log n). Prefix Codes Consider the problem of using bit strings to encode the letters of the English alphabet (where no distinction is made between lowercase and uppercase letters). We can represent each letter with a bit string of length five, because there are only 26 letters and there are 32 bit strings of length five. The total number of bits used to encode data is five times the number of characters in the text when each character is encoded with five bits. Is it possible to find a coding scheme 10-19 n FIGURE 5 The Binary Tree with a Prefix Code. 10.2 Applications of Trees 701 of these letters such that, when data are coded, fewer bits are used? We can save memory and reduce transmittal time if this can be done. Consider using bit strings of different lengths to encode letters. Letters that occur more frequently should be encoded using short bit strings, and longer bit strings should be used to encode rarely occurring letters. When letters are encoded using varying numbers of bits, some method must be used to determine where the bits for each character start and end. For instance, if e were encoded with 0, a with 1, and t with 01, then the bit string OlOI could correspond to eat, tea, eaea, or ft. One way to ensure that no bit string corresponds to more than one sequence of letters is to encode letters so that the bit string for a letter never occurs as the first part of the bit string for another letter. Codes with this property are called prefix codes. For instance, the encoding of e as 0, a as 10, and t as 11 is a prefix code. A word can be recovered from the unique bit string that encodes its letters. For example, the string 10 11 ° is the encoding of ate. To see this, note that the initial 1 does not represent a character, but 10 does represent a (and could not be the first part of the bit string of another letter). Then, the next 1 does not represent a character, but 11 does represent t. The final bit, 0, represents e. A prefix code can be represented using a binary tree, where the characters are the labels of the leaves in the tree. The edges of the tree are labeled so that an edge leading to a left child is assigned a ° and an edge leading to a right child is assigned a 1. The bit string used to encode a character is the sequence oflabels of the edges in the unique path from the root to the leaf that has this character as its label. For instance, the tree in Figure 5 represents the encoding of e by 0, a by 10, t by llO, n by 1110, ands by 1111. The tree representing a code can be used to decode a bit string. For instance, consider the word encoded by lIIIlOlIlOO using the code in Figure 5. This bit string can be decoded by starting at the root, using the sequence of bits to form a path that stops when a leaf is reached. Each ° bit takes the path down the edge leading to the left child of the last vertex in the path, and each 1 bit corresponds to the right child ofthis vertex. Consequently, the initial 1111 corresponds to the path starting at the root, going right four times, leading to a leaf in the graph that has s as its label, because the string 1111 is the code for s. Continuing with the fifth bit, we reach a leaf next after going right then left, when the vertex labeled with a, which is encoded by 10, is visited. Starting with the seventh bit, we reach a leaf next after going right three times and then left, when the vertex labeled with n, which is encoded by 1110, is visited. Finally, the last bit, 0, leads to the leaf that is labeled with e. Therefore, the original word is sane. We can construct a prefix code from any binary tree where the left edge at each internal vertex is labeled by ° and the right edge by a 1 and where the leaves are labeled by characters. Characters are encoded with the bit string constructed using the labels of the edges in the unique path from the root to the leaves. HUFFMAN CODING We now introduce an algorithm that takes as input the frequencies (which are the probabilities of occurrences) of symbols in a string and produces as output a prefix code that encodes the string using the fewest possible bits, among all possible binary prefix codes for these symbols. This algorithm, known as Huffman coding, was developed by David Huffinan in a term paper he wrote in 1951 while a graduate student at MIT. (Note that this algorithm assumes that we already know how many times each symbol occurs in the string, so we can compute the frequency of each symbol by dividing the number of times this symbol occurs by the length of the string.) Huffinan coding is a fundamental algorithm in data compression, the subject devoted to reducing the number of bits required to represent information. Huffman coding is extensively used to compress bit strings representing text and it also plays an important role in compressing audio and image files. Algorithm 2 presents the Huffinan coding algorithm. Given symbols and their frequencies, our goal is to construct a rooted binary tree where the symbols are the labels of the leaves. The algorithm begins with a forest of trees each consisting of one vertex, where each vertex has a symbol as its label and where the weight of this vertex equals the frequency of the symbol that 702 10 / Trees Demo� EXAMPLE S EK1ra� Examples � linl{S� 10-20 is its label. At each step, we combine two trees having the least total weight into a single tree by introducing a new root and placing the tree with larger weight as its left subtree and the tree with smaller weight as its right subtree. Furthermore, we assign the sum of the weights of the two subtrees of this tree as the total weight ofthe tree.(Although procedures for breaking ties by choosing between trees with equal weights can be specified, we will not specify such procedures here.) The algorithm is finished when it has constructed a tree, that is, when the forest is reduced to a single tree. ALGORITHM 2 Huffman Coding. procedure Huffman( C: symbols ai with frequencies Wi, i = 1, ... , n) F := forest ofn rooted trees, each consisting of the single vertex ai and assigned weight Wi while F is not a tree begin Replace the rooted trees T and T' ofleast weights from F with weT) � weT') with a tree having a new root that has T as its left subtree and T' as its right subtree. Label the new edge to T with 0 and the new edge to T' with 1. Assign weT) + weT') as the weight of the new tree. end {the Huffinan coding for the symbol ai is the concatenation of the labels of the edges in the unique path from the root to the vertex ai} Example 5 illustrates how Algorithm 2 is used to encode a set of five symbols. Use Huffman coding to encode the following symbols with the frequencies listed: A: 0.08, B: 0.10, C: 0.12, D: 0.15, E: 0.20, F: 0.35. What is the average number of bits used to encode a character? Solution: Figure 6 displays the steps used to encode these symbol s. The encoding produced encodes A by 111, B by 110, C by 011, D by 010, E by 10, and F by 00. The average number of bits used to encode a symbol using this encoding is 3 ·0.08 + 3 ·0.10 + 3 ·0.12 + 3 ·0.15 + 2 . 0.20 + 2 . 0.35 = 2.45. .... DAVID A. HUFFMAN (1925-1999) David Huffman grew up in Ohio. At the age of 18 he received his B.S. in electrical engineering from The Ohio State University. Afterward he served in the U.S. Navy as a radar maintenance officer on a destroyer that had the mission of clearing mines in Asian waters after World War II. Later, he earned his M.S. from Ohio State and his Ph.D. in electrical engineering from MIT. Huffman joined the MIT faculty in 1953, where he remained until 1967 when he became the founding member of the computer science department at the University of California at Santa Cruz. He played an important role in developing this department and spent the remainder of his career there, retiring in 1994. Huffman is noted for his contributions to information theory and coding, signal designs for radar and for communications, and design procedures for asynchronous logical circuits. His work on surfaces with zero curvature led him to develop original techniques for folding paper and vinyl into unusual shapes considered works of art by many and publicly displayed in several exhibits. However, Huffman is best known for his development of what is now called Huffman coding, a result of a term paper he wrote during his graduate work at MIT. Huffman enjoyed exploring the outdoors, hiking, and traveling extensively. He became certified as a scuba diver when he was in his late 60s. He kept poisonous snakes as pets. 10-21 008 • A 0.12 • C 0.10 • B 018 i\ B A 0.15 • D 0.27 0.12 • C 0.18 i\ B A 0.20 • E 0.35 0.15 0.20 • • D E 0.20 • E 0.27 i\ D C 0.38 i\ • F ~ D C o 1 0.38 0.62 B � {\ E � F � BA D C 1.00 D C B A A FIGURE 6 Huffman Coding of Symbols in Example 4. 0.35 • F 10.2 Applications of Trees 703 0.35 • F 0.35 • F Initial forest Step I Step 2 Step 3 Step 4 Step 5 Note that Huffman coding is a greedy algorithm. Replacing the two subtrees with the smallest weight at each step leads to an optimal code in the sense that no binary prefix code for these symbols can encode these symbols using fewer bits. We leave the proof that Huffman codes are optimal as Exercise 32 at the end of this section. There are many variations of Huffman coding. For example, instead of encoding single symbols, we can encode blocks of symbols of a specified length, such as blocks of two symbols. Doing so may reduce the number of bits required to encode the string (see Exercise 30 at the end of this section). We can also use more than two symbols to encode the original symbols in the string (see the preamble to Exercise 28 at the end of this section). Furthermore, a variation known as adaptive Huffman coding (see [SaOO]) can be used when the frequency of each symbol in a string is not known in advance, so that encoding is done at the same time the string is being read. 704 10 / Trees 10-22 Game Trees Unks � Trees can be used to analyze certain types of games such as tic-tac-toe, nim, checkers, and chess. In each of these games, two players take turns making moves. Each player knows the moves made by the other player and no element of chance enters into the game. We model such games using game trees; the vertices of these trees represent the positions that a game can be in as it progresses; the edges represent legal moves between these positions. Because game trees are usually large, we simplifY game trees by representing all symmetric positions of a game by the same vertex. However, the same position of a game may be represented by different vertices if different sequences of moves lead to this position. The root represents the starting position. The usual convention is to represent vertices at even levels by boxes and vertices at odd levels by cir­ cles. When the game is in a position represented by a vertex at an even level, it is the first player's move; when the game is in a position represented by a vertex at an odd level, it is the second player's move. Game trees may be infinite when the games they represent never end, such as games that can enter infinite loops, but for most games there are rules that lead to finite game trees. The leaves of a game tree represent the final positions of a game. We assign a value to each leaf indicating the payoff to the first player if the game terminates in the position represented by this leaf. For games that are win-lose, we label a terminal vertex represented by a circle with a 1 to indicate a win by the first player and we label a terminal vertex represented by a box with a -1 to indicate a win by the second player. For games where draws are allowed, we label a terminal vertex corresponding to a draw position with a O. Note that for win-lose games, we have assigned values to terminal vertices so that the larger the value, the better the outcome for the first player. In Example 6 we display a game tree for a well-known and well-studied game. EXAMPLE 6 Nim In a version of the game of nim, at the start of a game there are a number of piles of stones. Two players take turns making moves; a legal move consists of removing one or more stones from one of the piles, without removing all the stones left. A player without a legal move loses. (Another way to look at this is that the player removing the last stone loses because the position with no piles of stones is not allowed.) The game tree shown in Figure 7 represents �� +1 +1 �� +1 +1 -I -I -I -I FIGURE 7 The Game Tree for a Game of Nim. � +1 +1 Terminal vertices are labeled with + I if the first player wins -I and -I if the second player wins 10-23 (a) (b) xxo OOX XOX XXO XOO XOX draw X win-. 10.2 Applications of Trees 705 FIGURE 8 Some of the Game Tree for Tic-Tac-Toe. this version of nim given the starting position where there are three piles of stones containing two, two, and one stone each, respective ly. We represent each position with an unordered list of the number of stones in the different piles (the order of the piles does not matter). The initial move by the first player can lead to three possible positions because this player can remove one stone from a pile with two stones (leaving three piles containing one, one, and two stones); two stones from a pile containing two stones (leaving two piles containing two stones and one stone); or one stone from the pile containing one stone (leaving two piles of two stones). When only one pile with one stone is left, no legal moves are possible, so such positions are terminal positions. Because nim is a win-lose game, we label the terminal vertices with + 1 when they represent wins for the first player and -1 when they represent wins for the second playeL � EXAMPLE 7 Tic-tac-toe The game tree fortie-tac-toe is extremely large and cannot be drawn here, although a computer could easily build such a tree. We show a portion of the game tic-tac-toe in Figure 8(a). Note that by considering symmetric positions equivalent, we need only consider three possible initial moves, as shown in Figure 8(a). We also show a subtree of this game tree leading to terminal positions in Figure 8(b), where a player who can win makes a winning move. � We can recursively define the values of all vertiees in a game tree in a way that enables us to determine the outcome of this game when both players follow optimal strategies. By a strategy we mean a set of rules that tells a player how to select moves to win the game. An optimal strategy for the first player is a strategy that maximizes the payoff to this player and for the second player is a strategy that minimizes this payoff. We now recursively define the value ofa vertex. 706 10 / Trees DEFINITION 1 THEOREM 3 10-24 The value of a vertex in a game tree is defined recursively as: (i) the value of a leaf is the payoff to the first player when the game terminates in the position represented by this leaf. (ii) the value of an internal vertex at an even level is the maximum of the values of its children, and the value of an internal vertex at an odd level is the minimum of the values of its children. The strategy where the first player moves to a position represented by a child with maximum ' value and the second player moves to a position of a child with minimum value is called the min max strategy. We can determine who will win the game when both players follow the minmax strategy by calculating the value of the root of the tree; this value is called the value of the tree. This is a consequence of Theorem 3. The value of a vertex of a game tree tells us the payoff to the first player ifboth players follow the minmax strategy and play starts from the position represented by this vertex. Proof: We will use induction to prove this theorem. BASIS STEP: If the vertex is a leaf, by definition the value assigned to this vertex is the payoff to the first player. INDUCTIVE STEP: The inductive hypothesis is the assumption that the values of the children of a vertex are the payoffs to the first player, assuming that play starts at each of the positions represented by these vertices. We need to consider two cases, when it is the first player's turn and when it is the second player's turn. When it is the first player's turn, this player follows the minmax strategy and moves to the position represented by the child with the largest value. By the inductive hypothesis, this value is the payoff to the first player when play starts at the position represented by this child and follows the minmax strategy. By the recursive step in the definition of the value of an internal vertex at an even level (as the maximum value of its children), the value of this vertex is the payoff when play begins at the position represented by this vertex. When it is the second player's turn, this player follows the minmax strategy and moves to the position represented by the child with the least value. By the inductive hypothesis, this value is the payoff to the first player when play starts at the position represented by this child and both players follow the minmax strategy. By the recursive definition of the value of an internal vertex at an odd level as the minimum value of its children, the value of this vertex is the payoff when play begins at the position represented by this vertex. <l Remark: By extending the proof of Theorem 3, it can be shown that the minmax strategy is the optimal strategy for both players. Example 8 illustrates how the minmax procedure works. It displays the values assigned to the internal vertices in the game tree from Example 6. Note that we can shorten the computation required by noting that for win-lose games, once a child of a square vertex with value + I is found, the value of the square vertex is also + I because + I is the largest possible payoff. 10-25 EXAM PLES unks� -I -I -I -I -I 10.2 Applications of Trees 707 max min max min +1 +1 FIGURE 9 Showing the Values of Vertices in the Game of Nim. Similarly, once a child of a circle vertex with value -1 is found, this is the value of the circle vertex also. In Example 6 we constructed the game tree for nim with a starting position where there are three piles containing two, two, and one stones. In Figure 9 we show the values of the vertices of this game tree. The values of the vertices are computed using the values of the leaves and working one level up at a time. In the right margin of this figure we indicate whether we use the maximum or minimum of the values of the children to find the value of an internal vertex at each level. For example, once we have found the values of the three children of the root, which are 1, -1, and -1, we find the value of the root by computing max(l, -1, -1) = 1. Because the value of the root is 1, it follows that the first player wins when both players follow a minmax strategy. .... Game trees for some well-known games can be extraordinarily large, because these games have many different possible moves. For example, the game tree for chess has been estimated to have as many as 10100 vertices! It may be impossible to use Theorem 3 directly to study a game because of the size of the game tree. Therefore, various approaches have been devised to help determine good strategies and to determine the outcome of such games. One useful technique, called alpha-beta pruning, eliminates much computation by pruning portions of the game tree that cannot affect the values of ancestor vertices. (For information about alpha-beta pruning, consult [Gr90].) Another useful approach is to use evaluation junctions, which estimate the value of internal vertices in the game tree when it is not feasible to compute these values exactly. For example, in the game oftic-tac-toe, as an evaluation function for a position, we may use the number of files (rows, columns, and diagonals) containing no Os (used to indicate moves of the second player) minus the number of files containing no Xs (used to indicate moves of the first player). This evaluation function provides some indication of which player has the advantage in the game. Once the values of an evaluation function are inserted, the value of the game can be computed following the rules used for the minmax strategy. Computer programs created to play chess, such as the famous Deep Blue program, are based on sophisticated evaluation functions. For more information about how computers play chess see [Le91]. 708 10 / Trees Exercises 1. Build a binary search tree for the words banana, peach, apple, pear, coconut, mango, and papaya using alphabet­ icalorder. 2. Build a binary search tree for the words oenology, phrenol­ ogy, campanology, ornithology, ichthyology, limnology, alchemy, and astrology using alphabetical order. 3. How many comparisons are needed to locate or to add each of these words in the search tree for Exercise 1, start­ ing fresh each time? a) pear c) kumquat b) banana d) orange 4. How many comparisons are needed to locate or to add each of the words in the search tree for Exercise 2, start­ ing fresh each time? a) palmistry c) paleontology b) etymology d) glaciology 5. Using alphabetical order, construct a binary search tree for the words in the sentence " The quick brown fox jumps over the lazy dog." 6. How many weighings of a balance scale are needed to find a lighter counterfeit coin among four coins? Describe an algorithm to find the lighter coin using this number of weighings. 7. How many weighings of a balance scale are needed to find a counterfeit coin among four coins if the counterfeit coin may be either heavier or lighter than the others? Describe an algorithm to find the counterfeit coin using this number of weighings. * 8. How many weighings of a balance scale are needed to find a counterfeit coin among eight coins ifthe counterfeit coin is either heavier or lighter than the others? Describe an al­ gorithm to find the counterfeit coin using this number of weighings. *9. How many weighings of a balance scale are needed to find a counterfeit coin among 12 coins if the counterfeit coin is lighter than the others? Describe an algorithm to find the lighter coin using this number of weighings. �1 O. One of four coins may be counterfeit. If it is counterfeit, it may be lighter or heavier than the others. How many weighings are needed, using a balance scale, to determine whether there is a counterfeit coin, and if there is, whether it is lighter or heavier than the others? Describe an algo­ rithm to find the counterfeit coin and determine whether it is lighter or heavier using this number of weighings. 11. Find the least number of comparisons needed to sort four elements and devise an algorithm that sorts these elements using this number of comparisons. �12. Find the least number of comparisons needed to sort five elements and devise an algorithm that sorts these elements using this number of comparisons. The tournament sort is a sorting algorithm that works by building an ordered binary tree. We represent the elements to 10-26 be sorted by vertices that will become the leaves. We build up the tree one level at a time as we would construct the tree repre­ senting the winners of matches in a tournament. Working left to right, we compare pairs of consecutive elements, adding a parent vertex labeled with the larger of the two elements under comparison. We make similar comparisons between labels of vertices at each level until we reach the root of the tree that is labeled with the largest element. The tree constructed by the tournament sort of22, 8,14,17,3,9,27,11 is illustrated in part (a) of the figure. Once the largest element has been de­ termined, the leaf with this label is relabeled by -00, which is defined to be less than every element. The labels of all ver­ tices on the path from this vertex up to the root of the tree are recalculated, as shown in part (b) ofthe figure. This produces the second largest element. This process continues until the entire list has been sorted. (a) � nd largest element � � ���� ®CD®®0® 8® (b) 13. Complete the tournament sort of the list 22,8, 14, 17,3, 9,27, 11. Show the labels of the vertices at each step. 14. Use the tournament sort to sort the list 17,4, 1, 5, 13, 10, 14,6. 15. Describe the tournament sort using pseudocode. 16. Assuming that n, the number of elements to be sorted, equals 2k for some positive integer k, determine the num­ ber of comparisons used by the tournament sort to find the largest element of the list using the tournament sort. 17. How many comparisons does the tournament sort use to find the second largest, the third largest, and so on, up to the (n -l)st largest (or second smallest) element? 18. Show that the tournament sort requires 8(n logn) com­ parisons to sort a list of n elements. [Hint: By inserting the appropriate number of dummy elements defined to be 10-27 smaller than all integers, such as -00, assume that n = 2k for some positive integer k.] 19. Which of these codes are prefix codes? a) a: 11, e: 00, t: 10, s: 01 b) a: 0, e: 1, t: 01, s: 001 c) a: 101, e: 11, t: 001, s: 011, n: 010 d) a: 010, e: 11, t: 011, s: 1011, n: 1001, i: 10101 20. Construct the binary tree with prefix codes representing these coding schemes. a) a: 11, e: 0, t: tOl, s: 100 b) a: 1, e: 01, t: 001, s: 0001, n: 00001 c) a: 1010, e: 0, t: 11, s: 1011, n: 1001, i: 10001 21. What are the codes for a, e, i, k, 0, p, and u if the coding scheme is represented by this tree? p u 22. Given the coding scheme a: 001, b: 0001, e: 1, r: 0000, s: 0100, t: 011, x: 01010, find the word represented by a) 01110100011. b) 0001110000. c) 01001010 10. d) 01100101010 . 23. Use Huffman coding to encode these symbols with given frequenc ies: a: 0.20, b: 0.10, c: 0.15, d: 0.25, e: 0.30. What is the average number of bits required to encode a character? 24. Use Huffman coding to encode these symbols with given frequenc ies: A: 0.10, B: 0.25, C: 0.05, D: 0.15, E: 0.30, F: 0.07, G: 0.08. What is the average number of bits re­ quired to encode a symbol? 25. Construct two different Huffman codes for these symbols and frequencies: t: 0.2, u: 0.3, v: 0.2, w: 0.3. 26. a) Use Huffman coding to encode these symbols with frequencies a: 0.4, b: 0.2, c: 0.2, d: 0.1, e: 0.1 in two different ways by breaking ties in the algorithm differ­ ently. First, among the trees of minimum weight select two trees with the largest number of vertices to com­ bine at each stage of the algorithm. Second, among the trees of minimum weight select two trees with the smallest number of vertices at each stage. b) Compute the average number of bits required to en­ code a symbol with each code and compute the vari­ ances of this number of bits for each code. Which tie-breaking procedure produced the smaller variance in the number of bits required to encode a symbol? 10.2 Applications of Trees 709 27. Construct a Huffman code for the letters ofthe English al­ phabet where the frequencies of letters in typical English text are as shown in this table. Letter Frequency Letter Frequency A 0.0817 N 0.0662 B 0.0145 0 0.0781 C 0.0248 P 0.0156 D 0.0431 Q 0.0009 E 0.1232 R 0.0572 F 0.0209 S 0.0628 G 0.0182 T 0.0905 H 0.0668 U 0.0304 I 0.0689 V 0.0102 J 0.0010 W 0.0264 K 0.0080 X 0.0015 L 0.0397 Y 0.0211 M 0.0277 Z 0.0005 Suppose that m is a positive integer with m � 2. An m-ary Huffman code for a set of N symbols can be constructed analogously to the construction of a binary Huffman code. At the initial step, «N -1) mod (m -1» + 1 trees consist­ ing of a single vertex with least weights are combined into a rooted tree with these vertices as leaves. At each subsequent step, the m trees ofleast weight are combined into an m-ary tree. 28. Describe the m-ary Huffman coding algorithm in pseudocode. 29. Using the symbols 0, 1, and 2 use ternary (m = 3) Huffman coding to encode these letters with the given frequencies: A: 0.25, E: 0.30, N: 0.10, R: 0.05, T: 0.12, Z: 0.18. 30. Consider the three symbols A, B, and C with frequencies A: 0.80, B: 0.19, C: om. a) Construct a Huffman code for these three symbols. b) Form a new set of nine symbols by grouping together blocks of two symbols, AA, AB, AC, BA, BB, BC, CA, CB, and CC. Construct a Huffman code for these nine symbols, assuming that the occurrences of symbols in the original text are independent. c) Compare the average number of bits required to en­ code text using the Huffman code for the three symbols in part (a) and the Huffman code for the nine blocks of two symbols constructed in part (b). Which is more efficient? 31. Given n + 1 symbols Xl, X2, ••• , Xn, Xn+l appearing 1, fl, fz, ... , fn times in a symbol string, respective ly, where jj is the jth Fibonacci number, what is the maxi­ mum number of bits used to encode a symbol when all pos­ sible tie-breaking selections are considered at each stage of the Huffman coding algorithm? 710 10 / Trees '32. Show that Huffman codes are optimal in the sense that they represent a string of symbols using the fewest bits among all binary prefix codes. 33. Draw a game tree for nim if the starting position consists of two piles with two and three stones, respective ly. When drawing the tree represent by the same vertex symmetric positions that result from the same move. Find the value of each vertex of the game tree. Who wins the game if both players follow an optimal strategy? 34. Draw a game tree for nim if the starting position consists of three piles with one, two, and three stones, respectivel y. When drawing the tree represent by the same vertex sym­ metric positions that result from the same move. Find the value of each vertex of the game tree. Who wins the game if both players follow an optimal strategy? 35. Suppose that we vary the payoff to the winning player in the game of nim so that the payoff is n dollars when n is the number of legal moves made before a terminal posi­ tion is reached. Find the payoff to the first player if the initial position consists of a) two piles with one and three stones, respectively. b) two piles with two and four stones, respectively. c) three piles with one, two, and three stones, respectively. 36. Suppose that in a variation ofthe game ofnim we allow a player to either remove one or more stones from a pile or merge the stones from two piles into one pile as long as at least one stone remains. Draw the game tree for this vari­ ation ofnim if the starting position consists ofthree piles containing two, two, and one stone, respectively. Find the values of each vertex in the game tree and determine the winner ifboth players follow an optimal strategy. 37. Draw the subtree of the game tree for tic-tac-toe begin­ ning at each of these positions . Determine the value of each of these subtrees. .) � I � I � c)� o o 0 X X b)�OX o X X o d)� X X 0 X 0 10.3 Tree Traversal Introduction 10-28 38. Suppose that the first four moves of a tic-tac-toe game are as shown. Does the first player (whose moves are marked by Xs) have a strategy that will always win? d)m= X 0 o 39. Show that if a game ofnim begins with two piles contain­ ing the same number of stones, as long as this number is at least two, then the second player wins when both players follow optimal strategies. 40. Show that if a game of nim begins with two piles con­ taining different numbers of stones, the first player wins when both players follow optimal strategies. 41. How many children does the root of the game tree for checkers have? How many grandchildren does it have? 42. How many children does the root of the game tree for nim have and how many grandchildren does it have if the starting position is a) piles with four and five stones, respectively. b) piles with two, three, and four stones, respectively. c) piles with one, two, three, and four stones, respectively. d) piles with two, two, three, three, and five stones, respectively. 43. Draw the game tree for the game of tic-tac-toe for the levels corresponding to the first two moves. Assign the value of the evaluation function mentioned in the text that assigns to a position the number of files containing no Os minus the number of files containing no Xs as the value of each vertex at this level and compute the value of the tree for vertices as if the evaluation function gave the correct values for these vertices. 44. Use pseudocode to describe an algorithm for determin­ ing the value of a game tree when both players follow a minmax strategy. linkS � Ordered rooted trees are often used to store information. We need procedures for visiting each vertex of an ordered rooted tree to access data. We will describe several important algorithms for visiting all the vertices of an ordered rooted tree. Ordered rooted trees can also be used to represent various types of expressions, such as arithmetic expressions involving numbers, 10-29 EXAM PLEl 10.3 Tree Traversal 711 variables, and operations. The different listings of the vertices of ordered rooted trees used to represent expressions are useful in the evaluation of these expressions. Universal Address Systems Procedures for traversing all vertices of an ordered rooted tree rely on the orderings of children. In ordered rooted trees, the children of an internal vertex are shown from left to right in the drawings representing these directed graphs. We will describe one way we can totally order the vertices of an ordered rooted tree. To produce this ordering, we must first label all the vertices. We do this recursively: 1. Label the root with the integer O. Then label its k children (at level 1) from left to right with1,2,3, ... ,k. 2. For each vertex v at level n with label A, label its kv children, as they are drawn from left to right, with A.1, A.2, ... , A.kv. Following this procedure, a vertex vat level n, for n ::: 1, is labeled Xl.X2 ....• Xn, where the unique path from the root to v goes through the Xlst vertex at levell, the x2nd vertex at level2, and so on. This labeling is called the universal address system of the ordered rooted tree. We can totally order the vertices using the lexicographic ordering oftheir labels in the univer­ sal address system. The vertex labeled Xl.X2 .••.. Xn is less than the vertex labeled Yl.Y2 ••••• Ym if there is an i, 0 :::: i :::: n, with Xl = YJ. X2 = Y2, ... , Xi-l = Yi-J. and Xi < Yi; or if n < m and Xi = Yi for i = 1,2, ... , n. We display the labelings of the universal address system next to the vertices in the ordered rooted tree shown in Figure 1. The lexicographic ordering of the labelings is 0<1 < 1.1 < 1.2 < 1.3 < 2 < 3 < 3.1 < 3.1.1 < 3.1.2 < 3.1.2.1 < 3.1.2.2 < 3.1.2.3 < 3.1.2.4 < 3.1.3 < 3.2 < 4 < 4.1 < 5 < 5.1 < 5.1.1 < 5.2 < 5.3 1.1 1.2 1.3 o 3.1.2.1 I 3.1.2.3 I 3.1.2.2 3.1.2.4 FIGURE 1 The Universal Address System of an Ordered Rooted Tree. 712 10 / Trees DEFINITION 1 EXAMPLE 2 Extra� Examples � Step 2: Step 3: Visit T] Visit T2 r in preorder in pre order Step I: Visit r Preorder traversal Stepn+ I: Visit Tn in preorder FIGURE 2 Preorder Traversal. Traversal Algorithms n 0 p a JO-30 FIGURE 3 The Ordered Rooted Tree T. Procedures for systematically visiting every vertex of an ordered rooted tree are called traversal algorithms. We will describe three of the most commonly used such algorithms, preorder traversal, inorder traversal, and postorder traversal. Each of these algorithms can be defined recursively. We first define preorder traversal. Let T be an ordered rooted tree with root r. If T consists only of r, then r is the preorder traversal of T. Otherwise, suppose that TJ, Tz, ... , Tn are the subtrees at r from left to right in T. The preorder traversal begins by visiting r. It continues by traversing TJ in preorder, then Tz in preorder, and so on, until Tn is traversed in preorder. The reader should verify that the preorder traversal of an ordered rooted tree gives the same ordering of the vertices as the ordering obtained using a universal address system. Figure 2 indicates how a preorder traversal is carried out. Example 2 illustrates preorder traversal. In which order does a preorder traversal visit the vertices in the ordered rooted tree T shown in Figure 3? Solution: The steps of the preorder traversal of T are shown in Figure 4. We traverse T in preorder by first listing the root a, followed by the preorder list of the subtree with root b, the preorder list of the subtree with root c (which is just c) and the preorder list of the subtree with root d. The preorder list of the subtree with root b begins by listing b, then the vertices of the subtree with root e in pre order, and then the subtree with root f in preorder (which is just f). The preorder list of the subtree with root d begins by listing d, followed by the preorder list of the subtree with root g, followed by the subtree with root h (which is just h), followed by the subtree with root i (which is just i). /0-3/ 10.3 Tree Traversal 713 a Preorder traversal: Visit root, visit subtrees left to right n 0 p a b c d • fr. m n 0 p a b e f c d g h • • � • 1\ • m n 0 p a b e j k f c d g m h • • • 11\ • • • • • • • n 0 p a b e j k n 0 p f c d g m h • • • • • • • • • • • • • • • • FIGURE 4 The Preorder Traversal of T. The preorder list of the subtree with root e begins by listing e, followed by the preorder listing of the subtree with root j (which is just j), followed by the preorder listing of the subtree with root k. The preorder listing of the subtree with root g is g followed by I, followed by m. The pre order listing of the subtree with root k is k, n, 0, p. Consequently, the preorder traversal ofT is a, b, e, j, k, n, 0, p, J, c, d, g, I, m, h, i. .... We will now define inorder traversal. 714 10 / Trees DEFINITION 2 EXAMPLE 3 Extra� Examples � DEFINITION 3 Step I: Visit T] in inorder Step 3: Visit T2in inorder r Step 2: Visit r Inorder traversal Step n + I: Visit Tn in inorder FIGURE 5 Inorder Traversal. 10-32 Let T be an ordered rooted tree with root r. If T consists only of r, then r is the inorder traversal of T . Otherwise, suppose that T\, T2, ... , Tn are the subtrees at r from left to right. The inorder traversal begins by traversing T\ in inorder, then visiting r. It continues by traversing T2 in inorder, then T3 in inorder, ... , and finally Tn in inorder. Figure 5 indicates how inorder traversal is carried out. Example 3 illustrates how inorder traversal is carried out for a particular tree. In which order does an inorder traversal visit the vertices of the ordered rooted tree T in Figure 3? Solution: The steps of the inorder traversal of the ordered rooted tree T are shown in Figure 6. The inorder traversal begins with an inorder traversal of the subtree with root b, the root a, the inorder listing of the subtree with root c, which is just c, and the inorder listing of the subtree with root d. The inorder listing of the subtree with root b begins with the inorder listing of the subtree with root e, the root b, and /. The inorder listing of the subtree with root d begins with the inorder listing of the subtree with root g, followed by the root d, followed by h, followed by i. The inorder listing of the subtree with root e is j, followed by the root e, followed by the inorder listing of the subtree with root k. The inorder listing of the subtree with root g is I, g, m. The inorder listing of the subtree with root k is n, k, 0, p. Consequently, the inorder listing of the ordered rooted tree is j, e, n, k, 0, p, b, /, a, c, I, g, m, d, h, i. � We now define postorder traversal. Let T be an ordered rooted tree with root r. If T consists only of r, then r is the postorder traversal of T . Otherwise, suppose that T\, T2, ... , Tn are the subtrees at r from left to right. The postorder traversal begins by traversing T\ in postorder, then T2 in postorder, ... , then Tn in postorder, and ends by visiting r. Figure 7 illustrates how postorder traversal is done. Example 4 illustrates how postorder traversal works. 10-33 EXAM PLE 4 Extra� Examples IIiiiiiij 10.3 Tree Traversal 715 T a Inorder traversal: Visit leftmost subtree, visit root, visit other subtrees left to right n 0 p b a c d • R m n 0 p e b f a c g d h !\ • • 1\ • • m n 0 p j e k b f a c g m d h • it\ • • • • • • • • • n 0 p j e n k 0 p b f a c g m d h i • • • • • • • • • • • • • • • • FIGURE 6 The Inorder Traversal of T. In which order does a postorder traversal visit the vertices of the ordered rooted tree T shown in Figure 3? Solution: The st�ps of the postorder traversal of the ordered rooted tree T are shown in Figure 8. The postorder traversal begins with the postorder 'traversal ofthe subtree with root b, the postorder traversal of the subtree with root c, which is'j�st c, the postorder traversal of the subtree with root d, followed by the root a. ' 716 10 / Trees Step I' VisitTI in postorder r Step 2' Visit T2 in postorder Step 11 + I: Visit r Po_torder traversal Step 11: Visit Tn in postorder FIGURE 7 Postorder Traversal. JO-34 The postorder traversal of the subtree with root b begins with the postorder traversal of the subtree with root e, followed by j, followed by the root b. The postorder traversal of the rooted tree with root d begins with the postorder traversal of the subtree with root g, followed by h, followed by i, followed by the root d. The postorder traversal of the subtree with root e begins with j, followed by the postorder traversal of the subtree with root k, followed by the root e. The postorder traversal of the subtree with root g is t, m, g. The postorder traversal of the subtree with root k is n, 0, p, k. Therefore, the postorder traversal of T is j, n, 0, p, k, e, j, b, c, t, m, g, h, i, d, a. .... There are easy ways to list the vertices of an ordered rooted tree in preorder, inorder, and postorder. To do this, first draw a curve around the ordered rooted tree starting at the root, moving along the edges, as shown in the example in Figure 9. We can list the vertices in preorder by listing each vertex the first time this curve passes it. We can list the vertices in inorder by listing a leaf the first time the curve passes it and listing each internal vertex the second time the curve passes it. We can list the vertices in postorder by listing a vertex the last time it is passed on the way back up to its parent. When this is done in the rooted tree in Figure 9, it follows that the preorder traversal gives a, b, d, h, e, i, j, c, j, g, k, the inorder traversal gives h, d, b, i, e, j, a, j, c, k, g; and the postorder traversal gives h, d, i, j, e, b, j, k,g,c,a. Algorithms for traversing ordered rooted trees in preorder, inorder, or postorder are most easily expressed recursively. ALGORITHM 1 Pre order Traversal. procedure preorder (T : ordered rooted tree) r := root ofT list r for each child e of r from left to right begin T(e) := subtree with e as its root preorder (T(e» end 10-35 10.3 Tree Traversal 717 T a Postorder traver,al : Visit subtrees left to right; visit root n 0 p b c d a }<1\ • m n 0 p e f b c g h d a ~ • i\ • • • • m n 0 p j k e f b c m g h d a • !l\ • • • • • • • • • • n 0 p j n 0 p k e f b c m g h d a • • • • • • • • • • • • • • • • FIGURE 8 The Postorder Traversal of T. 718 10 I Trees a d g h k FIGURE 9 A Shortcut for Traversing an Ordered Rooted Tree in Preorder, Inorder, and Postorder . ALGOR ITHM 2 Inorder Traversal. procedure inorder(T: ordered rooted tree) r := root ofT if r is a leaf then list r else begin I := first child ofr from left to right T(l) := subtree with I as its root inorder(T (l)) list r for each child c of r except for I from left to right T(c) := subtree with c as its root inorder(T(c)) end ALGORITHM 3 Postorder Traversal. procedure postorder(T : ordered rooted tree) r := root ofT for each child c of r from left to right begin T(c) := subtree with c as its root postorder(T(c)) end list r /0-36 Note that both the preorder traversal and the postorder traversal encode the structure of an ordered rooted tree when the number of children of each vertex is specified. That is, an ordered rooted tree is uniquely determined when we specify a list of vertices generated by a preorder traversal or by a postorder traversal ofthe tree, together with the number of children of each vertex 10-37 + /\ .r y l' /\ + 2 /\ .r r - /\ r 4 / /\ -3 /\ x 4 10.3 Tree Traversal 719 + 1'/� /\ /\ + 2 -3 /\ /\ x y x 4 FIGURE 10 A Binary Tree Representing «x + y) i 2) + «x -4)/3). (see Exercises 26 and 27). In particular, both a preorder traversal and a postorder traversal encode the structure of a full ordered m-ary tree. However, when the number of children of vertices is not specified, neither a preorder traversal nor a postorder traversal encodes the structure of an ordered rooted tree (see Exercises 28 and 29). Infix, Prefix, and Postfix Notation We can represent complicated expressions, such as compound propositions, combinations of sets, and arithmetic expressions using ordered rooted trees. For instance, consider the repre­ sentation of an arithmetic expression involving the operators + (addition), -(subtraction), * (multiplication), / (division), and t (exponentia tion). We will use parentheses to indicate the order of the operations. An ordered rooted tree can be used to represent such expressions, where the internal vertices represent operations, and the leaves represent the variables or numbers. Each operation operates on its left and right subtrees (in that order). EXAMPLE 5 What is the ordered rooted tree that represents the expression «x + y) t 2) + «x -4)/3)? Solution: The binary tree for this expression can be built from the bottom up. First, a subtree for the expression x + y is constructed. Then this is incorporated as part of the larger subtree rep­ resenting (x + y) t 2. Also, a subtree for x -4 is constructed, and then this is incorporated into a subtree representing (x -4)/3. Finally the subtrees representing (x + y) t 2 and (x -4)/3 are combined to form the ordered rooted tree representing «x + y) t 2) + «x - 4)/3). These steps are shown in Figure 10. � An inorder traversal of the binary tree representing an expression produces the original expression with the elements and operations in the same order as they originally occurred, except for unary operations, which instead immediately follow their operands. For instance, inorder traversals of the binary trees in Figure 11, which represent the expressions (x + y)/ (x + 3), (x + (y/x» + 3, and x + (y/(x + 3», all lead to the infix expression x + y/x + 3. To make such expressions unambiguous it is necessary to include parentheses in the inorder traversal whenever we encounter an operation. The fully parenthesized expression obtained in this way is said to be in infix form. We obtain the prefix form of an expression when we traverse its rooted tree in preorder. Expressions written in prefix form are said to be in Polish notation, which is named after the Polish logician Jan Lukasiewicz. An expression in prefix notation (where each operation has 720 to I Trees + + I /� /� /� + 3 x I /\ /\ + + x /1\ v + /\ /\ /\ x y x 3 y x x 3 FIGURE 11 Rooted Trees Representing (x + y)/(x + 3), (x + (y Ix» + 3, and x + (y I (x + 3». 10-38 a specified number of operands), is unambiguous, so no parenthe ses are needed in such an expression. The verification of this is left as an exercise for the reader. EXAMPLE 6 What is the prefix form for ((x + y) t 2) + ((x -4)/3)? Solution: We obtain the prefix form for this expression by traversing the binary tree that represents it, shown in Figure to. This produces + t + x y 2 / - x 4 3. .... In the prefix form of an expression, a binary operator, such as +, precedes its two operands. Hence, we can evaluate an expression in prefix form by working from right to left. When we encounter an operator, we perform the corresponding operation with the two operands immediately to the right of this operand. Also, whenever an operation is performed, we consider the result a new operand. EXAMPLE 7 What is the value of the prefix expression + -* 2 35/ t 23 4? Solution: The steps used to evaluate this expression by working right to left, and performing operations using the operands on the right, are shown in Figure 12. The value of this expression is 3. .... Unks � We obtain the postfix form of an expression by traversing its binary tree in postorder. Expressions written in postfix form are said to be in reverse Polish notation. Expressions in reverse Polish notation are unambiguous, so parentheses are not needed. The verification ofthis is left to the reader. EXAMPLE 8 What is the postfix form of the expression ((x + y) t 2) + ((x -4)/3)? Solution: The postfix form of the expression is obtained by carrying out a postorder traversal of the binary tree for this expr ession, shown in Figure 10. This produces the postfix expression: x y + 2 t x 4 - 3 / +. .... In the postfix form of an expression, a binary operator follows its two operands. So, to evaluate an expression from its postfix form, work from left to right, carrying out operations whenever an operator follows two operands. After an operation is carried out, the result of this operation becomes a new operand. JO-3c) 1 0.3 Tree Traversal + * 2 3 5 / i 2 3 21'3 =8 + * 2 3 5 / 8 4 X /4= 2 + * 2 3 5 2 2 3=6 + 6 5 2 6-5 = I + 2 1+2=3 Value oj cxprc"lon 3 FIGURE 12 Evaluating a Prefix Expression. 4 7 2 3 * 4 t 2*3= 6 7 6 4 t 9 7-6 = 1 4 t 9 -' 14 = 1 9 -' / 9/3 = 3 3 1+3 =4 Value of expre"iOll" 4 + 9 3 3 / / + + FIGURE 13 Evaluating a Postfix Expression. EXAMPLE 9 What is the value of the postfix expression 7 2 3 * -4 t 93/+? / + 721 + Solution: The steps used to evaluate this expression by starting at the left and carrying out operations when two operands are followed by an operator are shown in Figure 13. The value of this expression is 4. -<I Rooted trees can be used to represent other types of expressions, such as those representing compound propositions and combinations of sets. In these examples unary operators, such as the negation of a proposition, occur. To represent such operators and their operands, a vertex representing the operator and a child of this vertex representing the operand are used. EXAMPLE 10 Find the ordered rooted tree representing the compound proposition (-'(p /\ q» ++ (-'P V -.q). Then use this rooted tree to find the prefix, postfix, and infix forms of this expression. JAN t.UKASIEWICZ (1878-1956) Jan Eukasiewicz was born into a Polish-speaking family in Lvov. At that time Lvov was part of Austria, but it is now in the Ukraine. His father was a captain in the Austrian army. Eukasiewicz became interested in mathematics while in high school. He studied mathematics and philosophy at the University of Lvov at both the undergraduate and graduate levels. After completing his doctoral work he became a lecturer there, and in 1911 he was appointed to a professorship. When the University of Warsaw was reopened as a Polish university in 1915, Eukasiewicz accepted an invitation to join the faculty. In 1919 he served as the Polish Minister of Education. He returned to the position of professor at Warsaw University where he remained from 1920 to 1939, serving as rector ofthe university twice. Eukasiewicz was one of the cofounders of the famous Warsaw School of Logic. He published his famous text, Elements of Mathematical Logic. in 1928. With his influence, mathematical logic was made a required course for mathematics and science undergraduates in Poland. His lectures were considered excellent, even attracting students of the humanities. Eukasiewicz and his wife experienced great suffering during World War II, which he documented in a posthumously published autobiography. After the war they lived in exile in Belgium. Fortunately, in 1949 he was offered a position at the Royal Irish Academy in Dublin. Eukasiewicz worked on mathematical logic throughout his career. His work on a three-valued logic was an important contribution to the subject. Neverthe less, he is best known in the mathematical and computer science communities for his introduction of parenthesis-free notation, now called Polish notation. 722 10 / Trees Elltra� Examples � Exercises /\ ---, /\ I p q p ---, I /\ /\ p q ---, I q v /\ ---, ---, I I p q H /� ---, v I /\ /\ ---, ---, /\ I I p q p q FIGURE 14 Constructing the Rooted Tree for a Compound Proposition. 10-40 Solution: The rooted tree for this compound proposition is constructed from the bottom up. First, subtrees for -'p and -'q are formed (where -, is considered a unary operator). Also, a subtree for p /\ q is formed. Then subtrees for -'(p /\ q) and (-,p) V (-,q) are constructed. Final ly, these two subtrees are used to form the final rooted tree. The steps of this procedure are shown in Figure 14. The prefix, postfix, and infix forms of this expression are found by traversing this rooted tree in preorder, postorder, and inorder (including parentheses), respectively. These traversals give *+ -, /\ pq V -'p-'q, pq /\ -'p-'q-' V *+, and (-'(p /\ q» *+ « -,p) v (-,q », respectively. .... Because prefix and postfix expressions are unambiguous and because they can be evaluated easily without scanning back and forth, they are used extensively in computer science. Such expressions are especially useful in the construction of compilers. In Exercises 1-3 construct the universal address system for the 3. given ordered rooted tree. Then use this to order its vertices using the lexicographic order of their labels. 1. 2. 4. Suppose that the address of the vertex v in the ordered rooted tree T is 3.4.5.2.4. a) At what level is v? b) What is the address of the parent of v? c) What is the least number of siblings v can have? d) What is the smallest possible number of vertices in T if v has this address? e) Find the other addresses that must occur. 5. Suppose that the vertex with the largest address in an 10-41 ordered rooted tree T has address 2.3.4.3.1. Is it possible to determine the number of vertices in T? 6. Can the leaves of an ordered rooted tree have the follow­ ing list of universal addresses? If so, construct such an ordered rooted tree. a) 1.1.1,1.1.2,1.2,2.1.1.1,2.1.2,2.1.3,2.2,3.1.1, 3.1.2.1,3.1.2.2,3.2 b) 1.1,1.2.1,1.2.2,1.2.3,2.1,2.2.1,2.3.1,2.3.2, 2.4.2.1,2.4.2.2,3.1,3.2.1,3.2.2 c) 1.1, 1.2.1, 1.2.2, 1.2.2.1, 1.3, 1.4,2,3.1,3.2,4.1.1.1 In Exercises 7-9 determine the order in which a preorder traversal visits the vertices of the given ordered rooted tree. 7. a 8. 9. f g b a m n 0 r s a c h p o p d q 10. In which order are the vertices of the ordered rooted tree in Exercise 7 visited using an inorder traversal? 11. In which order are the vertices ofthe ordered rooted tree in Exercise 8 visited using an inorder traversal? 10.3 Tree Traversal 723 12. In which order are the vertices of the ordered rooted tree in Exercise 9 visited using an inorder traversal? 13. In which order are the vertices of the ordered rooted tree in Exercise 7 visited using a postorder traversal? 14. In which order are the vertices of the ordered rooted tree in Exercise 8 visited using a postorder traversal? 15. In which order are the vertices ofthe ordered rooted tree in Exercise 9 visited using a postorder traversal? 16. a) Represent the expression ((x + 2) t 3) * (y - (3 +x») -5 using a binary tree. Write this expression in b) prefix notation. c) postfix notation. d) infix notation. 17. a) Represent the expressions (x + xy) + (x/y) and x + ((xy + x)/y) using binary trees. Write these expressions in b) prefix notation. c) postfix notation. d) infix notation. 18. a) Represent the compound propositions �(p /\ q) � (�p v �q) and (�p /\ (q � �p») v �q using or­ dered rooted trees. Write these expressions in b) prefix notation. c) postfix notation. d) infix notation. 19. a) Represent (A n B) -(A U (B -A)) using an ordered rooted tree. Write this expression in b) prefix notation. c) postfix notation. d) infix notation. *20. In how many ways can the string �p /\ q � �p V �q be fully parenthesized to yield an infix expression? *21. In how many ways can the string A n B -A n B -A be fully parenthe sized to yield an infix expression? 22. Draw the ordered rooted tree corresponding to each of these arithmetic expressions written in prefix notation. Then write each expression using infix notation. a) + * + - 5 3 2 1 4 b) t + 2 3 - 51 c) * / 9 3 + * 2 4 -7 6 23. What is the value of each ofthese prefix expressions? a) -* 2 / 8 4 3 b) t - * 3 3 * 4 2 5 c) + - t 3 2 t 2 3 /6 -42 d) *+3+3 t3+333 24. What is the value of each of these postfix expressions? a) 5 2 1 - - 3 1 4 ++ * b) 93/5 + 7 2 -* c) 3 2 * 2 t 5 3 -8 4/* - 25. Construct the ordered rooted tree whose preorder traver­ sal is a, b,f, c, g, h, i, d, e,j, k, I, where a has four children, 724 10 / Trees c has three children, j has two children, band e have one child each, and all other vertices are leaves. *26. Show that an ordered rooted tree is uniquely determined when a list of vertices generated by a preorder traversal of the tree and the number of children of each vertex are specified. *27. Show that an ordered rooted tree is uniquely determined when a list of vertices generated by a postorder traversal of the tree and the number of children of each vertex are specified. 28. Show that preorder traversals of the two ordered rooted trees displayed below produce the same list of vertices. Note that this does not contradict the statement in Ex­ ercise 26, because the numbers of children of internal vertices in the two ordered rooted trees differ. a c d g h b c d e a g h 29. Show that postorder traversals ofthese two ordered rooted trees produce the same list of vertices. Note that this does not contradict the statement in Exercise 27, because the numbers of children of internal vertices in the two ordered rooted trees differ. 10.4 Spanning Trees Introduction c a d f g h a c d b g 10-42 h Well-formed formulae in prefix notation over a set of sym­ bols and a set of binary operators are defined recursively by these rules: (i) if x is a symbol, then x is a well-formed formula in prefix notation; (ii) if X and Yare well-formed formulae and * is an operator, then * XY is a well-formed formula. 30. Which of these are well-formed formulae over the sym­ bols {x, y, z} and the set of binary operators {x, +, o}? a) x + +xyx b)oxyxxz c) x 0 x z x x x Y d) x + 0 x x 0 x x x *31. Show that any well-formed formula in prefix notation over a set of symbols and a set of binary operators contains ex­ actly one more symbol than the number of operators. 32. Give a definition of well-formed formulae in postfix no­ tation over a set of symbols and a set of binary operators. 33. Give six examples of well-formed formulae with three or more operators in postfix notation over the set of symbols {x, y, z} and the set of operators {+, x, o}. 34. Extend the definition of well-formed formulae in prefix notation to sets of symbols and operators where the oper­ ators may not be binary. Consider the system of roads in Maine represented by the simple graph shown in Figure lea). The only way the roads can be kept open in the winter is by frequently plowing them. The highway department wants to plow the fewest roads so that there will always be cleared roads connecting any two towns. How can this be done? DEFINITION 1 At least five roads must be plowed to ensure that there is a path between any two towns. Figure 1(b) shows one such set of roads. Note that the subgraph representing these roads is a tree, because it is connected and contains six vertices and five edges. This problem was solved with a connected subgraph with the minimum number of edges containing all vertices of the original simple graph. Such a graph must be a tree. Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G. 10-43 Etna Old Town Hampden (a) Etna Old Town Orono Hampden (b) FIGURE 1 (a) A Road System and (b) a Set of Roads to Plow. 10.4 Spanning Trees 725 a b c e __ -�f-- -___' g FIGURE 2 The Simple Graph G. d A simple graph with a spanning tree must be connected, because there is a path in the spanning tree between any two vertices. The converse is also true; that is, every connected simple graph has a spanning tree. We will give an example before proving this result. EXAMPLE 1 Find a spanning tree of the simple graph G shown in Figure 2. Solution: The graph G is connected, but it is not a tree because it contains simple circuits. Remove the edge {a, e}. This eliminates one simple circuit, and the resulting subgraph is still connected and still contains every vertex of G. Next remove the edge {e, f} to eliminate a second simple circuit. Finally, remove edge {c, g} to produce a simple graph with no simple circuits. This subgraph is a spanning tree, because it is a tree that contains every vertex of G. The sequence of edge removals used to produce the spanning tree is illustrated in Figure 3. The tree shown in Figure 3 is not the only spanning tree of G . For instance, each of the trees shown in Figure 4 is a spanning tree of G . ..... THEOREM l A simple graph is connected if and only if it has a spanning tree. a b Proof: First, suppose that a simple graph G has a spanning tree T. T contains every vertex of G. Furthermore, there is a path in T between any two of its vertices. Because T is a subgraph of G, there is a path in G between any two of its vertices. Hence, G is connected. Now suppose that G is connected. If G is not a tree, it must contain a simple circuit. Remove an edge from one of these simple circuits. The resulting sub graph has one fewer edge but still contains all the vertices of G and is connected. This subgraph is still connected because when two vertices are connected by a path containing the removed edge, they are connected by a path . not containing this edge. We can construct such a path by inserting into the original path, at c d a b c d a b c d e __ -�f-- -___ g e -----.g e -----.g f Edge removed: I a, e} le,f} Ie, g} (a) (b) (c) FIGURE 3 Producing a Spanning Tree for G by Removing Edges That Form Simple Circuits. 726 10 / Trees EXAMPLE 2 UnlS� unkS� /0-44 a b c d a b c d I I I • e g e f g f a b c d a b c d : V1 • e g e f g f FIGURE 4 Spanning Trees of G. the point where the removed edge once was, the simple circuit with this edge removed. If this subgraph is not a tree, it has a simple circuit; so as before, remove an edge that is in a simple circuit. Repeat this process until no simple circuits remain. This is possible because there are only a finite number of edges in the graph. The process terminates when no simple circuits remain. A tree is produced because the graph stays connected as edges are removed. This tree is a spanning tree because it contains every vertex of G . <I Spanning trees are important in data networking, as Example 2 shows. IP Multicasting Spanning trees play an important role in multicasting over Internet Protocol (IP) networks. To send data from a source computer to multiple receiving computers, each of which is a subnetwork, data could be sent separately to each computer. This type of networking, called unicasting, is inefficient, because many copies of the same data are transmitted over the network. To make the transmission of data to multiple receiving computers more efficient, IP multicasting is used. With IP multicasting, a computer sends a single copy of data over the network, and as data reaches intermediate routers, the data are forwarded to one or more other routers so that ultimately all receiving computers in their various subnetworks receive these data. (Routers are computers that are dedicated to forwarding IP datagrams between subnetworks in a network. In multicasting, routers use Class D addresses, each representing a session that receiving computers may join; see Example 16 in Section 5.1.) For data to reach receiving computers as quickly as possible, there should be no loops (which in graph theory terminology are circuits or cycles) in the path that data take through the network. That is, once data have reached a particular router, data should never return to this router. To avoid loops, the multicast routers use network algorithms to construct a spanning tree in the graph that has the multicast source, the routers, and the subnetworks containing receiving computers as vertices, with edges representing the links between computers and/or routers. The root of this spanning tree is the multicast source. The subnetworks containing receiving computers are leaves of the tree. (Note that subnetworks not containing receiving stations are not included in the graph.) This is illustrated in Figure 5. <l1li Depth-First Search The proof of Theorem 1 gives an algorithm for finding spanning trees by removing edges from simple circuits. This algorithm is inefficient, because it requires that simple circuits be identified. lO-45 Demo� EXAMPLE 3 Extra� ExamPles ..... IP network Multicast spanning tree (a) (b) o Router • Subnetwork ® Subnetwork with a receiving station FIGURE 5 A Multicast Spanning Tree. 10.4 Spanning Trees 727 Instead of constructing spanning trees by removing edges, spanning trees can be built up by successively adding edges. Two algorithms based on this principle will be presented here. We can build a spanning tree for a connected simple graph using depth-first search. We will form a rooted tree, and the spanning tree will be the underlying undirected graph of this rooted tree. Arbitrarily choose a vertex of the graph as the root. Form a path starting at this vertex by successively adding vertices and edges, where each new edge is incident with the last vertex in the path and a vertex not already in the path. Continue adding vertices and edges to this path as long as possible. If the path goes through all vertices of the graph, the tree consisting of this path is a spanning tree. However, if the path does not go through all vertices, more vertices and edges must be added. Move back to the next to last vertex in the path, and, if possible, form a new path starting at this vertex passing through vertices that were not already visited. If this cannot be done, move back another vertex in the path, that is, two vertices back in the path, and try again. Repeat this procedure, beginning at the last vertex visited, moving back up the path one vertex at a time, forming new paths that are as long as possible until no more edges can be added. Because the graph has a finite number of edges and is connected, this process ends with the production of a spanning tree. Each vertex that ends a path at a stage of the algorithm will be a leaf in the rooted tree, and each vertex where a path is constructed starting at this vertex will be an internal vertex. The reader should note the recursive nature of this procedure. Also, note that if the vertices in the graph are ordered, the choices of edges at each stage of the procedure are all determined when we always choose the first vertex in the ordering that is available. However, we will not always explicitly order the vertices of a graph. Depth-first search is also called backtracking, because the algorithm returns to vertices previously visited to add paths. Example 3 illustrates backtracking. Use depth-first search to find a spanning tree for the graph G shown in Figure 6. Solution: The steps used by depth-first search to produce a spanning tree of G are shown in Figure 7. We arbitrarily start with the vertex f. A path is built by successively adding edges incident with vertices not already in the path, as long as this is possible. This produces a path f, g, h, k, j (note that other paths could have been built). Next, backtrack to k. There is no path 728 10 / Trees JO-46 f f f f f e • ! ;{ a d j c· __ ----� ---4� --�� --� h k j j j a j b a b g (a) (b) (c) (d) (e) FIGURE 6 The Graph G. FIGURE 7 Depth-First Search of G. beginning at k containing vertices not already visited. So we backtrack to h. Form the path h, i. Then backtrack to h, and then to f. From f build the path f, d, e, c, a. Then backtrack to c and form the path c, b. This produces the spanning tree. � The edges selected by depth-first search of a graph are called tree edges. All other edges of the graph must connect a vertex to an ancestor or descendant of this vertex in the tree. These edges are called back edges. (Exercise 39 asks for a proof of this fact.) EXAMPLE 4 In Figure 8 we highlight the tree edges found by depth-first search starting at vertex f by showing them with heavy colored lines. The back edges (e, f) and (f, h) are shown with thinner black lines. � We have explained how to find a spanning tree of a graph using depth-first search. However, our discussio n so far has not brought out the recursive nature of depth-first search. To help make the recursive nature of the algorithm clear, we need a little terminolo gy. We say that we explore from a vertex v when we carry out the steps of depth-first search beginning when v is added to the tree and ending when we have backtracked back to v for the last time. The key observation needed to understand the recursive nature of the algorithm is that when we add an edge connecting a vertex v to a vertex w, we finish exploring from w before we return to v to complete exploring from v. In Algorithm I we construct the spanning tree of a graph G with vertices VI, ••• , Vn by first selecting the vertex VI to be the root. We initially set T to be the tree with just this one vertex. At each step we add a new vertex to the tree T together with an edge from a vertex already in T to this new vertex and we explore from this new vertex. Note that at the completion of the algorithm, T contains no simple circuits because no edge is added that connects a vertex already in the tree. Moreover, T remains connected as it is built. (These last two observations can be easily proved via mathematical induction.) Because G is connected, every vertex in G is visited by the algorithm and is added to the tree (as the reader should verify). It follows that T is a spanning tree of G . a d j g FIGURE 8 The Tree Edges and Back Edges of the Depth-First Search in Example 4. JO-47 Demo� unkS� EXAMPLE S Extra� Examples � ALGOR ITHM 1 Depth-First Search. procedure DFS(G: connected graph with vertices VI, V2, •.. , vn) T := tree consisting only ofthe vertex VI visit(vI) procedure visit(v: vertex of G) for each vertex w adjacent to v and not yet in T begin add vertex wand edge {v, w} to T visit(w) end 10.4 Spanning Trees 729 We now analyze the computational complexity of the depth-first search algorithm. The key obserVation is that for each vertex v, the procedure visit(v) is called when the vertex v is first encountered in the search and it is not called again. Assuming that the adjacency lists for G are available (see Section 9.3), no computations are required to find the vertices adjacent to v. As we follow the steps of the algorithm, we examine each edge at most twice to determine whether to add this edge and one of its endpoints to the tree. Consequently, the procedure DFS constructs a spanning tree using O(e), or O(n2), steps where e and n are the number of edges and vertices in G, respectively. [Note that a step involves examining a vertex to see whether it is already in the spanning tree as it is being built and adding this vertex and the corresponding edge if the vertex is not already in the tree. We have also made use ofthe inequality e S n(n -1)/2, which holds for any simple graph.] Depth-first search can be used as the basis for algorithms that solve many different problems. For example, it can be used to find paths and circuits in a graph, it can be used to determine the connected components of a graph, and it can be used to find the cut vertices of a connected graph. As we will see, depth-first search is the basis of backtracking techniques used to search for solutions of computationally difficult problems. (See [GrYe99], [Ma89], and [CoLeRiStO I] for a discussion of algorithms based on depth-first search.) Breadth-First Search We can also produce a spanning tree of a simple graph by the use of breadth-first search. Again, a rooted tree will be constructed, and the underlying undirected graph of this rooted tree forms the spanning tree. Arbitrarily choose a root from the vertices of the graph. Then add all edges incident to this vertex. The new vertices added at this stage become the vertices at level 1 in the spanning tree. Arbitrarily order them. Next, for each vertex at levell, visited in order, add each edge incident to this vertex to the tree as long as it does not produce a simple circuit. Arbitrarily order the children of each vertex at level 1. This produces the vertices at level 2 in the tree. Follow the same procedure until all the vertices in the tree have been added. The procedure ends because there are only a finite number of edges in the graph. A spanning tree is produced because we have produced a tree containing every vertex of the graph. An example of breadth-first search is given in Example 5. Use breadth-first search to find a spanning tree for the graph shown in Figure 9. Solution: The steps of the breadth-first search procedure are shown in Figure 10. We choose the vertex e to be the root. Then we add edges incident with all vertices adjacent to e, so edges from e to b, d, j, and i are added. These four vertices are at level 1 in the tree. Next, add the edges from these vertices at level 1 to adjacent vertices not already in the tree. Hence, the edges 730 10 / Trees a b c d .-_----=<et__---'f=-+---__ g m k FIGURE 9 A Graph G. 10-48 from b to a and c are added, as are edges from d to h, from f to j and g, and from ito k. The new vertices a, c, h, j, g, and k are at level 2. Next, add edges from these vertices to adjacent vertices not already in the graph. This adds edges from g to I and from k to m. ... We describe breadth-first search in pseudocode as Algorithm 2. In this algorithm, we assume the vertices of the connected graph G are ordered as VI, V2, ..• , Vn. In the algorithm we use the term "process" to describe the procedure of adding new vertices, and corresponding edges, to the tree adjacent to the current vertex being processed as long as a simple circuit is not produced. ALGORITHM 2 Breadt h-First Search. procedure BFS (G: connected graph with vertices VI, V2 ••••• vn) T := tree consisting only of vertex VI e • L := empty list put VI in the list L of unprocessed vertices while L is not empty begin remove the first vertex, V, from L for each neighbor w of V end if w is not in L and not in T then begin add w to the end ofthe list L add wand edge {v, w} to T end e � b d f d e a c h g FIGURE 10 Breadth-First Search of G. j k a e d m 10-49 10.4 Spanning Trees 731 We now analyze the computational complexity of breadth-first search. For each vertex v in the graph we examine all vertices adjacent to v and we add each vertex not yet visited to the tree T. Assuming we have the adjacency lists for the graph available, no computation is required to determine which vertices are adjacent to a given vertex. As in the analysis of the depth-first search algorithm, we see that we examine each edge at most twice to determine whether we should add this edge and its endpoint not already in the tree. It follows that the breadth-first search algorithm uses O(e) or O(n2) steps. Backtracking Applications There are problems that can be solved only by performing an exhaustive search of all possible solutions. One way to search systematically for a solution is to use a decision tree, where each internal vertex represents a decision and each leaf a possible solution. To find a solution via backtracking, first make a sequence of decisions in an attempt to reach a solution as long as this is possible. The sequence of decisions can be represented by a path in the decision tree. Once it is known that no solution can result from any further sequence of decisions, backtrack to the parent of the current vertex and work toward a solution with another ser ies of decisions, if this is possible. The procedure continues until a solution is found, or it is established that no solution exists. Examples 6 to 8 illustrate the usefulness of backtracking. EXAMPLE 6 Graph Colorings How can backtracking be used to decide whether a graph can be colored using n colors? EXAM PLE 7 unkS� Solution: We can solve this problem using backtracking in the following way. First pick some vertex a and assign it color 1. Then pick a second vertex b, and if b is not adjacent to a, assign it color 1. Otherwise, assign color 2 to b. Then go on to a third vertex c. Use color 1, if possible, for c. Otherwise use color 2, if this is possible. Only if neither color 1 nor color 2 can be used should color 3 be used. Continue this process as long as it is possible to assign one of the n colors to each additional vertex, always using the first allowable color in the list. If a vertex is reached that cannot be colored by any of the n colors, backtrack to the last assignment made and change the coloring of the last vertex colored, if possible, using the next allowable color in the list. If it is not possible to change this coloring, backtrack fa rther to previous assignments, one step back at a time, until it is possible to change a coloring of a vertex. Then continue assigning colors of additional vertices as long as possible. If a coloring using n colors exists, backtracking will produce it. (Unfortunately this procedure can be extremely inefficien t.) In particular, consider the problem of coloring the graph shown in Figure 11 with three colors. The tree shown in Figure 11 illustrates how backtracking can be used to construct a 3-coloring. In this procedure, red is used first, then blue, and finally green. This simple example can obviously be done without backtracking, but it is a good illustration of the technique. In this tree, the initial path from the root, which represents the assignment of red to a, leads to a coloring with a red, b blue, c red, and d green. It is impossible to color e using any of the three colors when a, b, c, and d are colored in this way. So, backtrack to the parent of the vertex representing this coloring. Because no other color can be used for d, backtrack one more level. Then change the color of c to green. We obtain a coloring of the graph by then assigning red to d and green to e. <III! The n-Queens Problem The n-queens problem asks how n queens can be placed on an n x n chessboard so that no two queens can attack one another. How can backtracking be used to solve the n-queens problem? Solution: To solve this problem we must find n positions on an n x n chessboard so that no two of these positions are in the same row, same column, or in the same diagonal [a diagonal consists of all positions (i, j) with i + j = m for some m, or i -j = m for some m]. We will 732 10 / Trees a red I a red, b blue e d IS2J /� a b c a red, b blue, c red, d green a red, b bl ue, c green, d red a red, b bl ue, c green, d red, e green FIGURE 11 Coloring a Graph Using Backtracking. 10-50 use backtracking to solve the n-queens problem. We start with an empty chessboard. At stage k + 1 we attempt putting an additional queen on the board in the (k + 1 )st column, where there are already queens in the first k columns. We examine squares in the (k + l)st column starting with the square in the first row, looking for a position to place this queen so that it is not in the same row or on the same diagonal as a queen already on the board. (We already know it is not in the same column.) If it is impossible to find a position to place the queen in the (k + 1 )st column, backtrack to the placement of the queen in the kth column, and place this queen in the next allowable row in this column, if such a row exists. If no such row exists, backtrack further. In particular, Figure 12 displays a backtracking solution to the four-queens problem. In this solution, we place a queen in the first row and column. Then we put a queen in the third row of the second column. However, this makes it impossible to place a queen in the third column. So we backtrack and put a queen in the fourth row of the second column. When we do this, we can place a queen in the second row of the third column. But there is no way to add a queen to the fourth column. This shows that no solution results when a queen is placed in the first row and column. We backtrack to the empty chessboard, and place a queen in the second row of the first column. This leads to a solution as shown in Figure 12. -<Ill EXAMPLE 8 Sums of Subsets Consider this problem. Given a set of positive integers Xl, X2, .•• , Xn, find a subset of this set of integers that has M as its sum. How can backtracking be used to solve this problem? Solution: We start with a sum with no terms. We build up the sum by successively adding terms. An integer in the sequence is included if the sum remains less than M when this integer is added to the sum. If a sum is reached such that the addition of any term is greater than M, backtrack by dropping the last term of the sum. Figure 13 displays a backtracking solution to the problem of finding a subset of {3l, 27,15,11,7, 5} with the sum equal to 39. -<Ill Depth-First Search in Directed Graphs We can easily modify both depth-first search and breadth-first search so that they can run given a directed graph as input. However, the output will not necessarily be a spanning tree, but rather a spanning forest. In both algorithms we can add an edge only when it is directed away from 10-51 10.4 Spanning Trees 733 / � X X / � X X X X X X X X X X X X X X X X represents a queen X FIGURE 12 A Backtracking Solution ofthe Four-Queens Problem. the vertex that is being visited and to a vertex not yet added. If at a stage of either algorithm we find that no edge exists starting at a vertex already added to one not yet added, the next vertex added by the algorithm becomes the root of a new tree in the spanning forest. This is illustrated in Example 9. EXAM PLE 9 What is the output of depth-first search given the graph G shown in Figure 14(a) as input? Solution: We begin the depth-first search at vertex a and add vertices b, c, and g and the corresponding edges where we are blocked. We backtrack to c but we are still blocked, and then o Sum=O /� {31 } Sum = 31 /� {31,7} {31, 5} Sum = 38 Sum = 36 {27} Sum = 27 /� {27, 11} Sum = 38 {27,7} Sum = 34 / {27, 7, 5} Sum = 39 FIGURE 13 Find a Sum Equal to 39 Using Backtracking. 734 10/ Trees a b c d a b f g c e h g j k (a) FIGURE 14 Depth-First Search of a Directed Graph. 10-52 d ei h e k j (b) backtrack to b, where we add vertices f and e and the corresponding edges. Backtracking takes us all the way back to a. We then start a new tree at d and add vertices h, I, k, and j and the corresponding edges. We backtrack to k, then I, then h, and back to d. Finally, we start a new tree at i, completing the depth-first search. The output is shown in Figure l4(b). � Depth-first search in directed graphs is the basis of many algorithms (see [GrY e99], [Ma89], and [CoLeRiStO l]). It can be used to determine whether a directed graph has a circuit, it can be used to carry out a topological sort of a graph, and it can also be used to find the strongly connected components of a directed graph. We conclude this section with an application of depth-first search and breadth-first search to search engines on the Web. EXAMPLE 10 Web Spiders To index websites, search engines such as Google and Yahoo systematically explore the Web starting at known sites. These search engines use programs called Web spiders (or crawlers or bots) to visit websites and analyze their contents. Web spiders use both depth-first searching and breadth-first searching to create indices. As described in Example 8 in Section 9.1, Web pages and links between them can be modeled by a directed graph called the Web graph. Web pages are represented by vertices and links are represented by directed edges. Using depth-first search, an initial Web page is selected, a link is followed to a second Web page (ifthere is such a link), a link on the second Web page is followed to a third Web page, ifthere is such a link, and so on, until a page with no new links is found. Backtracking is then used to examine links at the previous level to look for new links, and so on. (Because of practical limitations, Web spiders have limits to the depth they search in depth-first search.) Using breadth-first search, an initial Web page is selected and a link on this page is followed to a second Web page, then a second link on the initial page is followed (if it exists), and so on, until all links of the initial page have been followed. Then links on the pages one level down are followed, page by page, �W� � Exercises 1. How many edges must be removed from a connected graph with n vertices and m edges to produce a spanning tree? In Exercises 2-6 find a spanning tree for the graph shown by removing edges in simple circuits. 2. a b c 10-53 3. af-----��----� d g-------+----�. 4. a b c h@ d g f e s. a b k 6. a t.::-----__ k b c 7. Find a spanning tree for each of these graphs. a) K5 b) K4,4 c) K1,6 d) Q3 e) C5 1) W5 In Exercises 8-10 draw all the spanning trees of the given simple graphs. 8. a b c 9. a b f g I I � I I c den 10.4 Spanning Trees 735 10. c �:----���e----�j d *11. How many different spanning trees does each of these simple graphs have? a) K3 b) K4 c) K2,2 d) C5 *12. How many nonisomorphic spanning trees does each of these simple graphs have? a) K3 b) K4 c) K5 In Exercises 13-15 use depth-first search to produce a span­ ning tree for the given simple graph, Choose a as the root of this spanning tree and assume that the vertices are ordered alphabetically. 13. 14. 15. a e h 1> <J I : b f g j b c d e f a j g k m n g h a q b __ --__ -------- ��------ �._--_.--___ s c..- --..... d n m 16. Use breadth-first search to produce a spanning tree for each of the simple graphs in Exercises 13-15. Choose a as the root of each spanning tree. 17. Use depth-first search to find a spanning tree of each of these graphs. a) W6 (see Example 7 of Section 9.2), starting at the ver- tex of degree 6 b) K5 c) K3,4, starting at a vertex of degree 3 d) Q3 18. Use breadth-first search to find a spanning tree of each of !he graphs in Exercise 17. 19. Describe the trees produced by breadth-first search and depth-first search of the wheel graph Wn, starting at the 736 10 / Trees vertex of degree n, where n is an integer with n :::: 3. (See Example 7 of Section 9.2.) Justify your answers. 20. Describe the trees produced by breadth-first search and depth-first search of the complete graph Kn, where n is a positive integer. Justify your answers. 21. Describe the trees produced by breadth-first search and depth-first search of the complete bipartite graph Km•n, starting at a vertex of degree m, where m and n are posi­ tive integers. Justify your answers. 22. Explain how breadth-first search and how depth-first search can be used to determine whether a graph is bipartite. 23. Suppose that an airline must reduce its flight schedule to save money. If its original routes are as illustrated here, which flights can be discontinued to retain service be­ tween all pairs of cities (where it may be necessary to combine flights to fly from one city to another)? Bangor Seattle �--:::::" Boston San Dallas 24. When must an edge of a connected simple graph be in every spanning tree for this graph? 25. Which connected simple graphs have exactly one span­ ning tree? 26. Explain how breadth-first search or depth-first search can be used to order the vertices of a connected graph. *27. Show that the length of the shortest path between ver­ tices v and u in a connected simple graph equals the level number of u in the breadth-first spanning tree of G with root v. 28. Use backtracking to try to find a coloring of each of the graphs in Exercises 7-9 of Section 9.8 using three colors. 29. Use backtracking to solve the n-queens problem for these values ofn. a) n = 3 b) n = 5 c) n = 6 30. Use backtracking to find a subset, if it exists, of the set {27, 24, 19, 14, 11, 8} with sum a) 20. b) 41. c) 60. 31. Explain how backtracking can be used to find a Hamilton path or circuit in a graph. 32. a) Explain how backtracking can be used to find the way out of a maze, given a starting position and the exit position. Consider the maze divided into positions, where at each position the set of available moves in­ cludes one to four possibilities (up, down, right, left). b) Find a path from the starting position marked by X to the exit in this maze. 10-54 Exit A spanning forest of graph G is a forest that contains every vertex of G such that two vertices are in the same tree of the forest when there is a path in G between these two vertices. 33. Show that every finite simple graph has a spanning forest. 34. How many trees are in the spanning forest of a graph? 35. How many edges must be removed to produce the span­ ning forest of a graph with n vertices, m edges, and c connected components? 36. Devise an algorithm for constructing the spanning for­ est of a graph based on deleting edges that form simple circuits. 37. Devise an algorithm for constructing the spanning forest of a graph based on depth-first searching. 38. Devise an algorithm for constructing the spanning forest of a graph based on breadth-first searching. 39. Let G be a connected graph. Show that if T is a span­ ning tree of G constructed using depth-first search, then an edge of G not in T must be a back edge, that is, it must connect a vertex to one of its ancestors or one of its descendants in T. 40. Let G be a connected graph. Show that if T is a spanning tree of G constructed using breadth-first search, then an edge of G not in T must connect vertices at the same level or at levels that differ by 1 in this spanning tree. 41. For which graphs do depth-first search and breadth-first search produce identical spanning trees no matter which vertex is selected as the root of the tree? Justify your answer. 42. Use Exercise 39 to prove that if G is a connected, simple graph with n vertices and G does not contain a simple path oflength k then it contains at most (k -l)n edges. 43. Use mathematical induction to prove that breadth-first search visits vertices in order of their level in the resulting spanning tree. 44. Use pseudocode to describe a variation of depth-first search that assigns the integer n to the nth vertex visited in the search. Show that this numbering corresponds tf) the numbering of the vertices created by a preorder traversal of the spanning tree. 45. Use pseudocode to describe a variation of breadth-first search that assigns the integer m to the mth vertex visited in the search. *46. Suppose that G is a directed graph and T is a span­ ning tree constructed using breadth-first search. Show that every edge of G connects two vertices at the same level, a vertex to a vertex at one level lower, or a vertex to a vertex at some higher level. /0-55 47. Show that if G is a directed graph and T is a spanning tree constructed using depth-first search, then every edge not in the spanning tree is a forward edge connecting an ancestor to a descendant, a back edge connecting a descendant to an ancestor, or a cross edge connecting a vertex to a vertex in a previously visited subtree. *48. Describe a variation of depth-first search that assigns the smallest available positive integer to a vertex when the algorithm is totally finished with this vertex. Show that in this numbering, each vertex has a larger number than its children and that the children have increasing numbers from left to right. Let TJ and T2 be spanning trees of a graph. The distance be­ tween TJ and T2 is the number of edges in TJ and T2 that are not common to TJ and T2. 49. Find the distance between each pair of spanning trees shown in Figures 3( c) and 4 of the graph G shown in Figure 2. *50. Suppose that TJ, T2, and T3 are spanning trees of the sim­ ple graph G. Show that the distance between TJ and T3 does not exceed the sum of the distance between TJ and T2 and the distance between T2 and T3. **51. Suppose that TJ and T2 are spanning trees of a simple graph G. Moreover, suppose that eJ is an edge in TJ that is not in T2. Show that there is an edge e2 in T2 that is not 10.5 Minimum Spanning Trees Introduction 10.5 Minimum Spanning Trees 737 in TJ such that TJ remains a spanning tree if eJ is removed from it and e2 is added to it, and T2 remains a spanning tree if e2 is removed from it and eJ is added to it. *52. Show that it is possible to find a sequence of spanning trees leading from any spanning tree to any other by suc­ cessively removing one edge and adding another. A rooted spanning tree of a directed graph is a rooted tree containing edges of the graph such that every vertex of the graph is an endpoint of one of the edges in the tree. 53. For each ofthe directed graphs in Exercises 18-23 of Sec­ tion 9.5 either find a rooted spanning tree ofthe graph or determine that no such tree exists. *54. Show that a connected directed graph in which each ver­ tex has the same in-degree and out-degree has a rooted spanning tree. [Hint: Use an Euler circuit.] *55. Give an algorithm to build a rooted spanning tree for con­ nected directed graphs in which each vertex has the same in-degree and out-degree. *56. Show that if G is a directed graph and T is a spanning tree constructed using depth-first search, then G contains a circuit if and only if G contains a back edge (see Exercise 47) relative to the spanning tree T. *57. Use Exercise 56 to construct an algorithm for determining whether a directed graph contains a circuit. unkS� A company plans to build a communications network connecting its five computer centers. Any pair of these centers can be linked with a leased telephone line. Which links should be made to ensure that there is a path between any two computer centers so that the total cost of the network is minimized? We can model this problem using the weighted graph shown in Figure I, where vertices represent computer centers, edges represent possible leased lines, and the weights on edges are the monthly lease rates of the lines represented by the edges. We can solve this problem by finding a spanning tree so that the sum of the weights of the edges of the tree is minimized. Such a spanning tree is called a minimum spanning tree. $2000 San Francisco L�� __ !.G�---::�::::?�$Jo(�:=/i FIGURE 1 A We ighted Graph Showing Monthly Lease Costs for Lines in a Computer Network. 738 10 / Trees DEFINITION 1 jO-56 Algorithms for Minimum Spanning Trees A wide variety of problems are solved by finding a spanning tree in a weighted graph such that the sum of the weights of the edges in the tree is a minimum. A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible sum of weights of its edges. We will present two algorithms for constructing minimum spanning trees. Both proceed by successively adding edges of smallest weight from those edges with a specified property that have not already been used. Both are greedy algorithm s. Recall from Section 3.1 that a greedy algorithm is a procedure that makes an optimal choice at each of its steps. Optimizing at each step does not guarantee that the optimal overall solution is produced. However, the two algorithms presented in this section for constructing minimum spanning trees are greedy algorithms that do produce optimal solutions. The first algorithm that we will discuss was given by Robert Prim in 1957, although the basic ideas of this algorithm have an earlier origin. To carry out Prim's algorithm, begin by choosing any edge with smallest weight, putting it into the spanning tree. Successively add to the tree edges of minimum weight that are incident to a vertex already in the tree and not forming a simple circuit with those edges already in the tree. Stop when n -I edges have been added. Later in this section, we will prove that this algorithm produces a minimum spanning tree for any connected weighted graph. Algorithm I gives a pseudocode description of Prim's algorithm. ALGORI THM 1 Prim's Algorithm. procedure Prim(G: weighted connected undirected graph with n vertices) T := a minimum-weight edge for i := 1 to n -2 begin e := an edge of minimum weight incident to a vertex in T and not forming a simple circuit in T if added to T T := T with e added end {T is a minimum spanning tree of G) Note that the choice of an edge to add at a stage of the algorithm is not determined when there is more than one edge with the same weight that satisfies the appropriate criteria. We need to order the edges to make the choices determini stic. We will not worry about this in the remainder of the section. Also note that there may be more than one minimum spanning tree for a given ROBERT CLAY PRIM (BORN 1921) Robert Prim, born in Sweetwater, Texas, received his B.S. in electrical engineering in 1941 and his Ph.D. in mathematics from Princeton University in 1949. He was an engineer at the General Electric Company from 1941 until 1944, an engineer and mathematician at the United States Naval Ordnance Lab from 1944 until 1949, and a research associate at Princeton University from 1948 until 1949. Among the other positions he has held are director of mathematics and mechanics research at Bell Telephone Laboratories from 1958 until 1961 and vice president of research at Sandia Corporation. He is currently retired. 10-57 $2000 10.5 Minimum Spanning Trees 739 San Francisco $1200 $900 Denver a 2 b 3 c d 3 1 2 5 Choice Edge Co';t e 4 f 3 g 3 h 1 (Chicago, Atlanta} $ 700 2 (Atlanta, New York} $ 800 4 2 4 3 3 (Chicago, San Francisco} $1200 4 ( San Franc isco, Denver} $ 900 3 3 1 Total' $3600 j k FIGURE 2 A Minimum Spanning Tree for the Weighted Graph in Figure 1. FIGURE 3 A Weighted Graph. connected weighted simple graph. (See Exercise 9.) Examples 1 and 2 illustrate how Prim's algorithm is used. EXAMPLE 1 Use Prim's algorithm to design a minimum-cost communications network connecting all the computers represented by the graph in Figure 1. Solution: We solve this problem by finding a minimum spanning tree in the graph in Figure 1. Prim's algorithm is carried out by choosing an initial edge of minimum weight and successively adding edges of minimum weight that are incident to a vertex in the tree and that do not form simple circuits. The edges in color in Figure 2 show a minimum spanning tree produced by Prim's algorithm, with the choice made at each step displayed. .... EXAMPLE 2 Use Prim's algorithm to find a minimum spanning tree in the graph shown in Figure 3. Solution: A minimum spanning tree constructed using Prim's algorithm is shown in Figure 4. The successive edges chosen are displayed. .... The second algorithm we will discuss was discovered by Joseph Kruskal in 1956, although the basic ideas it uses were described much earlier. To carry out KruskaI's algorithm, choose an edge in the graph with minimum weight. Successively add edges with minimum weight that do not form a simple circuit with those edges already chosen. Stop after n -1 edges have been selected. The proof that Kruskal's algorithm produces a minimum spanning tree for every connected weighted graph is left as an exercise at the end ofthis section. Pseudocode for Kruskal 's algorithm is given in Algorithm 2. JOSEPH BERNARD KRUSKAL (BORN 1928) Joseph Kruskal, born in New York City, attended the Univer­ sity of Chicago and received his Ph.D. from Princeton University in 1954. He was an instructor in mathematics at Princeton and at the University of Wisconsin, and later he was an assistant professor at the University of Michigan. In 1959 he became a member of the technical staff at Bell Laboratori es, a position he continues to hold. His current research interests include statistical linguistics and psychometrics. Besides his work on mini­ mum spanning trees, Kruskal is also known for contributions to multidimensional scaling. Kruskal discovered his algorithm for producing minimum spanning trees when he was a second-year graduate student. He was not sure his 2 � -page paper on this subject was worthy of publication, but was convinced by others to submit it. 740 10 / Trees EXAMPLE 3 Extra� Examples "" Choice Edge Weight a 2 b 3 e d {b, f} 1 2 {a, bl 2 3 If, j} 2 3 1 2 5 4 {a, e} 3 5 { i, j I 3 h 6 If, gl 3 7 {e, g} 2 4 f 3 g 3 e 8 {e, dl 1 4 2 4 3 9 {g, h} 3 10 {h, II 3 3 3 1 11 {k, I I j k Total: 24 (a) (b) FIGURE 4 A Minimum Spanning Tree Produced Using Prim's Algorithm. ALGOR ITHM 2 Kruskal's Algorithm. procedure Kruskal(G: weighted connected undirected graph with n vertices) T := empty graph for i := 1 to n -1 begin e := any edge in G with smallest weight that does not form a simple circuit when added to T T := T with e added end {T is a minimum spanning tree ofG} 10-58 The reader should note the difference between Prim's and Kruskal's algorithm s. In Prim's algorithm edges of minimum weight that are incident to a vertex already in the tree, and not forming a circuit, are chosen; whereas in Kruskal's algorithm edges of minimum weight that are not necessarily incident to a vertex already in the tree, and that do not form a circuit, are chosen. Note that as in Prim's algorithm, if the edges are not ordered, there may be more than one choice for the edge to add at a stage of this procedure. Consequently, the edges need to be ordered for the procedure to be deterministic. Example 3 illustrates how Kruskal's algorithm is used. Use Kruskal's algorithm to find a minimum spanning tree in the weighted graph shown in Figure 3. Solution: A minimum spanning tree and the choices of edges at each stage ofKruskal's algorithm are shown in Figure 5. .. HISTORICAL NOTE Joseph Kruskal and Robert Prim developed their algorithms for constructing minimum spanning trees in the mid-1950s. However, they were not the first people to discover such algorithms. For example, the work of the anthropologist Jan Czekanowski, in 1909, contains many of the ideas required to find minimum spanning trees. In 1926, Otakar Boruvka described methods for constructing minimum spanning trees in work relating to the construction of electric power networks. 10-59 a 2 3 I e 4 f 4 2 3 b 3 e 2 3 g 4 3 j k (a) 3 I 10.5 Minimum Spanning Trees Choice Edge Weight d {e, dl 2 Ik, II 3 Ib, f} I 5 4 {e, gl 2 5 la, bl 2 h 6 If, j} 2 7 Ib, el 3 8 {j, k I 3 3 9 {g, hI 3 10 { i, j I 3 11 la, el 3 Total: 24 (b) FIGURE 5 A Minimum Spanning Tree Produced by Kruskal's Algorithm. 741 We will now prove that Prim's algorithm produces a minimum spanning tree of a connected weighted graph. Proof: Let G be a connected weighted graph. Suppose that the successive edges chosen by Prim's algorithm are el, e2, ... , en-I. Let S be the tree with el, e2, ... , en-l as its edges, and let Sk be the tree with el, e2, ... ,ek as its edges. Let T be a minimum spanning tree of G containing the edges el, e2, ... , ek, where k is the maximum integer with the property that a minimum spanning tree exists containing the first k edges chosen by Prim's algorithm. The theorem follows if we can show that S = T. Suppose that S #-T, so that k < n -1. Consequently, T contains el, e2, ... , ek, but not ek+ 1 . Consider the graph made up of T together with ek+ 1. Because this graph is connected and has n edges, too many edges to be a tree, it must contain a simple circuit. This simple circuit must contain ek+l because there was no simple circuit in T. Furthermore, there must be an edge in the simple circuit that does not belong to Sk+l because Sk+l is a tree. By starting at an endpoint of ek+ 1 that is also an endpoint of one of the edges el, ... , ek, and following the circuit until it reaches an edge not in Sk+l, we can find an edge e not in Sk+l that has an endpoint that is also an endpoint of one of the edges el, e2, ... , ek. By deleting e from T and adding ek+l, we obtain a tree T' with n -1 edges (it is a tree because it has no simple circuits). Note that the tree T' contains el, e2, ... , ek, ek+l. Furthermore, because ek +l was chosen by Prim's algorithm at the (k + l)st step, and e was also available at that step, the weight of ek+1 is less than or equal to the weight of e. From this observation it follows that T' is also a minimum spanning tree, because the sum of the weights of its edges does not exceed the sum ofthe weights of the edges of T. This contradicts the choice of k as the maximum integer such that a minimum spanning tree exists containing el, ... , ek. Hence, k = n -1, and S = T. It follows that Prim's algorithm produces a minimum spanning tree. <l It can be shown (see [CoLeRiStOl]) that to find a minimum spanning tree of a graph with e edges and v vertices, Kruskal's algorithm can be carried out using a (e log e) opera­ tions and Prim's algorithm can be carried out using a(e log v) operations. Consequently, it is preferable to use Kruskal's algorithm for graphs that are sparse, that is, where e is very small compared to C(v, 2) = v(v -1)/2, the total number of possible edges in an undirected graph with v vertices. Otherwise, there is little difference in the complexity of these two algorithms. 742 10/ Trees Exercises 1. The roads represented by this graph are all unpaved. The lengths ofthe roads between pairs oftowns are represented by edge weights. Which roads should be paved so that there is a path of paved roads between each pair of towns so that a minimum road length is paved? (Note: These towns are in Nevada.) Manhattan Beatty In Exercises 2-4 use Prim's algorithm to find a minimum span­ ning tree for the given weighted graph. 2'4�3 � 1 c d d f g 4 h 2 4. 2 b 2 c 3 3 g 2 h 2 3 4 3 2 j 4 k 3 4 3 2 2 n 0 2 10-60 5. Use Kruskal's algorithm to design the communications network described at the beginning of the section. 6. Use Kruskal's algorithm to find a minimum spanning tree for the weighted graph in Exercise 2. 7. Use Kruskal's algorithm to find a minimum spanning tree for the weighted graph in Exercise 3. 8. Use Kruskal's algorithm to find a minimum spanning tree for the weighted graph in Exercise 4. 9. Find a connected weighted simple graph with the fewest edges possible that has more than one minimum spanning tree. 10. A minimum spanning forest in a weighted graph is a spanning forest with minimal weight. Explain how Prim's and Kruskal's algorithms can be adapted to construct min­ imum spanning forests. A maximum spanning tree of a connected weighted undi­ rected graph is a spanning tree with the largest possible weight. 11. Devise an algorithm similar to Prim's algorithm for constructing a maximum spanning tree of a connected weighted graph. 12. Devise an algorithm similar to Kruskal's algorithm for constructing a maximum spanning tree of a connected weighted graph. :' 13. Find a maximum spanning tree for the weighted graph in Exercise 2. 14. Find a maximum spanning tree for the weighted graph in Exercise 3. 15. Find a maximum spanning tree for the weighted graph in Exercise 4. 16. Find the second least expensive communications network connecting the five computer centers in the problem posed at the beginning of the section. *17. Devise an algorithm for finding the second shortest span­ ning tree in a connected weighted graph. *18. Show that an edge with smallest weight in a connected weighted graph must be part of any minimum spanning tree. 19. Show that there is a unique minimum spanning tree in a connected weighted graph if the weights of the edges are all different. 20. Suppose that the computer network connecting the cities in Figure I must contain a direct link between New York and Denver. What other links should be included so that there is a link between every two computer centers and the cost is minimized? 21. Find a spanning tree with minimal total weight contain­ ing the edges {e, i} and {g, k} in the weighted graph in Figure 3. 22. Describe an algorithm for finding a spanning tree with minimal weight containing a specified set of edges in a connected weighted undirected simple graph. 10-61 23. Express the algorithm devised in Exercise 22 in pseudocode. 8ollin's algorithm produces a minimum spanning tree from a connected weighted simple graph G = (V, E) by succes­ sively adding groups of edges. Suppose that the vertices in V are ordered. This produces an ordering of the edges where {uo, vol precedes {UI, vd ifuo precedes UI orifuo = UI and Vo precedes VI. The algorithm begins by simultaneously choos­ ing the edge ofleast weight incident to each vertex. The first edge in the ordering is taken in the case of ties. This pro­ duces a graph with no simple circuits, that is, a forest of trees (Exercise 24 asks for a proof of this fact). Next, simultaneously choose for each tree in the forest the shortest edge between a vertex in this tree and a vertex in a different tree. Again the first edge in the ordering is chosen in the case of ties. (This pro­ duces a graph with no simple circuits containing fewer trees than were present before this step; see Exercise 24.) Continue the process of simultaneously adding edges connecting trees until n -1 edges have been chosen. At this stage a minimum spanning tree has been constructed. '24. Show that the addition of edges at each stage of Sollin' s algorithm produces a forest. Key Terms and Results TERMS tree: a connected undirected graph with no simple circuits forest: an undirected graph with no simple circuits rooted tree: a directed graph with a specified vertex, called the root, such that there is a unique path to any other vertex from this root subtree: a subgraph of a tree that is also a tree parent of v in a rooted tree: the vertex u such that (u, v) is an edge of the rooted tree child of a vertex v in a rooted tree: any vertex with v as its parent sibling of a vertex v in a rooted tree: a vertex with the same parent as v ancestor of a vertex v in a rooted tree: any vertex on the path from the root to v descendant of a vertex v in a rooted tree: any vertex that has v as an ancestor internal vertex: a vertex that has children leaf: a vertex with no children level of a vertex: the length of the path from the root to this vertex height of a tree: the largest level ofthe vertices of a tree m-ary tree: a tree with the property that every internal vertex has no more than m children full m-ary tree: a tree with the property that every internal vertex has exactly m children binary tree: an m-ary tree with m = 2 (each child may be designated as a left or a right child of its parent) ordered tree: a tree in which the children of each internal vertex are linearly ordered Key Tenns and Results 743 25. Use Sollin's algorithm to produce a minimum spanning tree for the weighted graph shown in a) Figure I. b) Figure 3. *26. Express Sollin's algorithm in pseudocode. **27. Prove that Sollin 's algorithm produces a minimum span­ niJJgjree in a connected undirected weighted graph. *28. Show that the first step of Sollin's algorithm produces a forest containing at least r n 121 edges. *29. Show that ifthere are r trees in the forest at some intenne­ diate step of Soli in's algorithm, then at least rrl21 edges are added by the next iteration of the algorithm. *30. Show that no more than Ln 12k J trees remain after the first step of Sollin's algorithm has been carried out and the second step of the algorithm has been carried out k -1 times. *31. Show that Sollin's algorithm requires at most log n iter­ ations to produce a minimum spanning tree from a con­ nected undirected weighted graph with n vertices. 32. Prove that Kruskal's algorithm produces minimum span­ ning trees. balanced tree: a tree in which every leaf is at level h or h -1, where h is the height of the tree binary search tree: a binary tree in which the vertices are la­ beled with items so that a label of a vertex is greater than the labels of all vertices in the left subtree of this vertex and is less than the labels of all vertices in the right subtree of this vertex decision tree: a rooted tree where each vertex represents a possible outcome of a decision and the leaves represent the possible solutions prefix code: a code that has the property that the code of a character is never a prefix of the code of another character minmax strategy: the strategy where the first player and sec­ ond player move to positions represented by a child with maximum and minimum value, respectively value of a vertex in a game tree: for a leaf, the payoff to the first player when the game tenninates in the position repre­ sented by this leaf; for an internal vertex, the maximum or minimum of the values of its children, for an internal vertex at an even or odd level, respectively tree traversal: a listing of the vertices of a tree preorder traversal: a listing of the vertices of an ordered rooted tree defined recursive ly-the root is listed, followed by the first subtree, followed by the other subtrees in the order they occur from left to right inorder traversal: a listing ofthe vertices of an ordered rooted tree defined recursive ly-the first subtree is listed, followed by the root, followed by the other subtrees in the order they occur from left to right postorder traversal: a listing of the vertices of an ordered rooted tree defined recursively-the subtrees are listed in 744 10 / Trees the order they occur from left to right, followed by the root infix notation: the form of an expression (including a full set of parenthe ses) obtained from an inorder traversal of the binary tree representing this expression prefix (or Polish) notation: the form of an expression ob­ tained from a preorder traversal of the tree representing this expression postfix (or reverse Polish) notation: the form of an expression obtained from a postorder traversal of the tree representing this expression spanning tree: a tree containing all vertices of a graph minimum spanning tree: a spanning tree with smallest pos­ sible sum of weights of its edges greedy algorithm: an algorithm that optimizes by making the optimal choice at each step RESULTS A graph is a tree if and only if there is a unique simple path between any of its vertices. A tree with n vertices has n -1 edges. A full m-ary tree with i internal vertices has mi + 1 vertices. Review Questions 1. a) Define a tree. b) Define a forest. 2. Can there be two different simple paths between the ver­ tices of a tree? 3. Give at least three examples of how trees are used in modeling. 4. a) Define a rooted tree and the root of such a tree. b) Define the parent of a vertex and a child of a vertex in a rooted tree. c) What are an internal vertex, a leaf, and a subtree in a rooted tree? d) Draw a rooted tree with at least 10 vertices, where the degree of each vertex does not exceed 3. IdentifY the root, the parent of each vertex, the children of each vertex, the internal vertices, and the leaves. 5. a) How many edges does a tree with n vertices have? b) What do you need to know to determine the number of edges in a forest with n vertices? 6. a) Define a full m-ary tree. b) How many vertices does a full m-ary tree have if it has i internal vertices? How many leaves does the tree have? 7. a) What is the height of a rooted tree? b) What is a balanced tree? c) How many leaves can an m-ary tree of height h have? 8. a) What is a binary search tree? b) Describe an algorithm for constructing a binary search tree. 10-62 The relationships among the numbers of vertices, leaves, and internal vertices in a full m-ary tree (see Theorem 4 in Section 10.1) There are at most mh leaves in an m-ary tree of height h. If an m-ary tree has I leaves, its height h is at least rlogm 11. If the tree is also full and balanced, then its height is rlogm 11. Huffman coding: a procedure for constructing an optimal binary code for a set of symbols, given the frequencies of these symbols depth-first search, or backtracking: a procedure for con­ structing a spanning tree by adding edges that form a path until this is not possible, and then moving back up the path until a vertex is found where a new path can be formed breadth-first search: a procedure for constructing a spanning tree that successively adds all edges incident to the last set of edges added, unless a simple circuit is formed Prim's algorithm: a procedure for producing a minimum span­ ning tree in a weighted graph that successively adds edges with minimal weight among all edges incident to a vertex already in the tree such that no edge produces a simple circuit when it is added Kruskal's algorithm: a procedure for producing a minimum spanning tree in a weighted graph that successively adds edges of least weight that are not already in the tree such that no edge produces a simple circuit when it is added c) Form a binary search tree for the words vireo, warbler. egret, grosbeak, nuthatch, and kingfisher. 9. a) What is a prefix code? b) How can a prefix code be represented by a binary tree? 10. a) Define preorder, inorder, and postorder tree traversal. b) Give an example of preorder, postorder, and inorder traversal of a binary tree of your choice with at least 12 vertices. 11. a) Explain how to use preorder, inorder, and postorder traversals to find the prefix, infix, and postfix forms of an arithmetic expression. b) Draw the ordered rooted tree that represents «x - 3) + «x/4) + (x -y) t 3». c) Find the prefix and postfix forms of the expression in part (b), 12. Show that the number of comparisons used by a sorting algorithm is at least rlog n fl. 13. a) Describe the Huffman coding algorithm for construct­ ing an optimal code for a set of symbols, given the frequency of these symbols. b) Use Huffman coding to find an optimal code for these symbols and frequenc ies: A: 0.2, B: 0.1, C: 0.3, D: 0.4. 14. Draw the game tree for nim if the startin� position consists of two piles with one and four stones, respectively. Who wins the game if both players follow an optimal strategy? 15. a) What is a spanning tree of a simple graph? 10-63 b) Which simple graphs have spanning trees? c) Describe at least two different applications that require that a spanning tree of a simple graph be found. 16. a) Describe two different algorithms for finding a span­ ning tree in a simple graph. b) Illustrate how the two algorithms you described in (a) can be used to find the spanning tree of a simple graph, using a graph of your choice with at least eight vertices and 15 edges. 17. a) Explain how backtracking can be used to determine whether a simple graph can be colored using n colors. b) Show, with an example, how backtracking can be used to show that a graph with a chromatic number equal Supplementar y Exercises * 1. Show that a simple graph is a tree if and only if it contains no simple circuits and the addition of an edge connecting two nonadjacent vertices produces a new graph that has exactly one simple circuit (where cir­ cuits that contain the same edges are not considered different). * 2. How many nonisomorphic rooted trees are there with six vertices? 3. Show that every tree with at least one edge must have at least two pendant vertices. 4. Show that a tree with n vertices that has n - I pendant vertices must be isomorphic to K I.n-I. 5. What is the sum of the degrees of the vertices of a tree with n vertices? *6. Suppose that dl, d2, ••• , dn are n positive integers with sum 2n -2. Show that there is a tree that has n vertices such that the degrees of these vertices are dl, d2, ••• , dn• 7. Show that every tree is a planar graph. 8. Show that every tree is bipartite. 9. Show that every forest can be colored using two colors. A B-tree of degree k is a rooted tree such that all its leaves are at the same level, its root has at least two and at most k children unless it is a leaf, and every internal vertex other than the root has at least rk/21, but no more than k, children. Computer files can be accessed efficiently when B-trees are used to represent them. 10. Draw three different B-trees of degree 3 with height 4. '11. Give an upper bound and a lower bound for the number of leaves in a B-tree of degree k with height h. '12. Give an upper bound and a lower bound for the height of a B-tree of degree k with n leaves. The binomial trees Bj, i = 0, 1,2, ... , are ordered rooted trees defined recursively : Basis step: The binomial tree Bo is the tree with a single vertex. Recursive step: Let k be a nonnegative integer. To con­ struct the binomial tree B k+ I, add a copy of B k to a second Supplementary Exercises 745 to 4 cannot be colored with three colors, but can be colored with four colors. 18. a) What is a minimum spanning tree of a connected weighted graph? b) Describe at least two different applications that require that a minimum spanning tree of a connected weighted graph be found. 19. a) Describe Kruskal's algorithm and Prim's algorithm for finding minimum spanning trees. b) Illustrate how Kruskal's algorithm and Prim's algo­ rithm are used to find a minimum spanning tree, using a weighted graph with at least eight vertices and 15 edges. copy of Bk by adding an edge that makes the root of the first copy of Bk the leftmost child of the second copy of Bk. 13. Draw Bk for k = 0, 1,2,3,4. 14. How many vertices does Bk have? Prove that your answer is correct. 15. Find the height of Bk. Prove that your answer is correct. 16. How many vertices are there in Bk at depth j, where o � j � k? JustifY your answer. 17. What is the degree of the root of B k? Prove that your answer is correct. 18. Show that the vertex oflargest degree in Bk is the root. A rooted tree T is called an Sk-tree if it satisfies this recursive definition. It is an So-tree if it has one vertex. For k > 0, T is an Sk-tree if it can be built from two Sk-I -trees by making the root of one the root of the Sk-tree and making the root of the other the child ofthe root of the first Sk-tree. 19. Draw an Sk-tree for k = 0, 1,2,3,4. 20. Show that an Sk-tree has 2k vertices and a unique vertex at level k. This vertex at level k is called the handle. *21. Suppose that T is an Sk-tree with handle v. Show that T can be obtained from disjoint trees To, TI, ... , Tk-I, where v is not in any of these trees, where Tj is an Sj-tree for i = 0, I, ... , k -I, by connecting v to ro and rj to rHI fori =0, 1, ... ,k-2. The listing of the vertices of an ordered rooted tree in level order begins with the root, followed by the vertices at level I from left to right, followed by the vertices at level 2 from left to right, and so on. 22. List the vertices of the ordered rooted trees in Figures 3 and 9 of Section 10.3 in level order. 23. Devise an algorithm for listing the vertices of an ordered rooted tree in level order. *24. Devise an algorithm for determining if a set of universal adclresses can be the addresses of the leaves of a rooted tree. 25. Devise an algorithm for constructing a rooted tree from the universal addresses of its leaves. 746 10 / Trees A cut set of a graph is a set of edges such that the removal of these edges produces a subgraph with more connected com­ ponents than in the original graph, but no proper subset ofthis set of edges has this property. 26. Show that a cut set of a graph must have at least one edge in common with any spanning tree of this graph. A cactus is a connected graph in which no edge is in more than one simple circuit not passing through any vertex other than its initial vertex more than once or its initial vertex other than at its terminal vertex (where two circuits that contain the same edges are not considered different). 27. Which of these graphs are cacti? a) b) c) 28. Is a tree necessarily a cactus? 29. Show that a cactus is formed if we add a circuit con­ taining new edges beginning and ending at a vertex of a tree. 1<30. Show that if every circuit not passing through any vertex other than its initial vertex more than once in a connected graph contains an odd number of edges, then this graph must be a cactus. A degree-constrained spanning tree of a simple graph G is a spanning tree with the property that the degree of a vertex /0-64 in this tree cannot exceed some specified bound. Degree­ constrained spanning trees are useful in models of transport a­ tion systems where the number of roads at an intersection is limited, models of communications networks where the num­ ber of links entering a node is limited, and so on. In Exercises 31-33 find a degree-constrained spanning tree of the given graph where each vertex has degree less than or equal to 3, or show that such a spanning tree does not exist. 31. a .... __ ...... __ .. c d ___ -�'---.... I 33. a b c d ...----.-- .... 1 h g 32. h b a c g e 1 d 34. Show that a degree-constrained spanning tree of a sim­ ple graph in which each vertex has degree not exceeding 2 consists of a single Hamilton path in the graph. 35. A tree with n vertices is called graceful if its vertices can be labeled with the integers 1, 2, ... , n such that the absolute values of the difference of the labels of adja­ cent vertices are all different. Show that these trees are graceful. a) ee--<.I---<el-_. b) •• - "r>---· -... d) A caterpillar is a tree that contains a simple path such that every vertex not contained in this path is adjacent to a vertex in the path. 36. Which of the graphs in Exercise 35 are caterpillars? 37. How many nonisomorphic caterpillars are there with six vertices? **38. a) Prove or disprove that all trees whose edges form a single path are graceful. b) Prove or disprove that all caterpillars are graceful. 0-65 9. Suppose that in a long bit string the frequency of occur­ rence of a 0 bit is 0.9 and the frequency of a 1 bit is 0.1 and bits occur independently. a) Construct a Huffman code for the four blocks of two bits, 00, 01, 10, and II. What is the average number of bits required to encode a bit string using this code? b) Construct a Huffman code for the eight blocks of three bits. What is the average number of bits required to encode a bit string using this code? o. Suppose that G is a directed graph with no circuits. De­ scribe how depth-first search can be used to carry out a topological sort of the vertices of G. 1. Suppose that e is an edge in a weighted graph that is in­ cident to a vertex v such that the weight of e does not exceed the weight of any other edge incident to v. Show that there exists a minimum spanning tree containing this edge. 2. Three couples arrive at the bank of a river. Each of the wives is jealous and does not trust her husband when he is with one of the other wives (and perhaps with other people), but not with her. How can six people cross to the other side of the river using a boat that can hold no more than two people so that no husband is alone :omputer Projects \Trite programs with these input and output. 1. Given the adjacency matrix of an undirected simple graph, determine whether the graph is a tree. 2. Given the adjacency matrix of a rooted tree and a vertex in the tree, find the parent, children, ancestors, descendants, and level of this vertex. 3. Given the list of edges of a rooted tree and a vertex in the tree, find the parent, children, ancestors, descendants, and level of this vertex. 4. Given a list of items, construct a binary search tree con­ taining these items. 5. Given a binary search tree and an item, locate or add this item to the binary search tree. 6. Given the ordered list of edges of an ordered rooted tree, find the universal addresses of its vertices. 7. Given the ordered list of edges of an ordered rooted tree, list its vertices in preorder, inorder, and postorder. 8. Given an arithmetic expression in prefix form, find its value. 9. Given an arithmetic expression in postfix form, find its value. o. Given the frequency of symbols, use Huffman coding to find an optimal code for these symbols. Computer Projects 747 with a woman other than his wife? Use a graph theory model. *43. Show that if no two edges in a weighted graph have the same weight, then the edge with least weight incident to a vertex v is included in every minimum spanning tree. 44. Find a minimum spanning tree of each of these graphs where the degree of each vertex in the spanning tree does not exceed 2. f 2 g 11. Given an initial position in the game of nim, determine an optimal strategy for the first player. 12. Given the adjacency matrix of a connected undirected simple graph, find a spanning tree for this graph using depth-first search. 13. Given the adjacency matrix of a connected undirected simple graph, find a spanning tree for this graph using breadth-first search. 14. Given a set of positive integers and a positive integer N, use backtracking to find a subset of these integers that have N as their sum. *15. Given the adjacency matrix of an undirected simple graph, use backtracking to color the graph with three colors, if this is possible. *16. Given a positive integer n, solve the n-queens problem using backtracking. 17. Given the list of edges and their weights of a weighted undirected connected graph, use Prim's algorithm to find a minimum spanning tree of this graph. 18. Given the list of edges and their weights of a weighted undirected connected graph, use Kruskal's algorithm to find a minimum spanning tree of this graph. 748 to / Trees 10-66 Computations and Explorations Use a compu tational program or programs you have written to do these exercises. 1. Display all trees with six vertices. 2. Display a full set of nonisomorphic trees with seven vertices. *3. Construct a Huffman code for the symbols with ASCII codes given the frequency oftheir occurrence in represen­ tative input. 4. Compute the number of different spanning trees of Kn for n = 1,2,3,4,5,6. Conjecture a formula for the number of such spanning trees whenever n is a positive integer. 5. Compare the number of comparisons needed to sort lists of n elements for n = 100, 1000, and 10,000, where the elements are randomly selected positive integers, using the Writing Projects Respond to these with essays using outside sources. 1. Explain how Cayley used trees to enumerate the number of certain types of hydrocar bons. 2. Define AVL-trees (sometimes also known as height­ balanced trees). Describe how and why AVL-trees are used in a variety of different algorithm s. 3. Define quad trees and explain how images can be repre­ sented using them. Describe how images can be rotated, scaled, and translated by manipulating the corresponding quad tree. 4. Define a heap and explain how trees can be turned into heaps. Why are heaps useful in sorting? 5. Describe dynamic algorithms for data compression based on letter frequencies as they change as characters are suc­ cessively read, such as adaptive Huffman coding. 6. Explain how alpha-beta pruning can be used to simplify the computation of the value ofa game tree. 7. Describe the techniques used by chess-pla ying programs such as Deep Blue. 8. Define the type of graph known as a mesh of trees. selection sort, the insertion sort, the merge sort, and the quick sort. 6. Compute the number of different ways n queens can be arranged on an n x n chessboa rd so that no two queens can attack each other for all positive integers n not exceeding 10. * 7. Find a minimum spanning tree of the graph that connects the capital cities of the 50 states in the United States to each other where the weight of each edge is the distance between the cities. 8. Draw the complete game tree for a game of checkers on a 4 x 4 board. Explain how this graph is used in applications to very large system integration and parallel computing. 9. Discuss the algorithms used in IP multicasting to avoid loops between routers. 10. Describe an algorithm based on depth-first search for find­ ing the articulation points of a graph. 11. Describe an algorithm based on depth-first search to find the strongly connected components of a directed graph. 12. Describe the search techniques used by the crawlers and spiders in different search engines on the Web. 13. Describe an algorithm for finding the minimum spanning tree of a graph such that the maximum degree of any ver­ tex in the spanning tree does not exceed a fixed constant k. 14. Compare and contrast some of the most important sorting algorithms in terms of their complexity and when they are used. 15. Discuss the history and origins of algorithms for con­ structing minimum spanning trees. 16. Describe algorithms for producing random trees. CHAPTER 11.1 Boolean Functions 11.2 Representing Boolean Functions 11.3 Logic Gates 11.4 Minimization of Circuits Boolean Algebra T he circuits in computers and other electronic devices have inputs, each of which is either .ll. a 0 or aI, and produce ou tputs that are also Os and 1 s. Circuits can be constructed using any basic element that has two different states. Such elements include switches that can be in either the on or the off position and optical devices that can either be lit or unlit. In 1938 Claude Shannon showed how the basic rules oflogic, first given by George Boole in 1854 in his The Laws of Thought, could be used to design circuits. These rules form the basis for Boolean algebra. In this chapter we develop the basic properties of Boolean algebra. The operation ofa circuit is defined by a Boolean function that specifies the value of an output for each set of inputs. The first step in constructing a circuit is to represent its Boolean function by an expression built up using the basic operations of Boolean algebra. We will provide an algorithm for producing such expressions. The expression that we obtain may contain many more operations than are necessary to represent the function. Later in the chapter we will describe methods for finding an expression with the minimum number of sums and products that represents a Boolean function. The procedures that we will develop, Karnaugh maps and the Quine-McCluskey method, are important in the design of efficient circuits. 11.1 Boolean Functions II-I Introduction Boolean algebra provides the operations and the rules for working with the set to, I}. Electronic and optical switches can be studied using this set and the rules of Boolean algebra. The three operations in Boolean algebra that we will use most are complementation, the Boolean sum, and the Boolean product. The complement of an element, denoted with a bar, is defined by 0 = 1 and T = O. The Boolean sum, denoted by + or by OR, has the following values: 1 + 1 = 1, 1 +0 = 1, 0+1 = 1, 0+0 = O. The Boolean product, denoted by . or by AND, has the following values: 1·1 = 1, 1· 0 = 0, 0·1 = 0, 0·0=0. When there is no danger of confusion, the symbol· can be deleted, just as in writing algebraic products. Unless parentheses are used, the rules of precedence for Boolean operators are: first, all complements are computed, followed by Boolean products, followed by all Boolean sums. This is illustrated in Example 1. EXAM PLE 1 Find the value of 1 . 0 + (0 + 1). Soitltion: Using the definitions of complementation, the Boolean sum, and the Boolean product, it follows that 1·0+ (0 + 1) = 0 + T =0+0 =0 . 749 750 II/Boolean Algebra 1/-2 The complement, Boolean sum, and Boolean product correspond to the logical operators, -., v, and /\, respective ly, where 0 corresponds to F (false) and 1 corresponds to T (true). Equal­ ities in Boolean algebra can be directly translated into equivalences of compound propositions. Converse ly, equivalences of compound propositions can be translated into equalities in Boolean algebra. We will see later in this section why these translations yield valid logical equivalences and identities in Boolean algebra. Example 2 illustrates the translation from Boolean algebra to propositional logic. EXAMPLE 2 Translate 1 ·0 + (0 + 1) = 0, the equality found in Example 1, into a logical equivalence. Solution: We obtain a logical equivalence when we translate each 1 into aT, each 0 into a F, each Boolean sum into a disjunction, each Boolean product into a conjunction, and each complementation into a negation. We obtain (T /\ F) v -.(T v F) == F. Example 3 illustrates the translation from propositional logic to Boolean algebra. EXAM PLE 3 Translate the logical equivalence (T /\ T) v -.F == T into an identity in Boolean algebra. unkS� Solution: We obtain an identity in Boolean algebra when we translate each T into ai, each F into a 0, each disjunction into a Boolean sum, each conjunction into a Boolean product, and each negation into a complementation. We obtain (1 . I) + 0 = 1. Boolean Expres sions and Boolean Functions Let B = {O, I}. Then Bn = {(x" X2, ••• , xn) I Xi E B for 1 � i � n} is the set of all possible n-tuples of Os and Is. The variable x is called a Boolean variable ifit assumes values only from B, that is, if its only possible values are 0 and 1. A function from Bn to B is called a Boolean function of degree n. CLAUDE ELWOOD SHANNON (1916-200 I) Claude Shannon was born in Petoskey , Michigan, and grew up in Gaylord, Michigan. His father was a businessman and a probate judge, and his mother was a language teacher •. and a high school principal. Shannon attended the University of Michigan, graduating in 1936. He continued his studies at M.L T., where he took the job of maintaining the differential analyzer, a mechanical computing device consisting of shafts and gears built by his professor, Vannevar Bush. Shannon's master's thesis, written in 1936, studied the logical aspects ofthe differential analyzer. This master's thesis presents the first application of Boolean algebra to the design of switching circuits; it is perhaps the most famous master's thesis of the . twentieth century. He received his Ph.D. from M.LT. in 1940. Shannon joined Bell Laboratories in 1940, where he worked on transmitting data efficiently. He was one of the first people to use bits to represent information. At Bell Laboratories he worked on determining the amount of traffic that telephone lines can carry. Shannon made many fundamental contributions to information theory. In the early 1950s he was one of the founders of the study of artificial intelligence. He joined the M.LT. faculty in 1956, where he continued his study of information theory. Shannon had an unconventional side. He is credited with inventing the rocket-powered Frisbee. He is also famous for riding a unicycle down the hallways of Bell Laboratories while juggling four balls. Shannon retired when he was 50 years old, publishing papers sporadically over the following 10 years. In his later years he concentrated on some pet projects, such as building a motorized pogo stick. One interesting quote from Shannon, published in Omni Magazine in 1987, is "I visualize a time when we will be to robots what dogs are to humans. And I am rooting for the machines." 11-3 11.1 Boolean Functions 751 EXAMPLE 4 The function F(x, y) = xy from the set of ordered pairs of Boolean variables to the set to, I} is a Boolean function of degree 2 with F(I, 1) = 0, F(I, 0) = I, F(O, 1) = 0, and F(O, 0) = 0. We display these values of F in Table 1. .... TABLE 1 x I I 0 0 y F(x,y) I 0 0 I I 0 0 0 Boolean functions can be represented using expressions made up from variables and Boolean operations. The Boolean expressions in the variables XJ , X2, ••• ,Xn are defined recursively as 0,1, XJ, X2, ••• , Xn are Boolean expressions; if EJ and E2 are Boolean expressions, then E 1. (EJE2), and (EJ + E2) are Boolean expressions. Each Boolean expression represents a Boolean function. The values of this function are obtained by substituting ° and I for the variables in the expression. In Section 11.2 we will show that every Boolean function can be represented by a Boolean expression. EXAMPLE 5 Find the values of the Boolean function represented by F(x, y, z) = xy + z. EXAM PLE 6 IlO III Solution: The values of this function are displayed in Table 2. Note that we can represent a Boolean function graphically by distinguishing the vertices of the n-cube that correspond to the n-tuples of bits where the function has value 1. The function F(x, y, z) = xy + z from B3 to B from Example 3 can be represented by distin­ guishing the vertices that correspond to the five 3-tuples (1,1,1), (1,1,0), (1,0,0), (0,1,0), and (0,0,0), where F(x, y, z) = 1, as shown in Figure 1. These vertices are displayed using solid black circles. .... Boolean functions F and G of n variables are equal if and only if F(bl. b2, ••. , bn) = G(bJ, b2, ••• , bn) whenever bJ, b2, ••• , bn belong to B. Two different Boolean expressions that OIO�--+-� OIl represent the same function are called equivalent. For instance, the Boolean expressions xy, 000 001 xy + 0, and xy . 1 are equivalent. The complement of the Boolean function F is the function F, where F(xJ, ... ,xn) = F(xJ, ... , xn). Let F and G be Boolean functions of degree n. The Boolean sum F + G and the Boolean product F G are defined by FIGURE 1 (F + G)(xJ, ... , xn) = F(xJ, ... , xn) + G(xJ, ... , xn), (FG)(xJ, ... , xn) = F(xJ, ... , xn)G(xJ, ... , xn). TABLE 2 x y z xy z F(x,y,z) = xy + Z I I I I 0 I I I 0 I I I I 0 I 0 0 0 I 0 0 0 I 1 0 I I 0 0 0 0 1 0 0 I I 0 0 I 0 0 0 0 0 0 0 I I 752 II/Boolean Algebra 11-4 TABLE 3 The Boolean Functions of Degree Two. x y FI F2 F3 F4 Fs F6 F, Fa F, FlO FII FI2 FI3 FI4 FI5 FI6 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 0 1 1 1 . 1 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 A Boolean function of degree two is a function from a set with four elements, namely, pairs of elements from B = {O, I}, to B, a set with two elements. Hence, there are 16 different Boolean functions of degree two. In Table 3 we display the values of the 16 different Boolean functions of degree two, labeled F1, F2, ••• , F16• EXAMPLE 7 How many different Boolean functions of degree n are there? Solution: From the product rule for counting, it follows that there are 2n different n-tuples of Os and 1 s. Because a Boolean function is an assignment of 0 or 1 to each of these 2n different n-tuples, the product rule shows that there are 22" different Boolean functions of degree n. .... Table 4 displays the number of different Boolean functions of degrees one through six. The number of such functions grows extremely rapidly. Identities of Boolean Algebra There are many identities in Boolean algebra. The most important of these are displayed in Table 5. These identities are particularly useful in simplifying the design of circuits. Each of the identities in Table 5 can be proved using a table. We will prove one of the distributive laws in this way in Example 8. The proofs of the remaining properties are left as exercises for the reader. EXAMPLE 8 Show that the distributive law xCV + z) = xy + xz is valid. Solution: The verification of this identity is shown in Table 6. The identity holds because the last two columns of the table agree. .... TABLE4 The Number of Boolean Functions of Degree ". Degree Number 1 4 2 16 3 256 4 65,536 5 4,294,967,296 6 18,446,744,073,709,551,616 11-5 TABLE 5 Boolean Identities. Identity x=x x+x=x x·x =x x+O=x x·l =x x+l= 1 x ·0=0 x+y=y+x xy=yx x+(y+z)=(x+y)+z x(yz) = (xy)x x + yz = (x + y)(x + z) x(y+z)=xy+xz (�y) :;:::x+y (x + y) =xy x +xy =x x(x + y) = x x+x=l xx=O Name Law of the double complement Idempotent laws Identity laws Domination laws Commutative laws Associative laws Distributive laws � M9f§�'� laws Absorp�Pfll �ws Unit property Zero property 11.1 Boolean Functions 753 The reader should compare the Boolean identities in Table 5 to the logical equivalences in Table 6 of Section 1.2 and the set identities in Table 1 in Section 2.2. All are special cases of the same set of identities in a more abstract structure . Each collection of identities can be obtained by making the appropriate translations. For example, we can transform each of the identities in Ta­ ble 5 iqto a IQgical equivalence by changing each Boolean variable into a propositional variable, �!lch 0 �nto a F, each 1 into a T, each Boolean sum into a disjunction, each Boolean product ,nto a conjunction, and each complementation into a negation, as we illustrate in Example 9. TABLE 6 Verifying One of the Distributive Laws. x y z y+z xy xz x(y +z) xy+xz 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 0 1 1 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 754 II/Boolean Algebra 11-6 EXAMPLE 9 Translate the distributive law x + yz = (x + y)(x + z) in Table 5 into a logical equivalence. Solution: To translate a Boolean identity into a logical equivalence, we change each Boolean variable into a propositional variable. Here we will change the Boolean variables x, y, and z into the propositional variables p, q, and r. Next, we change each Boolean sum to a disjunction and each Boolean product into a conjunction. (Note that 0 and 1 do not appear in this identity and complementation also does not appear.) This transforms the Boolean identity into the logical equivalence p v (q /\ r) == (p V q) /\ (p V r). This logical equivalence is one of the distributive laws for propositional logic in Table 6 in Section 1.2. � Identities in Boolean algebra can be used to prove further identities. We demonstrate this in Example 10. EXAMPLE 10 Prove the absorption law x(x + y) = x using the other identities of Boolean algebra shown in Table 5. (This is called an absorption law because absorbing x + y into x leaves x unchange d.) Exam: � Solution: The steps used to derive this identity and the law used in each step follow: x(x + y) = (x + O)(x + y) Identity law for the Boolean sum Duality = X + 0 . y Distributive law of the Boolean sum over the Boolean product = x + Y . 0 Commutative law for the Boolean product = X + 0 Domination law for the Boolean product = x. Identity law for the Boolean sum The identities in Table 5 come in pairs (except for the law of the double complement and the unit and zero properties). To explain the relationship between the two identities in each pair we use the concept of a dual. The dual of a Boolean expression is obtained by interchanging Boolean sums and Boolean products and interchanging Os and 1 s. EXAMPLE 11 Find the duals ofx(y + 0) and x ·1 + (y + z). Solution: Interchanging . signs and + signs and interchanging Os and 1 s in these expressions produces their duals. The duals are x + (y ·1) and (X + O)(yz), respectively. � The dual of a Boolean function F represented by a Boolean expression is the function represented by the dual of this expression. This dual function, denoted by Fd, does not depend on the particular Boolean expression used to represent F. An identity between functions represented by Boolean expressions remains valid when the duals of both sides of the identity are taken. (See Exercise 30 for the reason this is true.) This result, called the duality principle, is useful for obtaining new identities. EXAMPLE 12 Construct an identity from the absorption law x(x + y) = x by taking duals. Solution: Taking the duals of both sides of this identity produces the identity x + xy = x, which is also called an absorption law and is shown in Table 5. � 1/-7 11.1 Boolean Functions 755 The Abstract Definition of a Boolean Algebra In this section we have focused on Boolean functions and expressions. However, the results we have established can be translated into results about propositions or results about sets. Because of this, it is useful to define Boolean algebras abstractly. Once it is shown that a particular structure is a Boolean algebra, then all results established about Boolean algebras in general apply to this particular structure. Boolean algebras can be defined in several ways. The most common way is to specify the properties that operations must satisfy, as is done in Definition 1. DEFINITION 1 A Boolean algebra is a set B with two binary operations V and /\, elements 0 and 1, and a unary operation -such that these properties hold for all x, y, and z in B: x vO =x} x /\ 1 = x Identity laws xVx= I} x /\ x = 0 . Complement laws (x V y) V Z = x V (y V Z)} Associative laws (x /\ y) /\ z = x /\ (y /\ z) x V y = y V x} Commutative laws x/\y=y/\x x V (y /\ z) = (x V y) /\ (x V Z)} Distributive laws x /\ (y V z) = (x /\ y) V (x /\ z) Using the laws given in Definition 1, it is possible to prove many other laws that hold for every Boolean algebra, such as idempotent and domination laws. (See Exercises 35-42.) From our previous discussion, B = {O, I} with the OR and AND operations and the com­ plement operator, satisfies all these properties. The set of propositions in n variables, with the V and /\ operators, F and T, and the negation operator, also satisfies all the properties of a Boolean algebra, as can be seen from Table 6 in Section 1.2. Similarly, the set of subsets of a universal set U with the union and intersection operations, the empty set and the universal set, and the set complementation operator, is a Boolean algebra as can be seen by consulting Table 1 in Section 2.2. So, to establish results about each of Boolean expressions, propositions, and sets, we need only prove results about abstract Boolean algebras. Boolean algebras may also be defined using the notion of a lattice, discussed in Chapter 8. Recall that a lattice L is a partially ordered set in which every pair of elements x, y has a least upper bound, denoted by lub(x, y) and a greatest lower bound denoted by glb(x, y). Given two elements x and y of L, we can define two' operations V and /\ on pairs of elements of L by x V y = lub(x, y) and x /\ y = glb(x, y). For a lattice L to be a Boolean algebra as specified in Definition I, it must have two properties. First, it must be complemented. For a lattice to be complemented it must have a least element 0 and a greatest element 1 and for every element x of the lattice there must exist an element x such that x V x = 1 and x /\ x = O. Second, it must be distributive. This means that for every x,y, andz inL,x V (y /\ z) = (x V y) /\ (x V z) and x /\ (y V z) = (x /\ y) V (x /\ z). Showing that a complemented, distributive lattice is a Boolean algebra is left as Exercise 39 at the end of this section. 756 II/Boolean Algebra Exercises 1. Find the values of these expressions. a) 1·0 b) 1 + 1 c) 0·0 d) (1 + 0) 2. Find the values, if any, of the Boolean variable x that satisfy these equations. a) x·l=O b) x+x=O c) X· 1 = x d) X· x = 1 3. a) Show that (1 . 1) + (0 . I + 0) = 1. b) Translate the equation in part (a) into a propositional equivalence by changing each 0 to a F, each 1 to a T, each Boolean sum into a disjunction, each Boolean product into a conjunction, each complementation into a negation, and the equals sign to a propositional equivalence sign. 4. a) Show that (1 . 0) + (1 ·0) = 1. b) Translate the equation in part (a) into a propositional equivalence by changing each 0 to a F, each I to a T, each Boolean sum into a disjunction, each Boolean product into a conjunction, each complementation into a negation, and the equals sign to a propositional equivalence sign. 5. Use a table to express the values of each of these Boolean functions. a) F(x,y,z) =xy b) F(x,y,z) = x + yz c) F(x, y, z) = xy + (xyz) d) F(x,y,z)=x(yz+yZ) 6. Use a table to express the values of each of these Boolean functions. a) F(x,y,z)=z b) F(x,y,z)=xy+yz c) F(x, y, z) = xyz + (xyz) d) F(x,y,z)=y(xz+xZ) 7. Use a 3-cube Q3 to represent each of the Boolean func­ tions in Exercise 5 by displaying a black circle at each vertex that corresponds to a 3-tuple where this function has the value 1. 8. Use a 3-cube Q3 to represent each of the Boolean func­ tions in Exercise 6 by (tisplaying a black circle at each vertex that corresponds to a 3-tuple where this function has the value 1. 9. What values of the Boolean variables x and y satisfy xy =x+y? 10. How many different Boolean functions are there of de­ gree 7? 11. Prove the absorption law x + xy = x using the other laws in Table 5. ts"12. Show that F(x, y, z) = xy + xz + yz has the value 1 if and only if at least two of the variables x, y, and z have the value 1. 13. Show that xy + yz + xz = xy + yz + xz. Exercises 14-23 deal with the Boolean algebra to, I} with addition, multiplication, and complement defined at the 11-8 beginning of this section. In each case, use a table as in Example 8. 14. Verify the law of the double complement. 15. Verify the idempotent laws. 16. Verify the identity laws. 17. Verify the domination laws. 18. Verify the commutative laws. 19. Verify the associative laws. 20. Verify the first distributive law in Table 5. 21. Verify De Morgan' s laws. 22. Verify the unit property. 23. Verify the zero property. The Boolean operator EB, called the XOR operator, is defined by 1 EB 1 = 0,1 EB 0 = 1,0 EB 1 = 1, and 0 EB 0 = o. 24. Simplify these expressions. a) x EB 0 b) x EB 1 c)xEBx d)xEBx 25. Show that these identities hold. a) x EBy = (x + y)(xy) b) x EB Y = (xY) + (Xy) 26. Show that x EB y = y EB x . 27. Prove or disprove these equalities. a) x EB (y EBz) = (x EBy) EBz b) x + (y EB z) = (x + y) EB (x + z) c) x EB (y +z) = (x EBy) + (x EBz) 28. Find the duals of these Boolean expressions. a) x + y b) xy c) xyz + xyz d) xz + x . 0 + x . 1 *29. Suppose that F is a Boolean function represented by a Boolean expression in the variables XI, ••. , xn• Show that Fd(XI, ... ,xn) = F(XI, ... ,xn). *30. Show that if F and G are Boolean functions represented by Boolean expressions in n variables and F = G, then Fd = Gd, where Fd and Gd are the Boolean functions represented by the duals of the Boolean expressions rep­ resenting F and G, respectively. [Hint: Use the result of Exercise 29.] *31. How many different Boolean functions F(x, y, z) are there such that F(X, y, Z) = F(x, y, z) for all values of the Boolean variables x, y, and z? *32. How many different Boolean functions F(x, y, z) are there such that F(x,y,z) = F(x,y,z) = F(x,y,z) for all values of the Boolean variables x, y, andz? 33. Show that you obtain De Morgan's laws for proposi­ tions (in Table 5 in Section 1.2) when you transform De Morgan's laws for Boolean algebra in Table 6 into logical equivalences. 11-9 11.2 Representing Boolean Functions 757 34. Show that you obtain the absorption laws for proposi­ tions (in Table 5 in Section 1.2) when you transform the absorption laws for Boolean algebra in Table 5 into logical equivalences. 39. Show that De Morgan's laws hold in a Boolean algebra. That is, show that for all x and y, (x v y) = x A y and (XAy)=xvy. 40. Show that in a Boolean algebra, the modular properties hold. That is, show that x A (y V (x A z» = (x Ay) V (x A z) and x v (y A (x V z» = (x v y) A (x V z). In Exercises 35-42, use the laws in Definition 1 to show that the stated properties hold in every Boolean algebra. 35. Show that in a Boolean algebra, the idempotent laws x v x = x and x A x = x hold for every element x. 41. Show that in a Boolean algebra, if x v y = 0, then x = 0 andy = 0, and that if x Ay = 1, then x = 1 andy = 1. 36. Show that in a Boolean algebra, every element x has a unique complement x such that x v x = 1 andx A x = o. 42. Show that in a Boolean algebra, the dual of an identity, obtained by interchanging the v and A operators and in­ terchanging the elements 0 and 1, is also a valid identity. 37. Show that in a Boolean algebra, the complement of the element 0 is the element 1 and vice versa. 38. Prove that in a Boolean algebra, the law of the double complement holds; that is, x = x for every element x. 43. Show that a complemented, distributive lattice is a Boolean algebra. 11.2 Repres enting Boolean Functions Two important problems of Boolean algebra will be studied in this section. The first problem is: Given the values of a Boolean function, how can a Boolean expression that represents this function be found? This problem will be solved by showing that any Boolean function can be represented by a Boolean sum of Boolean products of the variables and their complements. The solution of this problem shows that every Boolean function can be represented using the three Boolean operators ., +, and -. The second problem is: Is there a smaller set of operators that can be used to represent all Boolean functions? We will answer this problem by showing that all Boolean functions can be represented using only one operator. Both of these problems have practical importance in circuit design. Sum-of-Products Expansions We will use examples to illustrate one important way to find a Boolean expression that represents a Boolean function. EXAMPLE 1 Find Boolean expressions that represent the functions F (x, y, z) and G(x, y, z), which are given in Table 1. Solution: An expression that has the value 1 when x = z = 1 and y = 0, and the value 0 TABLE 1 otherwise, is needed to represent F. Such an expression can be formed by taking the Boolean product of x, y, and z. This product, xyz, has the value 1 if and only if x = Y = z = 1, which holds if and only if x = z = 1 and y = O. x y z 1 1 1 1 1 0 1 0 1 1 0 0 0 1 1 0 1 0 0 0 1 0 0 0 F 0 0 1 0 0 0 0 0 G 0 1 0 0 0 1 0 0 To represent G, we need an expression that equals 1 when x = y = 1 and z = 0, or when x = z = 0 and y = 1. We can form an expression with these values by taking the Boolean sum of two different Boolean products. The Boolean product xyz has the value 1 if and only if x = y = 1 and z = O. Similarly, the product xyz has the value 1 if and only if x = z = 0 and y = 1. The Boolean sum of these two products, xyz +xyz, represents G, because it has the value 1 if and only if x = y = I and z = 0 or x = z = 0 and y = 1. .... Example 1 illustrates a procedure for constructing a Boolean expression representing a function with given values. Each combination of values of the variables for which the function has the value 1 leads to a Boolean product of the variables or their complements. 758 11 / Boolean Algebra 11-10 DEFINITION 1 A literal is a Boolean variable or its complement. A minterm of the Boolean variables Xl, X2, ••• , Xn is a Boolean product YIY2 ••• Yn, where Yi = Xi or Yi = Xi. Hence, aminterm is a product of n literals, with one literal for each variable. A minterm has the value 1 for one and only one combination of values of its variables. More precisely, the minterm YI Y2 .•• Yn is I if and only if each Yi is I, and this occurs if and only if Xi = I whenYi = Xi and Xi = 0 whenYi = Xi' EXAM PLE 2 Find a minterm that equals I if Xl = X3 = 0 and X2 = X4 = X5 = I, and equals 0 otherwise. EXAMPLE 3 Extra� Examples""" Solution: The minterm XIX2X3X4X5 has the correct set of values. By taking Boolean sums of distinct minterms we can build up a Boolean expression with a specified set of values. In particular, a Boolean sum of minterms has the value I when exactly one of the minterms in the sum has the value I. It has the value 0 for all other combinations of values of the variables. Consequently, given a Boolean function, a Boolean sum of minterms can be formed that has the value I when this Boolean function has the value I, and has the value 0 when the function has the value O. The minterms in this Boolean sum correspond to those combinations of values for which the function has the value I. The sum of minterms that represents the function is called the sum-of-products expansion or the disjunctive normal form of the Boolean function. (See Exercise 42 in Section 1.2 for the development of disjunctive normal form in propositional calculus.) Find the sum-of-products expansion for the function F(x, y, z) = (x + y)z. Solution: We will find the sum-of-products expansion of F(x, y, z) in two ways. First, we will use Boolean identities to expand the product and simplify. We find that F(x,y,z) = (x + y)z = xz + yz Distributive law = X lZ + Iyz Identity law = x(y + Y)z + (x + X)yz Unit property = xyz + xyz + xyz + xyz Distributive law = xyz +xyz +xyz. Idempotent law Second, we can construct the sum-of-products expansion by determining the values of F for all possible values of the variables x, y, and z. These values are found in Table 2. The sum-of­ products expansion of F is the Boolean sum of three minterms corresponding to the three rows of this table that give the value I for the function. This gives F(x,y, z) = xyz +xyz +xyz. It is also possible to find a Boolean expression that represents a Boolean function by taking a Boolean product of Boolean sums. The resulting expansion is called the conjunctive normal form or product-of-sums expansion of the function. These expansions can be found from sum-of-products expansions by taking duals. How to find such expansions directly is described in Exercise 10 at the end of this section. JJ-JJ TABLE 2 x y z x+y z I I I I 0 I I 0 I I I 0 I I 0 I 0 0 I I 0 I I I 0 0 I 0 I I 0 0 I 0 0 0 0 0 0 I Functional Completeness (x+y)z 0 I 0 I 0 I 0 0 11.2 Representing Boolean Functions 759 Every Boolean function can be expressed as a Boolean sum of mintenns. Each mintenn is the Boolean product of Boolean variables or their complements. This shows that every Boolean function can be represented using the Boolean operators ., +, and -. Because every Boolean function can be represented using these operators we say that the set {., +, -} is functionally complete. Can we find a smaller set of functionally complete operators? We can do so if one of the three operators of this set can be expressed in tenns of the other two. This can be done using one of De Morgan's laws. We can eliminate all Boolean sums using the identity x+y=xy, which is obtained by taking complements of both sides in the second De Morgan law, given in Table 5 in Section 11.1, and then applying the double complementation law. This means that the set {., -} is functionally complete. Similarly, we could eliminate all Boolean products using the identity xy =x+y, which is obtained by taking complements of both sides in the first De Morgan law, given in Table 5 in Section 11.1, and then applying the double complementation law. Consequently {+, -} is functionally complete. Note that the set {+, .} is not functionally complete, because it is impossible to express the Boolean function F(x) = x using these operators (see Exer­ cise 19). We have found sets containing two operators that are functionally complete. Can we find a smaller set of functionally complete operators, namely, a set containing just one operator? Such sets exist. Define two operators, the I or NAND operator, defined by 1 I 1 = 0 and 1 I 0 = o I 1 = 0 I 0 = 1; and the -I-or NOR operator, defined by 1 -I- 1 = 1 -I-0 = 0 -I- 1 = 0 and o -I-0 = 1. Both of the sets { I } and { -I- } are functionally complete. To see that { I } is function­ ally complete, because {., -} is functionally complete, all that we have to do is show that both of the operators · and -can be expressed using just the I operator. This can be done as x=x lx, xy = (x I y) I (x I y). The reader should verify these identities (see Exercise 14). We leave the demonstration that { -I- } is functionally complete for the reader (see Exercises 15 and 16). 760 II/Boolean Algebra Exercises 1. Find a Boolean product of the Boolean variables x, y, and z, or their complements, that has the value 1 if and only if a) x = y = 0, Z = 1. c) x = 0, y = z = 1. b) x=O,y=l,z=O. d) x =y =z = O. 2. Find the sum-of-products expansions of these Boolean functions. a) F(x,y)=x +y c) F(x,y) = 1 b) F(x, y) = xy d)F(x,y)=y 3. Find the sum-of-products expansions of these Boolean functions. a) F(x,y,z)=x +y+z b) F(x, y, z) = (x + z)y c) F(x,y,z) =X d) F(x,y,z)=xy 4. Find the sum-of-products expansions of the Boolean func­ tion F(x, y, z) that equals 1 if and only if a) x = O. b) xy = O. c) x + y = O. d) xyz = O. 5. Find the sum-of-products expansion of the Boolean func­ tion F (w , x, y, z) that has the value 1 if and only if an odd number of w, x, y, and z have the value 1. 6. Find the sum-of-products expansion of the Boolean func­ tion F(XI' X2, X3, X4, X5) that has the value 1 if and only if three or more of the variables Xl, x2, x3, X4, and X5 have the value 1. Another way to find a Boolean expression that represents a Boolean function is to form a Boolean product of Boolean sums of literals. Exercises 7-11 are concerned with represen­ tations of this kind. 7. Find a Boolean sum containing either x or X, either y or y, and either z or z that has the value 0 if and only if a) x = y = 1, z = O. b) x = y = z = O. c) x = z = 0, y = 1. 8. Find a Boolean product of Boolean sums of literals that has the value 0 if and only if either x = y = 1 and z = 0, x = z = 0 andy = 1, 01: x = y = z = O. [Hint: Takethe 11.3 Logic Gates Introduction 11-12 Boolean product of the Boolean sums found in parts (a), (b), and (c) in Exercise 7.] 9. Show that the Boolean sum Yl + Y2 + ... + Yn, where Yi = Xi or Yi = Xi, has the value 0 for exactly one combi­ nation of the values of the variables, namely, when Xi = 0 if Yi = Xi and Xi = 1 if Yi = Xi. This Boolean sum is called a maxterm. 10. Show that a Boolean function can be represented as a Boolean product of maxterms. This representation is called the product-of-sums expansion or conjunctive normal form of the function. [Hint: Include one max­ term in this product for each combination of the variables where the function has the value 0.] 11. Find the product-of-sums expansion of each of the Boolean functions in Exercise 3. 12. Express each of these Boolean functions using the oper­ ators . and -. a) x+y+z c) (x +)i) b) x + y(X +z) d) x(x + y +Z) 13. Express each of the Boolean functions in Exercise 12 using the operators + and -. 14. Show that a) X = x I x. b) xy = (x I y) I (x I y). c) x + y = (x I x) I (y I y). 15. Show that a) x=x -!-x. b) xy = (x -!-x)..!-(y -!-y). c) x + y = (x -!-y)..!-(x -!-y). 16. Show that { -!-} is functionally complete using Exercise 15. 17. Express each of the Boolean functions in Exercise 3 using . the operator I . 18. Express each of the Boolean functions in Exercise 3 using the operator -!-. 19. Show that the set of operators {+, .} is not functionally complete. 20. Are these sets of operators functionally complete? .){+, EB} b) C EB} c){" EB} uDaiC Boolean algebra is used to model the circuitry of electronic devices. Each input and each output of such a device can be thought of as a member of the set to, I}. A computer, or other electronic device, is made up of a number of circuits. Each circuit can be designed using the rules of Boolean algebra that were studied in Sections 11.1 and 11.2. The basic elements of circuits are called gates. Each type of gate implements a Boolean operation. In this section we define several types of gates. Using these gates, we will apply the rules of Boolean algebra to design circuits that perform a variety of tasks. The circuits that we will study in this chapter give output 11-13 :--.:0 x+y� (a) Inverter (b) OR gate FIGURE 1 Basic Types of Gates. FIGURE 2 Gates with n Inputs. 11.3 Logic Gates 761 (c) AND gate that depends only on the input, and not on the current state of the circuit. In other words, these circuits have no memory capabilities. Such circuits are called combinational circuits or gating networks. We will construct combinational circuits using three types of elements. The first is an inverter, which accepts the value of one Boolean variable as input and produces the complement of this value as its output. The symbol used for an inverter is shown in Figure l(a). The input to the inverter is shown on the left side entering the element, and the output is shown on the right side leaving the element. The next type of element we will use is the OR gate. The inputs to this gate are the values of two or more Boolean variables. The output is the Boolean sum of their values. The symbol used for an OR gate is shown in Figure I (b). The inputs to the OR gate are shown on the left side entering the element, and the output is shown on the right side leaving the element. The third type of element we will use is the AND gate. The inputs to this gate are the values of two or more Boolean variables. The output is the Boolean product of their values. The symbol used for an AND gate is shown in Figure 1 (c). The inputs to the AND gate are shown on the left side entering the element, and the output is shown on the right side leaving the element. We will permit multiple inputs to AND and OR gates. The inputs to each ofthese gates are shown on the left side entering the element, and the output is shown on the right side. Examples of AND and OR gates with n inputs are shown in Figure 2. Combinations of Gates Combinational circuits can be constructed using a combination of inverters, OR gates, and AND gates. When combinations of circuits are formed, some gates may share inputs. This is shown in one of two ways in depictions of circuits. One method is to use branchings that indicate all the gates that use a given input. The other method is to indicate this input separately for each gate. Figure 3 illustrates the two ways of showing gates with the same input values. Note also that output from a gate may be used as input by one or more other elements, as shown in Figure 3. Both drawings in Figure 3 depict the circuit that produces the output xy + xy. EXAM PLE 1 Construct circuits that produce the following outputs: (a) (x + y)x, (b) x(y +Z), and (c) (x + y + z)(XyZ). Solution: Circuits that produce these outputs are shown in Figure 4. 762 II/Boolean Algebra x-------.! Y --------.t xy +xy x---.! y-------.! x ----.----� y-....+-----� xy +xy FIGURE 3 Two Ways to Draw the Same Circuit. (a) (b) y--.... L-__ x---� x---.r-...... y---.t z--�-./ (c) x -----pj y----.! z-----.t (x + y)x x+y+z (x + y + z )xyz FIGURE 4 Circuits that Produce the Outputs Specified in Example 1. 11-15 EXAM PLE 2 EXAM PLE 3 TABLE 1 x y F(x,y) I I I I 0 0 0 I 0 0 0 I y ---I�� x--� ,.--. xy +xz+ yz FIGURE 5 A Circuit for Majority Voting. Examples of Circuits 11.3 Logic Gates 763 We will give some examples of circuits that perform some useful functions. A committee of three individuals decides issues for an organization. Each individual votes either yes or no for each proposal that arises. A proposal is passed if it receives at least two yes votes. Design a circuit that determines whether a proposal passes. Solution: Let x = I if the first individual votes yes, and x = 0 if this individual votes no; let y = 1 if the second individual votes yes, and y = 0 if this individual votes no; let z = 1 if the third individual votes yes, and z = 0 if this individual votes no. Then a circuit must be designed that produces the output 1 from the inputs x, y, and z when two or more of x, y, and z are 1. One representation of the Boolean function that has these output values is xy + xz + yz (see Exercise 12 in Section 11.1). The circuit that implements this function is shown in Figure 5. ... Sometimes light fixtures are controlled by more than one switch. Circuits need to be designed so that flipping anyone of the switches for the fixture turns the light on when it is off and turns the light off when it is on. Design circuits that accomplish this when there are two switches and when there are three switches. Solution: We will begin by designing the circuit that controls the light fixture when two different switches are used. Let x = 1 when the first switch is closed and x = 0 when it is open, and let y = 1 when the second switch is closed and y = 0 when it is open. Let F (x, y) = 1 when the light is on and F(x, y) = 0 when it is off. We can arbitrarily decide that the light will be on when both switches are closed, so that F (1, I) = 1. This determines all the other values of F. When one of the two switches is opened, the light goes off, so F(I, 0) = F(O, 1) = O. When the other switch is also opened, the light goes on, so F(O, 0) = 1. Table 1 displays these values. We see that F(x, y) = xy + xy. This function is implemented by the circuit shown in Figure 6. x---+r-""" .xy + 1'y FIGURE 6 A Circuit for a Light Controlled by Two Switches. 764 II/Boolean Algebra 11-16 TABLE 2 x y z F(x,y,z) I I I I I I 0 0 I 0 I 0 I 0 0 I 0 I I 0 0 I 0 I 0 0 I I 0 0 0 0 uDaLe TABLE 3 Input and Output for the Half Adder. Input Output x y s c I I 0 I I 0 I 0 0 I I 0 0 0 0 0 x -----�-....., xyz y � ----� z-----�-/ x ----------, x y z --------' xyz + xyz + xyz + xyz . FIGURE 7 A Circuit for a Fixture Controlled by Three Switches. We will now design a circuit for three switches. Let x, y, and z be the Boolean variables that indicate whether each of the three switches is closed. We let x = 1 when the first switch is closed, andx = 0 when it is open; y = 1 when the second switch is closed, and y = 0 when it is open; and z = 1 whenthethirdswitchisclosed,andz = Owhenitisopen.LetF(x,y,z) = 1 when the light is on and F (x, y, z) = 0 when the light is off. We can arbitrarily specify that the light be on when all three switches are closed, so that F (1, 1, 1) = 1. This determines all other values of F. When one switch is opened, the light goes off, so F(I, 1,0) = F(I, 0,1) = F(O, 1, 1) = O. When a second switch is opened, thelightgoeson,soF(I, 0, 0) = F(O, 1,0) = F(O, 0,1) = 1. Finally, when the third switch is opened, the light goes off again, so F(O, 0, 0) = 0. Table 2 shows the values of this function. The function F can be represented by its sum-of-products expansion as F (x, y, z) = xyz + xyz + xyz + xyz. The circuit shown in Figure 7 implements this function. ... Adders We will illustrate how logic circuits can be used to carry out addition of two positive integers from their binary expansions. We will build up the circuitry to do this addition from some component circuits. First, we will build a circuit that can be used to find x + y, where x and y are two bits. The input to our circuit will be x and y, because these each have the value 0 or the value 1. The output will consist of two bits, namely, s and c, where s is the sum bit and c is the carry bit. This circuit is called a multiple output circuit because it has more than one output. The circuit that we are designing is called the half adder, because it adds two bits, without considering a carry from a previous addition. We show the input and output for the half adder in Table 3. From Table 3 we see that c = xy and that s = xy + xy = (x + y)(xy). Hence, the circuit shown in Figure 8 computes the sum bit s..and the carry bit c from the bits x and y. We use the full adder to compute the sum bit and the carry bit when two bits and a carry are added. The inputs to the full adder are the bits x and y and the carry Cj. The outputs are the sum bit s and the new carry Ci+ 1. The inputs and outputs for the full adder are shown in Table 4. 11-17 X+Y Carry =xy Ci -------.J (X+Y)(Xy) x Y 11.3 Logic Gates 765 S = XYCi + XYCi + XYCi + XYCi Ci+1 =XYCi+XYCi+ XY FIGURE 8 The Half Adder. FIGURE 9 A Full Adder. TABLE 4 Input and Output for the Full Adder. Input Output x y CI S CI+1 1 1 1 1 1 1 1 0 0 1 1 0 1 0 1 1 0 0 1 0 0 1 1 0 1 0 1 0 1 0 0 0 1 1 0 0 0 0 0 0 Exercises Xo 1--------. So Yo Full xI adder YI ci x2 Y2 FIGURE 10 Adding Two Three-Bit Integers with Full and Half Adders. The two outputs of the full adder, the sum bit s and the carryCi+J. are given by the sum­ of-products expansions xyCj + xyCj + xyCj + xyCj and xyCj + xyCj + xyCj + xyCj, respec­ tively. However, instead of designing the full adder from scratch, we will use half adders to produce the desired output. A full adder circuit using half adders is shown in Figure 9. Finally, in Figure lOwe show how full and half adders can be used to add the two three-bit integers (X2XIXoh and (Y2YIYoh to produce the sum (S3S2SISoh. Note that S3, the highest-order bit in the sum, is given by the carry C2. In Exercises 1-5 find the output of the given circuit. 3. X---'�---"" Y ----. .. __ �___" 1. x Y ---I� z __ --.! X ___ ------' Y---.J 4. x y----� z--_--' 2. X--� x Y----� Y--� z 766 II/Boolean Algebra 5. x - --- ____ ..-----...... y------I� Z - --- ____ L-____ x y --"'::""---+1 z ---- -.L-____ x y z 6. Construct circuits from inverters, AND gates, and OR gates to produce these outputs. a) x+y b) �x c) xyz + xyz d) (X + z)(y + Z) 7. Design a circuit that implements majority voting for five individuals. 8. Design a circuit for a light fixture controlled by four switches, where flipping one of the switches turns the light on when it is off and turns it off when it is on. 9. Show how the sum of two five-bit integers can be found using full and half adders. 10. Construct a circuit for a half subtractor using AND gates, OR gates, and inverters. A half subtractor has two bits as input and produces as output a difference bit and a borrow. 11. Construct a circuit for a full subtractor using AND gates, OR gates, and inverters. A full subtractor has two bits and a borrow as input, and produces as output a difference bit and a borrow. 12. Use the circuits from Exercises 10 and 11 to find the dif­ ference of two four-bit integers, where the first integer is greater than the second integer. *13. Construct a circuit that compares the two-bit integers (xlxoh and (YlYoh, returning an output of 1 when the first of these numbers is larger and an output of 0 other­ wise. *14. Construct a circuit that computes the product of the two- 11.4 Minimization of Circuits Introduction 11-18 bit integers (xlxoh and (YlYO)2. The circuit should have four output bits for the bits in the product. Two gates that are often used in circuits are NAND and NOR gates. When NAND or NOR gates are used to represent cir­ cuits, no other types of gates are needed. The notation for these gates is as follows: x�x� y� y� *15. Use NAND gates to construct circuits with these outputs. a)x b)x + Y c)xy d)x €By *16. Use NOR gates to construct circuits for the outputs given in Exercise 15. *17. Construct a half adder using NAND gates. *18. Construct a half adder using NOR gates. A multiplexer is a switching circuit that produces as output one of a set of input bits based on the value of control bits. 19. Construct a multiplexer using AND gates, OR gates, and inverters that has as input the four bits xo, Xl, X2, and X3 and the two control bits Co and Cl. Set up the circuit so that Xi is the output, where i is the value of the two-bit integer (Cl CO)2. The depth of a combinatorial circuit can be defined by spec­ ifying that the depth of the initial input is 0 and if a gate has n different inputs at depths dl• d2 ••••• dn, respective ly, then its outputs have depth equal to max(dl• d2 •...• dn) + 1; this value is also defined to be the depth of the gate. The depth of a combinatorial circuit is the maximum depth of the gates in the circuit. 20. Find the depth of a) the circuit constructed in Example 2 for majority voting among three people. b) the circuit constructed in Example 3 for a light con­ trolled by two switches. c) the half adder shown in Figure 8. d) the full adder shown in Figure 9. The efficiency of a combinational circuit depends on the number and arrangement of its gates. The process of designing a combinational circuit begins with the table specifying the output for each combination of input values. We can always use the sum-of-products expansion of a circuit to find a set of logic gates that will implement this circuit. However, the sum-of­ products expansion may contain many more terms than are necessary. Terms in a sum-of­ products expansion that differ in just one variable, so that in one term this variable occurs and in the other term the complement of this variable occurs, can be combined. For instance, consider the circuit that has output I if and only if x = y = z = I or x = z = I and y = o. The 11-19 x --4-r---..... y----.! z ---I�--'" x _ __ ---, y z --------' xyz : ------.!:O xz � xyz +xyz FIGURE 1 Two Circuits with the Same Output. 11.4 Minimization of Circuits 767 sum-of-products expansion of this circuit is xyz + xyz. The two products in this expansion differ in exactly one variable, namely, y. They can be combined as xyz + xyz = (y + Y)(xz) = 1 . (xz) =xz. Hence, xz is a Boolean expression with fewer operators that represents the circuit. We show two different implementations of this circuit in Figure 1. The second circuit uses only one gate, whereas the first circuit uses three gates and an inverter. This example shows that combining terms in the sum-of-products expansion of a circuit leads to a simpler expression for the circuit. We will describe two procedures that simplify sum-of-products expansions. The goal of both procedures is to produce Boolean sums of Boolean products that represent a Boolean function with the fewest products of literals such that these products contain the fewest literals possible among all sums of products that represent a Boolean function. Finding such a sum of products is called minimization ofthe Boolean function. Minimizing a Boolean function makes it possible to construct a circuit for this function that uses the fewest gates and fewest inputs to the AND gates and OR gates in the circuit, among all circuits for the Boolean expression we are minimizing. Until the early 1960s logic gates were individual components. To reduce costs it was impor­ tant to use the fewest gates to produce a desired output. However, in the mid-1960s, integrated circuit technology was developed that made it possible to combine gates on a single chip. Even though it is now possible to build increasingly complex integrated circuits on chips at low cost, minimization of Boolean functions remains important. Reducing the number of gates on a chip can lead to a more reliable circuit and can reduce the cost to produce the chip. Also, minimization makes it possible to fit more circuits on the same chip. Furthermore, minimization reduces the number of inputs to gates in a circuit. This reduces the time used by a circuit to compute its output. Moreover, the number of inputs to a gate may be limited because of the particular technology used to build logic gates. The first procedure we will introduce, known as Karnaugh maps (or K-maps), was designed in the 1950s to help minimize circuits by hand. K-maps are useful in minimizing circuits with up to six variables, although they become rather complex even for five or six variables. The second procedure we will describe, the Quine-McCluskey method, was invented in the 1960s. It automates the process of minimizing combinatorial circuits and can be implemented as a computer program. 768 II/Boolean Algebra 11-20 liDksi:::i y y :� xL:I:J FIGURE 2 K-maps in Two Variables. EXAM PLEl Unfortunately, minimizing Boolean functions with many variables is a computationally intensive problem. It has been shown that this problem is an NP-complete problem (see Section 3.3 and [Ka93]), so the existence of a polynomial-time algorithm for minimizing Boolean circuits is unlikely. The Quine-McCluskey method has exponential complexit y. In practice, it can be used only when the number of literals does not exceed ten. Since the 1970s a number of newer algorithms have been developed for minimizing combinatorial circuits (see [Ha93] and [Ka93]). However, with the best algorithms yet devised, only circuits with no more than 25 variables can be minimized. Also, heuristic (or rule-of-thumb) methods can be used to substantially simplify, but not necessa rily minimize, Boolean expressions with a larger number of literals. Kamaugh Maps To reduce the number of terms in a Boolean expression representing a circuit, it is necessary to find terms to combine. There is a graphical method, called a Karnaugh map or K-map, for finding terms to combine for Boolean functions involving a relatively small number of variables. The method we will describe was introduced by Maurice Karnaugh in 1953. His method is based on earlier work by E. W. Veitch. (This method is usually applied only when the function involves six or fewer varia bles.) K-maps give us a visual method for simplifying sum-of-products expansions; they are not suited for mechanizing this process. We will first illustrate how K-maps are used to simplify expansions of Boolean functions in two variables. We will continue by showing how K-maps can be used to minimize Boolean functions in three variables and then in four variables. Then we will describe the concepts that can be used to extend K-maps to minimize Boolean functions in more than four variables. There are four possible minterms in the sum-of-products expansion of a Boolean function in the two variables x andy. A K-map for a Boolean function in these two variables consists of four cells, where a 1 is placed in the cell representing a minterm if this minterm is present in the expansion. Cells are said to be adjacent if the minterms that they represent differ in exactly one literal. For instance, the cell representing xy is adjacent to the cells representi ngxy andxy. The four cells and the terms that they represent are shown in Figure 2. Find the K-maps for (a) xy + xy, (b) xy + xy, and (c) xy + xy + xy. Solution: We include a 1 in a cell when the minterm represented by this cell is present in the sum-of-products expansion. The three K-maps are shown in Figure 3. <II1II We can identify minterms that can be combined from the K-map. Whenever there are Is in two adjacent cells in the K-map, the minterms represented by these cells can be combined int.o a product involving just o�e �f.��e r.ari�ble�: f�EJR�aR.Et: :X¥ �nd �y �e represente � by adJa�ent cells and can be com��?�d �p.to'y� ��c���e fr t t;f � �:f. t X)y = y. Moreover, If Is are m all four cells, the fo�r f11.fnte�� can R� ppmRm�4 Hl.J:g Rn-� term, namely, the Boolean expression 1 that involves none of the variables. We circle'SlocKs of cells in the K-map that represent minterms that can be combined and then find the corresponding sum of products. The MAURICE KARNAUGH (BORN 1924) Maurice Kamaugh, born in New York City, received his B.S. from the City College of New York and his M.S. and Ph.D. from Yale University. He was a member of the technical staff at Bell Laboratories from 1952 until 1966 and Manager of Research and Development at the Federal Systems Division of AT&T from 1966 to 1970. In 1970 he joined IBM as a member of the research staff. Kamaugh has made fundamental contributions to the application of digital techniques in both computing and telecommunications. His current interests include knowledge-based systems in computers and heuristic search methods. 11-21 y y y y y y :EE :EE :EE (a) (b) (c) 11.4 Minimization of Circuits 769 FIGURE 3 K-maps for the Sum-of-Products Expansions in Example 1. y y y y y :EE :58 :B8] (a) (b) (c) FIGURE 4 Simplifying the Sum-of-Products Expansion from Example 2. goal is to identify the largest possible blocks, and to cover all the I s with the fewest blocks using the largest blocks first and always using the largest possible blocks. EXAMPLE 2 Simplify the sum-of-products expansions given in Example 1. Solution: The grouping of minterms is shown in Figure 4 using the K-maps for these expansions. Minimal expansions for these sums-of-products are (a) y, (b) xy + xy, and (c) x + y. .... A K-map in three variables is a rectangle divided into eight cells. The cells represent the eight possible minterms in three variables. Two cells are said to be adjacent if the minterms that they represent differ in exactly one literal. One of the ways to fo rm a K-map in three variables is shown in Figure 5(a). This K-map can be thought of as lying on a cylinder, as shown in Figure 5(b). On the cylinder, two cells have a common border if and only if they are adjacent. To simplify a sum-of-products expansion in three variables, we use the K-map to identify blocks of minterms that can be combined. Blocks of two adjacent cells represent pairs of minterms that can be combined into a product of two literals; 2 x 2 and 4 x 1 blocks of cells represent minterms that can be combined into a single literal; and the block of all eight cells yz yz yz yz x xyz xyz xji£ xjiz iyz iyz xy£ xyz (a) FIGURE 5 K-maps in Three Variables. xyz iyz (b) 770 II/Boolean Algebra //-22 yz yz yz yz yz yz (a) (b) yz yz yz YZ (d) yz yZ yz yz yz yz yz I = xy: + xy;: + xv;: + xv: + Xyz + .�\';: + is';: + .r.v: (e) yz yz yz (c) FIGURE 6 Blocks in K-maps in Three Variables. represents a product of no literals, namely, the function 1. In Figure 6, 1 x 2, 2 x I, 2 x 2, 4 xl, and 4 x 2 blocks and the products they represent are shown. The product of literals corresponding to a block of alII s in the K -map is called an implicant of the function being minimized. It is called a prime implicant if this block of I s is not contained in a larger block of 1 s representing the product of fewer literals than in this product. The goal is to identify the largest possible blocks in the map and cover all the 1 s in the map with the least number of blocks, using the largest blocks first. The largest possible blocks are always chosen, but we must always choose a block if it is the only block of 1 s covering a 1 in the K-map. Such a block represents an essential prime implicant. By covering all the 1 s in the map with blocks corresponding to prime implicants we can express the sum of products as a sum of prime implicants. Note that there may be more than one way to cover all the Is using the least number of blocks. Example 3 illustrates how K-maps in three variables are used. EXAMPLE 3 Use K-maps to minimize these sum-of-products expansions. (a) xyz + xyz + xyz + xyz (b) xyz + xyz + xyz + xyz + xyz (c) xyz + xyz + xyz + xyz + xyz + xyz + xyz (d) xyz + xyz + xyz + xyz Solution: The K-maps for these sum-of-products expansions are shown in Figure 7. The grouping of blocks shows that minimal expansions into Boolean sums of Boolean products are (a) xz + yz + xyz, (b) y + xz, (c) x + y + z, and (d) xz + xy. In part (d) note that the prime implicants xz and xy are essential prime implicants, but the prime implicant yz is a prime implicant that is not essential because the cells it covers are covered by the other two prime implicants. <I A K-map in four variables is a square that is divided into 16 cells. The cells represent the 16 possible minterms in four variables. One of the ways to form a K-map in four variables is shown in Figure 8. //-23 yz ,,- ...... x f \ 1 1 x yz ,,­.r .... 1 1 11.4 Minimization of Circuits 771 '\ 8 1 "---J 8 1 1(1 .1' i (a) v-. " x ( 1 f \ 1 .1' 1 � (c) 1 1 x 1 1 .1' ,,­ ., .... yz I( 1 1\ FIGURE 7 Using K-maps in Three Variables. (b) 1 1 1\ / (d) Two cells are adjacent if and only if the minterms they represent differ in one literal. Consequently, each cell is adjacentto four other cells. The K -map of a sum-of-products expansion in four variables can be thought of as lying on a torus, so that adjacent cells have a common boundary (see Exercise 28). The simplification of a sum-of-products expansion in four variables is carried out by identifying those blocks of2, 4, 8, or 16 cells that represent minterms that can be combined. Each cell representing a minterm must either be used to form a product using fewer literals, or be included in the expansion. In Figure 9 some examples of blocks that represent products of three literals, products of two literals, and a single literal are illustrated. As is the case in K-maps in two and three variables, the goal is to identify the largest blocks of 1 s in the map that correspond to the prime implicants and to cover all the 1 s using the fewest blocks needed, using the largest blocks first. The largest pos sible blocks are always used. Example 4 illustrates how K-maps in four variables are used. EXAM PLE 4 Use K-maps to simplify these sum-of-products expa nsions. wx w.t w.\' »'X (a) wxyz + wxyz + wxyz + wxyz + wxyz + wxyz + wxyz + wxyz+wxyz (b) wxyz + wxyz + wxyz + wxyz + wxyz + w xyz + w xyz (c) wxyz + wxyz + wxyz + wxyz + wxyz + wxyz + wxyz + wxyz + wxyz+wxyz+wxyz yz yz YZ yz wxy� wxy;: wxyz wx.Vz WXF wxy;: wxy;: wxy;: ii'i,,;: wi)': ii'xS'Z wxyz wxy;: ii'xy;: wx.vz wx.vz FIGURE 8 K-maps in Four Variables. 772 II/Boolean Algebra wx wi wi wx wiz = wiyz + wi.vz (a) xz = wxyz + wxyz + wxyz + wxyz (c) wx wi wi = wiyz + 11'iy;: + wi5;z + w.i}z (b) z = wxyz + wxyz + wiy.:' + wi.v.:' + wiyz + 11'.1:.1'.:' + wxy.:' + ivx.i"':: (d) FIGURE 9 Blocks in K-maps in Four Variables. 11-24 Solution: The K-maps for these expansions are shown in Figure 10. Using the blocks shown leads to the sum of products (a) wyz + wxz + wxy + w xy + wxyz, (b) yz + wxy + XZ, and (c) Z + w x + w X y. The reader should determine whether there are other choices of blocks in each part that lead to different sums of products representing these Boolean functions. .... K-maps can realistically be used to minimize Boolean functions with five or six variables, but beyond that, they are rarely used because they become extremely complicated. However, the concepts used in K-maps play an important role in newer algorithms. Furthermore, mastering these concepts helps you understand these newer algorithms and the computer-aided design (CAD) programs that implement them. As we develop these concepts, we will be able to illustrate yz vz yz yz yz vz wx 1 (1 1) wx 1 wi 0 (1 1) wi 1 I) t' 1 1) wi 1 1/ wx 0) wx 0 (a) (b) FIGURE 10 Using K-maps in Four Variables. vz v- wx 1 1 wi . (1 I) 1 1 1 wx (1 1 1 1) (c) 11-25 11.4 Minimization of Circuits 773 them by referring back to our discussion of minimization of Boolean functions in three and in four variables. The K-maps we used to minimize Boolean functions in two, three, and four variables are built using 2 x 2, 2 x 4, and 4 . x 4 rectangles, respective ly. Furthermore, corresponding cells in the top row and bottom row and in the leftmost column and rightmost column in each of these cases are considered adjacent because they represent minterms differing in only one literal. We can build K-maps for minimizing Boolean functions in more than four variables in a similar way. We use a rectangle containing 2ln/2J rows and 2rn/21 columns. (These K-maps contain 2n cells because rnj21 + Lnj2J = n.) The rows and columns need to be positioned so that the cells representing minterms differing in just one literal are adjacent or are considered adjacent by specifying additional adjacencies of rows and columns. To help (but not entirely) achieve this, the rows and columns ofa K-map are arranged using Gray codes (see Section 9.5), where we associate bit strings and products by specifying that a I corresponds to the appearance of a variable and a 0 with the appearance of its complement. For example, in a 10-dimensional K-map, the Gray code 01110 used to label a row corresponds to the product XIX2X3X4XS . EXAMPLE 5 The K-maps we used to minimize Boolean functions with four variables have two rows and two columns. Both the rows and the columns are arranged using the Gray code 11,10,00,01. The rows represent products WX, WX, w X, and WX, respective ly, and the columns correspond to the products yz, yz, yz, and yz, respectivel y. Using Gray codes and considering cells adjacent in the first and last rows and in the first and last columns, we ensured that minterms that differ in only one variable are always adjacent. .... EXAM PLE 6 To minimize Boolean functions in five variables we use K-maps with 23 = 8 columns and 22 = 4 rows. We label the four rows using the Gray code 11,10,00,01, corresponding to XIX2, XIX2, XIX2, and XIX2, respectively. We label the eight columns using the Gray code 111,110,100,101,001,000,010,011 corresponding to the terms X3X4XS, X3X4XS, X3X4XS, X3X4XS, X3X4XS, X3X4XS, X3X4XS, and X3X4XS, respectively. Using Gray codes to label columns and rows ensures that the minterms represented by adjacent cells differ in only one variable. However, to make sure all cells representing products that differ in only one variable are considered adjacent, we consider cells in the top and bottom rows to be adjacent, as well as cells in the first and eighth columns, the first and fourth columns, the second and seventh columns, the third and sixth columns, and the fifth and eighth columns (as the reader should verify). .... To use a K-map to minimize a Boolean function in n variables, we first draw a K-map of the appropriate size. We place Is in all cells corresponding to minterms in the sum-of-products expansion of this function. We then identify all prime implicants of the Boolean function. To do this we look for the blocks consisting of 2k clustered cells all containing ai, where 1 ::: k ::: n. These blocks correspond to the product of n -k literals. (Exercise 33 asks the reader to verify this.) Furthermore, a block of 2k cells each containing a 1 not contained in a block of 2k+ 1 cells each containing a 1 represents a prime implicant. The reason that this implicant is a prime implicant is that no product obtained by deleting a literal is also represented by a block of cells all containing 1 s. EXAM PLE 7 A block of eight cells representing a product of two literals in a K-map for minimizing Boolean functions in five variables all containing 1 s is a prime implicant if it is not contained in a larger block of 16 cells all containing 1 s representing a single literal. .... Once all prime implicants have been identified, the goal is to find the smallest possible subset of these prime implicants with the property that the cells representing these prime implicants cover all the cells containing a 1 in the K-map. We begin by selecting the essential prime implicants because each of these is represented by a block that covers a cell containing a 1 that 774 II/Boolean Algebra //-26 unkS� EXAMPLE S is not covered by any other prime implicant. We add additional prime implicants to ensure that allIs in the K-map are covered. When the number of variables is large, this last step can become exceedingly complicated. Don't Care Conditions In some circuits we care only about the output for some combinations of input values, be­ cause other combinations of input values are not possible or never occur. This gives us freedom in producing a simple circuit with the desired output because the output values for all those combinations that never occur can be arbitrarily chosen. The values of the function for these combinations are called don't care conditions. A d is used in a K-map to mark those com­ binations of values of the variables for which the function can be arbitrarily assigned. In the minimization process we can assign Is as values to those combinations of the input values that lead to the largest blocks in the K-map. This is illustrated in Example 8. One way to code decimal expansions using bits is to use the four bits of the binary expansion of each digit in the decimal expansion. For instance, 873 is encoded as 100001110011. This encoding of a decimal expansion is called a binary coded decimal expansion. Because there are 16 blocks of four bits and only 10 decimal digits, there are six combinations of four bits that are not used to encode digits. Suppose that a circuit is to be built that produces an output of 1 if the decimal digit is 5 or greater and an output of 0 if the decimal digit is less than 5. How can this circuit be simply built using OR gates, AND gates, and inverters? Solution: Let F (w, x, y, z) denote the output of the circuit, where wxyz is a binary expansion of a decimal digit. The values of F are shown in Table 1. The K-map for F, with ds in the don't care positions, is shown in Figure l1(a). We can either include or exclude squares with ds from blocks. This gives us many possible choices for the blocks. For example, excluding all squares with ds and forming blocks, as shown in Figure 11(b), produces the expression wxy + wxy + wxz. Including some of the ds and excluding others and forming blocks, as shown in Figure II(c), produces the expression wx + wxy + xyz. Finally, including all the ds and using the blocks shown in Figure II(d) produces the simplest sum-of-products expansion possible, namely, F(x, y, z) = w + xy + xz. <III! TABLE 1 Digit w x y z F 0 0 0 0 0 0 I 0 0 0 I 0 2 0 0 I 0 0 3 0 0 1 I 0 4 0 1 0 0 0 5 0 I 0 I 1 6 0 I 1 0 1 7 0 1 1 1 1 8 1 0 0 0 I 9 1 0 0 1 1 //-27 lin!'S� linEIS� v- vz yz vz IV.I d d d d IVX w.! d d 1 1 wi wi 1 1 1 WX (a) v- v" vz vz 11'.1 d d d 110 lVX IC d d 1 1\ /' wx wi wi IC 1 1\ ./ 101 WX (c) vz d d l8 vz /d " ./ d � (1 \ )'" ,. d v- d d IC 1 1\ .; (b) vz d.; d d 1 1\ (d) vz d 1) I� Vi. I� 1 0 11,4 Minimization of Circuits 775 FIGURE 11 The K-map for F Showing Its Don't Care Positions. The Quine-M cClusk ey Method We have seen that K-maps can be used to produce minimal expansions of Boolean functions as Boolean sums of Boolean products. However, K-maps are awkward to use when there are more than four variables. Furthermore, the use ofK -maps relies on visual inspection to identify terms to group. For these reasons there is a need for a procedure for simplifying sum-of-products expansions that can be mechanized. The Quine-McCluskey method is such a procedure. It can be used for Boolean functions in any number of variables. It was developed in the 1950s by W. V. Quine and E. J. McCluskey, Jr. Basically, the Quine-McCluskey method consists of two parts. The first part finds those terms that are candidates for inclusion in a minimal expansion as a Boolean sum of Boolean products. The second part determines which of these terms to actually use. We will use Example 9 to illustrate how, by successively combining implicants into implicants with one fewer literal, this procedure works. EDWARD 1. McCLUSKEY (BORN 1929) Edward McCluskey attended Bowdoin College and M.I.T., where he received his doctorate in electrical engineering in 1956. He joined Bell Telephone Laboratories in 1955, remaining there until 1959. McCluskey was professor of electrical engineering at Princeton University from 1959 until 1966, also serving as director of the Computer Center at Princeton from 1961 to 1966. In 1967 he took a position as professor of computer science and electrical engineering at Stanford University, where he also served as director of the Digital Systems Laboratory from 1969 to 1978. McCluskey has worked in a variety of areas in computer science, including fault-tolerant computing, computer architecture, testing, and logic design. He is currently director of the Center for Reliable Computing at Stanford University. McCluskey is also an ACMFellow. 776 II/Boolean Algebra 11-28 TABLE 2 Minterm Bit String Number of Is xyz III 3 xyz 101 2 xyz Oil 2 xyz 001 I xyz 000 0 EXAMPLE 9 We will show how the Quine-McCluskey method can be used to find a minimal expansion equivalent to unksEC xyz +xyz +xyz+xyz +xyz. We will represent the minterms in this expansion by bit strings. The first bit will be 1 if x occurs and 0 if x occurs. The second bit will be 1 if y occurs and 0 if Y occurs. The third bit will be 1 if z occurs and 0 ifz occurs. We then group these terms according to the number of Is in the corresponding bit strings. This information is shown in Table 2. Minterms that can be combined are those that differ in exactly one literal. Hence, two terms that can be combined differ by exactly one in the number of 1 s in the bit strings that represent them. When two minterms are combined into a product, this product contains two literals. A product in two literals is represented using a dash to denote the variable that does not occur. For instance, the minterms xyz and xyz, represented by bit strings 101 and 001, can be combined into yz, represented by the string -01. All pairs of minterms that can be combined and the product formed from these combinations are shown in Table 3. Next, all pairs of products of two literals that can be combined are combined into one literal. Two such products can be combined if they contain literals for the same two variables, and literals for only one of the two variables differ. In terms of the strings representing the products, these strings must have a dash in the same position and must differ in exactly one of the other two slots. We can combine the products yz and yz, represented by strings -11 and -01, into z, represented by the string --1. We show all the combinations of terms that can be formed in this way in Table 3. WILLARD VAN ORMAN QUINE (1908-2000) Willard Quine, born in Akron, Ohio, attended Oberlin College and later Harvard University , where he received his Ph.D. in philosophy in 1932. He became a Junior Fellow at Harvard in 1933 and was appointed to a position on the faculty there in 1936. He remained at Harvard his entire professional life, except for World War II, when he worked for the U.S. Navy decrypting messages from German submarines. Quine was always interested in algorithms , but not in hardware. He arrived at his discovery of what is now called the Quine-McCluskey method as a device for teaching mathematical logic, rather than as a method for simplifying switching circuits. Q�ine was one of the most famOUS philosophers of the twentieth century. He made fundamental contributions to the theory of knowledge, mathematical logic and set theory, and the philosophies of logic and language. His books, including New Foundations of Mathematical Logic published in 1937 and Word and Object published in 1960, have had profound impact. Quine retired from Harvard in 1978 but continued to commute from his home in Beacon Hill to his office there. He used the 1927 Remington typewriter on which he prepared his doctoral thesis for his entire life. He even had an operation performed on this machine to add a few special symbols, removing the second period, the second comma, and the question mark. When asked whether he missed the question mark, he replied, "Well, you see, I deal in certainties." There is even a word quine, defined in the New Hacker s Dictionary as a program that generates a copy of its own source code as its complete output. Producing the shortest possible quine in a given programming language is a popular puzzle for hackers. 11-29 11.4 Miniinization of Circuits 777 TABLE 3 Step] Step 2 Term Bit String Term String Term String 1 xyz 111 (1,2) xz 1-1 (1,2,3,4) z --1 2 xyz 101 (1,3) yz -11 3 xyz 011 (2,4) yz -01 4 xyz 001 (3,4) xz 0-1 5 ---000 (4,5) xy 00- xyz In Table 3 we also indicate which tenns have been used to fonn products with fewer literals; these tenns will not be needed in a minimal expansion. The next step is to identify a minimal set of products needed to represent the Boolean function. We begin with all those products that were not used to construct products with fewer literals. Next, we fonn Table 4, which has a row for each candidate product fonned by combining original tenns, and a column for each original tenn; and we put an X in a position if the original tenn in the sum-of-products expansion was used to fonn this candidate product. In this case, we say that the candidate product covers the original mintenn. We need to include at least one product that covers each of the original mintenns. Consequently, whenever there is only one X in a column in the table, the product corresponding to the row this X is in must be used. From Table 4 we see that both z and xy are needed. Hence, the final answer is z + xy. .... As was illustrated in Example 9, the Quine-McCluskey method uses this sequence of steps to simplify a sum-of-products expression. I. Express each mintenn in n variables by a bit string oflength n with a I in the ith position if Xi occurs and a 0 in this position if Xi occurs. 2. Group the bit strings according to the number of I s in them. 3. Detennine all products in n -I variables that can be fonned by taking the Boolean sum of mintenns in the expansion. Mintenns that can be combined are represented by bit strings that differ in exactly one position. Represent these products in n -I variables with strings that have a I in the ith position if Xi occurs in the product, a 0 in this position if Xi occurs, and a dash in this position if there is no literal involving Xi in the product. 4. Detennine all products in n -2 variables that can be fonned by taking the Boolean sum of the products in n -I variables found in the previous step. Products in n -I variables that can be combined are represented by bit strings that have a dash in the same position and differ in exactly one position. S. Continue combining Boolean products into products in fewer variables as long as possible. 6. Find all the Boolean products that arose that were not used to fonn a Boolean product in one fewer literal. TABLE 4 xyz -\fz �z xyz xyz z X X X X xy X X 778 II/Boolean Algebra //-30 TABLE 5 Term Bit String Numherofls wxyz 1110 3 wxyz 1011 3 Wxyz 0111 3 wxyz 1010 2 Wxyz 0101 2 Wxyz 0011 2 Wxyz 0001 1 7. Find the smallest set of these Boolean products such that the sum of these products represents the Boolean function. This is done by forming a table showing which minterms are covered by which products. Every minterm must be covered by at least one product. The first step in using this table is to find all essential prime· implicants. Each essential prime implicant must be included because it is the only prime implicant that covers one of the minterms. Once we have found essential prime implicants, we can simplify the table by eliminating the columns for minterms covered by this prime implicant. Furthermore, we can eliminate any prime implicants that cover a subset of minterms covered by another prime implicant (as the reader should verify). Moreover, we can eliminate from the table the column for a minterm if there is another minterm that is covered by a subset of the prime implicants that cover this minterm. This process of identifying essential prime implicants that must be included, followed by eliminating redundant prime implicants and identifying minterms that can be ignored, is iterated until the table does not change. At this point we use a backtracking procedure to find the optimal solution where we add prime imp Ii cants to the cover to find possible solutions, which we compare to the best solution found so far at each step. A final example will illustrate how this procedure is used to simplify a sum-of-products expansion in four variables. EXAMPLE 10 Use the Quine-McCluskey method to simplify the sum-of-products expansion w xyz + w X yz + wxyz + wxyz + wxyz + w xyz + w xyz. TABLE 6 1 2 3 4 5 6 7 Term wxyz wxyz Wxyz - -wxyz Wxyz Wxyz Wxyz Bit String 1110 (1,4) 1011 (2,4) 0111 (2,6) 1010 (3,5) 0101 (3,6) 0011 (5,7) 0001 (6,7) Step 1 Step 2 Term String Term String wyz 1-10 (3,5,6,7) Wz 0--1 wxy 101- xyz -011 Wxz 01-1 Wyz 0-11 Wyz 0-01 Wxz 00-1 /1-3/ TABLE 7 wxyz wXyz Wz wyz X wxy X xyz X wxyz wXyz wxyz X X X X 1104 Minimization of Circuits 779 wXyz wxyz X X X Solution: We first represent the minterms by bit strings and then group these terms together according to the number of Is in the bit strings. This is shown in Table 5. All the Boolean products that can be formed by taking Boolean sums ofthese products are shown in Table 6. The only products that were not used to form products in fewer variables are wz, wyz, wxy, and xyz. In Table 7 we show the minterms covered by each of these products. To cover these minterms we must include wz and wyz, because these products are the only products that cover wxyz and wxyz, respectively. Once these two products are included, we see that only one of the two products left is needed. Consequently, we can take either wz + wyz + wxy or wz + wyz + xyz as the final answer. .... Exercis es 1. a) Draw a K-map for a function in two variables and put a 1 in the cell representing x y. b) What are the rninterms represented by cells adjacent to this cell? 2. Find the sum-of-products expansions represented by each of these K-maps. a) y y :E8 b) y y :E8 c) y y :B xB 3. Draw the K-maps of these sum-of-products expansions in two variables. a) xy b) xy +xy c) xy +xy+xy +xy 4. Use a K-map to find a minimal expansion as a Boolean sum of Boolean products of each of these functions of the Boolean variables x and y. a) xy+xy b) xy +xy c) xy +xy +xy +xy 5. a) Draw a K-map for a function in three variables. Put a 1 in the cell that represents xyz. b) Which minterms are represented by cells adjacent to this cell? 6. Use K-maps to find simpler circuits with the same output as each of the circuits shown. b) x y Z z x y Z x y z x y z c) x-{>o i: )ryZ iyz[(x + z) + <y + i)l � I U. x z Z y Y z z 780 II/Boolean Algebra 7. Draw the K-maps of these sum-of-products expansions in three variables. a) xyz b) xyz + xyz c) xyz + xyz + xyz + xyz 8. Construct a K-map for F(x, y, z) = xz + yz + xyz. Use this K-map to find the implicants, prime implicants, and essential prime implicants of F (x, y, z). 9. Construct a K-map for F(x, y, z) = xz + xyz + yz. Use this K-map to find the implicants, prime implicants, and essential prime implicants of F (x, y, z). 10. Draw the 3-cube Q3 and label each vertex with the minterm in the Boolean variables x, y, and z associated with the bit string represented by this vertex. For each literal in these variables indicate the 2-cube Q2 that is a subgraph of Q3 and represents this literal. 11. Draw the 4-cube Q4 and label each vertex with the minterm in the Boolean variables w, x, y, and z asso­ ciated with the bit string represented by this vertex. For each literal in these variables, indicate which 3-cube Q3 that is a subgraph of Q4 represents this literal. Indicate which 2-cube Q2 that is a subgraph of Q4 represents the products wz, xy, and yz. 12. Use a K-map to find a minimal expansion as a Boolean sum of Boolean products of each ofthese functions in the variables x, y, and z. a) xyz +xyz b) xyz + xyz + xyz +xyz c) xyz + xyz + xyz + xyz + xyz d) xyz + xyz + xyz + xyz + xyz + xyz 13. a) Draw a K-map for a function in four variables. Put a I in the cell that represents wxyz. b) Which minterms are represented by cells adjacent to this cell? 14. Use a K-map to find a minimal expansion as a Boolean sum of Boolean products of each of these functions in the variables w, x, y, and z. a) wxyz + wxyz + wxyz + wxyz + wxyz b) wxyz + wxyz + wxyz + wxyz + wxyz + wxyz c) wxyz + wxyz + wxyz + wxyz + wxyz + wxyz + wxyz + wxyz d) wxyz + wxyz + wxyz + wxyz + wxyz + wxyz + wxyz + wxyz + wxyz 15. Find the cells in a K-map for Boolean functions with five variables that correspond to each of these products. a) XlX2X3X4 d) X3X4 16. How many cells in a K-map for Boolean functions with six variables are needed to represent Xl, XIX6, XIX2X6, X2X3X4XS, and XIX2X4XS, respectively? 17. a) How many cells does a K-map in six variables have? b) How many cells are adjacent to a given cell in a K -map in six variables? 18. Show that cells in a K-map for Boolean functions in five variables represent minterms that differ in exactly one literal if and only if they are adjacent or are in cells 11-32 that become adjacent when the top and bottom rows and cells in the first and eighth columns, the first and fourth columns, the second and seventh columns, the third and sixth columns, and the fifth and eighth columns are con­ sidered adjacent. 19. Which rows and which columns of a 4 x 16 map for Boolean functions in six variables using the Gray codes 1111,1110,1010,1011,1001,1000,0000,000 1,0011,0010, 0110,0111,0101,0100,1100,1101 to label the columns and 11,10,00,01 to label the rows need to be considered adjacent so that cells that represent minterms that differ in exactly one literal are considered adjacent? *20. Use K-maps to find a minimal expansion as a Boolean sum of Boolean products of Boolean functions that have as input the binary code for each decimal digit and pro­ duce as output a 1 if and only if the digit corresponding to the input is a) odd. b) not divisible by 3. c) not 4, 5, or 6. *21. Suppose that there are five members on a committee, but that Smith and Jones always vote the opposite of Marcus. Design a circuit that implements majority voting of the committee using this relationship between votes. 22. Use the Quine-McCluskey method to simplify the sum­ of-products expansions in Example 3. 23. Use the Quine-McCluskey method to simplify the sum­ of-products expansions in Exercise 12. 24. Use the Quine-McCluskey method to simplify the sum­ of-products expansions in Example 4. 25. Use the Quine-McCluskey method to simplify the sum­ of-products expansions in Exercise 14. *26. Explain how K-maps can be used to simplify product-of­ sums expansions in three variables. [Hint: Mark with a 0 all the maxterms in an expansion and combine blocks of maxterms.] 27. Use the method from Exercise 26 to simplify the product-of-sums expansion (x + y + z)(x + y + Z) (x + y +Z)(x + y +z)(X + y +z). *28. Draw a K-map for the 16 minterms in four Boolean vari­ ables on the surface of a torus. 29. Build a circuit using OR gates, AND gates, and inverters that produces an output of 1 if a decimal digit, encoded using a binary coded decimal expansion, is divisible by 3, and an output of 0 otherwise. In Exercises 30-32 find a minimal sum-of-products expan­ sion, given the K-map shown with don '( care conditions indi­ cated with ds. 30. yz yz yt. yz wx d 1 d 1 wx d d wx d 1 wx 1 d 31. WX.�Z yz yz �z wx d 1 wx 1 d wx d d //-33 32. YZ YZ YZ y, wx d d 1 wx d d 1 d wi Wx 1 1 1 d Key Terms and Results TERMS Boolean variable: a variable that assumes only the values 0 and 1 x (complement of x): an expression with the value I when x has the value 0 and the value 0 when x has the value I X· Y (or xy) (Boolean product or conjunction of x and y): an expression with the value 1 when both x and y have the value 1 and the value 0 otherwise x + y (Boolean sum or disjunction of x and y): an expres­ sion with the value 1 when either x or y, or both, has the value 1, and 0 otherwise Boolean expressions: the expressions obtained recursively by specifying that 0, 1, x" ... , Xn are Boolean expressions and If" (E, + E2), and (E,E2) are Boolean expressions if E, and E2 are dual of a Boolean expression: the expression obtained by interchanging + signs and . signs and interchanging Os and Is Boolean function of degree n: a function from Bn to B where B={O,I} Boolean algebra: a set B with two binary operations v and 1\, elements 0 and 1, and a complementation operator -that satisfies the identity, complement, associative, commuta­ tive, and distributive laws literal of the Boolean variable x: either x or x minterm of x .. X2,'" ,xn: a Boolean product Y,Y2··· Yn, where each Yi is either Xi or Xi sum-of-products expansion (or disjunctive normal form): the representation of a Boolean function as a disjunction of minterms functionally complete: a set of Boolean operators is called functionally complete if every Boolean function can be rep­ resented using these operators x I y (or x NAND y): the expression that has the value 0 when both x and Y have the value 1 and the value 1 otherwise x � y (or x NOR y): the expression that has the value 0 when either x or Y or both have the value 1 and the value 0 other­ wise inverter: a device that accepts the value of a Boolean variable as input and produces the complement of the input Key Terms and Results 781 33. Show that products of k literals correspond to 2n-k_ dimensional subcubes of the n-cube Qn, where the ver­ tices of the cube correspond to the minterms represented by the bit strings labeling the vertices, as described in Example 8 of Section 9.2. OR gate: a device that accepts the values of two or more Boolean variables as input and produces their Boolean sum as output AND gate: a device that accepts the values of two or more Boolean variables as input and produces their Boolean prod­ uct as output half adder: a circuit that adds two bits, producing a sum bit and a carry bit full adder: a circuit that adds two bits and a carry, producing a sum bit and a carry bit K-map for n variables: a rectangle divided into 2n cells where each cell represents a minterm in the variables minimization of a Boolean function: representing a Boolean function as the sum of the fewest products of literals such that these products contain the fewest literals possi­ ble among all sums of products that represent this Boolean function implicant of a Boolean function: a product ofliterals with the property that if this product has the value 1, then the value of this Boolean function is 1 prime implicant of a Boolean function: a product of literals that is an implicant of the Boolean function and no product obtained by deleting a literal is also an implicant of this function essential prime implicant of a Boolean function: a prime implicant of the Boolean function that must be included in a minimization of this function RESULTS The identities for Boolean algebra (see Table 5 in Section 11.1). An identity between Boolean functions represented by Boolean expressions remains valid when the duals of both sides of the identity are taken. Every Boolean function can be represented by a sum-of- products expansion. Each of the sets {+, -} and {., -} is functionally complete. Each of the sets { + } and { I } is functionally complete. The use ofK-maps to minimize Boolean expressions. The Quine-McCluskey method for minimizing Boolean expressions. 782 II/Boolean Algebra Review Questions 1. Define a Boolean function of degree n. 2. How many Boolean functions of degree two are there? 3. Give a recursive definition of the set of Boolean expres­ sions. 4. a) What is the dual of a Boolean expression? b) What is the duality principle? How can it be used to find new identities involving Boolean expressions? 5. Explain how to construct the sum-of-products expansion of a Boolean function. 6. a) What does it mean for a set of operators to be func­ tionally complete? b) Is the set {+, .} functionally complete? c) Are there sets of a single operator that are functionally complete? 7. Explain how to build a circuit for a light controlled by two switches using OR gates, AND gates, and inverters. 8. Construct a half adder using OR gates, AND gates, and inverters. 9. Is there a single type oflogic gate that can be used to build Supplementary Exercises 1. For which values of the Boolean variables x, y, and z does a) x + y + z = xyz? b) x(y + z) = x + yz? c) xyz = x + y + z? 2. Let x and y belong to to, I}. Does it necessarily follow that x = y if there exists a value z in to, I} such that a) xz=yz? b) x+z=y+z? c) x €a z = y €a z? d) x ..J.. z = y ..J.. z? e) x I z = y I z? A Boolean function F is called self-dual if and only if F(xJ, ... ,xn) = F(XJ, ... , xn). 3. Which of these functions are self-dual? a) F(x,y)=x b) F(x,y)=xy+xy c) F(x,y)=x+y d) F(x,y)=xy+xy 4. Give an example of a self-dual Boolean function of three variables. *5. How many Boolean functions of degree n are self-dual? We define the relation :::; on the set of Boolean functions of degree n so that F :::; G, where F and G are Boolean functions if and only if G(x\, X2, ••• ,xn) = I whenever F(x\, X2, ••• , xn) = 1. 6. Determine whether F :::; G or G ::5 F for the following pairs of functions. a) F(x,y)=x,G(x,y)=x+y b) F(x,y) = x + y, G(x,y) = xy c) F(x,y) = x, G(x,y) = x + y //-34 all circuits that can be built using OR gates, AND gates, and inverters? 10. a) Explain how K-maps can be used to simplify sum-of­ products expansions in three Boolean variables. b) Use a K-map to simplify the sum-of-products expan­ sion xyz + xyz + xyz + xyz + xyz. 11. a) Explain how K-maps can be used to simplify sum-of­ products expansions in four Boolean variables. b) Use a K-map to simplify the sum-of-products ex­ pansion wxyz + wxyz + wxyz + wxyz + wxyz + wxyz + wxyz + wxyz + wxyz. 12. a) What is a don't care condition? b) Explain how don't care conditions can be used to build a circuit using OR gates, AND gates, and inverters that produces an output of I if a decimal digit is 6 or greater, and an output of 0 if this digit is less than 6. 13. a) Explain how to use the Quine-McCluskey method to simplify sum-of-products expansions. b) Use this method to simplify xyz + xyz + xyz + xyz+xyz. 7. Show that if F and G are Boolean functions of degree n, then a) F :::; F + G. b) FG:::; F. 8. Show that if F , G, and H are Boolean functions of degree n, then F + G :::; H if and only if F :::; H and G :::; H. *9. Show that the relation:::; is a partial ordering on the set of Boolean functions of degree n. *10. Draw the Hasse diagram for the poset consisting of the set of the 16 Boolean functions of degree two (shown in Table 3 of Section 11.1) with the partial ordering :::;. *11. For each of these equalities either prove it is an identity or find a set of values of the variables for which it does not hold. a) x I (y I z) = (x I y) I z b) x ..J.. (y ..J.. z) = (x ..J.. y) ..J.. (x ..J.. z) c) x ..J.. (y I z) = (x ..J.. y) I (x ..J.. z) Define the Boolean operator <:) as follows: I <:) 1 = 1, I <:) 0 = 0, 0 <:) 1 = 0, and 0 <:) 0 = 1. 12. Show that x <:) y = xy + xy. 13. Show that x <:) y = (x €a y). 14. Show that each of these identities holds. �x<:)x=1 �x<:)x=O c) x<:)y =y<:)x 15. Is it always true that (x <:) y) <:) z = x <:) (y <:) z)? *16. Determine whether the set (<:)} is functionally complete. *17. How many of the 16 Boolean functions in two variables 1/-35 x and y can be represented using only the given set of operators, variables x and y, and values 0 and I? a) {-I b) {.} c) H} d) {-, +} The notation for an XOR gate, which produces the output x $ y from x and y, is as follows: 18. Determine the output of each of these circuits. a) b) 19. Show how a half adder can be constructed using fewer gates than are used in Figure 8 of Section 11.3 whenXOR gates can be used in addition to OR gates, AND gates, and inverters. 20. Design a circuit that determines whether three or more of four individuals on a committee vote yes on an issue, where each individual uses a switch for the voting. � A threshold gate produces an output y that is either 0 or 1 given a set of input values for the Boolean variables XI, X2, ••• , xn• A threshold gate has a threshold value T, which is a real number, and weights WI, W2, ••• , Wn, each of which is a real number. The output y of the threshold gate is 1 if and only if WI XI + W2X2 + ... + WnXn � T. The Computer Projects Write programs with these input and output. 1. Given the values of two Boolean variables x and y, find the values ofx + y, xy, x $ y, x I y, and x .j.. y. 2. Construct a table listing the set of values of all 256 Boolean functions of degree three. 3. Given the values of a Boolean function in n variables, where n is a positive integer, construct the sum-of­ products expansion of this function. 4. Given the table of values of a Boolean function, express this function using only the operators · and -. Computer Projects 783 threshold gate with threshold value T and weights WI, W2, ••• ,Wn is represented by the following diagram. Threshold gates are useful in modeling in neurophysiology and in artificial intelligence. X2 I--.... y 21. A threshold gate represents a Boolean function. Find a Boolean expression for the Boolean function represented by this threshold gate. X2 __ -.! i---..... y 22. A Boolean function that can be represented by a thresh­ old gate is called a threshold function. Show that each of these functions is a threshold function. a) F(x)=x b) F(x,y)=x+y c) F(x,y) =xy d) F(x,y) =X I y e) F(x,y) =x.j.. y t) F(x,y,z) =X + yz g) F(w,x,y,z) = W +xy +Z h) F(w,x,y,z) = wxz +xyz *23. Show that F(x, y) = x $ y is not a threshold function. *24. Show that F(w,x,y,z) = wx + yz is not a threshold function. s. Given the table of values of a Boolean function, express this function using only the operators + and-. *6. Given the table of values of a Boolean function, express this function using only the operator I . *7. Given the table of values ofa Boolean function, express this function using only the operator .j... 8. Given the table of values of a Boolean function of degree three, construct its K-map. 784 II/Boolean Algebra 9. Given the table of values of a Boolean function of degree four, construct its K-map. '10. Given the table of values of a Boolean function, use the Quine-McCluskey method to find a minimal sum-of­ products representation of this function. Compu tations and Explorat ions 11. Given a threshold value and a set of weights for a thresh­ old gate and the values of the n Boolean variables in the input, determine the output of this gate. 12. Given a positive integer n, construct a random Boolean expression in n variables in disjunctive normal form. Use a computational program or programs you have written to do these exercises. 1. Compute the number of Boolean functions of degrees seven, eight, nine, and ten. 2. Construct a table of the Boolean functions of degree three. 3. Construct a table of the Boolean functions of degree four. 4. Express each of the different Boolean expressions in three variables in disjunctive normal form with just the NAND operator, using as few NAND operators as possible. What is the largest number of NAND operators required? 5. Express each of the different Boolean expressions in dis­ junctive normal form in four variables using just the NOR Writing Projects operator, with as few NOR operators as possible. What is the largest number of NOR operators required? 6. Randomly generate 10 different Boolean expressions in four variables and determine the average number of steps required to minimize them using the Quine-McCluskey method. 7. Randomly generate 10 different Boolean expressions in five variables and determine the average number of steps required to minimize them using the Quine-McCluskey method. Respond to these questions with essays using outside sources. 1. Describe some of the early machines devised to solve problems in logic, such as the Stanhope Demonst rator, Jevons's Logic Machine, and the Marquand Machine. 2. Explain the difference between combinational circuits and sequential circuits. Then explain how flip-flops are used to build sequential circuits. 3. Define a shift register and discuss how shift registers are used. Show how to build shift registers using flip-flops and logic gates. 4. Show how multipliers can be built using logic gates. 5. Find out how logic gates are physically constructed. Dis­ cuss whether NAND and NOR gates are used in building circuits. 6. Explain how dependency notation can be used to describe complicated switching circuits. 7. Describe how multiplexers are used to build switching circuits. 8. Explain the advantages of using threshold gates to con­ struct switching circuits. Illustrate this by using threshold gates to construct half and full adders. 9. Describe the concept of hazard-Jree switching circuits and give some of the principles used in designing such circuits. 10. Explain how to use K-maps to minimize functions of six variables. 11. Discuss the ideas used by newer methods for minimiz­ ing Boolean functions, such as Espresso. Explain how these methods can help solve minimization problems in as many as 25 variables. 12. Describe what is meant by the functional decompo­ sition of a Boolean function of n variables and dis­ cuss procedures for decomposing Boolean functions into a composition of Boolean functions with fewer variables. CHAPTER 12.1 Languages and Grammars 12.2 Finite-State Machines with Output 12.3 Finite-State Machines with No Output 12.4 Language Recognition 12.5 Turing Machines Modeling Computation <C omputers can perform many tasks. Given a task, two questions arise. The first is: Can it be carried out using a computer? Once we know that this first question has an affirmative answer, we can ask the second question: How can the task be carried out? Models of computation are used to help answer these questions. We will study three types of structures used in models of computation, namely, grammars, finite-state machines, and Turing machines. Grammars are used to generate the words of a language and to determine whether a word is in a language. Formal languages, which are generated by grammars, provide models for both natural languages, such as English, and for programming languages, such as Pascal, Fortran, Prolog, C, and Java. In particular, grammars are extremely important in the construction and theory of compilers. The grammars that we will discuss were first used by the American linguist Noam Chomsky in the 1950s. Various types of finite-state machines are used in modeling. All finite-state machines have a set of states, including a starting state, an input alphabet, and a transition function that assigns a next state to every pair of a state and an input. The states of a finite-state machine give it limited memory capabilities. Some finite-state machines produce an output symbol for each transition; these machines can be used to model many kinds of machine s, including vending machines, delay mac hines, binary adders, and language recognizers. We will also study finite­ state machines that have no output but do have final states. Such machines are extensively used in language recognition. The strings that are recognized are those that take the starting state to a final state. The concepts of grammars and finite-state machines can be tied together. We will characterize those sets that are recognized by a finite-state machine and show that these are precisely the sets that are generated by a certain type of grammar. Finally, we will introduce the concept of a Turing machine . We will show how Turing machines can be used to recognize sets. We will also show how Turing machines can be used to compute number-theoretic functions. We will discuss the Church-Turing thesis, which states that every effective computation can be carried out using a Turing machine . We will explain how Turing machines can be used to study the difficulty of solving certain classes of problems. In particular, we will describe how Turing machines are used to classify problems as tractable versus intractable and solvable versus unsolvable. 12.1 Languages and Grammars 12-1 Introduction Words in the English language can be combined in various ways. The grammar of English tells us whether a combination of words is a valid sentence. For instance, the frog writes neatly is a valid sentence, because it is formed from a noun phrase, the frog, made up of the article the and the nounfrog, followed by a verb phrase, writes neatly, made up of the verb writes and the adverb neatly. We do not care that this is a nonsensical statement, because we are concerned only with the syntax, or form, of the sentence, and not its semantics, or meaning. We also note that the combination of words swims quickly mathemat ics is not a valid sentence because it does not follow the rules of English grammar. The syntax of a natural language, that is, a spoken language, such as English, French, German, or Spanish, is extremely complicated. In fact, it does not seem possible to specify all 785 786 12 / Modeling Computation 12-2 the rules of syntax for a natural language. Research in the automatic translation of one language to another has led to the concept of a formal language, which, unlike a natural language, is specified by a well-defined set of rules of syntax. Rules of syntax are important not only in linguistics, the study of natural language s, but also in the study of programming language s. We will describe the sentences of a formal language using a grammar. The use of grammars helps when we consider the two classes of problems that arise most frequently in applications to programming languages: (1) How can we determine whether a combination of words is a valid sentence in a formal language? (2) How can we generate the valid sentences of a formal language? Before giving a technical definition of a grammar, we will describe an example of a grammar that generates a subset of English. This subset of English is defined using a list of rules that describe how a valid sentence can be produced. We specify that 1. a sentence is made up of a noun phrase followed by a verb phrase; 2. a noun phrase is made up of an article followed by an adjective followed by a noun, or 3. a noun phrase is made up of an article followed by a noun; 4. a verb phrase is made up of a verb followed by an adverb, or 5. a verb phrase is made up of a verb; 6. an article is a, or 7. an article is the; 8. an adjective is large, or 9. an adjective is hungry; 10. a noun is rabbit, or 11. a noun is mathematician; 12. a verb is eats, or 13. a verb is hops; 14. an adverb is quickly, or 15. an adverb is wildly. From these rules we can form valid sentences using a series of replacements until no more rules can be used. For instance, we can follow the sequence of replacemen ts: sentence noun phrase verb phrase article adjective noun verb phrase article adjective noun verb adverb the adjective noun verb adverb the large noun verb adverb the large rabbit verb adverb the large rabbit hops adverb the large rabbit hops quickly to obtain a valid sentence. It is also easy to see that some other valid sentences are: a hungry mathemat ician eats wildly, a large mathematician hops, the rabbit eats quickly, and so on. Also, we can see that the quickly eats mathematician is not a valid sentence. Phrase-Structure Grammars Before we give a formal definition of a grammar, we introduce a little terminology. 12-3 12.1 Languages and Grammars 787 DEFINITION 1 A vocabulary (or alphabet) V is a finite, nonempty set of elements called symbols. A word (or sentence) over V is a string of finite length of elements of V. The empty string or null string, denoted by A, is the string containing no symbols. The set of all words over V is denoted by V*. A language over V is a subset of V* . Note that A, the empty string, is the string containing no symbols. It is different from 0, the empty set. It follows that {A} is the set containing exactly one string, namely, the empty string. Languages can be specified in various ways. One way is to list all the words in the language. Another is to give some criteria that a word must satisfy to be in the language. In this section, we describe another important way to specify a language, namely, through the use of a grammar, such as the set of rules we gave in the introduction to this section. A grammar provides a set of symbols of various types and a set of rules for producing words. More precisely, a grammar has a vocabulary V, which is a set of symbols used to derive members of the language. Some of the elements of the vocabulary cannot be replaced by other symbols. These are called terminals, and the other members of the vocabulary, which can be replaced by other symbols, are called nonterminals. The sets of terminals and nonterminals are usually denoted by T and N, respectively. In the example given in the introduction of the section, the set of terminals is {a, the, rabbit, mathematician, hops, eats, quickly, wildly}, and the set of nonterminals is {sentence, noun phrase, verb phrase, adjective, article, noun, verb, adverb}. There is a special member of the vocabulary called the start symbol, denoted by S, which is the element of the vocabulary that we always begin with. In the example in the introduction, the start symbol is sentence. The rules that specify when we can replace a string from V*, the set of all strings of elements in the vocabulary, with another string are called the productio ns of the grammar. We denote by Zo -+ ZI the production that specifies that Zo can be replaced by ZI within a string. The productions in the grammar given in the introduction of this section were listed. The first production, written using this notation, is sentence -+ noun phrase verb phrase. We summarize with the following definition. DEFINITION 2 A phrase-structur e grammar G = (V, T, S, P) consists ofa vocabulary V, a subset T of V consisting of terminal elements, a start symbol S from V, and a finite set of productions P. The set V -T is denoted by N. Elements of N are called nonterminal symbols. Every production in P must contain at least one nonterminal on its left side. EXAMPLE 1 Let G = (V, T, S, P), where V = {a, b, A, B, S}, T = {a, b}, S is the start symbol, and P = {S -+ ABa, A -+ BB, B -+ ab, AB -+ b}. G is an example ofa phrase-structure grammar . .... DEFINITION 3 We will be interested in the words that can be generated by the productions of a phrase­ structure grammar. Let G = (V, T, S, P) be a phrase-structure grammar. Let Wo = lzor (that is, the concatena­ tion of I, zo, andr) and WI = IZlr be strings over V. Ifzo -+ ZI is a production ofG, we say that WI is directly derivable from Wo and we write Wo => WI. If Wo, WI, ••• , W n are strings over V such that Wo => WI, WI => W2, •.. , Wn-I => Wn, then we say that Wn is derivable from Wo, and we write Wo � Wn. The sequence of steps used to obtain Wn from Wo is called a derivation. 788 12/ Modeling Computation 12-4 EXAMPLE 2 The string Aaba is directly derivable from ABa in the grammar in Example because B � ab is a production in the grammar. The string abababa is derivable from ABa because ABa => Aaba => BBaba => Bababa =>abababa, using the productions B � ab, A � BB, B � ab, and B � ab in succession. <III DEFINITION 4 Let G = (V, T, S, P) be a phrase-st ructure grammar. The language generated by G (or the language olG), denoted by L(G), is the set of all strings of terminals that are derivable from the starting state S. In other words, L(G) = {w E T* I S � w}. In Examples 3 and 4 we find the language generated by a phrase-st ructure grammar. EXAMPLE 3 Let G be the grammar with vocabulary V = IS, A, a, b}, set of terminals T = {a, b}, starting symbol S, and productions P = {S � aA, S � b, A � aa}. What is L(G), the language of this grammar? Solution: From the start state S we can derive aA using the production S � aA. We can also use the production S � b to derive b. From aA the production A � aa can be used to derive aaa. No additional words can be derived. Hence, L(G) = {b, aaa}. <III EXAMPLE 4 Let G be the grammar with vocabulary V = {S,O, l},setofterminals T = to, l},startingsym­ bol S, and productions P = {S � lIS, S � OJ. What is L(G), the language of this grammar? Solution: From S we can derive 0 using S � 0, or 11 S using S � 11 S. From 11 S we can derive either 110 or 1111 S. From 1111 S we can derive 11110 and 111111 S. At any stage of a derivation we can either add two 1 s at the end of the string or terminate the derivation by adding a 0 at the end of the string. We surmise that L(G) = to, 110, 11110, 1111110, ... }, the set of all strings that begin with an even number of Is and end with a O. This can be proved using an inductive argument that shows that after n productions have been used, the only strings of terminals generated are those consisting of n -1 or fewer concatenations of 11 followed by o. (This is left as an exercise for the reader.) <III The problem of constructing a grammar that generates a given language often arises. Ex­ amples 5, 6, and 7 describe problems of this kind. EXAMPLE 5 Give a phrase- structure grammar that generates the set {on 1 n I n = 0, 1, 2, ... }. Solution: Two productions can be used to generate all strings consisting of a string of Os followed by a string of the same number of Is, including the null string. The first builds up successively longer strings in the language by adding a 0 at the start of the string and a 1 at the end. The second production replaces S with the empty string, The solqtiPIl is the grammar G = (V, T, S, P), where V = to, 1, S}, T = to, I}, S is the starting symbol, and the productions are S� OSl S�J.... The verification that this grammar generates the correct set is left as an exercise for the reader. <III 12-5 12.1 Languages and Grammars 789 Example 5 involved the set of strings made up of Os followed by 1 s, where the number of Os and Is are the same. Example 6 considers the set of strings consisting of Os followed by Is, where the number of Os and 1 s may differ. EXAMPLE 6 Find a phrase-structure grammar to generate the set {om I n I m and n are nonnegative integers}. Solution: We will give two grammars GI and G2 that generate this set. This will illustrate that two grammars can generate the same language. The grammar GI has alphabet V = {S, 0, I}; terminals T = to, I}; and productions S -+ OS, S -+ SI, and S -+ A. GI generates the correct set, because using the first production m times puts m Os at the beginning of the string, and using the second production n times puts n 1 s at the end of the string. The details of this verification are left to the reader. The grammar G2 has alphabet V = {S, A, 0, I}; terminals T = to, I}; and productions S -+ OS, S -+ lA, S -+ 1, A -+ lA, A -+ 1, and S -+ A. The details that this grammar gen­ erates the correct set are left as an exercise for the reader. � Sometimes a set that is easy to describe can be generated only by a complicated grammar. Example 7 illustrates this. EXAMPLE 7 One grammar that generates the set {onln2n In = 0,I,2,3, ... } is G = (V, T, S, P) with V = to, 1,2, S, A, B, C}; T = to, 1, 2}; starting state S; and productions S -+ C, C -+ OCAB, S -+ A, BA -+ AB, OA -+ 01, IA -+ 11, IB -+ 12, and 2B -+ 22. We leave it as an exercise for the reader (Exercise 12 at the end of this section) to show that this statement is correct. The grammar given is the simplest type of grammar that generates this set, in a sense that will be made clear later in this section. � unks� Types of Phrase-Structure Grammars Phrase-st ructure grammars can be classified according to the types of productions that are allowed. We will describe the classification scheme introduced by Noam Chomsky. In Section 12.4 we will see that the different types of languages defined in this scheme correspond to the classes of languages that can be recognized using different models of computing machines. A type 0 grammar has no restrictions on its productions. A type 1 grammar can have productions of the form WI -+ W2, where WI = IAr and W2 = Iwr, where A is a nonterminal symbol, I and r are strings of zero or more terminal or nonterminal symbols, and W is a nonempty string of terminal or nonterminal symbols. It can also have the production S -+ A as long as S does not appear on the right-hand side of any other production. A type 2 grammar can have productions only of the form WI -+ W2, where WI is a single symbol that is not a terminal symbol. A type 3 grammar can have productions only of the form WI -+ W2 with WI = A and either W2 = a B or W2 = a, where A and B are nonterminal symbols and a is a terminal symbol, or with WI = Sand W2 = A. Type 2 grammars are called context-free grammars because a nonterminal symbol that is the left side of a production can be replaced in a string whenever it occurs, no matter what else is in the string. A language generated by a type 2 grammar is called a context-free language. When there is a production of the form IWIr -+ IW2r (but not of the form WI -+ W2), the grammar is called type 1 or context-sensitive because WI can be replaced by W2 only when it is surrounded by the strings I and r. A language generated by a type 1 grammar is called a context-sensitive language. Type 3 grammars are also called regular grammars. A language generated by a regular grammar is called regular. Section 12.4 deals with the relationship between regular languages and finite-state machines. Of the four types of grammars we have defined, context-sensitive grammars have the most complicated definition. Sometimes, these grammars are defined in a different way. A production 790 12/ Modeling Computation 12-6 of the fonn WI -+ W2 is called noncontracting if the length of WI is less than or equal to the length of W2. According to our characterization of context-senstive languages, every production in a type 1 grammar, other than the production S -+ A, ifit is present, is noncontracting. It follows that the lengths of the strings in a derivation in a context-free language are nondecreasing unless the production S -+ A is used. This means that the only way for the empty string to belong to the language generated by a context-sensitive grammar is for the production S -+ A to be part of the. grammar. The other way that context-sensitive grammars are defined is by specifying that all productions are noncontracting. A grammar with this property is called noncontracting or monotonic. The class of noncontracting grammars is not the same as the class of context­ sensitive grammars. However, these two classes are closely related; it can be shown that they define the same set of languages except that noncontracting grammars cannot generate any language containing the empty string A. EXAMPLE 8 From Example 6 we know that {om 1 n 1m, n = 0, 1, 2, ... } is a regular language, because it can ExIra � be generated by a regular g rammar, namely, the grammar G2 in Example 6. .... Examples� Context-free and regular grammars. play an important role in programming languages. Context-free grammars are used to define the syntax of almost all programming languages. These grammars are strong enough to define a wide range of languages. Furthennore, efficient algorithms can be devised to detennine whether and how a string can be generated. Regular grammars are used to search text for certain patterns and in lexical analysis, which is the process of transfonning an input stream into a stream of tokens for use by a parser. EXAMPLE 9 It follows from Example 5 that {onln In = 0,1,2, ... } is a context-free language, because the productions in this grammar are S -+ OSI and S -+ A. However, it is not a regular language. This will be shown in Section 12.4. .... EXAMPLE 10 The set {onln2n In = 0,1,2, ... } is a context-sensitive language, because it can be generated by a type 1 grammar, as Example 7 shows, but not by any type 2 language. (This is shown in Exercise 28 in the supplementary exercises at the end of the chapter.) .... Table 1 summarizes the tenninology used to classify phrase-structure grammars. Derivation Trees A derivation in the language generated by a context-free grammar can be represented graphically using an ordered rooted tree, called a derivation, or parse, tree. The root of this tree represents the starting symbol. The internal vertices of the tree represent the nontenninal symbols that arise in the derivation. The leaves of the tree represent the tenninal symbols that arise. If the production A -+ W arises in the derivation, where W is a word, the vertex that represents A has as children vertices that represent each symbol in w, in order from left to right. TABLE 1 Types of Grammars. Type Restrictions on Productions Wl --+ W2 0 No restrictions 1 WI = lAr and W2 = lwr, where A E N, I, r, WE (N U T)* and W =F i..; or WI = S and W2 = i.. as long as S is not on the right-hand side of another production 2 WI = A, where A is a nonterminal symbol 3 WI = A and W2 = aB or W2 = a, where A E N, BEN, and a E T; or WI = Sand W2 = i.. 12-7 sentence /� noun phrase verb phrase /I� /\ article adjective noun verb adverb I I I I I the hungry rabbit eats FIGURE 1 A Derivation Tree. quickly 12.1 Languages and Grammars 791 EXAMPLE 11 Construct a derivation tree for the derivation of the hungry rabbit eats quickly, given in the introduction of this section. Solution: The derivation tree is shown in Figure 1. The problem of determining whether a string is in the language generated by a context-f ree grammar arises in many applications, such as in the construction of compilers. Two approaches to this problem are indicated in Example 12. EXAM PLE 12 Determine whether the word cbab belongs to the language generated by the grammar G = (V, T, S, P), where V = {a, b, c, A, B, C, S}, T = {a, b, c}, S is the starting symbol, and the productions are lin!iS� S-+ AB A -+ Ca B -+ Ba B -+ Cb B-+b C -+ cb C -+ b. Solution: One way to approach this problem is to begin with S and attempt to derive cbab using a series of productions. Because there is only one production with S on its left-hand side, we must start with S => AB. Next we use the only production that has A on its left-hand side, namely, A -+ Ca, to obtain S => AB => CaB. Because cbab begins with the symbols cb, we use the production C -+ cb. This gives us S => Ab => CaB => cbaB. We finish by using the production B -+ b, to obtain S => AB => CaB => cbaB => cbab. The approach that we have used is called top­ down parsing, because it begins with the starting symbol and proceeds by successively applying productions. There is another approach to this problem, called bottom-up parsing. In this approach, we work backward. Because cbab is the string to be derived, we can use the production C -+ cb, so AVRAM NOAM CHoMSKY (BORN 1928) Noam Chomsky, born in Philadelphia, is the son ofa Hebrew scholar. He received his B.A., M.A., and Ph.D. in linguistics, all from the University of Pennsylvania. He was on the staff of the University of Pennsylvania from 1950 until 1951. In 1955 he joined the faculty at M.1. T., beginning his M.I. T. career teaching engineers French and German. Chomsky is currently the Ferrari P. Ward Professor of foreign languages and linguistics at M.1. T. He is known for his many fundamental contributions to linguistics, including the study of grammars. Chomsky is also widely known for his outspoken political activism. 792 12 / Modeling Computation 12-8 that Cab => cbab. Then, we can use the production A -+ Ca, so that Ab => Cab => cbab. Using the production B -+ b gives AB => Ab => Cab => cbab. Finally, using S -+ AB shows that a complete derivation for cbab is S => AB => Ab => Cab => cbab. ... Backus-Naur Form There is another notation that is sometimes used to specify a type 2 grammar, called the Backus-Naur form (BNF), after John Backus, who invented it, and Peter Naur, who refined it for use in the specification of the programming language ALGOL. (Surprisingl y, a notation quite similar to the Backus-Naur form was used approximately 2500 years ago to describe the gram­ mar of Sanskrit.) The Backus-Naur form is used to specify the syntactic rules of many computer languages, including Java. The productions in a type 2 grammar have a single nonterminal sym­ bol as their left-hand side. Instead of listing all the productions separately, we can combine all those with the same nonterminal symbol on the left-hand side into one statement. Instead of using the symbol -+ in a production, we use the symbol ::=. We enclose all nonterminal symbols in brackets, ( ), and we list all the right-hand sides of productions in the same statement, separating them by bars. For instance, the productions A -+ Aa, A -+ a, and A -+ AB can be combined into (A) ::= (A)a I a I (A) (B). Example 13 illustrates how the Backus-Naur form is used to describe the syntax of pro­ gramming languages. Our example comes from the original use of Backus-Naur form in the description of ALGOL 60. JOHN BACKUS (BORN 1924) John Backus was born in Philadelphia and grew up in Wilmington, Delaware. He attended the Hill School in Pottstown, Pennsylvania. He needed to attend summer school every year because he disliked studying and was not a serious student. But he enjoyed spending his summers in New Hampshire where he attended summer school and amused himself with summer activities, including sailing. He obliged his father by enrolling at the University of Virginia to study chemistry. But he quickly decided chemistry was not for him, and in 1943 he entered the army, where he received medical training and worked in a neurosurgery ward in an army hospital. Ironically, Backus was soon diagnosed with a bone tumor in his skull and was fitted with a metal plate. His medical work in the army convinced him to try medical school, but he abandoned this after nine months because he disliked the rote memorization required. After dropping out of medical school, he entered a school for radio technicians because he wanted to build his own high fidelity set. A teacher in this school recognized his potential and asked him to help with some mathematical calculations needed for an article in a magazine. Finally, Backus found what he was interested in: mathematics and its applications. He enrolled at Columbia University, from which he received both bachelor's and master's degrees in mathemati cs. Backus joined IBM as a programmer in 1950. He participated in the design and development of two of IBM's early computers. From 1954 to 1958 he led the IBM group that developed FORTRAN. Backus became a staff member at the IBM Watson Research Center in 1958. He was part of the committees that designed the programming language ALGOL, using what is now called the Backus-Naur form for the description of the syntax of this language. Later, Backus worked on the mathematics of families of sets and on a functional style of programming. Backus became an IBM Fellow in 1963, and he received the National Medal of Science in 1974 and the prestigious Turing Award from the Association of Computing Machinery in 1977. PETER NAUR (BORN 1928) Peter Naur was born in Frederiksberg, near Copenhagen. As a boy he became interested in astronomy. Not only did he observe heavenly bodies, but he also computed the orbits of comets and asteroids. Naur attended Copenhagen University, receiving his degree in 1949. He spent 1950 and 1951 in Cambridge, where he used an early computer to calculate the motions of comets and planets. After returning to Denmark he continued working in astronomy but kept his ties to computing. In 1955 he served as a consultant to the building of the first Danish computer. In 1959 Naur made the switch from astronomy to computing as a full-time activity. His first job as a full-time computer scientist was participating in the development of the programming language ALGOL. From 1960 to 1967 he worked on the development of compilers for ALGOL and COBOL. In 1969 he became professor of computer science at Copenhagen University, where he has worked in the area of programming methodology. His research interests include the design, structure, and performance of computer programs. Naur has been a pioneer in both the areas of software architecture and software engineering. He rejects the view that computer programming is a branch of mathematics and prefers that computer science be called datalogy. 12-9 EXAMPLE 13 Exam=� 12.1 Languages and Grammars 793 In ALGOL 60 an identifier (which is the name of an entity such as a variable) consists of a string of alphanumeric characters (that is, letters and digits) and must begin with a letter. We can use these rules in Backus-Naur to describe the set of allowable identifiers: (identifier) ::= (letter) I (identifier) (letter) I (identifier) (digit) (letter) ::= a I b I ... I y I z the ellipsis indicates that all 26 letters are included (digit) ::= 0 I 1 I 2 I 3 I 4 I 5 I 6 I 7 I 8 I 9 For example, we can produce the valid identifier x99a by using the first rule to replace (identifier) by (identifier) (letter), the second rule to obtain (identifier) a , the first rule twice to obtain (identifier) (digit) (digit) a, the third rule twice to obtain (identifier)99a, the first rule to obtain (letter)99a, and finally the second rule to obtain x99a. .... EXAMPLE 14 What is the Backus-Naur form of the grammar for the subset of English described in the introduction to this section? Solution: The Backus-Naur form of this grammar is (sentence) ::= (noun phrase) (verb phrase) (noun phrase) ::= (article) (adjective) (noun) I (article) (noun) (verb phrase) ::= (verb) (adverb) I (verb) (article) ::= a I the (adjective) ::= large I hungry (noun) ::= rabbit I mathematician (verb) ::= eats I hops (adverb) ::= quickly I wildly EXAMPLE 15 Give the Backus-Naur form for the production of signed integers in decimal notation. (A signed integer is a nonnegative integer preceded by a plus sign or a minus sign.) Exercises Solution: The Backus-Naur form for a grammar that produces signed integers is (signed integer) ::= (sign) (integer) (sign) ::= + I - (integer) ::= (digit) I (digit) (integer) (digit) ::= 0 I 1 I 2 I 3 I 4 I 5 I 6 I 7 I 8 I 9 The Backus-Naur form, with a variety of extensions, is used extensively to specify the syntax of programming languages, such as Java and LISP; database languages, such as SQL; and markup languages, such as XML. Some extensions of the Backus-Naur form that are commonly used in the description of programming languages are introduced in the preamble to Exercise 34 at the end of this section. Exercises 1-3 refer to the grammar with start symbol sen­ tence, set of terminals T = {the, sleepy, happy, tortoise, hare, passes, runs, quickly, slowly}, set of non terminals N = {noun phrase, transitive verb phrase, intransitive verb phrase, article, adjective, noun, verb, adverb}, and producti ons: sentence ... noun phrase intransitive verb phrase noun phrase ... article adjective noun noun phrase ... article noun transitive verb phrase ... transitive verb intransitive verb phrase ... intransitive verb adverb sentence ... noun phrase transitive verb phrase noun phrase intransitive verb phrase ... intransitive verb article ... the 794 12/ Modeling Computation adjective � sleepy adjective � happy noun � tortoise noun � hare transitive verb � passes intransitive verb � runs adverb � quickly adverb � slowly 1. Use the set of productions to show that each of these sen­ tences is a valid sentence. a) the happy hare runs b) the sleepy tortoise runs quickly c) the tortoise passes the hare d) the sleepy hare passes the happy tortoise 2. Find five other valid sentences, besides those given in Exercise 1. 3. Show that the hare runs the sleepy tortoise is not a valid sentence. 4. Let G = (V, T, S, P) be the phrase-structure grammar with V = {O, I, A, S}, T = {O, I}, and set of produc­ tions P consisting of S � IS, S � OOA, A � OA, and A�O. a) Show that 111000 belongs to the language generated byG. b) Show that 11001 does not belong to the language gen­ erated by G. c) What is the language generated by G? 5. Let G = (V, T, S, P) be the phrase-st ructure grammar with V = {O, 1, A, B, S}, T = {O, I}, and set of pro­ ductions P consisting of S � OA, S � lA, A � OB, B � lA,B � 1. a) Show that 10101 belongs to the language generated byG. b) Show that 10110 does not belong to the language gen­ erated by G. c) What is the language generated by G? *6. Let V = IS, A, B, a, b} and T = {a, b}. Find the lan­ guage generated by the grammar (V, T, S, P) when the set P of productions consists of a) S � AB, A � ab,B � bb. b) S � AB, S � aA, A � a, B � ba. c) S � AB, S � AA, A � aB, A � ab, B � b. d) S � AA, S � B, A � aaA, A � aa, B � bB, B�b. e) S � AB, A � aAb, B � bBa, A � A, B � A. 7. Construct a derivation of 0313 using the grammar given in Example 5. 8. Show that the grammar given in Example 5 generates the set {on In In = 0,1,2, ... }. 9. a) Construct a derivation of0214 using the grammar G1 in Example 6. b) Construct a derivation of 0214 using the grammar G2 in Example 6. 12-10 10. a) Show that the grammar Gl given in Example 6 gen­ erates the set {O'" In 1m, n = 0,1,2, ... }. b) Show that the grammar G2 in Example 6 generates the same set. -11. Construct a derivation of 021222 in the grammar given in Example 7. *12. Show that the grammar given in Example 7 generates the set {on In2n I n = 0,1,2, ... }. 13. Find a phrase-structure grammar for each of these language s. a) the set consisting of the bit strings 0, 1, and 11 b) the set of bit strings containing only Is c) the set of bit strings that start with 0 and end with 1 d) the set of bit strings that consist of a 0 followed by an even number of 1 s 14. Find a phrase-structure grammar for each of these languages. a) the set consisting of the bit strings 10,01, and 101 b) the set of bit strings that start with 00 and end with one or more 1 s c) the set of bit strings consisting of an even number of Is followed by a final 0 d) the set of bit strings that have neither two consecutive Os nor two consecutive 1 s *15. Find a phrase-st ructure grammar for each of these languages. a) the set of all bit strings containing an even number of Os and no Is b) the set of all bit strings made up of a 1 followed by an odd number of Os c) the set of all bit strings containing an even number of Os and an even number of 1 s d) the set of all strings containing 10 or more Os and no Is e) the set of all strings containing more Os than Is f) the set of all strings containing an equal number of Os andls g) the set of all strings containing an unequal number of Os and Is 16. Construct phrase-structure grammars to generate each of these sets. a) Win?: O} b) {Ion In?: O} c) {(ll)n In?: O} 17. Construct phrase-structure grammars to generate each of these sets. a) Win?: O} b) WO In?: O} c) {(ooo)n In?: O} 18. Construct phrase-st ructure grammars to generate each of these sets. a) {OI2n In?: O} b) W12n I n ?:O} c) {onlmon 1m?: Oandn?: O} 19. Let V = IS, A, B, a, b} and T = {a, b}. Determine whether G = (V, T, S, P) is a type 0 grammar but not a type 1 grammar, a type 1 grammar but not a type 2 /2-/ / grammar, or a type 2 grammar but not a type 3 grammar if P, the set of productions, is a) S � aAB, A � Bb, B � A. b) S � aA, A � a, A � b. c) S � ABa, AB � a. d) S � ABA, A � aB, B � abo e) S � bA, A � B, B � a. t) S � aA, aA � B, B � aA, A � b. g) S � bA, A � b, S � A. h) S � AB, B � aAb,aAb � b. i) S � aA, A � bB, B � b, B � A. j) S � A, A � B, B � A. 20. A palindrome is a string that reads the same backward as it does forward, that is, a string w, where w = w R, where wR is the reversal of the string w. Find a context-free grammar that generates the set of all palindromes over the alphabet {O, I}. *21. Let Gl and G2 be context-free grammars, generating the languages L( G 1) and L( G2), respectively. Show that there is a context-free grammar generating each of these sets. a) L(Gl) U L(G2) b) L(GdL(G2) c) L(G1)* 22. Find the strings constructed using the derivation trees shown here. sentence /� noun phrase verb phrase /I� /\ article adjective noun verb adverb I I I I I a large mathemati cian hops signed integer /"'" sign integer I /� + digit integer wildly /� 9 digit integer I 8 digit I 7 23. Construct derivation trees for the sentences in Exercise 1. 24. Let G be the grammar with V = {a, b, e, S}; T = {a, b, e}; starting symbol S; and productions S � abS, 12.1 Languages and Grammars 795 S � beS, S � bbS, S � a, and S � eb. Construct derivation trees for a) bebba. b) bbbebba. c) beabbbbbeb. *25. Use top-down parsing to determine whether each of the following strings belongs to the language generated by the grammar in Example 12. a) baba b) abab c) ebaba d) bbbeba *26. Use bottom-up parsing to determine whether the strings in Exercise 25 belong to the language generated by the grammar in Example 12. 27. Construct a derivation tree for -109 using the grammar given in Example 15. 28. a) Explain what the productions are in a grammar ifthe Backus-Naur form for productions is as follows: (expression) ::= «(expression}) I (expression) + (expression) I (expression) * (expression) I (variable) (variable) ::= x I y b) Find a derivation tree for (x * y) + x in this grammar. 29. a) Construct a phrase-structure grammar that generates all signed decimal numbers, consisting of a sign, either + or -; a nonnegative integer; and a decimal fraction that is either the empty string or a decimal point fol­ lowed by a positive integer, where initial zeros in an integer are allowed. b) Give the Backus-Naur form of this grammar. c) Construct a derivation tree for -31.4 in this grammar. 30. a) Construct a phrase-structure grammar for the set of all fractions of the form alb, where a is a signed integer in decimal notation and b is a positive integer. b) What is the Backus-Naur form for this grammar? c) Construct a derivation tree for +311 / 17 in this grammar. 31. Give production rules in Backus-Naur form for an iden­ tifier if it can consist of a) one or more lowercase letters. b) at least three but no more than six lowercase letters. c) one to six uppercase or lowercase letters beginning with an uppercase letter. d) a lowercase letter, followed by a digit or an under­ score, followed by three or four alphanumeric charac­ ters (lower or uppercase letters and digits). 32. Give production rules in Backus-Naur form for the name of a person if this name consists of a first name, which is a string of letters, where only the first letter is uppercase; a middle initial; and a last name, which can be any string ofletters. 33. Give production rules in Backus-Naur form that gener­ ate all identifiers in the C programming language. In C 796 12/ Modeling Computation an identifier starts with a letter or an underscore (_) that is followed by one or more lowercase letters, uppercase letters, underscores, and digits. � Several extensions to Backus-Naur form are commonly used to define phrase-structure grammars. In one such extension, a question mark (?) indicates that the symbol, or group of symbols inside parentheses, to its left can appear zero or once (that is, it is optional), an asterisk (*) indicates that the symbol to its left can appear zero or more times, and a plus (+) indi­ cates that the symbol to its left can appear one or more times. These extensions are part of extended Backus-Naur form (EBNF), and the symbols ?, *, and + are called metacharac­ ters. In EBNF the brackets used to denote nonterminals are usually not shown. 34. Describe the set of strings defined by each of these sets of productions in EBNF. a) string ::= L+D?L+ L ::=a I b I c D ::=0 11 b) string ::= sign D+ I D+ sign ::= + 1- D ::= 0 I 1 I 2 I 3 I 4 I 5 I 6 I 7 I 8 I 9 c) string ::= L*(D+)? L* L ::=x Iy D ::=0 11 35. Give production rules in extended Backus-Naur form that generate all decimal numerals consisting of an optional sign, a nonnegative integer, and a decimal fraction that is either the empty string or a decimal point followed by an optional positive integer optionally preceded by some number of zeros. 36. Give production rules in extended Backus-Naur form that generate a sandwich if a sandwich consists of a lower slice of bread; mustard or mayonnaise; optional lettuce; an op­ tional slice oftomato; one or more slices of either turkey, chicken, or roast beef (in any combination); optionally some number of slices of cheese; and a top slice of bread. 12-12 37. Give production rules in extended Backus-Naur form for identifiers in the C programming language (see Exercise 33). 38. Describe how productions for a grammar in extended Backus-Naur form can be translated into a set of pro­ ductions for the grammar in Backus-Naur form. This is the Backus-Naur form that describes the syntax of expressions in postfix (or reverse Polish) notation. (expression) ::= (term) I (term)(term)(addOperator) (addOperator) ::= + I - (term) ::= ljactor) Iljactor)ljactor)(muIOperator) (muIOperator) ::= * I I ljactor) ::= (identifier) I (expression) (identifier) ::= a I b I ... I z 39. For each of these strings, determine whether it is gener­ ated by the grammar given for postfix notation. If it is, find the steps used to generate the string a) abc*+ b) xy++ c) xy-z* d) wxyz-*I e) ade-* 40. Use Backus-Naur form to describe the syntax of expres­ sions in infix notation, where the set of operators and iden­ tifiers is the same as in the BNF for postfix expressions given in the preamble to Exercise 39, but parentheses must surround expressions being used as factors. 41. For each of these strings, determine whether it is gener­ ated by the grammar for infix expressions from Exercise 40. If it is, find the steps used to generate the string. a) x + y + z b) alb + cld c) m * (n + p) d) +m - n + p - q e) (m+n)*(p-q) 42. Let G be a grammar and let R be the relation contain­ ing the ordered pair (wo, WI) if and only if WI is directly derivable from Wo in G. What is the reflexive transitive closure of R? 12.2 Finite-State Machines with Output unks� Introduction Many kinds of machines, including components in computers, can be modeled using a structure called a finite-state machine. Several types of finite-state machines are commonly used in models. All these versions of finite-state machines include a finite set of states, with a designated starting state, an input alphabet, and a transition function that assigns a next state to every state and input pair. Finite-state machines are used extensively in applications in computer science and data networking. For example, finite-state machines are the basis for programs for spell checking, grammar checking, indexing or searching large bodies of text, recognizing speech, transforming text using markup languages such as XML and HTML, and network protocols that specify how computers communicate. 12-13 TABLE 1 State Table for a Vending Machine. Next State Input State 5 10 25 0 R So SI S2 Ss So So SI S2 S3 S6 SI SI S2 S3 S4 S6 S2 S2 S3 S4 Ss S6 S3 S3 S4 Ss S6 S6 S4 S4 Ss S6 S6 S6 Ss Ss S6 S6 S6 S6 So So 5 n n n n n n 5 12.2 Finite-State Machines with Output 797 Output Input 10 25 0 R n n n n n n n n n 5 n n n 10 n n n 15 n n 5 20 n n 10 25 OJ AJ In this section, we will study those finite-state machines that produce output. We will show how finite-state machines can be used to model a vending machine, a machine that delays input, a machine that adds integers, and a machine that determines whether a bit string contains a specified pattern. Before giving formal definitions, we will show how a vending machine can be modeled. A vending machine accepts nickels (5 cents), dimes (10 cents), and quarters (25 cents). When a total of 30 cents or more has been deposited, the machine immediately returns the amount in excess of 30 cents. When 30 cents has been deposited and any excess refunded, the customer can push an orange button and receive an orange juice or push a red button and receive an apple juice. We can describe how the machine works by specifying its states, how it changes states when input is received, and the output that is produced for every combination of input and current state. The machine can be in any of seven different states Sj, i = 0,1,2, ... ,6, where Sj is the state where the machine has collected 5i cents. The machine starts in state So, with 0 cents received. The possible inputs are 5 cents, 10 cents, 25 cents, the orange button ( 0), and the red button (R). The possible outputs are nothing (n), 5 cents, 10 cents, 15 cents, 20 cents, 25 cents, an orange juice, and an apple juice. We illustrate how this model of the machine works with this example. Suppose that a student puts in a dime followed by a quarter, receives 5 cents back, and then pushes the orange button for an orange juice. The machine starts in state So. The first input is 10 cents, which changes FIGURE 1 A Vending Machine. 25,n 0, orange juice 25,5 25,20 798 12/ Modeling Computation 12-14 DEFINITION 1 the state of the machine to S2 and gives no output. The second input is 25 cents. This changes the state from S2 to S6, and gives 5 cents as output. The next input is the orange button, which changes the state from S6 back to So (because the machine returns to the start state) and gives an orange juice as its output. We can display all the state changes and output of this machine in a table. To do this we need to specify for each combination of state and input the next state and the output obtained. Table 1 shows the transitions and outputs for each pair of a state and an input. Another way to show the actions of a machine is to use a directed graph with labeled edges, where each state is represented by a circle, edges represent the transitions, and edges are labeled with the input and the output for that transition. Figure 1 shows such a directed graph for the vending machine. Finite-State Machines with Outputs We will now give the formal definition of a finite-state machine with output. A finite-state machine M = (S, I, 0, f, g, so) consists of a finite set S of states, a finite input alphabet /, a finite output alphabet 0, a transition function f that assigns to each state and input pair a new state, an output function g that assigns to each state and input pair an output, and an initial state so. Let M = (S, 1,0, J, g, so) be a finite-state machine. We can use a state table to represent the values of the transition function f and the output function 9 for all pairs of states and input. We previously constructed a state table for the vending machine discussed in the introduction to this section. EXAMPLE 1 The state table shown in Table 2 describes a finite-state machine with S = {so, SI, S2, S 3}, I = {O, I}, and 0 = {O, I}. The values of the transition function f are displayed in the first two columns, and the values of the output function g are displayed in the last two columns. .... Another way to represent a finite-state machine is to use a state diagram, which is a directed graph with labeled edges. In this diagram, each state is represented by a circle. Arrows labeled with the input and output pair are shown for each transition. EXAMPLE 2 Construct the state diagram for the finite-state machine with the state table shown in Table 2. Solution: The state diagram for this machine is shown in Figure 2. EXAMPLE 3 Construct the state table for the finite-state machine with the state diagram shown in Figure 3. Solution: The state table for this machine is shown in Table 3. An input string takes the starting state through a sequence of states, as determined by the transition function. As we read the input string symbol by symbol (from left to right), each input symbol takes the machine from one state to another. Because each transition produces an output, an input string also produces an output string. Suppose that the input string is x = XIX2 .•. Xk. Then, reading this input takes the machine from state So to state SI, where SI = f(so, XI), then to state S2, where S2 = f(SI, X2), and so on, 12-15 TABLE 2 f Input State 0 1 So SI So SI S3 So S2 SI S2 S3 S2 SI g Input 0 1 1 0 0 1 0 1 1 0 12.2 Finite-State Machines with Output 799 Start I, I FIGURE 2 The'State Diagram for the Finite-State Machine Shown in Table 2. with Sj = !(Sj-l, Xj) for j = 1,2, ... , k, ending at state Sk = !(Sk-I, Xk). This sequence of transitions produces an output string YIY2 ... Yk. where YI = g(so, XI) is the output correspond­ ing to the transition from So to SI, Y2 = g(si , X2) is the output corresponding to the transition fromsi tOS2, and so on. In general,Yj = g(Sj_l, Xj) for j = 1,2, ... , k. Hence, we can extend the definition of the output function 9 to input strings so that g(x) = y, where y is the output corresponding to the input string x. This notation is useful in many applications. EXAMPLE 4 Find the output string generated by the finite-state machine in Figure 3 if the input string is 101011. Solution: The output obtained is 001000. The successive states and outputs are shown in �� � We can now look at some examples of useful finite-state machines. Examples 5, 6, and 7 illustrate that the states of a finite-state machine give it limited memory capabilities. The states can be used to remember the properties of the symbols that have been read by the machine. However, because there are only finitely many different states, finite-state machines cannot be used for some important purposes. This will be illustrated in Section 12.4. EXAMPLE 5 An important element in many electronic devices is a unit-delay machine, which produces as output the input string delayed by a specified amount of time. How can a finite-state machine be constructed that delays an input string by one unit of time, that is, produces as output the bit string OXIX2 ... Xk-I given the input bit string XIX2 ... Xk? 0, 1 Start-----i� FIGURE 3 A Finite-State Machine. 1,0 TABLE 3 State So SI S2 S3 S4 'f Input 0 SI SI S3 SI S3 g Input 1 0 1 S3 1 0 S2 1 1 S4 0 0 So 0 0 S4 0 0 800 12/ Modeling Computation 12-16 TABLE 4 Input 1 State So Output 0 0 1 0 S3 SI S2 0 1 0 1 1 S3 So 0 0 - S3 -FIGURE 4 A Unit-Delay Machine. Solution: A delay machine can be constructed that has two possible inputs, namely, 0 and 1. The machine must have a start state So. Because the machine has to remember whether the previous input was a 0 or aI, two other states S I and S2 are needed, where the machine is in state S I if the previous input was 1 and in state S2 if the previous input was O. An output of 0 is produced for the initial transition from So. Each transition from SI gives an output of 1, and each transition from S2 gives an output of o. The output corresponding to the input of a string XI ... Xk is the string that begins with 0, followed by XI. followed by X2, ••• , ending with Xk-I. The state diagram for this machine is shown in Figure 4. .... EXAMPLE 6 Produce a finite-state machine that adds two integers using their binary expansions. Solution: When (xn ... XI xoh and (yn ... YIYoh are added, the following procedure (as described in Section 3.6) is followed. First, the bits Xo and Yo are added, producing a sum bit Zo and a carry bit Co. This carry bit is either 0 or 1. Then, the bits X I and YI are added, together with the carry Co. This gives a sum bit ZI and a carry bit CI. This procedure is continued until the nth stage, where Xn, Yn, and the previous carry Cn-I are added to produce the sum bit Zn and the carry bit Cn, which is equal to the sum bit Zn-I. . A finite-state machine to carry out this addition can be constructed using just two states. For simplicity we assume that both the initial bits Xn and Yn are 0 (otherwise we have to make special arrangements concerning the sum bit Zn+I). The start state So is used to remember that the previous carry is 0 (or for the addition of the rightmost bits). The other state, SJ, is used to remember that the previous carry is 1. Because the inputs to the machine are pairs of bits, there are four possible inputs. We represent these possibilities by 00 (when both bits are 0), 01 (when the first bit is 0 and the second is 1), 10 (when the first bit is 1 and the second is 0), and 11 (when both bits are 1). The transitions arid the outputs are constructed from the sum of the two bits represented by the input and the carry represented by the state. For instance, when the machine is in state SI and receives 01 as input, the next state is SI and the output is 0, because the sum that arises is 0+ 1 + 1 = (10)2. The state diagram for this machine is shown in Figure 5. .... 01,1 01,0 00,0 11, 1 10, 1 10,0 FIGURE 5 A Finite-State Machine for Addition. 12-17 1,1 0,0 12.2 Finite-State Machines with Output 801 FIGURE 6 A Finite-State Machine That Gives an Output of 1 If and Only If the Input String Read So Far Ends with 111. EXAMPLE 7 In a certain coding scheme, when three consecutive 1 s appear in a message, the receiver of the message knows that there has been a transmission error. Construct a finite-state machine that gives a 1 as its current output bit if and only if the last three bits received are allIs. Solution: Three states are needed in this machine. The start state So remembers that the previous input value, if it exists, was not a 1. The state Sl remembers that the previous input was a 1, but the input before the previous input, if it exists, was not a 1. The state S2 remembers that the previous two inputs were 1 s. An input of 1 takes So to SI, because now aI, and not two consecutive 1 s, has been read; it takes SI to S2, because now two consecutive Is have been read; and it takes S2 to itself, because at least two consecutive 1 s have been read. An input of 0 takes every state to So, because this breaks up any string of consecutive 1 s. The output for the transition from S2 to itself when a 1 is read is 1, because this combination of input and state shows that three consecutive 1 shave been read. All other outputs are O. The state diagram of this machine is shown in Figure 6. � The final output bit of the finite-state machine we constructed in Example 7 is 1 if and only if the input string ends with 111. Because of this, we say that this finite-state machine recognizes the set of bit strings that end with 111. This leads us to Definition 2. DEFINITION 2 Let M = (S, I, 0, f, g, so) be a finite-state machine and L £ I*. We say that M recognizes (or accepts) L if an input string x belongs to L if and only if the last output bit produced by M when given x as input is a 1. TYPES OF FINITE-STATE MACHINES Many different kinds of finite-state machines have been developed to model computing machine s. In this section we have given a definition of one type of finite-state machine. In the type of machine introduced in this section, outputs correspond to transitions between states. Machines of this type are known as Mealy machines, because they were first studied by G. H. Mealy in 1955. There is another important type of finite­ state machine with output, where the output is determined only by the state. This type of finite­ state machine is known as a Moore machine, because E. F. Moore introduced this type of machine in 1956. Moore machines are considered in a sequence of exercises at the end of this section. In Example 7 we showed how a Mealy machine can be used for language recognition. However, another type of finite-state machine, giving no output, is usually used for this purpose. Finite-state machines with no output, also known as finite-state automata, have a set of final states and recognize a string if and only if it takes the start state to a final state. We will study this type of finite-state machine in Section 12.3. 802 12 / Modeling Computation Exercises 1. Draw the state diagrams for the finite-state machines with these state tables. a) f g Input Input State 0 1 0 1 80 81 80 0 1 81 80 82 0 1 82 81 81 0 0 b) f g Input Input State 0 1 0 1 80 81 80 0 0 81 82 80 1 1 82 80 83 0 1 83 81 82 1 0 c) f g Input Input State 0 1 0 1 80 80 84 1 1 81 80 83 0 1 82 80 82 0 0 83 81 81 1 1 84 81 80 1 0 2. Give the state tables for the finite-state machines with these state diagrams. a) Start 12-18 b) 1,0 Start c) Start 1,1 1,0 3. Find the output generated from the input string 01110 for the finite-state machine with the state table in a) Exercise l(a). b) Exercise 1(b). c) Exercise l(c). 4. Find the output generated from the input string 10001 for the finite-state machine with the state diagram in a) Exercise 2(a). b) Exercise 2(b). c) Exercise 2(c). 5. Find the output for each of these input strings when given as input to the finite-state machine in Example 2. a) 0111 b) 11011011 c) 01010101010 6. Find the output for each of these input strings when given as input to the finite-state machine in Example 3. a) 0000 b) 101010 c) 11011100010 7. Construct a finite-state machine that models an old­ fashioned soda machine that accepts nickels, dimes, and quarters. The soda machine accepts change until 35 cents has been put in. It gives change back for any amount greater than 35 cents. Then the customer can push but­ tons to receive either a cola, a root beer, or a ginger ale. 8. Construct a finite-state machine that models a newspa­ per vending machine that has a door that can be opened only after either three dimes (and any number of other coins) or a quarter and a nickel (and any number of other coins) have been inserted. Once the door can be opened, the customer opens it and takes a paper, closing the door. No change is ever returned no matter how much extra money has been inserted. The next customer starts with no credit. 12-19 9. Construct a finite-state machine that delays an input string two bits, giving 00 as the first two bits of output. 10. Construct a finite-state machine that changes every other bit, starting with the second bit, of an input string, and leaves the other bits unchanged. 11. Construct a finite-state machine for the log-on procedure for a computer, where the user logs on by entering a user identification number, which is considered to be a single input, and then a password, which is considered to be a single input. If the password is incorrect, the user is asked for the user identification number again. 12. Construct a finite-state machine for a combination lock that contains numbers 1 through 40 and that opens only when the correct combination, 10 right, 8 second left, 37 right, is entered. Each input is a triple consisting of a num­ ber, the direction of the turn, and the number of times the lock is turned in that direction. 13. Construct a finite-state machine for a toll machine that opens a gate after 25 cents, in nickels, dimes, or quarters, has been deposited. No change is given for overpayment, and no credit is given to the next driver when more than 25 cents has been deposited. 14. Construct a finite-state machine for entering a security code into an automatic teller machine (ATM) that im­ plements these rule s: A user enters a string of four dig­ its, one digit at a time. If the user enters the correct four digits of the password, the ATM displays a wel­ come screen. When the user enters an incorrect string of four digits, the ATM displays a screen that informs the user that an incorrect password was entered. If a user enters the incorrect password three times, the account is locked. IS. Construct a finite-state machine for a restricted telephone switching system that implements these rules. Only calls to the telephone numbers 0, 911, and the digit 1 followed by lO-digit telephone numbers that begin with 212,800, 866,877, and 888 are sent to the network. All other strings of digits are blocked by the system and the user hears an error message. 16. Construct a finite-state machine that gives an output of 1 if the number of input symbols read so far is divisible by 3 and an output of 0 otherwise. 17. Construct a finite-state machine that determines whether the input string has a 1 in the last position and a 0 in the third to the last position read so far. 18. Construct a finite-state machine that determines whether the input string read so far ends in at least five consecu­ tive Is. 19. Construct a finite-state machine that determines whether the word computer has been read as the last eight charac­ ters in the input read so far, where the input can be any string of English letters. A Moore machine M = (S, I, 0, !, g, so) consists ofa finite set of states, an input alphabet I, an output alphabet 0, a tran­ sition function ! that assigns a next state to every pair of a 12.2 Finite-State Machines with Output 803 state and an input, an output function g that assigns an output to every state, and a starting state so. A Moore machine can be represented either by a table listing the transitions for each pair of state and input and the outputs for each state, or by a state diagram that displays the states, the transitions between states, and the output for each state. In the diagram, transi­ tions are indicated with arrows labeled with the input, and the outputs are shown next to the states. 20. Construct the state diagram for the Moore machine with this state table. f Input State 0 1 g So So S2 0 SI S3 So 1 S2 S2 S I 1 S3 S2 So 1 21. Construct the state table for the Moore machine with the state diagram shown here. Each input string to a Moore machine M produces an output string. In particular, the output corresponding to the input string ala2 ... ak is the string g(so)g(sl) ... g(Sk), where SI = !(Si-I, ai) for i = 1,2, ... , k. o Start 22. Find the output string generated by the Moore machine in Exercise 20 with each of these input strings. a) 0101 b) 111111 c) 11101110111 23. Find the output string generated by the Moore machine in Exercise 21 with each of the input strings in Exercise 22. 24. Construct a Moore machine that gives an output of 1 whenever the number of symbols in the input string read so far is divisible by 4. 25. Construct a Moore machine that determines whether an input string contains an even or odd number of 1 s. The machine should give 1 as output if an even number of 1 s are in the string and 0 as output if an odd number of 1 s are in the string. 804 12/ Modeling Computation 12-20 12.3 Finite-State Machines with No Output links� Introduction One ofthe most important applications of finite-state machines is in language recognition. This application plays a fundamental role in the design and construction of compilers for program­ ming languages. In Section 12.2 we showed that a finite-state machine with output can be used to recognize a language, by giving an output of 1 when a string from the language has been read and a 0 otherwise. However, there are other types of finite-state machines that are specially designed for recognizing languages. Instead of producing output, these machines have final states. A string is recognized if and only if it takes the starting state to one of these final states. Set of Strings Before discussing finite-state machines with no output, we will introduce some important back­ ground material on sets of strings. The operations that will be defined here will be used exten­ sively in our discussion of language recognition by finite-state machines. DEFINITION 1 Suppose that A and B are subsets of V*, where V is a vocabulary. The concatenation of A and B, denoted by AB, is the set of all strings of the form xy, where x is a string in A and y is a string in B. EXAMPLE 1 Let A = {O, II} and B = {I, 10, 110}. Find AB and BA. Solution: The set A B contains every concatenation of a string in A and a string in B. Hence, AB = {Ol, 010, 0110,111, 1110, 11110}. The set BA contains every concatenation ofa string in B and a string in A. Hence, BA = {IO, 111, 100, 1011, 1100, 11011}. .... Note that it is not necessarily the case that AB = BA when A and B are subsets of V*, where V is an alphabet, as Example 1 illustrates. From the definition of the concatenation of two sets of strings, we can define An, for n = 0, 1, 2, .... This is done recursively by specifying that AO = {A}, An+'=AnA forn=0,1,2, .... EXAMPLE 2 Let A = {I,OO}. Find An for n = 0,1,2, and 3. Solution: We have AO = {A} and A' = AO A = {A}A = {I, OO}. To find A2 we take concate­ nations of pairs of elements of A. This gives A2 = {11, 100,001, OOOO}. To find A3 we take concatenations of elements in A2 and A; this gives A3 = {Ill, 1100, 1001,10000, 0011,00100,00001,000000}. .... DEFINITION 2 Suppose that A is a subset of V*. Then the Kleene closure of A, denoted by A*, is the set consisting of concatenations of arbitrarily many strings from A. That is, A * = U�O A k. 12-21 12.3 Finite-State Machines with No Output 805 EXAMPLE 3 What are the Kleene closures of the sets A = {OJ, B = to, I}, and C = {II}? DEFINITION 3 Solution: The Kleene closure of A is the concatenation of the string ° with itself an arbitrary finite number of times. Hence, A* = {on In = 0,1,2, ... }. The Kleene closure of B is the concatenation of an arbitrary number of strings, where each string is either ° or 1. This is the set of all strings over the alphabet V = to, I}. That is, B* = V*. Finally, the Kleene closure of C is the concatenation of the string 11 with itself an arbitrary number of times. Hence, C* is the set of strings consisting of an even number of 1 s. That is, C* = {12n I n = 0, 1, 2, ... }. .... Finite-State Automata We will now give a definition of a finite-state machine with no output. Such machines are also called finite-state automata, and that is the terminology we will use for them here. (Note: The singular of automata is automaton.) These machines differ from the finite-state machines studied in Section 12.2 in that they do not produce output, but they do have a set of final states. As we will see, they recognize strings that take the starting state to a final state. A finite-state automaton M = (S, I, I, so, F) consists of a finite set S of states, a finite input alphabet I, a transition lunction I that assigns a next state to every pair of state and input (so that I : S x I -+ S), an initial or start state so, and a subset F of S consisting offinal (or accepting states). We can represent finite-state automata using either state tables or state diagrams. Final states are indicated in state diagrams by using double circles. EXAM PLE 4 Construct the state diagram for the finite-state automaton M = (S, I, J, so, F), where S = {so, St , S2, S3}, I = to, I}, F = {so, S3}, and the transition function f is given in Table 1. Linl'S� Solution: The state diagram is shown in Figure 1. Note that because both the inputs ° and 1 take S2 to so, we write 0,1 over the edge from S2 to so. .... EXTENDING THE TRANSITION FUNCTION The transition function I of a finite-state machine M = (S, I, I, so, F) can be extended so that it is defined for all pairs of states and strings;thatis,/canbeextendedtoafunction /: S x [* -+ S.Letx =XtX2 ... xkbeastringin 1*. Then I(st, x) is the state obtained by using each successive symbol of x, from left to right, as STEPH EN COLE KLEENE (1909-1994) Stephen Kleene was born in Hartford, Connecticut. His mother, Alice Lena Cole, was a poet, and his father, Gustav Adolph Kleene, was an economics professor. Kleene attended Amherst College and received his Ph.D. from Princeton in 1934, where he studied under the famous logician Alonzo Church. Kleene joined the faculty of the University of Wisconsin in 1935, where he remained except for several leaves, including stays at the Institute for Advanced Study in Princeton. During World War 11 he was a navigation instructor at the Naval Reserve's Midshipmen' s School and later served as the director ofthe Naval Research Laboratory. Kleene made significant contributions to the theory of recursive functions, investigating questions of computab ility and decidability, and proved one ofthe central results of automata theory. He served as the Acting Director of the Mathematics Research Center and as Dean of the College of Letters and Sciences at the University of Wisconsin. Kleene was a student of natural history. He discovered a previously undescribed variety ofbutterfty that is named after him. He was an avid hiker and climber. Kleene was also noted as a talented teller of anecdote s, using a powerful voice that could be heard several offices away. 806 12/ Modeling Computation 12-22 TABLE 1 f Input Start State 0 1 So So SI SI So S2 S2 So S3 S2 So SI FIGURE 1 The State Diagram for a Finite-State Automaton. input, starting with statesi. Fromsi we go on to states2 = l(sl, XI), then to states3 = l(s2, X2), and so on, with l(sl, x) = I(sk, Xk). Formally, we can define this extended transition function I recursively for the deterministic finite-state machine M = (S, I, I, So, F) by (i) I(s, )..) = S for every state S E S; and (ii) I(s, xa) = l(f(s, x), a) for all S E S, x E 1*, and a E I. We can use structural induction and this recursive definition to prove properties of this extended transition function. For example, in Exercise 15 we ask you to prove that I(s, xy) = 1(/(s, x), y) for every state s E S and strings x E 1* and y E 1*. Language Recognition by Finite-State Machines Next, we define some terms that are used when studying the recognition by finite-state automata of certain sets of strings. DEFINITION 4 A string x is said to be recognized or accepted by the machine M = (S, I, J, So. F) if it takes the initial state So to a final state, that is, I(so, x) is a state in F. The language recognized or accepted by the machine M, denoted by L(M), is the set of all strings that are recognized by M. Two finite-state automata are called equivalent if they recognize the same language. In Example 5 we will find the languages recognized by several finite-state automata. EXAMPLE 5 Determine the languages recognized by the finite-state automata MJ, M2, and M3 in Figure 2. Solution: The only final state of MI is So. The strings that take So to itself are those consisting of zero or more consecutive Is. Hence, L(Ml) = {In In = O. 1.2 •... }. The only final state of M2 is S2. The only strings that take So to S2 are 1 and 01. Hence, L(M2) = {I. OIl. The final states of M 3 are So and S3. The only strings that take So to itself are )... O. 00. 000 •... , that is, any string of zero or more consecutive Os. The only strings that take So to S3 are a string 12-23 EXAMPLE 6 Start o Start Mz Start 0,1 FIGURE 2 Some Finite-State Automata. 12.3 Finite-State Machines with No Output 807 of zero or more consecutive Os, followed by 10, followed by any string. Hence, L(M3) = {on, on lOx I n = 0, 1,2, ... , and x is any string}. .... DESIGNING FINITE-STATE AUTOMA TA We can often construct a finite-state automaton that recognizes a given set of strings by carefully adding states and transitions and determining which of these states should be final states. When appropriate we include states that can keep track of some of the properties of the input string, providing the finite-state automaton with limited memory. Examples 6 and 7 illustrate some of the techniques that can be used to construct finite-state automata that recognize particular types of sets of strings. Construct deterministic finite-state automata that recognize each of these languages. (a) the set of bit strings that begin with two Os (b) the set of bit strings that contain two consecutive Os (c) the set of bit strings that do not contain two consecutive Os (d) the set of bit strings that end with two Os (e) the set of bit strings that contain at least two Os Solution: (a) Our goal is to construct a deterministic finite-state automaton that recognizes the set of bit strings that begin with two Os. Besides the start state So, we include a nonfinal state SI; we move to SI from So if the first bit is a O. Next, we add a final state S2, which we move to from SI if the second bit is a O. When we have reached SI we know that the first two input bits 808 12/ Modeling Computation 12-24 (a) Start (b) Start (c) 0, I Start 0, I 0, I (d) I I _Start -----+l.�� (e) Start FIGURE 3 Deterministic Finite-State Automata Recognizing the Languages in Example 6. 0, I are both Os, so we stay in the state S2 no matter what the succeeding bits (if any) are. We move to a nonfinal state S3 from So if the first bit is a 1 and from Sl if the second bit is a 1. The reader should verify that the finite-state automaton in Figure 3(a) recognizes the set of bit strings that begin with two Os. (b) Our goal is to construct a deterministic finite-state automaton that recognizes the set of bit strings that contain two consecutive Os. Besides the start state So, we include a nonfinal state SJ, which te lls us that the last input bit seen is a 0, but either the bit before it was a 1, or this bit was the initial bit of the string. We include a final state S2 that we move to from Sl when the next input bit after a 0 is also a O. If a 1 follows a 0 in the string (before we encounter two consecutive Os), we return to So and begin looking for consecutive Os all over again. The reader should verify that the finite-state automaton in Figure 3(b) recognizes the set of bit strings that contain two consecutive Os. (c) Our goal is to construct a deterministic finite-state automaton that recognizes the set of bit strings that do not contain two consecutive Os. Besides the start state So, which should be a final state, we include a final state S1, which we move to from So when 0 in the first input bit. When an input bit is a 1, we return to, or stay in, state So. We add a state S2, which we move to from Sl when the input bit is a O. Reaching S2 tells us that we have seen two con­ secutive Os as input bits. We stay in state S2 once we have reached it; this state is not final. The reader should verify that the finite-state automaton in Figure 3( c) recognizes the set of bit strings that do not contain two consecutive Os. [The astute reader will notice the relation­ ship between the finite-state automaton constructed here and the one constructed in part (b). See Exercise 39.] (d) Our goal is to construct a deterministic finite-state automaton that recognizes the set of bit strings that end with two Os. Besides the start state So, we include a nonfinal state Sl, which we move to if the first bit is O. We include a final state S2, which we move to from Sl if the next input bit after a 0 is also a O. If an input of 0 follows a previous 0, we stay in state S2 because the last two input bits are still Os. Once we are in state S2, an input bit of 1 sends us back to So, and we begin looking for consecutive Os all over again. We also return to So if the next input is a 1 12-25 Start Number of Is Number of Os at end even even o even odd o 12.3 Finite-State Machines with No Output 809 o odd odd FIGURE 4 A Deterministic Finite-State Automaton Recognizing the Set of Bit Strings Containing an Odd Number of Is and Ending with at Least Two Os. when we are in state St. The reader should verify that the finite-state automaton in Figure 3( d) recognizes the set of bit strings that end with two Os. (e) Our goal is to construct a deterministic finite-state automaton that recognizes the set of bit strings that contain two Os. Besides the start state, we include a state St, which is not final; we stay in So until an input bit is a 0 and we move to St when we encounter the first 0 bit in the input. We add a final state S2, which we move to from St once we en­ counter a second 0 bit. Whenever we encounter a I as input, we stay in the current state. Once we have reached S2, we remain there. Here, St and S2 are used to tell us that we have already seen one or two Os in the input string so far, respectively. The reader should ver­ ify that the finite-state automaton in Figure 3( e) recognizes the set of bit strings that contain �� � EXAMPLE 7 Construct a deterministic finite-state automaton that recognizes the set of bit strings that contain an odd number of 1 s and that end with at least two consecutive Os. Solution: We can build a deterministic finite-state automaton that recognizes the specified set by including states that keep track of both the parity of the number of 1 bits and whether we have seen no, one, or at least two Os at the end of the input string. The start state So can be used to tell us that the input read so far contains an even number of Is and ends with no Os (that is, is empty or ends with a 1). Besides the start state, we include five more states. We move to states St, S2, S3, S4, and ss, respective ly, when the input string read so far contains an even number of Is and ends with one 0; when it contains an even number of 1 s and ends with at least two Os; when it contains an odd number of 1 s and ends with no Os; when it contains an odd number of Is and ends with one 0; and when it contains an odd number of 1 s and ends with two Os. The state Ss is a final state. The reader should verify that the finite-state automaton in Figure 4 recognizes the set of bit strings that contain an odd number of 1 s and end with at least two consecutive Os. � EQUIVALENT FINITE-STATE AUTOMA TA In Definition 4 we specified that two finite­ state automata are equivalent if they recognize the same language. Example 8 provides an example of two equivalent deterministic finite-state machines. 810 12/ Modeling Computation Start o Start 0, I FIGURE 5 Mo and M. Are Equivalent Finite-State Automata. EXAMPLE 8 Show that the two finite-state automata Mo and M\ shown in Figure 5 are equivalent. 12-26 Solution: For a string x to be recognized by Mo, x must take us from So to the final state s\ or the final state S4. The only string that takes us from So to s\ is the string 1. The strings that take us from So to S4 are those strings that begin with a 0, which takes us from So to S2, followed by zero or more additional Os, which keep the machine in state S2, followed by a 1, which takes us from state S2 to the final state S4. All other strings take us from So to a state that is not final. (We leave it to the reader to fill in the details.) We conclude that L(Mo) is the set of strings of zero or more 0 bits followed by a final 1. For a string x to be recognized by M \, X must take us from So to the final state St. So, for x to be recognized, it must begin with some number of Os, which leave us in state So, followed by a 1, which takes us to the final state St. A string of all zeros is not recognized because it leaves us in state So, which is not final. All strings that contain a 0 after 1 are not recognized because they take us to state S2, which is not final. It follows that L(Mt} is the same as L(Mo). We conclude that Mo and M\ are equivalent. Note that the finite-state machine M\ only has three states. No finite state machine with fewer than three states can be used to recognize the set of all strings of zero or more 0 bits followed by a 1 (see Exercise 37). .... As Example 8 shows, a finite-state automaton may have more states than one equivalent to it. In fact, algorithms used to construct finite-state automata to recognize certain languages may have many more states than necessary. Using unnecessarily large finite-state machines to recognize languages can make both hardware and software applications inefficient and costly. This problem arises when finite-state automata are used in compilers, which translate computer programs to a language a computer can understand (object code). Exercises 5� 1 develop a procedure that constructs a finite-state automaton with the fewest states possible among all finite-state automata equivalent to a given finite-state automaton. This procedure is known as machine minimization. The minimization procedure described in these exercises reduces the number of states by replacing states with equivalence classes of states with respect to an equivalence relation in which two states are equivalent if every input string either sends both states to a final state or sends both to a state that is not final. Before the minimization 12-27 12.3 Finite-State Machines with No Output 811 procedure begins, all states that cannot be reached from the start state using any input string are first removed; removing these does not change the language recognized. Nondetermi nistic Finite- State Automata The finite-state automata discussed so far are deterministic, because for each pair of state and input value there is a unique next state given by the transition function. There is another important type of finite-state automaton in which there may be several possible next states for each pair of input value and state. Such machines are called nondeterministic . Nondetermin­ istic finite-state automata are important in determining which languages can be recognized by a finite-state automaton. DEFINITION 5 UnkS� A nondeterministic finite-state automaton M = (S, I, f, so, F) consists of a set S of states, an input alphabet I, a transition function f that assigns a set of states to each pair of state and input (so that f : S x I -+ P(S)), a starting state so, and a subset F of S consisting of the final states. We can represent nondeterministic finite-state automata using state tables or state diagrams. When we use a state table, for each pair of state and input value we give a list of possible next GRACE BREWSTER MURRAY HOPPER (1906-1992) Grace Hopper, born in New York City, displayed an intense curiosity as a child with how things worked. At the age of seven, she disassembled alarm clocks to discover their mechanisms. She inherited her love of mathematics from her mother, who received special permission to study geometry (but not algebra and trigonometry) at a time when women were actively discouraged from such study. Hopper was inspired by her father, a successful insurance broker, who had lost his legs from circulatory problems. He told his children they could do anything if they put their minds to it. He inspired Hopper to pursue higher education and not conform to the usual roles for females. Her parents made sure that she had an excellent education; she attended private schools for girls in New York. Hopper entered Vassar College in 1924, where she majored in mathematics and physics; she graduated in 1928. She received a masters degree in mathematics from Yale University in 1930. In 1930 she also married an English instructor at the New York School of Commerce; she later divorced and did not have children. Hopper was a mathematics professor at Vassar from 1931 until 1943, earning a Ph.D. from Yale in 1934. After the attack on Pearl Harbor, Hopper, coming from a family with strong military traditions, decided to leave her academic position and join the Navy WAVES. To enlist, she needed special permission to leave her strategic position as a mathematics professor, as well as a waiver for weighing too little. In December 1943, she was sworn into the Navy Reserve and trained at the Midshipman' s School for Women. Hopper was assigned to work at the Naval Ordnance Laboratory at Harvard University. She wrote programs for the world's first large-scale automatically sequenced digital computer, which was used to help aim Navy artillery in varying weather. Hopper has been credited with coining the term "bug" to refer to a hardware glitch, but it was used at Harvard prior to her arrival there. However, it is true that Hopper and her programming team found a moth in one of the relays in the computer hardware that shut the system down. This famous moth was pasted into a lab book. In the 1950s Hopper coined the term "debug" for the process of removing programming errors. In 1946, when the Navy told her that she was too old for active service, Hopper chose to remain at Harvard as a civilian research fellow. In 1949 she left Harvard to join the Eckert-Mauchly Computer Corporation, where she helped develop the first commercial computer, UNIVAC. Hopper remained with this company when it was taken over by Remington Rand and when Remington Rand merged with the Sperry Corporation. She was a visionary for the potential power of computers; she understood that computers would become widely used if tools that were both programmer-friendly and application-friendly could be developed. In particular, she believed that computer programs could be written in English, rather than using machine instructions. To help achieve this goal, she developed the first compiler. She published the first research paper on compilers in 1952. Hopper is also known as the mother ofthe computer language COBOL; members of Hopper's staff helped to frame the basic language design for COBOL using their earlier work as a basis. . In 1966, Hopper retired from the Navy Reserve. However, only seven months later, the Navy recalled her from retirement to help standardize high-level naval computer languages. In 1983 she was promoted to the rank of Commodore by special Presidential appointment, and in 1985 she was elevated to the rank of Rear Admiral. Her retirement from the Navy, at the age of80, was held on the USS Constituti on. 812 12/ Modeling Computation TABLE 2 f Input State 0 So So, S( S( So S2 S3 So, Sb S2 1 S3 S(, S3 So, S2 S( 12-28 Stan FIGURE 6 The Nondeterministic Finite-State Automaton with State Table Given in Table 2. states. In the state diagram, we include an edge from each state to all possible next states, labeling edges with the input or inputs that lead to this transition. EXAMPLE 9 Find the state diagram for the nondeterministic finite-state automaton with the state table shown in Table 2. The final states are S2 and S3. Solution: The state diagram for this automaton is shown in Figure 6. EXAMPLE 10 Find the state table for the nondeterministic finite-state automaton with the state diagram shown in Figure 7. Solution: The state table is given as Table 3. What does it mean for a nondeterministic finite-state automaton to recognize a string x = XIX2 ••• Xk? The first input symbol XI takes the starting state So to a set SI of states. The next input symbol X2 takes each of the states in SI to a set of states. Let S2 be the union of these sets. We continue this process, including at a stage all states obtained using a state obtained at the previous stage and the current input symbol. We recognize, or accept, the string X if there is a final state in the set of all states that can be obtained from So using x. The language recognized by a nondeterministic finite-state automaton is the set of all strings recognized by this automaton. o Stan---I� FIGURE 7 A Nondeterministic Finite-State Automaton. TABLE 3 State So S( S2 S3 S4 f Input 0 1 So, S2 S( S3 S4 S4 S3 S3 S3 12-29 12.3 Finite-State Machines with No Output 813 EXAMPLE 11 Find the language recognized by the nondetenninistic finite-state automaton shown in Figure 7. THEOREM 1 Solution: Because So is a final state, and there is a transition from So to itself when 0 is the input, the machine recognizes all strings consisting of zero or more consecutive Os. Furthennore, because S4 is a final state, any string that has S4 in the set of states that can be reached from So with this input string is recognized. The only such strings are strings consisting of zero or more consecutive Os followed by 0 I or 11. Because So and S4 are the only final states, the language recognized by the machine is {on, on 0 1, on 11 In:::: O}. .... One important fact is that a language recognized by a nondetenninistic finite-state automaton is also recognized by a detenninistic finite-state automaton. We will take advantage of this fact in Section 12.4 when we will detennine which languages are recognized by finite-state automata. If the language L is recognized by a nondeterministic finite-state automaton Mo, then L is also recognized by adetenninistic finite-state automaton MI. Proof: We will describe how to construct the detenninistic finite-state automaton Ml that recognizes L from Mo, the nondetenninistic finite-state automaton that recognizes this language. Each state in MI will be made up ofa set of states in Mo. The start symbol of MI is {so}, which is the set containing the start state of Mo. The input set of MI is the same as the input set of Mo. Given a state {Sil' Siz' ... , Sik} of MI, the input symbol x takes this state to the union of the sets of next states for the elements ofthis set, that is, the union of the sets !(SiJ, !(Si2)' ... , !(Sik). The states of MI are all the subsets of S, the set of states of Mo, that are obtained in this way starting with So. (There are as many as 2n states in the detenninistic machine, where n is the number of states in the nondetenninistic machine, because all subsets may occur as states, including the empty set, although usually far fewer states occur.) The final states of M I are those sets that contain a final state of Mo. Suppose that an input string is recognized by Mo. Then one of the states that can be reached from So using this input string is a final state (the reader should provide an inductive proof of this). This means that in MI, this input string leads from {so} to a set of states of Mo that contains a final state. This subset is a final state of MI, so this string is also recognized by MI. Also, an input string not recognized by Mo does not lead to any final states in Mo. (The reader should provide the details that prove this statement .) Consequently, this input string does not lead from {so} to a final state in MI. <l EXAMPLE 12 Find a detenninistic finite-state automaton that recognizes the same language as the nondeter­ ministic finite-state automaton in Example 10. Solution: The detenninistic automaton shown in Figure 8 is constructed from the nondetennin­ istic automaton in Example 10. The states of this detenninistic automaton are subsets of the set of all states of the nondetenninistic machine. The next state of a subset under an input symbol is the subset containing the next states in the nondetenninistic machine of all elements in this subset. For instance, on input of 0, {so} goes to {so, S2}, because So has transitions to itself and to S2 in the nondetenninistic machine; the set {so, S2} goes to {S I , S4} on input of 1, because So goes just to SI and S2 goes just to S4 on input of 1 in the nondetenninistic machine; and the set {SI, S4} goes to {S3} on input of 0, because SI and S4 both go to just S3 on input of 0 in the detenninistic machine. All subsets that are obtained in this way are included in the detenninistic finite-state machine. Note that the empty set is one of the states of this machine, because it is the subset containing all the next states of {S3} on input of 1. The start state is {so}, and the set of final states are all those that include So or S4. .... 814 12/ Modeling Computation 12-30 Start FIGURE 8 A Deterministic Automaton Equivalent to the Nondeterministic Automaton in Example 10. Exercises 1. Let A = to, 11} and B = {00,01}. Find each of these sets. a) AB b) BA 2. Show that if A is a set of strings, then A0 = 0A = 0. 3. Find all pairs of sets of strings A and B for which AB = {l0, Ill, 1010, 1000, 10111, IOIOOO}. 4. Show that these equalities hold. a) {A}* = {A} b) (A*)* = A* for every set of strings A 5. Describe the elements of the set A * for these values of A. a) {l0} b) {llI} c) {O,OI} d) {I, IOI} 6. Let V be an alphabet, and let A and B be subsets of V*. Show that IABI :::: IAIIBI. 7. Let V be an alphabet, and let A and B be subsets of V* with A � B. Show that A* � B*. 8. Suppose that A is a subset of V*, where V is an alphabet. Prove or disprove each of these statements. a) A � A2 b) if A = A2, then A E A c) A{A} = A d) (A*)* = A* e) A*A=A* 1) IAnl=IAln 9. Determine whether the string 11101 is in each of these sets. a) {O,I}* c) {ll} {O}*{OI} e) {Ill}*{O}*{I} b) {1}*{O}*{I}* d) {II}*{OI}* 1) {ll, O}{OO, IOI} 10. Determine whether the string 01001 is in each of these sets. a) to, I}* c) {OIO}*{O}*{I} e) {00} {O}*{OI} b) {0}*{1O}{l}* d) {OIO,OII}{OO,OI} 1) {01}*{01}* 11. Determine whether each of these strings is recognized by the deterministic finite-state automaton in Figure 1. a) 111 b) 0011 c) 1010111 d) 011011011 12. Determine whether each of these strings is recognized by the deterministic finite-state automaton in Figure 1. a) 010 b) 1101 c) 1111110 d) 010101010 13. Determine whether all the strings in each of these sets are recognized by the deterministic finite-state automaton in Figure 1. a) {O}* d) {01}* b) (OJ {O}* e) {0}*{1}* c) {I} {O}* 1) {I} to, l}* 14. Show that if M = (S, /, f, so, F) is a deterministic finite­ state automaton and f(s, x) = s for the state s E S and the input string x E I*, then f(s, xn) = s for every non­ negative integer n. (Here xn is the concatenation of n copies of the string x , defined recursively in Exercise 37 in Section 4.3.) 15. Given a deterministic finite-state automaton M = (S, /, f, so, F), use structural induction and the recursive definition of the extended transition function f to prove that f(s, xy) = f(f(s, x), y) for all states s E S and all strings x E I* and y E /*. In Exercises 16-22 find the language recognized by the given deterministic finite-state automaton. 16. o 0,1 12-31 18. n.t 19. n.t o 20. o 23. Construct a deterministic finite-state automaton that rec­ ognizes the set of all bit strings beginning with 01. 24. Construct a deterministic finite-state automaton that rec­ ognizes the set of all bit strings that end with 10. 25. Construct a deterministic finite-state automaton that rec­ ognizes the set of all bit strings that contain the string 101. 26. Construct a deterministic finite-state automaton that rec­ ognizes the set of all bit strings that do not contain three consecutive Os. 12.3 Finite-State Machines with No Output 815 27. Construct a deterministic finite-state automaton that recognizes the set of all bit strings that contain exactly three Os. 28. Construct a deterministic finite-state automaton that rec­ ognizes the set of all bit strings that contain at least three Os. 29. Construct a deterministic finite-state automaton that rec­ ognizes the set of all bit strings that contain three consec­ utive Is. 30. Construct a deterministic finite-state automaton that rec­ ognizes the set of all bit strings that begin with 0 or with 11. 31. Construct a deterministic finite-state automaton that rec­ ognizes the set of all bit strings that begin and end with 11. 32. Construct a deterministic finite-state automaton that rec­ ognizes the set of all bit strings that contain an even num­ ber of Is. 33. Construct a deterministic finite-state automaton that rec­ ognizes the set of all bit strings that contain an odd number of Os. 34. Construct a deterministic finite-state automaton that rec­ ognizes the set of all bit strings that contain an even num­ ber of Os and an odd number of 1 s. 35. Construct a finite-state automaton that recognizes the set of bit strings consisting of a 0 followed by a string with an odd number of Is. 36. Construct a finite-state automaton with four states that recognizes the set of bit strings containing an even num­ ber of 1 s and an odd number of Os. 37. Show that there is no finite-state automaton with two states that recognizes the set of all bit strings that have one or more 1 bits and end with a O. 38. Show that there is no finite-state automaton with three states that recognizes the set of bit strings containing an even number of 1 s and an even number of Os. 39. Explain how you can change the deterministic finite-state automaton M so that the changed automaton recognizes the set [* -L(M). 40. Use Exercise 39 and finite-state automata constructed in Example 6 to find deterministic finite-state automata that recognize each of these sets. a) the set of bit strings that do not begin with two Os b) the set of bit strings that do not end with two Os c) the set of bit strings that contain at most one 0 (that is, that do not contain at least two Os) 41. Use the procedure you described in Exercise 39 and the finite-state automata you constructed in Exercise 25 to find a deterministic finite-state automaton that recognizes the set of all bit strings that do not contain the string 101. 42. Use the procedure you described in Exercise 39 and the finite-state automaton you constructed in Exercise 29 to find a deterministic finite-state automaton that recognizes the set of all bit strings that do not contain three consec­ utive Is. 816 12/ Modeling Computation In Exercises 43-49 find the language recognized by the given nondeterministic finite-state automaton. o 45. o o 0, \ 47. o o 0, \ \,0 12-32 50. Find a deterministic finite-state automaton that recognizes the same language as the nondeterministic finite-state au­ tomaton in Exercise 43. 51. Find a deterministic finite-state automaton that recognizes the same language as the nondeterministic finite-state au­ tomaton in Exercise 44. 52. Find a deterministic finite-state automaton that recognizes the same language as the nondeterministic finite-state au­ tomaton in Exercise 45. 53. Find a deterministic finite-state automaton that recognizes the same language as the nondeterministic finite-state au­ tomaton in Exercise 46. 54. Find a deterministic finite-state automaton that recognizes the same language as the nondeterministic finite-state au­ tomaton in Exercise 47. 55. Find a deterministic finite-state automaton that recognizes each of these sets. a) (OJ b) {I,OO} c) on I n = 2, 3, 4, ... } 56. Find a nondeterministic finite-state automaton that recog­ nizes each of the languages in Exercise 27, and has fewer states, if possible, than the deterministic automaton you found in that exercise. *57. Show that there is no finite-state automaton that recog­ nizes the set of bit strings containing an equal number of Os and Is. In Exercises 58-62 we introduce a technique for construct­ ing a deterministic finite-state machine equivalent to a given deterministic finite-state machine with the least number of states possible. Suppose that M = (S, I, f, So, F) is a finite­ state automaton and that k is a nonnegative integer. Let Rk be the relation on the set S of states of M such that s Rkt if and only iffor every input string x with l(x) ::s k [where l(x) is the length of x, as usual], f(s, x) and f(t, x) are both final states or both not final states. Furthermore, let R* be the relation on the set of states of M such that s R*t if and only if for every input string x, regardless of length, f(s, x) and f(t, x) are both final states or both not final states. *58. a) Show that for every nonnegative integer k, Rk is an equivalence relation on S. We say that two states s and t are k-equivalent if s Rkt. b) Show that R* is an equivalence relation on S. We say that two states s and t are *-equivalent if s R*t. c) Show that if sand t are two k-equivalent states of M, where k is a positive integer, then sand k are also (k -1 )-equivalent d) Show that the equivalence classes of Rk are a refine­ ment of the equivalence classes of Rk-I if k is a pos­ itive integer. (The refinement of a partition of a set is defined in the preamble to Exercise 49 in Section 8.5.) e) Show that if s and t are k-equivalent for every non­ negative integer k, then they are *-equivalent. 1) Show that all states in a given R*-equivalence class are final states or all are not final states. g) Show that if s and tare R*-equivalent, then f(s, a) and f(t, a) are also R*-equivalent for all a E I. 12-33 *59. Show that there is a nonnegative integer n such that the set of n-equivalence classes of states of M is the same as the set of (n + I)-equivalence classes of states of M. Then show for this integer n, the set of n-equivalence classes of states of M equals the set of *-equivalence classes of states of M. The quotient automaton M of the deterministic finite-state automaton M = (S, I, f, So, F) is the finite-state automaton (S, 1,7, [SO]R*, F), where the set of states S is the set of *-equivalence classes of S, the transition function 7 is defined by 7([S]R*, a) = [f(s, a)]R* for all states [s] of M and input symbols a E I, and F is the set consisting of R*-equivalence classes of final states of M. *60. a) Show that s arid t are O-equivalent if and only if either both s and t are final states or neither s nor t is a final .; state. Conclude that each final state of M, which is an R*-equivalence class, contains only final states of M. b) Show that if k is a positive integer, then s and t are k­ equivalent if and only if s and t are (k -I)-equivalent and for every input symbol a E I, f(s, a) and f(t, a) are (k -I)-equivalent. Conclude that the transition function 7 is well-defined. c) Describe a procedure that can be used to construct the quotient automaton of a finite-automaton M. 12.4 Language Recognition Introduction 12.4 Language Recognition 817 **61. a) Show that if M is a finite-state automaton, then the quotient automaton M recognizes the same language asM. b) Show that if M is a finite-state automaton with the property that for every state s of M there is a string x E J* such that f(so, x) = s, then the quotient au­ tomaton M has the minimum number of states of any . finite-state automaton equivalent to M. 62. Answer these questions about the finite-state automaton M shown here. a) Find the k-equivalence classes of M for k = 0, I, 2, and 3. Also, find the *-equivalence classes of M. b) Construct the quotient automaton M of M. We have seen that finite-state automata can be used as language recognizers. What sets can be recognized by these machines? Although this seems like an extremely difficult problem, there is a simple characterization of the sets that can be recognized by finite state automata. This problem was first solved in 1956 by the American mathematician Stephen Kleene. He showed that there is a finite-state automaton that recognizes a set if and only if this set can be built up from the null set, the empty string, and singleton strings by taking concatenations, unions, and Kleene closures, in arbitrary order. Sets that can be built up in this way are called regular sets. Regular grammars were defined in Section 12.1. Because of the terminology used, it is not surprising that there is a connection between regular sets, which are the sets recognized by finite-state automata, and regular grammars. In particular, a set is regular if and only if it is generated by a regular gramm ar. Finally, there are sets that cannot be recognized by any finite-state automata. We will give an example of such a set. We will briefly discuss more powerful models of computation, such as pushdown automata and Turing machines, at the end of this section. Regular Sets The regular sets are those that can be formed using the operations of concatenation, union, and Kleene closure in arbitrary order, starting with the empty set, the empty string, and singleton sets. We will see that the regular sets are those that can be recognized using a finite-state automaton. To define regular sets we first need to define regular expressions. 818 12/ Modeling Computation 12-34 DEFINITION 1 The regular expressions over a set I are defined recursively by: the symbol'" is a regular expression; the symbol A is a regular expr ession; the symbol x is a regular expression whenever x E I; the symbols (AB), (A U B), and A * are regular expressions whenever A and B are regular expressions. Each regular expression represents a set specified by these rules: '" represents the empty set, that is, the set with no strings; A represents the set {A}, which is the set containing the empty string; x represents the set {x} containing the string with one symbol x; (AB) represents the concatenation of the sets represented by A and by B; (A U B) represents the union ofthe sets represented by A and by B; A * represents the Kleene closure of the set represented by A. Sets represented by regular expr essions are called regular sets. Henceforth regular expressions will be used to describe regular sets, so when we refer to the regular set A, we will mean the regular set represented by the regular expression A. Example I shows how regular expressions are used to specify regular sets. EXAMPLE 1 What are the strings in the regular sets specified by the regular expressions 10*, (10)*, 0 U 01, 0(0 U 1)*, and (0*1)*? Solution: The regular sets represented by these expressions are given in Table 1 ,as the reader should verify. .... Finding a regular expression that specifies a given set can be quite tricky, as Example 2 illustrates. EXAMPLE 2 Find a regular expression that specifies each of these sets: (a) the set of bit strings with even length (b) the set of bit strings ending with a 0 and not containing 11 (c) the set of bit strings containing an odd number of Os Solution: (a) To construct a regular expression for the set of bit strings with even length, we use the fact that such a string can be obtained by concatenating zero or more strings each consisting of two bits. The set of strings of two bits is specified by the regular expression (00 U 01 U 10 U 11). Consequently, the set of strings with even length is specified by (00 U 01 U 10 U 11)*. TABLE 1 Expression Strings 10* a I followed by any number of Os (including no zeros) (10)* any number of copies of 10 (including the null string) OUOI the string 0 or the string 0 I O(OU 1)* any string beginning with 0 (0·1)· any string not ending with 0 12-35 THEOREM l unks� 12.4 Language Recognition 819 (b) A bit string ending with a 0 and not containing 11 must be the concatenation of one or more strings where each string is either a 0 or a 10. (To see this, note that such a bit string must consist of 0 bits or 1 bits each followed by a 0; the string cannot end with a single 1 because we know it ends with a 0.) It follows that the regular expression (0 U 10)* (0 U 10) specifies the set of bit strings that do not contain 11 and end with a O. [Note that the set specified by (0 U 10)* includes the empty string, which is not in this set, because the empty string does not end with a 0.] (c) A bit string containing an odd number of Os must contain at least one 0, which tells us that it starts with zero or more 1 s, followed by a 0, followed by zero or more 1 s. That is, each such bit string begins with a string of the form 1 j 01 k for nonnegative integers j and k. Because the bit string contains an odd number of Os, additional bits after this initial block can be split into blocks each starting with a 0 and containing one more O. Each such block is of the form 01 PO 1 q, where p and q are nonnegative integers. Consequently, the regular expression 1*01*(01 *01 *)* specifies the set of bit strings with an odd number of Os. .... Kleene's Theorem In 1956 Kleene proved that regular sets are the sets that are recognized by a finite-state automaton. Consequently, this important result is called Kleene's Theorem. KLEENE'S THEOREM A set is regular if and only if it is recognized by a finite-state automaton. Kleene's Theorem is one of the central results in automata theory. We will prove the only if part of this theorem, namely, that every regular set is recognized by a finite-state automaton. The proof of the if part, that a set recognized by a finite-state automaton is regular, is left as an exercise for the reader. Proof: Recall that a regular set is defined in terms of regular expressions, which are defined recursively. We can prove that every regular set is recognized by a finite-state automaton if we can do the following things. 1. Show that 0 is recognized by a finite-state automaton. 2. Show that {J...} is recognized by a finite-state automaton. 3. Show that fa} is recognized by a finite-state automaton whenever a is a symbol in I. 4. Show that AB is recognized by a finite-state automaton whenever both A and Bare. 5. Show that A U B is recognized by a finite-state automaton whenever both A and Bare. 6. Show that A * is recognized by a finite-state automaton whenever A is. We now consider each of these tasks. First, we show that 0 is recognized by a nondeterministic finite-state automaton . To do this, all we need is an automaton with no final states. Such an automaton is shown in Figure l(a). Second, we show that {J...} is recognized by a finite-state automaton. To do this, all we need is an automaton that recognizes J..., the null string, but not any other string. This can be done by making the start state So a final state and having no transitions, so that no other string takes So to a final state. The nondeterministic automaton in Figure 1(b) shows such a machine. Third, we show that {a} is recognized by a nondeterministic finite-state automaton. To do this, we can use a machine with a starting state So and a final state S\. We have a transition from 820 121 Modeling Computation 12-36 start-G) Start-® start� (a) (b) (c) FIGURE 1 Nondeterministic Finite-State Automata That Recognize Some Basic Sets. So to Sl when the input is a, and no other transitions. The only string recognized by this machine is a. This machine is shown in Figure l(c). Next, we show that AB and AU B can be recognized by finite-state automata if A and B are languages recognized by finite-state automata. Suppose that A is recognized by M A = (SA. I,/A. SA. FA) and B is recognized by MB = (SB. I,/B' SB. FB). We begin by constructing a finite-state machine MAB = (SAB. I. /AB. SAB. FAB) that recog­ nizes A B, the concatenation of A and B. We build such a machine by combining the machines fot A and B in series, so a string in A takes the combined machine from SA, the start state of M A, to S B, the start state of M B. A string in B should take the combined machine from S B to a final state of the combined machine. Consequently, we make the following construction. Let SAB be SA U SB. The starting state SAB is the same as SA. The set of final states, FAB, is the set of final states of M B with S AB included if and only if A e A n B. The transitions in M AB include all transitions in MA and in MB, as well as some new transitions. For every transition in MA that leads to a final state, we form a transition in MAB from the same state to SB, on the same input. In this way, a string in A takes MAB from SAB to SB, and then a string in B takes SB to a final state of MAB. Moreover, for every transition from SB we form a transition in MAB from SAB to the same state. Figure 2(a) contains an illustration of this construction. We now construct a machine M AUB = {S AUB. I. /AuB. S AUB. FAUB} that recognizes A U B. This automaton can be constructed by combining MA and M B in parallel, using a new start state that has the transitions that both SA and SB have. Let SAUB = SA USB U {SAUB}, where SAUB is a new state that is the start state of MAUB. Let the set of final states FAUB be FA U FB U {SAUB} if A eA U B, and FA U FB otherwise. The transitions in MAUB include all those in MA and in M B. Also, for each transition from SA to a state S on input i we include a transition from S AUB to S on input i, and for each transition from S B to a state S on input i we include a transition from S AUB to S on input i. In this way, a string in A leads from S AUB to a final state in the new machine, and a string in B leads from S AUB to a final state in the new machine. Figure 2(b) illustrates the construction of MAUB. Finally, we construct MA* = (SA*' I. /A*,sA*' FA*), a machine that recognizes A*, the Kleene closure of A. Let SA * include all states in SA and one additional state SA *, which is the starting state for the new machine. The set of final states FA * includes all states in FA as well as the start state sA *, because A must be recognized. To recognize concatenations of arbitrarily many strings from A, we include all the transitions in M A, as well as transitions from SA * that match the transitions from SA, and transitions from each final state that match the transitions from SA. With this set of transitions, a string made up of concatenations of strings from A will take SA * to a final state when the first string in A has been read, returning to a final state when the second string in A has been read, and so on. Figure 2( c) illustrates the construction we used. <I A nondeterministic finite-state automaton can be constructed for any regular set using the procedure described in this proof. We illustrate how this is done with Example 3. EXAMPLE 3 Construct a nondeterministic finite-state automaton that recognizes the regular set 1 * U 01. Solution: We begin by building a machine that recognizes 1 *. This is done using the machine that recognizes 1 and then using the construction for M A* described in the proof. Next, we build 12-37 (a) Transition to final state in M A produces a transition to sB' i Transition from sB in MB produces a transition from sAB = sA-12.4 Language Recognition 821 Start state is sAB = SA' which is final if SA and sB are final. Final states include all finite states of M B' (b) Final states are the final states in M A and M B' SAUB is the new start state. which is final if SA or sB is final. (c) Transitions from SA produce A transitions from sA* and all final states of MA- SA* is the new start state, which is a final state. Final states include all final states in M A- FIGURE 2 Building Automata to Recognize Concatenations, Unions, and Kleene Closures. a machine that recognizes 01, using machines that recognize 0 and 1 and the construction in the prooffor MAB• Finally, using the construction in the proof for MAUB, we construct the machine for 1 * U 01. The finite-state automata used in this construction are shown in Figure 3. The states in the successive machines have been labeled using different subscripts, even when a state is formed from one previously used in another machine. Note that the construction given here does not produce the simplest machine that recognizes 1 * U 01. A much simpler machine that recognizes this set is shown in Figure 3(b). <II1II Regular Sets and Regular Grammars In Section 12.1 we introduced phrase-structure grammars and defined different types of gram­ mars. In particular we defined regular, or type 3, grammars, which are grammars of the form G = (V, T, S, P), where each production is of the form S -+ A, A -+ a, or A -+ aB, where a 822 12/ Modeling Computation (a) Machine Set recognized _St_ar_t �.��r------------ -.� Start 1* o o Start 01 Start 1* UOI (b) I'"UOI Start FIGURE 3 Nondeterministic Finite-State Automata Recognizing 1* U 01. 12-38 12-39 THEOREM 2 EXAMPLE 4 FIGURE 4 A Nondeterministic Finite-State Automaton Recognizing L( G). 12.4 Language Recognition 823 is a terminal symbol, and A and B are nonterminal symbols. As the terminology suggests, there is a close connection between regular grammars and regular sets. A set is generated by a regular grammar if and only if it is a regular set. Proof: First we show that a set generated by a regular grammar is a regular set. Suppose that G = (V, T, S, P) is a regular grammar generating the set L(G). To show that L(G) is regular we will build a nondeterministic finite-state machine M = (S, I, f, So, F) that recognizes L( G). Let S, the set of states, contain a state SA for each nonterminal symbol A of G and an additional state SF, which is a final state. The start state So is the state formed from the start symbol S. The transitions of M are formed from the productions of G in the following way. A transition from SA to SF on input of a is included if A � a is a production, and a transition from SA to S B on input of a is included if A � a B is a production. The set of final states includes SF and also includes So if S � A is a production in G. It is not hard to show that the language recognized by M equals the language generated by the grammar G, that is, the L( M) = L( G). This can be done by determining the words that lead to a final state. The details are left as an exercise for the reader. <l Before giving the proof of the converse, we illustrate how a nondeterministic machine is constructed that recognizes the same set as a regular grammar. Construct a nondeterministic finite-state automaton that recognizes the language generated by theregulargrammarG = (V, T, s, P), where V = {O, I, A, S}, T = (O, l},andtheproductions in P are S � lA, S � 0, S � A, A � OA, A -+ lA, and A � 1. Solution: The state diagram for a nondeterministic finite-state automaton that recognizes L( G) is shown in Figure 4. This automaton is constructed following the procedure described in the proof. In this automaton, So is the state corresponding to S, SI is the state corresponding to A, and S2 is the final state. � We now complete the proof of Theorem 2. Proof: We now show that if a set is regular, then there is a regular grammar that generates it. Suppose that M is a finite-state machine that recognizes this set with the property that So, the starting state of M, is never the next state for a transition. (We can find such a machine by Exercise 20.) The grammar G = (V, T, S, P) is defined as follows. The set V of symbols of G is formed by assigning a symbol to each state of S and each input symbol in I. The set T of terminal symbols of G is the symbols of G formed from the input symbols in I. The start symbol S is the symbol formed from the start state So. The set P of productions in G is formed from the transitions in M. In particular, if the state S goes to a final state under input a, then the production As � a is included in P, where As is the nonterminal symbol formed from the state s. If the state S goes to the state t on input a, then the production As � aAt is included in P. The production S � A is included in P if and only if A E L(M). Because the productions of G correspond to the transitions of M and the productions leading to terminal\! correspond to transitions to final states, it is not hard to show that L(G) = L(M). We leave the details as an exercise for the reader. <l Example 5 illustrates the construction used to produce a grammar from an automaton that generates the language recognized by this automaton. 824 12/ Modeling Computation 12-40 EXAMPLE 5 Find a regular grammar that generates the regular set recognized by the finite-state automaton FIGURE 5 A Finite-State Automaton. EXAMPLE 6 shown in Figure 5. Solution: The grammar G = (V. T. S. P) generates the set recognized by this automaton where V = {So A. B. O. I}; where the symbols S. A, and B correspond to the states So, SJ, and S2, re­ spectively; T = {O. I}; S is the start symbol; and the productions are S -+ OA, S -+ IB, S -+ 1, S -+ A, A -+ OA, A -+ IB, A -+ I, B -+ OA, B -+ IB, and B -+ 1. .... A Set Not Recognized by a Finite-State Automaton We have seen that a set is recognized by a finite-state automaton if and only if it is regular. We will now show that there are sets that are not regular by describing one such set. The technique used to show that this set is not regular illustrates an important method for showing that certain sets are not regular. Show that the set {on 1 n 1 n = O. 1.2 •... }, made up of all strings consisting of a block of Os followed by a block of an equal number of 1 s, is not regular. Solution: Suppose that this set were regular. Then there would be a non-deterministic finite-state automaton M = (S. I. f, so. F) recognizing it. Let N be the number of states in this machine, that is, N = 1 S I. Because M recognizes all strings made up of a number of Os followed by an equal number of Is, M must recognize ON 1 N. Let so. Sl. S2 •... • S2N be the sequence of states that is obtained starting at So and using the symbols of ON I N as input, so that Sl = f(so. 0), S2 = j(SI.O) •... • SN = f(SN-l.O).SN+l = f(SN.I) •... • S2N = f(S2N-l.I).Notethats2N is a final state. Because there are only N states, the pigeonhole principle shows that at least two of the first N + I of the states, which are So •... • SN, must be the same. Say that Sj and Sj are two such identical states, with 0 � i < j � N. This means that f(sj. ot) = sj. where t = j -i. It follows that there is a loop leading from Sj back to itself, obtained using the input 0 a total of t times, in the state diagram shown in Figure 6. Now consider the input string ON ot IN = ON +t IN. There are t more consecutive Os at the start of this block than there are consecutive Is that follow it. Because this string is not ofthe form on In (because it has more Os that Is), it is not recognized by M. Consequently, f(so. ON+t IN) cannot be a final state. However, when we use the string ON +t I N as input, we end up in the same state as before, namely, S2N. The reason for this is that the extra t Os in this string take us around the loop from Sj back to itself an extra time, as shown in Figure 6. Then the rest of the string Start FIGUR E 6 The Path Produced by ONIN. �:.� ... � 12-41 unkS� Exercises 12.4 Language Recognition 825 leads us to exactly the same state as before. This contradiction shows that {onln In = 1,2, ... } ��re�� � More Powerful Types of Machines Finite-state automata are unable to carry out many computa tions. The main limitation of these machines is their finite amount of memory. This prevents them from recognizing languages that are not re�lar, such as {onln In = 0,1,2, ... }. Because a set is re�lar if and only if it is the lan�age generated by a re�lar grammar, Example 5 shows that there is no re�lar grammar that generates the set {on In I n = 0, 1,2, ... }. However, there is a context-free grammar that recognizes this set. Such a grammar was given in Example 5 in Section 12.1. Because of the limitations of finite-state machines, it is necessary to use other, more pow­ erful, models of computation. One such model is the pushdown automaton. A pushdown automaton includes everything in a finite-state automaton, as well as a stack, which provides unlimited memory. Symbols can be placed on the top or taken off the top of the stack. A set is recognized in one of two ways by a pushdown automaton. First, a· set is recognized if the set consists of all the strings that produce an empty stack when they are used as input. Second, a set is recognized if it consists of all the strings that lead to a final state when used as input. It can be shown that a set is recognized by a pushdown automaton if and only if it is the lan�age generated by a context-free grammar. However, there are sets that cannot be expressed as the lan�age generated by a context­ free grammar. One such set is {onln2n In = 0,1,2, ... }. We will indicate why this set cannot be recognized by a pushdown automaton, but we will not give a proof, because we have not developed the machinery needed. (However, one method of proof is given in Exercise 28 of the supplementary exercises at the end of this chapter.) The stack can be used to show that a string begins with a sequence of Os followed by an equal number of 1 s by placing a symbol on the stack for each 0 (as long as only Os are read), and removing one ofthese symbols for each 1 (as long as only I s following the Os are read). But once this is done, the stack is empty, and there is no way to determine that there are the same number of 2s in the string as Os. There are other machines called linear bounded automata, more powerful than push­ down automata, that can recognize sets such as {on 1 n 2n I n = 0, 1, 2, ... }. In particular, linear bounded automata can recognize context-sensitive language s. However, these machines cannot recognize all the languages generated by phrase-structure grammars. To avoid the limitations of special types of machines, the model known as a Turing machine, named after the British mathema tician Alan Turing, is used. A Turing machine is made up of everything included in a finite-state machine together with a tape, which is infinite in both directions. A Turing machine has read and write capabilities on the tape, and it can move back and forth along this tape. Turing machines can recognize alllan�ages generated by phrase-structure grammars. In addition, Tur­ ing machines can model all the computations that can be performed on a computing machine. Because of their power, Turing machines are extensively studied in theoretical computer science. We will briefly study them in Section 12.5. 1. Describe in words the strings in each of these regular sets. 2. Describe in words the strings in each of these regular sets. a) 1*0 b) 1*00* c) 111 U 001 d) (1 U 00)* e) (00*1)* f) (0 U 1)(0 U 1)*00 a) 001* b) (01)* c) 01 U 001 * d) 0(11 U 0)* e) (101*)* f) (O*Ul)11 826 12 / Modeling Computation 3. Determine whether 0101 belongs to each of these regular sets. a) 01*0* c) 0(10)*1* e) (01)*(11)* g) 0*(10)*11 b) 0(11)*(01)* d) 0*10(0 U 1) 1) 0*(10 U 11)* b) 01(01 U 0)1* 4. Determine whether 10 11 belongs to each ofthese regular sets. a) 10*1* c) 1(01)*1* e) (10)*(11)* g) (10)*1011 b) 0*(10 U 11)* d) 1*01(0 U 1) 1) 1(00)*(11)* b) (1 U 00)(01 U 0)1* 5. Express each of these sets using a regular expression. a) the set consisting of the strings 0, 11, and 010 b) the set of strings of three Os followed by two or more Os c) the set of strings of odd length d) the set of strings that contain exactly one 1 e) the set of strings ending in 1 and not containing 000 6. Express each of these sets using a regular expression. a) the set containing all strings with 0, I, or 2 bits b) the set of strings of two Os, followed by zero or more 1 s, and ending with a 0 c) the set of strings with every I followed by two Os d) the set of strings ending in 00 and not containing 11 e) the set of strings containing an even number of 1 s 7. Express each ofthese sets using a regular expression. a) the set of strings of one or more Os followed by a 1 b) the set of strings of two or more symbols followed by three or more Os c) the set of strings with either no 1 preceding a 0 or no o preceding a 1 LiI1IIS� 12-42 d) the set of strings containing a string of 1 s such that the number of Is equals 2 modulo 3, followed by an even number of Os 8. Construct deterministic finite-state automata that recog­ nize each of these sets from 1*, where I is an alphabet. a) 10 b) {A} c) {a}, where a E I 9. Construct nondeterministic finite-state automata that recognize each of the sets in Exercise 8. 10. Construct nondeterministic finite-state automata that recognize each of the following sets a) {A,O}. b) to, Il}. c) to, 11, OOO}. *11. Show that if A is a regular set, then A R, the set of all reversals of strings in A, is also regular. 12. Using the constructions described in the proof of Kleene's Theorem, find nondeterministic finite-state automata that recognize each of these sets. a) 01* b) (0 U 1)1* c) 00(1* U 10) 13. Using the constructions described in the proof ofKleene 's Theorem, find nondeterministic finite-state automata that recognize each of these sets. a) 0*1* b) (0 U 11)* c) 01* U 00*1 14. Construct a nondeterministic finite-state automaton that recognizes the language generated by the regular gram­ mar G = (V, T, S, P), where V = to, 1, S, A, B}, T = to, I}, S is the start symbol, and the set of productions is a) S � OA, S � 1 B, A � 0, B � O. b) S � lA, S � 0, S � A, A � OB, B � IB, B � 1. c) S � IB, S � 0, A � lA, A � OB, A � 1, A � 0, B � 1. In Exercises 15-17 construct a regular grammar G = (V, T, S, P) that generates the language recognized by the given finite-state machine. ALAN MATHISON TURING (1912-1954) Alan Turing was born in London, although he was conceived in India, where his father was employed in the Indian Civil Service. As a boy, he was fascinated by chemistry, performing a wide variety of experiments, and by machinery. Turing attended Sherborne, an English boarding school. In 1931 he won a scholarship to King's College, Cambridge. After completing his dissertation, which included a rediscovery ofthe Central Limit Theorem, a famous theorem in statistics, he was elected a fellow of his college. In 1935 Turing became fascinated with the decision problem, a problem posed by the great German mathematician Hilbert, which asked whether there is a general method that can be applied to any assertion to . determine whether the assertion is true. Turing enjoyed running (later in life running as a serious amateur in competitions), and one day, while resting after a run, he discovered the key ideas needed to solve the decision problem. In his solution, he invented what is now called a Thring machine as the most general model of a computing machine. Using these machines, he found a problem, involving what he called computable numbers, that could not be decided using a general method. From 1936 to 1938 Turing visited Princeton University to work with Alonzo Church, who had also solved Hilbert's decision problem. In 1939 Turing returned to King's College. However, at the outbreak of World War II, he joined the Foreign Office, performing cryptanalysis of German ciphers. His contribution to the breaking of the code of the Enigma, a mechanical German cipher machine, played an important role in winning the war. After the war, Turing worked on the development of early computers. He was interested in the ability of machines to think, proposing that if a computer could not be distinguished from a person based on written replies to questions, it should be considered to be ''thinking.'' He was also interested in biology, having written on morphogene sis, the development of form in organisms. In 1954 Turing committed suicide by taking cyanide, without leaving a clear explanation. Legal troubles related to a homosexual relationship and hormonal treatments mandated by the court to lessen his sex drive may have been factors in his decision to end his life. 12-43 15. Start 0, I J 16. Start 17. Start 18. Show that the finite-state automaton constructed from a regular grammar in the proof of Theorem 2 recognizes the set generated by this grammar. 19. Show that the regular grammar constructed from a finite­ state automaton in the proof of Theorem 2 generates the set recognized by this automaton. 20. Show that every nondeterministic finite-state automaton is equivalent to another such automaton that has the prop­ erty that its starting state is never revisited. *21. Let M = (S. J. J, So. F) be a deterministic finite-state au­ tomaton. Show that the language recognized by M. L(M), is infinite if and only if there is a word x recognized by M with l(x) � lSI. *22. One important technique used to prove that certain sets are C not regular is the pumping lemma. The pumping lemma states that if M = (S. J. f. so. F) is a deterministic finite- 12.5 Turing Machines Introduction 12.5 Turing Machines 827 state automaton and if x is a string in L( M), the language recognized by M, with l(x) � lSI, then there are strings u, v, and w in 1* such that x = uvw, l(uv) � lSI and l(v) � l,anduviw E L(M) fori = 0.1.2 ..... Provethe pumping lemma. [Hint: Use the same idea as was used in Example 5.] *23. Show that the set {02n 1 n} is not regular using the pumping lemma given in Exercise 22. *24. Show that the set {I n2 I n = O. 1. 2 •... } is not regular us­ ing the pumping lemma from Exercise 22. *25. Show that the set of palindromes over {O. I} is not reg­ ular using the pumping lemma given in Exercise 22. [Hint: Consider strings of the form ON ION.] **26. Show that a set recognized by a finite-state automaton is regular. (This is the if part of Kleene's Theorem.) Suppose that L is a subset of 1*, where J is a nonempty set of symbols. Ifx E 1*, we let L/x = {z E J* I xz E L}. We say that the strings x E J* and y E 1* are distinguishable with respect to L if L/x # L/y. A string z for which xz E L but yz ¢ L or xz ¢ L but yz E L is said to distinguish x and y with respect to L. When L / x = L / y, we say that x and y are indistinguishable with respect to L. 27. Let L be the set of all bit strings that end with 01. Show that 11 and 10 are distinguishable with respect to L and that the strings 1 and 11 are indistinguishable with respect to L. 28. SupposethatM = (S. J. f. So. F)isadeterministicfinite­ state machine. Show that if x and y are two strings in J* that are distinguishable with respect to L(M), then f(so. x) # f(so. y). *29. Suppose that L is a subset of 1* and for some positive inte­ ger n there are n strings in J* such that every two ofthese . strings are distinguishable with respect to L. Prove that every deterministic finite-state automaton recognizing L has at least n states. *30. Let Ln be the set of strings with at least n bits in which the nth symbol from the end is a O. Use Exercise 29 to show that a deterministic finite-state machine recognizing Ln must have at least 2n states. *31. Use Exercise 29 to show that the language consisting of all bit strings that are palindromes (that is, strings that equal their own reversals) is not regular. unks� The finite-state automata studied earlier in this chapter cannot be used as general models of computati on. They are limited in what they can do. For example, finite-state automata are able to recognize regular sets, but are not able to recognize many easy-to-describe sets, including {onln In:::: O}, which computers recognize using memory. We can use finite-state automata to compute relatively simple functions such as the sum of two numbers, but we cannot use them to compute functions that computers can, such as the product of two numbers. To overcome these 828 12/ Modeling Computation 12-44 DEFINITION 1 deficiencies we can use a more powerful type of machine known as a Turing machine, after Alan Turing, the famous mathematician and computer scientist who invented them in the 1930s. Basically, a Turing machine consists of a control unit, which at any step is in one of finitely many different states, together with a tape divided into cells, which is infinite in both directions. Turing machines have read and write capabilities on the tape as the control unit moves back and forth along this tape, changing states depending on the tape symbol read. Turing machines are more powerful than finite-state machines because they include memory capabilities that finite­ state machines lack. We will show how to use Turing machines to recognize sets, including sets that cannot be recognized by finite-state machines. We will also show how to compute functions using Turing machines. Turing machines are the most general models of computation; essentiall y, they can do whatever a computer can do. Note that Turing machines are much more powerful than real computers, which have finite memory capabilities. Definition of Turing Machines We now give the formal definition of a Turing machine. Afterward we will explain how this formal definition can be interpreted in terms of a control head that can read and write symbols on a tape and move either right or left along the tape. A Turing machine T = (8, I, f, so) consists of a finite set 8 of states, an alphabet I containing the blank symbol B, a partial function f from 8 x I to 8 x I x {R, L}, and a starting state so. Recall from the preamble to Exercise 73 in Section 2.3 that a partial function is defined ()nly for those elements in its domain of definition. This means that for some (state, symbol) pairs the partial function f may be undefined, but for a pair for which it is defined, there is a unique (state, symbol, direction) triple associated to this pair. We call the'five-tuples corresponding to the partial function in the definition of a Turing machine the transition rules of the machine. To interpret this definition in terms of a machine, consider a control unit and a tape divided into cells, infinite in both directions, having only a finite number of nonblank symbols on it at any given time, as pictured in Figure 1. The action of the Turing machine at each step of its operation depends on the value of the partial function f for the current state and tape symbol. At each step, the control unit reads the current tape symbol x. If the control unit is in state s and ifthe partial function f is defined for the pair (s, x) with f(s, x) = (s', x', d), the control unit 1. enters the state s' , 2. writes the symbol x' in the current cell, erasing x, and 3. moves right one cell if d = R or moves left one cell if d = L. We write this step as the five-tuple (s, x, s', x', d). If the partial function f is undefined for the pair (s, x), then the Turing machine T will halt. Tape is infinite in both directions. Only finitely many nonblank cells at any time. FIGURE 1 A Representation ofa Turing Machine. 12-45 EXAMPLE l Ex_=iC 12.5 Turing Machines 829 A common way to define a Turing machine is to specify a set of five-tuples of the form (s, x, s', x', d). The set of states and input alphabet is implicitly defined when such a definition is used. At the beginning of its operation a Turing machine is assumed to be in the initial state So and to be positioned over the leftmost nonblank symbol on the tape. If the tape is all blank, the control head can be positioned over any cell. We will call the positioning of the control head over the leftmost nonblank tape symbol the initial position of the machine. Example 1 illustrates how a Turing machine works. What is the final tape when the Turing machine T defined by the seven five-tuples (so, 0, so, 0, R), (so,l,sl,I,R), (SO,B,S3,B,R), (sl,O,so,O,R), (sl,l,s2,0,L) (sl,B,S3,B,R), and (S2, 1, S3, 0, R) is run on the tape shown in Figure 2(a)? Solution: We start the operation with T in state So and with T positioned over the leftmost nonblank symbol on the tape. The first step, using the five-tuple (so, 0, So, 0, R), reads the ° in the leftmost nonblank cell, stays in state So, writes a ° in this cell, and moves one cell right. (a) Initial Position (b) � ···1 B I B 0 0 1 I 1 I 0 IB IB (c) � ···1 B I B 0 I 1 1 I 1 I o I B I B I .. · (d) m ···1 B I B I 0 1 I 0 o I B IB I 1 I (e) R ···1 B IB 0 1 I 0 0 IB IB I 1 I (0 R ···1 B I B 0 1 I 0 o I B IB 101 (g) � ···1 B IB 0 1 I 0 I 0 o I B IB I .. · Machine Halts FIGURE 2 The Steps Produced by Running T on the Tape in Figure 1. 830 121 Modeling Computation 12-46 DEFINITION 2 The second step, using the five-tuple (so, 1, SI, 1, R), reads the 1 in the current cell, enters state SI, writes a 1 in this cell, and moves one cell right. The third step, using the five-tuple (SI' 0, so, 0, R), reads the ° in the current cell, enters state so, writes a ° in this cell, and moves one cell right. The fourth step, using the five-tuple (so, 1, SI, 1, R), reads the 1 in the current cell, enters state SI, writes a 1 in this cell, and moves right one cell. The fifth step, using the five-tuple (SI, 1, S2, 0, L), reads the 1 in the current cell, enters state S2, writes a ° in this cell, and moves left one cell. The sixth step, using the five-tuple (S2' 1, S3, 0, R), reads the 1 in the current cell, enters the state S3, writes a ° in this cell, and moves right one cell. Finally, in the seventh step, the machine halts because there is no five-tuple beginning with the pair (S3, 0) in the description of the machine. The steps are shown in Figure 2. Note that T changes the first pair of consecutive Is on the tape to Os and then halts . .... Using Turing Machines to Recognize Sets Turing machines can be used to recognize sets. To do so requires that we define the concept of a final state as follows. Afinal state of a Turing machine T is a state that is not the first state in any five-tuple in the description of T using five-tuples (for example, state S3 in Example 1). We can now define what it means for a Turing machine to recognize a string. Given a string, we write consecutive symbols in this string in consecutive cells. Let V be a subset of an alphabet I. A Turing machine T = (S, I, f, so) recognizes a string x in V* if and only if T, starting in the initial position when x is written on the tape, halts in a final state. T is said to recognize a subset A of V* if x is recognized by T if and only if x belongs to A. Note that to recognize a subset A of V* we can use symbols not in V. This means that the input alphabet I may include symbols not in V. These extra symbols are often used as markers (see Example 3). When does a Turing machine T not recognize a string x in V*? The answer is that x is not recognized if T does not halt or halts in a state that is not final when it operates on a tape containing the symbols of x in consecutive cells, starting in the initial position. (The reader should understand that this is one of many possible ways to define how to recognize sets using Turing machines.) We illustrate this concept with Example 2. EXAMPLE 2 Find a Turing machine that recognizes the set of bit strings that have a 1 as their second bit, that is, the regular set (0 U 1)1(0 U 1)*. Solution: We want a Turing machine that, starting at the leftmost nonblank tape cell, moves right, and determines whether the second symbol is a 1. If the second symbol is 1, the machine should move into a final state. If the second symbol is not a 1, the machine should not halt or it should halt in a nonflnal state. To construct such a machine, we include the five-tuples (so, 0, SI, 0, R) and (so, 1, SI, 1, R) to read in the first symbol and put the Turing machine in state SI. Next, we include the five-tu ples (sJ, 0, S2, 0, R) and (SI, 1, S3, 1, R) to read in the second symbol and either move to state S2 if this symbol is a 0, or to state S3 if this symbol is a 1. We do not want to recognize strings that have a ° as their second bit, so S2 should not be a final state. We want S3 to be a final state. So, we can include the five-tuple (S2, 0, S2, 0, R). Because we do not want to recognize the empty string or a string with one bit, we also include the five-tuples (so. B. S2, 0, R) and (SI' B, S2, 0, R). 12-47 12.5 Turing Machines 831 The Turing machine T consisting of the seven five-tuples listed here will terminate in the final state S3 if and only if the bit string has at least two bits and the second bit of the input string is a 1. If the bit string contains fewer than two bits or if the second bit is not aI, the machine will terminate in the nonfinal state S2. .... Given a regular set, a Turing machine that always moves to the right can be built to recognize this set (as in Example 2). To build the Turing machine, first find a finite-state automaton that recognizes the set and then construct a Turing machine using the transition function of the finite-state machine, always moving to the right. We will now show how to build a Turing machine that recognizes a nonregular set. EXAMPLE 3 Find a Turing machine that recognizes the set {on 1 n I n � I}. Solution: To build such a machine, we will use an auxiliary tape symbol M as a marker. We have V = {O, 1} and 1= {O, 1, M}. We wish to recognize only a subset of strings in V*. We will have one final state, S6. The Turing machine successively replaces a 0 at the leftmost position of the string with an M and a 1 at the rightmost position of the string with an M, sweeping back and forth, terminating in a final state if and only if the string consists of a block of Os followed by a block ofthe same number of 1 s. Although this is easy to describe and is easily carried out by a Turing machine, the machine we need to use is somewhat complicated. We use the marker M to keep track of the leftmost and rightmost symbols we have already examined. The five-tuples we use are (so, 0, s\, M, R), (s\, 0, S\, 0, R), (s\, 1, S\, 1, R), (s\, M, S2, M, L), (s\, B, S2, B, L), (S2, 1, S3, M, L), (S3, l,s3, I,L), (S3,0,S4,0,L), (s3,M,S5,M,R), (S4,0,S4,0,L), (s4,M,so,M,R), and (S5, M, S6, M, R). For example, the string 000111 would successively become M00111, M0011M, MM011M, MMOIMM, MMMIMM, MMMMMM as the machine operates until it halts. Only the changes are shown, as most steps leave the string unaltered. We leave it to the reader (Exercise 13 at the end of this section) to explain the actions of this Turing machine and to explain why it recognizes the set {on 1 n I n � I}. .... It can be shown that a set can be recognized by a Turing machine if and only if it can be generated by a type 0 grammar, or in other words, if the set is generated by a phrase-st ructure grammar. The proof will not be presented here. Computing Functions with Turing Machines A Turing machine can be thought of as a computer that finds the values of a partial function. To see this, suppose that the Turing machine T, when given the string x as input, halts with the string y on its tape. We can then define T (x) = y. The domain of T is the set of strings for which T halts; T (x) is undefined if T does not halt when given x as input. Thinking of a Turing machine as a machine that computes the values of a function on strings is useful, but how can we use Turing machines to compute functions defined on integers, on pairs of integers, on triples of integers, and so on? To consider a Turing machine as a computer of functions from the set of k-tuples of non­ negative integers to the set of nonnegative integers (such functions are called number -theoretic functions), we need a way to represent k-tuples of integers on a tape. To do so, we use unary representations of integers. We represent the nonnegative integer n by a string of n + lIs so that, for instance, 0 is represented by the string 1 and 5 is represented by the string 111111. To represent the k-tuple (n\, n2, ... , nk), we use a string ofnl + 1 Is, followed by an asterisk, fol­ lowed by a string ofn2 + lIs, followed by an asterisk, and so on, ending with a string ofnk + 1 1 s. For exam ple, to represent the four-tuple (2, 0, 1, 3) we use the string 111 * 1 * 11 * 1111. 832 12/ Modeling Computation 12-48 We can now consider a Turing machine T as computing a sequence of number-theoretic functions T, T2, ... , Tk, .... The function Tk is defined by the action of T on k-tuples of integers represented by unary representations of integers separated by asterisks. EXAMPLE 4 Construct a Turing machine for adding two nonnegative integers. Solution: We need to build a Turing machine T that computes the function f(n), n2) = n) + n2. The pair (nt. n2) is represented by a string of n) + 1 Is followed by an asterisk followed by n2 + lIs. The machine T should take this as input and produce as output a tape with n) + n2 + 1 1 s. One way to do this is as follows. The machine starts at the leftmost 1 of the input string, and carries out steps to erase this 1, halting if n) = 0 so that there are no more 1 s before the asterisk, replaces the asterisk with the leftmost remaining 1, and then halts. We can use these five-tuples to do this: (so, 1, s), B, R), (s), *, S3, B, R), (s), 1, S2, B, R), (S2, 1, S2, 1, R), and (S2, *, S3, 1, R). · ... Unfortunately, constructing Turing machines to compute relatively simple functions can be extremely demanding. For example, one Turing machine for multiplying two nonnegative integers found in many books has 31 five-tuples and 11 states. If it is challenging to construct Turing machines to compute even relatively simple functions, what hope do we have of building Turing machines for more complicated functions? One way to simplify this problem is to use a multitape Turing machine that uses more than one tape simultaneously and to build up multitape Turing machines for the composition of functions. It can be shown that for any multitape Turing machine there is a one-tape Turing machine that can do the same thing. Different Types of Turing Machines There are many variations on the definition of a Turing machine. We can expand the capabilities ofa Turing machine in a wide variety of ways. For example, we can allow a Turing machine to move right, left, or not at all at each step. We can allow a Turing machine to operate on multiple tapes, using (2 + 3n )-tuples to describe the Turing machine when n tapes are used. We can allow the tape to be two-dimensional, where at each step we move up, down, right, or left, not just right or left as we do on a one-dimensional tape. We can allow multiple tape heads that read different cells simultaneous ly. Furthermore, we can allow a Turing machine to be nondeterministic, by allowing a (state, tape symbol) pair to possibly appear as the first elements in more than one five-tuple of the Turing machine. We can also reduce the capabilities of a Turing machine in different ways. For example, we can restrict the tape to be infinite in only one dimension or we can restrict the tape alphabet to have only two symbols. All these variations of Turing machines have been studied in detail. The crucial point is that no matter which of these variations we use, or even which combi­ nation of variations we use, we never increase or decrease the power of the machine. Anything that one of these variations can do can be done by the Turing machine defined in this section, and vice versa. The reason that these variations are useful is that sometimes they make doing some particular job much easier than if the Turing machine defined in Definition I were used. They never extend the capability of the machine. Sometimes it is useful to have a wide variety of Turing machines with which to work. For example, one way to show that for every nonde­ terministic Turing machine, there is a deterministic Turing machine that recognizes the same language is to use a deterministic Turing machine with three taps. (For details on variations of Turing machines and demonstrations of their equivalence, see [HoMoUlO l].) Besides introducing the notion of a Turing machine, Turing also showed that it is possible to construct a single Turing machine that can simulate the computations of every Turing machine when given an encoding of this target Turing machine and its input. Such a machine is called a universal Turing machine. (See a book on the theory of computation, such as [Si06], for more about universal Turing machines.) 12-49 unlls� 12.5 Turing Machines 833 The Church-Turing Thesis Turing machines are relatively simple. They can have only finitely many states and they can read and write only one symbol at a time on a one-dimensional tape. But it turns out that Turing machines are extremely powerful. We have seen that Turing machines can be built to add numbers and to multiply numbers. Although it may be difficult to actually construct a Turing machine to compute a particular function that can be computed with an algorithm, such a Turing machine can always be found. This was the original goal of Turing when he invented his machines. Furthermore, there is a tremendous amount of evidence for the Church-Turing thesis, which states that given any problem that can be solved with an effective algorithm, there is a Turing machine that can solve this problem. The reason this is called a thesis rather than a theorem is that the concept of solvability by an effective algorithm is informal and imprecise, as opposed to the notion of solvability by a Turing machine, which is formal and precise. Certainly, though, any problem that can be solved using a computer with a program written in any language, perhaps using an unlimited amount of memory, should be considered effectively solvable. (Note that Turing machines have unlimited memory, unlike computers in the real world, which have only a finite amount of memory.) Many different formal theories have been developed to capture the notion of effective computa bility. These include Turing's theory and Church's lambda-calculus, as well as theories proposed by Stephen Kleene and by E. L. Post. These theories seem quite different on the surface. The surprising thing is that they can be shown to be equivalent by demonstrating that they define exactly the same class of functions. With this evidence, it seems that Turing's original ideas, formulated before the invention of modern computers, describe the ultimate capabilities of these machine s. The interested reader should consult books on the theory of computation, such as [HoMoUlO I] and [Si96], for a discussion ofthese different theories and their equivalence. For the remainder of this section we will briefly explore some of the consequences of the Church-Turing thesis and we will describe the importance of Turing machines in the study of the complexity of algorithms. Our goal will be to introduce some important ideas from theoretical computer science to entice the interested student to further study. We will cover a lot of ground quickly without providing explicit details. Our discussion will also tie together some of the concepts discussed in previous parts of the book with the theory of computation. Computational Complexity , Computabil ity, and Decidability Throughout this book we have discussed the computational complexity of a wide variety of problems. We described the complexity of these problems in terms ofthe number of operations used by the most efficient algorithms that solve them. The basic operations used by algorithms differ considerably; we have measured the complexity of different algorithms in terms of bit operations, comparisons ofintegers, arithmetic operations, and so on. In Section 3.3, we defined various classes of problems in terms of their computational complex ity. However, these defini­ tions were not precise, because the types of operations used to measure their complexity vary so drastical ly. Turing machines provide a way to make the concept of computational complexity precise. By the Church-Turing thesis we know that if a problem can be solved using an effective algorithm, then there is a Turing machine that can solve this problem. When a Turing machine is used to solve a problem, the input to the problem is encoded as a string of symbols that is written on the tape of this Turing machine . How we encode input depends on the domain of this input. For example, as we have seen, we can encode a positive integer using a string of 1 s. We can also devise ways to express pairs of integers, negative integers, and so on. Similarly, for graph algorithms, we need a way to encode graphs as strings of symbols. This can be done in many ways and can be based on adjacency lists or adjacency matrices. (We omit the details of how this is done.) However, the way input is encoded does not matter as long as it is relatively efficient, as a Turing machine can always change one encoding into another encoding. We will 834 12/ Modeling Computation 12-50 DEFINITION 3 DEFINITION 4 now use this model to make precise some of the notions concerning computational complexity that were informally introduced in Section 3.3. The kind of problems that are most easily studied by using Turing machines are those problems that can be answered either by a "yes" or by a "no." A decision problem asks whether statements from a particular class of statements are true. Decision problems are also known as yes-or-no problems. Given a decision problem, we would like to know whether there is an algorithm that can determine whether statements from the class of statements it addresses are true. For example, consider the class of statements each of which asks whether a particular integer n is prime. This is a decision problem because the answer to the question "Is n prime?" is either yes or no. Given this decision problem, we can ask whether there is an algorithm that can decide whether each of the statements in the decision problem is true, that is, given an integer n, deciding whether n is prime. The answer is that there is such an algorithm. In particular, in Section 3.5 we discussed the algorithm that determines whether a positive integer n is prime by checking whether it is divisible by primes not exceeding its square root. (There are many other algorithms for determining whether a positive integer is prime.) The set of inputs for which the answer to the yes-no problem is "yes" is a subset of the set of possible inputs, that is, it is a subset of the set of strings of the input alphabet. In other words, solving a yes-no problem is the same as recognizing the language consisting of all bit strings that represent input values to the problem leading to the answer "yes." Consequently, solving a yes-no problem is the same as recognizing the language corresponding to the input values for which the answer to the problem is "yes." DECIDABILITY When there is an effective algorithm that decides whether instances of a decision problem are true, we say that this problem is solvable or decidable. For instance, the problem of determining whether a positive integer is prime is a solvable problem. However, if no effective algorithm exists for solving a problem, then we say the problem is unsolvable or un­ decidable. To show that a decision problem is solvable we need only construct an algorithm that can determine whether statements of the particular class are true. On the other hand, to show that a decision problem is unsolvable we need to prove that no such algorithm exists. (The fact that we tried to find such an algorithm but failed, does not prove the problem is unsolvabl e.) By studying only decision problems, it may seem that we are studying only a small set of problems of interest. However, most problems can be recast as decision problems. Recasting the types of problems we have studied in this book as decision problems can be quite complica ted, so we will not go into the details of this process here. The interested reader can consult references on the theory of computation, such as [W087], which, for example, explains how to recast the traveling salesman problem (described in Section 9.6) as a decision problem. (To recast the traveling salesman problem as a decision problem, we first consider the. decision problem that asks whether there is a Hamilton circuit of weight not exceeding k, where k is a positive integer. With some additional effort it is possible to use answers to this question for different values of k to find the smallest possible weight of a Hamilton circuit.) In Section 3.1 we introduced the halting problem and proved that it is an unsolvable problem. That discussion was somewhat informal because the notion of a procedure was not precisely defined. A precise definition of the halting problem can be made in terms of Turing machines. The halting problem is the decision problem that asks whether a Turing machine T eventually halts when given an input string x. With this definition of the halting problem, we have Theorem 1. /2-5/ THEOREM 1 unkS� 12.5 Turing Machines 835 The halting problem is an unsolvable decision problem. That is, no Turing machine exists that, when given an encoding of a Turing machine T and its input string x as input, can determine whether T eventually halts when started with x written on its tape. The proof of Theorem 1 given in Section 3.1 for the informal definition of the halting problem still applies here. Other examples of unsolvable problems include: (a) the problem of determining whether two context-free grammars generate the same set of strings; (b) the problem of determining whether a given set of tiles can be used with repetition allowed to cover the entire plane without overlap; and (c) Hilbert:SO Tenth Problem, which asks whether there are integer solutions to a given polynomial equation with integer coefficients. (This question occurs tenth on the famous list of 23 problems Hilbert posed in 1900. Hilbert envisioned that the work done to solve these problems would help further the progress of mathematics in the twentieth century. The unsolvability of Hilbert's Tenth Problem was established in 1970 by Yuri Matiyasevich. ) COMPUTABILITY A function that can be computed by a Turing machine is called com­ putable and a function that cannot be computed by a Turing machine is called uncomputable. It is fairly straightforward, using a countability argument, to show that there are number-theoretic functions that are not computable (see Exercise 47 in Section 2.4). However, it is not so easy to actually produce such a function. The busy beaver function defined in the preamble to Ex­ ercise 31 at the end of this section is an example of an uncomputable function. One way to show that the busy beaver function is not computable is to show that it grows faster than any computable function. (See Exercise 32.) Note that every decision problem can be reformulated as the problem of computing a function, namely, the function that has the value I when the answer to the problem is "yes" and that has the value 0 when the answer to the problem is "no." A decision problem is solvable if and only if the corresponding function constructed in this way is computable. THE CLASSES P AND NP In Section 3.3 we informally defined the classes of problems called P and NP. We are now able to define these concepts precisely using the notions of deterministic and nondeterministic Turing machines. We first elaborate on the difference between a deterministic Turing machine and a nonde­ terministic Turing machine. The Turing machines we have studied in this section have all been deterministic. In a deterministic Turing machine T = (S, I, j, so), transition rules are defined by the partial function j from S x I to S x I x {R, L}. Consequently, when transition rules of the machine are represented as five-tuples of the form (s, x, s', x', d), where s is the current state, x is the current tape symbol, s' is the next state, x' is the symbol that replaces x on the tape, and d is the direction the machine moves on the tape, no two transition rules begin with the same pair (s, x). In a nondeterministic Turing machine, allowed steps are defined using a relation consisting of five-tuples rather than using a partial function. The restriction that no two transition rules begin with the same pair (s, x) is eliminated; that is, there may be more than one transition rule beginning with each (state, tape symbol) pair. Consequently, in a nondeterministic Turing machine, there is a choice of transitions for some pairs of the current state and the tape symbol being read. At each step of the operation of a nondeterministic Turing machine, the machine picks one of the different choices of the transition rules that begin with the current state and tape symbol pair. This choice can be considered to be a "guess" of which step to use. Just 836 12/ Modeling Computation 12-52 DEFINITION 5 lin!ts� as for deterministic Turing machine s, a nondeter ministic Turing machine halts when there is no transition rule in its definition that begins with the current state and tape symbol. Given a nondeterministic Turing machine T, we say that a string x is recognized by T if and only if there exists some sequence of transitions of T that ends in a final state when the machine starts in the initial position with x written on the tape. The nondeter ministic Turing machine T recognizes the set A if x is recognized by T if and only if x EA. The nondeterministic Turing machine T is said to solve a decision problem if it recognizes the set consisting of all input values for which the answer to the decision problem is yes. A decision problem is in P, the class of polynomial-time problems, if it can be solved by a deterministic Turing machine in polynomial time in terms of the size of its input. That is, a decision problem is in P if there is a deterministic Turing machine T that solves the decision problem and a polynomial p(n) such that for all integers n, T halts in a final state after no more than p(n) transitions whenever the input to T is a string oflength n. A decision problem is in NP, the class o/nondeterministic polynomial-time problems, if it can be solved by a nondeterministic Turing machine in polynomial time in terms of the size of its input. That is, a decision problem is in NP if there is a nondeterministic Turing machine T that solves the problem and a polynomial p(n) such that for all integers n, T halts for every choice of transitions after no more than p(n) transitions whenever the input to T is a string of length n. Problems in P are called tractable, whereas problems not in P are called intractable. For a problem to be in P, a deterministic Turing machine must exist that can decide in polynomial time whether a particular statement of the class addressed by the decision problem is true. For example, determining whether an item is in a list of n elements is a tractable problem. (We will not provide details on how this fact can be shown; the basic ideas used in the analyses of algorithms earlier in the text can be adapted when Turing machines are employed.) Surprisingl y, the problem of determining whether a positive integer n is prime is also tractable (a fact that was proved only in 2002 by Manindra Agrawal, Neeraj Kayal, and Nitin Saxena [AgKaSa02 ]). For a problem to be in NP, it is necessary only that there be a nondeterministic Turing machine that, when given a true statement from the set of statements addressed by the problem, can verify its truth in polynomial time by making the correct guess at each step from the set of allowable steps corresponding to the current state and tape sym bol. The problem of determining whether a given graph has a Hamilton circuit is an NP problem, because a nondeterministic Turing machine can easily verify that a simple circuit in a graph passes through each vertex exactly once. It can do this by making a series of correct guesses corresponding to successively adding edges to form the circuit. Because every deterministic Turing machine can also be considered to be a nondeterministic Turing machine where each (state, tape symbol) pair occurs in exactly one transition rule defining the machine, every problem in P is also in NP. In symbols, P � NP. One of the most perplexing open questions in theoretical computer science is whether every problem in NP is also in P, that is, whether P = NP. This problem is one of the seven problems known as the Millennium Problems, which are meant to playa similar role for twenty-first century ALONZO CHURCH (\903-\995) Alonzo Church was born in Washington, D.C. He studied at Gottingen under Hilbert and later in Amsterdam. He was a member ofthe faculty at Princeton University from 1927 until 1967 when he moved to UCLA. Church was one of the founding members of the Association for Symbolic Logic. He made many substantial contributions to the theory of computabilit y, including his solution to the decision problem, his invention of the lambda-calculus, and, of course, his statement of what is now known as the Church-Turing thesis. Among Church's students were Stephen Kleene and Alan Turing. He published articles past his 90th birthday. 12-53 12.5 Turing Machines 837 mathema tics as the Hilbert problems did for twentieth century mathema tics. (See [De02] for more about the Millennium Problems.) As mentioned in Section 3.3, there is an important class of problems, the class of NP-complete problems, such that a problem is in this class if it is in the class NP and if it can be shown that if it is also in the class P, then every problem in the class NP must also be in the class P. That is, a problem i� NP-complete if the existence of a polynomial-time algorithm for solving it implies the existence of a polynomial-time algorithm for every problem in NP. In this book we have discussed several different NP-complete problems, such as determining whether a simple graph has a Hamilton circuit and determining whether a proposition in n-variables is a tautology. Exercis es 1. Let T be the Turing machine defined by the five­ tuples: (so, 0, s\, 1, R), (so, 1, s\, 0, R), (so, B, s\, 0, R), (s\, 0, S2, 1, L), (s\, 1, s\, 0, R), and (s\, B, S2, 0, L). For each of these initial tapes, determine the final tape when T halts, assuming that T begins in initial position. a) ... I BIB I ° I ° I 1 I 1 I BIB I b) ... I BIB' 11 I ° I 1 I BIB I B I c) ... I BIB I 1 I 1 I B I ° I 1 I B I d) ... I BIB I BIB I BIB I BIB I ... 2. Let T be the Turing machine defined by the five­ tuples: (so, 0, SJ, 0, R), (so, 1, s\, 0, L), (so, B, s\, 1, R), (s\,0,s2,I,R), (s\,I,s\,I,R), (s\, B,S2,0, R), and (S2, B, S3, 0, R). For each of these initial tapes, determine the final tape when T halts, assuming that T begins in initial position. a) ... I BIB I 0 I 1 I 0 I 1 I BIB I b) ... I BIB I 1 I 1 I 1 I BIB I B I c) I BIB I ° I 0 I B I 0 I 0 I B I IBIBIBIBIBIBI BIBI··· d) 3. What does the Turing machine described by the five­ tuples (so, 0, so, 0, R), (so, 1, s\, 0, R), (so, B, S2, B, R), (s\, 0, s\, 0, R), (s\, 1, so, 1, R), and (s\, B, S2, B, R) do when given a) 11 as input? b) an arbitrary bit string as input? 4. What does the Turing machine described by the five­ tuples (so, 0, so, 1, R), (so, l,so, 1, R), (so, B,s\, B, L), (S\, 1, S2, 1, R), do when given a) 101 as input? b) an arbitrary bit string as input? 5. What does the Turing machine described by the five­ tuples (so, 1, S\, 0, R), (s\, 1, S\, 1, R), (s\, 0, S2, 0, R), (S2, 0, S3, 1, L), (S2, 1, S2, 1, R), (S3, 1, S3, 1, L), (S3,0,S4,0,L), (S4, l, s4, I,L), and (S4, 0, so, I,R) do when given a) 11 as input? b) a bit string consisting entirely of 1 s as input? 6. Construct a Turing machine with tape symbols 0, 1, and B that, when given a bit string as input, adds a 1 to the end of the bit string and does not change any of the other symbols on the tape. 7. Construct a Turing machine with tape symbols 0, 1, and B that, when given a bit string as input, replaces the first o with a 1 and does not change any of the other symbols on the tape. 8. Construct a Turing machine with tape symbols 0, 1, and B that, given a bit string as input, replaces all Os on the tape with 1 s and does not change any of the 1 s on the tape. 9. Construct a Turing machine with tape symbols 0, 1, and B that, given a bit string as input, replaces all but the left­ most 1 on the tape with Os and does not change any of the other symbols on the tape. 10. Construct a Turing machine with tape symbols 0, 1, and B that, given a bit string as input, replaces the first two consecutive 1 s on the tape with Os and does not change any of the other symbols on the tape. 11. Construct a Turing machine that recognizes the set of all bit strings that end with a 0. 12. Construct a Turing machine that recognizes the set of all bit strings that contain at least two Is. 13. Construct a Turing machine that recognizes the set of all bit strings that contain an even number of 1 s. 14. Show at each step the contents of the tape of the Tur­ ing machine in Example 3 starting with each of these strings. a) 0011 b) 00011 c) 101100 d) 000111 15. Explain why the Turing machine in Example 3 recognizes a bit string if and only if this string is of the form on 1 n for some positive integer n. 838 12/ Modeling Computation '16. Construct a Turing machine that recognizes the set {OZn In In 2: OJ. 17. Construct a Turing machine that recognizes the set {onln2n In 2: OJ. 18. Construct a Turing machine that computes the function f(n) = n + 2 for all nonnegative integers n. 19. Construct a Turing machine that computes the function f(n) = n - 3 if n 2: 3 and f(n) = 0 for n = 0, 1,2 for all nonnegative integers n. 20. Construct a Turing machine that computes the function f(n) = n mod 3. 21. Construct a Turing machine that computes the function f(n) = 3 ifn 2: 5 and f(n) = 0 ifn = 0, 1,2,3, or 4. 22. Construct a Turing machine that computes the function f(n) = 2n for all nonnegative integers n. 23. Construct a Turing machine that computes the function f(n) = 3n for all nonnegative integers n. 24. Construct a Turing machine that computes the function f(nl, n2) = n2 + 2 for all pairs of nonnegative integers nl andn2. 25. Construct a Turing machine that computes the function f(n I, n2) = min(n I, n2) for all nonnegative integers n I andn2. 26. Construct a Turing machine that computes the function f(n\, n2) = nl + n2 + I for all nonnegative integers nl andn2. Suppose that TI and T2 are Turing machines with disjoint sets of states SI and S2 and with transition functions fl and 12, respectively. We can define the Turing machine TI T2, the com­ posite of TI and T2, as follows. The set of states of TI T2 is SI U S2. TI T2 begins in the start state of SI. It first executes the transitions of TI using fl up to, but not including, the step at which TI would halt. Then, for all moves for which TI halts, it executes the same transitions of TI except that it moves to the start state of T2. From this point on, the moves of TI T2 are the same as the moves of T2• Z7. By finding the composite of the Turing machines you con­ structed in Exercises 18 and 22, construct a Turing ma­ chine that computes the function f(n) = 2n + 2. Key Terms and Results fERMS lIphabet (or vocabulary): a set that contains elements used to form strings language: a subset of the set of all strings over an alphabet [Jhrase-structure grammar (V, T, S, P): a description of a language containing an alphabet V, a set of termi­ nal symbols T, a start symbol S, and a set of produc­ tions P :he production W -+ WI: W can be replaced by WI whenever it occurs in a string in the language 12-54 28. By finding the composite of the Turing machines you constructed in Exercises 18 and 23, construct a Turing machine that computes the function f(n) = 3(n + 2) = 3n +6. 29. Which of the following problems is a decision problem? a) What is the smallest prime greater than n? b) Is a graph G bipartite? c) Given a set of strings, is there a finite-state automaton that recognizes this set of strings? d) Given a checkerboard and a particular type of poly­ omino (see Section 1.7), can this checkerboard be tiled using polyominoes of this type? 30. Which of the following problems is a decision problem? a) Is the sequence ai, a2, ... , an of positive integers in increasing order? b) Can the vertices of a simple graph G be colored using three colors so that no two adjacent vertices are the same color? c) What is the vertex of highest degree in a graph G? d) Given two finite-state machines, do these machines recognize the same language? � Let B(n) be the maximum number of 1 s that a Turing machine with n states with the alphabet {I, B} may print on a tape that is initially blank. The problem of determining B(n) for partic­ ular values of n is known as the busy beaver problem. This problem was first studied by Tibor Rado in 1962. Currently it is known that B(2) = 4, B(3) = 6, and B(4) = 13, but B(n) is not known for n 2: 5. B(n) grows rapidly; it is known that B(5) 2: 4098 and B(6) 2: 1.29 x 10865• *31. Show that B(2) is at least 4 by finding a Turing machine with two states and alphabet {I, B} that halts with four consecutive I s on the tape. **32. Show that the function B(n) cannot be computed by any Turing machine. [Hint: Assume that there is a Turing ma­ chine that computes B(n) in binary. Build a Turing ma­ chine T that, starting with a blank tape, writes n down in binary, computes B(n) in binary, and converts B(n) from binary to unary. Show that for sufficiently large n, the number of states of T is less than B(n), leading to a contradiction.] WI => Wz (wz is directly derivable from WI): W2 can be ob­ tained from WI using a production to replace a string in WI with another string WI '* Wz (wz is derivable from WI): W2 can be obtained from WI using a sequence of productions to replace strings by other strings type 0 grammar: any phrase-structure grammar type 1 grammar: a phrase-st ructure grammar in which every production is of the form WI � W2, where WI = IAr and W2 = Iwr, where A E N, I, r, WE (N U T) and W = A, or 12-55 WI = S and W2 = A as long as S is not on the right-hand side of another production type 2, or context-free, grammar: a phrase-structure gram­ mar in which every production is of the form A --+ W J. where A is a nonterminal symbol type 3, or regular, grammar: a phrase-structure grammar where every production is of the form A --+ a B, A --+ a, or S --+ A, where A and B are nonterminal symbols, S is the start symbol, and a is a terminal symbol derivation (or parse) tree: an ordered rooted tree where the root represents the starting symbol of a type 2 grammar, internal vertices represent nonterminal s, leaves represent terminals, and the children of a vertex are the symbols on the right side of a production, in order from left to right, where the symbol represented by the parent is on the left­ hand side Backus-Naur form: a description ofa context-free grammar in which all productions having the same nonterminal as their left-hand side are combined with the different right­ hand sides of these productions, each separated by a bar, with nonterminal symbols enclosed in angular brackets and the symbol --+ replaced by ::= finite-state machine (S, I, 0, f, g, so)( or a Mealy machine): a six-tuple containing a set S of states, an input alphabet I, an output alphabet 0, a transition function f that assigns a next state to every pair of a state and an input, an output function g that assigns an output to every pair of a state and an input, and a starting state So AB (concatenation of A and B): the set of all strings formed by concatenating a string in A and a string in B in that order A* (KIeene closure of A): the set of all strings made up by concatenating arbitrarily many strings from A deterministic finite-state automaton (S, I, t, so, F): a five­ tuple containing a set S of states, an input alphabet I, a transition function t that assigns a next state to every pair of a state and an input, a starting state So, and a set of final states F nondeterministic finite-state automaton (S, I, f, so, F): a five-tuple containing a set S of states, an input alphabet I, a transition function f that assigns a set of possible next states to every pair of a state and an input, a starting state So, and a set of final states F language recognized by an automaton: the set of input strings that take the start state to a final state of the automaton Review Questions 1. a) Define a phrase-str ucture grammar. b) What does it mean for a string to be derivable from a string w by a phrase-structure grammar O? 2. a) What is the language generated by a phrase-st ructure grammar G? b) What is the language generated by the grammar G with vocabulary {S,O, I},setofterminalsT = to, I},start­ ing symbol S, and productions S --+ OOOS, S --+ I? Review Questions 839 regular expression: an expression defined recursively by spec­ ifying that 0, A, and x, for all x in the input alphabet, are regular expressions, and that (AB), (A U B), and A* are regular expressions when A and B are regular expressions regular set: a set defined by a regular expression (see page 820) Turing machine T = (S, I, t, so): a four-tuple consisting of a finite set S of states, an alphabet I containing the blank sym­ bol B, a partial function f from S x I to S x I x {R, L}, and a starting state So nondeterministic Turing machine: a Turing machine that may have more than one transition rule corresponding to each (state, tape symbol) pair decision problem: a problem that asks whether statements from a particular class of statements are true solvable problem: a problem with the property that there is an effective algorithm that can solve all instances of the problem unsolvable problem: a problem with the property that no ef­ fective algorithm exists that can solve all instances of the problem computable function: a function whose values can be com­ puted using a Turing machine uncomputable function: a function whose values cannot be computed using a Turing machine P, the class of polynomial-time problems: the class ofprob­ lems that can be solved by a deterministic Turing machine in polynomial time in terms of the size of the input NP, the class of nondeterministic polynomial-time prob­ lems: the class of problems that can be solved by a nonde­ terministic Turing machine in polynomial time in terms of the size of the input NP-complete: a subset of the class of NP problems with the property that if anyone of them is in the class P, then all problems in NP are in the class P RESULTS For every nondeterministic finite-state automaton there is a de­ terministic finite-state automaton that recognizes the same set. K1eene's Theorem: A set is regular if and only if there is a finite-state automaton that recognizes it. A set is regular if and only if it is generated by a regular grammar. The halting problem is unsolvable. c) Give a phrase-structure grammar that generates the set {Oln In = 0,1,2, ... }. 3. a) Define a type 1 grammar. b) Give an example of a grammar that is not a type 1 grammar. c) Define a type 2 grammar. d) Give an example of a grammar that is not a type 2 grammar but is a type 1 grammar. 840 12/ Modeling Computation e) Define a type 3 grammar. 1) Give an example of a grammar that is not a type 3 grammar but is a type 2 grammar. 4. a) Define a regular grammar. b) Define a regular language. c) Show that the set {om 1 n 1m, n = 0, 1,2, ... } is a reg­ ular language. 5. a) What is Backus-Naur form? b) Give an example of the Backus-Naur form of the grammar for a subset of English of your choice. 6. a) What is a finite-state machine? b) Show how a vending machine that accepts only quar­ ters and dispenses a soft drink after 75 cents has been deposited can be modeled using a finite-state machine. 7. Find the set of strings recognized by the deterministic finite-state automaton shown here. 0,1 8. Construct a deterministic finite-state automaton that rec­ ognizes the set of bit strings that start with 1 and end with 1. Supplementary Exercises jc 1. Find a phrase-structure grammar that generates each of these languages. a) the set of bit strings of the form 02n 13n, where n is a nonnegative integer b) the set of bit strings with twice as many Os as Is c) the set of bit strings of the form w2, where w is a bit string 1<2. Find a phrase-structure grammar that generates the set {OZ" I n � OJ. For Exercises 3 and 4, letG = (V, T, S, P)be the context-free grammar with V = {(, ), S, A, B}, T = {(, )}, starting symbol S,andproductionsS -+ A,A-+ AB,A -+ B,B -+ (A),and B -+ 0, S -+ A. 3. Construct the derivation trees of these strings. a) (0) b) 0(0) c) «O())) 1<4. Show that L(G) is the set of all balanced strings of par en­ theses, defined in the preamble to Supplementary Exercise 55 in Chapter 4. A context-free grammar is ambiguous if there is a word in L( G) with two derivations that produce different derivation trees, considered as ordered, rooted trees. 5. Show that the grammar G = (V, T, S, P) with V = {O,S}, T = {O}, starting state S,andproductio nsS -+ OS, S -+ SO, and S -+ 0 is ambiguous by constructing two different derivation trees for 03. 9. a) What is the Kleene closure of a set of strings? b) Find the Kleene closure of the set {II, OJ. 10. a) Define a finite-state automaton. 12-56 b) What does it mean for a string to be recognized by a finite-state automaton? 11. a) Define a nondeterministic finite-state automaton. b) Show that given a nondeterministic finite-state au­ tomaton, there is a deterministic finite-state automaton that recognizes the same language. 12. a) Define the set of regular expressions over a set /. b) Explain how regular expressions are used to represent regular sets. 13. State Kleene's Theorem. 14. Show that a set is generated by a regular grammar if and only if it is a regular set. 15. Give an example of a set not recognized by a finite-state automaton. Show that no finite-state automaton recog­ nizes it. 16. Define a Turing machine. 17. Describe how Turing machines are used to recognize sets. 18. Describe how Turing machines are used to compute number-theoretic functions. 19. What is an unsolvable decision problem? Give an example of such a problem. 6. Show that the grammar G = (V, T, S, P) with V = {O,S}, T = {OJ, starting state S, and productions S -+ OS and S -+ 0 is unambiguous. 7. Suppose that A and B are finite subsets of V*, where V is an alphabet. Is it necessarily true that lAB I = IBAI? 8. Prove or disprove each of these statements for subsets A, B, and C of V*, where V is an alphabet. a) A(B U C) = AB U AC b) A(B n C) = AB n AC c) (AB)C = A(BC) d) (A U B)* = A* U B* 9. Suppose that A and B are subsets of V*, where V is an alphabet. Does it follow that A S; B if A * S; B*? 10. What set of strings with symbols in the set {O, 1, 2} is represented by the regular expression (2*)(0 U (12*»*? The star height heEl of a regular expression over the set / is defined recursively by h(0) = 0; hex) = 0 ifx E /; h«Ej U Ez» = h«EjEz» = max(h(Ej), h(E2» if E j and Ez are regular expressions; h(E*) = heEl + 1 ifE is a regular expression. 11. Find the star height of each ofthese regular expressions. a) 0*1 b) 0*1* 12-57 c) (0*01)* d) «0*1)*)* e) (010*)(1*01*)*«01)*(10)*)* t) ««(0*)1)*0)*)1)* *12. For each of these regular expressions find a regular ex­ pression that represents the same language with minimum star height. . a) (0*1*)* b) (0(01*0)*)* c) (0* U (01)* U 1 *)* 13. Construct a finite-state machine with output that produces an output of 1 if the bit string read so far as input contains four or more I s. Then construct a deterministic finite-state automaton that recognizes this set. 14. Construct a finite-state machine with output that pro­ duces an output of 1 if the bit string read so far as input contains four or more consecutive 1 s. Then construct a deterministic finite-state automaton that recognizes this set. 15. Construct a finite-state machine with output that pro­ duces an output of I if the bit string read so far as input ends with four or more consecutive 1 s. Then construct a deterministic finite-state automaton that recognizes this set. 16. A state s' in a finite-state machine is said to be reach­ able from state s if there is an input string x such that f(s. x) = s'. A state s is called transient if there is no nonempty input string x with f(s. x) = s. A state s is called a sink if f(s. x) = s for all input strings x. An­ swer these questions about the finite-state machine with the state diagram illustrated here. a) Which states are reachable from so? b) Which states are reachable from S2 ? c) Which states are transient? d) Which states are sinks? *17. Suppose that S, I, and 0 are finite sets such that lSI = n, III = k, and 101 = m. a) How many different finite-state machines (Mealy ma­ chines) M = (S. I. O. f, g. so) can be constructed, where the starting state So can be arbitrarily chosen? b) How many different Moore machines M = (S, I. O. f, g. so) can be constructed, where the start­ ing state So can be arbitrarily chosen? *18. Suppose that S and I are finite sets such that lSI = n Supplementary Exercises 841 and I I I = k. How many different finite-state automata M = (S, I. f. So. F) are there where the starting state So and the subset F of S consisting of final states can be chosen arbitrarily a) if the automata are deterministic? b) if the automata may be nondeterministic? (Note: This includes deterministic automata.) 19. Construct a deterministic finite-state automaton that is equivalent to the nondeterministic automaton with the state diagram shown here. 20. What is the language recognized by the automaton in Ex­ ercise 19? 21. Construct finite-state automata that recognize these sets. a) 0*(10)* b) (01 U 111)*10*(0 U 1) c) (001 U (11)*)* *22. Find regular expressions that represent the set of all strings of Os and Is a) made up of blocks of even numbers of I s interspersed with odd numbers of Os. b) with at least two consecutive Os or three consecutive Is. c) with no three consecutive Os or two consecutive Is. *23. Show that if A is a regular set, then so is A. *24. Show that if A and B are regular sets, then so is A n B. *25. Find finite-state automata that recognize these sets of strings of Os and Is. a) the set of all strings that start with no more than three consecutive Os and contain at least two consecu­ tive Is. b) the set of all strings with an even number of symbols that do not contain the pattern 101. c) the set of all strings with at least three blocks of two or more 1 s and at least two Os. *26. Show that {ij2n In EN} is not regular. You may use the pumping lemma given in Exercise 22 of Section 12.4. *27. Show that {lP I p is prime} is not regular. You may use the pumping lemma given in Exercise 22 of Section 12.4. *28. There is a result for context-free languages analogous to the pumping lemma for regular sets. Suppose that L( G) is the language recognized by a context-free language G. This result states that there is a constant N such that if z 842 12 / Modeling Computation is a word in L(G) with l(w) � N, then z can be written as uvwxy, where l(vwx)::: N, l(vx) � 1, and uviwxiy belongs to L(G) for i = 0, 1,2,3, .... Use this result to show that there is no context-free grainmar G with L(G) = Wln2n In = 0,1,2, ... }. Computer Projects Write programs with these input and output. 1. Given the productions in a phrase-st ructure grammar, de­ termine which type of grammar this is in the Chomsky classification scheme. 2. Given the productions of a phrase-structure grammar, find all strings that are generated using twenty or fewer appli­ cations of its production rules. 3. Given the Backus-Naur form of type 2 grammar, find all strings that are generated using twenty of fewer applica­ tions of the rules defining it. 1<4. Given the productions of a context-free grammar and a string, produce a derivation tree for this string if it is in the language generated by this grammar. 5. Given the state table of a Moore machine and an in­ put string, produce the output string generated by the machine. 6. Given the state table of a Mealy machine and an in­ put string, produce the output string generated by the machine. 7. Given the state table of a deterministic finite-state automa- Computations and Explorations 12-58 *29. Construct a Turing machine that computes the function f(nl, n2) = max(nl' n2). *30. Construct a Turing machine that computes the func­ tion f(nl, n2) = n2 - nl if n2 � nl and !(nJ, n2) = 0 ifn2 < n\. ton and a string, decide whether this string is recognized by the automaton. S. Given the state table of a nondeterministic finite-state au­ tomaton and a string, decide whether this string is recog­ nized by the automaton. *9. Given the state table of a nondeterministic finite-state au­ tomaton, construct the state table of a deterministic finite­ state automaton that recognizes the same language. **10. Given a regular expression, construct a nondeterminis­ tic finite-state automaton that recognizes the set that this expression represents. 11. Given a regular grammar, construct a finite-state au­ tomaton that recognizes the language generated by this grammar. 12. Given a finite-state automaton, construct a regular gram­ mar that generates the language recognized by this automaton. *13. Given a Turing machine, find the output string produced by a given input string. Use a computational program or programs you have written to do these exercises. 1. Solve the busy beaver problem for two states by testing all possible Turing machines with two states and alphabet {l, B}. Z. Solve the busy beaver problem for three states by testing all possible Turing machines with three states and alphabet {I, B}. 3. Find a busy beaver machine with four states by testing Writing Projects all possible Turing machines with four states and alphabet {l, B}. **4. Make as much progress as you can toward finding a busy beaver machine with five states. **5. Make as much progress as you can toward finding a busy beaver machine with six states. Respond to these questions with essays using outside sources. 1. Describe how the growth of certain types of plants can be modeled using a Lidenmeyer system. Such a system uses a grammar with productions modeling the different ways plants can grow. 2. Describe the Backus-Naur form (and extended Backus­ Naur form) rules used to specify the syntax of a pro­ gramming language, such as Java, LISp, or ADA, or the database language SQL. 12-59 3. Explain how finite-state machines are used by spell­ checkers. 4. Explain how finite-state machines are used in the study of network protocols. 5. Explain how finite-state machines are used in speech recognition programs. 6. Explain the concept of minimizing finite-state automata. Give an algorithm that carries out this minimization. 7. Give the definition of cellular automata. Explain their ap­ plications. Use the Game of Life as an example. 8. Define a pushdown automaton. Explain how pushdown automata are used to recognize sets. Which sets are rec­ ognized by pushdown automata? Provide an outline of a proof justifying your answer. 9. Define a linear-bounded automaton. Explain how linear­ bounded automata are used to recognize sets. Which sets are recognized by linear-bounded automata? Provide an outline of a proof justifying your answer. Writing Projects 843 10. Look up Turing's original definition of what we now call a Turing machine. What was his motivation for defining these machines? 11. Describe the concept of the universal Turing machine. Explain how such a machine can be built. 12. Explain the kinds of applications in which nondetermin­ istic Turing machines are used instead of deterministic Turing machines. 13. Show that a Turing machine can simulate any action of a nondeterministic Turing machine. 14. Show that a set is recognized by a Turing machine if and only if it is generated by a phrase-st ructure grammar. 15. Describe the basic concepts of the lambda-calculus and explain how it is used to study computability of functions. 16. Show that a Turing machine as defined in this chapter can do anything a Turing machine with n tapes can do. 17. Show that a Turing machine with a tape infinite in one direction can do anything a Turing machine with a tape infinite in both directions can do. APPENDIX A-I A I-I Axioms for the Real Numbers and the Positive Integers ]I n this book we have assumed an explicit set of axioms for the set of real numbers and for the set of positive integers. In this appendix we will list these axioms and we will illustrate how basic facts, also used without proof in the text, can be derived using them. Axioms for Real Numbers The standard axioms for real numbers include both the field (or algebraic) axioms, used to specify rules for basic arithmetic operations, and the order axioms, used to specify properties of the ordering of real numbers. THE FIELD AXIOMS We begin with the field axioms. As usual, we denote the sum and product of two real numbers x and y by x + y and x . y, respectively. (Note that the product of x and y is often denoted by xy without the use of the dot to indicate multiplication. We will not use this abridged notation in this appendix, but will within the text.) Also, by convention, we perform multiplications before additions unless parentheses are used. Although these statements are axioms, they are commonly called laws or rules. The first two of these axioms tell us that when we add or multiply two real numbers, the result is again a real number; these are the closure laws. Closure Law for Addition For all real numbers x and y, x + y is a real number. Closure Law for Multiplication For all real numbers x and y, x . y is a real number. The next two axioms tell us that when we add or multiply three real numbers, we get the same result regardless of the order of operations; these are the associative laws. Associative Law for Addition For all real numbers x, y, and z, (x + y) + z = x + (y + z). Associative Law for Multiplication For all real numbers x, y, and z, (x . y) . z = x . (y . z). Two additional algebraic axioms tell us that the order in which we add or multiply two numbers does not matter; these are the commutative laws. Commutative Law for Addition For all real numbers x and y, x + y = y + x. Commutative Law for Multiplication For all real numbers x and y, x . y = y . x. The next two axioms tell us that 0 and I are additive �d multiplicative identities for the set of real numbers. That is, when we add 0 to a real number or multiply a real number by I we do not change this real number. These laws are called identity laws. Additive Identity Law For every real number x, x + 0 = 0 + x = x. Multiplic ative Identity Law For every real number x, x . I = I . x = x. Although it seems obvious, we also need the following axiom. Identity Elements Axiom The additive identity 0 and the multiplica tive identity I are distinct, that is 0 "# 1. Two additional axioms tell us that for every real number, there is a real number that can be added to this number to produce 0, and for every nonzero real number, there is a real number by which it can be multiplied to produce 1. These are the inverse laws. A-I A-2 Appendix I / Axioms for the Real Numbers and the Positive Integers AI-2 Inverse Law for Addition For every real number x, there exists a real number -x (called the additive inverse of x) such that x + (-x) = (-x) + x = o. Inverse Law for Multiplication For every nonzero real number x, there exists a real number 1/ x (called the multiplicative inverse of x) such that x . (1/ x) = (1/ x) . x = 1. The final algebraic axioms for real numbers are the distributive laws, which tell us that multiplication distributes over addition; that is, that we obtain the same result when we first add a pair of real numbers and then multiply by a third real number or when we multiply each of these two real numbers by the third real number and then add the two products. Distributive Laws For all real numbers x, y, and z, x . (y + z) = x . y + x . z and (x + y). z = X· Z + y. z. ORDER AXIOM S Next, we will state the order axioms for the real numbers, which specify properties of the "greater than" relation, denoted by >, on the set of real numbers. We write x > y (and y < x) when x is greater than y, and we write x ::: y (and y ::: x) when x > y or x = y. The first of these axioms tells us that given two real numbers, exactly one of three possibilities occurs: the two numbers are equal, the first is greater than the second, or the second is greater than the first. This rule is called the trichotomy law. Trichotomy Law For all real numbers x and y, exactly one of x = y, x > y, or y > x is true. Next, we have an axiom, called the transitivity law, that tells us that if one number is greater than a second number and this second number is greater than a third, then the first number is greater than the third. Transitivity Law For all real numbers x, y, and z, if x > y and y > z, then x > z. We also have two compatibility laws, which tell us that when we add a number to both sides in a greater than relationship, the greater than relationship is preserved and when we multiply both sides of a greater than relationship by a positive real number (that is, a real number x with x > 0), the greater than relationship is preserved. Additive Compatibility Law For all real numbers x, y, and z, if x > y, then x + z > y+z. Multiplic ative Compatibility Law For all real numbers x, y, and z, if x > y and z > 0, then x . z > y . z. We leave it to the reader (see Exercise 15) to prove that for all real numbers x, y, and z, if x > y and z < 0, then x . z < y . z. That is, multiplication of an inequality by a negative real number reverses the direction of an inequality. The final axiom for the set of real numbers is the completeness property. Before we state this axiom, we need some definitions. First, given a nonempty set A of real numbers, we say that the real number b is an upper bound of A if for every real number a in A, b ::: a. A real number s is a least upper bound of A if s is an upper bound of A and whenever t is an upper bound of A then we have s ::: t. Completeness Property Every nonempty set of real numbers that is bounded above has a least upper bound. Using Axioms to Prove Basic Facts The axioms we have listed can be used to prove many properties that are often used without explicit mention. We give several examples of results we can prove using axioms and leave the AI-3 THEOREM l THEOREM 2 THEOREM 3 THEOREM 4 THEOREM S Appendix 1 / Axioms for the Real Numbers and the Positive Integers A-3 proof of a variety of other properties as exercises. Although the results we will prove seem quite obvious, proving them using only the axioms we have stated can be challenging. The additive identity element 0 of the real numbers is unique. Proof: To show that the additive identity element 0 of the real numbers is unique, suppose that 0' is also an additive identity for the real numbers. This means that 0' + x = x + 0' = x whenever x is a real number. By the additive identity law, it follows that 0 + 0' = 0'. Because 0' is an additive identity, we know that 0 + 0' = O. It follows that 0 = 0', because both equal o + 0'. This shows that 0 is the unique additive identity for the real numbers <l The additive inverse of a real number x is unique. Proof: Let x be a real number. Suppose that y and z are both additive inverses of x. Then, y=O+y = (z +x)+ y = z + (x + y) =z+O =z by the additive identity law because z is an additive inverse of x by the associative law for addition because y is an additive inverse of x by the additive identity law It follows that y = z. <l Theorems I and 2 tell us that the additive identity and additive inverses are unique. Theorems 3 and 4 tell us that the multiplica tive identity and multiplica tive inverses of nonzero real numbers are also unique. We leave their proofs as exercises at the end of this appendix. The multiplicative iden,tity.element 1 of the real numbers is unique. The multiplica tive inverse of a nonzero real number x is unique. For every real number x, x ·0 = O. Proof: Suppose that x is a real number. By the additive inverse law, there is a real number y that is the additive inverse of x . 0, so we have x . 0 + y = O. By the additive identity law, 0 + 0 = O . Using the distributive law, we see that x ·0 = x . (0 + 0) = x ·0 + x . O. It follows that o = x . 0 + y = (x . 0 + x . 0) + y. A-4 Appendix I / Axioms for the Real Numbers and the Positive Integers A /-4 THEOREM 6 THEOREM 7 linllS� Next, note that by the associative law for addition and because x . 0 + y = 0, it follows that (x ·0 + x ·0) + y = x ·0 + (x ·0 + y) = x ·0 + O. Finally, by the additive identity law, we know that x . 0 + 0 = x . O. Consequently, x . 0 = O. <I For all real numbers x and y, if x . y = 0, then x = 0 or y = O. Proof: Suppose that x and y are real numbers and x . y = O. If x #-0, then, by the multiplica tive inverse law, x has a multiplica tive inverse I/x, such that x . (l/x) = (l/x)· x = 1. Because x . y = 0, we have (I/x)· (x· y) = (l/x)· 0 = 0 by Theorem 5. Using the associate law for multiplication, we have «(l/x)· x)· y = O. This means that 1 . y = O. By the multiplica tive identity rule, we see that 1 . y = y, so y = O. Consequ ently, either x = 0 or y = O. <I The multiplicative identity element 1 in the set of real numbers is greater than the additive identity element O. Proof: By the trichotomy law, either 0 = 1, 0 > 1, or 1 > O. We know by the identity elements axiom that 0 #-1. So, assume that 0 > 1. We will show that this assumption leads to a contradiction. By the additive inverse law, 1 has an additive inverse -1 with 1 + (-1) = O. The additive compatibility law tells us that 0 + (-1) > 1 + (-1) = 0; the additive identity law tells us that 0 + ( -1) = -1. Consequently, -1 > 0, and by the multiplica tive compatibility law, (-1) . (-1) > (-1) . O. By Theorem 5 the right-hand side of last inequality is O. By the distributive law, (-1) . (-1) + ( -1) . 1 = (-1) . (-1 + 1) = (-1) . 0 = O. Hence, the left-hand side of this last inequality, ( -1) . (-1), is the unique additive inverse of -1, so this side of the inequality equals 1. Con­ sequently this last inequality becomes 1 > 0, contradicting the trichotom y law because we had assumed that 0 > 1. Because we know that 0 #-1 and that it is impossible for 0 > 1, by the trichotomy law, we conclude that 1 > O. <I ARCHIME DES (287 BCE-212 BCE) Archimedes was one of the greatest scientists and mathematicians of ancient times. He was born in Syracuse, a Greek city state in Sicily. His father, Phidias, was an astronomer. Archimedes was educated in Alexandria, Egypt. After completing his studies, he returned to Syracuse, where he spent the rest of his life. Little is known about his personal life; we do not know whether he was ever married or had children. Archmides was killed in 212 BCE by a Roman soldier when the Romans overran Syracuse. Archimedes made many important discoveries in geometry. His method for computing the area under a curve was described two thousand years before his ideas were re-invented as part of integral calculus. Archimedes also developed a method for expressing large integers inexpressible by the usual Greek method. He discovered a method for computing the volume of a sphere, as well as of other solids, and he calculated an approxi­ mation of 7r. Archimedes was also an accomplished engineer and inventor; his machine for pumping water, now called Archimedes' screw, is still in use today. Perhaps his best known discovery is the principle of buoyancy, which tells us that an object submerged in liquid becomes lighter by an amount equal to the weight it displaces. Some histories tell us that Archimedes was an early streaker, running naked through the streets of Syracuse shouting "Eureka" (which means "I have found it") when he made this discovery. He is also known for his clever use of machines that held off Roman forces sieging Syracuse for several years during the Second Punic War. Al-5 THEOREM S Appendix 1 / Axioms for the Real Numbers and the Positive Integers A-S The next theorem tells us that for every real number there is an integer (where by an integer, we mean 0, the sum of any number of 1 s, and the additive inverses of these sums) greater than this real number. This result is attributed to the Greek mathematician Archimede s. The result can be found in Book V of Euclid's Elements. ARCHIMED EAN PROPERTY For every real number x there exists an integer n such thatn > x. Proof: Suppose that x is a real number such that n ::: x for every integer n. Then x is an upper bound of the set of integers. By the completeness property it follows that the set ofintegers has a least upper bound M. Because M -1 < M and M is a least upper bound of the set of integers, M -I is not an upper bound of the set of integers. This means that there is an integer n with n > M -1. This implies that n + 1 > M, contradicting the fact that M is an upper bound of the set of integers. <l Axioms for the Set of Positive Integers The axioms we now list specify the set of positive integers as the subset of the set of integers satisfying four key properties. We assume the truth of these axioms in this textbook. Axiom 1 The number 1 is a positive integer. Axiom 2 If n is a positive integer, then n + 1, the successor of n, is also a positive integer. Axiom 3 Every positive integer other than 1 is the successor of a positive integer. Axiom 4 The Well-Ordering Property Every nonempty subset of the set of positive integers has a least element. In Sections 4.1 and 4.2 it is shown that the well-ordering principle is equivalent to the principle of mathematical induction. Mathematical Induction Axiom If S is a set such that 1 E S and such that ifn E S, then n + 1 E S, then S is the set of positive integers. Most mathema ticians take the real number system as already existing, with the real numbers satisfying the axioms we have listed in this appendix. However, mathemati cians in the nineteenth century developed techniques to construct the set of real numbers, starting with more basic sets of numbers. (The process of constructing the real numbers is sometimes studied in advanced undergraduate mathematics classes. A treatment of this can be found in [M087], for instance.) The first step in the process is the construction of the set of positive integers using Axioms 1-3 and either the Well-Ordering Property or the Mathematical Induction Axiom. Then, the operations of addition and multiplication of positive integers are defined. Once this has been done, the set of integers can be constructed using equivalence classes of pairs of positive integers where (a, b) "" (c, d) if and only if a + d = b + c; addition and multiplication of integers can be defined using these pairs (see Exercise 21). (Equivalence relations and equivalence classes are discussed in Chapter 8.) Next, the set of rational numbers can be constructed using the equivalence classes of pairs of integers where the second integer in the pair is not zero, where (a, b) � (c, d) if and only if a· d = b· c; addition and multiplication of rational numbers can be defined in terms of these pairs (see Exer cise 22). Using infinite sequences, the set of real numbers can then be constructed from the set of rational numbers. The interested reader will find it worthwhile to read through the many details of the steps of this construction. A-6 Appendix 1 / Axioms for the Real Numbers and the Positive Integers Al-6 Exercises Use only the axioms and theorems in this appendix in the proofs in your answers to these exercises. 1. Prove Theorem 3, which states that the multiplicative identity element in the real numbers is unique. 2. Prove Theorem 4, which states that for every nonzero real number x, the multiplicative inverse of x is unique. 3. Prove' that for all real numbers x and y, (-x)· y = x . (-y) = -(x · y). 4. Prove that for all real numbers x and y, -(x + y) = (-x) + (-y). 5. Prove that for all real numbers x and y, (-x) . ( -y) = x 'y. 6. Prove that for all real numbers x, y, and z, if x + Z = y+z,thenx =y. 7. Prove that for every real number x, -(-x) = x. Define the difference x -y of real numbers x and y by x -y = x + ( - y), where -y is the additive inverse of y, and the quotient x/y, where y I-0, by x/y = x . (l/y), where 1/ y is the multiplicative inverse of y. 8. Prove that for all real numbers x and y, x = y if and only ifx -y = O. 9. Prove that for all real numbers x and y, -x -y = -(x + y). 10. Prove that for all nonzero real numbers x andy, l/(x/y) = y/x, where l/(x/y) is the multiplicative inverse ofx/y. 11. Prove that for all real numbers w, x, y, and z, if x I- 0 andz I-0, then (w/x) + (y/z) = (w . z + X· y)/(x · z). 12. Prove that for every positive real number x, l/x is also a positive real number. 13. Prove that for all positive real numbers x and y, x . Y is also a positive real number. 14. Prove that for all real numbers x and y, if x > 0 and y < 0, then x . y < O. 15. Prove that for all real numbers x, y, and z, if x > y and z < 0, then x . z < y . z. 16. Prove that for every real number x, x I- 0 if and only if x2 > O. 17. Prove that for all real numbers w, x, y, and z, if w < x andy < z, then w + y < x + z. 18. Prove that for all positive real numbers x and y, if x < y, then l/x > l/y. 19. Prove that for every positive real number x, there exists a positive integer n such that n . x > 1. *20. Prove that between every two distinct real numbers there is a rational number (that is, a number of the form x/y, where x and y are integers with y I-0). Exercises 21 and 22 involve the notion of an equivalence re­ lation, discussed in Chapter 8 of the text. *21. Define a relation rv on the set of ordered pairs of positive integers by (w, x) rv (y, z) if and only ifw + z = x + y. Show that the operations [(w, x)]_ + [(y, z)]_ = [(w + y,x+z)]_ and [(w,x)]_·[(y,z)]_=[(w·y+x·z, x . y + w . z)]_ are well-defined, that is, they do not de­ pend on the representative of the equivalence classes cho­ sen for the computation. *22. Define a relation � on ordered pairs of integers with sec­ ond entry nonzero by(w, x) � (y, z)ifandonlyifw . z = x . y. Show that the operations [(w, x»)." + [(y, z»)." = [(w·z +x·y,x·z)]", and [(w,x)]",· [(y,z)]", = [(w·y, x . z»)." are well-defined, that is, they do not depend on the representative of the equivalence classes chosen for the computation. APPENDIX THEOREM 1 A2-J Exponential and Logarithmic Functions ][ n this appendix we review some of the basic properties of exponential functions and log­ arithms. These properties are used throughout the text. Students requiring further review of this material should consult precalculus or calculus books, such as those mentioned in the Suggested Readings. Exponential Functions Let n be a positive integer, and let b be a fixed positive real number. The function !b(n) = bn is defined by where there are n factors of b multiplied together on the right-hand side of the equation. We can define the function !b(X) = bX for all real numbers x using techniques from calculus. The function !b(X) = bX is called the exponential function to the base b. We will not discuss how to find the values of exponential functions to the base b when x is not an integer. Two of the important properties satisfied by exponential functions are given in Theorem 1. Proofs of these and other related properties can be found in calculus texts. Let b be a real number. Then 1. bX+Y = bXbY, and 2. (bX)Y = bXY. We display the graphs of some exponential functions in Figure 1. Logarithmic Functions Suppose that b is a real number with b > 1. Then the exponential function bX is strictly increasing (a fact shown in calculus). It is a one-to-one correspondence from the set of real numbers to the set of nonnegative real numbers. Hence, this function has an inverse, called the logarithmic function to the base b. In other words, if b is a real number greater than 1 and x is a positive real number, then The value of this function at x is called the logarithm of x to the base b. From the definition, it follows that 10gbbX = x. We give several important properties of logarithms in Theorem 2. A-7 A-8 Appendix 2 / Exponential and Logarithmic Functions A2-2 THEOREM 2 THEO REM 3 10 9 8 7 6 5 4 3 2 o y=OY 2 FI G URE 1 Graphs ofthe Exponen tial Functions to the Bases !, 2, and S. Let b be a real number greater than 1. Then 1. 10gb(xy) = 10gb x + 10gb y whenever x and y are positive real numbers, and 2. 10gb(xY) = y 10gb x whenever x is a positive real number. Proof: Because logb(xy) is the unique real number with bl<lgf,(xy) = xy, to prove part 1 it suffices to show that blo&,x+lo&,y = xy. By part 1 of Theorem 1, we have =xy. To prove part 2, it suffices to show that bY lo&, x = xy. By part 2 of Theorem 1, we have bYlo&,x = (blogbXY =xy• The following theorem relates logarithms to two different bases. Let a and b be real numbers greater than 1, and let x be a positive real number. Then lo� x = 10gb x 110gb a. Proof: To prove this result, it suffices to show that blog,. x . 10gb a = x. <l A2-3 3 2 2 3 4 Appendix 2 / Exponential and Logarithmic Functions A-9 5 FIGUR E 2 The Graph of f(x) = logx. By part 2 of Theorem I, we have blog,. x .108b a = (blo8/, a )Iog,. x = a log,. x =x. This completes the proof. <l Because the base used most often for logarithms in this text is b = 2, the notation log x is used throughout the test to denote log2 x. The graph of the function I(x) = logx is displayed in Figure 2. From Theorem 3, when a base b other than 2 is used, a function that is a constant multiple of the function log x, namely, (l/ log b) log x, is obtained. Exercises 1. Express each of the following quantities as powers of2. a) 2· 22 b) (22)3 c) 2(22) 2. Find each of the following quantities. a) log2 1024 b) log2 1/4 c) lo� 8 3. Suppose that lo� x = y. Find each of the following quantities. a) log2x b) loggx c) IOgl6x I:S> 4. Let a, b, and c be positive real numbers. Show that alO8b c = c108bQ• 5. Draw the graph of f(x) = lr if b is a) 3. b) 1/3. c) 1. 6. Draw the graph of f(x) = 10gb x if b is a) 4. b) 100. c) 1000. Pseudocode T he algorithms in this text are described both in English and in pseudocode. Pseudocode is J1 an intermediate step between an English language description of the steps of a procedure and a specification of this procedure using an actual programming language. The advantages of using pseudocode include the simplicity with which it can be written and understood and the ease of producing actual computer code (in a variety of programming languages) from the pseudocode. We will describe the particular types of statements, or high-level instructions, of the pseudocode that we will use. Each of these statements in pseudocode can be translated into one or more statements in a particular programming language, which in turn can be translated into one or more (possibly many) low-level instructions for a computer. This appendix describes the format and syntax of the pseudocode used in the text. This pseudocode is designed so that its basic structure resembles that of Pascal, which is currently the most commonly taught programming language. However, the pseudocode we use will be a lot looser than a formal programming language because a lot of English language descriptions of steps will be allowed. This appendix is not meant for formal study. Rather, it should serve as a reference guide for students when they study the descriptions of algorithms given in the text and when they write pseudocode solutions to exercises. Procedure Statements The pseudocode for an algorithm begins with a procedure statement that gives the name of an algorithm, lists the input variables, and describes what kind of variable each input is. For instance, the statement procedure maximum (L: list of integers) is the first statement in the pseudocode description of the algorithm, which we have named maximum, that finds the maximum of a list L of integers. Assignments and Other Types of Statements An assignment statement is used to assign values to variables. In an assignment statement the left-hand side is the name of the variable and the right-hand side is an expression that involves constants, variables that have been assigned values, or functions defined by procedure s. The right-hand side may contain any of the usual arithmetic operations. However, in the pseudocode in this book it may include any well-defined operation, even ifthis operation can be carried out only by using a large number of statements in an actual programming language. The symbol: = is used for assignments. Thus, an assignment statement has the form variable := expression For example, the statement A3-2 max:= a assigns the value of a to the variable max. A statement such as x : = largest integer in the list L Appendix 3 / Pseudocode A-ll can also be used. This sets x equal to the largest integer in the list L. To translate this statement into an actual programming language would require more than one statement. Also, the instruction interchange a and b can be used to interchange a and b. We could also express this one statement with several assignment statements (see Exercise 2 at the end of this appendix), but for simplicity, we will often prefer this abbreviated form of pseudocode. Blocks of Statements Statements can be grouped into blocks to carry out complicated procedure s. These blocks are set off using begin and end statements, and the statements in the block are all indented the same amount. begin statement 1 statement 2 statement 3 statement n end The statements in the block are executed sequentially. Comments In the pseudocode in this book, statements enclosed in curly braces are not executed. Such statements serve as comments or reminders that help explain how the procedure works. For instance, the statement A-12 Appendix 3 / Pseudocode {x is the largest element in L} A3-3 can be used to remind the reader that at that point in the procedure the variable x equals the largest element in the list L. Conditional Constructions The simplest form of the conditional construction that we will use is if condition then statement or if condition then begin block of statements end Here, the condition is checked, and if it is true, then the statement given is carried out. For example, in Algorithm 1 in Section 3.1, which finds the maximum of a set of integers, we use a conditional statement to check whether max < aj for each variable; if it is, we assign the value ofaj to max. Often, we require the use of a more general type of construction. This is used when we wish to do one thing when the indicated condition is true, but another when it is false. We use the construction if condition then statement 1 else statement 2 Note that either one or both of statement 1 and statement 2 can be replaced with a block of statements. Sometimes, we require the use of an even more general form of a conditional. The general form of the conditional construction that we will use is if condition 1 then statement 1 else if condition 2 then statement 2 else if condition 3 then statement 3 else if condition n then statement n else statement n + 1 A3-4 Appendix 3 / Pseudocode A-I3 When this construction is used, if condition 1 is true, then statement 1 is carried out, and the program exits this construction. In addition, if condition 1 is false, the program checks whether condition 2 is true; if it is, statement 2 is carried out, and so on. Thus, if none of the first n -1 conditions hold, but condition n does, statement n is carried out. Finally, if none of condition 1, condition 2, condition 3, ... , condition n is true, then statement n + 1 is executed. Note that any of the n + 1 statements can be replaced by a block of statements. Loop Constructions There are two types of loop construction in the pseudocode in this book. The first is the "for" construction, which has the form for variable : = initial value to final value statement or for variable : = initial value to final value begin block of statements end Here, at the start of the loop, variable is assigned initial value if initial value is less than or equal to final value, and the statements at the end of this construction are carried out with this value of variable. Then variable is increased by one, and the statement, or the statements in the block, are carried out with this new value of variable. This is repeated until variable reaches final value. After the instructions are carried out with variable equal to final value, the algorithm proceeds to the next statement. When initial value exceeds final value, none of the statements in the loop is executed. We can use the "for" loop construction to find the sum of the positive integers from I to n with the following pseudocode. sum :=0 for i := 1 to n sum :=sum +i Also, the more general "for" statement, of the form for all elements with a certain property is used in this text. This means that the statement or block of statements that follow are carried out successively for the elements with the given property. A-14 Appendix 3 / Pseudocode A3-5 The second type of loop construction that we will use is the "while" construction. This has the form whlle condition statement or while condition begin block of statements end When this construction is used, the condition given is checked, and if it is true, the statements that follow are carried out, which may change the values of the variables that are part of the condition. If the condition is still true after these instructions have been carried out, the instructions are carried out again. This is repeated until the condition becomes false. As an example, we can find the sum of the integers from I to n using the following block of pseudocode including a "while" construction. sum:= 0 whilen > 0 begin end sum :=sum +n n := n -1 Note that any "for" construction can be turned into a "while" construction (see Exercise 3 at the end of this appendix). However, it is often easier to understand the "for" construction. So, when it makes sense, we will use the "for" construction in preference to the corresponding "while" construction. Loops within Loops Loops or conditional statements are often used within other loops or conditional statements. In the pseudocode in this book, when a loop or a conditional statement is the entire contents of a loop or a conditional construction, we will not always use a separate pair of begin and end statements to enclose the statements that are carried out during each iteration, or when a specified condition is true. We do not always use a separate pair of begin and end statements because there is no ambiguity when they are omitted. (Note that some programming languages do not allow this omission.) Using Procedures in Other Procedures We can use a procedure from within another procedure (or within itself in a recursive program) simply by writing the name of this procedure followed by the inputs to this procedure. For instance, A3-6 max(L) Appendix 3 / Pseudocode A-IS will carry out the procedure max with the input list L. After all the steps of this procedure have been carried out, execution carries on with the next statement in the procedure. Exercises 1. What is the difference between the following blocks of two assignment statements? a :=b b :=c and b :=c a :=b 2. Give a procedure using assignment statements to inter­ change the values of the variables x and y. What is the minimum number of assignment statements needed to do this? 3. Show how a loop of the form for i := initial value to final value statement can be written using the ''while'' construction. Suggested Readings � mong the resources available for learning more about the topics covered in this book are printed ma­� terials and relevant websites. Printed resources are described in this section of suggested readings. Readings are listed by chapter and keyed to particular topics of interest. Some general references also de­ serve special mention. A book you may find particularly useful is the Handbook of Discrete and Combina­ torial Mathematics by Rosen [RoOO], a comprehensive reference book. Additional applications of discrete mathematics can be found in Michaels and Rosen [MiRo91], which is also available online in an Updated Edition on the MathZone companion website. Deeper coverage of many topics in computer science, in­ cluding those discussed in this book, can be found in Gruska [Gr97]. Biographical information about many of the mathematicians and computer scientists mentioned in this book can be found in Gillispie [Gi70]. To find pertinent websites, consult the links found in the Web Resources Guide on the MathZone companion website for this book. Its address is www.mhhe.com/rosen. CHAPTER 1 An entertaining way to study logic is to read Lewis Carroll's book [Ca7S]. General references for logic include M. Huth and M. Ryan [HuRy04 ], Mendelson [Me64], Stoll [St74], and Suppes [SuS7]. A comprehensive treatment of logic in discrete mathematics can be found in Gries and Schneider [GrSc93]. System specifications are discussed in Ince [In93]. Smullyan's knights and knaves puzzles were introduced in [Sm7S]. He has written many fascinating books on logic puzzles including [Sm92] and [Sm9S]. Prolog is discussed in depth in Nilsson and Maluszynski [NiMa95] and in Clocksin and Mellish [CIMe94]. The basics of proofs are covered in Cupillari [Cu05], Morash [Mo91], Solow [So04], Velleman [Ve94], and Wolf [Wo9S]. The science and art of constructing proofs is discussed in a delightful way in three books by P61ya: [Po62, 71, 90]. Problems involving tiling checkerboards using dominoes and polyominoes are discussed in Golomb [Go94] and Martin [Ma91]. CHAPTER 2 Lin and Lin [LiLiSl] is an easily read text on sets and their ap­ plications. Axiomatic developments of set theory can be found in Halmos [Ha60], Monk [Mo69], and Stoll [St74]. Brualdi [Br04] and Reingold, Nievergelt, and Deo [ReNiDe77] con­ tain introductions to multi sets. Fuzzy sets and their applica­ tion to expert systems and artificial intelligence are treated in Negoita [NeS5] and Zimmerman [Zi91]. Calculus books, such as Apostol [Ap67], Spivak [Sp94], and Thomas and Finney [ThFi96], contain discussions of functions. The best printed source of information about integer sequences is Sloan and Plouffe [SIPI95]. Stanat and McAllister [StMc77] has a thor­ ough section on countabil ity. Discussions of the mathemat­ ical foundations needed for computer science can be found 81-1 in Arbib, Kfoury, and Moll [ArKfMoSO], Bobrow and Arbib [BoAr74], Beckman [BeSO], and Tremblay and Manohar [TrMa75]. CHAPTER 3 The articles by Knuth [Kn77] and Wirth [WiS4] are ac­ cessible introductions to the subject of algorithms. Ex­ tensive material on big-O estimates of functions can be found in Knuth [Kn97a]. General references for algorithms and their complexity include Aho, Hopcroft, and Ullman [AhHoU174]; Baase and Van Gelder [BaGe99] ; Cormen, Leierson, Rivest, and Stein [CoLeRiStOl]; Gonnet [GoS4]; Goodman and Hedetniemi [GoHe77]; Harel [HaS7]; Horowitz and Sahni [HoSaS2]; Kreher and Stinson [KrSt9S]; the famous series of books by Knuth on the art of computer programming [Kn97a, b, 9S]; Kronsjo [KrS7]; Pohl and Shaw [PoShSl]; Purdom and Brown [PuBrS5]; Rawlins [Ra92]; Sedgewick [Se03]; Wilf [Wi02]; and Wirth [Wi76]. Sorting and search­ ing algorithms and their complexity are studied in detail in Knuth [Kn9 S]. References for number theory include Hardy and Wright [HaWr79], LeVeque [Le77], Rosen [Ro05], and Stark [St7S]. Applications of number theory to cryptography are covered in Denning [DeS2]; Menezes, van Oorschot, and Vanstone [MeOoV a97]; Rosen [Ro05]; Seberry and Pieprzyk [SePiS9]; Sinkov [Si66]; and Stinson [St05]. The RSA public­ key system was introduced in Rivest, Shamir, and Adleman [RiShAd7 S]. An appealing account of the history of cryptog­ raphy can be found in [Si99]. Algorithms for computer arith­ metic are discussed in Knuth [Kn97b] and Pohl and Shaw [PoShSl ]. Matrices and their operations are covered in all lin­ ear algebra books, such as Curtis [CuS4] and Strang [StSS]. 8-1 B-2 Suggested Readings CHAPTER 4 An accessible introduction to mathematical induction can be found in the English translation (from the Russian original) of Sominskii [S061]. Books that contain thorough treatments of mathematical induction and recursive definitions include Liu [Li85], Sahni [Sa85], Stanat and McAllister [StMc77], and Tremblay and Manohar [TrMa75]. The Ackermann function, introduced in 1928 by W. Ackermann, arises in the theory of recursive function (see Beckman [Be80] and McNaughton [Mc82], for instance) and in the analysis of the complexity of certain set theoretic algorithms (see Tarjan [Ta83]). Recursion is studied in Roberts [Ro86], Rohl [Ro84], and Wand [Wa80]. Discussions of program correctness and the logical machinery used to prove that programs are correct can be found in Alagic and Arbib [AIAr78], Anderson [An79], Backhouse [Ba86], Sahni [Sa85], and Stanat and McAllister [S tMc77]. CHAPTER S General references for counting techniques and their ap­ plications include Anderson [An74]; Berman and Fryer [BeFr72]; Bogart [BoOO]; Bona [Bo07]; Bose and Manvel [BoMa86]; Brualdi [Br04]; Cohen [Co78]; Grimaldi [Gr03]; Liu [Li68]; P6lya, Tarjan, and Woods [PoTaWo83]; Riordan [Ri58]; Roberts and Tesman [RoTe03]; Tucker [Tu02]; and Williamson [Wi85]. Vilenkin [Yi71] contains a selection of combinatorial problems and their solutions. A selection of more difficult combinatorial problems can be found in Lovasz [L079]. Information about Internet protocol addresses and datagrams can be found in Comer [Co05]. Applications of the pigeonhole principle can be found in Brualdi [Br04], Liu [Li85], and Roberts and Tesman [RoTe03]. A wide selection of combinatorial identities can be found in Riordan [Ri68] and in Benjamin and Quinn [BeQu03 ]. Combinatorial algorithms, including algorithms for generating permutations and combi­ nations, are described by Even [Ev73]; Lehmer [Le64]; and Reingold, Nievergelt, and Deo [ReNiDe77]. CHAPTER 6 Useful references for discrete probability theory include Feller [Fe68], Nabin [NaOO], and Ross [Ro05a]. Ross [Ro02], which rocuses on the application of probability theory to computer �cience, provides examples of average case complexity anal­ ysis and covers the probabilistic method. Abo and Ullman [AbUl95] includes a discussion of various aspects ofprobabil­ lty theory important in computer science, including program­ ning applications of probability. The probabilistic method is iiscussed in a chapter in Aigner and Ziegler [AiZi04], a mono­ �raph devoted to clever, insightful, and brilliant proofs, that is, )roofs that Paul Erdos described as coming from "The Book." �xtensive coverage of the probabilistic method can be found n Alon and Spencer [AISpOO]. Bayes' theorem is covered in :PaPiOI]. Additional material on spam filters can be found in :Zd05]. Bl-2 CHAPTER 7 Many different models using recurrence relations can be found in Roberts and Tesman [RoTe03] and Tucker [Tu02]. Exhaus­ tive treatments of linear homogeneous recurrence relations with constant coefficients, and related inhomogeneous recur­ rence relations, can be found in Brualdi [Br04] , Liu [Li68], and Mattson [Ma93]. Divide-and-conquer algorithms and their complexity are covered in Roberts and Tesman [RoTe03] and Stanat and McAllister [StMc77]. Descriptions offast multipli­ cation of integers and matrices can be found in Aho, Hopcroft, and Ullman [AhHoUl74] and Knuth [Kn97b]. An excellent introduction to generating functions can be found in P6lya, Tarjan, and Woods [PoTaWo83]. Generating functions are studied in detail in Brualdi [Br04]; Cohen [C078]; Graham, Knuth, and Patashnik [GrKnPa 94]; Grimaldi [Gr03]; and Roberts and Tesman [RoTe03]. Additional applications of the principle of inclusion-exclusion can be found in Liu [Li85, Li68], Roberts and Tesman [RoTe03], and Ryser [Ry63]. CHAPTER 8 General references for relations, including treatments of equivalence relations and partial orders, include Bobrow and Arbib [BoAr74], Grimaldi [Gr03] , Sanhi [Sa85], and Tremblay and Manohar [TrMa75]. Discussions of relational models for databases are given in Date [Da82] and Abo and Ullman [AbUl95]. The original papers by Roy and Warshall for finding transitive closures can be found in [Ro59] and [Wa62], respectively. Directed graphs are studied in Chartrand and Lesniak [ChLe96], Gross and Yellen [GrYe05], Robin­ son and Foulds [RoF080], Roberts and Tesman [RoTe03], and Tucker [Tu85]. The application oflattices to information flow is treated in Denning [De82]. CHAPTER 9 General references for graph theory include Behzad and Chartrand [BeCh 71], Chartrand and Lesniak [ChLe96], Char­ trand and Zhang [ChZh04], Bondy and Murty [BoMu76], Chartrand and Oellermann [ChOe93], Graver and Watkins [GrWa77], Gross and Yellen [GrYe03, 05], Harary [Ha69], Ore [0r63], Roberts and Tesman [RoTe03], Tucker [Tu02], Wilson [Wi85], and Wilson and Watkins [WiWa90]. A wide variety of applications of graph theory can be found in Char­ trand [Ch77] , Deo [De74], Foulds [F092], Roberts and Tesman [RoTe03], Roberts [Ro76], Wilson and Beineke [WiBe79], and McHugh [Mc90]. Applications involving large graphs, in­ cluding the Web graph, are discussed in Hayes [HaOOa, OOb]. A comprehensive description of algorithms in graph theory can be found in Gibbons [Gi85]. Other references for algorithms in graph theory include Buckley and Harary [BuHa90] ; Char­ trand and Oellermann [ChOe93]; Chachra, Ghare, and Moore [ChGhM079]; Even [Ev73, 79]; Hu [Hu82]; and Reingold, Nievergelt, andDeo [ReNiDe77]. A translation of Euler's orig­ inal paper on the Konigsberg bridge problem can be found in [Eu53]. Dijkstra's algorithm is studied in Gibbons [Gi85]; B1-3 Liu [Li85]; and Reingold, Nievergelt, and Deo [ReNiDe 77]. Dijkstra's original paper can be found in [Di59]. A proof of Kuratowski's theorem can be found in Harary [Ha69] and Liu [Li68]. Crossing numbers and thicknesses of graphs are stud­ ied in Chartrand and Lesniak [ChLe96]. References for graph coloring and the four-color theorem are included in Barnette [Ba83] and Saaty and Kainen [SaKa86]. The original conquest of the four-color theorem is reported in Appel and Haken [ApHa76]. Applications of graph coloring are described by Roberts and Tesman [RoTe03]. The history of graph theory is covered in Biggs, Lloyd, and Wilson [BiLlWi86]. Intercon­ nection networks for parallel processing are discussed in Akl [Ak89] and Siegel and Hsu [SiHs88]. CHAPTER 10 Trees are studied in Deo [De74], Grimaldi [Gr03], Knuth [Kn97a], Roberts and Tesman [RoTe03], and Tucker [Tu85]. The use of trees in computer science is described by Gotlieb and Gotlieb [GoGo78], Horowitz and Sahni [HoSa82], and Knuth [Kn97a, 98]. Roberts and Tesman [RoTe03] covers ap­ plications of trees to many different areas. Prefix codes and Huffman coding are covered in Hamming [Ha80]. Backtrack­ ing is an old technique; its use to solve maze puzzles can be found in the 1891 book by Lucas [Lu91]. An extensive dis­ cussion of how to solve problems using backtracking can be found in Reingold, Nievergelt, and Deo [ReNiDe 77]. Gibbons [Gi85] and Reingold, Nievergelt, and Deo [ReNiDe77] con­ tain discussions of algorithms for constructing spanning trees and minimal spanning trees. The background and history of algorithms for finding minimal spanning trees is covered in Graham and Hell [GrHe85]. Prim and Kruskal described their algorithms for finding minimal spanning trees in [Pr57] and [Kr56], respectively. Sollin's algorithm is an example of an algorithm well suited for parallel processing; although Sollin never published a description of it, his algorithm has been described by Even [Ev73] and Goodman and Hedetniemi [GoHe77]. CHAPTER 11 Boolean algebra is studied in Hohn [Ho66], Kohavi [Ko86], and Tremblay and Manohar [TrMa75]. Applications of Boolean algebra to logic circuits and switching circuits are described by Hayes [Ha93], Hohn [Ho66], Katz [Ka93], and Kohavi [K.o86]. The original papers dealing with the min­ imization of sum-of-products expansions using maps are Karnaugh [Ka53] and Veitch [Ve52]. The Quine-McClus key method was introduced in McCluskey [Mc56] and Quine [Qu52, 55]. Threshold functions are covered in Kohavi [K.o86]. CHAP TER 12 General references for formal grammars, automata theory, and the theory of computation include Davis, Sigal, and Weyuker Suggested Readings B-3 [DaSiWe94]; Denning, Dennis, and Qualitz [DeDeQu8 1]; Hopcroft, Motwani, and Ullman [HoMoUlO l]; Hopkin and Moss [HoMo76] ; Lewis and Papadimitriou [LePa97]; and McNaughton [Mc82]; and Sipser [Si06]. Mealy machines and Moore machines were originally introduced in Mealy [Me55] and Moore [Mo56]. The original proof of Kleene's theorem can be found in [Kl56]. Powerful models of com­ putation, including pushdown automata and Turing ma­ chines, are discussed in Brookshear [Br89], Hennie [He77], Hopcroft and Ullman [HoUl79], Hopkin and Moss [HoMo76], Martin [Ma03], Sipser [Si06], and Wood [Wo87]. Barwise and Etchemendy [BaEt93] is an excellent introduction to Turing machines. Interesting articles about the history and applica­ tion of Turing machines and related machines can be found in Herken [He88]. Busy beaver machines were first introduced by Rado in [Ra62], and information about them can be found in Dewdney [De84, 93], the article by Brady in Herken [He88], and in Wood [Wo87]. APPENDIXES A discussion ofaxioms for the real number and for the integers can be found in Morash [Mo91]. Detailed treatments of ex­ ponential and logarithmic functions can be found in calculus books such as Apostol [Ap67], Spivak [Sp94], and Thomas and Finney [ThFi96]. The pseudocode described in Appendix 3 resembles Pascal. Pohl and Shaw [PoSh81] uses a similar type of language to describe algorithms. Wirth [Wi76] de­ scribes how to use algorithms and data structures to build programs using Pascal. Also, Rohl [Ro84] shows how to im­ plement recursive programs using Pascal. REFER ENCES (AhHoUl74] A. V. Aho, J. E. Hopcroft, and J. D. Ullman, The Design and Analysis of Computer Algorithms, Addison­ Wesley, Reading, MA, 1974. (AhUl95] Alfred v. Aho and Jeffrey D. Ullman, Foundations of Computer Science, C Edition, Computer Science Press, New York, 1995. (AiZi04] Martin Aigner and Giinter M. Ziegler, Proofs from THE BOOK, 3d ed., Springer, Berlin, 2004. (Ak89] S. G. Akl, The Design and Analysis of Parallel Algo­ rithms, Prentice Hall, Englewood Cliffs, NJ, 1989. (AlAr78] S. Alagic and M. A. Arbib, The Design of Well-Structured and Correct Programs, Springer-Verlag, New York, 1978. (AlSpOO] Noga Alon and Joel H. Spencer, The Probabilistic Method, 2d ed., Wiley, New York, 2000. (An74] I. Anderson, A First Course in Combinatorial Math­ ematics, Clarendon, Oxford, England, 1974. (An79] R. B. Anderson, Proving Programs Correct, Wiley, New York, 1979. (Ap�7] T. M. Apostol, Calculus, Vol. I, 2d ed., Wiley, ·��W Yllrls 19�7� (ApHa76] K. Appel and W. Haken, "Every Planar Map Is 4-colorable," Bulletin of the AMS, 82 (1976), 711-712. B-4 Suggested Readings [ArKtMoSO] M. A. Arbib, A. 1. Kfoury, and R. N. Moll, A Basis for Theoretical Computer Science, Springer-Verlag, New York, 1980. [BaGe99] S. Baase and A. Van Gelder, Computer Algorithms: Introduction to Design and Analysis, 3d ed., Addison­ Wesley, Reading, MA, 1999. [BaS6] R. C. Backhouse, Program Construction and Verifica­ tion, Prentice-Hall, Englewood Cliffs, NJ, 1986. [BaS3] D. Barnette, Map Coloring, Polyhedra, and the Four­ Color Problem, Mathematical Association of America, Washington, DC, 1983. [BaEt93] Jon Barwise and John Etchemendy, Turing's World 3.0 for the Macintosh, CSLI Publications, Stanford, CA, 1993. [BeSO] F. S. Beckman, Mathematical Foundations of Pro­ gramming, Addison-Wesley, Reading, MA, 1980. [BeCh71] M. Behzad and G. Chartrand, Introduction to the Theory of Graphs, Allyn & Bacon, Boston, 1971. [BeQu03] A. Benjamin and 1. 1. Quine, Proofs that Really Count, Mathematical Association of America, Washing­ ton, DC, 2003. [BeS6] 1. Bentley, Programming Pearls, Addison-W esley, Reading, MA, 1986. [BeFr72] G. Berman and K. D. Fryer, Introduction to Combi­ natorics, Academic Press, New York, 1972. [BiLIWiS6] N. L. Biggs, E. K. Lloyd, and R. J. Wilson, Graph Theory 1736-1936, Clarendon, Oxford, England, 1986. [BoAr74] L. S. Bobrow and M. A. Arbib, Discrete Mathe­ matics, Saunders, Philadelphia, 1974. [BoOO] K. P. Bogart, Introductory Combinat orics, 3d ed. Academic Press, San Diego, 2000. [Bo07] M. Bona, Enumerative Combinat orics, McGraw-Hill, New York, 2007. [BoMu76] 1. A. Bondy and U. S. R. Murty, Graph Theory with Applications, North-Holland, New York, 1976. [BoMaS6] R. C. Bose and B. Manvel, Introduction to Com­ binatorial Theory, Wiley, New York, 1986. [BrS9] 1. G. Brookshear, Theory of Computation, Benjamin Cummings, Redwood City, CA, 1989. [Br04] R. A. Brualdi, Introductory Combinat orics, 4th ed., Prentice-Hall, Englewood Cliffs, NJ, 2004. [BuHa90] F. Buckley and F. Harary, Distance in Graphs, Addison-W esley, Redwood City, CA, 1990. :Ca79] L. Carmony, "Odd Pie Fights," Mathematics Teacher 72 (January, 1979),61-64. Ca7S] L. Carroll, Symbolic Logic, Crown, New York, 1978. ChGhM079] V. Chachra, P. M. Ghare, and 1. M. Moore, Applications of Graph Theory Algorithms, North-Holland, New York, 1979. Ch77] G. Chartrand, Graphs as Mathematical Models, Prindle, Weber & Schmidt, Boston, 1977. ChLe96] G. Chartrand and L. Lesniak, Graphs and Digraphs, 3d ed., Wadsworth, Belmont, CA, 1996. ChOe93] G. Chartrand and O. R. Oellermann, Applied Al­ gorithmic Graph Theory, McGraw-Hill, New York, 1993. ChZh04] G. Chartrand and P Zhang, Introduction to Graph Theory, McGraw-Hill, New York, 2004. BI-4 [ClMe94] W. F. Clocksin and C. S. Mellish, Programming in Prolog, 4th ed., Springer-Verlag, New York, 1994. [C07S] D. I. A. Cohen, Basic Techniques of Combinatorial Theory, Wiley, New York, 1978. [CoOS] D. Comer, Internetworking with TCPIIp, Principles, Protocols, and Architecture, Vol. I, 5th ed., Prentice-Hall, Englewood Cliffs, NJ, 2005. [CoLeRiStO l] T. H. Cormen, C. E. Leierson, R. L. Rivest, and C. Stein, Introduction to Algorithms, 2d ed., MIT Press, Cambridge, MA, 200 1. [CrPoOl] Richard Crandall and Carl Pomerance, Prime Numbers: A Computational Perspective, Springer-Verlag, New York, 2001. [CuOS] Antonella Cupillari, The Nuts and Bolts of Proofs, 3d ed., Academic Press, San Diego, 2005. [CuS4] C. W. Curtis, Linear Algebra, Springer-Verlag, New York, 1984. [DaS2] C. 1. Date, An Introduction to Database Systems, 3d ed., Addison-W esley, Reading, MA, 1982. [DaSiWe94] M. Davis, R. Sigal, and E. 1. Weyuker, Com­ putability, Complexity, and Languages, 2d ed., Academic Press, San Diego, 1994. [DeS2] D. E. R. Denning, Cryptography and Data Security, Addison-W esley, Reading, MA, 1982. [DeDeQuS l] P. 1. Denning, 1. B. Dennis, and 1. E. Qualitz, Machines, Languages, and Computation, Prentice-Hall, Englewood Cliffs, NJ, 1981. [De74] N. Deo, Graph Theory with Applications to Engi­ neering and Computer Science, Prentice-Hall, Englewood Cliffs, NJ, 1974. [De02] K. Devlin, The Millennium Problems: The Seven Greatest Unsolved Mathematical Puzzles of Our Time, Basic Book, New York, 2002. [DeS4] A. K. Dewdney, "Computer Recreations," Scien­ tific American, 251, no. 2 (August 1984), 19-23; 252, no. 3 (March 1985), 14-23; 251, no. 4 (April 1985), 20-30. [De93] A. K. Dewdney, The New Turing Omnibus: Sixty­ Six Excursions in Computer Science, W. H. Freeman, New York, 1993. [DiS9] E. Dijkstra, "Two Problems in Connexion with Graphs," Numerische Mathematik, 1 (1959),269-271. [EuS3] L. Euler, "The Koenigsberg Bridges," Scientific American, 189, no. 1 (July 1953),66-70. [Ev73] S. Even, Algorithmic Combinat orics, Macmillan, New York, 1973. [Ev79] S. Even, Graph Algorithms, Computer Science Press, Rockville, MD, 1979. [Fe6S] W. Feller, An Introduction to Probability Theory and Its Applications, Vol. 1, 3d ed., Wiley, New York, 1968. [F092] L. R. Foulds, Graph Theory Applications, Springer­ Verlag, New York, 1992. [GaJ079] Michael R. Garey and David S. Johnson, Computers and Intractability: A Guide to NP-Completeness, Freeman, New York, 1979. [GiSS] A. Gibbons, Algorithmic Graph Theory, Cambridge University Press, Cambridge, England, 1985. 81-5 [Gi70) C. C. Gillispie, ed., Dictionary of Scientific Biography, Scribner's, New York, 1970. [G094) S. W. Golomb, Polyominoes, Princeton University Press, Princeton, NJ, 1994. [GoS4) G. H. Gonnet, Handbook of Algorithms and Data Structures, Addison-W esley, London, 1984. [GoHe77) S. E. Goodman and S. T. Hedetniemi, Introduction to the Design and Analysis of Algorithms, McGraw-Hill, New York, 1977. [GoG07S) C. C. Gotlieb and L. R. Gotlieb, Data Types and Structures, Prentice-Hall, Englewood Cliffs, NJ, 1978. [GrHeS5) R. L. Graham and P. Hell, "On the History of the Minimum Spanning Tree Problem," Annals of the History of Computing, 7 (1985), 43-57. [GrKnPa94) R. L. Graham, D. E. Knuth, and o. Patashnik, Concrete Mathematics, 2d ed., Addison-W esley, Reading, MA,1994. [GrRoSp90) Ronald L. Graham, Bruce L. Rothschild, and Joel H. Spencer, Ramsey Theory, 2d ed., Wiley, New York, 1990. [GrWa77) 1. E. Graver and M. E. Watkins, Combinat orics with Emphasis on the Theory of Graphs, Springer-Verlag, New York, 1977. [GrSc93) D. Gries and F. B. Schneider, A Logical Approach to Discrete Math, Springer-Verlag, New York, 1993. [Gr03) R. P. Grimaldi, Discrete and Combinatorial Mathe­ matics, 5th ed., Addison-W esley, Reading, MA, 2003. [GrYe05) 1. L. Gross and 1. Yellen, Graph Theory and Its Applications, 2d ed., CRC Press, Boca Raton, FL, 2005. [GrYe03) 1. L. Gross and 1. Yellen, Handbook of Graph Theory, CRC Press, Boca Raton, FL, 2003. [Gr90) Jerrold W. Grossman, Discrete Mathematics: An Introduction to Concepts, Methods, and Applications, Macmillan, New York, 1990. [Gr97) 1. Gruska, Foundations of Computing, International Thomsen Computer Press, London, 1997. [Ha60) P. R. Ha1mos, Naive Set Theory, D. Van Nostrand, New York, 1960. [HaSO) R. W. Hamming, Coding and lriformation Theory, Prentice-Hall, Englewood Cliffs, NJ, 1980. [Ha69) F. Harary, Graph Theory, Addison-W esley, Reading, MA,1969. [Ha Wr79) G. H. Hardy and E. M. Wright, An Introduction to the Theory of Numbers, 5th ed., Oxford University Press, Oxford, England, 1979. [HaS7) D. Harel, Algorithmics, The Spirit of Computing, Addison-W esley, Reading, MA, 1987. [HaOOa) Brian Hayes, "Graph-Theory in Practice, Part I," American Scientist, 88, no. 1 (2000),9-13. [HaOOb) Brian Hayes, "Graph-Theory in Practice, Part II;' American Scientist, 88, no. 2 (2000), 104--109. [Ha93) John P. Hayes, Introduction to Digital Logic Design, Addison-Wesley, Reading, MA, 1993. [He77) F. Hennie, Introduction to Computability, Addison­ Wesley, Reading, MA, 1977. [HeSS) R. Herken, The Universal Turing Machine, A Half­ Century Survey, Oxford University Press, New York, 1988. Suggested Readings 8-5 [H076) C. Ho, "Decomposition of a Polygon into Triangles," Mathematical Gazette, 60 (1976), 132-134. [H099) D. Hofstadter, GOdel, Escher, Bach: An Internal Golden Braid, Basic Books, New York, 1999. [H066) F. E. Hohn, Applied Boolean Algebra, 2d ed., Macmillan, New York, 1966. [HoMoU lOl) 1. E. Hopcroft and 1. D. Ullman, Introduction to Automata Theory, Languages, and Computation, 2d ed., Addison-W esley, Reading, MA, 1979. [HoM076) D. Hopkin and B. Moss, Automata, Elsevier, North-Holland, New York, 1976. [HoSaS2) E. Horowitz and S. Sahni, Fundamentals of Com­ puter Algorithms, Computer Science Press, Rockville, MD,1982. [HuS2) T. c. Hu, Combinatorial Algorithms, Addison-W esley, Reading, MA, 1982. [HuRy04 ), M. Huth and M. Ryan, Logic in Computer Science, 2d ed., Cambridge University Press, Cambridge, England, 2004. [1093) D. C. Ince, An Introduction to Discrete Mathematics, Formal System Specification, and Z, 2d ed., Oxford, New York,1993. [KaM064) D. Kalish and R. Montague, Logic: Techniques of Formal Reasoning, Harcourt, Brace, Jovanovich, New York, 1964. [Ka53) M. Karnaugh, "The Map Method for Synthesis of Combinatorial Logic Circuits," Transactions of the AlEE, part I, 72 (1953), 593-599. [Ka93) Randy H. Katz, Contemporary Logic Design, Addison-W esley, Reading, MA, 1993. [Kl56) S. C. Kleene, "Representation of Events by Nerve Nets," in Automata Studies, 3-42, Princeton University Press, Princeton, NJ, 1956. [Kn77) D. E. Knuth, "Algorithms," Scientific American, 236, no. 4 (April 1977), 63-80. [K097a) D. E. Knuth, The Art of Computer Programming, Vol. I: Fundamental Algorithms, 3d ed., Addison-W esley, Reading, MA, 1997a. [Kn97b) D. E. Knuth, The Art of Computer Programming, Vol. II: Seminumerical Algorithms, 3d ed., Addison-W esley, Reading, MA, 1997b. [K09S) D. E. Knuth, The Art of Computer Programming, Vol. III: Sorting and Searching, 2d ed., Addison-W esley, Reading, MA, 1998. [KoS6) Z. Kohavi, Switching and Finite Automata Theory, 2d ed., McGraw-Hill, New York, 1986. [KrSt9S) Donald H. Kreher and Douglas R. Stinson, Com­ binatorial Algorithms: Generation, Enumeration, and Search, CRC Press, BocaRaton, FL, 1998. [KrS7) L. Kronsjo, Algorithms: Their Complexity and Efficiency, 2d ed., Wiley, New York, 1987. [Kr56) 1. B. Kruskal, "On the Shortest Spanning Subtree of a Graph and the Traveling Salesman Problem," Proceedings of the AMS, 1 (1956),48-50. [Le64) D. H. Lehmer, "The Machine Tools ofCombinatorics," in E. F. Beckenbach (ed.), Applied Combinatorial Mathe­ matics, Wiley, New York, 1964. B-6 Suggested Readings [Le77] W. 1. LeVeque, Fundamentals of Number Theory, Addison-W esley, Reading, MA, 1977. [LePa97] H. R. Lewis and C. H. Papadimitriou, Elements of the Theory of Computation, 2d ed., Prentice-Hall, Englewood Cliffs, NJ, 1997. [LiLi81] Y. Lin and S. Y. T. Lin, Set Theory with Applications, 2d ed., Mariner, Tampa, FL, 1981. [Li68] C. L. Liu, Introduction to Combinatorial Mathematics, McGraw-Hill, New York, 1968. [Li8S] C. L. Liu, Elements of Discrete Mathematics, 2d ed., McGraw-Hill, New York, 1985. [Lo79] L. Lovasz, Combinatorial Problems and Exercises, North-Holland, Amsterdam, 1979. [Lu91] E. Lucas, Recreations Mathematiques, Gauthier­ Villars, Paris, 1891. [Ma91] G. E. Martin, Polyominoes: A Guide to Puzzles and Problems in Tiling, Mathematical Association of America, Washington, DC, 1991. [Ma03] 1. C. Martin, Introduction to Languages and the The­ oryofComputation, 3d ed., McGraw-Hill, New York, 2003. [Ma93] H. F. Mattson, Jr., Discrete Mathematics with Appli­ cations, Wiley, New York, 1993. [McS6] E. 1. McCluskey, Jr., "Minimization of Boolean Func­ tions," Bell System Technical Journal, 35 (1956), 1417- 1444. [Mc90] 1. A. McHugh, Algorithmic Graph Theory, Prentice­ Hall, Englewood Cliffs, NJ, 1990. [Mc82] R. McNaughton, Elementary Computability, For­ mal Languages, and Automata, Prentice-Hall, Englewood Cliffs, NJ, 1982. [MeSS] G. H. Mealy, "A Method for Synthesizing Sequen­ tial Circuits," Bell System Technical Journal, 34 (1955), 1045-1079. [Me64] E. Mendelson, Introduction to Mathematical Logic, Van Nostrand Reinhold, New York, 1964. [MeOo Va97] A. 1. Menezes, P. C. van Oorschot, S. A. Vanstone, Handbook of Applied Cryptography, CRC Press, Boca Raton, FL, 1997. [MiRo91] 1. G. Michaels and K. H. Rosen, Applications of Discrete Mathematics, McGraw-Hill, New York, 1991. [Mo69] 1. R. Monk, Introduction to Set Theory, McGraw-Hill, New York, 1969. [Mo91] R. P. Morash, Bridge to Abstract Mathematics, McGraw-Hill, New York, 1991. [MoS6] E. F. Moore, "Gedanken-Experiments on Sequential Machines," in Automata Studies, 129-153, Princeton Uni­ versity Press, Princeton, NJ, 1956. [NaOO] Paul 1. Nabin, Duelling Idiots and Other Probability Puzzlers, Princeton University Press, Princeton, NJ, 2000. [Ne8S] C. V. Negoita, Expert Systems and Fuzzy Systems, Benjamin Cummings, Menlo Park, CA, 1985. [NiMa9S] UlfNilsson and Jan Maluszynski, Logic, Program­ ming, and Prolog, 2d ed., Wiley, Chichester, England, 1995. [orOO] 1. O'Rourke, Computational Geometry in C, Cambridge University Press, New York, 2000. B1-6 [Or63] O. Ore, Graphs and Their Uses, Mathematical Asso­ ciation of America, Washington, DC, 1963. [PaPiOl] A. Papoulis and S. U. Pillai, Probability, Ran­ dom Variables, and Stochastic Processes, McGraw-Hill, New York, 2001. [Pe87] A. Pelc, "Solution of Ulam's Problem on Searching with a Lie," Journal of Combinatorial Theory, Series A, 44 (1987),129-140. [PoSh81] I. Pohl and A. Shaw, The Nature of Computation: An Introduction to Computer Science, Computer Science Press, Rockville, MD, 1981. [Po62] George P61ya, Mathematical Discovery, Vols. I and 2, Wiley, New York, 1962. [Po71] George P61ya, How to Solve It, Princeton University Press, Princeton, NJ, 1971. [Po90] George P61ya, Mathematics and Plausible Reasoning, Princeton University Press, Princeton, NJ, 1990. [PoTaWo83] G. P61ya, R. E. Tarjan, and D. R. Woods, Notes on Introductory Combinat orics, Birkhauser, Boston, 1983. [PrS7] R. C. Prim, "Shortest Connection Networks and Some Generalizations," Bell System Technical Journal, 36 (1957), 1389-1401. [PuBr8S] P. W. Purdom, Jr. and C. A. Brown, The Analysis of Algorithms, Holt, Rinehart & Winston, New York, 1985. [QuS2] W. V. Quine, "The Problem of Simplifying Truth Func­ tions," American Mathematical Monthly, 59 (1952), 521- 531. [QuSS] W. V. Quine, "A Way to Simplify Truth Functions," American Mathematical Monthly, 62 (1955), 627--631. [Ra62] T. Rado, "On Non-Computable Functions," Bell Sys­ tem Technical Journal (May 1962), 877-884. [Ra92] Gregory 1. E. Rawlins, Compared to What? An Intro­ duction to the Analysis of Algorithms, Computer Science Press, New York, 1992. [ReNiDe77] E. M. Reingold, 1. Nievergelt, and N. Deo, Com­ binatorial Algorithms: Theory and Practice, Prentice-Hall, Englewood Cliffs, NJ, 1977. [RiS8] 1. Riordan, An Introduction to Combinatorial Analysis, Wiley, New York, 1958. [Ri68] 1. Riordan, Combinatorial Identities, Wiley, New York, 1968. [RiShAd78] R. Rivest, A. Shamir, and L. Adleman, "A Method for Obtaining Digital Signatures and Public-Key Cryptosystems," Communications of the Association for Computing Machinery, 31, no. 2 (1978), 120-128. [Ro86] E. S. Roberts, Thinking Recursivel y, Wiley, New York, 1986. [Ro76] F. S. Roberts, Discrete Mathematics Models, Prentice­ Hall, Englewood Cliffs, NJ, 1976. [RoTe03] F. S. Roberts· and B. Tesman, Applied Combina­ torics, 2d ed., Prentice-Hall, Englewood Cliffs, NJ, 2003. [RoFo80] D. F. Robinson and L. R. Foulds, Digraphs: Theory and Techniques, Gordon and Breach, New York, 1980. [Ro84] 1. S. Rohl, Recursion via Pascal, Cambridge Univer­ sity Press, Cambridge, England, 1984. BI-7 [RoOS] K. H. Rosen, Elementary Number Theory and Its Ap­ plications, 5th ed., Addison-W esley, Reading, MA, 2005. [RoOO] K. H. Rosen, Handbook of Discrete and Combinato­ rial Mathematics, CRC Press, Boca Raton, FL, 2000. [RoOSa] Sheldon M. Ross, A First Course in Probability The­ ory, 7th ed., Prentice-Hall, Englewood Cliffs, NJ, 2001. [Ro02] Sheldon M. Ross, Probability Models for Computer Science, Harcourt/Academic Press, 2002. [RoS9] B. Roy, "Transitivite et Connexite," C.R. Acad. Sci. Paris, 249 (1959), 216. [Ry63] H. Ryser, Combinatorial Mathematics, Mathematical Association of America, Washington, DC, 1963. [SaKaS6] T. L. Saaty and P. C. Kainen, The Four-Color Prob­ lem: Assaults and Conquest, Dover, New York, 1986. [SaSS] S. Sahni, Concepts in Discrete Mathematics, Camelot, Minneapolis, 1985. [SePiS9] 1. Seberry and 1. Pieprzyk, Cryptography: An Intro­ duction to Computer Security, Prentice-Hall, Englewood Cliffs, NJ, 1989. [Se03] R. Sedgewick, Algorithms in Java, 3d ed., Addison­ Wesley, Reading, MA, 2003. [SiHsSS] H. 1. Siegel and W. T. Hsu, "Interconnection Net­ works," in Computer Architectures, V. M. Milutinovic (ed.), North-Holland, New York, 1988, pp. 225-264. [Si99] S. Singh, The Code Book, Doubleday, New York, 1999. [Si66] A. Sinkov, Elementary Cryptanalysis, Mathematical Association of America, Washington, DC, 1966. [Si06] M. Sipser, Introduction to the Theory of Computation, Course Technology, Boston, 2006 [SIPI9S] N. 1. A. Sloane and S. Plouffe, The Encyclopedia of Integer Sequences, Academic Press, New York, 1995. [Sm7S] Raymond Smullyan, What Is the Name of This Book?: The Riddle of Dracula and Other Logical Puzzles, Prentice-Hall, Englewood Cliffs, NJ, 1978. [Sm92] Raymond Smullyan, Lady or the Tiger? And Other Logic Puzzles Including a Mathematical Novel That Fea­ tures Godel s Great Discovery, Times Book, New York, 1992. [Sm9S] Raymond Smullyan, The Riddle ofScheherazade: And Other Amazing Puzzles, Ancient & Modern, Harvest Book, Fort Washington, PA, 1998. [So04] Daniel Solow, How to Read and Do Proofs: An Intro­ duction to Mathematical Thought Processes, Wiley, New York,2001. [S061] I. S. Sominskii, Method of Mathematical Induction, Blaisdell, New York, 1961. [Sp94] M. Spivak, Calculus, 3d ed., Publish or Perish, Wilmington, DE, 1994. [StMc77] D. Stanat and D. F. McAllister, Discrete Mathemat­ ics in Computer Science, Prenti ce-Hall, Englewood Cliffs, NJ,1977. [St7S] H. M. Stark, An Introduction to Number Theory, MIT Press, Cambridge, MA, 1978. [StOS] Douglas R. Stinson, Cryptography, Theory and Prac­ tice, 3d ed., Chapman and Hall/CRC, Boca Raton, FL, 2005. Suggested Readings B-7 [St94] P. K. Stockmeyer, "Variations on the Four-Post Tower of Hanoi Puzzle," Congressus Numerantrum 102 (1994), 3-12. [St74] R. R. Stoll, Sets, Logic, and Axiomatic Theories, 2d ed., W. H. Freeman, San Francisco, 1974. [StSS] G. w. Strang, Linear Algebra and Its Applications, 3d ed., Harcourt Brace Jovanovich, San Diego, 1988. [SuS7] P. Suppes, Introduction to Logic, D. Van Nostrand, Princeton, NJ, 1987. [TaS3] R. E. Tarjan, Data Structures and Network Algo­ rithms, Society for Industrial and Applied Mathematics, Philadelphia, 1983. [ThFi96] G. B. Thomas and R. L. Finney, Calculus and An­ alytic Geometry, 9th ed., Addison-W esley, Reading, MA, 1996. [TrMa7S] 1. P. Tremblay and R. P. Manohar, Discrete Mathe­ matical Structures with Applications to Computer Science, McGraw-Hill, New York, 1975. [To02] Alan Tucker, Applied Combinat orics, 4th ed., Wiley, New York, 2002. [VeS2] E. W. Veitch, "A Chart Method for Simplifying Truth Functions," Proceedings of the ACM (1952), 127- 133. [Ve94] David Velleman, How to Prove It: A Structured Ap­ proach, Cambridge University Press, New York, 1994. [Vi71] N. Y. Vilenkin, Combinat orics, Academic Press, New York, 1971. [WaSO] M. Wand, Induction, Recursion, and Programming, North-Holland, New York, 1980. [Wa62] S. Warshall, ''A Theorem on Boolean Matrices," Jour­ nal of the ACM, 9 (1962), 11-12. [Wi02] Herbert S. Wilf, Algorithms and Complexity, 2d ed., A. K. Peters, Natick, MA, 2002. [WiSS] S. G. Williamson, Combinat orics for Computer Science, Computer Science Press, Rockville, MD, 1985. [WiSS] R. 1. Wilson, Introduction to Graph Theory, 3d ed., Longman, Essex, England, 1985. [WiBe79] R. 1. Wilson and L. W. Beineke, Applications of Graph Theory, Academic Press, London, 1979. [WiWa90] R. 1. Wilson and 1. 1. Watkins, Graphs, An Intro­ ductory Approach, Wiley, New York, 1990. [Wi76] N. Wirth, Algorithms + Data Structures = Programs, Prentice-Hall, Englewood Cliffs, NJ, 1976. [Wi84] N. Wirth, "Data Structures and Algorithms," Scientific American, 251 (September 1984), 60-69. [W09S] Robert S. Wolf, Proof, Logic, and Conjecture: The Mathematician s Toolbox, W. H. Freeman, New York, 1998. [WoS7] D. Wood, Theory of Computation, Harper & Row, New York, 1987. [ZdOS] 1. Zdziarski, Ending Spam: Bayesian Content Filter­ ing and the Art of Statistical Language Class ification, No Starch Press, San Francisco, 2005. [Zi91] H. 1. Zimmermann, Fuzzy Set Theory and Its Applica­ tions, 2d ed., Kluwer, Boston, 1991. Answers to Odd-Numbe red Exercises CHAPTER 1 Section 1.1 I. a) Yes, T b) Yes, F e) Yes, T d) Yes, F e) No t) No 3. a) Today is not Thursday. b) There is pollution in New Jersey. e) 2 + 1 =1= 3. d) The summer in Maine is not hot or it is not sunny. 5. a) Sharks have not been spotted near the shore. b) Swimming at the New Jersey shore is allowed, and sharks have been spotted near the shore. e) Swimming at the New Jersey shore is not allowed, or sharks have been spotted near the shore. d) If swimming at the New Jersey shore is allowed, then sharks have not been spotted near the shore. e) If sharks have not been spotted near the shore, then swimming at the New Jersey shore is allowed. t) If swimming at the New Jersey shore is not allowed, then sharks have not been spotted near the shore. g) Swimming at the New Jersey shore is allowed if and only if sharks have not been spotted near the shore. h) Swimming at the New Jersey shore is not allowed, and either swimming at the New Jersey shore is allowed or sharks have not been spotted near the shore. (Note that we were able to incorpo­ rate the parentheses by using the word "either" in the second half of the sentence.) 7. a) p 1\ q b) p I\ ..... q e) ..... p 1\ ..... q d)pvq e)p�q t)(pvq)l\(p� ..... q) g) q � p 9. a) ""'p b) p 1\ ..... q e) p � q d) ""'p � ..... q e) p � q t) q 1\ ""'p g) q � p 11. a) r 1\ ""'p b) ..... pl\q I\r e)r � (q � ..... p) d) ""'ql\""'pl\r e)(q �( ..... r 1\ ..... p» 1\ ..... « ..... r 1\ ..... p) � q) t) (p I\r) � ..... q 13. a) False b) True e) True d) True 15. a) Exclu­ sive or: You get only one beverage. b) Inclusive or: Long passwords can have any combination of symbols. e) Inclu­ sive or: A student with both courses is even more qualified. d) Either interpretation possible; a traveler might wish to pay with a mixture of the two currencies, or the store may not allow that. 17. a) Inclusive or: It is allowable to take discrete mathematics if you have had calculus or computer science, or both. Exclusive or: It is allowable to take discrete mathematics if you have had calculus or computer science, but not if you have had both. Most likely the inclusive or is intended. b) Inclusive or: You can take the rebate, or you can get a low-interest loan, or you can get both the rebate and a low-interest loan. Exclusive or: You can take the rebate, or you can get a low-interest loan, but you cannot get both the rebate and a low-interest loan. Most likely the exclusive or is intended. e) Inclusive or: You can order two items from column A and none from column B, or three items from column B and none from column A, or five items including two from column A and three from column B. Exclusive or: You can order two items from column A or three items from column B, but not both. Almost certainly the exclusive or is intended. d) Inclusive or: More than 2 feet of snow or windchill below -100, or both, will close school. Exclusive or: More than 2 feet of snow or windchill below -100, but not both, will close school. Certainly the inclusive or is intended. 19. a) If the wind blows from the northeast, then it snows. b) If it stays warm for a week, then the apple trees will bloom. e) If the Pistons win the championship; then they beat the Lakers. d) If you get to the top of Long's Peak, then you must have walked 8 miles. e) If you are world-famous, then you will get tenure as a professor. t) If you drive more than 400 miles, then you will need to buy gasoline. g) If your guarantee is good, then you must have bought your CD player less than 90 days ago. h) If the water is not too cold, then Jan will go swimming. 21. a) You buy an ice cream cone if and only if it is hot outside. b) You win the contest if and only if you hold the only winning ticket. e) You get promoted if and only if you have connections. d) Your mind will decay if and only if you watch television. e) The train runs late if and only if it is a day I take the train. 23. a) Converse: "I will ski tomorrow only if it snows today." Contrapositive: "If I do not ski tomorrow, then it will not have snowed to­ day." Inverse: "If it does not snow today, then I will not ski tomorrow." b) Converse: "If! come to class, then there will be a quiz." Contrapositive: "If I do not come to class, then there will not be a quiz." Inverse: "If there is not going to be a quiz, then I don't come to class." e) Converse: "A positive integer is a prime if it has no divisors other than 1 and itself." Contrapositive: "If a positive integer has a divisor other than 1 and itself, then it is not prime." Inverse: "If a positive integer is not prime, then it has a divisor other than 1 and itself." 25. a) 2 b) 16 c) 64 d) 16 27. a) p T F e) p T T F F d) p T T F F -,p P /\ -,p q T F T F q T F T F F F T F -,q p V-,q F T T T F F T T pVq P /\q T T T F T F F F b) P T F -,p P /\ -,p F T T T (p V -,q) -+ q T F T F (p V q) -+ (p /\ q) T F F T S-2 Answers to Odd-Numbered Exercises e) (p -+ q) +-+ p q p-+q -,q -,p -,q -+-'p (-,q -+ -'p) T T T F F T T T F F T F F T F T T F T T T F F T T T T T 29. For parts (a), (b), (c), (d), and (f) we have this table. p q (p V q) -+ (p ffi q) (p ffi q) -+ (p /\ q) (p V q) ffi (p /\ q) T T F T F T F T F T F T T F T F F T T F For part (e) we have this table. p q r -,p -,r p+-+q -,p +-+ -,r (p +-+ q) ffi (-,p +-+ -,r) T T T F F T T F T T F F T T F T T F T F F F T T T F F F T F F F F T T T F F F F F T F T T F T T F F T T F T F T F F F T T T T F S-2 t) (p -+ q)-+ p q p-+q q-+p (q -+ p) T T T T T T F F T T F T T F F F F T T T (p +-+ q) ffi (-,p +-+ q) (p ffi q) -+ (p ffi -,q) T T T F T F T T 31. (p -+ q)V (p -+ q)/\ (p +-+ q)V (-,p +-+ -,q) +-+ P q P -+-,q -,p +-+ q (-,p -+ q) (-,p -+ q) (-,p +-+ q) (p +-+ q) T T F F T T T T T F T T T F T T F T T T T T T T F F T F T F T T 33. (p -+ q)V (p -+ q)/\ (p +-+ q)V (-,p +-+ -,q) +-+ p q r p -+ (-,q V r) -,p -+ (q -+ r) (-,p -+ r) (-,p -+ r) (-,q +-+ r) (q +-+ r) T T T T T T T T T T T F F T T T T F T F T T T T F T T T F F T T T F F F F T T T T T T F F F T F T F T F T T F F T T T T T T F F F F T T T F T T S-3 35. (p +-+ q) +-+ p q r s p+-+q r +-+ s (r +-+ s) T T T T T T T T T T F T F F T T F T T F F T T F F T T T T F T T F T F T F T F F F T T F F T F F T T F F F F T F F T T T F T F F T T F F F T F T F T F F T F T F F F T F F F T T T T T F F T F T F F F F F T T F F F F F F T T T 37. a) Bitwise OR is III 1111; bitwise AND is 000 0000; bit­ wiseXOR is 1111111. b) Bitwise OR is 1111 1010; bitwise AND is 1010 0000; bitwise XOR is 0101 1010. c) Bitwise OR is 10 0111 1001; bitwise AND is 00 0100 0000; bitwise XOR is 100011 1001. d) Bitwise OR is 11 1111 1111; bit­ wise AND is 00 0000 0000; bitwise XOR is 11 1111 1111. 39. 0.2,0.6 41. 0.8,0.6 43. a) The 99th statement is true and the rest are false. b) Statements 1 through 50 are all true and statements 51 through 100 are all false. c) This cannot happen; it is a paradox, showing that these cannot be state­ ments. 45. "If I were to ask you whether the right branch leads to the ruins, would you answer yes?" 47. a) q -+- p b) q /\ ""p c) q -+- p d) ""q -+-""p 49. Not consistent 51. Consistent 53. NEW AND JERSEY AND BEACHES, (JERSEY AND BEACHES) NOT NEW 55. A is a knight and B is a knave. 57. A is a knight and B is a knight. 59. A is a knave and B is a knight. 61. In order of de­ creasing salary: Fred, Maggie, Janice 63. The detective can determine that the butler and cook are lying but cannot determine whether the gardener is telling the truth or whether the handyman is telling the truth. 65. The Japanese man owns the zebra, and the Norwegian drinks water. Section 1.2 1. The equivalences follow by showing that the appropriate pairs of columns of this table agree. P pl\T pVF pl\F pVT pVp pl\p T T T F T T T F F F F T F F 3. a) p q T T T F F T F F 5. P q r T T T T T F T F T T F F F T T F T F F F T F F F Answers to Odd-Numbered Exercises S-3 pVq qVp b) p q pl\q ql\p T T T T T T T T T F F F T T F T F F F F F F F F (p I\q)V q Vr .pl\(qVr) pl\q p I\r (p 1\ r) T T T T T T T T F T T T F T T F F F F F T F F F F T F F F F T F F F F F F F F F 7. a) Jan is not rich, or Jan is not happy. b) Carlos will not bicycle tomorrow, and Carlos will not run tomorrow. c) Mei does not walk to class, and Mei does not take the bus to class. d) Ibrahim is not smart, or Ibrahim is not hard working. 9. a) p q pl\q (p I\q) � P T T T T T F F T F T F T F F F T b) p q pVq P � (PVq) T T T T T F T T F T T T F F F T c) p q ""p p�q ...,p�(P �q) T T F T T T F F F T F T T T T F F T T T d) p q pl\q p�q (P 1\ q) � (P � q) T T T T T T F F F T F T F T T F F F T T S-4 Answers to Odd-Numbered Exercises e) p q p-q --.(p -q) --.(p -q) - P T T T F T T F F T T F T T F T F F T F T t) p q p- q --.(p -q) --.q --.(p - q) - --.q T T T F F T T F F T T T F T T F F T F F T F T T 11. In each case we will show that if the hypothesis is true, then the conclusion is also. a) If the hypothesis p /\ q is true, then by the definition of conjunction, the conclusion p must also be true. b) If the hypothesis p is true, by the definition of disjunction, the conclusion p V q is also true. c) If the hypothesis -'p is true, that is, if p is false, then the conclu­ sion p --+ q is true. d) If the hypothesis p /\ q is true, then both p and q are true, so the conclusion p --+ q is also true. e) If the hypothesis -.(p --+ q) is true, then p --+ q is false, so the conclusion p is true (and q is false). t) If the hypothesis ..... (p --+ q) is true, then p --+ q is false, so p is true and q is false. Hence, the conclusion -.q is true. 13. That the fourth column of the truth table shown is identical to the first column proves part (a), and that the sixth column is identical to the first column proves part (b). p q P /\q P V (p /\ q) pVq P /\ (p V q) r T T T T T r F F T T T F T F F T F F F F F F F 15. It is a tautology. 17. Each of these is true precisely when p and q have opposite truth values. 19. The proposition ..... p � q is true when -'p and q have the same truth val­ lIes, which means that p and q have different truth values. Similarly, p � -.q is true in. exactly the same cases. There­ fore, these two expressions are logically equivalent. 21. The proposition -'(p � q) is true when p � q is false, which means that p and q have different truth values. Because this is :>recisely when -'p � q is true, the two expressions are log­ icallyequivalent. 23. For (p --+ r) /\ (q --+ r) to be false, )ne of the two conditional statements must be false, which lappens exactly when r is false and at least one of p and 1 is true. But these are precisely the cases in which p v q IS true and r is false, which is precisely when (p v q) --+ r IS false. Because the two propositions are false in exactly he same situations, they are logically equivalent. 25. For :p --+ r) V (q --+ r) to be false, both of the two conditional ;tatements must be false, which happens exactly when r is false and both p and q are true. But this is precisely the case .n which p /\ q is true and r is false, which is precisely when S-4 (p /\ q) --+ r is false. Because the two propositions are false in exactly the same situations, they are logically equivalent. 27. This fact was observed in Section 1 when the biconditional was first defined. Each of these is true precisely when p and q have the same truth values. 29. The last column is all Ts. (p -q)/\ (p -q)/\ (q _r)_ p q r p-q q-r (q -r) p-r (p -r) T T T T T T T T T T F T F F F T T F T F T F T T T F F F T F F T F T T T T T T T F T F T F F T T F F T T T T T T F F F T T T T T 31. These are not logically equivalent because when p, q, and r are all false, (p --+ q) --+ r is false, but p --+ (q --+ r) is true. 33. Many answers are possible. Ifwe let r be true and p, q, and s be false, then (p --+ q) --+ (r --+ s) will be false, but (p --+ r) --+ (q --+ s) will be true. 35. a) p v -.q V-.r b)(p v q v r) /\s c)(p /\ T) v (q /\ F) 37. If we take duals twice, every v changes to an /\ and then back to an v, every /\ changes to an v and then back to an /\, every T changes to an F and then back to a T, every F changes to a T and then back to an F. Hence, (s*)* = s. 39. Let p and q be equivalent compound propositions involving only the operators /\, v, and -., and T and F. Note that -'p and-.q are also equivalent. Use De Morgan's laws as many times as necessary to push negations in as far as possible within these compound propositions, changing vs to /\s, and vice versa, and changing Ts to Fs, and vice versa. This shows that -'p and -.q are the same as p* and q* except that each atomic propo­ sition Pi within them is replaced by its negation. From this we can conclude that p* and q* are equivalent because -'p and -.q are. 41. (p /\ q /\ -.r) V (p /\ -.q /\ r) V (-'P /\ q /\ r) 43. Given a compound proposition p, form its truth table and then write down a proposition q in disjunctive nor­ mal form that is logically equivalent to p. Because q in­ volves only -., /\, and v, this shows that these three oper­ ators form a functionally complete set. 45. By Exercise 43, given a compound proposition p, we can write down a propo­ sition q that is logically equivalent to p and involves only -., /\, and v. By De Morgan's law we can eliminate all the /\'s by replacing each occurrence of PI /\ P2 /\ ... /\ Pn with -'(-'PI v -'P2 V ... v -'Pn). 47. -'(p /\ q) is true when ei­ ther p or q , or both, are false, andis false when both p and q are true. Because this was the definition of p I q, the two com­ pound propositions are logically equivalent. 49. -'(p v q) is true when both p and q are false, and is false other­ wise. Because this was the definition of p ..\, q, the two are logically equivalent. 51. «p ..\, p) ..\, q) ..\, «p ..\, p) ..\, q) 53. This follows immediately from the truth table or definition of p I q. 55. 16 57. If the database is open, then either the S-5 system is in its initial state or the monitor is put in a closed state. 59. All nine 61. To determine whether c is a tautol­ ogy apply an algorithm for satisfiability to -.c. If the algorithm says that -'c is satisfiable, then we report that c is not a tautol­ ogy, and if the algorithm says that -'c is not satisfiable, then we report that c is a tautology. Section 1.3 I. a) T b) T c) F 3. a) T b) F c) F d) F 5. a) There is a student who spends more than 5 hours ev­ ery weekday in class. b) Every student spends more than 5 hours every weekday in class. c) There is a student who does not spend more than 5 hours every weekday in class. d) No student spends more than 5 hours every weekday in class. 7. a) Every comedian is funny. b) Every person is a funny comedian. c) There exists a person such that if she or he is a comedian, then she or he is funny. d) Some comedians are funny. 9. a) 3x(P(x) /\ Q(x» b) 3x(P(x) /\ -.Q(x» c) '1x(P(x) v Q(x» d) '1x-.(P(x) v Q(x» 11. a) T b) T c) F d) F e) T 1) F 13. a) True b) True c) True d) True 15. a) True b) False c) True d) False 17. a) P(O) v pel) v P(2) v P(3) v P(4) b) P(O) /\ pel) /\ P(2) /\ P(3) /\ P(4) c) -.P(O) v -'P(l) v -'P(2) v -.P(3) v -.P(4) d) -'P(O) /\ -.P(l) /\ -'P(2) /\ -'P(3) /\ -.P(4) e) -.(P(O) v pel) v P(2) v P(3) v P(4» 1) -.(P(O) /\ pel) /\ P(2) /\ P(3) /\ P(4» 19. a) P(l) v P(2) v P(3) v P(4) v P(5) b) pel) /\ P(2) /\ P(3) /\ P(4) /\ P(5) c) -.(P(l) v P(2) v P(3) v P(4) v P(5» d) -.(P(l) /\ P(2) /\ P(3) /\ P(4) /\ P(5» e) (P(l) /\ P(2) /\ P(4) /\ P(5» v (-.P(l) v -.P(2) v -.P(3) v -'P(4) v -'P(5» 21. Many answers are possible. a) All students in your discrete mathematics class; all students in the world b) All United States senators; all college foot­ ball players c) George W. Bush and Jeb Bush; all politi­ cians in the United States d) Bill Clinton and George W. Bush; all politicians in the United States 23. Let C(x) be the propositional function "x is in your class." a) 3x H (x) and 3x(C(x) /\ H(x», where H(x) is "x can speak Hindi" b) '1xF(x) and '1x(C(x) -+ F(x», where F(x) is "x is friendly" c) 3x-.B(x) and 3x(C(x) /\ -.B(x», where B(x) is "x was born in California" d) 3xM(x) and 3x(C(x) /\ M(x», where M(x) is "x has been in a movie" e) '1x-.L(x) and'lx(C(x) -+ -.L(x», where L(x) is "x has taken a course in logic programming" 25. Let P(x) be "x is perfect"; let F(x) be "x is your friend"; and let the domain be all people. a) 'Ix -.P(x) b) -.'1x P(x) c) '1x(F(x) -+ P(x» d) 3x(F(x) /\ P(x» e) '1x(F(x) /\ P(x» or ('Ix F(x» /\ ('Ix P(x» 1) (-.'1x F(x» v (3x -.P(x» 27. Let Y(x) be the propositional function that x is in your school or class, as appropriate. a) Ifwe let Vex) be "x has lived in Vietnam," then we have 3x V (x) if the domain is just your schoolmates, or 3x(Y (x) /\ V (x» if the domain is all people. If we let D(x, y) mean that person x has lived in country y, then we can rewrite this last one as 3x(Y(x) /\ D(x, Vietnam». b) If we let H(x) be "x can speak Hindi," then we have 3x-.H(x) if the domain isjust your schoolmates, or 3x(Y(x) /\ -.H(x» Answers to Odd-Numbered Exercises S-5 if the domain is all people. If we let S(x, y) mean that per­ son x can speak language y, then we can rewrite this last one as 3x(Y(x) /\ -,S(x, Hindi». c) If we let lex), P(x), and C (x) be the propositional functions asserting x's knowl­ edge of Java, Prolog, and C++, respectively, then we have 3x(J(x) /\ P(x) /\ C(x» if the domain is just your school­ mates, or 3x(Y(x) /\ lex) /\ P(x) /\ C(x» if the domain is all people. If we let K (x, y) mean that person x knows pro­ gramming language y, then we can rewrite this last one as 3x(Y(x) /\ K(x, Java) /\ K(x, Prolog) /\ K(x, C++». d) Ifwe let T(x) be "x enjoys Thai food," then we have 'Ix T(x) if the domain is just your classmates, or '1x(Y (x) -+ T(x» if the domain is all people. If we let E(x,y) mean that person x enjoys food of type y, then we can rewrite this last one as '1x(Y(x) -+ E(x, Thai». e) If we let H(x) be "x plays hockey," then we have 3x -.H(x) if the domain is just your classmates, or 3x(Y(x) /\ -.H(x» if the domain is all people. If we let P (x, y) mean that person x plays game y, then we can rewrite this last one as 3x(Y(x) /\ -.P(x,hockey». 29. Let T(x) mean that x is a tautol­ ogy and C(x) mean that x is a contradiction. a) 3x T(x) b) '1x(C(x) -+ T(-.x» c) 3x3y(-.T(x) /\ -.C(x) /\ -'T(y) /\ -'C(y)/\T(x v y» d) '1x'ly«T(x) /\T(y» -+ T(x/\y» 31. a) Q(O,O,O) /\ Q(O, 1,0) b) Q(O, 1, 1) vQ(I, 1, 1) v Q(2, 1, 1) c) -.Q(O, 0, 0) v -.Q(O, 0, 1) d) -.Q(O, 0, 1) v -.Q(l, 0, 1) v -.Q(2,0, 1) 33. a) Let T(x) be the predicate that x can learn new tricks, and let the domain be old dogs. Original is 3x T(x). Negation is 'Ix -.T(x): "No old dogs can learn new tricks." b) Let C(x) be the predicate that x knows calculus, and let the domain be rabbits. Origi­ nal is -.3x C (x). Negation is 3x C (x): "There is a rabbit that knows calculus." c) Let F(x) be the predicate that x can fly, and let the domain be birds. Original is 'Ix F(x). Negation is 3x -.F(x): "There is a bird who cannot fly." d) Let T(x) be the predicate that x can talk, and let the do­ main be dogs. Original is -.3x T(x). Negation is 3x T(x): "There is a dog that talks." e) Let F(x) and R(x) be the predicates that x knows French and knows Russian, respec­ tively, and let the domain be people in this class. Original is -.3x(F(x) /\ R(x». Negation is 3x(F(x) /\ R(x»: "There is someone in this class who knows French and Russian." 35. a) There isno counterexamp le. b)x=O c)x=2 37. a) '1x«F(x, 25,000) v Sex, 25» -+ E(x», where E(x) is "Person x qualifies as an elite flyer in a given year," F (x, y) is "Person x flies more than y miles in a given year," and S(x, y) is "Person x takes more than y flights in a given year" b) '1x«(M(x) /\ T(x, 3» v (-.M(x) /\ T(x, 3.5») -+ Q(x», where Q(x) is "Person x qualifies for the marathon," M (x) is "Person x is a man," and T (x , y) is "Person x has run the marathon in less than y hours" c) M -+ «H(60) v (H(45) /\ T» /\ '1y G(B, y», where M is the proposition "The student received a masters degree," H(x) is "The stu­ dent took at least x course hours," T is the proposition "The student wrote a thesis," and G(x, y) is "The person got grade x or higher in course y" d) 3x «T(x, 21) /\ G(x, 4.0» , where T (x, y) is "Person x took more than y credit hours" and G(x, p) is "Person x earned grade point average p" (we assume that we are talking about one given semester) S-6 Answers to Odd-Numbered Exercises 39. a) If there is a printer that is both out of service and busy, then some job has been lost. b) If every printer is busy, then there is a job in the queue. c) If there is a job that is both queued and lost, then some printer is out of ser­ vice. d) If every printer is busy and every job is queued, then some job is lost. 41. a) (3x F(x, 10» --+ 3x S(x), where F (x, y) is "Disk x has more than y kilobytes of free space," and S(x) is "Mail message x can be saved" b) (3x A(x» --+ Vx(Q(x) --+ T(x», where A(x) is "Alert x is active," Q(x) is "Message x is queued," and T(x) is "Mes­ sage x is transmitted" c) Vx« x # main console) --+ T(x», where T(x) is "The diagnostic monitor tracks the status of system x" d) Vx( -.L(x) --+ B(x », where L(x) is "The host of the conference call put participant x on a special list" and B(x) is "Participant x was billed" 43. They are not equiva­ lent. Let P(x) be any propositional function that is sometimes true and sometimes false, and let Q(x) be any propositional function that is always false. Then Vx(P(x) --+ Q(x» is false but VxP(x) --+ VxQ(x) is true. 45. Both statements are true precisely when at least one of P(x) and Q(x) is true for at least one value of x in the domain. 47. a) If A is true, then both sides are logically equivalent to VxP(x). If A is false, the left-hand side is clearly false. Furthermore, for every x, P(x) /\ A is false, so the right-hand side is false. Hence, the two sides are logically equivalent. b) If A is true, then both sides are logically equivalent to 3x P(x). If A is false, the left-hand side is clearly false. Furthermore, for every x, P(x) /\ A is false, so 3x(P(x) /\ A) is false. Hence, the two sides are logically equivalent. 49. We can establish these equivalences by arguing that one side is true if and only if the other side is true. a) Suppose that A is true. Then for each x, P(x) --+ A is true; therefore the left-hand side is always true in this case. By similar reasoning the right-hand side is always true in this case. Therefore, the two propositions are logically equivalent when A is true. On the other hand, suppose that A is false. There are two subcases. If P(x) is false for every x, then P(x) --+ A is vacuously true, so the left-hand side is vacuously true. The same reasoning shows that the right-hand side is also true, because in this subcase 3xP(x) is false. For the second subcase, suppose that P(x) is true for some x. Then for that x, P(x) --+ A is false, so the left-hand side is false. The right-hand side is also false, because in this subcase 3xP(x) is true but A is false. Thus in all cases, the two propositions have the same truth value. b) If A is true, then both sides are trivially true, because the conditional statements have true conclusions. If A is false, then there are two subcases. If P(x) is false for some x, then P(x) --+ A is vacuously true for that x, so the left-hand side is true. The same reasoning shows that the right-hand side is true, because in this subcase VxP(x) is false. For the second subcase, suppose that P(x) is true for every x. Then for ev­ ery x, P(x) --+ A is false, so the left-hand side is false (there is no x making the conditional statement true). The right-hand side is also false, because it is a conditional statement with a true hypothesis and a false conclusion. Thus in all cases, the two propositions have the same truth value. 51. To show these are not logically equivalent, let P(x) be the statement "x is positive," and let Q(x) be the statement "x is negative" S-6 with domain the set of integers. Then 3x P(x) /\ 3x Q(x) is true, but 3x(P(x) /\ Q(x» is false. 53. a) True b) False, unless the domain consists of just one element c) True 55. a) Yes b) No c) juana, kiko d) math273, cs301 e) juana, kiko 57. sibling (X, Y) : -mother (M, X) , mother(M,Y), father (F,X), father(F,Y) 59. a) Vx(P(x) --+ -.Q(x» b) Vx(Q(x) --+ R(x» c) Vx(P(x) --+ -.R(x» d) The conclusion does not follow. There may be vain professors, because the premises do not rule out the possibility that there are other vain people besides ignorant ones. 61. a) Vx(P(x) --+ -.Q(x» b) Vx(R(x) --+ -.S(x» e) Vx(-.Q(x) --+S(x» d) Vx(P (x) --+ -.R(x» e) The conclusion follows. Suppose x is a baby. Then by the first premise, x is illogical, so by the third premise, x is despised. The second premise says that if x could manage a crocodile, then x would not be despised. Therefore, x cannot manage a crocodile. Section 1.4 1. a) For every real number x there exists a real number y such that x is less than y. b) For every real number x and real number y, if x and y are both nonnegative, then their product is nonnegative. e) For every real number x and real number y, there exists areal number z such thatxy = z. 3. a) There is some student in your class who has sent a message to some student in your class. b) There is some student in your class who has sent a message to every student in your class. e) Every student in your class has sent a message to at least one student in your class. d) There is a student in your class who has been sent a message by every student in your class. e) Every student in your class has been sent a message from at least one student in your class. 1) Every student in the class has sent a message to every student in the class. 5. a) Sarah Smith has visited www.att.com. b) At least one person has visited www.imdb.org. e) Jose Orez has visited at least one website. d) There is a website that both Ashok Puri and Cindy Yoon have visited. e) There is a person besides David Belcher who has visited all the websites that David Belcher has visited. 1) There are two different people who have vis­ ited exactly the same websites. 7. a) Abdallah Hussein does not like Japanese cuisine. b) Some student at your school likes Korean cuisine, and everyone at your school likes Mex­ ican CUlsme. e) There is some cuisine that either Monique Arsenault or Jay Johnson likes. d) For every pair of distinct students at your school, there is some cuisine that at least one them does not like. e) There are two students at your school who like exactly the same set of cuisines. 1) For every pair of students at your school, there is some cuisine about which they have the same opinion (either they both like it or they both do not like it). 9. a) VxL(x, Jerry) b) Vx3yL(x, y) e) 3yVxL(x, y) d) Vx3y-.L(x, y) e) 3x-.L(Lydia, x) 1) 3xVy-.L(y, x) g) 3x(VyL(y, x) /\ Vz«(VwL(w, z» --+ z = x» h) 3x3y(x # y /\ L(Lynn, x) /\ L(Lynn, y) /\ Vz(L(Lynn,z)--+(z=xvz=y» ) i)VxL(x,x) j)3xVy (L(x,y) �x = y) 11. a) A(Lois, Professor Michaels) S-7 b) Vx(S(x) � A(x, Professor Gross» c) Vx(F(x) � (A(x, Professor Miller) v A(Professor Miller, x») d) 3x(S(x) 1\ Vy(F(y) � -.A(x, y») e) 3x(F(x) 1\ Vy(S(y) � -.A(y,x») t) Vy(F(y) �3x(S(x) v A(x ,y») g) 3x(F(x) 1\ Vy«F(y) 1\ (y =Ix» � A(x,y») b) 3x(S(x) 1\ Vy(F(y) � -'A(y, x») 13. a) -.M (Chou, Koko) b) -.M(Arlene, Sarah) 1\ -.T(Arlene, Sarah) c) -.M (Deborah, Jose) d) VxM(x, Ken) e) Vx-.T(x, Nina) t) Vx(T -x, Avi) v M(x, Avi» g) 3xVy(y =I x � M(x, y» b) 3xVy(y =I x � (M(x, y) v T(x, y») i) 3x3y(x =I y 1\ M(x, y) 1\ M(y, x» j) 3xM(x, x) k) 3xVy(x =I y � (-.M(x, y) 1\ -.T(y, x») I) Vx(3y(x =I y 1\ (M(y, x) v T(y, x»» m) 3x3y(x =I y 1\ M(x, y) 1\ T(y, x» n) 3x3y(x =I y 1\ Vz«z=lx 1\ z=ly) � (M(x,z) v M(y,z) v T(x,z)v T(y, z»» 15. a) VxP(x), where P(x) is "x needs a course in discrete mathematics" and the domain consists of all com­ puter science students b) 3xP(x), where P(x) is "x owns a personal computer" and the domain consists of all students in this class c) Vx3yP(x,y), where P(x,y) is "x has taken y," the domain for x consists of all students in this class, and the domain for y consists of all computer science classes d) 3x3yP(x, y), where P(x, y) and domains are the same as in part (c) e) VxVyP(x, y), where P(x, y) is "x has been in y," the domain for x consists of all students in this class, and the domain for y consists of all buildings on campus t) 3x3yVz(P(z, y) � Q(x, z», where P(z, y) is "z is in y" and Q(x, z) is "x has been in z"; the domain for x consists of all students in the class, the domain for y consists of all build­ ings on campus, and the domain of z consists of all rooms. g) VxVy3z(P(z, y) 1\ Q(x, z», with same environment as in part (f) 17. a) Vu3m(A(u, m) 1\ Vn(n =I m � -.A(u, n»), where A(u, m) means that user u has access to mailbox m b) 3pVe(H(e) 1\ S(p, running» � S (kernel, working cor­ rectly), where H (e) means that error condition e is in effect and S(x, y)meansthatthestatusofx isy c) VuVs(E(s, .edu) � A( u, s», where E (s , x) means that website s has exten­ sion x, and A(u, s) means that user u can access website s d) 3x3y(x =I y 1\ Vz«Vs M(z,s» ++ (z = x V z = y»), where M(a, b) means that system a monitors remote server b 19. a) VxVy«x < 0) 1\ (y < 0) � (x + y < 0» b) -.VxVy «x > 0) 1\ (y > 0) � (x -y > 0» c) VxVy (x2 + y2 :::: (x + y)2) d) VxVy (Ixyl = Ix Ilyl) 21. Vx3a3b3c3d � > � � x � + � + 2 + d2), where the domain consists of all integers 23. a) Vx Vy «x < 0) 1\ (y < 0) � (xy > 0» b) Vx(x - x = 0) c) Vx3a3b(a =I b 1\ Vc(c2 = X ++ (c = a V c = b») d) Vx«x < 0) � -.3y(x = y2» 25. a) There is a multi­ plicative identity for the real numbers. b) The product of two negative real numbers is always a positive real number. c) There exist real numbers x and y such that x2 exceeds y but x is less than y. d) The real numbers are closed under the operation of addition. 27. a) True b) True c) True d) True e) True t) False g) False b) True i) False 29. a) P(I,I) 1\ P(l,2) 1\ P(l,3) 1\ P(2,1) 1\ P(2,2) 1\ P(2, 3) 1\ P(3, 1) 1\ P(3, 2) 1\ P(3, 3) b) P(I, 1) v P(I, 2) v P(I, 3) v P(2, 1) v P(2, 2) v P(2, 3) v P(3, 1) v P(3, 2) v P(3, 3) c) (P(I, 1) 1\ P(I, 2) 1\ P(I, 3» v (P(2, 1) 1\ P(2, 2) 1\ P(2, 3» v (P(3, 1) 1\ P(3, 2) 1\ P(3, 3» Answers to Odd-Numbered Exercises S-7 d)(P(I, I) v P(2, 1) v P(3, I» 1\ (P(I,2) v P(2, 2) v P(3, 2» 1\ (P(I, 3) v P(2, 3) v P(3, 3» 31. a) 3xVy3z-.T (x,y,z) b)3xVy-,P(x,Y)I\3xVy-.Q(x,y) c)3xVy (-.P(x, y) v Vz -.R(x, y, z» d) 3xVy(P(x, y) 1\ -.Q(x, y» 33. a) 3x3y-.P(x, y) b) 3yVx-.P(x, y) c) 3y3x(-.P(x, y) 1\ -.Q(x, y» d) (VxVyP(x, y» v (3x3y-.Q(x, y» e) 3x(Vy3z-.P(x,y,z) v Vz3y-.P(x, y, z» 35. Any domain with four or more members makes the statement true; any domain with three or fewer members makes the state­ ment false. 37. a) There is someone in this class such that for every two different math courses, these are not the two and only two math courses this person has taken. b) Every person has either visited Libya or has not vis­ ited a country other than Libya. c) Someone has climbed every mountain in the Himalayas. d) There is someone who has neither been in a movie with Kevin Bacon nor has been in a movie with someone who has been in a movie with Kevin Bacon. 39. a) x = 2, y = -2 b) x = -4 c) x = 17, y = -I 41. Vx VyVz«x·y)·z = X· (y ·z» 43. VmVb(m =I 0 � 3x(mx + b = 0 1\ Vw(mw + b = o � w = x») 45. a) True b) False c) True 47. -.(3xVyP(x,y» ++ Vx(-.VyP(x,y» ++ Vx3y-.P(x,y) 49. a) SupposethatVxP(x)1\3xQ(x) is true. Then P(x) is true for all x and there is an element y for which Q(y) is true. Because P(x) 1\ Q(y) is true for all x andthereisay for which Q(y) is true, Vx3y(P(x) 1\ Q(y» is true. Conversely, suppose that the second proposition is true. Let x be an element in the domain. There is a y such that Q(y) is true, so 3xQ(x) is true. Because VxP(x) is also true, it follows that the first proposition is true. b) Suppose that VxP(x) v 3xQ(x) is true. Then either P(x) is true for all x, or there exists a y for which Q(y) is true.In the former case, P(x) v Q(y) is true for all x, so Vx3y(P(x) v Q(y» is true. In the latter case, Q(y) is true for a particular y, so P(x) v Q(y) is true for all x and consequently Vx3y(P(x) v Q(y» is true. Conversely, suppose that the second proposition is true. If P(x) is true for all x, then the first proposition is true. Ifnot, P(x) is false for some x, and for this x there must be ay such that P(x) v Q(y) is true. Hence, Q(y) must be true, so 3yQ(y) is true. It fol­ lows that the first proposition must hold. 51. We will show how an expression can be put into prenex normal form (PNF) if subexpressions in it can be put into PNF. Then, working from the inside out, any expression can be put in PNF. (To formalize the argument, it is necessary to use the method of structural induction that will be discussed in Section 4.3.) By Exercise 45 of Section 1.2, we can assume that the proposition uses only v and -. as logical connectives. Now note that any proposition with no quantifiers is already in PNF. (This is the basis case of the argument.) Now suppose that the proposition is ofthe form QxP(x), where Q is a quantifier. Because P(x) is a shorter expression than the original proposition, we can put it into PNF. Then Q x followed by this PNF is again in PNF and is equivalent to the original proposition. Next, suppose that the proposition is ofthe form -.P. If P is already in PNF, we slide the negation sign past all the quantifiers using the equivalences in Table 2 in Section 1.3. Finally, assume that proposition is of the form P v Q, where each of P and Q is in PNF. If only one of P and Q has quantifiers, then we can 8-8 Answers to Odd-Numbered Exercises use Exercise 46 in Section 1.3 to bring the quantifier in front of both. Ifboth P and Q have quantifiers, we can use Exercise 45 in Section 1.3, Exercise 48, or part (b) of Exercise 49 to rewrite P v Q with two quantifiers preceding the disjunction of a proposition of the form R v S, and then put R v S into PNF. Section 1.5 1. Modus ponens; valid; the conclusion is true, because the hypotheses are true. 3. a) Addition b) Simplification c) Modus ponens d) Modus tollens e) Hypothetical syl­ logism 5. Let w be "Randy works hard," let d be "Randy is a dull boy," and let J be "Randy will get the job." The hypothe­ sesare w, w � d,andd � -.J. Using modus ponens and the first two hypothe ses, d follows. Using modus ponens and the last hypothesis, -'J, which is the desired conclusion, "Randy will not get the job," follows. 7. Universal instantiation is used to conclude that "If Socrates is a man, then Socrates is mortal." Modus ponens is then used to conclude that Socrates is mortal. 9. a) Valid conclusions are "I did not take Tuesday off," "I took Thursday off," "It rained on Thursday." b) "I did not eat spicy foods and it did not thunder" is a valid conclusion. c) "I am clever" is a valid conclusion. d) "Ralph is not a CS major" is a valid conclusion. e) "That you buy lots of stuff is good for the U.S. and is good for you" is a valid conclusion. 1) "Mice gnaw their food" and "Rabbits are not rodents" are valid conclusions. 11. Suppose that PI , P2, ... , Pn are true. We want to establish that q � r is true. If q is false, then we are done, vacuously. Otherwise, q is true, so by the validity of the given argument form (that whenever PI , P2, ... , Pn, q are true, then r must be true), we know that r is true. 13. a) Let c(x) be "x is in this class," J(x) be "x knows how to write pro­ grams in JAVA," and h(x) be "x can get a high-paying job." The premises are c(Doug), J(Doug), VxU(x) � h(x». Us­ ing universal instantiation and the last premise, J(Doug) � h(Doug) follows. Applying modus ponens to this conclusion and the second premise, h(Doug) follows. Using conjunc­ tion and the first premise, c(Doug) /\ h(Doug) follows. Fi­ nally, using existential generalization, the desired conclusion, 3x(c(x) /\ h(x» follows. b) Let c(x) be "x is in this class," w(x) be "x enjoys whale watching," and p(x) be "x cares about ocean pollution." The premises are 3x(c(x) /\ w(x» and Vx(w(x) � p(x». From the first premise, c(y) /\ w(y) for a particular person y. Using simplification, w(y) fol­ lows. Using the second premise and universal instantiation, w(y) � p(y) follows. Using modus ponens, p(y) follows, and by conjunction, c(y) /\ p(y) follows. Finally, by existen­ tial generalization, the desired conclusion, 3x(c(x) /\ p(x», follows. c) Let c(x) be "x is in this class," p(x) be "x owns a PC," and w(x) be "x can use a word-processing program." The premises are c(Zeke), Vx(c(x) � p(x», and Vx(P(x) � w(x». Using the second premise and univer­ sal instantiation, c(Zeke) � p(Zeke) follows. Using the first premise and modus ponens, p(Zeke) follows. Using the third S-8 premise and universal instantiation, p(Zeke) � w(Zeke) fol­ lows. Finally, using modus ponens, w(Zeke), the desired con­ clusion, follows. d) Let J(x) be "x is in New Jersey," !(x) be "x lives within 50 miles of the ocean," and s(x) be "x has seen the ocean." The premises are VxU(x) � !(x» and 3xU(x) /\ -.s(x». The second hypothesis and existential in­ stantiation imply that J(y) /\ -'s(y) for a particular person y. By simplification, J(y) for this person y. Using univer­ sal instantiation and the first premise, J(y) � !(Y), and by modus ponens, !(Y) follows. By simplification, -.s(y) fol­ lows from J(y) /\ -.s(y). So !(Y) /\ -.s(y) follows by con­ junction. Finally, the desired conclusion, 3x(f(x) /\ -.s(x», follows by existential generalization. 15. a) Correct, using universal instantiation and modus ponens b) Invalid; fallacy of affirming the conclusion c) Invalid; fallacy of denying the hypothesis d) Correct, using universal instantiation and modus tollens 17. We know that some x exists that makes H(x) true, but we cannot conclude that Lola is one such x. 19. a) Fallacy of affirming the conclusion b) Fallacy of beg­ ging the question c) Valid argument using modus tollens d) Fallacy of denying the hypothesis 21. By the second premise, there is some lion that does not drink coffee. Let Leo be such a creature. By simplification we know that Leo is a lion. By modus ponens we know from the first premise that Leo is fierce. Hence, Leo is fierce and does not drink cof­ fee. By the definition of the existential quantifier, there exist fierce creatures that do not drink coffee, that is, some fierce creatures do not drink coffee. 23. The error occurs in step (5), because we cannot assume, as is being done here, that the c that makes P true is the saine as the c that makes Q true. 25. We are given the premises Vx(P(x) � Q(x»and-.Q(a). We want to show -. P (a). Suppose, to the contrary, that -. P (a) is not true. Then P(a) is true. Therefore by universal modus ponens, we have Q(a). But this contradicts the given premise -. Q( a). Therefore our supposition must have been wrong, and so -. P (a) is true, as desired. 27. Step 1. Vx(P(x) /\ R(x» 2. P(a) /\ R(a) 3. P(a) 4. Vx(P(x) � " (Q(x) /\ S(x») 5. Q(a) /\ S(a) 6. S(a) 7. R(a) 8. R(a) /\ S(a) 9. Vx(R(x) /\ S(x» 29. Step 1. 3x-.P(x) 2. -.P(c) 3. Vx(P(x) v Q(x» 4. P(c) v Q(c) 5. Q(c) 6. Vx(-.Q(x) v S(x» Reason Premise Universal instantiation from (1 ) Simplification from (2) Premise Universal modus ponens from (3) and (4) Simplification from (5) Simplification from (2) Conjunction from (7) and (6) Universal generalization from (5) Reason Premise Existential instantiation from (1) Premise Universal instantiation from (3) Disjunctive syllogism from (4) and (2) Premise S-9 7. -.Q(c) v S(c) 8. S(c) 9. Vx(R(x) -+ -,S(x» 10. R(c) -+ -.S(c) II. -'R(c) 12.3x-.R(x) Universal instantiation from (6) Disjunctive syllogism from (5) and (7) Premise Universal instantiation from (9) Modus tollens from (8) and (10) Existential generalization from (11) 31. Let p be "It is raining"; let q be "Yvette has her umbrella"; let r be "Yvette gets wet." Assumptions are -. p v q, -.q v -.r, and p v -.r. Resolution on the first two gives -'p v -.r. Res­ olution on this and the third assumption gives -.r, as desired. 33. Assume that this proposition is satisfiable. Using resolu­ tion on the first two clauses enables us to conclude q v q; in other words, we know that q has to be true. Using resolution on the last two clauses enables us to conclude -.q v -'q; in other words, we know that -.q has to be true. This is a contradiction. So this proposition is not satisfiable. 35. Valid Section 1.6 1. Let n = 2k + I and m = 21 + I be odd integers. Then n + m = 2(k + 1 + I) is even. 3. Suppose that n is even. Then n = 2k for some integer k. Therefore, n2 = (2k)2 = 4k2 = 2(2k2). Because we have written n2 as 2 times an integer, we conclude that n2 is even. 5. Direct proof: Suppose that m + n and n + p are even. Then m + n = 2s for some integer s and n + p = 21 for some integer I. If we add these, we get m + p + 2n = 2s + 21. Subtracting 2n from both sides and factoring, we have m + p = 2s + 21 -2n = 2(s + 1 -n). Because we have written m + p as 2 times an in­ teger, we conclude that m + p is even. 7. Because n is odd, we can write n = 2k + I for some integer k. Then (k + 1)2 - � = � +2k+ 1-k2 = 2k+ 1= n. 9. Supposethatr is rational and i is irrational and s = r + i is rational. Then by Example 7, s + ( -r) = i is rational, which is a contradiction. 11. Because ,.fi . ,.fi = 2 is rational and ,.fi is irrational, the product of two irrational numbers is not necessarily irrational. 13. Proof by contraposition: If I/x were rational, then by def­ inition I/x = p/q for some integers p and q with q =1= O. Because I/x cannot be 0 (if it were, then we'd have the contradiction I = x . 0 by multiplying both sides by x), we know that p =1= O. Now x = I/(I/x) = I/(p/q) = q/p by the usual rules of algebra and arithmetic. Hence, x can be written as the quotient of two integers with the denomina­ tor nonzero. Thus by definition, x is rational. 15. Assume that it is not true that x :::: I or y :::: 1. Then x < I and y < 1. Adding these two inequalities, we obtain x + y < 2, which is the negation of x + y :::: 2. 17. a) Assume that n is odd, so n = 2k + I forsomeintegerk. Thenn3 + 5 = 2(4k3 + 6k2 + 3k + 3). Because n3 + 5 is two times some integer, it is even. b) Suppose that n3 + 5 is odd and n is odd. Because n is odd and the product of two odd numbers is odd, it follows that n2 is odd and then that n3 is odd. But then 5 = (n3 + 5) -n3 would have to be even because it is the difference of two odd numbers. Therefore, the supposition that n3 + 5 and n were both odd is wrong. 19. The proposition is vacuously true because 0 Answers to Odd-Numbered Exercises S-9 is not a positive integer. Vacuous proof. 21. P(I) is true because (a + b)' = a + b :::: a' + b' = a + b. Direct proof. 23. If we chose 9 or fewer days on each day of the week, this would account for at most 9 . 7 = 63 days. But we chose 64 days. This contradiction shows that at least 10 of the days we chose must be on the same day of the week. 25. Suppose by way of contradiction that a / b is a rational root, where a and b are integers and this fraction is in lowest terms (that is, a and b have no common divisor greater than I). Plug this proposed root into the equation to obtain a3/b3 + alb + I = O. Multi­ ply through by b3 to obtain a3 + ab2 + b3 = O. If a and b are both odd, then the left-hand side is the sum of three odd num­ bers and therefore must be odd. If a is odd and b is even, then the left-hand side is odd + even + even, which is again odd. Similarly, if a is even and b is odd, then the left-hand side is even + even + odd, which is again odd. Because the fraction a / b is in simplest terms, it cannot happen that both a and b are even. Thus in all cases, the left-hand side is odd, and therefore cannot equal O. This contradiction shows that no such root ex­ ists. 27. First, assume that n is odd, so that n = 2k + I for some integer k. Then 5n + 6 = 5(2k + I) + 6 = 10k + 11 = 2(5k + 5) + 1. Hence, 5n + 6 is odd. To prove the converse, suppose that n is even, so that n = 2k for some integer k. Then 5n + 6 = 10k + 6 = 2(5k + 3), so 5n + 6 is even. Hence, n is odd if and only if 5n + 6 is odd. 29. This proposition is true. Suppose that m is neither I nor -I. Then m n has a fac­ tor m larger than 1. On the other hand, m n = I, and I has no such factor. Hence, m = I or m = -I. In the first case n = I, and in the second case n = -I, because n = I/m. 31. We prove that all these are equivalent to x being even. If x is even, then x = 2k for some integer k. Therefore 3x + 2 = 3 . 2k + 2 = 6k + 2 = 2(3k + I), which is even, because it has been written in the form 21, where 1 = 3k + I. Similarly, x + 5 = 2k + 5 = 2k + 4 + I = 2(k + 2) + I, so x + 5 is odd; and x2 = (2k)2 = 2(2�), so x2 is even. For the converses, we will use a proof by contraposition. So as­ sume that x is not even; thus x is odd and we can write x = 2k + I for some integer k. Then 3x + 2 = 3(2k + I) + 2 = 6k + 5 = 2(3k + 2) + I, which is odd (i.e., not even), because it has been written in the form 21 + I, where 1 = 3k + 2. Similarly, x + 5 = 2k + I + 5 = 2(k + 3), so x + 5 is even (i.e., not odd). That x2 is odd was already proved in Exam­ ple 1. 33. We give proofs by contraposition of (i) -+ (ii), (ii) -+ (i), (i) -+ (iii), and (iii) -+ (i). For the first of these, suppose that 3x + 2 is rational, namely, equal to p / q for some integers p and q with q =1= O. Then we can write x = {(p/q) -2)/3 = (p -2q)/(3q), where 3q =1= O. This shows that x is rational. For the second conditional statement, sup­ pose that x is rational, namely, equal to p / q for some in­ tegers p and q with q =1= O. Then we can write 3x + 2 = (3p + 2q)/q, where q =1= O. This shows that 3x + 2 is ra­ tional. For the third conditional statement, suppose that x /2 is rational, namely, equal to p / q for some integers p and q with q =1= O. Then we can write x = 2p/q, where q =1= O. This shows that x is rational. And for the fourth conditional statement, suppose that x is rational, namely, equal to p / q for some integers p and q with q =1= O. Then we can. write x/2 = p/(2q), where 2q =1= O. This shows thatx/2 is rational. S-10 Answers to Odd-Numbered Exercises 35. No 37. Suppose that PI � P4 � P2 � Ps � P3 � PI. To prove that one of these propositions implies any of the others, just use hypothetical syllogism repeatedly. 39. We will give a proof by contradiction. Suppose that a I , a2, ... , an are all less than A, where A is the average of these num­ bers. Then a I + a2 + ... + an < n A. Dividing both sides by n shows that A = (al + a2 + ... + an)! n < A, which is a con­ tradiction. 41. We will show that the four statements are equivalent by showing that (i) implies (ii), (ii) implies (iii), (iii) implies (iv), and (iv) implies (i). First, assume that n is even. Then n = 2k for some integer k. Then n + 1 = 2k + 1, so n + 1 is odd. This shows that (i) implies (ii). Next, sup­ pose that n + 1 is odd, so n + 1 = 2k + 1 for some inte­ ger k. Then 3n + 1 = 2n +(n + 1) = 2(n +k)+ 1, which shows that 3n + 1 is odd, showing that (ii) implies (iii). Next, suppose that 3n + 1 is odd, so 3n + 1 = 2k + 1 for some integer k. Then 3n = (2k + 1) - 1 = 2k, so 3n is even. fhis shows that (iii) implies (iv). Finally, suppose that n is !lot even. Then n is odd, so n = 2k + 1 for some integer �. Then 3n = 3(2k + 1) = 6k + 3 = 2(3k + 1) + 1, so 3n is :>dd. This completes a proof by contraposition that (iv) im­ :>lies (i). Section 1.7 l. 12 + 1 = 2 � 2 = 2 1; 22 + 1 = 5 � 4 = 2 2; 32 + 1 = lO�8=23; 42+1=17�16=24 3. If x :::::y, then nax(x,y) + min(x,y) = y +x = x + y. If x � y, then nax(x, y) + min(x, y) = x + y. Because these are the only wo cases, the equality always holds. 5. There are four cases. ':ase 1: x � 0 andy � O. Then Ixl + Iyl = x + y = Ix + YI. ':ase 2: x < 0 and y < O. Then Ixl + Iyl = -x + (-y) = -(x + y) = Ix + yl because x + y < O. Case 3: x � 0 md y < O. Then Ixl + Iyl = x + (-y). If x � -y, then x + yl = x + y. But because y < 0, -y > y, so Ixl + Iyl = r+(-y»x+y=lx+yl. If x<-y, then Ix+yl = -(x + y) = -x + (-y). But because x � 0, x � -x, so xl + Iyl =X +(-y)� -x + (-y) = Ix + YI. Case 4: x < 0 md y � O. Identical to Case 3 with the roles of x and y re­ {ersed. 7. 10,001, 10,002, ... , 10,100 are all nonsquares, )ecause 1002 = 10,000 and 1012 = 10,201; constructive. �. 8 = 23 and 9 = 32 11. Let x = 2 and y =../2. If ,Y = 2v'2isirrational,wearedone.lfnot,thenletx = 2v'2and v = ../2/4. Then xY = (2v'2)v'2/4 = 2v'2·(v'2}/4 = 21/2 = ../2. l3. a) This statement asserts the existence of x with a certain )roperty. Ifwe let y = x, then we see that P(x) is true. If y s anything other than x, then P(x) is not true. Thus, x is the mique element that makes P true. b) The first clause here .ays that there is an element that makes P true. The second :lause says that whenever two elements both make P true, hey are in fact the same element. Together these say that P is • atisfied by exactly one element. c) This statement asserts he existence of an x that makes P true and has the further )roperty that whenever we find an element that makes P true, hat element is x. In other words, x is the unique element hat makes P true. 15. The equation la -cl = Ib -cl is :quivalent to the disjunction of two equations: a - c = b -c SolO or a - c = -b + c. The first of these is equivalent to a = b, which contradicts the assumptions made in this problem, so the original equation is equivalent to a - c = -b + c. By adding b + c to both sides and dividing by 2, we see that this equation is equivalent to c = (a + b)/2. Thus, there is a unique solution. Furthermore, this c is an integer, because the sum of the odd integers a and b is even. 17. We are being asked to solve n = (k -2) + (k + 3) for k. Using the usual, reversible, rules of algebra, we see that this equation is equivalent to k = (n -1)/2. In other words, this is the one and only value of k that makes our equation true. Because n is odd, n - 1 is even, so k is an integer. 19. If x is itself an integer, then we can take n = x and E = O. No other solution is possible in this case, because if the integer n is greater than x, then n is at least x + 1, which would make E � 1. If x is not an integer, then round it up to the next integer, and call that integer n. Let E = n -x. Clearly 0 ::::: E < 1; this is the only E that will work with this n, and n cannot be any larger, because E is constrained to be less than 1. 21. The harmonic mean of distinct positive real numbers x and y is always less than their geometric mean. To prove 2xy/(x + y) < ,JXY,multiplyboth sides by (x + y)/(2.JXY) to obtain the equivalent inequality ,JXY < (x + y)/2, which is proved in Example 14. 23. The parity (oddness or evenness) of the sum of the numbers written on the board never changes, because j + k and U -kl have the same parity (and at each step we reduce the sum by j + k but increase it by U -kl). Therefore the integer at the end of the process must have the same parity as 1 + 2 + ... + (2n) = n(2n + 1), which is odd because n is odd. 25. Without loss of generality we can assume that n is nonnegative, because the fourth power of an integer and the fourth power of its negative are the same. We divide an arbitrary positive integer n by 10, obtaining a quotient k and remainder I, whence n = 10k + I, and I is an integer between 0 and 9, inclusive. Then we compute n4 in each of these 10 cases. We get the following values, where X is some integer that is a multiple of 10, whose exact value we do not care about. (10k + 0)4 = 1O,000k4 = 1O,000k4 + 0, (10k + 1)4 = 1O,000k4 + X ·k3 + X·� + X·k + 1, (10k + 2)4 = 1O,000k4 + X . k3 + X . k2 + X . k + 16, (10k + 3)4 = 1O,000k4 + X . k3 + X . k2 + X . k + 81, (10k + 4)4 = 1O,000k4 + X . k3 + X . k2 + X . k + 256, (10k + 5)4 = IO,OOOk4 + X . k3 + X . k2 + X . k + 625, (10k + 6)4 = 1O,000k4 + X . k3 + :x . k2 + X . k + 1296, (10k + 7)4 = 1O,000k4 + X ·k3 + X ·k2 + X· k + 2401, (10k + 8)4 = 1O,000k4 + X ·k3 + X·� + X· k + 4096, (10k + 9)4 = IO,OOOk4 + X . k3 + X . � + X . k + 6561. Because each coefficient indicated by X is a multiple of 10, the corresponding term has no effect on the ones digit of the answer. Therefore the ones digits are 0, 1, 6, 1, 6, 5, 6, 1, 6, 1, respectively, so it is always a 0, 1, 5, or 6. 27. Because n3 > 100 for all n > 4, we need only note that n = l,n = 2,n = 3,andn = 4 do not satisfyn2 +n3 = 100 . 29. Because 54 = 625, both x and y must be less than 5. Then x4 + y4 ::::: 44 + 44 = 512 < 625. 31. If it is not true that a ::::: 4'n, b ::::: 4'n, or c ::::: 4'n, then a > 4'n, b > 4'n, and c > 4'n. Multiplying these inequalities of positive num­ bers together we obtain abc < (4'n)3 = n, which implies the S-ll negation of our hypothesis that n = abc. 33. By finding a common denominator, we can assume that the given ra­ tional numbers are alb and c/b, where b is a positive in­ teger and a and c are integers with a < c. In particular, (a + 1)/b:::: c/b. Thus,x = (a + �J2)/bisbetweenthetwo given rational numbers, because 0 < J2 < 2. Furthermore, x is irrational, because if x were rational, then 2( bx - a) = J2 would be as well, in violation of Example 10 in Section 1.6. 35. a) Without loss of generality, we can assume that the x sequence is already sorted into nondecreasing order, because we can relabel the indices. There are only a finite number of possible orderings for the Y sequence, so if we can show that we can increase the sum (or at least keep it the same) whenever we find Yi and Yj that are out of order (i.e., i < j but Yi > Yj) by switching them, then we will have shown that the sum is largest when the Y sequence is in nonde­ creasing order. Indeed, if we perform the swap, then we have added XiYj +XjYi to the sum and subtracted XiYi +XjYj· The net effect is to have added XiYj + XjYi - XiYi -XjYj = (Xj - Xi)(Yi -Yj), which is nonnegative by our ordering as­ sumptions. b) Similar to part (a) 37. a) 6 � 3 � 10 � 5 � 16 � 8 � 4 � 2 � 1 b) 7 � 22 � 11 � 34 � 17 � 52 � 26 � 13 � 40 � 20 � 10 � 5 � 16 � 8 � 4 � 2 � 1 c) 17 � 52 � 26 � 13 � 40 � 20 � 10 � 5 � 16 � 8 � 4 � 2 � 1 d) 21 � 64 � 32 � 16 � 8 � 4 � 2 � 1 39. Without loss of gener­ ality, assume that the upper left and upper right comers of the board are removed. Place three dominoes horizontally to fill the remaining portion of the first row, and fill each of the other seven rows with four horizontal dominoes. 41. Because there is an even number of squares in all, either there is an even number of squares in each row or there is an even num­ ber of squares in each column. In the former case, tile the board in the obvious way by placing the dominoes horizon­ tally, and in the latter case, tile the board in the obvious way by placing the dominoes vertically. 43. We can rotate the board if necessary to make the removed squares be 1 and 16. Square 2 must be covered by a domino. If that domino is placed to cover squares 2 and 6, then the following domino placements are forced in succession: 5-9, 13-14, and 10-11, at which point there is no way to cover square 15. Otherwise, square 2 must be covered by a domino placed at 2-3. Then the following domino placements are forced: 4-8, 11-12, 6-7, 5-9, and 10-14, and again there is no way to cover square 15. 45. Remove the two black squares adjacent to a white comer, and remove two white squares other than that comer. Then no domino can cover that white comer. 47. a) (1) EE (2) (3) (4) (5) �qp% Answers to Odd-Numbered Exercises S-l1 b) The picture shows tilings for the first four patterns. 3 1-t=r=+lR=-l-I+=-+I=!=-j To show that pattern 5 cannot tile the checkerboard, label the squares from 1 to 64, one row at a time from the top, from left to right in each row. Thus, square 1 is the upper left comer, and square 64 is the lower right. Suppose we did have a tiling. By symmetry and without loss of generality, we may suppose that the tile is positioned in the upper left comer, covering squares 1, 2, 10, and 11. This forces a tile to be adjacent to it on the right, covering squares 3, 4, 12, and 13. Continue in this manner and we are forced to have a tile covering squares 6,7, 15, and 16. This makes it impossible to cover square 8. Thus, no tiling is possible. Supplement ary Exercises 1. a) q � p b) q 1\ P c) -.q v -'p d) q B-P 3. a) The proposition cannot be false unless -'p is false, so p is true. If p is true and q is true, then -.q 1\ (p � q) is false, so the conditional statement is true. If p is true and q is false, then p � q is false, so -.q 1\ (p � q) is false and the conditional statement is true. b) The proposition cannot be false unless q is false. If q is false and p is true, then (p v q) 1\ -'p is false, and the conditional statement is true. If q is false and p is false, then (p v q) 1\ -'p is false, and the conditional state­ ment is true. 5. -.q � -'p; p � q; -.p � -.q 7. (p 1\ q 1\ r 1\ -'s) v (p 1\ q 1\ -'r 1\ s) v (p 1\ -.q 1\ r 1\ s) v (-'P 1\ q 1\ r 1\ s) 9. Translating these statements into symbols, using the obvious letters, we have -.[ � -'g, -.g � -'q, r � q, and -.[ 1\ r. Assume the statements are consistent. The fourth statement tells us that -.[ must be true. Therefore by modus ponens with the first statement, we know that -.g is true, hence (from the second state­ ment), that -.q is true. Also, the fourth statement tells us that r must be true, and so again modus ponens (third state­ ment) makes q true. This is a contradiction: q 1\ -'q. Thus the statements are inconsistent. 11. Brenda 13. The premises cannot both be true, because they are contradictory. There­ fore it is (vacuously) true that whenever all the premises are true, the conclusion is also true, which by definition makes this a valid argument. Because the premises are not both true, we cannot conclude that the conclusion is true. 15. a) F b) T c) F d) T e) F t) T 17. Many an­ swers are possible. One example is United States senators. S-12 Answers to Odd-Numbered Exercises 19.'v'x3y3z(y =1= ZI\'v'w(P(w,x) � (w = yvw = z») 21. a) -.3xP(x) b) 3x(P(x) 1\ 'v'y(P(y) � y = x» c) 3x13x2(P(xd 1\ P(X2) 1\ XI =1= X2 1\ 'v'y(P(y) � (y = XI vy = X2») d) 3xI 3X2 3X3(P(XI)I\P(X2) I\P(X3)I\XI =1= X2 1\ XI =1= X3 1\ X2 =1= X3 1\ 'v'y(P(y) � (y = XI V Y = X2 V Y = X3») 23. Suppose that 3x(P(x) � Q(x» is true. Then either Q(xo) is true for some xo, in which case 'v'xP(x) � 3x Q(x) is true; or P(xo) is false for some xo, in which case 'v'xP(x) � 3xQ(x) is true. Con­ versely, suppose that 3x(P(x) � Q(x» is false. That means that 'v'x(P(x) 1\ -.Q(x» is true, which implies 'v'xP(x) and 'v'x(-.Q(x». This latter proposition is equivalent to -.3xQ(x). Thus, 'v'xP(x) � 3xQ(x) is false. 25. No 27.'v'x'v'z3yT(x,y,z), where T(x,y,z) is the statement that student X has taken class y in department z, where the domains are the set of students in the class, the set of courses at this university, and the set of departments in the school of mathematical sciences 29. 3!x3!y T(x, y) and 3x'v'z«3y'v'w (T(z, w) � w = y» � z = x), where T(x, y) means that student X has taken class y and the domain is all students in this class 31. P(a) � Q(a) and Q(a) � R(a) by universal instantiation; then -.Q(a) by modus tollens and ..... P(a) by modus tollens 33. We give a proof by contra­ position and show that if "fi is rational, then X is rational, assuming throughout that X � 0. Suppose that "fi = p/q is rational, q =1= 0. Then X = ("fi)2 = p2/q2 is also rational (q2 is again nonzero). 35. We can give a constructive proof by lettingm = 10500 + 1. Thenm2 = (10500 + 1)2 > (10500)2 = 101000. 37.23 cannot be written as the sum of eight cubes. 39. 223 cannot be written as the sum of 36 fifth powers. CHAPTER 2 Section 2.1 I.a){-I,I} b){I,2,3,4,5,6,7,8,9,1O,1l} c){0,1,4,9,16, l5, 36, 49, 64, 81} d) 0 3. a) Yes b) No c) No 5. a) Yes b) No c) Yes d) No e) No t) No 7. a) False b) False c) False d) True e) False t) False �) True 9. a) True b) True c) False d) True �) True t) False II. January March February April October December 13. The dots in certain regions indicate that those regions are lot empty. S-/2 u IS. Suppose that x E A. Because A � B, this implies that x E B. Because B � C, we see that x E C. Because x E A implies that x E C, it follows that A � C. 17. a) I b)1 c)2 d)3 19.a){0,{a}} b){0,{a},{b},{a,b}} c) {0, {0}, {{0}}, (0, {0}}} 21. a) 8 b) 16 c) 2 23. a) {(a, y), (b, y), (e, y), (d, y), (a, z), (b, z), (e, z), (d, z)} b) {(y, a), (y, b), (y, c), (y, d), (z, a), (z, b), (z, c), (z, d)} 25. The set of triples (a, b, c), where a is an airline and band e are cities 27.0xA={(x,y)lxE0 and yE A} = 0 = { (x, y) I x E A and y E 0} = A x 0 29. mn 31. The elements of A x B x C consist of3-tuples (a, b, c), where a E A, b E B, and e E C, whereas the elements of (A x B) x C look like «a, b), e}-ordered pairs, the first coordinate of which is again an ordered pair. 33. a) The square of a real number is never -I. True b) There exists an integer whose square is 2. False c) The square of every integer is positive. False d) There is a real number equal to its own square. True 35. a) {-I, 0, I} b) Z -to, I} c) 0 37. We must show that {{a}, {a, b}} = {{e}, {e, d}} if and only if a = e and b = d. The "if" part is immediate. So assume these two sets are equal. First, consider the case when a =1= b. Then {{a}, {a, b}} contains exactly two elements, one of which contains one element. Thus, {{e}, {e, d}} must have the same property, so e =1= d and {e} is the element containing exactly one element. Hence, {a} == Ie}, which implies that a = e. Also, the two-element sets {a, b} and {e, d} must be equal. Because a = e and a =1= b, it follows that b = d. Sec­ ond, suppose that a = b. Then {{a}, {a, b}} = {{a}}, a set with one element. Hence, {{ e }, {e, d}} has only one element, which can happen only when e = d, and the set is {{ell. It then fol­ lows that a = e and b = d. 39. Let S = {aI, a2, ... , an}. Represent each subset of S with a bit string oflength n, where the ith bit is I if and only if aj E S. To generate all subsets of S, list a1l2n bit strings oflength n (for instance, in increasing order), and write down the corresponding subsets. Section 2.2 I. a) The set of students who live within one mile of sc:hool and who walk to classes b) The set of students who live within one mile of school or who walk to classes (or who do both) c) The set of students who live within one mile of school but do not walk to classes d) The set of students who walk to classes but live more than one mile away from school 3. a){O, 1,2,3,4,5,6} b) {3} c){l, 2, 4,5} d) to, 6} 5. A = (x I -'(x E A)} = (x I-.(-.x E A)} = {x I x E A} = A 7. a) A U U = {x I x E A v x E U} = {x I x E A v T} = {x I T} = U b) A n 0 = {x I x E A 1\ x E 0} = {x I x E S-/3 A 1\ F} = {x I F} = 0 9. a) A U A = {x I x E A v x ¢ A} = U b) A n A = {x I x E A 1\ x ¢ A} = 0 11. a) AU B = {x I x E A v x E B} = {x I x E B v x E A} = B U A b) A n B = {XIXEA I\xEB}={xlxEBI\XEA}= B n A 13. Suppose x E A n (A U B). Then x E A and x E A U B by the definition of intersection. Because x E A, we have proved that the left-hand side is a subset of the right-hand side. Conversely, let x E A. Then by the defini­ tion of union, x E A U B as well. Therefore x E A n (A U B) by the definition of intersection, so the right-hand side is a subset of the left-hand side. 15. a) x E (A U B) := x ¢ (A U B) := ..... (x E A v x E B) := ..... (x E A) 1\ ..... (x E B) := x¢Al\x¢B:=X EAI\XEIi:= XEAnIi b) A B AUB (AUB) A B AnB I I I 0 0 0 0 I 0 I 0 0 I 0 0 1 1 0 1 0 0 0 0 0 1 1 1 1 17. a)xEAnBne:=x¢AnBne:=x¢Avx¢Bv x¢e:=xEAvXEBvx EC:=XEAUIiuc b) A B C AnBnC (AnBnC) A B C AUBUC 1 1 I 1 0 0 0 0 0 I 1 0 0 I 0 0 1 1 I 0 I 0 I 0 I 0 1 I 0 0 0 1 0 1 1 1 0 I 1 0 1 1 0 0 I 0 I 0 0 1 1 0 I I 0 0 1 0 1 1 1 0 I 0 0 0 0 1 1 I 1 1 19. Both sides equal {x I x E A 1\ X ¢ B}. 21. x E AU (B U e):= (x E A) v (x E (B U e» := (x E A) v (x E B v X E e) := (x E A v X E B) v (x E e) := X E (A U B) U e 23. x E A U (B n e) := (x E A) v (x E (B n e» := (x E A) v (x E B 1\ x E e) := (x E A v x E B) 1\ (x E A v x E e) := x E (A U B) n (A U e) 25. a){4,6} b) {0,1,2,3,4,5,6,7,8,9,1O} c) {4, 5, 6, 8, 10} d) {0,2,4, 5, 6,7,8,9,1O} 27. a) The double-shaded portion is the desired set. b) The desired set is the entire shaded portion. Answers to Odd-Numbered Exercises S-13 c) The desired set is the entire shaded portion. 29. a) B � A b) A � B c) A n B = 0 d) Nothing, be­ cause this is always true e) A = B 31. A � B := Vx(x E A � x E B) := Vx(x ¢ B � x ¢ A) := Vx(x E Ii � x E A) := Ii � If 33. The ·set of students who are computer science majors but not mathematics majors or who are math­ ematics majors but not computer science majors 35. An element is in (A U B) - (A n B) if it is in the union of A and B but not in the intersection of A and B, which means that it is in either A or B but not in both A and B. This is exactly what it means for an element to belong to A E9 B. 37. a) A E9 A = (A -A) U (A -A) = 0 U 0 = 0 b) A E9 o = (A -0) U (0 - A) = AU 0 = A c) A E9 U = (A -U) U (U - A) = 0 U A = A d) A E9 A = (A - A) U (A - A) = A U A = U 39. B = 0 41. Yes 43. Yes 45.a) {1,2,3, ... ,n} b){l} 47. a) An b){O,I} 49.a)Z,{-1,0,1} b)Z-{0}, 0 c)R,[-I,I] d) [1, 00), 0 51. a) {l, 2, 3, 4,7,8,9, 1O} b) {2, 4,5,6, 7} c) {I, 1O} 53. The bit in the ith position of the bit string of the difference of two sets is ·1 if the ith bit of the first string is I and the ith bit of the second string is 0, and is 0 otherwise. 55. a) 11 1110 0000 0000 0000 0000 0000 v 0 I 1100 1000 0000 01 00 0101 0000 = 11 1110 1000 0000 01 00 0101 0000, representing {a, b, c, d, e, g, p, I, v} b) 11 1110 0000 0000 0000 0000 0000 1\ 01 11 00 1000 0000 0100 0101 0000 = 01 1100 0000 0000 0000 0000 0000, representing {b, c, d} c)(11 11100000 0000 0000 0000 0000 v 00 0110 0110 0001 100001100110) 1\ (01 1100 1000000001000101 0000 v 00 101000100000 100000100111) = 11 1110 0110 0001 1000 OlIO OlIO 1\ 01 1110 1010 0000 1100 0111 0111 = 01 1110 0010 0000 1000 0110 0110, repre­ senting {b,c,d,e,i,o,l,u,x,y} d) 11 1110 0000 0000 0000 0000 0000 v 01 1100 1000 0000 0100 0101 0000 v 00 1010 0010 0000 1000 0010 0111 v 00 0110 0110 0001 1000 0110 0110 = 11 1110 1110 0001 1100 0111 0111, representing {a,b,c,d,e,g,h,i ,n,o,p,1 ,u, v,x ,y,z} 57. a){l, 2, 3, {l, 2, 3}} b){0} c){0, {0}} d){0, {0}, {0, {0}}} 59. a){3 . a, 3 . b, 1 . c, 4 · d} b){2 . a, 2· b} c) {I . a, 1 . c} d) {I . b, 4· d} e) {5 . a, 5· b, 1· c, 4 · d} 61. If = {OA Alice, 0.1 Brian, 0.6 Fred, 0.9 Oscar, 0.5 Rita}, Ii = {0.6 Alice, 0.2 Brian, 0.8 Fred, 0.1 Oscar, 0.3 Rita} 63. F n R = {OA Alice, 0.8 Brian, 0.2 Fred, 0.1 Oscar, 0.5 Rita} Section 2.3 1. a) f(O) is not defined. b) f(x) is not defined for x < o. c) f(x) is not well-defined because there are two S-14 Answers to Odd-Numbered Exercises distinct values assigned to each x. 3. a) Not a func­ tion b) A function e) Not a function 5. a) Domain the set of bit strings; range the set of integers b) Domain the set of bit strings; range the set of even nonnegative integers e) Domain the set of bit strings; range the set of nonnegative integers not exceeding 7 d) Domain the set of positive integers; range the set of squares of pos­ itive integers={1.4.9.16 .... } 7.a)Domain Z+xZ+; range Z+ b) Domain Z+; range to. 1.2.3.4.5.6.7.8. 9} e) Domain the set of bit strings; range N d) Domain the set of bit strings; range N 9. a) I b) 0 e) 0 d) -I e) 3 t) -I g) 2 b) I 11. Only the function in part (a) 13. Only the functions in parts (a) and (d) 15. a) Onto b) Not onto e) Onto d) Not onto e) Onto 17. a) The function f(x) with f(x) = 3x + I whenx � o and f(x)=-3x+2 when x<O b)f(x)=lxl+1 e) The function f(x) with f(x) = 2x + 1 when x � 0 and f(x) = -2x when x < 0 d) f(x) = x2 + I 19. a) Yes b) No e) Yes d) No 21. Suppose that f is strictly decreasing. This means that f(x) > f(y) whenever x < y. To show that g is strictly increasing, suppose that x < y. Then g(x) = I/f(x) < I/f(y) = g(y). Conversely, suppose thatg is strictly increasing. This means that g(x) < g(y) whenever x < y. To show that f is strictly decreasing, suppose that x < y. Then f(x) = I/g(x) > I/g(y) = f(y). 23. Many answers are possible. One example is f(x) = 17. 25. The function is not one-to-one, so it is not invertible. On the restricted domain, the function is the identity function on the nonnegative real numbers, f(x) = x, so it is its own inverse. 27. a) f(S) = to. I, 3} b) f(S) = to. 1,3. 5.8} e) f(S) = to, 8. 16. 40} d) f(S) = {I. 12,33. 65} 29. a) Let x and y be distinct elements of A. Because g is one­ to-one, g(x) and g(y) are distinct elements of B. Because f is one-to-one, f(g(x» = (f 0 g)(x) and f(g(y» = (f 0 g)(y) are distinct elements of C. Hence, fog is one-to-one. b) Let y E C. Because f is onto, y = f(b) for some b E B. Nowbecausegisonto,b = g(x) for some x E A.Hence,y = f(b) = f(g(x» = (f 0 g)(x). It follows that fog is onto. 31. No. For example, suppose that A = {a}, B = {b, c}, and C = {d}.Letg(a) = b. f(b) = d,andf(c) = d. Then f and fog are onto, but g is not. 33. (f + g)(x) = x2 + X + 3, (fg)(x) = x3 + 2x2 + X + 2 35. f is one-to-one because f(Xl) = f(X2) -+ aXI + b =ax2 + b -+ aXI = aX2 -+ Xl = X2. f is onto because f«y -b)/a) = y.f-l(y) = (y -b)/a. 37. Let f(l) = a. f(2) = a. Let S = {I} and T = {2}. Then f(S n T) = f(0) = 0, but f(S) n f(T) = {a} n {a} = {a}. 39.a){xIO.:sx<I} b){xl-I.:sx<2} e)0 41. f-I(S) = {x E A I f(x) ¢ S} = {x E A I f(x) E S} = f-I(S) 43. Let x = LxJ + E, where E is a real number with 0 .:s E < 1. If E < !, then LxJ - 1 < x -! < LxJ, so rx -! 1 = LxJ and this is the integer closest to x. If E > !, then LxJ < x -! < LxJ + I,so rx -!l = LxJ + 1 and this is the integer closestto x. If E = !, then r x -! 1 = Lx J, which is the smaller of the two integers that surround x and are the same distance from x. 45. Write the real number x as LxJ + E, where E is a real number with 0 .:s E < 1. Because E = X -LxJ, it follows that O.:s -LxJ < 1. The first two in-S-/4 equalities, x - I < LxJ and LxJ .:s x, follow directly. For the other two inequalities, write x = r xl -E', where 0 .:s E' < 1. Then 0 .:s r xl -x < I, and the desired inequality follows. 47. a) If x < n, because LxJ .:s x, it follows that LxJ < n. Suppose that x � n. By the definition of the floor function, it followsthatLxJ �n.ThismeansthatifLxJ <n,thenx <no b) If n < x, then because x .:s rxl, it follows that n .:s rxl Suppose that n � x. By the definition of the ceiling func­ tion, it follows that r xl .:s n. This means that if n < r xl, then n < x. 49. If n is even, then n = 2k for some integer k. Thus, Ln/2J = LkJ = k = n/2. If n is odd, then n = 2k + I for some integer k. Thus, Ln/2J = Lk + !J = k = (n -1)/2. 51. Assume that x � O. The left-hand side is r -xl and the right-hand side is -LxJ. Ifx is an integer, then both sides equal '-x. Otherwise, let x = n + E, where n is a natural number and Eis a real number with 0 .:s E < 1. Then r -xl = r -n - El = -n and-LxJ = -Ln + EJ = -n also. When x < 0, the equation also holds because it can be obtained by substitut­ ing -x forx. 53. rbl - LaJ - I 55. a) 1 b) 3 e) 126 d) 3600 57. a) 100 b) 256 e) 1030 d) 30,200 59. 4 2 e-o I e-o -2 -I 0 I 2 3 4 -I e-o -2 63. a) 11 --0 \, 2 --0 I --0 I I I j j I I I I -4 -2--0 _I 2 4 --0 -2 --0 -3 e)� -2 a--I I I I -'H-U j_1 ' e) a-- -2 a-- -3 4 9 I 12 3 0----0 -2 -I -I -2 -3 -4 61. -2 -I 0 2 b) d) -I -2 ---0 -3 �L--o--O 1"1'''! --I I --0 -I --0 -2 --0 -3 4 .., 3 -.!......--I -2 .., -3 t) ...., 4 3 ...., -2 -I '1_1 I -2 -3 -4 g) See part (a). 65. f-I(y) = (y -1)1/3 67. a) /AnB(X) = I � x E An B � x E A and x E B � /A(x) = I and S-/5 h(x) = 1 ++ /A(x)h(x) = 1 b) /AuB(X) = 1 ++ X E AU B ++ x E A or x E B ++ /A(x) = 1 or h(x) = 1 ++ /A (x) + h(x) -/A(x)h(x) = 1 c) fx(x) = 1 ++ x E A ++ x¢A++/A(x)=O++ I-fA(x)= 1 d)/AE!)B(X)= 1 ++ x E A EB B ++ (x E A and x ¢ B) or (x ¢ A and x E B) ++ /A(x) + h(x) -2/A(x)h(x) = 1 69. a) True; because lxJ is already an integer, rlxJl = lxJ. b) False; x = ! is a counterexam ple. c) True; if x or y is an integer, then by property 4b in Table 1, the difference is o. If neither x nor y is an integer, then x = n + E and y = m + 8, where nand m are integers and E and 8 are positive real numbers less than 1. Then m + n < x + y < m + n + 2, so r x + y 1 is either m + n + I or m + n + 2. Therefore, the given expression is either (n + 1) + (m + 1) -(m + n + 1) = 1 or (n + 1) + (m + 1) -(m + n + 2) = 0, as desired. d) False; x = ! and y = 3 is a counterexam ple. e) False; x = ! is a coun­ terexample. 71. a) If x is a positive integer, then the two sides are equal. So suppose that x = n2 + m + E, where n2 is the largest perfect square less than x, m is a nOjegative integer, and 0 < E ::: 1. Then both ..;x and.J[XJ = n2 + m are between n and n + 1, so both sides equal n. b) If x is a positive integer, then the two sides are equal. So suppose that x = n2 - m - E, where n2 is the smallest perfect square greater than x , m is a nonnegative integer, and E is a real num­ ber with 0 < E ::: 1. Then both ..;x and.JTXT = .J n2 - m are between n - 1 and n. Therefore, both sides of the equation equal n. 73. a) Domain is Z; codomain is R; domain of definition is the set of nonzero integers; the set of values for which f is undefined is {O}; not a total function. b) Domain is Z; codomain is Z; domain of definition is Z; set of values for which f is undefined is 0; total function. c) Domain is Z x Z; codomain is Q; domain of definition is Z x (Z -(OJ); set of values for which f is undefined is Z x (OJ; not a total function. d) Domain is Z x Z; codomain is Z; domain of definition is Z x Z; set of values for which f is undefined is 0; total function. e) Domain is Z x Z; codomain is Z; domain of definitions is {( m, n) 1m> n}; set of values for which f is undefined is {(m, n) 1m::: n}; not a total func­ tion. 75. a) By definition, to say that S has cardinality m is to say that S has exactly m distinct elements. Therefore we can assign the first object to 1, the second to 2, and so on. This provides the one-to-one correspondence. b) By part (a), there is a bijection f from S to {I, 2, ... , m} and a bijection g from T to {I, 2, ... , mI. Then the composition g-I 0 f is the desired bijection from S to T. 77. It is clear from the formula that the range of values the function takes on for a fixed value ofm + n, say m + n = x, is (x - 2)(x -1)/2 + 1 through (x - 2)(x -1)/2 + (x -1), because m can assume the values 1,2,3, ... , (x -1) under these conditions, and the first term in the formula is a fixed positive integer when m + n is fixed. To show that this function is one-to-one and onto, we merely need to show that the range of val­ ues for x + 1 picks up precisely where the range of values for x left off, i.e., that f(x -1, 1) + 1 = f(l, x). We have f(x -1, 1)+ 1 = (X-2)�X-I) + (x -1)+ 1 x2-;+2 = (X-;I)x + 1 = f(I,x). Section 2.4 Answers to Odd-Numbered Exercises S-15 l.a)3 b)-I c) 787 d) 2639 3.a)ao=2,aI=3, a2=5, a3=9 b)ao= I,aI=4, a2=27, a3 =256 c) ao = 0, al = 0, a2 = 1, a3 = 1 d) ao = 0, al = 1, a2 = 2, a3 = 3 5. a) 2,5,8,11,14,17,20,23,26,29 b) 1, 1, 1,2,2,2,3,3,3,4 c) 1, 1,3,3,5,5,7,7,9,9 d) -1, -2, -2, 8, 88, 656, 4912, 40064, 362368, 3627776 e) 3, 6,12,24,48,96,192,384,768,1536 t) 1, 1,2,3,5, 8, 13,21,34,55 g) 1,2,2,3,3,3,3,4,4,4 b) 3, 3, 5, 4, 4,3,5,5,4,3 7. Each term could be twice the previous term; the nth term could be obtained from the previous term by adding n -1; the terms could be the positive integers that are not multiples of 3; there are infinitely many other possibilities. 9. a) One 1 and one 0, followed by two Is and two Os, followed by three 1 s and three Os, and so on; 1, 1, 1 b) The positive integers are listed in increasing order with each even positive integer listed twice; 9, 10, 10. c) The terms in odd-numbered locations are the successive powers of2; the terms in even-numbered locations are all 0; 32, 0, 64. d) an = 3 · 2n-I; 384, 768,1536 e) an = 15 -7(n -1) = 22 -7n; -34, -41, -48 t) an = (n2 + n + 4)/2; 57, 68, 80 g) an = 2n3; 1024, 1458, 2000 b) an = n! + 1; 362881, 3628801, 39916801 11. Among the integers 1, 2, ... , an, where an is the nth positive integer not a perfect square, the nonsquares are aI,a2, ... ,an and the squares are 12,22, ... ,k2, where k is the integer with k2 < n + k < (k + 1)2. Consequently, an = n + k, where k2 < an < (k + 1)2. To find k, first note that k2 < n + k < (k + 1)2, so k2 + 1 ::: n + k ::: (k + 1)2 -1. Hence, (k -!)2 + i = k2 - k + 1 ::: n ::: k2 + k = (k + !)2 -!. It follows that k - ! < y'n < k + !,sok = (y'n}andan = n + k = n + {y'n}. 13. a) 20 b) 11 c) 30 d) 511 15. a) 1533 b) 510 c) 4923 d) 9842 17. a) 21 b)78 c) 18 d) 18 19. L:j=I(aj -aj-I) = an -ao 21. a) n2 b) n(n + 1)/2 23.15150 25. n(n+I�(2n+I) + n(nt) + (n + I)(m - (n + 1)2 + 1), where n = l.jmJ - 1 27. a) 0 b) 1680 c) 1 d) 1024 29.34 31. a) Countable, -1, -2, -3, -4,... b) Countable, 0,2, -2,4, -4,... c) Uncountable d) Countable, 0, 7, -7, 14, -14, . . . 33. a) Countable: match n with the string of n 1 s. b) Countable. To find a correspondence, follow the path in Example 20, but omit fractions in the top three rows (as well as continu­ ing to omit fractions not in lowest terms). c) Uncountable d) Uncountable 35. Assume that A - B is countable. Then, because A = (A -B) U B, the elements of A can be listed in a sequence by alternating elements of A -B and elements of B. This contradicts the uncountability of A. 37. Assume that B is countable. Then the elements of B can be listed as bI. b2, b3, •••• Because A is a subset of B, taking the sub­ sequence of {bn} that contains the terms that are in A gives a listing of the elements of A. Because A is uncountable, this is impossible. 39. We are given bijections f from A to B and g from C to D. Then the function from A x C to B x D that sends (a, c) to (f(a), g(c» is a bijection. 41. Suppose that AI, A2, A3, ... are countable sets. Because Ai is countable, S-16 Answers to Odd-Numbered Exercises we can list its elements in a sequence as ail , ai2, ai3, .... The elements of the set U7=1 Ai can be listed by listing all terms aij with i + j = 2, then all terms aij with i + j = 3, then all terms aij with i + j = 4, and so on. 43. There are a finite number of bit strings of length m, namely, 2m• The set of all bit, strings is the union of the sets of bit strings oflength m for m = 0, I, 2, .... Because the union of a countable number of countable sets is countable (see Exercise 41), there are a countable number of bit strings. 45. For any finite alphabet there are a finite number of strings of length n, whenever n is a positive integer. It follows by the result of Exercise 41 that there are only a countable number of strings from any given finite alphabet. Because the set of all computer programs in a particular language is a subset of the set of all strings of a finite alphabet, which is a countable set by the result from Exercise 36, it is itself a countable set. 47. Exercise 45 shows that there are only a countable number of computer programs. Consequently, there are only a countable number of computable functions. Because, as Exercise 46 shows, there are an uncountable number of functions, not all functions are computable. Supplem entary Exercises 1. a) A b) A nBc) A - B d) An B e) A $ B 3. Yes 5.A-(A-B) = A-(AnB) = An(AnB) = An (AUB)=(AnA)U(AnB)=0U(AnB)=AnB 7. LetA = (I}, B = 0,C = {I}. Then (A -B) -C = 0, but A -(B -C) = {I}. 9. No. For example, let A = B = {a, b}, C = 0, and D = {a}. Then (A - B) - (C -D) = 0-0 = 0, but (A - C) -(B - D) = {a, b} -{b} = {a}. 11.a) 101 :::: IAnBI :::: IAI :::: IAUBI :::: lUI b) 101 :::: IA - BI :::: IA $ BI :::: IAUBI :::: IAI + IBI 13. a) Yes, no b) Yes, no c) f has inverse with f-I(a) = 3, f-I(b) = 4, f-I(C) = 2, f-I(d) = I; ghasnoinverse. 15. Letf(a) = f(b) = I, f(c) = f(d) = 2, S = {a, c}, T = {b, d}. Then f(S n T) = f(0) = 0, but f(S) n f(T) = {I, 2} n {I, 2} = {1,2}. 17. Let x E A. Then Sf({x}) = (f(y) lYE {x}} = (f(x)}. By the same reasoning, Sg({x}) = (g(x)}. Because Sf = Sg, we can conclude that (f(x)} = (g(x)}, and so nec­ essarily f(x) = g(x). 19. The equation is true if and only if the sum of the fractional parts of x and y is less than 1. 21. The equation is true if and only if either both x and y are integers, or x is not an integer but the sum of the fractional parts of x and y is less than or equal to 1. 23. If x is an integer, then LxJ + Lm -xJ = x + m -x = m. Otherwise, write x in terms of its integer and fractional parts: x = n + E, where n = LxJ and 0 < E < 1. In this case LxJ + Lm -xJ = Ln + EJ + Lm - n - EJ = n + m - n - I = m -1. 25. Write n = 2k + I for some integer k. Then n2 = 4t2 + 4k + 1, so n2/4 = k2 + k + !. Therefore, rn2/41 = k2 + k + 1. But (n2 + 3)/4 = (4t2 + 4k + I + 3)/4 = k2 + k + 1. 27. Let x = n + (r /m) + E, where n is an integer, r is a nonnegative integer less than m, and E is a real number with 0 :::: E < I/m. The left-hand side is Lnm + r + mEJ = nm + r. On the right-hand side, the terms Lx J through Lx + S-J6 (m + r -1)/mJ are all just n and the terms from Lx + (m - r)/ m J on are all n + 1. Therefore, the right-hand side is (m - r)n + r(n + I) = nm + r, as well. 29. WI 31. al = I; a2n+1 = n . a2n for all n > 0; and a2n = n + a2n-1 for all n > O. The next four terms are 5346, 5353, 37471, and 37479. CHAPTER 3 Section 3.1 1. max:= I, i := 2, max := 8, i := 3, max := 12, i := 4, i := 5, i := 6, i := 7, max:= 14, i := 8, i := 9, i := 10, i:= 11 3. procedure sum(al, ... , an: integers) sum:= al for i := 2 ton sum := sum + ai {sum has desired value} 5. procedure duplicates (a I , a2, ... , an: integers in nondecreasing order) k := 0 {this counts the duplicates} j :=2 whilej :::: n begin if aj = aj_1 then begin k:= k+ I Ck := aj while U :::: n and aj = Ck) j :=j + I end j:= j + I end {CI, C2, ... , Ck is the desired list} 7. procedure last even location(a l ,a2, ... ,an: integers) k:=O for i :=10 I to n if ai is even then k := i end {k is the desired location (or 0 if there are no evens)} 9. procedure palindrome check(ala2 ... an: string) answer:= true for i := I to Ln/2J if ai =I: an+l-i then answer:= false end {answer is true iff string is a palindrome} 11. procedure interchang e(x, y: real numbers) Z :=X x :=y Y:=Z The minimum number of assignments needed is three. 13. Linear search: i := I, i := 2, i := 3, i := 4, i := 5, i := 6, i := 7, location := 7; binary search: i := I, j := 8, m := 4, i := 5, m := 6, i := 7, m := 7, j := 7, location := 7 15. procedure insert(x, ai, a2, ... , an: integers) {the list is in order: al :::: a2 :::: ... :::: an} an+! := x + 1 i := I S-/7 whilex > ai i := i + 1 for j := 0 to n - i an-j+1 := an-j ai :=x {x has been inserted into correct position} 17. procedure first largest(a l, ... , an: integers) max :=al location := 1 for i := 2 to n begin if max < ai then begin max:= ai location := i end end 19. procedure mean-median-max-min(a , b, c: integers) mean:= (a + b + c)/3 {the six different orderings of a, b, c with respect to::: will be handled separately} if a > b then begin if b > c then median := b; max := a; min := c end (The rest of the algorithm is similar.) 21. procedure first-three (aI, a2, ... , an: integers) if al > a2 then interchange al and a2 if a2 > a3 then interchange a2 and a3 if al > a2 then interchange al and a2 23. procedure onto(f: function from A to B where A = {al, ... , an}, B = {bl, ... , bm}, al, ... , an, bl, ••• , bm are integers) for i := 1 to m hit(bi) := 0 count:= 0 for j := 1 ton if hit(f(a j» = 0 then begin hit(f(aj» := 1 count := count + 1 end if count = m then onto := true else onto := false 25. procedure ones(a: bit string, a = ala2 ... an) ones:= 0 for i := 1 to n begin if ai := 1 then ones := ones + 1 end {ones is the number of ones in the bit strings a} 27. procedure ternary search(s: integer, a\.a2, ... , an: increasing integers) i := 1 Answers to Odd-Numbered Exercises 8-17 j :=n whilei<j-l begin 1= l(i + j)/3J u = L2(i + j)/3J if x > au then i := u + 1 else if x > a1 then begin i := I + 1 j :=u end else j := I end if x = ai then location := i else if x = a j then location := j else location := 0 {location is the subscript of the term equal to x (0 ifnot found)} 29. procedure find a mode(al, a2, ... , an: nondecreasing integers) modecount := 0 i := 1 while i .:::: n begin value:= ai count:= 1 while i .:::: n and ai = value begin count := count + 1 i := i + 1 end if count > modecount then begin modecount := count mode:= value end end {mode is the first value occurring most often} 31. procedure find duplicate( a I , a2 •... , an: integers) location := 0 i:= 2 while i .:::: n and location = 0 begin j:= I while j < i and location = 0 if ai = aj then location := i elsej := j + 1 i := i + 1 end {location is the subscript of the first value that repeats a previous value in the sequence} 33. procedure find decrease(a l. a2, ...• an: positive integers) location := 0 i:= 2 while i .:::: n and location = 0 S-18 Answers to Odd-Numbered Exercises if ai < ai-I then location := i else i := i + 1 {location is the subscript of the first value less than the immediately preceding one} 35. At the end of the first pass: 1,3, 5,4, 7; at the end of the second pass: 1,3,4,5,7; at the end of the third pass: 1,3,4, 5, 7; at the end of the fourth pass: 1,3,4,5, 7 37. procedure better bubblesort (I'll, ... , an: integers) i : = 1; done : = false while (i < n and done = false) begin done: = true for j : = 1 to n - i if aj > aj+1 then begin interchange aj and aj+1 done : = false end i:=i+l end {aI, ... ,an is in increasing order} 39. At the end ofthe first, second, and third passes: 1, 3, 5, 7, 4; at the end of the fourth pass: 1,3,4,5,7 41. a) 1, 5, 4, 3, 2; 1, 2, 4, 3, 5; 1, 2, 3, 4, 5; 1, 2, 3, 4, 5 b) 1, 4, 3, 2, 5; 1, 2, 3, 4, 5; 1, 2, 3, 4, 5; 1, 2, 3, 4, 5 c) 1, 2, 3, 4, 5; 1, 2, 3, 4, 5; 1, 2, 3, 4, 5; 1, 2, 3, 4, 5 43. We carry out the linear search algorithm given as Algo­ rithm 2 in this section, except that we replace x i= ai by x < ai, and we replace the else clause with else location := n + 1. 45.2 + 3 + 4 + ... + n = (n2 + n -2)/2 47. Find the lo­ cation for the 2 in the list 3 (one comparison), and insert it in front of the 3, so the list now reads 2, 3,4, 5, 1, 6. Find the location for the 4 (compare it to the 2 and then the 3), and insert it, leaving 2, 3,4, 5, 1, 6. Find the location for the 5 (compare it to the 3 and then the 4), and insert it, leaving 2,3,4,5, 1,6. Find the location for the 1 (compare it to the 3 and then the 2 and then the 2 again), and insert it, leaving 1, 2, 3, 4, 5, 6. Find the location for the 6 (compare it to the 3 and then the 4 and then the 5), and insert it, giving the final answer 1,2,3,4,5,6. 49. procedure binary insertion sort(al' a2, ... ,an: real numbers with n � 2) for j := 2 to n begin {binary search for insertion location i} left:= 1 right:= j -1 while left < right begin middle := l(left + right)/2J if a j > amidd/e then left := middle + 1 else right := middle end if a j < a/eft then i := left else i := left + 1 {insert aj in location i by moving ai through aj_1 toward back oflist} m :=aj for k := 0 to j -i-I aj-k := aj-k-I ai :=m end {aI, a2, ... , an are sorted} S-J8 51. The variation from Exercise 50 53. a) Two quarters, one penny b) Two quarters, one dime, one nickel, four pen­ nies c) A three quarters, one penny d) Two quarters, one dime 55. Greedy algorithm uses fewest coins in parts (a), (c), and (d). a) Two quarters, one penny b) Two quar­ ters, one dime, nine pennies c) Three quarters, one penny d) Two quarters, one dime 57. a) The variable f will give the finishing time of the talk last selected, starting out with f equal to the time the hall becomes available. Order the talks in increasing order ofthe ending times, and start at the top of the list. At each stage of the algorithm, go down the list of talks from where it left off, and find the first one whose starting time is not less than f. Schedule that talk and update f to record its finishing time. b) The 9:00-9:45 talk, the 9:50-10:15 talk, the 10:15-10:45 talk, the 11:00-11:15 talk 59. a) Here we assume that the men are the suitors and the women the suitees. procedure stable(M I, M2, ... , Ms, WI, W2, •••. ' Ws: preference lists) for i := 1 to s mark man i as rejected for i := I to s set man i's rejection list to be empty for j := 1 tos set woman j's proposal list to be empty while rejected men remain begin for i := 1 to s if man i is marked rejected then add i to the proposal list for the woman j who ranks highest on his preference list but does not appear on his rejection list, and mark i as not rejected forj:=ltos end if woman j's proposal list is nonempty then remove from j's proposal list all men i except the man io who ranks highest on her preference list, and for each such man i mark him as rejected and add j to his rejection list for j := 1 tos match j with the one man on j's proposal list {This matching is stable.} b) There are at most s2 iterations of the while loop, so the algorithm must terminate. Indeed, if at the conclusion of the while loop rejected men remain, then some man must have been rejected and so his rejection list grew. Thus, each pass through the while loop, at least one more of the S2 possible rejections will have been recorded, unless the loop is about to terminate. Furthermore, when the while loop terminates, each man will have one pending proposal, and each woman will have at most one pending proposal, so the assignment must be one-to-one. c) If the assignment is not stable, then there is a man m and a woman w such that m prefers w to the woman w' with whom he is matched, and w prefers m to the man with whom she is matched. But m must have proposed 5-19 to w before he proposed to w', because he prefers the for­ mer. Because m did not end up matched with w, she must have rejected him. Women reject a suitor only when they get a better proposal, and they eventually get matched with a pending suitor, so the woman with whom w is matched must be better in her eyes than m, contradicting our original assumption. Therefore the marriage is stable. 61. Run the two programs on their inputs concurrently and report which one halts. Section 3.2 1. The choices of C and k are not unique. a) C = 1, k = 10 b) C = 4, k = 7 c) No d) C = 5, k = 1 e) C = 1, k = 0 1) C = 1, k = 2 3. X4 + 9x3 + 4x + 7 :s 4X4 for all x > 9; witnesses C = 4, k = 9 5. (x2 + I)/(x + 1) = x-I + 2/(x + 1) < x for all x > I;witnessesC = I,k = 1 7. The choices of C and k are not unique. a) n = 3, C = 3, k = 1 b) n = 3, C = 4, k = 1 c) n = 1, C = 2, k = 1 d) n = 0, C = 2, k = 1 9. x2 + 4x + 17 :s 3x3 for all x > 17, so x2 + 4x + 17 is O(x3), with witnesses C = 3, k = 17. How­ ever, if x3 were O(x2 + 4x + 17), then x3 :s C(x2 + 4x + 17) :s 3C x 2 for some C, for all sufficiently large x, which implies that x :s 3C for all sufficiently large x, which is im­ possible. Hence, x3 is not 0 (x2 + 4x + 17). 11. 3x4 + 1 :s 4x4 = 8(x4/2) for all x > 1, so 3x4 + 1 is 0 (x4 /2), with witnesses C = 8, k = 1. Also x4/2 :s 3x4 + 1 for all x > 0, so x4/2 is O(3x4 + 1), with witnesses C = 1, k = O. 13. Because 2n :s 3n for all n > 0, it follows that 2n is O(3n), with witnesses C = 1, k = O. However, if 3n were O(2n), then for some C, 3n :s C ·2n for all sufficiently large n. This says that C ::: (3/2t for all sufficiently large n, which is impossible. Hence, 3n is not O(2n). 15. All functions for which there exist real numbers k and C with If(x)1 :s C for x > k. These are the functions f(x) that are bounded for all sufficiently large x. 17. There are constants CI, C2, kl' and k2 such that If(x)1 :s C Jig(x)1 for all x > kl and Ig(x)1 :s C2Ih(x)1 for all x > k2• Hence, for x > max(kl, k2) it follows that If(x)1 :s CJig(x)1 :s CIC2Ih(x)l. This shows that f(x) is O(h(x». 19. a) O(n3) b) O(n5) c) O(n3 . n!) 21. a) O(n210gn) b) O(n2(logn)2) c) O(n2n) 23. a) Neither 8(x2) nor Q(X2) b) 8(x2) and Q(x2) c) Neither 8(x2) nor Q(X2) d) Q(x2), but not 8(x2) e) Q(x2), but not 8(x2) 1) Q(X2) and 8(x2) 25. If f(x) is 8(g(x », then there exist constants C I and C 2 with C Ilg(x)1 :s If(x)1 :s C2Ig(x)l. It follows that If(x)l:s C2Ig(x)1 and Ig(x)1 :s (I/CI)lf(x)1 for x> k. Thus, f(x) is O(g(x» and g(x) is O(f(x» . Conversely, suppose that f(x) is O(g(x» and g(x) is o (f(x» . Then there are constants CJ, C2, kJ, and k2 such that If(x)1 :s CJig(x)1 for x> kl and Ig(x)1 :s C2If(x)1 for x > k2. We can assume that C2 > 0 (we can al­ ways make C2 larger). Then we have (I/C2)lg(x)1 :s If(x)1 :s CJig(x)1 for x > max(kl' k2)' Hence, f(x) is 8(g(x». 27. If f(x) is 8(g(x», then f(x) is both O(g(x» and Q(g(x». Hence, there are positive constants CI, kJ, C2, and k2 such that If(x)1 :s C2Ig(x)1 for all x > k2 and If(x)1 ::: CJig(x)1 for all x > kl. It follows that CJig(x)1 :s If(x)1 :s Answers to Odd-Numbered Exerc ises S-19 C2Ig(x)lwheneverx > k,wherek = max(kl,k2).Converseiy, if there are positive constants CI, C2, and k such that CJig(x)1 :s If(x)1 :s C2Ig(x)1 for x > k, then taking kl = k2 = k shows that f(x) is both O(g(x» and 8(g(x» . 29. Y k x 31. If f(x) is 8(1), then If(x)1 is bounded between pos­ itive constants CI and C2. In other words, f(x) cannot grow larger than a fixed bound or smaller than the nega­ tive of this bound and must not get closer to 0 than some fixed bound. 33. Because f(x) is O(g(x», there are con­ stants C and 1 such that If(x)1 :s Clg(x)1 for x > I. Hence, Ifk(x)1 :s Cklgk(x)1 for x> I, so fk(x) is O(gk(x» by tak­ ing the constant to be Ck. 35. Because f(x) and g(x) are increasing and unbounded, we can assume f(x) ::: 1 and g(x) ::: 1 for sufficiently large x. There are constants C and k with f(x) :s Cg(x) for x > k. This implies that log f(x) :s log C + log g(x) < 2 log g(x) for sufficiently large x. Hence, logf(x) is O(logg(x» . 37. By definition there are posi­ tive constraints CI, C;, C2, C�, kl' k;, k2, and k� such that fl (x) ::: CJig(x)1 for all x > kl' fi (x) :s C; Ig(x)1 for all x > k;, h(x)::: C2Ig(x)1 for all x > k2' and f2(X) :S qlg(x)1 for all x > k�. Adding the first and third inequalities shows that fl(x) + h(x)::: (CI + C2)lg(x)1 for all x> k where k = max(kl, k2)' Adding the second and fourth inequalities shows that fl(x) + hex) :s (C; + CDlg(x)1 for all x > k' where k' = max(k;, k�). Hence, fl(x) + hex) is 8(g(x». This is no longer true if fl and h can assume negative val­ ues. 39. This is false. Let fl = x2 + 2x, hex) = x2 + x, and g(x) = x2. Then fl(x) and hex) are both O(g(x», but (fl -h)(x) is not. 41. Take fen) to be the function with fen) = n if n is an odd positive integer and fen) = 1 if n is an even positive integer and g(n) to be the function with g(n) = 1 if n is an odd positive integer and g(n) = n if n is an even positive integer. 43. There are positive constants C I, C2, C;, q, kl' k;, k2, and k� such that Ifl(x)1 ::: CJigl(x)1 for all x> kl' Ifl(x)1 :s C;lgI(x)1 for all x ::: k;, Ih(x)1 > C2Ig2(X)1 for all x> k2, and Ih(x)1 :s C�lg2(X)1 for all x > k�. Because hand g2 are never zero, the last two in­ equalities can be rewritten as II/h(x)1 :s (1/C2)II/g2(x)1 for all x> k2 and II/h(x)1 ::: (I/C�)II/g2(x)1 for all x > k�. Multiplying the first and rewritten fourth inequali­ ties shows that If I (x)/h(x)1 ::: (CI/C�)lgI(X)/g2(X)1 for all x > max(kl, kD, and multiplying the second and rewritten third inequalities gives If I (x)/h(x)1 :s (C;/C2)lgI(X)/g2(X)1 for all x > max(k; , k2)' It follows that fl/ h is big-Theta of gil g2. 45. There exist positive constants CI,C2,kl,k2,k;,k� such that If(x,y)I:sC1Ig(x,y)1 for S-20 Answers to Odd-Numbered Exercises all x> kl and y > k2 and If(x,y)1 � C2Ig(x,y)1 for all x > k; and y > k�. 47. (x2 + xy + X Iogy)3 < (3x2y3) = 27x6y3 for x > 1 and y > 1, because x2 < x 2y, xy < x2y, and x logy < x2y. Hence, (x2 + xy + X Iogy)3 is O(x6y3). 49. For all positive real numbers x andy, LxyJ :::: xy. Hence, Lxy J is O(xy) from the definition, taking C = 1 and kl = k2 = O. 51.a) Iimx .... oo X2/X3 = Iimx .... oo l/x = 0 b) 1· x lo¥x 1· � 1· I 0 ( . lmx-+oo x = lmx .... oo x = lmx .... oo xln2 = uSing PH" . l' 1» 1· x2 1· 2x 1· L Oplta s ru e c lmx .... oo F = lmx .... oo 2X.ln2 = lmx .... oo 2 _ 0 (. PH"· l' 1) d) 1· x2+x+1 - 2x.(ln2)2 -usmg L Oplta s ru e lmx .... oo -x-2 -- Iimx-.oo (1 + � + �) = 1 ;/= 0 53. Y xlogx --:;z x 55. No. Take f(x) = l/x2 and g(x) = l/x. 57. a) Be­ cause limx .... oo f(x)/g(x) = 0, If(x)I/lg(x)1 < 1 for suf­ ficiently large x. Hence, If(x)1 < Ig(x)1 for x> k for some constant k. Therefore, f(x) is O(g(x» . b) Let f(x) = g(x) = x. Then f(x) is o (g(x», but f(x) is not o(g(x» because f(x)/g(x) = 1. 59. Because fz(x) is o(g(x», from Exercise 57(a) it follows that fz(x) is O(g(x» . By Corollary 1, we have fl(x) + fz(x) is O(g(x» . 61. We can easily show that (n -i)(i + 1) � n for i =0,1, ... , n -1. Hence, (n!)2 = (n . 1)«n - 1) . 2)· «n -2) . 3)··· (2· (n - 1»· (1 . n) � nn. Therefore, 2Iogn! � n logn. 63. Compute that log 5! � 6.9 and (5 log 5)/4 � 2.9, so the inequality holds for n = 5. Assume n � 6. Because n! is the prod­ uct of all the integers from n down to 1, we have n! > n(n -I)(n -2)··· rn/21 (because at least the term 2 is missing). Note that there are more than n/2 terms in this product, and each term is at least as big as n /2. Therefore the product is greater than (n/2)(nf2). Taking the log of both sides of the inequality, we have log n! > log (�r/2 = � log � = �(logn - 1) > (n Iogn)/4, because n > 4 implies Iogn - 1 > (logn)/2. 65. All are not asymptotic. Section 3.3 1. 2n -1 3. Linear 5.0(n) 7. a) power := 1, y := 1; i := 1, power:= 2, y:= 3; i:= 2, power:= 4, y := 15 b) 2n multiplications and n additions 9. a) 2109 �103 x 10' b) 109 c) 3.96 X 107 d) 3.16 X 104 e) 29 t) 12 11. a) 36 years b) 13 days c) 19 minutes 13. The av­ erage number of comparisons is (3n+ 4)/2. 15. O(logn) 17.0(n) 19.0(n2) 21. O(n) 23.0(n) 25.0(Iogn) S-20 comparisons; O(n2) swaps 27. a) doubles b) increases by 1 Section 3.4 1. a) Yes b) No c) Yes d) No 3. Suppose that a I b. Then there exists an integer k such that ka = b. Because a( ek) = be itfollowsthata I be. 5.Ifa I bandb I a,thereareintegerse and d such that b = ae and a = bd. Hence, a = aed. Because a ;/= 0 it follows that cd = 1. Thus either e = d = 1 or e = d = -1. Hence, either a = b or a = -b. 7. Because ae I be there is an integer k such that aek = be. Hence, ak = b, soalb. 9.a)2,5 b)-l1,1O c)34,7 d)77,O e)O,O t) 0, 3 g) -1, 2 b) 4, 0 11. If a modm = bmodm, then a and b have the same remainder when divided by m. Hence, a=qlm+r and b=q2m+r, where O :::: r <m. It follows that a -b = (ql -q2)m, so m I (a - b). It foI­ Iowsthata == b (mod m). 13. There is some b with (b - 1) k < n :::: bk. Hence, (b -I)k :::: n - 1 < bk. Divide by k to obtain b - 1 < n/ k :::: band b - 1 :::: (n -1)/ k < b. Hence, rn/ kl = band L(n -1)/ kJ = b - 1. 15. x mod m if x modm :::: rm/21 and (x mod m) -m if x mod m > rm/21 17. a) 1 b)2 c)3 d)9 19.a)No b) No c) Yes d) No 21. Let m = tn. Because a == b (modm) there exists an integer s such that a = b + sm. Hence, a = b + (st)n, so a == b(modn). 23. a) Let m = e = 2, a = 0, and b = 1. Then 0 = ae == be = 2 (mod 2), but 0 = a ¥= b = 1 (mod 2). b) Let m = 5, a = b = 3, e = 1, and d = 6. Then 3 == 3 (mod 5) and 1 == 6 (mod 5), but 31 = 3 ¥= 4 == 729 = 36 (mod 5). 25. Because a == b (mod m), there exists an integer s such that a = b + sm, so a -b = sm. Then ak -bk = (a -b)(ak-I + ak-2 b + ... + abk-2 + bk-I), k � 2, is also a multiple of m. It follows that ak == bk (modm). 27. a) 7, 19, 7, 7, 18,0 b) Take the next available space mod 31. 29.2, 6, 7, 10, 8, 2, 6, 7, 10, 8,... 31. a) GR QRW SDVV JR b) QBABGCNFFTB c) QXUXMAHJJZX 33.4 35. The check digit of the ISBN for this book is valid because 1·0+2 ·0+3·7+4·2+5·8+6·8+7 ·0+8 ·0+9· 8 + 10 . 2 == 0 (mod 11). Section 3.5 1. 29,71,97 prime; 21, Ill, 143 not prime 3. a) 23. 11 b) 2.32.7 c)36 d)7 . 11 . 13 e) 11 . 101 t) 2.33. 5·7· 13 . 37 5.28.34.52.7 7. Suppose that Iog23 = a/b where a, bE Z+ and b;/= O. Then 2a/b = 3, so 2a = 3b• This violates the Fundamental Theorem of Arithmetic. Hence, 10g23 is irrational. 9.3, 5, and 7 are primes of the de­ sired form. 11.1, 7, 11, 13, 17, 19, 23, 29 13. a) Yes b) No c) Yes d) Yes 15. Suppose that n is not prime, so that n = ab, where a and b are integers greater than 1. Because a > 1, by the identity in the hint, 2a -1 is a fac­ tor of 2n -1 that is greater than 1, and the second factor in this identity is also greater than 1. Hence, 2n - 1 is not prime. 17. a) 2 b) 4 c) 12 19. ¢(pk) = pk _ pk-I 21. a) 35.53 b) 1 c) 2317 d) 41 ·43·53 e) 1 S-21 t)1111 23.a)211·37 ·59 ·73 b)29.37.55.73.11. 13 . 17 c) 2331 d) 41 ·43· 53 e)212313517721 t) Undefined 25. gcd (92928, 123552) = 1056; Icm(92928, 123552) = 10,872,576; both products are 11,481,440,256. 27. Because min(x, y) + max(x, y) = x + y, the exponent of Pi in the prime factorization of gcd(a, b)· lcm(a, b) is the sum of the exponents of Pi in the prime factorizations of a and b. 29. a) an = I if n is prime and an = 0 otherwise. b) an is the smallest prime factor of n with al = l. c) an is the number of positive divisors of n. d) an = I if n has no divisors that are perfect squares greater than I and an = 0 otherwise. e) an is the largest prime less than or equal to n. t) an is the product of the first n - 1 primes. 31. Because every second integer is divisible by 2, the product is divisible by 2. Because every third integer is divisible by 3, the product is divisible by 3. Therefore the product has both 2 and 3 in its prime factorization and is therefore divisible by 3 . 2 = 6. 33. n = 1601 is a counterexam ple. 35. Suppose that there are only finitely many primes of the form 4k + 3, namely ql, q2, ... ,qn, where ql = 3, q2 = 7, and so on. Let Q = 4qlq2 ... qn - l. Note that Q is of the form 4k + 3 (where k = ql q2 ... qn -1). If Q is prime, then we have found a prime of the desired form different from all those listed. If Q is not prime, then Q has at least one prime factor not in the list ql, q2, ... , qn, because the remainder when Q is di­ videdbyqj isqj -I,andqj - I =f. O.Becausealloddprimes are either of the form 4k + 1 or of the form 4k + 3, and the product of primes of the form 4k + I is also ofthis form (be­ cause (4k + 1)(4m + 1) = 4(4km + k + m) + 1), there must be-a factor of Q of the form 4k + 3 different from the primes we listed. 37. Given a positive integer x, we show that there is exactly one positive rational number m / n (in lowest terms) such that K (m / n) = x. From the prime factorization of x, read off the m and n such that K (m / n) = x. The primes that occur to even powers are the primes that occur in the prime factor­ ization ofm, with the exponents being half the corresponding exponents in x; and the primes that occur to odd powers are the primes that occur in the prime factorization of n, with the exponents being half of one more than the exponents in x. Section 3.6 I. a) 1110 0111 b) I 0001 1011 0100 c) I 0111 1101 01101100 3. a) 31 b) 513 c) 341 d) 26,896 5. a) 1000 0000 1110 b) 1 00 11 0101 1010 1011 c) 1010 1011 1011 1010 d) 1101 1110 1111 1010 11001110 1101 7.1010 10111100 11011110 1111 9. (B7B)16 11. Adding up to three leading Os if necessary, write the binary ex­ pansion as ( ... b23 b22b2 I b20b13bl2bl1 blOb03b02bOlboo}z. The value of this numeral is boo + 2bol + 4b02 + 8b03 + 24blO + 25bl1 + 26bl2 + 27b13 + 28b20 + 29b21 + 210b22 + 211b23 + ... , which we can rewrite as boo + 2bol +4b02 + 8b03 + (blO + 2bll + 4b12 + 8b13) . 24 + (b20 + 2b21 + 4b22 + 8b23)· 28 + .... Now (bi3bi2bilbiO)2 translates into the hexadecimal digit hi. So our number is ho + hi .24 + h2 .28 + ... = ho + hi . 16 + h2 . 162 + ... , which is the hexadecimal expansion ( ... hlhlho)16. 13. Group together Answers to Odd-Numbered Exercises S-21 blocks of three binary digits, adding up to two initial Os if necessary, and translate each block of three binary digits into a single octal digit. 15. (11101110010101101O001)z, (1273)8 17. Convert the given octal numeral to binary using Exercise 14, then convert from binary to hexadeci­ mal using Example 6. 19.436 21. 27 23. a) 6 b) 3 c) 11 d) 3 e) 40 t) 12 25. 8 27. The binary ex­ pansion of the integer is the unique such sum. 29. Let a = (an-Ian-2 ... alaO)IO. Then a = lOn-Ian_1 + lOn-2an_2 + ... + 10al +ao = an-I +an-2 + ... +al +ao(mod 3), because lW = l(mod 3) for all nonnegative integers j. It follows that 3 I a if and only if 3 divides the sum of the decimal digits ofa. 31. Leta = (an-Ian-2 ... alao}z. Then a = ao + 2al + 22a2 + ... + 2n-Ian_1 = ao -al + a2- a3 + ... ± an-I (mod 3). It follows that a is divisible by 3 if and only if the sum of the binary digits in the even­ numbered positions minus the sum of the binary digits in the odd-numbered positions is divisible by 3. 33. a) -6 b) 13 c) -14 d) 0 35. The one's complement of the sum is found by adding the one's complements of the two integers except that a carry in the leading bit is used as a carry to the last bit of the sum. 37. If m � 0, then the leading bit an-I of the one's complement expansion of m is 0 and the formula reads m = L7::� ai2i. This is correct because the right-hand side is the binary expansion of m. When m is negative, the leading bit an-I of the one's com­ plement expansion of m is 1. The remaining n - 1 bits can be obtained by subtracting -m from 111 ... I (where there are n - lis), because subtracting a bit from 1 is the same as complementing it. Hence, the bit string an-2 ... ao is the binary expansion of (2n-1 -I) - ( -m). Solving the equa­ tion (2n-1 -1) - ( -m) = L7::� ai 2i for m gives the desired equation because an-I = l. 39. a) -7 b) 13 c) -15 d) -1 41. To obtain the two's complement representation of the sum of two integers, add their two's complement repre­ sentations (as binary integers are added) and ignore any carry out of the leftmost column. However, the answer is invalid if an overflow has occurred. This happens when the leftmost dig­ its in the two's complement representation of the two terms agree and the leftmost digit of the answer differs. 43. If m � 0, then the leading bit an-I is 0 and the formula reads m = L7::� ai2i. This is correct because the right-hand side is the binary expansion ofm. Ifm < 0, its two's complement expansion has 1 as its leading bit and the remaining n - 1 bits are the binary expansion of 2n-1 - ( -m). This means that (2n-l) -(-m) = L7::� ai2i. Solving for m gives the desired equation because an-I == 1. 45. 4n 47. procedure Cantor(x: positive integer) n:= 1; f:= 1 while (n + 1) . f ::::: x begin n:= n + 1 f:=f·n end y:=x while n > 0 begin S-22 Answers to Odd-Numbered Exercises an := LYlfJ y :=y -an' I 1:= lin n:= n-l end (x = ann! + an_l(n -I)! + ... + ad!} 49.Firststep:e = O,d = O,so = 1; second step: e = O,d = 1, SI = 0; third step: e = 1, d = 1, S2 = 0; fourth step: e = 1, d = 1, S3 = 0; fifth step: e = 1, d = 1, S4 = 1; sixth step: e = 1, S5 = 1 51. procedure subtraet(a, b: positive integers, a > b, a = (an-lan-2 ... alaO)2, b = (bn-lbn-2 ... blbo)z) B := 0 {B is the borrow} for j := 0 to n - 1 begin if a j ::: b j + B then begin Sj := aj -bj - B B :=0 end else begin Sj := aj + 2 -bj - B B:= 1 end end {(Sn-ISn-2 ... SISO)z is the difference} 53. procedure eompare(a, b: positive integers, a = (anan-I ... alaoh, b = (bnbn-I ... blboh) k:=n whileak = bk andk > 0 k:= k-l if ak = bk then print "a equals b" if ak > bk then print "a is greater than b" if ak < bk then print "a is less than b" 55. 0 (log n) 57. The only time-consuming part of the algo­ rithm is the while loop, which is iterated q times. The work done inside is a subtraction of integers no bigger than a, which has log a bits. The result now follows from Example 8. Section 3.7 1. a) 1 =(-1)·10+ 1·11 b) 1 = 21·21 +(-10) · 44 c) 12 = (-1)·36 + 48 d) 1 = 13·55 + (-21) . 34 e) 3 = 11 ·213 + (-20)·117 t) 223 = 1·0 + 1 ·223 g) 1 = 37 . 2347 + (-706) . 123 h) 2 = 1128 . 3454 + (-835)·4666 i) 1 = 2468·9999 + (-2221)·11111 3. 15· 7 = 105 == 1 (mod 26) 5. 7 7.52 9. Suppose that b and e are both inverses of a modulo m. Then ba == 1 (mod m) and ea == 1 (mod m). Hence, ba == ea (mod m). Because gcd(a, m) = 1 it follows by Theorem 2 that b == e(mod m). 11. x == 8 (mod 9) 13. Let m' = ml gcd(e, m). Because all the common factors of m and e are divided out of m to obtain m', it follows that m' and e are relatively prime. Because m divides (ae -be) = (a -b)e, it follows that m' divides (a -b)e. By Lemma 1, we see that m' divides a -b, S-22 so a == b (mod m'). 15. Suppose that x2 == 1 (mod p). Then p divides x2 - 1 = (x + 1)(x -1). By Lemma 2 it follows that p I (x + 1) or p I (x -I), so x == -1 (mod p) or x == 1 (mod pl. 17. a) Suppose that ia == ja (mod p), where 1 :'S i < j < p. Then p divides ja -ia = aU -i). By Theorem 1, because a is not divisible by p, p divides j -i, which is impossible because j - i is a positive integer less thanp. b) Bypart(a),becausenotwoofa, 2a, ... , (p -l)a are congruent modulo p, each must be congruent to a differ­ ent number from 1 to p -1. It follows that a . 2a . 3a . ... . (p-l)·a == 1 · 2·3 · .... (p -1) (mod pl. It follows that (p -I)! . aP-1 == (p -I)! (mod pl. c) By Wilson's Theorem and part (b), if P does not divide a, it follows that (-I).aP-1 == -1 (modp).Hence,ap-1 == 1 (modp). d)If p I a, then pi aP• Hence, aP == a == 0 (mod pl. If p does not divide a, then aP-1 == a (mod p), by part (c). Mul­ tiplying both sides of this congruence by a gives aP == a (mod p). 19. All integers of the form 323 + 330k, where k is an integer 21. All integers of the form 16 + 252k, where k is an integer 23. Suppose that p is a prime ap­ pearing in the prime factorization of m 1m2' .. mn• Because the miS are relatively prime, p is a factor of exactly one of the miS, say mj. Because mj divides a -b, it follows that a - b has the factor p in its prime factorization to a power at least as large as the power to which it appears in the prime factorization of m j. It follows that m 1m2' .. m n divides a - b, so a == b (mod mlm2'" mn). 25. x == 1 (mod 6) 27. a) By Fermat's Little Theorem, we have 210 == 1 (mod 11). Hence, 2340 = (210)34 == 134 = 1 (mod 11). b) Because 32 == 1 (mod 31), it follows that 2340 = (25)68 = 3268 == 168 = 1 (mod 31). c) Because 11 and 31 are relatively prime, and 11 ·31 = 341, it follows by parts (a) and (b) and Exercise 23 that 2340 == 1 (mod 341). 29. a) 3, 4,8 b) 983 31. First, 2047 = 23 . 89 is composite. Write 2047 - 1 = 2046 = 2 . 1023, so S = 1 and t = 1023 in the definition. Then 21023 = (211 )93 = 204893 == 193 = 1 (mod 2047), as desired. 33. We must show that b2820 == 1 (mod 2821) for all b relatively prime to 2821. Note that 2821 = 7 ·13· 31, and if gcd(b , 2821) = 1, thengcd(b, 7) = gcd(b, 13) = gcd(b, 31) = 1. Using Fermat's Little Theorem we find that b6 == 1 (mod 7), bl2 == 1 (mod 13), and b30 == 1 (mod 31). It follows that b2820 == (b6)470 == 1 (mod 7), b2820 == (bI2)235 == 1 (mod 13), and b2820 == (b30)94 == 1 (mod 31). By Exercise 23 (or the Chinese Re­ mainder Theorem) it follows that b2820 == 1 (mod 2821), as desired. 35. a) If we multiply out this expression, we get n = 1296m3 + 396m2 + 36m + 1. Clearly 6m In -1, 12m In -1, and 18m In -1. Therefore, the conditions of Exercise 34 are met, and we conclude that n is a Carmichael number. b)Letting m =51 gives n = 172,947,529. 37.0= (0,0), 1 = (1,1),2 = (2,2), 3 = (0, 3),4 = (1,4), 5 = (2, 0), 6 = (0, 1), 7 = (1,2), 8 = (2, 3), 9 = (0,4), 10 = (1, 0), 11 = (2,1), 12 = (0,2), 13 = (1, 3), 14 = (2,4) 39. We have ml = 99, m2 = 98, m3 = 97, and m4 = 95,som = 99·98·97·95 = 89,403,930. We find that MI = mimI = 903,070, M2 = mlm2 = 912,285, M3 = mlm3 = 921,690, and M4 = mlm4 = 941,094. Using the Euclidean algorithm, we compute that YI = 37, Y2 = 33, S-23 Y3 = 24, and Y4 = 4 are inverses of Mk modulo mk for k = I, 2, 3, 4, respectively. It follows that the solution is 65·903,070·37 + 2·912,285·33 + 51·921,690·24 + 10 . 941,094·4 = 3,397,886,480 = 537,140 (mod 89,403,930). 41. By Exercise 40 it follows that gcd(2b -1, (2a -1) mod(2b -1» = gcd(2b -1, 2amodb -1). Because the expo­ nents involved in the calculation are b and a mod b, the same as the quantities involved in computing gcd(a, b), the steps used by the Euclidean algorithm to compute gcd(2a -I, 2b -1) run in parallel to those used to compute gcd(a, b) and show that gcd(2a -1, 2b -1) = 2gcd(a,b) -1. 43. Suppose that q is an odd prime with q I 2P -l. By Fermat's Little Theorem, q I 2q-1 - l. From Exercise 41, gcd(2P - I, 2q-1 -1) = 2gcd(p,q-l) - l. Because q is a common di­ visor of 2P -1 and 2q-1 -1, gcd(2P -1, 2q-1 -1) > l. Hence, gcd(p, q -1) = p, because the only other possibility, namely, gcd(p, q -1) = 1, gives us gcd(2P -1, 2q-1 -1) = 1. Hence, p I q -1, and therefore there is a positive inte­ ger m such that q - 1 = mp. Because q is odd, m must be even, say, m = 2k, and so every prime divisor of 2P -1 is of the form 2kp + I. Furthermore, the product of num­ bers of this form is also of this form. Therefore, all divi­ sors of 2P -1 are of this form. 45. Suppose we know both n = pq and (p -1)(q -1). To find p and q, first note that (p -1)(q -1) = pq -p -q + 1 = n -(p + q) + l. From this we can find s = (p + q). Because q = s -p, we have n = p(s -p). Hence, p2 -ps + n = O. We now can use the quadratic formula to find p. Once we have found p, we can find q because q = nip. 47. SILVER 49.34· 144 + (-55) . 89 = 1 51. procedure extended Euclidean (a, b: positive integers) x :=a y:=b oldolds:= 1 olds:= 0 oldoldt:= 0 oldt := 1 whiley =1= 0 begin q := x div y r :=x mody x :=y y:=r s := oldolds - q . olds t := oldoldt - q . oldt oldolds := olds oldoldt := oldt olds:= s oldt:= t end (gcd( a, b) is x, and (oldolds)a + (oldoldt)b = x} 53. Assume that s is a solution of x2 = a (mod p). Then because (-S)2 = S2, -s is also a solution. Further­ more, s ¢ -s (mod p). Otherwise, p I 2s, which implies that pis, and this implies, using the original assump­ tion, that p I a, which is a contradiction. Furthermore, if s and t are incongruent solutions modulo p, then because s2 = t2 (mod p), p I (s2 -t2). This implies that p I (s + t) Answers to Odd-Numbered Exercises S-23 (s -I), and by Lemma 2, p I (s -t) or p I (s + t), so s = t (mod p) or s = -I (mod p). Hence, there are at most two solutions. 55. The value of (�) depends only on whether a is a quadratic residue modulo p, that is, whether x2 = a (mod p) has a solution. Because this depends only on the equivalence class of a modulo p, it follows that (�) = (�) if a = b (mod p). 57. By Exercise 56, (�)(�) = a(p-I)/2b(p-I)/2 = (ab)(p-I)/2 = (�) (mod p). 59. x = 8, 13, 22, or 27 (mod 35) 61. Suppose that we use a prime for n. To find a private decryption key from the corresponding pub­ lic encryption key e, one would need to find a number d that is an inverse for e modulo n -1 so that the calculation shown before Example 12 can go through. But finding such a dis easy using the Euclidean algorithm, because the person doing this would already know n -1. In particular, to find d, one can work backward through the steps of the Euclidean algorithm to express 1 as a linear combination of e and n -1; then d is the coefficient of e in this linear combination. Section 3.8 1. a) 3 x 4 e) [� � �] 143 367 c) [=� �� -t �3] � !8 �: -�3 c)[2 0 4 6] d)l 5. [915 -6/5] -1/5 415 7.0+A= [0 + aij] = [aij +0] =O+A 9.A+(B+ C) = [aij + (bij +Cij)] = [(aij +bij) + Cij] = (A+B) + C 11. The number of rows of A equals the number of columns of B, and the number of columns of A equals the number of rows ofB. 13. A(BC) = [Lqaiq (LrbqrCr/)] = [LqLraiqbqrCrl] = [LrLqaiqbqrCrl] = [Lr {Lqaiqbqr)crl] = (AB)C 15. An = U n 17. a) Let A = [aij] and B = [bij]. Then A + B = [aij + bij]. We have (A + B)I = [aji + bj;] = [aj;] + [bj;] = AI + BI. b) Using the same notation as in part (a), we have BI AI = [ Lq bqia jq] = [ Lq a jqbqi] = (ABY, because the (i, j)th entry is the U, i)th entry of AB. 19. The result fol- lows because [� �] [!C �b ]=[ ad �bc ad �bC] = (ad -bc)I2 = [::.c �b] [� � J 21. An(A-I)n = A(A ... (A(AA-1)A-1)···A-1)A-1 by the associative law. Because AA -I = I, working from the inside shows that An (A -I)n = I. Similarly (A -I)n An = I. Therefore (An)-I = S-24 Answers to Odd-Numbered Exercises (A -I)n. 23. There are m2 multiplications used to find each of the mlm3 entries of the product. Hence, mlm2m3 multiplications are used. 25. AI«A2A3)�) 27. XI = I, X2 = -1,x3 =-2 29.8) [: 1 n 31.a) [l ! n 33. a) A vB = [aij V bij] = [bij vaij] = B v A b) A /\ B = [aij /\ bij] = [bij /\ aij] = B /\ A 35. a) A v (B /\ C) = [aij] v [bjj /\ Cij] = [ajj v (bjj /\ Cjj)] = [(aij V bij) /\ (aij V Cij)] = [aij V bij] /\ [ajj V Cij] = (A v B) /\ (A v C) b) A /\ (B v C) = [aij] /\ [bij v cij] = [ajj /\ (bij v Cij)] = [(ajj /\ bij) v (aij /\ Cjj)] = [ajj /\ bij] v [ajj /\ Cij] = (A /\ B) v (A /\ C) 37. A 0 (B 0 C) = [Vqajq /\ (Vr (bqr /\ Crt))] = [Vq Vr (ajq /\ bqr /\crt)] = [Vr Vq (ajq /\bqr /\crt)] = [ Vr (Vq (ajq /\ bqr) ) /\ Crt] = (A 0 B) 0 C Supplementar y Exercises 1. a) procedure last max( a I, ... , an: integers) max:= al last:= I i:= 2 while i .::: n begin if aj :::: max then begin max:= aj last:= i end i := i + I end {last is the location of final occurrence of largest integer in list} b) 2n - I = O(n) comparisons 3. a) procedure pair zeros(blb2 ... bn: bit string, n :::: 2) X :=bl y:=b2 k :=2 while (k < n and (x =1= 0 or y =1= 0» begin k:= k+ I x :=y y:=bk end if (x = 0 and y = 0) then print "YES" else print "NO" b) O(n) comparisons S-24 5. a) and b) procedure smallest and largest (at ,a2, ... ,an: integers) min:= al max:= al for i := 2 to n begin if ai < min then min := ai if aj > max then max := aj end {min is the smallest integer among the input, and max is the largest} c) 2n -2 7. Before any comparisons are done, there is a possibility that each element could be the maximum and a possibility that it could be the minimum. This means that there are 2n differ­ ent possibilities, and 2n -2 of them have to be eliminated through comparisons of elements, because we need to find the unique maximum and the unique minimum. We classify comparisons of two elements as "virgin" or "nonvirgin," de­ pending on whether or not both elements being compared have been in any previous comparison. A virgin comparison elim­ inates the possibility that the larger one is the minimum and that the smaller one is the maximum; thus each virgin com­ parison eliminates two possibilitie s, but it clearly cannot do more. A nonvirgin comparison must be between two elements that are still in the running to be the maximum or two elements that are still in the running to be the minimum, and at least one of these elements must not be in the running for the other category. For example, we might be comparing x and y, where all we know is that x has been eliminated as the minimum. If we find that x > y in this case, then only one possibility has been ruled out-we now know that y is not the maximum. Thus in the worst case, a nonvirgin comparison eliminates only one possibility. (The cases of other nonvirgin compar­ isons are similar.) Now there are at most l n /2 J comparisons of elements that have not been compared before, each removing two possibilities; they remove 2l n /2 J possibilities altogether. Therefore we need 2n -2 -2ln/2J more comparisons that, as we have argued, can remove only one possibility each, in order to find the answers in the worst case, because 2n -2 possibilities have to be eliminated. This gives us a total of 2n -2 -2ln/2J + In/2J comparisons in all. But 2n -2-2ln/2J + In/2J = 2n -2 -In/2J = 2n -2 + f -n/21 = f2n -n/21 -2 = f3n/21 -2, as desired. 9. At end of first pass: 3,1,4,5,2,6; at end of second pass: 1,3,2,4,5,6; at end of third pass: 1,2,3,4,5,6; fourth pass finds nothing to exchange and algorithm terminates 11. There are pos­ sibly as many as n passes through the list, and each pass uses O(n) comparisons. Thus there are O(n2) comparisons in all. 13. Because log n < n, we have (n log n + n2)3 .::: (n2 + n2)3 .::: (2n2)3 = 8n6 for all n > O. This proves that (n logn + n2)3 is O(n6), with witnesses C = 8 and k = O. 15.0(x22X) 17. Note that � = � . n21 ... � . � . ! > � . I . 1···1· ! = �. 19.5,22, -12, -29 21. Because ac == bc (mod m) there is an integer k such that ac = bc + km. Hence, a -b = km/c. Because a -b is an integer, C I km. Letting d = gcd(m, c), write C = de. Because no factor of e divides mid, it follows that dim and elk. S-25 Thus a - b = (k/e)(m/d), where k/e E Z and mid E Z. Therefore a == b (mod mid). 23. I 25. I 27. (anan-I ... alaO)1O = I:�=o IOkak == I:;=oak (mod 9) because 10k == I (mod 9) for every nonnegative integer k. 29. Ifnot, then suppose that qt, q2, ... ,qn are all the primes of the form 6k + 5. Let Q = 6qlq2··· qn -1. Note that Q is of the form 6k + 5, where k = qlq2··· qn -I. Let Q = PIP2··· PI be the prime factorization of Q. No Pi is 2,3, or any qj' because the remainder when Q is divided by 2 is I, by 3 is 2, and by q j is q j -I. All odd primes other than 3 are of the form 6k + I or 6k + 5, and the product of primes of the form 6k + I is also of this form. Therefore at least one of the Pi'S must be of the form 6k + 5, a contradiction. 31. a) Not mutually relatively prime b) Mutually relatively prime c) Mutually relatively prime d) Mutuallyrelatively prime 33. a) The decryption function is g(q) = ii(q -b) mod 26, where ii is an inverse of a modulo 26. b) PLEASE SEND MONEY 35. x == 28 (mod 30) 37. Recall that a nonconstant polynomial can take on the same value only a finite number of times. Thus f can take on the values 0 and ± 1 only finitely many times, so if there is not some y such that f(y) is composite, then there must be some xo such that ±f(xo) is prime, say p. Look at f(xo + kp). When we plug Xo + kp in for x in the polynomial and multiply it out, every term will contain a factor of p except for the terms that fOrIn f(xo). Therefore f(xo + kp) = f(xo) + mp = (m ± I)p for some integer m. As k varies, this value can be 0, p, or -p only finitely many times; therefore it must be a composite number for some values of k. 39. Assume that every even integer greater than 2 is the sum of two primes, and let n be an integer greater than 5. If n is odd, write n = 3 + (n -3) and decom­ pose n - 3 = p + q into the sum of two primes; if n is even, then write n = 2 + (n -2) and decompose n -2 = p + q into the sum of two primes. For the converse, assume that ev­ ery integer greater than 5 is the sum of three primes, and let n be an even integer greater than 2. Write n + 2 as the sum of three primes, one of which is necessarily 2, so n + 2 = 2 + p+q, whence n =p+q. 41.A4n = [� �], A4n+1 = [�I � J. A4n+2 = [�I �I J. A4n+3 = [� �I J. for n � 0 43. Suppose that A = [� � l Let B = [� � l Because AB = BA, it follows that c = 0 and a = d. Let B = [ � �]. Because AB = BA, it follows that b = O. Hence, A = [� �] = al. 45. procedure triangular matrix multiplication(A, B: upper triangular n x n matrices, A = [ai}] , B=[bi}]) for i := I to n for j := i to n begin Cij:= 0 Answers to Odd-Numbered Exercises S-25 for k:= i to j Cij := Cij + aikbkj end 47. (AB)(B-1A-1) = A(BB-1)A-1 = AIA-1 = AA-1 = I. Similarly, (B-1A-1)(AB) = I. Hence, (ABrl = B-1A-1. 49. a) Let A <::) 0 = [bi}]. Then bi} = (ail 1\ 0) V ..• v (aip 1\ 0) = O. Hence, A <::) 0 = O. Similarly 0 <::) A = O. b) A v o = [aij V 0] = [ai}] = A. Hence A v 0 = A. Similarly 0 v A = A. c) A 1\ 0 = [aij 1\ 0] = [0] = O. Hence A 1\ 0 = O. Similarly 01\ A = O. 51. We assume that someone has chosen a positive integer less than 2n, which we are to guess. We ask the person to write the number in binary, using leading Os if necessary to make it n bits Ipng. We then ask "Is the first bit a I ?", "Is the second bit a I ?", "Is the third bit a 1 ?", and so on. After we know the answers to these n questions, we will know the number, because we will know its binary expansion. CHAPTER 4 Section 4.1 1. Let P(n) be the statement that the train stops at sta­ tion n. Basis step: We are told that P(I) is true. Induc­ tive step: We are told that P(n) implies P(n + 1) for each n � I. Therefore by the principle of mathematical induc­ tion, P(n) is true for all positive integers n. 3. a) 12 = I ·2·3/6 b) Both sides of P{l) shown in part (a) equal!. c) 12 + 22 + ... + � = k(k + 1)(2k + 1)/6 d) For each k � I that P(k) implies P(k + I); in other words, that as­ suming the inductive hypothesis [see part (c)] we can show 12 + 22 + ... + � + (k + 1)2 = (k + I)(k + 2)(2k + 3)/6 e)(tl + 22 + ... +�) + (k + 1)2 = [k(k + 1)(2k + 1)/6] + (k + 1)2 = [(k + 1)/6][k(2k + I) + 6(k + 1)] = [(k + 1)/6](2� + 7k + 6) = [(k + 1)/6](k + 2)(2k + 3) = (k + I)(k + 2)(2k + 3)/6 t) We have com­ pleted both the basis step and the inductive step, so by the principle of mathematical induction, the statement is true for every positive integer n. 5. Let P(n) be "12 + 32 + ... + (2n + 1)2 = (n + 1)(2n + 1)(2n + 3)/3." Basis step: P(O) is true because 12 = I = (0 + 1)(2·0 + 1)(2·0 + 3)/3. Inductive step: Assume that P(k) is true. Then 12+32+ ... +(2k+ 1)2 + [2(k+ 1)+ 1]2 = (k+l)(2k+ 1)(2k + 3)/3 + (2k + W � (2k + 3)[(k + 1)(2k+ 1)/3 + (2k + 3)] = (2k + 3)(2� + 9k + 10)/3 = (2k + 3)(2k + 5)(k + 2)/3 = [(k + I) + 1][2(k + 1) + 1][2(k + 1) + 3]/3. 7. Let P(n) be "I:;=o 3 . 5j = 3(5n+1 -1)/4." Basis step: P(O) is true because I:�=o 3· 5j = 3 = 3(51 -1)/4. Inductive step: Assume that I:�=o 3·51 = 3(5k+1 -1)/4. Then I:�!� 3· 5j = (I:�=o 3 ·5j) + 3 · 5k + 1 = 3(5k + 1 -1)/4 + 3 · 5k+1 = 3(5k+1 + 4· 5k+1 -1)/4 = 3(5k+2 -1)/4. 9. a) 2 + 4 + 6+ ... +2n = n(n + 1) b) Basis step: 2 = 1· (I + I) is true. Inductive step: Assume that 2 + 4 + 6 + ... + 2k = k(k + 1). Then (2 + 4 + 6 + ... + 2k) + 2(k + 1) = k(k + I) + 2(k + I) = (k + I)(k + 2). 11. a) I:J=I 1/2j = S-26 Answers to Odd-Numbered Exercises (2n -1)/2n b) Basis step: P(I) is true because ! = (21_ 1)/21. InduCtive step: Assume that L�=I 1/2j = (2k - 1)/2k Th "k+1 I ("k I ) I 2k_1 I . en L..Jj=1 2J = L..Jj=1 2J + 2k+f = 2k + 2k+f = 2k+�;;'T+I = 2�;�-;-1. 13. Let P(n) be "12 -22 + 32 -... + (_l)n-In2 = (-I)n-In(n + 1)/2." Basis step: P(I) is true because 12 = 1 = (_1)°12. Inductive step: Assume that P(k) is true. Then 12 - 22 + 32 -... + (_I)k-Ik2 + (-ll(k + 1)2 = (-I)k-Ik(k + 1)/2 + (-ll(k + Ii = (-I)k(k+ 1)[-k/2+(k+ 1)] = (-ll(k+ 1)[(k/2)+ 1] = (-I)k(k + 1)(k + 2)/2. 15. Let P(n) be "1 ·2 + 2·3 + ... + n(n + 1) = n(n + 1)(n + 2)/3." Basis step: P(I) is true because 1 ·2 = 2 = 1(1 + 1)(1 + 2)/3. Inductive step: As­ sume that P(k) is true. Then 1 ·2 + 2·3 + ... + k(k + 1) + (k + 1)(k + 2) = [k(k + 1)(k + 2)/3] + (k + 1)(k + 2) = (k + 1)(k + 2)[(k/3) + 1] = (k + 1)(k + 2)(k + 3)/3. 17. Let P(n) be the statement that 14 + 24 + 34 + ... + n4 = n(n + 1)(2n + 1)(3n2 + 3n -1)/30. P(I) is true because 1·2·3·5/30 = 1. Assume that P(k) is true. Then (14 + 24 + 34 + ... + k4) + (k + 1)4 = k(k + 1)(2k + 1)(3k2 + 3k- 1)/30 + (k + 1)4 = [(k + 1)/30][k(2k + 1)(3k2 + 3k -1) + 30(k + 1)3] = [(k + 1)/30](6k4 + 39k3 + 91� + 89k + 30) = [(k + 1)/30](k+ 2)(2k+ 3)[3(k+ 1)2 + 3(k + 1) -1]. This demonstrates that P(k + 1) is true. 19. a) 1 + ! < 2 -! b) This is true because 5/4 is less than 6/4. c) 1+ ! + ... + b < 2 -l d) For each k � 2 that P(k) im­ plies P(k + 1); in other words, we want to show that as­ suming the inductive hypothesis [see part (c)] we can show 1 I I I 2 I ) 1 I + 4 + ... + p + (k+l? < -HI e + 4+· .. + I I 2 I I 2 [I I] 2 p + (k+l)2 < -k + (k + 1)2 = - k -(k+l)2 = - [k2+2k+l-k] 2 k2+k I 2 I I k(HI)2 = -k(HI)2 -k(HI? = -HI -k(k+l)2 < 2 -k!1 t) We have completed both the basis step and the inductive step, so by the principle of mathematical induc­ tion, the statement is true for every integer n greater than 1. 21. Let P(n) be "2n > n2." Basis step: P(5) is true because 25 = 32 > 25 = 52. Inductive step: Assume that P(k) is true, that is, 2k > k2. Then 2H I = 2 . 2k > k2 + � > � + 4k � � + 2k + 1 = (k + 1)2 because k > 4. 23. By inspection we find that the inequality 2n + 3 ::::: 2n does not hold for n = 0, I, 2, 3. Let P(n) be the proposition that this inequality holds for the positive integer n. P(4), the basis case, is true because 2· 4 + 3 = 11 ::::: 16 = 24. For the inductive step assume that P(k) is true. Then, by the inductive hypoth­ esis, 2(k + 1) + 3 = (2k + 3) + 2 < 2k + 2. But because k � 1,2k +2::::: 2k +2k = 2k+l. This shows that P(k+ 1) is true. 25. Let P(n) be "1 + nh ::::: (1 + h)n, h > -I." Ba­ sis step: P(O) is true because 1 + 0 . h = 1 ::::: 1 = (1 + h)o. Inductive step: Assume 1 + kh ::::: (1 + h)k. Then because (1 + h) > 0, (1 + hl + I = (1 + h)(1 + hl � (1 + h)(1 + kh) = 1 +(k+ l)h +kh2 � 1 + (k+ l)h. 27. LetP(n)be "1/0 + 1/..[2 + 1/./3 + ... + 1/.,fo > 2 (v"1l+T -1)." Basis step: P(I) is true because 1 > 2 (.J2 -1). Inductive step: Assume that P(k) is true. Then 1 + 1/:.ti + ... + 1/..jk + 1/.Jk+T > 2 (.Jk+T - 1) + 1/.Jk+T. If we show that 2 (.Jk+T -1) + 1/.Jk+T > 2 (Jk + 2 -I), S-26 it follows that P(k + 1) is true. This inequality is equiv­ alent to 2 (Jk + 2 -Jk+T) < 1/.Jk+T, which is equivalent to 2 (Jk + 2 -Jk+T) (Jk + 2 + Jk+T) < .Jk+T/.Jk+T + Jk + 2/.Jk+T. This is equivalentt02 < 1 + Jk + 2/.Jk+T, which is clearly true. 29. Let P(n) be "H2o ::::: 1 + n." Basis step: P(O) is true because H2o = HI = 1 ::::: 1 + O. Inductive step: Assume that H2k ::::: 1 + k. h ,,2k+l I k ( I ) T en H2k+l = H2k+ L..Jj=2k+1 7 ::::: 1 + k + 2 2k+f < 1 + k + 1 = 1 + (k + 1). 31. Basis step: 12 + 1 = 2 is divisi­ ble by 2. Inductive step: Assume the inductive hypothesis, that k2 + k is divisible by 2. Then (k + 1)2 + (k + 1) = k2 + 2k + 1 + k + 1 = (k2 + k) + 2(k + I), the sum of a multiple of 2 (by the inductive hypothesis) and a multiple of 2 (by definition), hence, divisible by 2. 33. Let P(n) be "n5 - n is divisible by 5." Basis step: P(O) is true be­ cause 05 -0 = 0 is divisible by 5. Inductive step: Assume that P(k) is true, that is, k5 -5 is divisible by 5. Then (k + 1)5 -(k + 1) = (k5 + 5k4 + lOk3 + lOk2 + 5k + 1)­ (k + 1) = (k5 -k) + 5(k4 + 2k3 + 2k2 + k) is also divisi­ ble by 5, because both terms in this sum are divisible by 5. 35. Let P(n) be the proposition that (2n -1)2 - 1 is divisible by 8. The basis case P(I) is true because 8 I O. Now assume that P(k) is true. Because [(2(k + 1) -1]2 - 1 = [(2k -1)2 -1] + 8k, P(k + 1) is true because both terms on the right-hand side are divisible by 8. This shows that P(n) is true for all positive integers n, so m2 - 1 is divisible by 8 when­ ever m is an odd positive integer. 37. Basis step: 111+1 + 122.1-1 = 121 + 12 = 133 Inductive step: Assume the in­ ductive hypothesis, that 11 n+1 + 122n-1 is divisible by 133. Then l1(n+I)+1 + 122(n+I)-1 = 11 . lln+1 + 144. 122n-1 = 11 . 11n+1 + (11 + 133)· 122n-1 = ll(lln+1 + 122n-l) + 133 . 122n-l. The expression in parentheses is divisible' by 133 by the inductive hypothesis, and obviously the second term is divisible by 133, so the entire quantity is divisible by 133, as desired. 39. Basis step: Al £: BI tautologically implies that n;=, Aj £: n;=, Bj. Inductive step: Assume the inductive hypothesis that if Aj £: Bj for j = 1,2, ... , k, then n�=, Aj £: n�=, Bj. We want to show that if Aj £: Bj for j = 1,2, ... , k + I, then n�!: Aj c n�!: Bj. Let x be an arbitrary element of n�!: Aj = Cn�=, Aj) n AHI. Because x E n�=, Aj, we know by the inductive hypothe- sis that x E n�=, Bj; because x E AH!. we know from the given fact that Ak+1 £: Bk+1 that x E BHI. Therefore, x E (n�=, Bj) n BHI = n�!: Bj. 41. Let P(n) be "(AI U A2 U ... U An) n B = (AI n B) U (A2 n B) U ... U (An n B)." Basis step: P(I) is trivially true. Inductive step: Assume that P(k) is true. Then (AI U A2 U ... U Ak U AHd n B = [(AI U A2 U ... U Ak) U AHd n B = [(AI U A2 U ... U Ak) n B] U (AHI n B) = [(AI n B) U (A2 n B) U ... U (Ak n B)] U (Ak+1 n B) = (AI n B) U (A2 n B) U ... U (Ak n B) U (AHI n B). 43. Let P(n) be "U;=I Ak = n�=, Ak." Basis step: P(I) is trivially true. Inductive step: Assume that P(k) is true. Then U�!: Aj = (U�=I Aj) U AHI = ( k ) -(k -) - k+1 - Uj=1 Aj n Ak+l = nj=, Aj n Ak+1 = nj=, Aj. S-27 45. Let P(n) be the statement that a set with n elements has n(n -1)/2 two-element subsets. P(2), the basis case, is true, because a set with two elements has one subset with two elements-namely, itself-and 2(2 -1)/2 = 1. Now as­ sume that P(k) is true. Let S be a set with k + 1 elements. Choose an element a in S and let T = S -{a}. A two­ element subset of S either contains a or does not. Those subsets not containing a are the subsets of T with two ele­ ments; by the inductive hypothesis there are k(k -1)/2 of these. There are k subsets of S with two elements that contain a, because such a subset contains a and one of the k ele­ ments in T. Hence, there are k(k -1)/2 + k = (k + l)k/2 two-element subsets of S. This completes the inductive proof. 47. The two sets do not overlap ifn + 1 = 2. In fact, the conditional statement P(I) � P(2) is false. 49. The mistake is in applying the inductive hypothesis to look at max(x -1, Y -1), because even though x and y are pos­ itive integers, x-I and y - 1 need not be (one or both could be 0). 51. We use the notation (i, j) to mean the square in row i and column j and use induction on i + j to show that every square can be reached by the knight. Basis step: There are six base cases, for the cases when i + j ::: 2. The knight is already at (0,0) to start, so the empty sequence of moves reaches that square. To reach (1 , 0), the knight moves (0, 0) � (2, 1) � (0, 2) � (1,0). Similarly, to reach (0, 1), the knight moves (0, 0) � (1, 2) � (2, 0) � (0, 1). Note that the knight has reached (2,0) and (0,2) in the process. For the last basis step there is (0, 0) � (1, 2) � (2, 0) � (0, 1) � (2, 2) � (0, 3) � (1, 1). Inductive step: Assume the inductive hypothesis, that the knight can reach any square (i, j) for which i + j = k, where k is an integer greater than 1. We must show how the knight can reach each square (i, j) when i + j = k + 1. Because k + 1 ::: 3, at least one of i and j is at least 2. If i ::: 2, then by the inductive hypoth­ esis, there is a sequence of moves ending at (i -2, j + 1), because i -2 + j + 1 = i + j - 1 = k; from there it is just one step to (i, j); similarly, if j ::: 2. 53. Basis step: The base cases n = 0 and n = 1 are true because the derivative of xO is 0 and the derivative of x I = x is 1. Inductive step: Using the product rule, the inductive hypothesis, and the basis step shows that .!LxHI = .!L(x .xk) = X · .!Lxk +xk.!Lx = X· b b b b kxk-I + xk . 1 = kXk + xk = (k + l)xk. 55. Basis step: For k = 0, 1 == 1 (mod m). Inductive step: Suppose that a == b (mod m) and ak == bk (mod m); we must show that ak=1 == bHI (mod m). By Theorem 5 from Section 3.4, a · ak == b· bk (mod m), which by definition says that aHI == bk+1 (mod m). 57. Let P(n) be "[(pI � P2) /\ (P2 � P3) /\ ... /\ (Pn-I � Pn)] � [(PI /\ ... /\ Pn-I) � Pn]." Basis step: P(2) is true because (PI � P2) � (PI � P2) is a tautology. Inductive step: Assume P(k) is true. To show [(PI � P2) /\ ... /\ (Pk-I � Pk) /\ (Pk � PHI)] � [(PI /\ ... /\ Pk-I /\ Pk) � PHd is a tautology, as­ sume that the hypothesis of this conditional state­ ment is true. Because both the hypothesis and P(k) are true, it follows that (PI /\ ... /\ Pk-I) � Pk is true. Because this is true, and because Pk � PHI is true (it is part of the assumption) it follows by hypothetical syllogism that (PI /\ ... /\ Pk-t> � PHI is true. The weaker Answers to Odd-Numbered Exercises 8-27 statement (PI /\ ... /\ Pk-I /\ Pk) � PHI follows from this. 59. We will first prove the result when n is a power of 2, that is, ifn = 2k, k = 1,2, .... Let P(k) be the statement A ::: G, where A and G are the arithmetic and geometric means, re­ spectively, of a set of n = 2k positive real numbers. Basis step: k = 1 and n = 21 = 2. Note that (..[iii -,.jii2)2 ::: O. Expanding this shows that al -2.jala2 + a2 ::: 0, that is, (al + a2)/2 ::: (al a2)1/2. Inductive step: Assume that P(k) is true, with n = 2k. We will show that P(k + 1) is true. We have 2HI = 2n. Now (al + a2 + ... + a2n)/(2n) = [(at + a2 + ... + an)/n + (an+1 + an+2 + ... + a2n)/n]/2 and similarly (ala2'" a2n)I/(2n) = [(at ... an)l/n(an+1 ... a2n)l/n]I/2. To simplify the notation, let A(x,y, ... ) and G(x, y, ... ) denote the arithmetic mean and geometric mean of x, y, ... , respectively. Also, if x ::: x', y ::: y', and so on, then A(x,y, ... ) ::: A(x',y', ... ) and G(x,y, ... )::: G(x',y', ... ). Hence, A(aJ, ... , a2n) = A(A(al, ... ,an), A(an+I, ... , a2n» ::: A(G(al, ... , an), G(an+I, ... , a2n» ::: G(G(al, ... , an), G(an+J, ... , a2n» = G(al , ... , a2n). This finishes the proof for powers of 2. Now if n is not a power of 2, let m be the next higher power of 2, and let an+l, ... ,am all equal A(aJ, ... , an) = "ii. Then we have [(ala2 ... an)"iim-n]l/m ::: A(al, ... , am), because m is a power of 2. Because A(al,"" am) = "ii, it follows that (al ... an)l/m"iil-n/m ::: "iin/m. Raising both sides to the (m/n)th power gives G(al, ... ,an) ::: A(al, ... , an). 61. Basis step: For n = 1, the left-hand side is just t, which is 1. For n = 2, there are three nonempty subsets {I}, {2}, and {l, 2}, so the left-hand side is t + ! + /2 = 2. Inductive step: Assume that the statement is true for k. The set of the first k + 1 positive integers has many nonempty subsets, but they fall into three categories: a nonempty subset of the first k positive integers together with k + 1, a nonempty subset of the first k positive integers, or just {k + I}. By the inductive hypothesis, the sum of the first category is k. For the second category, we can factor out 1/( k + 1) from each term of the sum and what remains is just k by the inductive hypothesis, so this part of the sum is k/(k + 1). Finally, the third cate­ gory simply yields 1/(k + 1). Hence, the entire summation is k + k/(k + 1) + 1/(k + 1) = k + 1. 63. Basis step: If AI � A2, then AI satisfies the condition of being a subset of each set in the collection; otherwise A2 � At. so A2 satisfies the condition. Inductive step: Assume the inductive hypothe­ sis, that the conditional statement is true for k sets, and suppose we are given k + 1 sets that satisfy the given conditions. By the inductive hypothesis, there must be a set Ai for some i ::: k such that Ai � Aj for 1 ::: j ::: k. If Ai � AHI, then we are done. Otherwise, we know that Ak+ I � Ai, and this tells us that Ak+1 satisfies the condition of being a subset of Aj for 1 ::: j ::: k + 1. 65. G(I) = 0, G(2) = 1, G(3) = 3, G(4) = 4 67. To show that 2n ...., 4 calls are sufficient to exchange all the gossip, select persons 1,2,3, and 4 to be the central committee. Every person outside the central commit­ tee calls one person on the central committee. At this point the central committee members as a group know all the scan­ dals. They then exchange information among themselves by making the calls 1-2,3-4, 1-3, and 2-4 in that order. At this S-28 Answers to Odd-Numbered Exercises point, every central committee member knows all the scan­ dals. Finally, again every person outside the central committee calls one person on the central committee, at which point ev­ eryone knows all the scandals. [The total number of calls is (n -4) + 4 + (n -4) = 2n -4.] That this cannot be done with fewer than 2n -4 calls is much harder to prove; see the website http://www . cs. cornell. edu/vogels/ Epidemics/gossips-telephones .pdf for details. 69. We prove this by mathematical induction. The basis step (n = 2) is true tautologically. For n = 3, suppose that the intervals are (a, b), (c, d), and (e, j), where without loss of generality we can assume that a :s c :s e. Because (a, b) n (e, f) =1= 13, we must have e < b; for a similar reason, e < d. It follows that the number halfway between e and the smaller of b and d is common to all three intervals. Now for the inductive step, assume that whenever we have k intervals that have pairwise nonempty intersections then there is a point common to all the intervals, and suppose that we are given intervals h, /z, ... ,h+1 that have pairwise nonempty intersections. For each i from I to k, let Ji = Ii n h+l. We claim that the collection JI, ]Z, ... , Jk satisfies the inductive hypothesis, that is, that Jit n Ji2 =1= 13 for each choice of sub­ scripts il and i2' This follows from the n = 3 case proved above, using the sets IiI' Ii2, and Ik+l. We can now invoke the inductive hypothesis to conclude that there is a number com­ mon to all of the sets Ji for i = I, 2, ... , k, which perforce is in the intersection of all the sets Ii for i = 1, 2, ... , k + 1. .71. Pair up the people. Have the people stand at mutually distinct small distances from their partners but far away from everyone else. Then each person throws a pie at his or her partner, so everyone gets hit. 73. 75. Let P(n) be the statementthat every 2n x 2n x 2n checker­ board with a I x 1 x 1 cube .removed can be covered by tiles that are 2 x 2 x 2 cubes each with a 1 x 1 x 1 cube removed. The basis step, P (1 ), holds because one tile coincides with the solid to be tiled. Now assume that P(k) holds. Now consider a 2k+ I x 2k+ I x 2k+ I cube with a 1 x 1 x I cube removed. Split this object into eight pieces using planes pamllel to its faces and running through its center. The missing 1 x 1 x 1 piece occurs in one of these eight pieces. Now position one tile with its center at the center of the large object so that the missing 1 x 1 x 1 cube lies in the octant in which the large object is missing a 1 x 1 x 1 cube. This creates eight 2k x 2k X 2k cubes, each missing a 1 x I x I cube. By the inductive hypothesis we can fill each of these eight objects with tiles. Putting these tilings together produces the desired tiling. S-28 77. 79. Let Q(n) be P(n + b -I). The statementthat P(n) is true for n = b, b + 1, b + 2, ... is the same as the statement that Q(m) is true for all positive integers m. We are given that P(b) is true [i.e., that Q(l) is true], and that P(k) -+ P(k + 1) for all k 2: b [i.e., that Q(m) -+ Q(m + 1) for all positive inte­ gers m]. Therefore, by the principle of mathematical induc­ tion, Q(m) is true for all positive integers m. Section 4.2 1. Basis step: We are told we can run one mile, so P(l) is true. Inductive step: Assume the inductive hypothesis, that we can run any number of miles from 1 to k. We must show that we can run k + 1 miles. If k = 1, then we are already told that we can run two miles. If k > 1, then the inductive hypothesis tells us that we can run k - I miles, so we can run (k -1) + 2 = k + 1 miles. 3. a) P(8) is true, because we can form 8 cents of postage with one 3-cent stamp and one 5-cent stamp. P(9) is true, because we can form 9 cents of postage with three 3-cent stamps. P(lO) is true, because we can form 10 cents of postage with two 5-cent stamps. b) The statement that using just 3-cent and 5-cent stamps we can form j cents postage for all j with 8 :s j :s k, where we assume that k 2: 10 c) Assuming the inductive hypothesis, we can form k + 1 cents postage using just 3-cent and 5-cent stamps d) Because k 2: 10, we know that P(k -2) is true, that is, that we can form k -2 cents of postage. Put one more 3-cent stamp on the envelope, and we have formed k + 1 cents of postage. e) We have completed both the basis step and the inductive step, so by the principle of strong induction, the statement is true for every integer n greater than or equal to 8. 5. a) 4, 8, 11, 12, 15, 16, 19,20,22,23,24,26,27, 28, and all values greater than or equal to 30 b) Let P(n) be the statement that we can form n cents of postage using just 4-cent and II-cent stamps. We want to prove that P(n) is true for all n 2: 30. For the basis step, 30 = 11 + 11 + 4 + 4. Assume that we can form k cents of postage (the inductive hy­ pothesis); we will show how to form k + 1 cents of postage. If the k cents included an II-cent stamp, then replace it by three 4-cent stamps. Otherwise, k cents was formed from just 4-cent stamps. Because k 2: 30, there must be at least eight 4-cent stamps involved. Replace eight 4-cent stamps by three II-cent stamps, and we have formed k + 1 cents in postage. c) P(n) is the same as in part (b). To prove that P(n) is true for all n 2: 30, we check for the basis step that S-29 30 = 11 + 11 + 4 + 4, 31 = 11 + 4 + 4 + 4 + 4 + 4, 32 = 4+4+4+4+4+4+4+4 ,and33 = 11 + 11 + 11. For the inductive step, assume the inductive hypothesis, that P(j) is true for all j with 30 ::: j ::: k, where k is an arbitrary integer greater than or equal to 33. We want to show that P(k + 1) is true. Because k - 3 � 30, we know that P(k -3) is true, that is, that we can form k - 3 cents of postage. Put one more 4- cent stamp on the envelope, and we have formed k + 1 cents of postage. In this proof, our inductive hypothesis was that P(j) was true for all values of j between 30 and k inclu­ sive, rather than just that P(30) was true. 7. We can form all amounts except $1 and $3. Let P (n) be the statement that we can form n dollars using just 2-dollar and 5-dollar bills. We want to prove that P(n) is true for all n � 5. (It is clear that $1 and $3 cannot be formed and that $2 and $4 can be formed.) For the basis step, note that 5 = 5 and 6 = 2 + 2 + 2. Assume the inductive hypothesis, that P(j) is true for all j with 5 ::: j ::: k, where k is an arbitrary integer greater than or equal to 6. We want to show that P(k + 1) is true. Be­ cause k -1 � 5, we know that P(k -1) is true, that is, that we can form k - 1 dollars. Add another 2-dollar bill, and we haveformedk + 1 dollars. 9. Let P(n) be the statement that there is no positive integer b such that"fi = nib. Basis step: P(I) is true because"fi > 1 � lib for all positive integers b. Inductive step: Assume that P(j) is true for all j ::: k, where k is an arbitrary positive integer; we prove that P(k + 1) is true by contradiction. Assume that"fi = (k + 1)lb for some positive integer b. Then 2b2 = (k + 1)2, so (k + 1)2 is even, and hence, k + 1 is even. So write k + 1 = 2t for some pos­ itive integer t, whence 2b2 = 4t2 and b2 = 2t2. By the same reasoning as before, b is even, so b = 2s for some positive in­ teger s. Then"fi = (k + 1)lb = (2t)/(2s) = tis. Butt ::: k, so this contradicts the inductive hypothesis, and our proof of the inductive step is complete. 11. Basis step: There are four base cases. If n = 1 = 4 . 0 + 1, then clearly the second player wins. If there are two, three, or four matches (n = 4 . 0 + 2, n = 4 . 0 + 3, or n = 4 . 1), then the first player can win by removing all but one match. Inductive step: Assume the strong inductive hypothesis, that in games with k or fewer matches, the first player can win if k == 0, 2, or 3 (mod 4) and the second player can win if k == 1 (mod 4). Suppose we have a game with k + 1 matches, with k � 4. If k + 1 == o (mod 4), then the first player can remove three matches, leaving k - 2 matches for the other player. Because k - 2 == 1 (mod 4), by the inductive hypothesis, this is a game that the second player at that point (who is the first player in our game) can win. Similarly, if k + 1 == 2 (mod 4), then the first player can remove one match; and if k + 1 == 3 (mod 4), then the first player can remove two matches. Finally, if k + 1 == 1 (mod 4), then the first player must leave k, k -1, or k - 2 matches for the other player. Because k == 0 (mod 4), k -1 == 3 (mod 4), and k - 2 == 2 (mod 4), so by the inductive hypothesis, this is a game that the first player at that point (who is the second player in our game) can win. 13. Let P(n) be the statement that exactly n - 1 moves are required to assemble a puzzle with n pieces. Now P(I) is trivially true. Assume that P(j) is true for all j ::: k, and Answers to Odd-Numbered Exercises S-29 consider a puzzle with k + 1 pieces. The final move must be the joining of two blocks, of size j and k + 1 - j for some integer j with 1 ::: j ::: k. By the inductive hypothe­ sis, it required j - 1 moves to construct the one block, and k + 1 - j - 1 = k - j moves to construct the other. There­ fore, 1 + (j -1) + (k -j) = k moves are required in all, so P(k + 1) is true. 15. Let the Chomp board have n rows and n columns. We claim that the first player can win the game by making the first move to leave just the top row and leftmost column. Let P(n) be the statement that if a player has presented his opponent with a Chomp configuration con­ sisting of just n cookies in the top row and n cookies in the leftmost column, then he can win the game. We will prove VnP(n) by strong induction. We know that P{l) is true, be­ cause the opponent is forced to take the poisoned cookie at his first turn . Fix k � 1 and assume that P(j) is true for all j ::: k. We claim that P(k + 1) is true. It is the opponent's turn to move. If she picks the poisoned cookie, then the game is over and she loses. Otherwise, assume she picks the cookie in the top row in column j, or the cookie in the left column in row j, for some j with 2 ::: j ::: k + 1. The first player now picks the cookie in the left column in row j, or the cookie in the top row in column j, respectively. This leaves the position covered by P(j -1) for his opponent, so by the inductive hy­ pothesis, he can win. 17. Let P(n) be the statement that if a simple polygon with n sides is triangulated, then at least two of the triangles in the triangulation have two sides that border the exterior of the polygon. We will prove "In � 4 P(n). The statement is clearly true for n = 4, because there is only one diagonal, leaving two triangles with the desired property. Fix k � 4 and assume that P(j) is true for all j with 4 ::: j ::: k. Consider a polygon with k + 1 sides, and some triangulation of it. Pick one of the diagonals in this triangulation. First sup­ pose that this diagonal divides the polygon into one triangle and one polygon with k sides. Then the triangle has two sides that border the exterior. Furthermore, the k-gon has, by the inductive hypothe sis, two triangles that have two sides that border the exterior of that k-gon, and only one of these trian­ gles can fail to be a triangle that has two sides that border the exterior of the original polygon. The only other case is that this diagonal divides the polygon into two polygons with j sides and k + 3 - j sides for some j with 4 ::: j ::: k -1. By the inductive hypothesis, each of these two polygons has two triangles that have two sides that border their exterior, and in each case only one of these triangles can fail to be a trian­ gle that has two sides that border the exterior of the original polygon. 19. Let P(n) be the statement that the area of a simple polygon with n sides and vertices all at lattice points is given by I(P) + B(P)/2 -1. We will prove P(n) for all n � 3. We begin with an additivity lemma: If P is a simple polygon with all vertices at the lattice points, divided into poly­ gons PI and P2 by a diagonal, then I(P) + B(P)/2 - 1 = [I(PI) + B(PI)/2 -1] + [I(P2) + B(P2)/2 -1]. To prove this, suppose there are k lattice points on the diagonal, not counting its endpoints. Then I(P) = I(PI) + I(P2) + k and B(P) = B(PI) + B(P2) -2k -2; and the result follows by simple algebra. What this says in particular is that if Pick's S-30 Answers to Odd-Nwnbered Exercises formula gives the correct area for PI and P2, then it must give the correct formula for P, whose area is the sum of the areas for PI and P2; and similarly if Pick's formula gives the correct area for P and one of the Pi'S, then it must give the correct formula for the other Pi. Next we prove the the­ orem for rectangles whose sides are parallel to the coordi­ nate axes. Such a rectangle necessarily has vertices at (a, b), (a, c), (d, b), and (d, c), where a, b, c, and d are integers with b < c and a < d. Its area is (c -b)(d -a). Also, B = 2(c - b + d - a) and 1= (c - b -1)(d -a-I) = (c -b)(d­ a) -(c -b) -(d - a) + 1. Therefore, I + B/2 - 1 = (c­ b)(d - a) - (c -b) -(d - a) + 1 + (c - b + d - a) - 1 = (c - b)(d - a), which is the desired area. Next consider a right triangle whose legs are parallel to the coordinate axes. This triangle is half a rectangle of the type just considered, for which Pick's formula holds, so by the additivity lemma, it holds for the triangle as well. (The values of B and I are the same for each of the two triangles, so if Picks's formula gave an answer that was either too small or too large, then it would give a correspondingly wrong answer for the rect­ angle.) For the next step, consider an arbitrary triangle with vertices at the lattice points that is not of the type already con­ sidered. Embed it in as small a rectangle as possible. There are several possible ways this can happen, but in any case (and adding one more edge in one case), the rectangle will have been partitioned into the given triangle and two or three right triangles with sides parallel to the coordinate axes. Again by the additivity lemma, we are guaranteed that Pick's formula gives the correct area for the given triangle. This completes the proof of P (3), the basis step in our strong induction proof. For the inductive step, given an arbitrary polygon, use Lemma 1 in the text to split it into two polygons. Then by the addi­ tivity lemma above and the inductive hypothesis, we know that Pick's formula gives the correct area for this polygon. 21. a) In the left figure Labp is smallest, but bp is not an in­ terior diagonal. b) In the right figure bd is not an interior diagonal. c) In the right figure bd is not an interior diago­ nal. 23. a) When we try to prove the inductive step and find a triangle in each subpolygon with at least two sides bordering the exterior, it may happen in each case that the triangle we are guaranteed in fact borders the diagonal (which is part of the boundary of that polygon). This leaves us with no trian­ gles guaranteed to touch the boundary of the original polygon. b) We proved the stronger statement "In::: 4 T(n) in Exercise 17. 25. a) The inductive step here allows us to conclude that P(3), P(5), ... are all true, but we can conclude nothing about P(2), P(4), .... b) P(n) is true for all positive integers n, using strong induction. c) The inductive step here enables us to conclude that P(2), P(4), P(8), P(16), ... are all true, but we can conclude nothing about P (n) when n is not a power of 2. d) This is mathematical induction; we can conclude that P(n) is true for all positive integers n. 27. Suppose, for a proofby contradiction, that there is some positive integer n such that P(n) is not true. Let m be the smallest positive inte­ ger greater than n for which P(m) is true; we know that such an m exists because P(m) is true for infinitely many values of m. But we know that P(m) -+ P(m -1), so P(m -1) is also S-30 true. Thus, m - 1 cannot be greater than n, so m - 1 = n and P(n) is in fact true. This contradiction shows that P(n) is true for all n. 29. The error is in going from the base case n = 0 to the next case, n = 1; we cannot write 1 as the sum of two smallernatural numbers. 31. Assume that the well-ordering property holds. Suppose that P(l) is true and that the condi­ tional statement [P(I) /\ P(2) /\ ... /\ P(n)] -+ P(n + 1) is true for every positive integer n. Let S be the set of positive integers n for which P( n) is false. We will show S = 0. As­ sume that S =1= 0. Then by the well-ordering property there is a least integer m in S. We know that m cannot be 1 because P(l) is true. Because n = m is the least integer such that P(n) is false, P(I), P(2), ... , P(m -1) are true, and m - 1 ::: 1. Because [P(I) /\ P(2) /\ ... /\ P(m -1)] -+ P(m) is true, it follows that P (m ) must also be true, which is a contradiction. Hence, S = 0. 33. In each case, give a proof by contradic­ tion based on a "smallest counterexample," that is, values of n and k such that P(n, k) is not true and n and k are small­ est in some sense. a) Choose a counterexample with n + k as small as possible. We cannot have n = 1 and k = 1, because we are given that P(I, 1) is true. Therefore, either n > 1 or k > 1. In the former case, by our choice of counterexam­ ple, we know that P(n -1, k) is true. But the inductive step then forces P(n, k) to be true, a contradiction. b) Choose a counterexample with n as small as possible. We cannot have n = 1, because we are given that P(l, k) is true for all k. Therefore, n > 1. By our choice of counterexamp le, we know that P(n -1, k) is true. But the inductive step then forces P(n, k) to be true, a contradiction. c) Choose a counterex­ ample with k as small as possible. We cannot have k = 1, because we are given that P(n, 1) is true for all n. There­ fore, k > 1. By our choice of counterexamp le, we know that P (n, k -1) is true. But the inductive step then forces P (n, k) to be true, a contradiction. 35. Let P(n) be the statement that if XI, X2, •.• , Xn are n distinct real numbers, then n - 1 multiplications are used to find the product of these numbers no matter how parentheses are inserted in the product. We will prove that P(n) is true using strong induction. The ba­ sis case P(I) is true because 1 - 1 = 0 multiplications are required to find the product of x), a product with only one factor. Suppose that P(k) is true for 1 ::: k ::: n. The last mul­ tiplication used to find the product of the n + 1 distinct real numbers XI, X2, ••• , Xn, Xn+1 is a multiplication of the prod­ uct of the first k of these numbers for some k and the prod­ uct of the last n + 1 - k of them. By the inductive hypoth­ esis, k - 1 multiplications are used to find the product of k of the numbers, no matter how parentheses were inserted in the product of these numbers, and n - k multiplications are used to find the product of the other n + 1 - k of them, no matter how parentheses were inserted in the product of these numbers. Because one more multiplication is required to find the product of all n + 1 numbers, the total number of mul­ tiplications used equals (k -1) + (n -k) + 1 = n. Hence, P(n + 1) is true. 37. Assume that a = dq + r = dq' + r' with 0 ::: r < d and 0 ::: r' < d. Then d(q -q') = r' -r. It follows that d divides r' -r. Because -d < r' - r < d, we have r' - r = o. Hence, r' = r. It follows that q = q'. S-3J 39. This is a paradox caused by self-reference. The answer is clearly "no." There are a finite number of English words, so only a finite number of strings of 15 words or fewer; there­ fore, only a finite number of positive integers can be so de­ scribed, not all of them. 41. Suppose that the well-ordering property were false. Let S be a nonempty set of nonnegative integers that has no least element. Let P(n) be the statement "i f/. S for i = 0, 1, ... , n." P(O) is true because if 0 E S then S has a least element, namely, O. Now suppose that P(n) is true. Thus, 0 f/. s, 1 f/. s, ... , n f/. S. Clearly, n + 1 cannot be in S, for if it were, it would be its least element. Thus P (n + 1) is true. So by the principle of mathematical induction, n f/. S for all nonnegative integers n. Thus, S = 0, a contradiction. 43. This follows immediately from Exercise 41 (if we take the principle of mathematical induction as an axiom) and from this exercise together with the discussion following the for­ mal statement of strong induction in the text, which showed that strong induction implies the principle of mathematical induction (if we take strong induction as an axiom). Section 4.3 1. a) f(1) = 3, f(2) = 5, f(3) = 7, f(4) = 9 b) f(l) = 3, f(2) = 9, f(3) = 27, f(4) = 81 c) f(l) = 2, f(2) = 4, f(3) = 16, f(4) = 65,536 d) f(1) = 3, f(2) = 13, f(3) = 183, f(4) = 33,673 3. a) f(2) = -1, f(3) = 5, f(4) = 2, f(5) = 17 b) f(2) = -4, f(3) = 32, f(4) = -4096, f(5) = 536,870,912 c) f(2) = 8, f(3) = 176, f(4) = 92,672, f(5) = 25,764,174,848 d) f(2) = -!, f(3) = -4, f(4) = 4, f(5) = -32 5. a) Not valid b) f(n) = 1 - n. Basis step: f(O) = 1 = 1 -O. Inductive step: if f(k) = 1 -k, then f(k + 1) = f(k) - 1 = 1 - k - 1 = 1- (k + 1). c) f(n) = 4 - n if n > 0, and f(O) = 2. Basis step: f(O) = 2 and f(l) = 3 = 4 -1. Inductive step (with k � 1): f(k + 1) = f(k) - 1 = (4 -k) - 1 = 4 -(k + 1). d) f(n) = 2L{n+I)/2j. Basis step: f(O) = 1 = 2l{0+1)/2j and f(l) = 2 = 2l{1+I)/2j. Inductive step (with k � 1): f(k + 1) = 2f(k -1) = 2. 2lk/2j = 2lk/2J+I = 2l{{k+1)+1)/2j. e) f(n) = 3n. Basis step: Trivial. Inductive step: For odd n, f(n) = 3f(n -1) = 3 . 3n-1 = 3n; and for even n > 1, f(n) = 9 f(n -2) = 9 . 3n-2 = 3n. 7. There are many possible correct answers. We will supply relatively simple ones. a) an+1 = an + 6 for n � 1 and al = 6 b) an+1 = an + 2 for n � 1 and al = 3 c) an+1 = Wan for n � 1 andal = 10 d) an+1 = an forn � 1 andal = 5 9. F(O) = 0, F(n) = F(n -1) + n for n � 1 11. Pm(O) = 0, Pm(n + 1) = Pm(n) + m 13. Let P(n) be "It + h + ... + fin-I = f2n'" Basis step: P(I) is true because It = 1 = fi. Inductive step: Assume that P(k) is true. Then fl + f3 + ... + 12k-I + fik+1 = fik + fik+1 = fik+2 + h{k+I)' 15. Basis step: fofl + flfi = O· 1 + 1 . 1 = 12 = ff. Inductive step: Assume that folt + fd2 + ... + f2k-d2k = flk' Then foft + fd2 + ... + f2k-lfik + f2d2k+1 + fik+d2k+2 = flk + fid2k + I + fik + d2k+2 = hk(f2k + fik+l) + fik + d2k+2 = hd2k+2 + fik+d2kH = (f2k + fik+l)fik+2 = flkH' 17. The number of divisions Answers to Odd-Numbered Exercises S-31 used by the Euclidean algorithm to find gcd(fn+1' fn) is o for n = 0, 1 for n = 1, and n - 1 for n � 2. To prove this result for n � 2 we use mathematical induction. For n = 2, one division shows that gcd(/3, h) = gcd(2, 1) = gcd(l, 0) = 1. Now assume that k - 1 divisions are used to find gcd(fk+I. Jic). To find gcd(fk+2. fk+I), first divide fk+2 by fk+1 to obtain fk+2 = 1 . fk+1 + fk. After one div­ ision we have gcd(fk+2, fk+l) = gcd(fk+1t fk). By the inductive hypothesis it follows that exactly k - 1 more di­ visions are required. This shows that k divisions are re­ quired to find gcd(fk+2. fk+I), finishing the inductive proof. 19.IAI = -1. Hence, IAnl = (_I)n. It follows that fn+dn-I -/; = (_I)n. 21. a) Proof by induction. Basis step: For n = 1, max( -al) = -al = -min(al)' For n = 2, there are two cases. If a2 � alt then -al � -a2, so max( -alt -a2) = -al = -rnin(al' a2). If a2 < alt then -al < -a2, so max( -al. -a2) = -a2 = -min(al. a2). Inductive ste p: Assume true for k with k � 2. Then max( -al. -a2 •...• -ak. -ak+l) = max(max( -al •...• -ak). -ak+l) = max(-min(al, ... ,ak).-ak+1) = -min(min (ai, ... ,ak), ak+l) = -min(al' ... ,ak+I). b) Proofby mathematical induction. Basis step: For n = 1, the result is the identity al + bl = al + bl. For n = 2, first con­ sider the case in which al + bl � a2 + b2. Then max(al + bl. a2 + b2) = al + bl. Also note that al :::: max(al. a2) and bl :::: max(bl• b2), so al + bl :::: max(al. a2) + max(bl• b2). Therefore,max(al + bl• a2 +b2) = al +bl :::: max(al. a2) + max(bl' b2). The case with al + bl < a2 + b2 is simi­ lar. Inductive ste p: Assume that the result is true for k. Then max(al + bl.a2 + b2 •...• ak + bk.ak+1 +bk+l) = max(max(al + bit a2 + b2 •...• ak + bk). ak+1 + bk+l) :::: max(max(a l. a2, ... ,ak) + max(bl' b2 •... ,bk), ak + I + bk+l) :::: max(max (al. a2.·.·, ak), ak+l) + max(max(b l, b2, ...• bk), bk+1) = max(al. a2, ...• at, ak+1) + max(bl, b2, ... , bk. bk+I)' c) Same as part (b), but replace every occurrence of "max" by "min" and invert each inequality. 23.5ES, and X +YES if X.yES. 25.a)OES, and if XES, then x + 2 E S and x - 2 E S. b) 2 E S, and if XES, then x + 3 E S. c) 1 E S,2 E S. 3 E S, 4 E S, and if X E S, then X + 5 E S . 27. a) (0,1), (1,1), (2,1); (0,2), (1,2), (2,2), (3,2), (4,2); (0.3), (1.3), (2.3), (3.3), (4.3), (5.3), (6.3); (0.4), (1.4), (2.4), (3.4), (4.4), (5.4), (6.4), (7.4), (8.4) b) Let P(n) be the statement that a :::: 2b whenever (a. b) E S is obtained by n applications of the recursive step. Basis step: P(O) is true, because the only element of S obtained with no applications of the recur­ sive' step is (0, 0), and indeed 0 :::: 2 . O. Inductive step: As­ sume that a :::: 2b whenever (a, b) E S is obtained by k or fewer applications of the recursive step, and consider an element obtained with k + 1 applications of the recursive step. Because the final application of the recursive step to an element (a, b) must be applied to an element obtained with fewer applications of the recursive step, we know that a :::: 2b. Add 0 :::: 2, 1 :::: 2, and 2 :::: 2, respective ly, to obtain a :::: 2(b + 1),a + 1 :::: 2(b + 1), and a + 2 :::: 2(b + 1),asde­ sired. c) This holds for the basis step, because 0 :::: O. If this holds for (a. b), then it also holds for the elements obtained S-32 Answers to Odd-Numbered Exercises from (a, b) in the recursive step, because adding 0 � 2, 1 � 2, and 2 � 2, respective ly, to a � 2b yields a � 2( b + 1), a + I � 2(b + 1), and a + 2 � 2(b + I). 29. a) Define S by (1, 1) E S, and if (a, b) E S, then (a + 2, b) E S, (a, b + 2) E S, and (a + I, b + 1) E S. All elements put in S satisfY the condition, because (1, 1) has an even sum of coor­ dinates, and if(a, b) has an even sum of coordinates, then so do (a + 2, b), (a, b + 2), and (a +1, b + I). Conversely, we show by induction on the sum of the coordinates that if a + b is even, then (a, b) E S. If the sum is 2, then (a, b) = (1,1), and the basis step put (a, b) into S. Otherwise the sum is at least 4, and at least one of (a - 2, b), (a, b - 2), and (a -I, b -1) must have positive integer coordinates whose sum is an even number smaller than a + b, and therefore must be in S. Then one application of the recursive step shows that (a, b) E S. b) Define S by (1,1), (1, 2), and (2,1) are in S, and if (a, b) E S, then (a + 2, b) and (a, b + 2) are in S. To prove that our definition works, we note first that (1, 1), (1,2), and (2, 1) all have an odd coordinate, and if (a, b) has an odd coordinate, then so do (a + 2, b) and (a, b + 2). Con­ versely, we show by induction on the sum of the coordinates that if (a, b) has at least one odd coordinate, then (a, b) E S. If (a, b) = (1, I) or (a, b) = (1, 2) or (a, b) = (2,1), then the basis step put (a, b) into S. Otherwise either a or b is at least 3, so at least one of (a -2, b) and (a, b -2) must have positive integer coordinates whose sum is smaller than a + b, and therefore must be in S. Then one application of the recursive step shows that (a, b) E S. c) (1, 6) E S and (2,3) E S, and if (a, b) E S, then (a + 2, b) E S and (a, b + 6) E S. To prove that our definition works, we note first that (1,6) and (2,3) satisfY the condition, and if (a, b) satisfies the condition, then so do (a + 2, b) and (a, b + 6). Conversely we show by induction on the sum of the coor­ dinates that if (a, b) satisfies the condition, then (a, b) E S. For sums 5 and 7, the only points are (1,6), which the basis step put into S, (2,3), which the basis step putinto S, and (4,3) = (2 + 2, 3), which is in S by one application of the recursive definition. For a sum greater than 7, either a :::: 3, or a � 2 and b :::: 9, in which case either (a - 2, b) or (a, b - 6) must have positive integer coordinates whose sum is smaller than a + b and satisfY the condition for being in S. Then one application of the recursive step shows that (a, b) E S. 31. If x is a set or a variable representing a set, then x is a well-formed formula. If x and y are well-formed formu­ lae, then so are X, (x U y), (x n y), and (x -y). 33. a) If xED = to, 1,2,3,4,5,6,7,8, 9}, then m(x) = x; if s = tx, where t E D* and xED, then m(s) = min(m(s), x). b) Let t = wx, where w E D* and XED. If w = A, then m(st) = m(sx) = min(m(s), x) = min(m(s), m(x» by the recursive step and the basis step of the definition of m. Otherwise, m(st) = m«sw)x) = min(m(sw), x) by the def­ inition of m. Now m(sw) = min(m(s), m(w» by the in­ ductive hypothe sis of the structural induction, so m(st) = min(min(m (s), m(w», x) = min(m(s), min(m(w), x»by the meaning of min. But min(m(w), x) = m(wx) = m(t) by the recursive step of the definition of m. Thus, m(st) = min(m(s), m(t». 35. A R = A and (ux)R = xuR for x E :E, u E :E*. 37. wo = A and wn+1 = wwn• 39. When the S-32 string consists of n Os followed by n I s for some non­ negative integer n 41. Let P(i) be "/(wi) = i ·/(w)." P(O) is true because I(wo) = 0 = 0 ·/(w). Assume P(i) is true. Then l(wHI) = I(wwi) = I(w) + I(wi) = I(w) + i ·/(w) = (i + I)· I(w). 43. Basis step: For the full binary tree con­ sisting of just a root the result is true because n(T) = I and h(T) = 0, and 1 :::: 2 . 0 + 1. Inductive step: Assume that n(TI) :::: 2h(TI) + I andn(T2) :::: 2h(T2) + 1. By the recursive definitions of n(T) and h(T), we have n(T) = 1+ n(TI) + n(T2) and h(T) = I + max(h(Td, h(T2». Therefore n(T) = 1+ n(TI) + n(T2) :::: 1+ 2h(TI) + 1 + 2h(T2) + I :::: 1 + 2· max(h(TI), h(T2» + 2 = I + 2 (max(h(Td, h(T2» + I) = 1 + 2h(T). 45. Basis step: ao.o = 0 = 0 + O . Inductive step: Assume that am',n' = m' + n' whenever (m', n') is less than (m, n) in the lexicographic ordering of N x N. If n = 0 then am,n = am-I,n + I = m - I + n + I = m + n. If n > 0, then am,n = am,n-I + 1= m + n -1+1 = m + n. 47. a) Pm.m = Pm because a number exceeding m cannot be used in a partition of m. b) Because there is only one way to partition 1, namely, 1 = I, it follows that Pl,n = 1. Because there is only one way to partition m into Is, Pm,l = 1. When n > m it follows that P m.n = P m,m because a number exceed­ ing m cannot be used. Pm,m = 1+ Pm,m-I because one extra partition, namely, m = m, arises when m is allowed in the par­ tition. Pm,n = Pm,n-I + Pm-n,n ifm > n because a partition of m into integers not exceeding n either does not use any n sand hence, is counted in Pm,n-I or else uses an n and a partition of m -n,andhence, iscountedinPm_n,n' c) Ps = 7, P6 = 11 49. Let P(n) be "A(n, 2) = 4." Basis step: P(I) is true be­ cause A(1, 2) = A(O, A(I, 1» = A(O, 2) = 2 · 2 = 4 . Induc­ tive step: Assume that P(n) is true, that is, A(n, 2) = 4. Then A(n + 1,2) = A(n, A(n + 1, 1» = A(n, 2) = 4. 51. a) 16 b) 65,536 53. Use a double induction argument to prove the stronger statement: A(m, k) > A(m, I) when k > I. Ba­ sis step: When m = 0 the statement is true because k > 1 implies that A(O, k) = 2k > 21 = A(O, I). Inductive step: Assume that A(m, x) > A(m, y) for all nonnegative inte­ gers x and y with x > y. We will show that this implies that A(m + 1, k) > A(m + 1, I) if k > I. Basis steps: When 1 = Oandk > 0, A(m + I, I) = o and either A(m + I, k) = 2 or A(m + I, k) = A(m, A(m + I, k -I». If m = 0, this is 2A(I, k -1) = 2k. If m > 0, this is greater than 0 by the inductive hypothesis. In all cases, A(m + I, k) > 0, and in fact, A(m + 1, k) :::: 2. If 1 = 1 and k> I, then A(m + I, I) = 2 and A(m + 1, k) = A(m, A(m + I, k -1», with A(m + I, k -1) :::: 2. Hence, by the inductive hypoth­ esis, A(m, A(m + 1, k -I»:::: A(m, 2) > A(m, I) = 2. In­ ductive step: Assume that A(m + l,r) > A(m + l,s) for all r > s, s = 0, 1, ... , I. Then if k + 1 > 1 + 1 it follows that A(m + 1, k + I) = A(m, A(m + 1, k» > A(m, A(m + 1, k» = A(m + 1,1 + 1). 55. From Exercise 54 it fol­ lows that A(i, j):::: A(i -I, j):::: ... :::: A(O, j) = 2j :::: j. 57. Let P(n) be "F(n) is well-defined." Then P(O) is true be­ cause F(O) is specified. Assume that P(k) is true for all k < n. Then F(n) is well-defined at n because F(n) is given in terms of F(O), F(I), ... , F(n -1). So P(n) is true for all integersn. 59. a) The value of F(1) is ambiguous. b) F(2) is not de­ fined because F(O) is not defined. c) F(3) is ambiguous 5-33 and F (4) is not defined because F (�) makes no sense. d) The definition of F(I) is ambiguous because both the second and third clause seem to apply. e) F(2) cannot be computed because trying to compute F(2) gives F(2) = 1 + F(F(I» = 1 + F(2). 61. a) 1 b) 2 c) 3 d) 3 e)4 t)4 g)5 63. fo*(n) = fn/al 65. f2*(n) = flog log n 1£or n � 2, fz*(l) = 0 Section 4.4 1. First, we use the recursive step to write 5! = 5 ·4L We then use the recursive step repeatedly to write 4! = 4 . 3!, 3! = 3· 2!, 2! = 2· I!, and I! = 1 ·OL Inserting the value of O! = 1, and working back through the steps, we see that I! = 1 . 1 = 1, 2! = 2· 1! = 2·1 = 2, 3! = 3· 2! = 3·2 = 6, 4! = 4· 3! = 4·6 = 24, and 5! = 5· 4! = 5 ·24 = 120. 3. First, because n = 11 is odd, we use the else clause to see that mpower(3, 11,5) = (mpower (3, 5, 5)2 mod 5· 3 mod 5) mod 5. We next use the else clause again to see that mpower(3, 5, 5) = (mpower (3, 2, 5)2 mod 5·3 mod 5) mod 5. Then we use the else if clause to see that mpower(3, 2, 5) = mpower(3, 1,5)2 mod 5. Us­ ing the else clause again, we have mpower(3, 1,5) = (mpower (3, 0, 5)2 mod 5·3 mod 5) mod 5. Finally, us�ng the if clause we see that mpower(3, 0, 5) = 1. Workmg , 2 backward it follows that mpower(3, 1,5) = (1 mod 5· 3 mod 5) mod 5 = 3, mpower(3, 2, 5) = 32 mod 5 = 4, mpower(3, 5, 5) = (42 mod 5·3 mod 5) mod 5 = 3, and fi­ nally mpower(3, 11,5) = (32 mod 5·3 mod 5) mod 5 = 2. We conclude that 311 mod 5 = 2. 5. With this input, the algorithm uses the else clause to find that gcd(8, l3) = gcd(l3 mod 8, 8) = gcd(5, 8). It uses this clause again to find that gcd(5, 8) = gcd(8 mod 5, 5) = gcd(3, 5), then to get gcd(3, 5) = gcd(5 mod 3, 3) = gcd(2, 3), then gcd(2, 3) = gcd(3 mod 2, 2) = gcd(l, 2), and once more to get gcd(I, 2) = gcd(2 mod 1, 1) = gcd(O, 1). Finally, to find gcd(O, 1) it uses the first step with a = 0 to find that gcd(O, 1) = 1. Consequently, the algorithm finds that gcd(8, l3) = 1. 7. procedure mult(n: positive integer, x: integer) if n = 1 then mult(n, x) := x else mult(n, x) := x + mult(n -1, x) 9. procedure sum of odds (n: positive integer) if n = 1 then sum of odds (n) := I else sum of odds(n) :=sum of odds (n -1) + 2n - 1 11. procedure smallest (a I, ... , an: integers) if n = 1 then smallest (ai, ... , an) = al elsesmallest(al, ... , an):= min(smalle st(al, ... , an-I), an) 13. procedure modfactorial(n, m: positive integers) if n = 1 then modfactorial(n, m) := 1 else modfactorial(n, m) := (n · modfactorial(n -1, m» mod m 15. procedure gcd(a, b: nonnegative integers) {a < b assumed to hold} if a = 0 then gcd(a, b) := b Answers to Odd-Numbered Exercises S-33 else if a = b -a then gcd(a, b) := a else if a < b -a then gcd(a, b) := gcd(a, b -a) else gcd(a, b) := gcd(b -a, a) 17. procedure multiply (x, y : nonnegative integers) if y = 0 then multiply (x, y) := 0 else if y is even then multiply (x, y) := 2 . multiply (x, y /2) else multiply (x, y) := 2 . multiply (x, (y-I)/2) + x 19. We use strong induction on a. Basis step: If a = 0, we know that gcd(O, b) = b for all b > 0, and that is precisely what the if clause does. Inductive step: Fix k > 0, assume the inductive hypothesi s-that the algorithm works correctly for all values of its first argument less than k-and consider what happens with input (k, b), where k < b. Because k > 0, the else clause is executed, and the answer is whatever the algorithm gives as output for inputs (b mod k, k). Because b mod k < k, the input pair is valid. By our inductive hy­ pothesis, this output is in fact gcd(b mod k, k), which equals gcd(k, b) by Lemma 1 in Section 3.6. 21. If n = 1, then nx = x, and the algorithm correctly returns x. Assume that the algorithm correctly computes kx. To compute (k + l)x it recursively computes the product of k + I - 1 = k and x, and then adds x. By the inductive hypothesis, it computes that product correctly, so the answer returned is kx + x = (k + I)x, which is correct. 23. procedure square (n: nonnegative integer) if n = 0 then square (n) := 0 else square(n) := square(n -1) + 2(n -1) + 1 Let pen) be the statement that this algorithm correctly com­ putes n2• Because 0 2 = 0, the algorithm works correctly (us­ ing the if clause) if the input is o. Assume that the algo­ rithm works correctly for input k. Then for input k + 1, it gives as output (because of the else clause) its output when the input is k, plus 2(k + 1 -1) + 1. By the inductive h�­ pothesis, its output at k is k2, so its output at k +.1 IS k2 + 2(k + 1 -1) + 1 = k2 + 2k + 1 = (k + 1)2, as desired. 25. n multiplications versus 2n 27. O(log n) versus n 29. procedure a(n: nonnegative integer) ifn = o then a(n) := 1 else ifn = 1 then a(n):= 2 else a(n) := a(n -1) * a(n -2) 31. Iterative 33. procedure iterative(n: nonnegative integer) if n = 0 then z := 1 . else if n = 1 then z := 2 else begin x:= 1 y:=2 z:= 3 for i := I to n - 2 begin w:=x+y+z x :=y y:=z z:=w end S-34 Answers to Odd-Numbered Exercises end {z is the nth term of the sequence} 35. We first give a recursive procedure and then an iterative procedure. procedure r(n: nonnegative integer) ifn < 3 thenr(n):= 2n + 1 else r(n) = r(n -1) . (r(n -2))2 . (r(n -3))3 procedure i(n : nonnegative integer) if n = 0 then z := 1 else if n = 1 then z := 3 else begin x:= 1 y:=3 z:= 5 for i := 1 to n - 2 . begin w:= z *y2*x3 x :=y y:=z z:=w end end {z is the nth term of the sequence} The iterative version is more efficient. 37. procedure reverse (w: bit string) n := length(w) ifn ::::: 1 then reverse(w):= w else reverse(w) := substr(w, n, n)reverse (substr (w, 1, n -1)) {substr (w, a, b) is the substring of w consisting of the symbols in the ath through bth positions} 39. The procedure correctly gives the reversal of A as A (ba­ sis step), and because the reversal of a string consists of its last character followed by the reversal of its first n - 1 characters (see Exercise 35 in Section 4.3), the algorithm behaves correctly when n > 0 by the inductive hypothesis. 41. The algorithm implements the idea of Example 13 in Sec­ tion 4.1. If n = 1 (basis step), place the one right triomino so that its armpit corresponds to the hole in the 2 x 2 board. If n > 1, then divide the board into four boards, each of size 2n-1 x 2n-l, notice which quarter the hole occurs in, position one right triomino at the center of the board with its armpit in the quarter where the missing square is (see Figure 8 in Sec­ tion 4.1), and invoke the algorithm recursively four times­ once on each of the 2n-1 x 2n-1 boards, each of which has one square missing (either because it was missing to begin with, or because it is covered by the central triomino). 43. procedure A(m, n: nonnegative integers) ifm = 0 then A(m, n):= 2n else ifn = 0 then A(m, n):= 0 else ifn = 1 then A(m, n):= 2 else A(m, n) := A(m -1, A(m, n -1)) 45. / h� JM\ ;po� j\ /\ /\ /\ /\ g/\ \) \) bd hz \ \ abd fg hpz ko �/ �/ a�/ pz abdfghkopz S-34 47. Let the two lists be 1,2, ... , m -1, m + n - 1 and m, m + 1, ... , m + n -2, m + n, respectively. 49. If n = 1, then the algorithm does nothing, which is correct because a list with one element is already sorted. Assume that the algo­ rithm works correctly for n = 1 through n = k. If n = k + 1, then the list is split into two lists, LI and L2. By the induc­ tive hypothesis, mergesort correctly sorts each of these sub­ lists; furthermore, merge correctly merges two sorted lists into one because with each comparison the smallest element in LI U L2 not yet put into L is put there. 51.0(n) 53.6 55.0(n2) Section 4.5 1. Suppose that x = O. The program segment first assigns the value 1 to y and then assigns the value x + y = 0 + 1 = 1 to z. 3. Suppose thaty = 3. The program segment assigns the value 2 to x and then assigns the value x + y = 2 + 3 = 5 to z. Be­ cause y = 3 > 0 it then assigns the value z + 1 = 5 + 1 = 6 toz. 5. (p/\ condition l){Sdq (p/\ -.conditionl /\ condition 2){S2}q (p /\ -.condition 1 /\ -.condition2 ... /\ -.condition(n -1){Sn}q p {if condition 1 then S 1 ; else if condition2 then S2; ... ; else Sn}q 7. We wi�l show $a� p ; ''power = Xi-I and i ::::: n + 1" is a loop invariant. Note that p is true initially, because before the lqopstarts,i = 1 and power = 1 =xo = Xl-I. Next, we must show that if p is true and i ::::: n after an execution of the loop, then p remains true after one more execution. The loop incre­ ments i by 1. Hence, because i ::::: n before this pass, i ::::: n + 1 after this pass. Also the loop assigns power· x to power. By S-35 the inductive hypothesis we see that power is assigned the value Xi-I. X = Xi. Hence, p remains true. Furthermore, the loop terminates after n traversals of the loop with i = n + I because i is assigned the value 1 prior to entering the loop, is incremented by I on each pass, and the loop terminates when i > n. Consequently, attermination power = xn, as desired. 9. Suppose that pis "m and n are integers." Then if the con­ dition n < 0 is true, a = -n = Inl after SI is executed. If the condition n < 0 is false, then a = n = In I after S I is executed. Hence, p{Sdq is true where q is p /\ (a = InD. Because S2 assigns the value 0 to both k and x, it is clear that q{S2}r is true where r is q /\ (k = 0) /\ (x = 0). Suppose that r is true. Let P(k) be "x = mk and k :::: a." We can show that P(k) is a loop invariant for the loop in S3. P(O) is true because before the loop is entered x = 0 = m · 0 and 0 :::: a. Now assume P(k) is true and k < a. Then P(k + 1) is true because x is as­ signed the value x + m = mk + m = m(k + 1). The loop ter­ minates when k = a, and at that point x = ma. Hence, r{S3}s is true where s is "a = Inl and x = ma." Now assume that s is true. Then if n < 0 it follows that a = -n, so x = -mn. In this case S4 assigns -x = mn to product. If n > 0 then x = ma = mn, so S4 assigns mn to product. Hence, S{S4}t is true. II. Suppose that the initial assertion p is true. Then because p{S}qo is true, qo is true after the segment S is exe­ cuted. Because qo -+ ql is true, it also follows that ql is true after S is executed. Hence, p{S}ql is true. 13. We will use the proposition p, "gcd(a, b) = gcd(x, y) and y :::: 0," as the loop invariant. Note that p is true before the loop is entered, because at that point x = a, y = b, and y is a positive inte­ ger, using the initial assertion. Now assume that p is true and y > 0; then the loop will be executed again. Inside the loop, x and y are replaced by y and x mod y, respectively. By Lemma I of Section 3.6, gcd(x, y) = gcd(y, x mody). Therefore, af­ ter execution of the loop, the value of gcd(x, y) is the same as it was before. Moreover, because y is the remainder, it is at least O. Hence, p remains true, so it is a loop invariant. Furthermore, if the loop terminates, then y = O. In this case, we have gcd(x, y) = x, the final assertion. Therefore, the pro­ gram, which gives x as its output, has correctly computed gcd(a, b). Finally, we can prove the loop must terminate, be­ cause each iteration causes the value of y to decrease by at least 1. Therefore, the loop can be iterated at most b times. Suppleme ntary Exercises 1. Let P(n) be the statement that this equation holds. Basis step: P(I) says 2/3 = 1-(1/31), which is true. Inductive step: Assume that P(k) is true. Then 2/3 + 2/9 + 2/27 + ... + 2/3n + 2/3n+1 = 1 -I/3n + 2/3n+1 (by the inductive hypothesis), and this equals 1 -I/3n+l, as desired. 3. Let P(n) be "1 . 1 + 2·2 + ... + n ·2n-1 = (n -I)2n + I." Basis step: P(I) is true because 1 . 1 = 1 = (1 -1)21 + 1. Inductive step: Assume that P(k) is true. Then 1 . 1 + 2 . 2 + ... + k· 2k-1 + (k + 1)· 2k = (k -I)2k + 1 + (k + I)2k = 2k· 2k + 1 = [(k + 1) - I]2k+1 + 1. 5. Let P(n) be "1/ (1 ·4) + ... + I/[(3n -2)(3n + 1)] = n/(3n + 1)." Basis step: P(I) is true because 1/(1·4) = 1/4. Inductive Answers to Odd-Numbered Exercises 8-35 step: Assume P(k) is true. Then 1/(1·4) + ... + I/[(3k -2)(3k + 1)] + I/[(3k + I)(3k+4)] = k/(3k+ 1)+ I/[(3k+ I)(3k+4)] = [k(3k+4) + I]/[(3k+ I)(3k + 4)] = [(3k + I)(k + I)]/[(3k + I)(3k + 4)] = (k + I)/(3k + 4). 7. Let P(n) be "2n > n3." Basis step: P(IO) is true be­ cause 1024> 1000. Inductive step: Assume P(k) is true. Then (k + 1)3 = k3 + 3� + 3k + 1 :::: k3 + 9k2 :::: k3 + k3 = 2k3 < 2 . 2k = 2k+l. 9. Let P(n) be "a - b is a factor of an - bn." Basis step: P(I) is trivially true. Assume P(k) is true. Then ak+1 -bk+1 = ak+1 -abk + abk -bk+ I = a(ak - bk) + bk(a -b). Then because a - b is a fac­ tor of ak - bk and a - b is a factor of a -b, it follows that a - b is a factor of ak+1 -bk+l. 11. Let P(n) be "a + (a + d) + ... + (a + nd) = (n + I)(2a + nd)/2." Ba­ sis step: P(I) is true because a + (a + d) = 2a + d = 2(2a + d)/2. Inductive step: Assume that P(k) is true. Then a + (a + d) + ... + (a + kd) + [a + (k + I)d] = (k + I)(2a + kd)/2 + a + (k + I)d = !(2ak + 2a + k2d + kd + 2a + 2kd + 2d) = !(2ak +4a + k2d + 3kd + 2d)= !(k + 2)[2a + (k + 1 )d]. 13. Basis step: This is true for n = 1 because 5/6 = 10/12. Inductive step: Assume that the equation holds for n = k, and consider n = k + 1. Th "k+1 i+4 "k i+4 k+5 en L...d=1 i(i+I)(i+2) = L...d=1 i(i+I)(i+2) + (k+I)(k+2)(k+3) = k(3k+ 7) k+5 (b th . d . h th·) 2(k+I)(k+2) + (k+I)(k+2)(k+3) Y e In uctlve ypo eSlS = (k+li(k+2) . e(3�+7) + !t�) = 2(k+I)(k�2)(k+3) . [k(3k + 7) (k + 3) + 2(k + 5)] = 2(k+l)(k�2)(k+3)' (3k3+ 16� + 23k + 10) = 2(k+I)(k�2)(k+3) . (3k + 1O)(k + 1)2 = 2(k+2�(k+3) . (3k + IO)(k 1) (k+ 1)(3(k+ 1)+7) d' d 15 B' . + = 2«k+ 1)+I)«k+ 1)+2) , as esrre . . aSls step. The statement is true for n = 1 because the derivative of g(x) = xtr is x . tr + tr = (x + I)tr by the product rule. Inductive step: Assume that the statement is true for n = k, i.e., the kth derivative is given by g(k) = (x + k)tr. Dif­ ferentiating by the product rule gives the (k + I)st deriva­ tive: g(k+I) = (x + k)tr + tr = [x + (k + I)]tr, as desired. 17. We will use strong induction to show that In is even if n == 0 (mod 3) and is odd otherwise. Basis step: This follows because 10 = 0 is even and II = 1 is odd. Inductive step: Assume that if j :::: k, then .N is even if j == 0 (mod 3) and is odd otherwise. Now suppose k + 1 == 0 (mod 3). Then 1k+1 = Ik + Ik-I is even because Ik and Ik-I are both odd. If k + 1 == 1 (mod 3), then Ik+1 = fi + fi-I is odd because Ik is even and fi-I is odd. Finally, if k + 1 == 2 (mod 3), then 1k+1 = fi + Ik-I is odd because Ik is odd and Ik-I is even. 19. LetP(n)bethestatement thatlkln + lk+dn+1 = fn+k+1 for every nonnegative integer k. Basis step: This consists of showing that P(O) and P(I) both hold. P(O) is true be­ cause filo + 1k+1 II = fi+1 . 0 + 1k+1 . 1 = Ji. Because fiji + Ik+lh = Ik + 1k+1 = fi+2, it follows that P(I) is true. Inductive step: Now assume that P(j) holds. Then, by the inductive hypothesis and the recursive definition of the Fibonacci numbers, it follows that 1k+1.N+1 + Ik + 2.N + 2 = Ik(.N-1 +.N) + Ik+l(.N + .N+d = (fk/.j-! + Ik + I.N) + (fk/j + Ik+l.N+I) = .N-I+k+1 + .N+k+1 = .N+k+2' This shows that P(j + 1) is true. 21. Let P(n) be the state- ment I� + If + ... + I; = Inln+1 + 2. Basis step: P(O) and S-36 Answers to Odd-Numbered Exercises P(I) both hold because 15 = 22 = 2 . 1+2 = loft + 2 and 15 + If = 22 + 12 = 1 ·3 + 2 = 11/3 + 2. Inductive step: Assume that P(k) holds. Then by the inductive hy­ pothesis 15 + If + ... + Ii + li+1 = hh+1 + 2 + Ii + I = h+l(lk + IHI) + 2 = h+lh+2 + 2. This shows that P(k + 1) holds. 23. Let P(n) be the statement that the identity holds for the integer n. Basis step: P(1) is obviously true. Inductive step: Assume that P(k) is true. Then cos(k + l)x + i sin(k + l)x = cos(kx + x) + i sin(kx + x) = coskx cosx -sinkx sinx + i(sinkx cosx + coskx sinx) = cosx (coskx + i sin kx)(cos x + i sin x) = (cos x + i sinx)k(cosx + i sinx) = (cos x + i sinxl+l. It follows that P(k + 1) is true. 25. Rewrite the right-hand side as 2n+l(n2_ 2n + 3) -6. For n = I we have 2 = 4 . 2 -6. Assume that the equation holds for n = k, and consider n = k + 1. Then :E��: j22j = :E�=I j22j + (k + 1)22k+ I = 2k+ l(k2_ 2k + 3) -6 + (� + 2k + 1)2k+1 (by the inductive hypo­ thesis) = 2HI(2� + 4) -6=2H2(� +2) -6 = 2H2[(k+ 1)2 - 2(k + 1) + 3] -6. 27. Let P(n) be the statement that this equation holds. Basis step: In P(2) both sides reduce to 1/3. Inductive step: Assume that P(k) is true. Then :E��: I/U2 -1) = (:E�=I I/U2 -1») + 1/[(k + 1)2 -1] = (k -1)(3k + 2)/[4k(k + I)] + l/[(k + 1)2 - 1] by the inductive hypothesis. This simplifies to (k -I )(3k + 2)/[4k(k + I)] + 1/(� + 2k) = (3k3 + 5�)/[4k(k + 1)(k + 2)] = {[(k+ I) -1][3(k+ 1) + 2]}/[4(k + 1)(k+2)], which is exactly what P(k + 1) asserts. 29. Let P(n) be the as­ sertion that at least n + 1 lines are needed to cover the lattice points in the given triangular region. Basis step: P(o) is true, because we need at least one line to cover the one point at (0,0). Inductive step: Assume the induc­ tive hypothesis, that at least k + 1 lines are needed to cover the lattice points with x � 0, y � 0, and x + y � k. Con­ sider the triangle of lattice points defined by x � 0, y � 0, and x + y � k + 1. By way of contradiction, assume that k + 1 lines could cover this set. Then these lines must cover the k + 2 points on the line x + y = k + 1. But only the line x + y = k + 1 itself can cover more than one of these points, because two distinct lines intersect in at most one point. Therefore none of the k + 1 lines that are needed (by the inductive hypothesis) to cover the set of lattice points within the triangle but not on this line can cover more than one of the points on this line, and this leaves at least one point uncovered. Therefore our assumption that k + I lines could cover the larger set is wrong, and our proof is complete. 31. Let P(n) be Bk = MAkM-I. Basis step: Part of the given conditions. Inductive step: Assume the inductive hypothesis. Then Bk+1 = BBk = MAM-IBk = MAM-lMAkM-1 (by the inductive hypothesis) = MAIAkM-I = MAAkM-I = MA k+l M-I . 33. We prove by mathematical induction the following stronger statement: For every n � 3, we can write n! as the sum of n of its distinct positive divisors, one of which is 1. That is, wecanwriten! = al + a2 + ... + an, where each ai is a divisor of n !, the divisors are listed in strictly decreasing order, and an = 1. Basis step: 3! = 3 + 2 + 1. Inductive step: Assume that we can write k! as a sum of the desired form, S-36 say k! = al + a2 + ... + ak, where each ai is a divisor ofn!, the divisors are listed in strictly decreasing order, and an = 1. Consider (k + I)!. Then we have (k + I)! = (k + l)k! = (k + 1)(al + a2 + ... +ak) = (k+ l)al +(k+ l)a2 + ... + (k+ l)ak = (k+ l)al +(k+ l)a2 + ... +k· ak +ak. Be­ cause each ai was a divisor of k!, each (k + l)ai is a divisor of (k + I)!. Furthermore, k· ak = k, which is a divisor of (k + I)!, and ak = 1, so the new last summand is again 1. (Notice also that our list of summands is still in strictly de­ creasing order.) Thus we have written (k + I)! in the desired form. 35. When n = 1 the statement is vacuously true. As­ sume that the statement is true for n = k, and consider k + I people standing in a line, with a woman first and a man last. If the kth person is a woman, then we have that woman standing in front of the man at the end. If the kth person is a man, then the first k people in line satisfy the conditions of the inductive hypothesis for the first k people in line, so again we can conclude that there is a woman directly in front of a man somewhere in the line. 37. Basis step: When n = I there is one circle, and we can color the inside blue and the outside red to satisfy the conditions. Inductive step: Assume the inductive hypothesis that if there are k circles, then the regions can be 2-colored such that no regions with a common boundary have the same color, and consider a situation with k + 1 circles. Remove one of the circles, producing a picture with k circles, and invoke the inductive hypothesis to color it in the prescribed manner. Then replace the removed circle and change the color of every region inside this circle. The resulting figure satisfies the condition, because if two regions have a common boundary, then either that boundary involved the new circle, in which case the regions on either side used to be the same region and now the inside portion is different from the outside, or else the boundary did not involve the new circle, in which case the regions are colored differently because they were colored differently before the new circle was restored. 39. If n = 1 then the equation reads 1 . 1 = 1 . 2/2, which is true. Assume that the equation is true for n and consider itforn + 1. Then:EJ!:(2j -1) (:E�!� n = :EJ=I(2j -1) (:E�!� n + [2(n + 1) -1]· nll = :EJ=I(2j -1) (nll + :E�=j i) + 2::/ = (nll :EJ=I(2j -1») + (:EJ=I(2j -1) ",n .!) + 2n+l = (_1_. n2) + n(n+l) + 2n+1 (by the ind- �k=J k n+l n+1 2 n+1 uctive hypothesis) = 2n2+n(n+I)2+(4n+2) = 2(n+I)'+n(n+l)' = 2(n+l) 2(n+l) (n+l�n+2) 41. a) 92 b) 91 c) 91 d) 91 e) 91 1) 91 43. The basis step is incorrect because n =F 1 for the sum shown. 45. Let P(n) be ''the plane is divided into n2 - n + 2 regions by n circles if every two of these cir­ cles have two common points but no three have a com­ mon point." Basis step: P(1) is true because a circle divides the plane into 2 = 12 - 1 + 2 regions. Inductive step: Assume that P(k) is true, that is, k circles with the specified properties divide the plane into k2 -k + 2 regions. Suppose thata (k + l)st circle is added. This circle intersects each of the other k circles in two points, so these points of intersection form 2k new arcs, each of which splits an old S-37 region. Hence, there are 2k regions split, which shows that there are 2k more regions than there were previously. Hence, k + I circles satisfying the specified properties divide the plane intok2 -k+2 + 2k = (k2+ 2k + 1) -(k + 1) +2 = (k + 1)2 -(k + 1) + 2 regions. 47. Suppose "fi were ra­ tional. Then "fi = alb, where a and b are positive inte­ gers. It follows that the set S = {n"fil n EN} n N is a nonempty set of positive integers, because b"fi = a belongs to S. Let t be the least element of S, which exists by the well­ ordering pr�erty. Then t = s"fi for some integer s. We have t -s = s-J2 - s = s("fi -1), so t - s is a positive integer because "fi > 1. Hence, t - s belongs to S. This is a con­ tradiction because t - s = s "fi - s < s. Hence, "fi is irra­ tional. 49. a) Let d = gcd(al' a2, ... , an). Then d is a divi­ sor of each aj and so must be a divisor ofgcd(an_1 , an). Hence, d is a common divisor of ai, a2, ... , an-2, and gcd(a�_I' an). To show that it is the greatest common divisor of these num­ bers, suppose that c is a common divisor of them. Then c is a divisor of aj for i = 1, 2, ... , n -2 and a divisor of gcd(an_h an), so it is a divisor of an-I and an. Hence, c is a common divisor of ai, a2, ... , an-I. and an. Hence, it is a divisor of d, the greatest common divisor of a I , a2, ... , an. It follows that d is the greatest common divisor, as claimed. b) If n = 2, apply the Euclidean algorithm. Otherwise, ap­ ply the Euclidean algorithm to an-I and an, obtaining d = gcd(an_I' an), and then apply the algorithm recursively to a" a2, ... , an-2, d. 51. f(n) = n2. Let P(n) be "f(n) = n2." Basis step: P(I) is true because f(l) = 1 = 12, which follows from the definition of f. Inductive step: Assume f(n) = n2. Then f(n + 1) = f«n + 1) -1) + 2(n + 1) - 1 = f(n) + 2n + 1 = n2 + 2n + 1 = (n + 1)2. 53. a) A, 0, 1,00,01, 11,000,001,011,111,0000,0001,0011,0111,1111,00000, 00001, 00011, 00111, 01111, 11111 b) S = {a,8 I a is a string of m Os and ,8 is a string of n Is, m :::: 0, n :::: O} 55. Apply the first recursive step to A to get 0 E B. Apply the second recursive step to this string to get 00 E B. Ap­ ply the first recursive step to this string to get (O()) E B. By Exercise 58, «())) is not in B because the number of left parenthe ses does not equal the number of right parenthe ses. 57. A, 0, «()), 00 59. a) 0 b) -2 c) 2 d) 0 61. procedure generate(n: nonnegative integer) if n is odd then begin S:= S(n -1);T:= T(n -1) end else if n = 0 then begin S:= 0;T:= {A} end else begin TI := T(n -2); SI := S(n -2) T := TI U {(x) I x E TI U SI and l(x) = n -2} S := SI U {xy I x E TI and YETI U SI andl(xy) = n} end {T US is the set of balanced strings oflength at most n} Answers to Odd-Numbered Exercises 8-37 63. If x ::::: y initially, then x :.= y is not executed, so x ::::: y is a true final assertion. If x > y initially, then x : = y is executed, so x ::::: y is again a true final assertion. 65. procedure zerocount (al, a2, ... ,an: list of integers) ifn = 1 then if al = 0 then zerocount(al, a2, ... , an) := I elsezerocount(al, a2, ... , an):= 0 else if an = 0 then zerocount(al, a2, ... , an) := zerocount(al, a2, ... , an-I) + 1 else zerocount (al, a2, ... ,an) := zerocount(al,a2, ... ,an-I) 67. We will prove that a(n) is a natural number and a(n) ::::: n. This is true for the base case n = 0 because a(O) = O. Now assume that a(n -1) is a natural number and a(n -I) ::::: n -1. Then a(a(n -1» is a applied to a natural number less than or equal to n -1. Hence, a(a(n -1» is also a natural number minus than or equal to n -I. Therefore, n -a(a(n -1» is n minus some natural number less than or equal to n -1, which is a natural number less than or equal to n. 69. From Exercise 68, a(n) = L(n + l)tLJ and a(n -I) = LntLJ. Because tL < 1, these two values are equal or they differ by 1. First suppose that tLn -LtLn J < I - tL. This is equivalent to tL( n + 1) < 1 + LtLn J. If this is true, then LtL(n + I)J = LtLnJ. On the other hand, if tLn - LtLnJ :::: 1 - tL, then tL(n + 1) :::: 1 + LtLnJ, so LtL(n + I)J = LtLnJ + 1, as desired. 71. f(O) = 1, m(O) = 0; f(l) = 1, m(l) = 0; f(2) = 2, m(2) = 1; f(3) = 2, m(3) = 2; f(4) = 3, m(4) = 2; f(5) = 3, m(5) = 3; f(6) = 4, m(6) = 4; f(7) = 5, m(7) = 4; f(8) = 5, m(8) = 5; f(9) = 6, m(9) = 6 73. The last occurrence of n is in the position for which the total number of 1 's, 2's, ... , n's all together is that posi­ tion number. But because ak is the number of occurrences of k, this is just L�=I ak, as desired. Because f(n) is the sum of the first n terms of the sequence, f(f(n» is the sum of the first f(n) terms of the sequence. But because f(n) is the last term whose value is n, this means that the sum is the sum of all terms of the sequence whose value is at most n. Because there are ak terms of the sequence whose value is k, this sum is L�=I k . at, as desired CHAPTER S Section 5.1 1. a) 5850 b) 343 3. a) 410 b) 510 5.42 7.263 9.676 11.28 13. n + 1 (counting the empty string) 15.475,255 (counting the empty string) 17. 1,321,368,961 19. a) Seven: 56,63, 70, 77, 84, 91, 98 b) Five: 55,66, 77, 88, 99 c) One: 77 21. a) 128 b) 450 c) 9 d) 675 �� Q� �m �� ll�� �� c) 27 25. 350 27.52,457,600 29.20,077,200 31. a) 37,822,859,361 b) 8,204,716,800 c) 40,159,050, 880 d) 12,113,640,000 e) 171,004,205 ,215 Q 72,043, 541,640 g) 6,230,721,635 h) 223,149,655. 33. a) 0 b) 120 c)720 d)2520 35.a)2 if n=I,2 if n=2,0 S-38 Answers to Odd-Numbered Exercises if n :::: 3 b) 2n-2 for n > 1; 1 if n = 1 c) 2(n -1) 37. (n + l)m 39. If n is even, 2n/2; if n is odd, 2(n+I)/2 41. a) 240 b) 480 c) 360 43. 352 45. 147 47.33 49. a) 9,920,671,339,261,325,541,376 � 9.9 x 1021 b) 6,641,514,961,387,068,437,760 � 6.6 x 1021 c) 9,920,671,339.261325541376 seconds, which is about 314,000 years 51. 7,104,000,000,000 53. 18 55. 17 57.22 59. Let P(m) be the sum rule for m tasks. For the ba­ siscasetakem = 2. Thisisjustthesumrulefortwotasks.Now assume that P(m) is true. Consider m + 1 tasks, TI, T2, ... , Tm, Tm+l, which can be done in n" n2, ... , nm, nm+1 ways, respectively, such that no two of these tasks can be done at the same time. To do one of these tasks, we can either do one of the first m of these or do task Tm+l. By the sum rule for two tasks, the number of ways to do this is the sum of the number of ways to do one of the first m tasks, plus nm+l. By the inductive hypothesis, this is n 1 + n2 + ... + nm + nm+1' as desired. 61. n(n -3)/2 Section 5.2 1. Because there are six classes, but only five weekdays, the pi­ geonhole principle shows that at least two classes must be held on the same day. 3. a) 3 b) 14 5. Because there are four possible remainders when an integer is divided by 4, the pi­ geonhole principle implies that given five integers, at least two have the same remainder. 7. Leta, a + 1, ... , a + n -1 be the integers in the sequence. The integers (a + i) mod n, i = 0, 1,2, ... , n -1, are distinct, because 0 < (a + j) -(a + k) < n whenever 0 :s k < j :s n -1. Because there are n possible values for (a + i) mod n and there are n different integers in the set, each of these values is taken on exactly once. It follows that there is exactly one integer in the se­ quence that is divisible by n. 9. 4951 11. The midpoint of the segment joining the points (a, b, c) and (d, e, f) is «a + d)/2, (b + e)/2, (c + /)/2). It has integer coefficients if and only if a and d have the same parity, b and e have the same parity, and c and / have the same parity. Because there are eight possible triples of parity [such as (even, odd, even)], by the pigeonhole principle at least two of the nine points have the same triple of parities. The midpoint of the segment join­ ing two such points has integer coefficients. 13. a) Group the first eight positive integers into four subsets of two inte­ gers each so that the integers of each subset add up to 9: {I, 8}, {2, 7}, {3, 6}, and {4, 5}. Iffive integers are selected from the first eight positive integers, by the pigeonhole principle at least two of them come from the same subset. Two such integers have a sum of9, as desired. b) No. Take {I, 2,3, 4}, for ex­ ample. 15.4 17.21,251 19. a) If there were fewer than 9 freshmen, fewer than 9 sophomores, and fewer than 9 ju­ niors in the class, there would be no more than 8 with each of these three class standings, for a total of at most 24 students, contradicting the fact that there are 25 students in the class. b) If there were fewer than 3 freshmen, fewer than 19 sopho­ mores, and fewer than 5 juniors, then there would be at most 2 freshmen, at most 18 sophomores, and at most 4 juniors, for a total of at most 24 students. This contradicts the fact that S-38 there are 25 students in the class. 21. 4, 3, 2, 1, 8, 7, 6, 5, 12,11,10,9,16,15,14,13 23. procedure long(al, ... , an: positive integers) {first find longest increasing subsequence} max := 0; set := 00 ... 00 {n bits} for i := 1 to 2n begin last := 0; count := 0, OK := true for j := 1 to n begin if setU) = 1 then begin if aj > last then last:= aj count: = count + 1 end else OK := false end if count> max then begin max:= count best:= set end set := set + 1 (binary addition) end {max is length and best indicates the sequence} {repeat for decreasing subsequence with only changes being a j < last instead of a j > last and last := 00 instead of last := O} 25. By symmetry we need prove only the first statement. Let A be one of the people. Either A has at least four friends, or A has at least six enemies among the other nine people (because 3 + 5 < 9). Suppose, in the first case, that B, C, D, and E are all A's friends. If any two of these are friends with each other, then we have found three mutual friends. Otherwise {B, C, D, E} is a set of four mutual enemies. In the second case, let {B, C, D, E, F, G} be a set of enemies of A. By Example 11, among B, C, D, E, F, and G there are either three mutual friends or three mutual enemies, who form, with A, a set of four mutual enemies. 27. We need to show two things: that if we have a group of n people, then among them we must find either a pair of friends or a subset of n of them all of whom are mutual enemies; and that there exists a group of n -1 people for which this is not possible. For the first statement, if there is any pair of friends, then the condition is satisfied, and if not, then every pair of people are enemies, so the second condition is satisfied. For the second statement, if we have a group of n -'-I people all of whom are enemies of each other, then there is neither a pair of friends nor a subset of n of them all of whom are mutual enemies. 29. There are 6,432,816 possibilities for the three initials and a birthday. So, by the generalized pigeonhole principle, there are at least r36,000,000/6,432,8161 = 6 people who share the same initials and birthday. 31. 18 33. Because there are six computers, the number of other computers a computer is connected to is an integer between 0 and 5, inclusive. How­ ever, 0 and 5 cannot both occur. To see this, note that if some computer is connected to no others, then no computer is con­ nected to all five others, and if some computer is connected to all five others, then no computer is connected to no others. S-39 Hence, by the pigeonhole principle, because there are at most five possibilities for the number of computers a computer is connected to, there are at least two computers in the set of six connected to the same number of others. 35. Label the com­ puters C I through C 100, and label the printers PI through P20. Ifwe connect Ck to Pk for k = 1,2, ... ,20 and connect each of the computers C21 through CIOO to all the printers, then we have used a total of20 + 80 . 20 = 1620 cables. Clearly this is sufficient, because if computers C I through C 20 need printers, then they can use the printers with the same subscripts, and if any computers with higher subscripts need a printer instead of one or more of these, then they can use the printers that are not being used, because they are connected to all the printers. Now we must show that 1619 cables is not enough. Because there are 1619 cables and 20 printers, the average number of com­ puters per printer is 1619/20, which is less than 81. Therefore some printer must be connected to fewer than 81 computers. That means it is connected to 80 or fewer computers, so there are 20 computers that are not connected to it. If those 20 com­ puters all needed a printer simultaneously, then they would be out of luck, because they are connected to at most the 19 other printers. 37. Let aj be the number of matches com­ pleted by hour i. Then 1 � al < a2 < ... < a75 � 125. Also 25 � al + 24 < a2 + 24 < ... < a75 + 24 � 149. There are 150 numbers aI, ... ,a75, al + 24, ... ,a75 + 24. By the pi­ geonhole principle, at least two are equal. Because all the ajS are distinct and all the (aj + 24)s are distinct, it follows that aj = a j + 24 for some i > j. Thus, in the period from the U + l)st to the ith hour, there are exactly 24 matches. 39. Use the generalized pigeonhole principle, placing the lSI objects f(s) for s E S in ITI boxes, one for each element of T. 41. Let dj be jx -NUx), where NUx) is the in­ teger closest to jx for 1 � j � n. Each dj is an irrational number between -112 and 1/2. We will assume that n is even; the case where n is odd is messier. Consider the n in­ tervals {x I jln < x < U + l)ln}, {x I -U + l)ln < x < -j In} for j = 0, 1, ... , (nI2) -1. If dj belongs to the inter­ val {x 10 < x < lin} or to the interval {x I -lin < x < O} for some j, we are done. If not, because there are n - 2 intervals and n numbers dj, the pigeonhole principle tells us that there is an interval {x I (k -1)ln < x < kin} con­ taining dr and ds with r < s. The proof can be finished by showing that (s -r)x is within 1 I n of its nearest integer. 43. a) Assume that ik � n for all k. Then by the generalized pigeonhole principle, at least r(n2 + l)/nl = n + 1 of the numbers iI, i2, .. " in2+1 are equal. b) If akj < akj+I' then the subsequence consisting of akj followed by the increasing subsequence oflength ikj+1 starting at akj+1 contradicts the fact that hj = hj+l• Hence, akj > akj+!. c) If there is no increas­ ing subsequence of length greater than n, then parts (a) and (b) apply. Therefore, we have ak.+1 > ak. > ... > ak2 > akp a decreasing sequence of length n + 1. Section 5.3 l.abc,acb,bac, bca, cab,cba c) 8 d) 6720 e) 40,320 3.720 5. a) 120 b) 720 t) 3,628,800 7. 15,120 Answers to Odd-Numbered Exercises S-39 9. 1320 11. a) 210 b) 386 c) 848 d) 252 13.2(n!)2 15.65,780 17. 2100 -5051 19. a) 1024 b) 45 c) 176 d) 252 21. a) 120 b) 24 c) 120 d) 24 e) 6 t) 0 23.609,638,400 25. a) 94,109,400 b) 941,094 c) 3,764,376 d) 90,345,024 e) 114,072 t) 2328 g) 24 b) 79,727,040 i) 3,764,376 j) 109,440 27. a) 12,650 b) 303,600 29. a) 37,927 b) 18,915 31. a) 122,523,030 b) 72,930,375 c) 223,149,655 d) 100,626,625 33.54,600 35.45 37. 912 39. 11,232,000 41. 13 43. 873 Section 5.4 1. x4 + 4x3y + 6x2y2 + 4xy3 + y4 3. x6 + 6x5y + 15x4 y2 + 20x3y3 + 15x2y4 + 6xy5 + y6 5. 101 7. -2IOC:) = -94,595,072 9. -210139ge:) 11. (_1)<200-k)/3 (2���)/3) if k == 2 (mod 3) and -100 � k � 200; 0 otherwise 13.1 9 36 84 126 126 84 36 9 1 15. The sum of all the positive numbers (�), as k runs from 0 to n, is 2n, so each one of them is no bigger than this sum. 17. (n) = n(n-I)(n-2)···(n-k+1) < !!:!!.:=!! = nkl2k-1 k k(k-l)(k-2)···2 -2·2·····2 19 ( n) (n) n! n! n! [k . k-I + k = (k-I)!(n-k+ I)! + k!(n-k)! = k!(n HI)! ' + (n - k + 1)] = k!(�:/�k)! = etl) 21. a) We show that each side counts the number of ways to choose from a set with n elements a subset with k elements and a dis­ tinguished element of that set. For the left-hand side, first choose the k-set (this can be done in (Z) ways) and then choose one of the k elements in this subset to be the dis­ tinguished element (this can be done in k ways). For the right-hand side, first choose the distinguished element out of the entire n-set (this can be done in n ways), and then choose the remaining k - 1 elements of the subset from the remaining n - 1 elements of the set (this can be done in (�::::D ) b) k(n) -k n! _ n·(n-I)! _ (n-I) ways . k -. k!(n-k)! -(k-I)!(n-k)! -n k-I 23. (ntl) = k!(�:II�k)! = (ntl) (k-I)![:�(k-I)l! = (n + 1) (k�I)1 k. This identity together with (�) = 1 gives a recur­ sive definition. 25. ( 2n) + (2n) = (2n+l) = ! [(2n+l) + n+1 n n+1 2 n+1 e':/)] = ! [e::n +en:I)] = ! e::12) 27. a) (n+;+1) counts the number of ways to choose a sequence of r Os and n + 1 Is by choosing the positions of the Os. Alternately, suppose that the U + 1 )st term is the last term equal to 1, so that n � j � n + r. Once we have determined where the last 1 is, we decide where the Os are to be placed in the j spaces before the last 1. There are n 1 sand j - n Os in this range. By the sum rule it follows that there are Lj!� C�n) = L�=o (ntk) ways to do this. b) Let P (r ) be the statement to be proved. The basis step is the equation (�) = (ntl), which is just 1 = 1. Assume that P(r) is true. Then L�!� (ntk) = ",r (n+k) + (n+r+l) = (n+r+l) + (n+r+l) = (n+r+2) using L..Jk=O k r+1 r r+1 r+I' the inductive hypothesis and Pascal's Identity. 29. We can choose the leader first in n different ways. We can then choose the rest of the committee in 2n-1 ways. Hence, there are n2n-1 ways to choose the committee and its leader. Meanwhile, S-40 Answers to Odd-Numbered Exercises the number of ways to select a committee with k people is m. Once we have chosen a committee with k people, there are k ways to choose its leader. Hence, there are I:�= I k(�) ways to choose the committee and its leader. Hence, I:�=I k(�) = n2n-l• 31. Let the set have n elements. From Corollary 2 we have (�) -m + (;) -... + (_I)n (:) = O. It follows that (�) + (;) + (:) + ... = (�) + (;) + (�) + .... The left-hand side gives the number of subsets with an even number of elements, and the right-hand side gives the number of subsets with an odd number of elements. 33. a) A path of the desired type consists of m moves to the right and n moves up. Each such path can be represented by a bit string of length m + n with m Os and n 1 s, where a 0 represents a move to the right and a 1 a move up. b) The number of bit strings of length m + n containing exactly n I s equals (m�n) = (m:n) because such a string is determined by speci­ fying the positions of the n 1 s or by specifying the positions of the mOs. 35. By Exercise 33 the number of paths oflength n of the type described in that exercise equals 2n, the number of bit strings of length n. On the other hand, a path of length n of the type described in Exercise 33 must end at a point that has n as the sum of its coordinates, say (n - k, k) for some k between 0 and n, inclusive. By Exercise 33, the num­ ber of such paths ending at (n - k, k) equals e-!+k) = (�). Hence, I:�=o (�) = 2n. 37. By Exercise 33 the number of paths from (0, 0) to (n + 1, r) of the type described in that ex­ ercise equals (n+;+I). But such a path starts by going j steps vertically for some j with 0 � j � r. The number of these paths beginning with j vertical steps equals the number of paths of the type described in Exercise 33 that go from (1, j) to (n + 1, r). This is the same as the number of such paths that go from (0,0) to (n, r -j), which by Exercise 33 equals (n+r-:-j). Because ,,� (n+r-:-j) = "r (n+k) it follows that r-] L...]=O r-] L...k=O k ' I:�=I etk) = (n+;-I). 39. a) (nil) b) (nj2) c) e:'=-12) d) (l(n��)�2J») e) Largest odd entry in nth row of Pascal's triangle 1) e:'=-13) Section 5.5 1.243 3.266 5.125 7.35 9. a) 1716 b) 50,388 c) 2,629,575 d) 330 e) 9,724 11.9 13.4,504,501 15. a) 10,626 b) 1,365 c) 11,649 d) 106 17. 2,520 19.302,702,400 21.3003 23.7,484,400 25.30,492 27. C(59, 50) 29.35 31. 83,160 33.63 35. 19,635 37.210 39.27,720 41. 52!/(7!517!) 43. Approximately 6.5 x 1032 45. a) C(k + n - 1, n) b) (k + n -1)!/(k -I)! 47. There are C(n, nl) ways to choose nl objects for the first box. Once these objects are chosen, there are C(n -nl, n2) ways to choose objects for the second box. Similarly, there are C(n -nl -n2, n3) ways to choose objects for the third box. Continue in this way un­ til there is C(n -nl -n2 -... -nk-I, nk) = C(nk, nk) = 1 way to choose the objects for the last box (because n I + n2 + ... + nk = n). By the product rule, the number of ways S-40 to make the entire assignment is C(n, nl)C(n -nb n2)C (n -nl -n2, n3)· .. C(n -nl -n2 -... -nk-b nk), which equals n!/(ndn2!·· ·nk!), as straightforward simplification shows. 49. a) Because XI � X2 � ... � X" it follows that XI + 0 < X2 + 1 < ... < Xr + r -1. The inequalities are strict because Xj + j -1 < Xj+1 + j as long as Xj � Xj+I. Because 1 � X j � n + r -1, this sequence is made up of r distinct elements from T. b) Suppose that 1 � XI < X2 < ... < Xr � n + r -1. Let Yk = Xk -(k -1). Then it is not hard to see that Yk � Yk+1 for k = 1,2, ... , r -1 and that 1 � Yk � n for k = 1,2, ... r. It follows that{Yb Y2, ... , Yr} is an r-combination with repetitions allowed of S. c) From parts (a) and (b) it follows that there is a one-to-one corre­ spondence of r-combinations with repetitions allowed of S and r-combinations of T, a set with n + r -1 elements. We conclude that there are C(n + r -1, r) r-combinations with repetitions allowed of S. 51.65 53.65 55. 2 57.3 59. a) 150 b) 25 c) 6 d) 2 61. 90,720 63. The terms in the expansion are of the form X�IX�2 ••• x::, .. , where nl + n2 + ... + nm = n. Such a term arises from choosing the XI in n I factors, the X2 in n2 factors, ... , and the Xm in nm factors. This can be done in C(n;nl, n2, ... , nm) ways, because a choice is a permutation of n I labels "1," n2 labels "2," ... , and nm labels "m." 65.2520 Section 5.6 1.14532, 15432, 21345, 23451, 23514, 31452, 31542, 43521, 45213, 45321 3. a) 2134 b) 54132 c) 12534 d) 45312 e) 6714253 1) 31542678 5.1234,1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321 7. {I, 2, 3}, {I, 2, 4}, {l, 2, 5}, {I, 3, 4}, {I, 3, 5}, {I, 4, 5}, {2, 3, 4}, {2, 3, 5}, {2, 4, 5}, {3, 4, 5} ,9. The bit string representing the next larger r­ combination must differ from the bit string representing the original one in position i because positions i + 1, ... , r are occupied by the largest possible numbers. Also ai + 1 is the smallest possible number we can put in position i if we want a combination greater than the original one. Then ai + 2, ... , ai + r - i + 1 are the smallest allowable num­ bers for positions i + 1 to r. Thus, we have produced the next r-combination. 11.123,132,213,231,312,321,124,142, 214,241,412,421, 125, 152,215,251,512,521, 134, 143, 314,341,413,431, 135, 153,315,351,513,531,145,154, 415,451,514,541,234,243,324,342,423,432,235,253, 325,352, 523,532,245,254,425,452,524, 542,345,354, 435, 453, 534, 543 13. We will show that it is a bijection by showing that it has an inverse. Given a positive integer less thann!, let ai, a2, ... , an-I be its Cantor digits. Put n in po­ sition n -an-I; then clearly, an-I is the number of integers less than n that follow n in the permutation. Then put n - 1 in free position (n -1) -an-2, where we have numbered the free positions 1, 2, ... , n - 1 (excluding the position that n is already in). Continue until 1 is placed in the only free position left. Because we have constructed an inverse, the correspon­ dence is a bijection. S-4! 15. procedure Cantor permutation(n, i: integers with n�landO:::i<n!) x :=n for j := 1 ton Pj:= 0 for k := 1 to n -1 begin e := Lx/(n -k)!J; x := x -e(n -k)!; h := n while Ph =1= 0 h:= h-l for j := 1 toe begin h := h-l while Ph =1= 0 h:= h-l end Ph:= n -k+ 1 end h:= 1 while Ph =1= 0 h:= h + 1 Ph:= 1 {PIP2 ... Pn is the permutation corresponding to i} Supplem entary Exercises I. a) 151,200 b) 1,000,000 c) 210 d) 5005 3.3100 5.24,600 7. a) 4,060 b) 2688 c) 25,009,600 9. a) 192 b) 301 c) 300 d) 300 11. 639 13. The maximwn possible sum is 240, and the minimwn possible swn is 15. So the nwnber of possible swns is 226. Because there are 252 subsets with five elements of a set with 10 elements, by the pigeonhole principle it follows that at least two have the same swn. 15. a) 50 b) 50 c) 14 d) 5 17. Let aI, a2, ... , am be the integers, and let d; = L�=I aj. If d; == 0 (mod m) for some i, we are done. Otherwise dl mod m, d2 mod m, ... , dm mod m are m integers with values in {I, 2, ... , m -I}. By the pigeonhole principle dk = dr for some 1 ::: k < I ::: m. Then L�=k+1 aj = dr -dk == 0 (mod m). 19. The decimal expansion of the rational nwn­ ber a/b can be obtained by division of b into a, where a is written with a decimal point and an arbitrarily long string of Os following it. The basic step is finding the next digit of the quotient, namely, Lr / b J, where r is the remainder with the next digit of the dividend brought down. The current remainder is obtained from the previous remainder by subtracting b times the previous digit of the quotient. Eventually the dividend has nothing but Os to bring down. Furthermore, there are only b possible remainders. Thus, at some point, by the pigeonhole principle, we will have the same situation as had previously arisen. From that point onward, the calculation must fol­ low the same pattern. In particular, the quotient will repeat. 21. a) 125,970 b) 20 c) 141,120,525 d) 141,120,505 e) 177,100 t) 141,078,021 23. a) 10 b) 8 c) 7 25.3n 27. C(n + 2, r + 1) = C(n + 1, r + 1) + C(n + 1, r) = 2C(n + 1, r + 1) -C(n + 1, r + 1) + C(n + 1, r) = Answers to Odd-Numbered Exercises S-41 2C(n + 1, r + 1) -(C(n, r + 1) + C(n, r» + (C(n, r) + C(n,r -1» = 2C(n + l,r + 1) -C(n,r + 1) + C(n, r -1) 29. Substitute x = 1 and y = 3 into the Bi­ nomial Theorem. 31. C(n + 1,5) 33.3,491,888,400 35.524 37. a) 45 b) 57 c) 12 39. a) 386 b) 56 c) 512 41. 0 ifn < m; C(n -1, n -m) ifn � m 43. a) 15,625 b) 202 c) 210 d) 10 45. procedure next permutation (n: positive integer, aI, a2, ... , ar: positive integers not exceeding n withala2 ... ar =1= nn ... n) i :=r while a; = n begin a;:= 1 i := i-I end ai := a; + 1 {ala2 ... ar is the next permutation in lexicographic order} 47. We must show that if there are R(m, n -1) + R(m -1, n) people at a party, then there must be at least m mutual friends or n mutual enemies. Consider one person; let's call him Jerry. Then there are R(m -1, n) + R(m, n -1) - 1 other people at the party, and by the pigeonhole principle there must be at least R(m -1, n) friends of Jerry or R(m, n -1) enemies of Jerry among these people. First let's suppose there are R(m -1, n) friends of Jerry. By the definition of R, among these people we are guaranteed to find either m -1 mutual friends or n mutual enemies. In the former case, these m -1 mutual friends together with Jerry are a set of m mutual friends; and in the latter case, we have the desired set of n mutual enemies. The other situation is similar: Suppose there are R(m, n -1) enemies of Jerry; we are guaranteed to find among them either m mutual friends or n -1 mutual ene­ mies. In the former case, we have the desired set of m mutual friends, and in the latter case, these n -1 mutual enemies together with Jerry are a set of n mutual enemies. CHAPTER 6 Section 6.1 1.1/13 3.1/2 5.1/2 7.1/64 9.47/52 11. 1/C(52, 5) 13. 1 -[C(48, 5)/C(52, 5)] 15, C(13, 2) C(4, 2)C(4, 2)C(44, 1)/C(52, 5) 17. 1O,240/C(52, 5) 19. 1,302,540/C(52, 5) 21. 1/64 23.8/25 25. a) 1/ C(50, 6) = 1/15,890,700 b) 1/C(52, 6) = 1/20,358,520 c) 1/C(56, 6) = 1/32,468,436 d) 1/C(60, 6) = 1/ 50,063,860 27. a) 139,128/319,865 b) 212, 667/511,313 c) 151,340/386,529 d) 163,647/446,276 29. 1/C(100, 8) 31. 3/100 33. a) 1/7,880,400 b) 1/8,000,000 35. a) 9/19 b) 81/361 c) 1/19 d) 1,889,568/2,476,099 e) 48/361 37. Three dice 39. The door the contestant chooses is cho­ sen at random without knowing where the prize is, but the door chosen by the host is not chosen at random, because he always avoids opening the door with the prize. This makes S-42 Answers to Odd-Numbered Exercises any argument based on symmetry invalid. 41. a) 671/1296 b) 1 -3524/3624; no c) Yes Section 6.2 1. peT) = 1/4, pCB) = 3/4 3. p(l) = p(3) = p(5) = p(6) = 1/16;p(2) = p(4) = 3/8 5.9/49 7. a) 1/2 b) 1/2 c) 1/3 d) 1/4 e) 1/4 9. a) 1/26! b) 1/26 c) 1/2 d) 1/26 e) 1/650 f) 1/15,600 11. Clearly, peE U F) :::: peE) = 0.7. Also, peE U F) :::: 1. If we apply Theorem 2 from Section 6.1, we can rewrite this as peE) + p(F) -peE n F) :::: 1, or 0.7 + 0.5 -peE n F) :::: 1. Solv­ ing for peE n F) gives peE n F) :::: 0.2. 13. Because peE U F) = peE) + p(F) -peE n F) and peE U F) :::: 1, it follows that 1 :::: peE) + p(F) -peE n F). From this in­ equality we conclude that peE) + p(F) :::: 1 + peE n F). 15. We will use mathematical induction to prove that the inequality holds for n :::: 2. Let pen) be the statement that p(Uj=1 Ej) :::: 2:j=1 p(Ej). Basis step: P(2) is true because p(EI U E2) = p(EI) + p(E2) -p(EI n E2) :::: p(EI) + p(E2). Inductive step: Assume that P(k) is true. Using the basis case and the inductive hypothesis, it follows that p(U��: Ej) :::: P(U�=I Ej) + p(Ek+I) :::: 2:��: p(Ej). This shows that P(k + 1) is true, completing the proof by mathe­ matical induction. 17. Because E U E is the entire sample space S, the event F can be split into two disjoint events: F = S n F = (E U E) n F = (E n F) U (E n F), using the distributive law. Therefore, p(F) = p«E n F) U (E n F» = peE n F) + peE n F), because these two events are disjoint. Subtracting peE n F) from both sides, using the fact that peE n F) = peE) . p(F) (the hypothesis that E and F are independent), and factoring, we have p(F)(1- peE)] =-peE n F).yecause 1 -peE) = pee), this says that peE n F) = peE) . p(F), as desired. 19. a) 1/12 b) 1 -it . t¥ ..... 1�2n c) 5 21.614 23. 1/4 25.3/8 27. a) Not independent b) Not independent c) Not inde- pendent 29.3/16 31. a) 1/32 = 0.03125 b) 0.495 � 0.02825 c) 0.03795012 33. a) 5/8 b) 0.627649 c) 0.6431 35.a)pn b)l-pn c)pn+n·pn-I.(I-p) d) 1 - [pn + n . pn-I . (l -p)] 37. P(U�I Ei) is the sum of pes) for each outcome s in U�I Ei. Because the Eis are pairwise disjoint, this is the sum of the probabili­ ties of all the outcomes in any of the Eis, which is what 2:�1 p(Ei) is. (We can rearrange the summands and still get the same answer because this series converges absolutely.) 39. a) E = u�21 Fj, so the given inequality now follows from Boole's Inequality (Exercise 15). b) The probability that a particular player not in the jth set beats all k of the play­ ers in the jth set is (1/2)k = 2-k. Therefore, the probability that this player does not do so is 1 -2-k, so the probability that all m - k of the players not in the jth set are unable to boast of a perfect record against everyone in the jth set is (l -2-k)m-k. That is precisely p(Fj). c) The first inequal­ ity follows immediately, because all the summands are the same and there are (;) of them. If this probability is less than I, then it must be possible that E fails, i.e., that E happens. So there is a tournament that meets the conditions of the S-42 problem as long as the second inequality holds. d) m :::: 21 for k = 2, and m :::: 91 for k = 3 41. procedure probabi/istic prime(n, k) composite := false i:= ° while composite = false and i < k begin i := i + 1 choose b uniformly at random with 1 < b < n apply Miller's test to base b if n fails the test then composite := true end if composite = true then print ("composite") else print ("probably prime") Section 6.3 NOTE: In the answers for Section 6.3, all probabilities given in decimal form are rounded to three decimal places. 1. 3/5 3.3/4 5.0.481 7. a) 0.999 b) 0.324 9. a) 0.740 b) 0.260 c) 0.002 d) 0.998 11.0.724 13.3/17 15. a) 1/3 b) p(M = j I W = k) = 1 ifi, j, andk are dis­ tinct; p(M = j I W = k) = ° if j = k or j = i; p(M = j I W = k) = 1/2 if i = k and j 1= i c) 2/3 d) You should change doors, because you now have a 2/3 chance to win by switching. 17. The definition of conditional probability tellsusthatp(Fj I E) = peE n Fj)/p(E).Forthenumerator, again using the definition of conditional probability, we have peE n Fj) = peE I Fj)p(Fj), as desired. For the denomina­ tor, we show that peE) = 2:7=1 peE I Fi)P(Fi). The events En Fi partition the event E; that is, (E n Fi,) n (E n Fi,) = o when ii 1= i2 (because the Fi'S are mutually exclusive), and U7=1 (E n Fi,) = E (because the U7=1 Fi = S). Therefore, peE) = 2:7=1 peE n Fi) = 2:7=1 peE I Fi)P(Fi). 19. No 21. Yes 23. By Bayes' Theorem,p(S I EI n E2) = p(EI n E2 I S)p(S)/[P(EI n E2 I S)p(S) + p(EI n E2 I S)p(S)]. Because we are assuming no prior knowledge about whether a message is or is not spam, we set peS) = p(S) = 0.5, and so the equation above simplifies to pes lEI n E2) = peE I n E2 I S)/[p(EI n E2 I S) + p(EI n E2 I S)]. Because of the assumed independence of Et. E2, and S, we have p(EI n E2 I S) = p(EI I S) . p(E2 I S), and similarly for S. Section 6.4 1.2.5 3.5/3 5.336/49 7. 170 9. (4n + 6)/3 11.50, 700,551/10,077,696 � 5.03 13.6 15. p(X :::: j) = 2:�j p(X = k) = 2:�/1 -pi-I P = p(1 -py-I 2:�o(l - pi = p(1 -py-I/(l -(1 -p» = (1 -py-I 17.2302 19. (7/2)·71= 329/12 21. p + (n -l)p(1 -p) 23.5/2 25. a) ° b) n 27. a) We are told that XI and X 2 are independent. To see that X I and X 3 are independent, we enumerate the eight possibilities for (X I, X 2, X 3) and find that (0, 0, 0),(1, 0, 1),(0, 1, 1),(1, 1, 0) each have probability 1/4 and the others have probability ° (because of the definition ofX3).Thus,p(XI =O/\X3 =0)= 1/4,p(XI =0)= 1/2, S-43 and p(X3 = 0) = 1/2, so it is true that p(X I = 0/\ X3 = 0) = P(XI = 0)P(X3 = 0). Essentially the same calculation shows that p(XI = 0/\ X3 = 1) = P(XI = 0)p(X3 = 1), P(XI = 1/\ X3 = 0) = P(XI = l)p(X3 = 0), and P(XI = 1/\ X3 = 1) = P(XI = l)p(X3 = 1). Therefore by defini­ tion, X I and X 3 are independent. The same reasoning shows that X 2 and X 3 are independent. To see that X 3 and XI + X2 are not independent, we observe that p(X3 = 1/\ XI + X2 = 2) = O. But P(X3 = l)p(XI + X2 = 2) = (1/2)(1/4) = 1/8. b) We see from the calculation in part (a) that Xl. X2, and X3 are all Bernoulli random variables, so the variance of each is (1/2)(1/2) = 1/4. Therefore, V(XI) + V(X2) + V(X3):;: 3/4. We use the calculations in part (a) to see that E(XI + X2 + X3) = 3/2, and then V(XI + X2 + X3) = 3/4. c) In order to use the first part of Theorem 7to show that V«XI + X2 + ... + Xk)+ Xk+I) = V(XI + X2 + ... + Xk) + V(Xk+1) in the inductive step of a proof by mathematical induction, we would have to know that X I + X 2 + ... + Xk and Xk+ I are independent, but we see from part (a) that this is not necessarily true. 29. 1/100 31. E(X)/a = Lr(r/a)· p(X = r) 2: Lr>a 1· p(X = r) = p(X 2: a) 33. a) 10/11 b) 0.9984 -35. a) Each of the n! permutations occurs with probability l/n!, so E(X) is the number of comparisons, averaged over all these permu­ tations. b) Even if the algorithm continues n -1 rounds, X will be at most n(n -1)/2. It follows from the formula for expectation that E(X) ::; n(n -1)/2. c) The algorithm proceeds by comparing adjacent elements and then swap­ ping them if necessary. Thus, the only way that inverted el­ ements can become uninverted is for them to be compared and swapped. d) Because X(P) 2: I(P) for all P, it fol­ lows from the definition of expectation that E(X) 2: E(I). e) This summation counts 1 for every instance of an in­ version. t) This follows from Theorem 3. g) By Theo­ rem 2 with n = 1, the expectation of hk is the probabil­ ity that ak precedes a j in the permutation. This is clearly 1/2 by symmetry. b) The summation in part (f) consists of C(n, 2) = n(n -1)/2 terms, each equal to 1/2, so the sum is n(n -1)/4. i) From part (a) and part (b) we know that E(X), the object of interest, is at most n(n -1)/2, and from part (d) and part (h) we know that E(X) is at least n(n -1)/4, both of which are E>(n2). 37.1 39. V(X + Y) = E«X + y)2) _ E(X + y)2 = E(X2 + 2XY + y2) _ [E(X) + E(y)]2 = E(X2) + 2E(XY) + E(y2) -E(X)2 - 2E(X)E(Y) -E(y)2 = E(X2) -E(X)2 + 2[E(XY) - E(X)E(Y)] + E(y2) -E(y)2 = V(X) + 2Cov(X, Y) + V(Y) 41. [(n -1)/nr 43. (n -l)m /nm-I Suppleme ntary Exercises 1. 1/109,668 3. a) 1/C(52, 13) b) 4/C(52, 13) c) 2,944,656/C(52, 13) d) 35,335,872/C(52, 13) 5. a) 9/2 b) 21/4 7. a) 9 b) 21/2 9. a) 8 b) 49/6 11. a) n/2n-1 b) p(1 -p)k-I, where p = n/2n-1 c) 2n-1 /n 13. (m-I)(n-�):!�(m.n)-I 15. a) 2/3 b) 2/3 17. 1/32 19. a) The probability that one wins 2n dollars is Answers to Odd-Numbered Exercises 8-43 1 /2n , because that happens precisely when the player gets n - 1 tails followed by a head. The expected value of the winnings is therefore the sum of2n times 1/2n as n goes from 1 to infin­ ity. Because each of these terms is 1, the sum is infinite. In other words,.one should be willing to wager any amount of money and expect to come out ahead in the long run. b) $9, $9 21. a) 1/3 when S = {I, 2, 3, 4,5,6,7,8,9,10,11, 12}, A = {I, 2, 3, 4, 5, 6, 7, 8, 9},andB = {I, 2, 3, 4}; 1/12when S = {1,2, 3,4, 5, 6, 7, 8, 9,10,11, 12},A = {4, 5, 6, 7, 8, 9, 10,11, 12}, and B = {I, 2, 3, 4} b) 1 when S = {1,2, 3,4, 5,6,7,8,9, 10, II, 12}, A = {4, 5, 6, 7, 8, 9, 10, II, 12}, and B = {I, 2, 3, 4}; 3/4 when S = {I, 2,3,4,5,6,7,8,9, 10,11, 12},A = {I, 2,3,4,5,6,7,8, 9},andB = {l, 2, 3, 4} 23. a) p(EI n E2) = p(EI)p(E2), p(EI n E3) = p(EI) p(E3), p(E2 n E3) = P(E2)p(E3), p(E I n E2 n E3) = p(E I) P(E2)P(E3) b) Yes c) Yes; yes d) Yes; no e) 2n -n -1 25. V(aX + b) = E«aX + b)2) -E(aX + b)2 = E(a2X2 +2abX +b2) -[aE(X)+b]2 = E(a2X2)+ E(2abX) + E(b2) -[a2E(X)2 + 2abE(X) + b2] = a2E(X2) + 2abE(X) + b2 - a2E(X)2 -2abE(X)­b2 = a2[E(X2) -E(X)2] = a2V(X) 27. To count every element in the sample space exactly once, we must include every element in each of the sets and then take away the double counting of the elements in the intersections. Thus p(EI U E2 U ... U Em) = p(EI) + P(E2) + ... + p(Em)­ p(EI n E2) -p(EI n E3) -... -p(EI n Em) -P(E2 n E3) -P(E2 n E4) -... -P(E2 n Em) -... -p(Em-1 n Em) = qm -(m(m -1)/2)r, because C(m, 2) terms are be­ ing subtracted. But p(EI U E2 U··· U Em) = 1, so we have qm -[m(m -1)/2]r = 1. Because r 2: 0, this equation tells us that qm 2: 1, so q 2: l/m. Because q ::; 1, this equation also implies that [m(m -1)/2]r = qm -1 ::; m -1, from which it follows that r ::; 2/m. 29. a) We purchase the cards until we have gotten one of each type. That means we have purchased X cards in all. On the other hand, that also means that we purchased X 0 cards until we got the first type we got, and then purchased X I more cards un­ til we got the second type we got, and so on. Thus, X is the sum of the X / s. b) Once j distinct types have been obtained, there are n -j new types available out of a to­ tal of n types available. Because it is equally likely that we get each type, the probability of success on the next pur­ chase (getting a new type) is (n -j)/n. c) This follows immediately from the definition of geometric distribution, the definition of X j, and part (b). d) From part (c) it follows that E(Xj) = n/(n -j). Thus by the linearity of expectation and part (a), we have E(X) = E(Xo) + E(Xt} + ... + E(Xn-l) = !!. + ...!L + ... + !!. = n (! + _1_ + ... + !). e) About n n-I Inn-I I 224.46 31. 24.134/(52.51.50.49) CHAPTER 7 Section 7.1 l.a)2, 12,72,432,2592 b)2,4,16,256,65,536 c) 1,2,5,11,26 d) I, 1,6,27,204 e) 1,2,0,1,3 S-44 Answers to Odd-Numbered Exercises 3. a) 6, 17,49, 143,421 b) 49 = 5 . 17 -6·6,143 = 5 . 49-6·17,421=5·143-6·49 c)5an-I-6an-2 = 5(2n-1 + 5. 3n-l) -6(2n-2 + 5· 3n-2) = 2n-2(10 -6)+ 3n-2(75 -30) = 2n-2 . 4 + 3n-2 . 9 . 5 = 2n + 3n . 5 = an 5. a) Yes b) No c) No d) Yes e) Yes t) Yes g) No b) No 7. a) an-I + 2an-2 + 2n -9 = -(n -1) + 2 + 2 [-(n -2) + 2] + 2n -9 = -n +2 = an b) an-I + 2an_2+2n -9 = 5(-I)n-1 -(n -1)+2+2[5(-I)n-2- (n -2) + 2] + 2n -9 = 5( _l)n -2(_1 + 2) -n + 2 = an c) an-I +2an-2 +2n -9=3(-l)n-I+2n-I-(n -1)+2+ 2[3(-l)n-2 + 2n-2 -(n -2) + 2] + 2n -9 = 3(_I)n-2 (-1 + 2) + 2n-2 (2 + 2) -n + 2 = an d) an-I + 2an-2 + 2n -9 = 7· 2n-1 -(n -1) + 2 + 2[7· 2n-2_ (n -2) + 2] + 2n -9 = 2n-2(7 . 2 + 2 . 7) -n + 2 = an 9. a) an = 2 . 3n b) an = 2n + 3 c) an = 1 + n(n + 1)/2 d) an = n2 + 4n + 4 e) an = 1 t) an = (3n+1 -1)/2 g) an = 5n! b) an = 2nn! 11. a) an = 3an_1 b) 5,904,900 13. a) an = n + an-I, ao = 0 b) a\2 = 78 c) an = n(n + 1)/2 15. B(k) = [1 + (0.07/12)]B(k -1)-100, with B(O) = 5000 17. Let P(n) be "Hn = 2n -I." Basis step: P(1) is true because HI = 1. Inductive step: Assume that Hn = 2n -1. Then because Hn+1 = 2Hn + 1, it follows that Hn+1 =2(2n -1) + 1 = 2n+1 -1. 19. a) an = 2an_1 + an-s for n � 5 b) ao = 1, al = 2, a3 = 8, a4 = 16 c) 1217 21.9494 23. a) an = an-I + an-2 + 2n-2 for n � 2 b) ao = 0, al = 0 c) 94 25. a) an = an-I + an-2 + an-3 for n � 3 b) ao = 1, al = 2, a2 = 4 c) 81 27. a) an = an-I + an-2 for n � 2 b) ao = 1, al = 1 c) 34 29. a) an = 2an_1 + 2an-2 for n � 2 b) ao = 1, al = 3 c) 448 31. a) an = 2an-1 + an_2forn�2 b)ao=l,al=3 c) 239 33.a)an= 2an_1 for n � 2 b) al = 3 c) 96 35. a) an = an-I + an-2 for n � 2 b) ao = 1, al = 1 c) 89 37. a) Rn = n + Rn-t. Ro = 1 b) Rn = n(n + 1)/2 + 1 39. a) Sn = Sn-I + (n2 -n + 2)/2, So = 1 b) Sn = (n3 + 5n + 6)/6 41. 64 43. a) an = 2an-1 + 2an-2 b) ao = 1, al = 3 c) 1224 45. Clearly, S(m, 1) = 1 for m � 1. If m � n, then a function that is not onto from the set with m el­ ements to the set with n elements can be specified by picking the size of the range, which is an integer be­ tween 1 and n -1 inclusive, picking the elements of the range, which can be done in C(n, k) ways, and picking an onto function onto the range, which can be done in S(m, k) ways. Hence, there are L�:: C(n, k)S(m, k) func­ tions that are not onto. But there are nm functions altogether, so S(m, n) = nm -L�:: C(n, k)S(m, k). 47. a) Cs = COC4+CIC3+C2C2+C3CI +C4Co = 1·14+ 1·5+2· 2 + 5 . 1 + 14· 1 = 42 b) C(10, 5)/6 = 42 49. J(I) = 1, J(2) = 1, J(3) = 3, J(4) = 1, J(5) = 3, J(6) = 5, J(7) = 7, J(8) = 1, J(9) = 3, J(IO) = 5, J(II) = 7, J(12) =9, J(13) = 11, J(14) = 13, J(15) = 15, J(16) = 1 51. First, suppose that the number of people is even, say 2n. After going around the circle once and returning to the first person, because the people at locations with even numbers have been eliminated, there are exactly n people left and the person currently at location i is the person who was originally at lo­ cation 2i -1. Therefore, the survivor [originally in location S-44 J(2n)] is now in location J(n); this was the person who was at location 2J(n) -1. Hence, J(2n) = 2J(n) -1. Similarly, when there are an odd number of people, say 2n + 1, then after going around the circle once and then eliminating person 1, there are n people left and the person currently at location i is the person who was at location 2i + 1. Therefore, the survivor will be the player currently occupying location J (n), namely, the person who was originally at location 2J(n) + 1. Hence, J(2n + 1) = 2J(n) + 1. The base case is J(l) = 1. 53.73,977,3617 55. These nine moves solve the puzzle: Move disk 1 from peg 1 to peg 2; move disk 2 from peg 1 to peg 3; move disk 1 from peg 2 to peg 3; move disk 3 from peg 1 to peg 2; move disk 4 from peg 1 to peg 4; move disk 3 from peg 2 to peg 4; move disk 1 from peg 3 to peg 2; move disk 2 from peg 3 to peg 4; move disk 1 from peg 2 to peg 4. To see that at least nine moves are required, first note that at least seven moves are required no matter how many pegs are present: three to unstack the disks, one to move the largest disk 4, and three more moves to restack them. At least two other moves are needed, because to move disk 4 from peg 1 to peg 4 the other three disks must be on pegs 2 and 3, so at least one move is needed to restack them and one move to unstack them. 57. The base cases are obvious. If n > 1, the algorithm consists of three stages. In the first stage, by the inductive hypothesis, R(n -k) moves are used to transfer the smallest n -k disks to peg 2. Then using the usual three-peg Tower of Hanoi algorithm, it takes 2k -1 moves to transfer the rest of the disks (the largest k disks) to peg 4, avoiding peg 2. Then again by the inductive hypothesis, it takes R(n -k) moves to transfer the small­ est n -k disks to peg 4; all the pegs are available for this, because the largest disks, now on peg 4, do not interfere. This establishes the recurrence relation. 59. First note that R(n) = L�=I[RU) -RU -1)] [which follows because the sum is telescoping and R(O) = 0]. By Exercise 58, this is the sum of 2'" -I for this range of values of j. Therefore, the sum is L� = I i 2; -I, except that if n is not a triangular number, then the last few values when i = k are missing, and that is what the final term in the given expression accounts for. 61. By Exercise 59, R(n) is no larger than L�=I i2;-I. It can be shown that this sum equals (k + 1)2k -2k+1 + 1, so it is no greater than (k + 1 )2k . Because n > k( k -1)/2, the quadratic formula can be used to show that k < 1 + ..tIn for all n > 1. Therefore, R(n) is bounded above by (1 +..tIn + 1)21+J2Ii < 8.jii2J21i for all n > 2 .. Hence, R(n) is o (.jii2J21i). 63. a) 0 b) 0 c) 2 d) 2n-1 -2n-2 65. an -2Van + V2an = an -2(an -an-I) + (Van -Van-I) = -an + 2an_1 + [(an -an-I) -(an-I -an-2)] = -an + 2an_1 + (an -2an-1 + an-2) = an-2 67. an = an-I + an-2 = (an -Van) + (an -2Van + V2an) = 2an -3Van + V2an, or an = 3Van -V2an Section 7.2 1. a) Degree 3 b) No c) Degree 4 d) No e) No t) Degree 2 g) No 3. a) an = 3 ·2n b) an = 2 c) an = S-45 3·2n -2 . 3n d) an = 6· 2n -2 · n2n e) an = n( _2)n-1 1) an = 2n -(_2)n g) an = (1/2)n+1 -(-1/2)n+1 5. an = Js(1+2J5r+l-Js (,-2J5r+1 7. [2n+1 + (-I)n]/3 9. a) Pn = 1.2Pn-1 + 0.45Pn-2, Po = 100,000, PI = 120,000 b) Pn (250,000/3)(3/2)n + (50,000/3)(-3/10)n 11. a) Basis step: For n = 1 we have 1 = 0 + 1, and for n = 2 we have 3 = 1 + 2. Inductive step: Assume true for k :::: n. Then Ln+1 = Ln + Ln-I = In-I + fn+1 + 1n-2 + fn = (In-I + fn-2) + (In+1 + fn) = fn + 1n+2. b) Ln = ('+2J5r + (,-2J5r 13. an = 8( _l)n - 3( _2)n + 4 . 3n 15. an = 5 + 3( _2)n - 3n 17. Let an = C(n, 0) + C(n - 1,1) + ... + C(n -k, k) where k = Ln/2J. First, assume that n is even, so that k = n /2, and the last term is C(k, k). By Pascal's Identity we have an = 1 + C(n - 2,0) + C(n -2,1) + C(n -3,1) + C(n -3, 2) + ... + C(n -' k, k -2) + C(n -k, k -1) + 1 = 1 + C(n -2,1) + C(n -3, 2)+ ... +C(n -k, k -1)+C(n -2, 0) + C(n - 3, 1) + ... + C(n -k, k - 2) + 1 = a n-I + an-2 because L(n - 1)/2J = k - 1 = L(n -2)/2J. A similar calculation works when n is odd. Hence, {an} satisfies the recurrence re­ lation an = an-I + an-2 for all positive integers n, n 2: 2. Also, al = C(1, 0) = 1 and a2 = C(2, 0) + C(1, 1) = 2, which are h and h. It follows that an = fn+ I for all posi­ tive integers n. 19. an = (n2 + 3n + 5)( _l)n 21. (al 0 + 23 ' al,ln + al,2n + al,3n ) + (a2,O + a2,In + a2,2n2)( _2)n + (a3,O + a3,In)3n +a4,o(-4l 23. a) 3an-1 + 2n = 3( _2)n + 2n = 2n( -3 + 1) = _2n+1 = an b) an = a3n -2n+1 c)an=3n+I-2n+1 25.a)A=-I,B=-7 b)an= a2n -n -7 c) an = 11 ·2n -n -7 27. a) P3n3 + P2n2 + PIn + Po b) n2 Po( _2)n c) n2(pln + Po)2n d) (P2n2 + PIn + PoW e) n2(p2n2 + PIn + Po)( _2)n 1) n2(p4n4 + P3n3 + P2n2 + PIn + Po)2n g) Po 29. a) an = a2n + 3n+1 b) an = -2· 2n + 3n+1 31. an = a2n + {33n -n . 2n+1 + 3n/2 + 21/4 33. an = (a + {3n + n2+n3/6)2n 35. an = -4·2n -n2/4-5n/2+ 1/8+ (39/8)3n 37.an=n(n+l)(n+2)/6 39.a)1,-I,i, -i b) an = � -�(_I)n + 2ti in + 24"i (_i)n 41. a) Using the formula for fn, we see that I fn -Js (I +/'5r I = I Js (,-l)" I < 1/ J5 < 1/2. This means that In is the integer closest to Js (1+2J5 r . b) Less when n is even; greater when n is odd 43. an = fn-I + 21n - 1 45. a) an = 3an_1 + 4an-2, ao = 2, al = 6 b) an = [4"+1 + (-I)n]/5 47. a) an = 2an+1 + (n -1)10,000 b) an = 70,000 . 2n-1 -10,000n -10,000 49. an = 5n2/12 + 13n/12 + 1 51. See Chapter 11, Section 5 in [Ma93]. 53.6n. 4n-1 /n Section 7.3 1. 14 3. The first step is (1 1 lOh(lOlOh.= (24 + 22)(IIh (10)2 + 22[(Ilh - (10)2][(10)2 - (10)2] + (22 + 1)(lOh . (lOh. The product is (1000110%. 5. C = 50, 665C + 729 = 33,979 7. a) 2 b) 4 c) 7 9. a) 79 b) 48,829 c) 30,517,579 11. 0(logn) 13.0(nlog32) 15.5 Answers to Odd-Numbered Exercises S-45 17. a) Basis step: If the sequence has just one element, then the one person on the list is the winner. Recursive step: Divide the list into two parts-the first half and the second half-as equally as possible. Apply the algorithm recursively to each half to come up with at most two names. Then run through the entire list to count the number of occurrences of each of those names to decide which, if either, is the winner. b) O(n logn) 19. a) f(n) = f(n/2) + 2 b) O(logn) 21. a)7 b) O(logn) 23. a) procedure largest sum(al, ... , an) best:= 0 {empty subsequence has sum O} for i := 1 to n begin sum:= 0 for j := i + 1 to n begin sum := sum + aj if sum> best then best := sum end end {best is the maximum possible sum of numbers in the list} b) O(n2) c) We divide the list into a first half and a second half and apply the algorithm recursively to find the largest sum of consecutive terms for each half. The largest sum of consec­ utive terms in the entire sequence is either one of these two numbers or the sum of a sequence of consecutive terms that crosses the middle of the list. To find the largest possible sum of a sequence of consecutive terms that crosses the middle of the list, we start at the middle and move forward to find the largest possible sum in the second half of the list, and move backward to find the largest possible sum in the first half of the list; the desired sum is the sum of these two quantities. The final answer is then the largest of this sum and the two answers obtained recursively. The base case is that the largest sum of a sequence of one term is the larger of that number and o. d) 11,9, 14 e) S(n) = 2S(n/2) + n, C(n) = 2C(n/2) + n + 2, S(1) = 0, C(1) = 1 1) O(n logn), better than O(n2) 25. (1,6) and (3, 6) at distance 2 27. The algorithm is essentially the same as the algorithm given in Example 12. The central strip still has width 2d but we need to consider just two boxes of size d x d rather than eight boxes of size (d/2) x (d/2). The recurrence relation is the same as the recurrence relation in Example 12, except that the coefficient 7 is replaced by 1. 29. With k = 10gb n, it follows that f(n) = ak f(I) + LS:�aj c(n/bj)d = ak f(l) + L�:� cnd = ak f(l) + kcnd = alol!J,n f(l) + c(logb n)nd = nlogbQ f(I) +cnd 10gb n = nd f(l) + cnd 10gb n. 31. Let k = 10gb n where n is a power of b. Basis step: If n = 1 and k = 0, then clnd + C2nlol!J,a = CI + C2 = bdc/ (bd -a) + f(I) + bdc/(a -bd) = f(I). Inductive step: Assume true for k, where n = bk• Then for n = bk+l, f(n) = af(n/b) + cnd = a{ [bdc/(bd -a)](n/b)d + [f(I) + bdc/ (a -bd)] . (n/b)logbQ)} + cnd = bdc/(bd -a)nda/bd + [f(1) + bdc/(a -bd)]nlol!J,a + cnd = nd[ac/(bd -a) + c(bd - a)/(bd -a)] + [/(1) + bdc/(a -bdc)]nlogba = [bdc/(bd -a)]nd + [f(I) + bdc/(a -bd)]nJogbQ. 33. If S-46 Answers to Odd-Numbered Exercises a > bd, then 10gb a > d, so the second term dominates, giving O(nlDgJ,a). 35.0(nl"845) 37.0(n3) Section 7.4 1. f(x) = 2(x6 -1)/(x -1) 3. a) f(x) = 2x(1 -x6)/(1 - x) b) x3/(1 - x) c) x/(1 -x3) d) 2/(1 -2x) e)(1+ xf t) 2/(1 + x) g) [1/(1 -x)] -x2 h) x3 /(1 -x)2 5. a) 5/(1 -x) b) 1/(1 -3x) c) 2x3/(1 - x) d)(3 -x)/ (1 -x)2 e)(1 +x)8 t) 1/(I-x)5 7.a)ao = -64, al = 144, a2 = -108, a3 = 27, and a" = 0 for all n � 4 b) The only nonzero coefficients are ao = 1, a3 = 3, a6 = 3, a9 = 1. c) a" = 5" d) an = (_3)n-3 for n � 3, andao = al = a2 = 0 e) ao = 8, al = 3, a2 = 2, a" = 0 for odd n greater than 2 and a" = 1 for even n greater than 2 t) an = 1 if n is a positive multiple 4, an = -1 if n < 4, and an = 0 otherwise g) a" = n -1 for n � 2 and ao=al =0 h) an =2,,+I/n! 9.a)6 b)3 c)9 d)O e) 5 11. a) 1024 b) 11 c) 66 d) 292,864 e) 20,412 13.10 15.50 17.20 19.f(x) = 1/[(I-x)(I-x2) (1 -x5)(1 -x 10)] 21. 15 23. a) x4(1 + x + x2 + x3)2/ (1 -x) b) 6 25. a) The coefficient of xr in the power se­ ries expansion of 1/[(1 -x3)(1 -x4)(1 -x20)] b) 1/(1 - x3 -X4 -x20) c) 7 d) 3224 27. a) 3 b) 29 c) 29 d) 242 29. a) 10 b) 49 c) 2 d) 4 31. a) G(x)-ao -alx -a2x2 b) G(x2) c) x4G(x) d) G(2x) e) J; G(t)dt t) G(x)/(1 -x) 33. ak = 2· 3k - 1 35. ak = 18· 3k -12· 2k 37. ak = k2 + 8k + 20 + (6k- 18)2k 39. Let G(x) = L�o fkXk. After shifting indices of summation and adding series, we see that G(x) -xG(x)­ x2G(x)=fo + (fi -fo)x + L�2(fk - fk-I - fk_2)Xk = 0+ x + L�20xk. Hence, G(x)-xG(x)-x2G(x)=x. Solving for G(x) gives G(x) = x/(1 -x -x2). By the method of partial fractions, it can be shown that x / (1 -x - x2) = (1/.J5)[1/(1 -ax) -1/(1 -fJx)], where a = (1 + .J5)/2andfJ = (1 - .J5)/2. Using the fact that 1/(1 -ax) = L�oakxk, it follows that G(x) = (1/.J5). L�o(ak­ fJk)xk. Hence,jk = (1/.J5) . (ak -fJk). 41. a) LetG(x) = L�o C "x" be the generating function for {C,,}. Then G(x)2 = L;o(L�=o CkC,,-k)x" = L�I (L�:� Ck C,,_I_k)X,,-1 = Ln=1 c"xn-I. Hence, xG(x)2 = L�I C"xn, which implies that xG(x)2 -G(x) + 1 = O. Applying the quadratic for­ mula shows that G(x) = I±J�-4x. We choose the minus sign in this formula because the choice of the plus sign leads to a division by zero. b) By Exercise 40, (1 -4x)-1/2 = L� e,,")x". Integrating term by term (which is valid by a theorem from calculus) shows that J;(1 -4t)-1/2dt = �oo I (2,,) ,,+1 �OO, (2,,) n B rX(1 L...,,=O ,,+1 " x = X L • .n=O ,,+' " x. ecause Jo - 4t)-1/2dt = I-Jt4x = xG(x),equatingcoefficients shows that C" = II! , e:). 43. Applying the Binomial Theorem to the equality (I + x)m+" = (1 + xr(l + x)", shows that L�!o" C(m + n, r)xr = L�=o C(m, r)xr. Lr=o C(n, r)xr = L�:; [L�=oC(m,r -k)C(n,k)] xr. Comparing coeffi­ cients gives the desired identity. 45. a) 2e b) e-X c) e3x d) xe + e e) (e -I)/x 47. a) a" = (_I)n b) an = S-46 3·2" c) a" =3"-3·2" d)a,,=(-2)nforn�2,al= -3, ao=2 e)a,,=(-2)"+n! t)a,,=(-3)"+n!·2" forn � 2,ao = l,al = -2 g) a" = Oifn is odd and a" = n!/(n/2)! ifn is even 49. a) a" = 6a,,_1 + 8,,-1 for n � I, ao = 1 b) The general solution of the associated linear ho­ mogeneous recurrence relation is a�h) = a6". A particular solution is a�p) = ! . 8" . Hence, the general solution is an = a6" +! . 8". Using the initial condition, it follows that a = !. Hence, a" = (6" + 8")/2. c) Let G(x) = L�o akxk. Using the recurrence relation for {ak}, it can be shown that G(x) -6xG(x) = (I -7x)/(1 -8x). Hence, G(x) = (l - 7x)/[(1 -6x)(1 -8x)]. Using partial fractions, it follows that G(x) = (1/2)/(1 -6x) + (1/2)/(1 - 8x). With the help of Table l,itfollowsthata" = (6" + 8")/2. 51. I�X • 2xr . 2xr ... 53.(I+x)(I +x)2(I+x)3 ... 55.The gener­ ating functions obtained in Exercises 52 and 53 are equal be- (I )(1 2)(1 3) I x2 I x' ,x6 cause + x + x + x ... = ,-='x . R . R ... = ,�x . 2xr . �"" 57. a) Gx(l) = L�oP(X = k)· Ik = L�o P(X = k) = I b) G�(1) = 1; L�oP(X = k) . xklx=1 = L�o p(X = k) . k . Xk-llx=1 = L�o p(X = k) . k = E(X) c) G�(l) = � L�o p(X = k) . xklx=1 = L,f=oP(X = k)· k(k -1)· Xk-2Ix=1 = L�oP(X = k)· (Jr -k) = V (X) + E(X)2 -E(X). Combining this with part (b) gives the desired results. 59. a) G(x) = pm /(1 -qx)m b) V(x) = mq/p2 Section 7.S 1. a)30 b) 29 c) 24 d) 18 3. 1% 5. a) 300 b) 150 c) 175 d) 100 7.492 9.974 11.55 13.248 15.50,138 17.234 19.IAI U A2 U A3 U A4 U A51 = lAd + IA21 + IA31 + IA41 + IA51-IA, n A21-IA, n A31-IA, n A41- IA,nA51-IA2nA31-IA2nA41-IA2nA51-IA3n A41 '-IA3 n A51 -IA4 n A51 + IAI n A2 n A31 + IAI n A2 n A41 + IAI n A2 n A51 + IAI n A3 n A41 + IAI n A3 n A51 + lA, n � n A51 + IA2 n A3 n A41 + IA2 n A3 n A51 + IA2 n A4 n A51 + IA3 n A4 n A51 -IAI n A2 n A3 n A41 - IAI n A2 n A3 n A51 -lA, n A2 n A4 n A51 -lA, n A3 n A4nA51-IA2nA3 nA4 nA51 + lA, nA2nA3 nA4nA51 21.IAI U A2 U A3 U A4 U A5 U A61 = IAII + IA21 + IA31 + IA41 + IA51 + IA61-IAI n A21-IAI n A31 -IAI n A41 - IAI n A51 -IAI n A61 -IA2 n A31 -IA2 n A41 -IA2n A51 -IA2 n A61 -IA3 n A41 -IA3 n A51 -IA3 n A61- IA4 n A51 -IA4 n A61 - IA5 n A61 23.p(E, U E2 U E3) = p(E,) + p(E2) + p(E3) -p(E, n E2) -p(E, n E3) -P(E2 n E3) + p(EI n E2 n E3) 25.4972/71,295 27.p(EI U E2 U E3 U E4 U E5)=p(EI)+ p(E2) + P(E3) + p(E4) + P(E5) - p(EI n E2) - p(EI n E3) - p(EI n E4) - p(E, n E5) -P(E2 n E3) -P(E2 n E4) -P(E2 n E5) -P(E3 n E4) -p(E3 n E5) -P(E4 n E5) + p(EI n E2 n E3) + p(E, n E2 n E4) + p(EI n E2 n E5) + p(EI n E3 n E4) + p(EI n E3 n E5) + p(EI n E4 n E5) + p(E2 n E3 n E4) + P(E2 n E3 n E5) + p(E2 n E4 n E5) + P(E3 n E4 n E5) 29.p (U7=, Ei) = LI�i�"p(Ei)- S-47 'L,1�i<j�np(Ei n Ej) + 'L,lg<j<k�n P(Ei n Ej n Ek)­ ... + (_l)n+lp (n7=1 Ei) Section 7.6 1. 75 3.6 5.46 7.9875 9.540 11. 2100 13.1854 15. a) DI00/100! b) 100D99/IOO! c) C(100,2)/100! d) 0 e) 1/100! 17. 2,170,680 19. By Exercise 18 we have Dn -nDn-1 = -[Dn-I -(n - I)Dn-2]. Iterating, we have Dn -nDn-1 = -[D,,_I-(n-l)D,,_2] = -[-(Dn-2- (n -2)D,,_3)] = Dn-2 -(n -2)Dn-3 = ... = (-I)n(D2 - 2DI) = (_I)" because D2 = 1 and DI = O. 21. When n is odd 23. ¢(n) = n -'L,:I * + 'L,1�i<j�m Pinpj -... ± _n_ = n 0'."-1 (a -1..) 25.4 27. There are nm P,{J2"""Pm .-PI functions from a set with m elements to a set with n el- ements, C(n, 1)(n - l)m functions from a set with m ele­ ments to a set with n elements that miss exactly one element, C(n,2)(n -2)m functions from a set with m elements to a set with n elements that miss exactly two elements, and so on, with C(n, n - I) . 1m functions from a set with m elements to a set with n elements that miss exactly n - 1 elements. Hence, by the principle of inclusion-exclusion, there are nm -C(n, 1)(n -l)m + C(n, 2)(n -2)m -... + (_l)n-lC(n, n - 1)· 1m onto functions. Suppleme ntary Exercises 1. a) A" = 4An_1 b) AI = 40 c) An = 10. 4n 3. a) Mn = M,,_I + 160,000 b) MI = 186,000 c) Mn = 160,000n + 26,000 d) T" = Tn-I + 160,000n + 26,000 e) T" = 80,000n2 + 106,000n 5. a) an = an-2 + an-3 b) al = 0, a2 = 1, a3 = 1 c) al2 = 12 7. a) 2 b) 5 c)8 d) 16 9.an=2n l1.an=2+4n/3+n2/2+ n3/6 13. a" = a,,-2 + an-3 15. O(n4) 17. O(n) 19. a) 18n + 18 b) 18 c) 0 21. d(a"b,,) = an+lb,,+1- a"b" = a,,+I(bn+l - b,,) + b,,(a,,+1 - an) = an+ldb" + b" da" 23. a) Let G(x) = 'L,:o a"x" . Then G' (x) = 'L,:I na"x,,-I = 'L,:o(n + l)a,,+lxn. Therefore, G'(x)­ G'(x) = 'L,:o[(n + l)a,,+I-an]x" = 'L,n=Ox"/n! = eK, as desired. That G(O) = ao = 1 is given. b) We have [e-xG(x)]' = e-xG'(x) -e-xG(x) = e-X[G'(x) -G(x)] = e-X. eK = 1. Hence, e-xG(x) = x + c, where c is a con­ stant. Consequently, G(x) = xeK + ceK. Because G(O) = 1, it follows that c = 1. c) We have G(x) = 'L,:ox,,+I/n!+ 'L,:ox"/n! = 'L,:lxn/(n -I)! + 'L,:ox"/nL There­ fore, an = 1/(n - I)! + l/n! for all n ::: 1, and ao = 1. 25.7 27.110 29.0 31. a) 19 b) 65 c) 122 d) 167 e) 168 33. Dn-t/(n - I)! 35.11/32 CHAPTER S Section 8.1 1. a) {(O, 0), (1, 1), (2, 2), (3, 3)} b) {(l, 3), (2, 2), (3, 1), (4,0)} c) {(I, 0), (2, 0), (2,1), (3, 0), (3,1), (3, 2), (4, 0), Answers to Odd-Numbered Exercises 5-47 (4,1), (4, 2), (4, 3)} d) {(I, 0), (1,1), (1, 2), (1, 3), (2, 0), (2,2), (3, 0), (3, 3), (4, O)} e) {(O, 1), (1, 0), (1,1), (1, 2), (1,3), (2, 1),(2,3),(3,1), (3, 2), (4,1), (4, 3)} I) {(I, �), (2, 1), (2, 2)} 3. a) Transitive b) Reflexive, symmetrIc, transitive c) Symmetric d) Antisymmetric e) Reflex­ ive, symmetric, antisymmetric, transitive I) None of these properties 5. a) Reflexive, transitive b) Symmetric c) Symmetric d) Symmetric 7. a) Symmetric b) Symme­ tric transitive c) Symmetric d) Reflexive, symmetric, �sitive e) Reflexive, transitive I) Reflexive, symmetric, transitive g) Antisymmetric b) Antisymmetric, transitive 9. (c), (d), (I) 11. a) Not irreflexive b) Not irreflexive c) Not irreflexive d) Not irreflexive 13. Yes, for instance {(l, I)} on {1,2} 15. (a, b) E R if and only if a is taller than b 17. (a) 19. None 21. VaVb [(a, b) E R -+ (b, a) ¢ R] 23.2mn 25. a) {(a, b) I b divides a} b) {(a, b) I a does not divide b} 27. The graph of I-I 29. a) {(a, b) I a is required to read or has read b} b) {(a, b) I a is required to read and has read b} c) {(a, b) I either a is required to read b but has not re�d it or a has read b but is not required to} d) {(a, b) I a IS required to read b but has not read it} e) {(a, b) I a has read b but is not required to} 31. S oR = {(a, b) I a is a parent of band b has a sibling}, R 0 S = {(a, b) I a is an aunt or un­ cle of b} 33. a) R2 b) R6 c) R3 d) R3 e) 0 I) RI g) R4 b) R4 35. a) RI b) R2 c) R3 d) R2 e) R3 I) R2 g) R2 b) R2 37. b got his or her doctorate �der someone who got his or her doctorate under a; there IS a sequence of n + 1 people, starting with a and ending with b, such that each is the advisor of the next person in the sequence 39. a) {(a, b) I a - b == 0, 3, 4, 6, 8, or 9 (mod 12)} b) {(a, b) I a == b (mod 12)} c) {(a, b) I a -b == 3, 6, or 9 (mod 12)} d) {(a, b) I a -b == 4 or 8 (mod 12)} e) {(a, b) I a -b == 3, 4, 6, 8, or 9 (mod 12)} 41.8 43. a) 65,536 b) 32,768 45. a) 2n(n+I)/2 b) 2n3n(n-I)/2 c) 3,,(n-I)/2 d) 2,,(n-l) e) 2n(,,-I)/2 t) 2,,2 - 2 . 2,,(,,-1) 47. There may be no such b. 49. If R is symmetric and (a, b) E R, then (b, a) E R, so (a, b) E R-I. Hence, R � R-I. Similarly, R-I � R. So R = R-I. Conversely, if R = R-I and (a, b) E R, then (a, b) E R-I, so (b, a) E R. Thus R is symmetric. 51. R is reflexive if and only if (a, a) E R for all a E A if and only if(a, a) E R-I [because (a, a) E R if and only if(a, a) E R-I] ifand only if R-I is re­ flexive. 53. Use mathematical induction. The result is trivial for n = 1. Assume R" is reflexive and transitive. By Theorem 1, R,,+I � R. To see that R � R,,+1 = R" 0 R, let (a, b) E R. By the inductive hypothesis, R" = R and hence, is reflexive. Thus (b, b) E R". Therefore (a, b) E R"+I. 55. Use math­ ematical induction. The result is trivial for n = 1. Assume R" is reflexive. Then (a, a) E R" for all a E A and (a, a) E R. Thus (a, a) E R" 0 R = R,,+1 for all a E A. 57. No, for instance, take R = {(I, 2), (2, I)}. Section 8.2 1. {(I, 2, 3), (1,2,4), (1, 3,4), (2, 3, 4)} 3. (Nadir, 122,34, Detroit, 08: 10), (Acme, 221, 22, Denver, 08: 17), (Acme, 122, S-48 Answers to Odd-Numbered Exercises 33, Anchorage, 08:22), (Acme, 323, 34, Honolulu 08:30), (Nadir, 199, 13, Detroit, 08:47), (Acme, 222, 22, Denver, 09: 10), (Nadir, 322, 34, Detroit, 09:44)5. Airline and flight number, airline and departure time 7. a) Yes b) No c) No 9. a) Social Security number b) There are no two people with the same name who happen to have the same street address. c) There are no two people with the same name living together. 11. (Nadir, 122, 34, Detroit, 08: 10), (Nadir, 199, 13, Detroit, 08: 47), (Nadir, 322, 34, Detroit, 09: 44) 13. (Nadir, 122,34, Detroit, 08: 10), (Nadir, 199, 13, Detroit, 08: 47), (Nadir, 322, 34, Detroit, 09: 44), (Acme, 221,22, Denver, 08: 17), (Acme, 222, 22, Denver, 09: 10) 15. P3.5.6 17. Airline Destination Nadir Detroit Acme Denver Acme Anchorage Acme Honolulu 19. Part- eolor_ Supplier number Project Quantity code 23 1092 1 2 2 23 1l0I 3 .. 1 23 9048 4 12 2 31 4975 3 6 2 31 3477 2 25 2 32 6984 4 10 1 32 9191 2 80 4 33 1001 1 14 8 21. Both sides of this equation pick out the subset of R con­ sisting of those n-tuples satisfying both conditions CI and C2. 23. Both sides of this equation pick out the set of n-tuples that are in R, are in S, and satisfy condition C. 25. Both sides of this equation pick out the m-tuples con­ sisting ofilth, i2th, ... , imth components ofn-tuples in either R or S. 27. Let R = {(a, b)} and S = {(a, c)}, n = 2, m = 1, and il = 1; PI(R - S) = {(a)}, but PI(R) -PI(S) = 121. 29. a) h followed by PI,3 b) (23, I), (23, 3), (31, 3), (32, 4) 31. There is no primary key. Section 8.3 I. a) U i i] b) [! 1 �] ,) U 0 :J d) [� � n 3. a)(I, 1),(1,3),(2,2),(3,1),(3,3) b)(I, 2),(2, 2),(3, 2) c)(I, 1), (1,2), (1,3), (2,1), (2,3), (3,1), (3,2), (3,3) 5. The relation is irreflexive if and only if the main diagonal of the matrix contains only Os. 7. a) Reflexive, symmet­ ric, transitive b) Antisymmetric, transitive c) Symmetric 9. a) 4950 b) 9900 c) 99 d) 100 e) 1 each 0 to a 1 and each 1 to a O. 13. a) U 1 n 1 0 IS. a) [! 0 n 1 1 17. n2 - k 19.0)1:21 2 3 ')0' 2 3 b) [i 0 n ,t 0 1 b)U 1 n ,) [1 1 1 b)� I . 4 b d d)I� ' 2 3 S-48 11. Change lJ lJ 21. For simplicity we have indicated pairs of edges between the same two vertices in opposite directions by using a double arrowhead, rather than drawing two separate lines. C)172 3� 4 23. {(a, b), (a, c), (b, c), (c, b)} 25. (a, c), (b, a), (c, d), (d, b) 27. {(a, a), (a, b), (a, c), (b, a), (b, b), (b, c), (c, a), (c, b), (d, d)} 29. The relation is asymmetric if and only if the directed graph has no loops and no closed paths of length 2. 31. Exercise 23: irreflexive. Exercise 24: reflex­ ive, antisymmetric, transitive. Exercise 25: irreflexive, anti­ symmetric. 33. Reverse the direction on every edge in the digraph for R. 35. Proof by mathematical induction. Ba­ sis step: Trivial for n = 1. Inductive step: Assume true for k. Because Rk+\ = Rk 0 R, its matrix is MR <:) MRk. By the inductive hypothesis this is MR <:) M�l = M�+ll. Section 8.4 1. a) {(O, 0), (0, 1), (I, 1), (1,2), (2,0), (2,2), (3,0), (3, 3)} b) {(O, 1), (0,2), (0,3), (1,0), (I, 1), (1,2), (2,0), (2,1), (2,2), (3, O)} 3. {(a, b) I a divides b or b divides a} 5.� 7.� � � � S-49 .'0)0 b)V, ')� cd c d c d 11.a)� b)� � CP dO c)� d� 13. The symmetric closure of R is R U R-1• MRUR-I = MR V MR-I = MR V M�. 15. Only when R is irreflexive, in which case it is its own closure. 17. a, a, a, a; a, b, e, ����������������� ���� �� ������ ���� ���������� ��� e, d, e; e, e, e, e 19. a) {(1, 1), (1,5), (2,3), (3, 1), (3,2), (3,3), (3, 4), (4, 1), (4, 5), (5, 3), (5, 4)} b) {(I, 1), (1, 2), (1,3), (1,4), (2, 1), (2,5), (3, 1), (3,3), (3,4), (3,5), (4, 1), (4,2), (4, 3), (4, 4), (5,1), (5, 3), (5, 5)} c) {(I, 1), (1, 3), (1,4), (1,5), (2, 1), (2,2), (2,3), (2,4), (3, 1), (3,2), (3,3), (3,4), (3,5), (4, 1), (4,3), (4,4), (4,5), (5, 1), (5,2), (5,3), (5,4), (5, 5)} d){(I, 1), (1,2), (1,3), (1,4), (1,5), (2,1), (2,3), (2,4), (2,5), (3, 1), (3,2), (3,3), (3,4), (3,5), (4, 1), (4,2), (4,3), (4,4), (4,5), (5, 1), (5,2), (5,3), (5,4), (5, 5)} e) {(I, 1),(1,2),(1,3),(1,4),(1,5),(2,1),(2,2),(2,3),(2,4), (2,5), (3, 1), (3,2), (3,3), (3,4), (3,5), (4, 1), (4,2), (4,3), (4,4), (4,5), (5, 1), (5,2), (5,3), (5,4), (5,5)} 1) {(1, 1), (1,2), (1,3), (1,4), (1, 5), (2, 1), (2,2), (2, 3), (2,4), (2, 5), (3, 1), (3,2), (3,3), (3,4), (3,5), (4, 1), (4,2), (4,3), (4,4), (4,5), (5, 1), (5,2), (5,3), (5,4), (5, 5)} 21. a) If there is a student c who shares a class with a and a class with b b) If there are two students c and d such that a and c share a class, c and d share a class, and d and b share a class c) If there is a sequence So, ••. , Sn of students with n 2': 1 such that So = a, Sn = b, and for each i = 1, 2, ... , n, Sj and Sj-l share a class 23. The result follows from (R*)-l = (UOO Rn)-l = UOO (Rn)-l = UOO Rn = R*. n=l n=l n=l 2s.0T 1] bT 0 0 I] 0 1 0 1 0 1 ,) [� 1 1 i] dT 1] 0 1 0 0 0 0 Answers to Odd-Numbered Exercises S-49 27. Answers same as for Exercise 25. 29. a) {(I, 1), (1,2), (1,4), (2,2), (3,3), (4,1), (4,2), (4,4)} b){(1,I), (1,2), (1,4), (2, 1), (2,2), (2,4), (3,3), (4, 1), (4,2), (4, 4)} c) {(I, 1), (1,2), (1,4), (2, 1), (2,2), (2,4), (3,3), (4,1), (4,2), (4, 4)} 31. Algorithm 1: O(n3.8); Algorithm 2: O(n3) 33. Initialize with A := MR V In and loop only for i := 2 to n -1. 35. a) Because R is reflexive, every relation contain­ ing it must also be reflexive. b) Both {(O, 0), (0, 1), (0,2), (1, 1), (2, 2)} and {(O, 0), (0, 1), (1,0), (1, 1), (2, 2)} contain R and have an odd number of elements, but neither is a subset of the other. Section 8.5 1. a) Equivalence relation b) Not reflexive, not transitive c) Equivalence relation d) Not transitive e) Not symme­ tric, not transitive 3. a) Equivalence relation b) Not transitive c) Not reflexive, not symmetric, not transitive d) Equivalence relation e) Not reflexive, not transitive 5. Many answers are possible. (1) Two buildings are equiva­ lent ifthey were opened during the same year; an equivalence class consists of the set of buildings opened in a given year (as long as there was at least one building opened that year). (2) Two buildings are equivalent if they have the same num­ ber of stories; the equivalence classes are the set of I-story buildings, the set of 2-story buildings, and so on (one class for each n for which there is at least one n-story building). (3) Every building in which you have a class is equivalent to every building in which you have a class (including itself), and every building in which you don't have a class is equivalent to every building in which you don't have a class (including itself); there are two equivalence classes-the set of buildings in which you have a class and the set of buildings in which you don't have a class (assuming these are nonempty). 7. The statement "p is equivalent to q" means that p and q have the same entries in their truth tables. R is reflexive, because p has the same truth table as p. R is symmetric, for if p and q have the same truth table, then q and p have the same truth table. If p and q have the same entries in their truth tables and q and r have the same entries in their truth tables, then p and r also do, so R is transitive. The equivalence class of T is the set of all tautologies; the equivalence class of F is the set of all contradictions. 9. a) (x, x) E R because J(x) = J(x). Hence, R is reflexive. (x, y) E R if and only if J(x) = J(Y), which holds if and only if J(y) = J(x) if and only if(y, x) E R. Hence, R is symmetric. If(x, y) E R and (y, z) E R, then J(x) == J(y) and J(y) = J(z). Hence, J(x) = J(z). Thus, (x, z) E R. It follows that R is transi­ tive. b) The sets J-1(b) for b in the range of J 11. Let x be a string of length 3 or more. Because x agrees with itself in the first three bits, (x, x) E R. Hence, R is reflex­ ive. Suppose that (x, y) E R. Then x and y agree in the first three bits. Hence, y and x agree in the first three bits. Thus, (y, x) E R. If (x, y) and (y, z) are in R, then x and y agree in the first three bits, as do y and z. Hence, x and z agree in the first three bits. Hence, (x, z) E R. It follows that R S-50 Answers to Odd-Numbered Exercises is transitive. 13. This follows from Exercise 9, where I is the function that takes a bit string of length 3 or more to the ordered pair with its first bit as the first component and the third bit as its second component. 15. For reflexivity, «a, b), (a, b» E R because a + b = b + a. For symmetry, if «a, b), (c, d» E R,thena + d = b + C,soc + b = d +a,so «c, d), (a, b» E R. For transitivity, if«a, b), (c, d» E Rand «c, d), (e, I» E R, then a + d = b + c and c + e = d + I, so a + d + c + e = b + c + d + I, so a + e = b + I, so «a, b), (e, I» E R. An easier solution is to note that by al­ gebra, the given condition is the same as the condition that I«a, b» = I«c, d», where I«x, y» = x -y; therefore by Exercise 9 this is an equivalence relation. 17. a) This fol­ lows from Exercise 9, where the function I from the set of differentiable functions (from R to R) to the set of functions (from R to R) is the differentiation operator. b) The set of all functions of the form g(x) = x2 + C for some constant C 19. This follows from Exercise 9, where the function I from the set of all URLs to the set of all Web pages is the func­ tion that assigns to each URL the Web page for that URL. 21. No 23. No 25. R is reflexive because a bit string s has the same number of 1 s as itself. R is symmetric because sand t having the same number of 1 s implies that t and s do. R is transitive because s and t having the same number of 1 s, and t and u having the same number of 1 s implies that s and u have the same number of Is. 27. a) The sets of people of the same age b) The sets of people with the same two parents 29. The set of all bit strings with ex­ actly two 1 s. 31. a) The set of all bit strings that begin 010 b) The set of all bit strings that begin 101 c) The set of all bit strings that begin III d) The set of all bit strings that begin 010 33. Each of the 15 bit strings of length less than four is in an equivalence class by itself: [).]R4 = {).}, [0]R4 = {OJ, [1]R4 = {I}, [00]R4 = {OO}, [OI]R4 = {OIl, ... , [111 ]R4 = {Ill}. The remaining 16 equivalence classes are determined by the bit strings of length 4: [0000]R4 = {OOOO, 00000,00001,000000,000001,000010,000011,0000000, ... }, [000I]R4 = {OOOI, 00010, 00011, 000100, 000101, 000110, 000111, 0001000, ... }, ... , [1111]R4 = {Ill 1, 11110, lIllI, 111100, 111101, 111110, 111111,1111000, ... } 35. a) [215 = {iii =2 (mod 5)} = { ... ,-8, -3,2,7, 12, ... } b) [315 = {iIi = 3 (mod 5)} = { ... , -7, -2,3, 8, 13, ... } c) [615 = {iii = 6 (mod 5)} = { ... , -9, -4, I, 6,11, ... } d) [-315 = {iii = -3 (mod 5)} = { ... , -8, -3,2,7,12, ... } 37. {6n + kin E Z} for k E {O, 1,2, 3,4,5} 39. a) [(1, 2)] = {(a, b) I a -b = -I} = {(I, 2), (3,4), (4, 5), (5, 6), ... } b) Each equivalence class can be interpreted as an integer (negative, positive, or zero); specificall y, [a, b] can be interpreted as a-b. 41. a) No b) Yes c) Yes d) No 43. (a), (c), (e) 45. (b), (d), (e) 47. a) {(O, 0),(1,1),(1,2), (2,1), (2, 2),(3, 3), (3,4), (3,5), (4,3), (4,4), (4,5), (5,3), (5,4), (5,5)} b) {(O, 0), (0, I), (1,0), (1, 1), (2,2), (2, 3), (3,2), (3,3), (4,4), (4,5), (5,4), (5,5)} c) {(O,O), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2),(3,3),(3,4), (3,5), (4,3), (4,4), (4,5), (5,3), (5, 4), (5, 5)} d) {(O,O), (1,1), (2,2), (3,3), (4,4), (5,5)} 49. [0]6 � [013, [1]6 � [lh, [2]6 � [213, [3]6 � [013, S-50 [4]6 � [113, [5]6 � [213 51. Let A be a set in the firstparti­ tion. Pick a particular element x of A. The set of all bit strings of length 16 that agree with x on the last eight bits is one of the sets in the second partition, and clearly every string in A is in that set. 53. We claim that each equivalence class [x ]R31 is a subset of the equivalence class [x ]Rg' To show this, choose an arbitrary element y E [X]R31' Then y is equivalent to x under R3', so either y = x or y and x are each at least 31 characters long and agree on their first 31 characters. Because strings that are at least 31 characters long and agree on their first 31 characters perforce are at least 8 characters long and agree on their first 8 characters, we know that either y = x or y and x are each at least 8 characters long and agree on their first 8 characters. This means that y is equivalent to x under Rg, so y E [X]Rg' 55. {(a, a), (a, b), (a, c), (b, a), (b, b), (b, c), (c, a), (c, b), (c, c), (d, d), (d, e), (e, d), (e, e)} 57. a) Z b) {n + ! In E Z} 59. a) R is reflexive because any coloring can be obtained from itself via a 360-degree rotation. To see that R is symmetric and transitive, use the fact that each rotation is the composition of two reflections and conversely the composition of two reflections is a ro­ tation. Hence, (C" C2) belongs to R if and only if C2 can be obtained from C, by a composition of reflections. So if (C" C2) belongs to R, so does (C2, Cl) because the inverse of the composition of reflections is also a composition of reflections (in the opposite order). Hence, R is symmetric. To see that R is transitive, suppose (Cl, C2) and (C2, C3) belong to R. Taking the composition of the reflections in each case yields a composition of reflections, showing that (C, , C 3) belongs to R. b) We express colorings with se­ quences of length four, with r and b denoting red and blue, respectively. We list letters denoting the colors of the upper left square, upper right square, lower left square, and lower right square, in that order. The equivalence classes are: {rrrr}, {bbbb}, {rrrb, rrbr, rbrr, brrr}, {bbbr, bbrb, brbb, rbbb}, {rbbr, brrb}, {rrbb, brbr, bbrr, rbrb}. 61. 5 63. Yes 65. R 67. First form the reflexive closure of R, then form the symmetric closure of the reflexive closure, and finally form the transitive closure of the symmetric closure of the reflexive closure. 69. p(O) = 1, p(I) = 1, p(2) = 2, p(3) = 5, p(4) = 15, p(5) = 52, p(6) = 203, p(7) = 877, p(8) = 4140, p(9) = 21147, p(10) = 115975 Section 8.6 1. a) Is a partial ordering b) Not antisymmetric, not transi­ tive c) Is a partial ordering d) Is a partial ordering e) Not antisymmetric, not transitive 3. a) No b) No �fu ��fu �� �fu �� ��� b) Yes c) No 9. No 11. Yes 13. a){(O, 0), (1, 0), (1,1), (2,0), (2,1), (2,2)} b) (Z, :::::) c) (P(Z),�) d) (Z+, "is a multiple of') 15. a) {OJ and {I}, for instance b) 4 and 6,for instance 17. a)(I, 1,2) < (1,2,1) b)(O,I, 2,3)«0,1,3,2) c)(O,I,I,I,O)<(1,O,I,O,I) 19.0< 0001 < 001 < 01 < 010 < 0101 < 011 < 11 S-5J 21. 15 11 10 5 2 o 23. a) 8 4 b) 2 3 5 7 11 13 �. 1 2 c) 48 d) 64 36 32 16 8 2 4 2 25. (a, b), (a, c), (a, d), (b, c), (b, d), (a, a), (b, b), (c, c), (d, d) 27. (a, a), (a, g), (a, d), (a, e), (a, f), (b, b), (b, g), (b, d), (b, e), (b, f), (c, c), (c, g), (c, d), (c, e), (c, f), (g, d), (g, e), (g, f), (g, g), (d, d), (e, e), (f, f) 29. (0, (a}), (0, {bn, (0, (c}), ({a), (a, b}), ({a), (a, c}), ({b), (a, b}), ({b}, {b, cn, ({c}, (a, c}), ({c}, (b, c}), ({a, b}, (a, b, c}), ({a, c), (a, b, c}) ({b, c), (a, b, c}) 31. Let (S, �) be a finite poset. We will show that this poset is the reflexive transitive closure of its covering relation. Suppose that (a, b) is in the reflexive transitive closure of the covering rela­ tion. Then a = b or a -< b, so a � b, or else there is a se­ quence at, a2, ... , an such that a -< at -< a2 -< ... -< an -< b, in which case again a � b by the transitivity of �. Con­ versely, suppose that a -< b. If a = b then (a, b) is in the reflexive transitive closure of the covering relation. If a -< b and there is no z such that a -< z -< b, then (a, b) is in the covering relation and therefore in its reflexive transitive clo­ sure. Otherwise, let a -< at -< a2 -< •.• -< an -< b be a longest possible sequence of this form (which exists because the poset Answers to Odd-Numbered Exercises S-51 is finite). Then no intermediate elements can be inserted, so each pair (a, at), (at. a2), ... , (an, b) is in the covering re­ lation, so again (a, b) is in its reflexive transitive closure. 33. a) 24, 45 b) 3, 5 c) No d) No e) 15, 45 t) 15 g) 15,5,3 b) 15 35. a) {1,2}, {1,3,4}, {2,3,4} b) {I}, {2}, {4} c) No d) No e) {2, 4}, {2, 3, 4} t) {2, 4} g) {3, 4}, {4} b) {3, 4} 37. Because (a, b) � (a, b), � is reflexive. If (at, a2) � (bt, b2) and (at, a2) =1= (bt, b2), either at -< bt. or at = bt and a2 -< b2. In either case, (bt. b2) is not less than or equal to (at, a2). Hence, � is antisym­ metric. Suppose that (at, a2) -< (bt, b2) -< (Ct, C2). Then if at -< bt or bt -< Ct, we have at -< Ct. so (at, a2) -< (Ct, C2), but if at = bt = Ct. then a2 -< b2 -< C2, which implies that (at, a2) -< (Ct, C2). Hence, � is transitive. 39. Because (s, t) � (s, t), � is reflexive. If (s, t) � (u, v) and (u, v) � (s, t), thens � u � s andt � v � t; hence,s = u andt = v. Hence, � is antisymmetri c. Suppose that (s, t) � (u, v) � (w, x). Then s � u, t � v, u � w, and v � x. It follows that s � w and t � x. Hence,(s, t) � (w, x). Hence, � is transitive. 41. a) Suppose that x is maximal and that y is the largest element. Then x � y. Because x is not less than y, it follows that x = y. By Exercise 40(a) y is unique. Hence, x is unique. b) Suppose that x is minimal and that y is the smallest element. Then x )r y. Because x is not greater than y, it follows that x = y. By Exercise 40(b) y is unique. Hence, x is unique. 43. a) Yes b) No c) Yes 45. Use mathe­ matical induction. Let P(n) be "Every subset with n elements from a lattice has a least upper bound and a greatest lower bound." Basis step: P (1) is true because the least upper bound and greatest lower bound of {x} are both x. Inductive step: Assume that P(k) is true. Let S be a set with k + I elements. Let XES and S' = S -{x}. Because S' has k elements, by the inductive hypothesis, it has a least upper bound y and a greatest lower bound a. Now because we are in a lattice, there are elements z = lub(x, y) and b = glb(x, a). We are done if we can show that z is the least upper bound of S and b is the greatest lower bound of S. To show that z is the least upper bound of S, first note that ifw E S, then w = x or w E S'. If w = x then w � z because z is the least upper bound of x and y. If w E S', then w � z because w � y, which is true be­ cause y is the least upper bound of S', and y � z, which is true because z = lub(x, y). To see that z is the least upper bound of S, suppose that u is an upper bound of S. Note that such an element u must be an upper bound of x and y, but because z = lub(x, y), it follows that z � u. We omit the similar argument that b is the greatest lower bound of S. 47. a) No b) Yes c) (Proprietary, (Cheetah, Puma}), (Restricted, (Cheetah, Puma}), (Registered, (Cheetah, Puma}), (Proprietary, (Cheetah, Puma, Impala}), (Restricted, (Cheetah, Puma, Impala}), (Register ed, (Cheetah, Puma, Impala}) d) (Non­ proprietary, (Impala, Puma}), (Proprietary, (Impala, Puma}), (Restricted, (Impala, Puma}), (Nonproprietary, (Impala}), (Proprietary, (Impala}), (Restricted, (Impala}), (Nonpropri­ etary, (Puma}), (Proprietary, (Puma}), (Restricted, (Puma}), (Nonproprietary, 0), (Proprietary, 0), (Restricted, 0) 49. Let n be the set of all partitions of a set S with Pt � P2 if Pt is a refinement of P2, that is, if every set in Pt is a S-52 Answers to Odd-Numbered Exercises subset of a set in P2. First, we show that (IT, �) is a poset. Because P � P for every partition P, � is reflexive. Now suppose that PI � P2 and P2 � PI. Let T E PI. Because PI � P2, there is a set T' E P2 such that T £ T'. Because P2 � PI there is a set T" E PI such that T' £ T". It follows that T £ T". But because PI is a partition, T = T", which implies that T = T' because T £ T' £ T". Thus, T E P2. By reversing the roles of PI and P2 it follows that every set in P2 is also in PI. Hence, PI = P2 and � is antisymmetric. Next, suppose that PI � P2 and P2 � P3. Let T E PI. Then there is a set T' E P2 such that T £ T'. Because P2 � P3 there is a set T" E P3 such that T' £ T". This means that T £ T". Hence, PI � P3. It follows that � is transitive. The greatest lower bound of the partitions PI and P2 is the partition P whose subsets are the nonempty sets of the form TI n T2 where TI E PI and T2 E P2. We omit the justification of this statement here. The least upper bound of the partitions PI and P2 is the partition that corresponds to the equivalence rela­ tion in which XES is related to YES if there is a sequence x =XO.XI.X2 •...• Xn =y for some nonnegative integer n such that for each i from 1 to n, Xi-I and Xi are in the same element of PI or of P2. We omit the details that this is an equivalence relation and the details of the proof that this is the least upper bound of the two partitions. 51. By Exercise 45 there is a least upper bound and a greatest lower bound for the entire finite lattice. By definition these elements are the great­ est and least elements, respectively. 53. The least element of a subset of Z+ x Z+ is that pair that has the smallest pos­ sible first coordinate, and, if there is more than one such pair, that pair among those that has the smallest second coordinate. 55. If x is an integer in a decreasing sequence of elements of this poset, then at most Ix I elements can follow x in the sequence, namely, integers whose absolute values are Ix I -1, Ix I '-2, ... , 1, 0. Therefore there can be no infinite decreasing sequence. This is not a totally ordered set, because 5 and -5, for example, are incomparable. 57. To find which of two rational numbers is larger, write them with a positive common denominator and compare numerators. To show that this set is dense, suppose that x < y are two rational numbers. Then their average, i.e., (x + y)/2, is a rational number between them. 59. Let (S. �) be a partially ordered set. It is enough to show that every nonempty subset of S contains a least element ifand only if there is no infinite decreasing sequence of elements al • a2. a3, ... in S (i.e., where ai+ I -< ai for all i). An infinite decreasing sequence of elements clearly has no least element. Conversely, let A be any nonempty subset of S that has no least element. Because A is nonempty, choose al E A. Because al is not the least element of A, choose a2 E A with a2 -< al. Because a2 is not the least element of A, choose a3 E A with a3 -< a2. Continue in this manner, producing an infinite decreasing sequence in S. 61. a -<I b -<I c -<I d -<, e -<, f -<, g -<I h -<I i -<, j -<, k -<, I -<, m 63. C -< A -< B -< D -< E -< F -< G 65. Determine user needs -< Write functional requirements -< Set up test sites -< Develop system requirements -< Write documentation -< Develop module A -< Develop module B -< Develop module C -< Integrate modules -< 01 test -< f3 test -< Completion S-52 Suppleme ntary Exercises 1. a) Irreflexive (we do not include the empty string), symmetric b) Irreflexive, symmetric c) Irreflexive, anti­ symmetric, transitive 3. «a, b), (a, b» E R because a + b = a + b. Hence, R is reflexive. If «a, b), (c. d» E R then a + d = b + c, so that c + b = d + a. It follows that «c, d). (a, b» E R. Hence, R is symmetric. Suppose that «a, b). (c, d» and «c, d), (e, f) belong to R. Then a + d = b + c and c + f = d + e. Adding these two equations and subtracting c + d from both sides gives a + f = b + e. Hence, «a, b). (e. f) belongs to R. Hence, R is transi­ tive. 5. Suppose that (a, b) E R. Because (b. b) E R it fol­ lows that (a, b) E R2. 7. Yes, yes 9. Yes, yes 11. Two records with identical keys in the projection would have iden­ tical keys in the original. 13. (� U R)-I = � -I U R-I = � U R-I 15. a) R = {(a, b), (a, c)}. The transitive closure of the symmetric closure of R is {(a. a), (a. b), (a. c), (b, a), (b, b), (b, c), (c, a), (c, b), (c, c)} and is different from the symmetric closure of the transitive closure of R, which is {(a. b), (a. c). (b. a). (c, a)}. b) Suppose that (a, b) is in the symmetric closure of the transitive closure of R. We must show that (a. b) is in the transitive closure of the symmetric closure of R. We know that at least one of (a. b) and (b. a) is in the transitive closure of R. Hence, there is either a path from a to b in R or a path from b to a in R (or both). In the former case, there is a path from a to b in the symmet­ ric closure of R. In the latter case, we can form a path from a to b in the symmetric closure of R by reversing the direc­ tions of all the edges in a path from b to a, going backward. Hence, (a, b) is in the transitive closure of the symmetric clo­ sure of R. 17. The closure of S with respect to property P is a relation with property P that contains R because R £ S. Hence, the closure of S with respect to property P contains the closure of R with respect to property P. 19. Use the basic idea of Wars hail's algorithm, except let w�J] equal the length of the longest path from Vi to V j using interior vertices with subscripts not exceeding k, and equal to -I if there is no such path. To find w�J] from the entries OfWk_l, deter­ mine for each pair (i, j) whether there are paths from VitO V k and from Vk to v j using no vertices labeled greater than k. If either w�Z-I] or wi�-I] is -I, then such a pair of paths does not exist, so set w �J] = w �t 1]. If such a pair of paths exists, then there are two possibilities. Ifwit-I] > 0, there are paths of arbitrary long length from Vi to Vj, so set w�J] = 00. If Ik-I] Ik-I] (Ik-I] Ik-I] Ik-I]) (I . wkk = 0, set wij = max wij , Wik + Wkj . m- tially take Wo = MR.) 21.25 23. Because Ai n Bj is a subset of Ai and of B j, the collection of subsets is a refine­ ment of each of the given partitions. We must show that it is a partition. By construction, each of these sets is nonempty. To see that their union is S, suppose that S E S. Because PI and P2 are partitions of S, there are sets Ai and B j such that S E Ai and S E B j. Therefore S E Ai n B j. Hence, the union of these sets is S. To see that they are pairwise disjoint, note that unless i = i' andj = j', (Ai n Bj) n (Ai' n Bt) = S-53 (Ai n Ai') n (Bj n Bj') = 0. 25. The subset relation is a partial ordering on any collection of sets, because it is reflex­ ive, antisymmetric, and transitive. Here the collection of sets is R(S). 27. Find recipe -< Buy seafood -< Buy groceries -< Wash shellfish -< Cut ginger and garlic -< Clean fish -< Steam rice -< Cut fish -< Wash vegetables -< Chop water chestnuts -< Make garnishes -< Cook in wok -< Arrange on platter -< Serve 29. a) The only antichain with more than one ele­ ment is {e, d}. b) The only antichains with more than one element are {b, e}, {e, e}, and {d, e}. c) The only antichains with more than one element are {a, b}, {a, e}, {b, e}, {a, b, e}, {d, e}, {d, f}, fe, f}, and {d, e, f}. 31. Let (S, �) be a fi­ nite poset, and let A be a maximal chain. Because (A, �) is also a poset it must have a minimal element m. Suppose that m is not minimal in S. Then there would be an element a of S with a -< m. However, this would make the set A U {a} a larger chain than A. To show this, we must show that a is comparable with every element of A. Because m is compara­ ble with every element of A and m is minimal, it follows that m -< x when x is inA and x =f:. m. Because a -< m andm -< x, the transitive law shows that a -< x for every element of A. 33. Let aRb denote that a is a descendant of b. By Exercise 32, if no set of n + 1 people none of whom is a descendant of any other (an antichain) exists, then k ::::: n, so the set can be parti­ tioned into k ::::: n chains. By the pigeonhole principle, at least one of these chains contains at least m + 1 people. 35. We prove by contradiction that if S has no infinite decreasing se­ quence and Vx ({Vy[y -< x -+ P(y)]} -+ P(x»), then P(x) is true for all XES. If it does not hold that P(x) is true for all XES, let XI be an element of S such that P(XI) is not true. Then by the conditional statement already given, it must be the case that Vy[y -< XI -+ P(y)] is not true. This means that there is some X2 with X2 -< XI such that P(X2) is not true. Again invoking the conditional statement, we get an X3 -< X2 such that P(X3) is not true, and so on forever. This contra­ dicts the well-foundedness of our poset. Therefore, P(x) is true for all XES. 37. Suppose that R is a quasi-ordering. Because R is reflexive, if a E A, then (a, a) E R. This im­ plies that (a, a) E R-I. Hence, a ERn R-I. It follows that R n R-I is reflexive. R n R-I is symmetric for any relation R because, for any relation R, if(a, b) E R then (b, a) E R-I and vice versa. To show that R n R - I is transitive, suppose that (a, b) ERn R-I and (b, c) ERn R-I. Because (a, b) E R and (b, c) E R, (a, c) E R, because R is transitive. Simi­ larly, because (a, b) E R-I and (b, c) E R-I, (b, a) E R and (e, b) E R, so (e, a) E R and (a, c) E R-I. Hence, (a, c) E R n R-I. It follows that R n R-I is an equivalence rela­ tion. 39. a) Because glb(x, y) = glb(y, x) and lub(x, y) = lub(y, x), it follows that x 1\ y = Y 1\ x and x v y = y v x. b) Using the definition, (x 1\ y) 1\ z is a lower bound of x, y, and z that is greater than every other lower bound. Because x, y, and z play interchangeable roles, x 1\ (y 1\ z) is the same element. Similarly, (x v y) v z is an upper bound of x, y, and z that is less than every other upper bound. Because x, y, and z play interchangeable roles, x v (y v z) is the same element. c) To show that x 1\ (x V y) = x it is sufficientto show that x is the greatest lower bound of x, and x v y. Note that x is a lower bound of x, and because x v y is by definition greater Answers to Odd-Numbered Exercises 8-53 than x, x is a lower bound for it as well. Therefore, x is a lower bound. But any lower bound of x has to be less than x, so x is the greatest lower bound. The second statement is the dual of the first; we omit its proof. d) x is a lower, and an upper, bound for itself and itself, and the greatest, and least, such bound. 41. a) Because 1 is the only element greater than or equal to 1, it is the only upper bound for 1 and there­ fore the only possible value of the least upper bound of x and 1. b) Because x � 1, x is a lower bound for both x and 1 and no other lower bound can be greater than x, so x 1\ 1 = x. c) Because 0 � x, x is an upper bound for both x and 0 and no other bound can be less than x, so x V 0 = x. d) Because 0 is the only element less than orequal to 0, it is the only lower bound for 0 and therefore the only possible value of the greatest lower bound of x and O. 43. L = (S, �) where S = {0, {I}, {2}, {3}, {I, 2}, {2, 3}, {I, 2, 3}} 45. Yes 47. The comple­ ment of a subset X � S is its complement S -X. To prove this, note that X v (S -X) = 1 and X 1\ (S -X) = 0 be­ cause X U (S -X) = S and X n (S -X) = 0. 49. Think of the rectangular grid as representing elements in a matrix. Thus we number from top to bottom and within that from left to right. The partial order is that (a, b) � (e, d) iff a ::::: e and b ::::: d. Note that (1, 1) is the least element under this relation. The rules for Chomp as explained in Chapter 1 coincide with the rules stated in the preamble here. But now we can identify the point (a, b) with the natural number pa-Iqb-I for all a and b with 1 ::::: a ::::: m and 1 ::::: b ::::: n. This identifies the points in the rectangular grid with the set S in this exercise, and the par­ tial order � just described is the same as the divides relation, because pa-Iqb-I I pc-Iqd-I if and only if the exponent on p on the left does not exceed the exponent of p on the right, and similarly for q. CHAPTER 9 Section 9.1 1. a) Boston Detroit Washington Miami b) Boston Detroit Washington Miami S-54 Answers to Odd-Numbered Exercises c) Boston Detroit Washington d) Detroit Boston Washington e) Detroit Washington 3. Simple graph 5. Pseudograph 7. Directed graph 9. Directed multigraph 11. If uRv, then there is an edge associated with {u, v}. But {u, v} = {v, u}, so this edge is as­ sociated with {v, u} and therefore vRu. Thus, by definition, R is a symmetric relation. A simple graph does not allow loops; therefore, uRu never holds, and so by definition R is irreflexive. 15. Hairy -z�T Mockingbird Blue jay Nuthatch 17. 19. 21. Ari�otle \. Euchd o al-Khowarizmi o Fibonacci Maui'olico President Director, Director, RandO marketing • Director, Chief financial operations officer Tigers Blue jays Orioles Cardinals Gol� Lame Vanderrnonde Boole .8-54 23. We find the telephone numbers in the call graph for Febru­ ary that are not present in the call graph for January and vice versa. For each number we find, we make a list of the num­ bers they called or were called by using the edges in the call graph. We examine these lists to find new telephone numbers in February that had similar calling patterns to defunct tele­ phone numbers in January. 25. We use the graph model that has e-mail addressesasverticesandforeachmessagesent.an edge from the e-mail address of the sender to the e-mail ad­ dress of the recipient. For each e-mail address, we can make a list of other addresses they sent messages to and a list of other address from which they received messages. If two e-mail ad­ dresses had almost the same pattern, we conclude that these addresses might have belonged to the same person who had recently changed his or her e-mail address. 27. Let V be the set of people at the party. Let E be the set of ordered pairs S-55 (u, v) in V x V such that u knows v's name. The edges are directed, but multiple edges are not allowed. Literally, there is a loop at each vertex, but for simplicity, the model could omit the loops. 29. Let the set of vertices be a set of people, and two vertices are joined by an edge if the two people were ever married. Ignoring complications, this graph has the property that there are two types of vertices (men and women), and every edge joins vertices of opposite types. 31. • SI S3 S7 33. Represent people in the group by vertices. Put a directed edge into the graph for every pair of vertices. Label the edge from the vertex representing A to the vertex representing B with a + (Plus) if A likes B, a -(minus) if A dislikes B, and a 0 if A is neutral about B. Section 9.2 1. v = 6; e = 6; deg(a) = 2, deg(b) = 4, deg(c) = 1, deg(d) = 0, deg(e) = 2, deg(f) = 3; c is pendant; d is iso­ lated. 3. v = 9; e = 12; deg(a) = 3, deg(b) = 2, deg(c) = 4,deg(d) = O,deg(e) = 6,deg(f) = O;deg(g) = 4;deg(h) = 2; deg(i) = 3; d andfare isolated. 5. No, because the sum of the degrees of the vertices cannot be odd 7. v = 4; e = 7; deg-(a) = 3, deg-(b) = 1, deg-(c) = 2, deg-(d) = 1, deg+(a) = 1, deg+(b) = 2, deg+(c) = 1, deg+(d) = 3 9.5 vertices, 13 edges; deg-(a) = 6, deg+(a) = 1, deg-(b) = 1, deg+(b) = 5, deg-(c) = 2, deg+(c) = 5, deg-(d) = 4, deg+(d) = 2, deg-(e) = 0, deg+(e) = 0 11. c • f 13. The number of collaborators v has; someone who has never collaborated; someone who has just one collaborator 15. In the directed graph deg-(v) = number of calls v re­ ceived, deg + (v) = number of calls v made; in the undirected graph, deg( v) is the number of calls either made or received by v. 17. (deg+(v),deg-(v» is the win-loss record ofv. 19. Construct the simple graph model in which V is the set of people in the group and there is an edge associated with {u, v} if u and v know each other. Then the degree of vertex v is the number of people v knows. By the result of Exercise 18, there are two vertices. with the same degree. Therefore there Answers to Odd-Numbered Exercises S-55 are two people who know the same number of other people in the group. 21. Bipartite 23. Not bipartite 25. Not bipartite 27. a) Let V = {Zamora, Agraharam, Smith, Chou, Macintyre, planning, publicity, sales, marketing, devel­ opment, industry relations} and E = {{Zamora, planning}, {Zamora, sales}, {Zamora, marketing}, {Zamora,industry relations}, {Agraharam, planning}, {Agraharam, develop­ ment}, {Smith, publicity}, {Smith, sales}, {Smith, industry relations}, {Chou, planning}, {Chou, sales}, {Chou, indus­ try relations}, {Macintyre, planning}, {Macintyre, public­ ity}, {Macintyre, sales}, {Macintyre, industry relations}}. b) Many answers are possible, such as Zamora-planning, Agraharam-de velopment, Smith-publicity, Chou-sales, and Macintyre-industry relations. 29. a) n vertices, n(n - 1 )/2 edges b) n vertices, n edges c) n + 1 vertices, 2n edges d) m + n vertices, mn edges e) 2n vertices, n2n-1 edges 31. a) 3, 3, 3, 3 b) 2, 2, 2, 2 c) 4,3,3,3,3 d) 3,3,2,2,2 e) 3, 3, 3, 3, 3, 3, 3, 3 33. Each of the n vertices is adjacent to each of the other n -1 vertices, so the degree sequence is n -1, n -1, ... , n -1 (n terms). 35.7 37. aJ Yes I::fo b) No, sum of degrees is odd. c) No d) No, sum of de­ grees is odd. e) Yes t) No, sum of degrees is odd. 39. First, suppose that dl, d2, ... , dn is graphic. We must show that the se­ quence whose terms are d2 -1, d3 -1, ... , ddl+1 -1, ddl+2, ddt+3, .•. , dn is graphic once it is put into nonincreasing or­ der. In Exercise 38 it is proved that if the original sequence is graphic, then in fact there is a graph having this degree sequence in which the vertex of degree dl is adjacent to the vertices of degrees d2, d3, ... , ddl+l. Remove from this graph the vertex of highest degree (dl). The resulting graph has the desired degree sequence. Conversely, suppose that dl, d2, ... ,dn is a nonincreasing sequence such that the se­ quence d2 -1, d3 -1, ... , ddt+! -1, ddt+2, ddt +3, ... , dn is graphic once it is put into nonincreasing order. Take a graph with this latter degree sequence, where vertex Vi has degree di - 1 for 2 ::::: i ::::: dl + 1 and vertex Vi has degree di for dl + 2 ::::: i ::::: n. Adjoin one new vertex (call it VI), and put in an edge from VI to each of the vertices V2, V3, ... , Vdt+l. The resulting graph has degree sequence dt. d2, ... , dn. 41. Let dt. d2, ••• ,dn be a nonincreasing sequence of nonnegative in­ tegers with an even sum. Construct a graph as follows: Take vertices Vt. V2, ... , Vn and put Ldi/2J loops at vertex Vi, for i = 1,2, ... , n. For each i, vertex Vi now has degree either di or di -1. Because the original sum was even, the number S-56 Answers to Odd-Numbered Exercises of vertices for which deg(v;) = d; - 1 is even. Pair them up arbitrarily, and put in an edge joining the vertices in each pair. 43. 17 45. K r: � ;( c Ii c Ii a-----.b ar .b � a. .b c· .d ·d c· Ii c· ·d c "1 ar .b a-----.b a. .b c· c· c c � a-----.b "�: a. eh ·d ·d �d ar :� a. ·d c· ·d c .b a-----.b a. .b ar c· ·d c a. "�, a. .b c· ·d c· .b a. .b ·d c· ·d c· ·d 47. a) For all n :::: 1 b) For all n :::: 3 c) For n = 3 d) For all n :::: 0 49.5 Sl'� c g d 53. a) The graph with n vertices and no edges b) The dis­ joint union of Km and Kn c) The graph with vertices {VI, •.• , vn} with an edge between V; and Vj unless i == j ± 1 (mod n) d) The graph whose vertices are represented by bit strings of length n with an edge between two vertices if the associated bit strings differ in more than one bit 55. v(v -1)/2 -e 57. n - 1 -dn, n - 1 -dn-h ... , n - 1 -d2, n - 1 -dl 59. The union of G and G contains an edge between each pair of the n vertices. Hence, this union is Kn. S-56 61. Exercise 7: a Exercise 8: Exercise 9: 63. A directed graph G = (V, E) is its own converse if and only if it satisfies the condition (u, v) E E if and only if (v, u) E E. But this is precisely the condition that the associ­ ated relation must satisfy to be symmetric. 65. P(O,O) P(O, I) P(O, 2) po, 0) P(I, I) P(I,2) P(2, 0) P(2, I) P(2, 2) 67. We can connect P(i, j) and P(k, l) by using Ii -kl hops to connect P(i, j) and P(k, j) and U -/1 hops to con­ nect P(k, j) and P(k, I). Hence, the total number of hops required to connect P(i, j) and P(k, /) does not exceed Ii -kl + Ij -/1. This is less than or equal to m + m = 2m, which is O(m). Section 9.3 1. Adjacent 3. Terminal J-ertex J-ertices J-ertex Vertices a b,c,d a a,b,c,d b a,d b d c a,d c a,b d a,b,c d b,c,d where vertices are listed in alphabetical order I I] I I o 1 1 0 S-57 bT 1 1 1 I] 'T 0 1 1 �] 0 0 o 0 0 1 1 0 0 o 0 1 0 0 0 0 o 0 1 0 0 0 0 o 0 1 0 0 d) [i 1 0 t] e) [f 1 0 1 1] 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 t) 0 1 1 0 1 0 0 0 1 0 0 1 0 1 0 0 1 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 1 0 0 1 0 0 1 0 0 1 0 1 0 0 1 0 0 0 1 0 1 1 0 11. d b 13. [! " 1 "] � 012 101 210 15. [� �] .7·n 0 2 1 1 1 1 2 0 d c ... [� 1 0 !] 21. [1 1 2 !] 1 1 0 0 1 1 0 1 0 0 2 1 23. 25. Yes b 27. ��13, [t 0 0 0 f] 1 1 1 1 0 0 0 1 1 E=m I4, [� 1 1 1 0 0 0 f] 1 1 0 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 Answers to Odd-Numbered Exercises S-57 Exercise 15: [1 1 1 1 0 0 0 0 o 0 0 0 1 111 o 1 100 100 000 1 001 1 29. deg(v) -numberofloops atv; deg-(v) a loop, I if e is a loop 33. a) 1 1 1 0 o 1 0 0 0 0 b) 1 0 1 1 0 I 0 0 0 0 c) 0 0 B 1 0 o 1 o 1 0 0 1 0 0 1 0 0 0 0 0 1 0 1 1 0 0 where B is the answer to (b) d) I 1 1 0 0 0 0 1 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 o o o 1 0 0 � �] I 0 o 1 31. 2 if e is not 35. Isomorphic 37. Isomorphic 39. Isomorphic 41. Not isomorphic 43. Isomorphic 45. G is isomorphic to itself by the identity function, so isomorphism is reflex­ ive. Suppose that G is isomorphic to H. Then there exists a one-to-one correspondence I from G to H that preserves adjacency and nonadjacency. It follows that 1-) is a one-to­ one correspondence from H to G that preserves adjacency and nonadjacency. Hence, isomorphism is symmetric. If G is isomorphic to H and H is isomorphic to K, then there are one-to-one correspondences I and g from G to H and from H to K that preserve adjacency and nonadjacency. It follows that g 0 I is a one-to-one correspondence from G to K that preserves adjacency and nonadjacency. Hence, isomorphism is transitive. 47. All zeros 49. Label the vertices in order so that all of the vertices in the first set of the partition of the vertex set come first. Because no edges join vertices in the same set of the partition, the matrix has the de­ sired form. 51. Cs 53. n = 5 only 55.4 57. a) Yes b) No c) No 59. G = (V). E)) is isomorphic to H = S-S8 Answers to Odd-Numbered Exercises (V2, E2) if and only ifthere exist functions f from VI to V2 and g from E I to E2 such that each is a one-to-one correspon­ dence and for every edge e in E I the endpoints of g( e) are f( v) and f(w) where v and w are the endpoints of e. 61. Yes 63. Yes 65. If f is an isomorphism from a directed graph G to a directed graph H, then f is also an isomorphism from Gconv to Hconv. To see this note that (u, v) is an edge ofGconv if and only if(v, u) is an edge ofGifand only if(f(v), f(u» is an edge of H if and only if(f(u), f(v» is an edge of Hconv. 67. Many answers are possible; for example, C6 and C3 U C3• 69. The product is [aij] where aij is the number of edges from Vi to Vj when i ¥= j and aii is the number of edges incident to Vi. 71. The graphs in Exercise 41 provide a devil's pair. Section 9.4 1. a) Path oflength 4; not a circuit; not simple b) Not a path c) Not a path d) Simple circuit of length 5 3. No 5. No 7. Maximal sets of people with the property that for any two of them, we can find a string of acquaintances that takes us from one to the other 9. If a person has Erdos number n, then there is a path oflength n from that person to Erdos in the collaboration graph, so by definition, that means that that person is in the same component as Erdos. If a per­ son is in the same component as Erdos, then there is a path from that person to Erdos, and the length of the shortest such path is that person's Erdos number. 11. a) Weakly con­ nected b) Weakly connected c) Not strongly or weakly connected 13. The maximal sets of phone numbers for which it is possible to find directed paths between every two different numbers in the set 15. a) {a, b, f}, {c, d, e} b) {a, b, c, d, e, hI, {f}, {g} c) {a, b, d, e, f, g, h, i}, {c} 17. a) 2 b)7 c) 20 d) 61 19. Not isomorphic (G has a triangle; H does not) 21. Isomorphic (the path UI, U2, U7, U6, Us, U4, U3, Us, UI corresponds to the path VI, V2, V3, V4, Vs, vs, v7, v6, VI) 23. a) 3 b) 0 c) 27 d) 0 25. a) 1 b) 0 c) 2 d) 1 e) 5 f) 3 27. R is reflexive by definition. Assume that (u, v) E R; then there is a path from u to v. Then (v, u) E R because there is a path from v to u, namely, the path from u to v traversed backward. Assume that (u, v) E R and (v, w) E R; then there are paths from u to v and from v to w. Putting these two paths together gives a path from u to w. Hence, (u, w) E R. It follows that R is transitive. 29. c 31. b, c, e, i 33. If a vertex is pen­ dant it is clearly not a cut vertex. So an endpoint of a cut edge that is a cut vertex is not pendant. Removal of a cut edge pro­ duces a graph with more connected components than in the original graph. If an endpoint of a cut edge is not pendant, the connected component it is in after the removal of the cut edge contains more than just this vertex. Consequently, removal of that vertex and all edges incident to it, including the original cut edge, produces a graph with more connected components than were in the original graph. Hence, an endpoint of a cut edge that is not pendant is a cut vertex. 35. Assume there exists a connected graph G with at most one vertex that is not a cut vertex. Define the distance between the vertices u and v, denoted by d(u, v), to be the length of the shortest path S-58 between u and v in G. Let s and t be vertices in G such that d(s, t) is a maximum. Either s or t (or both) is a cut vertex, so without loss of generality suppose that s is a cut vertex. Let w belong to the connected component that does not contain t of the graph obtained by deleting s and all edges incident to s from G. Because every path from w to t contains s, d(w, t) > d(s, t), which is a contradiction. 37. a) Denver­ Chicago, Boston-New York b) Seattle-Portland, Portland­ San Francisco, Salt Lake City-Denver, New York-Boston, Boston-Burlington, Boston-Bangor 39. A set of people who collectively influence everyone (directly or indirectly); {Deborah} 41. An edge cannot connect two vertices in different connected components. Because there are at most C(ni, 2) edges in the connected component with ni vertices, it follows that there are at most L�=I C(ni, 2) edges in the graph. 43. Suppose that G is not connected. Then it has a component of k vertices for some k, 1 ::::: k ::::: n -1. The most edges G could have is C(k, 2) + C(n -k, 2) = [k(k- 1) + (n -k)(n -k -1)]/2 = e -nk + (n2 -n)/2. This quadratic function of f is minimized at k = n/2 and maxi­ mized at k = 1 or k = n -1. Hence, if G is not connected, the number of edges does not exceed the value of this func­ tion at 1 and at n -I, namely, (n -l)(n -2)/2. 45. a) I b)2 c)6 d)21 47.2 49.LetthepathsPlandP2be u = XO,XI, .. ',Xn = v, respectively. Because PI and P2 do not contain the same set of edges, they must eventually di­ verge. If this happens only after one of them has ended, the rest of the other path is a simple circuit from v to v. Other­ wise, we can suppose that Xo = Yo, XI = YI, ... , Xi = Yi, but XHI ¥= YHI· Follow the path Yi, YHI, YH2, and so on, until it once again encounters a vertex on PI. Once we are back on PI, follow it, forward or backward as necessary, back to Xi. Because Xi = Yi, this forms a circuit that must be simple, because no edge among the XkS can be repeated and no edge among the XkS can equal one of the Y/s that we used. 51. The graph G is connected if and only if every off -diagonal entry of A + A2 + A3 + ... + An-I is positive, where A is the ad­ jacency matrix of G. 53. If the graph is bipartite, say with parts A and B, then the vertices in every path must alternately lie in A and B. Therefore a path that starts in A, say, will end in B after an odd number of steps and in A after an even number of steps. Because a circuit ends at the same vertex where it starts, the length must be even. Conversely, suppose that all circuits have even length; we must show that the graph is bipartite. We can assume that the graph is connected, be­ cause if it is not, then we can just work on one component at a time. Let v be a vertex of the graph, and let A be the set of all vertices to which there is a path of odd length starting at v, and let B be the set of all vertices to which there is a path of even length starting at v. Because the component is connected, every vertex lies in A or B. No vertex can lie in both A and B, because if one did, then following the odd-length path from v to that vertex and then back along the even-length path from that vertex to v would produce an odd circuit, contrary to the hypothesis. Thus, the set of vertices has been partitioned into two sets. To show that every edge has endpoints in dif­ ferent parts, suppose that xy is an edge, where X EA. Then the odd-length path from v to X followed by xy produces an S-59 even-length path from v to y, so Y E B. (Similarly, if x E B.) 55. (HI WI H2W2 (boat), 0) -+ (H2W2, HI W.(boat) -+ (HIH2 W2 (boat), WI) -+(W2,HI WI H2 (boat) -+(H2 W2 (boat), HI WI) -+ (0, HI WI H2W2 (boat) Section 9.5 1. Neither 3. No Euler circuit; a, e, c, e, b, e, d, b, a, c, d ������������ a ��tt&�� t&��t� c, a, b, i, c, b, h, a 9. No, A still has odd degree. 11. When the graph in which vertices represent intersections and edges streets has an Euler path 13. Yes 15. No 17. If there is an Euler path, as we follow it each vertex except the starting and ending vertices must have equal in-degree and out-degree, because whenever we come to a vertex along an edge, we leave it along another edge. The starting vertex must have out-degree 1 larger than its in-degree, because we use one edge leading out of this vertex and whenever we visit it again we use one edge leading into it and one leaving it. Simila rly, the ending vertex must have in-degree 1 greater than its out-degree. Because the Euler path with directions erased produces a path between any two vertices, in the underlying undirected graph, the graph is weakly connected. Conversely, suppose the graph meets the degree conditions stated. If we add one more edge from the vertex of deficient out-degree to the vertex of deficient in­ degree, then the graph has every vertex with equal in-degree and out-degree. Because the graph is still weakly connected, by Exercise 16 this new graph has an Euler circuit. Now delete the added edge to obtain the Euler path. 19. Neither 21. No Euler circuit; a, d, e, d, b, a, e, c, e, b, c, b, e 23. Neither 25. Follow the same procedure as Algorithm 1, taking care to follow the directions of edges. 27. a) n = 2 b) None c) None d)n = 1 29.Exercise 1:1 time; Exercises 2-7: o times 31. a, b, c, d, e, a is a Hamilton circuit. 33. No Hamilton circuit exists, because once a purported circuit has reached e it would have nowhere to go. 35. No Hamilton circuit exists, because every edge in the graph is incident to a vertex of degree 2 and therefore must be in the circuit. 37. a, b, c,t d, e is a Hamilton path. 39.t e, d, a, b, c is a Hamilton path. 41. No Hamilton path exists. There are eight vertices of degree 2, and only two of them can be end vertices of a path. For each of the other six, their two incident edges must be in the path. It is not hard to see that if there is to be a Hamilton path, exactly one of the inside comer vertices must be an end, and that this is impossible. 43. a, b, c, t i, h, g, d, e is a Hamilton path. 45. m = n � 2 47. a) (i) No, (ii) No, (iii) Yes b) (i) No, (ii) No, (iii) Yes c) (i) Yes, (ii) Yes, (iii) Yes d) (i) Yes, (ii) Yes, (iii) Yes 49. The re­ sult is trivial for n = 1: code is 0, 1. Assume we have a Gray code of order n. Let CI, ••• , Ck, k = 2n be such a code. Then OCI, ••• , OCk, let. ... , lcl is a Gray code of order n + 1. 51. procedure Fleury (G = (V, E): connected multigraph with the degrees of all vertices even, V = (VI, ... , vn}) V:=VI circuit:= v H:=G Answers to Odd-Numbered Exercises S-59 while H has edges begin e := first edge with endpoint v in H (with respect to listing of V) such that e is not a cut edge of H, if one exists, and simply the first edge in H with endpoint v otherwise w := other endpoint of e circuit := circuit with e, wadded V:=W H :=H -e end {circuit is an Euler circuit} 53. If G has an Euler circuit, it is also iln Euler path. If not, add an edge between the two vertices of odd degree and apply the algorithm to get an Euler circuit. Then delete the new edge. 55. Suppose G = (V, E) is a bipartite graph with V = VI U V2, where no edge connects a vertex in VI and a vertex in V2. Suppose that G has a Hamilton circuit. Such a circuit must be of the formal , b" a2, b2, ... , ak, bk, a" where aj E VI andb; E V2 fori = 1,2, ... , k. Because the Hamilton circuit visits each vertex exactly once, except for v I, where it begins and ends, the number of vertices in the graph equals 2k, an even number. Hence, a bipartite graph with an odd number of vertices cannot have a Hamilton circuit. 59. We represent the squares of a 3 x 4 chessboa rd as follows: 1 2 3 4 5 6 7 8 9 10 11 12 A knight's tour can be made by following the moves 8, 10, 1, 7,9,2, 11, 5,3,12,6,4. 61. We represent the squares ofa 4 x 4 chessboa rd as follows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 There are only two moves from each of the four comer squares. Ifwe include all the edges 1-10, 1-7, 16-10, and 16-7, a cir­ cuit is completed too soon, so at least one of these edges must 8-60 AnSwers to Odd-Numbered Exercises be missing. Without loss of generality, assume the path starts 1-10, 10--16, 16-7. Now the only moves from square 3 are to squares 5, 10, and 12, and square 10 already has two inci­ dent edges. Therefore, 3-5 and 3-12 must be in the Hamilton circuit. Similarly, edges 8-2 and 8-15 must be in the circuit. Now the only moves from square 9 are to squares 2, 7, and 15. If there were edges from square 9 to both squares 2 and 15, a circuit would be completed too soon. Therefore the edge 9-7 must be in the circuit giving square 7 its full complement of edges. But now square 14 is forced to be joined to squares 5 and 12, completing a circuit too soon (5-14-12-3-5). This contradiction shows that there is no knight's tour on the 4 x 4 board. 63. Because there are mn squares on an m x n board, ifboth m and n are odd, there are an odd number of squares. Because by Exercise 62 the corresponding graph is bipartite, by Exercise 55 it has no Hamilton circuit. Hence, there is no reentrant knight's tour. 65. a) If G does not have a Hamil­ ton circuit, continue as long as possible adding missing edges one at a time in such a way that we do not obtain a graph with a Hamilton circuit. This cannot go on forever, because once we've formed the complete graph by adding all missing edges, there is a Hamilton circuit. Whenever the process stops, we have obtained a (necessarily noncomplete) graph H with the desired property. b) Add one more edge to H. This produces a Hamilton circuit, which uses the added edge. The path con­ sisting of this circuit with the added edge omitted is a Hamil­ ton path in H. c) Clearly VI and Vn are not adjacent in H, because H has no Hamilton circuit. Therefore they are not ad­ jacent in G. But the hypothesis was that the sum of the degrees of vertices not adjacent in G was at least n. This inequality can be rewritten as n -deg(vn) � deg(vI)' But n -deg(vn) is just the number of vertices not adjacent to Vn• d) Because there is no vertex following Vn in the Hamilton path, Vn is not in S. Each one of the deg(vI) vertices adjacent to VI gives rise to an element of S, so S contains deg(vI) vertices. e) By part (c) there are at most deg(vI) -1 vertices other than Vn not adjacent to Vn, and by part (d) there are deg(vI) vertices in S, none of which is Vn• Therefore at least one vertex of Sis adjacent to Vn• By definition, ifvk is this vertex, then H con­ tains edges VkVn and VIVk+lo where 1 < k < n -1. 1) Now VI, V2, ••• , Vk-I, Vk, Vn, Vn-h ... , Vk+h VI is a Hamilton cir­ cuit in H, contradicting the construction of H. Therefore, our assumption that G did not originally have a Hamilton circuit is wrong, and our proof by contradiction is complete. Section 9.6 1. a) Vertices are the stops, edges join adjacent stops, weights are the times required to travel between adjacent stops. b) Same as part ( a), except weights are distances between adja­ cent stops. c) Same as part (a), except weights are fares be­ tween stops. 3. 16 5. Exercise 2: a, b, e, d, z; Exercise 3: a, c, d, e, g, z; Exercise 4: a, b, e, h, /, m, p, S, z 7. a) a, c, d b) a, c, d,f c) c, d,f e) b, d, e, g, z 9. a) Direct b) Via New York c) Via Atlanta and Chicago d) Via New York 11. a) Via Chicago b) Via Chicago c) Via Los Angeles d) Via Chicago 13. a) Via Chicago b) Via Chicago S-60 c) Via Los Angeles d) Via Chicago 15. Do not stop the algorithm when z is added to the set S. 17. a) Via Wood- bridge, via Woodbridge and Camden b) Via Woodbridge, via Woodbridge and Camden 19. For instance, sightseeing tours, street cleaning 21. a b c d e z a 4 3 2 8 10 13 b 3 2 1 5 7 10 c 2 1 2 6 8 11 d 8 5 6 4 2 5 e 10 7 8 2 4 3 z 13 10 11 5 3 6 23.0(n3) 25. a-c-b-d-a (or the same circuit starting at some point but traversing the vertices in the same or reverse order) 27. San Francisco-Den ver-Detroit-New York-Los Angeles-San Francisco (or the same circuit starting at some other point but traversing the vertices in the same or reverse order) 29. Consider this graph: a L�2 b� C The circuit a-b-a-c-a visits each vertex at least once (and the vertex a twice) and has total weight 6. Every Hamilton circuit has total weight 103. Section 9.7 1. Yes 7. Yes 3. b a<J> d C a d C.,....-----,.. b 5. No 9. No 11. A triangle is formed by the planar representation of the subgraph of Ks consisting of the edges connecting VI, V2, and V3. The vertex V4 must be placed either within the triangle or outside of it. We will consider only the case when V4 is inside the triangle; the other case is similar. Draw­ ing the three edges from VI, V2, and V3 to V4 forms four re­ gions. No matter which of these four regions Vs is in, it is possible to join it to only three, and not all four, of the other S-M vertices. 13. 8 15. Because there are no loops or multi­ ple edges and no simple circuits of length 3, and the degree of the unbounded region is at least 4, each region has de­ gree at least 4. Thus 2e ::: 4r, or r ::: e /2. But r = e -v + 2, so we have e -v + 2 ::: e/2, which implies that e ::: 2v -4. 17. As in the argument in the proof of Corollary 1, we have 2e ::: 5r and r = e -v + 2. Thus e -v + 2 ::: 2e/5, which implies that e ::: (5/3)v -(10/3). 19. Only (a) and (c) 21. Nothomeomorphic toK3,3 23. Planar 25. Nonplanar 27. a) 1 b) 3 c) 9 d) 2 e) 4 t) 16 29. Draw Km,n as described in the hint. The number of crossings is four times the number in the first quadrant. The vertices on the x-axis to the right of the origin are (1,0), (2, 0), ... , (m /2,0) and the vertices on the y-axis above the origin are (0, 1), (0, 2), ... , (0, n/2). We obtain all crossings by choosing any two numbers a and b with 1 ::: a < b ::: m /2 and two numbers r and s with 1 ::: r < s ::: n /2; we get exactly one crossing in the graph between the edge connecting (a, 0) and (0, s) and the edge connecting (b, 0) and (0, r). Hence, the number of cross­ ings in the first quadrant is C (1-,2) . C (I' 2) = (m/2)(;/2-1) . (n/2)(�/2-1). Hence, the total number of crossings is 4· mn (m -2)(n -2)/64 = mn(m -2)(n -2)/16. 31. a) 2 b) 2 c) 2 d) 2 e) 2 t) 2 33. The formula is valid for n ::: 4. If n > 4, by Exercise 32 the thickness of Kn is at least C(n, 2)/(3n -6) = (n + 1 + n=-2)/6 rounded up. Because this quantity is never an integer, it equals the next larger integer rounded down, which equals L(n + 7)/6J. 35. This follows from Exercise 34 because Km,n has mn edges and m + n ver­ tices and has no triangles because it is bipartite. 37. Section 9.8 1. Four colors 3. Three colors A�E F�E D 5.3 7.3 9.2 11. 3 13. Graphs with no edges 15. 3 ifn is even, 4 ifn is odd 17. Period 1: Math 115, Math 185; period 2: Math 116, CS473; period 3: Math 195, CS 101; period 4: CS 102; period 5: CS273 19. 5 21. Exercise 5: 3 Exercise 6: 6 Exercise 7: 3 Exercise 8: 4 Exercise 9: 3 Exercise 10: 6 Exercise 11: 4 23.5 25. Color 1: e,/, d; color 2: C, a, i, g; color 3: h, b, j 27. Color C6 29. Four colors are needed to color Wn when n is an odd integer greater Answers to Odd-Numbered Exercises 8-61 than 1, because three colors are needed for the rim (see Ex­ ample 4), and the center vertex, being adjacent to all the rim vertices, will require a fourth color. To see that the graph ob­ tained from Wn by deleting one edge can be colored with three colors, consider two cases. If we remove a rim edge, then we can color the rim with two colors, by starting at an endpoint of the removed edge and using the colors alternately around the portion of the rim that remains. The third color is then assigned to the center vertex. Ifwe remove a spoke edge, then we can color the rim by assigning color # I to the rim endpoint of the removed edge and colors #2 and #3 alternately to the remaining vertices on the rim, and then assign color #1 to the center. 31. Suppose that G is chromatically k-critical but has a vertex v of degree k -2 or less. Remove from Gone of the edges incident to v. By definition of "k-critical," the resulting graph can be colored with k -1 colors. Now restore the missing edge and use this coloring for all vertices except v. Because we had a proper coloring ofthe smaller graph, no two adjacent vertices have the same color. Furthermore, v has at most k -2 neighbors, so we can color v with an unused color to obtain a proper (k - 1 )-coloring of G. This contradicts the fact that G has chromatic number k. Therefore, our assump­ tion was wrong, and every vertex of G must have degree at least k -1. 33. a) 6 b) 7 c) 9 d) 11 35. Represent frequenc ies by colors and zones by vertices. Join two vertices with an edge if the zones these vertices represent interfere with one another. Then a k-tuple coloring is precisely an assign­ ment of frequencies that avoids interference. 37. We use induction on the number of vertices of the graph. Every graph with five or fewer vertices can be colored with five or fewer colors, because each vertex can get a different color. That takes care of the basis case(s). So we assume that all graphs with k vertices can be 5-colored and consider a graph G with k + 1 vertices. By Corollary 2 in Section 9.7, G has a vertex v with degree at most 5. Remove v to form the graph G'. Because G' has only k vertices, we 5-color it by the inductive hypothesis. If the neighbors of v do not use all five colors, then we can 5-color G by assigning to v a color not used by any of its neighbors. The difficulty arises if v has five neighbors, and each has a different color in the 5-coloring of G'. Suppose that the neighbors of v, when considered in clockwise order around v, are a, b, c, m, and p. (This order is determined by the clockwise order of the curves representing the edges inci­ dent to v .) Suppose that the colors of the neighbors are azure, blue, chartreuse, magenta, and purple, respectively. Consider the azure-chartreuse subgraph (i.e., the vertices in G colored azure or chartreuse and all the edges between them). If a and C are not in the same component of this graph, then in the component containing a we can interchange these two colors (make the azure vertices chartreuse and vice versa), and G' will still be properly colored. That makes a chartreuse, so we can now color v azure, and G has been properly colored. If a and c are in the same component, then there is a path of ver­ tices alternately colored azure and chartreuse joining a and c. This path together with edges av and vc divides the plane into two regions, with b in one of them and m in the other. If we now interchange blue and magenta on all the vertices in the same region as b, we will still have a proper coloring of G'; but S-62 Answers to Odd-Numbered Exercises now blue is available for v. In this case, too, we have found a proper coloring of G. This completes the inductive step, and the theorem is proved. 39. We follow the hint. Because the measures of the interior angles of a pentagon total 5400, there cannot be as many as three interior angles of measure more than 1800 (reflex angles). If there are no reflex angles, then the pentagon is convex, and a guard placed at any vertex can see all points. If there is one reflex angle, then the pen­ tagon must look essentially like figure (a) below, and a guard at vertex v can see all points. If there are two reflex angles, then they can be adjacent or nonadjacent (figures (b) and (c»; in either case, a guard at vertex v can see all points. [In figure (c), choose the reflex vertex closer to the bottom side.] Thus for all pentagons, one guard suffices, so g(5) = l. tdV (a) (b) A (c) 41. The figure suggested in the hint (generalized to have k prongs for any k ::: 1) has 3k vertices. The sets of locations from which the tips of different prongs are visible are dis­ joint. Therefore, a separate guard is needed for each of the k prongs, so at least k guards are needed. This shows that g(3k) ::: k = L3k/3J. If n = 3k + i, where 0 :::: i :::: 2, then g(n) ::: g(3k) ::: k = Ln/3J. Supplement ary Exercises l. 2500 3. Yes 5. Yes 7. L�\ ni vertices, Lt<j ninj edges 9. a) � b): : �.) � � ac e h g 11. Complete subgraphs containing the following sets of vertices: {b, e, e, fl, {a, b, g}, {a, d, g}, {d, e, g}, {b, e, g} 13. Complete subgraphs containing the following sets ofver­ tices: {b, e, d, j, k}, {a, b, j, k}, Ie, f, g, i}, {a, b, i}, {a, i, j}, {b, d, e}, {b, e, i}, {b, i, j}, {g, h, i}, {h, i, j} 15. {e, d} is a minimum dominating set. 17. a) S-62 b) 19. a) 1 b)2 c)3 21.a)ApathfromutovinagraphG induces a path from f(u) to f(v) in an isomorphic graph H. b) Suppose f is an isomorphism from G to H. Ifvo, v\, ... , vn, voisaHamilton circuitinG,thenf(vo), f(v\), ... , f(vn), f(vo) must be a Hamilton circuit in H because it is still a circuit and f(Vi) =1= f(vj) for 0 :::: i < j :::: n. c) Suppose f is an isomorphism from G to H. If VO, vI. ... , vn, Vo is an Euler circuit in G, then f(vo), f(v\), ... , f(vn), f(vo) must be an Euler circuit in H because it is a circuit that contains each edge exactly once. d) Two isomorphic graphs must have the same crossing number because they can be drawn exactly the same way in the plane. e) Suppose f is an isomorphism from G to H. Then v is isolated in G if and only if f(v) is isolated in H. Hence, the graphs must have the same number of isolated vertices. t) Suppose f is an isomorphism from G to H. If G is bipartite, then the vertex set of G can be partitioned into V\ and V2 with no edge connecting a vertex of V\ with one in V2. Then the vertex set of H can be partitioned into f(V\) and f(V2) with no edge connecting a vertex in f(V\) and one in f(V2). 23.3 25. a) Yes b) No 27. No 29. Yes 31. Ifeisacutedge with endpoints u and v, then if we direct e from u to v, there will be no path in the directed graph from v to u, or else e would not have been a cut edge. Similar reasoning works if we direct e from v to u. 33. n -1 35. Let the vertices repre­ sent the chickens. We include the edge (u, v) in the graph if and only if chicken u dominates chicken v. 37. a) 4 b) 2 c) 3 d) 4 e) 4 t) 2 39. a) Suppose that G = (V, E). Let a, b E V. We must show that the distance between a and b in G is at most 2. If {a, b} 'I E this distance is 1, so as­ sume {a, b} E E. Because the diameter of G is greater than 3 there are vertices u and v such that the distance in G be­ �een u and v is greater than 3. Either u or v, or both, is not in the set {a, b}. Assume that u is different from both a and b. Either {a, u} or {b, u} belongs to E; otherwise a, u, b would be a path in G of length 2. So, without loss of gen­ erality, assume {a, u} E E. Thus v cannot be a or b, and by the same reasoning either {a, v} E E or {b, v} E E. In either case, this gives a path of length less than or equal to 3 from S-63 u to v in G, a contradiction. b) Suppose G = (V, E). Let a, b E V. We must show that the distance between a and b in G does not exceed 3. If {a, b} fj E, the result follows, so assume that {a, b} E E. Because the diameter of G is greater than or equal to 3, there exist vertices u and v such that the distance in G between u and v is greater than or equal to 3. Either u or v, or both, is not in the set {a, b}. Assume u is different from both a and b. Either {a, u} E E or {b, u} E E; otherwise a, u, b is a path oflength 2 in G. So, without loss of generalit y, assume {a, u} E E. Thus v is different from a and from b.1f {a, v} E E, then u, a, vis a path oflength2 in G, so {a, v} fj E and thus {b, v} E E (or else there would be a path a, v, b oflength 2 in G). Hence, {u, b} fj E; otherwise u, b, v is a path of length 2 in G. Thus, a, v, u, b is a path of length 3 in G, as desired. 41. a, b, e, Z 43. a, c, b, d, e, Z 45. If G is planar, then because e :s 3v - 6, G has at most 27 edges. (If G is not connected it has even fewer edges.) Similarly, G has at most 27 edges. But the union of G and G is K II, which has 55 edges, and 55 > 27 + 27. 47. Suppose that G is colored with k colors and has independence number i. Because each color class must be an independent set, each color class has no more than i elements. Thus there are at most ki vertices. 49. a) By Theorem 2 of Section 6.2, the proba­ bility of selecting exactly m edges is C(n, m )pm(l _ p )n-m. b) By Theorem 2 in Section 6.4, the expected value is np. c) To generate a labeled graph G, as we apply the process to pairs of vertices, the random number x chosen must be less than or equal to 1/2 when G has an edge between that pair of vertices and greater than 1/2 when G has no edge there. Hence, the probability of making the correct choice is 1/2 for each edge and 1/2C(n.2) overall. Hence, all labeled graphs are equally likely. 51. Suppose P is monotone increasing. If the property of not having P were not retained whenever edges are removed from a simple graph, there would be a simple graph G not having P and another simple graph G' with the same vertices but with some of the edges of G missing that has P. But P is monotone increasing, so because G' has P, so does G obtained by adding edges to G'. This is a contradiction. The proof of the converse is similar. CHAPTER 10 Section 10.1 1. (a), (c), (e) 3. a) a b) a, b, c, d, /, h, j, q, t c) e, g, i,k,l,m,n,o,p,r,s,u d)q,r e)c 1)p g)/,b,a h) e,f,I,m,n 5. No 7. Level 0: a; level 1: b,c,d; level 2: e through k (in alphabetical order); level 3 : 1 through r; level 4: s, t; level 5: u 9. a) The entire tree b) c, g, h, 0, p and the four edges cg, ch, ho, hp c) e alone 11. a) 1 b) 2 13. a) 3 b) 9 15. The "only if' part is Theorem 2 and the definition of a tree. Suppose G is a connected simple graph with n vertices and n -1 edges. If G is not a tree, it contains, by Exercise 14, an edge whose removal produces a graph G', which is still connected. If G' is not a tree, remove an edge to produce a connected graph Gil. Repeat this procedure until the Answers to Odd-Numbered Exercises 8-63 result is a tree. This requires at most n -1 steps because there are only n -1 edges. By Theorem 2, the resulting graph has n -1 edges because it has n vertices. It follows that no edges were deleted, so G was already a tree. 17.9999 19.2000 21. 999 23.1,000,000 dollars 25. No such tree exists by Theorem 4 because it is impossible for m = 2 or m = 84. 27. Complete binary tree of height 4: Complete 3-ary tree of height 3: 29. a) By Theorem 3 it follows that n = mi + 1. Because i + 1 = n, we have 1 = n -i, so 1 = (mi + 1) - i = (m -l)i + 1. b) We have n = mi + 1 and i + 1 = n. Hence, i = n -I. It follows that n = m(n -I) + 1. Solv­ ing for n gives n = (ml -1)/(m -1). From i = n - 1 we obtain i = [(ml -1)/(m -1)] -I = (1-1)/(m -1). 31. n -t 33. a) 1 b) 3 c) 5 35. a) The parent direc­ tory b) A subdirectory or contained file c) A subdirec­ tory or contained file in the same parent directory d) All directories in the path name e) All subdirectories and files continued in the directory or a subdirectory of this directory, and so on 1) The length of the path to this directory or file g) The depth of the system, i.e., the length of the longest path 37. Let n = 2k, where k is a positive integer. If k = 1, there is nothing to prove because we can add two numbers with n -I = 1 processor in log 2 = I step. Assume we can add n = 2k numbers in log n steps using a tree-connected net­ work of n -1 processors. Let XI, X2, ••• , X2n be 2n = 2k+1 numbers that we wish to add. The tree-connected network of 2n - I processors consists of the tree-connected network of n -1 processors together with two new processors as chil­ dren of each leaf. In one step we can use the leaves of the larger network to find XI + xi, X3 + X4, ••• , X2n-1 + X2n, giv­ ing us n numbers, which, by the inductive hypothesis, we can add in log n steps using the rest of the network. Because we have used log n + I steps and log(2n) = log 2 + log n = 1 + logn, this completes the proof. 39. conly 41. candh 43. Suppose a tree T has at least two centers. Let u and v be distinct centers, both with eccentricity e, with u and v not ad­ jacent. Because T is connected, there is a simple path P from u to v. Let c be any other vertex on this path. Because the eccentricity of c is at least e, there is a vertex w such that the unique simple path from c to w has length at least e. Clearly, this path cannot contain both u and v or else there would be a simple circuit. In fact, this path from c to w leaves P and does not return to P once it, possibly, follows part of P toward S-64 Answers to Odd-Numbered Exercises either u or v. Without loss of generality, assume this path does not follow P toward u. Then the path from u to c to w is sim­ ple and oflength more than e, a contradiction. Hence, u and v are adjacent. Now because any two centers are adjacent, if there were more than two centers, T would contain K 3, a sim­ ple circuit, as a subgraph, which is a contradiction. 45. 47. The statement is that every tree with n vertices has a path of length n -I, and it was shown only that there exists a tree with n vertices having a path of length n -I. Section 10.2 1. banana papaya 3. a) 3 b) I c) 4 d) 5 S-64 5. the fox dog jumps over 7. At least rlog341 = 2 weighings are needed, because there are only four outcomes (because it is not required to determine whether the coin is lighter or heavier). In fact, two weighings suffice. Begin by weighing coin I against coin 2. If they bal­ ance, weigh coin I against coin 3. If coin I and coin 3 are the same weight, coin 4 is the counterfeit coin, and if they are not the same weight, then coin 3 is the counterfeit coin. If coin I and coin 2 are not the same weight, again weigh coin I against coin 3. If they balance, coin 2 is the counterfeit coin; if they do not balance, coin I is the counterfeit coin. 9. At least rtog3 131 = 3 weighings are needed. In fact, three weighings suffice. Start by putting coins I, 2, and 3 on the left-hand side of the balance and coins 4, 5, and 6 on the right-hand side. If equal, apply Example 3 to coins 1,2,7,8,9, 10, 11, and 12. Ifunequal, apply Example 3 to 1,2,3,4,5,6, 7, and 8. 11. The least number is five. Call the elements a, b, c, and d. First compare a and b; then compare c and d. Without loss of generality, assume that a < b and c < d. Next compare a and c. Whichever is smaller is the smallest element of the set. Again without loss of generality, suppose a < c. Finally, com­ pare b with both c and d to completely determine the ordering. 13. The first two steps are shown in the text. After 22 has been identified as the second largest element, we replace the leaf22 by -00 in the tree and recalculate the winner in the path from the leaf where 22 used to be up to the root. Next, we see that 17 is the third largest element, so we repeat the process: replace the leaf 17 by -00 and recalculate. Next, we see that 14 is the fourth largest element, so we repeat the process: replace the leaf14 by -00 and recalculate. Next, we see that 11 is the fifth largest element, so we repeat the process: replace the leaf 11 by -00 and recalculate. The process continues in this manner. We determine that 9 is the sixth largest element, 8 is the sev­ enth largest element, and 3 is the eighth largest element. The trees produced in all steps, except the second to last, are shown here. S-65 11 Answers to Odd-Nwnbered Exercises S-65 15. The value of a vertex is the list element currently there, and the label is the name (i.e., location) ofthe leaf responsible for that value. procedure tournament sort(a\, ... , an) k := rlognl build a binary tree of height k for i := 1 to n set the value of the ith leaf to be ai and its label to be itself for i := n + 1 to 2k set the value of the ith leafto be -00 and its label to be itself for i := k -1 downto 0 for each vertex v at level i set the value ofv to the larger of the values of its children and its label to be the label of the child with the larger value for i := 1 to n begin Ci := value at the root let v be the label of the root set the value ofv to be -00 while the label at the root is still v begin v := parent(v) set the value of v to the larger of the values of its children and its label to be the label of the child with the larger value end end {c\, ... ,Cn is the list in nonincreasing order} 17. k -1, wheren = 2k 19. a) Yes b) No c) Yes d) Yes 21. a: 000, e: 001, i: 01, k: 1100,0: 1101, p: 11110, u: 11111 23. a: 11; b: 101; c: 100; d: 01; e: 00; 2.25 bits (Note: This coding depends on how ties are broken, but the average num­ ber of bits is always the same.) 25. There are four possible answers in all, the one shown here and three more obtained from this one by swapping t and v and/or swapping u and w. w v 27. A:OOOl; B:101001; C:ll001; D:OOOOO; E:100; F:00ll00; G:OOllOl; H:OI0l; 1:0100; J:ll0100101; K:II01000; L:OOOOl; M:10101; N:OII0; 0:0010; P:I0I000; Q:II0I001000; R: 1011; 8:0111; T: 111; U:00ll1; V: 110101; W:ll000; X:ll010011; Y:ll0ll; Z:1101001001 29. A:2; E:l;N:010;R:Oll;T:02;Z:00 31. n 33. Because the tree is rather large, we have indicated in some places to "see text." Refer to Figure 9; the subtree rooted at these square or circle vertices is exactly the same as the corresponding subtree in Figure 9. First player wins. S-66 Answers to Odd-Numbered Exercises 35. a)$l b) $3 c)-$3 37.Seethefiguresshownnext. a) 0 b) 0 c) 1 d) This position cannot have occurred in a game; this picture is impossible. a) c) �I�I� o� o �I�I� �I�I� o X 0 X 01 10 b) �I�I� +I� O �I�I� �I�I� 00 0 0 +II 10 xox xox oxx oxx XOO XOO OXX XOX o X X X 00 X wins o X X OX 0 draw draw -I K+10 00 X X draw +1 �XO 00 X X �H� �HO X X X X X -I� O +I� O X wins fix 0 000 X X o wins �I�IO �I�I� �H� XOX X X X OX 01 +11 01 xxo xoo XXO OOX OOX OOX XOX XXX XOX draw X wins draw S-66 39. Proof by strong induction: Basis step: When there are n = 2 stones in each pile, if first player takes two stones from a pile, then second player takes one stone from the remaining pile and wins. If first player takes one stone from a pile, then second player takes two stones from the other pile and wins. In­ ductive step: Assume inductive hypothesis that second player can always win if the game starts with two piles of j stones for all 2 :::: j :::: k, where k :::: 2, and consider a game with two piles containing k + 1 stones each. Iffirst player takes all the stones from one of the piles, then second player takes all but one stone from the remaining pile and wins. If first player takes all but one stone from one of the piles, then second player takes all the stones from the other pile and wins. Otherwise first player leaves j stones in one pile, where 2 :::: j :::: k, and k + 1 stones in the other pile. Second player takes the same number of stones from the larger pile, also leaving j stones there. At this point the game consists of two piles of j stones each. By the inductive hypothesis, the second player in that game, who is also the second player in our actual game, can win, and the proofby strong induction is complete. 41. 7; 49 43. Value of tree is 1. Note: The second and third trees are the subtrees of the two children of the root in the first tree whose subtrees are not shown because of space limitations. They should be thought of as spliced into the first picture. +1 -I # # -1�-2 � ###+t=###=tt A 6-5=1 5-5=04-5=-16-5 =1 5-5=0 =ft==ft= 5-4= 1 6-4=2 -2 # *� 4-6=-2 6-6=0 5-6=-1 6-6=0 5-6=-1 Section 10.3 1. 0 1-1\ 1.11\ 1.: 0<1<1.1<1.2<2<3 S-67 3. 1.2.1.1 o 0< 1 < 1.1 < 1.2 < 1.2.1 < 1.2.1.1 < 1.2.1.2 < 1.2.2 < 1.2.3 < 1.2.3.1 < 1.2.3.2 < 1.2.3.2.1 < 1.2.3.2.2 < 1.2.3.3 < 2 < 2.1 1.2.3.2.2 5. No 7. a, b, d, e, j, g, e 9. a, b, e, k, I, m, f, g, n, r, s, e, d, h, 0, i, j, p, q 11. d, b, i, e, m, j, n, 0, a, j, e, g, k, h, p, 1 13. d, j, g, e, b, e, a 15. k, I, m, e, j, r, s, n, g, b, e, 0, h, i, p, q,j,d,a 17. a) + /� + / /\ /\ x /*\ x y x y b) + + x * xy/xy, +x/ + uyxy c) xxy * +xy/+, xxy * x + y/+ d) «x + (x * y» + (x/y», (x + «(x * y) +x)/y» 19. a) - /� n u /\ /\ A B A /-\ B A b) -nAB U A - B A d) «A nB) -(A U(B -A))) d) 2205 25. a b k c) A B nAB A -U- 21. 14 23. a) 1 b) 1 c) 4 Answers to Odd-Numbered Exercises 8-67 27. Use mathematical induction. The result is trivial for a list with one element. Assume the result is true for a list with n elements. For the inductive step, start at the end. Find the sequence of vertices at the end of the list starting with the last leaf, ending with the root, each vertex being the last child of the one following it. Remove this leaf and apply the inductive hypothesis. 29. e, d, b; j, g, h, e, a in each case 31. Proofby mathematical induction. Let S( X) and 0 (X) rep­ resent the number of symbols and number of operators in the well-formed formula X, respective ly. The statement is true for well-formed formulae of length 1, because they have 1 symbol and 0 operators. Assume the statement is true for all well-formed formulae of length less than n. A well-formed formula of length n must be of the form *XY, where * is an operator and X and Y are well-formed formulae oflength less than n. Then by the inductive hypot hesis S(*XY) = S(X) + S(Y) = [O(X) + 1] + [O(Y) + 1] = O(X) + O(Y) + 2. Because O(*Xy) = 1 + O(X) + O(y), it follows that S(*XY) = O(*XY) + 1. 33. x y +zx ° +x 0, x yz + + yx++, xyxyooxyoozo+, xzxzz+o, yyyyooo, Z x + y Z + 0, for instance Section 10.4 1. m -n + 1 3.: : !"-. g f e 7.')0 b)� c) S-68 Answers to Odd-Numbered Exercises S-68 9. I f � I I f I I I f I � I f I I e d e he de he de he d e h � i � i� i I i� i I �� iIi e d e he d e he d e he d e h I � � Ii � I Ii � I �i � Ii e de he de he d e he d e h I f � II f I Ii f I �i f I I e de he d e he de he de h 11. a) 3 b) 16 c) 4 d) 5 13. 15. � b f g j a b e d g h n m 17. a) A path oflength 6 b) A path oflength 5 c) A path oflength 6 d) Depends on order chosen to visit the vertices; may be a path oflength 7 19. With breadth-first search, the initial vertex is the middle vertex, and the n spokes are added to the tree as this vertex is processed. Thus, the resulting tree is Kl,n' With depth-first search, we start at the vertex in the middle of the wheel and visit a neighbor-one ofthe vertices on the rim. From there we move to an adjacent vertex on the rim, and so on all the way around until we have reached every vertex. Thus, the resulting spanning tree is a path oflength n. 21. With breadth-first search, we fan out from a vertex of de­ gree m to all the vertices of degree n as the first step. Next, a vertex of degree n is processed, and the edges from it to all the remaining vertices of degree m are added. The result is a Kl,n-l and a Kl,m-l with their centers joined by an edge. With depth-first search, we travel back and forth from one partite set to the other until we can go no further. If m = n or m = n -1, then we get a path of length m + n -1. Other­ wise, the path ends while some vertices in the larger partite set have not been visited, so we back up one link in the path to a vertex v and then successively visit the remaining vertices in that set from v. The result is a path with extra pendant edges coming out of one end of the path. 23. A possible set of flights to discontinue are: Boston-New York Detroit-Boston Boston-Washington, New York-Washing;on, New York� Chicago, Atlanta-Washington, Atlanta-Dallas, Atlanta-Los Angeles, Atlanta-St. Louis, St. Louis-Dallas, St. Louis­ Detroit, St. Louis-Denver, Dallas-San Diego, Dallas-Los Angeles, Dallas-San Francisco, San Diego-Los Angeles, Los Angeles-San Francisco, San Francisco-Seattle. 25. Trees 27. Proof by induction on the length of the path: If the path has length 0, then the result is trivial. Ifthe length is 1, then u is adjacent to v, so u is at level 1 in the breadth-first spanning tree. Assume that the result is true for paths of length I. If the length of a path is 1 + 1, let u' be the next-to-last vertex in a shortest path from v to u. By the inductive hypothesis, u' is at level 1 in the breadth-first spanning tree. If u were at a level not exceeding I, then clearly the length of the shortest path from v to u would also not exceed I. So u has not been added to the breadth-first spanning tree yet after the vertices oflevell have been added. Because u is adjacent to u', it will be added at level 1 + 1 (although the edge connecting u' and u is not necessarily added). 29. a) No solution b) c) S-69 31. Start at a vertex and proceed along a path without repeat­ ing vertices as long as possible, allowing the return to the start after all vertices have been visited. When it is impossible to continue along a path, backtrack and try another extension of the current path. 33. Take the union of the spanning trees of the connected components of G. They are disjoint, so the re­ sult is a forest. 35. m -n + c 37. Use depth-first search on each component. 39. If an edge uv is not followed while we are processing vertex u during the depth-first search pro­ cess, then it must be the case that the vertex v had already been visited. There are two cases. If vertex v was visited af­ ter we started processing u, then, because we are not finished processing u yet, v must appear in the subtree rooted at u (and hence, must be a descendant of u). On the other hand, if the processing of v had already begun before we started process­ ing u, then why wasn't this edge followed at that time? It must be that we had not finished processing v, in other words, that we are still forming the subtree rooted at v, so u is a descendant of v, and hence, v is an ancestor of u. 41. Certainly these two procedures produce the identical spanning trees if the graph we are working with is a tree itself, because in this case there is only one spanning tree (the whole graph). This is the only case in which that happens, however. Ifthe original graph has any other edges, then by Exercise 39 they must be back edges and hence, join a vertex to an ancestor or descendant, whereas by Exercise 40, they must connect vertices at the same level or at levels that differ by I. Clearly these two possibilities are mutually exclusive. Therefore there can be no edges other than tree edges if the two spanning trees are to be the same. 43. Because the edges not in the spanning tree are not followed in the process, we can ignore them. Thus we can assume that the graph was a rooted tree to begin with. The basis step is trivial (there is only one vertex), so we assume the inductive hypothesis that breadth-first search applied to trees with n ver­ tices have their vertices visited in order of their level in the tree and consider a tree T with n + I vertices. The last vertex to be visited during breadth-first search of this tree, say v, is the one that was added last to the list of vertices waiting to be processed. It was added when its parent, say u, was being processed. We must show that v is at the lowest (bottom-most, i.e., numerically greatest) level of the tree. Suppose not; say vertex x, whose parent is vertex w, is at a lower level. Then w is at a lower level than u. Clearly v must be a leaf, because any child of v could not have been seen before v is seen. Consider the tree T' obtained from T by deleting v. By the inductive hypothesis, the vertices in T' must be processed in order of their level in T' (which is the same as their level in T, and the absence ofv in T' has no effect on the rest of the algorithm). Therefore u must have been processed before w, and therefore v would have joined the waiting list before x did, a contradic­ tion. Therefore v is at the bottom-most level of the tree, and the proof is complete. 45. We modify the pseudocode given in Algorithm 2 by initializing m to be 0 at the beginning of the algorithm, and adding the statements "m := m + I" and "assign m to vertex v" after the statement that removes vertex v from L. 47. Ifa directed edge uv is not followed while we are processing its tail u during the depth-first search process, Answers to Odd-Numbered Exercises S-69 then it must be the case that its head v had already been visited. There are three cases. If vertex v was visited after we started processing u, then, because we are not finished processing u yet, v must appear in the subtree rooted at u (and hence, must be a descendant of u), so we have a forward edge. Other wise, the processing of v must have already begun before we started processing u. Ifit had not yet finished (i.e., we are still forming the subtree rooted at v), then u is a descendant of v, and hence, v is an ancestor ofu (we have a back edge). Finally, if the pro­ cessing of v had already finished, then by definition we have a cross edge. 49. Let T be the spanning tree constructed in Figure 3 and T\, T2, T3, and T4 the spanning trees in Fig­ ure 4. Denote by d(T', Til) the distance between T' and Til. Then d(T, T\) = 6, d(T, T2) = 4, d(T, T3) = 4, d(T, T4) = 2, d(h T2) = 4, d(h T3) = 4, d(h T4) = 6, d(Tz. T3) = 4, d(Tz. T4) = 2, and d(h T4) = 4. 51. Suppose e\ = {u, v} is as specified. Then T2 U fed contains a simple circuit C containing e\. The graph TJ -{eJ} has two connected compo­ nents; the endpoints of e\ are in different components. Travel C from u in the direction opposite to e\ until you come to the first vertex in the same component as v. The edge just crossed is e2. Clearly, T2 U fed -{e2} is a tree, because e2 was on C. Also TJ -fed U {e2} is a tree, because e2 reunited the two components. 53. Exercise 18: Exercise 19: Exercise 20: z:i CdC d b C a e Exercise 21: Exercise 22: Exercise 23: W LN ,2Jj, de fed ad g j 55. First construct an Euler circuit in the directed graph. Then delete from this circuit every edge that goes to a vertex pre­ viously visited. 57. According to Exercise 56, a directed graph contains a circuit if and only if there are any back edges. We can detect back edges as follows. Add a marker on each vertex v to indicate what its status is: not yet seen (the initial situation), seen (i.e., put into T) but not yet finished (i.e., visit(v) has not yet terminated), or finished (i.e., visit(v) has terminated). A few extra lines in Algorithm I will accomplish this bookkeeping. Then to determine whether a directed graph has a circuit, we just have to check when looking at edge uv whether the status of v is "seen." If that ever happens, then we know there is a circuit; if not, then there is no circuit. S-70 Answers to Odd-Numbered Exercises Section 10.5 1. Deep Springs- Oasis, Oasis-Dyer, Oasis-Si lverspeak, Silverspeak- -Goldfield, Lida--Gold Point, Gold Point-Beatt y, Lida--Goldfield, Goldfield -Tonopah, Tonopah-Manhattan, Tonopah-Warm Springs 3. Ie, fl, {e, f}, Ie, hI, {h, iI, {b, e}, {b, d}, {a, d}, {g, h} 5. San Francisco $1200 $900 Denver Chicago New York � �.'f � Atlanta 7. Ie, fl, {a, d}, {h, iI, {b, d}, {e, f}, Ie, h}, {b, e}, {g, h} 9. a tJ. b 1 c 11. Instead of choosing minimum-weight edges at each stage, choose maximum-weight edges at each stage with the same properties. 13. a b 15. 2 4!Ye1 3 a b c c d 3 3 e 2 3 g f j 4k 3 4 3 4 3 m n 0 d h 2 3 3 3 p 17. First find a minimum spanning tree T ofthe graph G with n edges. Then for i = 1 to n -1, delete only the ith edge of T from G and find a minimum spanning tree of the remain­ ing graph. Pick the one of these n - 1 trees with the shortest length. 19. If all edges have different weights, then a con­ tradiction is obtained in the proof that Prim's algorithm works when an edge ek+ 1 is added to T and an edge e is deleted, instead of possibly producing another spanning tree. 21. abc d 3 e 4 f 2 2 g....-..::...3--e h 4 j k 23. Same as Kruskal's algorithm, except start with T := this set of edges and iterate from i = 1 to i = n - 1 - s, where s is the number of edges you start with. 25. a) Chicago New York San Francisco 1200._-..... 900 Denver Atlanta b) a 2 b 1 f 3 e 2 j c 1 d 2 g k 3 1 h 3 S-70 27. By Exercise 24, at each stage of SolI in's algorithm a forest results. Hence, after n - 1 edges are chosen, a tree results. It remains to show that this tree is a minimum spanning tree. Let T be a minimum spanning tree with as many edges in common with Sollin's tree S as possible. If T =1= S, then there is an edge e E S -T added at some stage in the algorithm, where prior to that stage all edges in S are also in T. T U {e} contains a unique simple circuit. Find an edge e' E S -T and an edge e" E T -S on this circuit and "adjacent" when viewing the trees of this stage as "superverti ces." Then by the algorithm, wee') � w(e"). So replace T by T -{e"} U Ie'} to produce a minimum spanning tree closer to S than Twas. 29. Each of the r trees is joined to at least one other tree by a new edge. Hence, there are at most r /2 trees in the result (each new tree contains two or more old trees). To accomplish this, we need to add r -(r /2) = r /2 edges. Because the number of edges added is integral, it is at least r r /21. 31. If k � log n, then n/2k � 1, so rn/2k1 = 1, so by Exercise 30 the algorithm is finished after at most log n iterations. Supplem entary Exercises 1. Suppose T is a tree. Then clearly T has no simple circuits. Ifwe add an edge e connecting two nonadjacent vertices u and v, then obviously a simple circuit is formed, because when e is added to T the resulting graph has too many edges to be a . tree. The only simple circuit formed is made up of the edge e together with the unique path P in T from v to u. Suppose T satisfies the given conditions. All that is needed is to show that T is connected, because there are no simple circuits in the graph. Assume that T is not connected. Then let u and v be in separate connected components. Adding e = {u, v} does not satisfy the conditions. 3. Suppose that a tree T has n vertices ofdegreesd1, d2, ... , dn,respectively. Because 2e = L:7=1 dj and e = n -1, we have 2(n -1) = L:7=1 dj• Because each dj � 1, it follows that 2(n -1) = n + L:7=I(dj -1), or that n - 2 = L:7=I(dj -1). Hence, at most n - 2 of the terms of this sum can be 1 or more. Hence, at least two of them are O. It follows that dj = 1 for at least two values of i . 5. 2n - 2 7. T has no circuits, so it cannot have a subgraph homeo­ morphic to K 3.3 or K 5. 9. Color each connected component separately. For each of these connected components, first root the tree, then color all vertices at even levels red and all ver­ tices at odd levels blue. 11. Upper bound: kh; lower bound: 2 rk / 21 h-l S-71 15. Because Bk+l is formed from two copies of Bk, one shifted down one level, the height increases by 1 as k increases by 1. Because Bo had height 0, it follows by induction that Bk has height k. 17. Because the root of B k+ I is the root of B k with one additional child (namely the root ofthe other Bk), the de­ gree of the root increases by 1 as k increases by 1. Because Bo had a root with degree 0, it follows by induction that Bk has a root with degree k. 19. • So 21. Use mathematical induction. The result is trivial for k = O. Suppose it is true for k -1. Tk-l is the parent tree for T . By in­ duction, the child tree for T can be obtained from To, ... , Tk-2 in the manner stated. The final connection of rk-2 to rk-l is as stated in the definition of Sk-tree. 23. procedure level(T: ordered rooted tree with root r) queue := sequence consisting of just the root r while queue contains at least one term begin v : = first vertex in queue list v remove v from queue and put children of v onto the end of queue end 25. Build the tree by inserting a root for the address 0, and then inserting a subtree for each vertex labeled i, for i a positive integer, built up from subtrees for each vertex labeled i.j for j a positive integer, and so on. 27. a) Yes b) No c) Yes 29. The resulting graph has no edge that is in more than one simple circuit of the type described. Hence, it is a cactus. 31. ab c :z d e f 33. h g Answers to Odd-Numbered Exercises S-71 35. a)! 4 2 3 b)l 5 3 4 • • • • I • • 2 c) 1 6 3 5 d)T • I I • 2 4 5 37. 6 39. a) o for 00, 11 forOl, 100 for 10,101 for 11 (exact coding depends on how ties were broken, but all versions are equivalent); 0.645n for string oflength n b) 0 for 000, 100 for 001, 101 for 010, 110 for 100, 11100 for 011, 11101 for 101, 11110 for 110, 11111 for III (exact coding depends on how ties were broken, but all versions are equivalent); 0.5326n for string of length n 41. Let G' be the graph obtained by deleting from G the vertex v and all edges incident to v. A min­ imum spanning tree of G can be obtained by taking an edge of minimal weight incident to v together with a minimum span­ ning tree of G'. 43. Suppose that edge e is the edge ofleast weight incident to vertex v, and suppose that T is a spanning tree that does not include e. Add e to T, and delete from the simple circuit formed thereby the other edge of the circuit that contains v. The result will be a spanning tree of strictly smaller weight (because the deleted edge has weight greater than the weight of e). This is a contradiction, so T must include e. CHAPTER 11 Section 11.1 l.a)1 b)1 c)O d)O 3.a)(I·I)+(0·1+0)= 1 + (0 + 0) = 1 + (1 + 0) = 1 + 1 = 1 b)(T /\ T) v (--.(F /\ T) v F) == T 5. a) x y Z xy b) x y z x+yz 1 1 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 0 1 0 1 0 1 1 1 0 0 0 1 0 0 1 0 1 1 1 0 1 1 1 0 1 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 c) x y z xy +xyz d) x y z x(yz + yZ) 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 0 1 0 1 1 1 0 1 0 1 0 0 1 1 0 0 1 0 1 1 1 0 1 1 0 0 1 0 1 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 0 0 0 S-72 Answers to Odd-Numbered Exercises 7. a) 1 .11'"10 _ _ --,.,111 b) 110 100 f--+--{ 101 100 ... -1--_____ III '.--1----Jt 011 ')--1-� 011 c) 000 001 110 100ff---1--f( III d) 110 111 1 00ff---lf--( 1O I 1�-t-----fj 011 '�--I--,I 011 000 001 9. (0, 0) and (I, I) 1l.x+xy=x·l+xy=x(l+ y) = x(y + I) = X· I = x 13. xy+y"i xy+yz x y z xy y"i xz I I I 0 0 0 I I 0 0 I 0 I 0 I I 0 0 I 0 0 I 0 0 0 I I 0 0 I 0 I 0 0 I 0 0 0 I 0 0 I 0 0 0 0 0 0 x ·x 15·x x+x --11----1-­ 19. 21. o 0 I I x y z y+z I I I I I I 0 I I 0 I I I 0 0 0 0 I I I 0 I 0 I 0 0 I I 0 0 0 0 x y xy I I I I 0 0 0 I 0 0 0 0 o I x+ (y+z) I I I I I I I 0 (xy) X 0 0 1 0 I I I I +xz xy yz x"i 0 0 0 0 I 0 0 I I 0 I 0 I 0 0 I I I 0 0 I I 0 0 I 0 I 0 0 0 0 0 17. x x + 1 x· 0 -0+--1 -+-0- I I 0 (x+y) +x"i 0 I I I I I I 0 x+y +z yz x(yz) xy I I I 1 I I I 0 0 I I I 0 0 0 I I 0 0 0 I I I 0 0 I I 0 0 0 0 I 0 0 0 0 0 0 0 0 y x+y x+y (x+y) 0 0 I 0 I I I 0 0 I I 0 I I 0 I 23. 0 . 0 = 0 . I = 0; I . T = I . 0 = 0 (xy)z I 0 0 0 0 0 0 0 xy 0 0 0 I S-72 25. x y xEBy x+y xy (xy) (x+ y)(xy) xy xy xy+xy I 1 0 I I 0 0 0 0 0 I 0 I I 0 I I I 0 I 0 I I I 0 I I 0 I I 0 0 0 0 0 I 0 0 0 0 27. a) True, as a table of values can show b) False; take x = I, y = I, Z = I, for instance c) False; take x = I, Y = I, Z = 0, for instance 29. By De Morgan's laws, the com­ plement of an expression is like the dual except that the com­ plement of each variable has been taken. 31. 16 33. Ifwe replace each 0 by F, I by T, Boolean sum by v, Boolean prod­ uct by /\, and -by ..... (and x by p and y by q so that the variables look like they represent propositions, and the equals sign by the logical equivalence symbol), then xy = x + y becomes ..... (p /\ q) ==""'p v ..... q and x + y = xYbecomes ..... (p v q) == ""'p /\ ..... q. 35. By the domination, distributive, and iden­ tity laws, x v x = (x v x) /\ I = (x v x) /\ (x V X) = x v (x /\ X) = x v 0 = x . Similarly, x /\ x = (x /\ x) V 0 = (x /\ x)v(x/\X)=x /\(xvX)=x/\l=x. 37. Because o v I = I and 0 /\ I = 0 by the identity and commutative laws, it follows that 0 = I. Similarly, because I v 0 = I and I /\ 0 = I, it follows that T = O. 39. First, note that x /\ 0 = o and x v I = I for all x, as can easily be proved. To prove the first identity, it is sufficient to show that (x v y) v (X /\ y) = I and (x v y) /\ (X /\ y) = O. By the associative, commutative, distributive, domination, and identity laws, (x v y) v (X /\ y) = Y v [x v (X /\ Y)] = Y v [(x vX) /\ (x vy)] = y v [1 /\ (x V Y)] = y v (x. v y) = (y v y) v x = I v x = I and (x v y)/\(X /\y)=y /\ [x /\(xvy)]=y/\[(X /\x)v(X /\y)] =y /\ [0 V (X /\ y)] = Y /\ (X /\ y) = X /\ (y /\ y) = X /\ 0 = O. The second identity is proved in a similar way. 41. Using the hypotheses, Exercise 35, and the distributive law it follows that x = x v 0 = x v (x v y) = (x v x) v y = x v y = O. Similarly, y = O. To prove the second statement, note that x = x /\ I = x /\ (x /\ y) = (x /\ x) /\ Y = X /\ Y = 1. Sim­ ilarly, y = 1. 43. Use Exercises 39 and 41 in the Sup­ plementary Exercises in Chapter 8 and the definition of a complemented, distributed lattice to establish the five pairs of laws in the definition. Section 11.2 1. a) xyz b) xyz c) xyz d) xyz 3. a) xyz + xyz + xyz + xyz + xyz + xyz + xyz b) xyz + xyz + xyz c) xyz + xyz + xyz + xyz d) xyz + xyz 5. wxyz + wxyz + wxyz + Wxyz + Wxyz + Wxyz + Wxyz + wxyz 7.a)x+y+z b)x+y+z c)x+y+z 9. Yl + Y2 + ... + Yn = 0 if and only if Yi = 0 for i = I, 2, ... ,n. This holds if and only if Xi = 0 when Yi = Xi andxi = I whenYi = Xi. 11. a)x + y +z b) (x + y +z) (x + y +Z)(x + y +z)(X + y +z)(x + y +Z) c)(x + y + z)(x + y + Z)(x + y +z)(x + y +Z) d)(x + y+z) �+y+Z)�+y+��+y+Z)(X+y+�(X+y+Z) S-73 13. a) x + Y + z b) x + [y + (X + z)] c)(x + Y> d) [x + (x + Y + Z)] 15.a)x x x Lx 1 0 0 o 1 1 b) x y xy xLx yLy (x L x)L (y L y) 1 1 1 0 0 1 1 0 0 0 1 0 0 1 0 1 0 0 0 0 0 1 1 0 c) x y x+y (x L y) (x L y) L (x L y) 1 1 1 0 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 0 17. a) {[(x I x) I (y I y)] I [(x I x) I (y I y)]} I (z I z) b){[(xlx)l(zlz)]ly} I {[(x Ix) I (z Iz)] Iy} c)x d) [x I (y I y)] I [x I (y I y)] 19. It is impossible to repre­ sent x using + and . because there is no way to get the value o if the input is 1. Section 11.3 l.(x+y)y 3.(xy)+(z+x) (X + y +z)+(X + y +Z) 7. v--l_ ....... w J-----, x---t_-' v--l_ ....... W y--l�../ v--l_ ....... w z---t_-' v--l_ ....... X y--l�../ v--l_ ....... X z--I�../ v--l_ ....... y Z--I_--- w--l_ ....... x y---t---- w--l_ ....... x z---t_--- w--l_ ....... y )--'-''''-----' Z--I---- x--lw--...... y )--"-'--_...J z--l-"'" 5. (x + y+z)+ Answers to Odd-Numbered Exercises S-73 9. Xo Yo XI YI X2 Y2 X3 Y3 X4 Y4 X5 Y5 HA = half adder FA = full adder 11. x ----+l--......, Y------.! )------, bj ------.! x ------.!--......, Y -.;;x:o---+I J---...., bj --f.>co---+I x --f.>co---+l--""'" Y ------.! )--_....J bj --Dco-----.! x -... Dc:o-----.!- -......, y -.;;x:o---+I )------' bj----+I x ----+l--......, y ------.! )------, bj ------.! x -... {)C..------.!- -......, y ----+I J---...., bj--- -+l __ x --!)c:..----+l-....... y ------.! )--_...J bj --Dc..------.! x --{)C..------.!--......, y --!)c:..------+I )-------' bj ------+I 13. YI -----,.'L_----' Xo ---------+1 � ��------�_/ So SI S2 S3 S4 S5 S6 d 8-74 Answers to Odd-Numbered Exercises 15.a):� x d) x b) x x y y ---.'------' c) x y x y ---.'------' x -_�--" y------� x -------I� Y Y 17. x -'" y \. 19. Co CI X3 Co CI X2 Co CI XI Co CI Xo Circuit from 15 (d) Circuit from 15 (c) x+y xy Sum =xElh Carry=xy Section 11.4 1. a) y Y :E8 3. a) y y :EEJ b)xy andxy b) y y c) y y 5. a) :EE x[EJ x 1 x[EJ yz yz x b) xyz, xyz, xyz 7. a) yz y� yz x b) yz yz .Vz X X 1 1 S-74 S-75 c) v- x 1 1 1 r 1 9. Implicants: xyz, xyz, x yz, xyz, xy, xz, yz; prime impli­ cants: xy, xz, yz; essential prime implicants: xy, xz, yz x 1 1 1 x 1 11. The 3-cube on the right corresponds to w; the 3-cube given by the top surface of the whole figure represents x; the 3-cube given by the back surface of the whole figure represents y; the 3-cube given by the right surfaces of both the left and the right 3-cube represents z. In each case, the opposite 3-face repre­ sents the complemented literal. The 2-cube that represents w z is the right face of the 3-cube on the right; the 2-cube that represents xy is bottom rear; the 2-cube that represents yz is front left. 13. a) yz yz yz wx IV:\' w:\' wx 1 b) Wxyz, Wxyz, Wxyz, Wxyz 15. a) Answers to Odd-Numbered Exercises S-75 X1X4X, x3x4'1', x3.{4·1'5 Xj.{4x, ·1'j·1'4x, .{1·1'4X, ·1'1x4·1', ·l'lx4x, b) c) 1 1 1 1 1 1 1 1 d). 1 1 1 1 1 1 1 1 S-76 Answers to Odd-Numbered Exercises I I I I I I I I I I I I I I I I t) I I I I I I I I I I I I I I I I 17. a) 64 b) 6 19. Rows 1 and 4 are considered adjacent. The pairs of columns considered adjacent are: columns 1 and 4, 1 and 12, 1 and 16, 2 and 11,2 and 15,3 and 6,3 and 10, 4 and 9,5 and 8, 5 and 16, 6 and 15, 7 and 10, 7 and 14, 8 and 13,9 and 12, 11 and 14, 13 and 16. 21. Smith --.r Jones --.r Adams --�_/ Smith --.,--", Jones --� Burton --�_/ Marcus --..,--....., Adams -----,� Burton 23. a) xz b) y c) xz + xz + yz d) xz + xy + yz 25. a) wxz + wxy + wyz + wxyz b) xyz + wyz + wxyz + wxyz + wxyz c) yz + wxy + wxy + wxyz d) wy + yz + xy + wxz + wxz 27. x(y + z) 29. z -__ oc�-..... -........ y --'DO--� x --'DO--� w -__ oc�-..... --" z -__ -I>C�-+r- ........ y-----� x ------.r.._./ z------.r- ........ y-----.r x ---to.f>C>----.L_./ z -----� w -----.r S-76 31. xz + xz 33. We use induction on n. If n = 1, then we are looking at a line segment, labeled 0 at one end and 1 at the other end. The only possible value of k is also 1, and if the literal is x" then the subcube we have is the O-dimensional subcube consisting of the endpoint labeled 1, and if the lit­ eral is x" then the subcube we have is the O-dimensional subcube consisting of the endpoint labeled O. Now assume that the statement is true for n; we must show that it is true for n + 1. If the literal xn+) (or its complement) is not part of the product, then· by the inductive hypothesis, the prod­ uct when viewed in the setting of n variables corresponds to an (n -k)-dimensional subcube of the n-dimensional cube, and the Cartesian product of that subcube with the line seg­ ment [0, 1] gives us a subcube one dimension higher in our given (n + 1 )-dimensional cube, namely having dimension (n + 1) -k, as desired. On the other hand, if the literal xn+) (or its complement) is part of the product, then the product of the remaining k - 1 literals corresponds to a subcube of dimension n -(k -1) = (n + 1) - k in the n-dimensional cube, and that slice, at either the I-end or the O-end in the last variable, is the desired subcube. Supplem entary Exercises 1. a) x = 0, y = 0, z = 0; x = 1, y = 1, z = 1 b) x = 0, y = O,z = O;x = O,y = O,z = l;x = O,y = l,z = O;x = 1, Y = 0, z = 1; x = 1, Y = 1, z = 0; x = 1, Y = 1, z = 1 c) No values 3. a) Yes b) No c) No d) Yes 5.22.-1 7. a) If F(x), ... , xn) = 1, then (F + G)(X), ... , xn) = F(x), ... , xn) + G(x), ... , xn) = 1 by the dominance law. Hence, F :::: F + G. b) If(FG)(x), ... , xn) = 1, then F(x), ... , xn)· G(x), ... , xn) = 1. Hence, F(x), ... , xn) = 1. It follows that FG :::: F. 9. Because F(x), ... , xn) = 1 implies that F(x), ... , xn) = 1, :::: is reflexive. Suppose that F :::: G and G :::: F. Then F(x), ... , xn) = 1 if and only if G(x), ... , xn) = 1. This implies that F = G. Hence, :::: is antisymmetric. Suppose that F :::: G :::: H. Then if F(x), ... , xn) = 1, it follows that G(x), ... , xn) = 1, which implies that H(x), ... , xn) = 1. Hence, F :::: H, so that <istransltlve. 1l. a)x=l,y=0,z=0 b)x=l, y = 0, z = 0 c) x = 1, y = 0, z = 0 S-77 13. x y x0y xEBy (x EB y) 1 1 1 0 1 1 0 0 1 0 0 1 0 1 0 0 0 1 0 1 15. Yes, as a truth table shows 17. a)6 b)5 c)5 d)6 19. x ---+ ...... D--------. y-'-�"' I Sum =x EB y \---. Carry = xy 21. X3 + X2X, 23. Suppose it were with weights a and b. Then there would be a real number T such that xa + yb � T for (1,0) and (0,1), but with xa + yb < T for (0,0) and (1,1). Hence, a � T, b � T, 0 < T, and a + b < T. Thus, a and b are positive, which implies that a + b > a � T, a contradic­ tion. CHAPTER 12 Section 12.1 I. a) sentence => noun phrase intransitive verb phrase => article adjective noun intransitive verb phrase => article adjective noun intransitive verb => ... (after 3 steps) ... => the happy hare runs. b) sentence => noun phrase intransitive verb phrase => article adjective noun intransitive verb phrase => article adjective noun intransitive verb adverb ... (after 4 steps) ... => the sleepy tortoise runs quickly c) sentence => noun phrase transitive verb phrase noun phrase => article noun transitive verb phrase noun phrase => article noun transitive verb noun phrase => article noun transitive verb article noun => ... (after 4 steps) ... => the tortoise passes the hare d) sentence => noun phrase transitive verb phrase noun phrase => article adjective noun transitive verb phrase noun phrase => article adjective noun transitive verb noun phrase => article adjective noun transitive verb article adjective noun => ... (after 6 steps) ... => the sleepy hare passes the happy tortoise Answers to Odd-Numbered Exercises S-77 3. The only way to get a noun, such as tortoise, at the end is to have a noun phrase at the end, which can be achieved only via the production sentence � noun phrase transitive verb phrase noun phrase. However, transitive verb phrase � transitive verb � passes, and this sentence does not contain passes. . 5.a) S => IA => lOB => lOlA => IOIOB => 10101 b) Because of the productions in this grammar, every 1 must be followed by a 0 unless it occurs at the end of the string. c) All strings consisting of a 0 or a 1 followed by one or more repetitions of 01 7. S => OSI => OOSl1 => OOOSI11 => 000111 9. a) S => OS => OOS => OOS 1 =>OOS 11 => OOS III => OOS 1111 => 001111 b) S => OS => OOS => OOIA => OOIIA => OOIIIA => 001111 11. S => OSAB =>OOSABAB =>OOABAB =>00 AABB => OOIABB => 0011BB => 00112B => 001122 13. a) S � 0, S � 1, S � 11 b) S � IS, S � A c) S � OAI, A � lA, A � OA, A � A d) S � OA, A � IIA, A � A 15. a) S � OOS, S � A b) S � lOA, A�OOA,A�A c)S�AAS,S�BBS,AB�BA, BA � AB, S � A, A � 0, B � 1 d) S � OOOOOOOOOOA, A�OA, A�A e)S� AS, S� ABS, S� A, AB � BA, BA � AB, A � 0, B � 1 t) S � ABS, S � A, AB � BA, BA � AB, A � 0, B � 1 g) S � ABS, S � T, S � U, T � AT, T � A, U � BU, U � B, AB�BA,BA�AB,A�O,B�I 17. a)S�OS, S � A b) S � AO, A � lA, A � A c) S � OOOS, S � A 19. a) Type 2, not type 3 b) Type 3 c) Type 0, not type 1 d) Type 2, not type 3 e) Type 2, not type 3 t) Type 0, not type 1 g) Type 3 h) Type 0, not type 1 i) Type 2, not type 3 j) Type 2, not type 3 21. Let S, and S2 be the start symbols ofG, and G2, respectively. Let S be a new start symbol. a) Add S and productions S � S, and S � S2. b) Add S and production S � S,S2. c) Add S and production S � A and S � S, S. 23. a) the happy hare runs 8-78 Answers to Odd-Numbered Exercises b) sentence /� l� 2� article adJective noun verb adverb I I I I I c) sentence �I� noun phrase transitive verb phrase noun phrase /� I /� article noun transitive verb article noun I I I I I the sleepy tortoise runs quickly the tortoise passes the hare d) sentence �I� noun phrase transitive verb phrase noun phrase /I� I /I� article adJective noun transitive verb article adjective noun I I I I I I I the sleepy hare passes 25. a) Yes b) No c) Yes d) No 27. signed integer /� sign integer I /� digit integer I /� digit integer I I o digit I 9 the happy tortoise 29. a) S .... (sign) (integer) S .... (sign)(integer) . (positive integer) (sign) .... + (sign) .... - (integer) .... (digit) (integer) .... (integer) (digit) (digit) .... i, i = 1, 2,3,4,5,6,7,8,9, 0 (positive integer) .... (integer) (nonzero digit) (positive integer) .... (nonzero digit) (integer) (positive integer) .... (integer) (nonzero digit) (integer) (positive integer) .... (nonzero digit) (nonzero digit) .... i, i = 1,2,3,4,5,6,7,8,9 b) (signed decimal number) ::= (sign)(integer) 1 (sign) (integer) . (positive integer) (sign) ::= +1- (integer) ::= (digit) 1 (integer) (digit) (digit) ::= 01 1 12 1 3 141 5 161 7 1 8 19 (nonzero digit) ::= 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 (positive in teger) ::= (integer)(nonzero digit) 1 (nonzero digit) (integer) 1 (integer) (nonzero integer) (integer) 1 (nonzero digit) S-78 S-79 c) /7� i /\ . �·rF -integer digit nonzero digit I I I digit 1 4 I 3 31. a) (identifier) ::= (Icletter) 1 (identifier) (lcletter) (Icletter) ::= alb 1 c 1 ••• 1 z b) (identifier) ::= (Icletter) (Icletter) (Icletter) 1 (Icletter) (Icletter) (Icletter) (Icletter) 1 (Icletter) (Icletter) (Icletter) (Icletter) (Icletter) 1 (Icletter) (Icletter) (Icletter) (Icletter) (Icletter) (Icletter) (Icletter) ::= alb 1 c 1 ••• 1 z c) (identifier) ::= (ucletter) 1 (ucletter)(letter) 1 (ucletter)(letter)(letter) 1 Answers to Odd-Numbered Exercises S-79 (ucletter) (letter) (letter) (letter) 1 (ucletter) (letter) (letter) (letter) (letter) 1 (ucletter) (letter) (letter) (letter) (letter) (letter) (letter) ::= (Icletter) 1 (ucletter) (Icletter) ::= alb 1 c 1 ... 1 z (ucletter) ::= AlB 1 C 1 ... 1 z d) (identifier) ::= (lcletter) (digitorus) (alphanumeric) (alphanumeric) (alphanumeric) 1 (lcletter) (digitorus) (alphanumeric) (alphanumeric) (alphanumeric) (alphanumeric) (digitorus) ::= (digit) 1 _ (alphanumeric) ::= (letter) 1 (digit) (letter) ::= (lcletter) 1 (ucletter) (lcletter) ::= alb 1 c 1 ... 1 z (ucletter) ::= AlB 1 C 1 ... 1 Z (digit) ::= 0 1 1 1 2 1 ••• 1 9 33. (identifier) ::= (letterorus) 1 (identifier) (symbol) (letterorus) ::= (letter) 1 _ (symbol) ::= (Ietterorus) 1 (digit) (letter) ::= (lcletter) 1 (ucletter) (lcletter) ::= alb 1 c 1 ... 1 z (ucletter) ::= AlB 1 C 1 ... 1 Z (digit) ::= 0 1 1 1 2 1 ••• 1 9 35. numeral ::= sign? nonzerod igit digit* decimal? 1 sign? 0 decimal? sign ::= + 1- nonzerodigit ::= 1 1 2 1 ••• 1 9 digit ::= 01 nonzerod igit decimal ::= .digit* 37. identifier ::= letterorus symbol* letterorus ::= letter 1 - symbol ::= letterorus 1 digit letter ::= Icletter 1 ucletter lcletter ::= alb 1 c 1 ... 1 z ucletter ::= AlB 1 C 1 ••• 1 Z digit ::= 0 1 1 1 2 1 ... 1 9 S-80 Answers to Odd-Numbered Exercises 39. a) (expression) (term) (term) (addOperator) (jactor) (jactor) (jactor) (muIOperator) (addOperator) (identifier) (identifier) (identifier) (muIOperator) (addOperator) abc*+ b) Not generated c) (expression) (term) (jactor) (jactor) (muIOperator) (expression) (jactor) (muIOperator) (term) (term) (addOperator) (jactor) (muiOperator) (jactor) (jactor) (addOperator) (jactor) (muiOperator) (identifier) (identifier) (addOperator) (identifier) (muiOperator) xy-z* d) (expression) (term) (jactor) (jactor) (muIOperator) (jactor) (expression) (muIOperator) (jactor) (term) (muIOperator) (jactor) (jactor) (jactor) (muiOperator) (muiOperator) (jactor) (jactor) (expression) (muIOperator) (muiOperator) (jactor) (jactor) (term) (term) (addOperator) (muIOperator) (muIOperator) (jactor) (jactor) (jactor) (jactor) (addOperator) (muIOperator) (muIOperator) (identifier) (identifier) (identifier) (identifier) (addOperator) (muIOperator) (muIOperator) wxyz-*/ e) (expression) (term) (jactor) (jactor) (muiOperator) (jactor) (expression) (muIOperator) (jactor) (term) (term) (addOperator) (muiOperator) (jactor) (jactor) (jactor) (addOperator) (muIOperator) (identifier) (identifier) (identifier) (addOperator) (muiOperator) ade-* 41. a) Not generated b) (expression) (term) (addOperator) (term) (jactor) (muiOperator) (jactor) (addOperator) (jactor) (muIOperator) (jactor) (identifier) (muIOperator) (identifier) (addOperator) (identifier) (muIOperator) (identifier) a/b +c/d c) (expression) (term) (jactor) (muIOperator) (jactor) (jactor) (muIOperator)( (expression) (jactor) (muIOperator)( (term) (addOperator) (term) (jactor) (muiOperator)( (jactor) (addOperator) (jactor) (identifier) (muIOperator)( (identifier) (addOperator) (identifier) m *(n + p) S-80 S-8J Answers to Odd-Nwnbered Exercises S-81 d) Not generated e) (expression) (term) (factor) (muLOperator) (factor) ((expression) ) (muIOperator) ( (expression) ((term) (addOperator) (term) ) (muIOperator) ( (term) (addOperator) (term) «factor) (addOperator) (factor) ) (mulOperator) ( (factor) (addOperator) (factor) ((identifier) (addOperator) (identifier) ) (muLOperator)( (identifier) (addOperator) (identifier) (m+n)*(p-q) Section 12.2 1. a) b) 1,0 c) Start Start Start 3. a) 01010 b) 01000 c) 11011 5. a) 1100 b) 00110110 c) 11111111111 7. XE {'if',�,!I} 25,0 'if' = cola � = root beer !I = ginger ale 25,0 25,5 25,10 1,0 8-82 Answers to Odd-Numbered Exercises 13. 11. (5, 10,25), open 25,open 15. Let So be the start state and let SI be the state representing a successful call. From So, inputs of 2, 3, 4, 5, 6, 7, or 8 send the machine back to So with output of an error message for the user. From So an input of 0 sends the machine to state SI, with the output being that the 0 is sent to the network. From So an input of 9 sends the machine to state S2 with no output; from there an input of 1 sends the machine to state S3 with no out­ put; from there an input of 1 sends the machine to state SI with the output being that the 911 is sent to the network. All other inputs while in states S2 or S3 send the machine back to So with output of an error message for the user. From So an input of 1 sends the machine to state S4 with no output; from S4 an input v=Valid ID i = Invalid ID p = Valid password q = Invalid password a = "Enter user ID" b = "Enter password" c=Prompt X= Any input S-82 of2 sends the machine to state S5 with no output; and this.path continues in a similar manner to the 911 path, looking next for 1, then 2, then any seven digits, at which point the machine goes to state SI with the output being that the ten-digit input is sent to the network. Any "incorrect" input while in states Ss or S6 (that is, anything except a 1 while in S5 or a 2 while in S6) sends the machine back to So with output of an error message for the user. Similarly, from S4 an input of8 followed by appro­ priate successors drives us eventually to SI, but inappropriate outputs drive us back to So with an error message. Also, inputs while in state S4 other than 2 or 8 send the machine back to state So with output of an error message for the user. S-83 17. Start 19. 21. f Input State 0 1 So S l S2 Sl Sl So S2 Sl S2 g 1 1 0 1,0 1,0 R,I 23. a) lllll b) 1000000 c) 100011001100 25. Answers to Odd-Numbered Exercises s-83 S-84 Answers to Odd-Numbered Exercises Section 12.3 1. a) {OOO, 001, IlOO,IlOI} b) {OOO,OOIl,OIO,OIIl} c) {OO,OII, 110, IIII} d) {OOOOOO, 000001,000100, 000101,010000,010001,010100, 0l01OI} 3. A = {I, IOI}, B = {0,1l,000}; A={IO,IlI,IOIO,1000,IOIIl,101000}, B = {A}; A = {A, IO}, B = {IO, 111, 1000} or A = {A}, B = {IO, Ill, 1010, 1000,10111, IOIOOO} 5. a)·The set of strings consisting of zero or more consecutive bit pairs 10 b) The set of strings consisting of all 1 s such that the number of Is is divisible by 3, including the null string c) The set of strings in which every 1 is immediately pre­ ceded by a 0 d) The set of strings that begin and end with a 1 and have at least two 1 s between every pair of Os 7. A string is in A* if and only if it is a concatena­ tion of an arbitrary number of strings in A. Because each string in A is also in B, it follows that a string in A * is also a concatenation of strings in B. Hence, A * 5:; B * . ��fu �fu �� �� �fu Qfu II. a) Yes b) No c) Yes d) No 13. a) Yes b) Yes c) No d) No e) No Q No 15. We use structural in­ duction on the input string y. The basis step considers y = A, and for the inductive step we write y = w a, where w E [* and a E I. For the basis step, we have xy = x, so we must show that f(s, x) = f(f(s, x), A). But part (i) of the definition of the extended transition function says that this is true. We then assume the inductive hypothesis that the equation holds for w and prove that f(s, xwa) = f(f(s, x), wa). By part (ii) of the definition, the left-hand side of this equation equals f(f(s, xw), a). By the inductive hypothesis, f(s, xw) = f(f(s, x), w), so f(f(s, xw), a) = f(f(f(s, x), w), a). The right-hand side of our desired equality is, by part (ii) of the definition, also equal to f(f(f(s, x), w), a), as desired. 17. {O, 10, Il}{O, I}* 19. {omln I m � Oandn � I} 21. {A} U{O}{I}*{O} U{IO,Il}{O, I}*U{O}{I}*{OI}{O, I}*U {O}{I}*{OO}{O}*{I}{O, I}* 23. Let S2 be the only final state, and put transitions from S2 to itself on either input. Put a tran­ sition from the start state So to s, on input 0, and a transition from s, to S2 on input 1. Create state S3, and have the other transitions from So and s) (as well as both transitions from S3) lead to S3. 25. Start state So, only final state S3; transitions from So to So on 0, from So to s, on 1, from s, to S2 on 0, from s, to s) on 1, from S2 to 'so on 0, from S2 to S3 on 1, from S3 to S3 on 0, from S3 to S3 on 1 27. Have five states, with only S3 final. For i = 0, 1, 2, 3, transition from Si to itself on input 1 and to Si+' on input O. Both transitions from S4 are to itself. 29. Have four states, with only S3 final. For i = 0, 1, 2, transition from Si to Si +) on input 1 but back to So on input O. Both transitions from S3 are to itself. S-84 31. Start 0, 1 33. Start state So, only final state s); transitions from So to So on 1, from So to s) on 0, from s) to s) on 1; from s, to So on 0 35. Start -----.,� 37. Suppose that such a machine exists, with start state So and other state s). Because the empty string is not in the language but some strings are accepted, we must have s, as the only final state, with at least one transition from So to s). Because the string 0 is not in the language, the tran­ sition from So on input 0 must be to itself, so the transi­ tion from So on input 1 must be to s,. But this contradicts the fact that 1 is not in the language. 39. Change each fi­ nal state to a nonfinal state and vice versa. 41. Same ma­ chine as in Exercise 25, but with So, s" and S2 as the final states 43. {O,OI, Il} 45. {A,O} U {om In I m � 1, n � I} 47. {Ion I n � O} U {Ion 10m I n, m � O} 49. The union of the set of all strings that start with a 0 and the set of all strings that have no Os S-85 53. Add a nonfinal state S3 with transitions to S3 from So on input 0, from Sl on input 1, and from S3 on input 0 or 1. 0, 1 c) 0,1 57. Suppose that M is a finite-state automaton that accepts the set of bit strings containing an equal number of Os and I s. Suppose M has n states. Consider the string on+ II n+ I. By the pigeonhole principle, as M processes this string, it must encounter the same state more than once as it reads the first n + lOs; so let S be a state it hits at least twice. Then k Os in the input takes M from state s back to itselffor some positive integer k. But then M ends up exactly at the same place after reading on+l+k 1 n+1 as it will after reading on+II n+l. There­ fore, because M accepts on+ II n+ I it also accepts on+k+ II n+ I, which is a contradiction. 59. We know from Exercise 58d that the equivalence classes of Rk are a refinement of the equivalence classes of Rk-I for each positive integer k. The equivalence classes are finite sets, and finite sets cannot be refined indefinitely (the most refined they can be is for each Answers to Odd-Numbered Exercises S-85 equivalence class to contain just one state). Therefore this sequence of refinements must remain unchanged from some point onward. It remains to show that as soon as we have Rn = Rn+l, then Rn = Rm for all m > n, from which it fol­ lows that Rn = R*, and so the equivalence classes for these two relations will be the same. By induction, it suffices to show that if Rn = Rn+t. then Rn+1 = Rn+2. Suppose that Rn+1 =1= Rn+2• This means that there are states sand t that are (n + I)-equivalent but not (n + 2)-equivalent. Thus there is a string x oflength n + 2 such that, say, f(s, x) is final but f(t, x) is nonfinal. Write x = aw, where a E I. Then f(s, a) and f(t, a) are not (n + I)-equivalent, because w drives the first to a final state and the second to a nonfinal state. But f(s, a) and f(t, a) are n-equivalent, because sand tare (n + I)-equivalent. This contradicts the fact that Rn = Rn+l. 61. a) By the way the machine M was constructed, a string will drive M from the start state to a final state if and only if that string drives M from the start state to a final state. b) For a proof of this theorem, see a source such as Introduc­ tion to Automata Theory, Languages, and Computation (2nd Edition) by John E. Hopcroft, Rajeev Motwani, and Jeffrey D. Ullman (Addison -Wesley, 2000). Section 12.4 1. a) Any number of Is followed by a 0 b) Any number of Is followed by one or more Os c) III or 001 d) A string of any number of 1 s or OOs or some of each in a rowe) A or a string that ends with a 1 and has one or more Os before each I t) A string of length at least 3 that ends with 00 3. a) No �� �� �� �� t)� �� �� 5. a) 0 U 11 U 010 b) 000000* c) (0 U 1)«0 U 1)(0 U 1»)* d) 0*10* e) (1 U 01 U 001)* 7. a) 00*1 b) (0 U 1) (0 U 1)(0 U 1)*0000* c) 0*1* U 1*0* d) 11(111)*(00)* 9. a) Have the start state so, nonfinal, with no transitions. b) Have the start state so, final, with no trahsitions. c) Have the nonfinal start state So and a final state Sl and the transi­ tion from So to Sl on input a. 11. Use an inductive proof. If the regular expression for A is 0, A, or x, the result is triv­ ial. Otherwise, suppose the regular expression for A is BC. Then A = B C where B is the set generated by Band C is the set generated by C. By the inductive hypothesis there are regular expressions B' and C' that generate BRand C R , respective ly. Because AR = (BC)R = CR BR, C'B' is a reg­ ular expression for A R. If the regular expression for A is B U C, then the regular expression for A is B' U C' because (B U C)R = (BR) U (CR). Finally, if the regular expression for A is B* , then it is easy to see that (B')* is a regular expres­ sion for AR. 8-86 Answers to Odd-Numbered Exercises 13.a) o c) o S-86 o S-87 15. S � OA, S � IB, S � 0, A � OB, A � IB, B � OB,B� IB 17.S�Oe,s� IA,S� I,A� lA, A � Oe,A � I,B � OB,B � IB,B � O,B � I,e � oe, e � I B, e � I. 19. This follows because input that leads to a final state in the automaton corresponds uniquely to a derivation in the grammar. 21. The "only if" part is clear because I is finite. For the "if" part let the states be Sio' Sip Si2, ... , Si., where n = lex). Because n ::: lSI, some state is repeated by the pigeonhole principle. Let y be the part of x that causes the loop, so that x = uyv and y sends S j to Sj, for some j. Then uykv E L(M) for all k. Hence, L(M) is infinite. 23. Suppose that L = {02n In} were regular. Let S be the set of states of a finite-state machine recognizing this set. Let z = 02n I n where 3n ::: I S I. Then by the pump­ ing lemma, z = 02n In = uvw,l(v) ::: I, and uviw E {02nln I n ::: O}. Obviously V cannot contain both 0 and I, because v2 would then contain 10. So v is all Os or all Is, and hence, uv2w contains too many Os or too many Is, so it is not in L. This contradiction shows that L is not regular. 25. Suppose that the set of palindromes over to, I} were regular. Let S be the set of states of a finite-state machine recognizing this set. Let z = on IOn, where n > lSI. Apply the pumping lemma to get uviw E L for all nonnegative integers j where lev) ::: I, and l(uv)::::: lSI, and z = onIOn = uvw. Then v must be a string of Os (because Inl > lSI), so uv2w is not a palindrome. Hence, the set of palindromes is not regular. 27. Let z = I; then III ¢. L but 101 E L, so II and 10 are distinguishable. For the second question, the only way for Iz to be in L is for z to end with 01, and that is also the only way for lIz to be in L, so 1 and 11 are indistinguishable. 29. This follows immediately from Exercise 28, because the n distinguishable strings must drive the machine from the start state to n different states. 31. Any two distinct strings of the same length are distinguishable with respect to the language P of all palin­ dromes, because if x and y are distinct strings of length n, then xxR E P but yxR ¢. P. Because there are 2n different strings oflength n, Exercise 29 tells us that any deterministic finite-state automaton for recognizing palindromes must have at least 2n states. Because n is arbitrary, this is impossible. Section 12.5 1. a) The nonblank portion of the tape contains the string 1111 when the machine halts. b) The nonblank portion of the tape contains the string 011 when the machine halts. c) The nonblank portion of the tape contains the string 00001 when the machine halts. d) The nonblank portion of the tape contains the string 00 when the machine halts. 3. a) The machine halts (and accepts) at the blank following the input, having changed the tape from 11 to 0 1. b) The machine changes every other occurrence of a I, if any, starting with the first, to a 0, and otherwise leaves the string unchanged; it halts (and accepts) when it comes to the end of the string. 5. a) Halts with 01 on the tape, and does not accept b) The Answers to Odd-Numbered Exercises 8-87 first 1 (if any) is changed to a 0 and the others are left alone. The input is not accepted. 7. (so, 0, S\, 1, R), (so, 1, so, 1, R) 9. (so, 0, so, 0, R),(so, I, S\, 1 ,R), (S\, O,s\ ,0,R),(s\, l,s\,O,R) 11. (so,O,s\, 0, R), (so, 1, so, 0, R), (s\, 0, S\, 0, R), (s\, l,so,O,R), (s\, B, S2, B, R) 13. (so, 0, so, 0, R), (so, 1, S\, 1, R), (s\, 0, S\, 0, R), (s\, 1, so, I, R), (so, B, S2, B, R) 15. If the input string is blank or starts with a 1 the machine halts in nonfinal state so. Otherwise, the initial 0 is changed to an M and the machine skips past all the intervening Os and I s until it either comes to the end of the input string or else comes to an M. At this point, it backs up one square and enters state S2. Because the accept­ able strings must have a I at the right for each 0 at the left, there must be a I here if the string is acceptable. Therefore, the only transition out of S2 occurs when this square contains a 1. If it does, the machine replaces it with an M and makes its way back to the left; if it does not, the machine halts in nonfinal state S2. On its way back, it stays in S3 as long as it sees I s, then stays in S4 as long as it sees Os. Eventually either it encouters a 1 while in S4 at which point it halts without accepting or else it reaches the rightmost M that had been written over a 0 at the start of the string. If it is in S3 when this happens, then there are no more Os in the string, so it had better be the case that there are no more 1 s either; this is accomplished by the transitions (S3, M, ss, M, R) and (ss, M, S6, M, R), and S6 is a final state. Otherwise the machine halts in nonfinal state ss. If it is in S4 when this M is encountered, things start all over again, except now the string will have had its leftmost remain­ ing 0 and its rightmost remaining 1 replaced by Ms. So the machine moves, staying in state S4, to the leftmost remaining o and goes back into state So to repeat the process. 17. (so, B, S9, B, L), (so, 0, S\, 0, L), (s\, B, S2, E, R), (S2, M, S2, M, R), (S2, 0, S3, M, R), (S3, 0, S3, 0, R), (S3, M, S3, M, R), (S3, I, S4, M, R), (S4, 1, S4, 1. R), (S4, M, S4, M, R), (S4, 2, SS, M, R), (ss, 2, ss, 2. R), (ss, B, S6, B, L), (S6, M, Sg, M, L), (S6, 2, S7, 2, L), (S7, 0, S7, 0, L), (S7, I, S7, I, L), (S7, 2, S7, 2, L), (S7, M, S7, M, L), (S7, E, S2, E, R), (sg, M, Sg, M, L), (Sg, E, S9, E, L) where M and E are markers, with E marking the left end of the input 19. (so, 1, S\, B, R), (s\, I, S2, B, R), (S2, I, S3, B, R), (s3,I,s4, 1, R),(s\,B,S4, I,R),(s2,B, s4,l,R), (S3, B, S4, I, R) 21. (so, 1, S\, B, R), (s\, I, S2, B, R), (s\, B, S6, B, R), (S2, I. S3, B, R), (S2, B, S6, B, R), (S3. I, S4, B, R), (S3, B, S6, B. R), (S4, I, SS, B, R), (S4, B, S6, B, R), (S6, B, SIO, 1, R), (ss, 1, ss, B, R ), (ss, B. S7. 1, R), (S7, B, Sg, 1, R), (Sg, B,S9, 1, R), (S9, B. SIO, I, R) 23. (so, l,so, I,R),(so,B,s\,B,L),(s\, l, s2,0,L), (S2' 0, S2, 0, L), (S2, 1, S3, 0, R), (S2, B, S6, B, R), (S3,O,S3,0, R), (S3, I,S3, I,R),(S3,B,S4, I,R), (S4, B, SS, I. L), (ss, 1. ss, I, L), (ss. 0, S2, 0, L), (S6, 0, S6, I, R), (S6, I, S7, 1, R), (S6, B, S7, B, R) 25. (so, 0, so. 0, R), (so. *, ss, B, R), (S3, *, S3, *, L), (S3, 0, S3, 0, L), (S3, 1, S3, 1, L), (S3, B, so, B, R), S-88 Answers to Odd-Numbered Exercises (S5, 1, S5, B, R), (S5, 0, S5, B, R), (S5, B, S6, B, L), (S6, B, s6, B, L), (S6, 0, S7, 1, L), (S7, 0, S7, 1, L), (so, 1, Sh 0, R), (Sh 1, Sh 1, R), (S\, *, S2, *, R), (S2, 0, S2, 0, R), (S2, 1, S3, 0, L), (S2, B, S4, B, L), (S4' 0, S4, 1, L), (S4' *, S8, B, L), (S8, 0, S8, B, L), (s8,I, s8,B,L) 27. Suppose that Sm is the only halt state for the Turing ma': chine in Exercise 22, where m is the largest state number, and suppose that we have designed that machine so that when the machine halts the tape head is reading the leftmost 1 of the answer. Renumber each state in the machine for Exercise 18 by adding m to each subscript, and take the union of the two sets of five-tuples. 29. a) No b) Yes c) Yes d) Yes 31. (so, B, S\, I, L), (so, I,Shl,R),(ShB,SO, I,R) Suppleme ntary Exercises 1. a) S --+ 00S111, S --+ >.. b) S --+ AABS, AB --+ BA, BA --+ AB, A --+ 0, B --+ I, S --+ >.. c) S --+ ET, T --+ OT A, T --+ IT B, T --+ >..,OA --+ AO, IA --+ AI, OB --+ BO, IB --+ BI, EA --+ EO, EB --+ EI, E --+ >.. 3. 5 1 A 1 B /1\ ( A ) I B /\ ( ) 5. /5\ o 5 /\ 5 0 I o 5 /\ 5 0 /\ 5 0 I o 7. No, take A = {I, 10} and B = {O, OO}. 9. No, take A = {OO, 000, OOOOO}andB = {OO,OOO}. 11. a) I b) 1 c)2 d) 3 e)2 t) 4 S-88 13. 1, 1 0,0 0,0 0,0 0,1 0 0 0 0 0 15. 19. S-89 21. a) b) c) 23. Construct the deterministic finite automaton for A with states S and final states F. For if use the same automaton but with final states S -F. b) Answers to Odd-Numbered Exercises S-89 0, I 27. Suppose that L = {I Pip is prime} is regular, and let S be the set of states in a finite-state automaton recognizing L. Let z = IP where p is a prime with p > lSI (such a prime exists because there are infinitely many primes). By the pumping lemma it must be possible to write z = uvw with l(uv) � lSI, l(v)::: 1, and for all nonnegative inte­ gers i, uvi W E L. Because z is a string of all Is, u = I a , V = Ib, and w = IC, where a + b + c = p, a + b � n, and b> 1. This means that uviw = lalbilc = l(a+b+c)+b(i-l) = F+b(i-l). Now take i = P + 1. Then uviw = F(l+b). Be­ cause p(1 + b) is not prime, uviw rf. L, which is a contradic­ tion. 29. (so, *, ss, B, L), (so. o. so. o. R), (so. 1. Sl, 0, R), (Sl. *. S2. *. R), (Sl. I, S" I, R), (S2, O. S2, 0, R), (S2. 1. S3, o. L), (S2, B. S4, B, L), (S3, *, S3, *, L), (S3. O. S3, o. L), (S3. 1. S3. 1. L), (S3. B. so. B. R), (S4, *, SS, B, L), (S4, O. S4. B. L), (ss. 0, ss, B, L), (ss. B, S6, B, R), (S6, 0, S7, I, R), (S6, B, S6, B, R), (S7, 0, S7, I, R), (s7,I,s7, I, R), (ss, 0, ss, I, L), (ss, I, ss,I,L) APPENDIXES Appendix l 1. Suppose that I' is also a multiplica tive identity for the real numbers. Then, by definition, we have both I . I' = I and 1 . I' = I', so I' = 1. 3. For the first part, it suffices to show that [(-x) · y] + (x· y) = 0, because Theorem 2 guarantees S-90 Answers to Odd-Numbered Exercises that additive inverses are unique. Thus [(-x) . y] + (x· y) = ( -x + x) . y (by the distributive law) = 0 . y (by the inverse law) = y. 0 (by the commutative law) = 0 (by Theorem 5). The second part is almost identical. 5. It suffices to show that [(-x)· (-y)] + [-(x · y)] = 0, becauseTheorem 2guar­ antees that additive inverses are unique: [(-x) · (-y)] + [-(x · y)] = [(-x)· (-y)] + [(-x)· y] (by Exercise 3) = (-x)· [(-y) + y] (by the distributive law) = (-x)· 0 (by the inverse law) = 0 (by Theorem 5). 7. By definition, -(-x) is the additive inverse of -x. But -x is the additive in­ verse of x, so x is the additive inverse of -x. Therefore -(-x) = x by Theorem 2. 9. It suffices to show that (-x -y) + (x + y) = 0, because Theorem 2 guarante es that additive inverses are unique: (-x -y) + (x + y) = [(-x) + (-y)] + (x+y) (by definition of subtraction) = [(-y) + (-x)] + (x+y) (by the commutative law) = (-y) + [(-x) + (x + y)] (by the associative law) = (-y) + [(-x+x)+y] (by the associative law) = (-y)+(O+y) (by the inverse law) = (-y) + Y (by the identity law) = 0 (by the inverse law). 11. By definition of division and uniqueness of multiplica tive inverses (Theorem 4) it suf­ fices to prove that [(w/x)+(y/z)]·(x·z)=w·z+x·y . But this follows after several steps, using the distributive law, the associative and commutative laws for multiplication, and the definition that division is the same as multiplication by the inverse. 13. We must show that if x > 0 and y > 0, then x . y > O. By the multiplicative compatibility law, the commutative law, and Theorem 5, we have x . y > 0 . y = O. 15. First note that ifz < O,then-z > O(add-ztobothsides of the hypothesis). Now given x > y and -z > 0, we have x . (-z) > y . ( -z) by the multiplicative compatibility law. But by Exercise 3 this is equivalent to -(x · z) > -(y . z). Then add x . z and y . z to both sides and apply the various laws in the obvious ways to yield x . z < y . z. 17. The ad­ ditive compatibility law tells us that w + y < x + y and (to­ gether with the commutative law) that x + y < x + z. By the transitivity law, this gives the desired conclusion. 19. By Theorem 8, applied to l/x in place of x, there is an inte­ ger n (necessarily positive, because l/x is positive) such that n > l/x. By the multiplicative compatibility law, this means that n . x > 1. 21. We must show that if (w, x) '" (w', x') and(y, z) '" (y', Zl), then(w + y, x + z) '" (w' + y', x' + Zl) and that (w· y +x ·Z,X· Y +w ·z) '" (W'·y' +X'·Z',X'· y' + w'· Zl). Thus we are given that w + x' = x + w' and that y +Z' = z + y',andwewanttoshowthatw + y +X' +Z' = X + z + w' + y' and that w . y + x . z + x' . y' + w' . Zl = X • Y + w . z + w' . y' + x' . z'.Forthefirstofthedesiredcon­ clusions, add the two given equations. For the second, rewrite the given equations as w - x = w' -x' and y - z = y' -Zl, multiply them, and do the algebra. Appendix 2 1. a)23 b) 26 c) 24 3. a) 2y b) 2y /3 c) y /2 5. 10 9 8 7 6 5 4 3 2 S-90 y=OY o 2 3 2 2 3 4 5 Appendix 3 1. After the first block is executed, a has been assigned the original value of b and b has been assigned the original value of c, whereas after the second block is executed, b is assigned the original value of c and a the original value of c as well. 3. The following while construction does the same thing. i := initial value while i � final value begin statement i := i + 1 end CHAPTER 1 CHAPTER 2 CHAPTER 3 CHAPTER 4 CHAPTER S CHAPTER 6 CHAPTER 7 CHAPTER S CHAPTER 9 CHAPTER 10 CHAPTER 11 CHAPTER 12 APPENDIX: Photo Credits Page 2: © National Library of Medicine; p. 5: Library of Congress; p. 14: Courtesy Indiana University Archives; p. 16: University Archives. Department of Rare Books and Special Collections. Princeton University Library; p. 25: By pennission of the London Mathematical Society; p. 27: © Mary Evans Picture Library; p. 28: Henry Maurice Sheffer (1883-1964) by Mabel Lisle Ducasse. Courtesy ofthe Harvard University Portrait Collection, Department of Philosophy, CNAl. Photo by Katya Kallsen; p. 32: © BettmannlCorbis Images; p. 44: © Granger Collection; p. 93: © Master and Fellows of Trinity College, Cambridge; p. 94: © Master and Fellows of Trinity College Cambridge. Page 113: Library of Congress; p. 114: Library of Congress; p. 115: By pennission of the President and Council of the Royal Society; p. 117: © Scientists and Inventors Portrait File Collection, Archives Center, National Museum of American History; p. 154: Courtesy Neil Sloane and AT&T Shannon Lab. Page 168: © from THE SCIENTISTS OF THE ANCIENT WORLD, by Anderson/Stephenson. pg. 90. Illustra­ tion by Marie Ie Glatin Keis; p. 182: Bachmann, Paul. Die Arithmetik Der Quadratischen Fonnen. Verlag und Druck von BG Teubner. Leipzig. Berlin. 1923; p. 183: Smith Collection, Rare Book and Manuscript Library, Columbia University; p. 184: © Stanford University News Service; p. 204: Smithsonian Institution. Photo No. 46, 834-M; p. 213: © Mary Evans Picture Library; p. 228: Scientists and Inventors Portrait File Collection, Archives Center, National Museum of American History; p. 239: From A History of Science, Technology and Philosophy in the 16th and 17th Centuries, by Abraham Wolf. Reprinted by pennission of Harper Collins Publishers Lts. © Abraham Wolf; p. 242, top: Courtesy Ronald L. Rivest; p. 242, middle: © Kristen Tsolis; p. 242, bottom: © Courtesy Leonard Adleman. Page 298: © Granger Collection; p. 299: © Mary Evans Picture Library; p. 324: Courtesy Tony Hoare; p. 332: © Matthew Naythons/TimePixlGetty. Page 348: © Granger Collection; p. 352: Courtesy Mrs. Jane Burch; p. 366: © National Library of Medicine. Page 394 : FromA Concise History of Mathematics by Dirk Struik, 1967, Dover Publications; p. 407: © Granger Collection; p. 420: Courtesy Stephen Stigler; p. 438: © Smith Collection, Rare Book and Manuscript Library, Columbia University. Page 508: From Math Tutor Archive. Page 551: Courtesy Stephen Warshall; p. 571: © Deutsches Museum; p. 584: By pennission of the President and Council ofthe Royal Society. Page 635: © Scientists and Inventors Portrait File Collection, Archives Center, National Museum of American History; p. 640: Master and Fellows of Trinity College Cambridge; p. 646: Julius Petersen: Metoder og Teorier. Edition no. 6, Det Schoenbergske Forlag, Copenhagen 1912. Photo courtesy Bjarne Toft; p. 650: Courtesy, Edsger W. Dijkstra; p. 663: Courtesy, the Archive of Polish Mathemati cians, at the Institute of Mathematics of the Polish Academy of Sciences. Reproduced with pennission of the Institute; p. 668: By pennission of the London Mathematical Society. Page 688: © Master and Fellows of Trinity College Cambridge; p. 702: Courtesy, California State University, Santa Cruz; p. 721: Courtesy, the Archive of Polish Mathemati cians, at the Institute of Mathematics of the Polish Academy of Sciences. Reproduced with pennission of the Institute; p. 738: Courtesy Robert Clay Prim; p. 739: Courtesy, Joseph B. Kruskal. Photo by Rachel Kruskal. Page 750: Courtesy MIT Museum; p. 768: Courtesy Maurice Karnaugh; p. 775: Courtesy Edward J. McCluskey; p. 776: Courtesy the Quine Family. Page 791: © Donna CoveneylMIT; p. 792, top: Courtesy IBM, Almaden Research Center; p. 792, bottom: Courtesy Peter Naur; p. 805: From: I Have a Photogra phic Memory by Paul Halmos (1987), American Mathe­ matical Society; p. 811: © BettmannlCor bis; p. 826: © Science Photo LibrarylPhoto Researchers, Inc. p. 836: © University Archives. Department of Rare Books and Special Collections. Princeton University Library. A4 Appendix 1: © Hulton-Deutsch Collection/Corbis. Index of Biographies Ada, Augusta (Countess of Lovelace), 27 Adleman, Leonard, 242 Archimedes, A-4 Aristotle, 2 Bachmann, Paul Gustav Heinrich, 182 Backus, John, 792 Bayes, Thomas, 420 Bernoulli, James, 407 Boole, George, S Cantor, Georg, 113 Carmichael, Robert Daniel, 240 Carroll, Lewis (Charles Dodgson), 44 Cayley, Arthur, 688 Chebys hev, Pafnuty Lvovich, 438 Chomsky, Avram Noam, 791 Church, Alonzo, 836 De Morgan, Augustus, 25 Descartes, Rene, 117 Dijkstra, Edsger Wybe, 650 Dirichlet, G. Lejeune, 348 Dodgson, Charles (Lewis Carroll), 44 Eratosthenes, 508 Erdos, Paul, 584 Euclid, 228 Euler, Leonhard, 214, 635 Fermat, Pierre de, 239 Fibonacci, 298 Gauss, Karl Friedrich, 204 Goldbach, Christian, 215 Hamilton, William Rowan, 640 Hardy, Godfrey Harold, 93 Hasse, Helmut, 571 Hoare, C. Anthony R., 324 Hopper, Grace Brewster Murray, 811 Huffman, David A., 702 Karnaugh, Maurice, 768 1/-/ Kempe, Alfred Bray, 668 al-Khowarizmi, Abu Ja'far Moh ammed ibn Musa, 168 Kleene, Stephen Cole, 805 Knuth, Donald E., 184 Kruskal, Joseph Bernard, 739 Kuratowski, Kazimierz, 663 Lame, Gabriel, 299 Landau, Edmund, 183 Laplace, Pierre Simon, 394 Lukasiewicz, Jan, 721 McCarthy, John, 332 McClu skey, Edward J., 775 Mersenne, Marin, 213 Naur, Peter, 792 Pascal, Blaise, 366 Peirce, Charles Sanders, 32 Petersen, Julius Peter Christian, 646 Prim, Robert Clay, 738 Quine, Willard van Orman, 776 Ramanu jan, Srinivasa, 94 Ramsey, Frank Plwnpton, 352 Rivest, Ronald, 242 Russell, Bertrand, 114 Shamir, Adi, 242 Shannon, Claude Elwood, 750 Sheffer, Henry Maurice, 28 Sloane, Neil, 154 Smullyan, Raymond, 14 Stirling, James, 145 Tukey, John Wilder, 16 Turing, Alan Mathison, 176, 826 Vandermonde, Alexandre- Tbeophile, 368 Venn, John, 115 Warshall, Stephen, 551 1-1 Index Absorption laws for Boolean algebra, 753, 754 for lattices, 585 for propositions, 24 for sets, 124 Abstract definition of Boolean algebra, 755 Academic papers, 593 Academy, Plato's, 2 Acceptance, deferred, 179 Accepted language, 806 Accepted strings, 806 Ackermann, Wilhelm, 310 Ackermann's function, 310 Acquaintanceship graph, 592-593 paths in, 623 Actors, 593 Ada, Augusta (Countess of Lovelace), 25, 27 Adapting existing proofs, 96 Adders, 764-765 full, 764, 781 half, 764, 781 Addition of functions, 135-136 of geometric progressions, 270--271 of integers, 222-224 of matrices, 247-248 of multi sets, 132 of subsets, 732 Addition rule of inference, 66 Additive Compatibility Law, A-2 Address system, universal, 711 Adjacency list, 611 Adjacency matrices, 612--614, 676 counting paths between vertices by, 628--629 Adjacent cells, 768 Adjacent vertices, 598, 600, 675 Adjective, 786 Adleman, Leonard, 241, 242 Adverb,786 Affine transformation, 208 Airline system, weighted graphs modeling, 648 Alcuin of York, 632 Aleph null, 158 Alexander the Great, 2 Algebra, Boolean, 5, 28 abstract definition of, 755 definition of, 781 identities of, 752-754, 755 ALGOL, 792 ALGOL 60, 792 Algorithm(s), 167-177 approximation, 655 for base b expansion, 219, 221 for binary search tree, 695-707 for Boolean product of zero-one matrices, 252-254 bubble sort, 173 change-making, 174-176 for coloring graphs, 671, 674 complexity of, 193-199 1-2 average-case, 195, 430--433 of binary search algorithms, 194-195 of breadth-first search algorithms, 731 of bubble sort, 194-195 computational, 193 ofDijkstra's algorithm, 653 constant, 197 of depth-first search algorithms, 729 exponential, 197 factorial, 197 of finding maximum element of set, 194 of insertion sort, 196 linear, 197 of linear search algorithms, 194 logarithmic, 197 of merge sort, 479 polynomial, 197 of searching algorithms, 194-195, 197 of sorting algorithms, 195-196 space, 193 time, 193-196 worst-case, 194, 195-196 computer time used by, 198-199 for computing div and mod, 225-226 deferred acceptance, 179,293 definition of, 168 Dijkstra's, 649--653, 676 divide-and-conquer, 474, 514 division, 202-203, 257 estimating number of operations of, 180--182 Euclidean, 227-229, 258,298 extended, 232, 246 for Euler circuits, 636--637 for Euler paths, 637--638 for evaluating polynomials, 199 for fast integer multiplication, 475-476 for fast matrix multiplication, 475 for Fibonacci numbers, 316--317 for finding maximum element in sequence, 169 for finding minimum element in sequence, 259 Fleury's, 637, 646 Floyd's, 656--657 for generating combinations, 384-385 for generating permutations, 382-384 greed� 174-176,275-276, 703,738,744 history of word, 168 Huffinan coding, 702-703 inorder traversal, 718 insertion sort, 174 for integer addition, 222-224 for integer multiplication, 224-226 for integer operations, 222-226 integers and, 219-229 iterative, for Fibonacci numbers, 317 Kruskal's, 739-740, 744 for matrix multiplication, 249-250 merge sort, 317-321 for minimum spanning trees, 738-74 1 for modular exponentiation, 226 Monte Carlo, 411-413 NAUTY, 618 optimal, 200 parallel, 606 postorder traversal, 718 preorder traversal, 716 Prim�, 738-739, 744 probabilistic, 393, 411-413, 442 properties of, 169 recursive, 311-321, 329 binary search, 314 for computing aU, 312 for computing greatest common divisor, 313 correctness of, 315-317 for factorials, 197 for Fibonacci numbers, 316 linear search, 314 modular exponentiation, 313 searching, 170, 257 binary, 171,257,474 Boolean, 13 breadth-first, 729-73 1, 744 complexity of, 194-195, 197 depth-first, 726--729, 744 applications with, 727-729 in directed graphs, 732-734 linear, 170, 256, 257 average-case complexity of, 431-432 complexity of, 194, 197 recursive sequential, 314 ternary, 178 Web page, 13 serial,606 shortest-path, 649--653 Sollin's, 743 sorting, 172-174 complexity of, 195-196 topological, 576--578, 582 for spanning trees, 738-74 1 topological sorting, 577 for transitive closure, 550 traversa l,712-719 Warshall's, 550--553, 582 Alice in Wonderland (Carroll), 44 Alpha-beta pruning, 707, 748 Alphabet, 787, 838 Alphanumeric character, 340 Ambiguous grammar, 840 Analytic Engine, 27 Ancestors of vertex, 686, 743 AND, 13, 15-16 AND gate, 761, 781 Antecedent, 6 Antichain, 585 Antisymmetric relation, 523-524, 581 representing using digraphs, 542 using matrices, 538-539 Appel, Kenneth, 668, 669 Application( s) of backtracking, 731-732 of Bayes' Theorem, 419-421 of congruences, 205-207 of graph theory, 592-595, 604-607, 623--625, 627,638,643,671--672 of graphs colorings, 671--672 of inclusion-exclusion, 505-512 of number theory, 231-244 of pigeonhole principle, 351-353 of trees, 695-707 Approximation algorithm, 655 Archimedean property, A-5 Archimedes, A-4 Archimedes' screw, A-4 Argument, 44, 63 Cantor diagonalization, 160 form, 64 valid, 64 Aristotle, 2 Arithmetic computer, with large integers, 237-238 Fundamental Theorem of, 211, 257, 285-286 matrix, 247-248 modular, 203-205 Arithmetic mean, 95, 282 Arithmetic progression, 151 Arithmeticorum Libri Duo (Maurolico), 265 /2-/ 12-2 Array linear, 606, 607 two-dimensional, 606 Arrow, Peirce, 29 Ars Conjectandi (Bernoulli), 407 Art Gallery problem, 675 Theorem, 675 Art of Computer Programming, The (Knuth), 172 Article, 786 Articulation points, 625 Artificial intelligence, 332 fuzzy logic in, 19 fuzzy sets in, 132-133 Assertion final, 323, 329 initial, 323, 329 Assignment of jobs, 513 Assignment, stable, 179, 293 optimal, 293 Assignment statements, A-IO -A-II Assignments, frequency, 672 Associated homogenous recurrence relations, 467 Associative laws for addition, A-I for Boolean algebra, 753, 755 for lattices, 585 for multiplication, A-I for propositions, 24 for sets, 124 Asymmetric relation, 528 Asymptotic functions, 146, 192 Asynchronous transfer mode (ATM), 144 ATM cells, 144 AT&T network, 625 Automated theorem proving, 108 Automaton finite-state, 805-811 deterministic, 807-811, 839 nondeterministic, 811-814, 820-82 1, 822,839 regular sets recognized by, 819-821 set not recognized by, 824-825 linear bounded, 825 pushdown, 825 quotient, 817 Average-case complexity of algorithms, 195, 430-433 Avian influenza, 424 AVL-trees, 748 Axiom(s), 75, A-I -A-5 Completeness, A-2 field,A-I-A-2 of Mathematical Induction, A-5 order,A-2 in proving basic facts, A-2 -A-5 for real numbers, A-I -A-2 for set of positive integers, A-5 Babbage, Charles, 27 Bachmann, Paul Gustav Heinrich, 182 Back edges, 728 Backtracking, 726-729, 744 applications of, 731-732 in directed graphs, 733 Backus, John, 792 Backus-Naur form (BNF), 792-793, 839 Backward differenc es, 460 Backward reasoning, 94 Bacon, �in, 623-624 Bacon number, 623 Bacteria, 457 Balanced rooted trees, 691, 743 Balanced strings of parentheses, 332 Balanced ternary expansion, 230 Bandwidth of graph, 680 Bar chocolate, 292 Ziegler's Giant, 184 Barber paradox, 19-20 Baseb Miller's test for, 245 strong pseudoprime to, 245 Base b expansion, 219, 221 Base conversion, 220 BASIC,340 Basis step, 264, 284, 299, 328, 690, 706 Basis, vertex, 632 Basketba ll,382 Bayes, Thornas, 418,420 Bayes' Theorem, 417-425 generalized, 420 Bayesian spam filters, 421-423 Beaver, busy, 835 Begging the question, 84 Begin statements, A-II, A-14 Bell, E. T., 566 Bell numbers, 566 Bernoulli family tree, 684 Bernoulli, James, 406, 407 Bernoulli trial, 428, 442 and binomial distribution, 406-408 mutually independent, 378 Bernoulli's inequality, 280 Biconditional, 9-10 logical equivalences for, 25 Bicycle racers, 424 Bidirectional bubble sort, 173 Big-O notation, 180-186 Big-Omega notation, 182, 189-190 Big-Theta notation, 182, 189-190 Bigit,I6 Bijective function, 138, 163 Binary coded decimal, 231 Binary coded decimal expansion, 774 Binary digit origin of term, 15 Binary expansion, 219 Binary insertion sort, 172, 174, 179 Binary relation, 519, 581 Binary search algorithm, 171,257,474 complexity of, 194-195, 197 recursive, 314 Binary search trees, 695-698, 743 Binary trees, 302-304, 686, 687 definition of, 743 extended, 302 full,304-305 height of, 306 number of vertices of, 306 with prefix code, 700-70 I Binding variables, 38-39 Binit, 16 Binomial coefficients, 357, 363-368, 387 extended, 486 Binomial distribution, 406-408 Binomial expression, 363 Binomial Theorem, 363-365, 387 extended, 486 Binomial trees, 745 Bipartite graphs, 602-604, 675 coloring of, 603 Bird fiu, 424 Birthday problem, 409-41 1 Bit, 104 origin of term, 15, 16 Bit operations, 15, 104 Bit strings, 15, 104, 129-130 with an odd number of I s and ending with at least two Os, 809 counting, 336 without consecutive Os, 454-455 decoding, 700-70 I generating next largest, 385 in interstellar communication, 424 length of, 15,454-455 Bitwise operators, 15-16, 104 Blocks of statements, A-I I Index 1-3 BNF (Backus-Naur form), 792-793, 839 Boat, 632 Bonferroni's inequality, 415 Book number of graph, 682 Boole, George, 3, 5, 420, 749 Boolean algebra, 5, 28, 752-755 abstract definition of, 755 definition of, 781 identities of, 752-754, 755 Boolean expressions, 750-752, 781 dnal of, 754, 781 Boolean functions, 749-755, 781 dual of, 754, 781 functionally complete set of operators for, 759 implicant of, 770 minimization of, 766-779, 781 representing, 757-759 self-dual .. 782 threshold, 783 Boolean power (ofzero-one matrices), 252-254 Boolean product, 749-750, 751, 757-759, 781 ofzem-one matrices, 252-254, 257 Boolean searches, 13 Boolean sum, 749-750, 751, 757-759, 781 Boolean variable, 15, 104,750,758,781 Boole's inequality, 415 Boruvka, C»akar, 740 Bots, 734 Bottom-up parsing, 791 Bound greatest lower, 582 of po set, 574 least upper, 582, A-2 of po set, 574 lower oflattice, 586 of po set, 574, 582 upper,A-2 oflattice, 586 of poset, 574, 582 Bound variable, 38-39 Bounded lattices, 586 Boxes, distributing objects into, 376-379 distinguishable, 376 indistinguishable, 376 Breadth-first search, 729-73 1, 744 Bridge, in graphs, 625 Bridge problem, Konigsberg, 633-634, 636, 638 B-tree of degree k, 745 Bubble sort, 173, 257 bidirectional, 173, 259 variant of, 173,219 worst-case complexity of, 194-195 Bug, computer, 811 Buoyancy, principle of, A-4 Busy beaver function, 835 Butane, 688 Bytes, 220 C programming language, 346, 559 Cabbage, 632 Cactus, 746 CAD programs, 772 Caesar, Julius, 207 Caesar cipher, 207-208 Calculus predicate, 31 propositional, 2 Call graphs, 594 connected components of, 625 Canterbury Puzzles, The (Dudeney), 454 Cantor diagonalization argument, 160 Cantor digits, 386 Cantor expansion, 231, 386 Cantor, Georg, Ill, 113, 160 Card hands, 358-359, 376-377 Cardinality aleph null, 158 of set, 113, 116, 158-160 1-4 Index Carmichael, Robert Daniel, 240 Carmichael numbers, 240-24 1, 257 Carroll, Lewis (Charles Dodgson), 44-45, 50 Carry, 222 Cartesian products, 117-118 Cases, proofs by, 86-90, 105 Cash box, 372-373 Catalan numbers, 456 Caterpillar, 746 Cayley, Arthur, 683, 688 Ceiling function, 143-145, 163 Celebrity problem, 282 Cells, adjacent, 768 Center, 695 Chain, 585 Chain letter, 691 Change-making algorithm, 174-176 Characteristic equation, 461 Characteristic function, 148 Characteristic roots, 461 Chebyshev, Pafuuty Lvovich, 438 Chebyshev's inequali ty, 438-439, 442 Checkerboard, tiling of, 97-102, 277, 283 Cheese, in Reve's puzzle, 454 Chen, J. R., 215 Chess, game tree for, 707 Chessboard knight's tour on, 647 n-queens problem on, 731-732 squares controlled by queen on, 679 Chi (Greek letter), 667 Chickens, 680 Child of vertex, 686, 743 Chinese meal, preparing, 585 Chinese postman problem, 638, 682 Chinese Remainder Theorem, 235-237, 258 Chocolate bar, 292 Chomp, 91, 292, 586 Chomsky, Avram Noam, 785, 789, 790 Chomsky classification of grammars, 789-790 Chromatic number, 668, 676 edge, 674 of graph, 667 k-critical ,674 Chung-Wu Ho, 290 Church, Alonzo, 833 Church-Turing thesis, 833 Cipher affine, 208 Caesar, 207-208 RSA, 241-244 shift,208 Circuits combinationa l,761-763 depth of, 766 examples of, 763-764 minimization of, 766-779 in directed graph, 546, 582 Euler, 633-638, 676 in graph, 622 Hamilton, 638-643, 676 multiple output, 764 simple, 622 Circular reasoning, 84, 105 Circular relation, 584 Civil War, 32 Class congruence, 204 equivalence, 558-559, 582 definition of, 558 and partitions, 559-562 representative of, 558 NP, 197 P, 197 Class A addresses, 341 Class B addresses, 341 Class C addresses, 341 Class D addresses, 341 Class E addresses, 341 Clauses, 68 Clique, 678 Closed walk, 622 Closest-Pair Problem, 479-482 Closure, Kieene, 805, 817 Closure laws for addition, A-I for multiplication, A-I Closures of relations, 544-553, 582 reflexive, 544, 582 symmetric, 545,582 transitive, 544, 547-550, 582 computing, 550-553 Coast Survey, U. S., 32 COBOL, 792, 811 Codes, Gray, 642-643 Codes, prefix, 700-703, 743 Codeword enumeration, 455-456 Coding, Huffman, 701-703, 744 Codomain of function, 134, 149, 163 Coefficient( s) binomial, 357, 363-368, 387 extended, 486 constant linear homogenous recurrent relations with, 460,461-467,514 linear nonhomogenous recurrent relations �th,467-471,514 multinomial, 382 Coins, change using fewest, 175-176 Collaboration graphs, 593-594 paths in, 623 Collatz problem, 101-102 Collision, 206 in hashing functions, probability of, 410-41 1 Coloring of bipartitl' graphs, 603 chromatic number in, 667 of graphs, 666-672, 676 of maps, 666-667 Column of matrix, 247 Combinations, 357-360 ofevenm,396-398,403-404 generating, 384-385 linear, 232 with repetition, 371-375 Combinational circuits, 761-763 depth of, 766 examples of, 763-764 minimization of, 766-779 Combinatorial identities, 364-368 Combinatorial proof, 359, 386 Combinatorics, 335, 352, 378, 386 Comments in pseudocode, A-I I -A-12 Common difference, 151 Common divisor, greatest, 215-217 Common errors in exhaustive proofs, 90 in proof by cases, 90 Common multiple, least, 216, 217 Common ratio, 150 Commutative laws for addition, A-I for Boolean algebra, 753, 755 for lattices, 585 for multiplication, A-I for propositions, 26 forsem, 124 Comparable elements in poset, 567, 582 Compatible total ordering, 576, 582 Compatibility laws, A-2 Compilers, 559, 810 Complement, 749, 781 of Boolean function, 751 double, law of, in Boolean algebra, 753, 755, 757 of fuzzy set, 132 one's, 230 of set, 123, 163 two's, 230-231 Complement laws, for sets, 124 Complementary event, 396 Complementary graph, 611 Complementary relation, 528 Complementation law, for sets, 124 Complemented lattice, 586, 755 Complete bipartite graph, 604, 675 Complete graphs, 601, 675 Complete induction, 284 Complete m-ary tree, 694 Complete m-partite graph, 678 Complexity of algorithms, 193-199 average-case, 109,430-433 of binary search algorithms, 194-195 of breadth-first search algorithms, 731 of bubble sort, 194-195 computational, 193 ofDijkstra's algorithm, 653 constant, 197 of depth-first search algorithms, 729 exponential, 197 factorial, 197 of finding maximum element of set, 194 of insertion sort, 196 linear, 197 of linear search algorithms, 194 logarithmic, 197 of merge sort, 479 polynomial, 197 of searching algorithms, 194-195, 197 of sorting algorithms, 195-196 space, 193 time, 193-196 worst-case, 194, 195-196 Complexity of merge sort, 479 Components of graphs, connected, 625-626 strongly, 627, 676 Composite integers, 189-190, 210, 211, 217,257 Composite key, 532, 582 Composite of relations, 526-527, 581 Composition rule, 324 Compositions of functions, 139-141, 163 Compound interest, 451 12-3 Compound propositions, 3,10-11,21,22,104 consistent, 104 disjunctive normal form of, 29 dual of, 29 equivalences of, in Boolean algebra, 750 satisfiable, 30 well-formed formula for, 301 Computable function, 163,835 Computation, models of, 785-837 Computational complexity of algorithms, 193 ofDijkstra's algorithm, 653 Turing machine in making precise, 833 Computational geometry, 288-290, 479-48 1 Computer arithmetic, with large integers, 237-238 Computer debugging, 811 Computer file systems, 689 Computer network, 590 with diagnostic lines, 591 interconnection networks, 606-607 local area networks, 605-606 multicasting over, 726 with mUltiple lines, 590 with multiple one-way lines, 592 �th one-way lines, 591 Computer programming, 792 Computer programming languages, 68, 135 Computer representation, of sets, 129-130 Computer science, 792 Computer time used by algorithms, 198-199 Computer virus transmission, 389 Computer-aided design (CAD) programs, 772 Concatenation, 300, 804, 839 12-4 Conclusion, 6, 44, 64 fallacy of affirming, 69 Concurrent processing, 595 Conditional constructions, A-12 -A-13 Conditional probability, 400, 404-405, 442 Conditional statement, 6 for program correctness, 324-326 Conditions don�care. 774-775 initial, 450, 513 Congruence class, 204 Congruence modulo m. 203-204, 556, 558, 582 inverse of, 234 Congruences, 205-207 linear, 234-235, 257 Conjecture, 75, 105 3x + I, \01-\02 and counterexam ples, 96-97 Frame's, 460 Goldbach's, 214-215 and proof, 96-97 twin prime, 215 Co�unction, 4-5, 104,781 distributive law of disjunction over, 23 Conjunction rule of inference, 66 Conjunctive normal form, 758 Connected components of graphs, 625-626 strongly, 627, 676 Connected graphs, 621-629 directed, 626-627 planar simple, 659-663 strongly, 626 undirected, 624-626 weakly, 626 Connecting vertices, 598 Connectives, 4 Connectivity relation, 547, 582 Consecutive composite integers, 217 Consequence, 6 Consistency, of system specifications, 12 Consistent compound propositions, 104 Constant coefficients linear homogenous recurrent relations with, 460,461-467,513 linear nonhomogenous recurrent relations with, 467-471,514 Constant complexity of algorithms, 197 Construction of the real numbers, A-5 Constructions conditional, A-12 -A-13 loop, A-13 -A-14 Constructive existence proof, 91, 105 Contain, 112 Context-free grammars, 789, 839 Context-free language, 789 Context-sensitive grammars, 789 Contingency, 21, 104 Contradiction, 21, 80 proofs by, 80-83 Contrapositive, of implication, 8 Converse of directed graph, 611 of implication, 8, 104 Convex polygon, 288 Cookie, 373 Corollary, 75, 105 Correctness of programs, 322-328, 329 conditional statements for, 324-326 loop invariants for, 326-327 partial, 323 program verification for, 323 rules of inference for, 323-324 of recursive algorithms, 315-316 Correspondences, one-to-one, 136, 160 Countability, of rational numbers, 158, 163,218 Countable set, 158 Counterexamp les, 34, 83 conjecture and, 96-97 Counting, 335-391, 449-518 basic principles of, 335-340 bit strings, 336 without consecutive Os, 454-455 Boolean functions, 751 combinations, 357-360 derangements, 510, 514 by distributing objects into boxes, 376-379 functions, 336-337 generating functions for, 488-493 Internet addresses, 341 one-to-one functions, 337 onto functions, 509-510, 514 passwords, 340 paths between vertices, 628-629 permutations, 355-357 pigeonhole principle and, 347-353 reflexive relations, 524 relations, 522 with repetition, 370-371 subsets of finite set, 338 telephone numbers, 337 tree diagrams for, 343-344 variable names, 340 Countrapositive, of implication, 104 Course in Pure Mathematics, A (Hardy),93 Covariance, 442 Covering relation, 579 Covers, 579 CPM (Critical Path Method), 587 Crawlers, 734 Cricket, 93 Critical Path Method (CPM), 587 Crossing number, 666 Cryptography, 207-208 private key, 241 public key, 231, 241-244 Cunningham numbers, 214 Cut edge, 625 Cut set of graph, 746 Cut vertices, 625 Cycle in directed graph, 546, 582 with n vertices, 60 I Cylinder, 769 Czekanowski, Jan, 740 Data compression, 701 Database composite key of, 532 intension of, 531 primary key of, 531 records in, 531 relational model of, 531-532, 581 Database query language, 535-536 Datagrams, 346-347 Datalogy, 792 Datatype, 113 de Bruijn sequences, 682 de Mere, Chevalier, 400 De Morgan, Auguatus, 22, 25, 668, 669 De Morgan's laws for Boolean algebra, 753, 757 for propositions, 22, 25-26 proving by mathematical induction, 274-275 for quantifiers, 40-41 for sets, 124-126 Debugging, 811 Decimal, binary coded, 231 Decimal notation, 168,219 Decision problems, 834 Decision trees, 698-700, 743 Decreasing function, 137 Decryption, 208,231,257 RSA, 243 Deductive reasoning, 264 Deep-Blue, 707, 748 Deferred acceptance algorithm, 179, 293 Definition domain of, 149 recursive, 263, 294-308 Degree Index 1-5 of linear homogenous recurrence relations, 461 of membership, 132 of n-ary relations, 530 of region, 661 of vertex in undirected graph, 598 Degree-constrained spanning tree, 746 Delay machine, 799-800 Dense graph,614 poset, 581 Dependency notation, 784 Depth of combinatorial circuit, 766 Depth-first search, 726-729, 744 applications with, 732-734 in directed graphs, 732-734 Derangements, 510-512, 514 number of, 510, 514 Derivable form, 787 Derivation, 787 Derivation trees, 790-792, 839 Descartes, Rene, 117 Descendants of vertex, 686, 743 Detachment, law of, 65 Deterministic finite-state automata, . 807-811,839 Deviation, standard, 436 Devil's pair, 621 Diagnostic test results, 419-420 Diagonal matrix, 255 Diagonal of a polygon, 288 Diagonal relation, 545 Diagonalization argument, 160 Diagrams Hasse, 571-572,574,575,582 state, for finite-state machine, 798 tree, 343-344, 386 Venn, 114, 115, 122, 123, 163 Diameter of graph, 680 Dice, 394 Dictionary ordering, 382 Die, 416, 436-437 dodecahedral, 443 octahedral, 443 Difference, A-6 backward, 460 common, 151 forward, 516 of multisets, 132 of sets, 123, 163 symmetric, 131, 163 Difference equation, 460 Digits binary origin of term, 15, 16 Cantor, 386 Digraphs, 582, 591, 675 circuit (cycle) in, 546, 582 connectedness in, 626-627 converse of, 611 depth-first search in, 732-734 edges of, 591, 600-601 Euler circuit of, 634 loops in, 541, 582 paths in, 546-547, 582, 676 representing relations using, 541-542 self-converse, 679 vertex of, 590, 600 Dijkstra, Edsger Wybe, 649 Dijkstra's algorithm, 649-653, 676 Dimes, 175-176 Diophantus, 101,239 Dirac, G. A., 641 Dirac's Theorem, 641 Direct proof, 76-77, 105 Directed edges, 592, 600-60 1, 675 1-6 Index Directed graphs, 582, 591, 675 circuit ( cycle) in, 546, 582 connectedness in, 626--627 converse of, 611 depth-first search in, 732-734 edges of, 591, 600--601 Euler circuit of, 634 loops in, 541, 582 paths in, 546-547, 582,676 representing relations using, 541-542 self-converse, 679 vertex of, 590, 600 Directed multigraph, 591, 675 paths in, 623 Directly derivable form, 787 Dirichlet drawer principle, 348 Dirichlet, G. Lejeune, 348 Discours (Descartes), 117 Discourse, universe of, 34 Discrete mathematics, definition of, vii, viii Discrete probabilit y, 393-447, 442 assigning, 401-403 of collision in hashing functions, 410-41 1 of combinations of events, 396-398,403-404 conditional, 400, 404-405, 442 finite, 394-396 Laplace's definition of, 393 Disjoint set, 122 Disjunction, 4-5, 104,781 associative law of, 24 distributive law of, over conjunction, 23 Disjunctive normal form for Boolean variables, 758 for propositions, 29 Disjunctive syllogism, 66 Distance between distinct vertices, 680 between spanning trees, 737 Distinguishable boxes, 376 objects, 376 Distinguishable strings, 827 Distributing objects into boxes, 376-379 Distribution binomial, 406-408 probabilit y, 40 I of random variable, 408, 442 geometric, 433-434, 442 uniform, 402, 442 Distributive lattice, 586, 755 Distributive laws for Boolean algebra, 752, 753, 755 for propositions, 23, 24 for sets, 124 div, 202, 225-226 Divide-and-conquer algorithms, 474, 514 recurrence relations, 474-482 Dividend, 214 Divides, 201 Divine Benevolence (Bayes), 420 Divisibility facts, proving, by mathematical induction, 273 Divisibility relation, 567 Division of integers, 200-208 trial, 214 Division algorithm, 202-203 Divisor, 214 greatest common, 215-217, 232-234, 258 DNA sequencing, 638 Dodecahedral die, 443 Dodgson, Charles (Lewis Carroll), 44-45 Dogs, 17 Domain of definition, 149 of discourse, 34 ofa function, 134, 149, 163 of n-ary relation, 530 ofa quantifier, 34 of relation, 530 universe of, 34 Dominating set, 679 Domination laws for Boolean algebra, 753 for propositions, 27 for sets, 124 Dominos, 97, 266 Don't care conditions, 774-775 Double complement, law of, in Boolean algebra, 753, 757 Double negation law, for propositions, 26 Double summations, 156 Drug testing, 424 Dual of Boolean expression, 754 of Boolean function, 754 of compound proposition, 29 of po set, 579 Dual graph, 667 Duality in lattice, 587 Duality principle, for Boolean identities, 754 Dudeney, Henry, 454 Ear(s),292 nonoverlapping, 292 Earth, 424 EBNF (Extended Backus-Naur form), 796 Eccentricity of vertex, 695 Ecology, niche overlap graph in, 592 Edge chromatic number, 674 Edge coloring, 674 Edge vertex, 541 Edges cut, 625 directed, 592, 600, 675 of directed graph, 591, 600--60 I of directed multigraph, 591 endpoints of, 598 incident, 598, 676 of multi graph, 590 multiple, 590, 591, 675 of pseudograph, 591 of simple graph, 590, 611 undirected, 592, 675 of undirected graph, 592, 600 Egyptian (unit) fraction, 331 Einstein, Albert, 21 Electronic mail, 421 Elementary subdivision, 663, 676 Elements, 112, 163 comparable, in partially ordered set, 567, 582 equivalent, 556 fixed, 442 greatest, of partially ordered set, 573, 582 incomparable, in partially ordered set, 567, 582 least, of partially ordered set, 573, 582 of matrix, 247 maximal, of partially ordered set, 572, 582 minimal, of partially ordered set, 572, 573, 582 Elements (Euclid), 212 Elements of Mathematical Logic (Lukasiewicz), 721 Ellipses ( ... ), 112 Empty folder, 114 Empty set, 114, 163 Empty string, 151, 787 Encryption, 207, 231, 257 RSA, 242-243 Encyclopedia of Integers Sequences, The (Sloane and Plouffe), 153 End statements, A-II, A-14 Endpoints of edge, 598 English sentences translating logical expressions to, 42-43 translating to logical expressions, 11-12, 42-43,56-57 Entry of matrix, 247 Enumeration, 335, 386 codeword, 455-456 Equality, of sets, 113, 163 Equation characteristic, 461 difference, 460 Equivalence classes, 558-559, 582 definition of, 558 and partitions, 559-562 representative of, 558 Equivalence, proofs of, 82-83 Equivalence relations, 555-562, 582 definition of, 555 Equivalent Boolean expressions, 750, 751 Equivalent elements, 556 Equivalent finite-state automata, 806, 809-811 Equivalent propositions, 8, 21-30 Eratosthenes, 507-508 sieve of, 210, 514 Erdos number, 584, 623, 624 Erdos Number Project, 623 Erdos, Paul, 584, 623 Errors, in proofs, 83-84 by mathematical induction, 278-279 Essential prime implicant, 770, 781 Euclid, 212, 227, 228 Euclidean algorithm, 227-229, 258, 298 extended, 232, 246 Euler circuits, 633-638, 676 Euler tP-function, 218 Euler, Leonhard, 214, 633, 635 Euler paths, 633-638, 676 Euler's formula, 659-663, 676 "Eureka," A-4 Evaluation functions, 707 Even integer, 77 Event(s), 394 combinations of, 396-398, 403-404 complem entary, 396 independent, 399, 405-406, 442 mutually independent, 444 Exams, scheduling, 671, 672 Exclusion rule, 299 Exclusive or, 5,104 Exhaustive proof, 87 Existence proofs, 91-92, 105 constructive, 91 nonconstructive, 91 Existential generalization, 70, 71 Existential instantiation, 70, 71 Existential quantification, 36, 105 negation of, 39-41 Existential quantifier, 36-37 Expansion balanced ternary, 230 base b, 219,221 binary, 219, 257 binary coded decimal, 774 Cantor, 231 hexadecimal, 219-220, 257 octal, 220, 257 one's complement, 230 two's complement, 230-231 Expected values, 426-429, 442 in hatcheck problem, 430 of inversions in permutation, 430-431 linearity of, 429-43 1, 442 Experiment, 394 Exponential complexity ofalgorithms, 197 Exponential functions, A-7 Exponential generating function, 498 Exponentiation, modular, 226-227 recursive, 313 Expression(s) binomial, 363 Boolean, 750-752, 781 infix form of, 719 11-5 12-6 logical translating English sentences into, II, 42-43,56-57 translating to English sentences, 55 postfix form of, 721 prefix form of, 719 regular, 818, 839 Extended Backus-Naur form (EBNF), 796 Extended binary trees, 302 Extended binomial coefficients, 486 Extended Binomial Theorem, 487 Extended Euclidean algorithm, 232, 246 Exterior of simple polygon, 288 Factor, of integers, 201 Factorial complexity of algorithms, 197 Factorial function, 145 Factorials, recursive procedure for, 311-312 Factorization into primes, 211-212 for finding greatest common divisor, 216 for finding least common multiple, 216, 217 uniqueness of, 233-234 Facts, 75 Failure, 406 Fallacy, 63, 69,105 of affirming conclusion, 69 of begging the question, 84, 105 of circular reasoning, 84, 105 of denying hypothesis, 69 False negative, 419, 421 positive, 419, 421 Family trees, 683, 684 Farmer, 632 Fast multiplication of integers, 475-476 of matrices, 476-478 Fermat, Pierre de, 239 Fermat's Last Theorem, 100-101,299 Fermat's Little Theorem, 239, 258 Fibonacci, 298 Fibonacci numbers, 297 formula for, 463-464 and Huffman coding, 709 iterative algorithm for, 317 rabbits and, 451-452 recursive algorithms for, 316 Fibonacci trees, rooted, 695 Field axioms, A-I -A-2 Fields, 531 Filter, spam, 421-423 Final assertion, 323, 329 Final exams, scheduling, 671, 672 Final value, A-13 Finite graph, 589 Finite probabilit y, 394-396 Finite sets, 116, 158, 163,500,514 subsets of counting, 338 number of, 273-274 union of three, number of elements in, 501-503,514 union of two, number of elements in, 500, 514 Finite-state automata, 805-811 deterministic, 807-811, 839 equivalent, 809-81 1 nondeterministic, 811-814, 820-821, 822,839 regular sets recognized by, 819-821 set not recognized by, 824-825 Finite-state machines, 785, 796-814, 839 with no output, 804-814 with outputs, 798-80 I transition function extension in, 805 First difference, 460 First forward difference, 516 Fixed elements, 442 Fixture controlled by three switches, circuit for, 764 Flavius Josephus, 459 Fleury's algorithm, 637, 646 Flip-flops, 784 Floor function, 143-145, 163 Floyd's algorithm, 656-657 Folder, empty, 114 Forests, 684, 685 definition of, 742 minimum spanning, 742 spanning, 736 Form(s) Backus-Naur, 792-793, 839 conjunctive normal, 758 disjunctive normal for Boolean variables, 758 for propositions, 29 infix, 719 postfix, 720 prefix, 719 prenex normal, 62 Formal language, 786 Formal power series, 485 Formula(s) Euler's, 659-663, 676 for Fibonacci numbers, 463-464 Stirling's, 146 summation, 157,267 well-formed, 301 for compound propositions, 301 of operators and operands, 301 FORTRAN, 792 Fortune cookie, 388 Forward differences, 516 Forward reasoning, 94 Four Color Theorem, 66�71, 676 Fraction, unit (Egyptian), 331 Frame's conjecture, 460 Free variable, 38, 105 Frend Sophia, 25 William, 27 Frequency assignments, 672 Frisbee, rocket-powered, 750 Full adder, 764, 781 Full binary trees, 304-305 height of, 306 number of vertices of, 306 Full m-ary tree, 686, 690, 743 complete, 694 Full subtractor, 766 Function(s), 133-146, 163 Ackermann's, 310 addition of, 135-136 asymptotic, 192 bijective, 138, 163 Boolean, 749-755, 781 dual of, 754 functionally complete set of operators for, 759 implicant of, 770 minimization of, 766-779, 781 representing, 757-759 self-dual, 782 threshold, 783 busy beaver, 835 ceiling, 143-145, 163 characteristic, 148 codomain of, 134, 149, 163 compositions of, 139-141, 163 computable, 163,835 counting, 336-337 decreasing, 13 7 definition of, 133, 163 domain of, 134, 149, 163 Euler q" 218 evaluation, 707 exponential, A-7 factorial, 145 floor, 143-145, 163 generating, 484-495, 514 for counting, 488-493 exponential, 498 probabilit y, 499 for proving identities, 495 Index 1-7 for recurrence relations, 493-495 graphs of, 142 greatest integer, 143-145 growth of, 180-190 growth of combinations of, 186-189 hashing, 205-206 collision in, probability of, 410-41 1 identity, 138 increasing, 13 7 inverse, 139-140, 163 invertible, 140 iterated, 311 iterated logarithm, 311 logarithmic, A-7 -A-9 McCarthy 91, 331 multiplication of, 135-136 number-theoretic, 831 one-to-one (injective), 136, 138, 163 counting, 337 onto (smjective), 137-138, 163 number of, 509-510, 514 partial, 149 product of, 135-136 propositional, 30, 31, 105 existential quantification of, 36 universal quantification of, 33 range of, 134, 163 recursive, 133, 295-299, 328 as relations, 520 strictly decreasing, 137 strictly increasing, 137 sum of, 135-136 threshold, 783 total, 149 transition, 798 Turing machines computing, 831-832 undefined, 149 Functional completeness, 759 Functional decomposition, 784 Functionally complete set of operators for Boolean functions, 759, 781 for propositions, 29 Fundamental Theorem of Arithmetic, 211, 257 Fuzzy logic, 19 Fuzzy sets, 132-133 complement of, 132 intersection of, 133 union of, 133 Gambling, 393 Game of Chomp, 91 Game trees, 704-707, 743 Gates, logic, 760-765 AND, 761, 781 combination of, 761-763 NAND, 766 NOR, 766 OR, 761, 781 threshold, 783 Gating networks, 761-763 depth of, 766 examples of, 763-764 minimization of, 766-779 Gauss, Karl Friedrich, 204, 213 GCD (greatest common divisor), 215-217, 232-234, 258 Generalization existential, 70, 7I universal, 70, 71 Generalized combinations, 371-375 Generalized induction, 307-308 Generalized permutations, 370-371 Generalized pigeonhole principle, 349-351, 387 1-8 Index Generating functions, 484-495, 514 for counting, 488--493 exponential, 498 probability, 499 for proving identities, 495 for recurrence relations, 493--495 Geometric distribution, 433--434, 442 Geometric mean, 95, 282 Geometric progression(s), 150, 152 sums of, 270-271 Geometric series, 155 Geometry, computational, 288-290, 479--48 1 Giant strongly connected components (GSCC), 627 GIMPS (Great Internet Mersenne Prime Search), 213 Goat, 632 GOdel. Escher. Bach (Hofstader), 333 Goldbach, Christian, 214, 215 Goldbach's conjecture, 214--215 Golf, hole-in-one, 279 Golomb, Solomon, 99 Golomb's self-generating sequence, 333 Google, 13, 734 Gossip problem, by mathematical induction, 282 Graceful trees, 746 Graham, Ron, 584 Grammars, 785-793 ambiguous, 840 Backus-Naur form of, 792-793, 839 context-free (type 2), 789, 839 context-sensitive, 789 monotonic, 790 noncontracting, 790 phrase-structure, 787-790, 838 productions of, 787, 838 regular (type 3), 789, 817, 821-824, 839 type 0, 789, 838 type 1,789, 838-839 Graph(s),589-682 acquaintanceship, 592-593 bandwidth of, 680 bipartite, 602-604, 675 book number of, 682 call,594 connected components of, 625 chromatic number of, 667 chromatically k-critical, 674 collaboration, 593-594 coloring, 666-672, 676 complemen tary, 611 complete, 601, 675 complete bipartite, 604, 675 complete m-partite, 678 connected components of, 625-626, 676 connectedness in, 621-629 cut set of, 746 definition of, 591 diameter of, 680 directed, 582, 591, 675 circuit (cycle) in, 546, 582 connectedness in, 626-627 converse of, 611 dense, 614 depth-first search in, 732-734 edges of, 591, 600-601 Euler circuit of, 634 loops in, 541, 582 paths in, 546-547, 582, 676 representing relations using, 541-542 self-converse, 679 simple, 591 vertex of, 590, 600 directed multigraph, 592, 675 dual,667 finite, 589 of functions, 142 Hollywood, 593 homeomorphic, 663-665, 676 independence number of, 680 infinite, 589 influence, 593, 594 intersection, 596 isomorphic, 611, 615-618, 676 paths and, 627-628 matching in, 605 mixed, 591 models, 592-595 monotone decreasing property of, 681 monotone increasing property of, 681 multigraphs, 590, 675 niche overlap, 592, 593 nonplanar, 663-665 n-regular, 610 orientable, 679 paths in, 622-624 planar, 657-665, 676 precedence, 595 pseudograph, 590, 592, 675 radius of, 680 regular, 610, 676 representing, 611-618 in roadmap modeling, 595 self-complemen tary, 620 simple, 590, 601-602, 675 coloring of, 667 connected planar, 659-663 crossing number of, 666 dense, 614 edges of, 590, 608 isomorphic, 615-618, 675 orientation of, 679 paths in, 725 random, 680-681 self-complementary, 620 with spanning trees, 725-726 sparse, 614 thickness of, 666 vertices of, 590, 608 simple directed, 591 sparse, 614, 741 strongly directed connected, 626 subgraph of, 607, 676 terminology of, 597-601 undirected, 592, 599, 676 connectedness in, 624-626 Euler circuit of, 634 Euler path of, 638 orientation of, 679 paths in, 622, 676 underlying, 601, 675 union of, 661, 676 very large scale integration, 682 Web, 594--595 strongly connected components of, 627 weighted, 676 shortest path between, 647-653 traveling salesman problem with, 653-655 wheel, 601, 676 Gray codes, 642-643 Gray, Frank, 643 Great Internet Mersenne Prime Search (GIMPS), 213 "Greater than or equal" relation, 566-567 Greatest common divisor (GCD), 215-217, 232-234, 258 Greatest element of poset, 573, 582 Greatest integer function, 143-145, 163 Greatest lower bound, 582 of poset, 574 Greedy algorithms, 174--176,275-276, 703, 738, 744 Greedy change-making algorithm, 175 Growth of functions, 180-190 GSCC (giant strongly connected components), 627 Guarding set, 675 Guare, John, 623 Guthrie, Francis, 669 Hadamard, Jacques, 213 Haken, Wolfgang, 668 Halfadder, 764, 781 Half subtractor, 766 Halting problem, 176-177,834--835 Hamilton circuits, 636-643, 676 Hamilton paths, 638-643, 676 Hamilton, William Rowan, 640, 669 Hamilton's "Voyage Around the World" Puzzle, 639 Handle, 745 Handshaking Theorem, 599 Hanoi, tower of, 452--454 Hardware systems, 12 Hardy, Godfrey Harold, 93 Harmonic mean, 103 Harmonic number(s), 192 inequality of, 272-273 Harmonic series, 273 Hashing functions, 205-206 collision in, probability of, 410--411 Hasse diagrams, 571-572, 574, 575, 582 Hasse, Helmut, 571 Hasse's algorithm, 101-102 Hatcheck problem, 430, 510 Hazard-free switching circuits, 784 Heaps, 748 Heawood, Percy, 668 Height, star, 840 Height of full binary tree, 306 Height of rooted tree, 691, 743 Height-balanced trees, 748 Hermea s,2 Hexadecimal expansion, 219-220 Hexagon identity, 369 Hilbert's Tenth Problem, 835 HIY, 424 Ho, Chung-Wu, 290 Hoare, C. Anthony R., 323, 324 Hoare triple, 323 Hofstader, Douglas, 333 Hole-in-one, 279 Hollywood graph, 593 paths in, 623-624 Homeomorphic graphs, 663-665, 676 Hopper, Grace Brewster Murray, 811 Hops, 606 Horner's method, 199 Horse races, 27 Host number (hostid), 341 Hotbot, 734 HTML, 796 Huffman coding, 701-703, 744 variations of, 703 Huffman, David A., 70 I, 702 Husbands, jealous, 632-633 Hybrid topology for local area network, 605 Hydrocarbons, trees modeling, 688 Hypercube, 607 Hypothesis, 6 fallacy of denying, 69 inductive, 265 Hypothetical syllogism, 66 "lcosian Game," 639, 640 Idempotent laws for Boolean algebra, 753, 757 for lattices, 585 for propositions, 24 for sets, 124 Identifier, 559, 793 Identities, combinatorial, 364--368 Identity combinatorial, proof of, 359, 386 hexagon, 369 Pascal's, 366-367, 387 12-7 IN! proving, generating functions for, 495 Vandennonde' s,367-368 Identity elements axiom, A-I Identity function, 138 Identity laws additive, A-I for Boolean algebra, 752-754, 755 multiplicative, A-I for propositions, 26 for sets, 124 Identity matrix, 251, 257 "If and only if" statement, 10 If then statement, 6, 8 Image, 136, 163 inverse, of set, 147 of set, 136 Implicant, 770 prime, 770 essential, 770 Implications, 6--10, 105 logical equivalences for, 26--27 Incidence matrices, 614-615, 676 Incident edge, 598 Inclusion relation, 567 Inclusion-exclusion principle, 122,341-343, 500--504, 514 alternative fonn of, 506--507 applications with, 505-512 Inclusive or, 5 Incomparable elements in poset, 567, 582 Incomplete induction, 284 Increasing function, 137 Increment, 206 In-degree of vertex, 600, 675 Independence number, 680 Independent events, 399, 400, 405-406, 442 Independent random variables, 434-436, 442 Independent set of vertices, 680 Index, of summation, 153 Index registers, 672 Indicator random variable, 440 Indirect proof, 77 Indistinguishable boxes, 376--379 objects, 375, 376--379 objects, pennutations with, 375 Indistinguishable strings, 827 Induced subgraph, 678 Induction complete, 284 generalized, 307-308 incomplete, 284 mathematical, 25, 263-279 principle of, 328 proofs by, 266--278 second principle of, 284 strong, 283-285, 328 structural, 304-306, 329 validity of, 278 well-ordered, 568-569, 582 Inductive definitions, 295-308 offactorials, 296 of functions, 295-299, 328 ofseffi, 299-307, 329 of strings, 300 of structures, 299-307 Inductive hypothesis, 265 Inductive loading, 282, 293, 331 Inductive reasoning, 264 Inductive step, 264, 690, 706 Inequality Bernoulli's, 280 Bonferroni's, 415 Boole's, 415 Chebyshev's, 438-439, 442 of harmonic numbers, 272-273 Markov's, 441 proving by mathematical induction, 271 triangle, 102 Inference, rules of, 64-67, 105 for program correctuess, 323-324 for statements, 71-72 Infinite graph, 589 Infinite ladder, 263-264 by strong induction, 284 Infinite series, 157 Infinite set, 116, 158, 163 Infix fonn, 719 Infix notation, 719-721, 744 Influence graphs, 593, 594 Infonnation flow, lattice model of, 575-576 Initial assertion, 323, 329 Initial conditions, 450, 513 Initial position of a Turing machine, 829 Initial state, 798, 805 Initial tenn, 151 Initial value, A-13 Initial vertex, 541, 600 Injection, 136 Injective (one-to-one) function, 136, 139, 163 counting, 337 Inordertraversal, 712, 714, 715, 718, 743 Input alphabet, 798 Insertion sort, 172, 174, 257 average-case complexity of, 432-433 worst-case complexity of, 196 Instantiation existential, 70, 71 universal, 70, 71 Integer sequences, 151-153 Integers, 112 addition of, algorithms for, 222-224 applications with, 231-244 axioms for, A-5 composite, 210, 211, 217, 257 division of, 200--208 even, 77 finding maximum element in finite sequence of, 170 greatest common divisor of, 215-217 large, computer arithmetic with, 237-238 least common multiple of, 216, 217 multiplication of algorithms for, 224-226 fast, 475-476 odd, 77 partition of, 310 perfect, 218 pseudoprimes, 238-241, 245, 257 representations of, 219-222, 230--231 set of, 112 square free, 513 Intension, of database, 531 Interconnection networks for parallel computation, 606--607 Interest, compound, 451 Interior of simple polygon, 288 Interior vertices, 551 Internal vertices, 686, 743 International Standard Book Number (ISBN), 209 Internet search engines on, 734 searching, 13 Internet addresses, counting, 341 Internet datagram, 346--347 Internet Movie Database, 593 Internet Protocol (IP) multicasting, 726 Intersection of fuzzy sets, 133 of multisets, 132 ofseffi, 121-122, 126--128, 163 Intersection graph, 596 Interval, open, 282 Intractable problem, 197, 836 Invariant for graph isomorphism, 615, 676 Invariants, loop, 326--327, 329 Inverse of congruence modulo m, 234 of functions, 139-140, 163 of implication, 8, 105 of matrix, 255 Inverse image of set, 147 Inverse law for addition, A-2 for mUltiplication, A-2 Inverse relation, 528 Inverse, multiplicative, 54 Index 1-9 Inversions, in pennutation, expected number of, 430-431 Inverter, 761, 781 Invertible function, 140 Invertible matrix, 255 IP multicasting, 726 Irrational numbers, 218 Irrationality of -/2, 80--81, 292, 332 Irreflexive relation, 528 ISBN (International Standard Book Number), 209 Isobutane, 688 Isolated vertex, 598, 676 Isomorphic graphs, 611, 615-618, 676 paths and, 627-628 Iterated function, 311 Iterated logarithm, 311 Iteration, 311 Iterative algorithm, for Fibonacci numbers, 317 Iterative procedure, for factorials, 317 Iwaniec, Henryk, 215 Jacobean rebellion, 145 Jacquard loom, 27 Java, 135 Jealous husband problem, 632-633 Jewish-Roman wars, 459 Jigsaw puzzle, 292 Jobs, assignment of, 513, 604 Join, in lattice, 585 Join of n-ary relations, 533-534 Join of zero-one matrices, 252 Joint authorship, 593-594 Jordan Curve Theorem, 288 Josephus, Flavius, 459 Josephus problem, 459 Jug, 103, 633 Kakutani's problem, 102 Kaliningrad, Russia, 633 Karnaugh maps, 768-774, 781 Karnaug h,Maurice, 767, 768 Kempe, Alfred Bray, 668 Key composite, 532 for hashing, 205 primary, 531-532 al-Khowarizmi, Abu Ja'far Mohammed ibn Musa, 168 Kissing problem, 154 Kleene closure, 804-805, 839 Kleene, Stephen Cole, 805, 817 Kleene's Theorem, 819-821, 839 K-maps, 768-774, 781 Kneiphof isiand, 633 Knights and knaves, 13-14 Knight's tour, 647 reentrant, 647 Knuth, Donald E., 172, 182, 184, 189 Konigsberg bridge problem, 633-634, 636, 638 Kruskal, Joseph Bernard, 739, 740 Kruskal's algorithm, 739-740, 744 k-tuple graph coloring, 674 Kuratowski, Kazimierz, 663 Kuratow ski's Theorem, 663-665, 676 Labeled tree, 695 Ladder, infinite, 263-264, 284 Lagarias, Jeffrey, 102 1-10 Index Lame, Gabriel, 299 Lame's Theorem, 298 Landau, Edmund, 183 Landau symbol, 182 Language, 785-79 3,838 context-free, 789 fonnal,786 generated by grammar, 788 natural, 785-786 recognized by finite-state automata, 806-814,839 regular, 789 Language recognizer, 80 I Lady Byron, 27 Laplace, Pierre Simon, 393, 394, 420 Laplace' s definition of probability, 394 Large numbers, law of, 407 Lattice model ofinformation flow, 575-576 Lattice point, 330 Lattices, 574-576, 582 absorption laws for, 585 associative laws for, 585 bounded, 586 commutative laws for, 585 complemented, 586 distributive, 586 duality in, 587 idempotent laws for, 585 join in, 585 meet in, 585 modular, 587 Law(s) absorption for Boolean algebra, 753, 754 for lattices, 585 for propositions, 24 for sets, 124 associative for addition, A-I for Boolean algebra, 753, 755 for lattices, 585 for multiplication, A-I for propositions, 24 for sets, 124 closure for addition, A-I for multiplication, A-I commutative for addition, A-I for Boolean algebra, 753, 755 for lattices, 585 for multiplication, A-I for propositions, 26 for sets, 124 compatibility additive, A-2 multiplicative, A-2 complement, for sets, 124 complementation, for sets, 124 completeness, A-2 De Morgan's for Boolean algebra, 753, 757 for propositions, 22, 25-26 proving by mathematical induction, 274-275 for quantifiers, 40-41 for sets, 124 of detachment, 65 distributive, A-2 for Boolean algebra, 752, 753, 755 for propositions, 23, 24 for sets, 124 domination for Boolean algebra, 753 for propositions, 27 for sets, 124 of double complement, in Boolean algebra, 753, 755 idempotent for Boolean algebra, 753, 757 for lattices, 585 for propositions, 24 for sets, 124 identity additive, A-I for Boolean algebra, 752-754, 755 multiplicative, A-I for propositions, 26 for sets, 124 inverse for addition, A-2 for mUltiplication, A-2 of large numbers, 407 of mathematical induction, A-5 negation, for propositions, 26 used to prove basic facts, A-2 -A-5 transitivity, A-2 trichotomy, A-2 Laws of Thought. The (Boole), 3, 5, 420, 749 Leaf, 686, 743 Least common multiple (LCM), 216-217, 257 Least element of poset, 573, 582 Least upper bound, 582, A-2 of poset, 574 Left child of vertex, 687 Left subtree, 687 Lemma, 75, 105 Length of bit string, 15,454-455 Length of path in directed graph, 546 in weighted graph, 648 Length of string, 151 recursive definition of, 301 "Less than or equals" relation, 567 Level of vertex, 691, 743 Level order of vertex, 745 Lexicographic ordering, 307, 382-384, 385, 569-570 Liber Abaci (Fibonacci), 298 Light fixture controlled by three switches, circuit for, 764 Limit, definition of, 55 Linear array, 606, 607 Linear bounded automata, 825 Linear combination, gcd as, 232 Linear complexity of algorithms, 197 Linear congruences, 234-235, 257 Linear congruential method, 206 Linear equations, simultaneous, 256 Linear homogenous recurrence relations, 460, 461-467,513 Linear nonhomogenous recurrence relations, 467-471,514 Linear ordering, 568, 582 Linear search algorithm, 170, 257 average-case complexity of, 430-43 1 complexity of, 194, 197 recursive, 314 Linearity of expectations, 429-43 1, 442 Linearly ordered set, 568, 582 Lists, merging two, 319 Literal, 758, 781 Little-o notation, 192 Littlewood, John E., 93 Loading, inductive, 282, 293, 331 Loan, 457 Lobsters, 471 Local area networks, 605-606 Logarithm, iterated, 311 Logarithmic complexity of algorithms, 197 Logarithmic function, A-7 - A-9 Logic, 1-58, 104-105 fuzzy, 19 propositional, 2 rules of inference for, 64-67 Logic gates, 76(}-765 AND, 761, 781 combination of, 761-763 NAND, 766 NOR, 766 OR, 761, 781 threshold, 783 Logic Problem, 74 Logic programming, 45-46 Logic puzzles, 13-15 Logical connectives, 4 Logical equivalences, 22-27, 39,105 in predicate calculus, 39 in propositional calculus, 22-27 Logical expressions translating English sentences into, 1 (}-11, 42-43, 56-57 translating to English sentences, 42-43 Logical operators, 104 functionally complete, 29 precedence of, I (}-l l Long-distance telephone network, 594 Loom, Jacquard, 27 Loop constroctions, A-13 -A-14 Loop invariants, 326-327, 329 Loops in directed graphs, 541, 582 within loops, A-14 nested, 51-52 Lottery, 394-395 Lovelace, Countess of (Augusta Ada), 25, 27 Lower bound ofiattice, 586 of poset, 574, 582 Lower limit of summation, 153 Lucas, Edouard, 452 Lucas numbers, 330, 471 Lucas-Lehmer test, 213 Lucky numbers, 518 �ukasiewicz, Jan, 719, 744 Lyceum, 2 Lycos, 734 McCarthy, John, 332 McCarthy 91 function, 331 McClu skey, Edward J ., 775 Machine minimization, 810 Machines delay, 799-800 finite-state, 785, 798, 839 with no output, 804-814 with outputs, 798-801 Mealy, 801 Moore, 801 Turing, 785,825,826, 827-832, 839 computing functions with, 831-832 definition of, 828-830 nondetenninistic, 832 sets recognized by, 830-83 1 types of, 832 vending, 797-798 MAD Magazine. 184 Magic tricks, 14 Majority voting, circuit for, 763 Maps coloring of, 666-667 �ugh, 768-774 Markov's inequality, 441 Markup languages, 796 m-ary tree, 686, 743 complete, 694 full,686,690 height of, 692-693 Master Theorem, 479 Matching, 605 maximal, 605 12-9 Mathematical Analysis of Logic, The (Boole), 5 Mathematical induction, 25, 263-279 Axiom of, A-5 errors in, 278-279 generalized, 307-308 inductive loading with, 282 principle of, 328 1:3-1 () proofs by, 266-278 of divisibility facts, 273 errors in, 278-279 of inequalities, 271 of results about algorithms, 275 of results about sets, 273-275 of summation formulae, 267 second principle of, 284 strong, 283-285,328 structuml, 304-306, 329 validity of, 278 Mathematician s Apology, A (Hardy), 93 Matrix (matrices), 246-254 addition of, 247-248 adjacency, 612-614, 676 counting paths between vertices by, 628-629 Boolean product of, 252-254, 257 column of, 247 definition of, 257 diagouaI, 255 identity, 251, 257 incidence, 614-615, 676 inverse of,255 invertible, 255 multiplication of, 249-250 algorithms for, 249-250 fast, 476-478 powers of, 251-252 representing relations using, 538-540 row of, 247 sparse, 614 square, 251 symmetric, 251, 257 tmnsposes of, 251, 257 upper triangular, 260 zero-one, 252-254, 257 Boolean product of, 252-254 join of, 252 meet of, 252 representing relations using, 53&:-540 oftmnsitive closure, 549-550 Warshall's algorithm and, 550-553 Matrix-chain multiplication, 250 Maurolico, Francesco, 265 Maximal element ofposet, 572, 582 Maximal matching, 605 Maximum element in finite sequence, 169 time complexity of finding, 194 Maximum, of sequence, 475 Maximum satisfiability problems, 445 Maximum spanning tree, 742 Maxterm, 760 Mealy, G. H., 801 Mealy machines, 801, 839 Mean, 178 arithmetic, 95, 282 deviation from, 439 geometric, 95, 282 harmonic, 103 quadmtic, 103 Median, 178 Meet, in lattice, 585 Meet of zero-one matrices, 252 Meigu, Guan, 638 Members, 112, 163 Membership, degree of, 132 Membership tables, 126, 163 Menages, probleme des, 518 Merge sort, 172,317-321,329,475 complexity of, 479 recursive, 318 Merging two lists, 319 Mersenne, Marin, 212,213 Mersenne primes, 212, 257 Mesh network, 606-607 Mesh of trees, 748 Metacharacters, 796 Metafont, 184 Method(s) Critical Path, 587 Horner's, 199 linear congruential, 206 probabilistic, 413-414, 442 proof, 75-102,105 Quine-McClusk ey, 767, 775-779 Millbanke, Annabella, 27 Millennium problems, 836 Miller's test for base b, 245 Minimal element of po set, 572, 582 Minimization of Boolean functions, 766-779, 781 of combinational circuits, 766-779 Minimum dominating set, 679 Minimum, of sequence, 475 Minimum spanning forest, 742 Minimum spanning trees, 737-74 1, 744 Minmax stmtegy, 706, 743 Minterm, 758, 781 Mistakes, in proofs, 83--84 Mixed gmph, 591 mmod,202,203,205-207 ,225-226 Mode, I78 Modeling computation, 785-837 with gmphs, 592-595 with recurrence relations, 450-456 with trees, 688-690 Modular arithmetic, 203-205 Modular exponentiation, 226-227 recursive, 313 Modular lattice, 587 Modular properties, in Boolean algebra, 757 Modulus, 206 Modus ponens, 65, 66 Modus tollens, 66 Mohammed's scimitars, 637 Molecules, trees modeling, 688 Monotone decreasing property of graph, 681 Monotone increasing property of gmph, 681 Monotonic gmmmm ars, 790 Monte Carlo algorithms, 411-413 Montmort, Pierre Raymond de, 511, 518 Monty Hall Three Door Puzzle, 398, 425 Moore, E. F., 801 Moore machine, 801 Moth,811 Motorized pogo stick, 750 m-tuple, 533 Muddy children puzzle, 14-15 Multicasting, 726 Multigmphs, 590, 675 Euler circuit of, 637 Euler path of, 637 undirected, 592 Multinomial coefficient, 382 Multinomial Theorem, 382 Multiple of integers, 20 I least common, 216-217, 257 Multiple edges, 590, 591, 592, 675 Multiple output circuit, 764 Multiplexer, 766 Multiplication of functions, 135-136 of integers algorithms for, 224-226 fast, 475-476 of matrices, 249-250 algorithms for, 249-250 fast, 476-478 matrix-chain, 250 Multiplicative Commpatibility Law, A-2 Multiplicative inverse, 54 Multiplicity of membership in multisets, 132 Multiplier, 206 Multisets, 132 Index 1-11 Mutually independent events, 444 Mutually independent trials, 406 Mutually relative prime, 260 Naive set theory, 112 Narnagiri, 94 NAND, 29, 759, 781 NAND gate, 766 n-ary relations, 530-536, 581 domain of, 530 opemtions on, 532-535 Natuml language, 785-786 Natural numbers, 112 Naur, Peter, 792 NAUTY, 618 Naval Ordnance Labomtory, 811 Navy WAVES, 811 n-cubes, 602 Necessary, 6 and sufficient, 9 Negation of existential quantification, 39 of nested quantifiers, 57-58 of propositions, 3-4,104 of universal quantification, 39 Negation laws, for propositions, 26 Negation opemtor, 4 Negative false, 419 true, 419 Neighbors in gmphs, 598 Neptune, 424 Nested loops, 51-52 Nested quantifiers, 50-58 negating, 57-58 Network computer, 590 with diagnostic lines, 591 interconnection networks, 606-607 local area networks, 605-606 multicasting over, 726 with multiple lines, 590 with multiple one-way lines, 592 with one-way lines, 591 gating, 761-763 depth of, 766 examples of, 763-764 minimization of, 766-779 tree-co nnected, 689-690 Network number (netid), 341 New Foundations of Mathematical Logic (Quine ),776 New Jersey cmgs, 44 Newton-Pepys problem, 447 Niche overlap gmph, 592, 593 Nickels, 175-176 Nim, 704-705, 707 Nodes, 541, 589 Nonconf ormists, 420 Nonconstructive existence proof, 91, 105 Noncontmcting gmmmar, 790 Nondeterministic finite-state automata, 811--814, 820,822,839 Nondeterministic Thring machine, 832 Nonoverlapping ears, 292 Nonplanar gmphs, 663-665 NontermiuaIs, 787 NOR, 29, 759, 78 1 NOR gate, 766 NOT,13 Notation big-a, 180-186 big-Omega, 182, 189-190 big-Theta, 182, 189-190 dependency, 784 infix, 719-72 1, 744 little-o notation, 192 one's complement, 230 Polish, 719, 744 1-12 Index Notation-Cont. postfix, 719-721, 744 prefix, 719-721, 744 for products, 162 well-formed formula in, 724 reverse Polish, 720, 744 set builder, 112 summation, 153 two's complement, 230-23 1 Noun, 786 Noun phrase, 786 Nova, 101 NP, class, 197, 836 NP-complete problems, 197, 655, 768, 836 n-queens problem, 731-732 n-regular graph, 610 n-tuples, 531-532 ordered, 117 Null quantification, 49 Null set, 114, 163 Null string, 151,787 Number(s) Bacon, 623 Bell,566 Carmichael, 240-24 1, 257 Catalan, 456 chromatic, 667, 668, 676 edge, 674 crossing, 666 Cunningham, 214 Erdos, 584, 623, 624 Fibonacci, 297-298, 316-317, 709 formula for, 463-464 iterative algorithm for, 317 rabbits and, 451-452 recursive algorithms for, 316 harmonic, 192 inequality of, 272-273 independence, 680 irrational, 218 large, law of, 407 Lucas, 330,471 lucky,518 natural, 112 perfect, 218 pseudorandom, 206-207 Ramsey,3 52 rational, 105, 113 countability of, 158, 163 real, 113, A-I -A-5 Stirling, of the second kind, 378 Ten Mosted Wanted, 214 U1am, 165 Number theory, 200-244 Numbering plan, 337 Number-theoretic functions, 831 Object(s), 111-112 distinguishable, 376 and distinguishable boxes, 376-377 and indistinguishable boxes, 377-378 indistinguishable, 376 and indistinguishable boxes, 377, 378-379 unlabeled, 376 Octahedral die, 443 Octal expansion, 220, 257 Odd integer, 77 Odd pie fights, 276 Odlyzko, Andrew, 154 One's complement expansion, 230 One-to-one correspondence, 139, 160, 163 One-to-one (injective) function, 136, 139, 163 counting, 337 "Only if' statement, 9 Onto (surjective) function, 137-138, 139, 163 number of, 509-510, 514 Open interval, 282 Open problems, 214-215 Operands, well-formed formula of, 301 Operations bit, 15 estimating number of, of algorithms, 180-182 integer, algorithms for, 222-226 on n-ary relations, 532-535 on set, 121-130 Operators bitwise, 15-16, 104 functionally complete set of, for propositions, 29 logical, 104 functionally complete, 29 precedence of, 10-11 negation, 4 selection, 532 well-formed formula of, 301 Opium, 424 Optimal algorithm, 200 Optimal for suitors, stable assignment, 293 Optimization problems, 174-176 OR, 13, 15-16 OR gate, 761, 781 Oracle of Bacon, 624 Order, 190 of quantifiers, 52-54 same, 182 Ordered n-tuples, 117 Ordered pairs, 117, 120 Ordered rooted tree, 687, 743, 745 Ordering dictionary, 382 lexicographic, 307, 382-384, 569-570 linear, 568 partial, 566-578, 582 quasi-ordering, 585 total, 568, 582 Ordinary generating function, 484 Ore, 0., 641 Ore's Theorem, 641, 647 Orgwrizational tree, 689 Orientable graphs, 679 Orientation of undirected graph, 679 Quantifiers theorems stated as propositions involving, 10,23,24,64 Out-degree of vertex, 600, 675 Output alphabet, 798 Outputs finite-state machines with, 798-80 I finite-state machines without, 804-814 P, class, 197, 836 Pair devil's, 621 ordered, 117 Pairwise relatively prime integers, 216, 257 Palindrome, 177,346,795 Paradox, 112 barber's, 19-20 Russell's, 121 St. Petersburg, 444 Parallel algorithms, 606 Parallel edges, 590 Parallel processing, 199, 606 tree-connected, 689-690 Parent ofvertex, 686,743 Parent relation, 526 Parenthe ses, balanced strings of, 332 Parse tree, 790-792, 839 Parsing bottom-up, 791 top-down, 791 Partial correctness, 323 Partial function, 149 Partial orderings, 566-578, 582 compatible total ordering from, 582 Partially ordered set, 566, 582 antichain in, 585 chain in, 585 comparable elements in, 568, 582 dense, 581 dual of, 579 greatest element of, 573, 582 Hasse diagram of, 571-572, 573, 582 incomparable elements in, 568, 582 least element of, 573, 582 lower bound of, 574, 582 maximal element of, 572, 582 minimal element of, 572, 573, 582 upper bound of, 574, 582 well-founded, 581 Partition, 499 of positive integer, 310, 379 refinement of, 565 of set, 559-562, 582 Pascal, 135 Pascal, Blaise, 239, 366, 393, 400 Pascal's identity, 366-367, 387 Pascal's triangle, 366, 386 Passwords, 63, 340 Paths, 622-624 in acquantanc eship graphs, 623 in collaboration graphs, 623 counting between vertices, 628-629 in directed graphs, 546-547, 582, 676 in directed multigraphs, 623 Euler, 633-63 8, 676 and graph isomorphism, 627-628 Hamilton, 638-643, 676 in Hollywood graph, 623-624 oflength n, 623 length of, in weighted graph, 648 shortest, 647-655, 676 in simple graphs, 622 terminology of, 622 in undirected graphs, 622 Payoff, 704 Pearl Harbor, 811 Pecking order, 680 Peirce, Charles Sanders, 29, 32 Peirce arrow, 29 Pelc, A., 483 Pendant vertex, 598,676 Pennies, 175-176 Perfect integers, 218 number, 218 power, 87 Peripatetics, 2 Permuta tions, 355-357, 386 . generating, 382-384 generating random; 445 with indistinguishable objects, 375-376 inversions in, expected number of, 430-431 with repetition, 371 PERT (program Evaluation and Review Technique), 587 Petersen, Julius Peter Christian, 646 Phrase-structure grammars, 787-790, 838 Pick's Theorem, 292 Pie fights, odd, 276 Pigeonhole principle, 347-353, 387 applications with, 351-353 generalized, 349-351, 387 Planar graphs, 657-665, 676 Plato, 2 Plato's Academy, 2 Plouffe, Simon, 153 PNF (prenex normal form), 62 P=NP problem, 836 P(n,r),386 Pogo stick, motorized, 750 12-// Pointer, position of, digital representation of, 642-643 Poisonous snakes, 702 Pokerhands, 358-359 Polish notation, 719, 744 P61ya, George, 102 12-12 Polygon, 288 convex, 288 diagonal of, 288 exterior of, 288 interior of, 288 nonconvex, 288 with nonoveriapping ears, 292 sides of, 288 simple, 288 vertices of, 288 Polynomial complexity of algorithms, 197 Polynomials, rook, 518 Polynomial-time problems, 836 Polyominoes, 99, 277 Poset, 566, 582 antichain in, 585 chain in, 585 comparable elements in, 567, 582 dense, 581 dual of, 579 greatest element of, 573, 582 Hasse diagram of, 571-572, 573, 582 incomparable elements in, 568, 582 least element of, 573, 582 lower bound of, 574, 582 maximal element of, 572, 582 minimal element of, 572, 573, 582 upper bound of, 574, 582 well-founded, 581 Postcondition, 33, 323 Positive false, 419 true, 419 Positive integers, 113 axioms for, A-5 Positive rational numbers, countability of, 158 Postfix form, 720 Postfix notation, 719-721, 744 Postorder traversal, 712, 715-716, 717, 718, 743 Postulates, 75 Power, perfect, 88 Power, Boolean (of zero-one matrices), 252-254 Power series, 485-488 formal,485 Power set, 116-117, 163 Powers of matrices, 251-252 of relation, 527, 581 Pre-image, 147, 163 Precedence graphs, 595 Precedence levels, 11, 38 Precondition, 33, 323 Predicate, 30, 31 Predicate calculus, 33 logical equivalence in, 39 Prefix codes, 700-703, 743 Prefix form, 719 Prefix notation, 719-721, 744 well-formed formula in, 724 Premise, 6, 44, 63 Prenex normal form (PNF), 62 Preorder traversal, 712-713, 718, 743 Prim, Robert Clay, 738, 740 Primality testing, 412-413 Primary key, 531-532, 581 Prime factorization, 211-212 for finding greatest common divisor, 216 for finding least common multiple, 216-217 uniqueness of, 233-234 Prime implicant, 770, 781 essential, 770 Prime Number Theorem, 213, 584 Primes, 210-215 definition of, 257 distribution of, 213 infinitude of, 212 �ersenne, 212,257 mutually relative, 260 pseudoprimes, 238-24 1, 245, 257 relatively, 216, 257 sieve of Eratosthenes and, 210 twin, 215 Prim's algorithm, 738-739, 744 Princess of Parallelograms, 27 Principia Mathematica (Whitehead and Russell), 28,114 Principle( s) of buoyancy, A-4 of counting, 335-340 duality, for Boolean identities, 754 of inclusion -exclusion, 122,341-343, 500-504, 514 alternative form of, 506-507 applications with, 505-512 of mathematical induction, 328 pigeonhole, 347-353, 387 applications with, 351-353 generalized, 349-351, 387 of well-founded induction, 585 of well-ordered induction, 568-569 Private key cryptography, 241 Probabilistic algorithms, 393, 411-413, 442 Probabilistic method, 413-414, 442 Probabilistic primality testing, 412-413 Probabilistic reasoning, 398 Probability, discrete, 393-447 assigning, 401-403 of collision in hashing functions, 410-411 of combinations of events, 396-398, 403-404 conditional, 400, 404-405, 442 finite, 394-396 Laplace's definition of, 394 in medical test results, 419-420 Probability distribution, 40 I Probability generating function, 499 Probability theory, 393, 400-416 Problem(s) art gallery, 675 birthday, 409-41 1 bridge,633-634, 636,638 celebrity, 282 Chinese postman, 638 classes of, 835 Closest-Pair, 479-482 Collatz, 101-10 2 decision, 834 halting, 176-177, 834-835 hatcheck, 430, 510 Hilbert's tenth, 835 intracta ble, 197, 836 jealous husband, 632-633 Josephus, 459 Kakutani's, 102 kissing, 154 Logic, 74 maximum satisfiability, 445 �illennium, 836 Newton-Pep ys,447 NP-=mplete, 197,655,768 n-queens, 731-732 open, 214-215 optimization, 174-176 P=NP, 836 polynomial-time, 836 scheduling greedy algorithm for, 179 final exams, 671, 672 shortest-path, 647-655, 676 solvable, 197, 834 Syracuse, 102 tiling, 835 tractable, 197, 836 traveling salesman, 641, 649, 653-655, 676 Ulam's, 101-10 2 unsolvable, 197, 834, 835 utilities-and-houses, 657-659 yes-or-no, 834 Probleme de rencontres, 511, 518 Probleme des menages, 518 Index 1-1 Problems, �illennium, 836 Procedure statements, A-10, A-14 -A-15 Processing concurrent, 595 parallel, 199, 606 tree-connected, 689-690 Product Boolean, 749-750, 751, 757-759, 781 of zero-one matrices, 252-254, 257 Cartesian, 117-118 Product notation, 162 Product rule, 336 Productions of grammar, 787 Product-of-sums expansion, 760 Program correctness, 322-328, 329 conditional statements for, 324-326 loop invariants for, 326-327 partial, 323 program verification for, 323 rules ofinference for, 323-324 Program Evaluation and Review Technique (PERT),587 Program verification, 323 Programming, logic, 45-46 Programming languages, 68, 135, 346, 559 Progression( s) arithmetic, 151 geometric, 150, 152 sums of, 270-271 Projection of n-ary relations, 533, 582 Prolog, 45 Prolog facts, 45 Prolog rules, 45 Proof(s) adapting existing, 96 by cases, 86-90, 105 combinatorial, 359, 386 conjecture and, 96-97 by contradiction, 80-83, 105 definition of, 75, 105 direct, 76-77, 105 of equivalence, 82-83 exhaustive, 87 existence, 91-92, 105 indirect, 77, 105 by mathematical induction, 266-278 methods of, 75-102, 105 mistakes in, 83-84 of recursive algorithms, 315-317 by structural induction, 304 trivial, 78, 79, 105 uniqueness, 92-93, 105 vacuous, 78,105 Proofstrategy, 79, 86-102 Proper subset, 115, 163 Property Archimedean, A-5 Completeness, A-2 Well-ordering, 290-29 1, 328, A-5 Proposition(s), 1-6,75 compound, 3, 10-11,21,23, 104 consistent, 104 disjunctive normal form of, 29 dual of, 29 satisfiable, 30 well-formed formula for, 301 conjunction of, 4-5, 104 definition of, 2, 104 disjunction of, 4-5, 104 equivalent, 8, 21-30,104 functionally complete set of operators for, 29 negation of, 3-4, 104 rules of inference for, 64-67 truth value of, 3 Propositional equivalences, 21-30 Propositional function, 30, 31, 105 existential quantification of, 36 universal quantification of, 33 Propositional logic, 2 1-14 Index Protestant Noncomf ormists, 420 Pseudocode, 121, 169, A-IO -A-15 Pseudograph, 590, 592,675 Pseudoprimes, 238-239,245,257 Pseudorandom numbers, 206--207 Public key cryptography, 231, 241-244, 257 Pure multiplicative generator, 207 Pushdown automaton, 825 Puzzle(s) Birthday Problem, 409-411 Icosian, 639, 640 jigsaw, 292 logic, 13-15 Monty Hall Three Door, 398, 425 muddy children, 14-15 Reve's,454 river crossing, 632 Tower of Hanoi, 452-454 "Voyage Around the World," 639 zebra, 21 Quad trees, 748 Quadratic mean, 103 Quadratic residue, 246 Quality control, 412 Quantification existential, 36, 105 negation of, 39 as loops, 51-52 null,49 universal, 34, 105 negation of, 39 Quantifiers, 34-37 De Morgan's laws for quantifiers, 40-41 domain of, 34 existential, 36--37 negating, 39-41 nested, 50-58 negating, 57-58 order of, 52-54 precedence of, 38 rules of inference for, 70 truth set of, 119 uniqueness, 37 universal ,34-36 using in system specifications, 43-44 using set notation with, 119 with restricted domains, 38 Quarters, 175-176 Quasi-ordering, 585 Queens on chessboard, 679 Question, begging, 84,105 Quick sort, 172, 322 Quine, Willard van Orman, 775, 776 Quine-McCluskey method, 768, 775-779 Quotient, 202, A-6 Automaton, 817 Rabbits, 451-452 Races, horse, 27 Radius of graph, 680 Rado, Tibor, 838 Ramanu jan, Srinivasa, 93, 94 Raman;, 0., 215 Ramsey, Frank Plumpton, 352 Ramsey number, 352 Ramsey theory, 352 Random permutation, generating, 445 Random simple graph, 680-68 1 Random variables, 402, 408-409 covariance of, 442 definition of, 442 distribution of, 408, 442 geometric, 433-434 expected values of, 402--403, 426-429, 442 independent, 434-436, 442 indicator, 440 standard deviation of, 436 variance of, 426-429, 442 Range of function, 134, 163 Ratio, common, 150 Rational numbers, 105, 113 countability of, 158, 163,218 r-combination, 357, 386 Reachable state, 841 Real numbers, 113 constructing, A-5 least upper bound, A-2 uncountability of, 160 upper bound, A-2 Reasoning backward, 94 circular, 84, 105 deductive, 264 forward, 94 inductive, 264 probabilistic, 398 Recognized language, 806--814, 839 Recognized strings, 806 Records, 531 Recurrence relations, 449-460 associated homogenous, 467 definition of, 450, 513 divide-and-conquer, 474-482 initial condition for, 450 linear homogenous, 460, 461-467, 513 linear nonhomogenous, 467-47 1, 514 modeling with, 450-456 simultaneous, 472 solving, 460-47 1 generating functions for, 493-495 Recursion, 294 Recursive algorithms, 311-321, 329 for binary search, 314 for computing an, 312 for computing greatest common divisor, 313 correctness of, 315 for factorials, 311-312 for Fibonacci numbers, 316 for linear search, 314 for modular exponentiation, 313 proving correct, 315-317 trace of, 312, 313 Recursive definitions, 263, 295-308 of extended binary trees, 302 offactorials, 311-312 of full binary trees, 33 of functions, 295-299, 328 of sets, 299-307, 329 of strings, 300 of structures, 299-307 Recursive merge sort, 318 Recursive modular exponentiation, 313 Recursive sequential search algorithm, 314 Recursive step, 299 Reentrant knight's tour, 647 Refinement, of partition, 565 Reflexive closure of relation, 544, 582 Reflexive relation, 522, 581 representing using digraphs, 541-542 using matrices, 538 Regions of planar representation graphs, 659, 676 Regular expressions, 818, 839 Regular grammars, 789, 817, 821-824, 839 Regular graph, 610 Regular langnage, 789 Regular sets, 817-819, 820, 839 Relation(s ),519-587 antisymmetri c,523-524, 582 asymmetric, 528 binary, 519, 581 circular, 584 closures of, 544-553, 582 reflexive, 544, 582 symmetric, 545,582 transitive, 544, 547-550, 582 computing, 550-553 combining, 525-527 complementary, 528 composite of, 526--527, 581 connectivity, 547, 582 counting, 522 covering, 579 diagona l,545 divide-and-conquer recurrence, 474 divisibil ity, 567 domains of, 530 equivalence, 555-562, 582 functions as, 520 "greater than or equal," 566--567 inclusion, 566 inverse, 528, 581 irreflexive, 528 "less than or equals," 567 n-ary, 530-536, 581 domain of, 531 operations on, 532-535 parent, 526 paths in, 546--547 powers of, 527, 581 properties of, 522-525 reflexive, 522, 581 representing using digraphs, 541-542 using matrices, 538-540 on set, 118,521-522 symmetric, 523-524, 582 transitive, 524-525, 527, 582 Relational database model, 531-532, 581 Relatively prime integers, 216, 257 Remainder, 202, 203, 257 Rencontres. 511, 518 Repetition combinations with, 371-375 permutations with, 371 Replacement sampling with, 396 sampling without, 396 Representations binary, 219 decimal, 168,219 hexadecima l,219-220 octal,220 one's complement, 230 unary, 831 Representative, of equivalence class, 558 Resolution, 66, 68 Results, 75 Reverse Polish notation, 720, 744 Reve's puzzle, 454 Right child of vertex, 687 Rightsubtree, 687 Right triominoes, 100,277,283 Ring topology for local area network, 605 River crossing puzzle, 632 Rivest, Ronald, 241, 242 Roadmaps, 595 �ocket-powered Frisbee, 750 Rook polynomials, 518 Rooted Fibonacci trees, 695 Rooted spanning tree, 737 Rooted trees, 302, 685-688 balanced, 691, 743 binomial, 745 B-tree of degree Ie, 745 decision trees, 698-700 definition of, 743 height of, 691, 743 level order of vertices of, 745 ordered, 687, 743, 745 Sk-tree, 745 Roots, characteri stic, 461 Round-robin tournaments, 291, 593, 594 Row of matrix, 247 Roy, Bernard, 551 Roy-Warshall algorithm, 550-553 12-13 /2-/4 r-permutation, 355, 386 RSA system, 241-244 decryption, 243 encryption, 242-243 Rule . product, 336 sum, 338, 386--387 Rules of inference, 64-67, 70, 105 for program correctness, 323-324 for propositions, 64-67 for quantifiers, 70 for statements, 71-72 Run, 440 Russell, Bertrand, 112, 114 Russell's paradox, 121 St. Petersburg Paradox, 444 Same order, 182 Samos, Michael, 480 Sample space, 394 Sampling with replacement, 396 without replacement, 396 Sanskrit, 792 Satisfiability problem, maximum, 445 Satisfiable compound propositions, 30 Saturated hydrocarbons, trees modeling, 688 Scheduling problems, 179 final exams, 671, 672 greedy algorithm for, 275-276 software projects, 581 tasks, 577-578 Scimitars, Mohammed's, 637 Scope of a quantifier, 38 Screw, Archimedes, A-4 Search engines, 734 Search trees, binary, 695-698, 743 Searching algorithms, 170--172, 257 binary, 171-172,257,474 Boolean, 13 breadth-first, 729-73 1, 744 complexity of, 194-195, 197 depth-first, 726--729, 744 applications with, 732-734 in directed graphs, 732-734 linear, 170, 257 average-case complexity of, 430-43 1 complexity of, 195, 197 recursive, 314 recursive binary, 314 recursive linear, 314 recursive sequential, 314 ternary, 178 Web,13 Second principle of mathematical induction, 284 Secrets, 265-266 Seed, 206 Sees, 675 Selection operator, 532, 582 Selection sort, 172, 178-179 Self-complementary graph, 620 Self-converse directed graph, 679 Self-dual, 782 Self-generating sequences, 333 Semantics, 785 Sentence, 786, 787 Sequence(s), 149-153 de Bruijn, 682 definition of, 149 finding maximum and minimum of, 475 generating functions for, 484 integer, 151-153 self-generating, 333 strictly decreasing, 351 strictly increasing, 351 Sequential search, 170 Serial algorithms, 606 Series geometric, 155 harmonic, 273 infinite, 157 power, 485-488 formal, 485 Set(s), 111�121 cardinality of, 116, 158-160, 163 complement of, 123, 163 computer representation of, 129-130 countable, 158,218 cut, 746 definition of, Ill, 163 description of, 111-112 difference of, 123, 163 symmetric, 131, 163 disjoint, 122 dominating, 679 elements of, 112, 163 empty (null), 114, 163 equal, 113, 163 finite, 116, 158, 163,500,514 combinations of, 384-385 subsets of counting, 338 number of, 273-274 union of three, number of elements in, 501-503,514 union of two, number of elements in, 500,514 fuzzy, 132-133 complement of, 132 intersection of, 133 union of, 133 guarding, 675 identities of, 124-126 image of, 136 infinite, 116, 159, 163 intersection of, 121-122, 126--128, 163 inverse image of, 147 linearly ordered, 568, 582 members of, 112, 163 not recognized by finite-set automata, 824-825 operations on, 121-13 0 partially ordered, 566, 582 antichain in, 585 chain in, 585 comparable elements in, 567, 582 dense, 581 dual of, 579 greatest element of, 573, 582 Hasse diagram of, 571-572, 573, 582 incomparable elements in, 567, 568, 582 least element of, 573, 582 lower bound of, 574, 582 maximal element of, 572, 582 minimal element of, 572, 573, 582 upper bound of, 574, 582 well-founded, 581 partition of, 560--561, 582 power, 116--117, 163 proofs of facts about, by mathematical induction, 273 recognized by finite-set automata, 819-821 recognized by Turing machines, 830--831 recursively defined, 299-307, 329 regular, 817-819, 820, 821-824, 839 relation on, 118, 521-522 singleton, 114 successor of, 132 symmetric difference of, 131, 163 totally ordered, 568, 582 truth, 119 type, 113 uncountable, 160 union of, 121, 126--128, 163 universal, 113, 163 well-ordered, 332, 568, 582 Set builder, 112 Set notation, with quantifiers, 119 Index Sex, 406 Shaker sort, 172,259 Shamir, Adi, 241, 242 Shannon, Claude Elwood, 749, 750 Sheffer, Henry Maurice, 28, 29 Sheffer stroke, 28,29 Shift cipher, 208 Shift register, 784 Shifting, 224 Shortest-path algorithm, 649-653 Shortest-path problems, 647-655, 676 Sibling of vertex, 686, 743 Sides of a polygon, 288 Sieve of Eratosthenes, 210, 507-509, 514 Simple circuit, 622 Simple directed graph, 591 Simple graphs, 590, 601-604, 675 coloring of, 667 connected planar, 659-663 crossing number of, 666 dense, 614 edges of, 590, 608 isomorphic, 615-618, 676 orientation of, 679 paths in, 622 random, 680-681 self-complemen tary, 620 with spanning trees, 725-726 sparse, 614 thickness of, 666 vertices of, 590, 608 Simple polygon, 288 triangulation of, 288 Simplification rule of inference, 66 Simultaneous linear equations, 256 Singleton set, 114 Sink,841 Six Degrees o/Separation (Guare), 623 Sk-tree, 745 Sloane, Neil, 154 Smullyan, Raymond, 13, 14 Snakes, poisonous, 702 Sneakers (movie), 242 Soccer players, 424 Socks, 353 Software systems, 12 Sollin's algorithm, 743 Solution of a recurrence relation, 450 Solvable problem, 197 Solve a decision problem, 836 Solving using generating functions, counting problems, 488-493 recurrence relations, 493-495 Somerville, Mary, 27 Sort binary insertion, 172, 179 bubble, 173, 257 bidirectional, 173 worst-case complexity of, 194-195 insertion, 172, 174, 257 average-case complexity of, 432-433 worst-case complexity of, 196 merge, 172,317-321,329,475 complexity of, 479 recursive, 318 quick, 172, 322 selection, 172, 178-179 shaker, 172, 259 tournament, 172, 708 Sorting algorithms, 172-174 complexity of, 195-196 topological, 576--578, 582 Space complexity of algorithms, 193 Space probe, 424 Space, sample, 394 Spam, 421-423 Spam filters, Bayesian, 421-423 Spanning forest, 736 minimum, 744 1-16 Index Spanning trees, 724-734, 744 building by breadth-first search, 729-731 by depth-first search, 726-729 definition of, 724 degree-constrained, 746 distance between, 737 in IP multicasting, 726 maximum, 742 minimum, 737-74 1, 744 rooted, 737 Sparse graphs, 614, 741 Sparse matrix, 614 Specifications, system, 12, 4� Spiders, Web, 734 SQL, 535-536 Square matrix, 251 Squarefree integer, 5 13 Stable assignment, 179, 293 Standard deviation, 436 Star height, 840 Star topology for local area network, 605 Start symbol, 787 State diagram for finite-state machine, 798 State table for finite-state machine, 798 State tables, 802 Statement( s), A-IO assignment, A-I 0 -A-II begin, A-II, A-14 blocks of, A-II conditional, 6 for program correctness, 324-326 end, A-II, A-14 "if and only if," 8, 9· ifthen, 6, 8 procedure, A-IO rules of inference for, 71-72 States, 798 reachable, 841 transient, 841 Steroids, 424 Stirling, James, 145 Stirling numbers of the second kind, 378 Stirling's formula, 146 Straight triomino, 100 Strategies minmax, 706, 743 Strategy, proof, 79, 86-102 Strictly decreasing function, 137 Strictly decreasing sequence, 351 Strictly increasing function, 137 Strictly increasing sequence, 351 Strings, 151 bit, IS, 104, 129-130 concatenation of, 300, 804, 839 counting, 336 without consecutive Os, 454-455 decoding, 700-70 I distinguishable, 827 empty, 151,787 generating next largest, 385 indistinguishable, 827 length of, 15, 151,454-455 recursive definition of, 30 I lexicographic ordering of, 568 of parenthe ses, balanced, 332 recognized (accepted), 806, 836 recursively defined, 300 ternary, 458 Strong inductio� 283-285 ,328 Strongly connected components of graphs, 627,676 Strongly connected graphs, 626 Structural induction, 304-306, 329 proof by, 304 Structured query language, 535-536 Structures, recursively defined, 299-307 Subgraph, 607, 678 induced, 678 Subsequence, 351 Subsets, 114, 163 of finite set counting, 338 number of, 273-274 proper, 81, 163 sums of, 732 Subtractors full, 766 half, 766 Subtree, 686, 687, 743 Success, 406 Successor of integer, A-5 of set, 132 Sufficient, 6 secessary and, 9 Suitees, 179 Suitors, 179, 293 optimal for, 293 Sum(s) Boolean, 749-750, 751, 757-759, 781 of first n positive integers, 157, 162,267-268 of functions, 135-136 of geometric progressions, 270-271 of matrices, 247-248 of multisets, 132 of subsets, 732 Sum rule, 338, 386-387 Summation formulae proving by mathematical induction, 267-270 Summation notation, 153 Summations, 153-158 double, 156 index of, 153 lower limit of, 153 proving, by mathematical induction, 267 upper limit of, 153 Sum-of-products expansions, 757-759, 781 simplifying, 766-779 Sun-Tsu, 235 Surjective (onto) function, 137-138, 139, 163 number of, 509-510, 514 Switching circuits, hazard-free, 784 Syllogism disjunctive, 66 hypothet ical, 66 Symbol(s), 787 Landau, 182 start, 787 terminal, 787 Symbolic Logic (Carroll), 44 Symbolic Logic (Venn), liS Symmetric closure of relation, 545, 582 Symmetric difference, of sets, 131, 163 Symmetric matrix, 251, 257 Symmetric relation, 523-524, 581 representing using digraphs, 542 using matrices, 538-539 Syntax, 785 Syracuse problem, 102 System specifications, 12,43-44 consistent, 12 using quantifiers in, 43-44 Tables membership, 126 state, 798, 802 truth, 3 Tautology, 21, 27, 65, 66, 104 Tee-shirts, 344 Telephone call graph, 625 Telephone calls, 594 Telephone lines computer network with diagnostic, 591 computer network with multiple, 590 computer network with multiple one-way, 592 computer network with one-way, 591 Telephone network, 594, 625 Telephone number, 594 Telephone numbering plan, 337 Telescoping sum, 162 ''Ten Most Wanted" numbers, 214 Term, initial, lSI Terminal vertex, 541, 600 Terminals, 787 Ternary expansion, balanced, 230 Ternary search algorithm, 178 Ternary string, 458 Test Lucas-Lehmer, 212 Miller's, 245 primaIity, 412-413 probabilistic primality, 412-413 Turing, 27 Tetrominoes, 104 TeX,I84 Theorem(s ),75, lOS Archimedean property, A-5 Art Gallery, 675 Bayes', 417-425 Binomial, 363-365, 387 Chinese remainder, 235-237, 258 Dirac's, 641 Extended Binomial, 487 Fermat's Last, 100-101,299 Fermat's Little, 239, 258 Four Color, 668-67 1, 676 12-15 Fundamental, Theorem of Arithmetic, 211, 257,285-286 Handshaking, 599 Jordan Curve, 288 Kleene's, 819-821, 839 Kuratow ski's, 663-665, 676 Lame's, 298 Master, 479 methods of proving, 76-83 Multinomial, 382 Ore's, 641, 647 Pick's, 292 Prime Number, 213 Proving, automated, 108 Wilson's, 244 Theory, Ramsey, 352 Thesis, Church-Turing, 833 Thickness of a graph, 666 3x + I conjecture, 101-10 2 Threshold function, 783 Threshold gate, 783 Threshold value, 783 Tic-lac-toe, 705 Tiling of checkerboard, 97-102, 277 Time complexity of algorithms, 193-196 Top-down parsing, 79 1 Topological sorting, 576-578, 582 Topology for local area network hybrid, 605 ring, 605 star, 605 Torus, 666 Total function, 149 Total ordering, 567, 582 compatible, 576, 582 Totally ordered set, 568, 582 Tournament, 680 round-robin, 416, 593 Tournament sort, 172, 708 Tower of Hanoi, 452-454 Trace of recursive algorithm, 312, 3 13 Tractable problem, 197, 836 Trail, 622 Transfer mode, asynchronous, 144 Transient state, 841 Transition function, 798 extending, 805 Transitive closure of relation, 544, 547-550, 582 computing, 550-553 12-16 Transitive relation, 524-525, 527, 581 representing, using digraphs, 542 Transitivity law, A-2 Translating English sentences to logical expressions, 10-11,42-43,56-57 logical expressions to English sentences, 42-43 Transposes of matrices, 251, 257 "Traveler's Dodecahedron," 639, 640 Traveling salesman problem, 641, 649, 653-655,676 Traversal of tree, 710-722, 743 inorder, 712, 714, 715, 718, 743 level-order, 745 postorder, 712, 715-716, 717, 718, 743-744 preorder, 712-713, 718, 743 Tree(s),683-741 applications of, 695-707 AVL,748 binary, 302-304, 686, 687 extended, 302 full, 304-305 binary search, 695-698, 743 binomial, 745 caterpillar, 746 decision, 698--700, 743 definition of, 683 derivation, 790-792, 839 extended binary, 302 family, 683, 684 full binary, 303 full m-ary, 686, 690, 743 game, 704-707 graceful, 746 height-balanced, 748 labeled, 695 m-ary, 686, 743 complete, 694 height of, 692-693 mesh of, 748 as models, 688-690 properties of, 690-693 quad, 748 rooted, 302, 685-688 balanced, 691, 743 binomia l,745 B-tree of degree k, 745 decision trees, 698-700 definition of, 743 height of, 691, 743 level order of vertices of, 745 ordered, 687, 743, 745 Sk-tree, 745 rooted Fibonacci, 695 spanning, 724-734, 744 building by breadth-first search, 729- 731 by depth-first search, 726-729 definition of, 724 degree-constrained, 746 distance between, 737 in IP multicasting, 726 maximum, 742 minimum, 737-74 1, 744 rooted, 737 Tree diagrams, 343-344, 386 Tree edges, 728 Tree traversal, 710-722, 743 inorder, 712, 714, 715, 718, 743 postorder, 712, 715-716, 71 7,718,743-744 preorder, 712-713, 718, 743 Tree-connected network, 689-690 Tree-connected parallel processors, 689-690 Trial division, 214 Triangle inequality, 102 Triangle, Pascal's, 366, 386 Triangulation, 288 Trichotomy Law, A-2 Triominoes, 100, 277, 283 right, 100, 283 straight, 100 Trivial proof, 78, 79, 105 True negative, 419 True positive, 419 Truth set, 119 Truth table, 3-6, 10, 104 for biconditional statement, 9 for conjunction of two propositions, 4 for disjunction of two propositions, 4 for exclusive or of two propositions, 6 for implication, 6 for logical equivalences, 24 for negation of proposition, 3 Truth value, 104 of implication, 6 of proposition, 3 T-shirts, 344 Tukey, John Wilder, 16 Turing, Alan Mathison, 27,176,197, 825, 826 Turing machines, 785, 825, 826, 827-832, 839 in computational complexity, 833 computing functions with, 831-832 definition of, 828-830 nondeterministic, 832 sets recognized by, 830-831 types of, 832 Twin prime conjecture, 215 Twin primes, 215 Two-dimensional array, 606-607 Two's complement expansion, 230-231 Type, 113 Type 0 grammar, 789, 838 Type I grammar, 789, 838--839 Type 2 grammar, 789, 839 Type 3 grammar, 789, 817, 821-824, 839 U1am, Stanisla w, 483 U1am numbers, 165 U1am's problem, 101-10 2,483 Unary representations, 831 Uncountable set, 160 Undefined function, 149 Underlying undirected graph, 601, 675 Undirected edges, 592, 675 of simple graph, 590 Undirected graphs, 592, 599, 675 connectedness in, 624-626 Euler circuit of, 634 Euler path of, 638 orientation of, 679 paths in, 622, 676 underlying, 601, 675 Unicasting, 726 Unicoms, I7 Unicycle, 750 Uniform distribution, 402, 442 Union of fuzzy sets, 133 of graphs, 661,767 of multisets, 132 of sets, 121, 126-128, 163 of three finite sets, number of elements in, 501-503,514 of two finite sets, number of elements in, 500,514 Uniqueness proofs, 92-93, 105 Uniqueness quantifier, 37 Unit (Egyptian) fraction, 331 Unit property, in Boolean algebra, 753 Unit-delay machine; 799-800 United States Coast Survey, 32 UNIVAC, 811 Universal address system, 711 Universal generalization, 70, 71 Universal instantiation, 70, 71 Universal quantification, 34, 105 negation of, 39-41 Universal quantifier, 34-36 Universal set, 113, 163 Universal transitivity, 74 Universe of discourse, 34, 105 Unlabeled boxes, 376 objects, 376 Unless, 6 Unsolvable problem, 197 Upper bound, A-2 oflattice, 586 ofposet, 574, 582 Upper limit of summation, 153 Upper triangular matrix, 260 Index 1-17 U. S. Coast Survey, 32 Utilities-and-houses problem, 657-659 Vacuous proof, 78, 105 Valee-Poussin, Charles-Jean-Gustave-Nicholas de la, 213 Valid argument, 64 argument form, 64 Value(s) expected, 402-403, 426-429, 442 in hatcheck problem, 403 of inversions in permutation, 430-43 1 linearity of, 429-431, 442 final,A-13 initial, A-13 threshold, 783 of tree, 706 of vertex in game tree, 706-707, 743 Vandermonde, Alexandre- Theophile, 368 Vandermonde's identity, 367-368 Variable( s) binding, 38--39 Boolean, 15, 104,750,758,781 free, 38, 105 random, 402, 408-409 covariance of, 442 definition of, 442 distribution of, 408, 442 geometric, 433-434 expected values of, 402-403, 426-429, 442 independent, 434-436, 442 indicator, 440 standard deviation of, 436 variance of, 426-429, 442 Variance, 426,436-43 8,442 Veitch, E. w., 768 Vending machine, 797-798 Venn diagrams, 113, 115, 122, 123, 163 Venn, John, 115 Verb, 786 Verb phrase, 786 Vertex, 288 bipartition of, 602 Vertex basis, 632 Vertex (vertices), 541 adjacent, 598, 600, 675 ancestor of, 686, 743 child of, 686, 687, 743 connecting, 598 counting paths between, 628-629 cut, 625 degree of, in undirected graph, 598 descendant of, 686, 743 of directed graph, 591, 600 of directed multigraph, 592 distance between, 680 eccentricity of, 695 end, 600 in-degree of, 600, 675 independent set of, 680 initial, 541, 600 interior, 551 internal, 686, 743 isolated, 598, 676 level of, 691, 743 1-18 Index Vertex (vertices )-Cont. level order of, 745 of multi graph, 590 number of, of full binary tree, 306 out-degree of, 600, 675 parent of, 686, 743 pendant, 598, 676 of polygon, 288 of pseudograph, 590 sibling of, 686, 743 of simple graph, 590, 612 terminal, 541, 600 of undirected graph, 592, 598, 600 degree of, 598 value of, in game tree, 706-707, 743 Very large scale integration (VLSI) graphs, 682 Vocabulary, 787, 838 "Voyage Around the World" Puzzle, 639 Walk,622 closed, 622 Warshall, Stephen, 551 Warshall's algorithm, 550-553 WAVES, Navy, 811 Weakly connected graphs, 626 Web crawlers, 595 Web graph, 594-595, 734 strongly connected components of, 627 Web page searching, 13 Web pages, 13,595,627, 734 Web spiders, 734 Website for this book, xviii, xxi Weighted graphs, 676 minimum spanning tree for, 738-741 shortest path between, 647-{)53 traveling salesman problem with, 653-{)5 5 Well-formed expressions, 306 Well-formed formula, 301 for compound propositions, 301 of operators and operands, 301 in prefix notation, 724 structural induction and, 305 Well-founded induction, principle of,585 Well-founded poset, 581 Well-ordered induction, 582 principle of, 568-569 Well-ordered set, 332, 568, 582 Well-ordering property, 290-29 1, 328, A-5 WFF'N PROOF. The Game of Modem Logic (Allen),74 Wheels, 601, 676 Whitehead, Alfred North, 114 Wiles, Andrew, 101,239 Wilson's Theorem, 244 Without loss of generality, 89 Witnesses, 181 WLOG (without loss of generality), 89 Word, 787 Word and Object (Quine), 776 World Cup soccer tournament, 362 World Wide Web graph, 594-595 World's record, for twin primes, 215 Worst-case complexity of algorithms, 194, 195-196, 197 XML, 796 XOR,15-16 Yes-or-no problems, 834 Zebra puzzle, 21 Zero property, in Boolean algebra, 753 Zero-one matrices, 252-254, 257 Boolean product of, 252-254 join of, 252 meet of, 252 representing relations using, 538-540 of transitive closure, 549-550 WarshaIl's algorithm and, 550-553 Ziegler's Giant Bar, 184 Zodiac, signs of, 388 12-17 LIST OF SYMBOLS TOPIC COUNTING RELATIONS GRAPHS AND TREES SYMBOL pen, r) C(n, r) (:) C(n;nJ, n2," " nm) peE) peE I F) E(X) VeX) Cn N(P· ... p. ) 'I In N(P!···P!) II In Dn SoR Rn R-J Sc p ... 'I, '2,···,lm Jp(R, S) Do R* [a]R [a]m (S, R) a-<b a::-b a=:(,b a>rb (u, v) G = (V, E) {u, v} deg (v) deg-(v) deg+(v) Kn Cn Wn Qn Km,n MEANING number of r-permutations of a set with n elements number of r-combinations of a set with n elements binomial coefficient n choose r multinomial coefficient probability of E conditional probability of E given F expected value of the random variable X variance of the random variable X Catalan number number of elements having properties Pij,j = 1, ... ,n number of elements not having properties Pij, j = I, ... , n number of derangements of n objects composite of the relations R and S nth power of the relation R inverse relation select operator for condition C projection Jom diagonal relation connectivity relation of R equivalence class of a with respect to R congruence class modulo m poset consisting ofthe set S and partial ordering R a is less than b a is greater than b a is less than or equal to b a is greater than or equal to b directed edge graph with vertex set V and edge set E undirected edge degree of the vertex v in-degree of the vertex v out-degree of the vertex v complete graph on n vertices cycle of size n wheel of size n n-cube complete bipartite graph of size m, n PAGE 355 357 357 382 394 404 426 436 456 506 506 510 526 526 528 532 533 534 545 547 558 558 566 567 567 567 567 541 589 590 598 600 600 601 601 601 602 604 TOPIC SYMBOL MEANING PAGE GRAPHS AND GI UG2 union of G I and G2 608 TREES (cont.) a,XI, " "xn-I,b path from a to b 623 a,XI" ",Xn_l,a circuit 623 r number of regions of the plane 660 deg(R) degree of the region R 661 n number of vertices of a rooted tree 690 i number of internal vertices of a rooted tree 691 I number of leaves of a rooted tree 691 m greatest number of children of an internal vertex in a rooted tree 686 h height of a rooted tree 692 BOOLEAN x complement of the Boolean variable x 749 ALGEBRA x+y Boolean sum of x and y 749 x' y (or xy) Boolean product of x and y 749 B to, I} 750 pd dual of P 754 xly xNANDy 759 x-J,y x NORy 759 x----{>-+x inverter 761 :=I)--+x+y OR gate 761 : =I)----. xy AND gate 761 LANGUAGES A the empty string 151 AND xy concatenation of x and y 300 FINITE-STATE I(x) length of the string x 301 MACHINES wR reversal of W 309 (V,T,S,P) phrase-structure grammar 787 S start symbol 787 W ---+ WI production 787 WI =}W2 W2 is directly derivable from WI 787 * W2 is derivable from WI 787 WI =} W2 < A >::=< B > c I d Backus-Naur form 792 (S, 1,0, j, g, so) finite-state machine with output 798 So start state 798 AB concatenation of the sets A and B 804 A* -1Clecile closure of A 8()i4 (S, I, J, So, P) finite-state machine with no output 805 (S, I, j, so) Turing machine 828