Elements Of Discrete Mathematics 4th Edition Pdf

Reviews

The third edition of Discrete Mathematics with Applications received a Texty Award for Textbook Excellence in June 2005. Epp co-organized an international symposium on teaching logical reasoning, sponsored by the Institute for Discrete Mathematics and Theoretical Computer Science (DIMACS), and she was an associate editor of Mathematics Magazine. Fun Math Math Games Math Made Easy Learn Math Data Science Statistics Mathematics Ebooks Audio Books Management Maths Fun Math Forward Statistics For Dummies, Edition was previously published as Statistics For Dummies, Edition While this version features a new Dummies cover and design, the content is the same as the prior release and should not.


Author: Susanna S. Epp
Pub Date: 2010
ISBN: 978-0-495-39132-6Discrete
Pages: 993
Language: EnglishIts

Discrete Mathematics Applications Pdf 4th

Format: PDF
Size: 10 Mb
Download

Susanna Epp’s DISCRETE MATHEMATICS WITH APPLICATIONS, FOURTH EDITION provides a clear introduction to discrete mathematics. Renowned for her lucid, accessible prose, Epp explains complex, abstract concepts with clarity and precision. This book presents not only the major themes of discrete mathematics, but also the reasoning that underlies mathematical thought. Students develop the ability to think abstractly as they study the ideas of logic and proof. While learning about such concepts as logic circuits and computer addition, algorithm analysis, recursive thinking, computability, automata, cryptography, and combinatorics, students discover that the ideas of discrete mathematics underlie and are essential to the science and technology of the computer age. Overall, Epp’s emphasis on reasoning provides students with a strong foundation for computer science and upper-level mathematics courses.

If there is a material change in our privacy practices, we will indicate on our site that our privacy practices have changed and provide a link to the new privacy policy. Dos mujeres y un camino. Updates and Effective Date Watch TV Show Online reserves the right to make changes in this policy. If you have submitted personal information through our website and would like that information deleted from our records or would like to update or correct that information, please use our Contact Us page.

Auniversal statementsays that a certain property is true for all elements in a set. (For example:All positive numbers are greater than zero.)
Aconditional statementsays that if one thing is true then some other thing also has to be true. (For example:If 378 is divisible by 18, then 378 is divisible by 6.) Given a property that may or may not be true, anexistential statementsays that there is at least one thing for which the property is true. (For example: There is a prime number that is even.)

LetSbe a finite set with at least one element. Astring overSis a finite sequence of elements fromS. The elements of Sare calledcharactersof the string, and the length of a string is the number of characters it contains. Thenull string overSis defined to be the “string” with no characters. It is usually denoted and is said to have length 0.

Structural Introduction for Recursively Defined Sets
LetSbe a set that has been defined recursively, and consider a property that objects in S may or may not satisfy. To prove that every object inS satisfies theproperty:
1. Show that each object in the BASE forSsatisfies the property;
2. Show that for each rule in the RECURSION, if the rule is applied to objects in Sthat satisfy the property, then the objects defined by the rule also satisfy the property.
Because no objects other than those obtained through the BASE and RECURSION conditions are contained inS, it must be the case that every object in Ssatisfies the property.

Elements Of Discrete Mathematics 4th Edition Pdf Pdf

DISCRETE MATHEMATICS TIMELINE c. 300 xc
1500
0 1200
* c. 300 B.C. Euclid published the Euclidean Algorithm in his Elements.
* 1202 Fibonacci introduced the Fibonacci numbers in his Liber Abaci.
* 1575 Francesco Maurolico wrote his Arithmeticorum, which employed the principle of mathematical induction in proofs.
1600
1700 * 1654 Blaise Pascal's famous triangle appeared in A rithntetical triangle.
. 1713 James Bernoulli's Ars Conjectandi introduced several ideas related to permutations and combinations.
. 1654 Blaise Pascal and Pierre Fermat corresponded on the fundamentals of probability theory.
* 1718 Abraham De Moivre wrote about the principle of inclusion-exclusion in his Doctrine of Chances.
* 1666 Gottfried Leibniz published his ideas on combinatorics in Diertatiode Arte Combinatoria.
w1720 Abraham De Moivre began work on generating functions. * 1735 Leonhard Euler introduced functional notation. * 1736 Leonhard Euler published a paper on the KOnigsberg Bridge problem. * 1750 Leonhard Euler proposed his formula for the number of edges, vertices, and faces of polyhedra.
C.300 c.
0
120056
1500
1600
1700
1800
1I 185 0
* 1801 Carl Friedrich Gauss published Disquisitiones Arithmeticae, which outlined congruence modulo m and other number theory topics. a 1844
Gabriel Lame provided an analysis of the complexity of the Euclidean algorithm.
* 1847 Gustav Kirchhoff examined trees in the study of electrical circuits.
I1900
* 1852 Augustus De Morgan wrote William Rowen Hamilton outlining the 4-color problem.
* 1922 Oswald Veblen proved that every connected graph contains a spanning tree.
* 1854 George Boole published An Investigation of the Laws of Thought, which formalized the algebra of sets and logic.
* 1931
* 1855 Thomas P. Kirkman published a paper containing the traveling salesperson problem. a 1856
Denes Kbnig published his paper on matchings in graphs. * 1935 Philip Hall published necessary and sufficient conditions for the existence of a system of distinct representatives.
* 1936
William Rowen Hamilton proposed the traveling salesperson problem.
Denes Kdnig wrote the first book on graph theory, Theorie der Endlichen and Unendlichen Graphen.
a 1857
* 1938
Arthur Cayley introduced the name 'tree' and enumerated the number of rooted trees with n edges.
Claude Shannon devised the algebra of switching circuits and showed its connections to logic.
* 1872 George Boole published A Treatise on the Calculus of Finite Differences.
I 950
2000
* 1951 George Dantzig published his simplex algorithm for solving linear programming problems. * 1953 Maurice Karnaugh introduced the use of Karnaugh maps for the simplification of Boolean circuits. * 1954 G.H. Mealy developed a model for a finite state machine with output. * 1956 Joseph B. Kruskal, Jr., published his algorithm for minimum spanning tree length.
* 1956 Lester R. Ford, Jr., and Delbert R. Fulkerson published their work on maximal flows in a network. * 1957 Robert Prim developed Prim's algorithm. * 1958 PERT algorithm was developed and applied in the construction of the Nautilus submarine. * 1959 Edsger W. Dijkstra published a paper containing Dijkstra's algorithm. * 1976 Kenneth Appel and Wolfgang Haken released their proof of the 4-color theorem. * 1979 L.G. Khachian published his ellipsoidal algorithm for solving linear programming problems in polynomial time.
* 1877 James Joseph Sylvester introduced the word 'graph' in a paper.
* 1881 John Venn introduced the usage of Venn diagrams for reasoning.
* 1984 Narendra Karmarkar devised his interior algorithm for solving classes of linear programming problems. * 1991 Donald Miller and Joseph Pekny published an algorithm for solving a class of traveling salesperson problems. 1800
1850
1900
1950
2000
Discrete Mathematics Fourth Edition John A. Dossey Albert D. Otto Lawrence E.Spence Charles Vanden Eynden Illinois State University
Boston San Francisco New York London Toronto Sydney Tokyo Singapore Madrid Mexico City Munich Paris Cape Town Hong Kong Montreal
For ourfamilies.
Sponsoring Editor Jennifer Aquino Associate Editor RoseAnneJohnson Marketing Manager Michael Boezi Managing Editor Karen Guardino Associate Production Supervisor Cindy Cody Cover Designer Barbara Atkinson Prepress Service Manager Caroline Fell Manufacturing Buyer Evelyn Beaton Compositor TechBooks Printer R.R. Donnelley-Crawfordsville Discrete Mathematics, Fourth Edition Copyright O 2002 byAddison-'AsleyEducational Publishers,lnc. All rights reserved.Printed in the United States of America. No part of this bookmay beusedor reproducedin any manner whatsoevervithoutwritten permissionexceptin thecaseof brief quotations embodiedn critical articles and reviews.For information addressAddison-XAsley Educational Publishers,lnc., OneJacobWay, Reading, MA 01 867. Library of Congress Cataloging-in-Publication Data Discretemathematics/John A. Dossey .. [et al.].-4t*1. p. cm. Includes bibl iographicalreferencesand index. ISBN 0-321 -07912-4 1. Mathematics. 2. Computer science-Mathematick. Dosse~ohn A. QA39.3.D58 511 -dc2l
2001 2001040188
ISBN 0-321-07912-4 02 03 9 8 7
6
5
4
3
Preface Today an increasing proportion of the applications of mathematics involves discrete rather than continuous models. The main reason for this trend is the integration of the computer into more and more of modem society. This book is intended for a one-semester introductory course in discrete mathematics. Prerequisites Even though a course taught from this book requires few formal mathematical prerequisites, students are assumed to have the mathematical maturity ordinarily obtained by taking at least two years of high school mathematics, including problem-solving and algorithmic skills, and the ability to think abstractly. Approach This book has a strong algorithmic emphasis that serves to unify the material. Algorithms are presented in English so that knowledge of a particular programming language is not required. Choice of Topics The choice of topics is based upon the recommendations of various professional organizations, including those of the MAA's Panel on Discrete Mathematics in the First Two Years, the NCTM's Principlesand Standards for SchoolMathematics, and the CBMS 's recommendations for the mathematical education of teachers. Flexibility Although designed for a one-semester course, the book contains more material than can be covered in either one semester or two quarters. Consequently, instructors will have considerable freedom to choose topics tailored to the particular needs and interests of their students. Users of previous editions have reported considerable success in courses ranging from freshman-level courses for computer science students to upper-level courses for mathematics majors. The present edition continues to allow instructors the flexibility to devise a course that is appropriate for a variety of different types of students. Changes in the Fourth Edition At the suggestion of several users of the third edition, additional historical comments have been added; these are included at the end of each chapter. In addition, Chapters 3 and 4 have been rewritten so as to give the breadth-first search algorithm a more prominent role. (It now appears iii
iv
Preface
in Section 3.3 and is used in Sections 3.4 and 4.2.) Many examples in Chapters 3 and 4 have also been rewritten to be more useful to instructors who do not wish to discuss the details of the formal presentations of the algorithms. These examples now precede the algorithms and better reveal how the algorithms work without requiring discussion of the formal algorithms themselves. The previously separate sections on sparning trees and minimal and maximal spanning trees have been combined into a new Section 4.2, and the introductory material on matrices has been removed from Chapter 3 and placed in a new appendix (Appendix B). Another new appendix (Appendix C) describes the looping and branching structures used in the book's algorithms. Additional changes to the exposition have also been made Throughout the book to improve the clarity of the writing. Exercises The exercise sets in this book have been designed for flexibility. Many straightforward computational and algorithmic exercises are included after each section. These exercises give students hands-on practice with the concepts and algorithms of discrete mathematics and are especially important for students whose mathematical backgrounds are weak. Other exercises extend the material in the text or introduce new concepts not treated there. Exercise numbers in color indicate the more challenging problems. An instructor should choose those exercises appropriate to his or her course and students. Answers to odd-numbered computational exercises appear at the end of the book. At the end of each chapter, a set of Supplementary Exercises is provided. These reprise the most important concepts and techniques of the chapter and also explore new ideas not covered elsewhere. ChapterIndependence The sequence of chapters allows considerable flexibility in teaching a course from this' book. The following diagram shows the logical dependence of the chapters. The dashed line indicates that only the initial sections of Chapter 3 are needed fer Chapter 5. Although this book assumes only the familiarity with logic and proof ordinarily gained in high-school geometry, an appendix (Appendix A) is provided for those who prefer a more formal treatment. If this appendix is covered, it may be taught at any time as an independent unit or in combination with Chapter St. (1) An Introduction to Combinatorial Problems and Techniques
[Appendix: An Introduction to Logic and Proof]
(2) Seis, Relations, and Functions
(3) Graphs
(7) Counting Techniques
(8) Recurrence Relations and Generating Functions
(4) Trees
(5) Matching
(6) Network Flows
(9) Combinatorial Circuits and Finite State Machines
Preface
V
Chapters 1 and 2 are introductory in nature. Chapter 1, which should be covered fairly quickly, gives a sampling of the sort of discrete problems the course treats. Some questions are raised that will not be answered until later in the book. Section 1.4 contains a discussion of complexity that some instructors may want to omit or delay until students have had more experience with algorithms. An instructor may wish to cover only the illustrative algorithms in this section that are most relevant to his or her students. Chapter 2 reviews various basic topics, including sets, relations, functions, and mathematical induction. It can be taught more or less rapidly depending on the mathematical backgrounds of the students and the level of the course. It should be possible for students with good mathematics backgrounds to be able to read much of Chapter 2 on their own. The remaining chapters are, as the diagram shows, independent except that Chapters 4 and 6 depend on Chapter 3, and the beginning concepts of Chapter 3 are needed in Chapter 5. PossibleCourses A course emphasizing graph theory and its applications would cover most of Chapters 3-6, while a course with less graph theory would omit Chapters 5 and 6 and concentrate on Chapters 7-9. Two sample three-semesterhour courses along these lines are indicated below. First Course
Second Course
Chapter
Hours
Chapter
Hours
1
4
1 (skip 1.4)
3
2
6
2
6
3
6
3
6
4
7
4
6
5
6
7
8
6
4
8
5
7
8
Appendix
3
9
4
Courses of various levels of sophistication can be taught from this book. For example, the topic of computational complexity is of great importance, and so attention is given to the complexity of many algorithms in this text. Yet it is a difficult topic, and the detail with which it is treated should correspond to the intended level of the course and the preparation of students. Computer Projects Each chapter ends with a set of computer projects related to its content, algorithmic and otherwise. These are purposely stated in general terms, so as to be appropriate to students using various computing systems and languages. Supplements A Student's Solution Manual, available for purchase by students, provides detailed, worked-out solutions to the odd-numbered exercises. To
Vi
Preface
order, use ISBN 0-201-75483-:. An Instructor's Answer Manual, containing answers to all even-numbered computational exercises, is also available. (ISBN 0-201-75482-7).
Acknowledgements
We would like to thank the following mathematics professionals whose reviews guided the preparation of this text: Dorothee Blum, Millersville University; Richard Brualdi, University of Wisconsin, Madison; John L. Bryant, Florida State University; Richard Crittenden, Portland State University; Klaus Fischer, George Mason University; Dennis Grantham, East Texas State University; William R. Har, Clemson University; Christopher Hee, Eastern Michigan University; Frederick Hoffman, Florida Atlantic University; Julian L. Hook, Florida International University; Carmelita Keyes, Broome Community College; Richard K. Molnar, Macalester College; Catherine Murphy, Purdue University, Calumet; Charles Nelson, University of Florida; Fred Schuurmann, Miami University; Karen Sharp, Charles S. Mott Community College; and Donovan H. Van Osdol, University of New Hampshire. The second edition was improved by the helpful comments of our colleagues Saad El-Zanati, Michael Plantholt, and Shailesh Tipnis, and other users of the text throughout the country, as well as by Elaine Bohanon, Bemidji State University; George Dimitroff, Evergreen State College; Richard Enstac, University of Wisconsin-Whitewater; Donald Goldsmith, Western Michigan University; Thomas R. Graviss, Kentucky Educational Network; Gary Klatt, University of Wisconsin-Whitewater; Mark Michael, Kings College; Peter Morris, Shepherd College; Dix H. Pettey, University of Missouri; Matt Pickard, University of Puget Sound; Terry Walters, University of Tennessee; Porter Webster, University of Southern Mississippi; Richard Weimer, Frostburg State University; Thomas Weininger, University of Wisconsin-Eau Claire; and Mark Woodard, Furmian University. The third edition was further improved by the comments of users of the text throughout the country, as well as by the following list of colleague reviewers: Veena Chadha, University of WisconsinEau Claire; Gary Chartrand, West Michigan University; Tilak de Alwis, Southeastern Louisiana University: Ron Dalla, Eastern Washington University; George Dimitroff, Evergreen State College; Gayla S. Domke, Georgia State University; Jerome Eisenfeld, University of Texas at Arlington; Kathleen Elder, Frostburg State University; Klaus Fischer, George Mason University; Donald A. Goral, Northern Virginia Community College; Natasa Jonoska, University of South Florida; Thomas Kiley, George Mason University; TheresalD. Magnus, St. Mary's College; Chris Mawata, University of Tennessee; Robert C. Mers, North Carolina A & T University; Catherine IV. Murphy, Purdue University, Calumet; Anne Quinn, Edinboro University of Pennsylvania; Steen Pedersen, Wright State University; Dix H. Pettey, University of Missouri, Columbia; James L. Richards, Bemidji State University; A. Allan Riveland, Washburn University; Mohan Shrikhande, Central Michigan University; Allan Schwenk, Western Michigan University; and our colleague, Roger Eggleton. We wish to offer special thanks to Michael Plantholt and Dean Sanders of Illinois State University, who independently checked all the algorithms in the third
Preface
Vii
edition for both correctness and readability. Their suggestions led to substantial revisions and improvements of the algorithms. Changes made for this fourth edition were guided by the comments of the following reviewers: Mark Ferris, Midwestern State University; Johanne Hattingh, Georgia State University; Colleen Hoover, St. Mary's College; Jason Miller, Truman State University; and Richard Rockwell, Pacific Union College. We also appreciate the excellent editorial work of Cindy Cody and Kevin Bradley during the production process. John A. Dossey Albert D. Otto Lawrence E. Spence Charles Vanden Eynden
Contents TO THE STUDENT 1
co
2
g
xiii
AN INTRODUCTION TO COMBINATORIAL PROBLEMS AND TECHNIQUES 1 1.1 The Time to Complete a Project 2 1.2 AMatching Problem 10 1.3 A Knapsack Problem 16 1.4 Algorithms and Their Efficiency 23 Historical Notes 35 Supplementary Exercises 36 Computer Projects 39 Suggested Readings 39 SETS, RELATIONS, AND FUNCTIONS
40
2.1 Set Operations 40 2.2 Equivalence Relations 47 2.3 Congruence 54 2.4*Partial Ordering Relations 60 2.5 Functions 72 2.6 Mathematical Induction 84 2.7 Applications 92 Historical Notes 101 Supplementary Exercises 103 Computer Projects 106 Suggested Readings 106
*Sections denoted by an asterisk are optional.
ix
X
Contents
3
+
GRAPHS 108 3.1 Graphs and Their Representations 108 3.2 Paths and Circuits 11 9 3.3 Shortest Paths and Distance 136 3.4 Coloring a Graph 148 3.5 Directed Graphs and Multigraphs 158 Historical Notes 175 Supplementary Exercises 176 Computer Projects 182 Suggested Readings 183
4
+
TREES 184 4.1 Properties of Trees 184 4.2 Spanning Trees 194 4.3 Depth-First Search 210 4.4 Rooted Trees 223 4.5 Binary Trees and Traversals 232 4.6 Optimal Binary Trees in(d Binary Search Trees Historical Notes 265 Supplementary Exercises 266 Computer Projects 269 Suggested Readings 270
5 +
MATCHING 5.1 5.2 5.3 5.4 5.5
6
+
271
Systems of Distinct Representatives 271 Matchings in Graphs 277 A Matching Algorithm 286 Applications of the Algotilhm 296 The Hungarian Method 305 Historical Notes 313 Supplementary Exercises 313 Computer Projects 31 5 Suggested Readings 316
NETWORK FLOWS 317 6.1 Flows and Cuts 317
246
Contents
Xi
6.2 A Flow Augmentation Algorithm 328 6.3 The Max-Flow Min-Cut Theorem 341 6.4 Flows and Matchings 347 Historical Notes 355 Supplementary Exercises 356 Computer Projects 359 Suggested Readings 359 7
c
COUNTING TECHNIQUES
360
7.1 Pascal's Triangle and the Binomial Theorem 360 7.2 Three Fundamental Principles 365 7.3 Permutations and Combinations 374 7.4 Arrangements and Selections with Repetitions 379 7.5 Probability 387 7.6*The Principle of Inclusion-Exclusion 393 7.7*Generating Permutations and r-Combinations 404 Historical Notes 411 Supplementary Exercises 41 2 Computer Projects 415 Suggested Readings 416 8
+
9
g
RECURRENCE RELATIONS AND GENERATING FUNCTIONS 417 8.1 Recurrence Relations 41 7 8.2 The Method of Iteration 429 8.3 Linear Difference Equations with Constant Coefficients 441 8.4*Analyzing the Efficiency of Algorithms with Recurrence Relations 453 8.5 Counting with Generating Functions 465 8.6 The Algebra of Generating Functions 473 Historical Notes 483 Supplementary Exercises 484 Computer Projects 487 Suggested Readings 488 COMBINATORIAL CIRCUITS AND FINITE STATE MACHINES 9.1 Logical Gates 489 9.2 Creating Combinatorial Circuits 499
489
xii
Contents
9.3 Karnaugh Maps 507 9.4 Finite State Machines 521 Historical Notes 530 Supplementary Exercises 531 Computer Projects 534 Suggested Readings 534 APPENDIX A
+
AN A.1 A.2 A.3
INTRODUCTION TO LOGIC AND PROOF Statements and Connectives 535 Logical Equivalence 544 Methods of Proof 548 Historical Notes 554 Supplementary Exercises 556 Suggested Readings 557
APPENDIX B +
MATRICES 558 Historical Notes 564
APPENDIX C +
THE ALGORITHMS IN THIS BOOK BIBLIOGRAPHY
566
572
ANSWERS TO ODD-NUMBERED EXERCISES INDEX
615
535
576
To the Student This book is concerned with the discrete, that is, finite processes and sets of elements that can be listed. This contrasts with calculus, which has to do with infinite processes and intervals of real numbers. Although discrete mathematics has been around for a long time, it has enjoyed a recent rapid expansion, paralleling the growth in the importance of computers. A digital computer is a complicated, but essentially finite, machine. At any given time it can be described by a large, but finite, sequence of Os and Is, corresponding to the internal states of its electronic components. Thus discrete mathematics is essential in understanding computers and how they can be applied. An important part of discrete mathematics has to do with algorithms, which are explicit instructions for performing certain computations. You first learned algorithms in elementary school, for arithmetic is full of them. For example, there is the long division algorithm, which might cause an elementary school student to write down something like the following tableau. 32 13)425 39 35 26 9 Internally, the student is applying certain memorized procedures: There are three 13s in 42, 3 times 13 is 39, 42 minus 39 is 3, bring down the 5, etc. These procedures comprise the algorithm. Another example of an algorithm is a computer program. Suppose a small business wants to identify all customers who owe it more than $100 and have been delinquent in payments for at least 3 months. Even though the company's computer files contain this information, it constitutes only a small portion of their data. Thus a program must be written to sift out exactly what the company wants to know. This program consists of a precise set of instructions to the computer, covering all possibilities, that causes it to isolate the desired list of customers. Our two examples of algorithms are similar in that the entity executing the algorithm does not have to understand why it works. Students in elementary school generally do not know why the long division algorithm gives the correct xiii
Xiv
To the Student
answer, only what the proper steps are. Of course, a computer doesn't understand anything; it just follows orders (and if its orders are incorrect, so that the program is wrong, the computer will dutifully produce the wrong answer). If you are taking a course using this book, however, you are no longer in elementary school and you are a human being, not a computer. Thus you will be expected to know not only how :ur algorithms work, but why. We will investigate some algorithms you probably have never seen before. For example, suppose you are p inning to drive from Miami, Florida, to Seattle, Washington. Even if you stick to the interstate highways, there are hundreds of ways to go. Which way is the shortest? You might get out a map and, after playing around, find a route you though'n was shortest, but could you be sure? There is an algorithm that you could apply to this problem that would give you the correct answer. Better yet, you could program the algorithm into a computer, and let it find the shortest route. That algorithm is explained in this book. We will be interested not only in the how and why of algorithms, but also in the how long. Computer time can be expensive, so before we give a computer a job to do we may want an estimate of how long it will take. Sometimes the surprising answer is that the comT putation will take so long as to make a computer solution impractical, even if we use the largest and fastest existing machines. It is a popular but incorrect idea that computers can do any computation. No computer can take the data from the wo -dIs weather stations and use it to predict future weather accurately more than a few days in advance. The fact that no one knows how to do certain computations efficiently can actually be useful. For example, if n is the product of two primes of about 150 decimal digits, then to factor n takes hundreds of years (even using l'e best methods and computers known), and this is the basis of an important system of cryptography. You have probably already heard a number of times that mathematics is not a spectator sport, and that the only way to learn mathematics is by doing it. There is an important reason we are repeating this advice here. IT'S TRUE! Moreover, it's the best thing we know to tell you. You can't learn to play the guitar or shoot free throws just by watching someone else do these things, and you can't learn discrete mathematics just by reading this book or attending lectures. The mind must be in gear and active. When reading a mathematics book, you should always have paper and pencil handy to work out examples and the details of computations. When attending a mathematics lecture, it is best if you have read the material already. Then you can concentrate on seeing if your understanding of the content agrees with that of the professor, and you can ask questions about any difficult points. Of course, one of the best ways to be active in learning mathematics is by doing exercises. There are many of these in this book. Some are purely computational, others test understanding of concepts, and some require constructing proofs. Answers to odd-numbered computational exercises are in the back of the book, but don't look before you have determined your own answer. If your work consistently gives the same answer as in the back of the book, then you can have confidence that you are on the :-i ght track.
To the Student
XV
Some exercises are harder than others. The more time you spend on such exercises, the more you learn. There is a common notion (reinforced by some courses) that if you can't figure out how to do a problem in five minutes you should go on to the next problem. This attitude becomes less and less relevant the more skillful you become. Very few accomplishments of any importance can be done in five minutes. Many students do not realize the importance of learning the technical language of what they are studying. It is traditional in mathematics to assign special meaning to short, common words such as set,function, relation, graph, tree, network. These words have precise definitions that you must learn. Otherwise, how can you understand what you read in this book, or what your professor is saying? These technical words are necessary for efficient communication. How would you like to explain a baseball game to someone if you were not allowed to use the particular language of that sport? Every time you wanted to say that a pitch was a ball, you would have to say that it was a pitch that was not in the strike zone and that the batter didn't swing at it. For that matter, strike zone is a technical term that would also need an explanation in each instance. Communication on such a basis would be almost impossible. Finally, proper terminology is necessary to share information in a useful way with others. Mathematics is a human endeavor, and human cooperation depends on communication. In the real world, it is seldom sufficient simply to figure something out. You must be able to explain it to other people, and to convince them that your solution is correct. We hope your study of discrete mathematics is successful, and that you get from it techniques and attitudes that you will find useful in many contexts.
An Introduction to Combinatorial Problems and Techniques 1.1 The Time to Complete a Project 1.2 A Matching Problem 1
a A W - - - - ll 1.a A rsnapSaCK rioulem
1.4 Algorithms and Their Efficiency Combinatorial Analysis is an area of mathematics concerned with solving problems for which the number of possibilities is finite (though possibly quite large). These problems may be broken into three main categories: determining existence, counting, and optimization. Sometimes it is not clear whether a certain problem has a solution or not. This is a question of existence. In other cases, solutions are known to exist, but we want to know how many there are. This is a counting problem. Or we may desire a solution that is 'best' in some sense. This is an optimization problem. We will give a simple example of each type.
Four married couples play mixed doubles tennis on two courts each Sunday night. They play for two hours, but switch partners and opponents after each half-hour period. Does a schedule exist so that each man plays with and against each woman exactly once, and plays against each other man at least once?
A six-person investment club decides to rotate the positions of president and treasurer each year. How many years can pass before they will have to repeat the same people in the same offices?
An employer has three employees, Pat, Quentin, and Robin, who are paid $6, $7, and $8 per hour, respectively. The employer has three jobs to assign. The following table shows how much time each employee requires to do each job. 1
2
Chapter I An Introduction to CombinatorialProblems and T'chniques Pat
Quentin
Robin
7.5 hr
6 hr
6.5 hr
Job 2
8 hr
8.5 hr
7 hr
Job 3
5 hr
6.5 hr
5.5 hr
Job 1
How should the employer assign one job to each person to get the work done as cheaply as possible? Often the solution we develop for a combinatorial problem will involve an algorithm, that is, an explicit step-by-step procedure for solving the problem. Many algorithms lend themselv s well to implementation by a computer, and the importance of combinatorial mathematics has increased because of the wide use of these machines. However, even with a large computer, solving a combinatorial problem by simply running through all possible cases is often impossible. More sophisticated methods of attack are needed. In this chapter we will present more complicated examples of combinatorial problems and some analysis of how they might be solved.
1.1
+
THE TIME TO COMPLETE A PROJECT
A large department store is having a Fourth of July sale (which will actually start July 2), and plans to send out an eight-page advertisement for it. This advertisement must be mailed out at least 10 days before July 2 to be effective, but various tasks must be done and decisions made first. The department managers decide which items in stock to put on sale, and buyers decide what merchandise should be brought in for the sale. Then a management committee decides which items tc put in the advertisement and sets their sale prices. The art department prepare s pictures of the sale items, and a writer provides copy describing them. Then the final design of the advertisement, integrating words and pictures, is put together. A mailing list for the advertisement is compiled from several sources, depending on the items put on sale. Then the mailing labels are printed. After the advertisement itself is printed, labels are attached, and the finished product, sorted by zip code, is taken to the post office. Of course, all these operations take time. Unfortunately, it is already June 2, so only 30 days are available far the whole operation, including delivery. There is some concern whether the advertisements can be gotten out in time, and so estimates are made for the number of days needed for each task, based on past experience. These times are l sted in the table below.
1.1
Task
The Time to Complete a Project
Time in Days
Choose items (department managers)
3
Choose items (buyers)
2
Choose and price items for ad
2
Prepare art
4
Prepare copy
3
Design advertisement
2
Compile mailing list
3
Print labels
I
Print advertisement
5
Affix labels Deliver advertisements
3
2 10
If the time needed for all the jobs is added up, we get 37 days, which is more than is available. Some tasks can be done simultaneously, however. For example, the department managers and the buyers can be working on what they want to put on sale at the same time. On the other hand, many tasks cannot even be started until others are completed. For example, the mailing list cannot be compiled until it is decided exactly what items will be advertised. In order to examine which jobs need be done before which other jobs, we label them A, B. . . , K and list after each job any job which must immediately precede it. Task
Preceding Tasks
A
Choose items (department managers)
None
B
Choose items (buyer)
None
C
Choose and price items for ad
A, B
D Prepare art
C
E Prepare copy
C
F
Design advertisement
D, E
*
Compile mailing list
C
H Print labels
G
I
Print advertisement
F
J
Affix labels
H, I
K
Deliver advertisements
J
For example, the letters A and B are listed after task C because the items to be advertised and their prices cannot be decided until the department managers and buyers decide what they want to put on sale. Likewise, the letter C is listed after task D because the art cannot be prepared until the items to be advertised are
4
Chapter 1 An Introduction to CombinatorialProblems and TIchniques
decided. Notice that tasks A and B must also precede the preparation of the art, but this information is omitted because it is implied by what is given. That is, since A and B must precede C, and since C must precede D, logically A and B must go before D also, so this need not be said explicitly. Let us assume that workers are available to start on each task as soon as it is possible to do so. Even so, it is not clear whether the advertisement can be prepared in time, although we have all the relevant information. Here we have a problem of existence. Does a schedule exist that will allow the advertisement to be sent out in time for the sale?
Sometimes a body of information can be understood more easily if it is presented in graphical form. Let us repres nt each task by a point, and draw an arrow from one point to another if the task represented by the first point must immediately precede the task represented byt the second. For example, tasks A and B must precede task C, and C must precede D, so we start as in Figure 1.1. Ae.
D Be FIGURE 1.1
Continuing in this way produces the diagram of Figure 1.2(a). Note that the appearance of the diagram is not uniquely determined. For example, Figure 1.2(b) is consistent with the same information.
(a)
(b) FIGURE 1.2
If we agree that all arrows go from left to right, we can omit the arrowheads, which we will do from now on. This picture makes the whole project seem somewhat more comprehensible, but we must still take into account the time needed to do each task. Let us introduce these times into our diagram in Figure 1.3 by replacing each point with a circle containing the time in days needed for the corresponding task.
1.1
A
G
The Time to Complete a Project
5
H
FIGURE 1.3
Now we will determine the smallest number of days after the start of the whole project in which each task can be finished. For example, task A can be started at once, so it will be done after 3 days. We will write the number 3 by the corresponding circle to indicate this. Likewise, we write a 2 by circle B. How we treat task C is the key to the whole algorithm we will develop. This task cannot be started until both A and B are done. This will be after 3 days, since that is the time needed for A. Then task C will take 2 days. Thus, 5 days are needed until C can be completed, and this is the number we write by the circle for C. So far our diagram looks as in Figure 1.4. A3
G
H
FIGURE 1.4
We carry on in the same fashion. Notice that if more than one line comes into a point from the left, then we add to the time for that point the maximum of all the incoming times to determine when it can first be completed. For example, it will take 9 days until D is finished and 8 days until E is finished. Since task F must wait for both of these, it will not be done for (maximum of 8 and 9) + 2 = 11 days. The reader should check the numbers on the completed diagram in Figure 1.5. A3
G8
H9
C5
2
FIGURE 1.5
6
ChapterI
An Introduction to CombinatorialProblems and Techniques
We see that the advertisement can be produced and delivered in 28 days, in time for the sale! SAgigt Ana{esisL~iQ ( i ifu adV0
The method just described is c ailed PERT, which stands for Program Evaluation and Review Technique. The PERT method (in a somewhat more complicated form) was developed in 1958 for the U.S. Navy Polaris submarine and missile project, although similar techniques were invented at about the same time at the E.I. du Pont de Nemours chem ical company and in England, France, and Germany. Its usefulness in scheduling and estimating completion times for large projects, involving hundreds of steps an(d subcontracts, is obvious, and in various forms it has become a standard industrial technique. Any large library will contain dozens of books on the subject (look. wander PERT, Critical Path Analysis, or Network Analysis). More information may be gleaned from the diagram we have just created. Let us work backward, starting from task K, and see what makes the project take all of 28 days. Clearly it takes 28 days to finish K because it is 18 days until J is completed. Tasks H and I lead into J, but it is the 16 days needed to finish I that is important. Of course, task [ camnot be completed before F is finished. So far we have traced a path back from K to F as shown in color in Figure 1.6. A3 C~' B2
2
G8
H9
Fl
116
D -
J18
K28
E8/
FIGURE 1.6
In the same way, we work back from F to D (since the reason it takes 11 days to finish F is that it cannot be started until the 9 days it takes to complete D), then C, and finally A. The paw A-C-D-F-I-J-K (which is in color in Figure 1.7) is called a critical path. The method of identifying the path is called the critical
path method. A3
G8
3CS B2
H9
D1 a
116
-
FES /
FIGURE 1.7
Ji8 1Fi
K28
7
1.1 The Time to Complete a Project
A critical path is important because the tasks on it are those that determine the total project time. If this time is to be reduced, then some task on a critical path must be done faster. For example, if the mailing list is compiled in 2 days instead of 3, it will still take 28 days to prepare and deliver the advertisement, since compiling the list (task G) is not on a critical path. Shortening the printing time (task I) by a day, however, would reduce the total time to 27 days; I is on the critical path. (Note, however, that changing the time for one task may change the critical path, altering whether or not the other tasks are on it.) A d d;. ta, u!(,0.XA'K Exm A n
The following table gives the steps necessary in building a house, the number of days needed for each step, and the immediately preceding steps. Task A B C D E F G H I J K L M N
Site preparation Foundation Drains and services Framing Roof Windows Plumbing Electrical work Insulation Shell Drywall Cleanup and paint Floors and trim Inspection
Time in Days
PrecedingSteps
4 6 3 10 5 2 4 3 2 6 5 3 4 10
None A A B D E C, E E G, H F 1, J K L I
We prepare the diagram in Figure 1.8 showing times and precedences. Working first from left to right, then from right to left, we find the total times to complete each task, and determine the critical path, which is marked with color in Figure 1.9. The only decision to be made in finding it comes in working back from K, where the 33 days needed to complete J is what is important. We see that a total of 45 days are needed to build the house, and the critical path is A-B-D-E-F-J-K-L-M. A
B
D
E
F
J
K
L
M
A4 B lO D20 E25 F27 J33 K38 L41 M45
C
G
I/
FIGURE 1.8
N
C7 3
H28 G2 2
FIGURE 1.9
It 1
N4 1
8
Chapter I An Introduction to CombinatorialProblems and Techniques
The technique of represen i-ig a problem by a diagram of points, with lines between some of them, is useful in many other situations and will be used throughout this book. The formal study off iuch diagrams will begin in Chapter 3.
EXERCISES 1.1 In Exercises 1-8 use the PERT method to determine the total project time and all the criticalpaths. 1.
A
B
D
CI
E
F
2.
G
B
D
A
EG
C/
3.
A
C
4.
E G
B
5.
D
B AC
D
H
K
.8s
.3
.6
.
A
C
D
G
F
H
j
6.
A
I
8.
H F
C
/D
5}
E
G
FOH
B
An B
G
D
B
7.
E
9
F
A
F
E
H
1.1 The Time to Complete a Project
9
In Exercises 9-16 a table is given telling the time needed for each of a number of tasks and which tasks must immediately precede them. Make a PERT diagramfor each problem, and determine the project time and critical path.
9.
10. Task
Time
Preceding Tasks
Task
Time
Preceding Tasks
A
5
None
A
5
None
B
2
A
B
6
A
C
3
B
C
7
A
D
6
A
D
10
B
E
I
B,D
E
8
B,C
F
8
B,D
F
7
C
C,E,F
G
6
D,E,F
Time
PrecedingTasks
G
4
11
12. Task
Time
Preceding Tasks
A
3
None
A
10
None
B
5
None
B
12
None
C
4
A,B
C
15
None
D
2
A,B
D
6
A,C
E
6
C,D
E
3
A,B
F
7
C,D
F
5
B,C
E,F
G
7
D,F
H
6
D,E
I
9
E,F
G
8
13
Task
14. Task
Time
PrecedingTasks
Task
Time
Preceding Tasks
A
3.3
None
A
6
None
B
2.1
None
B
9
A,D B,I
C
4.6
None
C
10
D
7.2
None
D
8
None
E
6.1
None
E
9
B
F
4.1
A,B
F
13
I
G
1.3
B,C
G
5
C,E,F
H
2.0
F,G
H
9
None
1
6
D,H
1
8.5
D,E,G
J
6.2
E,H
10
Chapter1 An Introduction to CombinatorialProblems ana Techniques
16.
15. Task A
Task
Time
Preceding Tasks
Time
PrecedingTasks
.05
None
A
11
None
13
None
B
.09
A
B
C
.10
A, F
C
12
None
14
None
D
.07
B, C
D
E
.02
None
E
8
A,C,D
F
.04
E
F
6
A,B,D
G
.11
E
G
10
A,B,C
H
.09
F,G
H
5
B,C,D
I
.06
D, H
1
9
E,F,H
J
7
F,G,H
17. A small purse manufacturer has a single machine that makes the metal parts of a purse. This takes 2 minutes. Another single machine makes the cloth parts in 3 minutes. Then it takes a worker 4 minutes to sew the cloth and metal parts together. Only one worker has the ski! I to do this. How long will it take to make 6 purses? 18. What is the answer to the previous problem if the wotker can do the sewing in 2 minutes? 19. A survey is to be made of grocery shoppers in Los An geles, Omaha, and Miami. First, a preliminary telephone survey is made in each city to identify consumers in certain economic and ethnic groups willing to cooperate, and also to determine what supermarket characteristics they deem important. This will take 5 days in Los Angeles, 4 days in Miami, and 3 days in Omaha. After the telephone survey in each city, a list of shoppers to be visited in person is prepared for that city. This takes 6 days for Miami and 4 days each for Omaha and Los Angeles. After all three telephone surveys are made, a standard questionnaire is prepared. This takes 3 days. When the list of consumers to be visited has been prepared and the questionnaire is ready, the questionnaire is administered in each city. This takes 5 days in Los Angeles and Miami and 6 days in Omaha. How long will it take until all three cities are surveyed?
1.2
+
A MATCHING PROBLEM fhe;%11 ProblemWBip
An airline flying out of New York has seven long flights on its Monday morning schedule: Los Angeles, Seattle, London, Frankfort, Paris, Madrid, and Dublin. Fortunately, seven capable pilots are available: Alfors, Timmack, Jelinek, Tang, Washington, Rupp, and Ramirez. There is a complication, however. Pilots are allowed to request particular destinations, and these requests are to be honored if possible. The pilots requesting each city are listed below. Los Angeles: Timmack, Jelinek, Rupp Seattle: Alfors, Timmack Tang, Washington London: Timmack, Tang, Washington Frankfort: Alfors, Tang, Rupp, Ramirez
1.2 A Matching Problem
l1
Paris: Jelinek, Washington, Rupp Madrid: Jelinek, Ramirez Dublin: Timmack, Rupp, Ramirez This information could also be represented by a diagram (Figure 1.10(a)), where we draw a black line between a city and a pilot if the former is on the pilot's request list. Los Angeles
Alfors
Seattle
Timmack
London
Jelinek
Frankfort
Tang
Paris
Washington
Madrid
Rupp
Dublin
Ramirez (a)
Los Angeles
-
v
Seattle *
Alfors * Timmack
London *
* Jelinek
frankfort *
Tang
Paris *
Washington
Madrid * Dublin
* Rupp -
e
Ramirez
(b) FIGURE 1.10
The person assigning the flights would like to please all the pilots if this can be done, and if not, would like to accommodate as many as possible. This may be thought of as an optimization problem. We desire a matching of pilots with flights such that the number of pilots who get flights they have requested is as large as possible.
Let us start with a very crude attack on our matching problem. We could simply list all possible ways of assigning one pilot to each flight, and count for each the
12
Chapter] An Introduction to CombinatorialProbleo'.s au' Techniques
number of pilots who are assigned to flights they requested. For example, one matching would be to take the flights and pilots in the order they were listed. Flight
Pilot
Requested?
Los Angeles
Alfors
No
Seattle
Timmack
Yes
London
Jelinek
No
Frankfort
Tang
Yes
Paris
Washington
Yes
Madrid
Rupp
No
Dublin
Ramirez
Yes
This matching is indicated by he colored lines in Figure 1.10(b). Here four of the pilots would get flights they want, but perhaps a different matching would do even better. If we agree always to list the flights in the same order, say that of our original list, then an assignment will be determined by some arrangement of the seven pilots' names. For example, the arrangement Timmack, Alfors, Jelinek, Tang, Washington, Rupp, Ramirez would send Timmack to Los Angeles and Alfors to Seattle, while assigning the same pilots to the other flights is previously. Likewise, the arrangement Ramirez, Rupp, Washington, Tang, Jelinek, Timmack, Alfors would send Ramirez to Los Angeles, Rupp to Seattle, etc. The reader should check that this matching will accommodate only three pilots' wishes. Several questions come lo mind concerning our plan for solving this problem. (1) (2)
How much work will this be? In particular, how many arrangements will we have to check? How can we generate all possible arrangements so that we are sure we have not missed any?
The second question is somewhat special, and we will not answer it until Chapter 7, but the first quest cn is easier. (Note that it is a counting problem.) In order to make the count., we will invoke a simple principle that will be useful many times in this book. Pi'D
4C XD
Consider a procedure that is composed of a se-
quence of k steps. Suppose that the first step can be performed in nI ways, and for each of these the second ste 3 can be performed in n2 ways, and, in general, no matter how the preceding steps are performed, the ith step can be performed in ni ways (i = 2, 3, . . , k). Then the number of different ways in which the entire procedure can be performed is n I n2
..
nk
1.2 A Matching Problem
13
Example 1.1 A certain Japanese car is available in 6 colors, with 3 different engines and either a manual or automatic transmission. What is the total number of ways the car can be ordered? We can apply the multiplication principle with k = 3, n1 = 6, n2 = 3, and n3 = 2. The number of ways is (6)(3)(2) = 36. + Now we return to the problem of counting the number of ways the 7 flights can be assigned. Let us start with the Los Angeles flight. There are 7 pilots who can be assigned to it. We pick one, and turn to the Seattle flight. Now only 6 pilots are left. Choosing one of these leaves 5 from which to pick for the London flight. We can continue in this manner all the way to the Dublin flight, at which time only I pilot will be left. Thus, the total number of matchings we can devise will be7 .6. 5 4 3 .2 1. The same argument will work whenever we have the same number of flights and available pilots, producing n(n - 1)(n-2).. 3.2.1 possible matchings if there are n flights and n pilots.
The reader is probably aware that there is a shorter notation for a product of the type we just developed. If n is any nonnegative integer, we define n factorial, which is denoted by n!, as follows: O! = l, l! = l, 2! = 1 2, and,ingeneral, n! = 1- 2..
(n -)n.
Notice that if n > 1, then n! is just the product of the integers from 1 to n. By a permutation of a set of objects, we mean any ordering of those objects. For example, the penrmutations of the letters a, b, and c are: abc, acb, bac, bca, cab, cba. The analysis of the number of matchings when there are n flights and n pilots can be modified to prove the following result. Theorem 1.1
There are exactly n! permutations of a set of n objects. There is a generalization of the idea of apermutation that often arises. Suppose in the flight assignment problem the flights to Madrid and Dublin are cancelled because of bad weather. Now 7 pilots are available for the 5 remaining flights. There are 7 ways to choose a pilot for the Los Angeles flight, then 6 pilots to choose from for the Seattle flight, etc. Since only 5 pilots need be chosen, there
14
Chapter] An Introduction to CombinatorialProblen',.A a it' techniques
are a total of 7 6 5 4 .3 possible ways to make the assignments. (Notice that this product has 5 factors.) The same argument works in general. The number of ways an ordered list of r objects can be chosen without repetition from n objects is
Theorem 1.2
n~nn - r + 1)=(n n(n -- 1)(n
)
Proof: The first object can be chosen in n ways, the second in n -1 ways, etc. Since it is easy to check that the product on the left has r factors, by the multiplication principle it counts the total number of arrangements. As for the second expression, note that n(n-) .. (n -rn(n - 1).
)
r + l)(n - r)(n - r - 1).. 2 1 (ae-r)(n- r - 1) .. 2l -
n! (n -r)! '
Example 1.2 The junior class at Taylor High School is to elect a president, vice-president, and secretary from among its 30 members. How many different choices are possible? We are to choose an ordered list of 3 officers from 30 students. The number of possibilities is
30 29 28 = 24,360.
v
The number of ordered lists of r objects which can be chosen without repetition from n objects is denoted by P(n, r). These lists are called permutations of n objects, taken r at a time. For example, we have just seen that P (30, 3) 24,360, and, in general, according to the last theorem, -
P (n, r) =
hae Pracfica. Rfil, a,@ OpU' Ou,{l
-r) (n-)
4giot7l,7 ii
the Ai.r inle ProhWl;,etnp9
We were going to run through all the ways of assigning a pilot to each of the 7 flights to see which would please the most pilots. We now know the number of possible assignments is 7! = 'i040. This number is large enough to discourage us from trying this method by hand. If a computer were available, however, the
1.2 A Matching Problem
15
method would look more promising. We would need a way to tell the computer how to generate these 5040 permutations, that is, an algorithm. This would amount to an explicit answer to question (2) asked earlier. Of course, 7 flights and 7 pilots are really unrealistically small numbers. For example, at O'Hare field in Chicago an average of more than 1100 airplanes take off every day. Let us consider a small airline with 20 flights and 20 pilots to assign to them, and consider the practicality of running through all possible assignments. The number of these is 20!, which we calculate on a hand calculator to be about 2.4. 1018. This is a number of 19 digits, and a computer is apparently required. Let us suppose our computer can generate one million assignments per second and check for each of them how many pilots get their requested flights. How long would it take to run through them all? The answer is not hard to calculate. Doing 2.4. 1018 calculations at 1,000,000 per second would take
2.4. 1018= 2.4. 1012 seconds, 1,000,000
or or or or
2.4 1012
60
= -10 4
4~ 1010 60 60
minutes,
6.7. 108 hours,
6.7 108
24
2.8
2.8. i07 2.8 1
0 days,
7.6. 104 years.
365
The calculation would take about 76,000 years, just for 20 flights and 20 pilots. The point of this calculation is that even with a computer you sometimes have to be clever. In Chapter 5 we will explain a much more efficient way to solve our matching problem. This method will allow a person to handle 7 flights and 7 pilots in a few minutes, and a computer to deal with hundreds of flights and pilots in a reasonable time. EXERCISES 1.2 In Exercises 1-16 calculate the number shown. 7! 4!
8! 3!
2. 6!
3. !
4. -
2!8.
6. 9!
7. P(7,4)
8. P(8,4)
2! 6!
3! 6!
9. P(10, 7)
10. P(1l,9)
11.
14. P(7, 7)
15. P(8, 3) P(3, 3)
1. 5!
13. P(6, 6)
5!
4(9 )
12.
(I0, 2) 3!5!
16. P (9, 5) P(5, 5)
16
Chapter 1
An Introduction to CombinatorialProblems anm Techniques
17. A baseball manager has decided who his 9 starting hitters are to be, but not the order in which they will bat. How many possibilities are there? 18. A president, vice president, and treasurer are to be chosen from a club with 7 members. In how many ways can this be done? 19. A music company executive must decide the order in which to present 6 selections on a compact disk. How many choices does she have? 20. A Halloween makeup kit contains 3 different moustaches, 2 different sets of eyebrows, 4 different noses, and a set of ears. (It is not necessary to use any moustache, etc.) How many disguises using at least one of these items are possible? 21. A man has 5 sport coats, 4 pairs of slacks, 6 shirts, and [ tie. How many combinations of these can he wear, if he must wear at least slacks and a shirt? 22. Different prizes for first place, second place, and third pl ice are to be awarded to 3 of the 12 finalists in a beauty contest. How many ways is this possible? 23. Seven actresses have auditioned for the parts of the three daughters of King Lear: Goneril, Regan, and Cordelia. In how many ways can the roles be filled? 24. A farmer with 7 cows likes to milk them in a different order each morning. How many days can he do this without repeating? 25. A busy summer resort motel has 5 empty rooms and 3 1ravelers who want rooms. In how many ways can the motel manager assign a room of his or her own to each guest? 26. An Alaskan doctor visits each of 5 isolated settlements by plane once a month. He can use either of two planes, but once he starts out he visits all 5 settlements in some order before returning home. How many possibilities are there? 27. A tennis coach must pick her top 6 varsity and top 6 junior varsity players in order from among 9 varsity and 11 junior varsity players. In how many ways is this possible? 28. A dinner special for 4 at a Chinese restaurant allows one shrimp dish (from 3), one beef dish (from 5), one chicken dish (from 4), and one pork dish (from 4). Each diner can also choose either soup or an egg roll. How many different orders might be sent to the kitchen? 29. How many ways can 6 keys be placed on a circular key ring? Both sides of the ring are the same, and there is no way to tell which is the 'first' key on the ring. 30. Show that if n > 1, then P(n, 2) = n2 _ n. 31. Show that if n > 0, then P(n, n- 1) = n!. 32. Show that if 0 < 2r < n, then P(n
r)
P(n, r)
1.3
c
, r)
A KNAPSACK PROBLEM
A U.S. shuttle is to be sent to a space station in orbit around the earth, and 700 kilograms of its payload are allotted to experiments designed by scientists. Researchers from around the country apply for the inclusion of their experiments. They must specify the weight of the equipment they want taken into orbit. A
1.3 A Knapsack Problem
17
panel of reviewers then decides which proposals are reasonable. These proposals are then rated from 1 (the lowest score) to 10 (the highest) on their potential importance to science. The ratings are listed below. Experiment
Weight in Kilograms
Rating
I
Cloud patterns
36
5
2
Speed of light
264
9
3
Solar power
188
6
4
Binary stars
203
8
104
8
7
6
5 Relativity 6
Seed viability
7
Sun spots
92
2
8 Mice tumors
65
8
9
25
3
Weightless vines
10
Space dust
170
6
11
Cosmic rays
80
7
12
Yeast fermentation
22
4
It is decided to choose experiments so that the total of all their ratings is as large as possible. Since there is also the limitation that the total weight cannot exceed 700 kilograms, it is not clear how to do this. If we just start down the list, for example, experiments 1, 2, 3, and 4 have a total weight of 691 kilograms. Now we cannot take experiment 5, since its 104 kilograms would put us over the 700-kilogram limit. We could include experiment 6, however, which would bring us up to 698 kilograms. The following table shows how we might go down the list this way, keeping a running total of the weight and putting in whichever experiments do not put us over 700 kilograms. Weight
Include?
Total Weight
Rating
36
Yes
36
5
264
Yes
300
9
3
188
Yes
488
6
4
203
Yes
691
8
Experiment 2
5
104
No
691
6
7
Yes
698
7
92
No
698
8
65
No
698
9
25
No
698
10
170
No
698
11
80
No
698
12
22
No
698
6 -
18
Chapter I
An Introduction to CombinatorialProbleirsand Techniques
Note that the ratings total of thz experiments chosen this way is 5 -9 + 6 + 8 + 6
=
34.
The question is whether we can do better than this. Since we just took the experiments as they came, without paying any attention to their ratings, it seems likely that we can. Perhaps it would be better to start with the experiments with the highest rating and include as many of them as we can, then go on to those with the next highest rating, and so on. If two experiments have the same rating, we would naturally choose the lighter one first. The following table shows how such a tactic would work. Experiment
Razing
Weight
Include?
Total Weight
2
'
264
Yes
264
8
8
65
Yes
329
5
8
104
Yes
433
4
8
203
Yes
636
80
No
636
11 6
0
7
Yes
643
10
6
170
No
643
3
6
188
No
643
1
36
Yes
679
12
22
No
679
9
25
No
679
7
92
No
679
Using this method, we choose experiments 2, 8, 5, 4, 6, and 1, giving a rating total of 9 + 8 + 8 + 8 + 6 + 5 = 44. This is 10 better than our previous total, but perhaps it can be improved further. Another idea would be to start with the experiment of smallest weight (number 6), then include the next lightest (number 12), and so on, continuing until we reach the 700-kilogram limit. The reader should check that this would mean including experiments 6, 12, 9, 1, 8, 11. '7, 5, and 10 for a rating total of 49, which is still better. Yet another idea would be to compute a rating points-per-kilogram ratio for each experiment, and to include, whenever possible, the experiments for which this ratio is highest. We will illustrate this idea with a case where only three experiments are submitted, with the limit of 700 kilograms still in effect.
1.3 A Knapsack Problem Experiment
Weight
Rating
19
Ratio 8 9
1
390
8
2
350
6
3
3
340
5
5 -. 0147 34 0-
6
-. 0205 - .0171
Using our new scheme, we would choose experiment 1, since it has the highest ratio, and then not be able to include either of the other two. The total rating would be 8. But this is not as good as choosing experiments 2 and 3 for a rating total of 11. The ratio method does not assure us of the best selection either. It turns out that if this method is applied to our original 12 experiments, it yields a subset of 9 experiments with a total rating of 51. (See Exercise 19.) Even this is not the optimal subset, however.
We could play around with this problem, taking experiments out and putting experiments in, and perhaps find a collection of experiments with a higher rating total than 51. Even then, it would be hard for us to be sure we could not do even better somehow. Notice that this is another optimization problem. We want to find a selection of experiments from the 12 given whose total weight is no more than 700 kilograms and whose rating total is as large as possible. As in the case of the matching problem of the previous section, we will turn to the tedious method of trying all the possibilities. Getting a computer to do the calculations, even if there are many experiments, might be a practical way to attack the problem. Since the experiments are numbered from 1 to 12, we will save time by simply using the numbers. We will introduce some language (with which the reader is probably already familiar) in order to state the problem in a compact way. We need the idea of a set. Although we cannot give a definition of a set in terms of simpler ideas, we think of a set as a collection of objects of some sort such that, given any object, we can tell whether that object is in the set or not. If the object x is in the set S, we write x C S, and if not, we write x § S.
Example 1.3 Let P be the set of all presidents of the United States. Then George Washington E P. but Benjamin Franklin V P.
20
Chapter I
An Introduction to CombinatorialProblems and Techniques
If U is the set of all integers from 1 to 12, then
5 e U, but 15U.
4U
If a set has only a finite number of objects, one way to define it is simply to list them all between curly braces. For example, the set U of the example could also be defined by U = I1,2,3,4,5,6,7,8,9, 10, 11, 121. If the set has more elements than we care to list, we may use three dots to indicate some elements. For example, we could also write U := {1,2,3,.., 11, 12}. Another way to express a set is to enclose inside curly braces a variable standing for a typical element of the sei:, followed by a colon, followed by a description of what condition or conditions the variable must satisfy in order to be in the set. For example, U = {x:
x
is an integer and 0 < x < 131,
and P = {x: x is a president of the U.S.}. The latter expression is read 'the set of all x such that x is a president of the United States.' In these two ex amples the use of x for the variable is arbitrary; any other letter having no previous meaning could be used just as well. Let A and B be sets. We say that A is a subset of B, and write A C B. if every element of A is also in B. In this case, we also say that A is contained in B and that B contains A. Aln equivalent notation is
B D A. +
Example 1.4 If U is the set defined above ani =1, 2,3,4,61, then T
C
U. Likewise, if C = {Lincoln, A. Johnson, Grant),
then C C P, where, as before, P is the set of all American presidents. On the other hand, P C C is false. +
1.3 A Knapsack Problem
21
If A is a finite set, we will denote by IAI the number of elements in A. For example, if C, T,U, and P are as defined above, we have ICI = 3, ITI = 5, jUI = 12, and (in 2001) IPI = 42. (Although George W. Bush is often listed as the forty-third president of the United States, this number is achieved by counting Grover Cleveland twice, because Benjamin Harrison was president between Cleveland's two terms. But an element is either in a set or is not; it cannot be in the set more than once.) The empty set is the set that has no elements at all. We denote it by 0. Thus, if A is a set, then A = 0 if and only if IAI = 0. We say that two sets are equal if every element in the first is also in the second and, conversely, every element in the second is also in the first. Thus A = B if and only ifA C B andB C A.
Armed with the language of sets, we return to the question of selecting experiments. The set of all experiments corresponds to the set U = {1,2,.., 11, 121, and each selection corresponds to some subset of U. For example, the choice of experiments 1, 2, 3, 4, and 6 corresponds to the subset T = {1, 2,3,4, 6}. This happens to be the selection of our first attempt to solve the problem, with a rating total of 34. Of course, some subsets of U are unacceptable because their total weight exceeds 700 kilograms. An example of such a subset is {2, 3, 4, 10}, with a total weight of 825 kilograms. We could simply go through all the subsets of U, computing for each its total weight. If this does not exceed 700, then we will add up the ratings of the corresponding experiments. Eventually we will find which subset (or subsets) has the maximal rating total. As in the last section, two questions arise: (1) (2)
How many subsets are there? (Another counting problem.) How can we list all the subsets without missing any?
We will start with problem (1), saving problem (2) for Section 1.4. Let us start with some smaller sets to get the idea. Set
Subsets
11)
0,111
2
(1,21 11,2,31
0,1tl,{21,{1,21 0,1l),(2),{1,2},{3),f1,3),12,31,{1,2,3}
4 8
Number of Subsets
22
Chapter ]
An Introduction to CombinatorialProblemrs a,I'Techniques
We see that a set with 1 element has 2 subsets, a set with 2 elements has 4 subsets, and a set with 3 elements has 8 subsets. This suggests the following theorem, which will be proved in Section 2.7. Theorem 1.3
A set with n elements has exactly
2n
subsets.
The set U has 12 elements. and so by the theorem it has exactly 212 = 4096 subsets. This is more than we would like to run through by hand, but it would be easy enough for a computer. In fact, as n gets large, the quantity 2n does not grow as fast as the quantity n! that arose in the previous section. For example, 220 is only about a million. Our hypothetical computer that could check one million subsets per second could run through the possible selections from 20 experiments in about a second, which is considerably less than the 76,000 years we found it would take to check the 20! ways of assigning 20 pilots to 20 flights. Even so, 2 n can get unreasonably large Thor modest values of n. For example, 250 is about 1.13. 1015, and our computer would take about 36 years to run through this number of subsets. Our problem of choosing experiments is an example of the knapsack problem. The name comes from the idea of a hiker who has only so much room in his knapsack, and must choose which items-food, first aid kit, water, tools, etc.-to include. Each item takes up a certain amount of space and has a certain value to the hiker, and the idea is to choose items that fit with the greatest total value. In contrast to the matching problem of the previous section, there is no efficient way known to solve the knapsack problem. What exactly is meant by an 'efficient way' will be made clearer when complexity theory is discussed in the next section.
EXERCISES 1.3
101
In Exercises 1-14 let A = {1, 2), B = {2, 3, 41, C {', D = {x: x is an odd positive integer) and E = {3, 4). Tell whether each statement is true orfalse. 1. ACB D 9. 2 E {C, El 13. 0EA 5.
106
E
2. CCA 6. C E B 10. I C, E}I = 2 14. 0C{C, EI
3. 2cA 7. A C A 1I. 112, 3, 4, 3, 2}1 = 5
4. BCD 8. B ={C, E) 12. 1{0, 0)1 =I
In Exercises 15-18 the given sets represent a selection of space shuttle experimentsfrom among the 12 given in the text. Determine whether each selection is acceptable (i.e., not over 700 kilograms). If it is, thenfind the total rating. 15. {2,3,9,10, 121
16. [2,3,9, 10, 11)
17. 12,4,6,7,9, 11}
18. 12,3,4,6,91
19. Suppose that the rating/kilogram ratio is computed fbr each of the 12 proposed space shuttle experiments. Experiments are chosen by including those with the highest ratio that do not push the total weight over 700 kilograms. What set of experiments does this produce, and what is their total rating?
1.4 Algorithms and Their Efficiency
20. 21. 22. 23. 24. 25.
26. 27. 28. 29. 30.
23
List the subsets of {1,2, 3, 4). How many are there? How many subsets does {Sunday, Monday, .. , Saturday) have? How many subsets does (Dopey, Happy,.., Docl have? How many subsets does {Chico, Harpo, Groucho, Zeppo, Gummol have? How many subsets does {13,14,..,22} have? Suppose m and n are positive integers with m < n. How many elements does {m, m + 1, n I have? How many subsets does {2, 4, 8,.., 2561 have? A draw poker player may discard some of his 5 cards and be dealt new ones. The rules say he cannot discard all 5. How many sets of cards can be discarded? Suppose in the previous problem no more than 3 cards may be discarded. How many choices does a player have? How long would it take a computer that can check one million subsets per second to run through the subsets of a set of 40 elements? Find a subset of the 12 experiments with a total weight of 700 kilograms and a total rating of 49.
1.4
+
ALGORITHMS AND THEIR EFFICIENCY
In previous sections we developed algorithms for solving certain practical combinatorial problems. We also saw that in some cases solving a problem of reasonable size, even using a high-speed computer, can take an unreasonable amount of time. Obviously, an algorithm is not practical if its use will cost more than we are prepared to pay, or if it will provide a solution too late to be of value. In this section
we will examine in more detail the construction and efficiency of algorithms. We assume a digital computer will handle the actual implementation of the algorithms we develop. This means a precise set of instructions (a 'program') must be prepared telling the computer what to do. When an algorithm is presented
to human beings, it is explained in an informal way, with examples and references to familiar techniques. (Think of how the operation of long division is explained to children in grade school.) Telling a computer how to do something requires a more organized and precise presentation. Most computer programs are written in some specific higher-level computer language, such as FORTRAN, BASIC, COBOL, or C. In this book we will not write our algorithms in any particular computer language, but rather use English
in a form that is sufficiently organized and precise that a program could easily be written from it. Usually this will mean a numbered sequence of steps, with precise instructions on how to proceed from one step to the next. See Appendix C for an explanation of the technical terms in our algorithms. Of course a big, complicated problem will require a big, complicated solution, no matter how good an algorithm we find for it. For the problem of choosing an
24
Chapter] An Introduction to CombinatorialProblems ai 'Techniques
optimal set of experiments to place in a space shuttle, introduced in Section 1.3, choosing from among 12 submitted experiments requires looking at about 4000 subsets, while if there are 20 experiments, then there are about 1,000,000 subsets, 250 times as many. A reasonable measure of the 'size' of the problem in this example would be n, the number of experiments. For each type of problem we may be able to identify some number n that measures the amount of information upon which a solution must be based. Admittedly, choosing the quantity to be labeled n is often somewhat arbitrary. The precise choice may not be important, however, for the purpose of comparing two algorithms that do the same job, so long as n represents the same quantity in both algorithms. We will also try to measure the amount of work done in computing a solution to a problem. Of course this will depend on n, and for a desirable algorithm it will not grow too quickly as n gets larger. We need some unit in which to measure the size of an algorithmic solution. In the space shuttle problem, for example, we saw that a set of n experiments has 2n subsets that need to be checked. 'Checking' a subset itself involves certain computations, however. The weights of the experiments in the subset must be added to see if the 700-kilogram limit is exceeded. If not, then the rating, of the subset must be added and compared with the previous best total. How much work this will be for a particular subset will also depend on the number of experiments in it. We will take the conventio al course of measuring the size of an algorithm by counting the total number of elementary operations it involves, where an elementary operation is defined as the addition, subtraction, multiplication, division, or comparison of two numbers. For example, adding up the k numbers a, a2 , .. , ak involves k - 1 additions, as we :ompute
al + a2, (a, + a2) . a3, .. .
(a, +
+ ak-1) + ak
We will call the total number of elementary operations required the complexity of the algorithm. There are two disadvantages to measuring the complexity of an algorithm this way. (1)
(2)
This method essentially tries to measure the time it will take to implement an algorithm, assuming that each elementary operation takes the same time. But computers are also limited by their memory. An algorithm may require storage of more data than a given computer can hold. Or additional slower memory may have to be used, thereby slowing down the process. In any case, computer storage itself has a monetary value that our simple counting of elementary operations dces not take into account. It may be that not all operations take the same amount of computer time; for example, division may take longer than addition. Also the time an elementary operation takes may depend on the size of the numbers involved; computations with larger numbers take longer. Just the assignment of a value
25
1.4 Algorithms and Their Efficiency
to a variable also takes computer time, time which we are not taking into account. In spite of the criticisms that can be made of our proposed method of measuring the complexity of an algorithm, we will use it for simplicity and to avoid considering the internal operations of particular computers.
We will consider some examples of algorithms and their complexity. Let us start with the problem of evaluating x', where x is some number and n is a positive integer. To break this down into elementary operations, we could compute x2 = x x, then x 3 = (X 2 )X, . . .,until we get to x4. Since computing x2 takes 1 multiplication, computing x3 takes 2 multiplications, etc., a total of n - 1 multiplications is necessary. An algorithm for this process might be as follows.
Algorithm for Evaluating x' Given a real number x and a positive integer n, this algorithm computes P
=
x .
Step I (initialization) Set P = x and k = 1 Step 2 (next power) while k < n (a) Replace P with Px. (b) Replace k with k + 1. endwhile Step 3 (output P = xn) Print P.
Notice that step 2 entails n - 1 multiplications and n - 1 additions. There are also n comparisons, since we only exit step 2 when k = n. Thus we see that computing x' with this algorithm involves a total of 3n - 2 elementary operations. Since our method of estimating the number of operations in an algorithm involves various inaccuracies anyway, usually we are not interested in an exact count. Knowing that computing xn takes about 3n operations, or even a number of operations that is less than some constant multiple of n, may satisfy us. We are mainly interested in avoiding, when possible, an algorithm whose number of operations grows very quickly as n gets large. Later in this book we will often write algorithms in a less formal way that may make impractical an exact count of the elementary operations involved. For
26
Chapter] An Introduction to CombinatorialProblems ond Techniques
example, in the last algorithm, instead of incrementing k by 1 at each step and then comparing its new value to thai of n, we might simply have said something such as 'for k = I to n -1 replace jO by Px.' High level computer languages usually allow loops to be defined by some such language. If the number of operations required for each value of k does not exceed some constant C, then the complexity of the algorithm does not exceed Cn. (We could actually take C = 3 for the algorithm just presented.) Ofte:i knowing the precise value of C is not important to us. We will indicate why tins is true later in this section, after we have more examples of algorithms. Now we will give an example of two algorithms with the same purpose, one of which is more efficient than the other. By a polynomial of degree n in x we mean an expression of the form P(x) = aX' +an-
1 xn-l
+
+ ax + ao,
where an, an-, .. , a are constants and an # 0. Thus, a polynomial in x is a sum of terms, each of which is either a constant or else a constant times a positive integral power of x. We will consider two algorithms for computing the value of a polynomial. The first one, w& hich may seem the more natural, will start with ao, then add a1x to that, then add Cf2x2 to that, etc. ..
. ... ... ... ..
. .... D:,. .m. m. . D ..D. m eD.
Polynomial Evaluation Algorithm Thisalgorithmcomputes P(x) anx'n + an-Xn- 1 integer n and real numbers a,co, a . , a,.
+
*
+
ao,giventhenonnegative
Step I (initialization) Set S = ao andk = 1. Step 2 (add next term)
while k < n (a) Replace S with S + akxk. (b) Replace k with ic + 1.
endwhile Step 3
(output P(x) = S) Print S.
In this algorithm we will check whether k < n in step 2 a total of n + 1 times, with k = 1, 2, .. , n 4- 1. For a particular value of k < n this will entail 1 comparison, 2 additions (in computing the new values of S and k), and 1 multiplication (multiplying a, by xk), a total of 4 operations. But this assumes we know what xk is. We just saw that this number takes 3k - 2 operations to compute. Thus, for a given value of k < n we will use 4 + (3k - 2) = 3k + 2
1.4 Algorithms and Their Efficiency
operations. Letting k
=
27
1, 2,. . ,n accounts for a total of 5 + 8+ll1+- + (3n +2)
operations. It will be proved in Section 2.6 that the value of this sum is 1(3n2 + 7n). The extra comparison when k = n + 1 gives a total of 21(3n ±7n)+ I operations. Notice that here the complexity of our algorithm is itself a polynomial in n, namely l.5n 2 + 3.5n + 1. For a given polynomial in n of degree k, say aknk + k-l ak-In + + ao, the term aknk will exceed the sum of all the other terms in absolute value if n is sufficiently large. Thus, if the complexity of an algorithm is a polynomial in n, we are interested mainly in the degree of that polynomial. Even the coefficient of the highest power of n appearing is of secondary importance. That the complexity of the algorithm just presented is a polynomial with n 2 as its highest power of n (instead of n or n , for example) is more interesting to us than the fact that the coefficient of n2 is 1.5. We will say that an algorithm has order at most fln), where f (n) is some nonnegative expression in n, in case the complexity of the algorithm does not exceed Cf (n) for some constant C. Recall that the polynomial evaluation algorithm has complexity 1.5n 2 ± 3.5n + 1. It is not hard to see that 3.5n ± 1 < 4.5n2 for all positive integers n. Then .
1.5n2 + 3.5n + I
=
6n 2
for all positive integers n. Thus, (taking C = 6) we see that the polynomial evaluation algorithm has order at most n2. A similar proof shows that in general an algorithm whose complexity is no more than a polynomial of degree k has order no inore than n k Now we will present a more efficient algorithm for polynomial evaluation. It was first published in 1819 by W.G. Homer, an English schoolmaster. The idea behind it is illustrated by the following computation with n =3: a3X3 +a2X2
+ aix +ao =x(a3 X 2 ±+a2 X +a,)+ao X + a2) + a,) + ao = x(x(x(a 3 ) +
=x(x(a3
a2)
+ ai) ± ao.
Horner's Polynomial Evaluat-ion Algorithm This algorithm computes P(x) =a~x' + a,-1xn-i + integer n and real numbers x, ao, a, . . ,a,. Step I (initialization) Set S = an and k = 1. Step 2 (compute next expression) while k
endwhile
.
+ ao, given the nonnegative
28
Chapter ]
An Introduction to CombinatojialProblems mld Techniques
Step 3
(output P(x) = S) Print S.
The following table shows how this algorithm works for n = 3, P(x) 5x 3 -2x 2 -+3x +4(soa, = .5,a2 = -2,a, = 3,andao =4),andx = 2.
Start:
S
k
xS-+-a2 =2(5)+(-2)=8
2
a3 = 5
t
xS+a =2(8)+3= 19
3
xS+tao =2(19) + 4=42
4
We check whether k < n in step 2 for k = 1, 2, .. , n + 1, a total of n + 1 times, and each time except the last requires just 1 comparison, 1 multiplication, 2 additions, and 1 subtraction. Thus, the algorithm evaluates a polynomial of degree n using just 5n + I operations, as opposed to 1(3n2 + 7n) + 1 for our first version. If n were 10, the second algorithm would take 51 operations while the first would take 186; and For larger values of n the difference would be even more marked. In broader terms, Homer's polynomial evaluation algorithm is superior to the previous polynomial evaluation algorithm because it has order no more than n, while we could only say that the first algorithm had order no more than n2 . The algorithms we have presented so far are simple enough that we can compute their complexities exactly. Often, however, the exact number of operations necessary may not depend only on n. An algorithm to sort n numbers into numerical order, for example, nay entail more or fewer steps depending on how the numbers are arranged initially. Here we might count the number of operations in the worst possible case. The actual number of operations will then be less than or equal to this number. Later we may present more complicated algorithms in an informal way. An exact analysis of their complexity would entail a more detailed description revealing each elementary operation, akin to an actual program in some computer language. In this case the statement that the algorithm has order no more than f (n) is to be interpreted as saying that a computer implementation exists for which the number of elementary operations does not exceed Cf (n) for some constant C.
Now we will consider an algorithm for generating subsets of a set, as our solution of the space shuttle problem requires. If a set S consists of the n elements Xi, X2, .X . , xn, a compact way of representing a subset of S is as a string of Os and Is, where the kth entry in the string is 1 if xk E S and 0 otherwise. If n = 3, for
1.4 Algorithms and Their Efficiency
29
example, the 8 = 23 strings and the subsets to which they correspond are shown below. 000 0 001 {X3} 010
{X2 }
0l1
{X 2 , X31
100
(xil
101 110
{XI, X3} {XI, X21
111
{XI, X 2 , X3)
By examining this list we see how these digits might be generated in the order given. We look for the rightmost 0, change it to a 1, and then change any digits still further to the right to Os. If we let ala2 .. .an
be a given string with n Os and is, the following algorithm generates the next string.
Next Subset Algorithm Given a positive integer n and the string a1 a2 .. a, of Os and Is corresponding to a subset of a set with n elements, this algorithm computes the string corresponding to the next subset. Step I Step 2
(initialization) Set k = n. (look for rightmost 0)
while k > I and
ak = 1
Replace k with k -1. endwhile Step 3 (if there is a zero, form the next string)
if k> 1 Step 3.1 (change the rightmost 0 to 1) Replace ak with 1. Step 3.2 (change succeeding is to Os) for i = k + I to n Replace aj with 0. endfor Step 3.3 (output) Print al a 2 .. a, otherwise Step 3.4 (no successor) Print 'This string contains all 1s.' endif
30
Chapter]
An Introduction to CombinatorialProble, s at d Techniques
In this algorithm step 2 finds the rightmost string digit ak that is 0. (If all digits are 1, k reaches 0 and we stop at step 3.4.) Then ak is changed to 1 in step 3. 1, and the digits to its right are changed to Os in step 3.2. The actual number of arithmetic operations required will depend on the string a, a2 . . . an we start with, although since the replacements in steps 2 and 3 each can be repeated at most n times, the number of operations will be no more than some constant multiple of n. Let us consider how this algorithm might be applied to the space shuttle problem. We will restrict out attention to deciding whether the subset we have generated has a total weight of less than 700 kilograms. Let Wi be the weight of the ith experiment. Then we need to compute aoTV, +a 2 W2 +
+anWn
and see if this exceeds 700 or not. If we are including experiment i when ai is 1 and excluding it when ai is 0, then this sum gives the total weight of the included experiments. The reader should check that the sum may be computed using n multiplications and n - 1 additions, for a total of 2n - 1 operations, not counting any comparisons or index changes. Since for n experiments there are 2' subsets of experiments, and since generating and checking each subset takes a multiple of n operations, the complexity of this method of finding the best choice of experiments is Cn *2', where C is some constant. This is an expression that gets large quite quickly as n increases. The following table shows how long a computer that executes one million operations per second would take to do n * 2' operations for various values of n. For comparison purposes we also show how long 1000n 2 operations would take. n:
10
20
30
40
50
n2' operations
0.01 sec
21 sec
9 hr
1.4 years
1785 years
1000n 2 operations
0.1 sec
0.4 sec
0.9 sec
1.6 sec
2.5 sec
This table indicates that simply increasing computer speed may not make an algorithm practical, even for modest values of n. If, for example, our computer were capable of one billion operations per second instead of one million (making it 1000 times faster), performing n2' operations for n = 50 would still require 1.785 years. In general, an algorithm Cisconsidered 'good' if its complexity is no more than some polynomial in n. Of course, in practice, a nonpolynomial complexity may be acceptable if only smal values of n arise. Expressions depending on n increase at different rates as n gets large. In general, expressions with n as the exponent of a number greater than 1 grow faster than any polynomial in a, and n! grows even faster. On the other hand, although log2 n (to be explained in Section 2.5) increases without bound as n increases, it grows more slowly than any positive power of n. The mathematical comparison of these expressions entails analytic techniques not appropriate for
31
1.4 Algorithms and Their Efficiency
this course, but we offer the following table to give an idea of how fast various expressions grow. The time given is that for a computer executing one million operations per second to run through f (n) operations. 10
20
30
40
50
log 2 n
.0000033 S
.0000043 S
.0000049 S
.0000053 S
.0000056 S
.0000059 S
nI12
.0000032 S
.0000045 S
.0000055 S
.0000063 S
.0000071 S
.0000077 S
n
.00001 S
.00002 S
.00003 S
.00004 S
.00005 S
.00006 S
n2
.0001 S
.0004 S
.0009 S
.0016 S
.0025 S
.0036 S
n2 + iOn
.0002 S
.0006 S
.0012 S
.0020 S
.003 S
.0042 S
n:
60
f(n)
nlo 2' n!
2.8 H .001 S 3.6 S
119 D
19 Y
333 Y
3097 Y
19174 Y
IS
18M
13 D
36 Y
36559 Y
77147 Y
8.4. 1018 Y
34
2.6*10
Y
9.6 10
50
Y
2.6. 1068 Y
Now we will give an algorithm for sorting a list of items possessing a natural order, such as scores in a golf tournament (where the order is numerical) or names in a list (where the order is alphabetical). The most widely known such algorithm, called the bubble sort, is so called because of the similarity between its actions and the movement of a bubble to the surface in a glass of water. The smaller items 'bubble' to the beginning of the list. To keep the discussion simple, we will assume that the items a, a2, . . , an in the list are real numbers. We first consider the last two items in the list, a,-, and an, exchanging their values if an is less than a,- . We next consider the values in the n - 2 and n - I positions. Again, we exchange them if the n - 1 item is less than the n -2 item. This process of comparing two adjacent items continues until the comparison, and possible exchange, of the values in the first two positions. At this point, the smallest value in the list has been brought to the first position. We now start over again, this time operating on the smaller list consisting of the elements in the second through the nth positions. This will bring the smallest of the items in the second through the nth positions into the second position in the list. This process continues until all of the elements in the original list have been arranged in nondecreasing order.
Bubble Sort Algorithm This algorithm places the numbers in the list a, a2, . . Step I
(set beginning of sublist) for j = I to n - I
an in nondecreasing order.
32
Chapter] An Introduction to CombinatorialProblems and Techniques
Step 1.] (find smallest element of sublist) fork = n-I to j by -1 Step 1.1.1 (interchange if necessary) if ak+ I < ak Interchar ge the values of ak and ak+1, endif endfor endfor Step 2 (output list in nondecreasing order) Print a, a2 , . a,.
X
Example 1.5 We will use the bubble sort to order the list 7, 6, 14, 2. The following chart shows the positions of the numbers irn the list as step 1.1.1 of the algorithm is performed. The circled numbers are those being compared. a,
6e2
a3
a4
7
6
14
2
7
()
&
Q 7
6 a
14
2 2
(;
6
14
2
(,
7
2
0
7
i
k
1
3
14
2 1
2
3 2
3
3
14
Thus, 6 comparisons are required to sort the given list into nondecreasing order. + To measure how efficient the bubble sort is, we will count the number of comparisons required to get the n items in order. Notice that each comparison is accompanied by a bounded number of other operations, so that the complexity of the algorithm will not exceed some constant times the number of comparisons. The first pass through the list requires n - 1 comparisons; this moves the smallest element to the front A-- the list. The second pass, using the items in the second through last positions, requires n -2 comparisons. This pattern continues until the final pass, which corrlFares only the items in the last two positions of the altered list. In all, there are (n - 1) + (n -- 2) + (n - 3) + .. + 3 + 2 + I
1.4 Algorithms and Their Efficiency
33
comparisons. By a formula in the next chapter, the above expression has the value 2 nnThus, the
bubble sort algorithm has order at most n.
EXERCISES 1.4 In Exercises 1-6 tell whether the given expression is a polynomial in x or not, and if so give its degree.
1. 5X 2 -3x +-
2. 16
2
2x 2
4. 2' + 3x
3. x
--
1
X 6. 2x +3x 1 / 2 +4
7x±1
In Exercises 7-10 compute the various values S takes on when the polynomial evaluation algorithm is used to compute P(x). Then do the same thing using Horner'spolynomial evaluation algorithm. 7.P(x)=5x+3, x=2 9.P(x)=-x 3 +2X 2 +5x-7, x-2
8. P(x)-=3x2 +2x -1, x= 5 10. P(x)-=2x3 +5X 2 -4, x-=3
In Exercises 11-14 tell what next string will be produced by the next subset algorithm.
11. 110101
12. 110111
13. 001101
In Exercises 15-18 make a table listing the values of k, algorithm is applied to the given string.
j, and a,
15. 101
17. 1101
16. I11
a2 ,. .
14. 001001 ,an after each step when the next subset 18. 1110
In Exercises 19-22 illustrate as in Example 1.5 the use of the bubble sort algorithm to sort each given list of numbers. 19. 13, 56, 87, 42
20. 23, 5, 17, 12
21. 6, 33, 20, 200, 9
22. 88, 2, 75, 10, 48
In Exercises 23-26 estimate how long a computer doing one million operationsper second would take to do Y' and
lOOn3 operations. 23. n =20
24. n =30
25. n =40
26. n-=50
In Exercises 27 30 tell how many elementary operationsthe given algorithm uses. (It depends on n.) 27. Algorithm for evaluating n!
Step]I Step 2
Set k=OandP =1. while k < n
(a) Replace k with k + 1. (b) Replace P with k P. endwhile Step 3 Print P.
34
Chapter]1 An Introduction to CombinatorialProblemisan~lTechniques
28. Algorithm for computing the sum of an arithmetic progression of n terms with first term a and common difference d
Step]I Step 2
Set S-=a, k =1,and t =a.
while k < n (a) Replace t with t± d. (b) Reptace S with S + t.(c) Replace k with k ± 1.
endwhile Step 3
Print S.
29. Algorithm for computing the sum of a geometric progression of n terms with first term a and common ratio r
Step]I Step 2
SetS =a,P =ar,and k= 1. while k < n (a) Replace S with S + P. (b) Replace P with Pr. (c) Replace k with k + 1.
endwhile Step 3 Print S.
30. Algorithm for computing F~,the nth Fibonacci number (defined in Section 2.6)
Step]I Step 2
Set a
=
1, b
while k
< n
lc 1,
=
2, and k
=1.
(a) Replace c with a + b. (b) Replace a with b. (c) Replace b with c. (d) Replace k with k + 1.
endwhile Step 3 Print b.
The polynomial evaluation algorithm is inefficient because it computes x k anew for each value of k. The following revision corrects this.
HistoricalNotes
35
Revised Polynomial Evaluation Algorithm This algorithm computes P(x) = a~x' + a,-Ix'-' + numbers x, ao, a1,. . ,a,. Step I (initialization) Set S = a0, y = 1, and k =1. Step 2 (add next term) while k < n (a) Replace y with xy. (b) Replace S with S + yak. (c) Replace k with k + 1. endwhile Step 3 (output P(x) = ) Print S.
.
+ ao, given the nonnegative integer n and real
In Exercises 31-32 compute the various values S takes on when the revised polynomial evaluation algorithm is used to compute P(x), where P(x) and x are as in the given exercise. 31. Exercise 9
32. Exercise 10
33. Show that the complexity of the revised polynomial evaluation algorithm is 5n ± 1.
HISTORICAL NOTES
Leonardo of Pisa (Fibonacci)
Chapter 1 features the organizing aspects of algorithms. From PERT charts to matching and knapsack problems, the emphasis is on thinking about orderly procedures for confronting and solving problems. Algorithms have been part of mathematics since its beginning. The contents of the Rhind papyrus (c. 1650 B.C.) and the many cuneiform tablets of the Babylonians (c. 1750 B.C.) give evidence of the attempt to generalize the solutions to problems into computational formulas [73, 74] 1* Such formulas are forms of algorithms. The Greeks provided directions on how to perform various geometric constructions and analyze elementary problems in number theory. Perhaps the most famous among the latter are Eratosthenes' sieve for developing a listing of the first n primes, Euclid's algorithm for finding the greatest common divisor for a pair of positive integers, and Diophantus' methods for finding solutions to algebraic equations. It was with the publication of the Liber Abaci of Leonardo of Pisa (Fibonacci) in 1202 that Europeans had their first organized exposure to Arabic numerals and algorithms for operating with them [79- 79- R51. Fibonacci stated his algorithms by referring to the quantities held by the first person and second person and giving a verbal explanation of how to craft an answer for the situation at hand. While the Greeks had used letters to refer to points in earlier times, it was not until the work of Franqois Vi~te (1540-1603) that algorithms for algebraic operations began to appear in more modem symbolic forms. 'Numbers in brackets refer to References for the Historical Notes on pages 574-575.
36
Chapter 1
Augusta Ada Byron
An Introduction to CombinatorialProblern.sond Techniques
The very name 'algorithm' has a historical trail of its own. One of the earliest texts, if not the first, dealing with the arithmetic of the Hindu-Arabic numerals was written in Arabic by Mohammad ibn-Musa al-Khwarizmi (c. 783-850). His book was entitled Al-jabr wa' I muqbalah. Although he original text has been lost and we have only portions that were transcribed in Latin i t the 11 00s, the author and text had a large influence on what we say and do today. The word 'algorithm' is an Anglicized version of alKhwarizmi's name, and the title of his book has given way to our present day word 'algebra.' Al-Khwarizmi's name, in the form 'algorithmicians,' was used in the Middle Ages to separate calculating by Hlindu-Arabic algorithms from the work of the 'abacists' who calculated with counting tables and abacus-related methods based on the Roman numerals [73, 74, 85]. Forms of the word 'algcrithn' disappeared from common usage after the Middle Ages, to reappear around 1850 as a result of a reanalysis of the work of al-Khwarizmi. Since 1900, the notion of carefully specifying steps in carrying out a procedure has grown in importance as mathematicians a Id computer scientists have struggled to understand the efficiency of various procedures. Augusta Ada Byron (1815-1.352), the Countess of Lovelace, was one of the first to see the importance that algorithms could play in the development of computing devices. The only child of the English poet Lord Byron, Ada Byron had the great opportunity to be tutored by Augustus De Morgan (1806-1871) and to work with Charles Babbage (1791-1871), the inventor of the first automated computing machine. While the machine never worked flawlessly, the ideas behind it were as much the work of Byron as they were the work of Babbage. Her work in describing the programming of Babbage's Analytical Engine to carry out computation and derivations has been memorialized through the naming of the programming language ADA in her honor [83]. The program evaluation and review technique and critical path method, as mentioned earlier, were first developed and used by the U.S. Navy and Westinghouse engineers in 1958 to organize the building of the first nuclear powered submarines. The process was essentially discovered simultaneously by chemical engineers at du Pont, and by operations researchers in England, France, and Germany. As graph theoretic methods became better known, mathematical scientists around the world began to represent and study problems regarding orderings via graph theoretic representations.
SUPPLEMENTARY EXERCISES 1. Use the PERT method to determine the total project tim e and critical path for the following diagram. C
H
Supplementary Exercises
37
2. The table below tells the time needed for a number of tasks and which tasks precede them. Make a PERT diagram, and determine the project time and critical path. Task
Time
PrecedingTasks
A
3
None
B
5
None
C D
2 4
A A,B
E
6
A,B
F
6
C,D,E
G
3
D,E
H
3
F,G
I
2
F,G
3. In manufacturing a certain toy, Machine 1 makes part A in 3 minutes, Machine 2 makes part B in 5 minutes, Machine 3 makes the box in 2 minutes, Machine 4 assembles the two parts in 2 minutes, and Machine 5 puts an assembled toy into a box in 1 minute. How long will it take to make and box 5 toys? 8! 4. Calculate 5. Calculate P(11, 6). 6. A baseball team has 2 catchers, 4 starting pitchers, and 5 relief pitchers. A catcher, starting pitcher, and relief pitcher must be chosen for an all-star game. In how many ways can this be done? 7. A basketball team has 3 centers, 4 guards, and 4 forwards. A most-valuable player, captain, and most-improved player are to be chosen from the team. If all three players are to be different, in how many ways can this be done? 8. A student honor society has 10 juniors and 13 seniors. According to its bylaws, the president and treasurer must be seniors, and the vice-president and secretary must be juniors. In how many ways can the four offices be filled by four different students? Let A = {l, 3, 5J, B = {2, 6, 10}, and C = {x: x is an integer and 0 < x < 10). In Exercises 9-16 tell whether each statement is true orfalse. 9. B C C
10. AC C
11. 6 C B
12. A E C
13. {l} C A
14. 6 E C
15. 0 C B
16. 1{011 E A
17. What is I{X: X C {2, 4, 6, 8, 10}I? 18. In Cincinnati, chili consists of spaghetti topped by any (or none) of meat sauce, cheese, chopped onions, and beans. In how many ways can chili be ordered? 19. Five students decide to send a delegation to a professor to ask her to delay a test. The delegation is to have a spokesperson, and perhaps some accompanying members. In how many ways can it be chosen? In Exercises 20-23 tell whether each expression is a polynomial in x, and, if so, give its degree. 20. 2x + 3 +4x-1 3
21. x °° - 3
22. log 2 10
23. 3x].5 + 1.5x3
24. Let P(x) = 3x + 4x -5. Compute the various values S takes on when the polynomial evaluation algorithm is used to compute P(x) for x = 3. 25. Repeat the previous problem using Homer's polynomial evaluation algorithm.
38
Chapter I An Introduction to CombinatorialProblens and Techniques
26. Let S = {1, 2, 3, 41. Find the ordered sequence of all subsets of S as produced by the next subset algorithm, starting with 0. 27. Illustrate the use of the bubble sort algorithm to sort the following list of numbers as in Example 1.5: 44, 5, 13, 11, 35. 28. How long would it take a computer to do 25! operation:; if it can do one billion operations per second? 29. Apply the following algorithm to n
Step I Step 2
18. What is the value of s when the algorithm stops?
Setd= I ands =0. while d < n n. Step 2.1 if -is an integer d Replace s with s + d. endif Step 2.2 Replace d with d + 1. endwhile
30. Apply the following algorithm to n = 100. What is the value of s when the algorithm stops?
Step I Set s =n. Step 2 repeat Step 2.1 Set t =s. Step 2.2 while s is even Replace s with
Step 2.3 until s =t
5
-
2 endwhile Replace s with 3s + 1.
31. Determine how many elementary operations the following algorithm for finding the sum of the first n squares uses. (The answer depends on n.)
Step I SetS= 1 and k 1. Step 2 while k < n (a) Replace S with S + k . k. (b) Replace k with k + 1.
endwhile Step 3 Print S. 32. Determine how many elementary operations the following algorithm for finding P(n, r) uses. (The answer may depend on n or r.)
Step I Step 2
Set k = and Q =n. while k < r
Suggested Readings
39
(a) Replace Q with (n - k)Q. (b) Replace k with k + 1. endwhile Step 3 Print Q. 33. Write a formal algorithm for performing PERT to determine the project time and critical path for a project. Assume that the input data is given in tabular form as in Exercise 2 above.
COMPUTER PROJECTS Write a computer program having the specified input and output. 1. Given n, compute n factorial. 2. Given n and r, with 0 < r < n, compute P(n, r). 3. Use the algorithm for evaluating Xn to compute x', given a real number x and a positive integer n.
4. Use the polynomial evaluation algorithm to evaluate P(x) = 35x 4
17x
3
+ 5x2 + 41x -29,
given a real number x. Have the program call the program of the previous problem. Time the program in evaluating P(x) forx = 1, 2, . . 100. 5. Using Homer's polynomial evaluation algorithm, evaluate P(x) for any real number x, where P (x) is as in the previous exercise. Time the program in evaluating P(x) for x = 1, 2, .. , 100. 6. Given a string of twelve Os and Is, use the next subset algorithm to compute the next string. 7. Use the previous exercise to output all possible strings of twelve Os and Is. 8. Examine all subsets of the set of 12 space shuttle experiments of Section 1.3 to find an optimal subset having a total rating of 53. What experiments are used? 9. Given n, output all possible strings of n Os and Is. 10. Given a list of 10 numbers, output the list in nondecreasing order. Use the bubble sort algorithm.
SUGGESTED READINGS 1. Graham, Ronald L. 'The Combinatorial Mathematics of Scheduling.' Scientific American (March 1978): 124132. 2. Lawler, Eugene L. CombinatorialOptimization:Networks andMatroids. New York: Holt, Rinehart, and Winston, 1976. 3. Lewis, Harry R., and Christos H. Papadimitriou. 'The Efficiency of Algorithms.' Scientific American (March 1978): 96-109. 4. Lockyer, K.G. An Introduction to CriticalPath Analysis. 3rd ed. London: Pitman and Sons, 1969. 5. Niven, Ivan. Mathematics of Choice or How to Count Without Counting. New York: L.W. Singer, 1965. 6. Pipenger, Nicholas. 'Complexity Theory.' Scientific American (June 1978): 114-124.
Sets, Relations, and Functions 2.1 Set Operations 2.2 Equivalence Relations ion Cnn--n-
2.4* Partial Ordering Relations 2.5 Functions 2.6 Mathematical Induction 2.7 Applications
A swe saw in Chapter 1, discrete mathematics is concerned with solving problems in which the number of possibilities is finite. Often, as in the analysis of the knapsack problem in Section 1.3, the discussion of a problem requires consideration of all the possibilities for a solution. Such an approach can be made easier by the use of seis. In other situations we may need to consider relationships between the elements of sets. Such relationships frequently can be expressed using the mathematical ideas of relations and functions. In this chapter we will study these basic concepts as well as the principle of mathematical induction, an important method of proof in discrete mathematics.
2.1
SET OPERATIONS In Section 1.3 we presented some of the basic ideas about sets. In this section we will discuss several ways in which sets can be combined to produce new sets. Suppose that in the example discussed in Section 1.3 it is decided that the space shuttle will carry experiments on two successive trips. If S = {1, 5, 6, 8} is the set of experiments carried on the first trip and T = {2, 4, 5, 8, 9} is the set of experiments carried on the second trip, then {1, 2, 4, 5, 6, 8, 91 is the set of experiments carried on the firsi or second trip or both. This set is called the union of S and T. More generally, by the union of sets A and B we mean the set consisting of all the elements that are in A or in B. Note that, as always in mathematics, the word 'or' in this definition is used in the inclusive sense. Thus, an element x is
40
41
2.1 Set Operations
in the union of sets A and B in each of the following cases: (1) (2) (3)
xEAandx§fB, xgXAandxEB,or xEAandxEB.
The union of sets A and B is denoted A U B. Thus A uB
=
{x: x
E
A orx E B).
Another set of interest in the space shuttle example is the set {5, 8) of experiments carried on both trips. This set is called the intersection of S and T. In general, the intersection of sets A and B is the set consisting of all the elements that are in both A and B. This set is denoted A n B. So
A n B = {x: x
E
A and x
E
B).
If the intersection of two sets is the empty set, then these sets are said to be disjoint.
Example 2.1 If A = {1,2,4},B ={2,4,6,81,andC ={3,6},then AUB = {1,2,4,6,8}
and
AlB = {2,4},
AUC={1,2,3,4,6}
and
AnCr
=0,
and BUC={2,3,4,6,81
and
BnC={61.
Thus A and C are disjoint sets. + The set {1, 61 of elements carried on the first space shuttle trip but not on the second trip is called the difference of S and T. More generally, the difference of sets A and B, denoted A - B, is the set consisting of the elements in A that are not in B. Thus A
-
B = {x: x
E
A and x V B}.
Note that, as the following example shows, the sets A usually equal.
-
B and B
-
A are not
Example 2.2 IfAandBareasinExample2.1,thenA-B={1}andB-A={6,8}.
+
In many situations all of the sets under consideration are subsets of a set U. For example, in our discussion of the space shuttle example in Section 1.3, all of the sets were subsets of the set of experiments U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}.
42
Chapter 2 Sets, Relations, and Functions
Such a set containing all of ihe elements of interest in a particular situation is called a universal set. Since there are many different sets that could be used as a universal set, the particular universal set being considered must always be described explicitly. Given a universal set U and a subset A of U, the set U - A is called the complement of A and is denoted A. '
Example 2.3 If A = {1, 2, 4}, B = {2, 4. 6, 8}, and C = {3, 6} are the sets in Example 2.1 and U
{1, 2,3,4,5,6,7,8}
is the universal set, then A={3,5,6,7,8},
B={1,3,5,7},
and
C={1,2,4,5,7,81.
+
The theorem below lists sorne elementary properties of set operations. These properties follow immediately from the definitions given above by using the fact that A = B if and only if A C .B and B C A.
Theorem 2.1 Let U be a universal set. For any subsets A, B, and C of U, the following are true. (a) (b)
AUB=BUAandAn B= BnA (AUB)UC=AU(BUC)and
(c)
(A n B)AC = A n (B A C) AU(BAc)=(AUB)rn(AUC)and A A (B U C) =(A A B) U (A AC)
(d) (e)
A = A AUA=U
(commutative laws) (associative laws) (distributive laws)
(f) AnA =0 (g) (h) (i)
A c AUBandB C AUB AnB C AandAAn < B A-B =AnB
Relationships among sets can be pictured in Venn diagrams, which are named after the English logician John Venn (1834-1923). In a Venn diagram, the universal set is represented by a rectangular region and subsets of the universal set are usually represented by circular disks drawn within the rectangular region. Sets that are not known to be disjoint should be represented by overlapping circles as in Figure 2.1. Figure 2.2 contains Venn diagrams for the four set operations defined earlier. In each diagram the colored region depicts the set being represented. Venn diagrams depicting more complicated sets can be constructed by combining the basic diagrams found in Figure 2.2. For example, Figure 2.3 shows how to construct a Venn diagram for (A U B). (See also Example 2.4.)
2.1 Set Operations
FIGURE 2.1
A
B
A
AuB
A
B
AnB B
I
I
: A;
I
A-B
A
FIGURE 2.2
B B
A
L S S
W
I
I (AUB)
AuB FIGURE 2.3
43
44
Chapter 2 Sets, Relations, and Functions
The theorem below enables us to determine the complement of a union or intersection of sets. This result will be needed in Section 7.6 to help us find the number of elements in the union of several sets. Theorem 2.2
De Morgan's Laws are true. (a) (b)
For any subsets A and B of a universal set U, the following
(AUB)=AnB (AnB)=AUB
Proof. To prove that (A UB) AA B, we will show that each of the sets (A U B) and A n B is a subset Df the other. Firstsupposethatx E (A _B).Thenx g A U B.Butsincethisistrue,_x_ A and x V B. So x E A and x B.It follows that x e A n B. Therefore (A U B) C An B. Now suppose that x E AU B. Then x e A and x c B. Hence x § A and x t B. It follows that x V A U B. So x E (A U B). Thus AA B -C (A U B). Because we have (AU B) C A n B and A n B C (A U B), it follows that (A U B) = A n B. This proves part (a). The proof of part (b) is similar and will be left as an exercise. X
+
Example 2.4 We can compute (A U B) using Theorems 2.2(a), 2.1(d), and 2.1(i). (A U B) A n B = A n B = A This equality is illustrated by Figure 2.3.
+
-
B
+
Example 2.5 According to the U.S. customs laws, a person is not allowed to bring liquor into the United States duty-free if he or she is not over 21 or if he or she has brought duty-free liquor into this country in the previous 30 days. Who is allowed to bring duty-free liquor into the United States? Let A denote the set of people aged 21 or over, and let B denote the set of people who have brought duty-free liquor into the United States in the previous 30 days. Then the persons who are not allowed to bring duty-free liquor into the United States are those in the set A U B. This means that those who are eligible to bring duty-free liquor into this country are those in the set (A U B). Example 2.4 shows that the persons who can bring duty-free liquor into the United States are those in the set A - B, the set of people aged 21 or over who have not brought duty-free liquor into the United States in the previous 30 days. +
2.1
Set Operations
45
When listing the elements of a set, the order in which the elements are written is immaterial. Thus, for example, {1, 2, 3} = {2, 3, 11 = {3, 1, 2}. Often, however, we need to be able to distinguish the order in which two elements are listed. In an ordered pair of elements a and b, denoted (a, b), the order in which the entries is written is taken into account. Thus, (1, 2) # (2, 1), and (a, b) = (c, d) if and only if a = c and b = d. The final set operation that we will consider is the Cartesian product, which arises in connection with relations (to be studied in Section 2.2). Given sets A and B, the Cartesian product of A and B is the set consisting of all the ordered pairs (a, b), where a E A and b c B. The Cartesian product of A and B is denoted A x B. Thus A x B
= {(a,
b): a e A and bE B).
The Cartesian product is often encountered in discussions of the Euclidean plane, for if R denotes the set of all real numbers, then R x R is the set of all ordered pairs of real numbers, which can be pictured as the Euclidean plane. Example 2.6 Let A
1, 2, 3} and B = {3, 4}. Then A x B B x A
((1,3), (1,4), (2, 3), (2,4), (3, 3), (3, 4)) and -(3, 1), (3, 2), (3, 3), (4, 1), (4, 2), (4, 3)1. +
As Example 2.6 shows, usually A x B =#B x A. Example 2.7 A public opinion poll was taken to see how several of the leading Democratic presidential candidates would fare in the 1976 presidential election against the leading Republican candidates. The set of Democratic candidates considered was D = (Brown, Carter, Humphrey, Udall), and the set of Republican candidates considered was R = (Ford, Reagan). How many pairings of a Democratic and a Republican candidate are there? The set of all possible pairs of a Democratic and a Republican candidate is D x R. The elements of this set are the ordered pairs (Brown, Ford), (Brown, Reagan), (Carter, Ford), (Carter, Reagan), (Humphrey, Ford), (Humphrey, Reagan), (Udall, Ford), (Udall, Reagan).
46
Chapter 2 Sets, Relations, and Functions
Thus there are eight different airings of a Democratic and a Republican candidate. +
EXERCISES 2.1 In Exercises 1-4 evaluate A U B. A n B. A -B. that the universal set is U = {1, 2, .. , 91.
A, andBJbreach of the given sets A and B. In each case assume
1. A = {2, 3, 5, 7, 8}andB = 11,3,4,5,6,9)
2.A = [1,4,6,9landB=11,2,4,5,6,7,9}
3. A ={1,2,4,8,91andB={3,71
4. A = {3,4,6,7,8,9}andB={2,5,7,9}
In Exercises 5-8 compute A x B for each of the given sets 4 and B. 5. A = 11,2,3,41 andB = {7,8}
6. A= (3,4,5}andB={1,2,3}
7. A = [a, el and B = tx, y, z)
8.A = {p,q,r,s}and B = {ace}
Draw Venn diagramsrepresentingthe sets in Exercises 9-12. 9. (A 11. A
n B) n (BUC)
1(1. A-B 12. AU (B-C)
13. Give an example of sets for which A U C = B U C, bst A
#
B.
14. Give an example of sets for which A n C = B n C, but A
+
B.
15. Give an example of sets for which A -C
#
B.
=B
-
16. Give an example of sets A, B, and C for which (A
C', but A -
B)
-
C 5 A -(B
-
C).
Use Theorems 2.1 and 2.2 as in Example 2.4 to simplipf the sets in Exercises 17-24. 17. A A (B
-
A)
21. An(AUB)
18. (A-B)U(AnB)
19. (A-B)n(AUB)
20. An(AnB)
22. (A-B)nA
23. An(AnB)
24. AU(AUB)
25. If A is a set containing m elements and B is a set containing n elements, how many elements are there in A x B? 26. Under what conditions is A -B = B - A? 27. Under what conditions is A U B = A? 28. Under what conditions is A n B = A?
29. Prove parts (c) and (i) of Theorem 2.1.
30. Prove part (b) of Theorem 2.2 by using an argument similar to that in the proof of part (a). 31. Note that if A = B, then A = B. Use this fact to prove )art (b) of Theorem 2.2 from part (a). 32. Let A and B subsets of a universal set U. Prove that if A C B, then B C A. Prove the set equalities in Exercises 33-38. 33. (A-B)U(B-A)=(AUB)-(AnB) 35. (A-B)-C =(A-C)-(B-C) 37. (A-B)n(A-C)=A-(BUC)
34. (A-B)U(A-C)=A-(Bnc) 31,. (AxC)A(BxD)=(AnB)x(CnD)
38. (A-C)n(B-C)=(AnB)-C
# (A U B') x (C U D). 40. Prove that (A x C) U (B x D) C (A U B) x (C U D). 39. Give an example where (A x C) U (B x D)
oo
2.2 Equivalence Relations
2.2
47
+t EQUIVALENCE RELATIONS In Section 1.2 we considered a problem involving the matching of pilots to flights having different destinations. Recall that the seven destinations and the pilots who requested them are as shown below. Los Angeles: Timmack, Jelinek, Rupp Seattle: Alfors, Timmack, Tang, Washington London: Timmack, Tang, Washington Frankfort: Alfors, Tang, Rupp, Ramirez Paris: Jelinek, Washington, Rupp Madrid: Jelinek, Ramirez Dublin: Timmack, Rupp, Ramirez This list establishes a relation between the set of destinations and the set of pilots, where a pilot is related to one of the seven destinations whenever that destination was requested by the pilot. From this list we can construct a set of ordered pairs in which the first entry of each ordered pair is a destination and the second entry is a pilot who requested that destination. For example, the pairs (Los Angeles, Timmack), (Los Angeles, Jelinek), and (Los Angeles, Rupp) correspond to the three pilots who requested the flight to Los Angeles. Let S = {(Los Angeles, Timmack), (Los Angeles, Jelinek), . . ., (Dublin, Ramirez)) denote the set of all 22 ordered pairs of destinations and the pilots who requested them. This set contains exactly the same information as the original list of pilots and their requested destinations. Thus the relation between the destinations and the pilots who requested them can be completely described by a set of ordered pairs. Notice that S is a subset of A x B, where A is the set of destinations and B is the set of pilots. Generalizing from the previous example, we define a relation from a set A to a set B to be any subset of the Cartesian product A x B. If R is a relation from set A to set B and (x, y) is an element of R, we will say that x is related to y by R and write x R y instead of (x, y) E R. +
Example 2.8 Among three college professors, suppose that Lopez speaks Dutch and French, Parr speaks German and Russian, and Zak speaks Dutch. Let A = [Lopez, Parr, Zak)
48
Chapter2
Sets, Relations, and Functions
denote this set of professors and B = {I)ulch, French, German, Russian} denote the set of foreign languages they speak. Then R = {(Lopez, Dutch), (Lopez, French), (Parr, German), (Parr, Russian), (Zak, Dutch)} is a relation from A to B in which x is related to y by R whenever Professor x speaks language y. So, for instance, Lopez R French and Parr R German are both true, but Zak R Russian is false. +
Often we need to consider a relation between the elements of some set. In Section 1.1, for instance, we considered the problem of determining how long it would take to produce and deliver the advertisements for a department store sale. In analyzing this p)ioblem, we listed all the tasks that needed to be done and represented each task by a letter. Then we determined which tasks immediately preceded each task. This listing establishes a relation on the set of tasks S = {A, I;, C, D, E, F, G, H, I, J, KI in which task X is related to task Y if X immediately precedes Y. The resulting relation {(A, C), (B, C), (C, [)), (C, E), (D, F), (E, F), (C, G), (G, H), (1, [), (H, J), (I, J), (J, K)} is a relation from set S to itself that is, a subset of S x S. A relation from a set S to i :self is called a relation on S.
'
Example 2.9 Let S = {1, 2, 3, 4}. Define a ret ation R on S by letting x R y mean x < y. Then I is related to 4, but 4 is not relateddto 2. Likewise 2 R 3 is true, but 4 R 2 is false. '
A relation R on a set S may have any of the following special properties. (1) (2) (3)
If for each x in S, x R x is true, then R is called reflexive. If y R x is true whenever x R y is true, then R is called symmetric. If x R z is true whenever x R y and y R z are both true, then R is called transitive.
2.2 Equivalence Relations
49
The relation R in Example 2.9 is not reflexive since 1 R 1 is false. Likewise, it is not symmetric because 4 R 1 is false but 1 R 4 is true. However, R is transitive because if x is less than y and y is less than z, then x is less than z.
+
Example 2.10 Let S be the set of positive integers, and define x R y to mean that x divides y (that is, Y is an integer). Thus 3 R 6 and 7 R 35 are true, but 8 R 4 and 6 R 9 are false. Then R is a relation on S. Furthermore, R is reflexive since every positive integer divides itself, and R is transitive since if x divides y and y divides z, then x divides z. (To see why this is so, note that if Y and Z are integers, so is x Y.) However, R is not symmetric because 2 R 8 is true, but 8 R 2 is false. +
+
Example 2.11 Let S denote the set of all nonempty subsets of {1, 2, 3, 4, 5 1,and define A R B to mean that A n B #&0. Then R is clearly reflexive and symmetric. However, R is not transitive since {1, 21 R {2, 31 and {2, 31 R {3, 41 are true, but (1, 21 R {3, 41 is false. + A relation that is reflexive, symmetric, and transitive is called an equivalence relation. The most familiar example of an equivalence relation is the relation of equality. Two more examples follow.
+
Example 2.12 On the set of students attending a particular university, define one student to be related to another whenever their surnames begin with the same letter. This relation is easily seen to be an equivalence relation on the set of students at this university. +
+
Example 2.13 An integer greater than 1 is called prime if its only positive integer divisors are itself and 1. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, and 23. We will see in Theorem 2.13 that every integer greater than 1 is either prime or a product of primes. For example, 67 is prime and 65 = 5. 13 is a product of primes. On the set S of integers greater than 1, define x R y to mean that x has the same number of distinct prime divisors as y. Thus, for example, 12 R 55 since 12 = 2. 2 3 and 55 = 5. 11 both have two distinct prime divisors. Then R is an equivalence relation on S. + .
50
Chapter 2
Sets, Relations, and Functions
+
Example 2.14 Let S denote the set of all people in the United States. Define a relation R on S by letting x R y mean that x has the same mother or father as y. Then R is easily seen to be reflexive and symmetric. But R is not transitive, for x and y may have the same mother and y and z may have the same father, but x and z may have no parent in common. Hence R is not an equivalence relation on S. + If R is an equivalence relation on a set S and x C S, the set of elements of S that are related to x is called the equivalence class containing x and is denoted [x]. Thus [-:1 = {y
E
S: y R x}.
Note that by the reflexive property of R, x E [x] for each element x in S. In the equivalence relation described in Example 2.12, there are 26 possible equivalence classes; namely, the set of students whose surnames begin with A, the set of students whose surnames begin with B, and so forth. +
Example 2.15 Let S denote the set of integers greater than 1. For x and y in S, define x R y to mean that the largest prime divisor of x equals the largest prime divisor of y. Then R is an equivalence relat on on S. The equivalence class of R containing 2 consists of all elements in S that are related to 2, that is, all the elements in S whose largest prime divisor is 2. Such integers must be powers of 2, and hence [21 = {2k:k = 1,2,3,..}. Similarly, [3] consists of all the elements in S whose largest prime divisor is 3. Such integers must have 3 as a divisor and may or may not have 2 as a divisor. Thus
[3] = [2'3i: i =0, 1,2,.. and j = 1,2, 3,..}. Note that because the largest prime divisor of 4 is 2, we have 4 R 2. Since R is an equivalence relation on S, any element of S that is related to 2 must also be related to 4. Hence
[4:1 = 112] = 12k: k = 1, 2, 3,...}. + The fact that [2] = [4] in Example 2.15 illustrates part of the following theorem.
2.2 Equivalence Relations
Theorem 2.3
51
Let R be an equivalence relation on a set S. (a) If x and y are elements of S, then x is related to y by R if and only if [x] = [y].
(b) Two equivalence classes of R are either equal or disjoint. Proof (a) Let x and y be elements of S such that x R y. We will prove that [x] = [y] by showing that [x] C [y] and [y] C [x]. If z E [x], then z is related to x, that is, z R x. But if z R x and x R y, then z R y by the transitive property of R. So z E [y]. This proves that [x] C [y]. If z E [y], then z R y as above. By assumption x R y, and so y R x is true by the symmetric property of R. But then z R y and y R x imply z R x by the transitive property. Hence z E [x], proving that [y] C [x]. Since we have both [x] C [y] and [y] C [x], it follows that [x] = [y]. Conversely, suppose that [x] = [y]. Now x E [x] by the reflexive property, and so x E [y] because [x] = [y]. But if x E [y], then x R y. This completes the
proof of part (a). (b) Let [u] and [v] be any two equivalence classes of R. If [u] and [vI are not disjoint, then they contain a common element w. Since w E [u], part (a) shows that [w] = [u]. Likewise, [w] = [v]. It follows that [u] = [v]. Hence, [u] and [v] are either disjoint or equal. Because of part (b) of Theorem 2.3, the equivalence classes of an equivalence relation R on set S divide S into disjoint subsets. This family of subsets has the following properties: (1) (2) (3)
No subset is empty. Each element of S belongs to some subset. Two distinct subsets are disjoint.
Such a family of subsets of S is called a partition of S. '
Example 2.16 Let A = {1, 3, 4), B = 12, 6), and C = {5}. Then P = {A, B, C) is a partition of S = {1,2,3,4,5,61.SeeFigure2.4. +
FIGURE 2.4
52
Chapter 2
Sets, Relations, and Functions
We have seen that every equivalence relation on S gives rise to a partition of S by taking the family of subsets in the partition to be the equivalence classes of the equivalence relation. Crcnversely, if P is a partition of S, we can define a relation R on S by letting x R y mean that x and y lie in the same member of P. Using the partition in Examp .e 2.16, for instance, we obtain the relation ((I, 1), (1, 3), (1, 41, (3, 1), (3, 3), (3, 4), (4, 1), (4, 3), (4, 4), (2, 2) (2, 6), (6, 2), (6, 6), (5, 5)}. Then clearly R is an equivalenrce relation on S, and the equivalence classes of R are precisely the members of '2. We will state these facts formally as our next theorem. Theorem 2.4
(a) (b)
An equivalence relation R gives rise to a partition P in which the members of P are the equivalence Classes of R. Conversely, a partition P induces an equivalence relation R in which two elements are related by R whenever they lie in the same member of P. Moreover, the equivalence classes of this relation are the members of P.
Although the definitions of an equivalence relation and a partition appear to be quite different, as a result of Theorem 2.4 we see that these two concepts are actually just different ways of c describing the same situation.
EXERCISES 2.2 In Exercises 1-12 determine which of the reflexive, symnmett ic, and transitiveproperties are satisfied by the given relation R defined on set S. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
S = I1, 2,31 and R = {(l, 1), (1, 2), (2, 1), (2, 2)}. S = {1, 2,3) and [(I, 1), (1, 3), (2, 2), (2, 3), (3, 1), (3 2), (3, 3)}. S is the set of all Illinois residents and x R y means thst x has the same mother as y. S is the set of all citizens of the United States and x R y means that x has the same weight as y. S is the set of all students at Illinois State University and x R y means that the height of x differs from the height of y by no more than one inch. S is the set of all teenagers and x R y means that x has a grandfather in common with y. S is the set of all graduates of Michigan State University and x R y means that x first attended Michigan State University in the same year as y. S is the set of all residents of Los Angeles and x R Ymeans that x is a brother of y. S is the set of all real numbers and x R y means that x ' = y2 . S is the set of positive integers and x R y means that x divides y or y divides x. S is the set of all subsets of {1, 2, 3, 4} and X R Y means X C Y. S is the set of ordered pairs of real numbers and (x1, X2) R (Y1, Y2) means that xi = ye and x2 ' Y2-
2.2 Equivalence Relations
53
In Exercises 13-18 show thatthe given relationR is an equivalence relationon set S. Then describe the equivalence class containing the given element z in S, and determine the number of distinct equivalence classes of R. 13. Let S be the set of integers, let z = 7, and define x R y to mean that x - y is even. 14. Let S be the set of all possible strings of 3 or 4 letters, let z = ABCD, and define x R y to mean that x has the same first letter as y and also the same third letter as y. 15. Let S be the set of integers greater than 1, let z = 60, and define x R y to mean that the largest prime divisor of x equals the largest prime divisor of y. 16. Let S be the set of all subsets of (1, 2, 3, 4, 5}, let z = (1, 2, 3}, and define X R Y to mean that X n (1, 3,5} = Y nl 1, 3,51. 17. Let S be the set of ordered pairs of real numbers, let z = (3, -4), and define (xI, X2) R (Yl, Y2) to mean that 2 X1 +X
2 2
=
2 2 y 1 + Y2.
18. Let S be the set of ordered pairs of positive integers, let z = (5, 8), and define R so that (xI, x2 ) R (YI, means that xi
Y2)
+ Y2 = YI + X2 .
19. Write the equivalence relation on {1, 2, 3, 4, 5) that is induced by the partition with {I, 5}, (2, 4), and {3) as its partitioning subsets. 20. Write the equivalence relation on {1, 2, 3, 4, 5, 61 that is induced by the partition with (1, 3, 6), {2, 5}, and {4) as its partitioning subsets. 21. Let R be an equivalence relation on a set S. Prove that if x and y are any elements in S, then x R y is false if
and only if [x] n [y] = 0. 22. Let R be an equivalence relation on a set S, and let x and y be elements of S. Prove that if a E [x], b e [y], and [x] :A [y], then a R b is false. 23. What is wrong with the following argument that attempts to show that if R is a relation on set S that is both symmetric and transitive, then R is also reflexive? Since x R y implies y R x by the symmetric property, x R y and y R x imply x R x by the transitive property. Thus x R x is true for each x E S, and so R is reflexive. 24. Let RI and R2 be equivalence relations on sets S, and S2, respectively. Define a relation R on S, x S2 by letting (xI, x2 ) R (Yi, Y2) mean that xl R, yi and x2 R2 Y2. Prove that R is an equivalence relation on S, x S2 , and describe the equivalence classes of R. 25. Determine the number of relations on a set S containing n elements. 26. Call a relation R 'circular' if x R y and y R z imply z R x. Prove that R is an equivalence relation if and only if R is both reflexive and circular. 27. Let S be a set containing n elements, where n is a positive integer. How many ways are there to partition S into two subsets? 28. How many partitions are there of a set containing three elements? 29. How many partitions are there of a set containing four elements? 30. Prove Theorem 2.4. 31. Let S be any nonempty set and f any function with domain S. Define si R S2 to mean that f (sl) = f(s 2 ). Prove that R is an equivalence relation on S. 32. State and prove a converse to Exercise 31. 33. Let pm(n) be the number of partitions of a set of n elements into m subsets. Show that for I < m < n, pm(n + 1) = mpm(n) + pm. (n).
54
Chapter 2 Sets, Relations, and Functions
2.3
+
CONGRUENCE In this section we will discuss an important equivalence relation on the set of integers. This relation will lead to the study of number systems containing only a finite number of elements. Such number systems arise naturally in the study of computer arithmetic. We will begin by discussing some ideas from arithmetic. If m and n are integers and m + 0, the division algorithm states that n can be expressed in the form
n = qm, + i,
where
0 < r < Iml,
for unique integers q and r. (:Recall that Iml, the absolute value of m, is defined to be m if m > 0 and is defined to be -m if m < 0.) These integers q and r are called the quotient and remainder, respectively, in the division of n by m and can be found by the process of long division. Thus, for instance, in the division of 34 by 9, the quotient is 3 an(d the remainder is 7 because 34=3 9+7
and
0<7<9.
Note, however, that although -34 = 3 (-9) + (-7), 3 is not the quotient in the division of -34 by -9 because -7 is not a possible remainder. (It does not lie between 0 and 1-91 = 9.) In this case we have -34 = 4 (-9) + 2
and
0 < 2 < 9,
so that 4 is the quotient and 2 is the remainder in this division. If the remainder in the division of n by m is Ct, then we say that n is divisible by m (or that m divides n). Thus to say that n is divisible by m means that - is an integer. Now let m be an integer greater than 1. If x and y are integers, we say that x is congruent to y modulo m if x - y is divisible by m. If x is congruent to y modulo m, we write x - y (mod m); otherwise, we write x 0 y (mod m). We call this relation on the set of integers congruence modulo m. +
Example 2.17 Clearly, 3 =- 24 (mod 7) because 3 -24 = -21 is divisible by 7. And similarly, 98 - 43 (mod 11) because 98 -- 43 = 55 is divisible by 11. But 42 # 5 (mod 8) since 42
-
5 = 37 is not divisible by 8, and 4 # 29 (mod 6) since 4
is not divisible by 6.
-
29 = -25
+
The most common situation in which congruence occurs is in connection with the telling of time. Standard clocks and watches keep track of time modulo 12. Thus we say that 15 hours alter 7 o'clock is 10 o'clock, because 7 + 15 - 10
2.3 Congruence
55
(mod 12). Transportation schedules (such as train schedules) usually list times modulo 24 because there are 24 hours per day.
+
Example 2.18 Congruences often occur in applications involving error-detecting codes. In this example we will describe an application of such a code in the publishing industry. Since 1972 a book published anywhere in the world has carried a ten-digit code number called an International Standard Book Number (ISBN). For instance, the ISBN for Finite Mathematics by Spence and Vanden Eynden is 0-673-38582-5. By providing a standard identifier for books, these numbers have allowed publishers and bookstores to computerize their inventories and billing procedures more easily than if each book had to be referred to by author, title, and edition. An ISBN consists of four parts: a group code, a publisher code, an identifying number assigned by the publisher, and a check digit. In the ISBN 0-673-38582-5, the group code (0) denotes that the book was published in an English-speaking country (either Australia, Canada, New Zealand, South Africa, the United Kingdom, or the United States). The next group of digits (673) identifies the publisher, and the third group of digits (38582) designates this particular book among all those published by that publisher. The final digit of the ISBN (5) is the check digit, which is used to detect errors in copying or transmitting the ISBN. By using the check digit, publishers are often able to detect an incorrect ISBN and prevent the costly shipping charges that would result from filling an incorrect order. The check digit has eleven possible values: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, or X. (A check digit of X represents the number 10.) This digit is determined in the following way: multiply the first nine digits of the ISBN by 10, 9, 8, 7, 6, 5, 4, 3, and 2, respectively, and add these nine products to obtain a number y. The check digit d is then chosen so that y + d - 0 (mod 11). For example, the check digit for Finite Mathematics is 5 because 10(0) + 9(6) + 8(7) + 7(3) + 6(3) + 5(8) + 4(5) + 3(8) + 2(2) =
0 + 54 + 56 + 21 + 18 + 40 + 20 + 24 + 4 = 237
and 237 + 5 = 242 0 (mod 11). Likewise the ISBN for this book (found on the back of the title page) is 0-321-07912-4. Here the check digit is 4 because 10(0) + 9(3) + 8(2) + 7(1) + 6(0) + 5(7) + 4(9) + 3(l) + 2(2) =0+27+ 16+7+0+35+36+3+4= 128 and 128+4= 132 0(mod 11). For other uses of congruence in identification numbers, see suggested readings [2] and [3] at the end of this chapter. +
56
Chapter 2
Sets, Relations, and Functions
It can be shown thatx is congruent toy modulo m precisely when x = km + y for some integer k. In particular, x is congruent to the remainder in the division of x by m. Hence x is congrtient to y module m if and only if x and y have the same remainder when divided by m. (See Exercise 51.) From this fact the next theorem follows immediately. Theorem 2.5
Congruence module m is an equivalence relation. The equivalence classes for congruence modulo m are called congruence classes modulo m. The set of all the congruence classes modulo m will be denoted by Zm. It follows from Theorem 2.3 that any two congruence classes module m are either equal or disjoint. Moreover, in Zm, [x] = [y] if and only if x = y (mod m). Thus if r is the remainder in the division of x by m, then [x] = [r] in Z. So there are m distinct congruence classes in Zm, namely [0], [11], [2], . . ., [m - 1]. These correspond to the m possible remainders when dividing by m.
+
Example 2.19 In Z3 the distinct congruence --asses are [O = {..,-6, -3,0,3,6,9,..}, [1] = {..,-5, -2, 1, 4, 7, 10, .. }, and [2] = {.., -4,-1,2,5,8, 11,..}. Notice that each of the congruence classes in Z3 has many possible representations. For instance, [0] = [3] = [9] = [-12] and [2] = [-4] = [11] = [32]. + We would like to define addition and multiplication in Zm. There is a natural way to do this using the addition and multiplication of integers; simply define [x] + [yl =- x + y]
and
[x][y] = [xy].
In order for these definitions to make sense, however, we must be sure that they do not depend on the way in which congruence classes are represented. In other words, we must be certain thai these definitions depend only on the congruence classes themselves. For example, in Z 3 , we have [0] = [9] and [2] = [11]; so we must be certain that the sums [DI9+ [2] and [9] + [11] give the same answer. The following result gives us that assurance. Theorem 2.6
If x -x'
(mod m) and y - y' (mod m), then
(a) x + y - x' + y' (mod m)land (b)
xy
x'y' (mod m).
Proof. If x = x' (mod m) and y - y' (mod m), then there are integers a and b such that x = am + x' and Y bm + y'.
2.3
(a)
Congruence
57
Thusx+y=(am+x')+(bm+y')=(a+b)m+(x'+y').Then (x + y) -(x'+
y') = (a + b)m,
so that (x + y) -(x' + y') is divisible by m. This proves (a). (b) Likewise, xy = (am + x')(bm + y') = (amb + ay' + bx')m + x'y', so that xy-x'y'=(amb+ay'+bx')m. Hence xy-x'y' is divisible by m, proving (b). 0 Notice that part (b) of Theorem 2.6 implies that if x - z (mod m), then Xn = z' (mod m) for all positive integers n. Moreover, the definition of multiplication in Zm shows that [x]' = [z]n.
Example 2.20 In
Z6
we have [5] + [3] = [5 + 3] = [8] = [2]
since 8
-
2 (mod 6). Also, [5][3] = [5 .3] = [15] = [3]
because 15 - 3 (mod 6). And [8]4 = [2]4 = [24] = [16] =
since 8
2 (mod 6) and 16
4 (mod 6).
[4]
+
Example 2.21 In Z8 we have [4] + [7] = [4 + 7] = [11] =[3] since 11
=
3 (mod 8). Also [4][7]= [4 7] = [28] = [4]
because 28
=
4 (mod 8). And [7]9 = [-1]9 = [(-1)9] = [-1] = [7]
since 7
1 (mod 8).
+
Example 2.22 A scientific recording instrument uses 1 foot of paper per hour. If a new roll of paper 100 feet long is installed at 11 A.M., at what hour of the day will the instrument run out of paper? To answer this question, we will number the hours of a day with midnight being hour 0, 1 A.M. being hour 1, etc. Using arithmetic in Z24, we see that the
58
Chapter2
Sets, Relations, and Functions
paperwillrunoutattime[ II] 4 [100] = [111] = [15]. Sincehour l5corresponds to 3 P.M., we see that the paper will run out at 3 P.M. 0la
1
Example 2.23 In the Apple Pascal programming language, integer variables must have values between -32,768 and 32,767 inclusive. This range permits integer variables to have 65,536 = 216 different va- ues. Moreover, all integer arithmetic is done modulo 65,536, with answers given in the range above. Thus, 60,000 + 10,000 = 4464 since 70,000
=
4464 (mod 5:5,536). Likewise 23,000 + 3,000 = -29,536
because 36,000 - 36,000 -- 65,536 - -29,536 (mod 65,536). Similarly, we see that 400 500 = 3392 and 123 487 = -5635. o
Example 2.24
+
On a Sharp model EL-506S ca curator, the value of 230 is given as 1,073,741,820. If this value is correct, then the last digit of 228 = must be 5. But clearly no power of 2 can be odd, so the last digit of 230 must be wrong. What is the correct last digit of 230? It is easy to see that twvo positive integers have the same last digit if and only if they are congruent modulo 10. But in Z 10 [230] = [25]6 = [32]6 = [2]6
-
[26] = [64] = [4].
Hence, the last digit of 230 is 4. Actually 230 = 1,073,741,824.
c
EXERCISES 2.3 In Exercises 1-8 find the quotient and remainderin the division of n by m. 1. n= 67andm=9
2. n- 39andm=-13
4. n = 103 andm =8
5. n = -54 andm
7. n =-89 and m
-10
In Exercises 9-16 determine if p
6
3. n= 25andm=42 6. n =-75 andm = 23
8. n =-57 andm -=-11 =
q (mod m).
9. p=29,q=-34,andm=7
10. p-=47,q=8,andm=11
11. p = 96, q = 35, and m = 10
12. p = 21, q = 53, and m= 8
13. p = 39, q = -46, and m = 2
14. p = 75, q = -1, and m
15. p = 91, q = 37, and in = 9
16. p = 83, q = -23, and m zz: 6
=
19
2.3 Congruence
59
In Exercises 17-36 perform and indicated calculations in Zm. Write your answer in the form [r] with 0 < r < m. 17. [8] + [6] in Z12
18. [9] + [11] in Z15
19. [5] + [10] in Z,
20. [9] + [8] in Z 13
21. [23] + [15] in Z8
22. [12] + [25] in Z 7
23. [16] + [9] in Z6
24. [43] + [31] in Z22
25. [8][7] in Z6
26. [9][3] in Z4
27. [4][11] in Z9
28. [3][20] in Z
29. [5][12] in Z8
30. [8][11] in Zs
31. [9][6] in Zio
32. [16][3] in Z 7
33.
34. [1l]
35. [11]9 in Z12
36. [13]6 in Z 15
[9]7
in Z7
in Z 5
37. A newspaper teletypewriter that is in constant operation uses 4 feet of paper per hour. If a new roll of paper 200 feet long is installed at 6 P.M., at what hour of the day will the machine run out of paper? 38. A hospital heart monitoring device uses 2 feet of paper per hour. If it is attached to a patient at 8 A.M. with a supply of paper 150 feet long, at what hour of the day will the device run out of paper? 39. Use Example 2.18 to determine the correct check digit for the ISBN that has 3-540-90518 as its first nine digits. 40. Use Example 2.18 to determine the correct check digit for the ISBN that has 0-553-103 10 as its first nine digits. 41. The Universal Product Code (UPC) is a 12-digit number found on products that enables them to be identified by electronic scanning devices. The first six digits identify the country of origin and the manufacturer, the next five digits indicate the product, and the last digit is a check digit. If the first 11 digits of a UPC are a, a2 , . . ., atl, then the check digit a 12 is chosen so that 3a, + a2 + 3a3 + a4 + .. + 3a11 + a 2 =- 0 (mod 10). Find the correct check digit for the product that has 0 70330 20118 as its first 11 digits. 42. Federal Express packages carry a 10-digit identification number. The last digit is a check digit that equals the remainder in the division of the first nine digits by 7. Find the last digit of the Federal Express package tracking number with 903786299 as its first nine digits. 43. Use Example 2.23 to determine the result of the operations 26,793 + 28,519 and 418 . 697 if performed on integer variables in the Apple Pascal programming language. 44. Use Example 2.23 to determine the result of the operations 4,082 + 30,975 and 863 . 729 if performed on integer variables in the Apple Pascal programming language. 45. Let A denote the equivalence class containing 4 in Z6 and B denote the equivalence class containing 4 in Z8 . Is A = B? 46. In Z8 which of the following congruence classes are equal: [2], [7], [10], [16], [39], [45], [-1], [-3], [-6], [-17], and [-23]? 47. Let R be the equivalence relation defined in Example 2.13. Give an example to show it is possible that p R x and q R y are both true, yet (p + q) R (x + y) and pq R xy are both false. Thus the definitions [p] + [q] = [p + q] and [p][q] = [pq] do not define meaningful operations on the equivalence classes of R. 48. Give an example to show that in Zm it is possible that [x] #0[0] and [y] :A [0] but [x][y] = [0]. 49. Let m and n be positive integers such that m divides n. Define a relation R on Z, by [x] R [y] in case x (mod m). Prove that R is an equivalence relation on Z,. What can be said if m does not divide n?
=- y
50. A project has the nine tasks T1, T2, T3, T4 , T5, T6, T7, T8, and T9 . Task Tj takes i days to complete for i = 1, 2, .. , 9. If i divides j and i 54 j, then task Tj cannot be started until task T; is completed. (a) Make a PERT diagram for this project. Include the task symbols TI, T2, . . ., T9 and the times in each circle. (b) Apply the PERT method to assign to each task the shortest time until it can be completed. What is the shortest time to complete the whole project? (c) What is the critical path?
60
Chapter 2 Sets, Relations, and Functions
51. (a) Prove that x y (mod m) if and only if x = km + y for some integer k. (b) Prove that x - y (mod m) if and only if x and y have the same remainder when divided by m. 52. (a) Suppose that a, b, and c are integers such that x - y (mod m). Prove that ax 2 + bx + c - ay2 + by + c (mod m).
(b) Show that the result in part (a) may be false if a, o, and c are not all integers, even if ax2 + bx + c and ay2 + by + c are both integers.
2.4*
+
PARTIAL ORDERING RELATIONS In Section 1.1 we discussed a construction example in which the tasks necessary to build a house, the number of days needed to complete each task, and the immediately preceding tasks are as given in the following table. In Section 1.1 we saw that by doing some of the tasks simultaneously, all the tasks could be finished (and so the house could be completely built) in 45 days. In this section we will consider a different question: ina what sequence should the tasks be performed if all the tasks are to be carried out by a group of individuals who are capable of doing only one task at a time' Task
Time in Days
Preceding Steps
A
Site preparation
4
None
B
Foundation
6
A
C
Drains and services
D
Framing
3
A
10
B
E F
Roof
5
D
Windows
2
E
G
Plumbing
4
C, E
H
Electrical work
3
E
I
Insulation
2
G, H
J
Shell
6
F
K
Drywall
5
1,J
L
Cleanup and paint
3
K
M
Floors and trim
4
L
N
Inspectior
10
I
In this construction project, certain tasks cannot be started until others are completed. Task G (the plumbing), for example, cannot be started until both tasks C and E are completed. Recall that there are other requirements that are not as readily apparent from the table In this case task G cannot be started until each of A, B, C, D, and E are completed. This is because task E cannot be started until D is completed, D cannot be started until B is completed, and B cannot be started
2.4* PartialOrderingRelations
61
until A is completed. Seeing all of the dependencies among the tasks is easier in the following diagram, where an arrow from task X to task Y signifies that task Y cannot be started until task X and all of its predecessors have been completed. If we omit the arrowheads in Figure 2.5 by agreeing, as in Section 1.1, that all arrows point from left to right, then the resulting diagram is shown in Figure 2.6. (It is essentially the same as the one in Figure 1.8.) A
B
D
E
F
J
K
L
M
H
C
G
I
N
FIGURE 2.5
The sequencing of the tasks described in the original table and pictured in the diagram of Figure 2.6 creates a relation R on the set of tasks S = {A, B, C, D, E, F, G, H, I, J, K, L, M, N}, where X R Y means either that X = Y or that Y cannot be started before X is completed. This relation has some of the special properties that we encountered in Section 2.2. First, this relation is obviously reflexive since each task in S is related to itself. Second, the relation is transitive. To see why, suppose that X R Y and Y R Z. If X = Y or Y = Z, then clearly X R Z. Otherwise task Y cannot be started before task X is completed and task Z cannot be started before task Y is completed. It follows that task Z cannot be started before task X is completed. Thus in each case we have X R Z, proving that R is transitive. A
B
D
E
F
J
K
L
M
FIGURE 2.6
However, this relation is not symmetric because A R B is true but B R A is false. But the relation does have the following property: if both X R Y and Y R X hold, then necessarily X = Y. For if X =A Y and task Y cannot be started before task X is completed and also task X cannot be started before task Y is completed, then neither X nor Y can be started, and so the project cannot be finished! A relation R on a set S is called antisymmetric if, whenever x R y and y R x are both true, then x = y. A relation R on a set S is called a partial ordering relation or, more simply, a partial order if it has the following three properties.
62
Chapter2
Sets, Relations, andFunctions
(1) (2)
R is reflexive, that is, x R x is true for every x in S. R is antisymmetric, that is, whenever both x R y and y R x are true, then x = y.
(3)
R is transitive, that is, x R z is true whenever x R y and y R z are both true.
Many familiar relations are partial orders, as the following examples show.
Example 2.25 The equality relation on any set is obviously antisymmetric. Since equality is also reflexive and transitive, it is a partial order on any set. + Example 2.26 Let S be any collection of sets. For A, B E S define A R B to mean that A C B. Then R is an antisymmetric relation on S, for if X R Y and Y R X are both true, then X C Y and also Y C X, from which it follows that X = Y. Moreover, R is reflexive because X C X for every X E S, and R is transitive because X C Y and Y C Z imply that X C Z for all X, Y, Z e S. Therefore R is a partial order on S. 4 Example 2.27 Let S be a set of real numbers. The familiar less than or equal to relation (<) is an antisymmetric relation on S. Since this relation is also reflexive and transitive, it is a partial order on S. 4 Example 2.28 Let S be the set of positive integers. Define a relation R on S by letting a R b mean that a divides b (as defined in Section 2.3). Suppose that both x R y and y R x are true. Then :=m Y
and
-=n x
where m and n are positive integers. Thus x = my = m(nx) = (mn)x. Since m and n are positive integers such that mn = 1, it follows that m = n = 1. Hence x = my = y. Therefore R is an antisymmetric relation on S. Since this relation is also reflexive and transitive (see Example 2.10), it is a partial order on S. 4
Example 2.29 Congruence modulo 6 is not an antisymmetric relation on the set of integers because both 3 - 9 (mod 6) and 9 - 3 (mod 6) are true, but 3 :7 9. Hence congruence module 6 is not a partial order on the set of integers. +
2.4* PartialOrderingRelations
63
Example 2.30 Today Mr. Webster is scheduled to interview three applicants for a summer internship at 9:00, 10:00, and 11:00, and Ms. Collins is to interview three applicants at the same times. Unfortunately, both Mr. Webster and Ms. Collins have become ill, and so all six interviews are to be conducted by Ms. Herrera. She has decided to schedule the applicants to be interviewed by Ms. Collins in the order in which they were to appear followed by the applicants to be interviewed by Mr. Webster in the order in which they were to appear. Thus the sequence in which the interviews are to be conducted is (Collins, 9:00), (Collins, 10:00), (Collins, 11:00), (Webster, 9:00), (Webster, 10:00), and (Webster, 11:00). The ordering of applicants used here by Ms. Herrera is an example of a lexicographic order. + In Example 2.30 there are two sets SI = {Webster, Collins}
and
S2 = 19:00, 10:00, 11:00}
and two partial orders RI and R2 on those sets. In this case the relation RI on SI is alphabetical order, and the relation R2 on S2 is numerical order (i.e., less than or equal to). The sequence in which the interviews are to be conducted is obtained by extending R1 and R2 to a partial order on SI x S2. More generally, suppose that RI is a partial order on set SI and that R2 is a partial order on set S2. It is possible to use RI and R2 to define arelation R on SI x S2. We define R by (a, a2) R (hi, b2) if and only if one of the following is true: (1) (2)
a,i& bi anda, RI b, or a, = bi and a2 R2 b2 -
This relation is called the lexicographic order on SI x S2. This ordering is also called 'dictionary order' because it corresponds to the sequence in which words are listed in a dictionary. Theorem 2.7
If RI is a partial order on set SI and R2 is a partial order on set S2, then the lexicographic order is a partial order on SI x S2. Proof Let R be the lexicographic order on SI x S2. For any (ai, a2) e SI x S2, we have (a, a2 ) R (ai, a2) by condition (2) in the definition of the lexicographic order. Hence R is reflexive. Next suppose that (ai, a2) and (bl, b2 ) are elements of SI x S2 such that both (a, a2 ) R (b1, b2 ) and (bl, b2) R (a, a2 ). If ai #&bl, then condition (1) in the definition of the lexicographic order applied to (a, a2 ) R (bi, b2) implies that a, RI bl. Moreover, condition (1) applied to (bl, b2) R (a], a2) implies that b1 RI ai. Because RI is antisymmetric and both ai RI b, and b1 RI a, are true, we must have a, = bl, in contradiction of our assumption that at :A bl.
64
Chapter2
Sets, Relations, and Functions
Thus a, = b, and condition (2) must apply. Applying (2) to (a, a2) R (bl, b2 ) implies that a2 R2 b2 , and app l ing condition (2) to (bl, b2 ) R (a, a2 ) implies that b2 R2 a2 . Since R2 is antisymnnetric, then a2 = b2 . But if a, = b1 and a2 = b2 , then (a, a 2 ) = (b1, b2), proving that R is antisymmetric. An argument similar to that in the preceding paragraph shows that R is also transitive. Hence the lexicographic order on SI x S2 is a partial order. X Note that despite the similarity in their names, the concepts of a symmetric relation and an antisynnictric relation are independent of each other. The relation in Example 2.25 is both symmetric and antisymmetric, the relations in Examples 2.26 through 2.28 are antisymmetric but not symmetric, and the relation in Example 2.29 is symmetric but not antisymmetric. Moreover, it is not difficult to find examples of relations on sets that are neither symmetric nor antisymmetric. The reason for using the name partial order is that there may be elements in the underlying set that cannot be compared. For example, if we consider the partial order 'is a subset of' (Cc)on the collection S of all subsets of {1, 2, 3, 41, we see that the elements A -= {1, 21 and B = {1, 3} of S cannot be compared, that is, neither A C B nor B - A is true. If R is a partial order on S and S' is any subset of S, then R induces a partial order R' on S' by defining x R' y if and only if x R y. (In other words, two elements of S' are related by I?' if and only if the same two elements, regarded as elements of S, are related by P.) Using the ordered pair notation for a relation, this relation R' can be defined as R' = R n (S' x S'). So, for instance, in Example 2.28 the 'divides' relation induces a partial order on any subset of the set of positive integers. A partial order R on set S is called a total order (or a linear order) on S if every pair of elements in S can be compared, that is, if for every x, y E S, we have x R y or y R x. Thus, tie 'less than or equal to' relation (<) on the set of all real numbers is a total order. However, in Example 2.28 the 'divides' relation is not a total order on the set of positive integers because neither 6 divides 15 nor 15 divides 6, and so the positive integers 6 and 15 cannot be compared. Let R be a partial order on set S. An element x in S is called a minimal element of S (with respect to R) if the only element s E S satisfying s R x is x itself, that is, if s R x implies s = x. Likewise an element z in S is called a maximal element of S (with respect to R) if the only element S E S satisfying z R s is z itself, that is, if z R e mplies s = z. Minimal or maximal elements need not exist for a particular partia- order.
Example 2.31 The 'is a subset of' relation (C) is a partial order on the collection S of all subsets of {1, 2, 31. Here 0 (the empty set) is a minimal element of S because
2.4* PartialOrderingRelations
65
A C 0 implies that A = 0. Also, f 1, 2, 31 is a maximal element of S because {1, 2, 31 C A implies that A = {1, 2, 3}. +
4
Example 2.32 Let S denote the set of real numbers greater than or equal to 0 and less than or equal to 7. The 'less than or equal to' relation (<) is a total order on S, and with respect to this relation 0 is a minimal element of S and 7 is a maximal element of S. +
+
Example 2.33 The 'less than or equal to' relation (<) is a total order on the set of real numbers, but here the set has neither minimal nor maximal elements. o
+
Example 2.34 The 'is a subset of' relation (C) is a partial order on the collection S of subsets of ( 1, 2, 3, 41 that contain 1, 2, or 3 elements. In this setting the one-element subsets of S are minimal elements of S and the three-element subsets of S are maximal elements of S. + In Example 2.33 we saw that a set need not have minimal or maximal elements with respect to a particular partial order. This cannot happen if the set is finite.
Theorem 2.8
Let R be a partial order on a finite set S. Then S has both a minimal and a maximal element with respect to R. Proo.f Pick any element si E S. If there is no element s E S other than Si such that s R sI, then S1 is a minimal element of S. Otherwise there exists an element 52 E S such that S2 :A S, and S2 R sI. If there is no element s E S other than S2 such that s R S2 , then S2 is a minimal element of S. Otherwise there exists an element S3 E S such that S3 $ s2 and s3 R S2 . Note that S3 + S1 because 53 = SI would imply that s, R S2 . Since R is antisymmetric and we have S2 R sI, it would follow that s, = S2 , contrary to our choice of S2. Because S is a finite set, continuing in this manner must produce a minimal element of S. The proof that S contains a maximal element is similar. f ?'.se
Diligr
's;e9l ImS,'
We have already seen that the diagram in Figure 2.6 is helpful in visualizing the precedence relations among the tasks in the construction example discussed at the beginning of this section. Similar diagrams can be constructed for any partial order R on a finite set S. Such diagrams are named Hasse diagrams after the German number theorist Helmut Hasse (1898-1979).
66
Chapter2 Sets, Relations, and Functions
To construct a Hasse diagram for the partial order R on set S, we represent each element of S by a point, arid for each pair of distinct elements x and y in S, we draw an arrow from the point representing x to the point representing y whenever x R y and there is no s E S other than x and y such that x R s and s R y. Finally, arrange each arrow so that its initial point is below its terminal point and remove all the arrowheads. Thus bv convention, a Hasse diagram is read from bottom to top, so that all the line segmer ts between points are regarded as pointing upward. For instance, a Hasse diagram for the construction example would be drawn as in Figure 2.7. (This is just the diagram in Figure 2.6 rotated by 90°.) 11,2,31 Ma
IN
(2,31
11,21
Kit JO Fat 1
~11
H G
X
(I
131
Ei Do4
I
IC
Bei0 At4rK'
0
FIGURE 2.7
FIGURE 2.8
It is easy to detect minima, and maximal elements from a Hasse diagram. A minimal element is one that is joined by a segment to no lower point. Similarly, a maximal element is one that is joined by a segment to no higher point. Hence in Figure 2.7 we see that A is the only minimal element and that M and N are the only maximal elements in the precedence relation for the tasks in the construction example. Example 2.35 A Hasse diagram for the relation in Example 2.31 consists of eight points corresponding to the eight element Af S. A line segment is drawn upward from A to B whenever A C B and there is no C in S other than A or B such that A C C C B. The resulting diagram is showII in Figure 2.8. ' Example 2.36 Let R be the partial order 'divides' on the set S = {2, 3,4, 6, 8, 20, 24,48, 100, 120}.
2.4*
PartialOrderingRelations
67
48
24
6
3 FIGURE 2.9
A Hasse diagram for R and S is shown in Figure 2.9. Here 2 and 3 are minimal elements of S, and 48, 100, and 120 are maximal elements of S. + 4
Example 2.37 Let R be the partial order 'is a subset of' (defined in Example 2.26) on the set S = {l}, {2}, {3), {4), {1, 2}, 11, 51, {3, 6}, (4, 6), J0, 3, 61, {1, 5, 8), J0, 3, 4, 61). A Hasse diagram for R and S is shown in Figure 2.10. Here J1}, {2), J3} and (4) are minimal elements of S, and {1, 21, J1, 5, 81, and J0, 3, 4, 6} are maximal elements of S. + (1,5,8)
(1,51
{0,3,4,61
{1,2}
(0,3,61
/{3,6) {1)
{2)
{3)
(4,6) {4)
FIGURE 2.10
Now let us return to the question asked at the beginning of this section: In what
sequence should the tasks in the construction example be performed if all the tasks are to be carried out by a group of individuals who are capable of doing
only one task at a time? Since the tasks must be performed sequentially, we are seeking a total order T on the set of tasks that contains the original partial order R (the one depicted in the Hasse diagram in Figure 2.7). That is, we want a total order T such that x R y implies x T y.
68
Chapter2 Sets, Relations, and Functions
The process of constructing a total order that contains a given partial order is called topological sorting. The following algorithm will produce such a total order for any partial order on a finite set. It is based on the fact that R induces a partial order on every subset of S.
Topological Sorting Algorithm Given a partial order R on a finite set S, this algorithm produces a total order T such that x R y implies x T y. Step I Step 2
(initialization) Set k = t and S' = S. (pick next element) while S' is nonempty (a) Choose any minimal element to the order induced by R. (b) Delete Sk from S'. (c) Replace k with k + 1.
Sk
of S' with respect
endwhile Step 3
(define T) Define the total order r on S by si T sj if and only if i < j.
To illustrate the use of the 1opological sorting algorithm, we will construct a total order for the set S of tasks in the construction example that contains the given partial order. From the Hasse diagram in Figure 2.7, we see that A is the only minimal element of S. Therefore we take s, = A and delete A from S. The corresponding Hasse diagram f Dr this new set is shown in Figure 2.1 1. Here both B and C are minimal elements, and we may arbitrarily select either one. Suppose we take s2 = C. A Hasse diagram for this new set is shown in Figure 2.12. Since B is the only minimal element in this new set, we take s3 = B. M 0
N
L4 0
L
Ki
K
Ji F I
H
N
M
I
J
G
F
IG
E
EI Di I B.
C
D B
FIGURE 2.11
FIGURE 2.12
2.4*
PartialOrderingRelations
69
Continuing in this manner, we choose s4 = D, s5 = E, s6 = G, s7 = H, X8 = I,s9 = F, s10 = J, sII = N, s12 = K, s13 = L, and s 14 = M. Thus a sequence in which the tasks can be performed that contains the given partial order of the tasks is A, C, B, D, E, G, H, I, F, J, N, K, L, and M. Of course, other such sequences are possible because at various stages of the algorithm there were several possible minimal elements that could be chosen in step 2. Another possible sequence is A, B, D, E, F, J, C, H, G, I, K, L, M, and N. Each of these sequences corresponds to a total order of the tasks in S that contains the given partial order, namely, the order formed by defining X to be related to Y if and only if X = Y or X occurs before Y in the sequence. Example 2.38 To get the best seats at the basketball games of a large university, one must belong to the Basketball Booster Club. Occasionally, the athletic department receives complaints from members of the club who believe that their seats are inferior to those of another member. Experience shows that the complainer can be mollified by being assured that the other member either has been a member of the booster club longer or is giving a larger donation. Let us define a relation R on the members of the Basketball Booster Club by x R y in case both of the following are true: (1) (2)
x has belonged to the booster club at least as many years as y the current contribution of x is at least as great as that of y.
Suppose that the athletic department receives seating complaints from or about the following members of the booster club: Adams, Biaggi, Chow, Duda, El-Zanati, and Friedberg. It has agreed to examine the seats assigned to these six persons and make changes if the seating is found to be inequitable. The table below gives the number of years of membership in the Basketball Booster Club and the current contribution of these members. Member x
Years in the Club
Current Contribution
Adams
6
$150
Biaggi
3
$200
Chow
5
$600
Duda
8
$500
El-Zanati
7
$400
Friedberg
4
$450
70
Chapter2
Sets, Relations, and Functions
Because no two of these six persons have the same number of years of membership and the same cur-ent contribution, the relation defined above is a partial order on the set S= {A, B, C, D, E, F), where each person is denoted by his or her initial. The Hasse diagram for this partial order on the set S is shown in Figure 2.13. B
A
FE
FIGURE 2.13
If we apply the topological sorting algorithm to this situation (choosing minimal elements in alphabetical order when there is a choice), we get the sequence C, D, E, F, A, B. If the athletic department assigns seats in this sequence (with Chow getting the best seats, etc.), then it can answer any complaint about the seats of these six persons. For example, Chow has better seats than Duda because Chow's contribution is larger, and El-Zanati's seats are better than Friedberg's because she has been a member of the booster club longer. +
EXERCISES 2.4 In Exercises 1-8 determine if the given relation R is a partialorder on set S. Justify your answer. 1. 2. 3. 4. 5. 6. 7. 8.
S = {1, 2,3) and R = {(1, 1), (2, 3), (1,3)1 S = {1, 2, 3)and R = {(l, 1), (1, 2), (2, 2), (2, 3), (3, 3), (3, 1), (1, 3), (3, 2)) S = {3,4,5)andR = {(5,5),(5,3),(3,3),(3,4),(4,4),(5,4)) S = {0, {1}} and R = {({1}, 0), ({1}, {1}), (0, 0)} S = {1, 2, 3, 4} and x R y if y divides x S is the collection of all subsets of {I, 2, 3} and A R B if A C B or B C A S = Z6 andx R y ifx = y orx = y + [1] S is the collection of all subsets of {1, 2, 3) and A R B if IAi< I
In Exercises 9-12 determine a Hasse diagramfor the given partialorder R on set S. 9. S= {1,2,3)andR ={(I,1),(3,1),(2,1),(2,2),(3,3)) 10. S={1,2,3,4}andR = {(1,1),(2,2),(3,3),(4,4),i2,4),(3,1)}
2.4* PartialOrderingRelations
11. S
=
71
1,2,3,4,5,6,7,81 andx R y ifx divides y
12. S is the collection of all subsets of {1, 2, 3, 4} with an even number of elements and A R B if A C B In Exercises 13-16 construct the partialorder R on set S from the given Hasse diagram. 13.
14.
12
b
6d
2
15.
3C
16.
A
*A'
2
a
x 0
2
8
In Exercises 17-20 identify the minimal and maximal elements of S with respect to the given partialorder R. 17. S= {1, 2, 3, 4, 5, 61 and x R y if x divides y 18. S is the set of nonempty subsets of { 1, 2, 31 and A R B if B C A
19. S={1,2,3,4}andR {(1,1),(2,2),(3,3),(4,4),(4,1),(3,1)} = 20. S is the set of all real numbers x such that 0 < x < 1 and x R y if x < y In Exercises 21-24 apply the topological sorting algorithm to the given set S and partial order R, and give the sequence in which the elements of S are chosen. 21. S =
1,2,3,41 and R = {(l, 1), (2, 2), (3, 3), (4,4), (1, 3), (2, 4), (4,3), (2, 3)}
22. S = {1, 2, 3, 41 and x R y if x divides y 23. S is the collection of all subsets of {1, 2, 31 such that the sum of their elements is less than 5 and A R B if ACB 24. S = {1, 2, 3, 4, 6, 121 andx R y if x divides y 25. Let S be the set of nonzero integers. Define a relation R on S by letting a R b mean that aa is an integer. Is R an antisymmetric relation on S? 26. Find a relation R on a set S that is neither symmetric nor antisymmetric. 27. Give an example of a subset S of the set of positive integers such that S has at least three elements and the 'divides' relation is a total order on S. 28. Consider the 'divides' relation on the set of positive integers greater than 1. Determine all the minimal and maximal elements of S. 29. Give an example of a set S and a partial order R on S such that S has exactly three minimal elements with respect to R and exactly four maximal elements with respect to R.
72
Chapter2
Sets, Relations, and Functions
30. Let SI {1, 2, 31 and S2 = {1, 2, 3, 4), and let RI be t ie 'less than or equal to' relation on SI and R2 be the 'less than or equal to' relation on S2. The lexicographic order is a total order on SI x S2. List the elements of SI x S2 in the sequence given by the lexicographic order. 31. Let S be a set with five elements, and let R be a total oider on S. Draw a Hasse diagram for R. 32. Let S, = {7, 8, 91 and S2 = {2, 3, 4, 6}. Let R, be the 'less than or equal to' relation on S, and R2 be the 'divides' relation on S2. Construct a Hasse diagram for the lexicographic order on S, x S2. 33. Let R be a partial order on S and S' be any subset of S. Prove that R' = R n (S' x S') is a partial order on S'. 34. Complete the proof of Theorem 2.8 by showing that if R is a partial order on a finite set S, then S has a maximal element with respect to R. 35. Let R be a total order on set S. Prove that if S has a minimal element, then the minimal element is unique. 36. Let R be a partial order on S, and suppose that x is a anique minimal element in S. (a) Prove that if S is finite, then x R s for all s in S. (b) Show that the conclusion in (a) need not be true i f S is infinite. 37. Let RI be a total order on set S, and R2 a total order mn set S2. Prove or disprove that the lexicographic order is a total order on S, x S2. 38. Complete the proof of Theorem 2.7 by proving that the lexicographic order on S, x S2 is transitive. 39. Let RI be a partial order on set SI and R2 a partial onler on set S2. (a) Prove that if a is a minimal element of S, and b is .i minimal element of S2, then (a, b) is a minimal element of SI x S2 with respect to the lexicographic order. (b) State and prove a result about a maximal element cf S, x S2 that is similar to the result in (a). 40. Let S be a set containing exactly n elements. How many relations on S are both symmetric and antisymmetric? 41. Let S be a set containing exactly n elements. How many total orders on S are there? 42. Let S be a set containing exactly n elements. How many antisymmetric relations on S are there?
2.5
+
FUNCTIONS In the matching problem described in Section 1.2, we are seeking an assignment of flights and pilots such that at many pilots as possible are assigned to flights that they requested. We saw in Section 2.2 that the list of destinations requested by the pilots gives rise to a relation between the set of destinations and the set of pilots. An assignment of flights and pilots can therefore be thought of as a special type of relation between the set of destinations and the set of pilots in which exactly one pilot is assigned to each destination. We will now study this special type of relation. If X and Y are sets, a function f from X to Y is a relation from X to Y having the property that, for each element x in X, there is exactly one element y in Y such that x f y. Note that because a relation from X to Y is simply a subset of X x Y, a function is a subset S of X x Y such that for each x E X there is a unique y E Y with (x, y) in 'S.
2.5 Functions
73
Example 2.39 LetX = {1,2,3,4},andY
{5,6,7,8,9).Then
f = {(l, 5), (2, 8), (3, 7), (4, 5)) is a function from X to Ybecause, for each x E X, there is exactly one y E Y with (x, y) in f . Note that in this case not every element of Yoccurs as the second entry of an ordered pair in f (6 and 9 do not occur in any ordered pair in f ), and some element of Y (namely 5) occurs as the second entry of several ordered pairs in f. On the other hand, g ={(I, 5), (1, 6), (2, 7), (3, 8), (4, 9)) is not a function from X to Y because there is more than one y C Y (namely, 5 and 6) such that (1, y) belongs to g. And h = {(1, 5), (2, 6), (4, 7)} is not a function from X to Y because there is no element of Y associated with some element (namely 3) of X. However, h is a function from {1, 2, 41 to Y. + We denote that f is a function from set X to set Y by writing f: X Y. The sets X and Y are called the domain and codomain of the function, respectively. The unique element of Y such that x f y is called the image of x under f and is written f (x), read 'f of x.' For the function f defined in Example 2.39, for instance, f(1) = 5, f(2) = 8, f(3) = 7, and f(4) = 5. Thus writing y = f(x) is another way of expressing that (x, y) belongs to f. It is often useful to regard a function f: X -* Y as a pairing of each element x in X with a unique element f (x) in Y. (See Figure 2.14.) In fact, functions are often defined by giving a fonrmula that expresses f (x) in terms of x; for example, f (x) = 7x2 - 5x + 4. x
Y
FIGURE 2.14
Note that in order for a set X to be the domain of a function g, it is necessary that g(x) be defined for all x in X. Thus, g(x) = ax cannot have the set of all
74
Chapter 2 Sets, Relations, and Functions
real numbers as its domain and codomain, for g(x) is not a real number if x < 0. Likewise, g(x) = x cannot hive the set of all nonnegative real numbers as its domain because g(x) is not defined if x = 0. 01
Example 2.40 Let X = {-1, 0, 1, 2}and Y = {-4, -2, 0, 21. The function f: X by f (x) = X2- x behaves as follows. The The The The
image image image image
Thus, f(-l)
=
of-I under of 0 under f of 1 under f of 2 under f
f is the element (is the element (0)2 is the element (1)2 is the element (2)2
2, f(O) =0. f1) =0, and f(2)
1)2 - (0) -(1) - (2)
X
=
-*
Y defined
(-1) = 2 in Y. = 0 in Y. = 0 in Y. = 2 in Y.
2. See Figure 2.15. 0
Y
.2
10
2'2 FIGURE 2.15
01
Example 2.41 Let X denote the set of all real numbers and Y denote the set of all nonnegative real numbers. The function f: X -+ Y defined by g(x) = Ix I assigns to each element x of X its absolute value x . Tle domain of g is X and the codomain is Y. +
i)
Example 2.42 Let X be the set of all real numbers between 0 and 100 inclusive, and let Y be the set of all real numbers between 32 and 212 inclusive. The function F: X -+ Y that assigns to each Celsius temperature c its corresponding Fahrenheit temperature F(c) is defined by F(c) = 2c d- 32. Unlike the preceding examples, it is not immediately clear that the image under F of each element in X is an element of Y. To see that this is so, we must show that 32 < F(c) < 212 if 0 < c < 100. But if
0<
C < 100,
2.5 Functions
75
then 9 9 0< -C< - *100= ~5 - 5
180.
So 9 32 < c-+32 < 212. -5 Hence F(c) is an element of Y, and so F is a function with domain X and codomain Y. + Example 2.43 Let Z denote the set of integers. The function G: Z -* Z that assigns to each integer m the number 2m is defined by G (mi) = 2m. The domain and the codomain of G are both equal to Z. + +
Example 2.44 Let Z denote the set of integers and Z12 the set of congruence classes modulo 12. The function h: Z -* Z 12 defined by h(x) = [x] is the function that assigns to each integer its congruence class in Z12. Here the domain of h is Z and the codomain is Z12. 1
+
Example 2.45 In Apple Pascal, there is a built-in function named MOD that behaves as follows: If N and M are positive integers, the value of the expression N MOD M is the remainder in the division of N by M. Therefore, we can regard MOD as a function with {(N, M): N and M are positive integers) as its domain and the set of nonnegative integers as its codomain. +
+
Example 2.46 Let X denote the set of all subsets of U = {1, 2, 3, 4, 5), and let Y be the set of nonnegative integers less than 20. If S is an element of X (i.e., if S is a subset of U), define H(S) to be the number of elements in S. Then H: X -. Y is a function with domain X and codomain Y. + We have already noted that it is possible for a function to assign the same element of the codomain to different elements in the domain. The function g(x) =
IxI in Example 2.41, for instance, assigns to both -4 and 4 in the domain the element 4 in the codomain. If this does not occur, that is, if no two distinct elements
76
Chapter2 Sets, Relations, and Functions
of the domain are assigned the same element in the codomain, then the function is said to be one-to-one. Thus, to show that a function f: X -- Y is one-to-one, we must show that f(x 1 ) =- (r2) implies xi = x 2. It is also possible that one or more elements of the codomain are not paired by a function to any element in the domain. The function f in Example 2.40, for instance, does not pair the elements -4 and -2 in the codomain with any elements in the domain. Thus, in this case, only the elements 0 and 2 in the codomain are paired by f with elements in Ihe domain. The subset of the codomain consisting of the elements that are paired with elements of the domain is called the range of the function. In Example 2.40 the range of f is 10, 21. If the range and codomain of a function are equal, then the function is called onto. Hence, to show that a function f: X -) Y is onto, we must show that if y e Y, then there is an x E X such that y = f (x). A function that is both cne-to-one and onto is called a one-to-one correspondence. Note that if f: X -* Y is a one-to-one correspondence, then for each y E Y there is exactly one IV E: X such that y = f (x). For any set X, the function Ix: X -* X defined by Ix(x) = x is a one-to-one correspondence. This function is called the identity function on X.
Example 2.47 The function f in Example 2.40 is neither one-to-one nor onto. It is not oneto-one because f assigns the same element of the codomain (namely 0) to both 0 and 1, that is, because 0 and 1 are distinct elements of the domain for which f (0) = f (1), and f is not onto because, as noted above, the elements -4 and -2 in the codomain of f are not elements in the range of f. +
Example 2.48 Let X be the set of real numbers. We will show that the function f: X -> X defined by f (x) = 2x -3 is both one-to-one and onto and, hence, is a one-toone correspondence. In order to show that f is one-to-one, we must show that if f (x I) = f (X 2 ), then xi = x2. Let f (xl)I (x)) Then 2x,-3 = 2x2-
3
2x, = 2X2 Xi = X2.
Hence f is one-to-one. In order to show that f is, onto, we must show that if y is an element of the codomain of f, then there is an element x of the domain such that y = f (x). Since the domain and codomain of f are both the set of real numbers, we need to show that for any real number y, there is a real number x such that y = f (x).
2.5 Functions
Take x =
2(y
+ 3). (This value was found by solving y = 2x f(x)=f
-
77
3 for x.) Then
-(y+3) G3
=2[-2(y+3)] -3 = (y + 3)-
3
= y.
Thus f is onto and so is a one-to-one correspondence.
+
Note that whether or not a function is onto depends on the choice of domain and codomain. If, for instance, the codomain of the function f in Example 2.40 were changed from {-4, -2, 0, 2} to {0, 2), then f would be onto. Furthermore, if the set X in Example 2.48 were changed from the set of real numbers to the set of integers, then the function f in Example 2.48 would not be onto because there would be no element x E X such that f (x) = 0. Likewise whether or not a function is one-to-one depends on the choice of domain and codomain. Example 2.49 The function G: Z -* Z in Example 2.43 is one-to-one. For if G(xi) = G(X2), then 2x, = 2x2; so xI = x2. But G is not onto because there is no element x in the domain Z for which G(x) = 5. In fact, it is easy to see that the range of G is the set of all even integers, and so the range and codomain of G are not equal. + Example 2.50 The function h: Z one-to-one since 1
12 in Example 2.44 is easily seen to be onto. But h is not 13 but h(l) = [1] = [13] = h(13) in Z 12 . +
Z
#
Example 2.51 Let X denote the set of real numbers and Y = {x E X: -1 < x < 1}. Define a function f: X -+ Y by x XI We will show that f is a one-to-one correspondence.
78
Chapter 2
Sets, Relations, and Functions
As in Example 2.42, we will show first that if x E X, then f(x) E Y. For each x e X we have -lxi < x < 1xi. Now-1 - lxl <-Ixi and Ixi < 1 + IxI, so
-1
lxi < x < 1 + xi.
-
Dividing by 1 + ixI gives -1 <
x < 1, 1I+lxi
so that f(x) E Y.
Next we will prove that f is one-to-one. If xI, x2 E X and f(xI)
=
X2),
then X--lxi - IX2 1i2i XI~ll So +1+xil lXI I
1+ix21 X21
+ lXII
1 + Ix21
i1I(1 + 1X21) = Ix21(1 + lXIi) XIIi+ lXII IX21 = IX21 + 1X21 lXII lXII = IX21
I+±lxII = I + lX21. Multiplying the original equation xi +lxI
x 1+lX2l
by 1 + Ix I = 1 + Ix2I gives x1 = x2. Hence f is one-to-one. To show that f is onto, wz solve y = f (x) for x in terms of y as in Example 2.48. Then by taking Y YE X if O < y < 1, we have = y. And taking +YE X if-1 <=' o,=' we=' have=' f(=' y)='y.' thus=' f=' is=' onto.=' 4='>
-(IYY)
Since functions are sets of ordered pairs, the definition of equality for functions follows from the definition of equality for sets. That is, f: X -* Y and g: V -* W are equal if X = V, Y = W, and {(x, fix)):x E X} =
{(v, g(v)): v E V}.
2.5 Functions
79
It follows that f = g if and only if X = V, Y = W, and f(x) = g(x) for all x in X. It is possible for functions that appear different to be equal, as the following example shows. M
Example 2.52 Let X = g: X
-+
Y
{-1,0,1,2} and Y = {-4,-2,0,2). The functions f: X -- Y and defined by f (X) =
-x
21and
g(x) = 2
2
-
1x-
are equal since they have the same domains and codomains and f(x) = g(x) for eachx E X: f(-l)=2=g(-l)
f(0)=O=g(O),
f(l) = 0 = g(l),
f(2)= 2 =g(2). +
and
If f is a function from X to Y and g is a function from Y to Z, then it is possible to combine them to obtain a function gf from X to Z. The function gf is called the composition of g and f and is defined by taking the image of x under gf to be g(f (x)). Thus gf (x) = g(f (x)) for all x E X. The composition of g and f is therefore obtained by first applying f to x to obtain f (x), an element of Y, and then applying g to f (x) to obtain g(f (x)), an element of Z. (See Figure 2.16.) Note that in evaluating gf(x), we first apply f and then apply g. If X = Z, it is also possible to define the function fg; here we first apply g and then apply f. In general, however, the functions gf and fg are not equal. x
Y
z
FIGURE 2.16
+
Example 2.53 Let X denote the set of subsets of {1, 2, 3, 4, 51, Y the set of nonnegative integers less than 20, and Z the set of nonnegative integers. If S is an element of X, define f(S) to be the number of elements in set S, and if y E Y define g(y) = 2y. Then for S = {1, 3, 4} we have gf(S) = g(f(S)) = g( 3 ) = 6. In general, gf(S) = g(f(S)) = 2. f(S); thus, gf assigns to S the integer that is twice the
80
Chapter2
Sets, Relations, and Functions
number of elements in S. It follows that gf is a function with domain X and codomain Z. Note that in this case the function fg is not defined because g(y) does not lie in X, the domain off. +
'
Example 2.54 Let each of X, Y, and Z be the set of real numbers. Define f: X -+ Y and g: Y -+ Z by f(x) = xI for all x e X and g(y) = 3y + 2 for ally e Y. Then gf: X -+ Z is the function such that gf (x) = g(f (x)) = g(jxj) = 3Ixl + 2.
In this case we can also define :he function fg, but fg(x)
So gf
#
=- f ('(x)) =
fg because gf(- 1) 5
f (3x + 2) =
# 1=
13x + 21. +
fg(-1).
Suppose that f: X -- Y is a one-to-one correspondence. Then for each y e Y there is exactly one x E X such that y = f (x). Hence we may define a function with domain Y and codomain X by associating to each y e Y the unique x e X such that y = f (x). This function is denoted by f -1 and is called the inverse of function f . (See Figure 2.17.) The next theorem lists some properties that follow immediately from the definition of an inverse function.
xf
Y
FIGURE 2.17
Theorem 2.9
Let (a) (b) (c)
f: X
Y be a one-to-one correspondence. Then
f -l: Y -. X is a one-to-one correspondence. The inverse function of f -1 is f . For all x e X, f - 1J(x) = x; and for all y e Y, f -f
+
-*
= Ix andff -1=
ff -l(y) = y. That is,
fy.
Example 2.55 Theorem 2.9(c) can be used to compute the inverse of a given function. Suppose, for instance, that S is the set of real numbers, X={xES:
-I
<x<31,
Y= {yeS:6<><>
2.5 Functions
and f: X
81
-*
Y is defined by f (x) = 2x + 8. It can be shown that f is a one-to-one correspondence and, hence, has an inverse. If y = f (x), then, by Theorem 2.9(c), f -(y) = f 1 f(x) = x. Thus, if we solve the equation y = f (x) for x, we will obtain f 1(y). This calculation can be done as follows. y = 2x + 8 y - 8 = 2x 2(Y - 8) = x 1 Hence,f -(y) =(y
1 -8), and sof-(x)
=(x
-8).
+
We will conclude this section by discussing an important inverse function that frequently arises in discussions about the complexity of algorithms. Recall that for any positive integer n, 2' denotes the product of n factors of 2. Also 20 = 1,
and
2'
It is possible to extend the definition of an exponent to include any real number in such a way that all of the familiar exponent properties hold. When this is done, the equation f (x) = 2x defines a function with the set of real numbers as its domain and the set of positive real numbers as its codomain. We call f the exponential function with base 2. The behavior of this function is shown in Figure 2.18. f(x)
10
r
f(x)=2
5
x FIGURE 2.18
It can be seen in Figure 2.18 that the exponential function with base 2 is a one-to-one correspondence because each element of the codomain is associated with exactly one element of the domain. Hence this function has an inverse g called the logarithmic function with base 2. We denote this inverse function by g(x) =log2 x. Note that the definition of an inverse function implies that y =log 2 x if and only if x = 2Y. Thus log2 x is the exponent y such that x = 2Y. In particular, log 2 2' = n. So 1og2 4 = 1og 22 2 = 2, 1og 2 8 = 1og 2 23 = 3, log2 16 = 1og 2 24 = 4, lg 2 2 = log 2 2-1 = -1, and so forth. Although log 2 x increases as x increases, the rate of
82
Chapter 2
Sets, Relations, and Functions
growth of log 2 x is quite slow For example, log 2 l((C' -< log2 1024
=
log 2 210 = 10,
and similarly, log 2 2,000,000 < 20. The behavior of the function g(x) = log2 x is shown in Figure 2.19. g(x)
5
I (X
-
1012
5
X
10
-5
FIGURE 2.19
Scientific calculators usually contain a key marked LOG. This key can be used to find values of the logarithmic function with base 2, for log x
g2 +
log 2g
Example 2.56 A swarm of killer bees escaped several years ago from South America. Suppose that the bees originally occupied a region with area of one square mile and the region occupied by the bees doubles in area each year. How long will it take for the bees to cover the entire surface of the earth, which is 197 million square miles? Since the area of the region occupied by the bees doubles every year, after n years the bees will cover 2' square miles. We must determine x such that 2x = 197,000,000. But then x = 10g2
19>7,1)(0000 = log 197,000,000 log 2
2
Hence, the bees will cover the entire surface of the earth in about 27.55 years. +
EXERCISES 2.5 In Exercises 1-4 determine which of the given relations 11 arefunctions with domain X. 1. X ={1,3,5,7,8)andR =(1,7),(3,5),
(5,3),(7, 7,(8,5)l
2. X = (0, 1,2,31 and R = 1(0,0), (1, 1), (1, -1), (2, 2), (3, -3) 3. X = {-2, -1,0,1
andR = ((-2,6),(0,3),(1, -- 1))
4. X ={l,3,5)andR = [(I,9),(3,9),(5,9)1
2.5 Functions
83
In Exercises 5-12 determine if the given g is afunction with domain X and some codomain Y. 5. X is the set of residents of Iowa and, for x
E
X, g(x) is the mother of x.
6. X is the set of computers currently in use on the Illinois State University campus and, for x operating system that x is running. 7. X is the set of students at Illinois State University and, for x 8. X is the set of Presidents of the United States and, for x office of President. 9. X is the set of real numbers and, for x
E
10. X is the set of real numbers and, for x
E
X, g(x) X, g(x)
11. X is the set of real numbers and, for x
E
X, g(x) = x2x.
12. X is the set of real numbers and, for x
E
X, g(x) = x
E
E
e
X, g(x) is the
X, g(x) is the oldest brother of x.
X, g(x) is the year that x was first sworn into the
= 10g 2 x. = X
2
+ 3.
In Exercises 13-20 find the value of f (a). 13. f(x)=5x-7,a=3
14. f(x)= 4, a=8
15. f (x) = 2x, a =-2
16. f(x) = 3lx-
17. f(x)=
x-5,a=9
18. f (x) =
20. f(x) = 2x
2
19. f (x) =-x
2
2, a = -5
, a = -3
4,
a = 2
-x -3,a = -2
Evaluate the numbers in Exercises 21-28 using the fact that 1og 2 2n = n. 21. log2 8
22. log 2
25. log2
26. lg
16
2
2 4
23. log2 1
24. log2 64
27. log2
28. log 2 1024
3
Approximate the numbers in Exercises 29-36 using a calculator.
29. ]og2 37
30. 1lg2 1.72
31. log2 0.86
32. log 2 100
33. log2 1.54
34. log2 9.31
35. log2 1000
36. log2 0.17
Determine the functions gf and fg in Exercises 37-44. 37. f (x) = 4x + 7 and g(x) = 2x-3
38. f(x) = x2 + I and g(x)= j
39. f (x) = 2' and g(x) = 5x + 7
40. f (x) = 3x and g(x) = x1
41. f (x) = Ix I and g(x) = x 10g 2 x
42. f (x) = 2x and g(x) = 5x -x
43. f (x) = x 2 -2x and g(x) = x + 1
44. f (x) =
2-x
2
and g(x) = x-1
In Exercises 45-52, Z denotes the set of integers. Determine if each function g is one-to-one or onto. 45. g:
Z Z is defined by g(x) = 3x
46. g:
Z Z is defined by g(x) = x-2
47. g:
Z Z is defined by g(x) = 3 -x
48. g:
Z Z is defined by g(x) = x 2
49. g: Z
-(X+ 1)
Z is defined by g(x) =
f
-2x 50. g:Z 52. g:
{
is eve
if x is even
Z is defined by g(x) = 3x-5 Z Z is defined by g(x) =
ifxis odd 51. g: Z
I
if xx < 0 0 if
Z is defined by g(x) =Ix
84
Chapter 2 Sets, Relations, and Functions
In Exercises 53-60, X denotes the set of real numbers. Compute the inverse of eachfunction f: X 53. f(x) = 5x
54. f(x) = 3x - 2
57. f(x) =,~/
56. f(x) = x 2
55. f(x) = -x
58. f(x) = -'
59. f(x) = 3 2X+
61. Find a subset Y of the set of real numbers X such that g: X correspondence. Then compute g- '.
-*
1
X if it exists.
-*
+1
60. f(x) = x 3 -
Y defined by g(x) = 3 2x+1 is a one-to-one
62. Find a subset Y of the set of real numbers X such that g: Y -* correspondence. Then compute g l.
Y defined by g(x)
= -xl
is a one-to-one
63. If X has m elements and Y has n elements, how many functions are there with domain X and codomain Y? 64. If X has m elements and Y has n elements, how many one-to-one functions are there with domain X and
codomain Y?
f: X
Y and g: Y
66. Prove that if f: X
Y and g: Y
65. Prove that if function.
67. Let f: X -- Y and g: Y an example to show that
Z are both one-tc -one functions, then gf: X Z are both onto, then gf: X
Z be functions such that g: X
-*
-
-+
-*
Z is also a one-to-one
Z is also onto.
Z is onto. Prove that g must be onto, and give
f need not be onto.
68. Let f: X -- Y and g: Y -> Z be functions such that gf:,V -* Z is one-to-one. Prove that f must be one-to-one, and give an example to show that g need not be one-to-one. 69. Let f: X -* Y and g: Y and that (gf )-' = f -'g 70. Let
f: W
-.
X, g: X
2.6
o
-+
Z be one-to-one correspo ndences. Prove that gf is a one-to-one correspondence,
Y, and H: Y

Z be functions. Prove that h(gf) = (hg)f.
MATHEMATICAL INDUCTION In Section 1.4 we claimed tha -or any positive integer n 5 + 8 + I +. I
+ (3n + 2) = (3n2 + 7n).
Since there are infinitely man' positive integers, we cannot justify this assertion by verifying that this equation holds for each individual value of n. Fortunately, there is a formal scheme for proving statements are true for all positive integers; this scheme is called the principle of mathematical induction. 1he Pt'g A'
,ivhs
W';z i '
1icXl'
i;n
aIn
Let S(n) be a statement involving
the integer n. Suppose that for some fixed integer no (1) (2)
S(no) is true (that is, the statement is true if n = no) and whenever k is an integer sach that k > no and S(k) is true, then S(k + 1) is true.
Then S(n) is true for all integer, n
>
no.
The induction principle is a basic property of the integers, and so we will give no proof of it. The principle seems quite reasonable, however, for if condition
2.6 MathematicalInduction
85
(1) in the principle holds, then we know that statement S(no) is true. If condition (2) in the principle also holds, then we can use condition (2) with k = no to conclude that S(no + 1) is true. Using condition (2) with k = no + 1 now shows that S(no + 2) is true. If we then apply (2) with k = no + 2, we see that S(no + 3) is true. A continuation of this argument makes it plausible that S(n) is true for each integer n > no. A proof by mathematical induction consists of two parts. Part (1) establishes a base for the induction by proving that some statement S(no) is true. Part (2), called the inductive step, proves that if any statement S(k) is true, then so is the next statement S(k + 1). In this section we will give several examples of the use of mathematical induction. In these examples no, the base for the induction, will usually be either 0 or 1. The following example proves the result from Section 1.4 that was mentioned earlier. Example 2.57 We will prove that 5 + 8 ± I1 + .. + (3n + 2) = '(3n 2 + 7n) for any positive integer n. The proof will be by induction on n, with S(n) being the statement: 5 + 8 + 11 + + (3n + 2) = j(3n2 + 7n). Since S(n) is to be proved for all positive integers n, we will take the base of the induction to be no = 1. (1)
For n = 1, the left side of S(n) is 5 and the right side is
1-[3(1)2 121 1 + 7(1)] = -(3 + 7) = (10)
2
(2)
2
5.
2
Hence S(1) is true. To perform the inductive step, we assume that S(k) is true for some positive integer k and show that S(k + 1) is also true. Now S(k) is the equation 5 + 8 + 11 +
+ (3k + 2) = I(3k2 + 7k). 2
To prove that S(k + 1) is true, we must show that 5 + 8 + 11 +
11)
+ (3k +2) + [3(k + 1) + 2] = -[3(k + 1)2+ 7(k + 1)]. 2
But by using S(k), we can evaluate the left side of the equation to be proved as follows. [5+8+11+ .
+(3k+2)]+[3(k+1)+2] = (3k2+7k)+[3(k+1)+2] = (-k2+
k) +(3k+3+2)
= 3 k2+
2k
2
2
l(3k 2+13k +10) 2
86
Chapter 2
Sets, Relations, and Functions
On the other hand, the right side of the equation to be proved is 1_[3(k + 1)2 + 7(! ir 1)]
I[3(k2+ 2k + 1) + 7(k + 1)]
23k1)±(+-)
2 = !(3k2+6k+3+7k+7)
2 = !(3k2 + 13k + 10). 2
Because the left and right sides; are equal in the equation to be proved, S(k + 1) is true. Since both (1) and (2) are true, the principle of mathematical induction guarantees that S(n) is true for all integers n > 1, that is, for all positive integers n. + +
Example 2.58 Mathematical induction is often used to verify algorithms. To illustrate this, we will verify the polynomial evaluation algorithm stated in Section 1.4. Recall that this algorithm evaluates a polynomial P(x) = aImx,' + am-Ixm
+
+ ajx + ao
by the following steps. Step I Step 2 Step 3
Let S = ao and k = 1. While k < m, replace S by S + akxk and k by k + 1. P(x) = S.
Let S(n) be the statement: If the replacements in step 2 are executed exactly n times each, then S = anxn 4 an-xn-I + + aIx + ao. We will prove that S(n) is true for all nonnegative integers n. (1)
(2)
If n = 0, then the replacements in step 2 are not performed, so the value of S is the value ao given in step 1. But the equality S = ao is the statement S(0); so S(0) is true. To perform the inductive step, we assume that S(k) is true for some positive integer k and show that ',(k + 1) is also true. For S(k) to be true means + aIx + ao when the replacements in step that S = akxk + ak- lxk-1 + 2 are executed exactly k times. If the replacements in step 2 are executed one more time (k + 1 times in all), then the value of S is .
S + ak+IXk+
= (a. k
+
ak-lX
+ * * + aix + ao) + ak+IX +
= akF]X k+1 + akx + ak
1
++ax+ +'
Thus S(k + 1) is true, completing the inductive step.
ao.
2.6 MathematicalInduction
87
Since both (1) and (2) are true, the principle of mathematical induction guarantees that S(n) is true for all nonnegative integers n. In particular, S(m) is true. But S(m) is the statement that P(x) = S. The proof above shows that, after the replacements in step 2 are executed exactly k times each, the value of S is S = akxk + ak-Ixk- + + aix + ao. Since this relationship holds for any number of repetitions of the while loop in step 2, it is called a loop invariant. + In our subsequent proofs by induction, we will follow the usual practice of not stating explicitly what the statement S(n) is. Nevertheless, in every induction proof the reader should formulate this statement carefully.
Example 2.59 For any nonnegative integer n and any real number x 1+
X +
X2 +
..
+Xan
=X
#
1,
-1
x-1
The proof will be by induction on n with 0 as the base of the induction. For n = 0 the right side of the equation is Xn+-
1
x
-
1
x-l x-1 and so the equation is true for n 0. Assume that the equation is true for some nonnegative integer k, that is, 1+X +X
2
Xk+
-1
+ . . +Xk x-1
Then 1+ X + X +
+xk
+X k+=(1
+X Xk+
2
+x -
+ .+Xk)
1
+Xk+1
k+
x-1 Xk+l -1
+ Xk+(X -1)
x-1 Xk+J -
1+
Xk+2 -
Xk+1
x-1 Xk+2 -
1
x-1
proving that the equation is true for k + 1. Thus the equation is true for all nonnegative integers n by the principle of mathematical induction. +
88
Chapter 2
Sets, Relations, and Functions
In Examples 2.57 and 2.59, we used mathematical induction to prove certain formulas are true. The principle of mathematical induction is not limited, however, to proving equations or inequalities. In the following example, induction is used to establish a geometric result.
i]
Example 2.60 We will prove that, for any positive integer n, if any one square is removed from a 2 n x 2 n checkerboard (one having 2n squares in each row and column), then the remaining squares can be covered with L-shaped pieces (shown in Figure 2.20) that cover three squares.
FIGURE 2.20
Figure 2.21 shows that every 21 x 21 checkerboard with one square removed can be covered by a single L-shaped piece. Hence the result is true for n = 1. Now assume that the result is true for some positive integer k, that is, every 2k x 2 k checkerboard with one square removed can be covered by L-shaped pieces. We must shcw that any 2 k+1 x 2 k+1 checkerboard with one square removed can be covered by L-shaped pieces. If we divide the 2 k+1 x 2 k+1 checkerboard in half both horizontally and vertically, we obtain four 2 k x 2k checkerboards. One of these 2A x 2 k checkerboards has a square removed, and the other three are complete (See Figure 2.22.) From each of the complete 2k x 2 k checkerboards, remove the square that touches the center of the original 2 k+1 x 2 k+1 checkerboard. (See Figure 2.23.) By the induction hypothesis, we know that all four of the 24 x 2 k checkerboards with one square removed in Figure 2.23 can be covered with L-shaped pieces. So with one more L-shaped piece to cover the three squares touching the center of the 2 k+1 x 2 k+1 checkerboard, we can cover with L- shaped pieces the original 2 k+1 x 2 k+1 checkerboard with one square removed. This proves the result for k + 1. It now follows from the principle of mathematical induction that for every positive integer n, any 2' x 2' checkerboard with one square removed can be covered by L-shaped pieces. +
2.6
MathematicalInduction
89
FIGURE 2.21
I I
I
I FIGURE 2.22
FIGURE 2.23
Closely related to the induction principle are what are known as recursive definitions. To define an expression recursively for integers n > no, we must give
its value for no and a method of computing its value for k + 1 whenever we know its value for no, no + 1, . . ., k. An example is the quantity n! which was defined in Section 1.2. A recursive definition of n! is the following: 0! = 1,
and
if n > O, then n! = n(n-1)!.
By repeatedly using this definition, we can compute n! for any nonnegative integer n. For example, 4! = (4)3! = (4)(3)2! = (4)(3)(2)1! = (4)(3)(2)(1)O! = (4)(3)(2)(1)1 = 24. Example 2.61 We will prove that n! > induction with no = 4. (1) (2)
2n
if n > 4 by applying the principle of mathematical
If n = 4, then n! = 24 and 2' = 16; so the statement holds. Suppose k! > 2k for some integer k > 4. Then (k + 1)! = (k + I)k! > (4 + I)k! > 2k! > 2(2k) =
2 k+1.
This is the required inequality for k + 1. Thus, by the induction principle, the statement holds for all n > 4.
o
90
Chapter2
Sets, Relations, and Functions
Another example of a rec arsive definition is that of the Fibonacci numbers F1,F2 , .. , which are defined by F, = 1,
F2 = 1, and
if n > 2, then F, = Fn - +Fn-2
Forexample, F 3 = F2 + F1 =1 + I = 2, F4 = F3 + F 2 = 2 + I = 3, and F5s= F4 + F3 = 3 + 2 = 5. Note that since Fn depends on the two previous Fibonacci numbers, it is necessary to define both F1 and F2 at the start in order to have a meaningful definition. In some circumstances, a, Lightly different form of the principle of mathematical induction is needed. he ,S~uu7 MI,/ :i, v' 101,she~aF'l/r [email protected]'.2m Let S(n) be a statement involving the integer n. Suppose that for some fixed integer no
(1)
S(no) is true, and
(2) whenever k is an integer such that k
>
no and S(no), S(no + 1), . . ., S(k)
are all true, then S(k J- 1) is true. Then S(n) is true for all integers n > no. The only difference between the strong principle of induction and the previous version is in (2), where now we are allowed to assume not only that S(k), but also S(no), S(no + 1), .. , S(k - 1), are true. Thus, from the point of view of logic, the strong principle should be easier to apply, since more can be assumed. It is more complicated than the previous form, however, and usually is not needed. In this book we will primarily use the strong principle to prove results about certain types of recurrence relations. To illustrate its use, we will prove a fact about the Fibonacci numbers. cl
Example 2.62 We will prove that F,
< 2' for every positive integer n. Since
Ft = I <2 = 2'
and
F 2 = l <4=22 ,
the statement is true for n = 1 and n = 2. (We must verify the statement for both n = 1 and n = 2 because we need to assume that k > 2 in the inductive step in order to use the recursive de ignition of the Fibonacci numbers.) Now suppose that for some positive integer k > 2 the statement holds for n = 1, n = 2, n = k. Tlen Fk+I=Fk +
k- < 2k + 2k<2k
+2k
=2 2k2k
So the statement is true for n =k + if it holds for n = 1, n = 2, .. , n = k. Thus, by the strong principle of mathematical induction, the statement is true for all positive integers n. +
2.6 MathematicalInduction
91
EXERCISES 2.6 1. Compute the Fibonacci numbers F] through Flo. 2. Suppose that a number x, is defined recursively by xi = 7 and x, = 2xn-1 through X6. 3. Suppose that a number x, is defined recursively by xi = 3, x2 = 4, and X, xi through xg. 4. Give a recursive definition of xn for any positive integer n.
=
-
5 for n > 2. Compute xl
Xn-I + xn-2 for n > 3. Compute
5. Give a recursive definition of the nth even positive integer. 6. Give a recursive definition of the nth odd positive integer. In Exercises 7-10 determine what is wrong with the given induction arguments. 7. We will prove that 5 divides 5n + 3 for all positive integers n. Assume that for some positive integer k, 5 divides 5k + 3. Then there is a positive integer p such that 5k + 3 = 5p. Now 5(k + 1) + 3 = (5k + 5) + 3 = (5k + 3) + 5 =Sp + 5 = 5(p + 1). Since 5 divides 5(p + 1), it follows that 5 divides 5(k + 1) + 3, which is the statement that we want to prove. Hence, by the principle of mathematical induction, 5 divides 5n + 3 for all positive integers n. 8. We will prove that in any set of n persons, all people have the same age. Clearly all people in a set of 1 person have the same age, so the statement is true if n = 1. Now suppose that, in any set of k people, all persons have the same age. Let S = {xI, x2, Xk+J} be a set of k + 1 people. Then by the induction hypothesis all people in each of the sets {xI, x 2 ,. Xk) and {x2 , X3, . . - Xk+4 I have the same age. But thenxjx2, X2. .k X all have the same age and likewisex 2 , x3, .. , Xk+l all have the same age. It follows that xl, x2, . . ., Xk+I all have the same age. This completes the inductive step. The principle of mathematical induction therefore shows that for any positive integer n, all people in any set of n persons have the same age. 9. We will prove that for any positive integer n, if the maximum of two positive integers is n, then the integers are equal. If the maximum of any two positive integers is 1, then both of the integers must be 1. Hence the two integers are equal. This proves the result for n = 1. Assume that if the maximum of any two positive integers is k, then the integers are equal. Let x and y be two positive integers for which the maximum is k + 1. Then the maximum of x -1 and y - 1 is k. So by the induction hypothesis, x - 1 = y - 1. But then x = y, proving the result for n = k + 1. It follows by the principle of mathematical induction that for any positive integer n, if the maximum of two positive integers is n, then the integers are equal. Hence any two positive integers are equal. 10. Let a be a nonzero real number. We will prove that for any nonnegative integer n, an Since a0 = 1 by definition, the statement is true for n = 0. Assume that for some integer k, am = 1for 0 < m < k. Then k+
akak akI=
= 1.
1 1
The strong principle of induction therefore implies that a' = 1 for every nonnegative integer n.
92
Chapter 2
Sets, Relations, and Functions
In Exercises 11-26 prove each of the given statements by f, athematicalinduction. 11. 1 + 2
+
.
2
+n3 =
+
14. -++ 12 2 3 15. 1(1!) + 2(2!) + 16.
+ (
1)(2n + 1) for every pc sitive integer n.
6
4
)
for every positive integer n.
for every positive integer n.
=
n(n+1) n+ + n(n!) = (n + 1)!
(1-2 ) (I
17. 1
for every positive integer e.
2
+ n2 = n(n
12. 1 + 4 + 9 + 13. 1 + 8 + 27
(
+n =
1)
-
3. . . . (2n-
1)
>2
2
4.
18. n < 2' for every integer n
..
>
(2n
-
=
I for every positive integer n. L1 forevn y positive integern.
-2) for every int.ger n > 2.
5.
19. n! > 3n for every integer n > 7. 20. (2n)! < (n!) 2 4n 1 for every integern > 5. 21. F, < 2F,- for every integer n > 2. 22. F1 + F2 +
+ Fn= Fn+2 -I
for every positive integer n.
* + F2 4 = F2 n+I - 1 for every positive integer n.
23. F2 + F 4 + 24.
Fn
<
(< )
for
every positive integer
25.
F, >
(4-)
for
every integer
n.
n > 3.
26. For any integer n > 2, a 6 x n checkerboard can be covered by L-shaped pieces of the form in Figure 2.20. 27. A sequence SO, Si, S2, .. is called a geometric progression with common ratio r if there is a constant r such that sn = sorn for all nonnegative integers n. If so, s1, s, .. is a geometric progression with common ratio r, find a formula for s ±sSs + . -+A Sn as a function of r, and n. Then verify your formula by mathematical induction. (Hint: Use the equation in Example 2.59.)
sC,
28. A sequence, so, SI, S2, . . is called an arithmetic progression with common difference d if there is a constantd such that s, = so + nd for all nonnegative integers n. IF O, 0 SI, 2. . .is an arithmetic progression with common difference d, find a formula for so + sI + + s, as a function of so, d, and n. Then verify your formula by mathematical induction. 29. Prove that 2'
+
30. Prove that 16'
2.7
3f =
+
-
5' (mod 6) for every positive integer n.
1 - IOn (mod 25) for every positive integer n.
APPLICATIONS In this section we will apply the two versions of the principle of mathematical induction stated in Section 2.6 1o establish some facts that are needed elsewhere in this book. Our first two results give the maximum number of comparisons that
2.7 Applications
93
are needed to search and sort lists of numbers; these facts will be used in our discussion of searching and sorting in Chapter 8.
Example 2.63 There is a common children's game in which one child thinks of an integer and another tries to discover what it is. After each guess, the person trying to determine the unknown integer is told if the last guess was too high or too low. Suppose, for instance, that we must identify an unknown integer between 1 and 64. One way to find the integer would be to guess the integers from 1 through 64 in order, but this method may require as many as 64 guesses to determine the unknown number. A much better way is to guess an integer close to the middle of the possible values, thereby dividing the number of possibilities in half with each guess. For example, the following sequence of guesses will discover that the unknown integer is 37. Attempt
Result
Conclusion
1
Guess 32
Low
Integer is between 33 and 64.
2
48
High
Integer is between 33 and 47.
3
40
High
Integer is between 33 and 39.
4
36
Low
Integer is between 37 and 39
5
38
High
Integer is between 37 and 37.
6
37
Correct
010
If the strategy described here is used, it is not difficult to see that any unknown integer between 1 and 64 can be found with no more than 7 guesses. This simple game is related to the problem of searching a list of numbers by computer to see if a particular target value is in the list. Of course, this situation differs from the number-guessing game in that we do not know in advance what numbers are in the list being searched. But when the list of numbers is sorted in nondecreasing order, the most efficient searching technique is essentially the same as that used in the number-guessing game: repeatedly compare the target value to a number in the list that is close to the middle of the range of values in which the target must occur. The theorem below describes the efficiency of this searching strategy.
Theorem 2.10
For any nonnegative integer n, at most n + 1 comparisons are required to determine if a particular number is present in a list of 2n numbers that are sorted in nondecreasing order. Proof. The proof will be by induction on n. For n = 0, we need to show that at most n + 1 = 1 comparison is required to see if a particular number m is in a list containing 20 = 1 number. Since the list contains only one number, clearly only one comparison is needed to determine if this number is m. This establishes the result when n = 0.
94
Chapter 2 Sets, Relations, and Functions
Now assume that the result is true for some nonnegative integer k; that is, assume that at most k + 1 comparisons are needed to determine if a particular number is present in a sorted list of 2k numbers. Suppose that we have a list of 2 k+1 numbers in nondecreasing order. We must show that it is possible to determine if a particular number m occurs in this list using at most (k + 1) + 1 = k + 2 comparisons. To do so, we will compare m to the number p in position 2k of the list. Since the list is in nondecreasing order, for m to be present in the list it must lie in positions 1 through 2k. But the numbers in positions 1 through 2k are a list of 2k numbers in nondecreasing order. Hence, by the induction hypothesis, we can determine if m is present in this list by using at most k + 1 comparisons. So in this :ase, at most 1 + (k + 1) = k + 2 comparisons are needed to determine if m is present in the original list. C the lis i-s in nondecreasing order, form to be present in the list, it must lie in positions 2k + 1 through 2 k+I. Again, the induction hypothesis tells us that we can determine if m is present in this sorted list of 2k numbers with at most k + 1 comparisons. Hence, in this case also, at most k + 2 comparisons are needed to determine if m is present in the original list. Thus, in each of the cases, we can determine if m is present in the list of 2k+I sorted numbers with at most k + 2 comparisons. This completes the inductive step and, therefore, proves the theorem for all nonnegative integers n. a I
' 7Since :
Although Theorem 2.10 :s stated for lists of numbers in nondecreasing order, it is easy to see that the same conclusion is true for lists that are sorted in nonincreasing order. Moreover, the same conclusion is true for lists of words that are in alphabetical order. The next theorem is similar to Theorem 2.10; it gives an upper bound on the number of comparisons needed to merge two sorted lists of numbers into one sorted list. Before stating this result, we will illustrate the merging process to be used in proving Theorem 2.11. +
Example 2.64 Consider the two lists of numbers in nondecreasing order: 2.5, 7,9
and
3,4,7.
Suppose that we want to merge them into a single list 2,3,4,5,7,7,9 in nondecreasing order. To corn mine the lists efficiently, first compare the numbers at the beginning of each list (2 and 3) and take the smaller one (2) as the first number in the combined list. (Ii the first number in one list is the same as the first number in the other list, choose either of the equal numbers.) Then delete this smaller number from the list that contains it to obtain the lists 5,7, 9
and
3,4,7.
2.7 Applications
95
Second, compare the beginning numbers in each of these new lists (5 and 3) and take the smaller one (3) as the second number in the combined list. Delete this number from the list that contains it, and continue the process above until all of the original numbers have been merged into a single list. Figure 2.24 illustrates this process. + 2
2
/
3 4
3
5
5
4
7
7
7
9 FIGURE 2.24
Theorem 2.11
Let A and B be two lists containing Suppose that for some positive integer in the two lists. Then A and B can be nondecreasing order using at most n -
numbers sorted in nondecreasing order. n, there is a combined total of n numbers merged into a single list of n numbers in 1 comparisons.
Proof. The proof will be by induction on n. If n = 1, then either A or B must be an empty list (and the other must contain 1 number). But then the list C obtained by adjoining list B to the end of list A will be in nondecreasing order, and C is obtained by making 0 = n - I comparisons. This proves the theorem when n = 1. Now suppose that the conclusion of the theorem holds for some positive integer k, and let A and B be sorted lists containing a total of k + 1 numbers. We must show that A and B can be merged into a sorted list C using at most k comparisons. Compare a and b, the first elements of A and B, respectively. ( lfi~g
a
comparisons. ( i, '- a it b Delete b from B to form list B'. Then use the induction hypothesis as in case 1 to sort A and B' into a sorted list C' using at most k - 1 comparisons. The list C is then obtained by adjoining b to C' as the first element. As in case 1, C is in nondecreasing order and was formed using at most k comparisons.
96
Chapter 2
Sets, Relations, and Functions
Thus, in either case, we can merge A and B into a sorted list using at most k comparisons. This completes the proof of the inductive step, and so the conclusion is established for all positive integers n. a Our next two results involve the number of subsets of a set. These results arise in connection with the knapsack problem described in Section 1.3 and with counting techniques to be discussed in Chapter 7. Recall that we stated in Section 1.3 that the set 11,2, . . ., n) has precisely 2 n subsets. If this result is true in general, then increasing a by 1 doubles the number of subsets. An example will indicate why this is true. Let us take n = 2 and consider the subsets of {1, 21. They are 0,
{]},
{2},
and
{1, 2}.
Now consider the subsets of {1, 2, 3 }.Of course, the four sets we have just listed are also subsets of this larger set; but there are other subsets, namely those containing 3. In fact, any subset of 11, 2, 31 that is not a subset of 11, 21 must contain the element 3. If we removed the 3, we would have a subset of {1, 2} again. Thus the new subsets are just
(3),
11, 3},
12, 3},
and
(1, 2, 3},
formed by including 3 in eazh of the previous four sets. The total number of subsets has doubled, as our formula indicates. This argument is the basis for a proof of Theorem 1.3. Theorem 1.3
If n is any nonnegative integer, then a set with n elements has exactly 2 ' subsets. Proof. We will prove this result by induction on n. To establish a base for th- induction, we will show that any set having 0 elements has 20 = 1 subset. But a set having 0 elements must be the empty set, and so its only subset is 0. This establishes the result when n = 0. To perform the inductive step, we assume the result for some nonnegative integer k and prove it for k + 1. Thus we assume that any set with k elements has exactly 2k subsets. Let S be a set with k + 1 elements, say a, a2 ,. ak+l, and define a set R by =a,
a2 , . . ., ak).
Since R has k elements, ii has exactly 2k subsets by our assumption. But each subset of S is either a subset of R or else a set formed by inserting ak+1 into a subset of R. Thus S has exact y ,!k
+
2k =
2(2k) =
2
subsets, proving the result fork + 1. It therefore follows froni the principle of mathematical induction that the result is true for all nonnegative integers n. H
2.7 Applications
+
97
Example 2.65 For many years Wendy's Old Fashioned Hamburger Restaurants advertised that they serve hamburgers in 256 different ways. This claim can be justified by using Theorem 1.3, because hamburgers can be ordered at Wendy's with any combination of 8 toppings (cheese, ketchup, lettuce, mayonnaise, mustard, onions, pickles, and tomatoes). Since any selection of toppings can be regarded as a subset of the set of 8 toppings, the number of different toppings is the same as the number of subsets, which is 28 = 256. + We can say even more about the number of subsets of a set containing n elements. The following theorem tells us how many of its 2' subsets contain a specified number of elements.
Theorem 2.12
Let S be a set containing n elements, where n is a nonnegative integer. If r is an integer such that 0 < r < n, then the number of subsets of S containing exactly r elements is n! r! (n - r)! Proof. The proof will be by induction on n, starting with n = 0. If n = 0, then S is the empty set and r must also be 0. But there is exactly 1 subset of 0 with 0 elements, namely 0 itself. And n! r! (n -r)!
0! 0! 0!
because 0! = 1 by definition. Thus the formula is correct for n = 0. Now suppose that the formula is correct for some integer k > 0. Let S be a set containing k + 1 elements, say S = {a, a2 , .. , ak, ak+ )}.We must count the subsets of S containing exactly r elements, where 0 < r < k + 1. Clearly the only subset of S containing 0 elements is 0. Likewise there is only one subset of S containing k + 1 elements, namely S itself. In both these cases the formula gives the correct value since (k+)! =1 0! (k+ 1-0)!
and
(k+1)! (k+ 1)! [k+ 1 -(k+ 1)]!
Let R be any subset of S containing exactly r elements, where 1 < r < k. There are two cases to consider. Then R is a subset of {a1, a 2, the induction hypothesis there are k! r! (k -r)! such subsets.
. . ,
ak} having r elements. By
98
Chapter2
Sets, Relations, and Functions C vase: 2' {, ., R In this case, if we remove ak+l from R, we have a subset of {a, a2 , .. , ak} containing r -- 1 elements. By the induction hypothesis there are
k! (r -1)! [k -(r -1] sets like this. Putting the two cases together, we see that S has a total of k! r! (A-- )!
k! (r- 1)! (k-r + 1)!
subsets with r elements. But th is number equals k! (k - i + 1) r! (k -r)! (1 r + l) k! (i,.r
r(r-1)! (k-r
+ 1)
r! (k - r + I)! k (A r!
k! r +1)!
k! r
r! (k -r +I)
r + 1 +±r) r +)!
(k + 1)! r' (A I - r)! Since this is the number produced by the formula when k + 1 is substituted for n, the formula is correct for n -=k + 1. Thus by the principle of mathematical induction, the formula is correct for all nonnegative integers n. If Many counting problems require knowing the number of r-element subsets of a set with n elements. We will denote] this number by C(n, r). With this notation, Theorem 2.12 can be stated a, C'(n, r) = r! (n
+
r)!
Example 2.66 How many 2-person committees can be chosen from a set of 5 people? This is equivalent to asking how many subsets of {1, 2, 3, 4, 51 have exactly 2 elements. Taking n = 5 anc[ r = 2 in Theorem 2.12 gives the answer 5! C52) 2! (5 -2)!
' Another common notation is (, 1.
5!
~2! 3!=1.
2.7 Applications
99
The actual subsets are {1, 21, {1, 31, {1, 41, {1, 51, {2, 31, {2, 41, {2, 51, {3, 41, {3,51,and{4,5}.
+
The last result in this section proves a very basic result about the positive integers. This fact was referred to in Example 2.13.
Theorem 2.13
Every integer greater than 1 is either prime or a product of primes. Proof Let n be an integer greater than 1. The proof will be by induction on n using the strong form of the principle of mathematical induction. Since 2 is a prime number, the statement is true for n = 2. Assume that for some integer k > 1, the statement is true for n = 2, 3, . . ., k. We must prove that k + 1 is either prime or a product of primes. If k + 1 is prime, then there is nothing to prove; so suppose that k + 1 is not prime. Then there is a positive integer p other than l and k + 1 that divides k + 1. So k+1 = q is p an integer. Now q =A 1 (for otherwise p = k + 1) and q # k + 1 (for otherwise p = 1). Hence both p and q are integers between 2 and k, inclusive. So the induction hypothesis can be applied to both p and q. It follows that each of p and q is either prime or a product of primes. But then k + 1 = pq is a product of primes. This finishes the inductive step, and therefore completes the proof of the theorem. N With the development of larger and faster computers, it is possible to discover huge prime numbers. In 1978, for instance, Laura Nickel and Curt Noll, two teenagers from Hayward, California, used 440 hours of computer time to find the prime number 221701 - 1. At that time this 6533-digit number was the largest known prime number. But finding whether a particular positive integer is prime or a product of primes remains a very difficult problem. Note that although Theorem 2.13 tells us that positive integers greater than 1 are either prime or products of primes, it does not help determine which is the case. In particular, Theorem 2.13 is of no help in actually finding the prime factors of a specific positive integer. Indeed, the difficulty of finding the prime factors of large numbers is the basis for an important method of cryptography (encoding of data or messages) called the RSA method. (The name comes from the initials of its discoverers, R. L. Rivest, A. Shamir, and L. Adleman.) For more information on the RSA method, see suggested reading [8] at the end of this chapter.
EXERCISES 2.7 Evaluate the numbers in Exercises 1-12. 1. C(7, 2)
2. C(8, 3)
3. C(lO, 5)
4. C(12, 6)
5. C(II,4)
6. C(I0,7)
7. C(II,6)
8. C(13, 9)
Chapter2
100
Sets, Relations, andFunctions
9. C(n, 0)
10. C(n, 1)
11. C(n, 2)
12. P(n, r) C(n, r)
13. How many subsets of the set 11, 3,4, 6, 7,91 are there? 14. How many nonempty subsets of the set {a, e, i, o, u} are there? 15. At Avanti's, a pizza can be ordered with any combir ation of the following ingredients: green pepper, ham, hamburger, mushrooms, onion, pepperoni, and sausage How many different pizzas can be ordered? 16. If a test consists of 12 questions to be answered true or false, in how many ways can all 12 questions be answered? 17. A certain automobile can be ordered with any comb nation of the following options: air conditioning, automatic transmission, bucket seats, cruise control, power windo ws, rear window defogger, sun roof, and CD player. In how many ways can this car be ordered? 18. Jennifer's grandmother has told her she can take as many of her 7 differently colored glass rings as she wants. How many choices are there? 19. How many subsets of {1, 3, 4, 5, 6, 8, 9} contain exactlv 5 elements? 20. How many subsets of {a, e, i, o, u, yj contain exactly 4 elements? 21. A basketball coach must choose a 5-person starting team from a roster of 12 players. In how many ways is this possible? 22. A beginning rock group must choose 2 songs to record from among the 9 they know. How many choices are possible? 23. A person ordering a complete dinner at a restaurant may choose 3 vegetables from among 6 offered. In how many ways can this be done? 24. A hearts player must pass 3 cards from his 13-card hand. How many choices of cards to pass does he have? 25. Three persons will be elected from among 10 candidates running for city council. How many sets of winning candidates are possible? 26. A sociologist intends to select 4 persons from a lisi of c people for interviewing. How many sets of persons to interview can be chosen? 27. How many 13-card bridge hands can be dealt from a 52-card deck? Leave your answer in factorial notation. 28. A racketeer is allowed to bring no more than 3 of the 7 lawyers representing him to a Senate hearing. How many choices does he have? Prove each of the statements in Exercises 29-40 by mathematical induction. 29. For any distinct real numbers x and y and any nonnegative integer n Xfll
xnyO + xn-1Y1 +
+ XI
1+ X0 Yn
X
n±1
-y
X-y
30.
-+-+'+
12 + 22
n2
<2<
n_
for all integers n > 2.
31. (2n) is an integer for all positive integers n. 32. (n + 1)(n ...
(2n) is an integer for all positive integers n.
33. For all positive integers n, 3 divides 22n 3
-
1.
34. For all positive integers n, 6 divides n + 5n.
HistoricalNotes
35
101
(4n)! is an integer for all nonnegative integers n. Sn
36. (4
2)
is an integer for all integers n > 5.
8n
37. (1 +22 38. 12 -22 +
+ n)2
= 1
+ 2 +.. + n3 for all positive integers n.
2 + (-l)'+'nn = (_1)
) for all positive integers n.
n(n +
2 39. 40.
+
+
>/Vfor all integers n > 2.
3 45..(2n -l1) > - for all positive integers n. 2 -4 .6 -.. .(2n) - 2n
41. Let n be a positive integer and A, A2 , .. , An be subsets of a universal set U. Prove by mathematical induction that
(Al U A2 U 42. Let n be a positive integer and A 1 , A2 that (Al
,
.
UAn)
=
A;fl A
f2n ..- A'.
An be subsets of a universal set U. Prove by mathematical induction
n A2 r..
n An) = Al U A2 u.. u
An.
43. If n is an integer larger than three, determine the number of diagonals in a regular n-sided polygon. Then prove that your answer is correct using mathematical induction. 44. Suppose that, for some positive integer n, there are n lines in the Euclidean plane such that no two are parallel and no three meet at the same point. Determine the number of regions into which the plane is divided by these n lines, and prove that your answer is correct using mathematical induction. 45. Prove by mathematical induction that any list of 2' numbers can be sorted into nondecreasing order using at most n '2ncomparisons. 46. Prove by mathematical induction that a uniform cake can be divided by n persons so that each person believes that the volume of cake he or she receives is at least 1 of the total volume of the cake. Assume that each person is capable of dividing an object into parts that he or she considers to be of equal volume. 47. Mr. and Mrs. Lewis hosted a party for n married couples. As the guests arrived, some people shook hands. Later Mr. Lewis asked everyone else (including his wife) how many hands each had shaken. To his surprise, he found that no two people gave him the same answer. If no one shook his or her own hand, no spouses shook hands, and no two persons shook hands more than once, how many hands did Mrs. Lewis shake? Prove your answer by mathematical induction. 48. The well-ordering principle states that every nonempty set of positive integers contains a smallest element. (a) Assume the well-ordering principle holds and use it to prove the principle of mathematical induction. (b) Assume the principle of mathematical induction holds and use it to prove the well-ordering principle.
HISTORICAL NOTES The theory of sets, congruences, relations, functions,and mathematical induction share many common roots. Set theory, as a method of discussing classes of objects and their properties, got its start in the work of English mathematicians in the early to mid-1800s.
102
Chapter 2
George Boole
Sets, Relations, and Functions
George Boole's (1815-1864) publication of Investigation of Laws of Thoughts (1854) provided a basis for an algebra cf sets and related logical forms. Boole recognized that, through the limiting of ordinary aIgebraic thought to the values of 0 and 1, one could develop a model for mathematical reasoning. Boole's work amplified and clarified ideas developed earlier by George Peacock (1791-1858), Augustus De Morgan (1806-1871), and the Scottish philosopher Sir William Hamilton (1788-1856). This triohadbeenworking to generalize the connections between arithmetic and algebra by reducing mathematical thought and argumentation to a series of symbolic forms involving generalized numbers and operations. In 1847, Boole published a work entitled The MathematicalAnalysisof Logic. In it, he separated mathematical logic from the lozics emploved by the IJ reeks and scholastics. Boole's work elevated logic from its use in arguing particular cases to claim a role as a subdiscipline of the mathematical sciences in its own right. In the development of the algebra of logic, Boole's work laid out an algebra of sets where the union and intersection of sets were denoted by the signs + and x, respectively. The empty set was denoted by 0 Our contemporary symbols U, n, and 0 came later. The first two were developed frcm symbols used by the German algebraist Hermann Grassmann (1809-1877) in his 44 work Ausdehnungslehre. These symbols were later popularized by the Italian Giuseppe Peano (1858-1932) in his 1894 work Formulairede Mathematiques. In it, he added our present usage of E for set membership and c for set containment. The origin of the s ymbol 0 to denote the empty set is less clear, although it has been attributed to the Norwegian Niels Henrik Abel (1802-1829). Bertrand Russell (1872-1970) and Alfred North Whitehead (1861-1947) brought several other signs to common usage in their classic 1910-1913 multivolume PrincipiaMathematica. Among them were braces to denote sets, and bars above the symbols denoting sets to denote their complements [73, 74, 75, 80]. Venn diagrams are the work of the English logician John Venn (1834-1923). In his 1881 book Symbolic Logic, he used these diagrams to explain the ideas stated by Boole more than a quarter of a century earlier. Leonhard Euler (1707-1783) had earlier used a similar circle arrangement to make arguments about the relationships between logical classes. The use of such diagram to represent sets, set operations, and set relationships provided a readily understandable way of reasoning about the properties of sets. The development of the concept of an equivalence relation is difficult to trace. However, the ideas central to the concept are found in the work of Joseph-Louis Lagrange (1736-1813) and Carl Friedrich Gauss (1777-1855) to develop congruence relations defined on the integers. The ideas are also present in Peano's 1889 work I Principii di Geometria [82]. Gottfried Wilhelm Leibniz (1646-1716) was in 1692 the first mathematician to use the word 'function' to describe a quantity associated with an algebraic relationship describing acurve. In 1748, LeonhalrdEuler(1707-1783) wroteinhisIntroductio inAnalysin 1nfinitnrum. thit 'a fuinction nf a variahl-e ninntit, is an
Carl Friedrich Gauss
nnhltirnl P-nrpccinn c-mnnQ-A
in any manner from that variable quantity ... It is from the work of Euler and Alexis Clairaut (1713-1765) that we have inherited the f(x) notation that is still in use today. In 1837, Peter Gustav Le.leune Dirichlet (1805-1859) set down a more rigorous formulation of the concepts of variable, function, and the correspondence between the
Supplementary Exercises
103
independent variable x and the dependent variable y when y = f (x). Dirichlet's definition did not depend on an algebraic relationship, but allowed for a more abstract relationship to define the connection between the entities. He stated that 'y is a function of a variable x, defined on the interval a < x < b, if to every value of the variable x in this interval there corresponds a definite value of the variable y. Also, it is irrelevant in what way this correspondence is established.' The modem set theoretic definition of a function as a subset of a Cartesian product is based on Dirichlet's work, but its formal development comes from a group of mathematicians writing under the pseudonym of Bourbaki in the late 1930s [81] . Mathematical induction was first used by the Italian mathematician and engineer Francesco Maurocyulus (1494-1575) in his 1575 book Arithmetica to prove that the sum of the first n positive odd integers is n2 . Blaise Pascal (1623-1662) used induction in his work on his arithmetic triangle, now called the Pascal triangle. In his Traitt du triangle arithmetique (1653), Pascal gave a clear explanation of induction in proving the fundamental property defining his triangle. The actual name 'mathematical induction' was given to the principle by Augustus De Morgan in an article on the method of proof in 1838 [74].
SUPPLEMENTARY EXERCISES Compute each of the sets in Exercises 1-8 if A = {l, 2, 3, 4), B = {1,4, 5), C = {3, 5, 61, and the universalset is U = {1,2,3,4,5, 6}. 1. AnC
2. AUB
3.A
4. C
5. AnB
6.;AUC
7. (BUC)
8. An(BUC)
Draw Venn diagrams depicting the sets in Exercises 9-12. 10. A -(BUC)
9. (A-B)
11. AU(B -C)
12. An (C-B)
Determine if each statement in Exercises 13-16 is true orfalse. 13. 37
=
18 (mod 2)
14. 45 -- 21 (mod 11)
15. -7
=
53 (mod 12)
16. -18 --
64 (mod 7)
In Exercises 17-22 perform the indicated operationsin Zm . Write your answer in the form [r], where 0 < r < m. 17. [43] + [32] in Z1I 20. [ -3][9] in Z15 23. If x - 4 (mod 11) and y 24. If f(x) =
X
3
+
18. [-12] + [95] in
19. [5][1] in Z9
Z2 5
21. [22]7 in Z5 =
22. [13]6[23]5 in 2
9 (mod 11), what is the remainder when x -5, determine gf and fg.
+
Z1 2
3y is divided by 11?
1 and g(x) = 2x
In Exercises 25-28 determine if relation R is afunction with domain X = 1, 2, 3, 4}. 25. R = {(1, 4), (2, 1), (3, 2), (4,4)) 27. R = {(I, -1), (2, -1), (3, 1), 4, 1))
26. R = {(1, 3),(3, 4), (4, 1)1 28. R = {(I, 2), (2, 3), (2, 1), (3,0), (4, 1))
Let Z denote the set of integers. Which of the functions g: Z onto? 29. g(x) = 2x - 7
-*
Z in Exercises 29-32 are one-to-one? Which are
30. g(x) = x2 - 3
104
Chapter 2 Sets, Relations, and Functions x
31. F(x)=x+3
-2
x+
32'. g(x) = 5
ifx>0 if x
-
x
Let X denote the set of real numbers. In Exercises 33-36 compute the inverse of each function f: X exists. 33.
f (x) = xj - 2
34.
f (x) = 3x
+ 1
35.
f (x) = 3x - 6
36.
-*
X if it
f (x) = x3 + 5
37. At the local ice cream parlor, a sundae can be ordered with any combination of the following toppings: hot fudge, whipped cream, maraschino cherries, nuts. anc marshmallows. How many different sundaes can be ordered? 38. In how many different ways can the Supreme Court rernder a 6-to-3 decision? 39. A grievance committee consisting of 6 persons is to be formed from 7 men and 8 women. How many different committees can be formed? 40. An investor is going to buy 100 shares of stock in each of 6 companies selected from a list of 10 companies prepared by her broker. How many different selections of 6 companies are available to the investor? In Exercises 41-44 show that each relation R is an equivalence relation on set S. Then describe the distinct equivalence classes of R. 41. S ={1,2,3,4,5,6,7,8),andxRymeansthatx---
{-4,0,4).
42. S ={1,2,3,4,5,6,7,8},andxRymeansthat14--r = 14-
y.
43. S is the set of integers, and x R y means that either x = y or lx-y = 1 and the larger of x and y is even. 44. S is the set of nonzero real numbers, and x R y means that xy > 0. 45. Let x be any integer and a and b be integers greater tlhan 1. Define A to be the congruence class of x in Za and B to be the congruence class of x in Zh. Prove that if a divides b, then B C A. 46. How many relations can be defined on S = {a, b, c}? 47. How many equivalence relations on S = {a, b, c} are there? 48. How many functions f: S
-*
S are there if S = {a, L, c}?
49. Suppose that R is an equivalence relation on set S and also a function with domain S. Describe R. 50. Let g: Z -* Z be defined by g(x) = ax + b, where Z' Jenotes the set of integers and a, b (a) Prove that g is one-to-one. (b) What must be true about a and b if g is onto?
E Z with a $ 0.
Exercises 51-53 give a relation R on a set S. Tell which ofr he reflexive, antisymmetric, and transitive propertiesR has on S. 51. S is the set of all subsets of {1, 2,3,4) and A R B if and only if A 52. S = {{1,2,3}, {2,3,4}, {3,4,5}} andA
R B if and only
C
B and A
#
B.
ifIA -BI < 1.
53. S is the set of positive integers and x R y if and only if y = n2 x for some integer n. 54. Let S be a set of people. For x, y is a partial order on S.
E S define
xR
y to mean that x =
y or x is a descendant of y. Prove that R
55. Suppose that the advertisement for the Fourth of July sale (as described in Section 1.1) is to be created by a team of persons who perform only one task at a time In what sequence should the tasks be performed? 56. Let R be a relation on set S. Define a relation R' on 5 by x R' y if and only if y R x. Prove that if R is a partial order on S, then so is R'.
Supplementary Exercises
105
57. Suppose that R is a relation on set S that is both an equivalence relation and a partial order. Describe R. Use the following informationfor Exercises 58-62. If R is a partialorder on a set S and x, y, and z are in S, we call z the sup (pronounced 'soup') of x and y and write z = x v y in case (a) xRzandyRz, and (b) ifw E Sand bothxRwandyRw, thenzRw.
58. Let S = {l, 2, 3, 4, 5, 6} with x R y if and only if x divides y. Compute x V y for all pairs (x, y) in S x S for which it exists.
59. Let R be a partial order on set S, and let x, y e S. Prove that if x V y exists, then so does y v x, and that x V y = y V x.
60. Let T be a set, and let S be the set of all subsets of T. For A, B E S define A R B if and only if A C B. Prove that A v B = A U B for all A, B E S. 61. Let R be a partial order on set S, and let x, y, z E S. Prove that if x v y, y V z, (x V y) V z, and x v (y v z) all exist, then the latter two are equal. 62. Give an example of a partial order R on a set S where x v y, y v z, and x v z all exist, but (x V y) V z does not exist. In Exercises 63-66 let Z denote the set of integers, f: Z f(x) = f(y).
Z be a function, and let x R y be defined to mean that
-*
63. Prove that R is an equivalence relation on Z. 64. Determine [n], the equivalence class containing n E Z with respect to R, if f is the function defined by f (x) - X2 .
65. What must be true about a function f if, for every n E Z, the equivalence class containing [n] consists of exactly one element? 66. Give an example of a function f: Z -* Z for which every equivalence class [n] contains exactly three elements. Prove each of the set equalities in Exercises 67-72.
67. A x (B UC) = (A x B) U (A x C)
68. A x (B n C) = (A x B) n (A x C) 70. (AUB) -C=(A -C)U(B-C) 72. (A-B)U(A -C)A -(BnC)
69. Ax(B-C)=(AxB)-(AxC) 71. (A -B) -(AC)=An(C-B)
Prove the results in Exercises 73-80 by mathematical induction. + (2n - 1)2 = n(2n
73. For all positive integers n, 12 + 32 + 1
74. For all positive integers n, 13 +
1
I 3 3-5
5 75. For all positive integers n, 1*2*3 +
+ ±
1)(2n + 1) 3
1
(
n
(2n -l)(2n +l)
6
I2-3 2.-3.-4
76. 77. 78. 79. 80.
-
+.4+
2n±+1
n +4
n(3n +7)
n(n +l)(n±+2)
2(n +l)(n +2)
+ n
Any integer n > 23 can be written in the form 5r + 7s for some nonnegative integers r and s. Any postage of 8 cents or more can be obtained using only 3-cent and 5-cent stamps. For all positive integers n and any distinct real numbers x and y, x -y divides xn -yn. For all nonnegative integers n, 32n+1 + 2(- 1)n 0- (mod 5). For all nonnegative integers n, 7h1+2 + 8 2n+1 0-(mod 57).
106
Chapter 2 Sets, Relations, and Functions
81. Choose any n > 3 distinct points on the circumference (of a circle, and join consecutive points by line segments to form an n-sided polygon. Show that the sum of the interior angles of this polygon is 180n - 360 degrees. 82. Prove that Fn+1 = F,-,Fm + F,m+IFm+, for any irtegers m and n such that n > m > 1. (Hint: Fix m, and use induction on n beginning with n = m + 1.) 83. Prove that Fm divides Fmn for all positive integers n. 84. Prove that if n > 12 is an even integer not divisible by 3, then an n x n checkerboard with one square removed can be covered by L-shaped pieces as in Figure 2.20. (Hint: Divide the n x n board into (n - 6) x (n -6), 6 x (n -6), (n -6) x 6, and 6 x 6 subboards.)
COMPUTER PROJECTS Write a computer program having the specified input and o wtput. 1. Given a nonnegative integer n, list all the subsets of {[, 2, . . ., n 2. Let U be a finite set of real numbers. Given lists of the clements in the universal set U and in subsets A and B, list the elements in the sets A U B, A n B, A - B. A, and B. 3. Given a finite set S of integers and a subset R of S x S, determine which of the reflexive, symmetric, antisymmetric, and transitive properties are possessed by the relation R on S. Assume that the elements of the sets S and R are listed. 4. Given a finite set S of integers and a subset R of S x 5, determine if R is an equivalence relation on S. If so, list the distinct equivalence classes of R. Assume that tie elements of the sets S and R are listed. 5. Given a partial order R on a finite set S, determine a total order on S that contains R. Assume that the elements of the sets S and R are listed. 6. Given integers x, y, and m with m > 2, compute [x] t- l y] and [x][y] in Zm Write the answers in the form [r], where 0 < r < m. 7. Given a positive integer n, compute 1!, 2!, . . ., n!. 8. Given a positive integer n, compute F, F2 , .. , F,. 9. Given sets X = (XI, X2, .X., m) and Y = {yI, y2, .,) the functions with domain X and codomain Y.
containing m and n elements, respectively, list all
10. Given sets X = {xI, x2 . . ., Xm and Y = {yI, Y2. y, containing m and n elements, respectively, list all the one-to-one functions with domain X and codotnain Y. 11. Given sets X = {xI, x2, .X. ,m } and Y = {Y1, Y2. the onto functions with domain X and codomain Y.
y,Yn containing m and n elements, respectively, list all
12. Given sets X = {xI, x2 , . . , x,} and Y = {1Y, Y2, spondences with domain X and codomain Y.
n} containing n elements, list all the one-to-one corre-
..
13. Given two sorted lists of real numbers, merge them in o a single sorted list using the technique described in Example 2.64.
SUGGESTED READINGS 1. Buck, R. C. 'Mathematical Induction and Recursive Definitions.' American Mathematical Monthly, vol. 70, no. 2 (February 1963): 128-135. 2. Gallian, Joseph A. 'Assigning Driver's License Numbers.' Mathematics Magazine, vol. 64, no. 1 (February 1991): 13-22.
Suggested Readings 3.
107
. 'The Mathematics of Identification Numbers.' The College MathematicsJournal, vol. 22, no. 3 (May
1991): 194-202. 4. Halmos, Paul R. Naive Set Theory. New York: Springer, 1994. 5. Hayden, S. and J. Kennison. Zermelo-Fraenkel Set Theory. Columbus, Ohio: Charles Merrill, 1968. 6. Henken, L. 'On Mathematical Induction.'AmericanMathematicalMonthly, vol. 67, no.4 (April 1960): 323-337. 7. Tuchinsky, Phillip M. 'International Standard Book Numbers.' The UMAP Journal,vol. 6, no. I (1985): 41-53. 8. Vanden Eynden, Charles. Elementary Number Theory, 2nd ed. New York: McGraw-Hill, 2001.
3
Graphs
3.1 Graphs and Their Repre sensations 3.2 Paths and Circuits 3.3 Shortest Paths and Distance 3.4 Coloring a Graph 3.5 Directed Graphs and Multigraphs Even though graphs have been studied for a long time, the increased use of computer technology has generated a new interest in them. Not only have applications of graphs been found in computer science but in many other areas such as business and science. As a consequence, the study of graphs has become important to many.
3.1
o
GRAPHS AND THEIR REPRESENTATIONS It is quite common to represent situations involving objects and their relationships by drawing a diagram of pointy,with segments joining those points that are related. Let us consider some specific examples of this idea.
+
Example 3.1 Consider for a moment an airline route map in which dots represent cities, and two dots are joined by a segment whenever there is a nonstop flight between the corresponding cities. A portion of such an airline map is shown in Figure 3.1. I
San Francisco
Chicago
T/ Lo ASt. Los An yeFes
g
Louis
FIGURE 3.1
108
New York
109
3.1 Graphs and Their Representations
Example 3.2 Suppose we have four computers labeled A, B, C, and D, where there is a flow of information between computers A and B, C and D, and B and C. This situation can be represented by the diagram in Figure 3.2. This is usually referred to as a communication network. + A A
D
B B -OE
C
D FIGURE 3.2
+
C
F FIGURE 3.3
Example 3.3 Suppose that there is a group of people and a set of jobs that some of the people can do. For example, for individuals A, B, and C and jobs D, E, and F, suppose A can do only job D, B can do jobs D and E, and C can do jobs E and F. This type of situation can be represented by the diagram in Figure 3.3, where line segments are drawn between an individual and the jobs that person can do. + The general idea in the three examples is to represent by a picture a set of objects in which some pairs are related. We will now describe this type of representation more carefully. A graph is a nonempty finite set V along with a set £ of 2-element subsets of V. The elements of V are called vertices and the elements of £ are called edges. Figure 3.2 depicts a graph with vertices A, B, C, and D and edges IA, B), {B, C }, and {C, D }. Thus a graph can be described either by the use of sets or by the use of a diagram, where segments between the vertices in V describe which 2-element subsets are being included. Figure 3.3 shows a graph with vertices A, B, C, D, E, and F and with edges {A, D}, {B, D}, {B, El, {C, E}, and {C, F). We caution the reader that the use of terminology in graph theory is not consistent among users, and when consulting other books, definitions should always be checked to see how words are being used. In our definition of a graph, the set of vertices is required to be a finite set. Some authors do not make this restriction, but we find it convenient to do so. Also our definition of a graph does not allow an edge from a vertex to itself, or different edges between the same two vertices. Some authors allow such edges, but we do not. Whenever we have an edge e = {U, V}, we say that the edge e joins the vertices U and V and that U and V are adjacent. It is also said that edge e is incident with the vertex U and that the vertex U is incident with the edge e.
110
Chapter 3
Graphs
For the graph in Figure 3.2, we see that vertices A and B are adjacent, whereas vertices A and C are not becausMe there is no segment between them (that is, the set {A, C I is not an edge). In Fig jre 3.3 the edge {B, E} is incident with the vertex B. Note that the diagram in Figure 3.2 can be drawn differently and still represent the same graph. Another representation of this graph is given in Figure 3.4. .--
A
p
p
p
B
C
D
FIGURE 3.4
The way our picture is drawn is not important, although one picture may be much easier to understand that another. What is important in the picture is which vertices are joined by edges, fDr this describes what relationships exist between the vertices. In Figure 3.5 we have redrawn the graph from Figure 3.2 in such a way that the edges meet at a place other than a vertex. It is important not to be misled into believing that there is now a new vertex. Sometimes it is not possible to draw a picture of a graph without edges meeting in this way, and it is important to understand that such a cros sing does not generate a new vertex of the graph. It is often very difficult to determine if a graph can be drawn without any edges crossing at points other than vertices. D B
A
C.0
D E
FIGURE 3.5
FIGURE 3.6
In a graph, the number of edges incident with a vertex V is called the degree of V and is denoted as deg(V). [n Figure 3.6 we see that deg(A) = 1, deg(B) = 3, and deg(C) = 0. One special graph that is encountered frequently is the complete graph on n vertices, where every vertex is Joined to every other vertex. This graph is denoted by A2,. Figure 3.7 shows Kt 3 and IC4.
A ,3
X4 FIGURE 3.7
In Figures 3.6 and 3.7, notice that adding the degrees of the vertices in each graph yields a number that is twicee the number of edges. This result is true in general.
3.1 Graphs and Their Representations
Theorem 3.1
1l 1
In a graph, the sum of the degrees of the vertices equals twice the number of edges. Proof. The key to understanding why this theorem is true is to see that each edge is incident with two vertices. When we take the sum of the degrees of the vertices, each edge is counted twice in this sum. Thus the sum of the degrees is twice the number of the edges. Look again at Figures 3.5, 3.6, and 3.7 to see how this double counting of edges takes place. M 00htleS tRqressenfSalion;s of)(]grap'B~~~is
It is often necessary to analyze graphs and perform a variety of procedures and algorithms upon them. When a graph has many vertices and edges, it may be essential to use a computer to perform these algorithms. Thus it is necessary to communicate to the computer the vertices and edges of a graph. One way to do so is to represent a graph by means of matrices (discussed in Appendix B), which are easily manipulated with a computer. Suppose we have a graph g with n vertices labeled VI, V2, .. , V,. Such a graph is called a labeled graph. To represent the labeled graph g by a matrix, we form an n x n matrix in which the i, j entry is 1 if there is an edge between the vertices Vi and Vj and 0 if there is not. This matrix is called the adjacency matrix of g (with respect to the labeling) and is denoted by A(g). +
Example 3.4
Figure 3.8 contains two graphs and their adjacency matrices. For (a) the 1, 2 entry is 1 because there is an edge between vertices VI and V2, and the 3, 4 entry is 0 because there is no edge between V3 and V4. For (b) we see that the 1, 2 and 1, 3 entries are 1 because of the edges between VI and V2 and between VI and V3. Note that in A(g 1) the sum of the entries in row 1 is 1, which is the degree of VI, and likewise the sum of the entries in row 2 is the degree of V2. This illustrates a more general result. + V,
V2
q0
, V3
1 0 0 A((g,) =
V4
o l
tI L-
n
I
A (-J2)
n
I
=
0l 1 I
O
I
I
I
0
-- I2
(a)
(b) FIGURE 3.8
Theorem 3.2
The sum of the entries in row i of the adjacency matrix of a graph is the degree of the vertex V1in the graph.
112
Chapter 3
Graphs
Proof. We recall that each 1 in row i corresponds to an edge on the vertex Vi. Thus the number of Is in row i is the number of edges on Vi, which is the degree of Vi. a Matrices are not the only way to represent graphs in a computer. Although an adjacency matrix is easy to construct, this form of representation requires n n = n2 units of storage for a graph with n vertices and so can be quite inefficient if the matrix contains lots of zeros. This means that if an algorithm to be performed on the graph requires a lot of searching of vertices and adjacent vertices, then the matrix representation can require a lot of unnecessary time. A better representation for such a graph is an adjacency list. The basic idea of an adjacency list is to list each vertex followed by the vertices adjacent to it. This provides the basic information about a graph: the vertices and the edges. To form the adjacency list, we begin by labeling the vertices of the graph. Then we list the vertices in a vertical column, and after each one we write down the adjacent vertices. 'Tius we see that the Is in a row of an adjacency matrix tell what vertices are listed in the corresponding row of an adjacency list.
'
Example 3.5 For the graph in Figure 3.9 there are 6 labeled vertices, and we list them in a vertical column as in (b). Beside vertex VI we list the adjacent vertices, which are V2 and V3. Then proceeding to the next vertex V2, we list the vertices adjacent to it, VI and V4. This process is continued until we get the adjacency list in (b). + '2
VI: V2, V3 V2: VI, V4 V 3 : VI, V4
I/4
14: V2, V3, V5 V<: V.
V'
V6 : V5
(a)
(b) FIGURE 3.9
IS()rn('13 qp5 W.1-i iS nri,
In 1953 the CIA managed to photograph a KGB document listing their agents in a large third-world city, showing their past operations, duties, and contacts among themselves. Unfortunately, it listed the agents by the code designations D through L. The document shows that agent D 's contacts were F and L; E 's were
3.1
Graphsand Their Representations
113
J and K; F's were D, J, and L; G's were I and L; H's were I and J; I's were G, H, and K; J's were E, F, and H; K's were E and I; and L's were D, F, and G. Drawing an edge between agents if they are contacts produces the graph A shown in Figure 3.10. Unfortunately, the information in the document was of little use without knowing the identities of the agents. E
F
G
FIGURE 3.10
An inquiry to the CIA office in this city revealed that the suspected agents there were Telyanin, Rostov, Lavrushka, Kuragin, Ippolit, Willarski, Dolokhov, Balashev, and Kutuzov. By examining records of past meetings observed among them, the CIA created the contact graph C in Figure 3.11 by joining two individuals with an edge if they were known to have met together.
stov
PLavrushka
tragin wl HIM SKl
Ippolit
FIGURE 3.11
If these nine persons are the agents described in the KGB document, then it must be possible to match the code designations D through L with the names in Figure 3.11 so that the edges in A correspond exactly to the edges in C. In general, we say that a graph g1 is isomorphic to a graph g2 when there is a one-to-one correspondence f between the vertices of g1 and g2 such that the vertices U and W are adjacent in 5, if and only if the vertices f(U) and f (W) are adjacent in g2 . The function f is called an isomorphism of g1 with g2 . Because the relation 'is isomorphic to' is a symmetric relation on any set of graphs (see Exercise 38), we usually just say that the graphs 51 and g2 are isomorphic. Thus isomorphic graphs are essentially the same in the sense that, except for notation, they have the same vertices and the same pairs of vertices are adjacent. An examination of the graphs in Figure 3.12 shows that they are isomorphic by using the correspondence indicated in the figure. The graphs in Figures 3.10
114
Chapter3 Graphs A A
---- a
B ...
d
C ...
g
D ... E ... F ... G ...
c f b e
A
a
F
FIGURE 3.12
Q
FIGURE 3.13
and 3.12 are not isomorphic, however, because they have different numbers of vertices. In Figure 3.13, vertex C in g1 is adjacent to the vertices A, B, D, and E. Therefore, under any isomorphism of g1 with g2 , the image of C would also need to have 4 adjacent vertices s. Since there are no vertices of degree 4 in g2 , we see that g1 and g2 are not isomorphic. This last observation illustrates the following theorem.
Theorem 3.3
Let f be an isomorphism of graphs g1 and of V and f(V) are equal.
g2 . For any vertex V in g1 , the degrees
Proof. Suppose f is an iso-Morphism of g1 with g2 and that V is a vertex of degree k in gl. Then there are exactly k vertices U1, U2 ,. . - Uk in g1 that are adjacent to V. Since f is an isomnorphism, f (Ul), f (U2), . . , f (Uk) are adjacent to f (V). Since there are no oth r vertices in g1 adjacent to V, there are no other vertices in g2 adjacent to f(V) Thus f(V) has degree k in 2 . l As a consequence of this theorem, we see that the degrees of the vertices of isomorphic graphs must be exactly the same. A property is said to be a graph isomorphism invariant if, whenever g1 and 52 are isomorphic graphs and g has this property, then so does g2 . The properties 'has n vertices,' 'has e edges,' and 'has a vertex of degree k' are all invariants.
3.1 Graphs and TheirRepresentations
115
Thus one way to show two graphs are not isomorphic is to find an invariant property possessed by only one of the graphs. This is what was done in showing that the graphs in Figure 3.13 are not isomorphic. Returning to the graphs in Figures 3.10 and 3.11, we will construct an explicit isomorphism of A with C by observing similarities between the graphs. Note that vertices L, D, and F form the only 'triangle' in A, that is, the only set of three vertices in which each pair is adjacent. Thus, to have an isomorphism, these vertices must correspond to Kutuzov, Lavrushka, and Dolokhov, in some order, since they form the only similar set in C. In fact, since of these six vertices only D and Kutuzov have degree 2, we must have f (D) = Kutuzov. Moreover, since of L and F, only L is adjacent to another vertex of degree 2 (namely, G), and of Lavrushka and Dolokhov, only Dolokhov is joined to another vertex of degree 2 (namely Rostov), we must have f (L) = Dolokhov, f (F) = Lavrushka, and f (G) = Rostov.
Continuing in this way, we find that for f to be an isomorphism we must have f(J) = Telyanin, f(I) = Balashev, f(H) = Ippolit, f(K) = Kuragin, and f (E) = Willarski. It is easily checked that f is indeed an isomorphism. Note that if there had been more than one isomorphism from A to C, then a complete identification of the agents would not have been possible. For example, consider the graphs of agents 1Z and contacts S in Figure 3.14.
V*
V
x
x Rt
S FIGURE 3.14
Notice that in addition to the obvious isomorphism sending V, W, X, Y, and Z into V *, W *, X *, Y *, and Z *, respectively, there is also an isomorphism that
is the same except that W maps to X * and X to W *. Hence, in this case, it would be impossible to deduce whether W* or X * is the identity of agent W. EXERCISES 3.1 In Exercises 1-4 list the set of edges and set of vertices for each graph. L. A
t
B
2. Fi
* Ho
C
oD
G
116
Chapter 3
Graphs
3. Fe
OG
4. A
B
C
D
He
In Exercises 5-8 draw a diagram representingthe graph with the set V of vertices and the set £ of edges. 5. V
=
(A, B,C, D}, £
={{B,CI,{C,
Al, (B, D I
6. V= {X,Y,Z,W},E= {{X,YI,{X,Z),{Y,Z1,{:Y,AII 7. V={G,H,J},£=0 8. V
=
(A, X, B, Y}, £
=
((A, X}, (X, B], (B, Y), {Y, 4A}
In Exercises 9-14 determine if a graph is indicated.
9. * 11.
1i).
A
12. A
B
B
C
D
13. V = {A, B, C, D}, £ = ({A, B), 1A, A}}
14. V = [A, B}, S = [{A, B}, [B, C}}
15. Construct the graph where the vertices are you, your- parents, and your grandparents with a relationship of 'are the same sex.' 16. Construct the graph where the vertices are you, your parents, and your grandparents with a relationship of 'born in the same state.' 17. There is a group of 6 students, Alice, Bob, Carol, Dean, Santos, and Tom, where Alice and Carol are always feuding, likewise for Dean and Carol, and for Santus, Tom, and Alice. Draw the graph to represent this situation. 18. Draw the graph with V = {I, 2, .
1., 01 as its set of v-itices and
£={Jx, y}: x, y in V, x : y, and x divides y or y divides x} as its set of edges. In Exercises 19-20 list the vertices adjacent to A and give tee degree of A. Repeat for the vertex B. 19.
A
B
E
20. C a
-
A C
D
21. Draw graphs where (a) there are 4 vertices, each with degree 1. (b) there are 4 vertices, each with degree 2.
B
3.1 Graphsand TheirRepresentations 22. Show that there are an even number of vertices with odd degree in any graph. 23. How many edges does KC 3 have? 1(4? K5? IC, in general? 24. Can there be a graph with 8 vertices and 29 edges? Justify your answer. 25. How many vertices are there in a graph with 10 edges if each vertex has degree 2? In Exercises 26-29find the adjacency matrix and the adjacency listfor each graph.
26. 1(4 27.
28. V'
V2 VI
V3
V5
V4
29. VI -
V2
4
V4 411iV
3
* V2
V3 . In Exercises 30-31 construct the graphfor each adjacency matrix. Label the vertices V1 , V2 , V3 ,.
30.
0
101 1 if 110 1 0 Oi
1
11
1
0 1 i
31.
111
IO 0 0 0]
Li
0 1f
1 1 0]
In Exercises 32-33 construct the graphfor each adjacency list.
33. VI: V2 , V3
32. VI: V2 , V4, V5
V2 : VI, V4 V3: VI, V4 V4 : V2, V3
V 2 : VI, V 3 V3 : V2, V5
V4: VI V'i: VI, V3
34. What does it mean when the adjacency matrix of a graph contains only zeros? In Exercises 35-37, can each matrix be an adjacency matrix?
O 101 35.
1 0
0
1 I
1 1 0
f
Lool10]
Ol O OI I100 11
36. 0 0 0 0 I
I11000O
37.
I I
I 1
]
l
I
I
I
00000]
38. Show that the relation 'is isomorphic to' is an equivalence relation on any set of graphs. 39. Will two graphs that have the same number of vertices always be isomorphic? Justify your answer. 40. Will two graphs that have the same number of edges always be isomorphic? Justify your answer.
117
118
Chapter 3 Graphs
41. Will two graphs having the same number of vertices of degree k for each nonnegative integer k always be isomorphic? Justify your answer. 42. Are the following pairs of graphs isomorphic? Justify your answer.
(a)
(I)
(c)
--
A
N
43. Are the following pairs of graphs isomorphic? Justify y our answer. ,1
ka)
-
-
I,=
iK)
,
-
c)
-
I 44. Draw all the nonisomorphic graphs with 3 vertices. 45. Draw all the nonisomorphic graphs with 4 vertices. 46. Draw all the nonisomorphic graphs with 5 vertices of degrees 1, 2, 2, 2, and 3. 47. Draw all the nonisomorphic graphs with 6 vertices of degrees 1, 1, 1, 2, 2, and 3. 48. Consider a graph whose vertices are the closed intervals [m, n], where m and n are distinct integers between 1 and 4, inclusive. Let two distinct vertices be adjacent. if the corresponding intervals have at least one point in common. Draw the graph. 49. Suppose a graph has n vertices, each with degree at least 1. What is the smallest number of edges the graph can have? Justify your answer. 50. Suppose a graph has n vertices, each with degree at least 2. What is the smallest number of edges it can have? Justify your answer. 51. A graph has m edges with m > 2. What is the smalles- number of vertices it can have? Justify your answer. 52. For n > 3, let V = {1, 2, . , ni and £ = {{x, y}: x, y in V. x What vertices of this graph have degree 1?
$
y,
and x divides y or y divides x}
3.2 Paths and Circuits
119
53. Suppose Mr. and Mrs. Lewis attended a bridge party one evening. There were three other married couples in attendance and several handshakes took place. No one shook hands with himself or herself, no spouses shook hands, and no two people shook hands more than once. When each other person told Mr. Lewis how many hands he or she shook, the answers were all different. How many handshakes did Mr. and Mrs. Lewis each make? 54. Prove that if a graph has at least two vertices, then there are two distinct vertices that have the same degree.
3.2
+
PATHS AND CIRCUITS As we have seen, graphs can be used to describe a variety of situations. In many cases we want to know whether it is possible to go from one vertex to another by following edges. In other cases it may be necessary to perform a test that involves finding a route through all the vertices or over all the edges. While many situations can be described by graphs as we have defined them, there are others where it may be necessary to allow an edge from a vertex to itself or to allow more than one edge between vertices. For example, when a road system is being described, there can be two roads, an interstate highway and an older two-lane road, between the same two towns. There could even be a scenic route starting
and ending at the same town. To describe these situations, we need to generalize the concept of a graph. A multigraph consists of a nonempty finite set of vertices and a set of edges, where we allow an edge to join a vertex to itself or to a different vertex, and where we allow several edges joining the same pair of vertices. An edge from a vertex to itself is called a loop. When there is more than one edge between two vertices, these edges are called parallel edges. It is important to note that a graph is a special kind of multigraph. Thus all the definitions given for multigraphs apply to graphs as well. +
Example 3.6 The diagram in Figure 3.15 represents a multigraph but not a graph because there are two parallel edges k and m between the vertices Y and Z and a loop h at vertex X. +
FIGURE 3.15
120
Chapter 3
Graphs
In a multigraph the number of edges incident with a vertex V is called the degree of V and is denoted as deg(V). A loop on a vertex V is counted twice in deg(V). Thus, in Figure 3.15, deg(Y) = 3 and deg(X) = 4. Suppose 5 is a multigraph and U and V are vertices, not necessarily distinct. A U-V path or a path from U to V is an alternating sequence VI , e 1, V2, e2, V3, . . ., Vn, en, V,+ I
of vertices and edges, where the first vertex VI is U, the last vertex V,+I is V, and the edge ei joins V, and V,+1 for i = 1, 2, .. , n. The length of this path is n, the number of edges listed. We note that U is a path to itself of length 0. In a path the vertices need not be distinct, and some of the edges can be the same. When there can be no chance of confusion, a path can be represented by the vertices VI, V2, . . ., V,1 only or by the edges e1, e2, . . ., en only. Note that in a graph it is always sufficient to list only the vertices or the edges.
+
Example 3.7 In Figure 3.15, U, f, V, g, X is a path of length 2 from U to X. This path can also be written as f, g. Likewise f, g, h is a path of length 3 from U to X, and U, f, V, f, U is a path of length 2 from U to U. The path Z, m, Y cannot be described by just listing the vertices Z, Y since it would not be clear which edge between Z and Y, k or m, is part of the path. +
A path provides a way of describing how to go from one vertex to another by following edges. A U-V path need not be an efficient route; that is, it may repeat vertices or edges. However, a 1U-V simple path is a path from U to V in which no vertex and, hence, no edge i i repeated. There are no simple paths of length 1 or more from a vertex to itself. Furthermore, a simple path does not Stve loops or pairs of parallel edges in it. In some sense, a simple path is an efficient route between vertices, whereas a path allows wandering back and forth, repeating vertices and edges.
+
Example 3.8 For the multigraph in Figure 3.16, the edges a, c, d, j form a simple path from U to Z, whereas a, c, m, d, i is a path from U to Z that is not a simple path because the vertex W is repeated. Similarly, e, i is a simple path from X to Z, but f, i, j is a path from X to Z that is not a simple path. Note also that c, p, f, i, e, n is a path from V to U that is not simple, but deleting f, i, e produces a simple path c, p, n from V to U. This. illustrates the following theorem. +
3.2 Paths and Circuits
121
m
U g
R
FIGURE 3.16
Theorem 3.4
Every U-V path contains a U-V simple path. Proof. Let us suppose that U = VI, el, V2 , .. , e, V,+1 = V is a U-V path. In the special case that U = V we can choose our U-V simple path to be just the vertex U. So suppose that U :A V. If all of the vertices VI, V2, . . ., V,+, are different initially, then our path is already a U-V simple path. Thus let us suppose that at least two of the vertices are the same, say Vi = Vj, where i < j. See Figure 3.17 for an illustration of how to form a simple path from Vi to Vj. -
-
-
ei-
v,1
V.
FIGURE 3.17
We delete ei, Vi+, . . , ej-1 , Vj from the original path. What has been deleted is the part that is between vertex Vi and edge ej. This still leaves a path from U to V. If there are only distinct vertices left after this deletion, then we are done. If there are still repetitions among the remaining vertices, the above process is repeated. Because the number of vertices is finite, this process will eventually end and give a U-V simple path from U to V. id A multigraph is called connected if there is a path between every pair of vertices. Thus in a connected multigraph we can go from any one vertex to another by following some route along the edges.
Example 3.9 The multigraph in Figure 3.16 is connected since a path can be found between any two vertices. However, the graph in Figure 3.18 is not connected since there is no path from vertex U to vertex W. +
122
Chapter3 Graphs U
*X
We V FIGURE 3.18
A cycle is a path VI, el, 1 2, e2, . ., Vn, en, Vn+l, where n > 0, VI = Vn+l and all the vertices VU,V2, . ., V, and all the edges el, e2, . . ., e are distinct. Thus a cycle of length 3 or more cannot have loops or parallel edges as part of it.
Example 3.10
For the multigraph in Figure 3.16, the edges a, c, p, n form a cycle. Likewise, the edges g, b, c, p, f, h fonn a cycle. Furthermore, the edges f, p, d, e, n, g, h do not form a cycle because the vertex X is used twice. d E~der C,0.Sfir. .ds and.: PaIJ0Z:s In testing a communication network, it is often necessary to examine each link (edge) in the system. In order lo minimize the cost of such a test, it is desirable to devise a route that goes through each edge exactly once. Similarly, when devising a garbage pick-up route (where the garbage is picked up along both sides of the street with one pass), we will want to go over each street exactly once. Thus, when modeled by a multigraph (v% ith comers as the vertices and streets as the edges), we want a path that includes every edge exactly once. Because the mathematician Leonhard Euler was the first person known to consider this concept, a path in a multigraph g that includes exactly once all the edges of G and has different first and last vertices is called an Euler path. A path that includes exactly once all :he edges of G and has the same initial and terminal vertices is called an Euler circuit.
Example 3.11 For the graph in Figure 3.19(a), the path a, b, c, d is an Euler circuit since all the edges are included and each edge is included exactly once. However, the graph in Figure 3.19(b) has neither an Euler path nor circuit because, to include all three of the edges in a path, we would have to backtrack and use an edge twice. For the graph in Figure 3.19(c), there is an Euler path a, b, c, d, e, f but not an Euler circuit. +
123
3.2 Pathsand Circuits a
(a)
(b)
(c)
FIGURE 3.19
As we proceed along an Euler circuit, each time a vertex is reached along some edge, there must be another edge for us to exit that vertex. This implies that the degrees of the vertices must all be even. In fact, as we will see shortly, the converse statement is also true: Whenever a multigraph is connected and the degree of each vertex is even, then the graph has an Euler circuit. The following example shows how an Euler circuit can be constructed in such a case. +
Example 3.12 The multigraph shown in Figure 3.20(a) is connected, and the degree of each vertex is even. Therefore an Euler circuit can be constructed as follows. Select any vertex U, and construct a path C from U to U by randomly selecting unused edges for as long as possible. For example, if we start at G, we may construct the path C: G, h, E, d, C, e, F, g, E, j, H, k, G. The edges in this path C are shown in color in Figure 3.20(b).
f D
Ig i
hE
0F
ID
k
G
(a)
J
AH
m
m
(b)
m
(c)
FIGURE 3.20
Note that such a path must return to the starting vertex because the degree of each vertex is even and the number of vertices is finite. In addition, every edge incident with the starting vertex must be included in this path. If, as is the case here, this path C is not an Euler circuit, then there must be edges not in C. Moreover, since the multigraph is connected, there must be a vertex in C that is
124
Chapter3 Graphs
incident with an edge not in C'. In our case, the vertices E and H are vertices in C that are incident with an edge not in C. Arbitrarily choose one of these, say E, and construct a path P from E to E in the same manner that C was constructed. One possibility for P is P: E,c,B,a, A, b,D, f,E. We now enlarge C to include the path P by replacing any one occurrence of E in C by P. For example, if we replace the first occurrence of E in C, we obtain C': G, h, E, c, B, a, A, b, D, f, E, d, C, e, F, g, E, i, H, k, G. The edges in the enlarged pari C' are shown in color in Figure 3.20(c). Notice that C', although larger than C, is still not an Euler circuit. We now repeat the procedure used in the preceding paragraph. In this case H is the only vertex in C' that is incident with an edge not in C'. So we construct a path P' from H to H, say P': H, m, J, 1, H. Enlarging C' as before to include this path P', we obtain the Euler circuit
G, h,E,c, B,a, A, b,D. f E, d,C,e,F, g,E, i,H,m, J,1,H, k,G. + The process illustrated in Example 3.12 always produces an Euler circuit in a connected multigraph in which the degree of each vertex is even. A formal description of this procedure is given below.
Euler Circuit Algorithm This algorithm constructs an Euler circuit for a connected multigraph g in which every vertex has even degree. Step I
(start path) (a) Set £ to be the set of edges of 5. (b) Select a vertex U, and set C to be the path consisting of just U. Step 2 (expand the path) while £ is nonempty Step 2.1 (pick a starting point for expansion) (a) Set V to be a vertex in C that is incident with some edge inS. (b) Set P to be tLe path consisting of just V. Step 2.2 (expand P into a path from V to V) (a) Set W = V. (b) while there is an edge e on W in £ (a) RemDve e from E. (b) Replace W with the other vertex on e. (c) Apperd edge e and vertex W to path P.
endwhile
3.2
Pathsand Circuits
125
Step 2.3 (enlarge C) Replace any one occurrence of V in C with path P.
endwhile Step 3
(output) The path C is an Euler circuit.
The following theorem gives necessary and sufficient conditions for a connected multigraph to have an Euler circuit or path and justifies the Euler circuit algorithm. Theorem 3.5
Suppose a multigraph g is connected. Then g has an Euler circuit if and only if every vertex has even degree. Furthermore, g has an Euler path if and only if every vertex has even degree except for two distinct vertices, which have odd degree. When this is the case, the Euler path starts at one and ends at the other of these two vertices of odd degree. Proof We will give a proof only in the case that 5 contains no loops. An easy modification establishes the result when there are loops. Suppose the multigraph g has an Euler circuit. Every time this Euler circuit passes through a vertex, it enters along an edge and leaves along a different edge. Since every edge is used in an Euler circuit, every edge through a vertex can be paired as one of two, either coming in or going out. Thus each vertex has even degree. Conversely, suppose each vertex has even degree. The Euler circuit algorithm constructs a path starting at V. This path must return to V since when we enter a different vertex along one edge, another edge must leave the vertex because its degree is even. Thus a V-V path is constructed. The algorithm proceeds by starting along unused edges on vertices on this path. Since the multigraph is connected, there always exists a path from any unused edge to the path already constructed; so each edge is eventually included, and an Euler circuit is formed. If an Euler path exists between distinct vertices U and V, then clearly the degrees of U and V must be odd, while all the other vertices have even degree. Conversely, if only U and V have odd degrees in a connected multigraph, we can add an edge e between U and V. The new multigraph will have all degrees even, and so an Euler circuit will exist for it by what we have already proved. Removing e produces an Euler path between U and V. 8 From the last paragraph of the proof of Theorem 3.5, we see that the Euler circuit algorithm may be used to find an Euler path in a connected multigraph with exactly two vertices of odd degree by applying it to the multigraph formed by adding an edge between these two vertices.
126
Chapter3
Graphs
+
Example 3.13 For the multigraph in Figure 3.21 (a), an edge e is added between the two vertices U and V of odd degree. This results in the multigraph in Figure 3.21 (b) for which an Euler circuit, say e, a, d, c, b, can be found by using the Euler circuit algorithm. Deleting the edge e from this circuit gives the Euler path a, d, c, b between U and V for the multigraph in Figure 3.21(a). + e V
U
A
A
(a l
(b) FIGURE 3.21
In analyzing the complexity of the Euler circuit algorithm, we will use picking an edge as an elementary operation. Since each of the e edges is used once, this algorithm is of order at most e. For a graph with n vertices, n(n-l)= (n 2 n) 2 2 because C(n, 2) = 2n(n - 1) is the number of pairs of distinct vertices. (See Theorem 2.12.) Thus, for a graph with n vertices, this algorithm is of order at most n2 . e<
k andh i
nian 'e
V( lr4es . ;:
Pa th: eXs
In Example 3.1 a graph was used to describe a system of nonstop flights. Suppose that a salesperson needs to visit each of the cities in this graph. In this situation time and money would be saved by visiting each city exactly once. What is needed for an efficient scheduling is a path that begins and ends at the same vertex and uses each vertex once and only once. In the first part of this section, paths that used each edge once and only once were considered. Now we want to find a cycle that uses each vertex of a multigraph exactly once. But since we want to avoid repetition of vertices, loops and parallel edges will not be of any assistance. Consequently, we may assume that we are working with a graph. In a graph, a Hamiltonian path is a path that contains each vertex once and only once, and a Hamiltonian cycle is a cycle that includes each vertex. These are named after Sir William Rowan Hamilton, who developed a puzzle where the answer required the construction of this kind of cycle.
3.2 Paths and Circuits
+
127
Example 3.14 Suppose the graph in Figure 3.22 describes a system of airline routes where the vertices are towns and the edges represent airline routes. The vertex U is the home base for a salesperson who must periodically visit all of the other cities. To be economical, the salesperson wants a path that starts at U, ends at U, and visits each of the other vertices exactly once. A brief examination of the graph shows that the edges a, b, d, g, f, e form a Hamiltonian cycle. a U.
e
W
FIGURE 3.22
For the graph in Figure 3.1 there is no Hamiltonian cycle. The only way to reach New York or St. Louis is from Chicago, and once in New York or St. Louis the only way to leave is to return to Chicago. + Relatively easy criteria exist to determine if there is an Euler circuit or an Euler path. All that has to be done is to check the degree of each vertex. Furthermore, there is a straightforward algorithm to use in constructing an Euler circuit or path. Unfortunately, the same situation does not hold for Hamiltonian cycles and paths. It is a major unsolved problem to determine necessary and sufficient conditions for a graph to have either of them. In general, it is very difficult to find a Hamiltonian cycle for a graph. There are, however, some conditions that guarantee the existence of a Hamiltonian cycle in a graph. We will provide an example of one of these theorems. Theorem 3.6
Suppose 5 is a graph with n vertices, where n > 2. If for each pair of nonadjacent vertices U and V we have deg(U) + deg(V) > n,
(3.1)
then 5 has a Hamiltonian cycle. Proof. We will give a proof by contradiction. Suppose there exist graphs such that every pair of nonadjacent vertices satisfies (3.1), but which have no Hamiltonian cycle. Among all such graphs with n vertices, let 5 be one with a maximal number of edges. Then if any edge is added to g, the new graph has
a Hamiltonian cycle. Because 5 has no Hamiltonian cycle, 5 is not a complete graph, and so has nonadjacent vertices U and V. Let g' be the graph formed by
adding the edge {U, V} to 9.
128
Chapter3
Graphs
By assumption g' has a Il amiltonian cycle, and in fact every Hamilton cycle of g' contains the edge {U, V). Removing this edge from such a cycle leaves a Hamiltonian path U -=U 1 , U2 ,
Un = V
in g. We claim that for 2 < j n, if the edge {UI, Uj} is in g, then the edge {Uj-, Uj is not. For if both these edges were in g, then g would have the Hamiltonian cycle U1, Uj,
.
Un, Uj-1, Uj-2, 2.
. U 1,
contrary to assumption. (See Figure 3.23.) Ul
U2
Un
--- -----
FIGURE 3.23 Now let d and d' be the respective degrees of U1 and Un in 9. Then there are d edges from U1 to vertices Uj with 2 < j < n. This gives d vertices Uj1 , < j 1-< n - 1, not adjacent to U. Thus d' < (n -1) -d, and so d + d' < n -1, contradicting (3.1). It Example 3.15 It follows from Theorem 3.6 that if, in a graph with n vertices, the degree of each vertex is at least n2 then the graph must have a Hamiltonian cycle. Thus the graph in Figure 3.24(a) has a Hamiltonian cycle because there are 6 vertices, each with degree 3. However, even though the theorem says there is a Hamiltonian cycle, it does not tell us how to find on-. Fortunately in this case, one can be found by a little bit of trial and error.
(a)
(b) FIGURE 3.24
(c)
3.2 Paths and Circuits
129
On the other hand, the graph with 5 vertices in Figure 3.24(b) does not have a Hamiltonian cycle because, no matter where we start, we end up on the left side needing to go to a vertex on the left side, and there are no edges connecting the vertices on that side. Note that this graph does not satisfy the conditions of Theorem 3.6 because, among its 5 vertices, there are nonadjacent vertices of degree 2, so that (3.1) is not satisfied. On the other hand, the graph in Figure 3.24(c) also has 5 vertices, each with degree 2; yet it contains a Hamiltonian cycle. Thus, when (3.1) fails for some pair of nonadjacent vertices, it is not possible to conclude anything in general about the existence or nonexistence of a Hamiltonian cycle. + Example 3.16 There are several instances in which it is necessary to list all n-bit strings (a sequence of n symbols, each being a zero or one) in such a way that each n-bit string differs from the preceding string in exactly one position and the last n-bit string also differs from the first string in exactly one position. This kind of listing is called a Gray code. For n = 2, the listing 00, 01, 11, 10 is a Gray code, but 00, 01, 10, 11 is not because string 2 and string 3 differ in more than one position, as do strings 1 and 4. One way a Gray code is used is in determining the position of a circular disc after it stops rotating. In this situation a circular disc is divided into 2n equal sectors, and an n-bit string is assigned to each sector. Figure 3.25 shows an assignment of the 3-bit strings to a disc divided into 23 = 8 sectors.
FIGURE 3.25
To determine which n-bit string is to be assigned, the circular disc is divided into n circular rings. Thus each sector is subdivided into n parts, each of which is treated in one of two ways: as, for example, opaque or translucent. Under the rotating disc are placed n electrical devices, such as photoelectric cells, that can determine what type of treated material is above it. Figure 3.26(a) illustrates how this can be done for the assignment in Figure 3.25, where the electrical device below a shaded region will send a 1, the device below an unshaded region will send a 0, and the regions are read from the outside to the center.
130
Chapter3
Graphs
(a)
(b)
(c)
FIGURE 3.26
In Figure 3.26(b) the rotating disc has stopped so that the three electrical devices are completely contained within one sector. In this case the electrical devices will send 0 0 1, which indicates the sector in which the disc stopped. However, if the (lisc stopped as in Figure 3.26(c), there is a possibility for an incorrect reading. This time the innermost device could send either 0 or 1, as could the other two devices. Because of this, any of the eight possible 3-bit strings could be s-nt when describing the location of the disc in Figure 3.26(c). To minimize this type of error, we want two adjacent sectors to be assigned n-bit strings that differ in exactly one position. Then, no matter whether 0 or 1 is sent from the parts of the adjacent sectors, there will be only two possible strings that could be sent to describe the location of the disc, and these two strings will identify the two adjacent sectors where the disc stopped. To find a Gray code, we construct a graph using 2' vertices representing the 2' possible n-bit strings. Tvo vertices are connected with an edge if the corresponding n-bit strings differ in exactly one position. It can be proved that a graph constructed in this way always has a Hamiltonian cycle, and so this can be used to find a Gray code for any n. See Figure 3.27 for the case n = 3, where a Hamiltonian cycle is indicated by the colored edges. Thus when n = 3, one Gray code is 000, 001, 011, 010, 110, 111, 101, 100. The interested reader should consult suggested reading I I L] for more details. +
111
II0l
1o0
000 FIGURE 3.27
001
3.2 Paths and Circuits
131
EXERCISES 3.2 In Exercises 1-4 determine if the multigraph is a graph. 0
1. a
2.
.
4.
3.
In Exercises 5-S list the loops and paralleledges in the multigraph. 5.
C
a
6.
_ a
7.
8.
a
b a
b:
m
.
d
In Exercises 9-10 (i) (ii) (iii) (iv)
List at least 3 different pathsfrom A to D. Give the length of each. List the simple pathsfrom A to D. Give the length of each. For each path you listed in (i), find a simple path from A to D contained in it. List the distinct cycles. (Two cycles are distinct if there is an edge in one that is not in the other.) Give the length of each.
9.
A10.
a
A b
b
ae
C
d
D
11. Give examples of multigraphs satisfying each of the following conditions. (a) There are exactly 2 cycles. (b) There is a cycle of length 1. (c) There is a cycle of length 2.
132
Chapter3 Graphs
In Exercises 12-17 determine if the multigraph is connectJ.
12.
0
d
13.
0
14-5W
15.
16.
17.
e
In Exercises 18-23 determine if the multigraph has an Eul.?rpath. If it does, construct one using the Euler circuit algorithm as in Example 3.13.
18.
19.
20.
I21.
m
22.
23.
a
b
C
di
S V
t
U W
3.2 Paths andCircuits
133
In Exercises 24-29 determine if the multigraph in the indicatedexercise has an Euler circuit. If it does, construct an Euler circuit using the Euler circuit algorithm. 24. Exercise 18 27. Exercise 21
25. Exercise 19 28. Exercise 22
26. Exercise 20 29. Exercise 23
30. The city of Konigsberg, located on the banks of the Pregel River, had seven bridges that connected islands in the river to the shores as illustrated below. It was the custom of the townspeople to stroll on Sunday afternoons and, in particular, to cross over the bridges. The people of Kbnigsberg wanted to know if it was possible to stroll in such a way that it was possible to go over each bridge exactly once and return to the starting point. Is it? (Hint: Consider carefully what a vertex is to represent.) (This problem was presented to the famous mathematician, Leonhard Euler, and his solution is often credited with being the beginning of graph theory.)
31. 32. 33. 34.
Could the Could the Could the Could the
citizens citizens citizens citizens
of Konigsberg of Konigsberg of Konigsberg of Konigsberg
find an acceptable find an acceptable find an acceptable find an acceptable
route route route route
by building a new bridge? If so, how? by building two new bridges? If so, how? by tearing down a bridge? If so, how? by tearing down two bridges? If so, how?
An old childhood game asks children to trace afigure with a pencil without either lifting the pencilfrom the figure or tracing a line more than once. Determine if this can be done for the figures in Exercises 35-38, assuming that you must begin and end at the same point. 35.
36.
37.
38.
134
Chapter 3
Graphs
39. The following graph has 4 vertices of odd degree, and :hus it has no Euler circuit or path. However, it is possible to find two distinct paths, one from A to B and the other from C to D, that use all the edges and have no edge in common. Find two such paths. (See Exercise 63.)
40. In 1859 Sir William Rowan Hamilton, a famous Irish rn.athematician, marketed a puzzle which consisted of a regular dodecahedron made of wood in which each corner represented a famous city. The puzzle was to find a route that traveled along the edges of the dodecahedron, visited each city exactly once, and returned to the original starting city. (To make the task somewhat easier, each corner had a nail in it and one was to use string while tracing out a path.) A representation of this puzzle drawn in the plane is given below. Can you find an answer to the puzzle? 2
5
4
41. Give examples of connected graphs satisfying each set of conditions. (a) There is both an Euler circuit and a Hamiltonian cycle. (b) There is neither an Euler circuit nor a Hamiltonian cycle. (c) There is an Euler circuit but not a Hamiltonian cycle. (d) There is a Hamiltonian cycle but not an Euler circt it. (e) There is a Hamiltonian path but not a Hamiltonian cycle. 42. Construct a Gray code for n
=
4.
43. Draw all the nonisomorphic multigraphs having 5 vertices of degrees 1, 1, 2, 3, and 3. 44. Draw all the nonisomorphic multigraphs having 4 vertices of degrees 1, 2, 3, and 4. 45. Draw all the nonisomorphic multigraphs having 6 vertices of degrees 1, 1, 1, 2, 2, and 3. 46. Draw all the nonisomorphic multigraphs having 5 vertices of degrees 1, 2, 2, 2, and 3. 47. Is the property 'has a cycle of length n' a graph isomorphism invariant? Justify your answer. 48. Is the property 'has all vertices with even degrees' a graph isomorphism invariant? Justify your answer. 49. Is the property 'has a Hamiltonian path' a graph isomcrphism invariant? Justify your answer.
3.2 Pathsand Circuits
135
50. Are the following two graphs isomorphic? Justify your answer.
51. Are the following two graphs isomorphic? Justify your answer.
52. A bipartite graph is a graph in which the vertices can be divided into two disjoint nonempty sets A and B such that no two vertices in A are adjacent and no two vertices in B are adjacent. The complete bipartite graph ICmn is a bipartite graph in which the sets A and B contain m and n vertices, respectively, and every vertex in A is adjacent to every vertex in B. The graph IC2 3 is given below. How many edges does Km, have?
53. For which m and n does kImn have an Euler circuit? 54. For which m and n does KCm,n have a Hamiltonian cycle? 55. Prove that C, has a Hamiltonian cycle when n > 2. 56. In a multigraph with n vertices, what is the maximum length of a simple path? 57. Show that the relation 'there is a path from vertex V to vertex U' is an equivalence relation on the set of vertices of a graph. The vertices in an equivalence class of this relation along with the edges joining them form a component of the graph. 58. Find the components of the following graphs. (See Exercise 57.) (a)
1-1
(b) *
A
' '
G
!
H
(c) J
K
L
M
N
0
P
0
I
59. Find the components of the following graphs. (See Exercise 57.) (b) I
(a
G
H
V
11
J
I
(c)
136
Chapter 3
Graphs
60. A dog show is being judged from pictures of the (logs The judges would like to see pictures of the following pairs of dogs next to each other for their final decis on: Arfie and Fido, Arfie and Edgar, Arfie and Bowser, Bowser and Champ, Bowser and Dawg, Bowser and[ Edgar, Champ and Dawg, Dawg and Edgar, Dawg and Fido, Edgar and Fido, Fido and Goofy, Goofy and Dawvg. (a) Draw a graph modeling this situation. (b) Suppose that it is necessary to put pictures of the dogs in a row on the wall so that each desired pair of pictures appear together exactly once. (There are many copies of each picture.) What graph-theoretic object is being sought? (c) Can the pictures be arranged on the wall in this manner? If so, how? 61. At a recent college party there were a number of young men and women present, some of whom had dated each other recently. This situation can be represented by a graph in which the vertices are the individuals in attendance with adjacency being defined by having dated recently. If this graph has a Hamiltonian cycle, show that the number of men is the same as the number of %A omen. 62. Prove Theorem 3.4 by using mathematical induction. 63. Suppose a connected multigraph has the property that exactly four of its vertices have odd degree. Prove that there are two paths, one between two of these vertices and the other between the remaining two vertices, such that every edge is in exactly one of these two paths. 64. In a graph, prove that if there is a U-U path of odd ]entIh for some vertex U, then there is a cycle of odd length. 65. Prove that if a connected graph has n vertices, then it must have at least n - I edges.
3.3
+
SHORTEST PATHS AND DISTANCE In this section we will consider ways to find a shortest path between vertices in a graph. The need to find such paths arises in many different situations. We want to find a path of minimal length between two vertices S and T, that is, a path from S to T that has the fewest possible edges. This smallest possible number of edges in a path fro:m S to T is called the distance from S to T. To find the distance from S to T, the general approach is to look first at S, then at the vertices adjacent to S, then at the vertices adjacent to these vertices, and so forth. By keeping a record of the way in which vertices are examined, we are able to construct a shortest path from S to T. To find the distance from S to every vertex T for which there is a path froin S to T, we assign labels to some of the vertices in the graph. If a vertex V is assigned the label 3(U), then the distance from S to V is 3, and U is the predecessor of V on a shortest path from S to V (that is, a shortest path from S to V curtains the edge {U, V}).
+
Example 3.17 For the graph in Figure 3.28, let us find the distance from S to each vertex for which there is a path from S. We begin by assigning S the label 0(-), which signifies that the distance front S to S is 0 and that there are no edges on this
137
3.3 ShortestPathsandDistance
path. Next, we determine the vertices with distance 1 from S. These are A and B, which are both assigned the label I(S), as shown in Figure 3.29.
A
E
K
F
I(S) A
L 0O S
S
E
K
F
L
I
C
M B
D
G
M
J
B
D
I(S)
FIGURE 3.28
G
J
FIGURE 3.29
Having assigned labels to the vertices with distance I from S, we now determine the vertices with distance 2 from S. These are the vertices that are unlabeled and adjacent to a vertex whose distance from S is 1. For example, the unlabeled vertices C and E are adjacent to A, and so they are assigned the label 2(A). Likewise, the unlabeled vertex D is adjacent to B and so is given the label 2(B). The labels now appear as in Figure 3.30. I (S) 2(A)
I(S) 2(A) A
E
F
K
A
M
0(
S(0(C)H B D I (S) 2(B)
z G FIGURE 3.30
3(E) F
E
O( -=)HI 2(A)
K
1.
(C) 4(H)
I M J
B D I(S) 2(B)
G 3(D)
J 4(G)
FIGURE 3.31
We continue in this manner until no labeled vertex is adjacent to an unlabeled vertex. If every vertex in the graph is labeled when this occurs, then the graph is connected. Otherwise, there is no path from S to any unlabeled vertex. For the graph in Figure 3.28, vertices A through J and S are eventually labeled as in Figure 3.31. At this point, we stop because no labeled vertex is adjacent to an unlabeled vertex. Note that there is no path from S to any of the unlabeled vertices (K, L, or M). The label assigned to any labeled vertex gives its distance from S. For example, since the label assigned to I is 4(H), the distance from S to I is 4. Also, the predecessor of I is H, which means that a shortest path from S to I includes the edge {H, I}. Similarly, the predecessor of H is C, the predecessor of C is A, and the predecessor of A is S. Thus a shortest path from S to I includes the edges {H, 11, {C, H}, {A, C}, and {S, A), and so a shortest path from S to I is S, A, C, H, I. In this graph, another shortest path from S to I exists, namely S, B, C,
138
Chapter 3
Graphs
H, L. Which path is found depends on whether vertex C is labeled because it is adjacent to A or to B. + Here is a formal description of this process.
Breadth-First Search Algorithm This algorithm determines the d.stance and a shortest path in a graph from vertex S to every other vertex for which there is a path from S. In the algorithm, L denotes the set of labeled vertices, and the predecessorof vertex A is a vertex in L that is used in labeling A. Step ]
Step 2
Step 3
(label S) (a) Assign S the label 0, and let S have no predecessor. (b) Set L = {SI and k =O. (label vertices) repeat Step 2.1 (increase the label) Replace k with k + 1. Step 2.2 (enlarge labeling) while L contain': a vertex V with label k - 1 that is adjacene io a vertex W not in L (a) Assign the label k to W. (b) Aassign V to be the predecessor of W. (c) Include W in L. endwhile until no vertex in L is adjacent to a vertex not in L (construct a shortest path to a vertex) if a vertex T is in L' The label on 7' is its distance from S. A shortest path from S to T is fo med by taking in reverse order T, the predecessor of T the predecessor of the predecessor of T, and so forth, unti[ S is reached. otherwise There is no patL from S to T. endif
It can be shown that the label assigned to each vertex by the breadth-first search algorithm is its distance from S (see Exercise 18). We will regard labeling a vertex and using an edge to find an adjacent vertex as the elementary operations in analyzing this algorithm. For a graph with n vertices and e edges, each vertex is labeled exactly once and each edge is used at most
3.3
Shortest Pathsand Distance
139
once to find an adjacent vertex. Hence there will be at most n + e elementary operations. But since n+e
Frequently when graphs are used to describe relationships between objects, a number is associated with each edge. For example, if a graph is being used to represent a highway system in the usual way, then a number can be assigned to each edge indicating the mileage between the two cities. This idea of assigning numbers to the edges is a very important one in applications. A weighted graph is a graph in which a number called the weight is assigned to each edge. The weight of a path is the sum of weights of the edges in the path. When a weighted graph describes a highway system with vertices representing cities and weights representing mileage between cities, the weight of a path is simply the total mileage between the cities representing the start and end of the path. Example 3.18 The graph in Figure 3.32 is a weighted graph since each edge has a number assigned to it. For example, the weight of the edge on A and B is 3 and the weight oftheedgeonDandFis5.TheweightofthepathA,C,D,Fis4+2+5 = 11, andtheweightofthepathF,D,B,E,Dis5+1+2+1 =9. + B
2
C
E
F FIGURE 3.32
In many applications we need to find a path of smallest weight. However, there need not always be one. This kind of situation can occur if there is a cycle with negative weight. +
Example 3.19 For the weighted graph in Figure 3.33, the path A, B, D, E has weight 2, and the path A, B, D, C, B, D, E has weight -2, which is a smaller weight than
140
Chapter 3 Graphs
that of the first path. Note that -As the cycle B, D, C is repeated, the weight of the path gets smaller and smaller. Thus there is no path of smallest weight between A and E. +
A *--
D FIGURE 3.33
Consequently, we shall assume, unless explicitly stated otherwise, that weighted graphs do not have a cycle with negative weight. This assumption assures the existence of a path of smallest weight between two vertices if there is any path between them. Furthermore, a path of smallest weight between two vertices may be assumed to be simple since any cycle of weight 0 could be removed as in Theorem 3.4. A path of smallest weight is called a shortest path between those two vertices, and the weight of that path is called the distance between them. When the weights assigned to edges are positive, as is the case with highway or airline mileage, there is an a- gorithm that finds the distance and a shortest path between two vertices S and T In fact, it can be used to find the distance and a shortest path between S and al Iother vertices at the same time. The idea of this algorithm is to find the vertex closest to 5, then the second closest vertex to S, and so forth. In this way we can find the distance between S and all other vertices. In addition, if we keep a record of the vertices used in determining distances, it is possible to find a shortest path from S to any other vertex. This algorithm is due to E. Dijkstra, one of the pioneers in computer science.
Dijkstra's Algorithm Let 0be a weighted graph in w hich there is more than one vertex and all weights are positive. This algorithm determines the distance and a shortest path from vertex S to every other vertex in g. In the a lp orithm, P denotes the set of vertices with permanent labels. The predecessor of verleK A is a vertex in P used to label A. The weight of the edge on vertices U and V! Ls denoted hy W(U. V). and if there is no edge on U and V, we write W(U, V) =: Do) Step]I
Step 2
(label 5) (a) Assign S the label 0, and let S have no predecessor. (b) Set P-={SI. (label vertices) Assign to each vertex V1not in P the (perhaps temporary) label W (S, V), and let V have the (perhaps temporary) predecessor S.
3.3 Shortest Paths and Distance
Step 3
Step 4
141
(enlarge P and revise labels) repeat Step 3.1 (make another label permanent) Include in P a vertex U having the smallest label of the vertices not in P. (If there is more than one such vertex, arbitrarily choose any one of them.) Step 3.2 (revise temporary labels) For each vertex X not in P that is adjacent to U, replace the label on X with the smaller of the old label on X and the sum of the label on U and W(U, X). If the label on X was changed, let U be the new (perhaps temporary) predecessor of X. until P contains every vertex of 5 (find distances and shortest paths) The label on a vertex Y is its distance from S. If the label on Y is xc, then there is no path, and hence no shortest path, from S to Y. Otherwise, a shortest path from S to Y is formed by using in reverse order the vertices Y, the predecessor of Y, the predecessor of the predecessor of Y, and so forth until S is reached.
The proof that this algorithm actually computes the distance between S and every other vertex can be found in Exercises 21-24. In analyzing this algorithm for a graph with n vertices, we will consider assignments involving one vertex as being just one operation. So in step 1 there is just one operation, and in step 2 there are n - 1 more. Step 3 is done n - 1 times. Each time at most n - 2 comparisons are done on the labels to find the smallest one, and then at most one assignment occurs. Also, in revising the labels, we examine at most n - 1 vertices, each of which requires an addition, a comparison, and two possible assignments for a total of 4 operations. So for step 3 there are at most (n- 1)[n - 2 + 1 + 4(n - 1)] = (n -1)(5n - 5) operations. In step 4, looking up the distance and tracing back at most n - I predecessors to find a shortest path takes at most n operations. From this, we see that there are at most
+ (n - 1) + (n - l)(5n - 5) + n =Sn 2 operations, and so the algorithm is of order at most n2. A
2
D
FIGURE 3.34
C
-
8n + 5
142
Chapter3
Graphs
+
Example 3.20 For the weighted graph in Figure 3.34 we want to find a shortest path and the distance from S to every other vertex. In step 1 we set P = {S} and assign S the label 0. We indicate this on the graph by writing the label and predecessor (in parentheses) beside S. We use an asterisk to denote that S is in P. The graph now looks like that in Figure 3.35. Next, in step 2, we assign the label W(S, V) and the predecessor S to every other vertex V. Recall that VWA.S, V) = oc when there is no edge joining S and V. The graph now looks like that in Figure 3.36. 3(S) A
2
oo(S) C
5)
FIGURE 3.35
FIGURE 3.36
Now we perform step 3. Tiie vertex not in P with the smallest label is B, and so we include B in P. The vertices not in P and adjacent to B are A, C, and D; and we replace the label on each such vertex X by the minimum of the old label and the sum of the label on E and W(S, B). These numbers are as follows. VertexX
Old Lbel
(Label on B) + W(B, X)
Minimum
A
3
I +1 =2
2
C
x]
1+3 =4
4
D
x
1 +5=6
6
Since each label is changed, wve also replace the predecessor of each of these vertices by B, producing Figuie 3.37. 2(B)
4(B)
A
2
/
3
C
3 /I3 1. 0(--)
B* I(S)
E
1 (S)
S
D 6(B)
FIGURE 3.37
3.3 Shortest Paths andDistance
143
We continue in this way until P contains every vertex in the weighted graph. The following table shows the labels, predecessors, and vertices added to P at each stage. No entry in a column indicates no change from the previous stage. Labels and Predecessors Vertex
S
A
B
C
D
E
Vertex Added to P
0(-)
3(S)
1(S)
-o(S)
0o(S)
-o(S)
S
4(B)
6(B)
2(B)
B A
5(C)
7(C)
C
6(D)
D E
The final graph is shown in Figure 3.38. In this figure the label on each vertex gives the distance between it and S, and a path of this length can be found by backtracking through the predecessors of the vertices. For example, the distance from S to E is 6, and the path S, B, C, D, E has this length. + 2(B)
4(B)
S*4 of-)
FIGURE 3.38
We conclude this section by considering the number of paths between two vertices, or, alternatively, how many paths of length m there are between a pair of vertices. One answer to these questions involves powers of the adjacency matrix of the graph. Theorem 3.7
For a graph 5 with vertices labeled VI, V2 , . . , V, and adjacency matrix A, the number of paths of length m from Vi to Vj is the i, j entry of Am . Before we show how the theorem is proved for m = 1, 2, and 3, we present an example to illustrate the theorem.
144
Chapter 3 Graphs V4
V2
XX
A= [I°
(a)
[]0
I
(b) FIGURE 3.39
+
Example 3.21 The graph in Figure 3.39(a) has the adjacency matrix A given in Figure 3.39(b). To find the number of pa-hs of length 2, we compute the product 0
A 2= AA I -1
1 1-1
1)
l
IC I('
O
0 1
1
1
0 1 1 -12 0-
1 1
1 0 0 1 0
-
-
3 1 1 l
2 2
2 2
That the 3, 4 entry is 2 means there are 2 paths of length 2 between V3 and V4, namely, V3, VI, V4 and V 3 , 1/2, V4. Likewise the 1, 3 entry being 1 means there is only one path of length 2 betwe-en VI and V3, namely, VI, V2, V3. The number of paths of length 3 is given by the product A2 . A = A3 computed below.
A3 = A2A =
-3 2 1 1- - 1 1 12 3 1 1 1 0 1 1 =
4 5 5 5~ 5 4 5 5
l1 l 2 2- -1 I 0 0-
-5 5 2 2-
Since the 1, 2 entry of A3 is ';, there are 5 paths of length 3 between VI and V2, namely, VI, V2, VI, V2; VI, V, V4, V2; VI, V2, V3, V2; VI, V3, VI, V2; and VI, V4, VI, Y2.
+
We now give the proof of Theorem 3.7 for m = 1, 2, and 3. Let aij denote the i, j entry of A. The number of paths of length 1 between Vi and Vj is either o or 1 depending on whether there is an edge joining these vertices. But this is the same as aij, which is 1 when there is an edge joining Vi and Vj and 0 otherwise. So the i, j entry of A gives the number of paths of length 1 from Vi to V1. For a path of length 2 between Vi and Vj, there needs to be a vertex Vk such that there is an edge joining 1'i and Vk and an edge joining Vk and Vj. In terms of the adjacency matrix, this is the same as saying that there is an index k such that
3.3 Shortest Paths andDistance
145
both aik and akj are 1, or equivalently, aikaki = 1. Thus the number of paths of length 2 between Vi and Vj is the number of k's where aikakj = 1. This number is the value of aila1 j + ai2a2 j +
+ ainanj
since each term in the sum is 1 or 0. But this sum is also the i, j entry in A 2, the product of A with A, and thus the i, j entry in A2 is the number of paths of length 2 between Vi and Vj. For a path of length 3 between Vi and Vj, there are vertices Vp and Vk with edges joining Vi and Vp, joining Vp and Vk, and joining Vk and Vj. But this means there is a path of length 2 between Vi and Vk and an edge on Vk and Vj. If bik denotes the i, k entry of A 2, then the number of paths Vi, Vp, Vk, Vj of length 3 between Vi and Vj is bikakj. Thus the total number of paths of length 3 between Vi and V1 is the value of bilaij + bi2 a2 j +
-±+ binan
which is the same as the i, j entry in A2 . A = A3 . Hence the i, j entry in A3 is the number of paths of length 3 between Vi and Vj. The proof of the general case of Theorem 3.7 is left as an exercise; the last paragraph suggests what is to be done for the inductive step.
EXERCISES 3.3 In Exercises 1-4 use the breadth-firstsearch algorithm to determine the distance and a shortest pathfrom S to T in the graph. Use alphabeticalorder when there is a choice for a predecessor.
B A
2. C
S
E
G
F
Hf
T
I
-
.
-
146
Chapter3 Graphs
3.
S
A
B
C
4,
S
A
B
E
OF
I
J
C
G
H~ L
K
N
OM
Q
V
U
D
VI
R
W
T
In Exercises 5-8 determine the distancefrom S to all the cther vertices in the weighted graph.Find a shortest path from S to A.
5.
C
2
D
6.
A
5
2
-
FI
32
E4
2
E
A
D
H
FN 01
1 4
S
5
C
1
2
3
S-
I
B
3H
J
K
In Exercises 9-12 find a shortest pathfrom S to T that gses through the vertex A in the weighted graph. Explain your procedure.
9.
S
8
B
2 6
4
6 1
5 J
A
C
7 1
1 K
2
D
2 1
3 L
7
E 2
3 M
C'0,
T
3
B
F
4
C
A
1
2
G
H
3.3 Shortest Paths andDistance
12.
11.
B
H
I
C
I
D
I
8
I
147
5
Use Theorem 3.7 to solve Exercises 13-16. 13. For the graph below, determine the number of paths of lengths 1, 2, 3, and 4 from VI to V2 , and from V2 to V3 .
14. For the graph below, determine the number of paths of lengths 1, 2, 3, and 4 from VI to V2 , and from VI to V3 .
15. For the graph below, determine the number of paths of lengths 1, 2, 3, and 4 from V, to VI, and from V4 to V3 . V,
V2
148
Chapter 3
Graphs
16. For the graph below, determine the number of paths of lengths 1, 2, 3, and 4 from VI to V3 , and from V2 to V 4.
V4
17. If A is the adjacency matrix of a labeled graph
V5
G, what does the i, j element of A + A2 + A 3 describe?
18. Prove that the label given to each vertex by the breadth-first search algorithm is its distance from S. 19. Prove Theorem 3.7 using mathematical induction. 20. Two weighted graphs are said to be isomorphic wher there is an isomorphism between the underlying graphs such that the edges joining corresponding vertices have the same weight. Give an example of two nonisomorphic weighted graphs where the underlying graphs are isoinorphic. Exercises 21-24 provide a proofof the validity of'Dijkstra's algorithm. Assume in them that 5 is a weighted graph with all weights W(U, V) positive, and that S is a vertAx Of 5. 21. Suppose each vertex V of g is assigned a label L(V), which is either a number or no. Assume that P is a set of vertices of 5 containing S such that (i) if V is in P, then L(V) is the length of a shortest path from S to V and (ii) if V is not in P, then L(V) is the length of a shortest path from S to V subject to the restriction that V is the only vertex of the path not in 'P. Let U be a vertex not in P with minimal label among such vertices. Show that a shortest path from S to U contains no element nct in P except U. 22. Show that under the assumptions of Exercise 21 the length of a shortest path from S to U is L(U). 23. Assume the hypotheses of Exercise 21, and let P' be the set formed by U and the elements of P. Show that P' satisfies property (i) of Exercise 21, and show that if V is not in P', then the length of a shortest path from S to V, all of whose vertices except V are in P', is the rrinimum of L(V) and L(U) + W(U, V). 24. Prove that Dijkstra's algorithm gives the length of a shortest path from S to each vertex of 5 by mathematical induction on the number of elements in P. Let the induction hypothesis be that 'P is a set of vertices containing S and satisfying properties (i) and (ii) of Exercise 21.
3.4
o
COLORING A GRAPH In Sections 3.1, 3.2, and 3.3 we discussed several situations described by graphs or multigraphs. Sometimes the situation in which a graph can be used is somewhat unexpected. Two such examples follow.
+
Example 3.22 Suppose that a chemical manu Fa cturer needs to ship a variety of chemical products from a refinery to a processing plant. Shipping will be by rail, but according to
3.4
Coloringa Graph
149
EPA regulations not all of these chemical products can be shipped together in one railroad car because of the possibility of their mixing together and creating a violent reaction should an accident occur. How can these products be shipped? In order to minimize expenses, the manufacturer wants to use the smallest possible number of railroad cars. What is this number? +
+
Example 3.23 The State Senate has a number of major standing committees with every senator on one or more of these. Each committee meets every week for an hour. Each senator must be able to attend each meeting of a committee he or she is on, and so no two committees can meet at the same time if they have a member in common. The Clerk of the Senate is responsible for scheduling these meetings. How should the Clerk schedule these committee meetings so that the senators can attend their major committee meetings and yet keep the number of meeting times as small as possible? +
In these examples there are objects (chemical products or committees) and relationships (cannot travel in the same railroad car or cannot meet at the same time) existing among them. Since this is the basic idea of a graph, it seems natural to describe each of these examples by a graph. In the first example the vertices are the chemical products, and an edge is drawn between two vertices whenever they represent chemical products that cannot be in the same railroad car. In the second example the vertices are the committees, and an edge is drawn between two vertices whenever some senator is on both of these committees. To illustrate this idea further, let us assume in Example 3.22 that there are six chemical products Pi, P2 , P3 , P4 , P5 , and P6 and that PI cannot ride in the same railroad car as P2 , P3 , or P4; P2 also cannot ride with P3 or P5; P3 also cannot be with P4; and P5 cannot be with P6. The graph that is described is found in Figure 3.40, where the vertices represent the six products and the edges join pairs of products that cannot ride together. The question still remains: What is the smallest number of railroad cars needed? In the graph in Figure 3.40, products represented by adjacent vertices are to be in different cars. For example, product PI could be in car 1. Then because PI and P2 are adjacent, a different car is needed for P2, say car 2. Since P3 is adjacent to both PI and P2 , another car is needed for P3, say car 3. But a new car is not needed for P4 ; car 2 can be used again. Likewise, for P5 a new car is not needed, as either car 1 or car 3 can be used. Let car 1 be chosen. Then for P 6, car 2 or car 3 can be picked, say car 2. The graph in Figure 3.41 shows how the vertices are labeled so that incompatible chemical products travel in different cars. Furthermore, because PI, P2 , and P3 are adjacent to each other, at least three different railroad cars must be used; so three is the smallest number of railroad cars that can be used.
150
Chapter3
Graphs
P2
P5
3
P4 FIGURE 3.40
FIGURE 3.41
We have assigned labels to the vertices of a graph so that adjacent vertices have different labels. This idea occurs frequently in graph theory, and for historical reasons the labels are called colors. To color a graph means to assign a color to each vertex so that adjacent vertices have different colors. Asking what is the smallest number of railroad cars needed in Example 3.22 is the same as asking what is the smallest number of colors needed to color the graph in Figure 3.40, with a color corresponding to a railroad car. When a graph can be colored with n colors but not with a smaller number of colors, it is said to have chromatic number n. Thus the graph in Figure 3.40 has chromatic number 3. +
Example 3.24 The graph in Figure 3.42(a) has chromatic number 2 since the vertices VI, V3, and V5 can be colored with one color (say red) and the other three vertices with a second color (blue), as shown in Figure 3.42(b). In general, if a cycle has an even number of vertices, then it can be colored using 2 colors. + VI red
V1
blue
V2 blue
red
V3 red
V4
V 4 blue
(a)
(b) FIGURE 3.42
3.4 Coloring a Graph
151
Example 3.25 When a cycle has an odd number of vertices, such as that in Figure 3.43(a), then 3 colors must be used. If we try to alternate colors, as was done in Figure 3.42, with the color red assigned to vertices VI and V3 and the color blue assigned to vertices V2 and V4, then it is not possible to use either red or blue for V5. Using 3 colors to color a cycle with an odd number of vertices is illustrated in Figure 3.43(b). + V,
V5
green
V4
blue
V2 blue
V3 red
(a)
(b) FIGURE 3.43
+
Example 3.26 The complete graph IC, with n vertices can be colored using n colors. But since every vertex is adjacent to every other vertex, a smaller number of colors will not work. Thus IC, has chromatic number n. +
+
Example 3.27 The graph in Figure 3.44(a) can be colored with 2 colors as indicated in Figure 3.44(b). + Vi red
V3
red
red red V4
V4 4
blue V6 4
H.,>
U
(b)
(a) FIGURE 3.44
152
Chapter 3
Graphs
*
Example 3.28 The graph in Figure 3.45 has chromatic number 2 since the vertices on the left can be colored with one color and the vertices on the right can be colored with a second. * v V4
V, V5 V FIGURE 3.45
In general, it is very difficult to find the smallest number of colors needed to color a graph. One method is to list all the different ways to assign colors to the vertices of a graph, then gc through these ways one at a time to see which of them is a coloring, and then finally determine which colorings have the smallest number of colors. Unfortunate ly, even if the graph has a relatively small number of vertices, this becomes an extraordinarily time-consuming process, measured in centuries rather than minutes even with the use of a supercomputer. Nevertheless, there are a number of results that describe the chromatic number of a graph. For instance, as seen in Example 3.25, a cycle with an odd length has chromatic number 3. Thus any graph containing a cycle of this type needs at least 3 colors. The graph in Figure 3 41 is an example of this. When there are no cycles of odd length in a graph, there 2 colors are enough.
Theorem 3.8
A graph length.
G can be colored
with 2 colors if and only if it contains no cycle of odd
Proof
As noted above, when 5 has a cycle of odd length, then coloring 5 requires at least 3 colors. Hence if G can be colored with two colors, then it contains no cycle of odd length. Conversely, suppose G has no cycle of odd length. We will show that G can be colored with 2 colors. Since any coloring of each component of G with two colors
provides such a coloring of 5, we can assume Gis connected. (See Exercise 57 of Section 3.2 for the definition of a component.) Choose an arbitrary vertex S of G,and apply the breadth-first search algorithm to 5, starting with S. Since Gi, connected, every vertex gets labeled. Color each vertex red or blue according as its label is even or odd. We must show that no adjacent vertices, say U and V, have the same color. By the way the breadth-first search algorithm works, labels on adjacent vertices
153
3.4 Coloring a Graph
cannot differ by more than 1. Since the labels on U and V are both even or both odd, they must be the same, say m. Use the predecessors to trace shortest paths from U and V back to S. Let these paths first meet at the vertex W, with label k. (See Figure 3.46. We could have W = S and k = 0.) Then the portions of these paths from W to U and W to V, along with the edge {U, V }, form a cycle of length 2(m - k) + 1, which is odd, contrary to our assumption that 5 contains no odd cycle. W U
S 0 FIGURE 3.46
+
Example 3.29 The breadth-first search algorithm has been applied to the graph in Figure 3.47, starting with vertex V in the left component and X in the right. The resulting labels are shown. The coloring with 2 colors in Figure 3.48 is produced by coloring vertices red or blue according as their labels are even or odd. + (1) F &_
(°)
(1)
0
A
(1)
(2)
BI
IC
(2)
(2)
(3)
E4
D)
G
FIGURE 3.47
(0)
red
Ix
X41
red
(I) Ye
y blue
(2) Z4
z
red
FIGURE 3.48
The following result gives an upper bound on the number of colors needed to color a graph.
Theorem 3.9
The chromatic number of a graph 9 cannot exceed one more than the maximum of the degrees of the vertices of 9.
Proof Let k be the maximum of the degrees of the vertices of g. We will show that 9 can be colored using k + 1 colors Co, CI, . . . , Ck. First, select a vertex V and assign the color Co to it. Next, pick some other vertex W. Since there are at most k vertices adjacent to W and there are at least k + 1 colors available to choose from, there is at least 1 color (possible many) that has not been used on a vertex adjacent to W. Choose such a color. This process can be continued until all the vertices of 9 are colored. X
154
Chapter 3 Graphs
4
Example 3.30 The procedure described in Theorem 3.9 may use more colors than are really necessary. The graph in Figure 3.49 has a vertex of degree 4, which is the maximum degree, and so by Theorem 3 9 can be colored using 1 + 4 = 5 colors. However, by using the procedure described in Theorem 3.8, it can be colored using 2 colors. +
FIGURE 3.49
One of the most famous problems of the 19th century concerned the number of colors required to color a map. It is understood that, when coloring a map, countries with a common boundary other than a point are to be colored with different colors. The map is assumed to be drawn on a flat surface or globe, as opposed to a more complicated surface such as a doughnut. The usual approach to this problem is to let each country be a vertex of a graph and to join vertices representing countries with a.common boundary other than a point. Then coloring the map is the same as coloring the vertices of this graph so that no two adjacent vertices have the same color It was conjectured in 1852 that four colors would be enough to color any such map, but it was not until 1976 that Kenneth Appel and Wolfgang Haken, two mathe naticians at the University of Illinois, verified this conjecture. Their verification required an exhaustive analysis of more than 1900 cases that took more than 1200 hours on a high-speed computer. Example 3.31 In Figure 3.50(a) is a portion of a map of the United States. The associated graph obtained as described above is shown in Figure 3.50(b). This graph can be colored with 3 colors as illustrated in Figure 3.50(c). + -
I
'NY
WY
Vy I
UT CA
le
XA
(a)
k7Z
NM
fb) -, FIGURE 3.50
{Pa W
3.4 Coloring a Graph
155
EXERCISES 3.4 In Exercises 1-8 find the chromatic number of the graph. 1.
2.
3.
4.
5.
6.
7.
8.
9. What does it mean for a graph to have chromatic number 1? 10. What is the chromatic number of IC2,3? of IC7,4? of PCm,n? (See Exercise 52 of Section 3.2 for a definition of Kmn.)
11. Give examples of graphs where: (a) The chromatic number is one more than the maximum of the degrees of the vertices. (b) The chromatic number is not one more than the maximum of the degrees of the vertices. 12. It might be supposed that if a graph has a large number of vertices and each vertex has a large degree, then the chromatic number would have to be large. Show that this conclusion is incorrect by constructing a graph with at least 12 vertices, each of degree at least 3, that has chromatic number 2. 13. Using the process presented in the proof of Theorem 3.8, write a formal algorithm for coloring a graph with no cycles of odd length. 14. Show that when the algorithm in Exercise 13 is applied to a graph with n vertices and e edges, the graph can be colored using at most n + e elementary operations. (In analyzing the algorithm, consider the elementary operations to be coloring a vertex and using an edge.)
156
Chapter3
Graphs
In Exercises 15-18 color the graph using the algorithm in Exercise 13. 15.
i
i6.
i
i
I
0---A
40
4
4 41
17. _ 4
A ID--
18.
'
4
-
I I I I -
' ' I_-I
19. What is the chromatic number of the graph in Exercise 48 of Section 3. 1 ? 20. Suppose 5 is a graph with 3 vertices. How many ways are there to assign 3 colors to the vertices (this need not be a coloring of the graph)? What if the graph has 4 vertices and 4 colors are available? 21. Generalize Exercise 20 to the case of a graph with n vertices and n colors. 22. Suppose G is a graph with n vertices and there are n available colors to assign to the vertices. If one operation consists of assigning colors to the vertices and checking if a coloring has been made, how long would it take a computer that can perform one billion operations per second to check all possible color assignments for a graph with 20 vertices? Would this be a good way to find a coloring using the least number of colors? 23. Color the following map using only 3 colors.
24. Color the following map using only 3 colors.
---
K
-
3.4 Coloringa Graph
157
25. Color the following map using only 4 colors.
26. Color the following map using only 4 colors.
27. Solve Example 3.23 if there are 5 major committees: finance, budget, education, labor, and agriculture. The Clerk of the Senate needs only to consider State Senators Brown, Chen, Donskvy, Geraldo, Smith, and Wang. The finance committee has members Chen, Smith, and Wang; the budget committee has members Chen, Donskvy, and Wang; the education committee has members Brown, Chen, Geraldo, and Smith; the labor committee has only Geraldo; and the agriculture committee has Donskvy and Geraldo. 28. By representing the figure below by a graph, determine the minimum number of colors needed to color each circle so that touching circles have different colors.
29. The zookeeper of a major zoo wants to redo the zoo in such a way that the animals live together in their natural habitat. Unfortunately, it is not possible to put all the animals together in one location because some are predators of others. The dots in the chart below show which are predators or prey of others. What is the minimum number of locations the zookeeper needs?
158
Chapter3
Graphs a 1' a 0 cb 0 d e
d e
C
f
0
0
i
0.
0
f g h
S
0
0
0
0
0 6
0 i
g
0
0
30. Is 'can be colored with 3 colors' a graph isomorphismn invariant? 31. Show that 'has chromatic number 3' is a graph isomorphism invariant. 32. There are 7 tour bus companies in the Los Angeles area, each visiting at most three different locations from among Hollywood, Beverly Hills, Disneyland, and Universal Studios during a day. The same location cannot be visited by more than one tour company on the saire day. The first tour company visits only Hollywood, the second only Hollywood and Disneyland, the third only Universal Studios, the fourth only Disneyland and Universal Studios, the fifth Hollywood and Beverly Hills, the sixth Beverly Hills and Universal Studios, and the seventh Disneyland and Beverly Hills. Can these tours be scheduled only on Monday, Wednesday, and Friday? 33. Prove that if a graph with n vertices has chromatic number n, then the graph has in(n -1) edges. 34. Show that it is possible to assign one of the colors red and blue to each edge of K5 in such a way that no cycle of length 3 has all its edges the same color. 35. Show that the statement of Exercise 34 is incorrect if K5 is replaced by IC6. 36. Prove Theorem 3.9 by mathematical induction on the number of vertices. 37. For a graph 5, suppose that whenever a vertex V and the edges incident with V are removed from 5, the resulting graph has a smaller chromatic number. Prov- that if the chromatic number of 5 is k, then the degree of each vertex of g is at least k - 1.
3.5
o
DIRECTED GRAPHS AND MULTIGRAPHS In previous applications of graphs, an edge was used to represent a two-way or symmetric relationship between two vertices. However, there are situations where relationships hold in only one direction. In these cases the use of a line segment is not descriptive enough, and a directed line segment is needed.
+
Example 3.32 In many urban downtown areas the city streets are one-way. In such a case, it is necessary to use a directed line segment to indicate the legal flow of traffic. In Figure 3.51 major downtown locations are represented by dots, and two dots are connected by an arrow when it is possible to go from the first location to the
3.5 DirectedGraphs and Multigraphs
159
second by means of a one-way street. For example, the arrow from BANK to HOTEL denotes that there is a one-way street from BANK to HOTEL. + BANK
HOTEL
POST OFFICE
CITY HALL
DEPT. STORE FIGURE 3.51
i
Example 3.33 Although in a communication network there are routes where information can flow either way, there are also some where the flow is in just one direction. Within a microcomputer system, data usually can travel in either direction between CPU and the Memory, but only from the Input to the Memory and from the Memory to the Output. This type of situation can be represented by the diagram in Figure 3.52, where the arrows indicate how the data can flow. +
FIGURE 3.52
A directed graph is a finite nonempty set V and a set £ of ordered pairs of distinct elements of V. The elements of V are called vertices and the elements of £ are called directed edges. Figure 3.52 depicts a directed graph with vertices C, I, M, and 0 and directed edges (C, M), (M, C), (I, M), and (M, 0). As was true for graphs, a directed graph can be described either by the use of sets or by the use of a diagram, where arrows between the vertices in V describe which ordered pairs of vertices are being included. If there is a directed edge e = (A, B), it is said that e is a directed edge from A to B. In Figure 3.52 there is a directed edge from M to 0 but no directed edge from 0 to M. Similarly, there is a directed edge from M to C and one from C to M. Just as for graphs, two directed edges crossing in a diagram do not create a new vertex. Likewise, in this book the set of vertices is to be a finite set
160
Chapter 3 Graphs
(although not all authors require this). Finally, a directed edge cannot go from a vertex to itself, nor can there be two or more directed edges from one vertex to another. In a directed graph, the nu-nber of directed edges from vertex A is called the outdegree of A and is denoted as outdeg(A). Similarly, the number of directed edges to vertex A is called the indegree of A and is denoted by indeg(A). In Figure 3.52weseethatoutdeg(M) = 2,indeg(C) = l,andoutdeg(O) = O.Theorem3.1 states that in a graph the sum of the degrees is equal to twice the number of edges. Because each directed edge leaves one vertex and enters a second vertex, there is the following similar theorem for directed graphs. Theorem 3.10
In a directed graph the following three numbers are equal: the sum of the indegrees of the vertices, the sum of the cutdegrees of the vertices, and the number of directed edges. td
Represent'itf};7C
est;,z'g5A'fl)
As for graphs, a directed graph can be represented by a matrix. Suppose we have a directed graph D with n vertices labeled Vl, V2, . . , V,. Such a directed graph is called labeled. Form an n x n matrix in which the i, j entry is 1 if there is a directed edge from the vertex V, to the vertex Vj and 0 if there is not. This matrix is called the adjacency matrix of D (with respect to the labeling) and is denoted by A(D). Example 3.34 Figure 3.53 contains a directed graph and its adjacency matrix. The 1, 4 entry is O because there is no directed edge from VI to V4, but the 4, 1 entry is 1 because there is a directed edge from V 4 to VI. Row 3 contains all zeros because there are no directed edges from the vertex V3. Since there are no directed edges to the vertex V4, column 4 also contains all zeros.
V1
V2
Vl^
='V
0
1 00
V4
--
0 ok 0
V3
FIGURE 3.53
The last two observations in the previous example suggest the following version of Theorem 3.2 for directed graphs. The proof follows from the definitions.
3.5 Directed Graphs andMultigraphs
Theorem 3.11
161
The sum of the entries in row i of the adjacency matrix of a directed graph equals the outdegree of the vertex Vi, and the sum of the entries in column j equals the indegree of the vertex Vi. Directed graphs can also be represented by adjacency lists. To form an adjacency list, we begin by labeling the vertices of the directed graph. Then we list the vertices in a column, and after each vertex we list the vertices to which there is a directed edge from the given vertex.
+
Example 3.35 For the directed graph in Figure 3.53, the adjacency list is given below. Since V2 is the only vertex to which there is a directed edge from VI, V2 is the only vertex listed after VI. Similarly, because the only directed edges from V4 are to VI and V3, these are the two vertices listed after V4 . VI: V2
V2 : VI, V3 V3: (none) V4: VI,1/3 Y
In Sections 3.2 and 3.3 we introduced the concepts of multigraph, weighted graph, path, simple path, and cycle. These concepts have analogs using directed edges. We will leave the concept of directed weighted graphs to the exercises. To illustrate the other definitions, we will consider the diagram in Figure 3.54.
X
Y
FIGURE 3.54
The diagram in Figure 3.54 describes a directed multigraph. Here there is a directed loop h at the vertex W, and there are parallel directed edges i and j from V to X. Because a directed graph is also a special kind of directed multigraph,
162
Chapter 3 Graphs
definitions developed for a directed multigraph also apply to a directed graph. Note that directed edges such as f and m are not parallel directed edges, whereas i and j are parallel directed edges. An alternating sequence of vertices and directed edges I' 1 , 01,
V2, e2, . ., Vn, en, Vnt1
is called adirected path from V, to Vn+i if ei = (V, Vi+i)foreachi = 1, 2,.. n. The length of this directed path is n, the number of directed edges. Thus R, a, S, b, T, e, W is a directed path from R to W of length 3, which can also be written R, S, T, W or a, b, e. The directed path V, i X, k, U, m, V, j, X cannot be described by just using the vertices because there are two directed edges from V to X. However, this directed path can be described by just listing the directed edges i, k, m, j. Also T, b, S. d, V is not a directed path since the directed edge b is not from T to S but from S to T. Likewise, there is no directed path of positive length starting from Y. As before, a vertex is a directed path of length 0. The directed path a, d, , s a simple directed path from R to W, that is, a directed path with no vertex repeated. The directed path a, d, i, k, m, g is not a simple directed path because the vertex V is repeated. It is easily seen that a directed path contains a simple directed path. This proof follows very closely the proof of Theorem 3.4 for graphs and is omitted here. Theorem 3.12
Every U-V directed path con ains a U-V simple directed path. The directed path a, d, f, c in Figure 3.54 is a directed cycle because it is a directed path of positive length from R to R in which no other vertex is visited twice. But b, e, g, d is not a directed cycle because the directed edges g and d go in the wrong direction. Both h and f, m are considered to be directed cycles. The directed path k, m, f, c, a, d, j is not a directed cycle because the vertices U and V appear twice. A directed multigraph D is called strongly connected if for every pair A and B of vertices in D there is a directed path from A to B. Thus, in a strongly connected directed multigraph, we can go from any vertex to any other by following some route along the directed edges.
+
Example 3.36 The directed multigraph in Figure 3.54 is not strongly connected since there is no directed path from Y to any otier vertex. The directed graph in Figure 3.55(a) is strongly connected, however. Since a directed path can be found from any vertex to any other. On the other hand, the directed graph in Figure 3.55(b) is not strongly connected because there is no directed path from A to C. +
3.5 DirectedGraphs and Multigraphs A
B
A
B
C
D
C
D
(a)
163
(b) FIGURE 3.55
+
Example 3.37 Suppose the city council of a middle-sized city is concerned about traffic congestion in the downtown area. It instructs the city traffic engineer to turn each two-way street into a one-way street in the downtown area in such a way that there still will be a route from each downtown location to any other. If a graph is used to represent the current downtown street system (where intersections are vertices and streets are edges), then the city traffic engineer is to assign a direction to each edge, transforming the graph into a directed graph. Since there is to be a route from any place to any other, we want this new directed graph to be strongly connected. For example, if the graph in Figure 3.56 represents the downtown streets, then assigning directions as in Figure 3.55(a) produces a directed graph which is strongly connected. Thus this assignment of directions satisfies the city council's requirement. On the other hand, the assignment of directions as in Figure 3.55(b) yields a directed graph that is not strongly connected, and so it does not satisfy the city council's requirement. +
FIGURE 3.56
An important question: When can directions be assigned to the edges of a graph to yield a directed graph that is strongly connected? In Figure 3.57 there is an example of a graph that cannot be transformed into a strongly connected directed graph. The source of difficulty is the edge joining A and B. For if we direct this edge from A to B, then we cannot find a route from a place on the right side to any place on the left side. A similar problem occurs if we direct this edge from B to A. This edge joining A and B possesses an interesting property: if it is removed from the graph, the graph is no longer connected.
164
Chapter 3
Graphs
iA FIGURE 3.57
It can be proved that the absence of an edge whose removal disconnects a connected graph is equivalent to the existence of an assignment of directions to the edges to create a strongly connected directed graph. A proof can be found in suggested reading [9]. Thus, for the city traffic engineer in Example 3.37 to decide if there is an acceptable pattern of one-way streets, it suffices for the engineer to find if there is an edge whose removal will disconnect the graph. The material in Section 3.2 can be modified in a natural way to work for directed graphs. To illustrate ihis statement, we will consider directed Euler paths and circuits and directed I-lar]i Itonian paths and cycles.
DirctP::ed I rder
O.irct~isks ond P:athigs
The ideas of a directed Euler7 path and a directed Euler circuit in a directed multigraph are similar to the corresponding ones in a multigraph. A directed path in a directed multigraph D that includes exactly once all the directed edges of D and has different initial and ter-minal vertices is called a directed Euler path. A directed path that includes exactly once all the directed edges of D and has the same first and last vertices is called a directed Euler circuit. Recall that, in the proof of Theorem 3.5, constructing an Euler circuit required that each time we entered a vertex along an edge, there was another edge for us to leave on. This translated into the requirement that each vertex be of even degree. In constructing a directed Eu] er circuit, we require similarly that for each directed edge going into a vertex, there must be another directed edge leaving that vertex. This implies that the indegrez of each vertex is the same as the outdegree. These observations are summarized in the following theorem. Theorem 3.13
Suppose the directed multigiaph D has the property that whenever the directions are ignored on the directed edges, the resulting graph is connected. Then D has a directed Euler circuit if and only if, for each vertex of D, the indegree is the same as the outdegree. Furthermore, E has a directed Euler path if and only if every vertex of D has its inclegree equal to its outdegree except for two distinct vertices B and C, where the outdegree of B exceeds its indegree by 1 and where the indegree of C exceeds it; Dutdegree by 1. When this is the case, the directed Euler path begins at B and enAs at C.
3.5 Directed Graphsand Multigraphs
165
The algorithm for constructing an Euler circuit in a graph may be modified in a natural way (by choosing an unused directededge leaving the vertex) to construct directed Euler circuits and paths in directed graphs that satisfy the hypotheses of Theorem 3.13. Example 3.38 In telecommunications, there is an interesting application of directed Euler circuits that is due to Liu. (See [7] in the suggested readings.) Suppose there is a rotating drum with 8 different sectors, where each sector contains either a 0 or a 1. There are three detectors which are placed so that they can read the contents of three adjacent sectors. (See Figure 3.58.) a
FIGURE 3.58
The task is to assign Is and Os to the sectors so that a reading of the detectors describes the exact position of the rotating drum. Suppose the sectors are assigned Is and Os an in Figure 3.59. Then a reading of the detectors gives 010. If the drum is moved 1 sector clockwise, the reading becomes 101. However, if the drum is moved still another sector clockwise, the reading becomes 010 again. Thus two different positions of the rotating drum give the same reading. We want an assignment of I s and Os where this will not happen; that is, we want to arrange eight is and Os in a circle so that every sequence of three consecutive entries is different.
FIGURE 3.59
We will create a directed multigraph with 00, 01, 11, and 10 as vertices. From each vertex, construct two directed edges in the following manner. For the vertex
166
Chapter3
Graphs
ab, consider the two vertices o0 and bl (obtained from ab by dropping a and appending 0 and 1 at the end). Construct a directed edge from vertex ab to the vertex be (where c is either 0 or 1), and assign this directed edge the label abc. For example, there is a directed edge from 01 to 10 with label 010 and from 01 to 11 with label 011. This directed multigraph is shown in Figure 3.60. Note that the labels assigned to the directed edges are all different and would be an acceptable set of readings for the detector,;. 000
10
111 FIGURE 3.60
This is a directed multigraph such that when the directions on the directed edges are ignored, the resulting graph is connected. Furthermore, the indegree equals the outdegree of each vertex, and so a directed Euler circuit exists. Using the modification of the Euler circuit algorithm indicated in the paragraph preceding this example, we start with vertex 01 and construct the directed Euler circuit 011, 111, 110, 101, 010, 1)00, 000, 001. For the directed edges in this directed Euler circuit, the last 2 digits in each label are the first 2 digits in the label of the next directed edge. Thus, if we select the first digit of the label of each directed edge in the directed Euler circa uit, we get a sequence of 8 numbers such that every sequence of 3 consecutive entries is different (because the labels are all different). For this example this process gives the sequence 01110100. When this sequence is placed in the sectors of the rotating drum, the 8 positions of the drum will give 8 different readings. +
A directed Hamiltonian cycle (path) is a directed cycle (path) that includes each vertex exactly once. Because directed loops and parallel directed edges are not needed for a directed Haamiltonian cycle or path, we will assume that we are working with directed graphs rather than directed multigraphs. As with graphs, it
3.5 DirectedGraphsandMultigraphs
167
is very difficult to decide if there is a directed Hamiltonian cycle and if so, to find one. These concepts arise in connection with round-robin athletic competitions. In a round-robin contest, each team plays every other team exactly once, and a tie between two teams is not permitted. Such a competition can be described by a directed graph in which the teams are represented by vertices, and there is a directed edge from one vertex to another if the first team beats the second team. A directed graph of this kind is called a tournament directed graph or, more simply, a tournament. An alternate way of thinking of a tournament is that it is the result of taking the complete graph IC, and assigning a direction to each edge.
Example 3.39 Suppose there are three teams A, B, and C, where team A beat teams B and C, and team B beat team C. This is described in Figure 3.61(a). If, instead, team C beat team A, the tournament would be as in Figure 3.6 1(b). + A
b
a
B
A
c
b
a
C
B
(a)
C
C
(b) FIGURE 3.61
It may be desirable to find a ranking of the teams such that the first team beat the second team, the second team beat the third team, and so forth. Finding a ranking of the teams is the same as finding a directed Hamiltonian path for the tournament. It can be shown that every tournament has a directed Hamiltonian path. In Figure 3.61(a), the directed path a, c is Hamiltonian and thus provides a ranking of the teams. However, an examination of Figure 3.61(b) shows that there can be more than one directed Hamiltonian path. In fact, there are three: a, c; b, a; and c, b. This means that three separate rankings can be found. But also note that in Figure 3.61(b) there is a directed cycle, whereas in Figure 3.61(a) there is not. In general, if a tournament has no directed cycles, then there is only one directed Hamiltonian path, which provides a unique ranking of the teams. Further illustration of these points is found in Figure 3.62. For the tournament in Figure 3.62(a), there are no directed cycles and only one directed Hamiltonian path, namely, a, f, d, which gives the ranking A, B, C, D of the teams. In Figure 3.62(b) the tournament has a directed cycle, for example, a, f, d, e, and so there are several directed Hamiltonian paths, such as a, f, d and e, a, f.
168
Chapter3
Graphs AI
b
a
><
C,
I
-
d
C
D
(a)
(b) FIGURE 3.62
In Chapter 2 the concept of a relation on a set was introduced. When the set is finite, it is possible to depict the relation by a directed multigraph, which we will call the directed multigraph of the relation. In this multigraph the vertices correspond to the elements of the set, and there is a directed edge from x to y whenever x is related to y. Consider, for instance, the round-robin competition between the three teams A, B, and C described in Example 3.39. This situation can be described as a relation R on set S = {A, B, (71 where X R Y means that team X beat team Y. In this case the directed multigraph of relation R is precisely the tournament shown in Figure 3.61(a). Another example of the directed multigraph of a relation follows.
Example 3.40 Let R be the relation on set S = {2, 3, 4, 5, 6) defined by x R y whenever x divides y. Then R can be expressed as the following subset of S x S. R = {(2, 2), (2, 4), 2, 6), (3, 3), (3, 6), (4, 4), (5, 5), (6, 6)} Thus the directed multigraph of this relation has 5 vertices and 8 directed edges. It is shown in Figure 3.63. +
Cop/
FIGURE 3.63
Other material in Sections 3.1-3.3 can also be modified in an appropriate way for directed graphs. Some examples are found in the following exercises.
3.5 Directed Graphs andMultigraphs
169
EXERCISES 3.5 In Exercises 1-4 list the vertices and directed edges for the directedgraph. 1. A
B
3. A
B
2. A4
.0
4. A
B
C
D
E
D 04
B
E
D-,
C
D
In Exercises 5-S draw a diagram representing the directed graph with the set of vertices V and the set £ of directed edges. 5. V = {X, Y, Z, W, U ,8 = {(X, Y), (Z, U), (Y, X), (U, Z), (W, X), (Z, X)} 6. V = {A, B, C, DI, 6'=0 7. V= {A,B,C),6={(A,B),(B,C),(C,A),(B,A),(C,B)) 8. V={A,B,C,D),8=t(A,D),(D,B),(D,A)} In Exercises 9-12 construct the labeled directed graphfor the adjacency matrix. 0
9.
1
1 0 0
0
1
1 0 0 0
0
10.
0
1
Loilo] 11.
I
I
11I 0 I 1 0 1 0 0
12.
-O
I
0
1
I-
I
0
0
0
0
1 0
0
1
1
1 1 0 0 0 -1
0
0
1
0-
In Exercises 13-16 listfor the directed graph the other vertices on the directed edges to A, the other vertices on the directed edges from A, the indegree of A, and the outdegree of A. 13. A
G
dB
14. B w
An
-C
-.
170
Chapter3
Graphs
16.
15.
C
D
E E
A
For the directedgraphs in Exercises 17-18: (i) List the simple directed pathsfrom A to B. Give the length of each. (ii) List the distinct directed cycles. (Two directed cycles are distinct if there is a directed edge in one but not the other.) Give the length of each. 17. A
C
18. AyB3
B
E
In Exercises 19-22 find the adjacency matrix and adjacency list for the directed graph in the indicated exercises. Order the vertices according to alphabeticalorder. 19. Exercise 13
20. Exercise 14
21. Exercise 15
22. Exercise 16
23. Let V = {I, 2, . 1} and £ = {(x, y): x, y are in V, v : y, and x divides y}. Draw the directed graph with vertices V and directed edges £. 24. What does it mean if a row in an adjacency matrix for a directed graph contains only zeros? What if a column contains only zeros? 25. Draw all nonisomorphic directed graphs with 2 veriic.'. 26. Let S = {1, 2, 4, 8) and R = {(1, 8), (2, 4), (8, 2), (4, 1), (2, 2), (8, 1)) be the relation defined on S. Draw the directed multigraph of this relation. 27. Let S = {3, 5, 8, 10, 15, 24} and R be the relation defined on S by x R y whenever x divides y. Draw the directed multigraph of this relation. 28. Let S be the collection of all subsets of 1I, 2, 3} and R lre the relation defined on S by A R B whenever A is a subset of B or B is a subset of A. Draw the directed multigraph of this relation. 29. Describe the directed multigraph of a relation that is reflexive. 30. Describe the directed multigraph of a relation that is symmetric. 31. Describe the directed multigraph of a relation that is antisymmetric. 32. Construct the directed graph where the vertices are yoi, your parents, and your grandparents using the relationship 'is a child of.' 33. Construct the directed graph using 'is a parent of' in place of 'is a child of' in Exercise 32. How do the two directed graphs in Exercises 32 and 33 compare?
3.5 DirectedGraphs and Multigraphs
171
34. Susan has a fondness for chocolate desserts, in particular, pudding, pie, ice cream, eclairs, and cookies. Her preference is for pie over ice cream and cookies, eclairs over pie and cookies, cookies over pudding and ice cream, and pudding over eclairs, with no other preferences. Draw a directed graph to represent this situation. In Exercises 35-36 determine if the directed graphs in the indicatedexercises are strongly connected. 35. Exercise 17
36. Exercise 18
37. Give an example of a directed graph with 4 vertices where every directed path of positive length has length 1. 38. In a directed multigraph with n vertices what is the maximum length of a simple directed path? In Exercises 39-42 determine if a direction can be assignedto each edge of the graph resulting in a directedgraph that is strongly connected. If so, give such an assignment.
39.
40.
41.
42.
4
43. If a directed graph has a directed Hamiltonian cycle, why is it strongly connected? In Exercises 44-49 determine if the directedmultigraph hasa directedEulerpath or circuit. If there is one, construct it using the appropriatealgorithm as discussed in this section. 44.
45.
172
Chapter 3
Graphs
46.
47.
48.
49.
i
k
50. Suppose in Example 3.38 the rotating drum has only 4 different sectors on the drum and 2 detectors. Using the procedure described in that example, find a sequence of four Os and Is to be used on the rotating drum so that every sequence of two consecutive entries is different. 51. Show that in a tournament with n vertices the sum of the outdegrees is ln(n
-1).
52. Show that in a round-robin contest with 7 players thei-e cannot be 23 winners. In Exercises 53-54 find all the directed Hamiltonianpaths en the tournament.
53.
A
a
b
54.
B
e
C
f
D
55. Suppose that Susan has established a preference between any two chocolate desserts (pie, pudding, ice cream, Dthers, ice cream over all but cookies, pie over pudding, cookies, and eclairs). She prefers cookies over all of -he and eclairs over pie and pudding. Is there a ranking tc her preferences? How many? 56. In a tournament, the outdegree of a vertex is called the :sore (the number of wins for that team). In the following tournament, find a vertex with a maximum score and show that there is a directed path of length I or 2 from that vertex to any other. A
((
B d7
b
/
C
e
D
173
3.5 Directed Graphsand Multigraphs 57. Repeat Exercise 56 for the tournament below. A
D
C
58. Can a tournament have 2 teams that lose every time? 59. Suppose that the teams in the NFL Central Division play a round-robin contest, each team playing the others exactly once, in which the Bears beat every other team, the Lions lose to every other team, the Packers beat only the Lions and Bucs, and the Vikings beat everyone but the Bears. Is there a ranking of the teams? Is this ranking unique? 60. Write an algorithm for finding a directed Euler circuit. 61. Write a breadth-first search algorithm for directed graphs. In Exercises 62-65 use the breadth-first search algorithmfor directed graphs (see Exercise 61) to determine the distance and a shortest directedpathfrom S to T in the directed graph. 62.
s
A
B
C
D
63.
A S
BC
F
D
E
T
0
0 .
>¢H~iK Q
i
64.
K
L
M
T
*
65.
s
Er t K~
0
B
A
-T
G
'L
y
D
Il
U
R
I
i 'N 2i
r
.w
Is 2
Il 0
-d I
T
IJ
I
A
- zI
C
Ho
F4
r Ql
x
R
_f 1.
I 1
3
1
4
66. Write an algorithm for directed weighted graphs that finds the distance and a shortest directed path from a vertex S to every other vertex.
174
Chapter3 Graphs
In Exercises 67-70 determine the distancefrom S to all the other vertices in the directed weighted graph. Find a shortest directed pathfrom S to A.
67.
C 5
68
A
2
2.
4
C
-I
E12I
2
B
S
1
D
L
69.
CI 5 3 S
2
A
I
2
2
4
G!
B
1
1
3
H
2
2
A
Hi
1,
B
-
Ps
P
K
J
7 I.
De8 --
F3
I
4
2
,
i .
G
2
E
4I
F 32
F
2
S
4
3
/
I
71. Show that for a directed graph D with vertices V 1, V2.. V, and adjacency matrix A, the number of directed paths of length m from V, to Vj is the i, j entry of A'. 72. For the directed graph below, determine the number of directed paths of lengths 1, 2, 3, and 4 from VI to V3 , and from V2 to V 4 . V,
V2
V3
V4
73. For the directed graph below, determine the number of directed paths of lengths 1, 2, 3, and 4 from VI to and from V4 to VI. V
2
V4 ,
HistoricalNotes
175
74. Write a definition for an isomorphism between directed graphs. 75. Give two examples of properties of directed graphs that are invariant under isomorphism. Justify your answer. 76. Determine if the following pairs of directed graphs are isomorphic.
(a)
(b)
0
F I
77. Determine if the following pairs of directed graphs are isomorphic. (a)
X
(b)
78. Prove Theorem 3.10.
79. Prove Theorem 3.11.
80. Prove Theorem 3.12.
81. Prove Theorem 3.13.
79. Prove that every tournament has a directed Hamiltonian path. 80. Prove that if A is a vertex of maximum score (see Exercise 56) in a tournament, then there is a directed path of length 1 or 2 from A to any other vertex. 81. Give a definition of isomorphic directed weighted graphs. (See Exercise 20 of Section 3.3 for the definition of isomorphic weighted graphs.) Give an example of two nonisomorphic directed weighted graphs where the underlying graphs are isomorphic.
HISTORICAL NOTES
Leonhard Euler
The origin of graph theory is linked to Leonhard Euler's consideration of the bridges of Konigsberg problem. (See Exercise 30 in Section 3.2.) This problem dealt with a long-standing puzzle in the eastern Prussian city of Konigsberg. The center of the old city in Konigsberg was situated on an island in the Pregel River just below the point where its two upper branches joined. Seven bridges connected the land between the branches, the island, and the parts of the city on the two banks, as shown in the picture. The puzzle for the citizens of Konigsberg was to devise a walking route that crossed each of the bridges exactly once and ended at its starting place. Leonhard Euler (1707-1783) was the first to show this was impossible. In doing so, he characterized the situations where such paths and circuits were possible. Euler's solution, while it did not use a graphical representation, used the type of combinatorial reasoning that distinguishes graph theoretic forms of mathematical reasoning. His work in describing how to create Eulerian paths and circuits for such situations used a method of 'mentally removing' edges from graphs and considering the nature of the remaining structure. This representational process was central to the later proof, in 1752,
176
Chapter3 Graphs
of his famous formula for polyhedra and their related planar graphs, f - e + v = 2. (See Exercise 8 of the Supplementary Exercises for Chapter 3.) In his proof, Euler sliced off tetrahedral pieces of polyhedra associated with the planar graphs and noted that the (number of faces) -- number of edges) + (number of vertices) remains unchanged, eventually aniving at a tetrahedron. While there were some gaps in Euler's approach, they were filled in 1813 by Augustin-Louis Cauchy (1789-1857) [72]. In 1859, Sir William Rowar Hamilton (1805-1865) marketed a puzzle that required that one find specified paths and circuits on a planar graph consisting of the edges and vertices related to a regular dodecahedron. (See Exercise 40 in Section 3.2.) The first problem was to find a cycle passirg through each vertex once and only once. The puzzle was later offered in the form of a solid regular dodecahedron with pegs at the vertices and a string to mark out the edges in such a cycle. When the basic underlying graph is a weighted graph, the challenge of finding a Hamiltonian cycle of minimum weight is known William Rowan Hamilton
as the traveling salesperson problem. To date, mathematicians have been unable to find necessary and sufficient conditions that characterize those graphs that have a Hamiltonian path or cycle. The most famous problem in graph theory is the four color problem. This problem deals with the minimum number of colors needed to color a map in such a way that neighboring countries are differently colored. The four color problem was first examined by Francis Guthrie (1831-1899) in 1850. Augustus De Morgan learned of the problem through Guthrie's brother in 1852, and Arthur Cayley (1821-1895) posed it to the London 1'snhnmo3tanQ
RZ-etv in 1 272
While. --
1rsl ;n--rrnntnrnnf- wFe re -ff.r-A r-ver the --nrc
resolution of the problem had to wait until the 1976 proof by Kenneth Appel and Wolfgang Haken of the University of Illinois. Their methods, implemented by computer, called for the checking of nearly 2000 cases through an involved algorithm. These are but a few of the many different results that mark the first 250 years of work in graph theory. Its applications have become increasingly important in business and industry. In 1936, the first book on graph theory, written by the Hungarian mathematician AL11
D. Konig (1884-1944)
llioOj _-
11
17t'tJ, aN
wu[L]
a-uuay, a
iiiuiuI-UU
UI
-U1U1 L-AL- CA1SL, -I-I--
with several journals devoted to gr ph theory.
SUPPLEMENTARY EXERCISES
§'p
1. For a graph 5, the complement of 5 is the graph where the vertices are the same as the vertices of g and there is an edge between vertices A and B if and only if 5 does not have an edge between A and B. Find the complement of the following graph. 0
Supplementary Exercises
177
2. Is there a graph with 5 vertices, each with degree 1? Each with degree 2? Each with degree 3? Justify your answers. 3. Draw the graph with vertices X, Y, Z, W, R, and S, where X and R are adjacent, W, R, and S are adjacent to each other, and Y and Z are adjacent. 4. A few years ago the National Football League had two conferences each with 13 teams. It was decided by the league office that each team would play a total of 14 games, 11 of which were to be with teams in their own conference and the other 3 games with teams outside their own conference. Show that this is not possible. 5. Are the following graphs isomorphic? Justify your answer.
6. For the following graph, label the vertices and construct the adjacency matrix. Then label the vertices in a different manner and construct the adjacency matrix. Compare the two adjacency matrices and describe how they are related.
7. Construct the graph for the following adjacency list. VI: V2 , V3 , V5 V2: VI, V4 , V5
V3: VI, V4, V5 V4: V2 , V3, V5 V5: VI, V2, V3, V4
Suppose a graphg is drawn in the plane so that the edges of G intersect only at the vertices of G. Then g partitions
the plane into afinite number ofparts, called regions. An illustrationis given below, where the regions are labeled A,B,C,D,E,F,H,andI.
I
8. If g is such a connected graph in which e is the number of edges, v is the number of vertices, and f is the number of regions, prove that f - e + v = 2. (This result is called Euler'sformula.)
178
Chapter3 Graphs
9. The floor plan of the bottom level of a new home is shcwn below. Is it possible to enter the house at the front, exit at the rear, and travel through the house going through each doorway exactly once?
I
---
I
10. Find a Hamiltonian cycle for K14 and for 1C5. 11. Could the citizens of Kbnigsberg find an acceptable rcute by tearing down one bridge and building one new bridge? (See Exercise 30 in Section 3.2.) 12. Determine if the following multigraph has an Euler path. If it does, construct one using the Euler circuit algorithm.
f
a
g
1h
13. Determine if the following multigraph has an Euler circuit. If it does, construct one using the Euler circuit algorithm.
C
h
j
Supplementary Exercises
179
14. A street inspector wants to examine the streets in her region for potholes. If the map of her region is given below, is it possible for her to devise a route to examine each street once and return to her office?
15. Is the property 'is connected' a graph isomorphism invariant? 16. Is the property 'has an Euler circuit' a graph isomorphism invariant? 17. Use the breadth-first search algorithm to determine the distance and a shortest path from S to T in the following graph.
In Exercises 18-19 determine the distancefrom S to all the other vertices in the weighted graph. Find a shortest pathfrom S to A. Find a shortest pathfrom S to B. 18.
C
2
3
S
S
D
5
19.
A
4
1 D E
2
5
1 H
11FlI
G
3
2
I
C
B
E/
5
D
A
1
j
4 K
OL 2 B
180
Chapter3 Graphs
20. In the following weighted graph, find a shortest path fr:)m S to T which goes through the vertex A.
B S 2
I
C
I
4
D 2 6
G
A
5
E' I X3
J
t
4
<3
4H
2
I
8
j
5
T
21. For the following graph, determine the number of pati s of lengths 1, 2, 3, and 4 from VI to V2 and from VI to V4. V1
V4
22. Find the chromatic number of each of the following graphs.
(a)
(b)
-
23. Color the following map.
24. Is 'has two vertices with the same color' a graph isomnorphism invariant? 25. Show that a map formed by crossing a square with line segments can be colored with two colors. 26. Show that 'has chromatic number n' is a graph isomorphism invariant.
Supplementary Exercises
181
27. There are locations in a computer memory where stacks are stored during the execution of a computer program. Furthermore, a location can store only one stack at a time. Suppose stacks SI, S2, . . ., Sio are to be constructed during the execution of a computer program and stacks S and Sj will be in use at the same time if i = j (mod 3) or i - j (mod 4). What is the minimum number of locations for the stacks that will be needed during the execution of this computer program? 28. Suppose that the Illinois Electric Company has a power plant from which electrical power is sent along transmission lines to the surrounding communities. However, there is a continuing problem with power loss in these lines because of their deteriorated condition. The table below describes the loss along a transmission line from one community to another. What is the best route (one with least power loss) from the plant to the surrounding communities? (A dash in the table means there is no transmission line.)
To
Plant
Plant
-
Normal
-
Normal
Hudson
Ospur
Kenney
Lane
Maroa
3
2
6
1
4
5
-
4
4
Hudson
9
2
-
3
Ospur
3
-
-
Kenney
2
Lane
1
Maroa
6
3
-
3
5
6
3
6
9
4
7
2 6
-
1
1
-
2
2
7
-
2
3
4
2
2
29. Construct the directed graph for the following adjacency matrix.
0 0 1 1
0 1 1 0 0 0 0 0 1 0 1 0
30. In a large corporation, the chief executive officer communicates with her vice-presidents, and they can communicate with her. Furthermore, the vice-presidents can communicate with the directors, field managers, and division heads, but only the directors can communicate back. Also, the field managers and division heads can communicate with salespersons, but they can communicate back only with the field managers. Draw a directed graph to represent the communication lines among these positions. 31. Construct, if possible, a directed graph with 6 vertices where the outdegrees of the vertices are 2, 3, 4, 1, 0, and 5 and the indegrees of the vertices are 2, 4, 1, 1, 5, and 2. 32. Give an example of a directed graph with 6 vertices where every directed path is a simple directed path. 33. Work Example 3.38 if there are 16 different sectors and 4 detectors. A tournament is transitive if whenever (A, B) and (B, C) are directed edges in the tournament, then so is (A, C). 34. Prove that a tournament is transitive if and only if there are no directed cycles. 35. Prove that the scores (see Exercise 56 of Section 3.5) in a transitive tournament with n vertices are 0, 1, 2, 3,..,n-1.
182
Chapter3 Graphs
36. Find all the directed Hamiltonian paths in the following tournament. A
a
b
B
e
c
f
D
37. For the directed graph below, determine the number of directed paths of length 1, 2, 3, and 4 from VI to and from V2 to V5 . V
V4,
V
--
,% 3
V4
--
V5
38. Show that a connected graph can be made into a strongly connected directed graph if and only if each edge is an edge of some cycle. 39. The directed edges of a directed graph can be considered as a relation on the set of vertices. (See Section 2.2.) When will this relation be reflexive? Symmetric? Transitive? 40. For the following directed weighted graph, determine the distance from S to all the other vertices in the directed graph. Find a shortest directed path from S to A. Find Etshortest directed path from S to B.
COMPUTER PROJECTS
I
1
Write a computer program having the specified input and output. 1. Given the adjacency matrix of a graph, find the degree of each vertex. 2. Given the adjacency matrix of a directed graph, find the indegree and outdegree of each vertex.
Suggested Readings
183
3. Given the adjacency matrix of a graph, find its adjacency list. 4. Given the adjacency list of a graph, find its adjacency matrix. 5. Given the adjacency matrix of a graph with vertices VI, V2 , . .V.,V, find the number of paths of length m from
Vi to Vj. 6. Determine if a given graph is a complete graph. 7. Given a graph and U-V path, find a U-V simple path. 8. Given a multigraph, determine if there are any loops or parallel edges. 9. Determine if a given graph is a bipartite graph. 10. Use the breadth-first search algorithm to find the components of a given graph. (See Exercise 57 in Section 3.2.) 11. Given a graph, find a coloring of its vertices in which the number of colors does not exceed one more than the maximum of the degrees of the vertices. 12. Given a graph, use the breadth-first search algorithm to label its vertices. 13. Given a weighted graph 5 with positive weights and a vertex S. use Dijkstra's algorithm to determine the distance and a shortest path from S to every other vertex in g. 14. Given a connected graph in which every vertex has even degree, find an Euler circuit. 15. Given a connected graph in which every vertex has even degree except for two vertices A and B, find an Euler path between A and B. 16. Given a graph with n vertices in which each vertex has degree greater than ', find a Hamiltonian cycle. 17. Given a graph and a positive integer n, determine if the graph has a cycle of length n.
SUGGESTED READINGS 1. Bogart, Kenneth P. Introductory Combinatorics, 3rd ed. San Diego, CA: Academic Press, 1999. 2. Bondy, J. A. and U. S. R. Murty. Graph Theory with Applications. New York: Elsevier Science, 1979. 3. Chachra, Vinod, Prabhakar M. Ghare, and James M. Moore. Applications of Graph Theory Algorithms. New York: Elsevier Science, 1979. 4. Chartrand, Gary. Graphs as MathematicalModels. Boston: Prindle, Weber & Schmidt, 1977. 5. Even, Shimon. GraphAlgorithms. New York: Freeman, 1984. 6. Harary, Frank. GraphTheory. Reading, MA: Addison-Wesley, 1994. 7. Liu, C. L. Introductionto CombinatorialMathematics. New York: McGraw-Hill, 1968. 8. Ore, Oystein. Graphs andTheir Uses, Washington: Mathematical Association of America, 1963. 9. Polimeni, Albert D. and Joseph H. Straight. Foundationsof DiscreteMathematics, 2nd ed. Pacific Grove, CA: Brooks/Cole, 1990. 10. Roberts, Fred S. Graph Theory and Its Applications to Problems of Society. Philadelphia: SIAM, 1978. 11. Wilf, Herbert S. CombinatorialAlgorithms: An Update. Philadelphia: SIAM, 1989.
Trees 4.1 Properties of Trees 4.2 Spanning Trees 4.3 Depth-First Search 4.4 Rooted Trees 4.5 Binary Trees and Traversals 4.6 Optimal Binary Trees and Binary Search Trees
In Chapter 3 we studied several different types of graphs and their applications. A special class of graphs-trees-has been found to be very useful in computer science. Trees were irst used in 1847 by Gustav Kirchhoff in his work on electrical networks. Later they were used by Arthur Cayley in the study of chemistry. Now trees are widely used in computer science as a way to organize and manipulate data.
4.1
+
PROPERTIES OF TREES We begin this section by looking at some examples.
o
Example 4.1
In 1857 Arthur Cayley studied hydrocarbons, chemical compounds formed from hydrogen and carbon atoms. In particular, he investigated saturated hydrocarbons, which have k carbon atoms and 2k + 2 hydrogen atoms. He knew that a hydrogen atom was bonded (chemically kept together) with one other atom, and each carbon atom was bonded with four other atoms. These compounds are usually represented pictorially as in Figure 4.1, where a line segment between two atoms indicates a bonding. These chemical diagrams can be redrawn as graphs, as illustrated in Figure 4.2. Note that in these graphs we have followed the customary practice of using the same chemical symbol on different vertices representing the same element. However, it is not really necessary to label the vertices with C and H since a vertex
184
4.] Propertiesof Trees
185
of degree 4 represents carbon and a vertex of degree 1 represents hydrogen. It was through the mathematical analysis of these graphs that Cayley predicted the existence of new saturated hydrocarbons. Later discoveries proved his predictions correct. + H
H
H
C
H
H
C
H
H
C
H
H methane
H ethane FIGURE 4.1
H
r H
H
l
He
C
,-o
*H
He
Ii H
C -
OH
H
-
I0
H FIGURE 4.2
+
Example 4.2 Suppose we are planning the telephone network for an underdeveloped area, where the goal is to link together five isolated towns. We can build a telephone line between any two towns, but time and cost limitations restrict us to building as few lines as possible. It is important that each town be able to communicate with
186
Chapter 4 Trees
each other town, but it is not necessary that there be a direct line between any pair of towns, since it is possible to) route calls through other towns. If we represent the towns by the vertices of a graph and the possible telephone lines by edges between the vertices, then the graph in Figure 4.3 represents all the possibilities we can have for the telephone lines. (This is merely the complete graph on five vertices.) We need to select a set of edges that will give us a path between any two vertices and that has no more edges than necessary. One such set of edges is la, b, c, d), as illustrated in Figure 4.4. This choice of edges allows communication between any two towns For example, to communicate between Y and X, we can use edges d, b, a, c in that order. Notice that if any edge is deleted from this set, then it is not possible to communicate between some pair of towns. For example, if we use only edges a, b, and c, towns U and Y cannot communicate. Another setofacceptableedg.!is {e, g, h, k).The sets {g, h, j, k) and {a, b, e, h) are not acceptable because nct every pair of towns can communicate. Also the set {a, b, g, j, k} is bigger than necessary because the edge g can be left out without disrupting communication between any two towns. +
V
FIGURE 4.3
FIGURE 4.4
For the graphs in Figures 4.2 and 4.4, we note two common characteristics; namely, these graphs are connected (there is a path between any two vertices) and have no cycles. Any graph that is connected and has no cycles is called a tree. Additional examples of trees follow.
+
Example 4.3 Since each of the graphs in Figure 4.5 is connected and has no cycles, each is a
tree. +
4.1 Propertiesof Trees
*
(a)
187
a
(b) FIGURE 4.5
+
Example 4.4 Neither of the graphs in Figure 4.6 is a tree. The graph in Figure 4.6(a) is not connected, and the graph in Figure 4.6(b) has a cycle. +
(a)
(b) FIGURE 4.6
Theorem 4.1
Let U and V be vertices in a tree. Then there is exactly one simple path from U to V. Proof Since a tree is a connected graph, there is at least one path from U to V. Thus, by Theorem 3.4, there is a simple path from U to V. We will now show that there cannot be two distinct simple paths from U to V. To do so, we will assume that there are distinct simple paths Pi and P2 from
188
Chapter4
Trees
U to V and show that this leads to a contradiction. Since PI and P2 are different, there must be a vertex A (possibly A = U) lying on both PI and P2 such that the vertex B following A on PI does not follow A on P2 . In other words, PI and P2 separate at A. (See Figure 4.7.) Now follow path PI until we come to the first vertex C that is again on both paths. (The paths must rejoin because they meet again at V.) Consider the par: of the simple path PI from A to C and the part of the simple path P 2 from C to A. These parts form a cycle. But trees contain no cycles, so we have a contradiction. It follows that there cannot be two distinct simple paths between any pair of vertices. 8 B Up-.
P.
-T
P9 FIGURE 4.7
Looking at each previous example of a tree reinforces the idea that there is a unique simple path from an) vertex to another. Notice also that, in all of these examples, every tree has at least two vertices of degree 1. Theorem 4.2
In a tree T with more than ore vertex, there are at least two vertices of degree 1.
Proof: Since T is a connected graph with at least two vertices, there is a simple path with at least two distinct vertices. Thus 7 contains a simple path with a maximal number of edges, sely from U to V, where U and V are distinct. If U has degree more than 1, then since T has no cycles, a longer simple path would exist; likewise for V. Thus U and V have degree 1. G For the tree in Figure 4.4, there are 5 vertices and 4 edges, and for the tree in Figure 4.5(a), there are 9 vertices and 8 edges. In fact, in each previous example of a tree, the number of vertices is one more than the number of edges. The next theorem establishes that this :is always the case.
Theorem 4.3
A tree with
11 vertices
has exactly n -1 edges.
Proof. The proof will be by induction on n, the number of vertices. Because a tree is a graph, there are no loops in a tree. Hence there are no edges in a tree with only one vertex, and the theorem holds when n = 1. Now assume the theorem holds for all trees which have k vertices. We will prove that the theorem holds for a tree T with k + 1 vertices. By Theorem 4.2 there is a vertex V with degree ] . Remove the vertex V and the edge on V from the graph T to obtain a new graph T'. (See Figure 4.8.) This graph T' has k vertices
4.1 Propertiesof Trees
189
and is still a tree. (Why?) Thus, by the induction assumption, T' has k - 1 edges. But then T has k edges. By mathematical induction the theorem holds for all positive integers n. g
.
I
@
0
8
J V
A FIGURE 4.8
*
Example 4.5 An intelligence agency has established a network of 10 spies engaging in industrial espionage. It is important that each spy be able to communicate with any other, either directly or indirectly through a chain of others. Establishing secret locations to exchange messages is difficult, and the agency wants to keep the number of these meeting places as small as possible. Yet for reasons of secrecy, no more than 2 spies should know about any particular meeting place. This communication network can be represented by a graph in which the vertices correspond to spies and an edge joins 2 vertices when the corresponding spies know about the same meeting place. In fact, this graph is a tree with 10 vertices, and so there will need to be 9 meeting places
in all.
Theorem 4.4
(a) (b)
M
When an edge is removed from a tree (leaving all the vertices), the resulting graph is not connected and hence is not a tree. When an edge is added to a tree (without adding additional vertices), the resulting graph has a cycle and hence is not a tree.
Proof If an edge is added or removed from a tree, the resulting new graph can no longer be a tree by Theorem 4.3. Since removing an edge cannot create a cycle nor adding an edge disconnect the graph, both parts of the theorem follow. X
190
Chapter 4 Trees
Theorem 4.4 shows that a tree has just the right number of edges to be connected and not have any cyz]es. By looking at the tree in Figure 4.5(a), we can see how the deletion of any edge produces a disconnected graph by breaking the tree into two parts. In addition, we can see how the addition of an edge between two existing vertices creates a cycle in the new graph. The following theorem gives some other ways of characterizing a tree. Its proof will be left to the exercises. Theorem 4.5
The following statements are equivalent for a graph T. (a) (b)
T is a tree. T is connected, and the number of vertices is one more than the number of edges. (c) T has no cycles, and the number of vertices is one more than the number of edges. (d) There is exactly one simple path between each pair of vertices in T. (e) T is connected, and the removal of any edge of T results in a graph that is not connected. (f) T has no cycles, and the addition of any edge between two nonadjacent vertices results in a graph with a cycle. I I It is the equivalence of pasts (a) and (b) in Theorem 4.5 that helps in the mathematical analysis of saturated hydrocarbons of the type CkH2k+2. (See Example 4.1.) We know that there will be k carbon atoms and 2k + 2 hydrogen atoms represented in the graph. Furthermore, since the atoms form a compound, the graph will be connected. Since each vertex represents an atom, there will be k + (2k + 2) = 3k + 2 vertices. Also, since a carbon atom has degree 4 and a hydrogen atom has degree 1, the sum of the degrees is 4k + (2k + 2) = 6k + 2. By Theorem 3.1 the number of edges is 2(6k + 2) = 3k + 1, which is one less than the number of vertices. Hence, by Theorem 4.5(b), the graph representing the chemical compound is always a tree. Knowing this, Cayley used information about trees to predict the existence of new saturated hydrocarbons. The interested reader should consult suggested reading [2] for more details.
EXERCISES 4.1 In Exercises 1-S determine if each graph is a tree. 1. a
2.
4.1 Propertiesof Trees
3.*
0
191
4.
5.
6. *-
7.
8.
9. How many vertices are there in a tree with 15 edges? 10. How many edges are there in a tree with 21 vertices? 11. Seven farming communities in Iowa want to develop a computer telecommunications network to facilitate communication during a farm crisis. For reasons of economy, they want to build as few lines as possible but still allow communication between any two towns. Indicate how this might be done for the following map. Traer 0
Tama 0
Conrad
0 Lincoln Garwin
Beaman Gladbrook
12. As few trails as possible are to be built between houses in a primitive community so that it is possible for a resident to go from any house to any other. If there are 34 houses, how many trails need to be built? Since it is considered bad luck to live at the end of a trail, can the trails be constructed so that no house is so situated? 13. A farmer needs to irrigate the fields in which his crops are growing. (A map of the fields is given below, in which the fields are the enclosed areas and edges represent earthen walls between the fields.) Because he lacks modem equipment, his method of irrigation is to break holes in the walls and let water from the outside cover
192
Chapter4
Trees
the entire field. He wants to irrigate each field and to break as few walls as possible. In how many walls should he break holes?
14. Draw a graph that is not a tree for which the number (if vertices is one more than the number of edges. 15. Draw a tree with at least 6 vertices that has exactly 2 vertices of degree 1. 16. What is the smallest number of edges in a connected graph with n vertices? 17. What is the largest number of vertices in a connected graph with n edges? 18. How many simple paths of nonzero length are there in a tree with n vertices, where n > 2? (Regard two simple paths as the same if they have the same edges.) 19. Prove that the graph T' in the proof of Theorem 4.3 is
a tree.
20. Prove that if an edge is deleted from a cycle in a connected graph, the graph remains connected. 21. For which n is IC, a tree? (The graph En is defined in Section 3.1.) 22. Prove by mathematical induction on the number of velrtices that any tree can be drawn on a sheet of paper so that its edges do not intersect except at vertices. 23. There are two saturated hydrocarbons of the type C4 H 0: butane and isobutane. Draw a tree representing the chemical structure of each. 24. Draw a graph representing a saturated hydrocarbon with 5 carbon atoms. 25. Can a tree with 13 vertices have 4 vertices of degree 3, 3 vertices of degree 4, and 6 vertices of degree 1? 26. How many vertices of degree 1 are there in a tree with 3 vertices of degree 4, 1 vertex of degree 3, 2 vertices of degree 2, and no vertices of degree more than 4? As trees on the vertices labeled A, B, and C, the two trees; in figures (a) and (b) below are the same since they both have the same set of edges, namely, {A, B} and {B, C). The trees in figures (a) and (c) are distinct since they do not have the same set of edges. For example, {A, C') is an edge of the tree in figure (c) but not of the tree in figure (a).
A
B
C
A
C
A
B
0----
(a)
C
0fi)
(c)
B
4.1 Propertiesof Trees
193
27. Draw the 3 distinct trees with 3 labeled vertices. (Use 1, 2, 3 as the labels.) 28. Draw the 16 distinct trees with 4 labeled vertices. (Use 1, 2, 3, 4 as the labels.) In order to count the number of distinct trees with vertices labeled 1, 2, . ., n we establisha one-to-one correspondence between each such tree and a list a, a2 , . . ., an- 2 , where 1 < a; < n for i = 1, 2, .., n -2. The following algorithm shows how to get such a listfrom a labeled tree T.
Prufer's Algorithm This algorithm constructs a list a, a2, . and the labels are 1, 2, n.
an-2 of numbers for a tree with n labeled vertices, where n
Step 1
(initialization) (a) Set T to be the given tree. (b) Set k = 1. Step 2 (choose ak) while T has more than two vertices Step 2.1 (find a vertex of degree 1)
Select from T the vertex X of degree 1 that has the smallest label. Step 2.2 (make a new tree) (a) Find the edge e on X, and let W denote the other vertex on e.
(b) Set ak to be the label on W. (c) Delete the edge e and vertex X from T to form a new tree T'. Step 2.3 (change T and k) (a) Replace T with T'. (b) Replace k with k + 1.
endwhile U U D .f .. . QUa,U. ............ AEW. ,=W S ,S A...S nD .,.......
For example, the listfor the following tree is 6, 5, 1, 5, 6. 2
6 35
7
1
4
In Exercises 29-32 use Prufer's algorithm to find the listfor each tree in the indicated exercise or graph.
29. Exercise 27 30. Exercise 28
>
3
194
Chapter 4
31. 1
Trees
2
3
32.
1 .
*.
5
3
5
,I 4
2
6
6 .74
8
I
7
9
33. We can construct a tree from a list L of n -2 numnbes taken from N = {l, 2, .. , n) as follows. (Here we assume that the vertices of the tree are labeled 1, 2, . ., n.) Pick the smallest number k from N that is not in the list L and construct an edge on that number and the first number in the list L. Then delete the first number in L, delete k from N, and repeat the process. When L is exhausted, construct an edge joining the two numbers remaining in N. For example, the tree generated by the 1 st 6, 5, 1, 5, 6 is pictured before Exercise 29. Construct the tree for the list 2, 2, 2, 2. In Exercises 34-37 repeat Exercise 33 for each list. 34. 1,2,3,4
35. 1,2,3,2,1
3i. 4,3,2,1
37. 3,5,7,3,5,7
38. Assuming that Prufer's algorithm establishes a one-to one correspondence between trees with vertices labeled 1, 2, . . ., n and lists as described in Exercise 33, prove ti at the number of distinct trees with vertices 1, 2, n is n'-2 for n > 1. Exercises 39-44 establish in a cyclicalfashion a proof of Theorem 4.5. 39. Prove that part (a) implies part (b) in Theorem 4.5. 40. Prove that part (b) implies part (c) in Theorem 4.5. 41. Prove that part (c) implies part (d) in Theorem 4.5. 42. Prove that part (d) implies part (e) in Theorem 4.5. 43. Prove that part (e) implies part (f) in Theorem 4.5. 44. Prove that part (f) implies part (a) in Theorem 4.5. 45. Give an inductive proof of Theorem 4.3 that does not use Theorem 4.2. (Hint: Use mathematical induction on the number of edges.) 46. Use Theorem 4.3 to give an alternate proof of Theore mn4.2. 47. Show that Prufer's algorithm establishes a one-to-one correspondence between trees with vertices labeled 1, 2, . n and lists as described in Exercise 33.
4.2
+
SPANNING TREES In Example 4.2 in Section 4. L,we found a tree that contained all the vertices of the original graph. This is an idea that appears in many applications, including those that involve power lines, pipeline networks, and road construction.
4.2
Spanning Trees
195
Example 4.6 Suppose an oil company wants to build a series of pipelines between six storage facilities in order to be able to move oil from one storage facility to any of the other five. Because the construction of a pipeline is very expensive, the company wants to construct as few pipelines as possible. Thus the company does not mind if oil has to be routed through one or more intermediate facilities. For environmental reasons, it is not possible to build a pipeline between some pairs of storage facilities. The graph in Figure 4.9(a) shows the pipelines that can be built. a
a
b
h
(a)
(b) FIGURE 4.9
The task is to find a set of edges which, together with the incident vertices, form a connected graph containing all the vertices and having no cycles. This will allow oil to go from any storage facility to any other without unnecessary duplication of routes and, hence, unnecessary building costs. Thus a tree containing all the vertices of a graph is again being sought. One selection of edges is b, e, g, i, and j, as illustrated by the colored edges in Figure 4.9(b). +
A spanning tree of a graph g is a tree (formed by using edges and vertices of g) containing all the vertices of S. Thus in Figure 4.9, the edges b, e, g, i, and j and their incident vertices form a spanning tree for the graph. We shall follow the customary practice of describing a tree by listing only its edges, with the understanding that its vertices are those incident with the edges. Thus in Figure 4.9, we would say that the edges b, e, g, i, and j form a spanning tree for the graph. If a graph is a tree, then its only spanning tree is itself. But, in general, a graph may have more than one spanning tree. For example, the edges a, b, c, d, and e also form a spanning tree for the graph in Figure 4.9(a). There are several ways to find a spanning tree for a graph. One is to get rid of cycles by removing edges. This process is illustrated in the following example.
196
Chapter 4 Trees
+
Example 4.7 The graph in Figure 4.10(a) is not a tree because it contains cycles such as a, b, e, d. In order to obtain a tree, our procedure will be to delete an edge in each cycle. Deleting b from the cycle a, b, e, d gives the graph in Figure 4.10(b), which is still not a tree because of the cycle c, e, d. So we delete an edge in this cycle, say e. The resulting graph in Figure 4.10(c) is now a tree. This, then, is a spanning tree for the original graph. +
a
b
C
(a)
(b)
(c)
FIGURE 4.10
If a connected graph has n vertices and e edges, with e > n, we must perform this deletion process e - n + 1 times in order to obtain a spanning tree. By performing these deletions, we change the number of edges from e to e - (e - n + 1) = n - 1, which is the number of edges in a tree with n vertices. Tmte Bread1,-First Seareh Algorfthnn, The method described above :is not the only way to find a spanning tree. There are many others, and some of these are easier to program on a computer because they do not require that cycles be found. One of these methods uses the breadth-first search algorithm, which was discussed in Section 3.3. Recall that in the breadth-first search algorithm, we start with a vertex S. Then we find the vertices adjacent to S, and assign them the label 1(S). (The label given to a vertex by the breadth-first search algorithm indicates its distance from S and its predecessor on a shortest path from S.) Next, we look at each unlabeled vertex that is adjacent to a vertex V with label 1; these vertices are then given the label 2(V). We continue in this manner until there are no more unlabeled vertices adjacent to labeled vertices. Let T denote the set of edges that join each labeled vertex to its predecessor. The labeling process in step 2.2 of the breadth-first search algorithm guarantees that the edges in T form a connected graph. Furthermore, each edge in T joins two vertices labeled with consecutive integers, and no vertex in L is joined by an edge in T to more than one vertex with a smaller label. Therefore no collection of
4.2 Spanning Trees
197
edges in T forms a cycle. Because, in a connected graph, every vertex is eventually labeled, the edges in T form a tree that includes every vertex in the graph, and so T is a spanning tree for the graph. (As before, we are referring to T as a tree, with the understanding that the vertices of the tree are those incident with the edges.) Example 4.8 We shall apply the breadth-first search algorithm to find a spanning tree for the graph in Figure 4.11. We may start the breadth-first search algorithm at any vertex, say K, which is labeled 0(-). The vertices adjacent to K are A and B, and these are labeled 1 (K). Next we label the unlabeled vertices adjacent to A and B, which are D and E. These are labeled 2(A) and 2(B), respectively. We continue in this manner until all the vertices are labeled. One possible set of labels is shown in Figure 4.12. The edges that join each vertex to its predecessor (which is indicated in the label on the vertex) then form a spanning tree for the graph. These edges are shown in color in Figure 4.12. + I(K) A
3(D)
C
4(C)
F
i
2(A) D
0H K
B
E
B 1(K)
H
FIGURE 4.11
i E 2(B)
4(G) I
H :J()
FIGURE 4.12
We should note that, when using the breadth-first search algorithm, there are places where edges are chosen arbitrarily. Different choices lead to different spanning trees. In Example 4.8, for instance, instead of choosing the edges {D, H I and IC, F), we could have chosen the edges {E, H) and {F, G). This would give the spanning tree shown in color in Figure 4.13. I(K)
3(D)
4(C)
A
C
F
0H
4(G)
2(A-)
B l (K)
E 2(B)
H 3(D)
FIGURE 4.13
198
Chapter 4
Trees
A simple path from the starting vertex S to any other vertex that uses only edges in a spanning tree is a shortest path in the original graph between these vertices. (Recall from Section 3.3 that the label given to each vertex by the breadthfirst search algorithm is its distance from S.) For this reason, a spanning tree constructed by means of the bieadth-first search algorithm is sometimes called a shortest path tree. In the examples so far, the graphs have had spanning trees. However, this is not always the case, as the next example shows.
+
Example 4.9 The graph shown in Figure 4.14 does not have a spanning tree because it is not possible to choose edges that connect all the vertices. In particular, we cannot find edges that can be used to rr ake a path from A to E.
RI E FIGURE 4.14
In previous examples we have seen that the existence of a spanning tree is related to the connectedness of the graph. This relationship is made explicit in the following theorem.
Theorem 4.6
A graph is connected if and only if it has a spanning tree.
Proof Suppose that the graph g has a spanning tree T. Since T is a connected graph containing all the vertices in g, there is a path between any two vertices U and V in g using edges from T. But since the edges of T are also edges of g, we have a path between U and V using edges in g. Hence g is connected. Conversely, suppose g is connected. Applying the breadth-first search algorithm to g yields a set L of vertices with labels and a set T of edges connecting the vertices in L. Moreover, 7 is a tree. Since g is connected, each vertex of g is labeled. Thus £ contains all the vertices of g, and T is a spanning tree for g. We will now discuss two types of spanning trees that occur frequently in applications.
4.2
Spanning Trees
199
When pipelines are to be constructed between oil storage facilities, it is likely that the cost of building each pipeline is not the same. Because of terrain, distance, and other factors, it may cost more to build one pipeline than another. We can describe this problem by a weighted graph (discussed in Section 3.3), in which the weight of each edge is the cost of building the corresponding pipeline. Figure 4.15 depicts such a weighted graph. The problem is to build the cheapest set of pipelines. In other words, we want to find a spanning tree in which the sum of the costs of all the edges is as small as possible.
FIGURE 4.15
In a weighted graph, the weight of a tree is the sum of the weights of the edges in the tree. A minimal spanning tree in a weighted graph is a spanning tree for which the weight of the tree is as small as possible. In other words, a minimal spanning tree is a spanning tree such that no other spanning tree has a smaller weight. +
Example 4.10 For the weighted graph in Figure 4.15, the edges b, c, e, g, and h form a spanning tree with weight 3 + 4 + 3 + 4 + 3 = 17. The edges a, b, c, d, and e form another spanning tree with weight 2 + 3 + 4 + 2 + 3 = 14. The edges a, d, f, i, and j form yet another spanning tree, which has weight 8. Since this spanning tree uses the five edges with the smallest weights, there can be no spanning tree with smaller weight. Thus, the edges a, d, f, i, and j form a minimal spanning tree for this weighted graph. + In Example 4.10, we were able to find a minimal spanning tree by trial and error. However, for a weighted graph with a large number of vertices and edges, this is not a very practical approach. One systematic approach would be to find all the spanning trees of a connected weighted graph, compute their weights, and then select a spanning tree with the smallest weight. Although this
200
Chapter 4
Trees
approach will always find a min imal spanning tree for a connected weighted graph, checking out all the possibilities can be a very time-consuming task, even for a supercomputer. A natural way to try to construct a minimal spanning tree is to build a spanning tree using edges oF smallest weights. This approach is illustrated in Example 4.1 1.
+
Example 4.11 For the weighted graph in Figure 4.16(a), we begin with any vertex, say A, and select the edge of smallest weight on it, which is b. To continue building a tree, we look at the edges a, c, e, and f touching edge b and select the one with the smallest weight, which is f. Tha next edges to look at are a, c, e, and g, the ones touching b and f, the edges already selected. There are two with the smallest weight, e and g, and we select one arbitrarily, say e. The next edges we consider are a, c, and d. (The edge g is not considered any longer, for its inclusion will form a cycle with e and f.) The edge with the smallest weight is a, and so it is added to the tree. These four edges a, b, e, f form a spanning tree (see Figure 4.16(b)), which also turns out to be a minimal spanning tree. +
S
(a)
:J
(b) FIGURE 4.16
The method in Example 4.11 is due to Prim and will always produce a minimal spanning tree. Prim's algorithm builds a tree by selecting any vertex and then an edge of smallest weight on that vertex. The tree is then extended by choosing an edge of smallest weight that forms a tree with the previously chosen edge. This tree is extended further by choosing an edge of smallest weight that forms a tree with the two previously chosen edges. This process is continued until a spanning tree is obtained, which turns out to be a minimal spanning tree. This process can be formalized as follows.
4.2
Spanning Trees
201
Prim's Algorithm This algorithm finds a minimal spanning tree, if one exists, for a weighted graph with n vertices. In the algorithm, I is a set of edges that form a tree and £ is the set of vertices incident with the edges in T. Step 1 Step 2
Step 3
(select starting vertex) Select a vertex U, and set C = {U} and T = 0. (enlarge T) while some edge has one vertex in L and one not in £ (a) Choose an edge of smallest weight from among those with one vertex in L and the other not in £. (Ties can be broken arbitrarily.) (b) Place the edge in T. (c) Place its vertices in L (one of these is already in C). endwhile (is there a minimal spanning tree?) if JCl < n The graph is not connected, and so it has no minimal spanning tree. otherwise
The edges in T and their incident vertices form a minimal spanning tree. endif
In step 2 of Prim's algorithm, the selection of an edge with one vertex in L and the other not in L guarantees that there are no cycles formed by any collection of edges in T. Thus, at the end of each iteration of the loop in step 2, the edges in T and the vertices in L form a tree. Furthermore, when L contains all the vertices of 5, a spanning tree is formed. As usual, we will denote this tree by T. The proof that Prim's algorithm yields a minimal spanning tree is found at the end of this section.
cM
Example 4.12
Prim's algorithm will be applied to the weighted graph in Figure 4.17. We start with vertex F, and set L = (F) and T = 0. Since there are edges that have one vertex in C and the other not in C, we perform (a), (b), and (c) of step 2. The edges on F that do not have their other vertex in L are a, b, f, and g (see Figure 4.18), and, of these, a is the one of smallest weight. Therefore a is included in T and its vertices are included in L. Thus L = {F, C I and T = {a). Since there are edges that have one vertex in L and the other not in L, we continue step 2. The
202
Chapter4 Trees
A
C
9 Ae
B
X
d
i
C
m
g 9
31 D
3
N 6
11
1
E
D
FIGURE 4.17
E FIGURE 4.18
edges having exactly one vertex in L are b, d, e, f, and g. (See Figure 4.19.) Of these, e has the smallest weight and is therefore included in T, and the vertex E is included in L. Now L = {F, C, E} and T = la, el. Again there are edges with exactly one vertex in L, and so we continue step 2. This time the edges to consider are b, d, g, and j. (See Figure 4.20.) Notice that edge f is not considered, for it has both of its vertices in L. Of the edges b, d, g, and j, there are two with the smallest weight, namely b and d. Let us arbitrarily choose b and include it in T and Bin L. Thus L = IF, C. E, B} andT = {a, e, b}. B
8
d 8
-C
F
9f D
F3
D
L'
FIGURE 4.19
FIGURE 4.20
Again there are edges heating one vertex in C and the other not in L, and so step 2 continues. The edges with exactly one vertex in C are c, g, i, and j. Of these, both c and g have the smallest weight. Suppose that we choose c. Then we include c in T and A in C, making 1C={F,C,HB,Al
and
T ={a,e,b,cl.
As step 2 continues, the edges to consider are g, h, i, and j. The one with the smallest weight is h, and so it is inserted in T and D is inserted in L. Now C= {F,CE,B,A,D}
and
T = a,e,b,c,h).
Since there is no longer an edge with exactly one vertex in 1 (because C contains all the vertices of the weighted graph), we proceed to step 3. It tells us that the edges in T and their incident vertices form a minimal spanning tree, as illustrated in Figure 4.21. The weight of this spanning tree is 28. +
4.2 Spanning Trees A c
9
B
d
8 b
e
f J
D
C
a
9
203
3 11
E
FIGURE 4.21
There are two places in the example above where we have a choice of edges with the same least weight. Step 2(a) of the algorithm indicates that any edge of least weight could be chosen in such cases. If other choices are made, different minimal spanning trees would be constructed. For example, if in Example 4.12 we choose edge d instead of b, followed by the choices of g and h, the minimal spanning tree in Figure 4.22 results. Thus we see that minimal spanning trees need not be unique. A
c
9
B
d
8
C
5
e
a
33 D
11
1
E
FIGURE 4.22
Prim's algorithm is an example of what is called a greedy algorithm since at each iteration we do the thing that seems best at that step (extending a tree by including an available edge of smallest weight). In Prim's algorithm this approach does lead to a minimal spanning tree, although in general a greedy algorithm need not produce the best possible result. (See Exercises 36 and 37.) In analyzing the complexity of Prim's algorithm for a weighted graph with n vertices and e edges, we will consider comparing the weights of two edges as the basic operation. At each iteration of the loop in step 2, there will be at most e - 1 comparisons made in order to find an edge of smallest weight having one vertex in L and one vertex not in L. Step 2 is done at most n times, and so there are at most n(e
-
1) operations. Since e < C(n, 2) = -n(n - 1),
2
our implementation of Prim's algorithm is of order at most n3 . Another algorithm that can be used to find a minimal spanning tree is due to Kruskal. It is found in Exercises 4.2. Let us return to Figure 4.15. Suppose now that the weights of the edges measure the profit that results when oil is pumped through the corresponding pipelines. Our problem is to find a spanning tree of pipelines that generates the
204
Chapter 4
Trees
most profit. Thus we want a spanning tree for which the sum of the weights of the edges is not as small as po, sible, but as large as possible. A maximal spanning tree in a weighted graph is a spanning tree such that the weight of the tree is as large as possible. In other words, there is no spanning tree with larger weight. Fortunately, finding a maximal spanning tree is very similar to finding a minimal spanning tree. All that is needed is to replace the phrase 'an edge of smallest weight' by the phrase 'an edge of largest weight' in step 2(a) of Prim's algorithm. l
B
A
1(
C
I5 b h
F
8 11
D
E
D
FIGURE 4.23
1
11
E
FIGURE 4.24
Example 4.13 We will begin to construct a maximal spanning tree for the weighted graph in Figure 4.17 by picking the vertex F. Then T = 0 and L = (F . Examining the edges on F (see Figure 4.23). we pick one with the largest weight. This is the edge g, and so T = {g) and )< = IF, D}. The edges with one vertex in L and one vertex not in £ are a, b, J' h, i, and j (see Figure 4.24). Of these, there are two with the largest weight, i and j. We choose one arbitrarily, say i. So now T = {g, i} and £ =IF,D, B). Again the process is repeated (see Figure 4.25) by choosing edge j (the edge of largest weight having one vertex in L and one not in L). Now T = {g, i, j) anti f = {F, D, B, E}. Again we look at the edges with a vertex in L and one not in L:. Of these, c is the edge with the largest weight, and so T = {g, i, j, c} and L£= {F, D, B, E, Al. One more iteration yields the choice of the edge d; therefore, T = {g, i, j, c, d) and L = (F, D, B, E, A, C}, which is the set of all vertices. Hence T is a maximal spanning tree, as illustrated in Figure 4.26. The reader should check that the weight of this tree is 48. 4 A
c
9 B
d
8
C
11f
D
A
c
9B
d
ti i
FIGURE 4.25
1t
E
D
8
9
f
I
11
FIGURE 4.26
C
E
4.2 Spanning Trees
205
Now we will prove that if Prim's algorithm is applied to a connected weighted graph with n vertices, it actually produces a minimal spanning tree. Let T be as in the algorithm, that is, T is a set to which we add edges one at a time until we get a spanning tree. We will prove that this spanning tree is minimal by induction on m, the number of edges in T. The induction hypothesis will be that when T has m edges, then T is contained in some minimal spanning tree. If m = 0, then T is the empty set. Since some minimal spanning tree exists, the hypothesis holds for m = 0. Now suppose that T is a set with k edges, and that T is contained in a minimal spanning tree T'. Let L be the corresponding set of vertices given by Prim's algorithm, and suppose {U, V } is the next edge that it will put in T, where U is in L but V is not in L. If {U, V I is in f, then our induction hypothesis holds for k + 1, and we are done. Thus we suppose that {U, V} T'. Since TF is a spanning tree, there must be a path from U to V in T'. Since U E C and V £, this path must contain an edge e having one vertex in L and the other not. Because Prim's algorithm chooses {U, V} instead of e, the weight of {U, V) must be less than or equal to the weight of e. Thus if we form F' by adding {U, V } to T' and taking out e, we do not increase its weight. Since T' is connected and has n - I edges, it is also a minimal spanning tree. But T' contains the k + 1 edges of T U {{U, VI}. This proves the induction hypothesis for k + 1. By the principle of mathematical induction, we see that the tree T produced by Prim's algorithm is always contained in a minimal spanning tree. But when the algorithm ends and T has n - 1 edges, this spanning tree can only be T itself. Thus Prim's algorithm produces a minimal spanning tree. '
'
EXERCISES 4.2 In Exercises 1-6 use the breadth-first search algorithm to find a spanning tree for each connected graph. (Start with A and use alphabeticalorder when there is a choice for a vertex.)
1.
-
n
2.
A E L
E
206
Chapter4
Trees
4.
3.
a
5. -
-
6.
H C
D
7. At its refinery an oil company has 7 major buildings that are connected by underground tunnels, as illustrated below. Because of the possibility of a major explosion, there is a need to reinforce some of these tunnels to avoid a possible cave-in. The company wants to be able to go from any building to any other in case of a major fire above ground, but it wants to avoid reinforcing more tunnels than necessary. How can this be done?
8. One of the primary responsibilities of the National Security Agency is to assist other governmental agencies in providing secure computer communications. The Department of Agriculture does not ordinarily need to be concerned about this, but when estimates of future crop productions arrive, it is important that these be kept secret until the time of the public announcement. The map of computer links between reporting agencies for the Department of Agriculture is shown below. Realizing that there is a need for complete security only at certain times, the National Security Agency will make secure only the minimum number of lines. How can this be done?
4.2 Spanning Trees
207
B
G
H
9. Will any two spanning trees for a connected graph always have an edge in common? If so, give a proof, and if not, give a counterexample. 10. How many different spanning trees are there for a cycle with n vertices, where n > 3? 11. Prove that any edge whose removal disconnects a connected graph is part of every spanning tree. 12. Draw a spanning tree formed by applying the breadth-first search algorithm to Ka. 13. Draw a spanning tree formed by applying the breadth-first search algorithm to
JCm,n.
14. Can a spanning tree formed using the breadth-first search algorithm be a simple path? 15. A graph with vertices labeled 1, 2, .. , 9 is described below by giving its adjacency list. Determine if this graph is connected by using the breadth-first search algorithm.
1: 2,3,5,7,9 2: 1,3,4,5,9 3: 4: 5: 6: 7: 8: 9:
1,2,4,6,8 2, 3, 5, 6 1,2,6,7 3,4,5,7,9 1,5,6,8,9 3, 7 1,2,6,7
16. Repeat Exercise 15 with the adjacency list below. 1: 2, 5 2: 1,3 3: 2, 6
4:5,6 5: 1, 4 6: 3,4 7: 8, 9 8: 7, 9 9: 7, 8
208
Chapter4
Trees
Throughout the remaining exercises, if there is a choice of edges to use informing a minimal or maximal spanning tree, select edges accordingto alphabeticalorder. In Exercises 17-20 use Prim's algorithm to find a minimal spanning tree for each weighted graph. (Start at A.) Give the weight of the minimal spanning treefound. 17.
A
a
3
B
18.
A
a
4
B
de
f
3.
3
2
,
3
D
h
3
X h
5 F
19.
B 4 3 C
9
b 6 40A d
3
I
2 G
3
D
i-X2
j m
4l H
:X l
A
i
6 k 4
E
h
f
e
c
b
4
C
,
3
J
HA
1
2U.
f i 2 3 e
D
2 E
m
4
F
In Exercises 21-24 use Prim's algorithm to find a minimal spanning tree for the weighted graphs in the indicated exercises. Give the weight of the minimal spanning tree found. 21. Exercise 17 (Start at E.)
22. Exercise 18 (Start at H.)
23. Exercise 19 (Start at G.)
24. Exercise 20 (Start at H.)
In Exercises 25-28 use Prim's algorithm to find a maximal spanning tree for the weighted graphs in the indicated exercises. Give the weight of the maximal spanning tree fouad. 25. Exercise 17 (Start at A.) 27. Exercise 19 (Start at F.)
26. Exercise 18 (Start at A.) 28. Exercise 20 (Start at D.)
29. The Gladbrook Feed Company has 7 bins of corn that must be connected by grain pipes so that grain can be moved from one bin to another. To minimize the cost of construction, it wants to build as few grain pipes as possible. The cost (in hundreds of thousands of dollars) of building a pipeline between two bins is given in the following table, where a '-' indicates no pipeline, can be built. How can the pipes be built at minimal cost?
4.2 Spanning Trees Bin
1
2
1
-
4
2
4
-
3
-
5
4
6
2
5 6 7
2
-
3
3
4
5
6
7
6
2
-
3
3
1
2
2
5 -
2 7
7
-
4
-
4
3
2
1
1
2
1
-
I 1
-
209
-
-
2
2 -
30. FBI Special Agent Hwang is working with 5 informants who have infiltrated organized crime. She needs to make arrangements for the informants to communicate with each other, either directly or through others, but never in groups of more than two. For reasons of security, the number of meeting places must be kept as small as possible. Furthermore, each pair of informants has been assigned a danger rating (given in the table below) which indicates the risk involved in their being seen together. How can Special Agent Hwang arrange communication so as to minimize the danger? Assume the danger is proportional to the sum of the danger ratings of the individuals who meet directly. Jones
Brown
Hill
Ritt
Chen
3
4
5
2
3
-
3
1
4
4
3
-
2
3
Ritt
5
1
2
Chen
2
4
3
Jones
-
Brown Hill
-
4 4
31. Give an example of a connected weighted graph (not a tree) where the same edge is part of every minimal spanning tree and every maximal spanning tree. 32. Modify Prim's algorithm to find a spanning tree that is minimal with respect to those containing a specified edge. Illustrate your modification with edge g in Exercise 17. 33. Repeat the second part of Exercise 32 with edge b in Exercise 19. 34. If the weights in a connected graph correspond to distances, does a minimal spanning tree give the shortest distance between any two vertices? If so, give a proof. If not, give a counterexample. 35. Can a minimal spanning tree in a connected weighted graph (not a tree) contain an edge of largest weight? If so, give an example. If not, give a proof. 36. In the knapsack problem of Section 1.3, explain why choosing the experiment with the highest rating does not give a good procedure. 37. Suppose that we want to mail a package and have stamps worth 1, 13, and 22 cents. If we want to make the necessary postage with the minimum number of stamps, the greedy algorithm approach is to use as many 22-cent stamps as possible, then as many 13-cent stamps as possible, and, finally, the necessary number of 1-cent stamps. Show that this approach need not result in the fewest stamps being used.
210
Chapter 4
Trees
Kruskal's Algorithm This algorithm finds a minimal spanning tree, if one exists, for a weighted graph n > 2. In the algorithm, S and T are sets of edges of g
g with n vertices, where
Step 1 (initialization) (a) Set T =0. (b) Let S be the set of all edges of g Step 2 (enlarge T) while ITI < n 1 and S is not empty (a) Choose an edge e of smallest weight from S. (Ties can be broken arbitrarily.) (b) If no cycle is formed by any of the edges in 'T U fel, then replace T with T U [el. (c) Remove e from S. endwhile Step 3 (is there a minimal spanning tree?) if ITI
In Exercises 38-41 use Kruskal's algorithm to find a minimal spanningtree for the weighted graphs in the indicated exercises.
3'). Exercise 18 41. Exercise 20
38. Exercise 17
40. Exercise 19
42. Modify Kruskal's algorithm to find a spanning tree that isi minimal with respect to all those containing a specified edge. Illustrate your modification with edge d in Exercise 17. 43. Repeat the second part of Exercise 42 with edge b in Exercise 19. 44. Prove that Kruskal's algorithm gives a minimal spanring tree for a connected weighted graph. 45. Prove that if the weights in a connected weighted graphic are all different, then the weighted graph has exactly one minimal spanning tree.
4.3
do
DEPTH-FIRST SEARCH In Section 4.2, we saw how breadth-first search can be used to find a spanning tree in a connected graph. TI'is algorithm starts from one vertex and spreads out
4.3 Depth-FirstSearch
211
to all the adjacent vertices. From each of these, we spread out again to all the adjacent vertices that have not been reached and continue in this fashion until we can go no further. In this way, we obtain the distance from the initial vertex to each vertex and also a spanning tree. Another algorithm for finding a spanning tree in a connected graph is the depth-first search algorithm. In this algorithm, we label the vertices with consecutive integers that indicate the sequence in which the vertices are encountered. The underlying idea of the algorithm is that, to find the vertex that should be labeled immediately after labeling vertex V, the first vertices to consider are the unlabeled ones adjacent to V. If there is an unlabeled vertex W adjacent to V, W is assigned the next label number, and the process of searching for the next vertex to label is begun with W. If V has no unlabeled adjacent vertices, we back up along the edge that we traveled to label V and continue backing up, if necessary, until we reach a vertex having an unlabeled adjacent vertex U. Vertex U is then assigned the next label number, and the process of searching for the next vertex to label is begun with U. The key idea in the depth-first search algorithm is to back up when we have gone as far as we can. As an example of this process, consider the graph in Figure 4.27. We will assign to each vertex V a label that indicates both the sequence in which V is labeled and its predecessor (the vertex that we came from to reach V). We start at any vertex, say A, and assign it the label I(-) to indicate that it is the first vertex labeled and that it has no predecessor. Then, of the two adjacent vertices B and D, we arbitrarily choose one, say B, and give it the label 2(A). Next, of the two unlabeled vertices adjacent to B, we arbitrarily choose C and give it the label 3(B). Since C is a vertex with no unlabeled adjacent vertices, we back up to B, the predecessor of C, and go next to D, giving it the label 4(B). When all the vertices are labeled, we can construct a spanning tree for the graph by selecting the edges (and their incident vertices) that join each vertex to its predecessor. These edges are shown in color in Figure 4.27. 3(B)
D 4(B) FIGURE 4.27
The following example demonstrates this technique on a more complicated graph.
212
Chapter 4
Trees
+
Example 4.14 We will find a spanning tree for the graph in Figure 4.28 by using the depth-first search process. In this example, we will follow the convention that when there is a choice of vertices, vertices will be chosen in alphabetical order. We begin by selecting a starting vertex, arid using our convention, we choose A. Thus A is assigned the label 1(-), indicating that it is the first vertex labeled and that it has no predecessor. Now we select an unlabeled vertex adjacent to A. The possibilities are B and G, and, according 10 our convention, we choose B and assign it the label 2(A). (As illustrated in Figure 4.29, we display each vertex's label near the vertex, and show in color the edge joining a vertex to its predecessor.) 1(-)
I
FIGURE 4.28
FIGURE 4.29
We now continue from B, selecting an adjacent unlabeled vertex from among F, J, and H. Here we choose F, and assign it the label 3(B). Continuing from F, we select C and give it the label 4(F). Next, we select D and label it 5(C). The present situation is shown in Figure 4.30. At this point, there are no unlabeled vertices adjacent to D, the last labeled vertex. Thus we must back up from D to its predecessor C. Since there are unlabeled vertices adjacent to C. we select one, namely E, and label it 6(C). The current situation is shown in Figure 4.31. 1(-)
4(F)
2(A B
5(C) D
1(-) A
3(B) F
2(A) B
J
H FIGURE 4.30
I
J
4(F) C
G
4
H FIGURE 4.31
6(C)
1E
I
5(C) D
4.3 Depth-First Search
213
Because there are no unlabeled vertices adjacent to E, we back up to the predecessor C of E and select G next, giving it the label 7(C). See Figure 4.32. Once again we must back up, because there are no unlabeled vertices adjacent to G. Thus we return to C, the predecessor of G. This time, however, there are no unlabeled vertices adjacent to C, and so we are forced to continue backing up to F. the predecessor of C. Since there are unlabeled vertices adjacent to F. we continue labeling from F. We next select H and label it 8(F). Continuing from H, we choose I and give it the label 9(H). The present situation is shown in Figure 4.33. I
7(C)
4(F)
H
1
1(-)
7(C)
4(F)
I
H 8(F)
I 9(H)
5(C)
5(C)
2(A) B
J
FIGURE 4.33
FIGURE 4.32
Because there are no unlabeled vertices adjacent to I, we back up to H. Now we select J and assign it the label 10(H). At this point, every vertex is labeled (see Figure 4.34), and so we stop. In Figure 4.34, the colored edges (and their incident vertices) form a spanning tree. + 1(-) A
2(A)
7(C) G
4(F) C
5(C) D
3()6C
BG
J 10(H)
H 8(F)
I 9(H)
FIGURE 4.34
The procedure used in Example 4.14 is formalized below.
214
Chapter 4
Trees
Depth-First Search Algorithm This algorithm finds a spanning tree, if one exists, for a graph 5 with at least two vertices. In the algorithm, £ is the set of vertices with labels, the predecessorof vertex Y is a vertex in £ that is used in labeling Y, and T is the set of edges that join each vertex to its predecessor. Step I
Step 2
Step 3
(label the starting vertex) (a) Select a vertex U, assign U the label 1, and let U have no predecessor. (b) Set £ = {U) and T = 0. (c)Setk =2andA = U. (label other vertices) repeat Step 2.1 (label a -ertex adjacent to X) while there is a vertex Y not in £ that is adjacentto X (a) Place the edge (X, YJ in T. (b) Assign X to be the predecessor of Y. (c) Assign Y the label k. (d) Include Y in L. (e) Replace k with k + 1. (f) Now let X de note the vertex Y. endwhile Step 2.2 (back up) Replace X with the predecessor of X. until X = null or every vertex of 5 is in L (is there a spanning tree'?) if every vertex of Gis in £ The edges in T and their incident vertices form a spanning tree
of g. otherwise There is no spanning iree for g because G is not connected. endif .
~~~~~~~~~~~~~~~~~~.
... . A.... ....--..... ..... ,S:,.tS.,SS.S:
There is a fundamental difference between breadth-first search and depth-first search. With breadth-first search, we fan out from each vertex to all the adjacent vertices, and this process is repeated at each vertex. Furthermore, at no time do we back up in order to continue -he search. But with depth-first search, we go out from a vertex as far as we can, and when unable to continue, we back up to the most recent vertex from which there was a choice; then we resume going out as far as we can. An analogous situation can be found in two different ways to explore a cave with many tunnels. With the breadth-first search approach, a posse searches the
4.3 Depth-FirstSearch
215
cave and whenever a tunnel branches off into several others, subgroups are formed to explore each of these simultaneously. With the depth-first search approach, one person explores the cave by leaving a phosphorous trail to mark where she has been. When there is a choice of tunnels, an unexplored one is chosen at random to be explored next. Upon reaching a dead end, she backtracks using the marked trail to find the next unvisited tunnel. Theorem 4.7
Let the depth-first search algorithm be applied to a graph 5. (a) (b)
The edges in T and the vertices in L form a tree. Furthermore, if g is connected, this tree is a spanning tree.
Proof. (a) By the construction process of depth-first search, the edges of T and the vertices in L form a connected graph. In step 2 each time an edge is selected to be placed in T, one vertex is in L: and the other is not in 12. Thus this selection does not create any cycles using the other edges in T. Consequently, at the end
of the depth-first search algorithm, the graph formed by the edges in T and the vertices in L contains no cycles and is, therefore, a tree. The proof of part (b) is left as an exercise. W
We will follow our convention and refer to the tree in Theorem 4.7 formed by the edges in T and the vertices in 1 as simply T. The tree T is called a depth-first search tree. The edges in T are called tree edges and the other edges are called back edges. The labeling of the vertices is called a depth-first search numbering. Thus, in Figure 4.34 vertex F has depth-first search number 3, the edge on vertices F and C is a tree edge, and the edge on vertices F and G is a back edge. Of course, the designation of edges as tree and back edges, as well as the depth-first search numbering, depends upon the choices made during implementation of the algorithm. In order to analyze the complexity of the depth-first search algorithm, we will regard labeling a vertex and using an edge as the elementary operations. For a graph with n vertices and e edges, each vertex is labeled at most once and each edge is used at most twice, once in going from a labeled vertex to an unlabeled vertex and once in backing up to a previously labeled vertex. Hence, there will be at most n+2e
2
operations, and thus this algorithm is of order at most n2 .
Depth-first search can be used in many ways to solve problems involving graphs. Some will be presented below. Following Example 3.41 in Section 3.5, we investigated the problem of how
to assign directions to the edges of a graph to create a strongly connected directed graph (a directed graph having a directed path between any two vertices). We stated that the absence of an edge (called a bridge) whose removal disconnects the graph is necessary and sufficient to guarantee that there is a way to assign directions to edges so as to produce a strongly connected directed graph. However,
216
Chapter 4
Trees
no procedure was given to determine if a graph has a bridge. We will now describe how depth-first search can be used for this purpose. We first apply depth-first search to a connected graph g to obtain a spanning tree T. Observe that a bridge in 5 must be one of the edges in T. Now successively delete each edge in T from g and apply depth-first search to see if the resulting graph is connected. If not, the deleted edge must be a bridge. Next we will show hown to assign directions to the edges of a connected graph 5 with no bridge so that g becomes a strongly connected directed graph. We begin by applying a depth-first searcli to 5 and assigning directions to the tree edges by going from the lower depth-first search number to the higher. Then we assign directions to back edges by going from the larger depth-first search number to the smaller. With these assignmerlt, a strongly connected directed graph is formed. M
Example 4.15 The depth-first search algorithra has been applied to a graph to yield the depthfirst search numbering in Figuie 4.35(a), where the tree edges are colored blue and the back edges black. Now we assign directions to all the edges as described above, producing the directed graph shown in Figure 4.35(b). Examination shows that it is strongly connected since there is a directed path from every vertex to every other vertex. + 2
5
(a)
2
5
(b)
FIGURE 4.35
This example illustrates the following theorem.
Theorem 4.8
Suppose depth-first search is applied to a connected graph without a bridge. If directions are assigned to tree edges by going from the lower depth-first search number to the higher and to back edges by going from the higher number to the lower, then the resulting graph is strongly connected.
4.3
Depth-FirstSearch
217
The process of going as far as possible before backing up is called backtracking when used as a general problem-solving strategy. It is often used as a systematic way to explore a large set of possibilities when looking for a solution or for all the solutions with particular characteristics. We will illustrate the idea of backtracking with two examples. Although backtracking may not seem to be directly related to the depth-first search algorithm as applied to graphs, the set of possible solutions for a problem can often be represented as a graph. Depth-first search can then be used as a systematic way to search for solutions. This idea is illustrated in the first example below.
Example 4.16 In the 4-queens problem we are asked to place 4 queens on a 4 x 4 chessboard so that no two queens can attack one another. This means that we must place 4 tokens on a 4 x 4 grid so that no two tokens are in the same row, column, or diagonal. We shall show how backtracking can be used to find a solution to this problem. First we observe how the placement of the queens can be described by a graph. Each vertex will represent a placement of n (n > 0) nonattacking queens placed in consecutive columns from left to right. An edge will connect two vertices when the two configurations differ by the placement of one additional queen. To help identify locations on the chessboard, we will think of it as a 4 x 4 matrix. We begin by placing a queen in the 1, 1 position. Then in column 2, the only acceptable positions for a queen are positions 3, 2 or 4, 2 because position 1, 2 would result in two queens in the same row and position 2, 2 would result in two queens in the same diagonal. With the choice of the queen in position 3, 2 no further placements can be made, while the placement in position 4, 2 allows for the placement of another queen in position 2, 3. The rest of the graph can be completed in the same way, giving the graph in Figure 4.36. Now depth-first search can be used to search this graph for a solution to the problem, that is, a placement containing 4 nonattacking queens. In practice, however, it is often desirable to search for solutions to a problem without first constructing a graph like that in Figure 4.36. We will illustrate this technique by reworking the 4-queens problem described above. Our overall general search strategy will be to place queens on the chessboard in columns from left to right and in rows from top to bottom. If we are unable to place a queen in a column, we backtrack to the previous column and change the location of the queen there. If that does not work, we go back one more column. As before, to identify locations on the chessboard, we will think of it as a 4 x 4 matrix. We begin by placing a queen in the 1, 1 position. (See Figure 4.37(a)). Then, going from top to bottom in column 2, we place a queen in position 3, 2
218
Chapter4
Trees
FIGURE 4.36
(Figure 4.37(b)) because position 1, 2 would result in two queens in the same row and position 2, 2 would result in two queens in the same diagonal. However, we now see that there i, no spot in which to place a queen in column 3. So we backtrack to the queen in column 2 and move the queen to position 4, 2 (Figure 4.37(c)). Then in column 3 we are able to place a queen in position 2, 3 (Figure 4.37(d)). However, we are now unable to find a position in column 4 in
4.3 Depth-FirstSearch
219
(e
FIGURE 4.37
which to place a queen. Since the location chosen in column 3 for the queen is the only one possible, it is necessary to backtrack to column 2. But again there is no place (other than position 3, 2, which was previously considered) in which to move the queen in column 2. So we must backtrack all the way to column 1 and move the queen in column 1 to position 2, 1 (Figure 4.37(e)). Then the only location in column 2 for a queen is position 4, 2, the only location for a queen in column 3 is position 1, 3, and for column 4 the only location is position 3, 4 (Figure 4.37(f)). Thus a solution to the 4-queens problem has been found. + Example 4.17 Another use of backtracking is in providing a systematic way to go through a maze such as in Figure 4.38, where the colored lines indicate barriers. Since at each location we have four possible choices to make, our general strategy will
220
Chapter4 Trees
be to go east, north, west, or south in that order without returning to a previously occupied location. When we can go no further, we backtrack to the last location where there was a choice and foIlow our priority list of directions. Again we will use matrix notation to describe locations in the maze. From the starting position 1, 1 we go east, east, and then 5.outh, ending up in location 2, 3. We are unable to proceed any further from this location, and so we back up to location 1, 2. Then we go south, west, south, east, east, east, north, and north, ending in location 1, 4. Again unable to go any further. we backtrack to location 3, 1. Then we go south, east, east, and east, ending up at position 4, 4. +
Start
Finish FIGURE 4.38
For other applications of depth-first search, the interested reader should consult suggested reading [9] at the end of the chapter.
EXERCISES 4.3
4
1'
Throughout these exercises, if there is a choice of vertices choose the vertex that appearsfirst in alphabetical order. In Exercises 1-6 apply depth-first search to each graph to obtain a depth-first search numbering of the vertices. 2.
1.
A
C4
H
D
E
D
G
,-B --
if
J
L
I
K
4.3 Depth-FirstSearch A
3.
221
J
4.
B
F F
5. H
I
E
B
B
6.
I
A C F
G
D
J
In Exercises 7-12 use the depth-first search numbering obtained for the graph in the indicated exercises to form the spanning tree described in Theorem 4.7. 7. Exercise 1
8. Exercise 2
9. Exercise 3
10. Exercise 4
11. Exercise 5
12. Exercise 6
In Exercises 13-18 use the depth-first search numbering obtained in the indicatedexercises to list the back edges in the graphs.
13. Exercise 1
14. Exercise 2
15. Exercise 3
16. Exercise 4
17. Exercise 5
18. Exercise 6
In Exercises 19-22 determine if there are any bridges using the discussion preceding Example 4.15.
20.A
19.
D
C
E
D
B
C
F
222
Chapter4
21.
A
Trees
22.
B E
F
In Exercises 23-26 use Theorem 4.8 and the depth-first search numbering obtained in the indicated exercise to assign directions to the edges that will make each graph intc a strongly connected directed graph. 23. Exercise 1
24. Exercise 2
25. Exercise 4
26. Exercise 6
27. The city manager of a community with a large university believes that something needs to be done to handle the large influx of automobile traffic on those days that stadents are checking into the dormitories. She instructs the chief of police to transform the current two-way street system into a system of one-way streets to handle the extra traffic, with the provision that students can still get from any place to any other. The campus area is given below. Is there a way for the chief of police to can y out these instructions? If so, how? A
C
28. Can the spanning trees for a connected graph formed by breadth-first search and depth-first search be the same? 29. Prove part (b) of Theorem 4.7. 30. Show that every strongly connected directed graph with more than one vertex has at least one additional orientation of its edges under which it is strongly connected. 31. Label the vertices of IC3 as 1, 2, 3. Apply depth-first search to C3, starting at 1. How many different depth-first search numberings are there? 32. Label the vertices of k 4 as 1, 2, 3,4. Apply depth-first search to search numberings are there?
K4, starting at 1. How many different depth-first
33. Label the vertices of IC, as 1, 2, .. , n. Apply depth-first search to IC, starting at 1. How many different depth-first search numberings are there? 34. Suppose breadth-first search and depth-first search are applied to a connected graph starting at the same vertex. If b is the label assigned to a vertex by breadth-first search and d is the label assigned to the same vertex by depth-first search, what is the relationship between 1)and d? Why?
4.4 Rooted Trees
35. Let depth-first search be applied to a connected graph every back edge is contained in a cycle of 5.
223
g. Prove that every cycle of g contains a back edge, and
A vertex A is called an articulation point of a connected graph g when the deletion of A and the edges incident on A creates a graph that is not connected. For example, the vertex A is an articulation point of the graph below.
36. Prove that A is an articulation point of a connected graph g if and only if there exist vertices U and V such that U, V, and A are distinct and every path between U and V contains the vertex A. 37. Use backtracking to show that there is no solution to the 2-queens problem. 38. Use backtracking to show that there is no solution to the 3-queens problem. 39. Use backtracking to find a solution to the 5-queens problem. 40. Use backtracking to show that it is not possible to fit 7 dominoes (consisting of two unit squares) into a 4 x 4 chessboard that is missing opposite corners. 41. Use backtracking to construct a sequence of length 8 composed of the digits 1, 2, 3 with the property that nowhere in the sequence are there two adjacent subsequences that are identical.
4.4
o
ROOTED TREES People have always been interested in learning about the descendants of historically important individuals. To assist in these investigations, a genealogical chart is often drawn. An example is given in Figure 4.39, where for simplicity only first names are used. It is understood that the downward lines represent the 'is a parent of' relationship. Peter Godfried Mary
Marvin
Albert
Betty Doris Judy
Denise
Hal
FIGURE 4.39
Gregory
224
Chapter 4 Trees
This chart can also be represented by a directed graph in which vertices represent individuals and directed edges begin at a parent and end at a child. Such a directed graph is shown in Figure 4.40.
s Gregory
Judy
Hal
FIGURE 4.40
Since all the arrows in Figure 4.40 point downward, it is not really necessary to draw the arrowheads on the edges as long as the directions are understood to be downward. Figure 4.41 shows the corresponding directed tree without these arrowheads. Peter
Ma
iGregory
Hal FIGURE 4.41
For the directed graph in Figure 4.40, there is one vertex with indegree 0, and all the other vertices have indegree 1. Furthermore, when the directions on the edges are ignored, we have a tree. A rooted tree is a directed graph f satisfying two conditions: (1) When the directions of the edges in 'F are ignored, the resulting undirected graph is a tree; and (2) there is a unique vertex R such that the indegree of R is 0 and the indegree of any other vertex is .- This vertex R is called the root of the rooted tree. The directed graph in Figure 4.40 is a rooted tree with Peter as its root. We will follow the customary practice of drawing rooted trees with the roots at the
4.4 Rooted Trees
225
top and omitting arrowheads on the directed edges, with the understanding that edges are directed downward. +
Example 4.18 The graph in Figure 4.42(a) is a rooted tree with root A since (1) when the directions on the edges are ignored, the resulting graph is a tree; and (2) A has indegree 0, and all the other vertices have indegree 1. The usual way of drawing this tree is shown in Figure 4.42(b). + A
En
B
A
C
A-
G
F
D
HI (a)
(b) FIGURE 4.42
Rooted trees are often used to describe hierarchical structures. One such example occurs with the family tree of Peter. Another example is given below.
Example 4.19 A rooted tree can be used to describe the organization of a book by using 'book' as the root and other vertices as subdivisions. In some books there are subsections of a section, and so another level of vertices could be added in this case. See Figure 4.43 for an illustration. + Book
Sec I Sec 2 Sec 3 Sec I Sec 2
Sec I
Sec 2
FIGURE 4.43
The following theorem gives some properties of rooted trees.
226
Chapter4
Trees
Theorem 4.9
In a rooted tree: (a) The number of vertices is one more than the number of directed edges. (b) There are no directed cycles. (c) There is a unique simple directed path from the root to every other vertex. Proof. Suppose T is a rooted tree with root R. The proofs of (a) and (b) follow immediately because T is a tree if the directions on the directed edges are ignored. Next we will show that there is a directed path (and, hence, there will be a simple directed path) from R to any other vertex V =A R. Since the indegree of V is 1, there is a vertex l'1 $ V and a directed edge from VI to V. If V, R, we are finished. If not, since the indegree of V, is 1, there is a vertex V2 7 V, and a directed edge from V2 to VI. Since there are no directed cycles, V2 7 V. If V 2 = R, then we are done. Otherwise, this process can be repeated with each iteration generating a new verlex. Since the number of vertices is finite, we must eventually reach R. Thus we create a directed path from R to V. The uniqueness of a simple directed path from R to V follows immediately as in parts (a) and (b). Family terms are used to de.s cribe the relationships among vertices in a rooted tree, just as they describe relationships in a genealogical chart. If in a rooted tree there is a directed edge from a vertex U to a vertex V, we say U is a parent of V or V is a child of U. For a vertex V, the vertices other than V on the directed simple path from the root to V are called the ancestors of V, or, equivalently, we say that V is a descendent of these vertices. A terminal vertex is a vertex that has no children, and an internal vertex is one that has children. For the rooted tree in Figure 4.42, E is a child of G, and A, F, and G are ancestors of E. Also, F has H, G, and E as its descendants. Vertices B, D, E, and H are terminal vertices, and the others are internal vertices. Note that, in any rooted tree, the root has no ancestors, and every other vertex is a descendant of the root. A terminal vertex is a vertex w Itl outdegree 0, and an internal vertex has nonzero outdegree. We will now consider two examples where a rooted tree is used to obtain a solution to a problem.
+
Example 4.20 In Chapter 2 the concept of a partition of a set was introduced. To list all the partitions of {1, 2, . . ., n} requires a systematic approach, so as not to miss any possibility. The rooted tree in Figure 4.44 shows one such approach for n = 4. Here the terminal vertices are the partitions. Do you recognize the pattern? The children of {1,31, {2} are 1,3,4), {2}; {1,3}, {2,4}; and {1,3}, {2}, {4}. +
4.4 Rooted Trees
227
I
FIGURE 4.44
Example 4.21 Suppose we have seven identical coins and an eighth that looks the same but is heavier. With the use of a balance scale, we want to identify the counterfeit coin in the smallest number of weighings. Let us label the coins 1, 2, .. , 8. Note that when coins are placed on the two sides of the balance scale, either the left side will go down, the two sides will balance, or the right side will go down. We can construct a rooted tree as in Figure 4.45 giving a systematic approach for weighing the coins. The label beside each vertex indicates which coins are being weighed on each side of the balance scale. For example {1, 2}-{3, 4} means that coins 1 and 2 are weighed on the left side and coins 3 and 4 on the right. If the right side goes down, we proceed to the child on the right side for the next weighing, and similarly when the left side goes down. The terminal vertex indicates the heavy coin. For example, we begin by comparing the weight of coins 1, 2, 3, and 4 on the left with the weight of coins 5, 6, 7, and 8 on the right. If the balance tips to the left, we then compare coins 1 and 2 against coins 3 and 4. If in this weighing the right side goes down, we next compare coins 3 and 4. If this weighing shows the right side going down again, we reach the terminal vertex indicating that 4 is the counterfeit coin. Since each terminal vertex is at the end of a simple directed path of length 3 from the root, we see that this scheme requires three weighings to find the counterfeit coin. Could there be a different approach that will find the counterfeit coin with fewer weighings? Since a balance scale has three possible outcomes, we can build a rooted tree in which there are three children rather than just two as was done above. Figure 4.46 gives one such possibility, where we proceed to the middle child when the two sides balance. Here, because each terminal vertex is at the end of a simple directed path of length two from the root, we can find
228
Chapter4
Trees
18,
MII
121
:31
(4)
15)
(61
17)
18)
FIGURE 4.45
the counterfeit coin with just two weighings. The trees in Figures 4.45 and 4.46 are called decision trees because of the way they structure a decision-making process. +
{1}
{2}
{3}
{4}
{5}
{6}
{7}
{8}
FIGURE 4.46
We will finish this section by describing the relationship between depth-first search and rooted trees. We %N ill use from Section 4.3 the definitions and the notation of Theorem 4.7.
Theorem 4.10
If the depth-first search algorithm is applied to a graph, then the edges in T, when oriented from the lower depth-first search number to the higher, form a rooted tree whose root is the vertex with depth-first search number 1. Proof Theorem 4.7 shows that T is a tree. Let R be the vertex with depth-first search number 1. Only during step 2.1 of depth-first search is a vertex assigned a depth-first search number and a tree edge going into it. This means that the root R has indegree 0, and each vertex in the tree other than R has indegree 1. N
+
Example 4.22 In Figure 4.47(b), the vertices are labeled using a depth-first search numbering obtained by applying depth-iilst search to the graph in Figure 4.47(a). If we
4.4 Rooted Trees
229
assign directions to the tree edges as described in Theorem 4.10 and delete the back edges, we obtain the rooted tree in Figure 4.47(c). +
(a) 10
10
6 I
2
2 (b)
(c) FIGURE 4.47
The ideas in Example 4.22 can also be used to find bridges in a graph. The interested reader should consult suggested reading [3] for more details.
EXERCISES 4.4 In Exercises 1-8 determine if each directed graph is a rooted tree. 1.
2-
3.
4.
A
A2
230
Chapter4 Trees
5.
6.
7.
8.
I
I
I
I
I
II
I L
I
In Exercises 9-12 draw the rooted trees in the indicated exercises in the usual way, with the root at the top and without the arrowheads. 9. Exercise 1
10. Exercise 4
11. Exercise 6
12. Exercise 8
13. LISP is the primary programming language used in artificial intelligence. There are seven objects manipulated by LISP: S-expressions, atoms, lists, numbers, symbols, fixed-point numbers, and floating-point numbers. An S-expression can be an atom or a list, an atom can be a number or a symbol, and a number can be either fixed-point or floating-point. Draw a rooted tree describing these relationships. 14. Draw a rooted tree for your mother and her descendants. 15. Tom and Sue are first cousins living in a state that allows first cousins to marry. If a child is born to this marriage, what effect would this have upon a genealogical chart in which the root is Tom and Sue's common grandfather? 16. It is known that a male bee has only a mother and teiat a female bee has both a mother and father. Draw a rooted tree giving the ancestors of a male bee for four generations back, assuming no mating between ancestors. 17. Write an algorithm describing how a tree with a vertex labeled R can be transformed into a rooted tree with root R. Illustrate your algorithm on the tree below.
4.4 Rooted Trees
18. Repeat the second part of Exercise 17 for the following tree. 0 0
1
4I
R
0
41
I
ii
iP
0
19. In how many ways can a tree with a vertex labeled R be transformed into a rooted tree with root R? In Exercises 20-23 listfor each rooted tree: (i) the root (ii) the internal vertices (iii) the terminal vertices (iv) the parentof G (v) the children of B (vi) the descendents of D (vii) the ancestorsof H.
20.
A
B
E
21.
A
23.
E
D
F.G
H
l
22.
B I
K G
F H
C
231
232
Chapter4
Trees
24. Draw a rooted tree with 7 vertices having as many terminal vertices as possible. 25. Draw a rooted tree with 7 vertices having as many internal vertices as possible. 26. Use Figure 4.44 to determine the number of partitions of {1, 2, 3,4, 5}. 27. Draw a rooted tree describing all the possible outconmes for a two-game match between two chess players. (Remember that a chess game can end in a win, draw, or loss.) 28. Draw a rooted tree showing how to sort letters having a three-digit zip code in which the digits are 1 and 2. 29. Suppose we have three identical coins and a fourth that ooks the same but is lighter. Construct a decision tree that will find the counterfeit coin using no more than two weighings on a balance scale. 30. Suppose we have eleven identical coins and a twelfth that looks the same but is lighter. Construct a decision tree that will find the counterfeit coin using no more than three weighings on a balance scale. 31. Suppose we have three identical coins and a fourth that looks the same but is heavier or lighter. Construct a decision tree that will find the counterfeit coin and detennine if it is heavier or lighter using no more than three weighings on a balance scale. 32. Suppose we have seven identical coins and an eighth [hat looks the same but is counterfeit (either heavier or lighter). Construct a decision tree that will find the counterfeit coin and determine if it is heavier or lighter using no more than three weighings on a balance scale. 33. In a rooted tree, the level of a vertex is defined to be the .ength of the simple directed path from the root to that vertex. What is the level of the root? 34. How many rooted trees are there with 2 vertices? With 'i vertices? With 4 vertices? In Exercises 35-38 determine the level of each indicatedv,?t tex. (See Exercise 33 for the definition of 'level.') 35. vertex F in the rooted tree of Exercise 20
36. vertex L in the rooted tree of Exercise 21
37. vertex H in the rooted tree of Exercise 22
38. vertex F in the rooted tree of Exercise 23
39. For the tree obtained by applying depth-first search to a connected graph, prove that the descendents (relative to the depth-first search tree) of any vertex V have larger depth-first search numbers than V.
4.5
+
BINARY TREES AND TRAVERSALS Expl! e, sA i;;ln.~ trees In previous examples and applications of rooted trees, it was not necessary to distinguish between the children of a parent. In other words, there was no need to designate a child as the first cii ld or the second child. However, there are many situations where it is necessary to make such a distinction. For example, in an arithmetic expression such as A - B, the order of A and B is important. Thus if we represent A - B by a rooted tree in which the root represents the operation (-) and the children represent the operands (A and B), then the order of the children is important.
A binary tree is a rooted tiee in which each vertex has at most two children and each child is designated as being a left child or a right child. Thus, in a
4.5 Binary Trees and Traversals
233
binary tree, each vertex may have 0, 1, or 2 children. When drawing a binary
tree, we will follow customary practice and draw a left child to the left and below its parent and a right child to the right and below its parent. The left subtree
of a vertex V in a binary tree is the graph formed by the left child L of V, the descendents of L, and the edges connecting these vertices. The right subtree of
V is defined in an analogous manner. Example 4.23
For the binary tree in Figure 4.48(a), A is the root. Vertex A has two children, a left child B and a right child C. Vertex B has one child, a left child D. Similarly, C has a right child E but no left child. The binary tree in Figure 4.48(b), in which A has a left child B, is different from the one in Figure 4.48(c), in which B is a right child. For the binary tree in Figure 4.48(d), the left subtree of V is shown in color in Figure 4.48(e), and the right subtree of W is shown in color in Figure 4.48(f). The right subtree of V consists of the vertex U alone. + A
A
A
B
(a)
(d)
B (b)
(e)
(C)
(D
FIGURE 4.48
Binary trees are used extensively in computer science to represent ways to organize data and describe algorithms. For example, during the execution of a computer program it may be necessary to evaluate arithmetic expressions such as (2 - 3 4) + (4 + 8). Our knowledge of the conventions for the order of operations tells us how to proceed with this calculation: Scan from left to right, first doing multiplication and division and then addition and subtraction, with the understanding that parentheses have priority. However, when an expression needs
234
Chapter4
Trees
to be evaluated frequently, this method cannot be used efficiently by a computer. An alternate approach is to represent an arithmetic expression by a binary tree and then process the data in sone other way. We will represent an arithraetic expression as a binary tree with the operations as internal vertices and the operands as terminal vertices. In this representation we let the root denote the final operation done in the expression, and we place the left operand as its left child and the right operand as its right child. If necessary, this process is repeated on these operands. The binary tree created by this process is called an expression tree.
+
Example 4.24 The expression a * b (where * denotes multiplication) is represented by the binary tree in Figure 4.49. Note that the operation * is represented by an internal vertex, and the operands a and b are represented by terminal vertices. +
a
b
FIGURE 4.49
+
Example 4.25 The expression a + b * c means a + (b * c). The last operation to be performed is addition. Thus we first represent this expression by the binary tree in Figure 4.50(a). Repeating the process with the operand b * c yields the expression tree in Figure 4.50(b). +
X
+
a
.
b*C
(a)
h
(b) FIGURE 4.50
c
4.5 Binary Trees and Traversals
+
235
Example 4.26 The expression tree for a + d * (b in Figure 4.51. +
-
c) is created by the sequence of binary trees
+
a
a /1%d d*(b -c)
b -c FIGURE 4.51
+
Example 4.27 The expression (a+b*c)- (f - d) is represented by the expression tree in Figure 4.52. +
+-
b
c d
e
FIGURE 4.52
We have seen that an arithmetic expression can be represented by an expression tree. Now we must process the expression tree in some way so as to obtain an evaluation of the original expression. We are looking for a systematic way to examine each vertex in the expression tree exactly once. Processing the data at a vertex is usually called visiting a vertex, and a search procedure that visits each vertex of a graph exactly once is called a traversal of the graph. For example, both breadth-first search and depth-first search are traversals of a connected graph because both are methods by which each vertex of the graph is visited (labeled)
236
Chapter 4
Trees
exactly once. Note that 'visit' is used in a technical sense; merely considering a vertex in an algorithm does not necessarily constitute a visit. We will consider a traversal of a binary tree characterized by visiting a parent before its children and a left child before a right child. (This holds for all the vertices in the binary tree.i Such a traversal is called a preorder traversal, and listing the vertices in the order they are visited is called a preorder
listing. Although it is possible to give a description of a preorder traversal using the depth-first search algorithm,* we will state an algorithm for the preorder traversal that is consistent with the descriptions of the other traversals we will discuss. This is a recursive formulation of the preorder traversal, which means that in this description the algorithm refers to itself. This is analogous to the definition of n! given in Section 2.6.
Preorder Traversal Algorithm This algorithm gives a preorder listing of the vertices in a binary tree. Step I Step 2 Step 3
A
(visit) Visit the root. (go left) Go to the left subtree, if one exists, and do a preorder traversal. (go right) Go to the right subtree, if one exists, and do a preorder traversal.
Example 4.28 For the binary tree in Figure 4.53(a), we start by visiting the root A. (We use the word 'visit' to indicate when a vertex should be listed, and in the figures we show the order of visiting in parentheses near the vertex.) Then we go to the left subtree of A (see Figure 4.5 3(b)) and start the preorder traversal again. Now we visit the root B and go to the left subtree of B (see Figure 4.53(c)), where we start another preorder traversal. Next we visit the root D. Since there is no left subtree of D, we go to the right subtree of D (which consists of just the vertex F), and again start a preorder traversal. Thus we visit the root F. Since there are no subtrees of F, we have completed the preorder traversal of the left *Apply the depth-first search algorithm to a binary tree by starting at the root and always choosing a left child inpreference to a right ch Ald. The order in which the vertices are labeled is the preorder listing.
4.5 Binary Trees and Traversals
237
subtree of B. Consequently, we next begin a preorder traversal of the right subtree of B (see Figure 4.53(d)). To do this, we visit the root E and then go to the left subtree of E (which consists of only the vertex G) to begin another preorder traversal. Thus we visit vertex G. Since G has no subtrees and E has no right subtree, both subtrees of B are traversed. This completes the traversal of the left subtree of A, and so we begin another preorder traversal on the right subtree of A. This consists only of visiting the root C and so completes the preorder traversal of the entire binary tree. The resulting preorder listing is A, B, D, F, E, G, C with a labeling of the vertices shown in Figure 4.53(e). +
A(1)
A(1)
A(1)
B C D
E
D
F G
F G
(a)
(b)
(c)
7)
D(3)
(d)
(e) FIGURE 4.53
+
Example 4.29 Applying preorder traversal to the binary tree in Figure 4.54 yields the order of visiting shown in Figure 4.55. + When a preorder traversal is performed on an expression tree, the resulting listing of operations and operands is called the prefix form or Polish notation for the expression. (The latter name is used in honor of the famous Polish logician Lukasiewicz.) For example, the four expressions in Examples 4.24, 4.25, 4.26,
238
Chapter4
Trees
and 4.27 have as their Polish notations *ab,
+a *bc,
+-a *d-bc,
and
-+a
*bc-f/de,
respectively. An expression in Polish notation is evaluated according to the following rule: Scan from left to right until coming to an operation sign, say T, that is followed by two successive numbers, say a and b. Evaluate T a b as a T b, and replace T a b by this value in the expression. Repeat this process until the entire expression is evaluatecL (Equivalently, an expression in Polish notation can be scanned from right to lefId ntil coming to two successive numbers followed immediately by an operation sign.)
FIGURE 4.54
o~10
6
7 FIGURE 4.55
+
Example 4.30 The expression (2 - 3 * 4) + (4 + 8) is represented by the expression tree in Figure 4.56. The Polish notation for this expression (found by doing a preorder traversal on the expression tree) is + - 2 * 3 4 + 4 / 8 2. The evaluation is performed as follows.
4.5 Binary Trees and Traversals
239
+
2 3
4
8
2
FIGURE 4.56
First, we evaluate * 3 4 and replace it by 3 * 4 = 12. This substitution gives the new expression + 2 12 + 4 / 8 2. Second, we evaluate - 2 12 and replace - 2 12 by -10. This substitution yields the new expression + -10 + 4 / 8 2, where we remember that the - is part of -10 and is not a new operation. Third, we evaluate / 8 2 and replace these symbols with 4. Thus the current expression is + -10 + 4 4. Fourth, we evaluate + 4 4 as 8. The expression now has the form + -10 8. Fifth, we evaluate + -10 8 to obtain the final result, which is -2. + The Polish notation for an expression provides an unambiguous way to write it without the use of parentheses or conventions about the order of operations. Many computers are designed to rewrite expressions in this form.
Po3stordXer Traversal Readers who are familiar with hand calculators know that some require algebraic expressions to be entered in a form known as reverse Polish notation or postfix form, also introduced by Lukasiewicz. Unlike Polish notation, in which the operation sign precedes the operands, in reverse Polish notation the operation sign follows the operands. The reverse Polish notation for the expression in Example 4.30 is 2 3 4 * - 4 8 2/ + +. It is evaluated in a fashion similar to Polish notation except that, as we scan from left to right, we look for two numbers immediately followed by an operation sign. (As with Polish notation, we could scan from right to left looking for an operation sign followed immediately by two consecutive numbers.) The steps in evaluating the expression above are 2 3 4 * -4 8 2 / + + 2 12 - 4 8 2 / + + -10 4 8 2 / + +
-10 4 4 + + -10 8 + -2
(First 3 4 * is evaluated and replaced.) (Next 2 12 - is evaluated and replaced.) (Then 8 2 / is evaluated and replaced. Note that the first symbol is part of the number -10 and not an operation sign.) (Next 4 4 + is evaluated and replaced.) (Finally we evaluate -10 8 + to obtain the final result.)
240
Chapter 4 Trees
Again we see that we car evaluate an expression without the need for parentheses and without worrying about the order of operations. Thus reverse Polish notation is an efficient method for use in hand calculators and computers. How can the reverse Polish notation for-an expression be obtained from an expression tree? By using a traversal called postorder, we can obtain the reverse Polish notation for an expression. The postorder traversal is characterized by visiting children before the parent and a left child before a right child. (This holds for all the vertices in the binary tree.) A systematic way to do this is described in the following recursive algorithm.
Postorder Traversal Algorithm This algorithm gives a postorder listing of the vertices of a binary tree. Step]I Step 2 Step 3 Step 4
+
(start) Go to the root. (go left) Go to the left subtree, if one exists, and do a postorder traversal. (go right) Go to the right subtrec, if one exists, and do a postorder traversal. (visit) Visit the root.
Example 4.31 For the binary tree in Figure 4.57(a), we begin at the root A, go to the left subtree of A (see Figure 4.57(b)), and begin postorder traversal again. Thus we go to the left subtree of B (see Figure 4 57(c)) and start postorder traversal again. Since there is no left subtree of D, xwe go to the right subtree of D (which consists only of the vertex F) and begin postorder traversal again. Because F has no subtrees, we visit F. Since the right subtree of D has been traversed, we next visit D. (Again we use the word 'visit' to indicate when a vertex should be listed, and in the figures, show the order of visiting in parentheses near the vertex.) Now the left subtree of B has been traversed, so we go to the right subtree of B (see Figure 4.57(d)) and start postorder traversal again. Next we go to the left subtree of E (which is just G) and start postorder traversal again. Since G has no subtrees, G is visited. Because the left subtree of E has been traversed and there is no right subtree of E, we visit E. Nowv both the left and right subtrees of B have been traversed; so we visit B. This completes the traversal of the left subtree of A, and so we go to the right subtree of A and start postorder traversal again. Because C has no subtrees, C is visited. Since both subtrees of A have been traversed, we visit A. This completes the poltorder listing F, D, G, E, B, C, A with a labeling of the vertices shown in Figure .4.57(e). +
4.5 Binary Trees and Traversals A
A
241
A
D
D
F G (a)
(b)
(c)
A(7)
A
F(1) G(3)
F(1) G(3)
(d)
(e) FIGURE 4.57
*
Example 4.32 The postorder traversal applied to the binary tree in Figure 4.54 yields the order of visiting shown in Figure 4.58. + 16
FIGURE 4.58
We have seen how expression trees yield the Polish and reverse Polish notations for an expression. In these notations, the operation sign precedes or follows the operands, respectively. With the use of the inorder traversal, it is possible to
242
Chapter 4
Trees
obtain an expression with the operation sign between the operands. However, this traversal requires the c'areful insertion of parentheses in order to evaluate the expression properly. The inorder traversal 's characterized by visiting a left child before the parent and a right child after the parent. (This holds for all the vertices in the binary tree.) A systematic way to do this is described in the following recursive algorithm.
Inorder Traversal Algorithm This algorithm gives an inorder listing of the vertices of a binary tree. Step 1 (start) Go to the root. Step 2 (go left) Go to the left subtree, if one exists, and do an inorder traversal. Step 3 (visit) Visit the root. Step 4 (go right) Go to the right subtvee, if one exists, and do an inorder traversal.
Example 4.33 For the binary tree in Figure 4.59(a), we begin at the root A, go to the left subtree of A (see Figure 4.59s b)), and then start inorder traversal again. Next we go to the left subtree of B (see Figure 4.59(c)) and start inorder traversal again. Since there is no left subtree of D, we visit the root D. (Again we use the word 'visit' to indicate when a vertex should be listed, and in the figures, show the order of visiting in parentheses near the vertex.) Then we go to the right subtree of D (which is just the vertex F) and start inorder traversal again. Since there is no left subtree of F, we visit the root F. Since F has no right subtree, we have traversed the left subtree of B. So we visit B, go to the right subtree of B (see Figure 4.59(d)), and do inorder traversal again. Thus we go to the left subtree of E (which is just the vertex G) and start inorder traversal again. Since G has no left subtree, we visit G. Because G has no right subtree, we have traversed the left subtree of E. Thus we visit E. Since E has no right subtree, we have traversed the right subtree of B. We have now traversed the left subtree of A. Hence we v sit the root A and go to the right subtree of A (which consists only of the vertex C) and begin inorder traversal again. Since C has no left subtree, we visit C'. This step completes the inorder traversal giving the inorder listing D, F, B, G, E, A, C with the labeling of vertices shown in Figure 4.59(e). +
4.5 Binary Trees and Traversals
243
B.
D
(a)
(b)
(c)
D(I
(d)
(e) FIGURE 4.59
+
Example 4.34 When inorder traversal is applied to the binary tree in Figure 4.54, the vertices are listed according to the numbering in Figure 4.60. + 9
FIGURE 4.60
+
Example 4.35 Applying inorder traversal to the expression tree in Figure 4.56 yields the expression2 - 3 * 4 + 4 + 8/2. +
244
Chapter 4
Trees
Other uses of traversals can be found in suggested reading [7] at the end of the chapter.
EXERCISES 4.5 In Exercises 1-6, construct an expression treefor each exp session. 1. a*b+c 3. ((a-b)/c)* (d + e/f) 5. a*(b*(c*(d*e+f)-g)+h)+j
2. (4+2)*(6-8) 4. (((6-3) * 2) + 7)/((5-1) * 4 + 8) 6. (((4*2)/3)-(6-7))+(((8-9)*8)/5)
In Exercises 7-12 find the indicated subtrees. 7. the left subtree of vertex A
8. the right subtree of vertex A
A
A
B
C
9. the left subtree of vertex C
1(. the right subtree of vertex E A
11. the left subtree of vertex E
12. the right subtree of vertex D
A B
C F I
L
m
N
0
K
P
Q
4.5 Binary Trees and Traversals
245
In Exercises 13-18 give the preorderlisting of verticesfor the binary trees in the indicatedexercises. 13. Exercise 7
14. Exercise 8
15. Exercise 9
16. Exercise 10
17. Exercise 11
18. Exercise 12
In Exercises 19-24 give the postorder listing of verticesfor the binary trees in the indicatedexercises. 19. Exercise 7
20. Exercise 8
21. Exercise 9
22. Exercise 10
23. Exercise 11
24. Exercise 12
In Exercises 25-30 give the inorder listing of vertices for the binary trees in the indicated exercises. 25. Exercise 7
26. Exercise 8
27. Exercise 9
28. Exercise 10
29. Exercise 11
30. Exercise 12
In Exercises 31-36 find the Polish notationfor the expressions in the indicatedexercises.
31. Exercise 1
32. Exercise 2
33. Exercise 3
34. Exercise 4
35. Exercise 5
36. Exercise 6
In Exercises 3 7-42 find the reverse Polish notationfor the expressions in the indicatedexercises. 37. Exercise 1
38. Exercise 2
39. Exercise 3
40. Exercise 4
41. Exercise 5
42. Exercise 6
Evaluate the Polish notation expressions in Exercises 43-46. 43. + /42+56
44. *+-+43628
45. +*4/62-+425
46. +*+34-12-3/42
Evaluate the reverse Polish notation expressions in Exercises 47-50. 47. 45-7*23++
48. 56422/+*-
49. 23+46--5*4+
50. 34+ 12 -*42/3-+
51. Construct an expression tree for the Polish notation expression * + B D -
AC.
52. Construct an expression tree for the Polish notation expression * + B -
DF + ACE.
53. Construct an expression tree for the reverse Polish notation expression AC * B -
D +.
54. Construct an expression tree for the reverse Polish notation expression ED 55. Construct a B, E, C, A, 56. Construct a listing is A,
A + BC -
F * +.
binary tree for which the preorder listing of vertices is C, B, E, D, A and the inorder listing is D. binary tree for which the preorder listing of vertices is E, C, A, D, B, F, G, H and the inorder C, D, E, F, B, G, H.
246
Chapter4 Trees
57. Construct a binary tree for which the postorder listing of vertices is E, B, F, C, A, D and the inorder listing is E, B, D, F, A, C.
58. Construct a binary tree for which the postorder listing of vertices is D, H, F, B, G, C, A, E and the inorder listing is D, F, H, E, B, A, G, C.
59. 60. 61. 62. 63. 64.
Construct a binary tree with 7 vertices for which the preorder listing is the same as the inorder listing. Construct a binary tree with 8 vertices for which the postorder listing is the same as the inorder listing. Construct a binary tree for which the preorder listing is the same as the postorder listing. Construct two distinct (nonisomorphic) binary trees hat have 1, 2, 3 as their preorder listing of vertices. Construct two distinct (nonisomorphic) binary trees 1hat have 1, 2, 3 as their postorder listing of vertices. Verify for n = 1, 2, and 3 that the number of binary trees with n vertices is
n!(A+
1)!
(Such numbers are called Catalan numbers.) 65. Prove that if vertex X is a descendant of vertex Y in a binary tree, then Y precedes X in the preorder listing of vertices and X precedes Y in a postorder listing. 66. Prove that if the preorder and the inorder listings of vertices of a binary tree are given, then it is possible to reconstruct the binary tree. 67. The Fibonacci trees are defined recursively as follows: each of El and T2 is a single vertex and for n > 3, T, is a tree where the left subtree of the root is Tn-1 and the right subtree is Tn-2. Find and prove a formula for the number of vertices in T.
4.6
+
OPTIMAL BINARY TREES AND BINARY SEARCH TREES In this section we present two applications, both of which require the construction of a binary tree to solve a problem. These can be studied in either order.
To represent symbols, computers use strings of Os and Is called codewords. For example, in the ASCII (American Standard Code for Information Interchange) code, the letter A is represented by the codeword 01000001, B by 01000010, and C by 01000011. In this system each symbol is represented by some string of eight bits, where a bit is either a 0 or a 1. To translate a long string of Os and Is into its ASCII symbols, we use the following procedure: Find the ASCII symbol represented by the first 8 bits, the ASCII symbol represented by the second 8 bits, etc. For example, 010000110100000101000010 is decoded as CAB.
4.6
OptimalBinary Trees and Binary Search Trees
247
For many purposes this kind of representation works well. However, there are situations, as in large-volume storage, where this is not an efficient method. In a fixed length representation, such as ASCII, every symbol is represented by a codeword of the same length. A more efficient approach is to use codewords of variable lengths, where the symbols used most often have shorter codewords than the symbols used less frequently. For example, in normal English usage the letters E, T, 0, and A are used much more frequently than the letters Q, J, X, and Z. Is there a way to assign the shortest codewords to the most frequently used symbols? If messages use only these eight letters, a natural assignment to try is: E: 0,
Q: 00,
T: 1, J: 10,
0: 01, X: 101,
A: 11, Z: 011.
Here the shortest possible codewords are assigned to the most frequently used letters, and longer codewords are assigned to the other letters. This appears to be a more efficient approach than assigning all these letters a codeword of the same fixed length, which would have to be three or more. (Why?) But how can we decode a string of Os and Is? For example, how should the string 0110110 be decoded? Should we start by looking at only the first digit, or the first two, or the first three? Depending upon the number of digits used, the first letter could be E, 0, or Z. We see that, in order to use variable length codewords, we need to select representations that permit unambiguous decoding. A way to do this is to construct codewords so that no codeword is the first part of any other codeword. Such a set of codewords is said to have the prefix property. This property is not enjoyed by the above choice of codewords since the codeword for T is also the first part of the codeword for A. On the other hand, the set of codewords S = {000, 001, 01, 10, 111 has the prefix property since no codeword appears as the first part of another codeword. The method for decoding a string of Os and Is into codewords that have the prefix property is to read one digit at a time until this string of digits becomes a codeword, then repeat the process starting with the next digit, and continue until the decoding is done. For example, using the set of codewords S above, we would decode the string 001100100011 as 001, 10, 01, 000, 11. Thus an efficient method of representation should use codewords such that (1) the codewords have the prefix property; and (2) the symbols used frequently have shorter codewords than those used less often. Any binary tree can be used to construct a set of codewords with the prefix property by assigning 0 to each edge from a parent to its left child and 1 to each edge from a parent to its right child. Following the unique directed path from the root to a terminal vertex will give a string of Os and Is. The set of all strings formed in this way will be a set of codewords with the prefix property, because corresponding to any codeword we can find the unique directed path by working down from the root of the binary tree, going left or right according to whether a digit is 0 or 1. By definition we finish at a terminal vertex, and so this codeword cannot be the first part of another codeword.
248
Chapter4
Trees
0 HI
(a)
(b) FIGURE 4.61
*
Example 4.36 For the binary tree in Figure 4.615(a), we assign Os and Is to its edges as shown in Figure 4.61(b). The directed paths from the root to all the terminal vertices then produce the codewords 000, 0011, 01, 10, 11 as illustrated in Figure 4.62, where each codeword is written below the corresponding terminal vertex. *
0
1 0
ON)
10
o01 FIGURE 4.62
Thus, by using a binary tree, we have found a way to produce codewords that have the prefix property. It remains to find a method for assigning shorter codewords to the more frequently used symbols. If we have only the 5 symbols in Figure 4.62, then we want to use the codewords 01, 10, and 11 for the three most frequently used symbols. Notice that these codewords correspond to the terminal vertices that are closest to the root. Thus, to obtain an efficient method for representing symbols by variable length codewords, we can use a binary tree and assign the most frequently used symbols to the terminal vertices that are closest to the root. We will restrict our discussion to those binary trees for which every internal vertex has exactly 2 children. Suppose wI, w2 . Wk are nonnegative real numbers. A binary tree for the weights wI, W2 . Wk is a binary tree with k terminal vertices labeled wI, w2 , .. , Wk. A binary tree for the weights wI, W2, . -Wk has weight d uwl + d2 w2 + + dkwk, where di is the length of the directed path from the root lo the vertex labeled wi (i = 1, 2, . . ., k). .
4.6 Optimal Binary Trees and Binary Search Trees
249
+ Example 4.37 The binary tree in Figure 4.63(a) is a binary tree for the weights 2, 4, 5, 6 and has weight 3 6 ± 3 5 + 2 4 + 1 2 = 43. In Figure 4.63 (b) is another binary tree for the weights 2, 4, 5, 6, but its weight is 2(2 + 4 + 5 ± 6) = 34, since the distance from the root to each terminal vertex is 2. + .
.
Distance from loot I-
-
-
2
2- 6
5
(a)
(b) FIGURE 4.63
For the coding problem, we want to find a binary tree of smallest possible weight in which the frequencies of the symbols to be encoded are the weights. A binary tree for the weights WI, W2 , , Wk is called an optimal binary tree for the weights w1 , W 2 . -Wk when its weight is as small as possible. Thus the binary tree in Figure 4.63(a) is not an optimal tree for the weights 2, 4, 5, 6 since there is another binary tree with smaller weight, namely that in Figure 4.63(b). The following algorithm due to David A. Huffman produces an optimal binary tree for the weights WI~, Wk-. ..
.,
Huffman's Optimal Binary Tree Algoritlhm For nonnegative real numbers w I, W2. an optimal binary tree for the weights referred to by its label. Step 1
Step 2
where k > 2, this algorithm constructs Wk. In the algorithm, a vertex is
-Wk, WI, W 2 ,
..
,-
(create trees) (a) For i = 1, 2,. k construct a tree consisting of one vertex that is labeled wi. (b) Let S denote the set of trees constructed in this manner. (make a larger tree)
repeat Step 2.1 (select smallest weights) From S select two trees El and T2 with roots that have the smallest labels, say V and W. (Ties can be broken arbitrarily.) Step 2.2 (combine the trees) (a) Construct the root of a binary tree, and assign the label V + W to this root.
250
Chapter 4
Trees
(b) Make El the lef t subtree of this root. (c) Make T2 the right subtree of this root. (d) In S, replace 'I- and T2 with the tree having root labeled V + W. until ISI I
Example 4.38 We begin the construction of an optimal binary tree for the weights 2, 3, 4, 7, and 8 by constructing five binary tre.es, each having a single vertex that is labeled by one of the given weights. (See Figure 4.64.) The set consisting of these five trees is denoted S. Now we select the two trees Ti and T 2 with roots having the smallest labels, namely 2 and 3. (For convenience, we will refer to a vertex by its label.) 2
3
4
7
8
FIGURE 4.64
We use Ti and T2 to forrri a new tree with root 5, and replace Ti and T2 in S by this new tree. The binary trees in S now have roots labeled 5, 4, 7, and 8, as shown in Figure 4.65. Next, we continue step 2 by combining the binary trees with roots labeled 5 and 4 to form a new binary tree with root 9. This tree replaces the two trees in S with labels 5 and 4. The binary trees now in S have roots labeled 9, 7, and 8. (See Figure 4.66.) As step 2 continues, we combine the binary trees with roots labeled 7 and 8 to form a binary tree with root 15. This 5
2
3
4
7
8
7
8
FIGURE 4.65 9
5 4
2
3
FIGURE 4.66
4.6 Optimal Binary Trees and BinarySearch Trees
251
new tree now replaces the trees in S with labels 7 and 8. At this point S consists of two binary trees with roots labeled 9 and 15. (See Figure 4.67.) Combining these two binary trees gives the optimal binary tree for the weights 2, 3, 4, 7, and 8 shown in Figure 4.68. The weight of this tree is 2(4+7+8)+3(2+3)=53.
+
9
15
FIGURE 4.67 24
3 FIGURE 4.68
+
Example 4.39 Using Huffman's optimal binary tree algorithm, we can construct an optimal binary tree for the weights 2, 4, 5, and 6 in the steps shown in Figure 4.69. This tree has weight 1 .6+2 5+3(2+4)= 34. Note that in step 2.1 we could have chosen the binary tree with one vertex labeled 6 instead of the tree with three vertices labeled 2, 4, and 6. In this case we would have obtained a different optimal binary tree. +
252
Chapter 4
Trees
2
4
5
6
5
6
(a) 6
2
4 (b) II
5
2
4
6 (c) 17
I]
5
2
4
(d) FIGURE 4.69
An analysis of this algorithm requires knowledge of sorting and inserting algorithms that we have not studied. Thus we state without proof (see suggested reading [4] at the end of the chapter) that Huffman's optimal binary tree algorithm is of order at most k2 , where k is the number of weights. A proof that the algorithm constructs an optimal binary tree is found in Exercises 45-47.
253
4.6 OptimalBinary Trees and Binary Search Trees
In order to find codewords with the prefix property such that the most frequently used symbols are assigned the shortest codewords, we construct an optimal binary tree with the stated frequencies of the symbols as its weights. Then by assigning Os and is to the edges of this tree, as described in Example 4.36, codewords can be efficiently assigned to the various symbols. Example 4.40 Suppose the characters E, T, A, Q, and Z have expected usage rates of 32, 28, 20, 4, and 1, respectively. In Figure 4.70 we see an optimal binary tree with weights 1, 4, 20, 28, 32 created by Huffman's optimal binary tree algorithm. Furthermore, each symbol has been placed in parentheses next to its usage rate. Then Os and Is are assigned to the edges of the trees so that codewords with the prefix property are formed at the terminal vertices. (See Figure 4.71.) Thus we see that E should be assigned the codeword 1, T should be assigned the codeword 01, A should be assigned 001, Q should be assigned 0001, and Z should be assigned 0000. +
0
~32(E) 28(T)
0
1
01 20(A)
I(Z) 0000
1(Z) FIGURE 4.70
4(Q) 0001 FIGURE 4.71
A binary tree describing the codewords can also be used to decode a string of Os and Is. To do so, we start with the digits in the string and follow the directed path from the root indicated by the Os and is. When a terminal vertex is reached, the string is then decoded by the codeword at that vertex. Then this process is begun again at the root with the next digit. For example, to decode the string 00101 with the tree in Figure 4.71, we start at the root and go to the left child, then to the left child again, and then to the right child, which is a terminal vertex
with the codeword 001 and symbol A. Then we go back to the root and decode the remaining bits 01, which correspond to the symbol T. Another application of Huffman's optimal binary tree algorithm arises in regard to the merging of sorted lists. Suppose we have two sorted lists of numbers L and L2 that we want to merge together into one sorted list. Recall from Theorem 2.11 that if L I and L 2 have n I and n2 numbers, respectively, then these two sorted
lists can be merged into one sorted list with at most n, + n2 - 1 comparisons. Now suppose we have 3 sorted lists LI, L 2 , and L 3 containing 150, 320, and 80 numbers, respectively. By merging only two lists at a time, how can we merge
254
Chapter4
Trees
these 3 lists into one sorted list so that we minimize the number of comparisons needed? One way is to merge L 1 and L2 into a third list with 470 numbers, which requires at most 469 comparisons. Then we merge this new list with L3 , which requires at most 470 + 80 -- 1 549 comparisons. Altogether this merge pattern requires at most 469 + 549 = 1018 comparisons. This process can be represented by the binary tree in Figure 4.72 a), where the labels represent the sizes of the lists. A second merge pattern is to merge L, and L3 first, followed by merging this new list with L2 . This merge pattern requires at most 229 + 549 = 778 comparisons and is represented in Figure 4.72(b). Finally, we can merge L2 and L3 first and then merge the result with LI. This requires at most 399 + 549 = 948 comparisons and is represented in Figure 4.72(c). Thus we see that the second merge pattern requires the fewest comparisons. Furthermore, we observe that the optimal pattern of merging (the one requiring the fewest comparisons) occurs when the smallest lists are used first, since in this way fewer comparisons are made overall. In fact, the number of times an item is sorted is the distance of its list from the root in the binary trees shown in Figure 4.72. Thus the optimal merging pattern corresponds to the tree of minimum weight in Figure 4.72, where the weight of a vertex is the number of items in the corresponding list. Hence the optimal merge pattern can be found by following the construction in Huffman's optimal binary tree algorithm. 550 470) 150
550
80 320 (a)
230
320
150
80 (b)
320
80 (c)
FIGURE 4.72
*
Example 4.41 In order to merge five sorted lists with 20, 30, 40, 60, and 80 numbers optimally, we begin by merging together the two sorted lists with the smallest number of items. These are the sorted lists with 20 and 30 numbers, giving a new sorted list of 50 numbers and using at most 49 comparisons. Now we consider the four sorted lists with 50, 40, 60, and 80 numbers and combine the lists with 50 and 40 items; this merging yields a new sorted list of 90 items using at most 89 comparisons. Next, from the three sorted lists with 90, 60, and 80 numbers, we merge the lists with 60 and 80 numbers to obtain a new sorted list of 140 items using at most 139 comparisons. Finally, we combine the sorted lists with 90 and 140 numbers giving one sorted list with 230 numbers and using at most 229 comparisons. This optimal merge pattern uses at most 506 comparisons. A binary tree representation of this optimal merge pattern is given in Figure 4.73. +
4.6 Optimal Binary Trees andBinary Search Trees
255
,140
To
20
30
FIGURE 4.73
Maintaining a large data set is a common problem for data processors. This consists not only of updating the data set by adding and deleting, but also of searching the data for a particular piece of information. Suppose, for instance, that the Acme Manufacturing Company maintains a list of its customers. When an order is received, the company must search this list to determine if the order is from an old or a new customer. If the order is from a new customer, then this customer's name must be added to the list. Moreover, when a customer goes out of business, that customer's name must be removed from the list. One way to maintain such lists is to keep the data in the order in which they are received. For example, if Acme Manufacturing has ten customers named Romano, Cohen, Moore, Walters, Smith, Armstrong, Garcia, O'Brien, Young, and Tucker, they can keep these names in an array in the given order. This method enables items to be added to the list easily; if Jones becomes a customer, this new name can be added to the end of the existing array. However, this method makes it very time-consuming to determine if a particular name is in the list. Determining that Kennedy is not a customer, for instance, will require checking every name in the list. Of course, the amount of checking required is minimal when Acme has only ten customers, but if Acme has a million customers, checking every name on the list is prohibitive. Another approach is to keep the list in alphabetical order. For example, Acme Manufacturing's list of customer names can be stored as Armstrong, Cohen, Garcia, Moore, O'Brien, Romano, Smith, Tucker, Walters, and Young. With this method, it is easy to search the list for a particular name. (The procedure used in the proof of Theorem 2.10 can be adapted to give an efficient searching method.) However, adding or deleting from the list is more difficult because of the need to reposition the entries when an item is added or deleted. For example, if Acme gains a new customer named Baker, then we need to insert this name as the second entry in the list. This insertion requires repositioning every name in the original list except Armstrong's. Again, this process is prohibitive if the list is very long. A third approach is to store data at the vertices of a binary tree. For example, the list of Acme Manufacturing's customer names can be stored as in Figure 4.74. This binary tree is arranged so that if a vertex U belongs to the left subtree of
256
Chapter4 Trees
vertex V, then U precedes V in alphabetical order; and if a vertex W lies in the right subtree of V, then W fellows V in alphabetical order. Adding a new name to this tree is simple because we need only to include one new vertex and edge in the tree, and searching the tree for a particular name requires no more than four comparisons if we search the tree properly. Romano
Am
mg
FIGURE 4.74
In order to generalize this example, suppose that we have a list of distinct
numbers or words. We will us, the symbol < to denote the usual numerical or alphabetical (dictionary) order. For example, 7 < 9 and ABGT < ACE. A binary search tree for the list is a binary tree in which each vertex is labeled by an element of the list such that: (1) (2) (3)
No two vertices have the same label. If vertex U belongs to the left subtree of vertex V, then U < V. If vertex W belongs to the right subtree of vertex V, then V < W.
Thus, for each vertex V, all descendents of V in the left subtree of V precede V, and all descendents of V in the right subtree of V follow V. Example 4.42 One possible binary search tree for the list 1, 2, 4, 5, 6, 8, 9, 10 is given in Figure 4.75(a). Another possibility is shown in Figure 4.75(b). + 6 4
8 6
9
9
5 4
10
10 2
1( (a)
(b) FIGURE 4.75
4.6
Optimal Binary Trees and Binary Search Trees
257
Example 4.43 A binary search tree for the list IF, THEN, FOR, BEGIN, END, WHILE, TO is illustrated in Figure 4.76.
+ IF FOR
THEN
BEGIN
WHILE
END
TO
FIGURE 4.76
There is a systematic way to construct a binary search tree for a list. The basic idea is to put smaller elements as left children and larger elements as right children.
Binary Search Tree Construction Algorithm This algorithm constructs a binary search tree in which the vertices are labeled al,a2, . . ., an,wherea 1,a 2 . an are distinct and n > 2. In the algorithm, avertex is referred to by its label. Step 1
(construct the root) (a) Construct the root of the binary tree, and label it ai.
Step 2
(insert elements into the tree)
(b) Set k= 1.
while k
repeat Perform exactly one of the following three steps. (a) (go left) if ak < V and V has a left child W Replace V with W.
endif (b) (go right) if ak > V and Vhas arightchild W Replace V with W.
endif
258
Chapter4
Trees
(c) (stay here if neither ' a) nor (b) is possible Do nothing. endif until either (aA < V and V has no left child) or (ak V has no right child) Step 2.2 (insert ak) if ak < V Construct a left child for V and label it ak. otherwise Construct a right child for V and label it ak. endif endwhile
>
V and
The construction described in the binary search tree construction algorithm does produce a binary tree. Furthermore, labels for the left descendants (those on the left side) are smaller than the label for the parent, and labels for the right descendants are larger. Thus the algorithm yields a binary search tree.
+
Example 4.44 The result of using the binary search tree construction algorithm on the list 5, 9, 8, 1, 2, 4, 10, 6 is shown ii Figure 4.77. + 5
5
5
5
5
0
%
9o
9
9
8
I1z8 2 2
1
I
I
2
2 4 FIGURE 4.77
to
4.6 OptimalBinary Trees and Binary Search Trees
259
Example 4.45 For the list of words in the sentence DISCRETE MATH IS FUN BUT HARD, the algorithm yields the binary search tree in Figure 4.78. + If an additional item is to be added to the binary search tree, then we can simply use the binary search tree construction algorithm one more time with that item. For example, to add SOMETIMES to the end of the list of words in the sentence DISCRETE MATH IS FUN BUT HARD, we would repeat the algorithm using the word SOMETIMES with the binary search tree in Figure 4.78 to obtain the one in Figure 4.79. This procedure for adding an item to a binary search tree is an efficient one. DISCRETE
'MATH
BUT TIMES FL
or) FIGURE 4.78
FIGURE 4.79
To determine if an item is in a binary search tree (or equivalently, a list), we follow closely the process used to construct a binary search tree. Specifically, we compare the item with the root and go left if it is smaller and go right if it is larger. This process is repeated until we either match some item in the tree or find that the item is not in the tree. This procedure is formalized in the following algorithm.
Binary Search Tree Search Algorithm This algorithm will examine a binary search tree to decide if a given element a is in the tree. Step I Step 2
(initialization) Let V be the root of the binary search tree. (descend the tree) while (a < V and V has a left child) or (a > V and V has a right child) if a < V and V has a left child Replace V with its left child. otherwise Replace V with its right child.
endif endwhile
260
Chapter4
Trees
Step 3
+
(is a in the tree?) if a :A V Element a is not in the tree. otherwise Element a is in the tiee. endif
Example 4.46 Let us use this algorithm to search for 7 in the last binary search tree of Figure 4.77. We begin by comparing 7 to the root of the tree. Since it is larger, we go to the right child 9 and make another comparison. This time 7 is smaller, and so we go to the left child 8. Comparing 7 to 8, we go to the left child 6. Now comparing 7 to 6, we go to the right child of 6. Since there is none, 7 is not in the binary search tree. We also note that f we wanted to add 7 to this binary search tree at this time, it would become the right child of 6. +
+
Example 4.47 To search for the word FUN in the tree of Figure 4.78, we begin with the root DISCRETE. The first comparison takes us to the right child MATH. From there another comparison takes us to the left child IS. Again we go to the left child FUN. This comparison results in a match. Hence we find that FUN is in the tree. + Applying the inorder traversal to the binary search tree in Figure 4.77 yields the inorder listing 1, 2, 4, 5, 6, 8, 9, 10, which is the usual numerical order for these numbers. Similarly, for the binary search tree in Figure 4.76, the inorder traversal gives the listing BEGIN, END, FOR, IF, THEN, TO, WHILE, which is the alphabetical order for thes- words. In general, when the inorder traversal is applied to a binary search tree, the resulting listing is the usual ordering of the elements. From this, the smallest and largest elements of the tree can be found. Thus by going left as far as pos sible in a binary search tree, the last vertex reached is the smallest element in the tree. Similarly, by going right as much as possible, the last vertex reached is the largest element in the tree. Deletions of items from a binary search tree can also be done efficiently. The details are left as exercises. The construction of a bin iry search tree depends upon the order in which the items appear in the list. Er other words, a different order for the items can produce a different binary search tree. For example, for the list 10, 9, 8, 6, 5, 4, 2, 1, which is the same set of items as in Example 4.44, the algorithm produces the binary search tree shown in Figure 4.80. It is easy to see that this tree offers no advantage over numerica. Drder in storing the numbers since a search may require a comparison with every item in the tree. However, there is an extensive
4.6 OptimalBinaryTrees and Binary Search Trees
261
literature on the construction of binary trees that make for efficient searching. For example, binary search trees can be constructed so that the more frequently accessed items are closer to the root than those items that are not. Interested readers should consult suggested readings [7] and [9] at the end of the chapter.
4
FIGURE 4.80
EXERCISES 4.6 In Exercises 1-4 determine if the given sets of codewords have the prefix property. 2. {00, 11,010, 100,0111 4. (00,110,101,011
1. {0, 100, 101, 11, 10111 3. {00, 101, 111, 10001, 10101
5. Can there be a set of 6 codewords with the prefix property that contains 0, 10, and 11? 6. Can there be a set of 6 codewords with the prefix property that contains 10, 00, and 110? 7. Determine values for a, b, and c so that {00, 01, 101, a 10, bcl} is a set of 5 codewords with the prefix property. 8. Determine values for a, b, c, and d so that (00, OaO, Obc, dO, 110, 1111 is a set of 6 codewords with the prefix property. For the values of n given in Exercises 9-14, draw a binary tree in which each vertex has 0 or 2 children that generates as in Example 4.36 a set of n codewords with the prefix property. Label the vertices with the codewords.
9. n = 2 12. n = 7
10. n = 3
11. n = 4
13. n = 8
14. n = 9
In Exercises 15-18 draw a binary tree that generates as in Example 4.36 the given codewords at the terminal vertices. 15. 1,00,011,0100,0101
16. 101, 00, 11, 01 1, 100, 010
17. 1111,0, 1110, 110, 10 18. 1100,000, 1111, 1101,0010, 10,0011 19. Decode the message 111010100000101 10 with the assignment A: 010, B: 111, M: 000, N: 110, and T: 10. 20. Decode the message 110010111011110 with the assignment 0: 0, B: 10, R: 110, 1:1110, N: 11110, and T: 11111. 21. Decode the message 100101110101001011 with the assignment A: 111, E: 0, N: 1010, 0: 1011, and T: 100.
262
Chapter 4
Trees
22. Decode the message 00111100010000111 with the assignment B: 1100, D: 111, E: 1101, J: 0011, N: 0000, 0: 01, S: 0010, and T: 0001. In Exercises 23-26 decode the messages using the given binary tree.
23. message: 01110111
24. message: 11001001011000
C
G
HI
T F
0
/A TI
25. message: 01111111011101001100
R
S
A
26. message: 00001001001100001101001 I
A
E
H -
H
AT
E
T
Locate a copy of the ASCII code in a computer programming book and use it to decode the messages given in Exercises 27-30.
27. 010001000100111101000111 29. 010100010101010101001001010001010101010(
28. 01001000010011110100110101000101 30. 01001000010001010100110001010000
In Exercises 31-34 construct an optimal binary tree for the given weights. In the construction, when there is a choice of trees having roots with the same label, select the tree having the greaternumber of vertices.
31. 2,4,6, 8,10 33. 1,4,9,16,25, 36
32. 4,6,8,14,15 34. 10, 12,13, 16,17, 17
In Exercises 35-38 determine the smallest maximum number of comparisonsneeded to merge sorted lists with the given numbers of items into one sorted list.
35. 4 lists with 20, 30, 40, and 50 items 37. 6 lists with 20, 40, 60, 70, 80, and 120 items
36. 5 lists with 15, 25, 35, 40, and 50 items 38. 7 lists with 10, 30, 40, 50, 50, 50, and 70 items
In Exercises 39-42, in the constructionof an optimal binary tree, when there is a choice of trees having roots with the same label, select the tree having the greaternumber of vertices.
39. The National Security Agency is helping American diplomats in foreign countries send coded messages back to the State Department in Washington, D.C. These messages are to be sent using the characters R, I, H, V with
4.6
Optimal Binary Trees and BinarySearch Trees
263
an expected usage rate of 40, 35, 20, 5, respectively, per 100 characters. Find an assignment of codewords that minimizes the number of bits needed to send a message. 40. Tom and Susan are exchanging love letters during class. In order to prevent others from reading these sweet words of romance, the messages are coded using only the characters T, A, I, L, P, and J with an expected usage rate of 34, 27, 21, 10, 6, 2, respectively, per 100 characters. Find an assignment of codewords that minimizes the amount of time (and hence, the number of bits) needed to send a message. 41. NASA is receiving information from one of its space probes. This information is in the form of numbers that represent pictures. (Each number corresponds to a shade of white, black, or gray.) The numbers used are 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10 with expected usage rates of 125, 100, 75, 40, 60, 180, 20, 120, 150, and 130, respectively, per 1000 colored dots. Find an assignment of codewords for these numbers that minimizes the number of bits needed for the storage of this information. 42. The Gregory Computer Company has received a contract to store nursing data for all hospitals in the Bloomington, Illinois, area. Even though the storage of this data will be on hard disks, the high volume of data makes it important that the data be stored efficiently. An analysis of sample data shows that only certain symbols are used; in, , c, s, po, os, od, tid, qod. Furthermore, the analysis shows a usage rate of7, 12,4, 9, 10, 8, 2, 18, 30, respectively, per 100 symbols. Find an assignment of codewords that minimizes the number of bits needed to store this data. 43. Prove that there exists a binary tree with n terminal vertices in which each vertex has 0 or 2 children. 44. In a binary tree in which each vertex has 0 or 2 children, prove that the number of terminal vertices is one more than the number of internal vertices. Exercises 45-47 provide a proof that Huffman's optimal binary tree algorithm creates an optimal binary tree. Suppose w , W2, . . - Wk are nonnegative real numbers and wI < W2 < . . < Wk. 45. Prove that if T is an optimal binary tree for the weights wI, W2, . . - Wk, and if wi < wj, then the distance from the root to wi is greater than or equal to the distance from the root to Wi . 46. Prove that there is an optimal binary tree for the weights wI, W2 . W.k, where wI and W2 are children of the same parent. 47. Prove that if T is an optimal binary tree for the weights wI + W2 , W3.. Wk, then the tree obtained by replacing the terminal vertex wI + w 2 by a binary tree with two children wI and w2 is an optimal binary tree for the weights wI, W2, .W. , Wk. In Exercises 48-53 construct a binary search tree for the items in the order given. 48. The accounting department in the Busby Insurance Company has 8 divisions with 11, 15, 8, 3, 6, 14, 19, and 10 staff members in them. Construct a binary search tree for the number of staff in these units. 49. Reserved words in Pascal include LABEL, SET, OR, BEGIN, THEN, END, GOTO, DO, PACKED, and ELSE. Construct a binary search tree for these reserved words. 50. Predefined identifiers in Apple Pascal include ORD, CHR, WRITE, SEEK, PRED, EOF, WRITELN, BOOLEAN, PAGE, GET, TRUE, COPY, PUT, and ABS. Construct a binary search tree for these predefined identifiers. 51. The mathematics department has 13 faculty members with 14, 17, 3, 6, 15, 1, 20, 2, 5, 10, 18, 7, and 16 years of teaching experience. Construct a binary search tree for the years of teaching experience by the faculty.
264
Chapter 4
Trees
52. In a survey of 15 mathematics departments it was found that there were 18, 9, 27, 20, 30, 15, 4, 13, 25, 31, 2, 19, 7, 5, and 28 faculty members. Construct a binary se arch tree for the sizes of the faculty. 53. ASCII code is used to represent more than just the alphabet. It is also used to represent the symbols ), :, -, #, <, @, ?, $, (, l, and &. The corresponding ASCUI codewords can be interpreted as binary numbers (with decimal values 41, 58, 37,45, 35, 60, 64, 63, 36,40, 33, and 38, respectively) and, hence, can be used to provide an ordering of these symbols. Construct a binary searz-l tree for these symbols. 54. Construct a binary search tree for the letters of the alphabet so that at most 5 comparisons are needed to locate any specified letter. 55. In the binary search tree of Exercise 49, draw the directed path required to show that FILE is not in the tree. Then indicate where FILE would be added to the tree. 56. In the binary search tree of Exercise 48, draw the directed path required to show that 16 is not in the tree. Then indicate where 16 would be added to the tree. 57. In the binary search tree of Exercise 51, draw the directed path required to show that 4 is not in the tree. Then indicate where 4 would be added to the tree. 58. In the binary search tree of Exercise 50, draw the directed path required to show that POS is not in the tree. Then indicate where POS would be added to the tree. 59. In the binary search tree of Exercise 53, draw the directed path required to show that > (with decimal number 62) is not in the tree. Then indicate where > would be added to the tree. 60. In the binary search tree of Exercise 52, draw the directed path required to show that 8 is not in the tree. Then indicate where 8 would be added to the tree. 61. Suppose that the vertices of a binary tree are assigned distinct elements from a list of either numbers or words with the property: if L is the left child of a vertex V. then L < V, and if R is the right child of a vertex V, then V < R. Must the binary tree with this assignment be a binary search tree for the list? Deletion of a terminal vertex V from a binary search tree is accomplished asfollows: Delete the vertex V and the edge on V and its parent. 62. Draw the binary search tree obtained by deleting 6 from the binary search tree in Exercise 48. 63. Repeat Exercise 62 for PACKED in the binary search tree in Exercise 49. When a binary search tree has a root R with only one child, deletion of the root is accomplished asfollows: Delete R and the edge on R and its child. 64. Draw the binary search tree obtained by deleting the root from the following binary search tree.
6
14
7 16
20
HistoricalNotes
265
65. Repeat Exercise 64 for the binary search tree below.
3
12
Suppose that V is a vertex in a binary search tree such that V is not the root and V has only one child C. Deletion of V from the binary search tree is accomplished as follows: Delete V and the edge on V and C, and replace the tree formed by V and its descendants by the treeformed by C and its descendants. 66. Draw the binary search tree obtained by deleting < from the binary search tree in Exercise 53. 67. Repeat Exercise 66 for the vertex 3 in the binary search tree of Exercise 48. In a binary search tree, deletion of a vertex V with 2 children is accomplishedasfollows. Find the largest item L in the left subtree of V. If L has no left child, delete L and the edge on L and its parent, and replace V by L. If L has a left child C, delete L and the edge on L and C, replace the treeformed by L and its descendants by the tree formed by C and its descendants, and then replace V by L. 68. Draw the tree obtained by deleting 9 from the binary search tree in Exercise 52. 69. Draw the tree obtained by deleting 3 from the binary search tree in Exercise 51. 70. Draw the tree obtained by deleting ORD from the binary search tree in Exercise 50. 71. Draw the tree obtained by deleting WRITE from the binary search tree in Exercise 50. 72. Draw the tree obtained by deleting 27 from the binary search tree in Exercise 52. 73. Draw the tree obtained by deleting 4 from the binary search tree in Exercise 52. 74. Prove that the inorder listing of the vertices in a binary search tree gives the natural order for the elements in the tree.
HISTORICAL NOTES
Arthur Cayley
Unlike the real-world motivation of the Konigsberg bridge problem, the study of trees got its start in considerations related to operators in differential calculus. The intuitive concept of a tree was first employed by the Germans G. K. C. Von Staudt (1798-1867) and Gustav Kirchhoff (1824-1887) in separate articles in 1847. Kirchhoff's article dealt with an extension of Ohm's laws for electrical flow. However, the introduction of the term 'tree' and the mathematical development of the concept came from the English mathematician Arthur Cayley (1821-1895) in 1857. Cayley noted that the number of rooted trees with n edges could be found by observing the effect of removing the root vertex from the tree and examining the remaining collection ot rooted trees. This observation, combined with some of the fundamental ideas concerning
266
Chapter 4
Trees
Jamesh JUNCp11
Sylvester
generating functions, led to a fonnula. Cayley's research in this area continued through the mid-1870s when he discovered a method for counting the number of unrooted trees. Around the same time, the French mathematician Camille Jordan (1838-1922) began a systematic study of graphs. As part of this, he focused on the question of when two graphs are essentially the same but have different representations, that is, when the graphs are isomorphic. In the special case when the vertices can be relabeled to result in an isomorphism, the mapping is called an automorphism. He noted that the complete graph IC, has n! automorphisms. Jordar also noted that certain trees have a special vertex, or vertices, called a centroid or biceneroids, that are preserved by automorphisms. This work drew the notice of Cayley, who used Jordan's concepts in 1881 to develop a more elegant proof of his result on the number of rooted trees. One of Cayley 's last major contributions to the development of trees was his proof in 1889 that the number of ways of joining n separate labeled vertices to form a tree is given by nn-2. (See Exercise 38 in Section 4.1.) This result was proved independently in 1918 by the German Heinz Prufer (1896-1934) [72]. Other attempts were made Iry James Joseph Sylvester (1814-1897) and William Kingdon Clifford (1845-1879) to develop an algebra of graphs, trees specifically, to develop and enumerate the various different compounds possible by joining atoms of various substances. While the graphical representations resulting from these efforts has had an enormous impact on chemistry, the enumeration attempts eventually failed. One side-product of these efforts was the use of the word 'graph,' which first appeared in an article in Nature authored by Sylvester in 1878. While Kirchhoff's initial work focused on electrical networks, his ideas were not lost on the American mathematicians George David Birkhoff (1884-1944) and Oswald Veblen (1880-1960). Veblen's analysis of Kirchhoff's work resulted in 1922 in a theorem that every connected graph contains a tree, called a spanning tree, which includes every vertex of the graph. In 1956, Joseph B. Kruskal (1928- ), and in 1957, Robert C. Prim (1921- ), coworkers at the Bell Telephone Laboratories in Murray Hill, New Jersey, developed the algorithms that bear their names b r finding a minimum spanning tree in a weighted graph. Their work opened new approaches to network designs for communication systems [72].
SUPPLEMENTARY EXERCISES 1. At the Illinois FBI office, Special Agent Jones is working with 7 informants who have infiltrated a gambling ring. She needs to arrange for the informants to comnmu nicate with each other in groups of two in such a way that messages can be passed on to others. For secrecy. the number of meeting places must be kept as small as possible. How many meeting places must Agent Jones find? 2. What is the smallest number of colors needed to color a tree with n vertices, where n > 2? 3. If m > 2, for which n is
KCmn
a tree? (The graph 1Q, 1 n i; defined in Exercise 52 of Exercises 3.2.)
4. Ask a chemist about the chemical structure of benzene .nd draw a graph describing it. Is it a tree? 5. Prove that if there is a vertex of degree k in a tree, then there are at least k vertices of degree 1. 6. Prove that if a tree has n vertices with degrees dl, d2 , . ., dn, then the sum of the degrees is 2n -2. 7. Prove that if dX, d 2 ,
some di > 1.
. . ., dn are positive integers with stun equal to 2n -2, then some di
= 1, and if n > 3, then
267
Supplementary Exercises
8. For n > 2, suppose di, d2, . . , dn are positive integers with sum 2n - 2. Prove that there is a tree with n vertices having degrees di, d2, . ., d. (Hint: Use mathematical induction on n.) 9. Find all the spanning trees of the following graph.
10. For a connected graph, show that the process of selecting edges so that an edge is not selected if it forms a cycle with edges already selected yields a spanning tree. 11. Suppose g is a connected graph with 10 vertices and 19 edges. What is the maximum number of edges that can be removed from g such that the remaining graph is still connected? Justify your answer. 12. For the following graph, use the breadth-first search algorithm to find a spanning tree. (Start with B and use alphabetical order when there is a choice for a vertex.)
G H K L
13. If 5 is a connected weighted graph and all the weights in 5 are distinct, must distinct spanning trees of 9 have distinct weights? Justify your answer. 14. Use Prim's algorithm to find a minimal spanning tree for the following two weighted graphs. (Start at G, and if there is a choice of edges to use in forming a minimal spanning tree, select edges according to alphabetical order.) Give the weight of the minimal spanning tree found. B
9
3
I
A
a
5
B
d 4
EmD
E
In
4
m 7 0F
4
A A:
C
f
g
2 KGn
3
,
I
D
j
9 H,
2
p 4
8
F
b
s
5 I
.
u
,
6
j
V
5
K
w
6
L
15. Show that if g is a connected weighted graph and e is an edge of minimum weight incident on some vertex V, then there is a minimal spanning tree containing e.
268
Chapter4
Trees
16. Can a maximal spanning tree in a connected weighted graph (not a tree) contain an edge of smallest weight? If so, give an example. If not, give a proof. 17. Can Kruskal's algorithm be modified to find a spanning tree that is minimal with respect to all those containing two specified edges? Justify your answer. (See Exerc is 42 in Section 4.2.) 18. Apply depth-first search to each graph below to obtain a depth-first search numbering of the vertices. If there is a choice of vertices, choose the vertex that appears first in alphabetical order. A
D
G E
H
E
F
D
H
B
19. Use the depth-first search numbering obtained for each graph in Exercise 18 to form the spanning tree described in Theorem 4.7. 20. Use Theorem 4.8 and the depth-first search numbering obtained in Exercise 18 to assign directions to the edges that will make each graph in Exercise 18 into a strongly connected directed graph. 21. Show that an edge {A, BI in a connected graph is a bridge if and only if every path from A to B includes {A, B}. 22. Show that an edge of a connected graph is a bridge if aid only if it is not in any cycle. 23. Use backtracking to find a solution to the 6-queens problem. 24. Use backtracking to construct a sequence of length 10 composed of the digits 1, 2, 3 with the property that nowhere in the sequence are there two adjacent subsequences that are identical. 25. For positive integers p, q, and n, where p + q = n, does there exist a rooted tree with n vertices having p internal and q terminal vertices? Justify your answer. 26. A foreign language facility has 28 CD players that must be connected to a wall socket with four outlets. Extension cords having four outlets each are to be used to make the connections. What is the least number of cords needed to get these CD players connected so that they can be used? 27. How many vertices are there in a rooted tree with p internal vertices, each having exactly q children? Justify your answer. 28. How many terminal vertices are there in a rooted tree with p internal vertices, each having exactly q children? Justify your answer. 29. Consider 3' or fewer coins that are identical except bor one that is lighter. Show by mathematical induction that they can be tested in at most n weighings on a balance sale to find out which one is lighter. 30. Suppose T is a rooted tree where every vertex has at most k > 2 children and the length of the longest path from the root to a terminal vertex is h. Prove that kh+1
(a) T has at most
k
-
I
vertices and
(b) if some vertex has k children, then T has at least h - k vertices. 31. For the tree obtained by applying depth-first search to a connected graph, show that if a vertex with depth-first search number k has m descendants (relative to the depth h-first search tree) with m > 1, then their depth-first search numbers are k + 1, k + 2, . . ., k + m.
269
ComputerProjects
32. Prove that when depth-first search is applied to a connected graph, one of the vertices on a back edge is an ancestor of the other (relative to the depth-first search tree). 33. Prove that when depth-first search is applied to a connected graph, one of the vertices on an edge is an ancestor of the other (relative to the depth-first search tree). For Exercises 34-35 let depth-first search be applied to a connected graph g as in Theorem 4.10, and let A be a vertex in 5. 34. If the depth-first search starts at A, prove that A is an articulation point of g if and only if A has more than one child. (See Exercise 36 of Section 4.3.) 35. If depth-first search does not start at A, prove that A is an articulation point of 9 if and only if, for some child C of A, there is no back edge between C or any of its descendants and an ancestor of A. The height of a binary tree is the maximum of the levels of its terminal vertices. 36. If T is a binary tree with n vertices and height h, prove that n < 2 37. Prove that a binary tree of height h has at most
2h
-h+
1.
terminal vertices.
A binary tree with height h is called balanced when the only vertices with no children are at level h or h
-
1.
38. Construct a balanced binary tree with 8 vertices. 39. Construct two distinct binary trees, each with more than one vertex, so that each has the same preorder listing and the same postorder listing as the other. 40. A complete binary tree has vertices V = {l, 2, 3, 4, 5, 6, 7, 8, 9, 101 with a postorder listing of 4, 5, 2, 8, 9, 6, 10, 7, 3, 1. Construct the tree if the height of the tree is 3. 41. Write the associative law for multiplication in Polish notation and in reverse Polish notation. 42. Write the distributive law of multiplication over addition in Polish notation and reverse Polish notation. 43. Does the set {00, 01, 100, 1010, 1011 , 11 of codewords have the prefix property? Justify your answer. 44. Can there be a set of 7 codewords with the prefix property that contains 11, 101, 0101? 45. Draw a binary tree that generates the following codewords at the terminal vertices: 01110.
1, 00, 10, 010, 0110, 01 111,
46. Construct an optimal binary tree for the weights 1, 3, 5, 7, 9, 11, 13, 15. In the construction, select a vertex with children in preference to a vertex without children, and use as the left child the vertex of smaller weight or the vertex with more children if the two vertices have equal weights. 47. Construct a binary search tree for the words in the order given in the sentence 'Gladly would he learn so that others can be taught.'
COMPUTER PROJECTS Write a computer program having the specified input and output. 1. Given a graph, determine if it is a tree. 2. Given a rooted tree, find the internal vertices, the terminal vertices, and the root. 3. Given a graph, use the breadth-first search algorithm to find a spanning tree if one exists. 4. Given a graph, use the breadth-first search algorithm to determine if it is connected.
270
Chapter 4
Trees
5. Given a graph, use the depth-first search algorithm to assign labels to its vertices. 6. Given a graph, use the depth-first search algorithm to determine if it is connected. 7. Given a graph, use the depth-first search algorithm to find a spanning tree if one exists. 8. Given a graph, use the discussion before Example 4.15 to determine if it has a bridge. 9. Given a weighted graph, use Prim's algorithm to find a minimal spanning tree if one exists. 10. Given a weighted graph, use Kruskal's algorithm to [id a minimal spanning tree if one exists. 11. Given a weighted graph, use Prim's algorithm to find a maximal spanning tree if one exists. 12. Given a binary tree, use the preorder traversal algorithm to give a preorder listing of the vertices. 13. Given a binary tree, use the postorder traversal algorithm to give a postorder listing of the vertices. 14. Given a binary tree, use the inorder traversal algorithm to give an inorder listing of the vertices. 15. Given an arithmetic expression in Polish notation, evaluate it. 16. Given an arithmetic expression in reverse Polish notation, evaluate it. 17. Given nonnegative real numbers wu, W2, . .w, use Huffman's optimal binary tree algorithm to construct an optimal binary tree for the weights wI, W2, . ., .Wn. 18. Given words a, a2 , . . ., a, use the binary search tree algorithm to construct a binary search tree with vertices labeled al, a2 , . . *, a,. 19. Given a binary search tree and an element a, use the binary search tree search algorithm to decide if a is in the tree. 20. Given a tree T with n labeled vertices, use Prufer's algorithm to construct a list of numbers that uniquely describes T. 21. Given a positive integer n, use backtracking to determine a solution to the n-queens problem. 22. Given a graph, use Theorem 4.8 to transform it into a strongly connected directed graph, if possible.
SUGGESTED READINGS 1. Aho, Alfred V., John E. Hopcroft, and Jeffrey D. Ullmnan. Data Structures and Algorithms. Reading, MA: Addison-Wesley, 1983. 2. Balaban, A. T. Chemical Applications of Graph Theory New York: Academic Press, 1976. 3. Bogart, Kenneth P. Introductory Combinatorics, 3rd ed. San Diego, CA: Academic Press, 1999. 4. Horowitz, Ellis and Sartaj Sahni. Fundamentals of CcnputerAlgorithms. New York: Freeman, 1984. 5.
. Fundamentals of Data Structures in Pascal. 41h ed. New York: Freeman, 1992.
6. Hu, T. C., T. C. Shing, and Y. S. Kuo. Combinatorial4.'gorithms.Reading, MA: Addison-Wesley, 1982. 7. Knuth, Donald E. The Art of ComputerProgramming,vol. 1: FundamentalAlgorithms, 2nd ed. Reading, MA: Addison-Wesley, 1973. 8. Liu, C. L. Elements of Discrete Mathematics, 2nd ed. New York: McGraw-Hill, 1985. 9. Reingold, Edward, Jurg Nievergelt, and Narsingh Deo. CombinatorialAlgorithms. Englewood Cliffs, NJ: Prentice Hall, 1977. 10. Stubbs, Daniel and Neil W. Webre. Data Structures with Abstract Data Types and Pascal. Monterey, CA: Brooks/Cole, 1985. 11. Tarjan, Robert Endre. Data Structuresand Network Aigorithms. Philadelphia, PA: SIAM, 1983.
Matching 5.1 Systems of Distinct Representatives 5.2 Matchings in Graphs 5.3 A Matching Algorithm 5.4 Applications of the Algorithm 5.5 The Hungarian Method
Many combinatorial problems involve matching items, subject to certain restrictions. An example is the problem of assigning airline pilots to flights (introduced in Section 1 .2). Another example is the assignment of pairs of participants at a conference to rooms so that roommates have the same smoking preference and sex. Sometimes an optimal matching may be desired. For example, a basketball coach must assign a player to guard each player on the opposing team in such a way as to minimize the opponents' total score. Such problems will be treated in this chapter.
5.1
+
SYSTEMS OF DISTINCT REPRESENTATIVES The same matching problem may be viewed in various ways. As an example, let us consider the summer schedule of classes of the English department at a small college. There is a demand for 6 courses. To keep things simple, we will call these Course 1, Course 2, . . ., Course 6. Certain professors are available to teach each course, as given in the following table. Course
Professors
I
Abel, Crittenden, Forcade
2
Crittenden, Donohue, Edge, Gilmore
3
Abel, Crittenden
4
Abel, Forcade
5
Banks, Edge, Gilmore
6
Crittenden, Forcade
271
272
Chapter5 Matching
For brevity we will denDle the professors by A, B, C, D, E, F, and G, according to their initials. In order to distribute the summer teaching jobs as fairly as possible, it is decided thai- no professor should teach more than one course. The question is whether all 6 courses can be taught, subject to this restriction. If not, what is the maximum number of courses that can be taught? This is a problem of exactly the same sort as that of assigning airline pilots in Section 1.2. With only 6 courses and 7 professors we could probably find the answer by considering all possible matchings. One systematic way of doing this is the following. Let PI denote the set of professors available to teach Course 1, P2 the set of professors available to teach Course 2, etc. Thus, PI = {A, C, F), P2 = IC, D, E, G} P3 = {A, C), P4 = {A, F), P5 = {B, E, G}, P6 = {C, FL.
If we forget for the moment the restriction that no professor teach more than one course, then a possible assignment of a professor to each course consists of a 6-tuple (xI, x2 , X3, X4, x5 , x6) where xi E PI, x2 E P2 , etc. This is an element of the Cartesian product PI X .P2 x P3 X P4 x P5 x P6 ,
which has 3 4 2. 2. 3 2 288 elements. We need to know whether any of these 288 6-tuples has all its entries distinct (so that no professor teaches more than one course). Checking this without the help of a computer would be possible but extremely tedious. As in the case of the pilot assignment problem, however, such crude methods of searching for a solution quickly get beyond the capability of even a computer as the number of items to be matched gets larger. For example, if there were 30 courses and 3 professors available for each, then the Cartesian product would contain 330 elements, and it would take a computer checking one million of these per second more than six years to go through them all. There is a name for the sort of sequence of distinct elements, one from each of a given sequence of sets, that vie are seeking in this example. Let SI, S2, . , S, be a finite sequence of sets, not necessarily distinct. By a system of distinct representatives for Si, S2, . - S,n we mean a sequence xI, x2 , . . , Xn such that xi E Si for i = 1, 2, . . ., n, arid such that the elements xi are all distinct. .
+
Example 5.1 Find all systems of distinct representatives forthe sets SI = {1, 2, 31,S2 = {1, 31, S3 = {1,3},S4 = {3,4, 51.
5.1 Systems of DistinctRepresentatives
273
Notice that the elements chosen from S2 and S3 must be 1 and 3 in some order. There are four systems of distinct representatives: 2, 1,3,4 2,3, 1,4 2, 1,3,5 2,3,1,5.
+
Example 5.2 Find all systems of distinct representatives for the sets SI = {2, 3}, S2 = {2, 3, 4, 5], S3 = {2, 3], S4 = {3}. There are none. For if xl, x 2 , x3 , X4 were a system of distinct representatives, then xi, x3 , and x4 would be 3 distinct elements of SI U S3 U S4 = {2, 31, which is impossible. +
+
Example 5.3 How many systems of distinct representatives does the sequence S, S, S, S have, where S = {1, 2,3,41? In this case a system of distinct representatives is simply a permutation of the integers 1, 2, 3, 4. By Theorem 1.1 there are exactly 4! = 24 of these. +
Now we return to our problem of assigning a professor to each summer English course. We are looking for a system of distinct representatives for the sequence Pi = (A, C, F], P2 = {C, D, E, G}, P3 = {A,C], P4 = {A, F),
P5 = (B, E, GI, P6 = IC, F). The problem seems small enough that we might expect to find the solution, if there is one, by simply trying different combinations. Yet perhaps the best we can come up with is to cover 5 of the 6 courses. For example, we might assign the first 5 courses as in the list below. Course I to Abel Course 2 to Donohue
274
ChapterS
Matching
Course 3 to Crittenden Course 4 to Forcade Course 5 to Banks We might suspect that it is not possible to do better than this, but it is difficult to be certain. We would like a Aay to convince ourselves that no assignment of all 6 courses is possible without going through all 288 possibilities. There is a way, and the key to it is to be found in Example 5.2. If we could discover some collection of sets chosen from PI through P6 , the union of which contained fewer elements than the number of sets in the collection, then we would know that a system of distinct representatives was impossible. Since this is a somewhat abstract idea, we will exhibit such a collection to make the argument more concrete. How such a collection might be found will be covered in a later section of this chapter. The collection we have in mind is PI, P3 , P4 , and P6. Notice that PI U
P' 3
U P4 U P6 =
{A, C, F),
and the argument is the same as in Example 5.2. Suppose we had a system of distinct representatives xI, x., .. , x6. Then xi, X 3 , X4 , and x6 would comprise 4 distinct elements lying in the union of the sets PI, P3, P4, and P 6. But this is impossible because this union contains only 3 elements. There are only 3 professors (Abel, Crittenden, and Forcade) available to teach 4 of the courses, and so an assignment where no professor teaches more than 1 course cannot be made. We have found a general p inciple, which could be stated as follows. Suppose SI, S2 , . . ., Sn is a finite sequence of sets, and suppose I is a subset of {l, 2, . . . , n} such that the union of the sets Si for i E I contains fewer elements than the set I does. Then SI, S2,. . . Sn has no system of distinct representatives. In our example (taking Si = Pi for i = I to 6), the set I is {l, 3, 4, 61. Finding such a set I enables us to be sure that no system of distinct representatives exists. The person responsible for assigning summer courses in our example will have to assign the same professor to teach 2 courses if all 6 courses are to be given. Professors with no summer employment may object that this is unfair, but the scheduler can use the set I to demonstrate to them that there is no way to cover all the courses otherwise. If a sequence of sets has no system of distinct representatives, is there always some set I as above that can be used to demonstrate this fact in a compact way? The answer is yes, but the proof is somewhat complicated. This is the content of a famous theorem due to Phil] ip Hall. Theorem 5.1
5 S2 .. ,Sn has a system of disa -,.'he:n The sequence of finite setsS, tinct representatives if and orly if whenever I is a subset of {l, 2, . . ., n}, then the union of the sets Si for i ( I contains at least as many elements as the set I does.
5.1 SystemsofDistinctRepresentatives
275
The 'only if' part of this theorem amounts to the principle we have already discovered. The 'if' part will be proved in Section 5.4 in a different context; for a direct proof see Exercise 31. +
Example 5.4 We will use Hall's theorem to show that the sequence Si = {A, C, El, S2 = 1A, B},
S3 = {B, E} has a system of distinct representatives. The subsets I of {1, 2, 3), and the corresponding unions of sets Si are given below. I
Union of Sets Si, i E I
0
0
11)
{A, C, El
{2}
(A, B)
131
{B, El
11,21
(A, B, C, El
11,31
{A, B, C, El
12,3}
IAB, E
11,2,31
{A,B,C,E}
Since every set on the right has at least as many elements as the corresponding set on the left, the sequence has a system of distinct representatives. Of course, it is easy in this case to find one by inspection, for example, A, B, E. +
Applying Hall's theorem to our course scheduling example would involve examining the 26 = 64 subsets of f 1, 2, 3, 4, 5, 61 and computing the corresponding union of sets Pi for each. (Of course, we would find that no system of distinct representatives exists.) Although this may seem better than our previous method, which entailed looking at 288 possible assignments, it is still not practical for finding whether a system of distinct representatives exists, since if there are n sets Si, then there are 2' sets I, and 2' increases very quickly with n. Also, although the theorem tells us when a system of distinct representatives exists, it does not tell how to find one. Efficient methods for finding optimal matchings will be developed later in this chapter. Readers interested in extensions of Hall's theorem should consult [7] in the suggested readings at the end of this chapter.
276
ChapterS Matching
EXERCISES 5.1 In Exercises 1-6 tell how many systems of distinct representativesthe given sequence of sets has.
1. {1,2), {2, 3), {I, 3)
2. L1, 41, {2}, L2, 3}, 11, 2, 3)
3. {1,2,3),{1,2,3),{1,2,3) 5. {1,2,5}, 12, 1),{3,4}, {1,51, {1,2,5), {2,4,5)
4.11,2,3,4,51,11,2,3,4,51 6i. {1,2,3), {4,5}, {6,7}
In Exercises 7-10 a sequence of sets SI, S2, . . - Sn is given. For each subset I of {1, 2, .. , n} compute the union of the correspondingsets Stand determine from the se unions whether the sequence has a system of distinct representativesor not.
S. {1,2,5), 15, 1}, {1,2), (2,5)
7. {1,2,4},{2,4},{2,3},{1,2,3}
9. (1),{1, 2), 1,2, 3}, t1, 3}
904'}'267'0
In Exercises 1-16 a sequence of setsS, , . S. Snis given. Find a subset of of the correspondingsets Si has fewer elements than I doc s
11. 11, 2), 12, 3), 0 13. (1,2,3},{1,2,41,L1,3,4),{1,2,3,41,{2,3,4} 14. {1, 2), {2, 3), (5), 1I, 3), {4, 5), 14, 5)
{1,2,..,n such that the union
12. tI), {1,2), {2, 3}, {2}
15. {2,5,7},{I,3,4,5},(5,7),{2,7),{1,3,61,{2,51 16. {1,2),{2,4,5,7},{1,2,3,5,6), {1,4,7}, 12,5,7}, {1,4,5,7), {2,4,71 17. Let Si
= {1, 2, . . , n) for i = 1, 2, . Si, S2, .. , S, have?
n. How many systems of distinct representatives does the sequence
18. Let Si = 11, 2, .. , k) for i = 1, 2, .
n, where n < k. How many systems of distinct representatives does
SI, S2, .. , S, have? 19. Let Si = (1, 2, .. , k) for i = 1, 2, . n, where k < i. How many systems of distinct representatives does S, S2, . . - S, have? 20. Show that if the nonempty set Si has ki elements for i = 1, 2, .. , n, then the sequence SI, S2, .. , S, has exactly kI k2 .. kn systems of distinct representatives if and only if the sets S are pairwise disjoint. 21. Mr. Jones brought home 6 differently flavored jelly beans for his 6 children. However, when he got home he found out that each child likes only certain flavors. Amy will eat only chocolate, banana, or vanilla, while Burt likes only chocolate and banana. Chris will eat only banana, strawberry, and peach, and Dan will accept only banana and vanilla. Edsel likes only chocolate and vanilla, and Frank will eat only chocolate, peach, and mint. Show that not every child will get a jellybean he or she likes. 22. Five girls go into a library to get a book. Jennifer wants to read only The Velvet Room or Daydreamer. Lisa wants only Summer of the Monkeys or The Velvet Room. Beth and Kim each want only Jelly Belly or Don't Hurt Laurie!, while Kara wants either one of the latter two books or else Daydreamer. If the library has only one copy of each book, can each girl take out a book she wants? 23. Show thatif theunion of the sets Si, S 2 , . . ,S, contains more than n elements, and if the sequence Si, S 2 has a system of distinct representatives, then it has more than one.
5, S,
5 2 Matchings in Graphs
277
24. Let S be a set with m elements, and let Si = S for i = 1, 2, . . ., n. Show that the number of systems of distinct n} into representatives of SI, S2 , Sn is the same as the number of one-to-one functions from {1, 2, . (I, 2, . ., m). 25. In the example in Section 1.2, there are 7 cities and a set of pilots who want to fly to each city. Either find a system of distinct representatives for this sequence of sets or else prove that none exists. 26. Let Si, S2, .. , S, and T1, T2, . T, be sequences of sets such that Si and Tj are disjoint for all i and j. Show that the sequence SI, S2 . Sm, TI, . . T, has a system of distinct representatives if and only if SI, S2, .. , Sm and TI, T2 , .. , T,do.
27. Let SI, S2, . .5, S,be a sequence of sets such that JSi I > i for i = 1, 2, n. Show that the sequence has a system of distinct representatives. 28. Let S = {1, 2, . i} for i = 1, 2, . n. How many systems of distinct representatives does SI, S2 . Sn have? 29. Let Si = {O. 1, 2, i } for i = 1, 2, n. How many systems of distinct representatives does SI, S2, .,Sn have? 30. Suppose that Si C Si+1 for i = 1, 2, . n - 1, and that ISj = ki for i = 1, 2, . n. How many systems of distinct representatives does SI, S2 . ,Sn have? n}, then 31. A sequence of finite sets SI, S2, . ., S, is said to satisfy Hall's condition if whenever I C {1, 2, . the number of elements in the union of the sets Si, i E I, is at least IIl. The 'if' part of Hall's theorem amounts to the statement that any sequence satisfying Hall's condition has a system of distinct representatives. Prove this by using the strong induction principle on n. To prove the inductive step, consider two cases: (a) whenever I is a nonempty subset of {1, 2, . ., k + 1} with fewer than k + 1 elements, then the union of the sets Si for i E I has at least one more element than I does; (b) for some nonempty subset I of (1, 2, .. , k + 1} with fewer than k + 1 elements, the union of the sets S for i E I has the same number of elements as I. 32. For r < n, an r x n Latin rectangle is an r x n matrix that has the numbers 1, 2, . ., n as its entries with no number occurring more than once in any row or column. An n x n Latin rectangle is called a Latin square. Show that if r < n, then it is possible to append n -r rows to an r x n Latin rectangle to form a Latin square. (Hint: Use Hall's theorem.)
5.2
+
MATCHINGS IN GRAPHS There is a symmetry in matching problems that is hidden when they are formulated in terms of sets as in Section 5.1. For example, when we were trying to match a professor with each English course, we associated with each of the 6 courses a setthe set of professors who could teach that course. But we could just as well have turned the problem around and considered for each professor the set of courses he or she can teach. This symmetry is displayed better if we draw a graph as we did in Figure 1.10 for the airline pilot problem. We will let the courses and professors be the vertices of the graph, and put an edge between a course and a professor whenever the professor can teach the course. The result is shown in Figure 5.1. The graph we get is of a special form, since no edge joins a course to a course, or a professor to a professor. We say a graph with vertex set V and edge set E
278
ChapterS
Matching
is bipartite in case V can be written as the union of two disjoint sets VI and V2 such that each edge joins an element of VI with an element of V2 . The graph of Figure 5.1 is bipartite since we could take VI to be the set of courses and V2 to be the set of professors. Courses
Professors
I
Abel
2
Banks
3
Crittenden
4
Donohue
5
Edge
6
Forcade Gilmore FIGURE 5.1
+
Example 5.5 The graph shown in Figure 5.2 is bipartite (even though it may not look it) because every edge goes between an odd-numbered vertex and an even-numbered one. Thus we could take VI = {1, 3, 5,7} and V2 =12,4, 6, 81.+ 2
3 86 FIGURE 5.2
1
2
4
5
7FIGURE 5.3
Example 5.6 The graph shown in Figure 5.3 is not bipartite, as we can see by considering the vertices 1, 3, and 4. If, for example, 1 is in VI, then 3 must be in V2 . But then 4 can be in neither of these sets. + In our course assignment problem, we wanted to pair up courses and professors. In terms of the graph representing the problem, this means that we want to choose a subset, say M, of the set of edges. No course can be taught by two professors, nor can a professor teach more than one course. This means that no vertex of the graph can be incident with more than one edge of M. In this application
5.2 Matchings in Graphs
279
we would like M to contain as many edges as possible. These considerations motivate the following definitions. A matching of a graph is a set M of edges such that no vertex of the graph is incident with more than one edge of M. A maximum matching is a matching such that no other matching contains more edges. Example 5.7
The colored edges in Figure 5.4(a) form a matching of the bipartite graph pictured, since no two of them are incident with the same vertex. This matching of 3 edges is not a maximum matching, however, since Figure 5.4(b) shows another matching with 4 edges. Note that even though the first matching is not a maximum matching, no edge could be added to it and still have a matching. A maximum matching need not be unique. Figure 5.4(c) shows another maximum matching of the graph. + I
A
1
A
1
A
2
B
2
B
2
B
3
C
3
C
3
C
4
D
4
D
4
D
(a)
(b)
(c)
FIGURE S.4
Our definition of a matching of a graph did not specify that the graph be bipartite. Finding a maximum matching is easier in the case of a bipartite graph, however, and many applications give rise to bipartite graphs. The following example gives a case when a maximum matching of a nonbipartite graph is desired. +
Example 5.8 A group of United Nations peacekeeping soldiers is to be divided into 2-person teams. It is important that the 2 members of a team speak the same language. The following table shows the languages spoken by the 7 soldiers available. If we make a graph, putting an edge between 2 soldiers whenever they speak a language in common, the result is exactly the graph of Figure 5.3, which we saw was not bipartite. One matching is pictured in color in Figure 5.5. It is clearly a maximum matching since only one soldier is unmatched. +
280
Chapter S Matching Soldier
3
Languages
I
French, German, English
2
'Spanish, French
3
German, Korean
4
Greek, German, Russian, Arabic
5
Spanish, Russian
6
Chinese, Korean, Japanese
7
Greek, Chinese
6
>
1
2
74
5
FIGURE 5.5
A convenient way to represent: a bipartite graph where every edge joins a vertex of V1 to a vertex of V2 is by a matrix of Os and Is, with the rows corresponding to the elements of VI anc the columns to the elements of V2. We put a 1 in the matrix whenever the vertices corresponding to the row and column are joined by an edge and a 0 otherwise. For example, the matrix of the graph of Figure 5.4 is A
B
LKO
C
D
1 0
I,
0
0
1
1
4
0
0
1
1
Of course, this matrix is unique y determined only if we specify some order for the vertices in VI and V2. Recall hat a matching of a graph is a subset of its edges, and each edge corresponds to a I in the matrix. Two edges incident on the same vertex correspond to Is in the same row or column of the matrix, depending on whether the vertex is in VI or V2 . Thus a matching of a bipartite graph corresponds to some set of Is in the matrix of the graph, no two of which are in the same row or column. Matrices have their cwn terminology, however. By a line of a matrix, we mean either a row or a column. Let A be a matrix. We say that a set of entries of A is independent if no two of them are in the same
5.2 Matchings in Graphs
281
line. An independent set of Is in A is a maximum independent set of Is if no independent set of Is in A contains more elements. We will mark the Is in a particular independent set with stars. The reader should check that the stars in the following three matrices mark independent sets corresponding to the three matchings shown in Figure 5.4. 0
[11*
0 0
1 0
O0 0
1*
I
0
0
0
0
1* 1
0 1*
0 0
1* 0
I 1*
0 I
0 0
1* 0
I 1
0 1*
1
1
O0 0
1
1*-
O
0
1* I
[1*
For example, since one of the edges in the matching shown in Figure 5.4(a) is 11, B }, a star is placed on the 1 in row 1 and column B of the first matrix. Although the language is different, finding a maximum matching in a bipartite graph and finding a maximum independent set of Is in a matrix of Os and Is are really the same problem, and we will use whichever form is more convenient. Graphs are sometimes more accessible to the intuition, while matrices may be better for computational purposes.
Recall Example 5.8, where a group of soldiers was to be broken into 2-person teams speaking a common language. Suppose that before any teams are formed, some of the soldiers are to attend a meeting. It is desired that each possible team should have at least one member at the meeting. Since each edge in the graph of Figure 5.5 represents a possible team, what we need is a set of vertices such that each edge of the graph is incident with at least one vertex in this set. We might want this set to be as small as possible so as to minimize the number of soldiers required to attend the meeting. Such considerations motivate the following definitions. By a covering C of a graph, we mean a set of vertices such that every edge is incident with at least one vertex in C. We say C is a minimum covering if no covering of the graph has fewer vertices. For example, the set {2, 3, 4, 5, 6) may be seen to be a covering of the graph shown in Figure 5.5. This is not a minimum covering, however, since the covering 11, 3, 5, 71 has fewer elements.
+
Example 5.9 Figure 5.6 represents the streets and intersections of the downtown area of a small city. A company wishes to place hot dog stands at certain intersections in such a way that no one in the downtown area will be more than one block from a stand. It would like to do this with as few stands as possible. If we interpret Figure 5.6 as a graph with vertices at the intersections, then our problem is exactly one of finding a minimum covering. One covering is the set of vertices {1, 3, 6, 8, 9, 111. We will see as a consequence of the next theorem that this is a minimum covering. +
282
ChapterS Matching
1
2
3
4
5
61
71
8
9
10
11
12
FIGURE 5.6
The next theorem gives a relation between the matchings and the coverings of a graph. Theorem 5.2
Let a graph have a matching M1 and covering C. Then IMI < ICI. Moreover, if IMI = ICI, then M is a maximum matching and C is a minimum covering.
Proof. By the definition of a covering, every edge of the graph, and in particular every edge in M, is incident with some vertex in C. If the edge e is in M, let v(e) be a vertex in C incident with e. Notice that if el and e2 are distinct edges in M, then v(el) and v(e 2 ) are also distinct, since by definition two edges in a matching cannot share a vertex. Thus there are at least as many vertices in C as edges in M, and so IMI < ICI. Now suppose IA = IC I]f M were not a maximum matching, there would be a matching M' with I.M'I > IMI = ICI, contradicting the first part of the theorem. Likewise if C were not a minimum covering, there would be a covering with fewer than IMI vertices leading to the same contradiction. X In light of the second part of this theorem, we can show the covering given in Example 5.9 is a minimum covering by exhibiting a matching with the same number of elements, namely 6. ODne is indicated by the colored edges in Figure 5.7; of course, Theorem 5.2 also implies that it is a maximum matching. 1 5 9
2 __6
10
3
4
7
8
11
12
FIGURE 5.7
In the case of a bipartite graph, we can translate Theorem 5.2 into matrix language. The vertices of the graph correspond to the lines of its matrix, and an edge is incident with a vertex when the 1 corresponding to the edge is in the line corresponding to the vertex. Thus we define a covering of the Is of a matrix of Os and Is to be a set of lines containing all the Is of the matrix. It is a minimum covering if there is no covering with fewer lines. With these definitions, the following theorem is an immediate consequence of Theorem 5.2.
5.2 Matchings in Graphs
Theorem 5.3
o
283
If a matrix of Os and Is has an independent set of m Is and a covering of c lines, then m < c. If m = c, then the independent set is a maximum independent set and the covering is a minimum covering. Example 5.10 The Scientific Matchmaking Service has as clients 5 men, Bob, Bill, Ron, Sam, and Ed, and 5 women, Cara, Dolly, Liz, Tammy, and Nan. The company believes that 2 people are not compatible if their first names do not contain a common letter. On the basis of this rule, the company constructs the following matrix, in which a 1 means that the man and woman corresponding to the row and column are compatible.
Bob Bill
Ron Sam
Cara
Dolly
Liz
0 0 1
1
11
0t
0
Tammy
Nan
0f 0
0 0
0
1
1
Ed
(0
0J
The company would like to match as many clients as possible; that is, it wants a maximum independent set of Is. Since all the Is lie in just 4 lines, namely the 3rd and 4th rows and 2nd and 3rd columns, it is realized that no independent set of Is can have more than 4 elements. An independent set with 4 elements does exist, however, and one is shown below. 0
1*0
O I I1* O
I 1 0 1
0
O-
l* 0 0 0 0 1* 1 1 0 0 0 0
EXERCISES 5.2 In Exercises 1-6 tell whether the graph is bipartite, and if so give disjoint sets of vertices VI and V2 so that every edge joins a vertex of V1 to a vertex of V2. 1
1
2
5
6 to
9
10
12
13
3
;
4
2
5
8
114
6
7
284
Chapter5 Matching
3.
1
4.
2
2
4
I
5
3
4
5,
1
2
6.
6 7<8
9
10
Il
7. Give a maximum matching for each graph in Exercises 1, 2, and 3. 8. Give a maximum matching for each graph in Exercises 4, 5, and 6. 9. Give a minimum covering for each graph in Exercises 1, 2, and 3. 10. Give a minimum covering for each graph in Exercises 4, 5, and 6. In Exercises 1-] 16 every edge of the graphjoins a vertex cf VI = {1, 3, 5. the matrix of each graph. (Take the vertices in increasing order.)
11. 1 8
2
3
o
4
12.
14
2
4
4 9
8
3 7
~5 14.
13.
1 8
6
11
12
2O
6 3
4
6
1
.
.) to one of
V 2 = {2,
4, 6, .
.}.
Give
5.2 Matchings in Graphs
15.
2
285
16.
1
6
3
5
11
7
8
9
1I0,
3
121
17. Find a maximum independent set of is for the matrices in Exercises 11, 12, and 13. 18. Find a maximum independent set of Is for the matrices in Exercises 14, 15, and 16. 19. Find a minimum covering for the matrices of Exercises 11, 12, and 13. 20. Find a minimum covering for the matrices of Exercises 14, 15, and 16. In Exercises 21 and 22 construct a bipartite graph and the corresponding matrix modeling the situation described. Indicate a maximum matching in the graph and the correspondingmaximum independent set of is in the matrix. 21. Four airplane passengers want to read a magazine, but only 5 are available. Of these, Mr. Brown will only read Time, Newsweek, or Fortune; Ms. Garvey will only read Newsweek or Organic Gardening; Miss Rollo will only read OrganicGardening or Time; and Mrs. Onishi will only read Fortune or Sunset. 22. The Glumby family is going to Europe and each member is to choose one country that he or she knows the language of to study beforehand. Mr. Glumby knows Russian and French; Mrs. Glumby knows only Russian; Sally knows French, German, and Spanish; and Tim knows only French. In Exercises 23 and 24 construct a graph modeling the situation described, andfind a maximum matchingfor it. 23. The church sewing circle wants to break into 2-person groups to make altar cloths. The two people in a group should own the same brand of sewing machine. Ann has a Necchi; Beth has a Necchi and a Singer; Cora has a Necchi, a Singer, and a White; Debby has a Singer, a White, and a Brother; Ellie has a White and a Brother; and Felicia has a Brother. 24. The Weight Whittler Club wants to break up into 2-person support groups. The weights of the two men in a group should differ by no more than 20 pounds. Andrew weighs 185, Bob 250, Carl 215, Dan 210, Edward 260, and Frank 205. In Exercises 25 and 26 model the situation described with a bipartite graph, and construct the corresponding matrix. Find a minimum covering for the graph and indicate the correspondinglines of the matrix. 25. The police department has a policy of putting an experienced officer together with a rookie in a squad car. The experienced officers are Anderson, Bates, Coony, and Dotson, and the rookies are Wilson, Xavier, Yood, and Zorn. Anderson always works with Wilson or Xavier; Bates with Xavier, Yood, or Zorn; Coony with Wilson;
and Dotson with Wilson or Xavier. The captain, who does not know what the teams for the next month will be, would like to call as small a number of officers as possible to tell at least one member of each team its schedule. 26. In mixed doubles Michael always plays with Venus, Martina, or Monica; Andre plays with Lindsay; Pete always plays with Venus or Lindsay; and Patrick always plays with Venus. The tournament director wants to tell each possible mixed-doubles pair its rating with as few phone calls as possible.
286
Chapter5 Matching
27. Show that the matrix of a bipartite graph is a submatriK of the adjacency matrix of that graph, as defined in Section 3.1. A submatrix of a matrix A is a matrix formed by removing some rows or columns (or both) from A. 28. Find a graph in which a maximum matching has fewer edges than a minimum covering has vertices. 29. Show that if a graph contains a cycle with an odd number of edges, then it is not bipartite. 30. Show that if a graph contains no cycle with an odd number of edges, then it is bipartite. 31. Show that a graph is bipartite if and only if it can be colored with two colors. 32. Consider K 20 , the complete graph on 20 vertices. (a) How many edges are in a maximum matching for K:20? (b) How many vertices are in a minimum covering for iC20? 33. Let a graph have a vertex set V, edge set 6£, and adjacency matrix A = [aij]. Show that a subset C of V is a covering if and only if aij = 0 whenever i g C and j is C.
5.3
c
A MATCHING ALGORITHM So far our examples have been snall enough that we could find a maximum matching by trial and error. For larger graphs, however, a better technique is needed;
and, as was indicated in Section 5.1, simple exhaustion of all possibilities soon becomes impractical, even with a computer. There is an efficient algorithm for finding a maximum matching in a graph. For simplicity, we consider the algorithm only for the case of a bipartite graph. To make explaining the algorithm easier, we will present it as a rrmethod of finding a maximum independent set of Is in a matrix of Os and Is. As we saw in the previous section, this is equivalent to the problem of finding a maximum matching in a bipartite graph. We will give an example of the use of the algorithm before we state it in a more formal way later in this section. We start with some independent set of Is. This set could be found by inspection and could even be the empty set! Starting with a larger independent set %ill speed up finding a maximum such set, however. The algorithm will either tell us that we have a maximum independent set of Is,
or else produce an independent set containing one more 1. We continue to apply the algorithm until a maximum independent set is reached. For our example we will use the matrix A
B
C
D
1
1*
0
1
1
2
0
1* 0
0
3
1
1
°
o
4
0
1
0
0]
in which an independent set of I s has been indicated. Notice that if any 1 is added to this set it will no longer be independent. Our algorithm will involve performing two operations on some of the lines of this matrix, operations which we will call
5.3 A Matching Algorithm
287
labeling and scanning. Once a line has been labeled, it will never be labeled again in one application of the algorithm, and the same is true for scanning. A line must be labeled before it can be scanned. We begin by labeling (with the symbol '#') all columns containing no starred is. (If there are no such columns, our set of starred Is is already a maximum independent set.) In our example, this produces the following matrix. A
B
I* 0 0 1* 1 1
2 3
4 -0
1
C
D
1 0 0
11 0 0
0
0-
Now we scan each labeled column for unstarred Is. In column C we find an unstarred 1 in the first row, so we label that row with a C to indicate that the unstarred 1 was found in column C. (In general, row labels are column names, and column labels are row names, except for the labels '#.') Then we put a check mark under column C to indicate that it has been scanned. The matrix now appears as follows. A
B
1 -1
C
D
0
1
1
0
1
0
0
3 1 4 -0
1 1
0 0
0 0
2
C
When we scan column D, we also find an unstarred I in row 1. Since this row has already been labeled, we put a check mark under column D to indicate that it also has now been scanned. Since all labeled columns have been scanned, we now turn our attention to the rows. Only row 1 has been labeled; so we scan it, now looking for starred Is. There is one in column A; so we label this column with a 1 (the row scanned), and put a check mark after row 1 to show that it has been scanned. A
B
C
D
1 r1* 0
2
0
1*
1 0
1 0
3 4
1 0
1 1
0 0
0 0
]CV
Since all labeled rows have been scanned, we go back to scanning columns. Column A is labeled but not scanned; so we scan it for unstarred is. There is
288
Chapter5
Matching
one in row 3; we label that row with an A, since we found it when scanning column A. A
C
D
0
1
1
1 1
B
2
3 4
C,/
1) 1* 0 0 1 1 0 0 -D 1 0 0 I a/ #A/ #'/
A
We are now at a turning point in the algorithm. When we scan the labeled row 3, we find no starred ], and so we mark this row with an exclamation point. This indicates that we will be able to improve on the independent set of is we started with. The labels on the lines of the matrix tell us exactly how to do this. Row 3 is labeled with an A, so we put a circle around the 1 in column A (and row 3). This column is labeled with a 1, so we put a circle around the starred 1 in row 1 (and column A). Row 1 is labeled with a C, so we put a circle around the 1 in column C (and row 1). CoLumn C is labeled with the symbol '#,' so we stop drawing circles at this point. Our matrix now appears as follows. A
B
C
D
1IL* 2 0
0 01 1* 0
I 0
C>,/
3 (f)
I
0
0
A!
4
1
0 #'/
0 #'//
0 1A/
At this point we find a larger independent set of 1s by reversing the stars on the circled Is, that is, by adding a star to any circled 1 without a star, and removing the star from any circled I with a star. The result is an independent set of Is with 3 elements instead of 2. A
B
1
0
1
1
0
1*
0
0
~3 1* 1I 0
0
.11 0
0
7
1
C
0
D
It is instructive to see whai we have done in this example in terms of graphs. Figure 5.8(a) shows the bipartite graph corresponding to our matrix, with the matching of our original set of two Is indicated in color. The three positions circled in our matrix operations correspond to the edges {3, A}, {1, Al, and {1, C I of the graph. These form a simple path from 3 to A to 1 to C. (See Figure 5.9(a).) Since the circled Is of the matrix are alternately starred and unstarred, the edges of this path are alternately in and not in the original matching. Note that, when inserting the circles in our matrix, we start with an unstarred 1 (corresponding lo a labeled row with no stars), and also end with
5.3 A Matching Algorithm
289
1A 2 eN
'
B
2
B
3
<
C
3
C
4
D
(a)
(b) FIGURE 5.8
an unstarred 1 (corresponding to a column labeled with the symbol '#' because it contained no stars). Thus the number of edges in the path must be odd; and reversing which of these edges are in the matching, as shown in Figure 5.9(b), increases the number of edges in our matching by one. The larger matching is shown in Figure 5.8(b).
2
2.
3
3
4.
*D
C
4* (b)
(a) FIGURE 5.9
Now we will apply the algorithm to the matrix with our new set of 3 starred 1s. Since only column D has no starred Is, we start by labeling that column. A
B
C
I
1 0 0
0
1 2 3
I*
0 1* 1* 0 1 0
4
0
1
D
0
Scanning this column for unstarred is leads us to label row 1. 1 2 3 4
A
B
C
[
0
1*
1 0
1*
0o
1* 0 1 0 1 0
D 1
01 0o 0 -
D
290
Chapter5
Matching
Scanning row 1 for starred Is leads us to label column C. I 2 3 4
B
I i)
0
1
I
I*
0
0
1* I
l)
I
C
D
4
0 0 I
D ma
0 0 #AV/
This is the most important point in the present application of the algorithm. When we scan column C there is nothing to label, and we have the following matrix.
1 2
Ai
B
C
I (0
0 1* 1* 0
D I
D>
0 0
3
]*
I
0
4
(0
1
0 0 1+/ #'/
All lines that are labeled have also been scanned, and there is nothing else we can do. This indicates thiat we started with a maximum independent set of I S. We now state our algorithm formally.
Independent Set Algorithm Given an independent set of starred Is in a matrix of Os and 1s, this algorithm either indicates that this independent ;et is a maximum independent set, or else it finds a larger independent set. Step I Step 2
(start) Label each column containing no starred 1. (scan and label) repeat Step 2.1 (scan columns) For each collns that is labeled but not scanned, look at every unstarred int that column. If such am is in an
unlabeled row, then label that row with the name of the column being scared. Mark the column to indicate that it has been scanners Step 2.2 (scan rows) For each row that is labeled but not scanned, look for a
5.3 A MatchingAlgorithm
Step 3
291
starred 1 in that row. If there is a starred 1 in the row, then label the column containing the starred 1 with the name of the row being scanned. Mark the row to indicate that it has been scanned. until either some labeled row contains no starred1 or all the labeled rows and all the labeled columns have been scanned (enlarge the independent set if possible) if some labeled row contains no starred 1 Step 3.1 (backtracking) Find the first labeled row that contains no starred 1. Circle the 1 in this row and in the column that the row is labeled with. Circle the starred 1 in this column and the row that this column is labeled with. Then circle the unstarred 1 in this new row and in the column that this row is labeled with. Continue in this manner until a 1 is circled in a column labeled in step 1. Step 3.2 (larger independent set) Reverse the stars on all the circled Is. This gives an independent set of Is with one more element than the original set. otherwise
Step 3.3 (no improvement) The present independent set is a maximum independent set. end if
This algorithm is due to Ford and Fulkerson, and can be found in suggested reading [3] at the end of this chapter. We will prove that it does what it says it does in the next section. Of course, with a change of language the algorithm could just as well be applied to a graph, but there are complications if it is not bipartite. A modification of the algorithm that applies to arbitrary graphs can be found in suggested reading [2]. Let us examine the complexity of this algorithm. In our analysis, we will use the word 'operation' in a somewhat vague way to indicate looking at some entry or row or column label of a matrix and perhaps taking some simple action such as applying or changing a symbol. Suppose a matrix of Os and Is has m rows and n columns. Step 1 involves looking at all mn entries in the matrix, which we count as mn operations. After this the algorithm alternates between steps 2.1 and 2.2, both of which involve scanning. In order to scan one of the n columns we need to look at the m entries in that column, so all column scanning will take at most mn operations. Likewise, row scanning will take at most nm operations.
292
ChapterS
Matching
If we get to step 3.3, we are done, so we analyze steps 3.1 and 3.2. Backtracking will take at most m 4- n operations, since each 1 we circle can be associated with a distinct row or column. Actually, we could combine step 3.2 into step 3.1 with no additional work, reversing the stars as we backtracked. Thus one application of the algorithm will take at most 3mn + m + n operations. To build up to a maximum indep dent set of Is, the algorithm will have to be repeated at most minim, ni tirrws, even if we start with the empty set as our first independent set of Is. Thus the complexity of the algorithm for finding a maximum independent set of ones in an m by n matrix is of order no more than (3mn + m + n) - minim, n}. For the case m = n = 30, fewer than 90,000 operations would be necessary, and a fast computer could do the problem in less than one second.
Assigning Courses As another example of the use of the algorithm, we will go back to the example of assigning English professor; to courses of Section 5.1. The matrix of the graph shown in Figure 5.1 is the following. A
B
C
D
E
F
G
0
0
1
1F 0
1
2 3
0 1
0 0
1* 1 1 0
4 5
I 0
0 0 1* 0
1
6L
0
1
0
1 0 0 0
1 0
0 0
0 1
1 0
0 1
0
0
1
0
The independent set of Is shown was chosen by taking the first available I in the first row, second row, etc. subject to the condition that we not choose two is in the same column. We will show what our matrix looks like after each step in the algorithm. A
B
C
D
E
F
G
1
1
0
1
0
0
1
0
2 3 4
0
0 0 0
1*1 1 0 0 0
5
0
1 1
6L o
1 0 1 0 0 0 0 1* 0
1* 0
0
1
0
1
o
0
0 1
0
#
I#
1
After Step 1
5.3 A Matching Algorithm
A
B
C
D
E
F
G
I
1
2 3
1*
0 1 0 0 0 0
0 1 0 0 1 0
1 0 0 1* 0 1
0 1 0
1* 0 0 1 0 4 1 0 5 0 1* 6 L 0
1 0 0 1
D
01 1
E
0
After Step 2.1 A 1*
I
0
2
3
B
C
D
E
0
1
0
1 0 0 0
1* 0 1 0 0 1* 0
4
1 1
5
0
6L
O o
1
5
2
F
G
0
1
0
1 0 0 1
0 0 1* 0
1 0 0 1
0 0 1 #V #V/
0
DV
J
EV,
After Step 2.2 A
1 2
3 4
B
1* 0 0 1 1
0 0 0
S O 1* 6
0
0
C
D
E
F
1 1* 1 0 0 1
0 1 0 0 0 0
0 1 0 0 1 0
1 0 0 1* 0 1
G 0 1 0
0 1 0-
C
DV1
I
C
EV C
#V
SV 2V #V #V After Step 2.1 A I 2 3 4 5 6
B
C
1*
0
1
0 1 1 0 0 1
0 0 0 1* 0 5,/
0
D
E
0
0
F
G
0
1
0
C
1
0
1
D4
0
C!
T
0
00
0
0
0
1*
0
0
0
1
0
1
EV
1
0
0
1
0
C
2V #V
#V/
After Step 3.1
293
294
Chapter5
Matching
A
1 2() 3
4 5
B
C
0
1
0 l
0
1 1
it 6L 0
D
E
F
G
0 1* 1*0
0 1 0
1 0 0
0 1 0
0 0 1* 0
1
1* 0 1 0 1
o
0
0
1
1
0
After Step 3.2 If the algorithm is now applied with this new independent set of Is, it tells us that we have a maximum independent set of 1s, and we end up with the following configuration. A
B
C
D
E
F
G
1
I*
0
1
0
0
1
0
2 3 4 5
0 1 1 0
0 0 0 1*
1 1 1*0 0 0 O
I 0 0 1
0 0 1* 0
1 0 0 1
6L
o o
1 o0
5>,/
2./
E as
E at/
1 0 #/
#
It should be noted that, in steps 2.1 and 2.2, the order in which the labeled but not scanned lines are chosen may affect what larger independent set of is the algorithm produces. In computing answers for the examples in this section, we always chose the rows from top to bottom and the columns from left to right.
EXERCISES 5.3 Throughout these exercises, when applying the independent set algorithm, choose rows from top to bottom and columns from left to right. In Exercises 1 and 2 one stage in the application of the independent set algorithm is shown. Apply step 2.1 or 2.2, as appropriate. 1.
A
B
C
D
1 1* 0
0 0
1
D1
2
1
D
3
1
1
0
0
2 3
4
I
0
0
0
4L0
#A/
#A/
1
1
0
2.
A
B
C
D
E
1* 0
1
0
1
CV/
0 1
0 0
1
1
Es
0
0
1
0
1* 0
2
#A/
1* 1
295
5.3 A Matching Algorithm In Exercises 3 and 4 the matrix is ready for step 3.1. What entries should be circled? 3.
A 1 -O
B
C
D
l* 0
1 0
0
2
1*
3
1 1 0 0 2V 1
4
1 0 0 1* IJ 4V #/
4. CV
1
DV1 A!
2 [*
DVs
4
3
A
B
C
0
0 0
1*
1
*
D
E
0
1 0
E as Cv
20 1
0
1
0
D3/
0
1
0
0
C!
2
3A>/
1^>/ #^>/ #,/
In Exercises 5-10 a matrix is given with an independent set of is. Use the independent set algorithm until it ends in step 3.3.
5.
0-
1*
1* 0 0 1 1 8.
O 0 0
0 1* 1
0 I 0
0
1* 1* I 0 1 1
I 0 0 0
1 I
9. -
I
0
1 1
0 0 0
0 1* 1 0 0 1
0 0
1* 1 0 0 01 1 1 1
I 0 00 0
I 0
I 0
0
0
-O 1* 0 0 0 1 O 1
I
1*
0
1
1I* 0
0
I 0 1
0 1* 1
1 0
9. 0 0 0 0
0 1 1* 0
10.
0 1 1* 1 1 0 1 0
00 0 0
1
1
1
In Exercises 11-16 a bipartitegraph is given with a matching. Convert it to a matrix andfind a maximum matching by using the independent set algorithm, starting with the correspondingindependent set of Is. A
11.
12. 1
B C
3 4
13. 1
A
14. 1
2
B
2
3
C
3
4
4
296
15.
ChapterS Matching 3
A
2
E X
D
lbO.
B 4
In Exercises 17-22 a sequence of sets is given with distinct elements in some of the sets starred. Convert it to a matrix and use the independent set algorithm tofind a system of distinct representatives, ifpossible. Start with the corresponding independent set of Is. 17. {B*1, {A*, B, C, D}, {A, B), {B, D*) 18. {C*, DI, {A*, B), {A, D*}, (A, C, D) 19. tW*}, (Y., Z1, {W. Y), {W. X., Y, Z}
20. {1*, 3, 5), 1,4*), (2*, 3,5,{1, 2,41 21. {carrot*, egg}, {apple*, bananna, date, fennel), (apple.
carrot,
egg*}, [apple, carrot, egg)
22. {5*, 13}, {1*, 6, 9), {1, 5}, {1, 6*, 13) 23. Five ships, the Arabella, Constantine, Drury, Egmont, and Fungo, arrive at five loading docks. For technical reasons, each dock can only accept certain ships. Dock I can only accept the Constantine or Drury. Likewise, Dock 2 can only accept the Egmont or Fungo; Dock 3 the Constantine, Egmont, or Fungo; Dock 4 the Arabella, Drury, or Fungo; and Dock 5 the Arabella, Constantine, or Egmont. The harbormaster sends the Constantine to Dock 1, the Egmont to Dock 2, the Fungo to Dock .3, and the Arabella to Dock 4. Use the independent set algorithm to improve on this, if possible. 24. A radio station wants to play an hour of rock music, followed by an hour each of classical, polka, and rap. Six disk jockeys are available, but each has his or her scru jes. Only Barb, Cal, Deb, and Felicia are willing to play rock. Likewise, only Andy, Barb, Erika, and Felicia will play classical; only Barb, Deb, and Felicia will play polkas; and only Andy, Barb, and Deb will play rap. Ns disk jockey is allowed to work more than one hour per day. At present, the station manager plans to use Barb, Andy, and Deb for the first 3 hours, but has no one left for the rap hour. Use the independent set algorithm to find a better matching.
5.4
+
APPLICATIONS OF THE ALGORITHM In this section we will prove that our independent set algorithm actually does what it claims to do. At the same tirre, we will find that the algorithm actually leads to more insight about the relations among independent sets, coverings, and systems of distinct representatives. W.- start with a sequence of short lemmas concerning the results of applying the algorithm.
Lemma 1
If the algorithm gets to step .3.1, then it produces an independent set of Is with more elements than the original one.
5.4 Applications of the Algorithm
297
Proof What goes on in the backtracking process was indicated in the previous section. Schematically, a pattern such as shown in Figure 5.10 emerges. According to the way the algorithm works, the circled symbols form an alternating sequence of unstarred and starred Is, beginning and ending with an unstarred 1. Reversing the stars on these clearly increases the number of starred Is by one. The new set is still independent since if a 1 given a star was in a line with any starred 1, the latter l's star is removed in Step 3.2. X (nol*s) ..........
.:
.
..
.
(no 1*s)
.
Q.
I
1*
FIGURE 5.10
Now we prove some lemmas about what our matrix looks like if step 3.3 is reached and the algorithm indicates that we have a maximum set of starred Is. Recall that in this case each line that has been labeled has also been scanned. Lemma 2
If step 3.3 is reached, then the labeled rows and unlabeled columns form a covering. Proof. If not, then some I is at the same time in an unlabeled row and labeled column. If this 1 is starred, then its column can only have been labeled when its row was scanned, contradicting the fact that the row is unlabeled. But if the 1 is unstarred, then when its column was scanned its row would have been labeled, another contradiction. K
Lemma 3
If step 3.3 is reached, then each labeled row and unlabeled column contains a starred 1. Proof Each unlabeled column contains a starred 1 since columns that do not are labeled at step 1. On the other hand, if a labeled row contained no starred 1, we would go to step 3.1 instead of step 3.3. M
Lemma 4
If step 3.3 is reached, then no starred 1 is in both a labeled row and unlabeled column.
298
Chapter5
Matching
Proof
If a starred 1 is in a labeled row, then its column is labeled when the row
is scanned.
Theorem 5.4
d
The independent set algorithm increases the number of elements when applied to an independent set that is not a maximum independent set. When applied to an independent set that is a maximum independent set, it tells us so. Proof
The flow of the algorithm is shown in Figure 5.11.
FIGURE 5.11
In steps 2.1 and 2.2, columns and rows are scanned. Since a matrix has only a finite number of lines, the algorithm eventually gets to step 3.2 or step 3.3. If it gets to step 3.2, then Lemma I tells us that it constructs an independent set with more elements than the one with which we started. It remains to show that if rhe algorithm gets to step 3.3, then the independent set we started with is actually a maximum independent set. According to Lemma 2, the labeled rows and unlabeled columns form a covering. But Lemmas 3 and 4 say that the lines in this covering are in one-to-one correspondence with our independent set. Thus the cov ring and the independent set contain the same number of elements. Then, by Theorem 5.3, the covering is a minimum covering and the independent set is a maximum independent set, which is what we want to prove. 9
The argument just given amounts to a proof of a famous theorem of graph theory, first stated in 1931 by D. Koriig, who pioneered the area. We state it in both its matrix and bipartite graph fonns.
Theorem 5.5
Aiy0nh': A I hvWv#_~ . In a matrix of Os and Is, a maximum independent set of Is contains the same number of elements as a minimum covering. Equivalently, in a bipartite graph, a maximum matching contains the same number of elements as a minimum covering.
When the independent set a]gorithm reaches step 3.3, it gives us a construction of a minimum covering, namely, the labeled rows and unlabeled columns.
5.4 Applications of the Algorithm
+
299
Example 5.11 We will use the algorithm to find a minimum covering for the graph shown in Figure 5.12.
FIGURE 5.12
We convert the graph to the matrix that follows, and by inspection find the independent set shown. A
B
C
D
IFO 0 1* 211*o 100 2 1* 3 0 0 1 4 1* 1
01 O 1 0 0]
Applying the algorithm yields the following. A
B
C
1 F0 0 2 1* 1 3 0 0
0 1
4 L0
D
1* 01
1* 1
1 0
DV
0 L
2V/ We see that the matching we found by inspection was a maximum matching, since we have reached step 3.3. A minimum covering consists of the labeled rows and unlabeled columns, namely row 2 and columns B and C. Thus vertices 2, B, and C form a minimum covering for the original graph. + Note that Konig's theorem only applies to bipartite graphs, even though matchings and coverings have been defined for arbitrary graphs. The reader should check that a maximum matching for the nonbipartite graph shown in Figure 5.13 contains 2 edges, while a minimum covering contains 3 vertices.
FIGURE 5.13
300
ChapterS
Matching
A Purof of Hall's '
wlter
We can also use our conclusions about the algorithm to complete the proof of Hall's theorem, which was. stated in Section 5.1. Recall that it remains to show that if SI, S2 ,. . ., S, is a sequence of sets not having a system of distinct representatives, then there exists a subset I of {1, 2, .. , n} such that the union of the sets S for i E I has fewer than III elements. Let the union of all the sets Si for i = 1, 2, . . ., n be {t, t2 , . . . , tml where the t's are distinct. We construct a matrix of Os and Is with rows corresponding to the sets Si and columns corresponding to the elements tj. Explicitly, the entry in row i and column j is to be 1 if tj c S and 0 otherwise. (We have already constructed such matrices; foi example, the sequence PI, P2 , . . ., P6 of sets of professors who teach certain courses mentioned in Section 5.1 leads to the matrix of the second example in Section 5.3.) We use our algorithm as many times as necessary to confirm that we have a maximum independent set of Is in the matrix. Let rL, ru, CL, and cu denote the number of labeled rows, unlabeled rows, labeled columns, and unlabeled columns in this matrix after the last application of the algorithm, respectively. Certainly rL + ru = n (the number of sets) and CL + CU = m (the number of elements). By Lemmas 2, 3, and 4, our maximum independent set has rL + CU elements. If our maximum independent set had n elements, it would correspond to a system of distinct representatives, so we may assume that rL +-CU <
n=
rL
+ ru.
Thus cu < ru. We claim that the union of the ru sets corresponding to the unlabeled rows contains fewer than ru elements. The reason is that each 1 in an unlabeled row must be in an unlabeled column by Lemma 2. Thus the union of the corresponding sets contains at most cu elements, and we know that cu < ru. Hence we can take I to be the numbers of the unlabeled rows. This completes the proof of Hall's theorem. Notice that we have an actual construction of the set I using the algorithm. For example, the problem of th- courses and professors led to the matrix below. A 1
B
1
o0
2
C
D
E
1
0
1
1*
3 4
1 1
)
1* 0 0 0V
5
0
l*
0
)
I o
6L- °
5,1
0
F
G
0
1
0
1
0
1
0
0 0 1* 0
0 1
0
1
o 1 0
2,/ #,/
A
EV E,1
5.4 Applications of the Algorithm
301
The unlabeled rows are the four rows 1, 3, 4, and 6. As we saw in Section 5. 1, the union of the corresponding sets contains fewer than four elements. Note that the unlabeled columns correspond to the three professors able to teach any of these four courses. Example 5.12 At a business meeting, 6 speakers are to be scheduled at 9, 10, 11, 1, 2, and 3 o'clock. Mr. Brown can only talk before noon. Ms. Krull can only speak at 9 or 2. Ms. Zeno cannot speak at 9, 11, or 2. Mr. Toomey cannot speak until 2. Mrs. Abernathy cannot speak between 10 and 3. Mr. Ng cannot speak from 10 until 2. The scheduler cannot seem to fit everyone in. Show that it is impossible to do so, and give a way the scheduler can convince the speakers of this fact. We construct the following matrix, where the rows correspond to speakers and columns to times. 9 10 It 1 2 3 I 0
B K
1*
Z
0 0
0
l
0
T A N
1
I
1 0 0 0 0 0
1*
0
0 0
0 1*
1
0
0
1
0
0
0
I
01 01 1
11*
The independent set indicated was found by inspection. Applying the algorithm produces first 1 2 3 11 9 10 B K z
T A N
@ 1 1 0 0 0
0 1* 0 0
1
0
BI
Zl
0 0 0 0 0 0
0 0 1
0 0 0
0 1°* 0 1
0 1
0 0 1 1* 1 1
111
9A/ 11 9! 9
K
and then the matrix below. 9 B K z T A N
1
10
1
1 0
0 0
0
1 1*
1* 0 0
Zi/
B,
1* 0 0 0 0 0
#1 0 0 1 0 0 0
2
3
0 0 1* 0 0 1 1* 1 1 0 1
101/
1I
302
Chapter 5 Matching
Notice that the rows corresponding to Krull, Toomey, Abernathy, and Ng are unlabeled. These four speakers all want to speak at 9, 2, or 3 (the unlabeled columns), which shows that all their restrictions cannot be accommodated. +
The 130lttlengeck Prhbt: ln
A foreman has 4 jobs that need to be done and 5 workers to whom he could assign them. The time in hours each worker would need to do each job is shown in the following table. Job]
Job2
Job3
Job4
Worker 1
3
7
5
8
Worker 2
6
3
2
3
Worker 3
3
5
8
6
Worker 4
5
8
6
4
Worker 5
6
5
7
3
He needs all 4 jobs finished as soon as possible and so is interested in making the maximum job time for the 4 workers chosen as small as possible. Only one worker can do a job in 2 hours, so that it is obviously impossible to get all 4 jobs done that fast. Three hours is more reasonable. Let us make a matrix of Os and is, putting a 1 in each position corresponding to a job time of 3 hours or less. I 0
0 0 0 1 1 1
1 0 0 0 0 0 0 0
0 0 0
1
We would like an independent set of Is having 4 elements to correspond to the 4 jobs. Unfortunately, no such set exists. Since all the is lie in 3 lines (row 2 and columns 1 and 4), this fact is implied by K6nig's theorem. Doing the jobs will take at least 4 hours, and so we add is to our matrix corresponding to the 4s in the original matrix. 1 0 0 0 0 1 1 1
1 0 0 0 0 0 0 0 O 0
1 1
5.4 Applications of the Algorithm
303
The same reasoning shows that still no independent set of four is exists, so we add is corresponding to the 5s in the original matrix.
[ I 1*0
10
01* 1 1 1 0 0 1
1 1 0 0 0
1*
0
1
The starred independent set was found by inspection. By applying the algorithm, we find the larger set that follows. I 0
0 1* 0 1* 1 1
1*
I
0
1
0
0
0
1
0
1
0
1
Thus the shortest time in which all the jobs can be completed is 5 hours. Problems such as this are called bottleneck problems, since we are interested in making the job time of the slowest worker as small as possible. In other circumstances, we might be interested instead in minimizing the total time to do all the jobs. Such problems will be treated in the next section.
EXERCISES 5.4 In Exercises 1-4 a matrix of Os and Is is given with an independentset indicated. Use the independentset algorithm to find a minimum covering. 1.
O 1* 0 1 1* 1 1 0 0 1 0 1* L 0
3. -1* 0 1 1 1
0
0
1
1
0
0 1* 1* 0 0 1 0 0
2.
1* 0 0 1 1
0
01
1* 0 1 00 01 0 1* 1
]-
4. -0
1* 0
0 0-
1 0 1 1 1* 0 1 0-
0
0
1*
0
0
0
1
1
0
0
0 _1*
0 1
1 0
0 1
0 1
304
Chapter5 Matching
In Exercises 5-S a bipartitegraph is given with a matching indicated. Use the independent set algorithmto find a minimum covering. 5. 1
A
2
*B
3 4
6. 1
/ A
2B
C
3CC D
4

OD H1E
8.
A
7. 1
*A
2
B
B
3
C
C
4
D
D
5
E
E
In Exercises 9-12 a sequence of sets SI, S2 , Sn is giver . Use the independent set algorithm tofind, if possible, a subset I of { 1, 2, . . n } such that the union of the sets S, for i E I hasfewer elements than I. 9. {2,4,5},{1,3,5},{2,3,5},{3,4,5},{2,3,41
10. {1,2,4},{2,3,4,5},{2,4,6},{1,6),{1,4,61,{1,2, 61 11. {2, 7), {1, 3, 6), (5,7}, (3,4, 6}, {2, 5), (2,5,7)
6}, {0, 1,3,5, 6), {1, 4,7), {2, 6}, I1,4, 7}, {2, 6, 7} 12. {1, 2}, (4, 13. A military commander must send a runner to each of four posts notifying them of a plan to attack. Because of differing terrain and skills, the time in hours for each runner to reach each post varies. Runner A takes 6 hours to get to Post 1, 5 hours to Post 2, 9 hours to Post 3, and 7 hours to Post 4. Runner B takes 4, 8, 7, and 8 hours to reach the four posts. Likewise, Runner C takes 5, 3, 9, and 8 hours; and runner D takes 7, 6, 3, and 5 hours. The attack cannot begin until all posts have gotten the message. What is the shortest time until it can begin? 14. One step of a manufacturing process takes 5 operations that can be done simultaneously. These take different times in minutes on the 5 machines available, as given in the following table. M]
M2
M3
M4
MS
Operation1
6
7
3
6
2
Operation2
6
3
4
3
3
Operation3
2
5
3
7
4
Operation4
3
i
2
6
3
Operation5
4
7
2
7
6
How fast can the entire step be accomplished?
5.5 The HungarianMethod
305
15. The graph below shows a city map. Adjacent vertices are one block apart. It is desired that a police officer be stationed at some of the vertices so that no one is more than one block from a police officer. Use the algorithm to find the smallest number of police officers necessary to accomplish this, and tell where they should be positioned. I
A
2
B
3
Ed
5F
D
4
C
G
7
F
i6
H
16. Show that if step 3.3 is reached in the independent set algorithm, then the number of labeled columns equals the number of labeled rows plus the number of columns containing no starred l. 17. Show that if the independent set algorithm is applied to the matrix derived from a sequence of sets SI, S2 . 5, as in the proof of Hall's theorem and step 3.3 is reached, then the union of the sets corresponding to the unlabeled rows has exactly cu elements, where cu is the number of unlabeled columns. 18. Consider a bipartite graph where every edge is from a vertex in VI to one in the disjoint set V2. If S C VI, let S* be the set of vertices of V2 adjacent to a vertex in S. Show that the graph has a matching with IV,I vertices if and only if 5*1> iTI whenever S C VI.
5.5
+
THE HUNGARIAN METHOD In the last section, we considered a problem of assigning 4 jobs to 5 workers in such a way that all 4 jobs got done as soon as possible. Although this might be our goal in special circumstances, a more common aim is to minimize the total time necessary to do the 4 jobs. If each worker was paid the same hourly rate, for example, this would minimize the labor cost for the project. For simplicity, we will start with an example in which there are the same number of jobs and workers. The times in hours for each worker to do each job are given in the following table. Worker I
Worker 2
Worker 3
Worker 4
Job]
3
6
3
5
Job2
7
3
5
8
Job3
5
2
8
6
Job4
8
3
6
4
An assignment of a worker to each job amounts to an independent set of four entries from the corresponding matrix, and we want the sum of the entries
306
Chapter5 Matching
in that set to be as small as possible. For example, two possible assignments are indicated below.
3* 6
5
3
7 5
8 6
7 3* 5 5* 2 8
4*
8
3* 5 2 S,*
-8 3
6
6 3
3
5* 8 6
6* 4
The first of these produces the sum 3 + 3 + 8 + 4 = 18, and the second gives 5 + 3 + 5 + 6 = 19; so the first independent set is better than the second for our purposes. Of course, other assignments might yield even smaller sums. Suppose we subtract 3 fromn each entry in the first row of our matrix. The two assignments are shown below for the new matrix. 0*
3
7 5 8
3* ' 8 2 i* 6 3 4*
[0
2
3
0
2*
7 3* 5 8 5* 2 8 6 58 3 6* 4
Now the first set has the sum 0 + 3 + 8 + 4 = 15 and the second has the sum 2 + 3 + 5 + 6 = 16. The firsi assignment still has a sum 1 less than the second. The point is that although subtracting the same number from each entry of the first row changes the problem. it does not change which positions give the answer. Since any independent set of four entries will have exactly one of them in the first row, the sum of the entries in any such set will be decreased by 3 by our operation. Any assignment producing a minimum sum for the new matrix will also give a minimum sum for the original matrix. Furthermore, the same analysis applies to the other rows as well. In order to have entries as small as possible without introducing negative numbers, we will subtract from all entries in each row the smallest number in that row. This means subtracting 3 from the entries of the second row, 2 from those of the third row, and 3 from the entries of the fourth row.
0 4 3 5
3 0 0 0
0 2 6 3
2 5 4 1
The same argument applies to columns, and so now we will subtract 1 from each entry of the fourth colurmn.
0 3 0 1 4 0 3 0 5 0
2 4 6 3 3 0
5.5 The HungarianMethod
307
Finding a four-entry independent set in this matrix will solve our original problem. Furthermore, now at least we might be able to recognize a solution. Suppose we could find an independent set of four Os. This will clearly have minimum sum, since the matrix has no negative entries. Unfortunately, a maximum independent set of Os has only three entries, as we can confirm with the independent set algorithm (modified to find an independent set of Os instead of is). 1 2 3 4
A
B
C
D
0* 4 3 5
3 0* 0 0
0 2 6 3
2 4 3 0*
C1
#1
1V
We have reached step 3.3 of that algorithm, and so the independent set of three Os indicated is a maximum independent set of Os. Now we will show how to change the matrix so as to have a better chance of finding an independent set of four Os. Later we will show why the solution to the minimum sum problem has not been changed. Since a maximum independent set of Os has fewer than four entries, there is a minimum covering consisting of fewer than four lines. In fact, by what we discovered in the last section, such a covering consists of the labeled rows and unlabeled columns of the above matrix. These lines are indicated below. A
B
C
D
1 [0*3
0
1]
2
4
CV
0* 2 4
4 - 5
0
11
3
0*
#V
Look at the entries not in any line of this covering. (By the definition of a covering, they are all positive.) The smallest of these is 2. Now we change our matrix as follows: (1) Subtract 2 from each entry not in a line of the covering. (2) Add 2 to each entry in both a row and column of the covering. (3) Leave unchanged the entries in exactly one line of the covering. The resulting matrix is shown below. 0 0 2004
1043 3 01
3
308
Chapter5 Matching
Now we can find an independent set of four Os, and pick out the corresponding set in the original matrix, as 3hown.
~0* 5 2
1 -3
hi) 1)' 0* 4 0 L 0
3 4
~
3* 7
6 3
3
5
2* 8
6
0*_
8
3
4*
3 5* 6
5 8
The minimum sum for an independent set of four entries in the original matrix is 3 + 5 + 2 + 4 = 14. Of course, several questions need to be answered. One is whether the operation involving a minimum covering we just described is legitimate, that is, does not change the solution to the minimum sum problem. Another is whether this operation even does any good for the purpose of producing an independent set of four Os, since although we subtract from some entries, we add to others. These questions will be answered aftter we state our method in a formal way.
Hungarian Algorithm Starting with an n x n matrix w iih integer entries, this algorithm finds an independent set of n entries with minimum sum. Step I
Step 2 Step 3
Step 4
(reduce the matrix) (a) Subtract from each entry of each row the smallest entry in that row. (b) Subtract from each entry of each column the smallest entry in that column. (determine a maxirnu 11 independent set of Os) Find in the matrix a maximum independent set, S, of Os. (enlarge the independent set if ISI < n) while ISI < n (a) Find a minimum covering for the Os of the matrix. (b) Let k be the smallest matrix entry not in any line of the covering. (c) Subtract k from each entry not in a line of the covering. (d) Add k to each entry in both a row and column of the covering. (e) Replace S with a new maximum independent set of Os. endwhile (output) The set S is an independent set of n entries with minimum sum.
5.5 The HungarianMethod
309
First we will show why the loop in step 3 in the algorithm does not change which independent set is a solution. The reason is that this loop may be broken down into adding and subtracting numbers from rows and columns of the matrix, which we have already seen do not change which independent set is a solution. In particular, let k be the smallest (positive) entry not in any line of a covering. Let us subtract k from every entry of every row of the whole matrix, and then add k to every entry of every line of the covering, line by line. The net effect is exactly that of the loop of step 3. The number k is subtracted from each entry not in a line of the covering. If an entry is in a line of the covering exactly once, then it is not changed, since k is both subtracted from and added to it. Entries in both a row and column of the covering have k subtracted once but added twice, a net result of +k. Now we address the question of whether step 3 does any good. It is conceivable that the algorithm could cycle through its parts forever without ever producing an independent set of n Os. We will show that this cannot happen. After step 1, our matrix will contain only nonnegative integers as entries. We will show that the sum of all entries in the matrix will decrease whenever a loop of step 3 is performed. Obviously, if this sum were 0, then all matrix entries would be 0 and an independent set of n Os would exist. Thus if the algorithm went on forever, the sums of all matrix entries would give an infinite decreasing sequence of positive integers, which is impossible. Step 3 continues only while no independent set of n Os exists. Then a minimum covering will contain c rows and columns, where c < n. (This is a consequence of Konig's theorem.) Let us compute the effect of a loop of step 3 on the sum of all the entries of the matrix. As we have just seen, this amounts to subtracting k from each entry of the entire matrix and then adding k to each entry of each line of the covering. Since there are n2 entries in the matrix, the subtraction decreases the sum of all entries by kn 2. Likewise, since there are c lines in the covering, each containing n entries, the addition increases the sum of all the entries by kcn. The net amount added to the sum of all the entries is -kn 2 + kcn = kn(-n + c). But this quantity is negative because c < n, and so the net effect is to decrease the sum of all entries, as claimed. The reason this method is called 'Hungarian' is to honor Konig, who was from Hungary, and upon whose theorem it is based. The algorithm is due to H. W. Kuhn. Matrsice ;/mthat gg,/E
AreS No,
Sqnuv tElt
Let us suppose that in our example a fifth worker becomes available, so that now our table becomes as follows.
310
Chapter5
Matching Worker I
Vorker 2
Worker 3
Worker 4
Worker 5
Jobi
3
6
3
5
3
Job2
7
3
5
8
5
Job3
5
2
8
6
2
Job4
8
3
6
4
4
It is still reasonable to ask how to assign the 4 jobs in such a way as to make the sum of their times minimal, but our matrix is no longer square, and the algorithm only applies to square matric.-s. Of course, one worker is not going to get a job, and this simple idea provides a key to how to adapt the method. We introduce a fifth job, one requiring no lime at all to do. This amounts to adding a row of Os to the matrix, producing the square matrix on the left below.
3 6 3 5 3]
0 3 0 2 0
7 5
4 0 3 0
3 5 2 8
8 5 6 2
5 4
2 6
2 0
5 0 3 1 1
8 3 6 4 4
The second matrix above shows the result of applying step 1. Applying the independent set algorithm to this matrix yields the matrix on the left below. The matrix on the right shows the result of applying step 3 (with k = 1) to it. A
B C
D E
0*3001 CV 31
4 - 2 4 l*2
2 2
3 3 06 4 5 3 5 L 0o*TI
'i
2 04
L
1
002 3 4 3 0
0 2 1 4
1 2
2 0 5 3 0 4 0 2 0 1 J D,1
1001
5V/#,/
l'/
An independent set of five Os is shown below for this matrix, along with the corresponding set for the original matrix. 0* 3 2 4 0
4 0* 0 0 1
0 1 5 2 0*
2 4 3 Cl* C
1 2 0* 1 I
3* 7 5 8 O0
6 3* 2 3 0
3 5 8 6 O*
5 8 6 4* 0
3 5 2* 4 0
By using the fifth worker, we can do all jobs in 3 + 3 + 4 + 2 = 12 hours instead of the previous minimum of 14 hours.
311
5.5 The HungarianMethod
A sweater factory has 4 workers and 4 machines on which sweaters can be made. The number of sweaters a worker can make in a day depends on the machine he or she uses, as indicated in the following table. Machine I
Machine 2
Machine 3
Machine 4
Worker 1
3
6
7
4
Worker 2
4
5
5
6
Worker 3
6
3
4
4
Worker 4
5
4
3
5
In this case, we are looking for an independent set with 4 entries, the sum of which is a maximum instead of a minimum. We reduce this to a problem we already know how to solve by multiplying the corresponding matrix by - . The result is shown at the left below. -3 -4 -6 -- 5
-6 -5 -3 -4
-7 -5 -4 -3
-4 -6 -4 -5
4 2 0 -
1 0 1 1 3 2 1 2
3 0 2 0
Finding a maximum sum in the original matrix is equivalent to finding a minimum sum in this matrix. The negative entries cause no problems, since they disappear when we subtract the least entries of each row (here -7, -6, -6, and -5). The result is shown at the right above. Thus a maximum sum problem may be solved by applying the Hungarian method to the negative of the original matrix. The reader should check that a maximum of 23 sweaters can be produced per day.
EXERCISES 5.5 In Exercises 1-8 find the smallest sum of an independent set of entries of the matrix with as many elements as the matrix has rows. 1. [1
2
2.
3]
1 4 3 8] 2 7 9 3 8 2
5
3.
3 3 6 6 2
5 1 5 4 3 1 4 5 4 5
22 4 4 4j
5.
5
3 5 5 3 84 6 4 2 6 4 6 1 3 6 3 4 -5 7
4 3
6 5 5 9
2 5
8]
6 7 1 6 6 3 4 5 5 4 3 4
6 6 4 7 4. -2 4 3 3 L4
6
6.
0
3 1 -1 2
1 0- -I 0 3 -2 4
4 7 2 7
4 4 3 5
1] 5 7 3 9-
312 7.
ChapterS Matching
~3 4 5
3
1 3
L6
5 4
7 5
62
45 5 4
3 3]
8.
~5 6
2 4
3 2
4
5
4 5 2 6 1 4
4 0
6
3~ 3
6 6
7 6
In Exercises 9-12 find the largest sum of an independent Yet of entries with as many elements as the matrix has rows.
9.
~5 4 2 3 3
1
4
3
10. 5
4 3
4
3
7
1
11.
6 5 3 1 41 2
5
3
7
1 1 1 3
7 5 2 10
8 3 7 5 4
5
2
7
3
6
3
4
2
7-
I
5
12.
8
3
6 7 3 8 9 4
75
6
2
2 5 8 6 9
8
13. A newspaper sports editor must send 4 of his reporters tc 4 cities. From past experience, he knows what expenses to expect from each reporter in each city. He can expe ct Addams to spend $700 in Los Angeles, $500 in New York, $200 in Las Vegas, and $400 in Chicago. Hart zan be expected to spend $500, $500, $100, and $600 in these cities; Young to spend $500, $300, $400, and $700; and Herriman to spend $400, $500, $600, and $500. How should the editor make the assignments to keep the total expenses to a minimum? 14. A supervisor has 5 salespeople who can be assigned to 5 different routes next month. Adam can be expected to see $9000 worth of goods on Route 1, $8000 on Route 2, $10,000 on Route 3, $7000 on Route 4, and $8000 on Route 5. Betty would sell $6000, $9000, $5000, $70CO, and $4000 on these routes; Charles would sell $4000, $5000, $4000, $8000, and $2000; Denise would sell $4000, $7000, $5000, $4000, and $2000; and Ed would sell $5000, $5000, $7000, $9000, and $3000. What is tbe maximum total expected sales possible next month? 15. A foreman has 4 jobs and 5 workers he could assign them to. The time in hours each worker needs for each job is shown in the following table. Worker I
Worker 2
Worker 3
Worker 4
Worker 5
Job 1
7
Job 2
6
3
5
7
2
1
4
2
Job 3
6
8
3
8
9
1
Job4
7
2
1
5
6
After subtracting the minimum entries from the rows and columns of the corresponding matrix, we have the matrix O0* 1
3
4
0
0
O0 3
0
5
2 I
2 I
7 3
0* 5
7 0*
in which the stars indicate a maximum independent set of Os. The corresponding job assignment will require a total of 7 + 1 + 1 + 1 = 10 hours. But by assigning the jobs to workers 2, 4, 5, and 3, the total time could be reduced to 3 + 2 + 1 + 1 = 7 hours. What is wrong?
Supplementary Exercises
313
HISTORICAL NOTES
Phillp Hall
Philip Hall (1904-1982), who contributed Theorem 5.1, was a very gifted English mathematician. After receiving his doctorate in algebra, Hall worked on generalizations of the Sylow theorems in group theory and with correlation in statistics. In 1935 he published the paper that listed the necessary and sufficient conditions for the existence of a system of distinct representatives for a sequence of finite sets. During World War II, he worked with the famous British cryptography group at Bletchley Park. The methods for linking systems of distinct representatives to matching are a combination of the work of Ddnes Konig (1884-1944), a Hungarian mathematician, and the two American mathematicians Lester R. Ford Jr. (1927- ) and Delbert R. Fulkerson (19241976), who considered the question of whether or not there is a subset of edges in a bipartite graph with the property that every vertex meets just one of them. Konig initially proved this in 1914 and published his result in 1916. The approach of Ford and Fulkerson used the independent set algorithm found in Section 5.3. When extended to apply to bipartite graphs, the result is equivalent to an algorithmic approach developed by Konig in 1931.
SUPPLEMENTARY EXERCISES 1. How many systems of distinct representatives does each of the following sequences of sets have? (a) {1,2,3,4,5),(1,2,3,4,51,{(,2,3,4,5}
(b) 11,2,3,41, (I,2,3,4),(5,6,71 (c) (1, 2,31, (2, 3,4), (1, 2,41, (1,3,4}, {1, 2,4}
2. Let SI = I1, 2, 5}, S2 =(1, 5}, S3
= (1, 2}, S 4 = (2, 3, 41, andS3 = (2, 5}. Give an argument to show that the sequence SI, S2, S3, S4 , S5 does not have a system of distinct representatives. 3. Tell whether each of the following graphs is bipartite or not, and if so give disjoint sets VI and V2 such that each edge joins a vertex in VI to one in V2 . 1 4 i
7 10
1
12 (a)
4. Give a maximum matching for each graph in Exercise 3. 5. Give a minimum covering for each graph in Exercise 3.
13
2
5t
1
154 10
3
8 11
16 (b)
4
6
9 12
314
ChapterS Matching
6. The graph below is bipartite in that every edge joins a vertex in VI = {1, 3, 6, 8} to one in V2 = {2, 4, 5, 71. Give the matrix of the graph. 8
4
3
--
5
2
--
X
7
6L
7. Find a maximum independent set of Is for the matrix of Exercise 6. 8. Find a minimum covering for the matrix of Exercise 6 9. Use the independent set algorithm to find a maximum independent set of Is for the following matrix, starting with the starred set of is. Use the algorithm to find a minimum covering also. 1*
0
0
0
1
0
0
1*
I
0
0
()
I
0
0
1
1*
0
1
1
0
0
1
0
0
10. Convert the following bipartite graph into a matrix, and use the independent set algorithm to find a maximum matching and minimum covering for the gragh. Start with the set of Is corresponding to the given matching. A B C D E
11. Convert the sequence of sets Jw*, y), {x*, zl, {vx, z}, {w, x}, {v, y*) to a matrix, and use the independent set algorithm to find a system of distinct representatives i- possible, starting with the set of Is corresponding to the starred elements. 12. Dan, Ed, Fred, Gil, and Hal are at a dance with Iv17, June, Kim, Lii, and Mae. The only compatible dancing partners are Dan with Kim or Lil, Ed with Ivy or Mae, Fred with June or Mae, Gil with June or Lil, and Hal with Ivy or Kim. Those currently dancing are Dan with Kim, Ed with Ivy, Fred with June, and Gil with Lil, with Hal and Mae left out. Use the independent set algorithm to find everyone a partner.
Computer Projects
315
13. The table below shows the number of hours it would take each of workers A, B, C, D, and E to do jobs 1, 2, 3, 4, and 5. What is the minimum time needed to do all five jobs? AB 1
2 3 4 5
CD
7 4
5 3
8 5
5
8
6 4
7 3
E
6 4
4 6
6
7
3
3 6
4 5
5 3
14. Use the Hungarian algorithm to find an independent set of entries with minimum sum for each of the following matrices. (a) l 3 5 7 (b) 3 4 7 81
2 5
8 4
6 4 1 2
6 4 3 5
7 5
5 6 5 3 4 4
8 6 5 7 4
2
8 9
15. A car dealer has four salespeople, and each is assigned to sell a particular brand of car. Adam can sell 6 Hupmobiles, 8 Studebakers, 7 Packards, or 4 Hudsons per month. Beth can sell 7, 3, 2, or 5 of each brand; Cal 6, 7, 8, or 7; and Danielle 6, 4, 5, or 4. How should each be assigned a different brand to maximize the number of cars sold? 16. Prove that if a graph with v vertices has a matching M, then 21MI < v. 17. Suppose M is a matching of a graph such that there is a simple path el, e2 . en of odd length that begins and ends at vertices not incident with any edge in M. Show that if el, e3 . en are not in M, while e2, e4 . e., are in M, then M is not a maximum matching. 18. Let a graph have a maximum matching with m edges and a minimum covering with c vertices. Show by mathematical induction on m that the greatest integer not exceeding c'l is less than or equal to m.
COMPUTER PROJECTS Write a computer programhaving the specified input and output. 1. Given a set S of m elements and n subsets T1, T2, . . ., T, of S. generate all possible lists XI, X2, Xn, where Xi E T, for i = 1, 2, . ., n. For each list, check whether the elements xi are all distinct. Apply the program to the example of professors and courses in Section 5.1 to confirm that the sets PI, P2 , .. , P6 there have no system of distinct representatives. 2. Given a graph with vertex set V = {1, 2, .. , n) and adjacency matrix A = {aij), decide if a given subset W of V is a covering or not. (See Exercise 33 of Section 5.2.) 3. Apply the independent set algorithm to an m x n matrix of Os, Is, and 2s, with no two 2s in a line. The Os and Is should be interpreted as in that algorithm, while the 2s correspond to starred is. Thus the program will either interchange some Is and 2s to get a new matrix with a larger independent set of 2s or else determine that this is impossible. In the latter case, output the row and column numbers corresponding to a minimum covering. 4. Find a maximum independent set of Is in a given matrix of Os and ls by repeatedly invoking the program of the previous exercise.
316
Chapter5 Matching
5. Solve the bottleneck problem, starting with an m x n matrix A = [aij] of positive integers. (Hint: For k = 1, 2, .. form a new matrix B = [bij], where byj 0 or I according as aij > k or not. Apply the program of the previous exercise until k is sufficiently large so that B has an independent set with n elements.) 6. Given an m x m matrix, perform step I of the Hung-rian algorithm to get a matrix with nonnegative entries and at least one 0 in each line. 7. Implement the Hungarian algorithm, given an m x in matrix. Use the program of Exercise 4. Note that an auxiliary matrix in which Os and Is correspond to positive and 0 entries will be needed to apply that program.
SUGGESTED READINGS 1. Berg, C. Graphsand Hypergraphs,2nd ed. New York: Elsevier Science, 1976. 2. Edmonds, J. 'Paths, Trees, and Flowers.' Canad. J. Moath. 17, 1965, 449-467. 3. Ford, L. R. and D. R. Fulkerson. Flows in NetworA-s. Princeton, NJ: Princeton University Press, 1962. 4. Hall, P. 'On Representations of Subsets.' J. London AMath. Soc. 10, 1935, 26-30. 5. Kuhn, H. W. 'The Hungarian Method for the Assignment Problem.' Naval Res. Logist. Quart. 2, 1955, 83-97. 6. Lawler, E. L. CombinatorialOptimization: Networks and Matroids. New York: Holt, Rinehart and Winston, 1976. 7. Mirsky, L. Transversal Theory. New York: Academic Press, 1971. 8. Roberts, Fred S. Applied Combinatorics. Englewood Cliffs, NJ: Prentice Hall, 1984.
Network Flows 6.1 Flows and Cuts 6.2 A Flow Augmentation Algorithm 6.3 The Max-Flow Min-Cut Theorem 6.4 Flows and Matchings Many practical problems require the movement of some commodity from one location to another. For example, an oil company must move crude oil from the oil fields to its refinery, and a long-distance telephone company must move messages from one cityto another. In both of these situations, there is a limitation to the amount of the commodity that can be moved at one time. The volume of crude oil that the oil company can move, for instance, is limited by the capacity of the pipeline through which the oil must flow. And the number of telephone calls that the phone company can handle is limited by the capacity of its cable and its switching equipment. This type of problem, in which some commodity must be moved from one location to another subject to the restriction that certain capacities not be exceeded, is called a network flow problem. In this chapter we will be primarily concerned with solving such problems.
6.1
+
FLOWS AND CUTS
When an oil company must ship crude oil from the oil fields to its refinery, there is one origin for the oil (the oil fields) and one destination (the refinery). However, there may be many different pipelines available through which the oil can be sent. Figure 6.1 shows this situation for an oil company with oil fields at Prudhoe Bay and a refinery in Seward, Alaska. (Here the pipeline capacities are given in thousands of barrels per day.) This figure showing the possible routes from the oil fields to the refinery is a special type of weighted directed graph. By a transportation network, or more simply a network, we mean a weighted directed graph satisfying the following three conditions. (1)
There is exactly one vertex having no incoming edges, i.e., exactly one vertex with indegree 0. This vertex is called the source.
317
318
Chapter6 Network Flows Drvr5ol
FIGURE 6.1
(2) (3)
There is exactly one verte x having no outgoing edges, i.e., exactly one vertex with outdegree 0. This v.r tex is called the sink. The weight assigned to each edge is a nonnegative number.
In this context a directed edge of the network will be called an arc, and the weight of an arc will be called its capacity.
Example 6.1 Figure 6.2 shows a weighted directed graph with five vertices and seven arcs. The seven arcs are: (A, B) with capacity 6, (A, C) with capacity 8, (A, D) with capacity 3, (B, C) with capacity 5, (B, D) with capacity 6, (C, E) with capacity 4, and (D, E) with capacity 9. Clearly the capacity of each arc is a nonnegative number. Note that vertex A is the only vertex having no incoming arcs, and vertex E is the only vertex having no outgoing arcs. Thus the directed graph in
6.1 Flows andCuts
319
Figure 6.2 is a transportation network with vertex A as its source and vertex E as its sink. +
OE
D FIGURE 6.2
In a transportation network, we consider a commodity flowing along arcs from the source to the sink. The amount carried by each arc must not exceed the capacity of the arc, and none of the commodity can be lost along the way. Thus, at each vertex other than the source and the sink, the amount of the commodity that arrives must equal the amount of the commodity that leaves. We will formalize these ideas in the following definition. Let A be the set of arcs in a transportation network AK, and for each arc e in A let c(e) denote the capacity of e. A flow in K is a function f that assigns to each arc e a number f(e), called the flow along arc e, such that (1) (2)
0 < f(e) < c(e), and for each vertex V other than the source and sink, the total flow into V (the sum of the flows along all arcs ending at V) equals the total flow out of V (the sum of the flows along all arcs beginning at V).
Since the capacity of an arc is nonnegative, it is clear that the function f assigning the number 0 to each arc is always a flow in a transportation network. Consequently, every network has a flow. +
Example 6.2 For the transportation network in Figure 6.2, the function f such that f(A, B)
=
6, f(A,C) = O, f(A,D) = 3, f(B,C) = 4, f(B,D) = 2, f(C,E) = 4, and f(D, E) = 5 is a flow. This flow is shown in Figure 6.3, where the first number on each arc is its capacity and the second number is the flow along that arc. Notice that each value of f is a nonnegative number that does not exceed the capacity of the corresponding arc. In addition, at vertices B, C, and D the total flow into the vertex equals the total flow out of the vertex. For instance, the total flow into vertex B is 6 along arc (A, B); and the total flow out of vertex B is also 6:4 along arc (B, C) and 2 along arc (B, D). Likewise, the total flow into vertex D is 5: 3 along arc (A, D) and 2 along arc (B,D); and the total flow outof vertex Disalso 5 along arc (DE). +
320
Chapter6 Network Flows
In Figure 6.3 the total flow out of vertex A is 9: 6 along arc (A, B), 0 along arc (A, C), and 3 along arc (tA, D). Notice that this number is the same as the total flow into vertex E, which is 4 along arc (C, E) and 5 along arc (D, E). This equality is a basic property or every flow. C 8,0
4,4 5,4
AK-
*E
D FIGURE 6.3
Theorem 6.1
For any flow in a transportation network, the total flow out of the source equals the total flow into the sink.
Proof.
Let VI, V2 , . ., denote the vertices of the network, with VI being the source and V, being the sink. Let f be a flow in this network, and for each k (1 < k < n) define Ik to be the total flow into Vk and Ok to be the total flow out of Vk. Finally, let S denote the sum of the flows along every arc in the network. For each arc e = (Vj, l'k). f (e) is included in the sum II + 12 + + I, exactly once (in the term Ok)and in the sum 01 + 02 + .* + O, exactly once (in the term 0j). Hence II + 12 +- -** +In = S and 01 + 02 + ***+On = S; SO O 1 + 02 + * ** +On = 11 - 12 + * ** + In. But for any vertex Vk other than the source and sink, Ik = 0 k Cancelling these common terms in the preceding equation gives 01 + O, = I, +- I,. Now I, = 0 because the source has no incoming arcs, and O, = 0 because the sink has no outgoing arcs. Hence we see that 01 = I, that is, the total flow out of the source equals the total flow into the sink. N .
.
If f is a flow in a transportation network, the common value of the total flow out of the source and the total flow into the sink is called the value of the flow f. In the network shown in Figure 6.1 in which crude oil is to be shipped through pipelines, the oil company would be interested in knowing how much oil can be sent per day from the oil fields to the refinery. Likewise, in any transportation network, it is important to know the amount of a commodity that can be shipped from the source to the sink without exceeding the capacities of the arcs. In other words, we would like to know the largest possible value of a flow in a transportation network. A flow having maximum value in a network is called a maximal flow. In Section 6.2 we will present an algorithm for finding a maximal flow in a transportation network. In order to understand this algorithm better, we will first consider some of the ideas that are involved in finding a maximal flow.
6.1 Flows and Cuts
321
Suppose, for example, that we want to find a maximal flow in the transportation network shown in Figure 6.2. Because this network is so small, it will not be difficult to determine a maximal flow by a little experimentation. Our approach will be to find a sequence of flows with increasing values. We will begin by taking the flow to be zero along every arc. Thus the current flow is as in Figure 6.4, where the numbers along each arc are the arc capacity and the current flow along the arc. C
A
D FIGURE 6.4
Now we will try to find a path from the source to the sink along which we can increase the present flow. Such a path is called a flow-augmenting path. In this case, since there is no arc along which the flow equals the capacity, any directed path from the source to the sink will suffice. Suppose that we choose the path A, C, E. By how much can we increase the flow along the arcs in this path? Because the capacities of the arcs (A, C) and (C, E) in this path are 8 and 4, respectively, it is clear that we can increase the flows along these two arcs by 4 without exceeding their capacities. Recall that we are only changing the flow along arcs in our chosen path A, C, E and that the flow out of vertex C must equal the flow into C. Consequently, if we tried to increase the flow along arc (A, C) by more than 4, then the flow along arc (C, E) would also need to be increased by more than 4. But a flow along arc (C, E) that is greater than 4 would exceed the capacity of this arc. Hence the largest amount by which we can increase the flow along the path A, C, E is 4. When we increase the flow in this manner, we obtain the flow shown in Figure 6.5.
Al
D FIGURE 6.5
Now we will try to find another flow-augmenting path so that we can increase the present flow. Note that such a path cannot use arc (C, E) because the flow in
322
Chapter 6 Network Flows
this arc is already at its capacity. One acceptable path is A, D, E. For this path we can increase the flow by as much as 3 without exceeding the capacity of any arc. (Why?) If we increase the flows in arcs (A, D) and (D, E) by 3, we obtain the new flow shown in Figure 5.6. Again we will try to find a flow-augmenting path. Path A, B, D, E is such a path. For this path we can increase the flow by as much as 6 without exceeding the capacity of any arc. If we increase the flows in arcs (A, B), (B, D), and (D, E) by 6, we obtain the new flow shown in Figure 6.7. C 8,4
C 4,
5,0 A
-- 6,0
.
t(66,0/
3,3
//
9.3
D FIGURE 6.6
D FIGURE 6.7
Is it possible to find another flow-augmenting path? Note that any path leading to the sink must use either arc (C, E) or arc (D, E) because these are the only arcs leading to the sink. But the flow along these arcs is already at the capacity of the arcs. Consequently. it is not possible to increase the flow in Figure 6.7 any further, and so this flow is a maximal flow. The value of this flow is 13, the common value of the flow out of the source and into the sink. The argument used here to justify that there could be no flow having a value larger than 13 is an important one. As this argument suggests, the value of a maximal flow is limited by the capacities of certain sets of arcs. Recall once more the oil pipeline network in Figure 6.1. Suppose that after analyzing this network you have determined that the value of a maximal flow is 18 but that your colleagues at the oil company are questioning your calculations. They point out that it is possible to ship 22 thousand barrels per day out of Prudhoe Bay and 22 thousand barrels per day into Seward; so they believe that there should be a flow having the value 22. How can y ou convince them that there can be no flow having a value greater than 18? Suppose that the vertices of the network are partitioned into two sets S and T such that the source belong!, to S and the sink belongs to T. (Recall that this statement means that each vertex belongs to exactly one of the sets S or T.) Since every path from the source to the sink begins at a vertex in S and ends at a vertex in T, each such path must contain an arc that joins some vertex in S to some vertex in T. So if we can partition thc vertices of the network into sets S and T in such a way that the total capacity of the arcs going from a vertex in S to a vertex in T is 18, we will have proved that there can be no flow with a value greater than 18.
6.1 Flows and Cuts
323
It can be seen in Figure 6.8 that such a partition is obtained by taking T = {Fairbanks, Delta Junction, Valdez, Seward)
and S to be the other cities in the figure. The heavy line in Figure 6.8 separates the cities in S (northwest of the line) from the cities in T (southeast of the line). Notice that the only arcs joining a city in S to a city in T are those from Anchorage to Seward (with capacity 9), Livengood to Fairbanks (with capacity 3), and Prudhoe Bay to Delta Junction (with capacity 6). These arcs have a total capacity of 9 + 3 + 6 = 18, and so no flow from a vertex in S to a vertex in T can exceed this number.
FIGURE 6.8
Generalizing from this example, we define a cut in a network to be a partition of its vertices into two sets S and T such that the source lies in S and the sink belongs to T. The sum of the capacities of all arcs leading from a vertex in S to a vertex in T is called the capacity of the cut. Note that in determining the capacity
324
Chapter6 Network Flows
of the cut, we consider only the capacity of arcs leading from a vertex in S to a vertex in T, and not those leading from a vertex in T to a vertex in S. +
Example 6.3 In Figure 6.8, let S = {Prudhoe Bay, Barrow, Wainwright, Point Hope, Kotzebue) and T contain the cities not in S. Then S, T is a cut because Prudhoe Bay is in S and Seward is in T. The arcs leading from a city in S to a city in T are Kotzebue to Unalakleet (with capacity 5), Kotzebue to Galena (with capacity 4), Prudhoe Bay to Wiseman (with capacity 12), and Prudhoe Bay to Delta Junction (with capacity 6). So the capacity of this cut is 5 + 4 + 12 + 6 = 27. +
C
C
A
OE
(a)
(b) FIGURE 6.9
Example 6.4 In Figure 6.9(a), S = {A. B, C) and T = {D, E} form a cut. The arcs leading from a vertex in S to a vertex in T are (A, D) with capacity 3, (B, D) with capacity 6, and (C, E) with capacity 4. Therefore the capacity of this cut is 3 + 6 + 4 = 13. The sets S' = {A, B, C, E I and T' = {E} also form a cut. See Figure 6.9(b). In this case the arcs leading from a vertex in S' to a vertex in T' are (C, E) with capacity 4 and (D, E) with capacity 9. Thus this cut also has capacity 13. + In Figure 6.10, let S, T be the cut with S = {A, C, D} and T = {B, E}. Let us consider the total flow (not the capacities) along the arcs joining vertices in S and T. Notice first that the toial flow from S to T (that is, the total flow along arcs leading from a vertex in S to a vertex in T) is 6 + 4 + 7 = 17, the sum of the flows along the blue arcs (.4, B), (C, E), and (D, E), respectively. Likewise, the total flow from T to S is 1 + 5 = 6, the sum of the flows along the black arcs (B, C) and (B, D). The difference between the total flow from S to T and
6.1 Flows and Cuts
325
the total flow from T to S is therefore 17 -6 = 11, which is the value of the flow shown in Figure 6.10. This equality is true in general, as the next theorem shows.
A
D FIGURE 6.10
Theorem 6.2
If f is a flow in a transportation network and S, T is a cut, then the value of f equals the total flow along arcs leading from a vertex in S to a vertex in T minus the total flow along arcs leading from a vertex in T to a vertex in S. Proof. If U and V are sets of vertices in the network, we will denote by f (U, V) the total flow along arcs leading from a vertex in U to a vertex in V. Let a be the value of f. With this notation, the result to be proved can be written as a = f (S, T) - f(T, S). Note that if V, n V2 = 0, then f (U, V, U V 2 ) = f (U, V,) + f (U, V2); and likewise, if U1 n U2 = 0, then f (Ul U U2, V) = f (UI, V) + f (U2, V). By the definition of a flow, f ({V}, S U fT)-f (S U T, {V}) = 0 if V is neither the source nor the sink, and f ({V}, S U T)-f (S U T, {V)) = a if V is the source. Summing these equations for all V in S gives the equation f(S,S UT) - f(S U TS) = a. Thus
a
f(S, S U T) =
-f(S
[f(S,S) + f(S, T)]
U T,S) -
[f(S,S) + f(T,S)]
=f(S,' ) - f(T,S).
Corollary
If f is a flow in a transportation network and S. T is a cut, then the value of f cannot exceed the capacity of S, T. Proof. Using the notation in the proof of Theorem 6.2, we have a = f(S, T) - f(T,S) < f(S, T) since f (T, S) > 0. But the flow along any arc leading from a vertex in S to a vertex in T cannot exceed the capacity of that arc. Thus f (S,T) cannot exceed the capacity of the cut S, T. It follows that the value of f cannot exceed the capacity of the cut S, T. T
326
Chapter 6
Network Flows
The corollary to Theorem 6.2 is a useful result. It implies that the value of a maximal flow in a transportation network cannot exceed the capacity of any cut in the network. By using this fact, we can easily obtain an upper bound on the value of a maximal flow. [n Section 6.3 we will be able to strengthen this result by showing that every transportation network contains at least one cut with capacity equal to the value of a maximal flow. (Notice, for instance, that Example 6.4 presents two cuts with capacity equal to the value of the maximal flow in the network shown in Figure 6.7.) This fact will enable us to prove that a particular flow is a maximal flow as we did in analyzing the flow in Figure 6.7.
EXERCISES 6.1 In Exercises 1-6 tell whether the given weighted directed graph is a transportationnetwork or not. If so, identify the source and sink. If not, tell why. 1.
:1.
A /4
I
D
3.
B
2
B
3
4
A
4.
D
1
I
6
D
3 OF
11 2
A
2
F
23
IC
3
E
5.
6.
C
4
E
B
3
D
21 IF
6
A
C
4
E
F
6.1 Flows and Cuts
327
In Exercises 7-12 a transportationnetwork is given. The first number along each arc gives the capacity of the arc. Tell whether the second set of numbers along the arcs is a flow for the network. If so, give the value of the flow. If not, tell why.
7.
8.
C
C 17,3
D
9.
7,3
B
10.
D
13,1 3,2
7,6
11.
C
6,4
E
B
3,1
D
F
%8,5
F
,1
A
12.
4,25,
C
2,2
E
B
5,1
D
17,36, 4,
A
D
2,1
I,
A<
53
B
13,3
5,1 12,2
4, C
A t4,2
F
6.2
8 ,4
6,3
,
6,4 C
E
15.2
F 3,2
E
In Exercises 13-18 tell whether the given sets S, T form a cutfor the network indicated. If so, give the capacity of the cut. If not, tell why. 13. S = {A,BIandT = {D,EIforthenetworkinExercise7 14. S = {A, D) and T = {B, C, El for the network in Exercise 8 15. S = {A, D, E} and T = {B, C, F} for the network in Exercise 9 16. S = {A, B, C, D} and T = {D, E, FI for the network in Exercise 10 17. S
I{A, D, E} and l = {B, C, FI for the network in Exercise 11
18. S
I{A, B, C} and T = {D, E, FI for the network in Exercise 12
In Exercises 19-24 find by inspection a flow satisfying the given conditions.
19. 20. 21. 22. 23.
A flow of value A flow of value A flow of value A flow of value A flow of value
11 13 11 17 18
for the for the for the for the for the
network in network in network in network in network in
Exercise Exercise Exercise Exercise Exercise
7 8 9 10 11
328
Chapter 6 Network Flows
24. A flow of value 18 for the network in Exercise 12 In Exercises 25-30find by inspection a cut satisfying the given conditions.
25. 26. 27. 28. 29. 30.
A cut A cut A cut A cut A cut A cut
of capacity of capacity of capacity of capacity of capacity of capacity
11 for the 13 for the 11 for the 17 for the 18 for the 18 for the
network network network network network network
in in in in in in
Exercise Exercise Exercise Exercise Exercise Exercise
7 8 9 10 I1 12
31. A telephone call can be routed from Chicago to Atlanta along various lines. The line from Chicago to Indianapolis can carry 40 calls at the same time. Other lines and their capacities are: Chicago to St. Louis (30 calls), Chicago to Memphis (20 calls), Indianapolis to Memphis (15 calls), Indianapolis to Lexington (25 calls), St. Louis to Little Rock (20 calls), Little Rock to Memphis ( 15 calls), Little Rock to Atlanta (10 calls), Memphis to Atlanta (25 calls), and Lexington to Atlanta (I 5 calls). Draw a transportation network displaying this information.
32. A power generator at a dam is capable of sending 300 megawatts to substation 1, 200 megawatts to substation 2, and 250 megawatts to substation 3. In addition, substat ion 2 is capable of sending 100 megawatts to substation 1 and 70 megawatts to substation 3. Substation 1 can send at most 280 megawatts to the distribution center, and substation 3 can send at most 300 megawatts to the distribution center. Draw a transportation network displaying this information. In Exercises 33-36 let f(U, V) be defined as in the proof of Theorem 6.2. 33. Find f(U, V) and f(V, U) if f is the flow in Exercise 1(0, U = {B, C, D}, and V = {A, E, Fl. 34. Find f (I,V) and f(V, U) if
f is the flow in Exercise 12, U = {C, E, F), and V = {A, B, D}.
35. Give an example to show that if V1 and V2 are not disj cint, then it is possible that f (U, VI U V2) may not equal f (W, V) + f(W, V 2 ). 36. Prove that f(U, V U W) = f(U, V) + f((U, WA - f(U, V n W) and W.
6.2
o
for any sets of vertices U, V,
A FLOW AUGMENTATION ALGORITHM In this section we will present an algorithm for finding the maximal flow in a transportation network. This algorithm is based on a procedure formulated by Ford and Fulkerson and utilizes a modification suggested by Edmonds and Karp. (See suggested readings [5] and [3] at the end of this chapter.) The essence of the algorithm is described in Section 6.1: (1) Begin with any flow, for example, the one having zero flow along every arc. (2) Find a flow-augmenting path (a path from the source to the sink along which the present flow can be increased), and increase the flow along this path by
as much as possible.
6.2 A Flow Augmentation Algorithm
(3)
329
Repeat step (2) until it is no longer possible to find a flow-augmenting path.
Some care must be taken in deciding if there is a flow-augmenting path. Consider, for example, the transportation network in Figure 6.11, where the numbers along each arc are the capacity of the arc and the present flow along the arc, in that order. This flow was obtained by sending 4 units of flow along path A, B, C, E; 3 units along path A, D, E; and then 2 units along path A, B, D, E. C 5,4 A
SE

6,2
3,3
9,5 D
FIGURE 6.11
The value of the flow in Figure 6.11 is 9, and we know from the argument following Figure 6.7 that the value of a maximal flow in this network is 13. Consequently, we will look for a path from A to E along which the present flow can be increased. Clearly the only way to increase the flow out of A is to use arc (A, C). But (C, E) is the only arc leading out of the vertex C, and the present flow along this arc equals its capacity. Thus we cannot increase the flow along arc (C, E). Therefore, there is no directedpath from A to E along which the flow can be increased. But if we allowed flow from vertex C to vertex B along the arc (B, C), we could send 4 units of flow along path A, C, B, D, E. This additional 4 units would give us a maximal flow from A to E. How can we justify sending 4 units from C to B when the arc is directed from B to C? Since there are already 4 units of flow along arc (B, C), sending 4 units of flow from C to B has the effect of cancelling the previous flow along (B, C). Thus, by sending 4 units of flow along path A, C, B, D, E, we obtain the maximal flow shown in Figure 6.7. If we look at the network in Figure 6.11 more carefully, we can see that our first path A, B, C, E was not well chosen. By using the arc (C, E) as part of this path, we prevent the later use of arc (A, C). (Note that because there is no arc except (C, E) leaving vertex C, any flow sent into vertex C along arc (A, C) must leave along arc (C, E).) Therefore, using arc (B, C) in the path A, C, B, D, E corrects the original poor choice of path A, B, C, E. Clearly our algorithm will need some method to correct a poor choice of path from source to sink made earlier. In the version of the algorithm that follows, this correction occurs in step 2.2(b). This algorithm, like the independent set algorithm in Section 5.3, is based on the labeling procedure devised by Ford and Fulkerson. In the algorithm two operations, called labeling and scanning, are performed on vertices. As in the independent set algorithm, a vertex must be labeled before it can be scanned.
330
Chapter 6
Network Flows
Flow Augmentation Algorithm For a transportation network in %hich arc (X, Y) has capacity c(X, Y), this algorithm either indicates that the current flow f is a maximal flow or else replaces f with a flow having a larger value. Step ]
(label the source) Label the source with the triple (source, +, cc). Step 2 (scan and label)
repeat
Step 3
Step 2.1 (select a vertex to scan) Among all the vert ces that have been labeled but not scanned, let V denote the one that was labeled first, and suppose that the label on V is (U, i, a). Step 2.2 (scan verie' V) For each unlabeled vertex W, perform exactly one of the following three actions. (a) If (V, TV) is 0, assign to W the label (V, -, b), where b is the smaller of a and f (W, V). (c) If neither (a) nor (b) holds, do not label W. Step 2.3 (mark as scanned) Mark vertex V as having been scanned. until either the sink is Icbeled or every labeled vertex has been scanned (increase the flow if possible) if the sink is unlabeled The present flow is a mtaximal flow. otherwise
Step 3.1 (breakthrough) Now let V denote the sink, and suppose that the label on V is (U, +, a). Step 3.2 (adjust the flow)
repeat (a)
if the labeI on V is (U, +, b) Replace f (U, V) with f (U, V) + a.
endif (b)
if the label on V is (U, -, b) Replace f (V, U) with f (V, U) - a.
endif (c) Now let V denote the vertex U. until V is the source
endif
6.2 A Flow Augmentation Algorithm
331
If the present flow is not a maximal flow, the algorithm uses breadth-first search to find a shortest flow-augmenting path (that is, one with the fewest arcs). Each vertex V along this path is labeled in one of two ways: (U, +, a) or (U, -, a). The first entry of the label, U, signifies that vertex U precedes V on this path. The second entry of the label denotes that (U, V) is an arc or that (V, U) is an arc on this path, depending on whether the entry is + or -, respectively. And the third entry of the label, a, is a positive number indicating how much the present flow can be increased (if the second entry of the label is +) or decreased (if the second entry of the label is -) without violating the restrictions in condition (1) of the definition of a flow for any arc along the path from the source to V. We will illustrate the use of the flow augmentation algorithm by finding a maximal flow for the network discussed in Section 6.1. When we reach step 2.2 of the algorithm, we will examine the unlabeled vertices in alphabetical order. In order to begin the algorithm, we will take the flow to be 0 along every arc, as shown in Figure 6.12. (Again the two numbers written beside each arc are the capacity and the present flow along that arc.) In step 1 we assign the label (source, +, oc) to the source, vertex A. In step 2, we note that only vertex A has been labeled but not scanned. In step 2.2 we scan vertex A by examining the unlabeled vertices (B, C, D, and E) to see if any of them can be assigned labels. Note that vertex B is unlabeled, (A, B) is an arc, and the flow along this arc (0) is less than the capacity (6). Thus we can perform action (a) in step 2.2 on vertex B. Since 6 is the smaller of oo (the third entry in the label on A) and 6- 0, we label B with (A, +, 6). Likewise, we can perform action (a) on the vertices C and D, which assigns them the labels (A, +, 8) and (A, +, 3), respectively. Because vertex E is not joined to vertex A, it cannot yet be given a label. This completes the scanning of vertex A. The current labels are shown in Figure 6.13. (A, + 8) C
D FIGURE 6.12
FIGURE 6.13
After scanning vertex A, we return to step 2.1. There are 3 vertices that have been labeled but not scanned (namely, vertices B, C, and D). Of these, vertex B was the first one to be labeled, and so we scan vertex B. Because there are no unlabeled vertices joined by an arc to vertex B, no changes result from the scanning of vertex B. Therefore we return to step 2.1 once more. At this stage there are two unlabeled vertices that have not been scanned (namely, vertices C
332
Chapter 6 Network Flows
and D), and, of these, C was ihe first to be labeled. Thus we scan vertex C in step 2.2. Since E is unlabeled. (C, E) is an arc, and the flow along this arc (0) is less than its capacity (4), we perform action (a). This action assigns to vertex E the label (C, +, 4) because 4 is the smaller of 8 (the third entry in the label on C) and 4 - 0 (the capacity ninus the flow along arc (C, E)). Since there are no unlabeled vertices remaining, this completes the scanning of vertex C. The present labels are shown in Figure 6.14. In the scanning of vertex C, the sink was labeled, and so we proceed to step 3. The fact that the sink has been labeled (C, +, 4) tells us that the current flow can be increased by 4 along a path through vertex C. The vertex that precedes C in this path is the first entry in the Label on C, which is (A, +, 8). Thus the path from the source to the sink along which the flow can be increased by 4 is A, C, E. When we increase the flow along the arcs in this path by 4, the resulting flow is as in Figure 6.15.
(A. + 3) C
,4)
A
*E
6,0 3,0
(A, - 3) FIGURE 6.14
6,0 /9,0 D
FIGURE 6.15
This finishes step 3, and so the first iteration of the algorithm has been completed. We now remove all the labels and repeat the algorithm with the flow in Figure 6.15. As before, we assi yn to vertex A the label (source , +, oc), and then in step 2.2 we scan vertex A. This results in vertices B, C, and D receiving the respective labels (A, +, 6), (A, +, 4), and (A, +, 3). This completes the scanning of vertex A. Since B is the unscanned vertex that was labeled first, we now scan vertex B. As in the first iteration of the algorithm, no changes result from the scanning of vertex B. So we return to step 2.1. This time vertex C is the unscanned vertex that was labeled first. But unlike the first iteration, we cannot label vertex E because the flow along arc (C, E) is not less than the capacity of the arc. Consequently, no changes result from scanning vertex C. Once more we return to step 2.1. This time vertex D is the only labeled vertex that has not been scanned, and so we scan vertex D. Since (D, E) is an arc along which the flow is less than the capacity, we perform action (a). As a result of this action, vertex E is labeled (D, +, 3). This completes the scanning of vertex D. But now the sink has been labeled; so we proceed to step 3. (See Figure 6.16.)
6.2 A Flow Augmentation Algorithm
333
Because the label on the sink is (D, +, 3), we can increase the current flow by 3 along a path through vertex D. To find the vertex that precedes D in this path, we examine the label on D, which is (A, +, 3). Since the first entry of this label is A, we see that the path along which the flow can be increased by 3 is A, D, E. When we increase the flow along the arcs in this path by 3, we obtain the flow shown in Figure 6.17. This completes the second iteration of the algorithm. (A, + .4) C
C 8,4 ,3)
(source,
A
4,4
60B
*E 9,3
3,3
MY
D (A. + ,3)
D
FIGURE 6.16
FIGURE 6.17
Again we discard all of the labels and perform another iteration of the algorithm. In this third iteration, we reach step 3 with the labels shown in Figure 6.18. From these labels, we see that the flow can be increased by 6 along the path A, B, D, E. By increasing the flows along the arcs in this path by 6, we obtain the flow in Figure 6.19. (A, + ,4) C C
(source,
.6)
D (B, +,6) FIGURE 6.18
-
D FIGURE 6.19
Again we discard all of the labels and perform another iteration of the algorithm. This time, however, we can label only vertex C when scanning vertex A. (See Figure 6.20.) Moreover, when vertex C is scanned, no changes occur. Consequently all of the labeled vertices have been scanned. Thus step 3 assures us that the present flow (the one shown in Figure 6.19) is a maximal flow. When the algorithm ends, the set of labeled vertices S = {A, C} and the set of unlabeled vertices T = {B, D, E} form a cut. Notice that the capacity of this cut is 4 + 6 + 3 = 13, which equals the value of the maximal flow in Figure 6.19. We will see in Section 6.3 that this is no coincidence: When the flow augmentation
334
Chapter 6 Network Flows
algorithmends with the sink 14nlabeled, the sets of labeledand unlabeledvertices always determine a cut with capacity equal to the value of a maximal flow. (A, +,4) C 4,4
8,4 66B
A adE5' (source,
6,6
i-
3,3
4
9,9 D
FIGURE 6.20
#
Example 6.5 We will use the flow augmentation algorithm to find a maximal flow for the network shown in Figure 6.21. When labeling vertices in step 2.2, we will consider them in alphabetical order.
A
FIGURE 6.21
}. The labels assigned in iteration 1 are shown in Figure 6.22. Thus we increase the flow by 3 along the path A, B, F, G.
i f','ii
(A,+ ,5) B
A (7 (source, + ,-)'
(B. +,3) F
350
907
0
D)
4,0
(A, + ,4) FIGURE 6.22
fA
E (C, + ,5)
+
6.2 A Flow Augmentation Algorithm
335
1!,rf'ion 2I,l The labels assigned in iteration 2 are shown in Figure 6.23. Thus we increase the flow by 5 along the path A, C, E, G. (A, +,2)
(C, +,4)
D (A, +,4)
4,0
E
(C, +,5)
FIGURE 6.23
emi: 38., f The labels assigned in iteration 3 are shown in Figure 6.24. Thus we increase the flow by 2 along the path A, C, F, G. I
(A, +,2)
(C, +,2)
A
'
(source,
,2)
+ .G
D
4,0
E
(D), + ,4)
(A, + ,4) FIGURE 6.24
4. The labels assigned in iteration 4 are shown in Figure 6.25. Thus we increase the flow by 1 along the path A, D, E, G. (A, + ,2) B
A (source,
7 oo)+ (+
(C, +,I) F
3,3
J
D 4,0 (A, + ,4) FIGURE 6.25

E
(D), + ,4)
336
Chapter 6 Network Flows 7$ealon .4'. The labels assigned in iteration 5 are shown in Figure 6.26. Thus we increase the flow by 1 along the path A, B, C, F, G.
(4 +,2)
(C't,1)
FIGURE 6.26
71labelsA assigned in iteration 6 are shown in Figure 6.27. Thus we increase the flow by 1 alo~n, the path A, D, E, F, 6,6 G. 4,1 ,77 7tl~t~l f.The
(4 +,1) B 2
(E, +,1)
3,3 14
1
2
(source, +
F
D (A, + ,3)
56
5,5 4,1
E (D, +,3)
FIGURE B3,3 6.27
F
~ -Bi r.7io 6,7 The labels assigned in(I~ iteration 7 11are shown in Figure 6.28. Thus we increase the flow by 1 along the path A, D, E, C, F, G. (Note that we are using arc (C, E) in the wrong direction to cancel 1 unit of the flow sent along this arc in iteration 2.) A, +J
,I)
FIGURE 6.27
(source, + ,5)
,UG
(A, +F,)
6
(D, + )
6.2 A Flow Augmentation Algorithm
337
The labels assigned in iteration 8 are shown in Figure 6.29.
(A, + 1)
A (source, +±
(A, + ,1)
(D, +,I1)
FIGURE 6.29
Since the sink is not labeled, the flow shown in Figure 6.29 is a maximal flow. The value of this maximal flow is 14. Note that the set S = {A, B, C, D, E} of labeled vertices and the set T = {F, GI of unlabeled vertices form a cut with capacity 3 + 4 + 1 + 6 = 14. + Maximal flows need not be unique. For instance, the flow shown in Figure 6.30 is a maximal flow for the network in Example 6.5. This flow isdifferent from the one shown in Figure 6.29.
A .1
FIGURE 6.30
We conclude this section with a useful observation about the flow augmentation algorithm. Any flow in which the flow along each arc is an integer is called an integral flow. Suppose that all of the arc capacities in a network are integers and we begin the flow augmentation algorithm with an integral flow. In this case the third entry of each label, which is assigned during step 2.2 of the algorithm, is a minimum of integers. Consequently, if all the arc capacities are integers and we begin with zeroflow along every arc, the maximalflow that resultsfrom repeated use of the flow augmentation algorithm is an integralflow.
338
Chapter6 Network Flows
EXERCISES 6.2 Throughout these exercises, if there is a choice of vertices to label when using the flow augmentation algorithm, label the vertices in alphabeticalorder. In Exercises 1-4 a network, aflow, and a flow-augmenting path are given. Determine the amount by which the flow can be increased along the given path. 1. Path:A,B,D,E
:2. Path: A, B, C, E C
C 2.2, 1.8
7.6, 5.1
3
6.,425.2, 3.3
A
15, 0.9 3.1,1.5
.2, 2.4 D
4. Path:D,B,C,E,F
3. Path: A, B, E, D, F
A
F
C
3,1
E
C
6,5
E
In Exercises 5-8 a network and flow are given. By perfcrmning the flow augmentationalgorithm on this network andflow, we obtain the labels shown in each network. Determine a flow having a larger value than the given flow by performing steps 3.1 and 3.2 of the flow augmentation algorithm. (B, + ,2) C
5. 4,4 A
6,4
(source, +,
6. 4,4
9,6
B
AI 5,2
)
9,6
(D, + ,4)
7,2
+,2)
(source
8,4 D (A, + ,5)
7.
(A, +,3) B
(D, + ,4) B
(E, -,2) D
6,6
6,3 A
(A, + ,5)
3
F
(source, + a)
(D, + ,2) 7,
(source, + ,oo) D
2,1
_ 6,2 7,6
7,3
A (B.-,1)
66 7,7 '
5,3
,
,24 (Al + ,4)
(B, + ,2)
C( (B, - 3)
4,4
F (C, - 3)
F (E,+,3)
6.2 A Flow Augmentation Algorithm
339
In Exercises 9-16 a network andflow are given. Use theflow augmentation algorithm to show that the given flow is maximal or else to find a flow with a largervalue. If the given flow is not maximal, name the flow-augmenting path and the amount by which the flow can be increased. C
9.
10. 9.7
A
A
E
11.
B
7,5
12.
D
10,8 A
33 3,1
22
44
3,3
E
B
5,5
D
6,4
11,6 3,0
6,6 AFF
C
5,1
E
B
5,5
D
6,1
74
A
F
F
20,11
4,1<>
6, 41
D
8,
14.
9,9 A
A
I11,9 C
8,5
5,1
F
6,
13.
B
,9
6,6
6,6
F
AIF
2,2,
7,3
15.
9,5
C
1,1
E
B
2,2
E
9,6
16.
77
9,6 3,3
A
C
C
8,4
B
6,3
9,4
G
7,2 E
-
7A5
A
37
7,7
z,
3,3
3,A3
5,5 6,6
7
G 11,7
C
F F4
7,2
F.
In Exercises 17-20 a transportationnetwork and flow are given. Use the flow augmentation algorithm to find a maximalflow for each network. 17.
B
18.
6,6
B 4,0
.07,6,4. A
2,2
2,0
3,0
F
A
42 51
41
3,1
3,0
I C
4,4
E
F 31
C
2,1
E
340
Chapter6 Network Flows
19.
B
5,0
4,0
E
B
6,4
4,4 4,0
A
3,3
50
3,3 4,3
F
5,1
E
A
G
4,0 C
4,0
G
6,
C
5,0
-F
In Exercises 21-28 a transportationnetwork is given. Fin'd a maximalflow in each network by starting with the flow that is 0 along every arc and applying the flow augmentationalgorithm. 21.
22.
Li
C
3.2
4
4.1
A1. A
B
E
41 2.5
3.6 D
23.
B
75
D
90
24. 65
A
55
80
C
F
Al
E
25.
2(.
At
B
5
A
B
A
4 B
28.
3
F
4
C 27.
iD
A
E
F G
8
F
E
7
2
G
H
H
2C 2~ C
F
D
29. Give an example of a transportation network in which each arc has an integral capacity and there is a maximal flow such that the flow along some arcs is not an integer.
6.3
TheMax-FlowMin-CutTheorem
341
30. Consider a transportation network with source U and sink V in which each arc has capacity 1. Show that the value of a maximal flow equals the number of directed paths from U to V that have no arcs in common.
6.3
+
THE MAX-FLOW MIN-CUT THEOREM In this section we will show that the flow augmentation algorithm described in
Section 6.2 does what we claim; that is, it either confirms that the current flow is a maximal flow or else finds a flow having a larger value. We will also verify the observation that when the algorithm ends, the sets of labeled and unlabeled vertices determine a cut with capacity equal to the value of a maximal flow. Such cuts are of special interest because they are cuts having the smallest possible capacity, as we will see in Theorem 6.4. A cut in a transportation network is called a minimal cut if no other cut has a smaller capacity. The theorem below provides a method for detecting maximal flows and minimal cuts.
Theorem 6.3
In any transportation network, if f is a flow and S, T is a cut such that the value of f equals the capacity of S, T, then f is a maximal flow and S, T is a minimal cut. Proo.f Let f be a flow having value c, and let S, T be a cut having capacity c. Let f' be any other flow in this network, and let the value of f' be v. Applying the corollary to Theorem 6.2 to f' and S, T, we see that v < c. Hence there is no flow in this network having a value greater than c, the value of f. It follows that f is a maximal flow. Now let S', T' be a cut in this network with capacity k. Applying the corollary to Theorem 6.2 to f and S', T' gives c < k. Consequently, there is no cut in this network having a value less than c, the capacity of S, T. Therefore, S, T is a minimal cut. 6 In order to justify the validity of the flow augmentation algorithm, we need to show that: (1) When the algorithm ends with the sink unlabeled, the current flow is a maximal flow. (2) When the algorithm ends with the sink labeled, the original flow has been replaced by a flow with a larger value. The proof of statement 2 will be left as an exercise (Exercise 24). Theorem 6.4 verifies statement 1 by showing that if an iteration of the flow augmentation algorithm ends with the sink unlabeled, then the present flow is a maximal flow.
Theorem 6.4
If, during an iteration of the flow augmentation algorithm, the sink is not labeled, then the present flow is maximal. Moreover, the sets of labeled and unlabeled
342
Chapter 6
Network Flows
vertices form a minimal cut having capacity equal to the value of the present flow. Proof. Suppose that during some iteration of the flow augmentation algorithm the sink is not labeled. Let f denote the current flow, c(X, Y) the capacity of arc (X, Y), S the set of labeled vertices, and T the set of unlabeled vertices. Then the source is in S and the sink is in T; so S, T is a cut. Let (X, Y) be an arc leadi -igfrom a vertex X in S to a vertex Y in T. Since X is in S, X has been labeled during this iteration of the flow augmentation algorithm. If f(X, Y) < c(X, Y), then when X was scanned, we would have labeled Y in step 2.2(a) of the algorithm. B3ut Y is in T and hence is unlabeled; thus we must have f(X, Y) = c(X, Y). Now let (Y, X) be an arc leading from a vertex Y in T to a vertex X in S. Since X is in S, X has been labeled during this iteration of the algorithm. When X is scanned, we would have labeled Y in step 2.2(b) of the algorithm if f (Y, X) > 0. But Y is in T and so is unlabeled; thus we must have f (Y, X) = 0. By Theorem 6.2, the value of f equals the total flow p along all arcs leading from a vertex in S to a vertex: n T minus the total flow q along all arcs leading from a vertex in T to a vertex .n S. But the two preceding paragraphs show that p equals the capacity of the c al S, T and q = 0. Therefore the value of f equals p, the capacity of the cut S, 'T. It then follows from Theorem 6.3 that f is a maximal flow and S, T is a minimal cut. : Theorem 6.4 also proves our earlier assertion that when the flow augmentation algorithm ends with the sink unlabeled, the cut determined by the sets of labeled and unlabeled vertices is a minimal cut. Thus, for example, in Figure 6.20 we see that S = {A, C) and Tr = [B, D, E} form a minimal cut, and in Figure 6.29 we see that S = {A, B, C, D, E) and T = {F, G} form a minimal cut. We have already seen tha. a network may have more than one maximal flow. Likewise, a network may have more than one minimal cut. In Figure 6.20, for instance, {A, B, C} and {D, E I is a different minimal cut from the one mentioned in the previous paragraph.
+
Example 6.6 A natural gas utility delivers gas to Little Rock from a source in Amarillo through the network of pipelines shown in Figure 6.31. In this diagram the first number beside each pipeline is the capacity of the pipeline and the second is the present flow, both measured in hundreds of millions of cubic feet per day. The utility has proposed raising its rates to pay for additional pipelines. Although the Arkansas Regulatory Commission agrees that more than the present 14.7 hundred million cubic feet of gas are needed in Little Rock each day, it is not convinced that additional pipelines need to be built. It questions the need for more pipelines because most of the piipelines operated by the utility are not being used to
6.3 The Max-Flow Min-Cut Theorem
343
capacity, and some are not being used at all. How should the utility argue for new pipelines?
FIGURE 6.31
In order to justify its request for additional pipelines, the utility should apply the flow augmentation algorithm to the network and flow in Figure 6.31. By doing so, it will find that only the vertices A, B, C, G, H, and J are labeled. Consequently, the flow in Figure 6.31 is a maximal flow, and S={A,B,C,G,H,J}
and
T={D,E,F,I,K,LI
form a minimal cut. The utility should, therefore, prepare a map as in Figure 6.32 with A, B, C, G, H, and J in the northwestern region and D, E, F, I, K, and L in the southeastern region. This map shows that only three pipelines (shown in color) carry gas from the northwestern region to the southeastern region, and each of these is being used to capacity. On this basis, the utility can argue the need for more pipelines from the northwestern region to the southeastern region. +
Rock 9
FIGURE 6.32
It is conceivable that the flow augmentation algorithm may never produce a maximal flow because no iteration occurs in which the sink cannot be labeled. Our next result, however, shows that this situation cannot occur if all the capacities in the network are rational numbers.
344
Chapter 6 Network Flows
Theorem 6.5
If all the capacities in a transportation network are rational numbers and we start with zero flow along each arc, then repeated use of the flow augmentation algorithm produces a maximal flow in a finite number of iterations. Proof Suppose first that all the capacities in the network are integers. Let S, T be the cut in which S consists of the source alone and T contains all the other vertices. Since all the arc capacities are integers, the capacity of the cut S. T is an integer c. Apply the flow augmentati:in algorithm, beginning with zero flow along every arc. Now consider any iteration of the algorithm in which the sink is labeled. The label on the sink must ba of the form (U, +, a) or (U, -, a), where a > 0. Moreover, because all the capacities are integers, a is a minimum of integers and hence is an integer. Therefore, a > 1, and each iteration of the algorithm in which the sink is labeled increases the value of the flow by at least 1. But by the corollary to Theorem 6.2, no flow in this network can have a value exceeding c. Hence after at most c + 1 iterations the flow augmentation algorithm must end with the sink unlabeled. But i the sink is unlabeled, then Theorem 6.4 guarantees that a maximal flow has been obtained. Suppose now that all the capacities in the network are rational numbers. Find the least common denominator d of all the arc capacities, and consider the new network obtained by multiplying all of the original capacities by d. In this new network, all the capacities are integers. Applying the flow augmentation algorithm to the new network must therefore produce a maximal flow f in a finite number of steps by the argument above But then this same sequence of steps will produce a maximal flow for the original network in which the flow along arc (X, Y) is f (X, Y)/d. (See Exercises 13--15.) R Theorem 6.5 can be proved without the requirement that the capacities be rational numbers. More generally, Edmonds and Karp (1972) have shown that the flow augmentation algorithm produces a maximal flow in no more than lmn iterations, where m is the number of arcs and n is the number of vertices in the network. (See pages 117-119 of suggested reading [9] at the end of the chapter.) Note that, in each iteration of .he algorithm, we consider an arc (V, W) at most twice, once in the proper direct on from V to W and once in the opposite direction from W to V. Thus if we count the number of times that an arc is considered before obtaining a maximal flow, the cDmplexity of the flow augmentation algorithm is at most 2m(mn) = m2n. Since the number of arcs m cannot exceed n(n - 1), it follows that the complexity of the flow augmentation algorithm is at most n3 (n -1)2. We will end this section by proving a famous theorem discovered independently by Ford and Fulkerson and by Elias, Feinstein, and Shannon. (See suggested readings [6] and [4] at the end of the chapter.)
Theorem 6.6
M i Ppi s al Fhv;oe In any transportation network, the value of a maximal flow equals the capacity of a minimal cut.
6.3
The Max-Flow Min-Cut Theorem
345
Proof. Let f be a maximal flow in a transportation network. Apply the flow augmentation algorithm to this network with f as the current flow. Clearly the sink will not be labeled, for otherwise we would obtain a flow having a greater value than f, which is a maximal flow. But if the sink is not labeled, then Theorem 6.4 shows that the sets of labeled and unlabeled vertices form a minimal cut having capacity equal to the value of f. X
EXERCISES 6.3 In Exercises 1-4 give the capacity of the cut S, Tfor the network below.
Al
1. 5= {A,C,F andT {B,D,E,GI 3. S = {A, D, E} and T = {B, C, F, G}
2. S = {A, B, E} and T = {C, D, F, G} 4. S = {A, E, F) and T = fB, C, D, GI
In Exercises 5-S a network and a maximalflow are given. Find a minimal cutfor the network by applying the flow augmentationalgorithm to this network andflow.
5.
B
4,4
D
6 ,6
B
_5 ,4 2,
A
6.
53
3,0
D
2 ,0
A
F
_ 6 ,6
4,0 AgW~,3
5,5 w F 10,8
3,0
7,68,
7.
4,3
C
5,5
E
B
3,2
D
8.
C
5,0
E
B
2,0
D
3,3 2
A
8,8
F
A
52
73
F
8446
C
5,4
E
C
2,2
E
346
Chapter6 Network Flows
In Exercises 9-12 use the flow augmentation algorithm tofind a minimal cut. 10,
9. 3
/8
12.
11.
C
4
F
C
4
F
In Exercises 13-14 a network X' with rationalarc capacities is given. Let A' be the network obtainedfrom X' by multiplying all the capacities in A by d, the least common denominator of the capacities. Apply the flow augmentation algorithm to A', and use the result to determine a maximalflow for the original network A'. 14.
13.
A
E
D
D
15. Let A be a transportation network and d > 0. Define A' as in Exercises 13 and 14 to be the network with the same directed graph as A' but with all the arc capacities of A' multiplied by d. (a) Show that S, T is a minimal cut for A' if and only if it is a minimal cut for A'. (b) Prove that if v and v' are the values of maximal flows for A' and A', respectively, then v' = dv. (c) Show that f is a maximal flow for A if and only if f 'is a maximal flow for A', where f' is defined by
f'(X, Y) = df (X, Y). 16. Suppose that D is a weighted directed graph having a nonnegative weight (capacity) on each directed edge. Show that if any two distinct vertices of D are designat d as the source and the sink, then repeated use of the flow augmentation algorithm will produce a maximal flow from the source to the sink. (Thus the flow augmentation algorithm can be used even if conditions 1 and 2 in sh. definition of a transportation network are not satisfied.) 17. How many cuts are there in a transportation network with n vertices? 18. Let D be a directed graph, and let X and Y be distinct vertices in D. Make D into a network by giving each directed edge a capacity of 1. Show that the value of a maximal flow in this network equals the minimum number n of directed edges that must be removed fiom D so that there is no directed path from X to Y. (Hint: Show that if S, T is a minimal cut, then n equals the number of arcs from X to Y.)
6.4 Flows and Matchings
347
In Exercises 19-20 use the result of Exercise 18 to find a minimal set of directed edges whose removal leaves no directed pathfrom S to T.
19.
A
20.
D
S
T
B C
D
F
F
21. Consider an (undirected) graph g in which each edge {X, Y} is assigned a nonnegative number c(X, Y) = c(Y, X) representing its capacity to transmit the flow of some substance in either direction. Suppose that we want to find the maximum possible flow between distinct vertices S and T of G, subject to the condition that, for any vertex X other than S and T, the total flow into X must equal the total flow out of X. Show that this problem can be solved with the flow augmentation algorithm by replacing each edge (X, Y} of ! by two directed edges (X, Y) and (Y, X), each having capacity c(X, Y). Forthe graphs in Exercises 22-23, use the method described in Exercise 21 to find the maximal possibleflowfrom S to T if the numbers on the edges represent the capacity of flow along the edge in eitherdirection: 22.
A
3
C
23.
Si
S
OT
-
B
4
D
B
-
3
E
24. Prove that if the flow augmentation algorithm ends with the sink labeled, then the original flow has been replaced by a flow with a larger value. 25. Consider a transportation network with source S, sink T, and vertex set V. Let c(X, Y) denote the capacity of arc (X, Y). If X, Y E V but (X, Y) is not an arc in the network, define c(X, Y) = 0. Show that the average capacity of a cut is I(c(S, T) + XEV
6.4
+
c(S, X) + A, c(X, T) +
L
xe=v
xYEV
c(X, Y)).
FLOWS AND MATCHINGS In this section we will relate network flows to the matchings studied in Section 5.2. Recall from Sections 5.1 and 5.2 that a graph 9 is called bipartite if its vertex set V can be written as the union of two disjoint sets V, and V2 in such a way that all the edges in g join a vertex in V, to a vertex in V2. A matching of g is a subset M of the edges of g such that no vertex in V is incident with more than
348
Chapter 6
Network Flows
one edge in M. In addition, a matching of g with the property that no matching of g contains more edges is called a maximum matching of g. From abipartite graph g, Ne can form atransportationnetworkA as follows. (1) (2)
The vertices of X are the vertices of g together with two additional vertices s and t. These vertices s and t are the source and sink for AC, respectively. The arcs in AC are of three types. (a) There is an arc in A'C from s to every vertex in V1. (b) There is an arc in A' from every vertex in V2 to t. (c) If X is in VI, Y is in V2 , and {X, Y} is an edge in g, there is an arc from X to Y in JV.
(3)
All arcs in A have capacity 1.
We call A the network associated with g. c
Example 6.7 In the bipartite graph g in Figure 6.33, the vertex set V = {A, B, C, W, X, Y, Z} is partitioned into the sets VI {A, B, C} and V2 = {W, X, Y, Z}.
s
t
z
FIGURE 6.33
FIGURE 6.34
The network AC associated with g is shown in Figure 6.34. Note that AC contains a copy of 5 and two new vertices s and t, which are the source and the sink for JA, respectively. The edges of G that join vertices in VI to vertices in V2 become arcs in AC of capacity I directed from the vertices in VI to the vertices in V2. The other arcs in AC are directed from the source s to each vertex in VI and from each vertex in V2 to the sink t; these arcs also have capacity 1. + Consider the bipartite graph in Figure 6.35. The network associated with this graph is shown in Figure 6.36.
6.4 Flows and Matchings
349
A
X B Y t
s
C Z
D e-
D
I
FIGURE 6.36
FIGURE 6.35
When the flow augmentation algorithm is applied to the network in Figure 6.36, the zero flow can be increased by 1 unit along the path s, A, X, t; by 1 unit along the path s, B, Y, t; and by 1 unit along the path s, C, Z, t. The resulting maximal flow is shown in Figure 6.37. Notice that this is an integral flow.
D
IV FIGURE 6.37
Thus we see that a maximal flow in the network shown in Figure 6.36 has value 3, and one maximal flow is obtained by sending: 1 unit along s, A, X, t; 1 unit along s, B, Y, t; and
1 unit along s, C, Z, t. If we disregard the source and sink in these three paths, we obtain the three arcs (A, X), (B, Y), and (C, Z). These arcs correspond to the edges {A, X}, {B, Y},
and {C, ZJ in Figure 6.35. Clearly these edges are a maximum matching of the bipartite graph in Figure 6.35, because in this graph the set V2 contains only three vertices. Thus we have obtained a maximum matching of a bipartite graph by using the flow augmentation algorithm on the network associated with the graph. Theorem 6.7 shows that this technique will always work.
350
Chapter 6 Network Flows
Theorem 6.7
Let G be a bipartite graph and AF the network associated with G. (a)
(b)
Every integral flow in Af c orresponds to a matching of g, and every matching of g corresponds to an integral flow in K. This correspondence is such that two vertices are matched in g if and only if there is 1 unit of flow along the corresponding arc in K. A maximal flow in ' corresponds to a maximum matching of g.
Proof. Let the vertex set of 5 be written as the union of disjoint sets V1 and V2 such that every edge in G joins. a vertex in VI to a vertex in V2. (a) Let f be an integral flow in K, and let M be the set of edges {X, Y } in g for which X is in VI, Y is in 122, and f (X, Y) = 1. To prove that M is a matching of G, we must show that no vertex of G is incident with more than one edge in M. Let U be any vertex in g. Since g is the union of the disjoint sets V1 and V2, U belongs to exactly one of the sets VI or V2. Assume without loss of generality that U belongs to V1and that U is incident with the edge {U, V I in M. 'W'e will show that vertex U is not incident with any other edge in M. Suppose that {U, WI is another edge in M. Then f (U, V) = 1 and f (U, W) = 1 by the definition of M. Thus in K the total flow out of vertex U is at least 2. But inKN the only arc entering U is (s, U), and this arc has capacity 1. So the total flow into vertex U does not equal the total flow out of vertex U, a fact which contradicts that f is a flow in K. Hence U is incident with at most one edge in M, and so M is a matching of G. This proves that every integral flow in K corresponds to a matching of g. Now suppose that M is a matching in g. Let K be the network associated with 5, and let s be the source e in K and t be the sink. For each arc in K, define a function f by:
f (s, X) = 1 if X E 1), and there exists Z e V2 such that {X, Z} E M; f (Y, t) = I if Y E V2 and there exists W E V1 such that {W, Y} E M; f(X,Y)=lifXEVI,1
eV2 ,and{X,Y}JM;and
f (U, V) = 0 otherwise. Since each arc e in K has capacity 1 and 0 < f (e) < 1, f satisfies condition (1) in the definition of a flow. Now consider any vertex X of K other than s and t. Such a vertex is a vertex of G and, hence, belongs to either VI or V2. Assume without loss of generality that X belongs to V12.By the definition of f, either f (s, X) = 0 or f (s, X) = 1. If f (s, X) = 0, then there exists no Z c 12 such that {X, Z) E M; so the total flow into X and the total flow out of X are both 0. On the other hand, if f (s, X) = 1, then there exists Z c V2 such ihat {X, Z} E M. Because M is a matching of g, Z is unique. Thus, in this case also, the total flow into X equals the total flow out of X, and so f satisfies condot on (2) in the definition of a flow. It follows that f is a flow in K. This proves that every matching of G corresponds to an integral flow in K.
6.4 Flows and Matchings
351
(b) Under the correspondence described in part (a), the total number of vertices in VI that are matched with vertices in V2 is the value of the flow f. Thus M is a maximum matching of G if and only if f is a maximal flow in .A. Course
Professors
I
Abel, Crittenden, Forcade
2
Crittenden, Donohue, Edge, Gilmore
3
Abel, Crittenden
4
Abel, Forcade
5
Banks, Edge, Gilmore
6
Crittenden, Forcade
Example 6.8 Recall the example from Section 5.1 in which an English department wishes to assign courses to professors, one course per professor. The list of professors available to teach the courses is given above. The English department would like to obtain a maximum matching so that it can offer the largest possible number of courses. As in Section 5.2, we can represent this problem by a bipartite graph with the vertex set V = {1, 2, 3, 4, 5, 6, A, B, C, D, E, F, G }, where we have denoted the professors by their initials. Here the set V can be partitioned as the union of the disjoint sets of courses and professors V1 = 11,2, 3,4,5,6)
and
V2 = {A, B,C, D, E, F, G},
respectively. By drawing an edge between each professor and the courses he or she can teach, we obtain the graph shown in Figure 6.38. (This is the graph obtained previously in Figure 5.1.) A
2
B
3
C
4
D
5
E
6
F G FIGURE 6.38
FIGURE 6.39
352
Chapter 6 Network Flows
We will obtain a maxirtlrn matching for the graph in Figure 6.38 using the flow augmentation algorithm. Let us begin by assigning professors A, C, F, and E to teach courses 1, 2, 4, and 5, respectively. This gives the matching with edges {1, Al, {2, C }, {4, Fl . and {5, E}. The network associated with the graph in Figure 6.38 is shown in Figure 6.39. Here all arcs are directed from the left to the right and have capacity 1. The matching {1, Al, {2, C}, {4, Fl, 15, El obtained above corresponds to the flow shown in Figure 6.40, where arcs having a flow of zero are shown in black and those with a flow of 1 are shown in blue. I
A
I
A
s
C
G
FIGURE 6.40
FIGURE 6.41
If we apply the flow augmentation algorithm to the network and flow in Figure 6.40, we find that s, 3, C, 2. D, t is a flow-augmenting path. Increasing the flow by 1 along this path gives the flow in Figure 6.41. If another iteration of the Row augmentation algorithm is performed on the flow in Figure 6.41, only vertices s, 1, 3, 4, 6, A, C, and F will be labeled. Thus the flow shown in Figure 6.41 is a maximal flow. By Theorem 6.7 this means that the corresponding matching {1, A), {2, D} {3, Cl, {4, Fl, and {5, El is a maximum matching for the bipartite graph in Figure 6.38. Hence the English department can offer 5 of the 6 courses by assigning course 1 to Abel, course 2 to Donohue, course 3 to Crittenden, course 4 to Forcade, and course 5 to Edge. +
EXERCISES 6.4 In Exercises 1-6 determine whether the given graph is bipartite or not. If it is, construct the network associated with the graph. 1.
2.
A
C
E
F
H
6.4 Flows andMatchings
3.
4.
5.
6.
E
F
F
353
G
In Exercises 7-10 a bipartitegraph is given with a matching indicatedin color. Construct the network associated with the given graph, and use the flow augmentationalgorithm to determine whether this is a maximum matching. If not,find a largermatching. -
7.
.
.
A
X
B
y
8. A
z
9. A
W
B
X
C
y
D
z
10.
B C W
D
X
y
Z
In Exercises 11-14 use theflow augmentationalgorithm tofind a maximum matchingfor the given bipartitegraph. 11.
,I
12.
a
b 2 C
d
354 13.
a
Chapter 6 Network Flows I,
-
-
-
1
1I4.
e
b
B
C
C
d
D
1
2
4 5
e
15. Four mixed couples are needed for a tennis team, and f men and 4 women are available. Andrew will not play with Flo or Hannah; Bob will not play with Iris; Flo, Greta, and Hannah will not play with Ed; Dan will not play with Hannah or Iris; and Cal will only play with (3-eta. Can a team be put together under these conditions? If so, how? 16. Five actresses are needed for parts in a play that require Chinese, Danish, English, French, and German accents. Sally does English and French accents; Tess does ('inese, Danish, and German; Ursula does English and French; Vickie does all accents except English; and Winona does all except Danish and German. Can the five roles be filled under these conditions? If so, how? 17. The five assistants in the Mathematics Department must decide which jobs each will do. Craig likes filing and collating, Dianne can distribute paychecks and help students, Gale types and collates, Marilyn enjoys typing and distributing the paychecks, and Sharon prefers lo help students. Can the jobs be assigned so that every assistant is given one of his or her preferences? If so, how? 18. When the flow augmentation algorithm is applied to the network and flow in Figure 6.41, only the vertices, s, 1, 3, 4, 6, A, C, and F will be labeled. What is the significance of courses 1, 3, 4, and 6 and professors A, C, and F in the context of Example 6.8? 19. Describe how to use the flow augmentation algorithm to determine if a system of distinct representatives exists for a sequence of sets SI, S2, . . ., Sn. 20. Apply the flow augmentation algorithm as described in Exercise 19 to find a system of distinct representatives, if possible, for the sequence of sets (3, 41, (1, 51, (2, 3}, (2, 51, 11, 4}. 21. Apply the flow augmentation algorithm as described in Exercise 19 to find a system of distinct representatives, if possible, for the sequence of sets {2, 5}, (1, 6}, 13, 51, (4, 61, {2, 3}, {2, 3, 5}. 22. Five men and five women are attending a dance. Ann will dance only with Gregory or Harry, Betty will dance only with Frank or Ian, Carol will dance only with Hany or Jim, Diane will dance only with Frank or Gregory, and Ellen will dance only with Gregory or Ian. Is it possible for all ten people to dance the last dance with an acceptable partner? If so, how? 23. The History Department at a state college would like to offer six courses during the summer. Although there are seven professors available, only certain professors can leach each course. The list of courses and the professors capable of teaching them is shown below. Is there an assignment of professors to courses so that no professor teaches more than one course? Courses
P'rofessors
American History
Getsi, Dammers, Kagle, Ericksen
British History
D)uncan, Getsi, Harris
Latin American History
D)uncan, Getsi
Oliver Cromwell
I)uncan, Harris
Ancient History
White, Kagle, Harris
20th Century History
Getsi, Harris
HistoricalNotes
355
24. Suppose that the flow augmentation algorithm is applied to find a system of distinct representatives for a sequence of sets SI, S 2 , . . ., S, as described in Exercise 19. If the algorithm is applied to a maximal flow with value less than n, prove that the number of labeled sets must exceed the number of elements in the union of the labeled sets. In Exercises 25-28 let 5 be a bipartite graph in which the set of vertices is written as the union of two disjoint sets VI and V 2 such that all the edges in 5 join a vertex in VI to a vertex in V2 . For each subset A of VI, let A* denote the set of vertices in 5 that are adjacent to some vertex in A. The maximum value d of AI - IA* I over all subsets A of VI is called the deficiency of 5. 25. Prove that d > 0. 26. Prove that if A' is the network associated with g, then A' has a flow with value IV1 I - d. 27. Let X be the network associated with 5, and let s and t be the source and sink of A, respectively. Let A be a subset of VI such that JAI - A*I = d. Prove that the cut S, T with
S={s}UAUA*
and
T=(Vl-A)U(V2 -A*)U
tt
has capacity IV, I-d. 28. Deduce that a maximal flow in A' has value VI -d and hence that a maximum matching in g contains VI I-d edges. In Exercises 29-31 let 5 be a bipartite graph in which the vertex set is written as the union of two disjoint sets VI and V2 such that all the edges of 5 join a vertex in VI to a vertex in V2 . Suppose that the flow augmentation algorithm is performed on the network associatedwith 5 until the algorithm ends with the sink unlabeled. 29. Prove that if X E VI, Y E V2 , and f(X, Y) = 1, then, in the last iteration of the flow augmentation algorithm, X is unlabeled or Y is labeled. 30. Prove that in Exercise 29 it is impossible that X is unlabeled and Y is labeled. Deduce that the number of unlabeled vertices in VI plus the number of labeled vertices in V2 equals the value of the present
flow f. 31. In the context of Exercise 30, show that there are no edges in 5 that join a labeled vertex X E Vi to an unlabeled vertex Y E V2. Deduce that the unlabeled vertices in VI and the labeled vertices in V2 form a minimum covering of 5 in the sense of Section 5.2. (Hint: The label on X must be either (s, +, 1) or (Z, -, 1) for Z 7$ Y.)
HISTORICAL NOTES The concept of flows in transportation networks is a recent mathematical discovery, with the majority of the work in this area appearing since 1960. The initial work in the field was provided by Lester Randolph Ford Jr. (1927- ) and Delbert Ray Fulkerson (1924-1976) in a series of papers, the first of which appeared in 1956 and 1957. Their seminal 1962 text Flows in Networks outlined the field. While others were later able to make some improvements to their algorithms, their basic approaches still define the way network flows are conceptualized.
356
Chapter 6 Network Flows
SUPPLEMENTARY EXERCISES In Exercises -l find a maximalflow and a minimal cut in each transportationnetwork by using the flow augmentation algorithm. Start with the flow that is 0 along every arc, and if there is a choice of vertices to label, label the vertices in alphabeticalorder. 2.
1.
A
IF
B
3.
A
4
E
<
5.
B
4.
4
E
A
GG
C
2
F
C
2
B
7
F
B
7
6.
6/
F
4
3S
4
5
H
A
A 5
6
,S
8
81 iD
7.
B
3 4
F
2 3 ? I
8.
1K
5
c.
9
3
H
45 !
A
8 D
G
E
6 A~
2
14
G
H
_
5H
C
.J
Cs D
9
46 G
7+
K
LIX
3
I
By a multisource transportation network we mean a weighted directed graph that satisfies the conditions in the definition of a transportationnetwork except that instead o, its containing only one vertex of indegree 0, there is a nonempty finite set So of vertices with indegree 0. We say that f is aflow in such a network if: (i) 0 < f (e) < c(e)for each arc e, where c(e) is the capacity of arc e; and (ii) for each vertex V other than the sink and the elements cf So, the totalfiow into V equals the totalfiow out of V. The value of such aflow is the totalflow into the sink, and a flow is called a maximal flow if its value is as large aspossible.
SupplementaryExercises
357
9. Show that in a multisource transportation network the value of a flow equals the total flow out of all the vertices in So. 10. Given a multisource transportation network K, create from JV a transportation network .,V' by introducing a new vertex u and edges with infinite capacity from u to each element in So. Show that a maximal flow in Al may be found by applying the flow augmentation algorithm to K'. By using Exercise 10, find a maximal flow in each of the multisource transportationnetworks given in Exercise 11-12. 11.
12.
A
G
13. Generalize the concept of a transportation network to allow multiple sources and sinks; then define 'flow,' 'value of a flow,' and 'maximal flow' for such a network. State and prove an analogue of Theorem 6.1 for such a network. 14. By a network with vertex capacities we mean a transportation network K along with a function k from its set of vertices to the nonnegative real numbers. In such a network a flow must satisfy the additional restriction that, for each vertex V, neither the total flow into V nor the total flow out of V can exceed k(V). (Of course, these totals are the same if V is a vertex other than the source or sink.) Show that the value of a maximal flow in such a network equals the value of a maximal flow in the ordinary transportation network JK*, where Ko* is formed as follows. (i) For each vertex X in K, include two vertices X' and X' in JV*. (ii) For each vertex X in K, include an arc (X', X') in JK* with capacity k(X). (iii) For each arc (X, Y) in jK, include an arc (X', Y') of the same capacity in JK*. (Note that if in
K the source is s and the sink is t, then in K* the source is s' and the sink is t'.)
For the networks with vertex capacities in Exercises 15-1 7, construct the network K* described in Exercise 14. 15. k(A) = 9, k(B) = 8, k(C) = 9, k(D) = 7, k(E) = 10
A
D
358
Chapter 6 Network Flows
16. k(A) = 8, k(B) = 4, k(C) = 7, k(D) = 7, k(E) = 6, k(F) = 9
B A
D
_,
4
C
3 3
4
3
E
F
17. k(A) = 16, k(B) = 9, k(C) = 6, k(D) = 5, k(E) -= I, k(F) = 7, k(G) = 15
B
A
2
E
3
C
G
F
In Exercises 18 and 19 use the method of Exercise 14 tofind a maximal flowfor the network with vertex capacities in the indicatedexercise. 18. Exercise 15
19. Exercise 16
20. Let s and t be vertices in a directed graph D having indegree 0 and outdegree 0, respectively. Make D into a transportation network./V with vertex capacities by letting s and t have infinite capacity, letting the other vertices have capacity 1, and letting each arc have capac ity 1. Use Exercise 14 above and Exercise 30 of Section 6.2 to show that the value of a maximal flow for .A equ als the number of directed paths from s to t that use no vertex other than s and t more than once. 21. Let m and n be positive integers. Make a transportation network with vertices Y1, Y2, .. Y, t and an arc of infinite capacity frorr s to each Xi, an arc of infinite Yj to t, and an arc of capacity I from Xi to Yj for e4ery i and j. Prove that if f is an network, then there exists an m x n matrix of Os and I s having f(s, Xi) Is in row i and f j for every i and j.
s, XI, X2 . , Xm, capacity from each integral flow in this (Yj, t) Is in column
22. Let A be an m x n matrix of Os and Is with u, Is in iow i and vj is in column j for every i and j. Prove that UI + U2 + ' '
+Um V1 + V2 + '
+ Vn
23. Let m and n be positive integers, and let ui and vj lhe nonnegative integers for 1 < i < m and I < j < n. Suppose that U I+
U2 +
U+ rm = VI + V2 +
+ Vn.
Construct a network having the same arcs as in Exercise 21, but let the capacity of each arc (s, Xi) be ui and the capacity of each arc (Yj, t) be vj. Show that if the value of a maximal flow in this network is u I + u2 + *.+ Ur, then there exists an m x n matrix of Os and is with u, Is in row i and vj is in column j for every i and j. 24. If in Exercise 23 the value of a maximal flow is not ,Il + u2 + *.+ um, prove that there does not exist an m x n matrix of Os and Is with u Is in row i and vj 1 3 in column ] for every i and j. 25. Use Exercise 23 to construct a 4 x 6 matrix of Os anc.: s in which there are (i) four is in rows 1, 2, and 4; and two Is in row 3; (ii) three Is in columns 1, 3, and 6; two Is in columns 2 and 4; and one 1 in column 5.
Suggested Readings
359
COMPUTER PROJECTS Write a computer program having the specified input and output. 1. For a given transportation network, compute the capacity of every cut. 2. For a given transportation network, determine a maximal flow by repeated use of the flow augmentation algorithm starting with the flow that is zero along every arc. 3. For a given bipartite graph, use the method described in Section 6.4 to produce a maximum matching. 4. For a given bipartite graph, use the method described in Section 6.4 to produce a minimum covering. (See Exercise 31 in Section 6.4.) 5. For a given bipartite graph, compute IAI-1 IA* I for each subset A of VI. (The notation is as in the instructions for Exercises 25-28 in Section 6.4.) 6. For a given network with multiple sources and sinks (see Supplementary Exercise 13), determine a maximal flow. 7. For a given network with vertex capacities (see Supplementary Exercise 14), determine a maximal flow. 8. Let m and n be positive integers, and let ui for 1 < i < m and vj for 1 < j < n be nonnegative integers such that U +U2
+
*+Um
=VI+
V2 +
+ V,-
Construct, if possible, an m x n matrix of Os and ls with ui Is in row i and vj Is in column j for every i and j. (See Supplementary Exercise 23.)
SUGGESTED READINGS 1. Bondy, J. A. and U. S. R. Murty. Graph Theory with Applications. New York: Elsevier Science, 1979. 2. Burr, Stefan A., ed. The Mathematics of Networks. In Proceedings of Symposia in Applied Mathematics, vol. 26. Providence, RI: American Mathematical Society, 1982. 3. Edmonds, Jack and Richard M. Karp. 'Theoretical Improvements in Algorithmic Efficiency for Network Flow Problems.' J. Assoc. Computing Machinery, vol. 19, no. 2 (April 1972): 248-264. 4. Elias, P., A. Feinstein, and C. E. Shannon. 'Note on Maximal Flow Through a Network.' IRE Transactions on Information Theory, IT- 2 (1956): 117-119. 5. Ford, Jr., L. R. and D. R. Fulkerson. Flows in Networks. Princeton, NJ: Princeton University Press, 1962. 6.
. 'Maximal Flow Through a Network.' Canad. J. Math. vol. 8, no. 3 (1956): 399-404.
7. Frank, Howard and Ivan T. Frisch. 'Network Analysis.' Sci. Amer. vol. 223, no. 1 (July 1970): 94-103. 8. Fulkerson, D. R., ed. Studies in GraphTheory, Part I. MAA Studies in Mathematics, vol. 11. The Mathematical Association of America, 1975. 9. Lawler, Eugene L. Combinatorial Optimization: Networks and Matroids. New York: Holt, Rinehart, and Winston, 1976. 10. Wilf, Herbert S. Algorithms and Complexity. Englewood Cliffs, NJ: Prentice Hall, 1986. 11. Zadeh, Norman. 'Theoretical Efficiency of the Edmonds-Karp Algorithm for Computing Maximal Flows.' J. Assoc. Computing Machinery, vol. 19, no. I (January 1972): 184-192.
Counting Techniques 7.1 Pascal's Triangle and the ]3 inomial Theorem 7.2 Three Fundamental Principles 7.3 Permutations and Combina ions 7.4 Arrangements and Selections with Repetitions 7.5 Probability 7.6*The Principle of Inclusion-lExclusion 7.7*Generating Permutations and r-Combinations
A
we saw in Sections 1.2 and 1.3, many combinatorial problems involve counting. Since the number of objects under consideration is often extremely large, it is desirable to be ableIo count a set of objects without havingto listthem all. In this chapter we will discuss several fundamental counting techniques that are frequently used in solving combinatorial problems. The reader should carefully review Sections 1.2 and 2.7, which contain several results we will refer to.
7.1
o
PASCAL'S TRIANGLE AND THE BINOMIAL
THEOREM One of the most basic problems that arises in combinatorial analysis is to count the number of subsets of a given set that contain a specified number of elements. Recall that we are denoting this number by C(n, r). Theorem 2.12 shows that C(n, r) = c
!(
)!
(7.1)
Example 7.1 By (7.1) we see that the number of subsets containing two elements that can be formed from the set of vowels {a, e, i, o, ul is C(5, 2)z
360
5! 2!3!
=
5 4.3! 2.1.-3!
=
5 4 = 10. 2.1
7.1
Pascal'sTriangle and the Binomial Theorem
361
The ten subsets in question are easily seen to be {a, el, {a, i}, {a, ol, {a, u}, {e, i}, {e, o}, {e, u), {i, ol, {i, u), and {o, u). + The proof of Theorem 2.12 shows that any subset R of {ai, a2 , . ., an} that contains r elements (1 < r < n) is either: (1) (2)
a subset of {a, a2 , . . ., an-l} containing r elements (if an g R), or the union of {an} and a subset of {a, a2, . . ., an- } containing r - 1 elements (if an E R).
Thus the number of subsets of {a 1, a 2 , . . ., an} containing r elements, C(n, r), is the sum of the number of subsets of type (1), C(n - 1, r), and the number of subsets of type (2), C(n - 1, r - 1). We have obtained the following result. Theorem 7.1
If r and n are integers such that 1 < r < n, then C(n,r)=C(n- 1,r - 1)+C(n - 1,r).
+
Example 7.2 It follows from Theorem 7.1 that C (7, 3) = C (6, 2) + C (6, 3). We will verify this equation by evaluating C(7, 3), C(6, 2), and C(6, 3) using (7.1): 6! 6.5.4! 6 .5 = = 1 = 15, 2! 4! 2 I.1.4! 2 -1 6! 6.5.4.3! 6.5.4 C(6, 3) = 3 = 3 = 20, and C(6, 2) =
3!3!
3.2.1.3!
3.2.1
7! C(7,3) = 3!4!
7.6.5.4! 3*2.1-4!
7.6.5 = 35. 3.2.1
SoC(6,2)+C(6,3)= 15+20=35=C(7,3).
+
The triangular array below n =0 n =1
C(0,0) C(1, O) C(1, 1) n 2 C(2,0) C(2, 1) C(2,2) n =3 C(3, 0) C(3, 1) C(3, 2) C(3, 3) n =4 C(4,0) C(4, 1) C(4,2) C(4,3) C(4,4)
is called Pascal's triangle. Although this array was first known to the Chinese, its name comes from the French mathematician Blaise Pascal (1623-1662), whose paper Traite du TriangleArithmetiquedeveloped many of the triangle's properties.
362
Chapter 7 Counting Techniques
Note that the rows of the triangle are numbered beginning with row n = 0 and that the entries C (n, r) for a fixed value of r lie along a diagonal extending from the upper right to the lower left. Let us consider the entries of Pascal's triangle in more detail. Since every set has exactly one subset containing 0 elements (namely the empty set), C (n, 0) = 1. Moreover, there is only one subset of n elements that can be formed from a set containing n elements (name ly the entire set); so C (n, n) = 1. Therefore the first and last numbers in every rowv of Pascal's triangle are ls. In addition, Theorem 7.1 states that every entry that is not first or last in its row is the sum of the two nearest entries in the row above. For example, C(4, 2) = C(3, 1) + C(3, 2) and C(4, 3) = C(3, 2) + C(3, 3). By repeatedly using these properties, we can easily evaluate the entries in Pascal's triangle. The resulting numbers are shown below. I 1
1 1 1
+
1
2 3
4
1 3
6
1 4
1
Example 7.3 Continuing in the triangle atove, we see that the numbers in the next row (the row n = 5)are 1, 1+4=5, 4 + 6 =10, 6+4= 10, 4+1=5, and1.
+
Pascal's triangle contains; an important symmetry: Each row reads the same from left to right as it does from right to left. In terms of our notation, this statement means that C(n, r) = C(n. n -- r) for any r satisfying 0 < r < n. Although this property is easily verified by computing C(n, r) and C(n, n - r) using (7.1), we will prove this fact by a combiatorialargument, a proof based on the definition of these numbers. (This is the same type of argument used to establish Theorem 7. 1.)
Theorem 7.2
If r and n are integers such that 0
<
r < n, then C(n, r) = C(n, n
-
r).
Proof. Recall that C(n, fl is the number of subsets containing k elements that can be formed from a set of n elements. Let S be a set of n elements. The function that assigns to an r-element subset A C S the (n - r)-element subset A, the complement of A witty respect to S, is easily seen to be a one-to-one correspondence between the sLbsets of S that contain r elements and the subsets of S that contain n - r elements. Hence the number of r-element subsets of S is the same as the number of subsets that contain exactly n - r elements, that is, C(n, r) = C(n, n - r). O
7.1 Pascal'sTriangleand the Binomial Theorem
363
The numbers C(n, r) are called binomial coefficients because they appear in the algebraic expansion of the binomial (x + y)n. More specifically, in this expansion, C(n, r) is the coefficient of the term x,-ryr. Thus the coefficients that occur in the expansion of (x + y)f are the numbers in row n of Pascal's triangle. For example, (x + y) 3 = (x + y)(x + y) 2 = (x + y)(X2 2
2
=x(x + 2xy + y ) +y(x
2
= (x3 + 2x 2 y + xy 2 ) + (x 2 y 3
2
2
+ 2xy + y 2 )
+ 2xy + y 2 )
+ 2xy 2
+y)
3
= x + 3x y + 3xy + y .
Note that the coefficients (1, 3, 3, and 1) occurring in this expansion are the numbers in the n = 3 row of Pascal's triangle.
Theorem 7.3
lhe
For every positive integer n,
inonua1TN nre,7n
(X + y)n
+ C(n, n -l)xy'-l + C(n, n)y'.
= C(n, O)Xn + C(n, I)x'-Iy +
Proof. In the expansion of (X + y)n = (X
+ y)(x
+ y)
(X + y),
we choose either an x or a y from each of the n factors x + y. The term in the expansion involving xn-ryr results from combining all the terms obtained by choosing x from n -r factors and y from r factors. The number of such terms is, therefore, the number of ways to select a subset of r factors from which to choose y. (We will select x from each factor from which we do not choose y.) Hence the coefficient of Xn-ryr in the expansion of (x + y)n is C(n, r). a
+
Example 7.4 Using the binomial theorem and the coefficients from the n triangle, we see that (x + y)4 = C(4,O)x 4 =x
4
3
+ 4x y
+
=
4 row of Pascal's
C(4, 1)x3 y + C(4, 2)x 2 y 2 + C(4, 3)xy 3 2 2
+ 6x y
+ 4xy3
4
+y .
+g
EXERCISES 7.1 Evaluate the numbers in Exercises 1-12. 2. C(7, 2) 1. C(5, 3) 2 2 5. the coefficient of x y in the expansion of (x +
3. C(8, 5) y)
4
4. C(12, 7)
+ C(4, 4)y4
364
Chapter 7 Counting Techniques
6. the coefficient of x 5 y in the expansion of (x + y)6 7. the coefficient of x 3 y 9 in the expansion of (x _ y) 12 8. the coefficient of x5 y4 in the expansion of (x -y)9 9. the coefficient of x6 y 4 in the expansion of (x + 2y) 1c 10. the coefficient of x4 y 9 in the expansion of (3x + )13 11. the coefficient of x3 y7 in the expansion of (x-
3) IC
7 2
12. the coefficient of x y in the expansion of (2x-
)9
13. Write the numbers in the n = 6 row of Pascal's triangle . 14. Write the numbers in the n = 7 row of Pascal's triangle. 15. Evaluate (x + y) 6 . 17. Evaluate (3x
-
16. Evaluate (x + y)7 .
y)4 .
13. Evaluate (x -2y)
5
.
19. How many subsets containing four different numbers can be formed from the set {I, 2, 3, 4, 5, 6, 71? 20. How many subsets containing eight different letters can be formed from the set {a, b, c, d, e, f, g, h, i, j, k, 11? 21. How many subsets of (b, c, d, f, g, h, j, k, I, ml contain five letters? 22. How many subsets of {2, 3, 5, 7, 11, 13, 17, 19, 231 contain four numbers? 23. How many four-element subsets of {1, 2, 3, 4, 5, 6, 7. 8;,9, 10, 11, 121 contain no odd numbers? 24. How many three-element subsets of {a, b, c, d, e, f, g. h, i, j, k} contain no vowels? 25. Use the binomial theorem to show that C(n, 0) + C(ri, 1) + * + C(n, n) 26. Use (7.1) to verify that C(n, r) = C(n, n -r)
for 0< E
=
2' for all nonnegative integers n.
< n.
27. Use the binomial theorem to show for n > 0 that C(n,0) - C(n,1) +C(n, 2) -C'n,3) + 28. Prove that 2°C(n, 0) + 21C(n, 1) + *.
29. Prove that rC(n, r) = nC(n-
1, r -1)
+ 2'C(n, n)
for 1 < r
<
-+ (-l)nC(n, n) =
.
3f for all positive integers n.
a.
30. Prove that 2C(n, 2) + n2 = C(2n, 2) for n > 2. 31. For any positive integer k and any nonnegative integer -, prove that
C(k,0)+C(k+l, 1)+- - -C(k+r,r)=C(k+r+1,r). 32. ProvethatC(r,r) +C(r + 1,r)+ --.. + C(n,r)= C(n + 1,r + 1)forO <=' n.whydoyouthinkthename=' ' hockey=' stick=' formula'=' is=' used=' for=' this=' result?=' 33.=' prove=' that=' c(2n=' +=' 1,=' 0)=' 1)='>
+ C(2n +-1, n) = 22n for all nonnegative integers n.
34. Let p and r be integers such that p is prime and I 35. Let k and n be nonnegative integers such that k
<
r < p - 1. Prove that C(p, r) is divisible by p. '. Prove that C(n, k) < C(n, k + 1).
36. Prove that C(m, 2) + C(n, 2) < C(m + n -1, 2) for any integers m, n > 2. 37. Prove that the product of any n consecutive positive integers is divisible by n!. 38. Prove that I * C(n, 0) + 2 . C(n, 1) + - . + (n + 1) Ci' n, n) = 2n + n2n
for all nonnegative integers n.
7.2 Three FundamentalPrinciples
7.2
+
365
THREE FUNDAMENTAL PRINCIPLES In this section we will introduce three basic principles that will find frequent use throughout this chapter. The first of these is a suprisingly simple existence statement that has many profound consequences.
Theorem 7.4
1 4ll Ou': . Vf
S
If asetof pigeonsisplacedintopigeonholes andthere
are more pigeons than pigeonholes, then some pigeonhole must contain at least two pigeons. More generally, if the number of pigeons is more than k times the number of pigeonholes, then some pigeonhole must contain at least k + 1pigeons. Proof The first statement is a special case of the more general result, namely, the case in which k = 1. We will prove only the more general result. Suppose that there are p pigeonholes and q pigeons. If no pigeonhole contains at least k + I pigeons, then each of the p pigeonholes contains at most k pigeons; so the total number of pigeons cannot exceed kp. Thus if the number of pigeons is more than k times the number of pigeonholes (that is, if q > kp), then some pigeonhole must contain at least k + 1 pigeons. 0 +
Example 7.5 How many people must be selected from a collection of 15 married couples to ensure that at least two of the persons chosen are married to each other? It is easy to see intuitively that if we choose any 16 persons from this collection of 15 couples, we must include at least one husband and wife pair. This conclusion is based on the pigeonhole principle. Let us place persons (the pigeons) into sets (the pigeonholes) in such a way that two persons are in the same set if and only if they are married to each other. Since there are only 15 possible sets, any distribution of 16 persons must place two people in the same set. Thus there must be at least one married couple included among the 16 persons. Note that if we choose fewer than 16 persons, a married couple may not be included (for instance, if we choose the 15 women). +
+
Example 7.6 How many distinct integers must be chosen to assure that there are at least 10 having the same congruence class modulo 7? This question involves placing integers (the pigeons) into congruence classes (the pigeonholes). Recall that there are 7 distinct congruence classes modulo 7, namely, [0], [1], [2], [3], [4], [5], and [6]. So if we want to guarantee that there are at least 10 = k + 1 integers in the same congruence class, the generalized form of the pigeonhole principle states that we must choose more than 7k = 7 9 = 63 distinct integers. Hence at least 64 integers must be chosen. +
366
Chapter 7 Counting Techniques
Often the pigeonhole principle is the key to the solution of a problem that requires producing a pair of elements with special properties. The next example is of this type. +
Example 7.7 Choose any five points from the interior of an equilateral triangle having sides of length 1. Show that the distance between some pair of these points does not exceed 2. Subdivide the given triangle into 4 equilateral triangles with side 22 as shown in Figure 7.1. Since there are 5 points and only 4 small triangles, some pair of points must lie in the same triangle. But it is easy to see that any 2 points lying in the same small triangle must be such that their distance apart does not exceed
FIGURE 7.1
In contrast to the pigeonhole principle, which asserts that some pigeonhole contains a certain number of pigeons (an existence statement), the next two results tell us how to count the number of ways to perform certain procedures. The first theorem is a restatement of a result from Section 1.2 that enables us to count the number of ways of performing a procedure that consists of a sequence of operations. Theorem 7.5
The Multiplication Principle Consider a procedure that is composed of a sequence of k steps. Suppose that the first step can be performed in nI ways, and for each of these the second step can be performed in n2 ways, and, in general, no matter how the preceding steps are performed, the ith step can be performed in ni ways (i = 2, 3, . . , k). Then the number of different ways in which the entire procedure can be performed is n1 * n2 -. . - nkTo illustrate the multiplication principle, suppose that a couple expecting a child has decided that if it is a girl, they will give it a first name of Jennifer, Karen, or Linda and a middle name of Ann or Marie. Since the process of naming the child can be divided into the two steps of selecting a first name and selecting a middle name, the multiplication principle tells us that there are 3 2 = 6 possible .
7.2 Three FundamentalPrinciples
367
names that can be given. To see that this is the correct answer, we can enumerate the possibilities as in Figure 7.2. Firstname
Middle name
Complete name
Ann
Jennifer Ann
Marie
Jennifer Marie
Ann
Karen Ann
Marie
Karen Marie
Ann
Linda Ann
Marie
Linda Marie
Jenniferc
Karen Lindac
K
FIGURE 7.2
+
Example 7.8 A bit (or binary digit) is a zero or a one. An n-bit string is a sequence of n bits. Thus 01101110 is an 8-bit string. Information is stored and processed in computers in bit strings because a bit string can be regarded as a sequence of on or off settings for switches inside the computer. Let us compute the number of 8-bit strings using the multiplication principle. To do so, we will regard the 8-bit string as a sequence of 8 choices (choose the first bit, then choose the second bit, and so forth). Because each bit can be chosen in 2 ways (namely, zero or one), the number of possible 8-bit strings is
2 2 2 2 2 2222 =28
= 256.
Since an 8-bit string can be regarded as the binary representation of a nonnegative integer, this calculation shows that 256 nonnegative integers can be expressed using no more than 8 binary digits. More generally, a similar argument shows that the number of n-bit strings (and, hence, the number of nonnegative integers that can be expressed using no more than n binary digits) is 2n. + +
Example 7.9 On January 20, 1996, telephones in the northern suburbs of Chicago were given a new area code (847). Previously all the suburbs had the same area code (708). Other suburban and city regions will soon be similarly divided. With the increased demand by businesses for telephone lines for computers, fax machines, and cellular phones, metropolitan areas are actually running out of phone numbers! How many different telephone numbers with the same area code are possible? Within one area code, a local telephone number is a sequence of seven digits (0-9) with the restriction that the first and second digits cannot be 0 or 1. The number of possible local phone numbers can therefore be counted using the multiplication principle. Each of the first two digits can be any of the numbers
368
Chapter 7
Counting Techniques
2-9, and the remaining digits can be any value 0-9. Thus the number of possible local phone numbers, which equals the number of ways that the seven digits can be chosen, is 8 .8.10
10
100.10
= 6,400,000.
+
When using the multiplication principle, it is important to note that the number of ways to perform a step must not depend on the particularchoice that is made at any previous step. That is, in the notation of Theorem 7.5, no matter how the first step is performed, there must be n2 ways of performing the second step; no matter how the first two steps are performed, there must be n3 ways of performing the third step, anc so forth. Because of this restriction, some ingenuity may be required to obtain the correct solution to a problem, as in the following example. +
Example 7.10 Suppose that we are to use the digits 1-8 without repetition to make five-digit numbers. (a) How many different five-digit integers can be made? (b) How many of the numbers in part (a) begin with 7? (c) How many of the numbers in part (a) contain both 1 and 2? (a) We can construct five-Jigit numbers by choosing a value for each of the five digits in the number. This amounts to filling each of the blanks below
with one of the digits 1-8. Clearly there are 8 ways in which the first digit can be selected because any of the digits 1-8 can be used. There are only 7 ways to choose the second digit, however, because the first digit cannot be repeated. Similar reasoning shows that there are 6 ways to choose the third digit, 5 ways to choose the fourth digit, and 4 ways to choose the fifth digit. Hence the multiplication principle shows that the number of possible ways of making all five choices is
8 . 7 . 6- 5 . 4 = 6720. This is the number of five-digit numbers that can be formed from the digits 1-8 without repetition. (b) To count the numbers. that begin with 7, we can proceed as above except that there is only one way to choose the first digit (since it must be 7). Therefore of the 6720 integers in part (a), I 7 . 6. 5 . 4 = 840 begin with 7. (c) The method used in the preceding parts cannot be used to count the number of five-digit numbers containing both 1 and 2. The reason this method fails is that
7.2 Three FundamentalPrinciples
369
the number of ways to select the fourth and fifth digits depends on earlier choices. For example, if the first three digits are 231, then there are 5 ways in which the fourth digit can be chosen (namely, 4, 5, 6, 7, or 8); but if the first three digits are 567, then there are only two ways in which the fourth digit can be chosen (namely, 1 or 2). Consequently we must look for another approach. Since the digits 1 and 2 must be used, we begin by deciding where to put them. We can then fill the three remaining positions with any of the digits 3-8. Thus we proceed as follows. Choose Choose Choose digits). Choose digits). Choose digits).
a position for the I (in 5 possible ways). a position for the 2 (in 4 possible ways). a value to put in the first unfilled blank (using one of 6 possible a value to put in the second unfilled blank (using one of 5 possible a value to put in the third unfilled blank (using one of 4 possible
Thus, by the multiplication principle, there are
5 . 4 6 . 5 4= 2400 ways of making all five choices. Hence 2400 of the integers in part (a) contain both of the digits 1 and 2. + The second basic counting principle is concerned with the number of elements in the union of pairwise disjoint sets.
Theorem 7.6
'^a
SD.'>
Suppose that there are k sets of elements with nI ele-
ments in the first set, n2 elements in the second set, etc. If all of the elements are distinct (that is, if all pairs of the k sets are disjoint), then the number of elements in the union of the sets is n I + n2 + * + nk. To illustrate this result, let A ={1, 2, 3} and B ={4, 5, 6, 7}. Since the elements in A and B are distinct, it follows from the addition principle that the number of elements in the union of sets A and B equals the number of elements in set A (which is 3) plus the number of elements in set B (which is 4). Therefore, in our example, the number of elements in A U B is 3 + 4 = 7. Clearly this answer is correct because A U B = {1, 2, 3, 4, 5, 6, 71. But note the necessity of distinct elements: If A had been the set A =f 1, 2, 4}, then the answer would no longer have been 7 since in this case A U B = {l, 2, 4, 5, 6, 7J. +
Example 7.11 Suppose that a couple expecting a child has decided to name it one of six names (Jennifer Ann, Jennifer Marie, Karen Ann, Karen Marie, Linda Ann, or Linda
370
Chapter 7 Counting Techniques
Marie) if it is a girl and one of f:bur names (Michael Alan, Michael Louis, Robert Alan, or Robert Louis) if it is a boy. How many different names can the child receive? Since the names to be given to a girl are different from those to be given to a boy, the addition principle states that the number of possible names is the sum of the number of girls' names and the number of the boys' names. Thus the answer to the question posed above is 6 + 4 = 10. 0
Example 7.12 How many integers between 1 and 100 (including 100) are even or end with 5? Let A denote the set of ceen integers between 1 and 100, and let B denote the set of integers between I And 100 that end with 5. The number of integers between I and 100 that are e-,en or end in 5 is then the number of elements in A U B. Now A contains 50 elements because every other number from 1 to 100 is even. And B contains 10 elements since 5, 15, 25, 35, 45, 55, 65, 75, 85, and 95 are the only integers between 1 and 100 that end in 5. Moreover, the elements in A and B are distinct because a number ending in 5 cannot be even. Thus it follows from the addition principle that the number of integers between 1 and 100 that are even or end with .5is 50 + 10 = 60. +
Often the multiplication and addition principles are both needed to solve a problem, as in the following examples.
Example 7.13 In the Applesoft BASIC langu age, the name of a real variable consists of alphanumeric characters beginning with a letter. (An alphanumeric character is a letter A-Z or a digit 0-9.) Although variable names may be as long as 238 characters, they are distinguished by their first two characters only. (Thus RATE and RATIO are regarded as the same name.) In addition, there are seven reserved words (AT, FN, GR, IF, ON, OR, and TO) that are not legal variable names. We will use the multiplication and addition principles to determine the number of legal variable names that can be distinguished in Applesoft BASIC. Clearly there are 26 real variable names consisting of a single character, namely A-Z. Any other distinct name will consist of a letter followed by an alphanumeric character. It fol cws from the multiplication principle that the number of distinguishable names con sisting of more than one character is 26. 36 = 936, since there are 26 letters and 36 alphanumeric characters. Thus, by the addition principlethenumberof one-characterortwo-characternames is26 + 936 = 962. So there are 962 distinguishable real variable names in Applesoft BASIC, and hence 962 - 7 = 955 legal names that can be distinguished. +
7.2 Three FundamentalPrinciples
+
371
Example 7.14 (a) (b)
How many 8-bit strings begin with 1011 or 01? How many 8-bit strings begin with 1011 or end with 01?
(a) An 8-bit string beginning with 1011 has the form 1011 , where the dashes denote either zeros or ones. The number of 8-bit strings that begin with 1011 is equal to the number of ways that bits five through eight can be chosen. By reasoning as in Example 7.8, we find that this number is 24 = 16. Likewise, the number of 8-bit strings that begin with 01 is 26 = 64. Since the set of strings beginning with 1011 is disjoint from the set of strings beginning with 01, the addition principle shows that the number of strings beginning with 1011 or 01 is 16 + 64 = 80. (b) Although it is tempting to approach this problem as in part (a), the set of strings beginning with 1011 is not disjoint from the set of strings ending with 01; so the correct answer is not 16 + 64 as before. Since the addition principle can only be used with disjoint sets, let us define sets of 8-bit strings A, B, and C as follows: A = {strings beginning with 1011 and not ending with 011, B = {strings ending with 01 and not beginning with 101 11, C = {strings beginning with 1011 and ending with 01}. Clearly all pairs of the sets A, B, and C are disjoint, and A U B U C consists of the strings that begin with 1011 or end with 01. Therefore the addition principle states that the number of elements in A U B U C is the sum of the sizes of A, B, and C. Now strings in A begin with 101 1 and end with 00, 10, or 11. Thus there is only one way to choose the first four bits of a string in A (namely 1011), there are two ways to choose each of the fifth and sixth bits (O or 1), and there are three ways to choose the last two bits (00, 10, or 11). Hence the number of strings in A is 1 2. 2 3 = 12. Similar arguments show that the numbers of strings in B and C are 15 2 2 1 = 60 and 1 2 2. 1 = 4, respectively. Therefore the number of strings in A U B U C is 12 + 60 + 4 = 76. + .
EXERCISES 7.2 1. How many people must there be in order to assure that at least two of their birthdays fall in the same month? 2. If a committee varies its meeting days, how many meetings must it schedule before we can guarantee that at least two meetings will be held on the same day of the week? 3. A drawer contains unsorted black, brown, blue, and gray socks. How many socks must be chosen in order to be certain of choosing two of the same color? 4. How many words must be chosen in order to assure that at least two begin with the same letter? 5. A conference room contains 8 tables and 105 chairs. What is the smallest possible number of chairs at the table having the most seats?
372
Chapter 7 Counting Techniques
6. If there are 6 sections of Discrete Math with a total enrollment of 199 students, what is the smallest possible number of students in the section with the largest enrol ment? 7. How many books must be chosen from among 24 mathematics books, 25 computer science books, 21 literature books, and 15 economic books in order to assure that there are at least 12 books on the same subject? 8. A sociologist intends to send a questionnaire to 32 whites, 19 blacks, 27 Hispanics, and 31 Native Americans. How many responses must she receive in order to guarantee that there will be at least 15 responses from the same ethnic group? 9. An automobile can be ordered with any combination of the following options: air conditioning, automatic transmission, bucket seats, rear window defogger, and CD player. In how many different ways can this car be equipped? 10. How many different pizzas can be ordered if a pizza can be selected with any combination of the following ingredients: anchovies, ham, mushrooms, olives, onion. pepperoni, and sausage? 11. Use the multiplication principle to determine the number of subsets of a set containing n elements. 12. How many different sequences of heads and tails can result if a coin is flipped 20 times? 13. A businessman must fly from Kansas City to Chicago Dn Monday and from Chicago to Boston on Thursday. If there are 8 daily flights from Kansas City to Chicago and 21 daily flights from Chicago to Boston, how many different routings are possible for this trip? 14. An interior decorator is creating layouts that consist of carpeting and draperies. If there are 4 choices of carpets and 6 choices of draperies, how many layouts must be made to show all of the possibilities? 15. Until recently, a telephone area code was a three-digit r umber that could not begin with 0 or 1 and must have 0 or 1 as its middle digit. How many such telephone area codes are possible? 16. How many different character strings of length three ca:i be formed from the letters A, B, C, D, E, and F if (a) letters can be repeated? (b) letters cannot be repeated? 17. In how many different orders can 3 married couples be seated in a row of 6 chairs under the following conditions? (a) Anyone may sit in any chair. (b) Men must occupy the first and last chairs, (c) Men must occupy the first three chairs and women the last three. (d) Everyone must be seated beside his or her spouse. 18. On student recognition night, a high school will present awards to 4 seniors and 3 juniors. In how many different orders can the awards be presented under the following conditions? (a) The awards can be presented in any order. (b) Awards are presented to juniors before awards are presented to seniors. (c) The first and last awards are presented to juniors. (d) The first and last awards are presented to seniors. 19. In the Apple Pascal programming language, identifies (that is, variable names, file names, and so forth) are subject to the following rules: (i) The first character in an identifier must be a letter (capital or lower case). (ii) Subsequent characters may be letters or digits (0, 1, . . ., 9). If the reserved words IF, LN, ON, OR, and TO cannot be used as identifiers, how many different Apple Pascal identifiers contain exactly two characters? 20. In FORTRAN, unless an integer variable is explicitly declared, its name must begin with one of the letters I, J, K, L, M, or N. Subsequent characters can be any letter A, B., Z or digit 0, 1,. 9. How many such integer variable names contain exactly four characters'?
7.2 Three FundamentalPrinciples
373
21. A men's clothing store has a sale on selected suits and blazers. If there are 30 suits and 40 blazers on sale, in how many ways may a customer select exactly one item that is on sale? 22. A restaurant offers a choice of 3 green vegetables or a potato prepared in one of 5 ways. How many different choices of vegetable can be made? 23. How many 8-bit strings begin with 1001 or 010? 24. How many 8-bit strings end with 1000 or 01011 ? 25. In the United States, radio station call letters consist of 3 or 4 letters beginning with either K or W. How many different sets of radio station call letters are possible? 26. Suppose that a license plate must contain a sequence of 2 letters followed by 4 digits or 3 letters followed by 3 digits. How many different license plates can be made? 27. From among a group of 4 men and 6 women, 3 persons are to be appointed as a branch manager in different cities. How many different appointments can be made under the following circumstances? (a) Any person is eligible for appointment. (b) One man and two women are to be appointed. (c) At least two men are to be appointed. (d) At least one person of each sex is to be appointed. 28. Suppose that 3 freshmen, 5 sophomores, and 4 juniors have been nominated to receive scholarships of $500, $250, and $100. How many different distributions of the three scholarships are possible under the following circumstances? (a) Anyone may receive any scholarship. (b) The $500 scholarship is to be awarded to a freshman, the $250 scholarship to a sophomore, and the $100 scholarship to a junior. (c) At least two scholarships are to be awarded to juniors. (d) One scholarship is to be awarded to someone from each class. 29. How many 8-bit strings begin with 11 or end with 00? 30. How many 8-bit strings begin with 010 or end with 11? 31. The digits 1-6 are to be used to make four-digit numbers. (a) How many such numbers can be made if repetition is allowed? (b) How many such numbers can be made if repetition is not allowed? (c) How many of the numbers in (b) begin with 3? (d) How many of the numbers in (b) contain 2? 32. A university task force on remedial courses is to be formed from among 3 mathematics teachers, 4 English teachers, 2 science teachers, and 2 humanities teachers. The committee must contain at least one math teacher and at least one English teacher. How many different committees can be formed if (a) two committees are considered the same when they contain precisely the same individuals? (b) two committees are considered the same when they contain the same number of teachers from each discipline? 33. Show that if the 26 letters of the English alphabet are written in a circular array in any order whatsoever, there
must be 5 consecutive consonants. 34. Prove that in any nonempty list of n integers (not necessarily distinct) there is some nonempty sublist having a sum that is divisible by n. 35. Let S = {a, a2, . . ., a9 1 be any set of 9 points in Euclidean space such that all three coordinates of each point are integers. Prove that for some i and j (i :A j) the midpoint of the segment joining ai and aj has only integer coordinates.
374
Chapter 7 Counting Techniques
36. Suppose that there are 15 identical copies of The Great Gatsby and 12 distinct biographies on a bookshelf. (a) How many different selections of 12 books are possible? (b) How many different selections of 10 books are possible?
7.3
c
PERMUTATIONS AND COMBINATIONS Two types of counting problems occur so frequently that they deserve special attention. These problems are: (1) (2)
How many different arrangements (ordered lists) of r objects can be formed from a set of n distinct objects? How many different selections (unordered lists) of r objects can be made from a set of n distinct objects?
In this section we will consider these two questions in the case that repetition of the n distinct objects is not allowed. Section 7.4 will answer these same questions when repetition is permitted. Recall from Section 1.2 that an arrangement or ordering of n distinct objects is called a permutation of the objects. If r < n, then the arrangement or ordering using r of the n distinct objects is called an r-permutation. Thus, 3142 is a permutation of the digits 1, 2, 3, and 4, and 412 is a 3-permutation of these digits. The number of different r -permutations of a set of n distinct elements is denoted P(n, r). In Theorem 1 2 this number was found to be n! 15(n, r) = (n r)!
(7.2)
Thus (7.2) gives us the answer to question 1 above. 4
Example 7.15 How many different three-digit numbers can be formed using the digits 5, 6, 7, 8, and 9 without repetition. This question asks for the number of 3-permutations from a set of 5 digits. This number is P(5, 3). So, using (7.2), we see that the answer to the question above is P(5 3)
5! 2!
5 4 .3 2!
2!
543=60
+
+~ Example 7.16 In how many different orders can 4 persons be seated in a row of 4 chairs?
7.3 Permutationsand Combinations
375
The answer to this question is the number of permutations of a set of 4 elements. Recalling that O! = 1, we see from (7.2) that this number is 4! 0!
4! 1
P(4,4)= 0 = - =4! =24. + Note that Example 7.16 could also have been answered by appealing to Theorem 1. 1, which can be rewritten using our present notation as P (n, n) = n!. Let us now consider the second question above. If r < n, then an unordered selection of r objects chosen from a set of n distinct objects is called an recombination of the objects. Thus {1, 41 and {2, 3} are both 2-combinations of the digits 1, 2, 3, and 4. Note that since combinations are unordered selections, the 2-combinations {1, 4} and {4, 11 are the same. In fact, an unordered selection of r elements from a set of n distinct elements is just a subset of the set that contains r elements. Thus the number of different r-combinations of a set of n distinct elements is C(n, r). So, using (7.1), we see that the answer to question 2 above is C(n,r) = ! (
o
)!
Example 7.17 How many different 4-member committees can be formed from a delegation of 7 members? Since a 4-member committee is just a selection of 4 members from the delegation of 7, the answer to this question is C(7, 4). Using (7.1), we find that C (7, 4)=
+
7! 4!3!
=
7.6.5.4! =~ 4!.3. 2- 1
7*6 5 3.2.1
= 35.c o
Example 7.18 How many 8-bit strings contain exactly three Os? Note that an 8-bit string containing exactly three Os is completely determined if we know the positions of the three Os (since the other five positions must be filled with 1s). Thus the number of 8-bit strings containing exactly three Os equals the number of different locations that the three Os can occupy. But this number is the number of ways to choose three positions from among eight, which is C (8, 3). So the number of 8-bit strings containing exactly three Os is C(8,3)=
8! 8.7.6.5! 5! 3!5! 3i2.1.5!
8.7.6 = 3.2.1
It is clear from (7. 1) and (7.2) that P(n, r) = r! C(n, r).
56. +
376
Chapter 7 Counting Techniques
This equation can be interpreted combinatorially as follows: The number of ways to arranger objects from a set of n objects equals the number of ways to select r of the n objects and then arri6nge the selected objects in order. Many counting problems require distinguishing permutations from combinations. Since permutations are ordered lists, they arise in problems where the order of selection is significant, such as when the selected objects are to be treated differently. Combinations, on the other hand, are unordered lists and occur when the order of selection is irrelevant, such as when the selected objects are treated the same. Note the use of permutations and combinations in the following example.
Example 7.19 An investor is going to invest $16,000 in 4 stocks chosen from a list of 12 prepared by her broker. How many different investments are possible if (a) (b)
$4000 is to be invested in each stock? $6000 is to be invested in one stock, $5000 in another, $3000 in the third, and $2000 in the fourth?
(a) Since each stock is to be treated the same, we need an unordered list of 4 stocks. Hence the number of investments in this case is 12! 12 11 10 9 C(12, 4,) =:--~ =495. 4! 8! 4 3 2 1 (b) Since each stock is to be treated differently, we need an ordered list of 4 stocks. Hence the number of investments is this case is P(12, 4) =
= 12 128!112!9 = 11,880.
Counting problems often require that permutations or combinations be used together with the multiplication or addition principles. The following examples are of this type. Example 7.20 Three men and three women are going to occupy a row of six seats. In how many different arrangements can they be seated so that men occupy the two end seats? We can regard the assigning of seats as a two-step process: First fill the two end seats, and then fill the middle four seats. Since the end seats must be filled by two of the three men, there are P (3, 2) different ways to occupy the end seats. The remaining four persons can fill ihe middle seats in any order, so there are P(4, 4) different ways to fill the middle seats. Thus, by the multiplication principle, the number of ways to fill both the end seats and the middle seats is P(3, 2) P(4,4)=6 24= 144.
d
7.3 Permutationsand Combinations
+
377
Example 7.21 An investor is going to purchase shares of 4 stocks chosen from a list of 12 prepared by her broker. How many different investments are possible if $5000 is to be invested in each of two stocks and $3000 in each of the others? We can regard the choice of stocks as a two-step process by first choosing the stocks in which $5000 is to be invested and then choosing the stocks in which $3000 is to be invested. Clearly the stocks in which $5000 is to be invested can be chosen in C(12, 2) ways. The stocks in which $3000 is to be invested must be chosen from the remaining 10 stocks, and so this choice can be made in C(10, 2) ways. The multiplication principle now gives the number of different investments to be C(12, 2) C(10, 2) = 66 45 = 2970.
i)
.
+l Example 7.22 From among a group of 6 men and 9 women, how many three-member committees contain only men or only women? The number of three-member committees containing only men is C (6, 3), and the number of three-member committees containing only women is C (9, 3). Since the set of committees containing only men is disjoint from the set of committees containing only women, the addition principle shows that the number of threemember committees containing only men or only women is C(6, 3) + C(9, 3) = 20 + 84 = 104. i
i
Example 7.23 How many 8-bit strings contain six or more Is? If an 8-bit string contains six or more I s, then the number of I s that it contains must be six, seven, or eight. Reasoning as in Example 7.18, we see that the number of strings containing exactly six Is is C(8, 6), the number of strings containing exactly seven Is is C(8, 7), and the number of strings containing exactly eight Is is C(8, 8). So the addition principle shows that the number of 8-bit strings containing six or more is is C(8,6)+C(8,7)+C(8,8)=28+8+I =37.
i)
+
Example 7.24 How many 8-bit strings with exactly two Is are such that the Is are not adjacent? If an 8-bit string contains exactly two Is, then it must also contain exactly six Os. We will consider two cases, according to whether the last bit is 0 or 1.
378
Chapter 7 Counting Techniques
If the last bit is 0 and the tvwo Is are not adjacent, then each 1 is followed by at least one 0. Hence we can regard the bits to be arranged as two strings of 10 and four single Os. The number of ways to arrange these six groups is the number of ways to choose positions for the four Os from six locations, which is C(6, 4). On the other hand, if the last digit is 1 and the two is are not adjacent, then we must arrange five Os and one string of 10. (The other 1 is reserved for the last bit.) The number of such arrangements is the number of ways to choose positions for the five Os from six locations, which is C(6, 5). Hence, by the addition principle, the number of 8-bil strings with exactly two nonadjacent Is is C(6.L )+C(6,5)= 15+6=21.
+
EXERCISES 7.3 Evaluate the numbers in Exercises 1-12. 1. C(6, 3) 5. P(4,2) 9. P(10,4) 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24.
25. 26.
2. C(7, 4) 6. P(6,3) 10. P(8, 3)
3. C(5, 2) 7,P(9,5) 11. P(n, 1)
4. C(8, 4) 8. P(12,3) 12. P(n,2)
How many different arrangements are there of the letters a, b, c, and d? How many different arrangements are there of the letters in the word 'number'? How many different four-digit numbers can be fotmn d using the digits 1, 2, 3, 4, 5, and 6 without repetition? How many different ways are there of selecting five persons from a group of seven persons and seating them in a row of five chairs? How many different 3-member subcommittees can bc formed from a committee with 13 members? How many different 16-bit strings contain exactly foar Is? How many different subsets of {1, 2, . . ., 101 contain exactly six elements? How many different 4-person delegations can be fbnn-d from a group of 12 people? Five speakers are scheduled to address a convention. In how many different orders can they appear? Six persons are running for four seats on a town council. In how many different ways can these four seats be filled? For marketing purposes, a manufacturer wants to tesW new product in three areas. If there are nine geographic areas in which to test market the product, in how many different ways can the test areas be selected? An investor intends to buy shares of stock in 3 companies chosen from a list of 12 companies recommended by her broker. How many different investment option are there under the following circumstances? (a) Equal amounts will be invested in each company. (b) Amounts of $5000, $3000, and $1000 will be invested in the chosen companies. How many different committees consisting of three representatives of management and two representatives of labor can be formed from among six representatives cf management and five representatives of labor? In how many different sequences can we list 4 novels followed by 6 biographies if there are 8 novels and 10 biographies from which to choose?
7.4 Arrangements and Selections with Repetitions
379
27. An election will be held to fill three faculty seats and two student seats on a certain college committee. The faculty member receiving the most votes will receive a three-year term, the one receiving the second highest total will receive a two-year term, and the one receiving the third highest total will receive a one-year term. Both of the open student seats are for one-year terms. If there are nine faculty members and seven students on the ballot, how many different election results are possible assuming that ties do not occur? 28. In how many different ways can 8 women be paired with 8 of 12 men at a dance? 29. Suppose that 3 freshmen, 4 sophomores, 2 juniors, and 3 seniors are candidates for four identical school service awards. In how many ways can the recipients be selected under the following conditions? (a) Any candidate may receive any award. (b) Only juniors and seniors receive awards. (c) One person from each class receives an award. (d) One freshman, two sophomores, and one senior receive awards. 30. Suppose that 3 freshmen, 5 sophomores, 4 juniors, and 2 seniors have been nominated to serve on a student advisory committee. How many different committees can be formed under the following circumstances? (a) The committee is to consist of any four persons. (b) The committee is to consist of one freshman, one sophomore, one junior, and one senior. (c) The committee is to consist of two persons: one freshman or sophomore and one junior or senior. (d) The committee is to consist of three persons from different classes. 31. Prove by a combinatorial argument that 2C(n, 2) + n2
=
33. Prove that C(n, m) . C(m, k) = C(n, k) *C(n 34. Prove that C(n, 0)2 + C(n, 1)2 + . + C(n, n)2 35. Prove that C(1, 1) + C(2, 1) + argument.
*
C(2n, 2) for n > 2.
1, r -1) for 1 < r < n. k, m -k) for k < m < n by a combinatorial argument.
32. Prove by a combinatorial argument that rC(n, r) = nC(n =
-
C(2n, n) for every positive integer n.
+ C(n, 1) = C(n + 1, 2) for every positive integer n by a combinatorial
36. State and prove a generalization of Exercise 35.
7.4
+
ARRANGEMENTS AND SELECTIONS WITH REPETITIONS In this section we will learn how to count the number of arrangements of a collection that includes repeated objects and the number of selections from a set when elements can be chosen more than once. As we will see, both of these counting problems require the use of ideas from the two preceding sections. Let us consider first the number of arrangements of a collection containing repeated indistinguishable objects. As a simple example of this type of problem, we will count the number of different arrangements of the letters in the word 'egg.' Since there are only three letters in 'egg,' it is not difficult to list all of the possible arrangements. These are: egg
geg
gge.
380
Chapter 7 Counting Techniques
Hence there are only 3 arrangements of the letters in 'egg' compared to the P (3, 3) = 6 arrangements that we would expect if all the letters had been distinct. To see more clearly the effect of the repeated letters, let us capitalize the first 'g' in 'egg' and regard a capital letter as different from a lowercase letter. Then the six possible arrangements of the letters in 'eGg' are: eGg egG
Geg geG
Gge gGe
Note that because the two g s in the first list are identical, interchanging their positions does not produce different arrangements. But each arrangement in the first list gives rise to two arrangements in the second list, one with 'G' preceding 'g' and the other with 'g' preceding 'G.' Thus the number of arrangements in the first list equals the number of arrangements in the second list divided by P(2, 2) = 2, the number of permutations of the two g's. Another way to count the number of arrangements of the letters in 'egg' is by thinking of an arrangement as having 3 positions and first choosing positions for the two g's and then choosing a position for the 'e.' Since the positions for the g's can be chosen in C 3, 2) ways and the remaining position for the 'e' can be chosen in only C(1, 1) way, the multiplication principle then gives the number of possible arrangements as C(3, 2). C(1, 1) = 3. 1 = 3. This analysis and the one in the preceding paragraph lead to the same answer (see Exercise 35), which demonstrates the following result. Theorem 7.7
Let S be a collection containing n objects of k different types. (Objects of the same type are indistinguishable, and objects of different types are distinguishable.) Suppose that each object is cf exactly one type and that there are n, objects of type 1, n2 objects of type 2, and, in general, ni objects of type i. Then the number of different arrangements of th- objects in S is C(n, nI) C(n -
ni, n 2 )
C(n -- n -n
2 , n3 )
.. C(n-n I
2-
-
nkk-
1,nk),
which equals
n! nl!n2! . nk! The conclusion of this theorem states that the number of different arrangements of the objects in S equals the number of ways C(n, nI) to place the n, objects of type 1 in n possible locations, times the number of ways C(n - n], n 2 ) to place the n2 objects of type 2 inn - n, unused locations, times the number of ways C(n - n1 - n2, n 3 ) to place the n3 objects of type 3 inn - n -n 2 unused locations, etc. Note that this number can also be written in the form n !
7.4 Arrangements and Selections with Repetitions
381
Also note that n = nI + n2 + * + nk because we are assuming that each of the n elements in S belongs to exactly one of the k types. . .
4
Example 7.25 How many arrangements are there of the letters in the word 'banana'? Since 'banana' is a six-letter word consisting of three types of letters (1 b, 3 a's, and 2 n's), the number of arrangements of its letters is 6! 1! 3! 2!
+
6 .5 4 3! 1 3! 2
6 .5. 4 2
Example 7.26 Each member of a nine-member committee must be assigned to exactly one of three subcommittees (the executive subcommittee, the finance subcommittee, or the rules committee). If these subcommittees are to contain 3, 4, and 2 members, respectively, how many different subcommittee appointments can be made? Let us arrange the nine persons in alphabetical order and give each person a slip of paper containing the name of a subcommittee. Then the number of possible subcommittee appointments is the same as the number of arrangements of 9 slips of paper, 3 of which read 'executive subcommittee,' 4 of which read 'finance subcommittee,' and 2 of which read 'rules subcommittee.' By Theorem 7.7, this number is 9! = 1260. + 3! 4! 2! Let us now consider the problem of counting the number of selections from a set when elements can be chosen more than once. As an example, suppose that seven persons in a hotel conference room call for refreshments from room service. If the choice of refreshments is limited to coffee, tea, or milk, how many different selections of seven refreshments are possible? Note that we are asking this question from the room service's point of view; that is, we are not interested in knowing who wants which beverage but only in the total number of beverages of each type that are desired. For example, one such selection is for 4 coffees, 1 tea, and 2 milks. Thus we are selecting seven times from {coffee, tea, milk} with repetition allowed. To answer this question, we will suppose that one of the seven persons in the room asks everyone which beverage he or she would like. In order to keep track of the answers, the responses are recorded on a tally sheet as shown below. Note that we need only two lines to divide our tally sheet into three columns. Coffee
Tea
Milk
382
Chapter 7
Counting Techniques
For example, an order for 4 coffees, I tea, and 2 milks would be recorded as follows. Coffee xxxx
Tea x
Milk xx
If we always list the beverages in the sequence above, the beverage names can be omitted from the tally sheet because every order corresponds uniquely to some arrangement of seven x's and two I's. For example, the order for 4 coffees, 1 tea, and 2 milks would be represented as xxxxIx lxx, and an order for 5 coffees, 2 teas, and 0 milks would appear as xxxxxlxxl. Hence the number of different refreshment orders is the same as the number of ways to arrange seven x's and two l's, or equivalently, the number of ways to choose positions for seven x's from nine possible locations. Thus there are C(9, 7) = 36 different refreshment orders possible. (Since C(9, 2) = C(9, 7) = 36, we can also interpret the number of different refreshment orders as the number of ways to choose positions for two I's from among nine positions.) By using the same type ofi easoning as above, we obtain the following result. Note that in this theorem s denotes the number of selections and t denotes the number of types of objects from which to choose. (In the beverage example, s = 7 and t = 3.) Theorem 7.8
+
If repetition is allowed, the number of selections of s elements that can be made from a set containing t distinct elements is C(s + t - 1, s). Example 7.27 Suppose that we take five coins from a piggy bank containing many pennies, nickels, and quarters. How many different amounts of money might we get? Note that because we are selecting five coins, each possible choice of coins corresponds to a different amount of money. (This would not be the case if we selected six coins, for 1 quarter and 5 pennies have the same value as 6 nickels.) Thus, by Theorem 7.8, the answer to this question is C(5 + 3 - 1, 5) = C(7, 5) = 21.
4
o
Example 7.28 A bakery makes four different types of donuts. (a) (b)
How many different assortments of one dozen donuts can be purchased? How many different assortments of one dozen donuts can be purchased that include at least one donut of each type?
7.4 Arrangements and Selections with Repetitions
383
(a) Since we are selecting 12 donuts from 4 types with repetition of the types allowed, we use Theorem 7.8 with s = 12 and t = 4. The number of possible choices is C(s + t
-
1, s) = C(12 + 4 - 1, 12) = C(15, 12) = 455.
(b) Because at least one donut of each type must be included, let us begin by choosing one donut of each type. The number of possible assortments is then the number of different ways the remaining 8 donuts can be selected. As in (a), this number is C(s + t - 1, s) = C(8 +4-
+
1,8) = C(1, 8) = 165.
+
Example 7.29 How many 8-bit strings with exactly two Is are such that the Is are not adjacent? The strings to be counted consist of two is and six Os. Arrange the two Is in a line. In order that the Is not be adjacent, we insert a 0 between them. The present configuration is shown below. 1 0
1
The string will be completely determined if we know the numbers of Os before the first 1, between the two Is, and after the second 1. Thus the number of different strings with the desired form is equal to the number of different ways to place the remaining five Os into three positions. But the number of different ways to place the remaining five Os into three positions equals the number of ways to choose 5 times with repetition from among 3 types of positions, which is C(5 + 3 - 1,5) = C(7,5) = 21. Compare this solution to that in Example 7.24.
+
Counting problems involving the distribution of objects can be interpreted as problems involving arrangements or selection with repetition. Usually problems involving the distribution of distinct objects correspond to arrangements with repetition,andproblems involving the distributionof identicalobjects correspond to selections with repetition. The following examples demonstrate the use of Theorems 7.7 and 7.8 in solving problems involving distributions. +
Example 7.30 How many distributions of 10 different books are possible if Carlos is to receive 5 books, Doris is to receive 3 books, and Earl is to receive 2 books?
384
Chapter 7 Counting Techniques
Distributing the 10 books,: s equivalent to lining them up in some order and inserting a piece of paper in eac i book marked with the recipient's name. Then the number of possible distributions is the same as the number of ways of arranging 5 slips of paper marked 'Carlos,' 3 slips marked 'Doris,' and 2 slips marked 'Earl.' Using Theorem 7.7, we see that this number is 10! 5! 3! 2!
=2520.
Note the similarity between this solution and that of Example 7.26. +
+
Example 7.31 If 9 red balloons and 6 blue balloons are to be distributed to 4 children, how many distributions are possible if every child must receive a balloon of each color? Let us distribute the red balloons first and the blue balloons second. Since every child must receive a ied balloon, we give one to each child. Now we can distribute the remaining 5 red balloons in any way whatsoever. To decide who will receive each of these 5 balloons, we will think of selecting five times with repetition from a set containing the children's names. The number of possible selections is given by Th-orem 7.8 to be C(5 + 4 - 1, 5) = C(8, 5). Similar reasoning shows that the number of ways in which the blue balloons can be distributed so that every child receives at least one is C(2 + 4 - 1, 2) = C(5, 2). Thus, by the multiple cation principle, the number of possible distributions of the balloons in which every child receives a balloon of each color is
C(8, 5)* C (5, 2)= 56 10 = 560. 4 In Section 7.3 we posed Iwo basic counting problems: (1) (2)
How many different arrangements (ordered lists) of r objects can be formed from a set of n distinct objects? How many different selections (unordered lists) of r objects can be formed from a set of n distinct objects?
Theorem 7.8 provides the answer to question (2) when repetition of items is permitted. The answer to question (1) in this case follows easily from the multiplication principle, for there are r objects to be chosen, and each object can be chosen in one of n ways. Fence the total number of arrangements of r objects that can be formed from a set of n distinct objects when repetition of items is permitted is n n r factors
nn=n'.
7.4 Arrangements and Selections with Repetitions
385
The following chart summarizes the answers to the two questions stated above. Number of Arrangements
Number of Selections
(ordered lists)
(unorderedlists)
Repetition of items not permitted
P(n, r)
C(n, r)
Repetition of items permitted
n'
C(n + r -1, r)
Note that, in this context, Theorem 7.7 gives the number of arrangements
with repetition when the number of items of each type is specified. EXERCISES 7.4 1. How many distinct arrangements of the letters in 'redbird' are there? 2. How many distinct arrangements of the letters in 'economic' are there? 3. How many different 7-digit numbers can be formed using the digits in the number 5,363,565? 4. How many different 9-digit numbers can be formed using the digits in the number 277,728,788? 5. How many different fruit baskets containing 8 pieces of fruit can be formed using only apples, oranges, and pears? 6. How many different assortments of 6 boxes of cereal can be made using packages of corn flakes, shredded wheat, and bran flakes? 7. How many different assortments of one dozen donuts can be purchased from a bakery that makes donuts with chocolate, vanilla, cinnamon, powdered sugar, and glazed icing? 8. How many different boxes containing 10 wedges of cheese can be made using wedges of Cheddar, Edam, Gouda, and Swiss cheese? 9. A box contains 16 crayons, no two having the same color. In how many different ways can they be given to four children so that each child receives 4 crayons? 10. In how many different ways can 15 distinct books be distributed so that Carol receives 6, Don receives 4, and Ellen receives 5? 11. A committee's chairperson and secretary must telephone the other 7 members about a change in the committee's meeting time. In how many different ways can these telephone calls be made if the chairperson calls 3 people and the secretary calls 4? 12. Paula has bought 6 different CDs to give as Christmas gifts. In how many different ways can she distribute the CDs so that each of her three boyfriends receives 2 CDs? 13. In how many different ways can 8 identical pieces of construction paper be distributed to 4 children? 14. In how many different ways can 10 identical quarters be distributed to 5 people? 15. In how many different ways can 6 identical sticks of white chalk be distributed to 3 students so that each student receives at least one stick? 16. A father has 10 identical life insurance policies. He wants to name one of his 3 children as the beneficiary of each policy. In how many different ways can the beneficiaries be chosen if each child is to be named a beneficiary on at least 2 policies?
386
Chapter 7
Counting Techniques
17. A concert pianist is preparing a recital that will consist of I Baroque piece, 3 classical pieces, and 3 romantic pieces. Assuming for the sake of programming that pie,-es of the same period are regarded as indistinguishable, how many different programs containing the 7 pieces can the pianist create? 18. In bridge a deal consists of distributing a 52-card deck into four 13-card hands. How many different deals are possible in bridge? 19. In how many different ways can 8 identical mathematics books and 10 identical computer science books be distributed among 6 students? 20. Twelve children are to be divided into groups of three lo play different number games. In how many ways can the groups be chosen? 21. Ten diplomats are awaiting assignments to foreign embassies. If 3 of these diplomats are to be assigned to England, 4 to France, and 3 to Germany, in how many ways can the assignments be made? 22. In order to stagger the terms of service of 12 people elected to a new committee, 4 members are to be assigned a one-year term, 4 members are to be assigned a two-year term, and 4 members are to be assigned a three-year term. In how many different ways can these assignment ts be made? 23. How many 16-bit strings are there containing six Os and ten Is with no consecutive Os? 24. How many positive integer solutions are there to the equation x + y + z = 17? 25. In how many ways can 2 identical teddy bears and 7 distinct Cabbage Patch dolls be distributed to 3 children so that each child receives 3 gifts? (It is possible for she 3 gifts to include both teddy bears.) 26. How many numbers greater than 50,000,000 can be fonned by rearranging the digits of the number 13,979,397? 27. How many positive integers less than 10,000 are such :hat the sum of their digits is 8? 28. How many distinct arrangements are there of two a's, one e, one i, one o, and seven x's in which no two vowels are adjacent? 29. How many positive integers less than 1,000,000 are such that the sum of their digits equals 12? 30. A domino contains two indistinguishable squares, eacL of which is marked with 0, 1, 2, 3, 4, 5, or 6 dots. How many different dominoes are possible? 31. In the following segment of a computer program, how many times is the PRINT statement executed?
FOR I: = 1 'ro 1o FOR J: = I TO I FOR, K: = I TO J PRINT I, J, K NEX T K NEXT J NEXT I 32. A pouch contains $1 in pennies, $1 in nickels, and $1 in dimes. In how many different ways can 12 coins be selected from this pouch? (Assume that all coins of the same value are indistinguishable.) 33. A pinochle deck consists of two each of 24 different cards. How many different 12-card pinochle hands are possible? 34. If m > n, how many different ways are there to distribute m indistinguishable balls into n distinguishable urns with no urn left empty? 35. Prove that the two expressions in Theorem 7.7 are equal. 36. Use Exercise 31 of Section 7.1 to prove Theorem 7.8 by induction on t. ego
7.5 Probability
7.5
+
387
PROBABILITY The subject of probability is generally accepted as having begun in 1654 with an exchange of letters between the great French mathematicians Blaise Pascal and Pierre de Fermat. During the next 200 years, probability was combined with statistics to form a unified theory of mathematical statistics, and it is in this context that any thorough discussion of probability must occur. Nevertheless, the history of probability is closely related to the history of combinatorics, the branch of mathematics concerned with counting. In this section we will discuss probability as an application of the combinatorial ideas presented in Sections 7.2, 7.3, and 7.4. Intuitively, probability measures how likely something is to occur. In his important book Theorie Analytique des Probabilites,the French mathematician Pierre Simon de Laplace (1749-1827) defined probability as follows: Probability is the ratio of the number of favorable cases to the total number of cases, assuming that all of the various cases are equally possible. Thus, according to Laplace's definition, probability measures the frequency with which a favorable case occurs. In this book we will study probability only in situations where this definition applies. Note that this definition requires that we know the number of favorable cases and the total number of cases and, therefore, requires the use of counting techniques. By an experiment we will mean any procedure that results in an observable outcome. Thus we may speak of the experiment of flipping a coin (and observing if it falls heads or tails) or the experiment of tossing a die (and noting the number of spots that show). A set consisting of all the possible outcomes of an experiment is called a sample space for the experiment. It is important to realize that there may be many possible sample spaces for an experiment. In the experiment of tossing an evenly balanced die, for instance, three possible sample spaces are {I, 2, 3, 4, 5, 6},
{even, odd),
and
{perfect square, not a perfect square}.
Which of these sample spaces may be most useful depends on the particular type of outcomes that we wish to consider. But in order to use Laplace's definition of probability, we must be certain that the outcomes in the sample space are all equally likely to occur. This is the case for the outcomes in the first two sample spaces above, but the outcomes in the third sample space are not equally likely since there are only two perfect squares among the numbers I through 6 (namely, I and 4). Thus the sample space {perfect square, not a perfect square) will not prove useful for computing probabilities. Any subset of a sample space is called an event. Thus, in the die-tossing experiment with sample space {1, 2, 3, 4, 5, 6), the following sets are events: A =1,2,4,6},
B={n: nisanintegerand4<><>
and
C = {n: n is an even positive integer less than 7}. Recall that the number of elements in a finite set X is denoted IXI. For any event E in a finite sample space S consisting of equally likely outcomes, we define
388
Chapter 7
Counting Techniques
the probability of E, denoted P(E), by
P(E) = ASi
(7.3)
Is'
So for the events A, B, and C above, we have P(A)= and P(C) =-
P(B) =
Example 7.32 In the experiment of flipping a properly balanced coin three times, what is the probability of obtaining exactly two heads? Since each flip of the coin has two possible results, heads (H) or tails (T), the multiplication principle shovws that there are 2. 2 2 = 8 possible outcomes for three flips. The set S = (HHH, HIFT, HTH, HTT, THH, THT, TTH, TTT} is a sample space for this experiment consisting of equally likely outcomes. The event of obtaining exactly two heads is the set E = {HHT, HTH, THH). Thus the probability of obtaining exactly two heads is
P(E) =
-- = 83
SI
8
In Example 7.32, we obtained the desired probability by listing the outcomes in a sample space of equally likely outcomes. Usually, however, a sample space will be so large that we must use counting techniques to determine its size. Examples 7.33 through 7.37 are of this type. Notice that, in these examples, we begin by determining the size of the sample space before counting the outcomes in the event of interest.
Example 7.33 Suppose that there are six applicants for a particularjob, fourmen and two women, who are to be interviewed in a random order. What is the probability that the four men are interviewed before either woman? To answer this question, we must decide on an appropriate sample space consisting of equally likely outcomes. Since the ordering of the interviews is important, the set S of all possible arrangements of the six interviews is the obvious choice. Let E denote the subset of S in which the men are interviewed before the women. Then the multiplication principle shows that the number of elements in E equals the number of arrangements of the men times the number
7.5 Probability
389
of arrangements of the women. So, by (7.3), we have
+
IEI P
P(4, 4). P(2, 2)
24 2
1
151
P(6, 6)
720
15
Example 7.34 Suppose that there are two defective pens in a box of 12 pens. If we choose 3 pens at random, what is the probability that we do not select a defective pen? In this problem, the set of all selections of 3 pens chosen from among the 12 in the box will be our sample space S. The set of all selections of 3 pens chosen from among the 10 nondefective pens is the event E in which we are interested. Thus, by (7.3), we find that P(E) = E
S1
+
C(10, 3) C(12, 3)
_
120 220
6 11
+
Example 7.35 What is the probability that a randomly chosen permutation of the letters in the word 'computer' has no adjacent vowels? Let the sample space be the set S of all permutations of the letters in the word 'computer,' and let E denote the subset of all such permutations in which no two vowels are adjacent. To count the permutations in E, we first arrange the five consonants in one of P(5, 5) = 120 ways, say _p -t-c
-r-m.
Since no two vowels are adjacent, we must insert at most one vowel in each blank above. The number of ways to choose positions for the three vowels is, therefore, C(6, 3) = 20. Finally we arrange the vowels in the chosen positions in P(3, 3) = 6 ways. Thus E contains 120(20)(6) = 14,400 permutations, and so IElI P(E) =I=
14,400 P(8, 8)
-
14,400 = 5 40,320 14
Example 7.36 Suppose that we have 10 differentnovels, five by Hemingway and five by Faulkner, that we want to distribute so that Barbara receives 5, Cathy receives 2, and Danielle receives 3. If the individual novels are distributed at random, what is the probability that Barbara receives all five of the novels by Hemingway? Here the sample space S is the set of all distributions of 5 novels to Barbara, 2 to Cathy, and 3 to Danielle, and the event of interest is the set E of all such distributions in which Barbara receives all the Hemingway novels. Note that the
390
Chapter 7
Counting Techniques
distributions in which Barbaia receives all the Hemingway novels are just those in which the Faulkner novels ure distributed so that Cathy receives 2 and Danielle receives 3. So, by reasoning as in Example 7.30, we see that
P(E)-
l
11i5 (2 -
+
5! )
) (5! 2! 3! 10!
5-5
10!
1 1 .
252
Example 7.37 We will compute the probability of being dealt each of the following hands if 5 cards are dealt from an ordinary 52-card deck: (a) (b)
a flush (5 cards of the same suit), and a full house (3 cards of one denomination and 2 of another denomination).
In each case, the sample space S consists of all possible five-card hands. The number of these is ('(52, 5) = 2,598,960. (a) We will count the number of different flushes. To obtain a flush, we must first choose a suit and then select 5 cards from that suit. Hence the multiplication principle shows that the numbe r of different flushes is C(4, 1). C(13, 5) = 4(1287) = 5148. It follows that the probability of being dealt a flush is 5148 2,598,960
.00198.
(b) As in (a), we will count the number of possible full houses. To obtain a full house, we must choose a denomination, pick 3 cards of that denomination, select a different denomination, and pick 2 cards of that denomination. The number of possible full houses is C(13, 1). C(4, 3). C(12, 1). C(4, 2) = 13. 4 12 6 = 3744. Hence the probability of being; dealt a full house is 3744 2,598,960 ______
~.00 144.
Since the probability of obtaining a full house is less than the probability of obtaining a flush, a full house ranks higher than a flush in poker. + Because our definition of probability requires that the sample space consist of equally likely outcomes, care must be taken when using Theorem 7.8. For
7.5 Probability
391
example, suppose that six identical cookies are to be distributed at random to three children. What is the probability that each child gets exactly two? In this problem we must consider what 'at random' means. Presumably the first cookie is given to one of the children, with each child equally likely to get it, then the second cookie, etc. Thus the sample space S consists of all 6-element lists with entries chosen from the set {1, 2, 31. For example, the list 2, 1, 3, 3, 3, 1 corresponds to giving the first cookie to child 2, the second cookie to child 1, etc. By the multiplication principle, ISI = 36. The event E consists of all rearrangements of the list 1, 1, 2, 2, 3, 3; so
E =6!
=90
IE = 2! 2! 2! =90 Thus the probability that each child receives two cookies is 90 10 P(E)= P()=36 =81 Notice that the above analysis treats the six cookies individually (first cookie, second cookie, . . . ) despite their being identical. The wrong answer is obtained if the sample space S is taken to be all ways of dividing 6 indistinguishable objects into 3 sets C 1, C2 , and C3 . Then ISI = C(6 + 3 - 1, 6) = 28 by Theorem 7.8, and IEl = 1, so that JEl
1
ISI
28
The reason that this quotient is not P(E) is that the elements of the sample space are not equally likely. For instance, a distribution in which each child receives two cookies is 90 times more likely than that the first child gets all six cookies.
EXERCISES 7.5 1. 2. 3. 4. 5. 6. 7. 8. 9.
In the experiment of rolling a die, what is the probability of rolling a number greater than 1? In the experiment of rolling a die, what is the probability of rolling a number divisible by 3? If a coin is tossed five times, what is the probability that it will land heads each time? If three dice are rolled, what is the probability that a 1 will appear on each die? If a pair of dice is rolled, what is the probability that the sum of the spots that appear is 11? If four coins are tossed, what is the probability that all of them land with the same side up? If five coins are tossed, what is the probability that exactly three of them land tails? If a coin is tossed 8 times, what is the probability that it will land heads exactly 4 times? If 3 persons are chosen at random from a set of 5 men and 6 women, what is the probability that 3 women are chosen? 10. Suppose that a 4-digit number is created using the digits 1, 2, 3, 4, and 5 as often as desired. What is the probability that it contains two Is and two 4s? 11. In a 7-horse race, a bettor bet the trifecta, which requires that the first three horses be identified in order of their finish. What is the probability of winning the trifecta under these conditions by randomly guessing three horses?
392
Chapter 7 Counting Techniques
12. If 4 persons are chosen at random from a class containing 8 freshmen and 12 sophomores, what is the probability that 4 freshmen are chosen? 13. What is the probability that a randomly chosen four-cligit number contains no repeated digits? 14. What is the probability that a randomly chosen string cf three letters contains no repeated letters? 15. If the letters of 'sassafras' are randomly permuted, w -at is the probability that the four s's are adjacent and the three a's are adjacent? 16. In a consumer preferences test, 10 people were asked to name their favorite fruit from among apples, bananas, and oranges. If each person named a fruit at random, wl at would be the probability that no one named bananas? 17. If the personnel files of 5 employees are randomly selected, what is the probability that they are chosen in order of increasing salary? (Assume that no two employees have the same salary.) 18. In a particular group of people, 10 are right-handed ind 4 are left-handed. If 5 of these people are chosen at random, what is the probability that exactly I left-hancled person is selected? 19. What is the probability that a randomly chosen subset of {I, 2, 3, 4, 5, 6} contains both 3 and 5? 20. A committee of 5 is to be formed from among 2 mathematics teachers, 2 English teachers, 2 science teachers, and 2 humanities teachers. If all such committees are equally likely, what is the probability that the committee contains at least 1 English teacher? 21. Thirteen sticks of chewing gum are to be given at random to 3 children. What is the probability that each child receives at least 4 sticks of gum? 22. Three $10 bills, four $5 bills, and six $1 bills are randc mly arranged in a stack. What is the probability that all of the $5 bills are adjacent? 23. If a 5-member committee is selected at random frorr among 7 faculty and 6 students, what is the probability that it contains exactly 3 faculty and 2 students? 24. Suppose that we randomly distribute 5 distinct Cabbage Patch dolls and 3 identical teddy bears to 4 children. What is the probability that each child receives 2 gifts' 25. In a small garden, there is a row of 8 tomato plants, 3 of which are diseased. Assuming that the disease occurs at random in the plants, what is the probability that the 3 diseased plants are all adjacent? 26. If 10 quarters are distributed at random to 4 people, what is the probability that everyone receives at least 50 cents? 27. Each of 9 different books is to be given at random lo Rebecca, Sheila, or Tom. What is the probability that Rebecca receives 2 books, Sheila receives 4, and Torm receives 3? 28. What is the probability that an odd number between 1000 and 9000 contains no repeated digits? 29. What is the probability that a randomly chosen pelnlutation of the letters in the word 'determine' has no adjacent e's? 30. Exactly 4 of 20 microcomputer diskettes are defective If the diskettes are packaged in two boxes of ten, what is the probability that (a) all the defective diskettes are packed in a particular box? (b) 3 defective diskettes are packed in the same box' (c) 2 defective diskettes are packed in each box? In Exercises 31-34 compute the probability of being de it each of the given hands if 5 cards are dealt from an ordinary 52-card deck. 31. a pair (2 cards of one denomination and 1 each of threz other denominations) 32. two pairs (2 cards of one denomination, 2 of another denomination, and 1 of a third denomination) 33. three-of-a-kind (3 cards of one denomination and I eazh of two others)
7.6* The Principleof Inclusion-Exclusion
393
34. a straight (5 cards of consecutive denominations, where an ace is the highest denomination) 35. A file contains 25 accounts numbered 1-25. If 5 of these accounts are selected at random for auditing, what is the probability that no 2 accounts with consecutive numbers are chosen? 36. In the Illinois State Lotto game, 6 of the integers 1, 2, .. , 54 are picked to be the winning numbers. What is the probability that 3 consecutive numbers n, n + 1, n + 2 are picked and no pairs of consecutive numbers other than n, n + I and n + 1, n + 2 are picked?
7.6*
+
THE PRINCIPLE OF INCLUSION-EXCLUSION The addition principle (Theorem 7.6) tells us how to find the number of elements in the union of pairwise disjoint sets in terms of the number of elements in the individual sets. In this section we will present a similar result that will enable us to count the number of elements in the union of any sets, whether pairwise disjoint or not. The following simple example will demonstrate the type of counting problem that we will be discussing. Suppose that a certain group of computer science students are all studying logic or mathematics. If 12 are studying logic, 26 are studying mathematics, and 5 are studying both logic and mathematics, how many students are in this group? If we let A denote the set of students studying logic and B denote the set of students studying mathematics, then the answer to this question is the number of elements in the set A U B. But since A and B are not disjoint, the addition principle cannot be used directly. It is not difficult, however, to see that the set B' of students studying mathematics but not logic contains 26 - 5 elements. Now A and B' are disjoint and contain all of the students in the group. So the answer to our question is the number of elements in A U B', which is 12 + (26 - 5) by the addition principle. (See Figure 7.3.) A
B
FIGURE 7.3
Our analysis in the example above showed that
IAU BI = IAI + IBI-IA n Bl.
(7.4)
It is not difficult to see that equation (7.4) holds for any finite sets A and B: The sum IA I + IB I counts the elements of A n B twice (once as members of A and once as members of B); so IAl + IBI -IA n BI counts each element of A U B exactly once.
394
Chapter 7
Counting Techniques
Example 7.38 In Example 7.14 we used the addition principle to count the number of 8-bit strings that begin with 1011 or end with 01. Let us count them again by using (7.4). Let A and B denote the sets of 8-bit strings that begin with 1011 and end with 01, respectively. Then A F1 B Wsthe set of strings that begin with 1011 and end with 01, that is, strings of the form 101 1- - 01. Since only the fifth and sixth bits are unspecified, the number of such strings is 2 2 = 4. But since IA I = 24 = 16 and FBI = 26 = 64 from Example 7.14, it follows from (7.4) that the number of 8-bit strings that begin with I C11 or end with 01 is .
IAUB I = IAI -IBI-IAnBI = 16+64- 4=76. Al
,
A2
FIGURE 7.4
Our objective in this section is to generalize (7.4) from two sets to r sets, Al, A2 , . ., Ar. But let us first consider the case that r = 3. It is easy to see in Figure 7.4 that (AI U A2 ) r A. = (AI n A3) U (A2 n A3). By using this fact and (7.4), we can obtain a formula for IAI U A2 U A3 1as follows. AI U A 2 U A3 1
=I(A U A2 ) U A3 1 = JAI U A.l -- IA 3 1-(Al U A2 ) n A3 1 =(JAI + IA2 1-IA
n A2 1) + IA 3 1-
(A, n A3) U (A2 n A3 )l
=(AI + IA 2 1-IAI n A21) + IA 31- (JA n A 31 + IA
2
n A 3 1-JAI n
A3
n
A2
=
JAII + IA21 + IA3 1- JAI n A21 -- IAI n A 3 1- IA2 n A3 1 + IA, n A2 n A3 1
=
(JAII + IA2 1 + IA 3 1)- (JAI i
A21 + IAI
n
A3 1)
n A3 1 + IA 2 n A3 1) + IAI n A2 n A3 1
In order to generalize (7.4) to r sets, let us define nr for 1 < s < r to be the sum of the sizes of all possible intersections of s sets chosen without repetition from among Al, A2 .. ,.Ar,. (For s = 1, we define the 'intersection' of a single set to be the set itself. Thus n ] = IA Il + IA21 + * * + IAr 1.) Note that since there are C(r, s) ways to choose s seLs from among A 1, A2 , . . , Ar, each n, is the sum of C(r, s) terms.
7.6* The Principle of Inclusion-Exclusion
395
If r = 3, so that there are only three sets Al, A2 , and A3, we have: nj = 1A11 +
IA21
+ 1A31,
n2 = IAI n A2 1+ JAI n A3 1 +JA 2 n A3 1, and n3 = JAI A 2 n A3 1. With this notation, the formula derived above can be written IAI U A2 U A3 1 = n1
-
n2 + n3 .
Likewise, if r = 4 (there are four sets AI, A2, A3 , and A4 ), we have: ni = JAI1 + IA2 1 + IA3 1 + lA41,
+ JAI n A4 1 + IA2 n A31 + IA2 n A41 + IA3 n A41, n3 = JI, n A2 n A31 + JI, n A2 n A41 + JAI n A3 n A41 + JA2 n A3 n A41, and n2 = IAI n A2 1 + AAI n A31 n4 =
AAI
n
A2
n
A3 n A41.
In this case, it can be shown that IAI U A2 U A3 U A4 1 =n
+ n3
- n2
- n4 .
Then the desired generalization of (7.4) can be stated as follows.
Theorem 7.9
sfin For any finite sets Al, A2 ,.., A, deTf t' ;in fi-f i~, fl':ig-t/7 fine nS for 1 < s < r to be the sum of the sizes of all possible intersections of s sets chosen without repetition from among Al, A2, . ., Ar. Then IAI UA
Proof
Letm
=
UL.UArI =n -n
2
IAI UA 2 U
2
+n
3
-n
4
+---+(_1)r-
nr.
U ArI. We will show that
m-nl+n2 -n3+ .. +(-)rfnr=O-
Let a E Al U A2 U .. U Ar, and suppose that a belongs to exactly k of the sets Ai. Thena is counted C(k, 0) = 1 time inm, C(k, 1) = k times in n1 (because a belongs to exactly k of the sets Ai), C(k, 2) times in n2 (because a belongs to exactly C(k, 2) of the intersections Ai n Aj), .. , and C(k, k) = 1 time in nk* Furthermore, if s > k, then a is not counted at all in nS because a does not belong to any intersection of more than k of the sets Ai. Hence, the number of times that a is counted in m - n, + n 2 -n 3 + .. + (_l)r nr, is
C(k,0) - C(k, l) +C(k, 2) -C(k, 3) +.
+(_-l)kC (k, k).
But this value is [I + (-l)]k = Ok = 0 by the binomial theorem. It therefore follows that m =n
1
-n 2 +n 3
-n4+--+
(_)r-
n,.
396
Chapter 7
Counting Techniques
Example 7.39 Among a group of programmers, 49 studied Pascal, 37 studied COBOL, and 21 studied FORTRAN. If 9 of these programmers studied Pascal and COBOL, 5 studied Pascal and FORTRAN. 4 studied COBOL and FORTRAN, and 3 studied Pascal, COBOL, and FORTRAN, how many programmers are in this group? Let us denote the sets of programmers who studied Pascal, COBOL, and FORTRAN by P, C, and F, respectively (instead of Al, A2 , and A3). Then the number of programmers in the group is IP U C U F . Now n = IPI + ICI - IFI =49+37+21 = 107, n 2 =lPnCI+IPrFI+ICnFl=9+5+4= n3
18, and
= IP n C n Fl = 3
So by the principle of inclusion-exclusion, we have IPUCUF =n -n
2
+n 3 =107 -18+3
=92.
Hence there are 92 programmers in this group. + +
Example 7.40 How many positive integers less than 2101 are divisible by at least one of the primes 2, 3, 5, or 7? Let AI, A2 , A3 , and A 4 denote the sets of positive integers less than 2101 that are divisible by 2, 3, .5, and 7, respectively, and let n, be defined as in the principle of inclusion-exclusion. Clearly 1AI
= 2100 =1050,
IA31 = 21= 420,
IA2 1=
and
2
IA41 = 2
= 700, = 300.
Thus n= All + IA21 + IA31 + IA4 1 = 1050 + 700 + 420 + 300 =
2470.
An element of AI n A2 is divisible by both 2 and 3 and hence is divisible by 6. Therefore
IAi. n A21 =
2100 = 350, 6
7.6* The Principleof Inclusion-Exclusion
397
and likewise IAI nA 31 =
2100
= 210,
IAI nA 41 =
IA2 n A31 = 20 = 140,
IA2 n A4 1 =
-
10
2100 14 1
= 150,
21
= 100, and
IA 3 n A41 = 20 = 60. Thus n2 = IAI
n A21 + IAI n A31 + IA1 n
A4 1 +
IA2 n A31 +
1A2
n A41 + IA3 n A 41
= 350+210+ 150+ 140+ 100+60 = 1010.
Similar reasoning shows that IA0nA 2 nA 31= 20 =70, 30 2100
JAI n A3 n A4 1 = 70 = 30,
AjnA 2 nA 4 1= -2 and
IA2 n A3
42
=50, 2100
n A4 1 = 105 = 20.
Hence n3 = =
AI n A2 n A31 + IA 1 n A2 n A4 1 + A1 0 A3 n A41 +
JA 2
n A3 0 A 4 1
70 + 50 + 30 + 20
= 170.
Finally we see that n4 = 1A 1 nA 2 nA 3 nA 4 1=
2100 =10. 210
Thus by the principle of inclusion-exclusion, the number of positive integers less than 2101 that are divisible by 2, 3, 5, or 7 is AI UA 2 UA 3 UA 4 1 =n -n
2
+n
3
-n
4
= 2470 - 1010 + 170 - 10 = 1620.
+
In many problems, there is a symmetry that makes the calculation of the numbers nS easier than in Example 7.40. The following example is of this type. +
Example 7.41 A bridge hand consists of 13 cards chosen from a standard 52-card deck. How many different bridge hands contain a void suit (that is, no cards in some suit)?
398
Chapter 7
Counting Techniques
Let A 1, A2, A3, and A4 der ote the sets of bridge hands that contain no spades, no hearts, no diamonds, and no clubs, respectively. Then the number of bridge hands that contain a void suit is JAI U A2 U A3 U A4 1. Let ns be defined as in the principle of inclusion-exclusion. Since a bridge hand that contains no spades must consist of 13 cards chosen from among the 39 hearts, diamonds, and clubs, we see that IAII = C(39, 13).
By the symmetry of the definition of the sets Ai, we see that JAI
= jA2 1 = IA3 1 = JA41,
and so nl
JAIA~ + IA21 + IA31 + IA41
=
(4, 1) IA 1 I 4 C(39, 13). .
Likewise a hand that is void in both spades and hearts must consist of 13 cards chosen from among the 26 diamonds and clubs; so IAI n A21 = C(26, 13). Again, by symmetry, all the sets Ai n Aj have the same size. Thus n2 = JAln A21 + IAI n A31-- JAI n A41 + IA2 n A3 1 + IA2 n A41 + IA3 n A4 1 = C(4, 2). IA n A21 = 6. C(26, 13).
Similar reasoning shows that a hand that is void in three suits must consist of all the cards from the remaining suit; so n3 = C(4,3)
JAI n A2 n A31 = 4 C(13, 13).
Finally no hand can be void in all four suits; so AI n A2 n A3 n A4 = 0, and hence n4 = 0.
Therefore, by the principle of inclusion-exclusion, we have IAI U A2 U A3 U A4 1 =nI - n2 + n 3 -n
4
=4 C(39, 13) - 6 C(26, 13) + 4 C(13, 13) - 0 4=(8,122,425,444) - 6(10,400,600) + 4(1) - 0 =:,427,298,180.
7.6* The Principle of Inclusion-Exclusion
399
Hence there are 32,427,298,180 different bridge hands containing a void suit. +
In Examples 7.39-7.41, we were interested in determining the number of elements in Al U A2 U
U Ar,
that is, the number of elements that belong to at least one of the sets Ai. When the sets A, are subsets of a set U, we can also use the principle of inclusion-exclusion to find the number of elements in none of the sets Ai, that is, the number of elements in (Al UA 2 U .UAr) ' = A, nA 2 n
.
Ar.
Suppose, for example, that we want to know the number of positive integers less than 2101 that are divisible by none of the numbers 2, 3, 5, or 7. In Example 7.40 we used the principle of inclusion-exclusion to calculate that there were 1620 positive integers less than 2101 that are divisible by at least one of the numbers 2, 3, 5, or 7. Hence the number of positive integers less than 2101 that are divisible by none of these is 2100 - 1620 = 480. In the remaining examples of this section, we will illustrate this use of the principle of inclusion-exclusion.
Example 7.42 Among a group of 200 college students, 19 study French, 10 study German, and 28 study Spanish. If 3 study both French and German, 8 study both French and Spanish, 4 study both German and Spanish, and 1 studies French, German, and Spanish, how many of these students are not studying French, German, or Spanish? Let U denote the set of all 200 students and F, G, and S denote the subsets of U consisting of the students who are studying French, German, and Spanish, respectively. Then the number of students in U who are not studying French, German, or Spanish is IU I- F U G U S 1.Now
n = IFI + IGI + SI n
2
=
=
19+ 10+28 = 57,
IFnGI+IFnSI+IGnSI =3+8+4= 15,
n3 = IF
f G n SI = 1.
and
400
Chapter 7 Counting Techniques
Thus, by the principle of inclusion-exclusion, IF U G U SI= So 200 -43 +
-
n2 + n3
=
57
-
15 + 1 =43.
= 157 students are not studying French, German, or Spanish.
+
Example 7.43 At McDonald's restaurants, a H-appy Meal box contains one of four possible gifts. If you buy five Happy Meal boxes, what is the probability that you will receive every one of the four gifts? Let U denote the set of all possible sequences in which five gifts can be obtained, and let Ai denote the subset of U consisting of all the sequences which do not include a gift of type (1 < i < 4). Then we must count the elements of U that are in none of the sets Ai. Clearly a sequence in Al must consist of only the second, third, and fourth gifts. Therefore IA II = 35, and so, by symmetry, n I = C(4, I) -IA,1
= 4(35) = 4(243) = 972.
Likewise a sequence in Al nT A 2 must consist of only the third and fourth gifts. Therefore IAI n A21 = 25, and so n2 =
C(4, 2). IA, n A2 1 = 6(25) = 6(32)
=
192.
Similar reasoning shows that n3 =C(4,3) IAI nA 2 nA 31 =4(15 )=4(l)=4 n4
= C(4, 4). IA n A2 n A3 n A4 1 = 1(o5) = 1(0)
and = 0.
Hence the principle of inclusion-exclusion gives IA, n
A2
- n2 + n3 -n 4 =972- 192+4- 0
A3
A4 1 =n
= 784. It follows that the number of elements of U that are in none of the sets Ai is
IUI - IAI U A2 U A 3 U A4 1 = 45- 784 = 1024-784
=
240.
Thus the probability of collect ing all four gifts if you buy five Happy Meal boxes is 240 1024
-_
.234.
o
A permutation of the integers 1, 2, . . ., n such that no integer occupies its natural position is called a derangement. So 41532 is a derangement of the integers 1, 2, 3, 4, 5 because 1 is not the first digit, 2 is not the second digit, and so forth. Counting the number of derangements is a famous problem that can be solved by the use of the principle of inclusion-exclusion.
401
7.6* The Principleof Inclusion-Exclusion +
Example 7.44 How many derangements of the integers 1, 2, 3, 4 are there? Let U denote the set of permutations of 1, 2, 3, 4; and let AI denote the set of members of U having a 1 as first digit, A2 denote the set of members of U having a 2 as second digit, and so forth. Then a derangement of the integers 1, 2, 3, 4 is a member of U that is not in Al U A2 U A3 U A4. Note that any permutation in A I has the form 1 - - -, where the second, third, and fourth digits can be chosen arbitrarily. So the number of such permutations is P(3, 3). Likewise, IA2 1 = IA3 1 = IA 4 1 = P(3, 3). Permutations in Al n A2 have the form 1 2 -- , and so there are P(2, 2) of them. Likewise AA,n A3 1 = AAI n A4 1 =
JA 2
n A3 1 =
JA 2
n A4 1 =A
3
n A4 1 = P(2, 2).
Similar reasoning shows that JA
ln A2 n A3 1 =A, n A 2
n
A4 1 = lA
n A 3 n A4
1 =JA
2
n
A 3 n A4 1 = P(1, 1)
and JA, n A2 f A3
A4 1 = 1.
Thus, by the principle of inclusion-exclusion, we have AI U A2 U A3 U A4 1 = 4 P(3, 3)- 6. P(2, 2) + 4 - P(l, 1) - P(l, 1) = 4 6 - 6 - 2 +4. 1 - 1 = 15. So the number of derangements of 1, 2, 3, 4 is U)-)AI1 UA 2 UA 3 UA 41=P(4,4)-15=24-15=9.
o
EXERCISES 7.6 1. In a survey of moviegoers it was found that 33 persons liked films by Bergman and 25 liked films by Fellini. If 18 of these persons liked both directors' films, how many liked films by Bergman or Fellini? 2. Among a group of children, 88 liked pizza and 27 liked Chinese food. If 13 of these children liked both pizza and Chinese food, how many liked pizza or Chinese food? 3. Among the 318 members of a local union, 127 liked their congressional representative and 84 liked their governor. If 53 of these members liked both their congressional representative and their governor, how many of these union members liked neither their congressional representative nor their governor? 4. In a particular dormitory, there are 350 college freshmen. Of these, 312 are taking an English course, and 108 are taking a mathematics course. If 95 of these freshmen are taking courses in both English and mathematics, how many are taking a course in neither English nor mathematics?
402
Chapter7
Counting Techniques
5. From a group of 650 residents of a city, the following information was obtained: 310 were college-educated. 356 were married. 328 were homeowners. 180 were college-educated and married. 147 were college-educated and homeowners. 166 were married and homeowners. 94 were college-educated, married, and homeowners. How many of these residents were not college-educated, not married, and not homeowners? 6. In tabulating the 5681 responses to a questionnaire sent to her constituents, a congresswoman found: 3819 favored 3307 favored 2562 favored 2163 favored 1985 favored 1137 favored 984 favored
tax reform. a balanced budget. offshore drilling. tax reform and a balanced budget. tax reform and offshore drilling. a balanced budget and offshore drilling. tax reform, a balanced budget, and offshore drilling.
How many of the respondents opposed tax reform, a balanced budget, and offshore drilling? 7. The following data were obtained from the fast-fooc restaurants in a certain city: 13 8 10 5 3 2 I 5
served hamburgers. served roast beef sandwiches. served pizza. served hamburgers and roast beef sandwiches. served hamburgers and pizza. served roast beef sandwiches and pizza. served hamburgers, rcast beef sandwiches, and pizza. served none of these three foods.
How many fast-food restaurants are there in this city'? 8. The following information was found about the residents of a certain retirement community: played golf. played tennis played bridge. played golf and tennis. played golf and bridge. played tennis and bridge. 5 played golf. tennis, and bridge. 72 did not play golf, tennis, or bridge. 38 21 56 8 17 13
How many residents are there in this retirement community? 9. Eight married couples came to a bridge party. Each woman randomly selected a different man to be her partner for the evening. What is the probability that exactly four husbands were paired with their wives? 10. List all the derangements of 1, 2, 3, 4.
7.6* The Principleof Inclusion-Exclusion
403
11. While taking a 6-week summer math class, Alison frequently had dinner with seven friends from her hometown. She ate dinner with each friend (exactly) 15 times, every pair of friends 8 times, every set of three friends 6 times, every foursome 5 times, every set of five 4 times, and every set of six 3 times, but she never ate with all seven at once. On how many days did Alison have dinner with none of these friends? 12. How many sequences of five digits (0-9) contain at least one 4 and at least one 7? 13. For the graph below, determine the number of ways to assign one of k colors to the vertices so that no adjacent vertices receive the same color. VI
V2
V3
V4
14. If three married couples are seated randomly in six chairs around a circular table, what is the probability that no couple is seated in adjacent seats? 15. How many positive integers less than 101 are square free, that is, divisible by no perfect square greater than 1? 16. How many sequences of six digits (0-9) contain at least one 3, at least one 5, and at least one 8? n. First, 17. The sieve of Eratosthenesis an ancient method for finding prime numbers in a list of integers 2, 3, . cross from the list every multiple of 2 greater than 2. Then cross from the list every multiple of the next prime (3) greater than that prime. Continue this process until no further crossing out is possible. The remaining integers are primes. Here is what the sieve looks like after crossing out multiples of 2 and 3 from the list 2, 3, .. , 20. 2 I
3
4
13
X
)A
5 X
X<
7
17
't,
'8 X
% 19
-M
How many integers in the list 2, 3, .. , 1000 are not crossed out after crossing out multiples of the primes 2, 3, 5, and 7? 18. At Brokaw Hospital, six babies were born to six different women on Monday through Thursday of a particular week. Assuming that each baby was equally likely to be born on any of the four days, what is the probability that there was at least one baby born on each day? 19. In how many ways can four married couples be seated in a row of eight chairs with no husband seated beside his wife? 20. How many arrangements of the numbers 1, 1, 2, 2, 3, 3, 4, 4 are there in which no adjacent numbers are equal? 21. How many five-card poker hands contain at least one card in each suit? 22. How many of the functions with domain {5, 6, 7, 8, 9, 101 and codomain {1, 2, 3, 4} are onto? 23. How many nonnegative integer solutions of xl + x2 +
X3
+
X4 =
12 are there in which no xi exceeds 4?
24. Suppose that five balls numbered 1, 2, 3, 4, and 5 are successively removed from an urn. A rencontre is said to occur if ball number k is the kth ball removed. What is the probability that no rencontres occur? 25. Let S be a set containing m elements, and let n > m be a positive integer. Ordered lists of n items chosen from S are to be constructed in which each element of S appears at least once. Show that the number of such lists is C(m, 0)(m
-
0),
-
C(m, l)(m - l), +
..
+ (-l),-
C(M m-
1)(I)n.
26. Two integers are called relatively prime if I is the only positive integer that divides both numbers. Show that if a positive integer n has PI, P2, , Pk as its distinct prime divisors, then the number of positive integers that
404
Chapter 7 Counting Techniques
are less than n and relatively prime to n is n
_-
-
Pi
n PiP2
+ P2
(-l)k
n + .+ P3
n PIP2.
27. Compute the number Dk of derangements of 1, 2, .
k.
28. For Dn as in Exercise 27, evaluate Dn+ 1 -(n +
when n is a positive integer.
11),
Pk
For nonnegative integers n and m, define S(n, m) to be she number of ways to distribute n distinguishable balls into m indistinguishableurns with no urns empty. These numbers are named Stirling numbers of the second kind after the British mathematicianJames Stirling (1692-1770).
29. Let n be a positive integer. Evaluate S(n, 0), S(n,
1), S n, 2), S(n, n -2),
S(n, n -1),
and S(n, n).
30. Let X be a finite set containing n elements. How many partitions of X into k subsets are there? (See Section 2.2 for the definition of a partition.) 31. How many equivalence relations are possible on a set of n elements?
32. For all integers n > 0 and
m >
1, prove that
S(n + 1, m) =C(n,O)S(O, m -) 33. Prove that S(n + 1, m)
=
S(n, m
-
+ C(n,
1)+ m - S(n,
m)
I)S(l,m-
) +
*+
C(n, n)S(n, m-1).
for all integers n > 0 and
m > 1.
34. Use the result of Exercise 33 to describe a procedure for computing the numbers S(n, m) that is similar to the manner in which the numbers C(n, r) can be compute(d using Pascal's triangle. 35. For all positive integers n and m, prove that [C(m, 0)(m-
S(n, m) =
0) -C(m,
1 )(m- l) +
-
++ (-
l)m - C(m, m -)(l)'
m!
36. Let X and Y be finite sets containing n and m elemenLs, respectively. How many functions with domain X and codomain Y are onto? 37. Let U be a finite set containing no elements, and let .41 , A 2 . Ar be subsets of U. Let n, for 1 < s < r be as defined in the principle of inclusion-exclusion, and for 0 < s < r let ps be the number of elements in U that belong to precisely s of the subsets Al, A2 , . . ., A,_ Prove that C(s, 0)
p,
n, -C(s
+
l, l) n,:+,
+
.
+ (-I)r-,C(r,
r -s)
- n,
,--s
-
(-I)kC(s
+ k, k)
n, 4 -k.
k=O
38. Use Exercise 37 to determine the number of different rearrangements of the letters in 'correspondents' having exactly three pairs of identical letters in adjacent positions.
7.7*
+
GENERATING PERMUTATIONS
AND r-COMBINATIONS Unfortunately there are many Practical problems for which no efficient method of solution is known (such as the knapsack problem described in Section 1.3). In such
7.7* GeneratingPermutationsand r-Combinations
405
cases, the only method of solution may be to perform an exhaustive search, that is, to systematically list and check all of the possibilities. Often, as in Section 1.2, listing all of the possibilities amounts to enumerating all the permutations or combinations of a set. In this section we will present procedures for listing all of the permutations and r-combinations of a set of n elements. For convenience, we will assume that the set in question is 11, 2, . . ., n}. The most natural order in which to list permutations is called lexicographic order (or dictionary order). To describe this order, let p = (Pi, P2, .. , p,) and q = (qI, q2, ., q) be two different permutations of the integers 1, 2, . . , n. Since p and q are different, they must differ in some entry. Let k denote the smallest index for which Pk : qk. Then (reading from left to right) the first k - 1 entries of p and q are the same, and the kth entries differ. In this case, we will say that p is greater than q in the lexicographic ordering if Pk > qk. If p is greater than q in the lexicographic ordering, then we write p > q or q < p. Thus, in the lexicographic order, we have (2,4,1,5,3) > (2,4,1,3,5)
and
(3,2,4, 1,5,6) < (3,2,6,5, 1,4).
By using a tree diagram and choosing entries in numerical order, we can list all the permutations of 1, 2, . . ., n in lexicographic order. Figure 7.5 depicts the case where n = 3. The permutations listed in the last column are in lexicographic order. First entry
Second entry
Third entry
Pennutation
2
3
(1,2,3)
3
2
(1,3,2)
1
3
(2,1,3)
3
1
(2,3,1)
2
(3,1,2)
1
(3,2, 1)
2
3 3
1
2 FIGURE 7.5
In order to have an efficient algorithm for listing permutations in lexicographic order, we must know how to find the successor of a permutation p in the lexicographic order, that is, the first permutation greater than p. Consider, for example, the permutation p = (3, 6, 2, 5, 4, 1) of the integers 1 through 6. Let q denote the successor of p in the lexicographic ordering, and let r denote any permutation greater than q. Since p < q < r, q must agree with at least as much
406
Chapter 7 Counting Techniques
of p (from the left) as r does. Thus q must differ from p as far to the right in its list as possible. Clearly we cannot rearrange the order of the last two entries of p (4 and 1) or the last three entries of p (5, 4, and 1) and obtain a greater permutation. But we can rearrange the last four entries of p (2, 5, 4, and 1) to get a greater permutation, and the least such rearrangement is 4, 1, 2, 5. Thus the successor of p in the lexicographic ordering is q = (3, 6, 4, 1, 2, 5). Notice that the first two entries of q are tlhe same as those of p and that the third entry of q is greater than that of p. Moreover, the third entry of q is the rightmost entry of p that exceeds the third entry of p. Finally, note that the entries of q to the right of the third entry are in increasing order. More generally, consider a permutation p = (PI, P2 . p,) of the integers 1 through n. The successor of p in the lexicographic ordering is the permutation q = (qj, q2,. ., qn) such that: (1) (2) (3)
The first k - 1 entries of q are the same as in p. The kth entry of q, q), is the rightmost entry of p that is greater than The entries of q that follow qk are in increasing numerical order.
Pk.
Therefore we can completely determine q from p if we know the value of k, the index of the entry of p to be changed. As we saw in our example, we want k to be chosen as large as possible. So because of condition 2 above, we must choose k to be the largest possible ind-x for which Pk is less than one of the entries that follow it. But then k is the largest index such that Pk < Pk+]. Thus if we examine the entries of p from right to /eft, the entry of p to be changed is the first entry we reach that is less than the number to its right. In addition, since the entries of p to the right of the kth entry are in decreasing order, qk equals the rightmost entry of p that exceeds Pk. If we now ;witch Pk with the rightmost entry of p that exceeds it, we obtain a new permutation in which the rightmost entries are the remaining entries of q in reverse order. +
Example 7.45 Let us determine the permutation q of the integers 1 through 7 that is the successor of p = (4, 1, 5, 3, 7, 6, 2). Scanning p from right to left, we see that the first entry we reach that is less than the nu mber to its right is the fourth entry, which is 3. (So in the notation above, k = 4.) Thus, q has the form (4, 1, 5, ?, ?, ?, ?). Moreover, the fourth entry of q will be the rightmost entry of p that exceeds the entry that is being changed (which is 3 in our case). Scanning p again from right to left, we see that the fourth entry of q will be 6. Interchanging the positions of the 3 and 6 in p, we obtain (4, 1, 5, 6, 7, 3, 2). [i we now reverse the order of the entries to the right of position k, we will have (4, 1, 5, 6, 2, 3, 7), which is the successor of p. The following algorithm uses the method described in the previous example to list all the permutations of 1, 2, . . ., n.
7,7* Generating Permutationsand r-Combinations
407
Algorithm for the Lexicographic Ordering of Permutations This algorithm prints all the permutations of 1, 2,. .,n in lexicographic order. In the algorithm, (pi, P2. -,Pn) denotes the permutation currently being considered. Step I
(initialization)
for i =O0to n Set p1 i.
endfor Step 2
(generate the permutations)
repeat Step 2.] (output) Print (pi, P2. ,Pn). Step 2.2 (find the index k of the leftmost entry to be changed) Find the largest index k for which Pk < Pk±1 Step 2.3 (is there something to change?) if k > 0 Step 2.3.1 (determine the new value for Pk) Find the largest index j for which p1 > Pk, and interchange the values Of Pk and pj. Step 2.3.2 (prepare to rearrange) Set r =k + 1 and s =n. Step 2.3.3 (rearrange) while r > s (a) Interchange the values of Pr and p5. (b) Replace r with r + l and s with s - 1.
endwhile endif until k
=0
Although the lexicographic ordering is the most natural ordering for listing permutations, determining the successor of a given permutation in the lexicographic ordering requires several comparisons. For this reason, an algorithm that lists permutations in lexicographic order may be less efficient than one that lists the permutations in a different order. But since there are n! permutations of the integers 1, 2,. . ,n, the complexity of any algorithm that lists these permutations will be at least W!. Readers who are interested in leamning more efficient algorithms for listing permutations should consult suggested
reading 113].
408
Chapter 7 Counting Techniques
In Section 1.4 we discussed an algorithm for generating all of the subsets of a set with n elements. Often, however, we need to consider only subsets of a specified size. We will now describe a procedure for generating all of the r -element subsets of {1, 2, . . , nJ. As for permutations, we will list the subsets in lexicographic order. Because a subset is not an ordered array, we will understand this to mean that the elements of a subset will be listed in increasing order as we readfrom left to right. Thus we will write the subset {3, 6, 2, 41 as {2, 3, 4, 6). In order to obtain an algorithm for listing subsets in lexicographic order, we need to determine the success Nor of any particular subset. Consider, for example, the 4-element subsets of {1, 2 3, 4, 5, 6}. There are C(6, 4) = 15 such subsets, and they are listed below in lexicographic order from left to right. {1,2,3,4}, {1,2,5,6}, {2,3,4,5},
{1,2,3,5}, {1,3,4,5), {2,3,4,6},
{1,2,3,6}, {1, 3,4, 61, {2,3,5,6),
{1,2,4,51, {1, 3,5, 6), {2,4,5,6},
{l,2,4,6}, {1, 4,5,6}, 13,4,5,61
As was true for the lexicographic ordering of permutations, the successor of a subset S must differ from S as far to the right in its list of elements as possible. Thus if the last element of a subset in the preceding list is not 6, the successor is obtained by adding 1 to the last element. For instance, the successor of {1, 2, 3, 4} thesuccessorof{1,2,3,5}
is is
11, 2, 3, 5), (1,2,3,6},
the successor of {1,3,4, 5) is
{1,3,4, 6}.
and
If the last element of a subset is 6, its successor will be obtained by a different procedure. Consider {1, 2, 5, 6}, for instance. Because the last element is 6, it cannot be increased. Likewise the next-to-last element is 5, and so it cannot be increased. However, the third-firom-last element can be increased from 2 to 3, and we finish the subset by listing consecutive integers beginning with 3. Thus the successor of {1, 2, 5, 6}
is
{1, 3, 4, 5),
the successor of {2, 3, 5, 61
is
{2, 4, 5, 6).
and similarly,
More generally, consider an r-element subset S = {5I, 2, s, I of {1, 2, . . ., n}. The successor of S in the lexicographic ordering is a subset T {t1 , t 2 , . . ., t, I such that: (1) (2) (3)
The first k - 1 elements in T are the same as those in S. The kth element in T, tk is one more than Sk, the kth element in S. The elements tk, tk+1, .. tr are consecutive integers.
7.7* GeneratingPermutationsand r-Combinations TABLE 7.1 Subset
Index k
(1,2,3,4)
4
(1,2,3,51
4
(1,2,3,61
3
{1,2,4,51
4
{1,2,4,6)
3
{1,2,5,6)
2
{1,3,4,5)
4
(1,3,4,6}
3
(1,3,5,6)
2
{1,4,5,6}
1
{2,3,4,5)
4
(2,3,4,6}
3
{2,3,5,6}
2
(2,4,5,6}
1
(3,4,5,6)
none
409
So, as for permutations, we can completely determine T from S if we know Sk, the leftmost element in S to be changed. Our example illustrates that Sk is the rightmost element in S which can be increased, that is, the rightmost element in S that does not equal its maximum value. Note that the maximum value of the last element in S is n, the maximum value of the next-to-last element in S is n - 1, and so forth. Hence Sk is the rightmost element in S that does not equal n - r + k. Then T is formed by replacing Sk by Sk + 1, Sk+I by Sk + 2, and so forth. For example, in the lexicographic ordering of the 5-element subsets of {, 2, 3, 4, 5, 6, 7,8, the successor of S = {1, 3, 4, 7, 8}
is
T ={1, 3, 5, 6, 7)
because 4 (the third element in S) is the rightmost element that can be increased. Although the procedure described above is easy for humans to implement, searching for the element Sk requires more comparisons than necessary. Consequently we can make the procedure more efficient for a computer by determining k rather than Sk. Let us return to the list of all 4-element subsets of {1, 2, 3, 4, 5, 6} considered earlier. Table 7.1 gives the value of k needed to determine the successor of each subset. The observation that enables us to determine k more quickly is that the value of k for the successor of a subset S is either r or one less than the value of k for S. The following algorithm uses this method of determining k.
Algorithm for the Lexicographic Ordering of r-Combinations This algorithm prints all the r-element subsets of {1, 2, nj in lexicographic order, where 1 < r < n. In the algorithm, {(S, S2, s,. denotes the subset currently being considered. Step I
(initialization) for j = 1 to r Set sj= j.
endfor if r
=
n
Setk -1. otherwise Set k = r.
endif Step 2
(create the subsets)
repeat Step 2.1 (output) Print {SI, S2, . . ., Sr I Step 2.2 (find the index, k, of the first element to be changed) if sk :A n-r +k Set k = r.
410
Chapter 7
Counting Techniques
otherwise Replace k with k - 1. endif Step 2.3 determinedc the successor) if k :A 0 (a) Replace Sk with Sk + 1.(b) for i = k -+ 1 to r Replace .si with Sk + (i- k). endfor endif until k =0
It can be shown that this algorithm has order at most n'r Therefore, for a fixed value of r, the algorithm for the lexicographic ordering of r -combinations is a 'good' algorithm.
EXERCISES 7.7
IM
For the permutations p and q in Exercises 1-6, determine whether p < q or p > q in the lexicographic ordering. l. p
(3, 2, 4,1), q=(4,1, 3, 2)
2. p
(2, 1, 3), q=(1,2, 3)
3.p=(1,2,3), q= (1,3, 2)
4.p=(2,1,3,4), q= (2,3,1,4)
5. p (4 ,2 ,5,3 ,l),q =(4,2,3,5,l)
6.p=(2,5,3, 4,1,6), q =(2,5,3,1,6,4)
In Exercises 7-18 determine the successor of permutation p inthe lexicographic ordering of the permutations of
1, 2,3, 4, 5,6. 7. p=(2,1,4,3,5,6)
9. p
(2, 1,4, 6, 5,3)
8. p =(3, 6, 4,2, 1, 5) 10. p=(3, 6,5, 4, 2,l)
ll. p ( 5 , 6 , 3 , 4 , 2 ,) 13. p=(6,5,4, 3,2,l1)
12
15
16
.p-( 4 ,5 ,6 ,3 ,2 ,1)
18
. p-(
.p=( 5 ,2 ,6 ,4 ,3 ,l)
1'7. p
(6, 3, 5,4, 2,l)
.p
( 5 ,1, 6 , 4 , 3 , 2 )
14. p
(1, 2,3, 6, 5,4) 2 3
, ,1, 6 , 5 , 4 )
19. List the permutations of 1, 2, 3, 4 in lexicographic ord,-r. InExercises 20-31 determine the successor of subset S
~n the lexicographic
ordering of the 5-element subsets of
{1, 2, 3, 4, 5,6,7, 8, 9}. 20. S={11,2, 4, 5, 6
21. S=lI,3, 5,7, 9)
22. S ={I, 3, 6, 8, 9
23.
24. S={[2,4, 6, 7, 9
25. S-={3, 4, 5,7, 8
26. S = 3,4, 7, 8, 9
27. S ={3, 5,7, 8, 9
= 12, 3, 5,8, 91
HistoricalNotes
28. S={4,5,6,7,8} 30. S={4,6,7,8,9)
411
29. S={4,5,7,8,9} 31. S={5,6,7,8,9}
32. List all the 3-element subsets of {I, 2, 3, 4, 5, 6} in lexicographic order.
HISTORICAL NOTES
Blaise Pascal
The roots of combinatorial enumerations reach back to at least the 79th problem of the Rhind papyrus (c. 1650 B.C.). Others lie in the work of Xenocrates of Chalcedon (396-314 B.C.) and his attempts to solve a problem involving permutations and combinations, and work by Oriental and Hindu mathematicians. By the sixth century B.C., texts exist listing the values of combinations of tastes drawn from six basic qualities-sweet, acid, saline, pungent, bitter, and astringent. In his text Lilavati, the Hindu mathematician Bhaskara (ca. 11 14-1185) wrote rules for the computation of combinations and the familiar n! rule for permutations. There is also evidence that the Hindus were familiar with the binomial expansion of (a + b)' for small positive integers n [71, 73, 78, 86, 87]. Girolamo Cardano (1501- 1576) stated the binomial theorem and Blaise Pascal (16231662) presented the first known proof in his 1665 Traite du Triangle Arithmentique. Jacob Bernoulli (1654-1705) provided an alternate proof of the theorem in his Ars Conjectandi (1713) and is often incorrectly given credit for the first proof of the theorem. The arithmetic triangle, often referred to as Pascal's triangle, was known to the Chinese through Chu Shih-Chieh's Ssu Yuan Yii Chien in 1303, and is believed to have been known to others in the Orient before that date [73]. Abraham De Moivre (1667-1754) extended the binomial theorem in 1697 to the multinomial theorem, which governs the expansion of forms such as (xI + x2 + .* + xr)n for positive integers r and n. By 1730, De Moivre and the British mathematician James Stirling (1692-1770) had obtained the asymptotic result, now known as Stirling's formula, that
n! ;t~(n)n (27rn)
1/2
e for large positive integers n. At much the same time, the foundations of probability were forming. Early work in the area came from Cardano and Niccolo Tartaglia (1500-1557), whose work dealt with odds and gambling situations. Cardano published his Liber de Ludo Alea, a book on games of chance, in 1526. In it, he shows knowledge of independent events and the multiplication rule.
The Dutchman Christian Huygens (1629-1695) wrote De Ludo Aleae in 1657. In this book, he considered problems dealing with the probabilities associated with drawing colored balls from an urn. Bernoulli's Ars Conjectandi, published posthumously in 1713, included informaJacob Bernoulli
tion on permutations and combinations, work on elementary nrobabilitv. and the law of large numbers. With the work of Bernoulli, one sees the binomial theorem being used to
412
Chapter 7 Counting Techniques
compute binomial-based probabilities. This work was extended by Pierre Simon Laplace (1749-1827) in Essai Philosoptnque sur les Probabilites in 1814. Laplace's work gave special attention to the applications of probability to demography and other social science problems. Abraham De Moivre is cred ited with the statement that the probability of a compound event is the product of the probabi lities of its components. He also presented an analytical version of the principle of incl usio -exclusion in his 1718 work on probability, the Doctrine of Chances. However, the modern version of the principle of inclusion-exclusion is usually credited to the English/American mathematician James Joseph Sylvester (1814-1897) [86, 87].
SUPPLEMENTARY EXERCISES
1r*1
Evaluate each of the expressions in Exercises 1-8. 1. C(9, 7) 5. (x
-
1)6
2. P(8, 5) 6. (x
3. P(9, 4) 7
+ 2y)
7. (2x + 3y)
4. C(10, 6) 5
8. (5x - 2y)4
9. What is the successor of (8, 2, 3, 7, 6, 5, 4, 1) in the lexicographic ordering of the permutations of
1, 2, 3,4, 5,6,7, 8? 10. What is the successor of {1, 3, 6, 7, 81 in the (I, 2, 3, 4, 5, 6, 7, 81?
lexicographic
ordering of the 5-combinations
of
11. The first nine numbers in the n = 17 row of Pascal's triangle are 1, 17, 136, 680, 2380, 6188, 12376, 19448, and 24310. What are the remaining numbers in this row of Pascal's triangle? 12. Use your answer to Exercise 11 to determine the coeff cient of x 1 2 in the binomial expansion of (x -2)17. 13. Use your answer to Exercise 11 to evaluate C(18. 12). 14. For a town's annual Easter egg hunt, 15 dozen eggs were hidden. There were 6 gold eggs, 30 pink eggs, 30 green eggs, 36 blue eggs, 36 yellow eggs, and 42 purple eggs. How many eggs must a child find in order to be assured of having at least 3 of the same color? 15. The snack bar at a movie theatre sells 5 different sizes of popcorn, 12 different candy bars, and 4 different beverages. In how many different ways can one snacks be selected? 16. A woman has 6 different pairs of slacks, 8 different blouses, 5 different pairs of shoes, and 3 different purses. How many outfits consisting of one pair of slacks, on- blouse, one pair of shoes, and one purse can she create? 17. A woman has 6 different pairs of slacks, 8 different blouses, 5 different pairs of shoes, and 3 different purses. Suppose that an outfit consists of one pair of slacks, cne blouse, one pair of shoes, and may or may not include a purse. How many outfits can the woman create? 18. How many integers between 1500 and 8000 inclusivee) contain no repeated digits? 19. A pianist participating in a Chopin competition has decided to perform 5 of the 14 Chopin waltzes. How many different programs are possible consisting of 5 waltzes played in a certain order? 20. How many ways are there to select a subcommittee of 5 members from among a committee of 12? 21. If two distinct integers are chosen from among the numbers 1, 2, .. , 60, what is the probability that their sum is even?
Supplementary Exercises
413
22. A committee of 4 is to be chosen at random from among 5 women and 6 men. What is the probability that the committee will contain at least 3 women? 23. In a literature class of 12 graduate students, the instructor will choose 3 students to analyze Howard'sEnd, 4 other students to analyze Room with a View, and the remaining 5 students to analyze A Passageto India. In how many different ways can the students be chosen? 24. How many nonnegative integer solutions are there to x + y + z = 15? 25. How many arrangements are there of all the letters in the word 'rearrangement'? 26. If an arrangement of all the letters in the word 'rearrangement' is chosen at random, what is the probability that all the r's are adjacent? 27. How many ways are there to select 4 novels from a list of 16 novels to be read for a literature class? 28. Nine athletes are entered in the conference high jump competition. In how many different ways can the gold, silver, and bronze medals be awarded? 29. A college student needs to choose one more course to complete next semester's schedule. She is considering 4 business courses, 7 physical education courses, and 3 economics courses. How many different courses can she select? 30. Suppose that 8 people raise their glasses in a toast. If every person clinks glasses exactly once with everyone else, how many clinks will there be? 31. Suppose that the digits 1-7 are to be used without repetition to make five-digit numbers. (a) How many different five-digit integers can be made? (b) What is the probability that if one of these numbers is chosen at random, the number begins with 6? (c) What is the probability that if one of these numbers is chosen at random, the number contains both the digits 1 and 2? 32. If a die is rolled six times, what is the probability of rolling two 3's, three 4's, and one 5? 33. A newly formed consumer action group has 30 members. In how many ways can the group elect (a) a president, vice-president, secretary, and treasurer (all different)? (b) an executive committee consisting of 5 members? 34. A bakery sells 8 varieties of bagels. How many ways are there to select a dozen bagels if we must choose at least one of each type? 35. A university's alumni service award can be given to at most 5 persons per year. This year there are 6 nominees from which the recipients will be chosen. In how many different ways can the recipients be selected? 36. A candy company has an unlimited supply of cherry, lime, licorice, and orange gumdrops. Each box of gumdrops contains 15 gumdrops, with at least 3 of each flavor. How many different boxes are possible? 37. If 10 different numbers are chosen from among the integers 1,2, .. ,40, what is the probability that no two of the numbers are adjacent? 38. How many different assortments of two dozen cupcakes can be purchased if there are six different types of cupcakes from which to choose? 39. How many different assortments of two dozen cupcakes can be purchased if there are six different types of cupcakes from which to choose and we must choose at least two cupcakes of each type? 40. In Bogart's restaurant, the entrees include prime rib, filet mignon, ribeye steak, scallops, and a fish-of-theday. Each dinner is served with salad and a vegetable. Customers may choose from 4 salad dressings and 5 vegetables, except that the seafood dishes are served with wild rice instead of the choice of a vegetable. In how many different ways can a dinner be ordered?
414
Chapter 7 Counting Techniques
41. What is the probability that a randomly chosen intbger between 10,000 and 99,999 (inclusive) contains a zero? 42. What is the probability that a randomly chosen list of . letters contains 3 different consonants and 2 different vowels? (Regard 'y' as a consonant.) 43. In how many distinguishable ways can 4 identical algebra books, 6 identical geometry books, 3 identical calculus books, and 5 identical discrete math bookss e arranged on a shelf? 44. A shish kebab is to be made by placing on a skewer a piece of beef followed by 7 vegetables, each of which is either a mushroom, a green pepper, or an onion. How many different shish kebabs are possible if at least 2 vegetables of each type must be used? 45. Sixteen subjects are to be used in a test of 3 exper mental drugs. Each experimental drug will be given to 4 subjects, and no subject will receive more than one drug. The 4 subjects who are not given an experimental drug will be given a placebo. In how many different ways can the drugs be assigned to the subjects? 46. The 12 guests of honor at an awards banquet are to be given corsages. Each guest of honor can choose the color: pink, red, yellow, or white. How many corsage orders to the florist are possible? 47. Fifteen geraniums are to be planted in a row. There are 4 red geraniums, 6 white geraniums, and 5 pink geraniums. Assuming that the plants are indistinguishable except for color, in how many distinguishable arrangements can the flowers be planted? 48. Let S be a 6-element subset of {1, 2, .. , 91. Show that S must contain a pair of elements with sum 10. 49. Can the integers 1, 2, .. , 12 be placed around a circle so that the sum of 5 consecutive numbers never exceeds 32? 50. How many nonnegative integer solutions are there to the equation XI + X2 + A3 + X 4 =
28
with l < 8,x2 < 6,x3 < 12, andX4 < 9? 51. A pinochle deck consists of 48 cards, including two each of the aces of spades, hearts, diamonds, and clubs. What is the probability that a random 12-card pinochle hand contains at least one ace of each suit? 52. In how many ways can n married couples be seated in a row of 2n chairs with no husband seated beside his wife? 53. Give a combinatorial proof that C(n, k) < 2n for 0 < 2 < n. 54. Prove that the largest entry in row n of Pascal's triangle exceeds (1 .5 )f for n > 4. 55. Evaluate C(2, 2) + C(3, 2) + -
+ C(n, 2), and verify) your answer by mathematical induction.
56. If (xI + x2 + .-. + Xk)' is expanded and like terms are combined, how many terms will there be in the answer? 57. Prove the multinomial theorem: For any positive integers k and n,
+ Xk)
(Xl +X2+
=
a
nx ! n2! ..
xl X
where the sum is taken over all nonnegative solutions of the equation nI + n2 + 58. Let s, = C(n, 0) + C(n -1, 1) + C(n than or equal to n
-
2, 2) +
.
-+ l C(n -
Xf2 k
+ nk
=
n.
r, r), where r denotes the greatest integer less
(a) What pattern does s, represent in Pascal's triangle? (b) Formulate a conjecture about the value of Sn for all nonnegative integers n. (c) Prove the conjecture made in part (b).
415
Computer Projects
59. To each subset of {1, 2, . . ., n}, assign one of n possible colors. Show that no matter how the colors are assigned, there are distinct sets A and B such that the four sets A, B, A U B, and A n B are all assigned the same color. Show that the conclusion need not be true if there are n + I colors available. 60. Complete the following chart giving the number of distributions of n balls into m distinguishable urns in the 8 cases indicated. In 6 of the 8 cases, whether the answer is zero or nonzero depends on m > n, m = n, or m < n. Note that each of the n balls must be put into some urn.
Are the Balls Distinguishable?
More than One Ball per Urn Allowed?
May Urns Be Empty?
Yes
Yes
Yes
Yes
Yes
No
Yes
No
Yes
Yes
No
No
No
Yes
Yes
No
Yes
No
No
No
Yes
No
No
No
Number ofDistributions of n Balls into m DistinguishableUrns
COMPUTER PROJECTS Write a computer program having the specified input and output. 1. Given integers n and r such that 0
<
r < n, compute the values of P(n. r) and C(n, r).
2. Given a nonnegative integer n, compute the numbers in rows 0, 1, . . ., n of Pascal's triangle. 3. Given positive integers k and n, list all the nonnegative integer solutions to the equation xi + x 2 +
+
Xk
= n.
4. Given a positive integer n, determine the probability that in a random selection of n people, no two have the same birthday. Assume that no one was born on February 29. 5. Given a positive integer n, list all the derangements of the integers 1, 2, . . ., n. (See Section 7.6 for the definition of a derangement.) 6. Given a positive integer n, use the sieve of Eratosthenes to determine all the primes less than or equal to n. (See Exercise 17 in Section 7.6.) 7. Given positive integers k and n, compute S(n, k), the Stirling number of the second kind. (See Exercise 33 in Section 7.6.) 8. Given a positive integer n, list all the permutations of 1, 2, . . ., n in lexicographic order. 9. Given positive integers r and n, list all the r -element subsets of {1, 2, . . ., n} in lexicographic order. 10. Given positive integers r and n, list all the r-permutations of 1, 2, . ., n in lexicographic order. 11. Given positive integers r and n, print all possible ordered lists of r items selected from the set (I, 2, . . ., nI if repetition of items is allowed. 12. Given positive integers r and n, print all possible unordered lists of r items selected from the set if repetition of items is allowed.
{(,2, .
nI
416
Chapter7 Counting Techniques
SUGGESTED READINGS
1
E1
1
1. Beckenbach, E. Applied CombinatorialMathematics. New York: Wiley, 1964. 2. Even, Shimon. Algorithmic Combinatorics. New York: Macmillan, 1973. 3. Nijenhuis, Albert and Herbert S. Wilf. CombinatorialAlgorithms, 2nd ed. New York: Academic Press, 1978. 4. Ryser, Herbert J. CombinatorialMathematics, Carus -Monograph Number 14. New York: Mathematical Association of America, 1963. 5. Whitworth, William Allen. Choice and Chance. Reprini of the 5th edition. New York: Hafner, 1965.
Recurrence Relations and Generating Functions 8.1 Recurrence Relations 8.2 The Method of Iteration X.i Linear Litterence Equations with Uonstant t-oetncients 8.4* Analyzing the Efficiency of Algorithms with Recurrence Relations 8.5 Counting with Generating Functions 8.6 The Algebra of Generating Functions
In preceding chapters, we have seen several situations
in which we wanted to associate with a set of objects a number such as the number of subsets of the set or the number of ways to arrange the objects in the set. Sometimes this number can be related to the corresponding number for a smaller set. In Section 2.7, for example, we saw that the number of subsets of a set with n elements is twice the number of subsets of a set with n - 1 elements. Often such a relationship can be exploited to derive a formula for the number we are seeking. Techniques for doing so will be explored in this chapter.
8.1
+
RECURRENCE RELATIONS An infinite ordered list is called a sequence. The individual items in the list are called terms of the sequence. For example,
0!, 1!, 2!,..-n!,
.
is a sequence with first term 0!, second term 1!, and so forth. In this case, the nth term of the sequence is defined explicitly as a function of n, namely (n - 1)!. In this chapter we will study sequences where a general term is defined as a function of preceding terms. An equation relating a general term to terms that precede it is called a recurrence relation. In Section 2.6 we saw that n! could be defined recursively by specifying that O! = 1
and
n! = n(n-1)!
for n > 1. 417
418
Chapter8 RecurrenceRelationsandGeneratingFunctisns
In this definition, the equation forn > 1
n! n(n -1)!
is a recurrence relation. It defines each term of the sequence of factorials as a function of the immediately preceding term. In order to determine the values of the terms in a recursively defined sequence, we must know the values of a specific set of terms in the sequence, usually the beginning terms. The assignment of values for these terms gives a set of initial conditions for the sequence. In the case of the factorials, there is a single initial condition, which is that 0! = 1. Knowing this value, we can then compute values for the other terms in the sequence from the recurrence relation. For example, =
1(0!)
1 1 = 1,
2! = 2(1!) = 2.1 = 2, 3! = 3(2!) = 3. 2 = 6, 4!
4(3!)
=
4 6
=
24,
and so on. Another example of a sequence that is defined by a recurrence relation is the sequence of Fibonacci numbers. Recall from Section 2.6 that the Fibonacci numbers satisfy the recurrence relation F, = F'n- 1 + Fn-2
for n > 3.
Because Fn is defined as a function of the two preceding terms, we must know two consecutive terms of the sequence in order to compute subsequent ones. For the Fibonacci numbers, the initial conditions are F1 = 1 and F2 = 1. Note that there are sequences other than the Fibonacci numbers that satisfy the same recurrence relation, for example, 3,47, 11, 18,29,47,76.. Here each term after the second is the sum of the two preceding terms, and so the sequence is completely detennined by the initial conditions s, = 3 and s2 = 4. In this section we will examine other situations in which recurrence relations occur and illustrate how they c an be used to solve problems involving counting. +
Example 8.1 Let us consider from a recursive point of view the question of determining the number of edges e, in the complete graph IC, with n vertices. We begin by considering how many new edges need to be drawn to obtain K, from IC, - I. The addition of one new vertex requires the addition of n - I new edges, one to each of the vertices in IC, - . (See Figure 8.1(a) for the case n = 4 and Figure 8.1(b) for the case n = 5.) Thus we see that the number of edges in ICn satisfies the
8.1 Recurrence Relations
419
recurrence relation en = en-l + (n
-1)
for n > 2.
In this equation, the definition of en involves only the preceding term en-l, and so we need only one value of en to use the recurrence relation. Since the complete graph with 1 vertex has no edges, we see that el = 0. This is the initial condition for this sequence. +
(a)
(b) FIGURE 8.1
+
Example 8.2 The Towers of Hanoi game is played with a set of disks of graduated size with holes in their centers and a playing board having three spokes for holding the disks. (See Figure 8.2.) The object of the game is to transfer all the disks from spoke A to spoke C by moving one disk at a time without placing a larger disk on top of a smaller one. What is the minimum number of moves required when there are n disks?
FIGURE 8.2
To answer the question, we will formulate a recurrence relation for mn, the minimum number of moves to transfer n disks from one spoke to another. This will require expressing m, in terms of previous terms mi. It is easy to see that the most efficient procedure for winning the game with n > 2 disks is as follows. (See Figure 8.3.)
420
Chapter 8
Recurrence Relations and GeneratingFiunctions A1
Step I
B
Posit o l after moving n - I disks from A to B
It
Step 2
B
C
Pos itio a Ifter moving the bottom disk from A to C
A
Step 3
C
B
C
Positi o l after moving n - I disks from B to C FIGURE 8.3
(1) (2) (3)
Move the smallest n - I disks (in accordance with the rules) as efficiently as possible from spoke A to spoke B. Move the largest disk from spoke A to spoke C. Move the smallest n - L disks (in accordance with the rules) as efficiently as possible from spoke B to spoke C.
Since step 1 requires moving n -1 disks from one spoke to another, the minimum number of moves recluired in step 1 is just mn,-. It then takes one move to accomplish step 2, and another Mn-1 moves to accomplish step 3. This analysis produces the recurrence re[at icn fn1r
= Mn1
+
1 + Mn-1,
8.1 RecurrenceRelations
421
which simplifies to the form mn = 2m-_ 1±1
for
n >
2.
Again we need to know one value of mn in order to use this recurrence relation. Because only one move is required to win a game with 1 disk, the initial condition for this sequence is ml = 1. By using the recurrence relation and the initial condition, we can determine the number of moves required for any desired number of disks. For example, ml = 1,
2(1) + 1 = 3 2(3) + 1 = 7, m4 = 2(7) + I = 15, M2 = M3 =
m5 =
and
2(15) + 1 = 31.
In Section 8.2 we will obtain an explicit formula that expresses m, in terms of n. + Example 8.3 A carpenter needs to cover n consecutive 1-foot gaps between the centers of successive roof rafters with 1-foot and 2-foot boards, as shown in Figure 8.4. In how many ways can the carpenter complete his task?
FIGURE 8.4
Our approach will be to determine a recurrence relation and initial conditions for Sn, the number of ways n gaps can be covered. This will require expressing Sn as a function of previous terms si. Note that in order to cover n gaps, the carpenter must finish with either a 1-foot board or a 2-foot board. If the carpenter finishes with a 1-foot board, then he must have covered n - 1 gaps prior to using the last board. There are Sn, I ways to cover these gaps. On the other hand, if the carpenter finishes with a 2-foot board, then he must have covered n - 2 gaps prior to using the last board. There are S,-2 ways to cover these gaps. Since exactly one of these
422
Chapter 8 Recurrence Relations and GeneratingFuncti ns
two cases must occur, the addition principle gives
for n > 3.
Sn = S.]- + Sn 2
To use this recurrence relation, we need to know two consecutive terms of the sequence. Clearly the only way to cover a single 1-foot gap is with a single 1-foot board; so s1 = 1. However, there are two ways to cover two 1-foot gaps, with a single 2-foot board or two 1-foot boards. Thus S2 = 2. The number Sn of ways for the carpenter to complete his task is therefore given by the recurrence relation sl = 4-,
for n > 3
+ Sn-2
subject to the initial conditions si = 1 and S2 = 2. Note the similarity between the numbers Sn and the Fibonacci numbers Fn.
e
Example 8.4 Recall from Section 7.6 that a permutation of the integers 1 through n in which no integer occupies its natural position is called a derangement. By enumerating all permutations of 1, 2, . . ., n, we find that there are no derangements of 1, there is one derangement of 1, 2 (namely 2, 1), and there are two derangements of 1, 2, 3 (namely 2, 3, 1 and 3, 1, 2). We have seen in Example 7.44 that the number D, of derangements of the integers 1, 2, .. , n can be computed by using the principle of inclusion-exclusion. In this example we will use a recurrence relation to count derangements. The comments above show that D1 = 0, D2 = 1, and D3 = 2. To illustrate the general technique, we will list the derangements of the integers 1, 2, 3, 4 that begin with 2. These derangements are of two types. The first type is a derangement that ha; 1 in position 2. Here the situation is as shown below. Prohibited d value:
1
2
3
4
Derangement:
2
1
?
?
It is easy to check that there -s exactly one derangement of this type, namely 2, 1, 4, 3. Notice that comple ing the derangement 2
1
?
?
amounts to deranging the integers 3 and 4, and there is D2 = 1 derangement of two integers. The second type of derangement of 1, 2, 3, 4 that begins with 2 has 2 in position 1 and 1 not in position 2. Note that in this case we have two restrictions on the second position; neither 1 nor 2 can occur in the second position. But since 2 is in the first position, it cannot also occur in the second position. Thus the only restriction with which we must be concerned is that 1 not occur in the second
8.1 Recurrence Relations
423
position. Hence the situation can be represented by the diagram below. Prohibited value:
1
1
3
4
Derangement:
2
?
?
?
In this case, it is easy to check that there are exactly two derangements of this type, namely 2, 3, 4, 1 and 2, 4, 1, 3. Note that since 1 cannot occur in the second position, 3 cannot occur in the third position, and 4 cannot occur in the fourth position, completing the derangement 2
?
?
?
amounts to deranging the integers 1, 3, 4, and there are D3 = 2 derangements of three integers. Thus there are, in all, D2 + D3 = 1 + 2 = 3
derangements of 1, 2, 3, 4 beginning with 2. In the general case, a derangement of 1, 2, . . ., n must begin with k, where k = 2, 3, . - n. For n > 3, there are two types, one in which integer 1 is moved to position k and one in which integer 1 is not moved to position k. If integer 1 is moved to position k, the situation is as shown below. Prohibitedvalue:
1
2
..
k-I
k
k+
Derangement:
k
?
..
?
1
?
. .
n
..
?
Here the remaining n - 2 positions can be filled with the integers other than 1 and k to form a derangement in Dn-2 ways. In the second type of derangement, we have the integer k in the first position and cannot have the integer 1 in position k. Since the integer k is in the first position, it cannot also be in the kth position, and so it is sufficient to require that the kth position not be filled by 1. We depict this situation as follows Prohibited value:
1
2
..
Derangement:
k
?
..
k-I ?
I
k+ I
..
n
?
?
..
?
Thus we must place the n - 1 integers other than k into n - 1 positions with no integer in a prohibited location. There are Dn-I ways to do this. Thus by the addition principle there are Dn-2 + Dn-1 derangements of the integers 1 through n in which integer k is moved to position 1. But there are n - 1 possible values of k (namely 2, 3, . ., n), and so Dn must satisfy the recurrence relation Dn = (n-1)(Dn-2 +
D.-,)
for n > 3.
To use this recurrence relation, we need two consecutive values of Dn Since we have already seen that DI = 0 and D2 = 1, these are the initial conditions. In the next section, we will obtain an explicit formula giving Dn as a function of n. +
424
Chapter 8 Recurrence Relations and GeneratingFun( tisns o
Example 8.5 A stack is an important data structure in computer science. It stores data subject to the restriction that all insertions and deletions take place at one end of the stack (called the top). As a consequence of this restriction, the last item inserted into the stack must be the first item deleted, and so a stack is an example of a last-in-first-out structure. We will insert all of the integers 1, 2, . . ., n into a stack (in sequence) and count the possible sequences in which they can leave the stack. Note that each integer from 1 through n enters and leaves the stack exactly once. We will denote that integer k enters the stack by writing k and denote that integer k leaves the stack by writing k. If n = 1, there is only one possible sequence, namely 1, 1. For n = 2 there are two possibilities.
(a) 1,2,2,1
Order leaving the stack 2,1
(b) 1,1,2, 2
1, 2
Thus if n = 2, there are two possible sequences in which the integers 1, 2 can leave a stack. Now consider the case n = 3. There are only five possibilities for inserting the integers 1, 2, 3 into a stack and deleting them from it.
(a) 1. 2, 3,3,2,1
Order leaving the stack 3,2,1
(b) 1, 2,2.3,3,1
2,3,1
(c) 1. 2, 2,1,3,3
2,1,3
(d) 1, 1,2.3,3,2
1,3,2
(e) 1 1, 2,2, 3,3
1,2,3
Thus of the six possible permutations of the integers 1, 2, 3, only five can result from the insertion and deletion of 1, 2, 3 using a stack. We will count the number cn of permutations of 1, 2, .. , n that can result from the use of a stack in this manner. (Thus cn is just the number of different ways that the integers 1 through n can leave a stack if they enter it in sequence.) The preceding paragraph shows that cl = 1,
c 2 =2,
c 3 =5.
It is convenient also to define co = 1. For an arbitrary positive integer n, we consider when the integer 1 i, deleted from the stack. If it is the first integer deleted from the stack, then the sequence of operations begins: 1, 1, 2, .. The number of permutations that can result from such a sequence of operations is just the number of possible ways that 2, 3, . . ., n can leave a stack if they enter it in sequence. This number is c,- 1 = COCn-I.
8.1 Recurrence Relations
425
If 1 is the second integer deleted from the stack, then the first integer deleted from the stack must be 2. Thus the sequence of operations must begin: 1,2,2, 1,3,.. The number of permutations that can result from such a sequence of operations is ClCn-2*
If 1 is the third integer deleted from the stack, then the first two integers deleted from the stack must be 2 and 3. Thus 1 must enter the stack, 2 and 3 must enter and leave the stack in some sequence, then 1 must leave, and finally 4, 5, .. , n must enter and leave the stack in some sequence. The number of permutations that can result from such a sequence of operations is C2Cn-3. In general, suppose that I is the kth integer deleted from the stack. Then the k - 1 integers 2, 3, . . ., k must enter and leave the stack in some sequence before deleting integer 1, and the n - k integers k + 1, k + 2, . . ., n must enter and leave the stack in some sequence after deleting integer 1. The multiplication principle shows that the number of ways to perform these two operations is Ck- I Cn -k. Thus the addition principle gives
for n > 1.
Cn = COCn-C+ CICn-2 + ' ' ' + Cn-lCO
Since we know that co = 1, the recurrence relation above can be used to compute subsequent values of the sequence. For example, Cl = COcO = 1 1 = 1,
2, COC2 + CC 1 + C2 CO = 1 2 + 1 1 + 2 1 = 5, COC3 + C I C2 + C2 CI + C 3 Co= 1 5 + 1 2 + 2* 1 + 5 1 = 14,
C2 = coaC + clCO = 1 I + I I = C3 = C4 =
.
C5 = COC4 + CIC3 + C2C2 + C3CI + C4CO
= 1
.
14 + 1 5 + 2 2 + 5 1 + 14. 1 = 42, .
.
and so forth. The numbers Cn are called Catalan numbers after Eugene Charles Catalan (1814-1894), who showed that they represent the number of ways in which n pairs of parentheses can be inserted into the expression XIX 2 ..
Xn+1
to group the factors into n products of pairs of numbers. For example, the C3 different groupings of xix2 X3x 4 into three products of pairs of numbers are ((X 1 X2 )X 3 )X 4 ,
xI((x2x3)x 4 ),
Xl(X 2 (X 3 X4 )),
and
=
5
(X 1 (X 2X 3 ))X 4 ,
(xIx 2)(x3x 4).
The Catalan numbers occur in several basic problems of computer science. + The preceding examples have shown several situations in which recurrence relations arise in counting problems. Recurrence relations are also invaluable in examining change over time in discrete settings, as shown in the next example.
426
Chapter 8 RecurrenceRelations and GeneratingFuns tins
Example 8.6 A grain elevator company rece yves 200 tons of corn per week from farmers once harvest starts. The elevator o-perators plan to ship out 30% of the corn on hand each week once the harvest season begins. If the company has 600 tons of corn on hand at the beginning of harvest, what recurrence relation describes the amount of corn on hand at the end of each week throughout the harvest season? If g, represents the number of tons of corn on hand at the end of week n of the harvest season, we can express the situation described in the preceding paragraph by the recurrence relation gn =
gn-It
with the initial condition go
-0.30gn-I + 200
for n
> I
and
go = 600.
600, that is,
g, = 0.70g, 1 -- 200
for n > 1
The 0.70 coefficient of gn-I reflects that 70% of the corn on hand is not shipped during the week, and the constant term 200 represents the amount of new corn brought to the elevator within the week. + Recurrence relations are also often used to study the current or projected status of financial accounts. Example 8.7 The Thompsons are purchasing a new house costing $200,000 with a down payment of $25,000 and a 30-year mortgage. Interest on the unpaid balance of the mortgage is to be compounded at the monthly rate of 1%, and the monthly payments will be $1800. How mruch will the Thompsons owe after n months of payments? Let bn denote the balance (:n dollars) that will be owed on the mortgage after n months of payments. We will obtain a recurrence relation expressing bn in terms of previous balances. Note thai the balance owed after n months will equal the balance owed after n - I mornhs plus the monthly interest minus one monthly payment. Symbolically, we haxe b, b,-l+ .0lbn- -1800, which simplifies to the form bn = l.0]bn
- 1800
forn > 1.
Since this equation expresses bn in terms of bn-I only, we need just one term to use this recurrence relation. Now the amount owed initially is the purchase price minus the down payment, and so the initial condition is b0 = 175,000. +
8.1 Recurrence Relations
427
Recurrence relations, when applied to the study of change as shown in Examples 8.6 and 8.7, are sometimes referred to as discrete dynamical systems. They are the discrete analogs of the differential equations used to study change in continuous settings. The examples in this section have shown several situations in which recurrence relations arise. Other examples will be considered throughout this chapter. When a sequence is defined by a recurrence relation, it is sometimes possible
to find an explicit formula that expresses the general term as a function of n. Sections 8.2 and 8.3 will be devoted primarily to this subject.
EXERCISES 8.1 In Exercises 1-12 determine S5 if so, sI, conditions.
S2, . . .
is a sequence satisfying the given recurrence relation and initial
forn > 1, so = 5
1. Sn = 3Sn-I -9
forn > 1,so=3 3. s, = 2sn-1 + 3n for n > 1, so= 5 2
2.
Sn =-Sn-1
4
for n > I, so 1 for n > 2, so = 2, s1 = -3 s, = 5sn- - 3s,-2 for n > 2, so =-1, sI = -2 S, = -Sn-I + nsn-2 - I for n > 2, so = 3, s1= 4 s, = 3Sn-I -2ns-2 + 2n for n > 2, s 0 = 2, s= 4 S. = 2s,_I + Sn-2 -S,-3 for n > 3, so = 2, s = -1,
. sn = 5s
5. s- =
6. 7. 8.
9.
+n
2n 2-
2sn-1 + Sn-2
10. S, = Sn-I -3Sn 2 + 2Sn-3
11.
Sn =
12.
Sn =
S2
=4
for n > 3, s 0 = 2, s =3, s2 = 4
-s,-I + 2s,-2 + Sn-3 + n 4 3 Sn-I - sn-2 + sn-3 + (_
for n > 3, so = 1,sI = 2, s2 -5 )n for n > 3, so = 3, sI= 2, S2 = 4
13. For the 1995-96 academic year, tuition at Stanford University was $28,000 and had increased by at least 5.25% for each of the preceding 15 years. Assuming that the tuition at Stanford increases by 5.25% per year for the indefinite future, write a recurrence relation and initial conditions for t, the cost of tuition at Stanford n years after 1995. 14. Individual membership fees at the Evergreen Tennis Club were $50 in 1970 and have increased by $2 per year since then. Write a recurrence relation and initial conditions for Mn, the membership fee n years after 1970. 15. A restaurant chain had 24 franchises in 1975 and has opened 6 new franchises each year since then. Assuming that this trend continues indefinitely, write a recurrence relation and initial conditions for r, the number of restaurant franchises n years after 1975. 16. A bank pays 6% interest compounded annually on its passbook savings accounts. Suppose that you deposit $800 in one of these accounts and make no further deposits or withdrawals. Write a recurrence relation and initial conditions for bn, the balance of the account after n years. 17. A consumer purchased items costing $280 with a department store credit card that charges 1.5% interest per month compounded monthly. Write a recurrence relation and initial conditions for b, the balance of the consumer's account after n months if no further charges occur and the minimum monthly payment of $25 is made.
428
Chapter 8 Recurrence Relations and GeneratingF' nations
18. Tom, a new college graduate, has just been offered a jot) paying $24,000 in the first year. Each year thereafter, the salary will increase by $1000 plus a 5% cost of liv ng adjustment. Write a recurrence relation and initial conditions for s, the amount of Tom's salary after n wears of employment. 19. The process for cleaning up waste in a nuclear reactor core room eliminates 85% of the waste present in the area. If there is 1.7 kg of waste in the room at the begi ming of the monitoring period and 2 kg of additional waste are generated each week, determine a recurrence relation and initial conditions describing the amount w, of waste in the core room at the end of week n of the monitoring period. 20. The jabby bird is in danger of being placed on the endangered species list, as there are only 975 of the birds known to be in existence. A bird is placed on the list 'hen the known population reaches 100. If 27% of the jabby bird population either dies or is taken by a peacher each year and only 5 new jabby birds are born, write a recurrence relation and initial conditions describing the number jn of jabby birds at the end of n years. 21. Each day you buy exactly one of the following items: tape (costing $1), a ruler (costing $1), pens (costing $2), pencils (costing $2), paper (costing $2), or a loose-leaF binder (costing $3). Write a recurrence relation and initial conditions for the number s, of different sequences in which you can spend exactly n dollars (n > 1). 22. Suppose that you have a large supply of 2¢, 3¢, and .5¢ stamps. Write a recurrence relation and initial conditions for the number s. of different ways in which no worth of postage can be attached to an envelope if the order in which the stamps are attached matters. (Thus a 2¢ stamp followed by a 3¢ stamp is different from a 3¢ stamp followed by a 2¢ stamp.) 23. Write a recurrence relation and initial conditions for an, the number of arrangements of the integers 1, 2, .
n.
24. Write a recurrence relation and initial conditions for ss, the number of subsets of a set with n elements. 25. Write a recurrence relation and initial conditions for s, the number of two-element subsets of a set with n elements. 26. Write a recurrence relation and initial conditions for the number s, of n-bit strings having no two consecutive zeros. Compute s6. 27. Write a recurrence relation and initial conditions for the number s, of sequences of nickels, dimes, and quarters that can be inserted into a vending machine to purchase a soft drink costing 5n cents. How many sequences are there for a drink costing 50¢? 28. For n > 2, a 6 x n checkerboard can be covered by L-shaped pieces of the type shown in Figure 2.20. Write a recurrence relation and initial conditions for Pn the number of L-shaped pieces needed to cover a 6 x n checkerboard. 29. Write a recurrence relation and initial conditions for the number cn of different ways to group 2n people into pairs to play n chess games. 30. Let p, denote the number of permutations of 1, 2, . , n in which each integer either occupies its natural position or is adjacent to its natural position. Write a recurrence relation and initial conditions for Pn 31. For some positive integer n, draw n circles in the Euclidean plane such that every pair of circles intersects at exactly two points and no three circles have a point in common. Write a recurrence relation and initial conditions for ri, the number of regions into which these circles Divide the plane. 32. Suppose that you have an unlimited supply of red, white, blue, green, and gold poker chips, which are indistinguishable except for color. Write a recurrence relation and initial conditions for the number Sn of ways to stack n chips with no two consecutive red chips. 33. Write a recurrence relation and initial conditions for thme number sn of n-bit strings having no three consecutive zeros. Compute s5 . 34. Write a recurrence relation and initial conditions for st, the number of three-element subsets of a set with n elements.
8.2 The Method of Iteration
429
35. Suppose that 2n points are marked on a circle and labeled 1, 2, .. , 2n. Write a recurrence relation and initial conditions for the number c, of ways to draw n nonintersecting chords joining two of these points. 36. Write a recurrence relation and initial conditions for s, the number of squares of any size that can be formed using the blocks on an n x n checkerboard. 37. Write a recurrence relation and initial conditions for the number s, of n-bit strings that do not contain the pattern 010. Then compute s6. 38. Write a recurrence relation and initial conditions for the number s, of n-bit strings that contain neither the pattern 1000 nor the pattern 00 1.
8.2
+
TH-E METHOD OF ITERATION In Example 8.2 we saw that the minimum number of moves required to shift n disks from one spoke to another in the Towers of Hanoi game satisfies the recurrence relation m, = 2m,_1 + 1
for n > 2
and the initial condition ml = 1. From this information, we can determine the value of m, for any positive integer n. For example, the first few terms of the sequence defined by these conditions are ml = 1, M2=
2(1) + 1 = 2 + 1 = 3,
M3 = 2(3) +
m4
=
1 = 6 + 1 = 7,
2(7) + I = 14 + 1 = 15,
and
m5 = 2(15) + 1 = 30 + I = 31.
We can continue evaluating terms of the sequence in this manner, and so we can eventually determine the value of any particular term. This process can be quite tedious, however, if we need to evaluate m, when n is large. In Example 8.7, for instance, we might need to know the unpaid balance of the mortgage after 20 years (240 months), which would require us to evaluate b240. Although straightforward, this calculation would be quite time-consuming if we were evaluating the terms by hand in this manner. We see, therefore, that it is often convenient to have a formula for computing the general term of a sequence defined by a recurrence relation without needing to calculate all of the preceding terms. A simple method that can be used to try to find such a formula is to start with the initial conditions and compute successive terms of the sequence, as illustrated above. If a pattern can be found, we can then guess an explicit formula for the general term and try to prove it by mathematical induction. This procedure is called the method of iteration.
430
Chapter8
Recurrence Relations and GeneratingFinctions
We will use the method of iteration to find an explicit formula for the general term of the sequence satisfying the Towers of Hanoi recurrence for n > 2
m,=1 m-1 +1
with the initial condition mn 1. We computed above the first few terms of the sequence satisfying these conditions. Although it is possible to see a pattern developing from these compilations, it is helpful to repeat these calculations without simplifying the results to a numerical value. ml = 1 M2 = M3
2(1) + 1
2+ 1
= 2(2 +)--
1 = 22 +2+ 1 H 1) + I = 23 + 22+ 2 + 1
m4 = 2(22 +2
2 + 1) + 1 = 24 + 2' + 22 + 2 + 1
M5 = 2(23 + 222
From these calculations, we can guess an explicit formula for m,: Mn = 2n 1+2n-2 + .. +22 +2+
1.
By using a familiar algebraic identity (see Example 2.59)
1
+
s
, -X2
x-1-
xn
X
I1
this formula can be expressed in an even more compact manner:
1=
=2
_21.
At this point, the formula obtained above is nothing more than an educated guess. To verify that it does indeed give the correct values for mu, we must prove by induction that the formula is correct. To do so, we must show that if a sequence mI, M2 , M 3 , .. satisfies the recurrence relation mn,
+1
2mIn
for n > 2
and the initial condition m l = 1, then mn = 2nClearly the formula is correct for n = 1 because 21--
=I =
1 for all positive integers n.
=21 =Imi.
Now we assume that the formula is correct for some nonnegative integer k, that is, we assume that Mk = 2 - 1.
8.2 The Method of Iteration
431
It remains to show that the formula is correct for k + 1. From the recurrence relation, we know that mk+l =
2
mk + 1.
Hence mk+l =2(2k -
1) + 1
2k±1 -
2+ 1
2
1,
-
-
which proves the formula for k + 1. It follows from the principle of mathematical induction that the formula mn = 2n-
1
is correct for all positive integers n. Certain formulas are very useful for simplifying the algebraic expressions that arise when using the method of iteration. One of these is the identity 1
X+2 +
.,
+
Xn
=Xn
x~l-
1
from Example 2.59. Another is the formula for the sum of the first n positive integers 1+2+3+
+
n(n
which was obtained in Exercise 11 of Section 2.6. +
Example 8.8 In Example 8.1 we saw that the number en of edges in the complete graph ICn satisfies the recurrence relation en =
en-l
+ (n-1)
for
n>2
and the initial condition el = 0. We will use the method of iteration to obtain a formula for en. To begin, we use the recurrence relation to compute several terms of the sequence. el =
0
e2 = 0 + 1
e3 = (O+1)+2 e4 =
(0 + 1 + 2) + 3
e5
(0 ± 1 + 2 + 3) + 4
=
432
Chapter 8 Recurrence Relations and GeneratingFunctions
From these calculations, we conjecture that
en = O+ I + 2 + - .+ (n -1 (n - I)n
2 2
n -n 2 To verify that the formula is correct, we again need a proof by induction to show that if a sequence satisfies the recurrence relation en =e
+ (n-1)
for n > 2
and the initial condition el = C, then its terms are given by the formula n2
_n
2
en
The formula is correct for n 1 because 12 1
n2 - n 2
2
Assume that ek =
k2 - k 2
for some k > 1. Then ekil
=ek + [(k + 1)- 1] k2 - k
2 2
k - k 2
2k 2
(k2 + 2k + 1) - (k + 1) 2 (k+ 1)2 -(k+ 1) 2 Thus the formula is correct for k + 1. It now follows from the principle of mathematical induction that the fonnula is correct for all positive integers n. + +
Example 8.9 Find a formula for pn, the number of ways to group 2n people into pairs. We begin by finding a recurrence relation and initial conditions for p, In order to group 2n people intc' pairs, we first select a person and find that person
8.2 The Method of Iteration
433
a partner. Since the partner can be taken to be any of the other 2n - 1 persons in the original group, there are 2n - 1 ways to form this first pair. We now are left with the problem of grouping the remaining 2n - 2 persons into pairs, and the number of ways of doing this is Pn-1. Thus, by the multiplication principle, we have p, = (2n - )Pn-1
forn > 1.
Since two people can be paired in only one way, the initial condition is p1 = 1. Let us use the method of iteration to find an explicit formula for Pn. Because PI = I P2 = 3(1) P3 = 5(3)(1) P4 = 7(5)(3)(1) P5 = 9(7)(5)(3)(1),
it appears that p, = (2n
-
1)(2n -3).. (3)(1),
the product of the odd integers from 1 through 2n -1. This expression can be written more compactly using factorial notation. Since the even integers are missing from this product, we insert them into both the numerator and the denominator: (2n- 1(2n3 . (3(1)
(
(
(2n)(2n
1)(2n
-
)
-
(2n)(2n
2)(2n -3) .. (3)(2)(1) -
2).. (2)
(2n)! (2)(n)(2)(n -1) . (2)(1) (2n)! 2nn! Thus our conjecture is that (2n)! 2nn!
MN
We must prove that this formula is correct for all positive integers n by mathematical induction. For n = 1, the formula gives (2n)! 2nn!
2! 21 1!
which is correct. Assume that (2k)! PK
-
2kk!
2 2
434
Chapter 8 Recurrence Relations and GeneratingFunctions
for some positive integer k. 'Then Pk+1
= [2(k + 1)=
1]Pk
(2k + 1) (2kk! (
)2kk!
(2k + 1)! 2kk! 2k+2
(2k+1)!
2(k + 1)
2kk!
(2k + 2)! 2k+1(k + 1)!'
proving the formula for k + 1. Thus the formula is correct for all positive integers n by the principle of mathematical induction. + The process for finding a formula for the general term of the sequence of values associated with a recurrence relation is akin to finding the solution of a differential equation in a continuous setting. For this reason, the formula expressing the relation is sometimes called a solution to the recurrence relation. In our examples so far, we verified formulas for recurrence relations that expressed Sn in terms of s1, -1 but no other si. When we want to verify a formula for a recurrence relation that expresses sn in terms of si for i < n - 2, then the strong principle of mathematical induction will be required.
Example 8.10 We will prove that if xn satisfies Xn = Xn 1 + 2xn-2 + 2n initial conditions xo = 6 and x I = 0, then Xn =
+ 2n +
3(-1)'
-
9 for n > 2 with the
for n > 0.
2+-n
It is easily checked that the formula is correct for n = 0 and n the formula is correct for n =- 0, , .t , k, where k > 1. Then Xk+l = Xk + = [ 3 (-l = -
3
-3(
2
Xk-I + 2(k
I ])-9
)k + 2k+ 2
- A]
(-1 )k
1+
- lIk-1 +
k
+
6(-
I
2
)k I + 2(2k)
=
3
(-
=
3
(- )k+l
+ 2k+1 -
2--
+
2
[ 3 (-l )k-I +
k + 6(-l)k
)k-I + 2 k+ 2 k +
2k1 +2-(k-
1+ 2 -k
2 (2 k
1
=
1)]+2k
) + 4 - 2(k-
+ 4 + 2-
1. Assume that
7
1) + 2k-7
7
-kkkI± 2 2 -- (k + 1),
which verifies the formula tcr k + 1. It follows from the strong principle of induction that the formula is correct for all nonnegative integers n. +
8.2 The Method of Iteration
435
There can be many formulas that agree with the beginning terms of a particular sequence. Here is a famous problem where it is easy to mistake the pattern of numbers. It can be shown that for any positive integer n, it is possible to draw n circles in the Euclidean plane such that every pair of circles intersects at exactly two points and no three circles have a point in common. Moreover, for any configuration of such circles, the number r, of regions into which these circles divide the plane is the same. Let us determine a formula expressing r, as a function of n.
(a) r,
=
1
(b) r, = 2
4 8 (c) r2 = 4
(d) r3 = 8 FIGURE 8.5
Figure 8.5 shows that ro = 1,r, = 2, r2 = 4, and r 3 = 8. From these numbers it is natural to conjecture that r, = 2' . However, the formula r, = 2' is not correct because Figure 8.6 shows that r4 = 14. To obtain a correct formula, we must discover a recurrence relation that relates the number of regions formed by n circles to the number of regions formed by n - 1 circles.
14
8
FIGURE 8.6
FIGURE 8.7
436
Chapter 8
RecurrenceRelations and Generating,uri cions
Suppose that we include a fourth circle with the three in Figure 8.5(d). This new circle (the inner circle in Figure 8.7) does not subdivide either region 5 or region 8 in Figure 8.5(d). We see, however, that this circle intersects each of the other three circles in two points each, and so the circle is subdivided into 2(3) = 6 arcs, each of which subdivides a region into two new regions. The arc from A to B in Figure 8.7, for instance, subdivides region 3 in Figure 8.5(d) into the two regions marked 3 in Figure 8.7. This same situation occurs in general: If there are n - 1 > 1 circles satisfying the given conditions and another circle is drawn so that every pair of circles intersects at exactly two points and no three circles have a point in common, then the new circle forms 2(n - 1) new regions. Hence we see that r,
r,-r + 2(n - 1)
for n > 2.
Note that this recurrence relation is not valid for n = 1. Hence in trying to find a formula for rn, we cannot expect our formula to be valid for n = 0. From this recurrence relation, we see that r =
2
r2 = 1r + 2(1'i = 2 r3 =r r4
2
+ 2(1)
+2(2'=2+2(1)+2(2)
= r3 + 2(3
r 5 = r4 +
= 2+ 2(1) + 2(2) + 2(3)
2(4 = 2 + 2(l) + 2(2) + 2(3) + 2(4).
From these calculations, the pattern appears to be rn
=
2[1+ 2 +
+ (n -)].
Using the formula for the sum of the first k positive integers, I + .i+3+
.+ k
k( 22
)'
we can simplify the expression above to the form
± +2-
rn = 2
l)
=2+(n- l)n =n2
n+2.
Thus we conjecture that on = n2 - n + 2 for n > 1. We will leave as an exercise the verification of this formula by induction. Note that, as expected, the
8.2 The Method of Iteration
437
formula r, = n2
n +2
-
that we obtained is valid only for n > 1. It is instructive to see what would happen if we try to prove that the incorrect formula rn = 2n satisfies the recurrence relation. Note that for n = 1 the formula is correct because 21 = 2 = rl. The difficulty arises in the inductive step. Assume that the formula is correct for some positive integer k, that is, assume that rk = 2 Then rk+l = rk
+ 2k = 2 + 2k,
which is not equal to 2 k+I for every positive integer k. Since our induction proof breaks down, we must conclude that the general term of the recurrence relation rn = r,-I + 2(n-1) is not given by the formula rn = 2'. We will conclude this section by obtaining a formula for the number of derangements of the integers 1 through n.
+
Example 8.11 In Example 8.4 we obtained the recurrence relation Dn = (n-1 )(Dn-I
+
for n > 3
D,-2)
and the initial conditions DI = 0 and D2 = 1 for the number Dn of derangements of the integers 1, 2, . . . , n. It turns out that a sequence that satisfies this recurrence relation must also satisfy the relation Dn =nDn-Il
+
(-_I )n.
To see why, note first that Dn-nD-
= Dn-(n -l)Dn-
-Dn-1
= (n-l)Dn-2 -D-, = -[D-l-(n
-l)Dn-2]
438
Chapter 8 Recurrence Relations and GeneratingFunctions
It follows that Dn - n Dl
= (-l )[Dn- -I(n
- I)Dn -2]
=
1) 2 [Dn-2
-(n-2)Dn-3]
=
1)3[Dn-3 -(n
= (-l)n-2[D2
3)Dn-4]
2Di]
-
2(0)]
2[1 -
= (-1
-
)n2 =
(-l)n.
The resulting recurrence relation Dn = nDn-1 + (-l)n
holds not only for n > 3, but also for n = 2. Hence it is valid for n > 2. We will use the method of iteration to obtain a formula expressing D, in terms of n. It will be easier to apply the method of iteration to the new recurrence relation above than to the one in Example 8.4 because the new one relates Dn to Dn-1 rather than to both Dn-1 and Dn- 2. It produces the following terms. D
2
= 1
D3 =
1 3- 1
3(1) --
1 = 4(3) I-
D4 = 4(3 - 1) D 5 = 5[4(3i -L+
4+
-
1
1] - 1 = 5(4)(3) - 5(4) + 5 - 1
D6 = 6[5(4i(3> - 5(4) + 5- 1] + 1 = 6(5)(4)(3;) - 6(5)(4) + 6(5) - 6 + 1 Note that D6 = P(6, 4)- 1'(6,3) + P(6, 2)-P(6, l) + P(6, O) 6! 2!
6! 3!
6! 4!
6! 5!
11 (2!
3!
4!
6! 6!
1+
iN
5!
6!)
+
+ (-)n'!] at
Thus we conjecture that Dn
= n!
---
We2! 3!
We leave as an exercise the verification that this formula is correct.
8.2 The Method of Iteration
439
The method of iteration depends upon being able to recognize a pattern being formed by successive terms. In practice this may be very difficult, or even impossible, to do. Nevertheless, the method of iteration can often be used to find a formula for the general term of a sequence defined by a recurrence relation, especially in problems where the recurrence relation is simple. Furthermore, the method of iteration is not limited to recurrence relations of a particular form. In the next section, we will use the method of iteration to find formulas for two very common types of recurrence relations. EXERCISES 8.2 1. Prove by mathematical induction that n2 - n + 2 is a solution to the recurrence relation r, = rn-1 + 2(n - 1) for n > 2 with the initial condition r1 = 2. 2. Prove by mathematical induction that 4(2n) + 3 is a solution to the recurrence relation s,= 2sn - 3 for n > 1
with the initial condition so = 7. 3. Prove by mathematical induction that 4' - 3n + 1 is a solution to the recurrence relation s,
= 7s,
- l12sn-
+6
for n > 2 with the initial conditions so = 1, s, = 2. 4. Prove by mathematical induction that 3'(3 + n) is a solution to the recurrence relation s,= 3 s,_1 + 3' for n > 1 with the initial condition so = 3.
5. In Example 8.11, prove by mathematical induction that D, =
2!
3!
. d (
for n > 2.
) n!
6. Prove by mathematical induction that -C(2n -2, n
n
-
1)
is a solution to the recurrence relation Sn =
4n
-
n
6
Sn-l
for n > 1
with the initial condition s, = 1. 7. Prove by mathematical induction that C(2n + 2, 3) is a solution to the recurrence relation s, = s,_1 + 4n 2 for n > 2 with the initial condition s, = 4. 8. Prove by mathematical induction that C (2n + 1, 3) is a solution to the recurrence relation Sn = sn for n > 2 with the initial condition s1 = 1.
9. Compute 10. Compute
+
32
+
52
+
+ (2n -1)2
+ (2n) 2 .
22 + 42 + 62 + 12
1
*
+ (2n - 1)2.
In Exercises 11-24 use the method of iteration to find a formula expressing x, as a function of n for the given recurrence relation and initial conditions. 11. Sn = Sn-I + 4, so = 9
12
13. xn
14.
=
3Sn-1, so
5
. Sn = -2s-n-, so = 3 sn = Sn-1
-2, so = 7
440
Chapter 8
Recurrence Relations and Generating lFun,-tions
15. s, = -s, -, so = 6
17. Sn =
5
sn-_
16. sn
+ 3, so = I
19. S, = Sn-I + 4(n -3), so = 10
21. sn = -sn-
1
+ a, so = 1, a
23. sn = nsn-I + 1, so = 3
1
Sn
13. sn
=
20. sn
= -Sn-I
2
l +
5 -3sSn-
10, so = -4 so = 2
+ (- l)n, so = 6
2. s, = s,_I + 2n + 4 , so = 5
24. s, = 4Sn-2 +
1, SO = 1 SI = 73
25. Suppose that a high school had 1000 students enrolled at the beginning of the 1995 school term. The trend in enrollment over the previous 20 years was that the number Sn of students beginning a school year was 5% less than that of the previous year. (a) Find a recurrence relation and initial conditions representing this situation, assuming that the enrollment trend continues. (b) Find a formula expressing s, as a function of n. (c) If the enrollment trend continues, what number cf students does the formula predict for the beginning of the school year 2005? 26. Zebra mussels are fresh water mollusks that attack underwater structures. Suppose that the volume of mussels in a confined area grows at a rate of 0.2% per day. (a) If there are now 10 cubic feet of mussels in a lcck on the Illinois River at Peoria, Illinois, develop a recurrence relation and initial conditions that represent the volume m, of the mussel colony n days hence. (b) Develop a formula expressing mn as a function o a. 27. The figure below shows that 4 one-inch segments are needed to make a 1 x 1 square, 12 one-inch segments are needed to make a 2 x 2 square composed of four I x 1 squares, and 24 one-inch segments are needed to make a 3 x 3 square composed of nine 1 x 1 squares. How many one-inch segments are needed to make an n x n square composed of 1 x 1 squares?
w]r 28. (a) A rabbit breeder has one male-female pair of newborn rabbits. After reaching two months of age, these rabbits and their offspring breed two other male-fe -nale pairs each month. Write a recurrence relation and initial conditions for rn, the number of pairs of racbits after n months. Assume that no rabbits die during the n months. (b) Showthattherecurrencerelationin(a)satisfiesr, = 2 rn 1 + (- ))n forn > 2,andfindaformulaexpressing rn as a function of n. (Hint: Use a procedure like that in Example 8. 11.) 29. Consider the sequences of n terms in which each term is- 1, 0, or 1. Let s, denote the number of such sequences in which no term of 0 occurs after a term of 1. Find a formula expressing Sn as a function of n. 30. Consider the sequences of n terms in which each term is- 1, 0 or 1. Let sn denote the number of such sequences that contain an even number of Is. Find a formula expressing Sn as a function of n. 31. For some positive integer n, draw n lines in the Euclidean plane so that every pair of lines intersects and no three lines have a point in common. Determine r,. ih- number of regions into which these lines divide the plane.
8.3 Linear Difference Equationswith Constant Coefficients
441
32. Let mn denote the number of multiplications performed in evaluating the determinant of an n x n matrix by the cofactor expansion method. Find a formula expressing mn as a function of n. 33. Suppose that the Towers of Hanoi game is played with 2n disks, two each of n different sizes. A disk may be moved on top of a disk of the same size or larger, but not on top of a smaller disk. Find a formula for the minimum number of moves required to transfer all the disks from one spoke to another. 34. Suppose that in the Towers of Hanoi game a disk can be moved only from one spoke to an adjacent spoke. Let m, denote the minimal number of moves required to move n disks from the leftmost spoke to the rightmost spoke. Find a formula expressing m, as a function of n.
8.3
+
LINEAR DIFFERENCE EQUATIONS WITH CONSTANT COEFFICIENTS The simplest type of recurrence relation gives
Sn
as a function of Sn-
1 for n > 1.
We call an equation of the form Sn
aSn-
+
b,
where a and b are constants and a 7 0, a first-order linear difference equation with constant coefficients. For example, the recurrence relations below are all first-order linear difference equations with constant coefficients: Sn
= 3sn1-1,
Sn = Sn-I + 7,
and
Sn =5 Sn- I
Recurrence relations of this type occur frequently in applications, especially in the analysis of financial transactions. The recurrence relations in Examples 8.2 and 8.7 are first-order linear difference equations with constant coefficients. Since a first-order linear difference equation with constant coefficients expresses Sn in terms of sn,-, a sequence defined by such a difference equation is completely determined if a single term is known. We will use the method of iteration to find an explicit formula for this type of equation that expresses sn as a function of n and so. Consider the first-order linear difference equation with constant coefficients Sn = asn- + b that has first term so. The first few terms of the sequence defined by this equation are so = So,
s = aso 0 + b, S2
= as, ± b
S3
= as
S4
= as3
2
=
a(aso + b) + b = a2 so + ab + b,
+b =a(a2 so+ab+b)+b=a3 so+a 2 b+ab+b, +
b
=
a(a3 so + a 2b + ab + b) + b
= a4 so+a3 b+a
2
b+ab+b.
442
Chapter 8 RecurrenceRelations and Generatinglun, tions
It appears that s, anso - a,- 'b+an- 2 b++
2
b+ab+b
2
2
= anso - (aI
+a
+ a + a + l)b.
+ an- +
If a = 1, the expression in parentheses equals n; otherwise it can be simplified by using the identity from Example 2.59: 1
x +x
2
+
+±xn = x+-1
x-1
Applying this identity to the expression for s, above, we obtain Sn =
at'so + (a a-
) b
=a'so+a'( b1)- (ab = a' (so + c)
)
-c,
where b a -
We will state this result as Theorem 8.1, leaving a formal proof by mathematical induction as an exercise.
Theorem 8.1 The general term of the first-order linear difference equation with constant coefficients Sn
= aSn-l +
b that has initial value so satisfies
an(so+ c)-c s = Iso + nb
if a :0 1 if a = 1,
where b c =a-1I +
Example 8.12 Find a formula for s, if s, 3s, -l for n > 1 and so = 2. Here a = 3 and b = -1 in the notation of Theorem 8.1. Thus c
b
a -1
1 =3
-1
3 -1
-
-1
2 '
8.3 Linear Difference Equationswith Constant Coefficients
443
and so s, = a (so + c)
c
-
2
2 =
( 3 f) + j
1
I (3n+1 + 1).
2
Substituting n
=
0, 1, 2, 3, 4, and 5 into this formula gives
so = 2, s, = 5,
S2 =
14,
s3 = 41,
54 =
122,
and S5 =365,
which are easily checked by using the recurrence relation Sn
=
for n > I
3Sn-1 - I
and the initial condition so = 2. +
+
Example 8.13 Find a formula for bn, the unpaid balance after n months of the Thompson's mortgage in Example 8.7. We saw in Example 8.7 that bn satisfies the recurrence relation bn =l.Olbn - 1800
forn > I
and the initial condition bo = 175,000. Since this recurrence relation is a firstorder linear difference equation with constant coefficients, Theorem 8.1 can be used to find a formula expressing bn as a function of n and bo. In the notation of Theorem 8.1, we have a = 1.01 and b =-1800. Hence b -1800 -1800 0.01 =-180,000. 1.01-1 a-1 Thus the desired formula for bn is bn =
a'(bo + c)
- c
= (1.0l)'[175,000 + (-180,000)]
-
(-180,000)
= -5000(1.01)n + 180,000. For example, the balance of the loan after 20 years (240 months) of payments is b24 = -5000(l.01)240 + 180,000
t
-54,462.77 + 180,000
= 125,537.23.
Thus the Thompsons will still owe $125,537.23 after 20 years. +
444
Chapter8 Recurrence Relations and GeneratingIbun tons
+
Example 8.14 A lumber company owns 7001) birch trees. Each year the company plans to harvest 12% of its trees and plant 600 new ones. (a) (b)
How many trees will theie be after 10 years? How many trees will thee be in the long run?
Let s, denote the number of trees after n years. During year n, 12% of the trees existing in yearn - I will be harvested; this number is 0.12sn 1. Since 600 additional trees will be planted during year n, the number of trees after n years is described by the equation as, = S -I - 0.1
2
+
Sn
600,
that is, =
0.88s,-I + 600.
This is a first-order linear diffe ence equation with constant coefficients a = 0.88 and b = 600. We are interested in the solution of this equation satisfying the initial condition so = 7000. In the notation of Theorem 8.1, b
600 0.88- 1
a-- 1
600 -0.12
Hence a formula expressing s, in terms of n is S
ael' (So + C) - C (0.88)'(7000
-
5000) + 5000
2(000(0.88)' + 5000. (a)
Therefore, after 10 years. the number of trees will be so) = 2000(0.88)10 + 5000 - 5557.
(b)
As n increases, the quantity (0. 88 )n decreases to zero. Hence the formula Sn
= 2000(0.88 ) + 5000
implies that the number of trees approaches 5000. (Note that as the number of trees approaches 5000, the number of trees being harvested each year approaches the number of new trees being planted.) + X~COJ Order
k obje X1ivle-:eo
Iain
Certainly one of the most famous sequences in mathematics is the sequence of Fibonacci numbers, named aFtzr Leonardo Fibonacci of Pisa (c. 1170-1250), the most famous European mathematician of the Middle Ages. The sequence first appeared in the following problem in his text Liber Abaci.
8.3 LinearDifference Equations with ConstantCoefficients
445
A man has one male-female pair of rabbits in a hutch entirely surrounded by a wall. We wish to know how many rabbits can be bred from this pair in one year, if the nature of these rabbits is such that every month they breed one other male-female pair which begin to breed in the second month after their birth. Assume that no rabbits die during the year. The diagram in Figure 8.8, with the letter 'M' denoting a mature pair and the letter 'I' denoting an immature pair, shows the pattern of reproduction described in the problem. January 1
1
February 1 M March I
M
AprillI
M
I
I
May I
June I
M
M
M
I
M
M I
M
I
M
I M
FIGURE 8.8
We see that the number of rabbits at the beginning of any month equals the number at the beginning of the previous month plus the number of new pairs, which equals the number of pairs two months earlier. Thus the number of rabbits at the beginning of month n satisfies the recurrence relation Fn = Fn-1 +Fn-2
for n > 3
with the initial conditions F, = F2 = 1. It is easily checked that there will be F 13 = 233 pairs of rabbits in the hutch one year later! The recurrence relation Fn = Fn-1 + Fn-2
for n > 3
is called the Fibonacci recurrence. It appears in a wide variety of applications, often where it is least expected. Recall, for instance, that the Fibonacci recurrence occurred in Example 8.3. A recurrence relation of the form Sn
asn-I + bSn-2,
where a and b are constants and b # 0, is called a second-order homogeneous linear difference equation with constant coefficients. The word 'homogeneous'
446
Chapter 8 RecurrenceRelations and GeneratingFunctions
indicates that there is no constant term in the recurrence relation. The Fibonacci recurrence is an example of a second-order homogeneous linear difference equation with constant coefficients. Since this type of recurrence relation occurs frequently in applications, it is useful to have a formula expressing Sn as a function of n for a sequence defined by such a recurrence. Theorem 8.2
Consider the second-order homogeneous linear difference equation with constant coefficients for n > 2
Sn = asn-l + bsn-2
that has initial values so and sl. Let r, and r2 denote the roots of the equation x2 =ax+b. Then
(a) If r, =A r 2, there exist constants cl and c2 such that
Sn =
n = 0, 1,2,. (b) If r =r2 = r, there exist constants cl and c2 such that for = 0, 1,2..
clr n
+ c2 rn
Sn = (cI
for
+ nc2 )r'
Proof. (a) If Sn = c rIn+ c 2 rn for n = 0, 1, 2, .. , then for n = 0 and n we must have
=
1
So = C1 + C2
sI = c 1rl + C2 r2. Multiplying the first equation by r2 and subtracting the second yields
car,
= cjr 2 -
-SI
'2SO
= cI(r 2 -r 1 ). Hence since r, 7 r2 , we have r 2 sO -S r2
- r
=
C,.
Thus C2= so-Cl r2So - SI = So r2 -r
so(r2 - r) r2s-
r
r2SO SI r2 - r
r 1so
r2 - ri
We leave it to the reader to show that for these values of cl and c2 , the expression clrn + c2r'1 with n = 0 and n = 1 yields the initial values so and sI.
8.3 Linear Difference Equationswith Constant Coefficients
447
This establishes the base for an induction proof. Assume now that for n = 0, 1, . . ., k we have s, = c1r,+ c2r2. Since ri and r2 are roots of the equation x 2 =ax + b, we have ar, + b = r2 and ar2 + b = r2. Thus +
Sk+1 = ask
bSk-1
= a [cirk
+ c2 rk]
+ b [cirf 1 +
C2rk
= Cl [ark+ brkk1] + C2 [ark + brk = clr, -(ar, + b) + c 2r2 = crk-1 =
k+1
= cirl
2(r)+c 2 rk-
]
1]
(ar2 +b) 2(r)
k+1
+ c 2 r2
Therefore Sn = cjrn + c2 r~'for n = k + 1. It follows from the strong principle of mathematical induction that Sn = clrI + c2r2' for all nonnegative integers n. (b) Note that since b 0 0 in the equation x2 = ax + b (because the given recurrence relation is of the second order), we must have r :A 0. Therefore if Sn = (c, + nC2 )rn for n = 0, 1, 2, . . ., then taking n = O and n = 1 gives SO =
Cl
St = cir + c 2 r.
Hence we see that cl = so
and
C2 =
si
-
r
sOT
r
Again we leave it to the reader to show that for these values of cl and c2 , the expression (c1 + nc2 )rn with n = 0 and n = 1 yields the initial values so and sj. Assume that for n = 0, 1, . . , k, we have Sn = (cI + nc 2 )rn. Since x2
ax-b=(x-r)2 =X 2 -2rx+r
2
,
we have a=2r and
b=-r2 .
Thus Sk+l =
=
ask + bSk-1
a(cl + kc 2)rk + b[ci + (k 2
-
1)c2 ]rk-I
= 2r(c, + kc 2 )r + (-r )[c + (k
-
l)c 2 ]rk-1
= (2c1 + 2kc 2 )rk+l- [c, + (k -)C2]rk+1
= [c, + (k +
1)c2 ]rk+.
The strong principle of mathematical induction therefore implies that s, (c1 + nC2)rn for all nonnegative integers n.
=
448
Chapter 8 Recurrence Relations and GeneratingI un 't ons
The equation x2 = ax + cb in Theorem 8.2 is called the auxiliary equation of the recurrence relation sn := asn-1 + bsn-2. Note that the proof of Theorem 8.2 actually produces the constants cl and c2 that occur in the formula expressing Sn = asn-1 + bsn-2 as a function of n. Rather than memorizing the formulas for these constants, however, we will obtain the values of c, and C2 by solving a system of linear equations as was done in the proof of Theorem 8.2. Examples 8.15 and 8.16 demonstrate this technique. o
Example 8.15 Find a formula for Sn if Sn satisfies the recurrence relation Sn = -Sn-
for n > 2
+ 6Sn-2
and the initial conditions so 7 and s, = 4. The given recurrence relation is a second-order homogeneous linear difference equation with constant coefficients; its auxiliary equation is x2 =-x +6. Rewriting this equation in the form x2+x-6=0 and factoring gives (x + 3)(x
-
2) = 0.
Thus the roots of the auxiliary equation are -3 and 2. Because these roots are distinct, we use part (a) of Theorem 8.2 to obtain a formula for s,. Hence there are constants cl and c2 such fnat s, = cl (- 3 )f + c 2(2)n. To determine these constants, we make use of the initial conditions so = 7 and s, = 4. For n = 0, we have 7 =s,0 = cl(-3)0 + c 2 (2)0 = c] + c 2. Likewise, for n = 1, we have 4 = sl = c (- 3)1 + c 2(2)' = -3c,
+ 2c 2.
Therefore the values of cl an: c2 satisfy the system of linear equations cl + c2
=7
-3c, + 2c2 = 4. A simple calculation gives cl = 2 and c2 = 5. Hence the terms of the sequence defined by the given recurrence relation and initial conditions satisfy S = c1(.
)n + c2 (2)n = 2(-3)n + 5(2)n.
+
8.3 Linear Difference Equations with Constant Coefficients
449
Example 8.16 Find a formula for s, if s, satisfies the recurrence relation Sn
=
6
sn-I - 9 Sn -
for n > 2
and the initial conditions so = -2 and xl = 6. The given recurrence relation is a second-order homogeneous linear difference equation with constant coefficients; its auxiliary equation is x = 6x-9. Rewriting this equation and factoring gives x 2 - 6x+9=(x-3) 2 =0. In this case, the roots of the auxiliary equation are equal, and so we use part (b) of Theorem 8.2 to obtain a formula for Sn. According to this theorem, there are constants cl and c2 such that Sn = (cl + nc2 )3n. To determine these constants, we make use of the initial conditions so = -2 and sl = 6. For n = 0, we have -2
=
= (cl + Oc2 )30 = cl.
Likewise, for n = 1, we have 6 = s= (cl + 1c2)3 1 = 3c1 + 3c 2 . Therefore the values of cl and c2 satisfy the system of linear equations cl -2 3c, + 3c2 = 6. Clearly cl = -2 and c2 = 4. Hence the terms of the sequence defined by the given recurrence relation and initial conditions satisfy sn = (cl + nC2)3n = (-2 + 4n)3 n. In the next example, we use Theorem 8.2 to find a formula for the Fibonacci numbers.
Example 8.17 Find a formula expressing the nth Fibonacci number Fn as a function of n. Recall that the recurrence relation satisfied by the Fibonacci numbers is Fn = Fn-1 + Fn-2
for n > 3,
a second-order homogeneous linear difference equation with constant coefficients. Its auxiliary equation is X2
= x +1.
450
Chapter 8 RecurrenceRelations and GeneratingFunctions
Rewriting this equation in the Form x 2 -x -1 = 0 and applying the quadratic formula, we find that there are two distinct roots, r
=-1
and
2
r2 =
22
Hence Theorem 8.2(a) guarantees that there are constants cl and
)± (
F, := ('I Cj(
)
+ C2 (
C2
such that
2) )n
To determine the values of c I and c2 , we use the initial values F, and F2 to obtain (,>)/' 1
CI= (1+l
.>-
C
2 ±5
(l
and
2
C2
}
2
2)
Solving this system of two equations for cl and c2, we get cl Substituting these values into the formula for Fn above gives I
/1
Fn=/-5 19
+
15
2
1I
}
I
-
and c 2 =Al.
15
2
}n
Our final example provides a solution to a problem known as the gambler's ruin. Example 8.18 Douglas and Jennifer have agreed to bet one dollar on each flip of a fair coin and to continue playing until one cf them wins all of the other's money. What is the probability that Douglas will v in all of Jennifer's money if Douglas starts with a dollars and Jennifer starts with b dollars? To analyze this game, we will obtain a recurrence relation and initial conditions for d, the probability that Douglas will win all of Jennifer's money if he currently has n dollars. Let t = a + b, the total amount of money available to the players. Note that do = 0 because Douglas has no money left, and d, = 1 because Douglas has all of the money. Moreover, if 1 < n
for 1 < n < t
-1.
Multiplying this equation by 2 and rearranging terms gives us d,+l = 2dn - dn-I
for 1 < n
8.3 LinearDifference Equations with ConstantCoefficients
451
Replacing n by n - 1 in this equation yields the second-order homogeneous linear difference equation dn=2 dn-l-dn-2
for 2 < n < t.
The auxiliary equation is x = 2x-1, which has r = 1 as a double root. Therefore Theorem 8.2(b) guarantees that there exist constants cl and C2 such that dn = (cl + nc2 )1n. Using the initial conditions do = 0 and d, = 1, we see that do
= 0 = c 1 + Oc 2
d,
=
1 = c1 + tc2.
The solution of this system of linear equations is cl = 0 and c2 dn = (O+
=
t. Hence
n)(l)n = n =
Therefore at the start (when Douglas has a dollars), the probability of Douglas's winning the game is a a + b' and the probability of Jennifer's winning the game is a = b a+b a+b Hence a player's probability of winning is proportional to the amount of money with which he or she starts the game. +
EXERCISES 8.3 In Exercises 1-24, find an explicit formula for relation and initialconditions.
s,
if So, sI, s2 ,
is a sequence satisfying the given recurrence
..
1. Sn= Sn- + 3, so = 2
5 2. Sn =sn-4, so = 1
3. S,= 4Sn-1, So = 5
4. Sn = 1 5. sn-
5. Sn =
+
6
-Sn-
, so = -4
7. Sn = 3s.-I -8, so = 3
13. Sn
-9,
-2Sn-
= Sn-I
2
=
15. Sn = 8s, I - 16Sn-2, So
17. Sn
=
9
Sn-2,
8.
s
9, SI = 0 =
SO = 1, SI = 9
6, Si = 20
14. 16.
- 10, so = 32
= -2Sn-,
12. Sn =
so = 7
+ 5n-2, SO
Sn= Sn-I
10. sn
9. Sn = Sn-I - 5, So = 100
11. sn =
6.
- 1, so = 4
lSO =
-5
7
-Sn-1 + , so = I 10
sn_
Sn= -2Sn-i 4
- 45, so = 2 - Sn-2, So =
Sn= Sn-2, SO = -1,
18. Sn =
SI =
3,
si =
1
-14
6Sn-I - 9Sn-2, SO = 1, SI = 9
452
Chapter8
19. s, = 21.
Sn =
- 4
4s-2, SO = -4, S
Sn-
lOSnI
Recurrence Relations and GeneratingF.'alcticns
-
25
s5-2,
23. Sn = - 5snl -4Sn-2,
SO SO =3,
-7,
S
=
s =
2
20. S,
=
-
8
snI
= -15
22;. S=
15
24. Sn = 4Sn-
15sn2, SO = 2, Si
-
24
IOSn - 1
-
4
sn-2,
So =
1, S1
sn-2, SO = -3, S
2 0
4
25. In order to combat hypertension, Mr. Lorenzo is to tak2 a capsule containing 25 mg of a drug each morning after awaking. During the day, 20% of the amount of rho drug in the body is eliminated. (a) Write a difference equation and initial conditions giving the amount of the drug in Mr. Lorenzo's body immediately after taking the nth capsule. (b) How much of the drug will there be in Mr. Lorenzo's body immediately after taking the eighth capsule? (c) To what level will this drug eventually accumulate n his body? 26. In Mayville, 90% of the existing dog licenses are reissued each year, and 1200 new licenses are issued. In 2000 there were 15,000 dog licenses issued. (a) Write a difference equation and initial conditions describing the number of dog licenses Mayville will issue n years after 2000. (b) How many dog licenses will Mayville issue in 2009? (c) If the present trend continues, how many dog lice uises can Mayville expect to issue after many years? 27. Michelle has just opened a savings account with an initial deposit of $1000. From the money she earns on her part-time job, Michelle will add $100 to her savings account at the end of each month. If the account compounds interest monthly at the rate of 0.5% per month, how much will it be worth two years from now? 28. Suppose that a corporate executive deposits $2000 per vear for 35 years into an individual retirement account. If interest is compounded annually at the rate of 8%, how much will the account be worth after the last deposit? 29. The Johnson family is considering the purchase of a house costing $159,000. They will make a $32,000 down payment and take a 30-year mortgage for the remainder of the cost. The mortgage compounds interest monthly at the rate of 0.9% per month. How much will the Johnson's monthly payment be under these conditions? 30. An automobile advertisement states that a new Dodgc Stratus LE can be purchased for $175 per month. If payments are to be made for 60 months and interest is charged at the rate of 1.075% per month compounded monthly, how much does this car cost? 31. Write a recurrence relation and initial conditions fir the number sn of sequences of Is and 2s having a sum of n. Use these to obtain a formula expressing Sn as a function of n. 32. Suppose that a bank, in order to promote long-terir saving by its customers, authorizes a new savings account that pays 6% interest on money during the first year it is in the account and 8.16% interest on money that is in the account for more than one year. Interest is to be compounded annually. If you deposit $1100 in such an account and allow the interest to accumulate, how much will the account be worth in n years? Vn(n > 3) be the vertices of a cycle (as defined in Section 3.2), and let cn denote the number 33. Let VI, 2, of distinguishable ways to color these vertices with the colors red, yellow, blue, and green so that no adjacent vertices have the same color. Determine a formula ex pressing cn as a function of n.
34. Use Theorem 8.1 to find a formula for 2n
Sn = so + Sot, + sor +
- + sor,
the sum of the first n + 1 terms of a geometric progression with first term so and common ratio r :A 1. (Hint: The sequence so, SI, S2 .. satisfies the first-order linear difference equation Sn = rsn-I + so with initial term so.)
8.4* Analyzing the Efficiency ofAlgorithms with Recurrence Relations
35. Prove by mathematical induction that if so, sI, s2 , equation Sn = asn-I + b, then
is a sequence satisfying the first-order linear difference
..
Jan(so + c) -c
if a :A 1
so+nb
Sn
453
if a=l
for all nonnegative integers n, where
b C-I
a -1
36. Let
so, SI, s2, .. be a sequence satisfying the first-order linear difference equation s, = as,-, + b for n and define t, for n > 0 by t,= so + SI + S2 + * + Sn. Prove by mathematical induction that
ana+1 (
an+I
-
a -
)so
1,
(ni + l)a + nl
-
(a-1)2
[+
>
ifa
tn =
(n + ) SO+
2)
if a
I
37. Show that if
c, = then the values of crI
+ c 2 r2
r 2 sO -
and
r 2 -ri
for n = 0 and n
=
c2 =
Si -rso
r2-
r
1 equal so and sI, respectively, in the proof of Theorem 8.2(a).
38. Show that if
cl = so and
C2 =
r
r
then the values of (cl + nc2 )r' for n = 0 and n = I equal so and sl, respectively, in the proof of Theorem 8.2(b).
8.4*
+
ANALYZING THE EFFICIENCY OF ALGORITHMS WITH RECURRENCE RELATIONS An important use of recurrence relations is in the analysis of the complexity of algorithms. In this section we will discuss the use of recurrence relations in determining the complexity of algorithms for searching and sorting, two fundamental processes of computer science. In order to keep the discussion simple, we will assume that the list of items to be searched or sorted consists of real numbers, but the algorithms we present can be used with any objects subject to a suitable order relation (for example, names and alphabetical order). To illustrate this use, we will analyze the complexity of the following algorithm for checking if a particular target value is present in an unsorted list. The algorithm proceeds in a natural manner, comparing each item in the list to the target value. It stops if a match is found or if the entire list has been searched.
454
Chapter 8
Recurrence Relations and GeneratingFunctians
Sequential Search Algorithm This algorithm searches a list of n items a, a 2 . a, for a given target value t. If t = ak, for some index k, then t1e algorithm gives the first such index k. Otherwise the algorithm gives k = 0. Step]I Step 2
Step 3
(initialize the starting point) Set j = 1. (look for a match) while j <=' n=' set=' k='j.' otherwise='>
How efficient is the sequential search algorithm? To answer this question, we will count the maximum number of times that the target value can be compared to an item in the given list. Thus wxe will determine an upper bound for the number of comparisons made in using the algorithm. Such an analysis is called a worst-case analysis. Suppose that the list to bie searched contains n items and that we count the maximum number of compar' sons between the target value and an item in the list. In the worst case, we will have, to search the entire list to determine if the target value is present, and this requires comparing the target value to every item in the list. Thus n comparisons ~me required in the worst case, and so the sequential search algorithm is of order at most n. Another way to count the maximum number of comparisons made by the sequential search algorithm Ls with a recurrence relation. For a list of n items, let Cn denote the maximum number of comparisons between the target value and an item in the list. To determine if the target is contained in a list of 0 items requires no compariSCns; hence co = 0. To search a list of n items, we compare the target value to the first item in the list and then, in the worst case, still have to search a list of n -- I items. Thus we see that cn satisfies the recurrence relation Cn :Cn-
+1I
for n>1I
8.4*
Analyzing the Efficiency ofAlgorithms with Recurrence Relations
455
and the initial condition co = 0. A formula for the solution to this first-order linear difference equation can be obtained from Theorem 8.1:
for n >O.
c, = n
Example 8.19 We will determine the number bn of comparisons of items that a bubble sort performs in arranging n numbers a, a2 , .. , an in nondecreasing order. Recall from Section 1.4 that in performing a bubble sort, the first iteration requires comparing an,1 to an, then comparing an-2 to an-1, and so forth until a 2 is compared to a,. After each such comparison, we interchange the values of ak and ak+I, if necessary, so that ak < ak+1. Thus when the first iteration is complete, a, is the smallest number in the original list. A second iteration is then performed on the smaller list a2, a 3, . ., an in order to make a2 the secondsmallest number in the original list. After n -1 such iterations, the original list will be in nondecreasing order. Since the first iteration of a bubble sort requires comparing ak to ak+1 for k = n - 1, n -2, .. 1, we need n - 1 comparisons to determine the smallest item in the original list. After this first iteration is complete, the bubble sort algorithm is continued to arrange the smaller list a 2, a3 , . . ., an in nondecreasing order. But the number of comparisons of items needed to do this is just bi. Thus we see that bn satisfies the recurrence relation bn = bn-, + (n-1)
for n > 2.
Since no comparisons are needed to sort a list containing only 1 item, an initial condition for this recurrence relation is b, = 0. This is the same recurrence relation and initial condition encountered in Example 8.1 and analyzed in Example 8.8. Therefore for n > 1, we have bn
n - n =2
and so the bubble sort algorithm has order at most n2.
+
Divid e-.11unit CR iqu a le- A mori 6thniS
A special type of recurrence relation occurs in the analysis of a class of algorithms called divide-and-conquer algorithms. A divide-and-conquer algorithm is one in which a problem is split into several smaller problems of the same type. These smaller problems are each split into the same number of smaller problems, and so forth, until the problems become so small that they can be readily solved. The
456
Chapter 8
Recurrence Relations and GeneratingFunctions
resulting solutions of the small problems are then reassembled to give a solution to the original problem.
Example 8.20 We will describe a divide-and-conquer approach for determining the largest item in a list of n = 2k items at, a 2 . an. First we divide the original list into two sublists and
ala2,..,ar
ar+1,ar+2,..,an,
where we take r = n in order to make the two sublists of equal size. Then we find the largest item u in the first iublist and the largest item v in the second sublist. The larger of u and v is the largest item in the original list. Thus we have reduced the original problem from one involving a list of 2 k items to two problems, each inv Dving a list of 2 k- 1 items. To solve these problems, we subdivide each of the subsists in half, obtaining four sublists with 2 k-2 items each. Continuing in this manner, we obtain 2k sublists after k such subdivisions, and each of these sublists contains a single item. Since the largest item in a list with one item is obvious, we can ultimately determine the largest item in the sublists. + If, in Example 8.20, the number n is not a power of two, at some stage of the subdivision process we wi[l have a list containing an odd number of items, say a, , a2 , . . . , am.
In order to obtain sublists of roughly equal size we divide the list as follows: al a2,..,ar
and
ar+1,ar+2,..,am,
where
r=
m-1 2
This puts r items in the first sublist and m - r = r + 1 items in the second sublist. Note that the number r is just the smallest positive integer less than m. We call the greatest integer less than or equal to a real number x the floor of x and denote it by Lxj .
+
Example 8.21 Find the floors of the following numbers: 312.5,
-
,
7,
-3.6,
and
1000.
8.4* Analyzing the Efficiency of Algorithms with Recurrence Relations
457
Since the floor of a number is the greatest integer less than or equal to that number, we have
L312.5j and, since 31 <
=
L0]
312,
=
3,
L71
=
7,
L-3.6j
=
-4,
1000 < 32, L[ llO0j = 31.
+
In Example 2.63 we used a divide-and-conquer approach to search for an unknown integer among the numbers 1, 2, . ., 64. We will now present an algorithm that formalizes this searching process.
Binary Search Algorithm This algorithm searches a sorted list of n items a, < a2 < .. < a, for a given target value t. If t = ak for some index k, then the algorithm gives one such index k; otherwise the algorithm gives k = 0. In the algorithm, b and e are the beginning and ending indices of the sublist of a, a2 . . a currently being searched. Step ] Step 2
(initialize the starting point) Set b = 1 and e = n. (look for a match) repeat Step 2.1 (determine the middle value of the sublist) Set m
Step 3
=
b +e)
Step 2.2 (determine the new sublist boundaries) Step 2.2.1 (is t before am?) if t < am Replace e with m - 1. endif Step 2.2.2 (is t after am?) if t > am Replace b with m + 1. endif until either am = t or b > e (was there a match?) if am = t Set k = m. otherwise Set k = 0. endif
458
Chapter8 Recurrence Relations and GeneratingF functions
The following example illustrates the use of the binary search algorithm when the target number is not contained in the given list.
+
Example 8.22 We will apply the binary search algorithm to determine whether the target number 253 is contained in the list of the 500 even integers from 2 through 1000. (Thus as = 2i for 1 <500.)=' in=' step=' 2.1=' our=' first=' value=' cf=' m=' is='>
5DO)j
(I
-(501)] = L250.51 = 250.
Comparing am = 500 to t = 25 3, we find that t =A am, and, in fact, t < am. Therefore we change e to 249; b remains 1. The table below exhibits the working of the algorithm as a sequence of questions and answers. -
-
.
am
Is am = 253?
500
no; greater
II -1+ 249)1-125 [2 j
250
no; less
249
19(1:66+249)
187
374
no; greater
126
186
1I -(16 + 186)= 156 2j
312
no; greater
126
155
(6 2j
280
no; greater
126
139
1I -(I 26+ 139)= 132 [2j
264
no; greater
126
131
- (26+131) [2
128
256
no; greater
126
127
= 126
252
no; less
127
127
=127
254
no; greater
127
126
b
e
1
500
1
249
126
m 1I -iI1+500)I-250 12 + =1
12
=
+ 155)I -140
O 26 + 127 (1
-(27 +127 [2
Since b > e in the last line of -.he table, we find that the target number is not in the given list. +
8.4* Analyzing the Efficiency of Algorithms with Recurrence Relations
459
We will analyze the complexity of the binary search algorithm by counting the maximum number of comparisons c, performed by the algorithm in searching a list of n items. To simplify the analysis, we will assume that n = 2 r for some nonnegative integer r. In step 2.2 we compare the target number to a middle item in the list, am. In the worst case, t # am, and we must search one of the two sublists a, < a2 < < am-, or am+, < < .. < an. Since the longer of these sublists contains n/2 items, we see that c, satisfies the recurrence relation for n = 2r > 2.
Cn = cn/2 + I
Since one comparison is needed to search a list containing a single item, an initial condition for this recurrence relation is cl = 1. We leave it to the reader to verify that a formula for the general term of a sequence defined by this recurrence relation is forn = 2r > 1.
Cn = I +log2 n
In the general case where n is any positive integer, it can be shown that c, satisfies the recurrence relation Cn = CLnh2i+
for n > 2
I
and the initial condition cl = 1. In this case, a formula for the general term of a sequence defined by this recurrence relation is Cn = X+ L1±g2
nj
for n > 1.
(See Exercise 39.) Thus the binary search algorithm is of order at most log 2 n, whereas the sequential search algorithm is of order at most n. Since lg 2
n < />
for all positive integers n, the binary search algorithm is more efficient than the sequential search algorithm when searching a sorted list. The recurrence relation obtained above is typical of that which results from the analysis of a divide-and-conquer algorithm. More generally, if a divide-andconquer algorithm subdivides a problem into p smaller problems, a complexity analysis usually leads to a recurrence relation of the form c, = kcL[/PJ + f (n), where k is a constant and f is some function of n. We will now describe an efficient sorting algorithm called a merge sort that uses a divide-and-conquer approach. The sorting is accomplished by merging two sorted lists into one larger sorted list as illustrated in Example 2.64. We first present a formal description of this merging process.
460
Chapter 8
Recurrence Relations and GeneratingFunations
Merging Algorithm This algorithm merges two sorted-( lists
A: a,
: can
and B: b < h2 <.. < b
into a single sorted list C:
Step 1 Step 2
Step 3
< Cm +n.
(initialization) Set i 1, j = 1, and k = 1. (construct C until either A or B is used up) repeat Step 2.1 (find the next item in C) if ai < bj (a) Set Ck = aj. (b) Replace i with i ± 1. (c) Repla~e k with k + 1. otherwise (a) Set Ck = b. (b) Replace j with j + 1. (c) Rep ace k with k + I. endif until i > m or j -- n (copy end of A onto C if necessary) while i < m (a) Set Ck =
Step 4
-'>
a,.
(b) Replace i wit~i i + 1. (c) Replace k with k + 1. endwhile (copy end of B onto C if necessary)
while j < n (a) Set Ck =b,. (b) Replace j wilih j ± 1. (c) Replace k wiih k + 1. endwhile
We will now use the merging algorithm to sort a list of n items. We begin by regarding the original list as n sublists containing exactly one item. These sublists are necessarily sorted, and we merge them together in pairs. We continue merging in this manner until all the sublists are combined into a single list.
8.4* Analyzing the Efficiency of Algorithms with Recurrence Relations
461
Merge Sort Algorithm This algorithm sorts a list of n items a, a2 , a, into nondecreasing order. In the algorithm, k denotes the number of sublists currently being processed. Step 1 Step 2
(initialization) (a) Regard each item al as a one-item list. (b) Set k = n. (merge sublists) while k > I if k is even Step 2.1 (merge an even number of sublists) (a) Use the merging algorithm to merge sublist I with sublist 2, sublist 3 with sublist 4, . . ., sublist k -I with sublist k. (b) Set k = n/2. otherwise Step 2.2 (merge an odd number of sublists) (a) Use the merging algorithm to merge sublist I with sublist 2, sublist 3 with sublist 4, . . ., sublist k - 2 with sublist k - 1, and sublist k with the empty list. (b) Set k =(
2 2
endif endwhile
The two examples that follow illustrate the working of the merge sort algorithm. +
Example 8.23 We will use the merge sort algorithm to sort the list (19, 14, 11, 18, 30, 17, 6) into nondecreasing order. In step 1 we regard the original list as seven one-item sublists (19), (14), (11), (18), (30), (17), (6) Since each sublist contains only one item, each sublist is in nondecreasing order. There are precisely seven sublists, and so we go to step 2 and apply the merging algorithm to the first and second sublists, the third and fourth sublists, and the
462
Chapter 8 Recurrence Relations and GeneratingFunct,a 's
fifth and sixth sublists. The seventh sublist is merged with the empty list (the list having no items), and so is unchanged. At this point, we have the four sublists (each in nondecreasing order) shown below. (14, 1')), (11, 18), (17, 30), (6) Again we go to step 2, where we apply the merging algorithm to the first and second and to the third and fourth of these sublists. This produces the following two ordered lists. (1i1, 14, 18, 19), (6, 17, 30) When we merge these two lists together, we obtain a single list, and so in step 2 we stop. The resulting list (6, 11,14,17,18,19, 30)
is the original list in nondecreasing order. +
+
Example 8.24 The sorting performed in Example 8.23 can be illustrated with a tree diagram. Each level of the diagram shows one iteration of step 2 of the merge sort algorithm (the application of the merging algorithm to the existing sublists). The final row in Figure 8.9 contains the output of the merge sort algorithm. + (19)
(14
(14, 9,
(11)
(18)
(30)
(I 1,18)
(17)
(17, 30)
(11, 14, 18, 19)
(6) (6)
(6, 17,30)
(6, 11, 14, 17, 18, 19,30) FIGURE 8.9
We will analyze the complexity of the merge sort algorithm by counting the maximum number of comparisons cn performed by the algorithm when the list being sorted contains n = 2ir items for some positive integer r. As we have seen, the merge sort algorithm works by successively applying the merging algorithm to combine two sublists. Recall from Theorem 2.11 that the merging algorithm requires at most -2] + [2-
= n-
8.4* Analyzing the Efficiency of Algorithms with Recurrence Relations
comparisons to merge two lists of length relation Cl= 2cLf/2j+ (n-
463
[nJ. Thus c, satisfies the recurrence
1)
for n = 2r > 2.
Since no comparisons are needed to sort a list containing a single item, an initial condition for this recurrence relation is cl = 0. A formula for the general term of a sequence defined by this recurrence relation can be shown to be c, = I + n(log 2 n-1)
for n = 2r > 1.
(See Exercise 37.) Hence the merge sort algorithm is of order at most n 10g2 n. lii
9iciet
-gv 6n
SSofm
A ,~
.f ,~ msC
In this section we have seen that the bubble sort algorithm has order at most n whereas the merge sort algorithm has order at most n lg 2 n. Consequently, the merge sort algorithm is the more efficient algorithm for large values of n. But is it possible to obtain an even more efficient algorithm? Suppose that we must sort a list of n distinct items a, a 2, . . . , a, into nondecreasing order. In any sorting algorithm, we compare pairs of items ai and aj to determine whether a, < aj or ai > aj. Such a comparison results in one of two possible outcomes. Some comparisons, of course, will not provide new information; for example, there is no reason to repeat a previous comparison or to compare a, to a3 if we know that a, < a2 and a2 < a3 . But no matter how we make k comparisons, there will be at most 2t different patterns for the pieces of information that we might receive. As a result, we can distinguish at most 2 k different orderings with k comparisons. If we are to sort the list, we must obtain enough information to distinguish among the n! different sequences in which the items may be listed. Therefore, to sort the list, we must make at least k comparisons, where 2 > n!.
It can be shown that n! > n l2 . (See Exercises 33-36.) Hence we must have 2k > n12 log2 ( 2 k) > log 2 (n n/ 2 )
k > - 10g 2 n. From this inequality, we see that any sorting algorithm must have complexity at least cn log2 n for some constant c. It follows that, up to a constant factor, the merge sort algorithm is as efficient as a sorting algorithm can be.
464
Chapter 8
Recurrence Relations and Generatingl'unitions
EXERCISES 8.4 In Exercises 1-8 find the floor of the given number. 33
1. 243
2. -34.5
.3.
5. 0.871
6. -2487
7-(-34 + 2) 2
-
7
4. 28.963
8.
2343 26
In Exercises 9-12 make a table as in Example 8.22 showing the working of the binarysearch algorithm. 9. t = 83, n = 100, ai = i
for i = 1, 2, .. , 100
10. t = 17, n = 125, ai = i
for i = 1, 2,.., 125
11. t = 400, n = 300, a, = 3i
for i = 1, 2,.., 300
12. t = 305, n = 100, as = 2i + 100
for i = 1, 2, .. , 10
In Exercises 13-18 draw a diagram as in Example 8.24 ifllstrating how the merge sort algorithm sorts the given numbers into nondecreasing order.
13. 19, 56, 87,42 15. 13, 89, 56, 45, 62, 75, 68
14. 42, 87, 56, 13 lU. 34, 67, 23, 54, 92, 18, 34, 54, 47
17. 95,87,15,42,56,54,16,23,73,39
18. 34, 81, 46, 2, 53, 5,4, 8, 26, 1,0,45, 35
19. Suppose that one sorting algorithm requires 22comparisons and a second requires n lg large must n be for the second algorithm to be more eff cient?
2
n comparisons. How
20. Explain how the merging algorithm treats equal items appearing in the two lists being merged. In Exercises 21-23 prove the statement true for all real numbers x and y. 21. LxJ <x <
xJ + 1.
22. Lxs + LyJ < lx + yj. Show by example that equality need not hold. 23. LxJ + LYj >
Lx + y
-
1. Show by example that equality need not hold.
24. If x is any real number and n is an integer, show that Ix + nj = Lxj + n. 25. Write a recurrence relation and initial conditions for the number en of elementary operations performed by the algorithm for evaluating xn in Section 1.4. Regard an elementary operation as the addition, subtraction, multiplication, division, or comparison of two numbers. 26. Write a recurrence relation and initial conditions for the number en of elementary operations performed by the polynomial evaluation algorithm in Section 1.4. Regarc an elementary operation as the addition, subtraction, multiplication, division, or comparison of two numbers. 27. Write a recurrence relation and initial conditions for the number e, of elementary operations performed by Homer's polynomial evaluation algorithm in Section 1.4. Regard an elementary operation as the addition, subtraction, multiplication, division, or comparison of two numbers. 28. Write a recurrence relation and initial conditions for the number cn of comparisons performed by the divideand-conquer algorithm in Example 8.20. 29. In Exercise 25 find a formula expressing en as a function of n. 30. In Exercise 26 find a formula expressing en as a function of n. 31. In Exercise 27 find a formula expressing en as a function of n.
8.5 Counting with GeneratingFunctions
32. 33. 34. 35.
In Exercise 28 find a formula expressing cn as a function of n. Prove that for any integer k such that 1 < k < n, we have k(n + I- k) > n. (Hint: (n -k)(kProve that if n is an even positive integer, then n! > n' 2. (Hint: Use Exercise 33.) Prove that ne2 1 > fi for all positive integers n. (Hint: Show that (n + 1)2 > (2,/n)2.)
465
1) > 0.)
36. Prove that if n is an odd positive integer, then n! > n/2. (Hint: Use Exercises 33 and 35.) 37. Prove that for all positive integers of the form n = 2 k, 1 + n(log 2 n- 1) is a solution to the recurrence relation Sn = 2sLf/ 21 + (n - 1) for n > 2 with the initial condition s1 = 0. 38. Prove that for all positive integers r,
J
log2
=
1og2(rjl)j
39. Letc andk be constants. Prove that for all positive integers n, k + c [log2 nj is a solution to the recurrence relation Sn = SLn 2J + c for n > 2 with the initial condition s, = k. (Hint: Use the strong principle of mathematical induction and Exercise 38.) 40. The following divide-and-conquer algorithm for sorting is due to R. C. Bose and R. J. Nelson. (See [I] in the suggested readings.) For simplicity, we state it for lists of n = 2 k items, where k is a nonnegative integer. To sort a list of 2 k items, divide it into two sublists, each containing 2k-' items. Sort each of the sublists, and then merge the two sorted sublists using the following divide-and-conquer algorithm. To merge sorted lists A and B, subdivide each list into two (sorted) sublists Al, A2 and B1, B2, respectively, of equal length. Merge Al and B1 into list C and A2 and B2 into list D. Subdivide lists C and D into sublists Cl, C2 and D, D2, respectively, of equal length. Then merge C2 and D, into list E. The final sorted list is C,. E, D2. (a) Let mk denote the number of comparisons needed to merge two lists, each containing 2 k items, by the procedure described above. Write a recurrence relation and initial conditions for Mk. (b) Find a formula expressing mk as a function of k. (c) Write a recurrence relation and initial conditions for bk, the number of comparisons needed to sort a list of 2 k items by the Bose-Nelson algorithm. (d) Use the method of iteration to find a formula expressing bk as a function of k.
8.5
+
COUNTING WITH GENERATING FUNCTIONS We saw in Section 7.1 that the numbers C(n, r) appear as the coefficients in the expansion of (x + y)n. For example, we have (l +x)' =C(n,O)+C(n,l)x+
+C(n,r)x +
+±C(n,n)xn.
Taking n = 5 yields (1 + x) 5 = C(5, 0)+ C(5, l)x + C(5, 2)x2 + C(5, 3)x3 + C(5, 4)x4 + C(5, 5)x 5
=I +5x + 1Ox
2
+
l0x 3
+ 5x
4
+x
5
.
(8.1)
466
Chapter8
Recurrence Relations and Generatinglunctions
Thus the coefficient of Xr in the expansion of (1 + x) 5 is exactly the number of ways of choosing r objects from a set of 5 objects. This makes sense since, as we saw in Chapter 7, the coeffic ent of xr in (1 + x)5 is just the number of ways of choosing the x instead of the 1 from exactly r factors of the product (1 + x)(L + +
±) +
±x)(l + x).
Example 8.25 A boy is allowed to choose two items from a basket containing an apple, an orange, a pear, a banana, and a plum. How many ways can this be done? Since the boy is to choose 2 from a set of 5 items, the number of ways is 10, the coefficient of x2 in (C. l). Of course, the expression (8.1) also reveals the number of ways the boy can choose any other number of items. It is suggestive to replace (I +- x)(l + x)(l + x)(l + x)(1 + x)
by (Oapples + I apple)(O oranges + I oiange)(0 pears + 1 pear)(O bananas + I banana)(O plums + 1 plum)
in order to see the connection between choosing r fruits and the coefficient of Xr in the polynomial (1 + x)5 . +
Example 8.26 A boy is allowed to choose two items from a basket containing two apples, an orange, a pear, and a banana. How many ways can this be done if we consider the two apples to be identical'? Instead of attempting a count by the methods of Chapter 7, we will look for a polynomial similar to that of (:3.1) such that the coefficient of Xr gives the number of ways of choosing r items One that does the job is (1
+xx 2 ) (1 + x) (1 + x) (1 + x). ap ple orange pear banana
(8.2)
It may help to think of the expression (O apples + I apple + 2 apples)(C oranges + 1 orange)(0 pears + I pear)(0 bananas + I banana)
to understand (8.2). The boy may choose 0, 1, or 2 apples and 0 or 1 oranges, pears, and bananas, for a total of two items. The number of ways of doing this is exactly the coefficient of x2 in (8.2). By computing (1 + x + x 2 )(1 + x)(1 +x)(I +x)= 1+4x+7x 2 + 7x 3 + 4x4 + X5,
(8.3)
8.5
Counting with GeneratingFunctions
467
we see that the number of ways is 7. As a check, we list these below. number of apples number of oranges numberof pears number of bananas
2 0 0 0
1 1 0 0
1 0 1 0
1 0 0 1
0 1 1 0
0 1 0 1
0 0 1 1
For example, the third column corresponds to forming x2 by choosing x from the first factor, I from the second factor, x from the third factor, and 1 from the fourth factor. Of course, (8.3) tells much more than that the boy can choose two items from the basket in 7 ways. From it we can deduce that the boy can choose no items in 1 way, one item in 4 ways, two items in 7 ways, three items in 7 ways, etc. Notice that in (8.2) distinguishable items (say an orange and pear) give rise to different factors, while indistinguishable items (the two apples) are included in the same factor. + ( ;' '; If :' '
1'
in'%t'
':
A nk }D
k'D,-,
In Examples 8.25 and 8.26, we have found polynomials with the property that the coefficient of xr gives us the number of elements in a set whose definition depends on r in some way. In Example 8.25, the coefficients of our polynomial count the ways of choosing r items from a basket containing one each of five different fruits, and in Example 8.26 they count the ways of choosing r items from a basket containing two apples and one each of three other fruits. In general, consider an infinite sequence of numbers ao, al, a2 ,
.
where for some integer n we have an+, = an 2 =
0. We say that the
polynomial ao + aix + a2x2 + a3x 3+
+ anon
is the generating function for the sequence. For example, if we define ar to be the number of ways of choosing r items from a basket containing two apples, one orange, one pear, and one banana, then, since there are only 5 fruits in the basket, we have a6 = a7 = .. = 0, and the generating function for the sequence (ar}is (I+x +x 2 )(l +x)(l +x)(l +x) = 1 +4x +7x2 +7x 3 +4x4 +x 5 according to Example 8.26.
Example 8.27 Each of r people wants to order a Danish pastry from a bakery. Unfortunately, the bakery only has 3 cheese, 2 apricot, and 4 raspberry pastries left. We want a
468
Chapter8 RecurrenceRelationsandGenerating.Furc ions
generating function for Id,. }, where dr is the number of fillable orders for the r pastries. In particular, what is d 7 ? The generating function is (1 +X +X
2
+X3)
(1 +X +X 2 ) (1 +X +X
cheese
apricot
2
+X
3
+X
4
),
raspberry
since we must pick 0 through 3 cheese Danishes, 0 through 2 apricot Danishes, and 0 through 4 raspberry Danishes, for a total of r Danishes. It is appropriate that this is a polynomial of degree 9 because the bakery only has 9 Danishes, and so clearly dr = 0 for r > 9. A tedious computation shows that our polynomial equals
1 +3x +6x2 +Qx 4-
11x 4 + I1xI +9X 6 +6X7 +3x 8 +x 9 ,
and so there are exactly 6 fillable orders for seven pastries. As a check, we list them below. number of cheese
3
3
3
2
2
1
number of apricot
2
1 0
2
1
2
number of raspberry
2*
3
3
4*
4*
4*
For example, column 4 corresponds to forming x 7 by choosing x2 from the first factor, x2 from the second factor, and X3 from the third factor. Now suppose that raspberry pastries only come two to a box, and so the bakery will sell them in multiples of two. Then the generating function for the number of billable orders for r pastries becomes (1+X +x
2
cheese
.1 X3 ) (I +X
apricot
2
) (
+
2 2 X+X
4
),
raspberry
since only 0, 2, or 4 raspberry can be bought. Multiplying this out gives 1 + 2x + 4x 2 + 5xl + 6x4 + 6x 5 + 5x6 + 4X7 + 2x + x9 .
For example, since the coefficient of X7 is 4, there are 4 ways to order 7 pastries with the new restriction. These ways are marked with an asterisk in the table above. + 'P;'iwer, S(t~at rif'es In Example 8.27, at most 9 pastries could be ordered. In some situations, however, the number of choices is effectively unlimited. +
Example 8.28 Now suppose that a multinationa corporation builds a large apricot Danish factory next to the bakery. Thus the supply of apricot pastries has become unlimited for
8.5 Counting with GeneratingFunctions
469
all practical purposes. Unfortunately, still only 3 cheese and 4 raspberry pastries are available, and the raspberry must be bought two at a time. We would like a generating function for the number of ways of buying r pastries. Since any number of apricot pastries can be supplied, the natural thing with which to replace the factor (1 + x + x2) in 2
+x
(1 +X
+x
3
+X
)(I +X
2
raspberry
apricot
cheese
+X 4 )
)(I +X2
seems to be (1+x+x 2
(8.4)
+x3)+
where the powers of x go on forever. Of course, there is a problem with this expression, since it indicates that infinitely many quantities are to be added. As long as we never substitute a specific number for x, however, this problem does not arise. We can treat the expression of (8.4) in a formal way, combining it with similar expressions by using the usual rules for adding and multiplying polynomials. For example, we would compute
(I + 2x +5x3 ) + (I +X
+X 3 + .. )
+X2
= (1+1)+(2+1)x+(O+l)x 2 +(5+ l)x3 2
= 2+3x+x
3
+6x
4 +
+X
4 + (0 + 1)x +*
X5 +
and (1 +X =
2
+X
+x 3 )(1 +X
l(l +X +X2 +X 2
+x
x(l+x
+
2
+ X (l +X +X + x 3 (l +X +X =
1+
+
X + X+ +
=
+X 3
2
3
+X
+
)
+ *--)
+ X3+..) 2 2
X + X2+ 2
3
+X +X
3
3
+
X X
3
+*-) + **)
+
X4 +X X4 +X 4
5 5
+
X +
X3+
X +
5+
+
X3+
X±4+
x5 +
1+2x+3x2
(8.5)
+
5 + 4X3 + 4X4 + 4X +*.
If we allow the expression (8.4), then the generating function we desire is F = (I +x+x
2
+x
cheese
3
)(1 +x+x
2
+x 3 +
apricot
)(
+x 2 +x 4 ).
raspberry
We have already multiplied out the first two factors above to get (8.5). Thus
470
Chapter 8 Recurrence Relations and GeneratingFun tons
we have F =(1+2x+3x 2 +4r
3
+4x
4
+4x
+.)(1+x2 +x 4 )
5
5 =( + 2x + 3x2 + 4&3 + 4x4 + 4x + .)I
+ (1 + 2x +3X 2 + 4X3 + 4x4 + 4x5 + =
.)X2
+ (I + 2x + 3X2 +- X3 + 4x4 + 4X5 + 1+ 2x +3X2 +43 +4x + 4X5 + 4X +
x2 ±223+
)X4
+ 4X7 + 4X8 +
34+ 4x5 + 4X62+
4X7 + 4X8+.
+ x 4 + 2x5 + 3X6 + 4X7 + 4X8i+ =
+2x
+4X2 +±6X 3
+8X4+
IOx
5
6
+ I1X
12x7 +12xX +
+
,
where the coefficient of Xr is12 for r > 7. In particular, there are now 12 ways of choosing 7 pastries, since the coefficient of x7 is 12. We list them below. cheese
0 0) 0
1
apricot
3
2 4
raspberry
4 2
:5
7
0 4
1
1 2 6
2 0
2
2
3
3
1 3
5
0
2 4
2
0
4
2
4
3 0
+
In light of Example 8.28, we extend the definition of a generating function given earlier to be an expression of the form ao + c Ix + a2x2 + a3x
+
,
where we now allow infinitely many of the coefficients ar to be nonzero. Such an expression is called a formal power series. We add and multiply generating functions just like polynomials, so that (ao+a1 x+a
=
x
2
2
+ ai;A3+
)+(bo+bix+b2 x
2
3
+bix
)
3 +
(ao + bo) 4- (al + bl)x + (a2 + b2 )x 2 + (a3 + b 3 )x
3
+
and (aO + aix + a2 x2+ a-,A3+
***)(bo + b-x + b2X2 + b3X3
+
2
+
= aobo + (aolbj + abo)x + (aOb2 + abi + a2bo)x
+
) .
Example 8.29 At a restaurant in a ski area, a grilled cheese sandwich costs $2 and a bowl of noodle soup costs $3. Let ar be the number of ways of ordering r dollars worth of grilled cheese sandwiches and bowls of noodle soup. We will find a generating function for the sequence la, ). The desired generating function is X6+ r+X2l+lX4e+
grilled cheek se
nool3
soup
noodle soup
8.5 Counting with GeneratingFunctions
471
Choosing a term from the first factor above determines whether we spend $0, $2, $4, etc. on grilled cheese sandwiches; likewise the term from the second factor corresponds to the number of bowls of soup. Notice that +x2
+X4
+x6 + . 3
2+x -+X +x
=I
)(I 4
+X3
+x
5
+x6
+2x
6
+ X9 + 7
+x
)
+2x8+2x9+x
10
+
For example, since a 8 = 2, there are exactly two ways of spending $8. These are to buy four grilled cheese sandwiches and no soup, or else one grilled cheese sandwich and two bowls of soup. +'
M
Example 8.30 A woman has a large supply of 1¢, 2¢, and 3¢ stamps. (All the 1¢ stamps are identical, etc.) Find a generating function for {ar}, where ar is the number of ways she can arrange exactly 3 of these stamps in a row on an envelope so that their total value is r cents. What if any number of stamps can be used? Since the first stamp will be worth one, two, or three cents, and likewise for the second and third stamp, the generating function for far }is (x + X2 + X3 )(X + X2 + X3 )(X + X2 + X) = (X + X + XY)
=X +3x 3X4 + 6x 5 + 7x6 + 6x7 + 3x8
+ X9 .
For example, the 6 ways to total 5¢ are 113, 131, 311, 122, 212, and 221. In the same way, if 4 stamps are to be used, the corresponding generating function is (x + x 2 + X3)4. If either 3 or 4 stamps are allowed, then the appropriate generating function is (X + X2 + X3 )3 + (x + x2 + X3 )4 ,
since the coefficient of xr in this expression will be the sum of the number of ways of totaling r cents with an arrangement of 3 or 4 stamps. What if we wish to count all arrangements of stamps totaling r cents, no matter how many stamps are used? Since we wish to allow 0, 1, 2, .. stamps to be used, the corresponding generating function is + (X + X2 + X3 ) + (X + x 2 + x 3 )2 + (X + X2+ =1
+X+X
+
+X
2
X3 )3 +
X
+2x
3
+
3
x
+3x
4
+ 3x
4
+2x
5
+
x6
+ 6x + 7x 6 + 6x 7 + 3x
= 1 +X +2X 2 + 4X 3 + 7X4 +.
5
8
+ x
9
472
Chapter 8 Recurrence Relations and GeneratingIvun -tons
For example, the 4 ways of arranging 3¢ worth of stamps are as 3, 12, 21, and 111.
c+
EXERCISES 8.5 Consider the generatingfunctions
B =1 +2x +4x4 +x 5 , D = I +x+x 2 +x 3 +.,
A = I +x +x 2 , C = 1-x
2
+x
4
,
3
E =1 +x ± x 6 x'+-x +
,
F =I 1x
2
+x
X 3 +x
4
In Exercises 1-12, write each indicatedexpression in the form a/ 4 aix + a 2x
+ a3 x3 +
*.
If the expression is a polynomial, then compute it compleley; otherwise compute it through the x7 term. 1. A+ B
2. B+C
5. B+D 9. EC
6. C+F 10. DC
.3. AB '7. AD I1. DE
4. AC 8. CF 12. FD
In Exercises 13-22 give a generatingfunctionfor the sequence {ar 1,and then write it in the form ao + a x + a2 x 2 + a3x3 +
..
through the x 6 term.
13. Let ar be the number of ways of taking r drinks from a refrigerator containing 3 Cokes and 5 Pepsis. 14. Let ar be the number of ways of choosing r cars from a rental agency that has a Buick, a Dodge, a Honda, and a Volkswagen available. 15. Let a, be the number of ways of choosing r jellybeans rom a basket containing 3 licorice, 4 strawberry, and 2 lemon jellybeans. 16. Let ar be the number of ways of buying r batteries from a store that has 3 C batteries, 4 D batteries, and 6 AA batteries, if the AA batteries are only sold in sets of two. 17. Let ar be the number of ways of buying r chicken parts from a grocery that has 4 wings, 3 breasts, and 5 drumsticks, if the drumsticks are wrapped in a package of 2 and a package of 3, and the packages cannot be broken up. 18. Let a,- be the number of ways of spending r dollars on posters, if four identical $1 and three identical $2 posters are available. 19. Let ar be the number of ways of ordering r glasses of liquid, if 3 glasses of milk and an unlimited supply of water are available. 20. Let a, be the number of ways of collecting r ounces of clams and mussels from a beach, if a clam weighs 3 ounces and a mussel 2 ounces. 21. Let ar be the number of ways of choosing r oak and nmple leaves for a scrapbook, if the book must contain at least 4 oak leaves and at least 2 maple leaves. 22. Let a, be the number of ways of buying r baseball cards, if I Mickey Mantle, 1 Stan Musial, 1 Willie Mays, and an unlimited supply of Pete Rose cards are available.
473
8.6 The Algebra of GeneratingFunctions
In Exercises 23-26find the generatingfunctionfor far }I 23. Let ar be the number of ways of choosing r books from seven different mathematics books and five identical copies of Peyton Place. 24. Let ar be the number of ways of spending r dollars on three different $7 books (there is only one copy of each) and an unlimited number of identical $9 books. 25. Let a, be the number of ways of catching r pounds of bluegill, catfish, and bass, if a bluegill weighs 1 pound, a catfish weighs 3 pounds, and a bass weighs 4 pounds. 26. Let a, be the number of solutions to a + b
= r,
where a and b are elements of the set (I, 2, 4, 8,
In Exercises 27-32 suppose the indicatedgeneratingfunctionF is written as ao + alx a formula for ar in terms of r.
27. F = (I+X
2
29. F=(I+x+x
30. 31.
F = (1 +x
28. F=((+x+x 2 +x
+x2+x3 + .)2 +x
+x 2
2
F=(I-x+x-x
+x
3
3 3
++
+ a3 x
3
. .
+
.
Find
+ *)(1-x)
)(1+x)
+ .*.)(1-x +x 2 +
3
+ a2 x
2
)(1+x)
x3 +
) 32. F=(I+X+X2+X3+ .. )3
33. Let ar be the number of solutions to p + q = r, where p and q are prime numbers. Find a generating function for {ar,) and write it through the x1l term. It is an unproved conjecture (called the Goldbach conjecture) that a, > 0 whenever r is even and greater than 2. 34. Let ar be the number of solutions to 2k + p = r, where k is a nonnegative integer and p is a prime number. Express a generating function for far) , and write it out through the x to term. What is the smallest r > 2 such that ar = O? 35. Let ar be the number of solutions to a2 + b2 + c2 + d2 = r, where a, b, c, and d are nonnegative integers. Express a generating function for {ar1,and write it out through the x1I term. (It can be proved that ar > 0 for all r > 0.)
8.6
+
THE ALGEBRA OF GENERATING FUNCTIONS We saw in the previous section how generating functions, even those with infinitely many terms, can be added and multiplied just like polynomials. With these definitions, generating functions obey the same algebraic laws as polynomials. Examples are the associative and commutative laws of addition and multiplication and the distributive law. The generating function Z =0+Ox+Ox
2
+Ox
3
+
takes the role of additive identity; that is
Z+G=G+Z=G
474
Chapter 8 Recurrence Relations and GeneratingFunctions
for every generating function G. Likewise the generating function U = 1 =1 I + Ox + Ox
2
+ Ox
3
+.
is the multiplicative identity; so that UG = GU =G for every generating function G. We define the subtraction f generating functions by (ao + aix + a2x + a3x3+± =
(ao
-
bo) +
)-(bo + bix + b2x2 + b3 x 3 + 2
)
3
bI)x + (a2 - b2)x + (a3 - b3)x +
(c', -
Division presents more of a problem. The key is the existence of inverses; that is, given a generating function (,. we would like to find another generating function G-1suchthatGG-1 = 1,the multiplicativeidentity.Suchaninverseoftenexists; for example (1-x)(1
x+x
2
+x 3
1x+x +)
2
+x 3 +
x-2-x
_
3
-
=1.
Thus (l-x Fx 2 +x 3 +
)-I =1-x,
and (1 y)l = 1
+x+x2 + x3 +
In fact, a similar computation shows that (1-G)(1 + G + G2 + G3 +
)=
(8.6)
for any generating function G having 0 as its constant term. For example, setting G = -x in (8.6) gives (I1
(IA.)
x + x2-_x3 +
)= 1
and taking G = 2x in (8.6) gives (1 -2r,(
+2x+4X 2 +8x
3
+
)= 1.
In Example 8.30 we found that the generating function for the number of ways of arranging a sequence of 1¢, 2¢ and 3¢ stamps totaling r cents is
475
8.6 The Algebra of GeneratingFunctions
By taking G = x + x 2 +
x3
in (8.6), we can write this as (1 -X -X
2
- X)-l.
It turns out that all that is needed for the inverse of a generating function ao + aix + a2x2 + a3x3 + to exist is that ao # 0. Theorem 8.3
Suppose that
G = ao + aix + a 2x where ao0
2
+
a3x 3
+
,
0. Then there is a unique generating function H such that GH
=
1.
Proof. We are interested in a generating function H = bo + bix + b x2
2
+ b3x
3
+
such that
GH = (ao + alx +
a 2x2
+ a3x +
)(bo + bjx + b2 x2 + b3 x3 + 2
= aobo + (aobi + albo)x + (aob 2 + alb1 + a2bo)x
)
+'
= 1.
This leads to the following equations. aobo = 1 aob1 + albo = 0
anb2 + alb, + a2 bo = 0
The first equation is true if and only if bo = a0 1 and aO- exists since ao &0. Then plugging this value of bo into the second equation determines b1 uniquely. Likewise, the third equation can be solved for b2 after our previously determined values of bo and b, are substituted in it. Continuing in this way, we see that a unique sequence bo, bl, b2 , b 3 ,.. is determined such that (ao+ alx + a 2 x +
+
a3x +3
)(bo + blx + b2x 2 + b3x 3 +
) =1.
Example 8.31 Let us try to find the inverse of the generating function 1 + 2x + 3X2 + 4x3 + We wish to determine a sequence {br I such that
(I+ 2x + 3X2 + 4x3 + .)(bo + bix + b2 x2 + b3 x3 + .)=
1.
476
Chapter 8
Recurrence Relations and GeneratingFunctions
Equating the constant terms on both sides of this equation gives lbo = 1, and so bo = 1. Likewise the coefficients of x must be the same on both sides, so lb, + 2bo = b1 + 2 =0. This implies b, =-2. Equating the coefficients of x2 yields lb 2 + 2
+ 3bo= b2 -4 + 3 = 0,
and so b2 = 1. The next equation is lb3 + 2b2 -- 3b, + 4bo = b3 + 2-6 + 4 = 0; so b3 = 0. The reader should check that b4 = 0 also. In fact, it can be proved that the rest of the coefficients b, are all 0, and so
(1 + 2x + 3x' + 4 X3 +
., )-I
= 1-2x +x 2.
The details are left for Exercis. 33. The same result could have been reached by another route if we assume that generating functions sati dy some familiar laws for exponents. According to Exercise 27 of Section 8.5, we have )2 = I
( + X + x2 J- a
+ 2x + 3x2 + 4X3
+
Thus 2 (l+2x±3x+4x'1
*
1
2
=[(l+x+x
+x
3
= [(1+x +x2 +x
3
.. ) 2]-1
+.
-]2 1
2
=
[1
=
1- 2x +x2
-x
where the next-to-last equality comes from (8.6).
+
+
Generating functions are an extremely flexible tool for studying combinatorial sequences, and we will only be able to touch on a few of their applications here. Given a recurrence relat vcn, it is often possible to use it to construct the generating function for the corresponding sequence. This is illustrated by the next example. +
Example 8.32 Consider the sequence {mn,} cf Example 8.2, which concerned the Towers of Hanoi game. The number m, is the minimal number of moves needed to transfer
8.6 The Algebra of GeneratingFunctions
477
a stack of r disks to an empty peg. We found that ml = 1 and mr = 2mr-I + 1 for r > 2. In fact, if we define mO to be 0, then our recurrence relation holds for r > 1. Let us define M to be the generating function for {mr }, so that M = mO + m x + m 2x 2 + m 3x 3 +
Then since mO = 0 and mr = M
=
2
** .
mr-1 + 1 for r > 1, we have
0 + (2mo + l)x + (2m, +1)x 2 + (2m
2
+ 1)x3 +
= 2mox + lx + 2mlx 2 + 1x 2 + 2m 2 x 3 + 1x 3 +
= 2x(mo + mlx
+m
2x2
= 2xM +x(l +x +x 2
+
)+ x + x 2 + x3 +
m 3x3 +
+
*.
).
Then M - 2xM = x(l + x + x2 +.), or M(1 -2x)
= x(l + x + x2 +
where the last equation follows from setting G M=
)=x(l-x)-i, x in (8.6). Thus we have
=
x (1 -2x)(1-x)'
where we have indicated the inverses by the usual fraction notation. In order to get a formula for the coefficients of M, we will express the fraction on the right in the form a 1-2x
b 1 -x'
where a and b are constants.' We have x (1- 2x)(1 - x)
a I - 2x
b 1- x
a(l - x) + b(1 - 2x) (1 - 2x)(1 - x)
(a + b) + (-a - 2b)x (1 - 2x)(1 - x) and therefore by equating coefficients in the numerators we get a + b = 0 and -a - 2b = 1. These equations are easily seen to have the solution a = 1 and b = -1. Thus M2
x (1- 2x)(1 - x)
1 I - 2x
'Calculus students may recognize the method of partial fractions.
1 -= I-x
478
Chapter 8 Recurrence Relations and GeneratingFun-ions
But then (8.6) yields M = (1-2x)-1 -(I-x)-1 =
(I + 2X + 4x 2+
8x3 +
-(I
2
= (1 -1) + (2 -- 1)x + (4 -)x
+ x + x 2+
x3 +
.
+ (8-1)x 3 +.
Thus we see that mr, the coefficient of xr, is 2 r - 1. This agrees with what we found in Section 8.2. + The method of Example S.32 can be used on any first-order linear difference equation. In fact, an alternate proof of Theorem 8.1 can be based on it; the details are left for the exercises. Generating functions can also be applied to higher order recurrences, as in the next example. 4
Example 8.33 Let us consider the recurrence relation So = 0,
S, = 1,
for n > 2.
Sn = 2sn-I -Sn-2
If S is the generating function for this sequence, then 2 2 .x
S = So+ SIX+-
= so + six +(2s
±s 3 X3 + * 2
- so)x
1
+ (2s 2
2
=O+x+2x(sX+S2X + = x + 2x(S
* )-X
3 sO)x +* 2
-
(So+SIX+
s,)) - x 2 S
-
= x + 2xS-
'S.
Thus we have S - 2xS+x
2
S =x,
S(1 -- Id + x2) = x, 2
S =x(l-2x+x
)-1.
But in Example 8.31 we fourd that the generating functions 1-2x+x 2
and
1+2x+3x2+4x 3 +
are inverses of each other. Thus S
=
A (I+ 2x + 3x 2 + 4x 3 +
=.A =4xnn 4-2x
from which we see that Sr
=
+3x
.e,
r for all nonnegative integers r.
*)
479
8.6 The Algebra of GeneratingFunctions o
Example 8.34 Let us use a generating function to find a formula for s, where so = s, = 1, and Sn =-s,-, + 6 sn-2 for n > 2. If S is the generating function for {ns, then S =So + SIX +S 2 X2
+
S3X3 + * -
+ X + (-s1 + 6so)x2+ (-S2 + 6sl)x3 +
=
= l+x-x(sIx+s
2
x
2
2
+*.-)+6x
(so+sSX
--)
2
=
1 +x -x(S -so)+6X S
=
1+x -x(S- 1)+6x 2 S
=
1+2x-xS +6x 2S.
Thus S + xS - 6x2 S = 1+ 2x, S(1+x - 6x2 ) = 1+ 2x,
S(l + 3x)(1 - 2x) = 1+ 2x, and so C=-
1 + 2x (1 + 3x)(1 -2x)
We will attempt to find constants a and b so that the last fraction has the form a
1+3x
b 1 - 2x
This gives 1+2x (1 + 3x)(1 - 2x)
a b +3x '1 - 2x
a(1- 2x) + b(1 + 3x) (1 + 3x)(1 - 2x)
and so a + b = 1 and -2a + 3b = 2. Solving these equations simultaneously yields a = 1 and b = 4 -5. Now we use (8.6) to write 1 1 5 1 + 3x
4 1 5 1- 2x
- 3x +9x 2 - 27x 3 +..) + -(1 + 2x + 4X2 + 8x3 +
=(l 5
5
Picking off the coefficient of x' tells us that 1 5
4 5
S = -(-3)n + -(2)n.
).
480
Chapter8
Recurrence Relations and GeneratingI un atons
For example, we have so + 4 1, SI = 0 S2 =(9) + 5(4) = + -5. O
(-3) + 4(2) = -3 +
1, and
The result of Example 8.34 could also have been found using Theorem 8.2. In fact, a proof of Theorem 8.2 using generating functions is sketched in the exercises at the end of this section. +
Example 8.35 An embassy communicates wi th its home country in code words consisting of a string of n decimal digits. In order to catch errors in transmission, it is agreed that the total number of 3s and 7s in each word should always be odd. How many code words are possible? Let Sn be the number of allowable words of length n. We can get a recurrence relation for Sn as follows. Consider a word W of length n + 1 counted by sn+1. It either ends in a 3 or a 7 or noi. If it ends in a 3 or a 7, then the word W* of length n formed by deleting the last digit from W must have an even number of 3s and 7s. Since there are Ion strings of n decimal digits, the number of such words W* is ion - s, Thus the number of possible words W of this form is 2(10 - Sn), since the last digit of W can be 3 or 7. Now suppose that the allowable word W ends in a digit other than 3 or 7. Then deleting its last digit leaves a word counted by Sn. Since there are 8 possibilities for the last digit of W, the number of allowable words of this form is 8 sn, By combining the results of the last two paragraphs, we see that Sn+-
= 2(10'
-SO)
+ 8s, = 2 10' + 6s,
for n > 1. Clearly so = 0, since the empty string cannot have an odd number of 3s and 7s. Using this relation allows us to compute the following table. n
Sn
0
0
1
2
2
2. 10 +6
3
2 102 + 6 32 = 392
100+6 0= 2 2 = 32
For example, S2 counts the number of 2-digit strings with exactly one 3 or 7. Since we can use either a 3 or 7, since this can be either the first or second digit, and since there are 8 choices for the remaining digit, the number of such strings is 2 . 2. 8 = 32. Now we will use generating functions to get an explicit formula for sno. Let S be the generating function for {sn }, so that S = SC + SIXi + S2X
+ S3X
+3
.
481
8.6 The Algebra of GeneratingFunctions
Then we have S = so + (2 100 + 6so)x + (2 101 + 6s1 )x2
+ (2
102
+ 6s2 )x3 +
= so + 2x(10 0 +lOIx + 10 2X2 + . ) + 6x(so +sIx +s 2x 2 + s 3x 3 2
= 0 + 2x(1 + lox + (lOx) +
+
)
)+ 6xS
= 2x(1 - lOx)- + 6xS. Solving for S yields S(1 - 6x) = 2x(1 - lOx)-f, or 2x
(1 - 6x)(1 - lox) We will find constants a and b such that 2x (l - 6x)(1 - lox)
a 1 - 6x
a(l-lox) + b(l-6x) (1 - 6x)(1 - lox)
b 1 - lox
Equating numerators gives the equations a + b = 0 and easily find that a =1- and b =1. Thus
-IlOa
-
6b
2. We
11 S=--(1-6x)-1 + -(1-lOx)2 2 =
-[(1
- lOx)- -(1 - 6x)-1]
2 2
=I-[(1+10x+l00x 2
+
)-(1+6x+36x 2 +
A)],
from which we see that the coefficient of xr in S is lor - 6r Sr
For example, 52 = 102 36 = 32, and with our earlier computations. #
2 S 3 = 10002 216
- 392. These values agree
EXERCISES 8.6 In Exercises 1-10 find the inverse of the given generatingfunction. 1. 1-3x
2. 1-5x 2
3
4. 1-3x+9x -27x ±+** 7. 1-x-x 2 10. 1 + x
2
5. 1+x 8. l+x+x 3
3. 1+2x+4x 2 +8x 3
6. 1+2x 9. 2+6x
3
++
482
Chapter8
Recurrence Relations and GeneratingF'untions
In Exercises 11-20 let S be the generatingfunction of the sequence {Sn 1.Find an equation satisfied by S as in Examples 8.32 through 8.35, and solve for S. 11. so= 1,
and
Sn =2s,_- +1
12. so = 3,
and
Sn
for
=-Sn-I + 2
n >1
for
n > 1
13. so = 1, si= 1,
and
Sn = 2s5 i- S-2
for
14. so = 2, s, = 1,
and
Sn = Sn - 3s,-2
for
15. so=-1, s, = 0,
and
Sn =-Sn-1 + 2 sn-2
16. so = 0, si=-2,
and
Sn = 3s,_1 + S,-2
for
Sn = S,-, + 3 s,-2 + 2
18. so =-3, s, = 2,
s = 4sn- -5sn-2- 1
and
20. sO= 1, sI = 1, s 2 = 5,
and and
Sn = Sn Sn
=
t > '2 for
17. so =-2, sI = 1, and 19. so = 2, sI= -1, S2 = 1,
it > 2 n >2 n - 2 for
n >2
for n > 2
3
Sn-2 + 3n-3
2Sn-1 + S,-
2
for for
-- Sn-3
n > 3 n >3
In Exercises 21-26 find constants a and b such that the given equations are identities in x. a x a h 2 21. 23
(1-x)(1 + 2x)
=
1-x
+
22.
1 +2x
1 + 3x a + b (1 + 2x)(1-x) 1 + 2x 1-x 1 +1x a b 2.= + T__+x22'). 2 1 + 2x (1 + 2x) (1 + 2x) 2
24
=
(1 + x)(1 + 3x) 1-x (I + 2x)(1-3x) 3-x a
~
2.
X) 2
=
1+x
+
b 1 + 3x
a + b I + 2x 1-3x b 1 + ( X) 2
In Exercises 27-32 give aformula for S, if {Sn ) has the given generatingfunction S.
1
x3
27. S=
I + 1 1- 2x I1+x
28. S =
29. S=
I+ 1-2x 2 2
3 + 1+2x 1-x 2 1 + 1+ 2 32. S = 1x ~1 -x
31. S = I
3x
I-
3
S=
430. +15x 1
33. Suppose that bo = 1, bi =-2, b2 = 1, and bn + 2bn-I + 3b,-2 + mathematical induction on n that bn = 0 for n > 3. In Exercises 34-36 assume that so is given, and that s, a 7# 1.
=
+ (n + 1)bo = 0 for all n > 1. Prove by
aSn-I + b for n > 1, where a and b are constants and
34. Show that if S is the generating function for {Sn ), then S =so+axS +bx(1-x)-1. 35. Show that so + (-so + b)x (1-ax)(1-x)
k1 1-ax
_
k2
1 -x
where k, = so +-a-1
.md
k2 =
a -1
HistoricalNotes
483
36. Show that Sn =(So+a
i)an-
i
for
n
> 0.
In Exercises 37-43 consider the second-order homogeneous difference equation 8n = asn-1 + bSn-2,
where so and s, are given. Assume that x 2 -ax 37. Show that r,
+ r2 =
a, rr
2 =
-b,
b = (x - rl)(x
-
and 1 - ax
-
-r 2 ).
bx2 = (1- rx)(l
r2x).
-
38. Show that if S is the generating function for Is, , then S = so + six + ax(S
-
so) + bx 2S.
In Exercises 39-40 assume that r1 :$ r2 . 39. Show that there exist constants cl and c2 such that so + (s1 + aso)x (1-
40. Show that s,
=
cl
rx)(1 -r 2 x)
C2
1-rx
1 -r 2 x
clrn + c2 r' for n > 0, where cl and c2 are as in Exercise 39.
In Exercises 41-43 assume that r, = r2 = r :A 0. 41. Show that there exist constants kAand k2 such that so + (s1 +aso)x (1 -rx)
2
k1 1-rx
k2
(1-rX)2
42. Show that s, = kjr' + k2 (n + l)r' for n > 0, where k, and k2 are as in Exercise 41. 43. Show that there exist constants cl and c2 such that s, = cjr' + nC2r' for n > 0.
HISTORICAL NOTES Recursion has been used from Greek times. Its formal development, however, dates back only to the past two and one-half centuries. Archimedes had two relationships that involved recursion. If a, and An are, respectively, the areas of the polygons with n sides inscribed in and circumscribed about a circle, they are related by the formulas a2n = V
A
and
A 2n
2Ana2
AI + a2r
In a like manner, when p,~and P, are the perimeters of the regular polygons inscribed in
484
Chapter8
Recurrence Relations and GeneratingFuinttions
and circumscribed about a circle, wve have P2n = V/ 0a P2 n
and
P2n = 2Pp
Pn + Pn
Starting with a regular hexagon, Archimedes developed reasonable estimates for the value
of 7r [73]. In his Liber Abaci of 121)2, Leonardo of Pisa (c. 1175-1250), known as Fibonacci, provided the first systematic introduction for Europeans to the Arabic notation for numerals and their algorithms for arithmetic. As part of the text, Fibonacci presented his famous recursion problem dealing with generations of rabbits. While he did not develop any of the many relationships stemming rom the recursion, the pattern was named in his honor by the French mathematician Edoliard Lucas in the late 1800s. The formula in Example 8.17 for the Fibonacci numbers was not derived until 1718,
Edouard Lucas
.-T I
1t
4 a:
-.
- A,17cAX. --
A_
-
.
I
.v
.
L- Id-
--
Abraham
when Abraham De ivioivre 1,00/-1 /34) obtained me result with an approach using a generating function. Extending the general techniques, Leonhard Euler (1707-1783) advanced the study of the partition, of integers in his 1748 two-volume opus Introductio in Analysin Infinitorum. Pierre Simon de Laplace (1749-1827) also published a significant amount of work on generating functions and their applications in his 1754 work The Calculus of GeneratingFunctions. TI e mathematical analysis of Tower of Hanoi puzzle and its general closed form solution via generating functions is credited to Lucas in his 1884
De Moivre
work Recrc'ationsMathematiques [74].
SUPPLEMENTARY EXERCISES In Exercises 1-5 determine S5 if So, S5, S2,
..
is a sequence satisfying the given recurrence relation and initial
conditions. 1.
Sn =
3. s,
3
sn
i
= 2ns,-
+ n2
for n > 1, so = 2
for n > 1, so = I
5.Sn = nsn- -Sn-2
2-
Sn = (-l)n + s,_]
4i s, = 3(s,_1 + Sn-2)
for n > 1, so = 1
for n > 2, so = 1,s1 = 2
for n > 2, so = 1, s- = 1
6. Suppose that $20,000 is deposited in an account with an annual interest rate of 8% compounded quarterly. Each quarter there is a withdrawal of $200 immediately afie- interest is credited to the account. Write a recurrence relation and initial conditions for vn, the value of the ac count n quarters after the initial deposit. 7. A data processing position pays a starting salary of $16,000 and offers yearly raises of $500 plus a 4% cost of living adjustment on the present year's salary. Write a recurrence relation and initial conditions for sno the salary during year n. 8. An ecology group bought a printing press for $18,000 to print leaflets. If the resale value of the press decreases by 12% of its current value each year, write a recurrence relation and initial conditions for v, the resale value of the press n years after its original purchase. 9. Write a recurrence relation and initial conditions for the number Cn of n-symbol codewords composed of dots and dashes with no two consecutive dashes. 10. Suppose that, at the beginning of an experiment, there are 500 cells in a sample and the number of cells is increasing at the rate of 150% per hour. Write a rectune ice relation and initial conditions for ca, the number of cells in the sample n hours after the start of the experiment.
Supplementary Exercises
485
11. Suppose that the efficiency en of a worker on an assembly line processing n units per minute is equal to the efficiency of the same worker processing n - 1 units per minute minus an incremental loss for the nth unit. Assume that the incremental loss is inversely proportional to n2 . Write a recurrence relation describing the efficiency of the worker. 12. Twenty years ago, an individual invested an inheritance in an account that pays 8% interest compounded quarterly. If the present value of the account is $75,569.3 1, what was the initial investment? 13. Prove by mathematical induction that 2n 2 + 2n is a solution to the recurrence relation Sn = s, I + 4n for n > 1 with the initial condition so = 0. 14. Prove by mathematical induction that 2 n-l + 2 is a solution to the recurrence relation s, = 2sn - -2 for n > 2 with sI = 3. 15. Prove by mathematical induction that (n + 1)! -1 is a solution to the recurrence relation s, = forn > I with so = 0.
-s,_
16. Prove that 2' + 2(3n) + n - 7 is a solution to the recurrence relation s, = 5s, Iwith the initial conditions so = -4, sI = 2.
21 for n > 2
Findan explicitformulafors if so, s, given in Exercises 17-20.
17. Sn 18. s,
= 3Sn-I - 12 = Sn-I
+7
Sn
=
7
5n-I -
-
is a sequence that satisfiesthe recurrencerelationandinitialconditions
for n > 1, so = 5 for n > 1, so = 2
19- Sn = 4sn-, -4Sn-2
20.
S2, ..
6s,-2 + 2n
+ n n!
0
' Sn-2
for n > 2, so = 4, s 1 = 6 for n > 2, so = -2, SI = -I
A Lucas sequence is a second-order homogeneous linear difference equation with constant coefficients that is similarto the Fibonacci sequence. The generalLucas sequence can be defined as p Ln
=
for n = I for n = 2 forn > 3,
q
L_
+
L-
2
where p and q are integers. 21. Find the first 10 terms of the Lucas sequence with initial conditions L I = 3 and L2 = 4. 22. Compute (to three decimal place accuracy) the quotient Li+, for the values obtained in Exercise 21. Compare the resulting quotients with the value of , the golden ratio. 23. Prove that if Ln is a Lucas sequence with initial conditions LI = p and L 2 = q, then
Ln = qFn-1 + pFn-2
for all n > 3. 24. Solve the system of recurrence relations Sn = t,
8 6
sn-1 -9tn-I sn-I
-
7
tn-I
with the initial conditions so = 4, to = 1. (Hint: Substitute Sn = 3Un + and Vn -)
vn
and
tn =
2
Un + vn,
and solve for un
486
Chapter 8
Recurrence Relations and GeneratingIFun .ions
25. Let k be a positive integer and a, a2, .. , ak be real numbers such that ak # 0. We call the equation xk = a1 xk-I + a2 xk-2 + + ak the auxiliary equation of the recurrence relation (8.7)
S- = alns-I + 12S,-2 + ***+ akSn-k Prove that rn is a solution of (8.7) if and only if r is a root of the auxiliary equation.
26. Prove that if u, and
Vn satisfy (8.7) for n > k, then for any constants b and c, bun + cv, also satisfies (8.7) for
n > k.
27. Find an explicit formula for S, if SO, S1 , S2 .. is a sequence that satisfies S, = 3s,-, + 1S,-224Sn-3 for n > 3 and the initial conditions so = -4, 5, =9. 9.2 = 13. (Hint: Proceed as in Theorem 8.2(a) using the results of Exercises 25 and 26.) 28. Findanexplicitformulaforsnifs
and the initial conditions Exercises 25 and 26.)
So =
o ,s 1 ,s2,
5, si
=
.. isasequer-cethatsatisfiess = 6s,-I-l2s,-2+8s,-3forn > 3 6, S2 = -20. (hint: Proceed as in Theorem 8.2(b) using the results of
29. Find an explicit formula for s, if sO, SI, S2, .. is a sequence that satisfies s, = 3Sn-2 + 2s,-3 for n > 3 and the initial conditions so = 4, sl = 4, S2 = -3. (Hint: P'roceed as in Theorem 8.2(b) using the results of Exercises 25 and 26.)
30. A linear inhomogeneous difference equation with constant coefficients is a recurrence relation of the form S- = als.-I + a2 S.,--2 +
(8.8)
**+ akSn-k + f(n),
where f is a nonzero function. Prove that if u, satisfy s (8.8) for n > k, then every solution of (8.8) has the form un + v, where v, satisfies (8.7) for n > k. (Hint if wn is a solution of (8.8) for n > k, consider Wn-, U,) 31. (a) Find values of a and
b
so that an + b satisfies tie recurrence relation S, = S,-i + 6Sn-2 + 6n
-
I for
n > 2.
(b) Use Exercise 30 to find an explicit formula for s, if So, sI, S2, . . . is a sequence satisfying the recurrence relation Sn = Sn,- + 6 sn-2 + 6n - 1 for n > 2 and the initial conditions so = -6, si = 10. 32. As in Exercise 31, find an explicit formula for s, ifsO, s , S2, . . is a sequence satisfying the recurrence relation Sn = 5s,-1 + 6s,-2 + lOn - 37 for n > 2 and the iniial conditions so = 7, Si = 3. 33. Explain as in Example 8.22 the operation of the binary search algorithm to search the list 2, 4, 6, 8 for the number 6. 34. Explain as in Example 8.22 the operation of the binary search algorithm to search the list 2, 4, 6, 8 for the number 7. Determine the number of comparisonsneeded by the merging algorithm to merge the lists in Exercises 35-38.
35. (45, 57) and (59, 87)
36. (45, 59) and (57, 87)
37. (1,3,5,7)and(2,4,6,8)
38. [(I)and(2,3)] and(4)
39. Give an example of ordered lists a, a2 . am and bl, b2 , . . , bn whose merging by the merging algorithm requires the minimum number of comparisons. Assume that m < n. 40. Give an example of ordered lists a, a2, . . ., am and bl, b2 , .. , b, whose merging by the merging algorithm requires the maximum number of comparisons. Assume that m < n. In Exercises 41-44, let S be the generatingfunction of taeh sequence {s,} Find an equation satisfied by S as in Section 8.6, and solve for S.
41. so = I and Sn = 2s,- 1 for
n
> 1
43. so = 1, si = 1, andSn = -2sn- I - Sn-2 for n > 2
42. so = I and Sn = Sn-I + 2 for
n > 1
44. so = 0, s1 = 1, and sn = Sn-2 for n > 2
ComputerProjects
487
In Exercises 45-48 give aformula for s, if is, } has the given generatingfunction S. 45. S=
546. 1 + 2x
S
47. S= 1+ 1 -2x 1 -3x
48. S=
1 + 6x I + 1 1- x 1- 2x
In Exercises 49-55 give a generatingfunctionfor the sequence {an }. 49. Let ar be the number of ways to select r balls from 3 red balls, 2 green balls, and 5 white balls. 50. In a soft serve ice-cream shop, Great Northern Delites are made using one candy bar flavor chosen from Heath, Snickers, or Butterfinger. Let ar be the number of possible orders for r Great Northern Delites. 51. In a store giveaway, 6 individual winners were identified. Each winner received at least 2 prizes, but no more than 4 prizes, and r identical prizes were awarded. Let a, be the number of ways the prizes could have been distributed among the winners. 52. Let ar be the number of ways of selecting r pastries from a cabinet containing three each of cherry-filled Bismarcks, lemon-filled Bismarcks, vanilla long-johns, chocolate long-johns, vanilla twists, chocolate twists, bearclaws, and apple fritters. 53. Let ar be the number of ways r cents worth of postage can be placed on a letter using only 5¢, 12¢, and 25¢ stamps. The positions of the stamps on the letter do not matter. 54. Sample packages of chocolate and licorice are being made. Each package must contain r pieces of candy, including at least 3 pieces of chocolate and at most 2 pieces of licorice. Let ar be the number of different ways to fill a package. 55. Let ar be the number of ways to pay for an item costing r cents with pennies, nickels, and dimes.
COMPUTER PROJECTS Write a computer program having the specified input and output.
1. Given a recurrence relation s,
= aISn-I + a2S,-2 + + akSn-k and initial values so, s specified term of the sequence defined by these conditions.
,
Sk-1, compute a
2. Given a positive integer n, list the moves necessary to win the Towers of Hanoi game with n disks using the fewest possible moves. 3. Given a positive integer n, compute the nth Catalan number. (See Example 8.5.) 4. Given a positive integer n, list all the sequences in which the numbers 1, 2, . . ., n can leave a stack if they enter it in sequence. (See Example 8.5.) 5. Given positive integers a and b, simulate 500 trials of the game in Example 8.18. 6. Given a list of n integers and a target integer t, find the first occurrence of t in the list using the sequential search algorithm in Section 8.4. 7. Given a list of n integers and a target integer t, find an occurrence of t in the list using the binary search algorithm in Section 8.4. 8. Given a list of n real numbers, sort the list into nondecreasing order using the merge sort algorithm in Section 8.4.
488
Chapter 8 Recurrence Relations and GeneratingFun. tons
9. Given a list of 2 k real numbers for some nonnegative integer k, sort the list using the Bose-Nelson algorithm described in Exercise 40 of Section 8.4. 10. Give f (x) = ao + a, x + *. + anx' and g(x) = b) I- 1 x + + bnx' and a nonnegative integer k, compute the coefficient of xk in the polynomial f (x)g(x).
SUGGESTED READINGS 1. 2. 3. 4. 5.
Bose, R. C. and R. J. Nelson. 'A Sorting Problem,']J. A.soc. Computing Machinery, vol. 9 (1962): 282-296. Goldberg, Samuel. Introduction to Difference Equations. New York: Wiley, 1958. Horowitz, Ellis and Sartaj Sahni. Fundamentals of ComputerAlgorithms. New York: Freeman, 1984. Levy, H. and F. Lessman. FiniteDifference Equations. New York: Dover, 1992. Ryser, Herbert John. CombinatorialMathematics. Washington, DC: Mathematical Association of America, 1963. 6. Stanat, Donald F. and David F. McAllister. Discrete Maihematics in Computer Science. Englewood Cliffs, NJ: Prentice Hall, 1977.
Combinatorial Circuits and Finite State Machines 9.1 Logical Gates 9.2 Creating Combinatorial Circuits 9.3 Karnaugh Maps 9.4 Finite State Machines
1Today tiny electronic devices cal led microprocessors are found in such diverse places as automobiles, digital watches, missiles, electronic games, compact disc players, and toasters. These devices control the larger machines in which they are embedded by responding to a variety of inputs according to a preset pattern. How they do this is determined by the circuits they contain. This chapter will provide an introduction to the logic of such circuits.
9.1
+
LOGICAL GATES The sensitive electronic equipment in the control room of a recording studio needs to be protected from both high temperatures and excess humidity. An air conditioner is provided that must go on whenever either the temperature exceeds 80° or the humidity exceeds 50%. What is required is a control device that has two inputs, one coming from a thermostat and one from a humidistat, and one output going to the air conditioner. It must perform the function of turning on the air conditioner if it gets a yes signal from either of the input devices, as summarized in the following table. Temperature > 80 ° ?
Humidity > 50%?
Air conditioner on?
no
no
no
no
yes
yes
yes
no
yes
yes
yes
yes
We will follow the usual custom of using x and y to label our two inputs and 1 and 0 to stand for the input or output signals yes and no, respectively. Thus x and y can assume only the values 0 and 1; such variables are 489
490
Chapter9
CombinatorialCircuits and FiniteState Machines
called Boolean variables. These conventions give our table a somewhat simpler form. x 0
Y Output 0
0
0 1 1
1 0
1 1
1
1
The required device is an example of a logical gate, and the particular one whose working we have just described is called an OR-gate, since its output is I whenever either x or y is 1. We will denote the output of an OR-gate with inputs x and y by x v y, so that
JI I
if x=1I or otherwise.
y=lI
We will not delve into the internal workings of the devices we call logical
gates, but merely describe hois they function. A logical gate is an electronic device that has either 1 or 2 inputs ania single output. These inputs and output are in one of two states, which we denote by 0 and 1. For example, the two states might be a low and high voltage. Logical gates are represented graphically by standard symbols established by the Institute of Electrical and Electronics Engineers. The symbol for an OR-gate is shown in Figure 9.1. xv y
x---
OR-gate
FIGURE 9.1
We will study only two other logical gates, the AND-gate and the NOT-gate. Their symbols are shown in Figure 9.2. Notice that the symbols for the OR-gate and AND-gate are quite similar, so care must be taken to distinguish between them.
xz
xD^Y
x NOT-gate
AND-gate FIGURE 9.2
9.1 Logical Gates
491
The output of an AND-gate with inputs x and y is 1 only when both x and y are 1. This output is denoted by x A y, so that the values of x A y are given by the following table, which, as in logic, is called a truth table. X
0
y
XAy
0
0
0 1
1
0
0
1
0 1
1
Example 9.1 An ink-jet printer attached to a personal computer will print only when the 'online' button on its case has been pressed and a paper sensor tells it that there is paper in the printer. We can represent this as an AND-gate as in Figure 9.3. ~ on-line? print paper present? FIGURE 9.3
The other logical gate we will consider is the NOT-gate, which has only a single input. Its output is always exactly the opposite from its input. If the input is x, then the output of a NOT-gate, which we will denote by x', is as follows. x
x
1
0
Example 9.2 A rental truck is equipped with a governor. If the speedometer exceeds 70 miles per hour, the ignition of the truck is cut off. We can describe this with a NOT-gate as in Figure 9.4. + ignition
speed greater than 70? FIGURE 9.4
The reader familiar with logic will notice the similarity between the three gates we have described and the logical operators 'or,' 'and,' and 'not.' Although
492
Chapter 9 CombinatorialCircuits and Finite State Mac )ines
other logical gates may be defined, by appropriately combining the three gates we have introduced, we can simulate any logical gate that has no more than two inputs.
+
Example 9.3 A home gas furnace is attached to two thermostats, one in the living area of the house and the other in the chamber where the furnace heats air to be circulated. If the first thermostat senses that the temperature in the house is below 68°, it sends a signal to the furnace to turn on. On the other hand, if the thermostat in the heating chamber becomes l otter than 150°, it sends a message to the furnace to turn off. This signal is for reasons of safety and should be obeyed no matter what message the house thermostat is sending. house temperature below 6Xc?furnace heating chamber temperature greater than I 50 °? FIGURE 9.5
One arrangement of gates giving the desired output is shown in Figure 9.5. It is easier to check the effect of this arrangement if we denote the signals from the two thermostats by x and y as il Figure 9.6. We can compute the value of x A y' for the possible values of x and y by means of a truth table. x
--
OX
Y
D'S
y FIGURE 9.6
-
Y
y
XAy'
O(room ok)
0 (chamber ok)
I
0 (furnace off)
o (room ok)
I (chamber hot)
o
o (furnace off)
I (room cold)
0 (chamber ok)
1
I (furnace on)
I (room cold)
I (chamber hot)
o
0 (furnace off)
x
9.1 Logical Gates
493
Notice that the furnace will run only when the house is cold and the heating chamber is not too hot. + Figure 9.6 shows an example of combining logical gates to produce what is called a combinatorial circuit, which we will usually refer to simply as a 'circuit.' More than two independent inputs are allowed, and an input may feed into more than one gate. A more complicated example is shown in Figure 9.7, in which the inputs are denoted by x, y, and z.
X
DL
D
FIGURE 9.7
We will only consider circuits that have a single output, and we will not allow circuits such as shown in Figure 9.8, in which the output of the NOT-gate doubles back to be an input for the previous AND-gate. (We leave the precise formulation of this condition for the exercises.) In Figure 9.7 the input x splits at the heavy dot. In order to simplify our diagrams we may instead label more than one original input with the same variable. Thus Figure 9.9 is simply another way to draw Figure 9.7. x y
FIGURE 9.8
Y
> l
(X V
)V (A A Z)
z FIGURE 9.9
The effect of complicated circuits can be computed by successively evaluating the output of each gate for all possible values of the input variables, as in the following truth table.
494
Chapter 9
CombinatorialCircuits and Finite State VWa.,hines TABLE 9.1 x
y
z
0
0
0
0
0
0
l
0
1I
01
()
XVyI
(X V y') V (X A Z)
XAZ
0 0
0l
0
0
0
0
0
01o
0I
0
1o
0
0
0
The strings of symbols heading the columns of our table are examples of Boolean expressions. In general, given a finite set of Boolean variables, by a Boolean expression we mean any of these Boolean variables, either of the constants 0 and 1 (which represent variables with the constant value 0 or 1, respectively) and any subsequently formed expressions BvC,
B AC,
or
B',
where B and C are Boolean expressions. Example 9.4 Which of the following are Boolean expressions for the set of Boolean variables x, y, z? X V iy (X
A (x
A z')')
Z) V y
l Ay
z
V y'A O
The first three are Boolear expressions, but the last two are not, since neither A' nor vy' makes sense.
+
Just as a combinatorial circuit leads to a Boolean expression, each Boolean expression corresponds to a circuit, which can be found by taking the expression apart from the outside. Consider the first expression of Example 9.4, which is X V (y A (X A z')'). This corresponds to a circuit with an OR-gate having inputs x and y A (x A z')', as in Figure 9.10. By continuing to work backward in this way, we find the circuit shown in Figure 9.11.
9.1 Logical Gates
495
X X V (y A (X A Z'))
YA (X FIGURE 9.10
FIGURE 9.11
It may be that different circuits produce the same output for each combination of values of the input variables. For example, if we examine Table 9. 1, in which we analyzed the effect of the circuit in Figure 9.9, we may notice that the output is 1 exactly when x is 1 or y is 0. Thus, the circuit has exactly the same effect as that corresponding to x v y', shown in Figure 9.12. Since this circuit is much simpler, manufacturing it rather than the circuit of Figure 9.9 will be cheaper. A simpler circuit will also usually run faster. Some integrated circuits contain more than 100,000 logical gates in an area of one square centimeter, and so their efficient use is very important. r
x
x vy
Y FIGURE 9.12
Circuits that give the same output for all possible values of their input variables are said to be equivalent, as are their corresponding Boolean expressions. Thus, (x V y') V (X A z) is equivalent to x v y', as can be confirmed by comparing the following table to the truth table for (x v y') v (x A z) (Table 9.1). x
y
z
0
0
0
0
0
0
I
I0
0
1
0
1
I
0
X V y'
I 0 0I
0 0 oo0
0
1
YI
0
o
1 I
496
Chapter 9 CombinatorialCircuitsand Finite State Machines
Since the circuits corresponding to equivalent Boolean expressions have exactly the same effect, we will write an equal sign between such expressions. For example, we will write (x V y') V (x A z) = x V y'
since the truth tables of the two expressions are the same. In subsequent sections, we will study how we can reduce Boolean expressions to simpler equivalent expressions to improve circuit design. EXERCISES 9.1 In Exercises 1-8 write the Boolean expression associated w,7th each circuit. 1.
*Y
r
2. x y 5
y y
4.x y
Yes[
5
y
5.
x
6.
x
r11
*Y
{C
X DC My
y 5
8.
.
S y
In Exercises 9-14 draw a circuit representingthe given Boolean expression. 9. (X A y) V (X' V y)
10. (X' A y) V [X A (y A Z)]
11.
[(X A y') V (X' A y')] V [X' A (y V z)]
12.
(W A x) V [(x V y') A (W' V x')]
13.
(y' A z') V [(W A X') A yT]
14.
[X A (y A z)] A [(x' A y') V (z A W')]
9.1 Logical Gates In Exercises 15-18 give the output valuefor the Boolean expression with the given input values. forx = 1, y = 1, z =0
15. (X Vy)A(x'Vz)
16. [(X A y) V z] A [x V (y'Az)J 17. [X A(y Az)]'
18.
for x = O, y = 1, z = 1
forx = O, y = 1, z =O
[(x A (y A z')) V ((x A y) A z)] A (X V z')
for x =O, y = 1, z =O
In Exercises 19-22 construct a truth tablefor the circuit shown.
19. x
20. x
y
y
x
x
y
21. x z
22. x y
y
y z
x
In Exercises 23-28 constructa truth tablefor the given Boolean expression. 23. X A (y V X')
24. (x v y')' v x
25. (X A y) V (x' A y')'
26. x
27. (X V y') V (X A Z')
28. [(X A y) A z] V [x A (y A z')]
V (X' A y)
In Exercises 29-36 use truth tables to determine which pairs of circuits are equivalent. 29. x
yx
x

and
y x
30. and
X
y 31.
a X
>and y
32. x x y
x and
x y
497
498
Chapter 9 CombinatorialCircuits and Finite State lMchines
33. x v
=
Xand
=
y
x y
34. x
---
x
y
-
IN
xand
y
-
35.
-
35 x and y
36.x
y
x
and
y
I
In Exercises 37-42 use truth tables to determine whether o.- not the Boolean expressions given are equivalent. 37. x v (x Ay) and x 38. x A (x' A y) and x A y 39. [(x V y) A (x' V y)] A (y V z) and (x V y) A (X' V z)
40. (x A (y A z)) v [x'V ((x A y) A z')] and x'V y 41. y' A (y V z') and y' A x' 42. x A [w A (y V z)] and (x A w) A (y V z) 43. A home security alarm is designed to alert the police department if a window signal is heard or if a door is opened without someone first throwing a safety switch. Draw a circuit for this situation, describing the meaning of your input variables. 44. The seatbelt buzzer for the driver's side of an autornobilh will sound if the belt is not buckled, the weight sensor indicates someone is in the seat, and the key is in the ignition. Draw a circuit for this situation, describing the meaning of your input variables. 45. Prove that equivalence of Boolean expressions using a fixed finite set of Boolean variables is an equivalence relation as defined in Chapter 2. 46. Define the directed graph associated with a combinatorial circuit. State a condition on this directed graph that excludes circuits similar to that shown in Figure 9.8.
9.2 CreatingCombinatorialCircuits
499
47. What is the output of the illegal circuit shown for x = 0 and 1? X
9.2
+
CREATING COMBINATORIAL CIRCUITS In Section 9.1 we saw how each combinatorial circuit corresponds to a Boolean expression, and observed that sometimes we could simplify a circuit by finding a simpler equivalent Boolean expression. One way to simplify Boolean expressions is by using standard identities, much in the way that the algebraic expression (a + b)2 - b(b - 3a) can be reduced to a(a + 5b) by using the rules of algebra. Some of these identities for Boolean expressions are listed in Theorem 9.1.
Theorem 9.1
For any Boolean expressions X, Y, and Z, (a) (b) (c)
XAY =YAXandXvY =YvX (XAY)AZ=XA(YAZ)and(XvY)vZ=Xv(YvZ) XA(YvZ)=(XAY)v(XAZ)and X V (Y A Z) =(X V Y) A (X v -)
(d) (e) (f) (g)
XV(XAY)XA(XVY)= X XVX=XAX=X XvX'= landXAX'=0 Xv0=XA1I=X XA0=0andXvl=I
(h) (i) (X')' = X, O' = 1, and l' = 0 (j)
(XvY)'=X'AY'and(XAY)'=X'vY'.
Many of these identities have the same form as familiar algebraic rules. For example, rule (a) says that the operations v and A are commutative, and rule (b) is an associative law for these operations. In spite of rule (b), x v (y A z) is not equivalent to (x v y) A z. Rule (c) gives two distributive laws. For example, if in the first equation of rule (c) we substitute multiplication for A and addition for v, we get X(Y + Z) = (XY) + (XZ),
which is the distributive law of ordinary algebra. Making the same substitution in the second equation, however, produces Y + (YZ) = (X + Y)(X + Z),
which is not an identity of ordinary algebra. Thus these rules must be used with care; one should not jump to conclusions about how Boolean expressions may be manipulated based on rules for other algebraic systems.
500
Chapter 9
CombinatorialCircuits and FiniteStat'e Mfahines
The equations of rule J) a-e known as De Morgan's laws; compare them to the rules for the complements of set unions and intersections in Theorem 2.2. The validity of all these identities can be proved by computing the truth tables for the expressions that are claimed i:c be equivalent. +
Example 9.5 Prove rule (d) in Theorem 9. ]. We compute truth tables for the expressions X as follows. x 01o
(X
Y) and X
A
X V (X A Y)
XvY
X A (X V Y)
0
0
0
0
1
0
l
0 1
I
I
y
1o 0
XA}
V
0 (1
1 .1
A
(X
V
Y)
Since the first, fourth, and sixth columns of this table are identical, rule (d) is proved. + As an example of the use of our rules, we will prove that the expressions z) and x v y' are equivalent without computing, as we did in Section 9.1, the truth table of eacheKpression. We will start with the more complicated expression and use our rules lo simplify it. (X V y') V (x A
(x V y') V (x , z) = (y' V x) V (x A z)
(rule (a))
= y' V (x V (X A z))
(rule (b))
= y' V x = x v y'
(rule (d)) (rule (a))
M Example 9.6 Simplify the expression x v (y shown in Figure 9.11.
A (X A Z')'),
which corresponds to the circuit
x V (y A (x A;z')') = x V (y A (x' Vz')) = x V (y A (X'V z)) =(xVy)A(xV(x'Vz)) = (x V y) A ((X V x') V z) = (X V y) A (I V z) = (x V y) A 1 =
x v
y
(rule(j)) (rule (i)) (rule(c)) (rule (b)) (rule (f)) (rule (h)) (rule (g))
9.2 Creating CombinatorialCircuits
501
We see that the complex circuit of Figure 9.11 can be replaced by a circuit having only one gate. +
Because of rule (b) in Theorem 9. 1, we can use expressions such as X V Y V Z without ambiguity, since the result is the same no matter whether we calculate X V Y or Y v Z first. In terms of circuits, this means that the two circuits in Figure 9.13 are equivalent. Thus we will use the diagram of Figure 9.14 to represent either of the circuits in Figure 9.13; its output is 1 when any of X, Y, or Z is 1. x
~
~Xvy
z
(XY
Z
xxvyz y
V
Xv(YvZ)
zr FIGURE 9.13
X
XVYVZ
z FIGURE 9.14
We use the same convention for more than 3 inputs. For example, the circuit shown in Figure 9.15 represents any of the equivalent circuits corresponding to a Boolean expression formed by putting parentheses in W A X A Y A Z; one such expression is (W A X) A (Y A Z), another is ((W A X) A Y) A Z.
w X Y
WAXAYAZ
z
FIGURE 9.15
Of course, before we can simplify a circuit we must have a circuit. Thus we must consider the problem of finding a circuit that will accomplish the particular job we have in mind. Whether the circuit we find is simple or complicated is of secondary importance. There is always the possibility of simplifying a complicated circuit by reducing its corresponding Boolean expression. As an example, we will consider the three-person finance committee of a state senate. The committee must vote on all revenue bills, and of course 2 or 3 yes votes are necessary for a bill to clear the committee. We will design a circuit that will take the three senators' votes as inputs and yield whether the bill passes or not as output. (Ours will be a scaled-down version of the electronic voting
502
Chapter9
CombinatorialCircuits and FiniteState Ma'JI ines
devices used in some legislatures.) If we denote yes votes and the passage of a bill by 1, we desire a circuit with the following truth table. y
z
Pass?
0
0
0
0
0
0
1
0
0 *0
1 1
0
0
1
1
0
0
0
01
1
1
o
0
1
1
1
x
*1
*I
We have marked the rows which have Is in the output column because these rows will be used to construct a Boolean expression with this truth table. Consider, for example, the fourth row of the table. Since there is a I in the output column in this row, when x is 0 and v and z are 1, our Boolean expression should have a value 1. But x is Oif and only if x' is 1; so this row corresponds to the condition that x', y, and z all have value 1. This happens exactly when x' A y A z has value 1.The other marked rows indicate that the output is 1 also when x A y' A z, x A y A z', or X A y A z have value 1. Thus we want an output of 1 exactly when the expression (x'A Y A z) V (x A y'A z) V (x A y Az') v (x A y Az) has value 1, and this is the Boolean expression we seek. The circuit corresponding to this expression is shown in Figure 9.16.
FIGURE 9.16
Notice that we have designed a crude arithmetic computer, since our circuit counts the number of yes voles and tells us whether there are 2 or more. Now we summarize our method of finding a Boolean expression corresponding to a given truth table. Let us suppose the input variables are x1, x2, . . . , X'. If
9.2 Creating CombinatorialCircuits
503
all outputs are 0, then the desired Boolean expression is 0. Otherwise we proceed as follows: Step I
Identify the rows of the truth table having output 1. For each such row, form the Boolean expression yj AY2 A *. Ayn,
Step 2
where yi is taken to be xi if there is a 1 in the x, column, and yj is taken to be xi' if there is a 0 in the xi column. The expressions thus formed are called minterms. If B, B2 ,..,Bk are the midterms formed in step 1, form the expression BuvB 2 v
..
v Bk-
This Boolean expression has a truth table identical to the one with which we started. Example 9.7 A garage light is to be controlled by 3 switches, one inside the kitchen to which the garage is attached, one at the garage door, and one at a back door to the garage. It should be possible to turn the light on or off with any of these switches, no matter what the positions of the other switches are. Design a circuit to make this possible. The inputs are the 3 switches, which we will label I or 0 according to whether they are in an up or down position. We will design a circuit that turns the light on whenever the number of inputs equal to 1 is odd, since flipping any switch will change whether this number is odd or even. We want a circuit with the following truth table. x
Y
0
00
*0 *0
0 1
0 *1
Number of Is
Output
0
0
1 0
1 1
1 1
1 0
1 0
2 1
0 1
1
0
1
2
0
1
1
2
0
3
1
*1 1
z
0 1
The rows having output 1 are marked, and the required Boolean expression is (X'Ay'Az)V(x'AyAz')V(xAy'Az')V(xAyAz). The corresponding circuit is shown in Figure 9.17. +
504
Chapter9
CombinatorialCircuits and Finite State Mac ines
FIGURE 9.17
The Boolean expressions that our method produces tend to be complicated, and so correspond to complicated circuits. The circuit shown in Figure 9.17 is actually more complex than i: appears, since if it were expressed using only our original three logical gates, each of the gates on the left of the diagram with three inputs would have to be replaced by two standard 2-input AND-gates, and the gate on the right with four inputs would have to be replaced by three standard 2-input OR-gates. Thus, the circuit of Figure 9.17 requires 6 NOT-gates, 8 AND-gates, and 3 OR-gates, for a total of 17 elementary gates. Although we might simplify the corresponding Boolean expression using the rules given at the beginning of this section, it is not clear how to do this. In the next section, we will consider a method for simplifying Boolean expressions in an organized way.
EXERCISES 9.2 In Exercises 1-8 prove the equivalence using truth tables. 1. X Ay = y Ax
2. XA(yVz)=(xAy)V(xAz)
3. xvx =x 5. (x A y)'= xVy'
4. (x')' = x
7. x AX'
8. XA(yAz)=(XAy)AZ
=
0
6. X'A y'=(XVY)x
In Exercises 9-18 establish the validity of the equivalence using Theorem 9.1. List by letter the rules you use, in order. Start with the expression on the left side. 9. (xAy)V(xAy')=x
10). X V (x'A y) = X V y
11. XA(X'Vy)=XAY
12. [(x A y) V (x A y')] V [(x' A y) V (x' A y')] = 1
13. (x'V y)'V (x A y') = x A y' 15. (x A y)'V z =x'v (y'Vz) 17. (xAy)A[(xAw)V(yAz)]=(xAy)A(wVz)
14. [(x V y) A (x' V y)] A [(x V y') A (x' V y')] = 0 16. ((x V y) A Z)' = z' V (x' A y')
18. (xVy)'V(xAy)'=(xAy)'
505
9.2 Creating CombinatorialCircuits
In Exercises 19-22 show that the Boolean expressions are not equivalent. 19. xA(yVz)and(xAy)vz
20. (xAy)'andx'Ay'
21. (xAy)V(x'Az)and(xVx')A(yVz)
22. (lvx)vxandx
In Exercises 23-28find aBoolean expression of mintermsthat has the given truth table. Then draw the corresponding circuit. 23.
24.
Output
25. -
x
y
0
0
0
00
0
1
1
0
1
1
1
0
0
1
10 1
1
x
y
0
y 0
z 0
0
0
O 0
Output
x
0 1 0
0
1
0
1
0 1 1
0 0 0
1
0
I
Output
0
O
0 y
0
1 0
0
26. -
28.
27.
x
y
z
0
0
0 10
Output
S
y
z
0
I
0
0
0
0
1
0
0
0
1
0
0
0
10
1
1
0 0
1
1
0
1
0
I
1
0 1o
0
I
o
1
0 1
0
o
0 x
z
Output
0
0
0
1
0
0
1
0
0
0
0
0
0
0
0
1
0
0
0
1
O0
0
1
0
0 0
1
0
1
1
1
0
0
1
1
In Exercises 29-34 give the number of AND-, OR-, and NOT-gates with one or two inputs it would take to represent the given circuits. 29.
30.
506
Chapter 9 CombinatorialCircuits and FiniteState Machines
31. x
y
32.
z y z x y z
33. x
y z x w z
34.
x V w yW w w z
35. Suppose a company wishes to manufacture logical devices having inputs x and y and with output equivalent to the value of the logical statement - (x -> y), whewe 0 corresponds to T and 1 to F. Draw a circuit using AND-, OR-, and NOT-gates that will do this. 36. A security network for a three-guard patrol at a missile base is set up so that an alarm is sounded if guard one loses contact and at least one of the other two guards is not in contact, or if guard one and guard two are in contact but guard three loses contact. Find a Boolean expression that has value 1 exactly when the alarm sounds. Let the input 1 correspond to losing contact. 37. An inventory control system for a factory recognizes ?di error in an order if it contains part A and part B but not part C; if it contains parts B or C, but not part D; or if it contains parts A and D. Find a Boolean expression in the variables a, b, c, and d that is I exactly when an error is recognized. Let a be 1 if part A is present, etc. 38. Which of the rules in Theorem 9.1 hold if X, Y, and Z stand for real numbers and we make the substitutions of multiplication for A, addition for v, and -X for )'? 39. Which of the rules in Theorem 9.1 hold if X, Y, and Z stand for subsets of a set U and we make the substitutions n for A, U for v, A (the complement of A) for A', U fbr 1, and 0 (the empty set) for 0? We define a Boolean algebra to be a set B satisfying the following conditions: (i) For each pair of elements a and b in B, there are defined unique elements a v b and a (ii) IfaandbareinB,thenavb=bvaandaAbbAa. (iii) If a, b, and care in B, then a v (b v c) = (a V b) 'v and a A (b Ac) = (a A b) Ac.
A
b in B.
9.3 Karnaugh Maps
507
(iv) Ifa,b,andcareinB,thenav(bAc)=(avb)A(avc)andaA(bvc)=(aAb)v(aAc).
(v) There exist distinct elements 0 and 1 in B such that if a (vi) If a c B, there is defined a unique element a' E B. (vii) If a E B, then a V a' = I and a A a' = 0.
E
B, then a v 0 = a and a
A
1 = a.
In Exercises 40-45 assume that B is a Boolean algebra. Exercises 41-45 show that the rules of Theorem 9.1 hold in any Boolean algebra. 40. Show that ifa and bare in B and a V b = I and a A b = 0, then b =a'.(Hint: Show that b b A a'=
b A (a v a')=
a'A (a V b).)
41. Show that if a E B, then a A 0 = 0 and a v 1 = . (Hint: Compute a A (O V a') and aV(lAa') in two ways.)
42. Show that if a and b are in B, then a v (a A b) = a A (a V b) = a. (Hint: Compute a A (1 V b) and a V (OA b) in two ways.) 43. Show that if a E B, then a v a = a A a = a. (Hint: Compute (a Va) A (a Va') and (a A a) V (a Aa') in two ways.) 44. Show that if a E B, then a' = a, O' = 1, and 1' = 0. (Hint: Use Exercise 40.) 45. Show that if a and b are in B, then (a v b)' = a' A b' and (a A b)' = a' v b'. (Hint: Use Exercise 40.)
9.3
+
KARNAUGH MAPS In the previous section, we saw how to create a Boolean expression, and therefore a logical circuit, that corresponds to any given truth table. The circuits we created, however, were usually quite complicated. We will show how to create simpler circuits by, in effect, making a picture of the truth table. Of course, 'simpler' has not been defined precisely, and, in fact, various definitions might be appropriate. For compactness and economy of manufacture, we might want to consider one circuit better than another if it contains fewer gates. For speed of operation, however, we might prefer a circuit such that the maximal number of gates between any original input and the output is as small as possible. The method we will describe will lead to circuits that are in general much simpler than those we learned to create at the end of the previous section, although they will not necessarily be simplest by either of the criteria just suggested. We will only treat the cases of 2, 3, or 4 Boolean variables as inputs, although there are methods for dealing with more than 4 input variables. (See suggested reading
[7].) We will show how to produce a simple Boolean expression that has a prescribed truth table. A circuit can then be constructed from this expression. The truth table we start with may represent the desired output of a circuit we are designing or it may have been computed from an existing circuit or Boolean expression that we wish to simplify.
508
Chapter9
CombinatorialCircuits andFinite Staie MAachines
To illustrate the technique, we will start with the following truth table. x
y
Output
0
0
1
0
1
0
1
0
1
1
1
1
For this truth table, our previous method yields the Boolean expression (X' A Y') V (X A y') V (X A~ y)
and the circuit of Figure 9.18. To find a simpler
circuit, we will represent our truth table graphically as in Figure 9.19(a). Each cell in the grid shown corresponds to a row of the truth table, with the rows of the grid corresponding to x and .~c'and the columns to y and y'. For example, the top left cell corresponds to the row of the truth table with x = 1 and y =1, and the 1 in that cell tells us that there is a I in the output column of this row. Since in the grid each row is labeled either X or x', each column either y or y', and there is either a O or 1 in each cell, from now cn we will save time by omitting the labels x' and y' and Os, as in Figure 9.19(b). T.-is is called the Karnaugh map of the truth table. X
XAY'
X'AY'XV'(X A Y
Y
(x'AY')v- (X AY')v- (X AY)
X
X
y XAY
FIGURE 9.18
X
XI
Y-
Y
I
I
0 1
1 -
I
tat
(b) FIGURE 9.19
Each cell in the Karnaugh map corresponds to a minterm, as shown in Figure 9.20(a). Thus we can create a B Dolean expression having the truth table we started with by joining with the symbol v the minterms in cells containing a l, as circled in Figure 9.20(b). This amounts to the method of the previous section, and produces the Boolean expression (X A V) V (x A y') V (x' A y').
9.3 Karnaugh Maps Y X
509
Y AY
XA)T
AA
X Ay
(a)
(b)
FIGURE 9.20
The key to our method is to notice that groups of adjacent cells may have even simpler Boolean expressions. For example, the two cells in the top row of the grid can be expressed simply as x. This can be confirmed using Theorem 9.1 as follows. (xAy)V(xAy')=xA(yVy') =X A1 = x
(rule(c)) (rule (f)) (rule (g))
Other such groups of two cells and the corresponding Boolean expressions are shown in Figures 9.21(a) and (b), where the ovals outline the cell groups named. y
(a)
(c)
(b) FIGURE 9.21
In Figure 9.21(c), we see that the three cells with Is can be characterized as those cells in either the x oval or the y' oval, and so correspond to the Boolean expression x v y'. This is the simpler expression we have been seeking. It is easily checked that x V y' has the desired truth table. The corresponding circuit is shown in Figure 9.22. Comparison with the circuit of Figure 9.18 shows that it is simpler by any reasonable criterion.
x
D
,y
92
FIGURE 9.22
510
Chapter 9 CombinatorialCircuits and Finite State Machines
Since the case of two input variables is fairly straightforward, we shall proceed to three input variables, say x, y, and z. The grid we will use is shown in Figure 9.23(a). Recall the convention that the unmarked second row corresponds to x'. Likewise, columns 3 and 4 correspond to y' and columns 1 and 4 to z'. The minterms for each cell are shown in Figure 9.23(b). y
x
X AY A ZX
X
AYAZ'
AYA
XAvA
AYA
(a)
Ay'A
A -AY
AA
AyAZ
(b) FIGURE 9.23
We will make a somewhat technical definition. We define two cells to be adjacent in case the minterms to which they correspond differ in only a single variable. A pair of adjacent c.Ils can be described by a Boolean expression with one variable fewer than a minterm. For example, the two cells in the second row and third and fourth columns correspond to (x' A y' A ,) N/ (x' A y' A z') = (x' A y') A (z V z') = (x' A y') A 1 = X' A y',
where we have used rules (c), (f), and (g) of Theorem 9.1. Any two cells next to each other in a row or column are adjacent and have a 2-variable Boolean expression, as shown in Figure 9.24. There are also two pairs of adjacent cells that wrap around the sides of our grid; these are shown in Figure 9.25, along with their simplified Boolean expre ssions. Y
x
x
FIGURE 9.24
9.3 Karnaugh Maps
511
y x Ix xD
:
FIGURE 9.25
There are also groups of four cells with single-variable Boolean expressions. These are shown in Figure 9.26. The student should not try to memorize the Boolean expression for the groups of cells outlined in Figures 9.24, 9.25, and 9.26, but rather should study them to understand the principles behind them.
x
y
Xy n
zY
y
I
-_J
x
L_ c-
Cx-
I
FIGURE 9.26
The method for constructing a simple Boolean expression corresponding to a truth table will be similar to the 2-variable case. We draw the Karnaugh map for the truth table, then enclose the cells containing Is (and only those cells) in ovals corresponding to Boolean expressions. Since larger groups of cells have simpler Boolean expressions, we use them whenever possible, and we try not to use more groups than necessary. We then join these expressions by v to form a Boolean expression with the required truth table. Consider, for example, the two Karnaugh maps shown in Figure 9.27. The appropriate groups of cells are shown in Figure 9.28. The corresponding Boolean expressions are
X V (y'A z)
and
(y A z') V (X A y) V (X'A y'A Z),
512
Chapter 9
CombinatorialCircuits and Finite Stat' Mu hines
respectively. Notice that the ce 1in the second row and third column of the second Karnaugh map is adjacent to no other cell with a 1, and so its 3-term minterm must be used. y -
-
Y --
-
-
x
x
I Id I1
FIGURE 9.27 By I
x
Y
9D,
x
[43
I I:jj FIGURE 9.28
+
Example 9.8 Simplify the voting-machine c [rcuit shown in Figure 9.16. Since the machine is to give output 1 when at least two of x, y, and z are 1, the corresponding Karnaugh maV is shown in Figure 9.29. Using the ovals indicated, we write the Boolean expression (x
A
y)
V
(x
A
Z) V (y
A
z). The corresponding
circuit is shown in Figure 9.30. This circuit is considerably simpler than the one of Figure 9.16. In fact, if only gates with no more than two inputs are used, the previous circuit contains 17 while our new version has only 5. + y
x
I2 _Ci__ JEt 7
FIGURE 9.29
9.3 KarnaughMaps
513
x y x z y z
FIGURE 9.30
*
Example 9.9 Simplify the expression x V (y A (x A z')') of Example 9.6. We compute the following truth table. X
y
0
0
0
0
0
10
0
1
0
1
1 1
0
0
0
1
1
0
0
1
1
0
0
0 1
0
1
01
1
1
1
1
Z
0 1
Z'
X AZ'
(X A Z')'
y A (X A z')'
X V (y A (X A Z')')
1
0
1
0
0
0
1
0
0
0
1
1
1
1
1
1
0
0
1
1
0
1
0
0
1
1
1
1
This leads to the Kamaugh map of Figure 9.31. Using the indicated groups of cells produces the same Boolean expression x v y that was derived using the rules of Theorem 9.1 in Example 9.6. +
I
y
x
I1 1 FIGURE 9.31
Finally, we consider Karnaugh maps for circuits with four inputs w, x, y, and z. We will use a 4-by-4-grid, labeled as in Figure 9.32(a). For example, the cell marked (1) corresponds to the minterm w A x' A y A z', and the cells marked (2) and (3) to the minterms w A x A y' A z and w' A x' A y A z, respectively.
514
Chapter 9 CombinatorialCircuits and Finite State Machlines y
y
(1) w
w.
(2)
(3)
(b)
(a) FIGURE 9.32
Figure 9.32(b) shows various groups of two adjacent cells and their Boolean expressions. Of course, there are many more such groups. Examples of groups of four cells and their 2-variable Boolean expressions are shown in Figure 9.33. Notice that they can wrap around either horizontally or vertically. There are also 8-cell groups whose Boolean expressions have a single variable; some of these are shown in Figure 9.34. y
W.
w
Ix
L
FIGURE 9.33
9.3 KarnaughMaps Y
Fy
515
Y
.
y-
1w
FIGURE 9.34
As before, given a truth table, we form its Karnaugh map and then enclose its Is (and only its Is) in rectangles of 1, 2, 4, or 8 cells that are as large as possible. The required Boolean expression is formed by joining the expressions for these rectangles with v. +
Example 9.10 Find a circuit having the following truth table. w
x
y
00
00
00
01
00 10 0 0 1 00 01
0
z
0
1
101
01 10 0 1 1 1 000
1
001
1 1
010 01 1
1
100
Output
1
1 101 1o1 10 1 1 1 1
0 o 0o 0
l
516
Chapter9 CombinatorialCircuits and Finite State M-a hines y
x
FIGURE 9.35
The Karnaugh map for this tabte is shown in Figure 9.35. Using the rectangles of cells shown yields the expression FigV (?e 9 .) V (X A ys) V (WcA Xcri y).
Figure 9.36 shows the corresponding circuit. +
z Xw x y w
I
FIGURE 9.36
+
Example 9.11 Use Karnaugh maps to simplify the circuit of Figure 9.37(a). We compute the Boolean expression (W A X A y) V (w G x A z') V (w A y A z) V (x'
yA A Z)
for the circuit, as shown in Figure 9.37(b). The terms separated by v's in this expression correspond to the four rectangles marked in Figure 9.38(a). The
9.3
(a)
KarnaughMaps
517
(b) FIGURE 9.37
same cells can be enclosed by two rectangles, as shown in Figure 9.38(b). These yield the Boolean expression (w A x) v (x' A y' A z) and the circuit of Figure 9.39. + Y
w!-X
W.-C
y
y.
z
z
(a)
(b) FIGURE 9.38
FIGURE 9.39
518
Chapter 9 CombinatorialCircuits and Finite State Madmines
EXERCISES 9.3 In Exercises 1-6 find a Boolean expression of minterm.s wh ch has the given truth table. 1.
2. x
y
0 0
3.
Output
x
y
z
C'u'put
0
1
1
1
0
0
0
1
0
0
1
1
0
0
0
1
1
1
0
x
y
z
0
0
0
00
0
0
10
0
0
1 1
0
O 0
01
0
L
1
O
0
1
1)
I
0
1
0
1
1
O
1
1
L
Output
L
0
00
I 1
d-
4;-
x
y
z
Output
6
w
x
000
0
000o
001
1
0001
0100
y
001C
01 100 101 1 10 1 1 1
1 1 1 1 0
-
-
- ----
0
0
z
Output
x
C
y
Output
0000
0
0
0001
C
0
0010
001 1 0 0 10 C 1 0 101 0 01 IC I I 0 1 1 1 0
001 1 0 100 0 101 01 10 0 1 1 1
1
0
1
0
1 001
0
1010 1 0 1
1 100
0
1 100
1 1
1 0 1 1 10
1 0
1
11 1
0
00
C
1 00 1 101 1 0 1
C0 1
1
000
y x
8.
d
0
0 01 00 1
1
1 0 1 1 1 0 1 1 1 1
y
0
0
In Exercises 7-12 write the Boolean expression correspondingto the ovals in the Karnaugh map. 7.
0
0 0
9.3 Karnaugh Maps 1U.
Y.
519
Y x
X
z
z
11.
y
12.
I
W
(7 0
71~ Ij
ELI =-
{x ID
w
x
z
z
In Exercises 13-18 draw a Karnaugh map for the Boolean expression of the indicatedexercise. 13. Exercise 1
14. Exercise 2
15. Exercise 3
16. Exercise 4
17. Exercise 5
18. Exercise 6
In Exercises 19-24 use the Karnaugh map method to simplify the Boolean expression in the indicated exercise. Then draw a circuit representingthe simplified Boolean expression. 19. Exercise 1
20. Exercise 2
21. Exercise 3
22. Exercise 4
23. Exercise 5
24. Exercise 6
In Exercises 25-32 use the Karnaugh map method to simplify the expression. 25. (x'A y'A z) V (x'A y A z) V (x A y'A z)
26. (x'
A y' A z) V (x' A y' A z') V (x A y A z) V (x A y' A z')
27. (X' A y' A z) V (x' A y A Z) V (X A y' A z') 28. [(x V y') A (x' A z')] V y 29. [x A (y V z)] V (y' A z') 30. (x A y A z) V (x A y'A z) V (x'A y'A z) 31. (w A x A y) V (w A x A z) V (w A y'A z') V (y'A z')
32. (w' A x' A y') V (W' A y' A z) V (w A y A z) V (W A X A z') V (W A y' A z') V (w A x'A y A Z) V (w' A X A y A Z')
520
Chapter9
CombinatorialCircuits and FiniteState Machines
In Exercises 33 and 34 use Karnaugh maps to simplify the 'iven circuit. .1 w
,
34.
x
y
z
-
w x
7y
I x y
r-Y
z :LJ 35. How many groups of two adjacent cells are there in a Karnaugh map grid for 4 Boolean variables? 36. How many 4-element square groups of adjacent cells Exre there in a Karnaugh map grid for 4 Boolean variables? Although rule (b) of Theorem 9.1 suggests that we get equa,' expressionsfor any two ways we insert parentheses in xi V x2 V .. V Xn, we have not given a formal proof of tl'i: fact. (We will only treat v; A could be handled in the same way.) Define the expression xi V x2 v .. v Xn recurisvely asfollows:
xlv
x2 v
37. Prove by induction on n that (xl
vn=

(x
v
v
-)
V Xn
v x2 v .. v Xm) v (y v Y2 V .. v Yn)
for
n>
=x1 v
1.
v
xm v yl v .. v y
for
any positive integers m and n. 38. Prove that any two expressions formed by inserting parentheses in the expression xi 39. Prove by induction that (xl V X2 V .. V XJ) = X' A tion of the latter expression is similar to that for V.
C' A
..
A Xn
V
x2
V ..
v xn are equal.
for all positive integers n, where the defini-
40. Denote by qn the number of ways of inserting n - 2 sets of parentheses in xl V x 2 v .. V Xn so that the order in which the v's are applied is unambiguous. For example, q3 = 2 counts the expressions (xi V X2) V X3 and xI V (X2 V X3 ). Likewise, q4 = 5. Show that qn = qlqn-1 + q2qn--2 +
+ qn-Iql for n > 1.
41. Let rn be the number of ways of listing xI, x2, . .Xn joined by v's in any order and with parentheses. For example, r1 = 1, r 2 = 2 counts the two expression, xl v x 2 and x 2 v xl, and r3 = 12. Show that r,+i = (4n - 2 )rn for all positive integers n.
9.4 Finite State Machines
42. Show that r, 40 and 41.
9.4
2
anddqq (n 1)!a
( n-2)!
+
=(2n-I2)! n! (n -1)! for all positive integers n, where
rn
521
and qn are defined as in Exercises
FINITE STATE MACHINES
In this section we will study devices, such as computers, that have not only inputs and outputs, but also a finite number of internal states. What the device does when presented with a given input will depend not only upon that input, but also upon the internal state that the device is in at the time. For example, if a person pushes the 'PLAY' button on a CD player, what happens will depend on various things, such as whether or not the player is turned on, contains a CD, or is already playing. The devices now considered will differ from those of the preceding sections in that output will depend not only on the immediate input but also on the past history of inputs. Thus their action has the ability to change with time. Such devices are called finite state machines. Various formal definitions of a finite state machine may be given. We will study two types, one simple and the other somewhat more complicated. Our main concern will be to understand what such machines are and how they operate, rather than to construct finite state machines for specific tasks. One simple example of a finite state machine is a newspaper vending machine. Such a vending machine has two states, locked and unlocked, which we will denote by L and U. We will consider a machine that only accepts quarters, the price of a paper. Two inputs are possible, to put a quarter into the machine (q), and to try to open and shut the door to get a paper (d). Putting in a quarter unlocks the machine, after which opening and shutting the door locks it again. Of course, putting a quarter into a machine that is already unlocked does not change the state of the machine, nor does trying to open the door of a locked machine. There are various ways we can represent this machine. One way is to make a table showing how each input affects the state the machine is in. Present state -L
Input
U
q
U
U
d
L
L
Here the entries in the body of the table show the next state the machine enters, depending on the present state (column) and input (row). For example, the colored entry means that if the machine is in state L and the input is q, it changes to state U. Since this table gives a state for each ordered pair (i, s) where i is an input and s is a state, it describes a function with the Cartesian product {q, d) x {U, LI as its domain and the sets of states I U, L} as its codomain. (The reader may want to review the concepts of Cartesian product and function in Sections 2.1 and 2.5.) Such a table is called the state table of the machine.
522
Chapter 9 CombinatorialCircuits and Finite State Mach nes
We can also represent OUI machine graphically, as in Figure 9.40. Here the states L and U are shown as circles, and labeled arrows indicate the effect of each input when the machine is in each state. For example, the colored arrow indicates that a machine in state L with input q moves to state U. This diagram is called the transition diagram of the machine. (In the language of Section 3.5, the transition diagram is a directed multigraph.) q d
/
q
d FIGURE 9.40
We will generally use the pictorial representation for finite state machines since our examples will be fairly simple. For a machine with many inputs and states, the picture may be so complicated that a state table is preferable. A Parix,ty
h
'e
Eakif 'i4Wg 4f
/}
hm
i
Before we give a formal definition of a finite state machine, we will give one more example. Data sent between electronic devices is generally represented as a sequence of Os and is. Some way of detecting errors in transmission is desirable. We will describe one simple means of doing this. Before a message is sent, the number of Is in the message is counted. If this number is odd, a single 1 is added to the end of the message, and if it is even, a 0 is added. Thus all transmissions will contain an even number of Is. After a transmission is received, the I s are counted again to determine whether there is an even or odd number of them. This is called a parity check. If there is an odd number of Is, then some error must have occurred in transmission. In this case, a repeat of the message can be requested. Of course, if there are two or more errors in transmission, a parity check may not tell the receiver so. But if the transmission of each digit is reliable and the message is not too long, this may be far less likely than a single error. If the received transmission passes the parity check, its last digit is discarded to regain the original message. Actually it is not necessa ry to count the number of Is in a message to tell if this number is odd or even. Figure 9.41 represents a device that can be used to do

I
FIGURE 9.41
9.4 FiniteState Machines
523
this job. Here the states are e (even) and o (odd), and the inputs are 0 and 1. The corresponding state table is as follows. State
Input
e
a
0
e
a
1
a
e
We can use this device to determine whether the number of Is in a string of Os and Is is even or odd by starting in state e and using each successive digit as a new input. For example, if the message 110100001 is used as input (reading from left to right), the machine starts in state e and moves to state o because the first input is 1. The second input is also 1, putting the machine back in state e, where it stays after the third input, 0. The way the machine moves from state to state is summarized in the following table. Input:
Start
I
I
0
1 0
0
0
1
State:
e
o
e
e
o
o
o
e
o
If 11010001 is received, we would presume that no error occurred in transmission and that the original message was 1101000. Two new symbolisms appear in Figure 9.41. One is the arrow pointing into state e. This indicates that we must start in state e for our device to work properly. The other is the double circle corresponding to state e. This indicates that this state is a desirable final state; otherwise in our example some error has occurred. Now we formally define a finite state machine to consist of a finite set of states S, a finite set of inputs I, and a function f with I x S as its domain and S as its codomain such that if i E I and s E S, then f (i, s) is the state the machine moves to when it is in state s and is given input i. We may also, depending upon the application, specify an initial state so, as well as a subset S' of S. The elements of S', called accepting states, are the states we would like to end in. Thus, our parity checking machine is a finite state machine with S = {e, o}, I = {0, 1}, so = e, and S' = {e}. The function f is specified by f(0, e) = e, f(l,e)=o,
f(O, o) = o, f(l,o)=e,
which corresponds to our previous state table. A string is a finite sequence of inputs, such as 110100001 in our last example. Suppose, given the string i1 i2 .. in and the initial state so, we successively compute f (il, so) = sI, then f(i 2 , SI) = S2, etc., finally ending up with state Sn. This amounts to starting in the initial state, applying the inputs of the string from left to right, and ending up in state s,. If Sn is in S', the set of accepting states, then we say that the string is accepted; otherwise it is rejected. In the parity check example, rejected transmissions contain some error, while accepted transmissions are presumed to be correct.
524
Chapter 9
CombinatorialCircuits and Finite State ,Va,-hines
+
Example 9.12
t0,
Figure 9.42 shows a finite state machine with input set I = 11 that accepts a string precisely when it ends with the triple 100. Here S = {A, B, C, D}, so = A, S' {D}, and the function f is as indicated by the labeled arrows in the diagram. For example, if the string 101010 is input, we move through the states ABC BC BC, and, since C is not in S', the string is rejected. On the other hand, if 001100 is input, we move [trough the states AAABBCD, and the string is accepted because D is an accepting state. -
0 FIGURE 9.42
To see that the machine of Figure 9.42 does what we claim, the reader should first check that no matter what state we are in, if the string 100 is input, we are taken to state D. This shows th at all strings ending in 100 will be accepted by the machine. It remains to show that an accepted string must end in 100. Since we start in state A, an accepted string clearly must contain at least three digits. Since when 1 is input we move to state B no matter what the present state is, the accepted string must end in 0. Likewise, the reader should check that any string ending in 10 leaves the machine in state C. Thus our accepted string must end in two Os. Finally, the reader should check that any string ending in 000 puts the machine in state A. Thus any accepted string must end in 100. + One important application of machines that accept certain strings and reject others is in compilers for computer languages. Before a program is run, each statement must be checked to see whether it conforms to the syntax of the language being used. Fini3ke
St ate M/achinvs t
i'vlv QOunui:'l
i
Now we consider a slightly rrore complicated type of device. We start with an example more sophisticated than a newspaper vending machine, namely, a gum machine. Our gum machine accepts only quarters, which is the price of a pack of gum. Three varieties are available: Doublemint (denoted by D), Juicy Fruit (J), and Spearmint (S), which can be chosen by pressing buttons d, j, or s, respectively. The internal states of the machine are locked (denoted by L) and unlocked (U); and if the machine is unlocked, it will return any extra quarters
9.4 Finite State Machines
525
put into it. The inputs are q (quarter), d, j, and s. A diagram showing some of the action of the machine is given in Figure 9.43(a). Figure 9.43(b) shows a more compact way of indicating multiple arrows going between the same two states; here, for example, the three arrows from U and L in Figure 9.43(a) have been replaced by a single arrow, and the corresponding inputs separated by commas. q d
q
(a)
A
(b) FIGURE 9.43
This diagram does not tell the whole story, however. Nowhere does it show that if we press the d button on a machine in state U we get a pack of Doublemint. Neither does it show that excess quarters are returned. We need to introduce the additional concept of outputs of the machine. In this example the possible outputs are D, J, S and also Q (an excess quarter returned) and 0, which we will use to stand for no output, as, for example, when a button is pressed while the machine is in state L. Notice that the output may depend upon both the input and the state of the machine. The inputs d and j produce the distinct outputs D and J when the machine is in state U. Likewise, the input d produces the outputs 0 or D depending on whether the machine is in state L or U. Another function is involved here, having the Cartesian product of the set of inputs and the set of states as its domain and the set of outputs as its codomain. Since each arrow in our diagram stands for the result of an input being applied to a particular state, we can also label these arrows to show the corresponding outputs. This is done in Figure 9.44. dj,s /0,0,0
q /0
q IQ
FIGURE 9.44
We will use slashes to separate the input and output labels on each arrow. Thus, in Figure 9.44, the q/0 on the arrow from L to U indicates that there is no output when we put a quarter in a locked machine; and the d, j, s/D, J, S on the arrow from U to L indicates the outputs D, J, and S, respectively, when we push buttons d, j, and s on an unlocked machine. We define a finite state machine with output to consist of finite sets S of states, I of inputs, and 0 of outputs, along with a function f: I x S -* S such that f (i, s) is the state the machine goes to from state s when the input is i, and another function g: I x S 0 such that g(i, s) is the output corresponding to
526
Chapter 9 CombinatorialCircuits and Finite State Ala .ines
input i when the machine is in state s. Depending on the application, we may again designate a particular state so as the initial state. In the gum machine example, we have S = {L, U}, I = {q, d, j, s}, and 0 = {D, J, S, Q, 01. The functions f and g are indicated in Figure 9.44, but they can also be described, as before, using tables. State
Input
State
L
U
q
U
U
d
L
L
j
L
L
s
L
L
L
U
0
Q
d
0
D
1
0
J
s
0
S
q Input
The first table, which gives the values of f, is still called the state table of the machine, while the second, which gives the values of g, is called the output table. If a string of inputs is fed into a finite state machine with output, a corresponding sequence of outputs is produced, called the output string. This is illustrated in the next example. +
Example 9.13 Figure 9.45 shows the transition diagram of a unit delay machine. This is a finite state machine with output in which I = {0, 1}, S = {A, B, C}, 0 = {O, 1}, and the initial state is A. Note that the first output is always 0, while any input of 0 puts the machine in state B, from which the next output will be 0. Likewise any input of 1 puts it in state C, from which the next output will be 1. Thus, each output after the first is always the same as the input one step previously. An input string il i2 . . in produces the output string 0 iI i2 . . 1in. For example, the input string 1100111 produces the output string 0 10011. If it is desired to copy an entire input string, then a 0 must be appended to it before the string is input. + 0/0
FIGURE 9.45
9.4 Finite State Machines
+
527
Example 9.14 Draw the transition diagram for the finite state machine with output that has the following state and output tables, and describe what the machine does to an input string of x's and y's. The initial state is A. State
Input
State
A
B
C
D
E
F
A
B
C D E F
x
A
C
C
E
E
F
o0
1
I
2
2
3
y
B
B
D D
I
1
2
2
3
3
FF
The transition diagram is shown in Figure 9.46. Notice that once an input x or y puts the machine into one of the states A, B, C, D, or E, the machine stays in that state until the input changes. The output is 0 or 1 according to whether the first input is x or y, and increases by one whenever the input changes from x to y. Thus, the output at any time counts the number of groups of consecutive y's in the input string, up to three such groups. For example, the input string xxyxxxyyyxx produces the output string 001 11122222; and the last 2 counts the two groups of y's (y and yyy) in the input string. +
FIGURE 9.46
EXERCISES 9.4 In Exercises 1-6 draw the transition diagramfor the finite state machine with the given state table.
2.
1. A
B
G A
A
IA
3. BA
B
1
A
CA
C
B
x
y
z
o
y
z
z
1
x
x
y
Initial state x Accepting state z
4.
6.
5. A
B
C
D
a
B
A
D
C
A
b
C
C
A
A
B
Initial state B Accepting states C, D
A
B
x
B
A
Y
A
z
B
Initial state A Accepting state A
u
V
W
u
w
V
1
u
W
W
2
w
v
u
o
Accepting states u, v
528
Chapter 9 CombinatorialCircuits and Finite State Machines
In Exercises 7-10 give the state tablefor the finite state machine with the given transition diagram. List the initial and accepting states, if any. 7.
S.
9.
10.
0
A.
0
For the finite state machine and input string in Exercises i'1-14, determine the state that the machine ends in if it starts at the initialstate. 11. Input string 1011001, machine of Exercise 3
12. Input string xyyzzx, machine of Exercise 4
13. Input string yxxxy, machine of Exercise 7
14. Input string 0100011, machine of Exercise 8
In Exercises 15-18 tell whether the given input string would be accepted by the indicatedfinite state machine. 15. Input string xyzxyzx, machine of Exercise 4
16. Input string aabbaba,machine of Exercise 5
17. Input string xyxxyy, machine of Exercise 7
18. Input string 0011010, machine of Exercise 10
In Exercises 19-22 draw the transition diagramfor the finite state machine with output whose state and output tables are given. 19.
2(0. A B
A B
1
2
3
1
2
3
0
BA
x y
red
2
3
1
A
B
A
1
A B
z x
blue
1
1
3
A
A
B
22.
21. -A
B
00
01
10
11
00
01
10
11
A
11
10
01
00
1
-1
0
1
x
B
01
10
11
11
w
Initial state 10
A B2.
0
A A
x
y
1
B B
w
2
A B
y
Initial state A
-1
0
1
-1
9.4 FiniteState Machines
529
In Exercises 23-26 give the state and output tables of the picturedfinite state machine with output. Name the initial state, if any. 23.
1,2/a,c
31c
1,3/aa
24.
0/0
1/4
0/0
26.
25.
0/1
0/2
O/t
IIt
1,3/y,z
In Exercises 27-30 give the output stringfor the given input string andfinite state machine with output. 27. Input string 2101211, machine of Exercise 21
28. Input string BAABBB, machine of Exercise 22
29. Input string 322113, machine of Exercise 25
30. Input string 10100110, machine of Exercise 26
In Exercises 31-34 describe which input strings of Os and Isare accepted by the picturedfinite state machine.
31.
0
1,0
33.
32.
1
34.
0
0
1
0
0,1
In Exercises 35-38 assume the set of inputs is {0, }1. 35. Design a finite state machine that accepts a string if and only if it ends with two Is. 36. Design a finite state machine that accepts a string if and only if it does not contain two consecutive Os.
530
Chapter 9 CombinatorialCircuits and FiniteState Moc/iines
37. Design a finite state machine with output such that. given an input string, its last output is the remainder when the number of Is in the input string is divided by 3. 38. Design a finite state machine with output such that it Jutput string contains as many Is as there are pairs of consecutive Os or Is in the input string. 39. Let F and G be finite state machines. We say that F arid G are equivalent if they have the same set of inputs and if, whenever a string is accepted by either of the in chines, it is also accepted by the other. Let I and S be sets. Show that equivalence of finite state machines is an equivalence relation on the set of finite state machines having input sets and state sets that are subsets of I a-id S, respectively. -
_
HISTORICAL NOTES Gottfried Wilhelm Leibniz (16461716) was probably the first person to note the relationships that allowed statements of logic to be represented algebraically. This freeing of logical symbols from representing specific interpretations allowed mathematicians and others to think about their forms abstractly. Augustus De Morgan (1806- 1871) and others contributed to this formal model for deductive logic. George Boole's two texts, The Mathematical Analysis of Logic (1847) and An Investigation of the Laws of Thought (1854), detailed the results of his investigations. The structures developed by Boole were expanded by Charles Sanders Peirce (183'--1914) and Ernst Schroder (1841-1902) in the final third of the nineteenth century. In 1869, the Englishman Wvilliam Stanley Jevons (1835-1882) constructed a rudimentary machine capable of performing simple Boolean operations. His design was substantially improved by Peirce's student Allan Marquand (1853-1924) during the 1880s. Marquand's machine involved the use of circuits and electricity and required manually opening and closing circuits through a keyboard [77]. Despite these advances. Boolean algebra was still used mainly as a model for logical reasoning and formal algebraic structure. It was in the late 1930s that Claude Shannon (1916-2001) recognized the application of Boolean algebra to the design of switching circuitry and other applications. Almost immediately, machines employing two-state I AAA1 AA IA - AA . . - I I I . +AAA.. I I switches were developed, andi other machines employing mem and nooiean aigeora conClaude Shannon structs became central compone nts in the emerging field of digital computing. ..
AI
A
.
AAADAI A+ .AA AA-
With this realization of Boolean statements and operations in mechanical form, there arose a need to minimize the number of switches or circuits for a given set of relations. Maurice Karnaugh (1924-) provided a method based on creating a map in 1953. Another method for minimization was the tabular approach developed by Willard Quine (1908-) during the period from 1952- S155. This procedure was altered and improved in Edward McCluskey (1929- ) in 1956 [76]. Edward McCluskey
Finite state machines first appeared in the literature in the early 1950s with the works of G. H. Mealy, D. A. Huffman. and E. F. Moore.
Supplementary Exercises
531
SUPPLEMENTARY EXERCISES 1. Write a Boolean expression corresponding to the following circuit, and construct the corresponding truth table.
x
z
2. Draw a circuit representing the Boolean expression [y truth table.
A (x' V z)] V (y A
z)', and construct the corresponding
3. Determine whether each of the following pairs of Boolean expressions are equivalent. (a) x A (y A z')' and (x A y') V (x A z) (b) xA(y'Vz)' and (XAy)V(xAz') 4. The lights on a private tennis court are to be controlled by either of two switches, one (labeled x) at the court and one (labeled y) at the house. If a third switch z at the house is thrown, however, then the switch at the court should no longer have any effect. Give the truth table modeling this situation. The output 1 means that the lights are on. 5. Establish the following equivalences using Theorem 9.1. List by letter the rules you use. Start with the expression on the left side.
(a) [(x V y) A (x' V y)] V y'= 1 (b) x' A (y A z')' = (x V y)' V (x' A z) 6. Find a Boolean expression in minterms which has the following truth table. Then draw the corresponding circuit. How many gates with 1 or 2 inputs does the circuit represent?
x
y
z
0
0
0
0
0
0
0
1 0
0
0
01 1
1
0
Output
0 0 0 1 1
7. Find a Boolean expression in minterms and draw the corresponding circuit for the truth table of Exercise 4. How many gates with 1 or 2 inputs does this circuit represent?
532
Chapter 9 CombinatorialCircuits and Finite Stare Miwhhines
8. Draw a Karnaugh map corresponding to each of the loLlowing truth tables. (a)
(b) X
y
(c) -
Output
x
y
00
0
0
0
01 1 1 1
1 I1 1
0
0
0
1
0
O 0
I 1
z 0
o0 0
0
w
Output
x
y
z
0
0
0
0
0 0
0
0
0
0
0
0
0
0
O
0 0
0
1
0 1 0 0
0 0
0
0
I 1
0
0 0 0
1 o
0
0
1
0
1 00 0
O
0
0O
0
0
I
0 0
0 1
1
I 0 0
O I
Output
1
9. Write a Boolean expression corresponding to each Karnaugh map below. (a)
(b)
(c)
x{
10. Use the Karnaugh map method to find a simple Boolean expression for each truth table in Exercise 8. Draw the corresponding circuit. 11. Use the Karnaugh map method to simplify the Boolean expression of Exercise 7, and draw the corresponding circuit. How many 1- or 2-input gates does the new circuit represent? 12. Use the Kamaugh map method to simplify each of the -ollowing expressions. (a) (x A y'A z) V (x'A y Az') V (x'A y'A z')
(b)
(wAx'Ay'Az)V(w' Ax Ay Az')V(W'A.X /,')V(W' Ax'Ay'Az')V(w'Ay'
AZ)
Supplementary Exercises
533
13. Use the Kamaugh map method to simplify the following circuit.
14. Draw the transition diagram for the finite state machine with the following state table. A
B
C
red
B
B
B
green
A
B
C
yellow
B
C
A
Initial state A Accepting state B 15. What is the final state if the machine of the previous exercise has the input string: green, red, green, red, yellow? 16. Give the state table for the finite state machine with the following transition diagram. List the initial and accepting states.
3
17. What is the final state if the machine of the previous exercise has the input string 5, 5, 3, 3, 5, 5, 3? 18. Draw the transition diagram for the finite state machine with output with the following state and output tables.
hot
cold
hot
cold
a
hot
cold
1
-1
b
cold
hot
0
1
Initial state cold 19. What is the output string if the machine of the previous exercise has input string abaabba?
534
Chapter 9
CombinatorialCircuits and Finite State. Machines
20. Give the state and output tables for the finite state machine with output pictured below. 0/a 1
1/b ider
x))
/foamn
, Ilb, c
21. What is the output string if the machine of the previous. exercise has input string 1001001? 22. Devise a finite state machine with inputs I = {0, 1) that accepts a string al a2 . a, exactly when n > 2 and an-, :A an
COMPUTER PROJECTS Write a computer program having the specified input and output. 1. Given a triple (x, y, z), where each of x, y, and z is 0 or L,output the corresponding value of (x V z) A (y V z'). 2. Input a quintuple (A, B, x, y, z), where A and B are 2 Oi 3 and x, y, and z are 0 or 1. Here 2 stands for A and 3 stands for v. Output the corresponding value of (x A yill z. 3. Let a given string of eight Os and 1s be interpreted as the ri ghtmost column of a truth table with Boolean variables x, y, and z. Output the corresponding Boolean expression in minterms. For example, the input 11000000 would produce the output (x' A y' A z') V (x' A y' A z). Exercises 4-7 refer to exercises in Section 9.4. 4. Given a finite string of Os and Is, find the final state ifithe string is the input of the machine in Exercise 3. 5. Given a finite string of Os and Is, find the final state if the string is the input of the machine in Exercise 8. 6. Given an input string of Os, Is, and 2s, find the output string, using the machine of Exercise 21. 7. Given an input string of is, 2s, and 3s, find the output string, using the machine of Exercise 25. 8. Given a finite state machine with inputs I = (1, 2, 31, states S = {1, 2, 3}, initial state 1, accepting states 1 and 3, and state table the 3 x 3 matrix A, determine whether a given input string is accepted or not.
SUGGESTED READINGS 1. Dornhoff, Larry L. and Franz E. Hohn. Applied Modert Algebra. Paramus, NJ: Prentice Hall, 1978. 2. Fisher, James L. Application-OrientedAlgebra. New York: Crowell, 1977. 3. Friedman, Arthur D. and Premachandran R. Menon. Theory & Design of Switching Circuits. Rockville, MD: Computer Science Press, 1975. 4. Liu, C. L. Elements of Discrete Mathematics, 2nd ed. New York: McGraw-Hill, 1985. 5. Stanat, Donald F. and David F. McAllister. Discrete VMatthematics in Computer Science. Englewood Cliffs, NJ: Prentice Hall, 1977. 6. Stone, Harold S. Discrete Mathematical Structures ana Their Applications. Chicago: Science Research Associates, 1973. 7. Tremblay, J. P. and R. Manohar. Discrete Mathematical Structureswith Applications to Computer Science. New York: McGraw-Hill, 1975.
Appendix: An Introduction to Logic and Proof A.1 Statements and Connectives A.2 Logical Equivalence A
'2
1k4-fLc
f Pn
I
is essential that persons in such fields as mathematics, physics, and computer science understand the basic principles of logic so that they are able to recognize valid and invalid arguments. In Chapter 9 we explored an application of logic to the design of circuits such as those found in computers. In this appendix we present an informal introduction to logic and proof that provides a sufficient working knowledge of these subjects for students of computer science, mathematics, and the sciences.
A.1
+
STATEMENTS AND CONNECTIVES One aspect of logic involves determining the truth or falsity of meaningful assertions. By a statement we will mean any sentence that is either true or false, but not both. For example, each of the following is a statement. (1) (2) (3) (4) (5) (6) (7)
George Washington was the first president of the United States. Baltimore is the capital of Maryland. 6+3=9. Texas has the largest area of any state in the United States. All dogs are animals. Some species of birds migrate. Every even integer greater than 2 is the sum of two primes.
In the sixth statement above, the word 'some' appears. In logic, we interpret the word 'some' to mean 'at least one.' Thus the sixth statement means that at least one species of birds migrates or that there is a species of birds that migrates. The first, third, fifth, and sixth of the statements above are true, and the second and fourth are false. At this time, however, it is not known whether the seventh statement is true or false. (This statement is a famous unsolved mathematical problem called Goldbach'sconjecture.) Nevertheless, it is a statement because it must be true or false but not both. 535
536
Appendix A
An Introduction to Logic and Proof
On the other hand, the following sentences are not statements. (1) (2) (3)
Why should we study logic? Eat at the cafeteria. Enjoy your birthday!
The reason that these fail to be statements is that none of them can be judged to be true or false. It is possible that a sentence is a statement and yet we are unable to ascertain its truth or falsity because of a ri ambiguity or lack of quantification. The following sentences are of this type. (1) (2) (3) (4)
Yesterday it was cold. He thinks New York is a wonderful city. There is a number x such that x2 = 5. Lucille is a brunette.
In order to decide whether th- first sentence is true or false, we need to specify what we mean by the word 'ccld.' Similarly, in the second sentence we need to know whose opinion is being considered in order to decide whether this sentence can be designated as true or false. Whether the third sentence is true or false depends upon what type of numbers are allowed as possible replacements, and the assignment of true or false to the last sentence depends upon which Lucille one might have in mind. Hereafter, we will not consider such ambiguous sentences as statements, due to their lack of specification, their quantification, or their lack of antecedents for critical words ar variables needed to determine the truth or falsity of the sentence. +
Example A.1 The following sentences are statements. (a)
On December 4, 1985, the temperature dropped below freezing in Miami, Florida. (b) In the opinion of some citizens of Kuwait, George H. Bush is a hero. (c) There is an integer x such that x2 = 5. (d) Singer Gloria Estefan is -from Cuba. + We will be interested in studying the truth or falsity of statements formed from other statements using the expressions below. These expressions are called
connectives. Conwec, ihe
Symbol
Name
not
-
negation
and
A
conjunction
V
disjunction
or if.
then ..
if and ont', if
conditional
biconditional
A.]
Statements and Connectives
537
The use of the connective 'not' in logic is the same as in standard English; that is, its use denies the statement to which it applies. It is easy to form the negation of most simple statements, as we see in the following example. Example A.2 Consider the following statements. (a) (b)
Today is Friday. Los Angeles is not the capital of California.
(c)
32 = 9.
(d) (e)
It is not true that I went to the movies today. The temperature is above 60° Fahrenheit.
The negations of the statements above are given below. (a) (b) (c) (d) (e)
Today is not Friday. Los Angeles is the capital of California. 32 #
9.
It is true that I went to the movies today. The temperature is less than or equal to 60° Fahrenheit.
a
However, the negation of statements containing words such as 'some,' 'all,' and 'every' requires more care. Consider, for instance, the statement s below. s: Some bananas are blue. Since 'some' means 'at least one,' the negation of s is the statement -s: No bananas are blue. Likewise, the negation of the statement t:
Every banana is yellow.
is the statement -t:
Some bananas are not yellow.
As these examples suggest, a statement involving the word 'some' is negated by changing 'some' to 'no,' and a sentence involving the words 'all,' 'each,' or 'every' is negated by changing these words to 'some .. not .. '
Example A.3 Negate each of the following statements. (a) Some cowboys live in Wyoming. (b) There are movie stars who are not famous. (c) No integers are divisible by 5.
538
Appendix A
An Introduction to Logic and Proof
(d) (e)
All doctors are rich. Every college football player weighs at least 200 pounds.
The negations of these statentents are given below. (a) (b) (c) (d) (e)
No cowboys live in Wyoming. No movie stars are not famous. (Or, all movie stars are famous.) Some integers are divisible by 5. Some doctors are not rich. Some college football players do not weigh at least 200 pounds.
o
It is obvious that the negation of a true statement is false, and the negation of a false statement is true. We can record this information in the following table, called a truth table. P
~P
T
F
F
T
Here p denotes a statement and -p denotes its negation. The letters T and F signify that the indicated statement is true or false, respectively. The conjunction of two statements is formed by joining the statements with the word 'and.' For example, the conjunction of the statements Today is Mondayy
p:
and
q:
I went to school
is the statement p
A
q:
Today is Monday, and I went to school.
This statement is true only when both of the original statements p and q are true: Thus the truth table for the connective 'and' is as shown below. p
q
pAq
T
T
T
T
F
F
F
T
F
F FF
The disjunction of two statements is formed by joining the statements with the word 'or.' For example, the disjunction of the statements p and q above is p V q:
Today is Monday, or I went to school.
This statement is true when at least one of the original statements is true. For example, the statement p V q is true in each of the following cases: (1)
Today is not Monday and I went to school.
A.]
(2) (3)
Statements and Connectives
539
Today is Monday and I did not go to school. Today is Monday and I went to school.
Thus the truth table for the connective 'or' is as shown below. p
q
pvq
T
T
T
T
F
T
F
T
T
F
F
F
The connectives 'if.. then. . .' and 'if and only if' occur rather infrequently in ordinary discourse, but they are used very often in mathematics. A statement containing the connective 'if .. then . . .' is called a conditional statement or, more simply, a conditional. For example, suppose Mary is a student we know, and p and q are the statements p:
Mary was at the play Thursday night
and q:
Mary doesn't have an 8 o'clock class on Friday morning.
Then the conditional p p
q:
-)
-)
q is the statement
If Mary was at the play on Thursday night, then she doesn't have an 8 o'clock class on Friday morning.
Another way of reading the statement 'if p, then q' is 'p implies q.' In the conditional statement 'if p, then q,' statement p is called the premise, and statement q is called the conclusion. It is important to note that conditional statements should not be interpreted in terms of cause and effect. Thus when we say 'if p, then q,' we do not mean that the premise p causes the conclusion q, but only that when p is true, q must be true also. In Normal, Illinois, there is a city ordinance designed to aid city street crews in removing snow. The ordinance states: If there is a snowfall of two or more inches, then cars cannot be parked overnight on city streets. As applied to a particular day, say December 15, 2000, this regulation is a conditional statement with premise p:
There is a snowfall of two or more inches on December 15, 2000
and conclusion q:
Cars are not parked overnight on city streets on December 15, 2000.
540
Appendix A
An Introduction to Logic and Proof
Let us consider under what Circumstances the conditional statement p -- q is false, that is, under what circumstances the ordinance has been violated. The ordinance is clearly violated if there is a snowfall of two or more inches and cars are parked overnight on city stieets on December 15, 2000, that is, if p is true and q is false. Moreover, if there is a snowfall of two or more inches and cars are not parked overnight on the city streets on that day (that is, if both p and q are true), then the ordinance has been followed. If there is no snowfall of two or more inches (that is, if p is false), then the ordinance does not apply. Hence, in this case, the ordinance is not violated whether there are cars parked overnight on city streets or not. Thus the ordinance is violated only when the premise is true and the conclusion is false. It may seem unnatural to regard a conditional statement p -* q as being true whenever p is false. Indeed, i: seems reasonable to regard a conditional statement as being not applicable when t e premise is false. But then the conditional p -. q would be neither true nor false when p is false, so p -* q would no longer be a statement by our definition. For this reason, logicians consider a conditional statement to be true if its premise e is false. Therefore the truth table for a conditional statement is as shown below. p
q
xp-q
T
T
T
T
F
F
F
T
T
F
F
T
The biconditional staterrient p *+ q means p -* q and q -* p. Thus a biconditional statement is the conjunction of two conditional statements. We read the biconditional statement p +-i q as 'p if and only if q' or 'p is necessary and sufficient for q.' For instance, the following statements are biconditional statements: Mary was at the play Thursday night if and only if she doesn't have a class at 8:00 on Friday morning. For John Snodgrass to drive his 1965 Mustang in the parade, it is necessary and sufficient ihat he buys a new muffler. We can obtain the truth table for p ++ q from the tables for p p
q-ip
(p-
q)A(q-p)
T
q' 7'
T
T
T
T
F
F
T
F
F
I
T
F
F
F
F
T
T
T
-*
q and q
-+
p.
A.]
Statements and Connectives
541
Thus we see that the conditional statements p -* q and q -> p are both true only when p and q are both true or false. Hence the truth table for a biconditional statement is as shown below. p
q
p÷4q
T
T
T
T
F
F
F
T
F
F
F
T
In the first table of the preceding paragraph, we can see that the conditional statements p -* q and q -+ p do not always have the same truth values. Unfortunately it is a common mistake to confuse these two conditionals and to assume that one is true if the other is. Although these two statements are different, they are obviously related because both involve the same p and q. We call the statement q -- p the converse of p -> q. There are two other conditional statements that are related to the conditional p -+ q. The statement -p -+ -q is called the inverse of p -+ q, and the statement -q p is called the contrapositive of p -* q. Example A.4 Form the converse, inverse, and contrapositive of the following statement about John Snodgrass: If John got a new muffler, then John drove his Mustang in the parade. The given conditional statement is of the form p -* q, where p and q are the statements below: p:
John got a new muffler
and q:
John drove his Mustang in the parade.
The converse, inverse, and contrapositive of the given statement are as follows. converse: If John drove his Mustang in the parade, then John got a new muffler. inverse: If John didn't get a new muffler, then John didn't drive his Mustang in the parade. contrapositive: If John didn't drive his Mustang in the parade, then John didn't get a new muffler. + +
Example A.5 Form the converse, inverse, and contrapositive of the statement: If it isn't raining today, then I am going to the beach.
542
Appendix A
An Introduction to Logic and Proof
The desired statements are as follows. converse: If I am going to the beach today, then it isn't raining. inverse: If it is raining today, then I am not going to the beach. contrapositive: If I am not going to the beach today, then it is raining. In this case, we must be careful not to read more into the given statement than it says. It is tempting to regard the given statement as a biconditional statement meaning that I am going to the beach today if it isn't raining and not going if it is. However, the given statement( does not say that I am not going to the beach if it is raining. This is the inverse of the given statement. Likewise, one must constantly guard against assuming the truth or falsity of the converse of a conditional on the basis of the truth or falsity of the conditional itself. + It should be noted that th-i term 'converse' is also commonly used in mathematics in a more complicated sense than that defined above. For example, consider the statement: A:
If the 3 sides of air angle are congruent, then the 3 angles of the triangle are congnient.
Most mathematicians would call the following statement the 'converse' of statement A: A*: If the 3 angles of it triangle are congruent, then the 3 sides of the triangle are congruent. Indeed this seems to be consistent with the definition of converse given above if we consider statement A to have the form p -* q, where p and q are as follow: p:
The 3 sides of a triangle are congruent
and q:
The 3 angles of a triangle are congruent.
The problem with this interpretation is that p and q are not statements. For example, whether p is true or false depends on what triangle we are talking about. Actually, the statement Labeled A is an abbreviated but common way of expressing the following: For all triangles T, if three sides of T are congruent, then the 3 angles of T are congruent. This statement is of the form
B:
For all x in S, p(x)
--
q(x).
Here x is a variable, S is some set (the set of all triangles in our example) and p(x) and q(x) are sentences thar become statements when x is given any particular
A.] Statements and Connectives
543
value in S. The conventional 'converse' of statement B is thus the statement: B*:
For all x in S, q(x)
-*
p(x).
In our example with triangles, both statements A and its 'converse' A* are true in Euclidean geometry. On the other hand, C:
For all real numbers x, if x > 3, then x > 2
is true, while C *: For all real numbers x, if x > 2, then x > 3 is not. Note that both the statements D: For all integers n, if n is even, then n is the square of an integer and D*:
For all integers n, if n is the square of an integer, then n is even
are false. EXERCISESA.1 In Exercises 1-12 determine if each sentence is a statement. If so, determine whether the statement is true orfalse. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
Georgia is the southernmost state in the United States. E.T., phone home. If x = 3, then x 2 = 9 Cats can fly. What's the answer? New York is the location of the United Nations building. Five is an odd integer, and seven is an even integer. Six is an even integer, or seven is an even integer. Please be quiet until I am finished, or leave the room. Nine is the largest prime number less than 10, and two is the smallest. Five is a positive integer, or zero is a positive integer. Go home and leave me alone.
Write the negations of the statements in Exercises 13-24. 13. 4+5=9.
15. California is not the largest state in the United States. 17. All birds can fly. 19. There is a man who weighs 400 pounds.
14. Christmas is celebrated on December 25. 16. It has never snowed in Chicago. 18. Some people are rich. 20. Every millionaire pays taxes.
21. Some students do not pass calculus.
22. All residents of Chicago love the Cubs.
23. Everyone enjoys cherry pie.
24. There are no farmers in South Dakota.
544
Appendix A An Introduction to Logic and Proof
For each of the given pairs of statements p and q in Exercises 25-32, write: (a) the conjunction and (b) the disjunction. Then indicate which, if either, of these statements is true.
25. 26. 27. 28. 29. 30. 31. 32.
p: p: p: p: p: p: p: p:
One is an even integer. Oregon borders Canada. The Atlantic is an ocean. Cardinals are red. Birds have four legs. Oranges are fruit. Flutes are wind instruments. Algebra is an English course.
q: q: q: q: q: q: q: q:
Nine is a positive integer. Egypt is in Asia. The Nile is a river. Robins are blue. Rabbits have wings. Potatoes are vegetables. Timpani ar, string instruments. Accounting is a business course.
For each statement in Exercises 33-36 write: (a) the converse, (b) the inverse, and (c) the contrapositive.
33. 34. 35. 36.
If this is Friday, then I will go to the movies. If I complete this assignment, then I will take a break. If Kennedy doesn't run for the Senate, then he will run for President. If I get an A on the final exam, then I'll get a B for the course. ce
A.2
+
LOGICAL EQUIVALENCE When analyzing a complicated statement involving connectives, it is often useful to consider the simpler statements that form it. The truth or falsity of the complicated statement can then be determined by considering the truth or falsity of the simpler statements. Consider. for instance, the statement Fred Nitney starts at guard in tonight's game implies that Sam Smith scores fewer than 10 points if and only if Sam Smith scores fewer than 10 points or Fred Nitney doesn't start at guard. This statement is formed from the two simpler statements p:
Fred Nitney starts at guard in tonight's game
and q:
Sam Smith scores fewer than 10 points in tonight's game.
We can write the given statement symbolically as (p
-* q) * (q v p). Let us analyze the truth of this statement in terms of the truth of p and q. This analysis can be conveniently carried out in the truth table shown below, where each row corresponds to a different pair of truth values for p and q.
545
A.2 Logical Equivalence p
q
p-q
-p
qv -p
T
T
T
F
T
(p-q)
- (qv -p) T
T
F
F
F
F
T
F
T
T
T
T
T
F
F
T
T
T
T
Thus, we see that the original statement Fred Nitney starts at guard in tonight's game implies that Sam Smith scores fewer than 10 points if and only if Sam Smith scores fewer than 10 points or Fred Nitney doesn't start at guard is always true, regardless of the truth or falsity of the statements Fred Nitney starts at guard in tonight's game and Sam Smith scores fewer than 10 points in tonight's game. +
Example A.6 Assuming that p, q, and r are statements, use a truth table to analyze the compound statement p V [(p
-q)
A
-+
r].
The truth table below shows that the statement p v [(p A -q)
-+
r] is always
true. p
q
r
-q
pA--q
T
T
T
F
F
T
T
T
T
F
F
F
T
T
T
F
T
T
T
T
T
T
F
F
T
T
F
T
F
T
T
F
F
T
T
F
T
F
F
F
T
T
F
F
T
T
F
T
T
F
F
F
T
F
T
T
[(pA-q)-rj
pv[(pA-q)-*rj
ogo
Compound statements such as the one in Example A.6. that are true no matter what the truth values of their component statements are of special interest because of their use in constructing valid arguments. Such a statement is said to be a tautology. Likewise, it is possible for a compound statement to be false no matter what the truth values of its component statements; such a statement is called a contradiction. Obviously the negation of a tautology is a contradiction and vice versa.
546
Appendix A An Introduction to Logic andProof
+
Example A.7 As we can see in the truth table below, the statement (p contradiction. p
q
T
T
F
T
F
F
T
F
F
Thus -[(p tautology. +
A
-
of?
p
pVq
pAq
(p A
A
q) A (-p
F
F
T
F
F
T
T
F
F
T
F
F
T
F
T
T
F
T
F
q) A (--)
v
-q)
A
(-p
V
q) is a
vq)
q)], the negation of the given statement, is a
Two compound statements are called logically equivalent if they have the same truth values for all possible truth values of their component statement variables. Thus two statements S and T are logically equivalent if and only if the biconditional S +- T is a tautology. For example, we saw in the first truth table in this section that the biconditional (p -. q) (q V '-p) is a tautology. Therefore the statements p -* q and q v -p are logically equivalent. -
Example A.8 Show that the compound statements -(p V q) and (-p) A (-q) are logically equivalent. (This fact is calledL De Morgan's law.) In order to prove that the two statements are logically equivalent, it is sufficient to show that the columns in a truth table corresponding to these statements are identical. Since this is the case in the truth table below, we conclude that -(p v q) and (-p) A (-q) are logically equivalent. p
q
pq
T
T
T
F
F F
-(pvq)
-p
-q
(p)A(-q)
T
F
F
F
F
r
F
F
T
F
T
;r
F
T
F
F
F
F
T
T
T
T
do
In logical arguments, it is often necessary to simplify a complicated statement. In order for this simplification to result in a valid argument, it is essential that the replacement statement be logically equivalent to the original statement, for then the two statements always have the same truth values. Thus, because of the logical equivalences shown in Example A.8, we can replace either of the
A.2
Logical Equivalence
547
statements -(p V q) or (-p) A (-q) by the other without affecting the validity of an argument. We will close this section by stating a theorem containing several important logical equivalences that occur frequently in mathematical arguments. The proof of this theorem will be left to the exercises. Note the similarity between parts (a) through (h) of this theorem and parts (a) through (c) of Theorem 2.1 and parts (a) and (b) of Theorem 2.2.
Theorem A.1
The following pairs of statements are logically equivalent.
(a) p A q and q A p
(commutative law for conjunction) (commutative law for disjunction) (associative law for conjunction) (associative law for disjunction) (distributive law) (distributive law) (De Morgan's law) (De Morgan's law) (law of the contrapositive)
(b) pvqandqvp
andp A (q Ar) (p vq)Vr and p v (q vr)
(C) (p Aq)Ar
(d) (e) (f) (g)
pV(q Ar)and (pVq)A(pVr) p A (q Vr) and (p A q) V(p Ar) -(p V q) and -p A-q (h) -(p A q) and -p V-q (i) p -+ q and -q p-* -p
EXERCISES A.2 In Exercises 1-10 construct a truth tablefor each compound statement. 1. (p V q) A [-(p A q)]
2. (-p v q) A (-q
3.
(p V q) -
4. (-p A
5.
(p-q)-(pvr)
(-p A q)
6. p
7. (-q Ar) - (-p V q) 9. [(p V q) Ar]
-
-+
q)
-
(-q
V p)
(-q Vr)
8. -[p A (q V 10. (r A -q) -
[(p A r) V q]
A p)
r) (q V p)
In Exercises 11-16 show that the given statements are tautologies. 11. -p vp
13. (-p A q) - -(q -- p) 15. -[((p - q) A (-q V r)) A (-r A p)]
12. (p -> q) V (-q A p) 14. -(-p A q)- (-q V p) 16. [(p A q) - r] - [-r -
(-p V -q)]
In Exercises 17-24 show that the given pairs of statements are logically equivalent.
18. p and p V (p A q) q and (-p v q) A (-q V p)
17. p and -(-p) 19. -(p -) q) and -q A (p V q) 21. p -*(q *r)and(p Aq) *r 23. (p v q)

r and (p
-
r) A (q
-*
r)
25. Prove Theorem A. I parts (a) and (b). 27. Prove Theorem A. 1 parts (e) and (f).
20. p
-*
22. (p
-+
24. p
--
q)
-*
r and (p V r) A (q
(q v r) and (p
-)
q) v (p
-* -÷
r)
r)
26. Prove Theorem A. I parts (c) and (d). 28. Prove Theorem A. I part (h).
548
Appendix A An Introductionto Logic and Proof
29. Prove Theorem A. 1 part (i). 30. The statement [(p
-*
q) A -q]
31. The statement [p A (p
-*
q)]
32. The statement [(p v q) A '-p]is a tautology.
-+
-p is called modus tollens. Prove that modus tollens is a tautology.
q is called modus ponens. Prove that modus ponens is a tautology. q is called the law of disjunctive syllogism. Prove that disjunctive syllogism
33. Define a new connective named 'exclusive or' and denoted v by regarding p v q to be true if and only if exactly one of p or q is true. (a) Write a truth table for 'exclusive or.' (b) Show that p v q is logically equivalent to -(p o * 34. The Sheffer stroke is a connective denoted I and defined by the truth table below.
p T
q 7.
p Iq
T
I
T
F
T
T
F
F
T
F
The following parts prove that all of the basic connectives can be written using only the Sheffer stroke. (a) (b) (c) (d)
Show Show Show Show
that p I p is logically equivalent to -p. that (p I p) I (q I q) is logically equivalent tc p v q. that (p I q) I (p I q) is logically equivalent tc p A q. that p I (q I q) is logically equivalent to p q.
A.3
c
METHODS OF PROOF Mathematics is probably the only human endeavor that places such a central emphasis on the use of logic and proof. Being able to think logically and to read proofs certainly increases mathematical understanding, but, more importantly, these skills enable us to apply mathematical ideas in new situations. In this section we will discuss basic methods of proof so that the reader will have a better understanding of the logical framework in which proofs are written. A theorem is a mathematical statement that is true. Theorems are essentially conditional statements, although the wording of a theorem may obscure this fact. For instance, Theorem 1.3 is worded: A set with n elements has exactly 2' subsets.
A.3 Methods of Proof
549
With this wording, the theorem does not seem to be a conditional statement; yet we can express this theorem as a conditional statement by writing: If S is a set with n elements, then S has exactly 2n subsets. When the theorem is expressed as a conditional statement, the premise and conclusion of the conditional statement are called the hypothesis and conclusion of the theorem. By a proof of a theorem, we mean a logical argument that establishes the theorem to be true. The most natural form of proof is a direct proof. Suppose that we wish to prove the theorem p -* q. Since p -* q is true whenever p is false, we need only show that whenever p is true, so is q. Therefore in a direct proof we assume that the hypothesis of the theorem, p, is true and demonstrate that the conclusion, q, is true. It then follows that p -- q is true. We will illustrate some types of proofs by proving certain elementary facts about integers that use the following two definitions. (1)
An integer n is called even if it can be written in the form n = 2k for some
integer k. (2)
An integer n is called odd if it can be written in the form n = 2k + 1 for some integer k.
We will also use the fact that every integer is either even or odd, but not both. The following theorem is proved using a direct proof.
+
Example A.9 Suppose that we wish to prove the theorem: If n is an even integer, then n2 is an even integer. To prove this result by a direct proof, we assume the hypothesis and prove the conclusion. Accordingly, we assume that n is an even integer and prove that n2 is even. Since n is even, n = 2k for some integer k. Then n2 = (2k) 2 = 4k 2 = 2(2k 2 ). If k is an integer, then so is 2k2 . Hence n2 can be expressed as 2 times the integer 2k2 , and so n2 is even. +
+
Example A.10 Consider the theorem: If x is a real number and x2 - 1 = 0, then x = -1 or x = 1. Since x 2 - 1 = 0, factoring gives (x + 1)(x - 1). But if the product of any
two real numbers is 0, at least one of them must be 0. Consequently, x + 1 = 0 or x - 1 = 0. In the first case, x = -1, and in the second, x = 1. Thus x = -I orx=1.
+
550
Appendix A
An Introduction to Logic and Proof
The argument in Example N.10 uses the law of syllogism, which states [(p
'-
q)
A
(q
-
r)]
(p
-
r).
-
Suppose that x is some f xed real number, and let p, q, r, and s be the statements I2-=O
p:
r: (x + 1)(x -1) = 0 s: c+ q:
=0
or or
c=-l
x - 1 =O x=1
Then the argument in ExampLe A. 10 shows that (p?
-*
r) A
(r
-- >s) A
(s
-*
q).
Hence by two applications of the law of syllogism, we conclude that p -*q, proving the theorem. Another type of proof is based on the law of the contrapositive, which states that the statements p -- q and -q - p are logically equivalent. To prove that p -+ q by this method, we give a direct proof of the statement -q -* -'p by assuming -q and proving -p. The law of the contrapositive then allows us to conclude that p -- q is also trae. Example A.11 We will prove the theorem: If.3c + y > 100, then x > 50 or y > 50. Suppose that x and y are some fixed real numbers. Then it suffices to show that p -* q, where p and q denote the statements below. p:
x + y - 1003
q: x > 50 or y > 50
and
We will establish the contrapositive of the desired result, which is -q -~- -'p. Consequently, we will assume that -q is true and show that -p is true. Using Example A.8, we see that -q and -p are the statements -q:
x <50andy < 50
and
-p:
x+y < 100.
Suppose that x < 50 and v <50. Then .c+y <50+50=100. Hence -p is proved, that i, -q contrapositive that p -* q.
-p.
It now follows from the law of the
Example A.12 We will prove the theorem: if n is an integer and n2 is even, then n is even.
A.3 Methods of Proof
551
The contrapositive of this theorem is: if an integer n is not even, then n2 is not even. This statement can also be expressed in the form: If n is an odd integer, then n2 is odd. This statement can be proved by an argument like that in the proof of the theorem in Example A.9. Assume that n is an odd integer. Then n = 2k + I for some integer k. Then n2 = (2k + 1)2 = 4k2 + 4k + I = 2(2k 2 + 2k) + 1. Since k is an integer, so is 2k2 + 2k. Hence n2 can be expressed as 2 times the integer 2k 2 + 2k plus 1, and so n2 is odd. Thus we have proved that if n is an odd integer, then n2 is odd. It follows that the contrapositive of this statement, if n is an integer and n2 is even, then n is even, is also true. + A very different style of proof is a proof by contradiction. In this method of proof, we prove the theorem p -- q by assuming p and -q are true and deducing a false statement r. Since (p A -q) -* r is true but r is false, we can conclude that the premise p A -q of this conditional statement is false. But then its negation -(p A -q) is true, which is logically equivalent to the desired statement p -* q. (See Exercise 1.)
Example A.13 We will prove the theorem: If n is the sum of the squares of two odd integers, then n is not a perfect square. Proving this theorem by contradiction seems natural because the theorem expresses a negative idea (that n is not a perfect square). Thus, when we deny the conclusion, we obtain the positive statement that n is a perfect square. Accordingly we will use a proof by contradiction. Therefore we assume the hypothesis and deny the conclusion, and so we assume both that n is the sum of the squares of two odd integers and that n is a perfect square. Since n is a perfect square, we have n = m2 for some integer m. But also n is the sum of the squares of two odd integers. Since an odd integer is one more than an even integer, we can express n in the form n = (2r +
1)2
+ (2s +
1)2
for some integers r and s. It follows that n
=
(2r + 1)2 + (2s + 1)2 = (4r2 + 4r + 1) + (4s2 +4s + 1)
= 4(r2 + S2 + r + s) + 2, so that n is even. Thus m2 = n is even. We deduce from Example A.12 that m is even, and so m = 2p for some integer p. Thus n = m2 = (2 p)2 = 4p2 is divisible by 4. But we saw above that n = 4(r2 + S2 + r + s) + 2, which is not divisible
552
Appendix A An Introduction to Logic and Proof
by 4. Hence we have derived a f else statement, namely, that n is both divisible by 4 and not divisible by 4. Thus assuming the hypothesis and denying the conclusion has led to a false statement. It follows that if the hypothesis is true, the conclusion must be true also. Consequently the theorem has been proved. + +
Example A.14 Show that there is no rational number r such that r 2 = 2. (Recall that a rational number is one that can be written as the quotient of two integers.) The theorem to be proved can be written as the conditional statement: If r is a rational number, then r2 7 2. Again proving this theorem by contradiction seems natural because the theorem expresses a negative idea (that r2 is not equal to 2). So if we deny the conclasion, we obtain the positive statement that there is a rational number r such that r = 2. Accordingly we will use a proof by contradiction. Thus we assume the hypothesis and deny the conclus en, and so we assume that there is a rational number r such that r2 = 2. Because r is a rational number, it can be expressed in the form n, where m and n are integers. Moreover, we may choose mn and n to have no common factors greater than 1, so that the fraction rn/n is in lowest terms. Then we have (in )2
from which it follows that in2 = 2n 2. Hence M2 is an even integer. Thus by Example A. 12, mn must be even, that is, mn = 2p for some integer p. Substituting this value for m in the equation rn2 = 2n2 yields 4p 2 = 2n 2 , so that 2p 2 = n2 Hence n2 is even, and so it follows as above that n is even. But then both m and n are even, that is, m and n have a common factor of 2. This fact contradicts our choice of m and n as having no common factor greater than 1, and so we deduce that the conclusion to our theorem must be true. Thus the theorem is proved. + We have discussed three basic methods of proof in this section, the direct proof, proof of the contrapositive, and proof by contradiction. There are other types of proofs as well. One method of proof that is quite important in discrete mathematics is proof by matnermatical induction, which is discussed in Section 2.6. Another type of proof is a proof by cases, in which the theorem to be proved is subdivided into parts, each of which is proved separately. The next example demonstrates this technique.
+
Example A.15 Show that if n is an integer, then n 3 - n is even. Since every integer n is either even or odd, we will consider these two cases.
A.3 Methods of Proof
553
Case 1: n is even: Then n = 2m for some integer m. Therefore n 3 -n = (2m) 3 -2m = 8m 3 -2m = 2(4m 3 -m), which is even. Case 2: a is odd: Then n = 2m + 1 for some integer m. Hence
n 3 -n = (2m + 3
3
1)
-(2m
=(8m +12m
2
+ 1)
+6m+1) -(2m+1)
= 8m 3 + 12m 2 + 4m
= 2(4m3 + 6m2 + 2m), which is even. Because n2 integers n. +
_
n is even in either case, we conclude that n 3
-
n is even for all
To close this section, we will briefly consider the problem of disproving a statement p -* q, that is, of showing that it is false. Because a conditional statement is false only when its premise is true and its conclusion is false, we must find an instance in which p is true and q is false. Such an instance is called a counterexample to the statement. For example, consider the statement: If an integer n is the sum of the squares of two even integers, then n is not a perfect square. To disprove this statement, we must find a counterexample, that is, an integer n that is the sum of the squares of two even integers and at the same time a perfect square itself. The equality 100 = 62 + 82 shows that 100 is such a number. The existence of a single counterexample is enough to invalidate the statement, even though there are many values of n for which the statement holds, i.e., 40 = 22 + 62. But, the statement is false because it is not true for all integers that satisfy the hypothesis.
EXERCISESA.3 1. Prove that
-(p
A
-q) is logically equivalent to p
--
q.
2. Prove that the law of syllogism is a tautology. 3. Prove that if m is an integer and m2 is odd, then m is odd. (Hint. Prove the contrapositive.) 4. Prove as in Example A.14 that there is no rational number r such that r2 = 3.
Prove the theorems in Exercises 5-12. Assume that all the symbols used in these exercisesrepresentpositive integers. 5. If a divides b, then ac divides bc for any c. 6. If ac divides bc, then a divides b. 7. If a divides b and b divides c, then a divides c.
554
Appendix A An Intooduction to Logic and Proof
8. If a divides b, then a < b. 9. If p and q are primes and p divides q, then p = q. 10. If a divides b and a divides b + 2, then a = 1 or a = 2. 11. If xy is even, then x is even or y is even. 12. For all positive integers n greater than 10, 12(n
-
2) - n2 -n.
Prove or disprove the results in Exercises 13-22. Assum' that all the numbers mentioned in these exercises are integers. 13. The sum of two odd integers is odd. 14. The product of two odd integers is odd. 15. If ac = bc,thena = b. 16. If 3 divides xy, then 3 divides x or 3 divides y. 17. If 6 divides xy, then 6 divides x or 6 divides y. 18. If 3 divides x and 3 divides y, then 3 divides ax + by 19. If a and b are odd, then a2 + b2 is even. 20. If a and b are odd, then a2 + b2 is not divisible by 4. 21. For all integers, n, n is odd if and only if 8 divides n2
-
1.
22. The product of two integers is odd if and only if both of the integers are odd. 23. Prove or disprove: For every positive integer n, n2
+
r, + 41 is prime.
24. Prove that in any set of three consecutive odd positive ntegers other than 3, 5, and 7, at least one number is not prime. 25. Prove that for each positive integer n, n' 26. Prove that for each positive integer n, n 4
-
2 is not divisible by 3. n2 is divisible by 6.
27. Prove that if p is a prime positive integer, then log, ) p i, not expressible as the quotient of two integers. 28. Prove that there are infinitely many primes. - Oo
HISTORICAL NOTES The study of logic and proof has played a central role in mathematics since the time of Thales of Miletus (ca. 580-50() B.C.), who is regarded as the first mathematician to offer deductive arguments. In the fifth century B.C., the Athenian Plato (429-348 B.C.) made a distinction between arithmetic ithe theory of numbers) and logistic (the techniques of computation). In drawing this distinction, Plato discussed the differences of theory and application. He saw the essence of mathematics residing in the analytic method. One begins with givens, in the form of axioms or postulates, and works, step-by-step, to develop a line of reasoning resulting in a specific desired statement. Throughout his work, he elevated the role of theory in comparison to :hat of application. Plato's student Aristotle (384-322 B.C.) was the first to systematize deductive arguments into a system of principles. While he wrote little about mathematics directly, his development of argument in his philosophical
HistoricalNotes
Augustus le Mvtorgan
U.S. Peirce
555
writings and his constant use of mathematical concepts in discussing argument forms left indelible marks on the subject [73, 74]. The German Gottfried Wilhelm Leibniz (1646-1716) presented his view of systematic argument forms in his 1666 book De Arte Combinatoria.While many of his scientific colleagues viewed his work as being 'metaphysical,' Leibniz worked from 1679 to 1690 developing a system built on undefined terms, axioms and postulates, logical rules, and derived statements. His goal was to develop a universal algebra for reasoning. It was not until the work of the English mathematician George Boole (1815-1864) that such a system of thought became widely accepted. Boole's 1847 work The MathematicalAnalysis of Logic and its 1854 extension An Investigation of the Laws of Thought ushered in a new focus on the nature of evidence, argument, and proof. These works were substantiated by Augustus De Morgan's (1806-1871) Formal Logic; or, the Calculus of Inference, Necessary and Probable. At the same time, the American Charles Sanders Peirce (1839-1914) argued fora separation of mathematics and logic and added emphasis on the role of quantifiers in logical arguments. Between 1880 and 1885, Pierce worked to develop a theory involving truth values and semantics for his logic. Peirce's work with quantification and the algebra of statements moved inference in a new direction. His algebra of logic was brought to new levels of rigor and formalism in German Ernst Schroder's (1841-1902) work Vorlesungen liber die Algebra der Logik (1890-1905) and Alfred North Whitehead's (1861-1947) A Treatise on UniversalAlgebra (1898) [80]. At the same time, a number of mathematicians began to work to develop a system for rigorously expressing all of mathematics in terms of this new logical language. Between the appearances of his books Foundationof Arithmetic in 1879 and The FundamentalLaws of Arithmetic in 1903, the German Gottlieb Frege (1848-1925) tried to develop a more rigorous basis for mathematics. The Italian Giuseppe Peano's (1858-1932) seminal 1894 work Formulairede Mathematiques laid out a view of arithmetic based on the undefined concepts of zero, number, and successor and the following fundamental axioms. * * * * *
Zero is a number. For any number n, its successor is a number. No number has zero as its successor. If two numbers m and n have the same successor, then m = n. If T is a set of numbers such that 0 is a member of T and the successor of n is in T whenever n is in T, then T is the set of all numbers.
In this, order (successor) and induction were tied to the development of number. These ideas were also used by Julius Wilhelm Richard Dedekind (1831-1916) and others to formulate a basis for arithmetic and, more generally, mathematics. The best known of these was the multi-volume work PrincipiaMathematica (1910-1913) developed by Bertrand Russell (1872-1970) and Alfred North Whitehead. The Austrian-born mathematician Kurt Godel's (1906-1978) 1931 paper showed that the axiomatic method has its limitations. His theorem, called Godel's proof, showed that any set of axioms broad enough to contain the fundamentals of the positive integers must also contain a well-defined statement that can neither be proven true or false within the system [80, 84].
556
Appendix A An Introduction to Logic and Proof
SUPPLEMENTARY EXERCISES In Exercises 1-8 determine if each sentence is a statement. If so, tell whether it is true orfalse. 1. All integers are real numbers.
2. Each real number is an integer.
3. Tom is the smartest student in class.
4. The day before Thursday is Friday.
5. n2
6. Some rectangles are squares.
> n! for all integers n.
7. No square of an integer has a unit's digit of 7.
.3. n! + I is a prime for all positive integers n.
Write the negation of each statement in Exercises 9-16. Indicate whether each negation is true orfalse. 9. No squares are triangles.
11). All isosceles triangles are equilateral.
11. Some scientists from the United States have received Nobel prizes. 12. Red is a primary color, and blue is not a primary color. 13. 2 + 2 > 4, or I is a root of x± + I = 0. 14. It is not the case that x2
I for all integers x.
>
15. In circling the globe along a line of latitude, one must cross the equator twice, the North Pole, and the South Pole.
16. There are five complex roots to the equation xA
- ] = 0.
Foreachpair of statementsp andq in Exercises17-20, wriae (a) theirconjunction and(b) their disjunction.Indicate the truth value of each compound statementformed. 17. p: Squares have four sides.
q: Triangles have three sides.
e=I
18. p: In
q:
log 10 = 2.718
.
19. p: If 3 > 2, then 3 x 0 > 2 x 0.
q: If 4 =5, then 5 = 9.
20. p: sin(s)-
q: cos(- )
0.5
=
0.5.
For each statement in Exercises 21-24, write (a) the converse, (b) the inverse, and (c) the contrapositive. Indicate the truth value of each. 21. If 3+3=6,then3 2 =6.
22. If(3+3)
2
18,then(3+3) 2 =332+32.
23. If 32=6,then3 x2=6.
24. If(3+3)2 =6
=
2
,then3
2
+2x3 x3+3 2 =6
In Exercises 25-28 constructa truth tablefor each compou'd statement. 25.
-[(pv q)A -p]A
27,
[p A(rA (-p Vq))]
26. -[(pv-q)Ap]
p
~[(p Ar)A (-p
Vq)]
28.
f(p -r)
-
A(p
(-pAq)
q)] [p (r Aq)]
In Exercises 29-32 determine if the given statements are tautologies. 29. [p v 31.
(p
A q)] -(p
[[(p v q) A
-pi
A q)
v q)
30.
(q A -q)
p
-q)
32. -[[(p v
--
[(p v q)
In Exercises 33-36 test whether the given statements are logically equivalent.
33.
[-p A (-p A q)] v [p A (p A -q)]
34. (p -q)
A (p
r)
and
q-- r
and
(-p A q) v (p A-q)
q]
q) A (-p v r)] v -(q A r)J
2
.
Suggested Readings 35. (pAqAr)v(pA-qAr)V(-pA-qAr)V(QpAqAr) 36. [(pvq)v
r]A[pv(qvr)] and
and
557
r
pvq
In Exercises 37-44 prove or disprove the statement given. Assume that all the variablesin these exercises represent positive integers. 37. If n > 4, then n can be written as the sum of two distinct primes. 38. If x is even and x does not have zero as a unit's digit, then x is not divisible by 5. 39. If x is even and x is a perfect square, then x is divisible by 4.
40. One or both of 6n + 1 and 6n -1 are prime. 41. 10 divides n5 _ n. 42. If s is the sum of a positive integer n and its square n2 , then s is even. 43. If d is the difference of two consecutive cubes, then d is odd. 44. If k is the sum of a positive integer c and its cube c3 , then k is even.
SUGGESTED READINGS 1. Kenelly, John W. Informal Logic. Boston: Allyn and Bacon, 1967. 2. Lucas, John. An Introduction to Abstract Mathematics. Belmont, CA: Wadsworth, 1986. 3. Mendelson, Elliott. Introductionto MathematicalLogic. Princeton, NJ: Van Nostrand, 1964. 4. Polya, G. How to Solve It. 2nd ed. Garden City, NY: Doubleday, 1957. 5. Solow, Daniel. How to Read and Do Proofs. New York: Wiley, 1982.
BVAppendix: Matrices In our study of graph theory, it will sometimes be useful to represent a graph as an array of Os and 1s. Arrays of numbers are useful not only in representing graphs, but also in performing computations. In this appendix, we will discuss the addition and multiplication of matrices, two operations that will be used in Chapters 3 and 4.
An m x n matrix is a rectangular array of numbers in which there are m horizontal rows and n vertical columns. For example, if
A= 5
0]
and B
3
4
°
then A is a 3 x 2 matrix and B is a 4 x 3 matrix. The numbers in a matrix are called its entries. More specifically, the number in row i and column j is called the i, j entry of the matrix. In the matrix B above, the 3, 2 entry is 9 and the 4, 1 entry is -7. Two matrices A and B are called equal whenever A and B have the same number of rows and the same number of columns and the i, j entry of A equals the i, j entry of B for every possible choice of i and j. In other words, two matrices are equal when they have the same size and all pairs of corresponding entries are equal. As with real numbers, if matrices A and B are equal, then we write A = B; otherwise, we write A :A B. +
Example B.1 Consider the matrices
C=[1 2], [ 3 4 ]'
D. =[1
2 0],
[3 4 0 ]
and E =[ 1
]3
[2 4]
Because C and D are of different sizes (C is a 2 x 2 matrix, whereas D is a 2 x 3 matrix), we have C 0 D. Also, C # E because the 1, 2 entry of C, which is 2, 558
Appendix B
Matrices
559
does not equal the 1, 2 entry of E, which is 3. However, if [(-1)4
FL[(,j
'-
22]
then C =F. +
Matrices are useful for storing information. Moreover, there are operations that can be performed on matrices that correspond to natural ways to work with the data stored in them. We will discuss here only the matrix operations used in this book, namely, matrix addition and multiplication. Suppose that the Mathematics Department of a university maintains two computer labs. The lab for upper-level courses contains 25 computers and 3 printers, and the lab for lower-level courses contains 30 computers and 2 printers. One way to record this information is with the 2 x 2 matrix
Upper Lower
Comp. Print. [25 31 [30 2
M M
In addition, suppose that the Computer Science Department also maintains two labs for upper- and lower-level courses, which have 50 computers and 10 printers and 28 computers and 4 printers, respectively. This information can be recorded in the 2 x 2 matrix Upper Lower
Comp. Print. [50 10 [28 4]
C
Then the sum M + C is the matrix M +C
[25
L30
31
[5+0 2] +28
Comp. Print. 101 - [25+50 3 + 101 - [75 131 Upper 4] [30+28 2+ 4] [58 6] Lower
whose entries give the total number of computers and printers in the Mathematics and Computer Science labs for each level of course. In general, suppose that A and B are two m x n matrices. The sum of A and B, denoted A + B, is the m x n matrix in which the i, j entry equals the sum of the i, j entry of A and the i, j entry of B. In other words, matrices of the same size can be added by adding their corresponding entries. Observe that only matrices of the same size can be added, and the sum is the same size as the matrices being added.
560
Appendix B
Matrices
Example B.2 Consider the matrices
6
4
A= 2
1
-2
and B=
6
3 0].
Since A and B are both 3 x 2 matrices, they can be added. Their sum is the 3 x 2 matrix
6
4~
A+B=22 A
1
---2 6
6+(-2)
4+61
3
0
2 +3
1+0
7
4
0[+ 7
+
_[
4 =
-5±+44
10
5 7
1i.
-1
+
Unfortunately, the multiplication of matrices is more complicated than addition. We will start by considering the product of a 1 x n matrix A and an n x 1 matrix B. If bi
A = [al ti, .. a,] and B=
b2
LbJ then the product AB is the 1
>
1 matrix
bi AB = [a,
a2
b2
.. a,]
[abi + a2b2 +
+ anb,],
bn -
in which the single entry is the sum of the products of the corresponding entries of A and B. Example B.3 Let
7 A= [I
2
3]
and
B=
8 -9-
The product of the 1 x 3 matrix A and the 3 x 1 matrix B is
7 AB =[1 2
3][8 -9-
=
[1(7) +2(8) +3(9)] = [50]. +
More generally, the product of an m x n matrix A and a p x q matrix B is defined whenever n = p, that is, whenever the number of columns in A equals
Appendix B Matrices
561
the number of rows in B. In this case, the product AB is the m x q matrix whose i, j entry equals the sum of the products of the corresponding entries of row i of A and column j of B. Symbolically, if A is the m x n matrix whose i, j entry is aij and B is the n x q matrix whose i, j entry is bij, then AB is the m x q matrix whose i, j entry equals aijbij +
ai2b2j
+
- +
ainbnj-
Note that this value is the same as the entry in the 1 x 1 matrix obtained by multiplying row i of A by column j of B, as described above.
Example B.4 Let
A
4 5 6
and B=
[L8 11f
Here A is a 2 x 3 matrix and B is a 3 x 2 matrix, and so the product AB is defined and is a 2 x 2 matrix. Its 1, 1 entry is the sum of the products of the corresponding entries of row 1 of A and column 1 of B (as in Example B.3): 1(7) + 2(8) + 3(9) = 50. Similarly, the 1, 2 entry of AB is the sum of the products of the corresponding entries of row 1 of A and column 2 of B: 1(10) + 2(11) + 3(12) = 68; the 2, 1 entry of AB is the sum of the products of the corresponding entries of row 2 of A and column 1 of B: 4(7) + 5(8) + 6(9) = 122; and the 2, 2 entry of AB is the sum of the products of the corresponding entries of row 2 of A and column 2 of B: 4(10) + 5(11) + 6(12) = 167. Thus AB is the 2 x 2 matrix AB 4
_
5 6]
112
1(7) + 2(8) + 3(9)
1(10) + 2(11) + 3(12)1
4(7) + 5(8) + 6(9)
4(10) + 5(11) + 6(12)]
[
50
681
122
1671*
562
Appendix B Matrices
In this book, we will often encounter the product of two n x n matrices. Note that such a product is defined and is another n x n matrix. In particular, if A is an n x n matrix, then the product AA is defined. As with real numbers, we denote this product as A2. Since A2 is also an n x n matrix, the product A3 = AA 2 is defined and is another n x n matrix. In a similar fashion, we can define Ak+l = AAk for every positive integer k, and all of the matrices A, A2, A 3 , . . . are n x n matrices. It is important to note that the multiplication of matrices is not commutative, that is, AB need not equal B A. In Example B.4, for instance, AB is a 2 x 2 matrix and BA is a 3 x 3 matrix; so AB :A BA. Moreover, even if both A and B are n x n matrices, it is possible that AB = BA. (See Exercise 30.) To conclude this appendix, we return to our example of the computer labs for an application of matrix multiplication. Suppose that the Mathematics Department wishes to know the value of the equipment in its two labs. If each of its computers costs $1000 and each of its printers costs $200, then the value of the equipment in its lab for upper-level courses is 25($1000) + 3($200) = $25,600, and the value of the equipment in its lab for lower-level courses is 30($1 300) + 2($200) = $30,400. Note that if
[
Value
10001 Upper 200] Lower
then the product matrix
F25 3 I 1000 L3 0 2 ]L 20C0
Value
F25(1000) + 3(200) l
F25,6001
Upper
30 400
Lower
L ,
L30(1000) +2(200)
j
gives the value of the equipment in each of the Mathematics Department's labs.
EXERCISES B.1 In Exercises 1-8 use the matrices
A=
l
B =
1[ 3] ,
and C=[
to compute the given matrix if it is defined. 1. A+B
2. B +A
3. C +A
4. A+ C
3
4
Appendix B Matrices 5. AB
6. BA
7. AC
8. CA
563
In Exercises 9-16 use the matrices
I]
A=[
and B=[I 2]
to compute the given matrix if it is defined. 9. AB 13. A 2 B2
10. BA
11. A2
12. B2
14. (AB) 2
15. A3
16. B3
In Exercises 17-24 use the matrices -
A=
0 0
-lI I
I
I
and B =
0 -I
0
to compute the given matrix if it is defined. 17. AB
18. BA
19. A2
20. B 2
21. A 2 B 2
22. (AB)2
23. A3
24. B 3
25. Show that, for any m x n matrices A and B, A + B = B + A. Thus matrix addition is a commutative operation. 26. Show that, for any m x n matrices A, B, and C, (A + B) + C = A + (B + C). Thus matrix addition is an associative operation. The m x n zero matrix is the m x n matrix in which each entry is zero. 27. Show that, for any m x n matrix A, A + 0 = A, where 0 is the m x n zero matrix. 28. Let A be an m x n matrix and 0 be the n x p zero matrix. Show that AO is the m x p zero matrix. 29. Let 0 be the m x n zero matrix and B be any n x p matrix. Show that OB is the m x p zero matrix. 30. Let
A =[I
]
and
B
[=2
1]
(a) Compute AB and BA. (b) This example illustrates two differences between the multiplication of matrices and the multiplication of real numbers. What are they? The n x n matrix whose i, i entry is 1for i = 1, 2, . . ., n and 0 otherwise is called the n x n identity matrix and is denoted In. 31. For any n x n matrix A, show that AI, = InA = A. 32. Prove that Inm = In for any positive integers m and n.
564
Appendix B Matrices
33. Let A be an m x n matrix and B and C be n x p matrices. Prove that A(B + C) = AB + AC. 34. Let A and B be m x n matrices and C be an n x 1pmatrix. Prove that (A + B)C = AC + BC. ogo
HISTORICAL NOTES The history of matrices traces back to Chinese mathematics in the period around 250 B.C. During that time, an unknown scribe wrote the Chui-chang suan-shu, which translates as Nine Chapters on the MathematicalArt. Like the Rhind papyrus from Egypt, the manuscript is a collection of worked problems, probably intended as a textbook for mathematics students. In considering the solution to the system of equations that would be written today as 2x+2y+ z=39 lx+3y+ z=34 x+2y-+ 3z =26, the manuscript contains the boxed array below. 1 2 3 26
Girolamo Cardano
2 3 1 34
3 2 1 39
The solution of the system was tl'en obtained by a series of operations on the columns of this rectangular array. The usage of such arrays to represent mathematical problems languished for some time after this. The Italian mathematician Girolamo Cardano (1501-1576) brought the methods back to Europe in 1545 in his Ars Magna. The Dutch mathematician Jan deWitt (1629-1672) used arrays in his Elements of Curves to represent transformations, but did not take the usage beyond that of representation. Gottfried Wilhelm Leibniz (1646-1716) was perhaps most responsible lor touring the attention of European mathematicians to the use of arrays for recording informal ion in problems and their solutions. During the period from 1700 to 1710, Leibniz's notes show that he experimented with more than 50 array systems. In the middle of the 1800s matrices moved beyond the writing of numbers in rectangular arrays to serve in the solution of equations. In 1848, James Joseph Sylvester (1814-1897) showed how arrays might be used to attack such problems more efficiently. In doing so, he called such an array of numbers a 'matrix.' In 1858, the English mathematician Arthur Cayley (1821-1895) wrote a treatise on geometric transformations. In it, Cayley looked for a way to represent the transformation
7
Ix'
= ax + by y' = cx + dy.
HistoricalNotes
565
To do so, Cayley used a rectangular array reminiscent of that used by the Chinese, but without the rotation of the array. Cayley wrote his array between two sets of vertical bars: a b
c d In working with the arrays of coefficients, Cayley recognized that operations could be defined on these arrays irrespective of the equations or transformations from which they were derived. He defined operations of addition and multiplication for these arrays, and noted that the resulting mathematical system satisfied several of the same properties that characterized number systems, such as the associative properties and the distributive property of multiplication over addition. He also noted, however, that while addition was commutative, multiplication was not. Furthermore, he noted that the product of two matrices might be zero although neither factor was the zero matrix. His 1858 paper, Memoir on the Theory of Matrices, provided a framework for the later development of matrix theory. In it, he stated the famous Cayley-Hamilton theorem and illustrated its proof with a computational example. The current bracket notation for matrices was first used by the English mathematician Cullis in 1913. His work also was the first to make significant use of the aij notation to represent the matrix entry in the ith row and jth column [73, 74, 75].
Appendix: The Algorithms in This Book T'he algorithms in this book are written in a form that, while not corresponding ar y+UW ally
1llr pati _ t,+ ta l Vadil LkUI
tmntorl
%dl. UIIIULtC
-Il
ICAllrUart-,
,
at a.C
c
-. bun
I
--
It t LLIty
+ct
-t~
CII -
tUI-1U I
i Dl.i-, +It-taf t tl--y
U-id
L
I..
be easily turned into programs. They are divided into steps, which are executed in order, subject to certain looping or branching instructions. Loops are begun by one of three special words: while, repeat, and for, printed in boldface type.
Wh l,
~
end t W I: 3; II
g
!6 I4::
i
This construction has the form below. while statement some instructions endwhile Here statement is checked. and if it is true, some instructions are executed. This is repeated until statement is false, at which point the algorithm resumes after the endwhile. An example follows.
Algorithm 1 Given a positive integer n, this algorithm computes the sum of the first n positive integers. StepI Step 2
Step 3
SetS=Oandk= 1. while k < n Replace S with S -- k and k with k + 1. endwhile Print S.
The following table show s how the values of S and k change as the algorithm is applied to n = 4. Making such a table is often helpful in understanding a new algorithm. 566
Appendix C The Algorithms in This Book k
S 0
0 +1I=
1
1 +2 =
3
3 +3 =
6
6 +4-=10
567
1+2 +1=32 23+1=43 4 +I=54
Here Step 2 would be repeated until k = 5, making the statement k < n false. Then Step 3 would be executed, printing the value S = 10. Note that the instructions between while and endwhile may not be executed even once. For example, if Algorithm 1 is applied with n = 0, nothing is ever done in Step 2 and the value S = 0 is printed.
Another looping structure has the following form.
repeat some instructions
until statement Here some instructions are executed, then statement is checked and, if found false, some instructions are executed again, etc. Only when statement is found true does the algorithm take up after the line containing until. The following algorithm has the same effect as Algorithm I if n is any positive integer.
Algorithm 2 Given a positive integer n, this algorithm computes the sum of the first n positive integers. Step]I Step 2
Set S=O0and k= 1.
repeat Replace S with S + k and k with k + 1. > n Print S.
until k Step 3
Unlike a while . . endwhile loop, the instructions between repeat and until are always executed at least once. Thus if Algorithm 2 were applied to n = 0, it would print the value S = 1.
568
Appendix C The Algorithms in This Book
When Algorithm 1 is run with a = 4, the instructions inside Step 2 are executed for k = 1, 2, 3 and 4. Although writing the algorithm this way is useful when one wants to count every elementary operation, most computer languages have a command something like 'for k = 1 to 4.' In the algorithms in this book, such loops begin with for and end with endfor. We could rewrite Algorithm I using this language as follows.
Algorithm 3 Given a positive integer n, this algorithm computes the sum of the first n positive integers. Step ] Step 2
Set S =O. for k = I to n Replace S with S +- k.
endfor Step 3
Print S.
In a for .. endfor loop, the variable may be incremented by an amount d other than 1 by adding the words by d. For example, the following algorithm computes 1 + 2 + + n by adding the larger numbers first.
Algorithm 4 Given a positive integer n, this algorithm computes the sum of the first n positive integers. Step] Step 2
SetS =O. for k = n to I by --1 Replace S with S 4- k.
endfor Step 3
Print S.
Branching in algorithms is accomplished by the if .. otherwise .. endif construction. This has the form below. if statement some instructions
Appendix C The Algorithms in This Book
569
otherwise other instructions endif Here statement is checked, and if found true, some instructions are executed. If statement is false, then other instructions are executed instead. In either case, the algorithm resumes after endif. If there are no other instructions~, then the branching construction can be shortened to the following form. if statement some instructions endif Algorithm 5 illustrates the if .. otherwise
..
endif construction.
Algorithm 5 Given a real number x, this algorithm computes its absolute value. Step]I
Step 2
if x >O Set A otherwise Set A endif Print A.
=
x.
=
-x.
In more complicated algorithms, looping and branching constructions are often nested. The example that follows has an if .. otherwise .. endif nested within a while .. endwhile. It concerns the Collatz sequence, in which a positive integer n is replaced by n/2 or 3n + 1 according as it is even or odd, and this is repeated. It has been conjectured, but never proved, that eventually the number 1 is reached, no matter what positive integer n we start with. The following algorithm counts how many steps this takes for a given n.
Algorithm 6 This algorithm counts how many steps the Collatz sequence takes to reach 1 from a given positive integer n. Step]I Step 2
Set k=O0and s-=n. while s > 1 Step 2.1 Replace kwith k+ 1.
570
Appendix C The Algorithms in This Book
Step 2.2 if s is even Replace s with s/2. otherwise
Replace s with 3s + 1. endif Step 3
endwhile Print k.
The following table shows how k and s change when the algorithm is applied ton = 3. k 0
5
3
3 3 + 1= 10 2
10/2 = 5
3
3 5+ I = 16
4
16/2 = 8
5
8/2 = 4
6
4/2 = 2
7
2/2 = 1
Thus, when step 3 is executed., the value k = 7 is printed.
In Section 4.5 are several algorithms that are recursive, in the sense that they call themselves. Below is a simple example of such an algorithm.
Algorithm 7 Givenapositive integer n,this algorithm prints outasequence of integers kl, k2 , . such that n = k2 +2 + + C Step I Step 2
Set s = n. while s > 0 Step 2.1 Set k= 1. Step 2.2 while (k + 1)2 < S Replace k with k + 1. endwhile Step 2.3 Print k.
,
Appendix C The Algorithms in This Book
Step 2.4 Apply Algorithm 7 with n = s endwhile
-
571
k2.
This algorithm finds the largest integer k such that k2 < n, prints this integer, then applies the same algorithm to n - k2. The following table shows how s and k change, and what is printed out, for n = 22. Note that 22 = 42 + 22 + 12 + 12. k
S
22
printed
I 2 3 4
4
2
2
22 - 42 = 6
6 -22 =2 2 - 12 = 1
1 1
Bibliography [1] Aho, Alfred, John Hopcroft, and Jeffrey Ullman. DataStructures and Algorithms. Reading, MA: Addison-Wesley, 1983. [2] The Design and Analysis of Computer Algorithms. Reading, MA: AddisonWesley, 1974. [3] Albertson, M. 0. and J. P. Hutc hinson. Discrete Mathematics with Algorithms. New York: Wiley, 1991. [4] Althoen, Steven C. and Robert J. Bumcrot. Introductionto Discrete Mathematics. Boston: PWS-KENT, 1988. [5] Anderson, Ian. A First Courcs' in CombinatorialMathematics. London: Oxford University Press, 1974. [6] Bamier, William and Jean C han. Discrete Mathematics with Applications. St. Paul, MN: West, 1989. [7] Behzad, Mehdi, Gary Chartrand, and Linda Lesniak. Graphsand Digraphs. 3rd ed. Boca Raton, FL: CRC Press, 1996. [8] Benedicty, Mario and Frank R. Sledge. Discrete MathematicalStructures. Philadelphia, PA: Saunders, 1987. [9] Biggs, Norman. DiscreteMatA ematics. 3rd ed. New York: Oxford University Press, 1993. [10] Bogart, Kenneth. Introductory)Combinatorics. 3rd edition. San Diego: Academic Press, 1999. [11] -. DiscreteMathematic.r. Boston: Houghton Mifflin, 1988. [12] Bondy, J. A. and U. S. R. Murty. Graph Theory with Applications. New York: Elsevier Science, 1979. [13] Bradley, James. Discrete Mathematics. Reading, MA: Addison-Wesley, 1988. [14] Brualdi, Richard A. Introdticory Combinatorics. 3rd ed. Paramus, NJ: Prentice Hall, 1998. [15] Busacker, Robert, G. and Thomas L. Saaty. Finite Graphs and Networks: An Introduction with Applications. New York: McGraw-Hill, 1965. [16] Chartrand, Gary. Graphs as MathematicalModels. Boston: Prindle, Weber & Schmidt, 1977. [17] Cohen, Daniel 1.A. Basic Techniques of CombinatorialTheory. New York: Wiley, 1978. [18] Dierker, Paul F. and William L. Voxman. Discrete Mathematics. Philadelphia, PA: Saunders, 1986. [19] Doerr, Alan and Kenneth Levaseur. Applied DiscreteStructuresfor Computer Science. 2nd edition. New York: Macrrillan, 1989. [20] Epp, Susanna. Discrete Mathematics with Applications. Boston: PWS Publishers, 1990. [21] Even, Shimon. GraphAlgorithms. New York: Freeman, 1984. [22] Finkbeiner, Daniel T., II and Viendell Lindstrom. A Primerof DiscreteMathematics. New York: Freeman, 1987. [23] Fletcher, Peter, Hughes B. Hovle III, and C. Wayne Patty. Foundationsof Discrete Mathematics. Boston: PWS-KENT 1991.
572
Bibliography
573
[24] Gerstein, Larry. Discrete Mathematics and Algebraic Structures. New York: Freeman, 1987. [25] Gersting, Judith. MathematicalStructuresfor Computer Science. 2nd edition. New York: Freeman, 1987. [26] Grimaldi, Ralph. Discrete and CombinatorialMathematics. 4th edition. Reading, MA: Addison-Wesley, 1998. [27] Grossman, Jerrold. Discrete Mathematics. Paramus, NJ: Prentice Hall, 1996. [28] Harary, Frank. Graph Theory. Reading, MA: Addison-Wesley, 1994. [29] Hillman, A., G. L. Alexanderson, and R. M. Grassl. Discreteand CombinatorialMathematics. San Francisco: Dellen, 1987. [30] Hirschfelder, R. and J. Hirschfelder. Introductionto DiscreteMathematics. Pacific Grove, CA: Brooks/Cole, 1991. [31] Hu, T. C. CombinatorialAlgorithms. Reading, MA: Addison-Wesley, 1982. [32] Johnsonbaugh, Richard. Discrete Mathematics. 5th edition. Paramus, NJ: Prentice Hall, 2000. [33] Kalmanson, Kenneth. An Introductionto Discrete Mathematics. Reading, MA: AddisonWesley, 1986. [34] Kincaid, David and E. Ward Cheney. Introductionto DiscreteMathematics. Pacific Grove, CA: Brooks/Cole, 1991. [35] Knuth, Donald. The Art of ComputerProgramming,Vol 1. 3rd ed. Reading, MA: AddisonWesley, 1997. [36] . TheArtofComputerProgramming,Vol2. 3rded. Reading, MA: Addison-Wesley, 1997. [37] . The Art of Computer Programming, Vol 3. 2nd ed. Reading, MA: AddisonWesley, 1998. [38] Kolman, Bernard and Robert Busby. Discrete Mathematical Structuresfor Computer Science. 4th edition. Paramus, NJ: Prentice Hall, 1999. [39] . IntroductoryDiscrete Structures with Applications. Paramus, NJ: Prentice Hall, 1987. [40] Lawler, Eugene L. CombinatorialOptimization: Networks and Matroids. Mineola, NY: Dover, 1976. [41] Liu, C. L. Elements of Discrete Mathematics. 2nd edition. New York: McGraw-Hill, 1985. [42] . Introduction to CombinatorialMathematics. New York: McGraw-Hill, 1968. [43] Maurer, Stephen B. and Anthony Ralston. Discrete Algorithmic Mathematics. 2nd ed. Nantick, MA: A. K. Peters, 1998. [44] McEliece, Robert, Robert Ash, and Carol Ash. Introduction to Discrete Mathematics. New York: McGraw-Hill, 1989. [45] Minieka, Edward. Optimization Algorithms for Networks and Graphs. 2nd ed. New York: Marcel Dekker, 1992. [46] Molluzzo, John C. and Fred Buckley. A FirstCourse in Discrete Mathematics. Prospect Heights, IL: Waveland Press, 1997. [47] Mott, Joe, Abraham Kandel, and Theodore Baker. Discrete Mathematics for Computer Scientists. 2nd edition. Paramus, NJ: Prentice Hall, 1985. [48] Nicodemi, Olympia. Discrete Mathematics. St. Paul, MN: West, 1987. [49] Niven, Ivan. Mathematics of Choice. Washington, DC: Mathematical Association of America, 1965. [50] Norris, Fletcher. DiscreteStructures:An Introductionto Mathematicsfor Computer Science. Englewood Cliffs, NJ: Prentice Hall, 1985.
574
Bibliography
[51] Pfleeger, Shari Lawrence arnd David W. Straight. Introduction to Discrete Structures. Melbourne, FL: Kreiger, 19%5. [52] Polimeni, Albert D. and Joseph Straight. Foundationsof Discrete Mathematics. 2nd edition. Pacific Grove, CA: BrookLs/Cole, 1985. [53] Prather, Ronald E. Elements oj'DiscreteMathematics. Boston: Houghton Mifflin, 1986. [54] Reingold, Edward M., Jurg N4ievergelt, and Narsingh Deo. CombinatorialAlgorithms: Theory and Practice. Englewood Cliffs, NJ: Prentice Hall, 1977. [55] Roberts, Fred S. Discrete Maihematical Models. Englewood Cliffs, NJ: Prentice Hall, 1976. [56] . Applied Combinatorics. Englewood Cliffs, NJ: Prentice Hall, 1984. [57] -. Graph Theory and Its applicationsto Problems of Society. Philadelphia: Society for Industrial and Applied Mathematics, 1978. [58] Roman, Steven. An Introduction to Discrete Mathematics. 2nd edition. Philadelphia: Saunders. 1986. [59] Rosen, Kenneth H. Discrete Mathematics and Its Applications. 4th edition. New York: McGraw-Hill, 1998. [60] Ross, Kenneth and Charles Wright. DiscreteMathematics. 2nd edition. Englewood Cliffs, NJ: Prentice Hall, 1988. [61] Ryser, Herbert John. CombinatorialMathematics. Washington, DC: Mathematical Association of America, 1963. [62] Shiflet, Angela. DiscreteMathematicsfor ComputerScience. St. Paul, MN: West, 1987. [63] Skvarcius, R. and W. B. Robinson. Discrete Mathematics with Computer Science Applications. Reading, MA: Addison-Wesley, 1986. [64] Stanat, Donald and David McAllister. DiscreteMathematics in Computer Science. Englewood Cliffs, NJ: Prentice Hill, 1977. [65] Stanton, Dennis and Dennis White. Constructive Combinatorics. New York: SpringerVerlag, 1986. [66] Stone, Harold. Discrete Mathematical Structures and Their Applications. Chicago: Science Research Associates, 1973. [67] Townsend, Michael. Discrete Mathematics:Applied Combinatoricsand Graph Theory. Reading, MA: Addison-Wesle), 1987. [68] Tremblay, J. P. and R. Manofar. Discrete MathematicalStructures with Applications to Computer Science. New York: MAcGraw-Hill, 1975. [69] Tucker, Alan. Applied Combiaotorics. 3rd ed. New York: Wiley, 1994. [70] Wiitala, Stephen. DiscreteMatnematics: A Unified Approach. New York: McGraw-Hill, 1987. REFERENCES FOR THE HISTORICAL NOTES [71] Biggs, N. L. 'The Roots of Combinatorics.' HistoricaMathematica, vol. 6 (1979): 109136. [72] Biggs, N. L., E. K. Lloyd, and R. J. Wilson. Graph Theory: 1736-1936. Oxford, UK: Clarendon Press, 1986. [73] Boyer, C. B., and U. C. Merzbach. A History of Mathematics. 2nd edition. New York: John Wiley & Sons, 1989. [74] Burton, D. M. The History of Mathematics: An Introduction. Dubuque, IA: William Brown, 1991. [75] Cajori, F. A History of Mathematical Notations (2 vols). La Salle, IL: Open Court Publishing, 1928-29.
Bibliography
575
[76] Gardner, Martin. Logic Machines and Diagrams. New York: 1958. [77] Goldstine, H. The Computerfrom Pascal to von Neumann. Princeton, NJ: Princeton University Press, 1972. [78] Heath, T. A History of Greek Mathematics (Vols 1-2). New York: Dover Publications, 1981. [79] Heath, T. Euclid's Elements (Vols 1-3). New York: Dover Publications, 1956. [80] Houser, N. 'Algebraic logic from Boole to Schroder, 1840-1900.' In I. Grattan-Guinness (ed.), Companion Encyclopedia of the History and Philosophy of the MathematicalSciences (Vol 2, pp. 600-616). London: Routledge, 1993. [81] Kleiner, I. 'Evolution of the Function Concept: A Brief Survey.' College Mathematics Journal, vol. 20, no. 4 (1989): 282-300. [82] Kline, M. Mathematical Thought from Ancient to Modern Times. New York: Oxford University Press, 1972. [83] Perl, T. Math Equals: Biographies of Women Mathematicians and Related Activities. Menlo Park, CA: Addison Wesley, 1978. [84] Rodrfquez-Consuegra, F. A. 'Mathematical Logic and Logicism from Peano to Quine, 1890-1940.' In I. Grattan-Guinness (ed.), Companion Encyclopedia of the History and Philosophyof the Mathematical Sciences (Vol 2, pp.617-628). London: Routledge, 1993. [85] Schreiber, P. 'Algorithms and Algrothmic Thinking Through the Ages.' In I. GrattanGuinness (ed.), Companion Encyclopedia of the History and Philosophy of the Mathematical Sciences (Vol 2, pp. 687-693). London: Routledge, 1993. [86] Todhunter, I. A History of the Mathematical Theory of Probability. Cambridge, UK: Cambridge University Press, 1865. [87] Wilson, R. J., and E. K. Lloyd. 'Combinatorics.' In I. Grattan-Guinness (ed.), Companion Encyclopedia of the History and Philosophy of the Mathematical Sciences (Vol 2, pp. 952-965). London: Routledge, 1993.
Answers to Odd-Numbered Exercises
CHAPTER 1 EXERCISES 1.1 (page 8) 1. 33; A-B-D-F-G or A-C-E-F-G 5. 20.7; A-D-H-K 3. 43; B-D-F-G 9. 23; A-D-F-G B C
A
13. 15.7; D-I
A
C
E
3
4
-6
B 5
D 2
F -7
G
: Gr D
15. 0.29; E-F-C-D-I A B E
7. 2.1; A-C-IH-FI-.[
11. 24; B-C-F-G
E
17. 27 minutes
19. 15 days
D
FI.
EXERCISES 1.2 (page 15) 1. 120 19. 720
576
3. 6720 21. 288
5. 28 7. 840 9. 604,800 11. 25.2 23. 210 25. 60 27. 20,118,1)67,200
13. 720 29. 60
15. 56
17. 362,880
Answers to Odd-Numbered Exercises
577
EXERCISES 1.3 (page 22) 1. F 3. F 5. F 7. T 19. 1,4,5,6,7,8,9,11,12;51
EXERCISES 1.4 (page 33) 1. yes; 2 3. no 5. no
15.
k
i
a,
a2
a3
3
1
0
1
2
1 1
1 1
1 0
3
21.
al
a2
6
33
20
6
33
(2
6
33 9
a3
92
' 33
a4 200 09
9. F 11. F 13. F 15. yes, 28 17. yes, 35 21. 128 23. 32 25. n-m+l 27. 31 29. 12.7days
7. 3, 13; 5, 13 17. k j a, 4 3
4
a5
j
k
9
1
4
200
3
20
200
2
20
200
1
6
9
33
(
(a)
6
9
(
)
200
6
&
()33
6
9
20
()
(
6
9
(@)
6j)
6
9
20
6
9
20
2
33
1 1
1 1
0
1
13
56
87
1
1
13
56
4
1
1
1
0
®4256 13
42
13
(
13
42
(3
13
42
56
1
13. 001110
k 3
87
2
87
1
(3
2 87
3 2
3
3
87
4 3
200
2 3
200
()
9. -7, 3, 11, 3; -1, 0, 5, 3 11. 110110 a3 a4 19. ai a2 a3 a4 j
a2
4 3
4
4
200
23. 58 minutes; 0.8 seconds 25. 385,517 years; 6.4 seconds 29. 4n-2 31. -7, 3, 11, 3
27. 3n + I
SUPPLEMENTARY EXERCISES (page 36) 1. 18; B-D-G-I 3. 28 minutes 19. 80 21. yes; 100 23. no
5. 332,640 7. 990 25. 3, 9, 31, 88
9. F
11. F
13. T
15. T
17. 32
578 27.
Answers to Odd-Numbered Exercises al
a2
44
44 44
5 5
a5
j
k
( 35
1
4
a3
a4
5
13
cm
5 (
ci'13
35
2
)
11
13
35
1
11
(@
44
(0
(g 13
(
(93
5
1
44
5
11
44
5
11
13/' 44
5
11
13
®
35
31, 4n- 3
3
2
35 35 (
13
29. 39
4
3 2 3
35
4 3
4
4
44
CHAPTER 2 EXERCISES 2.1 (page 46) 1. (1,2,3,4,5,6,7,8,91;{3,5};12,7,81;{1,4,6,9};{2.7,8) 3. {1,2,3,4,7,8,9};0;t1,2,4,8,9};{3,5,6,71;{1,2,4,5,5,8,91 5. {(1,7), (1, 8), (2,7), (2, 8), (3, 7), (3, 8), (4,7), (4, 8)) 7. {(a, x), (a, y), (a, z), (e, x), (e, y), (e, z)} 9. 11.
13. A={l},B={2,C={l,2} 15. A={1,21,B=-={:,3J,C=[{2,3} 17. 0 23. A-B 25. mn 27. B CA 39. A =t1},B = {21,C={3},D={4)
19. A-B
21. B-A
EXERCISES 2.2 (page 52) 1. symmetric and transitive 3. reflexive, symmetric, and transitive 5. reflexive and symmetric 7. reflexive, symmetric, and transitive 9. reflexive, symmetric, and transitive 11. reflexive and transitive 13. [z] is the set of odd integers, 2 equivalence classes 15. [z] is the set of integers greater than 1 that are divisible by 5 but no y any prime greater than 5, infinitely many equivalence classes 17. [z] consists of all ordered pairs (x, y) that satisfy the equation x2 A y 2 = 52, infinitely many equivalence classes 19. {(I, 1), (1,5), (5, 1), (5, 5), (2,2), (2,4), (4, 2), (4,4), (3, 3)1 23. There may be no element related to x; that is, x R y may not be true for any y. 25. 2' 27. 2' 1 - 1 29. 15
Answers to Odd-Numbered Exercises
579
EXERCISES 2.3 (page 58) 1. q =7,r=4 3. q = 0, r = 25 5. q =-9,r=0 7. q = -9,r = 1 9. p = q (mod m) 11. p # q (mod m) 13. p # q (mod m) 15. p - q (mod m) 17. [2] 19. [4] 21. [6] 23. [1] 25. [21 27. [8] 29. [4] 31. [4] 33. [2] 35. [11] 37. 8 PM. 39. 9 41. 7 43. -10,224 and 29,202 45. No, 10 E A but 10 B. 47. 3 R I Iand 6 R 10 are true, but both 9 R 21 and 18 R 110 are false. 49. The relation is not well defined if m does not divide n.
EXERCISES 2.4 (page 70) 1. not reflexive 3. a partial order a 7.
5. a partial order
7. not transitive
11 I I.
6
5
7
I
13. ((2, 2), (2, 6), (2, 12), (3, 3), (3, 6), (3, 12), (6, 6), (6, 12), (12, 12)1 15. ((2, 2), (x, x), (x, A), (A, A), (0, 0)} 17. 1 is a minimal element; 4,5, and 6 are maximal elements 19. 2,3, and 4 are minimal elements; 1 and 2 are maximal elements 21. RU ((1,2), (1, 4)} 23. For Al = 0, A2 = (1}, A3 = (2}, A4 = (31, A5 = (1, 2}, and A6 = (1, 31, define Ai T A, if and only if i < j. 25. No 27. (1, 2,4,8, 16} 29. S = (2, 3, 4, 5, 6, 9, 15) with x R y if and only if x divides y 31. 4I
I0
0
4
4
37. The lexicographic order is a total order on SI x S2.
41. n!
EXERCISES 2.5 (page 82) 1. function with domain X 3. not a function with domain X 5. function with domain X with domain X 9. not a function with domain X 11. function with domain X 13. 8 19. 37. 45. 53.
7. not a function 1 15. 17. 2 4 35. 9.97
-9 21. 3 23. 0 25. -4 27. -5 29. 5.21 31. -0.22 33. 0.62 8x + 11; 8x -5 39. 5(2X) + 7; 25x+7 41. Ix1(log2 Ixi); Ix log2 x 43. X 2 - 2x + 1; x 2 - 1 one-to-one; not onto 47. one-to-one; onto 49. onto; not one-to-one 51. neither one-to-one nor onto f -(x)=-
580
Answers to Odd-Numbered Exercises
55. f -'(x)
=
-x
57. f -'(x) =x 3
59. doesnotexis.
61. Y
=
{x
1 E X: x > 0}; -'(x) =-1 +
log2
(G)
63. n'
EXERCISES 2.6 (page 91) 1. 1, 1,2,3,5,8, 13,21,34,55
3. 3,4,7, 11, 18,29.47, 76
5. Let xn denote the nth even positive integer. Then x, = established.
I so(r+l- 1)
27.
2
±2
if n > 2.
9. The proof of the inductive step is faulty because x - 1 and y
7. No base for the induction was -
1 need not be positive integers.
ifr 6 1
ri-I
1so(n +1)
if r-=1
EXERCISES 2.7 (page 99) 1. 21 21. 792
3. 252 23. 20
5. 330 25. 120
7. 462 27.
9. 1 52! 13! 39!
qln - 1) 2 4.n(n - 3)
13. 64
15. 128
17. 256
19. 21
47. Mr. and Mrs. Lewis each shook n hands.
SUPPLEMENTARY EXERCISES (page 103)
1.
131
3. (5, 61
5. {61
7. (2}
9.
13. 37 # 18 (mod 2) 15. -7 = 53 (mod 12) 17. 191 19. [1] domain X 27. function with domain X 29. one-to-one, not onto exist 35. f -'(x)= -(x + 6) 37. 32 3 43. the sets {2n - 1, 2nI for every integer n 53. reflexive, antisymmetric, and transitive 65. f must be one-to-one
CHAPTER 3
OH
21. [3] 23. 10 25. function with 31. onto, not one-to-one 33. does not
39. 5005 41. 11,51, {2, 6}, 13, 7}, 14, 8} 47. 5 49. R = {(s, s): s E SI 51. antisymmetric and transitive 55. A, B. C, 1), E, F, G, H, I, J, K 57. R = {(s, s): s E SI
Ut
EXERCISES 3.1 (page 115) 1. V=(A,B,C,D};-=({A,BI,{A,CI,{B,C},{B,D),{C,DI)
7- GO
11.
9. yes
11. no
13. no
3. V={F,G,H},0
5. A L
D
Answers to Odd-Numbered Exercises
C I a-
For a Female
You
rora Male
Grandfather
Grandmother You
nal Grandfather
Grandmother
17. S
19. A: B, C, D, E (degree 4); B: A, C, F (degree 3) A
T
C
D
21. (a) 27.
0
I
(b)
[0
1
0
0
1-
VI: V2, V5
I
0 1 0
1 0 0 1 1 0
0 0 1
V2 : VI,V 3 V3 : V2,V4
0
0
0-
V5 : VI, V 4
0
0 -1
31-
B
1
VI
V2
V3
V4
23. 3,6, 10, C(n, 2) = 29.
~2
0
1
01
VI
I
0
0
O
0
0
V2: VI V3 : none
V2
V4 : V 3 , V 5
33.
VI
V2
V3;
V4
35. yes
37. No, diagonal entries are nonzero. 39. no 41. no 43. (a) yes (b) No, the first graph has two vertices of degree 2.
(c) yes
25. 10
581
582
Answers to Odd-Numbered Exercises
45. 6
*
*
*
*
*
*-O *
*-0
47.
49 - if n is even and 2 2 53. Each shook 3 hands.
if n is odd.
51. The least irteger greater than or equal to I
2
EXERCISES 3.2 (page 131) 1. a graph 3. not a graph 5. parallel edges: none; loops: a, c 7. parallel edges: a, b, c, d; loops: none 9. (i) c length ; a, c length 2; a, c, b length 3 (ii) c length 1 11. (a) 17. yes 33. no 41.
(a)
(iii) all have c as a simple path (b) (c)
19. no 35. yes
(iv) a length 1; b length I 13. yes
21. yes, d,a,b,c,e,g,k,m,h,i, j, f 37. yes 39. c, h, i, d, a and g,.kf (b)
*
(c)
A-
23. no b, e, j, nm ?
(d)
15. no
25. no
27. no
29. no (e)
*
31. no
Answers to Odd-Numbered Exercises
43.
Q
Q
Q
CYO
,:3Q
0 s
-
sq
.
-- i
CY .0-C
q 0---* 0A p.
p----
583
584
Answers to Odd-NumberedExercises
45. i-
(:cz)
Q,>
QQ 0--
pp
*
0
*
0-p1°
0-O
QQ
Y
Ei1°
-
*
p
* p
(,)
*Q-x*;
51. No, the second graph has a cycle of length 3. 49. yes 47. yes 53. m and n are both even. (c' IM, 0, Q}, {N}, {P, R, S, T} 59. (a) {A, C, G, E}, {B, D, H, F) (b) [I, J, K, L)
)
Answers to Odd-Numbered Exercises EXERCISES 3.3 (page 145) 1. 5;S,D,G,E,F,T I (S) 2(D) 4(E) A B C 3(G)4
3. 7;S,A,F,G,B,C,O,T °(-) 1(S) 4(G) S A B
585
5(B) C
4(E) 5(F) F T
E
°( 0(
I1(S) G 2(D)
H 3(G)
I 4(H)
P Q R U 6(L) 5(M) 6(Q) 7(0) 5. A: 8; B:9;C: 3; D:5; E:4; F:6;G:7;H:5;1:6; shortestpathtoA: S,C,E,F,G,A 7. A: 8; B:6;C:3;D:5; E:2; F:3;G:6;H:l;shortestpathtoA:S,C,D,G,A 9. S, E, F, K, L, G, A, G, L, M, T; Find shortest path from S to A and then from A to T. 11. S, F, A, C, D, E, T; Find shortest path from S to A and then from A to T. 13. From VI to V2: 1, 2, 7, 20; from V2 to V3: 1, 2, 7, 20 15. From VI to VI: 0, 4, 8, 34; from V4 to V3: 0, 2, 4, 18 17. the number of paths from Vi to Vj with length at most 3
EXERCISES 3.4 (page 155) 1. 3 3. 3 5. 3 7. 2
9. There are no edges.
11. (a)
(b)
13. This algorithm uses two colors (red and blue) to color a graph having no cycles of odd length. In the algorithm, 1 denotes the set of labeled vertices (those that have been colored). Step I (initialization) Let = 0. Step 2 (color all the vertices in another component) repeat Step 2.1 (color some vertex in an uncolored component) (a) Select a vertex S not in 1L. (b) Assign S the label 0, and color S red. (c) Include S in C. (d) Set k = 0.
Step 2.2 (color the other vertices in this component)
repeat Step 2.2.1 (increase the label) Replace k with k + 1. Step 2.2.2 (enlarge the labeling) while 1 contains a vertex V with label k is adjacent to a vertex W not in 1 (a) Assign the label k to W.
-
I that
586
Answers to Odd-Numbered Exercises (b) Color W red if k is exen and blue if k is odd. (c) Include W in L. endwhile until no vertex in £ is adjacent to a vertex not in L until every vertex is in £
15. r
b
r
b
b
bbrl
bIIr
17.
r
r
bi
II by
r'
biiŽ 21. nn
r
b
r
23.
Jr
b
r
T
- 11--.6
b 19. 5
b o
b
-D
bb
b
r
r
b
25.
27. Three separate meeting times are needed with finance and agriculture meeting at the same time and likewise for budget and labor. 29. 3 EXERCISES 3.5 (page 169) 1. vertices: A, B, C, D directed edges: (A, B), (B, D), (C, A), (C, D) 5. X 7. A I
3. vertices: A, B, C, D directed edges: (A, B), (A, C), (B, A), (C, D), (D, C) 9. V] i VI I
11.
V3
13. to A: B and C, from A: B and D, indegree 2, outdegree 2 15. to A: B, C, D, and E, from A: none, indegree 4, outdegrec 0 17. (i) A, B (); A,C, B (2);A, D, B( A( 3) ; C,A,D, BA ,(3) (ii)DA, B,A(2); C, D, C(2);A,C, B, A(3); A,D, B, A(3); A, C, D, B,A(4); A, D,C, B, A(4)
Answers to Odd-Numbered Exercises 19.
-O
I
0
1-
1 I
0 I
1 0
0 0
-O
I
0
0
A: B, D B: A, C C: A, B D: B
21.
-0 0
0
0
0]
I
0 0 0 0
I
I 0 0 1 0 0
I
0
0 -1 0 0 0 0-
A: none B:A C:A,B D:A,B E: A
La.
25.
*---
*
Ki
21.
29. There is a directed edge from each vertex to itself. 31. If there is a directed edge from A to B and one from B to A, then A = B. 33. -,l1(A, B) is a directed edge in the directed graph of Exercise 32 if and only if (B, A) is a directed edge in the directed graph of Exercise 33. z 35. yes
39. no
37. 0
41.
0
43. Any vertex can be reached from any other along the directed Hamiltonian cycle. 45. Eulercircuit:a,c,g,i,j,k,h,f,e,d,b 47. Euler path: j,g,f,nok,h,i,d,a,b,c,e,m 49. Neither, there is a vertex with indegree 3 and outdegree 1. 53. only 1; d, b, c
587
588
Answers to Odd-Numbered Exercises
55. Cookies, ice cream, eclairs, pie, pudding is the only ranking. 57. B and C have maximum score of 3. There is a directed path of length 1 from B to A, D, and E and one of length 2 to C. There is a directed path of length I from C to B, D, and E and one of length 2 to A. 59. Bears, Vikings, Packers, Bucs, Lions is the only ranking. 61.
This algorithm determines the distance and a shortest directed path in a directed graph from vertex S to every other vertex for which such a path exists. In the algorithm L denotes the set of labeled vertices, and the predecessor o vertex A is a vertex in C that is used in labeling A. Step I Step 2
(label S) (a) Assign S the label 0, and let S have no predet ssor. (b) Set L ={S) and k = 0. (label vertices) repeat Step 2.1 (increase the label) Replace k with k + 1. Step 2.2 (enlarge labeling) while £ contains a vertex V with label k - ] and with a directededge to a vertex W not in C
(a) Assign the label k to W. (b) Assign V to be the predecessor of W. (c) Include W in L. endwhile until no vertex in L has a directed edge to a lerie_- not in L Step 3 (construct a directed shortest path to T) if a vertex T is in L The length of a directed path from S to T is the Iabel on T. A shortest directed path from S to T is formed by taking in reverse order T, the predecessor of T, the predecessor of the predecessor of T, and so forth, until S is reached. otherwise
There is no directed path from S to T. endif 63. 65. 67. 69. 73. 75. 77.
S, B,G, N, H,C, D, I, Q, J, K, T; length 11 S,A,F,G,M,N,V,W,O,I,D,T;lengthll 5toA, lOtoB,4toC, 3to D,5toE, 2toF,4toG; S, F, G, A 7toA,lltoB,5toC,6toD,14toE,2toF,5toG,6toH,8tol;S,F,G,H,A from VI to V4: 0, 2, 1,4; from V4 to VI: 1, 0, 2, 2 the number of directed edges; a directed path of length n (a) no (b) no
SUPPLEMENTARY EXERCISES (page 176) 1.
J.
y
X
R
z
S
W
Answers to Odd-NumberedExercises
5. No, the first graph has a vertex of degree 2.
7. V
V f
589
9. no
3
4
V5
13. yes; a, b, d, h, j, i, g, f, e, c 15. yes 17. 6;S,D,H,E,F,J,T 19. lltoA,13toB,2toC,7toD,3toE,4toF,6toG,8toH,12to 1, 5 toJ,9toK, 13toL; S,C,D,H,A; S,C,E, J,K,B 21. from VI to V2 : 0, 1, 1,4; fromVI to V4:0, 1,1,4 23. 27. 4 29. VI 11. Yesteardownanybridge andconnecttheothertwolandmassesby anewbridge.
(r
~
b
b
r~
--
31.
------- ----
iiJ. UUIJU0t(JUtIHIU
37. from VI to V4: 1, 0, 2, 4; from V2 to V5: 0, 1, 1, 4 39. It is reflexive when there is a directed loop at each vertex. It is symmetric if whenever there is a directed edge from A to B, then there is a directed edge from B to A. It is transitive if whenever there is a directed edge from A to B and a directed edge from B to C, then there is a directed edge from A to C.
CHAPTER 4 EXERCISES 4.1 (page 190) 1. yes 3. no 5. no 1' .L An. F,
s-.
ISq
-
-
7. yes -
-
9. 16 -
*
11. Connect Lincoln to each other town, using only 6 lines. 17 .
,
.
n-4-1
21.
,
=
,
,
.
.
1 9 .
t
,
590
Answers to Odd-Numbered Exercises
23.
butane
I .
H
H
IC t-
IC t-
-
H
H
IC
IC t-
H II C
o H
I.41 I.- 1 I 1 I4',
1 a
2
3
2
******
1
1
.3
0
I
1
3
IC
I0
I
H
H
-s
H11
W
29. 2; 1;3
2
H
31. 2,2,5,5,5
_ 1
2'1
33.
O-*H
H H
27.
25. no
isobutane
33.
1
37. 1
3 4 3
2
5
6 *
4 40 4
3 6 5
EXERCISES 4.2
I. 8
6
(page 205)
1.
3-
B
5.
u
0
-
D 8
4 F
A
A
G
F
0
H F
K
-
L I
7
Af
N O
9. no
I.
X
D
D;i e 15. lies
1.Ii 1j.
n
+n
tWf
.E
K
L
Answers to Odd-Numbered Exercises
17. a,c,g, f;9
19. c,a,d,e,k, f,i, j;21
25. d, e, b, c; 18
27. m, j,g,h,e,n,a,b; 31 33. b,c,d,e,k, f,i, j
3
1.
591
21. g, f,c,a;9 23. k,e, f,i, j,d,c,a;21 29. (1,5},{5,61,{6,41,{4,21,{2,7},{6,3] 2
35.
4 ~3 '3 4 37. If the package cost 26 cents to mail, the greedy algorithm would use one 22-cent stamp and four 1-cent stamps. However, two 13-cent stamps would also do the job with fewer stamps. 39. i, m, d, f, g, b, c, n, a 41. k, f, j,c,e,g,b,d,q,i,o
EXERCISES 4.3 (page 220) 1. A,C, F, B, D, E,G, H 7- A C 9
43. b,k,e, f,i,c, j,d
3. A,B,E,C,D,H, J,I,G,F A 11. H I E
5. A,C,E,B,F, J,D,G,H,I B
*C /
H B
DC
A
F GCC
E HD DF&
J 6-.
JI
I
G
15. (A, E}, {B, F}, {C, HI, IC, I} 13. {A, H),IF, E), (B, El, (G, C}, {H, F} 19. There is no bridge. 21. There is a bridge, (B, El. 27 23. A r 25. 1 . A C
17. (A, I}, {FC}
l
I A
4i
H
G'
0k
4D
GNII
JF
K
F
B
I H
IC
E
F
B
D
31. 2 39.
33. (n-1)! 41. 12131231
71
M
H Ni
~_O
592
Answers to Odd-Numbered Exercises
EXERCISES 4.4 (page 229) 1. yes 3. no 5. no 7. no 11. 13.
S-expression atom
9.I
list
number
symbol
fixed point
floating-point
15. A vertex would have indegree greater than 1. 17. This algorithm directs the edges of a tree with a vertex labeled R to transform it into a rooted tree with root R. Step I
(label the root) Label R. Step 2 (direct the edges) while there are unlabeled vertices (a) Find an unlabeled vertex V that is adjacent to a labeled vertex U. (b) Direct the edge joining U and V from U to V. (c) Label V. endwhile
19. There is only one way. 21. (i) A (ii) A, B, C, D, h', I (iii) J, K, L, E, F, G (iv) C (v) D, E, F (vi) H, 1, J, K, L (vii) A, B, D 23. (i)E(ii)E,A,D,IJ(iii)1,K,G,F,H,C(iv)D(v)none(vi)G(vii)E,J 25. 41 27. f29. M - 111 I *J I IJ 4 4
W D
D
L
I21
I1I
141
{31
4 40
WW WDWL
41
DW DD DL
LW LD LL
4
31.
33. 0
(1,2] - (3,4) I') - {2) {mH {3} - (4}H (4)L {3}L
III - {2} (2)L 3) - I
{ I}L
{3}H (4)H
35. 2
37. 1
Answers to Odd-Numbered Exercises EXERCISES 4.5 (page 244) 1. + 3.
5.
*
7
9
. B
- E
+
a x)~cb
-/1
+I
cd
a
b
e
f
b
* C
11.
d d 15. A,B,D,F,C,E,G
13. A,B,C
H
f
e
z 17. A, B, D, G, L, E, H, M, I, N, C, F, J, 0, P, K, Q
19. B,C,A 21. F,D,B,G,E,C,A 25. B,A,C 27. D,F,B,A,E,G,C 29. L, G, D, B, M, H, E, N,l, A, C, 0, J, P, F, K, Q 31. + * abc 33. */-abc + d/ef 39. ab -c/def / + * 35. + * a + *b-*c + *def ghj 37. ab * c+ + *j+ 43. 13 47. -2 49. 39 45. 13 41. abcde * f + *g-*h 51. * 53. +
23. L, G, D, M, H, N,I, E, B, 0, P, J, Q, K, F, C, A
xx B
55.
DA
C
57.
C
59.
D
61.
A 0
B
B
D
EA
E
A
F
C F
63.
3
3 2
1
2
EXERCISES 4.6 (page 261) 1. no 3. no 5. no 7. a=1,b=1,c=1
11. 1%
9 0
1 00
0110
I11
593
594
Answers to Odd-Numbered Exercises
13.
17.
15. 00
10z 110z
0100 001
1110 1111
19. BATMAN 31. 30
21. TONTO 33.
12
25. THEHAT!S .NS. 277
23. GOGO 91
27. DOG 37. 955
29. QUIET
18
6
6 8 2
10
4
1
4
39. R: 0,: I H: 101, V: 100 41. 1: 100, 2: 1111, 3: 1110, 4: 01001, 5: 0101, 6:00, 7: 011)00, 8: 011, 9: 1 10, 10: 101 49. OIL. 00. 14 LABEL BEGIN
SET
3
END D ,OR
DO
1
6
i*
20
THEN
$
GOTO PACKED
2 5
16 18 7
ELSE
55.
17
57.
LABEL BEGIN
14
SET END
DOW
3 1
6
15
20
GOTO THEN PACKED
2
FILE ELSE
17
47
510
16 18
Answers to Odd-Numbered Exercises
59.
el
0t.
Z1
MA
Ito
.3.
LABEL BEGIN
SET END
DO
$
OR THEN GOTO
ELSE
65.
67.
14 10 3
8-15
18 1215
69.
11
20
71.
6
10
14
*20
19
73.
ORD
CHROM/^-U BOOLEAN S CP // ABS
GET i
COPY GET
RSEEKD
/WRITELN
2
UE PAGE PUT
SUPPLEMENTARY EXERCISES (page 266) 1.6 3.1 9. ' ,
NH ZZ
L i
11. 10
xxxxE
13. no
17. yes
595
596
Answers to Odd-Numbered Exercises
19. A
23.
D
- G E
39.
H
A B
E
F
E
H
25. yes
B
A %B
41. *a * bc=* * abc,abc * *=ab * c* 45.
43. yes 4'.
can
00
27. pq + I
would
l
CHAPTER 5 EXERCISES 5.1 (page 276) 1. 2 3. 6 5. 0 7. yes 9. no 11. {31 :13. {1, 2,3,4,5) 21. Amy, Burt, Dan, and Edsel like only 3 flavors among them. 25. Timmack, Alfors, Tang, Ramirez, Washington, Jelinek. Rupp 29. 2n
15. {1, 3, 4, 6}
EXERCISES 5.2 (page 283) 1. VI ={1,3,6,8,9,11,13},V 2 =(2,4,5,7,10,12} 3. no 5. no 7. t{1, 2}, {3, 4), (5, 6), {7, 8), (9, 101, (12, 13)), ((1,4), [3, 5, (6, 7)), {{1, 2), (3, 4)} 9. {2,4,5,7, 10, 12}, {1,3,6,71, (1,2,4}
17. n!
19. 0
Answers to Odd-Numbered Exercises
11. -I0
0
00 O-
13
1 011 0 010
1
1 00 0 1
0
O 1 1 0 0
1
1 0
0
0 0 0 00
F0 0 0 * -main
* O O O
O *
O
O O
*
11
10
i
1 0 0 0
1 O 1 0
0 O 0 1
1 0 0 0 1 0 0 0 1 0 0 0
0 0 0 0 1 0
O O O O
* O O *
O O O O
O
O O
O O
O
1
1
LJ 00
O O
15. -I I 0 0 0 00 0 1 0 0 0
0
011 010
O1 1 0
000
Lo01 17. [*
1
1 0I 10 O
-
diagonal
O0 0 0 * O - 000 O 00000 LOO 19. row named 5, columns named 2, 6, 8, 10; rows named 3, 5, column named 8; all rows T N F OG S Z1. r . * . A
B
l
o0
0
AA
1
0
1* 0
i
0
1*J
E
Ar
25.
A
W
X
1
1, 01
B
(U )
1* 0 0 0
I
R
I
1 I
GO
597
D
Y Z A A1 1 00
' C I O OO D-
0O O
EXERCISES 5.3 (page 294) 1.
0
l*
O
I
D,1
I* O [01 -1 I I
O 0 O
I O
D,1
2
1
DV
#V/ #,/
3. 3A, 2A, 2D 5. ID,2A,3C,4B 7. 1D,2E,3B,4A 9. ID, 2A, 3B, 5C 11. IB,3A,4D 13. 1, B), (2, Cl, (3, Al 15. (1,A},(2,D),(3,El,(4,BI,{5,Cl 17. B, C, A,D 19. W, Z, Y, X 21. carrot, banana, egg, apple 23. Constantine to 1, Egmont to 2, Fungo to 3, Drury to 4, Arabella to 5
EXERCISES 5.4 (page 303) 1. row 2, columns 2 and 4 3. row 3, columns 1, 3, 4 11. {1,3,5,6) 13. 7hours 15. (1,4,5,6,7,8,B}
5. {2, A, C]
7. {B, C, D, El
9. impossible
598
Answers to Odd-Numbered Exercises
EXERCISES 5.5 (page 311) 1. 13 3. 13 5. 18 7. 11 New York, Herriman to Los Angeles
9. 16 11. 28 13. Addams to Chicago, Hart to Las Vegas, Young to 15. The Hungarian algorithm must be applied to a square matrix.
SUPPLEMENTARY EXERCISES (page 313) 1. (a) 60 (b) 36 (c)0
3. (a) no
(b) yes; Vl= {1,2, 5,6, 7, 8,11, 12}, V2= {3,4,9,10, 13, 14, 15, 16} 5. (a){2,4,6,7,9,11) (b){1,2,5,6,7,8,11,12} 7. '1,21,{3,4),1{6,71,{8,5 -1* 0 0 0 10 0 1 1* 0 9. 0 0 1* 0 0 ; the 1st and 4th rows and 3rd and 4th columns 1
1* 0
1
1
-0 0 1 0 011. w,z,v,x,y 13. hours 15. One way is: Adam, Studebakers; Beth, Hupmobiles; Caj, Packards; Danielle, Hudsons.
CHAPTER 6 EXERCISES 6.1 (page 326) 1. a network with source A and sink E 3. not a network because arc (C, B) has negative capacity 5. a network with source D and sink B 7. not a flow because 5 comes into D and 6 comes out of D 9. a flow with value 3 11. not a flow because 2 comes into D and 3 comes out of D 13. not a cut because vertex C is not in S or T 15. a cut with capacity 40 17. a cut with capacity 34 19. C 21. B 7 D
0
A
0>
Da
23.
B
0
25. {A,B,C,D IEI
D
44 A
F
C
6
E
2
D
F
6
27. {A,B,C1,{D,E,F)
Answers to Odd-NumberedExercises
31.
29. {A, C}, {B, D, E, F)
Chicago 30 St. Louis
Little Rock
33. f (U, V) = 15 and f (V, U) = 3
40 20
20
599
15
Indianapolis 25 15 Lexington emphis
5
Atlanta 35. For the flow in Exercise 10, take U = {D), V1 = {A, B, C}, and V2 = (B, E, Fl. EXERCISES 6.2 (page 338) 1. 1 3. 2 5.
7.
C
B
6
D
5 2
A
A,
3 C
F
4 3
E
D
13. Increase the flow by 2 along 11. The given flow is maximal. 9. Increase the flow by 3 along A, C, E. A, D, B, E, F. 15. Increase the flow by 2 along A, B, D, C, F, E, G. 17. The given flow is maximal. ty.
B
4
.1.
E
4 5 AI
23.
4 C
1
F
B
15
D
70
25. 65
A
F
4 C
45 0
E
A
G
F
600
Answers to Odd-Numbered Exercises
27.
B
3
29.
E
B
3-
9
1,0.5
A
G
C
E
F
4
EXERCISES 6.3 (page 345) 1. 21 3. 28 5. (A, B, C, E}, {D, Fl 11. (A,C,F},{B,D,E,G) 13.
7. {A,BD},{C,E,F} C2
9. {A, B, C, D}, {E}
17. 2` 2
A
I)
19. ((S, A), (F, T)}
23.
A
6
4
D
2
7
3 B
EXERCISES 6.4 (page 352) 1. bipartite; VI = {A, D, El and V2 A
B
E
F
=
3
(B, C, F}
5. bipartite; VI = IA, D} and V2 = {B, C, E, F} B
E
3. not :ipartite
7. {(A, Y), (B, Z), (D, X)}
Answers to Odd-Numbered Exercises
601
9. The given matching is a maximum matching. 11. {(A, 1), (C, 3), (D, 2)}
13.
{(a, A), (b, C), (c, B), (d, D)}
15. Andrew and Greta, Bob and Hannah, Dan and Flo, Ed and Iris 17. Craig files, Dianne distributes paychecks, Gale collates, Marilyn types, and Sharon helps students. 19. Create a bipartite graph g with vertices U, that correspond to the sets SI, S2 , . . , S, and vertices Vj that correspond to the elements in SI U S2 U .. U S,. Join U. and Vj if and only if the element corresponding to Vj belongs to set Si. Apply the flow augmentation algorithm to the network K associated with S. Then Si, S2 . 5, has a system of distinct representatives if and only if the value of a maximal flow in AI is n. 21. No system of distinct representatives exists. 23. No acceptable assignment exists.
SUPPLEMENTARY EXERCISES (page 356) 1. A minimal cut is {A), (B, C, D, E, F). A maximal flow is shown below. B
2
E
76 A
F
C
3. A minimal cut is {A, B, C}, {D, E, F, G}. A maximal flow is shown below. 8
4 6*%
A/6
3
7
-~6
G
C 2 F 5. A minimal cut is {A, B, C, D, F}, {E, G, H}. A maximal flow is shown below. B 62A
5
F
H
6
D
3
o
7. A minimal cut is {A, D}, (B, C, E, F, G, H, I, J, K}. A maximal flow is shown below. H 3 9
E 3
H 12
C
K
A 9
57 D5
G9
i
602 II
II.
Answers to Odd-Numbered Exercises A
3
D
4
F
I-
1 .
B5
C
I. I7.
17 I1.
A'
*G'
F
b
25.
-
EK
11 1 0 0 1 11 1 00 1
CHAPTER 7 EXERCISES 7.1 (page 363) 1. 10 3. 56 5. 6 7. -220 9. 3360 11. --262,440 13. 1, 6,15, 20,15,6,1 15. x6 + 6x 5 y + 15x 4 y2 + 20x 3 y3 + 15x 2 y4 + 6xy 5 + y 6 17. 81x4 - 108x 3 y + 54x 2 y 2 -12xy 3 + y 4 19. 35 21. 252 23. 15
EXERCISES 7.2 1. 13 3. 5 17. (a) 720 (b) 27. (a) 720 (b)
(page 371) 5. 14 7. 45 9. 32 11. 2' 13. 168 15. 160 144 (c) 36 (d) 48 19. 3219 21. 70 23. 48 25. 36,504 360 (c) 240 (d) 576 29. 112 31. (a) 1296 (b) 360 (c) 60 (d) 240
EXERCISES 7.3 (page 378) 1. 20 3. 10 5. 12 7. 15,120 9. 5040 11. n 13. 24 21. 120 23. 84 25. 200 27. 10,584 29. (a) 495 (b) 5 (c) 72
EXERCISES 7.4 (page 385) 1. 1260 3. 210 5. 45 19. 3,864,861 21. 4200
7. 1820 23. 462
15. 360 (d) 54
17. 286
19. 210
9. 63,063,000 11. 35 13. 165 15. 10 17. 140 25. 1050 27. 165 29. 6062 31. 220 33. 287,134,346
603
Answers to Odd-Numbered Exercises
EXERCISES 7.5 (page 391) 5 1 1
6 17.31.
18
32 1
1
19.
120 352
33.
-
833
1
63
1
210
125
105
59,049 969
35.
-
4165
4
33
10,010
21.
4 88
5
16 23.-
2
175
429
25.
-
3
27.
28
140
2187
29.
-
5
12
2530
EXERCISES 7.6 (page 401) 1. 40 15. 61
3. 160
5. 55
17. 231
7. 27
19. 13,824
9. 64 21. 685,464
11. 7
13. k 4 -4k 3 +6k
23. 35
27.
2
-3k
Dk=k!
-
!+
+ (- )-
]
29. S(n,0)=0,S(n,1)=1,S(n,2)=2 -'-1,S(n,n-2)=C(n,3)+3C(n,4),S(n,n-1)=C(n,2),and S(n, n) = 1 31. S(n, 1)+ S(n, 2) + * + S(n, n) EXERCISES 7.7 (page 410) 1. pq 7. (2,1,4,3,6,5) 9. (2,1,5,3,4,6) 11. (5,6,4,1,2,3) 13. none 15. (5,3,1,2,4,6) 17. (6,4,1,2,3,5) 19. (1,2,3,4); (1,2,4,3); (1,3,2,4); (1,3,4,2); (1,4,2,3); (1,4,3,2); (2, 1,3,4);
(2, 1, 4, 3); (2, 3, 1, 4); (2, 3, 4, 1); (2, 4, 1, 3); (2, 4, 3, 1); (3, 1, 2, 4); (3, 1, 4, 2); (3, 2, 1, 4); (3,2,4, 1); (3,4, 1,2); (3,4, 2, 1); (4, 1, 2, 3); (4, 1, 3, 2); (4, 2, 1, 3); (4, 2, 3, 1); 25. (3,4,5,7,91 27. (3,6,7,8,9) (4,3,1,2);(4,3,2,1) 21. 11,3,5,8,91 23. (2,3,6,7,8) 31. none
29. {4,6,7, 8, 9}
SUPPLEMENTARY EXERCISES (page 412) 1. 36 3. 3024 5. x 6 - 6x5 + 15x 4 - 20x 3 + 15x 2 - 6x + 1
810xy4 + 243y 5 15. 21
9. (8, 2,4, 1, 3, 5, 6, 7) 19. 240,240
17. 960 1 7
31. (a) 2520
(b)
37.
39. 6188
10,005
191,216
47. 630,630
-
49. no
10 21
(c) -
21.
51.
29
-
33. (a) 657,720
41. .3439
105,293
3,81 1,606~
7. 32x5 + 240x 4 y + 720x 3 y2 + 1080x 2 y3 +
11. 24310, 19448, 12376, 6188, 2380, 680, 136, 17,1 23. 27,720 (b) 142,506
43. 514,594,080
25. 43,243,200
27. 1820
13. 18,564 29. 14
35. 63 45. 63,063,000
55. C(n + 1, 3)
CHAPTER 8 EXERCISES 8.1 (page 427) 1. 126 3. 331 5. -63 7. 56 9. 31 11. 3 13. t, = 1.0525t,-] for n > 1, to = 28,000 15. rn = rn- + 6 for n > 1, ro = 24 17. bn= 1.015bn - 25 for n > 1, bo = 280 19. w, = 0.15w, I + 2.0, wo = 1.7 21. Sn = 2s,-, + 3s,-2 + S,-3 for n > 4, s1 = 2, s 2 = 7, s 3 = 21 23. a, = nan-,Iforn > 2,a, = 1 25. s, = s,- +(n-I)forn >l,so = 0 27. Sn =S,-l + S-2 + Sn-5 for n > 6, s, = 1,s 2 = 2, S3 = 3, S4 = 5, s 5 = 9; There are 128 sequences for a drink costing S0q. 29. c,= (2n-I)c,-, for n > 2, cl = 1 31. r = r,-, + 2(n-1) for n > 2, r 1= 2
604
Answers to Odd-Numbered Exercises
33- Sn = Sn-l + Sn-2 + Sn-3 for n > 4, s= 2, S2 = 4, 53 =- 7 35. C, = Coen-I + clcC-2 + + n > 2, co = 1,cl = 1 3 7 . Sn = 2Sn-I Sn-2 + Sn-3 folt'> 4 , sl = 2 , S2 = 4 , S3 = 7 ; S6 = 37
EXERCISES 8.2 (page 439) 2 9. -n(2n + 1)(n + 1) = C(2n + 2, 3) 3
19. Sn = 2(n 2
17. sn = 1.75(5)n -0.75 23.
Sn =
n! 2 + + -
27. s =2n 2 + 2n
11.
+ -I)
+-
13. sn = 5(3)n 21. sn = a
15.
Sn =
6(-I)n
+
25. (a) Sn := 0.95Sn- , so = 1000 r.2 + n +2 31. rn-2
(b)
Sn
1000(0.95)'
=
3 -7(- I)n
Sn
+ 3(2)n
__
Sn=
1 j
k(
2
(C) 599
33. mn=2n+ -2
7. Sn = 4 - 3' 9. Sn = 1015. Sn = (6 - n)4n 17. Sn = 2(3)7)5n 23. Sn = 9(-I )n - 6(-4)n 0 (b) dg = 104.0284 8 mg (c) 125 mg 31. Sn = --i--lY-2 for n >3 =I 1,S2 = 2
5. Sn 11. Sn = 10(-2)n - 3 13. s, = 6(-I)n 19. Sn = (3n - 4)(-2)' 21. Sn = (4n 25. (a) dn = 0.80d,- + 25 for n > 1, do =
29. $1190.30
9 + 4rz
Sn + 5)
29. Sn=2n- (n+2)
EXERCISES 8.3 (page 451) 1. sn = 2 + 3n 3. Sn = 5(4')
27. About $3670.36
-
Sn =
c,-ICo for
)
5 - f'I
- /
10
2
+
(-3)n
)
33. Cn= 3 + 3(-1)n
EXERCISES 8.4 (page 464) 1. 243 3. 4 5. 0 7. 16 9. L1 I I D e m am is am= t!
I
100
51
100
76
100
76
87
L
= 50
50
No, less
= 75 -151
75
No, less
88
No, greater
81
No, less
[76L88 12 61 = 81 =
] 1.
L 1,
I
e
m
am
Is am = t?
450
No, greater
225
No, less
225 J= 112
336
No, less
262 ]= 131
393
No, less
300
I30-I= L2 J
149
L2j=75
76
149
113
149
150
I 21 82
87
82
83
83
83
L
= 84
84
No, greater
132
149
[281J =140
420
No, greater
= 82
82
No, less
132
139
L271=135
405
No, greater
83
83
Yes
132
134
266] = 133
399
No, less
134
134
134
402
No, greater
134
133
Since b
> e,
the target t is not in the list.
Answers to Odd-Numbered Exercises 13. (19)
(56)
(87) (42)
V
15- (13)
V
(56) (45)
(13, 89)
(19,42, 56, 87)
(62)
V
V
(42, 87)
(19,56)
(89)
(75)
(68)
V
(45, 56)
I
(62, 75)
(13, 45, 56, 89)
(68)
(62, 68, 75)
(13, 45, 56, 62, 68, 75, 89) 17. (95)
(87)
(15)
(87, 95)
(42)
(56)
(15, 42)
(54)
(16)
(54, 56)
(15, 42, 87, 95)
(23)
(73)
(16, 23)
(39)
19. n > 4
(39, 73)
(16, 23, 54, 56)
(39, 73)
(15, 16, 23, 42, 54, 56, 87, 95)
(39, 73)
(15, 16, 23, 39 42, 54, 56, 73 87, 95)
25. e,=e,-,+3forn>2,el=1 27. e,=e,-i+4forn>1,eo=1 29. en = 3n-2forn > 1 31. e, =4n+1forn>O
EXERCISES 8.5 (page 472) 1. 2+3x+x2 +4x 4 +x 5 3. 1+3x+3x 2 +2x 3 +4x 4 +5x 5 +xX +x 7 5. 2+3x+X2 +X 3 +5X4 +2X 5 +x 6 +x 7 +.. 7. 1+2x+3x2 +3x 3 +3x 4 +3x 57 +3x 6 +3x 7 +_* 9. 1-X2 +x3 +X4-X5 +x6+x 11. 1 +x +x 2 +2x 3 +2x 4 +2x 5 +3x 6 +3x 7 +.. 4 5 13. (1+x+x 22 +x 33 )(1+x+x22 +x 33 ±x +x ) 1+2x+3x 2 +4x 3 +4x 4 +4x 5 +3x 6±+ 15. (1+X ±x )(1+x+x +X +x 4 )(1+x+X2)=I+3x+6x 2 +9x3 +I1x 4++x +9x 17. (I + X + X2 + X3 + x 4 )(1 + X + X2 +x 3 )(1 + X2 )(1 + X3 ) = 1+2x+4x 2 +7x 3 +9x 4 + 1x 5 + 12x 6 +_ 19.
(I
+x+x
2
+x
3
)(1 +x+x
2
+..)=
2
1 +2x+3X
+4X
3
+4X
4
+4X
5
+4X
6
21. (X4 + X5 + -. .)(x2 + X3 + . ._.) = X6 + 23. (I +X) 7 (1 +X +X 2 +X 3 +x 4 +X 5 ) 25. (I+x+x2 + . )(I+x3 +x 6+ . )(1+x 4 +x 8 +.) 27. a,=r+l = 27if r >1 O 31. ao4 = I andar = Oifr > O8 29. ao2 = I anda, 3 5 5 6 7 10 33. (x +X + X + X + X' + . )2 = X + 2x +x + 2x + 2x + 2x9 + 3x + 4 9 35. (1+X+X +X +..)4 = 1+4x+6x2 +4X 3 +5X 4 +12x 5 + 12X 6 +4X 7 +6x 8 + 16x 9+ 18x 10 +**
EXERCISES 8.6 (page 481) 1. 1+3x+9x 2 +* 3. 1-2x 7. XI+ +X X X22 + 7. 1+(x+x 2 )+(x+x2 ) 2 + 11. S=2xS+1+x+x2 +-'* S = (I-2x)-'(1-x)-'
5. 1-x
2
+x
4
-x
6
+.-
9.1 _3 X+9 2 _ 27 3 9. 2-----x+x3+ 2 2 2 2 13. S=l+x+2x(S-I)-x2 s S = (1-x)(1-2x +x 2 )-l
+
6
+-
605
606
Answers to Odd-Numbered Exercises
15.
S =-l-x(S+ 1)+2x2 S S =-(1 +x)(1-x)- (1+2x)-1
17.
S = -2+x+x(S+2)+3x 2 S+2x2 (1 +x+x 2 S = (-x)- '(-x-3x )-l (-2+ 5x-X 2 )
19.
2 S = 2-x + X + x(S -2
21.
a =
23. a
=--,
+.)
+ x)-3x 2 (S -2) + x 3 S + 3X2 - x 3)
2
+ 8X )(1-x
S = (2-3x
2
,b=-
1
b =
4
1
25. a
29. Sn =-2n + 4( -5)n
1
b,
27. sn :=
2n + 1
31. s, = 2(3/n2) if n is even and sn = 0 if n is odd
SUPPLEMENTARY EXERCISES (page 484) 1. 829
3. 3840
5. 33
7. s, = 1.04s,-, + 500 for n > 2 and s, = 16,000 9 . cn = c-l+ Cn-2 for n > 3 , c, = 2 , and C2 = 3 11. en = en-1 - k/n 2 , where k is a constant 17. Sn =6-3n for n > 0 19. sn = (4-n)2n for n > C1 21. 3, 4, 7, 11, 18, 29, 47, 76, 123, 199 27. Sn = 2(4n) F (-3)n 29. Sn= 2n + (3-Sn)(-I)n for n > 0 31.
(a)a=-1,b=-2
33.-
35.
(b)Sn =3' ->5(-2)nn-2
b
e
m
1
4
[+4)]
3
4
Le3+
2
41. Snl+
37. 7
am
Is am =
2
4
No; less
]= 3
6
Yes
=
4
39. 1,2,.
6
- 7(2n) for n > 0
rn>0
?
,mandm+1,m+2,.rm+n
2xS;S=(I-2x)-'
45.
43. S+=I+X_25
Sn =5(-2)n 47. Sn = 2n + 2(3n) 51. (X2 + X3 + X4 )6 53. (1- X 5 ) (I
2x(S -
49. (1+x +x+x 2
- X1' )-l (
-
x2 5)yI
3
I
);S
EXERCISES 9.1 (page 496) 9. x y
x
3. ((x' v y) A x)Y
5. (X'V y') A x'
5
)(1 + X + X )(1 + X + X2 + X3 + X4 + X )
55. (I- _x)-'(l - x 5 )-'(1 -x1)-
CHAPTER 9
1. (XAy)vx
3x
2
7. (x' A (y' A X))
Answers to Odd-Numbered Exercises
IL.
Ii3.
17. 1
15. 0 19.
-21. x y
Output
0 0 0 1 10 1 1
1 1 0 1
x
y
z
Output
1
0 0 1 1 0 0 I 1
0 1 0 1 0 1 0 1
0 0 0 0 0 0 1 0
x
y
z
Output
0 0 0 0
0 0 I 1 0 0 1 1
0 1 0 1 0 1 0 1
1
0 0 0 0 1 I 1
25.
607
x
Y
0 0 0 1 1 0 1 1
Output 0 1 1 1
27
11 1 1
23.-
x
Y
0 0 0 1 10 1 1
Output 0 0 0 1
1 0 0 1 1 1
1
29. equivalent 31. equivalent 33. equivalent 41. not equivalent 43. window signal
35. not equivalent
door open safety thrown
EXERCISES 9.2 (page 504) 9. (c), (f), (g) 11. (c), (f), (g) 13. (j), (i), (e) 15. (j), (b) 17. (c), (a), (b), (b), (b), (b), (e), (e), (b), (b), (a), (c) 19. When x = y = 0, z = 1, the first is 0 and the second 1. 21. When x = z = O, y = 1, the first is 0 and the second 1.
37. equivalent 39. not equivalent 47. 1, undefined
608
23.
Answers to Odd-Numbered Exercises 25. (x',AyAz)V(xAy'Az)V(xAyAZ')
(x' Ay)V(xAy') x
x
27.
(X'AyAz)V(xAy'Az)V(xAyAz)
29. 12
31. 9
33. 14
35. x y
x
y
z x
y
z x y
z
37. (aAbAc')v((bAc)Ad')v(aAd) EXERCISES 9.3 (page 518) (X'AY')V(X'Ay)V(XAy) 3. (x'Ay'Az')V(x'Ay'Az)V(x'AyAz')V(x'AyAz) 5. (W'AX'Ay'AZ')V(W'AX'AyAz')V(W'AXAy'AZ')V(W'AXAyAZ')V(WAXAy'AZ) 1.
7. xvy' 13.
r
9.
yVx'V(y'Az')
15.
y
11.
y --
_
xx X
(XAZ)V(W'AXIV(WAX'Ay')V(W'AyAZ')
17.
y
X IX
o1w
z 19. x' v y
21. x' x
y
23. (W'AZ')V(wAXAy'Az)
Answers to Odd-Numbered Exercises 25. (x'Az)V(y'Az)
27. (x'Az)V(xAy'Az')
21
tC -1.
29. xV(y'Az') 12) 1.
EXERCISES 9.4 (page S27) 1. 0,1
1
3.
1
5.
0
0
7. x y
A
B
C
B C
C C
C A
Initial state B Accepting state A 11. y 19.
13. A
15. yes
9'a b c
21.
Ox
2
3
2 3 1
3 1 2
1 2 3
Accepting state 2 17. no
I/Z
2
1
0,21x,y
1,2/x,w
/x
O/y
23. 1 2 3
1
2
1
2
2 2 1
2 1 2
a c c
a b a
25. I
2 3
A
B
C
A
B
C
B C C
B C C
C A C
x z y
y x z
y z z
27. ywywwxx 29. yzzyyz 31. All strings containing a 1 33. All strings containing exactly n Is, where n - I (mod 3)
31. (wAx)V(y'Az')
609
610
Answers to Odd-Numbered Exercises
37.
35.
0/()
1/0
0/1
1/2
0,2
SUPPLEMENTARY EXERCISES (page 531) 1.
(X'Vy)A(ZAX)'
x
y
z
0 0 0 0 0 1 0 1 0 0 1 1 1 00 1 0 1 1 1 0 1 1 1
5. (a)
3. (a) yes
(x'vy)A(ZAX)'-
1 1 1 1 0 0 1 0
[(xvy)A(x'vy)]vy'=[(yvx)A(yvx')]vy'
= x'A
(a'
= [y v (x Ax')] v y'
(c,
=(yvO)vy'
(f)
= y V y'
(gl
= I
(f)
(j)
(b) x' A (y Az')'= x' A (y' v z')
(b) no
7. (x'AyAA')V(x'AyAz)V(xAy'Az')V(xAyAz)
(i)
(y'Vz)
x =
(x'
A y') V (x'
A z)
=(xvy)'V(x'Az)
(c)
(j)
y z x y z
x y
z x y
z 16 gates 9. (a) x'
(b) (x A z') V (y' A z')
(c) (x'A y A z') v (y'A z) v (x A y') v (w'Az')
Answers to Odd-Numbered Exercises
11. (x'Ay)V(yAZ)V(XAy'AZ')
611
13. , z
x y y
zY
z x
z 9 gates 15. C
17. y
19. -1, 1, 1, 1, 0, 1, 1
21. b, b, foam, b, a, foam, b
APPENDIX A EXERCISES A.1 (page 543)
1. false statement 3. true statement 5. not a statement 7. false statement 9. not a statement 11. true statement 13. 4 + 5 $ 9 15. California is the largest state in the United States. 17. Some birds cannot fly. 19. No man weighs 400 pounds. 21. No students do not pass calculus. (All students pass calculus.) 23. Someone does not enjoy cherry pie. 25. (a) One is an even integer and nine is a positive integer. (false) (b) One is an even integer or nine is a positive integer. (true) 27. (a) The Atlantic is an ocean and the Nile is a river. (true) (b) The Atlantic is an ocean or the Nile is a river. (true) 29. (a) Birds have four legs and rabbits have wings. (false) (b) Birds have four legs or rabbits have wings. (false) 31. (a) Flutes are wind instruments and timpani are string instruments. (false) (b) Flutes are wind instruments or timpani are string instruments. (true) 33. (a) If I go to the movies, then this is Friday. (b) If this isn't Friday, then I won't go to the movies. (c) If I don't go to the movies, then this isn't Friday. 35. (a) If Kennedy runs for President, then he won't run for the Senate. (b) If Kennedy runs for the Senate, then he won't run for President. (c) If Kennedy doesn't run for President, then he is running for the Senate.
EXERCISES A.2 (page 547) Note: only the last columns of truth tables are given. 3.
1. p
q
(pvq)A[ -'(pAq)]
T
T
F
T
F
T
F
T
T
F
F
F
p
q
(p V q)-
(-p
T
T
F
T F F
F T F
F T T
A q)
612
5.
9.
Answers to Odd-Numbered Exercises
p
q
7.
r I (p-q)-(pvr)
T T T T F F F F
T T F F T T F F
T F T F T F T F
T T T T T F T F
p
q
r I [(pvq)Ar]- [(pAr)vq]
T T T T F F F F
T T F F T T F F
T F T F T F T F
T T T T T T T T
p
q
r
T T T T F F F F
T T F F T T F F
T F T F T F T F
17.
(Q-qAr)(+*Q-pVq)
F F F T F F T F
p
(p)-p
T
F
T
F
T
F
19. If the truth table is arranged as in Exercise I above, other, the column corresponding to the given statement is: F, T, F, F. 21. If the truth table is arranged as in Exercise 5 at,ove, then the column corresponding to the given statements is: T, F, T, T, T, T, T, T. 23. If the truth table is arranged as in Exercise 5 above, then the column corresponding to the given statements is: T, F, T, F, T, F, T, T. 25.
p
q
T T F F
T F T F
pAq
qAp
T F F F
T F F F
pVq
T T T F
qVp
T T T F
27. (e) If the truth table is arranged as in Exercise 5 above, then the column corresponding to both statements is: T, T, T, T, T, F, F, F. (f) If the truth table is arranged as in Exercise 5 above, then the column corresponding to both statements is: T, T, T, F, F, F, F, F. 29. If the truth table is arranged as in Exercise 1 above, then the column corresponding to both statements is T, F, T, T.
33. (a) p
q
pVq
T T F F
T F T F
F T T F
(b) If the truth table is arranged as in part (a), then the column corresponding to both statements is F, T, T, F.
Answers to Odd-Numbered Exercises
EXERCISES A.3 (page 553) 1. p q -q p A(-q)
-[p A(-q)]
613
p -q
T
T
F
F
T
T
T F F
F T F
T F T
T F F
F T T
F T T
13. The statement is false. For example, 3 + 5 = 8.
15. The statement is false. For example, if a
= 3, b = 2, and
c = 0, then ac = bc, but a = b.
17. The statement is false. For example, if x = 4 and y = 9, then 6 divides xy, but 6 does not divide either x or y. 19. The statement is true. 21. The statement is true. 23. The statement is false. For example, if n = 41, then n' + n + 41 = 412 + 41 + 41 = 41(43).
SUPPLEMENTARY EXERCISES (page 556) 1. true statement 3. not a statement 5. false statement 7. true statement 9. There exists a square which is a triangle. (false) 11. No scientist from the U.S. has received a Nobel prize. (false) 13. 2 + 2 < 4 and 1 is not a root of x5 + I = 0. (true) 15. In circling the globe along a line of latitude, one must not cross the equator exactly twice or not cross the North Pole or not cross the South Pole. (true) 17. (a) Squares have four sides and triangles have three sides. (true) (b) Squares have four sides or triangles have three sides. (true) 19. (a) If 3 > 2, then 3 x 0 > 2 x 0, and if 4 = 5, then 5 = 9. (false) (b) If 3 > 2, then 3 x 0 > 2 x 0, or if 4 = 5, then 5 = 9. (true) 21. (a) If 32 = 6, then 3 + 3 = 6. (true) (b) If 3 + 3 # 6, then 32 #A 6. (true) (c) If 32 #A6, then 3 + 3 #A6. (false) 23. (a) If 3 x 2 = 6, then 32 = 6. (false) (b) If 32 54 6, then 3 x 2 # 6. (false) (c) If 3 x 2 :# 6, then 32 7#6. (true) 25. p q -[(p V q) A -p] A -p
27.
T
T
F
T F F
F T F
F F T
p
q
r
T T T T
T T F F
T F T F
T T T T
F F F F
T T F F
T F T F
T T T T
[pA(rA(-pVq))]-
[(pAr)A(-pVq)]
29. yes 31. no 33. yes 35. yes 37. 6 cannot 41. The statement is true. 43. The statement is true.
39. The statement is true.
614
Answers to Odd-Numbered Exercises
APPENDIX B EXERCISES B.1 (page 562) ]
3.
1. not defined
1.
19.
-3 3 3
3 3 3
33 3
21.
14
-5
-3
3
3 3
3 3
5
33 3
[
71
23.
]
7. not defined
3
1
-9 9 -9
9 9 9
1 -1 9 9 9
9. [_3
1
2]
Index
Abel, H., 106 Absolute value, 55, 77 Accepted string, 523 Accepting state, 523 Addition principle, 369-371 Adjacency list, 112, 161, 207 Adjacency matrix, 111-112, 143-145, 160 Adjacent cells, 510 Adjacent vertices, 109 Adleman, L., 103 Algorithm, 2, 23-33, 566-571 binary search, 457 binary search tree construction, 257-258 binary search tree search, 259 Bose-Nelson, 465 breadth-first search, 138, 196-198,331 bubble sort, 31 comparison of, 23-25 complexity of, 23-25 depth-first search, 214 Dijkstra's, 140-141, 148 efficiency of, 23-25 Euler circuit, 124-125 flow augmentation, 330 for evaluating n factorial, 33 for evaluating x', 25 for Fibonacci numbers, 34 for P(n, r), 38 greedy, 203 Homer's, 27 Huffman's optimal binary tree, 249-250, 263 Hungarian, 308 independent set, 290 inorder traversal, 242 Kruskal's, 210 lexicographic ordering of permutations, 407 lexicographic ordering of r -combinations, 409
merge sort, 461 merging, 460 next subset, 29 of order at most f (n), 27 polynomial evaluation, 26 postorder traversal, 240 preorder traversal, 236 Prim's, 201, 205 Prufer's, 193 revised polynomial evaluation, 35 sequential search, 454 sum of arithmetic progression, 34 sum of geometric progression, 34 topological sorting, 70 Al-Khwarizmi, A., 36 Ancestor, 226 And connective, 536 AND-gate, 490 Antisymmetric relation, 63-66 Appel, K., 154, 176 Arc in a network, 318 capacity of, 318 flow along, 319 Archimedes, 483 Aristotle, 554 Arithmetic progression, 96 common difference of, 96 sum of terms, 96 Arrangements, 374, 379-385 Articulation point, 223, 269 ASCII code, 246 Augmenting path for flow, 321, 328-331 Auxiliary equation, 448 Babbage, C., 36 Back edge, 215 Backtracking, 217 Balanced binary tree, 269 Bernoulli, J., 411 Bhaskara, 411 Biconditional statement, 536-541 Binary digit, see Bit, 367
Binary search algorithm, 457 Binary search tree, 255-261 Binary search tree construction algorithm, 257-258 Binary search tree search algorithm, 259 Binary tree, 232-244 balanced, 269 expression tree, 232-235 for weights, 248 height of, 269 left child in, 232 left subtree in, 233 optimal, 249 right child in, 232 right subtree in, 233 search tree, 255-261 traversal of, 235-244 visiting a vertex of, 235 weight of, 248 Binomial coefficient, 363 Binomial theorem, 363 Bipartite graph, 135, 278, 347 complete, 135 matching of, 347 matrix of, 280 maximum matching of, 348 network associated with, 348 Birkhoff, G. D., 266 Bit, 367 Boole, G., 106, 530 Boolean algebra, 499, 506 Boolean expression, 494 equivalent, 495 identities for, 499 Boolean variable, 490 Bose, R. C., 465 Bottleneck problem, 302-303 Branching in algorithms, 568-571 Breadth-first search algorithm, 138, 196, 331 Bridge, 163, 216, 229
615
616
Index
Bubble sort algorithm, 31, 455 Byron, A., 36 Capacity of a cut, 323, 341-345 Capacity of an arc, 318 Cardano, G., 411, 564 Cartesian product, 45-46 Catalan, E., 425 Catalan numbers, 246, 425 Cauchy, A. L., 176 Cayley, A., 176, 184-185, 190, 265, 564 Child, 226 left, 232 right, 232 Chromatic number, 150 Circuit, combinatorial, 493 Clifford, W. K., 266 Codewords, 246 Codomain of a function, 76 Collatz sequence, 569 Coloring of a graph, 150 Column of a matrix, 558 Combination, 102, 360-363 r-combination, 375 unordered lists, 374-378 with repetition, 379-385 Combinatorial circuit, 493 Common difference, 96 Common ratio, 96 Complement of a graph, 176 Complement of a set, 42 Complete bipartite graph, 135 Complete graph, 110, 418, 431 Complexity, 24-26 non-polynomial, 30 polynomial, 26-27 Component of a graph, 135 Composition of functions, 82 Conclusion of a conditional statement, 539 of a theorem, 549 Conditional statement, 539-543 conclusion of, 539 premise of, 539 Congruence class modulo m, 57 Congruence modulo m, 55-59 Conjunction, 538 Connected graph, 121, 186, 198 articulation point in, 223 Connectives, 536-541 and, 536 exclusive or, 548 if and only if, 536-541 if .. then . . , 536-539
not, 536-537 or, 536 Contradiction, 545 proof by, 551 Contrapositive of a statement, 541 Converse of a statement, 541
Counterexample, 553 Counting problem, 1, 12 Covering in a matrix, 282 of a graph, 281 Critical path, 6 Cryptography, 103 Cut, 323 capacity of a, 323, 341-345 in a network, 323 minimal, 341-345 Cycle, 122 directed, 162 directed Hamilioniaan, 166 Hamiltonian, 126-130 Decision tree, 228 Dedekind, J. W. R. 55:5 Deficiency of a giapt, 356 Degree of a vertex, 11, 120 Deletion of a vertex, 204-265 De Moivre, A., 41 1, 484 De Morgan, A., 36. 106, 107, 530, 555 De Morgan's laws, 44, 500, 547 Depth-first search, 211 -220, 228 algorithm, 214 back edge, 215 backtracking, 217 numbering, 215 tree, 215 tree edge, 215 Derangement, 400, 422 -424, 437 Descendant, 226 deWitt, J., 564 Diagram Hasse, 68 transition, 522 Venn, 42 Dictionary order, see Lexicographic order, 405 Difference equation, 411-451 first-order linear. 44 L,444 linear inhomogeneous, 486 second-order hormog eneous linear, 444-451 Difference of sets, 41-42 Dijkstra, E. W., 1(40 Dijkstra's algorithm, 110-141, 148 Direct proof, 549 Directed cycle, 162, 226 Hamiltonian c) cle, J66 Directed edge, 159 from U to V, 159 Directed Euler circuit, 164 Directed Euler path, 164 Directed graph, 158- 168 adjacency list for, 161 adjacency matrix lor, 160 directed cycle in, l 62, 226 directed edge ill, 15')
directed Euler circuit in, 164 directed Euler path in, 164 directed Hamiltonian cycle in, 166 directed Hamiltonian path in, 166 directed path in, 162 indegree, 160 labeled, 160 multigraph, 161-164 oumdegree, 160 simple directed path in, 162, 226 strongly connected, 162, 215-216 tournament, 167 vertices in, 159 Directed Hamiltonian cycle, 166 Directed Hamiltonian path, 166 Directed loop, 161 Directed multigraph, 161 -164 directed cycle, 162 directed Euler circuit, 164 directed Euler path, 164 directed Hamiltonian cycle, 166 directed Hamiltonian path, 166 directed loop, 161 directed parallel edges, 161 directed path, 162 length of a directed path in, 162 of a relation, 168 simple directed path, 162, 226 strongly connected, 162, 215-216 Directed parallel edges, 161 Directed path, 162 directed Euler, 164 directed Hamiltonian, 166 directed simple, 162 length, 162 Dirichlet, P. G. L., 106 Discrete dynamical system, 427 Disjoint sets, 41, 369, 371 Disjunction, 538 Disjunctive syllogism, 548 Distance between vertices, 136, 140 breadth-first search algorithm for, 138 Dijkstra's algorithm for, 140 Distinct representatives, 272 Distinct trees, 193 Distributions, 383 Divide-and-conquer algorithms, 455-462 Divides, 55, 64 Division algorithm, 55 Domain of a function, 76 Dynamical system, 427 Edge, 109, 119 back, 215 directed, 159 directed parallel, 161 in a tree, 215 incident with a vertex, 109 joining vertices, 109 loop, 119
Index
parallel, 119 weighted, 139 Edmonds, J., 328, 344 Efficiency of algorithms, 23-25 Elias, P., 344 Empty set, 21 Entry of a matrix, 111, 558 Equality of functions, 81 of matrices, 558 of sets, 21 Equivalence of Boolean expressions, 495 of circuits, 495 of finite state machines, 530 Equivalence class, 51-53 Equivalence of logical statements, 546 Equivalence relation, 50-53 and partitions, 52-53 Euclidean plane, 45 Euler, L., 106, 122, 133, 175, 484 Euler circuit, 122-126 directed, 164 Euler circuit algorithm, 124-125 Euler path, 122-126 directed, 164 Euler's formula, 177 Event, 387 Exclusive or, 548 Existence problem, 1, 4 Experiment, 387 Exponential function, 84 Expression tree, 232-235 Factorial, 13 Federal Express, 61 Feinstein, A., 344 Fermat, P., 387 Fibonacci, L., 35, 444, 484 Fibonacci numbers, 94, 444, 449 Fibonacci recurrence, 445 Fibonacci trees, 246 Finite state machines, 521 accepting state of, 523 definition of, 523 initial state of, 523 state table for, 521 transition diagram for, 522 with output, 525 First-order linear difference equation, 441, 444 formula for nth term, 442 Floor, 456, 464 Flow, 317-352 along an arc, 319 augmentation algorithm, 330 augmenting path, 321, 328-331 in a network, 319 integral, 337, 350
maximal, 320 value of, 320, 325, 341-345 Ford, L. R., 291, 313, 328-329, 344, 356 For .. endfor, 568
Formal power series, 470 Four-color theorem, 154 Four-queens problem, 217-219 Frege, G., 555 Fulkerson, D. R., 291, 313, 328-329, 344, 356 Function, 75-85, 88 codomain of, 76 composition of, 82 domain of, 76 exponential, 84 generating, 465-481 identity, 79 image of an element, 76 inverse of, 83 logarithmic, 84 one-to-one, 79-81, 88 one-to-one correspondence, 79 onto, 79-81, 404 range of, 79 Gambler's ruin, 450 Gauss, C. F., 106 Generating function, 465-481 algebra of, 473-481 inverse of, 474-476 Geometric progression, 96 common ratio of, 96 sum of terms, 96 Gbdel K., 555 Goldbach's conjecture, 473, 535 Graph, 108-168, see also Directed graph, Directed multigraph, Multigraph, and Tree, 176 adjacency list for, 207 adjacency matrix for, 111 -112, 143-145 adjacent vertices of, 109 bipartite, 135, 278, 347 chromatic number of, 150 coloring of, 150 complement of, 176 complete, 110 complete bipartite, 135 connected, 121, 186, 198 covering of, 281 cycle in, 122 deficiency of, 356 directed, 159 distance in, 136, 140 edge of, 109 Euler circuit, 122-126 Euler circuit algorithm, 124-125 Euler path in, 122-126 Hamiltonian cycle in, 126-130 Hamiltonian path in, 126-130
617
isomorphism of, 113-115 labeled, 111 length of a path in, 120 matching of, 279 maximum matching of, 279 minimum covering of, 281 path in, 120-130 simple path in, 120, 187-190, 226 vertex of, 109 weighted, 139 Grassmann, H., 106 Gray code, 129-130 Greedy algorithm, 203, 209 Guthrie, F., 176 Haken, W., 154, 176 Hall, P., 274, 313 Hall's condition, 277 Hall's theorem, 274, 277, 300 Hamilton, W., 106 Hamilton, W. R., 126, 134, 176 Hamiltonian cycle, 126-130 directed, 166 Hamiltonian path, 126-130 Hanoi, Tower of, 419, 429, 476 Hasse, H., 68 Hasse diagram, 68 Height of a binary tree, 269 Homer, W. G., 27 Homer's polynomial evaluation algorithm, 27 Huffman, D. A., 249 Huffman's optimal binary tree algorithm, 249-250,263 Hungarian algorithm, 308 Hungarian method, 305-309 Huygens, C., 411 Hydrocarbons, 184-185, 190 Hypothesis of a theorem, 549 Identity function, 79 If and only if, 536-541 If .. otherwise .. endif, 568 If . . then .. ,536-539
Image of a function, 76 Implication, see Conditional statement, 539 Inclusion-exclusion, principle of, 395-401 Indegree of vertex, 160 Independent set algorithm, 290 Independent set of entries, 280 maximum, 281 with maximum sum, 311 Induction, 88-95 inductive step, 89 principle of mathematical induction, 88 strong principle of mathematical induction, 94
618
Index
Inductive step, 89 Initial conditions, 418 Initial state, 523, 526 Inorder traversal algorithm, 242 Inorder traversal listing, 242-244, 260 Insertion of a vertex in a binary search tree, 259 Integers, 55-59 congruence of, 55-59 division of, 55 prime, 50, 103 relatively prime, 403 Integral flow, 337, 350 Internal vertex, 226 International Standard Book Number, 56 Intersection of sets, 41-44, 393-401 Invariant of a graph isomorphism, 114 Inverse function, 83 Inverse of a statement, 541 ISBN, 56 Isomorphism of graphs, 113-115 of weighted graphs, 148 Iteration, 429-439, 441 Jevons, W. S., 530 Jordan, C., 266 Kamaugh, M., 530 Kamaugh map, 508 adjacent cells in, 510 Karp, R., 328, 344 Kirchhoff, G., 184, 265 Knapsack problem, 16, 22 Kdnig, D., 176, 298, 309, 313 Konig's theorem, 298 Kbnigsberg bridge problem, 133 Kruskal, J. B., 203, 210, 266 Kruskal's algorithm for a minimal spanning tree, 210 Kuhn, H. W., 309 Labeled graph, 111 directed, 160 Labeling of a network, 331 Labeling of lines, 287 Lagrange, J. L., 106 Laplace, P. S., 387, 484 Latin rectangle, 277 Latin square, 277 Left child, 232 Left subtree, 233 Leibniz, G. W., 106, 530, 555, 564 Length of a directed path, 162 Length of a path, 120, 162 Level of a vertex, 232 Lexicographic order, 65,405 Line of a matrix, 280 Linear order, see Total order, 66 List of adjacent vertices, 112
Lists, searching and suiting of, 31-33, 453-463, 46'; Bose-Nelson algorithm, 465 bubble sort, 31--33. z55 merge sort, 461-463 Liu, C. L., 165 Logarithmic function, 84 Logical equivalence, 5416 Logical gates, 489 -493 AND-, 490 NOT-, 490 OR-, 490 Loop in a multigraph 119 directed, 161 Looping in algorithms, 566-568 Loop invariant, 91 Lucas sequence, 485 Lucas, E., 484 Lukasiewicz, J., 237-239 Machines parity checking, 522 unit delay, 526 Marquand, A., 530 Matching maximum, 348 of a graph, 279. 347 problem, 10-12, 347 Mathematical induction, principle of, 88 Matrix, 111, 558-562 adjacency, 111 --112, 143-145 column, 558 covering in a, 282 entry, 558 equality, 558 i, j entry, 111,558 independent entries of, 280 line of, 280 maximum independent set in, 281 of a bipartite graph, 280 product, 144-145, 5(0-562 row, 558 submatrix of, 286 sum,559 Maurocyulus, F., 107 Max-flow min-cut theorem, 345 Maximal element, 66 Maximal flow, 32(0 Maximal spanning tree. 204 Maximum independent set, 281 Maximum matching, 279, 348 Maximum sum of independent sets, 311 McCluskey, E., 530 Merge sort algorithm. 461-463 Merging algorithm, 41(' Merging lists, 96-10, .161-463 Method of iteration. 429-439, 441 Microprocessor, 489 Minimal cut, 341-34'5
Minimal element, 66 Minimal-length path, 136 Minimal spanning tree, 199 Kruskal's algorithm, 210 Prim's algorithm, 201, 205 Minimum covering in a matrix, 282 of a graph, 281 Minterm, 503 Modus ponens, 548 Modus tollens, 548 Multigraph, 119-130, see also Directed Multigraph, 168 connected, 121 cycle in, 122 Euler circuit algorithm, 124-125 Euler circuit in, 122-126 Euler path in, 122-126 length of path in, 120 loop in, 119 parallel edges in, 119 path in, 120-130 simple path in, 120 Multinomial theorem, 414 Multiplication of matrices, 144- 145 Multiplication principle, 12, 366-369 Multisource transportation network, 357 n-bit string, 367 Negation of a statement, 537 Nelson, R. J., 465 Network, 317-345 arc in, 318 associated with a bipartite graph, 348 capacity of a cut, 323, 341-345 capacity of an arc, 318 cut in, 323 flow along an arc in, 319 flow augmentation algorithm, 330 flow-augmenting path in, 321, 331 flow in, 319 flows, 317-345 flows and matchings, 347-352 integral flow, 337, 350 labeling of, 331 max-flow min-cut theorem, 345 maximal flow, 320, 350 minimal cut in, 341-345 multisource transportation, 357 scanning of, 331 sink in, 318 source in, 317 transportation, 317-345 value of a flow in, 320 with vertex capacities, 358 Nickel, L., 103 Noll, C., 103 Not connective, 536, 537 NOT-gate, 490 Numbering for depth-first search, 215
Index One-to-one correspondence, 79 One-to-one function, 79-81, 88 Onto function, 79-81, 404 Optimal binary tree, 246-255 Huffman's algorithm, 249-250, 263 Optimization problem, 1, 11, 19 Or connective, 536 Order dictionary, see Order, lexicographic, 405 lexicographic, 65, 405 linear, see Order, total, 66 of an algorithm, 27 partial, 63-72 topological sorting, 69-72 total, 66, 69-72 Ordered pair, 45 OR-gate, 490 Outdegree of a vertex, 160 Output of a finite state machine, 525 string, 526 table, 526 Parallel edges, 119 directed, 161 Parent of a vertex, 226 Parity-checking machine, 522 Partial order, 63-72 Partition of a set, 52-53, 404 Pascal, B., 107, 361, 387,411 Pascal's triangle, 361 Path, 120-130, 136-145, see also Directed path, 168 critical, 6 Euler, 122-126 Euler circuit algorithm, 124-125 Hamiltonian, 126-130 length of, 120 minimal-length, 136 number of, 143-145 shortest, 136-143 simple, 120, 187-190 weight of, 139 Peacock, G., 106 Peano, G., 106, 555 Peirce, C. S., 530, 555 Permutation, 13, 374-375, 405-406 algorithm for lexicographic ordering of, 407 lexicographic ordering of, 405-406 of n objects taken r at a time, 14 ordered lists, 374-378 r-permutation, 374 PERT, 6, 36 Pigeonhole principle, 365-366 Plato, 554 Polish notation, 237-239 Polynomial, 26-28 evaluation algorithm, 26
Homer's algorithm, 27 of degree n in x, 26 revised polynomial evaluation algorithm, 35 Postfix form, 239 Postorder traversal algorithm, 240 Postorder traversal listing, 240-241 reverse Polish notation, 239 Prefix form, 237 Prefix property for codewords, 247-248 Premise, 539 Preorder listing, 236 Preorder traversal, 236 Preorder traversal algorithm, 236 Prim, R. C., 200, 266 Prime number, 50, 103 relatively, 403 Prim's algorithm for a minimal spanning tree, 201, 205 Principle of inclusion-exclusion, 395-401 Probability, 387-391 event, 387 experiment, 387 sample space, 387 Product of matrices, 144-145, 560-562 Progression, 96 Proof, 549 by cases, 552 by contradiction, 551 direct, 549 of the contrapositive, 550 syllogistic, 550 Prufer's algorithm, 193 Quantifiers, 536-538 Quine, W., 530 Quotient, 55 recombination, 375, 408 lexicographic ordering of, 408 r-permutation, 374 Range of a function, 79 Recurrence relation, 417-463 initial conditions of, 418 Recursion, 236,417-427 Recursive definition, 93-94 Reflexive relation, 49, 64 Rejected string, 523 Relation, 48-53 antisymmetric, 63-66 congruence, 55-59 directed multigraph of, 168 equivalence, 50-53 function, 75-85 linear, 66 maximal element, 66-67 minimal element, 66-67 on a set, 49 partial order, 63-72
619
reflexive, 49, 64 root, 224 symmetric, 49 topological sorting, 69-72 total, 66 transitive, 49, 64 Relatively prime integer, 403 Remainder, 55 Rencontre, 403 Repeat .. until, 567 Reverse Polish notation, 239-240 Rhind Papyrus, 35 Right child, 232 Right subtree, 233 Rivest, R. L., 103 Root, 224, 264 Rooted tree, 223-229 ancestor in, 226 binary, 232 child in, 226 descendants in, 226 expression, 232-235 internal vertex in, 226 parent in, 226 root in, 224 terminal vertex of, 226 Row of a matrix, 558 RSA-cryptography, 103 Russell, B., 106, 555 Sample space, 387 Scanning a network, 331 Scanning lines, 287 Schrdder, E., 530, 555 Score in a tournament, 172 Searching, 97-98, 453-463 Second-order homogeneous linear difference equations, 444-451 auxiliary equation for, 448 formula for nth term, 446, 447 Selections, 374, 379-385 Sentence, 535-542 contradiction, 545 logical equivalence, 546 tautology, 545 variable, 542 Sequence, 417 Sequential search algorithm, 454 Set, 19, 46 Cartesian product, 45-46 complement, 42 containment, 20 De Morgan's laws, 44 difference, 41-42 disjoint, 41 element of, 19 empty, 21 equality, 21 intersection, 41-44 number of elements in, 21, 393-401
620
Index
Set (Cont.) number of subsets, 21-22, 100 operations, 46 partition of, 52-53 subset, 20 union, 40-44 universal, 42 Venn diagram, 42 Shamir, A., 103 Shannon, C., 344, 530 Sheffer stroke, 548 Shortest path, 136-143, 198 Shortest path tree, 198 Sieve of Eratosthenes, 35, 403 Simple directed path, 162, 226 Simple path, 120, 187-190 Sink, 318 Sorting algorithms Bose-Nelson, 465 bubble sort, 31-33 efficiency of, 454-455, 463 merge sort, 461-463 Source, 317 Spanning tree, 194-205, 215-216 Kruskal's algorithm, 210 maximal, 204 minimal, 199 Prim's algorithm, 201 Stack, 424 Statement, 535-543 contrapositive of, 541 converse of, 541 inverse of, 541 variable, 542 State table, 521 Stirling, J., 404, 411 Stirling numbers of the second kind, 404 Stirling's formula, 411 String, 523 accepted, 523 n-bit, 367 output, 526 rejected, 523 Strong principle of mathematical induction, 94 Strongly connected digraph, 162, 215-216 bridge in, 163, 215 Submatrix, 286 Subset, 20 next subset algorithm, 29 Sum of matrices, 559 Syllogism argument by, 550 disjunctive, 548 law of, 550
Sylvester, J. J., 266, 412 564 Symmetric relation, 4S System of distinct representatives, 272 Table output, 526 state, 521 truth, 491,538-546 Tartaglia, N., 411 Tautology, 545 Terminal vertex, 226, 264 Terms of a sequence. 4 17 Theorem, 548 conclusion of, 549 hypothesis of, 549 proof of, 549 Topological sorting, 60 72 Topological sorting algorithm, 70 Total order, 66, 69- 72 Tournament, 167 score in, 172 transitive, 181 Tower of Hanoi, 419, L.23, 476 Transition diagram 522 Transitive relation, 49, 64 Transitive tournament, IS3 Traversals, 235-244 inorder, 242-244, 260 postorder, 240 preorder, 236 Tree, 184-261 balanced binary, 269 binary, 232 binary search tree construction algorithm, 257-258 binary search tree sei ch algorithm,
259 child in, 226, 232 decision, 228 depth-first search algorithm, 214 depth-first search numbering, 215 descendant in, 226 distinct, 193 edges in, 215 expression, 232-235 Fibonacci, 246 Huffman's optimal binary tree algorithm, 249-250, 263 internal vertex in, 226 Kruskal's algorithm. 203, 210 level of vertex in, 232 maximal spanning, 204 minimal spanning, 19') optimal binary, 246-255 parent in, 226 Prim's algorithm, 2(1 205 Prufer's algorithm, 193
rooted, 223-229 shortest path in, 198 spanning, 194-205, 215-216 terminal vertex in, 226, 264 traversal, 235-244 weight of, 199 Tree edge, 215 Truth table, 491, 538-546 for AND connective, 491, 538 for biconditional statements, 540 for Boolean expressions, 491 for conditional statements, 540 for NOT connective, 491, 538 for OR connective, 490, 539 Union of sets, 40-44, 369, 371, 393-401 Unit delay machine, 526 Universal product code, 61 Universal set, 42 Value of a flow, 320 Variables Boolean, 490 sentence, 542 Veblen, O., 266 Venn, J., 106 Venn diagram, 42 Vertex, 109, 119, 159 adjacent, 109 degree of, 110 incident with an edge, 109 indegree of, 160 insertion of, 259 internal, 226 level of, 232 outdegree of, 160 terminal, 226, 264 visiting, 235 Visiting a vertex, 235 Viete, F., 35 Von Staudt, G. K. C., 265 Weight, 248 of a binary tree, 248 of an edge, 139 of a path, 139 of a tree, 199 Weighted graph, 139 distance in, 140 isomorphism of, 148 shortest path in, 140 Well-ordering principle, 105 While .. endwhile, 566
Whitehead, A. N., 106, 555 Xenocrates, 411
Index of Algorithms ALGEBRAIC AND COMBINATORIAL ALGORITHMS Algorithm for Evaluating 25 Algorithm for Evaluating n! 34 Algorithm for Evaluating P(n, r) 40 Algorithm for the Lexicographic Ordering of Permutations 423 Algorithm for the Lexicographic Ordering of r-Combinations 426 Algorithm for the Sum or an Arithmetic Progression 35 Algorithm for the Sum o-&a Geometric Progression 35 Algorithm for Evaluating the nth Fibonacci Number 35 Horner's Polynomial Evailiation Algorithm 28 Next Subset Algorithm 29 Polynomial Evaluation Algorithm 26 Revised Polynomial Evaluation Algorithm 36 'n
GRAPH-THEORETIC ALGORITHMS Breadth-First Search Algorithm 145 Depth-First Search Algorithm 223 Dijkstra's Algorithm 148 Euler Circuit Algorithm 131 Flow Augmentation Algorithm 344 Huffman's Optimal Bina y Tree Algorithm Hungarian Algorithm 322 Independent Set Algorithm 304 Inorder Traversal Algorithm 252 Kruskal's Algorithm 2193 Postorder Traversal Algorithm 250 Preorder Traversal Algorithm 246 Prim's Algorithm 210
261

<>

<>