DISCRETE MATHEMATICS IN COMPUTER SCIENCE DONALD F. STANAT Department of Computer Science University of North Carolina at Chapel Hill DAVID F. McALLISTER Department of Computer Science North Carolina State University Prentice/Hall International, Inc. Library of Congress Cataloging in Publication Data STANAT, DONALD F (date) Discrete mathematics in computer science. Bibliography: p. Includes index. 1. Mathematics—1961~- 2. Electronic data processing. I. McAllister, David F. (date) joint author. II. Title. QA39.2.8688 S121 76-48915 ISBN 0-13-216052-8 This edition may be sold only in those countries to which it is consigned by Prentice-Hall International. It is not to be re-exported and it is not for sale in the U.S.A., Mexico or Canada. © 1977 by Prentice-Hall, Inc., Englewood Cliffs, N.J. All rights reserved. No part of this book may be reproduced in any form, by mimeograph or any other means, without permission in writing from the publisher. 10 9 8 7 6 5 4 3 2 #1 ISBN Q-13-21605ce-4 Printed in the United States of America Prentice-Hall International (UK) Limited, London Prentice-Hall of Australia Pty. Limited, Sydney Prentice-Hall Canada Inc., Toronto Prentice-Hall Hispanoamericana, $.A., Mexico Prentice-Hall of India Private Limited, New Delhi Prentice-Hall of Japan, Inc., Tokyo Prentice-Hall of Southeast Asia Pte. Ltd., Singapore Editora Prentice-Hall do Brasil, Ltda., Rio de Janeiro Whitehall Books Limited, Wellington, New Zealand Prentice-Hall, Englewood Cliffs, New Jersey To Sylvia and Beth CONTENTS PREFACE x Notation xiii O MATHEMATICAL MODELS 1 0.0 Introduction 1 0.1 Principles and Models 1 0.2 Mathematical Models 2 0.3 Purposes of Models 6 1 MATHEMATICAL REASONING 8 1.0 Introduction 8 Propositions 9 Predicates and Quantifiers 20 3 Quantifiers and Logical Operators 29 .4 Logical Inference 39 5 Methods of Proof 47 6 Program Correctness 57 Axioms of Assignment 69 2 SETS 75 2.0 Introduction 75 2.1 The Primitives of Set Theory 75 {2.2 The Paradoxes of Set Theory 79 2.3 Relations Between Sets 82 {Denotes optional section CONTENTS vi 2.4 Operations on Sets 85 2.5 Induction 95 Inductive Definition of Sets 95 Recursive Procedures 98 Inductive Proofs 100 {2.6 The Natural Numbers 108 2.7 Set Operations on &* iil 3 BINARY RELATIONS 120 Introduction 120 Binary Relations and Digraphs 120 Trees 131 Search Trees 136 Tree Traversal Algorithms 140 Special Properties of Relations 145 Composition of Relations 149 Closure Operations on Relations 155 Order Relations 164 {Some Additional Concepts for Posets 173 Equivalence Relations and Partitions 178 {Sums and Products of Partitions 187 4 FUNCTIONS 193 4.0 Introduction 193 4.1 Basic Properties of Functions 193 Inductively Defined Functions 199 Partial Functions 201 4.2 Special Classes of Functions 204 Inverse Functions 209 {One-Sided Inverse Functions 213 5 COUNTING AND ALGORITHM ANALYSIS 218 3.0 Introduction 218 5.1 Basic Counting Techniques 218 Permutations and Combinations 222 Decision Trees 225 5.2 Asymptotic Behavior of Functions 232 Some Important Classes of Asymptotic Behavior 236 5.3 Recurrence Systems 243 Divide and Conquer Algorithms 248 5.4 Analysis of Algorithms 258 Searching Algorithms 262 Sorting Algorithms 265 CONTENTS vii 6 INFINITE SETS 275 Introduction 275 Finite and Infinite Sets 275 Countable and Uncountable Sets 279 Comparison of Cardinal Numbers 288 Cardinal Arithmetic 295 7 ALGEBRAS 300 Introduction 300 The Structure of Algebras 30] Some Varieties of Algebras 309 Semigroups 309 Monoids 310 Groups 311 Boolean Algebras 312 73 Homomorphisms 315 Congruence Relations 322 7.5 New Algebraic Systems from Old 327 Quotient Algebras 327 Product Algebras 329 APPENDIX: THE PROGRAMMING LANGUAGE 332 ANSWERS TO SELECTED EXERCISES 339 BIBLIOGRAPHY 391 INDEX 393 PREFACE This text is intended for use in a first course in discrete mathematics in an undergraduate computer science curriculum. The level is appropriate for a sopho- more or junior course. The student is assumed to have experience with a high-level programming language. No specific mathematics is prerequisite, but some previous exposure to college-level mathematics is desirable. The mathematics taught to students of computer science has changed radically since the early days of this academic discipline. Initially, nearly all topics were drawn from electrical engineering and numerical analysis. Over the years, however, the mathematics of computer science has developed a distinct character, incorpo- rating and melding aspects from such areas as logic, universal algebra and combi- natorics as well as analysis. Moreover, as the field has evolved, its use of mathematics has become more sophisticated. It is our view that a computer scien- tist must have substantial training in mathematics if he is to understand his tools and use them well. The purpose of this text is to provide a foundation for the discrete mathematics used in the theory and application of computer science. The major part of this book treats classical mathematical topics, including sets, relations, functions, cardinality, and algebra. The approach, however, is not classical; we have emphasized the topics of importance to computer science and provided examples to illustrate why the material is of interest. The first two topics of the text are usually not treated explicitly in a course of this type. Chapter 0 is a brief description of the nature and purpose of mathematical models. Chapter | treats mathematical reasoning, including the representation of assertions, how inferences are made, and how assertions are proved. The final section of the chapter is a description of how programs can be proved correct. The material of Chapter 1 is difficult for some students, especially those who have not had some previous experience in proving theorems in a college-level mathematics course. For this reason, many of the proofs in succeeding chapters are presented in vili